| 31 | * - Handle Y/N approval responses |
| 32 | */ |
| 33 | export function useTaskSubmit({ |
| 34 | sendToExtension, |
| 35 | runTask, |
| 36 | seenMessageIds, |
| 37 | firstTextMessageSkipped, |
| 38 | }: UseTaskSubmitOptions): UseTaskSubmitReturn { |
| 39 | const { |
| 40 | pendingAsk, |
| 41 | hasStartedTask, |
| 42 | isComplete, |
| 43 | addMessage, |
| 44 | setPendingAsk, |
| 45 | setHasStartedTask, |
| 46 | setLoading, |
| 47 | setComplete, |
| 48 | setError, |
| 49 | } = useCLIStore() |
| 50 | |
| 51 | const { setShowCustomInput, setIsTransitioningToCustomInput } = useUIStateStore() |
| 52 | |
| 53 | /** |
| 54 | * Handle user text submission (from input or followup question) |
| 55 | */ |
| 56 | const handleSubmit = useCallback( |
| 57 | async (text: string) => { |
| 58 | if (!sendToExtension || !text.trim()) { |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | const trimmedText = text.trim() |
| 63 | |
| 64 | if (trimmedText === "__CUSTOM__") { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | // Check for CLI global action commands (e.g., /new) |
| 69 | if (trimmedText.startsWith("/")) { |
| 70 | const commandMatch = trimmedText.match(/^\/(\w+)(?:\s|$)/) |
| 71 | |
| 72 | if (commandMatch && commandMatch[1]) { |
| 73 | const globalCommand = getGlobalCommand(commandMatch[1]) |
| 74 | |
| 75 | if (globalCommand?.action === "clearTask") { |
| 76 | // Reset CLI state and send clearTask to extension. |
| 77 | useCLIStore.getState().reset() |
| 78 | |
| 79 | // Reset component-level refs to avoid stale message tracking. |
| 80 | seenMessageIds.current.clear() |
| 81 | firstTextMessageSkipped.current = false |
| 82 | sendToExtension({ type: "clearTask" }) |
| 83 | |
| 84 | // Re-request state, commands and modes since reset() cleared them. |
| 85 | sendToExtension({ type: "requestCommands" }) |
| 86 | sendToExtension({ type: "requestModes" }) |
| 87 | return |
| 88 | } |
| 89 | } |
| 90 | } |