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

openai.ts

116 lines3.4 KB

1/**
2 * OpenAI — spec from openai-node's `api_reference/openapi.transformed.yml`
3 * (Castiron generation input). This leads the public openai-openapi export,
4 * which lags API releases (gpt-image-2 is enum-typed here; the public repo
5 * still caps at gpt-image-1.5). Models endpoint requires OPENAI_API_KEY.
6 */
7import type { Activity } from '#/db/schema.ts'
8import { bearerConnect } from './connect.ts'
9import {
10 displayNameFromRawId,
11 openaiGenerationEndpointId,
12 openaiModelActivity,
13} from './model-meta.ts'
14import { docsReport, docsRun } from './model-facts.ts'
15import { openaiModelFacts } from './openai-model-docs.ts'
16import { fetchJson, fetchOpenApi, skippedResult } from './types.ts'
17import type {
18 ListModelsResult,
19 OpenApiOperation,
20 ProviderConfig,
21 ProviderSecrets,
22 SpecFetchResult,
23} from './types.ts'
24​
25export const OPENAI_SPEC_URL =
26 'https://raw.githubusercontent.com/openai/openai-node/main/api_reference/openapi.transformed.yml'
27const OPENAI_MODELS_URL = 'https://api.openai.com/v1/models'
28​
29/**
30 * Tag → activity mapping. Tags absent from this map (Assistants, Batch,
31 * Files, Fine-tuning, Vector stores, and the organization/admin surface)
32 * are platform endpoints and classify to null.
33 */
34const OPENAI_TAG_ACTIVITIES: Record<string, Activity> = {
35 Chat: 'chat',
36 Completions: 'chat',
37 Responses: 'chat',
38 Conversations: 'chat',
39 Realtime: 'chat',
40 Audio: 'audio',
41 Images: 'image',
42 Videos: 'video',
43 Embeddings: 'embeddings',
44 Moderations: 'moderation',
46​
47function classify(path: string, op: OpenApiOperation): Activity | null {
48 const tags = Array.isArray(op.tags) ? (op.tags as Array<string>) : []
49 for (const tag of tags) {
50 const activity = OPENAI_TAG_ACTIVITIES[tag]
51 if (activity) return activity
52 }
53 // A handful of Responses-API operations ship untagged
54 // (/responses/input_tokens, /responses/compact).
55 if (path.startsWith('/responses')) return 'chat'
56 return null
58​
59async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
60 const { spec, hash } = await fetchOpenApi(OPENAI_SPEC_URL)
61 return {
62 specs: [spec],
63 sources: [{ url: OPENAI_SPEC_URL, hash }],
64 outputStrategy: 'post-200',
65 // Floating `main` URL — the document hash is the revision id.
66 specRevision: hash,
67 }
69​
70interface OpenAiModelList {
71 data?: Array<{ id: string; created?: number }>
73​
74async function listModels(
75 env: ProviderSecrets,
76 kv?: KVNamespace,
77): Promise<ListModelsResult> {
78 const key = env.OPENAI_API_KEY
79 if (!key) {
80 return { models: [], ...skippedResult('openai', 'OPENAI_API_KEY') }
81 }
82 const body = (await fetchJson(OPENAI_MODELS_URL, {
83 headers: { Authorization: `Bearer ${key}` },
84 })) as OpenAiModelList
85 const listed = body.data ?? []
86 const docs = docsRun()
87 const facts = await openaiModelFacts(
88 listed.map((m) => m.id),
89 kv,
90 docs,
91 )
92 return {
93 docsFailures: docsReport(docs),
94 models: listed.map((m) => ({
95 rawId: m.id,
96 displayName: displayNameFromRawId(m.id),
97 activity: openaiModelActivity(m.id),
98 releasedAt: m.created ?? null,
99 ...facts(m.id),
100 })),
101 }
103​
104export const openaiProvider: ProviderConfig = {
105 id: 'openai',
106 displayName: 'OpenAI',
107 authEnvVar: 'OPENAI_API_KEY',
108 defaultDerivation: 'upstream-spec',
109 specGrain: 'provider',
110 connect: bearerConnect('https://api.openai.com/v1'),
111 fetchSpec,
112 listModels,
113 classify,
114 generationEndpointId: ({ rawId, activity }) =>
115 openaiGenerationEndpointId(rawId, activity),