agent-contracts-runtime 0.36.3 → 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 {
@@ -1401,6 +1114,10 @@ async function loadPlugins(options) {
1401
1114
  return loadPluginsFromDirectory(conventionDir);
1402
1115
  }
1403
1116
 
1117
+ // src/lib/dsl-context.ts
1118
+ import { readFile } from "fs/promises";
1119
+ import YAML from "yaml";
1120
+
1404
1121
  // src/lib/schema-converter.ts
1405
1122
  import { z as z2 } from "zod";
1406
1123
  function applyStringValidations(schema, def) {
@@ -1631,9 +1348,17 @@ async function loadDslContext(options) {
1631
1348
  let dsl;
1632
1349
  if (options.projectDslPath) {
1633
1350
  const { resolve: resolveDsl, mergeDsl, expandDefaults } = await import("agent-contracts");
1634
- const projectResult = await resolveDsl(options.projectDslPath);
1635
- const merged = mergeDsl(options.embeddedDsl, projectResult.data);
1636
- dsl = expandDefaults(merged);
1351
+ const raw = await readFile(options.projectDslPath, "utf8");
1352
+ const projectData = YAML.parse(raw);
1353
+ if (projectData.extends === "/$embedded/") {
1354
+ const { extends: _extends, ...projectOverrides } = projectData;
1355
+ const merged = mergeDsl(options.embeddedDsl, projectOverrides);
1356
+ dsl = expandDefaults(merged);
1357
+ } else {
1358
+ const projectResult = await resolveDsl(options.projectDslPath);
1359
+ const merged = mergeDsl(options.embeddedDsl, projectResult.data);
1360
+ dsl = expandDefaults(merged);
1361
+ }
1637
1362
  } else {
1638
1363
  dsl = options.embeddedDsl;
1639
1364
  }
@@ -1779,15 +1504,19 @@ Install it with: npm install ${pkg}`
1779
1504
  // src/lib/execute.ts
1780
1505
  import { resolve as resolve2 } from "path";
1781
1506
  async function executeTask(taskId, options) {
1782
- const adapter = await createAdapter(options.adapter, {
1783
- model: options.model,
1784
- ...options.adapterOptions
1785
- });
1786
1507
  const ctx = await loadDslContext({
1787
1508
  embeddedDsl: options.dsl,
1788
1509
  projectDslPath: options.projectDslPath,
1789
1510
  requiredEntities: { tasks: [taskId] }
1790
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
+ });
1791
1520
  const progressSink = createProgressSink(
1792
1521
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1793
1522
  );
@@ -1810,15 +1539,19 @@ async function executeTask(taskId, options) {
1810
1539
  }
1811
1540
  }
1812
1541
  async function executeWorkflow(workflowId, options) {
1813
- const adapter = await createAdapter(options.adapter, {
1814
- model: options.model,
1815
- ...options.adapterOptions
1816
- });
1817
1542
  const ctx = await loadDslContext({
1818
1543
  embeddedDsl: options.dsl,
1819
1544
  projectDslPath: options.projectDslPath,
1820
1545
  requiredEntities: { workflows: [workflowId] }
1821
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
+ });
1822
1555
  const progressSink = createProgressSink(
1823
1556
  options.logFile ? { stderr: true, file: resolve2(options.logFile), naming: "single" } : { stderr: true }
1824
1557
  );
@@ -1853,8 +1586,6 @@ async function executeWorkflow(workflowId, options) {
1853
1586
  }
1854
1587
 
1855
1588
  export {
1856
- PluginRegistry,
1857
- pluginRegistry,
1858
1589
  renderAgentSystemPrompt,
1859
1590
  buildCandidateAgents,
1860
1591
  zodSchemaToPromptDescription,
@@ -1867,11 +1598,6 @@ export {
1867
1598
  runWorkflow,
1868
1599
  buildGateEvaluationPrompt,
1869
1600
  parseGateEvaluationResponse,
1870
- getBlockingViolations,
1871
- getWarnings,
1872
- hasBlockingViolation,
1873
- createGuardrailHooks,
1874
- createGuardrailHooksFromRules,
1875
1601
  createRuntime,
1876
1602
  WorkflowBuilder,
1877
1603
  loadPluginsFromPaths,
@@ -1888,4 +1614,4 @@ export {
1888
1614
  executeTask,
1889
1615
  executeWorkflow
1890
1616
  };
1891
- //# sourceMappingURL=chunk-LNKWDQAJ.js.map
1617
+ //# sourceMappingURL=chunk-ADNNYJAU.js.map