@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
@@ -0,0 +1,178 @@
1
+ import type { EventFilter } from "@capybara-hq/plugin-sdk";
2
+ import type { ModelRef } from "../agents/definition.js";
3
+ /**
4
+ * Automations (spec 2026-11-11-scheduled-tasks-design.md §4): the shapes the kernel reads and
5
+ * writes. The rows live in `capybara.automations`, `automation_runs` and
6
+ * `automation_grants` (migration 0113).
7
+ */
8
+ /**
9
+ * When an automation fires: a 5-field cron, or once at a local date-time, both in an IANA zone
10
+ * (§5); or on an extension's trigger events (collaboration spec §4.1): events of `types` from the
11
+ * extension `source` whose facts match `filter`. An event automation has no next run time.
12
+ */
13
+ export type AutomationTrigger = {
14
+ kind: "cron";
15
+ expr: string;
16
+ timeZone: string;
17
+ } | {
18
+ kind: "once";
19
+ at: string;
20
+ timeZone: string;
21
+ } | {
22
+ kind: "event";
23
+ source: string;
24
+ types: string[];
25
+ filter: EventFilter;
26
+ };
27
+ /** The time triggers: the ones with a next run time. */
28
+ export type AutomationTimeTrigger = Exclude<AutomationTrigger, {
29
+ kind: "event";
30
+ }>;
31
+ /**
32
+ * Where a run posts: a new conversation, one existing conversation, an IM chat (§3), or one
33
+ * conversation per event subject (collaboration spec §4.5).
34
+ */
35
+ export type AutomationMode = "new" | "conversation" | "chat" | "subject";
36
+ /** Who made an automation: its owner, or the kernel from an acceptance rule (collaboration §4.4). */
37
+ export type AutomationOrigin = "person" | "acceptance";
38
+ /** Whom an automation acts as (`automations.principal`); today always the member `account_id`. */
39
+ export type AutomationPrincipal = {
40
+ kind: "member";
41
+ accountId: string;
42
+ };
43
+ /** What a question asked in an unattended run does (collaboration spec §4.7). */
44
+ export type AutomationUnattendedAsk = "fail" | "wait";
45
+ /** The IM chat a `chat`-mode task posts into. */
46
+ export interface AutomationChat {
47
+ connectionId: string;
48
+ chatId: string;
49
+ chatType: "dm" | "group";
50
+ }
51
+ export type AutomationStatus = "active" | "paused" | "ended";
52
+ export type AutomationPauseReason = "creator" | "admin" | "failures" | "creator_inactive" | "agent_gone" | "target_gone" | "conversation_gone" | "org_disabled" | "expired"
53
+ /** The stored trigger cannot be evaluated (the scanner stops selecting the task). */
54
+ | "invalid_schedule";
55
+ export type TaskRunStatus = "pending" | "running" | "completed" | "failed" | "skipped";
56
+ export type TaskRunSkipReason = "missed" | "overlap" | "rate_limited" | "queue_paused" | "removed" | "device_offline" | "creator_inactive" | "agent_gone" | "target_gone" | "conversation_gone" | "org_disabled"
57
+ /** The channel's extension is disabled in the org: the task stays active. */
58
+ | "plugin_disabled"
59
+ /** A group chat on a channel without threads (a group run needs its own thread). */
60
+ | "unsupported_target"
61
+ /** The task was deleted after the run was recorded. */
62
+ | "task_deleted"
63
+ /** The task was paused after the scan recorded the tick (a "run now" still runs). */
64
+ | "task_paused"
65
+ /** An event run: the subject's run rate limit was reached (collaboration spec §4.7). */
66
+ | "subject_rate";
67
+ /** A task as the kernel reads it: the columns of `capybara.automations`, camel-cased. */
68
+ export interface Automation {
69
+ id: string;
70
+ orgId: string;
71
+ /** The creator: every run acts as this account. */
72
+ accountId: string;
73
+ agentId: string;
74
+ name: string;
75
+ prompt: string;
76
+ trigger: AutomationTrigger;
77
+ mode: AutomationMode;
78
+ /** The target conversation (`conversation` mode only). */
79
+ conversationId: string | null;
80
+ /** The target chat (`chat` mode only). */
81
+ chat: AutomationChat | null;
82
+ /** Null = the agent's default model. */
83
+ model: ModelRef | null;
84
+ /** Null = the agent's default effort. */
85
+ effort: string | null;
86
+ /** Null = the default pick for new conversations. */
87
+ deviceId: string | null;
88
+ /** What a question in an unattended run does; null = the org's default. */
89
+ unattendedAsk: AutomationUnattendedAsk | null;
90
+ status: AutomationStatus;
91
+ pauseReason: AutomationPauseReason | null;
92
+ /** The account that paused the task (creator or admin); null when not paused by a person. */
93
+ pausedBy: string | null;
94
+ /** The next fire time, jitter included; null when the task is not active. */
95
+ nextRunAt: Date | null;
96
+ jitterSeconds: number;
97
+ expiresAt: Date | null;
98
+ consecutiveFailures: number;
99
+ createdAt: Date;
100
+ updatedAt: Date;
101
+ }
102
+ /** One tick of a task (or a trial run): the columns of `capybara.automation_runs`. */
103
+ export interface TaskRun {
104
+ id: string;
105
+ taskId: string;
106
+ orgId: string;
107
+ /**
108
+ * The tick, before jitter (for a trial run: when it was requested); null for an event run
109
+ * (collaboration spec §4.1).
110
+ */
111
+ scheduledFor: Date | null;
112
+ /**
113
+ * What the run is unique by outside trials: the tick as `Date.toISOString()` for a time run, the
114
+ * event id for an event run (`UNIQUE (automation_id, trigger_key) WHERE NOT trial`).
115
+ */
116
+ triggerKey: string;
117
+ firedAt: Date | null;
118
+ trial: boolean;
119
+ /** A person's "run now" (trial or not), outside the schedule. */
120
+ manual: boolean;
121
+ conversationId: string | null;
122
+ runId: string | null;
123
+ status: TaskRunStatus;
124
+ skipReason: TaskRunSkipReason | null;
125
+ error: string | null;
126
+ /** Tool keys refused for lack of a grant (§7). */
127
+ missingGrants: string[];
128
+ }
129
+ /** A run with a tick: every time run (event runs have none). */
130
+ export type TimedTaskRun = TaskRun & {
131
+ scheduledFor: Date;
132
+ };
133
+ /**
134
+ * The marker a task's message carries (`conversation_queue.schedule`, then `user_message`): not
135
+ * model-visible as such; `taskName` and `localTime` feed the English header the model sees.
136
+ */
137
+ export interface ScheduleMarker {
138
+ taskId: string;
139
+ taskRunId: string;
140
+ /** ISO timestamp of the tick. */
141
+ scheduledFor: string;
142
+ trial: boolean;
143
+ taskName: string;
144
+ /** The tick in the task's zone, as the header shows it. */
145
+ localTime: string;
146
+ }
147
+ /**
148
+ * The marker an event automation's message carries (`conversation_queue.automation_event`, then
149
+ * `user_message.automationEvent`, collaboration spec §4.6): the automation and run, and the
150
+ * events the message reports, in order (coalesced events share one message): at most the first 5
151
+ * and the latest 15 of the run's events, `omittedEvents` counting the ones left out between them.
152
+ */
153
+ export interface AutomationEventMarker {
154
+ automationId: string;
155
+ automationRunId: string;
156
+ automationName: string;
157
+ /** The extension that emitted the events (the automation's trigger source). */
158
+ source: string;
159
+ events: Array<{
160
+ id: string;
161
+ type: string;
162
+ subject: {
163
+ kind: string;
164
+ id: string;
165
+ title: string;
166
+ url?: string | undefined;
167
+ };
168
+ summary: string;
169
+ }>;
170
+ /** How many of the run's events the marker leaves out (absent: none). */
171
+ omittedEvents?: number | undefined;
172
+ }
173
+ /** A remembered "always allow" of a task (same meaning as `conversation_tool_grants`). */
174
+ export interface AutomationGrant {
175
+ toolKey: string;
176
+ specHash: string;
177
+ connectorId: string | null;
178
+ }
@@ -1,8 +1,8 @@
1
1
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
2
  import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
