@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
|
@@ -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
|
}
|
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { Agent } from "../agents/agent.js";
|
|
3
|
+
/**
|
|
4
|
+
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
5
|
+
*
|
|
6
|
+
* This class extends the Agent class and provides a common interface for handling model inputs,
|
|
7
|
+
* outputs, and capabilities. Specific model implementations (like OpenAI, Anthropic, etc.)
|
|
8
|
+
* should inherit from this class and implement their specific functionalities.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to implement a custom ChatModel:
|
|
12
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example showing streaming response with readable stream:
|
|
16
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming}
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* Here's an example showing streaming response with async generator:
|
|
20
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming-async-generator}
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* Here's an example with tool calls:
|
|
24
|
+
* {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
|
|
25
|
+
*/
|
|
3
26
|
export class ChatModel extends Agent {
|
|
4
27
|
constructor() {
|
|
5
28
|
super({
|
|
@@ -7,7 +30,20 @@ export class ChatModel extends Agent {
|
|
|
7
30
|
outputSchema: chatModelOutputSchema,
|
|
8
31
|
});
|
|
9
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Indicates whether the model supports parallel tool calls
|
|
35
|
+
*
|
|
36
|
+
* Defaults to true, subclasses can override this property based on
|
|
37
|
+
* specific model capabilities
|
|
38
|
+
*/
|
|
10
39
|
supportsParallelToolCalls = true;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the model's supported capabilities
|
|
42
|
+
*
|
|
43
|
+
* Currently returns capabilities including: whether parallel tool calls are supported
|
|
44
|
+
*
|
|
45
|
+
* @returns An object containing model capabilities
|
|
46
|
+
*/
|
|
11
47
|
getModelCapabilities() {
|
|
12
48
|
return {
|
|
13
49
|
supportsParallelToolCalls: this.supportsParallelToolCalls,
|
|
@@ -20,6 +56,15 @@ export class ChatModel extends Agent {
|
|
|
20
56
|
}
|
|
21
57
|
}
|
|
22
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Performs preprocessing operations before handling input
|
|
61
|
+
*
|
|
62
|
+
* Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
|
|
63
|
+
*
|
|
64
|
+
* @param input Input message
|
|
65
|
+
* @param context Execution context
|
|
66
|
+
* @throws Error if token usage exceeds maximum limit
|
|
67
|
+
*/
|
|
23
68
|
preprocess(input, context) {
|
|
24
69
|
super.preprocess(input, context);
|
|
25
70
|
const { limits, usage } = context;
|
|
@@ -29,6 +74,15 @@ export class ChatModel extends Agent {
|
|
|
29
74
|
}
|
|
30
75
|
this.validateToolNames(input.tools);
|
|
31
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Performs postprocessing operations after handling output
|
|
79
|
+
*
|
|
80
|
+
* Primarily updates token usage statistics in the context
|
|
81
|
+
*
|
|
82
|
+
* @param input Input message
|
|
83
|
+
* @param output Output message
|
|
84
|
+
* @param context Execution context
|
|
85
|
+
*/
|
|
32
86
|
postprocess(input, output, context) {
|
|
33
87
|
super.postprocess(input, output, context);
|
|
34
88
|
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
|
}
|
|
@@ -3,9 +3,12 @@ import { z } from "zod";
|
|
|
3
3
|
import { parseJSON } from "../utils/json-schema.js";
|
|
4
4
|
import { mergeUsage } from "../utils/model-utils.js";
|
|
5
5
|
import { agentResponseStreamToObject } from "../utils/stream-utils.js";
|
|
6
|
-
import { checkArguments, isEmpty, isNonNullable } from "../utils/type-utils.js";
|
|
6
|
+
import { checkArguments, isEmpty, isNonNullable, } from "../utils/type-utils.js";
|
|
7
7
|
import { ChatModel, } from "./chat-model.js";
|
|
8
8
|
const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
|
|
9
|
+
/**
|
|
10
|
+
* @hidden
|
|
11
|
+
*/
|
|
9
12
|
export const claudeChatModelOptionsSchema = z.object({
|
|
10
13
|
apiKey: z.string().optional(),
|
|
11
14
|
model: z.string().optional(),
|
|
@@ -20,6 +23,24 @@ export const claudeChatModelOptionsSchema = z.object({
|
|
|
20
23
|
})
|
|
21
24
|
.optional(),
|
|
22
25
|
});
|
|
26
|
+
/**
|
|
27
|
+
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
28
|
+
*
|
|
29
|
+
* This model provides access to Claude's capabilities including:
|
|
30
|
+
* - Text generation
|
|
31
|
+
* - Tool use
|
|
32
|
+
* - JSON structured output
|
|
33
|
+
*
|
|
34
|
+
* Default model: 'claude-3-7-sonnet-latest'
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* Here's how to create and use a Claude chat model:
|
|
38
|
+
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model}
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Here's an example with streaming response:
|
|
42
|
+
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model-streaming-async-generator}
|
|
43
|
+
*/
|
|
23
44
|
export class ClaudeChatModel extends ChatModel {
|
|
24
45
|
options;
|
|
25
46
|
constructor(options) {
|
|
@@ -28,6 +49,9 @@ export class ClaudeChatModel extends ChatModel {
|
|
|
28
49
|
super();
|
|
29
50
|
this.options = options;
|
|
30
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* @hidden
|
|
54
|
+
*/
|
|
31
55
|
_client;
|
|
32
56
|
get client() {
|
|
33
57
|
const apiKey = this.options?.apiKey || process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY;
|
|
@@ -39,7 +63,15 @@ export class ClaudeChatModel extends ChatModel {
|
|
|
39
63
|
get modelOptions() {
|
|
40
64
|
return this.options?.modelOptions;
|
|
41
65
|
}
|
|
42
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Process the input using Claude's chat model
|
|
68
|
+
* @param input - The input to process
|
|
69
|
+
* @returns The processed output from the model
|
|
70
|
+
*/
|
|
71
|
+
process(input) {
|
|
72
|
+
return this._process(input);
|
|
73
|
+
}
|
|
74
|
+
async _process(input) {
|
|
43
75
|
const model = this.options?.model || CHAT_MODEL_CLAUDE_DEFAULT_MODEL;
|
|
44
76
|
const disableParallelToolUse = input.modelOptions?.parallelToolCalls === false ||
|
|
45
77
|
this.modelOptions?.parallelToolCalls === false;
|
|
@@ -56,7 +88,7 @@ export class ClaudeChatModel extends ChatModel {
|
|
|
56
88
|
...body,
|
|
57
89
|
stream: true,
|
|
58
90
|
});
|
|
59
|
-
if (
|
|
91
|
+
if (input.responseFormat?.type !== "json_schema") {
|
|
60
92
|
return this.extractResultFromClaudeStream(stream, true);
|
|
61
93
|
}
|
|
62
94
|
const result = await this.extractResultFromClaudeStream(stream);
|
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of the ChatModel interface for DeepSeek's API
|
|
4
|
+
*
|
|
5
|
+
* This model uses OpenAI-compatible API format to interact with DeepSeek's models,
|
|
6
|
+
* but with specific configuration and capabilities for DeepSeek.
|
|
7
|
+
*
|
|
8
|
+
* Default model: 'deepseek-chat'
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to create and use a DeepSeek chat model:
|
|
12
|
+
* {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example with streaming response:
|
|
16
|
+
* {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model-streaming}
|
|
17
|
+
*/
|
|
2
18
|
export declare class DeepSeekChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { OpenAIChatModel } from "./openai-chat-model.js";
|
|
2
2
|
const DEEPSEEK_DEFAULT_CHAT_MODEL = "deepseek-chat";
|
|
3
3
|
const DEEPSEEK_BASE_URL = "https://api.deepseek.com";
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of the ChatModel interface for DeepSeek's API
|
|
6
|
+
*
|
|
7
|
+
* This model uses OpenAI-compatible API format to interact with DeepSeek's models,
|
|
8
|
+
* but with specific configuration and capabilities for DeepSeek.
|
|
9
|
+
*
|
|
10
|
+
* Default model: 'deepseek-chat'
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Here's how to create and use a DeepSeek chat model:
|
|
14
|
+
* {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model}
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Here's an example with streaming response:
|
|
18
|
+
* {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model-streaming}
|
|
19
|
+
*/
|
|
4
20
|
export class DeepSeekChatModel extends OpenAIChatModel {
|
|
5
21
|
constructor(options) {
|
|
6
22
|
super({
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of the ChatModel interface for Google's Gemini API
|
|
4
|
+
*
|
|
5
|
+
* This model uses OpenAI-compatible API format to interact with Google's Gemini models,
|
|
6
|
+
* providing access to models like Gemini 1.5 and Gemini 2.0.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* Here's how to create and use a Gemini chat model:
|
|
10
|
+
* {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Here's an example with streaming response:
|
|
14
|
+
* {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model-streaming}
|
|
15
|
+
*/
|
|
2
16
|
export declare class GeminiChatModel extends OpenAIChatModel {
|
|
3
17
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
18
|
protected apiKeyEnvName: string;
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { OpenAIChatModel } from "./openai-chat-model.js";
|
|
2
2
|
const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai";
|
|
3
3
|
const GEMINI_DEFAULT_CHAT_MODEL = "gemini-2.0-flash";
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of the ChatModel interface for Google's Gemini API
|
|
6
|
+
*
|
|
7
|
+
* This model uses OpenAI-compatible API format to interact with Google's Gemini models,
|
|
8
|
+
* providing access to models like Gemini 1.5 and Gemini 2.0.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to create and use a Gemini chat model:
|
|
12
|
+
* {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example with streaming response:
|
|
16
|
+
* {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model-streaming}
|
|
17
|
+
*/
|
|
4
18
|
export class GeminiChatModel extends OpenAIChatModel {
|
|
5
19
|
constructor(options) {
|
|
6
20
|
super({
|