@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,70 +1,228 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
1
|
+
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
2
|
import type { Context } from "../aigne/context.js";
|
|
3
|
+
import { type DefaultMemoryOptions } from "../memory/default-memory.js";
|
|
3
4
|
import { ChatModel } from "../models/chat-model.js";
|
|
4
5
|
import type { ChatModelInput } from "../models/chat-model.js";
|
|
5
6
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
6
7
|
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for an AI Agent
|
|
10
|
+
*
|
|
11
|
+
* These options extend the base agent options with AI-specific parameters
|
|
12
|
+
* like model configuration, prompt instructions, and tool choice.
|
|
13
|
+
*
|
|
14
|
+
* @template I The input message type the agent accepts
|
|
15
|
+
* @template O The output message type the agent returns
|
|
16
|
+
*/
|
|
17
|
+
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends Omit<AgentOptions<I, O>, "memory"> {
|
|
18
|
+
/**
|
|
19
|
+
* The language model to use for this agent
|
|
20
|
+
*
|
|
21
|
+
* If not provided, the agent will use the model from the context
|
|
22
|
+
*/
|
|
8
23
|
model?: ChatModel;
|
|
24
|
+
/**
|
|
25
|
+
* Instructions to guide the AI model's behavior
|
|
26
|
+
*
|
|
27
|
+
* Can be a simple string or a full PromptBuilder instance for
|
|
28
|
+
* more complex prompt templates
|
|
29
|
+
*/
|
|
9
30
|
instructions?: string | PromptBuilder;
|
|
31
|
+
/**
|
|
32
|
+
* Custom key to use for text output in the response
|
|
33
|
+
*
|
|
34
|
+
* Defaults to $message if not specified
|
|
35
|
+
*/
|
|
10
36
|
outputKey?: string;
|
|
11
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Controls how the agent uses tools during execution
|
|
39
|
+
*
|
|
40
|
+
* @default AIAgentToolChoice.auto
|
|
41
|
+
*/
|
|
42
|
+
toolChoice?: AIAgentToolChoice | Agent;
|
|
43
|
+
/**
|
|
44
|
+
* Whether to catch errors from tool execution and continue processing.
|
|
45
|
+
* If set to false, the agent will throw an error if a tool fails.
|
|
46
|
+
*
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
catchToolsError?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Whether to include memory agents as tools for the AI model
|
|
52
|
+
*
|
|
53
|
+
* When set to true, memory agents will be made available as tools
|
|
54
|
+
* that the model can call directly to retrieve or store information.
|
|
55
|
+
* This enables the agent to explicitly interact with its memories.
|
|
56
|
+
*
|
|
57
|
+
* @default false
|
|
58
|
+
*/
|
|
59
|
+
memoryAgentsAsTools?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Custom prompt template for formatting memory content
|
|
62
|
+
*
|
|
63
|
+
* Allows customization of how memories are presented to the AI model.
|
|
64
|
+
* If not provided, the default template from MEMORY_MESSAGE_TEMPLATE will be used.
|
|
65
|
+
*
|
|
66
|
+
* The template receives a {{memories}} variable containing serialized memory content.
|
|
67
|
+
*/
|
|
68
|
+
memoryPromptTemplate?: string;
|
|
69
|
+
memory?: AgentOptions<I, O>["memory"] | DefaultMemoryOptions | true;
|
|
12
70
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
54
|
-
model?: ChatModel | undefined;
|
|
55
|
-
instructions?: string | PromptBuilder | undefined;
|
|
56
|
-
outputKey?: string | undefined;
|
|
57
|
-
enableHistory?: boolean | undefined;
|
|
58
|
-
maxHistoryMessages?: number | undefined;
|
|
59
|
-
disableLogging?: boolean | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Tool choice options for AI agents
|
|
73
|
+
*
|
|
74
|
+
* Controls how the agent decides to use tools during execution
|
|
75
|
+
*/
|
|
76
|
+
export declare enum AIAgentToolChoice {
|
|
77
|
+
/**
|
|
78
|
+
* Let the model decide when to use tools
|
|
79
|
+
*/
|
|
80
|
+
auto = "auto",
|
|
81
|
+
/**
|
|
82
|
+
* Disable tool usage
|
|
83
|
+
*/
|
|
84
|
+
none = "none",
|
|
85
|
+
/**
|
|
86
|
+
* Force tool usage
|
|
87
|
+
*/
|
|
88
|
+
required = "required",
|
|
89
|
+
/**
|
|
90
|
+
* Choose exactly one tool and route directly to it
|
|
91
|
+
*/
|
|
92
|
+
router = "router"
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Zod schema for validating AIAgentToolChoice values
|
|
96
|
+
*
|
|
97
|
+
* Used to ensure that toolChoice receives valid values
|
|
98
|
+
*
|
|
99
|
+
* @hidden
|
|
100
|
+
*/
|
|
101
|
+
export declare const aiAgentToolChoiceSchema: z.ZodUnion<[z.ZodNativeEnum<typeof AIAgentToolChoice>, ZodType<Agent<Message, Message>, z.ZodTypeDef, Agent<Message, Message>>]>;
|
|
102
|
+
/**
|
|
103
|
+
* Zod schema for validating AIAgentOptions
|
|
104
|
+
*
|
|
105
|
+
* Extends the base agent options schema with AI-specific parameters
|
|
106
|
+
*
|
|
107
|
+
* @hidden
|
|
108
|
+
*/
|
|
109
|
+
export declare const aiAgentOptionsSchema: ZodObject<{
|
|
110
|
+
[key in keyof AIAgentOptions]: ZodType<AIAgentOptions[key]>;
|
|
60
111
|
}>;
|
|
112
|
+
/**
|
|
113
|
+
* AI-powered agent that leverages language models
|
|
114
|
+
*
|
|
115
|
+
* AIAgent connects to language models to process inputs and generate responses,
|
|
116
|
+
* with support for streaming, function calling, and tool usage.
|
|
117
|
+
*
|
|
118
|
+
* Key features:
|
|
119
|
+
* - Connect to any language model
|
|
120
|
+
* - Use customizable instructions and prompts
|
|
121
|
+
* - Execute tools/function calls
|
|
122
|
+
* - Support streaming responses
|
|
123
|
+
* - Router mode for specialized agents
|
|
124
|
+
*
|
|
125
|
+
* @template I The input message type the agent accepts
|
|
126
|
+
* @template O The output message type the agent returns
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* Basic AIAgent creation:
|
|
130
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
|
|
131
|
+
*/
|
|
61
132
|
export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
133
|
+
/**
|
|
134
|
+
* Create an AIAgent with the specified options
|
|
135
|
+
*
|
|
136
|
+
* Factory method that provides a convenient way to create new AI agents
|
|
137
|
+
*
|
|
138
|
+
* @param options Configuration options for the AI agent
|
|
139
|
+
* @returns A new AIAgent instance
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* AI agent with custom instructions:
|
|
143
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-instructions}
|
|
144
|
+
*/
|
|
62
145
|
static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
146
|
+
/**
|
|
147
|
+
* Create an AIAgent instance
|
|
148
|
+
*
|
|
149
|
+
* @param options Configuration options for the AI agent
|
|
150
|
+
*/
|
|
63
151
|
constructor(options: AIAgentOptions<I, O>);
|
|
152
|
+
/**
|
|
153
|
+
* The language model used by this agent
|
|
154
|
+
*
|
|
155
|
+
* If not set on the agent, the model from the context will be used
|
|
156
|
+
*/
|
|
64
157
|
model?: ChatModel;
|
|
158
|
+
/**
|
|
159
|
+
* Instructions for the language model
|
|
160
|
+
*
|
|
161
|
+
* Contains system messages, user templates, and other prompt elements
|
|
162
|
+
* that guide the model's behavior
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* Custom prompt builder:
|
|
166
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
167
|
+
*/
|
|
65
168
|
instructions: PromptBuilder;
|
|
169
|
+
/**
|
|
170
|
+
* Custom key to use for text output in the response
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* Setting a custom output key:
|
|
174
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
|
|
175
|
+
*/
|
|
66
176
|
outputKey?: string;
|
|
67
|
-
|
|
177
|
+
/**
|
|
178
|
+
* Controls how the agent uses tools during execution
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* Automatic tool choice:
|
|
182
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-tool-choice-auto}
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* Router tool choice:
|
|
186
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-router}
|
|
187
|
+
*/
|
|
188
|
+
toolChoice?: AIAgentToolChoice | Agent;
|
|
189
|
+
/**
|
|
190
|
+
* Whether to include memory agents as tools for the AI model
|
|
191
|
+
*
|
|
192
|
+
* When set to true, memory agents will be made available as tools
|
|
193
|
+
* that the model can call directly to retrieve or store information.
|
|
194
|
+
* This enables the agent to explicitly interact with its memories.
|
|
195
|
+
*/
|
|
196
|
+
memoryAgentsAsTools?: boolean;
|
|
197
|
+
/**
|
|
198
|
+
* Custom prompt template for formatting memory content
|
|
199
|
+
*
|
|
200
|
+
* Allows customization of how memories are presented to the AI model.
|
|
201
|
+
* If not provided, the default template from MEMORY_MESSAGE_TEMPLATE will be used.
|
|
202
|
+
*
|
|
203
|
+
* The template receives a {{memories}} variable containing serialized memory content.
|
|
204
|
+
*/
|
|
205
|
+
memoryPromptTemplate?: string;
|
|
206
|
+
/**
|
|
207
|
+
* Whether to catch error from tool execution and continue processing.
|
|
208
|
+
* If set to false, the agent will throw an error if a tool fails
|
|
209
|
+
*
|
|
210
|
+
* @default true
|
|
211
|
+
*/
|
|
212
|
+
catchToolsError: boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Process an input message and generate a response
|
|
215
|
+
*
|
|
216
|
+
* @protected
|
|
217
|
+
*/
|
|
68
218
|
process(input: I, context: Context): AgentProcessAsyncGenerator<O>;
|
|
69
|
-
|
|
219
|
+
/**
|
|
220
|
+
* Process router mode requests
|
|
221
|
+
*
|
|
222
|
+
* In router mode, the agent sends a single request to the model to determine
|
|
223
|
+
* which tool to use, then routes the request directly to that tool
|
|
224
|
+
*
|
|
225
|
+
* @protected
|
|
226
|
+
*/
|
|
227
|
+
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, context: Context, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
|
|
70
228
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = void 0;
|
|
3
|
+
exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = exports.AIAgentToolChoice = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
|
+
const default_memory_js_1 = require("../memory/default-memory.js");
|
|
6
|
+
const memory_js_1 = require("../memory/memory.js");
|
|
5
7
|
const chat_model_js_1 = require("../models/chat-model.js");
|
|
6
8
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
7
9
|
const template_js_1 = require("../prompt/template.js");
|
|
@@ -9,36 +11,106 @@ const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
|
9
11
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
10
12
|
const agent_js_1 = require("./agent.js");
|
|
11
13
|
const types_js_1 = require("./types.js");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Tool choice options for AI agents
|
|
16
|
+
*
|
|
17
|
+
* Controls how the agent decides to use tools during execution
|
|
18
|
+
*/
|
|
19
|
+
var AIAgentToolChoice;
|
|
20
|
+
(function (AIAgentToolChoice) {
|
|
21
|
+
/**
|
|
22
|
+
* Let the model decide when to use tools
|
|
23
|
+
*/
|
|
24
|
+
AIAgentToolChoice["auto"] = "auto";
|
|
25
|
+
/**
|
|
26
|
+
* Disable tool usage
|
|
27
|
+
*/
|
|
28
|
+
AIAgentToolChoice["none"] = "none";
|
|
29
|
+
/**
|
|
30
|
+
* Force tool usage
|
|
31
|
+
*/
|
|
32
|
+
AIAgentToolChoice["required"] = "required";
|
|
33
|
+
/**
|
|
34
|
+
* Choose exactly one tool and route directly to it
|
|
35
|
+
*/
|
|
36
|
+
AIAgentToolChoice["router"] = "router";
|
|
37
|
+
})(AIAgentToolChoice || (exports.AIAgentToolChoice = AIAgentToolChoice = {}));
|
|
38
|
+
/**
|
|
39
|
+
* Zod schema for validating AIAgentToolChoice values
|
|
40
|
+
*
|
|
41
|
+
* Used to ensure that toolChoice receives valid values
|
|
42
|
+
*
|
|
43
|
+
* @hidden
|
|
44
|
+
*/
|
|
45
|
+
exports.aiAgentToolChoiceSchema = zod_1.z.union([zod_1.z.nativeEnum(AIAgentToolChoice), zod_1.z.instanceof(agent_js_1.Agent)], {
|
|
46
|
+
message: `aiAgentToolChoice must be ${Object.values(AIAgentToolChoice).join(", ")}, or an Agent`,
|
|
47
|
+
});
|
|
48
|
+
/**
|
|
49
|
+
* Zod schema for validating AIAgentOptions
|
|
50
|
+
*
|
|
51
|
+
* Extends the base agent options schema with AI-specific parameters
|
|
52
|
+
*
|
|
53
|
+
* @hidden
|
|
54
|
+
*/
|
|
55
|
+
exports.aiAgentOptionsSchema = agent_js_1.agentOptionsSchema.extend({
|
|
20
56
|
model: zod_1.z.instanceof(chat_model_js_1.ChatModel).optional(),
|
|
21
57
|
instructions: zod_1.z.union([zod_1.z.string(), zod_1.z.instanceof(prompt_builder_js_1.PromptBuilder)]).optional(),
|
|
22
58
|
outputKey: zod_1.z.string().optional(),
|
|
23
59
|
toolChoice: exports.aiAgentToolChoiceSchema.optional(),
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
includeInputInOutput: zod_1.z.boolean().optional(),
|
|
27
|
-
subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
|
|
28
|
-
publishTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string()), zod_1.z.function()]).optional(),
|
|
29
|
-
name: zod_1.z.string().optional(),
|
|
30
|
-
description: zod_1.z.string().optional(),
|
|
31
|
-
skills: zod_1.z.array(zod_1.z.union([zod_1.z.instanceof(agent_js_1.Agent), zod_1.z.function()])).optional(),
|
|
32
|
-
disableLogging: zod_1.z.boolean().optional(),
|
|
33
|
-
memory: zod_1.z.union([zod_1.z.boolean(), zod_1.z.any(), zod_1.z.any()]).optional(),
|
|
60
|
+
memoryAgentsAsTools: zod_1.z.boolean().optional(),
|
|
61
|
+
memoryPromptTemplate: zod_1.z.string().optional(),
|
|
34
62
|
});
|
|
63
|
+
/**
|
|
64
|
+
* AI-powered agent that leverages language models
|
|
65
|
+
*
|
|
66
|
+
* AIAgent connects to language models to process inputs and generate responses,
|
|
67
|
+
* with support for streaming, function calling, and tool usage.
|
|
68
|
+
*
|
|
69
|
+
* Key features:
|
|
70
|
+
* - Connect to any language model
|
|
71
|
+
* - Use customizable instructions and prompts
|
|
72
|
+
* - Execute tools/function calls
|
|
73
|
+
* - Support streaming responses
|
|
74
|
+
* - Router mode for specialized agents
|
|
75
|
+
*
|
|
76
|
+
* @template I The input message type the agent accepts
|
|
77
|
+
* @template O The output message type the agent returns
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* Basic AIAgent creation:
|
|
81
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
|
|
82
|
+
*/
|
|
35
83
|
class AIAgent extends agent_js_1.Agent {
|
|
84
|
+
/**
|
|
85
|
+
* Create an AIAgent with the specified options
|
|
86
|
+
*
|
|
87
|
+
* Factory method that provides a convenient way to create new AI agents
|
|
88
|
+
*
|
|
89
|
+
* @param options Configuration options for the AI agent
|
|
90
|
+
* @returns A new AIAgent instance
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* AI agent with custom instructions:
|
|
94
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-instructions}
|
|
95
|
+
*/
|
|
36
96
|
static from(options) {
|
|
37
97
|
return new AIAgent(options);
|
|
38
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Create an AIAgent instance
|
|
101
|
+
*
|
|
102
|
+
* @param options Configuration options for the AI agent
|
|
103
|
+
*/
|
|
39
104
|
constructor(options) {
|
|
40
|
-
|
|
41
|
-
|
|
105
|
+
super({
|
|
106
|
+
...options,
|
|
107
|
+
memory: !options.memory
|
|
108
|
+
? undefined
|
|
109
|
+
: Array.isArray(options.memory) || options.memory instanceof memory_js_1.MemoryAgent
|
|
110
|
+
? options.memory
|
|
111
|
+
: new default_memory_js_1.DefaultMemory(options.memory === true ? {} : options.memory),
|
|
112
|
+
});
|
|
113
|
+
(0, type_utils_js_1.checkArguments)("AIAgent", exports.aiAgentOptionsSchema, options);
|
|
42
114
|
this.model = options.model;
|
|
43
115
|
this.instructions =
|
|
44
116
|
typeof options.instructions === "string"
|
|
@@ -46,11 +118,77 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
46
118
|
: (options.instructions ?? new prompt_builder_js_1.PromptBuilder());
|
|
47
119
|
this.outputKey = options.outputKey;
|
|
48
120
|
this.toolChoice = options.toolChoice;
|
|
121
|
+
this.memoryAgentsAsTools = options.memoryAgentsAsTools;
|
|
122
|
+
this.memoryPromptTemplate = options.memoryPromptTemplate;
|
|
123
|
+
if (typeof options.catchToolsError === "boolean")
|
|
124
|
+
this.catchToolsError = options.catchToolsError;
|
|
49
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* The language model used by this agent
|
|
128
|
+
*
|
|
129
|
+
* If not set on the agent, the model from the context will be used
|
|
130
|
+
*/
|
|
50
131
|
model;
|
|
132
|
+
/**
|
|
133
|
+
* Instructions for the language model
|
|
134
|
+
*
|
|
135
|
+
* Contains system messages, user templates, and other prompt elements
|
|
136
|
+
* that guide the model's behavior
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* Custom prompt builder:
|
|
140
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
141
|
+
*/
|
|
51
142
|
instructions;
|
|
143
|
+
/**
|
|
144
|
+
* Custom key to use for text output in the response
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* Setting a custom output key:
|
|
148
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
|
|
149
|
+
*/
|
|
52
150
|
outputKey;
|
|
151
|
+
/**
|
|
152
|
+
* Controls how the agent uses tools during execution
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* Automatic tool choice:
|
|
156
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-tool-choice-auto}
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* Router tool choice:
|
|
160
|
+
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-router}
|
|
161
|
+
*/
|
|
53
162
|
toolChoice;
|
|
163
|
+
/**
|
|
164
|
+
* Whether to include memory agents as tools for the AI model
|
|
165
|
+
*
|
|
166
|
+
* When set to true, memory agents will be made available as tools
|
|
167
|
+
* that the model can call directly to retrieve or store information.
|
|
168
|
+
* This enables the agent to explicitly interact with its memories.
|
|
169
|
+
*/
|
|
170
|
+
memoryAgentsAsTools;
|
|
171
|
+
/**
|
|
172
|
+
* Custom prompt template for formatting memory content
|
|
173
|
+
*
|
|
174
|
+
* Allows customization of how memories are presented to the AI model.
|
|
175
|
+
* If not provided, the default template from MEMORY_MESSAGE_TEMPLATE will be used.
|
|
176
|
+
*
|
|
177
|
+
* The template receives a {{memories}} variable containing serialized memory content.
|
|
178
|
+
*/
|
|
179
|
+
memoryPromptTemplate;
|
|
180
|
+
/**
|
|
181
|
+
* Whether to catch error from tool execution and continue processing.
|
|
182
|
+
* If set to false, the agent will throw an error if a tool fails
|
|
183
|
+
*
|
|
184
|
+
* @default true
|
|
185
|
+
*/
|
|
186
|
+
catchToolsError = true;
|
|
187
|
+
/**
|
|
188
|
+
* Process an input message and generate a response
|
|
189
|
+
*
|
|
190
|
+
* @protected
|
|
191
|
+
*/
|
|
54
192
|
async *process(input, context) {
|
|
55
193
|
const model = this.model ?? context.model;
|
|
56
194
|
if (!model)
|
|
@@ -63,7 +201,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
63
201
|
});
|
|
64
202
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
65
203
|
if (this.toolChoice === "router") {
|
|
66
|
-
yield* this.
|
|
204
|
+
yield* this._processRouter(input, model, modelInput, context, toolsMap);
|
|
67
205
|
return;
|
|
68
206
|
}
|
|
69
207
|
const toolCallMessages = [];
|
|
@@ -88,7 +226,19 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
88
226
|
if (!tool)
|
|
89
227
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
90
228
|
// NOTE: should pass both arguments (model generated) and input (user provided) to the tool
|
|
91
|
-
const output = await context
|
|
229
|
+
const output = await context
|
|
230
|
+
.invoke(tool, { ...input, ...call.function.arguments }, { disableTransfer: true })
|
|
231
|
+
.catch((error) => {
|
|
232
|
+
if (!this.catchToolsError) {
|
|
233
|
+
return Promise.reject(error);
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
isError: true,
|
|
237
|
+
error: {
|
|
238
|
+
message: error.message,
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
});
|
|
92
242
|
// NOTE: Return transfer output immediately
|
|
93
243
|
if ((0, types_js_1.isTransferAgentOutput)(output)) {
|
|
94
244
|
return output;
|
|
@@ -114,7 +264,15 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
114
264
|
return;
|
|
115
265
|
}
|
|
116
266
|
}
|
|
117
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Process router mode requests
|
|
269
|
+
*
|
|
270
|
+
* In router mode, the agent sends a single request to the model to determine
|
|
271
|
+
* which tool to use, then routes the request directly to that tool
|
|
272
|
+
*
|
|
273
|
+
* @protected
|
|
274
|
+
*/
|
|
275
|
+
async *_processRouter(input, model, modelInput, context, toolsMap) {
|
|
118
276
|
const { toolCalls: [call] = [], } = await context.invoke(model, modelInput);
|
|
119
277
|
if (!call) {
|
|
120
278
|
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
@@ -42,20 +42,132 @@ export type SSEServerParameters = {
|
|
|
42
42
|
*/
|
|
43
43
|
shouldReconnect?: (error: Error) => boolean;
|
|
44
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* MCPAgent is a specialized agent for interacting with MCP (Model Context Protocol) servers.
|
|
47
|
+
* It provides the ability to connect to remote MCP servers using different transport methods,
|
|
48
|
+
* and access their tools, prompts, and resources.
|
|
49
|
+
*
|
|
50
|
+
* MCPAgent serves as a bridge between your application and MCP servers, allowing you to:
|
|
51
|
+
* - Connect to MCP servers over HTTP/SSE or stdio
|
|
52
|
+
* - Access server tools as agent skills
|
|
53
|
+
* - Utilize server prompts and resources
|
|
54
|
+
* - Manage server connections with automatic reconnection
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* Here's an example of creating an MCPAgent with SSE transport:
|
|
58
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
|
|
59
|
+
*/
|
|
45
60
|
export declare class MCPAgent extends Agent {
|
|
61
|
+
/**
|
|
62
|
+
* Create an MCPAgent from a connection to an SSE server.
|
|
63
|
+
*
|
|
64
|
+
* This overload establishes a Server-Sent Events connection to an MCP server
|
|
65
|
+
* and automatically discovers its available tools, prompts, and resources.
|
|
66
|
+
*
|
|
67
|
+
* @param options SSE server connection parameters
|
|
68
|
+
* @returns Promise resolving to a new MCPAgent instance
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* Here's an example of creating an MCPAgent with StreamableHTTP transport:
|
|
72
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-streamable-http}
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* Here's an example of creating an MCPAgent with SSE transport:
|
|
76
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* Here's an example of creating an MCPAgent with Stdio transport:
|
|
80
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-stdio}
|
|
81
|
+
*/
|
|
46
82
|
static from(options: MCPServerOptions): Promise<MCPAgent>;
|
|
83
|
+
/**
|
|
84
|
+
* Create an MCPAgent from a pre-configured MCP client.
|
|
85
|
+
*
|
|
86
|
+
* This overload uses an existing MCP client instance and optionally
|
|
87
|
+
* pre-defined prompts and resources.
|
|
88
|
+
*
|
|
89
|
+
* @param options MCPAgent configuration with client instance
|
|
90
|
+
* @returns A new MCPAgent instance
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* Here's an example of creating an MCPAgent with a client instance:
|
|
94
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-direct}
|
|
95
|
+
*/
|
|
47
96
|
static from(options: MCPAgentOptions): MCPAgent;
|
|
48
97
|
private static fromTransport;
|
|
98
|
+
/**
|
|
99
|
+
* Create an MCPAgent instance directly with a configured client.
|
|
100
|
+
*
|
|
101
|
+
* @param options MCPAgent configuration options, including client instance
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* Here's an example of creating an MCPAgent with an existing client:
|
|
105
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-direct}
|
|
106
|
+
*/
|
|
49
107
|
constructor(options: MCPAgentOptions);
|
|
108
|
+
/**
|
|
109
|
+
* The MCP client instance used for communication with the MCP server.
|
|
110
|
+
*
|
|
111
|
+
* This client manages the connection to the MCP server and provides
|
|
112
|
+
* methods for interacting with server-provided functionality.
|
|
113
|
+
*/
|
|
50
114
|
client: Client;
|
|
115
|
+
/**
|
|
116
|
+
* Array of MCP prompts available from the connected server.
|
|
117
|
+
*
|
|
118
|
+
* Prompts can be accessed by index or by name.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* Here's an example of accessing prompts:
|
|
122
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-prompts}
|
|
123
|
+
*/
|
|
51
124
|
readonly prompts: MCPPrompt[] & {
|
|
52
125
|
[key: string]: MCPPrompt;
|
|
53
126
|
};
|
|
127
|
+
/**
|
|
128
|
+
* Array of MCP resources available from the connected server.
|
|
129
|
+
*
|
|
130
|
+
* Resources can be accessed by index or by name.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* Here's an example of accessing resources:
|
|
134
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-resources}
|
|
135
|
+
*/
|
|
54
136
|
readonly resources: MCPResource[] & {
|
|
55
137
|
[key: string]: MCPResource;
|
|
56
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* Check if the agent is invokable.
|
|
141
|
+
*
|
|
142
|
+
* MCPAgent itself is not directly invokable as it acts as a container
|
|
143
|
+
* for tools, prompts, and resources. Always returns false.
|
|
144
|
+
*/
|
|
57
145
|
get isInvokable(): boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Process method required by Agent interface.
|
|
148
|
+
*
|
|
149
|
+
* Since MCPAgent itself is not directly invokable, this method
|
|
150
|
+
* throws an error if called.
|
|
151
|
+
*
|
|
152
|
+
* @param _input Input message (unused)
|
|
153
|
+
* @param _context Execution context (unused)
|
|
154
|
+
* @throws Error This method always throws an error since MCPAgent is not directly invokable
|
|
155
|
+
*/
|
|
58
156
|
process(_input: Message, _context?: Context): Promise<Message>;
|
|
157
|
+
/**
|
|
158
|
+
* Shut down the agent and close the MCP connection.
|
|
159
|
+
*
|
|
160
|
+
* This method cleans up resources and closes the connection
|
|
161
|
+
* to the MCP server.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* Here's an example of shutting down an MCPAgent:
|
|
165
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown}
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* Here's an example of shutting down an MCPAgent by using statement:
|
|
169
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown-by-using}
|
|
170
|
+
*/
|
|
59
171
|
shutdown(): Promise<void>;
|
|
60
172
|
}
|
|
61
173
|
export interface ClientWithReconnectOptions {
|