@capybara-hq/kernel 0.1.0-rc.105 → 0.1.0-rc.107

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 (359) hide show
  1. package/dist/admin/admin.schemas.d.ts +3 -0
  2. package/dist/agents/agents.schemas.d.ts +9 -9
  3. package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
  4. package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
  5. package/dist/automations/automations.module.d.ts +16 -0
  6. package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
  7. package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
  8. package/dist/automations/automations.schemas.js +253 -0
  9. package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
  10. package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
  11. package/dist/{schedules → automations}/calendar.d.ts +8 -8
  12. package/dist/{schedules → automations}/calendar.js +2 -2
  13. package/dist/automations/effective-settings.d.ts +28 -0
  14. package/dist/automations/effective-settings.js +25 -0
  15. package/dist/automations/errors.d.ts +19 -0
  16. package/dist/automations/errors.js +19 -0
  17. package/dist/automations/events/coalesce.d.ts +28 -0
  18. package/dist/automations/events/coalesce.js +131 -0
  19. package/dist/automations/events/deadline.d.ts +10 -0
  20. package/dist/automations/events/deadline.js +24 -0
  21. package/dist/automations/events/dispatcher.d.ts +74 -0
  22. package/dist/automations/events/dispatcher.js +353 -0
  23. package/dist/automations/events/events.module.d.ts +7 -0
  24. package/dist/automations/events/events.module.js +26 -0
  25. package/dist/automations/events/guards.d.ts +32 -0
  26. package/dist/automations/events/guards.js +87 -0
  27. package/dist/automations/events/sweep.job.d.ts +49 -0
  28. package/dist/automations/events/sweep.job.js +202 -0
  29. package/dist/automations/events/token.d.ts +5 -0
  30. package/dist/automations/events/token.js +5 -0
  31. package/dist/automations/events/trigger-events.service.d.ts +47 -0
  32. package/dist/automations/events/trigger-events.service.js +254 -0
  33. package/dist/{schedules → automations}/fire.d.ts +57 -17
  34. package/dist/{schedules → automations}/fire.js +311 -79
  35. package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
  36. package/dist/{schedules → automations}/grant-rules.js +5 -5
  37. package/dist/automations/job-ids.d.ts +113 -0
  38. package/dist/automations/job-ids.js +117 -0
  39. package/dist/{schedules → automations}/pause.d.ts +3 -3
  40. package/dist/{schedules → automations}/pause.js +3 -3
  41. package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
  42. package/dist/{schedules → automations}/run-tracker.js +68 -56
  43. package/dist/{schedules → automations}/scan.job.d.ts +12 -8
  44. package/dist/{schedules → automations}/scan.job.js +39 -23
  45. package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
  46. package/dist/{schedules → automations}/schedule-math.js +17 -7
  47. package/dist/{schedules → automations}/store.d.ts +57 -27
  48. package/dist/{schedules → automations}/store.js +102 -52
  49. package/dist/automations/store.module.d.ts +6 -0
  50. package/dist/{schedules → automations}/store.module.js +9 -9
  51. package/dist/automations/types.d.ts +178 -0
  52. package/dist/{schedules → automations}/unattended.d.ts +22 -11
  53. package/dist/{schedules → automations}/unattended.js +53 -30
  54. package/dist/catalog/marketplaces.service.d.ts +1 -1
  55. package/dist/channels/channels.module.js +7 -6
  56. package/dist/channels/file-delivery.d.ts +26 -11
  57. package/dist/channels/file-delivery.js +32 -11
  58. package/dist/channels/outbound.d.ts +8 -8
  59. package/dist/channels/outbound.js +32 -30
  60. package/dist/channels/threads.d.ts +1 -1
  61. package/dist/channels/threads.js +1 -1
  62. package/dist/config/schema.d.ts +8 -1
  63. package/dist/config/schema.js +56 -6
  64. package/dist/conversations/approval-profile.d.ts +2 -2
  65. package/dist/conversations/approval-profile.js +3 -3
  66. package/dist/conversations/artifacts.d.ts +20 -17
  67. package/dist/conversations/artifacts.js +30 -37
  68. package/dist/conversations/attachments.controller.d.ts +6 -19
  69. package/dist/conversations/attachments.controller.js +8 -59
  70. package/dist/conversations/attachments.service.d.ts +30 -74
  71. package/dist/conversations/attachments.service.js +51 -193
  72. package/dist/conversations/conversations.controller.d.ts +7 -1
  73. package/dist/conversations/conversations.controller.js +26 -10
  74. package/dist/conversations/conversations.module.js +4 -0
  75. package/dist/conversations/conversations.service.d.ts +112 -35
  76. package/dist/conversations/conversations.service.js +160 -43
  77. package/dist/conversations/event-log.d.ts +1 -0
  78. package/dist/conversations/events.d.ts +95 -27
  79. package/dist/conversations/events.js +108 -10
  80. package/dist/conversations/files.d.ts +66 -64
  81. package/dist/conversations/files.js +127 -147
  82. package/dist/conversations/message-context.d.ts +2 -2
  83. package/dist/conversations/message-context.js +1 -1
  84. package/dist/conversations/permission-profile-store.d.ts +2 -1
  85. package/dist/conversations/permission-profiles.controller.d.ts +3 -0
  86. package/dist/conversations/permission-profiles.service.js +1 -1
  87. package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
  88. package/dist/conversations/prompt-suggestion-request.js +4 -2
  89. package/dist/conversations/prompt-suggestions.d.ts +3 -3
  90. package/dist/conversations/prompt-suggestions.js +11 -10
  91. package/dist/conversations/queue.d.ts +7 -4
  92. package/dist/conversations/queue.js +6 -2
  93. package/dist/conversations/queue.service.d.ts +5 -5
  94. package/dist/conversations/queue.service.js +27 -16
  95. package/dist/ctx/create-ctx.d.ts +23 -2
  96. package/dist/ctx/create-ctx.js +78 -3
  97. package/dist/db/kernel-schema.d.ts +184 -27
  98. package/dist/devices/device-project-files.service.d.ts +88 -0
  99. package/dist/devices/device-project-files.service.js +249 -0
  100. package/dist/devices/device-projects.service.d.ts +40 -0
  101. package/dist/devices/device-projects.service.js +16 -13
  102. package/dist/devices/devices.controller.d.ts +9 -3
  103. package/dist/devices/devices.controller.js +27 -5
  104. package/dist/devices/devices.module.js +9 -1
  105. package/dist/devices/devices.schemas.js +6 -1
  106. package/dist/devices/dto.d.ts +15 -0
  107. package/dist/devices/dto.js +11 -0
  108. package/dist/devices/project-paths.d.ts +29 -0
  109. package/dist/devices/project-paths.js +66 -0
  110. package/dist/devices/project-rules.d.ts +5 -1
  111. package/dist/devices/project-rules.js +5 -1
  112. package/dist/errors.d.ts +53 -13
  113. package/dist/errors.js +53 -13
  114. package/dist/evals/canonical-json.d.ts +7 -0
  115. package/dist/evals/canonical-json.js +21 -0
  116. package/dist/evals/reconstruct.d.ts +10 -1
  117. package/dist/evals/reconstruct.js +20 -1
  118. package/dist/evals/recorded-tools.d.ts +0 -2
  119. package/dist/evals/recorded-tools.js +1 -16
  120. package/dist/evals/snapshot.d.ts +26 -3
  121. package/dist/evals/snapshot.js +110 -57
  122. package/dist/evals/worker.d.ts +4 -3
  123. package/dist/evals/worker.js +8 -4
  124. package/dist/extensions/host.d.ts +9 -2
  125. package/dist/extensions/host.js +12 -0
  126. package/dist/extensions/instance.d.ts +3 -1
  127. package/dist/extensions/instance.js +4 -0
  128. package/dist/extensions/ledger.d.ts +1 -1
  129. package/dist/extensions/permission-set.d.ts +17 -1
  130. package/dist/extensions/permission-set.js +13 -1
  131. package/dist/extensions/permission-view.d.ts +1 -1
  132. package/dist/extensions/permission-view.js +4 -1
  133. package/dist/files/blob-objects.d.ts +124 -0
  134. package/dist/files/blob-objects.js +341 -0
  135. package/dist/files/blob-sweep.job.d.ts +122 -0
  136. package/dist/files/blob-sweep.job.js +389 -0
  137. package/dist/files/blobs.service.d.ts +135 -0
  138. package/dist/files/blobs.service.js +300 -0
  139. package/dist/files/conversation-media.d.ts +41 -0
  140. package/dist/files/conversation-media.js +124 -0
  141. package/dist/files/deliveries.d.ts +64 -0
  142. package/dist/files/deliveries.js +156 -0
  143. package/dist/files/deliveries.module.d.ts +6 -0
  144. package/dist/files/deliveries.module.js +24 -0
  145. package/dist/files/draft-cleanup.job.d.ts +26 -0
  146. package/dist/files/draft-cleanup.job.js +93 -0
  147. package/dist/files/files.module.d.ts +1 -1
  148. package/dist/files/files.module.js +3 -2
  149. package/dist/files/hash-job.d.ts +127 -0
  150. package/dist/files/hash-job.js +447 -0
  151. package/dist/files/library-backfill.d.ts +63 -0
  152. package/dist/files/library-backfill.js +143 -0
  153. package/dist/files/library-files.service.d.ts +255 -0
  154. package/dist/files/library-files.service.js +883 -0
  155. package/dist/files/library-jobs.module.d.ts +8 -0
  156. package/dist/files/library-jobs.module.js +27 -0
  157. package/dist/files/library-query.d.ts +127 -0
  158. package/dist/files/library-query.js +322 -0
  159. package/dist/files/library-read.module.d.ts +9 -0
  160. package/dist/files/library-read.module.js +30 -0
  161. package/dist/files/library.controller.d.ts +77 -0
  162. package/dist/files/library.controller.js +235 -0
  163. package/dist/files/library.module.d.ts +9 -0
  164. package/dist/files/library.module.js +29 -0
  165. package/dist/files/library.schemas.d.ts +46 -0
  166. package/dist/files/library.schemas.js +49 -0
  167. package/dist/files/previewers.controller.d.ts +20 -0
  168. package/dist/files/previewers.controller.js +47 -0
  169. package/dist/files/previewers.d.ts +25 -11
  170. package/dist/files/previewers.js +43 -27
  171. package/dist/files/referenced-files.d.ts +150 -0
  172. package/dist/files/referenced-files.js +234 -0
  173. package/dist/git/gateway.js +2 -2
  174. package/dist/html/html-document.controller.d.ts +8 -1
  175. package/dist/html/html-document.controller.js +19 -1
  176. package/dist/html/html-document.service.d.ts +19 -4
  177. package/dist/html/html-document.service.js +43 -22
  178. package/dist/html/html-token.d.ts +10 -1
  179. package/dist/html/html-token.js +14 -0
  180. package/dist/html/html.module.js +8 -1
  181. package/dist/http/api.module.js +10 -5
  182. package/dist/http/request-signal.d.ts +8 -0
  183. package/dist/http/request-signal.js +14 -0
  184. package/dist/identity/settings.d.ts +26 -0
  185. package/dist/identity/settings.js +33 -2
  186. package/dist/inbox/inbox.module.js +2 -2
  187. package/dist/inbox/inbox.service.d.ts +8 -8
  188. package/dist/inbox/inbox.service.js +7 -7
  189. package/dist/infra/config/kernel-config.service.js +10 -5
  190. package/dist/infra/outbox/registry.d.ts +111 -2
  191. package/dist/infra/outbox/registry.js +104 -9
  192. package/dist/infra/throttler/named-throttlers.d.ts +2 -0
  193. package/dist/infra/throttler/named-throttlers.js +14 -0
  194. package/dist/kernel.module.js +3 -0
  195. package/dist/lifecycle/kernel-lifecycle.js +6 -0
  196. package/dist/models/purposes.js +1 -1
  197. package/dist/orgs/offboarding.js +1 -1
  198. package/dist/orgs/roles.d.ts +1 -1
  199. package/dist/orgs/roles.js +3 -3
  200. package/dist/orgs/settings.controller.d.ts +1 -1
  201. package/dist/orgs/settings.service.d.ts +75 -13
  202. package/dist/orgs/settings.service.js +104 -26
  203. package/dist/platform/exclusions.js +45 -8
  204. package/dist/platform/file-ref.d.ts +18 -3
  205. package/dist/platform/file-ref.js +22 -11
  206. package/dist/platform/operation.js +6 -2
  207. package/dist/platform/ops/admin.ops.js +5 -2
  208. package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
  209. package/dist/platform/ops/automations.ops.js +560 -0
  210. package/dist/platform/ops/devices.ops.d.ts +3 -1
  211. package/dist/platform/ops/devices.ops.js +77 -3
  212. package/dist/platform/ops/files.ops.d.ts +18 -0
  213. package/dist/platform/ops/files.ops.js +267 -0
  214. package/dist/platform/ops/inbox.ops.js +4 -4
  215. package/dist/platform/ops/org-settings.ops.js +6 -2
  216. package/dist/platform/ops/orgs.ops.js +2 -1
  217. package/dist/platform/ops/platform.ops.js +4 -4
  218. package/dist/platform/ops/shares.ops.js +2 -2
  219. package/dist/platform/platform-ops.module.d.ts +3 -2
  220. package/dist/platform/platform-ops.module.js +10 -5
  221. package/dist/platform/route-schemas.js +6 -3
  222. package/dist/push/jobs.d.ts +3 -3
  223. package/dist/push/jobs.js +2 -2
  224. package/dist/push/preferences.service.js +1 -1
  225. package/dist/push/push-owed.d.ts +3 -3
  226. package/dist/push/push-owed.js +6 -6
  227. package/dist/push/push.schemas.d.ts +4 -4
  228. package/dist/push/push.schemas.js +3 -3
  229. package/dist/push/recipients.d.ts +4 -4
  230. package/dist/push/recipients.js +23 -23
  231. package/dist/push/texts.d.ts +3 -3
  232. package/dist/push/texts.js +2 -2
  233. package/dist/push/triggers.d.ts +1 -1
  234. package/dist/roles/worker.module.js +18 -4
  235. package/dist/runtime/compaction-settings.service.d.ts +13 -1
  236. package/dist/runtime/compaction-settings.service.js +16 -1
  237. package/dist/runtime/compaction.service.d.ts +3 -1
  238. package/dist/runtime/compaction.service.js +3 -1
  239. package/dist/runtime/context/build.d.ts +24 -1
  240. package/dist/runtime/context/build.js +33 -11
  241. package/dist/runtime/context/cache-expiry.d.ts +31 -0
  242. package/dist/runtime/context/cache-expiry.js +60 -0
  243. package/dist/runtime/context/cache-ttl.d.ts +18 -0
  244. package/dist/runtime/context/cache-ttl.js +30 -0
  245. package/dist/runtime/context/compaction.d.ts +9 -2
  246. package/dist/runtime/context/compaction.js +64 -21
  247. package/dist/runtime/context/in-context-compaction.d.ts +156 -0
  248. package/dist/runtime/context/in-context-compaction.js +310 -0
  249. package/dist/runtime/context/run-context.d.ts +38 -0
  250. package/dist/runtime/context/run-context.js +54 -0
  251. package/dist/runtime/hook-calls.d.ts +2 -1
  252. package/dist/runtime/hook-calls.js +5 -2
  253. package/dist/runtime/maintenance.d.ts +16 -4
  254. package/dist/runtime/maintenance.js +79 -18
  255. package/dist/runtime/model-call.d.ts +34 -5
  256. package/dist/runtime/model-call.js +207 -22
  257. package/dist/runtime/providers.js +11 -1
  258. package/dist/runtime/run-finish.d.ts +4 -4
  259. package/dist/runtime/run-finish.js +3 -3
  260. package/dist/runtime/run-queue.js +12 -7
  261. package/dist/runtime/run-steps.d.ts +4 -2
  262. package/dist/runtime/run-steps.js +10 -5
  263. package/dist/runtime/run-worker.js +7 -1
  264. package/dist/runtime/runtime.module.js +6 -3
  265. package/dist/runtime/subagents/dispatch.js +11 -3
  266. package/dist/sandbox/browse-roots.d.ts +8 -0
  267. package/dist/sandbox/browse-roots.js +22 -0
  268. package/dist/sandbox/network-grants.d.ts +4 -4
  269. package/dist/sandbox/network-grants.js +7 -7
  270. package/dist/sandbox/operation-authority.d.ts +10 -3
  271. package/dist/sandbox/operation-authority.js +20 -10
  272. package/dist/sandbox/operation-intents.d.ts +3 -1
  273. package/dist/sandbox/operation-intents.js +11 -4
  274. package/dist/sandbox/permission-call-grants.js +2 -2
  275. package/dist/sandbox/permission-facts.js +1 -1
  276. package/dist/sandbox/review/classifier.d.ts +2 -2
  277. package/dist/sandbox/review/classifier.js +22 -11
  278. package/dist/sandbox/review/permission-candidate.d.ts +1 -1
  279. package/dist/sandbox/review/permission-candidate.js +1 -1
  280. package/dist/sandbox/sandbox.service.d.ts +15 -5
  281. package/dist/sandbox/sandbox.service.js +14 -8
  282. package/dist/shares/liveness.d.ts +26 -12
  283. package/dist/shares/liveness.js +79 -20
  284. package/dist/shares/revoke.d.ts +16 -11
  285. package/dist/shares/revoke.js +18 -14
  286. package/dist/shares/share-objects.d.ts +16 -21
  287. package/dist/shares/share-objects.js +25 -55
  288. package/dist/shares/share-open.service.d.ts +8 -4
  289. package/dist/shares/share-open.service.js +14 -11
  290. package/dist/shares/shares.controller.d.ts +16 -5
  291. package/dist/shares/shares.controller.js +39 -5
  292. package/dist/shares/shares.module.d.ts +4 -4
  293. package/dist/shares/shares.module.js +7 -6
  294. package/dist/shares/shares.schemas.d.ts +4 -0
  295. package/dist/shares/shares.schemas.js +2 -0
  296. package/dist/shares/shares.service.d.ts +93 -54
  297. package/dist/shares/shares.service.js +292 -131
  298. package/dist/shares/sweep.job.d.ts +18 -39
  299. package/dist/shares/sweep.job.js +47 -206
  300. package/dist/storage/purge.d.ts +1 -1
  301. package/dist/storage/purge.js +2 -2
  302. package/dist/tools/approvals.service.d.ts +6 -6
  303. package/dist/tools/approvals.service.js +19 -19
  304. package/dist/tools/builtin-names.d.ts +12 -12
  305. package/dist/tools/builtin-names.js +29 -29
  306. package/dist/tools/builtins/artifacts.d.ts +21 -13
  307. package/dist/tools/builtins/artifacts.js +81 -51
  308. package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
  309. package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
  310. package/dist/tools/builtins/builtin-tools.d.ts +15 -9
  311. package/dist/tools/builtins/builtin-tools.js +33 -22
  312. package/dist/tools/builtins/conversation-files.d.ts +23 -13
  313. package/dist/tools/builtins/conversation-files.js +62 -83
  314. package/dist/tools/builtins/image/generate.d.ts +7 -1
  315. package/dist/tools/builtins/image/generate.js +1 -1
  316. package/dist/tools/builtins/platform.js +1 -0
  317. package/dist/tools/builtins/sandbox/common.d.ts +6 -2
  318. package/dist/tools/builtins/sandbox/deliver.js +6 -7
  319. package/dist/tools/builtins/sandbox/fetch-attachment.d.ts +6 -5
  320. package/dist/tools/builtins/sandbox/fetch-attachment.js +19 -12
  321. package/dist/tools/call-scope.d.ts +1 -1
  322. package/dist/tools/git-asks.js +1 -1
  323. package/dist/tools/git-push-policy.d.ts +3 -3
  324. package/dist/tools/git-push-policy.js +6 -6
  325. package/dist/tools/network-asks.d.ts +3 -3
  326. package/dist/tools/network-asks.js +3 -3
  327. package/dist/tools/pipeline.d.ts +4 -4
  328. package/dist/tools/pipeline.js +2 -2
  329. package/dist/tools/policy.d.ts +3 -3
  330. package/dist/tools/policy.js +3 -3
  331. package/dist/tools/tools.module.js +15 -9
  332. package/dist/ui/call-extension.d.ts +6 -4
  333. package/dist/ui/call-extension.js +5 -4
  334. package/dist/ui/embed-token.js +6 -3
  335. package/dist/ui/registry.d.ts +21 -5
  336. package/dist/ui/registry.js +24 -6
  337. package/dist/ui/ui.schemas.d.ts +22 -0
  338. package/dist/ui/ui.schemas.js +22 -4
  339. package/dist/ui/views.controller.d.ts +13 -3
  340. package/dist/ui/views.controller.js +33 -2
  341. package/dist/ui/views.d.ts +29 -1
  342. package/dist/ui/views.js +131 -7
  343. package/package.json +3 -3
  344. package/src/migrations/0188_automations_rename.sql +309 -0
  345. package/src/migrations/0189_event_automations.sql +136 -0
  346. package/src/migrations/0190_file_library.sql +285 -0
  347. package/src/migrations/0191_file_library_copy.sql +4 -0
  348. package/dist/platform/ops/schedules.ops.js +0 -509
  349. package/dist/schedules/effective-settings.d.ts +0 -22
  350. package/dist/schedules/effective-settings.js +0 -18
  351. package/dist/schedules/errors.d.ts +0 -8
  352. package/dist/schedules/errors.js +0 -8
  353. package/dist/schedules/job-ids.d.ts +0 -81
  354. package/dist/schedules/job-ids.js +0 -95
  355. package/dist/schedules/schedules.module.d.ts +0 -16
  356. package/dist/schedules/schedules.schemas.js +0 -213
  357. package/dist/schedules/store.module.d.ts +0 -6
  358. package/dist/schedules/types.d.ts +0 -110
  359. /package/dist/{schedules → automations}/types.js +0 -0
