Skip to content

Commit b40121d

Browse files
committed
refactor(cli): simplify chat input submission routing
Consolidate Build buttons and initial prompt handling through routeUserPrompt for consistent queue and command routing.
1 parent f405b43 commit b40121d

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

cli/src/chat.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,32 @@ export const Chat = ({
538538
setAgentMode,
539539
separatorWidth,
540540
initialPrompt,
541-
sendMessageRef,
542-
isChainInProgressRef,
543-
streamMessageIdRef,
544-
isStreaming,
545-
addToQueue,
541+
onSubmitPrompt: (content, mode) => {
542+
return routeUserPrompt({
543+
abortControllerRef,
544+
agentMode: mode,
545+
inputRef,
546+
inputValue: content,
547+
isChainInProgressRef,
548+
isStreaming,
549+
logoutMutation,
550+
streamMessageIdRef,
551+
addToQueue,
552+
clearMessages,
553+
clearQueue,
554+
handleCtrlC,
555+
saveToHistory,
556+
scrollToLatest,
557+
sendMessage,
558+
setCanProcessQueue,
559+
setInputFocused,
560+
setInputValue,
561+
setIsAuthenticated,
562+
setMessages,
563+
setUser,
564+
stopStreaming,
565+
})
566+
},
546567
})
547568

548569
const {

cli/src/hooks/use-chat-input.ts

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useCallback, useEffect, useRef } from 'react'
22
import stringWidth from 'string-width'
33

44
import type { InputValue } from '../state/chat-store'
5-
import type { SendMessageFn } from '../types/contracts/send-message'
65
import type { AgentMode } from '../utils/constants'
76

87
interface UseChatInputOptions {
@@ -12,11 +11,7 @@ interface UseChatInputOptions {
1211
setAgentMode: (mode: AgentMode) => void
1312
separatorWidth: number
1413
initialPrompt: string | null
15-
sendMessageRef: React.MutableRefObject<SendMessageFn | undefined>
16-
isChainInProgressRef: React.MutableRefObject<boolean>
17-
streamMessageIdRef: React.MutableRefObject<string | null>
18-
isStreaming: boolean
19-
addToQueue: (message: string) => void
14+
onSubmitPrompt: (content: string, mode: AgentMode) => void | Promise<unknown>
2015
}
2116

2217
const BUILD_IT_TEXT = 'Build it!'
@@ -28,11 +23,7 @@ export const useChatInput = ({
2823
setAgentMode,
2924
separatorWidth,
3025
initialPrompt,
31-
sendMessageRef,
32-
isChainInProgressRef,
33-
streamMessageIdRef,
34-
isStreaming,
35-
addToQueue,
26+
onSubmitPrompt,
3627
}: UseChatInputOptions) => {
3728
const hasAutoSubmittedRef = useRef(false)
3829

@@ -62,19 +53,10 @@ export const useChatInput = ({
6253
lastEditDueToNav: true,
6354
})
6455
setTimeout(() => {
65-
// Check if streaming or chain in progress - if so, add to queue instead of sending
66-
if (
67-
isStreaming ||
68-
streamMessageIdRef.current ||
69-
isChainInProgressRef.current
70-
) {
71-
addToQueue(BUILD_IT_TEXT)
72-
} else if (sendMessageRef.current) {
73-
sendMessageRef.current({ content: BUILD_IT_TEXT, agentMode: 'DEFAULT' })
74-
}
56+
onSubmitPrompt(BUILD_IT_TEXT, 'DEFAULT')
7557
setInputValue({ text: '', cursorPosition: 0, lastEditDueToNav: false })
7658
}, 0)
77-
}, [setAgentMode, setInputValue, sendMessageRef, isStreaming, streamMessageIdRef, isChainInProgressRef, addToQueue])
59+
}, [setAgentMode, setInputValue, onSubmitPrompt])
7860

7961
const handleBuildMax = useCallback(() => {
8062
setAgentMode('MAX')
@@ -84,41 +66,23 @@ export const useChatInput = ({
8466
lastEditDueToNav: true,
8567
})
8668
setTimeout(() => {
87-
// Check if streaming or chain in progress - if so, add to queue instead of sending
88-
if (
89-
isStreaming ||
90-
streamMessageIdRef.current ||
91-
isChainInProgressRef.current
92-
) {
93-
addToQueue('Build it!')
94-
} else if (sendMessageRef.current) {
95-
sendMessageRef.current({ content: 'Build it!', agentMode: 'MAX' })
96-
}
69+
onSubmitPrompt('Build it!', 'MAX')
9770
setInputValue({ text: '', cursorPosition: 0, lastEditDueToNav: false })
9871
}, 0)
99-
}, [setAgentMode, setInputValue, sendMessageRef, isStreaming, streamMessageIdRef, isChainInProgressRef, addToQueue])
72+
}, [setAgentMode, setInputValue, onSubmitPrompt])
10073

10174
useEffect(() => {
10275
if (initialPrompt && !hasAutoSubmittedRef.current) {
10376
hasAutoSubmittedRef.current = true
10477

10578
const timeout = setTimeout(() => {
106-
// Check if streaming or chain in progress - if so, add to queue instead of sending
107-
if (
108-
isStreaming ||
109-
streamMessageIdRef.current ||
110-
isChainInProgressRef.current
111-
) {
112-
addToQueue(initialPrompt)
113-
} else if (sendMessageRef.current) {
114-
sendMessageRef.current({ content: initialPrompt, agentMode })
115-
}
79+
onSubmitPrompt(initialPrompt, agentMode)
11680
}, 100)
11781

11882
return () => clearTimeout(timeout)
11983
}
12084
return undefined
121-
}, [initialPrompt, agentMode, sendMessageRef, isStreaming, streamMessageIdRef, isChainInProgressRef, addToQueue])
85+
}, [initialPrompt, agentMode, onSubmitPrompt])
12286

12387
return {
12488
inputWidth,

0 commit comments

Comments
 (0)