@sixb/server 0.1.20 → 0.1.22

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 (179) hide show
  1. package/dist/artifacts/frame.d.ts +18 -0
  2. package/dist/artifacts/frame.d.ts.map +1 -0
  3. package/dist/auth/browser-origin.d.ts +10 -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/files/content.d.ts +2 -3
  13. package/dist/files/content.d.ts.map +1 -1
  14. package/dist/index.js +10636 -7071
  15. package/dist/index.js.map +56 -38
  16. package/dist/models/display.d.ts +27 -0
  17. package/dist/models/display.d.ts.map +1 -1
  18. package/dist/openapi/components.d.ts +14 -0
  19. package/dist/openapi/components.d.ts.map +1 -0
  20. package/dist/openapi/tags.d.ts +22 -2
  21. package/dist/openapi/tags.d.ts.map +1 -1
  22. package/dist/registerRoutes.d.ts +3 -0
  23. package/dist/registerRoutes.d.ts.map +1 -1
  24. package/dist/routes/actions.d.ts +5 -5
  25. package/dist/routes/activity.d.ts +35 -0
  26. package/dist/routes/activity.d.ts.map +1 -0
  27. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  28. package/dist/routes/agent-approval-gate.d.ts +27 -0
  29. package/dist/routes/agent-approval-gate.d.ts.map +1 -0
  30. package/dist/routes/agent-approvals.d.ts +599 -0
  31. package/dist/routes/agent-approvals.d.ts.map +1 -0
  32. package/dist/routes/agent-children.d.ts +318 -0
  33. package/dist/routes/agent-children.d.ts.map +1 -0
  34. package/dist/routes/agent-memory.d.ts +267 -0
  35. package/dist/routes/agent-memory.d.ts.map +1 -0
  36. package/dist/routes/agents.d.ts +1008 -98
  37. package/dist/routes/agents.d.ts.map +1 -1
  38. package/dist/routes/ai-accounting.d.ts.map +1 -1
  39. package/dist/routes/artifact-frames.d.ts +98 -0
  40. package/dist/routes/artifact-frames.d.ts.map +1 -0
  41. package/dist/routes/artifacts.d.ts +1508 -0
  42. package/dist/routes/artifacts.d.ts.map +1 -0
  43. package/dist/routes/auth.d.ts +117 -11
  44. package/dist/routes/auth.d.ts.map +1 -1
  45. package/dist/routes/connector-connection-runs.d.ts +84 -84
  46. package/dist/routes/connector-connections.d.ts +2 -2
  47. package/dist/routes/connectors.d.ts +2 -2
  48. package/dist/routes/datasets.d.ts +27 -27
  49. package/dist/routes/events.d.ts +5 -5
  50. package/dist/routes/files.d.ts +14 -14
  51. package/dist/routes/files.d.ts.map +1 -1
  52. package/dist/routes/links.d.ts +2 -2
  53. package/dist/routes/logs.d.ts +5 -5
  54. package/dist/routes/model-calls.d.ts +155 -0
  55. package/dist/routes/model-calls.d.ts.map +1 -0
  56. package/dist/routes/model-generations.d.ts +400 -0
  57. package/dist/routes/model-generations.d.ts.map +1 -0
  58. package/dist/routes/models.d.ts +434 -1
  59. package/dist/routes/models.d.ts.map +1 -1
  60. package/dist/routes/objects.d.ts +71 -40
  61. package/dist/routes/objects.d.ts.map +1 -1
  62. package/dist/routes/observed-agent-threads.d.ts +682 -0
  63. package/dist/routes/observed-agent-threads.d.ts.map +1 -0
  64. package/dist/routes/ontology.d.ts +66 -62
  65. package/dist/routes/ontology.d.ts.map +1 -1
  66. package/dist/routes/pipelines.d.ts +53 -53
  67. package/dist/routes/projections.d.ts +63 -63
  68. package/dist/routes/rules.d.ts +2 -2
  69. package/dist/routes/share-grants.d.ts +4 -4
  70. package/dist/routes/syncs.d.ts +18 -18
  71. package/dist/routes/users.d.ts +117 -0
  72. package/dist/routes/users.d.ts.map +1 -0
  73. package/dist/routes/webhook-runs.d.ts +1 -1
  74. package/dist/routes/workflows.d.ts +142 -76
  75. package/dist/routes/workflows.d.ts.map +1 -1
  76. package/dist/routes/ws/agents.d.ts +7 -2
  77. package/dist/routes/ws/agents.d.ts.map +1 -1
  78. package/dist/routes/ws/events.d.ts +6 -6
  79. package/dist/routes/ws/events.d.ts.map +1 -1
  80. package/dist/routes/ws/logs.d.ts +14 -14
  81. package/dist/routes/ws/subscription-hub.d.ts +8 -3
  82. package/dist/routes/ws/subscription-hub.d.ts.map +1 -1
  83. package/dist/schemas/actions.d.ts +15 -15
  84. package/dist/schemas/activity.d.ts +1298 -0
  85. package/dist/schemas/activity.d.ts.map +1 -0
  86. package/dist/schemas/agent-approvals.d.ts +1091 -0
  87. package/dist/schemas/agent-approvals.d.ts.map +1 -0
  88. package/dist/schemas/agent-memory.d.ts +250 -0
  89. package/dist/schemas/agent-memory.d.ts.map +1 -0
  90. package/dist/schemas/agents.d.ts +5485 -460
  91. package/dist/schemas/agents.d.ts.map +1 -1
  92. package/dist/schemas/ai-accounting.d.ts +1315 -436
  93. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  94. package/dist/schemas/ai-limits.d.ts +36 -36
  95. package/dist/schemas/ai-usage.d.ts +12 -0
  96. package/dist/schemas/ai-usage.d.ts.map +1 -1
  97. package/dist/schemas/artifacts.d.ts +1857 -0
  98. package/dist/schemas/artifacts.d.ts.map +1 -0
  99. package/dist/schemas/auth.d.ts +125 -52
  100. package/dist/schemas/auth.d.ts.map +1 -1
  101. package/dist/schemas/connectors.d.ts +42 -42
  102. package/dist/schemas/datasets.d.ts +108 -108
  103. package/dist/schemas/event-payloads.d.ts +6 -6
  104. package/dist/schemas/events.d.ts +32 -32
  105. package/dist/schemas/events.d.ts.map +1 -1
  106. package/dist/schemas/files.d.ts +24 -24
  107. package/dist/schemas/links.d.ts +2 -2
  108. package/dist/schemas/logs.d.ts +32 -32
  109. package/dist/schemas/models.d.ts +8889 -10
  110. package/dist/schemas/models.d.ts.map +1 -1
  111. package/dist/schemas/objects.d.ts +48 -12
  112. package/dist/schemas/objects.d.ts.map +1 -1
  113. package/dist/schemas/ontology.d.ts +127 -104
  114. package/dist/schemas/ontology.d.ts.map +1 -1
  115. package/dist/schemas/pipelines.d.ts +166 -166
  116. package/dist/schemas/projections.d.ts +346 -346
  117. package/dist/schemas/rules.d.ts +4 -4
  118. package/dist/schemas/share-grants.d.ts +14 -14
  119. package/dist/schemas/syncs.d.ts +40 -40
  120. package/dist/schemas/users.d.ts +72 -0
  121. package/dist/schemas/users.d.ts.map +1 -0
  122. package/dist/schemas/webhook-runs.d.ts +6 -6
  123. package/dist/schemas/workflows.d.ts +769 -254
  124. package/dist/schemas/workflows.d.ts.map +1 -1
  125. package/dist/server.d.ts +1 -0
  126. package/dist/server.d.ts.map +1 -1
  127. package/dist/utils/http.d.ts.map +1 -1
  128. package/package.json +7 -7
  129. package/src/artifacts/frame.ts +51 -0
  130. package/src/auth/browser-origin.ts +20 -5
  131. package/src/auth/experience.ts +1 -1
  132. package/src/auth/guard.ts +2 -2
  133. package/src/auth/public-routes.ts +16 -0
  134. package/src/auth/responses.ts +7 -6
  135. package/src/auth/scope.ts +32 -3
  136. package/src/files/content.ts +9 -13
  137. package/src/models/display.ts +61 -16
  138. package/src/openapi/components.ts +54 -0
  139. package/src/openapi/tags.ts +20 -0
  140. package/src/registerRoutes.ts +23 -0
  141. package/src/routes/activity.ts +327 -0
  142. package/src/routes/agent-api-gateway.ts +115 -91
  143. package/src/routes/agent-approval-gate.ts +213 -0
  144. package/src/routes/agent-approvals.ts +299 -0
  145. package/src/routes/agent-children.ts +209 -0
  146. package/src/routes/agent-memory.ts +210 -0
  147. package/src/routes/agents.ts +364 -143
  148. package/src/routes/ai-accounting.ts +4 -0
  149. package/src/routes/artifact-frames.ts +144 -0
  150. package/src/routes/artifacts.ts +717 -0
  151. package/src/routes/auth.ts +306 -13
  152. package/src/routes/file-downloads.ts +1 -3
  153. package/src/routes/files.ts +6 -7
  154. package/src/routes/model-calls.ts +120 -0
  155. package/src/routes/model-generations.ts +245 -0
  156. package/src/routes/models.ts +116 -9
  157. package/src/routes/objects.ts +55 -61
  158. package/src/routes/observed-agent-threads.ts +253 -0
  159. package/src/routes/ontology.ts +1 -0
  160. package/src/routes/users.ts +73 -0
  161. package/src/routes/workflows.ts +6 -9
  162. package/src/routes/ws/agents.ts +10 -5
  163. package/src/routes/ws/events.ts +3 -2
  164. package/src/routes/ws/subscription-hub.ts +53 -20
  165. package/src/schemas/activity.ts +287 -0
  166. package/src/schemas/agent-approvals.ts +129 -0
  167. package/src/schemas/agent-memory.ts +96 -0
  168. package/src/schemas/agents.ts +240 -2
  169. package/src/schemas/ai-accounting.ts +40 -4
  170. package/src/schemas/ai-usage.ts +10 -0
  171. package/src/schemas/artifacts.ts +246 -0
  172. package/src/schemas/auth.ts +25 -0
  173. package/src/schemas/models.ts +197 -1
  174. package/src/schemas/objects.ts +181 -0
  175. package/src/schemas/ontology.ts +1 -0
  176. package/src/schemas/users.ts +24 -0
  177. package/src/schemas/workflows.ts +2 -0
  178. package/src/server.ts +34 -19
  179. package/src/utils/http.ts +2 -0
