agent-contracts-runtime 0.36.16 → 0.36.18
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/README.md +6 -65
- package/cli-contract.yaml +3 -3
- package/dist/adapters/claude-agent-sdk.d.ts +2 -0
- package/dist/adapters/claude-agent-sdk.js +21 -7
- package/dist/adapters/claude-agent-sdk.js.map +1 -1
- package/dist/adapters/mock.js +3 -37
- package/dist/adapters/mock.js.map +1 -1
- package/dist/agent-runtime.bundle.mjs +523 -725
- package/dist/agent-runtime.bundle.mjs.map +4 -4
- package/dist/{bound-resolver-I7WLYGC6.js → bound-resolver-RPJYAOCW.js} +4 -4
- package/dist/{chunk-S7MTKPBF.js → chunk-CFOEW6XJ.js} +4 -25
- package/dist/chunk-CFOEW6XJ.js.map +1 -0
- package/dist/chunk-KB4VDAAQ.js +740 -0
- package/dist/chunk-KB4VDAAQ.js.map +1 -0
- package/dist/chunk-L5OAGF7D.js +42 -0
- package/dist/chunk-L5OAGF7D.js.map +1 -0
- package/dist/{chunk-6JVGBBP3.js → chunk-UHHKLLA3.js} +375 -58
- package/dist/chunk-UHHKLLA3.js.map +1 -0
- package/dist/cli/index.js +40 -57
- package/dist/cli/index.js.map +1 -1
- package/dist/generator/index.js +2 -2
- package/dist/{guardrail-hooks-CFd3a8tQ.d.ts → guardrail-hooks-BiLI9u9-.d.ts} +12 -6
- package/dist/index.d.ts +13 -6
- package/dist/index.js +17 -21
- package/dist/{task-runner-CD8nnKpn.d.ts → task-runner-Ba4kLv7O.d.ts} +5 -4
- package/docs/cli-reference.md +1 -1
- package/docs/sdk-options-bridge-spec.md +4 -3
- package/dsl/agents/llm-feature-implementer.yaml +2 -4
- package/dsl/tasks.yaml +1 -5
- package/package.json +5 -20
- package/templates/implement-reference-templates.md +3 -17
- package/templates/implement-reference.md +3 -3
- package/dist/adapters/adk-sdk.d.ts +0 -129
- package/dist/adapters/adk-sdk.js +0 -185
- package/dist/adapters/adk-sdk.js.map +0 -1
- package/dist/chunk-2E2LWE6C.js +0 -171
- package/dist/chunk-2E2LWE6C.js.map +0 -1
- package/dist/chunk-6JVGBBP3.js.map +0 -1
- package/dist/chunk-D2QTXQB2.js +0 -266
- package/dist/chunk-D2QTXQB2.js.map +0 -1
- package/dist/chunk-KKJW4TE5.js +0 -302
- package/dist/chunk-KKJW4TE5.js.map +0 -1
- package/dist/chunk-M57HVYUU.js +0 -325
- package/dist/chunk-M57HVYUU.js.map +0 -1
- package/dist/chunk-S7MTKPBF.js.map +0 -1
- package/dist/dsl-context-LACANPO4.js +0 -13
- package/dist/dsl-context-LACANPO4.js.map +0 -1
- /package/dist/{bound-resolver-I7WLYGC6.js.map → bound-resolver-RPJYAOCW.js.map} +0 -0
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
// src/lib/bound-resolver.ts
|
|
2
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
3
|
+
import YAML2 from "yaml";
|
|
4
|
+
|
|
5
|
+
// src/generator/template-engine.ts
|
|
6
|
+
import { readFile } from "fs/promises";
|
|
7
|
+
import { resolve, join } from "path";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
import Handlebars from "handlebars";
|
|
10
|
+
import { existsSync } from "fs";
|
|
11
|
+
var __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
12
|
+
function findPackageRoot() {
|
|
13
|
+
let dir = __dirname;
|
|
14
|
+
for (let i = 0; i < 5; i++) {
|
|
15
|
+
if (existsSync(join(dir, "package.json"))) return dir;
|
|
16
|
+
dir = resolve(dir, "..");
|
|
17
|
+
}
|
|
18
|
+
return resolve(__dirname, "..");
|
|
19
|
+
}
|
|
20
|
+
var BUILTIN_TEMPLATES_DIR = join(findPackageRoot(), "templates");
|
|
21
|
+
function toCamelCase(str) {
|
|
22
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
23
|
+
}
|
|
24
|
+
function toPascalCase(str) {
|
|
25
|
+
const camel = toCamelCase(str);
|
|
26
|
+
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
27
|
+
}
|
|
28
|
+
var hbs = Handlebars.create();
|
|
29
|
+
hbs.registerHelper("camelCase", (str) => toCamelCase(str));
|
|
30
|
+
hbs.registerHelper("PascalCase", (str) => toPascalCase(str));
|
|
31
|
+
hbs.registerHelper("eq", (a, b) => a === b);
|
|
32
|
+
hbs.registerHelper(
|
|
33
|
+
"notEmpty",
|
|
34
|
+
(arr) => Array.isArray(arr) ? arr.length > 0 : !!arr
|
|
35
|
+
);
|
|
36
|
+
hbs.registerHelper(
|
|
37
|
+
"json",
|
|
38
|
+
(value) => new Handlebars.SafeString(JSON.stringify(value, null, 2))
|
|
39
|
+
);
|
|
40
|
+
hbs.registerHelper("relativeImport", (fileName) => {
|
|
41
|
+
if (typeof fileName !== "string" || !fileName.endsWith(".ts")) {
|
|
42
|
+
throw new Error(`relativeImport expects a generated .ts file name, got ${JSON.stringify(fileName)}`);
|
|
43
|
+
}
|
|
44
|
+
return `./${fileName.slice(0, -".ts".length)}.js`;
|
|
45
|
+
});
|
|
46
|
+
hbs.registerHelper(
|
|
47
|
+
"hasDependsOn",
|
|
48
|
+
(step) => "depends_on" in step && Array.isArray(step.depends_on)
|
|
49
|
+
);
|
|
50
|
+
hbs.registerHelper(
|
|
51
|
+
"jsonInline",
|
|
52
|
+
(value) => new Handlebars.SafeString(JSON.stringify(value))
|
|
53
|
+
);
|
|
54
|
+
hbs.registerHelper("indent", (text, spaces) => {
|
|
55
|
+
if (typeof text !== "string") return text;
|
|
56
|
+
const pad = " ".repeat(spaces);
|
|
57
|
+
return text.split("\n").map((line, i) => i === 0 ? line : pad + line).join("\n");
|
|
58
|
+
});
|
|
59
|
+
function escapeRegexPatternForSource(pattern) {
|
|
60
|
+
return pattern.replace(/\\/g, "\\\\").replace(/\//g, "\\/");
|
|
61
|
+
}
|
|
62
|
+
function appendStringValidations(base, schema) {
|
|
63
|
+
let result = base;
|
|
64
|
+
if (schema.minLength !== void 0) result += `.min(${schema.minLength})`;
|
|
65
|
+
if (schema.maxLength !== void 0) result += `.max(${schema.maxLength})`;
|
|
66
|
+
if (schema.pattern !== void 0) {
|
|
67
|
+
const escaped = escapeRegexPatternForSource(schema.pattern);
|
|
68
|
+
result += `.regex(/${escaped}/)`;
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
function appendNumberValidations(base, schema, isInteger) {
|
|
73
|
+
let result = base;
|
|
74
|
+
if (isInteger) result += ".int()";
|
|
75
|
+
if (schema.minimum !== void 0) result += `.min(${schema.minimum})`;
|
|
76
|
+
if (schema.maximum !== void 0) result += `.max(${schema.maximum})`;
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
function appendArrayValidations(base, schema) {
|
|
80
|
+
let result = base;
|
|
81
|
+
if (schema.minItems !== void 0) result += `.min(${schema.minItems})`;
|
|
82
|
+
if (schema.maxItems !== void 0) result += `.max(${schema.maxItems})`;
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
hbs.registerHelper("zodType", function zodType(schema) {
|
|
86
|
+
if (!schema || typeof schema !== "object") return "z.unknown()";
|
|
87
|
+
if (schema.enum) {
|
|
88
|
+
const vals = schema.enum.map((v) => `"${v}"`).join(", ");
|
|
89
|
+
return `z.enum([${vals}])`;
|
|
90
|
+
}
|
|
91
|
+
if (schema.const !== void 0) {
|
|
92
|
+
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
93
|
+
}
|
|
94
|
+
const type = schema.type;
|
|
95
|
+
if (type === "string") return appendStringValidations("z.string()", schema);
|
|
96
|
+
if (type === "integer") return appendNumberValidations("z.number()", schema, true);
|
|
97
|
+
if (type === "number") return appendNumberValidations("z.number()", schema, false);
|
|
98
|
+
if (type === "boolean") return "z.boolean()";
|
|
99
|
+
if (type === "array") {
|
|
100
|
+
const items = schema.items;
|
|
101
|
+
const inner = items ? zodType(items) : "z.unknown()";
|
|
102
|
+
return appendArrayValidations(`z.array(${inner})`, schema);
|
|
103
|
+
}
|
|
104
|
+
if (type === "object") {
|
|
105
|
+
const props = schema.properties;
|
|
106
|
+
if (!props) return "z.record(z.string(), z.unknown())";
|
|
107
|
+
const required = schema.required ?? [];
|
|
108
|
+
const fields = Object.entries(props).map(([key, val]) => {
|
|
109
|
+
const base = zodType(val);
|
|
110
|
+
const opt = required.includes(key) ? "" : ".optional()";
|
|
111
|
+
const safeKey = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : `"${key}"`;
|
|
112
|
+
return ` ${safeKey}: ${base}${opt},`;
|
|
113
|
+
}).join("\n");
|
|
114
|
+
return `z.object({
|
|
115
|
+
${fields}
|
|
116
|
+
})`;
|
|
117
|
+
}
|
|
118
|
+
if (schema.allOf) {
|
|
119
|
+
const parts = schema.allOf.map(zodType);
|
|
120
|
+
if (parts.length === 1) return parts[0];
|
|
121
|
+
return parts.reduce((acc, part) => `${acc}.extend(${part}.shape)`);
|
|
122
|
+
}
|
|
123
|
+
return "z.unknown()";
|
|
124
|
+
});
|
|
125
|
+
hbs.registerHelper("regexEscape", (str) => {
|
|
126
|
+
if (typeof str !== "string") return str;
|
|
127
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
128
|
+
});
|
|
129
|
+
hbs.registerHelper("stringArray", (arr) => {
|
|
130
|
+
if (!Array.isArray(arr)) return "[]";
|
|
131
|
+
return "[" + arr.map((s) => `"${s}"`).join(", ") + "]";
|
|
132
|
+
});
|
|
133
|
+
async function loadTemplate(name, customDir) {
|
|
134
|
+
if (customDir) {
|
|
135
|
+
try {
|
|
136
|
+
return await readFile(join(customDir, name), "utf8");
|
|
137
|
+
} catch {
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return readFile(join(BUILTIN_TEMPLATES_DIR, name), "utf8");
|
|
141
|
+
}
|
|
142
|
+
async function renderTemplate(name, context, customDir) {
|
|
143
|
+
const source = await loadTemplate(name, customDir);
|
|
144
|
+
const compiled = hbs.compile(source, { noEscape: true });
|
|
145
|
+
return compiled(context);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/generator/context.ts
|
|
149
|
+
function isTaskOptional(taskId, workflows) {
|
|
150
|
+
for (const wf of Object.values(workflows)) {
|
|
151
|
+
for (const step of wf.steps) {
|
|
152
|
+
if (step.type === "delegate" && step.task === taskId && step.optional) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
function buildContractContext(dsl) {
|
|
160
|
+
const agents = dsl.agents ?? {};
|
|
161
|
+
const tasks = dsl.tasks ?? {};
|
|
162
|
+
const workflows = dsl.workflow ?? {};
|
|
163
|
+
const handoffTypes = dsl.handoff_types ?? {};
|
|
164
|
+
const agentEntries = Object.entries(agents).map(([id, agent]) => ({
|
|
165
|
+
id,
|
|
166
|
+
varName: toCamelCase(id),
|
|
167
|
+
role_name: agent.role_name ?? id,
|
|
168
|
+
purpose: agent.purpose ?? "",
|
|
169
|
+
mode: agent.mode ?? "read-write",
|
|
170
|
+
dispatch_only: agent.dispatch_only ?? false,
|
|
171
|
+
can_read_artifacts: agent.can_read_artifacts ?? [],
|
|
172
|
+
can_write_artifacts: agent.can_write_artifacts ?? [],
|
|
173
|
+
can_execute_tools: agent.can_execute_tools ?? [],
|
|
174
|
+
can_invoke_agents: agent.can_invoke_agents ?? [],
|
|
175
|
+
can_return_handoffs: agent.can_return_handoffs ?? [],
|
|
176
|
+
responsibilities: agent.responsibilities ?? [],
|
|
177
|
+
constraints: agent.constraints ?? [],
|
|
178
|
+
rules: agent.rules ?? [],
|
|
179
|
+
escalation_criteria: agent.escalation_criteria ?? []
|
|
180
|
+
}));
|
|
181
|
+
const taskEntries = Object.entries(tasks).map(([id, task]) => ({
|
|
182
|
+
id,
|
|
183
|
+
varName: toCamelCase(id),
|
|
184
|
+
description: task.description ?? "",
|
|
185
|
+
target_agent: task.target_agent ?? "",
|
|
186
|
+
allowed_from_agents: task.allowed_from_agents ?? [],
|
|
187
|
+
workflow: task.workflow ?? "",
|
|
188
|
+
invocation_handoff: task.invocation_handoff ?? "",
|
|
189
|
+
result_handoff: task.result_handoff ?? "",
|
|
190
|
+
input_artifacts: task.input_artifacts ?? [],
|
|
191
|
+
responsibilities: task.responsibilities ?? [],
|
|
192
|
+
completion_criteria: task.completion_criteria ?? [],
|
|
193
|
+
optional: isTaskOptional(id, workflows),
|
|
194
|
+
...task.model_class ? { model_class: task.model_class } : {}
|
|
195
|
+
}));
|
|
196
|
+
const workflowEntries = Object.entries(workflows).map(
|
|
197
|
+
([id, wf]) => {
|
|
198
|
+
const steps = (wf.steps ?? []).map((step) => {
|
|
199
|
+
const entry = { type: step.type };
|
|
200
|
+
if (step.type === "delegate") {
|
|
201
|
+
entry.task = step.task;
|
|
202
|
+
entry.from_agent = step.from_agent;
|
|
203
|
+
entry.description = step.description ?? "";
|
|
204
|
+
const wfStep = step;
|
|
205
|
+
entry.optional = isTaskOptional(
|
|
206
|
+
step.task,
|
|
207
|
+
workflows
|
|
208
|
+
);
|
|
209
|
+
entry.max_retries = wfStep.max_retries ?? (wfStep.retry ? 1 : 0);
|
|
210
|
+
entry.max_follow_ups = wfStep.max_follow_ups;
|
|
211
|
+
if (wfStep.depends_on !== void 0) {
|
|
212
|
+
entry.depends_on = wfStep.depends_on;
|
|
213
|
+
}
|
|
214
|
+
if (wfStep.retry) {
|
|
215
|
+
const r = wfStep.retry;
|
|
216
|
+
entry.retry = {
|
|
217
|
+
condition: r.condition,
|
|
218
|
+
fix_task: r.fix_task,
|
|
219
|
+
revalidate_task: r.revalidate_task
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
} else if (step.type === "gate") {
|
|
223
|
+
entry.gate_kind = step.gate_kind;
|
|
224
|
+
entry.description = step.description ?? "";
|
|
225
|
+
if (step.depends_on !== void 0) {
|
|
226
|
+
entry.depends_on = step.depends_on;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return entry;
|
|
230
|
+
});
|
|
231
|
+
return {
|
|
232
|
+
id,
|
|
233
|
+
varName: toCamelCase(id),
|
|
234
|
+
description: wf.description ?? "",
|
|
235
|
+
trigger: wf.trigger ?? "",
|
|
236
|
+
entry_conditions: wf.entry_conditions ?? [],
|
|
237
|
+
steps
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
const handoffEntries = Object.entries(handoffTypes).map(
|
|
242
|
+
([id, ht]) => ({
|
|
243
|
+
id,
|
|
244
|
+
varName: toCamelCase(id),
|
|
245
|
+
pascalName: toPascalCase(id),
|
|
246
|
+
schemaName: toPascalCase(id) + "Schema",
|
|
247
|
+
description: ht.description ?? "",
|
|
248
|
+
schema: ht.schema ?? {}
|
|
249
|
+
})
|
|
250
|
+
);
|
|
251
|
+
return { agentEntries, taskEntries, workflowEntries, handoffEntries };
|
|
252
|
+
}
|
|
253
|
+
function buildGuardrailContext(dsl, binding, policyName) {
|
|
254
|
+
const empty = {
|
|
255
|
+
commandChecks: [],
|
|
256
|
+
fileChecks: [],
|
|
257
|
+
contentChecks: [],
|
|
258
|
+
allChecks: [],
|
|
259
|
+
hasGuardrails: false
|
|
260
|
+
};
|
|
261
|
+
if (!binding?.guardrail_impl) return empty;
|
|
262
|
+
const guardrails = dsl.guardrails ?? {};
|
|
263
|
+
const policies = dsl.guardrail_policies ?? {};
|
|
264
|
+
const policy = policyName ? policies[policyName] : void 0;
|
|
265
|
+
const allChecks = [];
|
|
266
|
+
for (const [guardrailId, impl] of Object.entries(
|
|
267
|
+
binding.guardrail_impl
|
|
268
|
+
)) {
|
|
269
|
+
const guardrail = guardrails[guardrailId];
|
|
270
|
+
const description = guardrail?.description ?? guardrailId;
|
|
271
|
+
const policyRule = policy?.rules?.find(
|
|
272
|
+
(r) => r.guardrail === guardrailId
|
|
273
|
+
);
|
|
274
|
+
const severity = policyRule?.severity ?? "warning";
|
|
275
|
+
const action = policyRule?.action ?? "warn";
|
|
276
|
+
for (const check of impl.checks) {
|
|
277
|
+
const matcher = check.matcher;
|
|
278
|
+
if (!matcher) continue;
|
|
279
|
+
allChecks.push({
|
|
280
|
+
guardrail_id: guardrailId,
|
|
281
|
+
description,
|
|
282
|
+
severity,
|
|
283
|
+
action,
|
|
284
|
+
matcher_type: matcher.type,
|
|
285
|
+
pattern: matcher.pattern,
|
|
286
|
+
message: check.message ?? description,
|
|
287
|
+
file_glob: matcher.file_glob,
|
|
288
|
+
exclude_glob: matcher.exclude_glob
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
commandChecks: allChecks.filter((c) => c.matcher_type === "command_regex"),
|
|
294
|
+
fileChecks: allChecks.filter((c) => c.matcher_type === "file_glob"),
|
|
295
|
+
contentChecks: allChecks.filter((c) => c.matcher_type === "content_regex"),
|
|
296
|
+
allChecks,
|
|
297
|
+
hasGuardrails: allChecks.length > 0
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/lib/dsl-context.ts
|
|
302
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
303
|
+
import YAML from "yaml";
|
|
304
|
+
|
|
305
|
+
// src/lib/schema-converter.ts
|
|
306
|
+
import { z } from "zod";
|
|
307
|
+
function applyStringValidations(schema, def) {
|
|
308
|
+
let s = schema;
|
|
309
|
+
if (typeof def.minLength === "number") s = s.min(def.minLength);
|
|
310
|
+
if (typeof def.maxLength === "number") s = s.max(def.maxLength);
|
|
311
|
+
if (typeof def.pattern === "string") s = s.regex(new RegExp(def.pattern));
|
|
312
|
+
return s;
|
|
313
|
+
}
|
|
314
|
+
function applyNumberValidations(schema, def, isInteger) {
|
|
315
|
+
let n = schema;
|
|
316
|
+
if (isInteger) n = n.int();
|
|
317
|
+
if (typeof def.minimum === "number") n = n.min(def.minimum);
|
|
318
|
+
if (typeof def.maximum === "number") n = n.max(def.maximum);
|
|
319
|
+
return n;
|
|
320
|
+
}
|
|
321
|
+
function applyArrayValidations(schema, def) {
|
|
322
|
+
let a = schema;
|
|
323
|
+
if (typeof def.minItems === "number") a = a.min(def.minItems);
|
|
324
|
+
if (typeof def.maxItems === "number") a = a.max(def.maxItems);
|
|
325
|
+
return a;
|
|
326
|
+
}
|
|
327
|
+
function jsonSchemaToZod(schema) {
|
|
328
|
+
if (!schema || typeof schema !== "object") return z.unknown();
|
|
329
|
+
if (schema.enum) {
|
|
330
|
+
const vals = schema.enum;
|
|
331
|
+
if (vals.length === 0) return z.unknown();
|
|
332
|
+
return z.enum(vals);
|
|
333
|
+
}
|
|
334
|
+
if (schema.const !== void 0) {
|
|
335
|
+
const val = schema.const;
|
|
336
|
+
if (typeof val === "string" || typeof val === "number" || typeof val === "boolean") {
|
|
337
|
+
return z.literal(val);
|
|
338
|
+
}
|
|
339
|
+
return z.unknown();
|
|
340
|
+
}
|
|
341
|
+
const type = schema.type;
|
|
342
|
+
if (type === "string") return applyStringValidations(z.string(), schema);
|
|
343
|
+
if (type === "integer") return applyNumberValidations(z.number(), schema, true);
|
|
344
|
+
if (type === "number") return applyNumberValidations(z.number(), schema, false);
|
|
345
|
+
if (type === "boolean") return z.boolean();
|
|
346
|
+
if (type === "array") {
|
|
347
|
+
const items = schema.items;
|
|
348
|
+
const inner = items ? jsonSchemaToZod(items) : z.unknown();
|
|
349
|
+
return applyArrayValidations(z.array(inner), schema);
|
|
350
|
+
}
|
|
351
|
+
if (type === "object") {
|
|
352
|
+
const props = schema.properties;
|
|
353
|
+
if (!props) return z.record(z.string(), z.unknown());
|
|
354
|
+
const required = new Set(schema.required ?? []);
|
|
355
|
+
const shape = {};
|
|
356
|
+
for (const [key, val] of Object.entries(props)) {
|
|
357
|
+
const fieldSchema = jsonSchemaToZod(val);
|
|
358
|
+
shape[key] = required.has(key) ? fieldSchema : fieldSchema.optional();
|
|
359
|
+
}
|
|
360
|
+
return z.object(shape).passthrough();
|
|
361
|
+
}
|
|
362
|
+
if (schema.allOf) {
|
|
363
|
+
const parts = schema.allOf.map(jsonSchemaToZod);
|
|
364
|
+
if (parts.length === 0) return z.unknown();
|
|
365
|
+
if (parts.length === 1) return parts[0];
|
|
366
|
+
let merged = parts[0];
|
|
367
|
+
for (let i = 1; i < parts.length; i++) {
|
|
368
|
+
const part = parts[i];
|
|
369
|
+
if (merged instanceof z.ZodObject && part instanceof z.ZodObject) {
|
|
370
|
+
merged = merged.merge(part);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return merged;
|
|
374
|
+
}
|
|
375
|
+
return z.unknown();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// src/lib/dsl-context.ts
|
|
379
|
+
var DslValidationError = class extends Error {
|
|
380
|
+
missing;
|
|
381
|
+
constructor(missing) {
|
|
382
|
+
super(
|
|
383
|
+
"Required entities missing after DSL merge:\n" + missing.map((m) => ` - ${m}`).join("\n")
|
|
384
|
+
);
|
|
385
|
+
this.name = "DslValidationError";
|
|
386
|
+
this.missing = missing;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
function buildAgentRegistry(agents) {
|
|
390
|
+
const registry = {};
|
|
391
|
+
for (const [id, agent] of Object.entries(agents)) {
|
|
392
|
+
const rawMode = agent.mode ?? "read-only";
|
|
393
|
+
const mode = rawMode === "read-write" ? "read-write" : "read-only";
|
|
394
|
+
registry[id] = {
|
|
395
|
+
id,
|
|
396
|
+
role_name: agent.role_name ?? id,
|
|
397
|
+
purpose: agent.purpose ?? "",
|
|
398
|
+
mode,
|
|
399
|
+
responsibilities: agent.responsibilities ?? [],
|
|
400
|
+
constraints: agent.constraints ?? [],
|
|
401
|
+
rules: agent.rules ?? [],
|
|
402
|
+
can_invoke_agents: agent.can_invoke_agents ?? void 0,
|
|
403
|
+
can_execute_tools: agent.can_execute_tools ?? void 0
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
return registry;
|
|
407
|
+
}
|
|
408
|
+
function buildTaskRegistry(tasks) {
|
|
409
|
+
const registry = {};
|
|
410
|
+
for (const [id, task] of Object.entries(tasks)) {
|
|
411
|
+
registry[id] = {
|
|
412
|
+
id,
|
|
413
|
+
description: task.description ?? "",
|
|
414
|
+
target_agent: task.target_agent ?? "",
|
|
415
|
+
result_handoff: task.result_handoff ?? "",
|
|
416
|
+
completion_criteria: task.completion_criteria ?? [],
|
|
417
|
+
model_class: task.model_class
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
return registry;
|
|
421
|
+
}
|
|
422
|
+
function buildWorkflowStep(step) {
|
|
423
|
+
if (step.type === "gate") {
|
|
424
|
+
return {
|
|
425
|
+
type: "gate",
|
|
426
|
+
gate_kind: step.gate_kind ?? "",
|
|
427
|
+
description: step.description ?? "",
|
|
428
|
+
depends_on: step.depends_on
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
type: "delegate",
|
|
433
|
+
task: step.task ?? "",
|
|
434
|
+
from_agent: step.from_agent ?? "",
|
|
435
|
+
description: step.description ?? "",
|
|
436
|
+
optional: step.optional ?? false,
|
|
437
|
+
max_retries: step.max_retries ?? 0,
|
|
438
|
+
max_follow_ups: step.max_follow_ups,
|
|
439
|
+
depends_on: step.depends_on,
|
|
440
|
+
retry: step.retry
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function buildWorkflowRegistry(workflows) {
|
|
444
|
+
const registry = {};
|
|
445
|
+
for (const [id, wf] of Object.entries(workflows)) {
|
|
446
|
+
const steps = (wf.steps ?? []).map(buildWorkflowStep);
|
|
447
|
+
registry[id] = { id, description: wf.description ?? "", steps };
|
|
448
|
+
}
|
|
449
|
+
return registry;
|
|
450
|
+
}
|
|
451
|
+
function buildHandoffSchemas(handoffTypes) {
|
|
452
|
+
const schemas = {};
|
|
453
|
+
for (const [id, ht] of Object.entries(handoffTypes)) {
|
|
454
|
+
const schema = ht.schema;
|
|
455
|
+
if (schema) {
|
|
456
|
+
schemas[id] = jsonSchemaToZod(schema);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return schemas;
|
|
460
|
+
}
|
|
461
|
+
function buildGuardrailRulesFromDsl(dsl) {
|
|
462
|
+
const raw = dsl._guardrailRules;
|
|
463
|
+
if (!raw) return void 0;
|
|
464
|
+
return {
|
|
465
|
+
commandRules: raw.commandRules ?? [],
|
|
466
|
+
fileRules: raw.fileRules ?? [],
|
|
467
|
+
contentRules: raw.contentRules ?? []
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
function buildRegistriesFromDsl(dsl) {
|
|
471
|
+
const agents = dsl.agents ?? {};
|
|
472
|
+
const tasks = dsl.tasks ?? {};
|
|
473
|
+
const workflows = dsl.workflow ?? {};
|
|
474
|
+
const handoffTypes = dsl.handoff_types ?? {};
|
|
475
|
+
return {
|
|
476
|
+
agentRegistry: buildAgentRegistry(agents),
|
|
477
|
+
taskRegistry: buildTaskRegistry(tasks),
|
|
478
|
+
workflowRegistry: buildWorkflowRegistry(workflows),
|
|
479
|
+
handoffSchemas: buildHandoffSchemas(handoffTypes)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function validateRequiredEntities(dsl, required) {
|
|
483
|
+
const missing = [];
|
|
484
|
+
const workflows = dsl.workflow ?? {};
|
|
485
|
+
const tasks = dsl.tasks ?? {};
|
|
486
|
+
const handoffTypes = dsl.handoff_types ?? {};
|
|
487
|
+
if (required.workflows) {
|
|
488
|
+
for (const wfId of required.workflows) {
|
|
489
|
+
if (!(wfId in workflows)) {
|
|
490
|
+
missing.push(`workflow "${wfId}" is required by the CLI but not found in the resolved DSL`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (required.tasks) {
|
|
495
|
+
for (const taskId of required.tasks) {
|
|
496
|
+
if (!(taskId in tasks)) {
|
|
497
|
+
missing.push(`task "${taskId}" is required by the CLI but not found in the resolved DSL`);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const task = tasks[taskId];
|
|
501
|
+
const invHandoff = task.invocation_handoff;
|
|
502
|
+
const resHandoff = task.result_handoff;
|
|
503
|
+
if (invHandoff && !(invHandoff in handoffTypes)) {
|
|
504
|
+
missing.push(`handoff_type "${invHandoff}" (invocation_handoff of task "${taskId}") not found`);
|
|
505
|
+
}
|
|
506
|
+
if (resHandoff && !(resHandoff in handoffTypes)) {
|
|
507
|
+
missing.push(`handoff_type "${resHandoff}" (result_handoff of task "${taskId}") not found`);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (required.workflows) {
|
|
512
|
+
for (const wfId of required.workflows) {
|
|
513
|
+
if (!(wfId in workflows)) continue;
|
|
514
|
+
const wf = workflows[wfId];
|
|
515
|
+
const steps = wf.steps ?? [];
|
|
516
|
+
for (const step of steps) {
|
|
517
|
+
if (step.type !== "delegate") continue;
|
|
518
|
+
const taskId = step.task;
|
|
519
|
+
if (taskId && taskId in tasks) {
|
|
520
|
+
const task = tasks[taskId];
|
|
521
|
+
const resHandoff = task.result_handoff;
|
|
522
|
+
if (resHandoff && !(resHandoff in handoffTypes)) {
|
|
523
|
+
missing.push(`handoff_type "${resHandoff}" (result of task "${taskId}" in workflow "${wfId}") not found`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (missing.length > 0) {
|
|
530
|
+
throw new DslValidationError(missing);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
async function loadDslContext(options) {
|
|
534
|
+
let dsl;
|
|
535
|
+
if (options.projectDslPath) {
|
|
536
|
+
const { resolve: resolveDsl, mergeDsl, expandDefaults } = await import("agent-contracts");
|
|
537
|
+
const raw = await readFile2(options.projectDslPath, "utf8");
|
|
538
|
+
const projectData = YAML.parse(raw);
|
|
539
|
+
if (projectData.extends === "/$embedded/") {
|
|
540
|
+
const { extends: _extends, ...projectOverrides } = projectData;
|
|
541
|
+
const merged = mergeDsl(options.embeddedDsl, projectOverrides);
|
|
542
|
+
dsl = expandDefaults(merged);
|
|
543
|
+
} else {
|
|
544
|
+
const projectResult = await resolveDsl(options.projectDslPath);
|
|
545
|
+
const merged = mergeDsl(options.embeddedDsl, projectResult.data);
|
|
546
|
+
dsl = expandDefaults(merged);
|
|
547
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
dsl = options.embeddedDsl;
|
|
550
|
+
}
|
|
551
|
+
if (options.requiredEntities) {
|
|
552
|
+
validateRequiredEntities(dsl, options.requiredEntities);
|
|
553
|
+
}
|
|
554
|
+
const registries = buildRegistriesFromDsl(dsl);
|
|
555
|
+
const guardrailRules = buildGuardrailRulesFromDsl(dsl);
|
|
556
|
+
return { registries, dsl, guardrailRules };
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/lib/bound-resolver.ts
|
|
560
|
+
function getEffectiveGuardrailIds(dsl, agentId) {
|
|
561
|
+
const ids = /* @__PURE__ */ new Set();
|
|
562
|
+
const agents = dsl.agents ?? {};
|
|
563
|
+
const agent = agents[agentId];
|
|
564
|
+
if (agent) {
|
|
565
|
+
for (const ref of agent.guardrails ?? []) {
|
|
566
|
+
ids.add(ref);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const referenced = new Set(
|
|
570
|
+
Object.values(agents).flatMap((a) => a.guardrails ?? [])
|
|
571
|
+
);
|
|
572
|
+
const guardrails = dsl.guardrails ?? {};
|
|
573
|
+
for (const [guardrailId, guardrail] of Object.entries(guardrails)) {
|
|
574
|
+
const scopedAgents = guardrail.scope?.agents;
|
|
575
|
+
if (scopedAgents ? scopedAgents.includes(agentId) : !referenced.has(guardrailId)) {
|
|
576
|
+
ids.add(guardrailId);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return ids;
|
|
580
|
+
}
|
|
581
|
+
function guardrailCheckToCommandRule(c) {
|
|
582
|
+
return {
|
|
583
|
+
guardrail_id: c.guardrail_id,
|
|
584
|
+
pattern: c.pattern,
|
|
585
|
+
action: c.action,
|
|
586
|
+
message: c.message
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function guardrailCheckToFileRule(c) {
|
|
590
|
+
return {
|
|
591
|
+
guardrail_id: c.guardrail_id,
|
|
592
|
+
pattern: c.pattern,
|
|
593
|
+
action: c.action,
|
|
594
|
+
message: c.message,
|
|
595
|
+
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function guardrailCheckToContentRule(c) {
|
|
599
|
+
return {
|
|
600
|
+
guardrail_id: c.guardrail_id,
|
|
601
|
+
pattern: c.pattern,
|
|
602
|
+
action: c.action,
|
|
603
|
+
message: c.message,
|
|
604
|
+
...c.file_glob ? { file_glob: c.file_glob } : {},
|
|
605
|
+
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function guardrailContextToRuleData(ctx, effectiveIds) {
|
|
609
|
+
const filter = (checks) => effectiveIds ? checks.filter((c) => effectiveIds.has(c.guardrail_id)) : checks;
|
|
610
|
+
return {
|
|
611
|
+
commandRules: filter(ctx.commandChecks).map(guardrailCheckToCommandRule),
|
|
612
|
+
fileRules: filter(ctx.fileChecks).map(guardrailCheckToFileRule),
|
|
613
|
+
contentRules: filter(ctx.contentChecks).map(guardrailCheckToContentRule)
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
function buildPerAgentGuardrails(dsl, guardrailCtx) {
|
|
617
|
+
const perAgent = /* @__PURE__ */ new Map();
|
|
618
|
+
const agents = dsl.agents ?? {};
|
|
619
|
+
for (const agentId of Object.keys(agents)) {
|
|
620
|
+
const effectiveIds = getEffectiveGuardrailIds(dsl, agentId);
|
|
621
|
+
const rules = guardrailContextToRuleData(guardrailCtx, effectiveIds);
|
|
622
|
+
if (rules.commandRules.length > 0 || rules.fileRules.length > 0 || rules.contentRules.length > 0) {
|
|
623
|
+
perAgent.set(agentId, rules);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return perAgent;
|
|
627
|
+
}
|
|
628
|
+
function hasGuardrailRules(rules) {
|
|
629
|
+
return rules.commandRules.length > 0 || rules.fileRules.length > 0 || rules.contentRules.length > 0;
|
|
630
|
+
}
|
|
631
|
+
var BoundResolver = class {
|
|
632
|
+
cache = /* @__PURE__ */ new Map();
|
|
633
|
+
/**
|
|
634
|
+
* Resolve binding + policy in one pass. Results are cached by component id.
|
|
635
|
+
*/
|
|
636
|
+
async resolve(opts) {
|
|
637
|
+
const cached = this.cache.get(opts.id);
|
|
638
|
+
if (cached) return cached;
|
|
639
|
+
const diagnostics = [];
|
|
640
|
+
let dsl;
|
|
641
|
+
if (opts.embeddedDsl) {
|
|
642
|
+
dsl = opts.embeddedDsl;
|
|
643
|
+
} else if (opts.dslPath) {
|
|
644
|
+
const { resolve: resolveDsl } = await import("agent-contracts");
|
|
645
|
+
const result = await resolveDsl(opts.dslPath);
|
|
646
|
+
dsl = result.data;
|
|
647
|
+
} else {
|
|
648
|
+
throw new Error("BoundResolver.resolve requires embeddedDsl or dslPath");
|
|
649
|
+
}
|
|
650
|
+
if (opts.artifactBinding) {
|
|
651
|
+
const { resolveBound } = await import("agent-contracts");
|
|
652
|
+
const boundResult = await resolveBound(dsl, {
|
|
653
|
+
artifactBinding: opts.artifactBinding,
|
|
654
|
+
paths: opts.paths
|
|
655
|
+
});
|
|
656
|
+
dsl = boundResult.data;
|
|
657
|
+
for (const d of boundResult.diagnostics) {
|
|
658
|
+
diagnostics.push({
|
|
659
|
+
path: d.rule,
|
|
660
|
+
message: d.message,
|
|
661
|
+
severity: d.severity
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
let guardrailCtx = {
|
|
666
|
+
commandChecks: [],
|
|
667
|
+
fileChecks: [],
|
|
668
|
+
contentChecks: [],
|
|
669
|
+
allChecks: [],
|
|
670
|
+
hasGuardrails: false
|
|
671
|
+
};
|
|
672
|
+
if (opts.bindingPaths && opts.bindingPaths.length > 0) {
|
|
673
|
+
for (const bindingPath of opts.bindingPaths) {
|
|
674
|
+
const raw = await readFile3(bindingPath, "utf8");
|
|
675
|
+
const binding = YAML2.parse(raw);
|
|
676
|
+
guardrailCtx = buildGuardrailContext(
|
|
677
|
+
dsl,
|
|
678
|
+
binding,
|
|
679
|
+
opts.activeGuardrailPolicy
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
const perAgent = guardrailCtx.hasGuardrails ? buildPerAgentGuardrails(dsl, guardrailCtx) : /* @__PURE__ */ new Map();
|
|
684
|
+
const component = {
|
|
685
|
+
id: opts.id,
|
|
686
|
+
dsl,
|
|
687
|
+
registries: buildRegistriesFromDsl(dsl),
|
|
688
|
+
guardrails: { perAgent },
|
|
689
|
+
diagnostics
|
|
690
|
+
};
|
|
691
|
+
this.cache.set(opts.id, component);
|
|
692
|
+
return component;
|
|
693
|
+
}
|
|
694
|
+
/** Clear cached components (e.g. for testing). */
|
|
695
|
+
clearCache() {
|
|
696
|
+
this.cache.clear();
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
function getGuardrailRulesForAgent(component, agentId, fallbackDsl) {
|
|
700
|
+
const perAgent = component.guardrails.perAgent.get(agentId);
|
|
701
|
+
if (perAgent && hasGuardrailRules(perAgent)) {
|
|
702
|
+
return perAgent;
|
|
703
|
+
}
|
|
704
|
+
return fallbackDsl ? buildGuardrailRulesFromDsl(fallbackDsl) : void 0;
|
|
705
|
+
}
|
|
706
|
+
function getGuardrailRulesForComponent(component, embeddedDsl) {
|
|
707
|
+
const embedded = embeddedDsl ? buildGuardrailRulesFromDsl(embeddedDsl) : void 0;
|
|
708
|
+
if (embedded || component.guardrails.perAgent.size === 0) return embedded;
|
|
709
|
+
const union = { commandRules: [], fileRules: [], contentRules: [] };
|
|
710
|
+
const seen = /* @__PURE__ */ new Set();
|
|
711
|
+
for (const rules of component.guardrails.perAgent.values()) {
|
|
712
|
+
for (const kind of ["commandRules", "fileRules", "contentRules"]) {
|
|
713
|
+
for (const rule of rules[kind]) {
|
|
714
|
+
const key = `${kind}\0${rule.guardrail_id}\0${rule.pattern}`;
|
|
715
|
+
if (seen.has(key)) continue;
|
|
716
|
+
seen.add(key);
|
|
717
|
+
union[kind].push(rule);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return union;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
export {
|
|
725
|
+
renderTemplate,
|
|
726
|
+
buildContractContext,
|
|
727
|
+
buildGuardrailContext,
|
|
728
|
+
jsonSchemaToZod,
|
|
729
|
+
DslValidationError,
|
|
730
|
+
buildGuardrailRulesFromDsl,
|
|
731
|
+
buildRegistriesFromDsl,
|
|
732
|
+
loadDslContext,
|
|
733
|
+
getEffectiveGuardrailIds,
|
|
734
|
+
guardrailContextToRuleData,
|
|
735
|
+
buildPerAgentGuardrails,
|
|
736
|
+
BoundResolver,
|
|
737
|
+
getGuardrailRulesForAgent,
|
|
738
|
+
getGuardrailRulesForComponent
|
|
739
|
+
};
|
|
740
|
+
//# sourceMappingURL=chunk-KB4VDAAQ.js.map
|