⚠️ refactor: Use Error Content Part Instead Of Throwing Error for Agents (#6262)

This commit is contained in:
Danny Avila 2025-03-09 18:06:34 -04:00 committed by GitHub
parent 3e3dfe5bad
commit d6ab769b80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 2 deletions

View file

@ -325,4 +325,37 @@ describe('formatAgentMessages', () => {
); );
expect(result[0].content).not.toContain('Analyzing the problem...'); expect(result[0].content).not.toContain('Analyzing the problem...');
}); });
it('should exclude ERROR type content parts', () => {
const payload = [
{
role: 'assistant',
content: [
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Hello there' },
{
type: ContentTypes.ERROR,
[ContentTypes.ERROR]:
'An error occurred while processing the request: Something went wrong',
},
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Final answer' },
],
},
];
const result = formatAgentMessages(payload);
expect(result).toHaveLength(1);
expect(result[0]).toBeInstanceOf(AIMessage);
expect(result[0].content).toEqual([
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Hello there' },
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Final answer' },
]);
// Make sure no error content exists in the result
const hasErrorContent = result[0].content.some(
(item) =>
item.type === ContentTypes.ERROR || JSON.stringify(item).includes('An error occurred'),
);
expect(hasErrorContent).toBe(false);
});
}); });

View file

@ -211,6 +211,8 @@ const formatAgentMessages = (payload) => {
} else if (part.type === ContentTypes.THINK) { } else if (part.type === ContentTypes.THINK) {
hasReasoning = true; hasReasoning = true;
continue; continue;
} else if (part.type === ContentTypes.ERROR) {
continue;
} else { } else {
currentContent.push(part); currentContent.push(part);
} }

View file

@ -812,7 +812,10 @@ class AgentClient extends BaseClient {
'[api/server/controllers/agents/client.js #sendCompletion] Unhandled error type', '[api/server/controllers/agents/client.js #sendCompletion] Unhandled error type',
err, err,
); );
throw err; this.contentParts.push({
type: ContentTypes.ERROR,
[ContentTypes.ERROR]: `An error occurred while processing the request${err?.message ? `: ${err.message}` : ''}`,
});
} }
} }
} }

View file

@ -32,7 +32,12 @@ const Part = memo(({ part, isSubmitting, attachments, showCursor, isCreatedByUse
} }
if (part.type === ContentTypes.ERROR) { if (part.type === ContentTypes.ERROR) {
return <ErrorMessage text={part[ContentTypes.TEXT].value} className="my-2" />; return (
<ErrorMessage
text={part[ContentTypes.ERROR] ?? part[ContentTypes.TEXT]?.value}
className="my-2"
/>
);
} else if (part.type === ContentTypes.TEXT) { } else if (part.type === ContentTypes.TEXT) {
const text = typeof part.text === 'string' ? part.text : part.text.value; const text = typeof part.text === 'string' ? part.text : part.text.value;