agent-contracts-runtime 0.36.6 → 0.36.8
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/cli-contract.yaml +76 -0
- package/dist/adapters/adk-sdk.d.ts +2 -2
- package/dist/adapters/adk-sdk.js +1 -1
- package/dist/adapters/claude-agent-sdk.d.ts +2 -2
- package/dist/adapters/mock.d.ts +1 -1
- package/dist/adapters/openai-agents-sdk.d.ts +2 -2
- package/dist/agent-runtime.bundle.mjs +584 -403
- package/dist/agent-runtime.bundle.mjs.map +4 -4
- package/dist/bound-resolver-K4UHLXN6.js +17 -0
- package/dist/bound-resolver-K4UHLXN6.js.map +1 -0
- package/dist/{chunk-FFCXUPYA.js → chunk-5UGCLWDM.js} +68 -281
- package/dist/chunk-5UGCLWDM.js.map +1 -0
- package/dist/chunk-D2QTXQB2.js +266 -0
- package/dist/chunk-D2QTXQB2.js.map +1 -0
- package/dist/chunk-LTZA6QWC.js +296 -0
- package/dist/chunk-LTZA6QWC.js.map +1 -0
- package/dist/{chunk-JDXRLHY3.js → chunk-MGRZBGQL.js} +20 -1
- package/dist/chunk-MGRZBGQL.js.map +1 -0
- package/dist/chunk-MWZXU25C.js +171 -0
- package/dist/chunk-MWZXU25C.js.map +1 -0
- package/dist/chunk-VOBS62OH.js +323 -0
- package/dist/chunk-VOBS62OH.js.map +1 -0
- package/dist/cli/index.js +95 -16
- package/dist/cli/index.js.map +1 -1
- package/dist/context-B_bekYiE.d.ts +106 -0
- package/dist/dsl-context-LACANPO4.js +13 -0
- package/dist/dsl-context-LACANPO4.js.map +1 -0
- package/dist/generator/index.d.ts +17 -106
- package/dist/generator/index.js +5 -3
- package/dist/{guardrail-hooks-B1_vgkE5.d.ts → guardrail-hooks-C8G5NGsj.d.ts} +8 -1
- package/dist/index.d.ts +80 -5
- package/dist/index.js +26 -9
- package/dist/{task-runner-C1bAy5k9.d.ts → task-runner-Bo37lS9q.d.ts} +5 -0
- package/docs/cli-reference.md +160 -0
- package/package.json +7 -2
- package/dist/chunk-FFCXUPYA.js.map +0 -1
- package/dist/chunk-JDXRLHY3.js.map +0 -1
- package/dist/chunk-KQOZCJK6.js +0 -593
- package/dist/chunk-KQOZCJK6.js.map +0 -1
package/dist/chunk-KQOZCJK6.js
DELETED
|
@@ -1,593 +0,0 @@
|
|
|
1
|
-
// src/generator/index.ts
|
|
2
|
-
import { readFile as readFile3, writeFile, mkdir, rm } from "fs/promises";
|
|
3
|
-
import { resolve as resolve3, join as join3, relative } from "path";
|
|
4
|
-
import { createHash } from "crypto";
|
|
5
|
-
import YAML2 from "yaml";
|
|
6
|
-
|
|
7
|
-
// src/generator/config.ts
|
|
8
|
-
import { existsSync } from "fs";
|
|
9
|
-
import { readFile } from "fs/promises";
|
|
10
|
-
import { resolve, dirname, join, basename } from "path";
|
|
11
|
-
import { z } from "zod";
|
|
12
|
-
import YAML from "yaml";
|
|
13
|
-
var DEFAULT_CONFIG_PATH = "./agents.conf.yaml";
|
|
14
|
-
var LEGACY_CONFIG_FILENAME = "agent-runtime.config.yaml";
|
|
15
|
-
var RecoverySchema = z.object({
|
|
16
|
-
max_follow_ups: z.number().int().min(0).default(2),
|
|
17
|
-
max_retries: z.number().int().min(0).default(0)
|
|
18
|
-
}).default(() => ({ max_follow_ups: 2, max_retries: 0 }));
|
|
19
|
-
var ModelClassEntrySchema = z.object({
|
|
20
|
-
adapter: z.string(),
|
|
21
|
-
model: z.string().optional()
|
|
22
|
-
});
|
|
23
|
-
var ModelMappingSchema = z.object({
|
|
24
|
-
fast: ModelClassEntrySchema.optional(),
|
|
25
|
-
standard: ModelClassEntrySchema.optional(),
|
|
26
|
-
thinking: ModelClassEntrySchema.optional()
|
|
27
|
-
}).default({});
|
|
28
|
-
var ProgressLogSchema = z.object({
|
|
29
|
-
destination: z.enum(["stderr", "file", "both", "none"]).default("stderr"),
|
|
30
|
-
file: z.string().optional(),
|
|
31
|
-
naming: z.enum(["single", "per-invocation", "daily"]).default("single")
|
|
32
|
-
}).default(() => ({ destination: "stderr", naming: "single" })).refine(
|
|
33
|
-
(val) => {
|
|
34
|
-
if ((val.destination === "file" || val.destination === "both") && !val.file) return false;
|
|
35
|
-
return true;
|
|
36
|
-
},
|
|
37
|
-
{ message: "logging.progress_log.file is required when destination is 'file' or 'both'" }
|
|
38
|
-
);
|
|
39
|
-
var LoggingSchema = z.object({
|
|
40
|
-
progress_log: ProgressLogSchema
|
|
41
|
-
}).default(() => ({ progress_log: { destination: "stderr", naming: "single" } }));
|
|
42
|
-
var RuntimeConfigSchema = z.object({
|
|
43
|
-
dsl: z.string().default("./agent-contracts.yaml"),
|
|
44
|
-
generated_dir: z.string().default("./agent/generated"),
|
|
45
|
-
bindings: z.array(z.string()).default([]),
|
|
46
|
-
active_guardrail_policy: z.string().optional(),
|
|
47
|
-
templates_dir: z.string().optional(),
|
|
48
|
-
plugins: z.array(z.string()).default([]),
|
|
49
|
-
recovery: RecoverySchema,
|
|
50
|
-
model_mapping: ModelMappingSchema,
|
|
51
|
-
logging: LoggingSchema
|
|
52
|
-
});
|
|
53
|
-
var CliSectionSchema = z.object({
|
|
54
|
-
dsl: z.string().optional(),
|
|
55
|
-
generated_dir: z.string().optional(),
|
|
56
|
-
plugins: z.array(z.string()).optional(),
|
|
57
|
-
bindings: z.array(z.string()).optional()
|
|
58
|
-
}).strict();
|
|
59
|
-
var ProjectConfigSchema = z.object({
|
|
60
|
-
dsl: z.string().optional(),
|
|
61
|
-
generated_dir: z.string().optional(),
|
|
62
|
-
bindings: z.array(z.string()).optional(),
|
|
63
|
-
active_guardrail_policy: z.string().optional(),
|
|
64
|
-
templates_dir: z.string().optional(),
|
|
65
|
-
plugins: z.array(z.string()).optional(),
|
|
66
|
-
recovery: RecoverySchema,
|
|
67
|
-
model_mapping: ModelMappingSchema,
|
|
68
|
-
logging: LoggingSchema,
|
|
69
|
-
cli: z.record(z.string(), CliSectionSchema).optional()
|
|
70
|
-
});
|
|
71
|
-
function resolveConfigFilePath(configPath) {
|
|
72
|
-
const absPath = resolve(configPath);
|
|
73
|
-
if (existsSync(absPath)) {
|
|
74
|
-
return absPath;
|
|
75
|
-
}
|
|
76
|
-
const dir = dirname(absPath);
|
|
77
|
-
const base = basename(absPath);
|
|
78
|
-
if (base === "agents.conf.yaml") {
|
|
79
|
-
const legacyPath = join(dir, LEGACY_CONFIG_FILENAME);
|
|
80
|
-
if (existsSync(legacyPath)) {
|
|
81
|
-
return legacyPath;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return absPath;
|
|
85
|
-
}
|
|
86
|
-
function buildResolvedConfig(config, configDir) {
|
|
87
|
-
const pl = config.logging.progress_log;
|
|
88
|
-
return {
|
|
89
|
-
...config,
|
|
90
|
-
configDir,
|
|
91
|
-
dslPath: resolve(configDir, config.dsl),
|
|
92
|
-
generatedDir: resolve(configDir, config.generated_dir),
|
|
93
|
-
bindingPaths: config.bindings.map((b) => resolve(configDir, b)),
|
|
94
|
-
templatesDir: config.templates_dir ? resolve(configDir, config.templates_dir) : void 0,
|
|
95
|
-
pluginPaths: config.plugins,
|
|
96
|
-
resolvedLogging: {
|
|
97
|
-
progressLog: {
|
|
98
|
-
destination: pl.destination,
|
|
99
|
-
file: pl.file ? resolve(configDir, pl.file) : void 0,
|
|
100
|
-
naming: pl.naming
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
async function loadConfig(configPath) {
|
|
106
|
-
const absPath = resolveConfigFilePath(configPath);
|
|
107
|
-
const raw = await readFile(absPath, "utf8");
|
|
108
|
-
const parsed = YAML.parse(raw);
|
|
109
|
-
const config = RuntimeConfigSchema.parse(parsed);
|
|
110
|
-
const configDir = dirname(absPath);
|
|
111
|
-
return buildResolvedConfig(config, configDir);
|
|
112
|
-
}
|
|
113
|
-
async function loadProjectConfig(configPath, cliName) {
|
|
114
|
-
const absPath = resolveConfigFilePath(configPath);
|
|
115
|
-
const raw = await readFile(absPath, "utf8");
|
|
116
|
-
const parsed = YAML.parse(raw);
|
|
117
|
-
const projectConfig = ProjectConfigSchema.parse(parsed);
|
|
118
|
-
const { cli: _cli, ...baseConfig } = projectConfig;
|
|
119
|
-
let merged = { ...baseConfig };
|
|
120
|
-
if (cliName && projectConfig.cli?.[cliName]) {
|
|
121
|
-
merged = { ...merged, ...projectConfig.cli[cliName] };
|
|
122
|
-
}
|
|
123
|
-
const config = RuntimeConfigSchema.parse(merged);
|
|
124
|
-
const configDir = dirname(absPath);
|
|
125
|
-
return buildResolvedConfig(config, configDir);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// src/generator/template-engine.ts
|
|
129
|
-
import { readFile as readFile2 } from "fs/promises";
|
|
130
|
-
import { resolve as resolve2, join as join2 } from "path";
|
|
131
|
-
import { fileURLToPath } from "url";
|
|
132
|
-
import Handlebars from "handlebars";
|
|
133
|
-
import { existsSync as existsSync2 } from "fs";
|
|
134
|
-
var __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
135
|
-
function findPackageRoot() {
|
|
136
|
-
let dir = __dirname;
|
|
137
|
-
for (let i = 0; i < 5; i++) {
|
|
138
|
-
if (existsSync2(join2(dir, "package.json"))) return dir;
|
|
139
|
-
dir = resolve2(dir, "..");
|
|
140
|
-
}
|
|
141
|
-
return resolve2(__dirname, "..");
|
|
142
|
-
}
|
|
143
|
-
var BUILTIN_TEMPLATES_DIR = join2(findPackageRoot(), "templates");
|
|
144
|
-
function toCamelCase(str) {
|
|
145
|
-
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
146
|
-
}
|
|
147
|
-
function toPascalCase(str) {
|
|
148
|
-
const camel = toCamelCase(str);
|
|
149
|
-
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
150
|
-
}
|
|
151
|
-
var hbs = Handlebars.create();
|
|
152
|
-
hbs.registerHelper("camelCase", (str) => toCamelCase(str));
|
|
153
|
-
hbs.registerHelper("PascalCase", (str) => toPascalCase(str));
|
|
154
|
-
hbs.registerHelper("eq", (a, b) => a === b);
|
|
155
|
-
hbs.registerHelper(
|
|
156
|
-
"notEmpty",
|
|
157
|
-
(arr) => Array.isArray(arr) ? arr.length > 0 : !!arr
|
|
158
|
-
);
|
|
159
|
-
hbs.registerHelper(
|
|
160
|
-
"json",
|
|
161
|
-
(value) => new Handlebars.SafeString(JSON.stringify(value, null, 2))
|
|
162
|
-
);
|
|
163
|
-
hbs.registerHelper(
|
|
164
|
-
"hasDependsOn",
|
|
165
|
-
(step) => "depends_on" in step && Array.isArray(step.depends_on)
|
|
166
|
-
);
|
|
167
|
-
hbs.registerHelper(
|
|
168
|
-
"jsonInline",
|
|
169
|
-
(value) => new Handlebars.SafeString(JSON.stringify(value))
|
|
170
|
-
);
|
|
171
|
-
hbs.registerHelper("indent", (text, spaces) => {
|
|
172
|
-
if (typeof text !== "string") return text;
|
|
173
|
-
const pad = " ".repeat(spaces);
|
|
174
|
-
return text.split("\n").map((line, i) => i === 0 ? line : pad + line).join("\n");
|
|
175
|
-
});
|
|
176
|
-
function escapeRegexPatternForSource(pattern) {
|
|
177
|
-
return pattern.replace(/\\/g, "\\\\").replace(/\//g, "\\/");
|
|
178
|
-
}
|
|
179
|
-
function appendStringValidations(base, schema) {
|
|
180
|
-
let result = base;
|
|
181
|
-
if (schema.minLength !== void 0) result += `.min(${schema.minLength})`;
|
|
182
|
-
if (schema.maxLength !== void 0) result += `.max(${schema.maxLength})`;
|
|
183
|
-
if (schema.pattern !== void 0) {
|
|
184
|
-
const escaped = escapeRegexPatternForSource(schema.pattern);
|
|
185
|
-
result += `.regex(/${escaped}/)`;
|
|
186
|
-
}
|
|
187
|
-
return result;
|
|
188
|
-
}
|
|
189
|
-
function appendNumberValidations(base, schema, isInteger) {
|
|
190
|
-
let result = base;
|
|
191
|
-
if (isInteger) result += ".int()";
|
|
192
|
-
if (schema.minimum !== void 0) result += `.min(${schema.minimum})`;
|
|
193
|
-
if (schema.maximum !== void 0) result += `.max(${schema.maximum})`;
|
|
194
|
-
return result;
|
|
195
|
-
}
|
|
196
|
-
function appendArrayValidations(base, schema) {
|
|
197
|
-
let result = base;
|
|
198
|
-
if (schema.minItems !== void 0) result += `.min(${schema.minItems})`;
|
|
199
|
-
if (schema.maxItems !== void 0) result += `.max(${schema.maxItems})`;
|
|
200
|
-
return result;
|
|
201
|
-
}
|
|
202
|
-
hbs.registerHelper("zodType", function zodType(schema) {
|
|
203
|
-
if (!schema || typeof schema !== "object") return "z.unknown()";
|
|
204
|
-
if (schema.enum) {
|
|
205
|
-
const vals = schema.enum.map((v) => `"${v}"`).join(", ");
|
|
206
|
-
return `z.enum([${vals}])`;
|
|
207
|
-
}
|
|
208
|
-
if (schema.const !== void 0) {
|
|
209
|
-
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
210
|
-
}
|
|
211
|
-
const type = schema.type;
|
|
212
|
-
if (type === "string") return appendStringValidations("z.string()", schema);
|
|
213
|
-
if (type === "integer") return appendNumberValidations("z.number()", schema, true);
|
|
214
|
-
if (type === "number") return appendNumberValidations("z.number()", schema, false);
|
|
215
|
-
if (type === "boolean") return "z.boolean()";
|
|
216
|
-
if (type === "array") {
|
|
217
|
-
const items = schema.items;
|
|
218
|
-
const inner = items ? zodType(items) : "z.unknown()";
|
|
219
|
-
return appendArrayValidations(`z.array(${inner})`, schema);
|
|
220
|
-
}
|
|
221
|
-
if (type === "object") {
|
|
222
|
-
const props = schema.properties;
|
|
223
|
-
if (!props) return "z.record(z.string(), z.unknown())";
|
|
224
|
-
const required = schema.required ?? [];
|
|
225
|
-
const fields = Object.entries(props).map(([key, val]) => {
|
|
226
|
-
const base = zodType(val);
|
|
227
|
-
const opt = required.includes(key) ? "" : ".optional()";
|
|
228
|
-
const safeKey = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : `"${key}"`;
|
|
229
|
-
return ` ${safeKey}: ${base}${opt},`;
|
|
230
|
-
}).join("\n");
|
|
231
|
-
return `z.object({
|
|
232
|
-
${fields}
|
|
233
|
-
})`;
|
|
234
|
-
}
|
|
235
|
-
if (schema.allOf) {
|
|
236
|
-
const parts = schema.allOf.map(zodType);
|
|
237
|
-
if (parts.length === 1) return parts[0];
|
|
238
|
-
return parts.reduce((acc, part) => `${acc}.extend(${part}.shape)`);
|
|
239
|
-
}
|
|
240
|
-
return "z.unknown()";
|
|
241
|
-
});
|
|
242
|
-
hbs.registerHelper("regexEscape", (str) => {
|
|
243
|
-
if (typeof str !== "string") return str;
|
|
244
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
245
|
-
});
|
|
246
|
-
hbs.registerHelper("stringArray", (arr) => {
|
|
247
|
-
if (!Array.isArray(arr)) return "[]";
|
|
248
|
-
return "[" + arr.map((s) => `"${s}"`).join(", ") + "]";
|
|
249
|
-
});
|
|
250
|
-
async function loadTemplate(name, customDir) {
|
|
251
|
-
if (customDir) {
|
|
252
|
-
try {
|
|
253
|
-
return await readFile2(join2(customDir, name), "utf8");
|
|
254
|
-
} catch {
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return readFile2(join2(BUILTIN_TEMPLATES_DIR, name), "utf8");
|
|
258
|
-
}
|
|
259
|
-
async function renderTemplate(name, context, customDir) {
|
|
260
|
-
const source = await loadTemplate(name, customDir);
|
|
261
|
-
const compiled = hbs.compile(source, { noEscape: true });
|
|
262
|
-
return compiled(context);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// src/generator/context.ts
|
|
266
|
-
function isTaskOptional(taskId, workflows) {
|
|
267
|
-
for (const wf of Object.values(workflows)) {
|
|
268
|
-
for (const step of wf.steps) {
|
|
269
|
-
if (step.type === "delegate" && step.task === taskId && step.optional) {
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return false;
|
|
275
|
-
}
|
|
276
|
-
function buildContractContext(dsl) {
|
|
277
|
-
const agents = dsl.agents ?? {};
|
|
278
|
-
const tasks = dsl.tasks ?? {};
|
|
279
|
-
const workflows = dsl.workflow ?? {};
|
|
280
|
-
const handoffTypes = dsl.handoff_types ?? {};
|
|
281
|
-
const agentEntries = Object.entries(agents).map(([id, agent]) => ({
|
|
282
|
-
id,
|
|
283
|
-
varName: toCamelCase(id),
|
|
284
|
-
role_name: agent.role_name ?? id,
|
|
285
|
-
purpose: agent.purpose ?? "",
|
|
286
|
-
mode: agent.mode ?? "read-write",
|
|
287
|
-
dispatch_only: agent.dispatch_only ?? false,
|
|
288
|
-
can_read_artifacts: agent.can_read_artifacts ?? [],
|
|
289
|
-
can_write_artifacts: agent.can_write_artifacts ?? [],
|
|
290
|
-
can_execute_tools: agent.can_execute_tools ?? [],
|
|
291
|
-
can_invoke_agents: agent.can_invoke_agents ?? [],
|
|
292
|
-
can_return_handoffs: agent.can_return_handoffs ?? [],
|
|
293
|
-
responsibilities: agent.responsibilities ?? [],
|
|
294
|
-
constraints: agent.constraints ?? [],
|
|
295
|
-
rules: agent.rules ?? [],
|
|
296
|
-
escalation_criteria: agent.escalation_criteria ?? []
|
|
297
|
-
}));
|
|
298
|
-
const taskEntries = Object.entries(tasks).map(([id, task]) => ({
|
|
299
|
-
id,
|
|
300
|
-
varName: toCamelCase(id),
|
|
301
|
-
description: task.description ?? "",
|
|
302
|
-
target_agent: task.target_agent ?? "",
|
|
303
|
-
allowed_from_agents: task.allowed_from_agents ?? [],
|
|
304
|
-
workflow: task.workflow ?? "",
|
|
305
|
-
invocation_handoff: task.invocation_handoff ?? "",
|
|
306
|
-
result_handoff: task.result_handoff ?? "",
|
|
307
|
-
input_artifacts: task.input_artifacts ?? [],
|
|
308
|
-
responsibilities: task.responsibilities ?? [],
|
|
309
|
-
completion_criteria: task.completion_criteria ?? [],
|
|
310
|
-
optional: isTaskOptional(id, workflows),
|
|
311
|
-
...task.model_class ? { model_class: task.model_class } : {}
|
|
312
|
-
}));
|
|
313
|
-
const workflowEntries = Object.entries(workflows).map(
|
|
314
|
-
([id, wf]) => {
|
|
315
|
-
const steps = (wf.steps ?? []).map((step) => {
|
|
316
|
-
const entry = { type: step.type };
|
|
317
|
-
if (step.type === "delegate") {
|
|
318
|
-
entry.task = step.task;
|
|
319
|
-
entry.from_agent = step.from_agent;
|
|
320
|
-
entry.description = step.description ?? "";
|
|
321
|
-
const wfStep = step;
|
|
322
|
-
entry.optional = isTaskOptional(
|
|
323
|
-
step.task,
|
|
324
|
-
workflows
|
|
325
|
-
);
|
|
326
|
-
entry.max_retries = wfStep.max_retries ?? (wfStep.retry ? 1 : 0);
|
|
327
|
-
entry.max_follow_ups = wfStep.max_follow_ups;
|
|
328
|
-
if (wfStep.depends_on !== void 0) {
|
|
329
|
-
entry.depends_on = wfStep.depends_on;
|
|
330
|
-
}
|
|
331
|
-
if (wfStep.retry) {
|
|
332
|
-
const r = wfStep.retry;
|
|
333
|
-
entry.retry = {
|
|
334
|
-
condition: r.condition,
|
|
335
|
-
fix_task: r.fix_task,
|
|
336
|
-
revalidate_task: r.revalidate_task
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
} else if (step.type === "gate") {
|
|
340
|
-
entry.gate_kind = step.gate_kind;
|
|
341
|
-
entry.description = step.description ?? "";
|
|
342
|
-
if (step.depends_on !== void 0) {
|
|
343
|
-
entry.depends_on = step.depends_on;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return entry;
|
|
347
|
-
});
|
|
348
|
-
return {
|
|
349
|
-
id,
|
|
350
|
-
varName: toCamelCase(id),
|
|
351
|
-
description: wf.description ?? "",
|
|
352
|
-
trigger: wf.trigger ?? "",
|
|
353
|
-
entry_conditions: wf.entry_conditions ?? [],
|
|
354
|
-
steps
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
);
|
|
358
|
-
const handoffEntries = Object.entries(handoffTypes).map(
|
|
359
|
-
([id, ht]) => ({
|
|
360
|
-
id,
|
|
361
|
-
varName: toCamelCase(id),
|
|
362
|
-
pascalName: toPascalCase(id),
|
|
363
|
-
schemaName: toPascalCase(id) + "Schema",
|
|
364
|
-
description: ht.description ?? "",
|
|
365
|
-
schema: ht.schema ?? {}
|
|
366
|
-
})
|
|
367
|
-
);
|
|
368
|
-
return { agentEntries, taskEntries, workflowEntries, handoffEntries };
|
|
369
|
-
}
|
|
370
|
-
function buildGuardrailContext(dsl, binding, policyName) {
|
|
371
|
-
const empty = {
|
|
372
|
-
commandChecks: [],
|
|
373
|
-
fileChecks: [],
|
|
374
|
-
contentChecks: [],
|
|
375
|
-
allChecks: [],
|
|
376
|
-
hasGuardrails: false
|
|
377
|
-
};
|
|
378
|
-
if (!binding?.guardrail_impl) return empty;
|
|
379
|
-
const guardrails = dsl.guardrails ?? {};
|
|
380
|
-
const policies = dsl.guardrail_policies ?? {};
|
|
381
|
-
const policy = policyName ? policies[policyName] : void 0;
|
|
382
|
-
const allChecks = [];
|
|
383
|
-
for (const [guardrailId, impl] of Object.entries(
|
|
384
|
-
binding.guardrail_impl
|
|
385
|
-
)) {
|
|
386
|
-
const guardrail = guardrails[guardrailId];
|
|
387
|
-
const description = guardrail?.description ?? guardrailId;
|
|
388
|
-
const policyRule = policy?.rules?.find(
|
|
389
|
-
(r) => r.guardrail === guardrailId
|
|
390
|
-
);
|
|
391
|
-
const severity = policyRule?.severity ?? "warning";
|
|
392
|
-
const action = policyRule?.action ?? "warn";
|
|
393
|
-
for (const check of impl.checks) {
|
|
394
|
-
const matcher = check.matcher;
|
|
395
|
-
if (!matcher) continue;
|
|
396
|
-
allChecks.push({
|
|
397
|
-
guardrail_id: guardrailId,
|
|
398
|
-
description,
|
|
399
|
-
severity,
|
|
400
|
-
action,
|
|
401
|
-
matcher_type: matcher.type,
|
|
402
|
-
pattern: matcher.pattern,
|
|
403
|
-
message: check.message ?? description,
|
|
404
|
-
file_glob: matcher.file_glob,
|
|
405
|
-
exclude_glob: matcher.exclude_glob
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return {
|
|
410
|
-
commandChecks: allChecks.filter((c) => c.matcher_type === "command_regex"),
|
|
411
|
-
fileChecks: allChecks.filter((c) => c.matcher_type === "file_glob"),
|
|
412
|
-
contentChecks: allChecks.filter((c) => c.matcher_type === "content_regex"),
|
|
413
|
-
allChecks,
|
|
414
|
-
hasGuardrails: allChecks.length > 0
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// src/generator/index.ts
|
|
419
|
-
var DO_NOT_EDIT_HEADER = `/**
|
|
420
|
-
* AUTO-GENERATED by agent-contracts-runtime. DO NOT EDIT.
|
|
421
|
-
*
|
|
422
|
-
* Regenerate via: npx agent-runtime generate
|
|
423
|
-
*/
|
|
424
|
-
|
|
425
|
-
`;
|
|
426
|
-
async function loadYaml(filePath) {
|
|
427
|
-
const raw = await readFile3(filePath, "utf8");
|
|
428
|
-
return YAML2.parse(raw);
|
|
429
|
-
}
|
|
430
|
-
function computeHash(data) {
|
|
431
|
-
return createHash("sha256").update(data).digest("hex");
|
|
432
|
-
}
|
|
433
|
-
function sortDeep(value) {
|
|
434
|
-
if (Array.isArray(value)) return value.map(sortDeep);
|
|
435
|
-
if (value !== null && typeof value === "object") {
|
|
436
|
-
return Object.fromEntries(
|
|
437
|
-
Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => [k, sortDeep(v)])
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
return value;
|
|
441
|
-
}
|
|
442
|
-
function canonicalStringify(obj) {
|
|
443
|
-
return JSON.stringify(sortDeep(obj));
|
|
444
|
-
}
|
|
445
|
-
async function loadDsl(dslPath) {
|
|
446
|
-
const { resolve: resolveDsl } = await import("agent-contracts");
|
|
447
|
-
const result = await resolveDsl(dslPath);
|
|
448
|
-
return result.data;
|
|
449
|
-
}
|
|
450
|
-
var CONTRACT_TEMPLATES = [
|
|
451
|
-
{ template: "agents.ts.hbs", output: "agents.ts" },
|
|
452
|
-
{ template: "tasks.ts.hbs", output: "tasks.ts" },
|
|
453
|
-
{ template: "workflows.ts.hbs", output: "workflows.ts" },
|
|
454
|
-
{ template: "handoffs.ts.hbs", output: "handoffs.ts" },
|
|
455
|
-
{ template: "contracts-index.ts.hbs", output: "index.ts" }
|
|
456
|
-
];
|
|
457
|
-
async function generate(options = {}) {
|
|
458
|
-
const configPath = options.configPath ?? "./agents.conf.yaml";
|
|
459
|
-
const config = await loadConfig(configPath);
|
|
460
|
-
const templatesDir = options.templates ?? config.templatesDir;
|
|
461
|
-
const checkMode = options.check ?? false;
|
|
462
|
-
const cleaned = options.clean ?? false;
|
|
463
|
-
if (cleaned && !checkMode) {
|
|
464
|
-
try {
|
|
465
|
-
await rm(config.generatedDir, { recursive: true, force: true });
|
|
466
|
-
} catch {
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
const dsl = await loadDsl(config.dslPath);
|
|
470
|
-
const dslCanonical = canonicalStringify(dsl);
|
|
471
|
-
const dslHash = computeHash(dslCanonical);
|
|
472
|
-
const contractCtx = buildContractContext(dsl);
|
|
473
|
-
const files_generated = [];
|
|
474
|
-
const generatedContents = /* @__PURE__ */ new Map();
|
|
475
|
-
for (const { template, output } of CONTRACT_TEMPLATES) {
|
|
476
|
-
const rendered = DO_NOT_EDIT_HEADER + await renderTemplate(template, contractCtx, templatesDir);
|
|
477
|
-
const outPath = join3(config.generatedDir, output);
|
|
478
|
-
files_generated.push(relative(config.configDir, outPath));
|
|
479
|
-
generatedContents.set(outPath, rendered);
|
|
480
|
-
}
|
|
481
|
-
let guardrailRules;
|
|
482
|
-
if (config.bindingPaths.length > 0) {
|
|
483
|
-
for (const bindingPath of config.bindingPaths) {
|
|
484
|
-
const binding = await loadYaml(bindingPath);
|
|
485
|
-
const guardrailCtx = buildGuardrailContext(dsl, binding, config.active_guardrail_policy);
|
|
486
|
-
if (guardrailCtx.hasGuardrails) {
|
|
487
|
-
guardrailRules = {
|
|
488
|
-
commandRules: guardrailCtx.commandChecks.map((c) => ({
|
|
489
|
-
guardrail_id: c.guardrail_id,
|
|
490
|
-
pattern: c.pattern,
|
|
491
|
-
action: c.action,
|
|
492
|
-
message: c.message
|
|
493
|
-
})),
|
|
494
|
-
fileRules: guardrailCtx.fileChecks.map((c) => ({
|
|
495
|
-
guardrail_id: c.guardrail_id,
|
|
496
|
-
pattern: c.pattern,
|
|
497
|
-
action: c.action,
|
|
498
|
-
message: c.message,
|
|
499
|
-
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
500
|
-
})),
|
|
501
|
-
contentRules: guardrailCtx.contentChecks.map((c) => ({
|
|
502
|
-
guardrail_id: c.guardrail_id,
|
|
503
|
-
pattern: c.pattern,
|
|
504
|
-
action: c.action,
|
|
505
|
-
message: c.message,
|
|
506
|
-
...c.file_glob ? { file_glob: c.file_glob } : {},
|
|
507
|
-
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
508
|
-
}))
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
{
|
|
514
|
-
const dslWithRules = guardrailRules ? { ...dsl, _guardrailRules: guardrailRules } : dsl;
|
|
515
|
-
const dslDataContent = DO_NOT_EDIT_HEADER + `export const resolvedDsl: Record<string, unknown> = ${JSON.stringify(dslWithRules, null, 2)} as const;
|
|
516
|
-
`;
|
|
517
|
-
const outPath = join3(config.generatedDir, "dsl-data.ts");
|
|
518
|
-
files_generated.push(relative(config.configDir, outPath));
|
|
519
|
-
generatedContents.set(outPath, dslDataContent);
|
|
520
|
-
}
|
|
521
|
-
const generationInputParts = [dslCanonical];
|
|
522
|
-
for (const bp of config.bindingPaths) {
|
|
523
|
-
try {
|
|
524
|
-
generationInputParts.push(await readFile3(bp, "utf8"));
|
|
525
|
-
} catch {
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
const generation_input_hash = computeHash(generationInputParts.join("\n---\n"));
|
|
529
|
-
let has_differences = false;
|
|
530
|
-
if (checkMode) {
|
|
531
|
-
for (const [outPath, content] of generatedContents) {
|
|
532
|
-
try {
|
|
533
|
-
const existing = await readFile3(outPath, "utf8");
|
|
534
|
-
if (existing !== content) {
|
|
535
|
-
has_differences = true;
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
} catch {
|
|
539
|
-
has_differences = true;
|
|
540
|
-
break;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
} else {
|
|
544
|
-
for (const [outPath, content] of generatedContents) {
|
|
545
|
-
const dir = resolve3(outPath, "..");
|
|
546
|
-
await mkdir(dir, { recursive: true });
|
|
547
|
-
await writeFile(outPath, content, "utf8");
|
|
548
|
-
}
|
|
549
|
-
const manifest = {
|
|
550
|
-
generated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
551
|
-
dsl_hash: dslHash,
|
|
552
|
-
generation_input_hash,
|
|
553
|
-
files: files_generated
|
|
554
|
-
};
|
|
555
|
-
const manifestPath = join3(config.generatedDir, ".manifest.json");
|
|
556
|
-
await mkdir(resolve3(manifestPath, ".."), { recursive: true });
|
|
557
|
-
await writeFile(manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8");
|
|
558
|
-
}
|
|
559
|
-
return {
|
|
560
|
-
files_generated,
|
|
561
|
-
dsl_hash: dslHash,
|
|
562
|
-
generation_input_hash,
|
|
563
|
-
cleaned,
|
|
564
|
-
check_mode: checkMode,
|
|
565
|
-
has_differences
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
async function checkFreshness(configPath) {
|
|
569
|
-
const config = await loadConfig(configPath ?? "./agents.conf.yaml");
|
|
570
|
-
const manifestPath = join3(config.generatedDir, ".manifest.json");
|
|
571
|
-
try {
|
|
572
|
-
const raw = await readFile3(manifestPath, "utf8");
|
|
573
|
-
const manifest = JSON.parse(raw);
|
|
574
|
-
const dsl = await loadDsl(config.dslPath);
|
|
575
|
-
const dslCanonical = canonicalStringify(dsl);
|
|
576
|
-
const currentHash = computeHash(dslCanonical);
|
|
577
|
-
return manifest.dsl_hash === currentHash;
|
|
578
|
-
} catch {
|
|
579
|
-
return false;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
export {
|
|
584
|
-
DEFAULT_CONFIG_PATH,
|
|
585
|
-
ProjectConfigSchema,
|
|
586
|
-
loadConfig,
|
|
587
|
-
loadProjectConfig,
|
|
588
|
-
buildContractContext,
|
|
589
|
-
buildGuardrailContext,
|
|
590
|
-
generate,
|
|
591
|
-
checkFreshness
|
|
592
|
-
};
|
|
593
|
-
//# sourceMappingURL=chunk-KQOZCJK6.js.map
|