okami.mom/strata

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

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

main
3.0 KiB67 linesraw
1import { useEffect, useSyncExternalStore } from 'react';
2import { api } from './client';
3
4/** A chat's goal as the host reports it (notes-split-agent3.md): the bar above the input draws it; the host owns it. */
5export interface Goal {
6  objective: string;
7  phase: 'active' | 'paused' | 'blocked' | 'complete';
8  round: number;
9  maxRounds: number;
10  blocked?: string;
11  /** While a plan runs: which step the goal is on (notes-split-agent6.md). */
12  step?: { index: number; total: number; text: string };
13}
14
15const goals = new Map<string, Goal>();
16const listeners = new Set<() => void>();
17const emit = () => listeners.forEach(fn => fn());
18
19/** A goal object from a frame, an event or a route answer; anything that is not one means "no goal". */
20export function readGoal(raw: unknown): Goal | null {
21  if (!raw || typeof raw !== 'object') return null;
22  const g = raw as Record<string, unknown>;
23  const phase = g.phase;
24  if (typeof g.objective !== 'string' || !g.objective || !['active', 'paused', 'blocked', 'complete'].includes(String(phase))) return null;
25  return {
26    objective: g.objective, phase: phase as Goal['phase'],
27    round: Number(g.round) || 0, maxRounds: Number(g.maxRounds) || 0,
28    ...(typeof g.blocked === 'string' && g.blocked ? { blocked: g.blocked } : {}),
29    ...readStep(g.step),
30  };
31}
32
33function readStep(raw: unknown): { step?: Goal['step'] } {
34  if (!raw || typeof raw !== 'object') return {};
35  const s = raw as Record<string, unknown>;
36  const index = Number(s.index), total = Number(s.total);
37  return index > 0 && total >= index ? { step: { index, total, text: typeof s.text === 'string' ? s.text : '' } } : {};
38}
39
40export function applyGoalFrame(chatId: string, raw: unknown) {
41  const goal = readGoal(raw);
42  const before = goals.get(chatId);
43  if (!goal) { if (goals.delete(chatId)) emit(); return; }
44  if (before && JSON.stringify(before) === JSON.stringify(goal)) return;
45  goals.set(chatId, goal);
46  emit();
47}
48
49export const goalOf = (chatId: string | null | undefined): Goal | null => (chatId ? goals.get(chatId) ?? null : null);
50
51export function useGoal(chatId: string | null | undefined): Goal | null {
52  const goal = useSyncExternalStore(fn => { listeners.add(fn); return () => { listeners.delete(fn); }; }, () => goalOf(chatId), () => null);
53  // Page load or a switch to another chat: ask the host once.
54  useEffect(() => { if (chatId) void loadGoal(chatId); }, [chatId]);
55  return goal;
56}
57
58export async function loadGoal(chatId: string) {
59  try { applyGoalFrame(chatId, (await api.get<{ goal: unknown }>(`/api/goal?chatId=${encodeURIComponent(chatId)}`)).goal); }
60  catch { /* an older host has no goal route: no bar */ }
61}
62
63/** Pause, change or remove the goal. The host answers at once with the goal as it now is. */
64export async function goalAction(chatId: string, action: 'pause' | 'edit' | 'clear', text?: string) {
65  const result = await api.post<{ goal: unknown }>(`/api/goal/${action}`, { chatId, ...(text !== undefined ? { text } : {}) });
66  applyGoalFrame(chatId, result.goal);
67}