@zhin.js/adapter-sandbox 1.1.35 → 1.2.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 +67 -0
- package/README.md +5 -0
- package/package.json +22 -19
- package/pages/index/ConversationRecoveryPanel.js +5 -4
- package/pages/index/ConversationRecoveryPanel.tsx +7 -6
- package/pages/index/HtmlMessage.js +45 -8
- package/pages/index/HtmlMessage.tsx +67 -17
- package/pages/index/SandboxChat.js +135 -66
- package/pages/index/SandboxChat.tsx +353 -273
- package/pages/index/agentTrace.js +1 -1
- package/pages/index/agentTrace.ts +1 -1
- package/pages/index/conversationRecovery.js +28 -0
- package/pages/index/conversationRecovery.ts +41 -0
- package/pages/index/index.js +2 -0
- package/pages/index/index.tsx +2 -0
- package/pages/index/messages.js +2 -1
- package/pages/index/messages.ts +2 -1
- package/pages/index/styles.js +322 -309
- package/pages/index/styles.ts +322 -309
- package/pages/index/workspaceStyles.js +1737 -0
- package/pages/index/workspaceStyles.ts +1736 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,72 @@
|
|
|
1
1
|
# @zhin.js/adapter-process
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 58046de: Sandbox 会话页迁到 `@zhin.js/console-ui`(ADR 0063 P2,D13 两步)。
|
|
8
|
+
|
|
9
|
+
- 第一步:zhinjs/console 中只服务 Sandbox 的样式(`agent-playground.css` 全部、`im-workspace.css` 中 Sandbox 用到的规则)搬进插件 `pages/index/workspaceStyles.ts`,由页面自己注入、全部限定在 `.zhin-sandbox` 内,不再依赖 Console 的样式表与 Tailwind 扫描。
|
|
10
|
+
- 第二步:按钮、输入框、下拉框(原生 `<select>` 换成 `SelectField`)、开关、页签、「更多」菜单(`DropdownMenu`)、清空确认(`ConfirmDialog`)、提示条(`Alert`)改用 `@zhin.js/console-ui`;危险操作为描边按钮,实心红色只在确认对话框中出现;页面以 `definePage({ fullWidth: true })` 占满内容区,页头交给 Console 外壳(`usePageChrome`),无外壳时页面自己画「会话列表」「检查器」开关;手机上运行栏只保留运行状态、停止与「更多」,其余操作收进「更多」,可点控件不小于 44px。
|
|
11
|
+
- 修正 768–1180px 宽度下会话列表变成抽屉却没有开关、主区被挤进第一列的问题;会话抽屉不再用 `position: fixed` 盖住 Console 外壳;打开检查器时宽屏下输入区不再被挤出。
|
|
12
|
+
- 样式令牌化:页面自带 CSS 中的字号、字重、间距、圆角、阴影改用 `@zhin.js/console-ui` 令牌(`var(--console-…)`),断点改为 Console 的 40 / 48 / 64 / 90rem(`@media (width < 64rem)` 这类范围写法,列表抽屉与手机运行栏的脚本判断同步),界面颜色改用主题色令牌(代码块的语法高亮配色保留);`zhin plugin check` 第 1–6 条与 Playwright 布局审计均无违规,Sandbox 进入官方插件 UI 门禁。表情列表、Agent Trace、待处理交互的响应格式异常时不再让整页崩溃。
|
|
13
|
+
- `package.json` 新增 peerDependency `@zhin.js/console-ui`(`workspace:^`,发布时为 `^` 加当时的 console-ui 版本,即 `^1.x`),需要提供该共享模块的 Console。
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 19526a6: 没回复、被拒绝、离线都说清原因(zhinjs/zhin#1250 第 31 批):
|
|
18
|
+
|
|
19
|
+
- `self-service-unknown-conversation-opaque`(回退的根因):第 9 批(ADR 0060 P3,b6ec01464「Sandbox speaks as the linked chat identity; typed ids are simulated and role-less」)起,Sandbox 里手填的发送者以 `~sim:<id>` 进入运行时,而 `chatRoleMessageContext()` 把这种保留 id 当成「无法识别」,`功能` / `角色` / `认领` / `绑定` 都只回「无法识别当前会话。」;之前同一个群里 `功能 关闭 repeater` 能用,是因为当时手填的 id 还会被当成真实身份。现在 `@zhin.js/core` 新增 `resolveChatRoleMessageContext()`,返回 `{ ok: false, reason }`(`no-conversation` / `unknown-endpoint` / `no-adapter` / `no-sender` / `simulated-sender` / `invalid-sender`),实例侧再加 `unkeyed-conversation`(适配器没有提供可用的会话类型或会话 id);四个指令按原因回复下一步(如「你正以模拟身份发言……请在 Sandbox 打开『以我的身份发言』,或在 Console『功能』/『聊天角色』页设置」)。不带参数的 `功能` / `角色` 直接列出。
|
|
20
|
+
- 被拒绝或失败的自助指令不再记成 `command_replied | executed`:`UserFacingError` 新增闭合的拒绝码 `reason`,指令派发结果带 `refusalReason`,入站结局记 `refused` 与该码(如 `simulated-sender`、`not-admin`、`usage`、`locked`、`self-service-disabled`),回复文字不变。没有码的 `UserFacingError` 仍记 `user_facing_error`。
|
|
21
|
+
- `unaddressed-message-no-record`(复测失败的根因):第 18 批之后,没被 @、不是指令的群消息其实已经记了 `not_addressed`,但未点名的静默结局一律只写 debug 日志,Console 的运行日志看不到;结局只存在进程内,Sandbox 和轨迹页也没有入口,看起来就像「什么都没记」。现在每个会话每分钟第一条 `not_addressed` 写 info(`inbound_outcome | … | sampled: first_per_minute`),其余仍为 debug;新增 `GET {base}/conversations/outcomes`(`endpoint.view`,只限当前 Sandbox 连接绑定的会话、且只列该连接 owner 本人或其模拟身份发出的消息;只返回结局码和面向发送者的文案,不含插件名、阶段等运维文案),Sandbox 发消息后读取这条消息的结局,没有回复时显示原因(如「这条消息没有 @ 我,也不是指令,本群我只在被 @ 时回复。(not_addressed · no_command_not_ambient)」)。`ConsoleMessageDiagnostic` 补上已有的 `outcome` 字段。
|
|
22
|
+
- `console-offline-no-reason`:适配器不上报连接状态(如 GitHub 这类 Webhook 适配器)且没有失败记录的已启动账号,不再显示「离线 · 连接状态:未知」,改为「未上报连接状态」(提示级),并说明常见原因与检查方法;确实离线时附上最近一次失败的原因、失败时间与自动重试次数 / 下次重试时间。这些只改显示文字:条目的 revision 与之前相同,已忽略的待办不会重新出现(「已从配置导入聊天角色」的 revision 也仍按原始条目数计算)。
|
|
23
|
+
- `principal-link-attention-ids`:「人员名单或身份链接等待确认」按人分组,每个身份写出适配器与账号(`qq·bot-a`),平台 id 只显示后 4 位(`尾号 1234`),数量按去重后的 (adapter, endpoint, userId) 计算;查看者本人显示为「你(显示名)」,其他 Console 用户显示 id 前 8 位,条目新增 `links`(`ConsoleAttentionItem.links`)供 Console 按 `iam.users.list` 换成用户名。
|
|
24
|
+
|
|
25
|
+
- f1affdd: 游戏输入与入站结局(zhinjs/zhin#1250 第 34 批):
|
|
26
|
+
|
|
27
|
+
- `game-input-also-reaches-agent`:猜数字的数字中间件在游戏回复之后仍调用 `next()`,同一条「50」又流到 Agent,于是有两条回复。现在游戏处理过的输入就结束分发,只有没有对局时才放行。`replyGameOperation()` 返回游戏是否回复了(`boolean`),并接受可选的 `input`:传入后把消息结局记成 `handler_replied` / `game_input`。所有游戏的对局中间件都会传入它。成语接龙和猜谜的自由文本作答也改为只在游戏真正处理了输入时才结束分发。
|
|
28
|
+
- `inbound-outcome-middleware-reply-mislabeled`:中间件先回复(回执 `sent`)再停止分发的,结局记 `handler_replied` / `middleware_replied`。只看停止分发的那个中间件所属插件自己送达的回复:别的插件先回复过、而它没回复就停止的,仍记 `filtered_by_policy` / `middleware_stopped`。入站结局端口 `InboundOutcomeSink` 新增可选方法 `replied(owner?)`。ADR 0061 已同步;没有改动已有的 reason 取值。
|
|
29
|
+
- `game-button-label-text-not-accepted`:游戏会话同时接受序号和按钮文字(忽略 emoji、空白与大小写),例如「要牌」「🏳️ 放弃」。对局结束后的文字「再来一局」(以及二十一点的序号 1)只在结束后 10 分钟内有效,之后按普通聊天放行。地牢远征在群里由整个频道共享:不在队伍里的人只能用文字「加入」,其余文字和序号都放行。`buildChoiceKeyboard()` / `buildGridKeyboard()` 统一在卡片上加一行「回复序号或按钮文字也可以」:猜数字的数字是猜测本身,所以只提示按钮文字;对局结束后没有可点按钮时不加。键盘段新增 `fallback.layout: 'inline'`,在能力画像为 `interactive: 'text'` 的适配器上,按钮渲染成一行 `1. 要牌 2. 停牌 3. 放弃`。井字棋改为按棋盘位置编号:回复 1–9 落在对应格,卡片上写明「回复 1–9 在对应位置落子(左上为 1,右下为 9)」。
|
|
30
|
+
- `game-command-names-mixed-language`:所有游戏的子命令都有中英两种写法,都能用(开始 / 继续 / 退出 / 认输 / 加入 / 离开 / 观战 / 人机)。帮助和提示里的写法跟命令名同一种语言,`formatGameHelp()` / `gameCommandHint()` 统一处理。井字棋的中文命令改为与标题一致的「井字棋」,旧名「井字游戏」仍可用。约定写在 `plugins/games/README.md`。
|
|
31
|
+
- `near-miss-suggestion-param-misleading`:指令参数新增只用于显示的 `suggestions` / `suggestionsI18n`。「你是不是想用」改为列出真实子命令(`玩石头剪刀布 开始|继续|退出`),不再显示 `[action]`。「玩石头剪刀布 石头」/「猜拳 石头」现在一步开局并出拳。
|
|
32
|
+
- `game-quit-means-forfeit`:会判负的退出(井字棋、成语接龙)在帮助里写「认输并结束对局」;其他游戏的退出写「结束当前对局(不计胜负)」。
|
|
33
|
+
- `command-menu-unreadable-in-sandbox`:指令菜单模板加了媒体查询:宽度 860px 以下为两列,560px 以下为单列,名称按词换行。Sandbox 按 HTML 段请求的 `width` 渲染,再等比缩小到聊天栏宽度,和平台实际收到的图片一致。
|
|
34
|
+
- `config-write-moves-inline-comment`:YAML 逐键补丁不再把写在键行上的注释(`"user:…": # …` 后接块状列表或映射)挪到下一行。补丁没有触及的顶层条目逐字节保留原文,flow 集合的空格风格也沿用原文件。`zhin disable` 现在只改 `disable_plugins`。
|
|
35
|
+
|
|
36
|
+
- 9b868ac: Sandbox 会话栏(无 Console 外壳时)去掉写死的英文副标题「Conversations」,标题已按语言显示「会话 / Conversations」(zhinjs/console#125)。
|
|
37
|
+
- Updated dependencies [8941172]
|
|
38
|
+
- Updated dependencies [f370ec5]
|
|
39
|
+
- Updated dependencies [48dc650]
|
|
40
|
+
- Updated dependencies [a7f3a7c]
|
|
41
|
+
- Updated dependencies [19526a6]
|
|
42
|
+
- Updated dependencies [f1affdd]
|
|
43
|
+
- Updated dependencies [2ab7fb9]
|
|
44
|
+
- @zhin.js/client@1.3.0
|
|
45
|
+
- @zhin.js/console-contract@1.3.1
|
|
46
|
+
- @zhin.js/core@1.11.0
|
|
47
|
+
- @zhin.js/host-http@1.6.0
|
|
48
|
+
- @zhin.js/page@1.2.1
|
|
49
|
+
- @zhin.js/adapter@1.3.3
|
|
50
|
+
- @zhin.js/feature-kit@1.1.19
|
|
51
|
+
|
|
52
|
+
## 1.1.36
|
|
53
|
+
|
|
54
|
+
### Patch Changes
|
|
55
|
+
|
|
56
|
+
- Updated dependencies [a257dae]
|
|
57
|
+
- Updated dependencies [941d534]
|
|
58
|
+
- Updated dependencies [816c3f3]
|
|
59
|
+
- Updated dependencies [d21754f]
|
|
60
|
+
- Updated dependencies [0e6a82f]
|
|
61
|
+
- @zhin.js/client@1.2.2
|
|
62
|
+
- @zhin.js/console-contract@1.3.0
|
|
63
|
+
- @zhin.js/page@1.2.0
|
|
64
|
+
- @zhin.js/core@1.10.0
|
|
65
|
+
- @zhin.js/logger@1.3.0
|
|
66
|
+
- @zhin.js/im-contract@1.3.0
|
|
67
|
+
- @zhin.js/host-http@1.5.0
|
|
68
|
+
- @zhin.js/adapter@1.3.2
|
|
69
|
+
|
|
3
70
|
## 1.1.35
|
|
4
71
|
|
|
5
72
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -30,11 +30,16 @@ Sandbox 不需要外部账号。只需用 `zhin start` 启动项目(管理网
|
|
|
30
30
|
- `@zhin.js/host-http` — Root 提供的 `httpHostToken`(WebSocket `/sandbox` + Console HTTP)
|
|
31
31
|
- `@zhin.js/core` — `endpointSpec.events` 入站、`outboundMessageToken` 出站
|
|
32
32
|
- `@zhin.js/page` + `pages/index/index.tsx` — ADR 0046 约定页(`definePage`;路由 `/sandbox`)
|
|
33
|
+
- `@zhin.js/console-ui`(peerDependency,由 Console 提供单例)— 页面的按钮、输入框、下拉框、开关、页签、菜单和确认对话框(ADR 0063)
|
|
33
34
|
|
|
34
35
|
`zhin start` 拉起的实例装载 `@zhin.js/host-http`、`ConsoleRuntime` 与
|
|
35
36
|
`ClientBuildModuleRuntime`(HTTP 由管理网关监听并转发)。打开 `http://<host>:<port>/console` 可浏览页面;Sandbox 页
|
|
36
37
|
(路由 `/sandbox`,与 WebSocket `/sandbox` 同 path:GET 开页、Upgrade 走 WS)内置聊天壳。
|
|
37
38
|
|
|
39
|
+
页面以 `definePage({ fullWidth: true })` 占满内容区,页头(标题与「会话列表」「检查器」两个开关)交给 Console 外壳
|
|
40
|
+
(`usePageChrome`);没有外壳的旧 Console 中由页面自己画这两个开关。工作区样式随页面注入(`pages/index/workspaceStyles.ts`、
|
|
41
|
+
`styles.ts`,全部限定在 `.zhin-sandbox` 内),不依赖 Console 的样式表或 Tailwind 扫描(ADR 0063 D13)。
|
|
42
|
+
|
|
38
43
|
旧 `client/`(`register(api)` / `pageManager.addEntry`)仅保留给 legacy Host 栈参考,
|
|
39
44
|
**不是** Plugin Runtime 生产入口。
|
|
40
45
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/adapter-sandbox",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Sandbox adapter: a local chat workspace for the Remote Console, for development and testing without a real platform.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zhin.js",
|
|
@@ -39,35 +39,38 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"lucide-react": "^0.525.0",
|
|
42
|
-
"@zhin.js/adapter": "1.3.
|
|
43
|
-
"@zhin.js/client": "1.
|
|
44
|
-
"@zhin.js/console-contract": "1.
|
|
45
|
-
"@zhin.js/core": "1.
|
|
46
|
-
"@zhin.js/feature-kit": "1.1.
|
|
47
|
-
"@zhin.js/host-http": "1.
|
|
48
|
-
"@zhin.js/im-contract": "1.
|
|
49
|
-
"@zhin.js/logger": "1.
|
|
50
|
-
"@zhin.js/page": "1.1
|
|
42
|
+
"@zhin.js/adapter": "1.3.3",
|
|
43
|
+
"@zhin.js/client": "1.3.0",
|
|
44
|
+
"@zhin.js/console-contract": "1.3.1",
|
|
45
|
+
"@zhin.js/core": "1.11.0",
|
|
46
|
+
"@zhin.js/feature-kit": "1.1.19",
|
|
47
|
+
"@zhin.js/host-http": "1.6.0",
|
|
48
|
+
"@zhin.js/im-contract": "1.3.0",
|
|
49
|
+
"@zhin.js/logger": "1.3.0",
|
|
50
|
+
"@zhin.js/page": "1.2.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/react": "^19.2.18",
|
|
54
54
|
"@types/ws": "^8.18.1",
|
|
55
55
|
"react": "^19.2.8",
|
|
56
|
+
"react-dom": "^19.2.8",
|
|
56
57
|
"typescript": "^6.0.3",
|
|
57
58
|
"vitest": "^4.1.10",
|
|
58
59
|
"ws": "^8.21.1",
|
|
59
|
-
"@zhin.js/
|
|
60
|
-
"@zhin.js/
|
|
61
|
-
"zhin.js": "1.
|
|
60
|
+
"@zhin.js/console-ui": "1.1.0",
|
|
61
|
+
"@zhin.js/pagemanager": "1.2.1",
|
|
62
|
+
"@zhin.js/runtime": "1.6.0",
|
|
63
|
+
"zhin.js": "1.6.0"
|
|
62
64
|
},
|
|
63
65
|
"peerDependencies": {
|
|
64
66
|
"react": "^19.0.0",
|
|
65
|
-
"@zhin.js/adapter": "^1.3.
|
|
66
|
-
"@zhin.js/console-contract": "^1.
|
|
67
|
-
"@zhin.js/
|
|
68
|
-
"@zhin.js/
|
|
69
|
-
"@zhin.js/
|
|
70
|
-
"zhin.js": "^1.
|
|
67
|
+
"@zhin.js/adapter": "^1.3.3",
|
|
68
|
+
"@zhin.js/console-contract": "^1.3.1",
|
|
69
|
+
"@zhin.js/console-ui": "^1.1.0",
|
|
70
|
+
"@zhin.js/core": "^1.11.0",
|
|
71
|
+
"@zhin.js/host-http": "^1.6.0",
|
|
72
|
+
"@zhin.js/page": "^1.2.1",
|
|
73
|
+
"zhin.js": "^1.6.0"
|
|
71
74
|
},
|
|
72
75
|
"peerDependenciesMeta": {
|
|
73
76
|
"react": {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
4
|
import { MarkdownContent } from '@zhin.js/client';
|
|
5
|
+
import { Button } from '@zhin.js/console-ui';
|
|
5
6
|
import { sandboxText } from './messages.js';
|
|
6
7
|
import { captureSandboxRequestContext } from './sandboxTransport.js';
|
|
7
8
|
import { prependResultPage, readConversationResults, readPendingInteractions, resultDeliveryStatus } from './conversationRecovery.js';
|
|
@@ -32,7 +33,7 @@ export default function ConversationRecoveryPanel(props) {
|
|
|
32
33
|
const data = await readPendingInteractions(connectionId, sessionKey, controller.signal, current.context);
|
|
33
34
|
if (!active())
|
|
34
35
|
return;
|
|
35
|
-
const pending = data?.pending
|
|
36
|
+
const pending = Array.isArray(data?.pending) ? data.pending : [];
|
|
36
37
|
const retired = data ? current.interactions.filter(previous => !pending.some(next => next.generation === previous.generation && next.id === previous.id)) : [];
|
|
37
38
|
if (data)
|
|
38
39
|
current.interactions = pending;
|
|
@@ -117,13 +118,13 @@ export default function ConversationRecoveryPanel(props) {
|
|
|
117
118
|
})();
|
|
118
119
|
return current.results;
|
|
119
120
|
};
|
|
120
|
-
const results = page?.results.filter(result => resultDeliveryStatus(result) !== 'sent')
|
|
121
|
+
const results = (Array.isArray(page?.results) ? page.results : []).filter(result => resultDeliveryStatus(result) !== 'sent');
|
|
121
122
|
if (!pending.length && !results.length && !pendingError && !resultError && !page?.hasMore)
|
|
122
123
|
return null;
|
|
123
|
-
return _jsxs("div", { className: "agent-playground-recovery", hidden: props.hidden, children: [pendingError ? _jsxs("p", { role: "alert", children: [pendingError, " ", _jsx(
|
|
124
|
+
return _jsxs("div", { className: "agent-playground-recovery", hidden: props.hidden, children: [pendingError ? _jsxs("p", { role: "alert", children: [pendingError, " ", _jsx(Button, { type: "button", size: "sm", variant: "outline", onClick: () => void reloadPending(true), children: sandboxText(locale, '重试') })] }) : null, pending.length ? _jsx(Button, { type: "button", size: "sm", variant: "outline", onClick: () => void reloadPending(true), children: sandboxText(locale, '重新读取') }) : null, pending.map(interaction => _jsxs("section", { children: [_jsx("h3", { children: sandboxText(locale, '待处理操作') }), props.renderInteraction(interaction.segments, interaction.id, submitted.has(interaction.id), () => {
|
|
124
125
|
setSubmitted(previous => new Set([...previous, interaction.id]));
|
|
125
126
|
void reloadPending();
|
|
126
|
-
})] }, `${interaction.generation}:${interaction.id}`)), resultError ? _jsxs("p", { role: "alert", children: [resultError, " ", _jsx(
|
|
127
|
+
})] }, `${interaction.generation}:${interaction.id}`)), resultError ? _jsxs("p", { role: "alert", children: [resultError, " ", _jsx(Button, { type: "button", size: "sm", variant: "outline", onClick: () => void reloadResults(), children: sandboxText(locale, '重试') })] }) : null, page?.hasMore ? _jsx(Button, { type: "button", size: "sm", variant: "outline", disabled: loadingEarlier, onClick: () => void earlier(), children: sandboxText(locale, '加载更早结果') }) : null, results.map(result => {
|
|
127
128
|
const status = resultDeliveryStatus(result);
|
|
128
129
|
const resources = new Map(result.deliveries.flatMap(delivery => delivery.resources).map(resource => [resource.url, resource]));
|
|
129
130
|
return _jsxs("details", { children: [_jsxs("summary", { children: [sandboxText(locale, status === 'not_sent' ? '已完成,未投递' : status === 'partial' ? '已完成,部分内容未投递' : '已完成,投递结果未知'), _jsx("time", { children: new Date(result.completedAt).toLocaleString(locale) })] }), _jsx(MarkdownContent, { text: result.message }), [...resources.values()].map(resource => _jsx("a", { href: resource.url, target: "_blank", rel: "noopener noreferrer", children: resource.type }, resource.url))] }, result.turnId);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { MarkdownContent, type MessageSegment } from '@zhin.js/client';
|
|
3
|
+
import { Button } from '@zhin.js/console-ui';
|
|
3
4
|
import { sandboxText, type SandboxLocale } from './messages.js';
|
|
4
5
|
import { captureSandboxRequestContext, type SandboxRequestContext } from './sandboxTransport.js';
|
|
5
6
|
import { prependResultPage, readConversationResults, readPendingInteractions, resultDeliveryStatus,
|
|
@@ -42,7 +43,7 @@ export default function ConversationRecoveryPanel(props: ConversationRecoveryPro
|
|
|
42
43
|
try {
|
|
43
44
|
const data = await readPendingInteractions(connectionId, sessionKey, controller.signal, current.context);
|
|
44
45
|
if (!active()) return;
|
|
45
|
-
const pending = data?.pending
|
|
46
|
+
const pending = Array.isArray(data?.pending) ? data.pending : [];
|
|
46
47
|
const retired = data ? current.interactions.filter(previous => !pending.some(next => next.generation === previous.generation && next.id === previous.id)) : [];
|
|
47
48
|
if (data) current.interactions = pending;
|
|
48
49
|
setPending(pending);
|
|
@@ -106,11 +107,11 @@ export default function ConversationRecoveryPanel(props: ConversationRecoveryPro
|
|
|
106
107
|
return current.results;
|
|
107
108
|
};
|
|
108
109
|
|
|
109
|
-
const results = page?.results.filter(result => resultDeliveryStatus(result) !== 'sent')
|
|
110
|
+
const results = (Array.isArray(page?.results) ? page.results : []).filter(result => resultDeliveryStatus(result) !== 'sent');
|
|
110
111
|
if (!pending.length && !results.length && !pendingError && !resultError && !page?.hasMore) return null;
|
|
111
112
|
return <div className="agent-playground-recovery" hidden={props.hidden}>
|
|
112
|
-
{pendingError ? <p role="alert">{pendingError} <
|
|
113
|
-
{pending.length ? <
|
|
113
|
+
{pendingError ? <p role="alert">{pendingError} <Button type="button" size="sm" variant="outline" onClick={() => void reloadPending(true)}>{sandboxText(locale, '重试')}</Button></p> : null}
|
|
114
|
+
{pending.length ? <Button type="button" size="sm" variant="outline" onClick={() => void reloadPending(true)}>{sandboxText(locale, '重新读取')}</Button> : null}
|
|
114
115
|
{pending.map(interaction => <section key={`${interaction.generation}:${interaction.id}`}>
|
|
115
116
|
<h3>{sandboxText(locale, '待处理操作')}</h3>
|
|
116
117
|
{props.renderInteraction(interaction.segments, interaction.id, submitted.has(interaction.id), () => {
|
|
@@ -118,8 +119,8 @@ export default function ConversationRecoveryPanel(props: ConversationRecoveryPro
|
|
|
118
119
|
void reloadPending();
|
|
119
120
|
})}
|
|
120
121
|
</section>)}
|
|
121
|
-
{resultError ? <p role="alert">{resultError} <
|
|
122
|
-
{page?.hasMore ? <
|
|
122
|
+
{resultError ? <p role="alert">{resultError} <Button type="button" size="sm" variant="outline" onClick={() => void reloadResults()}>{sandboxText(locale, '重试')}</Button></p> : null}
|
|
123
|
+
{page?.hasMore ? <Button type="button" size="sm" variant="outline" disabled={loadingEarlier} onClick={() => void earlier()}>{sandboxText(locale, '加载更早结果')}</Button> : null}
|
|
123
124
|
{results.map(result => {
|
|
124
125
|
const status = resultDeliveryStatus(result);
|
|
125
126
|
const resources = new Map(result.deliveries.flatMap(delivery => delivery.resources).map(resource => [resource.url, resource]));
|
|
@@ -2,11 +2,42 @@
|
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { sandboxText } from './messages.js';
|
|
4
4
|
import React, { useEffect, useRef, useState } from 'react';
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/** Largest render width honoured from an HTML segment (wider requests render at this width). */
|
|
6
|
+
const MAX_RENDER_WIDTH = 4096;
|
|
7
|
+
/** A positive render width requested by the HTML segment (`data.width`), or `undefined`. */
|
|
8
|
+
export function htmlRenderWidth(value) {
|
|
9
|
+
const width = typeof value === 'string' && value.trim() ? Number(value) : value;
|
|
10
|
+
return typeof width === 'number' && Number.isFinite(width) && width >= 1 ? Math.min(MAX_RENDER_WIDTH, Math.round(width)) : undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Scale that fits a `width`-wide render into `available` pixels (never enlarges). */
|
|
13
|
+
export function htmlRenderScale(width, available) {
|
|
14
|
+
if (!width || !(available > 0))
|
|
15
|
+
return 1;
|
|
16
|
+
return Math.min(1, available / width);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* HTML stays in an opaque-origin frame; only the size reporter can run scripts. With a render
|
|
20
|
+
* `width` (the HTML segment's `data.width`, e.g. 1080 for the command menu) the document is laid out
|
|
21
|
+
* at that width — the same as the image the renderer sends to a platform — and the frame is scaled
|
|
22
|
+
* down proportionally to the chat column (#1250 command-menu-unreadable-in-sandbox).
|
|
23
|
+
*/
|
|
24
|
+
export default function HtmlMessage({ html, locale = 'zh-CN', width: requestedWidth }) {
|
|
7
25
|
const frame = useRef(null);
|
|
26
|
+
const box = useRef(null);
|
|
8
27
|
const [nonce] = useState(createHtmlMessageNonce);
|
|
9
28
|
const [height, setHeight] = useState(240);
|
|
29
|
+
const [available, setAvailable] = useState(0);
|
|
30
|
+
const width = htmlRenderWidth(requestedWidth);
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const element = box.current;
|
|
33
|
+
if (!width || !element || typeof ResizeObserver === 'undefined')
|
|
34
|
+
return;
|
|
35
|
+
const measure = () => setAvailable(element.getBoundingClientRect().width);
|
|
36
|
+
const observer = new ResizeObserver(measure);
|
|
37
|
+
observer.observe(element);
|
|
38
|
+
measure();
|
|
39
|
+
return () => observer.disconnect();
|
|
40
|
+
}, [width]);
|
|
10
41
|
useEffect(() => {
|
|
11
42
|
const onSize = (event) => {
|
|
12
43
|
if (event.source !== frame.current?.contentWindow
|
|
@@ -15,20 +46,26 @@ export default function HtmlMessage({ html, locale = 'zh-CN' }) {
|
|
|
15
46
|
return;
|
|
16
47
|
const value = event.data.height;
|
|
17
48
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
18
|
-
setHeight(Math.ceil(Math.min(1200, Math.max(32, value))));
|
|
49
|
+
setHeight(Math.ceil(Math.min(width ? 12000 : 1200, Math.max(32, value))));
|
|
19
50
|
}
|
|
20
51
|
};
|
|
21
52
|
window.addEventListener('message', onSize);
|
|
22
53
|
return () => window.removeEventListener('message', onSize);
|
|
23
|
-
}, [nonce]);
|
|
24
|
-
const srcDoc = createHtmlMessageDocument(html, nonce);
|
|
25
|
-
|
|
54
|
+
}, [nonce, width]);
|
|
55
|
+
const srcDoc = createHtmlMessageDocument(html, nonce, width);
|
|
56
|
+
if (!width) {
|
|
57
|
+
return _jsx("iframe", { ref: frame, title: sandboxText(locale, "HTML 消息"), sandbox: "allow-scripts", srcDoc: srcDoc, className: "sandbox-html-frame", style: { height } });
|
|
58
|
+
}
|
|
59
|
+
const scale = htmlRenderScale(width, available);
|
|
60
|
+
return _jsx("div", { ref: box, className: "sandbox-html-scaled", style: { height: Math.ceil(height * scale) }, children: _jsx("iframe", { ref: frame, title: sandboxText(locale, "HTML 消息"), sandbox: "allow-scripts", srcDoc: srcDoc, className: "sandbox-html-frame", style: { width, height, transform: `scale(${scale})`, transformOrigin: 'top left' } }) });
|
|
26
61
|
}
|
|
27
62
|
/** getRandomValues is available on ordinary HTTP LAN pages as well as HTTPS. */
|
|
28
63
|
export function createHtmlMessageNonce() {
|
|
29
64
|
return Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
30
65
|
}
|
|
31
66
|
/** The trusted reporter precedes raw markup so an unfinished tag cannot swallow it. */
|
|
32
|
-
export function createHtmlMessageDocument(html, nonce) {
|
|
33
|
-
|
|
67
|
+
export function createHtmlMessageDocument(html, nonce, width) {
|
|
68
|
+
const renderWidth = htmlRenderWidth(width);
|
|
69
|
+
const sized = renderWidth ? `html,body{width:${renderWidth}px;overflow:hidden}` : '';
|
|
70
|
+
return `<!doctype html><html><head><meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data: blob:; font-src data:; style-src 'unsafe-inline'; script-src 'nonce-${nonce}'; base-uri 'none'; form-action 'none'"><style>html,body{margin:0;padding:0;min-height:0}body{display:flow-root;overflow-wrap:anywhere}img{max-width:100%}${sized}</style></head><body><script nonce="${nonce}">const report=()=>parent.postMessage({type:'sandbox-html-size',nonce:'${nonce}',height:document.body.getBoundingClientRect().height},'*');new ResizeObserver(report).observe(document.body);report();<\/script>${html}</body></html>`;
|
|
34
71
|
}
|
|
@@ -1,11 +1,46 @@
|
|
|
1
1
|
import { sandboxText, type SandboxLocale } from './messages.js';
|
|
2
2
|
import React, { useEffect, useRef, useState } from 'react';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/** Largest render width honoured from an HTML segment (wider requests render at this width). */
|
|
5
|
+
const MAX_RENDER_WIDTH = 4096;
|
|
6
|
+
|
|
7
|
+
/** A positive render width requested by the HTML segment (`data.width`), or `undefined`. */
|
|
8
|
+
export function htmlRenderWidth(value: unknown): number | undefined {
|
|
9
|
+
const width = typeof value === 'string' && value.trim() ? Number(value) : value;
|
|
10
|
+
return typeof width === 'number' && Number.isFinite(width) && width >= 1 ? Math.min(MAX_RENDER_WIDTH, Math.round(width)) : undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Scale that fits a `width`-wide render into `available` pixels (never enlarges). */
|
|
14
|
+
export function htmlRenderScale(width: number | undefined, available: number): number {
|
|
15
|
+
if (!width || !(available > 0)) return 1;
|
|
16
|
+
return Math.min(1, available / width);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HTML stays in an opaque-origin frame; only the size reporter can run scripts. With a render
|
|
21
|
+
* `width` (the HTML segment's `data.width`, e.g. 1080 for the command menu) the document is laid out
|
|
22
|
+
* at that width — the same as the image the renderer sends to a platform — and the frame is scaled
|
|
23
|
+
* down proportionally to the chat column (#1250 command-menu-unreadable-in-sandbox).
|
|
24
|
+
*/
|
|
25
|
+
export default function HtmlMessage({ html, locale = 'zh-CN', width: requestedWidth }: {
|
|
26
|
+
readonly html: string; readonly locale?: SandboxLocale; readonly width?: number;
|
|
27
|
+
}) {
|
|
6
28
|
const frame = useRef<HTMLIFrameElement>(null);
|
|
29
|
+
const box = useRef<HTMLDivElement>(null);
|
|
7
30
|
const [nonce] = useState(createHtmlMessageNonce);
|
|
8
31
|
const [height, setHeight] = useState(240);
|
|
32
|
+
const [available, setAvailable] = useState(0);
|
|
33
|
+
const width = htmlRenderWidth(requestedWidth);
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
const element = box.current;
|
|
37
|
+
if (!width || !element || typeof ResizeObserver === 'undefined') return;
|
|
38
|
+
const measure = () => setAvailable(element.getBoundingClientRect().width);
|
|
39
|
+
const observer = new ResizeObserver(measure);
|
|
40
|
+
observer.observe(element);
|
|
41
|
+
measure();
|
|
42
|
+
return () => observer.disconnect();
|
|
43
|
+
}, [width]);
|
|
9
44
|
|
|
10
45
|
useEffect(() => {
|
|
11
46
|
const onSize = (event: MessageEvent) => {
|
|
@@ -14,23 +49,36 @@ export default function HtmlMessage({ html, locale = 'zh-CN' }: { readonly html:
|
|
|
14
49
|
|| event.data?.nonce !== nonce) return;
|
|
15
50
|
const value = event.data.height;
|
|
16
51
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
17
|
-
setHeight(Math.ceil(Math.min(1200, Math.max(32, value))));
|
|
52
|
+
setHeight(Math.ceil(Math.min(width ? 12000 : 1200, Math.max(32, value))));
|
|
18
53
|
}
|
|
19
54
|
};
|
|
20
55
|
window.addEventListener('message', onSize);
|
|
21
56
|
return () => window.removeEventListener('message', onSize);
|
|
22
|
-
}, [nonce]);
|
|
23
|
-
|
|
24
|
-
const srcDoc = createHtmlMessageDocument(html, nonce);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
57
|
+
}, [nonce, width]);
|
|
58
|
+
|
|
59
|
+
const srcDoc = createHtmlMessageDocument(html, nonce, width);
|
|
60
|
+
|
|
61
|
+
if (!width) {
|
|
62
|
+
return <iframe
|
|
63
|
+
ref={frame}
|
|
64
|
+
title={sandboxText(locale, "HTML 消息")}
|
|
65
|
+
sandbox="allow-scripts"
|
|
66
|
+
srcDoc={srcDoc}
|
|
67
|
+
className="sandbox-html-frame"
|
|
68
|
+
style={{ height }}
|
|
69
|
+
/>;
|
|
70
|
+
}
|
|
71
|
+
const scale = htmlRenderScale(width, available);
|
|
72
|
+
return <div ref={box} className="sandbox-html-scaled" style={{ height: Math.ceil(height * scale) }}>
|
|
73
|
+
<iframe
|
|
74
|
+
ref={frame}
|
|
75
|
+
title={sandboxText(locale, "HTML 消息")}
|
|
76
|
+
sandbox="allow-scripts"
|
|
77
|
+
srcDoc={srcDoc}
|
|
78
|
+
className="sandbox-html-frame"
|
|
79
|
+
style={{ width, height, transform: `scale(${scale})`, transformOrigin: 'top left' }}
|
|
80
|
+
/>
|
|
81
|
+
</div>;
|
|
34
82
|
}
|
|
35
83
|
|
|
36
84
|
/** getRandomValues is available on ordinary HTTP LAN pages as well as HTTPS. */
|
|
@@ -40,6 +88,8 @@ export function createHtmlMessageNonce(): string {
|
|
|
40
88
|
}
|
|
41
89
|
|
|
42
90
|
/** The trusted reporter precedes raw markup so an unfinished tag cannot swallow it. */
|
|
43
|
-
export function createHtmlMessageDocument(html: string, nonce: string): string {
|
|
44
|
-
|
|
91
|
+
export function createHtmlMessageDocument(html: string, nonce: string, width?: number): string {
|
|
92
|
+
const renderWidth = htmlRenderWidth(width);
|
|
93
|
+
const sized = renderWidth ? `html,body{width:${renderWidth}px;overflow:hidden}` : '';
|
|
94
|
+
return `<!doctype html><html><head><meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data: blob:; font-src data:; style-src 'unsafe-inline'; script-src 'nonce-${nonce}'; base-uri 'none'; form-action 'none'"><style>html,body{margin:0;padding:0;min-height:0}body{display:flow-root;overflow-wrap:anywhere}img{max-width:100%}${sized}</style></head><body><script nonce="${nonce}">const report=()=>parent.postMessage({type:'sandbox-html-size',nonce:'${nonce}',height:document.body.getBoundingClientRect().height},'*');new ResizeObserver(report).observe(document.body);report();<\/script>${html}</body></html>`;
|
|
45
95
|
}
|