3
3
  /**
4
- * The scheduled task run a kernel run serves (scheduled tasks spec §4, §7), or null for an attended
5
- * run: `runs.scheduled_task_run_id`, set on the run a task's message triggered and inherited by the
4
+ * The automation run a kernel run serves (scheduled tasks spec §4, §7), or null for an attended
5
+ * run: `runs.automation_run_id`, set on the run a task's message triggered and inherited by the
6
6
  * runs descending from it without a person (sub-agent runs, wake runs). A run whose task run row is
7
7
  * gone is treated as attended.
8
8
  */
@@ -11,16 +11,25 @@ export declare function unattendedTaskRunOf(db: KernelSchemaDb, runId: string):
11
11
  taskId: string;
12
12
  trial: boolean;
13
13
  } | null>;
14
+ /** What a new run takes from its boundary event ({@link automationRunAt}). */
15
+ export interface AutomationLineage {
16
+ /** `runs.automation_run_id`: the automation run it serves (unattended); null = attended. */
17
+ automationRunId: string | null;
18
+ /** `runs.trigger_event_id`: the trigger event it started from (collaboration spec §4.7). */
19
+ triggerEventId: string | null;
20
+ }
14
21
  /**
15
- * The `scheduled_task_run_id` a new run whose boundary (`runs.trigger_seq`) is event `seq` of
16
- * `conversationId` gets (scheduled tasks spec §4), read in the caller's transaction:
17
- * - a `user_message`: its schedule marker's task run; a sub-agent's task or `SendMessage` message
18
- * (`dispatch`): what its parent run has; a person's message: null;
22
+ * The `automation_run_id` and `trigger_event_id` a new run whose boundary (`runs.trigger_seq`) is
23
+ * event `seq` of `conversationId` gets (scheduled tasks spec §4, collaboration spec §4.7), read in
24
+ * the caller's transaction:
25
+ * - a `user_message`: its schedule marker's task run (no event), or its event marker's run and
26
+ * last event (the most recent cause); a sub-agent's task or `SendMessage` message (`dispatch`):
27
+ * what its parent run has; a person's message: neither;
19
28
  * - a `background_task_finished`: what the run that launched the task has;
20
29
  * - a `subagent_delivery`: what the child run that delivered it has (it inherited from its parent);
21
- * - anything else, or no such event: null.
30
+ * - anything else, or no such event: neither.
22
31
  */
