adding to display the image in the chat conversation. and paste image too. tnx to @Stijnus

This commit is contained in:
Andrew Trokhymenko 2024-12-02 14:08:41 -05:00
parent ccaa67b6b2
commit 0ab334126a
2 changed files with 61 additions and 7 deletions

View File

@ -190,6 +190,35 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
input.click(); input.click();
}; };
const handlePaste = async (e: React.ClipboardEvent) => {
const items = e.clipboardData?.items;
if (!items) {
return;
}
for (const item of items) {
if (item.type.startsWith('image/')) {
e.preventDefault();
const file = item.getAsFile();
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const base64Image = e.target?.result as string;
setUploadedFiles?.([...uploadedFiles, file]);
setImageDataList?.([...imageDataList, base64Image]);
};
reader.readAsDataURL(file);
}
break;
}
}
};
const baseChat = ( const baseChat = (
<div <div
ref={ref} ref={ref}
@ -286,6 +315,7 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
onChange={(event) => { onChange={(event) => {
handleInputChange?.(event); handleInputChange?.(event);
}} }}
onPaste={handlePaste}
style={{ style={{
minHeight: TEXTAREA_MIN_HEIGHT, minHeight: TEXTAREA_MIN_HEIGHT,
maxHeight: TEXTAREA_MAX_HEIGHT, maxHeight: TEXTAREA_MAX_HEIGHT,

View File

@ -6,10 +6,39 @@ import { MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
import { Markdown } from './Markdown'; import { Markdown } from './Markdown';
interface UserMessageProps { interface UserMessageProps {
content: string; content: string | Array<{ type: string; text?: string; image?: string }>;
} }
export function UserMessage({ content }: UserMessageProps) { export function UserMessage({ content }: UserMessageProps) {
if (Array.isArray(content)) {
const textItem = content.find((item) => item.type === 'text');
const textContent = sanitizeUserMessage(textItem?.text || '');
const images = content.filter((item) => item.type === 'image' && item.image);
return (
<div className="overflow-hidden pt-[4px]">
<div className="flex items-start gap-4">
<div className="flex-1">
<Markdown limitedMarkdown>{textContent}</Markdown>
</div>
{images.length > 0 && (
<div className="flex-shrink-0 w-[160px]">
{images.map((item, index) => (
<div key={index} className="relative">
<img
src={item.image}
alt={`Uploaded image ${index + 1}`}
className="w-full h-[160px] rounded-lg object-cover border border-bolt-elements-borderColor"
/>
</div>
))}
</div>
)}
</div>
</div>
);
}
const textContent = sanitizeUserMessage(content); const textContent = sanitizeUserMessage(content);
return ( return (
@ -19,11 +48,6 @@ export function UserMessage({ content }: UserMessageProps) {
); );
} }
function sanitizeUserMessage(content: string | Array<{ type: string; text?: string; image_url?: { url: string } }>) { function sanitizeUserMessage(content: string) {
if (Array.isArray(content)) {
const textItem = content.find((item) => item.type === 'text');
return textItem?.text?.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '') || '';
}
return content.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, ''); return content.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
} }