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