@auipage/agent 0.1.0-alpha.4 → 0.1.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/AUTHORS.txt +6 -6
- package/CHANGELOG +4 -4
- package/README.md +154 -154
- package/dist/index.js +616 -534
- package/package.json +3 -3
- package/src/callFuns.js +34 -34
- package/src/createTool.js +59 -59
- package/src/index.js +20 -20
- package/src/runConversation.js +55 -55
- package/types/agent.d.ts +47 -47
- package/types/create-tool.d.ts +38 -38
- package/types/index.d.ts +20 -20
- package/types/tool.d.ts +15 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auipage/agent",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "@auipage/agent",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/handless-ui/auipage#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@auipage/model": "^0.1.0
|
|
34
|
-
"@auipage/types": "^0.1.0
|
|
33
|
+
"@auipage/model": "^0.1.0",
|
|
34
|
+
"@auipage/types": "^0.1.0",
|
|
35
35
|
"oipage": "^2.1.0"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/src/callFuns.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 根据模型返回的 tool_calls,依次在 _this.tools 中查找匹配的工具并执行,
|
|
3
|
-
* 返回与 tool_calls 等长的结果数组(同步值/异步值都会被规范化)。
|
|
4
|
-
*
|
|
5
|
-
* @param {Object} _this Agent 实例
|
|
6
|
-
* @param {Array} tool_calls 模型返回的工具调用列表
|
|
7
|
-
* @returns {Promise<Array>} 每个工具的执行结果,未匹配到的槽位会被填入错误说明字符串
|
|
8
|
-
*/
|
|
9
|
-
export default function callFuns(_this, tool_calls) {
|
|
10
|
-
|
|
11
|
-
const tasks = tool_calls.map(call => {
|
|
12
|
-
const tool = _this.tools.find(
|
|
13
|
-
t => t.description.function.name === call.function.name
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
if (!tool) {
|
|
17
|
-
return Promise.resolve("工具" + call.function.name + "不存在");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let result;
|
|
21
|
-
try {
|
|
22
|
-
result = tool.execute(JSON.parse(call.function.arguments));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
return Promise.resolve("参数解析失败:" + e);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (result instanceof Promise) {
|
|
28
|
-
// 工具内部报错也不要阻断对话,转成字符串继续往下走
|
|
29
|
-
return result.catch(e => "运行出错:" + e);
|
|
30
|
-
}
|
|
31
|
-
return Promise.resolve(result);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return Promise.all(tasks);
|
|
1
|
+
/**
|
|
2
|
+
* 根据模型返回的 tool_calls,依次在 _this.tools 中查找匹配的工具并执行,
|
|
3
|
+
* 返回与 tool_calls 等长的结果数组(同步值/异步值都会被规范化)。
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} _this Agent 实例
|
|
6
|
+
* @param {Array} tool_calls 模型返回的工具调用列表
|
|
7
|
+
* @returns {Promise<Array>} 每个工具的执行结果,未匹配到的槽位会被填入错误说明字符串
|
|
8
|
+
*/
|
|
9
|
+
export default function callFuns(_this, tool_calls) {
|
|
10
|
+
|
|
11
|
+
const tasks = tool_calls.map(call => {
|
|
12
|
+
const tool = _this.tools.find(
|
|
13
|
+
t => t.description.function.name === call.function.name
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
if (!tool) {
|
|
17
|
+
return Promise.resolve("工具" + call.function.name + "不存在");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let result;
|
|
21
|
+
try {
|
|
22
|
+
result = tool.execute(JSON.parse(call.function.arguments));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return Promise.resolve("参数解析失败:" + e);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (result instanceof Promise) {
|
|
28
|
+
// 工具内部报错也不要阻断对话,转成字符串继续往下走
|
|
29
|
+
return result.catch(e => "运行出错:" + e);
|
|
30
|
+
}
|
|
31
|
+
return Promise.resolve(result);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return Promise.all(tasks);
|
|
35
35
|
};
|
package/src/createTool.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { initOption } from "oipage/web/option/index.js";
|
|
2
|
-
|
|
3
|
-
function CreateTool(ms) {
|
|
4
|
-
this.ms = ms;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
CreateTool.prototype.create = function ({ description, execute }) {
|
|
8
|
-
let _this = this;
|
|
9
|
-
return {
|
|
10
|
-
description,
|
|
11
|
-
execute: async function (args) {
|
|
12
|
-
let toolInfo = {
|
|
13
|
-
type: "tool-call",
|
|
14
|
-
status: "pending",
|
|
15
|
-
id: Date.now().toString()+"_"+Math.random().toString(36).substring(2).toUpperCase(),
|
|
16
|
-
name: description.function.name,
|
|
17
|
-
caller: execute ? "backend" : "frontend",
|
|
18
|
-
args
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// 后端工具调用
|
|
22
|
-
if (execute) {
|
|
23
|
-
_this.ms.send(toolInfo); // 开始调用工具
|
|
24
|
-
|
|
25
|
-
let result, status;
|
|
26
|
-
try {
|
|
27
|
-
result = await execute(args);
|
|
28
|
-
status = "success"; // 调用成功
|
|
29
|
-
} catch (error) {
|
|
30
|
-
result = error;
|
|
31
|
-
status = "error"; // 调用失败
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
_this.ms.send(initOption({ // 结束调用工具
|
|
35
|
-
status,
|
|
36
|
-
result
|
|
37
|
-
}, toolInfo));
|
|
38
|
-
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 前端工具调用
|
|
43
|
-
else {
|
|
44
|
-
let _resolve = null;
|
|
45
|
-
globalThis[toolInfo.id] = function (result) {
|
|
46
|
-
_resolve(result);
|
|
47
|
-
delete globalThis[toolInfo.id];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
_this.ms.send(toolInfo); // 开始调用工具
|
|
51
|
-
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
_resolve = resolve;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
1
|
+
import { initOption } from "oipage/web/option/index.js";
|
|
2
|
+
|
|
3
|
+
function CreateTool(ms) {
|
|
4
|
+
this.ms = ms;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
CreateTool.prototype.create = function ({ description, execute }) {
|
|
8
|
+
let _this = this;
|
|
9
|
+
return {
|
|
10
|
+
description,
|
|
11
|
+
execute: async function (args) {
|
|
12
|
+
let toolInfo = {
|
|
13
|
+
type: "tool-call",
|
|
14
|
+
status: "pending",
|
|
15
|
+
id: Date.now().toString()+"_"+Math.random().toString(36).substring(2).toUpperCase(),
|
|
16
|
+
name: description.function.name,
|
|
17
|
+
caller: execute ? "backend" : "frontend",
|
|
18
|
+
args
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// 后端工具调用
|
|
22
|
+
if (execute) {
|
|
23
|
+
_this.ms.send(toolInfo); // 开始调用工具
|
|
24
|
+
|
|
25
|
+
let result, status;
|
|
26
|
+
try {
|
|
27
|
+
result = await execute(args);
|
|
28
|
+
status = "success"; // 调用成功
|
|
29
|
+
} catch (error) {
|
|
30
|
+
result = error;
|
|
31
|
+
status = "error"; // 调用失败
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_this.ms.send(initOption({ // 结束调用工具
|
|
35
|
+
status,
|
|
36
|
+
result
|
|
37
|
+
}, toolInfo));
|
|
38
|
+
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 前端工具调用
|
|
43
|
+
else {
|
|
44
|
+
let _resolve = null;
|
|
45
|
+
globalThis[toolInfo.id] = function (result) {
|
|
46
|
+
_resolve(result);
|
|
47
|
+
delete globalThis[toolInfo.id];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
_this.ms.send(toolInfo); // 开始调用工具
|
|
51
|
+
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
_resolve = resolve;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
60
|
export default CreateTool;
|
package/src/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { Model } from "@auipage/model/src/index.js";
|
|
2
|
-
import runConversation from "./runConversation.js";
|
|
3
|
-
import CreateTool from "./createTool.js";
|
|
4
|
-
|
|
5
|
-
function Agent(option) {
|
|
6
|
-
this.model = new Model(option.model);
|
|
7
|
-
this.tools = option.tools || [];
|
|
8
|
-
this.systemPrompt = option.systemPrompt || "";
|
|
9
|
-
this.stream = option.stream || false;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
Agent.prototype.generate = function (messages, logback, thinkback) {
|
|
13
|
-
if (this.systemPrompt) messages.unshift({
|
|
14
|
-
role: "system",
|
|
15
|
-
content: this.systemPrompt
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
return runConversation(this, messages, logback, thinkback);
|
|
19
|
-
};
|
|
20
|
-
|
|
1
|
+
import { Model } from "@auipage/model/src/index.js";
|
|
2
|
+
import runConversation from "./runConversation.js";
|
|
3
|
+
import CreateTool from "./createTool.js";
|
|
4
|
+
|
|
5
|
+
function Agent(option) {
|
|
6
|
+
this.model = new Model(option.model);
|
|
7
|
+
this.tools = option.tools || [];
|
|
8
|
+
this.systemPrompt = option.systemPrompt || "";
|
|
9
|
+
this.stream = option.stream || false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
Agent.prototype.generate = function (messages, logback, thinkback) {
|
|
13
|
+
if (this.systemPrompt) messages.unshift({
|
|
14
|
+
role: "system",
|
|
15
|
+
content: this.systemPrompt
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return runConversation(this, messages, logback, thinkback);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
21
|
export { Agent, CreateTool };
|
package/src/runConversation.js
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import callFuns from "./callFuns.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 把一次工具调用的执行结果转成 tool 消息。
|
|
5
|
-
*/
|
|
6
|
-
function toToolMessage(call, value) {
|
|
7
|
-
return {
|
|
8
|
-
role: "tool",
|
|
9
|
-
tool_call_id: call.id,
|
|
10
|
-
tool_name: call.function.name,
|
|
11
|
-
content: typeof value === "string" ? value : (JSON.stringify(value) + "")
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 把模型响应按 role 追加到 messages,返回是否还有工具调用需要下一轮处理。
|
|
17
|
-
*/
|
|
18
|
-
function appendAssistantMessage(messages, result) {
|
|
19
|
-
if (result.content) {
|
|
20
|
-
messages.push({ role: "assistant", content: result.content });
|
|
21
|
-
}
|
|
22
|
-
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
23
|
-
messages.push({ role: "assistant", tool_calls: result.tool_calls });
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 执行模型对话循环:
|
|
31
|
-
* 1. 调用模型获取响应
|
|
32
|
-
* 2. 若返回工具调用,依次执行工具并将结果追加到 messages,继续循环
|
|
33
|
-
* 3. 否则将最终内容 resolve 给调用方
|
|
34
|
-
*/
|
|
35
|
-
export default async function runConversation(_this, messages, logback, thinkback) {
|
|
36
|
-
|
|
37
|
-
while (true) {
|
|
38
|
-
const result = await _this.model.completions({
|
|
39
|
-
messages,
|
|
40
|
-
tools: _this.tools,
|
|
41
|
-
stream: _this.stream,
|
|
42
|
-
}, logback, thinkback);
|
|
43
|
-
|
|
44
|
-
const hasToolCalls = appendAssistantMessage(messages, result);
|
|
45
|
-
|
|
46
|
-
if (!hasToolCalls) {
|
|
47
|
-
return result.content;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const toolResults = await callFuns(_this, result.tool_calls);
|
|
51
|
-
|
|
52
|
-
result.tool_calls.forEach((call, i) => {
|
|
53
|
-
messages.push(toToolMessage(call, toolResults[i]));
|
|
54
|
-
});
|
|
55
|
-
}
|
|
1
|
+
import callFuns from "./callFuns.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 把一次工具调用的执行结果转成 tool 消息。
|
|
5
|
+
*/
|
|
6
|
+
function toToolMessage(call, value) {
|
|
7
|
+
return {
|
|
8
|
+
role: "tool",
|
|
9
|
+
tool_call_id: call.id,
|
|
10
|
+
tool_name: call.function.name,
|
|
11
|
+
content: typeof value === "string" ? value : (JSON.stringify(value) + "")
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 把模型响应按 role 追加到 messages,返回是否还有工具调用需要下一轮处理。
|
|
17
|
+
*/
|
|
18
|
+
function appendAssistantMessage(messages, result) {
|
|
19
|
+
if (result.content) {
|
|
20
|
+
messages.push({ role: "assistant", content: result.content });
|
|
21
|
+
}
|
|
22
|
+
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
23
|
+
messages.push({ role: "assistant", tool_calls: result.tool_calls });
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 执行模型对话循环:
|
|
31
|
+
* 1. 调用模型获取响应
|
|
32
|
+
* 2. 若返回工具调用,依次执行工具并将结果追加到 messages,继续循环
|
|
33
|
+
* 3. 否则将最终内容 resolve 给调用方
|
|
34
|
+
*/
|
|
35
|
+
export default async function runConversation(_this, messages, logback, thinkback) {
|
|
36
|
+
|
|
37
|
+
while (true) {
|
|
38
|
+
const result = await _this.model.completions({
|
|
39
|
+
messages,
|
|
40
|
+
tools: _this.tools,
|
|
41
|
+
stream: _this.stream,
|
|
42
|
+
}, logback, thinkback);
|
|
43
|
+
|
|
44
|
+
const hasToolCalls = appendAssistantMessage(messages, result);
|
|
45
|
+
|
|
46
|
+
if (!hasToolCalls) {
|
|
47
|
+
return result.content;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const toolResults = await callFuns(_this, result.tool_calls);
|
|
51
|
+
|
|
52
|
+
result.tool_calls.forEach((call, i) => {
|
|
53
|
+
messages.push(toToolMessage(call, toolResults[i]));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
56
|
}
|
package/types/agent.d.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent 构造器:模型对话循环 + 工具调用编排
|
|
3
|
-
*/
|
|
4
|
-
import type { ChatMessage, ModelConfig } from "@auipage/types";
|
|
5
|
-
import type { Model } from "@auipage/model";
|
|
6
|
-
import type { AgentTool } from "./tool.js";
|
|
7
|
-
|
|
8
|
-
/** 流式正文回调,参数为模型本次给出的文本(增量) */
|
|
9
|
-
export type Logback = (text: string) => void;
|
|
10
|
-
|
|
11
|
-
/** 流式思维链 / 推理过程回调,参数为本次增量文本 */
|
|
12
|
-
export type Thinkback = (text: string) => void;
|
|
13
|
-
|
|
14
|
-
export interface AgentOptions {
|
|
15
|
-
/** 透传给 @auipage/model 的模型配置 */
|
|
16
|
-
model: ModelConfig;
|
|
17
|
-
/** 系统提示词,会作为首条 system 消息发送,默认 "" */
|
|
18
|
-
systemPrompt?: string;
|
|
19
|
-
/** 工具列表,默认 [] */
|
|
20
|
-
tools?: AgentTool[];
|
|
21
|
-
/** 是否开启流式输出,默认 false */
|
|
22
|
-
stream?: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export declare class Agent {
|
|
26
|
-
constructor(options: AgentOptions);
|
|
27
|
-
|
|
28
|
-
/** 底层模型实例 */
|
|
29
|
-
model: Model;
|
|
30
|
-
tools: AgentTool[];
|
|
31
|
-
systemPrompt: string;
|
|
32
|
-
stream: boolean;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 执行对话循环:模型请求 → 工具调用 → 结果回传 → 继续请求,直至拿到最终文本。
|
|
36
|
-
*
|
|
37
|
-
* @param messages 本轮对话消息数组;配置了非空 systemPrompt 时会自动插到最前(原地修改)
|
|
38
|
-
* @param logback 流式正文增量回调
|
|
39
|
-
* @param thinkback 流式思维链增量回调
|
|
40
|
-
* @returns 模型最终回复;只产出工具调用而无文本时可能为 undefined
|
|
41
|
-
*/
|
|
42
|
-
generate(
|
|
43
|
-
messages: ChatMessage[],
|
|
44
|
-
logback?: Logback,
|
|
45
|
-
thinkback?: Thinkback
|
|
46
|
-
): Promise<string | undefined>;
|
|
47
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Agent 构造器:模型对话循环 + 工具调用编排
|
|
3
|
+
*/
|
|
4
|
+
import type { ChatMessage, ModelConfig } from "@auipage/types";
|
|
5
|
+
import type { Model } from "@auipage/model";
|
|
6
|
+
import type { AgentTool } from "./tool.js";
|
|
7
|
+
|
|
8
|
+
/** 流式正文回调,参数为模型本次给出的文本(增量) */
|
|
9
|
+
export type Logback = (text: string) => void;
|
|
10
|
+
|
|
11
|
+
/** 流式思维链 / 推理过程回调,参数为本次增量文本 */
|
|
12
|
+
export type Thinkback = (text: string) => void;
|
|
13
|
+
|
|
14
|
+
export interface AgentOptions {
|
|
15
|
+
/** 透传给 @auipage/model 的模型配置 */
|
|
16
|
+
model: ModelConfig;
|
|
17
|
+
/** 系统提示词,会作为首条 system 消息发送,默认 "" */
|
|
18
|
+
systemPrompt?: string;
|
|
19
|
+
/** 工具列表,默认 [] */
|
|
20
|
+
tools?: AgentTool[];
|
|
21
|
+
/** 是否开启流式输出,默认 false */
|
|
22
|
+
stream?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export declare class Agent {
|
|
26
|
+
constructor(options: AgentOptions);
|
|
27
|
+
|
|
28
|
+
/** 底层模型实例 */
|
|
29
|
+
model: Model;
|
|
30
|
+
tools: AgentTool[];
|
|
31
|
+
systemPrompt: string;
|
|
32
|
+
stream: boolean;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 执行对话循环:模型请求 → 工具调用 → 结果回传 → 继续请求,直至拿到最终文本。
|
|
36
|
+
*
|
|
37
|
+
* @param messages 本轮对话消息数组;配置了非空 systemPrompt 时会自动插到最前(原地修改)
|
|
38
|
+
* @param logback 流式正文增量回调
|
|
39
|
+
* @param thinkback 流式思维链增量回调
|
|
40
|
+
* @returns 模型最终回复;只产出工具调用而无文本时可能为 undefined
|
|
41
|
+
*/
|
|
42
|
+
generate(
|
|
43
|
+
messages: ChatMessage[],
|
|
44
|
+
logback?: Logback,
|
|
45
|
+
thinkback?: Thinkback
|
|
46
|
+
): Promise<string | undefined>;
|
|
47
|
+
}
|
package/types/create-tool.d.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CreateTool:前后端工具桥接器
|
|
3
|
-
*
|
|
4
|
-
* 运行在服务端的 Agent 通过消息流把工具调用状态推送给页面,
|
|
5
|
-
* 页面执行完“前端工具”后再把结果回传,唤醒挂起的调用。
|
|
6
|
-
*/
|
|
7
|
-
import type {
|
|
8
|
-
StreamEvent,
|
|
9
|
-
ToolDescription,
|
|
10
|
-
ToolExecutor
|
|
11
|
-
} from "@auipage/types";
|
|
12
|
-
import type { AgentTool } from "./tool.js";
|
|
13
|
-
|
|
14
|
-
/** 消息流发送端(结构与 @auipage/core 的 MessageStream 兼容) */
|
|
15
|
-
export interface MessageSender {
|
|
16
|
-
send(event: StreamEvent): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** ct.create 的入参:execute 缺省即“前端工具”,调用会挂起等待页面回传 */
|
|
20
|
-
export interface CreateToolInput {
|
|
21
|
-
description: ToolDescription;
|
|
22
|
-
/** 后端工具:在 Agent 所在服务端执行;缺省则为前端工具 */
|
|
23
|
-
execute?: ToolExecutor;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export declare class CreateTool {
|
|
27
|
-
constructor(ms: MessageSender);
|
|
28
|
-
|
|
29
|
-
/** 构造时传入的消息流发送端 */
|
|
30
|
-
ms: MessageSender;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 包装一个工具为标准 AgentTool:
|
|
34
|
-
* - 有 execute:执行前后通过消息流推送 pending / success / error;
|
|
35
|
-
* - 无 execute:只推送 pending,随后挂起,等待 globalThis[id](result) 唤醒。
|
|
36
|
-
*/
|
|
37
|
-
create(input: CreateToolInput): AgentTool;
|
|
38
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* CreateTool:前后端工具桥接器
|
|
3
|
+
*
|
|
4
|
+
* 运行在服务端的 Agent 通过消息流把工具调用状态推送给页面,
|
|
5
|
+
* 页面执行完“前端工具”后再把结果回传,唤醒挂起的调用。
|
|
6
|
+
*/
|
|
7
|
+
import type {
|
|
8
|
+
StreamEvent,
|
|
9
|
+
ToolDescription,
|
|
10
|
+
ToolExecutor
|
|
11
|
+
} from "@auipage/types";
|
|
12
|
+
import type { AgentTool } from "./tool.js";
|
|
13
|
+
|
|
14
|
+
/** 消息流发送端(结构与 @auipage/core 的 MessageStream 兼容) */
|
|
15
|
+
export interface MessageSender {
|
|
16
|
+
send(event: StreamEvent): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** ct.create 的入参:execute 缺省即“前端工具”,调用会挂起等待页面回传 */
|
|
20
|
+
export interface CreateToolInput {
|
|
21
|
+
description: ToolDescription;
|
|
22
|
+
/** 后端工具:在 Agent 所在服务端执行;缺省则为前端工具 */
|
|
23
|
+
execute?: ToolExecutor;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export declare class CreateTool {
|
|
27
|
+
constructor(ms: MessageSender);
|
|
28
|
+
|
|
29
|
+
/** 构造时传入的消息流发送端 */
|
|
30
|
+
ms: MessageSender;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 包装一个工具为标准 AgentTool:
|
|
34
|
+
* - 有 execute:执行前后通过消息流推送 pending / success / error;
|
|
35
|
+
* - 无 execute:只推送 pending,随后挂起,等待 globalThis[id](result) 唤醒。
|
|
36
|
+
*/
|
|
37
|
+
create(input: CreateToolInput): AgentTool;
|
|
38
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @auipage/agent 类型入口
|
|
3
|
-
*
|
|
4
|
-
* 智能体对话循环:工具调用编排、思维链回调,以及前后端工具桥接。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
Agent,
|
|
9
|
-
type AgentOptions,
|
|
10
|
-
type Logback,
|
|
11
|
-
type Thinkback
|
|
12
|
-
} from "./agent.js";
|
|
13
|
-
|
|
14
|
-
export type { AgentTool } from "./tool.js";
|
|
15
|
-
|
|
16
|
-
export {
|
|
17
|
-
CreateTool,
|
|
18
|
-
type MessageSender,
|
|
19
|
-
type CreateToolInput
|
|
20
|
-
} from "./create-tool.js";
|
|
1
|
+
/**
|
|
2
|
+
* @auipage/agent 类型入口
|
|
3
|
+
*
|
|
4
|
+
* 智能体对话循环:工具调用编排、思维链回调,以及前后端工具桥接。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
Agent,
|
|
9
|
+
type AgentOptions,
|
|
10
|
+
type Logback,
|
|
11
|
+
type Thinkback
|
|
12
|
+
} from "./agent.js";
|
|
13
|
+
|
|
14
|
+
export type { AgentTool } from "./tool.js";
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
CreateTool,
|
|
18
|
+
type MessageSender,
|
|
19
|
+
type CreateToolInput
|
|
20
|
+
} from "./create-tool.js";
|
package/types/tool.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent 直接执行的工具形态
|
|
3
|
-
*/
|
|
4
|
-
import type { ToolDescription, ToolExecutor } from "@auipage/types";
|
|
5
|
-
|
|
6
|
-
export interface AgentTool {
|
|
7
|
-
/** 符合模型 tools 规范的描述对象 */
|
|
8
|
-
description: ToolDescription;
|
|
9
|
-
/**
|
|
10
|
-
* 工具执行函数。
|
|
11
|
-
* 注意:普通工具必须提供;只有经 CreateTool 桥接的“前端工具”
|
|
12
|
-
* 才允许在创建时缺省(由 CreateTool.create 补齐 execute)。
|
|
13
|
-
*/
|
|
14
|
-
execute: ToolExecutor;
|
|
15
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Agent 直接执行的工具形态
|
|
3
|
+
*/
|
|
4
|
+
import type { ToolDescription, ToolExecutor } from "@auipage/types";
|
|
5
|
+
|
|
6
|
+
export interface AgentTool {
|
|
7
|
+
/** 符合模型 tools 规范的描述对象 */
|
|
8
|
+
description: ToolDescription;
|
|
9
|
+
/**
|
|
10
|
+
* 工具执行函数。
|
|
11
|
+
* 注意:普通工具必须提供;只有经 CreateTool 桥接的“前端工具”
|
|
12
|
+
* 才允许在创建时缺省(由 CreateTool.create 补齐 execute)。
|
|
13
|
+
*/
|
|
14
|
+
execute: ToolExecutor;
|
|
15
|
+
}
|