-
-
Notifications
You must be signed in to change notification settings - Fork 115
feat: add multimodal UIMessage support #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakobhoeg
wants to merge
5
commits into
TanStack:main
Choose a base branch
from
jakobhoeg:feat/multimodal-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+418
β11
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@tanstack/ai-client': patch | ||
| '@tanstack/ai': patch | ||
| --- | ||
|
|
||
| feat: Add multimodal UIMessage support for images, audio, video, and documents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| import type { | ||
| AudioMessagePart, | ||
| ContentPart, | ||
| DocumentMessagePart, | ||
| ImageMessagePart, | ||
| MessagePart, | ||
| ModelMessage, | ||
| TextPart, | ||
| ToolCallPart, | ||
| ToolResultPart, | ||
| UIMessage, | ||
| VideoMessagePart, | ||
| } from '../../types' | ||
| // =========================== | ||
| // Message Converters | ||
|
|
@@ -29,6 +33,58 @@ function getTextContent(content: string | null | Array<ContentPart>): string { | |
| .join('') | ||
| } | ||
|
|
||
| /** | ||
| * Convert ContentPart array to MessagePart array | ||
| * Preserves all multimodal content (text, image, audio, video, document) | ||
| */ | ||
| function contentPartsToMessageParts( | ||
| contentParts: Array<ContentPart>, | ||
| ): Array<MessagePart> { | ||
| const messageParts: Array<MessagePart> = [] | ||
|
|
||
| for (const part of contentParts) { | ||
| switch (part.type) { | ||
| case 'text': | ||
| messageParts.push({ | ||
| type: 'text', | ||
| content: part.content, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| } as TextPart) | ||
| break | ||
| case 'image': | ||
| messageParts.push({ | ||
| type: 'image', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| } as ImageMessagePart) | ||
| break | ||
| case 'audio': | ||
| messageParts.push({ | ||
| type: 'audio', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| } as AudioMessagePart) | ||
| break | ||
| case 'video': | ||
| messageParts.push({ | ||
| type: 'video', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| } as VideoMessagePart) | ||
| break | ||
| case 'document': | ||
| messageParts.push({ | ||
| type: 'document', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| } as DocumentMessagePart) | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return messageParts | ||
| } | ||
|
|
||
| /** | ||
| * Convert UIMessages or ModelMessages to ModelMessages | ||
| */ | ||
|
|
@@ -52,7 +108,8 @@ export function convertMessagesToModelMessages( | |
| * Convert a UIMessage to ModelMessage(s) | ||
| * | ||
| * This conversion handles the parts-based structure: | ||
| * - Text parts β content field | ||
| * - Text parts β content field (string or ContentPart[]) | ||
| * - Multimodal parts (image, audio, video, document) β ContentPart[] | ||
| * - ToolCall parts β toolCalls array | ||
| * - ToolResult parts β separate role="tool" messages | ||
| * | ||
|
|
@@ -72,12 +129,24 @@ export function uiMessageToModelMessages( | |
| // Separate parts by type | ||
| // Note: thinking parts are UI-only and not included in ModelMessages | ||
| const textParts: Array<TextPart> = [] | ||
| const imageParts: Array<ImageMessagePart> = [] | ||
| const audioParts: Array<AudioMessagePart> = [] | ||
| const videoParts: Array<VideoMessagePart> = [] | ||
| const documentParts: Array<DocumentMessagePart> = [] | ||
| const toolCallParts: Array<ToolCallPart> = [] | ||
| const toolResultParts: Array<ToolResultPart> = [] | ||
|
|
||
| for (const part of uiMessage.parts) { | ||
| if (part.type === 'text') { | ||
| textParts.push(part) | ||
| } else if (part.type === 'image') { | ||
| imageParts.push(part) | ||
| } else if (part.type === 'audio') { | ||
| audioParts.push(part) | ||
| } else if (part.type === 'video') { | ||
| videoParts.push(part) | ||
| } else if (part.type === 'document') { | ||
| documentParts.push(part) | ||
| } else if (part.type === 'tool-call') { | ||
| toolCallParts.push(part) | ||
| } else if (part.type === 'tool-result') { | ||
|
|
@@ -86,8 +155,55 @@ export function uiMessageToModelMessages( | |
| // thinking parts are skipped - they're UI-only | ||
| } | ||
|
|
||
| // Build the main message (user or assistant) | ||
| const content = textParts.map((p) => p.content).join('') || null | ||
| const hasMultimodalContent = | ||
| imageParts.length > 0 || | ||
| audioParts.length > 0 || | ||
| videoParts.length > 0 || | ||
| documentParts.length > 0 | ||
|
|
||
| // Build the content field - use ContentPart[] if multimodal, string otherwise | ||
| let content: string | null | Array<ContentPart> | ||
| if (hasMultimodalContent) { | ||
| const contentParts: Array<ContentPart> = [] | ||
| for (const part of uiMessage.parts) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could've used the utlity function you created? is there some weird drift in types that causes it to fail due to some undefined check? it should be mappable 1:1 |
||
| if (part.type === 'text') { | ||
| contentParts.push({ | ||
| type: 'text', | ||
| content: part.content, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| }) | ||
| } else if (part.type === 'image') { | ||
| contentParts.push({ | ||
| type: 'image', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| }) | ||
| } else if (part.type === 'audio') { | ||
| contentParts.push({ | ||
| type: 'audio', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| }) | ||
| } else if (part.type === 'video') { | ||
| contentParts.push({ | ||
| type: 'video', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| }) | ||
| } else if (part.type === 'document') { | ||
| contentParts.push({ | ||
| type: 'document', | ||
| source: part.source, | ||
| ...(part.metadata !== undefined && { metadata: part.metadata }), | ||
| }) | ||
| } | ||
| } | ||
| content = contentParts.length > 0 ? contentParts : null | ||
| } else { | ||
| // Text-only: use simple string | ||
| content = textParts.map((p) => p.content).join('') || null | ||
| } | ||
|
|
||
| const toolCalls = | ||
| toolCallParts.length > 0 | ||
| ? toolCallParts | ||
|
|
@@ -144,7 +260,7 @@ export function uiMessageToModelMessages( | |
| * Convert a ModelMessage to UIMessage | ||
| * | ||
| * This conversion creates a parts-based structure: | ||
| * - content field β TextPart | ||
| * - content field β TextPart (for string) or multimodal MessageParts (for ContentPart[]) | ||
| * - toolCalls array β ToolCallPart[] | ||
| * - role="tool" messages should be converted separately and merged | ||
| * | ||
|
|
@@ -158,13 +274,18 @@ export function modelMessageToUIMessage( | |
| ): UIMessage { | ||
| const parts: Array<MessagePart> = [] | ||
|
|
||
| // Handle content (convert multimodal content to text for UI) | ||
| const textContent = getTextContent(modelMessage.content) | ||
| if (textContent) { | ||
| parts.push({ | ||
| type: 'text', | ||
| content: textContent, | ||
| }) | ||
| // Handle content - preserve multimodal content | ||
| if (modelMessage.content !== null) { | ||
| if (typeof modelMessage.content === 'string') { | ||
| if (modelMessage.content) { | ||
| parts.push({ | ||
| type: 'text', | ||
| content: modelMessage.content, | ||
| }) | ||
| } | ||
| } else if (Array.isArray(modelMessage.content)) { | ||
| parts.push(...contentPartsToMessageParts(modelMessage.content)) | ||
| } | ||
| } | ||
|
|
||
| // Handle tool calls | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused, don't these two types match identically? from what I see what you're doing is just coping the old data into the new one?