@capybara-hq/kernel 0.1.0-rc.106 → 0.1.0-rc.108

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 (190) hide show
  1. package/bootstrap.json +13 -13
  2. package/dist/admin/admin.schemas.d.ts +3 -0
  3. package/dist/catalog/marketplaces.service.d.ts +1 -1
  4. package/dist/channels/channels.module.js +4 -3
  5. package/dist/channels/file-delivery.d.ts +26 -11
  6. package/dist/channels/file-delivery.js +32 -11
  7. package/dist/channels/outbound.d.ts +3 -3
  8. package/dist/channels/outbound.js +15 -13
  9. package/dist/config/schema.d.ts +3 -0
  10. package/dist/config/schema.js +12 -0
  11. package/dist/conversations/artifacts.d.ts +20 -17
  12. package/dist/conversations/artifacts.js +30 -37
  13. package/dist/conversations/attachments.controller.d.ts +6 -19
  14. package/dist/conversations/attachments.controller.js +8 -59
  15. package/dist/conversations/attachments.service.d.ts +30 -74
  16. package/dist/conversations/attachments.service.js +51 -193
  17. package/dist/conversations/conversations.module.js +4 -0
  18. package/dist/conversations/conversations.service.d.ts +10 -9
  19. package/dist/conversations/conversations.service.js +28 -11
  20. package/dist/conversations/event-log.d.ts +1 -0
  21. package/dist/conversations/events.d.ts +49 -27
  22. package/dist/conversations/events.js +54 -7
  23. package/dist/conversations/files.d.ts +66 -64
  24. package/dist/conversations/files.js +127 -147
  25. package/dist/conversations/prompt-suggestion-request.js +2 -1
  26. package/dist/conversations/prompt-suggestions.d.ts +3 -3
  27. package/dist/conversations/prompt-suggestions.js +11 -10
  28. package/dist/conversations/queue.service.js +7 -2
  29. package/dist/ctx/create-ctx.d.ts +3 -1
  30. package/dist/ctx/create-ctx.js +19 -1
  31. package/dist/db/kernel-schema.d.ts +80 -2
  32. package/dist/errors.d.ts +19 -0
  33. package/dist/errors.js +19 -0
  34. package/dist/evals/canonical-json.d.ts +7 -0
  35. package/dist/evals/canonical-json.js +21 -0
  36. package/dist/evals/reconstruct.js +13 -1
  37. package/dist/evals/recorded-tools.d.ts +0 -2
  38. package/dist/evals/recorded-tools.js +1 -16
  39. package/dist/evals/snapshot.d.ts +26 -3
  40. package/dist/evals/snapshot.js +110 -57
  41. package/dist/evals/worker.d.ts +4 -3
  42. package/dist/evals/worker.js +8 -4
  43. package/dist/extensions/host.d.ts +3 -1
  44. package/dist/extensions/host.js +2 -0
  45. package/dist/extensions/instance.d.ts +3 -1
  46. package/dist/extensions/instance.js +4 -0
  47. package/dist/extensions/ledger.d.ts +1 -1
  48. package/dist/files/blob-objects.d.ts +124 -0
  49. package/dist/files/blob-objects.js +341 -0
  50. package/dist/files/blob-sweep.job.d.ts +122 -0
  51. package/dist/files/blob-sweep.job.js +389 -0
  52. package/dist/files/blobs.service.d.ts +135 -0
  53. package/dist/files/blobs.service.js +300 -0
  54. package/dist/files/conversation-media.d.ts +41 -0
  55. package/dist/files/conversation-media.js +124 -0
  56. package/dist/files/deliveries.d.ts +64 -0
  57. package/dist/files/deliveries.js +156 -0
  58. package/dist/files/deliveries.module.d.ts +6 -0
  59. package/dist/files/deliveries.module.js +24 -0
  60. package/dist/files/draft-cleanup.job.d.ts +26 -0
  61. package/dist/files/draft-cleanup.job.js +93 -0
  62. package/dist/files/files.module.d.ts +1 -1
  63. package/dist/files/files.module.js +3 -2
  64. package/dist/files/hash-job.d.ts +127 -0
  65. package/dist/files/hash-job.js +447 -0
  66. package/dist/files/library-backfill.d.ts +63 -0
  67. package/dist/files/library-backfill.js +143 -0
  68. package/dist/files/library-files.service.d.ts +255 -0
  69. package/dist/files/library-files.service.js +883 -0
  70. package/dist/files/library-jobs.module.d.ts +8 -0
  71. package/dist/files/library-jobs.module.js +27 -0
  72. package/dist/files/library-query.d.ts +127 -0
  73. package/dist/files/library-query.js +322 -0
  74. package/dist/files/library-read.module.d.ts +9 -0
  75. package/dist/files/library-read.module.js +30 -0
  76. package/dist/files/library.controller.d.ts +77 -0
  77. package/dist/files/library.controller.js +235 -0
  78. package/dist/files/library.module.d.ts +9 -0
  79. package/dist/files/library.module.js +29 -0
  80. package/dist/files/library.schemas.d.ts +46 -0
  81. package/dist/files/library.schemas.js +49 -0
  82. package/dist/files/previewers.controller.d.ts +20 -0
  83. package/dist/files/previewers.controller.js +47 -0
  84. package/dist/files/previewers.d.ts +25 -11
  85. package/dist/files/previewers.js +43 -27
  86. package/dist/files/referenced-files.d.ts +150 -0
  87. package/dist/files/referenced-files.js +234 -0
  88. package/dist/html/html-document.controller.d.ts +8 -1
  89. package/dist/html/html-document.controller.js +19 -1
  90. package/dist/html/html-document.service.d.ts +19 -4
  91. package/dist/html/html-document.service.js +43 -22
  92. package/dist/html/html-token.d.ts +10 -1
  93. package/dist/html/html-token.js +14 -0
  94. package/dist/html/html.module.js +8 -1
  95. package/dist/http/api.module.js +5 -0
  96. package/dist/identity/settings.d.ts +26 -0
  97. package/dist/identity/settings.js +33 -2
  98. package/dist/infra/outbox/registry.d.ts +82 -0
  99. package/dist/infra/outbox/registry.js +74 -2
  100. package/dist/infra/throttler/named-throttlers.d.ts +2 -0
  101. package/dist/infra/throttler/named-throttlers.js +14 -0
  102. package/dist/lifecycle/kernel-lifecycle.js +6 -0
  103. package/dist/models/purposes.js +1 -1
  104. package/dist/orgs/offboarding.js +1 -1
  105. package/dist/orgs/settings.service.d.ts +48 -2
  106. package/dist/orgs/settings.service.js +63 -3
  107. package/dist/platform/exclusions.js +45 -8
  108. package/dist/platform/file-ref.d.ts +18 -3
  109. package/dist/platform/file-ref.js +22 -11
  110. package/dist/platform/operation.js +4 -0
  111. package/dist/platform/ops/admin.ops.js +5 -2
  112. package/dist/platform/ops/files.ops.d.ts +18 -0
  113. package/dist/platform/ops/files.ops.js +267 -0
  114. package/dist/platform/ops/org-settings.ops.js +6 -2
  115. package/dist/platform/ops/orgs.ops.js +1 -0
  116. package/dist/platform/ops/shares.ops.js +2 -2
  117. package/dist/platform/platform-ops.module.d.ts +2 -1
  118. package/dist/platform/platform-ops.module.js +5 -0
  119. package/dist/platform/route-schemas.js +3 -0
  120. package/dist/roles/worker.module.js +15 -1
  121. package/dist/runtime/compaction-settings.service.d.ts +13 -1
  122. package/dist/runtime/compaction-settings.service.js +16 -1
  123. package/dist/runtime/compaction.service.d.ts +3 -1
  124. package/dist/runtime/compaction.service.js +3 -1
  125. package/dist/runtime/context/build.d.ts +18 -1
  126. package/dist/runtime/context/build.js +19 -8
  127. package/dist/runtime/context/cache-expiry.d.ts +31 -0
  128. package/dist/runtime/context/cache-expiry.js +60 -0
  129. package/dist/runtime/context/cache-ttl.d.ts +18 -0
  130. package/dist/runtime/context/cache-ttl.js +30 -0
  131. package/dist/runtime/context/compaction.d.ts +9 -2
  132. package/dist/runtime/context/compaction.js +50 -17
  133. package/dist/runtime/context/in-context-compaction.d.ts +156 -0
  134. package/dist/runtime/context/in-context-compaction.js +310 -0
  135. package/dist/runtime/hook-calls.d.ts +2 -1
  136. package/dist/runtime/hook-calls.js +5 -2
  137. package/dist/runtime/maintenance.d.ts +10 -2
  138. package/dist/runtime/maintenance.js +48 -8
  139. package/dist/runtime/model-call.d.ts +34 -5
  140. package/dist/runtime/model-call.js +207 -22
  141. package/dist/runtime/providers.js +11 -1
  142. package/dist/runtime/run-steps.d.ts +4 -2
  143. package/dist/runtime/run-steps.js +10 -5
  144. package/dist/runtime/runtime.module.js +3 -0
  145. package/dist/shares/liveness.d.ts +26 -12
  146. package/dist/shares/liveness.js +79 -20
  147. package/dist/shares/revoke.d.ts +16 -11
  148. package/dist/shares/revoke.js +18 -14
  149. package/dist/shares/share-objects.d.ts +16 -21
  150. package/dist/shares/share-objects.js +25 -55
  151. package/dist/shares/share-open.service.d.ts +8 -4
  152. package/dist/shares/share-open.service.js +14 -11
  153. package/dist/shares/shares.controller.d.ts +16 -5
  154. package/dist/shares/shares.controller.js +39 -5
  155. package/dist/shares/shares.module.d.ts +4 -4
  156. package/dist/shares/shares.module.js +7 -6
  157. package/dist/shares/shares.schemas.d.ts +4 -0
  158. package/dist/shares/shares.schemas.js +2 -0
  159. package/dist/shares/shares.service.d.ts +93 -54
  160. package/dist/shares/shares.service.js +292 -131
  161. package/dist/shares/sweep.job.d.ts +18 -39
  162. package/dist/shares/sweep.job.js +47 -206
  163. package/dist/tools/builtins/artifacts.d.ts +21 -13
  164. package/dist/tools/builtins/artifacts.js +81 -51
  165. package/dist/tools/builtins/builtin-tools.d.ts +7 -1
  166. package/dist/tools/builtins/builtin-tools.js +17 -6
  167. package/dist/tools/builtins/conversation-files.d.ts +23 -13
  168. package/dist/tools/builtins/conversation-files.js +62 -83
  169. package/dist/tools/builtins/image/generate.d.ts +7 -1
  170. package/dist/tools/builtins/image/generate.js +1 -1
  171. package/dist/tools/builtins/platform.js +1 -0
  172. package/dist/tools/builtins/sandbox/common.d.ts +6 -2
  173. package/dist/tools/builtins/sandbox/deliver.js +6 -7
  174. package/dist/tools/builtins/sandbox/fetch-attachment.d.ts +6 -5
  175. package/dist/tools/builtins/sandbox/fetch-attachment.js +19 -12
  176. package/dist/tools/tools.module.js +6 -0
  177. package/dist/ui/call-extension.d.ts +6 -4
  178. package/dist/ui/call-extension.js +5 -4
  179. package/dist/ui/embed-token.js +6 -3
  180. package/dist/ui/registry.d.ts +21 -5
  181. package/dist/ui/registry.js +24 -6
  182. package/dist/ui/ui.schemas.d.ts +22 -0
  183. package/dist/ui/ui.schemas.js +22 -4
  184. package/dist/ui/views.controller.d.ts +13 -3
  185. package/dist/ui/views.controller.js +33 -2
  186. package/dist/ui/views.d.ts +29 -1
  187. package/dist/ui/views.js +131 -7
  188. package/package.json +3 -3
  189. package/src/migrations/0190_file_library.sql +285 -0
  190. package/src/migrations/0191_file_library_copy.sql +4 -0
