@auipage/agent 0.1.0-alpha.5 → 0.2.0-alpha.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 CHANGED
@@ -1,4 +1,29 @@
1
1
  v0.1.0:
2
- date:
2
+ date:2026-10-25
3
3
  changes:
4
4
  - 初始化版本
5
+ v0.2.0:
6
+ date:
7
+ changes:
8
+ - 修复bug:
9
+ 1、模型请求 4xx / 网络失败时进程崩溃或请求永久挂起
10
+ 2、页面在适配器、前端工具、历史入库任一环节失败后锁死(isExecuting 一直为 true)
11
+ 3、工具并行调用分片串味,无参工具的调用直接失败
12
+ 4、复用同一 messages 数组时 systemPrompt 重复累积
13
+ 5、流式数据含 \r 换行或末尾 [DONE] 时正文被污染、解析提前中断
14
+ 6、响应结束后才订阅 error 的监听器收不到终态
15
+ - 优化改造:
16
+ 1、请求支持 timeout 与 signal,可主动取消
17
+ 2、非 2xx 响应新增 http-error 事件,不再只有 ok=false
18
+ 3、页面历史入库失败会明确报错,不再静默丢弃
19
+ 4、监听器异常不再抛出为进程级异常
20
+ 5、流式生成器取消后清空挂起队列,不再堆积残留
21
+ - 新增功能:
22
+ 1、Agent 支持 maxSteps(默认 0,不限制)
23
+ 2、新增 toModelMessages,把页面消息转成模型入参
24
+ 3、新增 AuiPage.stop():执行中主动结束,内容保留、消息组标记 stopped,sendMessage 正常 resolve
25
+ 4、新增 AuiPage.cancel():取消本轮,消息组标记 cancelled,sendMessage 以「已取消」reject
26
+ 5、主动结束与取消的那一轮仍入库,存或丢由业务自行决定
27
+ 6、task:end 增加 by(user / tool),取消时另带 displayed,列出已显示条目供界面撤销
28
+ 7、工具 execute / render 拿到任务控制句柄,可直接 stop() / cancel() 结束整轮
29
+ 8、取消信号经 context.signal 透传,Agent / model 均支持中断
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # [@auipage/agent](https://github.com/handless-ui/auipage/packages/agent/README.md)
1
+ # [@auipage/agent](https://github.com/handless-ui/auipage)
2
2
 
3
- 智能体对话循环:在 `@auipage/model` 之上处理多轮工具调用(模型要调工具 → 执行 → 结果回灌模型 → 继续生成),并提供 `CreateTool` 把服务端工具调用桥接到浏览器页面执行。
3
+ 智能体对话循环:模型要调工具 → 执行 → 结果回灌 → 继续生成,直到拿到最终回复。可选地把工具调用桥接到浏览器,让页面来执行。
4
4
 
5
5
  <p>
6
6
  <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@auipage/agent&interval=7">
@@ -14,45 +14,22 @@
14
14
  </a>
15
15
  </p>
16
16
 
17
- <img src="https://nodei.co/npm/@auipage/agent.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
17
+ ## 它解决什么
18
18
 
19
- ## 安装
19
+ 单轮对话只是“发一条消息、收一段文本”。一旦模型会用工具,循环就变复杂了:
20
20
 
21
- ```
22
- npm install --save @auipage/agent
23
- ```
21
+ - 模型返回 `tool_calls` 后,要按名字找到工具、执行、把结果作为 `tool` 消息塞回消息列表;
22
+ - 一轮可能调多次工具,也可能调完还要继续调,循环什么时候停要判断对;
23
+ - 工具在服务端执行还是浏览器执行,两套路径要分开;
24
+ - 浏览器执行的工具,结果还得推回服务端,唤醒挂起的那次调用。
24
25
 
25
- ## 基本用法
26
+ `@auipage/agent` 把循环和桥接都收进一个 `generate()`,你只需要描述工具。
27
+
28
+ ## 30 秒例子
26
29
 
27
30
  ```javascript
28
31
  import { Agent } from "@auipage/agent";
29
32
 
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
33
  const getWeather = {
57
34
  description: {
58
35
  type: "function",
@@ -61,20 +38,18 @@ const getWeather = {
61
38
  description: "查询指定城市的天气",
62
39
  parameters: {
63
40
  type: "object",
64
- properties: {
65
- city: { type: "string", description: "城市名称,例如:南京" }
66
- },
41
+ properties: { city: { type: "string", description: "城市名称,例如:南京" } },
67
42
  required: ["city"]
68
43
  }
69
44
  }
70
45
  },
71
46
  execute({ city }) {
72
- return `${city} 今天晴,25°C`; // 也可以 return fetch(...).then(...)
47
+ return `${city} 今天晴,25°C`; // 也可以 return fetch(...)
73
48
  }
74
49
  };
75
50
 
76
51
  const agent = new Agent({
77
- model: { /* 同上 */ },
52
+ model: { model: "qwen3.5", apiKey: "ollama", baseURL: "http://localhost:11434/v1" },
78
53
  systemPrompt: "你可以使用 getWeather 查询天气。",
79
54
  tools: [getWeather]
80
55
  });
@@ -82,13 +57,25 @@ const agent = new Agent({
82
57
  const answer = await agent.generate([
83
58
  { role: "user", content: "南京今天天气怎么样?" }
84
59
  ]);
60
+
61
+ console.log(answer); // 模型拿到工具结果后给出的最终回复
85
62
  ```
