Open History to see the last commit
1/**
2 * Stability AI — public OpenAPI at api.stability.ai/v2alpha/openapi
3 * (legacy path; `info.version` is v2beta). HTML docs at
4 * platform.stability.ai/docs/api-reference have no downloadable spec URL.
5 * Model catalog is GET /v1/engines/list (Bearer).
6 */
7import type { Activity } from '#/db/schema.ts'
8import { docsReport, docsRun, tryDocs, unavailable } from '../model-facts.ts'
9import {
10 STABILITY_PRICING_URL,
11 stabilityModelPricing,
12} from '../stability-pricing.ts'
13import { fetchJson, fetchOpenApi, skippedResult } from '../types.ts'
14import type {
15 ListModelsResult,
16 ProviderConfig,
17 ProviderSecrets,
18 SpecFetchResult,
19} from '../types.ts'
20
21const STABILITY_OPENAPI_URL = 'https://api.stability.ai/v2alpha/openapi'
22const STABILITY_ENGINES_URL = 'https://api.stability.ai/v1/engines/list'
23
24const ENGINE_ACTIVITIES: Record<string, Activity> = {
25 PICTURE: 'image',
26 AUDIO: 'audio',
27 VIDEO: 'video',
28}
29
30/**
31 * Generation is the v2beta image/audio surface (plus leftover v2alpha
32 * `/generation/` image POSTs). Result polling, 3D, user/account, and
33 * engines/list are platform.
34 */
35function classify(path: string): Activity | null {
36 if (path.includes('/result/') || path.includes('/results/')) return null
37 if (path.includes('/stable-image/') || path.includes('/generation/')) {
38 return 'image'
39 }
40 if (path.includes('/audio/')) return 'audio'
41 return null
42}
43
44async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
45 const { spec, hash } = await fetchOpenApi(STABILITY_OPENAPI_URL)
46 return {
47 specs: [spec],
48 sources: [{ url: STABILITY_OPENAPI_URL, hash }],
49 outputStrategy: 'post-200',
50 }
51}
52
53interface StabilityEngine {
54 id: string
55 name?: string
56 type?: string
57}
58
59async function listModels(
60 env: ProviderSecrets,
61 kv?: KVNamespace,
62): Promise<ListModelsResult> {
63 const key = env.STABILITY_API_KEY
64 if (!key) {
65 return { models: [], ...skippedResult('stability', 'STABILITY_API_KEY') }
66 }
67 const body = (await fetchJson(STABILITY_ENGINES_URL, {
68 headers: { Authorization: `Bearer ${key}` },
69 })) as Array<StabilityEngine>
70 const docs = docsRun()
71 const pricing = await tryDocs(docs, STABILITY_PRICING_URL, (cached) =>
72 stabilityModelPricing(kv, cached),
73 )
74 return {
75 models: (Array.isArray(body) ? body : [])
76 .filter((m) => typeof m.id === 'string' && m.id.length > 0)
77 .map((m) => ({
78 rawId: m.id,
79 displayName: m.name ?? null,
80 activity: ENGINE_ACTIVITIES[m.type ?? ''] ?? null,
81 ...(pricing ? pricing(m.id) : unavailable('pricing')),
82 })),
83 docsFailures: docsReport(docs),
84 }
85}
86
87export const provider: ProviderConfig = {
88 id: 'stability',
89 displayName: 'Stability AI',
90 authEnvVar: 'STABILITY_API_KEY',
91 specSourceUrl: STABILITY_OPENAPI_URL,
92 modelsEndpoint: STABILITY_ENGINES_URL,
93 defaultDerivation: 'upstream-spec',
94 fetchSpec,
95 listModels,
96 classify,
97 // Engine ids from /v1/engines/list are not the v2beta operation paths.
98 // Binding every PICTURE engine to generate/ultra stored image input the
99 // ultra body does not have. Leave the link unset.
100 generationEndpointId: () => null,
101}