@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.
- package/dist/agent/agent-factory.d.ts +67 -71
- package/dist/agent/agent-factory.d.ts.map +1 -1
- package/dist/agent/agent-factory.js +658 -100
- package/dist/agent/agent.test.d.ts +2 -0
- package/dist/agent/agent.test.d.ts.map +1 -0
- package/dist/agent/contract-spec-agent.d.ts +48 -52
- package/dist/agent/contract-spec-agent.d.ts.map +1 -1
- package/dist/agent/contract-spec-agent.js +605 -146
- package/dist/agent/index.d.ts +4 -4
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +2102 -4
- package/dist/agent/json-runner.d.ts +15 -19
- package/dist/agent/json-runner.d.ts.map +1 -1
- package/dist/agent/json-runner.js +672 -57
- package/dist/agent/json-runner.test.d.ts +2 -0
- package/dist/agent/json-runner.test.d.ts.map +1 -0
- package/dist/agent/unified-agent.d.ts +132 -109
- package/dist/agent/unified-agent.d.ts.map +1 -1
- package/dist/agent/unified-agent.js +2011 -293
- package/dist/approval/index.d.ts +3 -2
- package/dist/approval/index.d.ts.map +1 -0
- package/dist/approval/index.js +128 -2
- package/dist/approval/workflow.d.ts +106 -110
- package/dist/approval/workflow.d.ts.map +1 -1
- package/dist/approval/workflow.js +126 -157
- package/dist/exporters/claude-agent-exporter.d.ts +50 -48
- package/dist/exporters/claude-agent-exporter.d.ts.map +1 -1
- package/dist/exporters/claude-agent-exporter.js +258 -203
- package/dist/exporters/index.d.ts +28 -4
- package/dist/exporters/index.d.ts.map +1 -0
- package/dist/exporters/index.js +737 -3
- package/dist/exporters/opencode-exporter.d.ts +47 -45
- package/dist/exporters/opencode-exporter.d.ts.map +1 -1
- package/dist/exporters/opencode-exporter.js +507 -191
- package/dist/exporters/types.d.ts +171 -169
- package/dist/exporters/types.d.ts.map +1 -1
- package/dist/exporters/types.js +1 -0
- package/dist/index.d.ts +15 -39
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3337 -32
- package/dist/interop/index.d.ts +48 -4
- package/dist/interop/index.d.ts.map +1 -0
- package/dist/interop/index.js +709 -3
- package/dist/interop/spec-consumer.d.ts +57 -53
- package/dist/interop/spec-consumer.d.ts.map +1 -1
- package/dist/interop/spec-consumer.js +302 -282
- package/dist/interop/tool-consumer.d.ts +57 -53
- package/dist/interop/tool-consumer.d.ts.map +1 -1
- package/dist/interop/tool-consumer.js +412 -213
- package/dist/interop/types.d.ts +173 -171
- package/dist/interop/types.d.ts.map +1 -1
- package/dist/interop/types.js +1 -0
- package/dist/knowledge/index.d.ts +2 -2
- package/dist/knowledge/index.d.ts.map +1 -0
- package/dist/knowledge/index.js +66 -2
- package/dist/knowledge/injector.d.ts +16 -20
- package/dist/knowledge/injector.d.ts.map +1 -1
- package/dist/knowledge/injector.js +56 -47
- package/dist/memory/in-memory.d.ts +15 -19
- package/dist/memory/in-memory.d.ts.map +1 -1
- package/dist/memory/in-memory.js +152 -46
- package/dist/memory/index.d.ts +3 -3
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +155 -3
- package/dist/memory/manager.d.ts +32 -36
- package/dist/memory/manager.d.ts.map +1 -1
- package/dist/memory/manager.js +96 -70
- package/dist/memory/memory.test.d.ts +2 -0
- package/dist/memory/memory.test.d.ts.map +1 -0
- package/dist/node/agent/agent-factory.js +661 -0
- package/dist/node/agent/contract-spec-agent.js +607 -0
- package/dist/node/agent/index.js +2103 -0
- package/dist/node/agent/json-runner.js +684 -0
- package/dist/node/agent/unified-agent.js +2019 -0
- package/dist/node/approval/index.js +129 -0
- package/dist/node/approval/workflow.js +129 -0
- package/dist/node/exporters/claude-agent-exporter.js +265 -0
- package/dist/node/exporters/index.js +738 -0
- package/dist/node/exporters/opencode-exporter.js +516 -0
- package/dist/node/exporters/types.js +0 -0
- package/dist/node/index.js +3337 -0
- package/dist/node/interop/index.js +710 -0
- package/dist/node/interop/spec-consumer.js +307 -0
- package/dist/node/interop/tool-consumer.js +419 -0
- package/dist/node/interop/types.js +0 -0
- package/dist/node/knowledge/index.js +67 -0
- package/dist/node/knowledge/injector.js +67 -0
- package/dist/node/memory/in-memory.js +154 -0
- package/dist/node/memory/index.js +156 -0
- package/dist/node/memory/manager.js +105 -0
- package/dist/node/providers/claude-agent-sdk/adapter.js +624 -0
- package/dist/node/providers/claude-agent-sdk/index.js +673 -0
- package/dist/node/providers/claude-agent-sdk/session-bridge.js +149 -0
- package/dist/node/providers/claude-agent-sdk/tool-bridge.js +118 -0
- package/dist/node/providers/index.js +1261 -0
- package/dist/node/providers/opencode-sdk/adapter.js +669 -0
- package/dist/node/providers/opencode-sdk/agent-bridge.js +299 -0
- package/dist/node/providers/opencode-sdk/index.js +703 -0
- package/dist/node/providers/opencode-sdk/tool-bridge.js +141 -0
- package/dist/node/providers/registry.js +89 -0
- package/dist/node/providers/types.js +56 -0
- package/dist/node/schema/index.js +195 -0
- package/dist/node/schema/json-schema-to-zod.js +152 -0
- package/dist/node/schema/schema-output.js +190 -0
- package/dist/node/session/index.js +90 -0
- package/dist/node/session/store.js +90 -0
- package/dist/node/spec/index.js +85 -0
- package/dist/node/spec/registry.js +56 -0
- package/dist/node/spec/spec.js +44 -0
- package/dist/node/telemetry/adapter.js +85 -0
- package/dist/node/telemetry/index.js +86 -0
- package/dist/node/tools/index.js +345 -0
- package/dist/node/tools/knowledge-tool.js +74 -0
- package/dist/node/tools/mcp-client.js +47 -0
- package/dist/node/tools/mcp-server.js +205 -0
- package/dist/node/tools/tool-adapter.js +197 -0
- package/dist/node/types.js +0 -0
- package/dist/providers/claude-agent-sdk/adapter.d.ts +60 -52
- package/dist/providers/claude-agent-sdk/adapter.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/adapter.js +622 -304
- package/dist/providers/claude-agent-sdk/index.d.ts +22 -4
- package/dist/providers/claude-agent-sdk/index.d.ts.map +1 -0
- package/dist/providers/claude-agent-sdk/index.js +672 -4
- package/dist/providers/claude-agent-sdk/session-bridge.d.ts +43 -40
- package/dist/providers/claude-agent-sdk/session-bridge.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/session-bridge.js +121 -130
- package/dist/providers/claude-agent-sdk/tool-bridge.d.ts +63 -60
- package/dist/providers/claude-agent-sdk/tool-bridge.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/tool-bridge.js +104 -108
- package/dist/providers/index.d.ts +28 -7
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +1261 -8
- package/dist/providers/opencode-sdk/adapter.d.ts +56 -48
- package/dist/providers/opencode-sdk/adapter.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/adapter.js +667 -274
- package/dist/providers/opencode-sdk/agent-bridge.d.ts +62 -57
- package/dist/providers/opencode-sdk/agent-bridge.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/agent-bridge.js +289 -155
- package/dist/providers/opencode-sdk/index.d.ts +22 -4
- package/dist/providers/opencode-sdk/index.d.ts.map +1 -0
- package/dist/providers/opencode-sdk/index.js +702 -4
- package/dist/providers/opencode-sdk/tool-bridge.d.ts +41 -42
- package/dist/providers/opencode-sdk/tool-bridge.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/tool-bridge.js +121 -107
- package/dist/providers/registry.d.ts +10 -11
- package/dist/providers/registry.d.ts.map +1 -1
- package/dist/providers/registry.js +86 -49
- package/dist/providers/types.d.ts +169 -166
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +54 -42
- package/dist/schema/index.d.ts +3 -3
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +194 -3
- package/dist/schema/json-schema-to-zod.d.ts +23 -26
- package/dist/schema/json-schema-to-zod.d.ts.map +1 -1
- package/dist/schema/json-schema-to-zod.js +138 -110
- package/dist/schema/schema-output.d.ts +29 -32
- package/dist/schema/schema-output.d.ts.map +1 -1
- package/dist/schema/schema-output.js +178 -53
- package/dist/session/index.d.ts +2 -2
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +89 -2
- package/dist/session/store.d.ts +51 -55
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +85 -74
- package/dist/spec/index.d.ts +3 -3
- package/dist/spec/index.d.ts.map +1 -0
- package/dist/spec/index.js +84 -3
- package/dist/spec/registry.d.ts +32 -36
- package/dist/spec/registry.d.ts.map +1 -1
- package/dist/spec/registry.js +51 -60
- package/dist/spec/spec.d.ts +80 -84
- package/dist/spec/spec.d.ts.map +1 -1
- package/dist/spec/spec.js +40 -26
- package/dist/telemetry/adapter.d.ts +33 -37
- package/dist/telemetry/adapter.d.ts.map +1 -1
- package/dist/telemetry/adapter.js +78 -96
- package/dist/telemetry/index.d.ts +2 -2
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +85 -2
- package/dist/tools/index.d.ts +5 -5
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +344 -5
- package/dist/tools/knowledge-tool.d.ts +4 -8
- package/dist/tools/knowledge-tool.d.ts.map +1 -1
- package/dist/tools/knowledge-tool.js +68 -48
- package/dist/tools/mcp-client.d.ts +17 -21
- package/dist/tools/mcp-client.d.ts.map +1 -1
- package/dist/tools/mcp-client.js +42 -53
- package/dist/tools/mcp-server.d.ts +14 -18
- package/dist/tools/mcp-server.d.ts.map +1 -1
- package/dist/tools/mcp-server.js +200 -64
- package/dist/tools/tool-adapter.d.ts +7 -11
- package/dist/tools/tool-adapter.d.ts.map +1 -1
- package/dist/tools/tool-adapter.js +187 -70
- package/dist/tools/tools.test.d.ts +2 -0
- package/dist/tools/tools.test.d.ts.map +1 -0
- package/dist/types.d.ts +108 -111
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +448 -90
- package/dist/_virtual/_rolldown/runtime.js +0 -8
- package/dist/agent/agent-factory.js.map +0 -1
- package/dist/agent/contract-spec-agent.js.map +0 -1
- package/dist/agent/json-runner.js.map +0 -1
- package/dist/agent/unified-agent.js.map +0 -1
- package/dist/approval/workflow.js.map +0 -1
- package/dist/exporters/claude-agent-exporter.js.map +0 -1
- package/dist/exporters/opencode-exporter.js.map +0 -1
- package/dist/interop/spec-consumer.js.map +0 -1
- package/dist/interop/tool-consumer.js.map +0 -1
- package/dist/knowledge/injector.js.map +0 -1
- package/dist/memory/in-memory.js.map +0 -1
- package/dist/memory/manager.js.map +0 -1
- package/dist/providers/claude-agent-sdk/adapter.js.map +0 -1
- package/dist/providers/claude-agent-sdk/session-bridge.js.map +0 -1
- package/dist/providers/claude-agent-sdk/tool-bridge.js.map +0 -1
- package/dist/providers/opencode-sdk/adapter.js.map +0 -1
- package/dist/providers/opencode-sdk/agent-bridge.js.map +0 -1
- package/dist/providers/opencode-sdk/tool-bridge.js.map +0 -1
- package/dist/providers/registry.js.map +0 -1
- package/dist/providers/types.js.map +0 -1
- package/dist/schema/json-schema-to-zod.js.map +0 -1
- package/dist/schema/schema-output.js.map +0 -1
- package/dist/session/store.js.map +0 -1
- package/dist/spec/registry.js.map +0 -1
- package/dist/spec/spec.js.map +0 -1
- package/dist/telemetry/adapter.js.map +0 -1
- package/dist/tools/knowledge-tool.js.map +0 -1
- package/dist/tools/mcp-client.js.map +0 -1
- package/dist/tools/mcp-server.js.map +0 -1
- package/dist/tools/tool-adapter.js.map +0 -1
|
@@ -0,0 +1,1261 @@
|
|
|
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/knowledge/injector.ts
|
|
43
|
+
async function injectStaticKnowledge(instructions, knowledgeRefs, retriever) {
|
|
44
|
+
if (!retriever)
|
|
45
|
+
return instructions;
|
|
46
|
+
const requiredRefs = knowledgeRefs.filter((ref) => ref.required);
|
|
47
|
+
if (requiredRefs.length === 0)
|
|
48
|
+
return instructions;
|
|
49
|
+
const blocks = [];
|
|
50
|
+
for (const ref of requiredRefs) {
|
|
51
|
+
if (!retriever.supportsSpace(ref.key)) {
|
|
52
|
+
console.warn(`Required knowledge space "${ref.key}" is not available`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const content = await retriever.getStatic(ref.key);
|
|
57
|
+
if (content) {
|
|
58
|
+
const header = ref.instructions ? `## ${ref.key}
|
|
59
|
+
${ref.instructions}` : `## ${ref.key}`;
|
|
60
|
+
blocks.push(`${header}
|
|
61
|
+
|
|
62
|
+
${content}`);
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.warn(`Failed to load required knowledge "${ref.key}":`, error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (blocks.length === 0)
|
|
69
|
+
return instructions;
|
|
70
|
+
return `${instructions}
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
# Reference Knowledge
|
|
75
|
+
|
|
76
|
+
The following information is provided for your reference. Use it to inform your responses.
|
|
77
|
+
|
|
78
|
+
${blocks.join(`
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
`)}`;
|
|
83
|
+
}
|
|
84
|
+
function createKnowledgeInjector(retriever) {
|
|
85
|
+
return {
|
|
86
|
+
inject: (instructions, knowledgeRefs) => injectStaticKnowledge(instructions, knowledgeRefs, retriever),
|
|
87
|
+
hasSpace: (spaceKey) => retriever?.supportsSpace(spaceKey) ?? false,
|
|
88
|
+
listSpaces: () => retriever?.listSpaces() ?? []
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/providers/types.ts
|
|
93
|
+
var ExternalProviderError, ProviderNotAvailableError, ProviderExecutionError, ContextCreationError;
|
|
94
|
+
var init_types = __esm(() => {
|
|
95
|
+
ExternalProviderError = class ExternalProviderError extends Error {
|
|
96
|
+
provider;
|
|
97
|
+
code;
|
|
98
|
+
cause;
|
|
99
|
+
constructor(message, provider, code, cause) {
|
|
100
|
+
super(message);
|
|
101
|
+
this.provider = provider;
|
|
102
|
+
this.code = code;
|
|
103
|
+
this.cause = cause;
|
|
104
|
+
this.name = "ExternalProviderError";
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
ProviderNotAvailableError = class ProviderNotAvailableError extends ExternalProviderError {
|
|
108
|
+
constructor(provider, reason) {
|
|
109
|
+
super(`Provider '${provider}' is not available${reason ? `: ${reason}` : ""}`, provider, "PROVIDER_NOT_AVAILABLE");
|
|
110
|
+
this.name = "ProviderNotAvailableError";
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
ProviderExecutionError = class ProviderExecutionError extends ExternalProviderError {
|
|
114
|
+
constructor(provider, message, cause) {
|
|
115
|
+
super(message, provider, "EXECUTION_FAILED", cause);
|
|
116
|
+
this.name = "ProviderExecutionError";
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
ContextCreationError = class ContextCreationError extends ExternalProviderError {
|
|
120
|
+
constructor(provider, message, cause) {
|
|
121
|
+
super(message, provider, "CONTEXT_CREATION_FAILED", cause);
|
|
122
|
+
this.name = "ContextCreationError";
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// src/providers/claude-agent-sdk/tool-bridge.ts
|
|
128
|
+
function specToolToClaudeAgentTool(tool, handler, context) {
|
|
129
|
+
return {
|
|
130
|
+
name: tool.name,
|
|
131
|
+
description: tool.description ?? `Execute ${tool.name}`,
|
|
132
|
+
input_schema: normalizeSchema(tool.schema),
|
|
133
|
+
requires_confirmation: tool.requiresApproval ?? !tool.automationSafe,
|
|
134
|
+
execute: async (input) => {
|
|
135
|
+
const fullContext = {
|
|
136
|
+
agentId: context.agentId ?? "unknown",
|
|
137
|
+
sessionId: context.sessionId ?? "unknown",
|
|
138
|
+
tenantId: context.tenantId,
|
|
139
|
+
actorId: context.actorId,
|
|
140
|
+
metadata: context.metadata,
|
|
141
|
+
signal: context.signal
|
|
142
|
+
};
|
|
143
|
+
return handler(input, fullContext);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function specToolsToClaudeAgentTools(tools, handlers, context) {
|
|
148
|
+
return tools.filter((tool) => handlers.has(tool.name)).map((tool) => {
|
|
149
|
+
const handler = handlers.get(tool.name);
|
|
150
|
+
if (!handler) {
|
|
151
|
+
throw new Error(`Handler not found for tool ${tool.name}`);
|
|
152
|
+
}
|
|
153
|
+
return specToolToClaudeAgentTool(tool, handler, context);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function claudeAgentToolToSpecTool(claudeTool, execute) {
|
|
157
|
+
const config = {
|
|
158
|
+
name: claudeTool.name,
|
|
159
|
+
description: claudeTool.description,
|
|
160
|
+
schema: claudeTool.input_schema,
|
|
161
|
+
requiresApproval: claudeTool.requires_confirmation,
|
|
162
|
+
automationSafe: !claudeTool.requires_confirmation
|
|
163
|
+
};
|
|
164
|
+
const handler = execute ? async (input, _ctx) => String(await execute(input)) : undefined;
|
|
165
|
+
return { config, handler };
|
|
166
|
+
}
|
|
167
|
+
function claudeAgentToolsToSpecTools(claudeTools) {
|
|
168
|
+
const configs = [];
|
|
169
|
+
const handlers = new Map;
|
|
170
|
+
for (const claudeTool of claudeTools) {
|
|
171
|
+
const { config, handler } = claudeAgentToolToSpecTool(claudeTool, claudeTool.execute);
|
|
172
|
+
configs.push(config);
|
|
173
|
+
if (handler) {
|
|
174
|
+
handlers.set(config.name, handler);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { configs, handlers };
|
|
178
|
+
}
|
|
179
|
+
function specToolToExternalTool(tool, handler, context) {
|
|
180
|
+
return {
|
|
181
|
+
name: tool.name,
|
|
182
|
+
description: tool.description ?? `Execute ${tool.name}`,
|
|
183
|
+
inputSchema: tool.schema ?? { type: "object" },
|
|
184
|
+
requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
|
|
185
|
+
execute: handler ? async (input) => {
|
|
186
|
+
const fullContext = {
|
|
187
|
+
agentId: context?.agentId ?? "unknown",
|
|
188
|
+
sessionId: context?.sessionId ?? "unknown",
|
|
189
|
+
tenantId: context?.tenantId,
|
|
190
|
+
actorId: context?.actorId,
|
|
191
|
+
metadata: context?.metadata,
|
|
192
|
+
signal: context?.signal
|
|
193
|
+
};
|
|
194
|
+
return handler(input, fullContext);
|
|
195
|
+
} : undefined
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function normalizeSchema(schema) {
|
|
199
|
+
if (!schema) {
|
|
200
|
+
return { type: "object" };
|
|
201
|
+
}
|
|
202
|
+
if (schema.type === "object") {
|
|
203
|
+
return schema;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
type: "object",
|
|
207
|
+
properties: {
|
|
208
|
+
value: schema
|
|
209
|
+
},
|
|
210
|
+
required: ["value"]
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function extractToolCalls(response) {
|
|
214
|
+
if (!response.content) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
return response.content.filter((block) => block.type === "tool_use").map((block) => ({
|
|
218
|
+
toolCallId: block.id ?? "",
|
|
219
|
+
toolName: block.name ?? "",
|
|
220
|
+
args: block.input
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/providers/claude-agent-sdk/session-bridge.ts
|
|
225
|
+
function toClaudeAgentSession(state) {
|
|
226
|
+
const messages = [];
|
|
227
|
+
for (const msg of state.messages) {
|
|
228
|
+
if (msg.role === "user" || msg.role === "assistant") {
|
|
229
|
+
const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
230
|
+
messages.push({
|
|
231
|
+
role: msg.role,
|
|
232
|
+
content
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
sessionId: state.sessionId,
|
|
238
|
+
messages,
|
|
239
|
+
metadata: {
|
|
240
|
+
agentId: state.agentId,
|
|
241
|
+
tenantId: state.tenantId,
|
|
242
|
+
actorId: state.actorId
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function createClaudeAgentSession(options) {
|
|
247
|
+
return {
|
|
248
|
+
sessionId: options?.sessionId,
|
|
249
|
+
messages: [],
|
|
250
|
+
metadata: {
|
|
251
|
+
tenantId: options?.tenantId,
|
|
252
|
+
actorId: options?.actorId,
|
|
253
|
+
...options?.metadata
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function createEmptyClaudeSession() {
|
|
258
|
+
return createClaudeAgentSession();
|
|
259
|
+
}
|
|
260
|
+
function buildClaudeAgentContext(options) {
|
|
261
|
+
return {
|
|
262
|
+
tenantId: options?.tenantId,
|
|
263
|
+
actorId: options?.actorId,
|
|
264
|
+
sessionId: options?.sessionId,
|
|
265
|
+
...options?.metadata
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function appendUserMessage(session, content) {
|
|
269
|
+
return {
|
|
270
|
+
...session,
|
|
271
|
+
messages: [...session.messages, { role: "user", content }]
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function appendAssistantMessage(session, content) {
|
|
275
|
+
return {
|
|
276
|
+
...session,
|
|
277
|
+
messages: [...session.messages, { role: "assistant", content }]
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function clearSession(session) {
|
|
281
|
+
return {
|
|
282
|
+
...session,
|
|
283
|
+
messages: []
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function getRecentMessages(session, count) {
|
|
287
|
+
return session.messages.slice(-count);
|
|
288
|
+
}
|
|
289
|
+
function extractToolUseBlocks(content) {
|
|
290
|
+
if (typeof content === "string") {
|
|
291
|
+
return [];
|
|
292
|
+
}
|
|
293
|
+
return content.filter((block) => block.type === "tool_use");
|
|
294
|
+
}
|
|
295
|
+
function createToolResultBlock(toolUseId, result, isError = false) {
|
|
296
|
+
return {
|
|
297
|
+
type: "tool_result",
|
|
298
|
+
tool_use_id: toolUseId,
|
|
299
|
+
content: typeof result === "string" ? result : JSON.stringify(result),
|
|
300
|
+
is_error: isError
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function updateSessionMetadata(session, metadata) {
|
|
304
|
+
return {
|
|
305
|
+
...session,
|
|
306
|
+
metadata: {
|
|
307
|
+
...session.metadata,
|
|
308
|
+
...metadata
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function getMessageCount(session) {
|
|
313
|
+
return session.messages.length;
|
|
314
|
+
}
|
|
315
|
+
function estimateTokens(session) {
|
|
316
|
+
let chars = 0;
|
|
317
|
+
for (const msg of session.messages) {
|
|
318
|
+
if (typeof msg.content === "string") {
|
|
319
|
+
chars += msg.content.length;
|
|
320
|
+
} else {
|
|
321
|
+
chars += JSON.stringify(msg.content).length;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return Math.ceil(chars / 4);
|
|
325
|
+
}
|
|
326
|
+
function summarizeSession(session) {
|
|
327
|
+
let userMessages = 0;
|
|
328
|
+
let assistantMessages = 0;
|
|
329
|
+
for (const msg of session.messages) {
|
|
330
|
+
if (msg.role === "user") {
|
|
331
|
+
userMessages++;
|
|
332
|
+
} else {
|
|
333
|
+
assistantMessages++;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
messageCount: session.messages.length,
|
|
338
|
+
userMessages,
|
|
339
|
+
assistantMessages,
|
|
340
|
+
estimatedTokens: estimateTokens(session)
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// src/providers/claude-agent-sdk/adapter.ts
|
|
345
|
+
class ClaudeAgentSDKProvider {
|
|
346
|
+
name = "claude-agent-sdk";
|
|
347
|
+
version = "1.0.0";
|
|
348
|
+
config;
|
|
349
|
+
sdkAvailable = null;
|
|
350
|
+
constructor(config = {}) {
|
|
351
|
+
this.config = {
|
|
352
|
+
model: "claude-sonnet-4-20250514",
|
|
353
|
+
extendedThinking: false,
|
|
354
|
+
computerUse: false,
|
|
355
|
+
maxTokens: 4096,
|
|
356
|
+
temperature: 0.7,
|
|
357
|
+
...config
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
isAvailable() {
|
|
361
|
+
if (this.sdkAvailable !== null) {
|
|
362
|
+
return this.sdkAvailable;
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
__require.resolve("@anthropic-ai/claude-agent-sdk");
|
|
366
|
+
const apiKey = this.config.apiKey ?? process.env.ANTHROPIC_API_KEY;
|
|
367
|
+
this.sdkAvailable = Boolean(apiKey);
|
|
368
|
+
} catch {
|
|
369
|
+
this.sdkAvailable = false;
|
|
370
|
+
}
|
|
371
|
+
return this.sdkAvailable;
|
|
372
|
+
}
|
|
373
|
+
async createContext(spec) {
|
|
374
|
+
if (!this.isAvailable()) {
|
|
375
|
+
throw new ProviderNotAvailableError(this.name, "SDK not installed or API key not configured");
|
|
376
|
+
}
|
|
377
|
+
try {
|
|
378
|
+
const toolSet = {};
|
|
379
|
+
for (const tool of spec.tools) {
|
|
380
|
+
toolSet[tool.name] = specToolToExternalTool(tool);
|
|
381
|
+
}
|
|
382
|
+
const instructions = await injectStaticKnowledge(spec.instructions, spec.knowledge ?? [], undefined);
|
|
383
|
+
const contextId = `claude-${agentKey(spec.meta)}-${Date.now()}`;
|
|
384
|
+
const metadata = {
|
|
385
|
+
computerUseEnabled: this.config.computerUse ?? false,
|
|
386
|
+
extendedThinkingEnabled: this.config.extendedThinking ?? false,
|
|
387
|
+
mcpServerIds: this.config.mcpServers?.map((s) => s.name) ?? []
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
id: contextId,
|
|
391
|
+
spec: {
|
|
392
|
+
...spec,
|
|
393
|
+
instructions
|
|
394
|
+
},
|
|
395
|
+
tools: toolSet,
|
|
396
|
+
metadata,
|
|
397
|
+
cleanup: async () => {}
|
|
398
|
+
};
|
|
399
|
+
} catch (error) {
|
|
400
|
+
throw new ContextCreationError(this.name, `Failed to create context: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async execute(context, params) {
|
|
404
|
+
try {
|
|
405
|
+
const sdk = await this.loadSDK();
|
|
406
|
+
const systemPrompt = params.systemOverride ? `${context.spec.instructions}
|
|
407
|
+
|
|
408
|
+
${params.systemOverride}` : context.spec.instructions;
|
|
409
|
+
const claudeContext = buildClaudeAgentContext(params.options);
|
|
410
|
+
let session = createEmptyClaudeSession();
|
|
411
|
+
session = appendUserMessage(session, params.prompt);
|
|
412
|
+
const claudeTools = this.prepareToolsForSDK(context, params);
|
|
413
|
+
const rawResponse = await sdk.execute({
|
|
414
|
+
model: this.config.model,
|
|
415
|
+
system: systemPrompt,
|
|
416
|
+
messages: session.messages,
|
|
417
|
+
tools: claudeTools,
|
|
418
|
+
max_tokens: this.config.maxTokens,
|
|
419
|
+
temperature: this.config.temperature,
|
|
420
|
+
metadata: claudeContext,
|
|
421
|
+
extended_thinking: this.config.extendedThinking,
|
|
422
|
+
computer_use: this.config.computerUse
|
|
423
|
+
});
|
|
424
|
+
const response = rawResponse;
|
|
425
|
+
const toolCalls = extractToolCalls(response);
|
|
426
|
+
const toolResults = await this.executeTools(toolCalls, context, params);
|
|
427
|
+
if (response.content) {
|
|
428
|
+
const content = response.content;
|
|
429
|
+
session = appendAssistantMessage(session, content);
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
text: this.extractTextContent(response),
|
|
433
|
+
toolCalls: toolCalls.map((tc) => ({
|
|
434
|
+
type: "tool-call",
|
|
435
|
+
toolCallId: tc.toolCallId,
|
|
436
|
+
toolName: tc.toolName,
|
|
437
|
+
args: tc.args
|
|
438
|
+
})),
|
|
439
|
+
toolResults: toolResults.map((tr) => ({
|
|
440
|
+
type: "tool-result",
|
|
441
|
+
toolCallId: tr.toolCallId,
|
|
442
|
+
toolName: tr.toolName,
|
|
443
|
+
output: tr.output
|
|
444
|
+
})),
|
|
445
|
+
usage: {
|
|
446
|
+
inputTokens: response.usage?.input_tokens ?? 0,
|
|
447
|
+
outputTokens: response.usage?.output_tokens ?? 0
|
|
448
|
+
},
|
|
449
|
+
finishReason: this.mapStopReason(response.stop_reason),
|
|
450
|
+
metadata: {
|
|
451
|
+
sessionId: context.metadata?.sessionId,
|
|
452
|
+
model: response.model
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
} catch (error) {
|
|
456
|
+
throw new ProviderExecutionError(this.name, `Execution failed: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async* stream(context, params) {
|
|
460
|
+
try {
|
|
461
|
+
const sdk = await this.loadSDK();
|
|
462
|
+
const systemPrompt = params.systemOverride ? `${context.spec.instructions}
|
|
463
|
+
|
|
464
|
+
${params.systemOverride}` : context.spec.instructions;
|
|
465
|
+
const claudeContext = buildClaudeAgentContext(params.options);
|
|
466
|
+
const claudeTools = this.prepareToolsForSDK(context, params);
|
|
467
|
+
const stream = await sdk.stream({
|
|
468
|
+
model: this.config.model,
|
|
469
|
+
system: systemPrompt,
|
|
470
|
+
messages: [{ role: "user", content: params.prompt }],
|
|
471
|
+
tools: claudeTools,
|
|
472
|
+
max_tokens: this.config.maxTokens,
|
|
473
|
+
temperature: this.config.temperature,
|
|
474
|
+
metadata: claudeContext,
|
|
475
|
+
extended_thinking: this.config.extendedThinking,
|
|
476
|
+
computer_use: this.config.computerUse
|
|
477
|
+
});
|
|
478
|
+
let fullText = "";
|
|
479
|
+
const allToolCalls = [];
|
|
480
|
+
const allToolResults = [];
|
|
481
|
+
let stepIndex = 0;
|
|
482
|
+
for await (const event of stream) {
|
|
483
|
+
if (event.type === "content_block_delta" && event.delta?.type === "text_delta") {
|
|
484
|
+
const text = event.delta.text ?? "";
|
|
485
|
+
fullText += text;
|
|
486
|
+
yield { type: "text", text };
|
|
487
|
+
}
|
|
488
|
+
if (event.type === "content_block_start" && event.content_block?.type === "tool_use") {
|
|
489
|
+
const toolCall = {
|
|
490
|
+
toolCallId: event.content_block.id ?? "",
|
|
491
|
+
toolName: event.content_block.name ?? "",
|
|
492
|
+
args: event.content_block.input
|
|
493
|
+
};
|
|
494
|
+
allToolCalls.push(toolCall);
|
|
495
|
+
yield {
|
|
496
|
+
type: "tool-call",
|
|
497
|
+
toolCall: {
|
|
498
|
+
type: "tool-call",
|
|
499
|
+
...toolCall
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
if (event.type === "message_stop") {
|
|
504
|
+
stepIndex++;
|
|
505
|
+
yield { type: "step-complete", stepIndex };
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
for (const toolCall of allToolCalls) {
|
|
509
|
+
const result = await this.executeTool(toolCall, context, params);
|
|
510
|
+
allToolResults.push(result);
|
|
511
|
+
yield {
|
|
512
|
+
type: "tool-result",
|
|
513
|
+
toolResult: {
|
|
514
|
+
type: "tool-result",
|
|
515
|
+
...result
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
yield {
|
|
520
|
+
type: "done",
|
|
521
|
+
result: {
|
|
522
|
+
text: fullText,
|
|
523
|
+
toolCalls: allToolCalls.map((tc) => ({
|
|
524
|
+
type: "tool-call",
|
|
525
|
+
...tc
|
|
526
|
+
})),
|
|
527
|
+
toolResults: allToolResults.map((tr) => ({
|
|
528
|
+
type: "tool-result",
|
|
529
|
+
...tr
|
|
530
|
+
})),
|
|
531
|
+
usage: { inputTokens: 0, outputTokens: 0 },
|
|
532
|
+
finishReason: "stop"
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
} catch (error) {
|
|
536
|
+
throw new ProviderExecutionError(this.name, `Stream failed: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
async loadSDK() {
|
|
540
|
+
try {
|
|
541
|
+
const module = __require("@anthropic-ai/claude-agent-sdk");
|
|
542
|
+
return module.default ?? module;
|
|
543
|
+
} catch {
|
|
544
|
+
throw new ProviderNotAvailableError(this.name, "@anthropic-ai/claude-agent-sdk is not installed");
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
prepareToolsForSDK(context, params) {
|
|
548
|
+
const handlers = new Map;
|
|
549
|
+
for (const tool of context.spec.tools) {
|
|
550
|
+
const externalTool = context.tools[tool.name];
|
|
551
|
+
if (externalTool?.execute) {
|
|
552
|
+
handlers.set(tool.name, async (input) => {
|
|
553
|
+
if (!externalTool.execute) {
|
|
554
|
+
throw new Error(`Tool ${tool.name} has no execute handler`);
|
|
555
|
+
}
|
|
556
|
+
const result = await externalTool.execute(input);
|
|
557
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return specToolsToClaudeAgentTools(context.spec.tools, handlers, {
|
|
562
|
+
agentId: context.id,
|
|
563
|
+
sessionId: params.options?.sessionId,
|
|
564
|
+
tenantId: params.options?.tenantId,
|
|
565
|
+
actorId: params.options?.actorId,
|
|
566
|
+
metadata: params.options?.metadata,
|
|
567
|
+
signal: params.signal
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
async executeTool(toolCall, context, _params) {
|
|
571
|
+
const tool = context.tools[toolCall.toolName];
|
|
572
|
+
if (!tool?.execute) {
|
|
573
|
+
return {
|
|
574
|
+
toolCallId: toolCall.toolCallId,
|
|
575
|
+
toolName: toolCall.toolName,
|
|
576
|
+
output: `Error: Tool '${toolCall.toolName}' not found or has no handler`
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
try {
|
|
580
|
+
const output = await tool.execute(toolCall.args);
|
|
581
|
+
return {
|
|
582
|
+
toolCallId: toolCall.toolCallId,
|
|
583
|
+
toolName: toolCall.toolName,
|
|
584
|
+
output
|
|
585
|
+
};
|
|
586
|
+
} catch (error) {
|
|
587
|
+
return {
|
|
588
|
+
toolCallId: toolCall.toolCallId,
|
|
589
|
+
toolName: toolCall.toolName,
|
|
590
|
+
output: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
async executeTools(toolCalls, context, params) {
|
|
595
|
+
return Promise.all(toolCalls.map((tc) => this.executeTool(tc, context, params)));
|
|
596
|
+
}
|
|
597
|
+
extractTextContent(response) {
|
|
598
|
+
if (!response.content) {
|
|
599
|
+
return "";
|
|
600
|
+
}
|
|
601
|
+
return response.content.filter((block) => typeof block === "object" && block !== null && block.type === "text").map((block) => block.text).join("");
|
|
602
|
+
}
|
|
603
|
+
mapStopReason(stopReason) {
|
|
604
|
+
switch (stopReason) {
|
|
605
|
+
case "end_turn":
|
|
606
|
+
case "stop_sequence":
|
|
607
|
+
return "stop";
|
|
608
|
+
case "tool_use":
|
|
609
|
+
return "tool-calls";
|
|
610
|
+
case "max_tokens":
|
|
611
|
+
return "length";
|
|
612
|
+
default:
|
|
613
|
+
return "stop";
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
var init_adapter = __esm(() => {
|
|
618
|
+
init_types();
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
// src/providers/claude-agent-sdk/index.ts
|
|
622
|
+
var exports_claude_agent_sdk = {};
|
|
623
|
+
__export(exports_claude_agent_sdk, {
|
|
624
|
+
updateSessionMetadata: () => updateSessionMetadata,
|
|
625
|
+
toClaudeAgentSession: () => toClaudeAgentSession,
|
|
626
|
+
summarizeSession: () => summarizeSession,
|
|
627
|
+
specToolsToClaudeAgentTools: () => specToolsToClaudeAgentTools,
|
|
628
|
+
specToolToExternalTool: () => specToolToExternalTool,
|
|
629
|
+
specToolToClaudeAgentTool: () => specToolToClaudeAgentTool,
|
|
630
|
+
getRecentMessages: () => getRecentMessages,
|
|
631
|
+
getMessageCount: () => getMessageCount,
|
|
632
|
+
extractToolUseBlocks: () => extractToolUseBlocks,
|
|
633
|
+
extractToolCalls: () => extractToolCalls,
|
|
634
|
+
estimateTokens: () => estimateTokens,
|
|
635
|
+
createToolResultBlock: () => createToolResultBlock,
|
|
636
|
+
createEmptyClaudeSession: () => createEmptyClaudeSession,
|
|
637
|
+
createClaudeAgentSession: () => createClaudeAgentSession,
|
|
638
|
+
clearSession: () => clearSession,
|
|
639
|
+
claudeAgentToolsToSpecTools: () => claudeAgentToolsToSpecTools,
|
|
640
|
+
claudeAgentToolToSpecTool: () => claudeAgentToolToSpecTool,
|
|
641
|
+
buildClaudeAgentContext: () => buildClaudeAgentContext,
|
|
642
|
+
appendUserMessage: () => appendUserMessage,
|
|
643
|
+
appendAssistantMessage: () => appendAssistantMessage,
|
|
644
|
+
ClaudeAgentSDKProvider: () => ClaudeAgentSDKProvider
|
|
645
|
+
});
|
|
646
|
+
var init_claude_agent_sdk = __esm(() => {
|
|
647
|
+
init_adapter();
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
// src/providers/opencode-sdk/tool-bridge.ts
|
|
651
|
+
function specToolToOpenCodeTool(tool) {
|
|
652
|
+
return {
|
|
653
|
+
name: tool.name,
|
|
654
|
+
description: tool.description ?? `Execute ${tool.name}`,
|
|
655
|
+
parameters: normalizeToOpenCodeParameters(tool.schema),
|
|
656
|
+
permission: getPermissionLevel(tool)
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function specToolsToOpenCodeTools(tools) {
|
|
660
|
+
return tools.map(specToolToOpenCodeTool);
|
|
661
|
+
}
|
|
662
|
+
function getPermissionLevel(tool) {
|
|
663
|
+
if (tool.requiresApproval) {
|
|
664
|
+
return "ask";
|
|
665
|
+
}
|
|
666
|
+
if (tool.automationSafe === false) {
|
|
667
|
+
return "ask";
|
|
668
|
+
}
|
|
669
|
+
return "allow";
|
|
670
|
+
}
|
|
671
|
+
function openCodeToolToSpecTool(openCodeTool) {
|
|
672
|
+
return {
|
|
673
|
+
name: openCodeTool.name,
|
|
674
|
+
description: openCodeTool.description,
|
|
675
|
+
schema: openCodeTool.parameters,
|
|
676
|
+
requiresApproval: openCodeTool.permission === "ask",
|
|
677
|
+
automationSafe: openCodeTool.permission === "allow"
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
function openCodeToolsToSpecTools(openCodeTools) {
|
|
681
|
+
return openCodeTools.map(openCodeToolToSpecTool);
|
|
682
|
+
}
|
|
683
|
+
function specToolToExternalToolForOpenCode(tool, handler, context) {
|
|
684
|
+
return {
|
|
685
|
+
name: tool.name,
|
|
686
|
+
description: tool.description ?? `Execute ${tool.name}`,
|
|
687
|
+
inputSchema: tool.schema ?? { type: "object" },
|
|
688
|
+
requiresApproval: tool.requiresApproval ?? !tool.automationSafe,
|
|
689
|
+
execute: handler ? async (input) => {
|
|
690
|
+
const fullContext = {
|
|
691
|
+
agentId: context?.agentId ?? "unknown",
|
|
692
|
+
sessionId: context?.sessionId ?? "unknown",
|
|
693
|
+
tenantId: context?.tenantId,
|
|
694
|
+
actorId: context?.actorId,
|
|
695
|
+
metadata: context?.metadata,
|
|
696
|
+
signal: context?.signal
|
|
697
|
+
};
|
|
698
|
+
return handler(input, fullContext);
|
|
699
|
+
} : undefined
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
function normalizeToOpenCodeParameters(schema) {
|
|
703
|
+
if (!schema) {
|
|
704
|
+
return { type: "object" };
|
|
705
|
+
}
|
|
706
|
+
if (schema.type === "object") {
|
|
707
|
+
return {
|
|
708
|
+
type: "object",
|
|
709
|
+
properties: schema.properties,
|
|
710
|
+
required: schema.required
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
return {
|
|
714
|
+
type: "object",
|
|
715
|
+
properties: {
|
|
716
|
+
value: convertToOpenCodeParameter(schema)
|
|
717
|
+
},
|
|
718
|
+
required: ["value"]
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function convertToOpenCodeParameter(schema) {
|
|
722
|
+
const param = {
|
|
723
|
+
type: schema.type ?? "string"
|
|
724
|
+
};
|
|
725
|
+
if (schema.description) {
|
|
726
|
+
param.description = schema.description;
|
|
727
|
+
}
|
|
728
|
+
if (schema.enum) {
|
|
729
|
+
param.enum = schema.enum;
|
|
730
|
+
}
|
|
731
|
+
if (schema.default !== undefined) {
|
|
732
|
+
param.default = schema.default;
|
|
733
|
+
}
|
|
734
|
+
return param;
|
|
735
|
+
}
|
|
736
|
+
function createToolHandlerMap(tools) {
|
|
737
|
+
const handlers = new Map;
|
|
738
|
+
for (const [name, tool] of Object.entries(tools)) {
|
|
739
|
+
if (tool.execute) {
|
|
740
|
+
handlers.set(name, tool.execute);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return handlers;
|
|
744
|
+
}
|
|
745
|
+
async function executeToolCall(toolCall, handlers) {
|
|
746
|
+
const handler = handlers.get(toolCall.name);
|
|
747
|
+
if (!handler) {
|
|
748
|
+
return {
|
|
749
|
+
tool_call_id: toolCall.id,
|
|
750
|
+
output: `Error: Tool '${toolCall.name}' not found`,
|
|
751
|
+
is_error: true
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
try {
|
|
755
|
+
const result = await handler(toolCall.arguments);
|
|
756
|
+
return {
|
|
757
|
+
tool_call_id: toolCall.id,
|
|
758
|
+
output: typeof result === "string" ? result : JSON.stringify(result)
|
|
759
|
+
};
|
|
760
|
+
} catch (error) {
|
|
761
|
+
return {
|
|
762
|
+
tool_call_id: toolCall.id,
|
|
763
|
+
output: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
764
|
+
is_error: true
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// src/providers/opencode-sdk/agent-bridge.ts
|
|
770
|
+
function inferAgentType(spec) {
|
|
771
|
+
const hasCodeTools = spec.tools.some((tool) => [
|
|
772
|
+
"write_file",
|
|
773
|
+
"edit_file",
|
|
774
|
+
"create_file",
|
|
775
|
+
"delete_file",
|
|
776
|
+
"bash",
|
|
777
|
+
"execute",
|
|
778
|
+
"run_command",
|
|
779
|
+
"terminal"
|
|
780
|
+
].includes(tool.name.toLowerCase()));
|
|
781
|
+
const instructionsLower = spec.instructions.toLowerCase();
|
|
782
|
+
const hasPlanningKeywords = /\b(plan|design|architect|strategy|analyze|review|assess|evaluate)\b/.test(instructionsLower);
|
|
783
|
+
const hasExplorationKeywords = /\b(search|find|explore|discover|locate|grep|pattern)\b/.test(instructionsLower);
|
|
784
|
+
const hasResearchKeywords = /\b(research|investigate|understand|learn|gather|collect)\b/.test(instructionsLower);
|
|
785
|
+
if (hasCodeTools) {
|
|
786
|
+
return "build";
|
|
787
|
+
}
|
|
788
|
+
if (hasPlanningKeywords && !hasCodeTools) {
|
|
789
|
+
return "plan";
|
|
790
|
+
}
|
|
791
|
+
if (hasExplorationKeywords && !hasResearchKeywords) {
|
|
792
|
+
return "explore";
|
|
793
|
+
}
|
|
794
|
+
return "general";
|
|
795
|
+
}
|
|
796
|
+
function specToOpenCodeConfig(spec, options) {
|
|
797
|
+
const agentType = options?.agentType ?? inferAgentType(spec);
|
|
798
|
+
return {
|
|
799
|
+
name: spec.meta.key,
|
|
800
|
+
version: spec.meta.version,
|
|
801
|
+
description: spec.description ?? spec.meta.description,
|
|
802
|
+
instructions: spec.instructions,
|
|
803
|
+
agent_type: agentType,
|
|
804
|
+
tools: specToolsToOpenCodeTools(spec.tools),
|
|
805
|
+
config: {
|
|
806
|
+
max_steps: options?.maxSteps ?? spec.maxSteps ?? 10,
|
|
807
|
+
temperature: options?.temperature ?? 0.7,
|
|
808
|
+
model: options?.model,
|
|
809
|
+
permissions: buildPermissions(spec)
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
function buildPermissions(spec) {
|
|
814
|
+
const permissions = {};
|
|
815
|
+
for (const tool of spec.tools) {
|
|
816
|
+
if (tool.requiresApproval) {
|
|
817
|
+
permissions[tool.name] = "ask";
|
|
818
|
+
} else if (tool.automationSafe === false) {
|
|
819
|
+
permissions[tool.name] = "ask";
|
|
820
|
+
} else {
|
|
821
|
+
permissions[tool.name] = "allow";
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return permissions;
|
|
825
|
+
}
|
|
826
|
+
function specToOpenCodeMarkdown(spec, options) {
|
|
827
|
+
const agentType = options?.agentType ?? inferAgentType(spec);
|
|
828
|
+
return {
|
|
829
|
+
frontmatter: {
|
|
830
|
+
name: spec.meta.key,
|
|
831
|
+
type: agentType,
|
|
832
|
+
version: spec.meta.version,
|
|
833
|
+
model: options?.model,
|
|
834
|
+
temperature: options?.temperature ?? 0.7,
|
|
835
|
+
max_steps: options?.maxSteps ?? spec.maxSteps ?? 10,
|
|
836
|
+
tools: spec.tools.map((t) => t.name)
|
|
837
|
+
},
|
|
838
|
+
body: buildMarkdownBody(spec)
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
function buildMarkdownBody(spec) {
|
|
842
|
+
const lines = [];
|
|
843
|
+
lines.push(`# ${spec.meta.key}`);
|
|
844
|
+
lines.push("");
|
|
845
|
+
if (spec.description ?? spec.meta.description) {
|
|
846
|
+
lines.push(spec.description ?? spec.meta.description ?? "");
|
|
847
|
+
lines.push("");
|
|
848
|
+
}
|
|
849
|
+
lines.push("## Instructions");
|
|
850
|
+
lines.push("");
|
|
851
|
+
lines.push(spec.instructions);
|
|
852
|
+
lines.push("");
|
|
853
|
+
if (spec.tools.length > 0) {
|
|
854
|
+
lines.push("## Tools");
|
|
855
|
+
lines.push("");
|
|
856
|
+
for (const tool of spec.tools) {
|
|
857
|
+
const permission = tool.requiresApproval ? "(requires approval)" : tool.automationSafe === false ? "(ask mode)" : "";
|
|
858
|
+
lines.push(`- **${tool.name}**: ${tool.description ?? "No description"} ${permission}`.trim());
|
|
859
|
+
}
|
|
860
|
+
lines.push("");
|
|
861
|
+
}
|
|
862
|
+
if (spec.knowledge && spec.knowledge.length > 0) {
|
|
863
|
+
lines.push("## Knowledge Sources");
|
|
864
|
+
lines.push("");
|
|
865
|
+
for (const k of spec.knowledge) {
|
|
866
|
+
const required = k.required ? "(required)" : "(optional)";
|
|
867
|
+
lines.push(`- ${k.key} ${required}`);
|
|
868
|
+
}
|
|
869
|
+
lines.push("");
|
|
870
|
+
}
|
|
871
|
+
return lines.join(`
|
|
872
|
+
`);
|
|
873
|
+
}
|
|
874
|
+
function serializeOpenCodeMarkdown(markdown) {
|
|
875
|
+
const lines = [];
|
|
876
|
+
lines.push("---");
|
|
877
|
+
lines.push(`name: ${markdown.frontmatter.name}`);
|
|
878
|
+
lines.push(`type: ${markdown.frontmatter.type}`);
|
|
879
|
+
if (markdown.frontmatter.version) {
|
|
880
|
+
lines.push(`version: ${markdown.frontmatter.version}`);
|
|
881
|
+
}
|
|
882
|
+
if (markdown.frontmatter.model) {
|
|
883
|
+
lines.push(`model: ${markdown.frontmatter.model}`);
|
|
884
|
+
}
|
|
885
|
+
if (markdown.frontmatter.temperature !== undefined) {
|
|
886
|
+
lines.push(`temperature: ${markdown.frontmatter.temperature}`);
|
|
887
|
+
}
|
|
888
|
+
if (markdown.frontmatter.max_steps !== undefined) {
|
|
889
|
+
lines.push(`max_steps: ${markdown.frontmatter.max_steps}`);
|
|
890
|
+
}
|
|
891
|
+
if (markdown.frontmatter.tools && markdown.frontmatter.tools.length > 0) {
|
|
892
|
+
lines.push(`tools:`);
|
|
893
|
+
for (const tool of markdown.frontmatter.tools) {
|
|
894
|
+
lines.push(` - ${tool}`);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
lines.push("---");
|
|
898
|
+
lines.push("");
|
|
899
|
+
lines.push(markdown.body);
|
|
900
|
+
return lines.join(`
|
|
901
|
+
`);
|
|
902
|
+
}
|
|
903
|
+
function openCodeConfigToSpec(config) {
|
|
904
|
+
return {
|
|
905
|
+
meta: {
|
|
906
|
+
key: config.name,
|
|
907
|
+
version: config.version ?? "1.0.0",
|
|
908
|
+
description: config.description ?? "",
|
|
909
|
+
stability: "experimental",
|
|
910
|
+
owners: [],
|
|
911
|
+
tags: []
|
|
912
|
+
},
|
|
913
|
+
description: config.description,
|
|
914
|
+
instructions: config.instructions ?? "",
|
|
915
|
+
tools: config.tools?.map((tool) => ({
|
|
916
|
+
name: tool.name,
|
|
917
|
+
description: tool.description,
|
|
918
|
+
schema: tool.parameters,
|
|
919
|
+
requiresApproval: tool.permission === "ask",
|
|
920
|
+
automationSafe: tool.permission === "allow"
|
|
921
|
+
})) ?? [],
|
|
922
|
+
maxSteps: config.config?.max_steps ?? 10
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
var init_agent_bridge = () => {};
|
|
926
|
+
|
|
927
|
+
// src/providers/opencode-sdk/adapter.ts
|
|
928
|
+
class OpenCodeSDKProvider {
|
|
929
|
+
name = "opencode-sdk";
|
|
930
|
+
version = "1.0.0";
|
|
931
|
+
config;
|
|
932
|
+
sdkAvailable = null;
|
|
933
|
+
constructor(config = {}) {
|
|
934
|
+
this.config = {
|
|
935
|
+
serverUrl: "http://127.0.0.1:4096",
|
|
936
|
+
port: 4096,
|
|
937
|
+
agentType: "general",
|
|
938
|
+
temperature: 0.7,
|
|
939
|
+
timeout: 30000,
|
|
940
|
+
...config
|
|
941
|
+
};
|
|
942
|
+
if (!config.serverUrl && config.port) {
|
|
943
|
+
this.config.serverUrl = `http://127.0.0.1:${config.port}`;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
isAvailable() {
|
|
947
|
+
if (this.sdkAvailable !== null) {
|
|
948
|
+
return this.sdkAvailable;
|
|
949
|
+
}
|
|
950
|
+
try {
|
|
951
|
+
__require.resolve("@opencode-ai/sdk");
|
|
952
|
+
this.sdkAvailable = true;
|
|
953
|
+
} catch {
|
|
954
|
+
this.sdkAvailable = false;
|
|
955
|
+
}
|
|
956
|
+
return this.sdkAvailable;
|
|
957
|
+
}
|
|
958
|
+
async createContext(spec) {
|
|
959
|
+
if (!this.isAvailable()) {
|
|
960
|
+
throw new ProviderNotAvailableError(this.name, "SDK not installed");
|
|
961
|
+
}
|
|
962
|
+
try {
|
|
963
|
+
const sdk = await this.loadSDK();
|
|
964
|
+
const { client } = await sdk.createOpencode({
|
|
965
|
+
hostname: this.getHostname(),
|
|
966
|
+
port: this.config.port ?? 4096,
|
|
967
|
+
timeout: this.config.timeout
|
|
968
|
+
});
|
|
969
|
+
const session = await client.session.create({
|
|
970
|
+
agent: this.config.agentType ?? inferAgentType(spec),
|
|
971
|
+
model: this.config.model
|
|
972
|
+
});
|
|
973
|
+
const toolSet = {};
|
|
974
|
+
for (const tool of spec.tools) {
|
|
975
|
+
toolSet[tool.name] = specToolToExternalToolForOpenCode(tool);
|
|
976
|
+
}
|
|
977
|
+
const instructions = await injectStaticKnowledge(spec.instructions, spec.knowledge ?? [], undefined);
|
|
978
|
+
const contextId = `opencode-${agentKey(spec.meta)}-${Date.now()}`;
|
|
979
|
+
const metadata = {
|
|
980
|
+
sessionId: session.id,
|
|
981
|
+
agentType: this.config.agentType ?? inferAgentType(spec),
|
|
982
|
+
serverUrl: this.config.serverUrl ?? `http://127.0.0.1:${this.config.port ?? 4096}`
|
|
983
|
+
};
|
|
984
|
+
return {
|
|
985
|
+
id: contextId,
|
|
986
|
+
spec: {
|
|
987
|
+
...spec,
|
|
988
|
+
instructions
|
|
989
|
+
},
|
|
990
|
+
tools: toolSet,
|
|
991
|
+
metadata,
|
|
992
|
+
cleanup: async () => {
|
|
993
|
+
try {
|
|
994
|
+
await client.session.delete({ id: session.id });
|
|
995
|
+
} catch {}
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
} catch (error) {
|
|
999
|
+
throw new ContextCreationError(this.name, `Failed to create context: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
async execute(context, params) {
|
|
1003
|
+
try {
|
|
1004
|
+
const sdk = await this.loadSDK();
|
|
1005
|
+
const metadata = context.metadata;
|
|
1006
|
+
const { client } = await sdk.createOpencodeClient({
|
|
1007
|
+
baseUrl: metadata.serverUrl
|
|
1008
|
+
});
|
|
1009
|
+
const systemPrompt = params.systemOverride ? `${context.spec.instructions}
|
|
1010
|
+
|
|
1011
|
+
${params.systemOverride}` : context.spec.instructions;
|
|
1012
|
+
const response = await client.session.prompt({
|
|
1013
|
+
id: metadata.sessionId,
|
|
1014
|
+
body: {
|
|
1015
|
+
content: params.prompt,
|
|
1016
|
+
system: systemPrompt
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
const toolCalls = this.extractToolCalls(response);
|
|
1020
|
+
const toolHandlers = createToolHandlerMap(context.tools);
|
|
1021
|
+
const toolResults = await Promise.all(toolCalls.map((tc) => executeToolCall(tc, toolHandlers)));
|
|
1022
|
+
return {
|
|
1023
|
+
text: this.extractTextContent(response),
|
|
1024
|
+
toolCalls: toolCalls.map((tc) => ({
|
|
1025
|
+
type: "tool-call",
|
|
1026
|
+
toolCallId: tc.id,
|
|
1027
|
+
toolName: tc.name,
|
|
1028
|
+
args: tc.arguments
|
|
1029
|
+
})),
|
|
1030
|
+
toolResults: toolResults.map((tr) => ({
|
|
1031
|
+
type: "tool-result",
|
|
1032
|
+
toolCallId: tr.tool_call_id,
|
|
1033
|
+
toolName: toolCalls.find((tc) => tc.id === tr.tool_call_id)?.name ?? "",
|
|
1034
|
+
output: tr.output
|
|
1035
|
+
})),
|
|
1036
|
+
usage: {
|
|
1037
|
+
inputTokens: response.usage?.input_tokens ?? 0,
|
|
1038
|
+
outputTokens: response.usage?.output_tokens ?? 0
|
|
1039
|
+
},
|
|
1040
|
+
finishReason: this.mapFinishReason(response.finish_reason),
|
|
1041
|
+
metadata: {
|
|
1042
|
+
sessionId: metadata.sessionId,
|
|
1043
|
+
agentType: metadata.agentType
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
throw new ProviderExecutionError(this.name, `Execution failed: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
async* stream(context, params) {
|
|
1051
|
+
try {
|
|
1052
|
+
const sdk = await this.loadSDK();
|
|
1053
|
+
const metadata = context.metadata;
|
|
1054
|
+
const { client } = await sdk.createOpencodeClient({
|
|
1055
|
+
baseUrl: metadata.serverUrl
|
|
1056
|
+
});
|
|
1057
|
+
const systemPrompt = params.systemOverride ? `${context.spec.instructions}
|
|
1058
|
+
|
|
1059
|
+
${params.systemOverride}` : context.spec.instructions;
|
|
1060
|
+
const events = client.session.subscribe({
|
|
1061
|
+
id: metadata.sessionId
|
|
1062
|
+
});
|
|
1063
|
+
await client.session.prompt({
|
|
1064
|
+
id: metadata.sessionId,
|
|
1065
|
+
body: {
|
|
1066
|
+
content: params.prompt,
|
|
1067
|
+
system: systemPrompt
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
let fullText = "";
|
|
1071
|
+
const allToolCalls = [];
|
|
1072
|
+
const toolHandlers = createToolHandlerMap(context.tools);
|
|
1073
|
+
let stepIndex = 0;
|
|
1074
|
+
for await (const event of events) {
|
|
1075
|
+
if (event.type === "message.delta") {
|
|
1076
|
+
const text = event.delta?.content ?? "";
|
|
1077
|
+
fullText += text;
|
|
1078
|
+
yield { type: "text", text };
|
|
1079
|
+
}
|
|
1080
|
+
if (event.type === "tool.call") {
|
|
1081
|
+
const toolCall = {
|
|
1082
|
+
id: event.tool_call_id ?? `tc-${Date.now()}`,
|
|
1083
|
+
name: event.tool_name ?? "",
|
|
1084
|
+
arguments: event.arguments ?? {}
|
|
1085
|
+
};
|
|
1086
|
+
allToolCalls.push(toolCall);
|
|
1087
|
+
yield {
|
|
1088
|
+
type: "tool-call",
|
|
1089
|
+
toolCall: {
|
|
1090
|
+
type: "tool-call",
|
|
1091
|
+
toolCallId: toolCall.id,
|
|
1092
|
+
toolName: toolCall.name,
|
|
1093
|
+
args: toolCall.arguments
|
|
1094
|
+
}
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
if (event.type === "step.complete") {
|
|
1098
|
+
stepIndex++;
|
|
1099
|
+
yield { type: "step-complete", stepIndex };
|
|
1100
|
+
}
|
|
1101
|
+
if (event.type === "message.complete") {
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
for (const toolCall of allToolCalls) {
|
|
1106
|
+
const result = await executeToolCall(toolCall, toolHandlers);
|
|
1107
|
+
yield {
|
|
1108
|
+
type: "tool-result",
|
|
1109
|
+
toolResult: {
|
|
1110
|
+
type: "tool-result",
|
|
1111
|
+
toolCallId: result.tool_call_id,
|
|
1112
|
+
toolName: toolCall.name,
|
|
1113
|
+
output: result.output
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1117
|
+
yield {
|
|
1118
|
+
type: "done",
|
|
1119
|
+
result: {
|
|
1120
|
+
text: fullText,
|
|
1121
|
+
toolCalls: allToolCalls.map((tc) => ({
|
|
1122
|
+
type: "tool-call",
|
|
1123
|
+
toolCallId: tc.id,
|
|
1124
|
+
toolName: tc.name,
|
|
1125
|
+
args: tc.arguments
|
|
1126
|
+
})),
|
|
1127
|
+
toolResults: [],
|
|
1128
|
+
usage: { inputTokens: 0, outputTokens: 0 },
|
|
1129
|
+
finishReason: "stop"
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
throw new ProviderExecutionError(this.name, `Stream failed: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
async loadSDK() {
|
|
1137
|
+
try {
|
|
1138
|
+
const module = __require("@opencode-ai/sdk");
|
|
1139
|
+
return module;
|
|
1140
|
+
} catch {
|
|
1141
|
+
throw new ProviderNotAvailableError(this.name, "@opencode-ai/sdk is not installed");
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
getHostname() {
|
|
1145
|
+
if (!this.config.serverUrl) {
|
|
1146
|
+
return "127.0.0.1";
|
|
1147
|
+
}
|
|
1148
|
+
try {
|
|
1149
|
+
const url = new URL(this.config.serverUrl);
|
|
1150
|
+
return url.hostname;
|
|
1151
|
+
} catch {
|
|
1152
|
+
return "127.0.0.1";
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
extractToolCalls(response) {
|
|
1156
|
+
if (!response.tool_calls) {
|
|
1157
|
+
return [];
|
|
1158
|
+
}
|
|
1159
|
+
return response.tool_calls.map((tc) => ({
|
|
1160
|
+
id: tc.id,
|
|
1161
|
+
name: tc.name,
|
|
1162
|
+
arguments: tc.arguments ?? {}
|
|
1163
|
+
}));
|
|
1164
|
+
}
|
|
1165
|
+
extractTextContent(response) {
|
|
1166
|
+
return response.content ?? response.message?.content ?? "";
|
|
1167
|
+
}
|
|
1168
|
+
mapFinishReason(finishReason) {
|
|
1169
|
+
switch (finishReason) {
|
|
1170
|
+
case "stop":
|
|
1171
|
+
case "end":
|
|
1172
|
+
return "stop";
|
|
1173
|
+
case "tool_use":
|
|
1174
|
+
case "tool_calls":
|
|
1175
|
+
return "tool-calls";
|
|
1176
|
+
case "length":
|
|
1177
|
+
case "max_tokens":
|
|
1178
|
+
return "length";
|
|
1179
|
+
default:
|
|
1180
|
+
return "stop";
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
var init_adapter2 = __esm(() => {
|
|
1185
|
+
init_types();
|
|
1186
|
+
init_agent_bridge();
|
|
1187
|
+
});
|
|
1188
|
+
|
|
1189
|
+
// src/providers/opencode-sdk/index.ts
|
|
1190
|
+
var exports_opencode_sdk = {};
|
|
1191
|
+
__export(exports_opencode_sdk, {
|
|
1192
|
+
specToolsToOpenCodeTools: () => specToolsToOpenCodeTools,
|
|
1193
|
+
specToolToOpenCodeTool: () => specToolToOpenCodeTool,
|
|
1194
|
+
specToolToExternalToolForOpenCode: () => specToolToExternalToolForOpenCode,
|
|
1195
|
+
specToOpenCodeMarkdown: () => specToOpenCodeMarkdown,
|
|
1196
|
+
specToOpenCodeConfig: () => specToOpenCodeConfig,
|
|
1197
|
+
serializeOpenCodeMarkdown: () => serializeOpenCodeMarkdown,
|
|
1198
|
+
openCodeToolsToSpecTools: () => openCodeToolsToSpecTools,
|
|
1199
|
+
openCodeToolToSpecTool: () => openCodeToolToSpecTool,
|
|
1200
|
+
openCodeConfigToSpec: () => openCodeConfigToSpec,
|
|
1201
|
+
inferAgentType: () => inferAgentType,
|
|
1202
|
+
executeToolCall: () => executeToolCall,
|
|
1203
|
+
createToolHandlerMap: () => createToolHandlerMap,
|
|
1204
|
+
OpenCodeSDKProvider: () => OpenCodeSDKProvider
|
|
1205
|
+
});
|
|
1206
|
+
var init_opencode_sdk = __esm(() => {
|
|
1207
|
+
init_adapter2();
|
|
1208
|
+
init_agent_bridge();
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
// src/providers/registry.ts
|
|
1212
|
+
init_types();
|
|
1213
|
+
|
|
1214
|
+
class InMemoryProviderRegistry {
|
|
1215
|
+
providers = new Map;
|
|
1216
|
+
get(name) {
|
|
1217
|
+
return this.providers.get(name);
|
|
1218
|
+
}
|
|
1219
|
+
register(name, provider) {
|
|
1220
|
+
this.providers.set(name, provider);
|
|
1221
|
+
}
|
|
1222
|
+
list() {
|
|
1223
|
+
return Array.from(this.providers.keys());
|
|
1224
|
+
}
|
|
1225
|
+
isAvailable(name) {
|
|
1226
|
+
const provider = this.providers.get(name);
|
|
1227
|
+
return provider !== undefined && provider.isAvailable();
|
|
1228
|
+
}
|
|
1229
|
+
require(name) {
|
|
1230
|
+
const provider = this.providers.get(name);
|
|
1231
|
+
if (!provider) {
|
|
1232
|
+
throw new ProviderNotAvailableError(name, "not registered");
|
|
1233
|
+
}
|
|
1234
|
+
if (!provider.isAvailable()) {
|
|
1235
|
+
throw new ProviderNotAvailableError(name, "dependencies not installed or not configured");
|
|
1236
|
+
}
|
|
1237
|
+
return provider;
|
|
1238
|
+
}
|
|
1239
|
+
clear() {
|
|
1240
|
+
this.providers.clear();
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
function createProviderRegistry() {
|
|
1244
|
+
return new InMemoryProviderRegistry;
|
|
1245
|
+
}
|
|
1246
|
+
var defaultProviderRegistry = createProviderRegistry();
|
|
1247
|
+
|
|
1248
|
+
// src/providers/index.ts
|
|
1249
|
+
init_claude_agent_sdk();
|
|
1250
|
+
init_opencode_sdk();
|
|
1251
|
+
init_types();
|
|
1252
|
+
export {
|
|
1253
|
+
defaultProviderRegistry,
|
|
1254
|
+
createProviderRegistry,
|
|
1255
|
+
ProviderNotAvailableError,
|
|
1256
|
+
ProviderExecutionError,
|
|
1257
|
+
OpenCodeSDKProvider,
|
|
1258
|
+
ExternalProviderError,
|
|
1259
|
+
ContextCreationError,
|
|
1260
|
+
ClaudeAgentSDKProvider
|
|
1261
|
+
};
|