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

fireworks.ts

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