23
- export declare function scheduledTaskRunAt(t: KernelSchemaDb, conversationId: string, seq: number | string): Promise<string | null>;
32
+ export declare function automationRunAt(t: KernelSchemaDb, conversationId: string, seq: number | string): Promise<AutomationLineage>;
24
33
  /** The tool result of an ask outside the task's grants in an unattended run, `fail` mode (§7). */
25
34
  export declare const UNATTENDED_ASK_ERROR: (tool: string) => string;
26
35
  /**
@@ -48,7 +57,7 @@ export declare const connectionGrantKey: (connectorSlug: string) => string;
48
57
  /** The `missing_grants` entry of a refused network host (network ask-to-allow spec §8.1, §8.3). */
49
58
  export declare const networkGrantKey: (host: string) => string;
50
59
  /**
51
- * What the tool policy knows of the scheduled task run a kernel run serves (`PolicyContext`): the
60
+ * What the tool policy knows of the automation run a kernel run serves (`PolicyContext`): the
52
61
  * task (its grants), the task run (its `missing_grants`), whether it is a trial run (attended), and
53
62
  * the org's `unattendedAsk` and `forbiddenTools`, read live.
54
63
  */
@@ -66,13 +75,15 @@ export declare const unattendedRules: (u: UnattendedRun | null | undefined) => u
66
75
  /** In `fail` mode, an unattended run never waits for a person (§7). */
67
76
  export declare const unattendedFails: (u: UnattendedRun | null | undefined) => boolean;
68
77
  /**
69
- * The scheduled task context of a run's tool calls (`PolicyContext.unattended`), or null when the
78
+ * The automation context of a run's tool calls (`PolicyContext.unattended`), or null when the
70
79
  * run is attended: it serves no task run, or a person's message joined it ({@link personJoined}).
71
80
  * A trial run is returned with `trial: true` (attended; its approvals become task grants).
81
+ * `unattendedAsk` is the automation's own `unattended_ask` when set (collaboration spec §4.4),
82
+ * else the org's default.
72
83
  */
73
84
  export declare function unattendedRunOf(db: KernelSchemaDb, config: Pick<KernelConfigService, "get">, orgId: string, runId: string): Promise<UnattendedRun | null>;
