config yaml package resolution
This commit is contained in:
parent
9255f14c42
commit
e844b98a67
10 changed files with 4226 additions and 0 deletions
2
packages/config-yaml/.npmignore
Normal file
2
packages/config-yaml/.npmignore
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
.env
|
||||
test
|
||||
20
packages/config-yaml/jest.config.mjs
Normal file
20
packages/config-yaml/jest.config.mjs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
export default {
|
||||
transform: {
|
||||
"\\.[jt]sx?$": ["ts-jest", { useESM: true }],
|
||||
},
|
||||
|
||||
moduleNameMapper: {
|
||||
"(.+)\\.js": "$1",
|
||||
},
|
||||
extensionsToTreatAsEsm: [".ts"],
|
||||
preset: "ts-jest/presets/default-esm",
|
||||
testTimeout: 10000,
|
||||
testEnvironment: "node",
|
||||
globals: {
|
||||
__dirname: path.dirname(fileURLToPath(import.meta.url)),
|
||||
__filename: path.resolve(fileURLToPath(import.meta.url)),
|
||||
},
|
||||
};
|
||||
4041
packages/config-yaml/package-lock.json
generated
Normal file
4041
packages/config-yaml/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
23
packages/config-yaml/package.json
Normal file
23
packages/config-yaml/package.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "@continuedev/config-yaml",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
|
||||
"build": "tsc"
|
||||
},
|
||||
"author": "Continue Dev, Inc.",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"yaml": "^2.6.1",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cross-env": "^7.0.3",
|
||||
"ts-jest": "^29.2.3",
|
||||
"ts-node": "^10.9.2"
|
||||
}
|
||||
}
|
||||
67
packages/config-yaml/src/index.ts
Normal file
67
packages/config-yaml/src/index.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import * as YAML from "yaml";
|
||||
import { ConfigYaml, configYamlSchema } from "./schemas/index.js";
|
||||
|
||||
const REGISTRY_URL = "https://registry.continue.dev";
|
||||
const LATEST = "latest";
|
||||
|
||||
function parseUses(uses: string): {
|
||||
owner: string;
|
||||
packageName: string;
|
||||
version: string;
|
||||
} {
|
||||
const [owner, packageNameAndVersion] = uses.split("/");
|
||||
const [packageName, version] = packageNameAndVersion.split("@");
|
||||
return {
|
||||
owner,
|
||||
packageName,
|
||||
version: version ?? LATEST,
|
||||
};
|
||||
}
|
||||
|
||||
function extendConfig(config: ConfigYaml, pkg: ConfigYaml): ConfigYaml {
|
||||
return {
|
||||
...config,
|
||||
models: [...(config.models ?? []), ...(pkg.models ?? [])],
|
||||
context: [...(config.context ?? []), ...(pkg.context ?? [])],
|
||||
tools: [...(config.tools ?? []), ...(pkg.tools ?? [])],
|
||||
data: [...(config.data ?? []), ...(pkg.data ?? [])],
|
||||
mcpServers: [...(config.mcpServers ?? []), ...(pkg.mcpServers ?? [])],
|
||||
};
|
||||
}
|
||||
|
||||
export async function resolvePackages(
|
||||
configYaml: ConfigYaml,
|
||||
): Promise<ConfigYaml> {
|
||||
if (!configYaml.packages) return configYaml;
|
||||
|
||||
for (const pkgDesc of configYaml.packages) {
|
||||
const { owner, packageName, version } = parseUses(pkgDesc.uses);
|
||||
const downloadUrl = new URL(
|
||||
`/${owner}/${packageName}/${version}`,
|
||||
REGISTRY_URL,
|
||||
);
|
||||
const resp = await fetch(downloadUrl);
|
||||
if (!resp.ok) {
|
||||
throw new Error(
|
||||
`Failed to fetch package ${pkgDesc.uses} from registry: ${resp.statusText}`,
|
||||
);
|
||||
}
|
||||
const downloadBuf = await resp.arrayBuffer();
|
||||
const downloadStr = new TextDecoder().decode(downloadBuf);
|
||||
const pkg = YAML.parse(downloadStr);
|
||||
|
||||
const validatedPkg = configYamlSchema.parse(pkg);
|
||||
configYaml = extendConfig(configYaml, validatedPkg);
|
||||
}
|
||||
return configYaml;
|
||||
}
|
||||
|
||||
export function renderConfigYaml(configYaml: string): ConfigYaml {
|
||||
try {
|
||||
const parsed = YAML.parse(configYaml);
|
||||
const result = configYamlSchema.parse(parsed);
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(`Failed to parse config yaml: ${e.message}`);
|
||||
}
|
||||
}
|
||||
6
packages/config-yaml/src/schemas/context.ts
Normal file
6
packages/config-yaml/src/schemas/context.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const contextSchema = z.object({
|
||||
uses: z.string(),
|
||||
with: z.any().optional(),
|
||||
});
|
||||
5
packages/config-yaml/src/schemas/data.ts
Normal file
5
packages/config-yaml/src/schemas/data.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const dataSchema = z.object({
|
||||
provider: z.string(),
|
||||
});
|
||||
33
packages/config-yaml/src/schemas/index.ts
Normal file
33
packages/config-yaml/src/schemas/index.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import * as z from "zod";
|
||||
import { contextSchema } from "./context.js";
|
||||
import { dataSchema } from "./data.js";
|
||||
import { modelSchema } from "./models.js";
|
||||
|
||||
const packageSchema = z.object({
|
||||
uses: z.string(),
|
||||
with: z.any().optional(),
|
||||
});
|
||||
|
||||
const toolSchema = z.object({
|
||||
url: z.string(),
|
||||
apiKey: z.string().optional(),
|
||||
});
|
||||
|
||||
const mcpServerSchema = z.object({
|
||||
name: z.string(),
|
||||
command: z.string(),
|
||||
args: z.string().optional(),
|
||||
env: z.record(z.string()).optional(),
|
||||
});
|
||||
|
||||
export const configYamlSchema = z.object({
|
||||
name: z.string(),
|
||||
packages: z.array(packageSchema).optional(),
|
||||
models: z.array(modelSchema).optional(),
|
||||
context: z.array(contextSchema).optional(),
|
||||
data: z.array(dataSchema).optional(),
|
||||
tools: z.array(toolSchema).optional(),
|
||||
mcpServers: z.array(mcpServerSchema).optional(),
|
||||
});
|
||||
|
||||
export type ConfigYaml = z.infer<typeof configYamlSchema>;
|
||||
6
packages/config-yaml/src/schemas/models.ts
Normal file
6
packages/config-yaml/src/schemas/models.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const modelSchema = z.object({
|
||||
provider: z.string(),
|
||||
model: z.string(),
|
||||
});
|
||||
23
packages/config-yaml/tsconfig.json
Normal file
23
packages/config-yaml/tsconfig.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "NodeNext",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["DOM", "ESNext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": false,
|
||||
"noEmitOnError": false,
|
||||
"types": ["node", "jest"],
|
||||
"outDir": "dist",
|
||||
"declaration": true
|
||||
// "sourceMap": true
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue