agent-contracts-runtime 0.36.4 → 0.36.5

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 = [];
@@ -1057,203 +967,6 @@ function emitStepEvent(options, workflowId, stepIndex, step, result) {
1057
967
  });
1058
968
  }
1059
969
 
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
970
  // src/lib/builder.ts
1258
971
  function createRuntime(config) {
1259
972
  return {
@@ -1791,15 +1504,19 @@ Install it with: npm install ${pkg}`
1791
1504
  // src/lib/execute.ts
1792
1505
  import { resolve as resolve2 } from "path";
1793
1506
  async function executeTask(taskId, options) {
1794
- const adapter = await createAdapter(options.adapter, {
1795
- model: options.model,
1796
- ...options.adapterOptions
1797
- });
1798
1507
  const ctx = await loadDslContext({
1799
1508
  embeddedDsl: options.dsl,
1800
1509
  projectDslPath: options.projectDslPath,
1801
1510
  requiredEntities: { tasks: [taskId] }
1802
1511
  });
1512
+ const adapterOptions = { ...options.adapterOptions };
1513
+ if (!adapterOptions.guardrailHooks && ctx.guardrailRules && (ctx.guardrailRules.commandRules.length > 0 || ctx.guardrailRules.fileRules.length > 0 || ctx.guardrailRules.contentRules.length > 0)) {
1514
+ adapterOptions.guardrailHooks = createGuardrailHooksFromRules(ctx.guardrailRules);
1515
+ }
1516
+ const adapter = await createAdapter(options.adapter, {
1517
+ model: options.model,
1518
+ ...adapterOptions
1519
+ });
1803
1520
  const progressSink = createProgressSink(
1804
1521
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1805
1522
  );
@@ -1822,15 +1539,19 @@ async function executeTask(taskId, options) {
1822
1539
  }
1823
1540
  }
1824
1541
  async function executeWorkflow(workflowId, options) {
1825
- const adapter = await createAdapter(options.adapter, {
1826
- model: options.model,
1827
- ...options.adapterOptions
1828
- });
1829
1542
  const ctx = await loadDslContext({
1830
1543
  embeddedDsl: options.dsl,
1831
1544
  projectDslPath: options.projectDslPath,
1832
1545
  requiredEntities: { workflows: [workflowId] }
1833
1546
  });
1547
+ const adapterOptions = { ...options.adapterOptions };
1548
+ if (!adapterOptions.guardrailHooks && ctx.guardrailRules && (ctx.guardrailRules.commandRules.length > 0 || ctx.guardrailRules.fileRules.length > 0 || ctx.guardrailRules.contentRules.length > 0)) {
1549
+ adapterOptions.guardrailHooks = createGuardrailHooksFromRules(ctx.guardrailRules);
1550
+ }
1551
+ const adapter = await createAdapter(options.adapter, {
1552
+ model: options.model,
1553
+ ...adapterOptions
1554
+ });
1834
1555
  const progressSink = createProgressSink(
1835
1556
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1836
1557
  );
@@ -1865,8 +1586,6 @@ async function executeWorkflow(workflowId, options) {
1865
1586
  }
1866
1587
 
1867
1588
  export {
1868
- PluginRegistry,
1869
- pluginRegistry,
1870
1589
  renderAgentSystemPrompt,
1871
1590
  buildCandidateAgents,
1872
1591
  zodSchemaToPromptDescription,
@@ -1879,11 +1598,6 @@ export {
1879
1598
  runWorkflow,
1880
1599
  buildGateEvaluationPrompt,
1881
1600
  parseGateEvaluationResponse,
1882
- getBlockingViolations,
1883
- getWarnings,
1884
- hasBlockingViolation,
1885
- createGuardrailHooks,
1886
- createGuardrailHooksFromRules,
1887
1601
  createRuntime,
1888
1602
  WorkflowBuilder,
1889
1603
  loadPluginsFromPaths,
@@ -1900,4 +1614,4 @@ export {
1900
1614
  executeTask,
1901
1615
  executeWorkflow
1902
1616
  };
1903
- //# sourceMappingURL=chunk-VMCNFGEY.js.map
1617
+ //# sourceMappingURL=chunk-ADNNYJAU.js.map