vzcode 2.27.0 → 2.29.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/index.html CHANGED
@@ -20,8 +20,8 @@
20
20
  href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap"
21
21
  rel="stylesheet"
22
22
  />
23
- <script type="module" crossorigin src="/assets/index-CHCIQbYC.js"></script>
24
- <link rel="stylesheet" crossorigin href="/assets/index-Dyt9tpmu.css">
23
+ <script type="module" crossorigin src="/assets/index-JcHUw7hN.js"></script>
24
+ <link rel="stylesheet" crossorigin href="/assets/index-CCmGeE_1.css">
25
25
  </head>
26
26
  <body>
27
27
  <div id="root"></div>
@@ -58,6 +58,44 @@ export const addUserMessage = (shareDBDoc, chatId, content) => {
58
58
  shareDBDoc.submitOp(userMessageOp);
59
59
  return userMessage;
60
60
  };
61
+ /**
62
+ * Persists AI metadata fields on a chat (Phase 3).
63
+ *
64
+ * `baseCommitId` records the commit the current AI attempt is applied on
65
+ * top of. `escalationLevel` records how many times the user has pressed
66
+ * "Try Harder". Persisting these on the chat makes retries idempotent and
67
+ * keeps the level consistent across reloads and clients.
68
+ *
69
+ * Only fields that are explicitly provided are written.
70
+ */
71
+ export const setChatAIMetadata = (shareDBDoc, chatId, metadata) => {
72
+ const chat = shareDBDoc.data.chats[chatId];
73
+ if (!chat) {
74
+ return;
75
+ }
76
+ const updates = {};
77
+ if (metadata.baseCommitId !== undefined) {
78
+ updates.baseCommitId = metadata.baseCommitId;
79
+ }
80
+ if (metadata.escalationLevel !== undefined) {
81
+ updates.escalationLevel = metadata.escalationLevel;
82
+ }
83
+ if (Object.keys(updates).length === 0) {
84
+ return;
85
+ }
86
+ const op = diff(shareDBDoc.data, {
87
+ ...shareDBDoc.data,
88
+ chats: {
89
+ ...shareDBDoc.data.chats,
90
+ [chatId]: {
91
+ ...chat,
92
+ ...updates,
93
+ updatedAt: dateToTimestamp(new Date()),
94
+ },
95
+ },
96
+ });
97
+ shareDBDoc.submitOp(op);
98
+ };
61
99
  const DEBUG = false;
