fix: enhance UserMessage component to support image parts and improve rendering
- Updated UserMessage to accept a new `parts` prop for handling different message types, including images. - Refactored image handling to extract and display images from the parts array, ensuring proper rendering of image content. - Adjusted the layout and styling of the UserMessage component for better visual presentation.
This commit is contained in:
@@ -589,6 +589,8 @@ export const ChatImpl = memo(
|
|||||||
textareaRef.current?.blur();
|
textareaRef.current?.blur();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(messages);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the change event for the textarea and updates the input state.
|
* Handles the change event for the textarea and updates the input state.
|
||||||
* @param event - The change event from the textarea.
|
* @param event - The change event from the textarea.
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export const Messages = forwardRef<HTMLDivElement, MessagesProps>(
|
|||||||
>
|
>
|
||||||
<div className="grid grid-col-1 w-full">
|
<div className="grid grid-col-1 w-full">
|
||||||
{isUserMessage ? (
|
{isUserMessage ? (
|
||||||
<UserMessage content={content} />
|
<UserMessage content={content} parts={parts} />
|
||||||
) : (
|
) : (
|
||||||
<AssistantMessage
|
<AssistantMessage
|
||||||
content={content}
|
content={content}
|
||||||
|
|||||||
@@ -6,17 +6,34 @@ import { MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
|
|||||||
import { Markdown } from './Markdown';
|
import { Markdown } from './Markdown';
|
||||||
import { useStore } from '@nanostores/react';
|
import { useStore } from '@nanostores/react';
|
||||||
import { profileStore } from '~/lib/stores/profile';
|
import { profileStore } from '~/lib/stores/profile';
|
||||||
|
import type {
|
||||||
|
TextUIPart,
|
||||||
|
ReasoningUIPart,
|
||||||
|
ToolInvocationUIPart,
|
||||||
|
SourceUIPart,
|
||||||
|
FileUIPart,
|
||||||
|
StepStartUIPart,
|
||||||
|
} from '@ai-sdk/ui-utils';
|
||||||
|
|
||||||
interface UserMessageProps {
|
interface UserMessageProps {
|
||||||
content: string | Array<{ type: string; text?: string; image?: string }>;
|
content: string | Array<{ type: string; text?: string; image?: string }>;
|
||||||
|
parts:
|
||||||
|
| (TextUIPart | ReasoningUIPart | ToolInvocationUIPart | SourceUIPart | FileUIPart | StepStartUIPart)[]
|
||||||
|
| undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function UserMessage({ content }: UserMessageProps) {
|
export function UserMessage({ content, parts }: UserMessageProps) {
|
||||||
|
const profile = useStore(profileStore);
|
||||||
|
|
||||||
|
// Extract images from parts - look for file parts with image mime types
|
||||||
|
const images =
|
||||||
|
parts?.filter(
|
||||||
|
(part): part is FileUIPart => part.type === 'file' && 'mimeType' in part && part.mimeType.startsWith('image/'),
|
||||||
|
) || [];
|
||||||
|
|
||||||
if (Array.isArray(content)) {
|
if (Array.isArray(content)) {
|
||||||
const textItem = content.find((item) => item.type === 'text');
|
const textItem = content.find((item) => item.type === 'text');
|
||||||
const textContent = stripMetadata(textItem?.text || '');
|
const textContent = stripMetadata(textItem?.text || '');
|
||||||
const images = content.filter((item) => item.type === 'image' && item.image);
|
|
||||||
const profile = useStore(profileStore);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="overflow-hidden flex flex-col gap-3 items-center ">
|
<div className="overflow-hidden flex flex-col gap-3 items-center ">
|
||||||
@@ -43,7 +60,7 @@ export function UserMessage({ content }: UserMessageProps) {
|
|||||||
{images.map((item, index) => (
|
{images.map((item, index) => (
|
||||||
<img
|
<img
|
||||||
key={index}
|
key={index}
|
||||||
src={item.image}
|
src={`data:${item.mimeType};base64,${item.data}`}
|
||||||
alt={`Image ${index + 1}`}
|
alt={`Image ${index + 1}`}
|
||||||
className="max-w-full h-auto rounded-lg"
|
className="max-w-full h-auto rounded-lg"
|
||||||
style={{ maxHeight: '512px', objectFit: 'contain' }}
|
style={{ maxHeight: '512px', objectFit: 'contain' }}
|
||||||
@@ -57,7 +74,22 @@ export function UserMessage({ content }: UserMessageProps) {
|
|||||||
const textContent = stripMetadata(content);
|
const textContent = stripMetadata(content);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="overflow-hidden pt-[4px]">
|
<div className="flex flex-col bg-accent-500/10 backdrop-blur-sm px-5 p-3.5 w-auto rounded-lg ml-auto">
|
||||||
|
<div className="flex gap-3.5 mb-4">
|
||||||
|
{images.map((item, index) => (
|
||||||
|
<div className="relative flex rounded-lg border border-bolt-elements-borderColor overflow-hidden">
|
||||||
|
<div className="h-16 w-16 bg-transparent outline-none">
|
||||||
|
<img
|
||||||
|
key={index}
|
||||||
|
src={`data:${item.mimeType};base64,${item.data}`}
|
||||||
|
alt={`Image ${index + 1}`}
|
||||||
|
className="h-full w-full rounded-lg"
|
||||||
|
style={{ objectFit: 'fill' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
<Markdown html>{textContent}</Markdown>
|
<Markdown html>{textContent}</Markdown>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user