@capybara-hq/kernel 0.1.0-rc.106 → 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/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +4 -3
- package/dist/channels/file-delivery.d.ts +26 -11
- package/dist/channels/file-delivery.js +32 -11
- package/dist/channels/outbound.d.ts +3 -3
- package/dist/channels/outbound.js +15 -13
- package/dist/config/schema.d.ts +3 -0
- package/dist/config/schema.js +12 -0
- 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.module.js +4 -0
- package/dist/conversations/conversations.service.d.ts +10 -9
- package/dist/conversations/conversations.service.js +28 -11
- package/dist/conversations/event-log.d.ts +1 -0
- package/dist/conversations/events.d.ts +49 -27
- package/dist/conversations/events.js +54 -7
- package/dist/conversations/files.d.ts +66 -64
- package/dist/conversations/files.js +127 -147
- package/dist/conversations/prompt-suggestion-request.js +2 -1
- package/dist/conversations/prompt-suggestions.d.ts +3 -3
- package/dist/conversations/prompt-suggestions.js +11 -10
- package/dist/conversations/queue.service.js +7 -2
- package/dist/ctx/create-ctx.d.ts +3 -1
- package/dist/ctx/create-ctx.js +19 -1
- package/dist/db/kernel-schema.d.ts +80 -2
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +19 -0
- package/dist/evals/canonical-json.d.ts +7 -0
- package/dist/evals/canonical-json.js +21 -0
- package/dist/evals/reconstruct.js +13 -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 +3 -1
- package/dist/extensions/host.js +2 -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/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/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 +5 -0
- package/dist/identity/settings.d.ts +26 -0
- package/dist/identity/settings.js +33 -2
- package/dist/infra/outbox/registry.d.ts +82 -0
- package/dist/infra/outbox/registry.js +74 -2
- package/dist/infra/throttler/named-throttlers.d.ts +2 -0
- package/dist/infra/throttler/named-throttlers.js +14 -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/settings.service.d.ts +48 -2
- package/dist/orgs/settings.service.js +63 -3
- 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 +4 -0
- package/dist/platform/ops/admin.ops.js +5 -2
- package/dist/platform/ops/files.ops.d.ts +18 -0
- package/dist/platform/ops/files.ops.js +267 -0
- package/dist/platform/ops/org-settings.ops.js +6 -2
- package/dist/platform/ops/orgs.ops.js +1 -0
- package/dist/platform/ops/shares.ops.js +2 -2
- package/dist/platform/platform-ops.module.d.ts +2 -1
- package/dist/platform/platform-ops.module.js +5 -0
- package/dist/platform/route-schemas.js +3 -0
- package/dist/roles/worker.module.js +15 -1
- 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 +18 -1
- package/dist/runtime/context/build.js +19 -8
- 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 +50 -17
- package/dist/runtime/context/in-context-compaction.d.ts +156 -0
- package/dist/runtime/context/in-context-compaction.js +310 -0
- package/dist/runtime/hook-calls.d.ts +2 -1
- package/dist/runtime/hook-calls.js +5 -2
- package/dist/runtime/maintenance.d.ts +10 -2
- package/dist/runtime/maintenance.js +48 -8
- 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-steps.d.ts +4 -2
- package/dist/runtime/run-steps.js +10 -5
- package/dist/runtime/runtime.module.js +3 -0
- 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/tools/builtins/artifacts.d.ts +21 -13
- package/dist/tools/builtins/artifacts.js +81 -51
- package/dist/tools/builtins/builtin-tools.d.ts +7 -1
- package/dist/tools/builtins/builtin-tools.js +17 -6
- 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/tools.module.js +6 -0
- 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/0190_file_library.sql +285 -0
- package/src/migrations/0191_file_library_copy.sql +4 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { ToolError } from "@capybara-hq/plugin-sdk";
|
|
3
3
|
import { findRepeat } from "../../conversations/delivery-scope.js";
|
|
4
|
+
import { DeliveryBlobGoneError } from "../../files/deliveries.js";
|
|
4
5
|
import { isStorageUnavailable } from "../../storage/storage.service.js";
|
|
5
6
|
/**
|
|
6
7
|
* The tool error when the deployment has no object store (or it went away): files cannot be
|
|
@@ -9,26 +10,32 @@ import { isStorageUnavailable } from "../../storage/storage.service.js";
|
|
|
9
10
|
export const NO_STORAGE = "File storage is not configured in this deployment, so files cannot be delivered; ask an administrator, or provide the result in your reply instead.";
|
|
10
11
|
/** The same as the "tool unavailable" reason (the kernel adds the final period). */
|
|
11
12
|
export const NO_STORAGE_REASON = NO_STORAGE.slice(0, -1);
|
|
13
|
+
/** How often a delivery whose blob was claimed by a deletion meanwhile is tried again. */
|
|
14
|
+
const BLOB_ATTEMPTS = 3;
|
|
12
15
|
/** The clause that says a file repeats an artifact already delivered in the scope. */
|
|
13
16
|
export const REPEAT_CLAUSE = (prior) => `same content as ${prior.name} (artifact ${prior.artifactId}), already delivered to the user since the person last spoke; no new artifact was created.`;
|
|
14
17
|
/** The result text of a file that repeats an artifact already delivered in the scope. */
|
|
15
18
|
export const REPEAT_TEXT = (name, prior) => `${name} has the ${REPEAT_CLAUSE(prior)}`;
|
|
16
19
|
/**
|
|
17
|
-
* Stores `files` as artifacts of the call's conversation, all or none
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
20
|
+
* Stores `files` as artifacts of the call's conversation, all or none (file library spec §4): the
|
|
21
|
+
* bytes of every file become a ready blob of the org ({@link FileDeliveries.ensureBlob}: an
|
|
22
|
+
* existing blob of the same sha256 is reused with no upload, else a new one is written intent
|
|
23
|
+
* first), then all their `artifact_created` events (the call's run and `callId`, the bytes'
|
|
24
|
+
* sha256, `fileId` = the artifact id, `objectKey` = the blob's key) and their files (owner the
|
|
25
|
+
* conversation's owner, origin `generated` for a `generated/` source path, else `delivered`) are
|
|
26
|
+
* written in one transaction. A blob a deletion claimed in between aborts that transaction, and
|
|
27
|
+
* the bytes are ensured again and the whole unit retried (a bounded number of times). Nothing is
|
|
28
|
+
* cleaned up on failure: a blob may serve other files, and an unreferenced one is the blob sweep's.
|
|
29
|
+
* A store that is not there is the {@link NO_STORAGE} tool error. The conversation's files are
|
|
30
|
+
* announced changed once, when something new was stored.
|
|
24
31
|
*
|
|
25
32
|
* With a run, the same bytes reach the user once per delivery scope (truthful delivery spec §2):
|
|
26
33
|
* a file equal to an earlier file of the call, or a repeat by {@link findRepeat} of the scope's
|
|
27
|
-
* artifacts, is not
|
|
34
|
+
* artifacts, is not stored and comes back with `repeatOf`. Each event's idempotency key is
|
|
28
35
|
* `artifact:<runId>:<boundary>:<sha256>:<lastOfSource>`, so two parallel calls that both saw no
|
|
29
36
|
* repeat compute the same key: the append's row lock and the unique key give the second the
|
|
30
|
-
* first's event
|
|
31
|
-
*
|
|
37
|
+
* first's event (and record no file for it), making that file a repeat of the returned event.
|
|
38
|
+
* No process state.
|
|
32
39
|
*/
|
|
33
40
|
export async function storeArtifacts(deps, ctx, files) {
|
|
34
41
|
const hashes = files.map((f) => createHash("sha256").update(f.data).digest("hex"));
|
|
@@ -39,16 +46,23 @@ export async function storeArtifacts(deps, ctx, files) {
|
|
|
39
46
|
}));
|
|
40
47
|
/** Per input: the earlier input of the same call it repeats (with a run), else itself. */
|
|
41
48
|
const firstOf = hashes.map((h, i) => (ctx.runId === undefined ? i : hashes.indexOf(h)));
|
|
42
|
-
|
|
43
|
-
/** The inputs written as events, in the order of `events`, with their generated ids. */
|
|
49
|
+
/** The inputs to write as events, in event order, with their generated ids and blobs. */
|
|
44
50
|
const written = [];
|
|
45
|
-
const events = [];
|
|
46
51
|
let stored = [];
|
|
47
52
|
try {
|
|
53
|
+
const deliveries = deps.deliveries;
|
|
54
|
+
if (deliveries === undefined)
|
|
55
|
+
throw new ToolError(NO_STORAGE);
|
|
48
56
|
const scope = ctx.runId === undefined
|
|
49
57
|
? null
|
|
50
58
|
: await deps.eventLog.readDeliveryScope(ctx.conversationId, ctx.runId);
|
|
51
|
-
const
|
|
59
|
+
const ensure = (i) => {
|
|
60
|
+
const f = files[i];
|
|
61
|
+
return deliveries.ensureBlob(ctx.orgId, hashes[i], f.data, {
|
|
62
|
+
contentType: f.contentType,
|
|
63
|
+
signal: ctx.signal,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
52
66
|
for (const [i, f] of files.entries()) {
|
|
53
67
|
if (firstOf[i] !== i)
|
|
54
68
|
continue;
|
|
@@ -63,46 +77,51 @@ export async function storeArtifacts(deps, ctx, files) {
|
|
|
63
77
|
idemKey = `artifact:${ctx.runId}:${scope.boundary}:${sha256}:${lastOfSource}`;
|
|
64
78
|
}
|
|
65
79
|
const artifactId = randomUUID();
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
size: f.data.byteLength,
|
|
70
|
-
signal: ctx.signal,
|
|
71
|
-
});
|
|
72
|
-
uploaded.push(objectKey);
|
|
73
|
-
written.push({ index: i, artifactId, objectKey });
|
|
74
|
-
events.push({
|
|
75
|
-
type: "artifact_created",
|
|
76
|
-
...(ctx.runId !== undefined && { runId: ctx.runId }),
|
|
80
|
+
written.push({
|
|
81
|
+
index: i,
|
|
82
|
+
artifactId,
|
|
77
83
|
...(idemKey !== undefined && { idemKey }),
|
|
78
|
-
|
|
79
|
-
artifactId,
|
|
80
|
-
name: f.name,
|
|
81
|
-
contentType: f.contentType,
|
|
82
|
-
size: f.data.byteLength,
|
|
83
|
-
objectKey,
|
|
84
|
-
sourcePath: f.sourcePath,
|
|
85
|
-
callId: ctx.callId,
|
|
86
|
-
sha256,
|
|
87
|
-
},
|
|
84
|
+
blob: await ensure(i),
|
|
88
85
|
});
|
|
89
86
|
out[i] = { artifactId, name: f.name, size: f.data.byteLength };
|
|
90
87
|
}
|
|
91
|
-
// a call stopped while
|
|
88
|
+
// a call stopped while storing writes no event (its blobs are shared or the sweep's)
|
|
92
89
|
ctx.signal.throwIfAborted();
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
for (let attempt = 1; written.length > 0; attempt++) {
|
|
91
|
+
try {
|
|
92
|
+
stored = await deliveries.appendDelivered(ctx.orgId, ctx.conversationId, written.map((w) => artifactEvent(ctx, files[w.index], w, hashes)), written.map((w) => {
|
|
93
|
+
const f = files[w.index];
|
|
94
|
+
return {
|
|
95
|
+
id: w.artifactId,
|
|
96
|
+
blobId: w.blob.id,
|
|
97
|
+
name: f.name,
|
|
98
|
+
contentType: f.contentType,
|
|
99
|
+
size: f.data.byteLength,
|
|
100
|
+
origin: f.sourcePath.startsWith("generated/") ? "generated" : "delivered",
|
|
101
|
+
sourcePath: f.sourcePath,
|
|
102
|
+
};
|
|
103
|
+
}));
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
if (!(e instanceof DeliveryBlobGoneError) || attempt >= BLOB_ATTEMPTS)
|
|
108
|
+
throw e;
|
|
109
|
+
// a deletion claimed the blob after it was ensured: ensure those bytes again
|
|
110
|
+
for (const w of written) {
|
|
111
|
+
if (w.blob.id === e.blobId)
|
|
112
|
+
w.blob = await ensure(w.index);
|
|
113
|
+
}
|
|
114
|
+
ctx.signal.throwIfAborted();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
95
117
|
}
|
|
96
118
|
catch (e) {
|
|
97
|
-
// no event references them: remove what was uploaded (best effort)
|
|
98
|
-
await removeObjects(deps, ctx, uploaded);
|
|
99
119
|
if (isStorageUnavailable(e))
|
|
100
120
|
throw new ToolError(NO_STORAGE);
|
|
101
121
|
throw e;
|
|
102
122
|
}
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
const unused = [];
|
|
123
|
+
// an event that is not the one generated (a parallel call appended the same key first): the
|
|
124
|
+
// file repeats that event
|
|
106
125
|
let fresh = false;
|
|
107
126
|
for (const [j, w] of written.entries()) {
|
|
108
127
|
const ev = stored[j];
|
|
@@ -111,10 +130,8 @@ export async function storeArtifacts(deps, ctx, files) {
|
|
|
111
130
|
fresh = true;
|
|
112
131
|
continue;
|
|
113
132
|
}
|
|
114
|
-
unused.push(w.objectKey);
|
|
115
133
|
setRepeat(out, w.index, payload);
|
|
116
134
|
}
|
|
117
|
-
await removeObjects(deps, ctx, unused);
|
|
118
135
|
// a file equal to an earlier one of the call repeats whatever that one became
|
|
119
136
|
for (const [i, first] of firstOf.entries()) {
|
|
120
137
|
if (first === i)
|
|
@@ -126,6 +143,25 @@ export async function storeArtifacts(deps, ctx, files) {
|
|
|
126
143
|
await deps.eventLog.publishChanged?.(ctx.conversationId, "files");
|
|
127
144
|
return out;
|
|
128
145
|
}
|
|
146
|
+
/** The `artifact_created` event of one written file. */
|
|
147
|
+
function artifactEvent(ctx, f, w, hashes) {
|
|
148
|
+
return {
|
|
149
|
+
type: "artifact_created",
|
|
150
|
+
...(ctx.runId !== undefined && { runId: ctx.runId }),
|
|
151
|
+
...(w.idemKey !== undefined && { idemKey: w.idemKey }),
|
|
152
|
+
payload: {
|
|
153
|
+
artifactId: w.artifactId,
|
|
154
|
+
fileId: w.artifactId,
|
|
155
|
+
name: f.name,
|
|
156
|
+
contentType: f.contentType,
|
|
157
|
+
size: f.data.byteLength,
|
|
158
|
+
objectKey: w.blob.objectKey,
|
|
159
|
+
sourcePath: f.sourcePath,
|
|
160
|
+
callId: ctx.callId,
|
|
161
|
+
sha256: hashes[w.index],
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
129
165
|
function setRepeat(out, i, prior) {
|
|
130
166
|
const cur = out[i];
|
|
131
167
|
out[i] = {
|
|
@@ -135,9 +171,3 @@ function setRepeat(out, i, prior) {
|
|
|
135
171
|
repeatOf: { artifactId: prior.artifactId, name: prior.name },
|
|
136
172
|
};
|
|
137
173
|
}
|
|
138
|
-
/** Deletes `keys` from the org's kernel store, best effort (a failure leaves an orphan object). */
|
|
139
|
-
async function removeObjects(deps, ctx, keys) {
|
|
140
|
-
await Promise.all(keys.map((key) => Promise.resolve()
|
|
141
|
-
.then(() => deps.storage.kernel(ctx.orgId).delete(key))
|
|
142
|
-
.catch(() => { })));
|
|
143
|
-
}
|
|
@@ -7,6 +7,8 @@ import { AttachmentsService } from "../../conversations/attachments.service.js";
|
|
|
7
7
|
import { EventLog } from "../../conversations/event-log.js";
|
|
8
8
|
import { ConversationDevices } from "../../devices/conversation-devices.js";
|
|
9
9
|
import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
|
|
10
|
+
import { FileDeliveries } from "../../files/deliveries.js";
|
|
11
|
+
import { ReferencedFiles } from "../../files/referenced-files.js";
|
|
10
12
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
11
13
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
12
14
|
import { ModelImages } from "../../models/images.js";
|
|
@@ -53,7 +55,11 @@ export declare class BuiltinTools {
|
|
|
53
55
|
images: ModelImages, providers: ProviderRegistry,
|
|
54
56
|
/** The clock `CurrentTime` reads (the run context's clock seam; `Date.now` by default). */
|
|
55
57
|
/** The managed sandbox route's device context writer (permission context replay spec B3). */
|
|
56
|
-
managedDeviceContext: ManagedDeviceContextRecorder,
|
|
58
|
+
managedDeviceContext: ManagedDeviceContextRecorder,
|
|
59
|
+
/** The library files a conversation refers to (`Read` / `Deliver` by id, `FetchAttachment`). */
|
|
60
|
+
referencedFiles: ReferencedFiles,
|
|
61
|
+
/** `Deliver` and `GenerateImage` store files as library files on shared blobs. */
|
|
62
|
+
deliveries: FileDeliveries, now?: () => number);
|
|
57
63
|
/** The builtin tools, in a fixed order. */
|
|
58
64
|
list(): readonly BuiltinTool[];
|
|
59
65
|
}
|
|
@@ -20,6 +20,8 @@ import { ConversationDevices } from "../../devices/conversation-devices.js";
|
|
|
20
20
|
import { ApiError } from "../../errors.js";
|
|
21
21
|
import { ExtensionHost } from "../../extensions/host.js";
|
|
22
22
|
import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
|
|
23
|
+
import { FileDeliveries } from "../../files/deliveries.js";
|
|
24
|
+
import { ReferencedFiles } from "../../files/referenced-files.js";
|
|
23
25
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
24
26
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
25
27
|
import { KERNEL_LOGGER } from "../../infra/tokens.js";
|
|
@@ -76,7 +78,11 @@ let BuiltinTools = class BuiltinTools {
|
|
|
76
78
|
images, providers,
|
|
77
79
|
/** The clock `CurrentTime` reads (the run context's clock seam; `Date.now` by default). */
|
|
78
80
|
/** The managed sandbox route's device context writer (permission context replay spec B3). */
|
|
79
|
-
managedDeviceContext,
|
|
81
|
+
managedDeviceContext,
|
|
82
|
+
/** The library files a conversation refers to (`Read` / `Deliver` by id, `FetchAttachment`). */
|
|
83
|
+
referencedFiles,
|
|
84
|
+
/** `Deliver` and `GenerateImage` store files as library files on shared blobs. */
|
|
85
|
+
deliveries, now) {
|
|
80
86
|
// `Read` beyond the device (vision-read design §1): files by id, the vision read with its
|
|
81
87
|
// hooks (as the WebFetch extraction), the stored copies and the chat model's capabilities
|
|
82
88
|
const vision = { purposes, eventLog, stepLog, config, log, hooks: host?.hooks };
|
|
@@ -86,7 +92,7 @@ let BuiltinTools = class BuiltinTools {
|
|
|
86
92
|
return db.db;
|
|
87
93
|
},
|
|
88
94
|
attachments,
|
|
89
|
-
|
|
95
|
+
files: referencedFiles,
|
|
90
96
|
},
|
|
91
97
|
reads: attachments,
|
|
92
98
|
vision,
|
|
@@ -101,8 +107,9 @@ let BuiltinTools = class BuiltinTools {
|
|
|
101
107
|
inTree: async (root, id) => id !== root && (await conversationAncestors(db.db, id)).includes(root),
|
|
102
108
|
childrenOf: (orgId, conversationId) => readChildAgents(db.db, orgId, conversationId),
|
|
103
109
|
},
|
|
104
|
-
attachments,
|
|
110
|
+
attachments: referencedFiles,
|
|
105
111
|
storage,
|
|
112
|
+
deliveries,
|
|
106
113
|
eventLog,
|
|
107
114
|
config,
|
|
108
115
|
log,
|
|
@@ -126,7 +133,7 @@ let BuiltinTools = class BuiltinTools {
|
|
|
126
133
|
config,
|
|
127
134
|
sandbox,
|
|
128
135
|
devices,
|
|
129
|
-
attachments,
|
|
136
|
+
attachments: referencedFiles,
|
|
130
137
|
eventLog,
|
|
131
138
|
log,
|
|
132
139
|
managedDeviceContext,
|
|
@@ -150,12 +157,14 @@ let BuiltinTools = class BuiltinTools {
|
|
|
150
157
|
providers,
|
|
151
158
|
storage,
|
|
152
159
|
eventLog,
|
|
160
|
+
deliveries,
|
|
153
161
|
stepLog,
|
|
154
162
|
config,
|
|
155
163
|
log,
|
|
156
164
|
hooks: host?.hooks,
|
|
157
165
|
db,
|
|
158
166
|
attachments,
|
|
167
|
+
files: referencedFiles,
|
|
159
168
|
sandbox: sandboxDeps,
|
|
160
169
|
}),
|
|
161
170
|
// the platform's own operations and docs, as the run's person (Platform tool spec)
|
|
@@ -258,8 +267,10 @@ BuiltinTools = __decorate([
|
|
|
258
267
|
__param(25, Inject(ModelImages)),
|
|
259
268
|
__param(26, Inject(ProviderRegistry)),
|
|
260
269
|
__param(27, Inject(ManagedDeviceContextRecorder)),
|
|
261
|
-
__param(28,
|
|
262
|
-
__param(
|
|
270
|
+
__param(28, Inject(ReferencedFiles)),
|
|
271
|
+
__param(29, Inject(FileDeliveries)),
|
|
272
|
+
__param(30, Optional()),
|
|
273
|
+
__param(30, Inject(RUN_CONTEXT_CLOCK))
|
|
263
274
|
], BuiltinTools);
|
|
264
275
|
export { BuiltinTools };
|
|
265
276
|
/**
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { AttachmentsService } from "../../conversations/attachments.service.js";
|
|
2
|
+
import type { ReferencedFiles } from "../../files/referenced-files.js";
|
|
2
3
|
import type { KernelDb } from "../../infra/database/kernel-db.js";
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* A file of the conversation by id: a library file the conversation set refers to (an upload, a
|
|
6
|
+
* delivery or a generated image) or a `read` copy (an image a tool showed the agent).
|
|
7
|
+
*/
|
|
5
8
|
export interface ConversationFile {
|
|
9
|
+
/** `attachment`: an upload or a `read` copy; `artifact`: a delivered or generated file. */
|
|
6
10
|
kind: "attachment" | "artifact";
|
|
7
11
|
id: string;
|
|
8
12
|
/**
|
|
@@ -10,13 +14,14 @@ export interface ConversationFile {
|
|
|
10
14
|
* `Read` of a copy that is already the calling conversation's needs no new copy.
|
|
11
15
|
*/
|
|
12
16
|
readCopyOf?: string;
|
|
17
|
+
/** The owner of a library file; absent for a `read` copy. */
|
|
18
|
+
accountId?: string;
|
|
13
19
|
name: string;
|
|
14
20
|
contentType: string;
|
|
15
21
|
size: number;
|
|
16
22
|
/**
|
|
17
|
-
* Reads the bytes, at most `maxBytes` of them when given (the head of a larger file:
|
|
18
|
-
*
|
|
19
|
-
* {@link discard}, exactly once: an attachment holds its stream.
|
|
23
|
+
* Reads the bytes, at most `maxBytes` of them when given (the head of a larger file: the stream
|
|
24
|
+
* is cancelled there). Call it, or {@link discard}, exactly once: the lookup holds the stream.
|
|
20
25
|
*/
|
|
21
26
|
open(opts?: {
|
|
22
27
|
maxBytes?: number;
|
|
@@ -26,23 +31,28 @@ export interface ConversationFile {
|
|
|
26
31
|
}
|
|
27
32
|
export interface ConversationFileDeps {
|
|
28
33
|
db: Pick<KernelDb, "selectFrom">;
|
|
34
|
+
/** The `read` copies. */
|
|
29
35
|
attachments: Pick<AttachmentsService, "openForConversation">;
|
|
30
|
-
|
|
36
|
+
/** The library files the conversations refer to, and their bytes. */
|
|
37
|
+
files: Pick<ReferencedFiles, "referenced" | "referencedBelow" | "open" | "head">;
|
|
31
38
|
}
|
|
32
39
|
/**
|
|
33
|
-
* A file of the conversation by id, always inside `orgId
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* Anything else (a
|
|
38
|
-
*
|
|
40
|
+
* A file of the conversation by id, always inside `orgId` (file library spec §2, §5): a library
|
|
41
|
+
* file referred to (`conversation_files`) by the conversation, by its root, or by a sub-agent
|
|
42
|
+
* conversation below the conversation (so a parent reaches an image its sub-agent's tool returned;
|
|
43
|
+
* tool result images design §5), else a `read` copy bound to one of them (this lookup is the
|
|
44
|
+
* model's, vision-read design §1.1). Anything else (a library file the person never attached, a
|
|
45
|
+
* sibling's or another tree's, another org's, a deleted file, bytes gone or replaced, an id that is
|
|
46
|
+
* not a UUID) is `null`. A library file's size and type come from its rows and its bytes are
|
|
47
|
+
* checked by a HEAD; they are opened only when {@link ConversationFile.open} is called. Storage
|
|
48
|
+
* errors are thrown as they are. `signal` aborts the storage reads.
|
|
39
49
|
*/
|
|
40
50
|
export declare function findConversationFile(deps: ConversationFileDeps, q: {
|
|
41
51
|
orgId: string;
|
|
42
52
|
conversationId: string;
|
|
43
53
|
rootConversationId: string;
|
|
44
54
|
id: string;
|
|
45
|
-
/** The most bytes {@link ConversationFile.open} may be asked for (
|
|
55
|
+
/** The most bytes {@link ConversationFile.open} may be asked for (a range read). */
|
|
46
56
|
maxBytes?: number;
|
|
47
57
|
signal?: AbortSignal;
|
|
48
58
|
}): Promise<ConversationFile | null>;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/** Lower-case or upper-case UUIDs: attachment and artifact ids. */
|
|
1
|
+
/** Lower-case or upper-case UUIDs: file and `read` copy ids. */
|
|
3
2
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
4
3
|
/**
|
|
5
4
|
* The bytes of `body`, at most `maxBytes` (the stream is cancelled once that many are read: a
|
|
@@ -55,108 +54,88 @@ async function isBelow(db, orgId, candidate, ancestor) {
|
|
|
55
54
|
}
|
|
56
55
|
return false;
|
|
57
56
|
}
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
* in `orgId`; null when there is none.
|
|
61
|
-
*/
|
|
62
|
-
async function ownerOf(db, orgId, id) {
|
|
57
|
+
/** The conversation a ready `read` copy of this id is bound to, in `orgId`; null when none. */
|
|
58
|
+
async function readCopyOwner(db, orgId, id) {
|
|
63
59
|
const att = await db
|
|
64
60
|
.selectFrom("attachments")
|
|
65
|
-
.select(
|
|
61
|
+
.select("conversation_id")
|
|
66
62
|
.where("id", "=", id)
|
|
67
63
|
.where("org_id", "=", orgId)
|
|
68
64
|
.where("status", "=", "ready")
|
|
69
|
-
.where("purpose", "
|
|
65
|
+
.where("purpose", "=", "read")
|
|
70
66
|
.executeTakeFirst();
|
|
71
|
-
|
|
72
|
-
return { conversationId: att.conversation_id, purpose: att.purpose };
|
|
73
|
-
const ev = await db
|
|
74
|
-
.selectFrom("conversation_events as e")
|
|
75
|
-
.innerJoin("conversations as c", "c.id", "e.conversation_id")
|
|
76
|
-
.select("e.conversation_id")
|
|
77
|
-
.where("c.org_id", "=", orgId)
|
|
78
|
-
.where("e.type", "=", "artifact_created")
|
|
79
|
-
.where(sql `e.payload->>'artifactId'`, "=", id)
|
|
80
|
-
.limit(1)
|
|
81
|
-
.executeTakeFirst();
|
|
82
|
-
return ev ? { conversationId: ev.conversation_id, purpose: "artifact" } : null;
|
|
67
|
+
return att?.conversation_id ?? null;
|
|
83
68
|
}
|
|
84
69
|
/**
|
|
85
|
-
* A file of the conversation by id, always inside `orgId
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* Anything else (a
|
|
90
|
-
*
|
|
70
|
+
* A file of the conversation by id, always inside `orgId` (file library spec §2, §5): a library
|
|
71
|
+
* file referred to (`conversation_files`) by the conversation, by its root, or by a sub-agent
|
|
72
|
+
* conversation below the conversation (so a parent reaches an image its sub-agent's tool returned;
|
|
73
|
+
* tool result images design §5), else a `read` copy bound to one of them (this lookup is the
|
|
74
|
+
* model's, vision-read design §1.1). Anything else (a library file the person never attached, a
|
|
75
|
+
* sibling's or another tree's, another org's, a deleted file, bytes gone or replaced, an id that is
|
|
76
|
+
* not a UUID) is `null`. A library file's size and type come from its rows and its bytes are
|
|
77
|
+
* checked by a HEAD; they are opened only when {@link ConversationFile.open} is called. Storage
|
|
78
|
+
* errors are thrown as they are. `signal` aborts the storage reads.
|
|
91
79
|
*/
|
|
92
80
|
export async function findConversationFile(deps, q) {
|
|
93
81
|
if (!UUID.test(q.id))
|
|
94
82
|
return null;
|
|
95
83
|
const id = q.id.toLowerCase();
|
|
96
84
|
const conversations = [...new Set([q.conversationId, q.rootConversationId])];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const opened = await deps.attachments.openForConversation(q.orgId, conversationId, id, {
|
|
107
|
-
purposes: ["chat", "read"],
|
|
108
|
-
...(q.maxBytes !== undefined && { maxBytes: q.maxBytes }),
|
|
109
|
-
...(q.signal !== undefined && { signal: q.signal }),
|
|
110
|
-
});
|
|
111
|
-
if (opened === null)
|
|
112
|
-
continue;
|
|
85
|
+
const files = deps.files;
|
|
86
|
+
// the conversation and its root first; only on a miss the sub-agents below the conversation
|
|
87
|
+
let refs = (await files.referenced(q.orgId, conversations, [id])).sort(attachedFirst);
|
|
88
|
+
if (refs.length === 0)
|
|
89
|
+
refs = await files.referencedBelow(q.orgId, q.conversationId, id);
|
|
90
|
+
const file = refs[0];
|
|
91
|
+
if (file !== undefined) {
|
|
92
|
+
if ((await files.head(q.orgId, file)) === null)
|
|
93
|
+
return null;
|
|
113
94
|
return {
|
|
114
|
-
|
|
95
|
+
// an upload, or a delivered file sent into this conversation set, is an attachment here
|
|
96
|
+
kind: file.role === "attached" || file.sourcePath === null ? "attachment" : "artifact",
|
|
115
97
|
id,
|
|
116
|
-
|
|
117
|
-
name:
|
|
118
|
-
contentType:
|
|
119
|
-
size:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
98
|
+
accountId: file.accountId,
|
|
99
|
+
name: file.name,
|
|
100
|
+
contentType: file.contentType,
|
|
101
|
+
size: file.blob.size,
|
|
102
|
+
async open(opts) {
|
|
103
|
+
const max = opts?.maxBytes ?? q.maxBytes;
|
|
104
|
+
// only a larger object is ranged (a range on an empty object is unsatisfiable)
|
|
105
|
+
const got = await files.open(q.orgId, file, {
|
|
106
|
+
...(q.signal !== undefined && { signal: q.signal }),
|
|
107
|
+
...(max !== undefined && file.blob.size > max && { range: { start: 0, end: max - 1 } }),
|
|
108
|
+
});
|
|
109
|
+
if (got === null)
|
|
110
|
+
throw new Error(`file ${id} is no longer available`);
|
|
111
|
+
return bytesOf(got.body, max);
|
|
112
|
+
},
|
|
113
|
+
discard: async () => { },
|
|
123
114
|
};
|
|
124
115
|
}
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
.where("e.conversation_id", "in", conversations)
|
|
131
|
-
.where("e.type", "=", "artifact_created")
|
|
132
|
-
.where(sql `e.payload->>'artifactId'`, "=", id)
|
|
133
|
-
.limit(1)
|
|
134
|
-
.executeTakeFirst();
|
|
135
|
-
if (!row)
|
|
116
|
+
const owner = await readCopyOwner(deps.db, q.orgId, id);
|
|
117
|
+
if (owner === null)
|
|
118
|
+
return null;
|
|
119
|
+
if (!conversations.includes(owner) &&
|
|
120
|
+
!(await isBelow(deps.db, q.orgId, owner, q.conversationId))) {
|
|
136
121
|
return null;
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
122
|
+
}
|
|
123
|
+
const opened = await deps.attachments.openForConversation(q.orgId, owner, id, {
|
|
124
|
+
...(q.maxBytes !== undefined && { maxBytes: q.maxBytes }),
|
|
125
|
+
...(q.signal !== undefined && { signal: q.signal }),
|
|
126
|
+
});
|
|
127
|
+
if (opened === null)
|
|
141
128
|
return null;
|
|
142
129
|
return {
|
|
143
|
-
kind: "
|
|
130
|
+
kind: "attachment",
|
|
144
131
|
id,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const ranged = max !== undefined && head.size > max;
|
|
152
|
-
const got = await store.get(a.objectKey, {
|
|
153
|
-
...(q.signal !== undefined && { signal: q.signal }),
|
|
154
|
-
...(ranged && { range: { start: 0, end: max - 1 } }),
|
|
155
|
-
});
|
|
156
|
-
if (got === null)
|
|
157
|
-
throw new Error(`artifact ${id} is no longer available`);
|
|
158
|
-
return bytesOf(got.body, max);
|
|
159
|
-
},
|
|
160
|
-
discard: async () => { },
|
|
132
|
+
readCopyOf: owner,
|
|
133
|
+
name: opened.name,
|
|
134
|
+
contentType: opened.contentType,
|
|
135
|
+
size: opened.size,
|
|
136
|
+
open: (opts) => bytesOf(opened.body, opts?.maxBytes),
|
|
137
|
+
discard: () => opened.body.cancel().catch(() => { }),
|
|
161
138
|
};
|
|
162
139
|
}
|
|
140
|
+
/** References of one file across a conversation set: an `attached` one first. */
|
|
141
|
+
const attachedFirst = (a, b) => (a.role === "attached" ? 0 : 1) - (b.role === "attached" ? 0 : 1);
|
|
@@ -2,6 +2,7 @@ import { type ImageMediaType, type Logger, type ToolSpec } from "@capybara-hq/pl
|
|
|
2
2
|
import type { AttachmentsService } from "../../../conversations/attachments.service.js";
|
|
3
3
|
import type { EventLog } from "../../../conversations/event-log.js";
|
|
4
4
|
import type { DeviceUnusableReason } from "../../../devices/conversation-devices.js";
|
|
5
|
+
import type { FileDeliveries } from "../../../files/deliveries.js";
|
|
5
6
|
import type { KernelConfigService } from "../../../infra/config/kernel-config.service.js";
|
|
6
7
|
import type { KernelDb } from "../../../infra/database/kernel-db.js";
|
|
7
8
|
import { type ImageInputCaps } from "../../../models/image-usable.js";
|
|
@@ -12,6 +13,7 @@ import { type ProviderRegistry } from "../../../runtime/providers.js";
|
|
|
12
13
|
import type { StepLog } from "../../../runtime/step-log.js";
|
|
13
14
|
import type { StorageService } from "../../../storage/storage.service.js";
|
|
14
15
|
import type { BuiltinTool } from "../../registry.js";
|
|
16
|
+
import { type ConversationFileDeps } from "../conversation-files.js";
|
|
15
17
|
import { type SandboxToolDeps } from "../sandbox/common.js";
|
|
16
18
|
/** The listed spec for an org whose image model accepts `caps` (pure; `n` and references drop or cap). */
|
|
17
19
|
export declare function generateImageSpec(caps: ImageInputCaps, standardPixels: number): ToolSpec;
|
|
@@ -33,7 +35,9 @@ export interface GenerateImageDeps {
|
|
|
33
35
|
purposes: Pick<ModelPurposesService, "resolve">;
|
|
34
36
|
providers: Pick<ProviderRegistry, "resolve">;
|
|
35
37
|
storage: Pick<StorageService, "kernel" | "available">;
|
|
36
|
-
eventLog: Pick<EventLog, "
|
|
38
|
+
eventLog: Pick<EventLog, "notify" | "readDeliveryScope"> & Partial<Pick<EventLog, "publishChanged">>;
|
|
39
|
+
/** The images' blobs and their delivery transaction ({@link storeArtifacts}). */
|
|
40
|
+
deliveries: Pick<FileDeliveries, "ensureBlob" | "appendDelivered">;
|
|
37
41
|
stepLog: Pick<StepLog, "appendCounted" | "appendReported" | "appendAudited">;
|
|
38
42
|
config: Pick<KernelConfigService, "get">;
|
|
39
43
|
log: Pick<Logger, "warn">;
|
|
@@ -42,6 +46,8 @@ export interface GenerateImageDeps {
|
|
|
42
46
|
/** The artifact lookup of `reference_images`. */
|
|
43
47
|
db: Pick<KernelDb, "selectFrom">;
|
|
44
48
|
attachments: Pick<AttachmentsService, "openForConversation">;
|
|
49
|
+
/** The library files of `reference_images` (attached or delivered in the conversation set). */
|
|
50
|
+
files: ConversationFileDeps["files"];
|
|
45
51
|
/** The device copy of `file_path` (the sandbox tools' own dependencies). */
|
|
46
52
|
sandbox: SandboxToolDeps;
|
|
47
53
|
}
|
|
@@ -302,7 +302,7 @@ async function resolveReferences(deps, ctx, ids) {
|
|
|
302
302
|
const out = [];
|
|
303
303
|
for (const [k, id] of ids.entries()) {
|
|
304
304
|
try {
|
|
305
|
-
const found = await findConversationFile({ db: deps.db, attachments: deps.attachments,
|
|
305
|
+
const found = await findConversationFile({ db: deps.db, attachments: deps.attachments, files: deps.files }, { orgId: ctx.orgId, conversationId: ctx.conversationId, rootConversationId: root, id });
|
|
306
306
|
if (found === null)
|
|
307
307
|
throw new ToolError(unknownReferenceText(k));
|
|
308
308
|
const mediaType = found.contentType.split(";")[0]?.trim().toLowerCase() ?? "";
|