Elffuss BraidHub

kikocis/continue public

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