okami.mom/strata

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

Niko1221Merge f-split-int (a3024ed10) into f-split-qa1: qa2's s3237-s3238 kept, my 21 strings renumbered s3260-s32808667a3dbf

main
6.6 KiB100 linesraw
1import { useEffect, useState } from 'react';
2import { tr } from '../lib/i18n';
3import { cancelQueued, planEdit, planReject, queuedWhen, stepsDone, usePlan, useQueue, type Plan, type QueuedMessage } from '../lib/plan';
4import { approvePlan, pushToast, startPlanGoal, useStore } from '../lib/store';
5import { plainFailureText } from '../lib/plain-errors';
6import { Btn, Icon, IconBtn } from './ui';
7
8const STEP_WORDS = { done: 'done', running: 'working on it', failed: 'failed', skipped: 'skipped', pending: 'waiting' } as const;
9
10/** The steps with their state: a number, a check once done, a soft glow on the one being worked on. Used in the chat and in the sidebar. */
11export function PlanSteps({ plan }: { plan: Plan }) {
12  return <ol className="plan__steps">
13    {plan.steps.map((s, i) => <li key={s.id} className="plan__step" data-state={s.state}>
14      <span className="plan__no" aria-hidden="true">{s.state === 'done' ? <Icon name="check" size={12}/> : s.state === 'failed' ? <Icon name="x" size={12}/> : i + 1}</span>
15      <span className="plan__text">{s.text}{s.state === 'done' && s.summary ? <small>{s.summary}</small> : s.detail ? <small>{s.detail}</small> : null}<span className="sr-only"> ({tr(STEP_WORDS[s.state])})</span></span>
16    </li>)}
17  </ol>;
18}
19
20/** The words after "Plan": how many steps, or how far it has come. */
21function planCount(plan: Plan): string {
22  if (plan.state === 'proposed' || plan.state === 'rejected') return tr('{0} steps', [plan.steps.length]);
23  if (plan.state === 'paused') return tr('Paused · {0} of {1} done', [stepsDone(plan), plan.steps.length]);
24  return tr('{0} of {1} done', [stepsDone(plan), plan.steps.length]);
25}
26
27/**
28 * The /plan the assistant proposed, in the chat in place of its closing text: Approve, Edit, Reject, or Start as goal. Nothing runs until the
29 * person decides. Once it runs, the same card shows each step's state; a paused plan offers Resume.
30 */
31export function PlanCard({ chatId, planId }: { chatId: string | null | undefined; planId: string }) {
32  const plan = usePlan(chatId);
33  const streaming = useStore(s => s.streaming);
34  const [editing, setEditing] = useState(false), [draft, setDraft] = useState<string[]>([]), [busy, setBusy] = useState(false);
35  useEffect(() => { setEditing(false); }, [chatId, plan?.id]);
36  if (!chatId || !plan || plan.id !== planId) return null;
37  const run = async (fn: () => Promise<unknown>) => {
38    setBusy(true);
39    try { await fn(); }
40    catch (e) { pushToast('error', tr("The plan was not changed"), plainFailureText(e instanceof Error ? e.message : String(e)).message); }
41    finally { setBusy(false); }
42  };
43  const proposed = plan.state === 'proposed';
44  if (editing) return <section className="plan" aria-label={tr("Edit the plan")}>
45    <div className="plan__head"><b>{tr("Edit the plan")}</b></div>
46    <div className="plan__edit">
47      {draft.map((text, i) => <div className="plan__row" key={i}>
48        <span className="plan__no" aria-hidden="true">{i + 1}</span>
49        <input className="st-input" value={text} aria-label={tr("Step {0}", [i + 1])} onChange={e => setDraft(d => d.map((t, j) => j === i ? e.target.value : t))}/>
50        <IconBtn name="x" size={15} title={tr("Remove this step")} disabled={draft.length < 2} onClick={() => setDraft(d => d.filter((_, j) => j !== i))}/>
51      </div>)}
52      <Btn size="sm" icon="plus" onClick={() => setDraft(d => [...d, ''])}>{tr("Add a step")}</Btn>
53    </div>
54    <div className="plan__acts">
55      <Btn size="sm" variant="primary" disabled={busy || !draft.some(t => t.trim())} onClick={() => void run(async () => { await planEdit(chatId, plan, draft.map(t => t.trim()).filter(Boolean)); setEditing(false); })}>{tr("Save plan")}</Btn>
56      <Btn size="sm" onClick={() => setEditing(false)}>{tr("Cancel")}</Btn>
57    </div>
58  </section>;
59  return <section className="plan" data-state={plan.state} aria-label={tr("Plan")}>
60    <div className="plan__head"><b>{tr("Plan")}</b><span>{planCount(plan)}</span></div>
61    <PlanSteps plan={plan}/>
62    {plan.caution ? <p className="plan__note" role="status">{tr(plan.caution)}</p> : null}
63    {proposed ? <p className="plan__note">{tr("Nothing runs until you approve.")}</p> : null}
64    {proposed ? <div className="plan__acts">
65      <Btn size="sm" variant="primary" icon="check" disabled={busy || streaming} onClick={() => void run(() => approvePlan(chatId, plan.id))}>{tr("Approve")}</Btn>
66      <Btn size="sm" icon="pencil" disabled={busy} onClick={() => { setDraft(plan.steps.map(s => s.text)); setEditing(true); }}>{tr("Edit")}</Btn>
67      <Btn size="sm" disabled={busy} onClick={() => void run(() => planReject(chatId, plan.id))}>{tr("Reject")}</Btn>
68      <span className="plan__sp"/>
69      <Btn size="sm" icon="bolt" disabled={busy || streaming} onClick={() => void run(() => startPlanGoal(plan.id))}>{tr("Start as goal")}</Btn>
70    </div> : null}
71    {plan.state === 'paused' ? <div className="plan__acts">
72      <Btn size="sm" variant="primary" icon="play" disabled={busy || streaming} onClick={() => void run(() => startPlanGoal(plan.id))}>{tr("Resume")}</Btn>
73    </div> : null}
74  </section>;
75}
76
77/** The same steps in the sidebar, so they stay in view while the goal runs. */
78export function PlanPanel({ chatId }: { chatId: string | null | undefined }) {
79  const plan = usePlan(chatId);
80  if (!plan) return <aside className="ts-panel plan-panel" id="plan-content" aria-label={tr("Plan")}><p className="ts-empty">{tr("There is no plan in this chat.")}</p></aside>;
81  return <aside className="ts-panel plan-panel" id="plan-content" aria-label={tr("Plan")}>
82    <div className="plan__head"><b>{tr("Plan")}</b><span>{planCount(plan)}</span></div>
83    {plan.title ? <p className="plan__goal">{plan.title}</p> : null}
84    <PlanSteps plan={plan}/>
85  </aside>;
86}
87
88/** Messages sent while the assistant works: shown in the chat as they are, with when they will go out, and a way to take them back. */
89export function QueuedMessages({ chatId }: { chatId: string | null | undefined }) {
90  const items = useQueue(chatId), plan = usePlan(chatId);
91  if (!chatId || !items.length) return null;
92  return <>{items.map((q: QueuedMessage, i) => {
93    const when = queuedWhen(q, i, plan);
94    return <div className="queued" key={q.id}>
95      <div className="queued__bubble">{q.text}</div>
96      <div className="queued__line"><span>{tr(when.id, when.args)}</span>
97        <button type="button" className="queued__x" title={tr("Cancel this message")} aria-label={tr("Cancel this message")} onClick={() => void cancelQueued(chatId, q.id).catch(e => pushToast('error', tr("The message was not cancelled"), plainFailureText(e instanceof Error ? e.message : String(e)).message))}><Icon name="x" size={13}/></button></div>
98    </div>;
99  })}</>;
100}