vzcode 1.51.0 → 1.52.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-DkCXnCvn.js → index-CoGL485l.js} +46 -46
- package/dist/assets/{index-BXyJ2hMf.css → index-L1AANcDx.css} +1 -1
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/src/client/CodeEditor/json1PresenceDisplay.ts +40 -7
- package/src/client/VZSidebar/AIChat/DiffView.scss +0 -26
- package/src/client/VZSidebar/AIChat/DiffView.tsx +1 -67
- package/src/client/VZSidebar/AIChat/Message.tsx +60 -8
- package/src/client/VZSidebar/AIChat/ThinkingScratchpad.tsx +1 -1
- package/src/client/VZSidebar/AIChat/index.tsx +124 -85
- package/src/client/VZSidebar/AIChat/styles.scss +27 -0
- package/src/client/VZSidebar/index.tsx +1 -1
- package/src/server/aiChatHandler/index.ts +3 -3
- package/src/server/aiChatHandler/llmStreaming.ts +13 -10
- package/src/server/generateAIResponse.ts +3 -0
- package/src/server/handleAICopilot.ts +3 -0
- package/src/server/index.ts +7 -7
|
@@ -13,6 +13,8 @@ import './styles.scss';
|
|
|
13
13
|
|
|
14
14
|
const defaultAIChatEndpoint = '/api/ai-chat/';
|
|
15
15
|
|
|
16
|
+
const DEBUG = false;
|
|
17
|
+
|
|
16
18
|
export const AIChat = () => {
|
|
17
19
|
const { aiChatMessage, setAIChatMessage } =
|
|
18
20
|
useContext(VZCodeContext);
|
|
@@ -54,110 +56,147 @@ export const AIChat = () => {
|
|
|
54
56
|
// Check if this is the first time opening the chat (no messages)
|
|
55
57
|
const isEmptyState = rawMessages.length === 0;
|
|
56
58
|
|
|
57
|
-
const handleSendMessage = useCallback(
|
|
58
|
-
|
|
59
|
+
const handleSendMessage = useCallback(
|
|
60
|
+
async (messageToSend?: string) => {
|
|
61
|
+
DEBUG &&
|
|
62
|
+
console.log(
|
|
63
|
+
'AIChat: handleSendMessage called with:',
|
|
64
|
+
messageToSend,
|
|
65
|
+
'aiChatMessage:',
|
|
66
|
+
aiChatMessage,
|
|
67
|
+
);
|
|
68
|
+
const messageContent = messageToSend || aiChatMessage;
|
|
69
|
+
if (
|
|
70
|
+
!messageContent ||
|
|
71
|
+
typeof messageContent !== 'string' ||
|
|
72
|
+
!messageContent.trim() ||
|
|
73
|
+
isLoading
|
|
74
|
+
)
|
|
75
|
+
return;
|
|
59
76
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
const currentPrompt = aiChatMessage.trim();
|
|
78
|
+
setAIChatMessage('');
|
|
79
|
+
setIsLoading(true);
|
|
80
|
+
setErrorMessage(null); // Clear any previous errors
|
|
64
81
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
// Call backend endpoint for AI response
|
|
83
|
+
// The server will handle all ShareDB operations including adding the user message
|
|
84
|
+
try {
|
|
85
|
+
const response = await fetch(aiChatEndpoint, {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
},
|
|
90
|
+
body: JSON.stringify({
|
|
91
|
+
...aiChatOptions,
|
|
92
|
+
vizId: aiChatOptions.vizId,
|
|
93
|
+
content: messageContent.trim(),
|
|
94
|
+
chatId: currentChatId,
|
|
95
|
+
mode: aiChatMode,
|
|
96
|
+
}),
|
|
97
|
+
});
|
|
81
98
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// Parse the response to check for errors
|
|
89
|
-
const responseData = await response.json();
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`HTTP error! status: ${response.status}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
90
104
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
responseData.outcome === 'failure' &&
|
|
94
|
-
responseData.error
|
|
95
|
-
) {
|
|
96
|
-
const errorMessage = responseData.error.message;
|
|
105
|
+
// Parse the response to check for errors
|
|
106
|
+
const responseData = await response.json();
|
|
97
107
|
|
|
98
|
-
// Check if
|
|
108
|
+
// Check if the response contains a VizHub error
|
|
99
109
|
if (
|
|
100
|
-
|
|
101
|
-
|
|
110
|
+
responseData.outcome === 'failure' &&
|
|
111
|
+
responseData.error
|
|
102
112
|
) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
const errorMessage = responseData.error.message;
|
|
114
|
+
|
|
115
|
+
// Check if this is the specific permission error that should trigger auto-fork
|
|
116
|
+
if (
|
|
117
|
+
errorMessage ===
|
|
118
|
+
'You do not have permission to use AI chat on this visualization. Only users with edit access can use this feature. Fork the viz to edit it.'
|
|
119
|
+
) {
|
|
120
|
+
// Trigger auto-fork instead of showing error
|
|
121
|
+
try {
|
|
122
|
+
await autoForkAndRetryAI?.(
|
|
123
|
+
currentPrompt,
|
|
124
|
+
aiChatMode,
|
|
125
|
+
);
|
|
126
|
+
// If we reach here, the fork was successful and redirect should happen
|
|
127
|
+
return;
|
|
128
|
+
} catch (forkError) {
|
|
129
|
+
console.error('Auto-fork failed:', forkError);
|
|
130
|
+
setErrorMessage(
|
|
131
|
+
'Failed to fork visualization. Please try forking manually.',
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
117
135
|
}
|
|
136
|
+
|
|
137
|
+
// For other errors, show the error message
|
|
138
|
+
setErrorMessage(errorMessage);
|
|
139
|
+
return;
|
|
118
140
|
}
|
|
119
141
|
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
142
|
+
// The backend handles all ShareDB operations for successful responses
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error('Error getting AI response:', error);
|
|
145
|
+
setErrorMessage(
|
|
146
|
+
'Failed to send message. Please try again.',
|
|
147
|
+
);
|
|
148
|
+
} finally {
|
|
149
|
+
setIsLoading(false);
|
|
123
150
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
aiChatMessage,
|
|
136
|
-
isLoading,
|
|
137
|
-
aiChatEndpoint,
|
|
138
|
-
aiChatOptions,
|
|
139
|
-
currentChatId,
|
|
140
|
-
aiChatMode,
|
|
141
|
-
autoForkAndRetryAI,
|
|
142
|
-
]);
|
|
151
|
+
},
|
|
152
|
+
[
|
|
153
|
+
aiChatMessage,
|
|
154
|
+
isLoading,
|
|
155
|
+
aiChatEndpoint,
|
|
156
|
+
aiChatOptions,
|
|
157
|
+
currentChatId,
|
|
158
|
+
aiChatMode,
|
|
159
|
+
autoForkAndRetryAI,
|
|
160
|
+
],
|
|
161
|
+
);
|
|
143
162
|
|
|
144
163
|
// Check for stored AI prompt on component mount (post-fork restoration)
|
|
145
164
|
useEffect(() => {
|
|
165
|
+
DEBUG &&
|
|
166
|
+
console.log('AIChat: Checking for stored AI prompt');
|
|
146
167
|
const storedPrompt = getStoredAIPrompt();
|
|
168
|
+
DEBUG &&
|
|
169
|
+
console.log(
|
|
170
|
+
'AIChat: Stored prompt result:',
|
|
171
|
+
storedPrompt,
|
|
172
|
+
);
|
|
147
173
|
if (storedPrompt) {
|
|
148
174
|
// Restore the prompt and mode
|
|
175
|
+
DEBUG &&
|
|
176
|
+
console.log('AIChat: Restoring prompt and mode');
|
|
149
177
|
setAIChatMessage(storedPrompt.prompt);
|
|
150
178
|
setAIChatMode(
|
|
151
179
|
storedPrompt.modelName === 'ask' ? 'ask' : 'edit',
|
|
152
180
|
);
|
|
153
181
|
|
|
154
182
|
// Clear the stored prompt
|
|
183
|
+
DEBUG &&
|
|
184
|
+
console.log('AIChat: Clearing stored prompt');
|
|
155
185
|
clearStoredAIPrompt();
|
|
156
186
|
|
|
157
187
|
// Auto-submit the restored prompt after a short delay
|
|
188
|
+
DEBUG &&
|
|
189
|
+
console.log('AIChat: Scheduling auto-submit');
|
|
158
190
|
setTimeout(() => {
|
|
159
|
-
|
|
191
|
+
DEBUG &&
|
|
192
|
+
console.log(
|
|
193
|
+
'AIChat: Auto-submitting restored prompt',
|
|
194
|
+
);
|
|
195
|
+
handleSendMessage(storedPrompt.prompt);
|
|
160
196
|
}, 100);
|
|
197
|
+
} else {
|
|
198
|
+
DEBUG &&
|
|
199
|
+
console.log('AIChat: No stored prompt found');
|
|
161
200
|
}
|
|
162
201
|
}, [
|
|
163
202
|
getStoredAIPrompt,
|
|
@@ -181,7 +220,7 @@ export const AIChat = () => {
|
|
|
181
220
|
<div className="ai-chat-empty">
|
|
182
221
|
<div className="ai-chat-empty-icon">✨</div>
|
|
183
222
|
<h3 className="ai-chat-empty-title">
|
|
184
|
-
Hi
|
|
223
|
+
Hi! I'm here to help you edit with AI.
|
|
185
224
|
</h3>
|
|
186
225
|
<div className="ai-chat-empty-text">
|
|
187
226
|
How can I help you?
|
|
@@ -194,7 +233,7 @@ export const AIChat = () => {
|
|
|
194
233
|
<button
|
|
195
234
|
className="ai-chat-suggested-prompt"
|
|
196
235
|
onClick={() =>
|
|
197
|
-
|
|
236
|
+
handleSendMessage(
|
|
198
237
|
'Explain how this works',
|
|
199
238
|
)
|
|
200
239
|
}
|
|
@@ -204,7 +243,7 @@ export const AIChat = () => {
|
|
|
204
243
|
<button
|
|
205
244
|
className="ai-chat-suggested-prompt"
|
|
206
245
|
onClick={() =>
|
|
207
|
-
|
|
246
|
+
handleSendMessage(
|
|
208
247
|
'How could I change it so that the circles are bigger?',
|
|
209
248
|
)
|
|
210
249
|
}
|
|
@@ -215,7 +254,7 @@ export const AIChat = () => {
|
|
|
215
254
|
<button
|
|
216
255
|
className="ai-chat-suggested-prompt"
|
|
217
256
|
onClick={() =>
|
|
218
|
-
|
|
257
|
+
handleSendMessage(
|
|
219
258
|
'What does this function do?',
|
|
220
259
|
)
|
|
221
260
|
}
|
|
@@ -225,7 +264,7 @@ export const AIChat = () => {
|
|
|
225
264
|
<button
|
|
226
265
|
className="ai-chat-suggested-prompt"
|
|
227
266
|
onClick={() =>
|
|
228
|
-
|
|
267
|
+
handleSendMessage(
|
|
229
268
|
'How can I make this more accessible?',
|
|
230
269
|
)
|
|
231
270
|
}
|
|
@@ -241,7 +280,7 @@ export const AIChat = () => {
|
|
|
241
280
|
<button
|
|
242
281
|
className="ai-chat-suggested-prompt"
|
|
243
282
|
onClick={() =>
|
|
244
|
-
|
|
283
|
+
handleSendMessage(
|
|
245
284
|
'Change the circles to squares',
|
|
246
285
|
)
|
|
247
286
|
}
|
|
@@ -251,7 +290,7 @@ export const AIChat = () => {
|
|
|
251
290
|
<button
|
|
252
291
|
className="ai-chat-suggested-prompt"
|
|
253
292
|
onClick={() =>
|
|
254
|
-
|
|
293
|
+
handleSendMessage(
|
|
255
294
|
'Add a button that toggles the animation',
|
|
256
295
|
)
|
|
257
296
|
}
|
|
@@ -262,7 +301,7 @@ export const AIChat = () => {
|
|
|
262
301
|
<button
|
|
263
302
|
className="ai-chat-suggested-prompt"
|
|
264
303
|
onClick={() =>
|
|
265
|
-
|
|
304
|
+
handleSendMessage(
|
|
266
305
|
'Fix the CSS so the layout is responsive',
|
|
267
306
|
)
|
|
268
307
|
}
|
|
@@ -273,7 +312,7 @@ export const AIChat = () => {
|
|
|
273
312
|
<button
|
|
274
313
|
className="ai-chat-suggested-prompt"
|
|
275
314
|
onClick={() =>
|
|
276
|
-
|
|
315
|
+
handleSendMessage(
|
|
277
316
|
'Refactor this function to use async/await',
|
|
278
317
|
)
|
|
279
318
|
}
|
|
@@ -150,6 +150,33 @@
|
|
|
150
150
|
line-height: 1.4;
|
|
151
151
|
word-wrap: break-word;
|
|
152
152
|
|
|
153
|
+
// Undo button styles (moved from DiffView)
|
|
154
|
+
.undo-button-container {
|
|
155
|
+
display: flex;
|
|
156
|
+
justify-content: flex-end;
|
|
157
|
+
margin-top: 16px;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.undo-button {
|
|
161
|
+
background-color: #dc3545;
|
|
162
|
+
color: white;
|
|
163
|
+
border: none;
|
|
164
|
+
border-radius: 4px;
|
|
165
|
+
padding: 4px 8px;
|
|
166
|
+
font-size: 12px;
|
|
167
|
+
cursor: pointer;
|
|
168
|
+
transition: background-color 0.2s;
|
|
169
|
+
|
|
170
|
+
&:hover:not(:disabled) {
|
|
171
|
+
background-color: #c82333;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
&:disabled {
|
|
175
|
+
background-color: #6c757d;
|
|
176
|
+
cursor: not-allowed;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
153
180
|
// Markdown styling for dark backgrounds
|
|
154
181
|
pre {
|
|
155
182
|
background-color: rgba(0, 0, 0, 0.3);
|
|
@@ -21,11 +21,11 @@ const DEBUG = false;
|
|
|
21
21
|
export const handleAIChatMessage =
|
|
22
22
|
({
|
|
23
23
|
shareDBDoc,
|
|
24
|
-
|
|
24
|
+
createAIEditLocalPresence,
|
|
25
25
|
onCreditDeduction,
|
|
26
26
|
}: {
|
|
27
27
|
shareDBDoc: ShareDBDoc<VizContent>;
|
|
28
|
-
|
|
28
|
+
createAIEditLocalPresence: () => any;
|
|
29
29
|
onCreditDeduction?: any;
|
|
30
30
|
}) =>
|
|
31
31
|
async (req: any, res: any) => {
|
|
@@ -58,7 +58,7 @@ export const handleAIChatMessage =
|
|
|
58
58
|
// Create LLM function for streaming
|
|
59
59
|
const llmFunction = createLLMFunction({
|
|
60
60
|
shareDBDoc,
|
|
61
|
-
|
|
61
|
+
createAIEditLocalPresence,
|
|
62
62
|
chatId,
|
|
63
63
|
});
|
|
64
64
|
|
|
@@ -44,15 +44,15 @@ const enableStreamingEditing = false;
|
|
|
44
44
|
*/
|
|
45
45
|
export const createLLMFunction = ({
|
|
46
46
|
shareDBDoc,
|
|
47
|
-
|
|
47
|
+
createAIEditLocalPresence,
|
|
48
48
|
chatId,
|
|
49
49
|
}: {
|
|
50
50
|
shareDBDoc: ShareDBDoc<VizContent>;
|
|
51
|
-
|
|
51
|
+
createAIEditLocalPresence: () => any;
|
|
52
52
|
chatId: VizChatId;
|
|
53
53
|
}) => {
|
|
54
54
|
return async (fullPrompt: string) => {
|
|
55
|
-
const localPresence =
|
|
55
|
+
const localPresence = createAIEditLocalPresence();
|
|
56
56
|
|
|
57
57
|
// Create OpenRouter client for reasoning token support
|
|
58
58
|
const openRouterClient = new OpenAI({
|
|
@@ -133,13 +133,13 @@ export const createLLMFunction = ({
|
|
|
133
133
|
line,
|
|
134
134
|
);
|
|
135
135
|
|
|
136
|
-
// Update
|
|
136
|
+
// Update AI presence to show cursor at the end of the file
|
|
137
137
|
const currentFile =
|
|
138
138
|
shareDBDoc.data.files[currentEditingFileId];
|
|
139
139
|
if (currentFile && currentFile.text) {
|
|
140
140
|
const textLength = currentFile.text.length;
|
|
141
141
|
const filePresence = {
|
|
142
|
-
username: '
|
|
142
|
+
username: 'AI Editor',
|
|
143
143
|
start: [
|
|
144
144
|
'files',
|
|
145
145
|
currentEditingFileId,
|
|
@@ -157,7 +157,7 @@ export const createLLMFunction = ({
|
|
|
157
157
|
localPresence.submit(filePresence, (error) => {
|
|
158
158
|
if (error) {
|
|
159
159
|
console.warn(
|
|
160
|
-
'
|
|
160
|
+
'AI Editor line presence submission error:',
|
|
161
161
|
error,
|
|
162
162
|
);
|
|
163
163
|
}
|
|
@@ -201,6 +201,9 @@ export const createLLMFunction = ({
|
|
|
201
201
|
effort: 'medium',
|
|
202
202
|
exclude: false,
|
|
203
203
|
},
|
|
204
|
+
provider: {
|
|
205
|
+
sort: 'throughput',
|
|
206
|
+
}, // New parameter for OpenRouter routing
|
|
204
207
|
usage: { include: true },
|
|
205
208
|
stream: true,
|
|
206
209
|
});
|
|
@@ -275,16 +278,16 @@ export const createLLMFunction = ({
|
|
|
275
278
|
// Finalize the AI message by clearing temporary fields
|
|
276
279
|
finalizeAIMessage(shareDBDoc, chatId);
|
|
277
280
|
|
|
278
|
-
// Clear
|
|
281
|
+
// Clear AI Editor presence when done
|
|
279
282
|
DEBUG &&
|
|
280
283
|
console.log(
|
|
281
|
-
'AI editing done, clearing
|
|
284
|
+
'AI editing done, clearing AI Editor presence',
|
|
282
285
|
);
|
|
283
286
|
localPresence.submit(null, (error) => {
|
|
284
|
-
DEBUG && console.log('
|
|
287
|
+
DEBUG && console.log('AI Editor presence cleared');
|
|
285
288
|
if (error) {
|
|
286
289
|
console.warn(
|
|
287
|
-
'
|
|
290
|
+
'AI Editor presence cleanup error:',
|
|
288
291
|
error,
|
|
289
292
|
);
|
|
290
293
|
}
|
|
@@ -154,6 +154,9 @@ export const generateAIResponse = async ({
|
|
|
154
154
|
effort: 'medium',
|
|
155
155
|
exclude: false,
|
|
156
156
|
} as any, // Type assertion for OpenRouter-specific reasoning parameter
|
|
157
|
+
provider: {
|
|
158
|
+
sort: 'throughput',
|
|
159
|
+
} as any, // Type assertion for OpenRouter-specific provider parameter
|
|
157
160
|
stream: true,
|
|
158
161
|
});
|
|
159
162
|
|
|
@@ -40,6 +40,9 @@ export const handleAICopilot = () => {
|
|
|
40
40
|
apiKey: VZCODE_AI_COPILOT_API_KEY,
|
|
41
41
|
baseURL: VZCODE_AI_COPILOT_BASE_URL,
|
|
42
42
|
},
|
|
43
|
+
additionalParameters: {
|
|
44
|
+
provider: { sort: 'throughput' },
|
|
45
|
+
},
|
|
43
46
|
streaming: false,
|
|
44
47
|
};
|
|
45
48
|
debug && console.log('chatModel options:', options);
|
package/src/server/index.ts
CHANGED
|
@@ -100,20 +100,20 @@ app.use(express.static(dir));
|
|
|
100
100
|
const shareDBConnection = shareDBBackend.connect();
|
|
101
101
|
const shareDBDoc = shareDBConnection.get('documents', '1');
|
|
102
102
|
|
|
103
|
-
// Set up presence for
|
|
103
|
+
// Set up presence for AI editing following the same pattern as useShareDB.ts
|
|
104
104
|
const docPresence = shareDBConnection.getDocPresence(
|
|
105
105
|
'documents',
|
|
106
106
|
'1',
|
|
107
107
|
);
|
|
108
108
|
|
|
109
|
-
// Create local presence for
|
|
110
|
-
const
|
|
109
|
+
// Create local presence for AI editing with a unique ID
|
|
110
|
+
const generateAIEditId = () => {
|
|
111
111
|
const timestamp = Date.now().toString(36);
|
|
112
|
-
return `
|
|
112
|
+
return `ai-edit-${timestamp}`;
|
|
113
113
|
};
|
|
114
114
|
|
|
115
|
-
const
|
|
116
|
-
docPresence.create(
|
|
115
|
+
const createAIEditLocalPresence = () =>
|
|
116
|
+
docPresence.create(generateAIEditId());
|
|
117
117
|
|
|
118
118
|
shareDBDoc.create(initialDocument, json1Presence.type.uri);
|
|
119
119
|
|
|
@@ -137,7 +137,7 @@ app.post(
|
|
|
137
137
|
bodyParser.json(),
|
|
138
138
|
handleAIChatMessage({
|
|
139
139
|
shareDBDoc,
|
|
140
|
-
|
|
140
|
+
createAIEditLocalPresence,
|
|
141
141
|
onCreditDeduction: undefined,
|
|
142
142
|
}),
|
|
143
143
|
);
|