import { useEffect, useState } from 'react'; import { tr } from '../lib/i18n'; import { cancelQueued, planEdit, planReject, queuedWhen, stepsDone, usePlan, useQueue, type Plan, type QueuedMessage } from '../lib/plan'; import { approvePlan, pushToast, startPlanGoal, useStore } from '../lib/store'; import { plainFailureText } from '../lib/plain-errors'; import { Btn, Icon, IconBtn } from './ui'; const STEP_WORDS = { done: 'done', running: 'working on it', failed: 'failed', skipped: 'skipped', pending: 'waiting' } as const; /** 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. */ export function PlanSteps({ plan }: { plan: Plan }) { return
    {plan.steps.map((s, i) =>
  1. {s.text}{s.state === 'done' && s.summary ? {s.summary} : s.detail ? {s.detail} : null} ({tr(STEP_WORDS[s.state])})
  2. )}
; } /** The words after "Plan": how many steps, or how far it has come. */ function planCount(plan: Plan): string { if (plan.state === 'proposed' || plan.state === 'rejected') return tr('{0} steps', [plan.steps.length]); if (plan.state === 'paused') return tr('Paused ยท {0} of {1} done', [stepsDone(plan), plan.steps.length]); return tr('{0} of {1} done', [stepsDone(plan), plan.steps.length]); } /** * 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 * person decides. Once it runs, the same card shows each step's state; a paused plan offers Resume. */ export function PlanCard({ chatId, planId }: { chatId: string | null | undefined; planId: string }) { const plan = usePlan(chatId); const streaming = useStore(s => s.streaming); const [editing, setEditing] = useState(false), [draft, setDraft] = useState([]), [busy, setBusy] = useState(false); useEffect(() => { setEditing(false); }, [chatId, plan?.id]); if (!chatId || !plan || plan.id !== planId) return null; const run = async (fn: () => Promise) => { setBusy(true); try { await fn(); } catch (e) { pushToast('error', tr("The plan was not changed"), plainFailureText(e instanceof Error ? e.message : String(e)).message); } finally { setBusy(false); } }; const proposed = plan.state === 'proposed'; if (editing) return
{tr("Edit the plan")}
{draft.map((text, i) =>
setDraft(d => d.map((t, j) => j === i ? e.target.value : t))}/> setDraft(d => d.filter((_, j) => j !== i))}/>
)} setDraft(d => [...d, ''])}>{tr("Add a step")}
t.trim())} onClick={() => void run(async () => { await planEdit(chatId, plan, draft.map(t => t.trim()).filter(Boolean)); setEditing(false); })}>{tr("Save plan")} setEditing(false)}>{tr("Cancel")}
; return
{tr("Plan")}{planCount(plan)}
{plan.caution ?

{tr(plan.caution)}

: null} {proposed ?

{tr("Nothing runs until you approve.")}

: null} {proposed ?
void run(() => approvePlan(chatId, plan.id))}>{tr("Approve")} { setDraft(plan.steps.map(s => s.text)); setEditing(true); }}>{tr("Edit")} void run(() => planReject(chatId, plan.id))}>{tr("Reject")} void run(() => startPlanGoal(plan.id))}>{tr("Start as goal")}
: null} {plan.state === 'paused' ?
void run(() => startPlanGoal(plan.id))}>{tr("Resume")}
: null}
; } /** The same steps in the sidebar, so they stay in view while the goal runs. */ export function PlanPanel({ chatId }: { chatId: string | null | undefined }) { const plan = usePlan(chatId); if (!plan) return ; return ; } /** 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. */ export function QueuedMessages({ chatId }: { chatId: string | null | undefined }) { const items = useQueue(chatId), plan = usePlan(chatId); if (!chatId || !items.length) return null; return <>{items.map((q: QueuedMessage, i) => { const when = queuedWhen(q, i, plan); return
{q.text}
{tr(when.id, when.args)}
; })}; }