@@ -16,10 +16,10 @@ import type {
1616 DataPart ,
1717 FilePart ,
1818 IdGenerator ,
19- ToolPart ,
2019 ToolApprovalResponseInput ,
2120 ToolOutputInput ,
2221 ToolOutputSuccessInput ,
22+ ToolPart ,
2323 UIMessage ,
2424 UIMessageChunk ,
2525 UIMessageStreamTerminal ,
@@ -137,7 +137,10 @@ export class Chat<METADATA = unknown> {
137137 }
138138 }
139139
140- async sendMessage ( message ?: SendMessageInput < METADATA > , options ?: ChatRequestOptions ) : Promise < void > {
140+ async sendMessage (
141+ message ?: SendMessageInput < METADATA > ,
142+ options ?: ChatRequestOptions ,
143+ ) : Promise < void > {
141144 if ( message ) {
142145 this . applyUserMessage ( message )
143146 }
@@ -164,7 +167,12 @@ export class Chat<METADATA = unknown> {
164167
165168 const keepUntil = messages [ index ] . role === 'assistant' ? index : index + 1
166169 this . setMessages ( messages . slice ( 0 , keepUntil ) )
167- await this . makeRequest ( { trigger : 'regenerate-message' , messageId, options, requestMessages : messages } )
170+ await this . makeRequest ( {
171+ trigger : 'regenerate-message' ,
172+ messageId,
173+ options,
174+ requestMessages : messages ,
175+ } )
168176 }
169177
170178 stop ( ) : void {
@@ -178,7 +186,12 @@ export class Chat<METADATA = unknown> {
178186 }
179187 return assistant . parts
180188 . filter ( isToolPart )
181- . every ( ( part ) => part . state === 'output-available' || part . state === 'output-error' || part . state === 'output-denied' )
189+ . every (
190+ ( part ) =>
191+ part . state === 'output-available' ||
192+ part . state === 'output-error' ||
193+ part . state === 'output-denied' ,
194+ )
182195 }
183196
184197 private async appendToolOutputSuccess (
@@ -188,11 +201,11 @@ export class Chat<METADATA = unknown> {
188201 result : unknown
189202 providerMetadata ?: Record < string , unknown >
190203 } ,
191- options ?: ChatRequestOptions
204+ options ?: ChatRequestOptions ,
192205 ) : Promise < void > {
193206 const tool = this . resolveToolCall ( input . toolCallId , input . toolName )
194207 this . updateLastAssistant ( ( message ) =>
195- withToolOutput ( message , { ...input , toolName : tool . toolName } )
208+ withToolOutput ( message , { ...input , toolName : tool . toolName } ) ,
196209 )
197210 await this . maybeSendAutomatically ( options )
198211 }
@@ -204,11 +217,11 @@ export class Chat<METADATA = unknown> {
204217 errorText : string
205218 providerMetadata ?: Record < string , unknown >
206219 } ,
207- options ?: ChatRequestOptions
220+ options ?: ChatRequestOptions ,
208221 ) : Promise < void > {
209222 const tool = this . resolveToolCall ( input . toolCallId , input . toolName )
210223 this . updateLastAssistant ( ( message ) =>
211- withToolError ( message , { ...input , toolName : tool . toolName } )
224+ withToolError ( message , { ...input , toolName : tool . toolName } ) ,
212225 )
213226 await this . maybeSendAutomatically ( options )
214227 }
@@ -223,7 +236,7 @@ export class Chat<METADATA = unknown> {
223236 errorText : input . errorText ,
224237 providerMetadata : input . providerMetadata ,
225238 } ,
226- options
239+ options ,
227240 )
228241 return
229242 }
@@ -235,20 +248,20 @@ export class Chat<METADATA = unknown> {
235248 result : outputInput . output ?? outputInput . result ,
236249 providerMetadata : outputInput . providerMetadata ,
237250 } ,
238- options
251+ options ,
239252 )
240253 }
241254
242255 async rejectToolCall (
243256 input : Omit < ToolApprovalResponseInput , 'approved' > & { approved ?: false } ,
244- options ?: ChatRequestOptions
257+ options ?: ChatRequestOptions ,
245258 ) : Promise < void > {
246259 await this . addToolApprovalResponse ( { ...input , approved : false } , options )
247260 }
248261
249262 async addToolApprovalResponse (
250263 input : ToolApprovalResponseInput ,
251- options ?: ChatRequestOptions
264+ options ?: ChatRequestOptions ,
252265 ) : Promise < void > {
253266 const approval =
254267 input . id || input . approvalId
@@ -267,7 +280,7 @@ export class Chat<METADATA = unknown> {
267280 approved : input . approved ,
268281 reason : input . reason ,
269282 providerMetadata : input . providerMetadata ,
270- } )
283+ } ) ,
271284 )
272285 await this . maybeSendAutomatically ( options )
273286 }
@@ -289,10 +302,7 @@ export class Chat<METADATA = unknown> {
289302 if ( messages [ index ] . role !== 'user' ) {
290303 throw new Error ( `message with id ${ message . messageId } is not a user message` )
291304 }
292- this . setMessages ( [
293- ...messages . slice ( 0 , index ) ,
294- { ...nextMessage , id : message . messageId } ,
295- ] )
305+ this . setMessages ( [ ...messages . slice ( 0 , index ) , { ...nextMessage , id : message . messageId } ] )
296306 return
297307 }
298308
@@ -352,7 +362,12 @@ export class Chat<METADATA = unknown> {
352362 } ) ,
353363 } )
354364
355- if ( this . status === 'ready' && ! outcome . isError && allowAutoSubmit && ( await this . shouldSendAutomatically ( ) ) ) {
365+ if (
366+ this . status === 'ready' &&
367+ ! outcome . isError &&
368+ allowAutoSubmit &&
369+ ( await this . shouldSendAutomatically ( ) )
370+ ) {
356371 await this . makeRequest ( {
357372 trigger : 'submit-message' ,
358373 messageId : this . messages [ this . messages . length - 1 ] ?. id ,
@@ -433,7 +448,7 @@ export class Chat<METADATA = unknown> {
433448
434449 private applyAssistantChunk (
435450 reducer : ReturnType < typeof createUIMessageReducer < METADATA > > ,
436- chunk : UIMessageChunk
451+ chunk : UIMessageChunk ,
437452 ) {
438453 applyUIMessageChunk ( reducer , chunk )
439454 if ( isDataChunk ( chunk ) ) {
@@ -579,7 +594,7 @@ function findLastIndex<T>(items: T[], predicate: (item: T) => boolean): number {
579594
580595function findLastToolPart < METADATA > (
581596 messages : UIMessage < METADATA > [ ] ,
582- toolCallId : string
597+ toolCallId : string ,
583598) : { toolName : string } | undefined {
584599 for ( let messageIndex = messages . length - 1 ; messageIndex >= 0 ; messageIndex -= 1 ) {
585600 const message = messages [ messageIndex ]
@@ -598,7 +613,7 @@ function findLastToolPart<METADATA>(
598613
599614function findLastApprovalRequest < METADATA > (
600615 messages : UIMessage < METADATA > [ ] ,
601- approvalId : string
616+ approvalId : string ,
602617) : ToolPart | undefined {
603618 for ( let messageIndex = messages . length - 1 ; messageIndex >= 0 ; messageIndex -= 1 ) {
604619 const message = messages [ messageIndex ]
@@ -623,13 +638,15 @@ function isToolPart(part: UIMessage['parts'][number]): part is ToolPart {
623638 return part . type . startsWith ( 'tool-' )
624639}
625640
626- function isDataChunk ( chunk : UIMessageChunk ) : chunk is Extract < UIMessageChunk , { type : `data-${string } ` } > {
641+ function isDataChunk (
642+ chunk : UIMessageChunk ,
643+ ) : chunk is Extract < UIMessageChunk , { type : `data-${string } ` } > {
627644 return chunk . type . startsWith ( 'data-' )
628645}
629646
630647function dataPartFromChunk < METADATA > (
631648 message : UIMessage < METADATA > ,
632- chunk : Extract < UIMessageChunk , { type : `data-${string } ` } >
649+ chunk : Extract < UIMessageChunk , { type : `data-${string } ` } > ,
633650) : DataPart {
634651 if ( chunk . transient ) {
635652 return {
@@ -642,17 +659,17 @@ function dataPartFromChunk<METADATA>(
642659 }
643660 const part = message . parts . find (
644661 ( item ) : item is DataPart =>
645- item . type === chunk . type &&
646- item . type . startsWith ( 'data-' ) &&
647- item . id === chunk . id
662+ item . type === chunk . type && item . type . startsWith ( 'data-' ) && item . id === chunk . id ,
663+ )
664+ return (
665+ part ?? {
666+ type : chunk . type ,
667+ id : chunk . id ,
668+ name : chunk . name ,
669+ data : chunk . data ,
670+ transientData : false ,
671+ }
648672 )
649- return part ?? {
650- type : chunk . type ,
651- id : chunk . id ,
652- name : chunk . name ,
653- data : chunk . data ,
654- transientData : false ,
655- }
656673}
657674
658675export function lastAssistantMessageIsCompleteWithApprovalResponses < METADATA = unknown > ( {
@@ -666,10 +683,10 @@ export function lastAssistantMessageIsCompleteWithApprovalResponses<METADATA = u
666683 }
667684 const approvalParts = assistant . parts . filter (
668685 ( part ) : part is ToolPart =>
669- isToolPart ( part ) && ( part . state === 'approval-requested' || part . state === 'approval-responded' )
686+ isToolPart ( part ) &&
687+ ( part . state === 'approval-requested' || part . state === 'approval-responded' ) ,
670688 )
671689 return (
672- approvalParts . length > 0 &&
673- approvalParts . every ( ( part ) => part . state === 'approval-responded' )
690+ approvalParts . length > 0 && approvalParts . every ( ( part ) => part . state === 'approval-responded' )
674691 )
675692}
0 commit comments