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

modelschemas.ts

95 lines3.0 KB

1/**
2 * Thin wrapper around `@modelschemas/client` for the model sync scripts.
3 */
4​
5import {
6 createModelschemasClient,
7 getSchema,
8 listModels,
9} from '@modelschemas/client'
10import type { GetSchemaData } from '@modelschemas/client'
11import { isRecord, parseCatalogModels } from './catalog'
12import type { CatalogModel } from './catalog'
13import { SYNCED_PROVIDERS } from './provider-supports'
14import type { SyncedProvider } from './provider-supports'
15​
16export function createSyncClient(options?: {
17 apiKey?: string
18 fetch?: typeof globalThis.fetch
19}): ReturnType<typeof createModelschemasClient> {
20 const apiKey = options?.apiKey ?? process.env.MODELSCHEMAS_API_KEY
21 return createModelschemasClient({
22 baseUrl: 'https://modelschemas.com',
23 ...(apiKey ? { apiKey } : {}),
24 ...(options?.fetch ? { fetch: options.fetch } : {}),
25 })
27​
28type SyncClient = ReturnType<typeof createSyncClient>
29​
30function errorMessage(error: unknown): string {
31 if (typeof error === 'string') return error
32 if (!isRecord(error)) return String(error)
33 if (isRecord(error.error) && typeof error.error.message === 'string') {
34 return error.error.message
35 }
36 if (typeof error.message === 'string') return error.message
37 return JSON.stringify(error)
39​
40/** The raw `listModels` payload for one provider. */
41export async function listRawModels(
42 client: SyncClient,
43 provider: string,
44): Promise<unknown> {
45 const result = await listModels({ client, query: { provider } })
46 if (result.error !== undefined) {
47 throw new Error(
48 `modelschemas listModels provider=${provider}: ${errorMessage(result.error)}`,
49 )
50 }
51 return result.data
53​
54export async function fetchCatalog(
55 client: SyncClient,
56 provider: string,
57): Promise<Array<CatalogModel>> {
58 return parseCatalogModels(await listRawModels(client, provider))
60​
61/** The raw input-schema payload for one endpoint, e.g. `v1/music`. */
62export async function getInputSchema(
63 client: SyncClient,
64 path: GetSchemaData['path'],
65): Promise<unknown> {
66 const result = await getSchema({ client, path, query: { kind: 'input' } })
67 if (result.error !== undefined) {
68 const { provider, activity, endpointId } = path
69 throw new Error(
70 `modelschemas getSchema ${provider}/${activity}/${endpointId}: ${errorMessage(result.error)}`,
71 )
72 }
73 return result.data
75​
76/**
77 * Every synced provider's own catalog. An empty catalog throws: it would
78 * hide every insert for that provider behind a "no new models" exit 0.
79 */
80export async function fetchSyncCatalogs(
81 client: SyncClient,
82): Promise<Record<SyncedProvider, Array<CatalogModel>>> {
83 const nativeLists = await Promise.all(
84 SYNCED_PROVIDERS.map((provider) => fetchCatalog(client, provider)),
85 )
86 const native = {} as Record<SyncedProvider, Array<CatalogModel>>
87 for (const [index, provider] of SYNCED_PROVIDERS.entries()) {
88 const rows = nativeLists[index]
89 if (!rows?.length) {
90 throw new Error(`modelschemas returned an empty ${provider} catalog`)
91 }
92 native[provider] = rows
93 }
94 return native