62
100
  /**
63
101
  * Updates AI status in the chat
@@ -162,7 +162,6 @@ enableReasoningTokens = false, model, aiRequestOptions, }) => {
162
162
  model: modelName,
163
163
  messages: [{ role: 'user', content: fullPrompt }],
164
164
  stream: true,
165
- ...aiRequestOptions,
166
165
  };
167
166
  // Only include reasoning configuration if reasoning tokens are enabled
168
167
  // if (enableReasoningTokens) {
@@ -1,5 +1,5 @@
1
1
  import { validateRequest } from '../../llm-streaming-server/validation.js';
2
- import { ensureChatsExist, ensureChatExists, addUserMessage, setAIStatus, } from '../../llm-streaming-server/chatOperations.js';
2
+ import { ensureChatsExist, ensureChatExists, addUserMessage, setAIStatus, setChatAIMetadata, } from '../../llm-streaming-server/chatOperations.js';
3
3
  import { createLLMFunction } from '../../llm-streaming-server/llmStreaming.js';
4
4
  import { performAIEditing } from '../../llm-streaming-server/aiEditing.js';
5
5
  import { handleError, handleBackgroundError, } from '../../llm-streaming-server/errorHandling.js';
@@ -7,7 +7,7 @@ import { createRunCodeFunction } from '../../runCode.js';
7
7
  import { createSubmitOperation } from '../../submitOperation.js';
8
8
  import { getGenerationMetadata } from 'editcodewithai';
9
9
  const DEBUG = false;
10
- export const handleAIChatMessage = ({ shareDBDoc, onCreditDeduction, model, aiRequestOptions, enableReasoningTokens, }) => async (req, res) => {
10
+ export const handleAIChatMessage = ({ shareDBDoc, onCreditDeduction, onGenerationFinished, model, aiRequestOptions, enableReasoningTokens, baseCommitId, escalationLevel, }) => async (req, res) => {
11
11
  const { content, chatId } = req.body;
12
12
  if (DEBUG) {
13
13
  console.log('[handleAIChatMessage] content:', content, 'chatId:', chatId, 'shareDBDoc:', shareDBDoc);
@@ -22,6 +22,12 @@ export const handleAIChatMessage = ({ shareDBDoc, onCreditDeduction, model, aiRe
22
22
  ensureChatExists(shareDBDoc, chatId);
23
23
  // Add user message to chat
24
24
  addUserMessage(shareDBDoc, chatId, content);
25
+ // Persist explicit escalation metadata on the chat, so that
26
+ // retries are idempotent and the level survives reloads.
27
+ setChatAIMetadata(shareDBDoc, chatId, {
28
+ baseCommitId,
29
+ escalationLevel,
30
+ });
25
31
  // Return success immediately - AI generation continues in background
26
32
  res.status(200).json('success');
27
33
  // Continue AI processing in background (don't await)
@@ -33,6 +39,7 @@ export const handleAIChatMessage = ({ shareDBDoc, onCreditDeduction, model, aiRe
33
39
  aiRequestOptions,
34
40
  enableReasoningTokens,
35
41
  onCreditDeduction,
42
+ onGenerationFinished,
36
43
  }).catch((error) => {
37
44
  console.error('Background AI processing error:', error);
38
45
  // Handle error without HTTP response
@@ -46,7 +53,7 @@ export const handleAIChatMessage = ({ shareDBDoc, onCreditDeduction, model, aiRe
46
53
  /**
47
54
  * Processes the AI request asynchronously in the background
48
55
  */
