@contractspec/lib.ai-agent 1.57.0 → 1.59.0

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.
Files changed (232) hide show
  1. package/dist/agent/agent-factory.d.ts +67 -71
  2. package/dist/agent/agent-factory.d.ts.map +1 -1
  3. package/dist/agent/agent-factory.js +658 -100
  4. package/dist/agent/agent.test.d.ts +2 -0
  5. package/dist/agent/agent.test.d.ts.map +1 -0
  6. package/dist/agent/contract-spec-agent.d.ts +48 -52
  7. package/dist/agent/contract-spec-agent.d.ts.map +1 -1
  8. package/dist/agent/contract-spec-agent.js +605 -146
  9. package/dist/agent/index.d.ts +4 -4
  10. package/dist/agent/index.d.ts.map +1 -0
  11. package/dist/agent/index.js +2102 -4
  12. package/dist/agent/json-runner.d.ts +15 -19
  13. package/dist/agent/json-runner.d.ts.map +1 -1
  14. package/dist/agent/json-runner.js +672 -57
  15. package/dist/agent/json-runner.test.d.ts +2 -0
  16. package/dist/agent/json-runner.test.d.ts.map +1 -0
  17. package/dist/agent/unified-agent.d.ts +132 -109
  18. package/dist/agent/unified-agent.d.ts.map +1 -1
  19. package/dist/agent/unified-agent.js +2011 -293
  20. package/dist/approval/index.d.ts +3 -2
  21. package/dist/approval/index.d.ts.map +1 -0
  22. package/dist/approval/index.js +128 -2
  23. package/dist/approval/workflow.d.ts +106 -110
  24. package/dist/approval/workflow.d.ts.map +1 -1
  25. package/dist/approval/workflow.js +126 -157
  26. package/dist/exporters/claude-agent-exporter.d.ts +50 -48
  27. package/dist/exporters/claude-agent-exporter.d.ts.map +1 -1
  28. package/dist/exporters/claude-agent-exporter.js +258 -203
  29. package/dist/exporters/index.d.ts +28 -4
  30. package/dist/exporters/index.d.ts.map +1 -0
  31. package/dist/exporters/index.js +737 -3
  32. package/dist/exporters/opencode-exporter.d.ts +47 -45
  33. package/dist/exporters/opencode-exporter.d.ts.map +1 -1
  34. package/dist/exporters/opencode-exporter.js +507 -191
  35. package/dist/exporters/types.d.ts +171 -169
  36. package/dist/exporters/types.d.ts.map +1 -1
  37. package/dist/exporters/types.js +1 -0
  38. package/dist/index.d.ts +15 -39
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +3337 -32
  41. package/dist/interop/index.d.ts +48 -4
  42. package/dist/interop/index.d.ts.map +1 -0
  43. package/dist/interop/index.js +709 -3
  44. package/dist/interop/spec-consumer.d.ts +57 -53
  45. package/dist/interop/spec-consumer.d.ts.map +1 -1
  46. package/dist/interop/spec-consumer.js +302 -282
  47. package/dist/interop/tool-consumer.d.ts +57 -53
  48. package/dist/interop/tool-consumer.d.ts.map +1 -1
  49. package/dist/interop/tool-consumer.js +412 -213
  50. package/dist/interop/types.d.ts +173 -171
  51. package/dist/interop/types.d.ts.map +1 -1
  52. package/dist/interop/types.js +1 -0
  53. package/dist/knowledge/index.d.ts +2 -2
  54. package/dist/knowledge/index.d.ts.map +1 -0
  55. package/dist/knowledge/index.js +66 -2
  56. package/dist/knowledge/injector.d.ts +16 -20
  57. package/dist/knowledge/injector.d.ts.map +1 -1
  58. package/dist/knowledge/injector.js +56 -47
  59. package/dist/memory/in-memory.d.ts +15 -19
  60. package/dist/memory/in-memory.d.ts.map +1 -1
  61. package/dist/memory/in-memory.js +152 -46
  62. package/dist/memory/index.d.ts +3 -3
  63. package/dist/memory/index.d.ts.map +1 -0
  64. package/dist/memory/index.js +155 -3
  65. package/dist/memory/manager.d.ts +32 -36
  66. package/dist/memory/manager.d.ts.map +1 -1
  67. package/dist/memory/manager.js +96 -70
  68. package/dist/memory/memory.test.d.ts +2 -0
  69. package/dist/memory/memory.test.d.ts.map +1 -0
  70. package/dist/node/agent/agent-factory.js +661 -0
  71. package/dist/node/agent/contract-spec-agent.js +607 -0
  72. package/dist/node/agent/index.js +2103 -0
  73. package/dist/node/agent/json-runner.js +684 -0
  74. package/dist/node/agent/unified-agent.js +2019 -0
  75. package/dist/node/approval/index.js +129 -0
  76. package/dist/node/approval/workflow.js +129 -0
  77. package/dist/node/exporters/claude-agent-exporter.js +265 -0
  78. package/dist/node/exporters/index.js +738 -0
  79. package/dist/node/exporters/opencode-exporter.js +516 -0
  80. package/dist/node/exporters/types.js +0 -0
  81. package/dist/node/index.js +3337 -0
  82. package/dist/node/interop/index.js +710 -0
  83. package/dist/node/interop/spec-consumer.js +307 -0
  84. package/dist/node/interop/tool-consumer.js +419 -0
  85. package/dist/node/interop/types.js +0 -0
  86. package/dist/node/knowledge/index.js +67 -0
  87. package/dist/node/knowledge/injector.js +67 -0
  88. package/dist/node/memory/in-memory.js +154 -0
  89. package/dist/node/memory/index.js +156 -0
  90. package/dist/node/memory/manager.js +105 -0
  91. package/dist/node/providers/claude-agent-sdk/adapter.js +624 -0
  92. package/dist/node/providers/claude-agent-sdk/index.js +673 -0
  93. package/dist/node/providers/claude-agent-sdk/session-bridge.js +149 -0
  94. package/dist/node/providers/claude-agent-sdk/tool-bridge.js +118 -0
  95. package/dist/node/providers/index.js +1261 -0
  96. package/dist/node/providers/opencode-sdk/adapter.js +669 -0
  97. package/dist/node/providers/opencode-sdk/agent-bridge.js +299 -0
  98. package/dist/node/providers/opencode-sdk/index.js +703 -0
  99. package/dist/node/providers/opencode-sdk/tool-bridge.js +141 -0
  100. package/dist/node/providers/registry.js +89 -0
  101. package/dist/node/providers/types.js +56 -0
  102. package/dist/node/schema/index.js +195 -0
  103. package/dist/node/schema/json-schema-to-zod.js +152 -0
  104. package/dist/node/schema/schema-output.js +190 -0
  105. package/dist/node/session/index.js +90 -0
  106. package/dist/node/session/store.js +90 -0
  107. package/dist/node/spec/index.js +85 -0
  108. package/dist/node/spec/registry.js +56 -0
  109. package/dist/node/spec/spec.js +44 -0
  110. package/dist/node/telemetry/adapter.js +85 -0
  111. package/dist/node/telemetry/index.js +86 -0
  112. package/dist/node/tools/index.js +345 -0
  113. package/dist/node/tools/knowledge-tool.js +74 -0
  114. package/dist/node/tools/mcp-client.js +47 -0
  115. package/dist/node/tools/mcp-server.js +205 -0
  116. package/dist/node/tools/tool-adapter.js +197 -0
  117. package/dist/node/types.js +0 -0
  118. package/dist/providers/claude-agent-sdk/adapter.d.ts +60 -52
  119. package/dist/providers/claude-agent-sdk/adapter.d.ts.map +1 -1
  120. package/dist/providers/claude-agent-sdk/adapter.js +622 -304
  121. package/dist/providers/claude-agent-sdk/index.d.ts +22 -4
  122. package/dist/providers/claude-agent-sdk/index.d.ts.map +1 -0
  123. package/dist/providers/claude-agent-sdk/index.js +672 -4
  124. package/dist/providers/claude-agent-sdk/session-bridge.d.ts +43 -40
  125. package/dist/providers/claude-agent-sdk/session-bridge.d.ts.map +1 -1
  126. package/dist/providers/claude-agent-sdk/session-bridge.js +121 -130
  127. package/dist/providers/claude-agent-sdk/tool-bridge.d.ts +63 -60
  128. package/dist/providers/claude-agent-sdk/tool-bridge.d.ts.map +1 -1
  129. package/dist/providers/claude-agent-sdk/tool-bridge.js +104 -108
  130. package/dist/providers/index.d.ts +28 -7
  131. package/dist/providers/index.d.ts.map +1 -0
  132. package/dist/providers/index.js +1261 -8
  133. package/dist/providers/opencode-sdk/adapter.d.ts +56 -48
  134. package/dist/providers/opencode-sdk/adapter.d.ts.map +1 -1
  135. package/dist/providers/opencode-sdk/adapter.js +667 -274
  136. package/dist/providers/opencode-sdk/agent-bridge.d.ts +62 -57
  137. package/dist/providers/opencode-sdk/agent-bridge.d.ts.map +1 -1
  138. package/dist/providers/opencode-sdk/agent-bridge.js +289 -155
  139. package/dist/providers/opencode-sdk/index.d.ts +22 -4
  140. package/dist/providers/opencode-sdk/index.d.ts.map +1 -0
  141. package/dist/providers/opencode-sdk/index.js +702 -4
  142. package/dist/providers/opencode-sdk/tool-bridge.d.ts +41 -42
  143. package/dist/providers/opencode-sdk/tool-bridge.d.ts.map +1 -1
  144. package/dist/providers/opencode-sdk/tool-bridge.js +121 -107
  145. package/dist/providers/registry.d.ts +10 -11
  146. package/dist/providers/registry.d.ts.map +1 -1
  147. package/dist/providers/registry.js +86 -49
  148. package/dist/providers/types.d.ts +169 -166
  149. package/dist/providers/types.d.ts.map +1 -1
  150. package/dist/providers/types.js +54 -42
  151. package/dist/schema/index.d.ts +3 -3
  152. package/dist/schema/index.d.ts.map +1 -0
  153. package/dist/schema/index.js +194 -3
  154. package/dist/schema/json-schema-to-zod.d.ts +23 -26
  155. package/dist/schema/json-schema-to-zod.d.ts.map +1 -1
  156. package/dist/schema/json-schema-to-zod.js +138 -110
  157. package/dist/schema/schema-output.d.ts +29 -32
  158. package/dist/schema/schema-output.d.ts.map +1 -1
  159. package/dist/schema/schema-output.js +178 -53
  160. package/dist/session/index.d.ts +2 -2
  161. package/dist/session/index.d.ts.map +1 -0
  162. package/dist/session/index.js +89 -2
  163. package/dist/session/store.d.ts +51 -55
  164. package/dist/session/store.d.ts.map +1 -1
  165. package/dist/session/store.js +85 -74
  166. package/dist/spec/index.d.ts +3 -3
  167. package/dist/spec/index.d.ts.map +1 -0
  168. package/dist/spec/index.js +84 -3
  169. package/dist/spec/registry.d.ts +32 -36
  170. package/dist/spec/registry.d.ts.map +1 -1
  171. package/dist/spec/registry.js +51 -60
  172. package/dist/spec/spec.d.ts +80 -84
  173. package/dist/spec/spec.d.ts.map +1 -1
  174. package/dist/spec/spec.js +40 -26
  175. package/dist/telemetry/adapter.d.ts +33 -37
  176. package/dist/telemetry/adapter.d.ts.map +1 -1
  177. package/dist/telemetry/adapter.js +78 -96
  178. package/dist/telemetry/index.d.ts +2 -2
  179. package/dist/telemetry/index.d.ts.map +1 -0
  180. package/dist/telemetry/index.js +85 -2
  181. package/dist/tools/index.d.ts +5 -5
  182. package/dist/tools/index.d.ts.map +1 -0
  183. package/dist/tools/index.js +344 -5
  184. package/dist/tools/knowledge-tool.d.ts +4 -8
  185. package/dist/tools/knowledge-tool.d.ts.map +1 -1
  186. package/dist/tools/knowledge-tool.js +68 -48
  187. package/dist/tools/mcp-client.d.ts +17 -21
  188. package/dist/tools/mcp-client.d.ts.map +1 -1
  189. package/dist/tools/mcp-client.js +42 -53
  190. package/dist/tools/mcp-server.d.ts +14 -18
  191. package/dist/tools/mcp-server.d.ts.map +1 -1
  192. package/dist/tools/mcp-server.js +200 -64
  193. package/dist/tools/tool-adapter.d.ts +7 -11
  194. package/dist/tools/tool-adapter.d.ts.map +1 -1
  195. package/dist/tools/tool-adapter.js +187 -70
  196. package/dist/tools/tools.test.d.ts +2 -0
  197. package/dist/tools/tools.test.d.ts.map +1 -0
  198. package/dist/types.d.ts +108 -111
  199. package/dist/types.d.ts.map +1 -1
  200. package/dist/types.js +1 -0
  201. package/package.json +448 -90
  202. package/dist/_virtual/_rolldown/runtime.js +0 -8
  203. package/dist/agent/agent-factory.js.map +0 -1
  204. package/dist/agent/contract-spec-agent.js.map +0 -1
  205. package/dist/agent/json-runner.js.map +0 -1
  206. package/dist/agent/unified-agent.js.map +0 -1
  207. package/dist/approval/workflow.js.map +0 -1
  208. package/dist/exporters/claude-agent-exporter.js.map +0 -1
  209. package/dist/exporters/opencode-exporter.js.map +0 -1
  210. package/dist/interop/spec-consumer.js.map +0 -1
  211. package/dist/interop/tool-consumer.js.map +0 -1
  212. package/dist/knowledge/injector.js.map +0 -1
  213. package/dist/memory/in-memory.js.map +0 -1
  214. package/dist/memory/manager.js.map +0 -1
  215. package/dist/providers/claude-agent-sdk/adapter.js.map +0 -1
  216. package/dist/providers/claude-agent-sdk/session-bridge.js.map +0 -1
  217. package/dist/providers/claude-agent-sdk/tool-bridge.js.map +0 -1
  218. package/dist/providers/opencode-sdk/adapter.js.map +0 -1
  219. package/dist/providers/opencode-sdk/agent-bridge.js.map +0 -1
  220. package/dist/providers/opencode-sdk/tool-bridge.js.map +0 -1
  221. package/dist/providers/registry.js.map +0 -1
  222. package/dist/providers/types.js.map +0 -1
  223. package/dist/schema/json-schema-to-zod.js.map +0 -1
  224. package/dist/schema/schema-output.js.map +0 -1
  225. package/dist/session/store.js.map +0 -1
  226. package/dist/spec/registry.js.map +0 -1
  227. package/dist/spec/spec.js.map +0 -1
  228. package/dist/telemetry/adapter.js.map +0 -1
  229. package/dist/tools/knowledge-tool.js.map +0 -1
  230. package/dist/tools/mcp-client.js.map +0 -1
  231. package/dist/tools/mcp-server.js.map +0 -1
  232. package/dist/tools/tool-adapter.js.map +0 -1
