@sixb/server 0.1.21 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/dist/artifacts/frame.d.ts +5 -0
  2. package/dist/artifacts/frame.d.ts.map +1 -1
  3. package/dist/auth/browser-origin.d.ts +5 -1
  4. package/dist/auth/browser-origin.d.ts.map +1 -1
  5. package/dist/auth/experience.d.ts +1 -0
  6. package/dist/auth/experience.d.ts.map +1 -1
  7. package/dist/auth/public-routes.d.ts.map +1 -1
  8. package/dist/auth/responses.d.ts +1 -1
  9. package/dist/auth/responses.d.ts.map +1 -1
  10. package/dist/auth/scope.d.ts +19 -3
  11. package/dist/auth/scope.d.ts.map +1 -1
  12. package/dist/index.js +10093 -7553
  13. package/dist/index.js.map +51 -39
  14. package/dist/models/display.d.ts +27 -0
  15. package/dist/models/display.d.ts.map +1 -1
  16. package/dist/openapi/components.d.ts +14 -0
  17. package/dist/openapi/components.d.ts.map +1 -0
  18. package/dist/openapi/tags.d.ts +14 -2
  19. package/dist/openapi/tags.d.ts.map +1 -1
  20. package/dist/registerRoutes.d.ts.map +1 -1
  21. package/dist/routes/actions.d.ts +5 -5
  22. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  23. package/dist/routes/agent-approval-gate.d.ts +27 -0
  24. package/dist/routes/agent-approval-gate.d.ts.map +1 -0
  25. package/dist/routes/agent-approvals.d.ts +599 -0
  26. package/dist/routes/agent-approvals.d.ts.map +1 -0
  27. package/dist/routes/agent-children.d.ts +318 -0
  28. package/dist/routes/agent-children.d.ts.map +1 -0
  29. package/dist/routes/agent-memory.d.ts +267 -0
  30. package/dist/routes/agent-memory.d.ts.map +1 -0
  31. package/dist/routes/agents.d.ts +999 -80
  32. package/dist/routes/agents.d.ts.map +1 -1
  33. package/dist/routes/ai-accounting.d.ts.map +1 -1
  34. package/dist/routes/artifact-frames.d.ts +3 -3
  35. package/dist/routes/artifact-frames.d.ts.map +1 -1
  36. package/dist/routes/artifacts.d.ts +790 -134
  37. package/dist/routes/artifacts.d.ts.map +1 -1
  38. package/dist/routes/auth.d.ts +116 -10
  39. package/dist/routes/auth.d.ts.map +1 -1
  40. package/dist/routes/connector-connection-runs.d.ts +50 -50
  41. package/dist/routes/connectors.d.ts +2 -2
  42. package/dist/routes/datasets.d.ts +20 -20
  43. package/dist/routes/events.d.ts +4 -4
  44. package/dist/routes/files.d.ts +14 -14
  45. package/dist/routes/files.d.ts.map +1 -1
  46. package/dist/routes/links.d.ts +2 -2
  47. package/dist/routes/logs.d.ts +5 -5
  48. package/dist/routes/model-calls.d.ts +155 -0
  49. package/dist/routes/model-calls.d.ts.map +1 -0
  50. package/dist/routes/model-generations.d.ts +400 -0
  51. package/dist/routes/model-generations.d.ts.map +1 -0
  52. package/dist/routes/models.d.ts +434 -1
  53. package/dist/routes/models.d.ts.map +1 -1
  54. package/dist/routes/objects.d.ts +71 -40
  55. package/dist/routes/objects.d.ts.map +1 -1
  56. package/dist/routes/observed-agent-threads.d.ts +682 -0
  57. package/dist/routes/observed-agent-threads.d.ts.map +1 -0
  58. package/dist/routes/ontology.d.ts +66 -62
  59. package/dist/routes/ontology.d.ts.map +1 -1
  60. package/dist/routes/pipelines.d.ts +24 -24
  61. package/dist/routes/projections.d.ts +55 -55
  62. package/dist/routes/rules.d.ts +2 -2
  63. package/dist/routes/share-grants.d.ts +9 -9
  64. package/dist/routes/syncs.d.ts +46 -46
  65. package/dist/routes/users.d.ts +117 -0
  66. package/dist/routes/users.d.ts.map +1 -0
  67. package/dist/routes/webhook-runs.d.ts +1 -1
  68. package/dist/routes/workflows.d.ts +118 -52
  69. package/dist/routes/workflows.d.ts.map +1 -1
  70. package/dist/routes/ws/agents.d.ts +7 -2
  71. package/dist/routes/ws/agents.d.ts.map +1 -1
  72. package/dist/routes/ws/events.d.ts +6 -6
  73. package/dist/routes/ws/logs.d.ts +14 -14
  74. package/dist/schemas/actions.d.ts +15 -15
  75. package/dist/schemas/activity.d.ts +48 -28
  76. package/dist/schemas/activity.d.ts.map +1 -1
  77. package/dist/schemas/agent-approvals.d.ts +1091 -0
  78. package/dist/schemas/agent-approvals.d.ts.map +1 -0
  79. package/dist/schemas/agent-memory.d.ts +250 -0
  80. package/dist/schemas/agent-memory.d.ts.map +1 -0
  81. package/dist/schemas/agents.d.ts +5442 -369
  82. package/dist/schemas/agents.d.ts.map +1 -1
  83. package/dist/schemas/ai-accounting.d.ts +1290 -411
  84. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  85. package/dist/schemas/ai-limits.d.ts +10 -10
  86. package/dist/schemas/ai-usage.d.ts +12 -0
  87. package/dist/schemas/ai-usage.d.ts.map +1 -1
  88. package/dist/schemas/artifacts.d.ts +650 -98
  89. package/dist/schemas/artifacts.d.ts.map +1 -1
  90. package/dist/schemas/auth.d.ts +119 -46
  91. package/dist/schemas/auth.d.ts.map +1 -1
  92. package/dist/schemas/connectors.d.ts +22 -22
  93. package/dist/schemas/datasets.d.ts +98 -98
  94. package/dist/schemas/event-payloads.d.ts +6 -6
  95. package/dist/schemas/events.d.ts +26 -26
  96. package/dist/schemas/events.d.ts.map +1 -1
  97. package/dist/schemas/files.d.ts +24 -24
  98. package/dist/schemas/links.d.ts +2 -2
  99. package/dist/schemas/logs.d.ts +32 -32
  100. package/dist/schemas/models.d.ts +8889 -10
  101. package/dist/schemas/models.d.ts.map +1 -1
  102. package/dist/schemas/objects.d.ts +48 -12
  103. package/dist/schemas/objects.d.ts.map +1 -1
  104. package/dist/schemas/ontology.d.ts +127 -104
  105. package/dist/schemas/ontology.d.ts.map +1 -1
  106. package/dist/schemas/pipelines.d.ts +132 -132
  107. package/dist/schemas/projections.d.ts +314 -314
  108. package/dist/schemas/rules.d.ts +4 -4
  109. package/dist/schemas/share-grants.d.ts +30 -30
  110. package/dist/schemas/syncs.d.ts +49 -49
  111. package/dist/schemas/users.d.ts +72 -0
  112. package/dist/schemas/users.d.ts.map +1 -0
  113. package/dist/schemas/webhook-runs.d.ts +6 -6
  114. package/dist/schemas/workflows.d.ts +701 -186
  115. package/dist/schemas/workflows.d.ts.map +1 -1
  116. package/dist/server.d.ts +1 -0
  117. package/dist/server.d.ts.map +1 -1
  118. package/dist/utils/http.d.ts.map +1 -1
  119. package/package.json +7 -7
  120. package/src/artifacts/frame.ts +6 -1
  121. package/src/auth/browser-origin.ts +8 -3
  122. package/src/auth/experience.ts +1 -1
  123. package/src/auth/guard.ts +2 -2
  124. package/src/auth/public-routes.ts +6 -0
  125. package/src/auth/responses.ts +7 -6
  126. package/src/auth/scope.ts +32 -3
  127. package/src/models/display.ts +61 -16
  128. package/src/openapi/components.ts +54 -0
  129. package/src/openapi/tags.ts +12 -0
  130. package/src/registerRoutes.ts +14 -0
  131. package/src/routes/agent-api-gateway.ts +115 -91
  132. package/src/routes/agent-approval-gate.ts +213 -0
  133. package/src/routes/agent-approvals.ts +299 -0
  134. package/src/routes/agent-children.ts +209 -0
  135. package/src/routes/agent-memory.ts +210 -0
  136. package/src/routes/agents.ts +363 -142
  137. package/src/routes/ai-accounting.ts +4 -0
  138. package/src/routes/artifact-frames.ts +14 -8
  139. package/src/routes/artifacts.ts +297 -22
  140. package/src/routes/auth.ts +306 -13
  141. package/src/routes/files.ts +6 -7
  142. package/src/routes/model-calls.ts +120 -0
  143. package/src/routes/model-generations.ts +245 -0
  144. package/src/routes/models.ts +116 -9
  145. package/src/routes/objects.ts +55 -61
  146. package/src/routes/observed-agent-threads.ts +253 -0
  147. package/src/routes/ontology.ts +1 -0
  148. package/src/routes/users.ts +73 -0
  149. package/src/routes/workflows.ts +6 -9
  150. package/src/routes/ws/agents.ts +10 -5
  151. package/src/schemas/activity.ts +2 -0
  152. package/src/schemas/agent-approvals.ts +129 -0
  153. package/src/schemas/agent-memory.ts +96 -0
  154. package/src/schemas/agents.ts +239 -0
  155. package/src/schemas/ai-accounting.ts +40 -4
  156. package/src/schemas/ai-usage.ts +10 -0
  157. package/src/schemas/artifacts.ts +91 -3
  158. package/src/schemas/auth.ts +25 -0
  159. package/src/schemas/models.ts +197 -1
  160. package/src/schemas/objects.ts +181 -0
  161. package/src/schemas/ontology.ts +1 -0
  162. package/src/schemas/users.ts +24 -0
  163. package/src/schemas/workflows.ts +2 -0
  164. package/src/server.ts +9 -10
  165. package/src/utils/http.ts +2 -0