49
- const processAIRequestAsync = async ({ shareDBDoc, chatId, content, model, aiRequestOptions, enableReasoningTokens, onCreditDeduction, }) => {
56
+ const processAIRequestAsync = async ({ shareDBDoc, chatId, content, model, aiRequestOptions, enableReasoningTokens, onCreditDeduction, onGenerationFinished, }) => {
50
57
  try {
51
58
  // Create LLM function for streaming
52
59
  const llmFunction = createLLMFunction({
@@ -66,18 +73,34 @@ const processAIRequestAsync = async ({ shareDBDoc, chatId, content, model, aiReq
66
73
  llmFunction,
67
74
  runCode,
68
75
  });
69
- // Handle credit deduction if callback is provided
76
+ // Billing is best-effort. It must NEVER prevent the edit from
77
+ // being committed. Any metadata failure is logged and swallowed.
78
+ let metrics = null;
70
79
  if (onCreditDeduction && editResult.generationId) {
71
80
  try {
72
- await onCreditDeduction(await getGenerationMetadata({
81
+ metrics = await getGenerationMetadata({
73
82
  apiKey: aiRequestOptions?.apiKey ||
74
83
  process.env.VZCODE_EDIT_WITH_AI_API_KEY,
75
84
  generationId: editResult.generationId,
76
- }));
85
+ });
86
+ await onCreditDeduction(metrics);
77
87
  }
78
88
  catch (creditError) {
79
- console.error('Credit deduction error:', creditError);
80
- // Don't fail the request if credit deduction fails
89
+ console.error('Credit deduction error (edit will still be finalized):', creditError);
90
+ }
91
+ }
92
+ // Always notify settlement on success, regardless of billing
93
+ // outcome, so the caller can commit the edit and release locks.
94
+ if (onGenerationFinished) {
95
+ try {
96
+ await onGenerationFinished({
97
+ success: true,
98
+ editResult,
99
+ metrics,
100
+ });
101
+ }
102
+ catch (settleError) {
103
+ console.error('onGenerationFinished (success) error:', settleError);
81
104
  }
82
105
  }
83
106
  // Clear the AI status to indicate completion
@@ -86,6 +109,19 @@ const processAIRequestAsync = async ({ shareDBDoc, chatId, content, model, aiReq
86
109
  catch (error) {
87
110
  // Set error status and add error message to chat
88
111
  setAIStatus(shareDBDoc, chatId, 'error');
112
+ // Always notify settlement on failure so the caller can roll back
113
+ // the pre-restore snapshot and release locks.
114
+ if (onGenerationFinished) {
115
+ try {
116
+ await onGenerationFinished({
117
+ success: false,
118
+ error,
119
+ });
120
+ }
121
+ catch (settleError) {
122
+ console.error('onGenerationFinished (failure) error:', settleError);
123
+ }
124
+ }
89
125
  handleBackgroundError(shareDBDoc, chatId, error);
90
126
  }
91
127
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vzcode",
3
- "version": "2.27.0",
3
+ "version": "2.29.0",
4
4
  "description": "Multiplayer code editor system",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -117,28 +117,28 @@
117
117
  "@codemirror/lang-json": "^6.0.2",
118
118
  "@codemirror/lang-markdown": "^6.5.0",
119
119
  "@codemirror/lint": "^6.9.2",
120
- "@codemirror/state": "^6.5.2",
120
+ "@codemirror/state": "^6.5.3",
121
121
  "@codemirror/theme-one-dark": "^6.1.3",
122
- "@codemirror/view": "^6.38.8",
123
- "@langchain/core": "^1.1.0",
124
- "@langchain/openai": "^1.1.3",
122
+ "@codemirror/view": "^6.39.8",
123
+ "@langchain/core": "^1.1.8",
124
+ "@langchain/openai": "^1.2.0",
125
125
  "@lezer/highlight": "^1.2.3",
126
- "@livekit/components-react": "^2.9.16",
126
+ "@livekit/components-react": "^2.9.17",
127
127
  "@replit/codemirror-indentation-markers": "^6.5.3",
128
128
  "@replit/codemirror-interact": "^6.3.1",
129
129
  "@replit/codemirror-lang-svelte": "^6.0.0",
130
130
  "@replit/codemirror-vscode-keymap": "^6.0.2",
131
131
  "@teamwork/websocket-json-stream": "^2.0.0",
132
132
  "@typescript/vfs": "^1.6.2",
133
- "@uiw/codemirror-theme-abcdef": "^4.25.3",
134
- "@uiw/codemirror-theme-dracula": "^4.25.3",
135
- "@uiw/codemirror-theme-eclipse": "^4.25.3",
136
- "@uiw/codemirror-theme-github": "^4.25.3",
137
- "@uiw/codemirror-theme-material": "^4.25.3",
138
- "@uiw/codemirror-theme-nord": "^4.25.3",
139
- "@uiw/codemirror-theme-okaidia": "^4.25.3",
140
- "@uiw/codemirror-theme-xcode": "^4.25.3",
141
- "@uiw/codemirror-themes": "^4.25.3",
133
+ "@uiw/codemirror-theme-abcdef": "^4.25.4",
134
+ "@uiw/codemirror-theme-dracula": "^4.25.4",
135
+ "@uiw/codemirror-theme-eclipse": "^4.25.4",
136
+ "@uiw/codemirror-theme-github": "^4.25.4",
137
+ "@uiw/codemirror-theme-material": "^4.25.4",
138
+ "@uiw/codemirror-theme-nord": "^4.25.4",
139
+ "@uiw/codemirror-theme-okaidia": "^4.25.4",
140
+ "@uiw/codemirror-theme-xcode": "^4.25.4",
141
+ "@uiw/codemirror-themes": "^4.25.4",
142
142
  "@valtown/codemirror-ts": "^2.3.1",
143
143
  "@vizhub/runtime": "^4.5.0",
144
144
  "@vizhub/viz-types": "^0.5.0",
@@ -154,25 +154,25 @@
154
154
  "d3-color": "^3.1.0",
155
155
  "diff": "^8.0.2",
156
156
  "@dmsnell/diff-match-patch": "^1.1.0",
157
- "diff2html": "^3.4.52",
157
+ "diff2html": "3.4.52",
158
158
  "dotenv": "^17.2.3",
159
159
  "editcodewithai": "^2.4.0",
160
- "eslint-linter-browserify": "^9.39.1",
161
- "express": "^5.1.0",
160
+ "eslint-linter-browserify": "^9.39.2",
161
+ "express": "^5.2.1",
162
162
  "ignore": "^7.0.5",
163
163
  "json0-ot-diff": "^1.1.2",
164
164
  "jszip": "^3.10.1",
165
- "livekit-server-sdk": "^2.14.2",
165
+ "livekit-server-sdk": "^2.15.0",
166
166
  "llm-code-format": "^3.1.0",
167
- "lucide-react": "^0.555.0",
168
- "npm": "^11.6.4",
167
+ "lucide-react": "^0.562.0",
168
+ "npm": "^11.7.0",
169
169
  "open": "^11.0.0",
170
- "prettier-plugin-svelte": "^3.4.0",
170
+ "prettier-plugin-svelte": "^3.4.1",
171
171
  "react": "^18",
172
172
  "react-bootstrap": "^2.10.10",
173
173
  "react-dom": "^18",
174
174
  "react-markdown": "^10.1.0",
175
- "react-router-dom": "^7.9.6",
175
+ "react-router-dom": "^7.11.0",
176
176
  "remark-gfm": "^4.0.1",
177
177
  "sharedb": "^5.2.2",
178
178
  "sharedb-client-browser": "^5.2.2",
@@ -181,32 +181,32 @@
181
181
  "ws": "^8.18.3"
182
182
  },
183
183
  "devDependencies": {
184
- "@eslint/js": "^9.39.1",
185
- "@tauri-apps/cli": "^2.9.5",
184
+ "@eslint/js": "^9.39.2",
185
+ "@tauri-apps/cli": "^2.9.6",
186
186
  "@types/d3-color": "^3.1.3",
187
187
  "@types/react": "^18",
188
188
  "@types/react-dom": "^18",
189
- "@typescript-eslint/eslint-plugin": "^8.48.0",
190
- "@typescript-eslint/parser": "^8.48.0",
191
- "@vitejs/plugin-react": "^5.1.1",
189
+ "@typescript-eslint/eslint-plugin": "^8.51.0",
190
+ "@typescript-eslint/parser": "^8.51.0",
191
+ "@vitejs/plugin-react": "^5.1.2",
192
192
  "concurrently": "^9.2.1",
193
193
  "cross-env": "^10.1.0",
194
- "eslint": "^9.39.1",
194
+ "eslint": "^9.39.2",
195
195
  "eslint-plugin-jsx-a11y": "^6.10.2",
196
196
  "eslint-plugin-react": "^7.37.5",
197
197
  "eslint-plugin-react-hooks": "^7.0.1",
198
- "globals": "^16.5.0",
199
- "npm-check-updates": "^19.1.2",
200
- "prettier": "^3.7.3",
201
- "sass": "^1.94.2",
198
+ "globals": "^17.0.0",
199
+ "npm-check-updates": "^19.2.0",
200
+ "prettier": "^3.7.4",
201
+ "sass": "^1.97.1",
202
202
  "ts-node": "^10.9.2",
203
- "tsx": "^4.20.6",
203
+ "tsx": "^4.21.0",
204
204
  "typescript": "^5.9.3",
205
- "vite": "^7.2.4",
206
- "vitest": "^4.0.14"
205
+ "vite": "^7.3.0",
206
+ "vitest": "^4.0.16"
207
207
  },
208
208
  "optionalDependencies": {
209
- "@rollup/rollup-darwin-arm64": "^4.53.3",
210
- "@rollup/rollup-win32-x64-msvc": "^4.53.3"
209
+ "@rollup/rollup-darwin-arm64": "^4.54.0",
210
+ "@rollup/rollup-win32-x64-msvc": "^4.54.0"
211
211
  }
212
212
  }
@@ -178,7 +178,7 @@ export type VZCodeContextValue = {
178
178
  handleSendMessage: (
179
179
  messageToSend?: string,
180
180
  options?: Record<string, string>,
181
- ) => void;
181
+ ) => Promise<any>;
182
182
 
183
183
  // Message history navigation
184
184
  navigateMessageHistoryUp: () => void;
@@ -204,11 +204,18 @@ export type VZCodeContextValue = {
204
204
  handleSendMessage?: (
205
205
  messageToSend?: string,
206
206
  options?: Record<string, string>,
207
- ) => void;
207
+ ) => Promise<any>;
208
208
  }) => React.ReactNode;
209
209
 
210
210
  // Feature flags
211
211
  enableMinimalEditFlow: boolean;
212
+
213
+ // Analytics callback for chat message submissions
214
+ onAIChatMessageSubmitted?: (params: {
215
+ messageLength: number;
216
+ isRetry?: boolean;
217
+ isUndo?: boolean;
218
+ }) => void;
212
219
  };
213
220
 
214
221
  export interface VZCodeProviderProps {
@@ -247,11 +254,16 @@ export interface VZCodeProviderProps {
247
254
  handleSendMessage?: (
248
255
  messageToSend?: string,
249
256
  options?: Record<string, string>,
250
- ) => void;
257
+ ) => Promise<any>;
251
258
  }) => React.ReactNode;
252
259
  iframeRef?: React.MutableRefObject<HTMLIFrameElement>;
253
260
  handleChatError?: (
254
261
  error: string,
255
262
  message?: string,
256
263
  ) => void;
264
+ onAIChatMessageSubmitted?: (params: {
265
+ messageLength: number;
266
+ isRetry?: boolean;
267
+ isUndo?: boolean;
268
+ }) => void;
257
269
  }
