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

seed-providers.ts

124 lines4.7 KB

1import type { ProviderConfig } from '#/server/providers/types.ts'
2​
3import type { providers } from './schema.ts'
4​
5export type ProviderSeed = typeof providers.$inferInsert
6​
7/**
8 * Seed row for any registered provider: the hand-written row for the
9 * original 8, `seedFromAdapter` for auto-registered adapters. Lets the
10 * sync/poll engines upsert provider rows so a fresh adapter appears without
11 * a manual `bun run seed` against prod. Null when no seed can be derived
12 * (a config without specSourceUrl, e.g. a test stub) — callers skip the
13 * upsert and rely on the row already existing.
14 */
15export function seedForProvider(provider: ProviderConfig): ProviderSeed | null {
16 const listed = providerSeeds.find((seed) => seed.id === provider.id)
17 if (listed) return listed
18 return provider.specSourceUrl ? seedFromAdapter(provider) : null
20​
21/** Map an auto-registered adapter onto a seed row. */
22export function seedFromAdapter(provider: ProviderConfig): ProviderSeed {
23 const specSourceUrl = provider.specSourceUrl
24 if (!specSourceUrl) {
25 throw new Error(`adapter ${provider.id} is missing specSourceUrl`)
26 }
27 return {
28 id: provider.id,
29 displayName: provider.displayName,
30 specSourceUrl,
31 modelsEndpoint: provider.modelsEndpoint,
32 authEnvVar: provider.authEnvVar ?? null,
33 }
35​
36/**
37 * Spec-source config for the 8 monitored providers, ported from TanStack AI
38 * PR #622 (`packages/ai-schemas/scripts/providers/*`):
39 *
40 * - OpenAI's seeded URL is openai-node's Castiron `openapi.transformed.yml`,
41 * which leads the public openai-openapi export.
42 * - Anthropic's spec is the gzipped OpenAPI JSON bundled in the Stainless
43 * TypeScript SDK (`scripts/mock-spec.json.gz`), gunzipped at sync time.
44 * - Gemini publishes a Google Discovery document, converted to OpenAPI during
45 * sync.
46 * - FAL lists per-model OpenAPI specs from its models API via the
47 * `expand=openapi-3.0` query param; output schemas use the PR's
48 * `sibling-get` strategy rather than `post-200`.
49 * - OpenRouter also exposes `/api/v1/videos/models` for synthesized
50 * per-model video endpoints.
51 * - BytePlus publishes no machine-readable spec; specs and the model catalog
52 * are embedded in `src/server/providers/byteplus{,-spec}.ts` (ported from
53 * `@tanstack/ai-byteplus`), so the seeded URLs are provenance/config
54 * metadata, not fetch targets.
55 */
56export const providerSeeds: Array<ProviderSeed> = [
57 {
58 id: 'openai',
59 displayName: 'OpenAI',
60 specSourceUrl:
61 'https://raw.githubusercontent.com/openai/openai-node/main/api_reference/openapi.transformed.yml',
62 modelsEndpoint: 'https://api.openai.com/v1/models',
63 authEnvVar: 'OPENAI_API_KEY',
64 },
65 {
66 id: 'anthropic',
67 displayName: 'Anthropic',
68 // Stainless-bundled spec; gunzipped at sync time.
69 specSourceUrl:
70 'https://raw.githubusercontent.com/anthropics/anthropic-sdk-typescript/main/scripts/mock-spec.json.gz',
71 modelsEndpoint: 'https://api.anthropic.com/v1/models',
72 authEnvVar: 'ANTHROPIC_API_KEY',
73 },
74 {
75 id: 'gemini',
76 displayName: 'Google Gemini',
77 // Discovery document; converted to OpenAPI at sync time.
78 specSourceUrl:
79 'https://generativelanguage.googleapis.com/$discovery/rest?version=v1beta',
80 modelsEndpoint: 'https://generativelanguage.googleapis.com/v1beta/models',
81 authEnvVar: 'GEMINI_API_KEY',
82 },
83 {
84 id: 'grok',
85 displayName: 'xAI Grok',
86 specSourceUrl: 'https://docs.x.ai/openapi.json',
87 modelsEndpoint: 'https://api.x.ai/v1/models',
88 authEnvVar: 'XAI_API_KEY',
89 },
90 {
91 id: 'elevenlabs',
92 displayName: 'ElevenLabs',
93 specSourceUrl: 'https://api.elevenlabs.io/openapi.json',
94 modelsEndpoint: 'https://api.elevenlabs.io/v1/models',
95 authEnvVar: 'ELEVENLABS_API_KEY',
96 },
97 {
98 id: 'openrouter',
99 displayName: 'OpenRouter',
100 specSourceUrl: 'https://openrouter.ai/openapi.json',
101 // No key required — OpenRouter is the keyless smoke-test provider.
102 modelsEndpoint: 'https://openrouter.ai/api/v1/models',
103 authEnvVar: null,
104 },
105 {
106 id: 'fal',
107 displayName: 'FAL',
108 // Per-model specs via expand=openapi-3.0; paginated.
109 specSourceUrl: 'https://api.fal.ai/v1/models?expand=openapi-3.0',
110 modelsEndpoint: 'https://api.fal.ai/v1/models',
111 authEnvVar: 'FAL_KEY',
112 },
113 {
114 id: 'byteplus',
115 displayName: 'BytePlus',
116 // Human docs the embedded spec was derived from — nothing is fetched.
117 specSourceUrl: 'https://docs.byteplus.com/en/docs/ModelArk',
118 // Polled live when ARK_API_KEY is set; the embedded catalog fills the
119 // gaps (Seed Speech, ids Ark serves but does not enumerate) and stands in
120 // entirely when it is not.
121 modelsEndpoint: 'https://ark.ap-southeast.bytepluses.com/api/v3/models',
122 authEnvVar: 'ARK_API_KEY',
123 },