Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

[3/4] fix(eve): deliver attachments on Telegram, Linear, Twilio, and Chat SDK (#4374)

Ben Pankow1921221e9ebe
diff --git a/.changeset/channel-attachments-reach-agent.md b/.changeset/channel-attachments-reach-agent.md
new file mode 100644
index 000000000..1334e7b9b
--- /dev/null
+++ b/.changeset/channel-attachments-reach-agent.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Files people send now reach the agent on more channels. Telegram keeps a file's real type instead of `application/octet-stream`, Linear reads uploaded files linked in a prompt (not only images) and leaves a note for one it can't download, and Twilio passes MMS media to the agent. Slack, Telegram, Twilio, and Linear stop a file download at the upload limit instead of reading the whole file into memory.
diff --git a/docs/channels/linear.mdx b/docs/channels/linear.mdx
index 2e95cc09c..ccbc7a36d 100644
--- a/docs/channels/linear.mdx
+++ b/docs/channels/linear.mdx
@@ -95,7 +95,7 @@ Start a session without an inbound webhook with `receive(linear, { target })`. S
### Attachments
-Markdown images hosted at `https://uploads.linear.app` in Agent Session prompts are fetched with the resolved Linear access token and included as image file parts. eve sends the bearer token only to that exact HTTPS origin; images from other hosts remain markdown text. If a Linear upload fails or returns non-image content, eve preserves its markdown reference and continues the text turn. Other inbound file attachments are not supported on this channel today.
+Files uploaded to Linear and referenced in an Agent Session prompt, whether as markdown images (`![name](url)`) or file links (`[name](url)`) hosted at `https://uploads.linear.app`, are fetched with the resolved Linear access token and included as file parts. eve sends the bearer token only to that exact HTTPS origin; images and links from other hosts remain markdown text. If a Linear upload fails, an image reference returns non-image content, or a file link returns an HTML page, the reference becomes its label in the text, the agent gets a short note that the file couldn't be retrieved, and the text turn continues.
### API handle
diff --git a/docs/channels/twilio.mdx b/docs/channels/twilio.mdx
index 0359434b2..c040f7c29 100644
--- a/docs/channels/twilio.mdx
+++ b/docs/channels/twilio.mdx
@@ -70,7 +70,7 @@ Start a session without an inbound message through `to(twilio, target).send(mess
### Attachments
-Inbound media attachments are not supported on this channel today.
+MMS media reaches the agent as file parts. eve downloads each media URL with the account SID and auth token, and only from the Twilio API origin (`https://api.twilio.com`, or `api.apiBaseUrl` when set). If a download fails, the agent gets a short note in place of the file and the text turn continues.
## Disclaimer
diff --git a/packages/eve/src/internal/attachments/limited-read.test.ts b/packages/eve/src/internal/attachments/limited-read.test.ts
index ec532ad17..d213cc51f 100644
--- a/packages/eve/src/internal/attachments/limited-read.test.ts
+++ b/packages/eve/src/internal/attachments/limited-read.test.ts
@@ -1,7 +1,11 @@
import { describe, expect, it } from "vitest";
import { EveAttachmentError } from "#internal/attachments/errors.js";
-import { assertWithinLimit, readLimitedBytes } from "#internal/attachments/limited-read.js";
+import {
+ assertWithinLimit,
+ limitedFetch,
+ readLimitedBytes,
+} from "#internal/attachments/limited-read.js";
const MB = 1024 * 1024;
@@ -65,3 +69,25 @@ describe("assertWithinLimit", () => {
expect(() => assertWithinLimit(new Uint8Array(5), 5, "test")).not.toThrow();
});
});
+
+describe("limitedFetch", () => {
+ it("fails a body that passes the limit while the caller reads it", async () => {
+ const fetch = limitedFetch(async () => streamed(30 * MB, MB), 25 * MB, "test");
+ const response = await fetch("https://files.test/big");
+ await expect(response.arrayBuffer()).rejects.toThrow("it is over the 25 MB upload limit.");
+ });
+
+ it("refuses a response whose declared length is over the limit", async () => {
+ const fetch = limitedFetch(
+ async () => new Response("x", { headers: { "content-length": String(30 * MB) } }),
+ 25 * MB,
+ "test",
+ );
+ await expect(fetch("https://files.test/big")).rejects.toThrow(EveAttachmentError);
+ });
+
+ it("passes a body within the limit through", async () => {
+ const fetch = limitedFetch(async () => new Response("hello"), 5, "test");
+ expect(await (await fetch("https://files.test/small")).text()).toBe("hello");
+ });
+});
diff --git a/packages/eve/src/internal/attachments/limited-read.ts b/packages/eve/src/internal/attachments/limited-read.ts
index a114a729e..f50b800c2 100644
--- a/packages/eve/src/internal/attachments/limited-read.ts
+++ b/packages/eve/src/internal/attachments/limited-read.ts
@@ -1,4 +1,10 @@
import { EveAttachmentError } from "#internal/attachments/errors.js";
+import type { UploadPolicy } from "#public/channels/upload-policy.js";
+
+/** The byte cap an upload policy sets; a disabled policy accepts nothing. */
+export function maxBytesOf(policy: UploadPolicy): number {
+ return policy === "disabled" ? 0 : policy.maxBytes;
+}
/**
* Reads a download's body, failing as soon as it passes `maxBytes`. Checks a
@@ -33,6 +39,41 @@ export async function readLimitedBytes(
return Buffer.concat(chunks);
}
+/**
+ * Wraps `fetch` so every response body fails once it passes `maxBytes`, for a
+ * download that reads the body itself, such as a vendored client's.
+ */
+export function limitedFetch(
+ fetchImpl: typeof globalThis.fetch,
+ maxBytes: number,
+ adapterKind: string,
+): typeof globalThis.fetch {
+ return async (input, init) => {
+ const response = await fetchImpl(input, init);
+ const declared = Number(response.headers.get("content-length") ?? Number.NaN);
+ if (Number.isFinite(declared) && declared > maxBytes) {
+ await response.body?.cancel();
+ throw overLimit(maxBytes, adapterKind);
+ }
+ if (response.body === null) return response;
+ let received = 0;
+ const capped = response.body.pipeThrough(
+ new TransformStream<Uint8Array, Uint8Array>({
+ transform(chunk, controller) {
+ received += chunk.byteLength;
+ if (received > maxBytes) controller.error(overLimit(maxBytes, adapterKind));
+ else controller.enqueue(chunk);
+ },
+ }),
+ );
+ return new Response(capped, {
+ headers: response.headers,
+ status: response.status,
+ statusText: response.statusText,
+ });
+ };
+}
+
/** Fails a download whose bytes arrived some other way than a `Response`, once they're in hand. */
export function assertWithinLimit(bytes: Uint8Array, maxBytes: number, adapterKind: string): void {
if (bytes.byteLength > maxBytes) throw overLimit(maxBytes, adapterKind);
diff --git a/packages/eve/src/internal/testing/channel-conformance/MATRIX.md b/packages/eve/src/internal/testing/channel-conformance/MATRIX.md
index 640dc4587..bab4b7390 100644
--- a/packages/eve/src/internal/testing/channel-conformance/MATRIX.md
+++ b/packages/eve/src/internal/testing/channel-conformance/MATRIX.md
@@ -85,10 +85,10 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform
| Rule | `tui` | `web chat` | `chat-sdk` | `chat-sdk-dm` | `chat-sdk-text` | `discord` | `discord-dm` | `linq` | `linq-dm` | `slack` | `slack-dm` | `teams` | `teams-dm` | `telegram` | `telegram-dm` | `github` | `linear` | `photon` | `twilio` |
| --- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
-| an image a person sends reaches the agent with its bytes and type | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ❌<sup>[21](#note-21)</sup> | ❌<sup>[21](#note-21)</sup> | ❌<sup>[21](#note-21)</sup> | ❌<sup>[22](#note-22)</sup> | ❌<sup>[22](#note-22)</sup> | ✅ | ✅ | ✅ | ✅ | ❌<sup>[23](#note-23)</sup> | ❌<sup>[23](#note-23)</sup> | ❌<sup>[24](#note-24)</sup> | ❌<sup>[24](#note-24)</sup> | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ❌<sup>[25](#note-25)</sup> |
-| a PDF a person sends reaches the agent with its bytes and type | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ❌<sup>[21](#note-21)</sup> | | ❌<sup>[21](#note-21)</sup> | ❌<sup>[22](#note-22)</sup> | | ✅ | | ✅ | | ❌<sup>[23](#note-23)</sup> | | ❌<sup>[24](#note-24)</sup> | | —<sup>[20](#note-20)</sup> | ❌<sup>[26](#note-26)</sup> | ✅ | ❌<sup>[25](#note-25)</sup> |
-| a file that can't be downloaded reaches the agent as a note, not a link, and the next message still works | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ❌<sup>[21](#note-21)</sup> | | ❌<sup>[21](#note-21)</sup> | ❌<sup>[22](#note-22)</sup> | | ✅ | | ✅ | | ❌<sup>[23](#note-23)</sup> | | ✅ | | —<sup>[20](#note-20)</sup> | ❌<sup>[27](#note-27)</sup> | ✅ | ❌<sup>[25](#note-25)</sup> |
-| a file sent earlier in the conversation is still there on a later message | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ❌<sup>[21](#note-21)</sup> | | ❌<sup>[21](#note-21)</sup> | —<sup>[28](#note-28)</sup> | | ✅ | | ✅ | | ❌<sup>[23](#note-23)</sup> | | ❌<sup>[24](#note-24)</sup> | | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ❌<sup>[25](#note-25)</sup> |
+| an image a person sends reaches the agent with its bytes and type | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ | ❌<sup>[21](#note-21)</sup> | ❌<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ | ✅ | ❌<sup>[22](#note-22)</sup> | ❌<sup>[22](#note-22)</sup> | ✅ | ✅ | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ |
+| a PDF a person sends reaches the agent with its bytes and type | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ✅ | | ✅ | ❌<sup>[21](#note-21)</sup> | | ✅ | | ✅ | | ❌<sup>[22](#note-22)</sup> | | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ |
+| a file that can't be downloaded reaches the agent as a note, not a link, and the next message still works | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ✅ | | ✅ | ❌<sup>[21](#note-21)</sup> | | ✅ | | ✅ | | ❌<sup>[22](#note-22)</sup> | | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ |
+| a file sent earlier in the conversation is still there on a later message | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | ✅ | | ✅ | —<sup>[23](#note-23)</sup> | | ✅ | | ✅ | | ❌<sup>[22](#note-22)</sup> | | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ |
## Notes
@@ -112,11 +112,6 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform
18. <a id="note-18"></a>the platform has no second person who can act
19. <a id="note-19"></a>the platform has no second person who can act or buttons a person can press
20. <a id="note-20"></a>the platform has no files a person can send
-21. <a id="note-21"></a>the bridge passes each attachment's URL to the model provider, whose download fails (#855, #3419)
-22. <a id="note-22"></a>eve reads only the slash command's message option, not its attachment options
-23. <a id="note-23"></a>Teams drops files unless files.enabled is set and their host is allowlisted
-24. <a id="note-24"></a>#1217: Telegram serves files as application/octet-stream, which wins over their known type
-25. <a id="note-25"></a>the channel parses MMS media but never passes it to the agent
-26. <a id="note-26"></a>eve reads only uploaded images from Linear; other files stay links in the text
-27. <a id="note-27"></a>a Linear upload eve can't download stays a link in the text, with no note
-28. <a id="note-28"></a>each slash command starts its own session, so no later message shares one with the file
+21. <a id="note-21"></a>eve reads only the slash command's message option, not its attachment options
+22. <a id="note-22"></a>Teams drops files unless files.enabled is set and their host is allowlisted
+23. <a id="note-23"></a>each slash command starts its own session, so no later message shares one with the file
diff --git a/packages/eve/src/internal/testing/channel-conformance/chat-sdk-driver.ts b/packages/eve/src/internal/testing/channel-conformance/chat-sdk-driver.ts
index fdc147cc9..8d591e8cf 100644
--- a/packages/eve/src/internal/testing/channel-conformance/chat-sdk-driver.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/chat-sdk-driver.ts
@@ -1,8 +1,8 @@
-// Sends attachments with the text, as Linq and Photon do.
import { chatSdkChannel, messageToUserContent } from "#public/channels/chat-sdk/index.js";
import {
type Adapter,
type AdapterPostableMessage,
+ type Attachment,
BaseFormatConverter,
type ChatInstance,
Message,
@@ -45,12 +45,16 @@ interface CardNode {
readonly value?: string;
}
+interface InboundFile extends Pick<SentFile, "mediaType" | "name"> {
+ readonly url: string;
+}
+
type Inbound =
| {
readonly kind: "message";
readonly text: string;
- /** Files on the message, as the adapter lists them: a URL to each, not its bytes. */
- readonly files?: readonly Pick<SentFile, "mediaType" | "name">[];
+ /** Files on the message, as the platform lists them: a URL to each, not its bytes. */
+ readonly files?: readonly InboundFile[];
}
| {
readonly kind: "action";
@@ -177,6 +181,8 @@ function chatSdkDriverWith(input: {
nextThread += 1;
const threadId = `${ADAPTER}:D${nextThread}`;
let sequence = 0;
+ /** Files a person sent, by the URL the platform lists each under. */
+ const uploads = new Map<string, SentFile>();
function inbound(body: Inbound): Request {
return new Request(`https://agent.example.com/eve/v1/${ADAPTER}`, {
@@ -191,7 +197,14 @@ function chatSdkDriverWith(input: {
inbound,
createChannel(record) {
const dm = input.surface === "private";
- const adapter = fakeAdapter(threadId, record, () => (sequence += 1), input.render, dm);
+ const adapter = fakeAdapter({
+ dm,
+ nextId: () => (sequence += 1),
+ record,
+ render: input.render,
+ threadId,
+ uploads,
+ });
const bridge = chatSdkChannel({
adapters: { [ADAPTER]: adapter },
concurrency: "concurrent",
@@ -232,7 +245,11 @@ function chatSdkDriverWith(input: {
},
message: (text, files = []) =>
inbound({
- files: files.map(({ mediaType, name }) => ({ mediaType, name })),
+ files: files.map((file) => {
+ const url = `https://files.conformance.example/${uploads.size + 1}/${encodeURIComponent(file.name)}`;
+ uploads.set(url, file);
+ return { mediaType: file.mediaType, name: file.name, url };
+ }),
kind: "message",
text,
}),
@@ -251,18 +268,41 @@ function isPost(call: PlatformCall): boolean {
return call.method === "postMessage" || call.method === "editMessage";
}
-function fakeAdapter(
- threadId: string,
- record: (call: PlatformCall) => void,
- nextId: () => number,
- render: (posted: AdapterPostableMessage) => unknown,
- dm: boolean,
-): Adapter {
+function fakeAdapter({
+ dm,
+ nextId,
+ record,
+ render,
+ threadId,
+ uploads,
+}: {
+ readonly dm: boolean;
+ readonly nextId: () => number;
+ readonly record: (call: PlatformCall) => void;
+ readonly render: (posted: AdapterPostableMessage) => unknown;
+ readonly threadId: string;
+ readonly uploads: ReadonlyMap<string, SentFile>;
+}): Adapter {
+ /** Downloads a file as an adapter's `fetchData` does: with its own auth, failing on a refusal. */
+ async function fetchData(url: string): Promise<Buffer> {
+ const file = uploads.get(url);
+ record({ body: {}, method: `GET ${url}`, response: {} });
+ if (file === undefined || file.downloadable === false) {
+ throw new Error(`Failed to fetch file: 403 Forbidden`);
+ }
+ return Buffer.from(file.bytes);
+ }
+
const visibility = dm ? ("private" as const) : ("workspace" as const);
let chat: ChatInstance | null = null;
const self = {
name: ADAPTER,
userName: "eve",
+ // As Slack's adapter does, so the channel can rebuild a download after the queue.
+ rehydrateAttachment(attachment: Attachment): Attachment {
+ const { url } = attachment;
+ return url === undefined ? attachment : { ...attachment, fetchData: () => fetchData(url) };
+ },
async initialize(instance: ChatInstance) {
chat = instance;
},
@@ -287,7 +327,7 @@ function fakeAdapter(
adapter,
threadId,
// A person mentions the bot to start a channel thread; Chat routes the rest by subscription.
- inboundMessage(threadId, id, body.text, !dm, body.files),
+ inboundMessage(threadId, id, body.text, !dm, body.files, fetchData),
options,
);
}
@@ -332,16 +372,17 @@ function inboundMessage(
id: string,
text: string,
isMention = false,
- files: readonly Pick<SentFile, "mediaType" | "name">[] = [],
+ files: readonly InboundFile[] = [],
+ fetchData?: (url: string) => Promise<Buffer>,
): Message {
return new Message({
attachments: files.map((file) => ({
+ fetchData: fetchData && (() => fetchData(file.url)),
mimeType: file.mediaType,
name: file.name,
type: file.mediaType.startsWith("image/") ? ("image" as const) : ("file" as const),
- // The AI SDK refuses IP-literal URLs before connecting, so the provider's download
- // fails as it would for a private platform URL, and nothing leaves the machine.
- url: `http://127.0.0.1:9/files/${encodeURIComponent(file.name)}`,
+ // Private to the platform, as Slack's or Teams' are: only `fetchData` can download it.
+ url: file.url,
})),
author: PERSON,
formatted: parseMarkdown(text),
diff --git a/packages/eve/src/internal/testing/channel-conformance/conformance.ts b/packages/eve/src/internal/testing/channel-conformance/conformance.ts
index ef1f64eb8..0fab1ab70 100644
--- a/packages/eve/src/internal/testing/channel-conformance/conformance.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/conformance.ts
@@ -131,27 +131,6 @@ function filesNotSeen(
);
}
-/**
- * The Chat SDK bridge hands the model provider each attachment's URL, which it
- * then fails to download. `symptom` is how the failed turn shows on the channel.
- */
-function attachmentUrlPassedThrough(
- symptom: RegExp,
-): Partial<Record<ContractRuleName, BrokenCell>> {
- return Object.fromEntries(
- [IMAGE_REACHES_AGENT, PDF_REACHES_AGENT, UNDOWNLOADABLE_FILE, EARLIER_FILE].map((rule) => [
- rule,
- {
- reason:
- "the bridge passes each attachment's URL to the model provider, whose download fails (#855, #3419)",
- symptom,
- },
- ]),
- );
-}
-
-const ATTACHMENT_URL_PASSED_THROUGH = attachmentUrlPassedThrough(/AI_DownloadError/u);
-
const SIGN_IN_NOT_SHOWN = /Timed out waiting for the bot to show/u;
/** The channel has no default `authorization.required` renderer, so a sign-in shows nothing. */
@@ -185,7 +164,6 @@ const UNNAMED_RESPONDER =
"a resolved prompt doesn't say who answered; input.resolved carries no responder";
const CHAT_SDK_BROKEN = {
- ...ATTACHMENT_URL_PASSED_THROUGH,
...unnamedAnsweredPrompts(UNNAMED_RESPONDER, [
"approvalPress",
"approvalText",
@@ -255,12 +233,6 @@ const SIGN_IN_LINK_POSTED_TO_THREAD = {
} satisfies Partial<Record<ContractRuleName, BrokenCell>>;
const TELEGRAM_BROKEN = {
- ...filesNotSeen(
- "#1217: Telegram serves files as application/octet-stream, which wins over their known type",
- IMAGE_REACHES_AGENT,
- PDF_REACHES_AGENT,
- EARLIER_FILE,
- ),
...unnamedAnsweredPrompts(UNNAMED_RESPONDER, [
"approvalPress",
"approvalText",
@@ -290,10 +262,7 @@ const WEB_CHAT_SINGLE_PERSON =
const channelConformance = {
"chat-sdk": [
{ driver: chatSdkDriver, broken: { ...CHAT_SDK_BROKEN, ...SIGN_IN_ONLY_IN_DMS } },
- {
- driver: chatSdkTextDriver,
- broken: { ...ATTACHMENT_URL_PASSED_THROUGH },
- },
+ { driver: chatSdkTextDriver },
],
"chat-sdk-dm": [{ dm: true, driver: () => chatSdkDriver("private"), broken: CHAT_SDK_BROKEN }],
discord: [{ driver: discordDriver, broken: DISCORD_BROKEN, unsupported: DISCORD_UNSUPPORTED }],
@@ -321,14 +290,6 @@ const channelConformance = {
{
driver: linearDriver,
broken: {
- ...filesNotSeen(
- "eve reads only uploaded images from Linear; other files stay links in the text",
- PDF_REACHES_AGENT,
- ),
- ...filesNotSeen(
- "a Linear upload eve can't download stays a link in the text, with no note",
- UNDOWNLOADABLE_FILE,
- ),
"only the person signing in sees the sign-in link and code": {
reason:
"the code is in the elicitation body the whole issue sees; who sees the auth signal's link is unverified",
@@ -362,20 +323,7 @@ const channelConformance = {
},
},
],
- twilio: [
- {
- driver: twilioDriver,
- broken: {
- ...filesNotSeen(
- "the channel parses MMS media but never passes it to the agent",
- IMAGE_REACHES_AGENT,
- PDF_REACHES_AGENT,
- UNDOWNLOADABLE_FILE,
- EARLIER_FILE,
- ),
- },
- },
- ],
+ twilio: [{ driver: twilioDriver }],
"web-chat": [
{
driver: webChatDriver,
diff --git a/packages/eve/src/internal/testing/channel-conformance/harness.ts b/packages/eve/src/internal/testing/channel-conformance/harness.ts
index 55b1a0f2f..ad2fdfdc0 100644
--- a/packages/eve/src/internal/testing/channel-conformance/harness.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/harness.ts
@@ -156,6 +156,12 @@ export interface ChannelDriver {
shownMessage?(call: PlatformCall): ShownMessage | undefined;
/** How the person driving the conversation appears in the platform's text, in any form. */
readonly personShownAs?: readonly string[];
+ /**
+ * The address a person's next message continues, when it continues one the
+ * bot claimed, such as the Telegram message it replies to. The harness waits
+ * for the session to claim it before sending.
+ */
+ nextAddress?(): string | undefined;
}
/**
@@ -269,6 +275,8 @@ export interface ClientView {
shownPrompt?(prompt: string): ShownMessage | Promise<ShownMessage>;
/** How the person appears in the client's text, in any form. */
readonly personShownAs?: readonly string[];
+ /** See {@link ChannelDriver.nextAddress}. */
+ nextAddress?(): string | undefined;
/** Everything the client shows now that a person can read or open, one entry per message. */
shown(): readonly ShownText[] | Promise<readonly ShownText[]>;
/** What the client shows now, for timeout errors. */
@@ -512,6 +520,7 @@ function webhookView(
.at(-1)!;
},
personShownAs: driver.personShownAs ?? [],
+ nextAddress: driver.nextAddress?.bind(driver),
shown: () =>
calls.flatMap((call) => {
const shown = driver.shownMessage?.(call);
@@ -777,6 +786,9 @@ async function converse(
const conversation: ChannelConversation = {
async say(text, files) {
await waitForStepsToFinish([...sessions.values()], wait);
+ // A step can be done before its aliases are claimed; see waitForAddress.
+ const address = view.nextAddress?.();
+ if (address !== undefined) await waitForAddress(address);
await view.say(text, files);
},
press: (option, person = "alice") => view.press(option, person),
@@ -929,8 +941,11 @@ async function cancelUntilResting(session: Session): Promise<void> {
while (Date.now() < deadline) {
await session.cancel();
const tail = await session.getStreamTailIndex();
- const reader = (await session.getEventStream({ startIndex: tail })).getReader();
- last = (await reader.read().finally(() => reader.cancel())).value?.type;
+ // An empty stream has nothing to read, and reading it waits for its first event.
+ if (tail >= 0) {
+ const reader = (await session.getEventStream({ startIndex: tail })).getReader();
+ last = (await reader.read().finally(() => reader.cancel())).value?.type;
+ }
running = await runningSteps(session);
if (last === "session.waiting" && running.length === 0) return;
await new Promise((resolve) => setTimeout(resolve, 25));
diff --git a/packages/eve/src/internal/testing/channel-conformance/telegram-driver.ts b/packages/eve/src/internal/testing/channel-conformance/telegram-driver.ts
index 783ff6068..460ce0d60 100644
--- a/packages/eve/src/internal/testing/channel-conformance/telegram-driver.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/telegram-driver.ts
@@ -1,3 +1,4 @@
+import { telegramContinuationToken } from "#public/channels/telegram/api.js";
import { telegramChannel } from "#public/channels/telegram/index.js";
import {
type ChannelDriver,
@@ -203,6 +204,11 @@ export function telegramDriver(surface: Exclude<Surface, "public"> = "shared"):
};
},
personShownAs: [PERSON.first_name],
+ // A reply to the bot's message continues the session that posted it.
+ nextAddress: () =>
+ group && lastBotMessage !== undefined
+ ? telegramContinuationToken({ chatId: CHAT.id, conversationId: lastBotMessage })
+ : undefined,
press: (option, person: Person) => {
const { data, messageId } = option.handle as PressHandle;
return update({
diff --git a/packages/eve/src/internal/testing/channel-conformance/twilio-driver.ts b/packages/eve/src/internal/testing/channel-conformance/twilio-driver.ts
index 2e5d04d62..0f009c048 100644
--- a/packages/eve/src/internal/testing/channel-conformance/twilio-driver.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/twilio-driver.ts
@@ -6,8 +6,10 @@ import {
numberedOptions,
recordingFetch,
type SentFile,
+ serveFile,
} from "#internal/testing/channel-conformance/harness.js";
+const ACCOUNT_SID = "AC123";
const AUTH_TOKEN = "twilio-conformance-secret";
const TO = "+15550000001";
let nextPerson = 0;
@@ -17,15 +19,21 @@ export function twilioDriver(): ChannelDriver {
nextPerson += 1;
const PERSON = `+1555000${String(nextPerson).padStart(4, "0")}`;
let messageSid = 0;
+ /** Files a person sent, by the media URL the webhook listed each under. */
+ const uploads = new Map<string, SentFile>();
const webhookUrl = "https://agent.example.com/eve/v1/twilio/messages";
function message(text: string, files: readonly SentFile[] = []): Request {
messageSid += 1;
// An MMS lists its media by URL, numbered from zero.
- const media = files.flatMap((file, index) => [
- [`MediaContentType${index}`, file.mediaType],
- [`MediaUrl${index}`, `https://api.twilio.com/media/conformance/${messageSid}/${index}`],
- ]);
+ const media = files.flatMap((file, index) => {
+ const url = `https://api.twilio.com/2010-04-01/Accounts/${ACCOUNT_SID}/Messages/MM${messageSid}/Media/ME${index}`;
+ uploads.set(url, file);
+ return [
+ [`MediaContentType${index}`, file.mediaType],
+ [`MediaUrl${index}`, url],
+ ];
+ });
const params = new URLSearchParams({
Body: text,
From: PERSON,
@@ -46,6 +54,18 @@ export function twilioDriver(): ChannelDriver {
async function decode(request: Request): Promise<PlatformCall> {
const url = new URL(request.url);
+ if (request.method === "GET" && url.pathname.includes("/Media/")) {
+ // Twilio serves media to the account's Basic auth.
+ const authorized =
+ request.headers.get("authorization") === `Basic ${btoa(`${ACCOUNT_SID}:${AUTH_TOKEN}`)}`;
+ return {
+ body: {},
+ method: `GET ${url.pathname}`,
+ response: authorized
+ ? serveFile(uploads.get(url.href))
+ : new Response("Unauthorized", { status: 401 }),
+ };
+ }
return {
body: Object.fromEntries(new URLSearchParams(await request.text())),
method: url.pathname.split("/").at(-1) ?? "",
@@ -60,7 +80,7 @@ export function twilioDriver(): ChannelDriver {
createChannel: (record) =>
twilioChannel({
allowFrom: "*",
- credentials: { accountSid: "AC123", authToken: AUTH_TOKEN },
+ credentials: { accountSid: ACCOUNT_SID, authToken: AUTH_TOKEN },
api: { fetch: recordingFetch(record, decode) },
messaging: { from: TO },
}),
diff --git a/packages/eve/src/public/channels/linear/inbound-images.ts b/packages/eve/src/public/channels/linear/inbound-images.ts
deleted file mode 100644
index f1e40582b..000000000
--- a/packages/eve/src/public/channels/linear/inbound-images.ts
+++ /dev/null
@@ -1,145 +0,0 @@
-import type { FilePart, TextPart, UserContent } from "ai";
-
-import type { LinearFetch } from "#public/channels/linear/api.js";
-import {
- resolveLinearAccessToken,
- type LinearChannelCredentials,
-} from "#public/channels/linear/auth.js";
-
-const LINEAR_UPLOAD_ORIGIN = "https://uploads.linear.app";
-const MARKDOWN_IMAGE_PATTERN =
- /!\[([^\]\r\n]*)\]\(\s*(?:<([^>\r\n]+)>|([^\s)\r\n]+))(?:\s+(?:"[^"\r\n]*"|'[^'\r\n]*'|\([^)\r\n]*\)))?\s*\)/gu;
-
-/** One trusted Linear upload referenced by markdown image syntax. */
-interface LinearUploadImageReference {
- readonly altText: string;
- readonly end: number;
- readonly start: number;
- readonly url: URL;
-}
-
-/** Extracts markdown image references that target Linear's exact upload origin. */
-export function extractLinearUploadImageReferences(
- markdown: string,
-): readonly LinearUploadImageReference[] {
- const references: LinearUploadImageReference[] = [];
- for (const match of markdown.matchAll(MARKDOWN_IMAGE_PATTERN)) {
- const href = match[2] ?? match[3];
- const start = match.index;
- if (href === undefined || start === undefined) continue;
-
- const url = parseLinearUploadUrl(href);
- if (url === null) continue;
-
- references.push({
- altText: match[1] ?? "",
- end: start + match[0].length,
- start,
- url,
- });
- }
- return references;
-}
-
-/** Adds authenticated Linear upload images to otherwise text-only inbound content. */
-export async function attachLinearInboundImages(input: {
- readonly content: UserContent;
- readonly credentials?: LinearChannelCredentials;
- readonly fetch?: LinearFetch;
-}): Promise<UserContent> {
- if (typeof input.content !== "string") return input.content;
-
- const references = extractLinearUploadImageReferences(input.content);
- if (references.length === 0) return input.content;
-
- let token: string;
- try {
- token = await resolveLinearAccessToken(input.credentials?.accessToken);
- } catch {
- return input.content;
- }
-
- const fetchImage = input.fetch ?? fetch;
- const files = await Promise.all(
- references.map((reference) => fetchLinearUploadImage(reference.url, token, fetchImage)),
- );
- if (files.every((file) => file === null)) return input.content;
-
- return buildLinearImageContent(input.content, references, files);
-}
-
-function parseLinearUploadUrl(href: string): URL | null {
- let url: URL;
- try {
- url = new URL(href);
- } catch {
- return null;
- }
- if (url.origin !== LINEAR_UPLOAD_ORIGIN || url.username !== "" || url.password !== "") {
- return null;
- }
- return url;
-}
-
-async function fetchLinearUploadImage(
- url: URL,
- token: string,
- fetchImage: LinearFetch,
-): Promise<FilePart | null> {
- if (parseLinearUploadUrl(url.href) === null) return null;
-
- try {
- const response = await fetchImage(url.href, {
- credentials: "omit",
- headers: {
- accept: "image/*",
- authorization: `Bearer ${token}`,
- },
- redirect: "manual",
- });
- if (!response.ok) return null;
-
- const mediaType = readImageMediaType(response.headers.get("content-type"));
- if (mediaType === null) return null;
-
- return {
- data: Buffer.from(await response.arrayBuffer()),
- mediaType,
- type: "file",
- };
- } catch {
- return null;
- }
-}
-
-function readImageMediaType(contentType: string | null): string | null {
- const mediaType = contentType?.split(";", 1)[0]?.trim().toLowerCase();
- return mediaType?.startsWith("image/") === true && mediaType.length > "image/".length
- ? mediaType
- : null;
-}
-
-function buildLinearImageContent(
- markdown: string,
- references: readonly LinearUploadImageReference[],
- files: readonly (FilePart | null)[],
-): UserContent {
- let cursor = 0;
- let text = "";
- const attached: FilePart[] = [];
-
- for (const [index, reference] of references.entries()) {
- const file = files[index];
- if (file === null || file === undefined) continue;
-
- text += markdown.slice(cursor, reference.start);
- text += reference.altText;
- cursor = reference.end;
- attached.push(file);
- }
- text += markdown.slice(cursor);
-
- if (text.trim().length === 0) return attached;
- const textPart: TextPart = { text, type: "text" };
- return [textPart, ...attached];
-}
diff --git a/packages/eve/src/public/channels/linear/inbound-images.test.ts b/packages/eve/src/public/channels/linear/inbound-uploads.test.ts
similarity index 60%
rename from packages/eve/src/public/channels/linear/inbound-images.test.ts
rename to packages/eve/src/public/channels/linear/inbound-uploads.test.ts
index e3eb77bb9..885e39e62 100644
--- a/packages/eve/src/public/channels/linear/inbound-images.test.ts
+++ b/packages/eve/src/public/channels/linear/inbound-uploads.test.ts
@@ -2,37 +2,46 @@ import type { FilePart, TextPart } from "ai";
import { describe, expect, it, vi } from "vitest";
import {
- attachLinearInboundImages,
- extractLinearUploadImageReferences,
-} from "#public/channels/linear/inbound-images.js";
+ attachLinearInboundUploads,
+ extractLinearUploadReferences,
+} from "#public/channels/linear/inbound-uploads.js";
-describe("extractLinearUploadImageReferences", () => {
- it("extracts trusted Linear markdown images and ignores ordinary image hosts", () => {
+describe("extractLinearUploadReferences", () => {
+ it("extracts trusted Linear markdown images and file links and ignores other hosts", () => {
const markdown = [
'First ![screenshot](https://uploads.linear.app/acme/one/image.png?signature=one "One").',
"Second ![diagram](<https://uploads.linear.app/acme/two/diagram.jpg?signature=two>).",
- "External ![chart](https://images.example.com/chart.png).",
+ "Report [report.pdf](https://uploads.linear.app/acme/three/report.pdf).",
+ "External ![chart](https://images.example.com/chart.png) [docs](https://example.com/a.pdf).",
].join("\n");
expect(
- extractLinearUploadImageReferences(markdown).map((reference) => ({
- altText: reference.altText,
+ extractLinearUploadReferences(markdown).map((reference) => ({
+ image: reference.image,
+ label: reference.label,
url: reference.url.href,
})),
).toEqual([
{
- altText: "screenshot",
+ image: true,
+ label: "screenshot",
url: "https://uploads.linear.app/acme/one/image.png?signature=one",
},
{
- altText: "diagram",
+ image: true,
+ label: "diagram",
url: "https://uploads.linear.app/acme/two/diagram.jpg?signature=two",
},
+ {
+ image: false,
+ label: "report.pdf",
+ url: "https://uploads.linear.app/acme/three/report.pdf",
+ },
]);
});
});
-describe("attachLinearInboundImages", () => {
+describe("attachLinearInboundUploads", () => {
it("fetches image bytes with the resolved credential and response media type", async () => {
const token = vi.fn().mockResolvedValue("linear-token");
const fetchMock = vi.fn().mockResolvedValue(
@@ -41,7 +50,7 @@ describe("attachLinearInboundImages", () => {
}),
);
- const content = await attachLinearInboundImages({
+ const content = await attachLinearInboundUploads({
content:
"Review ![screenshot](https://uploads.linear.app/acme/one/image.png?signature=secret).",
credentials: { accessToken: token },
@@ -65,7 +74,50 @@ describe("attachLinearInboundImages", () => {
expect(file.data).toEqual(Buffer.from([1, 2, 3, 4]));
});
- it("retains surrounding and fallback markdown in mixed content", async () => {
+ it("fetches an uploaded file link with its response media type and name", async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValue(
+ new Response("%PDF-1.4", { headers: { "content-type": "application/pdf" } }),
+ );
+
+ const content = await attachLinearInboundUploads({
+ content: "See [report.pdf](https://uploads.linear.app/acme/one/report.pdf).",
+ credentials: { accessToken: "linear-token" },
+ fetch: fetchMock,
+ });
+
+ const [, init] = fetchMock.mock.calls[0]!;
+ expect(new Headers(init.headers).get("accept")).toBe("*/*");
+ expect(content).toEqual([
+ { text: "See report.pdf.", type: "text" },
+ {
+ data: Buffer.from("%PDF-1.4"),
+ filename: "report.pdf",
+ mediaType: "application/pdf",
+ type: "file",
+ },
+ ]);
+ });
+
+ it("leaves a note for a file link when Linear answers with an HTML page", async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValue(new Response("<html>", { headers: { "content-type": "text/html" } }));
+
+ await expect(
+ attachLinearInboundUploads({
+ content: "See [report.pdf](https://uploads.linear.app/acme/one/report.pdf).",
+ credentials: { accessToken: "linear-token" },
+ fetch: fetchMock,
+ }),
+ ).resolves.toEqual([
+ { text: "See report.pdf.", type: "text" },
+ { text: "Attachment report.pdf could not be retrieved.", type: "text" },
+ ]);
+ });
+
+ it("keeps surrounding text and untrusted markdown, and notes uploads it can't read", async () => {
const fetchMock = vi.fn(async (url: string | URL | Request, _init?: RequestInit) => {
if (String(url).includes("/attached.png")) {
return new Response(new Uint8Array([7, 8]), {
@@ -79,7 +131,7 @@ describe("attachLinearInboundImages", () => {
const nonImage = "![document](https://uploads.linear.app/acme/two/document.txt?signature=two)";
const hostile = "![external](https://images.example.com/external.png)";
- const content = await attachLinearInboundImages({
+ const content = await attachLinearInboundUploads({
content:
`Before ![attached](https://uploads.linear.app/acme/one/attached.png?signature=one) ` +
`between ${nonImage} after ${hostile}.`,
@@ -88,14 +140,18 @@ describe("attachLinearInboundImages", () => {
});
expect(fetchMock).toHaveBeenCalledTimes(2);
- expect(content).toHaveLength(2);
+ expect(content).toHaveLength(3);
expect((content[0] as TextPart).text).toBe(
- `Before attached between ${nonImage} after ${hostile}.`,
+ `Before attached between document after ${hostile}.`,
);
expect((content[1] as FilePart).data).toEqual(Buffer.from([7, 8]));
+ expect(content[2]).toEqual({
+ text: "Attachment document could not be retrieved.",
+ type: "text",
+ });
});
- it("preserves markdown when trusted downloads fail, redirect, or are not images", async () => {
+ it("notes trusted uploads that fail, redirect, or are not images", async () => {
const markdown = [
"![failed](https://uploads.linear.app/acme/one/failed.png)",
"![redirect](https://uploads.linear.app/acme/two/redirect.png)",
@@ -114,12 +170,18 @@ describe("attachLinearInboundImages", () => {
});
await expect(
- attachLinearInboundImages({
+ attachLinearInboundUploads({
content: markdown,
credentials: { accessToken: "linear-token" },
fetch: fetchMock,
}),
- ).resolves.toBe(markdown);
+ ).resolves.toEqual([
+ { text: "failed redirect text", type: "text" },
+ ...["failed", "redirect", "text"].map((label) => ({
+ text: `Attachment ${label} could not be retrieved.`,
+ type: "text",
+ })),
+ ]);
expect(fetchMock).toHaveBeenCalledTimes(3);
for (const [, init] of fetchMock.mock.calls) {
expect(init?.redirect).toBe("manual");
@@ -138,7 +200,7 @@ describe("attachLinearInboundImages", () => {
const fetchMock = vi.fn();
await expect(
- attachLinearInboundImages({
+ attachLinearInboundUploads({
content: markdown,
credentials: { accessToken: token },
fetch: fetchMock,
diff --git a/packages/eve/src/public/channels/linear/inbound-uploads.ts b/packages/eve/src/public/channels/linear/inbound-uploads.ts
new file mode 100644
index 000000000..1b90c1a45
--- /dev/null
+++ b/packages/eve/src/public/channels/linear/inbound-uploads.ts
@@ -0,0 +1,158 @@
+import type { FilePart, TextPart, UserContent } from "ai";
+
+import { readLimitedBytes } from "#internal/attachments/limited-read.js";
+import type { LinearFetch } from "#public/channels/linear/api.js";
+import {
+ resolveLinearAccessToken,
+ type LinearChannelCredentials,
+} from "#public/channels/linear/auth.js";
+import { DEFAULT_UPLOAD_POLICY } from "#public/channels/upload-policy.js";
+
+const LINEAR_UPLOAD_ORIGIN = "https://uploads.linear.app";
+// Linear writes an uploaded image as `![name](url)` and any other uploaded file as `[name](url)`.
+const MARKDOWN_UPLOAD_PATTERN =
+ /(!?)\[([^\]\r\n]*)\]\(\s*(?:<([^>\r\n]+)>|([^\s)\r\n]+))(?:\s+(?:"[^"\r\n]*"|'[^'\r\n]*'|\([^)\r\n]*\)))?\s*\)/gu;
+
+/** One trusted Linear upload referenced by markdown image or link syntax. */
+interface LinearUploadReference {
+ readonly end: number;
+ /** Whether markdown embeds it as an image, so only image bytes are accepted for it. */
+ readonly image: boolean;
+ readonly label: string;
+ readonly start: number;
+ readonly url: URL;
+}
+
+/** Extracts markdown images and links that target Linear's exact upload origin. */
+export function extractLinearUploadReferences(markdown: string): readonly LinearUploadReference[] {
+ const references: LinearUploadReference[] = [];
+ for (const match of markdown.matchAll(MARKDOWN_UPLOAD_PATTERN)) {
+ const href = match[3] ?? match[4];
+ const start = match.index;
+ if (href === undefined || start === undefined) continue;
+
+ const url = parseLinearUploadUrl(href);
+ if (url === null) continue;
+
+ references.push({
+ end: start + match[0].length,
+ image: match[1] === "!",
+ label: match[2] ?? "",
+ start,
+ url,
+ });
+ }
+ return references;
+}
+
+/** Adds authenticated Linear uploads, images and other files, to otherwise text-only inbound content. */
+export async function attachLinearInboundUploads(input: {
+ readonly content: UserContent;
+ readonly credentials?: LinearChannelCredentials;
+ readonly fetch?: LinearFetch;
+}): Promise<UserContent> {
+ if (typeof input.content !== "string") return input.content;
+
+ const references = extractLinearUploadReferences(input.content);
+ if (references.length === 0) return input.content;
+
+ let token: string;
+ try {
+ token = await resolveLinearAccessToken(input.credentials?.accessToken);
+ } catch {
+ return input.content;
+ }
+
+ const fetchUpload = input.fetch ?? fetch;
+ const files = await Promise.all(
+ references.map((reference) => fetchLinearUpload(reference, token, fetchUpload)),
+ );
+
+ return buildLinearUploadContent(input.content, references, files);
+}
+
+function parseLinearUploadUrl(href: string): URL | null {
+ let url: URL;
+ try {
+ url = new URL(href);
+ } catch {
+ return null;
+ }
+ if (url.origin !== LINEAR_UPLOAD_ORIGIN || url.username !== "" || url.password !== "") {
+ return null;
+ }
+ return url;
+}
+
+async function fetchLinearUpload(
+ reference: LinearUploadReference,
+ token: string,
+ fetchUpload: LinearFetch,
+): Promise<FilePart | null> {
+ if (parseLinearUploadUrl(reference.url.href) === null) return null;
+
+ try {
+ const response = await fetchUpload(reference.url.href, {
+ credentials: "omit",
+ headers: {
+ accept: reference.image ? "image/*" : "*/*",
+ authorization: `Bearer ${token}`,
+ },
+ redirect: "manual",
+ });
+ if (!response.ok) return null;
+
+ const mediaType = readMediaType(response.headers.get("content-type"), reference.image);
+ if (mediaType === null) return null;
+
+ const file: FilePart = {
+ data: await readLimitedBytes(response, DEFAULT_UPLOAD_POLICY.maxBytes, "linear"),
+ mediaType,
+ type: "file",
+ };
+ if (!reference.image && reference.label.length > 0) file.filename = reference.label;
+ return file;
+ } catch {
+ return null;
+ }
+}
+
+function readMediaType(contentType: string | null, image: boolean): string | null {
+ const mediaType = contentType?.split(";", 1)[0]?.trim().toLowerCase();
+ if (image) {
+ return mediaType?.startsWith("image/") === true && mediaType.length > "image/".length
+ ? mediaType
+ : null;
+ }
+ // An HTML answer is a sign-in or error page, not the uploaded file.
+ if (mediaType === "text/html") return null;
+ return mediaType === undefined || mediaType.length === 0 ? "application/octet-stream" : mediaType;
+}
+
+function buildLinearUploadContent(
+ markdown: string,
+ references: readonly LinearUploadReference[],
+ files: readonly (FilePart | null)[],
+): UserContent {
+ let cursor = 0;
+ let text = "";
+ const attached: (FilePart | TextPart)[] = [];
+
+ for (const [index, reference] of references.entries()) {
+ text += markdown.slice(cursor, reference.start);
+ text += reference.label;
+ cursor = reference.end;
+ // A link the agent can't open is no use to it; a note tells it the file didn't arrive.
+ attached.push(
+ files[index] ?? {
+ text: `Attachment ${reference.label || "file"} could not be retrieved.`,
+ type: "text",
+ },
+ );
+ }
+ text += markdown.slice(cursor);
+
+ if (text.trim().length === 0) return attached;
+ const textPart: TextPart = { text, type: "text" };
+ return [textPart, ...attached];
+}
diff --git a/packages/eve/src/public/channels/linear/linearChannel.ts b/packages/eve/src/public/channels/linear/linearChannel.ts
index 20a70bb60..10a265cc2 100644
--- a/packages/eve/src/public/channels/linear/linearChannel.ts
+++ b/packages/eve/src/public/channels/linear/linearChannel.ts
@@ -15,7 +15,7 @@ import {
import type { LinearChannelCredentials } from "#public/channels/linear/auth.js";
import { LINEAR_CHANNEL_DEFAULT_ROUTE } from "#public/channels/linear/constants.js";
import { createDefaultEvents, defaultOnAgentSession } from "#public/channels/linear/defaults.js";
-import { attachLinearInboundImages } from "#public/channels/linear/inbound-images.js";
+import { attachLinearInboundUploads } from "#public/channels/linear/inbound-uploads.js";
import {
formatLinearContextBlock,
linearContinuationToken,
@@ -350,7 +350,7 @@ async function dispatchAgentSession(input: {
const result = await input.onAgentSession(context, event);
if (result === null) return;
- const message = await attachLinearInboundImages({
+ const message = await attachLinearInboundUploads({
content: messageFromLinearAgentSessionEvent(event),
credentials: input.config.credentials,
fetch: input.config.api?.fetch,
diff --git a/packages/eve/src/public/channels/slack/attachments.ts b/packages/eve/src/public/channels/slack/attachments.ts
index ef25eb5a0..47e310094 100644
--- a/packages/eve/src/public/channels/slack/attachments.ts
+++ b/packages/eve/src/public/channels/slack/attachments.ts
@@ -2,6 +2,7 @@ import type { FilePart, TextPart, UserContent } from "ai";
import type { FetchFileContext, FetchFileResult } from "#channel/adapter.js";
import { EveAttachmentError } from "#internal/attachments/errors.js";
+import { readLimitedBytes } from "#internal/attachments/limited-read.js";
import { createLogger } from "#internal/logging.js";
import {
resolveSlackBotToken,
@@ -19,7 +20,7 @@ import {
formatUploadPolicyViolation,
isUploadsDisabled,
} from "#public/channels/upload-policy.js";
-import type { UploadPolicy } from "#public/channels/upload-policy.js";
+import { DEFAULT_UPLOAD_POLICY, type UploadPolicy } from "#public/channels/upload-policy.js";
const log = createLogger("slack.attachments");
@@ -175,6 +176,8 @@ export function buildSlackTurnMessage(
export function createSlackFetchFile(input: {
readonly api?: SlackTransportOptions;
readonly botToken?: SlackBotToken;
+ /** Downloads stop at this many bytes. Defaults to the framework's 25 MB. */
+ readonly maxBytes?: number;
}): (url: string, context?: FetchFileContext) => Promise<FetchFileResult | null> {
const api = resolveSlackTransportOptions(input.api);
return async (url, context) => {
@@ -206,7 +209,11 @@ export function createSlackFetchFile(input: {
});
}
return {
- bytes: Buffer.from(await response.arrayBuffer()),
+ bytes: await readLimitedBytes(
+ response,
+ input.maxBytes ?? DEFAULT_UPLOAD_POLICY.maxBytes,
+ "slack",
+ ),
mediaType,
};
};
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index bcb14ca6d..348b3aa0b 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -15,6 +15,7 @@ import type { CardElement } from "#compiled/chat/index.js";
import type { SessionContext } from "#public/definitions/callback-context.js";
import type { ChannelContinuationOps } from "#public/definitions/channel.js";
+import { maxBytesOf } from "#internal/attachments/limited-read.js";
import { createLogger, logError } from "#internal/logging.js";
import { attachInputText } from "#internal/input-text.js";
import type { UnstampedMessageStreamEvent } from "#protocol/message.js";
@@ -826,6 +827,7 @@ export function slackChannel(config: SlackChannelConfig = {}): SlackChannel {
const slackFetchFile = createSlackFetchFile({
api,
botToken: config.credentials?.botToken,
+ maxBytes: maxBytesOf(uploadPolicy),
});
const onInputResponse = config.onInputResponse ?? defaultOnInputResponse;
const rendering = composeSlackRenderers(config.renderers ?? [], {
diff --git a/packages/eve/src/public/channels/telegram/attachments.test.ts b/packages/eve/src/public/channels/telegram/attachments.test.ts
index db0c137ef..0113902bb 100644
--- a/packages/eve/src/public/channels/telegram/attachments.test.ts
+++ b/packages/eve/src/public/channels/telegram/attachments.test.ts
@@ -57,7 +57,8 @@ describe("createTelegramFetchFile", () => {
}),
)
.mockResolvedValueOnce(
- new Response("PDF", { headers: { "content-type": "application/pdf" } }),
+ // Telegram serves every file as octet-stream; the declared type must win (#1217).
+ new Response("PDF", { headers: { "content-type": "application/octet-stream" } }),
);
const fetchFile = createTelegramFetchFile({
diff --git a/packages/eve/src/public/channels/telegram/attachments.ts b/packages/eve/src/public/channels/telegram/attachments.ts
index c6120b457..98b7ca864 100644
--- a/packages/eve/src/public/channels/telegram/attachments.ts
+++ b/packages/eve/src/public/channels/telegram/attachments.ts
@@ -2,6 +2,7 @@ import type { FilePart, TextPart, UserContent } from "ai";
import type { FetchFileResult } from "#channel/adapter.js";
import { EveAttachmentError } from "#internal/attachments/errors.js";
+import { maxBytesOf, readLimitedBytes } from "#internal/attachments/limited-read.js";
import { createLogger } from "#internal/logging.js";
import {
downloadTelegramFile,
@@ -94,9 +95,11 @@ export function createTelegramFetchFile(input: {
});
}
- const bytes = Buffer.from(await response.arrayBuffer());
+ const bytes = await readLimitedBytes(response, maxBytesOf(input.policy), "telegram");
+ // Telegram's file endpoint serves everything as `application/octet-stream`,
+ // so the type the message declared is the only real one (#1217).
const mediaType =
- response.headers.get("content-type") ?? ref.mediaType ?? "application/octet-stream";
+ ref.mediaType ?? response.headers.get("content-type") ?? "application/octet-stream";
const result: FetchFileResult = {
bytes,
filename: ref.filename,
diff --git a/packages/eve/src/public/channels/twilio/api.ts b/packages/eve/src/public/channels/twilio/api.ts
index e96aebdce..0e9719716 100644
--- a/packages/eve/src/public/channels/twilio/api.ts
+++ b/packages/eve/src/public/channels/twilio/api.ts
@@ -1,6 +1,7 @@
import {
callTwilioApi as callPrimitive,
encodeTwilioForm,
+ fetchTwilioMedia,
resolveTwilioCredential,
TwilioApiError,
type TwilioApiResponse,
@@ -8,8 +9,14 @@ import {
type TwilioFetch,
} from "#compiled/@chat-adapter/twilio/api.js";
+import type { FetchFileResult } from "#channel/adapter.js";
+import { EveAttachmentError } from "#internal/attachments/errors.js";
+import { limitedFetch } from "#internal/attachments/limited-read.js";
+import { DEFAULT_UPLOAD_POLICY } from "#public/channels/upload-policy.js";
import { resolveTwilioAuthToken, type TwilioAuthToken } from "#public/channels/twilio/verify.js";
+const DEFAULT_TWILIO_API_BASE_URL = "https://api.twilio.com";
+
/**
* Builds the Twilio channel-local continuation token (`<from>:<to>`).
* Eve namespaces this with the channel name before handing it to the runtime.
@@ -129,6 +136,40 @@ export async function updateTwilioCall(input: TwilioUpdateCallInput): Promise<Tw
);
}
+/**
+ * Creates the channel's `fetchFile` for MMS media. Downloads URLs on the Twilio
+ * API origin with the account's Basic auth, and returns `null` for any other
+ * URL so it never receives the credentials.
+ */
+export function createTwilioFetchFile(
+ input: TwilioApiOptions,
+): (url: string) => Promise<FetchFileResult | null> {
+ const origin = new URL(input.apiBaseUrl ?? DEFAULT_TWILIO_API_BASE_URL).origin;
+ return async (url) => {
+ if (URL.parse(url)?.origin !== origin) return null;
+ try {
+ const bytes = await fetchTwilioMedia({
+ apiBaseUrl: input.apiBaseUrl,
+ credentials: await resolveCredentials(input.credentials),
+ fetch: limitedFetch(input.fetch ?? fetch, DEFAULT_UPLOAD_POLICY.maxBytes, "twilio"),
+ url,
+ });
+ return { bytes: Buffer.from(bytes) };
+ } catch (cause) {
+ if (cause instanceof EveAttachmentError) throw cause;
+ throw new EveAttachmentError({
+ adapterKind: "twilio",
+ cause,
+ kind: "resolver-threw",
+ message:
+ cause instanceof TwilioApiError && cause.status !== 0
+ ? `Twilio media fetch returned HTTP ${cause.status}.`
+ : "Twilio media fetch failed.",
+ });
+ }
+ };
+}
+
async function resolveCredentials(
credentials: TwilioCredentials | undefined,
): Promise<{ accountSid: string; authToken: string }> {
diff --git a/packages/eve/src/public/channels/twilio/inbound.ts b/packages/eve/src/public/channels/twilio/inbound.ts
index 19c5aee8b..9173fe541 100644
--- a/packages/eve/src/public/channels/twilio/inbound.ts
+++ b/packages/eve/src/public/channels/twilio/inbound.ts
@@ -1,3 +1,5 @@
+import type { UserContent } from "ai";
+
import {
parseTwilioWebhookBody,
type TwilioMediaPayload,
@@ -71,6 +73,24 @@ export function parseTwilioTextMessage(params: URLSearchParams): TwilioTextMessa
};
}
+/**
+ * The model-visible content of an inbound message: its body, plus one file part
+ * per MMS media URL, which the channel's `fetchFile` downloads with the
+ * account's credentials.
+ */
+export function twilioMessageContent(message: TwilioTextMessage): string | UserContent {
+ const media = message.media ?? [];
+ if (media.length === 0) return message.body;
+ const files = media.map((item) => ({
+ data: new URL(item.url),
+ mediaType: item.contentType ?? "application/octet-stream",
+ type: "file" as const,
+ }));
+ return message.body.trim().length > 0
+ ? [{ text: message.body, type: "text" as const }, ...files]
+ : files;
+}
+
/** Parses Twilio's incoming-call webhook fields into Eve's voice payload. */
export function parseTwilioVoiceCall(params: URLSearchParams): TwilioVoiceCall | null {
const call = parsePrimitiveVoiceCall(params);
diff --git a/packages/eve/src/public/channels/twilio/twilioChannel.ts b/packages/eve/src/public/channels/twilio/twilioChannel.ts
index 54bf57117..fd50b4056 100644
--- a/packages/eve/src/public/channels/twilio/twilioChannel.ts
+++ b/packages/eve/src/public/channels/twilio/twilioChannel.ts
@@ -9,6 +9,7 @@ import { createLogger } from "#internal/logging.js";
import type { UnstampedMessageStreamEvent } from "#protocol/message.js";
import {
callTwilioApi,
+ createTwilioFetchFile,
sendTwilioMessage,
twilioContinuationToken,
updateTwilioCall,
@@ -27,6 +28,7 @@ import {
parseTwilioTextMessage,
parseTwilioVoiceCall,
parseTwilioVoiceTranscription,
+ twilioMessageContent,
type TwilioTextMessage,
type TwilioVoiceCall,
type TwilioVoiceTranscription,
@@ -316,6 +318,7 @@ export function twilioChannel(config: TwilioChannelConfig): TwilioChannel {
};
},
audience: () => "private",
+ fetchFile: createTwilioFetchFile({ ...config.api, credentials: config.credentials }),
context(state, session) {
return rebuildTwilioContext(state, session, config);
@@ -563,17 +566,19 @@ async function dispatchText(input: {
});
try {
- await input.from(twilioContinuationToken(message.from, message.to)).send(message.body, {
- auth: result.auth,
- context: [contextBlock],
- state: {
- from: message.from,
- lastCallSid: null,
- lastMessageSid: message.messageSid ?? null,
- to: message.to ?? null,
- },
- title: result.title,
- });
+ await input
+ .from(twilioContinuationToken(message.from, message.to))
+ .send(twilioMessageContent(message), {
+ auth: result.auth,
+ context: [contextBlock],
+ state: {
+ from: message.from,
+ lastCallSid: null,
+ lastMessageSid: message.messageSid ?? null,
+ to: message.to ?? null,
+ },
+ title: result.title,
+ });
} catch (error) {
log.error("text delivery failed", { error });
}