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
)}
;
}
/** 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
: 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