Skip to content

Commit 02440b5

Browse files
committed
fix: always wait for name generation to complete on full message
Remove Promise.race timeout - with voice input, messages can go from empty to complete instantly, so we must ensure the generated name reflects the total content, not a partial intermediate state.
1 parent d7be2dc commit 02440b5

File tree

1 file changed

+22
-41
lines changed

1 file changed

+22
-41
lines changed

src/browser/hooks/useWorkspaceName.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -183,54 +183,35 @@ export function useWorkspaceName(options: UseWorkspaceNameOptions): UseWorkspace
183183
return manualName;
184184
}
185185

186-
// If we already have a generated name and nothing is pending, return it
187-
if (generatedName && !isGenerating && !debounceTimerRef.current) {
188-
return generatedName;
189-
}
190-
191-
// Helper to wait for pending generation with optional timeout
192-
const waitForPending = async (timeoutMs?: number): Promise<string> => {
193-
// If there's a debounced generation pending, trigger it now
194-
if (debounceTimerRef.current) {
195-
clearTimeout(debounceTimerRef.current);
196-
debounceTimerRef.current = null;
197-
return generateName(message);
198-
}
199-
200-
// If generation is in progress, wait for it (with optional timeout)
201-
if (generationPromiseRef.current) {
202-
if (timeoutMs !== undefined) {
203-
const timeout = new Promise<string>((resolve) =>
204-
setTimeout(() => resolve(""), timeoutMs)
205-
);
206-
return Promise.race([generationPromiseRef.current.promise, timeout]);
207-
}
208-
return generationPromiseRef.current.promise;
209-
}
186+
// Always wait for any pending generation to complete on the full message.
187+
// This is important because with voice input, the message can go from empty
188+
// to complete very quickly - we must ensure the generated name reflects the
189+
// total content, not a partial intermediate state.
210190

211-
// Generate if we don't have a name yet
212-
if (!generatedName && message.trim()) {
213-
return generateName(message);
214-
}
191+
// If there's a debounced generation pending, trigger it immediately
192+
if (debounceTimerRef.current) {
193+
clearTimeout(debounceTimerRef.current);
194+
debounceTimerRef.current = null;
195+
return generateName(message);
196+
}
215197

216-
return "";
217-
};
198+
// If generation is in progress, wait for it to complete
199+
if (generationPromiseRef.current) {
200+
return generationPromiseRef.current.promise;
201+
}
218202

219-
// If we have no name, we must wait fully for generation
220-
if (!generatedName) {
221-
return waitForPending();
203+
// If we have a name that was generated for the current message, use it
204+
if (generatedName && lastGeneratedForRef.current === message) {
205+
return generatedName;
222206
}
223207

224-
// We have a name but generation might be pending - wait up to 2s for potential update
225-
const pending = isGenerating || debounceTimerRef.current;
226-
if (pending) {
227-
const result = await waitForPending(2000);
228-
// Use result if we got one, otherwise fall back to existing name
229-
return result || generatedName;
208+
// Otherwise generate a fresh name for the current message
209+
if (message.trim()) {
210+
return generateName(message);
230211
}
231212

232-
return generatedName;
233-
}, [autoGenerate, manualName, generatedName, isGenerating, message, generateName]);
213+
return "";
214+
}, [autoGenerate, manualName, generatedName, message, generateName]);
234215

235216
return useMemo(
236217
() => ({

0 commit comments

Comments
 (0)