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

groq.ts

102 lines3.2 KB

1/**
2 * Groq — official OpenAPI spec bundled in Groq's Stainless-generated Python
3 * SDK: `scripts/mock` embeds it as base64+gzip `EMBEDDED_SPEC` (refreshed on
4 * every SDK codegen). `.stats.yml` stopped carrying `openapi_spec_url` on
5 * 2026-08-11. The hinted console URL is docs, not a spec.
6 */
7import type { Activity } from '#/db/schema.ts'
8import {
9 classifyOpenAiCompat,
10 listOpenAiCompatibleModels,
11} from '../openai-compat.ts'
12import { GROQ_PRICING_URL, groqModelPricing } from '../groq-pricing.ts'
13import { docsReport, docsRun, tryDocs, unavailable } from '../model-facts.ts'
14import {
15 groqModelReasoning,
16 listsReasoning,
17 overlayModelFacts,
18} from '../reasoning-config.ts'
19import { groqModelServerTools } from '../server-tools.ts'
20import { groqGenerationEndpointId, groqModelActivity } from '../model-meta.ts'
21import { fetchText, parseGzippedOpenApi } from '../types.ts'
22import type {
23 ListModelsResult,
24 ProviderConfig,
25 ProviderSecrets,
26 SpecFetchResult,
27} from '../types.ts'
28​
29const GROQ_MOCK_URL =
30 'https://raw.githubusercontent.com/groq/groq-python/main/scripts/mock'
31const GROQ_MODELS_URL = 'https://api.groq.com/openai/v1/models'
32​
33/**
34 * Stainless paths are `/openai/v1/...` (server is `https://api.groq.com`).
35 * Strip that prefix and reuse the OpenAI-compat path map. Batches, files,
36 * fine-tunes, models, and rerank classify null.
37 */
38function classify(path: string): Activity | null {
39 const withoutOpenAi = path.startsWith('/openai/')
40 ? path.slice('/openai'.length)
41 : path
42 return classifyOpenAiCompat(withoutOpenAi)
44​
45async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
46 const script = await fetchText(GROQ_MOCK_URL)
47 const blob = script.match(/EMBEDDED_SPEC="([A-Za-z0-9+/=\s]+)"/)?.[1]
48 if (!blob) {
49 throw new Error(`groq: no EMBEDDED_SPEC in ${GROQ_MOCK_URL}`)
50 }
51 const bytes = Uint8Array.from(atob(blob.replace(/\s/g, '')), (c) =>
52 c.charCodeAt(0),
53 )
54 return parseGzippedOpenApi(bytes, 'groq', GROQ_MOCK_URL)
56​
57async function listModels(
58 env: ProviderSecrets,
59 kv?: KVNamespace,
60): Promise<ListModelsResult> {
61 const listed = await listOpenAiCompatibleModels({
62 providerId: 'groq',
63 url: GROQ_MODELS_URL,
64 env,
65 envVar: 'GROQ_API_KEY',
66 activity: groqModelActivity,
67 })
68 if (listed.models.length === 0) return listed
69 const docs = docsRun()
70 // Pricing reads several docs pages; one failing keeps the stored cards.
71 const [pricing, reasoning, tools] = await Promise.all([
72 tryDocs(docs, GROQ_PRICING_URL, (cached) => groqModelPricing(kv, cached)),
73 groqModelReasoning(kv),
74 groqModelServerTools(kv),
75 ])
76 return {
77 ...listed,
78 models: listed.models.map((model) =>
79 overlayModelFacts(
80 model,
81 pricing ? pricing(model.rawId) : unavailable('pricing'),
82 reasoning(model.rawId, listsReasoning(model.capabilities)),
83 tools(model.rawId),
84 ),
85 ),
86 docsFailures: docsReport(docs),
87 }
89​
90export const provider: ProviderConfig = {
91 id: 'groq',
92 displayName: 'Groq',
93 authEnvVar: 'GROQ_API_KEY',
94 specSourceUrl: GROQ_MOCK_URL,
95 modelsEndpoint: GROQ_MODELS_URL,
96 defaultDerivation: 'upstream-spec',
97 fetchSpec,
98 listModels,
99 classify,
100 generationEndpointId: ({ rawId, activity }) =>
101 groqGenerationEndpointId(rawId, activity),