@@ -0,0 +1,235 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Controller, Delete, Get, Header, HttpCode, Inject, Param, Post, Res, } from "@nestjs/common";
11
+ import { z } from "zod";
12
+ import { ARTIFACT_DOWNLOAD_CACHE_SECONDS } from "../conversations/artifacts.js";
13
+ import { DOWNLOAD_AS } from "../conversations/conversations.schemas.js";
14
+ import { FILE_CONTENT_MAX_BYTES } from "../conversations/files.js";
15
+ import { CurrentPrincipal, requireUser } from "../identity/principal.js";
16
+ import { Throttled } from "../infra/throttler/throttler.guard.js";
17
+ import { createZodDto } from "../infra/validation/create-zod-dto.js";
18
+ import { ZBody, ZParam, ZQuery } from "../infra/validation/zod-params.js";
19
+ import { CurrentOrg, OrgAction } from "../orgs/org.guard.js";
20
+ import { FileCreateBody, FileIdParams, LibraryListQuery } from "./library.schemas.js";
21
+ import { LibraryFilesService, } from "./library-files.service.js";
22
+ import { LibraryQuery, } from "./library-query.js";
23
+ const ContentQuery = z.object({
24
+ limit: z.coerce.number().int().min(1).max(FILE_CONTENT_MAX_BYTES).default(FILE_CONTENT_MAX_BYTES),
25
+ });
26
+ /**
27
+ * `inline=1`: open inline (bitmaps only); `as=url`: answer `{ url }` instead of redirecting. Not
28
+ * strict: the web adds a cache-busting `retry=1` when it reloads a failed image.
29
+ */
30
+ const DownloadQuery = z.object({
31
+ inline: z.enum(["0", "1"]).optional(),
32
+ as: DOWNLOAD_AS.optional(),
33
+ });
34
+ class FileCreateBodyDto extends createZodDto(FileCreateBody, "FileCreateBody") {
35
+ }
36
+ class LibraryListQueryDto extends createZodDto(LibraryListQuery, "LibraryListQuery") {
37
+ }
38
+ class FileIdParamsDto extends createZodDto(FileIdParams, "FileIdParams") {
39
+ }
40
+ class LibraryFileContentQueryDto extends createZodDto(ContentQuery, "LibraryFileContentQuery") {
41
+ }
42
+ class LibraryFileDownloadQueryDto extends createZodDto(DownloadQuery, "LibraryFileDownloadQuery") {
43
+ }
44
+ /**
45
+ * The person's files (`/api/v1/orgs/:orgId/files`, `agents.use`, people only; file library spec
46
+ * §3):
47
+ * 1. `POST /` with `{ name, contentType, size, sha256, purpose }` → 201 `{ file, upload,
48
+ * expiresAt }`: `upload` is a presigned form (`method` `POST`: the `fields` first, the `file`
49
+ * last), or null when the caller already holds these bytes (the file exists at once);
50
+ * 2. the browser sends the bytes to the store;
51
+ * 3. `POST /:id/finalize` → `{ id, name, contentType, size, status }`;
52
+ * 4. a message names the id in `files`.
53
+ * Finalize is limited to 60 a minute per person (429 `rate_limited`). `DELETE /:id/draft` (204)
54
+ * drops a composer upload before it is sent; `GET /limits` answers the types, sizes, files per
55
+ * message, the media budget per request and whether storage is available (no object store
56
+ * needed). Without an object store the upload routes answer 503 `storage.unavailable`.
57
+ *
58
+ * The library (file library spec §6, §8; the caller's own files only: another account's file is
59
+ * 404 `file.not_found` on every route; `Cache-Control: private, no-store`):
60
+ * - `GET /?origin=&agentId=&type=&q=&limit=&cursor=` → `{ files, next }`: ready files newest first,
61
+ * the deliveries of one source path in one conversation collapsed to the newest with `versions`;
62
+ * `q` a case-insensitive substring of the name (the composer's `@` search);
63
+ * - `GET /usage` → `{ bytes, files }`; `GET /:id` → `{ file, conversations, versions }`;
64
+ * - `GET /:id/content?limit=` and `GET /:id/download?as=url&inline=1`: the conversation-scoped
65
+ * routes' twins (same headers; the download a 302 with `private, max-age=60`);
66
+ * - `DELETE /:id` (204): the file is deleted and its shares revoked.
67
+ */
68
+ let LibraryFilesController = class LibraryFilesController {
69
+ files;
70
+ library;
71
+ constructor(files, library) {
72
+ this.files = files;
73
+ this.library = library;
74
+ }
75
+ limits(principal) {
76
+ requireUser(principal);
77
+ return this.files.limits();
78
+ }
79
+ async create(principal, org, body) {
80
+ const p = requireUser(principal);
81
+ return this.files.create(org.orgId, p.account.id, body);
82
+ }
83
+ async finalize(principal, org, id) {
84
+ const p = requireUser(principal);
85
+ return this.files.finalize(org.orgId, p.account.id, id);
86
+ }
87
+ async discard(principal, org, id) {
88
+ const p = requireUser(principal);
89
+ await this.files.discard(org.orgId, p.account.id, id);
90
+ }
91
+ async list(principal, org, q) {
92
+ const p = requireUser(principal);
93
+ return this.library.list(org.orgId, p.account.id, {
94
+ limit: q.limit,
95
+ ...(q.origin !== undefined && { origin: q.origin }),
96
+ ...(q.agentId !== undefined && { agentId: q.agentId }),
97
+ ...(q.type !== undefined && { type: q.type }),
98
+ ...(q.q !== undefined && { q: q.q }),
99
+ ...(q.cursor !== undefined && { cursor: q.cursor }),
100
+ });
101
+ }
102
+ async usage(principal, org) {
103
+ const p = requireUser(principal);
104
+ return this.library.usage(org.orgId, p.account.id);
105
+ }
106
+ async details(principal, org, params) {
107
+ const p = requireUser(principal);
108
+ return this.library.details(org.orgId, p.account.id, params.id);
109
+ }
110
+ /**
111
+ * The first `limit` bytes (≤ 1 MiB) of a text file, always as `text/plain; charset=utf-8` with
112
+ * the stored type in `X-Content-Original-Type` (sandboxed, never sniffed);
113
+ * `X-Content-Truncated: true` when cut. Not text → 400 `conversation.artifact_not_previewable`;
114
+ * bytes gone → 410 `file.deleted`.
115
+ */
116
+ async content(principal, org, params, q, reply) {
117
+ const p = requireUser(principal);
118
+ const c = await this.library.content(org.orgId, p.account.id, params.id, q.limit);
119
+ reply
120
+ .header("content-type", c.contentType)
121
+ .header("x-content-original-type", c.originalType)
122
+ .header("cache-control", "private, no-store")
123
+ .header("x-content-type-options", "nosniff")
124
+ .header("content-security-policy", "sandbox; default-src 'none'");
125
+ if (c.truncated)
126
+ reply.header("x-content-truncated", "true");
127
+ await reply.send(c.body);
128
+ }
129
+ /**
130
+ * A 302 to the presigned address of the file's bytes (`attachment` with its name; `inline=1`
131
+ * only for bitmaps), `Cache-Control: private, max-age=60`; `as=url` answers `{ url }` instead
132
+ * (never cached).
133
+ */
134
+ async download(principal, org, params, q, reply) {
135
+ const p = requireUser(principal);
136
+ const { url } = await this.library.downloadUrl(org.orgId, p.account.id, params.id, {
137
+ inline: q.inline === "1",
138
+ });
139
+ if (q.as === "url") {
140
+ await reply.header("cache-control", "private, no-store").send({ url });
141
+ return;
142
+ }
143
+ await reply
144
+ .header("cache-control", `private, max-age=${ARTIFACT_DOWNLOAD_CACHE_SECONDS}`)
145
+ .redirect(url, 302);
146
+ }
147
+ async remove(principal, org, params) {
148
+ const p = requireUser(principal);
149
+ await this.files.delete(org.orgId, p.account.id, params.id);
150
+ }
151
+ };
152
+ __decorate([
153
+ Get("limits"),
154
+ OrgAction("agents.use"),
155
+ __param(0, CurrentPrincipal())
156
+ ], LibraryFilesController.prototype, "limits", null);
157
+ __decorate([
158
+ Post(),
159
+ OrgAction("agents.use"),
160
+ __param(0, CurrentPrincipal()),
161
+ __param(1, CurrentOrg()),
162
+ __param(2, ZBody(FileCreateBodyDto))
163
+ ], LibraryFilesController.prototype, "create", null);
164
+ __decorate([
165
+ Post(":id/finalize"),
166
+ HttpCode(200),
167
+ OrgAction("agents.use"),
168
+ Throttled("file-finalize"),
169
+ __param(0, CurrentPrincipal()),
170
+ __param(1, CurrentOrg()),
171
+ __param(2, Param("id"))
172
+ ], LibraryFilesController.prototype, "finalize", null);
173
+ __decorate([
174
+ Delete(":id/draft"),
175
+ HttpCode(204),
176
+ OrgAction("agents.use"),
177
+ __param(0, CurrentPrincipal()),
178
+ __param(1, CurrentOrg()),
179
+ __param(2, Param("id"))
180
+ ], LibraryFilesController.prototype, "discard", null);
181
+ __decorate([
182
+ Get(),
183
+ Header("cache-control", "private, no-store"),
184
+ OrgAction("agents.use"),
185
+ __param(0, CurrentPrincipal()),
186
+ __param(1, CurrentOrg()),
187
+ __param(2, ZQuery(LibraryListQueryDto))
188
+ ], LibraryFilesController.prototype, "list", null);
189
+ __decorate([
190
+ Get("usage"),
191
+ Header("cache-control", "private, no-store"),
192
+ OrgAction("agents.use"),
193
+ __param(0, CurrentPrincipal()),
194
+ __param(1, CurrentOrg())
195
+ ], LibraryFilesController.prototype, "usage", null);
196
+ __decorate([
197
+ Get(":id"),
198
+ Header("cache-control", "private, no-store"),
199
+ OrgAction("agents.use"),
200
+ __param(0, CurrentPrincipal()),
201
+ __param(1, CurrentOrg()),
202
+ __param(2, ZParam(FileIdParamsDto))
203
+ ], LibraryFilesController.prototype, "details", null);
204
+ __decorate([
205
+ Get(":id/content"),
206
+ OrgAction("agents.use"),
207
+ __param(0, CurrentPrincipal()),
208
+ __param(1, CurrentOrg()),
209
+ __param(2, ZParam(FileIdParamsDto)),
210
+ __param(3, ZQuery(LibraryFileContentQueryDto)),
211
+ __param(4, Res())
212
+ ], LibraryFilesController.prototype, "content", null);
213
+ __decorate([
214
+ Get(":id/download"),
215
+ OrgAction("agents.use"),
216
+ __param(0, CurrentPrincipal()),
217
+ __param(1, CurrentOrg()),
218
+ __param(2, ZParam(FileIdParamsDto)),
219
+ __param(3, ZQuery(LibraryFileDownloadQueryDto)),
220
+ __param(4, Res())
221
+ ], LibraryFilesController.prototype, "download", null);
222
+ __decorate([
223
+ Delete(":id"),
224
+ HttpCode(204),
225
+ OrgAction("agents.use"),
226
+ __param(0, CurrentPrincipal()),
227
+ __param(1, CurrentOrg()),
228
+ __param(2, ZParam(FileIdParamsDto))
229
+ ], LibraryFilesController.prototype, "remove", null);
230
+ LibraryFilesController = __decorate([
231
+ Controller("api/v1/orgs/:orgId/files"),
232
+ __param(0, Inject(LibraryFilesService)),
233
+ __param(1, Inject(LibraryQuery))
234
+ ], LibraryFilesController);
235
+ export { LibraryFilesController };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The file library (file library spec): blobs ({@link BlobsService}) and the person's files
3
+ * ({@link LibraryFilesService}). The hourly cleanup of expired uploads and drafts
4
+ * (`FileDraftCleanupJob`) and the owed object deletions (`BlobObjects`, through the maintenance
5
+ * queue) are the worker role's. Its own module (no conversations dependency) so conversations and
6
+ * tools can use it.
7
+ */
8
+ export declare class LibraryFilesModule {
9
+ }
@@ -0,0 +1,29 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Module } from "@nestjs/common";
8
+ import { EventLogModule } from "../conversations/event-log.js";
9
+ import { StorageModule } from "../storage/storage.module.js";
10
+ import { BlobObjectsModule } from "./blob-objects.js";
11
+ import { BlobHashLock, BlobsService } from "./blobs.service.js";
12
+ import { LibraryFilesService } from "./library-files.service.js";
13
+ /**
14
+ * The file library (file library spec): blobs ({@link BlobsService}) and the person's files
15
+ * ({@link LibraryFilesService}). The hourly cleanup of expired uploads and drafts
16
+ * (`FileDraftCleanupJob`) and the owed object deletions (`BlobObjects`, through the maintenance
17
+ * queue) are the worker role's. Its own module (no conversations dependency) so conversations and
18
+ * tools can use it.
19
+ */
20
+ let LibraryFilesModule = class LibraryFilesModule {
21
+ };
22
+ LibraryFilesModule = __decorate([
23
+ Module({
24
+ imports: [StorageModule, BlobObjectsModule, EventLogModule],
25
+ providers: [BlobHashLock, BlobsService, LibraryFilesService],
26
+ exports: [BlobHashLock, BlobsService, LibraryFilesService],
27
+ })
28
+ ], LibraryFilesModule);
29
+ export { LibraryFilesModule };
@@ -0,0 +1,46 @@
1
+ import { z } from "zod";
2
+ import type { RouteSchemas } from "../platform/route-schemas.js";
3
+ /** `POST /files`: the file to upload and the sha256 the web computed over its bytes. */
4
+ export declare const FileCreateBody: z.ZodObject<{
5
+ name: z.ZodString;
6
+ contentType: z.ZodString;
7
+ size: z.ZodNumber;
8
+ sha256: z.ZodString;
9
+ purpose: z.ZodEnum<{
10
+ composer: "composer";
11
+ library: "library";
12
+ }>;
13
+ }, z.core.$strict>;
14
+ /** The origins of a library file, as `GET /files?origin=` filters them. */
15
+ export declare const LIBRARY_ORIGINS: readonly ["upload", "delivered", "generated"];
16
+ /** The kinds `GET /files?type=` filters by (the content type's family). */
17
+ export declare const LIBRARY_TYPES: readonly ["image", "document", "text", "other"];
18
+ /** The most files one page of `GET /files` returns. */
19
+ export declare const LIBRARY_PAGE_MAX = 100;
20
+ /**
21
+ * `GET /files`: the caller's ready files, newest first. `q`: a case-insensitive substring of the
22
+ * name (the composer's `@` search); `cursor`: the `next` of the previous page.
23
+ */
24
+ export declare const LibraryListQuery: z.ZodObject<{
25
+ origin: z.ZodOptional<z.ZodEnum<{
26
+ delivered: "delivered";
27
+ generated: "generated";
28
+ upload: "upload";
29
+ }>>;
30
+ agentId: z.ZodOptional<z.ZodString>;
31
+ type: z.ZodOptional<z.ZodEnum<{
32
+ document: "document";
33
+ image: "image";
34
+ other: "other";
35
+ text: "text";
36
+ }>>;
37
+ q: z.ZodOptional<z.ZodString>;
38
+ limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
39
+ cursor: z.ZodOptional<z.ZodString>;
40
+ }, z.core.$strip>;
41
+ /** `…/files/:id` routes. */
42
+ export declare const FileIdParams: z.ZodObject<{
43
+ id: z.ZodString;
44
+ }, z.core.$strip>;
45
+ /** The library routes a Platform operation mirrors (file library spec §6). */
46
+ export declare const FILE_ROUTES: readonly RouteSchemas[];
@@ -0,0 +1,49 @@
1
+ import { hasControlChars, MEDIA_NAME_MAX_CHARS } from "@capybara-hq/plugin-sdk";
2
+ import { z } from "zod";
3
+ /** A hex sha256 (lower case), as the web computes it before uploading. */
4
+ const Sha256 = z.string().regex(/^[0-9a-f]{64}$/, "sha256 must be 64 lower-case hex digits");
5
+ /** Where the upload is made: `composer` (a draft until sent) or `library` (ready at once). */
6
+ const Purpose = z.enum(["composer", "library"]);
7
+ /** `POST /files`: the file to upload and the sha256 the web computed over its bytes. */
8
+ export const FileCreateBody = z
9
+ .object({
10
+ name: z
11
+ .string()
12
+ .min(1)
13
+ .max(MEDIA_NAME_MAX_CHARS)
14
+ .refine((s) => !hasControlChars(s), "name must not contain control characters"),
15
+ contentType: z.string().min(1).max(200),
16
+ size: z.number().int().min(1).max(Number.MAX_SAFE_INTEGER),
17
+ sha256: Sha256,
18
+ purpose: Purpose,
19
+ })
20
+ .strict();
21
+ /** The origins of a library file, as `GET /files?origin=` filters them. */
22
+ export const LIBRARY_ORIGINS = ["upload", "delivered", "generated"];
23
+ /** The kinds `GET /files?type=` filters by (the content type's family). */
24
+ export const LIBRARY_TYPES = ["image", "document", "text", "other"];
25
+ /** The most files one page of `GET /files` returns. */
26
+ export const LIBRARY_PAGE_MAX = 100;
27
+ /**
28
+ * `GET /files`: the caller's ready files, newest first. `q`: a case-insensitive substring of the
29
+ * name (the composer's `@` search); `cursor`: the `next` of the previous page.
30
+ */
31
+ export const LibraryListQuery = z.object({
32
+ origin: z.enum(LIBRARY_ORIGINS).optional(),
33
+ agentId: z.string().min(1).max(100).optional(),
34
+ type: z.enum(LIBRARY_TYPES).optional(),
35
+ q: z.string().min(1).max(200).optional(),
36
+ limit: z.coerce.number().int().min(1).max(LIBRARY_PAGE_MAX).default(50),
37
+ cursor: z.string().min(1).max(200).optional(),
38
+ });
39
+ /** `…/files/:id` routes. */
40
+ export const FileIdParams = z.object({ id: z.string().min(1).max(100) });
41
+ /** The library routes a Platform operation mirrors (file library spec §6). */
42
+ export const FILE_ROUTES = [
43
+ { route: "GET /api/v1/orgs/:orgId/files", query: LibraryListQuery },
44
+ { route: "POST /api/v1/orgs/:orgId/files", body: FileCreateBody },
45
+ { route: "POST /api/v1/orgs/:orgId/files/:id/finalize", params: FileIdParams },
46
+ { route: "GET /api/v1/orgs/:orgId/files/usage" },
47
+ { route: "GET /api/v1/orgs/:orgId/files/:id", params: FileIdParams },
48
+ { route: "DELETE /api/v1/orgs/:orgId/files/:id", params: FileIdParams },
49
+ ];
@@ -21,6 +21,16 @@ declare const PreviewParams: z.ZodObject<{
21
21
  plugin: z.ZodString;
22
22
  previewerId: z.ZodString;
23
23
  }, z.core.$strip>;
