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

groq.ts

98 lines2.9 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 { groqModelPricing } from '../groq-pricing.ts'
13import {
14 groqModelReasoning,
15 listsReasoning,
16 overlayModelFacts,
17} from '../reasoning-config.ts'
18import { groqModelServerTools } from '../server-tools.ts'
19import { groqGenerationEndpointId, groqModelActivity } from '../model-meta.ts'
20import { fetchText, parseGzippedOpenApi } from '../types.ts'
21import type {
22 ListModelsResult,
23 ProviderConfig,
24 ProviderSecrets,
25 SpecFetchResult,
26} from '../types.ts'
27​
28const GROQ_MOCK_URL =
29 'https://raw.githubusercontent.com/groq/groq-python/main/scripts/mock'
30const GROQ_MODELS_URL = 'https://api.groq.com/openai/v1/models'
31​
32/**
33 * Stainless paths are `/openai/v1/...` (server is `https://api.groq.com`).
34 * Strip that prefix and reuse the OpenAI-compat path map. Batches, files,
35 * fine-tunes, models, and rerank classify null.
36 */
37function classify(path: string): Activity | null {
38 const withoutOpenAi = path.startsWith('/openai/')
39 ? path.slice('/openai'.length)
40 : path
41 return classifyOpenAiCompat(withoutOpenAi)
43​
44async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
45 const script = await fetchText(GROQ_MOCK_URL)
46 const blob = script.match(/EMBEDDED_SPEC="([A-Za-z0-9+/=\s]+)"/)?.[1]
47 if (!blob) {
48 throw new Error(`groq: no EMBEDDED_SPEC in ${GROQ_MOCK_URL}`)
49 }
50 const bytes = Uint8Array.from(atob(blob.replace(/\s/g, '')), (c) =>
51 c.charCodeAt(0),
52 )
53 return parseGzippedOpenApi(bytes, 'groq', GROQ_MOCK_URL)
55​
56async function listModels(
57 env: ProviderSecrets,
58 kv?: KVNamespace,
59): Promise<ListModelsResult> {
60 const listed = await listOpenAiCompatibleModels({
61 providerId: 'groq',
62 url: GROQ_MODELS_URL,
63 env,
64 envVar: 'GROQ_API_KEY',
65 activity: groqModelActivity,
66 })
67 if (listed.models.length === 0) return listed
68 const [pricing, reasoning, tools] = await Promise.all([
69 groqModelPricing(kv),
70 groqModelReasoning(kv),
71 groqModelServerTools(kv),
72 ])
73 return {
74 ...listed,
75 models: listed.models.map((model) =>
76 overlayModelFacts(
77 model,
78 pricing(model.rawId),
79 reasoning(model.rawId, listsReasoning(model.capabilities)),
80 tools(model.rawId),
81 ),
82 ),
83 }
85​
86export const provider: ProviderConfig = {
87 id: 'groq',
88 displayName: 'Groq',
89 authEnvVar: 'GROQ_API_KEY',
90 specSourceUrl: GROQ_MOCK_URL,
91 modelsEndpoint: GROQ_MODELS_URL,
92 defaultDerivation: 'upstream-spec',
93 fetchSpec,
94 listModels,
95 classify,
96 generationEndpointId: ({ rawId, activity }) =>
97 groqGenerationEndpointId(rawId, activity),