HEL-822: land HEL-821 A1+A2 source onto PR#4 base (db571ba)
- 13 modified / 8 added / 1 deleted per CHANGES.json (author package 76bc120b) - docs/requirements v1.10 + prototypes/hel-799 + PROVENANCE from bundle, verified byte-identical to63799f0- AGENTS.md + scripts/check_requirements.py restored from governance commit63799f0Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
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' }
|
||||
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 }
|
||||
|
||||
export const graphemes = (text: string): string[] => typeof Intl.Segmenter === 'function' ? [...new Intl.Segmenter('zh', { granularity: 'grapheme' }).segment(text)].map(s => s.segment) : Array.from(text);
|
||||
|
||||
// A turn starts with your message (or with something she says on her own). Server turn ids keep a reply with the
|
||||
// message it answers; older local records without ids fall back to their order.
|
||||
export function groupTurns(messages: ShownMessage[]): Turn[] {
|
||||
const turns: Turn[] = [];
|
||||
for (const m of messages) {
|
||||
const cur = turns[turns.length - 1], head = cur?.items[0];
|
||||
const fresh = !cur || m.role === 'user' || !!m.proactive || (!!m.turnId && !!head?.turnId && m.turnId !== head.turnId);
|
||||
if (fresh || !cur) turns.push({ key: m.turnId || m.id, items: [m] }); else cur.items.push(m);
|
||||
}
|
||||
return turns;
|
||||
}
|
||||
|
||||
export function chunks(text: string, { width, font, compact }: Metrics): string[] {
|
||||
const perLine = Math.floor(Math.max(220, Math.min(width, 900) - 24) / font), max = Math.max(14, Math.min(60, Math.floor(perLine * (compact ? 1.6 : 2))));
|
||||
const out: string[] = []; let line = '', n = 0;
|
||||
for (const ch of graphemes(text)) {
|
||||
if (ch === '\n') { if (line.trim()) { out.push(line); line = ''; n = 0; } continue; }
|
||||
// Keep closing punctuation with its sentence; never produce an empty line.
|
||||
if (!line && out.length && /[。!?!?,、;:,;:」』))”]/u.test(ch)) { out[out.length - 1] += ch; continue; }
|
||||
line += ch; n++;
|
||||
// Short sentences travel together (as in the approved still); a line ends at a sentence once it has some weight.
|
||||
if (/[。!?!?]/u.test(ch) && n >= Math.min(22, max * .45)) { out.push(line); line = ''; n = 0; }
|
||||
else if (n >= max) {
|
||||
// Long text without punctuation still breaks, so nothing ever overflows the stage.
|
||||
const cut = Math.max(line.lastIndexOf(','), line.lastIndexOf('、'), line.lastIndexOf(';'));
|
||||
if (cut > max * .55) { out.push(line.slice(0, cut + 1)); line = line.slice(cut + 1); n = graphemes(line).length; } else { out.push(line); line = ''; n = 0; }
|
||||
}
|
||||
}
|
||||
if (line.trim()) out.push(line);
|
||||
return out;
|
||||
}
|
||||
|
||||
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);
|
||||
// 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;
|
||||
// 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 });
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
// Each line stays on screen in proportion to its length (about 2.4–8 s for her, shorter for yours).
|
||||
export function dwellFor(page: Page) {
|
||||
const n = graphemes(page.text).length;
|
||||
return page.message.role === 'user' ? Math.min(3000, Math.max(1400, 900 + n * 60)) : Math.min(8000, Math.max(2400, 1500 + n * 150));
|
||||
}
|
||||
|
||||
// Reading position as a character offset inside one message, so a re-chunk (resize, keyboard) keeps your place.
|
||||
export interface Anchor { mi: number; offset: number }
|
||||
export function anchorOf(pages: Page[], cursor: number): Anchor | null {
|
||||
const page = pages[cursor];
|
||||
if (!page) return null;
|
||||
let offset = 0;
|
||||
for (const p of pages) { if (p === page) break; if (p.mi === page.mi) offset += p.text.length; }
|
||||
return { mi: page.mi, offset };
|
||||
}
|
||||
export function cursorAt(pages: Page[], anchor: Anchor | null): number {
|
||||
if (!anchor) return 0;
|
||||
let used = 0, best = -1;
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
if (pages[i].mi < anchor.mi) { best = i; continue; }
|
||||
if (pages[i].mi > anchor.mi) break;
|
||||
best = i;
|
||||
if (used + pages[i].text.length > anchor.offset) return i;
|
||||
used += pages[i].text.length;
|
||||
}
|
||||
return Math.max(0, best);
|
||||
}
|
||||
Reference in New Issue
Block a user