74
85
  /**
75
- * The scheduled task whose grants a run's actions use (spec §7; network ask-to-allow spec §8.1),
86
+ * The automation whose grants a run's actions use (spec §7; network ask-to-allow spec §8.1),
76
87
  * or null: the run serves a task run that is no trial run and no person joined
77
88
  * ({@link personJoined}): the same rule as {@link unattendedRules} on {@link unattendedRunOf},
78
89
  * without the org's settings. `accountId`: the task's creator; `orgId`: the task's org.
@@ -1,30 +1,33 @@
1
1
  import { sql } from "kysely";
2
- import { readScheduleSettings } from "./effective-settings.js";
2
+ import { readAutomationSettings } from "./effective-settings.js";
3
3
  /**
4
- * The scheduled task run a kernel run serves (scheduled tasks spec §4, §7), or null for an attended
5
- * run: `runs.scheduled_task_run_id`, set on the run a task's message triggered and inherited by the
4
+ * The automation run a kernel run serves (scheduled tasks spec §4, §7), or null for an attended
5
+ * run: `runs.automation_run_id`, set on the run a task's message triggered and inherited by the
6
6
  * runs descending from it without a person (sub-agent runs, wake runs). A run whose task run row is
7
7
  * gone is treated as attended.
8
8
  */
9
9
  export async function unattendedTaskRunOf(db, runId) {
10
10
  const row = await db
11
11
  .selectFrom("runs as r")
12
- .innerJoin("scheduled_task_runs as tr", "tr.id", "r.scheduled_task_run_id")
13
- .select(["tr.id", "tr.task_id", "tr.trial"])
12
+ .innerJoin("automation_runs as tr", "tr.id", "r.automation_run_id")
13
+ .select(["tr.id", "tr.automation_id", "tr.trial"])
14
14
  .where("r.id", "=", runId)
15
15
  .executeTakeFirst();
16
- return row ? { taskRunId: row.id, taskId: row.task_id, trial: row.trial } : null;
16
+ return row ? { taskRunId: row.id, taskId: row.automation_id, trial: row.trial } : null;
17
17
  }
18
+ const NO_LINEAGE = { automationRunId: null, triggerEventId: null };
18
19
  /**
19
- * The `scheduled_task_run_id` a new run whose boundary (`runs.trigger_seq`) is event `seq` of
20
- * `conversationId` gets (scheduled tasks spec §4), read in the caller's transaction:
21
- * - a `user_message`: its schedule marker's task run; a sub-agent's task or `SendMessage` message
22
- * (`dispatch`): what its parent run has; a person's message: null;
20
+ * The `automation_run_id` and `trigger_event_id` a new run whose boundary (`runs.trigger_seq`) is
21
+ * event `seq` of `conversationId` gets (scheduled tasks spec §4, collaboration spec §4.7), read in
22
+ * the caller's transaction:
23
+ * - a `user_message`: its schedule marker's task run (no event), or its event marker's run and
24
+ * last event (the most recent cause); a sub-agent's task or `SendMessage` message (`dispatch`):
25
+ * what its parent run has; a person's message: neither;
23
26
  * - a `background_task_finished`: what the run that launched the task has;
24
27
  * - a `subagent_delivery`: what the child run that delivered it has (it inherited from its parent);
25
- * - anything else, or no such event: null.
28
+ * - anything else, or no such event: neither.
26
29
  */
