@segmentstream/agent 0.1.5 → 0.2.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 (30) hide show
  1. package/README.md +21 -9
  2. package/dist/extension/instructions.md +6 -0
  3. package/dist/extension/lib/attachment-tools.d.ts +68 -0
  4. package/dist/extension/lib/attachment-tools.mjs +110 -0
  5. package/dist/extension/lib/attachments.d.ts +96 -0
  6. package/dist/extension/lib/attachments.mjs +233 -0
  7. package/dist/extension/lib/connection-proxy.d.ts +14 -1
  8. package/dist/extension/lib/connection-proxy.mjs +135 -39
  9. package/dist/extension/lib/telegram/adapter.d.ts +3 -0
  10. package/dist/extension/lib/telegram/adapter.mjs +39 -3
  11. package/dist/extension/lib/telegram/files.d.ts +25 -0
  12. package/dist/extension/lib/telegram/files.mjs +77 -0
  13. package/dist/extension/lib/telegram/inbound-request.d.ts +20 -1
  14. package/dist/extension/lib/telegram/inbound-request.mjs +29 -1
  15. package/dist/extension/lib/telegram/inbound.d.ts +10 -4
  16. package/dist/extension/lib/telegram/inbound.mjs +12 -0
  17. package/dist/extension/lib/telegram/proxy-fetch.mjs +3 -2
  18. package/dist/extension/lib/telegram/voice.d.ts +2 -2
  19. package/dist/extension/lib/telegram/voice.mjs +2 -2
  20. package/dist/extension/tools/attachments_discard.d.ts +30 -0
  21. package/dist/extension/tools/attachments_discard.mjs +22 -0
  22. package/dist/extension/tools/attachments_list.d.ts +30 -0
  23. package/dist/extension/tools/attachments_list.mjs +25 -0
  24. package/dist/extension/tools/attachments_promote.d.ts +36 -0
  25. package/dist/extension/tools/attachments_promote.mjs +69 -0
  26. package/dist/extension/tools/attachments_read.d.ts +9 -0
  27. package/dist/extension/tools/attachments_read.mjs +43 -0
  28. package/dist/tools/index.d.ts +4 -0
  29. package/dist/tools/index.mjs +5 -1
  30. package/package.json +1 -1
@@ -5,6 +5,8 @@ const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  const MAXIMUM_PROXY_BODY_BYTES = 2097152;
8
+ const MAXIMUM_DOWNLOAD_BYTES = 20971520;
9
+ const PROXY_STREAM_MEDIA_TYPE = "application/vnd.segmentstream.proxy-stream.v1+ndjson";
8
10
  const CALL_TIMEOUT_MS = 4e4;