24
+ /** `…/files/:id/previewers` (a library file). */
25
+ declare const LibraryFileParams: z.ZodObject<{
26
+ id: z.ZodString;
27
+ }, z.core.$strip>;
28
+ /** `…/files/:id/previews/:plugin/:previewerId` (a library file). */
29
+ declare const LibraryFilePreviewParams: z.ZodObject<{
30
+ plugin: z.ZodString;
31
+ previewerId: z.ZodString;
32
+ id: z.ZodString;
33
+ }, z.core.$strip>;
24
34
  /**
25
35
  * `file-preview@1` (UI refresh B Task 11):
26
36
  * - `GET /api/v1/orgs/:orgId/conversations/:id/files/:fileId/previewers` → `{ previewers }`: the
@@ -30,6 +40,10 @@ declare const PreviewParams: z.ZodObject<{
30
40
  * render (10 s → 504 `ui.timeout`, throws → 502 `ui.render_failed`, invalid → 502
31
41
  * `ui.invalid_view`); an embed previewer gets its token from the ui embed-token route with
32
42
  * `{ conversationId, fileId }` and the file over the message bridge;
43
+ * - `GET /api/v1/orgs/:orgId/files/:id/previewers` and `GET .../files/:id/previews/:plugin/:previewerId`
44
+ * (file library spec §6): the same for one of the caller's own library files (anyone else's:
45
+ * 404 `file.not_found`); args `source: "conversation"` without a `conversationId`; an embed
46
+ * previewer's token takes `{ fileId }` alone;
33
47
  * - `GET /api/v1/orgs/:orgId/skills/:id/files/previewers?version=&path=` and
34
48
  * `GET .../skills/:id/files/previews/:plugin/:previewerId?version=&path=&locale=` (`skills.read`,
35
49
  * skill file browser spec): the same for one file of a skill version, visibility as
@@ -47,6 +61,12 @@ export declare class FilePreviewersController {
47
61
  preview(principal: Principal, org: OrgContext, id: string, fileId: string, { plugin, previewerId }: z.infer<typeof PreviewParams>, q: z.infer<typeof PreviewQuery>): Promise<{
48
62
  view: import("@capybara-hq/plugin-sdk").UiView;
49
63
  }>;
64
+ forLibraryFile(principal: Principal, org: OrgContext, params: z.infer<typeof LibraryFileParams>): Promise<{
65
+ previewers: PreviewerItem[];
66
+ }>;
67
+ previewLibraryFile(principal: Principal, org: OrgContext, params: z.infer<typeof LibraryFilePreviewParams>, q: z.infer<typeof PreviewQuery>): Promise<{
68
+ view: import("@capybara-hq/plugin-sdk").UiView;
69
+ }>;
50
70
  forSkillFile(principal: Principal, org: OrgContext, params: z.infer<typeof SkillIdParams>, q: z.infer<typeof SkillFilePreviewersQuery>): Promise<{
51
71
  previewers: PreviewerItem[];
52
72
  }>;
@@ -21,6 +21,14 @@ const PreviewQuery = z.object({ locale: z.enum(["zh", "en"]).default("en") });
21
21
  const OrderBody = FilePreviewersOrderBody;
22
22
  /** The previewer named in a preview path (the other path parameters are read as they are). */
