@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
@@ -1,287 +1,307 @@
1
- import { agentKey } from "../spec/spec.js";
2
-
3
- //#region src/interop/spec-consumer.ts
4
- /**
5
- * ContractSpec consumer for external agents.
6
- *
7
- * @example
8
- * ```typescript
9
- * const consumer = createSpecConsumer({
10
- * specs: [myAgentSpec, anotherSpec],
11
- * includeMetadata: true,
12
- * });
13
- *
14
- * // Get markdown representation
15
- * const markdown = consumer.getSpecMarkdown('my-agent.v1.0.0');
16
- *
17
- * // Get prompt for LLM
18
- * const prompt = consumer.getSpecPrompt('my-agent.v1.0.0', {
19
- * includeTools: true,
20
- * });
21
- * ```
22
- */
23
- var ContractSpecConsumer = class {
24
- specs;
25
- includeMetadata;
26
- baseUrl;
27
- constructor(config) {
28
- this.specs = /* @__PURE__ */ new Map();
29
- this.includeMetadata = config.includeMetadata ?? true;
30
- this.baseUrl = config.baseUrl;
31
- for (const spec of config.specs) {
32
- const key = agentKey(spec.meta);
33
- this.specs.set(key, spec);
34
- }
35
- }
36
- /**
37
- * Get a spec as markdown.
38
- */
39
- getSpecMarkdown(specKey, options) {
40
- const spec = this.specs.get(specKey);
41
- if (!spec) throw new Error(`Spec not found: ${specKey}`);
42
- const sections = [];
43
- const opts = {
44
- includeToc: options?.includeToc ?? true,
45
- includeTools: options?.includeTools ?? true,
46
- ...options
47
- };
48
- if (opts.customHeader) {
49
- sections.push(opts.customHeader);
50
- sections.push("");
51
- }
52
- const specName = spec.meta.key;
53
- sections.push(`# ${specName}`);
54
- sections.push("");
55
- if (spec.description) {
56
- sections.push(spec.description);
57
- sections.push("");
58
- }
59
- if (opts.includeToc) {
60
- sections.push("## Table of Contents");
61
- sections.push("");
62
- sections.push("- [Overview](#overview)");
63
- sections.push("- [Instructions](#instructions)");
64
- if (opts.includeTools && spec.tools && spec.tools.length > 0) sections.push("- [Tools](#tools)");
65
- if (spec.knowledge && spec.knowledge.length > 0) sections.push("- [Knowledge](#knowledge)");
66
- sections.push("");
67
- }
68
- sections.push("## Overview");
69
- sections.push("");
70
- if (this.includeMetadata) {
71
- sections.push(`- **Key**: \`${spec.meta.key}\``);
72
- sections.push(`- **Version**: ${spec.meta.version}`);
73
- if (spec.meta.stability) sections.push(`- **Stability**: ${spec.meta.stability}`);
74
- if (spec.meta.owners && spec.meta.owners.length > 0) sections.push(`- **Owners**: ${spec.meta.owners.join(", ")}`);
75
- if (spec.tags && spec.tags.length > 0) sections.push(`- **Tags**: ${spec.tags.join(", ")}`);
76
- }
77
- sections.push("");
78
- sections.push("## Instructions");
79
- sections.push("");
80
- sections.push(spec.instructions);
81
- sections.push("");
82
- if (opts.includeTools && spec.tools && spec.tools.length > 0) {
83
- sections.push("## Tools");
84
- sections.push("");
85
- for (const tool of spec.tools) {
86
- sections.push(`### ${tool.name}`);
87
- sections.push("");
88
- if (tool.description) {
89
- sections.push(tool.description);
90
- sections.push("");
91
- }
92
- if (tool.schema) {
93
- sections.push("**Schema:**");
94
- sections.push("");
95
- sections.push("```json");
96
- sections.push(JSON.stringify(tool.schema, null, 2));
97
- sections.push("```");
98
- sections.push("");
99
- }
100
- if (tool.automationSafe !== void 0) {
101
- sections.push(`**Automation Safe**: ${tool.automationSafe ? "Yes" : "No"}`);
102
- sections.push("");
103
- }
104
- }
105
- }
106
- if (spec.knowledge && spec.knowledge.length > 0) {
107
- sections.push("## Knowledge");
108
- sections.push("");
109
- for (const k of spec.knowledge) {
110
- sections.push(`- **${k.key}**${k.required ? " (required)" : ""}`);
111
- if (k.instructions) sections.push(` - ${k.instructions}`);
112
- }
113
- sections.push("");
114
- }
115
- if (spec.policy) {
116
- sections.push("## Policy");
117
- sections.push("");
118
- if (spec.policy.confidence) sections.push(`- **Minimum Confidence**: ${spec.policy.confidence.min ?? .7}`);
119
- if (spec.policy.escalation) {
120
- const esc = spec.policy.escalation;
121
- if (esc.confidenceThreshold) sections.push(`- **Escalation Threshold**: ${esc.confidenceThreshold}`);
122
- if (esc.onToolFailure) sections.push("- **Escalate on Tool Failure**: Yes");
123
- if (esc.onTimeout) sections.push("- **Escalate on Timeout**: Yes");
124
- }
125
- sections.push("");
126
- }
127
- return sections.join("\n");
128
- }
129
- /**
130
- * Get a spec as an LLM prompt.
131
- */
132
- getSpecPrompt(specKey, options) {
133
- const spec = this.specs.get(specKey);
134
- if (!spec) throw new Error(`Spec not found: ${specKey}`);
135
- const sections = [];
136
- const opts = {
137
- includeTools: options?.includeTools ?? true,
138
- format: options?.format ?? "structured",
139
- ...options
140
- };
141
- sections.push("# Agent Identity");
142
- sections.push("");
143
- sections.push(`You are ${spec.meta.key} (v${spec.meta.version}).`);
144
- sections.push("");
145
- if (spec.description) {
146
- sections.push("## Description");
147
- sections.push("");
148
- sections.push(spec.description);
149
- sections.push("");
150
- }
151
- sections.push("## Instructions");
152
- sections.push("");
153
- sections.push(spec.instructions);
154
- sections.push("");
155
- if (opts.includeTools && spec.tools && spec.tools.length > 0) {
156
- sections.push("## Available Tools");
157
- sections.push("");
158
- sections.push("You have access to the following tools:");
159
- sections.push("");
160
- for (const tool of spec.tools) {
161
- sections.push(`### ${tool.name}`);
162
- sections.push("");
163
- if (tool.description) {
164
- sections.push(tool.description);
165
- sections.push("");
166
- }
167
- if (tool.schema && opts.format === "structured") {
168
- sections.push("Parameters:");
169
- sections.push("```json");
170
- sections.push(JSON.stringify(tool.schema, null, 2));
171
- sections.push("```");
172
- sections.push("");
173
- }
174
- }
175
- }
176
- if (spec.knowledge && spec.knowledge.length > 0) {
177
- const requiredKnowledge = spec.knowledge.filter((k) => k.required);
178
- if (requiredKnowledge.length > 0) {
179
- sections.push("## Knowledge Context");
180
- sections.push("");
181
- for (const k of requiredKnowledge) if (k.instructions) {
182
- sections.push(k.instructions);
183
- sections.push("");
184
- }
185
- }
186
- }
187
- if (options?.customContext) {
188
- sections.push("## Additional Context");
189
- sections.push("");
190
- sections.push(options.customContext);
191
- sections.push("");
192
- }
193
- return sections.join("\n");
194
- }
195
- /**
196
- * List available specs.
197
- */
198
- listSpecs(options) {
199
- const results = [];
200
- for (const [key, spec] of this.specs) {
201
- if (options?.stability && spec.meta.stability !== options.stability) continue;
202
- if (options?.tags && options.tags.length > 0) {
203
- const specTags = spec.tags ?? [];
204
- if (!options.tags.some((tag) => specTags.includes(tag))) continue;
205
- }
206
- results.push({
207
- key,
208
- name: spec.meta.key,
209
- version: spec.meta.version,
210
- description: spec.description,
211
- stability: spec.meta.stability,
212
- tags: spec.tags,
213
- toolCount: spec.tools?.length ?? 0
214
- });
215
- }
216
- return results;
217
- }
218
- /**
219
- * Query a spec by key.
220
- */
221
- querySpec(specKey) {
222
- const spec = this.specs.get(specKey);
223
- if (!spec) return;
224
- return {
225
- key: specKey,
226
- spec,
227
- markdown: this.getSpecMarkdown(specKey),
228
- prompt: this.getSpecPrompt(specKey)
229
- };
230
- }
231
- /**
232
- * Check if a spec exists.
233
- */
234
- hasSpec(specKey) {
235
- return this.specs.has(specKey);
236
- }
237
- /**
238
- * Get a spec by key.
239
- */
240
- getSpec(specKey) {
241
- return this.specs.get(specKey);
242
- }
243
- /**
244
- * Get all specs.
245
- */
246
- getAllSpecs() {
247
- return Array.from(this.specs.values());
248
- }
249
- /**
250
- * Get spec count.
251
- */
252
- getSpecCount() {
253
- return this.specs.size;
254
- }
255
- /**
256
- * Add a spec to the consumer.
257
- */
258
- addSpec(spec) {
259
- const key = agentKey(spec.meta);
260
- this.specs.set(key, spec);
261
- }
262
- /**
263
- * Remove a spec from the consumer.
264
- */
265
- removeSpec(specKey) {
266
- return this.specs.delete(specKey);
267
- }
1
+ // @bun
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
+ });
268
11
  };
269
- /**
270
- * Create a new spec consumer.
271
- */
12
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
13
+ var __require = import.meta.require;
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
+ }
272
294
  function createSpecConsumer(config) {
273
- return new ContractSpecConsumer(config);
295
+ return new ContractSpecConsumer(config);
274
296
  }
275
- /**
276
- * Create a spec consumer from a single spec.
277
- */
278
297
  function createSingleSpecConsumer(spec, options) {
279
- return new ContractSpecConsumer({
280
- specs: [spec],
281
- ...options
282
- });
298
+ return new ContractSpecConsumer({
299
+ specs: [spec],
300
+ ...options
301
+ });
283
302
  }
284
-
285
- //#endregion
286
- export { ContractSpecConsumer, createSingleSpecConsumer, createSpecConsumer };
287
- //# sourceMappingURL=spec-consumer.js.map
303
+ export {
304
+ createSpecConsumer,
305
+ createSingleSpecConsumer,
306
+ ContractSpecConsumer
307
+ };