agent-contracts-runtime 0.36.4 → 0.36.6

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.
@@ -1,102 +1,12 @@
1
+ import {
2
+ createGuardrailHooksFromRules,
3
+ pluginRegistry
4
+ } from "./chunk-JDXRLHY3.js";
5
+
1
6
  // src/lib/task-runner.ts
2
7
  import { z } from "zod";
3
8
  import * as yaml from "yaml";
4
9
 
5
- // src/lib/plugin.ts
6
- var PluginRegistry = class {
7
- plugins = [];
8
- register(plugin) {
9
- if (this.plugins.some((p) => p.id === plugin.id)) {
10
- throw new Error(`Plugin "${plugin.id}" is already registered`);
11
- }
12
- this.plugins.push(plugin);
13
- }
14
- getAll() {
15
- return this.plugins;
16
- }
17
- async runBeforeTask(taskId, context) {
18
- let ctx = context;
19
- for (const plugin of this.plugins) {
20
- if (!plugin.beforeTask || ctx === null) continue;
21
- ctx = await plugin.beforeTask(taskId, ctx);
22
- }
23
- return ctx;
24
- }
25
- async runAfterTask(taskId, outcome) {
26
- let result = outcome;
27
- for (const plugin of this.plugins) {
28
- if (!plugin.afterTask) continue;
29
- result = await plugin.afterTask(taskId, result);
30
- }
31
- return result;
32
- }
33
- applyContextEnhancers(taskId, context) {
34
- let ctx = context;
35
- for (const plugin of this.plugins) {
36
- if (!plugin.contextEnhancer) continue;
37
- ctx = plugin.contextEnhancer(taskId, ctx);
38
- }
39
- return ctx;
40
- }
41
- /**
42
- * Try plugin promptBuilder hooks. Returns custom prompt if any plugin provides one,
43
- * or null to use the default buildTaskPrompt.
44
- */
45
- applyPromptBuilder(args) {
46
- for (const plugin of this.plugins) {
47
- if (!plugin.promptBuilder) continue;
48
- const result = plugin.promptBuilder(args);
49
- if (result !== null) return result;
50
- }
51
- return null;
52
- }
53
- applyPromptEnhancers(taskId, prompt, context) {
54
- let enhanced = prompt;
55
- for (const plugin of this.plugins) {
56
- if (!plugin.promptEnhancer) continue;
57
- enhanced = plugin.promptEnhancer(taskId, enhanced, context);
58
- }
59
- return enhanced;
60
- }
61
- evaluateCommandGuardrails(command) {
62
- const results = [];
63
- for (const plugin of this.plugins) {
64
- if (!plugin.customGuardrails?.evaluateCommand) continue;
65
- results.push(...plugin.customGuardrails.evaluateCommand(command));
66
- }
67
- return results;
68
- }
69
- evaluateFilePathGuardrails(filePath) {
70
- const results = [];
71
- for (const plugin of this.plugins) {
72
- if (!plugin.customGuardrails?.evaluateFilePath) continue;
73
- results.push(...plugin.customGuardrails.evaluateFilePath(filePath));
74
- }
75
- return results;
76
- }
77
- evaluateFileContentGuardrails(filePath, content) {
78
- const results = [];
79
- for (const plugin of this.plugins) {
80
- if (!plugin.customGuardrails?.evaluateFileContent) continue;
81
- results.push(...plugin.customGuardrails.evaluateFileContent(filePath, content));
82
- }
83
- return results;
84
- }
85
- async runBeforeWorkflow(workflowId, userRequest) {
86
- for (const plugin of this.plugins) {
87
- if (!plugin.beforeWorkflow) continue;
88
- await plugin.beforeWorkflow(workflowId, userRequest);
89
- }
90
- }
91
- async runAfterWorkflow(workflowId, result) {
92
- for (const plugin of this.plugins) {
93
- if (!plugin.afterWorkflow) continue;
94
- await plugin.afterWorkflow(workflowId, result);
95
- }
96
- }
97
- };
98
- var pluginRegistry = new PluginRegistry();
99
-
100
10
  // src/lib/candidate-agents.ts
