fixed processing message not stopping after pattern output completion

This commit is contained in:
jmd1010
2025-03-21 23:26:54 -04:00
parent 943b26eeef
commit 4208a02191

View File

@@ -377,8 +377,137 @@ async function readFileContent(file: File): Promise<string> {
}
}
async function handleSubmit() {
if (!userInput.trim()) return;
try {
console.log('\n=== Submit Handler Start ===');
// Store the user input before any processing
const inputText = userInput.trim();
console.log('Captured user input:', inputText);
// Handle YouTube URLs with the existing flow
if (isYouTubeURL) {
console.log('2a. Starting YouTube flow');
await processYouTubeURL(inputText);
return;
}
// For regular text input, add the user message to the UI first
messageStore.update(messages => [...messages, {
role: 'user',
content: inputText
}]);
// Add loading indicator
messageStore.update(messages => [...messages, {
role: 'system',
content: 'Processing...',
format: 'loading'
}]);
// Clear input fields
userInput = "";
const filesForProcessing = [...uploadedFiles];
const contentsForProcessing = [...fileContents];
uploadedFiles = [];
fileContents = [];
fileButtonKey = !fileButtonKey;
// Prepare content with file attachments if any
const contentWithFiles = contentsForProcessing.length > 0
? `${inputText}\n\nFile Contents (${filesForProcessing.map(f => f.endsWith('.pdf') ? 'PDF' : 'Text').join(', ')}):\n${contentsForProcessing.join('\n\n---\n\n')}`
: inputText;
// Get the enhanced prompt
const enhancedPrompt = contentsForProcessing.length > 0
? `${$systemPrompt}\nAnalyze and process the provided content according to these instructions.`
: $systemPrompt;
console.log('Content to send:', {
text: contentWithFiles.substring(0, 100) + '...',
length: contentWithFiles.length,
hasFiles: contentsForProcessing.length > 0
});
try {
// Get the chat stream
const stream = await chatService.streamChat(contentWithFiles, enhancedPrompt);
// Process the stream
await chatService.processStream(
stream,
(content, response) => {
messageStore.update(messages => {
const newMessages = [...messages];
// Remove the loading message
const loadingIndex = newMessages.findIndex(m => m.format === 'loading');
if (loadingIndex !== -1) {
newMessages.splice(loadingIndex, 1);
}
// Add or update the assistant message
const assistantIndex = newMessages.findIndex(m => m.role === 'assistant');
if (assistantIndex !== -1) {
newMessages[assistantIndex].content = content;
newMessages[assistantIndex].format = response?.format;
} else {
newMessages.push({
role: 'assistant',
content,
format: response?.format
});
}
return newMessages;
});
},
(error) => {
// Make sure to remove loading message on error
messageStore.update(messages =>
messages.filter(m => m.format !== 'loading')
);
console.error('Stream processing error:', error);
// Show error message using a valid format type
messageStore.update(messages => [...messages, {
role: 'system',
content: `Error: ${error instanceof Error ? error.message : String(error)}`,
format: 'plain'
}]);
}
);
} catch (error) {
// Make sure to remove loading message on error
messageStore.update(messages =>
messages.filter(m => m.format !== 'loading')
);
throw error; // Re-throw to be caught by the outer try/catch
}
} catch (error) {
console.error('Chat submission error:', error);
// Make sure to remove loading message on error (redundant but safe)
messageStore.update(messages =>
messages.filter(m => m.format !== 'loading')
);
// Show error message using a valid format type
messageStore.update(messages => [...messages, {
role: 'system',
content: `Error: ${error instanceof Error ? error.message : String(error)}`,
format: 'plain'
}]);
} finally {
// As a final safety measure, ensure loading message is removed
messageStore.update(messages =>
messages.filter(m => m.format !== 'loading')
);
}
}
async function handleSubmit() {
/* async function handleSubmit() {
if (!userInput.trim()) return;
try {
@@ -420,7 +549,7 @@ async function handleSubmit() {
} catch (error) {
console.error('Chat submission error:', error);
}
}
} */