configJson to configYaml converter

This commit is contained in:
Nate 2025-01-05 22:46:24 -05:00
parent 8915a53321
commit fcdc63efda
5 changed files with 164 additions and 4 deletions

View file

@ -1,14 +1,15 @@
{
"name": "@continuedev/config-yaml",
"version": "1.0.0",
"version": "1.0.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@continuedev/config-yaml",
"version": "1.0.0",
"version": "1.0.7",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/config-types": "file:../config-types",
"yaml": "^2.6.1",
"zod": "^3.23.8"
},
@ -18,6 +19,16 @@
"ts-node": "^10.9.2"
}
},
"../config-types": {
"version": "1.0.13",
"license": "Apache-2.0",
"dependencies": {
"zod": "^3.23.8"
},
"devDependencies": {
"typescript": "^5.5.2"
}
},
"node_modules/@ampproject/remapping": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz",
@ -575,6 +586,10 @@
"dev": true,
"peer": true
},
"node_modules/@continuedev/config-types": {
"resolved": "../config-types",
"link": true
},
"node_modules/@cspotcode/source-map-support": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",

View file

@ -1,6 +1,6 @@
{
"name": "@continuedev/config-yaml",
"version": "1.0.4",
"version": "1.0.8",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@ -13,7 +13,8 @@
"license": "Apache-2.0",
"dependencies": {
"yaml": "^2.6.1",
"zod": "^3.23.8"
"zod": "^3.23.8",
"@continuedev/config-types": "file:../config-types"
},
"devDependencies": {
"cross-env": "^7.0.3",

View file

@ -0,0 +1,141 @@
import { ConfigJson } from "@continuedev/config-types";
import { ConfigYaml } from "./schemas/index.js";
import { ModelRoles } from "./schemas/models.js";
type ModelYaml = NonNullable<ConfigYaml["models"]>[number];
type ContextYaml = NonNullable<ConfigYaml["context"]>[number];
type PromptYaml = NonNullable<ConfigYaml["prompts"]>[number];
function convertModel(
m: ConfigJson["models"][number],
roles: ModelRoles[],
): ModelYaml {
return {
name: m.title,
provider: m.provider,
model: m.model,
apiKey: m.apiKey,
apiBase: m.apiBase,
roles,
requestOptions: m.requestOptions,
defaultCompletionOptions: m.completionOptions,
};
}
function convertEmbeddingsProvider(
m: NonNullable<ConfigJson["embeddingsProvider"]>,
): ModelYaml {
return {
name: "Embeddings Model",
provider: m.provider,
model: m.model ?? "",
apiKey: m.apiKey,
apiBase: m.apiBase,
roles: ["embed"],
};
}
function convertReranker(m: NonNullable<ConfigJson["reranker"]>): ModelYaml {
return {
name: "Reranker",
provider: m.name,
model: m.params?.model ?? "",
apiKey: m.params?.apiKey,
apiBase: m.params?.apiBase,
roles: ["rerank"],
};
}
function convertContext(configJson: ConfigJson): ContextYaml[] {
const context: ContextYaml[] =
configJson.contextProviders?.map((ctx) => {
return {
uses: `builtin/${ctx.name}`,
with: ctx.params,
};
}) ?? [];
return context;
}
function convertCustomCommand(
cmd: NonNullable<ConfigJson["customCommands"]>[number],
): PromptYaml {
return {
name: cmd.name,
description: cmd.description,
prompt: (cmd as any).prompt, // The type is wrong in @continuedev/config-types
type: "slash-command",
};
}
function convertMcp(mcp: any): NonNullable<ConfigYaml["mcpServers"]>[number] {
const { transport } = mcp;
const { command, args, env } = transport;
return {
command,
args,
env,
name: "MCP Server",
};
}
function convertDoc(doc: NonNullable<ConfigJson["docs"]>[number]): any {
// NonNullable<ConfigYaml["docs"]>[number]
return {
title: doc.title,
startUrl: doc.startUrl,
rootUrl: doc.rootUrl,
faviconUrl: doc.faviconUrl,
};
}
export function convertJsonToYamlConfig(configJson: ConfigJson): ConfigYaml {
// models
const models = configJson.models.map((m) => convertModel(m, ["chat"]));
const autocompleteModels = Array.isArray(configJson.tabAutocompleteModel)
? configJson.tabAutocompleteModel
: configJson.tabAutocompleteModel
? [configJson.tabAutocompleteModel]
: [];
models.push(
...autocompleteModels.map((m) => convertModel(m, ["autocomplete"])),
);
if (configJson.embeddingsProvider) {
models.push(convertEmbeddingsProvider(configJson.embeddingsProvider));
}
if (configJson.reranker) {
models.push(convertReranker(configJson.reranker));
}
// context
const context = convertContext(configJson);
// mcpServers
// Types for "experimental" don't exist
const mcpServers = (
configJson as any
).experimental?.modelContextProtocolServers?.map(convertMcp);
// prompts
const prompts = configJson.customCommands?.map(convertCustomCommand);
// docs
const docs = configJson.docs?.map(convertDoc);
const configYaml: ConfigYaml = {
name: "Continue Config",
version: "0.0.1",
models,
context,
rules: configJson.systemMessage,
prompts,
mcpServers,
// docs
};
return configYaml;
}

View file

@ -91,6 +91,7 @@ export function fillTemplateVariables(
});
}
export { convertJsonToYamlConfig } from "./converter.js";
export { ConfigYaml, configYamlSchema } from "./schemas/index.js";
export type { ModelConfig } from "./schemas/models.js";
export { validateConfigYaml } from "./validation.js";

View file

@ -6,6 +6,7 @@ import { modelSchema } from "./models.js";
const packageSchema = z.object({
uses: z.string(),
with: z.any().optional(),
secrets: z.array(z.string()).optional(),
});
const toolSchema = z.object({
@ -33,6 +34,7 @@ const promptSchema = z.object({
export const configYamlSchema = z.object({
name: z.string(),
version: z.string(),
packages: z.array(packageSchema).optional(),
models: z.array(modelSchema).optional(),
context: z.array(contextSchema).optional(),