config yaml package resolution

This commit is contained in:
Nate 2024-12-04 21:02:50 -08:00
parent 9255f14c42
commit e844b98a67
10 changed files with 4226 additions and 0 deletions

View file

@ -0,0 +1,2 @@
.env
test

View 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

File diff suppressed because it is too large Load diff

View 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"
}
}

View 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}`);
}
}

View file

@ -0,0 +1,6 @@
import { z } from "zod";
export const contextSchema = z.object({
uses: z.string(),
with: z.any().optional(),
});

View file

@ -0,0 +1,5 @@
import { z } from "zod";
export const dataSchema = z.object({
provider: z.string(),
});

View 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>;

View file

@ -0,0 +1,6 @@
import { z } from "zod";
export const modelSchema = z.object({
provider: z.string(),
model: z.string(),
});

View 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/**/*"]
}