27
- export async function scheduledTaskRunAt(t, conversationId, seq) {
30
+ export async function automationRunAt(t, conversationId, seq) {
28
31
  const ev = await t
29
32
  .selectFrom("conversation_events")
30
33
  .select(["type", "payload"])
@@ -32,12 +35,21 @@ export async function scheduledTaskRunAt(t, conversationId, seq) {
32
35
  .where("seq", "=", String(seq))
33
36
  .executeTakeFirst();
34
37
  if (!ev)
35
- return null;
38
+ return NO_LINEAGE;
36
39
  const p = ev.payload;
37
40
  let runId;
38
41
  if (ev.type === "user_message") {
39
- if (typeof p.schedule?.taskRunId === "string")
40
- return p.schedule.taskRunId;
42
+ if (typeof p.schedule?.taskRunId === "string") {
43
+ return { automationRunId: p.schedule.taskRunId, triggerEventId: null };
44
+ }
45
+ const marker = p.automationEvent;
46
+ if (typeof marker?.automationRunId === "string") {
47
+ const last = marker.events?.at(-1)?.id;
48
+ return {
49
+ automationRunId: marker.automationRunId,
50
+ triggerEventId: typeof last === "string" ? last : null,
51
+ };
52
+ }
41
53
  if (typeof p.dispatch?.parentRunId === "string")
42
54
  runId = p.dispatch.parentRunId;
43
55
  }
@@ -55,13 +67,15 @@ export async function scheduledTaskRunAt(t, conversationId, seq) {
55
67
  runId = p.childRunId;
56
68
  }
57
69
  if (runId === undefined)
58
- return null;
70
+ return NO_LINEAGE;
59
71
  const run = await t
60
72
  .selectFrom("runs")
61
- .select("scheduled_task_run_id")
73
+ .select(["automation_run_id", "trigger_event_id"])
62
74
  .where("id", "=", runId)
63
75
  .executeTakeFirst();
64
- return run?.scheduled_task_run_id ?? null;
76
+ return run
77
+ ? { automationRunId: run.automation_run_id, triggerEventId: run.trigger_event_id }
78
+ : NO_LINEAGE;
65
79
  }
66
80
  /** The tool result of an ask outside the task's grants in an unattended run, `fail` mode (§7). */
67
81
  export const UNATTENDED_ASK_ERROR = (tool) => `This scheduled run cannot ask for approval; the task's owner can add ${tool} to the task's approved tools.`;
@@ -101,9 +115,9 @@ export const unattendedRules = (u) => u !== null && u !== undefined && !u.trial;
101
115
  /** In `fail` mode, an unattended run never waits for a person (§7). */
102
116
  export const unattendedFails = (u) => unattendedRules(u) && u.unattendedAsk === "fail";
103
117
  /**
104
- * Whether the task's creator joined the run (scheduled tasks rulings, Task 7 and Task 8): the run's
118
+ * Whether the task's creator joined the run (automations rulings, Task 7 and Task 8): the run's
105
119
  * conversation holds a `user_message` after the run's boundary (`trigger_seq`) that the creator
106
- * wrote (no schedule marker, no sub-agent dispatch, `authorAccountId` = the task's account) and
120
+ * wrote (no schedule or event marker, no sub-agent dispatch, `authorAccountId` = the task's account) and
107
121
  * that the run already absorbed (at or below the `absorbedUpToSeq` of its latest model call). The
108
122
  * rest of such a run is attended. Another person's message (someone replying in a group thread the
109
123
  * task posted into) does not make it attended: the task's grants are the creator's, and only the
@@ -112,14 +126,15 @@ export const unattendedFails = (u) => unattendedRules(u) && u.unattendedAsk ===
112
126
  async function personJoined(db, runId) {
113
127
  const row = await db
114
128
  .selectFrom("runs as r")
115
- .innerJoin("scheduled_task_runs as tr", "tr.id", "r.scheduled_task_run_id")
116
- .innerJoin("scheduled_tasks as st", "st.id", "tr.task_id")
129
+ .innerJoin("automation_runs as tr", "tr.id", "r.automation_run_id")
130
+ .innerJoin("automations as st", "st.id", "tr.automation_id")
117
131
  .select(sql `EXISTS (
118
132
  SELECT 1 FROM capybara.conversation_events u
119
133
  WHERE u.conversation_id = r.conversation_id
120
134
  AND u.type = 'user_message'
121
135
  AND u.seq > r.trigger_seq
122
136
  AND NOT (u.payload ? 'schedule')
137
+ AND NOT (u.payload ? 'automationEvent')
123
138
  AND NOT (u.payload ? 'dispatch')
124
139
  AND u.payload->>'authorAccountId' = st.account_id
125
140
  AND u.seq <= (
@@ -136,9 +151,11 @@ async function personJoined(db, runId) {
136
151
  return row?.joined === true;
137
152
  }
138
153
  /**
139
- * The scheduled task context of a run's tool calls (`PolicyContext.unattended`), or null when the
154
+ * The automation context of a run's tool calls (`PolicyContext.unattended`), or null when the
140
155
  * run is attended: it serves no task run, or a person's message joined it ({@link personJoined}).
141
156
  * A trial run is returned with `trial: true` (attended; its approvals become task grants).
157
+ * `unattendedAsk` is the automation's own `unattended_ask` when set (collaboration spec §4.4),
158
+ * else the org's default.
142
159
  */
143
160
  export async function unattendedRunOf(db, config, orgId, runId) {
144
161
  const run = await unattendedTaskRunOf(db, runId);
@@ -146,16 +163,22 @@ export async function unattendedRunOf(db, config, orgId, runId) {
146
163
  return null;
147
164
  if (!run.trial && (await personJoined(db, runId)))
148
165
  return null;
149
- const s = await readScheduleSettings(db, config, orgId);
166
+ const s = await readAutomationSettings(db, config, orgId);
167
+ // org-filter: the task id is read from the kernel run's own automation_run_id, never input
168
+ const own = await db
169
+ .selectFrom("automations")
170
+ .select("unattended_ask")
171
+ .where("id", "=", run.taskId)
172
+ .executeTakeFirst();
150
173
  return {
151
174
  ...run,
152
- unattendedAsk: s.unattendedAsk,
175
+ unattendedAsk: own?.unattended_ask ?? s.unattendedAsk,
153
176
  forbiddenTools: s.forbiddenTools,
154
177
  forbiddenToolsUnreadable: s.forbiddenToolsUnreadable,
155
178
  };
156
179
  }
157
180
  /**
158
- * The scheduled task whose grants a run's actions use (spec §7; network ask-to-allow spec §8.1),
181
+ * The automation whose grants a run's actions use (spec §7; network ask-to-allow spec §8.1),
159
182
  * or null: the run serves a task run that is no trial run and no person joined
160
183
  * ({@link personJoined}): the same rule as {@link unattendedRules} on {@link unattendedRunOf},
161
184
  * without the org's settings. `accountId`: the task's creator; `orgId`: the task's org.
@@ -166,9 +189,9 @@ export async function taskGrantsRunOf(db, runId) {
166
189
  return null;
167
190
  if (await personJoined(db, runId))
168
191
  return null;
169
- // org-filter: the task id is read from the kernel run's own scheduled_task_run_id, never input
192
+ // org-filter: the task id is read from the kernel run's own automation_run_id, never input
170
193
  const task = await db
171
- .selectFrom("scheduled_tasks")
194
+ .selectFrom("automations")
172
195
  .select(["id", "account_id", "org_id"])
173
196
  .where("id", "=", run.taskId)
174
197
  .where("deleted_at", "is", null)
@@ -180,10 +203,10 @@ export async function taskGrantsRunOf(db, runId) {
180
203
  * atomic update, so parallel calls of a run and of its sub-agents never lose or repeat a key.
181
204
  */
182
205
  // org-filter: callers pass the task run id of the kernel run's own unattended context
183
- // (PolicyContext.unattended, from runs.scheduled_task_run_id), never a caller-supplied id
206
+ // (PolicyContext.unattended, from runs.automation_run_id), never a caller-supplied id
184
207
  export async function appendMissingGrant(db, taskRunId, key) {
185
208
  await db
186
- .updateTable("scheduled_task_runs")
209
+ .updateTable("automation_runs")
187
210
  .set({
188
211
  missing_grants: sql `CASE WHEN missing_grants ? ${key} THEN missing_grants
189
212
  ELSE missing_grants || jsonb_build_array(${key}::text) END`,
@@ -208,7 +208,7 @@ export declare function headsToResolve(entries: readonly MarketplaceEntry[], imp
208
208
  /** The host versions (an entry's `minKernel` is judged against them). */
209
209
  export declare const HOST_VERSIONS: {
210
210
  readonly kernel: string;
211
- readonly sdk: "0.1.0-rc.84";
211
+ readonly sdk: "0.1.0-rc.86";
212
212
  };
213
213
  /** Whether this deployment satisfies the entry's `minKernel`. */
214
214
  export declare const entryCompatible: (e: Pick<MarketplaceEntry, "minKernel">) => boolean;
@@ -11,10 +11,12 @@ var ChannelWorkersModule_1;
11
11
  import { OnWorkerEvent, WorkerHost } from "@nestjs/bullmq";
12
12
  import { Inject, Injectable, Module } from "@nestjs/common";
13
13
  import { UnrecoverableError } from "bullmq";
14
+ import { AutomationsModule } from "../automations/automations.module.js";
14
15
  import { ConversationsModule } from "../conversations/conversations.module.js";
15
16
  import { EventLogModule } from "../conversations/event-log.js";
16
17
  import { DevicesModule } from "../devices/devices.module.js";
17
18
  import { ExtensionsModule } from "../extensions/extensions.module.js";
19
+ import { LibraryFilesModule } from "../files/library.module.js";
18
20
  import { IdentityModule } from "../identity/identity.module.js";
19
21
  import { configuredProcessor, } from "../infra/queue/configured-processor.js";
20
22
  import { QueueModule } from "../infra/queue/queue.module.js";
@@ -22,8 +24,6 @@ import { runJob } from "../infra/queue/run-job.js";
22
24
  import { OrgLockModule } from "../orgs/org-lock.js";
23
25
  import { OrgsModule } from "../orgs/orgs.module.js";
24
26
  import { RuntimeModule } from "../runtime/runtime.module.js";
25
- import { SchedulesModule } from "../schedules/schedules.module.js";
26
- import { StorageModule } from "../storage/storage.module.js";
27
27
  import { ToolsModule } from "../tools/tools.module.js";
28
28
  import { ChannelConnections } from "./connections.service.js";
29
29
  import { ChannelInbound } from "./inbound.js";
@@ -53,10 +53,11 @@ ChannelsModule = __decorate([
53
53
  ToolsModule,
54
54
  ConversationsModule,
55
55
  RuntimeModule,
56
- // outbound pauses the scheduled task of a run whose chat is gone (scheduled tasks spec §8)
57
- SchedulesModule,
58
- // outbound reads conversation files to send them into the chat (channel file delivery §4.2)
59
- StorageModule,
56
+ // outbound pauses the automation of a run whose chat is gone (scheduled tasks spec §8)
57
+ AutomationsModule,
58
+ // outbound reads conversation files from their blobs to send them into the chat (channel
59
+ // file delivery §4.2, file library spec §4)
60
+ LibraryFilesModule,
60
61
  CHANNEL_QUEUE_MODULE,
61
62
  ],
62
63
  providers: [
@@ -1,5 +1,7 @@
1
- import { type Channel, type ObjectStore } from "@capybara-hq/plugin-sdk";
1
+ import { type Channel, type ObjectHead } from "@capybara-hq/plugin-sdk";
2
2
  import type { ConversationEvent } from "../conversations/events.js";
3
+ import type { KernelSchemaDb } from "../db/kernel-schema.js";
4
+ import type { BlobRef } from "../files/blobs.service.js";
3
5
  import { type ChannelLocale } from "./phrases.js";
4
6
  type ArtifactPayload = Extract<ConversationEvent, {
5
7
  type: "artifact_created";
@@ -11,16 +13,18 @@ export type FileDeliveryPlan = {
11
13
  cap: number;
12
14
  } | {
13
15
  mode: "link";
14
- reason: "unsupported" | "disabled" | "too_large" | "empty" | "foreign_key";
16
+ reason: "unsupported" | "disabled" | "too_large" | "empty" | "unreferenced";
15
17
  };
16
18
  /**
17
- * Decides native upload or a link line for one artifact. Order: a key outside the conversation's
18
- * `artifacts/<conversationId>/` prefix, an empty file, a channel without `sendFile`, the deployment
19
- * switch (`fileMaxBytes` 0), then the size cap (the smaller of the channel's and the deployment's).
19
+ * Decides native upload or a link line for one artifact. Order: a file the conversation does not
20
+ * reference ({@link referencedBlob}: file library spec §4), an empty file, a channel without
21
+ * `sendFile`, the deployment switch (`fileMaxBytes` 0), then the size cap (the smaller of the
22
+ * channel's and the deployment's).
20
23
  */
21
24
  export declare function planFileDelivery(input: {
22
- conversationId: string;
23
- artifact: Pick<ArtifactPayload, "objectKey" | "size" | "contentType">;
25
+ /** Whether the conversation references the artifact's file (a ready file on a ready blob). */
26
+ referenced: boolean;
27
+ artifact: Pick<ArtifactPayload, "size" | "contentType">;
24
28
  features: Channel["features"];
25
29
  canSendFile: boolean;
26
30
  fileMaxBytes: number;
@@ -48,6 +52,12 @@ export declare function fileLinkText(input: {
48
52
  }): string;
49
53
  /** The root message text of a native file delivery. */
50
54
  export declare function fileRootText(name: string, locale: ChannelLocale): string;
55
+ /**
56
+ * The blob of the file `fileId` when the conversation references it (a `conversation_files` row
57
+ * in the org) and both the file and its blob are ready; null otherwise. A channel sends only
58
+ * such a file (file library spec §4 "Channel delivery").
59
+ */
60
+ export declare function referencedBlob(db: KernelSchemaDb, orgId: string, conversationId: string, fileId: string): Promise<BlobRef | null>;
51
61
  export type CappedRead = {
52
62
  kind: "ok";
53
63
  data: Uint8Array;
@@ -58,12 +68,17 @@ export type CappedRead = {
58
68
  } | {
59
69
  kind: "unavailable";
60
70
  };
71
+ /** Opens the bytes to read: null when they are gone (a blob read checks size and etag). */
72
+ export type OpenBytes = (signal: AbortSignal) => Promise<{
73
+ head: ObjectHead;
74
+ body: ReadableStream<Uint8Array>;
75
+ } | null>;
61
76
  /**
62
- * Reads an object into memory, never more than `cap` bytes: a larger head, or a body that passes
63
- * `cap` while streaming, is `too_large` (the body is cancelled). A missing object is `missing`, a
64
- * store that is not there `unavailable`; any other error is rethrown (retryable).
77
+ * Reads bytes into memory, never more than `cap` bytes: a larger head, or a body that passes
78
+ * `cap` while streaming, is `too_large` (the body is cancelled). Bytes that are gone are
79
+ * `missing`, a store that is not there `unavailable`; any other error is rethrown (retryable).
65
80
  */
66
- export declare function readArtifactCapped(store: ObjectStore, key: string, cap: number, signal: AbortSignal): Promise<CappedRead>;
81
+ export declare function readArtifactCapped(open: OpenBytes, cap: number, signal: AbortSignal): Promise<CappedRead>;
67
82
  /** A counting semaphore with a FIFO of waiters; one per worker replica (not a provider). */
68
83
  export declare class FileSlots {
69
84
  private free;
@@ -3,15 +3,15 @@ import { isStorageUnavailable } from "../storage/storage.service.js";
3
3
  import { conversationUrl } from "./cards.js";
4
4
  import { phrase } from "./phrases.js";
5
5
  /**
6
- * Decides native upload or a link line for one artifact. Order: a key outside the conversation's
7
- * `artifacts/<conversationId>/` prefix, an empty file, a channel without `sendFile`, the deployment
8
- * switch (`fileMaxBytes` 0), then the size cap (the smaller of the channel's and the deployment's).
6
+ * Decides native upload or a link line for one artifact. Order: a file the conversation does not
7
+ * reference ({@link referencedBlob}: file library spec §4), an empty file, a channel without
8
+ * `sendFile`, the deployment switch (`fileMaxBytes` 0), then the size cap (the smaller of the
9
+ * channel's and the deployment's).
9
10
  */
10
11
  export function planFileDelivery(input) {
11
12
  const { artifact, features, fileMaxBytes } = input;
12
- if (!artifact.objectKey.startsWith(`artifacts/${input.conversationId}/`)) {
13
- return { mode: "link", reason: "foreign_key" };
14
- }
13
+ if (!input.referenced)
14
+ return { mode: "link", reason: "unreferenced" };
15
15
  if (artifact.size === 0)
16
16
  return { mode: "link", reason: "empty" };
17
17
  const files = features.files;
@@ -87,13 +87,34 @@ export function fileRootText(name, locale) {
87
87
  return fill(phrase("fileRoot", locale), { name: asCode(name), size: "", url: "" });
88
88
  }
89
89
  /**
90
- * Reads an object into memory, never more than `cap` bytes: a larger head, or a body that passes
91
- * `cap` while streaming, is `too_large` (the body is cancelled). A missing object is `missing`, a
92
- * store that is not there `unavailable`; any other error is rethrown (retryable).
90
+ * The blob of the file `fileId` when the conversation references it (a `conversation_files` row
91
+ * in the org) and both the file and its blob are ready; null otherwise. A channel sends only
92
+ * such a file (file library spec §4 "Channel delivery").
93
+ */
94
+ export async function referencedBlob(db, orgId, conversationId, fileId) {
95
+ const row = await db
96
+ .selectFrom("conversation_files as cf")
97
+ .innerJoin("files as f", (j) => j.onRef("f.id", "=", "cf.file_id").onRef("f.org_id", "=", "cf.org_id"))
98
+ .innerJoin("blobs as b", (j) => j.onRef("b.id", "=", "f.blob_id").onRef("b.org_id", "=", "cf.org_id"))
99
+ .select(["b.id", "b.object_key", "b.size", "b.etag"])
100
+ .where("cf.org_id", "=", orgId)
101
+ .where("cf.conversation_id", "=", conversationId)
102
+ .where("cf.file_id", "=", fileId)
103
+ .where("f.status", "=", "ready")
104
+ .where("b.status", "=", "ready")
105
+ .executeTakeFirst();
106
+ if (!row)
107
+ return null;
108
+ return { id: row.id, objectKey: row.object_key, size: Number(row.size), etag: row.etag };
109
+ }
110
+ /**
111
+ * Reads bytes into memory, never more than `cap` bytes: a larger head, or a body that passes
112
+ * `cap` while streaming, is `too_large` (the body is cancelled). Bytes that are gone are
113
+ * `missing`, a store that is not there `unavailable`; any other error is rethrown (retryable).
93
114
  */
94
- export async function readArtifactCapped(store, key, cap, signal) {
115
+ export async function readArtifactCapped(open, cap, signal) {
95
116
  try {
96
- const got = await store.get(key, { signal });
117
+ const got = await open(signal);
97
118
  if (got === null)
98
119
  return { kind: "missing" };
99
120
  const reader = got.body.getReader();