101
11
  function renderAgentSystemPrompt(agent) {
102
12
  const sections = [];
@@ -614,6 +524,7 @@ async function runTask(adapter, taskId, context, options) {
614
524
  prompt = enhancedPrompt;
615
525
  }
616
526
  const isReadonly = agentContract.mode === "read-only";
527
+ const allowDynamicWorkflow = task.allow_dynamic_workflow ?? false;
617
528
  const candidateAgents = Object.keys(agentReg).length > 0 ? buildCandidateAgents(agentReg, {
618
529
  entryAgentId: agentContract.id,
619
530
  allowedAgentIds: agentContract.can_invoke_agents
@@ -652,10 +563,10 @@ async function runTask(adapter, taskId, context, options) {
652
563
  memoryRef,
653
564
  splitPrompt,
654
565
  agents,
655
- options: { readonly: isReadonly, onProgress, splitPrompt, agents, maxFollowUps, attempt: 0 }
566
+ options: { readonly: isReadonly, onProgress, splitPrompt, agents, allowDynamicWorkflow, maxFollowUps, attempt: 0 }
656
567
  });
657
568
  } else {
658
- fullOutput = await adapter.send(prompt, { readonly: isReadonly, onProgress, splitPrompt, agents });
569
+ fullOutput = await adapter.send(prompt, { readonly: isReadonly, onProgress, splitPrompt, agents, allowDynamicWorkflow });
659
570
  }
660
571
  } catch (err) {
661
572
  return { outcome: { status: "error", message: `SDK error: ${err.message}` }, follow_ups_used: 0, retries_used: 0 };
@@ -1057,203 +968,6 @@ function emitStepEvent(options, workflowId, stepIndex, step, result) {
1057
968
  });
1058
969
  }
1059
970
 