@@ -0,0 +1,717 @@
1
+ import {
2
+ type ArtifactAccess,
3
+ type ArtifactContentInput,
4
+ type ArtifactRecord,
5
+ ArtifactRequestError,
6
+ type ArtifactVersionRecord,
7
+ isFileRef,
8
+ type PublishedArtifact,
9
+ type SixbHostView,
10
+ } from "@sixb/core"
11
+ import {
12
+ createArtifactFrame,
13
+ serializeArtifactQueryResult,
14
+ } from "@sixb/core/internal/artifacts/frames"
15
+ import type { Elysia } from "elysia"
16
+ import { ZodError } from "zod"
17
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
18
+ import { requireRequestSixb } from "../auth/scope"
19
+ import {
20
+ createFileContentResponse,
21
+ fileContentGetResponses,
22
+ fileContentHeadResponses,
23
+ } from "../files/content"
24
+ import { OPENAPI_TAGS } from "../openapi/tags"
25
+ import {
26
+ ArtifactErrorResponseSchema,
27
+ ArtifactFrameResponseSchema,
28
+ ArtifactListQuerySchema,
29
+ ArtifactListResponseSchema,
30
+ ArtifactParamsSchema,
31
+ ArtifactQueryParamsSchema,
32
+ ArtifactResponseSchema,
33
+ ArtifactStateParamsSchema,
34
+ ArtifactStateResponseSchema,
35
+ ArtifactVersionParamsSchema,
36
+ ArtifactVersionSchema,
37
+ ClearArtifactStateResponseSchema,
38
+ DeleteArtifactResponseSchema,
39
+ DeleteArtifactStateResponseSchema,
40
+ PublishArtifactBodySchema,
41
+ PublishArtifactVersionBodySchema,
42
+ PublishedArtifactResponseSchema,
43
+ SetArtifactStateBodySchema,
44
+ SetArtifactStateResponseSchema,
45
+ } from "../schemas/artifacts"
46
+ import { FileContentQuerySchema } from "../schemas/files"
47
+ import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
48
+ import { handleObjectQueryError } from "./objects"
49
+
50
+ type RouteSet = { status?: number | string }
51
+
52
+ // The two below build response bodies; each route parses its whole response once.
53
+ function artifactBody(artifact: ArtifactRecord) {
54
+ return {
55
+ id: artifact.id,
56
+ projectId: artifact.projectId,
57
+ kind: artifact.kind,
58
+ title: artifact.title,
59
+ owner: artifact.owner,
60
+ latestVersion: artifact.latestVersion,
61
+ createdAt: toIsoString(artifact.createdAt),
62
+ updatedAt: toIsoString(artifact.updatedAt),
63
+ }
64
+ }
65
+
66
+ function versionBody(version: ArtifactVersionRecord) {
67
+ return {
68
+ artifactId: version.artifactId,
69
+ version: version.version,
70
+ title: version.title,
71
+ entry: version.entry,
72
+ files: version.files,
73
+ outline: version.outline,
74
+ references: version.references,
75
+ author: version.author,
76
+ origin: version.origin,
77
+ createdAt: toIsoString(version.createdAt),
78
+ }
79
+ }
80
+
81
+ function serializePublished(published: PublishedArtifact) {
82
+ return PublishedArtifactResponseSchema.parse({
83
+ artifact: artifactBody(published.artifact),
84
+ version: versionBody(published.version),
85
+ warnings: published.warnings,
86
+ })
87
+ }
88
+
89
+ /** The body's file references narrowed to stored-file references; core checks each one. */
90
+ function contentInput(body: {
91
+ readonly kind: ArtifactContentInput["kind"]
92
+ readonly title: string
93
+ readonly files: readonly { readonly path: string; readonly file: unknown }[]
94
+ }): ArtifactContentInput {
95
+ return {
96
+ kind: body.kind,
97
+ title: body.title,
98
+ files: body.files.map(({ path, file }) => {
99
+ if (!isFileRef(file)) {
100
+ throw new ArtifactRequestError(
101
+ "invalid_artifact",
102
+ `[SixbServer] '${path}' does not have a valid file reference.`
103
+ )
104
+ }
105
+ return { path, file }
106
+ }),
107
+ }
108
+ }
109
+
110
+ function versionParam(value: string): number | "latest" {
111
+ return value === "latest" ? "latest" : Number(value)
112
+ }
113
+
114
+ function notFound(set: RouteSet) {
115
+ set.status = 404
116
+ return { error: "Artifact not found", code: "artifact_not_found" as const }
117
+ }
118
+
119
+ /**
120
+ * Why a read found nothing: 404 when there is no such artifact, 403 `not_shared` when there is one
121
+ * the caller cannot read, so a page can say it is not shared in this conversation.
122
+ */
123
+ async function refusal(context: unknown, artifactId: string, set: RouteSet) {
124
+ return refusalFor(await requireRequestSixb(context).artifacts.access(artifactId), set)
125
+ }
126
+
127
+ /** The refusal for what the caller may do with an artifact, once that is known. */
128
+ function refusalFor(access: ArtifactAccess | null, set: RouteSet) {
129
+ if (access !== "none") return notFound(set)
130
+
131
+ set.status = 403
132
+ return {
133
+ error: "Artifact not shared in a conversation you read",
134
+ code: "not_shared" as const,
135
+ }
136
+ }
137
+
138
+ function handleArtifactRouteError(error: unknown, set: RouteSet) {
139
+ if (error instanceof ArtifactRequestError) {
140
+ switch (error.code) {
141
+ case "artifact_not_found":
142
+ case "query_not_found":
143
+ set.status = 404
144
+ break
145
+ case "artifact_kind_mismatch":
146
+ case "artifact_moved":
147
+ case "query_invalid":
148
+ set.status = 409
149
+ break
150
+ case "invalid_artifact":
151
+ case "invalid_state":
152
+ set.status = 400
153
+ break
154
+ case "state_full":
155
+ set.status = 413
156
+ break
157
+ case "not_allowed":
158
+ case "not_shared":
159
+ case "read_only":
160
+ set.status = 403
161
+ break
162
+ case "storage_unavailable":
163
+ set.status = 501
164
+ break
165
+ }
166
+ return {
167
+ error: error.message,
168
+ code: error.code,
169
+ ...(error.latest ? { latest: error.latest } : {}),
170
+ }
171
+ }
172
+ if (error instanceof ZodError) {
173
+ set.status = 400
174
+ return { error: "Invalid artifact request" }
175
+ }
176
+ return handleRouteError(error, set)
177
+ }
178
+
179
+ /** The artifact error body, for a route that declares its responses in OpenAPI. */
180
+ function artifactErrorResponse(status: number) {
181
+ return {
182
+ description: `Response for status ${status}`,
183
+ content: {
184
+ "application/json": { schema: { $ref: "#/components/schemas/ArtifactErrorResponse" } },
185
+ },
186
+ }
187
+ }
188
+
189
+ const publishResponses = {
190
+ 201: PublishedArtifactResponseSchema,
191
+ 400: ArtifactErrorResponseSchema,
192
+ 403: ArtifactErrorResponseSchema,
193
+ 404: ArtifactErrorResponseSchema,
194
+ 409: ArtifactErrorResponseSchema,
195
+ 501: ArtifactErrorResponseSchema,
196
+ }
197
+
198
+ async function artifactFileContentResponse(
199
+ host: SixbHostView,
200
+ context: {
201
+ readonly params: { readonly artifactId: string; readonly version: string }
202
+ readonly query: unknown
203
+ readonly request: Request
204
+ readonly set: RouteSet
205
+ },
206
+ options: { readonly head?: boolean } = {}
207
+ ) {
208
+ try {
209
+ const query = FileContentQuerySchema.parse(context.query)
210
+ const version = await requireRequestSixb(context).artifacts.getVersion(
211
+ context.params.artifactId,
212
+ versionParam(context.params.version)
213
+ )
214
+ if (!version) return await refusal(context, context.params.artifactId, context.set)
215
+
216
+ const file = version.files.find((candidate) => candidate.path === query.path)
217
+ const response =
218
+ file &&
219
+ (await createFileContentResponse({
220
+ blobStorage: host.blobStorage,
221
+ fileRef: file.file,
222
+ disposition: query.disposition,
223
+ head: options.head,
224
+ request: context.request,
225
+ }))
226
+ if (!response) {
227
+ context.set.status = 404
228
+ return { error: "File not found" }
229
+ }
230
+ return response
231
+ } catch (error) {
232
+ if (error instanceof ZodError) {
233
+ context.set.status = 400
234
+ return { error: "Invalid file content query" }
235
+ }
236
+ return handleArtifactRouteError(error, context.set)
237
+ }
238
+ }
239
+
240
+ export function registerArtifactRoutes(app: Elysia, host: SixbHostView) {
241
+ return app
242
+ .get(
243
+ "/api/artifacts",
244
+ async (context) => {
245
+ try {
246
+ const query = ArtifactListQuerySchema.parse(context.query)
247
+ const limit = parseOptionalInt(query.limit)
248
+ const offset = parseOptionalInt(query.offset)
249
+ const result = await requireRequestSixb(context).artifacts.list({
250
+ ...(query.thread === undefined ? {} : { threadId: query.thread }),
251
+ ...(query.kind === undefined ? {} : { kind: query.kind }),
252
+ ...(limit === undefined ? {} : { limit }),
253
+ ...(offset === undefined ? {} : { offset }),
254
+ })
255
+ return ArtifactListResponseSchema.parse({
256
+ artifacts: result.artifacts.map(artifactBody),
257
+ hasMore: result.hasMore,
258
+ total: result.total,
259
+ })
260
+ } catch (error) {
261
+ return handleArtifactRouteError(error, context.set)
262
+ }
263
+ },
264
+ {
265
+ query: ArtifactListQuerySchema,
266
+ response: {
267
+ 200: ArtifactListResponseSchema,
268
+ 400: ArtifactErrorResponseSchema,
269
+ 501: ArtifactErrorResponseSchema,
270
+ },
271
+ detail: {
272
+ summary: "List artifacts",
273
+ description:
274
+ "The caller's artifacts, most recently updated first. With thread, every artifact the conversation shares, for whoever reads it.",
275
+ tags: [OPENAPI_TAGS.artifacts.name],
276
+ operationId: "listArtifacts",
277
+ security: accessTokenSecurityRequirement("listArtifacts"),
278
+ },
279
+ }
280
+ )
281
+ .post(
282
+ "/api/artifacts",
283
+ async (context) => {
284
+ try {
285
+ const body = PublishArtifactBodySchema.parse(context.body)
286
+ const published = await requireRequestSixb(context).artifacts.create(contentInput(body))
287
+ context.set.status = 201
288
+ return serializePublished(published)
289
+ } catch (error) {
290
+ return handleArtifactRouteError(error, context.set)
291
+ }
292
+ },
293
+ {
294
+ body: PublishArtifactBodySchema,
295
+ response: publishResponses,
296
+ detail: {
297
+ summary: "Create an artifact",
298
+ description:
299
+ "Creates an artifact owned by the caller, with its first version. Store each file with POST /api/files first.",
300
+ tags: [OPENAPI_TAGS.artifacts.name],
301
+ operationId: "createArtifact",
302
+ security: accessTokenSecurityRequirement("createArtifact"),
303
+ },
304
+ }
305
+ )
306
+ .get(
307
+ "/api/artifacts/:artifactId",
308
+ async (context) => {
309
+ try {
310
+ const { artifactId } = context.params
311
+ // What the caller may do is decided once; the reads after it need no second decision.
312
+ const access = await requireRequestSixb(context).artifacts.access(artifactId)
313
+ const storage = host.storage.artifacts
314
+ if (!storage || access === null || access === "none") {
315
+ return refusalFor(access, context.set)
316
+ }
317
+
318
+ const at = { projectId: host.id, id: artifactId }
319
+ const [artifact, versions] = await Promise.all([
320
+ storage.get(at),
321
+ storage.listVersions({ projectId: host.id, artifactId }),
322
+ ])
323
+ // Deleted since the decision.
324
+ if (!artifact) return notFound(context.set)
325
+
326
+ return ArtifactResponseSchema.parse({
327
+ artifact: artifactBody(artifact),
328
+ versions: versions.versions.map(versionBody),
329
+ access,
330
+ })
331
+ } catch (error) {
332
+ return handleArtifactRouteError(error, context.set)
333
+ }
334
+ },
335
+ {
336
+ params: ArtifactParamsSchema,
337
+ response: {
338
+ 200: ArtifactResponseSchema,
339
+ 403: ArtifactErrorResponseSchema,
340
+ 404: ArtifactErrorResponseSchema,
341
+ 501: ArtifactErrorResponseSchema,
342
+ },
343
+ detail: {
344
+ summary: "Get an artifact and its versions",
345
+ tags: [OPENAPI_TAGS.artifacts.name],
346
+ operationId: "getArtifact",
347
+ security: accessTokenSecurityRequirement("getArtifact"),
348
+ },
349
+ }
350
+ )
351
+ .delete(
352
+ "/api/artifacts/:artifactId",
353
+ async (context) => {
354
+ try {
355
+ const artifacts = requireRequestSixb(context).artifacts
356
+ const { artifactId } = context.params
357
+ if (await artifacts.delete(artifactId)) {
358
+ return DeleteArtifactResponseSchema.parse({ deleted: true })
359
+ }
360
+
361
+ // Only its owner deletes it, from every conversation that shares it.
362
+ const access = await artifacts.access(artifactId)
363
+ if (access === "update" || access === "read") {
364
+ context.set.status = 403
365
+ return {
366
+ error: "Only the artifact's owner can delete it",
367
+ code: "not_allowed" as const,
368
+ }
369
+ }
370
+ return await refusal(context, artifactId, context.set)
371
+ } catch (error) {
372
+ return handleArtifactRouteError(error, context.set)
373
+ }
374
+ },
375
+ {
376
+ params: ArtifactParamsSchema,
377
+ response: {
378
+ 200: DeleteArtifactResponseSchema,
379
+ 403: ArtifactErrorResponseSchema,
380
+ 404: ArtifactErrorResponseSchema,
381
+ 501: ArtifactErrorResponseSchema,
382
+ },
383
+ detail: {
384
+ summary: "Delete an artifact",
385
+ description:
386
+ "Deletes the artifact with every version, from every conversation that shares it. Only its owner can.",
387
+ tags: [OPENAPI_TAGS.artifacts.name],
388
+ operationId: "deleteArtifact",
389
+ security: accessTokenSecurityRequirement("deleteArtifact"),
390
+ },
391
+ }
392
+ )
393
+ .get(
394
+ "/api/artifacts/:artifactId/state",
395
+ async (context) => {
396
+ try {
397
+ const entries = await requireRequestSixb(context)
398
+ .artifacts.state(context.params.artifactId)
399
+ .entries()
400
+ return ArtifactStateResponseSchema.parse({
401
+ entries: entries.map((entry) => ({
402
+ ...entry,
403
+ updatedAt: toIsoString(entry.updatedAt),
404
+ })),
405
+ })
406
+ } catch (error) {
407
+ return handleArtifactRouteError(error, context.set)
408
+ }
409
+ },
410
+ {
411
+ params: ArtifactParamsSchema,
412
+ response: {
413
+ 200: ArtifactStateResponseSchema,
414
+ 403: ArtifactErrorResponseSchema,
415
+ 404: ArtifactErrorResponseSchema,
416
+ 501: ArtifactErrorResponseSchema,
417
+ },
418
+ detail: {
419
+ summary: "Read what a page keeps",
420
+ description:
421
+ "Every value the artifact keeps for the caller as its reader, as a page keeps it with sixb.state, across its versions.",
422
+ tags: [OPENAPI_TAGS.artifacts.name],
423
+ operationId: "getArtifactState",
424
+ security: accessTokenSecurityRequirement("getArtifactState"),
425
+ },
426
+ }
427
+ )
428
+ .put(
429
+ "/api/artifacts/:artifactId/state/:key",
430
+ async (context) => {
431
+ try {
432
+ const body = SetArtifactStateBodySchema.parse(context.body)
433
+ const entry = await requireRequestSixb(context)
434
+ .artifacts.state(context.params.artifactId)
435
+ .set(context.params.key, body.value)
436
+ return SetArtifactStateResponseSchema.parse({
437
+ ...entry,
438
+ updatedAt: toIsoString(entry.updatedAt),
439
+ })
440
+ } catch (error) {
441
+ return handleArtifactRouteError(error, context.set)
442
+ }
443
+ },
444
+ {
445
+ params: ArtifactStateParamsSchema,
446
+ body: SetArtifactStateBodySchema,
447
+ response: {
448
+ 200: SetArtifactStateResponseSchema,
449
+ 400: ArtifactErrorResponseSchema,
450
+ 403: ArtifactErrorResponseSchema,
451
+ 404: ArtifactErrorResponseSchema,
452
+ 413: ArtifactErrorResponseSchema,
453
+ 501: ArtifactErrorResponseSchema,
454
+ },
455
+ detail: {
456
+ summary: "Keep a value for a page",
457
+ description:
458
+ "Keeps a JSON value under the key for the caller, replacing one there. Refused with 413 state_full past the limits.",
459
+ tags: [OPENAPI_TAGS.artifacts.name],
460
+ operationId: "setArtifactState",
461
+ security: accessTokenSecurityRequirement("setArtifactState"),
462
+ },
463
+ }
464
+ )
465
+ .delete(
466
+ "/api/artifacts/:artifactId/state/:key",
467
+ async (context) => {
468
+ try {
469
+ const deleted = await requireRequestSixb(context)
470
+ .artifacts.state(context.params.artifactId)
471
+ .delete(context.params.key)
472
+ return DeleteArtifactStateResponseSchema.parse({ deleted })
473
+ } catch (error) {
474
+ return handleArtifactRouteError(error, context.set)
475
+ }
476
+ },
477
+ {
478
+ params: ArtifactStateParamsSchema,
479
+ response: {
480
+ 200: DeleteArtifactStateResponseSchema,
481
+ 400: ArtifactErrorResponseSchema,
482
+ 403: ArtifactErrorResponseSchema,
483
+ 404: ArtifactErrorResponseSchema,
484
+ 501: ArtifactErrorResponseSchema,
485
+ },
486
+ detail: {
487
+ summary: "Delete a value a page keeps",
488
+ tags: [OPENAPI_TAGS.artifacts.name],
489
+ operationId: "deleteArtifactStateValue",
490
+ security: accessTokenSecurityRequirement("deleteArtifactStateValue"),
491
+ },
492
+ }
493
+ )
494
+ .delete(
495
+ "/api/artifacts/:artifactId/state",
496
+ async (context) => {
497
+ try {
498
+ const cleared = await requireRequestSixb(context)
499
+ .artifacts.state(context.params.artifactId)
500
+ .clear()
501
+ return ClearArtifactStateResponseSchema.parse({ cleared })
502
+ } catch (error) {
503
+ return handleArtifactRouteError(error, context.set)
504
+ }
505
+ },
506
+ {
507
+ params: ArtifactParamsSchema,
508
+ response: {
509
+ 200: ClearArtifactStateResponseSchema,
510
+ 403: ArtifactErrorResponseSchema,
511
+ 404: ArtifactErrorResponseSchema,
512
+ 501: ArtifactErrorResponseSchema,
513
+ },
514
+ detail: {
515
+ summary: "Reset what a page keeps",
516
+ description: "Deletes every value the artifact keeps for the caller.",
517
+ tags: [OPENAPI_TAGS.artifacts.name],
518
+ operationId: "clearArtifactState",
519
+ security: accessTokenSecurityRequirement("clearArtifactState"),
520
+ },
521
+ }
522
+ )
523
+ .post(
524
+ "/api/artifacts/:artifactId/versions",
525
+ async (context) => {
526
+ try {
527
+ const body = PublishArtifactVersionBodySchema.parse(context.body)
528
+ const published = await requireRequestSixb(context).artifacts.publish(
529
+ context.params.artifactId,
530
+ {
531
+ ...contentInput(body),
532
+ ...(body.baseVersion === undefined ? {} : { baseVersion: body.baseVersion }),
533
+ }
534
+ )
535
+ context.set.status = 201
536
+ return serializePublished(published)
537
+ } catch (error) {
538
+ return handleArtifactRouteError(error, context.set)
539
+ }
540
+ },
541
+ {
542
+ params: ArtifactParamsSchema,
543
+ body: PublishArtifactVersionBodySchema,
544
+ response: publishResponses,
545
+ detail: {
546
+ summary: "Publish an artifact's next version",
547
+ description:
548
+ "The kind must be the artifact's. The new version becomes the latest. With baseVersion, it is refused with 409 artifact_moved when another version was published since.",
549
+ tags: [OPENAPI_TAGS.artifacts.name],
550
+ operationId: "publishArtifactVersion",
551
+ security: accessTokenSecurityRequirement("publishArtifactVersion"),
552
+ },
553
+ }
554
+ )
555
+ .post(
556
+ "/api/artifacts/:artifactId/versions/:version/frame",
557
+ async (context) => {
558
+ try {
559
+ const storage = host.storage.artifacts
560
+ // Reading the version as the caller is the check: whoever may read it may frame it, with
561
+ // the files it embeds from artifacts the caller may read too.
562
+ const artifacts = requireRequestSixb(context).artifacts
563
+ const version = await artifacts.getVersion(
564
+ context.params.artifactId,
565
+ versionParam(context.params.version)
566
+ )
567
+ if (!storage) return notFound(context.set)
568
+ if (!version) return await refusal(context, context.params.artifactId, context.set)
569
+ // Any kind but a deck is refused as `artifact_kind_mismatch`.
570
+ const frame = await createArtifactFrame({
571
+ projectId: host.id,
572
+ storage,
573
+ version,
574
+ read: (id, number) => artifacts.getVersion(id, number),
575
+ })
576
+ return ArtifactFrameResponseSchema.parse({
577
+ path: frame.path,
578
+ expiresAt: frame.expiresAt.toISOString(),
579
+ })
580
+ } catch (error) {
581
+ return handleArtifactRouteError(error, context.set)
582
+ }
583
+ },
584
+ {
585
+ params: ArtifactVersionParamsSchema,
586
+ response: {
587
+ 200: ArtifactFrameResponseSchema,
588
+ 403: ArtifactErrorResponseSchema,
589
+ 404: ArtifactErrorResponseSchema,
590
+ 409: ArtifactErrorResponseSchema,
591
+ 501: ArtifactErrorResponseSchema,
592
+ },
593
+ detail: {
594
+ summary: "Open a version in a frame",
595
+ description:
596
+ "A short-lived path that serves the files of a version that opens in a sandboxed frame, as slides do. It expires within an hour. A kind that does not open in a frame is refused with 409 artifact_kind_mismatch.",
597
+ tags: [OPENAPI_TAGS.artifacts.name],
598
+ operationId: "createArtifactFrame",
599
+ security: accessTokenSecurityRequirement("createArtifactFrame"),
600
+ },
601
+ }
602
+ )
603
+ .get(
604
+ "/api/artifacts/:artifactId/versions/:version",
605
+ async (context) => {
606
+ try {
607
+ const version = await requireRequestSixb(context).artifacts.getVersion(
608
+ context.params.artifactId,
609
+ versionParam(context.params.version)
610
+ )
611
+ if (!version) return await refusal(context, context.params.artifactId, context.set)
612
+ return ArtifactVersionSchema.parse(versionBody(version))
613
+ } catch (error) {
614
+ return handleArtifactRouteError(error, context.set)
615
+ }
616
+ },
617
+ {
618
+ params: ArtifactVersionParamsSchema,
619
+ response: {
620
+ 200: ArtifactVersionSchema,
621
+ 403: ArtifactErrorResponseSchema,
622
+ 404: ArtifactErrorResponseSchema,
623
+ 501: ArtifactErrorResponseSchema,
624
+ },
625
+ detail: {
626
+ summary: "Get an artifact version",
627
+ description: 'Its files, outline, and references. Use "latest" for the newest version.',
628
+ tags: [OPENAPI_TAGS.artifacts.name],
629
+ operationId: "getArtifactVersion",
630
+ security: accessTokenSecurityRequirement("getArtifactVersion"),
631
+ },
632
+ }
633
+ )
634
+ .get(
635
+ "/api/artifacts/:artifactId/versions/:version/queries/:name",
636
+ async (context) => {
637
+ try {
638
+ const { artifactId, version, name } = context.params
639
+ const result = await requireRequestSixb(context).artifacts.query(artifactId, name, {
640
+ version: versionParam(version),
641
+ })
642
+ if (!result) return await refusal(context, artifactId, context.set)
643
+
644
+ // Typed by the ArtifactQueryResponse component, as the query route types its rows.
645
+ return serializeArtifactQueryResult(result)
646
+ } catch (error) {
647
+ // What the query itself runs into answers as the objects API answers it.
648
+ return error instanceof ArtifactRequestError || error instanceof ZodError
649
+ ? handleArtifactRouteError(error, context.set)
650
+ : handleObjectQueryError(error, context.set)
651
+ }
652
+ },
653
+ {
654
+ params: ArtifactQueryParamsSchema,
655
+ detail: {
656
+ summary: "Run an artifact's query",
657
+ description:
658
+ 'Runs the version\'s queries/<name>.json as the caller, with their rights: a type they cannot view is refused with 403, and a marked property they are not cleared for is left out of each row. Rows are serialized as POST /api/objects/query returns them, beside the schema of what they hold as the caller may view it. Use "latest" for the newest version. A query the ontology no longer admits is refused with 409 query_invalid.',
659
+ tags: [OPENAPI_TAGS.artifacts.name],
660
+ operationId: "getArtifactQuery",
661
+ security: accessTokenSecurityRequirement("getArtifactQuery"),
662
+ // Declared in OpenAPI, not zod, as the query route declares its own: the rows take the
663
+ // ObjectQueryObject component, so they are typed as that route's rows.
664
+ responses: {
665
+ 200: {
666
+ description: "Response for status 200",
667
+ content: {
668
+ "application/json": {
669
+ schema: { $ref: "#/components/schemas/ArtifactQueryResponse" },
670
+ },
671
+ },
672
+ },
673
+ 400: {
674
+ description: "Response for status 400",
675
+ content: {
676
+ "application/json": { schema: { $ref: "#/components/schemas/ErrorResponse" } },
677
+ },
678
+ },
679
+ 403: artifactErrorResponse(403),
680
+ 404: artifactErrorResponse(404),
681
+ 409: artifactErrorResponse(409),
682
+ 501: artifactErrorResponse(501),
683
+ },
684
+ },
685
+ }
686
+ )
687
+ .get(
688
+ "/api/artifacts/:artifactId/versions/:version/content",
689
+ async (context) => artifactFileContentResponse(host, context),
690
+ {
691
+ params: ArtifactVersionParamsSchema,
692
+ query: FileContentQuerySchema,
693
+ detail: {
694
+ summary: "Get a file of an artifact version",
695
+ tags: [OPENAPI_TAGS.artifacts.name],
696
+ operationId: "getArtifactFileContent",
697
+ security: accessTokenSecurityRequirement("getArtifactFileContent"),
698
+ responses: fileContentGetResponses({ optionalStorage: true }),
699
+ },
700
+ }
701
+ )
702
+ .head(
703
+ "/api/artifacts/:artifactId/versions/:version/content",
704
+ async (context) => artifactFileContentResponse(host, context, { head: true }),
705
+ {
706
+ params: ArtifactVersionParamsSchema,
707
+ query: FileContentQuerySchema,
708
+ detail: {
709
+ summary: "Head a file of an artifact version",
710
+ tags: [OPENAPI_TAGS.artifacts.name],
711
+ operationId: "headArtifactFileContent",
712
+ security: accessTokenSecurityRequirement("headArtifactFileContent"),
713
+ responses: fileContentHeadResponses({ optionalStorage: true }),
714
+ },
715
+ }
716
+ )
717
+ }