Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

gemini-live-thinking.ts

60 lines2.1 KB

1import { cachedDocs, markdocTables, markdownTableRows } from './model-facts.ts'
2import { fetchText, sha256Text } from './types.ts'
3import type { ModelReasoning } from './types.ts'
4​
5export const GEMINI_LIVE_GUIDE_URL =
6 'https://aistudio.google.com/docs/live-api/capabilities.md'
7​
8/**
9 * The Live API guide's Model comparison table. The one column whose
10 * Thinking cell says "Uses `thinkingBudget`" is the native audio model;
11 * its id is the `/docs/models/{id}` link the guide gives that column's
12 * name. The guide no longer says thinking can be disabled for it, so
13 * `mandatory` is unstated.
14 */
15export function parseGeminiNativeAudioThinking(markdown: string): {
16 rawId: string
17 reasoning: ModelReasoning
18} {
19 const text = markdocTables(markdown.replace(/```[\s\S]*?```/g, ''))
20 const rows = markdownTableRows(text)
21 const header = rows.find((row) => row[0] === 'Feature')
22 const thinking = rows.find((row) =>
23 (row[0] ?? '').startsWith('**[Thinking]('),
24 )
25 const budgetColumns = (header ?? []).filter(
26 (_name, column) =>
27 column > 0 &&
28 (thinking?.[column] ?? '').startsWith('Uses `thinkingBudget`'),
29 )
30 const name = budgetColumns.length === 1 ? budgetColumns[0] : undefined
31 const rawId = name
32 ? [...text.matchAll(/\[([^\]]+)\]\(\/docs\/models\/([a-z0-9.-]+)\)/g)].find(
33 (link) => link[1] === name,
34 )?.[2]
35 : undefined
36 if (!rawId?.includes('native-audio')) {
37 throw new Error(
38 'gemini live guide: native audio thinkingBudget column unreadable',
39 )
40 }
41 return { rawId, reasoning: { mode: 'budget', mandatory: null } }
43​
44export async function geminiNativeAudioThinking(
45 rawIds: ReadonlyArray<string>,
46 kv?: KVNamespace,
47 cached: typeof cachedDocs = cachedDocs,
48) {
49 // Names select which own guide to check; only its literal model ID binds facts.
50 if (!rawIds.some((id) => id.includes('native-audio'))) return null
51 return cached(kv, GEMINI_LIVE_GUIDE_URL, async () => {
52 const markdown = await fetchText(GEMINI_LIVE_GUIDE_URL, {
53 signal: AbortSignal.timeout(20_000),
54 })
55 return {
56 ...parseGeminiNativeAudioThinking(markdown),
57 hash: await sha256Text(markdown),
58 }
59 })