|
| 1 | +import { getTaskLabel, type Task } from "@repo/shared"; |
| 2 | +import { |
| 3 | + VscodeIcon, |
| 4 | + VscodeProgressRing, |
| 5 | +} from "@vscode-elements/react-elements"; |
| 6 | +import { useState } from "react"; |
| 7 | + |
| 8 | +import { useTasksApi } from "../hooks/useTasksApi"; |
| 9 | + |
| 10 | +interface TaskInputProps { |
| 11 | + taskId: string; |
| 12 | + task: Task; |
| 13 | + canPause: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +function getPlaceholder(task: Task): string { |
| 17 | + if (task.status === "error" || task.current_state?.state === "failed") { |
| 18 | + return task.current_state?.message || "Error occurred..."; |
| 19 | + } |
| 20 | + if (task.status === "paused") { |
| 21 | + return "Task paused"; |
| 22 | + } |
| 23 | + if (task.status === "pending" || task.status === "initializing") { |
| 24 | + return "Initializing..."; |
| 25 | + } |
| 26 | + if (task.current_state?.state === "working") { |
| 27 | + return "Agent is working..."; |
| 28 | + } |
| 29 | + if (task.current_state?.state === "complete") { |
| 30 | + return "Task completed"; |
| 31 | + } |
| 32 | + return "Type a message to the agent..."; |
| 33 | +} |
| 34 | + |
| 35 | +function isInputEnabled(task: Task): boolean { |
| 36 | + const state = task.current_state?.state; |
| 37 | + return state === "idle" || state === "complete" || task.status === "paused"; |
| 38 | +} |
| 39 | + |
| 40 | +export function TaskInput({ taskId, task, canPause }: TaskInputProps) { |
| 41 | + const api = useTasksApi(); |
| 42 | + const [message, setMessage] = useState(""); |
| 43 | + const [isPausing, setIsPausing] = useState(false); |
| 44 | + const [isSending, setIsSending] = useState(false); |
| 45 | + |
| 46 | + const inputEnabled = isInputEnabled(task); |
| 47 | + const showPauseButton = task.current_state?.state === "working" && canPause; |
| 48 | + const placeholder = getPlaceholder(task); |
| 49 | + |
| 50 | + const handleSend = () => { |
| 51 | + if (!message.trim() || !inputEnabled || isSending) return; |
| 52 | + setIsSending(true); |
| 53 | + api.sendTaskMessage(taskId, message.trim()); |
| 54 | + setMessage(""); |
| 55 | + setTimeout(() => setIsSending(false), 500); |
| 56 | + }; |
| 57 | + |
| 58 | + const handlePause = async () => { |
| 59 | + if (isPausing) return; |
| 60 | + setIsPausing(true); |
| 61 | + try { |
| 62 | + await api.pauseTask({ taskId: task.id, taskName: getTaskLabel(task) }); |
| 63 | + } catch { |
| 64 | + // Extension shows error notification |
| 65 | + } finally { |
| 66 | + setIsPausing(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 71 | + if (e.key === "Enter" && (e.metaKey || e.ctrlKey) && inputEnabled) { |
| 72 | + e.preventDefault(); |
| 73 | + void handleSend(); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className="task-input-container"> |
| 79 | + <textarea |
| 80 | + className="task-input" |
| 81 | + placeholder={placeholder} |
| 82 | + value={message} |
| 83 | + onChange={(e) => setMessage(e.target.value)} |
| 84 | + onKeyDown={handleKeyDown} |
| 85 | + disabled={!inputEnabled} |
| 86 | + /> |
| 87 | + <div className="task-input-button"> |
| 88 | + {showPauseButton ? ( |
| 89 | + isPausing ? ( |
| 90 | + <VscodeProgressRing className="task-input-spinner" /> |
| 91 | + ) : ( |
| 92 | + <VscodeIcon |
| 93 | + actionIcon |
| 94 | + name="debug-pause" |
| 95 | + label="Pause task" |
| 96 | + onClick={() => void handlePause()} |
| 97 | + /> |
| 98 | + ) |
| 99 | + ) : isSending ? ( |
| 100 | + <VscodeProgressRing className="task-input-spinner" /> |
| 101 | + ) : ( |
| 102 | + <VscodeIcon |
| 103 | + actionIcon |
| 104 | + name="send" |
| 105 | + label="Send message" |
| 106 | + onClick={ |
| 107 | + inputEnabled && message.trim() |
| 108 | + ? () => void handleSend() |
| 109 | + : undefined |
| 110 | + } |
| 111 | + className={!inputEnabled || !message.trim() ? "disabled" : ""} |
| 112 | + /> |
| 113 | + )} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
0 commit comments