"use client"; import { useMemo, useState, useSyncExternalStore } from "react"; import { useTranslation } from "react-i18next"; import { DndContext, closestCenter, type DragEndEvent, PointerSensor, useSensor, useSensors, } from "@dnd-kit/core"; import { SortableContext, horizontalListSortingStrategy, arrayMove, useSortable, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { IconGripVertical, IconPlus, IconTrash, IconChevronRight, IconRosetteNumber1, IconAlertTriangle, } from "@tabler/icons-react"; import { StepCapabilityIcons } from "@/components/step-capability-icons"; import { ScrollArea, ScrollBar } from "@kandev/ui/scroll-area"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip"; import type { WorkflowStep } from "@/lib/types/http"; import type { WorkflowReplayCycleDiagnostic } from "@/lib/workflows/replay-cycle-analysis"; import { cn } from "@/lib/utils"; import { StepConfigPanel } from "./workflow-pipeline-editor-panels"; import { isWorkflowStepDirty } from "./workflow-dirty-state"; import { WorkflowCycleDiagnostic } from "./workflow-cycle-diagnostic"; type WorkflowPipelineEditorProps = { steps: WorkflowStep[]; savedSteps?: WorkflowStep[]; diagnostics?: WorkflowReplayCycleDiagnostic[]; onUpdateStep: (stepId: string, updates: Partial) => void; onAddStep: () => void; onRemoveStep: (stepId: string) => void; onReorderSteps: (steps: WorkflowStep[]) => void; readOnly?: boolean; onSessionConfigResolutionPendingChange?: (pending: boolean) => void; }; // --- Helpers --- function getTransitionType(step: WorkflowStep): string { const action = step.events?.on_turn_complete?.find((a) => ["move_to_next", "move_to_previous", "move_to_step"].includes(a.type), ); return action?.type ?? "none"; } // The transition `type`s are wire values; only the connector caption is copy, // so the mapping yields a catalog key that the caller resolves at render. function transitionLabelKey(step: WorkflowStep): string { const type = getTransitionType(step); if (type === "move_to_next") return "workflows:transitionAuto"; if (type === "move_to_previous") return "workflows:transitionBack"; if (type === "move_to_step") return "workflows:transitionGoto"; return "workflows:transitionManual"; } // --- Pipeline Node --- type PipelineNodeProps = { step: WorkflowStep; isDirty: boolean; isReplayCycleAffected: boolean; isSelected: boolean; onSelect: () => void; onRemove: () => void; readOnly?: boolean; }; function PipelineNode({ step, isDirty, isReplayCycleAffected, isSelected, onSelect, onRemove, readOnly = false, }: PipelineNodeProps) { const { t } = useTranslation(); const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: step.id, }); const style = { transform: CSS.Transform.toString(transform), transition }; return (
{step.is_start_step && (
{t("workflows:startStep")}
)}
{step.name}
{t("workflows:transitionManual")} } /> {isReplayCycleAffected && ( )}
{!readOnly && ( )}
); } // --- Connector --- function PipelineConnector({ labelKey }: { labelKey: string }) { const { t } = useTranslation(); return (
{t(labelKey)}
); } // --- Pipeline Area --- type PipelineAreaProps = { steps: WorkflowStep[]; savedStepsById: ReadonlyMap; affectedStepIds: Set; selectedStepId: string | null; onSelectStep: (stepId: string) => void; onRemoveStep: (stepId: string) => void; onAddStep: () => void; readOnly: boolean; }; function PipelineArea({ steps, savedStepsById, affectedStepIds, selectedStepId, onSelectStep, onRemoveStep, onAddStep, readOnly, }: PipelineAreaProps) { return (
{steps.map((step, index) => (
{index > 0 && } onSelectStep(step.id)} onRemove={() => onRemoveStep(step.id)} readOnly={readOnly} />
))} {steps.length > 0 && (
)}
); } function affectedStepIds(diagnostics: WorkflowReplayCycleDiagnostic[]): Set { return new Set(diagnostics.flatMap((diagnostic) => diagnostic.affectedStepIds)); } function WorkflowCycleAlerts({ diagnostics }: { diagnostics: WorkflowReplayCycleDiagnostic[] }) { if (diagnostics.length === 0) return null; return (
{diagnostics.map((diagnostic) => ( ))}
); } const EMPTY_DIAGNOSTICS: WorkflowReplayCycleDiagnostic[] = []; // --- Main Pipeline Editor --- export function WorkflowPipelineEditor({ steps, savedSteps = [], diagnostics = EMPTY_DIAGNOSTICS, onUpdateStep, onAddStep, onRemoveStep, onReorderSteps, readOnly = false, onSessionConfigResolutionPendingChange, }: WorkflowPipelineEditorProps) { const [selectedStepId, setSelectedStepId] = useState(null); const [prevStepCount, setPrevStepCount] = useState(steps.length); if (steps.length !== prevStepCount) { if (steps.length > prevStepCount && steps.length > 0) setSelectedStepId(steps[steps.length - 1].id); setPrevStepCount(steps.length); } const stepItems = useMemo(() => steps.map((step) => step.id), [steps]); const savedStepsById = useMemo( () => new Map(savedSteps.map((step) => [step.id, step])), [savedSteps], ); const affectedIds = useMemo(() => affectedStepIds(diagnostics), [diagnostics]); const isMounted = useSyncExternalStore( () => () => {}, () => true, () => false, ); const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 8 } })); const handleDragEnd = (event: DragEndEvent) => { if (readOnly) return; const { active, over } = event; if (!over || active.id === over.id) return; const oldIndex = steps.findIndex((step) => step.id === active.id); const newIndex = steps.findIndex((step) => step.id === over.id); if (oldIndex === -1 || newIndex === -1) return; onReorderSteps( arrayMove(steps, oldIndex, newIndex).map((step, index) => ({ ...step, position: index })), ); }; const handleSelectStep = (stepId: string) => setSelectedStepId((prev) => (prev === stepId ? null : stepId)); const handleRemoveStep = (stepId: string) => { onRemoveStep(stepId); if (selectedStepId === stepId) setSelectedStepId(null); }; const selectedStep = steps.find((s) => s.id === selectedStepId); const pipelineArea = ( ); return (
{isMounted ? ( {pipelineArea} ) : ( pipelineArea )} {selectedStep && ( onUpdateStep(selectedStep.id, updates)} onRemove={() => handleRemoveStep(selectedStep.id)} readOnly={readOnly} onSessionConfigResolutionPendingChange={onSessionConfigResolutionPendingChange} /> )}
); }