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

fireworks.ts

164 lines5.0 KB

1/**
2 * Fireworks AI — official text-completion OpenAPI at
3 * docs.fireworks.ai/text-completion.openapi.yaml (public). The hinted
4 * api-reference page is HTML, not a spec; the published YAML covers
5 * POST /v1/chat/completions and /v1/completions. Models list needs
6 * FIREWORKS_API_KEY.
7 *
8 * Prices, reasoning, and the chat request map are read on each poll from
9 * the pricing page, the serverless models API, and that same YAML. A chat
10 * row with no stated reasoning also reads its native metadata
11 * (`/v1/<rawId>`); a failed load marks only that row unavailable.
12 */
13import { fireworksHostedReasoning } from '../native-host-reasoning.ts'
14import { mapConcurrent, docsReport, docsRun, tryDocs } from '../model-facts.ts'
15import type { Activity } from '#/db/schema.ts'
16import {
17 applyFireworksDocs,
18 FIREWORKS_SERVERLESS_URL,
19 FIREWORKS_SPEC_URL,
20 loadFireworksChatSpec,
21 loadFireworksServerless,
22 mergeFireworksRates,
23} from '../fireworks-facts.ts'
24import {
25 FIREWORKS_PRICING_URL,
26 loadFireworksPricingDoc,
27} from '../fireworks-pricing.ts'
28import {
29 compatGenerationEndpointId,
30 fireworksModalities,
31 fireworksModelActivity,
32} from '../model-meta.ts'
33import {
34 classifyOpenAiCompat,
35 listOpenAiCompatibleModels,
36} from '../openai-compat.ts'
37import { overlayModelFacts } from '../reasoning-config.ts'
38import { fetchOpenApi } from '../types.ts'
39import type {
40 ListModelsResult,
41 ProviderConfig,
42 ProviderSecrets,
43 SpecFetchResult,
44} from '../types.ts'
45​
46const FIREWORKS_MODELS_URL = 'https://api.fireworks.ai/inference/v1/models'
47​
48/**
49 * Inference paths may be served under /inference (docs) or as the OpenAI
50 * /v1-prefixed forms in the published spec. Anthropic-compatible /messages
51 * is chat; rerank and the account control plane are platform.
52 */
53function classify(path: string): Activity | null {
54 const stripped = path.startsWith('/inference/')
55 ? path.slice('/inference'.length)
56 : path
57 const activity = classifyOpenAiCompat(stripped)
58 if (activity) return activity
59 const bare = stripped.startsWith('/v1/') ? stripped.slice(3) : stripped
60 if (bare === '/messages') return 'chat'
61 return null
63​
64async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
65 const { spec, hash } = await fetchOpenApi(FIREWORKS_SPEC_URL)
66 return {
67 specs: [spec],
68 sources: [{ url: FIREWORKS_SPEC_URL, hash }],
69 outputStrategy: 'post-200',
70 }
72​
73async function listModels(
74 env: ProviderSecrets,
75 kv?: KVNamespace,
76): Promise<ListModelsResult> {
77 const listed = await listOpenAiCompatibleModels({
78 providerId: 'fireworks',
79 url: FIREWORKS_MODELS_URL,
80 env,
81 envVar: 'FIREWORKS_API_KEY',
82 activity: fireworksModelActivity,
83 extend: async (row) => {
84 const modalities = fireworksModalities(row)
85 const stated =
86 typeof row.context_length === 'number' && row.context_length > 0
87 return {
88 ...(modalities ? { modalities } : {}),
89 ...(stated
90 ? {
91 factSources: {
92 contextWindow: {
93 derivation: 'listing' as const,
94 sourceUrl: FIREWORKS_MODELS_URL,
95 path: 'context_length',
96 },
97 },
98 }
99 : {}),
100 }
101 },
102 })
103 if (listed.models.length === 0) return listed
104 const docs = docsRun()
105 const key = env.FIREWORKS_API_KEY
106 const markdown = await tryDocs(docs, FIREWORKS_PRICING_URL, (cached) =>
107 loadFireworksPricingDoc(kv, cached),
108 )
109 const serverless = key
110 ? await tryDocs(docs, FIREWORKS_SERVERLESS_URL, (cached) =>
111 loadFireworksServerless(kv, cached, key),
112 )
113 : null
114 const chat = await tryDocs(docs, FIREWORKS_SPEC_URL, (cached) =>
115 loadFireworksChatSpec(kv, cached),
116 )
117 const prices = mergeFireworksRates(markdown, serverless)
118 const context = serverless?.context ?? {}
119 return {
120 ...listed,
121 models: await mapConcurrent(listed.models, 4, async (model) =>
122 fireworksHostedReasoning(
123 overlayModelFacts(
124 model,
125 applyFireworksDocs(model, {
126 prices,
127 context,
128 contextHash: serverless?.hash ?? null,
129 chat,
130 }),
131 ),
132 key ??
133 (() => {
134 throw new Error(
135 'fireworks: missing API key for native model metadata',
136 )
137 })(),
138 docs,
139 kv,
140 ),
141 ),
142 docsFailures: docsReport(docs),
143 }
145​
146export const provider: ProviderConfig = {
147 id: 'fireworks',
148 displayName: 'Fireworks AI',
149 authEnvVar: 'FIREWORKS_API_KEY',
150 specSourceUrl: FIREWORKS_SPEC_URL,
151 modelsEndpoint: FIREWORKS_MODELS_URL,
152 defaultDerivation: 'upstream-spec',
153 fetchSpec,
154 listModels,
155 classify,
156 generationEndpointId: ({ activity }) =>
157 activity === 'embeddings'
158 ? null
159 : compatGenerationEndpointId(activity, 'v1/'),
160 // The shared chat schema lists `reasoning_effort` on every model.
161 // Fireworks names the families that accept it in that field's
162 // description. The walk must not stamp `reasoning` on the rest.
163 perModelSchemaFlags: ['reasoning', 'reasoning_effort'],