@auipage/agent 0.1.0-alpha.5 → 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 CHANGED
@@ -1,6 +1,6 @@
1
- Authors ordered by first contribution.
2
-
3
- zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
-
5
- -----------------------------------------------------
6
- We thank all of them for their contributions!
1
+ Authors ordered by first contribution.
2
+
3
+ zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
+
5
+ -----------------------------------------------------
6
+ We thank all of them for their contributions!
package/CHANGELOG CHANGED
@@ -1,4 +1,4 @@
1
- v0.1.0:
2
- date:
3
- changes:
4
- - 初始化版本
1
+ v0.1.0:
2
+ date:2026-10-25
3
+ changes:
4
+ - 初始化版本
package/README.md CHANGED
@@ -1,154 +1,154 @@
1
- # [@auipage/agent](https://github.com/handless-ui/auipage/packages/agent/README.md)
2
-
3
- 智能体对话循环:在 `@auipage/model` 之上处理多轮工具调用(模型要调工具 → 执行 → 结果回灌模型 → 继续生成),并提供 `CreateTool` 把服务端工具调用桥接到浏览器页面执行。
4
-
5
- <p>
6
- <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@auipage/agent&interval=7">
7
- <img src="https://img.shields.io/npm/dm/@auipage/agent.svg" alt="downloads">
8
- </a>
9
- <a href="https://www.npmjs.com/package/@auipage/agent">
10
- <img src="https://img.shields.io/npm/v/@auipage/agent.svg" alt="npm">
11
- </a>
12
- <a href="https://github.com/handless-ui/auipage" target='_blank'>
13
- <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/handless-ui/auipage?style=social">
14
- </a>
15
- </p>
16
-
17
- <img src="https://nodei.co/npm/@auipage/agent.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
18
-
19
- ## 安装
20
-
21
- ```
22
- npm install --save @auipage/agent
23
- ```
24
-
25
- ## 基本用法
26
-
27
- ```javascript
28
- import { Agent } from "@auipage/agent";
29
-
30
- const agent = new Agent({
31
- model: {
32
- model: "gpt-4o-mini",
33
- apiKey: "your-api-key",
34
- baseURL: "https://api.openai.com/v1"
35
- },
36
- systemPrompt: "你是一个简洁、乐于助人的助手。"
37
- });
38
-
39
- const answer = await agent.generate([
40
- { role: "user", content: "用一句话介绍 JavaScript" }
41
- ]);
42
- ```
43
-
44
- `generate(messages, logback?, thinkback?)`:
45
-
46
- - `messages` 为消息数组;构造时配置了非空 `systemPrompt` 才会插到队首作为 `system` 消息(会原地修改传入的数组)。
47
- - `logback(delta)`:流式正文增量回调。
48
- - `thinkback(reasoning)`:推理过程增量回调。
49
- - 返回 `Promise<string | undefined>`;模型整轮只产出工具调用、没有正文时为 `undefined`。
50
-
51
- ## 注册工具
52
-
53
- 工具是 `{ description, execute }` 对象:`description` 遵循模型的 tools 描述规范,`execute(args)` 在服务端执行,可同步可返回 `Promise`,模型返回的参数会解析后传入。
54
-
55
- ```javascript
56
- const getWeather = {
57
- description: {
58
- type: "function",
59
- function: {
60
- name: "getWeather",
61
- description: "查询指定城市的天气",
62
- parameters: {
63
- type: "object",
64
- properties: {
65
- city: { type: "string", description: "城市名称,例如:南京" }
66
- },
67
- required: ["city"]
68
- }
69
- }
70
- },
71
- execute({ city }) {
72
- return `${city} 今天晴,25°C`; // 也可以 return fetch(...).then(...)
73
- }
74
- };
75
-
76
- const agent = new Agent({
77
- model: { /* 同上 */ },
78
- systemPrompt: "你可以使用 getWeather 查询天气。",
79
- tools: [getWeather]
80
- });
81
-
82
- const answer = await agent.generate([
83
- { role: "user", content: "南京今天天气怎么样?" }
84
- ]);
85
- ```
86
-
87
- 模型决定调用工具时,Agent 按名找到工具执行,把结果作为 `tool` 消息回灌模型继续生成,直到拿到最终回复。
88
-
89
- ## 前后端工具桥接(CreateTool)
90
-
91
- Agent 跑在服务端、部分工具需要浏览器执行时,用 `CreateTool` 包装。它接收一个有 `send(data)` 方法的消息流(如 `@auipage/core` 的 `MessageStream`),在工具执行节点把事件推给页面,并支持页面把结果回传唤醒挂起的调用。
92
-
93
- ```javascript
94
- import { Agent, CreateTool } from "@auipage/agent";
95
- import { MessageStream } from "@auipage/core";
96
-
97
- const ms = new MessageStream(
98
- data => res.write("data: " + JSON.stringify(data) + "\n\n"),
99
- () => res.end()
100
- );
101
- const ct = new CreateTool(ms);
102
-
103
- const agent = new Agent({
104
- model: { /* 同上 */ },
105
- tools: [
106
- // 后端工具:带 execute,在服务端执行
107
- ct.create({
108
- description: { /* ... name: "readPlain" */ },
109
- execute({ filepath }) {
110
- return readFile(filepath);
111
- }
112
- }),
113
- // 前端工具:不带 execute,调用挂起,等页面回传结果
114
- ct.create({
115
- description: { /* ... name: "showPlain" */ }
116
- })
117
- ]
118
- });
119
- ```
120
-
121
- `ct.create(tool)` 返回标准的 `{ description, execute }`,按是否传 `execute` 分两类:
122
-
123
- - **后端工具**(`caller: "backend"`):执行前推 `pending`,结束后在同一事件上补 `success` / `error` 和 `result`。
124
-
125
- ```javascript
126
- { type: "tool-call", status: "pending", id, name, caller: "backend", args }
127
- { type: "tool-call", status: "success", id, name, caller: "backend", args, result }
128
- ```
129
-
130
- - **前端工具**(`caller: "frontend"`):只推 `pending`,本次调用返回一个 Promise 挂起。页面处理完后把 `{ id, result }` 发回服务端,由服务端调用 `globalThis[id](result)` 唤醒,对话才继续:
131
-
132
- ```javascript
133
- if (globalThis[data.id]) {
134
- globalThis[data.id](data.result);
135
- delete globalThis[data.id];
136
- }
137
- ```
138
-
139
- 不走 `CreateTool` 时请直接传带 `execute` 的普通工具;没有 `execute` 的工具只在桥接场景下合法,否则执行会报错。
140
-
141
- ## 配置
142
-
143
- | 字段 | 类型 | 必填 | 说明 |
144
- | --- | --- | --- | --- |
145
- | `model` | `{ model, apiKey, baseURL }` | 是 | 透传给 `@auipage/model` |
146
- | `systemPrompt` | `string` | 否 | 系统提示词,默认空字符串 |
147
- | `stream` | `boolean` | 否 | 默认 `false` |
148
- | `tools` | `AgentTool[]` | 否 | 工具列表,默认 `[]`;直接给 Agent 的工具必须带 `execute`,省略 `execute` 只在 `CreateTool` 桥接场景合法 |
149
-
150
- ## 版权
151
-
152
- MIT License
153
-
154
- Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
1
+ # [@auipage/agent](https://github.com/handless-ui/auipage/packages/agent/README.md)
2
+
3
+ 智能体对话循环:在 `@auipage/model` 之上处理多轮工具调用(模型要调工具 → 执行 → 结果回灌模型 → 继续生成),并提供 `CreateTool` 把服务端工具调用桥接到浏览器页面执行。
4
+
5
+ <p>
6
+ <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@auipage/agent&interval=7">
7
+ <img src="https://img.shields.io/npm/dm/@auipage/agent.svg" alt="downloads">
8
+ </a>
9
+ <a href="https://www.npmjs.com/package/@auipage/agent">
10
+ <img src="https://img.shields.io/npm/v/@auipage/agent.svg" alt="npm">
11
+ </a>
12
+ <a href="https://github.com/handless-ui/auipage" target='_blank'>
13
+ <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/handless-ui/auipage?style=social">
14
+ </a>
15
+ </p>
16
+
17
+ <img src="https://nodei.co/npm/@auipage/agent.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
18
+
19
+ ## 安装
20
+
21
+ ```
22
+ npm install --save @auipage/agent
23
+ ```
24
+
25
+ ## 基本用法
26
+
27
+ ```javascript
28
+ import { Agent } from "@auipage/agent";
29
+
30
+ const agent = new Agent({
31
+ model: {
32
+ model: "gpt-4o-mini",
33
+ apiKey: "your-api-key",
34
+ baseURL: "https://api.openai.com/v1"
35
+ },
36
+ systemPrompt: "你是一个简洁、乐于助人的助手。"
37
+ });
38
+
39
+ const answer = await agent.generate([
40
+ { role: "user", content: "用一句话介绍 JavaScript" }
41
+ ]);
42
+ ```
43
+
44
+ `generate(messages, logback?, thinkback?)`:
45
+
46
+ - `messages` 为消息数组;构造时配置了非空 `systemPrompt` 才会插到队首作为 `system` 消息(会原地修改传入的数组)。
47
+ - `logback(delta)`:流式正文增量回调。
48
+ - `thinkback(reasoning)`:推理过程增量回调。
49
+ - 返回 `Promise<string | undefined>`;模型整轮只产出工具调用、没有正文时为 `undefined`。
50
+
51
+ ## 注册工具
52
+
53
+ 工具是 `{ description, execute }` 对象:`description` 遵循模型的 tools 描述规范,`execute(args)` 在服务端执行,可同步可返回 `Promise`,模型返回的参数会解析后传入。
54
+
55
+ ```javascript
56
+ const getWeather = {
57
+ description: {
58
+ type: "function",
59
+ function: {
60
+ name: "getWeather",
61
+ description: "查询指定城市的天气",
62
+ parameters: {
63
+ type: "object",
64
+ properties: {
65
+ city: { type: "string", description: "城市名称,例如:南京" }
66
+ },
67
+ required: ["city"]
68
+ }
69
+ }
70
+ },
71
+ execute({ city }) {
72
+ return `${city} 今天晴,25°C`; // 也可以 return fetch(...).then(...)
73
+ }
74
+ };
75
+
76
+ const agent = new Agent({
77
+ model: { /* 同上 */ },
78
+ systemPrompt: "你可以使用 getWeather 查询天气。",
79
+ tools: [getWeather]
80
+ });
81
+
82
+ const answer = await agent.generate([
83
+ { role: "user", content: "南京今天天气怎么样?" }
84
+ ]);
85
+ ```
86
+
87
+ 模型决定调用工具时,Agent 按名找到工具执行,把结果作为 `tool` 消息回灌模型继续生成,直到拿到最终回复。
88
+
89
+ ## 前后端工具桥接(CreateTool)
90
+
91
+ Agent 跑在服务端、部分工具需要浏览器执行时,用 `CreateTool` 包装。它接收一个有 `send(data)` 方法的消息流(如 `@auipage/core` 的 `MessageStream`),在工具执行节点把事件推给页面,并支持页面把结果回传唤醒挂起的调用。
92
+
93
+ ```javascript
94
+ import { Agent, CreateTool } from "@auipage/agent";
95
+ import { MessageStream } from "@auipage/core";
96
+
97
+ const ms = new MessageStream(
98
+ data => res.write("data: " + JSON.stringify(data) + "\n\n"),
99
+ () => res.end()
100
+ );
101
+ const ct = new CreateTool(ms);
102
+
103
+ const agent = new Agent({
104
+ model: { /* 同上 */ },
105
+ tools: [
106
+ // 后端工具:带 execute,在服务端执行
107
+ ct.create({
108
+ description: { /* ... name: "readPlain" */ },
109
+ execute({ filepath }) {
110
+ return readFile(filepath);
111
+ }
112
+ }),
113
+ // 前端工具:不带 execute,调用挂起,等页面回传结果
114
+ ct.create({
115
+ description: { /* ... name: "showPlain" */ }
116
+ })
117
+ ]
118
+ });
119
+ ```
120
+
121
+ `ct.create(tool)` 返回标准的 `{ description, execute }`,按是否传 `execute` 分两类:
122
+
123
+ - **后端工具**(`caller: "backend"`):执行前推 `pending`,结束后在同一事件上补 `success` / `error` 和 `result`。
124
+
125
+ ```javascript
126
+ { type: "tool-call", status: "pending", id, name, caller: "backend", args }
127
+ { type: "tool-call", status: "success", id, name, caller: "backend", args, result }
128
+ ```
129
+
130
+ - **前端工具**(`caller: "frontend"`):只推 `pending`,本次调用返回一个 Promise 挂起。页面处理完后把 `{ id, result }` 发回服务端,由服务端调用 `globalThis[id](result)` 唤醒,对话才继续:
131
+
132
+ ```javascript
133
+ if (globalThis[data.id]) {
134
+ globalThis[data.id](data.result);
135
+ delete globalThis[data.id];
136
+ }
137
+ ```
138
+
139
+ 不走 `CreateTool` 时请直接传带 `execute` 的普通工具;没有 `execute` 的工具只在桥接场景下合法,否则执行会报错。
140
+
141
+ ## 配置
142
+
143
+ | 字段 | 类型 | 必填 | 说明 |
144
+ | --- | --- | --- | --- |
145
+ | `model` | `{ model, apiKey, baseURL }` | 是 | 透传给 `@auipage/model` |
146
+ | `systemPrompt` | `string` | 否 | 系统提示词,默认空字符串 |
147
+ | `stream` | `boolean` | 否 | 默认 `false` |
148
+ | `tools` | `AgentTool[]` | 否 | 工具列表,默认 `[]`;直接给 Agent 的工具必须带 `execute`,省略 `execute` 只在 `CreateTool` 桥接场景合法 |
149
+
150
+ ## 版权
151
+
152
+ MIT License
153
+
154
+ Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步