vzcode 2.28.0 → 2.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/index-CD0x02Rq.js +364 -0
- package/dist/index.html +1 -1
- package/dist/llm-streaming-server/chatOperations.js +38 -0
- package/dist/llm-streaming-server/llmStreaming.js +0 -1
- package/dist/server/aiChatHandler/index.js +44 -8
- package/package.json +34 -34
- package/src/client/App/index.tsx +38 -24
- package/src/client/VZCodeContext/types.ts +15 -3
- package/src/client/VZCodeContext/useVZCodeState.ts +36 -0
- package/src/client/featureFlags.ts +8 -0
- package/src/llm-streaming-server/chatOperations.ts +48 -0
- package/src/llm-streaming-server/llmStreaming.ts +3 -1
- package/src/llm-streaming-ui/components/ChatInput.tsx +49 -29
- package/src/llm-streaming-ui/components/useSpeechRecognition.ts +84 -60
- package/src/server/aiChatHandler/index.ts +77 -11
- package/src/types.ts +3 -0
- package/src/vite-env.d.ts +1 -0
- package/dist/assets/index-CTdYkAQU.js +0 -403
|
@@ -10,10 +10,50 @@ import {
|
|
|
10
10
|
ButtonGroup,
|
|
11
11
|
ToggleButton,
|
|
12
12
|
} from '../../client/bootstrap';
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
enableAskMode,
|
|
15
|
+
enableVoiceInput,
|
|
16
|
+
} from '../../client/featureFlags';
|
|
14
17
|
import { useSpeechRecognition } from './useSpeechRecognition';
|
|
15
18
|
import { MicSVG, MicOffSVG } from '../../client/Icons';
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Microphone toggle for voice typing. Purely presentational: the
|
|
22
|
+
* speech recognition state lives in `ChatInputComponent`, and is only
|
|
23
|
+
* ever instantiated when the user actually toggles the button.
|
|
24
|
+
*
|
|
25
|
+
* Rendered only when `enableVoiceInput` is set. See `featureFlags`.
|
|
26
|
+
*/
|
|
27
|
+
const VoiceInputButton = ({
|
|
28
|
+
isSpeaking,
|
|
29
|
+
onToggle,
|
|
30
|
+
}: {
|
|
31
|
+
isSpeaking: boolean;
|
|
32
|
+
onToggle: () => void;
|
|
33
|
+
}) => {
|
|
34
|
+
const label = isSpeaking
|
|
35
|
+
? 'Stop voice typing'
|
|
36
|
+
: 'Start voice typing';
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<Button
|
|
40
|
+
variant={isSpeaking ? 'danger' : 'outline-secondary'}
|
|
41
|
+
size="sm"
|
|
42
|
+
onClick={onToggle}
|
|
43
|
+
aria-label={label}
|
|
44
|
+
title={label}
|
|
45
|
+
style={{
|
|
46
|
+
borderColor: isSpeaking ? undefined : '#dee2e6',
|
|
47
|
+
borderRadius: '0.375rem',
|
|
48
|
+
borderWidth: '1px',
|
|
49
|
+
borderStyle: 'solid',
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
{isSpeaking ? <MicOffSVG /> : <MicSVG />}
|
|
53
|
+
</Button>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
17
57
|
interface ChatInputProps {
|
|
18
58
|
aiChatMessage: string;
|
|
19
59
|
setAIChatMessage: (message: string) => void;
|
|
@@ -39,7 +79,8 @@ const ChatInputComponent = ({
|
|
|
39
79
|
}: ChatInputProps) => {
|
|
40
80
|
const inputRef = useRef<HTMLTextAreaElement>(null);
|
|
41
81
|
|
|
42
|
-
//
|
|
82
|
+
// Speech recognition is created lazily on first toggle, so simply
|
|
83
|
+
// mounting this hook does not touch any browser media APIs.
|
|
43
84
|
const {
|
|
44
85
|
isSpeaking,
|
|
45
86
|
toggleSpeechRecognition,
|
|
@@ -186,33 +227,12 @@ const ChatInputComponent = ({
|
|
|
186
227
|
{aiChatMessage ? 'Press Enter to send' : ''}
|
|
187
228
|
</span>
|
|
188
229
|
<div style={{ display: 'flex', gap: '8px' }}>
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
isSpeaking
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
aria-label={
|
|
196
|
-
isSpeaking
|
|
197
|
-
? 'Stop voice typing'
|
|
198
|
-
: 'Start voice typing'
|
|
199
|
-
}
|
|
200
|
-
title={
|
|
201
|
-
isSpeaking
|
|
202
|
-
? 'Stop voice typing'
|
|
203
|
-
: 'Start voice typing'
|
|
204
|
-
}
|
|
205
|
-
style={{
|
|
206
|
-
borderColor: isSpeaking
|
|
207
|
-
? undefined
|
|
208
|
-
: '#dee2e6',
|
|
209
|
-
borderRadius: '0.375rem',
|
|
210
|
-
borderWidth: '1px',
|
|
211
|
-
borderStyle: 'solid',
|
|
212
|
-
}}
|
|
213
|
-
>
|
|
214
|
-
{isSpeaking ? <MicOffSVG /> : <MicSVG />}
|
|
215
|
-
</Button>
|
|
230
|
+
{enableVoiceInput && (
|
|
231
|
+
<VoiceInputButton
|
|
232
|
+
isSpeaking={isSpeaking}
|
|
233
|
+
onToggle={toggleSpeechRecognition}
|
|
234
|
+
/>
|
|
235
|
+
)}
|
|
216
236
|
<Button
|
|
217
237
|
variant={
|
|
218
238
|
aiChatMessage.trim()
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
useState,
|
|
3
3
|
useEffect,
|
|
4
4
|
useCallback,
|
|
5
|
+
useRef,
|
|
5
6
|
Dispatch,
|
|
6
7
|
SetStateAction,
|
|
7
8
|
} from 'react';
|
|
@@ -12,18 +13,41 @@ export interface UseSpeechRecognitionResult {
|
|
|
12
13
|
stopSpeaking: () => void;
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Speech-to-text input for the AI chat box.
|
|
18
|
+
*
|
|
19
|
+
* The underlying `SpeechRecognition` instance is created lazily, on the
|
|
20
|
+
* first call to `toggleSpeechRecognition`, rather than eagerly when the
|
|
21
|
+
* hook mounts. Eager construction can make some browsers show a
|
|
22
|
+
* microphone permission prompt as soon as the editor loads, before the
|
|
23
|
+
* user has asked for voice input.
|
|
24
|
+
*/
|
|
15
25
|
export const useSpeechRecognition = (
|
|
16
26
|
onTranscriptChange: Dispatch<SetStateAction<string>>,
|
|
17
27
|
): UseSpeechRecognitionResult => {
|
|
18
28
|
const [isSpeaking, setIsSpeaking] =
|
|
19
29
|
useState<boolean>(false);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
// `SpeechRecognition` is not part of the DOM lib, so the type
|
|
31
|
+
// reference is suppressed here, as with the constructor lookups below.
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
const recognitionRef = useRef<SpeechRecognition | null>(
|
|
34
|
+
null,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Keep the latest callback in a ref so the lazily-created
|
|
38
|
+
// recognition instance always calls the current handler, without
|
|
39
|
+
// having to tear down and recreate the instance.
|
|
40
|
+
const onTranscriptChangeRef = useRef(onTranscriptChange);
|
|
26
41
|
useEffect(() => {
|
|
42
|
+
onTranscriptChangeRef.current = onTranscriptChange;
|
|
43
|
+
}, [onTranscriptChange]);
|
|
44
|
+
|
|
45
|
+
// Create the SpeechRecognition instance on first use.
|
|
46
|
+
const getRecognition = useCallback(() => {
|
|
47
|
+
if (recognitionRef.current) {
|
|
48
|
+
return recognitionRef.current;
|
|
49
|
+
}
|
|
50
|
+
|
|
27
51
|
// Check if browser supports SpeechRecognition
|
|
28
52
|
const SpeechRecognition =
|
|
29
53
|
// @ts-ignore
|
|
@@ -31,56 +55,52 @@ export const useSpeechRecognition = (
|
|
|
31
55
|
// @ts-ignore
|
|
32
56
|
window.webkitSpeechRecognition;
|
|
33
57
|
|
|
34
|
-
if (SpeechRecognition) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
if (!SpeechRecognition) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const recognitionInstance = new SpeechRecognition();
|
|
63
|
+
recognitionInstance.continuous = true;
|
|
64
|
+
recognitionInstance.interimResults = true;
|
|
65
|
+
|
|
66
|
+
recognitionInstance.onresult = (event) => {
|
|
67
|
+
let finalText = '';
|
|
68
|
+
let interimText = '';
|
|
69
|
+
|
|
70
|
+
// Process all results to separate final and interim text
|
|
71
|
+
for (let i = 0; i < event.results.length; i++) {
|
|
72
|
+
const result = event.results[i];
|
|
73
|
+
if (result.isFinal) {
|
|
74
|
+
finalText += result[0].transcript;
|
|
75
|
+
} else {
|
|
76
|
+
interimText += result[0].transcript;
|
|
51
77
|
}
|
|
78
|
+
}
|
|
52
79
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
setRecognition(recognitionInstance);
|
|
76
|
-
|
|
77
|
-
return () => {
|
|
78
|
-
recognitionInstance.abort();
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}, [onTranscriptChange]);
|
|
80
|
+
// Combine final and interim text and update the prompt
|
|
81
|
+
onTranscriptChangeRef.current(
|
|
82
|
+
finalText + interimText,
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
recognitionInstance.onerror = (event) => {
|
|
87
|
+
console.error(
|
|
88
|
+
'Speech recognition error',
|
|
89
|
+
event.error,
|
|
90
|
+
);
|
|
91
|
+
setIsSpeaking(false);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
recognitionInstance.onend = () => {
|
|
95
|
+
setIsSpeaking(false);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
recognitionRef.current = recognitionInstance;
|
|
99
|
+
return recognitionInstance;
|
|
100
|
+
}, []);
|
|
82
101
|
|
|
83
102
|
const toggleSpeechRecognition = useCallback(() => {
|
|
103
|
+
const recognition = getRecognition();
|
|
84
104
|
if (!recognition) {
|
|
85
105
|
console.error(
|
|
86
106
|
'Speech recognition not supported in this browser',
|
|
@@ -92,21 +112,25 @@ export const useSpeechRecognition = (
|
|
|
92
112
|
recognition.stop();
|
|
93
113
|
setIsSpeaking(false);
|
|
94
114
|
} else {
|
|
95
|
-
// Reset final transcript when starting new recognition
|
|
96
|
-
setFinalTranscript('');
|
|
97
115
|
recognition.start();
|
|
98
116
|
setIsSpeaking(true);
|
|
99
117
|
}
|
|
100
|
-
}, [
|
|
118
|
+
}, [getRecognition, isSpeaking]);
|
|
101
119
|
|
|
102
120
|
const stopSpeaking = useCallback(() => {
|
|
103
|
-
if (
|
|
104
|
-
|
|
121
|
+
if (recognitionRef.current && isSpeaking) {
|
|
122
|
+
recognitionRef.current.stop();
|
|
105
123
|
setIsSpeaking(false);
|
|
106
|
-
// Reset final transcript when manually stopping
|
|
107
|
-
setFinalTranscript('');
|
|
108
124
|
}
|
|
109
|
-
}, [isSpeaking
|
|
125
|
+
}, [isSpeaking]);
|
|
126
|
+
|
|
127
|
+
// Release the microphone if the component unmounts while listening.
|
|
128
|
+
useEffect(
|
|
129
|
+
() => () => {
|
|
130
|
+
recognitionRef.current?.abort();
|
|
131
|
+
},
|
|
132
|
+
[],
|
|
133
|
+
);
|
|
110
134
|
|
|
111
135
|
return {
|
|
112
136
|
isSpeaking,
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
ensureChatExists,
|
|
5
5
|
addUserMessage,
|
|
6
6
|
setAIStatus,
|
|
7
|
+
setChatAIMetadata,
|
|
7
8
|
} from '../../llm-streaming-server/chatOperations.js';
|
|
8
9
|
import { createLLMFunction } from '../../llm-streaming-server/llmStreaming.js';
|
|
9
10
|
import { performAIEditing } from '../../llm-streaming-server/aiEditing.js';
|
|
@@ -23,15 +24,31 @@ export const handleAIChatMessage =
|
|
|
23
24
|
({
|
|
24
25
|
shareDBDoc,
|
|
25
26
|
onCreditDeduction,
|
|
27
|
+
onGenerationFinished,
|
|
26
28
|
model,
|
|
27
29
|
aiRequestOptions,
|
|
28
30
|
enableReasoningTokens,
|
|
31
|
+
baseCommitId,
|
|
32
|
+
escalationLevel,
|
|
29
33
|
}: {
|
|
30
34
|
shareDBDoc: ShareDBDoc<VizContent>;
|
|
31
35
|
onCreditDeduction?: any;
|
|
36
|
+
onGenerationFinished?: (result: {
|
|
37
|
+
success: boolean;
|
|
38
|
+
editResult?: any;
|
|
39
|
+
metrics?: any;
|
|
40
|
+
error?: any;
|
|
41
|
+
}) => Promise<void> | void;
|
|
32
42
|
model?: string;
|
|
33
43
|
aiRequestOptions?: any;
|
|
34
44
|
enableReasoningTokens?: boolean;
|
|
45
|
+
// Phase 3: explicit escalation base. Persisted on the chat so that
|
|
46
|
+
// repeated "Try Harder" clicks are idempotent and do not depend on
|
|
47
|
+
// the fragile `parent(currentCommit)` heuristic.
|
|
48
|
+
baseCommitId?: string;
|
|
49
|
+
// Explicit escalation level, persisted on the chat so it survives
|
|
50
|
+
// reloads and is shared across clients.
|
|
51
|
+
escalationLevel?: number;
|
|
35
52
|
}) =>
|
|
36
53
|
async (req: any, res: any) => {
|
|
37
54
|
const { content, chatId } = req.body;
|
|
@@ -60,6 +77,13 @@ export const handleAIChatMessage =
|
|
|
60
77
|
// Add user message to chat
|
|
61
78
|
addUserMessage(shareDBDoc, chatId, content);
|
|
62
79
|
|
|
80
|
+
// Persist explicit escalation metadata on the chat, so that
|
|
81
|
+
// retries are idempotent and the level survives reloads.
|
|
82
|
+
setChatAIMetadata(shareDBDoc, chatId, {
|
|
83
|
+
baseCommitId,
|
|
84
|
+
escalationLevel,
|
|
85
|
+
});
|
|
86
|
+
|
|
63
87
|
// Return success immediately - AI generation continues in background
|
|
64
88
|
res.status(200).json('success');
|
|
65
89
|
|
|
@@ -72,6 +96,7 @@ export const handleAIChatMessage =
|
|
|
72
96
|
aiRequestOptions,
|
|
73
97
|
enableReasoningTokens,
|
|
74
98
|
onCreditDeduction,
|
|
99
|
+
onGenerationFinished,
|
|
75
100
|
}).catch((error) => {
|
|
76
101
|
console.error(
|
|
77
102
|
'Background AI processing error:',
|
|
@@ -96,6 +121,7 @@ const processAIRequestAsync = async ({
|
|
|
96
121
|
aiRequestOptions,
|
|
97
122
|
enableReasoningTokens,
|
|
98
123
|
onCreditDeduction,
|
|
124
|
+
onGenerationFinished,
|
|
99
125
|
}: {
|
|
100
126
|
shareDBDoc: ShareDBDoc<VizContent>;
|
|
101
127
|
chatId: string;
|
|
@@ -104,6 +130,12 @@ const processAIRequestAsync = async ({
|
|
|
104
130
|
aiRequestOptions?: any;
|
|
105
131
|
enableReasoningTokens?: boolean;
|
|
106
132
|
onCreditDeduction?: any;
|
|
133
|
+
onGenerationFinished?: (result: {
|
|
134
|
+
success: boolean;
|
|
135
|
+
editResult?: any;
|
|
136
|
+
metrics?: any;
|
|
137
|
+
error?: any;
|
|
138
|
+
}) => Promise<void> | void;
|
|
107
139
|
}) => {
|
|
108
140
|
try {
|
|
109
141
|
// Create LLM function for streaming
|
|
@@ -128,23 +160,40 @@ const processAIRequestAsync = async ({
|
|
|
128
160
|
runCode,
|
|
129
161
|
});
|
|
130
162
|
|
|
131
|
-
//
|
|
163
|
+
// Billing is best-effort. It must NEVER prevent the edit from
|
|
164
|
+
// being committed. Any metadata failure is logged and swallowed.
|
|
165
|
+
let metrics: any = null;
|
|
132
166
|
if (onCreditDeduction && editResult.generationId) {
|
|
133
167
|
try {
|
|
134
|
-
await
|
|
135
|
-
|
|
136
|
-
apiKey
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
);
|
|
168
|
+
metrics = await getGenerationMetadata({
|
|
169
|
+
apiKey:
|
|
170
|
+
aiRequestOptions?.apiKey ||
|
|
171
|
+
process.env.VZCODE_EDIT_WITH_AI_API_KEY,
|
|
172
|
+
generationId: editResult.generationId,
|
|
173
|
+
});
|
|
174
|
+
await onCreditDeduction(metrics);
|
|
142
175
|
} catch (creditError) {
|
|
143
176
|
console.error(
|
|
144
|
-
'Credit deduction error:',
|
|
177
|
+
'Credit deduction error (edit will still be finalized):',
|
|
145
178
|
creditError,
|
|
146
179
|
);
|
|
147
|
-
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Always notify settlement on success, regardless of billing
|
|
184
|
+
// outcome, so the caller can commit the edit and release locks.
|
|
185
|
+
if (onGenerationFinished) {
|
|
186
|
+
try {
|
|
187
|
+
await onGenerationFinished({
|
|
188
|
+
success: true,
|
|
189
|
+
editResult,
|
|
190
|
+
metrics,
|
|
191
|
+
});
|
|
192
|
+
} catch (settleError) {
|
|
193
|
+
console.error(
|
|
194
|
+
'onGenerationFinished (success) error:',
|
|
195
|
+
settleError,
|
|
196
|
+
);
|
|
148
197
|
}
|
|
149
198
|
}
|
|
150
199
|
|
|
@@ -153,6 +202,23 @@ const processAIRequestAsync = async ({
|
|
|
153
202
|
} catch (error) {
|
|
154
203
|
// Set error status and add error message to chat
|
|
155
204
|
setAIStatus(shareDBDoc, chatId, 'error');
|
|
205
|
+
|
|
206
|
+
// Always notify settlement on failure so the caller can roll back
|
|
207
|
+
// the pre-restore snapshot and release locks.
|
|
208
|
+
if (onGenerationFinished) {
|
|
209
|
+
try {
|
|
210
|
+
await onGenerationFinished({
|
|
211
|
+
success: false,
|
|
212
|
+
error,
|
|
213
|
+
});
|
|
214
|
+
} catch (settleError) {
|
|
215
|
+
console.error(
|
|
216
|
+
'onGenerationFinished (failure) error:',
|
|
217
|
+
settleError,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
156
222
|
handleBackgroundError(shareDBDoc, chatId, error);
|
|
157
223
|
}
|
|
158
224
|
};
|
package/src/types.ts
CHANGED
|
@@ -262,6 +262,9 @@ export interface ExtendedVizChat extends VizChat {
|
|
|
262
262
|
currentStatus?: string;
|
|
263
263
|
isStreaming?: boolean;
|
|
264
264
|
model?: string; // The LLM model used for this chat
|
|
265
|
+
// Phase 3: explicit escalation metadata persisted on the chat.
|
|
266
|
+
baseCommitId?: string; // Commit the current AI attempt is applied on top of
|
|
267
|
+
escalationLevel?: number; // How many times "Try Harder" has been pressed
|
|
265
268
|
}
|
|
266
269
|
|
|
267
270
|
// Extended VizChatMessage with progressive rendering support
|