fix: improve dictation
This commit is contained in:
@@ -6,11 +6,18 @@
|
||||
* key in the browser bundle):
|
||||
* - `GEMINI_API_KEY`: AI Studio API key. Without it every call fails with `missing-key`.
|
||||
* - `GEMINI_MODEL`: model id, `gemma-4-26b-a4b-it` by default.
|
||||
* - `GEMINI_TIMEOUT_MS`: request timeout, 8000 ms by default.
|
||||
* - `GEMINI_TIMEOUT_MS`: request timeout, 8000 ms by default (retries included).
|
||||
*
|
||||
* Gemma 4 writes out its reasoning before answering, which takes from 5 to over 30
|
||||
* seconds, so the request asks for `thinkingLevel: 'minimal'` (about a second). The API
|
||||
* also answers 5xx now and then, fast; those calls are retried once.
|
||||
*/
|
||||
|
||||
const GEMINI_API_URL = 'https://generativelanguage.googleapis.com/v1beta/models'
|
||||
|
||||
/** Attempts per call when the API answers 5xx. */
|
||||
const MAX_ATTEMPTS = 2
|
||||
|
||||
/** Gemma 4's small, fast variant (a mixture of experts with ~4B active parameters). */
|
||||
export const DEFAULT_GEMINI_MODEL = 'gemma-4-26b-a4b-it'
|
||||
|
||||
@@ -74,28 +81,37 @@ export class GeminiClient implements TextGenerator {
|
||||
async generate({ systemInstruction, turns }: GenerateInput): Promise<string> {
|
||||
if (!this.config.apiKey) throw new GeminiError('missing-key')
|
||||
|
||||
let response: Response
|
||||
try {
|
||||
response = await this.fetchFn(
|
||||
`${GEMINI_API_URL}/${encodeURIComponent(this.config.model)}:generateContent`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', 'x-goog-api-key': this.config.apiKey },
|
||||
body: JSON.stringify({
|
||||
systemInstruction: { parts: [{ text: systemInstruction }] },
|
||||
contents: turns.map(({ role, text }) => ({ role, parts: [{ text }] })),
|
||||
generationConfig: { temperature: 0 },
|
||||
}),
|
||||
signal: AbortSignal.timeout(this.config.timeoutMs),
|
||||
},
|
||||
)
|
||||
} catch (error) {
|
||||
const name = error instanceof Error ? error.name : ''
|
||||
throw new GeminiError(
|
||||
name === 'TimeoutError' || name === 'AbortError' ? 'timeout' : 'network',
|
||||
)
|
||||
// One deadline for every attempt, so a retry never makes the user wait longer.
|
||||
const signal = AbortSignal.timeout(this.config.timeoutMs)
|
||||
const request = {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', 'x-goog-api-key': this.config.apiKey },
|
||||
body: JSON.stringify({
|
||||
systemInstruction: { parts: [{ text: systemInstruction }] },
|
||||
contents: turns.map(({ role, text }) => ({ role, parts: [{ text }] })),
|
||||
generationConfig: { temperature: 0, thinkingConfig: { thinkingLevel: 'minimal' } },
|
||||
}),
|
||||
signal,
|
||||
}
|
||||
|
||||
let response: Response | undefined
|
||||
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
response = await this.fetchFn(
|
||||
`${GEMINI_API_URL}/${encodeURIComponent(this.config.model)}:generateContent`,
|
||||
request,
|
||||
)
|
||||
} catch (error) {
|
||||
const name = error instanceof Error ? error.name : ''
|
||||
throw new GeminiError(
|
||||
name === 'TimeoutError' || name === 'AbortError' ? 'timeout' : 'network',
|
||||
)
|
||||
}
|
||||
|
||||
if (response.status < 500) break
|
||||
}
|
||||
|
||||
if (!response) throw new GeminiError('network')
|
||||
if (response.status === 429) throw new GeminiError('rate-limited')
|
||||
if (!response.ok) throw new GeminiError(`http-${response.status}`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user