23
23
  const PreviewParams = z.object({ plugin: Id, previewerId: Id });
24
+ /** `…/files/:id/previewers` (a library file). */
25
+ const LibraryFileParams = z.object({ id: Id });
26
+ /** `…/files/:id/previews/:plugin/:previewerId` (a library file). */
27
+ const LibraryFilePreviewParams = LibraryFileParams.extend(PreviewParams.shape);
28
+ class LibraryFilePreviewersParamsDto extends createZodDto(LibraryFileParams, "LibraryFilePreviewersParams") {
29
+ }
30
+ class LibraryFilePreviewParamsDto extends createZodDto(LibraryFilePreviewParams, "LibraryFilePreviewParams") {
31
+ }
24
32
  class FilePreviewQueryDto extends createZodDto(PreviewQuery, "FilePreviewQuery") {
25
33
  }
26
34
  class FilePreviewParamsDto extends createZodDto(PreviewParams, "FilePreviewParams") {
@@ -44,6 +52,10 @@ class SkillFilePreviewQueryDto extends createZodDto(SkillFilePreviewQuery, "Skil
44
52
  * render (10 s → 504 `ui.timeout`, throws → 502 `ui.render_failed`, invalid → 502
45
53
  * `ui.invalid_view`); an embed previewer gets its token from the ui embed-token route with
46
54
  * `{ conversationId, fileId }` and the file over the message bridge;
55
+ * - `GET /api/v1/orgs/:orgId/files/:id/previewers` and `GET .../files/:id/previews/:plugin/:previewerId`
56
+ * (file library spec §6): the same for one of the caller's own library files (anyone else's:
57
+ * 404 `file.not_found`); args `source: "conversation"` without a `conversationId`; an embed
58
+ * previewer's token takes `{ fileId }` alone;
47
59
  * - `GET /api/v1/orgs/:orgId/skills/:id/files/previewers?version=&path=` and
48
60
  * `GET .../skills/:id/files/previews/:plugin/:previewerId?version=&path=&locale=` (`skills.read`,
49
61
  * skill file browser spec): the same for one file of a skill version, visibility as
@@ -74,6 +86,24 @@ let FilePreviewersController = class FilePreviewersController {
74
86
  });
