okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-020-int into f-020-rel3d9c21509

main
4.3 KiB89 linesraw
1import { useSyncExternalStore } from 'react';
2import { tr } from './i18n';
3
4/**
5 * The one transient line under the newest message while the model works (frames from the host: `status`, see notes-split-agent3.md).
6 * Only the LATEST frame of a turn is kept; it is removed on `idle`, on `done` and `error`, and when the stream ends, so the line can never
7 * show something that stopped being true.
8 */
9export interface StatusFrame {
10  seq: number;
11  phase: string;
12  code: string;
13  text: string;
14  args?: Record<string, unknown>;
15  tokens?: number;
16  elapsedMs: number;
17  /** When this page received the frame; elapsed time keeps counting from it between frames. */
18  receivedAt: number;
19}
20
21const frames = new Map<string, StatusFrame>();
22const listeners = new Set<() => void>();
23const emit = () => listeners.forEach(fn => fn());
24
25/** Take a status frame for a chat. An older frame (lower `seq`) never replaces a newer one. Returns whether the line changed. */
26export function applyStatusFrame(sessionId: string, raw: Record<string, unknown>, now = Date.now()): boolean {
27  const seq = Number(raw.seq);
28  const phase = String(raw.phase ?? '');
29  const current = frames.get(sessionId);
30  if (Number.isFinite(seq) && current && seq < current.seq) return false;
31  if (phase === 'idle' || !phase) { const had = frames.delete(sessionId); if (had) emit(); return had; }
32  frames.set(sessionId, {
33    seq: Number.isFinite(seq) ? seq : (current?.seq ?? 0) + 1,
34    phase, code: String(raw.code ?? phase), text: String(raw.text ?? ''),
35    args: raw.args && typeof raw.args === 'object' ? raw.args as Record<string, unknown> : undefined,
36    tokens: typeof raw.tokens === 'number' ? raw.tokens : undefined,
37    elapsedMs: Number(raw.elapsedMs) || 0, receivedAt: now,
38  });
39  emit();
40  return true;
41}
42
43export function clearStatus(sessionId: string) { if (frames.delete(sessionId)) emit(); }
44export const statusOf = (sessionId: string | null | undefined): StatusFrame | null => (sessionId ? frames.get(sessionId) ?? null : null);
45
46export function useLiveStatus(sessionId: string | null | undefined): StatusFrame | null {
47  return useSyncExternalStore(
48    fn => { listeners.add(fn); return () => { listeners.delete(fn); }; },
49    () => statusOf(sessionId),
50    () => null,
51  );
52}
53
54/** The playful words shown while the model is thinking, in the order they rotate. Translated through the catalog. */
55export const PLAYFUL_VERBS = [
56  'Thinking…', 'Pondering…', 'Cooking up an answer…', 'Mulling it over…', 'Connecting the dots…', 'Chasing an idea…',
57  'Sketching it out…', 'Weighing the options…', 'Reading between the lines…', 'Brewing a reply…', 'Putting it together…', 'Following a hunch…',
58] as const;
59
60/** Phases where the model is working things out and nothing concrete can be named: the playful words rotate here and only here. */
61export function isPlayful(frame: StatusFrame): boolean {
62  return frame.phase === 'thinking' || (frame.phase === 'waiting' && frame.code === 'model-working');
63}
64
65/** The words for a frame. `rotation` counts how many times the playful word has advanced (the page advances it every few seconds). */
66export function statusWords(frame: StatusFrame, rotation = 0): string {
67  if (isPlayful(frame)) return tr(PLAYFUL_VERBS[((rotation % PLAYFUL_VERBS.length) + PLAYFUL_VERBS.length) % PLAYFUL_VERBS.length]);
68  const a = frame.args ?? {};
69  const target = typeof a.target === 'string' && a.target ? a.target : '';
70  switch (frame.code) {
71    case 'starting': return tr('Starting…');
72    case 'reading-prompt': return tr('Reading your message…');
73    case 'answering': return tr('Writing the answer…');
74    case 'preparing-tool': case 'tool': {
75      const verb = typeof a.verb === 'string' && a.verb ? tr(a.verb) : '';
76      return verb ? `${verb}${target ? ` · ${target}` : ''}…` : tr(frame.text);
77    }
78    case 'waiting-approval': return tr('Waiting for your answer…');
79    case 'helpers': return tr('Helpers are working ({0})…', [Number(a.n) || 1]);
80    case 'goal-step': return tr('Working on the goal…');
81    default: return tr(frame.text);
82  }
83}
84
85/** "14s", "1m 04s": how long the turn has been going. */
86export function elapsedText(startedAt: number, now: number): string {
87  const total = Math.max(0, Math.round((now - startedAt) / 1000));
88  return total < 60 ? `${total}s` : `${Math.floor(total / 60)}m ${String(total % 60).padStart(2, '0')}s`;
89}