fix: handle text parts with tool_call_ids and empty text

This commit is contained in:
Danny Avila 2024-09-03 23:15:29 -04:00
parent 0c04018bb3
commit 350d709763
No known key found for this signature in database
GPG key ID: 2DD9CC89B9B50364
4 changed files with 17 additions and 5 deletions

View file

@ -31,10 +31,14 @@ const Part = memo(({ part, isSubmitting, showCursor, messageId, isCreatedByUser
if (part.type === ContentTypes.ERROR) {
return <ErrorMessage text={part[ContentTypes.TEXT].value} className="my-2" />;
} 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 as { value: string }).value;
if (typeof text !== 'string') {
return null;
}
if (part.tool_call_ids != null && !text) {
return null;
}
return (
<Container>
<Text
@ -55,7 +59,7 @@ const Part = memo(({ part, isSubmitting, showCursor, messageId, isCreatedByUser
if ('args' in toolCall && (!toolCall.type || toolCall.type === ToolCallTypes.TOOL_CALL)) {
return (
<ToolCall
args={toolCall.args}
args={toolCall.args ?? ''}
name={toolCall.name ?? ''}
output={toolCall.output ?? ''}
initialProgress={toolCall.progress ?? 0.1}

View file

@ -19,6 +19,8 @@ type TStepEvent = {
data: Agents.MessageDeltaEvent | Agents.RunStep | Agents.ToolEndEvent;
};
type MessageDeltaUpdate = { type: ContentTypes.TEXT; text: string; tool_call_ids?: string[] };
type AllContentTypes =
| ContentTypes.TEXT
| ContentTypes.TOOL_CALL
@ -55,11 +57,16 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
ContentTypes.TEXT in contentPart &&
typeof contentPart.text === 'string'
) {
const currentContent = updatedContent[index] as { type: ContentTypes.TEXT; text: string };
updatedContent[index] = {
const currentContent = updatedContent[index] as MessageDeltaUpdate;
const update: MessageDeltaUpdate = {
type: ContentTypes.TEXT,
text: (currentContent.text || '') + contentPart.text,
};
if (contentPart.tool_call_ids != null) {
update.tool_call_ids = contentPart.tool_call_ids;
}
updatedContent[index] = update;
} else if (contentType === ContentTypes.IMAGE_URL && 'image_url' in contentPart) {
const currentContent = updatedContent[index] as {
type: ContentTypes.IMAGE_URL;

View file

@ -10,6 +10,7 @@ export namespace Agents {
export type MessageContentText = {
type: ContentTypes.TEXT;
text: string;
tool_call_ids?: string[];
};
export type MessageContentImageUrl = {

View file

@ -381,7 +381,7 @@ export type ContentPart = (
export type TMessageContentParts =
| { type: ContentTypes.ERROR; text: Text & PartMetadata }
| { type: ContentTypes.TEXT; text: string | (Text & PartMetadata) }
| { type: ContentTypes.TEXT; text: string; tool_call_ids?: string[] | (Text & PartMetadata) }
| {
type: ContentTypes.TOOL_CALL;
tool_call: (