({
inputValue,
isAssistantResponding,
}: Props)
| 13 | } |
| 14 | |
| 15 | export function usePromptSuggestion({ |
| 16 | inputValue, |
| 17 | isAssistantResponding, |
| 18 | }: Props): { |
| 19 | suggestion: string | null |
| 20 | markAccepted: () => void |
| 21 | markShown: () => void |
| 22 | logOutcomeAtSubmission: ( |
| 23 | finalInput: string, |
| 24 | opts?: { skipReset: boolean }, |
| 25 | ) => void |
| 26 | } { |
| 27 | const promptSuggestion = useAppState(s => s.promptSuggestion) |
| 28 | const setAppState = useSetAppState() |
| 29 | const isTerminalFocused = useTerminalFocus() |
| 30 | const { |
| 31 | text: suggestionText, |
| 32 | promptId, |
| 33 | shownAt, |
| 34 | acceptedAt, |
| 35 | generationRequestId, |
| 36 | } = promptSuggestion |
| 37 | |
| 38 | const suggestion = |
| 39 | isAssistantResponding || inputValue.length > 0 ? null : suggestionText |
| 40 | |
| 41 | const isValidSuggestion = suggestionText && shownAt > 0 |
| 42 | |
| 43 | // Track engagement depth for telemetry |
| 44 | const firstKeystrokeAt = useRef<number>(0) |
| 45 | const wasFocusedWhenShown = useRef<boolean>(true) |
| 46 | const prevShownAt = useRef<number>(0) |
| 47 | |
| 48 | // Capture focus state when a new suggestion appears (shownAt changes) |
| 49 | if (shownAt > 0 && shownAt !== prevShownAt.current) { |
| 50 | prevShownAt.current = shownAt |
| 51 | wasFocusedWhenShown.current = isTerminalFocused |
| 52 | firstKeystrokeAt.current = 0 |
| 53 | } else if (shownAt === 0) { |
| 54 | prevShownAt.current = 0 |
| 55 | } |
| 56 | |
| 57 | // Record first keystroke while suggestion is visible |
| 58 | if ( |
| 59 | inputValue.length > 0 && |
| 60 | firstKeystrokeAt.current === 0 && |
| 61 | isValidSuggestion |
| 62 | ) { |
| 63 | firstKeystrokeAt.current = Date.now() |
| 64 | } |
| 65 | |
| 66 | const resetSuggestion = useCallback(() => { |
| 67 | abortSpeculation(setAppState) |
| 68 | |
| 69 | setAppState(prev => ({ |
| 70 | ...prev, |
| 71 | promptSuggestion: { |
| 72 | text: null, |
no test coverage detected