@@ -96,9 +96,7 @@ export class BetaToolRunner<Stream extends boolean>
9696 }
9797
9898 async * [ Symbol . asyncIterator ] ( ) : AsyncIterator <
99- Stream extends true ?
100- ChatCompletionStream // TODO: for now!
101- : ChatCompletion
99+ Stream extends true ? ChatCompletionStream : ChatCompletion
102100 > {
103101 if ( this . #consumed) {
104102 throw new OpenAIError ( 'Cannot iterate over a consumed stream' ) ;
@@ -241,7 +239,7 @@ export class BetaToolRunner<Stream extends boolean>
241239 * }
242240 */
243241 async generateToolResponse ( ) {
244- // The most recent message from the assistant. TODO: do we want || this.params.messages.at(-1)?
242+ // The most recent message from the assistant.
245243 const message = await this . #message;
246244 if ( ! message ) {
247245 return null ;
@@ -281,7 +279,7 @@ export class BetaToolRunner<Stream extends boolean>
281279 * console.log('Final response:', finalMessage.content);
282280 */
283281 done ( ) : Promise < ChatCompletionMessage > {
284- return this . #completion. promise ; // TODO: find a more type safe way to do this
282+ return this . #completion. promise ;
285283 }
286284
287285 /**
@@ -351,7 +349,7 @@ export class BetaToolRunner<Stream extends boolean>
351349 * Makes the ToolRunner directly awaitable, equivalent to calling .runUntilDone()
352350 * This allows using `await runner` instead of `await runner.runUntilDone()`
353351 */
354- then < TResult1 = ChatCompletionMessage , TResult2 = never > ( // TODO: make sure these types are OK
352+ then < TResult1 = ChatCompletionMessage , TResult2 = never > (
355353 onfulfilled ?: ( ( value : ChatCompletionMessage ) => TResult1 | PromiseLike < TResult1 > ) | undefined | null ,
356354 onrejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | undefined | null ,
357355 ) : Promise < TResult1 | TResult2 > {
@@ -364,12 +362,7 @@ async function generateToolResponse(
364362 tools : BetaRunnableChatCompletionFunctionTool < any > [ ] ,
365363) : Promise < null | ChatCompletionToolMessageParam [ ] > {
366364 // Only process if the last message is from the assistant and has tool use blocks
367- if (
368- ! lastMessage ||
369- lastMessage . role !== 'assistant' ||
370- // !lastMessage.content || TODO: openai doesn't give content at the same time. ensure this is really always true though
371- typeof lastMessage . content === 'string'
372- ) {
365+ if ( ! lastMessage || lastMessage . role !== 'assistant' || typeof lastMessage . content === 'string' ) {
373366 return null ;
374367 }
375368
@@ -382,7 +375,7 @@ async function generateToolResponse(
382375 return (
383376 await Promise . all (
384377 prevToolCalls . map ( async ( toolUse ) => {
385- if ( toolUse . type !== 'function' ) return ; // TODO: what about other calls?
378+ if ( toolUse . type !== 'function' ) return ; // TODO: eventually we should support additional tool call types
386379
387380 const tool = tools . find (
388381 ( t ) => t . type === 'function' && toolUse . function . name === t . function . name ,
@@ -398,14 +391,7 @@ async function generateToolResponse(
398391 }
399392
400393 try {
401- let input = toolUse . function . arguments ;
402- // TODO: is this always safe?
403- if ( typeof input === 'string' ) {
404- input = JSON . parse ( input ) ;
405- }
406- input = tool . parse ( input ) ;
407-
408- const result = await tool . run ( input ) ;
394+ const result = await tool . run ( tool . parse ( JSON . parse ( toolUse . function . arguments ) ) ) ;
409395 return {
410396 type : 'tool_result' as const ,
411397 tool_call_id : toolUse . id ,
0 commit comments