|
1 | 1 | import { useLoaderData, useNavigate, useSearchParams } from '@remix-run/react'; |
2 | | -import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { useState, useEffect, useCallback, useRef } from 'react'; |
3 | 3 | import { atom } from 'nanostores'; |
4 | 4 | import { type JSONValue, type Message } from 'ai'; |
5 | 5 | import { toast } from 'react-toastify'; |
@@ -51,6 +51,9 @@ export function useChatHistory() { |
51 | 51 | const [ready, setReady] = useState<boolean>(false); |
52 | 52 | const [urlId, setUrlId] = useState<string | undefined>(); |
53 | 53 |
|
| 54 | + // Track last snapshot parameters so debounced file-change saves use the same message ID |
| 55 | + const lastSnapshotParamsRef = useRef<{ chatIdx: string; chatSummary?: string } | null>(null); |
| 56 | + |
54 | 57 | useEffect(() => { |
55 | 58 | if (!db) { |
56 | 59 | setReady(true); |
@@ -159,6 +162,50 @@ export function useChatHistory() { |
159 | 162 | [db], |
160 | 163 | ); |
161 | 164 |
|
| 165 | + /* |
| 166 | + * Debounced file-change subscriber: re-saves the snapshot after file writes settle. |
| 167 | + * The normal snapshot fires 50ms after the last message change, but file actions |
| 168 | + * may still be in the async execution queue or watcher buffer at that point. |
| 169 | + * This subscriber ensures a final snapshot is taken once all files are written. |
| 170 | + */ |
| 171 | + useEffect(() => { |
| 172 | + if (!db) { |
| 173 | + return undefined; |
| 174 | + } |
| 175 | + |
| 176 | + let debounceTimer: ReturnType<typeof setTimeout> | null = null; |
| 177 | + |
| 178 | + const unsubscribe = workbenchStore.files.subscribe(() => { |
| 179 | + const id = chatId.get(); |
| 180 | + const params = lastSnapshotParamsRef.current; |
| 181 | + |
| 182 | + if (!id || !params) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + if (debounceTimer) { |
| 187 | + clearTimeout(debounceTimer); |
| 188 | + } |
| 189 | + |
| 190 | + debounceTimer = setTimeout(() => { |
| 191 | + const files = workbenchStore.files.get(); |
| 192 | + |
| 193 | + if (Object.keys(files).length > 0) { |
| 194 | + logger.debug('Debounced file-change snapshot save'); |
| 195 | + takeSnapshot(params.chatIdx, files, undefined, params.chatSummary); |
| 196 | + } |
| 197 | + }, 500); |
| 198 | + }); |
| 199 | + |
| 200 | + return () => { |
| 201 | + unsubscribe(); |
| 202 | + |
| 203 | + if (debounceTimer) { |
| 204 | + clearTimeout(debounceTimer); |
| 205 | + } |
| 206 | + }; |
| 207 | + }, [db, takeSnapshot]); |
| 208 | + |
162 | 209 | const restoreSnapshot = useCallback(async (id: string, snapshot?: Snapshot) => { |
163 | 210 | const container = await webcontainer; |
164 | 211 |
|
@@ -254,6 +301,9 @@ export function useChatHistory() { |
254 | 301 | } |
255 | 302 | } |
256 | 303 |
|
| 304 | + // Save params so debounced file-change subscriber can re-save with updated files |
| 305 | + lastSnapshotParamsRef.current = { chatIdx: messages[messages.length - 1].id, chatSummary }; |
| 306 | + |
257 | 307 | takeSnapshot(messages[messages.length - 1].id, workbenchStore.files.get(), _urlId, chatSummary); |
258 | 308 |
|
259 | 309 | if (!description.get() && firstArtifact?.title) { |
|
0 commit comments