fix: Ai Chat: do not send tools if empty + respond even if tool fails (#5692)

* do not send tools if empty

* respond to user request even if tool call fails

* remove test line

* add missing await
This commit is contained in:
centdix
2025-05-01 15:36:22 +02:00
committed by GitHub
parent 41c15fc78a
commit 9c55040e47
2 changed files with 27 additions and 18 deletions
@@ -487,6 +487,31 @@ async function callTool(
}
}
async function processToolCall(
toolCall: ChatCompletionMessageToolCall,
messages: ChatCompletionMessageParam[],
lang: ScriptLang | 'bunnative'
) {
try {
const args = JSON.parse(toolCall.function.arguments)
let result = ''
try {
result = await callTool(toolCall.function.name, args, lang, get(workspaceStore) ?? '')
} catch (err) {
console.error(err)
result =
'Error while calling tool, MUST tell the user to check the browser console for more details, and then respond as much as possible to the original request'
}
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: result
})
} catch (err) {
console.error(err)
}
}
export async function chatRequest(
messages: ChatCompletionMessageParam[],
abortController: AbortController,
@@ -554,23 +579,7 @@ export async function chatRequest(
tool_calls: toolCalls
})
for (const toolCall of toolCalls) {
try {
const args = JSON.parse(toolCall.function.arguments)
const result = await callTool(
toolCall.function.name,
args,
lang,
get(workspaceStore) ?? ''
)
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: result
})
} catch (err) {
console.error(err)
throw new Error('Error while calling tool')
}
await processToolCall(toolCall, messages, lang)
}
} else {
break
+1 -1
View File
@@ -385,7 +385,7 @@ function getProviderAndCompletionConfig<K extends boolean>({
: {
model: modelProvider.model,
temperature: 0,
tools
...(tools && tools.length > 0 ? { tools } : {})
}),
max_tokens: getModelMaxTokens(modelProvider.model),
messages: processedMessages,