@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,129 @@
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/approval/workflow.ts
16
+ import { randomUUID } from "node:crypto";
17
+
18
+ class InMemoryApprovalStore {
19
+ items = new Map;
20
+ async create(request) {
21
+ this.items.set(request.id, request);
22
+ }
23
+ async get(id) {
24
+ return this.items.get(id) ?? null;
25
+ }
26
+ async getByToolCallId(toolCallId) {
27
+ for (const request of this.items.values()) {
28
+ if (request.toolCallId === toolCallId) {
29
+ return request;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ async update(id, updates) {
35
+ const existing = this.items.get(id);
36
+ if (existing) {
37
+ this.items.set(id, { ...existing, ...updates });
38
+ }
39
+ }
40
+ async list(options) {
41
+ let results = [...this.items.values()];
42
+ if (options?.status) {
43
+ results = results.filter((r) => r.status === options.status);
44
+ }
45
+ if (options?.agentId) {
46
+ results = results.filter((r) => r.agentId === options.agentId);
47
+ }
48
+ if (options?.tenantId) {
49
+ results = results.filter((r) => r.tenantId === options.tenantId);
50
+ }
51
+ return results.sort((a, b) => b.requestedAt.getTime() - a.requestedAt.getTime());
52
+ }
53
+ clear() {
54
+ this.items.clear();
55
+ }
56
+ }
57
+
58
+ class ApprovalWorkflow {
59
+ store;
60
+ constructor(store = new InMemoryApprovalStore) {
61
+ this.store = store;
62
+ }
63
+ async requestApproval(params) {
64
+ const request = {
65
+ id: randomUUID(),
66
+ sessionId: params.sessionId,
67
+ agentId: params.agentId,
68
+ tenantId: params.tenantId,
69
+ toolName: params.toolName,
70
+ toolCallId: params.toolCallId,
71
+ toolArgs: params.toolArgs,
72
+ reason: params.reason,
73
+ requestedAt: new Date,
74
+ status: "pending",
75
+ payload: params.payload
76
+ };
77
+ await this.store.create(request);
78
+ return request;
79
+ }
80
+ async requestApprovalFromToolCall(toolCall, context) {
81
+ return this.requestApproval({
82
+ sessionId: context.sessionId,
83
+ agentId: context.agentId,
84
+ tenantId: context.tenantId,
85
+ toolName: toolCall.toolName,
86
+ toolCallId: toolCall.toolCallId,
87
+ toolArgs: toolCall.args,
88
+ reason: context.reason ?? `Tool "${toolCall.toolName}" requires approval`
89
+ });
90
+ }
91
+ async approve(id, reviewer, notes) {
92
+ await this.store.update(id, {
93
+ status: "approved",
94
+ reviewer,
95
+ resolvedAt: new Date,
96
+ notes
97
+ });
98
+ }
99
+ async reject(id, reviewer, notes) {
100
+ await this.store.update(id, {
101
+ status: "rejected",
102
+ reviewer,
103
+ resolvedAt: new Date,
104
+ notes
105
+ });
106
+ }
107
+ async getStatus(toolCallId) {
108
+ const request = await this.store.getByToolCallId(toolCallId);
109
+ return request?.status ?? null;
110
+ }
111
+ async isApproved(toolCallId) {
112
+ const status = await this.getStatus(toolCallId);
113
+ return status === "approved";
114
+ }
115
+ async listPending(options) {
116
+ return this.store.list({ ...options, status: "pending" });
117
+ }
118
+ async get(id) {
119
+ return this.store.get(id);
120
+ }
121
+ }
122
+ function createApprovalWorkflow(store) {
123
+ return new ApprovalWorkflow(store);
124
+ }
125
+ export {
126
+ createApprovalWorkflow,
127
+ InMemoryApprovalStore,
128
+ ApprovalWorkflow
129
+ };
@@ -0,0 +1,129 @@
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/approval/workflow.ts
16
+ import { randomUUID } from "node:crypto";
17
+
18
+ class InMemoryApprovalStore {
19
+ items = new Map;
20
+ async create(request) {
21
+ this.items.set(request.id, request);
22
+ }
23
+ async get(id) {
24
+ return this.items.get(id) ?? null;
25
+ }
26
+ async getByToolCallId(toolCallId) {
27
+ for (const request of this.items.values()) {
28
+ if (request.toolCallId === toolCallId) {
29
+ return request;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ async update(id, updates) {
35
+ const existing = this.items.get(id);
36
+ if (existing) {
37
+ this.items.set(id, { ...existing, ...updates });
38
+ }
39
+ }
40
+ async list(options) {
41
+ let results = [...this.items.values()];
42
+ if (options?.status) {
43
+ results = results.filter((r) => r.status === options.status);
44
+ }
45
+ if (options?.agentId) {
46
+ results = results.filter((r) => r.agentId === options.agentId);
47
+ }
48
+ if (options?.tenantId) {
49
+ results = results.filter((r) => r.tenantId === options.tenantId);
50
+ }
51
+ return results.sort((a, b) => b.requestedAt.getTime() - a.requestedAt.getTime());
52
+ }
53
+ clear() {
54
+ this.items.clear();
55
+ }
56
+ }
57
+
58
+ class ApprovalWorkflow {
59
+ store;
60
+ constructor(store = new InMemoryApprovalStore) {
61
+ this.store = store;
62
+ }
63
+ async requestApproval(params) {
64
+ const request = {
65
+ id: randomUUID(),
66
+ sessionId: params.sessionId,
67
+ agentId: params.agentId,
68
+ tenantId: params.tenantId,
69
+ toolName: params.toolName,
70
+ toolCallId: params.toolCallId,
71
+ toolArgs: params.toolArgs,
72
+ reason: params.reason,
73
+ requestedAt: new Date,
74
+ status: "pending",
75
+ payload: params.payload
76
+ };
77
+ await this.store.create(request);
78
+ return request;
79
+ }
80
+ async requestApprovalFromToolCall(toolCall, context) {
81
+ return this.requestApproval({
82
+ sessionId: context.sessionId,
83
+ agentId: context.agentId,
84
+ tenantId: context.tenantId,
85
+ toolName: toolCall.toolName,
86
+ toolCallId: toolCall.toolCallId,
87
+ toolArgs: toolCall.args,
88
+ reason: context.reason ?? `Tool "${toolCall.toolName}" requires approval`
89
+ });
90
+ }
91
+ async approve(id, reviewer, notes) {
92
+ await this.store.update(id, {
93
+ status: "approved",
94
+ reviewer,
95
+ resolvedAt: new Date,
96
+ notes
97
+ });
98
+ }
99
+ async reject(id, reviewer, notes) {
100
+ await this.store.update(id, {
101
+ status: "rejected",
102
+ reviewer,
103
+ resolvedAt: new Date,
104
+ notes
105
+ });
106
+ }
107
+ async getStatus(toolCallId) {
108
+ const request = await this.store.getByToolCallId(toolCallId);
109
+ return request?.status ?? null;
110
+ }
111
+ async isApproved(toolCallId) {
112
+ const status = await this.getStatus(toolCallId);
113
+ return status === "approved";
114
+ }
115
+ async listPending(options) {
116
+ return this.store.list({ ...options, status: "pending" });
117
+ }
118
+ async get(id) {
119
+ return this.store.get(id);
120
+ }
121
+ }
122
+ function createApprovalWorkflow(store) {
123
+ return new ApprovalWorkflow(store);
124
+ }
125
+ export {
126
+ createApprovalWorkflow,
127
+ InMemoryApprovalStore,
128
+ ApprovalWorkflow
129
+ };
@@ -0,0 +1,265 @@
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/exporters/claude-agent-exporter.ts
43
+ class ClaudeAgentExporter {
44
+ format = "claude-agent";
45
+ export(spec, options = {}) {
46
+ const tools = this.exportTools(spec);
47
+ const config = this.buildConfig(spec, tools, options);
48
+ const claudeMd = options.generateClaudeMd ? this.generateClaudeMd(spec, options) : undefined;
49
+ return {
50
+ config,
51
+ claudeMd,
52
+ tools,
53
+ exportedAt: new Date,
54
+ sourceSpec: agentKey(spec.meta)
55
+ };
56
+ }
57
+ exportMany(specs, options = {}) {
58
+ return specs.map((spec) => this.export(spec, options));
59
+ }
60
+ validate(spec) {
61
+ const errors = [];
62
+ if (!spec.meta?.key) {
63
+ errors.push("Spec must have a meta.key");
64
+ }
65
+ if (!spec.instructions) {
66
+ errors.push("Spec must have instructions");
67
+ }
68
+ if (!spec.tools || spec.tools.length === 0) {
69
+ errors.push("Spec must have at least one tool");
70
+ }
71
+ for (const tool of spec.tools ?? []) {
72
+ if (!tool.name) {
73
+ errors.push("All tools must have a name");
74
+ }
75
+ if (!tool.description && !tool.name) {
76
+ errors.push(`Tool must have a description or name`);
77
+ }
78
+ }
79
+ return { valid: errors.length === 0, errors };
80
+ }
81
+ buildConfig(spec, tools, options) {
82
+ const config = {
83
+ model: options.model ?? "claude-sonnet-4-20250514",
84
+ system: this.buildSystemPrompt(spec, options),
85
+ tools,
86
+ max_turns: spec.maxSteps ?? 10
87
+ };
88
+ if (options.computerUse) {
89
+ config.computer_use = true;
90
+ }
91
+ if (options.extendedThinking) {
92
+ config.extended_thinking = true;
93
+ }
94
+ if (options.mcpServers && options.mcpServers.length > 0) {
95
+ config.mcp_servers = options.mcpServers;
96
+ }
97
+ return config;
98
+ }
99
+ buildSystemPrompt(spec, options) {
100
+ const parts = [];
101
+ parts.push(spec.instructions);
102
+ if (spec.knowledge && spec.knowledge.length > 0) {
103
+ parts.push("");
104
+ parts.push("## Knowledge Sources");
105
+ for (const k of spec.knowledge) {
106
+ if (k.instructions) {
107
+ parts.push(`- ${k.key}: ${k.instructions}`);
108
+ }
109
+ }
110
+ }
111
+ if (spec.policy) {
112
+ parts.push("");
113
+ parts.push("## Policy");
114
+ if (spec.policy.confidence?.min) {
115
+ parts.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
116
+ }
117
+ if (spec.policy.escalation) {
118
+ parts.push("- Escalation policy is configured");
119
+ }
120
+ }
121
+ if (options.metadata) {
122
+ parts.push("");
123
+ parts.push("## Additional Context");
124
+ for (const [key, value] of Object.entries(options.metadata)) {
125
+ parts.push(`- ${key}: ${String(value)}`);
126
+ }
127
+ }
128
+ return parts.join(`
129
+ `);
130
+ }
131
+ exportTools(spec) {
132
+ return spec.tools.map((tool) => ({
133
+ name: tool.name,
134
+ description: tool.description ?? `Execute ${tool.name}`,
135
+ input_schema: this.normalizeSchema(tool.schema),
136
+ requires_confirmation: tool.requiresApproval ?? !tool.automationSafe
137
+ }));
138
+ }
139
+ normalizeSchema(schema) {
140
+ if (!schema) {
141
+ return { type: "object" };
142
+ }
143
+ if (schema.type === "object") {
144
+ return {
145
+ type: "object",
146
+ properties: schema.properties,
147
+ required: schema.required
148
+ };
149
+ }
150
+ return {
151
+ type: "object",
152
+ properties: { value: schema },
153
+ required: ["value"]
154
+ };
155
+ }
156
+ generateClaudeMd(spec, options) {
157
+ const lines = [];
158
+ lines.push("# Agent Configuration");
159
+ lines.push("");
160
+ if (spec.description) {
161
+ lines.push(`> ${spec.description}`);
162
+ lines.push("");
163
+ }
164
+ lines.push("## Metadata");
165
+ lines.push("");
166
+ lines.push(`- **Name**: ${spec.meta.key}`);
167
+ lines.push(`- **Version**: ${spec.meta.version}`);
168
+ if (spec.meta.owners && spec.meta.owners.length > 0) {
169
+ lines.push(`- **Owners**: ${spec.meta.owners.join(", ")}`);
170
+ }
171
+ if (options.model) {
172
+ lines.push(`- **Model**: ${options.model}`);
173
+ }
174
+ lines.push("");
175
+ lines.push("## Instructions");
176
+ lines.push("");
177
+ lines.push(spec.instructions);
178
+ lines.push("");
179
+ if (spec.tools.length > 0) {
180
+ lines.push("## Available Tools");
181
+ lines.push("");
182
+ for (const tool of spec.tools) {
183
+ const flags = [];
184
+ if (tool.requiresApproval) {
185
+ flags.push("requires approval");
186
+ }
187
+ if (tool.automationSafe === false) {
188
+ flags.push("not automation safe");
189
+ }
190
+ const flagStr = flags.length > 0 ? ` (${flags.join(", ")})` : "";
191
+ lines.push(`### ${tool.name}${flagStr}`);
192
+ lines.push("");
193
+ if (tool.description) {
194
+ lines.push(tool.description);
195
+ lines.push("");
196
+ }
197
+ if (tool.schema) {
198
+ lines.push("**Parameters:**");
199
+ lines.push("```json");
200
+ lines.push(JSON.stringify(tool.schema, null, 2));
201
+ lines.push("```");
202
+ lines.push("");
203
+ }
204
+ }
205
+ }
206
+ if (spec.knowledge && spec.knowledge.length > 0) {
207
+ lines.push("## Knowledge Sources");
208
+ lines.push("");
209
+ for (const k of spec.knowledge) {
210
+ const required = k.required ? "(required)" : "(optional)";
211
+ lines.push(`- **${k.key}** ${required}`);
212
+ if (k.instructions) {
213
+ lines.push(` - ${k.instructions}`);
214
+ }
215
+ }
216
+ lines.push("");
217
+ }
218
+ if (spec.policy) {
219
+ lines.push("## Policy");
220
+ lines.push("");
221
+ if (spec.policy.confidence?.min) {
222
+ lines.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
223
+ }
224
+ if (spec.policy.escalation) {
225
+ lines.push("- Escalation policy configured");
226
+ }
227
+ if (spec.policy.flags && spec.policy.flags.length > 0) {
228
+ lines.push(`- Feature flags: ${spec.policy.flags.join(", ")}`);
229
+ }
230
+ lines.push("");
231
+ }
232
+ if (options.mcpServers && options.mcpServers.length > 0) {
233
+ lines.push("## MCP Servers");
234
+ lines.push("");
235
+ for (const server of options.mcpServers) {
236
+ lines.push(`- **${server.name}**: \`${server.command}${server.args ? " " + server.args.join(" ") : ""}\``);
237
+ }
238
+ lines.push("");
239
+ }
240
+ lines.push("---");
241
+ lines.push("");
242
+ lines.push(`*Generated from ContractSpec: ${agentKey(spec.meta)}*`);
243
+ return lines.join(`
244
+ `);
245
+ }
246
+ }
247
+ function exportToClaudeAgent(spec, options) {
248
+ const exporter = new ClaudeAgentExporter;
249
+ return exporter.export(spec, options);
250
+ }
251
+ function generateClaudeMd(spec, options) {
252
+ const exporter = new ClaudeAgentExporter;
253
+ const result = exporter.export(spec, { ...options, generateClaudeMd: true });
254
+ return result.claudeMd ?? "";
255
+ }
256
+ function validateForClaudeAgent(spec) {
257
+ const exporter = new ClaudeAgentExporter;
258
+ return exporter.validate(spec);
259
+ }
260
+ export {
261
+ validateForClaudeAgent,
262
+ generateClaudeMd,
263
+ exportToClaudeAgent,
264
+ ClaudeAgentExporter
265
+ };