@@ -59,21 +59,30 @@ async function main() {
5959 const streamEventMessages = await client . runs . create ( thread . id , agent . id ) . stream ( ) ;
6060
6161 for await ( const eventMessage of streamEventMessages ) {
62- switch ( eventMessage . event ) {
63- case RunStreamEvent . ThreadRunCreated :
64- console . log ( `ThreadRun status: ${ eventMessage ?. data ?. status } ` ) ;
62+ switch ( eventMessage . event ) { case RunStreamEvent . ThreadRunCreated :
63+ // Type check or cast to access the status property safely
64+ if ( typeof eventMessage . data === 'object' && eventMessage . data !== null && 'status' in eventMessage . data ) {
65+ console . log ( `ThreadRun status: ${ eventMessage . data . status } ` ) ;
66+ } else {
67+ console . log ( `ThreadRun created: ${ JSON . stringify ( eventMessage . data ) } ` ) ;
68+ }
6569 break ;
6670 case MessageStreamEvent . ThreadMessageDelta :
6771 {
68-
6972 const messageDelta = eventMessage . data ;
70- messageDelta ?. delta ?. content . forEach ( ( contentPart ) => {
71- if ( contentPart . type === "text" ) {
72- const textContent = contentPart ;
73- const textValue = textContent . text ?. value || "No text" ;
74- console . log ( `Text delta received:: ${ textValue } ` ) ;
75- }
76- } ) ;
73+ // Type check or cast to access the delta property safely
74+ if ( typeof messageDelta === 'object' && messageDelta !== null && 'delta' in messageDelta &&
75+ messageDelta . delta && 'content' in messageDelta . delta && Array . isArray ( messageDelta . delta . content ) ) {
76+ messageDelta . delta . content . forEach ( ( contentPart ) => {
77+ if ( contentPart . type === "text" ) { const textContent = contentPart ;
78+ // Add type guard for text content
79+ if ( 'text' in textContent && textContent . text && typeof textContent . text === 'object' ) {
80+ const textValue = textContent . text . value || "No text" ;
81+ console . log ( `Text delta received:: ${ textValue } ` ) ;
82+ }
83+ }
84+ } ) ;
85+ }
7786 }
7887 break ;
7988
@@ -100,22 +109,39 @@ async function main() {
100109 messagesArray . push ( m ) ;
101110 }
102111 console . log ( "Messages:" , messagesArray ) ;
103-
104112 // Get most recent message from the assistant
105113 const assistantMessage = messagesArray . find ( ( msg ) => msg . role === "assistant" ) ;
106114 if ( assistantMessage ) {
107115 const textContent = assistantMessage . content . find ( ( content ) => isOutputOfType ( content , "text" ) ) ;
108116 if ( textContent ) {
109117 // Save the newly created file
110118 console . log ( `Saving new files...` ) ;
111- const imageFileOutput = messagesArray [ 0 ] . content [ 0 ] ;
112- const imageFile = imageFileOutput ?. imageFile ?. fileId ;
119+ const imageFileOutput = messagesArray [ 0 ] . content [ 0 ] ; // Use type checking to safely access the imageFile property
120+ let imageFileId = '' ;
121+
122+ // Check if content has image file type and has the correct structure
123+ if ( isOutputOfType ( imageFileOutput , "image_file" ) &&
124+ 'image_file' in imageFileOutput &&
125+ imageFileOutput . image_file &&
126+ typeof imageFileOutput . image_file === 'object' ) {
127+ // Use type assertion after validating the structure
128+ const typedImageFile = imageFileOutput . image_file as { fileId : string } ;
129+ if ( 'fileId' in typedImageFile && typeof typedImageFile . fileId === 'string' ) {
130+ imageFileId = typedImageFile . fileId ;
131+ }
132+ }
133+
134+ if ( ! imageFileId ) {
135+ console . log ( "No image file found in the message content" ) ;
136+ return ;
137+ }
138+
113139 const imageFileName = path . resolve (
114- "./data/" + ( await client . files . get ( imageFile ) ) . filename + "ImageFile.png" ,
140+ "./data/" + ( await client . files . get ( imageFileId ) ) . filename + "ImageFile.png" ,
115141 ) ;
116142 console . log ( `Image file name : ${ imageFileName } ` ) ;
117143
118- const fileContent = await ( await client . files . getContent ( imageFile ) . asNodeStream ( ) ) . body ;
144+ const fileContent = await ( await client . files . getContent ( imageFileId ) . asNodeStream ( ) ) . body ;
119145 if ( fileContent ) {
120146 const chunks = [ ] ;
121147 for await ( const chunk of fileContent ) {
@@ -133,10 +159,12 @@ async function main() {
133159 console . log ( `Message Details:` ) ;
134160 messagesArray . forEach ( ( m ) => {
135161 console . log ( `File Paths:` ) ;
136- console . log ( `Type: ${ m . content [ 0 ] . type } ` ) ;
137- if ( isOutputOfType ( m . content [ 0 ] , "text" ) ) {
162+ console . log ( `Type: ${ m . content [ 0 ] . type } ` ) ; if ( isOutputOfType ( m . content [ 0 ] , "text" ) ) {
138163 const textContent = m . content [ 0 ] ;
139- console . log ( `Text: ${ textContent ?. text ?. value } ` ) ;
164+ // Use type guard to safely access text property
165+ if ( 'text' in textContent && textContent . text && typeof textContent . text === 'object' && 'value' in textContent . text ) {
166+ console . log ( `Text: ${ textContent . text . value } ` ) ;
167+ }
140168 }
141169 console . log ( `File ID: ${ m . id } ` ) ;
142170 // firstId and lastId are properties of the paginator, not the messages array
0 commit comments