@@ -72,6 +72,11 @@ export class BetaToolRunner<Stream extends boolean> {
7272 this . #completion = promiseWithResolvers ( ) ;
7373 }
7474
75+ get #firstChoiceInCurrentMessage( ) : ChatCompletionMessageParam | null {
76+ const lastMessage = this . #state. params . messages [ this . #state. params . messages . length - 1 ] ;
77+ return lastMessage ?? null ;
78+ }
79+
7580 async * [ Symbol . asyncIterator ] ( ) : AsyncIterator <
7681 Stream extends true ?
7782 ChatCompletionStream // TODO: for now!
@@ -112,18 +117,38 @@ export class BetaToolRunner<Stream extends boolean> {
112117 // this.#message.catch(() => {});
113118 // yield stream as any;
114119 } else {
115- this . #message = this . client . beta . chat . completions . create ( { ...params , stream : false } ) ;
120+ console . log ( 'making request with params:' , JSON . stringify ( params , null , 2 ) ) ;
121+ this . #message = this . client . beta . chat . completions . create ( {
122+ stream : false ,
123+ tools : params . tools ,
124+ messages : params . messages ,
125+ model : params . model ,
126+ } ) ;
127+ console . log ( 'Message created:' , JSON . stringify ( await this . #message, null , 2 ) ) ;
116128 yield this . #message as any ;
117129 }
118130
131+ if ( ! this . #message) {
132+ throw new Error ( 'No message created' ) ; // TODO: use better error
133+ }
134+
135+ // TODO: we should probably hit the user with a callback or somehow offer for them to choice between the choices
136+ const { choices } = await this . #message;
137+
138+ if ( ! this . #firstChoiceInCurrentMessage) {
139+ throw new Error ( 'No choices found in message' ) ; // TODO: use better error
140+ }
141+
142+ const { role : firstChoiceRole , content : firstChoiceContent } = this . #firstChoiceInCurrentMessage;
143+
119144 if ( ! this . #mutated) {
120- const { choices } = await this . #message;
121- const { role, content } = choices [ 0 ] ! . message ; // TODO: ensure there's at least one choice
145+ console . log ( choices ) ;
122146 // this.#state.params.messages.push({ role, content }); TODO: we want to add all
123- this . #state. params . messages . push ( { role , content } ) ;
147+ this . #state. params . messages . push ( this . #firstChoiceInCurrentMessage as ChatCompletionMessageParam ) ;
124148 }
125149
126- const toolMessage = await this . #generateToolResponse( this . #state. params . messages . at ( - 1 ) ! ) ;
150+ const toolMessage = await this . #generateToolResponse( ( await this . #message) . choices [ 0 ] ! ) ;
151+ console . log ( 'Tool message:' , toolMessage ) ;
127152 if ( toolMessage ) {
128153 this . #state. params . messages . push ( toolMessage ) ;
129154 }
@@ -204,15 +229,17 @@ export class BetaToolRunner<Stream extends boolean> {
204229 if ( ! message ) {
205230 return null ;
206231 }
232+ console . log ( "Message:" , message [ 0 ] ) ;
207233 // TODO: this cast is probably bad
208- return this . #generateToolResponse( message as ChatCompletionMessageParam ) ;
234+ return this . #generateToolResponse( message [ 0 ] ) ;
209235 }
210236
211- async #generateToolResponse( lastMessage : ChatCompletionMessageParam ) {
237+ async #generateToolResponse( lastMessage : ChatCompletion . Choice ) {
238+ console . log ( 'Last message:' , lastMessage . message ) ;
212239 if ( this . #toolResponse !== undefined ) {
213240 return this . #toolResponse;
214241 }
215- this . #toolResponse = generateToolResponse ( this . #state. params , lastMessage ) ;
242+ this . #toolResponse = generateToolResponse ( this . #state. params , lastMessage . message ! ) ; // TODO: maybe undefined
216243 return this . #toolResponse;
217244 }
218245
@@ -313,19 +340,19 @@ export class BetaToolRunner<Stream extends boolean> {
313340
314341async function generateToolResponse (
315342 params : BetaToolRunnerParams ,
316- lastMessage = params . messages . at ( - 1 ) ,
343+ lastMessageFirstChoice = params . messages . at ( - 1 ) ,
317344) : Promise < ChatCompletionToolMessageParam | null > {
318345 // Only process if the last message is from the assistant and has tool use blocks
319346 if (
320- ! lastMessage ||
321- lastMessage . role !== 'assistant' ||
322- ! lastMessage . tool_calls ||
323- lastMessage . tool_calls . length === 0
347+ ! lastMessageFirstChoice ||
348+ lastMessageFirstChoice . role !== 'assistant' ||
349+ ! lastMessageFirstChoice . tool_calls ||
350+ lastMessageFirstChoice . tool_calls . length === 0
324351 ) {
325352 return null ;
326353 }
327354
328- const toolUseBlocks = lastMessage . tool_calls . filter ( ( toolCall ) => toolCall . type === 'function' ) ;
355+ const toolUseBlocks = lastMessageFirstChoice . tool_calls . filter ( ( toolCall ) => toolCall . type === 'function' ) ;
329356 if ( toolUseBlocks . length === 0 ) {
330357 return null ;
331358 }
0 commit comments