@@ -1,13 +1,14 @@
1
+ import type { BlobRef } from "../files/blobs.service.js";
2
+ import { ReferencedFiles } from "../files/referenced-files.js";
1
3
  import { KernelDb } from "../infra/database/kernel-db.js";
2
- import { StorageService } from "../storage/storage.service.js";
3
4
  import { ConversationAccess } from "./access.js";
4
- import { AttachmentsService } from "./attachments.service.js";
5
5
  /**
6
- * A conversation's files (UI refresh B, spec §5.8 / §5.9): the files the agent delivered
7
- * (`artifact_created`), grouped by `sourcePath` into versions, and the files users sent in it
8
- * (ready `chat` attachments bound to the conversation), newest first. Read on every request (no
9
- * cache); `status` comes from the object store. Both kinds are read under the conversation's
10
- * access: whoever may read the conversation may read the attachments sent in it, nobody else.
6
+ * A conversation's files (UI refresh B, spec §5.8 / §5.9; file library spec §2): the library files
7
+ * the conversation refers to (`conversation_files`). Files an agent delivered or generated are
8
+ * grouped by `source_path` into versions; files a person uploaded are listed one by one; newest
9
+ * first. Read on every request (no cache); `status` comes from the file and its blob (a deleted
10
+ * file, or a blob gone or replaced, is `removed`). Everything is read under the conversation's
11
+ * access: whoever may read the conversation may read the files it refers to, nobody else.
11
12
  */
