forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePromptSuggestion.ts
More file actions
177 lines (162 loc) · 5.19 KB
/
Copy pathusePromptSuggestion.ts
File metadata and controls
177 lines (162 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { useCallback, useRef } from 'react'
import { useTerminalFocus } from '../ink/hooks/use-terminal-focus.js'
import {
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
logEvent,
} from '../services/analytics/index.js'
import { abortSpeculation } from '../services/PromptSuggestion/speculation.js'
import { useAppState, useSetAppState } from '../state/AppState.js'
type Props = {
inputValue: string
isAssistantResponding: boolean
}
export function usePromptSuggestion({
inputValue,
isAssistantResponding,
}: Props): {
suggestion: string | null
markAccepted: () => void
markShown: () => void
logOutcomeAtSubmission: (
finalInput: string,
opts?: { skipReset: boolean },
) => void
} {
const promptSuggestion = useAppState(s => s.promptSuggestion)
const setAppState = useSetAppState()
const isTerminalFocused = useTerminalFocus()
const {
text: suggestionText,
promptId,
shownAt,
acceptedAt,
generationRequestId,
} = promptSuggestion
const suggestion =
isAssistantResponding || inputValue.length > 0 ? null : suggestionText
const isValidSuggestion = suggestionText && shownAt > 0
// Track engagement depth for telemetry
const firstKeystrokeAt = useRef<number>(0)
const wasFocusedWhenShown = useRef<boolean>(true)
const prevShownAt = useRef<number>(0)
// Capture focus state when a new suggestion appears (shownAt changes)
if (shownAt > 0 && shownAt !== prevShownAt.current) {
prevShownAt.current = shownAt
wasFocusedWhenShown.current = isTerminalFocused
firstKeystrokeAt.current = 0
} else if (shownAt === 0) {
prevShownAt.current = 0
}
// Record first keystroke while suggestion is visible
if (
inputValue.length > 0 &&
firstKeystrokeAt.current === 0 &&
isValidSuggestion
) {
firstKeystrokeAt.current = Date.now()
}
const resetSuggestion = useCallback(() => {
abortSpeculation(setAppState)
setAppState(prev => ({
...prev,
promptSuggestion: {
text: null,
promptId: null,
shownAt: 0,
acceptedAt: 0,
generationRequestId: null,
},
}))
}, [setAppState])
const markAccepted = useCallback(() => {
if (!isValidSuggestion) return
setAppState(prev => ({
...prev,
promptSuggestion: {
...prev.promptSuggestion,
acceptedAt: Date.now(),
},
}))
}, [isValidSuggestion, setAppState])
const markShown = useCallback(() => {
// Check shownAt inside setAppState callback to avoid depending on it
// (depending on shownAt causes infinite loop when this callback is called)
setAppState(prev => {
// Only mark shown if not already shown and suggestion exists
if (prev.promptSuggestion.shownAt !== 0 || !prev.promptSuggestion.text) {
return prev
}
return {
...prev,
promptSuggestion: {
...prev.promptSuggestion,
shownAt: Date.now(),
},
}
})
}, [setAppState])
const logOutcomeAtSubmission = useCallback(
(finalInput: string, opts?: { skipReset: boolean }) => {
if (!isValidSuggestion) return
// Determine if accepted: either Tab was pressed (acceptedAt set) OR
// final input matches suggestion (empty Enter case)
const tabWasPressed = acceptedAt > shownAt
const wasAccepted = tabWasPressed || finalInput === suggestionText
const timeMs = wasAccepted ? acceptedAt || Date.now() : Date.now()
logEvent('tengu_prompt_suggestion', {
source:
'cli' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
outcome: (wasAccepted
? 'accepted'
: 'ignored') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
prompt_id:
promptId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
...(generationRequestId && {
generationRequestId:
generationRequestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
}),
...(wasAccepted && {
acceptMethod: (tabWasPressed
? 'tab'
: 'enter') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
}),
...(wasAccepted && {
timeToAcceptMs: timeMs - shownAt,
}),
...(!wasAccepted && {
timeToIgnoreMs: timeMs - shownAt,
}),
...(firstKeystrokeAt.current > 0 && {
timeToFirstKeystrokeMs: firstKeystrokeAt.current - shownAt,
}),
wasFocusedWhenShown: wasFocusedWhenShown.current,
similarity:
Math.round(
(finalInput.length / (suggestionText?.length || 1)) * 100,
) / 100,
...(process.env.USER_TYPE === 'ant' && {
suggestion:
suggestionText as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
userInput:
finalInput as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
}),
})
if (!opts?.skipReset) resetSuggestion()
},
[
isValidSuggestion,
acceptedAt,
shownAt,
suggestionText,
promptId,
generationRequestId,
resetSuggestion,
],
)
return {
suggestion,
markAccepted,
markShown,
logOutcomeAtSubmission,
}
}