@@ -105,11 +105,14 @@ const messageActionsTemplate = (msg: any) => {
105105 : html `` ;
106106} ;
107107
108+ const _composingIndicatorTemplate = html `< span > LOADING...</ span > ` ;
109+
108110const ai_chat_options = {
109111 headerText : 'Chat' ,
110112 suggestions : [ 'Hello' , 'Hi' , 'Generate an image of a pig!' ] ,
111113 templates : {
112114 messageActionsTemplate : messageActionsTemplate ,
115+ //composingIndicatorTemplate: _composingIndicatorTemplate,
113116 } ,
114117} ;
115118
@@ -414,92 +417,94 @@ async function handleAIMessageSend(e: CustomEvent) {
414417 return ;
415418 }
416419
417- chat . options = { ...ai_chat_options , suggestions : [ ] } ;
418-
419- let response : any ;
420- let responseText = '' ;
421- const attachments : IgcMessageAttachment [ ] = [ ] ;
422- const botResponse : IgcMessage = {
423- id : Date . now ( ) . toString ( ) ,
424- text : responseText ,
425- sender : 'bot' ,
426- timestamp : new Date ( ) ,
427- } ;
420+ chat . options = { ...ai_chat_options , suggestions : [ ] , isComposing : true } ;
421+ setTimeout ( async ( ) => {
422+ chat . options = { ...ai_chat_options , suggestions : [ ] , isComposing : false } ;
423+ let response : any ;
424+ let responseText = '' ;
425+ const attachments : IgcMessageAttachment [ ] = [ ] ;
426+ const botResponse : IgcMessage = {
427+ id : Date . now ( ) . toString ( ) ,
428+ text : responseText ,
429+ sender : 'bot' ,
430+ timestamp : new Date ( ) ,
431+ } ;
428432
429- userMessages . push ( { role : 'user' , parts : [ { text : newMessage . text } ] } ) ;
433+ userMessages . push ( { role : 'user' , parts : [ { text : newMessage . text } ] } ) ;
430434
431- if ( newMessage . attachments && newMessage . attachments . length > 0 ) {
432- for ( const attachment of newMessage . attachments ) {
433- if ( attachment . file ) {
434- const filePart = fileToGenerativePart (
435- await attachment . file . arrayBuffer ( ) ,
436- attachment . file . type
437- ) ;
438- userMessages . push ( { role : 'user' , parts : [ filePart ] } ) ;
435+ if ( newMessage . attachments && newMessage . attachments . length > 0 ) {
436+ for ( const attachment of newMessage . attachments ) {
437+ if ( attachment . file ) {
438+ const filePart = fileToGenerativePart (
439+ await attachment . file . arrayBuffer ( ) ,
440+ attachment . file . type
441+ ) ;
442+ userMessages . push ( { role : 'user' , parts : [ filePart ] } ) ;
443+ }
439444 }
440445 }
441- }
442446
443- if ( newMessage . text . includes ( 'image' ) ) {
444- response = await ai . models . generateContent ( {
445- model : 'gemini-2.0-flash-preview-image-generation' ,
446- contents : userMessages ,
447- config : {
448- responseModalities : [ Modality . TEXT , Modality . IMAGE ] ,
449- } ,
450- } ) ;
451-
452- for ( const part of response ?. candidates ?. [ 0 ] ?. content ?. parts || [ ] ) {
453- // Based on the part type, either show the text or save the image
454- if ( part . text ) {
455- responseText = part . text ;
456- } else if ( part . inlineData ) {
457- const _imageData = part . inlineData . data ;
458- const byteCharacters = atob ( _imageData ) ;
459- const byteNumbers = new Array ( byteCharacters . length ) ;
460- for ( let i = 0 ; i < byteCharacters . length ; i ++ ) {
461- byteNumbers [ i ] = byteCharacters . charCodeAt ( i ) ;
447+ if ( newMessage . text . includes ( 'image' ) ) {
448+ response = await ai . models . generateContent ( {
449+ model : 'gemini-2.0-flash-preview-image-generation' ,
450+ contents : userMessages ,
451+ config : {
452+ responseModalities : [ Modality . TEXT , Modality . IMAGE ] ,
453+ } ,
454+ } ) ;
455+
456+ for ( const part of response ?. candidates ?. [ 0 ] ?. content ?. parts || [ ] ) {
457+ // Based on the part type, either show the text or save the image
458+ if ( part . text ) {
459+ responseText = part . text ;
460+ } else if ( part . inlineData ) {
461+ const _imageData = part . inlineData . data ;
462+ const byteCharacters = atob ( _imageData ) ;
463+ const byteNumbers = new Array ( byteCharacters . length ) ;
464+ for ( let i = 0 ; i < byteCharacters . length ; i ++ ) {
465+ byteNumbers [ i ] = byteCharacters . charCodeAt ( i ) ;
466+ }
467+ const byteArray = new Uint8Array ( byteNumbers ) ;
468+ const type = part . inlineData . type || 'image/png' ;
469+ const blob = new Blob ( [ byteArray ] , { type : type } ) ;
470+ const file = new File ( [ blob ] , 'generated_image.png' , {
471+ type : type ,
472+ } ) ;
473+ const attachment : IgcMessageAttachment = {
474+ id : Date . now ( ) . toString ( ) ,
475+ name : 'generated_image.png' ,
476+ type : 'image' ,
477+ url : URL . createObjectURL ( file ) ,
478+ file : file ,
479+ } ;
480+ attachments . push ( attachment ) ;
462481 }
463- const byteArray = new Uint8Array ( byteNumbers ) ;
464- const type = part . inlineData . type || 'image/png' ;
465- const blob = new Blob ( [ byteArray ] , { type : type } ) ;
466- const file = new File ( [ blob ] , 'generated_image.png' , {
467- type : type ,
468- } ) ;
469- const attachment : IgcMessageAttachment = {
470- id : Date . now ( ) . toString ( ) ,
471- name : 'generated_image.png' ,
472- type : 'image' ,
473- url : URL . createObjectURL ( file ) ,
474- file : file ,
475- } ;
476- attachments . push ( attachment ) ;
477482 }
478- }
479483
480- botResponse . text = responseText ;
481- botResponse . attachments = attachments ;
482- chat . messages = [ ...chat . messages , botResponse ] ;
483- } else {
484- chat . messages = [ ...chat . messages , botResponse ] ;
485- response = await ai . models . generateContentStream ( {
486- model : 'gemini-2.0-flash' ,
487- contents : userMessages ,
488- config : {
489- responseModalities : [ Modality . TEXT ] ,
490- } ,
491- } ) ;
484+ botResponse . text = responseText ;
485+ botResponse . attachments = attachments ;
486+ chat . messages = [ ...chat . messages , botResponse ] ;
487+ } else {
488+ chat . messages = [ ...chat . messages , botResponse ] ;
489+ response = await ai . models . generateContentStream ( {
490+ model : 'gemini-2.0-flash' ,
491+ contents : userMessages ,
492+ config : {
493+ responseModalities : [ Modality . TEXT ] ,
494+ } ,
495+ } ) ;
492496
493- const lastMessageIndex = chat . messages . length - 1 ;
494- for await ( const chunk of response ) {
495- chat . messages [ lastMessageIndex ] = {
496- ...chat . messages [ lastMessageIndex ] ,
497- text : `${ chat . messages [ lastMessageIndex ] . text } ${ chunk . text } ` ,
498- } ;
499- chat . messages = [ ...chat . messages ] ;
497+ const lastMessageIndex = chat . messages . length - 1 ;
498+ for await ( const chunk of response ) {
499+ chat . messages [ lastMessageIndex ] = {
500+ ...chat . messages [ lastMessageIndex ] ,
501+ text : `${ chat . messages [ lastMessageIndex ] . text } ${ chunk . text } ` ,
502+ } ;
503+ chat . messages = [ ...chat . messages ] ;
504+ }
505+ chat . options = { ...ai_chat_options , suggestions : [ 'Thank you!' ] } ;
500506 }
501- chat . options = { ...ai_chat_options , suggestions : [ 'Thank you!' ] } ;
502- }
507+ } , 2000 ) ;
503508}
504509
505510export const Basic : Story = {
0 commit comments