1060
- // src/lib/guardrail-hooks.ts
1061
- function globFragmentToRegex(fragment) {
1062
- let out = "";
1063
- for (const ch of fragment) {
1064
- if (ch === "*") out += "[^/]*";
1065
- else if (ch === "?") out += "[^/]";
1066
- else if (".+^$|()[]\\".includes(ch)) out += "\\" + ch;
1067
- else out += ch;
1068
- }
1069
- return out;
1070
- }
1071
- function matchGlob(pattern, path) {
1072
- let regex = "";
1073
- let i = 0;
1074
- while (i < pattern.length) {
1075
- if (pattern[i] === "*" && pattern[i + 1] === "*" && pattern[i + 2] === "/") {
1076
- regex += "(?:.+/)?";
1077
- i += 3;
1078
- } else if (pattern[i] === "*" && pattern[i + 1] === "*") {
1079
- regex += ".*";
1080
- i += 2;
1081
- } else if (pattern[i] === "*") {
1082
- regex += "[^/]*";
1083
- i += 1;
1084
- } else if (pattern[i] === "?") {
1085
- regex += "[^/]";
1086
- i += 1;
1087
- } else if (pattern[i] === "{") {
1088
- const end = pattern.indexOf("}", i);
1089
- if (end !== -1) {
1090
- const alts = pattern.slice(i + 1, end).split(",").map(globFragmentToRegex);
1091
- regex += "(?:" + alts.join("|") + ")";
1092
- i = end + 1;
1093
- } else {
1094
- regex += "\\{";
1095
- i += 1;
1096
- }
1097
- } else if (".+^$|()[]\\".includes(pattern[i])) {
1098
- regex += "\\" + pattern[i];
1099
- i += 1;
1100
- } else {
1101
- regex += pattern[i];
1102
- i += 1;
1103
- }
1104
- }
1105
- return new RegExp("^" + regex + "$").test(path);
1106
- }
1107
- function getBlockingViolations(results) {
1108
- return results.filter((r) => !r.passed && r.action === "block");
1109
- }
1110
- function getWarnings(results) {
1111
- return results.filter((r) => !r.passed && r.action === "warn");
1112
- }
1113
- function hasBlockingViolation(results) {
1114
- return results.some((r) => !r.passed && r.action === "block");
1115
- }
1116
- function pluginResultToCheckResult(r) {
1117
- return {
1118
- guardrail_id: r.guardrail_id,
1119
- passed: !r.matched,
1120
- action: r.action,
1121
- message: r.matched ? r.message : ""
1122
- };
1123
- }
1124
- function createGuardrailHooks(checks) {
1125
- function runChecks(ctx) {
1126
- const results = [];
1127
- if (ctx.command) {
1128
- results.push(...checks.checkCommand(ctx.command));
1129
- results.push(
1130
- ...pluginRegistry.evaluateCommandGuardrails(ctx.command).map(pluginResultToCheckResult)
1131
- );
1132
- }
1133
- if (ctx.filePath) {
1134
- results.push(...checks.checkFilePath(ctx.filePath));
1135
- results.push(
1136
- ...pluginRegistry.evaluateFilePathGuardrails(ctx.filePath).map(pluginResultToCheckResult)
1137
- );
1138
- }
1139
- if (ctx.filePath && ctx.content) {
1140
- results.push(...checks.checkContent(ctx.filePath, ctx.content));
1141
- results.push(
1142
- ...pluginRegistry.evaluateFileContentGuardrails(ctx.filePath, ctx.content).map(pluginResultToCheckResult)
1143
- );
1144
- }
1145
- return results;
1146
- }
1147
- function beforeShellExecution(input) {
1148
- const command = input.command ?? "";
1149
- if (!command) return {};
1150
- const results = runChecks({ command });
1151
- const blocks = getBlockingViolations(results);
1152
- const warns = getWarnings(results);
1153
- if (blocks.length > 0) {
1154
- const msg = blocks.map((r) => `[${r.guardrail_id}] ${r.message}`).join("\n");
1155
- return { permission: "deny", user_message: msg, agent_message: msg };
1156
- }
1157
- if (warns.length > 0) {
1158
- return {
1159
- additionalContext: warns.map((r) => `[${r.guardrail_id}] ${r.message}`).join("\n")
1160
- };
1161
- }
1162
- return {};
1163
- }
1164
- function preToolUse(input) {
1165
- const filePath = input.tool_input?.file_path ?? input.tool_input?.path ?? "";
1166
- if (!filePath) return {};
1167
- const results = runChecks({ filePath });
1168
- const blocks = getBlockingViolations(results);
1169
- if (blocks.length > 0) {
1170
- const msg = blocks.map((r) => `[${r.guardrail_id}] ${r.message}`).join("\n");
1171
- return { permission: "deny", user_message: msg, agent_message: msg };
1172
- }
1173
- return {};
1174
- }
1175
- function afterFileEdit(input) {
1176
- const filePath = input.file_path ?? "";
1177
- const content = input.content ?? "";
1178
- if (!filePath) return {};
1179
- const results = runChecks({ filePath, content });
1180
- const blocks = getBlockingViolations(results);
1181
- if (blocks.length > 0) {
1182
- const msg = blocks.map((r) => `[${r.guardrail_id}] ${r.message}`).join("\n");
1183
- return { permission: "deny", user_message: msg, agent_message: msg };
1184
- }
1185
- return {};
1186
- }
1187
- return { beforeShellExecution, preToolUse, afterFileEdit, runChecks };
1188
- }
1189
- function createGuardrailHooksFromRules(rules) {
1190
- const commandChecks = rules.commandRules.map((rule) => ({
1191
- guardrail_id: rule.guardrail_id,
1192
- pattern: new RegExp(rule.pattern),
1193
- action: rule.action,
1194
- message: rule.message
1195
- }));
1196
- const fileChecks = rules.fileRules;
1197
- const contentChecks = rules.contentRules.map((rule) => ({
1198
- guardrail_id: rule.guardrail_id,
1199
- pattern: new RegExp(rule.pattern),
1200
- action: rule.action,
1201
- message: rule.message,
1202
- file_glob: rule.file_glob,
1203
- exclude_glob: rule.exclude_glob
1204
- }));
1205
- return createGuardrailHooks({
1206
- checkCommand(command) {
1207
- const results = [];
1208
- for (const check of commandChecks) {
1209
- const passed = !check.pattern.test(command);
1210
- results.push({
1211
- guardrail_id: check.guardrail_id,
1212
- passed,
1213
- action: check.action,
1214
- message: passed ? "" : check.message
1215
- });
1216
- }
1217
- return results;
1218
- },
1219
- checkFilePath(filePath) {
1220
- const results = [];
1221
- for (const check of fileChecks) {
1222
- if (check.exclude_glob && matchGlob(check.exclude_glob, filePath)) {
1223
- continue;
1224
- }
1225
- const matched = matchGlob(check.pattern, filePath);
1226
- results.push({
1227
- guardrail_id: check.guardrail_id,
1228
- passed: !matched,
1229
- action: check.action,
1230
- message: matched ? check.message : ""
1231
- });
1232
- }
1233
- return results;
1234
- },
1235
- checkContent(filePath, content) {
1236
- const results = [];
1237
- for (const check of contentChecks) {
1238
- if (check.file_glob && !matchGlob(check.file_glob, filePath)) {
1239
- continue;
1240
- }
1241
- if (check.exclude_glob && matchGlob(check.exclude_glob, filePath)) {
1242
- continue;
1243
- }
1244
- const matched = check.pattern.test(content);
1245
- results.push({
1246
- guardrail_id: check.guardrail_id,
1247
- passed: !matched,
1248
- action: check.action,
1249
- message: matched ? check.message : ""
1250
- });
1251
- }
1252
- return results;
1253
- }
1254
- });
1255
- }
1256
-
1257
971
  // src/lib/builder.ts
