@capybara-hq/kernel 0.1.0-rc.37 → 0.1.0-rc.39
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.module.d.ts +2 -1
- package/dist/admin/admin.module.js +5 -1
- package/dist/admin/org-network.controller.d.ts +30 -0
- package/dist/admin/org-network.controller.js +77 -0
- package/dist/admin/org-network.schemas.d.ts +16 -0
- package/dist/admin/org-network.schemas.js +27 -0
- package/dist/admin/org-network.service.d.ts +41 -0
- package/dist/admin/org-network.service.js +101 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +1 -0
- package/dist/config/schema.js +8 -2
- package/dist/conversations/attachments.service.d.ts +39 -0
- package/dist/conversations/attachments.service.js +59 -22
- package/dist/conversations/conversations.controller.d.ts +1 -1
- package/dist/conversations/conversations.controller.js +1 -1
- package/dist/conversations/conversations.service.d.ts +5 -1
- package/dist/conversations/conversations.service.js +9 -2
- package/dist/conversations/event-log.js +4 -1
- package/dist/conversations/files.d.ts +39 -12
- package/dist/conversations/files.js +134 -55
- package/dist/ctx/create-ctx.d.ts +8 -1
- package/dist/ctx/create-ctx.js +12 -1
- package/dist/ctx/net.d.ts +50 -0
- package/dist/ctx/net.js +67 -0
- package/dist/db/kernel-schema.d.ts +10 -0
- package/dist/devices/devices.service.js +2 -1
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +5 -0
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/host.d.ts +7 -1
- package/dist/extensions/host.js +13 -3
- package/dist/files/previewers.js +6 -0
- package/dist/http/api.module.js +2 -0
- package/dist/infra/database/transaction-host.d.ts +32 -1
- package/dist/infra/database/transaction-host.js +30 -4
- package/dist/infra/database/xact-lock.d.ts +16 -0
- package/dist/infra/database/xact-lock.js +28 -1
- package/dist/net/public-address.d.ts +21 -1
- package/dist/net/public-address.js +56 -10
- package/dist/net/safe-fetch.d.ts +4 -1
- package/dist/net/safe-fetch.js +15 -4
- package/dist/orgs/directory-sync.js +2 -0
- package/dist/orgs/members.service.js +5 -0
- package/dist/orgs/network-allowances.service.d.ts +79 -0
- package/dist/orgs/network-allowances.service.js +371 -0
- package/dist/orgs/offboarding.d.ts +3 -1
- package/dist/orgs/offboarding.js +9 -2
- package/dist/orgs/org-lock.d.ts +28 -2
- package/dist/orgs/org-lock.js +59 -4
- package/dist/orgs/orgs.controller.d.ts +11 -2
- package/dist/orgs/orgs.controller.js +19 -3
- package/dist/orgs/orgs.module.d.ts +2 -1
- package/dist/orgs/orgs.module.js +5 -1
- package/dist/orgs/orgs.schemas.d.ts +12 -0
- package/dist/orgs/orgs.schemas.js +5 -0
- package/dist/orgs/orgs.service.js +6 -1
- package/dist/overrides.d.ts +4 -0
- package/dist/overrides.js +2 -0
- package/dist/platform/ops/conversations.ops.js +2 -1
- package/dist/platform/ops/orgs.ops.d.ts +7 -1
- package/dist/platform/ops/orgs.ops.js +122 -3
- package/dist/platform/route-schemas.js +3 -0
- package/dist/runtime/checkpointer.d.ts +3 -0
- package/dist/runtime/checkpointer.js +7 -4
- package/dist/runtime/langgraph-loop.js +8 -3
- package/dist/runtime/run-execution.d.ts +56 -3
- package/dist/runtime/run-execution.js +77 -8
- package/dist/runtime/step-log.d.ts +5 -0
- package/dist/runtime/step-log.js +9 -4
- package/dist/runtime/waits.js +3 -1
- package/dist/tools/connector-content.js +1 -0
- package/dist/tools/connectors.service.js +9 -6
- package/dist/tools/drift.d.ts +5 -1
- package/dist/tools/drift.js +9 -4
- package/dist/tools/pipeline.js +1 -0
- package/package.json +3 -3
- package/src/migrations/0145_org_network_allowances.sql +12 -0
|
@@ -123,6 +123,9 @@ let EventLog = class EventLog {
|
|
|
123
123
|
const { events: out, fresh } = await this.write(conversationId, events, bodies);
|
|
124
124
|
return { events: out, fresh };
|
|
125
125
|
}
|
|
126
|
+
// shared admission under a run fence (`RunExecution`): an append locks only the conversation row
|
|
127
|
+
// and its own rows, so the runs of one org append in parallel (a tool step's are still admitted
|
|
128
|
+
// exclusively)
|
|
126
129
|
async appendOwn(conversationId, events, bodies) {
|
|
127
130
|
const { events: out, toSeq } = await this.write(conversationId, events, bodies);
|
|
128
131
|
return { events: out, toSeq };
|
|
@@ -419,7 +422,7 @@ __decorate([
|
|
|
419
422
|
Transactional({ propagation: "mandatory" })
|
|
420
423
|
], EventLog.prototype, "appendInReport", null);
|
|
421
424
|
__decorate([
|
|
422
|
-
Transactional({ propagation: "requires_new" })
|
|
425
|
+
Transactional({ propagation: "requires_new", admission: "shared" })
|
|
423
426
|
], EventLog.prototype, "appendOwn", null);
|
|
424
427
|
EventLog = __decorate([
|
|
425
428
|
Injectable(),
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
2
|
import { StorageService } from "../storage/storage.service.js";
|
|
3
3
|
import { ConversationAccess } from "./access.js";
|
|
4
|
+
import { AttachmentsService } from "./attachments.service.js";
|
|
4
5
|
/**
|
|
5
|
-
* A conversation's
|
|
6
|
-
* (`artifact_created`), grouped by `sourcePath` into versions,
|
|
7
|
-
*
|
|
6
|
+
* A conversation's files (UI refresh B, spec §5.8 / §5.9): the files the agent delivered
|
|
7
|
+
* (`artifact_created`), grouped by `sourcePath` into versions, and the files users sent in it
|
|
8
|
+
* (ready `chat` attachments bound to the conversation), newest first. Read on every request (no
|
|
9
|
+
* cache); `status` comes from the object store. Both kinds are read under the conversation's
|
|
10
|
+
* access: whoever may read the conversation may read the attachments sent in it, nobody else.
|
|
8
11
|
*/
|
|
9
12
|
/** Versions listed per file (newest first). */
|
|
10
13
|
export declare const FILE_VERSIONS_MAX = 20;
|
|
@@ -32,6 +35,12 @@ export interface FileOutput {
|
|
|
32
35
|
/** `removed`: the latest version's object is gone from storage (decision 8). */
|
|
33
36
|
status: "available" | "removed";
|
|
34
37
|
previewable: "image" | "text" | null;
|
|
38
|
+
/**
|
|
39
|
+
* `delivered`: the agent delivered it (`artifact_created`); `uploaded`: a user sent it with a
|
|
40
|
+
* message (an attachment: `id` is the attachment id, one version, `sourcePath` is its name and
|
|
41
|
+
* `callId` is empty).
|
|
42
|
+
*/
|
|
43
|
+
origin: "delivered" | "uploaded";
|
|
35
44
|
}
|
|
36
45
|
/** Text content of one file version, at most the requested bytes. */
|
|
37
46
|
export interface FileContent {
|
|
@@ -52,17 +61,28 @@ export declare class ConversationFiles {
|
|
|
52
61
|
private readonly storage;
|
|
53
62
|
/** Same visibility as the event stream. */
|
|
54
63
|
private readonly access;
|
|
64
|
+
/** The attachments sent in the conversation, and the rule their stored content must meet. */
|
|
65
|
+
private readonly attachments;
|
|
55
66
|
constructor(db: KernelDb, storage: Pick<StorageService, "kernel">,
|
|
56
67
|
/** Same visibility as the event stream. */
|
|
57
|
-
access: Pick<ConversationAccess, "isAccessible"
|
|
68
|
+
access: Pick<ConversationAccess, "isAccessible">,
|
|
69
|
+
/** The attachments sent in the conversation, and the rule their stored content must meet. */
|
|
70
|
+
attachments: Pick<AttachmentsService, "boundRows" | "storedMatches">);
|
|
58
71
|
private checkAccess;
|
|
59
72
|
/** Every delivery of the conversation in log order. */
|
|
60
73
|
private deliveries;
|
|
61
74
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
75
|
+
* One file of the conversation by id: a delivered version first, then an attachment sent in it;
|
|
76
|
+
* neither → `conversation.artifact_not_found`. An attachment carries `upload`, the row its stored
|
|
77
|
+
* object must still match. Call after {@link checkAccess}.
|
|
78
|
+
*/
|
|
79
|
+
private resolve;
|
|
80
|
+
/**
|
|
81
|
+
* Files newest first: a delivered file by its **first** delivery, so a new version keeps the
|
|
82
|
+
* file's place and a cursor stays stable while files are re-delivered (review M2); an uploaded
|
|
83
|
+
* file by its upload time; equal times by id. `after` is a file id from the previous page (any
|
|
84
|
+
* version's id finds its file); a cursor that names no file gives an empty page. Storage HEADs run
|
|
85
|
+
* at most {@link HEAD_CONCURRENCY} at a time.
|
|
66
86
|
*/
|
|
67
87
|
list(orgId: string, conversationId: string, viewer: string, opts: {
|
|
68
88
|
after?: string;
|
|
@@ -72,9 +92,11 @@ export declare class ConversationFiles {
|
|
|
72
92
|
next: string | null;
|
|
73
93
|
}>;
|
|
74
94
|
/**
|
|
75
|
-
* One file version of the conversation (any version's id
|
|
76
|
-
* the viewer can read the conversation. For the kernel only
|
|
77
|
-
* never leaves the kernel.
|
|
95
|
+
* One file version of the conversation (any version's id, or an attachment sent in it) with its
|
|
96
|
+
* object key, after checking the viewer can read the conversation. For the kernel only
|
|
97
|
+
* (previewers, embed grants): the key never leaves the kernel. An attachment whose stored object
|
|
98
|
+
* is gone or no longer the one finalize checked → `conversation.artifact_removed`; `recorded` is
|
|
99
|
+
* then what a later read of the object must still match ({@link attachmentContentMatches}).
|
|
78
100
|
*/
|
|
79
101
|
file(orgId: string, conversationId: string, viewer: string, fileId: string): Promise<{
|
|
80
102
|
id: string;
|
|
@@ -82,9 +104,14 @@ export declare class ConversationFiles {
|
|
|
82
104
|
contentType: string;
|
|
83
105
|
size: number;
|
|
84
106
|
objectKey: string;
|
|
107
|
+
recorded?: {
|
|
108
|
+
size: number;
|
|
109
|
+
etag: string | null;
|
|
110
|
+
};
|
|
85
111
|
}>;
|
|
86
112
|
/**
|
|
87
|
-
* The first `limit` bytes of a text file version (any version's id
|
|
113
|
+
* The first `limit` bytes of a text file version (any version's id, or an attachment sent in the
|
|
114
|
+
* conversation). Not text, or NUL bytes in
|
|
88
115
|
* what was read → `conversation.artifact_not_previewable`; the object is gone →
|
|
89
116
|
* `conversation.artifact_removed`. A cut never splits a UTF-8 character.
|
|
90
117
|
*/
|
|
@@ -15,10 +15,13 @@ import { StorageService } from "../storage/storage.service.js";
|
|
|
15
15
|
import { UUID_RE } from "../ui/registry.js";
|
|
16
16
|
import { ConversationAccess } from "./access.js";
|
|
17
17
|
import { isInlineArtifactType, isTextArtifactType } from "./artifacts.js";
|
|
18
|
+
import { AttachmentsService } from "./attachments.service.js";
|
|
18
19
|
/**
|
|
19
|
-
* A conversation's
|
|
20
|
-
* (`artifact_created`), grouped by `sourcePath` into versions,
|
|
21
|
-
*
|
|
20
|
+
* A conversation's files (UI refresh B, spec §5.8 / §5.9): the files the agent delivered
|
|
21
|
+
* (`artifact_created`), grouped by `sourcePath` into versions, and the files users sent in it
|
|
22
|
+
* (ready `chat` attachments bound to the conversation), newest first. Read on every request (no
|
|
23
|
+
* cache); `status` comes from the object store. Both kinds are read under the conversation's
|
|
24
|
+
* access: whoever may read the conversation may read the attachments sent in it, nobody else.
|
|
22
25
|
*/
|
|
23
26
|
/** Versions listed per file (newest first). */
|
|
24
27
|
export const FILE_VERSIONS_MAX = 20;
|
|
@@ -28,6 +31,10 @@ export const FILES_PAGE_MAX = 100;
|
|
|
28
31
|
export const FILE_CONTENT_MAX_BYTES = 1024 * 1024;
|
|
29
32
|
const conversationNotFound = () => new ApiError("conversation.not_found", "Conversation not found");
|
|
30
33
|
const artifactNotFound = () => new ApiError("conversation.artifact_not_found", "Artifact not found");
|
|
34
|
+
const removed = () => new ApiError("conversation.artifact_removed", "The file is no longer available");
|
|
35
|
+
/** Whether `a` lists before `b`: newer first, then the larger id (a total, stable order). */
|
|
36
|
+
const before = (a, b) => a.at.getTime() !== b.at.getTime() ? a.at.getTime() > b.at.getTime() : a.id > b.id;
|
|
37
|
+
const previewKind = (contentType) => isInlineArtifactType(contentType) ? "image" : isTextArtifactType(contentType) ? "text" : null;
|
|
31
38
|
/** Drops an incomplete UTF-8 sequence at the end of a cut buffer. */
|
|
32
39
|
function trimUtf8(b) {
|
|
33
40
|
let i = b.length - 1;
|
|
@@ -82,12 +89,16 @@ let ConversationFiles = class ConversationFiles {
|
|
|
82
89
|
db;
|
|
83
90
|
storage;
|
|
84
91
|
access;
|
|
92
|
+
attachments;
|
|
85
93
|
constructor(db, storage,
|
|
86
94
|
/** Same visibility as the event stream. */
|
|
87
|
-
access
|
|
95
|
+
access,
|
|
96
|
+
/** The attachments sent in the conversation, and the rule their stored content must meet. */
|
|
97
|
+
attachments) {
|
|
88
98
|
this.db = db;
|
|
89
99
|
this.storage = storage;
|
|
90
100
|
this.access = access;
|
|
101
|
+
this.attachments = attachments;
|
|
91
102
|
}
|
|
92
103
|
async checkAccess(orgId, conversationId, viewer) {
|
|
93
104
|
if (!UUID_RE.test(conversationId))
|
|
@@ -110,10 +121,49 @@ let ConversationFiles = class ConversationFiles {
|
|
|
110
121
|
}));
|
|
111
122
|
}
|
|
112
123
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
|
|
124
|
+
* One file of the conversation by id: a delivered version first, then an attachment sent in it;
|
|
125
|
+
* neither → `conversation.artifact_not_found`. An attachment carries `upload`, the row its stored
|
|
126
|
+
* object must still match. Call after {@link checkAccess}.
|
|
127
|
+
*/
|
|
128
|
+
async resolve(orgId, conversationId, fileId) {
|
|
129
|
+
if (!UUID_RE.test(fileId))
|
|
130
|
+
throw artifactNotFound();
|
|
131
|
+
const row = await this.db
|
|
132
|
+
.selectFrom("conversation_events")
|
|
133
|
+
.select("payload")
|
|
134
|
+
.where("conversation_id", "=", conversationId)
|
|
135
|
+
.where("type", "=", "artifact_created")
|
|
136
|
+
.where(sql `payload->>'artifactId'`, "=", fileId)
|
|
137
|
+
.limit(1)
|
|
138
|
+
.executeTakeFirst();
|
|
139
|
+
if (row) {
|
|
140
|
+
const a = row.payload;
|
|
141
|
+
return {
|
|
142
|
+
id: a.artifactId,
|
|
143
|
+
name: a.name,
|
|
144
|
+
contentType: a.contentType,
|
|
145
|
+
size: a.size,
|
|
146
|
+
objectKey: a.objectKey,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const [u] = await this.attachments.boundRows(orgId, conversationId, fileId);
|
|
150
|
+
if (!u)
|
|
151
|
+
throw artifactNotFound();
|
|
152
|
+
return {
|
|
153
|
+
id: u.id,
|
|
154
|
+
name: u.name,
|
|
155
|
+
contentType: u.contentType,
|
|
156
|
+
size: u.size,
|
|
157
|
+
objectKey: u.objectKey,
|
|
158
|
+
upload: u,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Files newest first: a delivered file by its **first** delivery, so a new version keeps the
|
|
163
|
+
* file's place and a cursor stays stable while files are re-delivered (review M2); an uploaded
|
|
164
|
+
* file by its upload time; equal times by id. `after` is a file id from the previous page (any
|
|
165
|
+
* version's id finds its file); a cursor that names no file gives an empty page. Storage HEADs run
|
|
166
|
+
* at most {@link HEAD_CONCURRENCY} at a time.
|
|
117
167
|
*/
|
|
118
168
|
async list(orgId, conversationId, viewer, opts) {
|
|
119
169
|
await this.checkAccess(orgId, conversationId, viewer);
|
|
@@ -124,18 +174,65 @@ let ConversationFiles = class ConversationFiles {
|
|
|
124
174
|
g.push(d);
|
|
125
175
|
groups.set(d.sourcePath, g);
|
|
126
176
|
}
|
|
127
|
-
const
|
|
177
|
+
const delivered = [...groups.values()].reverse().map((g) => {
|
|
178
|
+
const first = g[0];
|
|
179
|
+
return { origin: "delivered", at: first.createdAt, id: first.artifactId, group: g };
|
|
180
|
+
});
|
|
181
|
+
// newest first, the larger id first at equal times
|
|
182
|
+
const bound = (await this.attachments.boundRows(orgId, conversationId)).reverse();
|
|
183
|
+
const uploaded = bound.map((u) => ({
|
|
184
|
+
origin: "uploaded",
|
|
185
|
+
at: u.createdAt,
|
|
186
|
+
id: u.id,
|
|
187
|
+
upload: u,
|
|
188
|
+
}));
|
|
189
|
+
// merge the two newest-first lists (each keeps its own order)
|
|
190
|
+
const ordered = [];
|
|
191
|
+
let i = 0;
|
|
192
|
+
let j = 0;
|
|
193
|
+
while (i < delivered.length || j < uploaded.length) {
|
|
194
|
+
const d = delivered[i];
|
|
195
|
+
const u = uploaded[j];
|
|
196
|
+
if (d && (!u || before(d, u))) {
|
|
197
|
+
ordered.push(d);
|
|
198
|
+
i++;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
ordered.push(u);
|
|
202
|
+
j++;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
128
205
|
let start = 0;
|
|
129
206
|
if (opts.after !== undefined) {
|
|
130
207
|
const after = opts.after;
|
|
131
|
-
const at = ordered.findIndex((
|
|
208
|
+
const at = ordered.findIndex((e) => e.origin === "delivered" ? e.group.some((d) => d.artifactId === after) : e.id === after);
|
|
132
209
|
if (at < 0)
|
|
133
210
|
return { files: [], next: null };
|
|
134
211
|
start = at + 1;
|
|
135
212
|
}
|
|
136
213
|
const page = ordered.slice(start, start + opts.limit);
|
|
137
214
|
const store = this.storage.kernel(orgId);
|
|
138
|
-
const files = await mapLimit(page, HEAD_CONCURRENCY, async (
|
|
215
|
+
const files = await mapLimit(page, HEAD_CONCURRENCY, async (e) => {
|
|
216
|
+
if (e.origin === "uploaded") {
|
|
217
|
+
const u = e.upload;
|
|
218
|
+
const head = await store.head(u.objectKey);
|
|
219
|
+
const same = this.attachments.storedMatches(u, head, { orgId, conversationId });
|
|
220
|
+
return {
|
|
221
|
+
id: u.id,
|
|
222
|
+
name: u.name,
|
|
223
|
+
contentType: u.contentType,
|
|
224
|
+
size: u.size,
|
|
225
|
+
sourcePath: u.name,
|
|
226
|
+
version: 1,
|
|
227
|
+
versions: [{ id: u.id, size: u.size, createdAt: u.createdAt }],
|
|
228
|
+
callId: "",
|
|
229
|
+
createdAt: u.createdAt,
|
|
230
|
+
status: same ? "available" : "removed",
|
|
231
|
+
previewable: previewKind(u.contentType),
|
|
232
|
+
origin: "uploaded",
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const g = e.group;
|
|
139
236
|
const latest = g.at(-1);
|
|
140
237
|
const head = await store.head(latest.objectKey);
|
|
141
238
|
return {
|
|
@@ -152,64 +249,39 @@ let ConversationFiles = class ConversationFiles {
|
|
|
152
249
|
callId: latest.callId,
|
|
153
250
|
createdAt: latest.createdAt,
|
|
154
251
|
status: head ? "available" : "removed",
|
|
155
|
-
previewable:
|
|
156
|
-
|
|
157
|
-
: isTextArtifactType(latest.contentType)
|
|
158
|
-
? "text"
|
|
159
|
-
: null,
|
|
252
|
+
previewable: previewKind(latest.contentType),
|
|
253
|
+
origin: "delivered",
|
|
160
254
|
};
|
|
161
255
|
});
|
|
162
256
|
const next = start + opts.limit < ordered.length ? (files.at(-1)?.id ?? null) : null;
|
|
163
257
|
return { files, next };
|
|
164
258
|
}
|
|
165
259
|
/**
|
|
166
|
-
* One file version of the conversation (any version's id
|
|
167
|
-
* the viewer can read the conversation. For the kernel only
|
|
168
|
-
* never leaves the kernel.
|
|
260
|
+
* One file version of the conversation (any version's id, or an attachment sent in it) with its
|
|
261
|
+
* object key, after checking the viewer can read the conversation. For the kernel only
|
|
262
|
+
* (previewers, embed grants): the key never leaves the kernel. An attachment whose stored object
|
|
263
|
+
* is gone or no longer the one finalize checked → `conversation.artifact_removed`; `recorded` is
|
|
264
|
+
* then what a later read of the object must still match ({@link attachmentContentMatches}).
|
|
169
265
|
*/
|
|
170
266
|
async file(orgId, conversationId, viewer, fileId) {
|
|
171
267
|
await this.checkAccess(orgId, conversationId, viewer);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
.where(sql `payload->>'artifactId'`, "=", fileId)
|
|
180
|
-
.limit(1)
|
|
181
|
-
.executeTakeFirst();
|
|
182
|
-
if (!row)
|
|
183
|
-
throw artifactNotFound();
|
|
184
|
-
const a = row.payload;
|
|
185
|
-
return {
|
|
186
|
-
id: a.artifactId,
|
|
187
|
-
name: a.name,
|
|
188
|
-
contentType: a.contentType,
|
|
189
|
-
size: a.size,
|
|
190
|
-
objectKey: a.objectKey,
|
|
191
|
-
};
|
|
268
|
+
const { upload, ...f } = await this.resolve(orgId, conversationId, fileId);
|
|
269
|
+
if (!upload)
|
|
270
|
+
return f;
|
|
271
|
+
const head = await this.storage.kernel(orgId).head(upload.objectKey);
|
|
272
|
+
if (!this.attachments.storedMatches(upload, head, { orgId, conversationId }))
|
|
273
|
+
throw removed();
|
|
274
|
+
return { ...f, recorded: { size: upload.size, etag: upload.etag } };
|
|
192
275
|
}
|
|
193
276
|
/**
|
|
194
|
-
* The first `limit` bytes of a text file version (any version's id
|
|
277
|
+
* The first `limit` bytes of a text file version (any version's id, or an attachment sent in the
|
|
278
|
+
* conversation). Not text, or NUL bytes in
|
|
195
279
|
* what was read → `conversation.artifact_not_previewable`; the object is gone →
|
|
196
280
|
* `conversation.artifact_removed`. A cut never splits a UTF-8 character.
|
|
197
281
|
*/
|
|
198
282
|
async content(orgId, conversationId, viewer, fileId, limit) {
|
|
199
283
|
await this.checkAccess(orgId, conversationId, viewer);
|
|
200
|
-
|
|
201
|
-
throw artifactNotFound();
|
|
202
|
-
const row = await this.db
|
|
203
|
-
.selectFrom("conversation_events")
|
|
204
|
-
.select("payload")
|
|
205
|
-
.where("conversation_id", "=", conversationId)
|
|
206
|
-
.where("type", "=", "artifact_created")
|
|
207
|
-
.where(sql `payload->>'artifactId'`, "=", fileId)
|
|
208
|
-
.limit(1)
|
|
209
|
-
.executeTakeFirst();
|
|
210
|
-
if (!row)
|
|
211
|
-
throw artifactNotFound();
|
|
212
|
-
const a = row.payload;
|
|
284
|
+
const a = await this.resolve(orgId, conversationId, fileId);
|
|
213
285
|
if (!isTextArtifactType(a.contentType)) {
|
|
214
286
|
throw new ApiError("conversation.artifact_not_previewable", "Only text files have content");
|
|
215
287
|
}
|
|
@@ -218,7 +290,13 @@ let ConversationFiles = class ConversationFiles {
|
|
|
218
290
|
.kernel(orgId)
|
|
219
291
|
.get(a.objectKey, { range: { start: 0, end: max - 1 } });
|
|
220
292
|
if (!got)
|
|
221
|
-
throw
|
|
293
|
+
throw removed();
|
|
294
|
+
// an attachment whose object changed after finalize is not the upload any more
|
|
295
|
+
if (a.upload &&
|
|
296
|
+
!this.attachments.storedMatches(a.upload, got.head, { orgId, conversationId })) {
|
|
297
|
+
await got.body.cancel().catch(() => { });
|
|
298
|
+
throw removed();
|
|
299
|
+
}
|
|
222
300
|
let body = await readPrefix(got.body, max);
|
|
223
301
|
if (body.includes(0)) {
|
|
224
302
|
throw new ApiError("conversation.artifact_not_previewable", "This file contains binary data and has no text content");
|
|
@@ -234,6 +312,7 @@ ConversationFiles = __decorate([
|
|
|
234
312
|
Injectable(),
|
|
235
313
|
__param(0, Inject(KernelDb)),
|
|
236
314
|
__param(1, Inject(StorageService)),
|
|
237
|
-
__param(2, Inject(ConversationAccess))
|
|
315
|
+
__param(2, Inject(ConversationAccess)),
|
|
316
|
+
__param(3, Inject(AttachmentsService))
|
|
238
317
|
], ConversationFiles);
|
|
239
318
|
export { ConversationFiles };
|
package/dist/ctx/create-ctx.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { HookRunner } from "../runtime/hook-calls.js";
|
|
|
10
10
|
import type { EmbedTokens } from "../ui/embed-token.js";
|
|
11
11
|
import { type UiActionHandler, type UiRender } from "../ui/registry.js";
|
|
12
12
|
import { type BusSubscriber } from "./bus.js";
|
|
13
|
+
import { type ExtensionNetServices } from "./net.js";
|
|
13
14
|
import { type Keyring } from "./secrets.js";
|
|
14
15
|
/** 内核提供给插件宿主的共享依赖。 */
|
|
15
16
|
export interface KernelDeps {
|
|
@@ -30,6 +31,11 @@ export interface KernelDeps {
|
|
|
30
31
|
* test): `ctx.models` calls reject.
|
|
31
32
|
*/
|
|
32
33
|
models?: Pick<ModelEmbeddings, "embed" | "describe">;
|
|
34
|
+
/**
|
|
35
|
+
* What `ctx.net` reads per call: the kernel config (`net.*`) and the org network allowances.
|
|
36
|
+
* Absent (a ctx built alone in a test): `ctx.net.fetch` rejects every request.
|
|
37
|
+
*/
|
|
38
|
+
net?: ExtensionNetServices;
|
|
33
39
|
/**
|
|
34
40
|
* The host's hook registry: `ctx.models.embed` fires `model.request` through it (spec B §5.4).
|
|
35
41
|
* A required key, so no builder leaves policy hooks out by omission (fail closed by
|
|
@@ -157,7 +163,8 @@ export declare class LiveConfig {
|
|
|
157
163
|
* registered routes, provided extension points and a close function.
|
|
158
164
|
* `deploymentConfig` may be a {@link LiveConfig} (shared by the instance, replaceable in place);
|
|
159
165
|
* otherwise a fixed value is wrapped.
|
|
160
|
-
* - Granted automatically: `kv`, `lock`, `bus`, `queue`, `log`, `db`, `config`, `provide
|
|
166
|
+
* - Granted automatically: `kv`, `lock`, `bus`, `queue`, `log`, `db`, `config`, `provide`, `net`
|
|
167
|
+
* (through `deps.net`, see `ctx/net.ts`);
|
|
161
168
|
* - `secrets` only with `permissions.secrets`, `storage` only with `permissions.storage`, `models`
|
|
162
169
|
* only with `permissions.models` (and only its `embedding` purposes, through `deps.models`); `http.route`
|
|
163
170
|
* only the method + path declared in `permissions.http` (the record carries the declared access and
|
package/dist/ctx/create-ctx.js
CHANGED
|
@@ -11,6 +11,7 @@ import { canUseSlot, orgUiTopic, PLATFORM_UI_TOPIC, PluginUiRegistration, } from
|
|
|
11
11
|
import { createBus } from "./bus.js";
|
|
12
12
|
import { createKv } from "./kv.js";
|
|
13
13
|
import { createLock } from "./lock.js";
|
|
14
|
+
import { createExtensionNet } from "./net.js";
|
|
14
15
|
import { createQueue } from "./queue.js";
|
|
15
16
|
import { createSecrets } from "./secrets.js";
|
|
16
17
|
/**
|
|
@@ -180,7 +181,8 @@ export class LiveConfig {
|
|
|
180
181
|
* registered routes, provided extension points and a close function.
|
|
181
182
|
* `deploymentConfig` may be a {@link LiveConfig} (shared by the instance, replaceable in place);
|
|
182
183
|
* otherwise a fixed value is wrapped.
|
|
183
|
-
* - Granted automatically: `kv`, `lock`, `bus`, `queue`, `log`, `db`, `config`, `provide
|
|
184
|
+
* - Granted automatically: `kv`, `lock`, `bus`, `queue`, `log`, `db`, `config`, `provide`, `net`
|
|
185
|
+
* (through `deps.net`, see `ctx/net.ts`);
|
|
184
186
|
* - `secrets` only with `permissions.secrets`, `storage` only with `permissions.storage`, `models`
|
|
185
187
|
* only with `permissions.models` (and only its `embedding` purposes, through `deps.models`); `http.route`
|
|
186
188
|
* only the method + path declared in `permissions.http` (the record carries the declared access and
|
|
@@ -399,6 +401,15 @@ export function createExtensionContext(deps, manifest, deploymentConfig, extensi
|
|
|
399
401
|
},
|
|
400
402
|
},
|
|
401
403
|
extensionPoints,
|
|
404
|
+
// the kernel's guarded client for person-supplied URLs, no permission needed; without the
|
|
405
|
+
// services (a ctx built alone in a test) it refuses rather than reaching the network unguarded
|
|
406
|
+
net: deps.net
|
|
407
|
+
? createExtensionNet({ ...deps.net, log: deps.log, plugin: name, signal: lifecycle.signal })
|
|
408
|
+
: {
|
|
409
|
+
async fetch() {
|
|
410
|
+
throw new Error(`plugin ${name}: ctx.net is not available`);
|
|
411
|
+
},
|
|
412
|
+
},
|
|
402
413
|
bus: {
|
|
403
414
|
publish: bus.publish,
|
|
404
415
|
async subscribe(topic, handler) {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type ExtensionNet, type Logger } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import type { Agent } from "undici";
|
|
3
|
+
import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
4
|
+
import { type LookupFn } from "../net/public-address.js";
|
|
5
|
+
import type { OrgNetworkAllowances } from "../orgs/network-allowances.service.js";
|
|
6
|
+
/** Tests only: `safeFetch`'s DNS and dispatcher seams for `ctx.net`. */
|
|
7
|
+
export type ExtensionNetSeams = {
|
|
8
|
+
lookup?: LookupFn;
|
|
9
|
+
dispatcherFor?: (ips: string[]) => Agent;
|
|
10
|
+
proxyDispatcherFor?: (proxyUrl: string) => Agent;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Optional provider (a test seam, `KernelAppOptions.overrides`; the kernel never provides it):
|
|
14
|
+
* `ctx.net`'s DNS and dispatcher seams for every extension the host loads, so a kernel test can
|
|
15
|
+
* reach a fake server through a hostname that resolves to a private address.
|
|
16
|
+
*/
|
|
17
|
+
export declare const EXTENSION_NET_SEAMS: unique symbol;
|
|
18
|
+
/** The kernel services `ctx.net` reads per call (built once by the extension host). */
|
|
19
|
+
export type ExtensionNetServices = {
|
|
20
|
+
config: Pick<KernelConfigService, "get">;
|
|
21
|
+
allowances: Pick<OrgNetworkAllowances, "policyFor">;
|
|
22
|
+
/** Tests only: `safeFetch`'s DNS and dispatcher seams. */
|
|
23
|
+
seams?: ExtensionNetSeams;
|
|
24
|
+
};
|
|
25
|
+
/** What a refused private address tells the reader of an org-bound request. */
|
|
26
|
+
export declare const ORG_PRIVATE_HINT = "ask a super admin to allow this internal endpoint for the organization";
|
|
27
|
+
/** What a refused private address tells the reader of a request made outside an organization. */
|
|
28
|
+
export declare const NO_ORG_PRIVATE_HINT = "a request made outside an organization reaches public addresses only";
|
|
29
|
+
/**
|
|
30
|
+
* `ctx.net` (plugin safe fetch spec): the kernel's guarded client (`safeFetch`) for URLs a person
|
|
31
|
+
* supplied. Per call:
|
|
32
|
+
* - the policy is built fresh: `net.allowInsecureLoopback` (tests), the org's allowances read from
|
|
33
|
+
* PG when `opts.orgId` is given (no cache: a removal applies to the next request on every
|
|
34
|
+
* replica; another org's allowances never apply), public addresses only without an org, and the
|
|
35
|
+
* deployment's egress proxy (`net.httpsProxy` / `net.noProxy`: a proxied https request is
|
|
36
|
+
* resolved by the proxy, which then owns SSRF protection; IP-literal targets are still checked);
|
|
37
|
+
* - loopback, link-local and cloud metadata addresses are refused whatever the allowances
|
|
38
|
+
* (loopback allowed only under `net.allowInsecureLoopback`);
|
|
39
|
+
* - redirects are never followed: `redirect: "manual"` returns the 3xx response, otherwise a 3xx
|
|
40
|
+
* rejects with `net.redirect`;
|
|
41
|
+
* - no body size cap (bodies stream); `init.signal` is combined with `ctx.signal`;
|
|
42
|
+
* - a refusal rejects with the SDK's `NetDeniedError` and is logged at `warn` with the plugin, org,
|
|
43
|
+
* host and code (never the URL's path, query or headers).
|
|
44
|
+
*/
|
|
45
|
+
export declare function createExtensionNet(deps: ExtensionNetServices & {
|
|
46
|
+
log: Pick<Logger, "warn">;
|
|
47
|
+
plugin: string;
|
|
48
|
+
/** `ctx.signal`: aborts every request of the instance. */
|
|
49
|
+
signal: AbortSignal;
|
|
50
|
+
}): ExtensionNet;
|
package/dist/ctx/net.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { NetDeniedError as SdkNetDeniedError, } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { NetDeniedError } from "../net/public-address.js";
|
|
3
|
+
import { safeFetch } from "../net/safe-fetch.js";
|
|
4
|
+
/**
|
|
5
|
+
* Optional provider (a test seam, `KernelAppOptions.overrides`; the kernel never provides it):
|
|
6
|
+
* `ctx.net`'s DNS and dispatcher seams for every extension the host loads, so a kernel test can
|
|
7
|
+
* reach a fake server through a hostname that resolves to a private address.
|
|
8
|
+
*/
|
|
9
|
+
export const EXTENSION_NET_SEAMS = Symbol("EXTENSION_NET_SEAMS");
|
|
10
|
+
/** What a refused private address tells the reader of an org-bound request. */
|
|
11
|
+
export const ORG_PRIVATE_HINT = "ask a super admin to allow this internal endpoint for the organization";
|
|
12
|
+
/** What a refused private address tells the reader of a request made outside an organization. */
|
|
13
|
+
export const NO_ORG_PRIVATE_HINT = "a request made outside an organization reaches public addresses only";
|
|
14
|
+
/**
|
|
15
|
+
* `ctx.net` (plugin safe fetch spec): the kernel's guarded client (`safeFetch`) for URLs a person
|
|
16
|
+
* supplied. Per call:
|
|
17
|
+
* - the policy is built fresh: `net.allowInsecureLoopback` (tests), the org's allowances read from
|
|
18
|
+
* PG when `opts.orgId` is given (no cache: a removal applies to the next request on every
|
|
19
|
+
* replica; another org's allowances never apply), public addresses only without an org, and the
|
|
20
|
+
* deployment's egress proxy (`net.httpsProxy` / `net.noProxy`: a proxied https request is
|
|
21
|
+
* resolved by the proxy, which then owns SSRF protection; IP-literal targets are still checked);
|
|
22
|
+
* - loopback, link-local and cloud metadata addresses are refused whatever the allowances
|
|
23
|
+
* (loopback allowed only under `net.allowInsecureLoopback`);
|
|
24
|
+
* - redirects are never followed: `redirect: "manual"` returns the 3xx response, otherwise a 3xx
|
|
25
|
+
* rejects with `net.redirect`;
|
|
26
|
+
* - no body size cap (bodies stream); `init.signal` is combined with `ctx.signal`;
|
|
27
|
+
* - a refusal rejects with the SDK's `NetDeniedError` and is logged at `warn` with the plugin, org,
|
|
28
|
+
* host and code (never the URL's path, query or headers).
|
|
29
|
+
*/
|
|
30
|
+
export function createExtensionNet(deps) {
|
|
31
|
+
return {
|
|
32
|
+
async fetch(url, init, opts) {
|
|
33
|
+
const orgId = opts?.orgId;
|
|
34
|
+
const net = deps.config.get().net;
|
|
35
|
+
const exemptions = orgId
|
|
36
|
+
? await deps.allowances.policyFor(orgId)
|
|
37
|
+
: { allowPrivateHosts: [], trustedOrigins: [] };
|
|
38
|
+
const policy = {
|
|
39
|
+
allowInsecureLoopback: net.allowInsecureLoopback,
|
|
40
|
+
allowPrivateHosts: exemptions.allowPrivateHosts,
|
|
41
|
+
trustedOrigins: exemptions.trustedOrigins,
|
|
42
|
+
...(net.httpsProxy !== undefined && {
|
|
43
|
+
proxy: { url: net.httpsProxy, noProxy: net.noProxy },
|
|
44
|
+
}),
|
|
45
|
+
extension: { privateHint: orgId ? ORG_PRIVATE_HINT : NO_ORG_PRIVATE_HINT },
|
|
46
|
+
};
|
|
47
|
+
const signal = init?.signal ? AbortSignal.any([deps.signal, init.signal]) : deps.signal;
|
|
48
|
+
try {
|
|
49
|
+
return await safeFetch(url, { ...init, signal }, policy, {
|
|
50
|
+
redirects: init?.redirect === "manual" ? "return" : "refuse",
|
|
51
|
+
...deps.seams,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
if (!(e instanceof NetDeniedError))
|
|
56
|
+
throw e;
|
|
57
|
+
deps.log.warn("ctx.net.fetch refused a request", {
|
|
58
|
+
plugin: deps.plugin,
|
|
59
|
+
orgId: orgId ?? null,
|
|
60
|
+
host: e.host,
|
|
61
|
+
code: e.code,
|
|
62
|
+
});
|
|
63
|
+
throw new SdkNetDeniedError(e.code, e.host, e.message, e.status);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -156,6 +156,15 @@ export interface OrgToolSwitchesTable {
|
|
|
156
156
|
updated_by: string | null;
|
|
157
157
|
updated_at: Ts;
|
|
158
158
|
}
|
|
159
|
+
/** 0145: an internal endpoint a super admin lets the org's extension requests reach. */
|
|
160
|
+
export interface OrgNetworkAllowancesTable {
|
|
161
|
+
id: string;
|
|
162
|
+
org_id: string;
|
|
163
|
+
/** A hostname, `*.domain`, IP, CIDR or origin `http(s)://host[:port]`, normalized. */
|
|
164
|
+
entry: string;
|
|
165
|
+
created_by: string | null;
|
|
166
|
+
created_at: Ts;
|
|
167
|
+
}
|
|
159
168
|
export interface AuditEventsTable {
|
|
160
169
|
id: Generated<string>;
|
|
161
170
|
at: Ts;
|
|
@@ -1316,6 +1325,7 @@ export interface KernelDatabase {
|
|
|
1316
1325
|
extension_org_configs: ExtensionOrgConfigsTable;
|
|
1317
1326
|
extension_org_overrides: ExtensionOrgOverridesTable;
|
|
1318
1327
|
org_tool_switches: OrgToolSwitchesTable;
|
|
1328
|
+
org_network_allowances: OrgNetworkAllowancesTable;
|
|
1319
1329
|
audit_events: AuditEventsTable;
|
|
1320
1330
|
secrets: SecretsTable;
|
|
1321
1331
|
agents: AgentsTable;
|
|
@@ -15,7 +15,7 @@ import { ExtensionDisplayNames } from "../extensions/display-name.js";
|
|
|
15
15
|
import { audit } from "../identity/audit.js";
|
|
16
16
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
17
17
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
18
|
+
import { assertNotSharedAdmission, TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
19
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
20
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
21
21
|
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
@@ -896,6 +896,7 @@ let DevicesService = class DevicesService {
|
|
|
896
896
|
* member (any more) → `org.not_found`, as the org guard answers.
|
|
897
897
|
*/
|
|
898
898
|
async lockMember(orgId, accountId, mode = "update") {
|
|
899
|
+
assertNotSharedAdmission("locking a membership");
|
|
899
900
|
const org = await this.db
|
|
900
901
|
.selectFrom("orgs")
|
|
901
902
|
.select("id")
|
package/dist/errors.d.ts
CHANGED
|
@@ -37,6 +37,11 @@ export declare const ERROR_CODES: {
|
|
|
37
37
|
readonly "org.sandbox_egress_not_allowed": 400;
|
|
38
38
|
/** The org asked for auto mode where the platform turned it off, or widened it through a token. */
|
|
39
39
|
readonly "org.auto_mode_not_allowed": 400;
|
|
40
|
+
/** An org network allowance entry that is malformed, names a link-local address, or is over the cap. */
|
|
41
|
+
readonly "org.network_allowance_invalid": 400;
|
|
42
|
+
readonly "org.network_allowance_not_found": 404;
|
|
43
|
+
/** Adding an org network allowance through an API token or the Platform tool (a person widens). */
|
|
44
|
+
readonly "org.network_allowance_widen_not_allowed": 400;
|
|
40
45
|
/** A deployment setting set in the startup config (file, Nacos or env): read-only in the admin console. */
|
|
41
46
|
readonly "deployment.setting_readonly": 409;
|
|
42
47
|
/** Turning the platform's auto mode on through an API token or the Platform tool (only off). */
|
package/dist/errors.js
CHANGED
|
@@ -37,6 +37,11 @@ export const ERROR_CODES = {
|
|
|
37
37
|
"org.sandbox_egress_not_allowed": 400,
|
|
38
38
|
/** The org asked for auto mode where the platform turned it off, or widened it through a token. */
|
|
39
39
|
"org.auto_mode_not_allowed": 400,
|
|
40
|
+
/** An org network allowance entry that is malformed, names a link-local address, or is over the cap. */
|
|
41
|
+
"org.network_allowance_invalid": 400,
|
|
42
|
+
"org.network_allowance_not_found": 404,
|
|
43
|
+
/** Adding an org network allowance through an API token or the Platform tool (a person widens). */
|
|
44
|
+
"org.network_allowance_widen_not_allowed": 400,
|
|
40
45
|
/** A deployment setting set in the startup config (file, Nacos or env): read-only in the admin console. */
|
|
41
46
|
"deployment.setting_readonly": 409,
|
|
42
47
|
/** Turning the platform's auto mode on through an API token or the Platform tool (only off). */
|