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

stability.ts

93 lines2.8 KB

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