@@ -13,6 +13,7 @@ import {
13
13
import { MainChannels } from '@onlook/models/constants' ;
14
14
import {
15
15
generateObject ,
16
+ NoSuchToolError ,
16
17
RetryError ,
17
18
streamText ,
18
19
type CoreMessage ,
@@ -101,6 +102,37 @@ class LlmManager {
101
102
onError : ( error ) => {
102
103
throw error ;
103
104
} ,
105
+ experimental_repairToolCall : async ( {
106
+ toolCall,
107
+ tools,
108
+ parameterSchema,
109
+ error,
110
+ } ) => {
111
+ if ( NoSuchToolError . isInstance ( error ) ) {
112
+ console . error ( 'Invalid tool name' , toolCall . toolName ) ;
113
+ return null ; // do not attempt to fix invalid tool names
114
+ }
115
+ const tool = tools [ toolCall . toolName as keyof typeof tools ] ;
116
+
117
+ console . warn (
118
+ `Invalid parameter for tool ${ toolCall . toolName } with args ${ JSON . stringify ( toolCall . args ) } , attempting to fix` ,
119
+ ) ;
120
+
121
+ const { object : repairedArgs } = await generateObject ( {
122
+ model,
123
+ schema : tool . parameters ,
124
+ prompt : [
125
+ `The model tried to call the tool "${ toolCall . toolName } "` +
126
+ ` with the following arguments:` ,
127
+ JSON . stringify ( toolCall . args ) ,
128
+ `The tool accepts the following schema:` ,
129
+ JSON . stringify ( parameterSchema ( toolCall ) ) ,
130
+ 'Please fix the arguments.' ,
131
+ ] . join ( '\n' ) ,
132
+ } ) ;
133
+
134
+ return { ...toolCall , args : JSON . stringify ( repairedArgs ) } ;
135
+ } ,
104
136
} ) ;
105
137
const streamParts : TextStreamPart < ToolSet > [ ] = [ ] ;
106
138
for await ( const partialStream of fullStream ) {
0 commit comments