@aigne/core 1.12.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 +28 -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/loader/index.js +2 -0
- 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 +79 -0
- package/lib/cjs/models/bedrock-chat-model.js +303 -0
- package/lib/cjs/models/chat-model.d.ts +279 -1
- package/lib/cjs/models/chat-model.js +62 -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 +15 -0
- package/lib/cjs/models/gemini-chat-model.js +15 -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 +70 -3
- package/lib/cjs/models/openai-chat-model.js +147 -102
- 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 +21 -20
- 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 +2 -0
- package/lib/cjs/utils/type-utils.js +26 -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 +79 -0
- package/lib/dts/models/chat-model.d.ts +279 -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 +15 -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 +70 -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 +2 -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/loader/index.js +2 -0
- 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 +79 -0
- package/lib/esm/models/bedrock-chat-model.js +298 -0
- package/lib/esm/models/chat-model.d.ts +279 -1
- package/lib/esm/models/chat-model.js +62 -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 +15 -0
- package/lib/esm/models/gemini-chat-model.js +15 -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 +70 -3
- package/lib/esm/models/openai-chat-model.js +147 -102
- 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 +22 -21
- 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 +2 -0
- package/lib/esm/utils/type-utils.js +24 -0
- package/package.json +21 -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,25 +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
|
};
|
|
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
|
+
*/
|
|
9
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
|
+
*/
|
|
10
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>>;
|
|
11
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
|
+
*/
|
|
12
103
|
export interface ChatModelInput extends Message {
|
|
104
|
+
/**
|
|
105
|
+
* Array of messages to send to the model
|
|
106
|
+
*/
|
|
13
107
|
messages: ChatModelInputMessage[];
|
|
108
|
+
/**
|
|
109
|
+
* Specifies the expected response format
|
|
110
|
+
*/
|
|
14
111
|
responseFormat?: ChatModelInputResponseFormat;
|
|
112
|
+
/**
|
|
113
|
+
* List of tools available for the model to use
|
|
114
|
+
*/
|
|
15
115
|
tools?: ChatModelInputTool[];
|
|
116
|
+
/**
|
|
117
|
+
* Specifies the tool selection strategy
|
|
118
|
+
*/
|
|
16
119
|
toolChoice?: ChatModelInputToolChoice;
|
|
120
|
+
/**
|
|
121
|
+
* Model-specific configuration options
|
|
122
|
+
*/
|
|
17
123
|
modelOptions?: ChatModelOptions;
|
|
18
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
|
+
*/
|
|
19
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
|
+
*/
|
|
20
140
|
export interface ChatModelInputMessage {
|
|
141
|
+
/**
|
|
142
|
+
* Role of the message (system, user, agent, or tool)
|
|
143
|
+
*/
|
|
21
144
|
role: Role;
|
|
145
|
+
/**
|
|
146
|
+
* Message content, can be text or multimodal content array
|
|
147
|
+
*/
|
|
22
148
|
content?: ChatModelInputMessageContent;
|
|
149
|
+
/**
|
|
150
|
+
* Tool call details when the agent wants to execute tool calls
|
|
151
|
+
*/
|
|
23
152
|
toolCalls?: {
|
|
24
153
|
id: string;
|
|
25
154
|
type: "function";
|
|
@@ -28,18 +157,44 @@ export interface ChatModelInputMessage {
|
|
|
28
157
|
arguments: Message;
|
|
29
158
|
};
|
|
30
159
|
}[];
|
|
160
|
+
/**
|
|
161
|
+
* For tool response messages, specifies the corresponding tool call ID
|
|
162
|
+
*/
|
|
31
163
|
toolCallId?: string;
|
|
164
|
+
/**
|
|
165
|
+
* Name of the message sender (for multi-agent scenarios)
|
|
166
|
+
*/
|
|
32
167
|
name?: string;
|
|
33
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
|
+
*/
|
|
34
174
|
export type ChatModelInputMessageContent = string | (TextContent | ImageUrlContent)[];
|
|
175
|
+
/**
|
|
176
|
+
* Text content type
|
|
177
|
+
*
|
|
178
|
+
* Used for text parts of message content
|
|
179
|
+
*/
|
|
35
180
|
export type TextContent = {
|
|
36
181
|
type: "text";
|
|
37
182
|
text: string;
|
|
38
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* Image URL content type
|
|
186
|
+
*
|
|
187
|
+
* Used for image parts of message content, referencing images via URL
|
|
188
|
+
*/
|
|
39
189
|
export type ImageUrlContent = {
|
|
40
190
|
type: "image_url";
|
|
41
191
|
url: string;
|
|
42
192
|
};
|
|
193
|
+
/**
|
|
194
|
+
* Model response format settings
|
|
195
|
+
*
|
|
196
|
+
* Can be specified as plain text format or according to a JSON Schema
|
|
197
|
+
*/
|
|
43
198
|
export type ChatModelInputResponseFormat = {
|
|
44
199
|
type: "text";
|
|
45
200
|
} | {
|
|
@@ -51,14 +206,51 @@ export type ChatModelInputResponseFormat = {
|
|
|
51
206
|
strict?: boolean;
|
|
52
207
|
};
|
|
53
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
|
+
*/
|
|
54
218
|
export interface ChatModelInputTool {
|
|
219
|
+
/**
|
|
220
|
+
* Tool type, currently only "function" is supported
|
|
221
|
+
*/
|
|
55
222
|
type: "function";
|
|
223
|
+
/**
|
|
224
|
+
* Function tool definition
|
|
225
|
+
*/
|
|
56
226
|
function: {
|
|
227
|
+
/**
|
|
228
|
+
* Function name
|
|
229
|
+
*/
|
|
57
230
|
name: string;
|
|
231
|
+
/**
|
|
232
|
+
* Function description
|
|
233
|
+
*/
|
|
58
234
|
description?: string;
|
|
235
|
+
/**
|
|
236
|
+
* Function parameter structure definition
|
|
237
|
+
*/
|
|
59
238
|
parameters: object;
|
|
60
239
|
};
|
|
61
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
|
+
*/
|
|
62
254
|
export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
63
255
|
type: "function";
|
|
64
256
|
function: {
|
|
@@ -66,30 +258,116 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
|
66
258
|
description?: string;
|
|
67
259
|
};
|
|
68
260
|
};
|
|
261
|
+
/**
|
|
262
|
+
* Model-specific configuration options
|
|
263
|
+
*
|
|
264
|
+
* Contains various parameters for controlling model behavior, such as model name, temperature, etc.
|
|
265
|
+
*/
|
|
69
266
|
export interface ChatModelOptions {
|
|
267
|
+
/**
|
|
268
|
+
* Model name or version
|
|
269
|
+
*/
|
|
70
270
|
model?: string;
|
|
271
|
+
/**
|
|
272
|
+
* Temperature parameter, controls randomness (0-1)
|
|
273
|
+
*/
|
|
71
274
|
temperature?: number;
|
|
275
|
+
/**
|
|
276
|
+
* Top-p parameter, controls vocabulary diversity
|
|
277
|
+
*/
|
|
72
278
|
topP?: number;
|
|
279
|
+
/**
|
|
280
|
+
* Frequency penalty parameter, reduces repetition
|
|
281
|
+
*/
|
|
73
282
|
frequencyPenalty?: number;
|
|
283
|
+
/**
|
|
284
|
+
* Presence penalty parameter, encourages diversity
|
|
285
|
+
*/
|
|
74
286
|
presencePenalty?: number;
|
|
287
|
+
/**
|
|
288
|
+
* Whether to allow parallel tool calls
|
|
289
|
+
*/
|
|
75
290
|
parallelToolCalls?: boolean;
|
|
76
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
|
+
*/
|
|
77
305
|
export interface ChatModelOutput extends Message {
|
|
306
|
+
/**
|
|
307
|
+
* Text format response content
|
|
308
|
+
*/
|
|
78
309
|
text?: string;
|
|
310
|
+
/**
|
|
311
|
+
* JSON format response content
|
|
312
|
+
*/
|
|
79
313
|
json?: object;
|
|
314
|
+
/**
|
|
315
|
+
* List of tools the model requested to call
|
|
316
|
+
*/
|
|
80
317
|
toolCalls?: ChatModelOutputToolCall[];
|
|
318
|
+
/**
|
|
319
|
+
* Token usage statistics
|
|
320
|
+
*/
|
|
81
321
|
usage?: ChatModelOutputUsage;
|
|
322
|
+
/**
|
|
323
|
+
* Model name or version used
|
|
324
|
+
*/
|
|
82
325
|
model?: string;
|
|
83
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
|
+
*/
|
|
84
336
|
export interface ChatModelOutputToolCall {
|
|
337
|
+
/**
|
|
338
|
+
* Unique ID of the tool call
|
|
339
|
+
*/
|
|
85
340
|
id: string;
|
|
341
|
+
/**
|
|
342
|
+
* Tool type, currently only "function" is supported
|
|
343
|
+
*/
|
|
86
344
|
type: "function";
|
|
345
|
+
/**
|
|
346
|
+
* Function call details
|
|
347
|
+
*/
|
|
87
348
|
function: {
|
|
349
|
+
/**
|
|
350
|
+
* Name of the function being called
|
|
351
|
+
*/
|
|
88
352
|
name: string;
|
|
353
|
+
/**
|
|
354
|
+
* Arguments for the function call
|
|
355
|
+
*/
|
|
89
356
|
arguments: Message;
|
|
90
357
|
};
|
|
91
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Model usage statistics
|
|
361
|
+
*
|
|
362
|
+
* Records the number of input and output tokens for tracking model usage
|
|
363
|
+
*/
|
|
92
364
|
export interface ChatModelOutputUsage {
|
|
365
|
+
/**
|
|
366
|
+
* Number of input tokens
|
|
367
|
+
*/
|
|
93
368
|
inputTokens: number;
|
|
369
|
+
/**
|
|
370
|
+
* Number of output tokens
|
|
371
|
+
*/
|
|
94
372
|
outputTokens: number;
|
|
95
373
|
}
|
|
@@ -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
|
}
|
|
@@ -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,8 +1,23 @@
|
|
|
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;
|
|
5
19
|
protected supportsEndWithSystemMessage: boolean;
|
|
6
20
|
protected supportsToolsUseWithJsonSchema: boolean;
|
|
7
21
|
protected supportsParallelToolCalls: boolean;
|
|
22
|
+
protected supportsToolStreaming: boolean;
|
|
8
23
|
}
|
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of the ChatModel interface for Ollama
|
|
4
|
+
*
|
|
5
|
+
* This model allows you to run open-source LLMs locally using Ollama,
|
|
6
|
+
* with an OpenAI-compatible API interface.
|
|
7
|
+
*
|
|
8
|
+
* Default model: 'llama3.2'
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to create and use an Ollama chat model:
|
|
12
|
+
* {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example with streaming response:
|
|
16
|
+
* {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model-streaming}
|
|
17
|
+
*/
|
|
2
18
|
export declare class OllamaChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of the ChatModel interface for OpenRouter service
|
|
4
|
+
*
|
|
5
|
+
* OpenRouter provides access to a variety of large language models through a unified API.
|
|
6
|
+
* This implementation uses the OpenAI-compatible interface to connect to OpenRouter's service.
|
|
7
|
+
*
|
|
8
|
+
* Default model: 'openai/gpt-4o'
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to create and use an OpenRouter chat model:
|
|
12
|
+
* {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example with streaming response:
|
|
16
|
+
* {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model-streaming}
|
|
17
|
+
*/
|
|
2
18
|
export declare class OpenRouterChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import type {
|
|
5
|
-
import type
|
|
4
|
+
import type { AgentProcessResult } from "../agents/agent.js";
|
|
5
|
+
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
6
6
|
import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
|
|
7
7
|
export interface OpenAIChatModelCapabilities {
|
|
8
8
|
supportsNativeStructuredOutputs: boolean;
|
|
@@ -10,14 +10,39 @@ export interface OpenAIChatModelCapabilities {
|
|
|
10
10
|
supportsToolsUseWithJsonSchema: boolean;
|
|
11
11
|
supportsParallelToolCalls: boolean;
|
|
12
12
|
supportsToolsEmptyParameters: boolean;
|
|
13
|
+
supportsToolStreaming: boolean;
|
|
13
14
|
supportsTemperature: boolean;
|
|
14
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Configuration options for OpenAI Chat Model
|
|
18
|
+
*/
|
|
15
19
|
export interface OpenAIChatModelOptions {
|
|
20
|
+
/**
|
|
21
|
+
* API key for OpenAI API
|
|
22
|
+
*
|
|
23
|
+
* If not provided, will look for OPENAI_API_KEY in environment variables
|
|
24
|
+
*/
|
|
16
25
|
apiKey?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Base URL for OpenAI API
|
|
28
|
+
*
|
|
29
|
+
* Useful for proxies or alternate endpoints
|
|
30
|
+
*/
|
|
17
31
|
baseURL?: string;
|
|
32
|
+
/**
|
|
33
|
+
* OpenAI model to use
|
|
34
|
+
*
|
|
35
|
+
* Defaults to 'gpt-4o-mini'
|
|
36
|
+
*/
|
|
18
37
|
model?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Additional model options to control behavior
|
|
40
|
+
*/
|
|
19
41
|
modelOptions?: ChatModelOptions;
|
|
20
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* @hidden
|
|
45
|
+
*/
|
|
21
46
|
export declare const openAIChatModelOptionsSchema: z.ZodObject<{
|
|
22
47
|
apiKey: z.ZodOptional<z.ZodString>;
|
|
23
48
|
baseURL: z.ZodOptional<z.ZodString>;
|
|
@@ -69,9 +94,31 @@ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
|
|
|
69
94
|
apiKey?: string | undefined;
|
|
70
95
|
baseURL?: string | undefined;
|
|
71
96
|
}>;
|
|
97
|
+
/**
|
|
98
|
+
* Implementation of the ChatModel interface for OpenAI's API
|
|
99
|
+
*
|
|
100
|
+
* This model provides access to OpenAI's capabilities including:
|
|
101
|
+
* - Text generation
|
|
102
|
+
* - Tool use with parallel tool calls
|
|
103
|
+
* - JSON structured output
|
|
104
|
+
* - Image understanding
|
|
105
|
+
*
|
|
106
|
+
* Default model: 'gpt-4o-mini'
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* Here's how to create and use an OpenAI chat model:
|
|
110
|
+
* {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model}
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* Here's an example with streaming response:
|
|
114
|
+
* {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model-streaming}
|
|
115
|
+
*/
|
|
72
116
|
export declare class OpenAIChatModel extends ChatModel {
|
|
73
117
|
options?: OpenAIChatModelOptions | undefined;
|
|
74
118
|
constructor(options?: OpenAIChatModelOptions | undefined);
|
|
119
|
+
/**
|
|
120
|
+
* @hidden
|
|
121
|
+
*/
|
|
75
122
|
protected _client?: OpenAI;
|
|
76
123
|
protected apiKeyEnvName: string;
|
|
77
124
|
protected apiKeyDefault: string | undefined;
|
|
@@ -80,20 +127,40 @@ export declare class OpenAIChatModel extends ChatModel {
|
|
|
80
127
|
protected supportsToolsUseWithJsonSchema: boolean;
|
|
81
128
|
protected supportsParallelToolCalls: boolean;
|
|
82
129
|
protected supportsToolsEmptyParameters: boolean;
|
|
130
|
+
protected supportsToolStreaming: boolean;
|
|
83
131
|
protected supportsTemperature: boolean;
|
|
84
132
|
get client(): OpenAI;
|
|
85
133
|
get modelOptions(): ChatModelOptions | undefined;
|
|
86
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Process the input and generate a response
|
|
136
|
+
* @param input The input to process
|
|
137
|
+
* @returns The generated response
|
|
138
|
+
*/
|
|
139
|
+
process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
140
|
+
private _process;
|
|
87
141
|
private getParallelToolCalls;
|
|
88
142
|
private getRunMessages;
|
|
89
143
|
private getRunResponseFormat;
|
|
90
144
|
private requestStructuredOutput;
|
|
145
|
+
private extractResultFromStream;
|
|
91
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* @hidden
|
|
149
|
+
*/
|
|
92
150
|
export declare const ROLE_MAP: {
|
|
93
151
|
[key in Role]: ChatCompletionMessageParam["role"];
|
|
94
152
|
};
|
|
153
|
+
/**
|
|
154
|
+
* @hidden
|
|
155
|
+
*/
|
|
95
156
|
export declare function contentsFromInputMessages(messages: ChatModelInputMessage[]): Promise<ChatCompletionMessageParam[]>;
|
|
157
|
+
/**
|
|
158
|
+
* @hidden
|
|
159
|
+
*/
|
|
96
160
|
export declare function toolsFromInputTools(tools?: ChatModelInputTool[], options?: {
|
|
97
161
|
addTypeToEmptyParameters?: boolean;
|
|
98
162
|
}): ChatCompletionTool[] | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* @hidden
|
|
165
|
+
*/
|
|
99
166
|
export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of the ChatModel interface for X.AI's API (Grok)
|
|
4
|
+
*
|
|
5
|
+
* This model uses OpenAI-compatible API format to interact with X.AI models,
|
|
6
|
+
* providing access to models like Grok.
|
|
7
|
+
*
|
|
8
|
+
* Default model: 'grok-2-latest'
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Here's how to create and use an X.AI chat model:
|
|
12
|
+
* {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Here's an example with streaming response:
|
|
16
|
+
* {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model-streaming}
|
|
17
|
+
*/
|
|
2
18
|
export declare class XAIChatModel extends OpenAIChatModel {
|
|
3
19
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
20
|
protected apiKeyEnvName: string;
|