Open History to see the last commit
1/**
2 * Grain=provider model → generation-route binding (issue #50).
3 *
4 * Catalog rows carry `schemaEndpointId` (the public endpoint id of the
5 * shared HTTP route). Schema reads also accept the model rawId / slug as
6 * an alias onto that route, pinning the request `model` field to the one
7 * id so a Run form can be built the same way as for model-grained FAL.
8 */
9import { and, eq, or } from 'drizzle-orm'
10
11import type { Db } from '#/db/index.ts'
12import { endpoints, models } from '#/db/schema.ts'
13import type { Activity } from '#/db/schema.ts'
14import { getProvider } from '#/server/providers/index.ts'
15import { resolveSpecGrain } from '#/server/providers/connect.ts'
16
17export interface SchemaBindingModel {
18 providerId: string
19 rawId: string
20 activity: Activity | null
21 capabilities?: unknown
22 /** Stored at poll time when the route depends on listing data. */
23 schemaEndpointId?: string | null
24}
25
26function isRecord(value: unknown): value is Record<string, unknown> {
27 return typeof value === 'object' && value !== null && !Array.isArray(value)
28}
29
30/**
31 * Canonical generation route for a listed model, or null when the
32 * provider has no binding (and is not model-grained).
33 */
34export function resolveSchemaEndpointId(
35 row: SchemaBindingModel,
36): string | null {
37 if (row.schemaEndpointId) return row.schemaEndpointId
38 const config = getProvider(row.providerId)
39 // A model-grained provider opting in requires a poll-verified native
40 // binding; its raw model id alone does not establish a schema endpoint.
41 if (
42 config?.bindSyncedRoutesOnly === true &&
43 resolveSpecGrain(config) === 'model'
44 )
45 return null
46 if (row.activity !== null && config?.generationEndpointId) {
47 const bound = config.generationEndpointId({
48 rawId: row.rawId,
49 activity: row.activity,
50 capabilities: row.capabilities,
51 })
52 if (bound) return bound
53 }
54 if (resolveSpecGrain(config) === 'model') return row.rawId
55 return null
56}
57
58/** Narrow a request schema's `model` property to one listed id. */
59export function pinModelField(schema: unknown, rawId: string): unknown {
60 if (!isRecord(schema)) return schema
61 const properties = schema.properties
62 if (!isRecord(properties) || !('model' in properties)) return schema
63 const existing = isRecord(properties.model) ? properties.model : {}
64 const pinned: Record<string, unknown> = {
65 ...existing,
66 type: 'string',
67 enum: [rawId],
68 }
69 delete pinned.const
70 if ('example' in pinned) pinned.example = rawId
71 if ('examples' in pinned) pinned.examples = [rawId]
72 return { ...schema, properties: { ...properties, model: pinned } }
73}
74
75export interface ResolvedEndpoint {
76 /** Db id: `${providerId}/${publicId}`. */
77 dbId: string
78 /** Public endpoint id (path-derived). */
79 publicId: string
80 activity: Activity
81 /**
82 * When the lookup was a model-id alias, the listed rawId to pin on the
83 * input schema. Null for a direct endpoint hit.
84 */
85 pinRawId: string | null
86}
87
88/**
89 * Resolve a public endpoint id OR a listed model rawId/slug to a stored
90 * endpoint. `activity` is required for schema reads (the URL includes it);
91 * validate omits it and uses the model's stored activity.
92 */
93export async function resolveEndpointAlias(
94 db: Db,
95 providerId: string,
96 endpointId: string,
97 activity?: Activity,
98): Promise<ResolvedEndpoint | null> {
99 const dbId = `${providerId}/${endpointId}`
100 const direct = await db.query.endpoints.findFirst({
101 where:
102 activity === undefined
103 ? eq(endpoints.id, dbId)
104 : and(eq(endpoints.id, dbId), eq(endpoints.activity, activity)),
105 })
106 if (direct) {
107 return {
108 dbId,
109 publicId: endpointId,
110 activity: direct.activity,
111 pinRawId: null,
112 }
113 }
114
115 const model = await db.query.models.findFirst({
116 where: and(
117 eq(models.providerId, providerId),
118 or(eq(models.id, endpointId), eq(models.rawId, endpointId)),
119 ),
120 })
121 if (!model || model.activity === null) return null
122 if (activity !== undefined && model.activity !== activity) return null
123
124 const bound = resolveSchemaEndpointId({
125 providerId: model.providerId,
126 rawId: model.rawId,
127 activity: model.activity,
128 capabilities: model.capabilities,
129 schemaEndpointId: model.schemaEndpointId,
130 })
131 if (!bound || bound === endpointId) return null
132
133 const boundDbId = `${providerId}/${bound}`
134 const boundRow = await db.query.endpoints.findFirst({
135 where: and(
136 eq(endpoints.id, boundDbId),
137 eq(endpoints.activity, model.activity),
138 ),
139 })
140 if (!boundRow) return null
141 return {
142 dbId: boundDbId,
143 publicId: bound,
144 activity: model.activity,
145 pinRawId: model.rawId,
146 }
147}