@aigne/core 1.14.0 → 1.16.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/CHANGELOG.md +27 -0
- package/README.md +9 -7
- package/README.zh.md +9 -7
- package/lib/cjs/agents/agent.d.ts +129 -6
- package/lib/cjs/agents/agent.js +112 -20
- package/lib/cjs/agents/ai-agent.d.ts +3 -2
- package/lib/cjs/agents/ai-agent.js +12 -9
- package/lib/{esm/models → cjs/agents}/chat-model.d.ts +24 -13
- package/lib/cjs/{models → agents}/chat-model.js +48 -7
- package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
- package/lib/cjs/agents/guide-rail-agent.js +14 -0
- package/lib/cjs/agents/mcp-agent.js +9 -9
- package/lib/cjs/agents/team-agent.js +1 -1
- package/lib/cjs/aigne/aigne.d.ts +3 -2
- package/lib/cjs/aigne/aigne.js +2 -2
- package/lib/cjs/aigne/context.d.ts +2 -1
- package/lib/cjs/aigne/context.js +8 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +1 -1
- package/lib/cjs/loader/index.d.ts +18 -11
- package/lib/cjs/loader/index.js +8 -27
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +3 -3
- package/lib/cjs/prompt/template.d.ts +3 -3
- package/lib/cjs/prompt/template.js +1 -1
- package/lib/cjs/utils/json-schema.js +1 -1
- package/lib/cjs/utils/logger.d.ts +33 -8
- package/lib/cjs/utils/logger.js +63 -5
- package/lib/cjs/utils/model-utils.d.ts +1 -1
- package/lib/cjs/utils/stream-utils.d.ts +3 -2
- package/lib/cjs/utils/stream-utils.js +50 -26
- package/lib/cjs/utils/type-utils.d.ts +5 -0
- package/lib/dts/agents/agent.d.ts +129 -6
- package/lib/dts/agents/ai-agent.d.ts +3 -2
- package/lib/{cjs/models → dts/agents}/chat-model.d.ts +24 -13
- package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
- package/lib/dts/aigne/aigne.d.ts +3 -2
- package/lib/dts/aigne/context.d.ts +2 -1
- package/lib/dts/index.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +1 -1
- package/lib/dts/loader/index.d.ts +18 -11
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +3 -3
- package/lib/dts/prompt/template.d.ts +3 -3
- package/lib/dts/utils/logger.d.ts +33 -8
- package/lib/dts/utils/model-utils.d.ts +1 -1
- package/lib/dts/utils/stream-utils.d.ts +3 -2
- package/lib/dts/utils/type-utils.d.ts +5 -0
- package/lib/esm/agents/agent.d.ts +129 -6
- package/lib/esm/agents/agent.js +112 -20
- package/lib/esm/agents/ai-agent.d.ts +3 -2
- package/lib/esm/agents/ai-agent.js +12 -9
- package/lib/{dts/models → esm/agents}/chat-model.d.ts +24 -13
- package/lib/esm/{models → agents}/chat-model.js +48 -7
- package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
- package/lib/esm/agents/guide-rail-agent.js +11 -0
- package/lib/esm/agents/mcp-agent.js +9 -9
- package/lib/esm/agents/team-agent.js +2 -2
- package/lib/esm/aigne/aigne.d.ts +3 -2
- package/lib/esm/aigne/aigne.js +2 -2
- package/lib/esm/aigne/context.d.ts +2 -1
- package/lib/esm/aigne/context.js +9 -2
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +1 -1
- package/lib/esm/loader/index.d.ts +18 -11
- package/lib/esm/loader/index.js +8 -27
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +3 -3
- package/lib/esm/prompt/template.d.ts +3 -3
- package/lib/esm/prompt/template.js +1 -1
- package/lib/esm/utils/json-schema.js +1 -1
- package/lib/esm/utils/logger.d.ts +33 -8
- package/lib/esm/utils/logger.js +61 -4
- package/lib/esm/utils/model-utils.d.ts +1 -1
- package/lib/esm/utils/stream-utils.d.ts +3 -2
- package/lib/esm/utils/stream-utils.js +48 -25
- package/lib/esm/utils/type-utils.d.ts +5 -0
- package/package.json +1 -20
- package/lib/cjs/client/client.d.ts +0 -97
- package/lib/cjs/client/client.js +0 -87
- package/lib/cjs/client/index.d.ts +0 -1
- package/lib/cjs/client/index.js +0 -17
- package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
- package/lib/cjs/models/bedrock-chat-model.js +0 -303
- package/lib/cjs/models/claude-chat-model.d.ts +0 -114
- package/lib/cjs/models/claude-chat-model.js +0 -317
- package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
- package/lib/cjs/models/deepseek-chat-model.js +0 -35
- package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
- package/lib/cjs/models/gemini-chat-model.js +0 -35
- package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
- package/lib/cjs/models/ollama-chat-model.js +0 -34
- package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
- package/lib/cjs/models/open-router-chat-model.js +0 -34
- package/lib/cjs/models/openai-chat-model.d.ts +0 -166
- package/lib/cjs/models/openai-chat-model.js +0 -415
- package/lib/cjs/models/xai-chat-model.d.ts +0 -21
- package/lib/cjs/models/xai-chat-model.js +0 -33
- package/lib/cjs/server/error.d.ts +0 -15
- package/lib/cjs/server/error.js +0 -22
- package/lib/cjs/server/index.d.ts +0 -2
- package/lib/cjs/server/index.js +0 -18
- package/lib/cjs/server/server.d.ts +0 -135
- package/lib/cjs/server/server.js +0 -188
- package/lib/dts/client/client.d.ts +0 -97
- package/lib/dts/client/index.d.ts +0 -1
- package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
- package/lib/dts/models/claude-chat-model.d.ts +0 -114
- package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
- package/lib/dts/models/gemini-chat-model.d.ts +0 -23
- package/lib/dts/models/ollama-chat-model.d.ts +0 -22
- package/lib/dts/models/open-router-chat-model.d.ts +0 -22
- package/lib/dts/models/openai-chat-model.d.ts +0 -166
- package/lib/dts/models/xai-chat-model.d.ts +0 -21
- package/lib/dts/server/error.d.ts +0 -15
- package/lib/dts/server/index.d.ts +0 -2
- package/lib/dts/server/server.d.ts +0 -135
- package/lib/esm/client/client.d.ts +0 -97
- package/lib/esm/client/client.js +0 -83
- package/lib/esm/client/index.d.ts +0 -1
- package/lib/esm/client/index.js +0 -1
- package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
- package/lib/esm/models/bedrock-chat-model.js +0 -298
- package/lib/esm/models/claude-chat-model.d.ts +0 -114
- package/lib/esm/models/claude-chat-model.js +0 -310
- package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
- package/lib/esm/models/deepseek-chat-model.js +0 -31
- package/lib/esm/models/gemini-chat-model.d.ts +0 -23
- package/lib/esm/models/gemini-chat-model.js +0 -31
- package/lib/esm/models/ollama-chat-model.d.ts +0 -22
- package/lib/esm/models/ollama-chat-model.js +0 -30
- package/lib/esm/models/open-router-chat-model.d.ts +0 -22
- package/lib/esm/models/open-router-chat-model.js +0 -30
- package/lib/esm/models/openai-chat-model.d.ts +0 -166
- package/lib/esm/models/openai-chat-model.js +0 -405
- package/lib/esm/models/xai-chat-model.d.ts +0 -21
- package/lib/esm/models/xai-chat-model.js +0 -29
- package/lib/esm/server/error.d.ts +0 -15
- package/lib/esm/server/error.js +0 -18
- package/lib/esm/server/index.d.ts +0 -2
- package/lib/esm/server/index.js +0 -2
- package/lib/esm/server/server.d.ts +0 -135
- package/lib/esm/server/server.js +0 -181
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BedrockChatModel = exports.bedrockChatModelOptionsSchema = void 0;
|
|
4
|
-
exports.extractLastJsonObject = extractLastJsonObject;
|
|
5
|
-
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
|
|
6
|
-
const nanoid_1 = require("nanoid");
|
|
7
|
-
const zod_1 = require("zod");
|
|
8
|
-
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
9
|
-
const model_utils_js_1 = require("../utils/model-utils.js");
|
|
10
|
-
const prompts_js_1 = require("../utils/prompts.js");
|
|
11
|
-
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
12
|
-
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
13
|
-
const chat_model_js_1 = require("./chat-model.js");
|
|
14
|
-
/**
|
|
15
|
-
* @hidden
|
|
16
|
-
*/
|
|
17
|
-
function extractLastJsonObject(text) {
|
|
18
|
-
return text.replace(/<thinking>[\s\S]*?<\/thinking>/g, "").trim();
|
|
19
|
-
}
|
|
20
|
-
const BEDROCK_DEFAULT_CHAT_MODEL = "us.amazon.nova-lite-v1:0";
|
|
21
|
-
/**
|
|
22
|
-
* @hidden
|
|
23
|
-
*/
|
|
24
|
-
exports.bedrockChatModelOptionsSchema = zod_1.z.object({
|
|
25
|
-
region: zod_1.z.string().optional(),
|
|
26
|
-
model: zod_1.z.string().optional(),
|
|
27
|
-
modelOptions: zod_1.z
|
|
28
|
-
.object({
|
|
29
|
-
model: zod_1.z.string().optional(),
|
|
30
|
-
temperature: zod_1.z.number().optional(),
|
|
31
|
-
topP: zod_1.z.number().optional(),
|
|
32
|
-
frequencyPenalty: zod_1.z.number().optional(),
|
|
33
|
-
presencePenalty: zod_1.z.number().optional(),
|
|
34
|
-
parallelToolCalls: zod_1.z.boolean().optional().default(true),
|
|
35
|
-
})
|
|
36
|
-
.optional(),
|
|
37
|
-
});
|
|
38
|
-
class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
39
|
-
options;
|
|
40
|
-
constructor(options) {
|
|
41
|
-
if (options)
|
|
42
|
-
(0, type_utils_js_1.checkArguments)("BedrockChatModel", exports.bedrockChatModelOptionsSchema, options);
|
|
43
|
-
super();
|
|
44
|
-
this.options = options;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* @hidden
|
|
48
|
-
*/
|
|
49
|
-
_client;
|
|
50
|
-
get client() {
|
|
51
|
-
const credentials = this.options?.accessKeyId && this.options?.secretAccessKey
|
|
52
|
-
? {
|
|
53
|
-
accessKeyId: this.options.accessKeyId,
|
|
54
|
-
secretAccessKey: this.options.secretAccessKey,
|
|
55
|
-
}
|
|
56
|
-
: undefined;
|
|
57
|
-
this._client ??= new client_bedrock_runtime_1.BedrockRuntimeClient({
|
|
58
|
-
region: this.options?.region,
|
|
59
|
-
credentials,
|
|
60
|
-
});
|
|
61
|
-
return this._client;
|
|
62
|
-
}
|
|
63
|
-
get modelOptions() {
|
|
64
|
-
return this.options?.modelOptions;
|
|
65
|
-
}
|
|
66
|
-
async process(input) {
|
|
67
|
-
const modelId = input.modelOptions?.model ?? this.modelOptions?.model ?? BEDROCK_DEFAULT_CHAT_MODEL;
|
|
68
|
-
const { messages, system } = getRunMessages(input);
|
|
69
|
-
const toolConfig = convertTools(input);
|
|
70
|
-
const body = {
|
|
71
|
-
modelId,
|
|
72
|
-
messages,
|
|
73
|
-
system,
|
|
74
|
-
toolConfig,
|
|
75
|
-
inferenceConfig: {
|
|
76
|
-
temperature: input.modelOptions?.temperature ?? this.modelOptions?.temperature,
|
|
77
|
-
topP: input.modelOptions?.topP ?? this.modelOptions?.topP,
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
const command = new client_bedrock_runtime_1.ConverseStreamCommand(body);
|
|
81
|
-
const response = await this.client.send(command);
|
|
82
|
-
const jsonMode = input.responseFormat?.type === "json_schema";
|
|
83
|
-
if (!jsonMode) {
|
|
84
|
-
return this.extractResultFromStream(response.stream, modelId, true);
|
|
85
|
-
}
|
|
86
|
-
const result = await this.extractResultFromStream(response.stream, modelId, false);
|
|
87
|
-
if (!result.toolCalls?.length && jsonMode && result.text) {
|
|
88
|
-
const output = await this.requestStructuredOutput(body, input.responseFormat);
|
|
89
|
-
return { ...output, usage: (0, model_utils_js_1.mergeUsage)(result.usage, output.usage) };
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
async extractResultFromStream(stream, modelId, streaming) {
|
|
94
|
-
if (!stream)
|
|
95
|
-
throw new Error("Unable to get AI model response.");
|
|
96
|
-
const result = new ReadableStream({
|
|
97
|
-
start: async (controller) => {
|
|
98
|
-
try {
|
|
99
|
-
controller.enqueue({ delta: { json: { model: modelId } } });
|
|
100
|
-
const toolCalls = [];
|
|
101
|
-
let usage;
|
|
102
|
-
for await (const chunk of stream) {
|
|
103
|
-
if (chunk.contentBlockStart?.start?.toolUse) {
|
|
104
|
-
const toolUse = chunk.contentBlockStart.start.toolUse;
|
|
105
|
-
if (!toolUse.name)
|
|
106
|
-
throw new Error("Tool use is invalid");
|
|
107
|
-
if (chunk.contentBlockStart.contentBlockIndex === undefined)
|
|
108
|
-
throw new Error("Tool use content block index is required");
|
|
109
|
-
toolCalls[chunk.contentBlockStart.contentBlockIndex] = {
|
|
110
|
-
type: "function",
|
|
111
|
-
id: toolUse.toolUseId || (0, nanoid_1.nanoid)(),
|
|
112
|
-
function: {
|
|
113
|
-
name: toolUse.name,
|
|
114
|
-
arguments: {},
|
|
115
|
-
},
|
|
116
|
-
args: "",
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
if (chunk.contentBlockDelta) {
|
|
120
|
-
const block = chunk.contentBlockDelta;
|
|
121
|
-
const delta = block.delta;
|
|
122
|
-
if (delta?.text) {
|
|
123
|
-
controller.enqueue({ delta: { text: { text: delta.text } } });
|
|
124
|
-
}
|
|
125
|
-
if (delta?.toolUse) {
|
|
126
|
-
if (block.contentBlockIndex === undefined)
|
|
127
|
-
throw new Error("Content block index is required");
|
|
128
|
-
const call = toolCalls[block.contentBlockIndex];
|
|
129
|
-
if (!call)
|
|
130
|
-
throw new Error("Tool call not found");
|
|
131
|
-
call.args += delta.toolUse.input;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (chunk.metadata) {
|
|
135
|
-
usage = chunk.metadata.usage;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (toolCalls.length) {
|
|
139
|
-
controller.enqueue({
|
|
140
|
-
delta: {
|
|
141
|
-
json: {
|
|
142
|
-
toolCalls: toolCalls
|
|
143
|
-
.map(({ args, ...c }) => ({
|
|
144
|
-
...c,
|
|
145
|
-
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
146
|
-
}))
|
|
147
|
-
.filter(type_utils_js_1.isNonNullable),
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
controller.enqueue({ delta: { json: { usage } } });
|
|
153
|
-
controller.close();
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
controller.error(error);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
161
|
-
}
|
|
162
|
-
async requestStructuredOutput(body, responseFormat) {
|
|
163
|
-
if (responseFormat?.type !== "json_schema") {
|
|
164
|
-
throw new Error("Expected json_schema response format");
|
|
165
|
-
}
|
|
166
|
-
const system = [
|
|
167
|
-
...(body.system ?? []),
|
|
168
|
-
{
|
|
169
|
-
text: `Use the generate_json tool to generate a json result. ${(0, prompts_js_1.getJsonToolInputPrompt)(responseFormat.jsonSchema.schema)}`,
|
|
170
|
-
},
|
|
171
|
-
];
|
|
172
|
-
const toolConfig = {
|
|
173
|
-
tools: [
|
|
174
|
-
{
|
|
175
|
-
toolSpec: {
|
|
176
|
-
name: "generate_json",
|
|
177
|
-
description: "Generate a json result by given context",
|
|
178
|
-
inputSchema: { json: responseFormat.jsonSchema.schema },
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
toolChoice: { tool: { name: "generate_json" } },
|
|
183
|
-
};
|
|
184
|
-
const command = new client_bedrock_runtime_1.ConverseCommand({ ...body, system, toolConfig });
|
|
185
|
-
const response = await this.client.send(command);
|
|
186
|
-
const jsonTool = response.output?.message?.content?.find((i) => i.toolUse?.name === "generate_json");
|
|
187
|
-
if (!jsonTool)
|
|
188
|
-
throw new Error("Json tool not found");
|
|
189
|
-
return {
|
|
190
|
-
json: jsonTool.toolUse?.input,
|
|
191
|
-
model: body.modelId,
|
|
192
|
-
usage: response.usage,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
exports.BedrockChatModel = BedrockChatModel;
|
|
197
|
-
const getRunMessages = ({ messages: msgs, }) => {
|
|
198
|
-
const system = [];
|
|
199
|
-
const messages = [];
|
|
200
|
-
for (const msg of msgs) {
|
|
201
|
-
if (msg.role === "system") {
|
|
202
|
-
if (typeof msg.content !== "string")
|
|
203
|
-
throw new Error("System message must have content");
|
|
204
|
-
system.push({ text: msg.content });
|
|
205
|
-
}
|
|
206
|
-
else if (msg.role === "tool") {
|
|
207
|
-
if (!msg.toolCallId)
|
|
208
|
-
throw new Error("Tool message must have toolCallId");
|
|
209
|
-
if (typeof msg.content !== "string")
|
|
210
|
-
throw new Error("Tool message must have string content");
|
|
211
|
-
if (messages.at(-1)?.role === "user") {
|
|
212
|
-
messages.at(-1)?.content?.push({
|
|
213
|
-
toolResult: { toolUseId: msg.toolCallId, content: [{ json: (0, json_schema_js_1.parseJSON)(msg.content) }] },
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
messages.push({
|
|
218
|
-
role: "user",
|
|
219
|
-
content: [
|
|
220
|
-
{
|
|
221
|
-
toolResult: {
|
|
222
|
-
toolUseId: msg.toolCallId,
|
|
223
|
-
content: [{ json: (0, json_schema_js_1.parseJSON)(msg.content) }],
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
],
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
else if (msg.role === "user") {
|
|
231
|
-
if (!msg.content)
|
|
232
|
-
throw new Error("User message must have content");
|
|
233
|
-
messages.push({ role: "user", content: convertContent(msg.content) });
|
|
234
|
-
}
|
|
235
|
-
else if (msg.role === "agent") {
|
|
236
|
-
if (msg.toolCalls?.length) {
|
|
237
|
-
messages.push({
|
|
238
|
-
role: "assistant",
|
|
239
|
-
content: msg.toolCalls.map((i) => ({
|
|
240
|
-
toolUse: {
|
|
241
|
-
toolUseId: i.id,
|
|
242
|
-
name: i.function.name,
|
|
243
|
-
input: i.function.arguments,
|
|
244
|
-
},
|
|
245
|
-
})),
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
else if (msg.content) {
|
|
249
|
-
messages.push({ role: "assistant", content: convertContent(msg.content) });
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
throw new Error("Agent message must have content or toolCalls");
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
if (messages.at(0)?.role !== "user") {
|
|
257
|
-
messages.unshift({ role: "user", content: [{ text: "." }] });
|
|
258
|
-
}
|
|
259
|
-
return { messages, system };
|
|
260
|
-
};
|
|
261
|
-
function convertContent(content) {
|
|
262
|
-
if (typeof content === "string")
|
|
263
|
-
return [{ text: content }];
|
|
264
|
-
if (Array.isArray(content)) {
|
|
265
|
-
const blocks = [];
|
|
266
|
-
for (const item of content) {
|
|
267
|
-
if (item.type === "text")
|
|
268
|
-
blocks.push({ text: item.text });
|
|
269
|
-
}
|
|
270
|
-
return blocks;
|
|
271
|
-
}
|
|
272
|
-
throw new Error("Invalid chat message content");
|
|
273
|
-
}
|
|
274
|
-
function convertTools({ tools, toolChoice }) {
|
|
275
|
-
if (!tools?.length || toolChoice === "none")
|
|
276
|
-
return undefined;
|
|
277
|
-
let choice;
|
|
278
|
-
if (typeof toolChoice === "object" && "type" in toolChoice && toolChoice.type === "function") {
|
|
279
|
-
choice = { tool: { name: toolChoice.function.name } };
|
|
280
|
-
}
|
|
281
|
-
else if (toolChoice === "required") {
|
|
282
|
-
choice = { any: {} };
|
|
283
|
-
}
|
|
284
|
-
else if (toolChoice === "auto") {
|
|
285
|
-
choice = { auto: {} };
|
|
286
|
-
}
|
|
287
|
-
return {
|
|
288
|
-
tools: tools.map((i) => {
|
|
289
|
-
const parameters = i.function.parameters;
|
|
290
|
-
if (Object.keys(parameters).length === 0) {
|
|
291
|
-
parameters.type = "object";
|
|
292
|
-
}
|
|
293
|
-
return {
|
|
294
|
-
toolSpec: {
|
|
295
|
-
name: i.function.name,
|
|
296
|
-
description: i.function.description,
|
|
297
|
-
inputSchema: { json: parameters },
|
|
298
|
-
},
|
|
299
|
-
};
|
|
300
|
-
}),
|
|
301
|
-
toolChoice: choice,
|
|
302
|
-
};
|
|
303
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import type { AgentProcessResult } from "../agents/agent.js";
|
|
4
|
-
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
|
-
import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
|
|
6
|
-
/**
|
|
7
|
-
* Configuration options for Claude Chat Model
|
|
8
|
-
*/
|
|
9
|
-
export interface ClaudeChatModelOptions {
|
|
10
|
-
/**
|
|
11
|
-
* API key for Anthropic's Claude API
|
|
12
|
-
*
|
|
13
|
-
* If not provided, will look for ANTHROPIC_API_KEY or CLAUDE_API_KEY in environment variables
|
|
14
|
-
*/
|
|
15
|
-
apiKey?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Claude model to use
|
|
18
|
-
*
|
|
19
|
-
* Defaults to 'claude-3-7-sonnet-latest'
|
|
20
|
-
*/
|
|
21
|
-
model?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Additional model options to control behavior
|
|
24
|
-
*/
|
|
25
|
-
modelOptions?: ChatModelOptions;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @hidden
|
|
29
|
-
*/
|
|
30
|
-
export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
31
|
-
apiKey: z.ZodOptional<z.ZodString>;
|
|
32
|
-
model: z.ZodOptional<z.ZodString>;
|
|
33
|
-
modelOptions: z.ZodOptional<z.ZodObject<{
|
|
34
|
-
model: z.ZodOptional<z.ZodString>;
|
|
35
|
-
temperature: z.ZodOptional<z.ZodNumber>;
|
|
36
|
-
topP: z.ZodOptional<z.ZodNumber>;
|
|
37
|
-
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
38
|
-
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
39
|
-
parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
40
|
-
}, "strip", z.ZodTypeAny, {
|
|
41
|
-
parallelToolCalls: boolean;
|
|
42
|
-
model?: string | undefined;
|
|
43
|
-
temperature?: number | undefined;
|
|
44
|
-
topP?: number | undefined;
|
|
45
|
-
frequencyPenalty?: number | undefined;
|
|
46
|
-
presencePenalty?: number | undefined;
|
|
47
|
-
}, {
|
|
48
|
-
model?: string | undefined;
|
|
49
|
-
temperature?: number | undefined;
|
|
50
|
-
topP?: number | undefined;
|
|
51
|
-
frequencyPenalty?: number | undefined;
|
|
52
|
-
presencePenalty?: number | undefined;
|
|
53
|
-
parallelToolCalls?: boolean | undefined;
|
|
54
|
-
}>>;
|
|
55
|
-
}, "strip", z.ZodTypeAny, {
|
|
56
|
-
modelOptions?: {
|
|
57
|
-
parallelToolCalls: boolean;
|
|
58
|
-
model?: string | undefined;
|
|
59
|
-
temperature?: number | undefined;
|
|
60
|
-
topP?: number | undefined;
|
|
61
|
-
frequencyPenalty?: number | undefined;
|
|
62
|
-
presencePenalty?: number | undefined;
|
|
63
|
-
} | undefined;
|
|
64
|
-
model?: string | undefined;
|
|
65
|
-
apiKey?: string | undefined;
|
|
66
|
-
}, {
|
|
67
|
-
modelOptions?: {
|
|
68
|
-
model?: string | undefined;
|
|
69
|
-
temperature?: number | undefined;
|
|
70
|
-
topP?: number | undefined;
|
|
71
|
-
frequencyPenalty?: number | undefined;
|
|
72
|
-
presencePenalty?: number | undefined;
|
|
73
|
-
parallelToolCalls?: boolean | undefined;
|
|
74
|
-
} | undefined;
|
|
75
|
-
model?: string | undefined;
|
|
76
|
-
apiKey?: string | undefined;
|
|
77
|
-
}>;
|
|
78
|
-
/**
|
|
79
|
-
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
80
|
-
*
|
|
81
|
-
* This model provides access to Claude's capabilities including:
|
|
82
|
-
* - Text generation
|
|
83
|
-
* - Tool use
|
|
84
|
-
* - JSON structured output
|
|
85
|
-
*
|
|
86
|
-
* Default model: 'claude-3-7-sonnet-latest'
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* Here's how to create and use a Claude chat model:
|
|
90
|
-
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model}
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* Here's an example with streaming response:
|
|
94
|
-
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model-streaming-async-generator}
|
|
95
|
-
*/
|
|
96
|
-
export declare class ClaudeChatModel extends ChatModel {
|
|
97
|
-
options?: ClaudeChatModelOptions | undefined;
|
|
98
|
-
constructor(options?: ClaudeChatModelOptions | undefined);
|
|
99
|
-
/**
|
|
100
|
-
* @hidden
|
|
101
|
-
*/
|
|
102
|
-
protected _client?: Anthropic;
|
|
103
|
-
get client(): Anthropic;
|
|
104
|
-
get modelOptions(): ChatModelOptions | undefined;
|
|
105
|
-
/**
|
|
106
|
-
* Process the input using Claude's chat model
|
|
107
|
-
* @param input - The input to process
|
|
108
|
-
* @returns The processed output from the model
|
|
109
|
-
*/
|
|
110
|
-
process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
111
|
-
private _process;
|
|
112
|
-
private extractResultFromClaudeStream;
|
|
113
|
-
private requestStructuredOutput;
|
|
114
|
-
}
|