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

groq.ts

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