86
63
 
87
- 模型决定调用工具时,Agent 按名找到工具执行,把结果作为 `tool` 消息回灌模型继续生成,直到拿到最终回复。
64
+ `generate(messages, logback?, thinkback?)`:
65
+
66
+ - `messages` 是 OpenAI 格式消息数组;构造时配了非空 `systemPrompt` 才会插到队首(**会原地修改数组**);
67
+ - `logback(delta)` 收正文增量,`thinkback(reasoning)` 收推理增量;
68
+ - 返回 `Promise<string | undefined>`;整轮只有工具调用、没有正文时为 `undefined`。
69
+
70
+ 安装:
71
+
72
+ ```bash
73
+ npm install --save @auipage/agent
74
+ ```
88
75
 
89
- ## 前后端工具桥接(CreateTool)
76
+ ## 前后端工具桥接
90
77
 
91
- Agent 跑在服务端、部分工具需要浏览器执行时,用 `CreateTool` 包装。它接收一个有 `send(data)` 方法的消息流(如 `@auipage/core` 的 `MessageStream`),在工具执行节点把事件推给页面,并支持页面把结果回传唤醒挂起的调用。
78
+ 工具需要浏览器执行时(比如展示文件、弹窗确认),用 `CreateTool` 包装一个有 `send()` 的消息流:
92
79
 
93
80
  ```javascript
94
81
  import { Agent, CreateTool } from "@auipage/agent";
@@ -103,49 +90,44 @@ const ct = new CreateTool(ms);
103
90
  const agent = new Agent({
104
91
  model: { /* 同上 */ },
105
92
  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
- })
93
+ ct.create(readPlain), // 带 execute:服务端执行
94
+ ct.create(showPlain) // 不带 execute:下发浏览器执行
117
95
  ]
118
96
  });
119
97
  ```
120
98
 
121
- `ct.create(tool)` 返回标准的 `{ description, execute }`,按是否传 `execute` 分两类:
99
+ - **后端工具**:推 `pending`,同一事件上补 `success` / `error` 和 `result`;
100
+ - **前端工具**:只推 `pending`,本次调用挂起;页面执行完把 `{ id, result }` POST 回来,服务端 `globalThis[id](result)` 唤醒,对话继续。
122
101
 
123
- - **后端工具**(`caller: "backend"`):执行前推 `pending`,结束后在同一事件上补 `success` / `error` 和 `result`。
102
+ 页面侧用 `@auipage/core` 的 `defineTool` 注册同名工具即可,详见 [defineTool 参考](https://handless-ui.github.io/auipage/packages/core-utils.html)。
124
103
 
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
- ```
104
+ ## 这个包不是必须的
129
105
 
130
- - **前端工具**(`caller: "frontend"`):只推 `pending`,本次调用返回一个 Promise 挂起。页面处理完后把 `{ id, result }` 发回服务端,由服务端调用 `globalThis[id](result)` 唤醒,对话才继续:
106
+ | 你的情况 | 可以换成 | 会失去什么 |
107
+ | --- | --- | --- |
108
+ | 工具都在服务端 | 官方 Agents SDK、LangChain、Vercel AI SDK | 生态更大,但抽象更重、依赖更多 |
109
+ | 只有一两个工具 | 自己写 `while` 循环(约 20 行) | 要自己处理 `tool_calls` 回灌和终止条件 |
110
+ | 不需要工具 | 直接用 `@auipage/model` 或官方 SDK | 少一层封装,代码更短 |
111
+ | 工具要在浏览器执行 | 自己做 SSE 推事件 + 结果回传 | 等价于重写 `CreateTool`,容易漏掉挂起与唤醒 |
131
112
 
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` 的工具只在桥接场景下合法,否则执行会报错。
113
+ 它最独特的价值是 **`CreateTool` 的前后端桥接**:同一份工具描述,服务端和浏览器各执行各的部分。如果你的工具全在服务端,别的 Agent 框架同样好用。
140
114
 
141
115
  ## 配置
142
116
 
143
117
  | 字段 | 类型 | 必填 | 说明 |
144
118
  | --- | --- | --- | --- |
145
119
  | `model` | `{ model, apiKey, baseURL }` | 是 | 透传给 `@auipage/model` |
146
- | `systemPrompt` | `string` | 否 | 系统提示词,默认空字符串 |
120
+ | `systemPrompt` | `string` | 否 | 默认空字符串 |
147
121
  | `stream` | `boolean` | 否 | 默认 `false` |
148
- | `tools` | `AgentTool[]` | 否 | 工具列表,默认 `[]`;直接给 Agent 的工具必须带 `execute`,省略 `execute` 只在 `CreateTool` 桥接场景合法 |
122
+ | `tools` | `AgentTool[]` | 否 | 默认 `[]`;直接给 Agent 的工具必须带 `execute`,省略只在 `CreateTool` 场景合法 |
123
+
124
+ ## 它在哪一环
125
+
126
+ ```
127
+ fetch ← model ← agent
128
+ ```
129
+
130
+ `@auipage/core` 的 `ChatTransportAdapter` 通常在里面调它。完整事件协议见 [agent 参考](https://handless-ui.github.io/auipage/packages/agent.html)。
149
131
 
150
132
  ## 版权
151
133