@@ -805,7 +805,29 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
805805 return `data:${ mimeType } ;base64,${ fixedBase64Data } ` ;
806806 }
807807
808+ private handleArtifactFetchFailure (
809+ placeholderIndex : number , artifactId : string , versionId : string ) {
810+ this . openSnackBar (
811+ 'Failed to fetch artifact data' ,
812+ 'OK' ,
813+ ) ;
814+ // Remove placeholder message and artifact on failure
815+ this . messages . update (
816+ messages => messages . filter ( ( m , i ) => i !== placeholderIndex ) ) ;
817+ this . artifacts = this . artifacts . filter (
818+ a => a . id !== artifactId || a . versionId !== versionId ) ;
819+ }
820+
808821 private renderArtifact ( artifactId : string , versionId : string ) {
822+ // If artifact/version already exists, do nothing.
823+ const artifactExists = this . artifacts . some (
824+ ( artifact ) =>
825+ artifact . id === artifactId && artifact . versionId === versionId ,
826+ ) ;
827+ if ( artifactExists ) {
828+ return ;
829+ }
830+
809831 // Add a placeholder message for the artifact
810832 // Feed the placeholder with the artifact data after it's fetched
811833 let message = {
@@ -819,8 +841,19 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
819841
820842 const currentMessages = this . messages ( ) ;
821843 const lastMessage = currentMessages [ currentMessages . length - 1 ] ;
822- const currentIndex = lastMessage ?. isLoading ? currentMessages . length - 2 :
823- currentMessages . length - 1 ;
844+ const placeholderIndex = lastMessage ?. isLoading ?
845+ currentMessages . length - 2 :
846+ currentMessages . length - 1 ;
847+
848+ // Add placeholder artifact.
849+ const placeholderArtifact = {
850+ id : artifactId ,
851+ versionId,
852+ data : '' ,
853+ mimeType : 'image/png' ,
854+ mediaType : MediaType . IMAGE ,
855+ } ;
856+ this . artifacts = [ ...this . artifacts , placeholderArtifact ] ;
824857
825858 this . artifactService
826859 . getArtifactVersion (
@@ -830,40 +863,53 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
830863 artifactId ,
831864 versionId ,
832865 )
833- . subscribe ( ( res ) => {
834- const mimeType = res . inlineData . mimeType ;
835- const base64Data =
836- this . formatBase64Data ( res . inlineData . data , mimeType ) ;
866+ . subscribe ( {
867+ next : ( res ) => {
868+ const { mimeType, data} = res . inlineData ?? { } ;
869+ if ( ! mimeType || ! data ) {
870+ this . handleArtifactFetchFailure (
871+ placeholderIndex , artifactId , versionId ) ;
872+ return ;
873+ }
874+ const base64Data =
875+ this . formatBase64Data ( data , mimeType ) ;
837876
838- const mediaType = getMediaTypeFromMimetype ( mimeType ) ;
877+ const mediaType = getMediaTypeFromMimetype ( mimeType ) ;
839878
840- let inlineData = {
841- name : this . createDefaultArtifactName ( mimeType ) ,
842- data : base64Data ,
843- mimeType : mimeType ,
844- mediaType,
845- } ;
846-
847- this . messages . update ( messages => {
848- const newMessages = [ ...messages ] ;
849- newMessages [ currentIndex ] = {
850- role : 'bot' ,
851- inlineData,
879+ const inlineData = {
880+ name : this . createDefaultArtifactName ( mimeType ) ,
881+ data : base64Data ,
882+ mimeType : mimeType ,
883+ mediaType,
852884 } ;
853- return newMessages ;
854- } ) ;
855885
856- // To trigger ngOnChanges in the artifact tab component
857- this . artifacts = [
858- ...this . artifacts ,
859- {
860- id : artifactId ,
861- data : base64Data ,
862- mimeType,
863- versionId,
864- mediaType : getMediaTypeFromMimetype ( mimeType ) ,
865- } ,
866- ] ;
886+ this . messages . update ( messages => {
887+ const newMessages = [ ...messages ] ;
888+ newMessages [ placeholderIndex ] = {
889+ role : 'bot' ,
890+ inlineData,
891+ } ;
892+ return newMessages ;
893+ } ) ;
894+
895+ // Update placeholder artifact with fetched data.
896+ this . artifacts = this . artifacts . map ( artifact => {
897+ if ( artifact . id === artifactId && artifact . versionId === versionId ) {
898+ return {
899+ id : artifactId ,
900+ versionId,
901+ data : base64Data ,
902+ mimeType,
903+ mediaType,
904+ } ;
905+ }
906+ return artifact ;
907+ } ) ;
908+ } ,
909+ error : ( err ) => {
910+ this . handleArtifactFetchFailure (
911+ placeholderIndex , artifactId , versionId ) ;
912+ }
867913 } ) ;
868914 }
869915
0 commit comments