Compare commits

...
Author SHA1 Message Date
leeferandmultica-agent c61d9c95c9 HEL-897: record bytewise preservation and independent regression checks
Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 02:52:42 +08:00
leeferandmultica-agent 761a2331f5 HEL-897: preserve visual author voice session race fixes
Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 02:52:08 +08:00
leeferandmultica-agent f259bd34e1 HEL-878: retain requirement sources and record independent review
Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 02:39:05 +08:00
leeferandmultica-agent b95ce798b4 HEL-878: preserve original author continuous voice wiring
Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 02:35:37 +08:00
39 changed files with 1848 additions and 12 deletions
+73
View File
@@ -0,0 +1,73 @@
{
"base": "bf234570c0b6595a72f8dbc79e42c63685e08e01",
"patch_sha256": "02a7b3d2c03aee79c9fc7c8a470c7053cd7b8aa410699ec51b255c333acdeda4",
"source_zip_sha256": "4596f2cc03210074be1fc9fe23dcdd607f5d240b16f142b67986c1886ce6e9be",
"files": [
{
"path": "docs/requirements/handoff-HEL-878.json",
"sha256": "258cf2935f5aafc20cd7a80a9550ae062024849ff39c078068f371692bbaa516",
"bytes": 9439
},
{
"path": "web/README.md",
"sha256": "f596492a443e7ae9972496a40db2f40c79f8667d4552628afa4718efbd29c726",
"bytes": 3113
},
{
"path": "web/VENDOR.md",
"sha256": "e11c139b5c7a6f86264439b03997bd89c79e40c994ac1db57f31717e83b660b6",
"bytes": 893
},
{
"path": "web/public/vendor/livekit-client-2.22.4.umd.min.js",
"sha256": "040db4c0db72e7f7599cd0e06b0f58f95e0082a395cdde410ca26224a79a0626",
"bytes": 598796
},
{
"path": "web/src/app/RealApp.tsx",
"sha256": "8afb63b8a6b196e29ced1a8bcef541f1f80a10811f1dc0f965631a9f0c201f5e",
"bytes": 14382
},
{
"path": "web/src/chat/ChatPanel.tsx",
"sha256": "419c2f4243d7a147ea8ebcf377d74c97dc558049a3bf76dee39bba0746d2c9e6",
"bytes": 34936
},
{
"path": "web/src/styles/components.css",
"sha256": "f93b875f6918d402aaaefeffdc52fdd908328fb8ac70766704e528c6a8bc154d",
"bytes": 89243
},
{
"path": "web/src/voice/livekit.ts",
"sha256": "c42475d37f7eef7abe0903b66656aedc5379bc9de3dcba11c1b88450a470ab04",
"bytes": 6859
},
{
"path": "web/src/voice/session.ts",
"sha256": "128123ab75e788699150321ca1341081450c34b09918b0b825c542dbc60b6e07",
"bytes": 18058
},
{
"path": "web/tests/fake-livekit.js",
"sha256": "d16e794308073e967566f3ebda16e72d0f5eb0132b657338baa6a29115b02508",
"bytes": 4177
},
{
"path": "web/tests/hel878_mock.mjs",
"sha256": "d7c9d79308979f1f3cd7172f56f52b4cfb35de1fcbae2876fa7d66ea4e2c3071",
"bytes": 8855
},
{
"path": "web/tests/hel878_shots.mjs",
"sha256": "ad664b26b66990e59ca118b0afe1e6dfea8a64351bb9bbb7a666f332b2f15760",
"bytes": 22874
},
{
"path": "web/tests/voice.test.ts",
"sha256": "f6bc899b3202465ec68b276ed21ccbbc5ba6e7edda014e2d7ea3ff63e5683a73",
"bytes": 20829
}
],
"unchanged_archive_files_match_base": true
}
+20
View File
@@ -0,0 +1,20 @@
HEL-878 源码保全与补查(总管,2026-10-27 北京时间)
原作者提交评论:01a1797c-188d-79df-9be1-df6bd47effc6。读取需求 v1.11/60项;前端基点 bf234570c0b6595a72f8dbc79e42c63685e08e01,后台契约 aba2aeb,实际总装基点 04eff611e7d85e539d43717711172441420ac853,main 3a986726d8593e0ed2274f79db6cc5d807d2ebf0。
13 个改动文件逐字节匹配作者 MANIFEST;补丁 SHA-256 匹配,git apply --reverse --check 通过;源码包其余文件与 bf234570 内容一致(仅允许未改文件 CRLF/LF 差异),包内 MANIFEST 与单独附件一致。原样提交 b95ce798b4a9455d941d247a46865b3ac202bf2c,不改作者业务代码或视觉。
补齐前端分支缺少的历史用户原话文件 comment-evidence-full-experience-20261026.json 及 source-manifest,逐字节来自总装 04eff611;无需求变更。Python 原版检查对 origin/main、origin/hel-870-assembly、origin/agent/2/6a08a4c18f23 各带/不带 handoff-HEL-878.json 共6次通过。
使用既有 node_modules(未安装下载)复跑 typecheck、lint、81项单测、build 全通过;lint 0错/1条既有警告。作者45项浏览器检查本轮未重跑,仍为作者证据。真实语音、真实麦克风、WebRTC、ASR/TTS/LLM请求均未执行;未部署、未合并、未改现有服务或绑定。
独立补查发现必须返修的问题:
1. 第一次会话正常开启;模拟掉线,服务端 DELETE s1 延迟返回。
2. 用户立即开启第二次会话,worker已加入,界面 listening,controller.active=true。
3. 放行第一次的 DELETE(含 settle 补偿请求)。旧 fail() 在 await teardown(live) 之后继续写 OFF,没有重新核对代次,覆盖新会话的界面。
4. 实测:controller.active=true,但最后 onView.phase=off。旧失败提示也会覆盖新会话,界面与麦克风会话状态不一致。
定位 web/src/voice/session.ts 的 start() 内 fail()。附件 review878_late_cleanup.test.ts 为独立负例,放到 web/tests/ 后用 ./node_modules/.bin/vitest run tests/review878_late_cleanup.test.ts 可复现;本轮实际执行失败:Expected listening / Received off,原始日志见 review878-race.log。这里只做有限复现,未代替原作者修改;交原作者修复异步清理后的状态提交边界并补有意义回归。
关联 CG-010/018、VIS-013、UX-005/006、GOV-001–006、DEL-001。原81项通过不涵盖这个负例,不能称离线验收通过。修正后交 HEL-879 统一审核;CG-011语音主动开口仍为空操作、增量字幕仍缺、远端断网清理/租约与API重启实情、真实SDK回调与播放回执、真机三轮/打断/停止/HTTPS均继续承接,不因协议现状或mock通过降低完整要求。R6/R7继续主卡HEL-797承接。
+43
View File
@@ -0,0 +1,43 @@
{
"baseline_version": "1.11",
"read_commit": "bf234570c0b6595a72f8dbc79e42c63685e08e01",
"task": "HEL-878(R5 前端接线,视觉总监本人设计并编码):在已认可的交谈界面上由用户主动开启/结束连续语音;按 HEL-877 后台契约(aba2aeb docs/runbook-hel877-voice.md §3)接 GET /api/voice/status、POST/DELETE /api/voice/session 与 cg.subtitle 字幕,复用 .12 :8090 既有 livekit-client 2.22.4 浏览器客户端;文字与语音同一位她、同一聊天记录,轮次从 /api/messages 回读不串台;所有退出路径立即停麦并清理;零真实模型/嵌入/合成/识别/采集/部署",
"requirement_ids": [
"CG-006", "CG-010", "CG-011", "CG-018", "CG-020",
"VIS-001", "VIS-002", "VIS-003", "VIS-004", "VIS-005", "VIS-006", "VIS-007", "VIS-008", "VIS-009", "VIS-010", "VIS-011", "VIS-012", "VIS-013",
"UX-001", "UX-002", "UX-005", "UX-006",
"AM-001",
"GOV-001", "GOV-002", "GOV-003", "GOV-004", "GOV-005", "GOV-006",
"DEL-001"
],
"deliverable": "自前端原稿 hel-875-proactive-wiring @ bf23457 起的补丁(仅 web/ 与本交接):1) web/src/voice/session.ts——VoiceController 生命周期(代次计数:任何迟到的 token/房间/麦克风/房间事件只清理不显示);顺序为诚实状态(只 GET status,不建房不采麦)→ 加载客户端 → 采麦(拒绝则服务端什么都不开)→ POST session(结束时已在途的请求等其返回并 DELETE,不留孤儿房)→ 加入并发布麦克风 → 等 worker 加入(20 s 未加入如实失败);状态取 worker 的 lk.agent.state,缺属性时按字幕保守推断;interim 为草稿、user_final 取消在途文字回复并触发回读、assistant_final 触发回读、error(φ) 只显示「未记入聊天」不入记录;文字盖过她的语音时静音残余音频直到她回到聆听;结束顺序为停麦→关房间与音频→DELETE;断网时本地立即停采,远端 DELETE 记入本浏览器 cg.voice.owed,恢复网络/再次进入/再次开启时补发(404 视为已清);页面卸载用 keepalive DELETE;mergeTurns/recentRows 只合并非主动留言的服务器行(主动留言仍走原 inbox 轮询),按 turn_id 与已有文字轮去重,pending 行回读至落定。2) web/src/voice/livekit.ts——同源懒加载 /vendor/livekit-client-2.22.4.umd.min.js(来源 .12:8090/static,SHA-256 见 web/VENDOR.md,未改 package.json/锁文件),回声消除采集、她的音频集中在隐藏容器(结束即暂停/清空/移除)、自动播放受阻时提供「点一下听她的声音」、加入可被结束即时中止。3) ChatPanel.tsx——真实模式麦克风按钮按状态诚实标注(不可用时淡化并说明原因),确认框说明麦克风、去向、同一份记忆、如何打断与停止、未经真机验收;沿用原演示 voice-row 位置与视觉语言做成玻璃单行(连接/等待为呼吸、聆听用你的颜色、她说话用她的光色,文字始终写明状态);文字流在无 final 时先查服务器行:被新轮取消即按已取消处理,不报连接中断、不回填草稿;离开交谈/Esc/结束交谈/导航卸载面板即结束语音;在途语音轮期间她的主动留言照原规则排队。4) RealApp.tsx——语音进行中顶栏写「语音(未经真机验收)」。5) components.css 新增 .voice-row.real 等样式(含手机)。6) 测试:web/tests/voice.test.ts 26 项;web/tests/hel878_mock.mjs(HEL-877 路由契约 mock)+ fake-livekit.js(脚本化替身,浏览器假音频设备)+ hel878_shots.mjs 浏览器检查 45 项与截图;web/VENDOR.md、README 语音段落。",
"acceptance": [
"到达交谈页只发一次 GET /api/voice/status:不建房、不申请麦克风、不加载 LiveKit 客户端;configured=false / tts_ok=false / 无路由各有诚实说明,按钮淡化且不弹确认;voice=false 不被当作已接通(浏览器 1–2、单测 honest switch)",
"用户确认后:一个会话、一个麦克风、发布到该房间;等待 worker 时显示等待而非已接通;聆听/思考/说话随 lk.agent.state 切换;字幕草稿、φ 原句(标未记入聊天且不入记录)均按契约(浏览器 4–6)",
"语音轮只从 /api/messages 进入舞台与完整记录,各一次,与文字同一段记录(浏览器 5);前端从不 POST /api/chat 或 /api/chat/received(单测)",
"双通道:文字盖过她说话时残余音频静音、回到聆听再恢复;语音打断在途文字回复时该轮按已取消处理,无“连接中断/重试”、不回填草稿,记录显示已取消(浏览器 7–8)",
"结束语音/导航离开/Esc/结束交谈/房间掉线/麦克风被撤销/服务端拒绝/重复开关后:本地麦克风 track 全部 ended、她的音频与容器移除、服务端房间删除;迟到事件不复活旧会话(浏览器 10–14,单测 lifecycle 15 项)",
"断网:本地立即停麦停音频,远端房间如实记为欠删(不冒充已删);恢复网络后补发 DELETE 并清除记录;页面卸载 keepalive DELETE 到达(浏览器 14);欠删跨页面保留、404 视为已清(单测)",
"电脑 1440×900 与手机 390×844(2x)、月光/雾光两主题截图;手机语音行不出屏(浏览器 15)",
"真实 livekit-client 2.22.4 构建中,适配器用到的全部名称存在(RoomEvent 七项、Track.Source.Microphone、ParticipantKind.AGENT、createLocalAudioTrack、Room 方法/属性);仅 API 面核对,未连接",
"演示入口零语音请求;工程:tsc 0 错,eslint 0 错(1 条既有警告),vitest 7 文件 81/81,vite build 通过"
],
"deferred": [
"真实通话全部未测(R5 真机验收交 HEL-879 统一审核):连续 3 轮、开口打断约 1 s 停声、结束停麦无残音、回声、首包延迟、移动端 Safari/Chrome 麦克风与自动播放、自签 HTTPS 信任;浏览器检查用的是脚本化 LiveKit 替身与 Chromium 假音频设备,不是 WebRTC 通话",
"lk.agent.state 属性名按 livekit-agents 1.x 惯例接入,并在缺失时用字幕推断;worker 实际是否发布该属性须真机确认",
"断网时服务端房间的即时删除在物理上做不到:依赖 worker 在浏览器参与者离开后结束任务与会话登记过期(runbook §3.4/3.5,后台行为未在本卡实测),本页恢复网络或下次进入时补发 DELETE",
"CG-011 语音内主动开口:worker._maybe_open_proactive 仍是空操作,前端未虚构;后台最小需求见 runbook §3.6(会话内取已落库 proactive 行 session.say 原文、可打断、无新回执通道),呈现节奏待总管/视觉总监另定;目前语音开启时她的主动留言仍以文字进入并在她思考/说话时排队",
"她说话时没有逐字字幕(契约只在播放完成后发 assistant_final);舞台在落库后呈现全文。若要边说边显示需后台发增量字幕,属契约变更",
"D1 音色、D3 设备信任、测试额度由总管与白栖知沟通;本卡未接触任何服务、凭据或部署",
"原版 Python 需求检查未在本运行时执行(无 Python、无 git),须总管/总工对实际目标分支与 origin/main 含/不含本交接复跑;需与 HEL-882 最新母线同步后再审",
"R6 天气/素材/备份、R7 视觉观察、CG-028/029 口型与真实视频仍由主卡 HEL-797 承接;整体沉浸体验由白栖知验收"
],
"requirement_changes": [],
"verification": [
"读取:前端 bf234570(总管输入包 frontend/,HEL-875/880 原作者原稿);后台契约 aba2aeb(输入包 voice-backend/:runbook-hel877-voice.md、voice/router.py、session.py、worker.py、handoff-HEL-877.json、requirements v1.11 含 comment-evidence-full-experience-20261026.json);已读 AGENTS.md、README/current(CG-006/010/011/018/020、UX-005/006、VIS-013)/coverage/changes、integration-plan-v1 R5 与 D1/D3、voice-r5-readiness-HEL876.md",
"web:tsc --noEmit 0;eslint src 0 错;vitest run 7 文件 81/81(原 55 + 语音 26,含变异检查:去掉迟到授权回收或代次判定各使对应用例失败);vite build 通过(node_modules 沿用 HEL-875 同一锁文件离线树,无新增依赖、无下载)",
"浏览器:node tests/hel878_mock.mjs 8878 + node tests/hel878_shots.mjs <msedge.exe> <out> 8878,45/45(无头 Edge,--use-fake-device-for-media-stream;脚本化 LiveKit 替身;本机回环);中断前一轮为 39/43,失败的 4 项(语音打断文字误报、断网远端清理、卸载检查受其连带)经本轮修复后通过",
"唯一外部读取:一次只读 GET .12:8090 页面及其 /static/livekit-client.umd.min.js 以复用既有客户端;无模型、嵌入、合成、识别请求,无真实麦克风/摄像头,无部署或服务改动",
"需求结构检查:本运行时无 Python/git,未执行原版脚本,不声称通过;仅用 HEL-875 留下的 Node 移植版辅助:以 bf23457 为基含本交接通过(60 项);以后台 aba2aeb 的需求文件为基失败「历史来源必须保留」,原因是该线已追加 comment-evidence-full-experience-20261026.json 而前端原稿尚未包含,与本补丁无关,合入母线前须先同步该需求目录"
]
}
+34
View File
@@ -0,0 +1,34 @@
{
"baseline_version": "1.11",
"read_commit": "b95ce798b4a9455d941d247a46865b3ac202bf2c",
"task": "HEL-897(HEL-878 有限返修,视觉总监本人编码):修正重开语音被旧掉线结果覆盖——旧会话 fail() 在 await teardown 之后提交 OFF,覆盖已 listening 的新会话;并检查同一失败/结束/重开路径中 await 前后的代次与所有权。不重新设计,不改视觉",
"requirement_ids": [
"CG-010", "CG-018",
"VIS-013",
"UX-005", "UX-006",
"GOV-001", "GOV-002", "GOV-003", "GOV-004", "GOV-005", "GOV-006",
"DEL-001"
],
"deliverable": "自 HEL-878 原稿(总管保全提交 b95ce798,13 文件哈希逐一核对一致)起的补丁,只动 web/src/voice/session.ts 与测试:1) start() 内 fail():代次与 live 释放后先发起 teardown(麦克风同步先停、房间关闭与 DELETE 已发出),立即在本会话仍拥有页面时提交 OFF 与失败提示,再等待清理完成;清理迟到返回只释放旧会话自己的麦克风/房间/服务端房间,之后不再写界面。2) start() 先同步取得新代次与 live,再等待被替换会话的清理(原先 await end() 之后才取代次:两次开启交错时,较早一次醒来会抢走代次、把已接通的新会话孤立在外,其麦克风与房间不再被释放);被替换后醒来即退出;settle 补发在取得代次之后。3) web/tests/voice.test.ts 新增「a late end of an older call」6 项(掉线/麦克风被撤销各一,worker 未加入超时,正常结束慢 DELETE,交错重开,迟到麦克风授权);Server 假件增加按会话挂起 DELETE。4) web/tests/review878_late_cleanup.test.ts:总管独立负例原样纳入。5) hel878_mock.mjs 增加 /__hold-delete 控制;hel878_shots.mjs 增加第 12 段后 HEL-897 浏览器复现 4 项。",
"acceptance": [
"总管负例 review878_late_cleanup.test.ts:修前失败(Expected listening / Received off,与总管日志一致),修后通过;测试未改",
"掉线与麦克风被撤销后,旧 DELETE 与 settle 补发都挂起期间重开:新会话 listening、active=true、无旧提示;放行后界面不再变化;旧麦克风已停、旧房间已关,新麦克风/房间未被停;服务端只剩新房间;再结束后全部清空(单测各 1 项)",
"worker 未加入超时失败且 DELETE 慢、正常结束 DELETE 慢后重开、两次开启交错(前一次仍在等旧房间归还)、迟到麦克风授权:只有最新会话存活,界面与 active 一致(单测 4 项;交错一项修前失败,暴露同路径的第二处代次漏洞)",
"修前对照:同一测试集在原 session.ts 上 5 项失败(总管负例 + 新增 4 项),修后 33/33",
"浏览器(无头 Edge,脚本化 LiveKit 替身与假音频设备):掉线后旧 DELETE 挂起,提示与停麦立即生效;新会话 listening;放行旧 DELETE 后新会话仍 listening、无旧提示、1 路活麦克风、她的音频在、服务端只剩新房间;结束后全部清理。修前构建 45/49(实测 phase=off 而新麦克风仍活),修后 49/49,原 45 项(结束/导航/Esc/结束交谈/断网/卸载/快速开关等退出路径)全部保持通过",
"工程:tsc 0 错;eslint 0 错(1 条既有警告);vitest 8 文件 88/88;vite build 通过"
],
"deferred": [
"CG-011 语音内主动开口、她说话时的增量字幕、断网时远端即时清理/租约与 API 重启实情、真实 SDK 回调与播放回执、真机连续三轮/打断/停麦/HTTPS/Safari 全部未测,仍交 HEL-879 统一审核;本卡单测与浏览器均为替身与假设备,不是 WebRTC 通话",
"原版 Python 需求检查未在本运行时执行(无 Python、无 git),须总管对实际目标分支复跑(含/不含 handoff-HEL-878.json 与本交接);未用 Node 移植版冒充通过",
"总管在前端分支从 04eff611 补齐的历史原话文件与 source-manifest 以总管保全分支为准;本补丁不触碰需求目录中除本交接以外的任何文件",
"D1 音色、D3 设备信任由总管与白栖知沟通;R6/R7、CG-028/029 仍由主卡 HEL-797 承接;整体沉浸体验由白栖知验收"
],
"requirement_changes": [],
"verification": [
"读取:总管 HEL-897 卡与附件(HEL-878-总管复核.md、preservation-878.json、review878-race.log、review878_late_cleanup.test.ts);本人 HEL-878 原稿树(逐文件 SHA-256 与 preservation-878.json 13 项一致后才施工,原稿另行备份);需求与 integration-plan R5/D1/D3 沿用 HEL-878 已读内容,本卡无需求变更",
"web:tsc --noEmit 0;eslint src 0 错 1 既有警告;vitest run 8 文件 88/88(原 81 + 本卡 6 + 总管负例 1);vite build 通过;node_modules 沿用 HEL-878 离线树,未下载、未安装",
"修前/修后:同一测试集与浏览器脚本分别在原 session.ts 与修正版上运行,日志随交付包",
"无模型、嵌入、合成、识别请求,无真实麦克风采集,无部署、合并、服务或绑定改动;未改后台与 HEL-896 发布包"
]
}
+2 -1
View File
@@ -14,5 +14,6 @@
"dynamic-sample-effect-acceptance-20261023.md": "f5e9fde65a58ebc365de47569053ad7c30ce77ae82bb78773cc070e3cbffd90e",
"comment-evidence-mature-reuse-20261025.json": "38ca076e26477e89cd161eaadb0fb00b0ee7bb5a26de2bd6ab41de52faaae79e",
"mature-reuse-principle-20261025.md": "a2a75e071d3af5db722727bfaf8083bb60d07dcdd74e90d1e1374c1aafd2064d",
"other-agent-discussion-20261025.txt": "42cd97029d73eace110e49e0566343dfeb25fe4f0633a10b78b126289938aaeb"
"other-agent-discussion-20261025.txt": "42cd97029d73eace110e49e0566343dfeb25fe4f0633a10b78b126289938aaeb",
"comment-evidence-full-experience-20261026.json": "75dd55b8755bdad217b44c3f516c9372b046aae06dff511527014c7b209eccff"
}
@@ -0,0 +1,18 @@
[
{
"id": "01a17806-cb7e-76f3-8f3f-3f6360ddc668",
"author_type": "member",
"author_id": "e5c6c938-db48-4ff9-954f-4fb8b063a132",
"content": "我听不懂你的意思,用大白话问我",
"created_at": "2026-10-26T11:44:33Z",
"parent_id": "01a174b3-867e-7a90-9628-e1abd82a71b7"
},
{
"id": "01a1780e-6954-73ad-91da-3a5d81831a07",
"author_type": "member",
"author_id": "e5c6c938-db48-4ff9-954f-4fb8b063a132",
"content": "我觉得这是工程问题,我不懂所以我无法抉择,但我知道截止目前,整个项目仅前端通过了我的审核,我没有现有的聊天和资料,也不存在真实或假聊天,因为一切都还没开始,我期望的是进行完整的项目体验,然后慢慢迭代,否则我根本不知道哪里是对的哪里是错的,你交由我决定的东西我认为是在原地打转或者说是在外围转圈而不是直奔核心",
"created_at": "2026-10-26T11:52:52Z",
"parent_id": "01a174b3-867e-7a90-9628-e1abd82a71b7"
}
]
+13
View File
@@ -0,0 +1,13 @@
# HEL-897 MANIFEST
Base: HEL-878 author draft as preserved by the supervisor, b95ce798b4a9455d941d247a46865b3ac202bf2c (13 files verified against preservation-878.json before work). Patch is relative to that tree; full tree (no node_modules/dist) in HEL-897-source.zip. Nothing else changed; HEL-878 files and handoff-HEL-878.json untouched except the two test helpers listed below.
## Changed/added files (sha256)
- docs/requirements/handoff-HEL-897.json 56CF0E97B5035CCFAAFF3F7DD219CA30352DF600FA4C9684EB690E4E44731BC9
- web/src/voice/session.ts 4B74731B7DC34534FF8F046B9487E115B3A38CFB202B045E34A12FA27FE528FE
- web/tests/hel878_mock.mjs 1B1608D5E0858842E9A0CF51FAAF9BAECE5E52B546757464B046DF0E66A4B815
- web/tests/hel878_shots.mjs E5706C5A4459315ECDB991CBD8D3776DB3EFE035FE520930FFB09E726EB639C7
- web/tests/review878_late_cleanup.test.ts 65261CD249A36693D0C6B06CE279DE8A60AA74DD7BAAFAD6999D2FEC790F9935
- web/tests/voice.test.ts 4C120B9449CE3AF0B66B3B8A18668D5012D286D6C18C0DEE1347A880F7780471
HEL-897.patch 1D8C43B0FE9984066406E60FAC715764DCD375546713DEB6BD034160F2050070
+18
View File
@@ -0,0 +1,18 @@
HEL-897 返修保全与独立补查(总管,2026-10-27 北京时间)
原作者交付评论:01a1798b-60b0-7562-9a70-82584f52dd54;主卡 HEL-797。需求 v1.11 / 60项,无需求变更。关联 CG-010/018、VIS-013、UX-005/006、GOV-001–006、DEL-001。
本次原样保全提交:761a2331f5898bacada8d9665da517c816af2367;作者原稿基点 b95ce798b4a9455d941d247a46865b3ac202bf2c,实际追加基点 f259bd34e19959db210578b11076e6f29a6bd15e。仅作者补丁所列6文件,业务代码仅 web/src/voice/session.ts;未改视觉、后台或HEL-896。补丁哈希及6文件SHA-256与独立MANIFEST、源码包逐字节匹配,git apply反向检查通过;总管负例与上一轮文件逐字节一致。源码包其余127文件经CRLF/LF规范化匹配;源码包携带旧source-manifest,按作者交接保留实际分支从04eff611补全的版本,逐字节核对未回退。另在本记录及preservation.json保存证据,不改作者handoff中的历史未测记录。
独立实际执行:
- 使用相同的33项语音测试,仅将隔离副本session.ts替换为b95ce798原版:5失败/28通过。原负例仍为Expected listening / Received off;交错start负例有两间未释放房间。
- 修后完整8文件88/88单测通过(包含上述33项)。typecheck通过;lint 0错/1条既有警告;build通过。沿用同锁文件的本机既有node_modules复制树,没有安装或下载。
- Python原版scripts/check_requirements.py:对origin/main、实际总装origin/hel-870-assembly(04eff611)、后台origin/agent/2/6a08a4c18f23(aba2aeb)、本次实际追加目标origin/hel-878-voice-wiring(f259bd3),各不带handoff、带handoff-HEL-878、带handoff-HEL-897,共12次通过。各精确提交在preservation.json。
- 作者的无头Edge浏览器49/49及修前45/49已随卡交证据;本轮未独立复跑浏览器,不把它当真WebRTC、真实麦克风或真机证据。
代码核对:fail同步让出旧会话所有权,启动仅清旧资源的teardown后立即提交本次OFF提示,清理等待后不再提交状态;start在await前取得代次,清理旧会话后先核对是否过期。负例与新增回归覆盖迟到DELETE/settle、掉线、撤销权限、worker超时、正常结束慢DELETE、交错start和迟到麦克风授权;原旧会话资源与新会话存活断言保持。
本次有限返修可交HEL-879独立统一审核,不代表完整R5通过。完整需求原话已核对R5/D1/D3、视觉分工、动态认可、成熟复用、完整体验指令;CG-011主动开口仍为后台no-op、增量字幕仍缺,须列有限接缝修正;真实远端断线清理/租约/API重启、SDK回调和播放回执、真实三轮/打断/停麦/HTTPS/WSS/Safari全未测。R6/R7、CG-028/029继续主卡承接。
推进顺序:总工HEL-896仍在运行,不能并行修改其母线;HEL-879接到此保全候选后,待HEL-896交回且总管核对实际母线再启动统一审核,避免覆盖新预算/发布成果。真实调用、音色、设备路径、预算和部署仍按主卡最终批准,默认真实cap=0。不合并、不部署、不碰旧服务/档案/凭据/运行时绑定。
+43
View File
@@ -0,0 +1,43 @@
{
"author_comment": "01a1798b-60b0-7562-9a70-82584f52dd54",
"baseline_version": "1.11",
"author_base": "b95ce798b4a9455d941d247a46865b3ac202bf2c",
"applied_to": "f259bd34e19959db210578b11076e6f29a6bd15e",
"patch_sha256": "1d8c43b0fe9984066406e60fac715764dcd375546713deb6bd034160f2050070",
"files": [
{
"path": "docs/requirements/handoff-HEL-897.json",
"sha256": "56cf0e97b5035ccfaaff3f7dd219ca30352df600fa4c9684eb690e4e44731bc9"
},
{
"path": "web/src/voice/session.ts",
"sha256": "4b74731b7dc34534ff8f046b9487e115b3a38cfb202b045e34a12fa27fe528fe"
},
{
"path": "web/tests/hel878_mock.mjs",
"sha256": "1b1608d5e0858842e9a0cf51faaf9baece5e52b546757464b046df0e66a4b815"
},
{
"path": "web/tests/hel878_shots.mjs",
"sha256": "e5706c5a4459315ecdb991cbd8d3776db3efe035fe520930ffb09e726eb639c7"
},
{
"path": "web/tests/review878_late_cleanup.test.ts",
"sha256": "65261cd249a36693d0c6b06ce279de8a60aa74dd7baafad6999d2fec790f9935"
},
{
"path": "web/tests/voice.test.ts",
"sha256": "4c120b9449ce3af0b66b3b8a18668d5012d286d6c18c0dee1347a880f7780471"
}
],
"archive_other_files_matched_normalized_lf": 127,
"archive_extras": [],
"source_manifest": "archive retains older manifest; actual branch retains 04eff611 authoritative manifest as author instructed; bytewise verified",
"preserved_commit": "761a2331f5898bacada8d9665da517c816af2367",
"requirement_ref_commits": {
"origin/main": "3a986726d8593e0ed2274f79db6cc5d807d2ebf0",
"origin/hel-870-assembly": "04eff611e7d85e539d43717711172441420ac853",
"origin/agent/2/6a08a4c18f23": "aba2aebeb08d87854b5b037b5e69d76ddfba446e",
"origin/hel-878-voice-wiring": "f259bd34e19959db210578b11076e6f29a6bd15e"
}
}
@@ -0,0 +1,159 @@
RUN v3.1.2 /home/xiaomo/multica_workspaces/hello-af23464d222e/hel-897-cfe565d5b1bd/workdir/baseline-web
❯ tests/review878_late_cleanup.test.ts (1 test | 1 failed) 12ms
× old room disconnect cleanup cannot overwrite a newly opened call 11ms
→ expected 'off' to be 'listening' // Object.is equality
❯ tests/voice.test.ts (32 tests | 4 failed) 49ms
✓ honest switch > reads status only: no room, no microphone 4ms
✓ honest switch > configured && tts_ok never means "verified": voice:false is not read as connected 1ms
✓ honest switch > refuses honestly before touching the microphone when the server cannot speak 1ms
✓ honest switch > an insecure page is told why, without asking for the microphone 0ms
✓ lifecycle > opens, joins, publishes the microphone, follows her state; ending stops everything 1ms
✓ lifecycle > permission denied: nothing opened on the server, nothing left running 1ms
✓ lifecycle > server refusal shows its own words and releases the microphone 0ms
✓ lifecycle > a failed join releases the microphone and gives the room back 0ms
✓ lifecycle > ending while the microphone prompt is open: the late microphone is stopped, no room is asked for 2ms
✓ lifecycle > ending while the room is being opened: the late grant is given back, never joined 3ms
✓ lifecycle > ending while joining: the join is aborted, a late room is closed, the server room deleted 3ms
✓ lifecycle > rapid on/off/on: only the last call stays, every earlier microphone and room is released 3ms
✓ lifecycle > late events from an ended call never bring it back 2ms
✓ lifecycle > the room dropping on its own (network, server ended it) cleans up and says so 1ms
✓ lifecycle > microphone permission revoked mid-call ends the call 1ms
✓ lifecycle > her worker never joining is a failure, not an endless wait 2ms
✓ lifecycle > offline ends with its own words; unload releases at once and the DELETE survives the page 2ms
✓ lifecycle > a DELETE that cannot get through (offline) is owed, survives a new page, and is sent once back online 1ms
✓ lifecycle > an owed room already gone on the server (404) counts as settled 0ms
✓ lifecycle > never confirms a receipt or posts chat itself: the worker owns both 0ms
× a late end of an older call > the room dropped, its DELETE (and the settle retry) answering after a new call is up, leaves the new call alone 6ms
→ expected { phase: 'listening', heard: '', …(3) } to match object { phase: 'off' }
(4 matching properties omitted from actual)
× a late end of an older call > the microphone was taken away, its DELETE (and the settle retry) answering after a new call is up, leaves the new call alone 2ms
→ expected { phase: 'listening', heard: '', …(3) } to match object { phase: 'off' }
(4 matching properties omitted from actual)
× a late end of an older call > her worker never joined, the DELETE is slow, and a new call is started before it answers 1ms
→ expected { phase: 'joining', heard: '', …(3) } to match object { phase: 'off', …(1) }
(3 matching properties omitted from actual)
✓ a late end of an older call > a normal end whose DELETE is slow, then a new call, then the old answer arrives 2ms
× a late end of an older call > starting again while the previous start still waits for the old room to be given back: only the newest call survives 5ms
→ expected [ Room{ closed: +0, …(3) }, …(1) ] to deeply equal [ Room{ closed: +0, …(3) } ]
✓ a late end of an older call > a microphone granted late to an older start is stopped without touching the new call 3ms
✓ subtitles and the two channels > interim is a draft; user_final cancels a text reply and asks for the record; φ is heard but not recorded 0ms
✓ subtitles and the two channels > without the agent state attribute, subtitles still move the state honestly 0ms
✓ subtitles and the two channels > typing over her spoken answer silences what is left of it until she is listening again 0ms
✓ record re-read > adds voice turns once, in id order; leaves reach-outs to the inbox and text turns to the stream 0ms
✓ record re-read > a pending voice row settles in place (sent or cancelled), never duplicated 0ms
✓ record re-read > reads newest pages down to what the page holds, bounded 1ms
Test Files 2 failed (2)
Tests 5 failed | 28 passed (33)
Start at 02:51:41
Duration 268ms (transform 81ms, setup 0ms, collect 150ms, tests 61ms, environment 0ms, prepare 76ms)
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 5 ⎯⎯⎯⎯⎯⎯⎯
FAIL tests/review878_late_cleanup.test.ts > old room disconnect cleanup cannot overwrite a newly opened call
AssertionError: expected 'off' to be 'listening' // Object.is equality
Expected: "listening"
Received: "off"
❯ tests/review878_late_cleanup.test.ts:39:29
37| await new Promise(resolve => setTimeout(resolve, 0));
38| expect(controller.active).toBe(true);
39| expect(view?.phase).toBe('listening');
| ^
40| } finally {
41| releaseOld();
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/5]⎯
FAIL tests/voice.test.ts > a late end of an older call > the room dropped, its DELETE (and the settle retry) answering after a new call is up, leaves the new call alone
FAIL tests/voice.test.ts > a late end of an older call > the microphone was taken away, its DELETE (and the settle retry) answering after a new call is up, leaves the new call alone
AssertionError: expected { phase: 'listening', heard: '', …(3) } to match object { phase: 'off' }
(4 matching properties omitted from actual)
- Expected
+ Received
{
- "phase": "off",
+ "phase": "listening",
}
❯ tests/voice.test.ts:274:30
272| const r = await connected(), slow = hold(r, 's1');
273| cause(r); await flush();
274| expect(r.view()).toMatchObject({ phase: 'off' });
| ^
275| expect(r.transport.mics[0].stopped).toBeGreaterThan(0); …
276| expect(r.c.active).toBe(false);
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/5]⎯
FAIL tests/voice.test.ts > a late end of an older call > her worker never joined, the DELETE is slow, and a new call is started before it answers
AssertionError: expected { phase: 'joining', heard: '', …(3) } to match object { phase: 'off', …(1) }
(3 matching properties omitted from actual)
- Expected
+ Received
{
- "notice": "她没有接上这次通话,麦克风已经关闭。文字仍可使用。",
- "phase": "off",
+ "notice": "",
+ "phase": "joining",
}
❯ tests/voice.test.ts:293:26
291| const r = rig(); await r.c.start(); const slow = hold(r, 's1');
292| await vi.advanceTimersByTimeAsync(1100);
293| expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.n…
| ^
294| await reopened(r, 1);
295| slow.open(); await vi.advanceTimersByTimeAsync(0);
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/5]⎯
FAIL tests/voice.test.ts > a late end of an older call > starting again while the previous start still waits for the old room to be given back: only the newest call survives
AssertionError: expected [ Room{ closed: +0, …(3) }, …(1) ] to deeply equal [ Room{ closed: +0, …(3) } ]
- Expected
+ Received
@@ -14,6 +14,23 @@
"ended": [Function anonymous],
"stopped": 0,
},
],
},
+ Room {
+ "closed": 0,
+ "hushed": [],
+ "on": {
+ "agentJoined": [Function agentJoined],
+ "agentState": [Function agentState],
+ "audioBlocked": [Function audioBlocked],
+ "dropped": [Function dropped],
+ "subtitle": [Function subtitle],
+ },
+ "published": [
+ Mic {
+ "ended": [Function anonymous],
+ "stopped": 0,
+ },
+ ],
+ },
]
❯ tests/voice.test.ts:316:58
314| slow.open(); await Promise.all([b, c]); await flush(); // B w…
315| const last = r.transport.rooms.length - 1;
316| expect(r.transport.rooms.filter(x => !x.closed)).toEqual([r.tr…
| ^
317| expect(r.transport.mics.filter(m => !m.stopped)).toEqual([r.tr…
318| expect(r.server.open.size).toBe(1);
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/5]⎯
+13
View File
@@ -0,0 +1,13 @@
> cybergril-web@0.1.0 build
> tsc --noEmit && vite build
vite v6.3.6 building for production...
transforming...
✓ 57 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html 0.49 kB │ gzip: 0.35 kB
dist/assets/index-BpZaybfD.css 83.54 kB │ gzip: 17.53 kB
dist/assets/index-BzbZ2t4t.js 306.87 kB │ gzip: 102.13 kB
✓ built in 620ms
+10
View File
@@ -0,0 +1,10 @@
> cybergril-web@0.1.0 lint
> eslint src
/home/xiaomo/multica_workspaces/hello-af23464d222e/hel-897-cfe565d5b1bd/workdir/CyberGril/web/src/chat/ChatPanel.tsx
222:100 warning The ref value 'fullButton.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'fullButton.current' to a variable inside the effect, and use that variable in the cleanup function react-hooks/exhaustive-deps
✖ 1 problem (0 errors, 1 warning)
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/main --handoff docs/requirements/handoff-HEL-878.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/main --handoff docs/requirements/handoff-HEL-897.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/main
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-870-assembly --handoff docs/requirements/handoff-HEL-878.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-870-assembly --handoff docs/requirements/handoff-HEL-897.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-870-assembly
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/agent/2/6a08a4c18f23 --handoff docs/requirements/handoff-HEL-878.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/agent/2/6a08a4c18f23 --handoff docs/requirements/handoff-HEL-897.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/agent/2/6a08a4c18f23
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-878-voice-wiring --handoff docs/requirements/handoff-HEL-878.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-878-voice-wiring --handoff docs/requirements/handoff-HEL-897.json
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,2 @@
python3 scripts/check_requirements.py --base-ref origin/hel-878-voice-wiring
需求结构检查通过:60项;来源和覆盖完整。仍须人工核对授权语义与实际体验。
@@ -0,0 +1,4 @@
> cybergril-web@0.1.0 typecheck
> tsc --noEmit
@@ -0,0 +1,21 @@
> cybergril-web@0.1.0 test
> vitest run
RUN v3.1.2 /home/xiaomo/multica_workspaces/hello-af23464d222e/hel-897-cfe565d5b1bd/workdir/CyberGril/web
✓ tests/r3memory.test.ts (5 tests) 7ms
✓ tests/review878_late_cleanup.test.ts (1 test) 5ms
✓ tests/real.test.ts (3 tests) 11ms
✓ tests/domain.test.ts (12 tests) 9ms
✓ tests/inbox.test.ts (13 tests) 24ms
✓ tests/r2wiring.test.ts (11 tests) 27ms
✓ tests/reading.test.ts (11 tests) 34ms
✓ tests/voice.test.ts (32 tests) 51ms
Test Files 8 passed (8)
Tests 88 passed (88)
Start at 02:51:00
Duration 305ms (transform 533ms, setup 0ms, collect 875ms, tests 168ms, environment 1ms, prepare 331ms)
+2
View File
@@ -32,4 +32,6 @@ python3 -m pytest -q # 现有 Python 核心,仓库根目录
脚本在随机 loopback 端口同步测试静态产物,结束关闭浏览器和服务;截图写入 `docs/frontend/screenshots/`(不进入前端产物)。没有持续预览服务或部署。真实手机、Safari、真实麦克风/摄像头、ASR/TTS、后台生活和口型均未测试。
语音(HEL-878,R5 前端接线):交谈页麦克风按钮按 `GET /api/voice/status` 诚实显示;用户确认后才申请麦克风、`POST /api/voice/session` 取短时 token,用 `public/vendor/` 下既有 livekit-client 加入房间(来源见 `VENDOR.md`)。状态、字幕、结束与清理逻辑在 `src/voice/`。离线检查:`npm test`(含 `tests/voice.test.ts`),构建后 `node tests/hel878_mock.mjs 8878` + `node tests/hel878_shots.mjs <msedge.exe> <outDir> 8878`(假 LiveKit + 浏览器假音频设备,不是真实通话)。真实通话、真机、Safari、回声与打断时延均未测。
接线边界、交付与测试记录见 `docs/frontend/IMPLEMENTATION.md`。
+9
View File
@@ -0,0 +1,9 @@
Vendored browser client (HEL-878)
`public/vendor/livekit-client-2.22.4.umd.min.js`
- What: livekit-client 2.22.4, UMD build (global `LivekitClient`), Apache-2.0. The file header names its origin: jsdelivr `/npm/livekit-client@2.22.4/dist/livekit-client.umd.js`.
- Where it came from: the existing .12 call page, `https://192.168.200.12:8090/static/livekit-client.umd.min.js` (the page that verified LAN microphone calls), read once with a plain GET on 2026-10-26. Not downloaded from npm or a CDN, nothing installed.
- SHA-256: `040DB4C0DB72E7F7599CD0E06B0F58F95E0082A395CDDE410CA26224A79A0626` (598 796 bytes).
- Loaded only when the user switches voice on (`src/voice/livekit.ts`, same origin, no CDN at run time). Text and demo paths never fetch it.
- Not an npm dependency: package.json and the lock file are unchanged. Upgrading means replacing this file and the name in `LIVEKIT_SRC`.
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -79,7 +79,7 @@ export function RealApp() {
useEffect(() => { if (fresh && !unread.some(m => m.id === fresh.id)) setFresh(null); }, [fresh, unread]);
// Reply to her: one new message plays on the stage; several open the record at the first one you have not seen.
const openUnread = () => { setFresh(null); const first = unread[0]; leaveSettingsThen(() => { setArea('talk'); setFocusLife(''); setLog(unread.length > 1); setFocusIds(unread.length > 1 && first ? [first.id] : []); }); };
const ignoreVoice = useCallback(() => {}, []);
const ignoreVoice = useCallback(() => {}, []), [voiceOn, setVoiceOn] = useState(false);
// Status words follow the server, re-read when you open a page that shows them; a failed read keeps the last one.
const life = lifeStatus(health);
const refreshHealth = useCallback(() => { api<Health>('health').then(setHealth, () => {}); }, []);
@@ -100,12 +100,12 @@ export function RealApp() {
const lastReply = state ? [...state.messages].reverse().find(m => m.role === 'companion') : undefined;
const talking = area === 'talk' && !!state && !creating;
return <><Presenter real scene={{ mode: area === 'talk' ? 'talk' : 'daily', activity: '文字已接通', time_band: '', weather: '', outfit: '' }} space={space} theme={theme} instant={instant} observation="" now={state && !creating ? life.now : ''} lifeLabel={life.wired ? life.label : undefined} onApproach={() => navigate('talk')}/>
<header className="topbar"><div className="who"><i className="dot"/><span className="name" title={state?.identity.name}>{state?.identity.name || 'CyberGril'}</span>{talking && <span className="presence">{presence}</span>}<span className="state">{ready ? '真实档案 · 文字对话' : '连接真实服务'}</span></div><div className="env"><span className="chip glass weather">天气尚未接通</span><span className="theme-switch glass">{(['moon', 'fog'] as Theme[]).map(t => <button key={t} data-t={t} aria-label={t === 'moon' ? '靛蓝月光' : '蓝紫雾光'} aria-pressed={theme === t} onClick={() => setTheme(t)}><i/><span>{t === 'moon' ? '月光' : '雾光'}</span></button>)}</span>{talking && <button className="end-talk" onClick={() => navigate('now')}><Icon name="x"/><span>结束交谈</span></button>}</div></header>
<header className="topbar"><div className="who"><i className="dot"/><span className="name" title={state?.identity.name}>{state?.identity.name || 'CyberGril'}</span>{talking && <span className="presence">{presence}</span>}<span className="state">{!ready ? '连接真实服务' : talking && voiceOn ? '真实档案 · 语音(未经真机验收)' : '真实档案 · 文字对话'}</span></div><div className="env"><span className="chip glass weather">天气尚未接通</span><span className="theme-switch glass">{(['moon', 'fog'] as Theme[]).map(t => <button key={t} data-t={t} aria-label={t === 'moon' ? '靛蓝月光' : '蓝紫雾光'} aria-pressed={theme === t} onClick={() => setTheme(t)}><i/><span>{t === 'moon' ? '月光' : '雾光'}</span></button>)}</span>{talking && <button className="end-talk" onClick={() => navigate('now')}><Icon name="x"/><span>结束交谈</span></button>}</div></header>
{!ready ? <main className="entry"><div className="card glass"><h1>一扇亮着的窗。</h1><Status text={error || '正在读取服务器档案…'} error={!!error}/>{error && <><p>请检查真实服务是否可达。资料仍在服务器保存。</p><Button primary onClick={() => void initialize()}>重新连接</Button></>}</div></main> : <>
{creating && <CreationWizard real fail={false} onCreate={setState} onEnter={() => setCreating(false)}/>}
{!creating && state && <>
{area === 'now' && <main className="home"><div className="eyebrow">一扇亮着的窗</div><h1 className="whisper">慢慢说,我在这里。</h1><div className="meta"><span><Icon name="heart"/>{state.identity.relation} · 慢慢熟悉</span></div><button className="away" onClick={() => navigate('days')}>日子与回忆 · {life.label} <Icon name="next"/></button>{unread.length ? <button className="letter glass" onClick={openUnread}><span className="lh"><b>她留给你的</b><span>{unread.length > 1 ? `${unread.length} 句还没看` : '还没看'}</span></span><p>{unread[unread.length - 1].text}</p></button> : lastReply && <button className="letter glass read" onClick={() => { navigate('talk'); setLog(true); }}><span className="lh"><b>最近说过的话</b><span>打开完整记录</span></span><p>{lastReply.text}</p></button>}<div className="cta"><Button primary small={false} onClick={() => navigate('talk')}><Icon name="chat"/>和她说话</Button></div><p className="first-note">{memoryNote(health, life)}</p>{health && !health.llm.ok && <Status error text="模型暂时不可达,档案和历史仍可查看。"/>}</main>}
{area === 'talk' && <ChatPanel real state={state} onChange={changeMessages} onPresence={setPresence} onVoice={ignoreVoice} onObservation={ignoreVoice} onRecord={setRecord} offline={false} fail={false} log={log} setLog={setLog} focusLife={focusLife} focusIds={focusIds} onReceipt={acknowledged} incoming={incoming} onAdmit={admitted}/>}
{area === 'talk' && <ChatPanel real state={state} onChange={changeMessages} onPresence={setPresence} onVoice={setVoiceOn} onObservation={ignoreVoice} onRecord={setRecord} offline={false} fail={false} log={log} setLog={setLog} focusLife={focusLife} focusIds={focusIds} onReceipt={acknowledged} incoming={incoming} onAdmit={admitted}/>}
<LifePanel real state={state} open={area === 'days'} now={life.now} life={life} ackAt={ackAt} onClose={() => navigate('now')} onChat={openTold} onSaid={openSaid} onSettings={() => { navigate('settings'); setHerFocus(n => n + 1); }}/>
<ManagePanel real state={state} open={area === 'settings'} now={life.now} life={life} focus={herFocus} onClose={() => navigate('now')} onSave={setState} fail={false} theme={theme} setTheme={setTheme}/>
{/* Same card as the approved demo reach-out, now with her real words; it never opens the conversation by itself. */}
+90 -8
View File
@@ -10,6 +10,9 @@ import { snap } from '../shared/motion';
import { motionOff } from '../stage/roomScene';
import { LineStage, type PresentHow } from './lineStage';
import { anchorOf, cursorAt, groupTurns, pagesOf, ReadingClock, type Anchor, type Metrics, type ShownMessage } from './reading';
import { mergeTurns, OFF, recentRows, VoiceController, voiceStatus, WORDS, type VoiceView } from '../voice/session';
import { loadTransport } from '../voice/livekit';
import { newestId, serverId } from '../adapters/inbox';
const START: Anchor = { mi: 0, offset: 0 };
const metricsFor = (width: number): Metrics => { const compact = document.documentElement.classList.contains('compact'); return { width, font: compact ? 16 : innerWidth <= 720 ? 18 : 23, compact }; };
@@ -47,6 +50,51 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
const name = state.identity.name;
const change = (messages: Message[]) => { live.current = messages; onChange(messages); };
// ── Real voice (R5). The controller owns the call; this panel shows its state and keeps the record in step. ──
const [voiceView, setVoiceView] = useState<VoiceView>(OFF), [voiceOff, setVoiceOff] = useState('');
const voiceCtl = useRef<VoiceController | null>(null), busyNow = useRef(busy); busyNow.current = busy;
// Your voice turn and her spoken answer reach the record only as server rows (subtitles carry no message id).
// Rows still pending on the page are read again until they settle.
const recordSync = useRef({ running: false, again: false, timer: 0 as ReturnType<typeof setTimeout> | 0 });
const reread = useRef(() => {});
reread.current = () => {
const sync = recordSync.current;
if (sync.running) { sync.again = true; return; }
sync.running = true; sync.again = false;
const pending = live.current.filter(m => m.status === 'pending').map(serverId).filter(Boolean);
const since = Math.min(newestId(live.current), ...pending.map(id => id - 1));
recentRows(since, new AbortController().signal).then(rows => { const next = mergeTurns(live.current, rows); if (next) change(next); }, () => { /* The next turn or the inbox read tries again. */ })
.finally(() => { sync.running = false; if (sync.again) reread.current(); });
};
const spoke = useRef(() => {});
// Speaking cancels a text reply still streaming (the server's turn gate already did); it is kept as a cut fragment.
spoke.current = () => { if (busyNow.current) stop(true); };
useEffect(() => {
if (!real) return;
const sync = recordSync.current;
const c = new VoiceController({ transport: loadTransport, onView: setVoiceView, onSpoke: () => spoke.current(), onRecord: () => { clearTimeout(sync.timer); sync.timer = setTimeout(() => reread.current(), 250); } });
voiceCtl.current = c;
const gone = () => c.unload(), offline = () => { if (c.active) void c.end(WORDS.offline); }, online = () => void c.settle();
addEventListener('pagehide', gone); addEventListener('offline', offline); addEventListener('online', online); online();
// Leaving the conversation (nav, Esc, 结束交谈) unmounts this panel: the call ends with it.
return () => { removeEventListener('pagehide', gone); removeEventListener('offline', offline); removeEventListener('online', online); voiceCtl.current = null; void c.end(); };
}, [real]);
// The switch reads the server's honest state once on arrival; it never opens a room or touches the microphone.
useEffect(() => {
if (!real) return;
const c = new AbortController();
voiceStatus(c.signal).then(s => setVoiceOff(s.available ? '' : s.reason), () => {});
return () => c.abort();
}, [real]);
const voiceOn = real ? voiceView.phase !== 'off' : voice;
const voiceLabel = { off: '', opening: '正在接通…', joining: `在等${name}接起…`, listening: '在听你说', thinking: `${name}在想`, speaking: `${name}在说 · 开口就能打断` }[voiceView.phase];
useEffect(() => {
if (!real) return;
onVoice(voiceView.phase !== 'off');
if (voiceView.phase !== 'off') onPresence(voiceLabel);
else if (voiceView.notice) { onPresence('在你面前'); setNotice(voiceView.notice); }
}, [real, voiceView.phase, voiceView.notice, voiceLabel, onVoice, onPresence]);
// ── What the stage shows: the newest turn of the (already privacy-filtered) record, plus the live reply. ──
// Real messages are trimmed by the server before transmission; their lifeIds point at records this page has not
// necessarily loaded, so the demo's local filter would wrongly hide them.
@@ -78,10 +126,10 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
// Her own message that arrives while you are here waits its turn: never while she is answering you, a sheet is
// open, you paused or went back, or the line on screen has not had its time. Then it comes in as a new turn.
const waiting = useRef({ count: 0, busy: false, sheet: false }), admit = useRef(() => {});
waiting.current = { count: incoming.length, busy, sheet: log || full };
waiting.current = { count: incoming.length, busy: busy || voiceView.phase === 'thinking' || voiceView.phase === 'speaking', sheet: log || full };
admit.current = () => { const add = incoming.filter(m => !live.current.some(k => k.id === m.id)); onAdmit?.(incoming.map(m => m.id)); if (add.length) change([...live.current, ...add]); };
function stop() { gate.current.cancel(); if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnLive.current }); replyLive.current = ''; setBusy(false); setReply(''); onPresence('停下来了'); change(live.current.map(m => m.status === 'pending' ? { ...m, status: 'cancelled' } : m)); }
function stop(quiet = false) { gate.current.cancel(); if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnLive.current }); replyLive.current = ''; setBusy(false); setReply(''); if (!quiet) onPresence('停下来了'); change(live.current.map(m => m.status === 'pending' ? { ...m, status: 'cancelled' } : m)); }
function endVoice() { stop(); voiceController.current?.abort(); setVoice(false); onVoice(false); onPresence('在你面前'); }
useEffect(() => { const current = gate.current; return () => { current.cancel(); if (real) onChange(live.current.map(m => m.status === 'pending' ? { ...m, status: 'cancelled' } : m)); voiceController.current?.abort(); onVoice(false); onObservation(''); }; }, [onVoice, onObservation, onChange, real]);
useEffect(() => { try { sessionStorage.setItem(draftKey, draft); } catch { /* Draft remains in memory. */ } }, [draft, draftKey]);
@@ -184,6 +232,8 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
if (!retry && real) retry = [...live.current].reverse().find(m => m.role === 'user' && m.status === 'failed' && m.text === draft.trim());
const text = retry?.text || draft.trim(); if (!text || offline) return;
stop();
// Typing over her spoken answer: the server cancels that voice turn; what is left of its audio stays silent.
voiceCtl.current?.hush();
const turnId = retry?.turnId || newId(), signal = gate.current.begin(turnId);
const msg: Message = { id: retry?.id || newId(), role: 'user', text, lifeIds: [], turnId, status: 'pending' };
sentTurn.current = turnId;
@@ -207,12 +257,26 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
replyLive.current = ''; setInterrupted(null); setReply('');
if (real) setDraft(current => current === text ? '' : current);
setBusy(false);
onPresence(voice ? '在听你说(模拟)' : '在你面前');
onPresence(real ? (voiceCtl.current?.active ? '在听你说' : '在你面前') : voice ? '在听你说(模拟)' : '在你面前');
}
}, fail);
}
catch (e) {
if (signal.aborted) return;
// A stream that ends without its final is not always a broken connection: a newer turn (your voice, or
// another page) takes the server's turn gate and cancels this one. The server's record says which.
if (real) {
const row = await recentRows(0, new AbortController().signal, 1).then(rows => rows.find(m => m.role === 'user' && m.turnId === turnId), () => undefined);
if (signal.aborted) return;
if (row?.status === 'cancelled') {
gate.current.cancel();
if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnId });
replyLive.current = ''; setReply(''); setBusy(false);
change(live.current.map(m => m.id === msg.id ? { ...m, status: 'cancelled' } : m));
if (!voiceCtl.current?.active) onPresence('在你面前');
return;
}
}
change(live.current.map(m => m.id === msg.id ? { ...m, status: 'failed' } : m));
setBusy(false);
if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnId });
@@ -222,7 +286,17 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
onPresence('在你面前');
}
}
const setVoiceMode = () => { if (real) { setNotice('语音尚未接通,文字仍可使用。'); return; } if (voice) { endVoice(); return; } setPermission('mic'); };
const setVoiceMode = () => {
if (real) {
const c = voiceCtl.current; if (!c) return;
if (c.active) { void c.end(); setNotice('语音已结束,麦克风已关闭。'); return; }
// Ask the server again: its state may have changed since the page opened (no room, no microphone yet).
setNotice('');
voiceStatus(AbortSignal.timeout(8000)).then(s => { setVoiceOff(s.available ? '' : s.reason); if (s.available) { setDenied(false); setPermission('mic'); } else setNotice(s.reason); }, () => setNotice(WORDS.unknown));
return;
}
if (voice) { endVoice(); return; } setPermission('mic');
};
const lastUser = [...view.messages].reverse().find(m => m.role === 'user');
// Only says why her new words wait when it is your own reading that holds them; otherwise they simply come in.
const queued = incoming.length && !busy ? (!flow ? `${name}刚又说了一句 · 继续后呈现` : !atEnd ? `${name}刚又说了一句 · 读完这段呈现` : '') : '';
@@ -239,7 +313,14 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
useLayoutEffect(() => { conversation.current?.toggleAttribute('inert', log || full); }, [log, full]);
return <><section ref={conversation} className={`conversation ${single ? 'single-page' : ''} ${flow ? '' : 'reading-paused'} ${holding && flow ? 'reading-held' : ''}`} aria-label="当前对话">
{voice && <div className="voice-row"><span className="voice-wave" aria-hidden="true"><i/><i/><i/><i/></span><span>{busy ? '她在想(模拟)' : '在听你说(模拟,无收音)'}</span><button onClick={() => { stop(); setNotice(fail ? '刚才没听清,你再说一遍?(模拟识别失败)' : '她停下来了,在等你。'); }}>打断她</button><button onClick={endVoice}>结束语音</button></div>}
{real && voiceOn && <div className="voice-row real" data-phase={voiceView.phase} role="status" aria-live="polite">
<span className="voice-wave" aria-hidden="true"><i/><i/><i/><i/></span>
<span className="voice-state">{voiceLabel}</span>
{voiceView.spoken ? <span className="voice-heard said" title={voiceView.spoken}>{name}:{voiceView.spoken}<em>未记入聊天</em></span> : voiceView.heard && <span className="voice-heard" title={voiceView.heard}>「{voiceView.heard}」</span>}
{voiceView.blocked && <button onClick={() => void voiceCtl.current?.resumeAudio()}>点一下听{name}的声音</button>}
<button className="voice-end" onClick={() => { void voiceCtl.current?.end(); setNotice('语音已结束,麦克风已关闭。'); }}>结束语音</button>
</div>}
{!real && voice && <div className="voice-row"><span className="voice-wave" aria-hidden="true"><i/><i/><i/><i/></span><span>{busy ? '她在想(模拟)' : '在听你说(模拟,无收音)'}</span><button onClick={() => { stop(); setNotice(fail ? '刚才没听清,你再说一遍?(模拟识别失败)' : '她停下来了,在等你。'); }}>打断她</button><button onClick={endVoice}>结束语音</button></div>}
<div className="stage" ref={stageRef} aria-hidden="true" onPointerEnter={e => { if (e.pointerType === 'mouse') hold('hover', true); }} onPointerLeave={() => hold('hover', false)}/>
<p className="sr" aria-live="polite" aria-atomic="true">{page ? `${page.message.role === 'user' ? '你' : name}:${page.text}` : ''}</p>
{status && <div className="talk-status" role="status"><span>{status}</span>{lastUser?.status === 'failed' && !busy && <button className="retry" onClick={() => void send(lastUser)}>重试</button>}</div>}
@@ -249,20 +330,21 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
<span className="count" aria-label={pages.length ? `第 ${cursor + 1} 句,共 ${pages.length} 句${busy ? ',还在说' : ''}` : '还没有对话'}><span>{pages.length ? `${cursor + 1} / ${pages.length}${busy ? '…' : ''}` : '0 / 0'}</span><i ref={meter}/></span>
<button className="icon" aria-label="下一句" disabled={atEnd} onClick={() => forward()}><Icon name="next"/></button>
<button className="read-all" ref={fullButton} disabled={!page} onClick={() => setFull(true)}>全文</button>
{busy && <button className="stop" onClick={stop}>停止回复</button>}
{busy && <button className="stop" onClick={() => stop()}>停止回复</button>}
{!flow && cursor < cursorAt(pages, head.current) && <button className="latest" onClick={toLatest}><Icon name="down"/><span>{unread > 0 ? `回到读到的地方 · 后面还有 ${unread} 句` : '回到读到的地方'}</span></button>}
</div>
<div className="composer"><button className={`round log-btn ${log ? 'on' : ''}`} ref={historyButton} aria-label="完整记录" aria-expanded={log} onClick={() => setLog(!log)}><Icon name="lines"/></button>
<textarea ref={textarea} aria-label="想和她说的话" rows={1} maxLength={4000} value={draft} placeholder="想和她说点什么…" onFocus={() => typing(true)} onBlur={() => typing(false)} onChange={e => setDraft(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) { e.preventDefault(); void send(); } }}/>
<button className={`round cam ${vision ? 'on' : ''}`} aria-label={real ? '视觉观察尚未接通' : '视觉观察(模拟)'} onClick={() => { if (real) { setNotice('视觉尚未接通,不采集影像。'); return; } if (vision) { setVision(false); onObservation('窗户关上了'); } else setPermission('vision'); }}><Icon name="eye"/></button>
<button className="round send" disabled={!draft.trim() || offline} aria-label="发送" onClick={() => void send()}><Icon name="up"/></button>
<button className={`voice-button ${denied ? 'denied' : ''}`} aria-label={real ? '语音尚未接通' : voice ? '停止语音' : '开始模拟语音'} disabled={offline} onClick={setVoiceMode}><Icon name={voice ? 'stop' : 'mic'}/></button>
<button className={`voice-button ${denied || (real && voiceOff && !voiceOn) ? 'denied' : ''} ${real && voiceOn ? 'on' : ''}`} aria-label={real ? voiceOn ? '结束语音' : voiceOff ? `语音不可用:${voiceOff}` : '开始语音' : voice ? '停止语音' : '开始模拟语音'} aria-pressed={real ? voiceOn : undefined} disabled={offline} onClick={setVoiceMode}><Icon name={voiceOn ? 'stop' : 'mic'}/></button>
</div>
</section>
<aside className="record log-panel glass read" hidden={!log} role="dialog" aria-modal="false" aria-labelledby="record-title"><header><div><p className="eyebrow">{real ? '服务器聊天记录' : '完整记录 · 本机演示'}</p><h2 id="record-title">你们说过的话</h2></div><button className="btn-round sm" ref={logClose} aria-label="关闭记录" onClick={() => setLog(false)}><Icon name="x"/></button></header>
<div className="scroll" ref={logRef}><p className="guide">内容完整保留;关闭后回到刚才那一句,自动呈现会接着走。</p>{record.map(m => <article key={m.id} data-life={m.lifeIds.join(' ') || undefined} data-mid={m.id} data-role={m.role} className={`message ${m.status === 'failed' ? 'fail' : ''} ${(focusLife && m.lifeIds.includes(focusLife)) || focusIds.includes(m.id) ? 'hl' : ''}`}><div className="author"><i/>{m.role === 'user' ? '你' : name}{m.proactive && <span className="tagm">她主动说的</span>}</div><p>{m.text}</p>{m.status === 'failed' && <button className="btn-text" onClick={() => void send(m)}>没发出去 · 重试</button>}{m.status === 'cancelled' && <span className="incomplete">已取消</span>}{m.live === 'cancelled' && <span className="incomplete">已停止 · 这是未完成片段,未存入聊天记录</span>}</article>)}{reply && <article className="message" data-role="companion"><div className="author"><i/>{name}</div><p>{reply}</p><span className="incomplete">还在说…</span></article>}</div></aside>
{full && fullMessage && <aside className="record full glass read" role="dialog" aria-modal="false" aria-labelledby="full-title"><header><div><p className="eyebrow">{fullMessage.role === 'user' ? '你' : name} · 全文</p><h2 id="full-title">这一段,慢慢看</h2></div><button className="btn-round sm" ref={fullClose} aria-label="关闭全文" onClick={() => setFull(false)}><Icon name="x"/></button></header>
<div className="scroll"><p className="guide">整段完整可读。关闭后回到刚才那一句。</p><article className="message" data-role={fullMessage.role}><div className="author"><i/>{fullMessage.role === 'user' ? '你' : name}</div><p>{fullMessage.text}</p>{fullMessage.live === 'generating' && <span className="incomplete">还在说…</span>}{fullMessage.live === 'cancelled' && <span className="incomplete">这段回复已停止,内容未完成,未存入聊天记录。</span>}</article></div></aside>}
{permission && <Modal title={permission === 'mic' ? '开启模拟语音?' : '打开模拟视觉窗?'} onClose={() => { setPermission(''); setDenied(true); setNotice('没有开启。真实服务尚未接通,文字仍可使用。'); }} confirm="开始模拟" onConfirm={() => { if (permission === 'mic') { setDenied(false); setVoice(true); onVoice(true); voiceController.current = new AbortController(); onPresence('在听你说(模拟)'); setNotice('模拟语音没有收音,可继续用文字输入。'); } else { if (fail) { onObservation('模拟 · 看不清,视觉服务未接通'); setNotice('模拟观察失败,文字仍可使用。'); } else setVision(true); } setPermission(''); }}>本次仅模拟界面状态,不申请麦克风或摄像头权限,不采集声音或影像,也不向外接服务发送请求。</Modal>}
{real && permission === 'mic' && <Modal title={`开始和${name}语音?`} onClose={() => { setPermission(''); setNotice('没有开启语音,文字仍可使用。'); }} confirm="开始语音" onConfirm={() => { setPermission(''); setNotice(''); void voiceCtl.current?.start(); }}>浏览器会请求麦克风权限。你的声音只送到这台服务器上{name}的语音服务,她用同一份记忆、同一段聊天回应你;你说的话和她的回答照常存进聊天记录。直接开口就能打断她。结束语音、离开交谈或关上页面时,麦克风立即关闭。<br/><small className="voice-note">语音通路还没有经过真机验收,效果可能不稳定。</small></Modal>}
{!real && permission && <Modal title={permission === 'mic' ? '开启模拟语音?' : '打开模拟视觉窗?'} onClose={() => { setPermission(''); setDenied(true); setNotice('没有开启。真实服务尚未接通,文字仍可使用。'); }} confirm="开始模拟" onConfirm={() => { if (permission === 'mic') { setDenied(false); setVoice(true); onVoice(true); voiceController.current = new AbortController(); onPresence('在听你说(模拟)'); setNotice('模拟语音没有收音,可继续用文字输入。'); } else { if (fail) { onObservation('模拟 · 看不清,视觉服务未接通'); setNotice('模拟观察失败,文字仍可使用。'); } else setVision(true); } setPermission(''); }}>本次仅模拟界面状态,不申请麦克风或摄像头权限,不采集声音或影像,也不向外接服务发送请求。</Modal>}
</>;
}
+21
View File
@@ -389,6 +389,26 @@ body.offline .composer .send{opacity:.4;pointer-events:none}
.voice-row{display:flex;align-items:center;justify-content:center;gap:12px;font-size:12px;color:var(--text-2);margin-bottom:6px;text-shadow:0 1px 10px var(--pool)}.voice-row button{color:var(--accent);padding:4px 2px}
.voice-wave{display:inline-flex;gap:3px;align-items:center;height:14px}.voice-wave i{width:2px;height:100%;background:var(--accent);border-radius:2px;animation:r-wave 1.1s var(--ease-in-out) infinite}.voice-wave i:nth-child(2){animation-delay:.15s}.voice-wave i:nth-child(3){animation-delay:.3s}.voice-wave i:nth-child(4){animation-delay:.45s}
@keyframes r-wave{50%{transform:scaleY(.3)}}
/* Real voice (HEL-878): one quiet glass line above the stage, same width language as the composer. The wave says who
has the floor without relying on colour alone (the words beside it always name the state): your open-ring colour
while she listens, her lit accent while she speaks, a slow breath while connecting or thinking. */
.voice-row.real{max-width:min(640px,100%);min-height:34px;padding:0 6px 0 14px;border-radius:17px;gap:10px;background:color-mix(in srgb,var(--surface) 34%,transparent);border:1px solid var(--glass-line);-webkit-backdrop-filter:blur(16px);backdrop-filter:blur(16px);animation:voice-in 280ms var(--ease-out) both}
@keyframes voice-in{from{opacity:0;transform:translateY(6px)}}
.voice-row.real .voice-state{white-space:nowrap;color:var(--text-1);letter-spacing:.04em}
.voice-row.real .voice-heard{min-width:0;max-width:24em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--user);opacity:.82}
.voice-row.real .voice-heard.said{color:var(--text-2)}
.voice-row.real .voice-heard em{font-style:normal;font-size:11px;color:var(--warn);margin-left:8px}
.voice-row.real button{white-space:nowrap;height:28px;padding:0 10px;border-radius:14px;transition:background-color 160ms var(--ease-out)}
.voice-row.real .voice-end{color:var(--text-1);box-shadow:inset 0 0 0 1px var(--glass-line)}
.voice-row.real .voice-wave i{transition:background-color var(--dur-ui) var(--ease-out),opacity var(--dur-ui) var(--ease-out)}
.voice-row[data-phase="opening"] .voice-wave i,.voice-row[data-phase="joining"] .voice-wave i,.voice-row[data-phase="thinking"] .voice-wave i{animation:r-breath 1.4s var(--ease-in-out) infinite;transform:scaleY(.35)}
.voice-row[data-phase="opening"] .voice-wave i,.voice-row[data-phase="joining"] .voice-wave i{background:var(--text-3)}
.voice-row[data-phase="listening"] .voice-wave i{background:var(--user);animation-duration:1.6s;opacity:.85}
.voice-row[data-phase="speaking"] .voice-wave i{box-shadow:0 0 6px var(--accent-glow)}
@keyframes r-breath{50%{opacity:.3}}
.voice-button.on{box-shadow:0 0 0 4px var(--accent-soft),0 0 18px var(--accent-glow)}
.voice-note{display:block;margin-top:10px;color:var(--warn);font-size:12px}
@media(hover:hover) and (pointer:fine){.voice-row.real button:hover{background:#ffffff0d}}
/* 完整记录与全文:房间里的阅读面,她仍在桌对面;不是模态框,导航始终可以直接切换 */
.record{position:fixed;z-index:20;top:calc(var(--view-top) + 84px);left:4vw;width:min(470px,36vw);height:calc(var(--view-h) - 150px - var(--safe-bottom));border-radius:26px;display:flex;flex-direction:column;overflow:hidden;box-shadow:0 25px 100px #02041270,inset 0 1px 0 #ffffff10;animation:sheet-in 240ms var(--ease-out)}
@@ -703,6 +723,7 @@ body.creating .create{opacity:1;pointer-events:auto}
.reader-bar .latest span{max-width:9em;overflow:hidden;text-overflow:ellipsis}
.composer{min-height:48px;padding:0 4px}.composer textarea{font-size:16px}
.composer .round{width:36px;height:36px}
.voice-row.real{width:100%;gap:8px;padding:0 4px 0 12px}.voice-row.real .voice-heard{flex:1;max-width:none}.voice-row.real button{height:34px}
.nav{padding:3px}
.nav-item{width:70px;height:40px}
+95
View File
@@ -0,0 +1,95 @@
// The existing LiveKit browser client (livekit-client 2.22.4, the same UMD build the .12 call page at :8090 serves from
// /static/). It is loaded from this site only when voice is switched on: the text and demo paths never fetch it.
// See web/VENDOR.md for its source and checksum.
import type { MicTrack, RoomEvents, VoiceRoom, VoiceTransport } from './session';
export const LIVEKIT_SRC = '/vendor/livekit-client-2.22.4.umd.min.js';
const SUBTITLES = 'cg.subtitle', AGENT_STATE = 'lk.agent.state';
// The few parts of the client used here (typed locally; the bundle carries no type package).
interface LkTrack { kind: string; mediaStreamTrack: MediaStreamTrack; attach(): HTMLMediaElement; detach(): HTMLMediaElement[]; stop(): void }
interface LkParticipant { identity: string; kind?: number; attributes?: Record<string, string> }
interface LkRoom {
on(event: string, cb: (...args: never[]) => void): LkRoom;
connect(url: string, token: string, options?: { autoSubscribe?: boolean }): Promise<void>;
disconnect(stopTracks?: boolean): Promise<void>;
startAudio(): Promise<void>;
canPlaybackAudio: boolean;
remoteParticipants: Map<string, LkParticipant>;
localParticipant: { publishTrack(track: LkTrack, options?: { source?: string }): Promise<unknown> };
}
export interface Livekit {
Room: new (options?: Record<string, unknown>) => LkRoom;
RoomEvent: Record<'TrackSubscribed' | 'TrackUnsubscribed' | 'ParticipantConnected' | 'ParticipantAttributesChanged' | 'DataReceived' | 'Disconnected' | 'AudioPlaybackStatusChanged', string>;
Track: { Source: { Microphone: string } };
ParticipantKind?: { AGENT?: number };
createLocalAudioTrack(options?: MediaTrackConstraints): Promise<LkTrack>;
}
declare global { interface Window { LivekitClient?: Livekit } }
let loading: Promise<Livekit> | null = null;
export function loadLivekit(src = LIVEKIT_SRC): Promise<Livekit> {
if (window.LivekitClient) return Promise.resolve(window.LivekitClient);
loading ??= new Promise<Livekit>((ok, ko) => {
const script = document.createElement('script');
script.src = src; script.async = true;
script.onload = () => window.LivekitClient ? ok(window.LivekitClient) : ko(new Error('livekit-client missing'));
script.onerror = () => { script.remove(); ko(new Error('livekit-client failed to load')); };
document.head.append(script);
}).catch(e => { loading = null; throw e; });
return loading;
}
const tracks = new WeakMap<MicTrack, LkTrack>();
export function livekitTransport(lk: Livekit): VoiceTransport {
const isAgent = (p: LkParticipant) => (lk.ParticipantKind?.AGENT !== undefined && p.kind === lk.ParticipantKind.AGENT) || AGENT_STATE in (p.attributes || {}) || p.identity.startsWith('agent-');
return {
async capture() {
// Same capture defaults as the :8090 call page: echo cancellation keeps her own voice out of the microphone.
const track = await lk.createLocalAudioTrack({ echoCancellation: true, noiseSuppression: true, autoGainControl: true });
const mic: MicTrack = { stop: () => { track.stop(); track.mediaStreamTrack.stop(); }, onEnded: cb => track.mediaStreamTrack.addEventListener('ended', cb, { once: true }) };
tracks.set(mic, track);
return mic;
},
async join(url, token, on: RoomEvents, signal) {
const room = new lk.Room({ adaptiveStream: true, dynacast: true });
// Her audio lives in one hidden holder; closing empties it, so nothing of an old call can keep playing.
const holder = document.createElement('div'), E = lk.RoomEvent;
holder.hidden = true; holder.dataset.voice = 'her'; document.body.append(holder);
let closing = false, hushed = false;
const players = () => [...holder.querySelectorAll<HTMLMediaElement>('audio,video')];
const close = async () => {
if (closing) return; closing = true;
players().forEach(el => { el.muted = true; el.pause(); el.srcObject = null; });
holder.remove();
await room.disconnect(true).catch(() => {});
};
room.on(E.TrackSubscribed, ((track: LkTrack) => { if (closing || track.kind !== 'audio') return; const el = track.attach(); el.muted = hushed; holder.append(el); }) as (...a: never[]) => void);
room.on(E.TrackUnsubscribed, ((track: LkTrack) => { track.detach().forEach(el => el.remove()); }) as (...a: never[]) => void);
room.on(E.ParticipantConnected, ((p: LkParticipant) => { if (!closing && isAgent(p)) on.agentJoined(); }) as (...a: never[]) => void);
room.on(E.ParticipantAttributesChanged, ((changed: Record<string, string>, p: LkParticipant) => { if (!closing && AGENT_STATE in changed && isAgent(p)) on.agentState(changed[AGENT_STATE]); }) as (...a: never[]) => void);
room.on(E.DataReceived, ((payload: Uint8Array, _p: unknown, _kind: unknown, topic?: string) => {
if (closing || topic !== SUBTITLES) return;
try { on.subtitle(JSON.parse(new TextDecoder().decode(payload))); } catch { /* A broken frame is dropped, never shown. */ }
}) as (...a: never[]) => void);
room.on(E.Disconnected, (() => { if (!closing) { void close(); on.dropped(); } }) as (...a: never[]) => void);
room.on(E.AudioPlaybackStatusChanged, (() => { if (!closing) on.audioBlocked(!room.canPlaybackAudio); }) as (...a: never[]) => void);
const abort = () => void close();
signal.addEventListener('abort', abort, { once: true });
try { await room.connect(url, token, { autoSubscribe: true }); }
catch (e) { await close(); throw e; }
if (signal.aborted) { await close(); throw new DOMException('ended', 'AbortError'); }
signal.removeEventListener('abort', abort);
room.remoteParticipants.forEach(p => { if (!isAgent(p)) return; on.agentJoined(); const state = p.attributes?.[AGENT_STATE]; if (state) on.agentState(state); });
void room.startAudio().catch(() => {}).then(() => { if (!closing && !room.canPlaybackAudio) on.audioBlocked(true); });
const voiceRoom: VoiceRoom = {
async publish(mic) { const track = tracks.get(mic); if (!track) throw new Error('unknown microphone'); await room.localParticipant.publishTrack(track, { source: lk.Track.Source.Microphone }); },
hush(quiet) { hushed = quiet; players().forEach(el => { el.muted = quiet; }); },
resumeAudio: () => room.startAudio(),
close,
};
return voiceRoom;
},
};
}
export const loadTransport = async (): Promise<VoiceTransport> => livekitTransport(await loadLivekit());
+299
View File
@@ -0,0 +1,299 @@
// ── Continuous voice on the approved conversation (R5, HEL-878). ──
// The server opens a room and hands out a short join token (POST /api/voice/session, keys never reach the page); the
// browser joins with the existing LiveKit client and publishes the microphone. Her words are spoken by the dedicated
// worker, which goes through the same /api/chat as text; the page never confirms a receipt for voice (the worker does,
// only after real playout) and never turns subtitles into chat rows: the record is re-read from /api/messages.
//
// One rule holds the whole lifecycle together: every call starts a new generation, and anything that answers for an
// older generation (a late token, a late join, a late microphone, a late room event) is cleaned up, never shown.
import type { Message } from '../domain/model';
import { api, toMessage, within, type MessagePage } from '../adapters/real';
import { serverId } from '../adapters/inbox';
export type VoicePhase = 'off' | 'opening' | 'joining' | 'listening' | 'thinking' | 'speaking';
export interface VoiceView {
phase: VoicePhase;
// What the recogniser heard of you so far (a draft; it is replaced, never stored).
heard: string;
// Her line spoken after a failure (φ): heard, not part of the record.
spoken: string;
// Why voice is off or what went wrong; empty while it simply runs.
notice: string;
// The browser held her audio back until a tap (autoplay rules); `resume` lets it out.
blocked: boolean;
}
export const OFF: VoiceView = { phase: 'off', heard: '', spoken: '', notice: '', blocked: false };
export interface Subtitle { kind: 'interim' | 'user_final' | 'assistant_final' | 'error'; role: 'user' | 'assistant'; text: string }
export function subtitle(v: unknown): Subtitle | null {
const s = v as Record<string, unknown> | null;
if (!s || typeof s !== 'object' || typeof s.text !== 'string' || !['interim', 'user_final', 'assistant_final', 'error'].includes(s.kind as string)) return null;
return { kind: s.kind as Subtitle['kind'], role: s.role === 'assistant' ? 'assistant' : 'user', text: s.text };
}
// ── What the controller needs from a real-time client. livekit.ts implements it with livekit-client; tests use fakes. ──
export interface MicTrack { stop(): void; onEnded(cb: () => void): void }
export interface RoomEvents {
agentJoined(): void;
// The worker's own state attribute (lk.agent.state): initializing / listening / thinking / speaking.
agentState(state: string): void;
subtitle(payload: unknown): void;
// The room went away without us closing it (server ended it, network, kicked).
dropped(): void;
audioBlocked(blocked: boolean): void;
}
export interface VoiceRoom { publish(mic: MicTrack): Promise<void>; hush(on: boolean): void; resumeAudio(): Promise<void>; close(): Promise<void> }
export interface VoiceTransport { capture(): Promise<MicTrack>; join(url: string, token: string, on: RoomEvents, signal: AbortSignal): Promise<VoiceRoom> }
export interface VoiceAvailability { available: boolean; reason: string }
export interface SessionGrant { url: string; token: string; room: string; session_id: string; epoch: number; expires_at: number }
export const WORDS = {
notConfigured: '语音还没有在这台服务上配置,文字仍可使用。',
ttsDown: '她的声音服务这会儿不在,稍后再试试。文字仍可使用。',
noRoute: '这台服务还没有语音通路,文字仍可使用。',
unknown: '暂时读不到语音状态,文字仍可使用。',
insecure: '这个地址不是安全连接,浏览器不允许使用麦克风。需要从 HTTPS 入口打开。',
denied: '没有拿到麦克风权限,语音没有开始。文字仍可使用。',
noMic: '没有找到可用的麦克风,语音没有开始。',
micBusy: '麦克风被别的程序占用了,语音没有开始。',
client: '语音组件没有加载出来,文字仍可使用。',
unreachable: '通话服务暂时连不上,稍后再试试。',
noAgent: '她没有接上这次通话,麦克风已经关闭。文字仍可使用。',
dropped: '通话断开了,麦克风已经关闭。文字仍可使用。',
micGone: '麦克风被关掉了,语音已结束。',
offline: '网络断开了,语音已结束,麦克风已关闭。',
} as const;
export interface VoiceDeps {
transport: () => Promise<VoiceTransport>;
onView: (view: VoiceView) => void;
// The record may have changed (a voice turn began, ended or was cut): re-read /api/messages.
onRecord: () => void;
// You finished saying something by voice: a text reply still streaming has been cancelled by the server.
onSpoke: () => void;
fetch?: typeof fetch;
// How long the room may wait for her worker to join before giving up honestly.
agentWaitMs?: number;
secure?: () => boolean;
}
interface Live { gen: number; abort: AbortController; mic?: MicTrack; room?: VoiceRoom; session?: string; agent: boolean; stated: boolean; hushed: boolean; timer?: ReturnType<typeof setTimeout> }
const AGENT_PHASE: Record<string, VoicePhase> = { initializing: 'joining', listening: 'listening', thinking: 'thinking', speaking: 'speaking' };
export async function voiceStatus(signal: AbortSignal, f: typeof fetch = fetch): Promise<VoiceAvailability> {
let response: Response;
try { response = await f('/api/voice/status', { signal }); } catch (e) { if (signal.aborted) throw e; return { available: false, reason: WORDS.unknown }; }
// A server from before R5 has no such route: that is "not wired", not a failure.
if (response.status === 404 || response.status === 405) return { available: false, reason: WORDS.noRoute };
const data = await response.json().catch(() => null) as { configured?: unknown; tts_ok?: unknown } | null;
if (!response.ok || !data) return { available: false, reason: WORDS.unknown };
if (data.configured !== true) return { available: false, reason: WORDS.notConfigured };
if (data.tts_ok !== true) return { available: false, reason: WORDS.ttsDown };
return { available: true, reason: '' };
}
function micFailure(e: unknown, secure: boolean) {
const name = (e as { name?: string } | null)?.name || '';
if (!secure) return WORDS.insecure;
if (name === 'NotAllowedError' || name === 'SecurityError' || name === 'PermissionDeniedError') return WORDS.denied;
if (name === 'NotFoundError' || name === 'DevicesNotFoundError' || name === 'OverconstrainedError') return WORDS.noMic;
if (name === 'NotReadableError' || name === 'AbortError') return WORDS.micBusy;
return WORDS.denied;
}
const OWED = 'cg.voice.owed';
function owedSessions(): string[] {
try { const v = JSON.parse(localStorage.getItem(OWED) || '[]'); return Array.isArray(v) ? v.filter((s): s is string => typeof s === 'string' && !!s && s.length < 100) : []; } catch { return []; }
}
export class VoiceController {
private gen = 0;
private live: Live | null = null;
private view: VoiceView = OFF;
private f: typeof fetch;
constructor(private deps: VoiceDeps) { this.f = deps.fetch ?? ((...a) => fetch(...a)); }
get phase() { return this.view.phase; }
get active() { return this.live !== null; }
private show(next: Partial<VoiceView>, gen: number) {
if (gen !== this.gen) return;
this.view = { ...this.view, ...next };
this.deps.onView(this.view);
}
async start(): Promise<void> {
// The generation is taken before anything is awaited, so a start that wakes up after a newer one never takes
// the call over; the call it replaces is released first, as end() would.
const prev = this.live;
const gen = ++this.gen, live: Live = { gen, abort: new AbortController(), agent: false, stated: false, hushed: false };
this.live = live;
this.view = { ...OFF, phase: 'opening' }; this.deps.onView(this.view);
const stale = () => gen !== this.gen;
if (prev) { await this.teardown(prev); this.deps.onRecord(); }
if (stale()) return;
void this.settle();
// Everything a failure says is said while this call still owns the page; its cleanup goes on afterwards and,
// however late it answers, only releases this call's own microphone, room and server room.
const fail = (notice: string) => {
if (stale()) return Promise.resolve();
this.live = null; this.gen++;
const released = this.teardown(live);
this.view = { ...OFF, notice }; this.deps.onView(this.view);
return released;
};
const secure = (this.deps.secure ?? (() => window.isSecureContext && !!navigator.mediaDevices?.getUserMedia))();
if (!secure) return fail(WORDS.insecure);
// 1. The honest switch: configured and her voice service reachable (no room, no microphone yet).
let status: VoiceAvailability;
try { status = await voiceStatus(live.abort.signal, this.f); } catch { return; }
if (stale()) return;
if (!status.available) return fail(status.reason);
// 2. The client, then the microphone. A refusal here opens nothing on the server.
let transport: VoiceTransport;
try { transport = await this.deps.transport(); } catch { return fail(WORDS.client); }
if (stale()) return;
let mic: MicTrack;
try { mic = await transport.capture(); } catch (e) { return fail(micFailure(e, true)); }
if (stale()) { mic.stop(); return; }
live.mic = mic;
mic.onEnded(() => { if (!stale()) void fail(WORDS.micGone); });
// 3. A room of her own for this call. This request is not cut short by ending: if it was already under way, its
// answer still owns a room on the server, so it is waited for and given back.
let response: Response, data: (SessionGrant & { detail?: unknown }) | null;
try {
response = await this.f('/api/voice/session', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}', signal: AbortSignal.timeout(15000) });
data = await response.json().catch(() => null);
} catch { return fail(WORDS.unreachable); }
if (response.ok && typeof data?.session_id === 'string') live.session = data.session_id;
if (stale()) { await this.teardown(live); return; }
if (!response.ok || !data?.token || !data.url || !live.session) return fail(typeof data?.detail === 'string' && data.detail ? data.detail : WORDS.unreachable);
const grant = data;
// 4. Join and speak.
this.show({ phase: 'joining' }, gen);
const events: RoomEvents = {
agentJoined: () => { if (stale() || live.agent) return; live.agent = true; clearTimeout(live.timer); if (!live.stated) this.show({ phase: 'listening' }, gen); },
agentState: state => {
if (stale()) return;
const phase = AGENT_PHASE[state]; if (!phase) return;
live.stated = true;
if (phase !== 'joining' && !live.agent) events.agentJoined();
// After text took over, her old voice stays silent until she is back to listening.
if (live.hushed && phase !== 'speaking') { live.hushed = false; live.room?.hush(false); }
this.show({ phase, ...(phase === 'thinking' ? { spoken: '' } : {}) }, gen);
},
subtitle: payload => {
if (stale()) return;
const s = subtitle(payload); if (!s) return;
if (s.kind === 'interim') { this.show({ heard: s.text, spoken: '', ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); if (live.hushed && !live.stated) { live.hushed = false; live.room?.hush(false); } return; }
if (s.kind === 'user_final') { this.show({ heard: s.text, spoken: '', ...(live.stated ? {} : { phase: 'thinking' as const }) }, gen); this.deps.onSpoke(); this.deps.onRecord(); return; }
if (s.kind === 'assistant_final') { this.show({ heard: '', spoken: '', ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); this.deps.onRecord(); return; }
this.show({ heard: '', spoken: s.text, ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); this.deps.onRecord();
},
dropped: () => { if (!stale()) void fail(WORDS.dropped); },
audioBlocked: blocked => this.show({ blocked }, gen),
};
let room: VoiceRoom;
try { room = await transport.join(grant.url, grant.token, events, live.abort.signal); } catch { return fail(WORDS.unreachable); }
if (stale()) { live.room = room; await this.teardown(live); return; }
live.room = room;
try { await room.publish(mic); } catch { return fail(WORDS.unreachable); }
if (stale()) { await this.teardown(live); return; }
if (!live.agent) live.timer = setTimeout(() => { if (!stale() && !live.agent) void fail(WORDS.noAgent); }, this.deps.agentWaitMs ?? 20000);
}
// Stop everything now: microphone first, then the room and its audio, then the server-side room. Safe to call twice.
async end(notice = ''): Promise<void> {
const live = this.live;
this.gen++; this.live = null;
const was = this.view.phase !== 'off' || this.view.notice !== notice;
this.view = { ...OFF, notice };
if (was) this.deps.onView(this.view);
if (live) { await this.teardown(live); this.deps.onRecord(); }
}
// Page is going away: same order, without waiting; the DELETE survives the unload.
unload() {
const live = this.live;
this.gen++; this.live = null; this.view = OFF;
if (live) void this.teardown(live, true);
}
// You sent text while she was answering by voice: the server cancels her voice turn; keep its last audio silent.
hush() {
const live = this.live;
if (!live?.room || (this.view.phase !== 'speaking' && this.view.phase !== 'thinking')) return;
live.hushed = true; live.room.hush(true);
this.show({ spoken: '' }, live.gen);
}
async resumeAudio() { const live = this.live; if (!live?.room) return; try { await live.room.resumeAudio(); this.show({ blocked: false }, live.gen); } catch { /* Still blocked; the tap stays offered. */ } }
private async teardown(live: Live, keepalive = false) {
live.abort.abort(); clearTimeout(live.timer);
try { live.mic?.stop(); } catch { /* Already stopped. */ }
const room = live.room; live.room = undefined;
const session = live.session; live.session = undefined;
const closing = room ? room.close().catch(() => {}) : Promise.resolve();
// The server deletes the room: her worker leaves and its job ends even if the page could not say goodbye itself.
// A DELETE that could not get through (offline) is kept and sent again when the network is back.
const ending = session ? this.release(session, keepalive) : Promise.resolve();
await Promise.all([closing, ending]);
}
// Kept in this browser (not only in memory), so leaving the conversation or reloading does not forget them. Until
// one gets through, the server side still ends on its own: the worker's job ends when the page's participant is
// gone from the room, and the session registry expires (runbook-hel877-voice §3.4/§3.5).
private owe(session: string, on: boolean) {
const list = new Set(owedSessions());
if (on) list.add(session); else list.delete(session);
try { if (list.size) localStorage.setItem(OWED, JSON.stringify([...list].slice(-20))); else localStorage.removeItem(OWED); } catch { /* Memory only for this visit. */ }
if (on) this.owed.add(session); else this.owed.delete(session);
}
private owed = new Set<string>();
private release(session: string, keepalive = false) {
// Noted before sending: if the page is gone before the answer, the next visit sends it again (404 = done).
this.owe(session, true);
return this.f(`/api/voice/session/${encodeURIComponent(session)}`, { method: 'DELETE', keepalive })
.then(r => { this.owe(session, !(r.ok || r.status === 404)); }, () => { this.owe(session, true); });
}
// Back online, arriving, or starting again: give back rooms an earlier end could not reach the server to delete.
async settle() { await Promise.all([...new Set([...this.owed, ...owedSessions()])].map(s => this.release(s))); }
}
// The record's view of voice: rows of your and her voice turns, re-read from the server. Her reach-outs (proactive)
// are left to the inbox reader so they keep their own entrance; text turns already on the page are matched by turn id.
export function mergeTurns(known: Message[], rows: Message[]): Message[] | null {
const byId = new Map(known.map((m, i) => [m.id, i])), turns = new Set(known.flatMap(m => m.turnId ? [`${m.role}:${m.turnId}`] : []));
let next: Message[] | null = null;
for (const m of [...rows].sort((a, b) => serverId(a) - serverId(b))) {
if (m.proactive || !serverId(m)) continue;
const at = byId.get(m.id);
if (at !== undefined) {
const old = (next ?? known)[at];
if (old.status !== m.status || old.text !== m.text) { next ??= [...known]; next[at] = { ...old, status: m.status, text: m.text }; }
continue;
}
if (m.turnId && turns.has(`${m.role}:${m.turnId}`)) continue;
next ??= [...known]; next.push(m); byId.set(m.id, next.length - 1); if (m.turnId) turns.add(`${m.role}:${m.turnId}`);
}
return next;
}
// Newest rows down to `since` (exclusive), at most `pages` pages of 50. Voice turns are few per read; a longer gap is
// left to the next re-read rather than read without bound.
export async function recentRows(since: number, signal: AbortSignal, pages = 4): Promise<Message[]> {
const rows: Message[] = [];
let before: number | null = null;
for (let i = 0; i < pages; i++) {
const page: MessagePage = await api(`messages?limit=50${before ? `&before=${before}` : ''}`, { signal: within(signal, 10000) });
const list = (Array.isArray(page.messages) ? page.messages : []).filter(m => Number.isSafeInteger(m?.id) && m.id > 0 && typeof m.text === 'string');
rows.push(...list.filter(m => m.id > since).map(toMessage));
if (list.some(m => m.id <= since) || !page.next_before || !Number.isSafeInteger(page.next_before)) break;
before = page.next_before;
}
return rows;
}
+38
View File
@@ -0,0 +1,38 @@
// HEL-878 browser check only: a scripted stand-in for the livekit-client UMD global, served by tests/hel878_mock.mjs in
// place of the vendored build. Same names the page uses (Room, RoomEvent, Track.Source, ParticipantKind,
// createLocalAudioTrack); no WebRTC, no signalling. The microphone is a real MediaStreamTrack from the browser's fake
// device (--use-fake-device-for-media-stream), so stopping it can be observed. window.__lk drives her side.
(() => {
const RoomEvent = { TrackSubscribed: 'trackSubscribed', TrackUnsubscribed: 'trackUnsubscribed', ParticipantConnected: 'participantConnected', ParticipantAttributesChanged: 'participantAttributesChanged', DataReceived: 'dataReceived', Disconnected: 'disconnected', AudioPlaybackStatusChanged: 'audioPlaybackChanged' };
const lk = window.__lk = { rooms: [], connects: [], published: [], mics: [], connectDelay: 200 };
class Room {
constructor() { this.handlers = {}; this.remoteParticipants = new Map(); this.canPlaybackAudio = true; this.state = 'new'; const self = this; this.localParticipant = { tracks: [], async publishTrack(t) { if (self.state !== 'connected') throw new Error('not connected'); self.localParticipant.tracks.push(t); lk.published.push(t); } }; lk.rooms.push(this); }
on(e, cb) { (this.handlers[e] ||= []).push(cb); return this; }
emit(e, ...a) { (this.handlers[e] || []).forEach(cb => cb(...a)); }
async connect(url, token) { lk.connects.push({ url, token }); this.state = 'connecting'; await new Promise(r => setTimeout(r, lk.connectDelay)); if (this.state !== 'connecting') throw new Error('aborted'); this.state = 'connected'; }
async disconnect(stop = true) { if (this.state === 'gone') return; this.state = 'gone'; if (stop) this.localParticipant.tracks.forEach(t => t.stop()); this.emit(RoomEvent.Disconnected); }
async startAudio() { this.canPlaybackAudio = true; }
}
const live = () => lk.rooms.filter(r => r.state === 'connected').at(-1);
// Her worker joins: an agent participant with a looping tone as her voice track.
lk.join = () => {
const room = live(); if (!room) return false;
const p = { identity: 'agent-AJ_fake', kind: 4, attributes: { 'lk.agent.state': 'listening' } };
room.remoteParticipants.set(p.identity, p); room.emit(RoomEvent.ParticipantConnected, p);
const ctx = new AudioContext(), osc = ctx.createOscillator(), dest = ctx.createMediaStreamDestination(); osc.connect(dest); osc.start();
const mst = dest.stream.getAudioTracks()[0];
const track = { kind: 'audio', mediaStreamTrack: mst, els: [], attach() { const el = document.createElement('audio'); el.srcObject = new MediaStream([mst]); el.autoplay = true; this.els.push(el); return el; }, detach() { const e = this.els; this.els = []; return e; }, stop() { mst.stop(); } };
room.emit(RoomEvent.TrackSubscribed, track, {}, p);
return true;
};
lk.state = s => { const room = lk.rooms.at(-1), p = [...room.remoteParticipants.values()][0]; if (!p) return false; p.attributes['lk.agent.state'] = s; room.emit(RoomEvent.ParticipantAttributesChanged, { 'lk.agent.state': s }, p); return true; };
lk.say = obj => { const room = lk.rooms.at(-1); room.emit(RoomEvent.DataReceived, new TextEncoder().encode(JSON.stringify(obj)), undefined, 0, 'cg.subtitle'); return true; };
lk.drop = () => { const room = live(); room.state = 'gone'; room.emit(RoomEvent.Disconnected); return true; };
lk.block = () => { const room = live(); room.canPlaybackAudio = false; room.emit(RoomEvent.AudioPlaybackStatusChanged); return true; };
async function createLocalAudioTrack(constraints) {
const stream = await navigator.mediaDevices.getUserMedia({ audio: constraints || true }), mst = stream.getAudioTracks()[0];
lk.mics.push(mst);
return { kind: 'audio', mediaStreamTrack: mst, attach() { return document.createElement('audio'); }, detach() { return []; }, stop() { mst.stop(); } };
}
window.LivekitClient = { Room, RoomEvent, Track: { Source: { Microphone: 'microphone' } }, ParticipantKind: { AGENT: 4 }, createLocalAudioTrack };
})();
+86
View File
@@ -0,0 +1,86 @@
// HEL-878 isolated contract mock for real voice on the approved conversation. Serves web/dist, the persisted-message
// contract (as tests/hel875_mock.mjs) and the HEL-877 voice router (src/cybergril/voice/router.py): GET
// /api/voice/status, POST /api/voice/session -> 201 grant, DELETE /api/voice/session/{id}. The LiveKit client path is
// answered with tests/fake-livekit.js (a scripted stand-in, no WebRTC); the real vendored build stays reachable at
// /__real-sdk.js for the API-surface check only. Synthetic data only: no model, no LiveKit, no TTS, no network.
// Usage: node tests/hel878_mock.mjs <port>. Control GETs: /__voice?configured=&tts=, /__refuse/<code|0>,
// /__voice-turn?stage=user|reply|cancel&text=, /__cut, /__sessions, /__log, /__rows.
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { extname, join, normalize } from 'node:path';
import { fileURLToPath } from 'node:url';
const dist = fileURLToPath(new URL('../dist/', import.meta.url)), fake = fileURLToPath(new URL('./fake-livekit.js', import.meta.url));
const port = Number(process.argv[2] || 8878);
const T0 = Date.UTC(2026, 9, 26, 12, 0);
const identity = { id: '1', name: '栖', birth: '2000-03-14', hour: '子', relation: '朋友', nickname: '', boundary: '', color: { pillars: { year: '庚辰', month: '己卯', day: '丙午', hour: '戊子' }, traits: '合成测试档案' } };
// /__hold-delete?id=<session> keeps that DELETE unanswered (a slow server); /__hold-delete with no id answers them all.
const epoch = 3, log = [], rows = [], sessions = new Map(), held = new Map();
let voice = { configured: true, tts: true }, refuse = 0, made = 0, voiceTurn = 0, textStream = null;
const add = row => { const id = rows.length + 1; rows.push({ id, ts: T0 + id * 60e3, status: 'sent', lifeIds: [], ...row }); return id; };
add({ role: 'user', text: '下雨天的时候,你会想听什么?', turn_id: '00000000-0000-4000-8000-000000000001' });
add({ role: 'companion', text: '会想把窗开一条缝,听雨落在铁皮檐上。你呢,今天累不累?', turn_id: '00000000-0000-4000-8000-000000000001' });
const dto = r => ({ id: r.id, ts: r.ts, role: r.role, text: r.text, turn_id: r.turn_id, status: r.status, lifeIds: r.lifeIds, ...(r.proactive ? { proactive: true } : {}) });
const send = (res, status, body, type = 'application/json; charset=utf-8') => { res.writeHead(status, { 'Content-Type': type, 'Cache-Control': 'no-store' }); res.end(typeof body === 'string' || Buffer.isBuffer(body) ? body : JSON.stringify(body)); };
const sleep = ms => new Promise(r => setTimeout(r, ms));
const REFUSALS = { 503: '她的声音服务这会儿不在,稍后再试试。', 409: '还没有创建真实伴侣,语音也不会开始。' };
createServer(async (req, res) => {
const url = new URL(req.url, 'http://x'), p = url.pathname;
if (!p.startsWith('/__') && !p.startsWith('/assets/') && !p.startsWith('/vendor/')) log.push(`${req.method} ${p}${url.search}`);
if (p === '/__log') return send(res, 200, log.splice(0));
if (p === '/__rows') return send(res, 200, rows.map(dto));
if (p === '/__sessions') return send(res, 200, { open: [...sessions.keys()], made });
if (p === '/__voice') { voice = { configured: url.searchParams.get('configured') !== '0', tts: url.searchParams.get('tts') !== '0' }; return send(res, 200, voice); }
if (p.startsWith('/__refuse/')) { refuse = Number(p.slice(10)); return send(res, 200, { refuse }); }
// What the worker's /api/chat call leaves in the record, one stage at a time (the browser never posts it).
if (p === '/__voice-turn') {
const stage = url.searchParams.get('stage'), text = url.searchParams.get('text') || '';
if (stage === 'user') { voiceTurn++; add({ role: 'user', text, turn_id: `voice-${voiceTurn}`, status: 'pending' }); }
if (stage === 'reply') { add({ role: 'companion', text, turn_id: `voice-${voiceTurn}` }); rows.forEach(r => { if (r.turn_id === `voice-${voiceTurn}`) r.status = 'sent'; }); }
if (stage === 'cancel') rows.forEach(r => { if (r.turn_id === `voice-${voiceTurn}`) r.status = 'cancelled'; });
return send(res, 200, { turn: voiceTurn });
}
// A voice turn took the server's turn gate: the text stream in flight ends without a final; its row is cancelled.
if (p === '/__cut') { const s = textStream; textStream = null; if (s) { rows.forEach(r => { if (r.turn_id === s.turn_id && r.role === 'user') r.status = 'cancelled'; }); s.cut = true; s.res.end(); } return send(res, 200, { cut: !!s }); }
if (p === '/__real-sdk.js') return send(res, 200, await readFile(join(dist, 'vendor/livekit-client-2.22.4.umd.min.js')), 'text/javascript');
if (p.startsWith('/vendor/livekit-client')) return send(res, 200, await readFile(fake), 'text/javascript');
if (p === '/api/health') return send(res, 200, { ok: true, version: 'R5-candidate', epoch, llm: { ok: true, model: 'mock' }, backend: { life: true, persona: true, receipt: true, web_connected: true }, life_runtime: { state: 'running', timezone: 'Asia/Shanghai', budget_warning: false, budget_stopped: false, last: null }, capabilities: { text: true, proactive: true, voice: false, memory: true } });
if (p === '/api/companion') return send(res, 200, identity);
if (p === '/api/messages') {
const limit = Math.min(200, Number(url.searchParams.get('limit') || 200)), before = Number(url.searchParams.get('before') || Number.MAX_SAFE_INTEGER);
const desc = rows.filter(r => r.id < before).sort((a, b) => b.id - a.id).slice(0, limit + 1), more = desc.length > limit, page = desc.slice(0, limit);
return send(res, 200, { messages: [...page].reverse().map(dto), next_before: more ? page[page.length - 1].id : null, epoch });
}
if (p === '/api/persona') return send(res, 200, { state: 'ready', interpretation: null, seeds: [], growth: [] });
if (p === '/api/life') return send(res, 200, { events: [], next_before: null, epoch });
if (p === '/api/memories') return send(res, 200, { memories: [], next_before: null, epoch });
if (p === '/api/voice/status') return send(res, 200, voice.configured ? { configured: true, tts_ok: voice.tts, agent_name: 'cybergril-voice-agent', voice: false, livekit_url: 'wss://192.168.200.12:7880' } : { configured: false, tts_ok: false, agent_name: null, voice: false });
if (p === '/api/voice/session' && req.method === 'POST') {
if (!voice.configured) return send(res, 503, { detail: '语音还没有在这台服务上配置,不能假装能通话。' });
if (refuse) return send(res, refuse, { detail: REFUSALS[refuse] || '通话服务暂时连不上,稍后再试试。' });
await sleep(120);
const id = `sess-${++made}`; sessions.set(id, { room: `cg-voice-${made}` });
return send(res, 201, { url: 'wss://lk.invalid', token: `token-${id}`, room: `cg-voice-${made}`, session_id: id, agent_name: 'cybergril-voice-agent', epoch, expires_at: Date.now() + 2 * 3600e3 });
}
if (p === '/__hold-delete') { const id = url.searchParams.get('id'); if (id) { let open; held.set(id, { promise: new Promise(r => { open = r; }), open }); } else { held.forEach(h => h.open()); held.clear(); } return send(res, 200, { held: [...held.keys()] }); }
if (p.startsWith('/api/voice/session/') && req.method === 'DELETE') { const id = decodeURIComponent(p.slice(19)); if (held.has(id)) await held.get(id).promise; return sessions.delete(id) ? send(res, 200, { ended: true }) : send(res, 404, { detail: '这次通话已经不在了。' }); }
if (p === '/api/chat' && req.method === 'POST') {
let body = ''; for await (const chunk of req) body += chunk;
const { text: said, turn_id } = JSON.parse(body), reply = '我在。今天的雨停了没有?要是还在下,就慢点回家。';
add({ role: 'user', text: said, turn_id, status: 'pending' });
res.writeHead(200, { 'Content-Type': 'text/event-stream' });
const stream = { res, turn_id, cut: false }; textStream = stream;
for (const part of reply.match(/.{1,4}/gu)) { if (stream.cut) return; res.write(`data: ${JSON.stringify({ t: 'chat.delta', epoch, turn_id, delta: part })}\n\n`); await sleep(400); }
if (stream.cut) return;
textStream = null;
add({ role: 'companion', text: reply, turn_id }); rows.forEach(r => { if (r.turn_id === turn_id) r.status = 'sent'; });
res.end(`data: ${JSON.stringify({ t: 'chat.final', epoch, turn_id, utterance: { text: reply, speech_tags: [] }, receipt: null })}\n\n`);
return;
}
if (p.startsWith('/api/')) return send(res, 404, { detail: 'Not Found' });
const file = normalize(join(dist, p === '/' ? 'index.html' : p));
if (!file.startsWith(normalize(dist))) return send(res, 403, 'no', 'text/plain');
try { send(res, 200, await readFile(file), { '.js': 'text/javascript', '.css': 'text/css', '.html': 'text/html; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.webp': 'image/webp' }[extname(file)] || 'application/octet-stream'); }
catch { send(res, 200, await readFile(join(dist, 'index.html')), 'text/html; charset=utf-8'); }
}).listen(port, '127.0.0.1', () => console.log(`hel878 mock on ${port}`));
+258
View File
@@ -0,0 +1,258 @@
// HEL-878 browser check: drives headless Edge over CDP against tests/hel878_mock.mjs (real voice on the approved
// conversation, with tests/fake-livekit.js in place of the LiveKit client and Chromium's fake audio device in place of
// a microphone). Usage: node tests/hel878_shots.mjs <edge.exe> <outDir> [port]. One PASS/FAIL line per check.
import { spawn } from 'node:child_process';
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
const [edge, out, portArg] = process.argv.slice(2);
const port = Number(portArg || 8878), base = `http://127.0.0.1:${port}`, cdpPort = 9478;
mkdirSync(out, { recursive: true });
const profile = join(tmpdir(), `hel878-edge-${Date.now()}`);
const browser = spawn(edge, ['--headless=new', `--remote-debugging-port=${cdpPort}`, `--user-data-dir=${profile}`, '--no-first-run', '--hide-scrollbars', '--force-color-profile=srgb', '--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream', '--autoplay-policy=no-user-gesture-required', 'about:blank'], { stdio: 'ignore' });
const sleep = ms => new Promise(r => setTimeout(r, ms));
let targets;
for (let i = 0; i < 60 && !targets; i++) { try { targets = await (await fetch(`http://127.0.0.1:${cdpPort}/json`)).json(); } catch { await sleep(250); } }
const ws = new WebSocket(targets.find(t => t.type === 'page').webSocketDebuggerUrl);
await new Promise(r => ws.addEventListener('open', r, { once: true }));
let seq = 0; const waiting = new Map();
ws.addEventListener('message', ({ data }) => { const m = JSON.parse(data); if (m.id && waiting.has(m.id)) { const { ok, ko } = waiting.get(m.id); waiting.delete(m.id); m.error ? ko(new Error(m.error.message)) : ok(m.result); } });
const cdp = (method, params = {}) => new Promise((ok, ko) => { const id = ++seq; waiting.set(id, { ok, ko }); ws.send(JSON.stringify({ id, method, params })); });
const js = async expr => { const r = await cdp('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: true }); if (r.exceptionDetails) throw new Error(r.exceptionDetails.exception?.description || 'js error'); return r.result.value; };
const results = [];
const check = (name, ok, detail = '') => { results.push(ok); console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail ? ` — ${detail}` : ''}`); };
const ctl = async path => (await fetch(`${base}${path}`)).json();
const shot = async name => { const { data } = await cdp('Page.captureScreenshot', { format: 'png' }); writeFileSync(join(out, `${name}.png`), Buffer.from(data, 'base64')); };
const click = async (selector, text = '') => js(`(() => { const el = [...document.querySelectorAll(${JSON.stringify(selector)})].find(e => e.offsetParent !== null && e.textContent.includes(${JSON.stringify(text)})); if (!el) return false; el.click(); return true; })()`);
const text = sel => js(`[...document.querySelectorAll(${JSON.stringify(sel)})].map(e => e.innerText).join('\\n')`);
const exists = sel => js(`!!document.querySelector(${JSON.stringify(sel)})`);
const said = () => js(`document.querySelector('.conversation .sr')?.textContent || ''`);
const status = () => js(`document.querySelector('.talk-status')?.innerText || ''`);
const row = () => js(`document.querySelector('.voice-row.real')?.innerText || ''`);
const phase = () => js(`document.querySelector('.voice-row.real')?.dataset.phase || 'off'`);
const lk = expr => js(`window.__lk ? (${expr}) : null`);
const type = t => js(`(() => { const el = document.querySelector('textarea'); const set = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set; set.call(el, ${JSON.stringify(t)}); el.dispatchEvent(new Event('input', { bubbles: true })); return true; })()`);
const until = async (fn, ms = 4000) => { const end = Date.now() + ms; while (Date.now() < end) { if (await fn()) return true; await sleep(100); } return false; };
const views = { desktop: { width: 1440, height: 900, deviceScaleFactor: 1, mobile: false }, phone: { width: 390, height: 844, deviceScaleFactor: 2, mobile: true } };
async function view(v) { await cdp('Emulation.setDeviceMetricsOverride', views[v]); await cdp('Emulation.setTouchEmulationEnabled', { enabled: views[v].mobile, maxTouchPoints: 5 }); }
async function boot(theme) {
await cdp('Page.navigate', { url: `${base}/` }); await sleep(500);
await js(`localStorage.setItem('cg.theme', ${JSON.stringify(theme)}); sessionStorage.clear(); true`);
await cdp('Page.navigate', { url: `${base}/` }); await sleep(1600);
}
async function frames(name, ms) { const list = []; const end = Date.now() + ms; while (Date.now() < end) { const { data } = await cdp('Page.captureScreenshot', { format: 'jpeg', quality: 70 }); list.push(data); } list.forEach((d, i) => writeFileSync(join(out, `${name}-${String(i).padStart(2, '0')}.jpg`), Buffer.from(d, 'base64'))); return list.length; }
// Every microphone request is counted (the fake device still answers it); a test can make the next one refuse.
await cdp('Page.enable'); await cdp('Runtime.enable'); await cdp('Network.enable');
await cdp('Page.addScriptToEvaluateOnNewDocument', { source: `window.__asked = 0; window.__refuseMic = ''; try { const md = navigator.mediaDevices, real = md.getUserMedia.bind(md); md.getUserMedia = c => { window.__asked++; if (window.__refuseMic) return Promise.reject(new DOMException('refused', window.__refuseMic)); return real(c); }; } catch {}` });
const micsLive = () => lk(`__lk.mics.filter(t => t.readyState === 'live').length`);
const herAudio = () => js(`document.querySelectorAll('[data-voice="her"] audio').length`);
const calls = async () => (await ctl('/__log'));
async function startVoice() {
await click('.voice-button'); await until(() => exists('.modal.open'));
await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await phase()) === 'joining');
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected');
await lk('__lk.join()'); await sleep(200);
}
const cleanEnd = async label => {
await sleep(400);
const s = await ctl('/__sessions'), mics = await micsLive(), audio = await herAudio(), holder = await exists('[data-voice="her"]');
check(`${label}: microphone stopped, her audio and room gone, server room deleted`, mics === 0 && audio === 0 && !holder && s.open.length === 0 && (await phase()) === 'off', `mics=${mics} audio=${audio} holder=${holder} open=${s.open.length}`);
};
// ── 1. Arriving at the conversation: one honest status read; no room, no microphone, no client download. ──
await view('desktop'); await boot('moon'); await calls();
await click('.nav-item', '交谈'); await sleep(1400);
let log = await calls();
check('status read on arrival only: no session, no microphone, no LiveKit client', log.filter(l => l.includes('/api/voice')).join() === 'GET /api/voice/status' && await js('window.__asked') === 0 && await js('!window.LivekitClient'), log.filter(l => l.includes('voice')).join(' | '));
check('voice button offers to start, honestly labelled', await js(`document.querySelector('.voice-button').getAttribute('aria-label')`) === '开始语音');
await shot('01-moon-desktop-talk-voice-off');
// ── 2. Not configured / TTS down: the button says so and nothing starts. ──
await ctl('/__voice?configured=0'); await boot('moon'); await click('.nav-item', '交谈'); await sleep(1400); await calls();
await click('.voice-button'); await sleep(500);
check('not configured: refusal in her own words, no dialog, no session, no microphone', (await status()).includes('语音还没有在这台服务上配置') && !(await exists('.modal.open')) && !(await calls()).some(l => l.startsWith('POST')) && await js('window.__asked') === 0 && await exists('.voice-button.denied'), await status());
await shot('02-moon-desktop-voice-not-configured');
await ctl('/__voice?configured=1&tts=0'); await click('.voice-button'); await sleep(500);
check('TTS down: says her voice service is away', (await status()).includes('她的声音服务这会儿不在'), await status());
await ctl('/__voice?configured=1&tts=1');
// ── 3. The ask: what happens, where the sound goes, that it is not yet real-device accepted. Cancel asks nothing. ──
await click('.voice-button'); await until(() => exists('.modal.open'));
const ask = await text('.modal.open');
check('the dialog explains microphone, destination, same memory, how to stop, and the unverified path', ask.includes('麦克风') && ask.includes('这台服务器') && ask.includes('同一份记忆') && ask.includes('立即关闭') && ask.includes('还没有经过真机验收'), ask.replace(/\n/g, ' '));
await shot('03-moon-desktop-voice-ask');
await click('.modal.open .btn-ghost', '取消'); await sleep(300);
check('cancel: no microphone request, no session', await js('window.__asked') === 0 && (await ctl('/__sessions')).made === 0);
// ── 4. Start: opening → waiting for her → listening. ──
await click('.voice-button'); await until(() => exists('.modal.open')); await calls();
await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await phase()) === 'joining');
await shot('04-moon-desktop-voice-joining');
check('waiting for her is shown as waiting, not as connected', (await row()).includes('在等栖接起') && (await text('.topbar .state')).includes('未经真机验收'), await row());
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected');
log = await calls();
check('one session opened with a server token; one microphone; mic published to that room', log.filter(l => l === 'POST /api/voice/session').length === 1 && (await lk('__lk.connects.at(-1).token')).startsWith('token-sess-') && await js('window.__asked') === 1 && await lk('__lk.published.length') === 1);
await lk('__lk.join()'); await sleep(300);
check('her worker joined: listening, wave in your colour, her audio attached', (await phase()) === 'listening' && (await row()).includes('在听你说') && await herAudio() === 1, await row());
await frames('m1-voice-in', 700);
await shot('05-moon-desktop-voice-listening');
// ── 5. A voice turn: your draft, the record row, her thinking and speaking, then her words on the stage. ──
await lk(`__lk.say({ kind: 'interim', role: 'user', text: '今天下班的路上' })`); await sleep(200);
check('interim words show as a draft line beside the state', (await row()).includes('今天下班的路上'));
await shot('06-moon-desktop-voice-heard');
await ctl('/__voice-turn?stage=user&text=' + encodeURIComponent('今天下班的路上看到一只猫'));
await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '今天下班的路上看到一只猫' })`); await lk(`__lk.state('thinking')`);
await until(async () => (await said()).includes('看到一只猫'));
check('your spoken words enter the stage from the server record (not from the subtitle)', (await said()).includes('看到一只猫') && (await phase()) === 'thinking', await said());
await shot('07-moon-desktop-voice-thinking');
await lk(`__lk.state('speaking')`); await sleep(250);
check('speaking: her name, and that you can cut in by speaking', (await row()).includes('栖在说') && (await row()).includes('开口就能打断'));
await frames('m2-voice-speaking', 900);
await shot('08-moon-desktop-voice-speaking');
await ctl('/__voice-turn?stage=reply&text=' + encodeURIComponent('是橘色的那只吗?它最近总在桥下晒太阳。'));
await lk(`__lk.say({ kind: 'assistant_final', role: 'assistant', text: '是橘色的那只吗?它最近总在桥下晒太阳。' })`); await lk(`__lk.state('listening')`);
await until(async () => (await said()).includes('晒太阳'), 6000);
check('her spoken answer reaches the stage once it is in the record', (await said()).includes('晒太阳'), await said());
await shot('09-moon-desktop-voice-reply-on-stage');
await click('.log-btn'); await sleep(700);
const rec = await text('.log-panel .scroll');
check('the record holds the voice turn once, same conversation as text', rec.split('看到一只猫').length === 2 && rec.split('晒太阳').length === 2 && rec.includes('下雨天'));
await shot('10-moon-desktop-voice-record');
await click('.log-panel .btn-round'); await sleep(400);
// ── 6. φ: her failure line is heard and shown, never recorded. ──
await lk(`__lk.say({ kind: 'error', role: 'assistant', text: '我刚刚没接上话,你再说一遍好吗?' })`); await sleep(400);
check('φ line shown as heard, marked not recorded; not in the record', (await row()).includes('没接上话') && (await row()).includes('未记入聊天') && !(await ctl('/__rows')).some(r => r.text.includes('没接上话')));
await shot('11-moon-desktop-voice-phi');
// ── 7. Typing over her spoken answer: what is left of it goes silent until she listens again. ──
await lk(`__lk.state('speaking')`); await sleep(150);
await type('等等,我先打字说'); await js(`document.querySelector('.send').click(), true`); await sleep(300);
check('text over voice: her remaining audio is muted', await js(`[...document.querySelectorAll('[data-voice="her"] audio')].every(a => a.muted)`));
await lk(`__lk.state('listening')`); await sleep(150);
check('…and unmuted once she is listening again', await js(`[...document.querySelectorAll('[data-voice="her"] audio')].every(a => !a.muted)`));
await until(async () => !(await exists('.reader-bar .stop')), 8000);
// ── 8. Speaking over a text reply: the text turn is cut (not "failed"), your words stay as said. ──
await type('再给我讲讲那只猫'); await js(`document.querySelector('.send').click(), true`); await sleep(1300);
check('text reply streaming', await exists('.reader-bar .stop'));
await ctl('/__cut'); await ctl('/__voice-turn?stage=user&text=' + encodeURIComponent('算了先不说了'));
await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '算了先不说了' })`); await sleep(1500);
const cutStatus = await status();
check('voice over text: no failure notice, no retry, draft not refilled', !cutStatus.includes('连接中断') && !(await exists('.talk-status .retry')) && await js(`document.querySelector('textarea').value`) === '', cutStatus);
await click('.log-btn'); await sleep(700);
const cutRec = await text('.log-panel .scroll');
check('…the cut text turn is marked cancelled in the record; the voice words are there once', cutRec.includes('已取消') && cutRec.split('算了先不说了').length === 2, cutRec.slice(-200).replace(/\n/g, ' '));
await click('.log-panel .btn-round'); await sleep(300);
// ── 9. Autoplay held back: a tap lets her voice out. ──
await lk('__lk.block()'); await sleep(200);
check('blocked audio offers a tap', (await row()).includes('点一下听栖的声音'));
await shot('12-moon-desktop-voice-audio-blocked');
await click('.voice-row.real button', '点一下'); await sleep(200);
check('…and the offer goes away after the tap', !(await row()).includes('点一下'));
// ── 10. 结束语音: everything stops at once; late events from the old room change nothing. ──
await calls();
await click('.voice-row.real .voice-end');
await cleanEnd('结束语音');
check('ended: says the microphone is closed; DELETE sent', (await status()).includes('麦克风已关闭') && (await calls()).some(l => l.startsWith('DELETE /api/voice/session/')), await status());
await shot('13-moon-desktop-voice-ended');
await lk(`__lk.state('speaking')`); await lk(`__lk.say({ kind: 'user_final', role: 'user', text: '幽灵' })`); await sleep(500);
check('late events after the end do not revive anything', (await phase()) === 'off' && !(await exists('.voice-row.real')) && !(await calls()).some(l => l.startsWith('POST')));
// ── 11. Microphone permission refused: nothing opened. ──
await js(`window.__refuseMic = 'NotAllowedError', true`); await calls();
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(800);
check('permission refused: told plainly, no session opened, text still there', (await status()).includes('没有拿到麦克风权限') && !(await calls()).some(l => l.startsWith('POST')) && await exists('textarea'), await status());
await shot('14-moon-desktop-voice-denied');
await js(`window.__refuseMic = '', true`);
// ── 12. Permission revoked mid-call / room dropped / server refusal. ──
await startVoice();
await lk(`(__lk.mics.at(-1).dispatchEvent(new Event('ended')), true)`);
await sleep(300);
check('microphone revoked mid-call: call ends, said so', (await status()).includes('麦克风被关掉了'), await status());
await cleanEnd('revoked');
await startVoice(); await lk('__lk.drop()');
await sleep(300);
check('room dropped: call ends, said so', (await status()).includes('通话断开了'), await status());
await cleanEnd('dropped');
// HEL-897: the dropped call's DELETE answers only after a new call is up; the late answer may not touch the new call.
await startVoice();
const firstSession = (await ctl('/__sessions')).open.at(-1);
await ctl(`/__hold-delete?id=${firstSession}`); await lk('__lk.drop()'); await sleep(300);
check('slow DELETE: dropped call says so and its microphone stops before the server answers', (await status()).includes('通话断开了') && await micsLive() === 0 && (await ctl('/__sessions')).open.includes(firstSession), await status());
await startVoice();
const secondSession = (await ctl('/__sessions')).open.find(s => s !== firstSession);
check('…a new call is up meanwhile: listening', (await phase()) === 'listening' && await micsLive() === 1);
await ctl('/__hold-delete'); await sleep(800);
const lateOpen = (await ctl('/__sessions')).open;
check('…the old DELETE answers late: the new call stays listening, no old notice, one live microphone, her audio, only the new room open', (await phase()) === 'listening' && !(await status()).includes('通话断开了') && await micsLive() === 1 && await herAudio() === 1 && lateOpen.length === 1 && lateOpen[0] === secondSession, `phase=${await phase()} status=${await status()} open=${lateOpen.join()}`);
await shot('12b-moon-desktop-voice-new-call-after-late-delete');
await click('.voice-row.real .voice-end'); await cleanEnd('new call after the late old DELETE');
await ctl('/__refuse/503'); await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(900);
check('server refusal shows its detail; microphone released', (await status()).includes('她的声音服务这会儿不在') && await micsLive() === 0, await status());
await ctl('/__refuse/0');
// ── 13. Repeated on/off: one call at most, nothing left behind. ──
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音'); await sleep(60);
await click('.voice-row.real .voice-end'); await sleep(40);
await click('.voice-button'); await until(() => exists('.modal.open')); await click('.modal.open .btn-primary', '开始语音');
await until(async () => (await lk('__lk.rooms.at(-1)?.state')) === 'connected'); await sleep(600);
const sess = await ctl('/__sessions');
check('rapid off/on: exactly one open session and one live microphone', sess.open.length === 1 && await micsLive() === 1, JSON.stringify(sess));
await click('.voice-row.real .voice-end'); await cleanEnd('rapid toggle');
// ── 14. Leaving: nav, Esc, 结束交谈, page unload, network loss — each ends the call. ──
await startVoice(); await click('.nav-item', '此刻'); await cleanEnd('nav away');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await js(`document.body.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })), true`); await cleanEnd('Esc');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await click('.end-talk'); await cleanEnd('结束交谈');
await click('.nav-item', '交谈'); await sleep(1300); await startVoice();
await cdp('Network.emulateNetworkConditions', { offline: true, latency: 0, downloadThroughput: -1, uploadThroughput: -1 }); await sleep(500);
check('network lost: call ends with its own words', (await status()).includes('网络断开了'), await status());
const offMics = await micsLive(), offAudio = await herAudio(), offOpen = (await ctl('/__sessions')).open.length;
check('network lost: microphone and her audio stop at once; the server room is owed, not pretended deleted', offMics === 0 && offAudio === 0 && offOpen === 1 && (await js(`localStorage.getItem('cg.voice.owed')`) || '').includes('sess-'), `mics=${offMics} audio=${offAudio} open=${offOpen}`);
await cdp('Network.emulateNetworkConditions', { offline: false, latency: 0, downloadThroughput: -1, uploadThroughput: -1 });
await cleanEnd('offline, after the network is back');
check('…the owed DELETE is sent once back online and forgotten', !(await js(`localStorage.getItem('cg.voice.owed')`)));
await startVoice(); await calls();
await cdp('Page.navigate', { url: 'about:blank' }); await sleep(800);
log = await calls();
check('page unload: the server room is deleted (keepalive)', log.some(l => l.startsWith('DELETE /api/voice/session/')) && (await ctl('/__sessions')).open.length === 0, log.join(' | '));
// ── 15. Phone and the fog theme: the same line, the same states. ──
for (const [theme, v] of [['fog', 'desktop'], ['moon', 'phone'], ['fog', 'phone']]) {
await view(v); await boot(theme); await click('.nav-item', '交谈'); await sleep(1400);
await startVoice();
await lk(`__lk.say({ kind: 'interim', role: 'user', text: '今天下班的路上看到一只很胖的橘猫' })`); await sleep(250);
await shot(`15-${theme}-${v}-voice-listening`);
await lk(`__lk.state('speaking')`); await sleep(300);
await shot(`16-${theme}-${v}-voice-speaking`);
if (v === 'phone') check(`${theme}/${v}: voice line fits the screen`, await js(`(() => { const r = document.querySelector('.voice-row.real').getBoundingClientRect(); return r.left >= 0 && r.right <= innerWidth; })()`));
await click('.voice-row.real .voice-end'); await sleep(400);
}
await shot('17-fog-phone-voice-ended');
// ── 16. The real vendored client: same API names the page uses (no connection is made). ──
await view('desktop');
await cdp('Page.navigate', { url: `${base}/__nothing` }); await sleep(800);
const sdk = await js(`new Promise(ok => { delete window.LivekitClient; const s = document.createElement('script'); s.src = '/__real-sdk.js'; s.onload = () => { const L = window.LivekitClient, E = L.RoomEvent; const room = new L.Room({ adaptiveStream: true, dynacast: true }); ok({ events: ['TrackSubscribed','TrackUnsubscribed','ParticipantConnected','ParticipantAttributesChanged','DataReceived','Disconnected','AudioPlaybackStatusChanged'].every(k => typeof E[k] === 'string'), values: [E.ParticipantAttributesChanged, E.DataReceived, E.AudioPlaybackStatusChanged], mic: L.Track.Source.Microphone, agent: L.ParticipantKind && L.ParticipantKind.AGENT, create: typeof L.createLocalAudioTrack, methods: ['connect','disconnect','startAudio','on'].every(m => typeof room[m] === 'function'), playback: typeof room.canPlaybackAudio, remote: room.remoteParticipants instanceof Map, publish: typeof room.localParticipant.publishTrack, version: L.version }); }; s.onerror = () => ok(null); document.head.append(s); })`);
check('real livekit-client 2.22.4: every name the adapter uses exists', !!sdk && sdk.events && sdk.mic === 'microphone' && typeof sdk.agent === 'number' && sdk.create === 'function' && sdk.methods && sdk.playback === 'boolean' && sdk.remote && sdk.publish === 'function', JSON.stringify(sdk));
// ── 17. The demo entry stays separate: no voice route, no client. ──
await calls();
await cdp('Page.navigate', { url: `${base}/?demo=1` }); await sleep(2500);
check('demo entry makes no voice requests', !(await calls()).some(l => l.includes('/api/voice')));
ws.close(); browser.kill();
try { rmSync(profile, { recursive: true, force: true }); } catch { /* Edge may still hold the profile. */ }
const failed = results.filter(r => !r).length;
console.log(`${results.length - failed}/${results.length} passed`);
process.exit(failed ? 1 : 0);
+44
View File
@@ -0,0 +1,44 @@
import { expect, test } from 'vitest';
import { VoiceController, type RoomEvents, type VoiceView } from '../src/voice/session';
test('old room disconnect cleanup cannot overwrite a newly opened call', async () => {
const pendingDeletes: (() => void)[] = [];
const releaseOld = () => pendingDeletes.splice(0).forEach(resolve => resolve());
let grant = 0;
const events: RoomEvents[] = [];
let view: VoiceView | undefined;
const controller = new VoiceController({
secure: () => true,
onView: v => { view = v; },
onRecord: () => {}, onSpoke: () => {},
agentWaitMs: 60000,
fetch: (async (url, init) => {
if (String(url).endsWith('/status')) return Response.json({ configured: true, tts_ok: true });
if (init?.method === 'POST') return Response.json({ session_id: `s${++grant}`, token: 'token', url: 'wss://fake.invalid' });
if (String(url).endsWith('/s1')) await new Promise<void>(resolve => { pendingDeletes.push(resolve); });
return Response.json({ ended: true });
}) as typeof fetch,
transport: async () => ({
capture: async () => ({ stop() {}, onEnded() {} }),
join: async (_url, _token, callbacks) => {
events.push(callbacks);
return { publish: async () => {}, close: async () => {}, hush() {}, resumeAudio: async () => {} };
},
}),
});
try {
await controller.start();
events[0].agentJoined();
events[0].dropped(); // DELETE s1 is deliberately held in flight.
await controller.start();
events[1].agentJoined();
expect(view?.phase).toBe('listening');
releaseOld();
await new Promise(resolve => setTimeout(resolve, 0));
expect(controller.active).toBe(true);
expect(view?.phase).toBe('listening');
} finally {
releaseOld();
await controller.end();
}
});
+400
View File
@@ -0,0 +1,400 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mergeTurns, recentRows, VoiceController, voiceStatus, WORDS, type MicTrack, type RoomEvents, type VoiceRoom, type VoiceView } from '../src/voice/session';
import type { ServerMessage } from '../src/adapters/real';
import type { Message } from '../src/domain/model';
// Fake transport and media objects: no LiveKit, no microphone, no network. The fake server follows the HEL-877 voice
// router (src/cybergril/voice/router.py): GET /api/voice/status, POST /api/voice/session -> 201 grant, DELETE
// /api/voice/session/{id} -> 200 or 404.
const gate = () => { let open!: () => void; const promise = new Promise<void>(r => { open = r; }); return { promise, open }; };
const flush = async (n = 8) => { for (let i = 0; i < n; i++) await Promise.resolve(); await new Promise(r => setTimeout(r, 0)); };
class Mic implements MicTrack { stopped = 0; ended?: () => void; stop() { this.stopped++; } onEnded(cb: () => void) { this.ended = cb; } }
class Room implements VoiceRoom {
closed = 0; published: MicTrack[] = []; hushed: boolean[] = []; on!: RoomEvents;
async publish(mic: MicTrack) { this.published.push(mic); }
hush(on: boolean) { this.hushed.push(on); }
async resumeAudio() {}
async close() { this.closed++; }
}
class Transport {
mics: Mic[] = []; rooms: Room[] = []; signals: AbortSignal[] = [];
micGate?: ReturnType<typeof gate>; joinGate?: ReturnType<typeof gate>; micError?: Error; joinError?: Error;
async capture() { const g = this.micGate; if (g) await g.promise; if (this.micError) throw this.micError; const m = new Mic(); this.mics.push(m); return m; }
async join(_url: string, _token: string, on: RoomEvents, signal: AbortSignal) { const r = new Room(); r.on = on; this.rooms.push(r); this.signals.push(signal); const g = this.joinGate; if (g) await g.promise; if (this.joinError) throw this.joinError; return r; }
}
class Server {
status: Record<string, unknown> | number = { configured: true, tts_ok: true, agent_name: 'cybergril-voice-agent', voice: false };
open = new Set<string>(); made = 0; calls: { method: string; url: string; keepalive?: boolean }[] = [];
postGate?: ReturnType<typeof gate>; refuse?: { status: number; detail: string };
// DELETEs of these sessions wait for the gate (a slow server answering for an older call).
deleteGate?: { ids: Set<string>; gate: ReturnType<typeof gate> };
fetch = vi.fn(async (url: string, init: RequestInit = {}) => {
const method = init.method || 'GET';
this.calls.push({ method, url, keepalive: init.keepalive });
if (url === '/api/voice/status') return typeof this.status === 'number' ? new Response('{"detail":"Not Found"}', { status: this.status }) : new Response(JSON.stringify(this.status));
if (url === '/api/voice/session' && method === 'POST') {
const g = this.postGate; if (g) await g.promise;
if (this.refuse) return new Response(JSON.stringify({ detail: this.refuse.detail }), { status: this.refuse.status });
const id = `s${++this.made}`; this.open.add(id);
return new Response(JSON.stringify({ url: 'wss://lk.test', token: `tok-${id}`, room: `cg-voice-${id}`, session_id: id, agent_name: 'cybergril-voice-agent', epoch: 3, expires_at: 0 }), { status: 201 });
}
const m = /^\/api\/voice\/session\/(.+)$/u.exec(url);
if (m && method === 'DELETE' && this.deleteGate?.ids.has(decodeURIComponent(m[1]))) await this.deleteGate.gate.promise;
if (m && method === 'DELETE') return this.open.delete(decodeURIComponent(m[1])) ? new Response('{"ended":true}') : new Response('{"detail":"这次通话已经不在了。"}', { status: 404 });
return new Response('{"detail":"Not Found"}', { status: 404 });
});
}
function rig() {
const server = new Server(), transport = new Transport(), views: VoiceView[] = [], record = vi.fn(), spoke = vi.fn();
const c = new VoiceController({ transport: async () => transport, onView: v => views.push(v), onRecord: record, onSpoke: spoke, fetch: server.fetch as unknown as typeof fetch, secure: () => true, agentWaitMs: 1000 });
const view = () => views.at(-1)!;
return { server, transport, views, view, record, spoke, c };
}
async function connected() {
const r = rig(); await r.c.start(); r.transport.rooms[0].on.agentJoined(); return r;
}
afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); });
describe('honest switch', () => {
it('reads status only: no room, no microphone', async () => {
const s = new Server();
expect(await voiceStatus(new AbortController().signal, s.fetch as unknown as typeof fetch)).toEqual({ available: true, reason: '' });
s.status = { configured: false, tts_ok: false, voice: false };
expect((await voiceStatus(new AbortController().signal, s.fetch as unknown as typeof fetch)).reason).toBe(WORDS.notConfigured);
s.status = { configured: true, tts_ok: false, voice: false };
expect((await voiceStatus(new AbortController().signal, s.fetch as unknown as typeof fetch)).reason).toBe(WORDS.ttsDown);
s.status = 404;
expect((await voiceStatus(new AbortController().signal, s.fetch as unknown as typeof fetch)).reason).toBe(WORDS.noRoute);
const down = vi.fn(async () => { throw new TypeError('Failed to fetch'); });
expect((await voiceStatus(new AbortController().signal, down as unknown as typeof fetch)).reason).toBe(WORDS.unknown);
expect(s.calls.every(c => c.method === 'GET' && c.url === '/api/voice/status')).toBe(true);
});
it('configured && tts_ok never means "verified": voice:false is not read as connected', async () => {
const r = rig(); r.server.status = { configured: true, tts_ok: true, voice: false };
await r.c.start();
// Starting is allowed (the user asked), but the page only says what the room is doing, never "已接通".
expect(r.views.map(v => v.phase)).toEqual(['opening', 'joining']);
});
it('refuses honestly before touching the microphone when the server cannot speak', async () => {
const r = rig(); r.server.status = { configured: true, tts_ok: false };
await r.c.start();
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.ttsDown });
expect(r.transport.mics).toHaveLength(0);
expect(r.server.calls.some(c => c.method === 'POST')).toBe(false);
});
it('an insecure page is told why, without asking for the microphone', async () => {
const r = rig(); const c = new VoiceController({ transport: async () => r.transport, onView: v => r.views.push(v), onRecord: r.record, onSpoke: r.spoke, fetch: r.server.fetch as unknown as typeof fetch, secure: () => false });
await c.start();
expect(r.view().notice).toBe(WORDS.insecure);
expect(r.server.calls).toHaveLength(0);
});
});
describe('lifecycle', () => {
it('opens, joins, publishes the microphone, follows her state; ending stops everything', async () => {
const r = await connected(), room = r.transport.rooms[0], mic = r.transport.mics[0];
expect(room.published).toEqual([mic]);
expect(r.view().phase).toBe('listening');
room.on.agentState('thinking'); expect(r.view().phase).toBe('thinking');
room.on.agentState('speaking'); expect(r.view().phase).toBe('speaking');
room.on.agentState('listening'); expect(r.view().phase).toBe('listening');
expect(r.server.open.size).toBe(1);
await r.c.end();
expect(mic.stopped).toBeGreaterThan(0);
expect(room.closed).toBe(1);
expect(r.server.open.size).toBe(0);
expect(r.view()).toMatchObject({ phase: 'off', notice: '' });
expect(r.c.active).toBe(false);
});
it('permission denied: nothing opened on the server, nothing left running', async () => {
const r = rig(); r.transport.micError = Object.assign(new Error('denied'), { name: 'NotAllowedError' });
await r.c.start();
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.denied });
expect(r.server.calls.filter(c => c.method !== 'GET')).toHaveLength(0);
r.transport.micError = Object.assign(new Error('none'), { name: 'NotFoundError' });
await r.c.start(); expect(r.view().notice).toBe(WORDS.noMic);
});
it('server refusal shows its own words and releases the microphone', async () => {
const r = rig(); r.server.refuse = { status: 409, detail: '还没有创建真实伴侣,语音也不会开始。' };
await r.c.start();
expect(r.view().notice).toBe('还没有创建真实伴侣,语音也不会开始。');
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
expect(r.transport.rooms).toHaveLength(0);
});
it('a failed join releases the microphone and gives the room back', async () => {
const r = rig(); r.transport.joinError = new Error('ws failed');
await r.c.start();
expect(r.view().notice).toBe(WORDS.unreachable);
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
expect(r.server.open.size).toBe(0);
});
it('ending while the microphone prompt is open: the late microphone is stopped, no room is asked for', async () => {
const r = rig(); r.transport.micGate = gate();
const starting = r.c.start(); await flush();
await r.c.end();
r.transport.micGate.open(); await starting; await flush();
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
expect(r.server.calls.some(c => c.method === 'POST')).toBe(false);
expect(r.view().phase).toBe('off');
});
it('ending while the room is being opened: the late grant is given back, never joined', async () => {
const r = rig(); r.server.postGate = gate();
const starting = r.c.start(); await flush();
await r.c.end();
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
r.server.postGate.open(); await starting; await flush();
expect(r.server.made).toBe(1);
expect(r.server.open.size).toBe(0);
expect(r.transport.rooms).toHaveLength(0);
expect(r.view().phase).toBe('off');
});
it('ending while joining: the join is aborted, a late room is closed, the server room deleted', async () => {
const r = rig(); r.transport.joinGate = gate();
const starting = r.c.start(); await flush();
expect(r.view().phase).toBe('joining');
await r.c.end();
expect(r.transport.signals[0].aborted).toBe(true);
r.transport.joinGate.open(); await starting; await flush();
expect(r.transport.rooms[0].closed).toBe(1);
expect(r.server.open.size).toBe(0);
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
expect(r.view().phase).toBe('off');
});
it('rapid on/off/on: only the last call stays, every earlier microphone and room is released', async () => {
const r = rig();
const a = r.c.start(); void r.c.end(); const b = r.c.start(); const c = r.c.start();
await Promise.all([a, b, c]); await flush();
expect(r.server.open.size).toBe(1);
const live = r.transport.mics.filter(m => !m.stopped);
expect(live).toHaveLength(1);
expect(r.transport.rooms.filter(x => !x.closed)).toHaveLength(1);
await r.c.end(); await flush();
expect(r.server.open.size).toBe(0);
expect(r.transport.mics.every(m => m.stopped)).toBe(true);
});
it('late events from an ended call never bring it back', async () => {
const r = await connected(), old = r.transport.rooms[0];
await r.c.end(); r.record.mockClear();
const seen = r.views.length;
old.on.agentState('speaking'); old.on.subtitle({ kind: 'user_final', role: 'user', text: '旧的话' }); old.on.agentJoined(); old.on.dropped(); old.on.audioBlocked(true);
r.transport.mics[0].ended?.();
await flush();
expect(r.views.length).toBe(seen);
expect(r.record).not.toHaveBeenCalled(); expect(r.spoke).not.toHaveBeenCalled();
expect(r.c.active).toBe(false);
});
it('the room dropping on its own (network, server ended it) cleans up and says so', async () => {
const r = await connected();
r.transport.rooms[0].on.dropped(); await flush();
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.dropped });
expect(r.transport.mics[0].stopped).toBeGreaterThan(0);
expect(r.server.open.size).toBe(0);
});
it('microphone permission revoked mid-call ends the call', async () => {
const r = await connected();
r.transport.mics[0].ended?.(); await flush();
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.micGone });
expect(r.server.open.size).toBe(0);
expect(r.transport.rooms[0].closed).toBe(1);
});
it('her worker never joining is a failure, not an endless wait', async () => {
vi.useFakeTimers();
const r = rig(); await r.c.start();
expect(r.view().phase).toBe('joining');
await vi.advanceTimersByTimeAsync(1100);
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.noAgent });
expect(r.server.open.size).toBe(0);
});
it('offline ends with its own words; unload releases at once and the DELETE survives the page', async () => {
const r = await connected();
await r.c.end(WORDS.offline);
expect(r.view().notice).toBe(WORDS.offline);
const u = await connected();
u.c.unload();
expect(u.transport.mics[0].stopped).toBeGreaterThan(0);
await flush();
expect(u.server.calls.find(c => c.method === 'DELETE')?.keepalive).toBe(true);
expect(u.server.open.size).toBe(0);
});
it('a DELETE that cannot get through (offline) is owed, survives a new page, and is sent once back online', async () => {
const data = new Map<string, string>();
vi.stubGlobal('localStorage', { getItem: (k: string) => data.get(k) ?? null, setItem: (k: string, v: string) => void data.set(k, v), removeItem: (k: string) => void data.delete(k) });
const r = await connected(), real = r.server.fetch.getMockImplementation()!;
r.server.fetch.mockImplementation(async (url: string, init: RequestInit = {}) => { if (init.method === 'DELETE') { r.server.calls.push({ method: 'DELETE', url }); throw new TypeError('Failed to fetch'); } return real(url, init); });
await r.c.end(WORDS.offline);
expect(r.transport.mics[0].stopped).toBeGreaterThan(0); // local capture stops regardless
expect(r.server.open.size).toBe(1); // the server room is not pretended gone
expect(JSON.parse(data.get('cg.voice.owed')!)).toEqual(['s1']);
r.server.fetch.mockImplementation(real);
// A new page (new controller) settles it: once, then forgets.
const again = new VoiceController({ transport: async () => r.transport, onView: () => {}, onRecord: () => {}, onSpoke: () => {}, fetch: r.server.fetch as unknown as typeof fetch, secure: () => true });
await again.settle();
expect(r.server.open.size).toBe(0);
expect(data.has('cg.voice.owed')).toBe(false);
const before = r.server.calls.length; await again.settle();
expect(r.server.calls.length).toBe(before);
});
it('an owed room already gone on the server (404) counts as settled', async () => {
const data = new Map<string, string>([['cg.voice.owed', '["gone-1"]']]);
vi.stubGlobal('localStorage', { getItem: (k: string) => data.get(k) ?? null, setItem: (k: string, v: string) => void data.set(k, v), removeItem: (k: string) => void data.delete(k) });
const r = rig(); await r.c.settle();
expect(r.server.calls.at(-1)).toMatchObject({ method: 'DELETE', url: '/api/voice/session/gone-1' });
expect(data.has('cg.voice.owed')).toBe(false);
});
it('never confirms a receipt or posts chat itself: the worker owns both', async () => {
const r = await connected(), room = r.transport.rooms[0];
room.on.subtitle({ kind: 'user_final', role: 'user', text: '今天好冷' });
room.on.subtitle({ kind: 'assistant_final', role: 'assistant', text: '那多穿一件。' });
await r.c.end();
expect(r.server.calls.some(c => c.url.startsWith('/api/chat'))).toBe(false);
});
});
// An older call's cleanup may answer long after a new call is up (a slow DELETE, the settle retry for it, a late
// microphone): it may only release the older call's own things, never touch the new call's view, microphone or room.
describe('a late end of an older call', () => {
const hold = (r: ReturnType<typeof rig>, ...ids: string[]) => { const g = gate(); r.server.deleteGate = { ids: new Set(ids), gate: g }; return g; };
const reopened = async (r: ReturnType<typeof rig>, i: number) => { await r.c.start(); r.transport.rooms[i].on.agentJoined(); expect(r.view().phase).toBe('listening'); };
const stillNew = (r: ReturnType<typeof rig>, i: number) => {
expect(r.c.active).toBe(true);
expect(r.view()).toMatchObject({ phase: 'listening', notice: '' });
expect(r.transport.mics[i].stopped).toBe(0);
expect(r.transport.rooms[i].closed).toBe(0);
expect(r.transport.mics.slice(0, i).every(m => m.stopped > 0)).toBe(true);
expect(r.transport.rooms.slice(0, i).every(x => x.closed > 0)).toBe(true);
};
const failures: [string, (r: ReturnType<typeof rig>) => void][] = [
['the room dropped', r => r.transport.rooms[0].on.dropped()],
['the microphone was taken away', r => r.transport.mics[0].ended?.()],
];
for (const [what, cause] of failures) {
it(`${what}, its DELETE (and the settle retry) answering after a new call is up, leaves the new call alone`, async () => {
const r = await connected(), slow = hold(r, 's1');
cause(r); await flush();
expect(r.view()).toMatchObject({ phase: 'off' });
expect(r.transport.mics[0].stopped).toBeGreaterThan(0); // released at once, not after the server answers
expect(r.c.active).toBe(false);
await reopened(r, 1);
expect(r.server.calls.filter(c => c.method === 'DELETE' && c.url.endsWith('/s1')).length).toBe(2); // first try + settle
const seen = r.views.length;
slow.open(); await flush();
expect(r.views.length).toBe(seen);
stillNew(r, 1);
expect(r.server.open).toEqual(new Set(['s2']));
await r.c.end(); await flush();
expect(r.server.open.size).toBe(0);
expect(r.transport.mics.every(m => m.stopped)).toBe(true);
});
}
it('her worker never joined, the DELETE is slow, and a new call is started before it answers', async () => {
vi.useFakeTimers();
const r = rig(); await r.c.start(); const slow = hold(r, 's1');
await vi.advanceTimersByTimeAsync(1100);
expect(r.view()).toMatchObject({ phase: 'off', notice: WORDS.noAgent });
await reopened(r, 1);
slow.open(); await vi.advanceTimersByTimeAsync(0);
stillNew(r, 1);
await r.c.end();
});
it('a normal end whose DELETE is slow, then a new call, then the old answer arrives', async () => {
const r = await connected(), slow = hold(r, 's1');
const ending = r.c.end();
expect(r.view()).toMatchObject({ phase: 'off', notice: '' });
await reopened(r, 1);
slow.open(); await ending; await flush();
stillNew(r, 1);
expect(r.server.open).toEqual(new Set(['s2']));
await r.c.end();
});
it('starting again while the previous start still waits for the old room to be given back: only the newest call survives', async () => {
const r = await connected(), slow = hold(r, 's1');
const b = r.c.start(); await flush(); // B ends A; A's DELETE is held
const c = r.c.start(); await flush(); // C does not wait behind B
r.transport.rooms.at(-1)!.on.agentJoined();
slow.open(); await Promise.all([b, c]); await flush(); // B wakes up late: it must not take over
const last = r.transport.rooms.length - 1;
expect(r.transport.rooms.filter(x => !x.closed)).toEqual([r.transport.rooms[last]]);
expect(r.transport.mics.filter(m => !m.stopped)).toEqual([r.transport.mics[last]]);
expect(r.server.open.size).toBe(1);
stillNew(r, last);
await r.c.end(); await flush();
expect(r.server.open.size).toBe(0);
expect(r.transport.mics.every(m => m.stopped)).toBe(true);
expect(r.transport.rooms.every(x => x.closed)).toBe(true);
});
it('a microphone granted late to an older start is stopped without touching the new call', async () => {
const r = rig(); r.transport.micGate = gate(); const late = r.transport.micGate;
const a = r.c.start(); await flush();
r.transport.micGate = undefined;
await reopened(r, 0);
late.open(); await a; await flush();
expect(r.transport.mics).toHaveLength(2);
expect(r.transport.mics[1].stopped).toBeGreaterThan(0); // A's late microphone
expect(r.transport.mics[0].stopped).toBe(0); // the live call's own
expect(r.c.active).toBe(true);
expect(r.view()).toMatchObject({ phase: 'listening', notice: '' });
expect(r.server.made).toBe(1);
await r.c.end();
});
});
describe('subtitles and the two channels', () => {
it('interim is a draft; user_final cancels a text reply and asks for the record; φ is heard but not recorded', async () => {
const r = await connected(), room = r.transport.rooms[0];
room.on.subtitle({ kind: 'interim', role: 'user', text: '今天' });
expect(r.view().heard).toBe('今天'); expect(r.record).not.toHaveBeenCalled();
room.on.subtitle({ kind: 'user_final', role: 'user', text: '今天好冷' });
expect(r.spoke).toHaveBeenCalledTimes(1); expect(r.record).toHaveBeenCalledTimes(1);
room.on.subtitle({ kind: 'error', role: 'assistant', text: '我刚才走神了,你再说一遍?' });
expect(r.view().spoken).toBe('我刚才走神了,你再说一遍?');
room.on.subtitle({ kind: 'assistant_final', role: 'assistant', text: '那多穿一件。' });
expect(r.view()).toMatchObject({ heard: '', spoken: '' });
room.on.subtitle({ kind: 'bogus', text: 1 }); room.on.subtitle(null);
expect(r.record).toHaveBeenCalledTimes(3);
});
it('without the agent state attribute, subtitles still move the state honestly', async () => {
const r = await connected(), room = r.transport.rooms[0];
room.on.subtitle({ kind: 'user_final', role: 'user', text: '在吗' }); expect(r.view().phase).toBe('thinking');
room.on.subtitle({ kind: 'assistant_final', role: 'assistant', text: '在。' }); expect(r.view().phase).toBe('listening');
});
it('typing over her spoken answer silences what is left of it until she is listening again', async () => {
const r = await connected(), room = r.transport.rooms[0];
r.c.hush(); expect(room.hushed).toEqual([]); // listening: nothing to silence
room.on.agentState('speaking'); r.c.hush();
expect(room.hushed).toEqual([true]);
room.on.agentState('speaking'); expect(room.hushed).toEqual([true]);
room.on.agentState('listening'); expect(room.hushed).toEqual([true, false]);
});
});
describe('record re-read', () => {
const msg = (id: number, role: Message['role'], turnId: string, extra: Partial<Message> = {}): Message => ({ id: String(id), role, text: `${role}${id}`, lifeIds: [], turnId, status: 'sent', ...extra });
it('adds voice turns once, in id order; leaves reach-outs to the inbox and text turns to the stream', () => {
const known = [msg(1, 'user', 't1'), msg(2, 'companion', 't1'), { ...msg(0, 'user', 'local-turn'), id: 'abc-local', status: 'pending' as const }];
const rows = [msg(5, 'companion', 'v1'), msg(4, 'user', 'v1'), msg(6, 'companion', 'r6', { proactive: true }), msg(7, 'user', 'local-turn'), msg(2, 'companion', 't1')];
const next = mergeTurns(known, rows)!;
expect(next.map(m => m.id)).toEqual(['1', '2', 'abc-local', '4', '5']);
expect(mergeTurns(next, rows)).toBeNull();
});
it('a pending voice row settles in place (sent or cancelled), never duplicated', () => {
const known = [msg(4, 'user', 'v1', { status: 'pending' })];
const next = mergeTurns(known, [msg(4, 'user', 'v1', { status: 'cancelled' })])!;
expect(next).toHaveLength(1); expect(next[0].status).toBe('cancelled');
});
it('reads newest pages down to what the page holds, bounded', async () => {
const rows: ServerMessage[] = Array.from({ length: 130 }, (_, i) => ({ id: i + 1, role: i % 2 ? 'companion' : 'user', text: `第${i + 1}句`, turn_id: `t${i}`, status: 'sent' }));
const calls: string[] = [];
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
calls.push(url);
const u = new URL(url, 'http://x'), limit = Number(u.searchParams.get('limit')), before = Number(u.searchParams.get('before') || Infinity);
const desc = rows.filter(r => r.id < before).sort((a, b) => b.id - a.id).slice(0, limit + 1), more = desc.length > limit, page = desc.slice(0, limit);
return new Response(JSON.stringify({ messages: [...page].reverse(), next_before: more ? page.at(-1)!.id : null }));
}));
expect((await recentRows(120, new AbortController().signal)).map(m => m.id)).toEqual(['121', '122', '123', '124', '125', '126', '127', '128', '129', '130']);
expect(calls).toHaveLength(1);
expect(await recentRows(60, new AbortController().signal)).toHaveLength(70);
calls.length = 0;
expect(await recentRows(0, new AbortController().signal, 2)).toHaveLength(100);
expect(calls).toHaveLength(2);
});
});