12
13
  /** Versions listed per file (newest first). */
13
14
  export declare const FILE_VERSIONS_MAX = 20;
@@ -16,7 +17,7 @@ export declare const FILES_PAGE_MAX = 100;
16
17
  /** Largest `limit` (bytes) of the text content route. */
17
18
  export declare const FILE_CONTENT_MAX_BYTES: number;
18
19
  export interface FileOutput {
19
- /** artifactId of the latest version. */
20
+ /** The file id of the latest version (an artifact id for a delivery). */
20
21
  id: string;
21
22
  name: string;
22
23
  contentType: string;
@@ -32,18 +33,18 @@ export interface FileOutput {
32
33
  }[];
33
34
  callId: string;
34
35
  createdAt: Date;
35
- /** `removed`: the latest version's object is gone from storage (decision 8). */
36
+ /** `removed`: the latest version's file was deleted or its bytes are gone (decision 8). */
36
37
  status: "available" | "removed";
37
38
  previewable: "image" | "text" | null;
38
39
  /**
39
- * `delivered`: the agent delivered it (`artifact_created`); `uploaded`: a user sent it with a
40
- * message (an attachment: `id` is the attachment id, one version, `sourcePath` is its name and
41
- * `callId` is empty).
40
+ * `delivered`: an agent delivered or generated it (`artifact_created`); `uploaded`: a person
41
+ * uploaded it and sent it with a message (one version, `sourcePath` is its name and `callId` is
42
+ * empty).
42
43
  */
43
44
  origin: "delivered" | "uploaded";
44
45
  /**
45
46
  * The viewer has a share of this file that is not revoked or expired (file sharing spec §5): of
46
- * its source path for a delivered file (any version), of the attachment for an uploaded one.
47
+ * its source path for a delivered file (any version), of the file for an uploaded one.
47
48
  */
48
49
  shared: boolean;
49
50
  }
