Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f905df72f3 |
@@ -48,3 +48,7 @@ models/
|
||||
*.swp
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Independent web demo
|
||||
web/node_modules/
|
||||
web/dist/
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
# HEL-715 素材包规范与施工方案(可交施工员)
|
||||
|
||||
依据 CG-029(日常用成品视频、交谈用同一人物实时口型)、CG-005/006(换外观保留人格/记忆/关系)、CG-030(首批素材小队制作)。原则:单图不自动生成整套;每份素材可独立替换、可校验、可回滚。
|
||||
|
||||
## 1. 素材包目录规范
|
||||
|
||||
```
|
||||
assets/personas/<persona>/ # persona=外观方案,如 wanwan-v2;换外观=新目录
|
||||
manifest.json # 见下
|
||||
daily/ # 日常成品视频(CG-029 主路线)
|
||||
<scene-id>.mp4 # H.264 25fps ≥6s 可无缝循环;按时段/天气/活动命名
|
||||
talk/ # 交谈底板(近景,同房间同脸同服装)
|
||||
<talk-id>.mp4 # H.264 25fps 5~15s,人物目视镜头、口部微闭或自然微动
|
||||
voice/ # CosyVoice 音色登记名(.12 voices.json 中的 voice 名)
|
||||
ref/ # 定妆照/场景照 PNG(身份对照用)
|
||||
```
|
||||
|
||||
`manifest.json` 必填字段:`persona_id`、`identity_ref`(ref/ 内主定妆图)、`daily_clips[{id, timeofday, weather, activity, loop_points}]`、`talk_clips[{id, avatar_id, framing}]`、`created_by`、`created_at`、`pipeline_version`(如 hel715-v1)。
|
||||
|
||||
## 2. 身份/服装/光线一致性标记(导入校验项)
|
||||
|
||||
- 同一 persona 内所有 daily/talk 片段必须与 `identity_ref` 同人物、同发型、同套服装;换服装=新 persona 目录。
|
||||
- 每段导入时人工对照 ref 图打勾(首版人工,不做自动人脸embedding校验——留待后续)。
|
||||
- 光线:同一 persona 的交谈底板光线需与常用日常场景一致(近景底板从日常成品裁切放大可天然保证,如本轮 talk-close = loop 1.75× 裁切)。
|
||||
|
||||
## 3. 可切节点与缺素材回退
|
||||
|
||||
- 日常片循环点:`loop_points` 记录首尾衔接帧(硬循环跳变处需美术确认或选首尾近同帧的段)。
|
||||
- 交谈进入点:任意时刻可切(预连接流,实测切换 0~3ms);退出点同。
|
||||
- 缺素材回退顺序:请求场景无 daily 片 → 用同 persona 默认 daily 片;无 talk 底板 → 禁用交谈入口并提示,不得用其他人物素材顶替;TTS 失败 → 显示重试按钮,不静默。
|
||||
|
||||
## 4. 导入与回滚流程(本轮实测命令)
|
||||
|
||||
```
|
||||
# 1) 预处理:任意源 → H.264 25fps(示例见 scripts/,PyAV,秒级)
|
||||
# 2) 生成交谈 avatar(47~60s GPU/份,独立目录,无硬编码):
|
||||
cd /opt/hel715-verify/LiveTalking
|
||||
venv/bin/python -m avatars.musetalk.genavatar --file <talk-clip.mp4> --avatar_id <persona>-<talk-id>
|
||||
# 3) 校验:data/avatars/<id>/ 含 avator_info.json/coords.pkl/latents.pt/mask;avator_info.json 的 video_path 指向素材包内文件
|
||||
# 4) 服务挂载:app.py --avatar_id <id>(或 custom_config 多 avatar 预载)
|
||||
```
|
||||
|
||||
回滚:素材包按目录整体保留旧版本(`<persona>.bak-<date>`),avatar 目录同理;manifest 记录版本,恢复=改回引用。模型权重不入 Git,assets 二进制走附件/对象存储,仓库只放 manifest 与脚本。
|
||||
|
||||
## 5. 换一套外观的实测成本(CG-005/006 输入)
|
||||
|
||||
| 项 | 实测耗时 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| 交谈底板由既有成品裁切 | <1min(PyAV 转码)+ 47~60s genavatar | 本轮 talk-close 路线,推荐 |
|
||||
| 交谈底板由 Wan2.2 全新生成 | ~57s/3s 片段 + 挑选重试(3~5 倍余量) | HEL-712 实测单次推理 |
|
||||
| 日常成品视频生成 | 同上量级/段 | 小队批量制作 |
|
||||
| 照片需求 | 定妆/场景各 ≥1 张 | 用于身份对照与 i2v 起点 |
|
||||
| 预处理 | 自动,无人工参数 | bbox_shift=0 默认可用 |
|
||||
|
||||
## 6. 首版最小验收矩阵(交谈链路)
|
||||
|
||||
| 项 | 通过标准 |
|
||||
| --- | --- |
|
||||
| 日常循环播放 | ≥3 周期无肉眼跳变(或美术确认衔接) |
|
||||
| 进交谈 | 切换 ≤200ms,同人物同服装目视确认 |
|
||||
| 口型 | 说话段口部动作肉眼可辨,静息段无明显异常 |
|
||||
| 打断 | interrupt 后 ≤1s 静音,不等播完 |
|
||||
| 再开口 | 打断后可立即再次发话 |
|
||||
| 回日常 | 切回 ≤200ms,日常从暂停点续播 |
|
||||
| 持续 | 单次会话 ≥10 分钟不崩、显存不涨 |
|
||||
| 失败路径 | TTS 断连时有可见错误提示,可恢复 |
|
||||
|
||||
## 7. 职责划分(衔接 HEL-714 施工)
|
||||
|
||||
- 客户端(web):日常选片/循环、交谈视图挂载(预连接 WebRTC)、打断信号(先按钮/快捷键,VAD 后置)、失败回退 UI。
|
||||
- 服务端:LiveTalking(交谈渲染 + TTS 适配,本分支 patch);日常视频静态文件由现有 web 服务出。
|
||||
- 素材:美术/施工按第 1 节规范产出,总工验收导入。
|
||||
@@ -1,42 +0,0 @@
|
||||
# HEL-715 实验复现说明
|
||||
|
||||
实验环境(.12,全部隔离在 `/opt/hel715-verify/`,未改系统 Python/CUDA/ASR/TTS):
|
||||
|
||||
- LiveTalking c726943(官方 GitHub)+ MuseTalk v1.5 权重 3.8GB(`LiveTalking/models/`)
|
||||
- venv:`/opt/hel715-verify/venv`(torch 2.8.0+cu128、aiortc 1.15.0、PyAV 17.1.0)
|
||||
- 既有服务复用:CosyVoice TTS `http://127.0.0.1:9880`(音色 xiaoyun)、LLM `http://192.168.200.11:8731`(halogen-qwen3.8-flash-next)
|
||||
- 对上游的两处改动:`livetalking-patch/cosyvoice.py`(新增,对接 .12 CosyVoice)、`livetalking-patch/enable-cosyvoice-registration.diff`(base_avatar.py 启用注册)
|
||||
|
||||
## 启动
|
||||
|
||||
```bash
|
||||
cd /opt/hel715-verify/LiveTalking
|
||||
nohup /opt/hel715-verify/venv/bin/python app.py --model musetalk \
|
||||
--avatar_id hel715-talk-close --transport webrtc \
|
||||
--tts cosyvoice --REF_FILE xiaoyun \
|
||||
--TTS_SERVER http://127.0.0.1:9880 --listenport 8010 \
|
||||
> /opt/hel715-verify/livetalking.log 2>&1 &
|
||||
```
|
||||
|
||||
注意:仓库内 `config.yaml` 的 `TTS_SERVER: ''` 会覆盖 argparse 默认值,必须显式传 `--TTS_SERVER`(本轮曾因此空跑过一次)。
|
||||
|
||||
已生成 avatar(`LiveTalking/data/avatars/`):`hel715-loop-wide`(loop-25fps.mp4,196 帧全景)、`hel715-talk-close`(talk-close.mp4,196 帧近景=loop 1.75× 裁切)、`hel715-return`(return-25fps.mp4,77 帧独立源)。
|
||||
|
||||
## 测试脚本
|
||||
|
||||
- `scripts/e2e_client.py`:aiortc 收流客户端,四句时间线(含打断),产出 mp4+stats.json+events.json+contact-sheet.png。在 .12 上运行:`E2E_OUT=... venv/bin/python e2e_client.py`。
|
||||
- `scripts/llm_chain2.py`:LLM→TTS→数字人单链路采样。
|
||||
- `scripts/hel715-chain.html`:日常→交谈→打断→回日常→再交谈演示页(部署于 LiveTalking `web/`,`?auto=1` 自动演示;依赖 `web/hel715-daily.webm`)。
|
||||
- `scripts/record_chain.py`:本机(192.168.200.13)Playwright+Chromium 录制上述页面,需 LD_LIBRARY_PATH 指向解包的运行库(脚本内已写死 /tmp 路径,迁移时改)。
|
||||
|
||||
## 清理
|
||||
|
||||
```bash
|
||||
# 停实验服务(先对 PID:pgrep -af 'app.py --model musetalk')
|
||||
pkill -f 'app.py --model musetalk'
|
||||
# 删除整个实验目录(含 venv/权重/素材,约 12GB)
|
||||
rm -rf /opt/hel715-verify
|
||||
# 演示页随目录一起消失;18080/18081 旧演示与 ASR/TTS 未动
|
||||
```
|
||||
|
||||
本轮收尾状态:LiveTalking 已停止;目录与权重保留供 HEL-714 复用。
|
||||
@@ -1,73 +0,0 @@
|
||||
# HEL-715 技术验证报告:本地真人交谈与日常视频衔接
|
||||
|
||||
日期:2026-10-20 | 环境:192.168.200.12(RTX 5090D 32GB,torch 2.8.0+cu128)
|
||||
候选:LiveTalking @ c726943(官方 GitHub)+ MuseTalk v1.5 + .12 既有 CosyVoice(9880) + .11 LLM(halogen-qwen3.8-flash-next)
|
||||
|
||||
## 一句话结论
|
||||
|
||||
**有条件可用。** 日常↔交谈切换、打断、25fps 连续口型视频、素材流水线复用均实测通过;条件是接受 ①发话到出声约 4.5~5.5s 的起步延迟,②TTS 流式按标点分句带来的句内 0.4~1.1s 停顿,③交谈驱动只改口型(表情/目光来自底板视频)。三项都需要在首版验收时向用户如实展示,其中 ①② 可在施工阶段继续压。
|
||||
|
||||
## 四个问题的实测回答
|
||||
|
||||
### 1. 主候选 LiveTalking+MuseTalk 实际跑通了吗?——跑通
|
||||
|
||||
- 安装:官方仓库 clone 至 `/opt/hel715-verify/LiveTalking`,独立 venv(不改系统 Python/CUDA/ASR/TTS);权重 musetalkV15/face-parse/sd-vae/whisper 共约 3.8GB,已就位。
|
||||
- 适配:仅两处改动(见 `livetalking-patch/`):`tts/cosyvoice.py` 新增(对接 .12 既有 CosyVoice 的 POST /tts 24kHz PCM 流)+ `avatars/base_avatar.py` 启用该 TTS 注册。其余代码未动。
|
||||
- 能力边界(CG-028 相关,明确来源):
|
||||
- **口型**:实时音频驱动(whisper 特征→unet 逐帧生成下半脸)——实测(口部区差分:静息中位 0.74 vs 说话峰值 13~14)。
|
||||
- **表情/目光/头姿/身体**:全部来自底板视频循环(预处理时逐帧 landmark+face-parse 固化),**不是**实时生成——来源:代码结构+录像复核。交谈底板选片时必须挑"目视镜头、微动作"段。
|
||||
- 不要求也不提供逐句新肩部手势;与 CG-027/028 的预期一致,未发现必须换 Ditto 等替代的实证。
|
||||
|
||||
### 2. 同一人物实时交谈可否成立?——可以,起步延迟与句内停顿需接受
|
||||
|
||||
实测(aiortc 客户端走真实 WebRTC 通道,含网络传输;e2e-session.mp4 / stats.json):
|
||||
|
||||
| 指标 | 实测值 |
|
||||
| --- | --- |
|
||||
| 连接→首帧视频 | 3.6s(含 ICE 协商 1.7s) |
|
||||
| 文本→首次出声 | 4.6~5.5s(CosyVoice 首包 3.4s + LiveTalking 音频缓冲约 1.2s) |
|
||||
| 连续帧率 | 25.05fps 恒定(27s 全程,说话与静息一致) |
|
||||
| 音画同步 | 容器内音视频齐备;口型动作较出声晚约 0.5~1.3s(MuseTalk 音频前瞻缓冲所致)。自动互相关度量噪声大,最终以录像人工复核为准 |
|
||||
| 句内停顿 | TTS 按标点分句合成,句内间隙 0.45~1.05s("抱歉"与"你刚想说什么"之间 0.66s)——CosyVoice 流式模式固有,非口型管线问题 |
|
||||
| 打断→静音 | **0.35s**(interrupt_talk 后队列即清) |
|
||||
| 打断后再开口 | 正常(S3/S4 均正常播出) |
|
||||
| 峰值资源 | 总显存 18.9GB(基线 5.8GB + CosyVoice 4.2GB + LiveTalking 空载 11.2GB/说话峰值约 13.1GB);GPU 利用率瞬时 100% 但帧率不跌 |
|
||||
| 失败行为 | 本轮未触发服务崩溃;语音名不存在时 TTS 适配层回退 default 音色 |
|
||||
|
||||
LLM 链路(llm-chain.mp4):.11 真实模型 halogen-qwen3.8-flash-next 回复 1.33s → 递给数字人出声共 4.63s,全程录音正常。
|
||||
|
||||
### 3. 日常→交谈→打断→回日常衔接?——信号级链路实测通过
|
||||
|
||||
两份证据:
|
||||
|
||||
- **浏览器真实链路**(chain/page@*.webm,Playwright+Chromium 走 LAN WebRTC):日常成品视频循环 → 切交谈视图(预连接流,切换 0~3ms)→ 发文本 → 语音进行中注入 interrupt_talk(HTTP 2~5ms)→ 立即切回日常(不等她说完、不等日常片尾)→ 再次进入交谈并发第二句 → 回日常。全程 33.5s,页面零 JS 错误。注:Playwright 录屏不含音轨,音频证据见下条。
|
||||
- **媒体级连续音画**(e2e-session.mp4):同一 WebRTC 通道收到的连续音视频原样落盘,覆盖 静息→说话→打断→静默→再说话 全程,有声有画。
|
||||
|
||||
边界说明:麦克风/VAD 未接入,打断由信号注入触发,**只是信号级验证**,不冒充自然语音抢话已通过。转场为视图级切换(同人物同房间同服装的近景底板 vs 日常全景),自然转场(如淡入淡出)属施工项,不计入本轮结论。
|
||||
|
||||
### 4. 素材能否持续制作/替换/维护?——流水线已验证可复用
|
||||
|
||||
- 同一流水线已跑通 **3 份素材、3 个 avatar**:loop 全景(196 帧)、面部近景(196 帧,经核实为 loop 素材 1.75× 放大裁切,同人物同房间同服装)、回程片段(77 帧,独立源文件)。
|
||||
- 新素材仅需:转码 25fps mp4 → `python -m avatars.musetalk.genavatar --file X --avatar_id Y`,47~60s/份(GPU),零代码改动、目录隔离、无硬编码人脸。
|
||||
- 成本实测:交谈底板若由 Wan2.2 生成约 57s/3s 片段(HEL-712 实测);由既有成品裁切仅需秒级(PyAV)。详见 MATERIAL-SPEC.md。
|
||||
- 限制:本轮三份素材同属一个人物;跨人物泛化未实测(离线环境无第二真人素材可用),算法层面为通用 landmark/face-parse 管线,无按脸硬编码逻辑。
|
||||
|
||||
## CG-029/028 能力矩阵(来源/实测/未测)
|
||||
|
||||
| 能力 | 状态 | 依据 |
|
||||
| --- | --- | --- |
|
||||
| 成品日常视频按时段/活动选片播放 | 实测(浏览器循环播放) | chain webm |
|
||||
| 同人物近景实时口型交谈 | 实测 | e2e-session.mp4 / llm-chain.mp4 |
|
||||
| 口型由音频实时驱动 | 实测 | 口部差分 15~19 倍 |
|
||||
| 表情/目光实时生成 | 未测(来源=底板视频) | 代码结构 |
|
||||
| 语音打断(信号级) | 实测 0.35s 停止 | events.json |
|
||||
| 自然语音抢话(VAD) | 未测 | 麦克风未接 |
|
||||
| 日常↔交谈视图切换 | 实测 0~3ms | chain webm + 日志 |
|
||||
| LLM→TTS→口型全链 | 实测 | llm-chain.mp4 |
|
||||
| 素材替换保留人格记忆 | 设计已验证路径(素材目录隔离) | genavatar 复用 |
|
||||
|
||||
## 风险与建议(给总管的决策输入)
|
||||
|
||||
1. **起步延迟 4.5~5.5s** 是当前体验最大短板:可在施工时用"思考中"微动作/状态提示掩盖,或后续换更低首包延迟的 TTS 配置(CosyVoice 首包 3.4s 占大头)。无已确认延迟门槛,不编造 SLA;建议向用户展示实测录像后再定验收线。
|
||||
2. **句内停顿**可通过 TTS 侧整句缓冲或标点预处理缓解,代价是首包更慢——二者权衡留施工阶段。
|
||||
3. LiveTalking 空载即占 11.2GB 显存,与 ComfyUI 重生成任务并存时需调度(先交谈后生成的队列约定)。
|
||||
@@ -1,82 +0,0 @@
|
||||
import time
|
||||
import numpy as np
|
||||
import resampy
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from typing import Iterator
|
||||
|
||||
from utils.logger import logger
|
||||
from .base_tts import BaseTTS, State
|
||||
from registry import register
|
||||
|
||||
|
||||
@register("tts", "cosyvoice")
|
||||
class CosyVoiceTTS(BaseTTS):
|
||||
"""适配本机既有 CosyVoice3 流式 TTS (POST /tts -> 24kHz int16 PCM 流)."""
|
||||
|
||||
def txt_to_audio(self, msg: tuple[str, dict]):
|
||||
text, textevent = msg
|
||||
voice = textevent.get('tts', {}).get('voice', self.opt.REF_FILE or 'default')
|
||||
self.stream_tts(self.cosyvoice(text, voice), msg)
|
||||
|
||||
def cosyvoice(self, text: str, voice: str) -> Iterator[bytes]:
|
||||
start = time.perf_counter()
|
||||
try:
|
||||
res = requests.post(
|
||||
f"{self.opt.TTS_SERVER}/tts",
|
||||
json={"text": text, "voice": voice},
|
||||
stream=True,
|
||||
timeout=(5, 300),
|
||||
)
|
||||
if res.status_code != 200:
|
||||
logger.error("cosyvoice http %s: %s", res.status_code, res.text[:200])
|
||||
return
|
||||
first = True
|
||||
for chunk in res.iter_content(chunk_size=4800): # 100ms @24k int16
|
||||
if first:
|
||||
logger.info("cosyvoice time to first chunk: %.3fs", time.perf_counter() - start)
|
||||
first = False
|
||||
if chunk and self.state == State.RUNNING:
|
||||
yield chunk
|
||||
elif self.state != State.RUNNING:
|
||||
logger.info("cosyvoice interrupted, stop consuming")
|
||||
res.close()
|
||||
return
|
||||
except Exception:
|
||||
logger.exception('cosyvoice tts error')
|
||||
|
||||
def stream_tts(self, audio_stream: Iterator[bytes], msg: tuple[str, dict]):
|
||||
text, textevent = msg
|
||||
first = True
|
||||
for chunk in audio_stream:
|
||||
if not chunk:
|
||||
continue
|
||||
pcm24 = np.frombuffer(chunk, dtype=np.int16).astype(np.float32) / 32767.0
|
||||
stream = resampy.resample(pcm24, sr_orig=24000, sr_new=self.sample_rate)
|
||||
streamlen = stream.shape[0]
|
||||
idx = 0
|
||||
while streamlen >= self.chunk:
|
||||
eventpoint = {}
|
||||
if first:
|
||||
eventpoint = {'status': 'start', 'text': text}
|
||||
first = False
|
||||
eventpoint.update(**textevent)
|
||||
self.parent.put_audio_frame(stream[idx:idx + self.chunk], eventpoint)
|
||||
streamlen -= self.chunk
|
||||
idx += self.chunk
|
||||
eventpoint = {'status': 'end', 'text': text}
|
||||
eventpoint.update(**textevent)
|
||||
self.parent.put_audio_frame(np.zeros(self.chunk, np.float32), eventpoint)
|
||||
diff --git a/avatars/base_avatar.py b/avatars/base_avatar.py
|
||||
index 28c826a..a9744e6 100644
|
||||
--- a/avatars/base_avatar.py
|
||||
+++ b/avatars/base_avatar.py
|
||||
@@ -90,7 +90,7 @@ class BaseAvatar:
|
||||
'edgetts': 'tts.edge',
|
||||
'gpt-sovits': 'tts.sovits',
|
||||
'xtts': 'tts.xtts',
|
||||
- # 'cosyvoice': 'tts.cosyvoice',
|
||||
+ 'cosyvoice': 'tts.cosyvoice',
|
||||
# 'fishtts': 'tts.fish',
|
||||
'tencent': 'tts.tencent',
|
||||
'doubao': 'tts.doubao',
|
||||
@@ -1,69 +0,0 @@
|
||||
import time
|
||||
import numpy as np
|
||||
import resampy
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from typing import Iterator
|
||||
|
||||
from utils.logger import logger
|
||||
from .base_tts import BaseTTS, State
|
||||
from registry import register
|
||||
|
||||
|
||||
@register("tts", "cosyvoice")
|
||||
class CosyVoiceTTS(BaseTTS):
|
||||
"""适配本机既有 CosyVoice3 流式 TTS (POST /tts -> 24kHz int16 PCM 流)."""
|
||||
|
||||
def txt_to_audio(self, msg: tuple[str, dict]):
|
||||
text, textevent = msg
|
||||
voice = textevent.get('tts', {}).get('voice', self.opt.REF_FILE or 'default')
|
||||
self.stream_tts(self.cosyvoice(text, voice), msg)
|
||||
|
||||
def cosyvoice(self, text: str, voice: str) -> Iterator[bytes]:
|
||||
start = time.perf_counter()
|
||||
try:
|
||||
res = requests.post(
|
||||
f"{self.opt.TTS_SERVER}/tts",
|
||||
json={"text": text, "voice": voice},
|
||||
stream=True,
|
||||
timeout=(5, 300),
|
||||
)
|
||||
if res.status_code != 200:
|
||||
logger.error("cosyvoice http %s: %s", res.status_code, res.text[:200])
|
||||
return
|
||||
first = True
|
||||
for chunk in res.iter_content(chunk_size=4800): # 100ms @24k int16
|
||||
if first:
|
||||
logger.info("cosyvoice time to first chunk: %.3fs", time.perf_counter() - start)
|
||||
first = False
|
||||
if chunk and self.state == State.RUNNING:
|
||||
yield chunk
|
||||
elif self.state != State.RUNNING:
|
||||
logger.info("cosyvoice interrupted, stop consuming")
|
||||
res.close()
|
||||
return
|
||||
except Exception:
|
||||
logger.exception('cosyvoice tts error')
|
||||
|
||||
def stream_tts(self, audio_stream: Iterator[bytes], msg: tuple[str, dict]):
|
||||
text, textevent = msg
|
||||
first = True
|
||||
for chunk in audio_stream:
|
||||
if not chunk:
|
||||
continue
|
||||
pcm24 = np.frombuffer(chunk, dtype=np.int16).astype(np.float32) / 32767.0
|
||||
stream = resampy.resample(pcm24, sr_orig=24000, sr_new=self.sample_rate)
|
||||
streamlen = stream.shape[0]
|
||||
idx = 0
|
||||
while streamlen >= self.chunk:
|
||||
eventpoint = {}
|
||||
if first:
|
||||
eventpoint = {'status': 'start', 'text': text}
|
||||
first = False
|
||||
eventpoint.update(**textevent)
|
||||
self.parent.put_audio_frame(stream[idx:idx + self.chunk], eventpoint)
|
||||
streamlen -= self.chunk
|
||||
idx += self.chunk
|
||||
eventpoint = {'status': 'end', 'text': text}
|
||||
eventpoint.update(**textevent)
|
||||
self.parent.put_audio_frame(np.zeros(self.chunk, np.float32), eventpoint)
|
||||
@@ -1,13 +0,0 @@
|
||||
diff --git a/avatars/base_avatar.py b/avatars/base_avatar.py
|
||||
index 28c826a..a9744e6 100644
|
||||
--- a/avatars/base_avatar.py
|
||||
+++ b/avatars/base_avatar.py
|
||||
@@ -90,7 +90,7 @@ class BaseAvatar:
|
||||
'edgetts': 'tts.edge',
|
||||
'gpt-sovits': 'tts.sovits',
|
||||
'xtts': 'tts.xtts',
|
||||
- # 'cosyvoice': 'tts.cosyvoice',
|
||||
+ 'cosyvoice': 'tts.cosyvoice',
|
||||
# 'fishtts': 'tts.fish',
|
||||
'tencent': 'tts.tencent',
|
||||
'doubao': 'tts.doubao',
|
||||
@@ -1,375 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
"""HEL-715 end-to-end verification client for LiveTalking(MuseTalk) @ 127.0.0.1:8010.
|
||||
|
||||
Connects as a WebRTC receiver (same path a browser takes), drives the avatar via
|
||||
/human + /interrupt_talk, records the received A/V to mp4 and logs timing stats.
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
import aiohttp
|
||||
import av
|
||||
import numpy as np
|
||||
from aiortc import RTCPeerConnection, RTCSessionDescription
|
||||
|
||||
BASE = os.environ.get("LT_BASE", "http://127.0.0.1:8010")
|
||||
OUT = os.environ.get("E2E_OUT", "/opt/hel715-verify/e2e")
|
||||
os.makedirs(OUT, exist_ok=True)
|
||||
|
||||
S1 = "在呢。你回来啦,今天在外面累不累?"
|
||||
S2 = "还有,我跟你说,今天下午我在窗边看了好久的天空,云跑得特别快,有一朵特别像小兔子,我还给它拍了张照片,等下拿给你看。"
|
||||
S3 = "抱歉,你刚想说什么?我在听。"
|
||||
S4 = "嗯,我在。"
|
||||
|
||||
# approx mouth region (lower third of face bbox 218,203,324,333 from avatar prep)
|
||||
MOUTH = (222, 268, 320, 331)
|
||||
|
||||
log_lines = []
|
||||
|
||||
|
||||
def log(msg):
|
||||
line = f"[{time.strftime('%H:%M:%S')}] {msg}"
|
||||
print(line, flush=True)
|
||||
log_lines.append(line)
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self, path, w, h):
|
||||
self.container = av.open(path, "w")
|
||||
self.vstream = self.container.add_stream("libx264", rate=25)
|
||||
self.vstream.width = w
|
||||
self.vstream.height = h
|
||||
self.vstream.pix_fmt = "yuv420p"
|
||||
self.vstream.options = {"crf": "20", "preset": "veryfast"}
|
||||
self.astream = self.container.add_stream("aac", rate=48000)
|
||||
self.astream.layout = "stereo"
|
||||
|
||||
def write_video(self, img_bgr):
|
||||
frame = av.VideoFrame.from_ndarray(img_bgr, format="bgr24")
|
||||
for p in self.vstream.encode(frame):
|
||||
self.container.mux(p)
|
||||
|
||||
def write_audio(self, pcm_i16, sr, ch):
|
||||
arr = pcm_i16.astype(np.int16)
|
||||
if ch == 1:
|
||||
arr = np.repeat(arr[:, None], 2, axis=1).reshape(-1)
|
||||
elif ch == 2:
|
||||
arr = arr.reshape(-1)
|
||||
else:
|
||||
arr = arr[: len(arr) // ch * ch].reshape(-1, ch)[:, :2].reshape(-1)
|
||||
if sr != 48000:
|
||||
n = len(arr) // 2
|
||||
idx = (np.arange(n) * (sr / 48000.0)).astype(int)
|
||||
idx = idx[idx < len(arr) // 2]
|
||||
arr = arr.reshape(-1, 2)[idx].reshape(-1)
|
||||
frame = av.AudioFrame.from_ndarray(
|
||||
np.ascontiguousarray(arr.reshape(1, -1)), format="s16", layout="stereo"
|
||||
)
|
||||
frame.sample_rate = 48000
|
||||
for p in self.astream.encode(frame):
|
||||
self.container.mux(p)
|
||||
|
||||
def close(self):
|
||||
for p in self.vstream.encode():
|
||||
self.container.mux(p)
|
||||
for p in self.astream.encode():
|
||||
self.container.mux(p)
|
||||
self.container.close()
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self):
|
||||
self.events = {"video": [], "audio": [], "speak": [], "actions": []}
|
||||
self.imgs_sample = []
|
||||
self.sid = None
|
||||
self.http = None
|
||||
|
||||
async def post(self, path, payload):
|
||||
t0 = time.perf_counter()
|
||||
async with self.http.post(BASE + path, json=payload) as r:
|
||||
body = await r.json()
|
||||
return time.perf_counter() - t0, body
|
||||
|
||||
async def poll_speaking(self, stop_evt):
|
||||
while not stop_evt.is_set():
|
||||
try:
|
||||
dt, body = await self.post("/is_speaking", {"sessionid": self.sid})
|
||||
self.events["speak"].append(
|
||||
[round(time.perf_counter(), 3), body.get("data", False)]
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
await asyncio.sleep(0.25)
|
||||
|
||||
|
||||
def mouth_motion(prev, cur):
|
||||
if prev is None or cur is None:
|
||||
return 0.0
|
||||
x0, y0, x1, y1 = MOUTH
|
||||
d = np.abs(
|
||||
cur[y0:y1, x0:x1].astype(np.int16) - prev[y0:y1, x0:x1].astype(np.int16)
|
||||
).mean()
|
||||
return float(d)
|
||||
|
||||
|
||||
async def main():
|
||||
stats = {"sentences": {}, "meta": {}}
|
||||
rec = None
|
||||
pc = RTCPeerConnection()
|
||||
sess = Session()
|
||||
t_start = time.perf_counter()
|
||||
|
||||
done = asyncio.Event()
|
||||
|
||||
@pc.on("track")
|
||||
def on_track(track):
|
||||
log(f"track: {track.kind}")
|
||||
if track.kind == "video":
|
||||
asyncio.ensure_future(video_loop(track))
|
||||
else:
|
||||
asyncio.ensure_future(audio_loop(track))
|
||||
|
||||
async def video_loop(track):
|
||||
nonlocal rec
|
||||
prev = None
|
||||
while True:
|
||||
try:
|
||||
frame = await track.recv()
|
||||
except Exception:
|
||||
break
|
||||
img = frame.to_ndarray(format="bgr24")
|
||||
if rec is None:
|
||||
rec = Recorder(os.path.join(OUT, "e2e-session.mp4"), img.shape[1], img.shape[0])
|
||||
log(f"recorder ready {img.shape[1]}x{img.shape[0]}")
|
||||
t = time.perf_counter()
|
||||
m = mouth_motion(prev, img)
|
||||
sess.events["video"].append([round(t, 3), round(m, 2)])
|
||||
prev = img
|
||||
rec.write_video(img)
|
||||
if len(sess.imgs_sample) < 400:
|
||||
sess.imgs_sample.append((round(t - t_start, 2), img))
|
||||
|
||||
async def audio_loop(track):
|
||||
while True:
|
||||
try:
|
||||
frame = await track.recv()
|
||||
except Exception:
|
||||
break
|
||||
t = time.perf_counter()
|
||||
arr = frame.to_ndarray()
|
||||
if arr.dtype != np.int16:
|
||||
if arr.dtype in (np.float32, np.float64):
|
||||
arr = (np.clip(arr, -1, 1) * 32767).astype(np.int16)
|
||||
else:
|
||||
arr = arr.astype(np.int16)
|
||||
ch = len(frame.layout.channels)
|
||||
if arr.ndim == 2 and arr.shape[0] == 1:
|
||||
pcm = arr.reshape(-1) # packed s16
|
||||
elif arr.ndim == 2 and arr.shape[0] == ch:
|
||||
pcm = arr.T.reshape(-1) # planar
|
||||
else:
|
||||
pcm = arr.reshape(-1)
|
||||
ch = 1
|
||||
rms = float(np.sqrt((pcm.astype(np.float64) ** 2).mean()) / 32767.0)
|
||||
sess.events["audio"].append([round(t, 3), round(rms, 5)])
|
||||
if rec is not None:
|
||||
rec.write_audio(pcm, frame.sample_rate, ch)
|
||||
|
||||
async with aiohttp.ClientSession() as http:
|
||||
sess.http = http
|
||||
pc.addTransceiver("video", direction="recvonly")
|
||||
pc.addTransceiver("audio", direction="recvonly")
|
||||
offer = await pc.createOffer()
|
||||
await pc.setLocalDescription(offer)
|
||||
while pc.iceGatheringState != "complete":
|
||||
await asyncio.sleep(0.1)
|
||||
t0 = time.perf_counter()
|
||||
async with http.post(
|
||||
BASE + "/offer", json={"sdp": pc.localDescription.sdp, "type": "offer"}
|
||||
) as r:
|
||||
answer = await r.json()
|
||||
log(f"offer round-trip {time.perf_counter()-t0:.3f}s sessionid={answer.get('sessionid')}")
|
||||
sess.sid = answer["sessionid"]
|
||||
await pc.setRemoteDescription(
|
||||
RTCSessionDescription(sdp=answer["sdp"], type=answer["type"])
|
||||
)
|
||||
stats["meta"]["sessionid"] = sess.sid
|
||||
|
||||
stop_evt = asyncio.Event()
|
||||
poller = asyncio.ensure_future(sess.poll_speaking(stop_evt))
|
||||
|
||||
def mark(name):
|
||||
t = time.perf_counter()
|
||||
sess.events["actions"].append([round(t, 3), name])
|
||||
log(f"ACTION {name}")
|
||||
return t
|
||||
|
||||
# wait for first video frame / idle
|
||||
for _ in range(200):
|
||||
if sess.events["video"]:
|
||||
break
|
||||
await asyncio.sleep(0.1)
|
||||
first_v = sess.events["video"][0][0] if sess.events["video"] else -1
|
||||
first_a = sess.events["audio"][0][0] if sess.events["audio"] else -1
|
||||
stats["meta"]["first_video_s"] = round(first_v - t_start, 3)
|
||||
stats["meta"]["first_audio_s"] = round(first_a - t_start, 3)
|
||||
log(f"first video {stats['meta']['first_video_s']}s audio {stats['meta']['first_audio_s']}s")
|
||||
await asyncio.sleep(3.0) # idle baseline
|
||||
|
||||
async def speak(name, text, wait_end=True, interrupt_after=None, next_line=None):
|
||||
t_send = mark(f"send:{name}")
|
||||
dt, body = await sess.post(
|
||||
"/human", {"sessionid": sess.sid, "type": "echo", "text": text}
|
||||
)
|
||||
log(f"/human {name} ack {dt:.3f}s {body}")
|
||||
t_interrupt = None
|
||||
if interrupt_after is not None:
|
||||
await asyncio.sleep(interrupt_after)
|
||||
t_interrupt = mark(f"interrupt:{name}")
|
||||
dt2, b2 = await sess.post("/interrupt_talk", {"sessionid": sess.sid})
|
||||
log(f"/interrupt_talk ack {dt2:.3f}s {b2}")
|
||||
if wait_end:
|
||||
# wait until speaking False and audio silent
|
||||
silent = 0.0
|
||||
last_speak_true = time.perf_counter()
|
||||
while True:
|
||||
await asyncio.sleep(0.1)
|
||||
now = time.perf_counter()
|
||||
sp = sess.events["speak"][-1][1] if sess.events["speak"] else None
|
||||
if sp:
|
||||
last_speak_true = now
|
||||
recent = [a for a in sess.events["audio"] if a[0] > now - 0.6]
|
||||
loud = any(a[1] > 0.02 for a in recent)
|
||||
if not sp and not loud:
|
||||
silent = now - last_speak_true
|
||||
if silent > 1.2:
|
||||
break
|
||||
else:
|
||||
last_speak_true = max(last_speak_true, now)
|
||||
return t_send, t_interrupt
|
||||
|
||||
# S1 short
|
||||
s, _ = await speak("S1", S1)
|
||||
stats["sentences"]["S1"] = {"text": S1, "sent_at": round(s - t_start, 2)}
|
||||
await asyncio.sleep(1.5)
|
||||
# S2 long with interrupt 3s after send
|
||||
s2, ti = await speak("S2", S2, wait_end=True, interrupt_after=3.0)
|
||||
stats["sentences"]["S2"] = {
|
||||
"text": S2, "sent_at": round(s2 - t_start, 2),
|
||||
"interrupt_at": round(ti - t_start, 2),
|
||||
}
|
||||
await asyncio.sleep(2.0)
|
||||
# S3 re-open after interrupt
|
||||
s3, _ = await speak("S3", S3)
|
||||
stats["sentences"]["S3"] = {"text": S3, "sent_at": round(s3 - t_start, 2)}
|
||||
await asyncio.sleep(3.0) # pause test
|
||||
s4, _ = await speak("S4", S4)
|
||||
stats["sentences"]["S4"] = {"text": S4, "sent_at": round(s4 - t_start, 2)}
|
||||
mark("end")
|
||||
await asyncio.sleep(2.0)
|
||||
stop_evt.set()
|
||||
poller.cancel()
|
||||
await pc.close()
|
||||
|
||||
if rec:
|
||||
rec.close()
|
||||
log("recording closed")
|
||||
|
||||
# analysis
|
||||
def rel(ts):
|
||||
return round(ts - t_start, 3)
|
||||
|
||||
ev_a = sess.events["audio"]
|
||||
ev_v = sess.events["video"]
|
||||
for name, info in stats["sentences"].items():
|
||||
sent = info.get("sent_at", 0) + t_start
|
||||
aud = [a for a in ev_a if a[0] >= sent and a[1] > 0.02]
|
||||
info["first_audible_rel_s"] = rel(aud[0][0]) if aud else None
|
||||
if aud:
|
||||
last = sent
|
||||
for a in aud:
|
||||
if a[0] - last < 1.5:
|
||||
last = a[0]
|
||||
info["last_audible_rel_s"] = rel(last)
|
||||
if "interrupt_at" in info:
|
||||
it = info["interrupt_at"] + t_start
|
||||
after = [a for a in ev_a if a[0] >= it and a[1] > 0.02]
|
||||
info["audible_after_interrupt_s"] = rel(after[0][0]) if after else None
|
||||
tail = [a for a in ev_a if a[0] >= it]
|
||||
tl = [a for a in tail if a[1] > 0.02]
|
||||
info["last_audible_after_interrupt_rel_s"] = rel(tl[-1][0]) if tl else None
|
||||
|
||||
# video motion around each sentence
|
||||
for name, info in stats["sentences"].items():
|
||||
sent = info.get("sent_at", 0) + t_start
|
||||
window = [v for v in ev_v if sent - 1.0 <= v[0] <= sent + 12]
|
||||
base = [v for v in ev_v if v[0] < sent - 1.0][-60:]
|
||||
if window and base:
|
||||
b = np.median([v[1] for v in base])
|
||||
info["mouth_motion_idle"] = round(float(b), 2)
|
||||
peak = max(v[1] for v in window)
|
||||
info["mouth_motion_peak"] = round(float(peak), 2)
|
||||
|
||||
# fps over speech windows
|
||||
if ev_v:
|
||||
dur = ev_v[-1][0] - ev_v[0][0]
|
||||
stats["meta"]["video_frames"] = len(ev_v)
|
||||
stats["meta"]["video_span_s"] = round(dur, 2)
|
||||
stats["meta"]["avg_recv_fps"] = round(len(ev_v) / dur, 2) if dur > 0 else None
|
||||
if ev_a:
|
||||
dur = ev_a[-1][0] - ev_a[0][0]
|
||||
stats["meta"]["audio_frames"] = len(ev_a)
|
||||
stats["meta"]["audio_span_s"] = round(dur, 2)
|
||||
|
||||
stats["meta"]["wall_total_s"] = round(time.perf_counter() - t_start, 2)
|
||||
|
||||
with open(os.path.join(OUT, "stats.json"), "w") as f:
|
||||
json.dump(stats, f, ensure_ascii=False, indent=1)
|
||||
with open(os.path.join(OUT, "events.json"), "w") as f:
|
||||
json.dump(
|
||||
{
|
||||
"t_start": t_start,
|
||||
"video": sess.events["video"],
|
||||
"audio": sess.events["audio"],
|
||||
"speak": sess.events["speak"],
|
||||
"actions": sess.events["actions"],
|
||||
},
|
||||
f,
|
||||
)
|
||||
with open(os.path.join(OUT, "run.log"), "w") as f:
|
||||
f.write("\n".join(log_lines))
|
||||
|
||||
# contact sheet: pick frames around S1 onset, S2 mid, interrupt, S3
|
||||
try:
|
||||
from PIL import Image
|
||||
|
||||
picks = []
|
||||
s1 = stats["sentences"]["S1"].get("first_audible_rel_s") or 5
|
||||
s2i = stats["sentences"]["S2"].get("interrupt_at")
|
||||
picks += [max(0, s1 - 1.5), s1 + 0.5, s1 + 2.5]
|
||||
if s2i:
|
||||
picks += [s2i - 1.0, s2i + 0.3, s2i + 1.5]
|
||||
picks += [stats["sentences"]["S3"]["sent_at"] + 2.0]
|
||||
tiles = []
|
||||
for p in picks:
|
||||
cand = min(sess.imgs_sample, key=lambda x: abs(x[0] - p))
|
||||
im = Image.fromarray(cand[1][:, :, ::-1]).resize((276, 276))
|
||||
tiles.append((cand[0], im))
|
||||
sheet = Image.new("RGB", (276 * len(tiles), 276 + 18), "black")
|
||||
from PIL import ImageDraw
|
||||
|
||||
d = ImageDraw.Draw(sheet)
|
||||
for i, (ts, im) in enumerate(tiles):
|
||||
sheet.paste(im, (i * 276, 18))
|
||||
d.text((i * 276 + 4, 2), f"t={ts:.1f}s", fill="white")
|
||||
sheet.save(os.path.join(OUT, "contact-sheet.png"))
|
||||
except Exception as e:
|
||||
log(f"contact sheet failed: {e}")
|
||||
|
||||
log("DONE")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,129 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>HEL-715 日常→交谈→打断→日常 链路演示</title>
|
||||
<style>
|
||||
html,body{margin:0;height:100%;background:#000;color:#eee;font-family:"Noto Sans CJK SC",sans-serif;overflow:hidden}
|
||||
#stage{position:relative;height:100%;display:flex;align-items:center;justify-content:center}
|
||||
video{position:absolute;height:92%;top:2%;left:50%;transform:translateX(-50%);background:#000}
|
||||
#daily{display:block}
|
||||
#talk{display:none}
|
||||
#hud{position:absolute;left:0;right:0;bottom:0;padding:10px 14px;background:rgba(0,0,0,.72);font-size:15px;line-height:1.5}
|
||||
#phase{font-size:19px;font-weight:700;color:#ffd166}
|
||||
#timer{position:absolute;top:8px;right:14px;font-family:monospace;font-size:18px;background:rgba(0,0,0,.6);padding:2px 8px;border-radius:4px}
|
||||
#log{position:absolute;top:8px;left:14px;font-family:monospace;font-size:12px;background:rgba(0,0,0,.6);padding:6px 10px;border-radius:4px;max-height:46%;overflow:hidden;white-space:pre}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="stage">
|
||||
<video id="daily" autoplay loop muted playsinline>
|
||||
<source src="/hel715-daily.webm" type="video/webm"/>
|
||||
<source src="/hel715-daily.mp4" type="video/mp4"/>
|
||||
</video>
|
||||
<video id="talk" autoplay playsinline></video>
|
||||
<div id="timer">0.0s</div>
|
||||
<div id="log"></div>
|
||||
<div id="hud"><div id="phase">初始化…</div><div id="note">成品日常视频循环播放 · 交谈流后台预连接</div></div>
|
||||
</div>
|
||||
<script>
|
||||
const BASE = location.origin;
|
||||
const $ = id => document.getElementById(id);
|
||||
const t0 = performance.now();
|
||||
const logs = [];
|
||||
function now(){ return (performance.now()-t0)/1000; }
|
||||
function log(msg){
|
||||
const line = `${now().toFixed(2)}s ${msg}`;
|
||||
logs.push(line);
|
||||
$('log').textContent = logs.slice(-14).join('\n');
|
||||
console.log('[chain]', line);
|
||||
}
|
||||
function phase(p, note){ $('phase').textContent = p; $('note').textContent = note||''; }
|
||||
setInterval(()=>{ $('timer').textContent = now().toFixed(1)+'s'; }, 100);
|
||||
|
||||
/* ---------- WebRTC pre-connect (same path as LiveTalking client.js) ---------- */
|
||||
let pc = null, sessionid = null, connected = false, firstTalkFrameAt = null;
|
||||
async function connectTalk(){
|
||||
pc = new RTCPeerConnection({sdpSemantics:'unified-plan'});
|
||||
pc.addTransceiver('video',{direction:'recvonly'});
|
||||
pc.addTransceiver('audio',{direction:'recvonly'});
|
||||
pc.addEventListener('track', ev=>{
|
||||
if(ev.track.kind==='video'){
|
||||
firstTalkFrameAt = firstTalkFrameAt || now();
|
||||
$('talk').srcObject = ev.streams[0];
|
||||
log('交谈视频轨到达');
|
||||
} else {
|
||||
$('talk').srcObject = $('talk').srcObject || ev.streams[0];
|
||||
}
|
||||
});
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
while(pc.iceGatheringState!=='complete') await new Promise(r=>setTimeout(r,50));
|
||||
const res = await fetch(BASE+'/offer',{method:'POST',headers:{'Content-Type':'application/json'},
|
||||
body:JSON.stringify({sdp:pc.localDescription.sdp,type:pc.localDescription.type})});
|
||||
const answer = await res.json();
|
||||
sessionid = answer.sessionid;
|
||||
await pc.setRemoteDescription(answer);
|
||||
log(`交谈流已连接 session=${sessionid.slice(0,8)}(预连接,用户尚未进入)`);
|
||||
connected = true;
|
||||
}
|
||||
async function say(text){
|
||||
const t=now();
|
||||
const r = await fetch(BASE+'/human',{method:'POST',headers:{'Content-Type':'application/json'},
|
||||
body:JSON.stringify({sessionid, type:'echo', text})});
|
||||
log(`发送对话文本(${text.slice(0,10)}…)HTTP ${(now()-t)*1000|0}ms`);
|
||||
}
|
||||
async function interrupt(){
|
||||
const t=now();
|
||||
await fetch(BASE+'/interrupt_talk',{method:'POST',headers:{'Content-Type':'application/json'},
|
||||
body:JSON.stringify({sessionid})});
|
||||
log(`打断信号 interrupt_talk HTTP ${(now()-t)*1000|0}ms`);
|
||||
}
|
||||
/* ---------- view switching ---------- */
|
||||
function showDaily(){ $('daily').style.display='block'; $('talk').style.display='none'; }
|
||||
function showTalk(){ $('daily').style.display='none'; $('talk').style.display='block'; }
|
||||
window.__chainLog = logs;
|
||||
|
||||
/* ---------- auto timeline ---------- */
|
||||
const auto = new URLSearchParams(location.search).has('auto');
|
||||
if(auto){
|
||||
(async ()=>{
|
||||
await connectTalk().catch(e=>log('连接失败: '+e));
|
||||
phase('① 日常播放','成品日常原片(HEL-712 同人物素材)循环播放');
|
||||
await sleep(7000);
|
||||
const sw = now(); showTalk();
|
||||
phase('② 进入交谈','同一人物·同一房间·同一套衣服 · 实时口型(MuseTalk)');
|
||||
log(`切换到交谈视图(预连接流,切换耗时 ${(now()-sw)*1000|0}ms)`);
|
||||
await say('你回来啦!我在窗边等你呢,快过来坐,我跟你说今天下午的云。');
|
||||
await sleep(6500); // speech ongoing (~1s in voice)
|
||||
phase('③ 用户开口→打断','interrupt_talk 信号注入 · 不等她说完、不等日常片尾');
|
||||
const ti = now();
|
||||
await interrupt();
|
||||
showDaily(); // 回日常/倾听,不等待
|
||||
log(`打断后立即切回日常视图(+${((now()-ti)*1000)|0}ms)`);
|
||||
await sleep(4000);
|
||||
showTalk();
|
||||
phase('④ 再次交谈','打断后可立即重新开口(清空待播队列后)');
|
||||
log('再次切换到交谈视图');
|
||||
await say('嗯,你说,我在听。');
|
||||
await sleep(8000);
|
||||
showDaily();
|
||||
phase('⑤ 回到日常','交谈结束 → 成品日常继续');
|
||||
log('回到日常播放');
|
||||
await sleep(4000);
|
||||
phase('演示结束','HEL-715 链路取样:日常→交谈→打断→回日常→再交谈');
|
||||
log('AUTO-DEMO-END');
|
||||
})();
|
||||
} else {
|
||||
phase('手动模式','D=日常 T=进入交谈并发送 I=打断并回日常');
|
||||
addEventListener('keydown', async ev=>{
|
||||
if(ev.key==='d'){ showDaily(); log('手动: 日常'); }
|
||||
if(ev.key==='t'){ showTalk(); log('手动: 交谈'); if(connected) await say('你回来啦!我在窗边等你呢。'); }
|
||||
if(ev.key==='i'){ await interrupt(); showDaily(); log('手动: 打断→日常'); }
|
||||
});
|
||||
connectTalk().catch(e=>log('连接失败: '+e));
|
||||
}
|
||||
function sleep(ms){ return new Promise(r=>setTimeout(r,ms)); }
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,124 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
"""LLM-chained conversation sample: .11 LLM -> .12 CosyVoice -> MuseTalk avatar.
|
||||
Same receiver architecture as e2e_client.py (proven to capture A/V)."""
|
||||
import asyncio, json, os, time
|
||||
import aiohttp, av, numpy as np
|
||||
from aiortc import RTCPeerConnection, RTCSessionDescription
|
||||
|
||||
BASE = "http://127.0.0.1:8010"
|
||||
LLM = "http://192.168.200.11:8731/v1/chat/completions"
|
||||
OUT = "/opt/hel715-verify/e2e"
|
||||
os.makedirs(OUT, exist_ok=True)
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self, path, w, h):
|
||||
self.container = av.open(path, "w")
|
||||
self.v = self.container.add_stream("libx264", rate=25)
|
||||
self.v.width, self.v.height, self.v.pix_fmt = w, h, "yuv420p"
|
||||
self.v.options = {"crf": "20", "preset": "veryfast"}
|
||||
self.a = self.container.add_stream("aac", rate=48000)
|
||||
self.a.layout = "stereo"
|
||||
|
||||
def video(self, img):
|
||||
for p in self.v.encode(av.VideoFrame.from_ndarray(img, format="bgr24")):
|
||||
self.container.mux(p)
|
||||
|
||||
def audio(self, frame):
|
||||
arr = frame.to_ndarray()
|
||||
if arr.dtype != np.int16:
|
||||
arr = (np.clip(arr, -1, 1) * 32767).astype(np.int16)
|
||||
ch = len(frame.layout.channels)
|
||||
if arr.ndim == 2 and arr.shape[0] == 1:
|
||||
pcm = arr.reshape(-1)
|
||||
elif arr.ndim == 2 and arr.shape[0] == ch:
|
||||
pcm = arr.T.reshape(-1)
|
||||
else:
|
||||
pcm = arr.reshape(-1)
|
||||
fr = av.AudioFrame.from_ndarray(np.ascontiguousarray(pcm.reshape(1, -1)), format="s16", layout="stereo")
|
||||
fr.sample_rate = 48000
|
||||
for p in self.a.encode(fr):
|
||||
self.container.mux(p)
|
||||
|
||||
def close(self):
|
||||
for p in self.v.encode(): self.container.mux(p)
|
||||
for p in self.a.encode(): self.container.mux(p)
|
||||
self.container.close()
|
||||
|
||||
|
||||
async def main():
|
||||
t0 = time.perf_counter()
|
||||
result = {}
|
||||
async with aiohttp.ClientSession() as http:
|
||||
async with http.post(LLM, json={
|
||||
"model": "halogen-qwen3.8-flash-next",
|
||||
"messages": [{"role": "user", "content": "你是住在高层公寓卧室里的伴侣晚晚,用一两句自然中文回应:我回来了,今天好累啊。"}],
|
||||
"max_tokens": 80,
|
||||
}) as r:
|
||||
body = await r.json()
|
||||
reply = body["choices"][0]["message"]["content"]
|
||||
result["llm_model"] = body["model"]
|
||||
result["llm_reply"] = reply
|
||||
result["llm_latency_s"] = round(time.perf_counter() - t0, 2)
|
||||
print(f"[{result['llm_latency_s']}s] LLM: {reply}", flush=True)
|
||||
|
||||
pc = RTCPeerConnection()
|
||||
rec = {"r": None}
|
||||
audio_marks = []
|
||||
|
||||
@pc.on("track")
|
||||
def on_track(track):
|
||||
async def drain():
|
||||
while True:
|
||||
try:
|
||||
f = await track.recv()
|
||||
except Exception:
|
||||
break
|
||||
t = time.perf_counter()
|
||||
if track.kind == "video":
|
||||
img = f.to_ndarray(format="bgr24")
|
||||
if rec["r"] is None:
|
||||
rec["r"] = Recorder(f"{OUT}/llm-chain.mp4", img.shape[1], img.shape[0])
|
||||
rec["r"].video(img)
|
||||
else:
|
||||
arr = f.to_ndarray()
|
||||
if arr.dtype != np.int16:
|
||||
arr = (np.clip(arr, -1, 1) * 32767).astype(np.int16)
|
||||
rms = float(np.sqrt((arr.astype(np.float64) ** 2).mean()) / 32767)
|
||||
audio_marks.append([round(t - t0, 3), round(rms, 5)])
|
||||
if rec["r"] is not None:
|
||||
rec["r"].audio(f)
|
||||
asyncio.ensure_future(drain())
|
||||
|
||||
pc.addTransceiver("video", direction="recvonly")
|
||||
pc.addTransceiver("audio", direction="recvonly")
|
||||
offer = await pc.createOffer()
|
||||
await pc.setLocalDescription(offer)
|
||||
while pc.iceGatheringState != "complete":
|
||||
await asyncio.sleep(0.1)
|
||||
async with http.post(BASE + "/offer", json={"sdp": pc.localDescription.sdp, "type": "offer"}) as r:
|
||||
answer = await r.json()
|
||||
await pc.setRemoteDescription(RTCSessionDescription(sdp=answer["sdp"], type=answer["type"]))
|
||||
sid = answer["sessionid"]
|
||||
await asyncio.sleep(3)
|
||||
t_send = time.perf_counter() - t0
|
||||
async with http.post(BASE + "/human", json={"sessionid": sid, "type": "echo", "text": reply}) as r:
|
||||
await r.json()
|
||||
print(f"[{t_send:.2f}s] sent to avatar", flush=True)
|
||||
await asyncio.sleep(15)
|
||||
await pc.close()
|
||||
|
||||
if rec["r"]:
|
||||
rec["r"].close()
|
||||
loud = [m for _, m in audio_marks if _ and m > 0.02]
|
||||
first_loud = next((t for t, m in audio_marks if m > 0.02), None)
|
||||
result["sent_at_s"] = round(t_send, 2)
|
||||
result["first_audible_s"] = round(first_loud, 2) if first_loud else None
|
||||
result["send_to_audible_s"] = round(first_loud - t_send, 2) if first_loud else None
|
||||
with open(f"{OUT}/llm-chain-stats.json", "w") as f:
|
||||
json.dump(result, f, ensure_ascii=False, indent=1)
|
||||
print(json.dumps(result, ensure_ascii=False), flush=True)
|
||||
print("DONE", flush=True)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -1,59 +0,0 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
os.environ["LD_LIBRARY_PATH"] = (
|
||||
"/tmp/multica-task-4070005680/debs/root/usr/lib/x86_64-linux-gnu"
|
||||
+ (":" + os.environ["LD_LIBRARY_PATH"] if os.environ.get("LD_LIBRARY_PATH") else "")
|
||||
)
|
||||
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
OUT = pathlib.Path("/tmp/multica-task-4070005680/opencode/chain")
|
||||
OUT.mkdir(exist_ok=True)
|
||||
URL = "http://192.168.200.12:8010/hel715-chain.html?auto=1"
|
||||
|
||||
|
||||
async def main():
|
||||
async with async_playwright() as p:
|
||||
browser = await p.chromium.launch(
|
||||
args=["--autoplay-policy=no-user-gesture-required", "--no-sandbox"]
|
||||
)
|
||||
ctx = await browser.new_context(
|
||||
viewport={"width": 1280, "height": 800},
|
||||
record_video_dir=str(OUT),
|
||||
record_video_size={"width": 1280, "height": 800},
|
||||
)
|
||||
page = await ctx.new_page()
|
||||
errors = []
|
||||
page.on("pageerror", lambda e: errors.append(str(e)))
|
||||
await page.goto(URL, wait_until="domcontentloaded")
|
||||
# wait for demo end marker (auto demo ~37s)
|
||||
try:
|
||||
await page.wait_for_function(
|
||||
"() => window.__chainLog && window.__chainLog.some(l => l.includes('AUTO-DEMO-END'))",
|
||||
timeout=60000,
|
||||
)
|
||||
except Exception as e:
|
||||
errors.append(f"wait_for_function: {e}")
|
||||
await asyncio.sleep(1.5)
|
||||
logs = await page.evaluate("() => window.__chainLog")
|
||||
metrics = {
|
||||
"url": URL,
|
||||
"chain_log": logs,
|
||||
"pageerrors": errors,
|
||||
}
|
||||
(OUT / "chain-browser-log.json").write_text(
|
||||
json.dumps(metrics, ensure_ascii=False, indent=1)
|
||||
)
|
||||
video = page.video
|
||||
await ctx.close()
|
||||
path = await video.path()
|
||||
await browser.close()
|
||||
print("video:", path)
|
||||
print("errors:", errors)
|
||||
print(json.dumps(logs, ensure_ascii=False, indent=1))
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1 @@
|
||||
qa-artifacts/
|
||||
@@ -0,0 +1,64 @@
|
||||
# HEL-700 施工与自测记录
|
||||
|
||||
基准 `1e9b08a`;交付分支 `hel-700-rain-room`。实现范围仅 `web/` 与根 `.gitignore`。未部署,未改 Python 核心、配置、测试或远端服务。
|
||||
|
||||
## 实现
|
||||
|
||||
- `src/app/`:真实 DOM 导航、玻璃面板、消息、输入、片刻/命盘预览、设置,样式 token 统一在 `style.css`。系统减少动效、触屏、抽屉和矮屏各有适配;设置在桌面右侧、手机底部最多55dvh,避开脸部。
|
||||
- `src/scene/scene.ts`:Pixi 仅允许 WebGL 后端并校验类型,独立 Canvas2D、CSS、静态降级;雨幕、水痕高光、粒子、光轨及指针视差。原始带 UI 参考图没有放入网页。
|
||||
- `src/engine/audio.ts`:用户主动开启程序化雨声、音量、隐藏暂停及恢复。语音和附件只解释未接入,不采集设备。
|
||||
- `src/net/llm.ts`:同源探测、UTF-8 SSE 分包、2/8/40 秒超时、取消、保留流式内容、手动重试;空响应与截断均报错。模型内容作为文本节点渲染。
|
||||
- `src/demo/`:演示台词、片刻、场景文案和预设唯一来源;`state/contracts.ts` 镜像 Python State/Utterance 字段。
|
||||
- `vite.config.ts` 开发/preview 同源代理;`nginx.conf.example` 是交总工的生产模板,没有执行服务器配置。
|
||||
|
||||
## 命令与结果
|
||||
|
||||
从仓库根:
|
||||
|
||||
```sh
|
||||
npm ci --prefix web
|
||||
npm run typecheck --prefix web
|
||||
npm run build --prefix web
|
||||
python3 -m pytest -q
|
||||
CYBERGRIL_ARTIFACT_DIR="$PWD/qa-output" python3 web/tests/browser_check.py
|
||||
CYBERGRIL_ARTIFACT_DIR="$PWD/qa-output" python3 web/tests/network_check.py
|
||||
CYBERGRIL_ARTIFACT_DIR="$PWD/qa-output" python3 web/tests/lifecycle_check.py
|
||||
CYBERGRIL_ARTIFACT_DIR="$PWD/qa-output" python3 web/tests/production_check.py
|
||||
```
|
||||
|
||||
浏览器脚本使用宿主已有 Python Playwright,不增加 npm 依赖;需要宿主可用的浏览器图形运行库。每个脚本前台启动并最终关闭自己的 Vite 子进程。严格 TS/noUnused、构建、锁文件重装均通过;既有 Python 测试 **78 passed**。未新增独立 linter 依赖,另做 `git diff --check` 与调试残留检查。
|
||||
|
||||
浏览器:Linux 无头 Chromium 151.0.7922.34,桌面 1600×900、1280×800,平板 768×1024,手机视口 390×844 收起/展开/聚焦、844×390 横屏。截图均为实际页面,生产截图前缀 `production-`。
|
||||
|
||||
- 主交互回归 32 项通过:Enter/Shift+Enter、组合输入保护、500 字、流式期间编辑、取消/重试、来源标注、预览、静态/轻量、减少动效、语音说明、折叠恢复、XSS 文本显示等。
|
||||
- 生命周期回归 13 项通过:真实 `WEBGL_lose_context` 丢失;屏蔽 WebGL API 转独立 Canvas2D,屏蔽 Canvas2D 转 CSS;20 FPS 注入触发自动逐级到静态;雨声隐藏暂停/回来恢复;长消息内部滚动和返回底部;抽屉拖动/键盘;软键盘布局分支。
|
||||
- 网络故障 8 项通过:逐字节 UTF-8/CRLF 分包、网络错误、取消、空响应、坏 SSE、首包 8001ms 超时、整包 40000ms 超时、探测 2000ms 超时。
|
||||
- 真实内网模型经开发同源代理完成回复,单次完整回复约 1408ms;生产 preview `/v1/models` 返回 200。实时失败未转换成演示台词,已有部分输出不会自动重试。
|
||||
- 构建产物 smoke 7 项通过,无未处理页面异常。
|
||||
|
||||
## 包体与性能(实际采样,不扩大适用范围)
|
||||
|
||||
| 项目 | 结果 |
|
||||
| --- | --- |
|
||||
| 全部 JS gzip 合计(Python gzip 默认级别9) | 269538 字节;低于 300KB 目标 |
|
||||
| JS 原始合计 | 931348 字节;Pixi 主块约 904KB,Vite 大块提示仍存在 |
|
||||
| 素材合计(WebP + JPEG 回退) | 593708 字节;低于 1.5MB |
|
||||
| 首屏 WebP / JPEG 回退 | 267992 / 325716 字节 |
|
||||
| 4 秒帧采样 | 198 帧;帧间隔 P95 33.40ms;未观测到 >50ms long task |
|
||||
| 场景 rAF 回调 | 379 次,P95 0.740ms,最长 2.497ms |
|
||||
| 浏览器主线程任务 | 3052 次,最长 10.304ms;3 次 >8ms |
|
||||
|
||||
**8ms 全主线程任务目标尚未完全达到。** 超出任务来自浏览器 `ScheduledActionSendBeginMainFrame`(含合成/布局),不是只统计业务 JS。保留完整 `chromium-main-thread-trace.json`,不以无 long task 替代 8ms 证明。快速连续键击字符串完整保留。该采样使用本机软件图形后端,不能代表低端安卓。
|
||||
|
||||
## 限制与待审核项
|
||||
|
||||
- Firefox 尝试启动,但宿主缺 libgtk/libXcursor 等运行库;未完成 Firefox、Edge、Safari/iOS 或低端安卓真机测试。
|
||||
- 中文输入法通过浏览器 composition 事件测试保护逻辑;未使用桌面系统真实输入法人工验收。软键盘布局通过 visualViewport 压缩事件模拟,未做手机系统键盘真机测试。
|
||||
- 隐藏/恢复通过 document visibility 事件和隐藏值注入,验证场景、CSS 和实际 AudioContext 状态;没有冒充真实设备后台生命周期测试。
|
||||
- 真正 WebGL 上下文丢失有实测;低帧率/不可用 API 和坏 SSE 使用明确的故障注入,不声称真实硬件故障。
|
||||
- 运行时依赖审计 0 漏洞。授权的 Vite 5.4.21 / esbuild 开发依赖仍有 1 high + 1 moderate 通告(详见 npm audit 原始附件);没有擅自升级授权之外的大版本。开发与 preview 只绑定本机;生产只交静态 dist 与代理模板。后续升级需总管确认版本范围。
|
||||
- nginx 模板未经远端部署验证;未接 ASR、TTS、Python 性格/记忆/生活/排盘;人物与城市是一体素材,不宣称独立人物动画。
|
||||
|
||||
## 证据索引
|
||||
|
||||
`browser-report.json`、`network-report.json`、`lifecycle-report.json`、`production-report.json` 为原始结果;PNG 包含主场景、发送/取消/失败、预览、各档降级与所有适配截图;trace 为 Chromium DevTools timeline 数据。证据随本卡附件交付,不以本地路径代替交付。
|
||||
@@ -0,0 +1,34 @@
|
||||
# 雨夜相处演示
|
||||
|
||||
独立前端,对应 HEL-700,基准 `1e9b08a`。不连接 Python 性格、记忆、生活、排盘或语音引擎。
|
||||
|
||||
```sh
|
||||
cd web
|
||||
npm ci
|
||||
npm run dev
|
||||
npm run typecheck
|
||||
npm run build
|
||||
npm run preview
|
||||
```
|
||||
|
||||
开发与 preview 均提供同源 `/v1` 代理。生产由总工按 `nginx.conf.example` 合入已有站点配置,禁止把 Vite 开发服务作为生产服务。模板不选择端口、不修改服务器。模型配置集中在 `src/net/llm.ts` 的请求和探测中。
|
||||
|
||||
目录边界:`app/` 真实 DOM 与视觉 token;`scene/` 只读取独立场景时间与氛围;`engine/audio.ts` 管理用户主动开启的程序雨声;`net/llm.ts` 管理探测、SSE、超时与取消;`demo/` 是演示台词与片刻的唯一来源;`state/contracts.ts` 镜像 Python State、Utterance 字段并定义前端消息类型。没有共享场景/输入状态。
|
||||
|
||||
降级:全特效首先创建 Pixi WebGL,创建失败或上下文丢失切到独立 Canvas2D;Canvas 创建失败切 CSS 雨幕;连续 4 秒采样低于 30 FPS 逐级降低,CSS 仍低帧率则静态。轻量直接用 Canvas2D,静态或系统减少动效关闭环境与波形动画。用户手动选择档位可重新尝试。`body[data-renderer]` 标出实际渲染器,`#effects[data-running]` 标出运行状态,供审核检查。素材始终静态,只有雨幕与光膜位移。手机不读设备传感器。
|
||||
|
||||
2 秒模型探测失败进入明确标注的演示模式。实时请求首个内容片段 8 秒超时,整包 40 秒超时。错误、截断、取消保留已有内容,不自动重试;只有用户点击重试才重置该失败回复。已产生后续消息时要求重新发送,防止重试混入过期上下文。模型内容始终 `textContent` 渲染。演示消息不会进入实时模型上下文。不会把模型错误转换为角色台词。
|
||||
|
||||
雨声每次打开页面保持关闭;用户开启后,页面隐藏会暂停,返回按当次选择恢复。音量保存在 localStorage(存储不可用不影响使用)。设置中的预设仅修改场景灯光和雨密度,界面暖色 token 不变。
|
||||
|
||||
手机默认抽屉 30dvh,展开 55dvh;横屏、矮屏或软键盘压缩可视区时切成上下可滚动布局。手柄支持点击/键盘与上下拖动。语音和附件只显示说明,不触发设备授权。
|
||||
|
||||
浏览器回归用本机已有的 Python Playwright(不增加 npm 依赖):
|
||||
|
||||
```sh
|
||||
python3 web/tests/browser_check.py
|
||||
```
|
||||
|
||||
其余网络故障、生命周期与生产脚本见 `QA.md`,上述测试命令从仓库根执行。脚本启动并最终关闭本地 Vite 服务,输出到执行时指定的 `CYBERGRIL_ARTIFACT_DIR`(默认 `web/qa-artifacts`)。截图、采样和实际覆盖情况见任务卡交付附件。
|
||||
|
||||
Pixi API 按官方 Application / Graphics 文档实现:https://pixijs.com/8.x/guides/components/application ,https://pixijs.com/8.x/guides/components/scene-objects/graphics 。
|
||||
@@ -0,0 +1 @@
|
||||
<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"><meta name="theme-color" content="#16233c"><title>CyberGril · 与晚晚相处</title></head><body><div id="app"></div><script type="module" src="/src/main.ts"></script></body></html>
|
||||
@@ -0,0 +1,17 @@
|
||||
# Template only. Integrator selects the existing server and release root.
|
||||
# Insert these locations in the deployment owner's server block.
|
||||
location / {
|
||||
root /srv/cybergril/current;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
location /v1/ {
|
||||
proxy_pass http://192.168.200.11:8731;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $proxy_host;
|
||||
proxy_set_header Connection "";
|
||||
proxy_buffering off;
|
||||
proxy_cache off;
|
||||
proxy_read_timeout 45s;
|
||||
proxy_send_timeout 45s;
|
||||
add_header X-Accel-Buffering no always;
|
||||
}
|
||||
Generated
+1166
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "cybergril-rain-room",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --host 127.0.0.1",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"build": "tsc --noEmit && vite build",
|
||||
"preview": "vite preview --host 127.0.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"pixi.js": "^8.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "~5.9.3",
|
||||
"vite": "^5.4.0"
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 318 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 262 KiB |
@@ -0,0 +1,14 @@
|
||||
// Static application chrome only. All message/model content uses textContent.
|
||||
export const shell = `
|
||||
<div class="scene" aria-hidden="true"><picture><source srcset="/assets/rain-room.webp" type="image/webp"><img src="/assets/rain-room.jpg" alt=""></picture><div class="lamp"></div><div id="effects"></div><div class="css-rain"></div><div class="light-trail"></div><div class="dust"></div><div class="shade"></div></div>
|
||||
<header><div class="brand"><span class="moon">☾</span> CyberGril <span>晚晚</span></div><div class="clock">她的时间 <time id="time"></time><button id="settings-open" aria-label="设置">⚙</button></div></header>
|
||||
<p class="badge">演示版 · 性格与记忆引擎未接入</p>
|
||||
<nav aria-label="主导航"><button data-view="chat" aria-current="page">相处</button><button data-view="daily">她的日常 <small>预览</small></button><button data-view="fate">命盘 <small>预览</small></button></nav>
|
||||
<p id="caption" class="caption"></p>
|
||||
<button id="restore" class="glass" hidden>与晚晚说说话 ↗</button>
|
||||
<section class="panel glass" aria-label="晚晚对话"><button id="drawer" class="handle" aria-label="展开对话" aria-expanded="false"><span></span></button><div class="panel-head"><div><h1>晚晚</h1><p><i class="presence"></i> 在这里 <small>· 场景演示</small></p></div><button id="collapse" aria-label="收起对话">···</button></div>
|
||||
<div id="chat-view" class="view"><div id="messages" role="log" aria-label="对话记录" aria-live="off"><p class="date">今晚 · 雨夜相处</p></div><button id="bottom" hidden>↓ 回到底部</button><div class="wave-card"><button id="wave" aria-label="切换波形演示" aria-pressed="false">▷</button><span class="wave-bars" aria-hidden="true"></span><small>波形演示 · 无语音播放</small></div><button id="moment" class="moment"><span class="thumb" aria-hidden="true"></span><span><small>今天的片刻 · 演示</small><br><span id="moment-title"></span></span><span>›</span></button></div>
|
||||
<div id="preview-view" class="view" hidden><h2 id="preview-title"></h2><div id="preview-content"></div><small>轻量预览 · 非最终版</small></div>
|
||||
<div class="compose-area"><div class="mode-row"><span id="connection" role="status">正在探测模型…</span><button id="mode" disabled>正在检测…</button></div><form id="compose"><button type="button" id="attachment" aria-disabled="true" aria-label="附件未接入,查看说明"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="m8 13 6-6a3 3 0 0 1 4 4l-8 8a5 5 0 0 1-7-7l9-9"/></svg></button><textarea id="input" rows="1" maxlength="500" placeholder="和她说点什么…" aria-label="和她说点什么"></textarea><button type="button" id="voice" aria-disabled="true" aria-describedby="voice-note" aria-label="语音未接入,查看说明"><svg viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0 0 14 0M12 17v5m-4 0h8"/></svg></button><button id="send" type="submit" aria-label="发送消息">↑</button></form><div class="compose-meta"><span id="count">0 / 500</span><span>文字 / 语音</span><button id="cancel" hidden>取消回应</button></div><p id="voice-note">语音录入与 TTS 未接入;附件功能未接入。</p><p id="chat-status" role="status"></p></div></section>
|
||||
<dialog id="settings" class="glass"><div class="dialog-head"><h2>雨夜设置</h2><button id="settings-close" aria-label="关闭设置">×</button></div><p>氛围预设 · 未接真实情绪引擎</p><div class="choices" id="presets"></div><label>动效档位<select id="quality"><option value="full">全特效 · WebGL 优先</option><option value="lite">轻量 · Canvas2D 优先</option><option value="static">静态</option></select></label><small>系统减少动效时自动静态,触屏关闭视差。</small><label class="audio-label"><input id="rain-sound" type="checkbox"> 程序化雨声</label><label>雨声音量<input id="volume" type="range" min="0" max="100" value="25"></label><p id="audio-status" role="status">默认静音,仅主动开启后播放。</p></dialog>
|
||||
<div id="toast" class="glass" role="status" hidden></div>`;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
export const caption = ['窗外下雨了。', '她刚读完一本书的一页。'];
|
||||
export const momentTitle = '雨声 · 阅读';
|
||||
export const opening = ['窗外又下雨了。', '刚好读完一页书。你今天过得怎么样?'];
|
||||
export const moments = [ ['20:36', '把台灯调暗了一点,留一盏暖光给这一页。'], ['21:02', '读到一句喜欢的话,停下来听了一会儿雨。'], ['21:08', '书还摊在膝上,城市在窗外慢慢亮着。'] ];
|
||||
export const presets = [ { name: '雨夜阅读', density: 1, warmth: .12 }, { name: '暖灯小憩', density: .55, warmth: .22 }, { name: '静听蓝城', density: 1.4, warmth: .04 } ];
|
||||
export function reply(text: string, turn: number): string {
|
||||
if (/书|读/.test(text)) return '这段演示里,我正翻到书中描写雨夜的那一页。你最近有读到喜欢的句子吗?';
|
||||
if (/累|忙|难过/.test(text)) return '那就先歇一会儿吧。不急着把今天说完,也可以从一件小事慢慢说起。';
|
||||
if (/雨|晚|好/.test(text)) return '雨落在窗上的声音很轻。你那边的夜晚是什么样的?';
|
||||
return ['这是一段预设的演示对话。你可以试着和我聊聊书、雨夜,或者今天的心情。', '把今天留一点在这里吧。现在显示的是演示台词,尚未接入性格与记忆引擎。'][turn % 2];
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
export class RainAudio {
|
||||
private context?: AudioContext;
|
||||
private gain?: GainNode;
|
||||
enabled = false;
|
||||
volume = .25;
|
||||
async set(enabled: boolean, volume = this.volume) {
|
||||
this.enabled = enabled; this.volume = volume;
|
||||
if (enabled && !this.context) {
|
||||
this.context = new AudioContext();
|
||||
const buffer = this.context.createBuffer(1, this.context.sampleRate * 3, this.context.sampleRate);
|
||||
const values = buffer.getChannelData(0);
|
||||
for (let i = 0; i < values.length; i++) values[i] = Math.random() * 2 - 1;
|
||||
const source = this.context.createBufferSource(); source.buffer = buffer; source.loop = true;
|
||||
const filter = this.context.createBiquadFilter(); filter.type = 'lowpass'; filter.frequency.value = 1200;
|
||||
this.gain = this.context.createGain();
|
||||
source.connect(filter).connect(this.gain).connect(this.context.destination); source.start();
|
||||
}
|
||||
if (this.gain && this.context) this.gain.gain.setTargetAtTime(volume * .3, this.context.currentTime, .1);
|
||||
if (this.context) {
|
||||
if (enabled && !document.hidden) await this.context.resume();
|
||||
else await this.context.suspend();
|
||||
}
|
||||
}
|
||||
async visibility() { if (this.context) { if (document.hidden) await this.context.suspend(); else if (this.enabled) await this.context.resume(); } }
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
import './app/style.css';
|
||||
import { shell } from './app/shell';
|
||||
import { opening, moments, presets, reply, caption, momentTitle } from './demo/script';
|
||||
import { probe, stream, demoStream } from './net/llm';
|
||||
import { Scene } from './scene/scene';
|
||||
import { RainAudio } from './engine/audio';
|
||||
import type { ChatMessage, FxLevel } from './state/contracts';
|
||||
const $ = <T extends HTMLElement = HTMLElement>(id: string) => document.getElementById(id) as T;
|
||||
$('app').innerHTML = shell;
|
||||
$('caption').textContent = caption.join('\n');
|
||||
$('moment-title').textContent = momentTitle;
|
||||
let toastTimer: ReturnType<typeof setTimeout>;
|
||||
function toast(text: string) { $('toast').textContent = text; $('toast').hidden = false; clearTimeout(toastTimer); toastTimer = setTimeout(() => { $('toast').hidden = true; }, 5000); }
|
||||
const scene = new Scene($('effects'), toast);
|
||||
void scene.set('full');
|
||||
const audio = new RainAudio();
|
||||
const history: ChatMessage[] = [];
|
||||
let live = false, modeChosen = false, probing = true, active: AbortController | undefined, composing = false, follow = true, turn = 0;
|
||||
let view = 'chat';
|
||||
const input = $<HTMLTextAreaElement>('input');
|
||||
const messages = $('messages');
|
||||
function scroll() { if (follow) messages.scrollTop = messages.scrollHeight; }
|
||||
new ResizeObserver(() => { if (follow) scroll(); }).observe(messages);
|
||||
messages.addEventListener('scroll', () => { follow = messages.scrollHeight - messages.scrollTop - messages.clientHeight < 70; $('bottom').hidden = follow; });
|
||||
$('bottom').onclick = () => { follow = true; scroll(); $('bottom').hidden = true; };
|
||||
function bubble(message: ChatMessage) {
|
||||
const article = document.createElement('article'); article.className = `message ${message.role}`;
|
||||
const label = document.createElement('small'); label.textContent = message.source === 'live' ? '实时模型' : '演示对话 · 非实时模型';
|
||||
const text = document.createElement('p'); text.textContent = message.text;
|
||||
article.append(label, text); messages.append(article); scroll(); return { article, text };
|
||||
}
|
||||
for (const text of opening) { const message: ChatMessage = { role: 'assistant', text, source: 'demo', complete: true }; history.push(message); bubble(message); }
|
||||
function modeLabel() {
|
||||
$<HTMLButtonElement>('mode').disabled = false;
|
||||
$('connection').textContent = live ? '实时模型 · 已连接' : '演示对话 · 非实时模型';
|
||||
$('mode').textContent = live ? '切换演示' : '检测并连接模型';
|
||||
}
|
||||
void probe().then(ok => { probing = false; if (!modeChosen) { live = ok; modeLabel(); if (!ok) toast('内网 LLM 不可达,已进入明确标注的演示模式'); } });
|
||||
$('mode').onclick = async () => {
|
||||
if (active) { toast('请先取消当前回应'); return; }
|
||||
modeChosen = true;
|
||||
if (live) { live = false; modeLabel(); return; }
|
||||
$('connection').textContent = '正在探测模型…';
|
||||
const ok = await probe(); live = ok; modeLabel(); if (!ok) toast('模型不可达,继续使用演示模式');
|
||||
};
|
||||
async function respond(user: ChatMessage, existing?: ChatMessage, node?: ReturnType<typeof bubble>) {
|
||||
const source = user.source;
|
||||
const response: ChatMessage = existing ?? { role: 'assistant', text: '', source, complete: false };
|
||||
if (!existing) history.push(response);
|
||||
response.text = ''; response.complete = false;
|
||||
const elements = node ?? bubble(response);
|
||||
elements.text.textContent = '回应中…'; elements.article.classList.add('pending');
|
||||
elements.article.querySelector('.error')?.remove();
|
||||
active = new AbortController(); const controller = active;
|
||||
$('cancel').hidden = false; $<HTMLButtonElement>('send').disabled = true;
|
||||
$('chat-status').textContent = source === 'live' ? '正在等待模型回应…' : '正在显示演示对话…';
|
||||
try {
|
||||
const delta = (text: string) => { response.text += text; elements.text.textContent = response.text; scroll(); };
|
||||
if (source === 'live') await stream(history.slice(0, history.indexOf(response)), controller.signal, delta);
|
||||
else await demoStream(reply(user.text, turn++), controller.signal, delta);
|
||||
response.complete = true; $('chat-status').textContent = '回应已完成';
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '网络连接失败';
|
||||
if (!response.text) elements.text.textContent = '未收到回复。';
|
||||
const row = document.createElement('div'); row.className = 'error';
|
||||
const label = document.createElement('span'); label.textContent = message;
|
||||
const retry = document.createElement('button'); retry.textContent = '重试';
|
||||
retry.onclick = () => {
|
||||
if (active) return toast('请先结束当前回应');
|
||||
// Keep retry context deterministic; no concurrent/newer turn is duplicated.
|
||||
if (history[history.length - 1] !== response) return toast('已有后续对话,请重新发送需要重试的内容');
|
||||
void respond(user, response, elements);
|
||||
};
|
||||
row.append(label, retry); elements.article.append(row); $('chat-status').textContent = message;
|
||||
} finally {
|
||||
elements.article.classList.remove('pending');
|
||||
if (active === controller) active = undefined;
|
||||
$('cancel').hidden = true; $<HTMLButtonElement>('send').disabled = false; scroll();
|
||||
}
|
||||
}
|
||||
$('compose').onsubmit = event => {
|
||||
event.preventDefault();
|
||||
if (active || composing || !input.value.trim()) return;
|
||||
if (probing) { toast('模型检测中,请稍候'); return; }
|
||||
const text = input.value.trim(); if ([...text].length > 500) { toast('最多输入500字'); return; }
|
||||
const message: ChatMessage = { role: 'user', text, source: live ? 'live' : 'demo', complete: true };
|
||||
history.push(message); follow = true; bubble(message); input.value = ''; count();
|
||||
void respond(message);
|
||||
};
|
||||
function count() { $('count').textContent = `${[...input.value].length} / 500`; }
|
||||
input.oninput = count;
|
||||
input.addEventListener('compositionstart', () => { composing = true; });
|
||||
input.addEventListener('compositionend', () => { composing = false; count(); });
|
||||
input.onkeydown = event => {
|
||||
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing && !composing && event.keyCode !== 229) {
|
||||
event.preventDefault(); $<HTMLFormElement>('compose').requestSubmit();
|
||||
}
|
||||
};
|
||||
$('cancel').onclick = () => active?.abort();
|
||||
function cancel() { active?.abort(); }
|
||||
$('collapse').onclick = () => { cancel(); document.body.classList.add('chat-hidden'); $('restore').hidden = false; };
|
||||
$('restore').onclick = () => { document.body.classList.remove('chat-hidden'); $('restore').hidden = true; requestAnimationFrame(scroll); };
|
||||
const drawer = $('drawer');
|
||||
function expand(expanded: boolean) { document.body.classList.toggle('expanded', expanded); drawer.setAttribute('aria-expanded', String(expanded)); drawer.setAttribute('aria-label', expanded ? '收起抽屉' : '展开对话'); }
|
||||
let dragged = false;
|
||||
drawer.onclick = () => { if (dragged) { dragged = false; return; } expand(!document.body.classList.contains('expanded')); };
|
||||
let startY = 0;
|
||||
drawer.onpointerdown = e => { dragged = false; startY = e.clientY; drawer.setPointerCapture(e.pointerId); };
|
||||
drawer.onpointerup = e => { if (Math.abs(e.clientY - startY) > 15) { dragged = true; expand(e.clientY < startY); } };
|
||||
function showView(next: string) {
|
||||
cancel(); view = next;
|
||||
document.body.classList.remove('chat-hidden'); $('restore').hidden = true;
|
||||
for (const button of document.querySelectorAll<HTMLButtonElement>('[data-view]')) {
|
||||
if (button.dataset.view === next) button.setAttribute('aria-current', 'page'); else button.removeAttribute('aria-current');
|
||||
}
|
||||
$('chat-view').hidden = next !== 'chat'; $('preview-view').hidden = next === 'chat';
|
||||
const content = $('preview-content'); content.replaceChildren();
|
||||
$('preview-title').textContent = next === 'daily' ? '她的日常' : '命盘';
|
||||
if (next === 'daily') {
|
||||
for (const [time, text] of moments) { const p = document.createElement('p'); const small = document.createElement('small'); small.textContent = `${time} · 演示片刻`; const body = document.createElement('p'); body.textContent = text; p.append(small, body); content.append(p); }
|
||||
} else { const p = document.createElement('p'); p.textContent = '星轨尚未点亮。命盘将呈现角色的稳定底色,本轮尚未接入排盘引擎,不提供运势推演。'; content.append(p); }
|
||||
if (next !== 'chat') expand(true); else { follow = true; requestAnimationFrame(scroll); }
|
||||
}
|
||||
for (const button of document.querySelectorAll<HTMLButtonElement>('[data-view]')) button.onclick = () => showView(button.dataset.view!);
|
||||
$('moment').onclick = () => showView('daily');
|
||||
$('attachment').onclick = () => toast('附件功能未接入,本轮只支持文字。');
|
||||
$('voice').onclick = () => toast('语音录入与 TTS 未接入,不会启动麦克风。');
|
||||
const settings = $<HTMLDialogElement>('settings');
|
||||
$('settings-open').onclick = () => settings.showModal(); $('settings-close').onclick = () => settings.close();
|
||||
settings.addEventListener('click', e => { if (e.target === settings && (e.clientX < settings.getBoundingClientRect().left || e.clientX > settings.getBoundingClientRect().right || e.clientY < settings.getBoundingClientRect().top || e.clientY > settings.getBoundingClientRect().bottom)) settings.close(); });
|
||||
presets.forEach((preset, index) => {
|
||||
const button = document.createElement('button'); button.textContent = preset.name; button.setAttribute('aria-pressed', String(index === 0));
|
||||
button.onclick = () => { scene.preset(index); for (const child of $('presets').children) child.setAttribute('aria-pressed', String(child === button)); toast(`氛围已切换为${preset.name}`); }; $('presets').append(button);
|
||||
});
|
||||
$<HTMLSelectElement>('quality').onchange = event => { const level = (event.target as HTMLSelectElement).value as FxLevel; void scene.set(level); toast(matchMedia('(prefers-reduced-motion: reduce)').matches ? '系统已开启减少动效,场景保持静态' : '动效档位已切换'); };
|
||||
try { audio.volume = Number(localStorage.getItem('rain-volume') ?? .25); } catch { /* storage optional */ }
|
||||
$<HTMLInputElement>('volume').value = String(audio.volume * 100);
|
||||
$<HTMLInputElement>('rain-sound').onchange = async event => {
|
||||
const checkbox = event.target as HTMLInputElement;
|
||||
try { await audio.set(checkbox.checked); $('audio-status').textContent = checkbox.checked ? '程序化雨声已开启' : '雨声已关闭'; }
|
||||
catch { checkbox.checked = false; audio.enabled = false; $('audio-status').textContent = '浏览器无法启动音频,雨声保持关闭'; }
|
||||
};
|
||||
$<HTMLInputElement>('volume').oninput = event => { const volume = Number((event.target as HTMLInputElement).value) / 100; void audio.set(audio.enabled, volume).catch(() => toast('音频不可用')); try { localStorage.setItem('rain-volume', String(volume)); } catch { /* optional */ } };
|
||||
const bars = document.querySelector('.wave-bars')!;
|
||||
for (let i = 0; i < 48; i++) { const bar = document.createElement('i'); bar.style.setProperty('--bar-height', `${4 + Math.sin(i * 1.8) ** 2 * 14}px`); bar.style.setProperty('--delay', `${i * -0.09}s`); bars.append(bar); }
|
||||
$('wave').onclick = () => { const on = $('wave').getAttribute('aria-pressed') !== 'true'; $('wave').setAttribute('aria-pressed', String(on)); $('wave').textContent = on ? 'Ⅱ' : '▷'; bars.classList.toggle('playing', on); };
|
||||
function clock() { $('time').textContent = new Intl.DateTimeFormat('zh-CN', { hour: '2-digit', minute: '2-digit' }).format(new Date()); }
|
||||
let clockTimer: ReturnType<typeof setInterval> | undefined;
|
||||
function scheduleClock() { clearInterval(clockTimer); clock(); if (!document.hidden) clockTimer = setInterval(clock, 30000); }
|
||||
scheduleClock();
|
||||
document.addEventListener('visibilitychange', () => { void audio.visibility().catch(() => {}); scheduleClock(); });
|
||||
function viewport() {
|
||||
const height = window.visualViewport?.height ?? innerHeight;
|
||||
document.documentElement.style.setProperty('--visible-height', `${height}px`);
|
||||
document.body.classList.toggle('keyboard', height < innerHeight * .78);
|
||||
if (document.body.classList.contains('keyboard') && document.activeElement === input) requestAnimationFrame(() => input.scrollIntoView({ block: 'end' }));
|
||||
}
|
||||
window.visualViewport?.addEventListener('resize', viewport); viewport();
|
||||
input.onfocus = () => { if (view !== 'chat') showView('chat'); };
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { ChatMessage } from '../state/contracts';
|
||||
export async function probe(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch('/v1/models', { signal: AbortSignal.timeout(2000) });
|
||||
const body = await response.json();
|
||||
return response.ok && body.data?.some((m: { id: string }) => m.id === 'halogen-qwen3.8-flash-next');
|
||||
} catch { return false; }
|
||||
}
|
||||
// SSE is decoded across arbitrary UTF-8 / line boundaries. Completion is required;
|
||||
// a truncated stream preserves visible output and offers explicit manual retry.
|
||||
export async function stream(messages: ChatMessage[], signal: AbortSignal, delta: (s: string) => void): Promise<void> {
|
||||
const controller = new AbortController();
|
||||
const cancel = () => controller.abort(signal.reason);
|
||||
signal.addEventListener('abort', cancel, { once: true });
|
||||
if (signal.aborted) cancel();
|
||||
let timeout = false;
|
||||
const expire = () => { timeout = true; controller.abort(); };
|
||||
const total = setTimeout(expire, 40000);
|
||||
let first: ReturnType<typeof setTimeout> | undefined = setTimeout(expire, 8000);
|
||||
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
||||
try {
|
||||
const response = await fetch('/v1/chat/completions', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: controller.signal,
|
||||
body: JSON.stringify({ model: 'halogen-qwen3.8-flash-next', stream: true, max_tokens: 600,
|
||||
messages: [{ role: 'system', content: '你叫晚晚,简短温和地用中文交流,每次不超过150字。不得声称拥有记忆或情绪引擎,不得声称已接入语音或真实生活引擎。' },
|
||||
...messages.filter(m => m.complete && m.source === 'live').slice(-16).map(m => ({ role: m.role, content: m.text }))] })
|
||||
});
|
||||
if (!response.ok || !response.body) throw new Error(`模型服务返回 ${response.status}`);
|
||||
reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '', done = false, received = false;
|
||||
const line = (s: string) => {
|
||||
if (!s.startsWith('data:')) return;
|
||||
const value = s.slice(5).trim();
|
||||
if (!value) return;
|
||||
if (value === '[DONE]') { done = true; return; }
|
||||
const event = JSON.parse(value);
|
||||
if (event.error) throw new Error('模型服务返回错误');
|
||||
const choice = event.choices?.[0];
|
||||
if (choice?.delta?.content) {
|
||||
received = true; clearTimeout(first); first = undefined;
|
||||
delta(choice.delta.content);
|
||||
}
|
||||
if (choice?.finish_reason) done = true;
|
||||
};
|
||||
while (!done) {
|
||||
const chunk = await reader.read();
|
||||
buffer += decoder.decode(chunk.value, { stream: !chunk.done });
|
||||
const lines = buffer.split('\n'); buffer = lines.pop() ?? '';
|
||||
for (const item of lines) line(item.trimEnd());
|
||||
if (chunk.done) { if (buffer) line(buffer); break; }
|
||||
}
|
||||
if (!done) throw new Error('流式连接中断,已收到的内容保留');
|
||||
if (!received) throw new Error('模型未返回正文,请手动重试');
|
||||
} catch (error) {
|
||||
if (timeout) throw new Error('模型响应超时,请手动重试');
|
||||
if (signal.aborted) throw new Error('已取消,已收到的内容保留');
|
||||
if (error instanceof SyntaxError) throw new Error('流式数据格式错误,已收到的内容保留');
|
||||
if (error instanceof TypeError) throw new Error('网络连接失败,请检查模型服务后重试');
|
||||
throw error instanceof Error ? error : new Error('网络连接失败');
|
||||
} finally {
|
||||
clearTimeout(total); clearTimeout(first);
|
||||
signal.removeEventListener('abort', cancel);
|
||||
await reader?.cancel().catch(() => {});
|
||||
}
|
||||
}
|
||||
export async function demoStream(text: string, signal: AbortSignal, delta: (s: string) => void) {
|
||||
for (const character of text) {
|
||||
if (signal.aborted) throw new Error('已取消,已收到的内容保留');
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const cancel = () => { clearTimeout(timer); reject(new Error('已取消,已收到的内容保留')); };
|
||||
const timer = setTimeout(() => { signal.removeEventListener('abort', cancel); resolve(); }, 40);
|
||||
signal.addEventListener('abort', cancel, { once: true });
|
||||
});
|
||||
delta(character);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import type { FxLevel } from '../state/contracts';
|
||||
import { presets } from '../demo/script';
|
||||
interface Renderer { draw(t: number, density: number): void; destroy(): void }
|
||||
type Tier = 'webgl' | 'canvas' | 'css' | 'static';
|
||||
// Pure timeline coordinates: the scene never reads or writes chat state.
|
||||
function drops(t: number, count: number, w: number, h: number, line: (x: number, y: number, length: number, alpha: number) => void) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
const speed = 48 + (i * 37 % 110);
|
||||
line((i * 137.51 % w), ((i * 87.37 + t * speed) % (h + 70)) - 35, 9 + i % 24, .1 + i % 5 * .045);
|
||||
}
|
||||
}
|
||||
export class Scene {
|
||||
private renderer?: Renderer;
|
||||
private frame = 0;
|
||||
private generation = 0;
|
||||
private tier: Tier = 'static';
|
||||
private level: FxLevel = 'full';
|
||||
private time = 0;
|
||||
private last = 0;
|
||||
private sample = 0;
|
||||
private frames = 0;
|
||||
private slow = 0;
|
||||
private reduced = matchMedia('(prefers-reduced-motion: reduce)');
|
||||
private mood = presets[0];
|
||||
private px = 0; private py = 0; private x = 0; private y = 0;
|
||||
constructor(private host: HTMLElement, private notify: (text: string) => void) {
|
||||
this.reduced.addEventListener('change', () => { void this.set(this.level); });
|
||||
document.addEventListener('visibilitychange', () => this.visibility());
|
||||
window.addEventListener('resize', () => { void this.configure(this.tier); });
|
||||
window.addEventListener('pointermove', e => {
|
||||
if (e.pointerType === 'mouse' && matchMedia('(pointer:fine)').matches) {
|
||||
this.px = (e.clientX / innerWidth - .5) * -12;
|
||||
this.py = (e.clientY / innerHeight - .5) * -12;
|
||||
}
|
||||
});
|
||||
}
|
||||
async set(level: FxLevel) {
|
||||
this.level = level;
|
||||
await this.configure(this.reduced.matches || level === 'static' ? 'static' : level === 'lite' ? 'canvas' : 'webgl');
|
||||
}
|
||||
preset(index: number) {
|
||||
this.mood = presets[index];
|
||||
document.documentElement.style.setProperty('--lamp-opacity', String(this.mood.warmth));
|
||||
}
|
||||
private async configure(tier: Tier) {
|
||||
const version = ++this.generation;
|
||||
cancelAnimationFrame(this.frame); this.frame = 0;
|
||||
this.renderer?.destroy(); this.renderer = undefined;
|
||||
this.host.replaceChildren();
|
||||
this.tier = tier; this.last = 0; this.sample = 0; this.frames = 0; this.slow = 0;
|
||||
const w = Math.min(innerWidth, 1920), h = Math.min(innerHeight, 1080);
|
||||
let renderer: Renderer | undefined;
|
||||
try {
|
||||
if (tier === 'webgl') {
|
||||
const { Application, Graphics, RendererType } = await import('pixi.js');
|
||||
if (version !== this.generation) return;
|
||||
const app = new Application();
|
||||
try { await app.init({ width: w, height: h, resolution: 1, backgroundAlpha: 0, preference: ['webgl'], autoStart: false, antialias: false }); if (app.renderer.type !== RendererType.WEBGL) throw new Error('WebGL renderer unavailable'); }
|
||||
catch (error) { try { app.destroy(true); } catch { /* incomplete initialization */ } throw error; }
|
||||
if (version !== this.generation) { app.destroy(true); return; }
|
||||
const graphics = new Graphics(); app.stage.addChild(graphics);
|
||||
const contextLost = (e: Event) => {
|
||||
e.preventDefault(); this.notify('图形上下文丢失,已切换轻量雨幕'); void this.configure('canvas');
|
||||
};
|
||||
app.canvas.addEventListener('webglcontextlost', contextLost);
|
||||
this.host.append(app.canvas);
|
||||
renderer = { draw: (t, density) => {
|
||||
graphics.clear();
|
||||
drops(t, Math.round(180 * density), w, h, (x, y, length) => {
|
||||
graphics.moveTo(x, y).lineTo(x - 2, y + length);
|
||||
});
|
||||
graphics.stroke({ color: 0x718fc2, alpha: .24, width: 1 });
|
||||
for (let i = 0; i < 16; i++) {
|
||||
const x = i * 101.3 % w, y = ((i * 137 + t * 12) % h);
|
||||
graphics.ellipse(x, y, 1.5, 4).fill({ color: 0xc9ddff, alpha: .25 + Math.sin(t + i) * .12 });
|
||||
}
|
||||
app.renderer.render(app.stage);
|
||||
}, destroy: () => { app.canvas.removeEventListener('webglcontextlost', contextLost); app.destroy(true, { children: true }); } };
|
||||
} else if (tier === 'canvas') {
|
||||
const canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h;
|
||||
const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Canvas2D unavailable');
|
||||
this.host.append(canvas);
|
||||
renderer = { draw: (t, density) => {
|
||||
ctx.clearRect(0, 0, w, h); ctx.lineWidth = 1;
|
||||
drops(t, Math.round(80 * density), w, h, (x, y, length, alpha) => {
|
||||
ctx.strokeStyle = `rgba(113,143,194,${alpha})`; ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x - 2, y + length); ctx.stroke();
|
||||
});
|
||||
}, destroy: () => canvas.remove() };
|
||||
}
|
||||
} catch {
|
||||
if (version !== this.generation) return;
|
||||
this.notify(tier === 'webgl' ? 'WebGL 不可用,已切换 Canvas2D' : 'Canvas2D 不可用,已切换 CSS 雨幕');
|
||||
await this.configure(tier === 'webgl' ? 'canvas' : 'css'); return;
|
||||
}
|
||||
if (version !== this.generation) { renderer?.destroy(); return; }
|
||||
this.renderer = renderer;
|
||||
document.body.dataset.renderer = tier;
|
||||
document.body.dataset.motion = tier === 'static' ? 'static' : 'active';
|
||||
this.visibility();
|
||||
}
|
||||
private visibility() {
|
||||
cancelAnimationFrame(this.frame); this.frame = 0; this.last = 0;
|
||||
document.body.classList.toggle('paused', document.hidden);
|
||||
this.host.dataset.running = String(!document.hidden && this.tier !== 'static');
|
||||
if (!document.hidden && this.tier !== 'static') this.frame = requestAnimationFrame(this.tick);
|
||||
}
|
||||
private tick = (now: number) => {
|
||||
const dt = this.last ? now - this.last : 16.7; this.last = now;
|
||||
this.time += Math.min(dt, 50) / 1000;
|
||||
this.x += (this.px - this.x) * .08; this.y += (this.py - this.y) * .08;
|
||||
if (matchMedia('(pointer:fine)').matches) this.host.style.transform = `translate(${this.x}px,${this.y}px)`;
|
||||
this.renderer?.draw(this.time, this.mood.density);
|
||||
this.sample += dt; this.frames++;
|
||||
if (this.sample >= 4000) {
|
||||
this.slow = this.frames / (this.sample / 1000) < 30 ? this.slow + 1 : 0;
|
||||
this.sample = 0; this.frames = 0;
|
||||
if (this.slow) {
|
||||
const next = this.tier === 'webgl' ? 'canvas' : this.tier === 'canvas' ? 'css' : 'static';
|
||||
this.notify('设备帧率较低,已自动降低场景动效'); void this.configure(next); return;
|
||||
}
|
||||
}
|
||||
this.frame = requestAnimationFrame(this.tick);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Exact data fields from Python contracts; these types do not connect the engine.
|
||||
export type Presence = 'listening' | 'speaking' | 'away' | 'asleep';
|
||||
export interface State { ts: number; v: number; a: number; d: number; e: number; mood_word: string; presence: Presence; drivers: readonly number[] }
|
||||
export type SpeechTag = 'laughter' | 'breath' | 'sigh' | 'pause' | 'cry' | 'hesitate';
|
||||
export interface Utterance { text: string; speech_tags: readonly SpeechTag[]; emotion_intent: string | null; withheld: string | null; interruptible: boolean }
|
||||
export type FxLevel = 'full' | 'lite' | 'static';
|
||||
export interface ChatMessage { role: 'user' | 'assistant'; text: string; source: 'live' | 'demo'; complete: boolean }
|
||||
@@ -0,0 +1,180 @@
|
||||
"""Foreground browser regression using preinstalled Python Playwright; no npm test dependency."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
OUT = Path(os.environ.get('CYBERGRIL_ARTIFACT_DIR', ROOT / 'qa-artifacts'))
|
||||
OUT.mkdir(parents=True, exist_ok=True)
|
||||
with socket.socket() as sock:
|
||||
sock.bind(('127.0.0.1', 0))
|
||||
PORT = sock.getsockname()[1]
|
||||
BASE = f'http://127.0.0.1:{PORT}'
|
||||
log = open(OUT / 'vite.log', 'w')
|
||||
server = subprocess.Popen(['node', 'node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', str(PORT), '--strictPort'], cwd=ROOT, stdout=log, stderr=log)
|
||||
report = {'checks': [], 'errors': [], 'browser': '', 'samples': {}}
|
||||
|
||||
def check(name, condition):
|
||||
assert condition, name
|
||||
report['checks'].append(name)
|
||||
print('PASS', name, flush=True)
|
||||
|
||||
def screenshot(page, name):
|
||||
page.screenshot(path=str(OUT / (name + '.png')), full_page=True)
|
||||
|
||||
try:
|
||||
for _ in range(60):
|
||||
try:
|
||||
urllib.request.urlopen(BASE, timeout=.5)
|
||||
break
|
||||
except Exception:
|
||||
time.sleep(.1)
|
||||
with sync_playwright() as pw:
|
||||
browser = pw.chromium.launch(args=['--no-sandbox', '--enable-unsafe-swiftshader'])
|
||||
report['browser'] = browser.version
|
||||
page = browser.new_page(viewport={'width': 1600, 'height': 900}, device_scale_factor=1)
|
||||
page.on('pageerror', lambda e: report['errors'].append(str(e)))
|
||||
# Unreachable probe is a deterministic offline case, not a test-only app mode.
|
||||
page.route('**/v1/models', lambda route: route.abort())
|
||||
page.goto(BASE)
|
||||
page.wait_for_function("document.querySelector('#connection').textContent.includes('非实时模型')")
|
||||
page.wait_for_timeout(5100)
|
||||
check('unreachable probe selects labelled demo', '演示' in page.locator('#connection').inner_text())
|
||||
check('real DOM + permanent engine disclosure', page.locator('.badge').is_visible() and page.locator('.message p').count() == 2)
|
||||
check('renderer starts', page.locator('body').get_attribute('data-renderer') in ['webgl', 'canvas'])
|
||||
screenshot(page, 'desktop-1600')
|
||||
page.locator('#input').fill('中文输入法')
|
||||
page.locator('#input').dispatch_event('compositionstart')
|
||||
page.locator('#input').press('Enter')
|
||||
check('IME Enter does not submit', page.locator('.message.user').count() == 0)
|
||||
page.locator('#input').dispatch_event('compositionend')
|
||||
page.locator('#input').press('Shift+Enter')
|
||||
check('Shift Enter preserves newline', '\n' in page.locator('#input').input_value())
|
||||
page.locator('#input').fill('今天有点累')
|
||||
page.locator('#input').press('Enter')
|
||||
page.locator('#cancel').wait_for(state='visible')
|
||||
page.locator('#input').fill('流式中继续快速输入,不应丢字')
|
||||
screenshot(page, 'desktop-stream-focus')
|
||||
check('input stays editable during stream', page.locator('#input').input_value() == '流式中继续快速输入,不应丢字')
|
||||
page.locator('#cancel').click()
|
||||
page.locator('.error').wait_for()
|
||||
check('cancel keeps partial and offers retry', '已取消' in page.locator('.error').inner_text())
|
||||
screenshot(page, 'desktop-cancel-retry')
|
||||
page.locator('.error button').click()
|
||||
page.locator('#cancel').wait_for(state='hidden', timeout=10000)
|
||||
check('manual retry succeeds without duplicate user turn', page.locator('.message.user').count() == 1 and page.locator('.error').count() == 0)
|
||||
page.locator('#input').fill('长' * 500)
|
||||
check('500 character input available', page.locator('#input').input_value() == '长' * 500)
|
||||
page.locator('#input').fill('')
|
||||
page.locator('#voice').click(force=True)
|
||||
check('unavailable voice explains itself', '不会启动麦克风' in page.locator('#toast').inner_text())
|
||||
page.locator('#collapse').click()
|
||||
check('collapse restores conversation entry', page.locator('#restore').is_visible() and not page.locator('.panel').is_visible())
|
||||
page.locator('#restore').click()
|
||||
page.locator('[data-view=daily]').click()
|
||||
check('daily has explicitly marked demo moments', page.locator('#preview-content small').count() == 3)
|
||||
screenshot(page, 'daily-preview')
|
||||
page.locator('[data-view=fate]').click()
|
||||
check('fate is preview only', '未接入排盘引擎' in page.locator('#preview-content').inner_text())
|
||||
page.locator('[data-view=chat]').click()
|
||||
page.locator('#settings-open').click()
|
||||
original = page.evaluate("getComputedStyle(document.documentElement).getPropertyValue('--accent')")
|
||||
page.get_by_role('button', name='静听蓝城', exact=True).click()
|
||||
check('preset keeps warm UI accent', original == page.evaluate("getComputedStyle(document.documentElement).getPropertyValue('--accent')"))
|
||||
page.locator('#quality').select_option('static')
|
||||
page.wait_for_function("document.body.dataset.renderer === 'static'")
|
||||
check('static stops scene', page.locator('#effects').get_attribute('data-running') == 'false')
|
||||
page.locator('#quality').select_option('lite')
|
||||
page.wait_for_function("document.body.dataset.renderer === 'canvas'")
|
||||
check('lite uses independent Canvas2D', page.locator('#effects canvas').count() == 1)
|
||||
page.locator('#rain-sound').check()
|
||||
check('rain only starts on user action', '已开启' in page.locator('#audio-status').inner_text())
|
||||
page.locator('#volume').fill('50')
|
||||
page.locator('#rain-sound').uncheck()
|
||||
page.locator('#settings-close').click()
|
||||
page.set_viewport_size({'width':1280,'height':800})
|
||||
screenshot(page,'desktop-1280')
|
||||
page.set_viewport_size({'width':390,'height':844})
|
||||
page.evaluate("document.body.classList.remove('expanded')")
|
||||
screenshot(page,'mobile-collapsed')
|
||||
check('mobile default drawer near 30 percent', abs(page.locator('.panel').bounding_box()['height'] / 844 - .30) < .02)
|
||||
page.locator('#drawer').click()
|
||||
screenshot(page,'mobile-expanded')
|
||||
check('mobile expansion at or below 55 percent', page.locator('.panel').bounding_box()['y'] >= 844*.45-1)
|
||||
page.locator('#input').focus()
|
||||
screenshot(page,'mobile-focus')
|
||||
page.set_viewport_size({'width':844,'height':390})
|
||||
screenshot(page,'mobile-landscape')
|
||||
check('landscape is scrollable scene above chat', page.locator('.panel').bounding_box()['y'] >= 300)
|
||||
check('no horizontal overflow', page.evaluate('document.documentElement.scrollWidth <= innerWidth'))
|
||||
page.set_viewport_size({'width':1600,'height':900})
|
||||
page.emulate_media(reduced_motion='reduce')
|
||||
page.wait_for_function("document.body.dataset.renderer === 'static'")
|
||||
screenshot(page,'reduced-motion')
|
||||
check('reduced motion enforces static', page.locator('#effects').get_attribute('data-running') == 'false')
|
||||
page.emulate_media(reduced_motion='no-preference')
|
||||
page.wait_for_function("document.querySelector('#effects').dataset.running === 'true'")
|
||||
# Visibility lifecycle event injected because headless tabs do not reliably hide.
|
||||
page.evaluate("Object.defineProperty(document, 'hidden', {configurable:true,get:()=>true}); document.dispatchEvent(new Event('visibilitychange'))")
|
||||
check('visibility handler pauses scene and CSS', page.locator('#effects').get_attribute('data-running') == 'false' and 'paused' in page.locator('body').get_attribute('class'))
|
||||
page.evaluate("Object.defineProperty(document, 'hidden', {configurable:true,get:()=>false}); document.dispatchEvent(new Event('visibilitychange'))")
|
||||
check('visibility handler restores selected animation', page.locator('#effects').get_attribute('data-running') == 'true')
|
||||
# True API availability failure injection, no app query override.
|
||||
for name, blocked, expected in [('webgl-fallback', ['webgl','webgl2','experimental-webgl'], 'canvas'), ('canvas-fallback', ['webgl','webgl2','experimental-webgl','2d'], 'css')]:
|
||||
ctx = browser.new_context(viewport={'width':1280,'height':800})
|
||||
ctx.add_init_script("const original=HTMLCanvasElement.prototype.getContext; HTMLCanvasElement.prototype.getContext=function(type,...args){if("+json.dumps(blocked)+".includes(type))return null;return original.call(this,type,...args)}")
|
||||
p = ctx.new_page(); p.route('**/v1/models', lambda route: route.abort()); p.goto(BASE)
|
||||
p.wait_for_function(f"document.body.dataset.renderer === '{expected}'",timeout=12000)
|
||||
check(name, True); screenshot(p,name); ctx.close()
|
||||
# Live mode cases use model-compatible SSE fixtures.
|
||||
page.unroute('**/v1/models')
|
||||
page.route('**/v1/models', lambda route: route.fulfill(json={'data':[{'id':'halogen-qwen3.8-flash-next'}]}))
|
||||
page.locator('#mode').click()
|
||||
page.wait_for_function("document.querySelector('#connection').textContent.includes('已连接')")
|
||||
def completion(route):
|
||||
report['last_request'] = route.request.post_data_json
|
||||
route.fulfill(content_type='text/event-stream', body='data: {"choices":[{"delta":{"content":"<script>模型文本</script>中文"}}]}\r\n\r\ndata: [DONE]\r\n\r\n')
|
||||
page.route('**/v1/chat/completions', completion)
|
||||
page.locator('#input').fill('实时测试'); page.locator('#send').click()
|
||||
page.wait_for_function("document.querySelector('#chat-status').textContent === '回应已完成'")
|
||||
check('live SSE renders unsafe-looking text as text', '<script>模型文本</script>' in page.locator('.message.assistant').last.inner_text() and page.locator('.message script').count()==0)
|
||||
check('live request excludes demo replies', all('窗外又下雨' not in m['content'] for m in report['last_request']['messages']))
|
||||
page.unroute('**/v1/chat/completions')
|
||||
page.route('**/v1/chat/completions', lambda route: route.fulfill(status=503,body='Unavailable'))
|
||||
page.locator('#input').fill('失败测试'); page.locator('#send').click(); page.locator('.error').wait_for()
|
||||
check('upstream error visible with retry', '503' in page.locator('.error').inner_text())
|
||||
screenshot(page,'live-failure')
|
||||
page.unroute('**/v1/chat/completions')
|
||||
page.route('**/v1/chat/completions', completion)
|
||||
page.locator('.error button').click(); page.wait_for_function("document.querySelector('#chat-status').textContent === '回应已完成'")
|
||||
check('live retry clears failure', page.locator('.error').count()==0)
|
||||
page.unroute('**/v1/chat/completions')
|
||||
page.route('**/v1/chat/completions', lambda route: route.fulfill(content_type='text/event-stream',body='data: {"choices":[{"delta":{"content":"已经收到的一部分"}}]}\n\n'))
|
||||
page.locator('#input').fill('中断测试'); page.locator('#send').click(); page.locator('.error').wait_for()
|
||||
check('truncated SSE keeps partial text', '已经收到的一部分' in page.locator('.message.assistant').last.inner_text() and '中断' in page.locator('.error').inner_text())
|
||||
# Browser scheduling sample with chat input during independent animation.
|
||||
page.locator('#settings-open').click(); page.locator('#quality').select_option('full'); page.locator('#settings-close').click()
|
||||
page.wait_for_timeout(1000)
|
||||
sample = page.evaluate("""async () => {const gaps=[],tasks=[]; const po=new PerformanceObserver(l=>tasks.push(...l.getEntries().map(e=>e.duration)));po.observe({type:'longtask'});let last=performance.now();await new Promise(resolve=>{const end=last+4000;function frame(t){gaps.push(t-last);last=t;if(t<end)requestAnimationFrame(frame);else resolve()}requestAnimationFrame(frame)});po.disconnect();gaps.sort((a,b)=>a-b);return {frames:gaps.length,intervalP95:gaps[Math.floor(gaps.length*.95)],longTasks:tasks,renderer:document.body.dataset.renderer}}""")
|
||||
report['samples']['chromium1600'] = sample
|
||||
check('no unhandled page errors', not report['errors'])
|
||||
browser.close()
|
||||
# Firefox scope is reported accurately if the installed browser cannot launch.
|
||||
try:
|
||||
firefox = pw.firefox.launch()
|
||||
fp = firefox.new_page(viewport={'width':1280,'height':800})
|
||||
fp.route('**/v1/models',lambda route:route.abort());fp.goto(BASE); fp.wait_for_timeout(1000)
|
||||
screenshot(fp,'firefox-1280'); report['firefox'] = firefox.version
|
||||
check('Firefox shell usable',fp.locator('#input').is_visible());firefox.close()
|
||||
except Exception as exc:
|
||||
report['firefox_unavailable'] = str(exc)[:500]
|
||||
finally:
|
||||
server.terminate()
|
||||
server.wait(timeout=10)
|
||||
log.close()
|
||||
(OUT/'browser-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2))
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Context loss, slow-frame degradation, drawer/keyboard and browser main-thread trace."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from playwright.sync_api import sync_playwright
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
OUT=Path(os.environ.get('CYBERGRIL_ARTIFACT_DIR',ROOT/'qa-artifacts'));OUT.mkdir(parents=True,exist_ok=True)
|
||||
with socket.socket() as s:s.bind(('127.0.0.1',0));port=s.getsockname()[1]
|
||||
base=f'http://127.0.0.1:{port}'
|
||||
log=open(OUT/'lifecycle-vite.log','w')
|
||||
server=subprocess.Popen(['node','node_modules/vite/bin/vite.js','--host','127.0.0.1','--port',str(port),'--strictPort'],cwd=ROOT,stdout=log,stderr=log)
|
||||
report={'checks':[]}
|
||||
def check(name,value):
|
||||
assert value,name
|
||||
report['checks'].append(name);print('PASS',name,flush=True)
|
||||
try:
|
||||
for _ in range(60):
|
||||
try:urllib.request.urlopen(base,timeout=.5);break
|
||||
except Exception:time.sleep(.1)
|
||||
with sync_playwright() as pw:
|
||||
browser=pw.chromium.launch(args=['--no-sandbox','--enable-unsafe-swiftshader'])
|
||||
ctx=browser.new_context(viewport={'width':1600,'height':900})
|
||||
# Capture actual AudioContext state without exposing anything in application code.
|
||||
ctx.add_init_script("const Original=AudioContext;window.AudioContext=class extends Original{constructor(...args){super(...args);window.testAudio=this}}")
|
||||
page=ctx.new_page();page.route('**/v1/models',lambda r:r.abort());page.goto(base)
|
||||
page.wait_for_function("document.body.dataset.renderer==='webgl'")
|
||||
page.evaluate("document.querySelector('#effects canvas').getContext('webgl2').getExtension('WEBGL_lose_context').loseContext()")
|
||||
page.wait_for_function("document.body.dataset.renderer==='canvas'")
|
||||
check('real WebGL context loss switches independent canvas',True)
|
||||
page.locator('#settings-open').click();page.locator('#quality').select_option('full');page.locator('#settings-close').click()
|
||||
page.wait_for_function("document.body.dataset.renderer==='webgl'")
|
||||
page.set_viewport_size({'width':1280,'height':800});page.wait_for_timeout(300)
|
||||
check('resize does not report false context loss',page.locator('body').get_attribute('data-renderer')=='webgl')
|
||||
page.locator('#settings-open').click();page.locator('#rain-sound').check();page.locator('#settings-close').click()
|
||||
check('audio context runs only after explicit enable',page.evaluate("testAudio.state==='running'"))
|
||||
page.evaluate("Object.defineProperty(document,'hidden',{configurable:true,get:()=>true});document.dispatchEvent(new Event('visibilitychange'))")
|
||||
page.wait_for_function("testAudio.state==='suspended'")
|
||||
check('hidden audio suspends',True)
|
||||
page.evaluate("Object.defineProperty(document,'hidden',{configurable:true,get:()=>false});document.dispatchEvent(new Event('visibilitychange'))")
|
||||
page.wait_for_function("testAudio.state==='running'")
|
||||
check('visible audio restores user selection',True)
|
||||
page.locator('#settings-open').click();page.locator('#rain-sound').uncheck();page.locator('#settings-close').click()
|
||||
# Deterministic large model body validates internal scroll and safe text.
|
||||
page.unroute('**/v1/models');page.route('**/v1/models',lambda r:r.fulfill(json={'data':[{'id':'halogen-qwen3.8-flash-next'}]}))
|
||||
page.locator('#mode').click();page.wait_for_function("document.querySelector('#connection').textContent.includes('已连接')")
|
||||
body='data: '+json.dumps({'choices':[{'delta':{'content':'长消息。'*400}}]},ensure_ascii=False)+'\n\ndata: [DONE]\n\n'
|
||||
page.route('**/v1/chat/completions',lambda r:r.fulfill(content_type='text/event-stream',body=body))
|
||||
page.locator('#input').fill('请返回长消息');page.locator('#send').click();page.wait_for_function("document.querySelector('#chat-status').textContent==='回应已完成'")
|
||||
check('long streamed message follows bottom',page.evaluate("(()=>{const e=document.querySelector('#messages');return e.scrollHeight-e.scrollTop-e.clientHeight<70})()"))
|
||||
page.locator('#messages').evaluate('(e)=>{e.scrollTop=0}')
|
||||
page.wait_for_timeout(100)
|
||||
check('scrolling up exposes return-to-bottom',page.locator('#bottom').is_visible())
|
||||
page.locator('#bottom').click()
|
||||
check('return-to-bottom restores follow',page.evaluate("(()=>{const e=document.querySelector('#messages');return e.scrollHeight-e.scrollTop-e.clientHeight<70})()"))
|
||||
page.set_viewport_size({'width':390,'height':844});page.evaluate("document.body.classList.remove('expanded')")
|
||||
box=page.locator('#drawer').bounding_box()
|
||||
page.mouse.move(box['x']+40,box['y']+10);page.mouse.down();page.mouse.move(box['x']+40,box['y']-80,steps=5);page.mouse.up();page.wait_for_timeout(100)
|
||||
check('drawer drag expands without click reversal',page.locator('#drawer').get_attribute('aria-expanded')=='true')
|
||||
page.locator('#drawer').press('Space')
|
||||
check('drawer keyboard toggles',page.locator('#drawer').get_attribute('aria-expanded')=='false')
|
||||
page.locator('#input').focus()
|
||||
page.evaluate("Object.defineProperty(visualViewport,'height',{configurable:true,value:400});visualViewport.dispatchEvent(new Event('resize'))")
|
||||
check('software keyboard handler uses scrollable stacked layout','keyboard' in page.locator('body').get_attribute('class') and page.locator('.panel').bounding_box()['y']>=300)
|
||||
page.locator('#input').scroll_into_view_if_needed();page.screenshot(path=str(OUT/'mobile-keyboard-layout.png'),full_page=True)
|
||||
page.evaluate("delete visualViewport.height;visualViewport.dispatchEvent(new Event('resize'))")
|
||||
page.set_viewport_size({'width':1600,'height':900});page.wait_for_timeout(1000)
|
||||
cdp=ctx.new_cdp_session(page);events=[]
|
||||
cdp.on('Tracing.dataCollected',lambda data:events.extend(data['value']))
|
||||
cdp.send('Tracing.start',{'categories':'devtools.timeline,toplevel','transferMode':'ReportEvents'})
|
||||
page.locator('#input').fill('')
|
||||
page.locator('#input').press_sequentially('持续快速打字同时场景运行。'*6,delay=8)
|
||||
page.wait_for_timeout(3000)
|
||||
cdp.send('Tracing.end')
|
||||
page.wait_for_timeout(1000)
|
||||
(OUT/'chromium-main-thread-trace.json').write_text(json.dumps({'traceEvents':events}))
|
||||
raf=sorted(e['dur']/1000 for e in events if e.get('name')=='FireAnimationFrame' and 'dur' in e)
|
||||
main_ids={(e['pid'],e['tid']) for e in events if e.get('name')=='thread_name' and e.get('args',{}).get('name')=='CrRendererMain'}
|
||||
tasks=sorted(e['dur']/1000 for e in events if (e.get('pid'),e.get('tid')) in main_ids and e.get('cat')=='toplevel' and 'dur' in e)
|
||||
report['trace']={'rafCount':len(raf),'rafMaxMs':max(raf,default=0),'rafP95Ms':raf[int(len(raf)*.95)] if raf else None,'mainTaskCount':len(tasks),'mainTaskMaxMs':max(tasks,default=0),'mainTaskOver8ms':sum(t>8 for t in tasks),'scope':'Linux headless Chromium, software WebGL; not low-end Android'}
|
||||
check('rapid keystrokes survive live scene',page.locator('#input').input_value()=='持续快速打字同时场景运行。'*6)
|
||||
# Inject a 20 FPS rAF clock, exercising the real 4-second rolling sampler.
|
||||
slowctx=browser.new_context(viewport={'width':1280,'height':800})
|
||||
slowctx.add_init_script("window.requestAnimationFrame=cb=>setTimeout(()=>cb(performance.now()),50);window.cancelAnimationFrame=id=>clearTimeout(id)")
|
||||
sp=slowctx.new_page();sp.route('**/v1/models',lambda r:r.abort());sp.goto(base)
|
||||
sp.wait_for_function("document.body.dataset.renderer==='static'",timeout=20000)
|
||||
check('slow renderer automatically descends WebGL Canvas CSS static',True)
|
||||
slowctx.close();browser.close()
|
||||
finally:
|
||||
server.terminate();server.wait(timeout=10);log.close();(OUT/'lifecycle-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2))
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Actual same-origin upstream check + deterministic stream faults with real timeout durations."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from playwright.sync_api import sync_playwright
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
OUT = Path(os.environ.get('CYBERGRIL_ARTIFACT_DIR', ROOT / 'qa-artifacts'))
|
||||
OUT.mkdir(parents=True, exist_ok=True)
|
||||
with socket.socket() as sock:
|
||||
sock.bind(('127.0.0.1',0)); port=sock.getsockname()[1]
|
||||
base=f'http://127.0.0.1:{port}'
|
||||
log=open(OUT/'network-vite.log','w')
|
||||
server=subprocess.Popen(['node','node_modules/vite/bin/vite.js','--host','127.0.0.1','--port',str(port),'--strictPort'],cwd=ROOT,stdout=log,stderr=log)
|
||||
report={}
|
||||
try:
|
||||
for _ in range(60):
|
||||
try: urllib.request.urlopen(base,timeout=.5);break
|
||||
except Exception:time.sleep(.1)
|
||||
with sync_playwright() as pw:
|
||||
browser=pw.chromium.launch(args=['--no-sandbox','--enable-unsafe-swiftshader'])
|
||||
page=browser.new_page()
|
||||
page.goto(base)
|
||||
report['real_upstream']=page.evaluate("""async()=>{const {probe,stream}=await import('/src/net/llm.ts'); const reachable=await probe(); if(!reachable)return {reachable:false,via:'same-origin /v1'}; const start=performance.now();let text='';try{await stream([{role:'user',text:'你好,请用一句话打招呼。',complete:true,source:'live'}],new AbortController().signal,s=>text+=s);return {reachable:true,complete:true,text,ms:performance.now()-start}}catch(e){return {reachable:true,complete:false,text,error:e.message,ms:performance.now()-start}}}""")
|
||||
print('Real upstream:',json.dumps(report['real_upstream'],ensure_ascii=False),flush=True)
|
||||
report['faults']=page.evaluate("""async()=>{
|
||||
const {stream,probe}=await import('/src/net/llm.ts'); const original=window.fetch;
|
||||
window.fetch=async(url,options={})=>{
|
||||
if(String(url).endsWith('/models'))return new Promise((_,reject)=>options.signal.addEventListener('abort',()=>reject(new DOMException('abort','AbortError'))));
|
||||
const mode=JSON.parse(options.body).messages.at(-1).content;
|
||||
if(mode==='network')throw new TypeError('Failed to fetch');
|
||||
let timer,interval;
|
||||
const body=new ReadableStream({start(c){
|
||||
const send=s=>c.enqueue(new TextEncoder().encode(s));
|
||||
const delta=s=>'data: '+JSON.stringify({choices:[{delta:{content:s}}]})+'\\r\\n\\r\\n';
|
||||
options.signal.addEventListener('abort',()=>{clearTimeout(timer);clearInterval(interval);c.error(new DOMException('abort','AbortError'))},{once:true});
|
||||
if(mode==='first-timeout')return;
|
||||
if(mode==='total-timeout'){send(delta('保留首段'));interval=setInterval(()=>send('data: {"choices":[{"delta":{}}]}\\n\\n'),1000);return;}
|
||||
if(mode==='cancel'){send(delta('取消前已显示'));return;}
|
||||
if(mode==='empty'){send('data: [DONE]\\n\\n');c.close();return;}
|
||||
if(mode==='invalid'){send(delta('格式错误前的内容'));send('data: invalid-json\\n\\n');c.close();return;}
|
||||
const bytes=new TextEncoder().encode(delta('中文🌧️,分包正文')+'data: [DONE]\\r\\n\\r\\n');let i=0;
|
||||
const emit=()=>{if(i===bytes.length){c.close();return}c.enqueue(bytes.slice(i,i+1));i++;timer=setTimeout(emit,1)};emit();
|
||||
},cancel(){clearTimeout(timer);clearInterval(interval)}});
|
||||
return new Response(body,{headers:{'Content-Type':'text/event-stream'}});
|
||||
};
|
||||
try{
|
||||
const run=async mode=>{const start=performance.now();const controller=new AbortController();let text='',error=null;if(mode==='cancel')setTimeout(()=>controller.abort(),100);try{await stream([{role:'user',text:mode,complete:true,source:'live'}],controller.signal,s=>text+=s)}catch(e){error=e.message}return {mode,text,error,ms:Math.round(performance.now()-start)}};
|
||||
const faults=await Promise.all(['utf8','network','cancel','empty','invalid','first-timeout','total-timeout'].map(run));
|
||||
const start=performance.now(),reachable=await probe();faults.push({mode:'probe-timeout',reachable,ms:Math.round(performance.now()-start)});return faults;
|
||||
}finally{window.fetch=original}
|
||||
}""")
|
||||
faults={r['mode']:r for r in report['faults']}
|
||||
assert faults['utf8']['text']=='中文🌧️,分包正文' and faults['utf8']['error'] is None
|
||||
assert '网络连接失败' in faults['network']['error']
|
||||
assert faults['cancel']['text']=='取消前已显示' and '已取消' in faults['cancel']['error']
|
||||
assert '未返回正文' in faults['empty']['error']
|
||||
assert faults['invalid']['text']=='格式错误前的内容' and '格式错误' in faults['invalid']['error']
|
||||
assert 7900<=faults['first-timeout']['ms']<10000 and '超时' in faults['first-timeout']['error']
|
||||
assert 39900<=faults['total-timeout']['ms']<43000 and faults['total-timeout']['text']=='保留首段'
|
||||
assert 1900<=faults['probe-timeout']['ms']<4000 and not faults['probe-timeout']['reachable']
|
||||
report['passed']=True
|
||||
print('PASS UTF8 fragmentation, network error, empty reply, invalid SSE, cancel, 8s first-content timeout, 40s total timeout, 2s probe timeout',flush=True)
|
||||
browser.close()
|
||||
finally:
|
||||
server.terminate();server.wait(timeout=10);log.close()
|
||||
(OUT/'network-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2))
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Built-output smoke and clean screenshots. Preview proxy is not a deployed nginx test."""
|
||||
import gzip
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from playwright.sync_api import sync_playwright
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
OUT=Path(os.environ.get('CYBERGRIL_ARTIFACT_DIR',ROOT/'qa-artifacts'));OUT.mkdir(parents=True,exist_ok=True)
|
||||
with socket.socket() as s:s.bind(('127.0.0.1',0));port=s.getsockname()[1]
|
||||
base=f'http://127.0.0.1:{port}'
|
||||
log=open(OUT/'preview.log','w')
|
||||
server=subprocess.Popen(['node','node_modules/vite/bin/vite.js','preview','--host','127.0.0.1','--port',str(port),'--strictPort'],cwd=ROOT,stdout=log,stderr=log)
|
||||
report={'checks':[],'errors':[]}
|
||||
try:
|
||||
for _ in range(60):
|
||||
try:urllib.request.urlopen(base,timeout=.5);break
|
||||
except Exception:time.sleep(.1)
|
||||
with sync_playwright() as pw:
|
||||
browser=pw.chromium.launch(args=['--no-sandbox','--enable-unsafe-swiftshader'])
|
||||
page=browser.new_page(viewport={'width':1600,'height':900})
|
||||
page.on('pageerror',lambda e:report['errors'].append(str(e)))
|
||||
page.goto(base);page.wait_for_function("document.body.dataset.renderer==='webgl'");page.wait_for_timeout(5200)
|
||||
report['proxy']=page.evaluate("async()=>{try{const r=await fetch('/v1/models',{signal:AbortSignal.timeout(2000)});return {status:r.status,body:await r.json()}}catch(e){return {error:e.message}}}")
|
||||
def shot(name):page.screenshot(path=str(OUT/f'production-{name}.png'),full_page=True)
|
||||
shot('desktop-1600')
|
||||
page.locator('#settings-open').click();shot('desktop-settings')
|
||||
assert page.locator('#settings').bounding_box()['x']>=1100
|
||||
page.locator('#settings-close').click()
|
||||
report['checks'].append('desktop settings align with right panel outside character')
|
||||
page.set_viewport_size({'width':1280,'height':800});page.wait_for_timeout(300);shot('desktop-1280')
|
||||
page.set_viewport_size({'width':768,'height':1024});page.wait_for_timeout(300);shot('tablet-portrait')
|
||||
page.set_viewport_size({'width':390,'height':844});page.wait_for_timeout(300);shot('mobile-collapsed')
|
||||
page.locator('#drawer').click();shot('mobile-expanded')
|
||||
page.locator('#settings-open').click();shot('mobile-settings')
|
||||
assert page.locator('#settings').bounding_box()['y']>=844*.45-1
|
||||
page.locator('#presets button').last.click()
|
||||
page.locator('#quality').select_option('lite')
|
||||
page.locator('#rain-sound').check();page.locator('#rain-sound').uncheck()
|
||||
page.locator('#settings-close').click()
|
||||
report['checks'].append('mobile settings preserve face area and all scrolled controls work')
|
||||
assert page.locator('.panel').bounding_box()['y']>=844*.45-1
|
||||
report['checks'].append('built mobile drawer preserves top 45 percent face area')
|
||||
page.locator('#input').focus();shot('mobile-input-focus')
|
||||
page.set_viewport_size({'width':844,'height':390});page.wait_for_timeout(300);shot('mobile-landscape')
|
||||
assert page.locator('.panel').bounding_box()['y']>=300
|
||||
report['checks'].append('built landscape layout is scene above conversation')
|
||||
page.set_viewport_size({'width':1600,'height':900});page.emulate_media(reduced_motion='reduce');page.wait_for_function("document.body.dataset.renderer==='static'");shot('reduced-motion')
|
||||
report['checks'].append('built reduced-motion is static')
|
||||
assert page.evaluate('document.documentElement.scrollWidth<=innerWidth')
|
||||
assert not report['errors']
|
||||
report['checks'].append('production output has no unhandled errors or horizontal overflow')
|
||||
browser.close()
|
||||
files=list((ROOT/'dist').rglob('*.js'))
|
||||
report['sizes']={'jsGzipBytes':sum(len(gzip.compress(p.read_bytes())) for p in files),'jsRawBytes':sum(p.stat().st_size for p in files),'assetsBytes':sum(p.stat().st_size for p in (ROOT/'public/assets').iterdir()),'visibleWebpBytes':(ROOT/'public/assets/rain-room.webp').stat().st_size,'jpegFallbackBytes':(ROOT/'public/assets/rain-room.jpg').stat().st_size}
|
||||
assert report['sizes']['jsGzipBytes']<=300000 and report['sizes']['assetsBytes']<=1500000 and report['sizes']['visibleWebpBytes']<=900000
|
||||
report['checks'].append('compressed JS and material size budgets met')
|
||||
report['passed']=True
|
||||
print(json.dumps(report,ensure_ascii=False,indent=2),flush=True)
|
||||
finally:
|
||||
server.terminate();server.wait(timeout=10);log.close();(OUT/'production-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2))
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"lib": [
|
||||
"ES2022",
|
||||
"DOM",
|
||||
"DOM.Iterable"
|
||||
],
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"skipLibCheck": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { defineConfig } from 'vite';
|
||||
const proxy = { '/v1': { target: 'http://192.168.200.11:8731', changeOrigin: true } };
|
||||
export default defineConfig({ server: { proxy }, preview: { proxy } });
|
||||
Reference in New Issue
Block a user