Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ debug-tools/*.json
# Sentry Config File
.env.sentry-build-plugin
data-dump
debug-tools/extracted
debug-tools/extracted
cli
debug-tools
16 changes: 16 additions & 0 deletions src/routes/chat/hooks/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ export function useChat({
return;
}

// Prevent duplicate session creation on rerenders while streaming
connectionStatus.current = 'connecting';

// Start new code generation using API client
const response = await apiClient.createAgentSession({
query: userQuery,
Expand Down Expand Up @@ -487,6 +490,10 @@ export function useChat({
setIsGeneratingBlueprint(false);
sendMessage(createAIMessage('main', 'Blueprint generation complete. Now starting the code generation...', true));

if (!result.websocketUrl || !result.agentId) {
throw new Error('Failed to initialize agent session');
}

// Connect to WebSocket
logger.debug('connecting to ws with created id');
connectWithRetry(result.websocketUrl);
Expand All @@ -498,6 +505,9 @@ export function useChat({
description: userQuery,
});
} else if (connectionStatus.current === 'idle') {
// Prevent duplicate connect calls on rerenders
connectionStatus.current = 'connecting';

setIsBootstrapping(false);
// Show starting message with thinking indicator
setMessages(() => [
Expand All @@ -516,12 +526,18 @@ export function useChat({
setChatId(urlChatId);


if (!response.data.websocketUrl) {
throw new Error('Missing websocketUrl for existing agent');
}

logger.debug('connecting from init for existing chatId');
connectWithRetry(response.data.websocketUrl, {
disableGenerate: true, // We'll handle generation resume in the WebSocket open handler
});
}
} catch (error) {
// Allow retry on failure
connectionStatus.current = 'idle';
logger.error('Error initializing code generation:', error);
if (error instanceof RateLimitExceededError) {
const rateLimitMessage = handleRateLimitError(error.details, onDebugMessage);
Expand Down