Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 3.7 KiBRawPermalink
1import {
2 BlockType,
3 ConfigYaml,
4 createRuleMarkdown,
5 RULE_FILE_EXTENSION,
6} from "@continuedev/config-yaml";
7import * as YAML from "yaml";
8import { IDE } from "../..";
9import { joinPathsToUri } from "../../util/uri";
10
11const BLOCK_TYPE_CONFIG: Record<
12 BlockType,
13 { singular: string; filename: string }
14> = {
15 context: { singular: "context", filename: "context" },
16 models: { singular: "model", filename: "model" },
17 rules: { singular: "rule", filename: "rule" },
18 docs: { singular: "doc", filename: "doc" },
19 prompts: { singular: "prompt", filename: "prompt" },
20 mcpServers: { singular: "MCP server", filename: "mcp-server" },
21 data: { singular: "data", filename: "data" },
22};
23
24function getContentsForNewBlock(blockType: BlockType): ConfigYaml {
25 const configYaml: ConfigYaml = {
26 name: `New ${BLOCK_TYPE_CONFIG[blockType]?.singular}`,
27 version: "0.0.1",
28 schema: "v1",
29 };
30 switch (blockType) {
31 case "context":
32 configYaml.context = [
33 {
34 provider: "file",
35 },
36 ];
37 break;
38 case "models":
39 configYaml.models = [
40 {
41 provider: "anthropic",
42 model: "claude-3-7-sonnet-latest",
43 apiKey: "${{ secrets.ANTHROPIC_API_KEY }}",
44 name: "Claude 3.7 Sonnet",
45 roles: ["chat", "edit"],
46 },
47 ];
48 break;
49 case "rules":
50 configYaml.rules = ["Always give concise responses"];
51 break;
52 case "docs":
53 configYaml.docs = [
54 {
55 name: "New docs",
56 startUrl: "https://docs.continue.dev",
57 },
58 ];
59 break;
60 case "prompts":
61 configYaml.prompts = [
62 {
63 name: "New prompt",
64 description: "New prompt",
65 prompt:
66 "Please write a thorough suite of unit tests for this code, making sure to cover all relevant edge cases",
67 },
68 ];
69 break;
70 case "mcpServers":
71 configYaml.mcpServers = [
72 {
73 name: "New MCP server",
74 command: "npx",
75 args: ["-y", "<your-mcp-server>"],
76 env: {},
77 },
78 ];
79 break;
80 }
81
82 return configYaml;
83}
84
85function getFileExtension(blockType: BlockType): string {
86 return blockType === "rules" ? RULE_FILE_EXTENSION : "yaml";
87}
88
89export function getFileContent(blockType: BlockType): string {
90 if (blockType === "rules") {
91 return createRuleMarkdown("New Rule", "Your rule content", {
92 description: "A description of your rule",
93 });
94 } else {
95 return YAML.stringify(getContentsForNewBlock(blockType));
96 }
97}
98
99export async function findAvailableFilename(
100 baseDirUri: string,
101 blockType: BlockType,
102 fileExists: (uri: string) => Promise<boolean>,
103 extension?: string,
104): Promise<string> {
105 const baseFilename = `new-${BLOCK_TYPE_CONFIG[blockType]?.filename}`;
106 const fileExtension = extension ?? getFileExtension(blockType);
107 let counter = 0;
108 let fileUri: string;
109
110 do {
111 const suffix = counter === 0 ? "" : `-${counter}`;
112 fileUri = joinPathsToUri(
113 baseDirUri,
114 `${baseFilename}${suffix}.${fileExtension}`,
115 );
116 counter++;
117 } while (await fileExists(fileUri));
118
119 return fileUri;
120}
121
122export async function createNewWorkspaceBlockFile(
123 ide: IDE,
124 blockType: BlockType,
125): Promise<void> {
126 const workspaceDirs = await ide.getWorkspaceDirs();
127 if (workspaceDirs.length === 0) {
128 throw new Error(
129 "No workspace directories found. Make sure you've opened a folder in your IDE.",
130 );
131 }
132
133 const baseDirUri = joinPathsToUri(workspaceDirs[0], `.continue/${blockType}`);
134
135 const fileUri = await findAvailableFilename(
136 baseDirUri,
137 blockType,
138 ide.fileExists.bind(ide),
139 );
140
141 const fileContent = getFileContent(blockType);
142
143 await ide.writeFile(fileUri, fileContent);
144 await ide.openFile(fileUri);
145}