1258
972
  function createRuntime(config) {
1259
973
  return {
@@ -1791,15 +1505,19 @@ Install it with: npm install ${pkg}`
1791
1505
  // src/lib/execute.ts
1792
1506
  import { resolve as resolve2 } from "path";
1793
1507
  async function executeTask(taskId, options) {
1794
- const adapter = await createAdapter(options.adapter, {
1795
- model: options.model,
1796
- ...options.adapterOptions
1797
- });
1798
1508
  const ctx = await loadDslContext({
1799
1509
  embeddedDsl: options.dsl,
1800
1510
  projectDslPath: options.projectDslPath,
1801
1511
  requiredEntities: { tasks: [taskId] }
1802
1512
  });
1513
+ const adapterOptions = { ...options.adapterOptions };
1514
+ if (!adapterOptions.guardrailHooks && ctx.guardrailRules && (ctx.guardrailRules.commandRules.length > 0 || ctx.guardrailRules.fileRules.length > 0 || ctx.guardrailRules.contentRules.length > 0)) {
1515
+ adapterOptions.guardrailHooks = createGuardrailHooksFromRules(ctx.guardrailRules);
1516
+ }
1517
+ const adapter = await createAdapter(options.adapter, {
1518
+ model: options.model,
1519
+ ...adapterOptions
1520
+ });
1803
1521
  const progressSink = createProgressSink(
1804
1522
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1805
1523
  );
@@ -1822,15 +1540,19 @@ async function executeTask(taskId, options) {
1822
1540
  }
1823
1541
  }
1824
1542
  async function executeWorkflow(workflowId, options) {
1825
- const adapter = await createAdapter(options.adapter, {
1826
- model: options.model,
1827
- ...options.adapterOptions
1828
- });
1829
1543
  const ctx = await loadDslContext({
1830
1544
  embeddedDsl: options.dsl,
1831
1545
  projectDslPath: options.projectDslPath,
1832
1546
  requiredEntities: { workflows: [workflowId] }
1833
1547
  });
1548
+ const adapterOptions = { ...options.adapterOptions };
1549
+ if (!adapterOptions.guardrailHooks && ctx.guardrailRules && (ctx.guardrailRules.commandRules.length > 0 || ctx.guardrailRules.fileRules.length > 0 || ctx.guardrailRules.contentRules.length > 0)) {
1550
+ adapterOptions.guardrailHooks = createGuardrailHooksFromRules(ctx.guardrailRules);
1551
+ }
1552
+ const adapter = await createAdapter(options.adapter, {
1553
+ model: options.model,
1554
+ ...adapterOptions
1555
+ });
1834
1556
  const progressSink = createProgressSink(
1835
1557
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1836
1558
  );
@@ -1865,8 +1587,6 @@ async function executeWorkflow(workflowId, options) {
1865
1587
  }
1866
1588
 
1867
1589
  export {
1868
- PluginRegistry,
1869
- pluginRegistry,
1870
1590
  renderAgentSystemPrompt,
1871
1591
  buildCandidateAgents,
1872
1592
  zodSchemaToPromptDescription,
@@ -1879,11 +1599,6 @@ export {
1879
1599
  runWorkflow,
1880
1600
  buildGateEvaluationPrompt,
1881
1601
  parseGateEvaluationResponse,
1882
- getBlockingViolations,
1883
- getWarnings,
1884
- hasBlockingViolation,
1885
- createGuardrailHooks,
1886
- createGuardrailHooksFromRules,
1887
1602
  createRuntime,
1888
1603
  WorkflowBuilder,
1889
1604
  loadPluginsFromPaths,
@@ -1900,4 +1615,4 @@ export {
1900
1615
  executeTask,
1901
1616
  executeWorkflow
1902
1617
  };
1903
- //# sourceMappingURL=chunk-VMCNFGEY.js.map
1618
+ //# sourceMappingURL=chunk-FFCXUPYA.js.map