"use client"; import { useCallback, useEffect, useMemo, useRef, useState, type Dispatch, type SetStateAction, } from "react"; import { IconArrowRight, IconMessageCircle, IconMessageDots, IconSend, IconTrash, IconX, } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Textarea } from "@kandev/ui/textarea"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { PRStatusChip } from "@/components/github/pr-status-chip"; import { PRMergedBanner } from "./chat/chat-input-area"; import { type ChatInputContainerHandle } from "./chat/chat-input-container"; import { useChatPanelState } from "./chat/use-chat-panel-state"; import { useAppStore } from "@/components/state-provider"; import { useNextWorkflowStep } from "@/hooks/domains/kanban/use-plan-actions"; import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut"; import { usePendingDiffCommentsByFile } from "@/hooks/domains/comments/use-diff-comments"; import { useCommentsStore } from "@/lib/state/slices/comments/comments-store"; import { useFileEditors } from "@/hooks/use-file-editors"; import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint"; import { getShortcut, isUnboundShortcut } from "@/lib/keyboard/shortcut-overrides"; import { formatShortcut } from "@/lib/keyboard/utils"; import type { KeyboardShortcut } from "@/lib/keyboard/constants"; import type { DiffComment } from "@/lib/diff/types"; import { PassthroughTerminal } from "./passthrough-terminal"; import { PassthroughComposerPanel, useSendPassthroughMessage } from "./passthrough-chat-composer"; function isEditableElement(element: Element | null) { if (element instanceof HTMLElement && element.closest(".xterm")) return false; return ( element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement || (element instanceof HTMLElement && element.isContentEditable) ); } function usePassthroughComposerShortcut({ focusShortcut, setComposerOpen, }: { focusShortcut: KeyboardShortcut; setComposerOpen: Dispatch>; }) { useKeyboardShortcut( focusShortcut, useCallback(() => { const activeElement = document.activeElement; setComposerOpen((open) => { if (open) return false; if (isEditableElement(activeElement)) return open; return true; }); }, [setComposerOpen]), { capture: true, enabled: !isUnboundShortcut(focusShortcut) }, ); } /** * PassthroughToolbar wraps the PTY terminal with the kandev surface that the * full ACP `ChatStatusBar` + `ChatInputArea` provide for chat mode: PR status, * merge banner, "Move to next step" workflow advancement, a collapsible Chat * compose box, and a collapsible Comments panel showing pending review comments * (each editable inline with a clickable file reference). * * Default focus stays on the xterm terminal — both Chat and Comments are * collapsed behind toolbar buttons so the user can keep raw-terminal * interaction front-and-centre and only opt in when they want to compose a * follow-up or review what's about to be attached. Ctrl-C in the xterm * cancels the agent, so no dedicated Stop button is needed. */ export function PassthroughToolbar({ sessionId, taskId, }: { sessionId: string | null | undefined; taskId: string | null; }) { const [composerOpen, setComposerOpen] = useState(false); const [commentsOpenState, setCommentsOpen] = useState(false); const [isSending, setIsSending] = useState(false); const chatInputRef = useRef(null); const sessionState = useAppStore((state) => sessionId ? (state.taskSessions.items[sessionId]?.state ?? null) : null, ); const keyboardShortcuts = useAppStore((state) => state.userSettings?.keyboardShortcuts); const focusShortcut = getShortcut("FOCUS_PASSTHROUGH_INPUT", keyboardShortcuts); const isAgentBusy = sessionState === "RUNNING" || sessionState === "STARTING"; const nextStep = useNextWorkflowStep(taskId); const showProceed = !!nextStep.proceedStepName && !isAgentBusy; const { pendingComments, pendingCount } = usePendingPassthroughComments(sessionId); const { isMobile } = useResponsiveBreakpoint(); const { openFile } = useFileEditors(); const panelState = useChatPanelState({ sessionId: sessionId ?? null, onOpenFile: openFile }); // Derive open state: auto-close when pending comments are cleared const commentsOpen = commentsOpenState && pendingCount > 0; const sendPassthroughMessage = useSendPassthroughMessage({ taskId, sessionId, pendingComments, panelState, onSent: () => { setComposerOpen(false); setCommentsOpen(false); }, }); const handleSendMessage = useCallback( async (...args: Parameters) => { if (isSending) return; setIsSending(true); try { await sendPassthroughMessage(...args); } finally { setIsSending(false); } }, [isSending, sendPassthroughMessage], ); useEffect(() => { if (!composerOpen) return; requestAnimationFrame(() => chatInputRef.current?.focusInput()); }, [composerOpen]); usePassthroughComposerShortcut({ focusShortcut, setComposerOpen }); return (
{commentsOpen && pendingCount > 0 && ( handleSendMessage("")} /> )} {composerOpen && ( setComposerOpen(false)} panelState={panelState} taskId={taskId} isMoving={nextStep.isMoving} isSending={isSending} /> )} setComposerOpen((open) => !open)} commentsOpen={commentsOpen} onToggleComments={() => setCommentsOpen((open) => !open)} pendingCommentsCount={pendingCount} />
); } function flattenComments(byFile: Record): DiffComment[] { const all: DiffComment[] = []; for (const list of Object.values(byFile)) all.push(...list); return all; } function usePendingPassthroughComments(sessionId: string | null | undefined) { const pendingCommentsByFile = usePendingDiffCommentsByFile(sessionId ?? null); const pendingComments = useMemo( () => flattenComments(pendingCommentsByFile), [pendingCommentsByFile], ); return { pendingComments, pendingCount: pendingComments.length }; } function ChatToggleButton({ composerOpen, focusShortcut, onToggle, }: { composerOpen: boolean; focusShortcut: KeyboardShortcut; onToggle: () => void; }) { return ( {composerOpen ? (

Close the compose box (or press Esc inside it). The CLI agent terminal keeps focus.

) : (

Open a kandev-controlled compose box above the terminal to type a follow-up message. Press Enter to send, Shift+Enter for a newline.

The text is delivered straight to the CLI agent's stdin — pending review comments (if any) are prepended automatically.

)}
); } function PassthroughChatShortcutHint({ shortcut }: { shortcut: KeyboardShortcut }) { if (isUnboundShortcut(shortcut)) return null; return (

Shortcut: {formatShortcut(shortcut)} toggles this chat input.

); } function commentsToggleClassName(count: number, commentsOpen: boolean): string { // Vivid amber when there are pending comments so the user sees "something // to do" — washes back to plain outline once they're cleared / sent. if (count === 0) return "h-6 gap-1 px-2.5 text-xs cursor-pointer"; if (commentsOpen) return "h-6 gap-1 px-2.5 text-xs cursor-pointer"; return "h-6 gap-1 px-2.5 text-xs cursor-pointer border-amber-500/60 bg-amber-500/15 text-amber-700 hover:bg-amber-500/25 hover:text-amber-700 dark:text-amber-300 dark:hover:text-amber-200"; } function CommentsToggleButton({ commentsOpen, onToggle, pendingCommentsCount, }: { commentsOpen: boolean; onToggle: () => void; pendingCommentsCount: number; }) { const disabled = pendingCommentsCount === 0; return ( ); } function CommentsTooltipBody({ commentsOpen, count }: { commentsOpen: boolean; count: number }) { if (count === 0) { return (

No pending review comments.

Add a comment from the diff or file view, then come back here to review and send it.

); } if (commentsOpen) { return (

Hide the comment list. The comments stay queued and will still be sent next time you submit (here or from the chat box).

); } const plural = count === 1 ? "" : "s"; return (

{count} pending review comment{plural}. Click to expand the list — you can edit each comment, click the file path to jump to the source, or remove a comment with the trash icon.

Hit Send to agent inside the panel to deliver them to the CLI agent right away, or just open the chat box and type a follow-up — the comments will be prepended.

); } function CommentsPanel({ comments, openFile, onSend, }: { comments: DiffComment[]; openFile: (path: string) => void; onSend: () => Promise | void; }) { const [isSending, setIsSending] = useState(false); const handleSend = useCallback(async () => { if (isSending) return; setIsSending(true); try { await onSend(); } catch { // onSend already toasted the error; keep panel open so the user can retry. } finally { setIsSending(false); } }, [isSending, onSend]); const count = comments.length; const plural = count === 1 ? "" : "s"; return (
{count} review comment{plural} ready to send

Deliver these comments to the CLI agent's stdin as a single message — no need to open the chat box. The agent receives the same markdown that prepending to a typed message would produce.

{comments.map((comment) => ( ))}
); } function formatLineRange(comment: DiffComment): string { return comment.startLine === comment.endLine ? `${comment.startLine}` : `${comment.startLine}-${comment.endLine}`; } function CommentCard({ comment, openFile, }: { comment: DiffComment; openFile: (path: string) => void; }) { const updateComment = useCommentsStore((s) => s.updateComment); const removeComment = useCommentsStore((s) => s.removeComment); const lineRange = formatLineRange(comment); const handleOpenFile = useCallback(() => { openFile(comment.filePath); }, [openFile, comment.filePath]); return (
{comment.codeContent && (
          {comment.codeContent}
        
)}