Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 14x 28x 28x 28x 28x 28x 23x 28x 120x 125x 125x 28x 125x 28x 58x 28x 17x 28x 28x 5x 5x 5x 5x 28x 17x 17x 17x 34x | import StageCard from "@/components/chat/ExecutionPipeline/StageCard";
import StageRail from "@/components/chat/ExecutionPipeline/StageRail";
import {
formatDuration,
formatTokens,
} from "@/components/chat/ExecutionPipeline/nodeMeta";
import { Citation, GraphPlanStage, NodeExecution } from "@/objects/Model";
import { buildPipeline, runWallClock } from "@/util/pipelineUtils";
import {
ChevronDown,
ChevronRight,
Loader2,
Workflow,
} from "lucide-react";
import React, { useMemo, useRef, useState } from "react";
/**
* The execution view for one agent turn: a schematic stage rail over a list of
* stage detail cards.
*
* svc-ai announces the whole pipeline up-front (`graph_plan`), so the rail is
* complete from the first frame — stages light up as they run instead of
* appearing one by one. Stages that produced output get a detail card; the
* ones routing bypassed stay in the rail, greyed, so a reader can see what the
* graph chose not to do. Without a plan (older runs, non-graph executions)
* the rail degrades to the nodes actually observed.
*/
const ExecutionPipeline: React.FC<{
plan: GraphPlanStage[] | undefined;
executions: NodeExecution[];
/** True once the turn is over — unreached stages become skipped, not pending. */
finished: boolean;
citations?: Citation[];
/** Live status line (model resolution, persistence) shown under the rail. */
statusMessage?: string;
/** Detail cards start collapsed for history, expanded for a live run. */
defaultExpanded?: boolean;
}> = ({
plan,
executions,
finished,
citations = [],
statusMessage,
defaultExpanded = false,
}) => {
const [expanded, setExpanded] = useState(defaultExpanded);
const [openOverrides, setOpenOverrides] = useState<Record<string, boolean>>(
{},
);
const [selectedStageId, setSelectedStageId] = useState<string | null>(null);
const cardRefs = useRef<Record<string, HTMLDivElement | null>>({});
const stages = useMemo(
() => buildPipeline({ plan, executions, finished }),
[plan, executions, finished],
);
if (stages.length === 0) return null;
// The stage in flight — or stopped for a person, which is where the
// reader's attention belongs until they answer.
const runningStage =
stages.find((s) => s.state === "running" || s.state === "awaiting") ??
null;
const detailStages = stages.filter((s) => s.attempts.length > 0);
const skippedStages = stages.filter((s) => s.state === "skipped");
const completedCount = stages.filter(
(s) => s.state === "done" || s.state === "failed",
).length;
// Tokens add up; time is the run's wall clock, so branches that ran side
// by side count once.
const totals = {
tokens: executions.reduce(
(acc, node) =>
acc + (node.tokens?.input ?? 0) + (node.tokens?.output ?? 0),
0,
),
durationMs: runWallClock(stages),
};
/** A stage is expanded while it streams, or whenever a reader opens it. */
const isOpen = (stageId: string, isRunning: boolean) =>
openOverrides[stageId] ?? isRunning;
const toggleStage = (stageId: string, isRunning: boolean) =>
setOpenOverrides((prev) => ({
...prev,
[stageId]: !isOpen(stageId, isRunning),
}));
const selectStage = (stageId: string) => {
setSelectedStageId(stageId);
setExpanded(true);
setOpenOverrides((prev) => ({ ...prev, [stageId]: true }));
// The card may be mounting in the same tick as the expand above.
requestAnimationFrame(() =>
cardRefs.current[stageId]?.scrollIntoView?.({
block: "nearest",
behavior: "smooth",
}),
);
};
return (
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800/60 overflow-hidden">
{/* Header — pipeline identity and run totals */}
<div className="flex items-center gap-2 px-2.5 py-1.5 border-b border-gray-100 dark:border-gray-700/70 bg-gray-50/80 dark:bg-gray-900/40">
<Workflow className="w-3.5 h-3.5 text-gray-400 dark:text-gray-500 shrink-0" />
<span className="text-[10px] font-semibold uppercase tracking-[0.12em] text-gray-500 dark:text-gray-400">
Execution Pipeline
</span>
<span className="flex-1" />
<span className="text-[10px] text-gray-400 dark:text-gray-500 tabular-nums">
{completedCount}/{stages.length} stages
{totals.tokens > 0 && ` · ${formatTokens(totals.tokens)} tok`}
{totals.durationMs > 0 && ` · ${formatDuration(totals.durationMs)}`}
</span>
{detailStages.length > 0 && (
<button
type="button"
onClick={() => setExpanded((v) => !v)}
aria-expanded={expanded}
aria-label={expanded ? "Hide stage details" : "Show stage details"}
className="flex items-center gap-0.5 text-[10px] font-medium text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
>
{detailStages.length} detail{detailStages.length === 1 ? "" : "s"}
{expanded ? (
<ChevronDown className="w-3 h-3" />
) : (
<ChevronRight className="w-3 h-3" />
)}
</button>
)}
</div>
<StageRail
stages={stages}
selectedStageId={selectedStageId}
onSelectStage={selectStage}
focusStageId={runningStage?.id ?? null}
/>
{/* Stage detail cards */}
{expanded && detailStages.length > 0 && (
<div className="px-2.5 pb-2 space-y-1.5 border-t border-gray-100 dark:border-gray-700/70 pt-2">
{detailStages.map((stage) => {
const index = stages.indexOf(stage);
const running =
stage.state === "running" || stage.state === "awaiting";
return (
<StageCard
key={stage.id}
stage={stage}
index={index}
citations={running ? citations : []}
open={isOpen(stage.id, running)}
onToggle={() => toggleStage(stage.id, running)}
registerRef={(el) => {
cardRefs.current[stage.id] = el;
}}
/>
);
})}
{skippedStages.length > 0 && (
<p className="text-[10px] text-gray-400 dark:text-gray-500 pt-0.5">
Not run: {skippedStages.length} stage
{skippedStages.length === 1 ? "" : "s"} bypassed by routing
</p>
)}
</div>
)}
{/* Live status — model resolution before the graph, persistence after */}
{!finished && statusMessage && (
<div className="flex items-center gap-1.5 px-2.5 py-1.5 border-t border-gray-100 dark:border-gray-700/70 text-[11px] text-gray-500 dark:text-gray-400">
<Loader2 className="w-3 h-3 animate-spin shrink-0" />
<span className="truncate">{statusMessage}</span>
</div>
)}
</div>
);
};
export default ExecutionPipeline;
|