75
87
  return { view };
76
88
  }
89
+ async forLibraryFile(principal, org, params) {
90
+ const p = requireUser(principal);
91
+ return {
92
+ previewers: await this.previewers.forFile(org.orgId, undefined, p.account.id, params.id),
93
+ };
94
+ }
95
+ async previewLibraryFile(principal, org, params, q) {
96
+ const p = requireUser(principal);
97
+ const view = await this.previewers.render({
98
+ orgId: org.orgId,
99
+ accountId: p.account.id,
100
+ fileId: params.id,
101
+ plugin: params.plugin,
102
+ id: params.previewerId,
103
+ locale: q.locale,
104
+ });
105
+ return { view };
106
+ }
77
107
  async forSkillFile(principal, org, params, q) {
78
108
  requireUser(principal);
79
109
  return {
@@ -127,6 +157,23 @@ __decorate([
127
157
  __param(4, ZParam(FilePreviewParamsDto)),
128
158
  __param(5, ZQuery(FilePreviewQueryDto))
129
159
  ], FilePreviewersController.prototype, "preview", null);
160
+ __decorate([
161
+ Get("files/:id/previewers"),
162
+ Header("cache-control", "private, no-store"),
163
+ OrgAction("agents.use"),
164
+ __param(0, CurrentPrincipal()),
165
+ __param(1, CurrentOrg()),
166
+ __param(2, ZParam(LibraryFilePreviewersParamsDto))
167
+ ], FilePreviewersController.prototype, "forLibraryFile", null);
168
+ __decorate([
169
+ Get("files/:id/previews/:plugin/:previewerId"),
170
+ Header("cache-control", "private, no-store"),
171
+ OrgAction("agents.use"),
172
+ __param(0, CurrentPrincipal()),
173
+ __param(1, CurrentOrg()),
174
+ __param(2, ZParam(LibraryFilePreviewParamsDto)),
175
+ __param(3, ZQuery(FilePreviewQueryDto))
176
+ ], FilePreviewersController.prototype, "previewLibraryFile", null);
130
177
  __decorate([
131
178
  Get("skills/:id/files/previewers"),
132
179
  Header("cache-control", "private, no-store"),
@@ -6,6 +6,8 @@ import { KernelDb } from "../infra/database/kernel-db.js";
6
6
  import { TransactionHost } from "../infra/database/transaction-host.js";
7
7
  import { SkillFiles } from "../skills/skill-files.js";
8
8
  import { StorageService } from "../storage/storage.service.js";
9
+ import { type BlobRef, BlobsService } from "./blobs.service.js";
10
+ import { LibraryQuery } from "./library-query.js";
9
11
  /** Time limit of a previewer's render (UI refresh spec §5.3: 10 s on the conversation page). */
10
12
  export declare const FILE_PREVIEW_TIMEOUT_MS = 10000;
11
13
  /** A previewer that takes a file, as listed for the conversation page. */
@@ -37,8 +39,8 @@ type PreviewFile = {
37
39
  export interface ShareSnapshot extends PreviewFile {
38
40
  id: string;
39
41
  orgId: string;
40
- /** Relative to the org's kernel prefix. */
41
- objectKey: string;
42
+ /** The share's blob, read through `BlobsService` (size and etag checked). */
43
+ blob: BlobRef;
42
44
  }
43
45
  /** Optional provider: a view previewer's render time limit (ms) instead of {@link FILE_PREVIEW_TIMEOUT_MS} (tests). */
44
46
  export declare const FILE_PREVIEW_TIMEOUT: unique symbol;
@@ -55,7 +57,7 @@ export declare class PreviewerRegistry {
55
57
  readonly txHost: TransactionHost;
56
58
  private readonly timeoutMs;
57
59
  private readonly d;
58
- constructor(kdb: Pick<KernelDb, "db">, host: Pick<ExtensionHost, "list" | "enabledPlugins">, files: Pick<ConversationFiles, "file">, skillFiles: Pick<SkillFiles, "resolve" | "bytes">, storage: Pick<StorageService, "kernel">, effective: Pick<EffectiveConfigs, "get">, log: Pick<Logger, "warn">, txHost: TransactionHost, timeoutMs?: number);
60
+ constructor(kdb: Pick<KernelDb, "db">, host: Pick<ExtensionHost, "list" | "enabledPlugins">, files: Pick<ConversationFiles, "file">, library: Pick<LibraryQuery, "live">, blobs: Pick<BlobsService, "open">, skillFiles: Pick<SkillFiles, "resolve" | "bytes">, storage: Pick<StorageService, "kernel">, effective: Pick<EffectiveConfigs, "get">, log: Pick<Logger, "warn">, txHost: TransactionHost, timeoutMs?: number);
59
61
  /** Every previewer loaded on this replica, in provider order (load order, then list order). */
60
62
  private loaded;
61
63
  /** The org's previewers (enabled extensions only) in its order, disabled ones included. */
@@ -79,8 +81,17 @@ export declare class PreviewerRegistry {
79
81
  }, auditDetail?: Record<string, unknown>): Promise<OrgPreviewerItem[]>;
80
82
  /** Replaces the org's rows of the previewers this replica knows, in one transaction. */
81
83
  private putIn;
82
- /** Previewers that take this file (viewer must read the conversation), in the org's order. */
83
- forFile(orgId: string, conversationId: string, viewer: string, fileId: string): Promise<PreviewerItem[]>;
84
+ /**
85
+ * The file a conversation or library previewer reads: with `conversationId`, a file of that
86
+ * conversation (the viewer must read it; `ConversationFiles.file`); without, one of the viewer's
87
+ * own library files (`LibraryQuery.live`: 404 `file.not_found` for anyone else's).
88
+ */
89
+ private fileFor;
90
+ /**
91
+ * Previewers that take this file, in the org's order: a file of the conversation (the viewer
92
+ * must read it), or without `conversationId` one of the viewer's library files.
93
+ */
94
+ forFile(orgId: string, conversationId: string | undefined, viewer: string, fileId: string): Promise<PreviewerItem[]>;
84
95
  /**
85
96
  * The org's enabled previewers that take the file, each with its extension's effective config
86
97
  * for the org. An extension whose effective config is not valid in the org (or cannot be read)
@@ -89,13 +100,15 @@ export declare class PreviewerRegistry {
89
100
  */
90
101
  private accepting;
91
102
  /**
92
- * Renders a view previewer for one file: 404 `ui.not_found` when it does not take the file (or is
93
- * disabled, not enabled, an embed); the render's errors as for plugin views (504 / 502).
103
+ * Renders a view previewer for one file (of a conversation, or without `conversationId` of the
104
+ * viewer's library; args `source: "conversation"` then carry no `conversationId`): 404
105
+ * `ui.not_found` when it does not take the file (or is disabled, not enabled, an embed); the
106
+ * render's errors as for plugin views (504 / 502).
94
107
  */
95
108
  render(t: {
96
109
  orgId: string;
97
110
  accountId: string;
98
- conversationId: string;
111
+ conversationId?: string;
99
112
  fileId: string;
100
113
  plugin: string;
101
114
  id: string;
@@ -174,13 +187,14 @@ export declare class PreviewerRegistry {
174
187
  private renderWith;
175
188
  /**
176
189
  * Whether an embed token for a `file.preview` contribution may be issued for this file: the
177
- * viewer reads the conversation, the file is in it, and an enabled previewer of that extension
178
- * with `embed: contribution` takes it. Conversation / file not found throws 404.
190
+ * viewer reads the conversation and the file is in it (without `conversationId`: the file is
191
+ * one of the viewer's own library files), and an enabled previewer of that extension with
192
+ * `embed: contribution` takes it. Conversation / file not found throws 404.
179
193
  */
180
194
  embedAllowed(t: {
181
195
  orgId: string;
182
196
  accountId: string;
183
- conversationId: string;
197
+ conversationId?: string;
184
198
  fileId: string;
185
199
  plugin: string;
186
200
  contribution: string;