@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,141 @@
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/opencode-sdk/tool-bridge.ts
16
+ function specToolToOpenCodeTool(tool) {
17
+ return {
18
+ name: tool.name,
19
+ description: tool.description ?? `Execute ${tool.name}`,
20
+ parameters: normalizeToOpenCodeParameters(tool.schema),
21
+ permission: getPermissionLevel(tool)
22
+ };
23
+ }
24
+ function specToolsToOpenCodeTools(tools) {
25
+ return tools.map(specToolToOpenCodeTool);
26
+ }
27
+ function getPermissionLevel(tool) {
28
+ if (tool.requiresApproval) {
29
+ return "ask";
30
+ }
31
+ if (tool.automationSafe === false) {
32
+ return "ask";
33
+ }
34
+ return "allow";
35
+ }
36
+ function openCodeToolToSpecTool(openCodeTool) {
37
+ return {
38
+ name: openCodeTool.name,
39
+ description: openCodeTool.description,
40
+ schema: openCodeTool.parameters,
41
+ requiresApproval: openCodeTool.permission === "ask",
42
+ automationSafe: openCodeTool.permission === "allow"
43
+ };
44
+ }
45
+ function openCodeToolsToSpecTools(openCodeTools) {
46
+ return openCodeTools.map(openCodeToolToSpecTool);
47
+ }
48
+ function specToolToExternalToolForOpenCode(tool, handler, context) {
49
+ return {
50
+ name: tool.name,
51
+ description: tool.description ?? `Execute ${tool.name}`,
52
+ inputSchema: tool.schema ?? { type: "object" },
53
+ requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
54
+ execute: handler ? async (input) => {
55
+ const fullContext = {
56
+ agentId: context?.agentId ?? "unknown",
57
+ sessionId: context?.sessionId ?? "unknown",
58
+ tenantId: context?.tenantId,
59
+ actorId: context?.actorId,
60
+ metadata: context?.metadata,
61
+ signal: context?.signal
62
+ };
63
+ return handler(input, fullContext);
64
+ } : undefined
65
+ };
66
+ }
67
+ function normalizeToOpenCodeParameters(schema) {
68
+ if (!schema) {
69
+ return { type: "object" };
70
+ }
71
+ if (schema.type === "object") {
72
+ return {
73
+ type: "object",
74
+ properties: schema.properties,
75
+ required: schema.required
76
+ };
77
+ }
78
+ return {
79
+ type: "object",
80
+ properties: {
81
+ value: convertToOpenCodeParameter(schema)
82
+ },
83
+ required: ["value"]
84
+ };
85
+ }
86
+ function convertToOpenCodeParameter(schema) {
87
+ const param = {
88
+ type: schema.type ?? "string"
89
+ };
90
+ if (schema.description) {
91
+ param.description = schema.description;
92
+ }
93
+ if (schema.enum) {
94
+ param.enum = schema.enum;
95
+ }
96
+ if (schema.default !== undefined) {
97
+ param.default = schema.default;
98
+ }
99
+ return param;
100
+ }
101
+ function createToolHandlerMap(tools) {
102
+ const handlers = new Map;
103
+ for (const [name, tool] of Object.entries(tools)) {
104
+ if (tool.execute) {
105
+ handlers.set(name, tool.execute);
106
+ }
107
+ }
108
+ return handlers;
109
+ }
110
+ async function executeToolCall(toolCall, handlers) {
111
+ const handler = handlers.get(toolCall.name);
112
+ if (!handler) {
113
+ return {
114
+ tool_call_id: toolCall.id,
115
+ output: `Error: Tool '${toolCall.name}' not found`,
116
+ is_error: true
117
+ };
118
+ }
119
+ try {
120
+ const result = await handler(toolCall.arguments);
121
+ return {
122
+ tool_call_id: toolCall.id,
123
+ output: typeof result === "string" ? result : JSON.stringify(result)
124
+ };
125
+ } catch (error) {
126
+ return {
127
+ tool_call_id: toolCall.id,
128
+ output: `Error: ${error instanceof Error ? error.message : String(error)}`,
129
+ is_error: true
130
+ };
131
+ }
132
+ }
133
+ export {
134
+ specToolsToOpenCodeTools,
135
+ specToolToOpenCodeTool,
136
+ specToolToExternalToolForOpenCode,
137
+ openCodeToolsToSpecTools,
138
+ openCodeToolToSpecTool,
139
+ executeToolCall,
140
+ createToolHandlerMap
141
+ };
@@ -0,0 +1,89 @@
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/types.ts
16
+ var ExternalProviderError, ProviderNotAvailableError, ProviderExecutionError, ContextCreationError;
17
+ var init_types = __esm(() => {
18
+ ExternalProviderError = class ExternalProviderError extends Error {
19
+ provider;
20
+ code;
21
+ cause;
22
+ constructor(message, provider, code, cause) {
23
+ super(message);
24
+ this.provider = provider;
25
+ this.code = code;
26
+ this.cause = cause;
27
+ this.name = "ExternalProviderError";
28
+ }
29
+ };
30
+ ProviderNotAvailableError = class ProviderNotAvailableError extends ExternalProviderError {
31
+ constructor(provider, reason) {
32
+ super(`Provider '${provider}' is not available${reason ? `: ${reason}` : ""}`, provider, "PROVIDER_NOT_AVAILABLE");
33
+ this.name = "ProviderNotAvailableError";
34
+ }
35
+ };
36
+ ProviderExecutionError = class ProviderExecutionError extends ExternalProviderError {
37
+ constructor(provider, message, cause) {
38
+ super(message, provider, "EXECUTION_FAILED", cause);
39
+ this.name = "ProviderExecutionError";
40
+ }
41
+ };
42
+ ContextCreationError = class ContextCreationError extends ExternalProviderError {
43
+ constructor(provider, message, cause) {
44
+ super(message, provider, "CONTEXT_CREATION_FAILED", cause);
45
+ this.name = "ContextCreationError";
46
+ }
47
+ };
48
+ });
49
+
50
+ // src/providers/registry.ts
51
+ init_types();
52
+
53
+ class InMemoryProviderRegistry {
54
+ providers = new Map;
55
+ get(name) {
56
+ return this.providers.get(name);
57
+ }
58
+ register(name, provider) {
59
+ this.providers.set(name, provider);
60
+ }
61
+ list() {
62
+ return Array.from(this.providers.keys());
63
+ }
64
+ isAvailable(name) {
65
+ const provider = this.providers.get(name);
66
+ return provider !== undefined && provider.isAvailable();
67
+ }
68
+ require(name) {
69
+ const provider = this.providers.get(name);
70
+ if (!provider) {
71
+ throw new ProviderNotAvailableError(name, "not registered");
72
+ }
73
+ if (!provider.isAvailable()) {
74
+ throw new ProviderNotAvailableError(name, "dependencies not installed or not configured");
75
+ }
76
+ return provider;
77
+ }
78
+ clear() {
79
+ this.providers.clear();
80
+ }
81
+ }
82
+ function createProviderRegistry() {
83
+ return new InMemoryProviderRegistry;
84
+ }
85
+ var defaultProviderRegistry = createProviderRegistry();
86
+ export {
87
+ defaultProviderRegistry,
88
+ createProviderRegistry
89
+ };
@@ -0,0 +1,56 @@
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/types.ts
16
+ var ExternalProviderError, ProviderNotAvailableError, ProviderExecutionError, ContextCreationError;
17
+ var init_types = __esm(() => {
18
+ ExternalProviderError = class ExternalProviderError extends Error {
19
+ provider;
20
+ code;
21
+ cause;
22
+ constructor(message, provider, code, cause) {
23
+ super(message);
24
+ this.provider = provider;
25
+ this.code = code;
26
+ this.cause = cause;
27
+ this.name = "ExternalProviderError";
28
+ }
29
+ };
30
+ ProviderNotAvailableError = class ProviderNotAvailableError extends ExternalProviderError {
31
+ constructor(provider, reason) {
32
+ super(`Provider '${provider}' is not available${reason ? `: ${reason}` : ""}`, provider, "PROVIDER_NOT_AVAILABLE");
33
+ this.name = "ProviderNotAvailableError";
34
+ }
35
+ };
36
+ ProviderExecutionError = class ProviderExecutionError extends ExternalProviderError {
37
+ constructor(provider, message, cause) {
38
+ super(message, provider, "EXECUTION_FAILED", cause);
39
+ this.name = "ProviderExecutionError";
40
+ }
41
+ };
42
+ ContextCreationError = class ContextCreationError extends ExternalProviderError {
43
+ constructor(provider, message, cause) {
44
+ super(message, provider, "CONTEXT_CREATION_FAILED", cause);
45
+ this.name = "ContextCreationError";
46
+ }
47
+ };
48
+ });
49
+ init_types();
50
+
51
+ export {
52
+ ProviderNotAvailableError,
53
+ ProviderExecutionError,
54
+ ExternalProviderError,
55
+ ContextCreationError
56
+ };
@@ -0,0 +1,195 @@
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/schema/json-schema-to-zod.ts
16
+ import { z } from "zod";
17
+ function jsonSchemaToZod(schema) {
18
+ const s = schema;
19
+ const makeNullable = (zodSchema) => {
20
+ return s.nullable ? z.union([zodSchema, z.null()]) : zodSchema;
21
+ };
22
+ if (s.const !== undefined) {
23
+ return z.literal(s.const);
24
+ }
25
+ if (s.enum) {
26
+ const values = s.enum;
27
+ return makeNullable(z.enum(values.map(String)));
28
+ }
29
+ if (s.anyOf && s.anyOf.length > 0) {
30
+ const schemas = s.anyOf.map((sub) => jsonSchemaToZod(sub));
31
+ if (schemas.length === 1)
32
+ return schemas[0] ?? z.unknown();
33
+ return z.union([
34
+ schemas[0] ?? z.unknown(),
35
+ schemas[1] ?? z.unknown(),
36
+ ...schemas.slice(2)
37
+ ]);
38
+ }
39
+ if (s.oneOf && s.oneOf.length > 0) {
40
+ const schemas = s.oneOf.map((sub) => jsonSchemaToZod(sub));
41
+ if (schemas.length === 1)
42
+ return schemas[0] ?? z.unknown();
43
+ return z.union([
44
+ schemas[0] ?? z.unknown(),
45
+ schemas[1] ?? z.unknown(),
46
+ ...schemas.slice(2)
47
+ ]);
48
+ }
49
+ if (s.allOf && s.allOf.length > 0) {
50
+ const schemas = s.allOf.map((sub) => jsonSchemaToZod(sub));
51
+ return schemas.reduce((acc, curr) => z.intersection(acc, curr));
52
+ }
53
+ switch (s.type) {
54
+ case "string":
55
+ return makeNullable(buildStringSchema(s));
56
+ case "number":
57
+ case "integer":
58
+ return makeNullable(buildNumberSchema(s));
59
+ case "boolean":
60
+ return makeNullable(z.boolean());
61
+ case "null":
62
+ return z.null();
63
+ case "array":
64
+ return makeNullable(buildArraySchema(s));
65
+ case "object":
66
+ return makeNullable(buildObjectSchema(s));
67
+ default:
68
+ return z.unknown();
69
+ }
70
+ }
71
+ function buildStringSchema(schema) {
72
+ let zodSchema = z.string();
73
+ if (schema.description) {
74
+ zodSchema = zodSchema.describe(schema.description);
75
+ }
76
+ switch (schema.format) {
77
+ case "email":
78
+ zodSchema = zodSchema.email();
79
+ break;
80
+ case "uri":
81
+ case "url":
82
+ zodSchema = zodSchema.url();
83
+ break;
84
+ case "uuid":
85
+ zodSchema = zodSchema.uuid();
86
+ break;
87
+ case "date-time":
88
+ zodSchema = zodSchema.datetime();
89
+ break;
90
+ case "date":
91
+ zodSchema = zodSchema.date();
92
+ break;
93
+ }
94
+ if (schema.minLength !== undefined) {
95
+ zodSchema = zodSchema.min(schema.minLength);
96
+ }
97
+ if (schema.maxLength !== undefined) {
98
+ zodSchema = zodSchema.max(schema.maxLength);
99
+ }
100
+ if (schema.pattern) {
101
+ zodSchema = zodSchema.regex(new RegExp(schema.pattern));
102
+ }
103
+ return zodSchema;
104
+ }
105
+ function buildNumberSchema(schema) {
106
+ let zodSchema = schema.type === "integer" ? z.number().int() : z.number();
107
+ if (schema.description) {
108
+ zodSchema = zodSchema.describe(schema.description);
109
+ }
110
+ if (schema.minimum !== undefined) {
111
+ zodSchema = zodSchema.min(schema.minimum);
112
+ }
113
+ if (schema.maximum !== undefined) {
114
+ zodSchema = zodSchema.max(schema.maximum);
115
+ }
116
+ return zodSchema;
117
+ }
118
+ function buildArraySchema(schema) {
119
+ const itemsSchema = schema.items ? jsonSchemaToZod(schema.items) : z.unknown();
120
+ let zodSchema = z.array(itemsSchema);
121
+ if (schema.description) {
122
+ zodSchema = zodSchema.describe(schema.description);
123
+ }
124
+ return zodSchema;
125
+ }
126
+ function buildObjectSchema(schema) {
127
+ const properties = schema.properties ?? {};
128
+ const required = new Set(schema.required ?? []);
129
+ const shape = {};
130
+ for (const [key, propSchema] of Object.entries(properties)) {
131
+ const zodProp = jsonSchemaToZod(propSchema);
132
+ shape[key] = required.has(key) ? zodProp : zodProp.optional();
133
+ }
134
+ let zodSchema = z.object(shape);
135
+ if (schema.description) {
136
+ zodSchema = zodSchema.describe(schema.description);
137
+ }
138
+ return zodSchema;
139
+ }
140
+ function jsonSchemaToZodSafe(schema) {
141
+ if (!schema || Object.keys(schema).length === 0) {
142
+ return z.object({});
143
+ }
144
+ return jsonSchemaToZod(schema);
145
+ }
146
+ var init_json_schema_to_zod = () => {};
147
+
148
+ // src/schema/schema-output.ts
149
+ init_json_schema_to_zod();
150
+ import { Output } from "ai";
151
+ function jsonSchemaToOutput(schema) {
152
+ const zodSchema = jsonSchemaToZod(schema);
153
+ return Output.object({
154
+ schema: zodSchema
155
+ });
156
+ }
157
+ function jsonSchemaToArrayOutput(itemSchema) {
158
+ const zodSchema = jsonSchemaToZod(itemSchema);
159
+ return Output.array({
160
+ element: zodSchema
161
+ });
162
+ }
163
+ function enumToChoiceOutput(choices) {
164
+ return Output.choice({
165
+ options: choices
166
+ });
167
+ }
168
+ function zodToOutput(schema) {
169
+ return Output.object({
170
+ schema
171
+ });
172
+ }
173
+ function textOutput() {
174
+ return Output.text();
175
+ }
176
+ var SchemaOutput = {
177
+ fromJsonSchema: jsonSchemaToOutput,
178
+ arrayFromJsonSchema: jsonSchemaToArrayOutput,
179
+ fromEnum: enumToChoiceOutput,
180
+ fromZod: zodToOutput,
181
+ text: textOutput
182
+ };
183
+
184
+ // src/schema/index.ts
185
+ init_json_schema_to_zod();
186
+ export {
187
+ zodToOutput,
188
+ textOutput,
189
+ jsonSchemaToZodSafe,
190
+ jsonSchemaToZod,
191
+ jsonSchemaToOutput,
192
+ jsonSchemaToArrayOutput,
193
+ enumToChoiceOutput,
194
+ SchemaOutput
195
+ };
@@ -0,0 +1,152 @@
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/schema/json-schema-to-zod.ts
16
+ import { z } from "zod";
17
+ function jsonSchemaToZod(schema) {
18
+ const s = schema;
19
+ const makeNullable = (zodSchema) => {
20
+ return s.nullable ? z.union([zodSchema, z.null()]) : zodSchema;
21
+ };
22
+ if (s.const !== undefined) {
23
+ return z.literal(s.const);
24
+ }
25
+ if (s.enum) {
26
+ const values = s.enum;
27
+ return makeNullable(z.enum(values.map(String)));
28
+ }
29
+ if (s.anyOf && s.anyOf.length > 0) {
30
+ const schemas = s.anyOf.map((sub) => jsonSchemaToZod(sub));
31
+ if (schemas.length === 1)
32
+ return schemas[0] ?? z.unknown();
33
+ return z.union([
34
+ schemas[0] ?? z.unknown(),
35
+ schemas[1] ?? z.unknown(),
36
+ ...schemas.slice(2)
37
+ ]);
38
+ }
39
+ if (s.oneOf && s.oneOf.length > 0) {
40
+ const schemas = s.oneOf.map((sub) => jsonSchemaToZod(sub));
41
+ if (schemas.length === 1)
42
+ return schemas[0] ?? z.unknown();
43
+ return z.union([
44
+ schemas[0] ?? z.unknown(),
45
+ schemas[1] ?? z.unknown(),
46
+ ...schemas.slice(2)
47
+ ]);
48
+ }
49
+ if (s.allOf && s.allOf.length > 0) {
50
+ const schemas = s.allOf.map((sub) => jsonSchemaToZod(sub));
51
+ return schemas.reduce((acc, curr) => z.intersection(acc, curr));
52
+ }
53
+ switch (s.type) {
54
+ case "string":
55
+ return makeNullable(buildStringSchema(s));
56
+ case "number":
57
+ case "integer":
58
+ return makeNullable(buildNumberSchema(s));
59
+ case "boolean":
60
+ return makeNullable(z.boolean());
61
+ case "null":
62
+ return z.null();
63
+ case "array":
64
+ return makeNullable(buildArraySchema(s));
65
+ case "object":
66
+ return makeNullable(buildObjectSchema(s));
67
+ default:
68
+ return z.unknown();
69
+ }
70
+ }
71
+ function buildStringSchema(schema) {
72
+ let zodSchema = z.string();
73
+ if (schema.description) {
74
+ zodSchema = zodSchema.describe(schema.description);
75
+ }
76
+ switch (schema.format) {
77
+ case "email":
78
+ zodSchema = zodSchema.email();
79
+ break;
80
+ case "uri":
81
+ case "url":
82
+ zodSchema = zodSchema.url();
83
+ break;
84
+ case "uuid":
85
+ zodSchema = zodSchema.uuid();
86
+ break;
87
+ case "date-time":
88
+ zodSchema = zodSchema.datetime();
89
+ break;
90
+ case "date":
91
+ zodSchema = zodSchema.date();
92
+ break;
93
+ }
94
+ if (schema.minLength !== undefined) {
95
+ zodSchema = zodSchema.min(schema.minLength);
96
+ }
97
+ if (schema.maxLength !== undefined) {
98
+ zodSchema = zodSchema.max(schema.maxLength);
99
+ }
100
+ if (schema.pattern) {
101
+ zodSchema = zodSchema.regex(new RegExp(schema.pattern));
102
+ }
103
+ return zodSchema;
104
+ }
105
+ function buildNumberSchema(schema) {
106
+ let zodSchema = schema.type === "integer" ? z.number().int() : z.number();
107
+ if (schema.description) {
108
+ zodSchema = zodSchema.describe(schema.description);
109
+ }
110
+ if (schema.minimum !== undefined) {
111
+ zodSchema = zodSchema.min(schema.minimum);
112
+ }
113
+ if (schema.maximum !== undefined) {
114
+ zodSchema = zodSchema.max(schema.maximum);
115
+ }
116
+ return zodSchema;
117
+ }
118
+ function buildArraySchema(schema) {
119
+ const itemsSchema = schema.items ? jsonSchemaToZod(schema.items) : z.unknown();
120
+ let zodSchema = z.array(itemsSchema);
121
+ if (schema.description) {
122
+ zodSchema = zodSchema.describe(schema.description);
123
+ }
124
+ return zodSchema;
125
+ }
126
+ function buildObjectSchema(schema) {
127
+ const properties = schema.properties ?? {};
128
+ const required = new Set(schema.required ?? []);
129
+ const shape = {};
130
+ for (const [key, propSchema] of Object.entries(properties)) {
131
+ const zodProp = jsonSchemaToZod(propSchema);
132
+ shape[key] = required.has(key) ? zodProp : zodProp.optional();
133
+ }
134
+ let zodSchema = z.object(shape);
135
+ if (schema.description) {
136
+ zodSchema = zodSchema.describe(schema.description);
137
+ }
138
+ return zodSchema;
139
+ }
140
+ function jsonSchemaToZodSafe(schema) {
141
+ if (!schema || Object.keys(schema).length === 0) {
142
+ return z.object({});
143
+ }
144
+ return jsonSchemaToZod(schema);
145
+ }
146
+ var init_json_schema_to_zod = () => {};
147
+ init_json_schema_to_zod();
148
+
149
+ export {
150
+ jsonSchemaToZodSafe,
151
+ jsonSchemaToZod
152
+ };