@glodon-aiot/bot-client-sdk 3.26.0-alpha.9 → 3.26.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/README.md +243 -225
- package/dist/es/AgentSession.mjs +267 -197
- package/dist/es/BotClient.mjs +200 -123
- package/dist/es/Config.mjs +56 -67
- package/dist/es/DialogSession.mjs +123 -118
- package/dist/es/EventBus.mjs +38 -34
- package/dist/es/Logger.mjs +19 -21
- package/dist/es/Session.mjs +338 -284
- package/dist/es/api/index.mjs +31 -30
- package/dist/es/builtinTools/constants.mjs +12 -5
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +182 -160
- package/dist/es/builtinTools/paths.mjs +34 -27
- package/dist/es/builtinTools/platformSkills.mjs +248 -0
- package/dist/es/builtinTools/schemas.mjs +63 -18
- package/dist/es/builtinTools/tools.mjs +368 -244
- package/dist/es/index.mjs +62 -61
- package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
- package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +113 -131
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +60 -55
- package/dist/es/localFiles/constants.mjs +24 -28
- package/dist/es/localFiles/createLocalFilesPlugin.mjs +53 -52
- package/dist/es/localFiles/errors.mjs +44 -35
- package/dist/es/localFiles/host/connectHostBridge.mjs +20 -15
- package/dist/es/localFiles/host/protocol.mjs +27 -14
- package/dist/es/localFiles/host/sandboxClient.mjs +297 -127
- package/dist/es/localFiles/preview.mjs +33 -0
- package/dist/es/localFiles/schemas.mjs +22 -53
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +156 -89
- package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
- package/dist/es/localMcp/client.mjs +234 -0
- package/dist/es/localMcp/endpoints.mjs +94 -0
- package/dist/es/localMcp/register.mjs +207 -0
- package/dist/es/localMcp/registry.mjs +43 -0
- package/dist/es/package.json.mjs +34 -54
- package/dist/es/sessionUpload.mjs +49 -0
- package/dist/es/utils.mjs +24 -11
- package/dist/lib/index.js +48 -40
- package/dist/src/AgentSession.d.ts +13 -0
- package/dist/src/BotClient.d.ts +13 -1
- package/dist/src/Config.d.ts +6 -7
- package/dist/src/EventBus.d.ts +3 -0
- package/dist/src/Logger.d.ts +15 -8
- package/dist/src/PayloadTypes.d.ts +2 -0
- package/dist/src/Session.d.ts +19 -2
- package/dist/src/api/index.d.ts +2 -2
- package/dist/src/builtinTools/command.d.ts +7 -0
- package/dist/src/builtinTools/constants.d.ts +10 -2
- package/dist/src/builtinTools/paths.d.ts +3 -2
- package/dist/src/builtinTools/platformSkills.d.ts +92 -0
- package/dist/src/builtinTools/tools.d.ts +13 -0
- package/dist/src/constant.d.ts +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +7 -4
- package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +8 -0
- package/dist/src/localFiles/constants.d.ts +4 -13
- package/dist/src/localFiles/errors.d.ts +1 -1
- package/dist/src/localFiles/host/protocol.d.ts +14 -3
- package/dist/src/localFiles/host/sandboxClient.d.ts +50 -0
- package/dist/src/localFiles/index.d.ts +9 -5
- package/dist/src/localFiles/preview.d.ts +2 -0
- package/dist/src/localFiles/schemas.d.ts +0 -2
- package/dist/src/localFiles/types.d.ts +2 -22
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +25 -3
- package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
- package/dist/src/localMcp/client.d.ts +15 -0
- package/dist/src/localMcp/endpoints.d.ts +19 -0
- package/dist/src/localMcp/register.d.ts +34 -0
- package/dist/src/localMcp/registry.d.ts +27 -0
- package/dist/src/sessionUpload.d.ts +14 -0
- package/dist/src/utils.d.ts +6 -0
- package/package.json +4 -24
package/README.md
CHANGED
|
@@ -1,61 +1,76 @@
|
|
|
1
1
|
# @glodon-aiot/bot-client-sdk
|
|
2
2
|
|
|
3
|
-
AIoT 应用客户端 SDK,支持 Agent
|
|
3
|
+
AIoT 应用客户端 SDK,支持 Agent 与对话型应用。会话、消息、产出物和客户端插件由本包执行,**界面由接入方实现**。
|
|
4
|
+
|
|
5
|
+
## 按角色阅读
|
|
6
|
+
|
|
7
|
+
| 你要做的事 | 读这些 |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| 只接对话 UI | [快速开始](#快速开始) → [对话界面](#对话界面) → [产出物](#产出物) |
|
|
10
|
+
| 浏览器里读本地文件夹 / 跑命令 | 上栏 + [Agent 插件](#agent-插件) → [本地工作文件夹](#本地工作文件夹) → [命令和脚本](#命令和脚本) |
|
|
11
|
+
| 端侧宿主(桌面内嵌页 + 沙箱) | [宿主通道](#宿主通道);沙箱协议与策略见 npm 包 `@glodon-aiot/workspace-sandbox` |
|
|
12
|
+
| 查工具参数 / 错误码 / 限额 | [内置工具参考](#内置工具参考) · [安全与限额](#安全与限额) · [错误码](#错误码) |
|
|
4
13
|
|
|
5
14
|
| 能力 | 适用应用 | 运行环境 | 见 |
|
|
6
15
|
| --- | --- | --- | --- |
|
|
7
16
|
| 会话与流式对话 | Agent、对话型 | 浏览器、端侧宿主 | [对话界面](#对话界面) |
|
|
8
17
|
| 会话产出物 | Agent、对话型 | 浏览器、端侧宿主 | [产出物](#产出物) |
|
|
9
18
|
| 业务插件、本地文件 | Agent | 浏览器、端侧宿主 | [Agent 插件](#agent-插件) |
|
|
10
|
-
|
|
|
11
|
-
| 本地 Python(`localPython` / `run_python`) | Agent | **优先端侧宿主**;浏览器可用 Pyodide | [命令和脚本](#命令和脚本) |
|
|
12
|
-
|
|
13
|
-
浏览器指现代浏览器(命令运行时另需能加载公司 OBS 上的 just-bash / Pyodide)。端侧宿主是内嵌此页面、负责读写本地工程并启动 `@glodon-aiot/workspace-sandbox` 的桌面程序,见[宿主通道](#宿主通道)。
|
|
19
|
+
| 命令与脚本 | Agent | 浏览器运行时或端侧宿主沙箱 | [命令和脚本](#命令和脚本) |
|
|
14
20
|
|
|
15
|
-
|
|
21
|
+
**术语:** 浏览器 = 现代浏览器(命令另需能加载公司 OBS 上的 just-bash / Pyodide)。端侧宿主 = 内嵌此页、选定本地工程并管理 `@glodon-aiot/workspace-sandbox` 的桌面程序;文件和命令由页面**直连**沙箱,不经宿主转发。
|
|
16
22
|
|
|
17
|
-
##
|
|
23
|
+
## 快速开始
|
|
18
24
|
|
|
19
25
|
```bash
|
|
20
26
|
yarn add @glodon-aiot/bot-client-sdk
|
|
21
|
-
# 或
|
|
22
|
-
npm install @glodon-aiot/bot-client-sdk
|
|
27
|
+
# 或 npm install @glodon-aiot/bot-client-sdk
|
|
23
28
|
```
|
|
24
29
|
|
|
25
30
|
```ts
|
|
26
31
|
import { BotClient } from '@glodon-aiot/bot-client-sdk';
|
|
32
|
+
|
|
33
|
+
const client = new BotClient(token, { env: 'dev', stream: true });
|
|
34
|
+
|
|
35
|
+
client.addEventListener('error', console.error);
|
|
36
|
+
client.addEventListener('ready', async () => {
|
|
37
|
+
const session = await client.loadSession(''); // 空字符串 = 新建
|
|
38
|
+
session.addEventListener('message:content', () => render(session.messages));
|
|
39
|
+
session.addEventListener('answer:finish', () => render(session.messages));
|
|
40
|
+
await session.send({ text: '你好' }, { stream: true });
|
|
41
|
+
});
|
|
27
42
|
```
|
|
28
43
|
|
|
29
|
-
|
|
44
|
+
`token` 为带应用 ID 的 AIoT 资源令牌。类型、工作区、工具 Schema、错误码均从包入口导出。需要本地文件时再开 `builtinPlugins.localFiles`,见下文。
|
|
30
45
|
|
|
31
46
|
## 创建客户端
|
|
32
47
|
|
|
33
48
|
```ts
|
|
34
49
|
const client = new BotClient(token, {
|
|
35
50
|
env: 'dev', // 'dev' | 'test' | 'prod' | 'local',决定默认 API 地址
|
|
36
|
-
// apiRoot: 'https://example.com/api/cvforce', // 传入后覆盖 env
|
|
37
|
-
stream: true, // 省略时 Agent
|
|
51
|
+
// apiRoot: 'https://example.com/api/cvforce', // 传入后覆盖 env
|
|
52
|
+
stream: true, // 省略时 Agent / 对话型默认流式
|
|
38
53
|
// plugins、builtinPlugins 见「Agent 插件」
|
|
39
54
|
});
|
|
40
55
|
|
|
41
56
|
client.addEventListener('error', (error) => {
|
|
42
|
-
//
|
|
57
|
+
// 配置问题:插件编码冲突、backend 写错等
|
|
43
58
|
console.error(error);
|
|
44
59
|
});
|
|
45
60
|
|
|
46
61
|
client.addEventListener('ready', () => {
|
|
47
|
-
//
|
|
62
|
+
// 应用信息已加载;loadSession / sessions / application 需等此事件
|
|
48
63
|
});
|
|
49
64
|
```
|
|
50
65
|
|
|
51
|
-
|
|
66
|
+
应用类型由平台应用决定,`loadSession` 按类型创建对应会话。
|
|
52
67
|
|
|
53
68
|
## 对话界面
|
|
54
69
|
|
|
55
|
-
|
|
70
|
+
界面自管渲染状态,数据以 `session.messages` 为准。事件都带当前消息或列表,收到后重绘即可。
|
|
56
71
|
|
|
57
72
|
```ts
|
|
58
|
-
const session = await client.loadSession(sessionId); //
|
|
73
|
+
const session = await client.loadSession(sessionId); // 空字符串 = 新建
|
|
59
74
|
|
|
60
75
|
session.addEventListener('history:loaded', () => render(session.messages));
|
|
61
76
|
session.addEventListener('message:new', () => render(session.messages));
|
|
@@ -63,20 +78,19 @@ session.addEventListener('message:updated', () => render(session.messages));
|
|
|
63
78
|
session.addEventListener('message:content', () => render(session.messages)); // 流式增量
|
|
64
79
|
session.addEventListener('answer:finish', () => render(session.messages));
|
|
65
80
|
session.addEventListener('message:error', (error) => showError(error));
|
|
66
|
-
session.addEventListener('message:action', () => render(session.messages)); //
|
|
81
|
+
session.addEventListener('message:action', () => render(session.messages)); // 工具步骤
|
|
67
82
|
|
|
68
|
-
await session.fetchMessages();
|
|
83
|
+
await session.fetchMessages();
|
|
69
84
|
await session.send({ text: '你好' }, { stream: true });
|
|
70
85
|
|
|
71
|
-
//
|
|
72
|
-
session.suspend();
|
|
86
|
+
session.suspend(); // 停止回答;进行中的客户端插件会取消,确认弹窗应关闭
|
|
73
87
|
```
|
|
74
88
|
|
|
75
89
|
会话列表:
|
|
76
90
|
|
|
77
91
|
```ts
|
|
78
92
|
const sessions = await client.getSessions();
|
|
79
|
-
const current = await client.loadSession(id); //
|
|
93
|
+
const current = await client.loadSession(id); // 同时设为活跃会话
|
|
80
94
|
await client.removeSession(id, true);
|
|
81
95
|
```
|
|
82
96
|
|
|
@@ -85,22 +99,21 @@ await client.removeSession(id, true);
|
|
|
85
99
|
| `history:loaded` | 用 `session.messages` 渲染历史 |
|
|
86
100
|
| `message:new` / `message:updated` | 重绘消息列表 |
|
|
87
101
|
| `message:content` | 流式更新当前回答 |
|
|
88
|
-
| `message:action` |
|
|
89
|
-
| `answer:finish` |
|
|
102
|
+
| `message:action` | 刷新工具步骤,见[展示工具步骤](#展示工具步骤) |
|
|
103
|
+
| `answer:finish` | 结束「正在回答」,允许再发送 |
|
|
90
104
|
| `message:error` / `error` | 展示错误 |
|
|
91
105
|
|
|
92
|
-
发送开始后 `session.frozen === true`,此时 `send`
|
|
106
|
+
发送开始后 `session.frozen === true`,此时 `send` 会失败;输入框保持禁用直到 `answer:finish`。
|
|
93
107
|
|
|
94
108
|
## 产出物
|
|
95
109
|
|
|
96
|
-
|
|
110
|
+
任务结束后可拉取本次会话留下的文件(会话级,不是单条消息附件)。无产出物返回空数组,不抛错。
|
|
97
111
|
|
|
98
112
|
```ts
|
|
99
113
|
import type { ISessionArtifact } from '@glodon-aiot/bot-client-sdk';
|
|
100
114
|
|
|
101
115
|
const artifacts: ISessionArtifact[] = await session.getArtifacts();
|
|
102
|
-
|
|
103
|
-
const same = await client.getArtifacts(session.id);
|
|
116
|
+
const same = await client.getArtifacts(session.id); // 省略 id 时用活跃会话
|
|
104
117
|
```
|
|
105
118
|
|
|
106
119
|
| 字段 | 含义 |
|
|
@@ -109,20 +122,20 @@ const same = await client.getArtifacts(session.id);
|
|
|
109
122
|
| `size` | 字节数 |
|
|
110
123
|
| `url` | 可访问地址 |
|
|
111
124
|
|
|
112
|
-
`Session.getArtifacts()
|
|
125
|
+
- `Session.getArtifacts()`:用该会话自己的应用 id / 会话 id,缺失则抛错
|
|
126
|
+
- `BotClient.getArtifacts(sessionId?)`:需等 `ready`;无 `sessionId` 且无活跃会话时抛错
|
|
127
|
+
- 已加载会话优先走 `Session.getArtifacts()`,否则直请求平台
|
|
113
128
|
|
|
114
|
-
一般在任务成功结束(`index === -1` 且 `code === 200
|
|
129
|
+
一般在任务成功结束(`index === -1` 且 `code === 200`)和用户中止(`index === -2`)后调用。
|
|
115
130
|
|
|
116
131
|
## Agent 插件
|
|
117
132
|
|
|
118
|
-
Agent
|
|
119
|
-
|
|
120
|
-
插件分两类,都在创建客户端时配置:
|
|
133
|
+
Agent 应用中,平台标记为**客户端执行**的插件函数由 SDK 在页面调用,结果回传平台。对话型应用不调用客户端插件。
|
|
121
134
|
|
|
122
135
|
| 配置 | 内容 | `pluginCode` |
|
|
123
136
|
| --- | --- | --- |
|
|
124
|
-
| `plugins` |
|
|
125
|
-
| `builtinPlugins` | SDK
|
|
137
|
+
| `plugins` | 接入方业务插件 | 平台分配,作为 key |
|
|
138
|
+
| `builtinPlugins` | SDK 内置工作区 / 命令 / 技能工具 | 固定 `builtin_tools`,不用传 |
|
|
126
139
|
|
|
127
140
|
```ts
|
|
128
141
|
const client = new BotClient(token, {
|
|
@@ -137,32 +150,31 @@ const client = new BotClient(token, {
|
|
|
137
150
|
},
|
|
138
151
|
builtinPlugins: {
|
|
139
152
|
localFiles: { enabled: true },
|
|
140
|
-
localPython: { enabled: true },
|
|
141
153
|
},
|
|
142
154
|
});
|
|
143
155
|
```
|
|
144
156
|
|
|
145
157
|
### 业务插件
|
|
146
158
|
|
|
147
|
-
`plugins` 的 key
|
|
159
|
+
`plugins` 的 key 是平台 `pluginCode`,值是 `functionCode` → 函数。调用签名 `(params, callback, context)`:
|
|
148
160
|
|
|
149
161
|
| 参数 | 含义 |
|
|
150
162
|
| --- | --- |
|
|
151
|
-
| `params` |
|
|
152
|
-
| `callback` |
|
|
153
|
-
| `context` | `requestId`、`applicationId`、`sessionId`、`pluginCode`、`functionCode`、`signal
|
|
163
|
+
| `params` | 模型参数,已解析为对象 |
|
|
164
|
+
| `callback` | 回传 `{ code, message, data? }`;`message` 给模型;只认第一次调用 |
|
|
165
|
+
| `context` | `requestId`、`applicationId`、`sessionId`、`pluginCode`、`functionCode`、`signal`(`ToolContext`) |
|
|
154
166
|
|
|
155
|
-
停止回答或会话失活时 `signal`
|
|
167
|
+
停止回答或会话失活时 `signal` aborted,之后的 `callback` 不再回传。函数抛错 / Promise 拒绝 → 500;未注册插件或函数 → 404。
|
|
156
168
|
|
|
157
169
|
### 内置插件
|
|
158
170
|
|
|
159
|
-
`localFiles`
|
|
171
|
+
`localFiles` 启用后,工具注册到 `pluginCode` `builtin_tools`。`client.localWorkspace` 仅在 `localFiles.enabled === true` 时存在。
|
|
160
172
|
|
|
161
|
-
`shell` / `run_file` / `run_skill_script`
|
|
173
|
+
`shell` / `run_file` / `run_skill_script` 的执行器选择见[命令和脚本](#命令和脚本):注入的 `commandRunner` → 已就绪的协议 1 沙箱 → 无宿主时的 just-bash / Pyodide(默认开)→ 否则 501。**宿主工作区已授权但沙箱未就绪时直接失败,不降级浏览器运行时。**
|
|
162
174
|
|
|
163
|
-
业务 `plugins`
|
|
164
|
-
|
|
165
|
-
|
|
175
|
+
- 业务 `plugins` 已有 `builtin_tools` key → 内置工具不注册,`error` 事件带原因
|
|
176
|
+
- 同一页面只支持一个启用了内置插件的 `BotClient`(插件表全局)
|
|
177
|
+
- 平台注册时 `functionCode` 不可改名;参数定义见 `BUILTIN_TOOL_SCHEMAS`
|
|
166
178
|
|
|
167
179
|
| functionCode | 名称 | 运行环境 |
|
|
168
180
|
| --- | --- | --- |
|
|
@@ -170,46 +182,46 @@ const client = new BotClient(token, {
|
|
|
170
182
|
| `read_file` | 读取文件 | 浏览器、端侧宿主 |
|
|
171
183
|
| `write_file` | 写入文件 | 浏览器、端侧宿主 |
|
|
172
184
|
| `append_file` | 追加文件 | 浏览器、端侧宿主 |
|
|
173
|
-
| `run_file` | 执行文件 |
|
|
185
|
+
| `run_file` | 执行文件 | 浏览器(`.py` / `.sh` / `.bash`)或宿主(另含 `.js` / `.mjs` / `.cjs`) |
|
|
174
186
|
| `edit_file` | 修改文件 | 浏览器、端侧宿主 |
|
|
175
187
|
| `grep` | 搜索文件 | 浏览器、端侧宿主 |
|
|
176
188
|
| `find` | 查找文件 | 浏览器、端侧宿主 |
|
|
177
189
|
| `load_skill` | 加载技能 | 浏览器、端侧宿主 |
|
|
178
190
|
| `read_skill_resource` | 读取技能资源 | 浏览器、端侧宿主 |
|
|
179
|
-
| `
|
|
191
|
+
| `list_skill_files` | 列出技能文件 | 端侧宿主 |
|
|
192
|
+
| `grep_skill` | 搜索技能内容 | 端侧宿主 |
|
|
193
|
+
| `run_skill_script` | 运行技能脚本 | 同 `run_file`(无沙箱时 501,不降级) |
|
|
180
194
|
|
|
181
195
|
### 展示工具步骤
|
|
182
196
|
|
|
183
|
-
`message:action`
|
|
197
|
+
`message:action` 出现内置工具步骤时,用已注册插件编码和 `BUILTIN_TOOL_SCHEMAS` 换成中文名;只展示相对路径和参数,**不展示工程绝对路径**:
|
|
184
198
|
|
|
185
199
|
```ts
|
|
186
200
|
import {
|
|
187
201
|
BUILTIN_TOOL_SCHEMAS,
|
|
188
202
|
getRegisteredLocalFilesPluginCode,
|
|
189
|
-
getRegisteredLocalPythonPluginCode,
|
|
190
203
|
} from '@glodon-aiot/bot-client-sdk';
|
|
191
204
|
|
|
192
|
-
const
|
|
193
|
-
const schema = pluginCodes.includes(pluginCode)
|
|
205
|
+
const schema = pluginCode === getRegisteredLocalFilesPluginCode()
|
|
194
206
|
? BUILTIN_TOOL_SCHEMAS[funcCode as keyof typeof BUILTIN_TOOL_SCHEMAS]
|
|
195
207
|
: undefined;
|
|
196
|
-
// schema
|
|
208
|
+
// schema 存在时展示 schema.name 与参数里的 path / destPath
|
|
197
209
|
```
|
|
198
210
|
|
|
199
|
-
|
|
211
|
+
业务插件步骤按接入方自己的规则展示。
|
|
200
212
|
|
|
201
213
|
## 本地工作文件夹
|
|
202
214
|
|
|
203
|
-
内置插件 `localFiles
|
|
215
|
+
内置插件 `localFiles`:助手只能访问一个文件夹——网页由用户选择,或由宿主指定。
|
|
204
216
|
|
|
205
|
-
|
|
217
|
+
### 路径约定
|
|
206
218
|
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
- `/dev`、`/usr`、`/bin`
|
|
219
|
+
- 工具参数:工作区内以 `/` 开头的绝对路径,或相对工作区根的相对路径(如 `/说明.md`、`notes/a.md`)
|
|
220
|
+
- **不要**约定 `input` / `output` / `uploads` / `tmp` 作为根目录
|
|
221
|
+
- 平台技能不在工程里;`load_skill` 等读端侧缓存;`read_file` / `shell` 等拒绝第一段为 `skills` 的路径
|
|
222
|
+
- `/dev`、`/usr`、`/bin` 仍是系统路径;脚本内请用相对路径
|
|
211
223
|
|
|
212
|
-
|
|
224
|
+
网页选文件夹依赖 File System Access API,仅 Chromium(Chrome / Edge / WebView2)的安全上下文(https 或 localhost)。宿主自管工程目录时不弹选择框,见[宿主通道](#宿主通道)。
|
|
213
225
|
|
|
214
226
|
### 开启
|
|
215
227
|
|
|
@@ -219,18 +231,14 @@ const client = new BotClient(token, {
|
|
|
219
231
|
builtinPlugins: {
|
|
220
232
|
localFiles: {
|
|
221
233
|
enabled: true,
|
|
222
|
-
// 'file-system-access'
|
|
223
|
-
// 'host-bridge'
|
|
234
|
+
// 'file-system-access'(默认):弹目录选择框
|
|
235
|
+
// 'host-bridge':目录由宿主推送
|
|
224
236
|
backend: 'file-system-access',
|
|
225
237
|
onApprovalRequired: async (request) => openApprovalDialog(request),
|
|
226
|
-
//
|
|
227
|
-
sensitiveRulesAppend: [],
|
|
238
|
+
sensitiveRulesAppend: [], // 只能追加,不能放宽基线
|
|
228
239
|
downloadUrlAllowlistAppend: [],
|
|
229
|
-
//
|
|
230
|
-
limits: { maxReadBytes: 512 * 1024 },
|
|
231
|
-
// 为 true 时 search_text 可以不指定子目录、搜整个工作区。默认 false
|
|
240
|
+
limits: { maxReadBytes: 512 * 1024 }, // 只能调小
|
|
232
241
|
allowWorkspaceWideTextSearch: false,
|
|
233
|
-
// 无宿主沙箱时,是否在页面里跑 shell / python(默认 true)
|
|
234
242
|
commandRuntime: {
|
|
235
243
|
browser: {
|
|
236
244
|
enabled: true,
|
|
@@ -244,67 +252,57 @@ const client = new BotClient(token, {
|
|
|
244
252
|
const workspace = client.localWorkspace;
|
|
245
253
|
```
|
|
246
254
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
### 十一个工具
|
|
250
|
-
|
|
251
|
-
路径见上文约定。`..`、反斜杠、越出工作区的路径返回 403。
|
|
252
|
-
|
|
253
|
-
**`read_file`** 读取 UTF-8 文本。`start_line` / `end_line` 从 1 计、含端点。不超过 81920 字节时按原文返回。超出后,JSON、XML、SVG、CSV、TSV 返回结构概览和短预览,`truncated` 为 true;指定了行范围则返回该片段。PDF 和图片只返回摘要,不返回正文。
|
|
254
|
-
|
|
255
|
-
**`write_file`** 覆盖写入。`content` 按 UTF-8 计不超过 81920 字节,超出返回 413。父目录会自动创建。需确认。
|
|
256
|
-
|
|
257
|
-
**`append_file`** 把文本接到文件末尾,文件不存在时创建。追加本身不设字节上限。
|
|
258
|
-
|
|
259
|
-
**`edit_file`** 把文件中第一处与 `old_text` 完全相同的内容换成 `new_text`。找不到时,`message` 里带最接近片段的起始行号和原文。
|
|
260
|
-
|
|
261
|
-
**`grep`** 在目录中递归搜索,`pattern` 按 grep 基本正则。`data.matches` 是带路径和行号的文本。
|
|
255
|
+
说明:
|
|
262
256
|
|
|
263
|
-
|
|
257
|
+
- `transfer` 仍可供旧上传下载实现;当前内置工具不走这套
|
|
258
|
+
- 宿主沙箱可用条件:`workspace.sandbox.state === "ready"` 且 `protocol === 1`(不在 `commandRuntime` 里配)
|
|
259
|
+
- `capabilities` 仅兼容旧 workspace 消息
|
|
264
260
|
|
|
265
|
-
|
|
261
|
+
### 内置工具参考
|
|
266
262
|
|
|
267
|
-
|
|
263
|
+
路径见上文。`..`、反斜杠、越出工作区 → 403。
|
|
268
264
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
265
|
+
| 工具 | 要点 |
|
|
266
|
+
| --- | --- |
|
|
267
|
+
| `read_file` | UTF-8;`start_line` / `end_line` 从 1 计。≤81920 字节原文;超出则结构化预览(`truncated`)。PDF / 图片只返回摘要 |
|
|
268
|
+
| `write_file` | 覆盖写;`content` ≤81920 字节(超则 413);自动建父目录;需确认 |
|
|
269
|
+
| `append_file` | 追加;文件不存在则创建;追加本身无字节上限 |
|
|
270
|
+
| `edit_file` | 替换第一处完全匹配的 `old_text`;找不到时 `message` 带最近片段行号 |
|
|
271
|
+
| `grep` | 递归搜索,基本正则;`data.matches` 含路径与行号 |
|
|
272
|
+
| `find` | 文件名通配(同 `find -name`);`data.files` 一行一个路径 |
|
|
273
|
+
| `shell` | 每次新 shell,只保留上次 cwd;≤20000 字;退出码在 `data.exitCode`;联网由宿主策略决定 |
|
|
274
|
+
| `run_file` | 浏览器:`.py` / `.sh` / `.bash`;`.js*` 不支持。宿主:另支持 `.js` / `.mjs` / `.cjs` |
|
|
275
|
+
| `load_skill` | 读已发布版 `SKILL.md` + 文件清单;技能在工程外缓存 |
|
|
276
|
+
| `read_skill_resource` | 技能包内相对路径;目录返回清单;`grep:正则` 搜全文 |
|
|
277
|
+
| `list_skill_files` | 列技能包或子目录 |
|
|
278
|
+
| `grep_skill` | 技能包内基本正则搜索 |
|
|
279
|
+
| `run_skill_script` | 仅宿主沙箱;`args` 为位置参数;无沙箱 → 501,不降级 |
|
|
274
280
|
|
|
275
281
|
### 浏览器里授权
|
|
276
282
|
|
|
277
|
-
`backend
|
|
283
|
+
`backend: 'file-system-access'` 时,构造会自己 `restore()`。订阅 `workspace:changed` 画按钮。**`pick()` / `requestPermission()` 必须在点击回调里同步调用**,中间不能有 `await`。
|
|
278
284
|
|
|
279
285
|
```ts
|
|
280
286
|
workspace.on('workspace:changed', ({ status, displayName }) => {
|
|
281
287
|
renderFolder(status, displayName);
|
|
282
288
|
});
|
|
283
289
|
|
|
284
|
-
selectButton.onclick = () => {
|
|
285
|
-
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
restoreButton.onclick = () => {
|
|
289
|
-
workspace.requestPermission().catch(console.error);
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
closeButton.onclick = () => {
|
|
293
|
-
workspace.clear().catch(console.error);
|
|
294
|
-
};
|
|
290
|
+
selectButton.onclick = () => { workspace.pick().catch(console.error); };
|
|
291
|
+
restoreButton.onclick = () => { workspace.requestPermission().catch(console.error); };
|
|
292
|
+
closeButton.onclick = () => { workspace.clear().catch(console.error); };
|
|
295
293
|
```
|
|
296
294
|
|
|
297
295
|
| status | 含义 | 按钮 |
|
|
298
296
|
| --- | --- | --- |
|
|
299
|
-
| `unsupported` |
|
|
300
|
-
| `none` |
|
|
301
|
-
| `prompt` |
|
|
302
|
-
| `granted` |
|
|
303
|
-
| `denied` |
|
|
297
|
+
| `unsupported` | 无目录选择能力 | 提示不可用 |
|
|
298
|
+
| `none` | 未选过 | 「选择工作文件夹」→ `pick()` |
|
|
299
|
+
| `prompt` | 有上次文件夹,需重新授权 | 「恢复访问」→ `requestPermission()` |
|
|
300
|
+
| `granted` | 可用;`displayName` 为文件夹名 | 「更换」→ `pick()`,「关闭」→ `clear()` |
|
|
301
|
+
| `denied` | 用户拒绝 | 「选择工作文件夹」→ `pick()` |
|
|
304
302
|
|
|
305
|
-
`pick()`
|
|
303
|
+
`pick()` 取消时返回 `false`,状态不变。`displayName` 只有文件夹名,无绝对路径。
|
|
306
304
|
|
|
307
|
-
|
|
305
|
+
首次选择前展示外发说明(助手会读该文件夹,内容可能发给模型;改 / 传 / 下前再确认)。SDK 只记「已看过」,文案由界面定:
|
|
308
306
|
|
|
309
307
|
```ts
|
|
310
308
|
selectButton.onclick = () => {
|
|
@@ -314,8 +312,7 @@ selectButton.onclick = () => {
|
|
|
314
312
|
}
|
|
315
313
|
openDisclosure({
|
|
316
314
|
onContinue: () => {
|
|
317
|
-
//
|
|
318
|
-
workspace.pick().catch(console.error);
|
|
315
|
+
workspace.pick().catch(console.error); // 仍须在本次点击里同步调用
|
|
319
316
|
disclosureAcked = true;
|
|
320
317
|
void workspace.acknowledgeDisclosure();
|
|
321
318
|
},
|
|
@@ -325,57 +322,70 @@ selectButton.onclick = () => {
|
|
|
325
322
|
const disclosureAcked = await workspace.isDisclosureAcknowledged();
|
|
326
323
|
```
|
|
327
324
|
|
|
328
|
-
|
|
325
|
+
不要先 `await acknowledgeDisclosure()` 再 `pick()`。`acknowledgeDisclosure` 按 `applicationId` 记忆。
|
|
329
326
|
|
|
330
|
-
###
|
|
327
|
+
### 读和写文件
|
|
331
328
|
|
|
332
|
-
|
|
329
|
+
`status === 'granted'` 后用 `workspace.files`。路径如 `notes/a.md` 或 `/notes/a.md`;根用 `''` 或省略。`..`、盘符、空字符会失败。
|
|
333
330
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (isNotAllowed(error)) void workspace.handlePermissionLost();
|
|
350
|
-
throw error;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
331
|
+
| 方法 | 作用 |
|
|
332
|
+
| --- | --- |
|
|
333
|
+
| `list(path?)` | 只列一层 |
|
|
334
|
+
| `stat(path?)` | 文件或目录 |
|
|
335
|
+
| `read(path)` | 整个文件 → `Blob` |
|
|
336
|
+
| `readText(path)` | UTF-8 文本 |
|
|
337
|
+
| `url(path)` | 给 `<img>` / `<video>` / `<audio>`;浏览器自拉字节 |
|
|
338
|
+
| `releaseUrl(url)` | 不再显示时调用;引用归零后作废 |
|
|
339
|
+
| `write(path, source)` | 覆盖写(字符串 / `Uint8Array` / 字节流);自动建父目录 |
|
|
340
|
+
| `append(path, source)` | 追加;不存在则创建 |
|
|
341
|
+
| `mkdir(path)` | 逐级创建;已存在则 noop |
|
|
342
|
+
| `remove(path, options?)` | 删文件或目录;非空目录需 `{ recursive: true }` |
|
|
343
|
+
| `move(from, to)` | 移动 / 重命名;`to` 为完整新路径,父目录须已存在 |
|
|
344
|
+
|
|
345
|
+
`list` / `stat` 每项字段:`name`、`path`(相对根,根为 `''`)、`kind`、`size?`、`lastModified?`、`sensitive`。
|
|
353
346
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
347
|
+
```ts
|
|
348
|
+
const entries = await workspace.files.list('notes');
|
|
349
|
+
const text = await workspace.files.readText('notes/a.md');
|
|
350
|
+
await workspace.files.write('notes/a.md', `${text}\n`);
|
|
351
|
+
await workspace.files.move('notes/a.md', 'notes/b.md');
|
|
357
352
|
```
|
|
358
353
|
|
|
359
|
-
|
|
354
|
+
失败抛 `ToolError`,用 `code` 与 `data.reason` 区分;文案无本机绝对路径。
|
|
355
|
+
|
|
356
|
+
| `data.reason` | 何时 |
|
|
357
|
+
| --- | --- |
|
|
358
|
+
| `workspace_not_granted` | 未授权(失效时 `status` 先离开 `granted`) |
|
|
359
|
+
| `path_outside_workspace` | 越界 |
|
|
360
|
+
| `sensitive_file` | 读改敏感文件或为其签发地址;`list`/`stat` 仍返回且 `sensitive: true` |
|
|
361
|
+
|
|
362
|
+
内存快照工作区(`attachFileSnapshot`)可读写列目录;`mkdir` / `remove` / `move` → 501,且不回写磁盘。
|
|
363
|
+
|
|
364
|
+
`write` / `append` / `mkdir` / `remove` / `move` 成功后发 `files:changed`。
|
|
365
|
+
|
|
366
|
+
### 目录树
|
|
367
|
+
|
|
368
|
+
展开哪层就列哪层。文件变化时 SDK 约每 150ms 合并一次 `files:changed`:
|
|
360
369
|
|
|
361
370
|
| 来源 | `paths` |
|
|
362
371
|
| --- | --- |
|
|
363
|
-
|
|
|
364
|
-
|
|
|
365
|
-
| `
|
|
372
|
+
| 网页 `FileSystemObserver` | 变化目录;根为 `''` |
|
|
373
|
+
| 宿主 `fs-changed` | 宿主给出的相对目录 |
|
|
374
|
+
| `shell` / `run_file` / `run_skill_script` 结束 | `[]` = 重读根 + 已展开各层 |
|
|
375
|
+
| `files` 写入 / 建目录 / 删除 / 移动 | 被改项的父目录 |
|
|
366
376
|
|
|
367
377
|
```ts
|
|
368
378
|
const expanded = new Set<string>();
|
|
369
379
|
|
|
380
|
+
async function loadDirectory(dir = '') {
|
|
381
|
+
return workspace.files.list(dir);
|
|
382
|
+
}
|
|
383
|
+
|
|
370
384
|
workspace.on('files:changed', (event) => {
|
|
371
385
|
const dirs = event.paths.length ? event.paths : ['', ...expanded];
|
|
372
386
|
dirs.forEach((dir) => void loadDirectory(dir));
|
|
373
387
|
});
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
`apply_patch`、`write_file`、`download_file` 成功时还会发 `tool:finish`。没有 `FileSystemObserver` 的环境靠它刷新被改文件的父目录。窗口重新获得焦点时,再重读根目录和已展开目录,用来补上观察器没有覆盖到的改动(例如在资源管理器里改了 `.git` 或 `node_modules` 里面的文件;宿主默认不监视这三个目录)。
|
|
377
388
|
|
|
378
|
-
```ts
|
|
379
389
|
window.addEventListener('focus', () => {
|
|
380
390
|
if (document.visibilityState === 'hidden') return;
|
|
381
391
|
['', ...expanded].forEach((dir) => void loadDirectory(dir));
|
|
@@ -389,7 +399,19 @@ workspace.on('tool:finish', (event) => {
|
|
|
389
399
|
});
|
|
390
400
|
```
|
|
391
401
|
|
|
392
|
-
|
|
402
|
+
目录树只展示名字与层级。打开文本用 `read` / `readText`;媒体与 PDF 用 `url`,离开时 `releaseUrl`。**不要** `read` 再转地址(占内存)。地址只给界面,不要写进发给模型的内容。
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
const url = await workspace.files.url('shots/a.png');
|
|
406
|
+
workspace.files.releaseUrl(url);
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
| 后端 | 地址形态 |
|
|
410
|
+
| --- | --- |
|
|
411
|
+
| 沙箱 | `http://127.0.0.1:<端口>/file/<票据>`;每次读磁盘当前内容;30 分钟无读过期 |
|
|
412
|
+
| 网页文件夹 | `blob:`;内容停在取址那一刻,文件改了需再 `url` |
|
|
413
|
+
|
|
414
|
+
仅图片 / 音视频 / PDF;其他类型失败。
|
|
393
415
|
|
|
394
416
|
### 确认弹窗
|
|
395
417
|
|
|
@@ -400,10 +422,10 @@ workspace.on('tool:finish', (event) => {
|
|
|
400
422
|
| `functionCode` | `apply_patch` / `write_file` / `upload_file` / `download_file` |
|
|
401
423
|
| `path` | 工作区相对路径 |
|
|
402
424
|
| `workspaceName` | 文件夹名 |
|
|
403
|
-
| `preview` |
|
|
404
|
-
| `signal` |
|
|
425
|
+
| `preview` | 给用户看的内容 |
|
|
426
|
+
| `signal` | 停止回答、切会话、换/关文件夹、确认超时(默认 5 分钟)时 abort |
|
|
405
427
|
|
|
406
|
-
`
|
|
428
|
+
仅严格 `true` 继续;`false` / 抛错 / 超时均拒绝,原文件不变。`signal` abort 时关弹窗并 `resolve(false)`。
|
|
407
429
|
|
|
408
430
|
```ts
|
|
409
431
|
import type { ApprovalRequest } from '@glodon-aiot/bot-client-sdk';
|
|
@@ -423,95 +445,89 @@ function openApprovalDialog(request: ApprovalRequest): Promise<boolean> {
|
|
|
423
445
|
}
|
|
424
446
|
```
|
|
425
447
|
|
|
426
|
-
`preview.type` 决定弹窗内容:
|
|
427
|
-
|
|
428
448
|
| preview | 展示 |
|
|
429
449
|
| --- | --- |
|
|
430
|
-
| `{ type: 'text', created: true, after }` |
|
|
431
|
-
| `{ type: 'text', created: false, before, after }` |
|
|
432
|
-
| `{ type: 'upload', name, size, target: 'session' }` |
|
|
433
|
-
| `{ type: 'download', source, name?, size?, overwrite }` |
|
|
434
|
-
|
|
435
|
-
`tool:approval-required` 只有 `requestId`、`functionCode`、`path`、`approvalId`,不含文件内容。预览文本只出现在 `request.preview` 里。
|
|
450
|
+
| `{ type: 'text', created: true, after }` | 新建,展示 `after` |
|
|
451
|
+
| `{ type: 'text', created: false, before, after }` | 修改,按行对比 |
|
|
452
|
+
| `{ type: 'upload', name, size, target: 'session' }` | 文件名、大小、路径,目标为当前会话 |
|
|
453
|
+
| `{ type: 'download', source, name?, size?, overwrite }` | 来源标签、文件名、大小、保存路径、是否覆盖 |
|
|
436
454
|
|
|
437
|
-
|
|
455
|
+
`tool:approval-required` 只有 id / 函数码 / 路径 / `approvalId`,不含文件内容。界面若晚于 `BotClient` 挂载:挂载前回调直接 `false`,挂载后再 `setApprovalHandler`。
|
|
438
456
|
|
|
439
457
|
### 进度与未授权
|
|
440
458
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
工作区事件一览(载荷里没有文件内容):
|
|
459
|
+
- 上传下载:订 `tool:progress`(`stage`:`reading` \| `uploading` \| `downloading` \| `writing` \| `done`)
|
|
460
|
+
- `done` / `tool:finish` / `tool:error` / `tool:cancelled` 时清掉该条进度
|
|
461
|
+
- 未授权工具调用 → `tool:error`,`reason === 'workspace_not_granted'`;亮起选择/恢复按钮;`granted` 后关掉;**不要**自动重放失败调用
|
|
446
462
|
|
|
447
463
|
| 事件 | 载荷要点 |
|
|
448
464
|
| --- | --- |
|
|
449
465
|
| `workspace:changed` | `status`、`displayName` |
|
|
450
|
-
| `files:changed` | `paths
|
|
466
|
+
| `files:changed` | `paths`;空数组 = 根 + 已展开 |
|
|
451
467
|
| `tool:start` | `requestId`、`functionCode`、`path` |
|
|
452
|
-
| `tool:approval-required` |
|
|
453
|
-
| `tool:progress` |
|
|
454
|
-
| `tool:finish` |
|
|
455
|
-
| `tool:error` |
|
|
468
|
+
| `tool:approval-required` | 同上 + `approvalId` |
|
|
469
|
+
| `tool:progress` | 同上 + `stage`、`loaded`、`total` |
|
|
470
|
+
| `tool:finish` | 同上 + `code` |
|
|
471
|
+
| `tool:error` | 同上 + `code`、`message`、`reason` |
|
|
456
472
|
| `tool:cancelled` | `requestId`、`functionCode`、`path` |
|
|
457
473
|
|
|
458
|
-
`workspace.on`
|
|
474
|
+
`workspace.on` 返回取消订阅函数。载荷不含文件内容。
|
|
459
475
|
|
|
460
476
|
## 命令和脚本
|
|
461
477
|
|
|
462
|
-
`shell
|
|
478
|
+
`shell` / `run_file` 共用执行器选择(`runnerFor`)。`run_skill_script` 只用注入执行器或已就绪宿主沙箱,**无沙箱不降级浏览器**。
|
|
463
479
|
|
|
464
|
-
|
|
480
|
+
成功时 `data`:`{ exitCode, stdout, stderr, cwd? }`。`exitCode !== 0` 仍算工具成功(`code === 0`)。跨命令只保留 cwd。浏览器运行时同一工作区一次一条;连上沙箱后并发由 `exec.maxConcurrent` 决定,超限工具 `code` 429。
|
|
465
481
|
|
|
466
|
-
|
|
467
|
-
2. **沙箱直连**:宿主工作区已授权,且 `sandbox.state` 为 `ready`、`protocol` 为 `2` → 页面对 `ws://127.0.0.1` 发 WebSocket,不经宿主转发文件和命令
|
|
468
|
-
3. **浏览器运行时**:没有宿主 WebView 时,`commandRuntime.browser.enabled` 默认为 `true` → just-bash + Pyodide Worker
|
|
469
|
-
4. 以上皆不可用 → **501**
|
|
482
|
+
### 执行器选择顺序
|
|
470
483
|
|
|
471
|
-
|
|
484
|
+
1. 注入的 `LocalWorkspace.commandRunner`(测试 / 自定义)
|
|
485
|
+
2. 沙箱直连:宿主已授权且 `sandbox.state === 'ready'`、`protocol === 1` → 页面连 `ws://127.0.0.1`
|
|
486
|
+
3. 浏览器运行时:无宿主 WebView 且 `commandRuntime.browser.enabled`(默认 true)→ just-bash + Pyodide
|
|
487
|
+
4. 否则 → **501**
|
|
472
488
|
|
|
473
|
-
|
|
489
|
+
宿主已授权但沙箱未就绪时**不**降级。`starting` = 启动中;失败看 `sandbox.message`。协议非 1 另发 `error`。收到 `bye` / `exited` / `failed` 后在途调用以 `AbortError` 失败且不重放;等新的 `ready` 再用新地址与令牌。
|
|
474
490
|
|
|
475
491
|
### 浏览器运行时
|
|
476
492
|
|
|
477
493
|
| 能力 | 行为 |
|
|
478
494
|
| --- | --- |
|
|
479
|
-
| shell |
|
|
480
|
-
| Python |
|
|
481
|
-
| Node / `.js` |
|
|
495
|
+
| shell | 按需加载 just-bash,读写已授权工作区 |
|
|
496
|
+
| Python | Pyodide Worker;白名单:numpy、pandas、matplotlib、pillow、lxml |
|
|
497
|
+
| Node / `.js` | 明确报错 |
|
|
482
498
|
| pip / 任意第三方 | 不支持 |
|
|
483
499
|
|
|
484
|
-
默认 CDN(路径带版本,长缓存;可用配置覆盖):
|
|
485
|
-
|
|
486
500
|
| 配置项 | 默认 |
|
|
487
501
|
| --- | --- |
|
|
488
502
|
| `commandRuntime.browser.bashModuleURL` | `https://cv-cdn.obs.cn-north-4.myhuaweicloud.com/glodon/libs/just-bash/3.6.0/just-bash.mjs` |
|
|
489
503
|
| `commandRuntime.browser.pyodideIndexURL` | `https://cv-cdn.obs.cn-north-4.myhuaweicloud.com/glodon/libs/pyodide/314.0.7/` |
|
|
490
504
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
浏览器运行时用于能力对齐与防误用,**不是**与宿主 MXC 同级的强隔离。
|
|
505
|
+
CSP:放行 OBS 的 `script-src` / `connect-src`;Pyodide 需 `worker-src blob:`。
|
|
494
506
|
|
|
495
|
-
|
|
507
|
+
浏览器运行时用于能力对齐与防误用,**不是**与宿主 MXC 同级的强隔离。just-bash / Pyodide **不打进** npm 包;请用 **ES 入口**。CJS + webpack 可能丢掉 `webpackIgnore` 导致错误解析动态 `import`。
|
|
496
508
|
|
|
497
509
|
### 宿主沙箱(摘要)
|
|
498
510
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
-
|
|
502
|
-
-
|
|
503
|
-
-
|
|
504
|
-
-
|
|
505
|
-
-
|
|
506
|
-
-
|
|
507
|
-
-
|
|
508
|
-
-
|
|
511
|
+
完整协议、策略 JSON、平台隔离差异见 npm 包 `@glodon-aiot/workspace-sandbox`。此处只列页面侧要点:
|
|
512
|
+
|
|
513
|
+
- 启动:`workspace-sandbox --listen --root <工程> --origin <页面来源>`;联网 / pip / 并发由 `--policy` 定,页面不能改
|
|
514
|
+
- 宿主职责:启停、读 ready、可选 status / policy、shutdown(3 秒后杀进程组)、把退出告诉页面;**不中转**文件与命令
|
|
515
|
+
- 页面用 `sandbox.url` / `sandbox.token` 直连 `ws://127.0.0.1`(无 `wss://`);CSP `connect-src` 放行 `ws://127.0.0.1:*`
|
|
516
|
+
- `auth` 与后续 `policy` 推送含摘要:`exec.maxConcurrent`、`network.mode` / `allow`、`packages.python.enabled`(不含上游代理与镜像)
|
|
517
|
+
- 崩溃推送 `sandbox.state: "exited"`;同一次打开最多自动重启一次
|
|
518
|
+
- 默认断网;`allowlist` 拒绝项写在 `stderr`;允许时 `pip install` 直装工程 venv,不必先改 `open`
|
|
519
|
+
- 平台技能:进会话按编排核对版本;页面用 `client.token` 下载 ZIP,经已认证 WS 调 `installSkill`(沙箱收不到令牌)。缓存在用户目录,不在工程内
|
|
520
|
+
- 分发:`Resources/node/`(Node 24)+ `Resources/workspace-sandbox/`;可选 `python/`
|
|
509
521
|
|
|
510
522
|
## 宿主通道
|
|
511
523
|
|
|
512
|
-
|
|
524
|
+
端侧宿主 = 内嵌此页的桌面程序(内核不限)。
|
|
525
|
+
|
|
526
|
+
`localFiles.backend === 'host-bridge'` 时,宿主在页面脚本前提供 `globalThis.__glodonHostBridge`(或 `registerHostBridge`):`postMessage` / `addEventListener('message')` / `removeEventListener('message')`。有一致的 `chrome.webview` 时可自动使用。
|
|
513
527
|
|
|
514
|
-
|
|
528
|
+
- 通道名固定 `glodon-workspace`
|
|
529
|
+
- 消息只有相对路径与文件夹名;绝对路径留在宿主
|
|
530
|
+
- 页面不再出现「选择工作文件夹」;宿主未推送时 `status` 为 `unsupported`
|
|
515
531
|
|
|
516
532
|
页面加载后先发:
|
|
517
533
|
|
|
@@ -519,7 +535,7 @@ function openApprovalDialog(request: ApprovalRequest): Promise<boolean> {
|
|
|
519
535
|
{ "channel": "glodon-workspace", "type": "hello" }
|
|
520
536
|
```
|
|
521
537
|
|
|
522
|
-
|
|
538
|
+
宿主立刻回当前工程,之后每次打开 / 关闭再推:
|
|
523
539
|
|
|
524
540
|
```json
|
|
525
541
|
{
|
|
@@ -533,41 +549,42 @@ function openApprovalDialog(request: ApprovalRequest): Promise<boolean> {
|
|
|
533
549
|
"state": "ready",
|
|
534
550
|
"url": "ws://127.0.0.1:53124",
|
|
535
551
|
"token": "<32 字节十六进制>",
|
|
536
|
-
"protocol":
|
|
552
|
+
"protocol": 1
|
|
537
553
|
}
|
|
538
554
|
}
|
|
539
555
|
```
|
|
540
556
|
|
|
541
|
-
`granted
|
|
557
|
+
- `granted: false` → 页面断开工作区
|
|
558
|
+
- `sandbox.state` 还可为 `starting` / `failed` / `exited`;`failed` 带固定 `message`,无令牌与绝对路径
|
|
559
|
+
- 是否直连只看 `sandbox.state`、`url`、`token`、`protocol`;`capabilities` / `sandboxVersion` 仅为兼容 / 诊断
|
|
560
|
+
- 页面**不**向宿主发文件或命令操作
|
|
542
561
|
|
|
543
|
-
|
|
562
|
+
目录增删时推送(`paths` 相对工程根;`""` = 根;空数组 = 重读已展开全部):
|
|
544
563
|
|
|
545
564
|
```json
|
|
546
565
|
{ "channel": "glodon-workspace", "type": "fs-changed", "paths": ["notes", ""] }
|
|
547
566
|
```
|
|
548
567
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
文件和命令不经过宿主。页面连上沙箱后发协议 2 的文本帧和二进制分片:`read` 按 `offset` / `maxBytes` 取窗口,`write` 按偏移写入临时文件再改名。占用时原文件不变。命令输出流式返回,SDK 每个流只保留最后 16000 字符;若有 `logPath`,截断说明里带上 `tmp/sandbox-exec/<id>.log`。
|
|
568
|
+
脚本跑完、用户在资源管理器改文件都应推送。Qt 示例默不监视 `.git` / `node_modules` / `__pycache__` 内部。
|
|
552
569
|
|
|
553
|
-
|
|
570
|
+
数据面:页面连沙箱后发协议 1 帧(细节见 `@glodon-aiot/workspace-sandbox`)。取消只发 `{ "op": "cancel", "id" }`,不关套接字。`bye` / `exited` / `failed` 后在途失败且不重放。
|
|
554
571
|
|
|
555
572
|
## 安全与限额
|
|
556
573
|
|
|
557
|
-
敏感文件不可读、不可写、不可上传、不可运行,也不出现在搜索结果里。`
|
|
574
|
+
敏感文件不可读、不可写、不可上传、不可运行,也不出现在搜索结果里。`list` / `stat` 仍返回并标 `sensitive: true`;真正读写 → 403,`reason: sensitive_file`,`data.ruleId` 为规则 ID。
|
|
558
575
|
|
|
559
|
-
|
|
576
|
+
基线 `SDK_SENSITIVE_BASELINE` 始终生效;`sensitiveRulesAppend` 只能再加:
|
|
560
577
|
|
|
561
578
|
| 匹配 | 规则 |
|
|
562
579
|
| --- | --- |
|
|
563
580
|
| 文件名 | `.env*`、`*.pem`、`*.key`、`*.p12`、`*.pfx`、`id_rsa*`、`id_ed25519*`、`id_ecdsa*`、`.credentials`、`.npmrc`、`.pypirc` |
|
|
564
581
|
| 整段相对路径 | `**/.aws/credentials`、`**/.ssh/**` |
|
|
565
582
|
|
|
566
|
-
|
|
583
|
+
追加规则:`pattern` 为 `/` 分隔 glob;`match` 省略比文件名,`path` 比整段路径;`operations` 可限 `read` / `write` / `search`;`source` 为 `product` 或 `enterprise`。
|
|
567
584
|
|
|
568
|
-
下载 URL
|
|
585
|
+
下载 URL 仅 https,且域名在允许列表或其子域(默认仅 `glodon.com`)。`downloadUrlAllowlistAppend` 追加后缀。拒 IP / localhost / 带账号密码的地址;重定向同样校验;不带 Cookie。
|
|
569
586
|
|
|
570
|
-
`limits`
|
|
587
|
+
`limits` 只能调小(`DEFAULT_LIMITS`):
|
|
571
588
|
|
|
572
589
|
| 项 | 默认 |
|
|
573
590
|
| --- | --- |
|
|
@@ -576,32 +593,33 @@ function openApprovalDialog(request: ApprovalRequest): Promise<boolean> {
|
|
|
576
593
|
| `maxTransferBytes` | 50 MB |
|
|
577
594
|
| `maxSearchResults` | 200 |
|
|
578
595
|
| `maxConcurrentRequests` | 4 |
|
|
579
|
-
| `toolTimeoutMs` | 30
|
|
596
|
+
| `toolTimeoutMs` | 30 秒(读写改文件) |
|
|
597
|
+
| `commandTimeoutMs` | 10 分钟(`shell` / `run_file` / `run_skill_script`,同时传给沙箱) |
|
|
580
598
|
| `searchTimeoutMs` | 60 秒 |
|
|
581
599
|
| `transferTimeoutMs` | 10 分钟 |
|
|
582
|
-
| `approvalTimeoutMs` | 5
|
|
600
|
+
| `approvalTimeoutMs` | 5 分钟(不计入工具超时) |
|
|
583
601
|
|
|
584
|
-
|
|
602
|
+
停止回答、更换或关闭文件夹都会取消进行中的调用;确认框应关闭。
|
|
585
603
|
|
|
586
|
-
已知限制:File System Access API
|
|
604
|
+
已知限制:File System Access API 不暴露符号链接目标;`upload_file` 返回 `fileId` 即视为成功。
|
|
587
605
|
|
|
588
606
|
## 错误码
|
|
589
607
|
|
|
590
|
-
|
|
608
|
+
工具结果 `{ code, message, data? }`。界面主要看 `tool:error` 的 `code` 与 `reason`。
|
|
591
609
|
|
|
592
610
|
| code | 含义 |
|
|
593
611
|
| --- | --- |
|
|
594
612
|
| 0 | 成功 |
|
|
595
|
-
| 400 |
|
|
596
|
-
| 403 |
|
|
597
|
-
| 404 |
|
|
613
|
+
| 400 | 参数错误、二进制、非 UTF-8、`search_text` 范围为整个工作区 |
|
|
614
|
+
| 403 | 未授权 / 越界 / 敏感文件 / 用户拒绝 / 无确认回调 / 下载地址不允许 |
|
|
615
|
+
| 404 | 不存在 |
|
|
598
616
|
| 408 | 超时 |
|
|
599
|
-
| 409 | `expectedHash`
|
|
617
|
+
| 409 | `expectedHash` 不一致,或下载目标已存在 |
|
|
600
618
|
| 413 | 超过大小上限 |
|
|
601
|
-
| 429 |
|
|
602
|
-
| 499 |
|
|
603
|
-
| 500 |
|
|
604
|
-
| 501 |
|
|
619
|
+
| 429 | 并发过多,或已有脚本在运行 |
|
|
620
|
+
| 499 | 已取消 |
|
|
621
|
+
| 500 | 内部异常(消息无路径与调用栈) |
|
|
622
|
+
| 501 | 无可用命令执行器 |
|
|
605
623
|
|
|
606
624
|
## 开发
|
|
607
625
|
|