Skip to content

Commit 093b26f

Browse files
authored
Self-healing tool-calls on invalid format (#1772)
1 parent d5dc0f3 commit 093b26f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

apps/studio/electron/main/chat/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { MainChannels } from '@onlook/models/constants';
1414
import {
1515
generateObject,
16+
NoSuchToolError,
1617
RetryError,
1718
streamText,
1819
type CoreMessage,
@@ -101,6 +102,37 @@ class LlmManager {
101102
onError: (error) => {
102103
throw error;
103104
},
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+
},
104136
});
105137
const streamParts: TextStreamPart<ToolSet>[] = [];
106138
for await (const partialStream of fullStream) {

0 commit comments

Comments
 (0)