@@ -9,6 +9,7 @@ import { useEncryption } from "@/hooks/useEncryption";
99import { LocalStorageService } from "@/services/LocalStorage" ;
1010import { useStreamingChat } from "../../hooks/useStreamingChat" ;
1111import type { ChatMessage as MessageType } from "../../types/chat" ;
12+ import type { TMessageAttachment } from "../../types/schemas" ;
1213import ChatInput from "./ChatInput" ;
1314import ChatMessage from "./ChatMessage" ;
1415
@@ -209,12 +210,15 @@ const StreamingChatArea: React.FC<StreamingChatAreaProps> = ({
209210 }
210211 } ;
211212
212- const createMessage = async (
213- chatId : string ,
214- role : "user" | "assistant" ,
215- content : string ,
216- order : number ,
217- ) => {
213+ const createMessage = async ( message : {
214+ chatId : string ;
215+ role : "user" | "assistant" ;
216+ content : string ;
217+ order : number ;
218+ attachments ?: TMessageAttachment [ ] ;
219+ } ) => {
220+ const { chatId, role, content, order, attachments } = message ;
221+
218222 if ( ! user ) {
219223 console . error ( "No authenticated user found" ) ;
220224 return ;
@@ -246,6 +250,7 @@ const StreamingChatArea: React.FC<StreamingChatAreaProps> = ({
246250 model : DEFAULT_MODEL ,
247251 creator : user ?. id ,
248252 blindfoldContent : blindfoldContent ,
253+ attachments,
249254 } ) ,
250255 } ) ;
251256
@@ -317,10 +322,17 @@ const StreamingChatArea: React.FC<StreamingChatAreaProps> = ({
317322 ? userMessage . content
318323 : userMessage . content . find ( ( content ) => content . type === "text" )
319324 ?. text || "" ;
325+ const userMessageAttachments : TMessageAttachment [ ] = imageDataUrl
326+ ? [ "image" ]
327+ : [ ] ;
320328
321329 setMessages ( ( prev ) => [
322330 ...prev ,
323- { ...userMessage , content : userMessageContentText } ,
331+ {
332+ ...userMessage ,
333+ content : userMessageContentText ,
334+ attachments : userMessageAttachments ,
335+ } ,
324336 ] ) ;
325337
326338 setTimeout ( ( ) => scrollToBottom ( true ) , 100 ) ;
@@ -366,37 +378,39 @@ const StreamingChatArea: React.FC<StreamingChatAreaProps> = ({
366378 chatIdRef . current = newChatId . message ;
367379
368380 // Add the two messages to the chat
369- await createMessage (
370- newChatId . message ,
371- "user" ,
372- userMessageContentText ,
373- 1 ,
374- ) ;
375- await createMessage (
376- newChatId . message ,
377- "assistant" ,
378- finalContent ,
379- 2 ,
380- ) ;
381+ await createMessage ( {
382+ chatId : newChatId . message ,
383+ role : "user" ,
384+ content : userMessageContentText ,
385+ order : 1 ,
386+ attachments : userMessageAttachments ,
387+ } ) ;
388+ await createMessage ( {
389+ chatId : newChatId . message ,
390+ role : "assistant" ,
391+ content : finalContent ,
392+ order : 2 ,
393+ } ) ;
381394 }
382395
383396 if ( totalMessages === 4 ) {
384397 const currentChatId = chatIdRef . current || chatId ;
385398
386399 if ( currentChatId ) {
387400 await Promise . all ( [
388- createMessage (
389- currentChatId ,
390- "user" ,
391- userMessageContentText ,
392- totalMessages - 1 ,
393- ) ,
394- createMessage (
395- currentChatId ,
396- "assistant" ,
397- finalContent ,
398- totalMessages ,
399- ) ,
401+ createMessage ( {
402+ chatId : currentChatId ,
403+ role : "user" ,
404+ content : userMessageContentText ,
405+ order : totalMessages - 1 ,
406+ attachments : userMessageAttachments ,
407+ } ) ,
408+ createMessage ( {
409+ chatId : currentChatId ,
410+ role : "assistant" ,
411+ content : finalContent ,
412+ order : totalMessages ,
413+ } ) ,
400414 ] ) ;
401415
402416 setIsUpdatingChat ( true ) ;
@@ -426,22 +440,23 @@ const StreamingChatArea: React.FC<StreamingChatAreaProps> = ({
426440 await Promise . all ( [
427441 // ODD (User)
428442 ( totalMessages - 1 ) % 2 === 1
429- ? createMessage (
430- currentChatId ,
431- "user" ,
432- userMessageContentText ,
433- totalMessages - 1 ,
434- )
443+ ? createMessage ( {
444+ chatId : currentChatId ,
445+ role : "user" ,
446+ content : userMessageContentText ,
447+ order : totalMessages - 1 ,
448+ attachments : userMessageAttachments ,
449+ } )
435450 : Promise . resolve ( ) ,
436451
437452 // EVEN (Assistant)
438453 totalMessages % 2 === 0
439- ? createMessage (
440- currentChatId ,
441- "assistant" ,
442- finalContent ,
443- totalMessages ,
444- )
454+ ? createMessage ( {
455+ chatId : currentChatId ,
456+ role : "assistant" ,
457+ content : finalContent ,
458+ order : totalMessages ,
459+ } )
445460 : Promise . resolve ( ) ,
446461 ] ) ;
447462
0 commit comments