@segmentstream/agent 0.45.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +25 -3
  3. package/dist/_chunks/{build-metadata-s0t4FPPK.mjs → build-metadata-5S6uTvyu.mjs} +1 -1
  4. package/dist/_chunks/{inbound-request-CK0QqEOP.mjs → inbound-request-B3RhCuOw.mjs} +6 -11
  5. package/dist/_chunks/{principal-BXvelsZk.mjs → principal-Dzs8hUDB.mjs} +1 -1
  6. package/dist/extension/config.d.ts +10 -0
  7. package/dist/extension/config.mjs +11 -0
  8. package/dist/extension/extension.d.ts +5 -0
  9. package/dist/extension/extension.mjs +2 -1
  10. package/dist/extension/hooks/authorization.mjs +1 -1
  11. package/dist/extension/hooks/workspace-source.d.ts +6 -0
  12. package/dist/extension/hooks/workspace-source.mjs +10 -0
  13. package/dist/extension/instructions/persona.mjs +1 -1
  14. package/dist/extension/instructions/sandbox.mjs +16 -2
  15. package/dist/extension/lib/actor.mjs +6 -2
  16. package/dist/extension/lib/attachment-tools.mjs +1 -1
  17. package/dist/extension/lib/build-metadata.mjs +1 -1
  18. package/dist/extension/lib/chat-address.d.ts +1 -1
  19. package/dist/extension/lib/chat-address.mjs +2 -2
  20. package/dist/extension/lib/chat-context.mjs +1 -1
  21. package/dist/extension/lib/chat-recorder.mjs +12 -1
  22. package/dist/extension/lib/confirmations.mjs +1 -1
  23. package/dist/extension/lib/ontology-connection.mjs +8 -4
  24. package/dist/extension/lib/ontology-file-call.mjs +1 -1
  25. package/dist/extension/lib/ontology-tools.mjs +1 -1
  26. package/dist/extension/lib/review/hold.mjs +2 -2
  27. package/dist/extension/lib/review/reply.mjs +1 -1
  28. package/dist/extension/lib/role-skill.mjs +1 -1
  29. package/dist/extension/lib/runtime-envelope.mjs +1 -1
  30. package/dist/extension/lib/slack/attachments-send.mjs +1 -1
  31. package/dist/extension/lib/stored-files.mjs +1 -1
  32. package/dist/extension/lib/telegram/adapter.mjs +15 -28
  33. package/dist/extension/lib/telegram/app-links.d.ts +22 -0
  34. package/dist/extension/lib/telegram/app-links.mjs +27 -1
  35. package/dist/extension/lib/telegram/attachment-description.mjs +1 -1
  36. package/dist/extension/lib/telegram/attachments-send.d.ts +3 -0
  37. package/dist/extension/lib/telegram/attachments-send.mjs +16 -4
  38. package/dist/extension/lib/telegram/bot.d.ts +17 -0
  39. package/dist/extension/lib/telegram/bot.mjs +21 -1
  40. package/dist/extension/lib/telegram/callbacks.mjs +2 -2
  41. package/dist/extension/lib/telegram/chat-archive.mjs +1 -1
  42. package/dist/extension/lib/telegram/confirmations.mjs +2 -2
  43. package/dist/extension/lib/telegram/customer-text.mjs +1 -1
  44. package/dist/extension/lib/telegram/files.mjs +2 -2
  45. package/dist/extension/lib/telegram/health.mjs +1 -1
  46. package/dist/extension/lib/telegram/images.mjs +1 -1
  47. package/dist/extension/lib/telegram/inbound-request.mjs +1 -1
  48. package/dist/extension/lib/telegram/inbound.mjs +2 -2
  49. package/dist/extension/lib/telegram/lifecycle.mjs +3 -3
  50. package/dist/extension/lib/telegram/media-policy.mjs +1 -1
  51. package/dist/extension/lib/telegram/principal.mjs +1 -1
  52. package/dist/extension/lib/telegram/session-recovery.mjs +1 -1
  53. package/dist/extension/lib/telegram/silent.mjs +1 -1
  54. package/dist/extension/lib/telegram/structured.mjs +1 -1
  55. package/dist/extension/lib/telegram/turn-errors.mjs +2 -2
  56. package/dist/extension/lib/telegram/voice.mjs +1 -1
  57. package/dist/extension/lib/web/auth.d.ts +60 -0
  58. package/dist/extension/lib/web/auth.mjs +130 -0
  59. package/dist/extension/lib/web/channel.d.ts +15 -0
  60. package/dist/extension/lib/web/channel.mjs +18 -0
  61. package/dist/extension/lib/workspace-source.d.ts +130 -0
  62. package/dist/extension/lib/workspace-source.mjs +451 -0
  63. package/dist/extension/model.mjs +1 -1
  64. package/dist/extension/tools/attachments_promote.mjs +1 -1
  65. package/dist/extension/tools/attachments_send.mjs +3 -1
  66. package/dist/extension/tools/questions_ask.mjs +1 -1
  67. package/dist/extension/tools/workspace_source.d.ts +2 -0
  68. package/dist/extension/tools/workspace_source.mjs +44 -0
  69. package/dist/tools/index.d.ts +1 -0
  70. package/dist/tools/index.mjs +2 -1
  71. package/package.json +7 -4
