HEL-898: preserve director three-fix rework verbatim on b0dcb17

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
总管
2026-10-27 07:09:23 +08:00
co-authored by multica-agent
parent b0dcb17e1a
commit 9254c17316
12 changed files with 527 additions and 115 deletions
+3 -2
View File
@@ -173,7 +173,8 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
// Her voice line withdrawn (cut, φ, text took over, you spoke, the call ended): it leaves at once, no fade.
const hadLine = useRef(false);
useLayoutEffect(() => { if (hadLine.current && !her) how.current = 'instant'; hadLine.current = !!herLine; }, [her, herLine]);
const signature = pages.map(p => p.key).join('|');
// Her spoken line can take in her next sentence under the same key, so its words count there too.
const signature = pages.map(p => p.message.live === 'speaking' || p.message.id.startsWith('voice-') ? `${p.key}=${p.text}` : p.key).join('|');
useLayoutEffect(() => {
lines.current?.present(pages, cursor, how.current, name, !motionOff());
how.current = 'advance';
@@ -360,7 +361,7 @@ export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation,
<aside className="record log-panel glass read" hidden={!log} role="dialog" aria-modal="false" aria-labelledby="record-title"><header><div><p className="eyebrow">{real ? '服务器聊天记录' : '完整记录 · 本机演示'}</p><h2 id="record-title">你们说过的话</h2></div><button className="btn-round sm" ref={logClose} aria-label="关闭记录" onClick={() => setLog(false)}><Icon name="x"/></button></header>
<div className="scroll" ref={logRef}><p className="guide">内容完整保留;关闭后回到刚才那一句,自动呈现会接着走。</p>{record.map(m => <article key={m.id} data-life={m.lifeIds.join(' ') || undefined} data-mid={m.id} data-role={m.role} className={`message ${m.status === 'failed' ? 'fail' : ''} ${(focusLife && m.lifeIds.includes(focusLife)) || focusIds.includes(m.id) ? 'hl' : ''}`}><div className="author"><i/>{m.role === 'user' ? '你' : name}{m.proactive && <span className="tagm">她主动说的</span>}</div><p>{m.text}</p>{m.status === 'failed' && <button className="btn-text" onClick={() => void send(m)}>没发出去 · 重试</button>}{m.status === 'cancelled' && <span className="incomplete">已取消</span>}{m.live === 'cancelled' && <span className="incomplete">已停止 · 这是未完成片段,未存入聊天记录</span>}</article>)}{reply && <article className="message" data-role="companion"><div className="author"><i/>{name}</div><p>{reply}</p><span className="incomplete">还在说…</span></article>}{herLine && !herLine.proactive && <article className="message" data-role="companion"><div className="author"><i/>{name}</div><p>{herLine.text}</p><span className="incomplete">{herOpen ? '还在说…' : '语音刚说完 · 正在读回聊天记录'}</span></article>}</div></aside>
{full && fullMessage && <aside className="record full glass read" role="dialog" aria-modal="false" aria-labelledby="full-title"><header><div><p className="eyebrow">{fullMessage.role === 'user' ? '你' : name} · 全文</p><h2 id="full-title">这一段,慢慢看</h2></div><button className="btn-round sm" ref={fullClose} aria-label="关闭全文" onClick={() => setFull(false)}><Icon name="x"/></button></header>
<div className="scroll"><p className="guide">整段完整可读。关闭后回到刚才那一句。</p><article className="message" data-role={fullMessage.role}><div className="author"><i/>{fullMessage.role === 'user' ? '你' : name}</div><p>{fullMessage.text}</p>{fullMessage.live === 'generating' && <span className="incomplete">还在说…</span>}{fullMessage.live === 'cancelled' && <span className="incomplete">这段回复已停止,内容未完成,未存入聊天记录。</span>}</article></div></aside>}
<div className="scroll"><p className="guide">整段完整可读。关闭后回到刚才那一句。</p><article className="message" data-role={fullMessage.role}><div className="author"><i/>{fullMessage.role === 'user' ? '你' : name}</div><p>{fullMessage.text}</p>{(fullMessage.live === 'generating' || fullMessage.live === 'speaking') && <span className="incomplete">还在说…</span>}{fullMessage.live === 'cancelled' && <span className="incomplete">这段回复已停止,内容未完成,未存入聊天记录。</span>}</article></div></aside>}
{real && permission === 'mic' && <Modal title={`开始和${name}语音?`} onClose={() => { setPermission(''); setNotice('没有开启语音,文字仍可使用。'); }} confirm="开始语音" onConfirm={() => { setPermission(''); setNotice(''); void voiceCtl.current?.start(); }}>浏览器会请求麦克风权限。你的声音只送到这台服务器上{name}的语音服务,她用同一份记忆、同一段聊天回应你;你说的话和她的回答照常存进聊天记录。直接开口就能打断她。结束语音、离开交谈或关上页面时,麦克风立即关闭。<br/><small className="voice-note">语音通路还没有经过真机验收,效果可能不稳定。</small></Modal>}
{!real && permission && <Modal title={permission === 'mic' ? '开启模拟语音?' : '打开模拟视觉窗?'} onClose={() => { setPermission(''); setDenied(true); setNotice('没有开启。真实服务尚未接通,文字仍可使用。'); }} confirm="开始模拟" onConfirm={() => { if (permission === 'mic') { setDenied(false); setVoice(true); onVoice(true); voiceController.current = new AbortController(); onPresence('在听你说(模拟)'); setNotice('模拟语音没有收音,可继续用文字输入。'); } else { if (fail) { onObservation('模拟 · 看不清,视觉服务未接通'); setNotice('模拟观察失败,文字仍可使用。'); } else setVision(true); } setPermission(''); }}>本次仅模拟界面状态,不申请麦克风或摄像头权限,不采集声音或影像,也不向外接服务发送请求。</Modal>}
</>;
+12
View File
@@ -14,6 +14,7 @@ export class LineStage {
const el = document.createElement('div');
el.className = 'line' + (current ? ' is-current' : ' is-prev');
el.dataset.role = page.message.role; el.dataset.key = page.key;
if (page.message.live === 'speaking') el.dataset.spoken = '1';
const who = document.createElement('span'); who.className = 'by';
const dot = document.createElement('i'), label = document.createElement('b'), state = document.createElement('em');
label.textContent = page.message.role === 'user' ? '你' : name; label.title = label.textContent; state.className = 'state';
@@ -38,6 +39,15 @@ export class LineStage {
{ duration: quick ? 240 : 520, easing: EASE, fill: 'forwards' }).finished.then(() => node.remove()).catch(() => node.remove());
}
clear() { this.stage.replaceChildren(); }
// Her spoken line took in the sentence she said next: the words already there stay as they are and only the new
// ones ink in after them (no motion: they just appear). Anything that is not a plain continuation is set at once.
private grow(node: HTMLElement, text: string, animate: boolean) {
const p = node.querySelector('p'); if (!p) return;
const was = p.textContent || ''; if (was === text) return;
if (!animate || !text.startsWith(was)) { p.className = ''; p.textContent = text; return; }
if (p.className !== 'ink') { p.append(text.slice(was.length)); return; }
graphemes(text.slice(was.length)).forEach((c, i) => { const s = document.createElement('span'); s.style.setProperty('--i', String(Math.min(i, 48))); s.textContent = c; p.append(s); });
}
// Shows the current page with the one before it. Returns early when the same lines are already on screen, so
// a growing stream, a status change or a re-render never replays anything.
present(pages: Page[], cursor: number, how: PresentHow, name: string, animate: boolean) {
@@ -46,6 +56,8 @@ export class LineStage {
const prev = pages[cursor - 1], want = [prev, page].filter((p): p is Page => !!p);
const have = [...stage.querySelectorAll<HTMLElement>('.line:not(.leaving)')], keys = have.map(n => n.dataset.key);
const inkFor = (p: Page) => p === page && animate && p.message.role === 'companion' && !this.inked.has(p.key);
// A line first shown while she was saying it keeps growing under its key (her final, then her stored row).
for (const p of want) { const n = have.find(x => x.dataset.key === p.key && x.dataset.spoken); if (n) this.grow(n, p.text, animate); }
if (how === 'instant' || !animate) {
// Keyboard review and reduced motion: show the target lines as they are — no slide, no re-played ink.
if (keys.join() === want.map(p => p.key).join() && how !== 'instant') return;
+14 -5
View File
@@ -2,8 +2,8 @@ import type { Message } from '../domain/model';
// Reading logic carried over from the verified HEL-793/795 prototype (PR#8) and HEL-799 (approved 01a16810), moved
// onto the real message flow: replies are shown one or two lines at a time and flow on by themselves.
// A message as the stage sees it: stored messages plus, at most, the reply still being written or cut off.
export interface ShownMessage extends Message { live?: 'generating' | 'cancelled' }
// A message as the stage sees it: stored messages plus, at most, the reply still being written, spoken or cut off.
export interface ShownMessage extends Message { live?: 'generating' | 'speaking' | 'cancelled' }
export interface Turn { key: string; items: ShownMessage[] }
export interface Page { key: string; text: string; index: number; last: boolean; mi: number; message: ShownMessage }
export interface Metrics { width: number; font: number; compact: boolean }
@@ -42,16 +42,23 @@ export function chunks(text: string, { width, font, compact }: Metrics): string[
return out;
}
// The finished sentences at the start of a line (with their closing marks); '' while the first is still forming.
const SAID = /^[\s\S]*[。!?!?…][」』))”’"]*/u;
export const said = (line: string) => SAID.exec(line)?.[0] ?? '';
export function pagesOf(turn: Turn | undefined, metrics: Metrics): Page[] {
const list: Page[] = [];
if (!turn) return list;
turn.items.forEach((message, mi) => {
const parts = chunks(message.text, metrics);
const parts = chunks(message.text, metrics), shown = parts.slice();
// The newest chunk of a reply still being written is held back (it may still grow or merge with the next
// sentence), so a line never changes or re-animates after it has appeared.
const held = message.live === 'generating' && parts.length ? 1 : 0;
if (message.live === 'generating') shown.pop();
// Her reply while she speaks it: the newest chunk shows the sentences she has already finished. It may still
// take the next short sentence in (it only ever grows, see LineStage.grow); the words still forming wait.
else if (message.live === 'speaking' && shown.length) { const done = said(shown.pop()!); if (done) shown.push(done); }
// Keys follow the turn and position, not the message id, so a streamed reply keeps its lines when it is stored.
for (let index = 0; index < parts.length - held; index++) list.push({ key: `${turn.key}:${mi}:${index}`, text: parts[index], index, last: index === parts.length - 1, mi, message });
shown.forEach((text, index) => list.push({ key: `${turn.key}:${mi}:${index}`, text, index, last: index === parts.length - 1, mi, message }));
});
return list;
}
@@ -75,6 +82,8 @@ export class ReadingClock {
const dt = this.last ? Math.min(STALL, Math.max(0, now - this.last)) : 0; this.last = now;
if (!page) return false;
if (page.key !== this.key) { this.key = page.key; this.dwell = 0; this.total = dwellFor(page); }
// A spoken line that took in her next sentence gets the extra time it now needs (never less).
else this.total = Math.max(this.total, dwellFor(page));
if (running && this.dwell < this.total) this.dwell = Math.min(this.total, this.dwell + dt);
return running && this.dwell >= this.total;
}