@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,298 +0,0 @@
|
|
|
1
|
-
import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand, } from "@aws-sdk/client-bedrock-runtime";
|
|
2
|
-
import { nanoid } from "nanoid";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
import { parseJSON } from "../utils/json-schema.js";
|
|
5
|
-
import { mergeUsage } from "../utils/model-utils.js";
|
|
6
|
-
import { getJsonToolInputPrompt } from "../utils/prompts.js";
|
|
7
|
-
import { agentResponseStreamToObject } from "../utils/stream-utils.js";
|
|
8
|
-
import { checkArguments, isNonNullable } from "../utils/type-utils.js";
|
|
9
|
-
import { ChatModel, } from "./chat-model.js";
|
|
10
|
-
/**
|
|
11
|
-
* @hidden
|
|
12
|
-
*/
|
|
13
|
-
export function extractLastJsonObject(text) {
|
|
14
|
-
return text.replace(/<thinking>[\s\S]*?<\/thinking>/g, "").trim();
|
|
15
|
-
}
|
|
16
|
-
const BEDROCK_DEFAULT_CHAT_MODEL = "us.amazon.nova-lite-v1:0";
|
|
17
|
-
/**
|
|
18
|
-
* @hidden
|
|
19
|
-
*/
|
|
20
|
-
export const bedrockChatModelOptionsSchema = z.object({
|
|
21
|
-
region: z.string().optional(),
|
|
22
|
-
model: z.string().optional(),
|
|
23
|
-
modelOptions: z
|
|
24
|
-
.object({
|
|
25
|
-
model: z.string().optional(),
|
|
26
|
-
temperature: z.number().optional(),
|
|
27
|
-
topP: z.number().optional(),
|
|
28
|
-
frequencyPenalty: z.number().optional(),
|
|
29
|
-
presencePenalty: z.number().optional(),
|
|
30
|
-
parallelToolCalls: z.boolean().optional().default(true),
|
|
31
|
-
})
|
|
32
|
-
.optional(),
|
|
33
|
-
});
|
|
34
|
-
export class BedrockChatModel extends ChatModel {
|
|
35
|
-
options;
|
|
36
|
-
constructor(options) {
|
|
37
|
-
if (options)
|
|
38
|
-
checkArguments("BedrockChatModel", bedrockChatModelOptionsSchema, options);
|
|
39
|
-
super();
|
|
40
|
-
this.options = options;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* @hidden
|
|
44
|
-
*/
|
|
45
|
-
_client;
|
|
46
|
-
get client() {
|
|
47
|
-
const credentials = this.options?.accessKeyId && this.options?.secretAccessKey
|
|
48
|
-
? {
|
|
49
|
-
accessKeyId: this.options.accessKeyId,
|
|
50
|
-
secretAccessKey: this.options.secretAccessKey,
|
|
51
|
-
}
|
|
52
|
-
: undefined;
|
|
53
|
-
this._client ??= new BedrockRuntimeClient({
|
|
54
|
-
region: this.options?.region,
|
|
55
|
-
credentials,
|
|
56
|
-
});
|
|
57
|
-
return this._client;
|
|
58
|
-
}
|
|
59
|
-
get modelOptions() {
|
|
60
|
-
return this.options?.modelOptions;
|
|
61
|
-
}
|
|
62
|
-
async process(input) {
|
|
63
|
-
const modelId = input.modelOptions?.model ?? this.modelOptions?.model ?? BEDROCK_DEFAULT_CHAT_MODEL;
|
|
64
|
-
const { messages, system } = getRunMessages(input);
|
|
65
|
-
const toolConfig = convertTools(input);
|
|
66
|
-
const body = {
|
|
67
|
-
modelId,
|
|
68
|
-
messages,
|
|
69
|
-
system,
|
|
70
|
-
toolConfig,
|
|
71
|
-
inferenceConfig: {
|
|
72
|
-
temperature: input.modelOptions?.temperature ?? this.modelOptions?.temperature,
|
|
73
|
-
topP: input.modelOptions?.topP ?? this.modelOptions?.topP,
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
const command = new ConverseStreamCommand(body);
|
|
77
|
-
const response = await this.client.send(command);
|
|
78
|
-
const jsonMode = input.responseFormat?.type === "json_schema";
|
|
79
|
-
if (!jsonMode) {
|
|
80
|
-
return this.extractResultFromStream(response.stream, modelId, true);
|
|
81
|
-
}
|
|
82
|
-
const result = await this.extractResultFromStream(response.stream, modelId, false);
|
|
83
|
-
if (!result.toolCalls?.length && jsonMode && result.text) {
|
|
84
|
-
const output = await this.requestStructuredOutput(body, input.responseFormat);
|
|
85
|
-
return { ...output, usage: mergeUsage(result.usage, output.usage) };
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
async extractResultFromStream(stream, modelId, streaming) {
|
|
90
|
-
if (!stream)
|
|
91
|
-
throw new Error("Unable to get AI model response.");
|
|
92
|
-
const result = new ReadableStream({
|
|
93
|
-
start: async (controller) => {
|
|
94
|
-
try {
|
|
95
|
-
controller.enqueue({ delta: { json: { model: modelId } } });
|
|
96
|
-
const toolCalls = [];
|
|
97
|
-
let usage;
|
|
98
|
-
for await (const chunk of stream) {
|
|
99
|
-
if (chunk.contentBlockStart?.start?.toolUse) {
|
|
100
|
-
const toolUse = chunk.contentBlockStart.start.toolUse;
|
|
101
|
-
if (!toolUse.name)
|
|
102
|
-
throw new Error("Tool use is invalid");
|
|
103
|
-
if (chunk.contentBlockStart.contentBlockIndex === undefined)
|
|
104
|
-
throw new Error("Tool use content block index is required");
|
|
105
|
-
toolCalls[chunk.contentBlockStart.contentBlockIndex] = {
|
|
106
|
-
type: "function",
|
|
107
|
-
id: toolUse.toolUseId || nanoid(),
|
|
108
|
-
function: {
|
|
109
|
-
name: toolUse.name,
|
|
110
|
-
arguments: {},
|
|
111
|
-
},
|
|
112
|
-
args: "",
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
if (chunk.contentBlockDelta) {
|
|
116
|
-
const block = chunk.contentBlockDelta;
|
|
117
|
-
const delta = block.delta;
|
|
118
|
-
if (delta?.text) {
|
|
119
|
-
controller.enqueue({ delta: { text: { text: delta.text } } });
|
|
120
|
-
}
|
|
121
|
-
if (delta?.toolUse) {
|
|
122
|
-
if (block.contentBlockIndex === undefined)
|
|
123
|
-
throw new Error("Content block index is required");
|
|
124
|
-
const call = toolCalls[block.contentBlockIndex];
|
|
125
|
-
if (!call)
|
|
126
|
-
throw new Error("Tool call not found");
|
|
127
|
-
call.args += delta.toolUse.input;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (chunk.metadata) {
|
|
131
|
-
usage = chunk.metadata.usage;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (toolCalls.length) {
|
|
135
|
-
controller.enqueue({
|
|
136
|
-
delta: {
|
|
137
|
-
json: {
|
|
138
|
-
toolCalls: toolCalls
|
|
139
|
-
.map(({ args, ...c }) => ({
|
|
140
|
-
...c,
|
|
141
|
-
function: { ...c.function, arguments: parseJSON(args) },
|
|
142
|
-
}))
|
|
143
|
-
.filter(isNonNullable),
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
controller.enqueue({ delta: { json: { usage } } });
|
|
149
|
-
controller.close();
|
|
150
|
-
}
|
|
151
|
-
catch (error) {
|
|
152
|
-
controller.error(error);
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
return streaming ? result : await agentResponseStreamToObject(result);
|
|
157
|
-
}
|
|
158
|
-
async requestStructuredOutput(body, responseFormat) {
|
|
159
|
-
if (responseFormat?.type !== "json_schema") {
|
|
160
|
-
throw new Error("Expected json_schema response format");
|
|
161
|
-
}
|
|
162
|
-
const system = [
|
|
163
|
-
...(body.system ?? []),
|
|
164
|
-
{
|
|
165
|
-
text: `Use the generate_json tool to generate a json result. ${getJsonToolInputPrompt(responseFormat.jsonSchema.schema)}`,
|
|
166
|
-
},
|
|
167
|
-
];
|
|
168
|
-
const toolConfig = {
|
|
169
|
-
tools: [
|
|
170
|
-
{
|
|
171
|
-
toolSpec: {
|
|
172
|
-
name: "generate_json",
|
|
173
|
-
description: "Generate a json result by given context",
|
|
174
|
-
inputSchema: { json: responseFormat.jsonSchema.schema },
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
],
|
|
178
|
-
toolChoice: { tool: { name: "generate_json" } },
|
|
179
|
-
};
|
|
180
|
-
const command = new ConverseCommand({ ...body, system, toolConfig });
|
|
181
|
-
const response = await this.client.send(command);
|
|
182
|
-
const jsonTool = response.output?.message?.content?.find((i) => i.toolUse?.name === "generate_json");
|
|
183
|
-
if (!jsonTool)
|
|
184
|
-
throw new Error("Json tool not found");
|
|
185
|
-
return {
|
|
186
|
-
json: jsonTool.toolUse?.input,
|
|
187
|
-
model: body.modelId,
|
|
188
|
-
usage: response.usage,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
const getRunMessages = ({ messages: msgs, }) => {
|
|
193
|
-
const system = [];
|
|
194
|
-
const messages = [];
|
|
195
|
-
for (const msg of msgs) {
|
|
196
|
-
if (msg.role === "system") {
|
|
197
|
-
if (typeof msg.content !== "string")
|
|
198
|
-
throw new Error("System message must have content");
|
|
199
|
-
system.push({ text: msg.content });
|
|
200
|
-
}
|
|
201
|
-
else if (msg.role === "tool") {
|
|
202
|
-
if (!msg.toolCallId)
|
|
203
|
-
throw new Error("Tool message must have toolCallId");
|
|
204
|
-
if (typeof msg.content !== "string")
|
|
205
|
-
throw new Error("Tool message must have string content");
|
|
206
|
-
if (messages.at(-1)?.role === "user") {
|
|
207
|
-
messages.at(-1)?.content?.push({
|
|
208
|
-
toolResult: { toolUseId: msg.toolCallId, content: [{ json: parseJSON(msg.content) }] },
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
messages.push({
|
|
213
|
-
role: "user",
|
|
214
|
-
content: [
|
|
215
|
-
{
|
|
216
|
-
toolResult: {
|
|
217
|
-
toolUseId: msg.toolCallId,
|
|
218
|
-
content: [{ json: parseJSON(msg.content) }],
|
|
219
|
-
},
|
|
220
|
-
},
|
|
221
|
-
],
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
else if (msg.role === "user") {
|
|
226
|
-
if (!msg.content)
|
|
227
|
-
throw new Error("User message must have content");
|
|
228
|
-
messages.push({ role: "user", content: convertContent(msg.content) });
|
|
229
|
-
}
|
|
230
|
-
else if (msg.role === "agent") {
|
|
231
|
-
if (msg.toolCalls?.length) {
|
|
232
|
-
messages.push({
|
|
233
|
-
role: "assistant",
|
|
234
|
-
content: msg.toolCalls.map((i) => ({
|
|
235
|
-
toolUse: {
|
|
236
|
-
toolUseId: i.id,
|
|
237
|
-
name: i.function.name,
|
|
238
|
-
input: i.function.arguments,
|
|
239
|
-
},
|
|
240
|
-
})),
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
else if (msg.content) {
|
|
244
|
-
messages.push({ role: "assistant", content: convertContent(msg.content) });
|
|
245
|
-
}
|
|
246
|
-
else {
|
|
247
|
-
throw new Error("Agent message must have content or toolCalls");
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (messages.at(0)?.role !== "user") {
|
|
252
|
-
messages.unshift({ role: "user", content: [{ text: "." }] });
|
|
253
|
-
}
|
|
254
|
-
return { messages, system };
|
|
255
|
-
};
|
|
256
|
-
function convertContent(content) {
|
|
257
|
-
if (typeof content === "string")
|
|
258
|
-
return [{ text: content }];
|
|
259
|
-
if (Array.isArray(content)) {
|
|
260
|
-
const blocks = [];
|
|
261
|
-
for (const item of content) {
|
|
262
|
-
if (item.type === "text")
|
|
263
|
-
blocks.push({ text: item.text });
|
|
264
|
-
}
|
|
265
|
-
return blocks;
|
|
266
|
-
}
|
|
267
|
-
throw new Error("Invalid chat message content");
|
|
268
|
-
}
|
|
269
|
-
function convertTools({ tools, toolChoice }) {
|
|
270
|
-
if (!tools?.length || toolChoice === "none")
|
|
271
|
-
return undefined;
|
|
272
|
-
let choice;
|
|
273
|
-
if (typeof toolChoice === "object" && "type" in toolChoice && toolChoice.type === "function") {
|
|
274
|
-
choice = { tool: { name: toolChoice.function.name } };
|
|
275
|
-
}
|
|
276
|
-
else if (toolChoice === "required") {
|
|
277
|
-
choice = { any: {} };
|
|
278
|
-
}
|
|
279
|
-
else if (toolChoice === "auto") {
|
|
280
|
-
choice = { auto: {} };
|
|
281
|
-
}
|
|
282
|
-
return {
|
|
283
|
-
tools: tools.map((i) => {
|
|
284
|
-
const parameters = i.function.parameters;
|
|
285
|
-
if (Object.keys(parameters).length === 0) {
|
|
286
|
-
parameters.type = "object";
|
|
287
|
-
}
|
|
288
|
-
return {
|
|
289
|
-
toolSpec: {
|
|
290
|
-
name: i.function.name,
|
|
291
|
-
description: i.function.description,
|
|
292
|
-
inputSchema: { json: parameters },
|
|
293
|
-
},
|
|
294
|
-
};
|
|
295
|
-
}),
|
|
296
|
-
toolChoice: choice,
|
|
297
|
-
};
|
|
298
|
-
}
|
|
@@ -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
|
-
}
|