@@ -0,0 +1,307 @@
1
+ import { createRequire } from "node:module";
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true,
8
+ configurable: true,
9
+ set: (newValue) => all[name] = () => newValue
10
+ });
11
+ };
12
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
13
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
14
+
15
+ // src/spec/spec.ts
16
+ function defineAgent(spec) {
17
+ if (!spec.meta?.key) {
18
+ throw new Error("Agent key is required");
19
+ }
20
+ if (typeof spec.meta.version !== "string") {
21
+ throw new Error(`Agent ${spec.meta.key} is missing a string version`);
22
+ }
23
+ if (!spec.instructions?.trim()) {
24
+ throw new Error(`Agent ${spec.meta.key} requires instructions`);
25
+ }
26
+ if (!spec.tools?.length) {
27
+ throw new Error(`Agent ${spec.meta.key} must expose at least one tool`);
28
+ }
29
+ const toolNames = new Set;
30
+ for (const tool of spec.tools) {
31
+ if (toolNames.has(tool.name)) {
32
+ throw new Error(`Agent ${spec.meta.key} has duplicate tool name: ${tool.name}`);
33
+ }
34
+ toolNames.add(tool.name);
35
+ }
36
+ return Object.freeze(spec);
37
+ }
38
+ function agentKey(meta) {
39
+ return `${meta.key}.v${meta.version}`;
40
+ }
41
+
42
+ // src/interop/spec-consumer.ts
43
+ class ContractSpecConsumer {
44
+ specs;
45
+ includeMetadata;
46
+ baseUrl;
47
+ constructor(config) {
48
+ this.specs = new Map;
49
+ this.includeMetadata = config.includeMetadata ?? true;
50
+ this.baseUrl = config.baseUrl;
51
+ for (const spec of config.specs) {
52
+ const key = agentKey(spec.meta);
53
+ this.specs.set(key, spec);
54
+ }
55
+ }
56
+ getSpecMarkdown(specKey, options) {
57
+ const spec = this.specs.get(specKey);
58
+ if (!spec) {
59
+ throw new Error(`Spec not found: ${specKey}`);
60
+ }
61
+ const sections = [];
62
+ const opts = {
63
+ includeToc: options?.includeToc ?? true,
64
+ includeTools: options?.includeTools ?? true,
65
+ ...options
66
+ };
67
+ if (opts.customHeader) {
68
+ sections.push(opts.customHeader);
69
+ sections.push("");
70
+ }
71
+ const specName = spec.meta.key;
72
+ sections.push(`# ${specName}`);
73
+ sections.push("");
74
+ if (spec.description) {
75
+ sections.push(spec.description);
76
+ sections.push("");
77
+ }
78
+ if (opts.includeToc) {
79
+ sections.push("## Table of Contents");
80
+ sections.push("");
81
+ sections.push("- [Overview](#overview)");
82
+ sections.push("- [Instructions](#instructions)");
83
+ if (opts.includeTools && spec.tools && spec.tools.length > 0) {
84
+ sections.push("- [Tools](#tools)");
85
+ }
86
+ if (spec.knowledge && spec.knowledge.length > 0) {
87
+ sections.push("- [Knowledge](#knowledge)");
88
+ }
89
+ sections.push("");
90
+ }
91
+ sections.push("## Overview");
92
+ sections.push("");
93
+ if (this.includeMetadata) {
94
+ sections.push(`- **Key**: \`${spec.meta.key}\``);
95
+ sections.push(`- **Version**: ${spec.meta.version}`);
96
+ if (spec.meta.stability) {
97
+ sections.push(`- **Stability**: ${spec.meta.stability}`);
98
+ }
99
+ if (spec.meta.owners && spec.meta.owners.length > 0) {
100
+ sections.push(`- **Owners**: ${spec.meta.owners.join(", ")}`);
101
+ }
102
+ if (spec.tags && spec.tags.length > 0) {
103
+ sections.push(`- **Tags**: ${spec.tags.join(", ")}`);
104
+ }
105
+ }
106
+ sections.push("");
107
+ sections.push("## Instructions");
108
+ sections.push("");
109
+ sections.push(spec.instructions);
110
+ sections.push("");
111
+ if (opts.includeTools && spec.tools && spec.tools.length > 0) {
112
+ sections.push("## Tools");
113
+ sections.push("");
114
+ for (const tool of spec.tools) {
115
+ sections.push(`### ${tool.name}`);
116
+ sections.push("");
117
+ if (tool.description) {
118
+ sections.push(tool.description);
119
+ sections.push("");
120
+ }
121
+ if (tool.schema) {
122
+ sections.push("**Schema:**");
123
+ sections.push("");
124
+ sections.push("```json");
125
+ sections.push(JSON.stringify(tool.schema, null, 2));
126
+ sections.push("```");
127
+ sections.push("");
128
+ }
129
+ if (tool.automationSafe !== undefined) {
130
+ sections.push(`**Automation Safe**: ${tool.automationSafe ? "Yes" : "No"}`);
131
+ sections.push("");
132
+ }
133
+ }
134
+ }
135
+ if (spec.knowledge && spec.knowledge.length > 0) {
136
+ sections.push("## Knowledge");
137
+ sections.push("");
138
+ for (const k of spec.knowledge) {
139
+ sections.push(`- **${k.key}**${k.required ? " (required)" : ""}`);
140
+ if (k.instructions) {
141
+ sections.push(` - ${k.instructions}`);
142
+ }
143
+ }
144
+ sections.push("");
145
+ }
146
+ if (spec.policy) {
147
+ sections.push("## Policy");
148
+ sections.push("");
149
+ if (spec.policy.confidence) {
150
+ sections.push(`- **Minimum Confidence**: ${spec.policy.confidence.min ?? 0.7}`);
151
+ }
152
+ if (spec.policy.escalation) {
153
+ const esc = spec.policy.escalation;
154
+ if (esc.confidenceThreshold) {
155
+ sections.push(`- **Escalation Threshold**: ${esc.confidenceThreshold}`);
156
+ }
157
+ if (esc.onToolFailure) {
158
+ sections.push("- **Escalate on Tool Failure**: Yes");
159
+ }
160
+ if (esc.onTimeout) {
161
+ sections.push("- **Escalate on Timeout**: Yes");
162
+ }
163
+ }
164
+ sections.push("");
165
+ }
166
+ return sections.join(`
167
+ `);
168
+ }
169
+ getSpecPrompt(specKey, options) {
170
+ const spec = this.specs.get(specKey);
171
+ if (!spec) {
172
+ throw new Error(`Spec not found: ${specKey}`);
173
+ }
174
+ const sections = [];
175
+ const opts = {
176
+ includeTools: options?.includeTools ?? true,
177
+ format: options?.format ?? "structured",
178
+ ...options
179
+ };
180
+ sections.push("# Agent Identity");
181
+ sections.push("");
182
+ sections.push(`You are ${spec.meta.key} (v${spec.meta.version}).`);
183
+ sections.push("");
184
+ if (spec.description) {
185
+ sections.push("## Description");
186
+ sections.push("");
187
+ sections.push(spec.description);
188
+ sections.push("");
189
+ }
190
+ sections.push("## Instructions");
191
+ sections.push("");
192
+ sections.push(spec.instructions);
193
+ sections.push("");
194
+ if (opts.includeTools && spec.tools && spec.tools.length > 0) {
195
+ sections.push("## Available Tools");
196
+ sections.push("");
197
+ sections.push("You have access to the following tools:");
198
+ sections.push("");
199
+ for (const tool of spec.tools) {
200
+ sections.push(`### ${tool.name}`);
201
+ sections.push("");
202
+ if (tool.description) {
203
+ sections.push(tool.description);
204
+ sections.push("");
205
+ }
206
+ if (tool.schema && opts.format === "structured") {
207
+ sections.push("Parameters:");
208
+ sections.push("```json");
209
+ sections.push(JSON.stringify(tool.schema, null, 2));
210
+ sections.push("```");
211
+ sections.push("");
212
+ }
213
+ }
214
+ }
215
+ if (spec.knowledge && spec.knowledge.length > 0) {
216
+ const requiredKnowledge = spec.knowledge.filter((k) => k.required);
217
+ if (requiredKnowledge.length > 0) {
218
+ sections.push("## Knowledge Context");
219
+ sections.push("");
220
+ for (const k of requiredKnowledge) {
221
+ if (k.instructions) {
222
+ sections.push(k.instructions);
223
+ sections.push("");
224
+ }
225
+ }
226
+ }
227
+ }
228
+ if (options?.customContext) {
229
+ sections.push("## Additional Context");
230
+ sections.push("");
231
+ sections.push(options.customContext);
232
+ sections.push("");
233
+ }
234
+ return sections.join(`
235
+ `);
236
+ }
237
+ listSpecs(options) {
238
+ const results = [];
239
+ for (const [key, spec] of this.specs) {
240
+ if (options?.stability && spec.meta.stability !== options.stability) {
241
+ continue;
242
+ }
243
+ if (options?.tags && options.tags.length > 0) {
244
+ const specTags = spec.tags ?? [];
245
+ const hasMatchingTag = options.tags.some((tag) => specTags.includes(tag));
246
+ if (!hasMatchingTag) {
247
+ continue;
248
+ }
249
+ }
250
+ results.push({
251
+ key,
252
+ name: spec.meta.key,
253
+ version: spec.meta.version,
254
+ description: spec.description,
255
+ stability: spec.meta.stability,
256
+ tags: spec.tags,
257
+ toolCount: spec.tools?.length ?? 0
258
+ });
259
+ }
260
+ return results;
261
+ }
262
+ querySpec(specKey) {
263
+ const spec = this.specs.get(specKey);
264
+ if (!spec) {
265
+ return;
266
+ }
267
+ return {
268
+ key: specKey,
269
+ spec,
270
+ markdown: this.getSpecMarkdown(specKey),
271
+ prompt: this.getSpecPrompt(specKey)
272
+ };
273
+ }
274
+ hasSpec(specKey) {
275
+ return this.specs.has(specKey);
276
+ }
277
+ getSpec(specKey) {
278
+ return this.specs.get(specKey);
279
+ }
280
+ getAllSpecs() {
281
+ return Array.from(this.specs.values());
282
+ }
283
+ getSpecCount() {
284
+ return this.specs.size;
285
+ }
286
+ addSpec(spec) {
287
+ const key = agentKey(spec.meta);
288
+ this.specs.set(key, spec);
289
+ }
290
+ removeSpec(specKey) {
291
+ return this.specs.delete(specKey);
292
+ }
293
+ }
294
+ function createSpecConsumer(config) {
295
+ return new ContractSpecConsumer(config);
296
+ }
297
+ function createSingleSpecConsumer(spec, options) {
298
+ return new ContractSpecConsumer({
299
+ specs: [spec],
300
+ ...options
301
+ });
302
+ }
303
+ export {
304
+ createSpecConsumer,
305
+ createSingleSpecConsumer,
306
+ ContractSpecConsumer
307
+ };
@@ -0,0 +1,419 @@
1
+ import { createRequire } from "node:module";
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true,
8
+ configurable: true,
9
+ set: (newValue) => all[name] = () => newValue
10
+ });
11
+ };
12
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
13
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
14
+
15
+ // src/providers/claude-agent-sdk/tool-bridge.ts
16
+ function specToolToClaudeAgentTool(tool, handler, context) {
17
+ return {
18
+ name: tool.name,
19
+ description: tool.description ?? `Execute ${tool.name}`,
20
+ input_schema: normalizeSchema(tool.schema),
21
+ requires_confirmation: tool.requiresApproval ?? !tool.automationSafe,
22
+ execute: async (input) => {
23
+ const fullContext = {
24
+ agentId: context.agentId ?? "unknown",
25
+ sessionId: context.sessionId ?? "unknown",
26
+ tenantId: context.tenantId,
27
+ actorId: context.actorId,
28
+ metadata: context.metadata,
29
+ signal: context.signal
30
+ };
31
+ return handler(input, fullContext);
32
+ }
33
+ };
34
+ }
35
+ function specToolsToClaudeAgentTools(tools, handlers, context) {
36
+ return tools.filter((tool) => handlers.has(tool.name)).map((tool) => {
37
+ const handler = handlers.get(tool.name);
38
+ if (!handler) {
39
+ throw new Error(`Handler not found for tool ${tool.name}`);
40
+ }
41
+ return specToolToClaudeAgentTool(tool, handler, context);
42
+ });
43
+ }
44
+ function claudeAgentToolToSpecTool(claudeTool, execute) {
45
+ const config = {
46
+ name: claudeTool.name,
47
+ description: claudeTool.description,
48
+ schema: claudeTool.input_schema,
49
+ requiresApproval: claudeTool.requires_confirmation,
50
+ automationSafe: !claudeTool.requires_confirmation
51
+ };
52
+ const handler = execute ? async (input, _ctx) => String(await execute(input)) : undefined;
53
+ return { config, handler };
54
+ }
55
+ function claudeAgentToolsToSpecTools(claudeTools) {
56
+ const configs = [];
57
+ const handlers = new Map;
58
+ for (const claudeTool of claudeTools) {
59
+ const { config, handler } = claudeAgentToolToSpecTool(claudeTool, claudeTool.execute);
60
+ configs.push(config);
61
+ if (handler) {
62
+ handlers.set(config.name, handler);
63
+ }
64
+ }
65
+ return { configs, handlers };
66
+ }
67
+ function specToolToExternalTool(tool, handler, context) {
68
+ return {
69
+ name: tool.name,
70
+ description: tool.description ?? `Execute ${tool.name}`,
71
+ inputSchema: tool.schema ?? { type: "object" },
72
+ requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
73
+ execute: handler ? async (input) => {
74
+ const fullContext = {
75
+ agentId: context?.agentId ?? "unknown",
76
+ sessionId: context?.sessionId ?? "unknown",
77
+ tenantId: context?.tenantId,
78
+ actorId: context?.actorId,
79
+ metadata: context?.metadata,
80
+ signal: context?.signal
81
+ };
82
+ return handler(input, fullContext);
83
+ } : undefined
84
+ };
85
+ }
86
+ function normalizeSchema(schema) {
87
+ if (!schema) {
88
+ return { type: "object" };
89
+ }
90
+ if (schema.type === "object") {
91
+ return schema;
92
+ }
93
+ return {
94
+ type: "object",
95
+ properties: {
96
+ value: schema
97
+ },
98
+ required: ["value"]
99
+ };
100
+ }
101
+ function extractToolCalls(response) {
102
+ if (!response.content) {
103
+ return [];
104
+ }
105
+ return response.content.filter((block) => block.type === "tool_use").map((block) => ({
106
+ toolCallId: block.id ?? "",
107
+ toolName: block.name ?? "",
108
+ args: block.input
109
+ }));
110
+ }
111
+
112
+ // src/providers/opencode-sdk/tool-bridge.ts
113
+ function specToolToOpenCodeTool(tool) {
114
+ return {
115
+ name: tool.name,
116
+ description: tool.description ?? `Execute ${tool.name}`,
117
+ parameters: normalizeToOpenCodeParameters(tool.schema),
118
+ permission: getPermissionLevel(tool)
119
+ };
120
+ }
121
+ function specToolsToOpenCodeTools(tools) {
122
+ return tools.map(specToolToOpenCodeTool);
123
+ }
124
+ function getPermissionLevel(tool) {
125
+ if (tool.requiresApproval) {
126
+ return "ask";
127
+ }
128
+ if (tool.automationSafe === false) {
129
+ return "ask";
130
+ }
131
+ return "allow";
132
+ }
133
+ function openCodeToolToSpecTool(openCodeTool) {
134
+ return {
135
+ name: openCodeTool.name,
136
+ description: openCodeTool.description,
137
+ schema: openCodeTool.parameters,
138
+ requiresApproval: openCodeTool.permission === "ask",
139
+ automationSafe: openCodeTool.permission === "allow"
140
+ };
141
+ }
142
+ function openCodeToolsToSpecTools(openCodeTools) {
143
+ return openCodeTools.map(openCodeToolToSpecTool);
144
+ }
145
+ function specToolToExternalToolForOpenCode(tool, handler, context) {
146
+ return {
147
+ name: tool.name,
148
+ description: tool.description ?? `Execute ${tool.name}`,
149
+ inputSchema: tool.schema ?? { type: "object" },
150
+ requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
151
+ execute: handler ? async (input) => {
152
+ const fullContext = {
153
+ agentId: context?.agentId ?? "unknown",
154
+ sessionId: context?.sessionId ?? "unknown",
155
+ tenantId: context?.tenantId,
156
+ actorId: context?.actorId,
157
+ metadata: context?.metadata,
158
+ signal: context?.signal
159
+ };
160
+ return handler(input, fullContext);
161
+ } : undefined
162
+ };
163
+ }
164
+ function normalizeToOpenCodeParameters(schema) {
165
+ if (!schema) {
166
+ return { type: "object" };
167
+ }
168
+ if (schema.type === "object") {
169
+ return {
170
+ type: "object",
171
+ properties: schema.properties,
172
+ required: schema.required
173
+ };
174
+ }
175
+ return {
176
+ type: "object",
177
+ properties: {
178
+ value: convertToOpenCodeParameter(schema)
179
+ },
180
+ required: ["value"]
181
+ };
182
+ }
183
+ function convertToOpenCodeParameter(schema) {
184
+ const param = {
185
+ type: schema.type ?? "string"
186
+ };
187
+ if (schema.description) {
188
+ param.description = schema.description;
189
+ }
190
+ if (schema.enum) {
191
+ param.enum = schema.enum;
192
+ }
193
+ if (schema.default !== undefined) {
194
+ param.default = schema.default;
195
+ }
196
+ return param;
197
+ }
198
+ function createToolHandlerMap(tools) {
199
+ const handlers = new Map;
200
+ for (const [name, tool] of Object.entries(tools)) {
201
+ if (tool.execute) {
202
+ handlers.set(name, tool.execute);
203
+ }
204
+ }
205
+ return handlers;
206
+ }
207
+ async function executeToolCall(toolCall, handlers) {
208
+ const handler = handlers.get(toolCall.name);
209
+ if (!handler) {
210
+ return {
211
+ tool_call_id: toolCall.id,
212
+ output: `Error: Tool '${toolCall.name}' not found`,
213
+ is_error: true
214
+ };
215
+ }
216
+ try {
217
+ const result = await handler(toolCall.arguments);
218
+ return {
219
+ tool_call_id: toolCall.id,
220
+ output: typeof result === "string" ? result : JSON.stringify(result)
221
+ };
222
+ } catch (error) {
223
+ return {
224
+ tool_call_id: toolCall.id,
225
+ output: `Error: ${error instanceof Error ? error.message : String(error)}`,
226
+ is_error: true
227
+ };
228
+ }
229
+ }
230
+
231
+ // src/interop/tool-consumer.ts
232
+ class MCPToolServer {
233
+ tools;
234
+ name;
235
+ version;
236
+ running = false;
237
+ constructor(config) {
238
+ this.tools = new Map;
239
+ this.name = config.name ?? "contractspec-tools";
240
+ this.version = config.version ?? "1.0.0";
241
+ for (const tool of config.tools) {
242
+ this.tools.set(tool.config.name, tool);
243
+ }
244
+ }
245
+ async start() {
246
+ if (this.running) {
247
+ return;
248
+ }
249
+ this.running = true;
250
+ console.log(`[MCPToolServer] Started ${this.name}@${this.version} with ${this.tools.size} tools`);
251
+ }
252
+ async stop() {
253
+ if (!this.running) {
254
+ return;
255
+ }
256
+ this.running = false;
257
+ console.log(`[MCPToolServer] Stopped ${this.name}`);
258
+ }
259
+ isRunning() {
260
+ return this.running;
261
+ }
262
+ getTools() {
263
+ return Array.from(this.tools.values()).map((t) => t.config);
264
+ }
265
+ async executeTool(toolName, args, context) {
266
+ const tool = this.tools.get(toolName);
267
+ if (!tool) {
268
+ throw new Error(`Tool not found: ${toolName}`);
269
+ }
270
+ if (!tool.handler) {
271
+ throw new Error(`No handler registered for tool: ${toolName}`);
272
+ }
273
+ const fullContext = {
274
+ agentId: context?.agentId ?? "mcp-server",
275
+ sessionId: context?.sessionId ?? "mcp-session",
276
+ tenantId: context?.tenantId,
277
+ actorId: context?.actorId,
278
+ metadata: context?.metadata,
279
+ signal: context?.signal
280
+ };
281
+ return await tool.handler(args, fullContext);
282
+ }
283
+ getMCPToolDefinitions() {
284
+ const definitions = [];
285
+ for (const [name, tool] of this.tools) {
286
+ definitions.push({
287
+ name,
288
+ description: tool.config.description ?? "",
289
+ inputSchema: tool.config.schema ?? { type: "object", properties: {} }
290
+ });
291
+ }
292
+ return definitions;
293
+ }
294
+ getServerInfo() {
295
+ return {
296
+ name: this.name,
297
+ version: this.version,
298
+ tools: this.tools.size,
299
+ running: this.running
300
+ };
301
+ }
302
+ }
303
+
304
+ class ContractSpecToolConsumer {
305
+ tools;
306
+ constructor(config) {
307
+ this.tools = new Map;
308
+ for (const tool of config.tools) {
309
+ this.tools.set(tool.config.name, tool);
310
+ }
311
+ }
312
+ createToolServer(config) {
313
+ return new MCPToolServer({
314
+ tools: Array.from(this.tools.values()),
315
+ ...config
316
+ });
317
+ }
318
+ exportToolsForSDK(format) {
319
+ const tools = Array.from(this.tools.values());
320
+ const defaultContext = {
321
+ agentId: "export",
322
+ sessionId: "export"
323
+ };
324
+ switch (format) {
325
+ case "claude-agent":
326
+ return tools.filter((tool) => !!tool.handler).map((tool) => specToolToClaudeAgentTool(tool.config, tool.handler, defaultContext));
327
+ case "opencode":
328
+ return tools.map((tool) => specToolToOpenCodeTool(tool.config));
329
+ case "mcp":
330
+ return tools.map((tool) => ({
331
+ name: tool.config.name,
332
+ description: tool.config.description ?? "",
333
+ inputSchema: tool.config.schema ?? { type: "object", properties: {} }
334
+ }));
335
+ case "openai":
336
+ return tools.map((tool) => ({
337
+ type: "function",
338
+ function: {
339
+ name: tool.config.name,
340
+ description: tool.config.description ?? "",
341
+ parameters: tool.config.schema ?? {
342
+ type: "object",
343
+ properties: {}
344
+ }
345
+ }
346
+ }));
347
+ default:
348
+ throw new Error(`Unknown export format: ${format}`);
349
+ }
350
+ }
351
+ createBridgedHandler(toolName, _format) {
352
+ const tool = this.tools.get(toolName);
353
+ if (!tool || !tool.handler) {
354
+ return;
355
+ }
356
+ const handler = tool.handler;
357
+ return async (args) => {
358
+ const context = {
359
+ agentId: "bridge",
360
+ sessionId: "bridge"
361
+ };
362
+ return handler(args, context);
363
+ };
364
+ }
365
+ getTools() {
366
+ return Array.from(this.tools.values()).map((t) => t.config);
367
+ }
368
+ getTool(name) {
369
+ return this.tools.get(name)?.config;
370
+ }
371
+ hasTool(name) {
372
+ return this.tools.has(name);
373
+ }
374
+ async executeTool(name, args, context) {
375
+ const tool = this.tools.get(name);
376
+ if (!tool) {
377
+ throw new Error(`Tool not found: ${name}`);
378
+ }
379
+ if (!tool.handler) {
380
+ throw new Error(`No handler for tool: ${name}`);
381
+ }
382
+ const fullContext = {
383
+ agentId: context?.agentId ?? "consumer",
384
+ sessionId: context?.sessionId ?? "consumer-session",
385
+ tenantId: context?.tenantId,
386
+ actorId: context?.actorId,
387
+ metadata: context?.metadata,
388
+ signal: context?.signal
389
+ };
390
+ return await tool.handler(args, fullContext);
391
+ }
392
+ addTool(config, handler) {
393
+ this.tools.set(config.name, { config, handler });
394
+ }
395
+ removeTool(name) {
396
+ return this.tools.delete(name);
397
+ }
398
+ getToolCount() {
399
+ return this.tools.size;
400
+ }
401
+ }
402
+ function createToolConsumer(config) {
403
+ return new ContractSpecToolConsumer(config);
404
+ }
405
+ function createToolServer(config) {
406
+ return new MCPToolServer(config);
407
+ }
408
+ function exportToolsForExternalSDK(tools, format) {
409
+ const consumer = new ContractSpecToolConsumer({
410
+ tools: tools.map((config) => ({ config }))
411
+ });
412
+ return consumer.exportToolsForSDK(format);
413
+ }
414
+ export {
415
+ exportToolsForExternalSDK,
416
+ createToolServer,
417
+ createToolConsumer,
418
+ ContractSpecToolConsumer
419
+ };