@@ -0,0 +1,245 @@
1
+ import type { LanguageModelCatalog, LanguageModelTier, SixbHostView } from "@sixb/core"
2
+ import type { FileRef } from "@sixb/core/blob-storage/server"
3
+ import { selectLanguageModel } from "@sixb/core/internal/model-execution"
4
+ import {
5
+ type LanguageModel,
6
+ type ModelCallCost,
7
+ ModelProviderError,
8
+ ModelStreamError,
9
+ type ModelUsage,
10
+ } from "@sixb/core/models"
11
+ import type { Elysia } from "elysia"
12
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
13
+ import { requireRequestSixb } from "../auth/scope"
14
+ import { OPENAPI_TAGS } from "../openapi/tags"
15
+ import { AiUsageLimitErrorResponseSchema, ErrorResponseSchema } from "../schemas/common"
16
+ import {
17
+ FileGenerationSchema,
18
+ ImageGenerationBodySchema,
19
+ SpeechGenerationBodySchema,
20
+ TextGenerationBodySchema,
21
+ TextGenerationSchema,
22
+ TranscriptionBodySchema,
23
+ TranscriptionSchema,
24
+ } from "../schemas/models"
25
+ import { handleRouteError } from "../utils/http"
26
+
27
+ type ModelKind = "language" | "image" | "speech" | "transcription"
28
+
29
+ interface ModelRef {
30
+ readonly provider: string
31
+ readonly modelId: string
32
+ }
33
+
34
+ /** Thrown before the call when a request names a model the catalog does not have. */
35
+ class UnknownModelError extends Error {
36
+ constructor(
37
+ readonly kind: ModelKind,
38
+ readonly ref: ModelRef | undefined
39
+ ) {
40
+ super(
41
+ ref
42
+ ? `[SixbServer] No ${kind} model '${ref.provider}/${ref.modelId}' is configured. GET /api/models lists them.`
43
+ : `[SixbServer] No ${kind} models are configured.`
44
+ )
45
+ }
46
+ }
47
+
48
+ interface CatalogKind<TModel> {
49
+ readonly default: ModelRef & { readonly model: TModel }
50
+ getByRef(ref: ModelRef): (ModelRef & { readonly model: TModel }) | null
51
+ }
52
+
53
+ /** The catalog's binding: the requested one, or the kind's default. */
54
+ function selectBinding<TModel>(
55
+ catalog: CatalogKind<TModel> | undefined,
56
+ kind: ModelKind,
57
+ ref: ModelRef | undefined
58
+ ): ModelRef & { readonly model: TModel } {
59
+ const entry = ref ? catalog?.getByRef(ref) : catalog?.default
60
+ if (!entry) throw new UnknownModelError(kind, ref)
61
+ return entry
62
+ }
63
+
64
+ /** The requested binding, or a tier's model as `generate({ model: tier })` selects it. */
65
+ function selectLanguageBinding(
66
+ catalog: LanguageModelCatalog | undefined,
67
+ model: ModelRef | LanguageModelTier | undefined
68
+ ): LanguageModel {
69
+ if (typeof model !== "string") return selectBinding(catalog, "language", model).model
70
+
71
+ const selected = selectLanguageModel(catalog, model)
72
+ if (!selected) throw new UnknownModelError("language", undefined)
73
+ return selected
74
+ }
75
+
76
+ function result(
77
+ entry: ModelRef,
78
+ call: { readonly callId: string; readonly usage: ModelUsage; readonly cost: ModelCallCost }
79
+ ) {
80
+ const { raw: _, ...usage } = call.usage
81
+ const cost = call.cost
82
+ return {
83
+ callId: call.callId,
84
+ model: { provider: entry.provider, modelId: entry.modelId },
85
+ usage,
86
+ cost: {
87
+ status: cost.status,
88
+ ...("money" in cost ? { money: cost.money } : {}),
89
+ ...(cost.status === "unpriceable" ? { reason: cost.reason } : {}),
90
+ },
91
+ }
92
+ }
93
+
94
+ function generationError(error: unknown, set: { status?: number | string }) {
95
+ if (error instanceof UnknownModelError) {
96
+ set.status = error.ref ? 400 : 404
97
+ return { error: error.message }
98
+ }
99
+ // The provider failed or answered with something unusable; the request itself was fine.
100
+ if (error instanceof ModelProviderError || error instanceof ModelStreamError) {
101
+ set.status = 502
102
+ return { error: error.message }
103
+ }
104
+ return handleRouteError(error, set)
105
+ }
106
+
107
+ const responses = {
108
+ 400: ErrorResponseSchema,
109
+ 403: ErrorResponseSchema,
110
+ 404: ErrorResponseSchema,
111
+ 429: AiUsageLimitErrorResponseSchema,
112
+ 502: ErrorResponseSchema,
113
+ }
114
+
115
+ function detail(operationId: string, summary: string) {
116
+ return {
117
+ summary,
118
+ tags: [OPENAPI_TAGS.models.name],
119
+ operationId,
120
+ security: accessTokenSecurityRequirement(operationId),
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Call the project's models directly. Each route makes the same runtime call project code does, in
126
+ * the request's execution, so accounting, AI limits and the files record apply unchanged. Calls
127
+ * need `can.run(project.models)`.
128
+ */
129
+ export function registerModelGenerationRoutes(app: Elysia, host: SixbHostView) {
130
+ const catalog = () => host.definitions.models
131
+
132
+ return app
133
+ .post(
134
+ "/api/models/language/generations",
135
+ async (context) => {
136
+ const { body, request, set } = context
137
+ try {
138
+ const sixb = requireRequestSixb(context)
139
+ const model = selectLanguageBinding(catalog()?.language, body.model)
140
+ const call = await sixb.models.language.generate({
141
+ model,
142
+ prompt: body.prompt,
143
+ ...(body.instructions === undefined ? {} : { instructions: body.instructions }),
144
+ ...(body.maxOutputTokens === undefined
145
+ ? {}
146
+ : { maxOutputTokens: body.maxOutputTokens }),
147
+ ...(body.reasoning === undefined ? {} : { reasoning: body.reasoning }),
148
+ signal: request.signal,
149
+ })
150
+ return TextGenerationSchema.parse({
151
+ ...result({ provider: model.providerId, modelId: model.modelId }, call),
152
+ text: call.output,
153
+ finishReason: call.finishReason,
154
+ })
155
+ } catch (error) {
156
+ return generationError(error, set)
157
+ }
158
+ },
159
+ {
160
+ body: TextGenerationBodySchema,
161
+ response: { 200: TextGenerationSchema, ...responses },
162
+ detail: detail("generateText", "Generate text with a language model"),
163
+ }
164
+ )
165
+ .post(
166
+ "/api/models/image/generations",
167
+ async (context) => {
168
+ const { body, request, set } = context
169
+ try {
170
+ const sixb = requireRequestSixb(context)
171
+ const { model: ref, images, ...options } = body
172
+ const entry = selectBinding(catalog()?.image, "image", ref)
173
+ const call = await sixb.models.image.generate({
174
+ ...options,
175
+ ...(images === undefined ? {} : { images: images as readonly FileRef[] }),
176
+ model: entry.model,
177
+ signal: request.signal,
178
+ })
179
+ return FileGenerationSchema.parse({
180
+ ...result(entry, call),
181
+ files: call.files,
182
+ warnings: call.warnings,
183
+ })
184
+ } catch (error) {
185
+ return generationError(error, set)
186
+ }
187
+ },
188
+ {
189
+ body: ImageGenerationBodySchema,
190
+ response: { 200: FileGenerationSchema, ...responses },
191
+ detail: detail("generateImages", "Generate images with an image model"),
192
+ }
193
+ )
194
+ .post(
195
+ "/api/models/audio/speech",
196
+ async (context) => {
197
+ const { body, request, set } = context
198
+ try {
199
+ const sixb = requireRequestSixb(context)
200
+ const { model: ref, ...options } = body
201
+ const entry = selectBinding(catalog()?.audio?.speech, "speech", ref)
202
+ const call = await sixb.models.audio.speak({
203
+ ...options,
204
+ model: entry.model,
205
+ signal: request.signal,
206
+ })
207
+ return FileGenerationSchema.parse({
208
+ ...result(entry, call),
209
+ files: [call.file],
210
+ warnings: call.warnings,
211
+ })
212
+ } catch (error) {
213
+ return generationError(error, set)
214
+ }
215
+ },
216
+ {
217
+ body: SpeechGenerationBodySchema,
218
+ response: { 200: FileGenerationSchema, ...responses },
219
+ detail: detail("generateSpeech", "Generate speech with a speech model"),
220
+ }
221
+ )
222
+ .post(
223
+ "/api/models/audio/transcriptions",
224
+ async (context) => {
225
+ const { body, request, set } = context
226
+ try {
227
+ const sixb = requireRequestSixb(context)
228
+ const entry = selectBinding(catalog()?.audio?.transcription, "transcription", body.model)
229
+ const call = await sixb.models.audio.transcribe({
230
+ audio: body.audio as FileRef,
231
+ model: entry.model,
232
+ signal: request.signal,
233
+ })
234
+ return TranscriptionSchema.parse({ ...result(entry, call), text: call.output.text })
235
+ } catch (error) {
236
+ return generationError(error, set)
237
+ }
238
+ },
239
+ {
240
+ body: TranscriptionBodySchema,
241
+ response: { 200: TranscriptionSchema, ...responses },
242
+ detail: detail("transcribeAudio", "Transcribe audio with a transcription model"),
243
+ }
244
+ )
245
+ }
@@ -1,6 +1,14 @@
1
1
  import type { LanguageModelEntry, SixbHostView } from "@sixb/core"
2
+ import type {
3
+ ImageModelEntry,
4
+ ModelDefinition,
5
+ ModelRateCard,
6
+ SpeechModelEntry,
7
+ TranscriptionModelEntry,
8
+ } from "@sixb/core/models"
2
9
  import type { Elysia } from "elysia"
3
- import { languageModelDisplay } from "../models/display"
10
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
11
+ import { languageModelDisplay, rateCardDisplay, resolvedForDisplay } from "../models/display"
4
12
  import { OPENAPI_TAGS } from "../openapi/tags"
5
13
  import { ModelCatalogSchema } from "../schemas/models"
6
14
 
@@ -10,32 +18,131 @@ async function serializeLanguageModel(entry: LanguageModelEntry, defaultEntry: L
10
18
  provider: entry.provider,
11
19
  modelId: entry.modelId,
12
20
  isDefault: entry.provider === defaultEntry.provider && entry.modelId === defaultEntry.modelId,
21
+ ...(entry.tier === undefined ? {} : { tier: entry.tier }),
13
22
  ...(entry.model.defaultReasoning === undefined
14
23
  ? {}
15
24
  : { defaultReasoning: entry.model.defaultReasoning }),
16
25
  }
17
26
  }
18
27
 
28
+ /** What every kind's entry shares: identity, the default, how to describe it, and its prices. */
29
+ function catalogEntry(
30
+ entry: { readonly provider: string; readonly modelId: string },
31
+ defaultEntry: { readonly provider: string; readonly modelId: string },
32
+ model: { readonly definition: ModelDefinition; readonly rateCard?: ModelRateCard }
33
+ ) {
34
+ const definition = model.definition
35
+ return {
36
+ provider: entry.provider,
37
+ modelId: entry.modelId,
38
+ isDefault: entry.provider === defaultEntry.provider && entry.modelId === defaultEntry.modelId,
39
+ name: definition.name ?? entry.modelId,
40
+ ...(definition.description ? { description: definition.description } : {}),
41
+ ...(definition.via ? { via: definition.via } : {}),
42
+ ...rateCardDisplay(entry, model.rateCard),
43
+ }
44
+ }
45
+
46
+ async function serializeImageModel(entry: ImageModelEntry, defaultEntry: ImageModelEntry) {
47
+ const model = await resolvedForDisplay(entry)
48
+ const definition = model.definition
49
+ return {
50
+ ...catalogEntry(entry, defaultEntry, model),
51
+ ...(definition.maxImagesPerCall === undefined
52
+ ? {}
53
+ : { maxImagesPerCall: definition.maxImagesPerCall }),
54
+ ...(definition.sizes ? { sizes: [...definition.sizes] } : {}),
55
+ ...(definition.aspectRatios ? { aspectRatios: [...definition.aspectRatios] } : {}),
56
+ ...(definition.qualities ? { qualities: [...definition.qualities] } : {}),
57
+ ...(definition.styles ? { styles: [...definition.styles] } : {}),
58
+ ...(definition.inputImages
59
+ ? {
60
+ inputImages: {
61
+ maxCount: definition.inputImages.maxCount,
62
+ maxBytes: definition.inputImages.maxBytes,
63
+ ...(definition.inputImages.mediaTypes
64
+ ? { mediaTypes: [...definition.inputImages.mediaTypes] }
65
+ : {}),
66
+ },
67
+ }
68
+ : {}),
69
+ }
70
+ }
71
+
72
+ async function serializeSpeechModel(entry: SpeechModelEntry, defaultEntry: SpeechModelEntry) {
73
+ const model = await resolvedForDisplay(entry)
74
+ const definition = model.definition
75
+ return {
76
+ ...catalogEntry(entry, defaultEntry, model),
77
+ ...(model.defaultVoice === undefined ? {} : { defaultVoice: model.defaultVoice }),
78
+ ...(definition.voices ? { voices: [...definition.voices] } : {}),
79
+ ...(definition.outputMediaTypes ? { outputMediaTypes: [...definition.outputMediaTypes] } : {}),
80
+ ...(definition.maxInputCharacters === undefined
81
+ ? {}
82
+ : { maxInputCharacters: definition.maxInputCharacters }),
83
+ ...(definition.instructions === undefined ? {} : { instructions: definition.instructions }),
84
+ }
85
+ }
86
+
87
+ async function serializeTranscriptionModel(
88
+ entry: TranscriptionModelEntry,
89
+ defaultEntry: TranscriptionModelEntry
90
+ ) {
91
+ const model = await resolvedForDisplay(entry)
92
+ const definition = model.definition
93
+ return {
94
+ ...catalogEntry(entry, defaultEntry, model),
95
+ maxInputBytes: definition.maxInputBytes,
96
+ ...(definition.mediaTypes ? { mediaTypes: [...definition.mediaTypes] } : {}),
97
+ }
98
+ }
99
+
19
100
  export function registerModelRoutes(app: Elysia, host: SixbHostView) {
20
101
  return app.get(
21
102
  "/api/models",
22
103
  async () => {
23
- const language = host.definitions.models?.language
104
+ const models = host.definitions.models
105
+ const language = models?.language
106
+ const image = models?.image
107
+ const speech = models?.audio?.speech
108
+ const transcription = models?.audio?.transcription
24
109
  return ModelCatalogSchema.parse({
25
- language:
26
- language === undefined
27
- ? []
28
- : await Promise.all(
29
- language.list().map((entry) => serializeLanguageModel(entry, language.default))
30
- ),
110
+ language: language
111
+ ? await Promise.all(
112
+ language.list().map((entry) => serializeLanguageModel(entry, language.default))
113
+ )
114
+ : [],
115
+ ...(language && language.tiers.length > 0
116
+ ? { languageRouting: models.decision !== undefined }
117
+ : {}),
118
+ image: image
119
+ ? await Promise.all(
120
+ image.list().map((entry) => serializeImageModel(entry, image.default))
121
+ )
122
+ : [],
123
+ audio: {
124
+ speech: speech
125
+ ? await Promise.all(
126
+ speech.list().map((entry) => serializeSpeechModel(entry, speech.default))
127
+ )
128
+ : [],
129
+ transcription: transcription
130
+ ? await Promise.all(
131
+ transcription
132
+ .list()
133
+ .map((entry) => serializeTranscriptionModel(entry, transcription.default))
134
+ )
135
+ : [],
136
+ },
31
137
  })
32
138
  },
33
139
  {
34
140
  response: { 200: ModelCatalogSchema },
35
141
  detail: {
36
- summary: "List the project model catalog",
142
+ summary: "List the project's models of every kind, with their options and prices",
37
143
  tags: [OPENAPI_TAGS.models.name],
38
144
  operationId: "listModels",
145
+ security: accessTokenSecurityRequirement("listModels"),
39
146
  },
40
147
  }
41
148
  )
@@ -7,13 +7,9 @@ import {
7
7
  type ObjectRef,
8
8
  type SixbHostView,
9
9
  } from "@sixb/core"
10
- import type {
11
- ExpandedLinkValue,
12
- ExpandedObjectRow,
13
- ObjectRow,
14
- ObjectRowLinks,
15
- Redactions,
16
- } from "@sixb/core/storage"
10
+ import { createSixbError } from "@sixb/core/internal/errors"
11
+ import { serializeExpandedObject, serializeObject } from "@sixb/core/internal/objects"
12
+ import type { ObjectRow } from "@sixb/core/storage"
17
13
  import type { Elysia } from "elysia"
18
14
  import { ZodError, z } from "zod"
19
15
  import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
@@ -27,7 +23,9 @@ import { OPENAPI_TAGS } from "../openapi/tags"
27
23
  import { ErrorResponseSchema } from "../schemas/common"
28
24
  import { FileContentQuerySchema } from "../schemas/files"
29
25
  import {
26
+ GetObjectByUniquePropertyBodySchema,
30
27
  ObjectListResponseSchema,
28
+ ObjectLookupNotFoundResponseSchema,
31
29
  ObjectParamsSchema,
32
30
  ObjectQueryCountRequestSchema,
33
31
  ObjectQueryExistsRequestSchema,
@@ -38,6 +36,8 @@ import {
38
36
  ObjectSearchResponseSchema,
39
37
  ObjectsQuerySchema,
40
38
  TwinObjectSchema,
39
+ UniquePropertyParamsSchema,
40
+ UniqueViolationErrorResponseSchema,
41
41
  UpsertObjectBodySchema,
42
42
  } from "../schemas/objects"
43
43
  import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
@@ -58,24 +58,9 @@ const ObjectFileContentQuerySchema = ObjectFileContentRouteQuerySchema.extend({
58
58
  .regex(/^\/properties(?:\/|$)/, "Object file content paths must start with /properties/"),
59
59
  })
60
60
 
61
- function serializeObject(row: {
62
- score?: number
63
- primaryId: string
64
- objectTypeId: string
65
- properties: Record<string, unknown>
66
- redactions?: Redactions
67
- createdAt: Date
68
- updatedAt: Date
69
- }) {
70
- return {
71
- ...(row.score === undefined ? {} : { score: row.score }),
72
- primaryId: row.primaryId,
73
- objectTypeId: row.objectTypeId,
74
- properties: row.properties,
75
- ...(row.redactions === undefined ? {} : { redactions: row.redactions }),
76
- createdAt: toIsoString(row.createdAt),
77
- updatedAt: toIsoString(row.updatedAt),
78
- }
61
+ /** The lookup's failure when no object visible to the caller holds the value. */
62
+ function noHolderError() {
63
+ return createSixbError("object.not_found", "Object not found")
79
64
  }
80
65
 
81
66
  function serializeObjectLink(link: {
@@ -104,41 +89,6 @@ function serializeObjectLink(link: {
104
89
  }
105
90
  }
106
91
 
107
- type SerializedObject = ReturnType<typeof serializeObject>
108
- type SerializedLinkValue = SerializedExpandedObject | SerializedExpandedObject[] | null
109
- type SerializedExpandedObject = SerializedObject & {
110
- links?: Record<string, SerializedLinkValue>
111
- linkProperties?: Record<string, unknown>
112
- }
113
-
114
- /**
115
- * Serialize a query row together with any `links` attached by an `expand` node.
116
- * Recurses through nested expansions; `linkProperties` carries edge metadata on
117
- * an expanded child. Only the query route hydrates links, so the plain list and
118
- * identity routes keep using `serializeObject`.
119
- */
120
- function serializeExpandedObject(row: ExpandedObjectRow): SerializedExpandedObject {
121
- const serialized: SerializedExpandedObject = serializeObject(row)
122
- if (row.links) serialized.links = serializeLinks(row.links)
123
- if (row.linkProperties !== undefined) serialized.linkProperties = row.linkProperties
124
- return serialized
125
- }
126
-
127
- function serializeLinks(links: ObjectRowLinks): Record<string, SerializedLinkValue> {
128
- const result: Record<string, SerializedLinkValue> = {}
129
- for (const [linkId, value] of Object.entries(links)) {
130
- result[linkId] = serializeLinkValue(value)
131
- }
132
- return result
133
- }
134
-
135
- function serializeLinkValue(value: ExpandedLinkValue): SerializedLinkValue {
136
- if (value === null) return null
137
- if (Array.isArray(value)) return value.map(serializeExpandedObject)
138
- // Array.isArray does not narrow a readonly[] away, so assert the scalar case.
139
- return serializeExpandedObject(value as ExpandedObjectRow)
140
- }
141
-
142
92
  function serializePlan(plan: {
143
93
  readonly mode: string
144
94
  readonly providerIssues: readonly unknown[]
@@ -155,7 +105,7 @@ function serializePlan(plan: {
155
105
  }
156
106
  }
157
107
 
158
- function handleObjectQueryError(error: unknown, set: { status?: number | string }) {
108
+ export function handleObjectQueryError(error: unknown, set: { status?: number | string }) {
159
109
  if (error instanceof AuthorizationError) {
160
110
  set.status = 403
161
111
  return { error: error.message }
@@ -900,6 +850,49 @@ export function registerObjectRoutes(app: Elysia, host: SixbHostView) {
900
850
  },
901
851
  }
902
852
  )
853
+ .post(
854
+ "/api/objects/:objectTypeId/by/:propertyId",
855
+ async (context) => {
856
+ const { params, body, set } = context
857
+ const sixb = requireRequestSixb(context)
858
+
859
+ try {
860
+ const row = await sixb.objects.getBy(params.objectTypeId, params.propertyId, body.value)
861
+ if (!row) return handleRouteError(noHolderError(), set)
862
+
863
+ return serializeObject(row)
864
+ } catch (error) {
865
+ if (!(error instanceof AuthorizationError)) return handleRouteError(error, set)
866
+
867
+ // A type the caller cannot view looks missing, like `getObject`. On a type it views, the
868
+ // refusal depends only on the request, as for a filter on a marked property.
869
+ if (!sixb.objects.getTypeById(params.objectTypeId)) {
870
+ return handleRouteError(noHolderError(), set)
871
+ }
872
+
873
+ set.status = 403
874
+ return { error: error.message }
875
+ }
876
+ },
877
+ {
878
+ params: UniquePropertyParamsSchema,
879
+ body: GetObjectByUniquePropertyBodySchema,
880
+ response: {
881
+ 200: TwinObjectSchema,
882
+ 400: ErrorResponseSchema,
883
+ 403: ErrorResponseSchema,
884
+ 404: ObjectLookupNotFoundResponseSchema,
885
+ },
886
+ detail: {
887
+ summary: "Get object by a unique property",
888
+ description:
889
+ "Returns the object of exactly this type that holds the value, compared as writes store it.",
890
+ tags: [OPENAPI_TAGS.objects.name],
891
+ operationId: "getObjectByUniqueProperty",
892
+ security: accessTokenSecurityRequirement("getObjectByUniqueProperty"),
893
+ },
894
+ }
895
+ )
903
896
  .put(
904
897
  "/api/objects/:objectTypeId/:objectId",
905
898
  async (context) => {
@@ -930,6 +923,7 @@ export function registerObjectRoutes(app: Elysia, host: SixbHostView) {
930
923
  400: ErrorResponseSchema,
931
924
  403: ErrorResponseSchema,
932
925
  404: ErrorResponseSchema,
926
+ 409: UniqueViolationErrorResponseSchema,
933
927
  },
934
928
  detail: {
935
929
  summary: "Create or update object",