agent-contracts-runtime 0.36.5 → 0.36.7
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 +123 -38
- 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/claude-agent-sdk.js +6 -3
- package/dist/adapters/claude-agent-sdk.js.map +1 -1
- package/dist/adapters/mock.d.ts +1 -1
- package/dist/adapters/openai-agents-sdk.d.ts +2 -2
- package/dist/agent-runtime.bundle.mjs +269 -269
- package/dist/agent-runtime.bundle.mjs.map +4 -4
- package/dist/{chunk-ADNNYJAU.js → chunk-DPJFPYJO.js} +383 -174
- package/dist/chunk-DPJFPYJO.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-PEHJ3J5C.js +307 -0
- package/dist/chunk-PEHJ3J5C.js.map +1 -0
- package/dist/cli/index.js +4 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/context-B_bekYiE.d.ts +106 -0
- package/dist/generator/index.d.ts +2 -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 +15 -2
- package/dist/{task-runner-Trjgd--b.d.ts → task-runner-Bo37lS9q.d.ts} +17 -0
- package/package.json +1 -1
- package/dist/chunk-ADNNYJAU.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
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createGuardrailHooksFromRules,
|
|
3
3
|
pluginRegistry
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-MGRZBGQL.js";
|
|
5
|
+
import {
|
|
6
|
+
buildGuardrailContext
|
|
7
|
+
} from "./chunk-LTZA6QWC.js";
|
|
5
8
|
|
|
6
9
|
// src/lib/task-runner.ts
|
|
7
10
|
import { z } from "zod";
|
|
@@ -53,10 +56,12 @@ function buildCandidateAgents(agentRegistry, opts) {
|
|
|
53
56
|
for (const [id, agent] of Object.entries(agentRegistry)) {
|
|
54
57
|
if (id === entryId) continue;
|
|
55
58
|
if (allowSet && !allowSet.has(id)) continue;
|
|
59
|
+
const tools = agent.can_execute_tools;
|
|
56
60
|
candidates.push({
|
|
57
61
|
name: id,
|
|
58
62
|
description: deriveDescription(agent.purpose),
|
|
59
|
-
prompt: renderAgentSystemPrompt(agent)
|
|
63
|
+
prompt: renderAgentSystemPrompt(agent),
|
|
64
|
+
...tools && tools.length > 0 ? { tools: [...tools] } : {}
|
|
60
65
|
});
|
|
61
66
|
}
|
|
62
67
|
return candidates;
|
|
@@ -524,6 +529,7 @@ async function runTask(adapter, taskId, context, options) {
|
|
|
524
529
|
prompt = enhancedPrompt;
|
|
525
530
|
}
|
|
526
531
|
const isReadonly = agentContract.mode === "read-only";
|
|
532
|
+
const allowDynamicWorkflow = task.allow_dynamic_workflow ?? false;
|
|
527
533
|
const candidateAgents = Object.keys(agentReg).length > 0 ? buildCandidateAgents(agentReg, {
|
|
528
534
|
entryAgentId: agentContract.id,
|
|
529
535
|
allowedAgentIds: agentContract.can_invoke_agents
|
|
@@ -562,10 +568,10 @@ async function runTask(adapter, taskId, context, options) {
|
|
|
562
568
|
memoryRef,
|
|
563
569
|
splitPrompt,
|
|
564
570
|
agents,
|
|
565
|
-
options: { readonly: isReadonly, onProgress, splitPrompt, agents, maxFollowUps, attempt: 0 }
|
|
571
|
+
options: { readonly: isReadonly, onProgress, splitPrompt, agents, allowDynamicWorkflow, maxFollowUps, attempt: 0 }
|
|
566
572
|
});
|
|
567
573
|
} else {
|
|
568
|
-
fullOutput = await adapter.send(prompt, { readonly: isReadonly, onProgress, splitPrompt, agents });
|
|
574
|
+
fullOutput = await adapter.send(prompt, { readonly: isReadonly, onProgress, splitPrompt, agents, allowDynamicWorkflow });
|
|
569
575
|
}
|
|
570
576
|
} catch (err) {
|
|
571
577
|
return { outcome: { status: "error", message: `SDK error: ${err.message}` }, follow_ups_used: 0, retries_used: 0 };
|
|
@@ -967,152 +973,9 @@ function emitStepEvent(options, workflowId, stepIndex, step, result) {
|
|
|
967
973
|
});
|
|
968
974
|
}
|
|
969
975
|
|
|
970
|
-
// src/lib/
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
workflow(workflowId) {
|
|
974
|
-
return new WorkflowBuilder(config.adapter, workflowId, config.registries);
|
|
975
|
-
}
|
|
976
|
-
};
|
|
977
|
-
}
|
|
978
|
-
var WorkflowBuilder = class {
|
|
979
|
-
adapter;
|
|
980
|
-
invocation;
|
|
981
|
-
registries;
|
|
982
|
-
constructor(adapter, workflowId, registries) {
|
|
983
|
-
this.adapter = adapter;
|
|
984
|
-
this.invocation = { workflow: workflowId };
|
|
985
|
-
this.registries = registries;
|
|
986
|
-
}
|
|
987
|
-
/** Set a typed handoff envelope as input. */
|
|
988
|
-
handoff(input) {
|
|
989
|
-
this.invocation = { ...this.invocation, handoff: input };
|
|
990
|
-
return this;
|
|
991
|
-
}
|
|
992
|
-
/** Set a simple string request (convenience shorthand). */
|
|
993
|
-
request(userRequest) {
|
|
994
|
-
this.invocation = { ...this.invocation, user_request: userRequest };
|
|
995
|
-
return this;
|
|
996
|
-
}
|
|
997
|
-
maxFollowUps(n) {
|
|
998
|
-
this.invocation = {
|
|
999
|
-
...this.invocation,
|
|
1000
|
-
runtime: { ...this.invocation.runtime, maxFollowUps: n }
|
|
1001
|
-
};
|
|
1002
|
-
return this;
|
|
1003
|
-
}
|
|
1004
|
-
maxRetries(n) {
|
|
1005
|
-
this.invocation = {
|
|
1006
|
-
...this.invocation,
|
|
1007
|
-
runtime: { ...this.invocation.runtime, maxRetries: n }
|
|
1008
|
-
};
|
|
1009
|
-
return this;
|
|
1010
|
-
}
|
|
1011
|
-
onStepComplete(cb) {
|
|
1012
|
-
this.invocation = {
|
|
1013
|
-
...this.invocation,
|
|
1014
|
-
hooks: { ...this.invocation.hooks, onStepComplete: cb }
|
|
1015
|
-
};
|
|
1016
|
-
return this;
|
|
1017
|
-
}
|
|
1018
|
-
onGate(cb) {
|
|
1019
|
-
this.invocation = {
|
|
1020
|
-
...this.invocation,
|
|
1021
|
-
hooks: { ...this.invocation.hooks, onGate: cb }
|
|
1022
|
-
};
|
|
1023
|
-
return this;
|
|
1024
|
-
}
|
|
1025
|
-
onOptionalStep(cb) {
|
|
1026
|
-
this.invocation = {
|
|
1027
|
-
...this.invocation,
|
|
1028
|
-
hooks: { ...this.invocation.hooks, onOptionalStep: cb }
|
|
1029
|
-
};
|
|
1030
|
-
return this;
|
|
1031
|
-
}
|
|
1032
|
-
/** Set progress output sink for real-time execution visibility. */
|
|
1033
|
-
progressOutput(sink) {
|
|
1034
|
-
this.invocation = { ...this.invocation, progressOutput: sink };
|
|
1035
|
-
return this;
|
|
1036
|
-
}
|
|
1037
|
-
async run() {
|
|
1038
|
-
return runWorkflow(this.adapter, this.invocation, this.registries);
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
|
|
1042
|
-
// src/lib/plugin-loader.ts
|
|
1043
|
-
import { existsSync } from "fs";
|
|
1044
|
-
import { readdir } from "fs/promises";
|
|
1045
|
-
import { resolve, join } from "path";
|
|
1046
|
-
var PLUGIN_EXTENSIONS = [".ts", ".js", ".mts", ".mjs"];
|
|
1047
|
-
function isPluginLike(obj) {
|
|
1048
|
-
return typeof obj === "object" && obj !== null && typeof obj.id === "string";
|
|
1049
|
-
}
|
|
1050
|
-
async function scanDirectory(dir) {
|
|
1051
|
-
if (!existsSync(dir)) return [];
|
|
1052
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
1053
|
-
return entries.filter(
|
|
1054
|
-
(e) => e.isFile() && PLUGIN_EXTENSIONS.some((ext) => e.name.endsWith(ext)) && !e.name.startsWith("_") && !e.name.endsWith(".d.ts") && !e.name.endsWith(".test.ts") && !e.name.endsWith(".spec.ts")
|
|
1055
|
-
).map((e) => resolve(dir, e.name));
|
|
1056
|
-
}
|
|
1057
|
-
async function loadPluginModule(modulePath) {
|
|
1058
|
-
const mod = await import(new URL(`file://${modulePath}`).href);
|
|
1059
|
-
let count = 0;
|
|
1060
|
-
for (const exportValue of Object.values(mod)) {
|
|
1061
|
-
if (isPluginLike(exportValue)) {
|
|
1062
|
-
try {
|
|
1063
|
-
pluginRegistry.register(exportValue);
|
|
1064
|
-
count++;
|
|
1065
|
-
} catch {
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
return count;
|
|
1070
|
-
}
|
|
1071
|
-
async function loadPluginsFromPaths(paths, baseDir) {
|
|
1072
|
-
const result = { loaded: 0, files: [], errors: [] };
|
|
1073
|
-
for (const p of paths) {
|
|
1074
|
-
const absPath = resolve(baseDir, p);
|
|
1075
|
-
if (!existsSync(absPath)) {
|
|
1076
|
-
result.errors.push({ file: p, error: `File not found: ${absPath}` });
|
|
1077
|
-
continue;
|
|
1078
|
-
}
|
|
1079
|
-
try {
|
|
1080
|
-
const count = await loadPluginModule(absPath);
|
|
1081
|
-
result.loaded += count;
|
|
1082
|
-
result.files.push(absPath);
|
|
1083
|
-
} catch (err) {
|
|
1084
|
-
result.errors.push({
|
|
1085
|
-
file: p,
|
|
1086
|
-
error: err.message
|
|
1087
|
-
});
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
return result;
|
|
1091
|
-
}
|
|
1092
|
-
async function loadPluginsFromDirectory(dir) {
|
|
1093
|
-
const result = { loaded: 0, files: [], errors: [] };
|
|
1094
|
-
const candidates = await scanDirectory(dir);
|
|
1095
|
-
for (const filePath of candidates) {
|
|
1096
|
-
try {
|
|
1097
|
-
const count = await loadPluginModule(filePath);
|
|
1098
|
-
result.loaded += count;
|
|
1099
|
-
result.files.push(filePath);
|
|
1100
|
-
} catch (err) {
|
|
1101
|
-
result.errors.push({
|
|
1102
|
-
file: filePath,
|
|
1103
|
-
error: err.message
|
|
1104
|
-
});
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
return result;
|
|
1108
|
-
}
|
|
1109
|
-
async function loadPlugins(options) {
|
|
1110
|
-
if (options.pluginPaths && options.pluginPaths.length > 0) {
|
|
1111
|
-
return loadPluginsFromPaths(options.pluginPaths, options.projectDir);
|
|
1112
|
-
}
|
|
1113
|
-
const conventionDir = join(options.projectDir, "src", "plugins");
|
|
1114
|
-
return loadPluginsFromDirectory(conventionDir);
|
|
1115
|
-
}
|
|
976
|
+
// src/lib/bound-resolver.ts
|
|
977
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
978
|
+
import YAML2 from "yaml";
|
|
1116
979
|
|
|
1117
980
|
// src/lib/dsl-context.ts
|
|
1118
981
|
import { readFile } from "fs/promises";
|
|
@@ -1214,7 +1077,9 @@ function buildAgentRegistry(agents) {
|
|
|
1214
1077
|
mode,
|
|
1215
1078
|
responsibilities: agent.responsibilities ?? [],
|
|
1216
1079
|
constraints: agent.constraints ?? [],
|
|
1217
|
-
rules: agent.rules ?? []
|
|
1080
|
+
rules: agent.rules ?? [],
|
|
1081
|
+
can_invoke_agents: agent.can_invoke_agents ?? void 0,
|
|
1082
|
+
can_execute_tools: agent.can_execute_tools ?? void 0
|
|
1218
1083
|
};
|
|
1219
1084
|
}
|
|
1220
1085
|
return registry;
|
|
@@ -1370,6 +1235,307 @@ async function loadDslContext(options) {
|
|
|
1370
1235
|
return { registries, dsl, guardrailRules };
|
|
1371
1236
|
}
|
|
1372
1237
|
|
|
1238
|
+
// src/lib/bound-resolver.ts
|
|
1239
|
+
function getEffectiveGuardrailIds(dsl, agentId) {
|
|
1240
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1241
|
+
const agents = dsl.agents ?? {};
|
|
1242
|
+
const agent = agents[agentId];
|
|
1243
|
+
if (agent) {
|
|
1244
|
+
for (const ref of agent.guardrails ?? []) {
|
|
1245
|
+
ids.add(ref);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
const guardrails = dsl.guardrails ?? {};
|
|
1249
|
+
for (const [guardrailId, guardrail] of Object.entries(guardrails)) {
|
|
1250
|
+
if (guardrail.scope?.agents?.includes(agentId)) {
|
|
1251
|
+
ids.add(guardrailId);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return ids;
|
|
1255
|
+
}
|
|
1256
|
+
function guardrailCheckToCommandRule(c) {
|
|
1257
|
+
return {
|
|
1258
|
+
guardrail_id: c.guardrail_id,
|
|
1259
|
+
pattern: c.pattern,
|
|
1260
|
+
action: c.action,
|
|
1261
|
+
message: c.message
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
function guardrailCheckToFileRule(c) {
|
|
1265
|
+
return {
|
|
1266
|
+
guardrail_id: c.guardrail_id,
|
|
1267
|
+
pattern: c.pattern,
|
|
1268
|
+
action: c.action,
|
|
1269
|
+
message: c.message,
|
|
1270
|
+
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1273
|
+
function guardrailCheckToContentRule(c) {
|
|
1274
|
+
return {
|
|
1275
|
+
guardrail_id: c.guardrail_id,
|
|
1276
|
+
pattern: c.pattern,
|
|
1277
|
+
action: c.action,
|
|
1278
|
+
message: c.message,
|
|
1279
|
+
...c.file_glob ? { file_glob: c.file_glob } : {},
|
|
1280
|
+
...c.exclude_glob ? { exclude_glob: c.exclude_glob } : {}
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
function guardrailContextToRuleData(ctx, effectiveIds) {
|
|
1284
|
+
const filter = (checks) => effectiveIds ? checks.filter((c) => effectiveIds.has(c.guardrail_id)) : checks;
|
|
1285
|
+
return {
|
|
1286
|
+
commandRules: filter(ctx.commandChecks).map(guardrailCheckToCommandRule),
|
|
1287
|
+
fileRules: filter(ctx.fileChecks).map(guardrailCheckToFileRule),
|
|
1288
|
+
contentRules: filter(ctx.contentChecks).map(guardrailCheckToContentRule)
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
function buildPerAgentGuardrails(dsl, guardrailCtx) {
|
|
1292
|
+
const perAgent = /* @__PURE__ */ new Map();
|
|
1293
|
+
const agents = dsl.agents ?? {};
|
|
1294
|
+
for (const agentId of Object.keys(agents)) {
|
|
1295
|
+
const effectiveIds = getEffectiveGuardrailIds(dsl, agentId);
|
|
1296
|
+
const rules = guardrailContextToRuleData(guardrailCtx, effectiveIds);
|
|
1297
|
+
if (rules.commandRules.length > 0 || rules.fileRules.length > 0 || rules.contentRules.length > 0) {
|
|
1298
|
+
perAgent.set(agentId, rules);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
return perAgent;
|
|
1302
|
+
}
|
|
1303
|
+
function hasGuardrailRules(rules) {
|
|
1304
|
+
return rules.commandRules.length > 0 || rules.fileRules.length > 0 || rules.contentRules.length > 0;
|
|
1305
|
+
}
|
|
1306
|
+
var BoundResolver = class {
|
|
1307
|
+
cache = /* @__PURE__ */ new Map();
|
|
1308
|
+
/**
|
|
1309
|
+
* Resolve binding + policy in one pass. Results are cached by component id.
|
|
1310
|
+
*/
|
|
1311
|
+
async resolve(opts) {
|
|
1312
|
+
const cached = this.cache.get(opts.id);
|
|
1313
|
+
if (cached) return cached;
|
|
1314
|
+
const diagnostics = [];
|
|
1315
|
+
let dsl;
|
|
1316
|
+
if (opts.embeddedDsl) {
|
|
1317
|
+
dsl = opts.embeddedDsl;
|
|
1318
|
+
} else if (opts.dslPath) {
|
|
1319
|
+
const { resolve: resolveDsl } = await import("agent-contracts");
|
|
1320
|
+
const result = await resolveDsl(opts.dslPath);
|
|
1321
|
+
dsl = result.data;
|
|
1322
|
+
} else {
|
|
1323
|
+
throw new Error("BoundResolver.resolve requires embeddedDsl or dslPath");
|
|
1324
|
+
}
|
|
1325
|
+
if (opts.artifactBinding) {
|
|
1326
|
+
const { resolveBound } = await import("agent-contracts");
|
|
1327
|
+
const boundResult = await resolveBound(dsl, {
|
|
1328
|
+
artifactBinding: opts.artifactBinding,
|
|
1329
|
+
paths: opts.paths
|
|
1330
|
+
});
|
|
1331
|
+
dsl = boundResult.data;
|
|
1332
|
+
for (const d of boundResult.diagnostics) {
|
|
1333
|
+
diagnostics.push({
|
|
1334
|
+
path: d.rule,
|
|
1335
|
+
message: d.message,
|
|
1336
|
+
severity: d.severity
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
let guardrailCtx = {
|
|
1341
|
+
commandChecks: [],
|
|
1342
|
+
fileChecks: [],
|
|
1343
|
+
contentChecks: [],
|
|
1344
|
+
allChecks: [],
|
|
1345
|
+
hasGuardrails: false
|
|
1346
|
+
};
|
|
1347
|
+
if (opts.bindingPaths && opts.bindingPaths.length > 0) {
|
|
1348
|
+
for (const bindingPath of opts.bindingPaths) {
|
|
1349
|
+
const raw = await readFile2(bindingPath, "utf8");
|
|
1350
|
+
const binding = YAML2.parse(raw);
|
|
1351
|
+
guardrailCtx = buildGuardrailContext(
|
|
1352
|
+
dsl,
|
|
1353
|
+
binding,
|
|
1354
|
+
opts.activeGuardrailPolicy
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const perAgent = guardrailCtx.hasGuardrails ? buildPerAgentGuardrails(dsl, guardrailCtx) : /* @__PURE__ */ new Map();
|
|
1359
|
+
const component = {
|
|
1360
|
+
id: opts.id,
|
|
1361
|
+
dsl,
|
|
1362
|
+
registries: buildRegistriesFromDsl(dsl),
|
|
1363
|
+
guardrails: { perAgent },
|
|
1364
|
+
diagnostics
|
|
1365
|
+
};
|
|
1366
|
+
this.cache.set(opts.id, component);
|
|
1367
|
+
return component;
|
|
1368
|
+
}
|
|
1369
|
+
/** Clear cached components (e.g. for testing). */
|
|
1370
|
+
clearCache() {
|
|
1371
|
+
this.cache.clear();
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
function getGuardrailRulesForAgent(component, agentId, fallbackDsl) {
|
|
1375
|
+
const perAgent = component.guardrails.perAgent.get(agentId);
|
|
1376
|
+
if (perAgent && hasGuardrailRules(perAgent)) {
|
|
1377
|
+
return perAgent;
|
|
1378
|
+
}
|
|
1379
|
+
if (fallbackDsl) {
|
|
1380
|
+
const raw = fallbackDsl._guardrailRules;
|
|
1381
|
+
if (raw) {
|
|
1382
|
+
return {
|
|
1383
|
+
commandRules: raw.commandRules ?? [],
|
|
1384
|
+
fileRules: raw.fileRules ?? [],
|
|
1385
|
+
contentRules: raw.contentRules ?? []
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
return void 0;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// src/lib/builder.ts
|
|
1393
|
+
function createRuntime(config) {
|
|
1394
|
+
return {
|
|
1395
|
+
workflow(workflowId) {
|
|
1396
|
+
return new WorkflowBuilder(config.adapter, workflowId, config.registries);
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
var WorkflowBuilder = class {
|
|
1401
|
+
adapter;
|
|
1402
|
+
invocation;
|
|
1403
|
+
registries;
|
|
1404
|
+
constructor(adapter, workflowId, registries) {
|
|
1405
|
+
this.adapter = adapter;
|
|
1406
|
+
this.invocation = { workflow: workflowId };
|
|
1407
|
+
this.registries = registries;
|
|
1408
|
+
}
|
|
1409
|
+
/** Set a typed handoff envelope as input. */
|
|
1410
|
+
handoff(input) {
|
|
1411
|
+
this.invocation = { ...this.invocation, handoff: input };
|
|
1412
|
+
return this;
|
|
1413
|
+
}
|
|
1414
|
+
/** Set a simple string request (convenience shorthand). */
|
|
1415
|
+
request(userRequest) {
|
|
1416
|
+
this.invocation = { ...this.invocation, user_request: userRequest };
|
|
1417
|
+
return this;
|
|
1418
|
+
}
|
|
1419
|
+
maxFollowUps(n) {
|
|
1420
|
+
this.invocation = {
|
|
1421
|
+
...this.invocation,
|
|
1422
|
+
runtime: { ...this.invocation.runtime, maxFollowUps: n }
|
|
1423
|
+
};
|
|
1424
|
+
return this;
|
|
1425
|
+
}
|
|
1426
|
+
maxRetries(n) {
|
|
1427
|
+
this.invocation = {
|
|
1428
|
+
...this.invocation,
|
|
1429
|
+
runtime: { ...this.invocation.runtime, maxRetries: n }
|
|
1430
|
+
};
|
|
1431
|
+
return this;
|
|
1432
|
+
}
|
|
1433
|
+
onStepComplete(cb) {
|
|
1434
|
+
this.invocation = {
|
|
1435
|
+
...this.invocation,
|
|
1436
|
+
hooks: { ...this.invocation.hooks, onStepComplete: cb }
|
|
1437
|
+
};
|
|
1438
|
+
return this;
|
|
1439
|
+
}
|
|
1440
|
+
onGate(cb) {
|
|
1441
|
+
this.invocation = {
|
|
1442
|
+
...this.invocation,
|
|
1443
|
+
hooks: { ...this.invocation.hooks, onGate: cb }
|
|
1444
|
+
};
|
|
1445
|
+
return this;
|
|
1446
|
+
}
|
|
1447
|
+
onOptionalStep(cb) {
|
|
1448
|
+
this.invocation = {
|
|
1449
|
+
...this.invocation,
|
|
1450
|
+
hooks: { ...this.invocation.hooks, onOptionalStep: cb }
|
|
1451
|
+
};
|
|
1452
|
+
return this;
|
|
1453
|
+
}
|
|
1454
|
+
/** Set progress output sink for real-time execution visibility. */
|
|
1455
|
+
progressOutput(sink) {
|
|
1456
|
+
this.invocation = { ...this.invocation, progressOutput: sink };
|
|
1457
|
+
return this;
|
|
1458
|
+
}
|
|
1459
|
+
async run() {
|
|
1460
|
+
return runWorkflow(this.adapter, this.invocation, this.registries);
|
|
1461
|
+
}
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
// src/lib/plugin-loader.ts
|
|
1465
|
+
import { existsSync } from "fs";
|
|
1466
|
+
import { readdir } from "fs/promises";
|
|
1467
|
+
import { resolve, join } from "path";
|
|
1468
|
+
var PLUGIN_EXTENSIONS = [".ts", ".js", ".mts", ".mjs"];
|
|
1469
|
+
function isPluginLike(obj) {
|
|
1470
|
+
return typeof obj === "object" && obj !== null && typeof obj.id === "string";
|
|
1471
|
+
}
|
|
1472
|
+
async function scanDirectory(dir) {
|
|
1473
|
+
if (!existsSync(dir)) return [];
|
|
1474
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
1475
|
+
return entries.filter(
|
|
1476
|
+
(e) => e.isFile() && PLUGIN_EXTENSIONS.some((ext) => e.name.endsWith(ext)) && !e.name.startsWith("_") && !e.name.endsWith(".d.ts") && !e.name.endsWith(".test.ts") && !e.name.endsWith(".spec.ts")
|
|
1477
|
+
).map((e) => resolve(dir, e.name));
|
|
1478
|
+
}
|
|
1479
|
+
async function loadPluginModule(modulePath) {
|
|
1480
|
+
const mod = await import(new URL(`file://${modulePath}`).href);
|
|
1481
|
+
let count = 0;
|
|
1482
|
+
for (const exportValue of Object.values(mod)) {
|
|
1483
|
+
if (isPluginLike(exportValue)) {
|
|
1484
|
+
try {
|
|
1485
|
+
pluginRegistry.register(exportValue);
|
|
1486
|
+
count++;
|
|
1487
|
+
} catch {
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
return count;
|
|
1492
|
+
}
|
|
1493
|
+
async function loadPluginsFromPaths(paths, baseDir) {
|
|
1494
|
+
const result = { loaded: 0, files: [], errors: [] };
|
|
1495
|
+
for (const p of paths) {
|
|
1496
|
+
const absPath = resolve(baseDir, p);
|
|
1497
|
+
if (!existsSync(absPath)) {
|
|
1498
|
+
result.errors.push({ file: p, error: `File not found: ${absPath}` });
|
|
1499
|
+
continue;
|
|
1500
|
+
}
|
|
1501
|
+
try {
|
|
1502
|
+
const count = await loadPluginModule(absPath);
|
|
1503
|
+
result.loaded += count;
|
|
1504
|
+
result.files.push(absPath);
|
|
1505
|
+
} catch (err) {
|
|
1506
|
+
result.errors.push({
|
|
1507
|
+
file: p,
|
|
1508
|
+
error: err.message
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
return result;
|
|
1513
|
+
}
|
|
1514
|
+
async function loadPluginsFromDirectory(dir) {
|
|
1515
|
+
const result = { loaded: 0, files: [], errors: [] };
|
|
1516
|
+
const candidates = await scanDirectory(dir);
|
|
1517
|
+
for (const filePath of candidates) {
|
|
1518
|
+
try {
|
|
1519
|
+
const count = await loadPluginModule(filePath);
|
|
1520
|
+
result.loaded += count;
|
|
1521
|
+
result.files.push(filePath);
|
|
1522
|
+
} catch (err) {
|
|
1523
|
+
result.errors.push({
|
|
1524
|
+
file: filePath,
|
|
1525
|
+
error: err.message
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
return result;
|
|
1530
|
+
}
|
|
1531
|
+
async function loadPlugins(options) {
|
|
1532
|
+
if (options.pluginPaths && options.pluginPaths.length > 0) {
|
|
1533
|
+
return loadPluginsFromPaths(options.pluginPaths, options.projectDir);
|
|
1534
|
+
}
|
|
1535
|
+
const conventionDir = join(options.projectDir, "src", "plugins");
|
|
1536
|
+
return loadPluginsFromDirectory(conventionDir);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1373
1539
|
// src/lib/progress-sink.ts
|
|
1374
1540
|
import { openSync, writeSync, closeSync, mkdirSync } from "fs";
|
|
1375
1541
|
import { dirname, join as join2, basename, extname } from "path";
|
|
@@ -1503,16 +1669,60 @@ Install it with: npm install ${pkg}`
|
|
|
1503
1669
|
|
|
1504
1670
|
// src/lib/execute.ts
|
|
1505
1671
|
import { resolve as resolve2 } from "path";
|
|
1506
|
-
async function
|
|
1672
|
+
async function resolveExecutionContext(options, requiredEntities) {
|
|
1673
|
+
const useBoundResolver = options.resolvedComponent || options.bindingPaths && options.bindingPaths.length > 0 || options.artifactBinding;
|
|
1674
|
+
if (useBoundResolver) {
|
|
1675
|
+
const component = options.resolvedComponent ?? await new BoundResolver().resolve({
|
|
1676
|
+
id: options.componentId ?? "default",
|
|
1677
|
+
embeddedDsl: options.dsl,
|
|
1678
|
+
bindingPaths: options.bindingPaths,
|
|
1679
|
+
activeGuardrailPolicy: options.activeGuardrailPolicy,
|
|
1680
|
+
artifactBinding: options.artifactBinding,
|
|
1681
|
+
paths: options.paths
|
|
1682
|
+
});
|
|
1683
|
+
if (requiredEntities) {
|
|
1684
|
+
const ctx2 = await loadDslContext({
|
|
1685
|
+
embeddedDsl: component.dsl,
|
|
1686
|
+
requiredEntities
|
|
1687
|
+
});
|
|
1688
|
+
return { registries: ctx2.registries, dsl: component.dsl, resolvedComponent: component };
|
|
1689
|
+
}
|
|
1690
|
+
return {
|
|
1691
|
+
registries: component.registries,
|
|
1692
|
+
dsl: component.dsl,
|
|
1693
|
+
resolvedComponent: component
|
|
1694
|
+
};
|
|
1695
|
+
}
|
|
1507
1696
|
const ctx = await loadDslContext({
|
|
1508
1697
|
embeddedDsl: options.dsl,
|
|
1509
1698
|
projectDslPath: options.projectDslPath,
|
|
1510
|
-
requiredEntities
|
|
1699
|
+
requiredEntities
|
|
1511
1700
|
});
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1701
|
+
return { registries: ctx.registries, dsl: ctx.dsl };
|
|
1702
|
+
}
|
|
1703
|
+
function injectGuardrailHooks(adapterOptions, execCtx, targetAgentId) {
|
|
1704
|
+
if (adapterOptions.guardrailHooks) return;
|
|
1705
|
+
if (execCtx.resolvedComponent && targetAgentId) {
|
|
1706
|
+
const rules2 = getGuardrailRulesForAgent(
|
|
1707
|
+
execCtx.resolvedComponent,
|
|
1708
|
+
targetAgentId,
|
|
1709
|
+
execCtx.dsl
|
|
1710
|
+
);
|
|
1711
|
+
if (rules2 && (rules2.commandRules.length > 0 || rules2.fileRules.length > 0 || rules2.contentRules.length > 0)) {
|
|
1712
|
+
adapterOptions.guardrailHooks = createGuardrailHooksFromRules(rules2);
|
|
1713
|
+
}
|
|
1714
|
+
return;
|
|
1515
1715
|
}
|
|
1716
|
+
const rules = buildGuardrailRulesFromDsl(execCtx.dsl);
|
|
1717
|
+
if (rules && (rules.commandRules.length > 0 || rules.fileRules.length > 0 || rules.contentRules.length > 0)) {
|
|
1718
|
+
adapterOptions.guardrailHooks = createGuardrailHooksFromRules(rules);
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
async function executeTask(taskId, options) {
|
|
1722
|
+
const execCtx = await resolveExecutionContext(options, { tasks: [taskId] });
|
|
1723
|
+
const targetAgentId = execCtx.registries.taskRegistry?.[taskId]?.target_agent;
|
|
1724
|
+
const adapterOptions = { ...options.adapterOptions };
|
|
1725
|
+
injectGuardrailHooks(adapterOptions, execCtx, targetAgentId);
|
|
1516
1726
|
const adapter = await createAdapter(options.adapter, {
|
|
1517
1727
|
model: options.model,
|
|
1518
1728
|
...adapterOptions
|
|
@@ -1522,13 +1732,13 @@ async function executeTask(taskId, options) {
|
|
|
1522
1732
|
);
|
|
1523
1733
|
try {
|
|
1524
1734
|
if (options.hooks?.beforeTask) {
|
|
1525
|
-
await options.hooks.beforeTask({ adapter, registries:
|
|
1735
|
+
await options.hooks.beforeTask({ adapter, registries: execCtx.registries, taskId });
|
|
1526
1736
|
}
|
|
1527
1737
|
const result = await runTask(adapter, taskId, { user_request: options.request }, {
|
|
1528
1738
|
maxFollowUps: options.maxFollowUps ?? 2,
|
|
1529
1739
|
maxRetries: options.maxRetries ?? 0,
|
|
1530
1740
|
progressOutput: progressSink,
|
|
1531
|
-
...
|
|
1741
|
+
...execCtx.registries
|
|
1532
1742
|
});
|
|
1533
1743
|
if (options.hooks?.afterTask) {
|
|
1534
1744
|
await options.hooks.afterTask(result);
|
|
@@ -1539,15 +1749,9 @@ async function executeTask(taskId, options) {
|
|
|
1539
1749
|
}
|
|
1540
1750
|
}
|
|
1541
1751
|
async function executeWorkflow(workflowId, options) {
|
|
1542
|
-
const
|
|
1543
|
-
embeddedDsl: options.dsl,
|
|
1544
|
-
projectDslPath: options.projectDslPath,
|
|
1545
|
-
requiredEntities: { workflows: [workflowId] }
|
|
1546
|
-
});
|
|
1752
|
+
const execCtx = await resolveExecutionContext(options, { workflows: [workflowId] });
|
|
1547
1753
|
const adapterOptions = { ...options.adapterOptions };
|
|
1548
|
-
|
|
1549
|
-
adapterOptions.guardrailHooks = createGuardrailHooksFromRules(ctx.guardrailRules);
|
|
1550
|
-
}
|
|
1754
|
+
injectGuardrailHooks(adapterOptions, execCtx);
|
|
1551
1755
|
const adapter = await createAdapter(options.adapter, {
|
|
1552
1756
|
model: options.model,
|
|
1553
1757
|
...adapterOptions
|
|
@@ -1557,7 +1761,7 @@ async function executeWorkflow(workflowId, options) {
|
|
|
1557
1761
|
);
|
|
1558
1762
|
try {
|
|
1559
1763
|
if (options.hooks?.beforeWorkflow) {
|
|
1560
|
-
await options.hooks.beforeWorkflow({ adapter, registries:
|
|
1764
|
+
await options.hooks.beforeWorkflow({ adapter, registries: execCtx.registries, workflowId });
|
|
1561
1765
|
}
|
|
1562
1766
|
const invocation = {
|
|
1563
1767
|
workflow: workflowId,
|
|
@@ -1575,7 +1779,7 @@ async function executeWorkflow(workflowId, options) {
|
|
|
1575
1779
|
},
|
|
1576
1780
|
context: options.context
|
|
1577
1781
|
};
|
|
1578
|
-
const result = await runWorkflow(adapter, invocation,
|
|
1782
|
+
const result = await runWorkflow(adapter, invocation, execCtx.registries);
|
|
1579
1783
|
if (options.hooks?.afterWorkflow) {
|
|
1580
1784
|
await options.hooks.afterWorkflow(result);
|
|
1581
1785
|
}
|
|
@@ -1598,20 +1802,25 @@ export {
|
|
|
1598
1802
|
runWorkflow,
|
|
1599
1803
|
buildGateEvaluationPrompt,
|
|
1600
1804
|
parseGateEvaluationResponse,
|
|
1601
|
-
createRuntime,
|
|
1602
|
-
WorkflowBuilder,
|
|
1603
|
-
loadPluginsFromPaths,
|
|
1604
|
-
loadPluginsFromDirectory,
|
|
1605
|
-
loadPlugins,
|
|
1606
1805
|
jsonSchemaToZod,
|
|
1607
1806
|
DslValidationError,
|
|
1608
1807
|
buildGuardrailRulesFromDsl,
|
|
1609
1808
|
buildRegistriesFromDsl,
|
|
1610
1809
|
loadDslContext,
|
|
1810
|
+
getEffectiveGuardrailIds,
|
|
1811
|
+
guardrailContextToRuleData,
|
|
1812
|
+
buildPerAgentGuardrails,
|
|
1813
|
+
BoundResolver,
|
|
1814
|
+
getGuardrailRulesForAgent,
|
|
1815
|
+
createRuntime,
|
|
1816
|
+
WorkflowBuilder,
|
|
1817
|
+
loadPluginsFromPaths,
|
|
1818
|
+
loadPluginsFromDirectory,
|
|
1819
|
+
loadPlugins,
|
|
1611
1820
|
createProgressSink,
|
|
1612
1821
|
InMemoryStore,
|
|
1613
1822
|
createAdapter,
|
|
1614
1823
|
executeTask,
|
|
1615
1824
|
executeWorkflow
|
|
1616
1825
|
};
|
|
1617
|
-
//# sourceMappingURL=chunk-
|
|
1826
|
+
//# sourceMappingURL=chunk-DPJFPYJO.js.map
|