@@ -2,6 +2,10 @@ import { chatConfig } from "@/const/chatConfig";
22import { useState } from "react" ;
33import { useTranslation } from "react-i18next" ;
44import { ExternalLink } from "lucide-react" ;
5+ import { storageService } from "@/services/storageService" ;
6+ import { ChatMessageType } from "@/types/chat" ;
7+ import { ROLE_ASSISTANT } from "@/const/agentConfig" ;
8+ import log from "@/lib/logger" ;
59import {
610 AiFillFileImage ,
711 AiFillFilePdf ,
@@ -316,3 +320,84 @@ export function ChatAttachment({
316320 </ div >
317321 ) ;
318322}
323+
324+ /**
325+ * Asynchronously load attachment URLs for messages
326+ * @param messages Array of chat messages
327+ * @param targetConversationId Target conversation ID
328+ * @param messageManagement Message management hook instance
329+ * @param t Translation function
330+ * @returns Promise that resolves when all URLs are loaded
331+ */
332+ export const loadAttachmentUrls = async (
333+ messages : ChatMessageType [ ] ,
334+ targetConversationId : number ,
335+ messageManagement : any ,
336+ t : any
337+ ) : Promise < void > => {
338+ // Create a copy to avoid directly modifying parameters
339+ const updatedMessages = [ ...messages ] ;
340+ let hasUpdates = false ;
341+
342+ // Process attachments for each message
343+ for ( const message of updatedMessages ) {
344+ if ( message . attachments && message . attachments . length > 0 ) {
345+ // Get URL for each attachment
346+ for ( const attachment of message . attachments ) {
347+ if ( attachment . object_name && ! attachment . url ) {
348+ try {
349+ // Get file URL
350+ const url = await storageService . getFileUrl (
351+ attachment . object_name
352+ ) ;
353+ // Update attachment info
354+ attachment . url = url ;
355+ hasUpdates = true ;
356+ } catch ( error ) {
357+ log . error (
358+ t ( "chatInterface.errorFetchingAttachmentUrl" , {
359+ object_name : attachment . object_name ,
360+ } ) ,
361+ error
362+ ) ;
363+ }
364+ }
365+ }
366+ }
367+ }
368+
369+ // If there are updates, set new message array
370+ if ( hasUpdates ) {
371+ messageManagement . setMessages ( targetConversationId , updatedMessages ) ;
372+ }
373+ } ;
374+
375+ /**
376+ * Handle image loading errors
377+ * @param imageUrl Failed image URL
378+ * @param messageManagement Message management hook
379+ * @param conversationId Current conversation ID
380+ * @param t Translation function
381+ */
382+ export const handleImageError = (
383+ imageUrl : string ,
384+ messageManagement : any ,
385+ conversationId : number ,
386+ t : any
387+ ) => {
388+ log . error ( t ( "chatInterface.imageLoadFailed" ) , imageUrl ) ;
389+
390+ // Remove failed images from messages
391+ const messages = messageManagement . getMessages ( conversationId ) ;
392+ if ( messages . length === 0 ) return ;
393+
394+ const lastMsg = messages [ messages . length - 1 ] ;
395+ if ( lastMsg . role === ROLE_ASSISTANT && lastMsg . images ) {
396+ // Filter out failed images
397+ const updatedImages = lastMsg . images . filter ( ( url : string ) => url !== imageUrl ) ;
398+ messageManagement . updateMessage ( conversationId , lastMsg . id , {
399+ ...lastMsg ,
400+ images : updatedImages ,
401+ } ) ;
402+ }
403+ } ;
0 commit comments