@aigne/core 1.13.0 → 1.14.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 +15 -0
- package/README.md +13 -26
- package/README.zh.md +24 -37
- package/lib/cjs/agents/agent.d.ts +522 -15
- package/lib/cjs/agents/agent.js +357 -36
- package/lib/cjs/agents/ai-agent.d.ts +210 -52
- package/lib/cjs/agents/ai-agent.js +182 -24
- package/lib/cjs/agents/mcp-agent.d.ts +112 -0
- package/lib/cjs/agents/mcp-agent.js +79 -1
- package/lib/cjs/agents/team-agent.d.ts +99 -0
- package/lib/cjs/agents/team-agent.js +94 -0
- package/lib/cjs/agents/user-agent.d.ts +6 -4
- package/lib/cjs/agents/user-agent.js +16 -5
- package/lib/cjs/aigne/aigne.d.ts +263 -16
- package/lib/cjs/aigne/aigne.js +130 -20
- package/lib/cjs/aigne/context.d.ts +24 -8
- package/lib/cjs/aigne/context.js +8 -22
- package/lib/cjs/aigne/message-queue.d.ts +26 -4
- package/lib/cjs/aigne/message-queue.js +42 -7
- package/lib/cjs/aigne/usage.d.ts +9 -0
- package/lib/cjs/aigne/usage.js +3 -0
- package/lib/cjs/client/client.d.ts +81 -3
- package/lib/cjs/client/client.js +38 -0
- package/lib/cjs/client/index.d.ts +1 -0
- package/lib/cjs/client/index.js +17 -0
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +2 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/memory/default-memory.d.ts +16 -0
- package/lib/cjs/memory/default-memory.js +70 -0
- package/lib/cjs/memory/index.d.ts +3 -0
- package/lib/cjs/memory/index.js +19 -0
- package/lib/cjs/memory/memory.d.ts +89 -0
- package/lib/cjs/memory/memory.js +132 -0
- package/lib/cjs/memory/recorder.d.ts +86 -0
- package/lib/cjs/memory/recorder.js +50 -0
- package/lib/cjs/memory/retriever.d.ts +99 -0
- package/lib/cjs/memory/retriever.js +51 -0
- package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
- package/lib/cjs/models/bedrock-chat-model.js +54 -24
- package/lib/cjs/models/chat-model.d.ts +278 -1
- package/lib/cjs/models/chat-model.js +54 -0
- package/lib/cjs/models/claude-chat-model.d.ts +49 -3
- package/lib/cjs/models/claude-chat-model.js +34 -2
- package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
- package/lib/cjs/models/deepseek-chat-model.js +16 -0
- package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
- package/lib/cjs/models/gemini-chat-model.js +14 -0
- package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
- package/lib/cjs/models/ollama-chat-model.js +16 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
- package/lib/cjs/models/open-router-chat-model.js +16 -0
- package/lib/cjs/models/openai-chat-model.d.ts +67 -3
- package/lib/cjs/models/openai-chat-model.js +47 -2
- package/lib/cjs/models/xai-chat-model.d.ts +16 -0
- package/lib/cjs/models/xai-chat-model.js +16 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
- package/lib/cjs/prompt/prompt-builder.js +19 -18
- package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
- package/lib/cjs/prompt/template.js +5 -1
- package/lib/cjs/server/error.d.ts +11 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +18 -0
- package/lib/cjs/server/server.d.ts +89 -8
- package/lib/cjs/server/server.js +58 -0
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/cjs/utils/prompts.d.ts +1 -0
- package/lib/cjs/utils/prompts.js +11 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +14 -0
- package/lib/dts/agents/agent.d.ts +522 -15
- package/lib/dts/agents/ai-agent.d.ts +210 -52
- package/lib/dts/agents/mcp-agent.d.ts +112 -0
- package/lib/dts/agents/team-agent.d.ts +99 -0
- package/lib/dts/agents/user-agent.d.ts +6 -4
- package/lib/dts/aigne/aigne.d.ts +263 -16
- package/lib/dts/aigne/context.d.ts +24 -8
- package/lib/dts/aigne/message-queue.d.ts +26 -4
- package/lib/dts/aigne/usage.d.ts +9 -0
- package/lib/dts/client/client.d.ts +81 -3
- package/lib/dts/client/index.d.ts +1 -0
- package/lib/dts/index.d.ts +0 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +4 -4
- package/lib/dts/memory/default-memory.d.ts +16 -0
- package/lib/dts/memory/index.d.ts +3 -0
- package/lib/dts/memory/memory.d.ts +89 -0
- package/lib/dts/memory/recorder.d.ts +86 -0
- package/lib/dts/memory/retriever.d.ts +99 -0
- package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
- package/lib/dts/models/chat-model.d.ts +278 -1
- package/lib/dts/models/claude-chat-model.d.ts +49 -3
- package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
- package/lib/dts/models/gemini-chat-model.d.ts +14 -0
- package/lib/dts/models/ollama-chat-model.d.ts +16 -0
- package/lib/dts/models/open-router-chat-model.d.ts +16 -0
- package/lib/dts/models/openai-chat-model.d.ts +67 -3
- package/lib/dts/models/xai-chat-model.d.ts +16 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -4
- package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/dts/server/error.d.ts +11 -0
- package/lib/dts/server/index.d.ts +2 -0
- package/lib/dts/server/server.d.ts +89 -8
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/dts/utils/prompts.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +522 -15
- package/lib/esm/agents/agent.js +351 -35
- package/lib/esm/agents/ai-agent.d.ts +210 -52
- package/lib/esm/agents/ai-agent.js +183 -25
- package/lib/esm/agents/mcp-agent.d.ts +112 -0
- package/lib/esm/agents/mcp-agent.js +79 -1
- package/lib/esm/agents/team-agent.d.ts +99 -0
- package/lib/esm/agents/team-agent.js +94 -0
- package/lib/esm/agents/user-agent.d.ts +6 -4
- package/lib/esm/agents/user-agent.js +17 -6
- package/lib/esm/aigne/aigne.d.ts +263 -16
- package/lib/esm/aigne/aigne.js +132 -22
- package/lib/esm/aigne/context.d.ts +24 -8
- package/lib/esm/aigne/context.js +9 -22
- package/lib/esm/aigne/message-queue.d.ts +26 -4
- package/lib/esm/aigne/message-queue.js +42 -8
- package/lib/esm/aigne/usage.d.ts +9 -0
- package/lib/esm/aigne/usage.js +3 -0
- package/lib/esm/client/client.d.ts +81 -3
- package/lib/esm/client/client.js +38 -0
- package/lib/esm/client/index.d.ts +1 -0
- package/lib/esm/client/index.js +1 -0
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +2 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/memory/default-memory.d.ts +16 -0
- package/lib/esm/memory/default-memory.js +63 -0
- package/lib/esm/memory/index.d.ts +3 -0
- package/lib/esm/memory/index.js +3 -0
- package/lib/esm/memory/memory.d.ts +89 -0
- package/lib/esm/memory/memory.js +127 -0
- package/lib/esm/memory/recorder.d.ts +86 -0
- package/lib/esm/memory/recorder.js +46 -0
- package/lib/esm/memory/retriever.d.ts +99 -0
- package/lib/esm/memory/retriever.js +47 -0
- package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
- package/lib/esm/models/bedrock-chat-model.js +56 -26
- package/lib/esm/models/chat-model.d.ts +278 -1
- package/lib/esm/models/chat-model.js +54 -0
- package/lib/esm/models/claude-chat-model.d.ts +49 -3
- package/lib/esm/models/claude-chat-model.js +35 -3
- package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
- package/lib/esm/models/deepseek-chat-model.js +16 -0
- package/lib/esm/models/gemini-chat-model.d.ts +14 -0
- package/lib/esm/models/gemini-chat-model.js +14 -0
- package/lib/esm/models/ollama-chat-model.d.ts +16 -0
- package/lib/esm/models/ollama-chat-model.js +16 -0
- package/lib/esm/models/open-router-chat-model.d.ts +16 -0
- package/lib/esm/models/open-router-chat-model.js +16 -0
- package/lib/esm/models/openai-chat-model.d.ts +67 -3
- package/lib/esm/models/openai-chat-model.js +47 -2
- package/lib/esm/models/xai-chat-model.d.ts +16 -0
- package/lib/esm/models/xai-chat-model.js +16 -0
- package/lib/esm/prompt/prompt-builder.d.ts +4 -4
- package/lib/esm/prompt/prompt-builder.js +20 -19
- package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
- package/lib/esm/prompt/template.js +5 -1
- package/lib/esm/server/error.d.ts +11 -0
- package/lib/esm/server/error.js +11 -0
- package/lib/esm/server/index.d.ts +2 -0
- package/lib/esm/server/index.js +2 -0
- package/lib/esm/server/server.d.ts +89 -8
- package/lib/esm/server/server.js +58 -0
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/lib/esm/utils/prompts.d.ts +1 -0
- package/lib/esm/utils/prompts.js +10 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +13 -0
- package/package.json +14 -11
- package/lib/cjs/agents/memory.d.ts +0 -26
- package/lib/cjs/agents/memory.js +0 -45
- package/lib/dts/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.js +0 -41
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -6,14 +6,21 @@ const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
|
|
|
6
6
|
const nanoid_1 = require("nanoid");
|
|
7
7
|
const zod_1 = require("zod");
|
|
8
8
|
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
9
|
+
const model_utils_js_1 = require("../utils/model-utils.js");
|
|
9
10
|
const prompts_js_1 = require("../utils/prompts.js");
|
|
10
11
|
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
11
12
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
12
13
|
const chat_model_js_1 = require("./chat-model.js");
|
|
14
|
+
/**
|
|
15
|
+
* @hidden
|
|
16
|
+
*/
|
|
13
17
|
function extractLastJsonObject(text) {
|
|
14
18
|
return text.replace(/<thinking>[\s\S]*?<\/thinking>/g, "").trim();
|
|
15
19
|
}
|
|
16
20
|
const BEDROCK_DEFAULT_CHAT_MODEL = "us.amazon.nova-lite-v1:0";
|
|
21
|
+
/**
|
|
22
|
+
* @hidden
|
|
23
|
+
*/
|
|
17
24
|
exports.bedrockChatModelOptionsSchema = zod_1.z.object({
|
|
18
25
|
region: zod_1.z.string().optional(),
|
|
19
26
|
model: zod_1.z.string().optional(),
|
|
@@ -36,6 +43,9 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
36
43
|
super();
|
|
37
44
|
this.options = options;
|
|
38
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* @hidden
|
|
48
|
+
*/
|
|
39
49
|
_client;
|
|
40
50
|
get client() {
|
|
41
51
|
const credentials = this.options?.accessKeyId && this.options?.secretAccessKey
|
|
@@ -53,7 +63,7 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
53
63
|
get modelOptions() {
|
|
54
64
|
return this.options?.modelOptions;
|
|
55
65
|
}
|
|
56
|
-
async process(input
|
|
66
|
+
async process(input) {
|
|
57
67
|
const modelId = input.modelOptions?.model ?? this.modelOptions?.model ?? BEDROCK_DEFAULT_CHAT_MODEL;
|
|
58
68
|
const { messages, system } = getRunMessages(input);
|
|
59
69
|
const toolConfig = convertTools(input);
|
|
@@ -70,13 +80,17 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
70
80
|
const command = new client_bedrock_runtime_1.ConverseStreamCommand(body);
|
|
71
81
|
const response = await this.client.send(command);
|
|
72
82
|
const jsonMode = input.responseFormat?.type === "json_schema";
|
|
73
|
-
if (
|
|
74
|
-
return this.extractResultFromStream(response.stream, modelId,
|
|
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) };
|
|
75
90
|
}
|
|
76
|
-
const result = await this.extractResultFromStream(response.stream, modelId, jsonMode, false);
|
|
77
91
|
return result;
|
|
78
92
|
}
|
|
79
|
-
async extractResultFromStream(stream, modelId,
|
|
93
|
+
async extractResultFromStream(stream, modelId, streaming) {
|
|
80
94
|
if (!stream)
|
|
81
95
|
throw new Error("Unable to get AI model response.");
|
|
82
96
|
const result = new ReadableStream({
|
|
@@ -84,7 +98,6 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
84
98
|
try {
|
|
85
99
|
controller.enqueue({ delta: { json: { model: modelId } } });
|
|
86
100
|
const toolCalls = [];
|
|
87
|
-
let text = "";
|
|
88
101
|
let usage;
|
|
89
102
|
for await (const chunk of stream) {
|
|
90
103
|
if (chunk.contentBlockStart?.start?.toolUse) {
|
|
@@ -107,10 +120,7 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
107
120
|
const block = chunk.contentBlockDelta;
|
|
108
121
|
const delta = block.delta;
|
|
109
122
|
if (delta?.text) {
|
|
110
|
-
text
|
|
111
|
-
if (!jsonMode) {
|
|
112
|
-
controller.enqueue({ delta: { text: { text: delta.text } } });
|
|
113
|
-
}
|
|
123
|
+
controller.enqueue({ delta: { text: { text: delta.text } } });
|
|
114
124
|
}
|
|
115
125
|
if (delta?.toolUse) {
|
|
116
126
|
if (block.contentBlockIndex === undefined)
|
|
@@ -125,14 +135,6 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
125
135
|
usage = chunk.metadata.usage;
|
|
126
136
|
}
|
|
127
137
|
}
|
|
128
|
-
if (jsonMode && text) {
|
|
129
|
-
const match = extractLastJsonObject(text);
|
|
130
|
-
if (!match)
|
|
131
|
-
throw new Error("Failed to extract JSON object from model output");
|
|
132
|
-
controller.enqueue({
|
|
133
|
-
delta: { json: { json: (0, json_schema_js_1.parseJSON)(match) } },
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
138
|
if (toolCalls.length) {
|
|
137
139
|
controller.enqueue({
|
|
138
140
|
delta: {
|
|
@@ -157,9 +159,42 @@ class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
|
157
159
|
});
|
|
158
160
|
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
159
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
|
+
}
|
|
160
195
|
}
|
|
161
196
|
exports.BedrockChatModel = BedrockChatModel;
|
|
162
|
-
const getRunMessages = ({ messages: msgs,
|
|
197
|
+
const getRunMessages = ({ messages: msgs, }) => {
|
|
163
198
|
const system = [];
|
|
164
199
|
const messages = [];
|
|
165
200
|
for (const msg of msgs) {
|
|
@@ -221,11 +256,6 @@ const getRunMessages = ({ messages: msgs, responseFormat, }) => {
|
|
|
221
256
|
if (messages.at(0)?.role !== "user") {
|
|
222
257
|
messages.unshift({ role: "user", content: [{ text: "." }] });
|
|
223
258
|
}
|
|
224
|
-
if (responseFormat?.type === "json_schema") {
|
|
225
|
-
system.push({
|
|
226
|
-
text: (0, prompts_js_1.getJsonOutputPrompt)(responseFormat.jsonSchema.schema),
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
259
|
return { messages, system };
|
|
230
260
|
};
|
|
231
261
|
function convertContent(content) {
|
|
@@ -1,26 +1,154 @@
|
|
|
1
|
-
import { Agent, type Message } from "../agents/agent.js";
|
|
1
|
+
import { Agent, type AgentProcessResult, type Message } from "../agents/agent.js";
|
|
2
2
|
import type { Context } from "../aigne/context.js";
|
|
3
|
+
import type { PromiseOrValue } from "../utils/type-utils.js";
|
|
4
|
+
/**
|
|
5
|
+
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
6
|
+
*
|
|
7
|
+
* This class extends the Agent class and provides a common interface for handling model inputs,
|
|
8
|
+
* outputs, and capabilities. Specific model implementations (like OpenAI, Anthropic, etc.)
|
|
9
|
+
* should inherit from this class and implement their specific functionalities.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* Here's how to implement a custom ChatModel:
|
|
13
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Here's an example showing streaming response with readable stream:
|
|
17
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming}
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* Here's an example showing streaming response with async generator:
|
|
21
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming-async-generator}
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* Here's an example with tool calls:
|
|
25
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
26
|
+
*/
|
|
3
27
|
export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
|
|
4
28
|
constructor();
|
|
29
|
+
/**
|
|
30
|
+
* Indicates whether the model supports parallel tool calls
|
|
31
|
+
*
|
|
32
|
+
* Defaults to true, subclasses can override this property based on
|
|
33
|
+
* specific model capabilities
|
|
34
|
+
*/
|
|
5
35
|
protected supportsParallelToolCalls: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the model's supported capabilities
|
|
38
|
+
*
|
|
39
|
+
* Currently returns capabilities including: whether parallel tool calls are supported
|
|
40
|
+
*
|
|
41
|
+
* @returns An object containing model capabilities
|
|
42
|
+
*/
|
|
6
43
|
getModelCapabilities(): {
|
|
7
44
|
supportsParallelToolCalls: boolean;
|
|
8
45
|
};
|
|
9
46
|
private validateToolNames;
|
|
47
|
+
/**
|
|
48
|
+
* Performs preprocessing operations before handling input
|
|
49
|
+
*
|
|
50
|
+
* Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
|
|
51
|
+
*
|
|
52
|
+
* @param input Input message
|
|
53
|
+
* @param context Execution context
|
|
54
|
+
* @throws Error if token usage exceeds maximum limit
|
|
55
|
+
*/
|
|
10
56
|
protected preprocess(input: ChatModelInput, context: Context): void;
|
|
57
|
+
/**
|
|
58
|
+
* Performs postprocessing operations after handling output
|
|
59
|
+
*
|
|
60
|
+
* Primarily updates token usage statistics in the context
|
|
61
|
+
*
|
|
62
|
+
* @param input Input message
|
|
63
|
+
* @param output Output message
|
|
64
|
+
* @param context Execution context
|
|
65
|
+
*/
|
|
11
66
|
protected postprocess(input: ChatModelInput, output: ChatModelOutput, context: Context): void;
|
|
67
|
+
/**
|
|
68
|
+
* Processes input messages and generates model responses
|
|
69
|
+
*
|
|
70
|
+
* This is the core method that must be implemented by all ChatModel subclasses.
|
|
71
|
+
* It handles the communication with the underlying language model,
|
|
72
|
+
* processes the input messages, and generates appropriate responses.
|
|
73
|
+
*
|
|
74
|
+
* Implementations should handle:
|
|
75
|
+
* - Conversion of input format to model-specific format
|
|
76
|
+
* - Sending requests to the language model
|
|
77
|
+
* - Processing model responses
|
|
78
|
+
* - Handling streaming responses if supported
|
|
79
|
+
* - Proper error handling and retries
|
|
80
|
+
* - Token counting and usage tracking
|
|
81
|
+
* - Tool call processing if applicable
|
|
82
|
+
*
|
|
83
|
+
* @param input - The standardized input containing messages and model options
|
|
84
|
+
* @param context - The execution context with settings and state
|
|
85
|
+
* @returns A promise or direct value containing the model's response
|
|
86
|
+
*/
|
|
87
|
+
abstract process(input: ChatModelInput, context: Context): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
12
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Input message format for ChatModel
|
|
91
|
+
*
|
|
92
|
+
* Contains an array of messages to send to the model, response format settings,
|
|
93
|
+
* tool definitions, and model-specific options
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* Here's a basic ChatModel input example:
|
|
97
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* Here's an example with tool calling:
|
|
101
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
102
|
+
*/
|
|
13
103
|
export interface ChatModelInput extends Message {
|
|
104
|
+
/**
|
|
105
|
+
* Array of messages to send to the model
|
|
106
|
+
*/
|
|
14
107
|
messages: ChatModelInputMessage[];
|
|
108
|
+
/**
|
|
109
|
+
* Specifies the expected response format
|
|
110
|
+
*/
|
|
15
111
|
responseFormat?: ChatModelInputResponseFormat;
|
|
112
|
+
/**
|
|
113
|
+
* List of tools available for the model to use
|
|
114
|
+
*/
|
|
16
115
|
tools?: ChatModelInputTool[];
|
|
116
|
+
/**
|
|
117
|
+
* Specifies the tool selection strategy
|
|
118
|
+
*/
|
|
17
119
|
toolChoice?: ChatModelInputToolChoice;
|
|
120
|
+
/**
|
|
121
|
+
* Model-specific configuration options
|
|
122
|
+
*/
|
|
18
123
|
modelOptions?: ChatModelOptions;
|
|
19
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Message role types
|
|
127
|
+
*
|
|
128
|
+
* - system: System instructions
|
|
129
|
+
* - user: User messages
|
|
130
|
+
* - agent: Agent/assistant messages
|
|
131
|
+
* - tool: Tool call responses
|
|
132
|
+
*/
|
|
20
133
|
export type Role = "system" | "user" | "agent" | "tool";
|
|
134
|
+
/**
|
|
135
|
+
* Structure of input messages
|
|
136
|
+
*
|
|
137
|
+
* Defines the format of each message sent to the model, including
|
|
138
|
+
* role, content, and tool call related information
|
|
139
|
+
*/
|
|
21
140
|
export interface ChatModelInputMessage {
|
|
141
|
+
/**
|
|
142
|
+
* Role of the message (system, user, agent, or tool)
|
|
143
|
+
*/
|
|
22
144
|
role: Role;
|
|
145
|
+
/**
|
|
146
|
+
* Message content, can be text or multimodal content array
|
|
147
|
+
*/
|
|
23
148
|
content?: ChatModelInputMessageContent;
|
|
149
|
+
/**
|
|
150
|
+
* Tool call details when the agent wants to execute tool calls
|
|
151
|
+
*/
|
|
24
152
|
toolCalls?: {
|
|
25
153
|
id: string;
|
|
26
154
|
type: "function";
|
|
@@ -29,18 +157,44 @@ export interface ChatModelInputMessage {
|
|
|
29
157
|
arguments: Message;
|
|
30
158
|
};
|
|
31
159
|
}[];
|
|
160
|
+
/**
|
|
161
|
+
* For tool response messages, specifies the corresponding tool call ID
|
|
162
|
+
*/
|
|
32
163
|
toolCallId?: string;
|
|
164
|
+
/**
|
|
165
|
+
* Name of the message sender (for multi-agent scenarios)
|
|
166
|
+
*/
|
|
33
167
|
name?: string;
|
|
34
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Type of input message content
|
|
171
|
+
*
|
|
172
|
+
* Can be a simple string, or a mixed array of text and image content
|
|
173
|
+
*/
|
|
35
174
|
export type ChatModelInputMessageContent = string | (TextContent | ImageUrlContent)[];
|
|
175
|
+
/**
|
|
176
|
+
* Text content type
|
|
177
|
+
*
|
|
178
|
+
* Used for text parts of message content
|
|
179
|
+
*/
|
|
36
180
|
export type TextContent = {
|
|
37
181
|
type: "text";
|
|
38
182
|
text: string;
|
|
39
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* Image URL content type
|
|
186
|
+
*
|
|
187
|
+
* Used for image parts of message content, referencing images via URL
|
|
188
|
+
*/
|
|
40
189
|
export type ImageUrlContent = {
|
|
41
190
|
type: "image_url";
|
|
42
191
|
url: string;
|
|
43
192
|
};
|
|
193
|
+
/**
|
|
194
|
+
* Model response format settings
|
|
195
|
+
*
|
|
196
|
+
* Can be specified as plain text format or according to a JSON Schema
|
|
197
|
+
*/
|
|
44
198
|
export type ChatModelInputResponseFormat = {
|
|
45
199
|
type: "text";
|
|
46
200
|
} | {
|
|
@@ -52,14 +206,51 @@ export type ChatModelInputResponseFormat = {
|
|
|
52
206
|
strict?: boolean;
|
|
53
207
|
};
|
|
54
208
|
};
|
|
209
|
+
/**
|
|
210
|
+
* Tool definition provided to the model
|
|
211
|
+
*
|
|
212
|
+
* Defines a function tool, including name, description and parameter structure
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* Here's an example showing how to use tools:
|
|
216
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
217
|
+
*/
|
|
55
218
|
export interface ChatModelInputTool {
|
|
219
|
+
/**
|
|
220
|
+
* Tool type, currently only "function" is supported
|
|
221
|
+
*/
|
|
56
222
|
type: "function";
|
|
223
|
+
/**
|
|
224
|
+
* Function tool definition
|
|
225
|
+
*/
|
|
57
226
|
function: {
|
|
227
|
+
/**
|
|
228
|
+
* Function name
|
|
229
|
+
*/
|
|
58
230
|
name: string;
|
|
231
|
+
/**
|
|
232
|
+
* Function description
|
|
233
|
+
*/
|
|
59
234
|
description?: string;
|
|
235
|
+
/**
|
|
236
|
+
* Function parameter structure definition
|
|
237
|
+
*/
|
|
60
238
|
parameters: object;
|
|
61
239
|
};
|
|
62
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Tool selection strategy
|
|
243
|
+
*
|
|
244
|
+
* Determines how the model selects and uses tools:
|
|
245
|
+
* - "auto": Automatically decides whether to use tools
|
|
246
|
+
* - "none": Does not use any tools
|
|
247
|
+
* - "required": Must use tools
|
|
248
|
+
* - object: Specifies a particular tool function
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* Here's an example showing how to use tools:
|
|
252
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
253
|
+
*/
|
|
63
254
|
export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
64
255
|
type: "function";
|
|
65
256
|
function: {
|
|
@@ -67,30 +258,116 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
|
67
258
|
description?: string;
|
|
68
259
|
};
|
|
69
260
|
};
|
|
261
|
+
/**
|
|
262
|
+
* Model-specific configuration options
|
|
263
|
+
*
|
|
264
|
+
* Contains various parameters for controlling model behavior, such as model name, temperature, etc.
|
|
265
|
+
*/
|
|
70
266
|
export interface ChatModelOptions {
|
|
267
|
+
/**
|
|
268
|
+
* Model name or version
|
|
269
|
+
*/
|
|
71
270
|
model?: string;
|
|
271
|
+
/**
|
|
272
|
+
* Temperature parameter, controls randomness (0-1)
|
|
273
|
+
*/
|
|
72
274
|
temperature?: number;
|
|
275
|
+
/**
|
|
276
|
+
* Top-p parameter, controls vocabulary diversity
|
|
277
|
+
*/
|
|
73
278
|
topP?: number;
|
|
279
|
+
/**
|
|
280
|
+
* Frequency penalty parameter, reduces repetition
|
|
281
|
+
*/
|
|
74
282
|
frequencyPenalty?: number;
|
|
283
|
+
/**
|
|
284
|
+
* Presence penalty parameter, encourages diversity
|
|
285
|
+
*/
|
|
75
286
|
presencePenalty?: number;
|
|
287
|
+
/**
|
|
288
|
+
* Whether to allow parallel tool calls
|
|
289
|
+
*/
|
|
76
290
|
parallelToolCalls?: boolean;
|
|
77
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Output message format for ChatModel
|
|
294
|
+
*
|
|
295
|
+
* Contains model response content, which can be text, JSON data, tool calls, and usage statistics
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* Here's a basic output example:
|
|
299
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* Here's an example with tool calls:
|
|
303
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
304
|
+
*/
|
|
78
305
|
export interface ChatModelOutput extends Message {
|
|
306
|
+
/**
|
|
307
|
+
* Text format response content
|
|
308
|
+
*/
|
|
79
309
|
text?: string;
|
|
310
|
+
/**
|
|
311
|
+
* JSON format response content
|
|
312
|
+
*/
|
|
80
313
|
json?: object;
|
|
314
|
+
/**
|
|
315
|
+
* List of tools the model requested to call
|
|
316
|
+
*/
|
|
81
317
|
toolCalls?: ChatModelOutputToolCall[];
|
|
318
|
+
/**
|
|
319
|
+
* Token usage statistics
|
|
320
|
+
*/
|
|
82
321
|
usage?: ChatModelOutputUsage;
|
|
322
|
+
/**
|
|
323
|
+
* Model name or version used
|
|
324
|
+
*/
|
|
83
325
|
model?: string;
|
|
84
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Tool call information in model output
|
|
329
|
+
*
|
|
330
|
+
* Describes tool calls requested by the model, including tool ID and call parameters
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* Here's an example with tool calls:
|
|
334
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
335
|
+
*/
|
|
85
336
|
export interface ChatModelOutputToolCall {
|
|
337
|
+
/**
|
|
338
|
+
* Unique ID of the tool call
|
|
339
|
+
*/
|
|
86
340
|
id: string;
|
|
341
|
+
/**
|
|
342
|
+
* Tool type, currently only "function" is supported
|
|
343
|
+
*/
|
|
87
344
|
type: "function";
|
|
345
|
+
/**
|
|
346
|
+
* Function call details
|
|
347
|
+
*/
|
|
88
348
|
function: {
|
|
349
|
+
/**
|
|
350
|
+
* Name of the function being called
|
|
351
|
+
*/
|
|
89
352
|
name: string;
|
|
353
|
+
/**
|
|
354
|
+
* Arguments for the function call
|
|
355
|
+
*/
|
|
90
356
|
arguments: Message;
|
|
91
357
|
};
|
|
92
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Model usage statistics
|
|
361
|
+
*
|
|
362
|
+
* Records the number of input and output tokens for tracking model usage
|
|
363
|
+
*/
|
|
93
364
|
export interface ChatModelOutputUsage {
|
|
365
|
+
/**
|
|
366
|
+
* Number of input tokens
|
|
367
|
+
*/
|
|
94
368
|
inputTokens: number;
|
|
369
|
+
/**
|
|
370
|
+
* Number of output tokens
|
|
371
|
+
*/
|
|
95
372
|
outputTokens: number;
|
|
96
373
|
}
|
|
@@ -3,6 +3,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ChatModel = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const agent_js_1 = require("../agents/agent.js");
|
|
6
|
+
/**
|
|
7
|
+
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
8
|
+
*
|
|
9
|
+
* This class extends the Agent class and provides a common interface for handling model inputs,
|
|
10
|
+
* outputs, and capabilities. Specific model implementations (like OpenAI, Anthropic, etc.)
|
|
11
|
+
* should inherit from this class and implement their specific functionalities.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* Here's how to implement a custom ChatModel:
|
|
15
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Here's an example showing streaming response with readable stream:
|
|
19
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming}
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* Here's an example showing streaming response with async generator:
|
|
23
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming-async-generator}
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* Here's an example with tool calls:
|
|
27
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
28
|
+
*/
|
|
6
29
|
class ChatModel extends agent_js_1.Agent {
|
|
7
30
|
constructor() {
|
|
8
31
|
super({
|
|
@@ -10,7 +33,20 @@ class ChatModel extends agent_js_1.Agent {
|
|
|
10
33
|
outputSchema: chatModelOutputSchema,
|
|
11
34
|
});
|
|
12
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Indicates whether the model supports parallel tool calls
|
|
38
|
+
*
|
|
39
|
+
* Defaults to true, subclasses can override this property based on
|
|
40
|
+
* specific model capabilities
|
|
41
|
+
*/
|
|
13
42
|
supportsParallelToolCalls = true;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the model's supported capabilities
|
|
45
|
+
*
|
|
46
|
+
* Currently returns capabilities including: whether parallel tool calls are supported
|
|
47
|
+
*
|
|
48
|
+
* @returns An object containing model capabilities
|
|
49
|
+
*/
|
|
14
50
|
getModelCapabilities() {
|
|
15
51
|
return {
|
|
16
52
|
supportsParallelToolCalls: this.supportsParallelToolCalls,
|
|
@@ -23,6 +59,15 @@ class ChatModel extends agent_js_1.Agent {
|
|
|
23
59
|
}
|
|
24
60
|
}
|
|
25
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Performs preprocessing operations before handling input
|
|
64
|
+
*
|
|
65
|
+
* Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
|
|
66
|
+
*
|
|
67
|
+
* @param input Input message
|
|
68
|
+
* @param context Execution context
|
|
69
|
+
* @throws Error if token usage exceeds maximum limit
|
|
70
|
+
*/
|
|
26
71
|
preprocess(input, context) {
|
|
27
72
|
super.preprocess(input, context);
|
|
28
73
|
const { limits, usage } = context;
|
|
@@ -32,6 +77,15 @@ class ChatModel extends agent_js_1.Agent {
|
|
|
32
77
|
}
|
|
33
78
|
this.validateToolNames(input.tools);
|
|
34
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Performs postprocessing operations after handling output
|
|
82
|
+
*
|
|
83
|
+
* Primarily updates token usage statistics in the context
|
|
84
|
+
*
|
|
85
|
+
* @param input Input message
|
|
86
|
+
* @param output Output message
|
|
87
|
+
* @param context Execution context
|
|
88
|
+
*/
|
|
35
89
|
postprocess(input, output, context) {
|
|
36
90
|
super.postprocess(input, output, context);
|
|
37
91
|
const { usage } = output;
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import type {
|
|
4
|
-
import type
|
|
3
|
+
import type { AgentProcessResult } from "../agents/agent.js";
|
|
4
|
+
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
5
|
import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for Claude Chat Model
|
|
8
|
+
*/
|
|
6
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
|
+
*/
|
|
7
15
|
apiKey?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Claude model to use
|
|
18
|
+
*
|
|
19
|
+
* Defaults to 'claude-3-7-sonnet-latest'
|
|
20
|
+
*/
|
|
8
21
|
model?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Additional model options to control behavior
|
|
24
|
+
*/
|
|
9
25
|
modelOptions?: ChatModelOptions;
|
|
10
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* @hidden
|
|
29
|
+
*/
|
|
11
30
|
export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
12
31
|
apiKey: z.ZodOptional<z.ZodString>;
|
|
13
32
|
model: z.ZodOptional<z.ZodString>;
|
|
@@ -56,13 +75,40 @@ export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
|
56
75
|
model?: string | undefined;
|
|
57
76
|
apiKey?: string | undefined;
|
|
58
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
|
+
*/
|
|
59
96
|
export declare class ClaudeChatModel extends ChatModel {
|
|
60
97
|
options?: ClaudeChatModelOptions | undefined;
|
|
61
98
|
constructor(options?: ClaudeChatModelOptions | undefined);
|
|
99
|
+
/**
|
|
100
|
+
* @hidden
|
|
101
|
+
*/
|
|
62
102
|
protected _client?: Anthropic;
|
|
63
103
|
get client(): Anthropic;
|
|
64
104
|
get modelOptions(): ChatModelOptions | undefined;
|
|
65
|
-
|
|
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;
|
|
66
112
|
private extractResultFromClaudeStream;
|
|
67
113
|
private requestStructuredOutput;
|
|
68
114
|
}
|