9
11
  var ConnectionProxyError = class extends Error {
10
12
  name = "ConnectionProxyError";
@@ -55,59 +57,153 @@ function connectionProxy(input) {
55
57
  const { environment, connection } = input;
56
58
  const fetchImpl = input.fetch ?? fetch;
57
59
  const url = `${`${environment.segmentstreamUrl}/api/internal/v1/workspaces/${environment.workspaceId}`}/ontology/connections/${encodeURIComponent(connection)}/request`;
60
+ async function send(call, accept) {
61
+ if (call.body.byteLength > 2097152) throw new ConnectionProxyError({
62
+ connection,
63
+ code: "invalid_request",
64
+ message: "The request body exceeds the proxy's 2 MiB limit."
65
+ });
66
+ const token = await input.token();
67
+ try {
68
+ return await fetchImpl(url, {
69
+ method: "POST",
70
+ headers: {
71
+ "x-segmentstream-runtime-identity": token,
72
+ "x-segmentstream-organization-id": environment.organizationId,
73
+ "content-type": "application/json",
74
+ accept
75
+ },
76
+ body: JSON.stringify({
77
+ ...call.api === void 0 ? {} : { api: call.api },
78
+ method: call.method,
79
+ path: call.path,
80
+ headers: call.headers,
81
+ body: Buffer.from(call.body).toString("base64")
82
+ }),
83
+ redirect: "error",
84
+ signal: AbortSignal.timeout(CALL_TIMEOUT_MS)
85
+ });
86
+ } catch (error) {
87
+ throw new ConnectionProxyError({
88
+ connection,
89
+ code: error instanceof DOMException && error.name === "TimeoutError" ? "timeout" : "unavailable",
90
+ message: "SegmentStream's connection proxy could not be reached.",
91
+ cause: error
92
+ });
93
+ }
94
+ }
95
+ async function buffered(response) {
96
+ const text = await response.text();
97
+ let value;
98
+ try {
99
+ value = JSON.parse(text);
100
+ } catch (error) {
101
+ if (response.ok) throw new ConnectionProxyError({
102
+ connection,
103
+ code: "invalid_response",
104
+ message: "The connection proxy answered with an invalid response.",
105
+ cause: error
106
+ });
107
+ throw refusal(connection, response.status, void 0, error);
108
+ }
109
+ if (!response.ok) throw refusal(connection, response.status, value);
110
+ return decodeReply(connection, value);
111
+ }
58
112
  return {
59
113
  connection,
60
114
  async request(call) {
61
- if (call.body.byteLength > 2097152) throw new ConnectionProxyError({
62
- connection,
63
- code: "invalid_request",
64
- message: "The request body exceeds the proxy's 2 MiB limit."
65
- });
66
- const token = await input.token();
67
- let response;
115
+ return buffered(await send(call, "application/json"));
116
+ },
117
+ async download(call, maxBytes = MAXIMUM_DOWNLOAD_BYTES) {
118
+ const response = await send(call, PROXY_STREAM_MEDIA_TYPE);
119
+ const type = response.headers.get("content-type") ?? "";
120
+ if (!response.ok || !type.startsWith("application/vnd.segmentstream.proxy-stream.v1+ndjson") || response.body === null) return buffered(response);
68
121
  try {
69
- response = await fetchImpl(url, {
70
- method: "POST",
71
- headers: {
72
- "x-segmentstream-runtime-identity": token,
73
- "x-segmentstream-organization-id": environment.organizationId,
74
- "content-type": "application/json",
75
- accept: "application/json"
76
- },
77
- body: JSON.stringify({
78
- ...call.api === void 0 ? {} : { api: call.api },
79
- method: call.method,
80
- path: call.path,
81
- headers: call.headers,
82
- body: Buffer.from(call.body).toString("base64")
83
- }),
84
- redirect: "error",
85
- signal: AbortSignal.timeout(CALL_TIMEOUT_MS)
86
- });
122
+ return await framedReply(connection, response.body, maxBytes);
87
123
  } catch (error) {
124
+ if (error instanceof ConnectionProxyError) throw error;
88
125
  throw new ConnectionProxyError({
89
126
  connection,
90
127
  code: error instanceof DOMException && error.name === "TimeoutError" ? "timeout" : "unavailable",
91
- message: "SegmentStream's connection proxy could not be reached.",
128
+ message: "SegmentStream's connection proxy stopped relaying the response.",
92
129
  cause: error
93
130
  });
94
131
  }
95
- const text = await response.text();
96
- let value;
97
- try {
98
- value = JSON.parse(text);
99
- } catch (error) {
100
- if (response.ok) throw new ConnectionProxyError({
132
+ }
133
+ };
134
+ }
135
+ async function framedReply(connection, body, maxBytes) {
136
+ const invalid = () => new ConnectionProxyError({
137
+ connection,
138
+ code: "invalid_response",
139
+ message: "The connection proxy answered with an invalid stream."
140
+ });
141
+ const reader = body.getReader();
142
+ const decoder = new TextDecoder();
143
+ const chunks = [];
144
+ let head;
145
+ let size = 0;
146
+ let pending = "";
147
+ let ended = false;
148
+ try {
149
+ while (!ended) {
150
+ const next = await reader.read();
151
+ if (next.done) break;
152
+ pending += decoder.decode(next.value, { stream: true });
153
+ for (let newline = pending.indexOf("\n"); newline >= 0; newline = pending.indexOf("\n")) {
154
+ const line = pending.slice(0, newline);
155
+ pending = pending.slice(newline + 1);
156
+ if (line.trim() === "") continue;
157
+ let frame;
158
+ try {
159
+ frame = JSON.parse(line);
160
+ } catch (error) {
161
+ throw new ConnectionProxyError({
162
+ connection,
163
+ code: "invalid_response",
164
+ message: "The connection proxy answered with an invalid stream.",
165
+ cause: error
166
+ });
167
+ }
168
+ if (!isRecord(frame)) throw invalid();
169
+ if (head === void 0) {
170
+ if (typeof frame.statusCode !== "number" || !isHeaderPairs(frame.headers)) throw invalid();
171
+ head = {
172
+ status: frame.statusCode,
173
+ headers: frame.headers
174
+ };
175
+ } else if (typeof frame.data === "string") {
176
+ const chunk = Buffer.from(frame.data, "base64");
177
+ size += chunk.byteLength;
178
+ if (size > maxBytes) throw new ConnectionProxyError({
179
+ connection,
180
+ code: "too_large",
181
+ message: `The response exceeds the ${Math.floor(maxBytes / 1024 / 1024)} MiB download limit.`
182
+ });
183
+ chunks.push(chunk);
184
+ } else if (frame.end === true) {
185
+ ended = true;
186
+ break;
187
+ } else if (typeof frame.error === "string") throw new ConnectionProxyError({
101
188
  connection,
102
- code: "invalid_response",
103
- message: "The connection proxy answered with an invalid response.",
104
- cause: error
189
+ code: frame.error,
190
+ message: "SegmentStream's connection proxy failed while relaying the response."
105
191
  });
106
- throw refusal(connection, response.status, void 0, error);
192
+ else throw invalid();
107
193
  }
108
- if (!response.ok) throw refusal(connection, response.status, value);
109
- return decodeReply(connection, value);
110
194
  }
195
+ } finally {
196
+ await reader.cancel().catch(() => void 0);
197
+ }
198
+ if (head === void 0 || !ended) throw new ConnectionProxyError({
199
+ connection,
200
+ code: "unavailable",
201
+ message: "The connection proxy's stream ended before the response did."
202
+ });
203
+ return {
204
+ status: head.status,
205
+ headers: head.headers,
206
+ body: new Uint8Array(Buffer.concat(chunks))
111
207
  };
112
208
  }
113
- export { ConnectionProxyError, MAXIMUM_PROXY_BODY_BYTES, connectionProxy };
209
+ export { ConnectionProxyError, MAXIMUM_DOWNLOAD_BYTES, MAXIMUM_PROXY_BODY_BYTES, PROXY_STREAM_MEDIA_TYPE, connectionProxy };
@@ -1,5 +1,6 @@
1
1
  import { type TelegramChannel } from "eve/channels/telegram";
2
2
  import type { SessionSettings, TelegramSettings } from "../../config.js";
3
+ import { type AgentAttachments } from "../attachments.js";
3
4
  import { type Environment } from "../environment.js";
4
5
  import { type RuntimeTokenVerifier } from "../runtime-identity.js";
5
6
  import { type RuntimeToken } from "../runtime-token.js";
@@ -32,6 +33,8 @@ export interface TelegramAdapterOptions {
32
33
  readonly verify?: RuntimeTokenVerifier;
33
34
  /** Reads the tail of a session's durable stream; tests pass a fake. */
34
35
  readonly history?: HistoryReader;
36
+ /** The attachment store; tests pass a fake. */
37
+ readonly attachments?: AgentAttachments;
35
38
  }
36
39
  /** The Telegram channel of the extension. */
37
40
  export declare function segmentstreamTelegram(options: TelegramAdapterOptions): TelegramChannel;
@@ -5,10 +5,11 @@ const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  import { appOrigin, channelsEnabled, proxyEnvironment, runtimeIdentityEnvironment, webhookSecret } from "../environment.mjs";
8
- import { MAXIMUM_PROXY_BODY_BYTES, connectionProxy } from "../connection-proxy.mjs";
8
+ import { MAXIMUM_DOWNLOAD_BYTES, connectionProxy } from "../connection-proxy.mjs";
9
+ import { runtimeToken } from "../runtime-token.mjs";
10
+ import { agentAttachments, parseAttachmentUrl } from "../attachments.mjs";
9
11
  import { agentIdentities } from "../identities.mjs";
10
12
  import { runtimeTokenVerifier } from "../runtime-identity.mjs";
11
- import { runtimeToken } from "../runtime-token.mjs";
12
13
  import { botIdentityCache } from "./bot.mjs";
13
14
  import { agentChats } from "./chats.mjs";
14
15
  import { observedMessages } from "./observed.mjs";
@@ -74,6 +75,15 @@ function segmentstreamTelegram(options) {
74
75
  return identities;
75
76
  }
76
77
  const chats = agentChats({ identities: pairing });
78
+ let store = options.attachments;
79
+ function attachments() {
80
+ store ??= agentAttachments({
81
+ origin: appOrigin(environment),
82
+ token,
83
+ ...options.fetch === void 0 ? {} : { fetch: options.fetch }
84
+ });
85
+ return store;
86
+ }
77
87
  const channel = telegramChannel({
78
88
  api: {
79
89
  apiBaseUrl: TELEGRAM_PROXY_ORIGIN,
@@ -84,7 +94,7 @@ function segmentstreamTelegram(options) {
84
94
  botToken: TELEGRAM_PLACEHOLDER_TOKEN,
85
95
  webhookVerifier: telegramWebhookVerifier(() => webhookSecret(environment))
86
96
  },
87
- uploadPolicy: { maxBytes: MAXIMUM_PROXY_BODY_BYTES },
97
+ uploadPolicy: { maxBytes: MAXIMUM_DOWNLOAD_BYTES },
88
98
  onMessage: (context, message) => handleInboundMessage({
89
99
  context,
90
100
  message,
@@ -105,6 +115,7 @@ function segmentstreamTelegram(options) {
105
115
  proxy: proxy(),
106
116
  fileId
107
117
  }),
118
+ attachments: attachments(),
108
119
  observed,
109
120
  chats,
110
121
  connectUrl: (code) => `${appOrigin(environment)}/agent/connect?code=${encodeURIComponent(code)}`
@@ -112,8 +123,29 @@ function segmentstreamTelegram(options) {
112
123
  }),
113
124
  events: { "message.completed": deliverUnlessSilent }
114
125
  });
126
+ const adapter = channel.adapter;
127
+ const fetchTelegramFile = adapter?.fetchFile;
128
+ async function fetchFile(url, context) {
129
+ const reference = parseAttachmentUrl(url);
130
+ if (reference === void 0) return fetchTelegramFile === void 0 ? null : fetchTelegramFile(url, context);
131
+ const sessionId = sessionIdOf(context);
132
+ const { attachment, bytes } = await attachments().read({
133
+ memberId: reference.memberId,
134
+ conversation: reference.conversation,
135
+ ...sessionId === void 0 ? {} : { sessionId }
136
+ }, reference.id);
137
+ return {
138
+ bytes: Buffer.from(bytes),
139
+ mediaType: attachment.contentType,
140
+ filename: attachment.filename
141
+ };
142
+ }
115
143
  return {
116
144
  ...channel,
145
+ ...adapter === void 0 ? {} : { adapter: {
146
+ ...adapter,
147
+ fetchFile
148
+ } },
117
149
  routes: [...channel.routes.map((route) => gatedRoute({
118
150
  route,
119
151
  options,
@@ -128,6 +160,10 @@ function segmentstreamTelegram(options) {
128
160
  })]
129
161
  };
130
162
  }
163
+ function sessionIdOf(context) {
164
+ const session = typeof context === "object" && context !== null ? context.session : void 0;
165
+ return typeof session?.id === "string" && session.id !== "" ? session.id : void 0;
166
+ }
131
167
  function gatedRoute(input) {
132
168
  const { route, options, environment } = input;
133
169
  if (route.transport === "websocket") return route;
@@ -0,0 +1,25 @@
1
+ import type { TelegramMessage } from "eve/channels/telegram";
2
+ import { type AgentAttachments } from "../attachments.js";
3
+ import type { StagedFile } from "./inbound-request.js";
4
+ /**
5
+ * Photos and documents a member sends the agent (`docs/agent.md`, "Attachments"). Before the turn
6
+ * starts, each file is downloaded through the connection and stored once as a temporary attachment
7
+ * of the member, in this chat, for 24 hours. The turn reads the stored copy, and the model learns
8
+ * each attachment's id from an `<attachments>` block, so that it can read, discard or promote it
9
+ * with the attachment tools. A file that cannot be stored is never passed on silently: the turn
10
+ * gets a note saying why.
11
+ */
12
+ /** The chat an attachment belongs to: `telegram:<chat id>`. */
13
+ export declare function telegramConversation(chatId: string): string;
14
+ /** Stores the message's files one after another, so that a large album holds one file at a time. */
15
+ export declare function stageFiles(input: {
16
+ readonly message: TelegramMessage;
17
+ readonly memberId: string;
18
+ readonly attachments: AgentAttachments;
19
+ readonly download: (fileId: string) => Promise<Uint8Array>;
20
+ }): Promise<ReadonlyMap<string, StagedFile>>;
21
+ /** The `<attachments>` block that tells the model which attachments the message carries. */
22
+ export declare function attachmentsContext(input: {
23
+ readonly files: ReadonlyMap<string, StagedFile>;
24
+ readonly group: boolean;
25
+ }): ReadonlyArray<string>;
@@ -0,0 +1,77 @@
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 { MAXIMUM_ATTACHMENT_BYTES, attachmentUrl } from "../attachments.mjs";
8
+ function telegramConversation(chatId) {
9
+ return `telegram:${chatId}`;
10
+ }
11
+ function described(file, index) {
12
+ return {
13
+ filename: file.fileName ?? (file.kind === "photo" ? `photo-${index}.jpg` : `file-${index}`),
14
+ contentType: file.mediaType ?? (file.kind === "photo" ? "image/jpeg" : "application/octet-stream")
15
+ };
16
+ }
17
+ function megabytes(bytes) {
18
+ return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
19
+ }
20
+ async function stageFiles(input) {
21
+ const { message } = input;
22
+ const conversation = telegramConversation(message.chat.id);
23
+ const staged = new Map();
24
+ for (const [index, file] of message.attachments.entries()) {
25
+ const { filename, contentType } = described(file, index);
26
+ if (file.size !== void 0 && file.size > MAXIMUM_ATTACHMENT_BYTES) {
27
+ staged.set(file.fileId, {
28
+ status: "refused",
29
+ filename,
30
+ reason: `it is ${megabytes(file.size)}, over the ${megabytes(MAXIMUM_ATTACHMENT_BYTES)} a bot may download`
31
+ });
32
+ continue;
33
+ }
34
+ try {
35
+ const bytes = await input.download(file.fileId);
36
+ const attachment = await input.attachments.receive({
37
+ memberId: input.memberId,
38
+ conversation
39
+ }, {
40
+ bytes,
41
+ filename,
42
+ contentType,
43
+ sourceRef: `${message.chat.id}:${message.messageId}:${index}`
44
+ });
45
+ staged.set(file.fileId, {
46
+ status: "stored",
47
+ attachment,
48
+ url: attachmentUrl({
49
+ id: attachment.id,
50
+ memberId: input.memberId,
51
+ conversation
52
+ })
53
+ });
54
+ } catch (error) {
55
+ console.error("[segmentstream] attachment could not be stored", {
56
+ kind: file.kind,
57
+ size: file.size,
58
+ error
59
+ });
60
+ staged.set(file.fileId, {
61
+ status: "refused",
62
+ filename,
63
+ reason: error instanceof Error ? error.message : "it could not be stored"
64
+ });
65
+ }
66
+ }
67
+ return staged;
68
+ }
69
+ function attachmentsContext(input) {
70
+ if (input.files.size === 0) return [];
71
+ const lines = ["<attachments>", `The member's files from this message, kept for 24 hours for this member in this chat only. Read, discard or promote them with the segmentstream__attachments_* tools and these ids; promote only what should be kept.`];
72
+ for (const staged of input.files.values()) lines.push(staged.status === "stored" ? `- id: ${staged.attachment.id}, filename: ${JSON.stringify(staged.attachment.filename)}, type: ${staged.attachment.contentType}, size: ${staged.attachment.size} bytes, expires_at: ${staged.attachment.expiresAt}` : `- ${JSON.stringify(staged.filename)}: not received, ${staged.reason}`);
73
+ if (input.group) lines.push("This is a group chat: everyone in it shares this conversation's history, so do not repeat a private file's contents here. Suggest sending private files in a direct message.");
74
+ lines.push("</attachments>");
75
+ return [lines.join("\n")];
76
+ }
77
+ export { attachmentsContext, stageFiles, telegramConversation };
@@ -1,5 +1,16 @@
1
1
  import type { ChannelSource } from "eve/channels";
2
+ import type { Attachment } from "../attachments.js";
2
3
  import type { SessionOperations } from "./sessions.js";
4
+ /** What became of one Telegram file of the message: stored as an attachment, or why not. */
5
+ export type StagedFile = {
6
+ readonly status: "stored";
7
+ readonly attachment: Attachment;
8
+ readonly url: URL;
9
+ } | {
10
+ readonly status: "refused";
11
+ readonly filename: string;
12
+ readonly reason: string;
13
+ };
3
14
  /**
4
15
  * What one webhook request shares between eve's `onMessage` hook and the route wrapper around it.
5
16
  * `onMessage` may only return `{ auth, context, title }`; it cannot change the message eve sends.
@@ -12,6 +23,11 @@ export declare class InboundRequest {
12
23
  readonly agentDescription: () => Promise<string>;
13
24
  /** The text the turn should carry instead of the parsed message, once a transcript exists. */
14
25
  text: string | undefined;
26
+ /**
27
+ * The message's Telegram files by file id, once stored as attachments: the turn reads each from
28
+ * the attachment store instead of Telegram, and one that could not be stored becomes a note.
29
+ */
30
+ files: ReadonlyMap<string, StagedFile> | undefined;
15
31
  /** The route's session operations, on addresses already mapped to the configured scope. */
16
32
  sessions: SessionOperations | undefined;
17
33
  /** Keeps work the request started, such as a chat report, alive after it answers. */
@@ -23,5 +39,8 @@ export declare class InboundRequest {
23
39
  export declare function runInboundRequest<Result>(request: InboundRequest, run: () => Result): Result;
24
40
  /** The current webhook request, or `undefined` outside the wrapped route. */
25
41
  export declare function currentInboundRequest(): InboundRequest | undefined;
26
- /** A channel source whose `send` delivers the request's replacement text, when one was recorded. */
42
+ /**
43
+ * A channel source whose `send` delivers the request's replacement text, when one was recorded,
44
+ * and its stored attachments in place of the Telegram files.
45
+ */
27
46
  export declare function withReplacedText<State>(source: ChannelSource<State>, request: InboundRequest): ChannelSource<State>;
@@ -8,6 +8,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
8
8
  var InboundRequest = class {
9
9
  agentDescription;
10
10
  text;
11
+ files;
11
12
  sessions;
12
13
  waitUntil;
13
14
  constructor(agentDescription) {
@@ -28,11 +29,38 @@ function replaceText(message, text) {
28
29
  text
29
30
  }, ...message.filter((part) => part.type !== "text")];
30
31
  }
32
+ function telegramFileId(data) {
33
+ const href = data instanceof URL ? data.href : typeof data === "string" ? data : void 0;
34
+ if (href === void 0 || !href.startsWith("telegram-file:")) return;
35
+ try {
36
+ return decodeURIComponent(new URL(href).pathname);
37
+ } catch {
38
+ return;
39
+ }
40
+ }
41
+ function replaceFiles(message, files) {
42
+ if (typeof message === "string") return message;
43
+ return message.map((part) => {
44
+ if (part.type !== "file") return part;
45
+ const fileId = telegramFileId(part.data);
46
+ const staged = fileId === void 0 ? void 0 : files.get(fileId);
47
+ if (staged === void 0) return part;
48
+ if (staged.status === "refused") return {
49
+ type: "text",
50
+ text: `[file ${staged.filename} was not received: ${staged.reason}]`
51
+ };
52
+ return {
53
+ ...part,
54
+ data: staged.url
55
+ };
56
+ });
57
+ }
31
58
  function withReplacedText(source, request) {
32
59
  return {
33
60
  ...source,
34
61
  send(message, options) {
35
- return source.send(request.text === void 0 ? message : replaceText(message, request.text), options);
62
+ const text = request.text === void 0 ? message : replaceText(message, request.text);
63
+ return source.send(request.files === void 0 ? text : replaceFiles(text, request.files), options);
36
64
  }
37
65
  };
38
66
  }
@@ -1,5 +1,6 @@
1
1
  import type { TelegramContext, TelegramInboundResult, TelegramMessage } from "eve/channels/telegram";
2
2
  import type { SessionSettings, TelegramPolicy } from "../../config.js";
3
+ import type { AgentAttachments } from "../attachments.js";
3
4
  import type { AgentIdentities } from "../identities.js";
4
5
  import type { BotIdentityLookup } from "./bot.js";
5
6
  import { type AgentChats } from "./chats.js";
@@ -21,10 +22,13 @@ import { type Transcriber } from "./voice.js";
21
22
  * 3. The router answers or observes. An observed message goes to the chat's ring and stops here.
22
23
  * 4. The session policy runs on the session the message continues: an idle one is cleared, the
23
24
  * first message after the day's compaction time compacts it.
24
- * 5. An answered message dispatches as the paired member, with the ring, the registry's `<chat>`
25
- * block and a `<speaker>` block naming the member, their roles and the message they replied to
26
- * ahead of it as context. An answered group message waits for its chat's report first, so that
27
- * a chat new to the process already carries its `<chat>` block.
25
+ * 5. An answered message's photos and documents are stored as the member's temporary attachments,
26
+ * which the turn reads in place of the Telegram files.
27
+ * 6. An answered message dispatches as the paired member, with the ring, the registry's `<chat>`
28
+ * block, a `<speaker>` block naming the member, their roles and the message they replied to and
29
+ * an `<attachments>` block naming its stored files ahead of it as context. An answered group
30
+ * message waits for its chat's report first, so that a chat new to the process already carries
31
+ * its `<chat>` block.
28
32
  */
29
33
  export interface InboundDependencies {
30
34
  readonly policy: TelegramPolicy;
@@ -36,6 +40,8 @@ export interface InboundDependencies {
36
40
  readonly decide: RouteDecider;
37
41
  readonly transcribe: Transcriber;
38
42
  readonly download: (fileId: string) => Promise<Uint8Array>;
43
+ /** The attachment store; without it, files reach the turn as eve's Telegram file parts. */
44
+ readonly attachments?: AgentAttachments;
39
45
  readonly observed: ObservedMessages;
40
46
  readonly chats: AgentChats;
41
47
  /** The app's `/agent/connect` link for a pairing code. */
@@ -6,6 +6,7 @@ __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  import { telegramPrincipal } from "./principal.mjs";
8
8
  import { chatContext } from "./chats.mjs";
9
+ import { attachmentsContext, stageFiles } from "./files.mjs";
9
10
  import { observedChatKey, observedContext } from "./observed.mjs";
10
11
  import { isAddressed, routeMessage } from "./router.mjs";
11
12
  import { messageAddress, scopedAddress } from "./sessions.mjs";
@@ -226,6 +227,13 @@ async function handleInboundMessage(input) {
226
227
  dependencies.sessionPolicy.delivered(address);
227
228
  }
228
229
  await context.telegram.startTyping();
230
+ const files = sent.attachments.length === 0 || dependencies.attachments === void 0 || input.request === void 0 ? new Map() : await stageFiles({
231
+ message: sent,
232
+ memberId: lookup.member.id,
233
+ attachments: dependencies.attachments,
234
+ download: dependencies.download
235
+ });
236
+ if (input.request !== void 0 && files.size > 0) input.request.files = files;
229
237
  await report;
230
238
  return {
231
239
  auth: telegramPrincipal({
@@ -240,6 +248,10 @@ async function handleInboundMessage(input) {
240
248
  member: lookup.member,
241
249
  bot
242
250
  }),
251
+ ...attachmentsContext({
252
+ files,
253
+ group
254
+ }),
243
255
  ...notes
244
256
  ]
245
257
  };
@@ -53,13 +53,14 @@ function hasNullBody(status) {
53
53
  function telegramProxyFetch(proxy) {
54
54
  return async (input, init) => {
55
55
  const route = telegramProxyRoute(requestUrl(input));
56
- const reply = await proxy.request({
56
+ const call = {
57
57
  api: route.api,
58
58
  method: proxyMethod(init?.method),
59
59
  path: route.path,
60
60
  headers: headerPairs(init?.headers),
61
61
  body: await bodyBytes(init?.body)
62
- });
62
+ };
63
+ const reply = route.api === "file" ? await proxy.download(call) : await proxy.request(call);
63
64
  return new Response(hasNullBody(reply.status) ? null : Buffer.from(reply.body), {
64
65
  status: reply.status,
65
66
  headers: reply.headers.map(([name, value]) => [name, value])
@@ -41,8 +41,8 @@ export interface VoiceText {
41
41
  readonly note: string;
42
42
  }
43
43
  /**
44
- * Turns a voice message into text. When it cannot (transcription is off, the file exceeds what the
45
- * proxy forwards, or a call fails), the text says so, so the agent can ask for a typed message.
44
+ * Turns a voice message into text. When it cannot (transcription is off, the file exceeds what a
45
+ * bot may download, or a call fails), the text says so, so the agent can ask for a typed message.
46
46
  */
47
47
  export declare function voiceText(input: {
48
48
  readonly voice: VoiceMessage;
@@ -67,7 +67,7 @@ async function downloadTelegramFile(input) {
67
67
  throw new TelegramFileError("Telegram answered getFile with an invalid body.", { cause: error });
68
68
  }
69
69
  if (metadata.status !== 200 || typeof filePath !== "string" || filePath === "") throw new TelegramFileError(`Telegram refused getFile with HTTP ${metadata.status}${description ? `: ${description}` : "."}`);
70
- const file = await input.proxy.request({
70
+ const file = await input.proxy.download({
71
71
  api: "file",
72
72
  method: "GET",
73
73
  path: `/${filePath}`,
@@ -84,7 +84,7 @@ async function voiceText(input) {
84
84
  text: voiceNote(voice, "voice transcription is off"),
85
85
  note
86
86
  };
87
- if (voice.size !== void 0 && voice.size > 2097152) return {
87
+ if (voice.size !== void 0 && voice.size > 20971520) return {
88
88
  text: voiceNote(voice, "too long to transcribe"),
89
89
  note
90
90
  };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * `segmentstream__attachments_discard`: drops an attachment at once. It can no longer be read or
3
+ * promoted, and its file is deleted by the platform's maintenance.
4
+ */
5
+ declare const _default: import("eve/tools").ToolDefinition<Record<string, unknown>, {
6
+ attachment: {
7
+ promotion?: {} | undefined;
8
+ id: string;
9
+ filename: string;
10
+ content_type: string;
11
+ size: number;
12
+ sha256: string;
13
+ state: string;
14
+ expires_at: string;
15
+ };
16
+ }> & {
17
+ execute(input: Record<string, unknown>, ctx: import("eve/tools").ToolContext): Promise<{
18
+ attachment: {
19
+ promotion?: {} | undefined;
20
+ id: string;
21
+ filename: string;
22
+ content_type: string;
23
+ size: number;
24
+ sha256: string;
25
+ state: string;
26
+ expires_at: string;
27
+ };
28
+ }>;
29
+ };
30
+ export default _default;
@@ -0,0 +1,22 @@
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 { ID_INPUT, attachmentForModel, attachmentOwner, stringArgument, toolAttachments } from "../lib/attachment-tools.mjs";
8
+ import { defineTool } from "eve/tools";
9
+ var attachments_discard_default = defineTool({
10
+ description: "Discard a file the member sent, by its attachment id, when it should not be kept. It cannot be read or promoted afterwards.",
11
+ inputSchema: ID_INPUT,
12
+ label: { start: () => "Discard an attachment" },
13
+ async execute(input, ctx) {
14
+ const owner = attachmentOwner({
15
+ principal: ctx.session.auth.current,
16
+ sessionId: ctx.session.id
17
+ });
18
+ const attachment = await toolAttachments().discard(owner, stringArgument(input, "id"));
19
+ return { attachment: attachmentForModel(attachment) };
20
+ }
21
+ });
22
+ export { attachments_discard_default as default };