@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,516 @@
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/providers/opencode-sdk/tool-bridge.ts
43
+ function specToolToOpenCodeTool(tool) {
44
+ return {
45
+ name: tool.name,
46
+ description: tool.description ?? `Execute ${tool.name}`,
47
+ parameters: normalizeToOpenCodeParameters(tool.schema),
48
+ permission: getPermissionLevel(tool)
49
+ };
50
+ }
51
+ function specToolsToOpenCodeTools(tools) {
52
+ return tools.map(specToolToOpenCodeTool);
53
+ }
54
+ function getPermissionLevel(tool) {
55
+ if (tool.requiresApproval) {
56
+ return "ask";
57
+ }
58
+ if (tool.automationSafe === false) {
59
+ return "ask";
60
+ }
61
+ return "allow";
62
+ }
63
+ function openCodeToolToSpecTool(openCodeTool) {
64
+ return {
65
+ name: openCodeTool.name,
66
+ description: openCodeTool.description,
67
+ schema: openCodeTool.parameters,
68
+ requiresApproval: openCodeTool.permission === "ask",
69
+ automationSafe: openCodeTool.permission === "allow"
70
+ };
71
+ }
72
+ function openCodeToolsToSpecTools(openCodeTools) {
73
+ return openCodeTools.map(openCodeToolToSpecTool);
74
+ }
75
+ function specToolToExternalToolForOpenCode(tool, handler, context) {
76
+ return {
77
+ name: tool.name,
78
+ description: tool.description ?? `Execute ${tool.name}`,
79
+ inputSchema: tool.schema ?? { type: "object" },
80
+ requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
81
+ execute: handler ? async (input) => {
82
+ const fullContext = {
83
+ agentId: context?.agentId ?? "unknown",
84
+ sessionId: context?.sessionId ?? "unknown",
85
+ tenantId: context?.tenantId,
86
+ actorId: context?.actorId,
87
+ metadata: context?.metadata,
88
+ signal: context?.signal
89
+ };
90
+ return handler(input, fullContext);
91
+ } : undefined
92
+ };
93
+ }
94
+ function normalizeToOpenCodeParameters(schema) {
95
+ if (!schema) {
96
+ return { type: "object" };
97
+ }
98
+ if (schema.type === "object") {
99
+ return {
100
+ type: "object",
101
+ properties: schema.properties,
102
+ required: schema.required
103
+ };
104
+ }
105
+ return {
106
+ type: "object",
107
+ properties: {
108
+ value: convertToOpenCodeParameter(schema)
109
+ },
110
+ required: ["value"]
111
+ };
112
+ }
113
+ function convertToOpenCodeParameter(schema) {
114
+ const param = {
115
+ type: schema.type ?? "string"
116
+ };
117
+ if (schema.description) {
118
+ param.description = schema.description;
119
+ }
120
+ if (schema.enum) {
121
+ param.enum = schema.enum;
122
+ }
123
+ if (schema.default !== undefined) {
124
+ param.default = schema.default;
125
+ }
126
+ return param;
127
+ }
128
+ function createToolHandlerMap(tools) {
129
+ const handlers = new Map;
130
+ for (const [name, tool] of Object.entries(tools)) {
131
+ if (tool.execute) {
132
+ handlers.set(name, tool.execute);
133
+ }
134
+ }
135
+ return handlers;
136
+ }
137
+ async function executeToolCall(toolCall, handlers) {
138
+ const handler = handlers.get(toolCall.name);
139
+ if (!handler) {
140
+ return {
141
+ tool_call_id: toolCall.id,
142
+ output: `Error: Tool '${toolCall.name}' not found`,
143
+ is_error: true
144
+ };
145
+ }
146
+ try {
147
+ const result = await handler(toolCall.arguments);
148
+ return {
149
+ tool_call_id: toolCall.id,
150
+ output: typeof result === "string" ? result : JSON.stringify(result)
151
+ };
152
+ } catch (error) {
153
+ return {
154
+ tool_call_id: toolCall.id,
155
+ output: `Error: ${error instanceof Error ? error.message : String(error)}`,
156
+ is_error: true
157
+ };
158
+ }
159
+ }
160
+
161
+ // src/providers/opencode-sdk/agent-bridge.ts
162
+ function inferAgentType(spec) {
163
+ const hasCodeTools = spec.tools.some((tool) => [
164
+ "write_file",
165
+ "edit_file",
166
+ "create_file",
167
+ "delete_file",
168
+ "bash",
169
+ "execute",
170
+ "run_command",
171
+ "terminal"
172
+ ].includes(tool.name.toLowerCase()));
173
+ const instructionsLower = spec.instructions.toLowerCase();
174
+ const hasPlanningKeywords = /\b(plan|design|architect|strategy|analyze|review|assess|evaluate)\b/.test(instructionsLower);
175
+ const hasExplorationKeywords = /\b(search|find|explore|discover|locate|grep|pattern)\b/.test(instructionsLower);
176
+ const hasResearchKeywords = /\b(research|investigate|understand|learn|gather|collect)\b/.test(instructionsLower);
177
+ if (hasCodeTools) {
178
+ return "build";
179
+ }
180
+ if (hasPlanningKeywords && !hasCodeTools) {
181
+ return "plan";
182
+ }
183
+ if (hasExplorationKeywords && !hasResearchKeywords) {
184
+ return "explore";
185
+ }
186
+ return "general";
187
+ }
188
+ function specToOpenCodeConfig(spec, options) {
189
+ const agentType = options?.agentType ?? inferAgentType(spec);
190
+ return {
191
+ name: spec.meta.key,
192
+ version: spec.meta.version,
193
+ description: spec.description ?? spec.meta.description,
194
+ instructions: spec.instructions,
195
+ agent_type: agentType,
196
+ tools: specToolsToOpenCodeTools(spec.tools),
197
+ config: {
198
+ max_steps: options?.maxSteps ?? spec.maxSteps ?? 10,
199
+ temperature: options?.temperature ?? 0.7,
200
+ model: options?.model,
201
+ permissions: buildPermissions(spec)
202
+ }
203
+ };
204
+ }
205
+ function buildPermissions(spec) {
206
+ const permissions = {};
207
+ for (const tool of spec.tools) {
208
+ if (tool.requiresApproval) {
209
+ permissions[tool.name] = "ask";
210
+ } else if (tool.automationSafe === false) {
211
+ permissions[tool.name] = "ask";
212
+ } else {
213
+ permissions[tool.name] = "allow";
214
+ }
215
+ }
216
+ return permissions;
217
+ }
218
+ function specToOpenCodeMarkdown(spec, options) {
219
+ const agentType = options?.agentType ?? inferAgentType(spec);
220
+ return {
221
+ frontmatter: {
222
+ name: spec.meta.key,
223
+ type: agentType,
224
+ version: spec.meta.version,
225
+ model: options?.model,
226
+ temperature: options?.temperature ?? 0.7,
227
+ max_steps: options?.maxSteps ?? spec.maxSteps ?? 10,
228
+ tools: spec.tools.map((t) => t.name)
229
+ },
230
+ body: buildMarkdownBody(spec)
231
+ };
232
+ }
233
+ function buildMarkdownBody(spec) {
234
+ const lines = [];
235
+ lines.push(`# ${spec.meta.key}`);
236
+ lines.push("");
237
+ if (spec.description ?? spec.meta.description) {
238
+ lines.push(spec.description ?? spec.meta.description ?? "");
239
+ lines.push("");
240
+ }
241
+ lines.push("## Instructions");
242
+ lines.push("");
243
+ lines.push(spec.instructions);
244
+ lines.push("");
245
+ if (spec.tools.length > 0) {
246
+ lines.push("## Tools");
247
+ lines.push("");
248
+ for (const tool of spec.tools) {
249
+ const permission = tool.requiresApproval ? "(requires approval)" : tool.automationSafe === false ? "(ask mode)" : "";
250
+ lines.push(`- **${tool.name}**: ${tool.description ?? "No description"} ${permission}`.trim());
251
+ }
252
+ lines.push("");
253
+ }
254
+ if (spec.knowledge && spec.knowledge.length > 0) {
255
+ lines.push("## Knowledge Sources");
256
+ lines.push("");
257
+ for (const k of spec.knowledge) {
258
+ const required = k.required ? "(required)" : "(optional)";
259
+ lines.push(`- ${k.key} ${required}`);
260
+ }
261
+ lines.push("");
262
+ }
263
+ return lines.join(`
264
+ `);
265
+ }
266
+ function serializeOpenCodeMarkdown(markdown) {
267
+ const lines = [];
268
+ lines.push("---");
269
+ lines.push(`name: ${markdown.frontmatter.name}`);
270
+ lines.push(`type: ${markdown.frontmatter.type}`);
271
+ if (markdown.frontmatter.version) {
272
+ lines.push(`version: ${markdown.frontmatter.version}`);
273
+ }
274
+ if (markdown.frontmatter.model) {
275
+ lines.push(`model: ${markdown.frontmatter.model}`);
276
+ }
277
+ if (markdown.frontmatter.temperature !== undefined) {
278
+ lines.push(`temperature: ${markdown.frontmatter.temperature}`);
279
+ }
280
+ if (markdown.frontmatter.max_steps !== undefined) {
281
+ lines.push(`max_steps: ${markdown.frontmatter.max_steps}`);
282
+ }
283
+ if (markdown.frontmatter.tools && markdown.frontmatter.tools.length > 0) {
284
+ lines.push(`tools:`);
285
+ for (const tool of markdown.frontmatter.tools) {
286
+ lines.push(` - ${tool}`);
287
+ }
288
+ }
289
+ lines.push("---");
290
+ lines.push("");
291
+ lines.push(markdown.body);
292
+ return lines.join(`
293
+ `);
294
+ }
295
+ function openCodeConfigToSpec(config) {
296
+ return {
297
+ meta: {
298
+ key: config.name,
299
+ version: config.version ?? "1.0.0",
300
+ description: config.description ?? "",
301
+ stability: "experimental",
302
+ owners: [],
303
+ tags: []
304
+ },
305
+ description: config.description,
306
+ instructions: config.instructions ?? "",
307
+ tools: config.tools?.map((tool) => ({
308
+ name: tool.name,
309
+ description: tool.description,
310
+ schema: tool.parameters,
311
+ requiresApproval: tool.permission === "ask",
312
+ automationSafe: tool.permission === "allow"
313
+ })) ?? [],
314
+ maxSteps: config.config?.max_steps ?? 10
315
+ };
316
+ }
317
+ var init_agent_bridge = () => {};
318
+
319
+ // src/exporters/opencode-exporter.ts
320
+ init_agent_bridge();
321
+
322
+ class OpenCodeExporter {
323
+ format = "opencode";
324
+ export(spec, options = {}) {
325
+ const jsonConfig = this.buildJsonConfig(spec, options);
326
+ const markdownConfig = this.generateMarkdown(spec, jsonConfig, options);
327
+ return {
328
+ jsonConfig,
329
+ markdownConfig,
330
+ exportedAt: new Date,
331
+ sourceSpec: agentKey(spec.meta)
332
+ };
333
+ }
334
+ exportMany(specs, options = {}) {
335
+ return specs.map((spec) => this.export(spec, options));
336
+ }
337
+ validate(spec) {
338
+ const errors = [];
339
+ if (!spec.meta?.key) {
340
+ errors.push("Spec must have a meta.key");
341
+ }
342
+ if (!spec.instructions) {
343
+ errors.push("Spec must have instructions");
344
+ }
345
+ for (const tool of spec.tools ?? []) {
346
+ if (!tool.name) {
347
+ errors.push("All tools must have a name");
348
+ }
349
+ if (tool.name && !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(tool.name)) {
350
+ errors.push(`Tool name '${tool.name}' should be a valid identifier (letters, numbers, underscores)`);
351
+ }
352
+ }
353
+ return { valid: errors.length === 0, errors };
354
+ }
355
+ buildJsonConfig(spec, options) {
356
+ const agentType = options.agentType ?? inferAgentType(spec);
357
+ return {
358
+ name: spec.meta.key,
359
+ version: spec.meta.version,
360
+ description: spec.description,
361
+ type: agentType,
362
+ instructions: spec.instructions,
363
+ tools: this.exportTools(spec),
364
+ config: {
365
+ max_steps: options.maxSteps ?? spec.maxSteps ?? 10,
366
+ temperature: options.temperature ?? 0.7,
367
+ model: options.model
368
+ }
369
+ };
370
+ }
371
+ exportTools(spec) {
372
+ return spec.tools.map((tool) => ({
373
+ name: tool.name,
374
+ description: tool.description ?? `Execute ${tool.name}`,
375
+ schema: tool.schema ?? { type: "object" },
376
+ requires_approval: tool.requiresApproval ?? !tool.automationSafe
377
+ }));
378
+ }
379
+ generateMarkdown(spec, jsonConfig, options) {
380
+ const lines = [];
381
+ lines.push("---");
382
+ lines.push(`name: ${jsonConfig.name}`);
383
+ lines.push(`type: ${jsonConfig.type}`);
384
+ if (jsonConfig.version) {
385
+ lines.push(`version: ${jsonConfig.version}`);
386
+ }
387
+ if (jsonConfig.config.model) {
388
+ lines.push(`model: ${jsonConfig.config.model}`);
389
+ }
390
+ if (jsonConfig.config.temperature !== undefined) {
391
+ lines.push(`temperature: ${jsonConfig.config.temperature}`);
392
+ }
393
+ if (jsonConfig.config.max_steps !== undefined) {
394
+ lines.push(`max_steps: ${jsonConfig.config.max_steps}`);
395
+ }
396
+ if (jsonConfig.tools.length > 0) {
397
+ lines.push("tools:");
398
+ for (const tool of jsonConfig.tools) {
399
+ const permission = tool.requires_approval ? " # requires approval" : "";
400
+ lines.push(` - ${tool.name}${permission}`);
401
+ }
402
+ }
403
+ lines.push("---");
404
+ lines.push("");
405
+ lines.push(`# ${spec.meta.key}`);
406
+ lines.push("");
407
+ if (spec.description) {
408
+ lines.push(spec.description);
409
+ lines.push("");
410
+ }
411
+ lines.push(`> Agent type: **${jsonConfig.type}**`);
412
+ lines.push("");
413
+ lines.push(this.getAgentTypeDescription(jsonConfig.type));
414
+ lines.push("");
415
+ lines.push("## Instructions");
416
+ lines.push("");
417
+ lines.push(spec.instructions);
418
+ lines.push("");
419
+ if (spec.tools.length > 0) {
420
+ lines.push("## Tools");
421
+ lines.push("");
422
+ for (const tool of spec.tools) {
423
+ const approval = tool.requiresApproval ? " *(requires approval)*" : "";
424
+ const safe = tool.automationSafe === false ? " *(not automation safe)*" : "";
425
+ lines.push(`### ${tool.name}${approval}${safe}`);
426
+ lines.push("");
427
+ if (tool.description) {
428
+ lines.push(tool.description);
429
+ lines.push("");
430
+ }
431
+ if (tool.schema && options.includeComments !== false) {
432
+ lines.push("**Parameters:**");
433
+ lines.push("");
434
+ lines.push("```json");
435
+ lines.push(JSON.stringify(tool.schema, null, options.prettyPrint !== false ? 2 : 0));
436
+ lines.push("```");
437
+ lines.push("");
438
+ }
439
+ }
440
+ }
441
+ if (spec.knowledge && spec.knowledge.length > 0) {
442
+ lines.push("## Knowledge Sources");
443
+ lines.push("");
444
+ for (const k of spec.knowledge) {
445
+ const required = k.required ? "(required)" : "(optional)";
446
+ lines.push(`- **${k.key}** ${required}`);
447
+ if (k.instructions) {
448
+ lines.push(` - ${k.instructions}`);
449
+ }
450
+ }
451
+ lines.push("");
452
+ }
453
+ if (spec.policy) {
454
+ lines.push("## Policy");
455
+ lines.push("");
456
+ if (spec.policy.confidence?.min) {
457
+ lines.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
458
+ }
459
+ if (spec.policy.escalation) {
460
+ lines.push("- Escalation policy configured");
461
+ }
462
+ lines.push("");
463
+ }
464
+ lines.push("## Configuration");
465
+ lines.push("");
466
+ lines.push("```json");
467
+ lines.push(JSON.stringify(jsonConfig.config, null, options.prettyPrint !== false ? 2 : 0));
468
+ lines.push("```");
469
+ lines.push("");
470
+ lines.push("---");
471
+ lines.push("");
472
+ lines.push(`*Generated from ContractSpec: ${agentKey(spec.meta)}*`);
473
+ lines.push(`*Exported at: ${new Date().toISOString()}*`);
474
+ return lines.join(`
475
+ `);
476
+ }
477
+ getAgentTypeDescription(type) {
478
+ switch (type) {
479
+ case "build":
480
+ return "Primary agent with full tool access for code generation and modification.";
481
+ case "plan":
482
+ return "Restricted agent for analysis and planning. File edits and bash commands require approval.";
483
+ case "general":
484
+ return "General-purpose subagent for complex questions and multi-step tasks.";
485
+ case "explore":
486
+ return "Fast subagent optimized for codebase exploration and pattern searching.";
487
+ default:
488
+ return "";
489
+ }
490
+ }
491
+ }
492
+ function exportToOpenCode(spec, options) {
493
+ const exporter = new OpenCodeExporter;
494
+ return exporter.export(spec, options);
495
+ }
496
+ function generateOpenCodeMarkdown(spec, options) {
497
+ const exporter = new OpenCodeExporter;
498
+ const result = exporter.export(spec, options);
499
+ return result.markdownConfig;
500
+ }
501
+ function generateOpenCodeJSON(spec, options) {
502
+ const exporter = new OpenCodeExporter;
503
+ const result = exporter.export(spec, options);
504
+ return result.jsonConfig;
505
+ }
506
+ function validateForOpenCode(spec) {
507
+ const exporter = new OpenCodeExporter;
508
+ return exporter.validate(spec);
509
+ }
510
+ export {
511
+ validateForOpenCode,
512
+ generateOpenCodeMarkdown,
513
+ generateOpenCodeJSON,
514
+ exportToOpenCode,
515
+ OpenCodeExporter
516
+ };
File without changes