@@ -0,0 +1,451 @@
1
+ import { fileURLToPath as __eveFileURLToPath } from "node:url";
2
+ import { dirname as __eveDirname, posix } from "node:path";
3
+ import { createRequire as __eveCreateRequire } from "node:module";
4
+ const __filename = __eveFileURLToPath(import.meta.url);
5
+ __eveDirname(__filename);
6
+ __eveCreateRequire(import.meta.url);
7
+ import { actorHeaders, actorOf } from "./actor.mjs";
8
+ import { CHAT_HEADER, SESSION_HEADER } from "./attachments.mjs";
9
+ import { chatAddressOf } from "./chat-address.mjs";
10
+ import { sandboxRecord } from "./sandbox-session.mjs";
11
+ import { defineState } from "eve/context";
12
+ import { gunzipSync } from "node:zlib";
13
+ const WORKSPACE_SOURCE_DIRECTORY = "/workspace/source";
14
+ const WORKSPACE_SOURCE_STAGING_DIRECTORY = "/workspace/source.tmp";
15
+ const WORKSPACE_SOURCE_PAGES_DIRECTORY = "/workspace/.segmentstream/source-pages";
16
+ const WORKSPACE_SOURCE_MARKER_PATH = "/workspace/.segmentstream/source.json";
17
+ const WORKSPACE_SOURCE_ROUTE = "/api/ontology/agent/source";
18
+ const COMMIT_HEADER = "x-segmentstream-source-commit";
19
+ const FILES_HEADER = "x-segmentstream-source-files";
20
+ const SKIPPED_HEADER = "x-segmentstream-source-skipped";
21
+ const NEXT_HEADER = "x-segmentstream-source-next";
22
+ const COMMIT = /^[0-9a-f]{40}$/;
23
+ const COUNT = /^(?:0|[1-9][0-9]{0,8})$/;
24
+ const PAGE_TIMEOUT_MS = 6e4;
25
+ const MAXIMUM_PAGE_BYTES = 8388608;
26
+ const MAXIMUM_ARCHIVE_BYTES = 33554432;
27
+ const MAXIMUM_PAGES = 200;
28
+ const MAXIMUM_CURSOR_LENGTH = 4096;
29
+ const workspaceSourceRecord = defineState("segmentstream.workspaceSource", () => null);
30
+ var WorkspaceSourceError = class extends Error {
31
+ name = "WorkspaceSourceError";
32
+ code;
33
+ status;
34
+ requestId;
35
+ constructor(input, options) {
36
+ super(`${input.code}: ${input.message}` + (input.requestId === void 0 ? "" : ` request_id ${input.requestId}.`), options);
37
+ this.code = input.code;
38
+ this.status = input.status;
39
+ this.requestId = input.requestId;
40
+ }
41
+ };
42
+ var SourceChanged = class extends Error {};
43
+ function workspaceSourceReader(input) {
44
+ const actor = actorOf(input.principal);
45
+ const chat = chatAddressOf(input.principal);
46
+ if (actor?.kind !== "member") throw new WorkspaceSourceError({
47
+ code: "member_required",
48
+ message: "the workspace's code is copied only for a member."
49
+ });
50
+ return {
51
+ memberId: actor.memberId,
52
+ actAs: actorHeaders({
53
+ actor,
54
+ principal: input.principal
55
+ }),
56
+ turnId: input.turnId,
57
+ ...chat === void 0 ? {} : { chat: `${chat.provider}:${chat.chatId}` },
58
+ sessionId: input.sessionId
59
+ };
60
+ }
61
+ function isRecord(value) {
62
+ return typeof value === "object" && value !== null && !Array.isArray(value);
63
+ }
64
+ function errorCode(body) {
65
+ if (!isRecord(body)) return;
66
+ const code = typeof body.error === "string" ? body.error : body.code;
67
+ if (typeof code === "string") return code;
68
+ return isRecord(body.error) && typeof body.error.code === "string" ? body.error.code : void 0;
69
+ }
70
+ function detailOf(body) {
71
+ const detail = isRecord(body) ? body.detail : void 0;
72
+ return typeof detail === "string" && detail.trim() !== "" ? detail.trim().slice(0, 300) : void 0;
73
+ }
74
+ function requestIdOf(body) {
75
+ const value = isRecord(body) ? body.requestId ?? body.request_id : void 0;
76
+ return typeof value === "string" && value !== "" ? value : void 0;
77
+ }
78
+ function invalidPage(message, options) {
79
+ return new WorkspaceSourceError({
80
+ code: "workspace_source_invalid",
81
+ message
82
+ }, options);
83
+ }
84
+ function unreachable(cause) {
85
+ return new WorkspaceSourceError({
86
+ code: "workspace_source_unreachable",
87
+ message: `${WORKSPACE_SOURCE_ROUTE} did not answer.`
88
+ }, { cause });
89
+ }
90
+ async function refused(response) {
91
+ const body = await response.json().catch(() => void 0);
92
+ const code = errorCode(body);
93
+ const detail = detailOf(body);
94
+ const requestId = requestIdOf(body);
95
+ const status = response.status;
96
+ const failure = (failureCode, message) => new WorkspaceSourceError({
97
+ code: failureCode,
98
+ message: detail === void 0 ? message : `${message} ${detail}`,
99
+ status,
100
+ ...requestId === void 0 ? {} : { requestId }
101
+ });
102
+ if (status === 409 && code === "workspace_source_changed") throw new SourceChanged();
103
+ if (status === 403) throw code === "permission_denied" ? failure(code, "the member may not read this workspace's code (workspace_source:read).") : failure(code ?? "forbidden", "the ontology refused the member (HTTP 403).");
104
+ if ((status === 409 || status === 503) && code === "workspace_source_unavailable") throw failure(code, `the workspace has no deployed source to copy (HTTP ${status}).`);
105
+ if (status === 404 && code === void 0) throw failure("not_found", `the ontology does not serve ${WORKSPACE_SOURCE_ROUTE}.`);
106
+ throw failure(code ?? "workspace_source_failed", `the ontology answered HTTP ${status}.`);
107
+ }
108
+ function header(response, name) {
109
+ const value = response.headers.get(name);
110
+ return value === null ? void 0 : value.trim();
111
+ }
112
+ function count(response, name) {
113
+ const value = header(response, name) ?? "";
114
+ if (!COUNT.test(value)) throw invalidPage(`${name} is not a count.`);
115
+ return Number(value);
116
+ }
117
+ async function boundedBody(response, maximum) {
118
+ if (response.body === null) return new Uint8Array();
119
+ const reader = response.body.getReader();
120
+ const chunks = [];
121
+ let size = 0;
122
+ try {
123
+ for (;;) {
124
+ const next = await reader.read();
125
+ if (next.done) break;
126
+ size += next.value.byteLength;
127
+ if (size > maximum) throw invalidPage(`a page is larger than ${maximum / 1024 / 1024} MiB.`);
128
+ chunks.push(next.value);
129
+ }
130
+ } catch (error) {
131
+ throw error instanceof WorkspaceSourceError ? error : unreachable(error);
132
+ } finally {
133
+ await reader.cancel().catch(() => void 0);
134
+ }
135
+ return new Uint8Array(Buffer.concat(chunks));
136
+ }
137
+ async function requestPage(request, cursor) {
138
+ const { reader } = request;
139
+ const query = cursor === void 0 ? "" : `?cursor=${encodeURIComponent(cursor)}`;
140
+ const authorization = `Bearer ${await request.token()}`;
141
+ let response;
142
+ try {
143
+ response = await request.fetch(`${request.origin}${WORKSPACE_SOURCE_ROUTE}${query}`, {
144
+ headers: {
145
+ authorization,
146
+ ...reader.actAs,
147
+ ...reader.chat === void 0 ? {} : { [CHAT_HEADER]: reader.chat },
148
+ ...reader.sessionId === "" ? {} : { [SESSION_HEADER]: reader.sessionId }
149
+ },
150
+ redirect: "error",
151
+ signal: AbortSignal.timeout(PAGE_TIMEOUT_MS)
152
+ });
153
+ } catch (error) {
154
+ throw unreachable(error);
155
+ }
156
+ if (response.status !== 200) return await refused(response);
157
+ const discard = async () => {
158
+ await response.body?.cancel().catch(() => void 0);
159
+ };
160
+ try {
161
+ const commit = header(response, COMMIT_HEADER) ?? "";
162
+ const next = header(response, NEXT_HEADER);
163
+ if (!COMMIT.test(commit)) throw invalidPage(`${COMMIT_HEADER} is not a commit.`);
164
+ if (next !== void 0 && (next === "" || next.length > MAXIMUM_CURSOR_LENGTH)) throw invalidPage(`${NEXT_HEADER} is not a cursor.`);
165
+ return {
166
+ commit,
167
+ files: count(response, FILES_HEADER),
168
+ skipped: count(response, SKIPPED_HEADER),
169
+ next,
170
+ bytes: () => boundedBody(response, MAXIMUM_PAGE_BYTES),
171
+ discard
172
+ };
173
+ } catch (error) {
174
+ await discard();
175
+ throw error;
176
+ }
177
+ }
178
+ const BLOCK = 512;
179
+ function text(block, start, length) {
180
+ const field = block.subarray(start, start + length);
181
+ const end = field.indexOf(0);
182
+ return Buffer.from(end === -1 ? field : field.subarray(0, end)).toString("utf8");
183
+ }
184
+ function octal(block, start, length) {
185
+ const value = text(block, start, length).trim();
186
+ if (!/^[0-7]{1,12}$/.test(value)) throw invalidPage("an archive header has an invalid number.");
187
+ return Number.parseInt(value, 8);
188
+ }
189
+ const PAX_KEYS = new Set([
190
+ "path",
191
+ "mtime",
192
+ "atime",
193
+ "ctime",
194
+ "uid",
195
+ "gid",
196
+ "uname",
197
+ "gname",
198
+ "charset",
199
+ "hdrcharset",
200
+ "comment"
201
+ ]);
202
+ function paxPath(body) {
203
+ let path;
204
+ let offset = 0;
205
+ while (offset < body.length && body[offset] !== 0) {
206
+ const space = body.indexOf(32, offset);
207
+ const digits = space === -1 ? "" : Buffer.from(body.subarray(offset, space)).toString("ascii");
208
+ const length = /^[1-9][0-9]{0,7}$/.test(digits) ? Number(digits) : NaN;
209
+ if (Number.isNaN(length) || offset + length > body.length || body[offset + length - 1] !== 10) throw invalidPage("an archive has an invalid pax header.");
210
+ const record = Buffer.from(body.subarray(space + 1, offset + length - 1)).toString("utf8");
211
+ const equals = record.indexOf("=");
212
+ const key = equals > 0 ? record.slice(0, equals) : "";
213
+ if (!PAX_KEYS.has(key)) throw invalidPage(`an archive has a pax record that is refused (${key}).`);
214
+ if (key === "path") path = record.slice(equals + 1);
215
+ offset += length;
216
+ }
217
+ return path;
218
+ }
219
+ function safeArchivePath(path) {
220
+ if (path === "" || path.startsWith("/") || path.includes("\0") || path.length > 4096) return false;
221
+ return path.split("/").every((segment) => segment !== ".." && Buffer.byteLength(segment, "utf8") <= 255);
222
+ }
223
+ function archivePaths(gzipped) {
224
+ let archive;
225
+ try {
226
+ archive = gunzipSync(gzipped, { maxOutputLength: MAXIMUM_ARCHIVE_BYTES });
227
+ } catch (error) {
228
+ throw invalidPage("a page is not a gzip archive within its size limit.", { cause: error });
229
+ }
230
+ const files = [];
231
+ let pending;
232
+ let offset = 0;
233
+ for (;;) {
234
+ if (offset + BLOCK > archive.length) throw invalidPage("an archive ends without its end-of-archive blocks.");
235
+ const block = archive.subarray(offset, offset + BLOCK);
236
+ if (block.every((byte) => byte === 0)) {
237
+ if (pending !== void 0) throw invalidPage("an archive ends after a pax header.");
238
+ return files;
239
+ }
240
+ let checksum = 0;
241
+ for (let index = 0; index < BLOCK; index++) checksum += index >= 148 && index < 156 ? 32 : block[index];
242
+ if (checksum !== octal(block, 148, 8) || text(block, 257, 5) !== "ustar") throw invalidPage("an archive header is not a valid ustar header.");
243
+ const type = String.fromCharCode(block[156]);
244
+ const size = octal(block, 124, 12);
245
+ const body = offset + BLOCK;
246
+ const end = body + Math.ceil(size / BLOCK) * BLOCK;
247
+ if (end > archive.length) throw invalidPage("an archive entry is truncated.");
248
+ offset = end;
249
+ if (type === "x") {
250
+ pending = paxPath(archive.subarray(body, body + size)) ?? pending;
251
+ continue;
252
+ }
253
+ const prefix = text(block, 345, 155);
254
+ const name = text(block, 0, 100);
255
+ const path = pending ?? (prefix === "" ? name : `${prefix}/${name}`);
256
+ pending = void 0;
257
+ if (type !== "0" && type !== "\0" && type !== "5") throw invalidPage(`an archive entry is neither a file nor a directory (type ${type}).`);
258
+ if (!safeArchivePath(path)) throw invalidPage("an archive entry names a path outside its root.");
259
+ if (type !== "5") {
260
+ if (posix.normalize(path).replace(/\/+$/, "") === ".") throw invalidPage("an archive file has no name.");
261
+ files.push(path);
262
+ }
263
+ }
264
+ }
265
+ function quoted(value) {
266
+ return `'${value.replaceAll("'", `'\\''`)}'`;
267
+ }
268
+ const SOURCE = quoted(WORKSPACE_SOURCE_DIRECTORY);
269
+ const STAGING = quoted(WORKSPACE_SOURCE_STAGING_DIRECTORY);
270
+ const PAGES = quoted(WORKSPACE_SOURCE_PAGES_DIRECTORY);
271
+ const MARKER = quoted(WORKSPACE_SOURCE_MARKER_PATH);
272
+ const writable = (path) => `if [ -e ${path} ]; then chmod -R u+w ${path}; fi`;
273
+ function installCommand(pages) {
274
+ return [
275
+ "set -eu",
276
+ `rm -f ${MARKER}`,
277
+ writable(STAGING),
278
+ `rm -rf ${STAGING}`,
279
+ `mkdir -p ${STAGING}`,
280
+ ...pages.map((page) => `tar -xzf ${quoted(page)} -C ${STAGING} --no-same-owner --no-same-permissions`),
281
+ writable(SOURCE),
282
+ `rm -rf ${SOURCE}`,
283
+ `mv ${STAGING} ${SOURCE}`,
284
+ `chmod -R a-w ${SOURCE}`,
285
+ `rm -rf ${PAGES}`
286
+ ].join("\n");
287
+ }
288
+ const REMOVE_COMMAND = [
289
+ "set -eu",
290
+ writable(SOURCE),
291
+ writable(STAGING),
292
+ `rm -rf ${SOURCE} ${STAGING} ${PAGES} ${MARKER}`
293
+ ].join("\n");
294
+ function parseMarker(text) {
295
+ let value;
296
+ try {
297
+ value = JSON.parse(text);
298
+ } catch {
299
+ return;
300
+ }
301
+ return isRecord(value) && typeof value.memberId === "string" && typeof value.commit === "string" && typeof value.turnId === "string" && typeof value.files === "number" && typeof value.skipped === "number" && typeof value.stagedAt === "string" ? {
302
+ memberId: value.memberId,
303
+ commit: value.commit,
304
+ turnId: value.turnId,
305
+ files: value.files,
306
+ skipped: value.skipped,
307
+ stagedAt: value.stagedAt
308
+ } : void 0;
309
+ }
310
+ async function installedMarker(sandbox) {
311
+ try {
312
+ const read = await sandbox.run({ command: `test -d ${SOURCE} && cat ${MARKER}` });
313
+ return read.exitCode === 0 ? parseMarker(read.stdout) : void 0;
314
+ } catch (error) {
315
+ console.warn("[segmentstream] workspace source marker could not be read; copying again", { error });
316
+ return;
317
+ }
318
+ }
319
+ async function copyOnce(input) {
320
+ const { memberId, turnId } = input.request.reader;
321
+ const first = await requestPage(input.request, void 0);
322
+ let page = first;
323
+ let sandbox;
324
+ let completed = false;
325
+ try {
326
+ sandbox = await input.sandbox();
327
+ const recorded = input.record.get();
328
+ if (!input.refresh && recorded?.memberId === memberId && recorded.turnId === turnId && recorded.commit === first.commit) {
329
+ const marker = await installedMarker(sandbox);
330
+ if (marker?.memberId === memberId && marker.turnId === turnId && marker.commit === first.commit) {
331
+ completed = true;
332
+ await first.discard();
333
+ return {
334
+ path: WORKSPACE_SOURCE_DIRECTORY,
335
+ commit: first.commit,
336
+ files: marker.files,
337
+ skipped: marker.skipped,
338
+ reused: true
339
+ };
340
+ }
341
+ }
342
+ input.record.update(() => ({
343
+ memberId,
344
+ commit: first.commit,
345
+ turnId
346
+ }));
347
+ const pages = [];
348
+ let files = 0;
349
+ let skipped = 0;
350
+ for (;;) {
351
+ if (page.commit !== first.commit) throw new WorkspaceSourceError({
352
+ code: "workspace_source_inconsistent",
353
+ message: `pages named commits ${first.commit} and ${page.commit}; nothing was replaced.`
354
+ });
355
+ const bytes = await page.bytes();
356
+ const path = `${WORKSPACE_SOURCE_PAGES_DIRECTORY}/${String(pages.length).padStart(4, "0")}.tar.gz`;
357
+ archivePaths(bytes);
358
+ await sandbox.writeBinaryFile({
359
+ path,
360
+ content: bytes
361
+ });
362
+ pages.push(path);
363
+ files += page.files;
364
+ skipped += page.skipped;
365
+ if (page.next === void 0) break;
366
+ if (pages.length >= MAXIMUM_PAGES) throw new WorkspaceSourceError({
367
+ code: "workspace_source_too_large",
368
+ message: `the source has more than ${MAXIMUM_PAGES} pages.`
369
+ });
370
+ page = await requestPage(input.request, page.next);
371
+ }
372
+ const installed = await sandbox.run({ command: installCommand(pages) });
373
+ if (installed.exitCode !== 0) throw new WorkspaceSourceError({
374
+ code: "workspace_source_install_failed",
375
+ message: `extraction exited ${installed.exitCode}: ${installed.stderr.slice(0, 1e3)}`
376
+ });
377
+ const marker = {
378
+ memberId,
379
+ commit: first.commit,
380
+ turnId,
381
+ files,
382
+ skipped,
383
+ stagedAt: input.now().toISOString()
384
+ };
385
+ await sandbox.writeTextFile({
386
+ path: WORKSPACE_SOURCE_MARKER_PATH,
387
+ content: `${JSON.stringify(marker)}\n`
388
+ });
389
+ completed = true;
390
+ return {
391
+ path: WORKSPACE_SOURCE_DIRECTORY,
392
+ commit: first.commit,
393
+ files,
394
+ skipped,
395
+ reused: false
396
+ };
397
+ } finally {
398
+ if (!completed) {
399
+ await page.discard();
400
+ if (sandbox !== void 0) await sandbox.run({ command: `rm -rf ${STAGING} ${PAGES}` }).then(void 0, (error) => {
401
+ console.warn("[segmentstream] workspace source cleanup failed", { error });
402
+ });
403
+ }
404
+ }
405
+ }
406
+ async function copyWorkspaceSource(input) {
407
+ const pass = {
408
+ request: {
409
+ reader: input.reader,
410
+ origin: input.origin,
411
+ token: input.token,
412
+ fetch: input.fetch ?? fetch
413
+ },
414
+ sandbox: input.sandbox,
415
+ refresh: input.refresh === true,
416
+ record: input.record ?? workspaceSourceRecord,
417
+ now: input.now ?? (() => new Date())
418
+ };
419
+ for (let attempt = 0;; attempt++) try {
420
+ return await copyOnce(pass);
421
+ } catch (error) {
422
+ if (!(error instanceof SourceChanged)) throw error;
423
+ if (attempt > 0) throw new WorkspaceSourceError({
424
+ code: "workspace_source_changed",
425
+ message: "production moved to another commit twice while the pages were read.",
426
+ status: 409
427
+ }, { cause: error });
428
+ }
429
+ }
430
+ function workspaceSourceGuard(options = {}) {
431
+ return { async "turn.started"(_event, context) {
432
+ const record = options.record ?? workspaceSourceRecord;
433
+ if (record.get() === null) return;
434
+ if ((options.sandbox ?? sandboxRecord).get() === null) {
435
+ record.update(() => null);
436
+ return;
437
+ }
438
+ try {
439
+ const removed = await (await context.getSandbox()).run({ command: REMOVE_COMMAND });
440
+ if (removed.exitCode !== 0) throw new Error(`Removal exited ${removed.exitCode}: ${removed.stderr.slice(0, 1e3)}`);
441
+ record.update(() => null);
442
+ } catch (error) {
443
+ console.error("[segmentstream] workspace source removal failed", {
444
+ sessionId: context.session.id,
445
+ turnId: context.session.turn.id,
446
+ error
447
+ });
448
+ }
449
+ } };
450
+ }
451
+ export { REMOVE_COMMAND, WORKSPACE_SOURCE_DIRECTORY, WORKSPACE_SOURCE_MARKER_PATH, WORKSPACE_SOURCE_PAGES_DIRECTORY, WORKSPACE_SOURCE_ROUTE, WORKSPACE_SOURCE_STAGING_DIRECTORY, WorkspaceSourceError, archivePaths, copyWorkspaceSource, installCommand, safeArchivePath, workspaceSourceGuard, workspaceSourceReader, workspaceSourceRecord };
@@ -6,7 +6,7 @@ __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  import { chatModelChoiceOf } from "./config.mjs";
8
8
  import { isPreview } from "./lib/environment.mjs";
9
- import { a as rolesOf } from "../_chunks/principal-BXvelsZk.mjs";
9
+ import { a as rolesOf } from "../_chunks/principal-Dzs8hUDB.mjs";
10
10
  import { defineDynamic } from "eve";
11
11
  import { runs } from "eve/events";
12
12
  function chooseModel(input) {
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
4
4
  const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
- import { u as runtimeEnvelope } from "../../_chunks/principal-BXvelsZk.mjs";
7
+ import { u as runtimeEnvelope } from "../../_chunks/principal-Dzs8hUDB.mjs";
8
8
  import segmentstream from "../extension.mjs";
9
9
  import { n as workflowToolResult } from "../../_chunks/workflow-serializable-DU7WeCIW.mjs";
10
10
  import { actionApproval } from "../lib/confirmations.mjs";
@@ -53,6 +53,7 @@ const tool = defineTool({
53
53
  label: { start: () => "Send stored attachments" },
54
54
  async execute(input, ctx) {
55
55
  const principal = ctx.session.auth.current;
56
+ if (chatAddressOf(principal)?.provider === "web") throw new Error("Stored attachments cannot be sent in a web chat; link to them instead.");
56
57
  const options = {
57
58
  input: sendAttachmentsInput(input),
58
59
  principal,
@@ -63,7 +64,8 @@ const tool = defineTool({
63
64
  connection: segmentstream.config.channels.slack?.connection
64
65
  }) : await sendAttachments({
65
66
  ...options,
66
- connection: segmentstream.config.channels.telegram?.connection
67
+ connection: segmentstream.config.channels.telegram?.connection,
68
+ appLinks: segmentstream.config.channels.telegram
67
69
  }), ctx);
68
70
  }
69
71
  });
@@ -9,7 +9,7 @@ import { agentTaskIdOf, agentTaskSessions } from "../lib/agent-task.mjs";
9
9
  import { stringArgument } from "../lib/attachment-tools.mjs";
10
10
  import { chatAddressOf } from "../lib/chat-address.mjs";
11
11
  import { pendingQuestions, questionKey } from "../lib/questions.mjs";
12
- import { i as memberIdOf } from "../../_chunks/principal-BXvelsZk.mjs";
12
+ import { i as memberIdOf } from "../../_chunks/principal-Dzs8hUDB.mjs";
13
13
  import { defineTool } from "eve/tools";
14
14
  var questions_ask_default = defineTool({
15
15
  availableInSubagents: false,
@@ -0,0 +1,2 @@
1
+ declare const _default: import("eve").DynamicSentinel<import("eve/tools").DynamicToolResult, import("eve").DynamicContext>;
2
+ export default _default;
@@ -0,0 +1,44 @@
1
+ import { fileURLToPath as __eveFileURLToPath } from "node:url";
2
+ import { dirname as __eveDirname } from "node:path";
3
+ import { createRequire as __eveCreateRequire } from "node:module";
4
+ const __filename = __eveFileURLToPath(import.meta.url);
5
+ __eveDirname(__filename);
6
+ __eveCreateRequire(import.meta.url);
7
+ import segmentstream from "../extension.mjs";
8
+ import { actorOf } from "../lib/actor.mjs";
9
+ import { appOrigin } from "../lib/environment.mjs";
10
+ import { runtimeToken } from "../lib/runtime-token.mjs";
11
+ import { sessionSandbox } from "../lib/sandbox-session.mjs";
12
+ import { n as workflowToolResult } from "../../_chunks/workflow-serializable-DU7WeCIW.mjs";
13
+ import { copyWorkspaceSource, workspaceSourceReader } from "../lib/workspace-source.mjs";
14
+ import { defineDynamic, defineTool } from "eve/tools";
15
+ const tool = defineTool({
16
+ description: "Copy this workspace's deployed code (the commit of main that production runs) into /workspace/source, read-only, as the current member, to find out what the workspace can and cannot do or why something fails. The code is for your own understanding, not to show the member. Call it before reading the workspace's own code, then search it with rg and read its files there. Answers the path, the commit and the counts of files copied and skipped. The copy lasts for this turn; in this turn a copy of the same commit is reused unless refresh is true.",
17
+ inputSchema: {
18
+ type: "object",
19
+ properties: { refresh: {
20
+ type: "boolean",
21
+ description: "Copy again even when the sandbox holds this commit."
22
+ } },
23
+ additionalProperties: false
24
+ },
25
+ label: { start: () => "Copy the workspace's code into the sandbox" },
26
+ async execute(input, ctx) {
27
+ const refresh = input.refresh;
28
+ if (refresh !== void 0 && typeof refresh !== "boolean") throw new TypeError("refresh must be true or false.");
29
+ const copy = await copyWorkspaceSource({
30
+ reader: workspaceSourceReader({
31
+ principal: ctx.session.auth.current,
32
+ sessionId: ctx.session.id,
33
+ turnId: ctx.session.turn.id
34
+ }),
35
+ origin: appOrigin(),
36
+ token: runtimeToken,
37
+ sandbox: () => sessionSandbox(ctx),
38
+ refresh: refresh === true
39
+ });
40
+ return workflowToolResult(copy, ctx);
41
+ }
42
+ });
43
+ var workspace_source_default = defineDynamic((context) => segmentstream.config.sandbox && segmentstream.config.workspaceSource === true && actorOf(context.session.auth.current)?.kind === "member" ? tool : null);
44
+ export { workspace_source_default as default };
@@ -11,3 +11,4 @@ export { default as no_reply } from "../extension/tools/no_reply.js";
11
11
  export { default as ontology_call_file } from "../extension/tools/ontology_call_file.js";
12
12
  export { default as questions_ask } from "../extension/tools/questions_ask.js";
13
13
  export { default as sandbox } from "../extension/tools/sandbox.js";
14
+ export { default as workspace_source } from "../extension/tools/workspace_source.js";
@@ -15,4 +15,5 @@ import no_reply_default from "../extension/tools/no_reply.mjs";
15
15
  import ontology_call_file_default from "../extension/tools/ontology_call_file.mjs";
16
16
  import questions_ask_default from "../extension/tools/questions_ask.mjs";
17
17
  import sandbox_default from "../extension/tools/sandbox.mjs";
18
- export { attachments_discard_default as attachments_discard, attachments_list_default as attachments_list, attachments_promote_default as attachments_promote, attachments_read_default as attachments_read, attachments_send_default as attachments_send, attachments_stage_default as attachments_stage, attachments_table_default as attachments_table, no_reply_default as no_reply, ontology_call_file_default as ontology_call_file, questions_ask_default as questions_ask, sandbox_default as sandbox };
18
+ import workspace_source_default from "../extension/tools/workspace_source.mjs";
19
+ export { attachments_discard_default as attachments_discard, attachments_list_default as attachments_list, attachments_promote_default as attachments_promote, attachments_read_default as attachments_read, attachments_send_default as attachments_send, attachments_stage_default as attachments_stage, attachments_table_default as attachments_table, no_reply_default as no_reply, ontology_call_file_default as ontology_call_file, questions_ask_default as questions_ask, sandbox_default as sandbox, workspace_source_default as workspace_source };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@segmentstream/agent",
3
- "version": "0.45.0",
4
- "description": "Mount a SegmentStream workspace's agent on eve: the ontology as tools, acting as the member who writes, and Telegram, WhatsApp and Slack through the workspace's connections.",
3
+ "version": "0.47.0",
4
+ "description": "The SegmentStream workspace agent: the ontology as tools, acting as the member who writes, and the browser, Telegram, WhatsApp and Slack through the workspace's connections.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",
7
7
  "homepage": "https://segmentstream.com",
@@ -12,7 +12,6 @@
12
12
  },
13
13
  "keywords": [
14
14
  "segmentstream",
15
- "eve",
16
15
  "agent",
17
16
  "ontology",
18
17
  "telegram"
@@ -46,6 +45,10 @@
46
45
  "./telegram": {
47
46
  "types": "./dist/extension/channels/telegram.d.ts",
48
47
  "default": "./dist/extension/channels/telegram.mjs"
48
+ },
49
+ "./web": {
50
+ "types": "./dist/extension/lib/web/channel.d.ts",
51
+ "default": "./dist/extension/lib/web/channel.mjs"
49
52
  }
50
53
  },
51
54
  "eve": {
@@ -77,7 +80,7 @@
77
80
  "@chat-adapter/state-memory": "4.42.0",
78
81
  "@chat-adapter/telegram": "4.41.0",
79
82
  "@chat-adapter/whatsapp": "4.42.0",
80
- "@segmentstream/ontology": "0.99.2",
83
+ "@segmentstream/ontology": "0.101.0",
81
84
  "@vercel/oidc": "^3.8.4",
82
85
  "ai": "^7.0.172",
83
86
  "chat": "4.42.0",