@@ -59,6 +60,24 @@ export interface FileContent {
59
60
  originalType: string;
60
61
  truncated: boolean;
61
62
  }
63
+ /** One readable file version of the conversation, for the kernel only (the key never leaves it). */
64
+ export interface ResolvedConversationFile {
65
+ id: string;
66
+ name: string;
67
+ contentType: string;
68
+ size: number;
69
+ /** The blob's key (relative to the org's kernel prefix). */
70
+ objectKey: string;
71
+ /** The version group of a delivered file; null for an upload. */
72
+ sourcePath: string | null;
73
+ /** What a later read of the object must still match (`blobContentMatches`). */
74
+ recorded: {
75
+ size: number;
76
+ etag: string | null;
77
+ };
78
+ /** The blob, for `BlobsService.open` / `head`. */
79
+ blob: BlobRef;
80
+ }
62
81
  /** The built-in preview kind of a content type (the files list's `previewable`; share metadata too). */
63
82
  export declare const previewKind: (contentType: string) => FileOutput["previewable"];
64
83
  /** Drops an incomplete UTF-8 sequence at the end of a cut buffer. */
@@ -69,31 +88,31 @@ export declare function readPrefix(body: ReadableStream<Uint8Array>, max: number
69
88
  export declare const HEAD_CONCURRENCY = 8;
70
89
  export declare class ConversationFiles {
71
90
  private readonly db;
72
- private readonly storage;
73
91
  /** Same visibility as the event stream. */
74
92
  private readonly access;
75
- /** The attachments sent in the conversation, and the rule their stored content must meet. */
76
- private readonly attachments;
77
- constructor(db: KernelDb, storage: Pick<StorageService, "kernel">,
93
+ /** The files the conversation refers to, and their bytes (size and etag checked). */
94
+ private readonly files;
95
+ constructor(db: KernelDb,
78
96
  /** Same visibility as the event stream. */
79
97
  access: Pick<ConversationAccess, "isAccessible">,
80
- /** The attachments sent in the conversation, and the rule their stored content must meet. */
81
- attachments: Pick<AttachmentsService, "boundRows" | "storedMatches">);
98
+ /** The files the conversation refers to, and their bytes (size and etag checked). */
99
+ files: Pick<ReferencedFiles, "references" | "open" | "head">);
82
100
  private checkAccess;
83
- /** Every delivery of the conversation in log order. */
84
- private deliveries;
101
+ /** Whether a reference can be read now: live, and its stored head still the recorded bytes. */
102
+ private available;
85
103
  /**
86
- * One file of the conversation by id: a delivered version first, then an attachment sent in it;
87
- * neither → `conversation.artifact_not_found`. An attachment carries `upload`, the row its stored
88
- * object must still match. Call after {@link checkAccess}.
104
+ * One file the conversation refers to by id (any status: a deleted file still resolves, so its
105
+ * shares are found); none → `conversation.artifact_not_found`. Call after {@link checkAccess}.
89
106
  */
90
107
  private resolve;
108
+ /** The tool call of each delivery logged in the conversation, by artifact (= file) id. */
109
+ private callIds;
91
110
  /**
92
111
  * Files newest first: a delivered file by its **first** delivery, so a new version keeps the
93
112
  * file's place and a cursor stays stable while files are re-delivered (review M2); an uploaded
94
- * file by its upload time; equal times by id. `after` is a file id from the previous page (any
95
- * version's id finds its file); a cursor that names no file gives an empty page. Storage HEADs run
96
- * at most {@link HEAD_CONCURRENCY} at a time.
113
+ * file by when the conversation started referring to it; equal times by id. `after` is a file id
114
+ * from the previous page (any version's id finds its file); a cursor that names no file gives an
115
+ * empty page. Storage HEADs run at most {@link HEAD_CONCURRENCY} at a time.
97
116
  */
98
117
  list(orgId: string, conversationId: string, viewer: string, opts: {
99
118
  after?: string;
@@ -103,29 +122,17 @@ export declare class ConversationFiles {
103
122
  next: string | null;
104
123
  }>;
105
124
  /**
106
- * One file version of the conversation (any version's id, or an attachment sent in it) with its
107
- * object key, after checking the viewer can read the conversation. For the kernel only
108
- * (previewers, embed grants): the key never leaves the kernel. An attachment whose stored object
109
- * is gone or no longer the one finalize checked → `conversation.artifact_removed`; `recorded` is
110
- * then what a later read of the object must still match ({@link attachmentContentMatches}).
111
- * `sourcePath` is the version group of a delivered file, null for an attachment.
125
+ * One file version of the conversation (any version's id, or an upload sent in it) with its
126
+ * blob, after checking the viewer can read the conversation. For the kernel only (previewers,
127
+ * embed grants, documents): the key never leaves the kernel. A deleted file, or a blob gone or no
128
+ * longer the recorded bytes → `conversation.artifact_removed`; `recorded` is what a later read of
129
+ * the object must still match. `sourcePath` is the version group of a delivered file, null for
130
+ * an upload.
112
131
  */
113
- file(orgId: string, conversationId: string, viewer: string, fileId: string): Promise<{
114
- id: string;
115
- name: string;
116
- contentType: string;
117
- size: number;
118
- objectKey: string;
119
- /** The version group of a delivered file; null for an attachment. */
120
- sourcePath: string | null;
121
- recorded?: {
122
- size: number;
123
- etag: string | null;
124
- };
125
- }>;
132
+ file(orgId: string, conversationId: string, viewer: string, fileId: string): Promise<ResolvedConversationFile>;
126
133
  /**
127
- * Which file a file id names (any version's id, or an attachment sent in the conversation), after
128
- * checking the viewer can read the conversation; storage is not read, so a removed file still
134
+ * Which file a file id names (any version's id, or an upload sent in the conversation), after
135
+ * checking the viewer can read the conversation; storage is not read, and a deleted file still
129
136
  * resolves (its shares are listed and revoked by it).
130
137
  */
131
138
  identify(orgId: string, conversationId: string, viewer: string, fileId: string): Promise<{
@@ -133,30 +140,25 @@ export declare class ConversationFiles {
133
140
  sourcePath: string | null;
134
141
  }>;
135
142
  /**
136
- * The newest delivered version of `sourcePath` whose object is still stored, after checking the
137
- * viewer can read the conversation (a share's "update to latest", file sharing spec §2); with
138
- * `after` (a version id), only versions delivered after it. Versions whose object is gone are
139
- * skipped; at most {@link FILE_VERSIONS_MAX} are looked at. Null when none is available.
143
+ * The newest delivered version of `sourcePath` the conversation refers to that is still
144
+ * readable, after checking the viewer can read the conversation (a share's "update to latest",
145
+ * file sharing spec §2); with `after` (a version id), only versions delivered after it. Versions
146
+ * deleted or whose bytes are gone are skipped; at most {@link FILE_VERSIONS_MAX} are looked at.
147
+ * Null when none is available.
140
148
  */
141
149
  latestAvailable(orgId: string, conversationId: string, viewer: string, sourcePath: string, opts?: {
142
150
  after?: string;
143
- }): Promise<{
144
- id: string;
145
- name: string;
146
- contentType: string;
147
- size: number;
148
- objectKey: string;
149
- } | null>;
151
+ }): Promise<ResolvedConversationFile | null>;
150
152
  /**
151
- * The source paths and attachments the viewer has a share of in the conversation that is not
153
+ * The source paths and uploads the viewer has a share of in the conversation that is not
152
154
  * revoked or expired ({@link FileOutput.shared}).
153
155
  */
154
156
  private viewerShares;
155
157
  /**
156
- * The first `limit` bytes of a text file version (any version's id, or an attachment sent in the
157
- * conversation). Not text, or NUL bytes in
158
- * what was read → `conversation.artifact_not_previewable`; the object is gone →
159
- * `conversation.artifact_removed`. A cut never splits a UTF-8 character.
158
+ * The first `limit` bytes of a text file version (any version's id, or an upload sent in the
159
+ * conversation). Not text, or NUL bytes in what was read → `conversation.artifact_not_previewable`;
160
+ * the file deleted or its bytes gone → `conversation.artifact_removed`. A cut never splits a
161
+ * UTF-8 character.
160
162
  */
161
163
  content(orgId: string, conversationId: string, viewer: string, fileId: string, limit: number): Promise<FileContent>;
162
164
  }
@@ -10,18 +10,18 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
10
10
  import { Inject, Injectable } from "@nestjs/common";
11
11
  import { sql } from "kysely";
12
12
  import { ApiError } from "../errors.js";
13
+ import { isLive, ReferencedFiles, } from "../files/referenced-files.js";
13
14
  import { KernelDb } from "../infra/database/kernel-db.js";
14
- import { StorageService } from "../storage/storage.service.js";
15
15
  import { UUID_RE } from "../ui/registry.js";
16
16
  import { ConversationAccess } from "./access.js";
17
17
  import { isInlineArtifactType, isTextArtifactType } from "./artifacts.js";
18
- import { AttachmentsService } from "./attachments.service.js";
19
18
  /**
20
- * A conversation's files (UI refresh B, spec §5.8 / §5.9): the files the agent delivered
21
- * (`artifact_created`), grouped by `sourcePath` into versions, and the files users sent in it
22
- * (ready `chat` attachments bound to the conversation), newest first. Read on every request (no
23
- * cache); `status` comes from the object store. Both kinds are read under the conversation's
24
- * access: whoever may read the conversation may read the attachments sent in it, nobody else.
19
+ * A conversation's files (UI refresh B, spec §5.8 / §5.9; file library spec §2): the library files
20
+ * the conversation refers to (`conversation_files`). Files an agent delivered or generated are
21
+ * grouped by `source_path` into versions; files a person uploaded are listed one by one; newest
22
+ * first. Read on every request (no cache); `status` comes from the file and its blob (a deleted
23
+ * file, or a blob gone or replaced, is `removed`). Everything is read under the conversation's
24
+ * access: whoever may read the conversation may read the files it refers to, nobody else.
25
25
  */
26
26
  /** Versions listed per file (newest first). */
27
27
  export const FILE_VERSIONS_MAX = 20;
@@ -34,6 +34,30 @@ const artifactNotFound = () => new ApiError("conversation.artifact_not_found", "
34
34
  const removed = () => new ApiError("conversation.artifact_removed", "The file is no longer available");
35
35
  /** Whether `a` lists before `b`: newer first, then the larger id (a total, stable order). */
36
36
  const before = (a, b) => a.at.getTime() !== b.at.getTime() ? a.at.getTime() > b.at.getTime() : a.id > b.id;
37
+ /**
38
+ * Whether a file is listed as an upload in this conversation (one version) rather than a version of
39
+ * one of its deliveries: everything the conversation got by a message (an upload, or a file another
40
+ * conversation delivered that the person sent here), and anything without a version group.
41
+ */
42
+ const isUpload = (f) => f.role === "attached" || f.sourcePath === null;
43
+ /** Log order of two deliveries of one conversation: the event that referred to each, then time. */
44
+ const deliveryOrder = (a, b) => {
45
+ if (a.firstSeq !== null && b.firstSeq !== null && a.firstSeq !== b.firstSeq) {
46
+ return a.firstSeq - b.firstSeq;
47
+ }
48
+ const t = a.referencedAt.getTime() - b.referencedAt.getTime();
49
+ return t !== 0 ? t : a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
50
+ };
51
+ const resolvedOf = (f) => ({
52
+ id: f.id,
53
+ name: f.name,
54
+ contentType: f.contentType,
55
+ size: f.size,
56
+ objectKey: f.blob.objectKey,
57
+ sourcePath: isUpload(f) ? null : f.sourcePath,
58
+ recorded: { size: f.blob.size, etag: f.blob.etag },
59
+ blob: { id: f.blob.id, objectKey: f.blob.objectKey, size: f.blob.size, etag: f.blob.etag },
60
+ });
37
61
  /** The built-in preview kind of a content type (the files list's `previewable`; share metadata too). */
38
62
  export const previewKind = (contentType) => isInlineArtifactType(contentType) ? "image" : isTextArtifactType(contentType) ? "text" : null;
39
63
  /** Drops an incomplete UTF-8 sequence at the end of a cut buffer. */
@@ -89,18 +113,16 @@ async function mapLimit(xs, limit, fn) {
89
113
  }
90
114
  let ConversationFiles = class ConversationFiles {
91
115
  db;
92
- storage;
93
116
  access;
94
- attachments;
95
- constructor(db, storage,
117
+ files;
118
+ constructor(db,
96
119
  /** Same visibility as the event stream. */
97
120
  access,
98
- /** The attachments sent in the conversation, and the rule their stored content must meet. */
99
- attachments) {
121
+ /** The files the conversation refers to, and their bytes (size and etag checked). */
122
+ files) {
100
123
  this.db = db;
101
- this.storage = storage;
102
124
  this.access = access;
103
- this.attachments = attachments;
125
+ this.files = files;
104
126
  }
105
127
  async checkAccess(orgId, conversationId, viewer) {
106
128
  if (!UUID_RE.test(conversationId))
@@ -108,88 +130,67 @@ let ConversationFiles = class ConversationFiles {
108
130
  if (!(await this.access.isAccessible(orgId, conversationId, viewer)))
109
131
  throw conversationNotFound();
110
132
  }
111
- /** Every delivery of the conversation in log order. */
112
- async deliveries(conversationId) {
113
- const rows = await this.db
114
- .selectFrom("conversation_events")
115
- .select(["payload", "created_at"])
116
- .where("conversation_id", "=", conversationId)
117
- .where("type", "=", "artifact_created")
118
- .orderBy("seq")
119
- .execute();
120
- return rows.map((r) => ({
121
- ...r.payload,
122
- createdAt: r.created_at,
123
- }));
133
+ /** Whether a reference can be read now: live, and its stored head still the recorded bytes. */
134
+ async available(orgId, f) {
135
+ return isLive(f) && (await this.files.head(orgId, f)) !== null;
124
136
  }
125
137
  /**
126
- * One file of the conversation by id: a delivered version first, then an attachment sent in it;
127
- * neither → `conversation.artifact_not_found`. An attachment carries `upload`, the row its stored
128
- * object must still match. Call after {@link checkAccess}.
138
+ * One file the conversation refers to by id (any status: a deleted file still resolves, so its
139
+ * shares are found); none → `conversation.artifact_not_found`. Call after {@link checkAccess}.
129
140
  */
130
141
  async resolve(orgId, conversationId, fileId) {
131
142
  if (!UUID_RE.test(fileId))
132
143
  throw artifactNotFound();
133
- const row = await this.db
144
+ const [f] = await this.files.references(orgId, [conversationId], { ids: [fileId] });
145
+ if (!f)
146
+ throw artifactNotFound();
147
+ return f;
148
+ }
149
+ /** The tool call of each delivery logged in the conversation, by artifact (= file) id. */
150
+ async callIds(conversationId) {
151
+ const rows = await this.db
134
152
  .selectFrom("conversation_events")
135
- .select("payload")
153
+ .select([
154
+ sql `payload->>'artifactId'`.as("id"),
155
+ sql `payload->>'callId'`.as("callId"),
156
+ ])
136
157
  .where("conversation_id", "=", conversationId)
137
158
  .where("type", "=", "artifact_created")
138
- .where(sql `payload->>'artifactId'`, "=", fileId)
139
- .limit(1)
140
- .executeTakeFirst();
141
- if (row) {
142
- const a = row.payload;
143
- return {
144
- id: a.artifactId,
145
- name: a.name,
146
- contentType: a.contentType,
147
- size: a.size,
148
- objectKey: a.objectKey,
149
- sourcePath: a.sourcePath,
150
- };
151
- }
152
- const [u] = await this.attachments.boundRows(orgId, conversationId, fileId);
153
- if (!u)
154
- throw artifactNotFound();
155
- return {
156
- id: u.id,
157
- name: u.name,
158
- contentType: u.contentType,
159
- size: u.size,
160
- objectKey: u.objectKey,
161
- sourcePath: null,
162
- upload: u,
163
- };
159
+ .execute();
160
+ return new Map(rows.map((r) => [r.id, r.callId]));
164
161
  }
165
162
  /**
166
163
  * Files newest first: a delivered file by its **first** delivery, so a new version keeps the
167
164
  * file's place and a cursor stays stable while files are re-delivered (review M2); an uploaded
168
- * file by its upload time; equal times by id. `after` is a file id from the previous page (any
169
- * version's id finds its file); a cursor that names no file gives an empty page. Storage HEADs run
170
- * at most {@link HEAD_CONCURRENCY} at a time.
165
+ * file by when the conversation started referring to it; equal times by id. `after` is a file id
166
+ * from the previous page (any version's id finds its file); a cursor that names no file gives an
167
+ * empty page. Storage HEADs run at most {@link HEAD_CONCURRENCY} at a time.
171
168
  */
172
169
  async list(orgId, conversationId, viewer, opts) {
173
170
  await this.checkAccess(orgId, conversationId, viewer);
171
+ const refs = await this.files.references(orgId, [conversationId]);
174
172
  const groups = new Map();
175
- for (const d of await this.deliveries(conversationId)) {
176
- // insertion order = first delivery (oldest first)
177
- const g = groups.get(d.sourcePath) ?? [];
178
- g.push(d);
179
- groups.set(d.sourcePath, g);
173
+ const uploads = [];
174
+ for (const f of refs) {
175
+ if (isUpload(f)) {
176
+ uploads.push(f);
177
+ continue;
178
+ }
179
+ const g = groups.get(f.sourcePath) ?? [];
180
+ g.push(f);
181
+ groups.set(f.sourcePath, g);
180
182
  }
181
- const delivered = [...groups.values()].reverse().map((g) => {
183
+ const delivered = [...groups.values()]
184
+ .map((g) => g.sort(deliveryOrder))
185
+ .map((g) => {
182
186
  const first = g[0];
183
- return { origin: "delivered", at: first.createdAt, id: first.artifactId, group: g };
184
- });
187
+ return { origin: "delivered", at: first.referencedAt, id: first.id, group: g };
188
+ })
189
+ .sort((a, b) => (before(a, b) ? -1 : 1));
185
190
  // newest first, the larger id first at equal times
186
- const bound = (await this.attachments.boundRows(orgId, conversationId)).reverse();
187
- const uploaded = bound.map((u) => ({
188
- origin: "uploaded",
189
- at: u.createdAt,
190
- id: u.id,
191
- upload: u,
192
- }));
191
+ const uploaded = uploads
192
+ .map((u) => ({ origin: "uploaded", at: u.referencedAt, id: u.id, upload: u }))
193
+ .sort((a, b) => (before(a, b) ? -1 : 1));
193
194
  // merge the two newest-first lists (each keeps its own order)
194
195
  const ordered = [];
195
196
  let i = 0;
@@ -209,19 +210,19 @@ let ConversationFiles = class ConversationFiles {
209
210
  let start = 0;
210
211
  if (opts.after !== undefined) {
211
212
  const after = opts.after;
212
- const at = ordered.findIndex((e) => e.origin === "delivered" ? e.group.some((d) => d.artifactId === after) : e.id === after);
213
+ const at = ordered.findIndex((e) => e.origin === "delivered" ? e.group.some((d) => d.id === after) : e.id === after);
213
214
  if (at < 0)
214
215
  return { files: [], next: null };
215
216
  start = at + 1;
216
217
  }
217
218
  const page = ordered.slice(start, start + opts.limit);
218
219
  const shares = await this.viewerShares(orgId, conversationId, viewer);
219
- const store = this.storage.kernel(orgId);
220
+ const calls = page.some((e) => e.origin === "delivered")
221
+ ? await this.callIds(conversationId)
222
+ : new Map();
220
223
  const files = await mapLimit(page, HEAD_CONCURRENCY, async (e) => {
221
224
  if (e.origin === "uploaded") {
222
225
  const u = e.upload;
223
- const head = await store.head(u.objectKey);
224
- const same = this.attachments.storedMatches(u, head, { orgId, conversationId });
225
226
  return {
226
227
  id: u.id,
227
228
  name: u.name,
@@ -229,10 +230,10 @@ let ConversationFiles = class ConversationFiles {
229
230
  size: u.size,
230
231
  sourcePath: u.name,
231
232
  version: 1,
232
- versions: [{ id: u.id, size: u.size, createdAt: u.createdAt }],
233
+ versions: [{ id: u.id, size: u.size, createdAt: u.referencedAt }],
233
234
  callId: "",
234
- createdAt: u.createdAt,
235
- status: same ? "available" : "removed",
235
+ createdAt: u.referencedAt,
236
+ status: (await this.available(orgId, u)) ? "available" : "removed",
236
237
  previewable: previewKind(u.contentType),
237
238
  origin: "uploaded",
238
239
  shared: shares.uploads.has(u.id),
@@ -240,95 +241,81 @@ let ConversationFiles = class ConversationFiles {
240
241
  }
241
242
  const g = e.group;
242
243
  const latest = g.at(-1);
243
- const head = await store.head(latest.objectKey);
244
+ const sourcePath = latest.sourcePath;
244
245
  return {
245
- id: latest.artifactId,
246
+ id: latest.id,
246
247
  name: latest.name,
247
248
  contentType: latest.contentType,
248
249
  size: latest.size,
249
- sourcePath: latest.sourcePath,
250
+ sourcePath,
250
251
  version: g.length,
251
252
  versions: g
252
253
  .slice(-FILE_VERSIONS_MAX)
253
254
  .reverse()
254
- .map((d) => ({ id: d.artifactId, size: d.size, createdAt: d.createdAt })),
255
- callId: latest.callId,
256
- createdAt: latest.createdAt,
257
- status: head ? "available" : "removed",
255
+ .map((d) => ({ id: d.id, size: d.size, createdAt: d.referencedAt })),
256
+ callId: calls.get(latest.id) ?? "",
257
+ createdAt: latest.referencedAt,
258
+ status: (await this.available(orgId, latest)) ? "available" : "removed",
258
259
  previewable: previewKind(latest.contentType),
259
260
  origin: "delivered",
260
- shared: shares.paths.has(latest.sourcePath),
261
+ shared: shares.paths.has(sourcePath),
261
262
  };
262
263
  });
263
264
  const next = start + opts.limit < ordered.length ? (files.at(-1)?.id ?? null) : null;
264
265
  return { files, next };
265
266
  }
266
267
  /**
267
- * One file version of the conversation (any version's id, or an attachment sent in it) with its
268
- * object key, after checking the viewer can read the conversation. For the kernel only
269
- * (previewers, embed grants): the key never leaves the kernel. An attachment whose stored object
270
- * is gone or no longer the one finalize checked → `conversation.artifact_removed`; `recorded` is
271
- * then what a later read of the object must still match ({@link attachmentContentMatches}).
272
- * `sourcePath` is the version group of a delivered file, null for an attachment.
268
+ * One file version of the conversation (any version's id, or an upload sent in it) with its
269
+ * blob, after checking the viewer can read the conversation. For the kernel only (previewers,
270
+ * embed grants, documents): the key never leaves the kernel. A deleted file, or a blob gone or no
271
+ * longer the recorded bytes → `conversation.artifact_removed`; `recorded` is what a later read of
272
+ * the object must still match. `sourcePath` is the version group of a delivered file, null for
273
+ * an upload.
273
274
  */
274
275
  async file(orgId, conversationId, viewer, fileId) {
275
276
  await this.checkAccess(orgId, conversationId, viewer);
276
- const { upload, ...f } = await this.resolve(orgId, conversationId, fileId);
277
- if (!upload)
278
- return f;
279
- const head = await this.storage.kernel(orgId).head(upload.objectKey);
280
- if (!this.attachments.storedMatches(upload, head, { orgId, conversationId }))
277
+ const f = await this.resolve(orgId, conversationId, fileId);
278
+ if (!isLive(f) || (await this.files.head(orgId, f)) === null)
281
279
  throw removed();
282
- return { ...f, recorded: { size: upload.size, etag: upload.etag } };
280
+ return resolvedOf(f);
283
281
  }
284
282
  /**
285
- * Which file a file id names (any version's id, or an attachment sent in the conversation), after
286
- * checking the viewer can read the conversation; storage is not read, so a removed file still
283
+ * Which file a file id names (any version's id, or an upload sent in the conversation), after
284
+ * checking the viewer can read the conversation; storage is not read, and a deleted file still
287
285
  * resolves (its shares are listed and revoked by it).
288
286
  */
289
287
  async identify(orgId, conversationId, viewer, fileId) {
290
288
  await this.checkAccess(orgId, conversationId, viewer);
291
289
  const f = await this.resolve(orgId, conversationId, fileId);
292
- return { id: f.id, sourcePath: f.sourcePath };
290
+ return { id: f.id, sourcePath: isUpload(f) ? null : f.sourcePath };
293
291
  }
294
292
  /**
295
- * The newest delivered version of `sourcePath` whose object is still stored, after checking the
296
- * viewer can read the conversation (a share's "update to latest", file sharing spec §2); with
297
- * `after` (a version id), only versions delivered after it. Versions whose object is gone are
298
- * skipped; at most {@link FILE_VERSIONS_MAX} are looked at. Null when none is available.
293
+ * The newest delivered version of `sourcePath` the conversation refers to that is still
294
+ * readable, after checking the viewer can read the conversation (a share's "update to latest",
295
+ * file sharing spec §2); with `after` (a version id), only versions delivered after it. Versions
296
+ * deleted or whose bytes are gone are skipped; at most {@link FILE_VERSIONS_MAX} are looked at.
297
+ * Null when none is available.
299
298
  */
300
299
  async latestAvailable(orgId, conversationId, viewer, sourcePath, opts = {}) {
301
300
  await this.checkAccess(orgId, conversationId, viewer);
302
- const rows = await this.db
303
- .selectFrom("conversation_events")
304
- .select(["payload", "seq"])
305
- .where("conversation_id", "=", conversationId)
306
- .where("type", "=", "artifact_created")
307
- .where(sql `payload->>'sourcePath'`, "=", sourcePath)
308
- .orderBy("seq", "desc")
309
- .execute();
310
- let candidates = rows.map((r) => r.payload);
301
+ const refs = await this.files.references(orgId, [conversationId]);
302
+ let candidates = refs
303
+ .filter((f) => !isUpload(f) && f.sourcePath === sourcePath)
304
+ .sort(deliveryOrder)
305
+ .reverse();
311
306
  if (opts.after !== undefined) {
312
- const at = candidates.findIndex((d) => d.artifactId === opts.after);
307
+ const at = candidates.findIndex((d) => d.id === opts.after);
313
308
  if (at >= 0)
314
309
  candidates = candidates.slice(0, at);
315
310
  }
316
- const store = this.storage.kernel(orgId);
317
311
  for (const d of candidates.slice(0, FILE_VERSIONS_MAX)) {
318
- if (await store.head(d.objectKey)) {
319
- return {
320
- id: d.artifactId,
321
- name: d.name,
322
- contentType: d.contentType,
323
- size: d.size,
324
- objectKey: d.objectKey,
325
- };
326
- }
312
+ if (isLive(d) && (await this.files.head(orgId, d)) !== null)
313
+ return resolvedOf(d);
327
314
  }
328
315
  return null;
329
316
  }
330
317
  /**
331
- * The source paths and attachments the viewer has a share of in the conversation that is not
318
+ * The source paths and uploads the viewer has a share of in the conversation that is not
332
319
  * revoked or expired ({@link FileOutput.shared}).
333
320
  */
334
321
  async viewerShares(orgId, conversationId, viewer) {
@@ -352,10 +339,10 @@ let ConversationFiles = class ConversationFiles {
352
339
  return { paths, uploads };
353
340
  }
354
341
  /**
355
- * The first `limit` bytes of a text file version (any version's id, or an attachment sent in the
356
- * conversation). Not text, or NUL bytes in
357
- * what was read → `conversation.artifact_not_previewable`; the object is gone →
358
- * `conversation.artifact_removed`. A cut never splits a UTF-8 character.
342
+ * The first `limit` bytes of a text file version (any version's id, or an upload sent in the
343
+ * conversation). Not text, or NUL bytes in what was read → `conversation.artifact_not_previewable`;
344
+ * the file deleted or its bytes gone → `conversation.artifact_removed`. A cut never splits a
345
+ * UTF-8 character.
359
346
  */
360
347
  async content(orgId, conversationId, viewer, fileId, limit) {
361
348
  await this.checkAccess(orgId, conversationId, viewer);
@@ -363,18 +350,12 @@ let ConversationFiles = class ConversationFiles {
363
350
  if (!isTextArtifactType(a.contentType)) {
364
351
  throw new ApiError("conversation.artifact_not_previewable", "Only text files have content");
365
352
  }
353
+ if (!isLive(a))
354
+ throw removed();
366
355
  const max = Math.min(limit, FILE_CONTENT_MAX_BYTES);
367
- const got = await this.storage
368
- .kernel(orgId)
369
- .get(a.objectKey, { range: { start: 0, end: max - 1 } });
356
+ const got = await this.files.open(orgId, a, { range: { start: 0, end: max - 1 } });
370
357
  if (!got)
371
358
  throw removed();
372
- // an attachment whose object changed after finalize is not the upload any more
373
- if (a.upload &&
374
- !this.attachments.storedMatches(a.upload, got.head, { orgId, conversationId })) {
375
- await got.body.cancel().catch(() => { });
376
- throw removed();
377
- }
378
359
  let body = await readPrefix(got.body, max);
379
360
  if (body.includes(0)) {
380
361
  throw new ApiError("conversation.artifact_not_previewable", "This file contains binary data and has no text content");
@@ -389,8 +370,7 @@ let ConversationFiles = class ConversationFiles {
389
370
  ConversationFiles = __decorate([
390
371
  Injectable(),
391
372
  __param(0, Inject(KernelDb)),
392
- __param(1, Inject(StorageService)),
393
- __param(2, Inject(ConversationAccess)),
394
- __param(3, Inject(AttachmentsService))
373
+ __param(1, Inject(ConversationAccess)),
374
+ __param(2, Inject(ReferencedFiles))
395
375
  ], ConversationFiles);
396
376
  export { ConversationFiles };