fix: fallback without tools on tool_use_failed error
All checks were successful
Deploy / deploy (push) Successful in 1m28s

This commit is contained in:
Cosmo
2026-05-01 11:19:26 +00:00
parent feafde37dc
commit 70284c69cf

View File

@@ -164,10 +164,29 @@ export async function POST(req: Request) {
break break
} }
} catch (e: any) { } catch (e: any) {
console.error('[voice/chat] groq error:', e?.message || e) const errStr = String(e?.message || e)
const msg = 'Что-то сломалось.' console.error('[voice/chat] groq error:', errStr)
emitVoice('error', agent, msg)
return NextResponse.json({ error: 'llm_failed', detail: String(e?.message || e), text: msg }, { status: 502 }) // tool_use_failed: модель неправильно сформировала tool call — повторить без tools
if (errStr.includes('tool_use_failed') || errStr.includes('Failed to call a function')) {
try {
const c2 = client()
const fallback = await c2.chat.completions.create({
model: MODEL,
max_tokens: MAX_TOKENS,
messages: apiMessages.slice(0, historyStartLen + 1),
})
finalText = fallback.choices[0]?.message?.content || ''
console.log('[voice/chat] tool_use_failed fallback ok')
} catch (e2) {
console.error('[voice/chat] fallback failed:', e2)
finalText = 'Не удалось выполнить запрос.'
}
} else {
const msg = 'Что-то сломалось.'
emitVoice('error', agent, msg)
return NextResponse.json({ error: 'llm_failed', detail: errStr, text: msg }, { status: 502 })
}
} }
if (!finalText.trim()) { if (!finalText.trim()) {