| 1 | import { ConfigJson } from "@continuedev/config-types"; |
| 2 | import { ConfigYaml } from "./schemas/index.js"; |
| 3 | import { ModelRole } from "./schemas/models.js"; |
| 4 | |
| 5 | type ModelYaml = NonNullable<ConfigYaml["models"]>[number]; |
| 6 | type ContextYaml = NonNullable<ConfigYaml["context"]>[number]; |
| 7 | type PromptYaml = NonNullable<ConfigYaml["prompts"]>[number]; |
| 8 | |
| 9 | function convertModel( |
| 10 | m: ConfigJson["models"][number], |
| 11 | roles: ModelRole[], |
| 12 | ): ModelYaml { |
| 13 | return { |
| 14 | name: m.title, |
| 15 | provider: m.provider, |
| 16 | model: m.model, |
| 17 | apiKey: m.apiKey, |
| 18 | apiBase: m.apiBase, |
| 19 | roles, |
| 20 | requestOptions: m.requestOptions, |
| 21 | defaultCompletionOptions: m.completionOptions, |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | function convertEmbeddingsProvider( |
| 26 | m: NonNullable<ConfigJson["embeddingsProvider"]>, |
| 27 | ): ModelYaml { |
| 28 | return { |
| 29 | name: "Embeddings Model", |
| 30 | provider: m.provider, |
| 31 | model: m.model ?? "", |
| 32 | apiKey: m.apiKey, |
| 33 | apiBase: m.apiBase, |
| 34 | roles: ["embed"], |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | function convertReranker(m: NonNullable<ConfigJson["reranker"]>): ModelYaml { |
| 39 | return { |
| 40 | name: "Reranker", |
| 41 | provider: m.name, |
| 42 | model: m.params?.model ?? "", |
| 43 | apiKey: m.params?.apiKey, |
| 44 | apiBase: m.params?.apiBase, |
| 45 | roles: ["rerank"], |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | function withFromContextProvider( |
| 50 | contextProvider: NonNullable<ConfigJson["contextProviders"]>[number], |
| 51 | ): Record<string, string> | undefined { |
| 52 | const { name, params } = contextProvider; |
| 53 | |
| 54 | switch (name) { |
| 55 | case "greptile": |
| 56 | return { |
| 57 | GITHUB_TOKEN: params?.GithubToken ?? "", |
| 58 | GREPTILE_TOKEN: params?.GreptileToken ?? "", |
| 59 | }; |
| 60 | case "jira": |
| 61 | return { |
| 62 | JIRA_TOKEN: params?.JiraToken ?? "", |
| 63 | JIRA_API_VERSION: params?.JiraEmail, |
| 64 | JIRA_DOMAIN: params?.JiraDomain ?? "", |
| 65 | }; |
| 66 | case "postgres": |
| 67 | return { |
| 68 | POSTGRES_HOST: params?.host, |
| 69 | POSTGRES_PORT: params?.port, |
| 70 | POSTGRES_USER: params?.user, |
| 71 | POSTGRES_PASSWORD: params?.password, |
| 72 | POSTGRES_DATABASE: params?.database, |
| 73 | POSTGRES_SCHEMA: params?.schema, |
| 74 | }; |
| 75 | case "gitlab-mr": |
| 76 | return { |
| 77 | GITLAB_TOKEN: params?.token, |
| 78 | DOMAIN: params?.domain, |
| 79 | }; |
| 80 | case "discord": |
| 81 | return { |
| 82 | DISCORD_KEY: params?.discordKey, |
| 83 | DISCORD_GUILD_ID: params?.guildId, |
| 84 | DISCORD_CHANNELS: params?.channels, |
| 85 | }; |
| 86 | case "commits": |
| 87 | return { |
| 88 | DEPTH: params?.Depth, |
| 89 | LAST_N_COMMITS_DEPTH: params?.LastXCommitsDepth, |
| 90 | }; |
| 91 | default: |
| 92 | return undefined; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | function convertContext(configJson: ConfigJson): ContextYaml[] { |
| 97 | const context: ContextYaml[] = |
| 98 | configJson.contextProviders?.map((ctx) => { |
| 99 | // ctx providers that weren't given official blocks |
| 100 | if ( |
| 101 | ["web", "debugger", "issue", "database", "google", "http"].includes( |
| 102 | ctx.name, |
| 103 | ) |
| 104 | ) { |
| 105 | return { |
| 106 | provider: ctx.name, |
| 107 | params: ctx.params, |
| 108 | }; |
| 109 | } |
| 110 | return { |
| 111 | uses: `continuedev/${ctx.name === "open" ? "open-files" : ctx.name}-context`, |
| 112 | with: ctx.params, |
| 113 | }; |
| 114 | }) ?? []; |
| 115 | |
| 116 | return context; |
| 117 | } |
| 118 | |
| 119 | function convertCustomCommand( |
| 120 | cmd: NonNullable<ConfigJson["customCommands"]>[number], |
| 121 | ): PromptYaml { |
| 122 | return { |
| 123 | name: cmd.name, |
| 124 | description: cmd.description, |
| 125 | prompt: (cmd as any).prompt, // The type is wrong in @continuedev/config-types |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | function convertMcp(mcp: any): NonNullable<ConfigYaml["mcpServers"]>[number] { |
| 130 | const { transport } = mcp; |
| 131 | const { command, args, env, server_name } = transport; |
| 132 | |
| 133 | return { |
| 134 | command, |
| 135 | args, |
| 136 | env, |
| 137 | name: server_name || "MCP Server", |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | function convertDoc( |
| 142 | doc: NonNullable<ConfigJson["docs"]>[number], |
| 143 | ): NonNullable<ConfigYaml["docs"]>[number] { |
| 144 | return { |
| 145 | name: doc.title, |
| 146 | startUrl: doc.startUrl, |
| 147 | rootUrl: doc.rootUrl, |
| 148 | faviconUrl: doc.faviconUrl, |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | export function convertJsonToYamlConfig(configJson: ConfigJson): ConfigYaml { |
| 153 | // models |
| 154 | const models = configJson.models.map((m) => convertModel(m, ["chat"])); |
| 155 | const autocompleteModels = Array.isArray(configJson.tabAutocompleteModel) |
| 156 | ? configJson.tabAutocompleteModel |
| 157 | : configJson.tabAutocompleteModel |
| 158 | ? [configJson.tabAutocompleteModel] |
| 159 | : []; |
| 160 | models.push( |
| 161 | ...autocompleteModels.map((m) => convertModel(m, ["autocomplete"])), |
| 162 | ); |
| 163 | |
| 164 | if (configJson.embeddingsProvider) { |
| 165 | models.push(convertEmbeddingsProvider(configJson.embeddingsProvider)); |
| 166 | } |
| 167 | |
| 168 | if (configJson.reranker) { |
| 169 | models.push(convertReranker(configJson.reranker)); |
| 170 | } |
| 171 | |
| 172 | // context |
| 173 | const context = convertContext(configJson); |
| 174 | |
| 175 | // mcpServers |
| 176 | // Types for "experimental" don't exist |
| 177 | const mcpServers = ( |
| 178 | configJson as any |
| 179 | ).experimental?.modelContextProtocolServers?.map(convertMcp); |
| 180 | |
| 181 | // prompts |
| 182 | const prompts = configJson.customCommands?.map(convertCustomCommand); |
| 183 | |
| 184 | // docs |
| 185 | const docs = configJson.docs?.map(convertDoc); |
| 186 | |
| 187 | const configYaml: ConfigYaml = { |
| 188 | name: "Continue Config", |
| 189 | version: "0.0.1", |
| 190 | models, |
| 191 | context, |
| 192 | rules: configJson.systemMessage ? [configJson.systemMessage] : undefined, |
| 193 | prompts, |
| 194 | mcpServers, |
| 195 | docs, |
| 196 | requestOptions: configJson.requestOptions, |
| 197 | }; |
| 198 | |
| 199 | return configYaml; |
| 200 | } |