@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.
- package/dist/admin/admin.schemas.d.ts +3 -0
- package/dist/agents/agents.schemas.d.ts +9 -9
- package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
- package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
- package/dist/automations/automations.module.d.ts +16 -0
- package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
- package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
- package/dist/automations/automations.schemas.js +253 -0
- package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
- package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
- package/dist/{schedules → automations}/calendar.d.ts +8 -8
- package/dist/{schedules → automations}/calendar.js +2 -2
- package/dist/automations/effective-settings.d.ts +28 -0
- package/dist/automations/effective-settings.js +25 -0
- package/dist/automations/errors.d.ts +19 -0
- package/dist/automations/errors.js +19 -0
- package/dist/automations/events/coalesce.d.ts +28 -0
- package/dist/automations/events/coalesce.js +131 -0
- package/dist/automations/events/deadline.d.ts +10 -0
- package/dist/automations/events/deadline.js +24 -0
- package/dist/automations/events/dispatcher.d.ts +74 -0
- package/dist/automations/events/dispatcher.js +353 -0
- package/dist/automations/events/events.module.d.ts +7 -0
- package/dist/automations/events/events.module.js +26 -0
- package/dist/automations/events/guards.d.ts +32 -0
- package/dist/automations/events/guards.js +87 -0
- package/dist/automations/events/sweep.job.d.ts +49 -0
- package/dist/automations/events/sweep.job.js +202 -0
- package/dist/automations/events/token.d.ts +5 -0
- package/dist/automations/events/token.js +5 -0
- package/dist/automations/events/trigger-events.service.d.ts +47 -0
- package/dist/automations/events/trigger-events.service.js +254 -0
- package/dist/{schedules → automations}/fire.d.ts +57 -17
- package/dist/{schedules → automations}/fire.js +311 -79
- package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
- package/dist/{schedules → automations}/grant-rules.js +5 -5
- package/dist/automations/job-ids.d.ts +113 -0
- package/dist/automations/job-ids.js +117 -0
- package/dist/{schedules → automations}/pause.d.ts +3 -3
- package/dist/{schedules → automations}/pause.js +3 -3
- package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
- package/dist/{schedules → automations}/run-tracker.js +68 -56
- package/dist/{schedules → automations}/scan.job.d.ts +12 -8
- package/dist/{schedules → automations}/scan.job.js +39 -23
- package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
- package/dist/{schedules → automations}/schedule-math.js +17 -7
- package/dist/{schedules → automations}/store.d.ts +57 -27
- package/dist/{schedules → automations}/store.js +102 -52
- package/dist/automations/store.module.d.ts +6 -0
- package/dist/{schedules → automations}/store.module.js +9 -9
- package/dist/automations/types.d.ts +178 -0
- package/dist/{schedules → automations}/unattended.d.ts +22 -11
- package/dist/{schedules → automations}/unattended.js +53 -30
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +7 -6
- package/dist/channels/file-delivery.d.ts +26 -11
- package/dist/channels/file-delivery.js +32 -11
- package/dist/channels/outbound.d.ts +8 -8
- package/dist/channels/outbound.js +32 -30
- package/dist/channels/threads.d.ts +1 -1
- package/dist/channels/threads.js +1 -1
- package/dist/config/schema.d.ts +8 -1
- package/dist/config/schema.js +56 -6
- package/dist/conversations/approval-profile.d.ts +2 -2
- package/dist/conversations/approval-profile.js +3 -3
- package/dist/conversations/artifacts.d.ts +20 -17
- package/dist/conversations/artifacts.js +30 -37
- package/dist/conversations/attachments.controller.d.ts +6 -19
- package/dist/conversations/attachments.controller.js +8 -59
- package/dist/conversations/attachments.service.d.ts +30 -74
- package/dist/conversations/attachments.service.js +51 -193
- package/dist/conversations/conversations.controller.d.ts +7 -1
- package/dist/conversations/conversations.controller.js +26 -10
- package/dist/conversations/conversations.module.js +4 -0
- package/dist/conversations/conversations.service.d.ts +112 -35
- package/dist/conversations/conversations.service.js +160 -43
- package/dist/conversations/event-log.d.ts +1 -0
- package/dist/conversations/events.d.ts +95 -27
- package/dist/conversations/events.js +108 -10
- package/dist/conversations/files.d.ts +66 -64
- package/dist/conversations/files.js +127 -147
- package/dist/conversations/message-context.d.ts +2 -2
- package/dist/conversations/message-context.js +1 -1
- package/dist/conversations/permission-profile-store.d.ts +2 -1
- package/dist/conversations/permission-profiles.controller.d.ts +3 -0
- package/dist/conversations/permission-profiles.service.js +1 -1
- package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
- package/dist/conversations/prompt-suggestion-request.js +4 -2
- package/dist/conversations/prompt-suggestions.d.ts +3 -3
- package/dist/conversations/prompt-suggestions.js +11 -10
- package/dist/conversations/queue.d.ts +7 -4
- package/dist/conversations/queue.js +6 -2
- package/dist/conversations/queue.service.d.ts +5 -5
- package/dist/conversations/queue.service.js +27 -16
- package/dist/ctx/create-ctx.d.ts +23 -2
- package/dist/ctx/create-ctx.js +78 -3
- package/dist/db/kernel-schema.d.ts +184 -27
- package/dist/devices/device-project-files.service.d.ts +88 -0
- package/dist/devices/device-project-files.service.js +249 -0
- package/dist/devices/device-projects.service.d.ts +40 -0
- package/dist/devices/device-projects.service.js +16 -13
- package/dist/devices/devices.controller.d.ts +9 -3
- package/dist/devices/devices.controller.js +27 -5
- package/dist/devices/devices.module.js +9 -1
- package/dist/devices/devices.schemas.js +6 -1
- package/dist/devices/dto.d.ts +15 -0
- package/dist/devices/dto.js +11 -0
- package/dist/devices/project-paths.d.ts +29 -0
- package/dist/devices/project-paths.js +66 -0
- package/dist/devices/project-rules.d.ts +5 -1
- package/dist/devices/project-rules.js +5 -1
- package/dist/errors.d.ts +53 -13
- package/dist/errors.js +53 -13
- package/dist/evals/canonical-json.d.ts +7 -0
- package/dist/evals/canonical-json.js +21 -0
- package/dist/evals/reconstruct.d.ts +10 -1
- package/dist/evals/reconstruct.js +20 -1
- package/dist/evals/recorded-tools.d.ts +0 -2
- package/dist/evals/recorded-tools.js +1 -16
- package/dist/evals/snapshot.d.ts +26 -3
- package/dist/evals/snapshot.js +110 -57
- package/dist/evals/worker.d.ts +4 -3
- package/dist/evals/worker.js +8 -4
- package/dist/extensions/host.d.ts +9 -2
- package/dist/extensions/host.js +12 -0
- package/dist/extensions/instance.d.ts +3 -1
- package/dist/extensions/instance.js +4 -0
- package/dist/extensions/ledger.d.ts +1 -1
- package/dist/extensions/permission-set.d.ts +17 -1
- package/dist/extensions/permission-set.js +13 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/files/blob-objects.d.ts +124 -0
- package/dist/files/blob-objects.js +341 -0
- package/dist/files/blob-sweep.job.d.ts +122 -0
- package/dist/files/blob-sweep.job.js +389 -0
- package/dist/files/blobs.service.d.ts +135 -0
- package/dist/files/blobs.service.js +300 -0
- package/dist/files/conversation-media.d.ts +41 -0
- package/dist/files/conversation-media.js +124 -0
- package/dist/files/deliveries.d.ts +64 -0
- package/dist/files/deliveries.js +156 -0
- package/dist/files/deliveries.module.d.ts +6 -0
- package/dist/files/deliveries.module.js +24 -0
- package/dist/files/draft-cleanup.job.d.ts +26 -0
- package/dist/files/draft-cleanup.job.js +93 -0
- package/dist/files/files.module.d.ts +1 -1
- package/dist/files/files.module.js +3 -2
- package/dist/files/hash-job.d.ts +127 -0
- package/dist/files/hash-job.js +447 -0
- package/dist/files/library-backfill.d.ts +63 -0
- package/dist/files/library-backfill.js +143 -0
- package/dist/files/library-files.service.d.ts +255 -0
- package/dist/files/library-files.service.js +883 -0
- package/dist/files/library-jobs.module.d.ts +8 -0
- package/dist/files/library-jobs.module.js +27 -0
- package/dist/files/library-query.d.ts +127 -0
- package/dist/files/library-query.js +322 -0
- package/dist/files/library-read.module.d.ts +9 -0
- package/dist/files/library-read.module.js +30 -0
- package/dist/files/library.controller.d.ts +77 -0
- package/dist/files/library.controller.js +235 -0
- package/dist/files/library.module.d.ts +9 -0
- package/dist/files/library.module.js +29 -0
- package/dist/files/library.schemas.d.ts +46 -0
- package/dist/files/library.schemas.js +49 -0
- package/dist/files/previewers.controller.d.ts +20 -0
- package/dist/files/previewers.controller.js +47 -0
- package/dist/files/previewers.d.ts +25 -11
- package/dist/files/previewers.js +43 -27
- package/dist/files/referenced-files.d.ts +150 -0
- package/dist/files/referenced-files.js +234 -0
- package/dist/git/gateway.js +2 -2
- package/dist/html/html-document.controller.d.ts +8 -1
- package/dist/html/html-document.controller.js +19 -1
- package/dist/html/html-document.service.d.ts +19 -4
- package/dist/html/html-document.service.js +43 -22
- package/dist/html/html-token.d.ts +10 -1
- package/dist/html/html-token.js +14 -0
- package/dist/html/html.module.js +8 -1
- package/dist/http/api.module.js +10 -5
- package/dist/http/request-signal.d.ts +8 -0
- package/dist/http/request-signal.js +14 -0
- package/dist/identity/settings.d.ts +26 -0
- package/dist/identity/settings.js +33 -2
- package/dist/inbox/inbox.module.js +2 -2
- package/dist/inbox/inbox.service.d.ts +8 -8
- package/dist/inbox/inbox.service.js +7 -7
- package/dist/infra/config/kernel-config.service.js +10 -5
- package/dist/infra/outbox/registry.d.ts +111 -2
- package/dist/infra/outbox/registry.js +104 -9
- package/dist/infra/throttler/named-throttlers.d.ts +2 -0
- package/dist/infra/throttler/named-throttlers.js +14 -0
- package/dist/kernel.module.js +3 -0
- package/dist/lifecycle/kernel-lifecycle.js +6 -0
- package/dist/models/purposes.js +1 -1
- package/dist/orgs/offboarding.js +1 -1
- package/dist/orgs/roles.d.ts +1 -1
- package/dist/orgs/roles.js +3 -3
- package/dist/orgs/settings.controller.d.ts +1 -1
- package/dist/orgs/settings.service.d.ts +75 -13
- package/dist/orgs/settings.service.js +104 -26
- package/dist/platform/exclusions.js +45 -8
- package/dist/platform/file-ref.d.ts +18 -3
- package/dist/platform/file-ref.js +22 -11
- package/dist/platform/operation.js +6 -2
- package/dist/platform/ops/admin.ops.js +5 -2
- package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
- package/dist/platform/ops/automations.ops.js +560 -0
- package/dist/platform/ops/devices.ops.d.ts +3 -1
- package/dist/platform/ops/devices.ops.js +77 -3
- package/dist/platform/ops/files.ops.d.ts +18 -0
- package/dist/platform/ops/files.ops.js +267 -0
- package/dist/platform/ops/inbox.ops.js +4 -4
- package/dist/platform/ops/org-settings.ops.js +6 -2
- package/dist/platform/ops/orgs.ops.js +2 -1
- package/dist/platform/ops/platform.ops.js +4 -4
- package/dist/platform/ops/shares.ops.js +2 -2
- package/dist/platform/platform-ops.module.d.ts +3 -2
- package/dist/platform/platform-ops.module.js +10 -5
- package/dist/platform/route-schemas.js +6 -3
- package/dist/push/jobs.d.ts +3 -3
- package/dist/push/jobs.js +2 -2
- package/dist/push/preferences.service.js +1 -1
- package/dist/push/push-owed.d.ts +3 -3
- package/dist/push/push-owed.js +6 -6
- package/dist/push/push.schemas.d.ts +4 -4
- package/dist/push/push.schemas.js +3 -3
- package/dist/push/recipients.d.ts +4 -4
- package/dist/push/recipients.js +23 -23
- package/dist/push/texts.d.ts +3 -3
- package/dist/push/texts.js +2 -2
- package/dist/push/triggers.d.ts +1 -1
- package/dist/roles/worker.module.js +18 -4
- package/dist/runtime/compaction-settings.service.d.ts +13 -1
- package/dist/runtime/compaction-settings.service.js +16 -1
- package/dist/runtime/compaction.service.d.ts +3 -1
- package/dist/runtime/compaction.service.js +3 -1
- package/dist/runtime/context/build.d.ts +24 -1
- package/dist/runtime/context/build.js +33 -11
- package/dist/runtime/context/cache-expiry.d.ts +31 -0
- package/dist/runtime/context/cache-expiry.js +60 -0
- package/dist/runtime/context/cache-ttl.d.ts +18 -0
- package/dist/runtime/context/cache-ttl.js +30 -0
- package/dist/runtime/context/compaction.d.ts +9 -2
- package/dist/runtime/context/compaction.js +64 -21
- package/dist/runtime/context/in-context-compaction.d.ts +156 -0
- package/dist/runtime/context/in-context-compaction.js +310 -0
- package/dist/runtime/context/run-context.d.ts +38 -0
- package/dist/runtime/context/run-context.js +54 -0
- package/dist/runtime/hook-calls.d.ts +2 -1
- package/dist/runtime/hook-calls.js +5 -2
- package/dist/runtime/maintenance.d.ts +16 -4
- package/dist/runtime/maintenance.js +79 -18
- package/dist/runtime/model-call.d.ts +34 -5
- package/dist/runtime/model-call.js +207 -22
- package/dist/runtime/providers.js +11 -1
- package/dist/runtime/run-finish.d.ts +4 -4
- package/dist/runtime/run-finish.js +3 -3
- package/dist/runtime/run-queue.js +12 -7
- package/dist/runtime/run-steps.d.ts +4 -2
- package/dist/runtime/run-steps.js +10 -5
- package/dist/runtime/run-worker.js +7 -1
- package/dist/runtime/runtime.module.js +6 -3
- package/dist/runtime/subagents/dispatch.js +11 -3
- package/dist/sandbox/browse-roots.d.ts +8 -0
- package/dist/sandbox/browse-roots.js +22 -0
- package/dist/sandbox/network-grants.d.ts +4 -4
- package/dist/sandbox/network-grants.js +7 -7
- package/dist/sandbox/operation-authority.d.ts +10 -3
- package/dist/sandbox/operation-authority.js +20 -10
- package/dist/sandbox/operation-intents.d.ts +3 -1
- package/dist/sandbox/operation-intents.js +11 -4
- package/dist/sandbox/permission-call-grants.js +2 -2
- package/dist/sandbox/permission-facts.js +1 -1
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +22 -11
- package/dist/sandbox/review/permission-candidate.d.ts +1 -1
- package/dist/sandbox/review/permission-candidate.js +1 -1
- package/dist/sandbox/sandbox.service.d.ts +15 -5
- package/dist/sandbox/sandbox.service.js +14 -8
- package/dist/shares/liveness.d.ts +26 -12
- package/dist/shares/liveness.js +79 -20
- package/dist/shares/revoke.d.ts +16 -11
- package/dist/shares/revoke.js +18 -14
- package/dist/shares/share-objects.d.ts +16 -21
- package/dist/shares/share-objects.js +25 -55
- package/dist/shares/share-open.service.d.ts +8 -4
- package/dist/shares/share-open.service.js +14 -11
- package/dist/shares/shares.controller.d.ts +16 -5
- package/dist/shares/shares.controller.js +39 -5
- package/dist/shares/shares.module.d.ts +4 -4
- package/dist/shares/shares.module.js +7 -6
- package/dist/shares/shares.schemas.d.ts +4 -0
- package/dist/shares/shares.schemas.js +2 -0
- package/dist/shares/shares.service.d.ts +93 -54
- package/dist/shares/shares.service.js +292 -131
- package/dist/shares/sweep.job.d.ts +18 -39
- package/dist/shares/sweep.job.js +47 -206
- package/dist/storage/purge.d.ts +1 -1
- package/dist/storage/purge.js +2 -2
- package/dist/tools/approvals.service.d.ts +6 -6
- package/dist/tools/approvals.service.js +19 -19
- package/dist/tools/builtin-names.d.ts +12 -12
- package/dist/tools/builtin-names.js +29 -29
- package/dist/tools/builtins/artifacts.d.ts +21 -13
- package/dist/tools/builtins/artifacts.js +81 -51
- package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
- package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
- package/dist/tools/builtins/builtin-tools.d.ts +15 -9
- package/dist/tools/builtins/builtin-tools.js +33 -22
- package/dist/tools/builtins/conversation-files.d.ts +23 -13
- package/dist/tools/builtins/conversation-files.js +62 -83
- package/dist/tools/builtins/image/generate.d.ts +7 -1
- package/dist/tools/builtins/image/generate.js +1 -1
- package/dist/tools/builtins/platform.js +1 -0
- package/dist/tools/builtins/sandbox/common.d.ts +6 -2
- package/dist/tools/builtins/sandbox/deliver.js +6 -7
- package/dist/tools/builtins/sandbox/fetch-attachment.d.ts +6 -5
- package/dist/tools/builtins/sandbox/fetch-attachment.js +19 -12
- package/dist/tools/call-scope.d.ts +1 -1
- package/dist/tools/git-asks.js +1 -1
- package/dist/tools/git-push-policy.d.ts +3 -3
- package/dist/tools/git-push-policy.js +6 -6
- package/dist/tools/network-asks.d.ts +3 -3
- package/dist/tools/network-asks.js +3 -3
- package/dist/tools/pipeline.d.ts +4 -4
- package/dist/tools/pipeline.js +2 -2
- package/dist/tools/policy.d.ts +3 -3
- package/dist/tools/policy.js +3 -3
- package/dist/tools/tools.module.js +15 -9
- package/dist/ui/call-extension.d.ts +6 -4
- package/dist/ui/call-extension.js +5 -4
- package/dist/ui/embed-token.js +6 -3
- package/dist/ui/registry.d.ts +21 -5
- package/dist/ui/registry.js +24 -6
- package/dist/ui/ui.schemas.d.ts +22 -0
- package/dist/ui/ui.schemas.js +22 -4
- package/dist/ui/views.controller.d.ts +13 -3
- package/dist/ui/views.controller.js +33 -2
- package/dist/ui/views.d.ts +29 -1
- package/dist/ui/views.js +131 -7
- package/package.json +3 -3
- package/src/migrations/0188_automations_rename.sql +309 -0
- package/src/migrations/0189_event_automations.sql +136 -0
- package/src/migrations/0190_file_library.sql +285 -0
- package/src/migrations/0191_file_library_copy.sql +4 -0
- package/dist/platform/ops/schedules.ops.js +0 -509
- package/dist/schedules/effective-settings.d.ts +0 -22
- package/dist/schedules/effective-settings.js +0 -18
- package/dist/schedules/errors.d.ts +0 -8
- package/dist/schedules/errors.js +0 -8
- package/dist/schedules/job-ids.d.ts +0 -81
- package/dist/schedules/job-ids.js +0 -95
- package/dist/schedules/schedules.module.d.ts +0 -16
- package/dist/schedules/schedules.schemas.js +0 -213
- package/dist/schedules/store.module.d.ts +0 -6
- package/dist/schedules/types.d.ts +0 -110
- /package/dist/{schedules → automations}/types.js +0 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
12
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
13
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
14
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
15
|
+
import { XactLock } from "../infra/database/xact-lock.js";
|
|
16
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
17
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
18
|
+
import { StorageService } from "../storage/storage.service.js";
|
|
19
|
+
import { blobObjectKey, deferred, oweBlobDelete } from "./blob-objects.js";
|
|
20
|
+
export { blobObjectKey, stagingObjectKey } from "./blob-objects.js";
|
|
21
|
+
/**
|
|
22
|
+
* The longest a kernel put of blob bytes may take (ms): well under the hour after which the blob
|
|
23
|
+
* sweep treats a `pending` blob with no file as abandoned.
|
|
24
|
+
*/
|
|
25
|
+
const PUT_TIMEOUT_MS = 10 * 60_000;
|
|
26
|
+
const BLOB_COLUMNS = ["id", "org_id", "sha256", "size", "etag", "object_key", "status"];
|
|
27
|
+
const toBlob = (r) => ({
|
|
28
|
+
id: r.id,
|
|
29
|
+
orgId: r.org_id,
|
|
30
|
+
sha256: r.sha256,
|
|
31
|
+
size: Number(r.size),
|
|
32
|
+
etag: r.etag,
|
|
33
|
+
objectKey: r.object_key,
|
|
34
|
+
status: r.status,
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Whether a stored object is still the blob's recorded bytes: the same size, and the same etag
|
|
38
|
+
* when one was recorded (migrated blobs have none: size only). Anything else is treated as gone.
|
|
39
|
+
*/
|
|
40
|
+
export function blobContentMatches(recorded, head) {
|
|
41
|
+
return head.size === recorded.size && (recorded.etag === null || recorded.etag === head.etag);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The lock of one hash in one org (decision R9: one provider per transaction-scoped lock):
|
|
45
|
+
* serializes the step that decides which blob holds a sha256 ({@link BlobsService.settle}), so two
|
|
46
|
+
* writes of the same bytes never race on the `(org_id, sha256)` unique index. Joins the caller's
|
|
47
|
+
* transaction, which holds it until it ends.
|
|
48
|
+
*/
|
|
49
|
+
let BlobHashLock = class BlobHashLock {
|
|
50
|
+
xactLock;
|
|
51
|
+
txHost;
|
|
52
|
+
constructor(xactLock, txHost) {
|
|
53
|
+
this.xactLock = xactLock;
|
|
54
|
+
this.txHost = txHost;
|
|
55
|
+
}
|
|
56
|
+
async lock(orgId, sha256) {
|
|
57
|
+
await this.xactLock.lock(`capybara.blob-hash:${orgId}:${sha256}`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
__decorate([
|
|
61
|
+
Transactional({ propagation: "mandatory" })
|
|
62
|
+
], BlobHashLock.prototype, "lock", null);
|
|
63
|
+
BlobHashLock = __decorate([
|
|
64
|
+
Injectable(),
|
|
65
|
+
__param(0, Inject(XactLock)),
|
|
66
|
+
__param(1, Inject(TransactionHost))
|
|
67
|
+
], BlobHashLock);
|
|
68
|
+
export { BlobHashLock };
|
|
69
|
+
/**
|
|
70
|
+
* The bytes of the file library (file library spec §1, §3 with its builder amendment, §4): per-org
|
|
71
|
+
* blobs keyed by sha256, every one written by the kernel itself to `blobs/<blobId>`.
|
|
72
|
+
*
|
|
73
|
+
* - {@link writeIntent} commits a `pending` blob (no hash yet) before the kernel puts verified
|
|
74
|
+
* bytes at its key, so a crash leaves a row the sweep finds.
|
|
75
|
+
* - {@link settle}, in the caller's transaction under the hash's lock, decides which blob holds
|
|
76
|
+
* the hash: an existing `ready` one (the new copy becomes `deleting`, its deletion owed) or the
|
|
77
|
+
* new one (`ready`, with the hash). An upload and a delivery do the same work either way.
|
|
78
|
+
* - {@link lockReady} is the one gate for a new reference: every transaction that inserts or moves
|
|
79
|
+
* a reference to a blob (a file row, a share, a repoint) calls it first, so a blob a deletion
|
|
80
|
+
* claims is never pointed at again (`BlobObjects.deleteBlob`).
|
|
81
|
+
* - Reads ({@link open}, {@link head}) check size and etag against the row.
|
|
82
|
+
*
|
|
83
|
+
* No upload form can write a blob's key, so no read depends on who uploaded what. No process state.
|
|
84
|
+
*/
|
|
85
|
+
let BlobsService = class BlobsService {
|
|
86
|
+
db;
|
|
87
|
+
txHost;
|
|
88
|
+
storage;
|
|
89
|
+
outbox;
|
|
90
|
+
hashLock;
|
|
91
|
+
log;
|
|
92
|
+
constructor(db, txHost, storage, outbox, hashLock, log) {
|
|
93
|
+
this.db = db;
|
|
94
|
+
this.txHost = txHost;
|
|
95
|
+
this.storage = storage;
|
|
96
|
+
this.outbox = outbox;
|
|
97
|
+
this.hashLock = hashLock;
|
|
98
|
+
this.log = log;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Bytes the kernel holds (a delivery): a `ready` blob of this hash in the org is returned as it
|
|
102
|
+
* is (every ready blob was written or checked by the kernel); else the bytes are written to a new
|
|
103
|
+
* blob ({@link writeIntent}, the put) and {@link settle}d in a transaction of their own. The
|
|
104
|
+
* returned blob is not locked: a caller that then references it must call {@link lockReady} in
|
|
105
|
+
* the transaction that inserts the reference and, on null (claimed by a deletion meanwhile), call
|
|
106
|
+
* this again.
|
|
107
|
+
*/
|
|
108
|
+
async ensureFromBytes(orgId, sha256, bytes, opts) {
|
|
109
|
+
const existing = await this.db
|
|
110
|
+
.selectFrom("blobs")
|
|
111
|
+
.select(BLOB_COLUMNS)
|
|
112
|
+
.where("org_id", "=", orgId)
|
|
113
|
+
.where("sha256", "=", sha256)
|
|
114
|
+
.where("status", "=", "ready")
|
|
115
|
+
.executeTakeFirst();
|
|
116
|
+
if (existing)
|
|
117
|
+
return toBlob(existing);
|
|
118
|
+
const own = await this.writeVerified(orgId, bytes, opts);
|
|
119
|
+
return this.settleAlone(orgId, own.id, sha256);
|
|
120
|
+
}
|
|
121
|
+
/** {@link settle} in a transaction of its own, never the caller's. */
|
|
122
|
+
async settleAlone(orgId, blobId, sha256) {
|
|
123
|
+
return this.settle(orgId, blobId, sha256);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Writes bytes the kernel verified to a new blob: the `pending` row first, committed on its own
|
|
127
|
+
* (the intent), then the put at `blobs/<blobId>`; the row records the stored size and the etag
|
|
128
|
+
* the store reported. Returns the still `pending` blob for {@link settle}. A failed put throws
|
|
129
|
+
* and leaves the row for the sweep (`pending` with no file).
|
|
130
|
+
*/
|
|
131
|
+
async writeVerified(orgId, bytes, opts) {
|
|
132
|
+
const intent = await this.writeIntent(orgId, bytes.byteLength);
|
|
133
|
+
const timeout = AbortSignal.timeout(PUT_TIMEOUT_MS);
|
|
134
|
+
const head = await this.storage.kernel(orgId).put(intent.objectKey, bytes, {
|
|
135
|
+
contentType: opts.contentType || "application/octet-stream",
|
|
136
|
+
size: bytes.byteLength,
|
|
137
|
+
signal: opts.signal ? AbortSignal.any([opts.signal, timeout]) : timeout,
|
|
138
|
+
});
|
|
139
|
+
return this.recordStored(orgId, intent.id, head.size, head.etag ?? null);
|
|
140
|
+
}
|
|
141
|
+
/** The `pending` intent of {@link writeVerified}, committed before the put. */
|
|
142
|
+
async writeIntent(orgId, size) {
|
|
143
|
+
const id = randomUUID();
|
|
144
|
+
const row = await this.db.db
|
|
145
|
+
.insertInto("blobs")
|
|
146
|
+
.values({
|
|
147
|
+
id,
|
|
148
|
+
org_id: orgId,
|
|
149
|
+
sha256: null,
|
|
150
|
+
size,
|
|
151
|
+
etag: null,
|
|
152
|
+
object_key: blobObjectKey(id),
|
|
153
|
+
status: "pending",
|
|
154
|
+
})
|
|
155
|
+
.returning(BLOB_COLUMNS)
|
|
156
|
+
.executeTakeFirstOrThrow();
|
|
157
|
+
return toBlob(row);
|
|
158
|
+
}
|
|
159
|
+
/** The stored size and etag of a `pending` blob after its put. */
|
|
160
|
+
async recordStored(orgId, id, size, etag) {
|
|
161
|
+
const row = await this.db.db
|
|
162
|
+
.updateTable("blobs")
|
|
163
|
+
.set({ size, etag })
|
|
164
|
+
.where("id", "=", id)
|
|
165
|
+
.where("org_id", "=", orgId)
|
|
166
|
+
.where("status", "=", "pending")
|
|
167
|
+
.returning(BLOB_COLUMNS)
|
|
168
|
+
.executeTakeFirst();
|
|
169
|
+
if (!row)
|
|
170
|
+
throw new Error(`blob ${id} is no longer pending`);
|
|
171
|
+
return toBlob(row);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Decides, in the caller's transaction and under the hash's lock, which blob holds `sha256` for
|
|
175
|
+
* the `pending` blob `blobId` the kernel just wrote: an existing `ready` blob of that hash is
|
|
176
|
+
* locked ({@link lockReady}) and returned, and the new blob becomes `deleting` with its deletion
|
|
177
|
+
* owed; otherwise the new blob becomes `ready` with the hash. A blob of that hash that is no longer
|
|
178
|
+
* `ready` (being deleted, gone) gives the hash up first. The caller points its reference at the
|
|
179
|
+
* returned blob in the same transaction.
|
|
180
|
+
*/
|
|
181
|
+
async settle(orgId, blobId, sha256) {
|
|
182
|
+
const trx = this.db.db;
|
|
183
|
+
await this.hashLock.lock(orgId, sha256);
|
|
184
|
+
const holder = await trx
|
|
185
|
+
.selectFrom("blobs")
|
|
186
|
+
.select("id")
|
|
187
|
+
.where("org_id", "=", orgId)
|
|
188
|
+
.where("sha256", "=", sha256)
|
|
189
|
+
.where("status", "=", "ready")
|
|
190
|
+
.executeTakeFirst();
|
|
191
|
+
const existing = holder ? await this.lockReady(orgId, holder.id) : null;
|
|
192
|
+
if (existing) {
|
|
193
|
+
await trx
|
|
194
|
+
.updateTable("blobs")
|
|
195
|
+
.set({ status: "deleting" })
|
|
196
|
+
.where("id", "=", blobId)
|
|
197
|
+
.where("org_id", "=", orgId)
|
|
198
|
+
.where("status", "=", "pending")
|
|
199
|
+
.execute();
|
|
200
|
+
// deferred: never due within the caller's request (no fast path runs while it waits)
|
|
201
|
+
await oweBlobDelete(this.outbox, orgId, blobId, deferred());
|
|
202
|
+
return existing;
|
|
203
|
+
}
|
|
204
|
+
await trx
|
|
205
|
+
.updateTable("blobs")
|
|
206
|
+
.set({ sha256: null })
|
|
207
|
+
.where("org_id", "=", orgId)
|
|
208
|
+
.where("sha256", "=", sha256)
|
|
209
|
+
.where("status", "<>", "ready")
|
|
210
|
+
.execute();
|
|
211
|
+
const own = await trx
|
|
212
|
+
.updateTable("blobs")
|
|
213
|
+
.set({ status: "ready", sha256 })
|
|
214
|
+
.where("id", "=", blobId)
|
|
215
|
+
.where("org_id", "=", orgId)
|
|
216
|
+
.where("status", "=", "pending")
|
|
217
|
+
.returning(BLOB_COLUMNS)
|
|
218
|
+
.executeTakeFirst();
|
|
219
|
+
if (!own)
|
|
220
|
+
throw new Error(`blob ${blobId} is no longer pending`);
|
|
221
|
+
return toBlob(own);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The gate of every new reference to a blob, in the caller's transaction: locks the blob row
|
|
225
|
+
* `FOR SHARE` and returns it only while it is `ready` (else null: being deleted, gone, pending or
|
|
226
|
+
* not in the org). A deletion claims the row `FOR UPDATE`, so it either waits for the caller's
|
|
227
|
+
* reference (and then sees it) or has already made the blob `deleting` (and this refuses it).
|
|
228
|
+
*/
|
|
229
|
+
async lockReady(orgId, blobId) {
|
|
230
|
+
const row = await this.db.db
|
|
231
|
+
.selectFrom("blobs")
|
|
232
|
+
.select(BLOB_COLUMNS)
|
|
233
|
+
.where("id", "=", blobId)
|
|
234
|
+
.where("org_id", "=", orgId)
|
|
235
|
+
.where("status", "=", "ready")
|
|
236
|
+
.forShare()
|
|
237
|
+
.executeTakeFirst();
|
|
238
|
+
return row ? toBlob(row) : null;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Opens a blob's bytes (whole, or a range): null when certainly gone (the object missing, or its
|
|
242
|
+
* size or etag not the recorded one: content replaced, logged as a warning). Every other error
|
|
243
|
+
* (storage unavailable, network) throws. The caller reads or cancels `body`.
|
|
244
|
+
*/
|
|
245
|
+
async open(orgId, blob, opts = {}) {
|
|
246
|
+
const got = await this.storage.kernel(orgId).get(blob.objectKey, {
|
|
247
|
+
...(opts.range !== undefined && { range: opts.range }),
|
|
248
|
+
...(opts.signal !== undefined && { signal: opts.signal }),
|
|
249
|
+
});
|
|
250
|
+
if (!got)
|
|
251
|
+
return null;
|
|
252
|
+
if (!this.matches(orgId, blob, got.head)) {
|
|
253
|
+
await got.body.cancel().catch(() => { });
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
return got;
|
|
257
|
+
}
|
|
258
|
+
/** The blob object's head when it is still the recorded bytes; null when gone or replaced. */
|
|
259
|
+
async head(orgId, blob) {
|
|
260
|
+
const head = await this.storage.kernel(orgId).head(blob.objectKey);
|
|
261
|
+
if (!head)
|
|
262
|
+
return null;
|
|
263
|
+
return this.matches(orgId, blob, head) ? head : null;
|
|
264
|
+
}
|
|
265
|
+
/** {@link blobContentMatches}, logging a replaced object. */
|
|
266
|
+
matches(orgId, blob, head) {
|
|
267
|
+
if (blobContentMatches(blob, head))
|
|
268
|
+
return true;
|
|
269
|
+
this.log.warn("blob content changed after it was recorded; treating it as unavailable", {
|
|
270
|
+
orgId,
|
|
271
|
+
blobId: blob.id,
|
|
272
|
+
});
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
__decorate([
|
|
277
|
+
Transactional({ propagation: "requires_new" })
|
|
278
|
+
], BlobsService.prototype, "settleAlone", null);
|
|
279
|
+
__decorate([
|
|
280
|
+
Transactional({ propagation: "requires_new" })
|
|
281
|
+
], BlobsService.prototype, "writeIntent", null);
|
|
282
|
+
__decorate([
|
|
283
|
+
Transactional()
|
|
284
|
+
], BlobsService.prototype, "recordStored", null);
|
|
285
|
+
__decorate([
|
|
286
|
+
Transactional({ propagation: "mandatory" })
|
|
287
|
+
], BlobsService.prototype, "settle", null);
|
|
288
|
+
__decorate([
|
|
289
|
+
Transactional({ propagation: "mandatory" })
|
|
290
|
+
], BlobsService.prototype, "lockReady", null);
|
|
291
|
+
BlobsService = __decorate([
|
|
292
|
+
Injectable(),
|
|
293
|
+
__param(0, Inject(KernelDb)),
|
|
294
|
+
__param(1, Inject(TransactionHost)),
|
|
295
|
+
__param(2, Inject(StorageService)),
|
|
296
|
+
__param(3, Inject(OutboxService)),
|
|
297
|
+
__param(4, Inject(BlobHashLock)),
|
|
298
|
+
__param(5, Inject(KERNEL_LOGGER))
|
|
299
|
+
], BlobsService);
|
|
300
|
+
export { BlobsService };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { AttachmentsService } from "../conversations/attachments.service.js";
|
|
2
|
+
import { CacheService } from "../infra/cache/cache.service.js";
|
|
3
|
+
import type { MediaBytes, MediaInfo } from "../runtime/context/build.js";
|
|
4
|
+
import { ReferencedFiles } from "./referenced-files.js";
|
|
5
|
+
/**
|
|
6
|
+
* The media request building resolves a conversation's refs with (file library spec §5): a ref id
|
|
7
|
+
* is a library file the conversation refers to (`conversation_files` → `files` → `blobs`, read
|
|
8
|
+
* through `BlobsService`), else a `read` copy bound to it (an image a tool showed the agent:
|
|
9
|
+
* `AttachmentsService`). The contract is the attachments' one: an id with neither is left out of
|
|
10
|
+
* {@link mediaInfo} (request building renders the "no longer available" placeholder), and
|
|
11
|
+
* {@link fetchMedia} returns null only when the bytes are certainly gone (a deleted file, a gone
|
|
12
|
+
* blob, a replaced object) and throws on every other error (Ruling M4). No process state.
|
|
13
|
+
*/
|
|
14
|
+
export declare class ConversationMedia {
|
|
15
|
+
private readonly files;
|
|
16
|
+
private readonly attachments;
|
|
17
|
+
/** where the model-sized copies of images are kept between steps ({@link fetchMedia}) */
|
|
18
|
+
private readonly cache;
|
|
19
|
+
constructor(files: Pick<ReferencedFiles, "referenced" | "open" | "head" | "modelRef">, attachments: Pick<AttachmentsService, "mediaInfo" | "fetchMedia">,
|
|
20
|
+
/** where the model-sized copies of images are kept between steps ({@link fetchMedia}) */
|
|
21
|
+
cache: Pick<CacheService, "get" | "set">);
|
|
22
|
+
/** The live files of the conversation among `ids`, by id. */
|
|
23
|
+
private filesOf;
|
|
24
|
+
/**
|
|
25
|
+
* The recorded facts request building needs about the refs (size, PDF page count, etag and the
|
|
26
|
+
* unsigned `uri` of the bytes): for a file, the blob's size and the pinned reference
|
|
27
|
+
* (`ReferencedFiles.modelRef`); for a `read` copy, the row's.
|
|
28
|
+
* An id that is neither is left out; a failed query throws.
|
|
29
|
+
*/
|
|
30
|
+
mediaInfo(orgId: string, conversationId: string, ids: readonly string[]): Promise<Map<string, MediaInfo>>;
|
|
31
|
+
/**
|
|
32
|
+
* The bytes request building sends for the refs. A file's bytes come from its blob (size and
|
|
33
|
+
* etag checked); a `read` copy's from its row. Null only when certainly gone. An id in `caps` is
|
|
34
|
+
* read by range up to that many leading bytes (the `text/*` cap). A whole image comes back as
|
|
35
|
+
* its model-sized copy ({@link fitImageForModel}), cached for {@link MODEL_IMAGE_CACHE_SECONDS}
|
|
36
|
+
* under the id and the recorded etag (or size), so later steps only check the object's head.
|
|
37
|
+
*/
|
|
38
|
+
fetchMedia(orgId: string, conversationId: string, ids: readonly string[], caps?: ReadonlyMap<string, number>): Promise<Map<string, MediaBytes | null>>;
|
|
39
|
+
/** One file's bytes for the model (see {@link fetchMedia}). */
|
|
40
|
+
private fileBytes;
|
|
41
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { AttachmentsService } from "../conversations/attachments.service.js";
|
|
12
|
+
import { isImageContentType } from "../conversations/media-types.js";
|
|
13
|
+
import { fitImageForModel, MODEL_IMAGE_CACHE_SECONDS, modelImageKey, parseModelImage, serializeModelImage, } from "../conversations/model-image.js";
|
|
14
|
+
import { CacheService } from "../infra/cache/cache.service.js";
|
|
15
|
+
import { ReferencedFiles } from "./referenced-files.js";
|
|
16
|
+
async function readAll(body) {
|
|
17
|
+
return new Uint8Array(await new Response(body).arrayBuffer());
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The media request building resolves a conversation's refs with (file library spec §5): a ref id
|
|
21
|
+
* is a library file the conversation refers to (`conversation_files` → `files` → `blobs`, read
|
|
22
|
+
* through `BlobsService`), else a `read` copy bound to it (an image a tool showed the agent:
|
|
23
|
+
* `AttachmentsService`). The contract is the attachments' one: an id with neither is left out of
|
|
24
|
+
* {@link mediaInfo} (request building renders the "no longer available" placeholder), and
|
|
25
|
+
* {@link fetchMedia} returns null only when the bytes are certainly gone (a deleted file, a gone
|
|
26
|
+
* blob, a replaced object) and throws on every other error (Ruling M4). No process state.
|
|
27
|
+
*/
|
|
28
|
+
let ConversationMedia = class ConversationMedia {
|
|
29
|
+
files;
|
|
30
|
+
attachments;
|
|
31
|
+
cache;
|
|
32
|
+
constructor(files, attachments,
|
|
33
|
+
/** where the model-sized copies of images are kept between steps ({@link fetchMedia}) */
|
|
34
|
+
cache) {
|
|
35
|
+
this.files = files;
|
|
36
|
+
this.attachments = attachments;
|
|
37
|
+
this.cache = cache;
|
|
38
|
+
}
|
|
39
|
+
/** The live files of the conversation among `ids`, by id. */
|
|
40
|
+
async filesOf(orgId, conversationId, ids) {
|
|
41
|
+
const live = await this.files.referenced(orgId, [conversationId], ids);
|
|
42
|
+
return new Map(live.map((f) => [f.id, f]));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The recorded facts request building needs about the refs (size, PDF page count, etag and the
|
|
46
|
+
* unsigned `uri` of the bytes): for a file, the blob's size and the pinned reference
|
|
47
|
+
* (`ReferencedFiles.modelRef`); for a `read` copy, the row's.
|
|
48
|
+
* An id that is neither is left out; a failed query throws.
|
|
49
|
+
*/
|
|
50
|
+
async mediaInfo(orgId, conversationId, ids) {
|
|
51
|
+
if (ids.length === 0)
|
|
52
|
+
return new Map();
|
|
53
|
+
const files = await this.filesOf(orgId, conversationId, ids);
|
|
54
|
+
const out = new Map();
|
|
55
|
+
for (const f of files.values()) {
|
|
56
|
+
// the uri and etag the model saw stay those of the blob the file was first stored on
|
|
57
|
+
const ref = this.files.modelRef(orgId, f);
|
|
58
|
+
out.set(f.id, { size: f.blob.size, pages: f.pages, etag: ref.etag, uri: ref.uri });
|
|
59
|
+
}
|
|
60
|
+
const rest = ids.filter((id) => !files.has(id));
|
|
61
|
+
for (const [id, info] of await this.attachments.mediaInfo(orgId, conversationId, rest)) {
|
|
62
|
+
out.set(id, info);
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The bytes request building sends for the refs. A file's bytes come from its blob (size and
|
|
68
|
+
* etag checked); a `read` copy's from its row. Null only when certainly gone. An id in `caps` is
|
|
69
|
+
* read by range up to that many leading bytes (the `text/*` cap). A whole image comes back as
|
|
70
|
+
* its model-sized copy ({@link fitImageForModel}), cached for {@link MODEL_IMAGE_CACHE_SECONDS}
|
|
71
|
+
* under the id and the recorded etag (or size), so later steps only check the object's head.
|
|
72
|
+
*/
|
|
73
|
+
async fetchMedia(orgId, conversationId, ids, caps) {
|
|
74
|
+
const out = new Map();
|
|
75
|
+
if (ids.length === 0)
|
|
76
|
+
return out;
|
|
77
|
+
const files = await this.filesOf(orgId, conversationId, ids);
|
|
78
|
+
const rest = ids.filter((id) => !files.has(id));
|
|
79
|
+
const [, others] = await Promise.all([
|
|
80
|
+
Promise.all([...files.values()].map(async (f) => out.set(f.id, await this.fileBytes(orgId, f, caps?.get(f.id))))),
|
|
81
|
+
this.attachments.fetchMedia(orgId, conversationId, rest, caps),
|
|
82
|
+
]);
|
|
83
|
+
for (const [id, media] of others)
|
|
84
|
+
out.set(id, media);
|
|
85
|
+
// every requested id answers (an unknown one: null), in the order asked
|
|
86
|
+
return new Map(ids.map((id) => [id, out.get(id) ?? null]));
|
|
87
|
+
}
|
|
88
|
+
/** One file's bytes for the model (see {@link fetchMedia}). */
|
|
89
|
+
async fileBytes(orgId, f, cap) {
|
|
90
|
+
const size = f.blob.size;
|
|
91
|
+
// a whole image already scaled for the model: the cached copy, once the object checks out
|
|
92
|
+
const cacheKey = cap === undefined && isImageContentType(f.contentType)
|
|
93
|
+
? modelImageKey(orgId, { id: f.id, etag: f.blob.etag, size })
|
|
94
|
+
: undefined;
|
|
95
|
+
const cached = cacheKey === undefined ? null : parseModelImage(await this.cache.get(cacheKey));
|
|
96
|
+
if (cached)
|
|
97
|
+
return (await this.files.head(orgId, f)) ? cached : null;
|
|
98
|
+
const want = cap !== undefined && cap < size ? Math.max(1, cap) : size;
|
|
99
|
+
const got = await this.files.open(orgId, f, want < size ? { range: { start: 0, end: want - 1 } } : {});
|
|
100
|
+
if (!got)
|
|
101
|
+
return null;
|
|
102
|
+
// a store that ignored the range sent the whole object: keep only the head
|
|
103
|
+
const all = await readAll(got.body);
|
|
104
|
+
const data = all.byteLength === size ? all.subarray(0, want) : all;
|
|
105
|
+
if (data.byteLength !== want)
|
|
106
|
+
return null;
|
|
107
|
+
const media = { mediaType: f.contentType, data };
|
|
108
|
+
if (cacheKey === undefined)
|
|
109
|
+
return media;
|
|
110
|
+
// a whole image goes out as the model-sized copy (the stored object stays unchanged)
|
|
111
|
+
const fitted = await fitImageForModel(media);
|
|
112
|
+
if (fitted !== media) {
|
|
113
|
+
await this.cache.set(cacheKey, serializeModelImage(fitted), MODEL_IMAGE_CACHE_SECONDS);
|
|
114
|
+
}
|
|
115
|
+
return fitted;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
ConversationMedia = __decorate([
|
|
119
|
+
Injectable(),
|
|
120
|
+
__param(0, Inject(ReferencedFiles)),
|
|
121
|
+
__param(1, Inject(AttachmentsService)),
|
|
122
|
+
__param(2, Inject(CacheService))
|
|
123
|
+
], ConversationMedia);
|
|
124
|
+
export { ConversationMedia };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type AppendEvent, EventLog } from "../conversations/event-log.js";
|
|
2
|
+
import type { ConversationEvent } from "../conversations/events.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { type BlobRow, BlobsService } from "./blobs.service.js";
|
|
6
|
+
/** One delivered file to record: its file id is the artifact id of its `artifact_created` event. */
|
|
7
|
+
export interface DeliveredFileRow {
|
|
8
|
+
id: string;
|
|
9
|
+
blobId: string;
|
|
10
|
+
name: string;
|
|
11
|
+
contentType: string;
|
|
12
|
+
size: number;
|
|
13
|
+
origin: "delivered" | "generated";
|
|
14
|
+
sourcePath: string;
|
|
15
|
+
/** The seq of the event that delivered it. */
|
|
16
|
+
firstSeq: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A blob a delivery wanted to reference was no longer `ready` when its transaction locked it (a
|
|
20
|
+
* deletion claimed it after {@link BlobsService.ensureFromBytes} returned it). The transaction
|
|
21
|
+
* rolls back; the caller ensures the bytes again and retries.
|
|
22
|
+
*/
|
|
23
|
+
export declare class DeliveryBlobGoneError extends Error {
|
|
24
|
+
readonly blobId: string;
|
|
25
|
+
constructor(blobId: string);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Deliveries into the file library (file library spec §4 "Delivering"): the bytes of a delivered
|
|
29
|
+
* or generated file become a shared blob of the org ({@link ensureBlob}, intent first, reused by
|
|
30
|
+
* hash), then the `artifact_created` events and their `files` / `conversation_files` rows commit
|
|
31
|
+
* in one transaction ({@link appendDelivered}). The owner of a delivered file is the
|
|
32
|
+
* conversation's owner, its agent the conversation's agent. No process state.
|
|
33
|
+
*/
|
|
34
|
+
export declare class FileDeliveries {
|
|
35
|
+
private readonly db;
|
|
36
|
+
readonly txHost: TransactionHost;
|
|
37
|
+
private readonly blobs;
|
|
38
|
+
private readonly eventLog;
|
|
39
|
+
constructor(db: KernelDb, txHost: TransactionHost, blobs: Pick<BlobsService, "ensureFromBytes" | "lockReady">, eventLog: Pick<EventLog, "appendInReport" | "notify">);
|
|
40
|
+
/**
|
|
41
|
+
* The ready blob holding `bytes` (hex `sha256`): an existing one of the org, else a new one
|
|
42
|
+
* written intent first ({@link BlobsService.ensureFromBytes}). Unlocked: the transaction that
|
|
43
|
+
* references it locks it ({@link recordDelivered}).
|
|
44
|
+
*/
|
|
45
|
+
ensureBlob(orgId: string, sha256: string, bytes: Uint8Array, opts: {
|
|
46
|
+
contentType: string;
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
}): Promise<BlobRow>;
|
|
49
|
+
/**
|
|
50
|
+
* Appends `events` to the conversation's log and records the delivered file of every event this
|
|
51
|
+
* call wrote (`files[i]` belongs to `events[i]`; an event returned by its idempotency key, a
|
|
52
|
+
* parallel call's, records nothing), all in one transaction; announces the log after the commit.
|
|
53
|
+
* Throws {@link DeliveryBlobGoneError} (nothing written) when a blob is no longer ready.
|
|
54
|
+
*/
|
|
55
|
+
appendDelivered(orgId: string, conversationId: string, events: AppendEvent[], files: readonly Omit<DeliveredFileRow, "firstSeq">[]): Promise<ConversationEvent[]>;
|
|
56
|
+
private appendDeliveredIn;
|
|
57
|
+
/**
|
|
58
|
+
* In the caller's transaction: locks every row's blob ({@link BlobsService.lockReady}, the gate
|
|
59
|
+
* of a new reference) and inserts the `files` rows (owner and agent the conversation's, status
|
|
60
|
+
* `ready`) and their `conversation_files` rows (`delivered`); idempotent by id. Throws
|
|
61
|
+
* {@link DeliveryBlobGoneError} when a blob is no longer ready.
|
|
62
|
+
*/
|
|
63
|
+
recordDelivered(orgId: string, conversationId: string, rows: readonly DeliveredFileRow[]): Promise<void>;
|
|
64
|
+
}
|