import { useState } from "react"; import { AlertTriangle, Gauge, ListChecks, LoaderCircle, RotateCcw, X } from "lucide-react"; import { Button } from "@tutti-os/ui-system"; import { TaskIcon } from "@tutti-os/ui-system/icons"; import { TuttiModePlanPanel, TuttiPlanIssuePanel, type TuttiModePlanAssignmentCatalog, type TuttiModePlanPanelLabels, type TuttiModePlanPanelViewModel, type TuttiModePlanTaskAssignmentDraft, type TuttiModePlanTaskAssignmentDrafts, type TuttiPlanIssuePanelLabels, type TuttiPlanIssueSnapshot, type TuttiPlanIssueTaskAction } from "../../workspaceWorkflow"; import { cn } from "../../app/renderer/lib/utils"; import { AgentComposerDisclosureCard } from "./AgentComposerDisclosureCard"; import styles from "./AgentGUIChrome.styles"; import { TuttiBudgetPopover, type TuttiBudgetPopoverLabels } from "./composer/TuttiBudgetPopover"; import { projectTuttiPreferencePreview } from "./composer/tuttiIntensityPreview"; export type TuttiWorkflowDockPhase = | { kind: "review"; effect: number; speed: number; preferencesDiverged: boolean; panel: TuttiModePlanPanelViewModel; submitting: boolean; } | { kind: "materializing"; title: string; } | { issue: TuttiPlanIssueSnapshot; kind: "execution"; } | { kind: "error"; message: string; retryable: boolean; } | { kind: "reviewFailure"; message: string; auditId?: string; }; export interface TuttiWorkflowDockLabels { cancel: string; collapse: string; errorTitle: string; expand: string; issueDone(done: string, total: string): string; issueFailed(count: string): string; issuePendingAcceptance(count: string): string; issueRunning(count: string): string; materializingHint: string; materializingTitle: string; retry: string; switchToSelfReview: string; switchingToSelfReview: string; selfReviewEnabled: string; selfReviewFailed: string; reviewHint: string; reviewHintReplan: string; reviewTitle: string; } interface TuttiWorkflowDockDisclosureState { expanded: boolean; reviewPanelId: string | null; } function countIssueTasks( issue: TuttiPlanIssueSnapshot, status: string ): number { return issue.tasks.filter((task) => task.status === status).length; } function issueSummary( labels: TuttiWorkflowDockLabels, issue: TuttiPlanIssueSnapshot ): string { const running = countIssueTasks(issue, "running"); const pendingAcceptance = countIssueTasks(issue, "pending_acceptance"); const failed = countIssueTasks(issue, "failed"); const done = countIssueTasks(issue, "completed"); const parts: string[] = []; if (running > 0) parts.push(labels.issueRunning(String(running))); if (pendingAcceptance > 0) { parts.push(labels.issuePendingAcceptance(String(pendingAcceptance))); } if (failed > 0) parts.push(labels.issueFailed(String(failed))); parts.push(labels.issueDone(String(done), String(issue.tasks.length))); return parts.join(" · "); } /** * Single composer-anchored projection for the active Tutti workflow. * * The disclosure shell stays mounted while its phase moves from review to * materialization and execution. Only one current panel is rendered, so the * conversation timeline never competes with a second plan or task surface. */ export function TuttiWorkflowDock({ assignmentCatalog, assignmentDrafts, preferencePopoverLabels, labels, onAssignmentDraftChange, onCancelReview, onTaskAction, onEffectChange, onSpeedChange, onOpenIssue, onOpenTask, onRetry, onSwitchToSelfReview, phase, planPanelLabels, planIssuePanelLabels }: { assignmentCatalog: TuttiModePlanAssignmentCatalog; assignmentDrafts: TuttiModePlanTaskAssignmentDrafts; preferencePopoverLabels: TuttiBudgetPopoverLabels; labels: TuttiWorkflowDockLabels; onAssignmentDraftChange( taskId: string, patch: TuttiModePlanTaskAssignmentDraft ): void; onCancelReview(): void; onTaskAction?: ( taskId: string, action: TuttiPlanIssueTaskAction ) => Promise; onEffectChange(value: number): void; onSpeedChange(value: number): void; onOpenIssue?: () => void; onOpenTask?: (taskId: string) => void | Promise; onRetry(): void; onSwitchToSelfReview?: () => Promise; phase: TuttiWorkflowDockPhase; planPanelLabels: TuttiModePlanPanelLabels; planIssuePanelLabels: TuttiPlanIssuePanelLabels; }): React.JSX.Element { const review = phase.kind === "review" ? phase : null; const execution = phase.kind === "execution" ? phase : null; const failure = phase.kind === "error" ? phase : null; const reviewFailure = phase.kind === "reviewFailure" ? phase : null; const [selfReviewState, setSelfReviewState] = useState< "idle" | "switching" | "enabled" | "failed" >("idle"); const reviewPanelId = review?.panel.id ?? null; const [disclosure, setDisclosure] = useState(() => ({ expanded: reviewPanelId !== null, reviewPanelId })); // Echo preferences just dragged until canonical composer state catches up // (review preference updates travel through an async daemon command), // so the banner and the open popover never display diverging values. The // echo clears during render (same adjustment pattern as the disclosure // above) once the canonical value matches. const [echoedEffect, setEchoedEffect] = useState(null); const [echoedSpeed, setEchoedSpeed] = useState(null); if (echoedEffect !== null && review?.effect === echoedEffect) { setEchoedEffect(null); } if (echoedSpeed !== null && review?.speed === echoedSpeed) { setEchoedSpeed(null); } const reviewDisplayEffect = review !== null ? (echoedEffect ?? review.effect) : null; const reviewDisplaySpeed = review !== null ? (echoedSpeed ?? review.speed) : null; const handleEffectChange = (value: number): void => { setEchoedEffect(value); onEffectChange(value); }; const handleSpeedChange = (value: number): void => { setEchoedSpeed(value); onSpeedChange(value); }; // A newly actionable checkpoint starts open once. Recording its stable panel // identity prevents ordinary snapshot updates from overriding a user's // explicit collapse, while phase handoffs retain the current disclosure. if (reviewPanelId !== null && reviewPanelId !== disclosure.reviewPanelId) { setDisclosure({ expanded: true, reviewPanelId }); } const setExpanded = (expanded: boolean): void => { setDisclosure((current) => current.expanded === expanded ? current : { ...current, expanded } ); }; const title = review !== null ? labels.reviewTitle : phase.kind === "materializing" ? labels.materializingTitle : execution !== null ? execution.issue.title : labels.errorTitle; const summary = review !== null ? `${review.panel.title} · ${ review.preferencesDiverged ? labels.reviewHintReplan : labels.reviewHint }` : phase.kind === "materializing" ? `${phase.title} · ${labels.materializingHint}` : execution !== null ? issueSummary(labels, execution.issue) : (failure?.message ?? (reviewFailure ? `${reviewFailure.message}${reviewFailure.auditId ? ` · ${reviewFailure.auditId}` : ""}` : "")); const icon = review !== null ? ( ) : phase.kind === "materializing" ? ( ) : execution !== null ? ( countIssueTasks(execution.issue, "running") > 0 ? ( ) : ( ) ) : ( ); const reviewTier = reviewDisplayEffect !== null && reviewDisplaySpeed !== null ? projectTuttiPreferencePreview(reviewDisplayEffect, reviewDisplaySpeed) .effectTier : null; const actions = review !== null ? ( <> ) : reviewFailure ? ( ) : phase.kind === "error" && phase.retryable ? ( ) : null; return ( {review !== null ? ( ) : phase.kind === "materializing" ? (
{labels.materializingHint}
) : execution !== null ? ( ) : (
{failure?.message ?? reviewFailure?.message} {reviewFailure?.auditId ? ( {reviewFailure.auditId} ) : null} {selfReviewState === "failed" ? ( {labels.selfReviewFailed} ) : null}
)}
); }