@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,624 @@
|
|
|
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
|
+
init_adapter();
|
|
621
|
+
|
|
622
|
+
export {
|
|
623
|
+
ClaudeAgentSDKProvider
|
|
624
|
+
};
|