@@ -54,6 +54,7 @@ export const useVZCodeState = ({
54
54
  additionalWidgets,
55
55
  iframeRef: externalIframeRef,
56
56
  handleChatError,
57
+ onAIChatMessageSubmitted,
57
58
  }: Omit<
58
59
  VZCodeProviderProps,
59
60
  'children'
@@ -273,7 +274,10 @@ export const useVZCodeState = ({
273
274
  const DEBUG = false;
274
275
 
275
276
  // Compute isLoading based on the current chat's aiStatus
276
- const [currentChatId] = useState(() => uuidv4());
277
+ // Chat ID changes with each new user message (but not for retry/undo)
278
+ const [currentChatId, setCurrentChatId] = useState(() =>
279
+ uuidv4(),
280
+ );
277
281
  const [selectedChatId, setSelectedChatId] = useState<
278
282
  string | null
279
283
  >(null);
@@ -309,6 +313,22 @@ export const useVZCodeState = ({
309
313
  [handleChatError],
310
314
  );
311
315
 
316
+ // Phase 5: Restore the prompt into the chat input when the server
317
+ // populates `currentChatDraft` (e.g. after Undo), then clear it so it
318
+ // is applied exactly once.
319
+ useEffect(() => {
320
+ const draft = (content as any)?.currentChatDraft;
321
+ if (typeof draft === 'string' && draft.trim()) {
322
+ setAIChatMessage(draft);
323
+ submitOperation((currentContent) => {
324
+ const next: any = { ...currentContent };
325
+ next.currentChatDraft = undefined;
326
+ return next;
327
+ });
328
+ }
329
+ // eslint-disable-next-line react-hooks/exhaustive-deps
330
+ }, [(content as any)?.currentChatDraft]);
331
+
312
332
  // Message history navigation functions
313
333
  const navigateMessageHistoryUp = useCallback(() => {
314
334
  if (messageHistory.length === 0) return;
@@ -385,6 +405,49 @@ export const useVZCodeState = ({
385
405
 
386
406
  setAIErrorMessage(null); // Clear any previous errors
387
407
 
408
+ // Track AI chat message submission
409
+ const isRetry = options?.isRetry === 'true';
410
+ const isUndo = options?.isUndo === 'true';
411
+ if (
412
+ !DEBUG &&
413
+ onAIChatMessageSubmitted &&
414
+ aiChatOptions?.vizId
415
+ ) {
416
+ onAIChatMessageSubmitted({
417
+ messageLength: messageContent.trim().length,
418
+ isRetry,
419
+ isUndo,
420
+ });
421
+ }
422
+
423
+ // Generate new chat ID only for fresh user messages
424
+ // Keep existing chat ID for retries (Try Harder) and undos
425
+ const isRetryOrUndo =
426
+ options?.isRetry === 'true' ||
427
+ options?.isUndo === 'true';
428
+
429
+ let chatIdForRequest: string;
430
+ if (isRetryOrUndo) {
431
+ // Use existing chat ID for retry/undo operations
432
+ chatIdForRequest = activeChatId;
433
+ DEBUG &&
434
+ console.log(
435
+ 'Using existing chatId for retry/undo:',
436
+ chatIdForRequest,
437
+ );
438
+ } else {
439
+ // Generate NEW chat ID for fresh user message
440
+ const newChatId = uuidv4();
441
+ setCurrentChatId(newChatId);
442
+ setSelectedChatId(newChatId);
443
+ chatIdForRequest = newChatId;
444
+ DEBUG &&
445
+ console.log(
446
+ 'Generated new chatId for fresh message:',
447
+ chatIdForRequest,
448
+ );
449
+ }
450
+
388
451
  // Call backend endpoint for AI response
389
452
  try {
390
453
  const response = await fetch(aiChatEndpoint, {
@@ -395,7 +458,7 @@ export const useVZCodeState = ({
395
458
  body: JSON.stringify({
396
459
  ...aiChatOptions,
397
460
  content: messageContent.trim(),
398
- chatId: activeChatId,
461
+ chatId: chatIdForRequest,
399
462
  mode: aiChatMode,
400
463
  ...options,
401
464
  }),
@@ -462,6 +525,7 @@ export const useVZCodeState = ({
462
525
 
463
526
  // The backend handles all ShareDB operations for successful responses
464
527
  // The loading state is now managed via ShareDB aiStatus
528
+ return responseData;
465
529
  } catch (error) {
466
530
  console.error('Error getting AI response:', error);
467
531
  setAIErrorMessage(
@@ -701,5 +765,8 @@ export const useVZCodeState = ({
701
765
 
702
766
  // Feature flags
703
767
  enableMinimalEditFlow,
768
+
769
+ // Analytics callback for chat message submissions
770
+ onAIChatMessageSubmitted,
704
771
  };
705
772
  };
@@ -3,14 +3,11 @@ import {
3
3
  parseMarkdownFiles,
4
4
  } from 'llm-code-format';
5
5
  import {
6
- FORMAT_INSTRUCTIONS,
7
6
  mergeFileChanges,
7
+ prepareFilesForPrompt,
8
8
  } from 'editcodewithai';
9
9
  import { VizFiles } from '@vizhub/viz-types';
10
- import {
11
- generateRunId,
12
- vizFilesToFileCollection,
13
- } from '@vizhub/viz-utils';
10
+ import { generateRunId } from '@vizhub/viz-utils';
14
11
  import JSZip from 'jszip';
15
12
 
16
13
  export const createAICopyPasteHandlers = (
@@ -32,17 +29,25 @@ export const createAICopyPasteHandlers = (
32
29
  }
33
30
 
34
31
  try {
35
- const fileCollection =
36
- vizFilesToFileCollection(files);
32
+ // Apply truncation logic to reduce token usage
33
+ const { files: truncatedFiles, imageFiles } =
34
+ prepareFilesForPrompt(files);
37
35
 
38
- // Format files for AI consumption
36
+ // Format truncated files for markdown
39
37
  const formattedFiles =
40
- formatMarkdownFiles(fileCollection) +
41
- '\n\n' +
42
- FORMAT_INSTRUCTIONS.whole;
38
+ formatMarkdownFiles(truncatedFiles);
39
+
40
+ // Add metadata about skipped image files
41
+ let finalContent = formattedFiles;
42
+ if (imageFiles.length > 0) {
43
+ finalContent +=
44
+ '\n\n<!-- Image files (not included): ' +
45
+ imageFiles.join(', ') +
46
+ ' -->';
47
+ }
43
48
 
44
49
  // Copy to clipboard
45
- await navigator.clipboard.writeText(formattedFiles);
50
+ await navigator.clipboard.writeText(finalContent);
46
51
 
47
52
  // Show success feedback
48
53
  setCopyButtonText('Copied!');
@@ -84,16 +89,8 @@ export const createAICopyPasteHandlers = (
84
89
  return;
85
90
  }
86
91
 
87
- // Preprocess to remove formatting instructions section to avoid creating extra files
88
- const preprocessed = normalized
89
- .split('## Formatting Instructions')[0]
90
- .trim();
91
-
92
92
  // Parse the markdown files format
93
- const parsed = parseMarkdownFiles(
94
- preprocessed,
95
- 'bold',
96
- );
93
+ const parsed = parseMarkdownFiles(normalized, 'bold');
97
94
 
98
95
  if (
99
96
  parsed.files &&
@@ -81,6 +81,54 @@ export const addUserMessage = (
81
81
  return userMessage;
82
82
  };
83
83
 
84
+ /**
85
+ * Persists AI metadata fields on a chat (Phase 3).
86
+ *
87
+ * `baseCommitId` records the commit the current AI attempt is applied on
88
+ * top of. `escalationLevel` records how many times the user has pressed
89
+ * "Try Harder". Persisting these on the chat makes retries idempotent and
90
+ * keeps the level consistent across reloads and clients.
91
+ *
92
+ * Only fields that are explicitly provided are written.
93
+ */
94
+ export const setChatAIMetadata = (
95
+ shareDBDoc: ShareDBDoc<VizContent>,
96
+ chatId: VizChatId,
97
+ metadata: {
98
+ baseCommitId?: string;
99
+ escalationLevel?: number;
100
+ },
101
+ ) => {
102
+ const chat = shareDBDoc.data.chats[chatId];
103
+ if (!chat) {
104
+ return;
105
+ }
106
+
107
+ const updates: Record<string, unknown> = {};
108
+ if (metadata.baseCommitId !== undefined) {
109
+ updates.baseCommitId = metadata.baseCommitId;
110
+ }
111
+ if (metadata.escalationLevel !== undefined) {
112
+ updates.escalationLevel = metadata.escalationLevel;
113
+ }
114
+ if (Object.keys(updates).length === 0) {
115
+ return;
116
+ }
117
+
118
+ const op = diff(shareDBDoc.data, {
119
+ ...shareDBDoc.data,
120
+ chats: {
121
+ ...shareDBDoc.data.chats,
122
+ [chatId]: {
123
+ ...chat,
124
+ ...updates,
125
+ updatedAt: dateToTimestamp(new Date()),
126
+ },
127
+ },
128
+ });
129
+ shareDBDoc.submitOp(op);
130
+ };
131
+
84
132
  const DEBUG = false;
85
133
 
86
134
  /**
@@ -78,6 +78,7 @@ export const createLLMFunction = ({
78
78
  },
79
79
  });
80
80
 
81
+
81
82
  let fullContent = '';
82
83
  let generationId = '';
83
84
  let currentEditingFileName = null;
@@ -252,12 +253,13 @@ export const createLLMFunction = ({
252
253
  const chunks = [];
253
254
  let reasoningContent = '';
254
255
 
256
+
257
+
255
258
  // Configure reasoning tokens based on enableReasoningTokens flag
256
259
  const requestConfig: any = {
257
260
  model: modelName,
258
261
  messages: [{ role: 'user', content: fullPrompt }],
259
262
  stream: true,
260
- ...aiRequestOptions,
261
263
  };
262
264
 
263
265
  // Only include reasoning configuration if reasoning tokens are enabled