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