mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-25 14:31:33 +08:00
Fix Anthropic responses when tools are called
This commit is contained in:
parent
38e7d10fd7
commit
e5aab5bc04
@ -319,11 +319,34 @@ export async function setupStreamingResponse(
|
|||||||
|
|
||||||
// Handle completion
|
// Handle completion
|
||||||
if (message.done) {
|
if (message.done) {
|
||||||
console.log(`[${responseId}] Stream completed for session ${sessionId}, has content: ${!!message.content}, content length: ${message.content?.length || 0}, current response: ${assistantResponse.length} chars, post-tool response: ${postToolResponse.length} chars`);
|
console.log(`[${responseId}] Stream completed for session ${sessionId}, has content: ${!!message.content}, content length: ${message.content?.length || 0}, current response: ${assistantResponse.length} chars`);
|
||||||
|
|
||||||
// Dump message content to console for debugging
|
// Dump message content to console for debugging
|
||||||
if (message.content) {
|
if (message.content) {
|
||||||
console.log(`[${responseId}] CONTENT IN DONE MESSAGE (first 200 chars): "${message.content.substring(0, 200)}..."`);
|
console.log(`[${responseId}] CONTENT IN DONE MESSAGE (first 200 chars): "${message.content.substring(0, 200)}..."`);
|
||||||
|
|
||||||
|
// Check if the done message contains the exact same content as our accumulated response
|
||||||
|
// We normalize by removing whitespace to avoid false negatives due to spacing differences
|
||||||
|
const normalizedMessage = message.content.trim();
|
||||||
|
const normalizedResponse = assistantResponse.trim();
|
||||||
|
|
||||||
|
if (normalizedMessage === normalizedResponse) {
|
||||||
|
console.log(`[${responseId}] Final message is identical to accumulated response, no need to update`);
|
||||||
|
}
|
||||||
|
// If the done message is longer but contains our accumulated response, use the done message
|
||||||
|
else if (normalizedMessage.includes(normalizedResponse) && normalizedMessage.length > normalizedResponse.length) {
|
||||||
|
console.log(`[${responseId}] Final message is more complete than accumulated response, using it`);
|
||||||
|
assistantResponse = message.content;
|
||||||
|
}
|
||||||
|
// If the done message is different and not already included, append it to avoid duplication
|
||||||
|
else if (!normalizedResponse.includes(normalizedMessage) && normalizedMessage.length > 0) {
|
||||||
|
console.log(`[${responseId}] Final message has unique content, using it`);
|
||||||
|
assistantResponse = message.content;
|
||||||
|
}
|
||||||
|
// Otherwise, we already have the content accumulated, so no need to update
|
||||||
|
else {
|
||||||
|
console.log(`[${responseId}] Already have this content accumulated, not updating`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear timeout if set
|
// Clear timeout if set
|
||||||
@ -332,24 +355,6 @@ export async function setupStreamingResponse(
|
|||||||
timeoutId = null;
|
timeoutId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the final message is displayed
|
|
||||||
if (message.content) {
|
|
||||||
// If we've been collecting post-tool content, add this content to it
|
|
||||||
if (receivedPostToolContent) {
|
|
||||||
// Only add if it's not already included (avoid duplication)
|
|
||||||
if (!postToolResponse.includes(message.content)) {
|
|
||||||
postToolResponse += message.content;
|
|
||||||
console.log(`[${responseId}] Added final chunk to post-tool response`);
|
|
||||||
}
|
|
||||||
// Use the post-tool response as our final response
|
|
||||||
assistantResponse = postToolResponse;
|
|
||||||
} else if (!assistantResponse.includes(message.content)) {
|
|
||||||
// For standard responses, add any new content in the final message
|
|
||||||
console.log(`[${responseId}] Final message has unique content, using it`);
|
|
||||||
assistantResponse = message.content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always mark as done when we receive the done flag
|
// Always mark as done when we receive the done flag
|
||||||
onContentUpdate(assistantResponse, true);
|
onContentUpdate(assistantResponse, true);
|
||||||
|
|
||||||
|
@ -996,18 +996,18 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
*/
|
*/
|
||||||
private showToolExecutionInfo(toolExecutionData: any) {
|
private showToolExecutionInfo(toolExecutionData: any) {
|
||||||
console.log(`Showing tool execution info: ${JSON.stringify(toolExecutionData)}`);
|
console.log(`Showing tool execution info: ${JSON.stringify(toolExecutionData)}`);
|
||||||
|
|
||||||
// Enhanced debugging for tool execution
|
// Enhanced debugging for tool execution
|
||||||
if (!toolExecutionData) {
|
if (!toolExecutionData) {
|
||||||
console.error('Tool execution data is missing or undefined');
|
console.error('Tool execution data is missing or undefined');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for required properties
|
// Check for required properties
|
||||||
const actionType = toolExecutionData.action || '';
|
const actionType = toolExecutionData.action || '';
|
||||||
const toolName = toolExecutionData.tool || 'unknown';
|
const toolName = toolExecutionData.tool || 'unknown';
|
||||||
console.log(`Tool execution details: action=${actionType}, tool=${toolName}, hasResult=${!!toolExecutionData.result}`);
|
console.log(`Tool execution details: action=${actionType}, tool=${toolName}, hasResult=${!!toolExecutionData.result}`);
|
||||||
|
|
||||||
// Force action to 'result' if missing but result is present
|
// Force action to 'result' if missing but result is present
|
||||||
if (!actionType && toolExecutionData.result) {
|
if (!actionType && toolExecutionData.result) {
|
||||||
console.log('Setting missing action to "result" since result is present');
|
console.log('Setting missing action to "result" since result is present');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user