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