"use client"; import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react"; import { IconCheck, IconClock, IconEdit, IconRobot, IconUser, IconX } from "@tabler/icons-react"; import { toast } from "sonner"; import { Button } from "@kandev/ui"; import { Textarea } from "@kandev/ui/textarea"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { cn } from "@/lib/utils"; import { QueueEntryNotFoundError } from "@/lib/api/domains/queue-api"; import type { QueuedMessage } from "@/lib/state/slices/session/types"; /** Strip internal ... blocks from display text. */ function stripSystemTags(text: string): string { return text.replace(/[\s\S]*?<\/kandev-system>/g, "").trim(); } /** Imperative handle for the ghost row, used by chat input "edit last queued" affordance. */ export type QueuedGhostMessageHandle = { startEdit: () => void; }; type SenderKind = "user" | "agent" | "system"; function senderKindOf(entry: QueuedMessage): SenderKind { if (!entry.queued_by) return "system"; // Inter-task messages dispatched via dispatchTaskMessage hardcode // queued_by="agent"; that's the only signal needed. if (entry.queued_by === "agent") return "agent"; return "user"; } function senderLabel(entry: QueuedMessage): string { const kind = senderKindOf(entry); if (kind === "agent") { const title = entry.metadata?.sender_task_title; return typeof title === "string" && title.length > 0 ? `From ${title}` : "From agent"; } if (kind === "system") return "System"; return "You"; } type SenderChipProps = { entry: QueuedMessage }; function SenderChip({ entry }: SenderChipProps) { const kind = senderKindOf(entry); const Icon = kind === "agent" ? IconRobot : IconUser; return ( {senderLabel(entry)} ); } type EditViewProps = { value: string; saving: boolean; onChange: (v: string) => void; onSave: () => void; onCancel: () => void; textareaRef: React.RefObject; }; function EditView({ value, saving, onChange, onSave, onCancel, textareaRef }: EditViewProps) { const onKeyDown = (event: React.KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); onCancel(); } else if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) { event.preventDefault(); onSave(); } }; return (