feat(voice): split dictations with Groq when it is configured
Gemma through the Gemini API answered 500 or timed out on most calls, so
dictated lists fell back to splitting on commas and "y" ("Azúcar leche y
café" became two items). With GROQ_API_KEY set the API now asks Groq
(openai/gpt-oss-120b by default), which answers in about a second; Gemini
stays as the provider when only its key is set.
The HTTP call, its deadline and the single retry on 5xx move to
lib/text-generator.ts and are shared by both clients.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,10 @@ beforeEach(() => {
|
||||
process.env.GEMINI_API_KEY = 'test-key'
|
||||
process.env.GEMINI_MODEL = 'gemma-test'
|
||||
process.env.GEMINI_TIMEOUT_MS = '50'
|
||||
// `bun test` loads `.env`: a real Groq key there would take over these Gemini tests.
|
||||
delete process.env.GROQ_API_KEY
|
||||
delete process.env.GROQ_MODEL
|
||||
delete process.env.GROQ_TIMEOUT_MS
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -175,3 +179,67 @@ describe('POST /api/voice/split', () => {
|
||||
expect(sent.contents.at(-1).parts[0].text).toHaveLength(500)
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /api/voice/split with Groq', () => {
|
||||
beforeEach(() => {
|
||||
process.env.GROQ_API_KEY = 'gsk_test'
|
||||
process.env.GROQ_MODEL = 'llama-test'
|
||||
process.env.GROQ_TIMEOUT_MS = '50'
|
||||
})
|
||||
|
||||
function groqAnswer(content: string) {
|
||||
return Response.json({ choices: [{ message: { role: 'assistant', content } }] })
|
||||
}
|
||||
|
||||
it('asks Groq instead of Gemini when its key is set', async () => {
|
||||
const fetchMock = mockFetch(async () => groqAnswer('["Azúcar", "Leche", "Café"]'))
|
||||
|
||||
const response = await split({ text: 'azúcar leche y café' })
|
||||
|
||||
expect(await response.json()).toEqual({ items: ['Azúcar', 'Leche', 'Café'] })
|
||||
expect(warnings).toHaveLength(0)
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] ?? []
|
||||
expect(url).toBe('https://api.groq.com/openai/v1/chat/completions')
|
||||
expect(new Headers(init?.headers).get('authorization')).toBe('Bearer gsk_test')
|
||||
const sent = JSON.parse(String(init?.body))
|
||||
expect(sent.model).toBe('llama-test')
|
||||
expect(sent.temperature).toBe(0)
|
||||
expect(sent.messages[0]).toEqual({
|
||||
role: 'system',
|
||||
content: expect.stringContaining('array JSON'),
|
||||
})
|
||||
expect(sent.messages[2].role).toBe('assistant')
|
||||
expect(sent.messages.at(-1)).toEqual({ role: 'user', content: 'azúcar leche y café' })
|
||||
})
|
||||
|
||||
it('retries a 5xx once', async () => {
|
||||
let calls = 0
|
||||
const fetchMock = mockFetch(async () =>
|
||||
++calls === 1 ? new Response('{}', { status: 502 }) : groqAnswer('["Pan"]'),
|
||||
)
|
||||
|
||||
const response = await split({ text: 'pan' })
|
||||
|
||||
expect(await response.json()).toEqual({ items: ['Pan'] })
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('falls back when Groq is rate limited (429)', async () => {
|
||||
mockFetch(async () => new Response('{}', { status: 429 }))
|
||||
|
||||
const response = await split({ text: 'pan y queso' })
|
||||
|
||||
expect(await response.json()).toEqual({ items: ['pan', 'queso'] })
|
||||
expect(JSON.stringify(warnings)).toContain('rate-limited')
|
||||
})
|
||||
|
||||
it('falls back when Groq answers with no content', async () => {
|
||||
mockFetch(async () => groqAnswer(''))
|
||||
|
||||
const response = await split({ text: 'pan, queso' })
|
||||
|
||||
expect(await response.json()).toEqual({ items: ['pan', 'queso'] })
|
||||
expect(JSON.stringify(warnings)).toContain('empty-response')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user