From 92a30e69c01e739711dbf7ca84ec5aec79fa8702 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Wed, 20 May 2026 08:21:14 +0200 Subject: [PATCH] fix(beacon): surface Groq STT errors with actionable status codes + hints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Investigated the user's 'STT microphone not working properly' report. Found two real problems on this machine: 1. GROQ_API_KEY in .env.local returns 401 from Groq (direct probe of https://api.groq.com/openai/v1/audio/transcriptions with the same bearer returned {'error':{'message':'Invalid API Key'}}). User needs to rotate the key at https://console.groq.com. 2. Local Whisper 'base' fallback takes 38-45s per 1-second WAV — within the route's 60s timeout but well past any user's patience. The route's 401 path previously wrapped the failure as a generic HTTP 500 with the error body inlined as a string — easy to miss in a network tab, no actionable hint. Now classifies common Groq failure modes: • 401 (Invalid API Key) → HTTP 502 with 'rotate the key' hint • 429 (rate limited) → HTTP 429 with 'retry or switch to local' • timeout / abort → HTTP 504 with 'try a shorter recording' • other → HTTP 502 (was 500; bad-upstream is more honest — the route itself didn't fail) The original error is preserved as for debugging without poisoning the user-facing string. Local Whisper path unchanged — the slowness there is a separate question (model size + GPU vs CPU) and outside the scope of error-message work. tsc clean, eslint clean. Doesn't fix the user's 401 — they still need to rotate the Groq key — but ensures future occurrences are diagnosed in seconds instead of requiring a deep dive into the route source. --- src/app/api/beacon/transcribe/route.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/api/beacon/transcribe/route.ts b/src/app/api/beacon/transcribe/route.ts index 15fe6bd92..a01e9e1ee 100644 --- a/src/app/api/beacon/transcribe/route.ts +++ b/src/app/api/beacon/transcribe/route.ts @@ -64,8 +64,30 @@ export async function POST(req: NextRequest) { if (!text) return NextResponse.json({ error: "No speech detected" }, { status: 422 }); return NextResponse.json({ text }); } catch (err) { + // Surface auth/quota problems with actionable status codes + hints so a + // user seeing "STT not working" sees the real cause in the network tab + // instead of a generic 500. callGroqTranscribe throws messages shaped + // "groq transcribe 401: " — match on the status segment. const msg = err instanceof Error ? err.message : String(err); - return NextResponse.json({ error: msg }, { status: 500 }); + if (/groq transcribe 401/i.test(msg) || /invalid.*api.*key/i.test(msg)) { + return NextResponse.json({ + error: "Groq API key invalid — rotate it at https://console.groq.com and update GROQ_API_KEY in your env.", + detail: msg, + }, { status: 502 }); + } + if (/groq transcribe 429/i.test(msg) || /rate.?limit/i.test(msg)) { + return NextResponse.json({ + error: "Groq rate-limited this transcription — retry in a moment or switch to local transcription.", + detail: msg, + }, { status: 429 }); + } + if (/abort|timeout/i.test(msg)) { + return NextResponse.json({ + error: "Groq transcription timed out (>30s) — try a shorter recording.", + detail: msg, + }, { status: 504 }); + } + return NextResponse.json({ error: msg }, { status: 502 }); } }