@agentspec/cli 0.2.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +183 -0
- package/dist/cli.js +1770 -676
- package/dist/cli.js.map +1 -1
- package/package.json +22 -20
package/dist/cli.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
4
|
import { Command } from "commander";
|
|
5
|
-
import { readFileSync as
|
|
5
|
+
import { readFileSync as readFileSync8 } from "fs";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
|
-
import { dirname as
|
|
7
|
+
import { dirname as dirname5, join as join7 } from "path";
|
|
8
8
|
|
|
9
9
|
// src/commands/validate.ts
|
|
10
10
|
import chalk2 from "chalk";
|
|
@@ -98,7 +98,7 @@ function formatCiGate(result) {
|
|
|
98
98
|
function registerValidateCommand(program2) {
|
|
99
99
|
program2.command("validate <file>").description("Validate an agent.yaml manifest against the schema (no I/O)").option("--json", "Output as JSON").action(async (file, opts) => {
|
|
100
100
|
try {
|
|
101
|
-
const { manifest } = loadManifest(file, { resolve: false });
|
|
101
|
+
const { manifest, warnings } = loadManifest(file, { resolve: false });
|
|
102
102
|
if (opts.json) {
|
|
103
103
|
console.log(
|
|
104
104
|
JSON.stringify(
|
|
@@ -106,7 +106,8 @@ function registerValidateCommand(program2) {
|
|
|
106
106
|
valid: true,
|
|
107
107
|
agentName: manifest.metadata.name,
|
|
108
108
|
version: manifest.metadata.version,
|
|
109
|
-
apiVersion: manifest.apiVersion
|
|
109
|
+
apiVersion: manifest.apiVersion,
|
|
110
|
+
warnings
|
|
110
111
|
},
|
|
111
112
|
null,
|
|
112
113
|
2
|
|
@@ -122,6 +123,7 @@ function registerValidateCommand(program2) {
|
|
|
122
123
|
console.log(chalk2.gray(` Tools : ${manifest.spec.tools?.length ?? 0}`));
|
|
123
124
|
console.log(chalk2.gray(` MCP : ${manifest.spec.mcp?.servers?.length ?? 0} servers`));
|
|
124
125
|
console.log(chalk2.gray(` Memory : ${manifest.spec.memory ? "configured" : "none"}`));
|
|
126
|
+
for (const warning of warnings) console.log(chalk2.yellow(` \u26A0 ${warning}`));
|
|
125
127
|
console.log();
|
|
126
128
|
} catch (err) {
|
|
127
129
|
if (opts.json) {
|
|
@@ -149,11 +151,206 @@ function registerValidateCommand(program2) {
|
|
|
149
151
|
}
|
|
150
152
|
|
|
151
153
|
// src/commands/health.ts
|
|
154
|
+
import { existsSync, readFileSync } from "fs";
|
|
155
|
+
import { dirname, join, resolve } from "path";
|
|
152
156
|
import chalk3 from "chalk";
|
|
153
|
-
import { loadManifest as loadManifest2, runHealthCheck } from "@agentspec/sdk";
|
|
157
|
+
import { computeHealthStatus, loadManifest as loadManifest2, runHealthCheck } from "@agentspec/sdk";
|
|
158
|
+
|
|
159
|
+
// src/deploy/compose-health.ts
|
|
160
|
+
import { CONTRACT as CONTRACT2, gatewayEnvNames as gatewayEnvNames2, secretEnvName } from "@agentspec/sdk";
|
|
161
|
+
import * as yaml2 from "js-yaml";
|
|
162
|
+
|
|
163
|
+
// src/deploy/pod-patch.ts
|
|
164
|
+
import { CONTRACT, gatewayEnvNames } from "@agentspec/sdk";
|
|
165
|
+
import * as yaml from "js-yaml";
|
|
166
|
+
function providerBaseUrl(provider) {
|
|
167
|
+
const gateway = `http://${CONTRACT.podPatch.loopback}:${CONTRACT.ports.gateway}`;
|
|
168
|
+
return provider === "openai" ? `${gateway}/v1` : gateway;
|
|
169
|
+
}
|
|
170
|
+
var DNS_LABEL_RE = /^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$|^[a-z0-9]$/;
|
|
171
|
+
function assertPortFree(port) {
|
|
172
|
+
const { agentBind, controlPlane, gateway, gatewayStats, gatewayReadiness } = CONTRACT.ports;
|
|
173
|
+
const taken = [agentBind, controlPlane, gateway, gatewayStats, gatewayReadiness];
|
|
174
|
+
if (taken.includes(port)) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
`spec.api.port ${port} collides with a port the pod patch reserves (${taken.join(", ")}). Choose another spec.api.port.`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function assertNameFree(name) {
|
|
181
|
+
const { podPatch } = CONTRACT;
|
|
182
|
+
const reserved = [podPatch.sidecarContainer, podPatch.gatewayContainer, podPatch.gatewayConfigInitContainer];
|
|
183
|
+
if (reserved.includes(name)) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`metadata.name "${name}" collides with a container the pod patch adds (${reserved.join(", ")}). Choose a different metadata.name.`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function contractOwnedEnv() {
|
|
190
|
+
const { env, providerBaseUrlEnv } = CONTRACT;
|
|
191
|
+
return [env.agentHost, env.agentPort, env.gatewayKey, ...Object.values(providerBaseUrlEnv)];
|
|
192
|
+
}
|
|
193
|
+
function collectEnvRefs(manifest) {
|
|
194
|
+
return collectRefs(manifest, /\$env:([A-Za-z0-9_]+)/g);
|
|
195
|
+
}
|
|
196
|
+
function collectSecretRefs(manifest) {
|
|
197
|
+
return collectRefs(manifest, /\$secret:([A-Za-z0-9_.-]+)/g);
|
|
198
|
+
}
|
|
199
|
+
function collectRefs(manifest, pattern) {
|
|
200
|
+
const names = [...JSON.stringify(manifest).matchAll(pattern)].map((m) => m[1]);
|
|
201
|
+
return [...new Set(names)].sort();
|
|
202
|
+
}
|
|
203
|
+
function planPodPatch(manifest) {
|
|
204
|
+
const name = manifest.metadata.name;
|
|
205
|
+
if (!DNS_LABEL_RE.test(name)) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`Invalid agent name "${name}" \u2014 metadata.name must be a lowercase DNS label (a-z, 0-9, hyphens, 1-63 chars)`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const port = manifest.spec.api?.port ?? CONTRACT.ports.agentDefault;
|
|
211
|
+
assertPortFree(port);
|
|
212
|
+
assertNameFree(name);
|
|
213
|
+
const gatewayEnv = gatewayEnvNames(manifest);
|
|
214
|
+
const envRefs = collectEnvRefs(manifest);
|
|
215
|
+
const ownedEnv = contractOwnedEnv();
|
|
216
|
+
const agentEnvRefs = envRefs.filter((v) => !gatewayEnv.includes(v) && !ownedEnv.includes(v));
|
|
217
|
+
const secretKeys = [.../* @__PURE__ */ new Set([...agentEnvRefs, ...gatewayEnv, CONTRACT.env.gatewayKey])].sort();
|
|
218
|
+
const configVars = {
|
|
219
|
+
AGENT_NAME: name,
|
|
220
|
+
MODEL_PROVIDER: manifest.spec.model.provider,
|
|
221
|
+
MODEL_ID: manifest.spec.model.id
|
|
222
|
+
};
|
|
223
|
+
const baseUrlEnv = Object.fromEntries(
|
|
224
|
+
Object.entries(CONTRACT.providerBaseUrlEnv).map(([provider, envName]) => [
|
|
225
|
+
envName,
|
|
226
|
+
providerBaseUrl(provider)
|
|
227
|
+
])
|
|
228
|
+
);
|
|
229
|
+
return { name, port, agentEnvRefs, gatewayEnv, secretKeys, configVars, agentImage: `${name}:latest`, baseUrlEnv };
|
|
230
|
+
}
|
|
231
|
+
function dumpManifest(manifest) {
|
|
232
|
+
return yaml.dump(manifest, { lineWidth: -1, noRefs: true });
|
|
233
|
+
}
|
|
234
|
+
function gatewayConfigRenderCommand() {
|
|
235
|
+
const { podPatch, paths } = CONTRACT;
|
|
236
|
+
return ["node", paths.sidecarEntrypoint, podPatch.gatewayConfigCommand, paths.manifestFile, "--out", paths.gatewayConfigFile];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/deploy/compose-health.ts
|
|
240
|
+
var pass = (id) => ({ id, category: "env", status: "pass", severity: "error" });
|
|
241
|
+
var fail = (id, message, remediation) => ({
|
|
242
|
+
id,
|
|
243
|
+
category: "env",
|
|
244
|
+
status: "fail",
|
|
245
|
+
severity: "error",
|
|
246
|
+
message,
|
|
247
|
+
remediation
|
|
248
|
+
});
|
|
249
|
+
function checkCompose(manifest, composeText) {
|
|
250
|
+
const service = findAgentService(manifest.metadata.name, composeText);
|
|
251
|
+
if (!service) {
|
|
252
|
+
return [
|
|
253
|
+
fail(
|
|
254
|
+
"compose:service",
|
|
255
|
+
`No service "${manifest.metadata.name}" in the compose file`,
|
|
256
|
+
`Name the agent service after metadata.name ("${manifest.metadata.name}"), as 'agentspec generate --deploy compose' does.`
|
|
257
|
+
)
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
const env = serviceEnv(service);
|
|
261
|
+
return [
|
|
262
|
+
...checkDeclaredEnv(manifest, service, env),
|
|
263
|
+
...checkSecrets(manifest, service, env),
|
|
264
|
+
...checkProviderKeys(manifest, service, env)
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
function findAgentService(name, composeText) {
|
|
268
|
+
let project;
|
|
269
|
+
try {
|
|
270
|
+
project = yaml2.load(composeText);
|
|
271
|
+
} catch (err) {
|
|
272
|
+
const { line, column } = err.mark ?? {};
|
|
273
|
+
throw new Error(`Compose file is not valid YAML (line ${(line ?? 0) + 1}, column ${(column ?? 0) + 1}).`);
|
|
274
|
+
}
|
|
275
|
+
if (!project || typeof project !== "object" || !project.services) {
|
|
276
|
+
throw new Error('Not a compose file: expected a top-level "services" map. Pass the docker-compose.yaml of the agent.');
|
|
277
|
+
}
|
|
278
|
+
if (!(name in project.services)) return void 0;
|
|
279
|
+
return project.services[name] ?? {};
|
|
280
|
+
}
|
|
281
|
+
function serviceEnv({ environment }) {
|
|
282
|
+
const env = { set: /* @__PURE__ */ new Set(), passThrough: /* @__PURE__ */ new Set() };
|
|
283
|
+
const entries = Array.isArray(environment) ? environment.map((entry) => [entry.split("=")[0], entry.includes("=")]) : Object.entries(environment ?? {}).map(([name, value]) => [name, value != null]);
|
|
284
|
+
for (const [name, hasValue] of entries) (hasValue ? env.set : env.passThrough).add(name);
|
|
285
|
+
return env;
|
|
286
|
+
}
|
|
287
|
+
function hasEnvFile({ env_file }) {
|
|
288
|
+
return Array.isArray(env_file) ? env_file.length > 0 : Boolean(env_file);
|
|
289
|
+
}
|
|
290
|
+
function checkEnvVar(id, name, declared, service, env) {
|
|
291
|
+
if (env.set.has(name)) return pass(id);
|
|
292
|
+
const missing = fail(id, `${declared} is declared but the agent service does not set ${name}`, `Add ${name} to the agent service's environment.`);
|
|
293
|
+
if (env.passThrough.has(name)) {
|
|
294
|
+
return { ...missing, status: "warn", message: `${missing.message} (it is taken from compose's own environment, which is not checked)` };
|
|
295
|
+
}
|
|
296
|
+
return hasEnvFile(service) ? { ...missing, status: "warn", message: `${missing.message}; it may come from env_file` } : missing;
|
|
297
|
+
}
|
|
298
|
+
function checkDeclaredEnv(manifest, service, env) {
|
|
299
|
+
return planPodPatch(manifest).agentEnvRefs.map((v) => checkEnvVar(`env:${v}`, v, `$env:${v}`, service, env));
|
|
300
|
+
}
|
|
301
|
+
function checkSecrets(manifest, service, env) {
|
|
302
|
+
const modelSecrets = new Set(modelSecretNames(manifest));
|
|
303
|
+
return collectSecretRefs(manifest).filter((name) => !modelSecrets.has(name)).map((name) => checkEnvVar(`secret:${name}`, secretEnvName(name), `$secret:${name}`, service, env));
|
|
304
|
+
}
|
|
305
|
+
function modelSecretNames({ spec: { model } }) {
|
|
306
|
+
return [model.apiKey, model.fallback?.apiKey].flatMap((key) => key?.startsWith("$secret:") ? [key.slice("$secret:".length)] : []);
|
|
307
|
+
}
|
|
308
|
+
function checkProviderKeys(manifest, service, env) {
|
|
309
|
+
const onService = (v) => env.set.has(v) || env.passThrough.has(v);
|
|
310
|
+
const envKeys = gatewayEnvNames2(manifest).filter((v) => onService(v) && !isClientRouting(manifest, v));
|
|
311
|
+
const mounted = new Set((service.secrets ?? []).map((s) => typeof s === "string" ? s : s.source));
|
|
312
|
+
const secretKeys = modelSecretNames(manifest).filter((name) => mounted.has(name));
|
|
313
|
+
return [...envKeys, ...secretKeys].map(
|
|
314
|
+
(v) => fail(
|
|
315
|
+
`key-custody:${v}`,
|
|
316
|
+
`${v} is a provider key (or gateway setting) and must not be on the agent service`,
|
|
317
|
+
`Move ${v} to the gateway service; the agent calls the gateway with its virtual key (ADR-0019).`
|
|
318
|
+
)
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
function isClientRouting({ spec: { model } }, name) {
|
|
322
|
+
const keyEnvs = [model.apiKey, model.fallback?.apiKey].map((key) => key?.replace(/^\$env:/, ""));
|
|
323
|
+
return Object.values(CONTRACT2.providerBaseUrlEnv).includes(name) && !keyEnvs.includes(name);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/commands/health.ts
|
|
327
|
+
function loadDotEnv(envPath) {
|
|
328
|
+
let raw;
|
|
329
|
+
try {
|
|
330
|
+
raw = readFileSync(envPath, "utf-8");
|
|
331
|
+
} catch {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
for (const line of raw.split("\n")) {
|
|
335
|
+
const trimmed = line.trim();
|
|
336
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
337
|
+
const eqIdx = trimmed.indexOf("=");
|
|
338
|
+
if (eqIdx < 1) continue;
|
|
339
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
340
|
+
const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, "");
|
|
341
|
+
if (key && !(key in process.env)) {
|
|
342
|
+
process.env[key] = val;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
154
346
|
function registerHealthCommand(program2) {
|
|
155
|
-
program2.command("health <file>").description("Run runtime health checks against all declared dependencies").option("--json", "Output as JSON").option("--format <fmt>", "Output format: table|json", "table").option("--fail-on <level>", "Exit 1 on: error|warning|info", "error").option("--no-model", "Skip model API reachability checks").option("--no-mcp", "Skip MCP server checks").option("--no-memory", "Skip memory backend checks").action(
|
|
347
|
+
program2.command("health <file>").description("Run runtime health checks against all declared dependencies").option("--json", "Output as JSON").option("--format <fmt>", "Output format: table|json", "table").option("--fail-on <level>", "Exit 1 on: error|warning|info", "error").option("--no-model", "Skip model API reachability checks").option("--no-mcp", "Skip MCP server checks").option("--no-memory", "Skip memory backend checks").option("--compose <file>", "Check the agent service of a compose file instead of the local environment").option("--env-file <path>", "Load env vars from a .env file before running checks").action(
|
|
156
348
|
async (file, opts) => {
|
|
349
|
+
const manifestDir = dirname(resolve(file));
|
|
350
|
+
const envFilePath = opts.envFile ? resolve(opts.envFile) : join(manifestDir, ".env");
|
|
351
|
+
if (existsSync(envFilePath)) {
|
|
352
|
+
loadDotEnv(envFilePath);
|
|
353
|
+
}
|
|
157
354
|
let manifest;
|
|
158
355
|
try {
|
|
159
356
|
manifest = loadManifest2(file, { resolve: false });
|
|
@@ -161,7 +358,7 @@ function registerHealthCommand(program2) {
|
|
|
161
358
|
printError(`Cannot load manifest: ${String(err)}`);
|
|
162
359
|
process.exit(1);
|
|
163
360
|
}
|
|
164
|
-
const report = await runHealthCheck(manifest.manifest, {
|
|
361
|
+
const report = opts.compose ? composeReport(manifest.manifest, opts.compose) : await runHealthCheck(manifest.manifest, {
|
|
165
362
|
checkModel: opts.model !== false,
|
|
166
363
|
checkMcp: opts.mcp !== false,
|
|
167
364
|
checkMemory: opts.memory !== false,
|
|
@@ -203,6 +400,16 @@ function registerHealthCommand(program2) {
|
|
|
203
400
|
}
|
|
204
401
|
);
|
|
205
402
|
}
|
|
403
|
+
function composeReport(manifest, composeFile) {
|
|
404
|
+
let checks;
|
|
405
|
+
try {
|
|
406
|
+
checks = checkCompose(manifest, readFileSync(resolve(composeFile), "utf-8"));
|
|
407
|
+
} catch (err) {
|
|
408
|
+
printError(`Cannot check compose file: ${err instanceof Error ? err.message : String(err)}`);
|
|
409
|
+
process.exit(1);
|
|
410
|
+
}
|
|
411
|
+
return { agentName: manifest.metadata.name, timestamp: (/* @__PURE__ */ new Date()).toISOString(), ...computeHealthStatus(checks), checks };
|
|
412
|
+
}
|
|
206
413
|
function groupByCategory(checks) {
|
|
207
414
|
const groups = {};
|
|
208
415
|
for (const check of checks) {
|
|
@@ -223,10 +430,16 @@ function shouldFail(checks, failOn) {
|
|
|
223
430
|
// src/commands/audit.ts
|
|
224
431
|
import { writeFileSync } from "fs";
|
|
225
432
|
import chalk4 from "chalk";
|
|
226
|
-
import { loadManifest as loadManifest3, runAudit,
|
|
433
|
+
import { CONTRACT as CONTRACT3, COMPLIANCE_PACKS, loadManifest as loadManifest3, runAudit, formatVerified, TrustPolicy } from "@agentspec/sdk";
|
|
227
434
|
function registerAuditCommand(program2) {
|
|
228
|
-
program2.command("audit <file>").description("Run compliance audit against configured packs").option("--pack <pack>", "Run a specific compliance pack only").option("--url <url>",
|
|
435
|
+
program2.command("audit <file>").description("Run compliance audit against configured packs").option("--pack <pack>", "Run a specific compliance pack only").option("--url <url>", `Accepted for compatibility; makes no request (audit is offline). Read live evidence from the sidecar ${CONTRACT3.sidecarPaths.gap}`).option("--trust <policy>", "Trust policy for the verified score: standard | strict", "standard").option("--json", "Output as JSON").option("--output <file>", "Write report to file").option("--fail-below <score>", "Exit 1 if score is below this threshold", "0").action(
|
|
229
436
|
async (file, opts) => {
|
|
437
|
+
if (opts.pack !== void 0 && !COMPLIANCE_PACKS.includes(opts.pack)) {
|
|
438
|
+
printError(`Unknown --pack "${opts.pack}". Valid packs: ${COMPLIANCE_PACKS.join(", ")}`);
|
|
439
|
+
process.exit(1);
|
|
440
|
+
}
|
|
441
|
+
const threshold = parseFailBelow(opts.failBelow);
|
|
442
|
+
const trust = parseTrust(opts.trust);
|
|
230
443
|
let parsed;
|
|
231
444
|
try {
|
|
232
445
|
parsed = loadManifest3(file, { resolve: false });
|
|
@@ -235,9 +448,8 @@ function registerAuditCommand(program2) {
|
|
|
235
448
|
process.exit(1);
|
|
236
449
|
}
|
|
237
450
|
const { manifest } = parsed;
|
|
238
|
-
const packs = opts.pack ? [opts.pack] : void 0;
|
|
239
|
-
const
|
|
240
|
-
const report = runAudit(manifest, { packs, proofRecords });
|
|
451
|
+
const packs = opts.pack !== void 0 ? [opts.pack] : void 0;
|
|
452
|
+
const report = runAudit(manifest, { packs, trust });
|
|
241
453
|
const outputJson = JSON.stringify(report, null, 2);
|
|
242
454
|
if (opts.output) {
|
|
243
455
|
writeFileSync(opts.output, outputJson, "utf-8");
|
|
@@ -245,12 +457,11 @@ function registerAuditCommand(program2) {
|
|
|
245
457
|
}
|
|
246
458
|
if (opts.json) {
|
|
247
459
|
console.log(outputJson);
|
|
248
|
-
|
|
249
|
-
if (report.overallScore < threshold2) process.exit(1);
|
|
460
|
+
if (report.overallScore < threshold) process.exit(1);
|
|
250
461
|
return;
|
|
251
462
|
}
|
|
252
463
|
printHeader(`AgentSpec Audit \u2014 ${manifest.metadata.name}`);
|
|
253
|
-
printScoreSummary(report
|
|
464
|
+
printScoreSummary(report);
|
|
254
465
|
console.log(
|
|
255
466
|
` Rules : ${chalk4.green(report.passedRules)} passed / ${chalk4.red(report.totalRules - report.passedRules)} failed / ${report.totalRules} total`
|
|
256
467
|
);
|
|
@@ -262,11 +473,8 @@ function registerAuditCommand(program2) {
|
|
|
262
473
|
console.log(` ${chalk4.gray(cat.padEnd(24))} ${color(String(score).padStart(3))}% ${bar}`);
|
|
263
474
|
}
|
|
264
475
|
console.log();
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
} else {
|
|
268
|
-
renderViolations(report.violations);
|
|
269
|
-
}
|
|
476
|
+
renderViolations(report.violations);
|
|
477
|
+
renderUnverified(report.verified.unverified);
|
|
270
478
|
if (report.suppressions.length > 0) {
|
|
271
479
|
console.log();
|
|
272
480
|
console.log(chalk4.bold(` Suppressions (${report.suppressions.length})`));
|
|
@@ -275,10 +483,10 @@ function registerAuditCommand(program2) {
|
|
|
275
483
|
}
|
|
276
484
|
}
|
|
277
485
|
if (report.evidenceBreakdown) {
|
|
278
|
-
formatEvidenceBreakdown(report.evidenceBreakdown
|
|
486
|
+
formatEvidenceBreakdown(report.evidenceBreakdown);
|
|
487
|
+
printSidecarAbsent();
|
|
279
488
|
}
|
|
280
489
|
console.log();
|
|
281
|
-
const threshold = parseInt(opts.failBelow ?? "0", 10);
|
|
282
490
|
if (report.overallScore < threshold) {
|
|
283
491
|
console.error(chalk4.red(` \u2717 Score ${report.overallScore} is below threshold ${threshold}`));
|
|
284
492
|
process.exit(1);
|
|
@@ -286,73 +494,48 @@ function registerAuditCommand(program2) {
|
|
|
286
494
|
}
|
|
287
495
|
);
|
|
288
496
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
printError(
|
|
497
|
+
function parseFailBelow(raw) {
|
|
498
|
+
const text2 = (raw ?? "0").trim().replace(/%$/, "");
|
|
499
|
+
const value = /^\d+(\.\d+)?$/.test(text2) ? Number(text2) : NaN;
|
|
500
|
+
if (!Number.isFinite(value) || value > 100) {
|
|
501
|
+
printError(`--fail-below must be a number between 0 and 100, got "${raw}"`);
|
|
294
502
|
process.exit(1);
|
|
295
503
|
}
|
|
296
|
-
|
|
297
|
-
const proofUrl = `${url.replace(/\/$/, "")}/proof`;
|
|
298
|
-
const res = await fetch(proofUrl, {
|
|
299
|
-
headers: { Accept: "application/json" },
|
|
300
|
-
signal: AbortSignal.timeout(5e3)
|
|
301
|
-
});
|
|
302
|
-
if (res.ok) {
|
|
303
|
-
const data = await res.json();
|
|
304
|
-
if (!Array.isArray(data)) {
|
|
305
|
-
process.stderr.write(
|
|
306
|
-
chalk4.yellow(` \u26A0 Unexpected proof response from ${proofUrl} \u2014 expected array, got ${typeof data}
|
|
307
|
-
`)
|
|
308
|
-
);
|
|
309
|
-
} else {
|
|
310
|
-
return data.filter(isProofRecord);
|
|
311
|
-
}
|
|
312
|
-
} else {
|
|
313
|
-
process.stderr.write(
|
|
314
|
-
chalk4.yellow(` \u26A0 Could not fetch proof records from ${proofUrl} (${res.status}) \u2014 showing declared score only
|
|
315
|
-
`)
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
} catch {
|
|
319
|
-
process.stderr.write(
|
|
320
|
-
chalk4.yellow(` \u26A0 Could not reach sidecar at ${url} \u2014 showing declared score only
|
|
321
|
-
`)
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
return void 0;
|
|
504
|
+
return value;
|
|
325
505
|
}
|
|
326
|
-
function
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
);
|
|
331
|
-
console.log(
|
|
332
|
-
` Proved score : ${formatGrade(report.provedGrade)} ${chalk4.bold(String(report.provedScore))}/100 \u2014 what has been verified`
|
|
333
|
-
);
|
|
334
|
-
console.log(
|
|
335
|
-
` Pending proof : ${chalk4.yellow(String(report.pendingProofCount))} rules \u2014 run external tools and POST to ${sidecarUrl}/proof/rule/:ruleId`
|
|
336
|
-
);
|
|
337
|
-
} else {
|
|
338
|
-
console.log(
|
|
339
|
-
` Score : ${formatGrade(report.grade)} ${chalk4.bold(String(report.overallScore))}/100`
|
|
340
|
-
);
|
|
506
|
+
function parseTrust(raw) {
|
|
507
|
+
const parsed = TrustPolicy.safeParse(raw);
|
|
508
|
+
if (!parsed.success) {
|
|
509
|
+
printError(`--trust must be standard or strict, got "${raw}"`);
|
|
510
|
+
process.exit(1);
|
|
341
511
|
}
|
|
512
|
+
return parsed.data;
|
|
342
513
|
}
|
|
343
|
-
function
|
|
344
|
-
|
|
514
|
+
function printScoreSummary(report) {
|
|
515
|
+
console.log(
|
|
516
|
+
` Declared score : ${formatGrade(report.grade)} ${chalk4.bold(String(report.overallScore))}/100 \u2014 what your spec says`
|
|
517
|
+
);
|
|
518
|
+
console.log(` Verified : ${chalk4.bold(formatVerified(report.verified))} \u2014 what evidence proves`);
|
|
519
|
+
}
|
|
520
|
+
var EVIDENCE_CLASS_ROWS = {
|
|
521
|
+
independent: { badge: "[I]", label: "Independent", note: "(gateway, sidecar, operator)", color: chalk4.blue },
|
|
522
|
+
"evidence-point": { badge: "[E]", label: "Evidence point", note: "(SDK calls your agent code wires)", color: chalk4.magenta },
|
|
523
|
+
offline: { badge: "[O]", label: "Offline", note: "(agent.lock review)", color: chalk4.yellow },
|
|
524
|
+
declared: { badge: "[D]", label: "Declared", note: "(manifest declarations)", color: chalk4.gray }
|
|
525
|
+
};
|
|
526
|
+
function formatEvidenceBreakdown(breakdown) {
|
|
345
527
|
console.log();
|
|
346
528
|
console.log(chalk4.bold(" Evidence Breakdown"));
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
console.log(
|
|
529
|
+
for (const cls of Object.keys(EVIDENCE_CLASS_ROWS)) {
|
|
530
|
+
const { badge, label, note, color } = EVIDENCE_CLASS_ROWS[cls];
|
|
531
|
+
const { passed, total } = breakdown[cls];
|
|
532
|
+
const count = total > 0 ? `${passed}/${total}` : "N/A";
|
|
533
|
+
console.log(` ${color(badge)} ${label.padEnd(14)} ${chalk4.cyan(count)} ${note}`);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function printSidecarAbsent() {
|
|
537
|
+
console.log();
|
|
538
|
+
console.log(chalk4.yellow(` \u26A0 sidecar absent: not verifiable \u2014 rules that need an independent or evidence-point source cannot be proved by an offline audit; read the sidecar's ${CONTRACT3.sidecarPaths.gap}`));
|
|
356
539
|
}
|
|
357
540
|
function progressBar(score) {
|
|
358
541
|
const width = 20;
|
|
@@ -360,34 +543,16 @@ function progressBar(score) {
|
|
|
360
543
|
const bar = "\u2588".repeat(filled) + "\u2591".repeat(width - filled);
|
|
361
544
|
return scoreColor(score)(bar);
|
|
362
545
|
}
|
|
363
|
-
function
|
|
364
|
-
|
|
365
|
-
case "declarative":
|
|
366
|
-
return chalk4.gray("[D]");
|
|
367
|
-
case "probed":
|
|
368
|
-
return chalk4.blue("[P]");
|
|
369
|
-
case "behavioral":
|
|
370
|
-
return chalk4.magenta("[B]");
|
|
371
|
-
case "external":
|
|
372
|
-
return chalk4.yellow("[X]");
|
|
373
|
-
default:
|
|
374
|
-
return chalk4.gray("[D]");
|
|
375
|
-
}
|
|
546
|
+
function evidenceBadges(classes) {
|
|
547
|
+
return classes.map((c) => EVIDENCE_CLASS_ROWS[c].color(EVIDENCE_CLASS_ROWS[c].badge)).join("");
|
|
376
548
|
}
|
|
377
|
-
function renderViolationDetail(v, badge
|
|
549
|
+
function renderViolationDetail(v, badge) {
|
|
378
550
|
console.log(`
|
|
379
551
|
${formatSeverity(v.severity)} ${badge} ${chalk4.bold(v.ruleId)} \u2014 ${chalk4.white(v.title)}`);
|
|
380
552
|
console.log(` ${chalk4.gray(v.message)}`);
|
|
381
553
|
if (v.path) console.log(` Path: ${chalk4.cyan(v.path)}`);
|
|
382
554
|
if (v.recommendation) console.log(` ${chalk4.cyan("\u2192")} ${v.recommendation}`);
|
|
383
|
-
if (v.proofTool) {
|
|
384
|
-
if (sidecarUrl) {
|
|
385
|
-
const endpoint = `POST ${sidecarUrl.replace(/\/$/, "")}/proof/rule/${v.ruleId}`;
|
|
386
|
-
console.log(` ${chalk4.yellow("\u2192 Prove:")} ${v.proofTool} \u2192 ${chalk4.cyan(endpoint)}`);
|
|
387
|
-
} else {
|
|
388
|
-
console.log(` ${chalk4.yellow("\u2192 Prove:")} ${v.proofTool}`);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
555
|
+
if (v.proofTool) console.log(` ${chalk4.yellow("\u2192 Prove:")} ${v.proofTool}`);
|
|
391
556
|
if (v.proofToolUrl) console.log(` ${chalk4.blue(v.proofToolUrl)}`);
|
|
392
557
|
if (v.references?.length) {
|
|
393
558
|
for (const ref of v.references) console.log(` ${chalk4.blue(ref)}`);
|
|
@@ -399,150 +564,112 @@ function renderViolations(violations) {
|
|
|
399
564
|
return;
|
|
400
565
|
}
|
|
401
566
|
console.log(chalk4.bold(` Violations (${violations.length})`));
|
|
402
|
-
for (const v of violations) renderViolationDetail(v,
|
|
567
|
+
for (const v of violations) renderViolationDetail(v, evidenceBadges(v.evidenceClasses));
|
|
403
568
|
}
|
|
404
|
-
function
|
|
405
|
-
if (
|
|
406
|
-
|
|
407
|
-
|
|
569
|
+
function renderUnverified(unverified) {
|
|
570
|
+
if (unverified.length === 0) return;
|
|
571
|
+
console.log();
|
|
572
|
+
console.log(chalk4.bold(` Unverified (${unverified.length})`));
|
|
573
|
+
for (const u of unverified) {
|
|
574
|
+
console.log(` ${chalk4.gray("\u2013")} ${u.ruleId} ${chalk4.gray(`(${u.severity})`)}: ${u.reason.code}`);
|
|
408
575
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// src/commands/register.ts
|
|
579
|
+
import chalk5 from "chalk";
|
|
580
|
+
import * as yaml3 from "js-yaml";
|
|
581
|
+
import { CONTRACT as CONTRACT4, declaredManifestSha256, loadManifest as loadManifest4, runAudit as runAudit2 } from "@agentspec/sdk";
|
|
582
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
583
|
+
function registerRegisterCommand(program2) {
|
|
584
|
+
program2.command("register <file>").description(`Send the declared audit report to the control plane (PUT ${CONTRACT4.controlPlanePaths.declaredAudit})`).requiredOption("--control-plane <url>", "Control plane URL (e.g. https://agentspec.example.com)").option("--agent-name <name>", "Registered agent name (defaults to metadata.name)").option("--api-key <jwt>", `Agent JWT from POST /api/v1/register (defaults to $${CONTRACT4.env.agentKey})`).action(async (file, opts) => {
|
|
585
|
+
const apiKey = resolveApiKey(opts.apiKey);
|
|
586
|
+
const { manifest, declared, raw } = loadDeclaredManifest(file);
|
|
587
|
+
const agentName = opts.agentName ?? manifest.metadata.name;
|
|
588
|
+
const report = runAudit2(manifest);
|
|
589
|
+
await putDeclaredAudit(opts.controlPlane, agentName, apiKey, {
|
|
590
|
+
manifest: declared,
|
|
591
|
+
manifestSha256: declaredManifestSha256(raw),
|
|
592
|
+
report
|
|
593
|
+
});
|
|
594
|
+
console.log(chalk5.green(` \u2713 Declared report for ${agentName} sent (score ${report.overallScore}, grade ${report.grade})`));
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
function resolveApiKey(flag) {
|
|
598
|
+
const apiKey = flag ?? process.env[CONTRACT4.env.agentKey];
|
|
599
|
+
if (!apiKey) {
|
|
600
|
+
printError(`No agent token. Set ${CONTRACT4.env.agentKey} or pass --api-key with the key from POST /api/v1/register.`);
|
|
601
|
+
process.exit(1);
|
|
414
602
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
603
|
+
return apiKey;
|
|
604
|
+
}
|
|
605
|
+
function loadDeclaredManifest(file) {
|
|
606
|
+
try {
|
|
607
|
+
const { manifest, raw } = loadManifest4(file, { resolve: false });
|
|
608
|
+
return { manifest, declared: yaml3.load(raw, { schema: yaml3.JSON_SCHEMA }), raw };
|
|
609
|
+
} catch (err) {
|
|
610
|
+
printError(`Cannot load manifest: ${String(err)}`);
|
|
611
|
+
process.exit(1);
|
|
419
612
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
613
|
+
}
|
|
614
|
+
async function putDeclaredAudit(controlPlane, agentName, apiKey, payload) {
|
|
615
|
+
const route = CONTRACT4.controlPlanePaths.declaredAudit.replace("{name}", encodeURIComponent(agentName));
|
|
616
|
+
let res;
|
|
617
|
+
try {
|
|
618
|
+
res = await fetch(`${controlPlane.replace(/\/+$/, "")}${route}`, {
|
|
619
|
+
method: "PUT",
|
|
620
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
|
|
621
|
+
body: JSON.stringify(payload),
|
|
622
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
623
|
+
});
|
|
624
|
+
} catch (err) {
|
|
625
|
+
printError(`Cannot reach the control plane at ${controlPlane}: ${String(err)}. Check --control-plane and that it is running.`);
|
|
626
|
+
process.exit(1);
|
|
627
|
+
}
|
|
628
|
+
if (!res.ok) {
|
|
629
|
+
printError(`Control plane rejected the declared report (${res.status}): ${await rejectionReason(res)}${authHint(res.status)}`);
|
|
630
|
+
process.exit(1);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
function authHint(status) {
|
|
634
|
+
if (status === 401) return `. Check the agent token: it may be expired or rotated; get a new one from POST /api/v1/register.`;
|
|
635
|
+
if (status === 403) return `. The token is for another agent: pass --agent-name matching the token's agent.`;
|
|
636
|
+
return "";
|
|
637
|
+
}
|
|
638
|
+
async function rejectionReason(res) {
|
|
639
|
+
const text2 = await res.text();
|
|
640
|
+
try {
|
|
641
|
+
const detail = JSON.parse(text2).detail;
|
|
642
|
+
return typeof detail === "string" ? detail : text2;
|
|
643
|
+
} catch {
|
|
644
|
+
return text2;
|
|
425
645
|
}
|
|
426
646
|
}
|
|
427
647
|
|
|
428
648
|
// src/commands/init.ts
|
|
429
|
-
import { writeFileSync as writeFileSync2, existsSync } from "fs";
|
|
430
|
-
import { resolve, join } from "path";
|
|
431
|
-
import
|
|
649
|
+
import { writeFileSync as writeFileSync2, existsSync as existsSync2, mkdirSync } from "fs";
|
|
650
|
+
import { resolve as resolve2, join as join2 } from "path";
|
|
651
|
+
import chalk6 from "chalk";
|
|
432
652
|
import * as p from "@clack/prompts";
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
let includeMemory = false;
|
|
454
|
-
let includeGuardrails = true;
|
|
455
|
-
let includeEval = false;
|
|
456
|
-
if (!opts.yes) {
|
|
457
|
-
p.intro(chalk5.cyan("Creating your agent.yaml"));
|
|
458
|
-
const answers = await p.group(
|
|
459
|
-
{
|
|
460
|
-
name: () => p.text({
|
|
461
|
-
message: "Agent name (slug)",
|
|
462
|
-
placeholder: "my-agent",
|
|
463
|
-
validate: (v) => /^[a-z0-9-]+$/.test(v) ? void 0 : "Must be lowercase slug (a-z, 0-9, -)"
|
|
464
|
-
}),
|
|
465
|
-
description: () => p.text({
|
|
466
|
-
message: "Description",
|
|
467
|
-
placeholder: "An AI agent that..."
|
|
468
|
-
}),
|
|
469
|
-
version: () => p.text({
|
|
470
|
-
message: "Version",
|
|
471
|
-
placeholder: "0.1.0",
|
|
472
|
-
initialValue: "0.1.0"
|
|
473
|
-
}),
|
|
474
|
-
provider: () => p.select({
|
|
475
|
-
message: "Model provider",
|
|
476
|
-
options: [
|
|
477
|
-
{ value: "openai", label: "OpenAI" },
|
|
478
|
-
{ value: "anthropic", label: "Anthropic" },
|
|
479
|
-
{ value: "groq", label: "Groq" },
|
|
480
|
-
{ value: "google", label: "Google" },
|
|
481
|
-
{ value: "mistral", label: "Mistral" },
|
|
482
|
-
{ value: "azure", label: "Azure OpenAI" }
|
|
483
|
-
]
|
|
484
|
-
}),
|
|
485
|
-
modelId: () => p.text({
|
|
486
|
-
message: "Model ID",
|
|
487
|
-
placeholder: "gpt-4o-mini"
|
|
488
|
-
}),
|
|
489
|
-
includeMemory: () => p.confirm({
|
|
490
|
-
message: "Include memory configuration?",
|
|
491
|
-
initialValue: false
|
|
492
|
-
}),
|
|
493
|
-
includeGuardrails: () => p.confirm({
|
|
494
|
-
message: "Include guardrails?",
|
|
495
|
-
initialValue: true
|
|
496
|
-
}),
|
|
497
|
-
includeEval: () => p.confirm({
|
|
498
|
-
message: "Include evaluation configuration?",
|
|
499
|
-
initialValue: false
|
|
500
|
-
})
|
|
501
|
-
},
|
|
502
|
-
{
|
|
503
|
-
onCancel: () => {
|
|
504
|
-
p.cancel("Init cancelled.");
|
|
505
|
-
process.exit(0);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
);
|
|
509
|
-
name = String(answers.name || "my-agent");
|
|
510
|
-
description = String(answers.description || "An AI agent");
|
|
511
|
-
version2 = String(answers.version || "0.1.0");
|
|
512
|
-
provider = String(answers.provider || "openai");
|
|
513
|
-
modelId = String(answers.modelId || "gpt-4o-mini");
|
|
514
|
-
includeMemory = Boolean(answers.includeMemory);
|
|
515
|
-
includeGuardrails = Boolean(answers.includeGuardrails);
|
|
516
|
-
includeEval = Boolean(answers.includeEval);
|
|
517
|
-
}
|
|
518
|
-
const apiKeyEnv = `${provider.toUpperCase()}_API_KEY`;
|
|
519
|
-
const yaml3 = generateManifest({
|
|
520
|
-
name,
|
|
521
|
-
description,
|
|
522
|
-
version: version2,
|
|
523
|
-
provider,
|
|
524
|
-
modelId,
|
|
525
|
-
apiKeyEnv,
|
|
526
|
-
includeMemory,
|
|
527
|
-
includeGuardrails,
|
|
528
|
-
includeEval
|
|
529
|
-
});
|
|
530
|
-
writeFileSync2(outFile, yaml3, "utf-8");
|
|
531
|
-
if (!opts.yes) p.outro(chalk5.green(`\u2713 Created ${outFile}`));
|
|
532
|
-
else console.log(chalk5.green(`
|
|
533
|
-
\u2713 Created ${outFile}
|
|
534
|
-
`));
|
|
535
|
-
console.log(chalk5.gray(" Next steps:"));
|
|
536
|
-
console.log(chalk5.gray(` 1. Edit ${outFile} to customize your agent`));
|
|
537
|
-
console.log(chalk5.gray(` 2. Run: npx agentspec validate agent.yaml`));
|
|
538
|
-
console.log(chalk5.gray(` 3. Run: npx agentspec health agent.yaml`));
|
|
539
|
-
console.log(chalk5.gray(` 4. Run: npx agentspec audit agent.yaml`));
|
|
540
|
-
console.log();
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
function generateManifest(opts) {
|
|
544
|
-
const sections = [
|
|
545
|
-
`apiVersion: agentspec.io/v1
|
|
653
|
+
|
|
654
|
+
// src/commands/init-helpers.ts
|
|
655
|
+
var PROVIDER_DEFAULTS = {
|
|
656
|
+
openai: "gpt-4o-mini",
|
|
657
|
+
anthropic: "claude-sonnet-4-6",
|
|
658
|
+
groq: "openai/gpt-oss-20b",
|
|
659
|
+
google: "gemini-2.0-flash",
|
|
660
|
+
mistral: "mistral-large-latest",
|
|
661
|
+
azure: "gpt-4o"
|
|
662
|
+
};
|
|
663
|
+
var PROVIDER_ENV_KEYS = {
|
|
664
|
+
openai: "OPENAI_API_KEY",
|
|
665
|
+
anthropic: "ANTHROPIC_API_KEY",
|
|
666
|
+
groq: "GROQ_API_KEY",
|
|
667
|
+
google: "GOOGLE_API_KEY",
|
|
668
|
+
mistral: "MISTRAL_API_KEY",
|
|
669
|
+
azure: "AZURE_OPENAI_API_KEY"
|
|
670
|
+
};
|
|
671
|
+
function generateMetadataSection(opts) {
|
|
672
|
+
return `apiVersion: agentspec.io/v1
|
|
546
673
|
kind: AgentSpec
|
|
547
674
|
|
|
548
675
|
metadata:
|
|
@@ -553,12 +680,16 @@ metadata:
|
|
|
553
680
|
author: ""
|
|
554
681
|
license: MIT
|
|
555
682
|
|
|
556
|
-
spec
|
|
683
|
+
spec:`;
|
|
684
|
+
}
|
|
685
|
+
function generateModelSection(opts) {
|
|
686
|
+
const apiKeyEnv = PROVIDER_ENV_KEYS[opts.provider] ?? `${opts.provider.toUpperCase()}_API_KEY`;
|
|
687
|
+
return `
|
|
557
688
|
# \u2500\u2500 MODEL \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
558
689
|
model:
|
|
559
690
|
provider: ${opts.provider}
|
|
560
691
|
id: ${opts.modelId}
|
|
561
|
-
apiKey: $env:${
|
|
692
|
+
apiKey: $env:${apiKeyEnv}
|
|
562
693
|
parameters:
|
|
563
694
|
temperature: 0.7
|
|
564
695
|
maxTokens: 2000
|
|
@@ -568,14 +699,31 @@ spec:
|
|
|
568
699
|
# id: gpt-4o-mini
|
|
569
700
|
# apiKey: $env:OPENAI_API_KEY
|
|
570
701
|
# triggerOn: [rate_limit, timeout, error_5xx]
|
|
571
|
-
# maxRetries: 2
|
|
572
|
-
|
|
702
|
+
# maxRetries: 2`;
|
|
703
|
+
}
|
|
704
|
+
function generatePromptsSection() {
|
|
705
|
+
return `
|
|
573
706
|
# \u2500\u2500 PROMPTS \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
574
707
|
prompts:
|
|
575
708
|
system: $file:prompts/system.md
|
|
576
709
|
fallback: "I'm experiencing difficulties. Please try again."
|
|
577
|
-
hotReload: false
|
|
578
|
-
|
|
710
|
+
hotReload: false`;
|
|
711
|
+
}
|
|
712
|
+
function generateToolsSection(include) {
|
|
713
|
+
if (include) {
|
|
714
|
+
return `
|
|
715
|
+
# \u2500\u2500 TOOLS \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
716
|
+
tools:
|
|
717
|
+
- name: my-tool
|
|
718
|
+
type: function
|
|
719
|
+
description: "Description of what this tool does"
|
|
720
|
+
module: $file:tools/my_tool.py
|
|
721
|
+
function: my_tool_function
|
|
722
|
+
annotations:
|
|
723
|
+
readOnlyHint: true
|
|
724
|
+
destructiveHint: false`;
|
|
725
|
+
}
|
|
726
|
+
return `
|
|
579
727
|
# \u2500\u2500 TOOLS (optional) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
580
728
|
# tools:
|
|
581
729
|
# - name: my-tool
|
|
@@ -585,22 +733,58 @@ spec:
|
|
|
585
733
|
# function: my_tool_function
|
|
586
734
|
# annotations:
|
|
587
735
|
# readOnlyHint: true
|
|
588
|
-
# destructiveHint: false
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
736
|
+
# destructiveHint: false`;
|
|
737
|
+
}
|
|
738
|
+
function generateMemorySection(opts) {
|
|
739
|
+
const backend = opts.memoryBackend ?? "in-memory";
|
|
740
|
+
let connectionLine = "";
|
|
741
|
+
if (backend === "redis") {
|
|
742
|
+
connectionLine = "\n connection: $env:REDIS_URL";
|
|
743
|
+
} else if (backend === "sqlite") {
|
|
744
|
+
connectionLine = "\n connection: file:./data/memory.db";
|
|
745
|
+
}
|
|
746
|
+
return `
|
|
592
747
|
# \u2500\u2500 MEMORY \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
593
748
|
memory:
|
|
594
749
|
shortTerm:
|
|
595
|
-
backend:
|
|
750
|
+
backend: ${backend}
|
|
596
751
|
maxTurns: 20
|
|
597
|
-
maxTokens: 8000
|
|
752
|
+
maxTokens: 8000${connectionLine}
|
|
598
753
|
hygiene:
|
|
599
754
|
piiScrubFields: []
|
|
600
|
-
auditLog: false
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
755
|
+
auditLog: false`;
|
|
756
|
+
}
|
|
757
|
+
function generateDefaultApiSection() {
|
|
758
|
+
return `
|
|
759
|
+
# \u2500\u2500 API \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
760
|
+
api:
|
|
761
|
+
type: rest
|
|
762
|
+
port: 8000
|
|
763
|
+
pathPrefix: /v1
|
|
764
|
+
corsOrigins: ["*"]`;
|
|
765
|
+
}
|
|
766
|
+
function generateApiSection(opts) {
|
|
767
|
+
const apiType = opts.apiType ?? "rest";
|
|
768
|
+
const port = opts.apiPort ?? 3e3;
|
|
769
|
+
if (apiType === "mcp") {
|
|
770
|
+
return `
|
|
771
|
+
# \u2500\u2500 API \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
772
|
+
api:
|
|
773
|
+
type: mcp
|
|
774
|
+
port: ${port}`;
|
|
775
|
+
}
|
|
776
|
+
return `
|
|
777
|
+
# \u2500\u2500 API \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
778
|
+
api:
|
|
779
|
+
type: rest
|
|
780
|
+
port: ${port}
|
|
781
|
+
chat:
|
|
782
|
+
path: /v1/chat
|
|
783
|
+
protocol: openai-compatible
|
|
784
|
+
streaming: true`;
|
|
785
|
+
}
|
|
786
|
+
function generateGuardrailsSection() {
|
|
787
|
+
return `
|
|
604
788
|
# \u2500\u2500 GUARDRAILS \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
605
789
|
guardrails:
|
|
606
790
|
input:
|
|
@@ -610,43 +794,417 @@ spec:
|
|
|
610
794
|
output:
|
|
611
795
|
- type: toxicity-filter
|
|
612
796
|
threshold: 0.7
|
|
613
|
-
action: reject
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
797
|
+
action: reject`;
|
|
798
|
+
}
|
|
799
|
+
function generateEvalSection() {
|
|
800
|
+
return `
|
|
617
801
|
# \u2500\u2500 EVALUATION \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
618
802
|
evaluation:
|
|
619
|
-
framework: deepeval
|
|
620
803
|
datasets:
|
|
621
804
|
- name: qa-test
|
|
622
805
|
path: $file:eval/datasets/qa.jsonl
|
|
623
806
|
metrics:
|
|
624
|
-
-
|
|
625
|
-
- hallucination
|
|
807
|
+
- grounded
|
|
626
808
|
thresholds:
|
|
627
|
-
|
|
628
|
-
ciGate: false
|
|
629
|
-
|
|
630
|
-
|
|
809
|
+
grounded: 0.95
|
|
810
|
+
ciGate: false`;
|
|
811
|
+
}
|
|
812
|
+
function generateObservabilitySection(opts) {
|
|
813
|
+
const backend = opts.tracingBackend ?? "langfuse";
|
|
814
|
+
let tracingConfig;
|
|
815
|
+
if (backend === "langfuse") {
|
|
816
|
+
tracingConfig = ` backend: langfuse
|
|
817
|
+
publicKey: $env:LANGFUSE_PUBLIC_KEY
|
|
818
|
+
secretKey: $env:LANGFUSE_SECRET_KEY`;
|
|
819
|
+
} else if (backend === "otel") {
|
|
820
|
+
tracingConfig = ` backend: otel
|
|
821
|
+
endpoint: $env:OTEL_EXPORTER_OTLP_ENDPOINT`;
|
|
822
|
+
} else {
|
|
823
|
+
tracingConfig = ` backend: datadog
|
|
824
|
+
endpoint: $env:DD_TRACE_AGENT_URL`;
|
|
825
|
+
}
|
|
826
|
+
return `
|
|
827
|
+
# \u2500\u2500 OBSERVABILITY \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
828
|
+
observability:
|
|
829
|
+
tracing:
|
|
830
|
+
${tracingConfig}
|
|
831
|
+
logging:
|
|
832
|
+
level: info
|
|
833
|
+
structured: true`;
|
|
834
|
+
}
|
|
835
|
+
function generateComplianceSection() {
|
|
836
|
+
return `
|
|
631
837
|
# \u2500\u2500 COMPLIANCE \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
632
838
|
compliance:
|
|
633
839
|
packs:
|
|
634
840
|
- owasp-llm-top10
|
|
635
841
|
- model-resilience
|
|
636
|
-
- memory-hygiene
|
|
637
|
-
|
|
842
|
+
- memory-hygiene`;
|
|
843
|
+
}
|
|
844
|
+
function generateRequiresSection(envVars) {
|
|
845
|
+
const envLines = envVars.map((v) => ` - ${v}`).join("\n");
|
|
846
|
+
return `
|
|
638
847
|
# \u2500\u2500 RUNTIME REQUIREMENTS \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
639
848
|
requires:
|
|
640
849
|
envVars:
|
|
641
|
-
|
|
642
|
-
|
|
850
|
+
${envLines}
|
|
851
|
+
`;
|
|
852
|
+
}
|
|
853
|
+
function collectRequiredEnvVars(opts) {
|
|
854
|
+
const vars = [];
|
|
855
|
+
const providerKey = PROVIDER_ENV_KEYS[opts.provider] ?? `${opts.provider.toUpperCase()}_API_KEY`;
|
|
856
|
+
vars.push(providerKey);
|
|
857
|
+
if (opts.includeMemory && opts.memoryBackend === "redis") {
|
|
858
|
+
vars.push("REDIS_URL");
|
|
859
|
+
}
|
|
860
|
+
if (opts.includeObservability) {
|
|
861
|
+
const backend = opts.tracingBackend ?? "langfuse";
|
|
862
|
+
if (backend === "langfuse") {
|
|
863
|
+
vars.push("LANGFUSE_PUBLIC_KEY", "LANGFUSE_SECRET_KEY");
|
|
864
|
+
} else if (backend === "otel") {
|
|
865
|
+
vars.push("OTEL_EXPORTER_OTLP_ENDPOINT");
|
|
866
|
+
} else if (backend === "datadog") {
|
|
867
|
+
vars.push("DD_TRACE_AGENT_URL");
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
return vars;
|
|
871
|
+
}
|
|
872
|
+
function generateManifest(opts) {
|
|
873
|
+
const envVars = collectRequiredEnvVars(opts);
|
|
874
|
+
const sections = [
|
|
875
|
+
generateMetadataSection(opts),
|
|
876
|
+
generateModelSection(opts),
|
|
877
|
+
generatePromptsSection(),
|
|
878
|
+
generateToolsSection(opts.includeToolsStarter)
|
|
879
|
+
];
|
|
880
|
+
if (opts.includeMemory) sections.push(generateMemorySection(opts));
|
|
881
|
+
if (opts.includeApi) sections.push(opts.apiType || opts.apiPort ? generateApiSection(opts) : generateDefaultApiSection());
|
|
882
|
+
if (opts.includeGuardrails) sections.push(generateGuardrailsSection());
|
|
883
|
+
if (opts.includeEval) sections.push(generateEvalSection());
|
|
884
|
+
if (opts.includeObservability) sections.push(generateObservabilitySection(opts));
|
|
885
|
+
sections.push(generateComplianceSection());
|
|
886
|
+
sections.push(generateRequiresSection(envVars));
|
|
643
887
|
return sections.join("\n");
|
|
644
888
|
}
|
|
889
|
+
function generateSystemPrompt(name, description) {
|
|
890
|
+
const title = name.split("-").map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
|
891
|
+
return `# ${title}
|
|
892
|
+
|
|
893
|
+
${description}
|
|
894
|
+
|
|
895
|
+
## Instructions
|
|
896
|
+
|
|
897
|
+
- Be helpful and concise
|
|
898
|
+
- Follow the user's instructions carefully
|
|
899
|
+
- Ask for clarification when the request is ambiguous
|
|
900
|
+
`;
|
|
901
|
+
}
|
|
902
|
+
async function fetchAvailableModels(provider) {
|
|
903
|
+
const controller = new AbortController();
|
|
904
|
+
const timeout = setTimeout(() => controller.abort(), 5e3);
|
|
905
|
+
try {
|
|
906
|
+
const res = await fetch("https://openrouter.ai/api/v1/models", {
|
|
907
|
+
signal: controller.signal
|
|
908
|
+
});
|
|
909
|
+
if (!res.ok) return null;
|
|
910
|
+
const json = await res.json();
|
|
911
|
+
if (!json.data || !Array.isArray(json.data)) return null;
|
|
912
|
+
const prefix = provider + "/";
|
|
913
|
+
return json.data.map((m) => m.id).filter((id) => id.startsWith(prefix) && !id.endsWith(":free")).map((id) => id.slice(prefix.length)).sort();
|
|
914
|
+
} catch {
|
|
915
|
+
return null;
|
|
916
|
+
} finally {
|
|
917
|
+
clearTimeout(timeout);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
function generateEnvExample(name, envVars) {
|
|
921
|
+
const placeholders = {
|
|
922
|
+
OPENAI_API_KEY: "sk-your-openai-api-key",
|
|
923
|
+
ANTHROPIC_API_KEY: "sk-ant-your-anthropic-api-key",
|
|
924
|
+
GROQ_API_KEY: "gsk_your-groq-api-key",
|
|
925
|
+
GOOGLE_API_KEY: "your-google-api-key",
|
|
926
|
+
MISTRAL_API_KEY: "your-mistral-api-key",
|
|
927
|
+
AZURE_OPENAI_API_KEY: "your-azure-openai-api-key",
|
|
928
|
+
REDIS_URL: "redis://localhost:6379",
|
|
929
|
+
LANGFUSE_PUBLIC_KEY: "pk-lf-your-langfuse-public-key",
|
|
930
|
+
LANGFUSE_SECRET_KEY: "sk-lf-your-langfuse-secret-key",
|
|
931
|
+
OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318",
|
|
932
|
+
DD_TRACE_AGENT_URL: "http://localhost:8126"
|
|
933
|
+
};
|
|
934
|
+
const lines = [`# Environment variables for ${name}`];
|
|
935
|
+
for (const v of envVars) {
|
|
936
|
+
lines.push(`${v}=${placeholders[v] ?? "your-value-here"}`);
|
|
937
|
+
}
|
|
938
|
+
return lines.join("\n") + "\n";
|
|
939
|
+
}
|
|
940
|
+
function validateAgentName(v) {
|
|
941
|
+
return /^[a-z0-9-]+$/.test(v ?? "") ? void 0 : "Must be lowercase slug (a-z, 0-9, -)";
|
|
942
|
+
}
|
|
943
|
+
function validatePort(v) {
|
|
944
|
+
const n = parseInt(v ?? "", 10);
|
|
945
|
+
return isNaN(n) || n < 1 || n > 65535 ? "Must be a valid port (1-65535)" : void 0;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// src/commands/init.ts
|
|
949
|
+
function buildDefaultOptions() {
|
|
950
|
+
return {
|
|
951
|
+
name: "my-agent",
|
|
952
|
+
description: "An AI agent",
|
|
953
|
+
version: "0.1.0",
|
|
954
|
+
provider: "openai",
|
|
955
|
+
modelId: "gpt-4o-mini",
|
|
956
|
+
includeMemory: false,
|
|
957
|
+
includeApi: false,
|
|
958
|
+
includeObservability: false,
|
|
959
|
+
includeGuardrails: true,
|
|
960
|
+
includeEval: false,
|
|
961
|
+
includeToolsStarter: false
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
async function runPhase1() {
|
|
965
|
+
const basics = await p.group(
|
|
966
|
+
{
|
|
967
|
+
name: () => p.text({
|
|
968
|
+
message: "Agent name (slug)",
|
|
969
|
+
placeholder: "my-agent",
|
|
970
|
+
validate: validateAgentName
|
|
971
|
+
}),
|
|
972
|
+
description: () => p.text({
|
|
973
|
+
message: "Description",
|
|
974
|
+
placeholder: "An AI agent that..."
|
|
975
|
+
}),
|
|
976
|
+
version: () => p.text({
|
|
977
|
+
message: "Version",
|
|
978
|
+
placeholder: "0.1.0",
|
|
979
|
+
initialValue: "0.1.0"
|
|
980
|
+
}),
|
|
981
|
+
provider: () => p.select({
|
|
982
|
+
message: "Model provider",
|
|
983
|
+
options: [
|
|
984
|
+
{ value: "openai", label: "OpenAI" },
|
|
985
|
+
{ value: "anthropic", label: "Anthropic" },
|
|
986
|
+
{ value: "groq", label: "Groq" },
|
|
987
|
+
{ value: "google", label: "Google" },
|
|
988
|
+
{ value: "mistral", label: "Mistral" },
|
|
989
|
+
{ value: "azure", label: "Azure OpenAI" }
|
|
990
|
+
]
|
|
991
|
+
})
|
|
992
|
+
},
|
|
993
|
+
{
|
|
994
|
+
onCancel: () => {
|
|
995
|
+
p.cancel("Init cancelled.");
|
|
996
|
+
process.exit(0);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
);
|
|
1000
|
+
const provider = String(basics.provider || "openai");
|
|
1001
|
+
const modelId = await selectModel(provider);
|
|
1002
|
+
return {
|
|
1003
|
+
...basics,
|
|
1004
|
+
modelId
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
async function selectModel(provider) {
|
|
1008
|
+
const s = p.spinner();
|
|
1009
|
+
s.start("Fetching available models...");
|
|
1010
|
+
const models = await fetchAvailableModels(provider);
|
|
1011
|
+
if (models && models.length > 0) {
|
|
1012
|
+
s.stop("Models loaded");
|
|
1013
|
+
const defaultModel = PROVIDER_DEFAULTS[provider];
|
|
1014
|
+
const selected = await p.select({
|
|
1015
|
+
message: "Model",
|
|
1016
|
+
options: models.map((id) => ({ value: id, label: id })),
|
|
1017
|
+
initialValue: models.includes(defaultModel) ? defaultModel : models[0]
|
|
1018
|
+
});
|
|
1019
|
+
if (p.isCancel(selected)) {
|
|
1020
|
+
p.cancel("Init cancelled.");
|
|
1021
|
+
process.exit(0);
|
|
1022
|
+
}
|
|
1023
|
+
return String(selected);
|
|
1024
|
+
}
|
|
1025
|
+
s.stop("Could not fetch models");
|
|
1026
|
+
const modelId = await p.text({
|
|
1027
|
+
message: "Model ID",
|
|
1028
|
+
placeholder: PROVIDER_DEFAULTS[provider] ?? "gpt-4o-mini",
|
|
1029
|
+
initialValue: PROVIDER_DEFAULTS[provider] ?? "gpt-4o-mini"
|
|
1030
|
+
});
|
|
1031
|
+
if (p.isCancel(modelId)) {
|
|
1032
|
+
p.cancel("Init cancelled.");
|
|
1033
|
+
process.exit(0);
|
|
1034
|
+
}
|
|
1035
|
+
return String(modelId);
|
|
1036
|
+
}
|
|
1037
|
+
async function runPhase2() {
|
|
1038
|
+
const answers = await p.group(
|
|
1039
|
+
{
|
|
1040
|
+
includeMemory: () => p.confirm({
|
|
1041
|
+
message: "Include memory configuration?",
|
|
1042
|
+
initialValue: false
|
|
1043
|
+
}),
|
|
1044
|
+
includeApi: () => p.confirm({
|
|
1045
|
+
message: "Include API endpoint configuration?",
|
|
1046
|
+
initialValue: false
|
|
1047
|
+
}),
|
|
1048
|
+
includeObservability: () => p.confirm({
|
|
1049
|
+
message: "Include observability (tracing/logging)?",
|
|
1050
|
+
initialValue: false
|
|
1051
|
+
}),
|
|
1052
|
+
includeGuardrails: () => p.confirm({
|
|
1053
|
+
message: "Include guardrails?",
|
|
1054
|
+
initialValue: true
|
|
1055
|
+
}),
|
|
1056
|
+
includeEval: () => p.confirm({
|
|
1057
|
+
message: "Include evaluation configuration?",
|
|
1058
|
+
initialValue: false
|
|
1059
|
+
}),
|
|
1060
|
+
includeToolsStarter: () => p.confirm({
|
|
1061
|
+
message: "Include starter tool template?",
|
|
1062
|
+
initialValue: false
|
|
1063
|
+
})
|
|
1064
|
+
},
|
|
1065
|
+
{
|
|
1066
|
+
onCancel: () => {
|
|
1067
|
+
p.cancel("Init cancelled.");
|
|
1068
|
+
process.exit(0);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
);
|
|
1072
|
+
return answers;
|
|
1073
|
+
}
|
|
1074
|
+
async function runPhase3(phase2) {
|
|
1075
|
+
const result = {};
|
|
1076
|
+
if (phase2.includeMemory) {
|
|
1077
|
+
const backend = await p.select({
|
|
1078
|
+
message: "Memory backend",
|
|
1079
|
+
options: [
|
|
1080
|
+
{ value: "in-memory", label: "In-memory (no persistence)" },
|
|
1081
|
+
{ value: "redis", label: "Redis" },
|
|
1082
|
+
{ value: "sqlite", label: "SQLite" }
|
|
1083
|
+
]
|
|
1084
|
+
});
|
|
1085
|
+
if (p.isCancel(backend)) {
|
|
1086
|
+
p.cancel("Init cancelled.");
|
|
1087
|
+
process.exit(0);
|
|
1088
|
+
}
|
|
1089
|
+
result.memoryBackend = backend;
|
|
1090
|
+
}
|
|
1091
|
+
if (phase2.includeApi) {
|
|
1092
|
+
const apiType = await p.select({
|
|
1093
|
+
message: "API type",
|
|
1094
|
+
options: [
|
|
1095
|
+
{ value: "rest", label: "REST (OpenAI-compatible)" },
|
|
1096
|
+
{ value: "mcp", label: "MCP (Model Context Protocol)" }
|
|
1097
|
+
]
|
|
1098
|
+
});
|
|
1099
|
+
if (p.isCancel(apiType)) {
|
|
1100
|
+
p.cancel("Init cancelled.");
|
|
1101
|
+
process.exit(0);
|
|
1102
|
+
}
|
|
1103
|
+
result.apiType = apiType;
|
|
1104
|
+
const port = await p.text({
|
|
1105
|
+
message: "API port",
|
|
1106
|
+
placeholder: "3000",
|
|
1107
|
+
initialValue: "3000",
|
|
1108
|
+
validate: validatePort
|
|
1109
|
+
});
|
|
1110
|
+
if (p.isCancel(port)) {
|
|
1111
|
+
p.cancel("Init cancelled.");
|
|
1112
|
+
process.exit(0);
|
|
1113
|
+
}
|
|
1114
|
+
result.apiPort = parseInt(String(port), 10);
|
|
1115
|
+
}
|
|
1116
|
+
if (phase2.includeObservability) {
|
|
1117
|
+
const backend = await p.select({
|
|
1118
|
+
message: "Tracing backend",
|
|
1119
|
+
options: [
|
|
1120
|
+
{ value: "langfuse", label: "Langfuse" },
|
|
1121
|
+
{ value: "otel", label: "OpenTelemetry (OTLP)" },
|
|
1122
|
+
{ value: "datadog", label: "Datadog" }
|
|
1123
|
+
]
|
|
1124
|
+
});
|
|
1125
|
+
if (p.isCancel(backend)) {
|
|
1126
|
+
p.cancel("Init cancelled.");
|
|
1127
|
+
process.exit(0);
|
|
1128
|
+
}
|
|
1129
|
+
result.tracingBackend = backend;
|
|
1130
|
+
}
|
|
1131
|
+
return result;
|
|
1132
|
+
}
|
|
1133
|
+
function scaffoldFiles(outDir, opts) {
|
|
1134
|
+
const promptsDir = join2(outDir, "prompts");
|
|
1135
|
+
const systemMdPath = join2(promptsDir, "system.md");
|
|
1136
|
+
const envExamplePath = join2(outDir, ".env.example");
|
|
1137
|
+
const envVars = collectRequiredEnvVars(opts);
|
|
1138
|
+
mkdirSync(promptsDir, { recursive: true });
|
|
1139
|
+
if (existsSync2(systemMdPath)) {
|
|
1140
|
+
p.log.warn(`Skipped prompts/system.md (already exists)`);
|
|
1141
|
+
} else {
|
|
1142
|
+
writeFileSync2(systemMdPath, generateSystemPrompt(opts.name, opts.description), "utf-8");
|
|
1143
|
+
}
|
|
1144
|
+
if (existsSync2(envExamplePath)) {
|
|
1145
|
+
p.log.warn(`Skipped .env.example (already exists)`);
|
|
1146
|
+
} else {
|
|
1147
|
+
writeFileSync2(envExamplePath, generateEnvExample(opts.name, envVars), "utf-8");
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
function registerInitCommand(program2) {
|
|
1151
|
+
program2.command("init [dir]").description("Interactive wizard to create a new agent.yaml manifest").option("--yes", "Skip prompts, create a minimal manifest").action(async (dir = ".", cmdOpts) => {
|
|
1152
|
+
const outDir = resolve2(dir);
|
|
1153
|
+
const outFile = join2(outDir, "agent.yaml");
|
|
1154
|
+
printHeader("AgentSpec Init");
|
|
1155
|
+
if (existsSync2(outFile) && !cmdOpts.yes) {
|
|
1156
|
+
const overwrite = await p.confirm({
|
|
1157
|
+
message: `agent.yaml already exists at ${outFile}. Overwrite?`,
|
|
1158
|
+
initialValue: false
|
|
1159
|
+
});
|
|
1160
|
+
if (!overwrite || p.isCancel(overwrite)) {
|
|
1161
|
+
p.cancel("Init cancelled.");
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
let opts;
|
|
1166
|
+
if (cmdOpts.yes) {
|
|
1167
|
+
opts = buildDefaultOptions();
|
|
1168
|
+
} else {
|
|
1169
|
+
p.intro(chalk6.cyan("Creating your agent.yaml"));
|
|
1170
|
+
const phase1 = await runPhase1();
|
|
1171
|
+
const phase2 = await runPhase2();
|
|
1172
|
+
const phase3 = await runPhase3(phase2);
|
|
1173
|
+
opts = {
|
|
1174
|
+
name: String(phase1.name || "my-agent"),
|
|
1175
|
+
description: String(phase1.description || "An AI agent"),
|
|
1176
|
+
version: String(phase1.version || "0.1.0"),
|
|
1177
|
+
provider: String(phase1.provider || "openai"),
|
|
1178
|
+
modelId: String(phase1.modelId || "gpt-4o-mini"),
|
|
1179
|
+
includeMemory: Boolean(phase2.includeMemory),
|
|
1180
|
+
includeApi: Boolean(phase2.includeApi),
|
|
1181
|
+
includeObservability: Boolean(phase2.includeObservability),
|
|
1182
|
+
includeGuardrails: Boolean(phase2.includeGuardrails),
|
|
1183
|
+
includeEval: Boolean(phase2.includeEval),
|
|
1184
|
+
includeToolsStarter: Boolean(phase2.includeToolsStarter),
|
|
1185
|
+
...phase3
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
const yaml7 = generateManifest(opts);
|
|
1189
|
+
writeFileSync2(outFile, yaml7, "utf-8");
|
|
1190
|
+
scaffoldFiles(outDir, opts);
|
|
1191
|
+
if (!cmdOpts.yes) p.outro(chalk6.green(`\u2713 Created ${outFile}`));
|
|
1192
|
+
else console.log(chalk6.green(`
|
|
1193
|
+
\u2713 Created ${outFile}
|
|
1194
|
+
`));
|
|
1195
|
+
console.log(chalk6.gray(" Next steps:"));
|
|
1196
|
+
console.log(chalk6.gray(` 1. Edit ${outFile} to customize your agent`));
|
|
1197
|
+
console.log(chalk6.gray(` 2. Run: npx agentspec validate agent.yaml`));
|
|
1198
|
+
console.log(chalk6.gray(` 3. Run: npx agentspec health agent.yaml`));
|
|
1199
|
+
console.log(chalk6.gray(` 4. Run: npx agentspec audit agent.yaml`));
|
|
1200
|
+
console.log();
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
645
1203
|
|
|
646
1204
|
// src/commands/generate.ts
|
|
647
|
-
import { existsSync as
|
|
648
|
-
import { basename, dirname, join as
|
|
649
|
-
import
|
|
1205
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
1206
|
+
import { basename, dirname as dirname2, join as join3, resolve as resolve3, sep } from "path";
|
|
1207
|
+
import chalk7 from "chalk";
|
|
650
1208
|
|
|
651
1209
|
// src/utils/spinner.ts
|
|
652
1210
|
import { spinner as clackSpinner } from "@clack/prompts";
|
|
@@ -666,7 +1224,7 @@ function createFallbackSpinner() {
|
|
|
666
1224
|
}
|
|
667
1225
|
};
|
|
668
1226
|
}
|
|
669
|
-
function
|
|
1227
|
+
function spinner2() {
|
|
670
1228
|
if (process.stdout.isTTY) {
|
|
671
1229
|
return clackSpinner();
|
|
672
1230
|
}
|
|
@@ -674,48 +1232,122 @@ function spinner() {
|
|
|
674
1232
|
}
|
|
675
1233
|
|
|
676
1234
|
// src/commands/generate.ts
|
|
677
|
-
import { loadManifest as
|
|
678
|
-
import {
|
|
1235
|
+
import { loadManifest as loadManifest5 } from "@agentspec/sdk";
|
|
1236
|
+
import { generateCode, listFrameworks, resolveProvider } from "@agentspec/codegen";
|
|
679
1237
|
|
|
680
1238
|
// src/deploy/k8s.ts
|
|
681
|
-
|
|
682
|
-
var DEFAULT_AGENT_PORT = 8e3;
|
|
683
|
-
var PROXY_PORT = 4e3;
|
|
684
|
-
var CONTROL_PORT = 4001;
|
|
685
|
-
var K8S_NAME_RE = /^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$|^[a-z0-9]$/;
|
|
1239
|
+
import { CONTRACT as CONTRACT5 } from "@agentspec/sdk";
|
|
686
1240
|
function escapeYamlDoubleQuotedValue(value) {
|
|
687
1241
|
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
688
1242
|
}
|
|
689
1243
|
function generateK8sManifests(manifest) {
|
|
690
|
-
const
|
|
691
|
-
|
|
692
|
-
throw new Error(
|
|
693
|
-
`Invalid agent name "${name}" for Kubernetes \u2014 must be a lowercase DNS label (a-z, 0-9, hyphens, 1-63 chars)`
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
const port = manifest.spec.api?.port ?? DEFAULT_AGENT_PORT;
|
|
697
|
-
const envRefs = collectEnvRefs(manifest);
|
|
1244
|
+
const plan = planPodPatch(manifest);
|
|
1245
|
+
const { name, port, agentEnvRefs, gatewayEnv, secretKeys, configVars } = plan;
|
|
698
1246
|
return {
|
|
699
|
-
"k8s/deployment.yaml": generateDeployment(
|
|
1247
|
+
"k8s/deployment.yaml": generateDeployment(plan),
|
|
700
1248
|
"k8s/service.yaml": generateService(name, port),
|
|
701
|
-
"k8s/configmap.yaml": generateConfigMap(name,
|
|
702
|
-
"k8s/
|
|
1249
|
+
"k8s/configmap.yaml": generateConfigMap(name, configVars),
|
|
1250
|
+
"k8s/manifest-configmap.yaml": generateManifestConfigMap(name, manifest),
|
|
1251
|
+
"k8s/secret.yaml.example": generateSecretExample(name, secretKeys)
|
|
703
1252
|
};
|
|
704
1253
|
}
|
|
705
|
-
function
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
1254
|
+
function secretEnv(name, vars, indent) {
|
|
1255
|
+
return vars.map(
|
|
1256
|
+
(v) => `${indent}- name: ${v}
|
|
1257
|
+
${indent} valueFrom:
|
|
1258
|
+
${indent} secretKeyRef:
|
|
1259
|
+
${indent} name: ${name}-secrets
|
|
1260
|
+
${indent} key: ${v}`
|
|
1261
|
+
).join("\n");
|
|
1262
|
+
}
|
|
1263
|
+
function renderInitContainer() {
|
|
1264
|
+
const { podPatch, paths } = CONTRACT5;
|
|
1265
|
+
return ` - name: ${podPatch.gatewayConfigInitContainer}
|
|
1266
|
+
image: ${CONTRACT5.images.sidecar}
|
|
1267
|
+
command: [${gatewayConfigRenderCommand().join(", ")}]
|
|
1268
|
+
volumeMounts:
|
|
1269
|
+
- name: ${podPatch.manifestVolume}
|
|
1270
|
+
mountPath: ${paths.manifestDir}
|
|
1271
|
+
readOnly: true
|
|
1272
|
+
- name: ${podPatch.gatewayConfigVolume}
|
|
1273
|
+
mountPath: ${paths.gatewayConfigDir}
|
|
1274
|
+
`;
|
|
1275
|
+
}
|
|
1276
|
+
function renderAgentContainer(name, image, baseUrlEnv, agentEnvRefs) {
|
|
1277
|
+
const baseUrls = Object.entries(baseUrlEnv).map(([envName, url]) => ` - name: ${envName}
|
|
1278
|
+
value: "${url}"`).join("\n");
|
|
1279
|
+
const secrets = secretEnv(name, [...agentEnvRefs, CONTRACT5.env.gatewayKey].sort(), " ");
|
|
1280
|
+
return ` - name: ${name}
|
|
1281
|
+
image: ${image}
|
|
1282
|
+
envFrom:
|
|
1283
|
+
- configMapRef:
|
|
1284
|
+
name: ${name}-config
|
|
1285
|
+
env:
|
|
1286
|
+
- name: ${CONTRACT5.env.agentHost}
|
|
1287
|
+
value: "${CONTRACT5.podPatch.loopback}"
|
|
1288
|
+
- name: ${CONTRACT5.env.agentPort}
|
|
1289
|
+
value: "${CONTRACT5.ports.agentBind}"
|
|
1290
|
+
${baseUrls}
|
|
1291
|
+
${secrets}
|
|
1292
|
+
`;
|
|
1293
|
+
}
|
|
1294
|
+
function renderSidecarContainer(port) {
|
|
1295
|
+
const { podPatch, paths } = CONTRACT5;
|
|
1296
|
+
return ` - name: ${podPatch.sidecarContainer}
|
|
1297
|
+
image: ${CONTRACT5.images.sidecar}
|
|
1298
|
+
ports:
|
|
1299
|
+
- containerPort: ${port}
|
|
1300
|
+
name: ${podPatch.proxyPortName}
|
|
1301
|
+
- containerPort: ${CONTRACT5.ports.controlPlane}
|
|
1302
|
+
name: ${podPatch.controlPortName}
|
|
1303
|
+
env:
|
|
1304
|
+
- name: ${CONTRACT5.env.proxyPort}
|
|
1305
|
+
value: "${port}"
|
|
1306
|
+
- name: ${CONTRACT5.env.upstreamUrl}
|
|
1307
|
+
value: "http://${podPatch.loopback}:${CONTRACT5.ports.agentBind}"
|
|
1308
|
+
- name: ${CONTRACT5.env.manifestPath}
|
|
1309
|
+
value: ${paths.manifestFile}
|
|
1310
|
+
volumeMounts:
|
|
1311
|
+
- name: ${podPatch.manifestVolume}
|
|
1312
|
+
mountPath: ${paths.manifestDir}
|
|
1313
|
+
readOnly: true
|
|
1314
|
+
`;
|
|
1315
|
+
}
|
|
1316
|
+
function renderGatewayContainer(name, gatewayEnv) {
|
|
1317
|
+
const { podPatch, paths } = CONTRACT5;
|
|
1318
|
+
const secrets = secretEnv(name, [...gatewayEnv, CONTRACT5.env.gatewayKey].sort(), " ");
|
|
1319
|
+
return ` - name: ${podPatch.gatewayContainer}
|
|
1320
|
+
image: ${CONTRACT5.images.gateway}
|
|
1321
|
+
args: [-f, ${paths.gatewayConfigFile}]
|
|
1322
|
+
env:
|
|
1323
|
+
${secrets}
|
|
1324
|
+
resources:
|
|
1325
|
+
requests:
|
|
1326
|
+
cpu: ${podPatch.gatewayCpuRequest}
|
|
1327
|
+
memory: ${podPatch.gatewayMemoryRequest}
|
|
1328
|
+
limits:
|
|
1329
|
+
memory: ${podPatch.gatewayMemoryLimit}
|
|
1330
|
+
volumeMounts:
|
|
1331
|
+
- name: ${podPatch.gatewayConfigVolume}
|
|
1332
|
+
mountPath: ${paths.gatewayConfigDir}
|
|
1333
|
+
readOnly: true
|
|
1334
|
+
- name: ${podPatch.gatewayDataVolume}
|
|
1335
|
+
mountPath: ${paths.gatewayDataDir}
|
|
1336
|
+
`;
|
|
710
1337
|
}
|
|
711
|
-
function
|
|
712
|
-
const
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
1338
|
+
function renderVolumes(name) {
|
|
1339
|
+
const { podPatch } = CONTRACT5;
|
|
1340
|
+
return ` volumes:
|
|
1341
|
+
- name: ${podPatch.manifestVolume}
|
|
1342
|
+
configMap:
|
|
1343
|
+
name: ${name}-manifest
|
|
1344
|
+
- name: ${podPatch.gatewayConfigVolume}
|
|
1345
|
+
emptyDir: {}
|
|
1346
|
+
- name: ${podPatch.gatewayDataVolume}
|
|
1347
|
+
emptyDir: {}
|
|
1348
|
+
`;
|
|
1349
|
+
}
|
|
1350
|
+
function generateDeployment({ name, port, agentEnvRefs, gatewayEnv, agentImage, baseUrlEnv }) {
|
|
719
1351
|
return `apiVersion: apps/v1
|
|
720
1352
|
kind: Deployment
|
|
721
1353
|
metadata:
|
|
@@ -729,32 +1361,14 @@ spec:
|
|
|
729
1361
|
app: ${name}
|
|
730
1362
|
template:
|
|
731
1363
|
metadata:
|
|
1364
|
+
annotations:
|
|
1365
|
+
${CONTRACT5.podPatch.agentNameAnnotation}: ${name}
|
|
732
1366
|
labels:
|
|
733
1367
|
app: ${name}
|
|
734
1368
|
spec:
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
ports:
|
|
739
|
-
- containerPort: ${port}
|
|
740
|
-
envFrom:
|
|
741
|
-
- configMapRef:
|
|
742
|
-
name: ${name}-config
|
|
743
|
-
env:
|
|
744
|
-
${secretEnvBlock}
|
|
745
|
-
- name: agentspec-sidecar
|
|
746
|
-
image: ${SIDECAR_IMAGE}
|
|
747
|
-
ports:
|
|
748
|
-
- containerPort: ${PROXY_PORT}
|
|
749
|
-
name: proxy
|
|
750
|
-
- containerPort: ${CONTROL_PORT}
|
|
751
|
-
name: control
|
|
752
|
-
env:
|
|
753
|
-
- name: UPSTREAM_URL
|
|
754
|
-
value: "http://localhost:${port}"
|
|
755
|
-
- name: MANIFEST_PATH
|
|
756
|
-
value: /manifest/agent.yaml
|
|
757
|
-
`;
|
|
1369
|
+
initContainers:
|
|
1370
|
+
` + renderInitContainer() + ` containers:
|
|
1371
|
+
` + renderAgentContainer(name, agentImage, baseUrlEnv, agentEnvRefs) + renderSidecarContainer(port) + renderGatewayContainer(name, gatewayEnv) + renderVolumes(name);
|
|
758
1372
|
}
|
|
759
1373
|
function generateService(name, port) {
|
|
760
1374
|
return `apiVersion: v1
|
|
@@ -768,23 +1382,16 @@ spec:
|
|
|
768
1382
|
selector:
|
|
769
1383
|
app: ${name}
|
|
770
1384
|
ports:
|
|
771
|
-
- name:
|
|
1385
|
+
- name: ${CONTRACT5.podPatch.proxyPortName}
|
|
772
1386
|
port: ${port}
|
|
773
1387
|
targetPort: ${port}
|
|
774
|
-
- name:
|
|
775
|
-
port: ${
|
|
776
|
-
targetPort: ${
|
|
777
|
-
- name: control
|
|
778
|
-
port: ${CONTROL_PORT}
|
|
779
|
-
targetPort: ${CONTROL_PORT}
|
|
1388
|
+
- name: ${CONTRACT5.podPatch.controlPortName}
|
|
1389
|
+
port: ${CONTRACT5.ports.controlPlane}
|
|
1390
|
+
targetPort: ${CONTRACT5.ports.controlPlane}
|
|
780
1391
|
`;
|
|
781
1392
|
}
|
|
782
|
-
function generateConfigMap(name,
|
|
783
|
-
const data = [
|
|
784
|
-
` AGENT_NAME: "${escapeYamlDoubleQuotedValue(name)}"`,
|
|
785
|
-
` MODEL_PROVIDER: "${escapeYamlDoubleQuotedValue(manifest.spec.model.provider)}"`,
|
|
786
|
-
` MODEL_ID: "${escapeYamlDoubleQuotedValue(manifest.spec.model.id)}"`
|
|
787
|
-
].join("\n");
|
|
1393
|
+
function generateConfigMap(name, configVars) {
|
|
1394
|
+
const data = Object.entries(configVars).map(([key, value]) => ` ${key}: "${escapeYamlDoubleQuotedValue(value)}"`).join("\n");
|
|
788
1395
|
return `apiVersion: v1
|
|
789
1396
|
kind: ConfigMap
|
|
790
1397
|
metadata:
|
|
@@ -795,8 +1402,22 @@ data:
|
|
|
795
1402
|
${data}
|
|
796
1403
|
`;
|
|
797
1404
|
}
|
|
798
|
-
function
|
|
799
|
-
const
|
|
1405
|
+
function generateManifestConfigMap(name, manifest) {
|
|
1406
|
+
const body = dumpManifest(manifest);
|
|
1407
|
+
const indented = body.trimEnd().split("\n").map((l) => ` ${l}`).join("\n");
|
|
1408
|
+
return `apiVersion: v1
|
|
1409
|
+
kind: ConfigMap
|
|
1410
|
+
metadata:
|
|
1411
|
+
name: ${name}-manifest
|
|
1412
|
+
labels:
|
|
1413
|
+
app: ${name}
|
|
1414
|
+
data:
|
|
1415
|
+
agent.yaml: |
|
|
1416
|
+
${indented}
|
|
1417
|
+
`;
|
|
1418
|
+
}
|
|
1419
|
+
function generateSecretExample(name, secretKeys) {
|
|
1420
|
+
const dataLines = secretKeys.length > 0 ? secretKeys.map((v) => ` ${v}: <base64-encoded-value> # echo -n "<value>" | base64`).join("\n") : " # No $env: references found in manifest";
|
|
800
1421
|
return `# Example Kubernetes Secret \u2014 fill in real values and apply with:
|
|
801
1422
|
# kubectl apply -f k8s/secret.yaml
|
|
802
1423
|
# DO NOT commit real values. This file is a template only.
|
|
@@ -812,33 +1433,130 @@ ${dataLines}
|
|
|
812
1433
|
`;
|
|
813
1434
|
}
|
|
814
1435
|
|
|
1436
|
+
// src/deploy/compose.ts
|
|
1437
|
+
import { CONTRACT as CONTRACT6 } from "@agentspec/sdk";
|
|
1438
|
+
import * as yaml4 from "js-yaml";
|
|
1439
|
+
function generateComposeFiles(manifest) {
|
|
1440
|
+
const plan = planPodPatch(manifest);
|
|
1441
|
+
return {
|
|
1442
|
+
"compose/docker-compose.yaml": yaml4.dump(composeProject(plan), { lineWidth: -1, noRefs: true }),
|
|
1443
|
+
"compose/agent.yaml": dumpManifest(manifest),
|
|
1444
|
+
"compose/.env.example": envExample(plan.secretKeys)
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
function interpolate(vars) {
|
|
1448
|
+
return Object.fromEntries(vars.map((v) => [v, `\${${v}:?set ${v} in .env}`]));
|
|
1449
|
+
}
|
|
1450
|
+
function composeProject({ name, port, agentEnvRefs, gatewayEnv, configVars, agentImage, baseUrlEnv }) {
|
|
1451
|
+
const { podPatch, paths } = CONTRACT6;
|
|
1452
|
+
const agentNamespace = `service:${name}`;
|
|
1453
|
+
return {
|
|
1454
|
+
services: {
|
|
1455
|
+
[podPatch.gatewayConfigInitContainer]: {
|
|
1456
|
+
image: CONTRACT6.images.sidecar,
|
|
1457
|
+
command: gatewayConfigRenderCommand(),
|
|
1458
|
+
volumes: [`./agent.yaml:${paths.manifestFile}:ro`, `${podPatch.gatewayConfigVolume}:${paths.gatewayConfigDir}`]
|
|
1459
|
+
},
|
|
1460
|
+
[name]: {
|
|
1461
|
+
image: agentImage,
|
|
1462
|
+
environment: {
|
|
1463
|
+
...configVars,
|
|
1464
|
+
[CONTRACT6.env.agentHost]: podPatch.loopback,
|
|
1465
|
+
[CONTRACT6.env.agentPort]: String(CONTRACT6.ports.agentBind),
|
|
1466
|
+
...baseUrlEnv,
|
|
1467
|
+
...interpolate([...agentEnvRefs, CONTRACT6.env.gatewayKey].sort())
|
|
1468
|
+
},
|
|
1469
|
+
// The sidecar and gateway share this namespace, so the agent's service publishes their ports.
|
|
1470
|
+
// The control plane accepts proof writes without a token, so it stays on the host's loopback.
|
|
1471
|
+
ports: [
|
|
1472
|
+
`${port}:${port}`,
|
|
1473
|
+
`${podPatch.loopback}:${CONTRACT6.ports.controlPlane}:${CONTRACT6.ports.controlPlane}`
|
|
1474
|
+
]
|
|
1475
|
+
},
|
|
1476
|
+
[podPatch.sidecarContainer]: {
|
|
1477
|
+
image: CONTRACT6.images.sidecar,
|
|
1478
|
+
network_mode: agentNamespace,
|
|
1479
|
+
depends_on: [name],
|
|
1480
|
+
environment: {
|
|
1481
|
+
[CONTRACT6.env.proxyPort]: String(port),
|
|
1482
|
+
[CONTRACT6.env.upstreamUrl]: `http://${podPatch.loopback}:${CONTRACT6.ports.agentBind}`,
|
|
1483
|
+
[CONTRACT6.env.manifestPath]: paths.manifestFile
|
|
1484
|
+
},
|
|
1485
|
+
volumes: [`./agent.yaml:${paths.manifestFile}:ro`]
|
|
1486
|
+
},
|
|
1487
|
+
[podPatch.gatewayContainer]: {
|
|
1488
|
+
image: CONTRACT6.images.gateway,
|
|
1489
|
+
network_mode: agentNamespace,
|
|
1490
|
+
// The gateway image is non-root and a fresh named volume is root-owned, so the data dir is a world-writable
|
|
1491
|
+
// tmpfs instead (ephemeral and sticky, like the k8s emptyDir). The gateway keeps its own UID (ADR-0002, ADR-0004).
|
|
1492
|
+
tmpfs: [`${paths.gatewayDataDir}:mode=1777`],
|
|
1493
|
+
command: ["-f", paths.gatewayConfigFile],
|
|
1494
|
+
depends_on: {
|
|
1495
|
+
[podPatch.gatewayConfigInitContainer]: { condition: "service_completed_successfully" },
|
|
1496
|
+
[name]: { condition: "service_started" }
|
|
1497
|
+
},
|
|
1498
|
+
environment: interpolate([...gatewayEnv, CONTRACT6.env.gatewayKey].sort()),
|
|
1499
|
+
volumes: [
|
|
1500
|
+
`${podPatch.gatewayConfigVolume}:${paths.gatewayConfigDir}:ro`
|
|
1501
|
+
],
|
|
1502
|
+
deploy: {
|
|
1503
|
+
resources: {
|
|
1504
|
+
reservations: { cpus: composeCpu(podPatch.gatewayCpuRequest), memory: composeMemory(podPatch.gatewayMemoryRequest) },
|
|
1505
|
+
limits: { memory: composeMemory(podPatch.gatewayMemoryLimit) }
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
},
|
|
1510
|
+
volumes: { [podPatch.gatewayConfigVolume]: {} }
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
function composeMemory(quantity) {
|
|
1514
|
+
if (!/^\d+Mi$/.test(quantity)) {
|
|
1515
|
+
throw new Error(`Cannot convert memory "${quantity}" to compose; the contract uses whole Mi values (e.g. 128Mi).`);
|
|
1516
|
+
}
|
|
1517
|
+
return quantity.replace(/Mi$/, "m");
|
|
1518
|
+
}
|
|
1519
|
+
function composeCpu(quantity) {
|
|
1520
|
+
if (!/^\d+m$/.test(quantity)) {
|
|
1521
|
+
throw new Error(`Cannot convert cpu "${quantity}" to compose; the contract uses millicores (e.g. 25m).`);
|
|
1522
|
+
}
|
|
1523
|
+
return String(Number.parseInt(quantity, 10) / 1e3);
|
|
1524
|
+
}
|
|
1525
|
+
function envExample(secretKeys) {
|
|
1526
|
+
const lines = secretKeys.map((v) => `${v}=`);
|
|
1527
|
+
return `# Copy to .env and fill in real values. DO NOT commit real values.
|
|
1528
|
+
# Provider keys are read by the ${CONTRACT6.podPatch.gatewayContainer} service only.
|
|
1529
|
+
${lines.join("\n")}
|
|
1530
|
+
`;
|
|
1531
|
+
}
|
|
1532
|
+
|
|
815
1533
|
// src/commands/generate.ts
|
|
816
|
-
var DEPLOY_TARGETS = ["k8s", "helm"];
|
|
1534
|
+
var DEPLOY_TARGETS = ["k8s", "compose", "helm"];
|
|
817
1535
|
function writeGeneratedFiles(files, outDir) {
|
|
818
|
-
const resolvedOutDir =
|
|
819
|
-
|
|
1536
|
+
const resolvedOutDir = resolve3(outDir);
|
|
1537
|
+
mkdirSync2(resolvedOutDir, { recursive: true });
|
|
820
1538
|
const safeOutDir = resolvedOutDir.endsWith(sep) ? resolvedOutDir : resolvedOutDir + sep;
|
|
821
1539
|
for (const [filename, content] of Object.entries(files)) {
|
|
822
|
-
const outPath =
|
|
1540
|
+
const outPath = resolve3(resolvedOutDir, filename);
|
|
823
1541
|
if (!outPath.startsWith(safeOutDir) && outPath !== resolvedOutDir) {
|
|
824
1542
|
throw new Error(`Rejected generated filename "${filename}" \u2014 path traversal detected`);
|
|
825
1543
|
}
|
|
826
|
-
|
|
1544
|
+
mkdirSync2(dirname2(outPath), { recursive: true });
|
|
827
1545
|
writeFileSync3(outPath, content, "utf-8");
|
|
828
|
-
console.log(` ${
|
|
1546
|
+
console.log(` ${chalk7.green("\u2713")} ${filename}`);
|
|
829
1547
|
}
|
|
830
1548
|
}
|
|
831
1549
|
function copyManifestToOutput(srcFile, outDir, alreadyGenerated) {
|
|
832
|
-
const manifestSrc =
|
|
833
|
-
const resolvedOutDir =
|
|
834
|
-
if (!
|
|
1550
|
+
const manifestSrc = resolve3(srcFile);
|
|
1551
|
+
const resolvedOutDir = resolve3(outDir);
|
|
1552
|
+
if (!existsSync3(manifestSrc) || alreadyGenerated[basename(manifestSrc)] !== void 0) {
|
|
835
1553
|
return;
|
|
836
1554
|
}
|
|
837
1555
|
try {
|
|
838
|
-
let content =
|
|
1556
|
+
let content = readFileSync2(manifestSrc, "utf-8");
|
|
839
1557
|
content = content.replace(/\$secret:[^\s\n'"]+/g, "<redacted>");
|
|
840
|
-
writeFileSync3(
|
|
841
|
-
console.log(` ${
|
|
1558
|
+
writeFileSync3(join3(resolvedOutDir, basename(manifestSrc)), content, "utf-8");
|
|
1559
|
+
console.log(` ${chalk7.green("\u2713")} ${basename(manifestSrc)} (manifest copy)`);
|
|
842
1560
|
} catch {
|
|
843
1561
|
}
|
|
844
1562
|
}
|
|
@@ -848,34 +1566,51 @@ function validateFramework(framework) {
|
|
|
848
1566
|
available = listFrameworks();
|
|
849
1567
|
} catch {
|
|
850
1568
|
printError(
|
|
851
|
-
"Failed to load available frameworks. Is @agentspec/
|
|
1569
|
+
"Failed to load available frameworks. Is @agentspec/codegen installed correctly?\n Try: pnpm --filter @agentspec/codegen build"
|
|
852
1570
|
);
|
|
853
1571
|
process.exit(1);
|
|
854
1572
|
}
|
|
855
1573
|
if (!available.includes(framework)) {
|
|
856
|
-
printError(
|
|
1574
|
+
printError(
|
|
1575
|
+
`Framework "${framework}" is not supported.
|
|
1576
|
+
Available: ${available.join(", ")}
|
|
1577
|
+
Add a new one: packages/codegen/src/skills/${framework}.md`
|
|
1578
|
+
);
|
|
857
1579
|
process.exit(1);
|
|
858
1580
|
}
|
|
859
1581
|
}
|
|
860
|
-
|
|
861
|
-
|
|
1582
|
+
var DETERMINISTIC_DEPLOYS = {
|
|
1583
|
+
k8s: { title: "Kubernetes", render: generateK8sManifests },
|
|
1584
|
+
compose: { title: "Docker Compose", render: generateComposeFiles }
|
|
1585
|
+
};
|
|
1586
|
+
function handleDeterministicDeploy(target, manifest, outDir, outputOpt, dryRun = false) {
|
|
1587
|
+
const { title, render } = DETERMINISTIC_DEPLOYS[target];
|
|
1588
|
+
printHeader(`AgentSpec Deploy \u2014 ${title}`);
|
|
862
1589
|
console.log();
|
|
863
1590
|
try {
|
|
864
|
-
|
|
1591
|
+
const files = render(manifest);
|
|
1592
|
+
if (dryRun) {
|
|
1593
|
+
printDryRunOutput(files);
|
|
1594
|
+
return;
|
|
1595
|
+
}
|
|
1596
|
+
writeGeneratedFiles(files, outDir);
|
|
865
1597
|
} catch (err) {
|
|
866
1598
|
printError(String(err));
|
|
867
1599
|
process.exit(1);
|
|
868
1600
|
}
|
|
869
1601
|
printSuccess(`Written to ${outputOpt}`);
|
|
870
1602
|
}
|
|
871
|
-
async function handleLLMGeneration(manifest, framework, manifestDir, spin,
|
|
1603
|
+
async function handleLLMGeneration(manifest, framework, manifestDir, spin, provider) {
|
|
872
1604
|
try {
|
|
873
|
-
return await
|
|
1605
|
+
return await generateCode(manifest, {
|
|
874
1606
|
framework,
|
|
875
1607
|
manifestDir,
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1608
|
+
provider,
|
|
1609
|
+
onChunk: (chunk) => {
|
|
1610
|
+
if (chunk.type === "delta" || chunk.type === "heartbeat") {
|
|
1611
|
+
const kb = chunk.type === "delta" ? ` \xB7 ${(chunk.accumulated.length / 1024).toFixed(1)}k chars` : "";
|
|
1612
|
+
spin.message(`Generating with ${provider.name} \xB7 ${chunk.elapsedSec}s${kb}`);
|
|
1613
|
+
}
|
|
879
1614
|
}
|
|
880
1615
|
});
|
|
881
1616
|
} catch (err) {
|
|
@@ -890,17 +1625,17 @@ function writePushModeEnv(outDir) {
|
|
|
890
1625
|
"AGENTSPEC_KEY=<paste key from: agentspec register>",
|
|
891
1626
|
""
|
|
892
1627
|
].join("\n");
|
|
893
|
-
writeFileSync3(
|
|
1628
|
+
writeFileSync3(join3(outDir, ".env.agentspec"), envContent, "utf-8");
|
|
894
1629
|
console.log(
|
|
895
|
-
` ${
|
|
1630
|
+
` ${chalk7.green("\u2713")} .env.agentspec (push mode \u2014 paste your key from agentspec register)`
|
|
896
1631
|
);
|
|
897
1632
|
}
|
|
898
1633
|
function printDryRunOutput(files) {
|
|
899
1634
|
console.log();
|
|
900
|
-
console.log(
|
|
1635
|
+
console.log(chalk7.bold(" Files to be generated:"));
|
|
901
1636
|
for (const [filename, content] of Object.entries(files)) {
|
|
902
1637
|
console.log();
|
|
903
|
-
console.log(
|
|
1638
|
+
console.log(chalk7.cyan(` \u2500\u2500 ${filename} \u2500\u2500`));
|
|
904
1639
|
console.log(content.split("\n").map((l) => ` ${l}`).join("\n"));
|
|
905
1640
|
}
|
|
906
1641
|
console.log();
|
|
@@ -908,41 +1643,53 @@ function printDryRunOutput(files) {
|
|
|
908
1643
|
function printPostGeneration(generated, outputDir) {
|
|
909
1644
|
console.log();
|
|
910
1645
|
if (generated.installCommands.length > 0) {
|
|
911
|
-
console.log(
|
|
1646
|
+
console.log(chalk7.bold(" Install commands:"));
|
|
912
1647
|
for (const cmd of generated.installCommands) {
|
|
913
|
-
console.log(` ${
|
|
1648
|
+
console.log(` ${chalk7.gray("$")} ${chalk7.cyan(cmd)}`);
|
|
914
1649
|
}
|
|
915
1650
|
console.log();
|
|
916
1651
|
}
|
|
917
1652
|
if (generated.envVars.length > 0) {
|
|
918
|
-
console.log(
|
|
1653
|
+
console.log(chalk7.bold(" Required env vars:"));
|
|
919
1654
|
for (const v of generated.envVars) {
|
|
920
|
-
console.log(` ${
|
|
1655
|
+
console.log(` ${chalk7.gray(v)}`);
|
|
921
1656
|
}
|
|
922
1657
|
console.log();
|
|
923
1658
|
}
|
|
924
1659
|
printSuccess(`Written to ${outputDir}`);
|
|
925
1660
|
}
|
|
926
|
-
async function runDeployTarget(target, manifest, outDir) {
|
|
1661
|
+
async function runDeployTarget(target, manifest, outDir, provider) {
|
|
927
1662
|
if (target === "k8s") {
|
|
928
1663
|
console.log();
|
|
929
|
-
console.log(
|
|
1664
|
+
console.log(chalk7.bold(" Kubernetes manifests:"));
|
|
930
1665
|
const deployFiles = generateK8sManifests(manifest);
|
|
931
1666
|
writeGeneratedFiles(deployFiles, outDir);
|
|
932
1667
|
return;
|
|
933
1668
|
}
|
|
934
1669
|
if (target === "helm") {
|
|
935
1670
|
console.log();
|
|
936
|
-
console.log(
|
|
937
|
-
|
|
1671
|
+
console.log(chalk7.bold(" Helm chart (LLM-generated):"));
|
|
1672
|
+
let helmGenerated;
|
|
1673
|
+
try {
|
|
1674
|
+
helmGenerated = await generateCode(manifest, { framework: "helm", provider });
|
|
1675
|
+
} catch (err) {
|
|
1676
|
+
printError(`Helm generation failed: ${String(err)}`);
|
|
1677
|
+
process.exit(1);
|
|
1678
|
+
}
|
|
938
1679
|
writeGeneratedFiles(helmGenerated.files, outDir);
|
|
939
1680
|
}
|
|
940
1681
|
}
|
|
941
1682
|
function registerGenerateCommand(program2) {
|
|
942
|
-
program2.command("generate <file>").description("Generate framework-specific agent code from a manifest").requiredOption(
|
|
1683
|
+
program2.command("generate <file>").description("Generate framework-specific agent code from a manifest").requiredOption(
|
|
1684
|
+
"--framework <fw>",
|
|
1685
|
+
"Target framework (e.g. langgraph, crewai, mastra)"
|
|
1686
|
+
).option("--output <dir>", "Output directory", "./generated").option("--dry-run", "Print generated files without writing them").option(
|
|
943
1687
|
"--deploy <target>",
|
|
944
1688
|
`Also generate deployment manifests: ${DEPLOY_TARGETS.join(", ")}`
|
|
945
|
-
).option("--push", "Write .env.agentspec with push mode env var placeholders").
|
|
1689
|
+
).option("--push", "Write .env.agentspec with push mode env var placeholders").option(
|
|
1690
|
+
"--provider <name>",
|
|
1691
|
+
"Override codegen provider: claude-sub, anthropic-api, openai-compatible"
|
|
1692
|
+
).action(
|
|
946
1693
|
async (file, opts) => {
|
|
947
1694
|
validateFramework(opts.framework);
|
|
948
1695
|
if (opts.deploy && !DEPLOY_TARGETS.includes(opts.deploy)) {
|
|
@@ -953,32 +1700,34 @@ function registerGenerateCommand(program2) {
|
|
|
953
1700
|
}
|
|
954
1701
|
let parsed;
|
|
955
1702
|
try {
|
|
956
|
-
parsed =
|
|
1703
|
+
parsed = loadManifest5(file, { resolve: false });
|
|
957
1704
|
} catch (err) {
|
|
958
1705
|
printError(`Cannot load manifest: ${String(err)}`);
|
|
959
1706
|
process.exit(1);
|
|
960
1707
|
}
|
|
961
|
-
if (opts.deploy
|
|
962
|
-
|
|
1708
|
+
if (opts.deploy && Object.hasOwn(DETERMINISTIC_DEPLOYS, opts.deploy)) {
|
|
1709
|
+
handleDeterministicDeploy(opts.deploy, parsed.manifest, resolve3(opts.output), opts.output, opts.dryRun);
|
|
963
1710
|
return;
|
|
964
1711
|
}
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
1712
|
+
printHeader(`AgentSpec Generate \u2014 ${opts.framework}`);
|
|
1713
|
+
const spin = spinner2();
|
|
1714
|
+
spin.start("Checking provider\u2026");
|
|
1715
|
+
let provider;
|
|
1716
|
+
try {
|
|
1717
|
+
provider = resolveProvider(opts.provider);
|
|
1718
|
+
} catch (err) {
|
|
1719
|
+
spin.stop("Provider unavailable");
|
|
1720
|
+
printError(`Codegen provider unavailable: ${String(err)}`);
|
|
969
1721
|
process.exit(1);
|
|
970
1722
|
}
|
|
971
|
-
|
|
972
|
-
const
|
|
973
|
-
const spin = spinner();
|
|
974
|
-
spin.start(`Generating with ${displayModel}`);
|
|
975
|
-
const manifestDir = dirname(resolve2(file));
|
|
1723
|
+
spin.message(`Generating with ${provider.name}`);
|
|
1724
|
+
const manifestDir = dirname2(resolve3(file));
|
|
976
1725
|
const generated = await handleLLMGeneration(
|
|
977
1726
|
parsed.manifest,
|
|
978
1727
|
opts.framework,
|
|
979
1728
|
manifestDir,
|
|
980
1729
|
spin,
|
|
981
|
-
|
|
1730
|
+
provider
|
|
982
1731
|
);
|
|
983
1732
|
const totalKb = (Object.values(generated.files).reduce((n, c) => n + c.length, 0) / 1024).toFixed(1);
|
|
984
1733
|
spin.stop(`Generated ${Object.keys(generated.files).length} files \xB7 ${totalKb}k chars`);
|
|
@@ -986,7 +1735,7 @@ function registerGenerateCommand(program2) {
|
|
|
986
1735
|
printDryRunOutput(generated.files);
|
|
987
1736
|
return;
|
|
988
1737
|
}
|
|
989
|
-
const outDir =
|
|
1738
|
+
const outDir = resolve3(opts.output);
|
|
990
1739
|
console.log();
|
|
991
1740
|
try {
|
|
992
1741
|
writeGeneratedFiles(generated.files, outDir);
|
|
@@ -999,7 +1748,7 @@ function registerGenerateCommand(program2) {
|
|
|
999
1748
|
writePushModeEnv(outDir);
|
|
1000
1749
|
}
|
|
1001
1750
|
if (opts.deploy === "helm") {
|
|
1002
|
-
await runDeployTarget("helm", parsed.manifest, outDir);
|
|
1751
|
+
await runDeployTarget("helm", parsed.manifest, outDir, provider);
|
|
1003
1752
|
}
|
|
1004
1753
|
printPostGeneration(generated, opts.output);
|
|
1005
1754
|
}
|
|
@@ -1007,12 +1756,12 @@ function registerGenerateCommand(program2) {
|
|
|
1007
1756
|
}
|
|
1008
1757
|
|
|
1009
1758
|
// src/commands/export.ts
|
|
1010
|
-
import { loadManifest as
|
|
1759
|
+
import { loadManifest as loadManifest6 } from "@agentspec/sdk";
|
|
1011
1760
|
function registerExportCommand(program2) {
|
|
1012
1761
|
program2.command("export <file>").description("Export manifest to other formats (agentcard, agents-md-block)").requiredOption("--format <fmt>", "Export format: agentcard|agents-md-block").action(async (file, opts) => {
|
|
1013
1762
|
let parsed;
|
|
1014
1763
|
try {
|
|
1015
|
-
parsed =
|
|
1764
|
+
parsed = loadManifest6(file, { resolve: false });
|
|
1016
1765
|
} catch (err) {
|
|
1017
1766
|
printError(`Cannot load manifest: ${String(err)}`);
|
|
1018
1767
|
process.exit(1);
|
|
@@ -1092,10 +1841,10 @@ npx agentspec generate agent.yaml --framework langgraph --output ./generated/
|
|
|
1092
1841
|
}
|
|
1093
1842
|
|
|
1094
1843
|
// src/commands/migrate.ts
|
|
1095
|
-
import { readFileSync as
|
|
1096
|
-
import { resolve as
|
|
1097
|
-
import
|
|
1098
|
-
import
|
|
1844
|
+
import { readFileSync as readFileSync3, writeFileSync as writeFileSync4 } from "fs";
|
|
1845
|
+
import { resolve as resolve4 } from "path";
|
|
1846
|
+
import chalk8 from "chalk";
|
|
1847
|
+
import * as yaml5 from "js-yaml";
|
|
1099
1848
|
import {
|
|
1100
1849
|
migrateManifest,
|
|
1101
1850
|
detectVersion,
|
|
@@ -1105,10 +1854,10 @@ import {
|
|
|
1105
1854
|
function registerMigrateCommand(program2) {
|
|
1106
1855
|
program2.command("migrate <file>").description("Migrate an agent.yaml manifest to the latest schema version").option("--dry-run", "Show what would change without writing files").option("-o, --output <file>", "Write migrated manifest to a different file").action(async (file, opts) => {
|
|
1107
1856
|
printHeader("AgentSpec Migrate");
|
|
1108
|
-
const absPath =
|
|
1857
|
+
const absPath = resolve4(file);
|
|
1109
1858
|
let raw;
|
|
1110
1859
|
try {
|
|
1111
|
-
raw =
|
|
1860
|
+
raw = readFileSync3(absPath, "utf-8");
|
|
1112
1861
|
} catch (err) {
|
|
1113
1862
|
printError(`Cannot read manifest: ${absPath}
|
|
1114
1863
|
${String(err)}`);
|
|
@@ -1116,14 +1865,14 @@ function registerMigrateCommand(program2) {
|
|
|
1116
1865
|
}
|
|
1117
1866
|
let parsed;
|
|
1118
1867
|
try {
|
|
1119
|
-
parsed =
|
|
1868
|
+
parsed = yaml5.load(raw);
|
|
1120
1869
|
} catch (err) {
|
|
1121
1870
|
printError(`Invalid YAML: ${String(err)}`);
|
|
1122
1871
|
process.exit(1);
|
|
1123
1872
|
}
|
|
1124
1873
|
const fromVersion = detectVersion(parsed);
|
|
1125
|
-
console.log(
|
|
1126
|
-
console.log(
|
|
1874
|
+
console.log(chalk8.gray(` File : ${absPath}`));
|
|
1875
|
+
console.log(chalk8.gray(` Version : ${fromVersion}`));
|
|
1127
1876
|
console.log();
|
|
1128
1877
|
if (isLatestVersion(parsed)) {
|
|
1129
1878
|
printSuccess(`Already at latest version (${LATEST_API_VERSION}) \u2014 no migration needed.`);
|
|
@@ -1131,25 +1880,25 @@ function registerMigrateCommand(program2) {
|
|
|
1131
1880
|
}
|
|
1132
1881
|
const { result, migrationsApplied } = migrateManifest(parsed);
|
|
1133
1882
|
if (migrationsApplied.length === 0) {
|
|
1134
|
-
console.log(
|
|
1135
|
-
console.log(
|
|
1883
|
+
console.log(chalk8.yellow(` \u26A0 No migration path found from ${fromVersion} to ${LATEST_API_VERSION}.`));
|
|
1884
|
+
console.log(chalk8.gray(" Manual update may be required."));
|
|
1136
1885
|
console.log();
|
|
1137
1886
|
process.exit(1);
|
|
1138
1887
|
}
|
|
1139
|
-
console.log(
|
|
1888
|
+
console.log(chalk8.cyan(" Migrations applied:"));
|
|
1140
1889
|
for (const m of migrationsApplied) {
|
|
1141
|
-
console.log(
|
|
1890
|
+
console.log(chalk8.gray(` \u2022 ${m}`));
|
|
1142
1891
|
}
|
|
1143
1892
|
console.log();
|
|
1144
|
-
const migratedYaml =
|
|
1893
|
+
const migratedYaml = yaml5.dump(result, { lineWidth: 120 });
|
|
1145
1894
|
if (opts.dryRun) {
|
|
1146
|
-
console.log(
|
|
1147
|
-
console.log(
|
|
1148
|
-
console.log(
|
|
1895
|
+
console.log(chalk8.cyan(" --- Migrated manifest (dry-run) ---"));
|
|
1896
|
+
console.log(chalk8.gray(migratedYaml));
|
|
1897
|
+
console.log(chalk8.yellow(" Dry run \u2014 no files written."));
|
|
1149
1898
|
console.log();
|
|
1150
1899
|
return;
|
|
1151
1900
|
}
|
|
1152
|
-
const outputPath = opts.output ?
|
|
1901
|
+
const outputPath = opts.output ? resolve4(opts.output) : absPath;
|
|
1153
1902
|
try {
|
|
1154
1903
|
writeFileSync4(outputPath, migratedYaml, "utf-8");
|
|
1155
1904
|
} catch (err) {
|
|
@@ -1163,16 +1912,16 @@ function registerMigrateCommand(program2) {
|
|
|
1163
1912
|
|
|
1164
1913
|
// src/commands/scan.ts
|
|
1165
1914
|
import {
|
|
1166
|
-
existsSync as
|
|
1915
|
+
existsSync as existsSync5,
|
|
1167
1916
|
lstatSync,
|
|
1168
|
-
readdirSync,
|
|
1169
|
-
readFileSync as
|
|
1917
|
+
readdirSync as readdirSync2,
|
|
1918
|
+
readFileSync as readFileSync5,
|
|
1170
1919
|
realpathSync,
|
|
1171
1920
|
writeFileSync as writeFileSync5
|
|
1172
1921
|
} from "fs";
|
|
1173
|
-
import { extname, join as
|
|
1174
|
-
import * as
|
|
1175
|
-
import {
|
|
1922
|
+
import { extname, join as join5, resolve as resolve6 } from "path";
|
|
1923
|
+
import * as jsYaml2 from "js-yaml";
|
|
1924
|
+
import { generateCode as generateCode2, repairYaml, resolveProvider as resolveProvider2 } from "@agentspec/codegen";
|
|
1176
1925
|
import { ManifestSchema } from "@agentspec/sdk";
|
|
1177
1926
|
|
|
1178
1927
|
// src/commands/scan-builder.ts
|
|
@@ -1339,6 +2088,149 @@ function buildManifestFromDetection(d) {
|
|
|
1339
2088
|
};
|
|
1340
2089
|
}
|
|
1341
2090
|
|
|
2091
|
+
// src/commands/scan-claude-code.ts
|
|
2092
|
+
import { existsSync as existsSync4, readdirSync, readFileSync as readFileSync4 } from "fs";
|
|
2093
|
+
import { basename as basename2, join as join4, resolve as resolve5 } from "path";
|
|
2094
|
+
import * as jsYaml from "js-yaml";
|
|
2095
|
+
var DESTRUCTIVE_BUILTINS = /* @__PURE__ */ new Set(["Bash", "Write", "Edit", "MultiEdit", "NotebookEdit"]);
|
|
2096
|
+
var READONLY_BUILTINS = /* @__PURE__ */ new Set(["Read", "Grep", "Glob", "WebFetch", "WebSearch", "TodoWrite"]);
|
|
2097
|
+
function readJsonIfPresent(path) {
|
|
2098
|
+
if (!existsSync4(path)) return void 0;
|
|
2099
|
+
try {
|
|
2100
|
+
return JSON.parse(readFileSync4(path, "utf-8"));
|
|
2101
|
+
} catch {
|
|
2102
|
+
return void 0;
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
function loadSettings(dir) {
|
|
2106
|
+
const base = readJsonIfPresent(join4(dir, ".claude", "settings.json")) ?? {};
|
|
2107
|
+
const local = readJsonIfPresent(join4(dir, ".claude", "settings.local.json")) ?? {};
|
|
2108
|
+
return {
|
|
2109
|
+
model: local.model ?? base.model,
|
|
2110
|
+
permissions: {
|
|
2111
|
+
allow: unionArrays(base.permissions?.allow, local.permissions?.allow),
|
|
2112
|
+
deny: unionArrays(base.permissions?.deny, local.permissions?.deny),
|
|
2113
|
+
ask: unionArrays(base.permissions?.ask, local.permissions?.ask)
|
|
2114
|
+
},
|
|
2115
|
+
mcpServers: { ...base.mcpServers, ...local.mcpServers }
|
|
2116
|
+
};
|
|
2117
|
+
}
|
|
2118
|
+
function unionArrays(a, b) {
|
|
2119
|
+
return [.../* @__PURE__ */ new Set([...a ?? [], ...b ?? []])];
|
|
2120
|
+
}
|
|
2121
|
+
function permissionToToolId(entry) {
|
|
2122
|
+
const withoutArgs = entry.split("(")[0]?.trim();
|
|
2123
|
+
return withoutArgs && withoutArgs.length > 0 ? withoutArgs : void 0;
|
|
2124
|
+
}
|
|
2125
|
+
function buildTool(toolId) {
|
|
2126
|
+
if (toolId.startsWith("mcp__")) {
|
|
2127
|
+
return {
|
|
2128
|
+
name: slugify(toolId),
|
|
2129
|
+
type: "mcp",
|
|
2130
|
+
description: `MCP tool exposed to Claude Code (${toolId})`
|
|
2131
|
+
};
|
|
2132
|
+
}
|
|
2133
|
+
const annotations = DESTRUCTIVE_BUILTINS.has(toolId) ? { destructiveHint: true } : READONLY_BUILTINS.has(toolId) ? { readOnlyHint: true } : void 0;
|
|
2134
|
+
return {
|
|
2135
|
+
name: slugify(toolId),
|
|
2136
|
+
type: "builtin",
|
|
2137
|
+
description: `Claude Code builtin tool (${toolId})`,
|
|
2138
|
+
...annotations ? { annotations } : {}
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
function buildTools2(settings) {
|
|
2142
|
+
const allow = settings.permissions?.allow ?? [];
|
|
2143
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2144
|
+
const tools = [];
|
|
2145
|
+
for (const entry of allow) {
|
|
2146
|
+
const toolId = permissionToToolId(entry);
|
|
2147
|
+
if (!toolId) continue;
|
|
2148
|
+
const tool = buildTool(toolId);
|
|
2149
|
+
if (seen.has(tool.name)) continue;
|
|
2150
|
+
seen.add(tool.name);
|
|
2151
|
+
tools.push(tool);
|
|
2152
|
+
}
|
|
2153
|
+
return tools.length ? tools : void 0;
|
|
2154
|
+
}
|
|
2155
|
+
function normalizeTransport(raw) {
|
|
2156
|
+
if (raw.type === "http" || raw.type === "sse" || raw.type === "stdio") return raw.type;
|
|
2157
|
+
if (raw.url) return "http";
|
|
2158
|
+
return "stdio";
|
|
2159
|
+
}
|
|
2160
|
+
function buildMcp(dir, settings) {
|
|
2161
|
+
const fromFile = readJsonIfPresent(
|
|
2162
|
+
join4(dir, ".mcp.json")
|
|
2163
|
+
)?.mcpServers;
|
|
2164
|
+
const merged = { ...fromFile, ...settings.mcpServers };
|
|
2165
|
+
const servers = Object.entries(merged).map(([name, raw]) => ({
|
|
2166
|
+
name: slugify(name),
|
|
2167
|
+
transport: normalizeTransport(raw),
|
|
2168
|
+
...raw.command ? { command: raw.command } : {},
|
|
2169
|
+
...raw.args?.length ? { args: raw.args } : {},
|
|
2170
|
+
...raw.url ? { url: raw.url } : {}
|
|
2171
|
+
}));
|
|
2172
|
+
return servers.length ? { servers } : void 0;
|
|
2173
|
+
}
|
|
2174
|
+
function parseAgentFrontmatterName(content, fallback) {
|
|
2175
|
+
const match = content.match(/^---\s*\n([\s\S]*?)\n---/);
|
|
2176
|
+
if (match?.[1]) {
|
|
2177
|
+
try {
|
|
2178
|
+
const fm = jsYaml.load(match[1]);
|
|
2179
|
+
if (fm?.name) return fm.name;
|
|
2180
|
+
} catch {
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
return fallback;
|
|
2184
|
+
}
|
|
2185
|
+
function buildSubagents(dir) {
|
|
2186
|
+
const agentsDir = join4(dir, ".claude", "agents");
|
|
2187
|
+
if (!existsSync4(agentsDir)) return void 0;
|
|
2188
|
+
const subagents = [];
|
|
2189
|
+
for (const file of readdirSync(agentsDir).sort()) {
|
|
2190
|
+
if (!file.endsWith(".md")) continue;
|
|
2191
|
+
const abs = join4(agentsDir, file);
|
|
2192
|
+
let content = "";
|
|
2193
|
+
try {
|
|
2194
|
+
content = readFileSync4(abs, "utf-8");
|
|
2195
|
+
} catch {
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
const rawName = parseAgentFrontmatterName(content, basename2(file, ".md"));
|
|
2199
|
+
subagents.push({
|
|
2200
|
+
name: slugify(rawName),
|
|
2201
|
+
ref: { agentspec: join4(".claude", "agents", file) },
|
|
2202
|
+
invocation: "on-demand",
|
|
2203
|
+
passContext: false
|
|
2204
|
+
});
|
|
2205
|
+
}
|
|
2206
|
+
return subagents.length ? subagents : void 0;
|
|
2207
|
+
}
|
|
2208
|
+
function buildDetection(dir, settings) {
|
|
2209
|
+
const hasClaudeMd = existsSync4(join4(dir, "CLAUDE.md"));
|
|
2210
|
+
return {
|
|
2211
|
+
name: basename2(resolve5(dir)) || "claude-code-agent",
|
|
2212
|
+
description: "Claude Code project agent (scanned from .claude/ config)",
|
|
2213
|
+
modelProvider: "anthropic",
|
|
2214
|
+
modelId: settings.model ?? "claude-sonnet-5",
|
|
2215
|
+
modelApiKeyEnv: "ANTHROPIC_API_KEY",
|
|
2216
|
+
...hasClaudeMd ? { promptFile: "CLAUDE.md" } : {},
|
|
2217
|
+
// Tools are attached post-build with Claude-specific types — leave empty here.
|
|
2218
|
+
tools: [],
|
|
2219
|
+
envVars: ["ANTHROPIC_API_KEY"]
|
|
2220
|
+
};
|
|
2221
|
+
}
|
|
2222
|
+
function buildManifestFromClaudeConfig(dir) {
|
|
2223
|
+
const settings = loadSettings(dir);
|
|
2224
|
+
const manifest = buildManifestFromDetection(buildDetection(dir, settings));
|
|
2225
|
+
const tools = buildTools2(settings);
|
|
2226
|
+
const mcp = buildMcp(dir, settings);
|
|
2227
|
+
const subagents = buildSubagents(dir);
|
|
2228
|
+
if (tools) manifest.spec.tools = tools;
|
|
2229
|
+
if (mcp) manifest.spec.mcp = mcp;
|
|
2230
|
+
if (subagents) manifest.spec.subagents = subagents;
|
|
2231
|
+
return manifest;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
1342
2234
|
// src/commands/scan.ts
|
|
1343
2235
|
var MAX_FILES = 50;
|
|
1344
2236
|
var MAX_BYTES = 200 * 1024;
|
|
@@ -1364,29 +2256,26 @@ var SKIP_DIRS = /* @__PURE__ */ new Set([
|
|
|
1364
2256
|
"out",
|
|
1365
2257
|
".turbo"
|
|
1366
2258
|
]);
|
|
1367
|
-
function
|
|
2259
|
+
function collectSourceFilesWithCount(srcDir, maxFiles = MAX_FILES, maxBytes = MAX_BYTES) {
|
|
1368
2260
|
let resolvedBase;
|
|
1369
2261
|
try {
|
|
1370
|
-
resolvedBase = realpathSync(
|
|
2262
|
+
resolvedBase = realpathSync(resolve6(srcDir));
|
|
1371
2263
|
} catch {
|
|
1372
|
-
resolvedBase =
|
|
2264
|
+
resolvedBase = resolve6(srcDir);
|
|
1373
2265
|
}
|
|
1374
2266
|
const results = [];
|
|
1375
2267
|
let totalBytes = 0;
|
|
2268
|
+
let totalFound = 0;
|
|
1376
2269
|
function walk(dir) {
|
|
1377
|
-
if (results.length >= maxFiles) return;
|
|
1378
|
-
if (totalBytes >= maxBytes) return;
|
|
1379
2270
|
let entries;
|
|
1380
2271
|
try {
|
|
1381
|
-
entries =
|
|
2272
|
+
entries = readdirSync2(dir).sort();
|
|
1382
2273
|
} catch {
|
|
1383
2274
|
return;
|
|
1384
2275
|
}
|
|
1385
2276
|
for (const entry of entries) {
|
|
1386
|
-
if (results.length >= maxFiles) break;
|
|
1387
|
-
if (totalBytes >= maxBytes) break;
|
|
1388
2277
|
if (entry.startsWith(".") || SKIP_DIRS.has(entry)) continue;
|
|
1389
|
-
const fullPath =
|
|
2278
|
+
const fullPath = join5(dir, entry);
|
|
1390
2279
|
let stat;
|
|
1391
2280
|
try {
|
|
1392
2281
|
stat = lstatSync(fullPath);
|
|
@@ -1411,9 +2300,12 @@ function collectSourceFiles(srcDir, maxFiles = MAX_FILES, maxBytes = MAX_BYTES)
|
|
|
1411
2300
|
continue;
|
|
1412
2301
|
}
|
|
1413
2302
|
if (!realPath.startsWith(resolvedBase + "/") && realPath !== resolvedBase) continue;
|
|
2303
|
+
totalFound++;
|
|
2304
|
+
if (results.length >= maxFiles) continue;
|
|
2305
|
+
if (totalBytes >= maxBytes) continue;
|
|
1414
2306
|
let content;
|
|
1415
2307
|
try {
|
|
1416
|
-
content =
|
|
2308
|
+
content = readFileSync5(fullPath, "utf-8");
|
|
1417
2309
|
} catch {
|
|
1418
2310
|
continue;
|
|
1419
2311
|
}
|
|
@@ -1427,35 +2319,34 @@ function collectSourceFiles(srcDir, maxFiles = MAX_FILES, maxBytes = MAX_BYTES)
|
|
|
1427
2319
|
}
|
|
1428
2320
|
}
|
|
1429
2321
|
walk(resolvedBase);
|
|
1430
|
-
return results;
|
|
2322
|
+
return { files: results, totalFound };
|
|
1431
2323
|
}
|
|
1432
2324
|
function resolveOutputPath(opts) {
|
|
1433
|
-
if (opts.out) return
|
|
1434
|
-
const defaultPath =
|
|
2325
|
+
if (opts.out) return resolve6(opts.out);
|
|
2326
|
+
const defaultPath = join5(resolve6(opts.srcDir), "agent.yaml");
|
|
1435
2327
|
if (opts.update || opts.dryRun) return defaultPath;
|
|
1436
|
-
if (
|
|
2328
|
+
if (existsSync5(defaultPath)) return join5(resolve6(opts.srcDir), "agent.yaml.new");
|
|
1437
2329
|
return defaultPath;
|
|
1438
2330
|
}
|
|
1439
2331
|
function collectAndValidateSourceFiles(srcDir) {
|
|
1440
|
-
const files =
|
|
2332
|
+
const { files, totalFound } = collectSourceFilesWithCount(srcDir);
|
|
1441
2333
|
if (files.length === 0) {
|
|
1442
2334
|
console.warn(`No source files found in ${srcDir}`);
|
|
1443
2335
|
}
|
|
1444
|
-
|
|
1445
|
-
if (rawCount > MAX_FILES) {
|
|
2336
|
+
if (totalFound > MAX_FILES) {
|
|
1446
2337
|
console.warn(
|
|
1447
|
-
`Found ${
|
|
2338
|
+
`Found ${totalFound} source files \u2014 truncating to ${MAX_FILES} files cap. Use a narrower --dir path to scan specific modules.`
|
|
1448
2339
|
);
|
|
1449
2340
|
}
|
|
1450
2341
|
return files;
|
|
1451
2342
|
}
|
|
1452
2343
|
function parseDetection(rawResult) {
|
|
1453
2344
|
if (!rawResult || typeof rawResult !== "object" || !("files" in rawResult) || typeof rawResult.files !== "object" || rawResult.files === null) {
|
|
1454
|
-
throw new Error('
|
|
2345
|
+
throw new Error('Provider returned an unexpected response format (missing "files" object).');
|
|
1455
2346
|
}
|
|
1456
2347
|
const detectionJson = rawResult.files["detection.json"];
|
|
1457
2348
|
if (!detectionJson) {
|
|
1458
|
-
throw new Error("
|
|
2349
|
+
throw new Error("Provider did not return detection.json in the output.");
|
|
1459
2350
|
}
|
|
1460
2351
|
let detection;
|
|
1461
2352
|
try {
|
|
@@ -1474,37 +2365,67 @@ function parseDetection(rawResult) {
|
|
|
1474
2365
|
function validateManifestYaml(yamlStr) {
|
|
1475
2366
|
let parsed;
|
|
1476
2367
|
try {
|
|
1477
|
-
parsed =
|
|
2368
|
+
parsed = jsYaml2.load(yamlStr);
|
|
1478
2369
|
} catch (e) {
|
|
1479
2370
|
return { valid: false, errors: `YAML parse error: ${e.message}`, errorCount: 1 };
|
|
1480
2371
|
}
|
|
1481
2372
|
const result = ManifestSchema.safeParse(parsed);
|
|
1482
2373
|
if (result.success) return { valid: true };
|
|
1483
|
-
const errors = result.error.
|
|
1484
|
-
return { valid: false, errors, errorCount: result.error.
|
|
2374
|
+
const errors = result.error.issues.map((e) => ` - ${e.path.join(".") || "(root)"}: ${e.message}`).join("\n");
|
|
2375
|
+
return { valid: false, errors, errorCount: result.error.issues.length };
|
|
2376
|
+
}
|
|
2377
|
+
function runClaudeCodeProfile(srcDir) {
|
|
2378
|
+
const manifest = buildManifestFromClaudeConfig(srcDir);
|
|
2379
|
+
let agentYaml = jsYaml2.dump(manifest, { lineWidth: 120 });
|
|
2380
|
+
const validation = validateManifestYaml(agentYaml);
|
|
2381
|
+
if (!validation.valid) {
|
|
2382
|
+
agentYaml = `# agentspec validate errors \u2014 fix before committing:
|
|
2383
|
+
` + validation.errors.split("\n").map((l) => `# ${l}`).join("\n") + "\n\n" + agentYaml;
|
|
2384
|
+
}
|
|
2385
|
+
return agentYaml;
|
|
1485
2386
|
}
|
|
1486
2387
|
function registerScanCommand(program2) {
|
|
1487
|
-
program2.command("scan").description("Scan source code and generate an agent.yaml manifest (
|
|
1488
|
-
if (
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
)
|
|
2388
|
+
program2.command("scan").description("Scan source code and generate an agent.yaml manifest (LLM-powered)").requiredOption("-d, --dir <src>", "Source directory to scan").option("--out <path>", "Explicit output path").option("--update", "Overwrite existing agent.yaml in place").option("--dry-run", "Print generated YAML to stdout without writing").option("--provider <name>", "Override codegen provider: claude-sub, anthropic-api, openai-compatible").option("--profile <name>", "Config profile for deterministic (no-LLM) scanning: claude-code").action(async (opts) => {
|
|
2389
|
+
if (opts.profile === "claude-code") {
|
|
2390
|
+
const srcDir2 = resolve6(opts.dir);
|
|
2391
|
+
const agentYaml2 = runClaudeCodeProfile(srcDir2);
|
|
2392
|
+
if (opts.dryRun) {
|
|
2393
|
+
console.log(agentYaml2);
|
|
2394
|
+
return;
|
|
2395
|
+
}
|
|
2396
|
+
const outPath2 = resolveOutputPath({ srcDir: srcDir2, out: opts.out, update: opts.update, dryRun: opts.dryRun });
|
|
2397
|
+
writeFileSync5(outPath2, agentYaml2, "utf-8");
|
|
2398
|
+
console.log(`\u2713 Written: ${outPath2}`);
|
|
2399
|
+
return;
|
|
2400
|
+
}
|
|
2401
|
+
if (opts.profile) {
|
|
2402
|
+
console.error(`Unknown --profile "${opts.profile}". Supported: claude-code`);
|
|
1492
2403
|
process.exit(1);
|
|
1493
2404
|
}
|
|
1494
|
-
const
|
|
2405
|
+
const s = spinner2();
|
|
2406
|
+
s.start("Checking provider\u2026");
|
|
2407
|
+
let provider;
|
|
2408
|
+
try {
|
|
2409
|
+
provider = resolveProvider2(opts.provider);
|
|
2410
|
+
} catch (err) {
|
|
2411
|
+
s.stop("Provider unavailable");
|
|
2412
|
+
console.error(`Codegen provider unavailable: ${err.message}`);
|
|
2413
|
+
process.exit(1);
|
|
2414
|
+
}
|
|
2415
|
+
const srcDir = resolve6(opts.dir);
|
|
1495
2416
|
const sourceFiles = collectAndValidateSourceFiles(srcDir);
|
|
1496
|
-
|
|
1497
|
-
s.start("Analysing source code\u2026");
|
|
2417
|
+
s.message(`Analysing source code with ${provider.name}\u2026`);
|
|
1498
2418
|
let rawResult;
|
|
1499
2419
|
try {
|
|
1500
|
-
rawResult = await
|
|
2420
|
+
rawResult = await generateCode2(
|
|
1501
2421
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1502
2422
|
{},
|
|
1503
2423
|
// empty manifest — the scan skill detects from source
|
|
1504
2424
|
{
|
|
1505
2425
|
framework: "scan",
|
|
1506
2426
|
contextFiles: sourceFiles.map((f) => f.path),
|
|
1507
|
-
manifestDir: srcDir
|
|
2427
|
+
manifestDir: srcDir,
|
|
2428
|
+
provider
|
|
1508
2429
|
}
|
|
1509
2430
|
);
|
|
1510
2431
|
} catch (err) {
|
|
@@ -1517,7 +2438,7 @@ function registerScanCommand(program2) {
|
|
|
1517
2438
|
s.message("Building manifest\u2026");
|
|
1518
2439
|
const detection = parseDetection(rawResult);
|
|
1519
2440
|
const manifest = buildManifestFromDetection(detection);
|
|
1520
|
-
agentYaml =
|
|
2441
|
+
agentYaml = jsYaml2.dump(manifest, { lineWidth: 120 });
|
|
1521
2442
|
} catch (err) {
|
|
1522
2443
|
s.stop("Failed");
|
|
1523
2444
|
console.error(`Build failed: ${err.message}`);
|
|
@@ -1529,7 +2450,7 @@ function registerScanCommand(program2) {
|
|
|
1529
2450
|
`Fixing ${validation.errorCount} schema error(s) \u2014 attempt ${attempt}/${MAX_REPAIR_ITERATIONS}\u2026`
|
|
1530
2451
|
);
|
|
1531
2452
|
try {
|
|
1532
|
-
agentYaml = await repairYaml(agentYaml, validation.errors);
|
|
2453
|
+
agentYaml = await repairYaml(provider, agentYaml, validation.errors);
|
|
1533
2454
|
validation = validateManifestYaml(agentYaml);
|
|
1534
2455
|
} catch (err) {
|
|
1535
2456
|
s.stop("Failed");
|
|
@@ -1558,76 +2479,24 @@ function registerScanCommand(program2) {
|
|
|
1558
2479
|
console.log(`\u2713 Written: ${outPath}`);
|
|
1559
2480
|
});
|
|
1560
2481
|
}
|
|
1561
|
-
function countSourceFiles(srcDir) {
|
|
1562
|
-
let resolvedBase;
|
|
1563
|
-
try {
|
|
1564
|
-
resolvedBase = realpathSync(resolve4(srcDir));
|
|
1565
|
-
} catch {
|
|
1566
|
-
resolvedBase = resolve4(srcDir);
|
|
1567
|
-
}
|
|
1568
|
-
let count = 0;
|
|
1569
|
-
function walk(dir) {
|
|
1570
|
-
let entries;
|
|
1571
|
-
try {
|
|
1572
|
-
entries = readdirSync(dir);
|
|
1573
|
-
} catch {
|
|
1574
|
-
return;
|
|
1575
|
-
}
|
|
1576
|
-
for (const entry of entries) {
|
|
1577
|
-
if (entry.startsWith(".") || SKIP_DIRS.has(entry)) continue;
|
|
1578
|
-
const fullPath = join3(dir, entry);
|
|
1579
|
-
let stat;
|
|
1580
|
-
try {
|
|
1581
|
-
stat = lstatSync(fullPath);
|
|
1582
|
-
} catch {
|
|
1583
|
-
continue;
|
|
1584
|
-
}
|
|
1585
|
-
if (stat.isSymbolicLink()) continue;
|
|
1586
|
-
if (stat.isDirectory()) {
|
|
1587
|
-
let resolvedDir;
|
|
1588
|
-
try {
|
|
1589
|
-
resolvedDir = realpathSync(fullPath);
|
|
1590
|
-
} catch {
|
|
1591
|
-
continue;
|
|
1592
|
-
}
|
|
1593
|
-
if (!resolvedDir.startsWith(resolvedBase + "/") && resolvedDir !== resolvedBase) continue;
|
|
1594
|
-
walk(fullPath);
|
|
1595
|
-
} else if (stat.isFile() && SOURCE_EXTENSIONS.has(extname(entry))) {
|
|
1596
|
-
count++;
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
walk(resolvedBase);
|
|
1601
|
-
return count;
|
|
1602
|
-
}
|
|
1603
2482
|
|
|
1604
2483
|
// src/commands/diff.ts
|
|
1605
|
-
import { readFileSync as
|
|
1606
|
-
import
|
|
2484
|
+
import { readFileSync as readFileSync6 } from "fs";
|
|
2485
|
+
import * as yaml6 from "js-yaml";
|
|
1607
2486
|
|
|
1608
2487
|
// src/utils/diff-score.ts
|
|
1609
2488
|
import { scoreToGrade } from "@agentspec/sdk";
|
|
1610
2489
|
var DIFF_SCORE_TABLE = {
|
|
1611
2490
|
// ── Guardrails ─────────────────────────────────────────────────────────────
|
|
1612
|
-
"spec.guardrails.
|
|
2491
|
+
"spec.guardrails.input": {
|
|
1613
2492
|
severity: "HIGH",
|
|
1614
2493
|
scoreImpact: -15,
|
|
1615
|
-
description: "
|
|
1616
|
-
},
|
|
1617
|
-
"spec.guardrails.rate_limit": {
|
|
1618
|
-
severity: "HIGH",
|
|
1619
|
-
scoreImpact: -10,
|
|
1620
|
-
description: "Rate limiting removed \u2014 DoS risk increased"
|
|
2494
|
+
description: "Input guardrail removed or changed \u2014 re-check input filtering"
|
|
1621
2495
|
},
|
|
1622
|
-
"spec.guardrails.
|
|
2496
|
+
"spec.guardrails.output": {
|
|
1623
2497
|
severity: "HIGH",
|
|
1624
2498
|
scoreImpact: -10,
|
|
1625
|
-
description: "Output
|
|
1626
|
-
},
|
|
1627
|
-
"spec.guardrails.pii_scrubber": {
|
|
1628
|
-
severity: "HIGH",
|
|
1629
|
-
scoreImpact: -12,
|
|
1630
|
-
description: "PII scrubbing removed \u2014 sensitive data may leak in outputs"
|
|
2499
|
+
description: "Output guardrail removed or changed \u2014 re-check output validation"
|
|
1631
2500
|
},
|
|
1632
2501
|
// ── Model ──────────────────────────────────────────────────────────────────
|
|
1633
2502
|
"spec.model.apiKey": {
|
|
@@ -1635,7 +2504,7 @@ var DIFF_SCORE_TABLE = {
|
|
|
1635
2504
|
scoreImpact: -12,
|
|
1636
2505
|
description: "Model API key reference removed \u2014 agent will fail to authenticate"
|
|
1637
2506
|
},
|
|
1638
|
-
"spec.model.
|
|
2507
|
+
"spec.model.id": {
|
|
1639
2508
|
severity: "MEDIUM",
|
|
1640
2509
|
scoreImpact: -5,
|
|
1641
2510
|
description: "Model changed \u2014 re-evaluate compliance for new model"
|
|
@@ -1651,26 +2520,26 @@ var DIFF_SCORE_TABLE = {
|
|
|
1651
2520
|
description: "Fallback model removed \u2014 no resilience on primary model failure"
|
|
1652
2521
|
},
|
|
1653
2522
|
// ── Evaluation ─────────────────────────────────────────────────────────────
|
|
1654
|
-
"spec.
|
|
1655
|
-
severity: "MEDIUM",
|
|
1656
|
-
scoreImpact: -8,
|
|
1657
|
-
description: "Eval hooks removed \u2014 regression detection disabled"
|
|
1658
|
-
},
|
|
1659
|
-
"spec.eval.datasets": {
|
|
2523
|
+
"spec.evaluation.datasets": {
|
|
1660
2524
|
severity: "MEDIUM",
|
|
1661
2525
|
scoreImpact: -5,
|
|
1662
|
-
description: "
|
|
2526
|
+
description: "Evaluation datasets removed or changed \u2014 regression coverage reduced"
|
|
1663
2527
|
},
|
|
1664
2528
|
// ── Memory ─────────────────────────────────────────────────────────────────
|
|
1665
|
-
"spec.memory.
|
|
2529
|
+
"spec.memory.hygiene.piiScrubFields": {
|
|
1666
2530
|
severity: "HIGH",
|
|
1667
2531
|
scoreImpact: -10,
|
|
1668
|
-
description: "Memory PII scrubbing removed \u2014 stored data may contain PII"
|
|
2532
|
+
description: "Memory PII scrubbing removed or changed \u2014 stored data may contain PII"
|
|
1669
2533
|
},
|
|
1670
|
-
"spec.memory.
|
|
2534
|
+
"spec.memory.shortTerm.ttlSeconds": {
|
|
1671
2535
|
severity: "LOW",
|
|
1672
2536
|
scoreImpact: -3,
|
|
1673
|
-
description: "
|
|
2537
|
+
description: "Short-term memory TTL removed or changed \u2014 data may be retained indefinitely"
|
|
2538
|
+
},
|
|
2539
|
+
"spec.memory.longTerm.ttlDays": {
|
|
2540
|
+
severity: "LOW",
|
|
2541
|
+
scoreImpact: -3,
|
|
2542
|
+
description: "Long-term memory TTL removed or changed \u2014 data may be retained indefinitely"
|
|
1674
2543
|
},
|
|
1675
2544
|
// ── Tools (additions) ──────────────────────────────────────────────────────
|
|
1676
2545
|
"spec.tools[+]": {
|
|
@@ -1704,18 +2573,14 @@ function flattenObject(obj, prefix = "") {
|
|
|
1704
2573
|
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
1705
2574
|
Object.assign(result, flattenObject(value, fullKey));
|
|
1706
2575
|
} else if (Array.isArray(value)) {
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
result[itemKey] = item;
|
|
1716
|
-
}
|
|
1717
|
-
});
|
|
1718
|
-
}
|
|
2576
|
+
value.forEach((item, idx) => {
|
|
2577
|
+
const itemKey = `${fullKey}.${idx}`;
|
|
2578
|
+
if (item !== null && typeof item === "object") {
|
|
2579
|
+
Object.assign(result, flattenObject(item, itemKey));
|
|
2580
|
+
} else {
|
|
2581
|
+
result[itemKey] = item;
|
|
2582
|
+
}
|
|
2583
|
+
});
|
|
1719
2584
|
} else {
|
|
1720
2585
|
result[fullKey] = value;
|
|
1721
2586
|
}
|
|
@@ -1723,15 +2588,15 @@ function flattenObject(obj, prefix = "") {
|
|
|
1723
2588
|
return result;
|
|
1724
2589
|
}
|
|
1725
2590
|
function lookupScore(property, changeType) {
|
|
2591
|
+
if (changeType === "added") {
|
|
2592
|
+
return { severity: "LOW", scoreImpact: 0, description: `New field added: ${property}` };
|
|
2593
|
+
}
|
|
1726
2594
|
if (DIFF_SCORE_TABLE[property]) return DIFF_SCORE_TABLE[property];
|
|
1727
2595
|
const parts = property.split(".");
|
|
1728
2596
|
for (let i = parts.length - 1; i > 0; i--) {
|
|
1729
2597
|
const candidate = parts.slice(0, i).join(".");
|
|
1730
2598
|
if (DIFF_SCORE_TABLE[candidate]) return DIFF_SCORE_TABLE[candidate];
|
|
1731
2599
|
}
|
|
1732
|
-
if (changeType === "added") {
|
|
1733
|
-
return { severity: "LOW", scoreImpact: 0, description: `New field added: ${property}` };
|
|
1734
|
-
}
|
|
1735
2600
|
return {
|
|
1736
2601
|
severity: "LOW",
|
|
1737
2602
|
scoreImpact: -2,
|
|
@@ -1838,8 +2703,8 @@ function registerDiffCommand(program2) {
|
|
|
1838
2703
|
let fromRaw;
|
|
1839
2704
|
let toRaw;
|
|
1840
2705
|
try {
|
|
1841
|
-
fromRaw =
|
|
1842
|
-
toRaw =
|
|
2706
|
+
fromRaw = yaml6.load(readFileSync6(fromPath, "utf-8"), { schema: yaml6.JSON_SCHEMA });
|
|
2707
|
+
toRaw = yaml6.load(readFileSync6(toPath, "utf-8"), { schema: yaml6.JSON_SCHEMA });
|
|
1843
2708
|
} catch (err) {
|
|
1844
2709
|
console.error(`Error loading manifests: ${err.message}`);
|
|
1845
2710
|
process.exit(1);
|
|
@@ -1870,9 +2735,9 @@ function registerDiffCommand(program2) {
|
|
|
1870
2735
|
}
|
|
1871
2736
|
|
|
1872
2737
|
// src/commands/generate-policy.ts
|
|
1873
|
-
import { mkdirSync as
|
|
1874
|
-
import { join as
|
|
1875
|
-
import { loadManifest as
|
|
2738
|
+
import { mkdirSync as mkdirSync3, writeFileSync as writeFileSync6 } from "fs";
|
|
2739
|
+
import { join as join6, resolve as resolve7 } from "path";
|
|
2740
|
+
import { loadManifest as loadManifest7 } from "@agentspec/sdk";
|
|
1876
2741
|
function sanitizePackageName(name) {
|
|
1877
2742
|
return name.replace(/-/g, "_");
|
|
1878
2743
|
}
|
|
@@ -1885,8 +2750,8 @@ function generateRegoPolicy(manifest) {
|
|
|
1885
2750
|
lines.push(`# Agent: ${agentName}`);
|
|
1886
2751
|
lines.push(`# Generated: ${(/* @__PURE__ */ new Date()).toISOString()}`);
|
|
1887
2752
|
lines.push(`#`);
|
|
1888
|
-
lines.push(`#
|
|
1889
|
-
lines.push(`#
|
|
2753
|
+
lines.push(`# The sidecar's /gap endpoint queries OPA with an input built from the`);
|
|
2754
|
+
lines.push(`# manifest and its probes.`);
|
|
1890
2755
|
lines.push(`# Only deny rules relevant to this agent's manifest declarations are included.`);
|
|
1891
2756
|
lines.push(`#`);
|
|
1892
2757
|
lines.push(`# OPA query: POST /v1/data/agentspec/agent/${packageName}/deny`);
|
|
@@ -1905,7 +2770,7 @@ function generateRegoPolicy(manifest) {
|
|
|
1905
2770
|
if (inputGuardrails.length > 0) {
|
|
1906
2771
|
lines.push(`# \u2500\u2500 Input guardrail enforcement \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`);
|
|
1907
2772
|
lines.push(`# Each declared input guardrail MUST appear in input.guardrails_invoked.`);
|
|
1908
|
-
lines.push(`# The
|
|
2773
|
+
lines.push(`# The sidecar does not populate that list yet: it sends it empty.`);
|
|
1909
2774
|
lines.push(``);
|
|
1910
2775
|
for (const g of inputGuardrails) {
|
|
1911
2776
|
const ruleId = g.type.replace(/-/g, "_") + "_not_invoked";
|
|
@@ -1936,16 +2801,6 @@ function generateRegoPolicy(manifest) {
|
|
|
1936
2801
|
}
|
|
1937
2802
|
}
|
|
1938
2803
|
}
|
|
1939
|
-
if (spec.model.costControls?.maxMonthlyUSD !== void 0) {
|
|
1940
|
-
lines.push(`# \u2500\u2500 Cost control enforcement \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`);
|
|
1941
|
-
lines.push(`# Monthly cost projected from daily spend \xD7 30.`);
|
|
1942
|
-
lines.push(``);
|
|
1943
|
-
lines.push(`deny contains "monthly_cost_limit_exceeded" if {`);
|
|
1944
|
-
lines.push(` input.request_type == "llm_call"`);
|
|
1945
|
-
lines.push(` input.cost_today_usd * 30 > data.maxMonthlyUSD`);
|
|
1946
|
-
lines.push(`}`);
|
|
1947
|
-
lines.push(``);
|
|
1948
|
-
}
|
|
1949
2804
|
const ttlSeconds = spec.memory?.shortTerm?.ttlSeconds;
|
|
1950
2805
|
if (ttlSeconds !== void 0) {
|
|
1951
2806
|
lines.push(`# \u2500\u2500 Memory TTL enforcement \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`);
|
|
@@ -1958,16 +2813,19 @@ function generateRegoPolicy(manifest) {
|
|
|
1958
2813
|
}
|
|
1959
2814
|
const destructiveTools = (spec.tools ?? []).filter((t) => t.annotations?.destructiveHint === true).map((t) => t.name);
|
|
1960
2815
|
if (destructiveTools.length > 0) {
|
|
1961
|
-
lines.push(`# \u2500\u2500 Destructive
|
|
1962
|
-
lines.push(`# Tools annotated destructiveHint=true
|
|
2816
|
+
lines.push(`# \u2500\u2500 Destructive tools \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`);
|
|
2817
|
+
lines.push(`# Tools annotated destructiveHint=true are always denied here; only the gateway approval token (Phase 5) lifts that.`);
|
|
1963
2818
|
lines.push(``);
|
|
1964
|
-
lines.push(`deny contains "
|
|
2819
|
+
lines.push(`deny contains "destructive_tool_requires_gateway_approval_token" if {`);
|
|
1965
2820
|
lines.push(` some tool in input.tools_called`);
|
|
1966
2821
|
lines.push(` tool in data.destructiveTools`);
|
|
1967
|
-
lines.push(` not input.user_confirmed`);
|
|
1968
2822
|
lines.push(`}`);
|
|
1969
2823
|
lines.push(``);
|
|
1970
2824
|
}
|
|
2825
|
+
if (!lines.some((l) => l.startsWith("deny contains"))) {
|
|
2826
|
+
lines.push(`deny := set()`);
|
|
2827
|
+
lines.push(``);
|
|
2828
|
+
}
|
|
1971
2829
|
return lines.join("\n");
|
|
1972
2830
|
}
|
|
1973
2831
|
function generateDataJson(manifest) {
|
|
@@ -1990,9 +2848,6 @@ function generateDataJson(manifest) {
|
|
|
1990
2848
|
if (spec.memory?.shortTerm?.ttlSeconds !== void 0) {
|
|
1991
2849
|
data.shortTermTtlSeconds = spec.memory.shortTerm.ttlSeconds;
|
|
1992
2850
|
}
|
|
1993
|
-
if (spec.model.costControls?.maxMonthlyUSD !== void 0) {
|
|
1994
|
-
data.maxMonthlyUSD = spec.model.costControls.maxMonthlyUSD;
|
|
1995
|
-
}
|
|
1996
2851
|
if (spec.model.costControls?.alertAtUSD !== void 0) {
|
|
1997
2852
|
data.alertAtUSD = spec.model.costControls.alertAtUSD;
|
|
1998
2853
|
}
|
|
@@ -2013,20 +2868,20 @@ function registerGeneratePolicyCommand(program2) {
|
|
|
2013
2868
|
async (file, opts) => {
|
|
2014
2869
|
let manifest;
|
|
2015
2870
|
try {
|
|
2016
|
-
const result2 =
|
|
2871
|
+
const result2 = loadManifest7(file, { resolve: false });
|
|
2017
2872
|
manifest = result2.manifest;
|
|
2018
2873
|
} catch (err) {
|
|
2019
2874
|
printError(`Cannot load manifest: ${String(err)}`);
|
|
2020
2875
|
process.exit(1);
|
|
2021
2876
|
}
|
|
2022
2877
|
const agentName = manifest.metadata.name;
|
|
2023
|
-
const outputDir =
|
|
2878
|
+
const outputDir = resolve7(opts.out ?? join6("policies", agentName));
|
|
2024
2879
|
const rego = generateRegoPolicy(manifest);
|
|
2025
2880
|
const dataJson = generateDataJson(manifest);
|
|
2026
2881
|
const rulesGenerated = (rego.match(/^deny contains/gm) ?? []).length;
|
|
2027
|
-
|
|
2028
|
-
const regoPath =
|
|
2029
|
-
const dataPath =
|
|
2882
|
+
mkdirSync3(outputDir, { recursive: true });
|
|
2883
|
+
const regoPath = join6(outputDir, "policy.rego");
|
|
2884
|
+
const dataPath = join6(outputDir, "data.json");
|
|
2030
2885
|
writeFileSync6(regoPath, rego, "utf-8");
|
|
2031
2886
|
writeFileSync6(dataPath, JSON.stringify(dataJson, null, 2), "utf-8");
|
|
2032
2887
|
const result = {
|
|
@@ -2052,15 +2907,16 @@ function registerGeneratePolicyCommand(program2) {
|
|
|
2052
2907
|
}
|
|
2053
2908
|
|
|
2054
2909
|
// src/commands/evaluate.ts
|
|
2055
|
-
import { readFileSync as
|
|
2056
|
-
import { resolve as
|
|
2057
|
-
import
|
|
2058
|
-
import {
|
|
2910
|
+
import { readFileSync as readFileSync7, existsSync as existsSync6, realpathSync as realpathSync2 } from "fs";
|
|
2911
|
+
import { resolve as resolve8, dirname as dirname3, sep as sep2 } from "path";
|
|
2912
|
+
import chalk9 from "chalk";
|
|
2913
|
+
import { createHash } from "crypto";
|
|
2914
|
+
import { loadManifest as loadManifest8 } from "@agentspec/sdk";
|
|
2059
2915
|
function loadDataset(datasetPath) {
|
|
2060
|
-
if (!
|
|
2916
|
+
if (!existsSync6(datasetPath)) {
|
|
2061
2917
|
throw new Error(`Dataset file not found: ${datasetPath}`);
|
|
2062
2918
|
}
|
|
2063
|
-
const content =
|
|
2919
|
+
const content = readFileSync7(datasetPath, "utf-8");
|
|
2064
2920
|
const samples = [];
|
|
2065
2921
|
for (const line of content.split("\n")) {
|
|
2066
2922
|
const trimmed = line.trim();
|
|
@@ -2100,15 +2956,49 @@ async function sendToAgent(baseUrl, chatPath, input, timeoutMs) {
|
|
|
2100
2956
|
function scoreStringMatch(expected, actual) {
|
|
2101
2957
|
return actual.toLowerCase().includes(expected.toLowerCase());
|
|
2102
2958
|
}
|
|
2103
|
-
|
|
2959
|
+
var STRING_MATCH_JUDGE = "string-match";
|
|
2960
|
+
var STRING_MATCH_RUBRIC_HASH = createHash("sha256").update("evaluate: case-insensitive substring match").digest("hex");
|
|
2961
|
+
function wilsonInterval(passed, n) {
|
|
2962
|
+
if (n === 0) return [0, 1];
|
|
2963
|
+
const z = 1.96;
|
|
2964
|
+
const p2 = passed / n;
|
|
2965
|
+
const denom = 1 + z * z / n;
|
|
2966
|
+
const centre = (p2 + z * z / (2 * n)) / denom;
|
|
2967
|
+
const margin = z * Math.sqrt(p2 * (1 - p2) / n + z * z / (4 * n * n)) / denom;
|
|
2968
|
+
return [Math.max(0, centre - margin), Math.min(1, centre + margin)];
|
|
2969
|
+
}
|
|
2970
|
+
function gradeThresholds(minimums, passRate) {
|
|
2971
|
+
return Object.entries(minimums).map(
|
|
2972
|
+
([metric, minimum]) => metric === "pass_rate" && passRate !== null ? { metric, minimum, value: passRate, status: passRate >= minimum ? "pass" : "fail" } : { metric, minimum, status: "not_verifiable" }
|
|
2973
|
+
);
|
|
2974
|
+
}
|
|
2975
|
+
function toEvalResult(graded, passed, total, datasetName, window) {
|
|
2976
|
+
const [ciLow, ciHigh] = wilsonInterval(passed, total);
|
|
2977
|
+
return {
|
|
2978
|
+
metric: graded.metric,
|
|
2979
|
+
n: total,
|
|
2980
|
+
passRate: graded.value,
|
|
2981
|
+
ciLow,
|
|
2982
|
+
ciHigh,
|
|
2983
|
+
threshold: graded.minimum,
|
|
2984
|
+
window,
|
|
2985
|
+
judgeModel: STRING_MATCH_JUDGE,
|
|
2986
|
+
rubricHash: STRING_MATCH_RUBRIC_HASH,
|
|
2987
|
+
source: "independent",
|
|
2988
|
+
dataset: datasetName
|
|
2989
|
+
};
|
|
2990
|
+
}
|
|
2991
|
+
function buildReport(results, datasetName, agentUrl, manifest, window = { start: (/* @__PURE__ */ new Date()).toISOString(), end: (/* @__PURE__ */ new Date()).toISOString() }) {
|
|
2104
2992
|
const total = results.length;
|
|
2105
2993
|
const passed = results.filter((r) => r.pass).length;
|
|
2106
|
-
const passRate = total === 0 ?
|
|
2107
|
-
const
|
|
2994
|
+
const passRate = total === 0 ? null : passed / total;
|
|
2995
|
+
const minimums = manifest.spec.evaluation?.thresholds ?? {};
|
|
2996
|
+
const thresholds = gradeThresholds(minimums, passRate);
|
|
2997
|
+
const evalResults = thresholds.filter((t) => t.status !== "not_verifiable").map((t) => toEvalResult(t, passed, total, datasetName, window));
|
|
2108
2998
|
const ciGate = manifest.spec.evaluation?.ciGate ?? false;
|
|
2109
2999
|
let ciGateResult = "N/A";
|
|
2110
|
-
if (ciGate &&
|
|
2111
|
-
ciGateResult =
|
|
3000
|
+
if (ciGate && thresholds.length > 0) {
|
|
3001
|
+
ciGateResult = thresholds.every((t) => t.status === "pass") ? "PASS" : "FAIL";
|
|
2112
3002
|
}
|
|
2113
3003
|
return {
|
|
2114
3004
|
dataset: datasetName,
|
|
@@ -2117,7 +3007,9 @@ function buildReport(results, datasetName, agentUrl, manifest) {
|
|
|
2117
3007
|
metrics: {
|
|
2118
3008
|
pass_rate: passRate
|
|
2119
3009
|
},
|
|
2120
|
-
threshold,
|
|
3010
|
+
threshold: minimums["pass_rate"],
|
|
3011
|
+
thresholds,
|
|
3012
|
+
evalResults,
|
|
2121
3013
|
ciGateResult,
|
|
2122
3014
|
samples: results
|
|
2123
3015
|
};
|
|
@@ -2125,33 +3017,40 @@ function buildReport(results, datasetName, agentUrl, manifest) {
|
|
|
2125
3017
|
function printTable(report) {
|
|
2126
3018
|
printHeader(`AgentSpec Evaluate \u2014 ${report.dataset}`);
|
|
2127
3019
|
console.log(
|
|
2128
|
-
` Evaluating: ${
|
|
3020
|
+
` Evaluating: ${chalk9.bold(report.dataset)} ${chalk9.gray(String(report.totalSamples) + " samples")} agent: ${chalk9.cyan(report.agentUrl)}`
|
|
2129
3021
|
);
|
|
2130
|
-
console.log(
|
|
3022
|
+
console.log(chalk9.gray(" " + "\u2500".repeat(65)));
|
|
2131
3023
|
console.log();
|
|
2132
3024
|
for (const s of report.samples) {
|
|
2133
|
-
const icon = s.pass ?
|
|
2134
|
-
const expected =
|
|
3025
|
+
const icon = s.pass ? chalk9.green(" \u2713") : chalk9.red(" \u2717");
|
|
3026
|
+
const expected = chalk9.gray(`"${s.expected}"`);
|
|
2135
3027
|
if (s.error) {
|
|
2136
|
-
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${
|
|
3028
|
+
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${chalk9.gray(s.input.slice(0, 50))} \u2192 ${chalk9.red("error: " + s.error)}`);
|
|
2137
3029
|
} else if (s.pass) {
|
|
2138
|
-
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${
|
|
3030
|
+
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${chalk9.gray(s.input.slice(0, 50))} \u2192 found ${expected} ${chalk9.gray("[" + s.latencyMs + "ms]")}`);
|
|
2139
3031
|
} else {
|
|
2140
|
-
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${
|
|
3032
|
+
console.log(`${icon} ${String(s.index + 1).padStart(2)} ${chalk9.gray(s.input.slice(0, 50))} \u2192 expected ${expected} not found ${chalk9.gray("[" + s.latencyMs + "ms]")}`);
|
|
2141
3033
|
}
|
|
2142
3034
|
}
|
|
2143
3035
|
console.log();
|
|
2144
|
-
console.log(
|
|
3036
|
+
console.log(chalk9.bold(" Results"));
|
|
2145
3037
|
const { pass_rate } = report.metrics;
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
)
|
|
3038
|
+
if (pass_rate !== null) {
|
|
3039
|
+
const pct = Math.round(pass_rate * 100);
|
|
3040
|
+
const pctColor = scoreColor(pct);
|
|
3041
|
+
const threshStr = report.threshold !== void 0 ? `(threshold: ${Math.round(report.threshold * 100)}%)` : "(no threshold declared)";
|
|
3042
|
+
console.log(
|
|
3043
|
+
` pass_rate ${pctColor(pct + "%")} ${chalk9.gray(threshStr)} ${formatCiGate(report.ciGateResult)}`
|
|
3044
|
+
);
|
|
3045
|
+
}
|
|
3046
|
+
for (const t of report.thresholds.filter((t2) => t2.status === "not_verifiable")) {
|
|
3047
|
+
console.log(
|
|
3048
|
+
` ${t.metric.padEnd(13)} ${chalk9.yellow("not_verifiable")} ${chalk9.gray(`(minimum ${Math.round(t.minimum * 100)}%; evaluate cannot compute it)`)}`
|
|
3049
|
+
);
|
|
3050
|
+
}
|
|
2152
3051
|
console.log();
|
|
2153
3052
|
console.log(` ciGate: ${formatCiGate(report.ciGateResult)}`);
|
|
2154
|
-
console.log(` Exit code: ${report.ciGateResult === "FAIL" ?
|
|
3053
|
+
console.log(` Exit code: ${report.ciGateResult === "FAIL" ? chalk9.red("1") : chalk9.green("0")}`);
|
|
2155
3054
|
console.log();
|
|
2156
3055
|
}
|
|
2157
3056
|
function printJSON(report) {
|
|
@@ -2197,13 +3096,13 @@ function registerEvaluateCommand(program2) {
|
|
|
2197
3096
|
async (file, opts) => {
|
|
2198
3097
|
let parsed;
|
|
2199
3098
|
try {
|
|
2200
|
-
parsed =
|
|
3099
|
+
parsed = loadManifest8(file, { resolve: false });
|
|
2201
3100
|
} catch (err) {
|
|
2202
3101
|
printError(`Cannot load manifest: ${String(err)}`);
|
|
2203
3102
|
process.exit(1);
|
|
2204
3103
|
}
|
|
2205
3104
|
const { manifest } = parsed;
|
|
2206
|
-
const manifestDir =
|
|
3105
|
+
const manifestDir = dirname3(resolve8(file));
|
|
2207
3106
|
const timeoutMs = parseInt(opts.timeout, 10) || 1e4;
|
|
2208
3107
|
const datasets = manifest.spec.evaluation?.datasets ?? [];
|
|
2209
3108
|
const datasetEntry = datasets.find((d) => d.name === opts.dataset);
|
|
@@ -2215,10 +3114,37 @@ function registerEvaluateCommand(program2) {
|
|
|
2215
3114
|
}
|
|
2216
3115
|
const rawPath = datasetEntry.path;
|
|
2217
3116
|
const relPath = rawPath.startsWith("$file:") ? rawPath.slice(6) : rawPath;
|
|
2218
|
-
const absPath =
|
|
3117
|
+
const absPath = resolve8(manifestDir, relPath);
|
|
3118
|
+
const safeBase = manifestDir.endsWith(sep2) ? manifestDir : manifestDir + sep2;
|
|
3119
|
+
if (absPath !== manifestDir && !absPath.startsWith(safeBase)) {
|
|
3120
|
+
printError(
|
|
3121
|
+
`Dataset path "${relPath}" resolves outside the manifest directory. Only paths within the same directory tree are allowed.`
|
|
3122
|
+
);
|
|
3123
|
+
process.exit(1);
|
|
3124
|
+
}
|
|
3125
|
+
let realAbsPath;
|
|
3126
|
+
try {
|
|
3127
|
+
realAbsPath = realpathSync2(absPath);
|
|
3128
|
+
} catch {
|
|
3129
|
+
printError(`Dataset path "${relPath}" could not be resolved: file may not exist.`);
|
|
3130
|
+
process.exit(1);
|
|
3131
|
+
}
|
|
3132
|
+
let realBase;
|
|
3133
|
+
try {
|
|
3134
|
+
realBase = realpathSync2(manifestDir);
|
|
3135
|
+
} catch {
|
|
3136
|
+
realBase = manifestDir;
|
|
3137
|
+
}
|
|
3138
|
+
const safeRealBase = realBase.endsWith(sep2) ? realBase : realBase + sep2;
|
|
3139
|
+
if (realAbsPath !== realBase && !realAbsPath.startsWith(safeRealBase)) {
|
|
3140
|
+
printError(
|
|
3141
|
+
`Dataset path "${relPath}" resolves via symlink outside the manifest directory. Only paths within the same directory tree are allowed.`
|
|
3142
|
+
);
|
|
3143
|
+
process.exit(1);
|
|
3144
|
+
}
|
|
2219
3145
|
let samples;
|
|
2220
3146
|
try {
|
|
2221
|
-
samples = loadDataset(
|
|
3147
|
+
samples = loadDataset(realAbsPath);
|
|
2222
3148
|
} catch (err) {
|
|
2223
3149
|
printError(`Cannot load dataset: ${String(err)}`);
|
|
2224
3150
|
process.exit(1);
|
|
@@ -2229,13 +3155,18 @@ function registerEvaluateCommand(program2) {
|
|
|
2229
3155
|
if (opts.sampleSize) {
|
|
2230
3156
|
const n = parseInt(opts.sampleSize, 10);
|
|
2231
3157
|
if (n > 0 && n < samples.length) {
|
|
2232
|
-
const shuffled = [...samples]
|
|
3158
|
+
const shuffled = [...samples];
|
|
3159
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
3160
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
3161
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
3162
|
+
}
|
|
2233
3163
|
samples = shuffled.slice(0, n);
|
|
2234
3164
|
}
|
|
2235
3165
|
}
|
|
2236
3166
|
const chatPath = resolveChatEndpoint(manifest);
|
|
3167
|
+
const start = (/* @__PURE__ */ new Date()).toISOString();
|
|
2237
3168
|
const results = await runInference(samples, opts.url, chatPath, timeoutMs);
|
|
2238
|
-
const report = buildReport(results, opts.dataset, opts.url, manifest);
|
|
3169
|
+
const report = buildReport(results, opts.dataset, opts.url, manifest, { start, end: (/* @__PURE__ */ new Date()).toISOString() });
|
|
2239
3170
|
if (opts.json) {
|
|
2240
3171
|
printJSON(report);
|
|
2241
3172
|
} else {
|
|
@@ -2246,90 +3177,252 @@ function registerEvaluateCommand(program2) {
|
|
|
2246
3177
|
);
|
|
2247
3178
|
}
|
|
2248
3179
|
|
|
2249
|
-
// src/commands/
|
|
2250
|
-
import
|
|
2251
|
-
import {
|
|
2252
|
-
import
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
3180
|
+
// src/commands/provider-status.ts
|
|
3181
|
+
import chalk10 from "chalk";
|
|
3182
|
+
import { CONTRACT as CONTRACT7 } from "@agentspec/sdk";
|
|
3183
|
+
import {
|
|
3184
|
+
probeProviders
|
|
3185
|
+
} from "@agentspec/codegen";
|
|
3186
|
+
var tick = chalk10.green("\u2713");
|
|
3187
|
+
var cross = chalk10.red("\u2717");
|
|
3188
|
+
var dash = chalk10.dim("\u2013");
|
|
3189
|
+
var warn = chalk10.yellow("!");
|
|
3190
|
+
function printSection(title) {
|
|
3191
|
+
console.log();
|
|
3192
|
+
console.log(chalk10.bold.underline(title));
|
|
3193
|
+
}
|
|
3194
|
+
function row(label, value, icon) {
|
|
3195
|
+
const iconPart = icon ? `${icon} ` : " ";
|
|
3196
|
+
console.log(` ${iconPart}${chalk10.dim(label.padEnd(22))} ${value}`);
|
|
3197
|
+
}
|
|
3198
|
+
function getString(details, key) {
|
|
3199
|
+
const v = details[key];
|
|
3200
|
+
return typeof v === "string" ? v : null;
|
|
3201
|
+
}
|
|
3202
|
+
function getNumber(details, key) {
|
|
3203
|
+
const v = details[key];
|
|
3204
|
+
return typeof v === "number" ? v : null;
|
|
3205
|
+
}
|
|
3206
|
+
function providerLabel(name) {
|
|
3207
|
+
switch (name) {
|
|
3208
|
+
case "claude-subscription":
|
|
3209
|
+
return "Claude subscription";
|
|
3210
|
+
case "openai-compatible":
|
|
3211
|
+
return "OpenAI-compatible";
|
|
3212
|
+
case "anthropic-api":
|
|
3213
|
+
return "Anthropic API";
|
|
3214
|
+
default:
|
|
3215
|
+
return name;
|
|
3216
|
+
}
|
|
3217
|
+
}
|
|
3218
|
+
function renderClaudeSubscription(result) {
|
|
3219
|
+
printSection("Claude subscription");
|
|
3220
|
+
if (result.status === "not-configured") {
|
|
3221
|
+
row("Installed", chalk10.red("no"), cross);
|
|
3222
|
+
return;
|
|
3223
|
+
}
|
|
3224
|
+
const d = result.details;
|
|
3225
|
+
row("Installed", chalk10.green("yes"), tick);
|
|
3226
|
+
const version2 = getString(d, "version");
|
|
3227
|
+
if (version2) row("Version", chalk10.cyan(version2));
|
|
3228
|
+
if (result.status === "misconfigured") {
|
|
3229
|
+
row("Authenticated", chalk10.red("no, run: claude auth login"), cross);
|
|
3230
|
+
} else if (result.status === "ready") {
|
|
3231
|
+
row("Authenticated", chalk10.green("yes"), tick);
|
|
3232
|
+
}
|
|
3233
|
+
const email = getString(d, "accountEmail");
|
|
3234
|
+
if (email) row("Account", chalk10.cyan(email), tick);
|
|
3235
|
+
const plan = getString(d, "plan");
|
|
3236
|
+
if (plan) {
|
|
3237
|
+
const planColor = plan.toLowerCase().includes("max") || plan.toLowerCase().includes("pro") ? chalk10.green : chalk10.yellow;
|
|
3238
|
+
row("Plan", planColor(plan), tick);
|
|
3239
|
+
}
|
|
3240
|
+
const activeModel = getString(d, "activeModel");
|
|
3241
|
+
if (activeModel) row("Active model", chalk10.cyan(activeModel));
|
|
3242
|
+
const authStatusRaw = getString(d, "authStatusRaw");
|
|
3243
|
+
if (authStatusRaw && result.status === "misconfigured") {
|
|
3244
|
+
console.log();
|
|
3245
|
+
console.log(chalk10.dim(" Raw auth status output:"));
|
|
3246
|
+
for (const line of authStatusRaw.split("\n").slice(0, 8)) {
|
|
3247
|
+
console.log(chalk10.dim(` ${line}`));
|
|
2311
3248
|
}
|
|
2312
|
-
|
|
3249
|
+
}
|
|
3250
|
+
}
|
|
3251
|
+
function renderAnthropicApi(result) {
|
|
3252
|
+
printSection("Anthropic API");
|
|
3253
|
+
if (result.status === "not-configured") {
|
|
3254
|
+
row("ANTHROPIC_API_KEY", chalk10.red("not set"), cross);
|
|
3255
|
+
return;
|
|
3256
|
+
}
|
|
3257
|
+
const d = result.details;
|
|
3258
|
+
const keyPreview = getString(d, "keyPreview") ?? "";
|
|
3259
|
+
row("ANTHROPIC_API_KEY", chalk10.cyan(keyPreview), tick);
|
|
3260
|
+
if (result.status === "ready") {
|
|
3261
|
+
const httpStatus = getNumber(d, "httpStatus");
|
|
3262
|
+
row("Key status", chalk10.green(`valid (HTTP ${httpStatus ?? 200})`), tick);
|
|
3263
|
+
} else if (result.status === "unreachable") {
|
|
3264
|
+
row("Key status", chalk10.red(`rejected (${result.reason})`), cross);
|
|
3265
|
+
}
|
|
3266
|
+
const baseURL = getString(d, "baseURL");
|
|
3267
|
+
if (baseURL) {
|
|
3268
|
+
row(CONTRACT7.providerBaseUrlEnv.anthropic, chalk10.cyan(baseURL), tick);
|
|
3269
|
+
} else {
|
|
3270
|
+
row(CONTRACT7.providerBaseUrlEnv.anthropic, chalk10.dim("not set (using default)"), dash);
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
function renderOpenAICompatible(result) {
|
|
3274
|
+
printSection("OpenAI-compatible");
|
|
3275
|
+
if (result.status === "not-configured") {
|
|
3276
|
+
row("AGENTSPEC_LLM_API_KEY", chalk10.red("not set"), cross);
|
|
3277
|
+
return;
|
|
3278
|
+
}
|
|
3279
|
+
const d = result.details;
|
|
3280
|
+
const keyPreview = getString(d, "apiKeyPreview") ?? "";
|
|
3281
|
+
row("AGENTSPEC_LLM_API_KEY", chalk10.cyan(keyPreview), tick);
|
|
3282
|
+
const baseURL = getString(d, "baseURL");
|
|
3283
|
+
if (baseURL) {
|
|
3284
|
+
row("AGENTSPEC_LLM_BASE_URL", chalk10.cyan(baseURL), tick);
|
|
3285
|
+
} else {
|
|
3286
|
+
row("AGENTSPEC_LLM_BASE_URL", chalk10.dim("not set (using default)"), dash);
|
|
3287
|
+
}
|
|
3288
|
+
if (result.status === "misconfigured") {
|
|
3289
|
+
row("AGENTSPEC_LLM_MODEL", chalk10.red("not set (required)"), cross);
|
|
3290
|
+
return;
|
|
3291
|
+
}
|
|
3292
|
+
const model = getString(d, "model");
|
|
3293
|
+
if (model) row("AGENTSPEC_LLM_MODEL", chalk10.cyan(model));
|
|
3294
|
+
if (result.status === "ready") {
|
|
3295
|
+
const httpStatus = getNumber(d, "httpStatus");
|
|
3296
|
+
row("Endpoint", chalk10.green(`reachable (HTTP ${httpStatus ?? 200})`), tick);
|
|
3297
|
+
} else if (result.status === "unreachable") {
|
|
3298
|
+
row("Endpoint", chalk10.red(`rejected (${result.reason})`), cross);
|
|
3299
|
+
}
|
|
3300
|
+
}
|
|
3301
|
+
function renderProbeResult(result) {
|
|
3302
|
+
switch (result.provider) {
|
|
3303
|
+
case "claude-subscription":
|
|
3304
|
+
renderClaudeSubscription(result);
|
|
3305
|
+
return;
|
|
3306
|
+
case "openai-compatible":
|
|
3307
|
+
renderOpenAICompatible(result);
|
|
3308
|
+
return;
|
|
3309
|
+
case "anthropic-api":
|
|
3310
|
+
renderAnthropicApi(result);
|
|
3311
|
+
return;
|
|
3312
|
+
default:
|
|
3313
|
+
printSection(result.provider);
|
|
3314
|
+
row("Status", result.status);
|
|
3315
|
+
}
|
|
2313
3316
|
}
|
|
2314
|
-
function
|
|
2315
|
-
|
|
3317
|
+
function renderEnv(report) {
|
|
3318
|
+
const { env } = report;
|
|
3319
|
+
printSection("Environment & resolution");
|
|
3320
|
+
row(
|
|
3321
|
+
"Provider override",
|
|
3322
|
+
env.providerOverride ? chalk10.cyan(`AGENTSPEC_CODEGEN_PROVIDER=${env.providerOverride}`) : chalk10.dim("not set (auto-detect)"),
|
|
3323
|
+
env.providerOverride ? warn : dash
|
|
3324
|
+
);
|
|
3325
|
+
console.log();
|
|
3326
|
+
if (env.resolvedProvider) {
|
|
3327
|
+
console.log(` ${tick} ${chalk10.bold("Would use:")} ${chalk10.green(providerLabel(env.resolvedProvider))}`);
|
|
3328
|
+
} else {
|
|
3329
|
+
console.log(` ${cross} ${chalk10.bold("Would use:")} ${chalk10.red("nothing, no provider available")}`);
|
|
3330
|
+
if (env.resolveError) {
|
|
3331
|
+
console.log();
|
|
3332
|
+
console.log(chalk10.red(" Error:"));
|
|
3333
|
+
for (const line of env.resolveError.split("\n")) {
|
|
3334
|
+
console.log(` ${line}`);
|
|
3335
|
+
}
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
2316
3338
|
}
|
|
2317
|
-
function
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
3339
|
+
function renderSummary(report) {
|
|
3340
|
+
const { env, results } = report;
|
|
3341
|
+
console.log();
|
|
3342
|
+
console.log(chalk10.bold("\u2500".repeat(50)));
|
|
3343
|
+
if (!env.resolvedProvider) {
|
|
3344
|
+
console.log(`${cross} ${chalk10.bold.red("Not ready: no codegen provider available")}`);
|
|
3345
|
+
console.log();
|
|
3346
|
+
console.log(" Set up one of:");
|
|
3347
|
+
console.log(` ${chalk10.cyan("claude auth login")} ${chalk10.dim("(claude-subscription)")}`);
|
|
3348
|
+
console.log(` ${chalk10.cyan("export AGENTSPEC_LLM_API_KEY=... AGENTSPEC_LLM_MODEL=...")} ${chalk10.dim("(openai-compatible)")}`);
|
|
3349
|
+
console.log(` ${chalk10.cyan("export ANTHROPIC_API_KEY=sk-ant-...")} ${chalk10.dim("(anthropic-api)")}`);
|
|
3350
|
+
return;
|
|
3351
|
+
}
|
|
3352
|
+
const label = providerLabel(env.resolvedProvider);
|
|
3353
|
+
const resolvedResult = results.find((r) => r.provider === env.resolvedProvider);
|
|
3354
|
+
if (resolvedResult?.provider === "claude-subscription" && resolvedResult.status === "ready") {
|
|
3355
|
+
const d = resolvedResult.details;
|
|
3356
|
+
const plan = getString(d, "plan");
|
|
3357
|
+
const email = getString(d, "accountEmail");
|
|
3358
|
+
const planLabel = plan ? ` (${plan})` : "";
|
|
3359
|
+
const emailLabel = email ? ` \xB7 ${email}` : "";
|
|
3360
|
+
console.log(`${tick} ${chalk10.bold.green(`Ready: ${label}${planLabel}${emailLabel}`)}`);
|
|
3361
|
+
} else if (resolvedResult?.provider === "anthropic-api" && resolvedResult.status === "ready") {
|
|
3362
|
+
console.log(`${tick} ${chalk10.bold.green(`Ready: ${label} \xB7 key verified`)}`);
|
|
3363
|
+
} else if (resolvedResult?.provider === "openai-compatible" && resolvedResult.status === "ready") {
|
|
3364
|
+
const d = resolvedResult.details;
|
|
3365
|
+
const model = getString(d, "model");
|
|
3366
|
+
const modelHint = model ? ` \xB7 ${model}` : "";
|
|
3367
|
+
console.log(`${tick} ${chalk10.bold.green(`Ready: ${label}${modelHint}`)}`);
|
|
3368
|
+
} else {
|
|
3369
|
+
console.log(`${tick} ${chalk10.bold.green(`Ready: ${label}`)}`);
|
|
2321
3370
|
}
|
|
2322
|
-
|
|
3371
|
+
console.log(chalk10.dim(` agentspec generate and scan will use the ${env.resolvedProvider} provider`));
|
|
3372
|
+
}
|
|
3373
|
+
function registerProviderStatusCommand(program2) {
|
|
3374
|
+
program2.command("provider-status").description("Show codegen provider status: Claude subscription, OpenAI-compatible, Anthropic API, and active config").option("--json", "Output as JSON").action(async (opts) => {
|
|
3375
|
+
if (!opts.json) {
|
|
3376
|
+
printHeader("AgentSpec: Provider Status");
|
|
3377
|
+
}
|
|
3378
|
+
const report = await probeProviders();
|
|
3379
|
+
if (opts.json) {
|
|
3380
|
+
console.log(JSON.stringify(report, null, 2));
|
|
3381
|
+
process.exit(!report.env.resolvedProvider ? 1 : 0);
|
|
3382
|
+
return;
|
|
3383
|
+
}
|
|
3384
|
+
for (const result of report.results) {
|
|
3385
|
+
renderProbeResult(result);
|
|
3386
|
+
}
|
|
3387
|
+
renderEnv(report);
|
|
3388
|
+
renderSummary(report);
|
|
3389
|
+
console.log();
|
|
3390
|
+
process.exit(!report.env.resolvedProvider ? 1 : 0);
|
|
3391
|
+
});
|
|
3392
|
+
}
|
|
3393
|
+
|
|
3394
|
+
// src/commands/gateway-config.ts
|
|
3395
|
+
import { mkdirSync as mkdirSync4, writeFileSync as writeFileSync7 } from "fs";
|
|
3396
|
+
import { dirname as dirname4 } from "path";
|
|
3397
|
+
import { describeManifestError, loadManifest as loadManifest9, renderGatewayConfig } from "@agentspec/sdk";
|
|
3398
|
+
function registerGatewayConfigCommand(program2) {
|
|
3399
|
+
program2.command("gateway-config <file>").description("Render the agentgateway config from an agent.yaml manifest").option("--out <path>", "Write the config to this file instead of stdout").action((file, opts) => {
|
|
3400
|
+
try {
|
|
3401
|
+
const { manifest } = loadManifest9(file, { resolve: false });
|
|
3402
|
+
const rendered = renderGatewayConfig(manifest);
|
|
3403
|
+
if (!opts.out) {
|
|
3404
|
+
process.stdout.write(rendered);
|
|
3405
|
+
return;
|
|
3406
|
+
}
|
|
3407
|
+
mkdirSync4(dirname4(opts.out), { recursive: true });
|
|
3408
|
+
writeFileSync7(opts.out, rendered);
|
|
3409
|
+
} catch (err) {
|
|
3410
|
+
printError(`gateway-config failed for ${file}:
|
|
3411
|
+
${describeManifestError(err)}`);
|
|
3412
|
+
process.exit(1);
|
|
3413
|
+
}
|
|
3414
|
+
});
|
|
2323
3415
|
}
|
|
2324
3416
|
|
|
2325
3417
|
// src/cli.ts
|
|
2326
|
-
var _dir =
|
|
2327
|
-
var { version } = JSON.parse(
|
|
3418
|
+
var _dir = dirname5(fileURLToPath(import.meta.url));
|
|
3419
|
+
var { version } = JSON.parse(readFileSync8(join7(_dir, "../package.json"), "utf8"));
|
|
2328
3420
|
var program = new Command();
|
|
2329
3421
|
program.name("agentspec").description("Universal Agent Manifest System \u2014 validate, health-check, audit, and generate agents").version(version);
|
|
2330
3422
|
registerValidateCommand(program);
|
|
2331
3423
|
registerHealthCommand(program);
|
|
2332
3424
|
registerAuditCommand(program);
|
|
3425
|
+
registerRegisterCommand(program);
|
|
2333
3426
|
registerInitCommand(program);
|
|
2334
3427
|
registerGenerateCommand(program);
|
|
2335
3428
|
registerExportCommand(program);
|
|
@@ -2337,7 +3430,8 @@ registerMigrateCommand(program);
|
|
|
2337
3430
|
registerScanCommand(program);
|
|
2338
3431
|
registerDiffCommand(program);
|
|
2339
3432
|
registerGeneratePolicyCommand(program);
|
|
3433
|
+
registerGatewayConfigCommand(program);
|
|
2340
3434
|
registerEvaluateCommand(program);
|
|
2341
|
-
|
|
3435
|
+
registerProviderStatusCommand(program);
|
|
2342
3436
|
program.parse(process.argv);
|
|
2343
3437
|
//# sourceMappingURL=cli.js.map
|