Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

[4/4] feat(eve): deliver Discord and Teams attachments to the agent (#4382)

Ben Pankowcebbc9b611be
diff --git a/.changeset/discord-teams-attachments.md b/.changeset/discord-teams-attachments.md
new file mode 100644
index 000000000..bd6cb0fa5
--- /dev/null
+++ b/.changeset/discord-teams-attachments.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Discord and Teams now pass files people send to the agent. Discord reads files from slash-command attachment options, and guided setup registers an optional `file` option. Teams accepts files by default from Bot Connector and SharePoint hosts; set `files: { enabled: false }` to opt out, and `allowedHosts` now adds hosts instead of replacing them. Downloads on both stop at the upload limit.
diff --git a/docs/channels/discord.mdx b/docs/channels/discord.mdx
index f81b63ed4..5bf26e6d0 100644
--- a/docs/channels/discord.mdx
+++ b/docs/channels/discord.mdx
@@ -19,7 +19,7 @@ The setup guides you through creating a Discord application and bot, then:
- signs you in to Vercel and creates or links a project when needed;
- validates the bot token;
- creates a Vercel Connect client and attaches `/eve/v1/discord` as its trigger destination;
-- registers an application command with a required `message` option;
+- registers an application command with a required `message` option and an optional `file` attachment option;
- configures the Discord application's Interactions Endpoint URL;
- scaffolds the channel with Connect-managed credentials; and
- prints the bot installation URL.
@@ -41,13 +41,14 @@ The route is `POST /eve/v1/discord` by default. Connect resolves the application
### Configure Discord manually
-If you do not use the guided Connect setup, register a command with Discord's API or the Developer Portal. A string option named `message` lines up with eve's default prompt extraction:
+If you do not use the guided Connect setup, register a command with Discord's API or the Developer Portal. A string option named `message` lines up with eve's default prompt extraction, and an attachment option lets people send a file with the command:
```bash
curl -X PUT "https://discord.com/api/v10/applications/$DISCORD_APPLICATION_ID/commands" \
-H "Authorization: Bot $DISCORD_BOT_TOKEN" -H "Content-Type: application/json" \
-d '[{"name":"ask","description":"Ask the eve agent","type":1,
- "options":[{"name":"message","description":"What should the agent do?","type":3,"required":true}]}]'
+ "options":[{"name":"message","description":"What should the agent do?","type":3,"required":true},
+ {"name":"file","description":"A file for the agent to read","type":11,"required":false}]}]'
```
Then configure the public `https://…/eve/v1/discord` route as the application's Interactions Endpoint URL. Pass `credentials: { applicationId, botToken, publicKey }` to `discordChannel`, or set the corresponding `DISCORD_APPLICATION_ID`, `DISCORD_BOT_TOKEN`, and `DISCORD_PUBLIC_KEY` environment variables.
@@ -99,7 +100,7 @@ Start a session without an inbound interaction through `to(discord, target).send
### Attachments
-Inbound file attachments are not supported on this channel today.
+Files attached through a command's attachment options (type `11`) reach the agent as file parts. eve downloads each one from Discord's signed CDN URL (`cdn.discordapp.com` or `media.discordapp.net`) without credentials. A file over 25 MB, or one whose download fails, reaches the agent as a short note instead. Each command starts its own session, so a file is available in the conversation that command starts.
## What to read next
diff --git a/docs/channels/teams.mdx b/docs/channels/teams.mdx
index 748a71d25..c7b4fc61b 100644
--- a/docs/channels/teams.mdx
+++ b/docs/channels/teams.mdx
@@ -95,19 +95,16 @@ Proactive sessions need an existing conversation reference, because the Bot Fram
### Attachments
-Inbound files are off by default. Opt in to allow personal-scope downloads and public media URLs:
+Inbound files reach the agent as file parts. eve downloads images pasted into a message from Microsoft Bot Connector hosts with your bot credentials, and files uploaded in a personal chat from their pre-authenticated SharePoint download URL (`*.sharepoint.com` or `*.sharepoint.us`). A failed download, or one over the upload limit (25 MB unless `files.uploadPolicy` sets `maxBytes`), reaches the agent as a short note. Personal-chat uploads need `supportsFiles: true` on the bot in your Teams app manifest.
+
+Use `allowedHosts` to download from other hosts, unauthenticated, or `enabled: false` to drop attachments:
```ts
export default teamsChannel({
- files: {
- enabled: true,
- allowedHosts: ["smba.trafficmanager.net", "contoso.sharepoint.com"],
- },
+ files: { allowedHosts: ["files.contoso.com"] },
});
```
-eve authenticates downloads from Microsoft Bot Connector hosts with your bot credentials. Downloads from other allowlisted hosts remain unauthenticated.
-
## What to read next
- [Channels overview](./overview): the channel contract and every built-in channel
diff --git a/packages/eve/src/internal/testing/channel-conformance/MATRIX.md b/packages/eve/src/internal/testing/channel-conformance/MATRIX.md
index bab4b7390..2a911240c 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>[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> | ✅ | ✅ | ✅ |
+| an image a person sends reaches the agent with its bytes and type | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</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>[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>[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>[21](#note-21)</sup> | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | ✅ | ✅ |
## Notes
@@ -112,6 +112,4 @@ 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>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
+21. <a id="note-21"></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/conformance.ts b/packages/eve/src/internal/testing/channel-conformance/conformance.ts
index 0fab1ab70..4808cf5c7 100644
--- a/packages/eve/src/internal/testing/channel-conformance/conformance.ts
+++ b/packages/eve/src/internal/testing/channel-conformance/conformance.ts
@@ -115,22 +115,6 @@ interface ConformanceChannel {
readonly unsupported?: Partial<Record<ContractRuleName, string>>;
}
-const IMAGE_REACHES_AGENT = "an image a person sends reaches the agent with its bytes and type";
-const PDF_REACHES_AGENT = "a PDF a person sends reaches the agent with its bytes and type";
-const UNDOWNLOADABLE_FILE =
- "a file that can't be downloaded reaches the agent as a note, not a link, and the next message still works";
-const EARLIER_FILE = "a file sent earlier in the conversation is still there on a later message";
-
-/** The agent sees none of the files a person sent. */
-function filesNotSeen(
- reason: string,
- ...rules: ContractRuleName[]
-): Partial<Record<ContractRuleName, BrokenCell>> {
- return Object.fromEntries(
- rules.map((rule) => [rule, { reason, symptom: /the agent saw \[\]/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. */
@@ -189,12 +173,6 @@ const DISCORD_BROKEN = {
"a sign-in without a link shows its instructions",
"completing a sign-in tells the person it succeeded",
),
- ...filesNotSeen(
- "eve reads only the slash command's message option, not its attachment options",
- IMAGE_REACHES_AGENT,
- PDF_REACHES_AGENT,
- UNDOWNLOADABLE_FILE,
- ),
};
const DISCORD_UNSUPPORTED = {
@@ -215,13 +193,6 @@ const SLACK_BROKEN = {
const TEAMS_BROKEN = {
...unnamedAnsweredPrompts(UNNAMED_RESPONDER, ["approvalText", "questionPress", "questionText"]),
- ...filesNotSeen(
- "Teams drops files unless files.enabled is set and their host is allowlisted",
- IMAGE_REACHES_AGENT,
- PDF_REACHES_AGENT,
- UNDOWNLOADABLE_FILE,
- EARLIER_FILE,
- ),
} satisfies Partial<Record<ContractRuleName, BrokenCell>>;
/** The sign-in prompt, link included, goes to the whole thread. */
diff --git a/packages/eve/src/public/channels/discord/attachments.test.ts b/packages/eve/src/public/channels/discord/attachments.test.ts
new file mode 100644
index 000000000..d2516256e
--- /dev/null
+++ b/packages/eve/src/public/channels/discord/attachments.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest";
+
+import { createDiscordFetchFile, discordCommandContent } from "./attachments.js";
+
+const OVER_LIMIT = 25 * 1024 * 1024 + 1;
+const CDN_URL = "https://cdn.discordapp.com/attachments/C1/A1/report.pdf?ex=signed";
+
+describe("discord attachments", () => {
+ it("notes a file Discord reports as over the upload limit without downloading it", () => {
+ expect(
+ discordCommandContent("", [
+ {
+ contentType: "application/pdf",
+ filename: "report.pdf",
+ id: "A1",
+ size: OVER_LIMIT,
+ url: CDN_URL,
+ },
+ ]),
+ ).toEqual([
+ {
+ text: "Attachment report.pdf was not retrieved: it is over the upload limit.",
+ type: "text",
+ },
+ ]);
+ });
+
+ it("stops a download that turns out larger than the upload limit", async () => {
+ const fetchFile = createDiscordFetchFile(async () => new Response(new Uint8Array(OVER_LIMIT)));
+ await expect(fetchFile(CDN_URL)).rejects.toThrow("it is over the 25 MB upload limit.");
+ });
+});
diff --git a/packages/eve/src/public/channels/discord/attachments.ts b/packages/eve/src/public/channels/discord/attachments.ts
new file mode 100644
index 000000000..2e44da46e
--- /dev/null
+++ b/packages/eve/src/public/channels/discord/attachments.ts
@@ -0,0 +1,67 @@
+import type { FilePart, UserContent } from "ai";
+
+import type { FetchFileResult } from "#channel/adapter.js";
+import { EveAttachmentError } from "#internal/attachments/errors.js";
+import { readLimitedBytes } from "#internal/attachments/limited-read.js";
+import type { DiscordFetch } from "#public/channels/discord/api.js";
+import type { DiscordAttachment } from "#public/channels/discord/inbound.js";
+import { DEFAULT_UPLOAD_POLICY } from "#public/channels/upload-policy.js";
+
+// Discord serves attachments from signed CDN URLs that need no credentials.
+const DISCORD_CDN_HOSTS = new Set(["cdn.discordapp.com", "media.discordapp.net"]);
+
+/**
+ * The model-visible content of a command: its prompt, plus one file part per
+ * attachment, which the channel's `fetchFile` downloads. Files over the default
+ * upload limit become a short note instead.
+ */
+export function discordCommandContent(
+ text: string,
+ attachments: readonly DiscordAttachment[],
+): string | UserContent {
+ if (attachments.length === 0) return text;
+ const parts: Exclude<UserContent, string> = text.trim() ? [{ text, type: "text" }] : [];
+ for (const attachment of attachments) {
+ if (attachment.size !== undefined && attachment.size > DEFAULT_UPLOAD_POLICY.maxBytes) {
+ parts.push({
+ text: `Attachment ${attachment.filename} was not retrieved: it is over the upload limit.`,
+ type: "text",
+ });
+ continue;
+ }
+ const file: FilePart = {
+ data: new URL(attachment.url),
+ filename: attachment.filename,
+ mediaType: attachment.contentType ?? "application/octet-stream",
+ type: "file",
+ };
+ parts.push(file);
+ }
+ return parts;
+}
+
+/**
+ * Creates the channel's `fetchFile` for Discord CDN URLs. Any other URL
+ * resolves to `null`, so it's never fetched here.
+ */
+export function createDiscordFetchFile(
+ fetchImpl: DiscordFetch = fetch,
+): (url: string) => Promise<FetchFileResult | null> {
+ return async (url) => {
+ const parsed = URL.parse(url);
+ if (parsed?.protocol !== "https:" || !DISCORD_CDN_HOSTS.has(parsed.hostname)) return null;
+ const response = await fetchImpl(url, { redirect: "error" });
+ if (!response.ok) {
+ throw new EveAttachmentError({
+ adapterKind: "discord",
+ kind: "resolver-threw",
+ message: `Discord file fetch returned HTTP ${response.status}.`,
+ });
+ }
+ return {
+ // Discord's reported size can be wrong, so the download itself stops at the limit too.
+ bytes: await readLimitedBytes(response, DEFAULT_UPLOAD_POLICY.maxBytes, "discord"),
+ mediaType: response.headers.get("content-type") ?? undefined,
+ };
+ };
+}
diff --git a/packages/eve/src/public/channels/discord/discordChannel.test.ts b/packages/eve/src/public/channels/discord/discordChannel.test.ts
index 4aab923b6..3fe21b780 100644
--- a/packages/eve/src/public/channels/discord/discordChannel.test.ts
+++ b/packages/eve/src/public/channels/discord/discordChannel.test.ts
@@ -583,6 +583,7 @@ describe("defaultDiscordAuth", () => {
it("derives guild-scoped user auth", () => {
const auth = defaultDiscordAuth({
applicationId: "APP1",
+ attachments: [],
channelId: "C01",
commandName: "ask",
guildId: "G01",
diff --git a/packages/eve/src/public/channels/discord/discordChannel.ts b/packages/eve/src/public/channels/discord/discordChannel.ts
index 746e85dd5..ba0c421d4 100644
--- a/packages/eve/src/public/channels/discord/discordChannel.ts
+++ b/packages/eve/src/public/channels/discord/discordChannel.ts
@@ -50,6 +50,10 @@ import { verifyDiscordInbound } from "#public/channels/discord/verifyInbound.js"
import { readNonEmptyString } from "#shared/guards.js";
import { parseJsonObject, type JsonObject } from "#shared/json.js";
import { defineChannel, POST, type Channel } from "#public/definitions/channel.js";
+import {
+ createDiscordFetchFile,
+ discordCommandContent,
+} from "#public/channels/discord/attachments.js";
import type { InputOption, ValidatedInputResponse } from "#shared/input.js";
import type { ChannelAudience } from "#shared/channel-audience.js";
import { discordAudience, discordInstrumentationMetadata } from "./audience.js";
@@ -239,6 +243,7 @@ export function discordChannel(config: DiscordChannelConfig = {}): DiscordChanne
state: initialDiscordState(),
metadata: discordInstrumentationMetadata,
audience: ({ state }) => state.audience ?? "unknown",
+ fetchFile: createDiscordFetchFile(config.api?.fetch),
context(state, session) {
return rebuildDiscordContext(state, session, config);
@@ -612,7 +617,10 @@ async function dispatchCommand(input: {
readonly from: ChannelFrom<DiscordChannelState>;
readonly state: DiscordChannelState;
}): Promise<void> {
- const turnMessage = commandInteractionMessage(input.interaction);
+ const turnMessage = discordCommandContent(
+ commandInteractionMessage(input.interaction),
+ input.interaction.attachments,
+ );
const contextBlock = formatDiscordContextBlock({
applicationId: input.interaction.applicationId,
channelId: input.interaction.channelId,
diff --git a/packages/eve/src/public/channels/discord/inbound.test.ts b/packages/eve/src/public/channels/discord/inbound.test.ts
index 630f37a7c..9b67eb5c9 100644
--- a/packages/eve/src/public/channels/discord/inbound.test.ts
+++ b/packages/eve/src/public/channels/discord/inbound.test.ts
@@ -34,7 +34,7 @@ describe("parseDiscordInteraction", () => {
expect(interaction.commandName).toBe("ask");
expect(interaction.channelType).toBe(1);
expect(interaction.options).toEqual([
- { name: "message", options: [], value: "hello from discord" },
+ { name: "message", options: [], type: 3, value: "hello from discord" },
]);
expect(interaction.user).toMatchObject({
globalName: "Ada Lovelace",
@@ -44,6 +44,45 @@ describe("parseDiscordInteraction", () => {
expect(interaction.member?.nick).toBe("Ada");
});
+ it("resolves files from attachment options and leaves their ids out of the prompt", () => {
+ const interaction = parseDiscordInteraction({
+ application_id: "APP1",
+ channel_id: "C01",
+ data: {
+ name: "ask",
+ options: [{ name: "file", type: 11, value: "A1" }],
+ resolved: {
+ attachments: {
+ A1: {
+ content_type: "image/png",
+ filename: "diagram.png",
+ id: "A1",
+ size: 42,
+ url: "https://cdn.discordapp.com/attachments/C01/A1/diagram.png?ex=1",
+ },
+ A2: { filename: "unused.png", id: "A2", url: "https://cdn.discordapp.com/x" },
+ },
+ },
+ },
+ id: "I01",
+ token: "tok",
+ type: 2,
+ user: { id: "U01", username: "ada" },
+ });
+ if (interaction?.type !== 2) throw new Error("Expected command interaction.");
+
+ expect(interaction.attachments).toEqual([
+ {
+ contentType: "image/png",
+ filename: "diagram.png",
+ id: "A1",
+ size: 42,
+ url: "https://cdn.discordapp.com/attachments/C01/A1/diagram.png?ex=1",
+ },
+ ]);
+ expect(commandInteractionMessage(interaction)).toBe("/ask");
+ });
+
it("parses a message component interaction", () => {
const interaction = parseDiscordInteraction({
application_id: "APP1",
diff --git a/packages/eve/src/public/channels/discord/inbound.ts b/packages/eve/src/public/channels/discord/inbound.ts
index a005fd4e3..ce749a169 100644
--- a/packages/eve/src/public/channels/discord/inbound.ts
+++ b/packages/eve/src/public/channels/discord/inbound.ts
@@ -64,12 +64,29 @@ export interface DiscordInteractionBase {
*/
export interface DiscordCommandOption {
readonly name: string;
+ /** Discord's option type integer, e.g. 3 for a string or 11 for an attachment. */
+ readonly type?: number;
readonly value?: string | number | boolean;
readonly options: readonly DiscordCommandOption[];
}
+/** A file a person attached to a slash command through an attachment option. */
+export interface DiscordAttachment {
+ readonly id: string;
+ /** Signed Discord CDN URL the file downloads from. */
+ readonly url: string;
+ readonly filename: string;
+ readonly contentType?: string;
+ readonly size?: number;
+}
+
+/** Discord's option type for an attachment; its value is the attachment's id. */
+const ATTACHMENT_OPTION_TYPE = 11;
+
/** Parsed Discord slash/application command interaction. */
export interface DiscordCommandInteraction extends DiscordInteractionBase {
+ /** Files attached through the command's attachment options, in option order. */
+ readonly attachments: readonly DiscordAttachment[];
readonly commandId?: string;
readonly commandName: string;
readonly options: readonly DiscordCommandOption[];
@@ -169,11 +186,13 @@ function parseCommandInteraction(raw: Record<string, unknown>): DiscordCommandIn
const base = parseInteractionBase(raw);
const data = isObject(raw.data) ? raw.data : null;
if (!base || !data || !isNonEmptyString(data.name)) return null;
+ const options = parseOptions(data.options);
return {
...base,
+ attachments: resolveAttachments(options, data.resolved),
commandId: isNonEmptyString(data.id) ? data.id : undefined,
commandName: data.name,
- options: parseOptions(data.options),
+ options,
type: DISCORD_INTERACTION_TYPE.APPLICATION_COMMAND,
};
}
@@ -282,6 +301,7 @@ function parseOptions(value: unknown): DiscordCommandOption[] {
const option: DiscordCommandOption = {
name: item.name,
options: parseOptions(item.options),
+ type: typeof item.type === "number" ? item.type : undefined,
value: parseOptionValue(item.value),
};
options.push(option);
@@ -289,6 +309,40 @@ function parseOptions(value: unknown): DiscordCommandOption[] {
return options;
}
+/** The attachments the command's attachment options name, from the interaction's `resolved` data. */
+function resolveAttachments(
+ options: readonly DiscordCommandOption[],
+ resolved: unknown,
+): DiscordAttachment[] {
+ const byId =
+ isObject(resolved) && isObject(resolved.attachments) ? resolved.attachments : undefined;
+ if (byId === undefined) return [];
+ return attachmentOptionIds(options).flatMap((id) => {
+ const raw = byId[id];
+ if (!isObject(raw) || !isNonEmptyString(raw.url) || !isNonEmptyString(raw.filename)) {
+ return [];
+ }
+ return [
+ {
+ contentType: isNonEmptyString(raw.content_type) ? raw.content_type : undefined,
+ filename: raw.filename,
+ id,
+ size: typeof raw.size === "number" ? raw.size : undefined,
+ url: raw.url,
+ },
+ ];
+ });
+}
+
+function attachmentOptionIds(options: readonly DiscordCommandOption[]): string[] {
+ return options.flatMap((option) => [
+ ...(option.type === ATTACHMENT_OPTION_TYPE && typeof option.value === "string"
+ ? [option.value]
+ : []),
+ ...attachmentOptionIds(option.options),
+ ]);
+}
+
function parseOptionValue(value: unknown): string | number | boolean | undefined {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
return value;
@@ -331,6 +385,8 @@ function formatCommandOptions(options: readonly DiscordCommandOption[]): string
}
function formatOption(option: DiscordCommandOption): string {
+ // An attachment option's value is an id; the file reaches the model as its own part.
+ if (option.type === ATTACHMENT_OPTION_TYPE) return "";
if (option.value !== undefined) return `${option.name}:${String(option.value)}`;
const nested = formatCommandOptions(option.options);
return nested ? `${option.name} ${nested}` : option.name;
diff --git a/packages/eve/src/public/channels/teams/attachments.test.ts b/packages/eve/src/public/channels/teams/attachments.test.ts
index 1bf4dbbad..f627026e0 100644
--- a/packages/eve/src/public/channels/teams/attachments.test.ts
+++ b/packages/eve/src/public/channels/teams/attachments.test.ts
@@ -1,6 +1,10 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { createTeamsFetchFile, normalizeTeamsFilesPolicy } from "./attachments.js";
+import {
+ collectTeamsFileParts,
+ createTeamsFetchFile,
+ normalizeTeamsFilesPolicy,
+} from "./attachments.js";
describe("createTeamsFetchFile", () => {
afterEach(() => {
@@ -57,6 +61,18 @@ describe("createTeamsFetchFile", () => {
},
);
+ it("stops a SharePoint download at the configured upload limit", async () => {
+ const apiFetch = vi.fn(async () => new Response(new Uint8Array(2 * 1024 * 1024)));
+ const fetchFile = createTeamsFetchFile(
+ normalizeTeamsFilesPolicy({ uploadPolicy: { maxBytes: 1024 * 1024 } }),
+ { credentials: { tokenProvider: () => "connector-token" }, fetch: apiFetch },
+ );
+
+ await expect(
+ fetchFile("https://contoso.sharepoint.com/personal/alice/report.pdf?tempauth=signed"),
+ ).rejects.toThrow("it is over the 1 MB upload limit.");
+ });
+
it("rejects insecure URLs before fetching", async () => {
const apiFetch = vi.fn();
const fetchFile = createTeamsFetchFile(
@@ -180,3 +196,41 @@ describe("createTeamsFetchFile", () => {
expect(apiFetch).toHaveBeenCalledTimes(2);
});
});
+
+describe("collectTeamsFileParts", () => {
+ const upload = {
+ content: {
+ downloadUrl: "https://contoso.sharepoint.com/personal/ada/report.pdf?tempauth=signed",
+ fileType: "pdf",
+ },
+ contentType: "application/vnd.microsoft.teams.file.download.info",
+ name: "report.pdf",
+ };
+
+ it("accepts SharePoint uploads and Bot Connector images by default", () => {
+ const parts = collectTeamsFileParts(
+ [
+ upload,
+ {
+ contentType: "image/png",
+ contentUrl: "https://smba.trafficmanager.net/amer/v3/attachments/A1/views/original",
+ },
+ { contentType: "image/png", contentUrl: "https://images.example.com/chart.png" },
+ ],
+ normalizeTeamsFilesPolicy(undefined),
+ );
+
+ expect(
+ parts.map(({ data, mediaType }) => ({ host: (data as URL).hostname, mediaType })),
+ ).toEqual([
+ { host: "contoso.sharepoint.com", mediaType: "application/pdf" },
+ { host: "smba.trafficmanager.net", mediaType: "image/png" },
+ ]);
+ });
+
+ it("drops every attachment when files are disabled", () => {
+ expect(collectTeamsFileParts([upload], normalizeTeamsFilesPolicy({ enabled: false }))).toEqual(
+ [],
+ );
+ });
+});
diff --git a/packages/eve/src/public/channels/teams/attachments.ts b/packages/eve/src/public/channels/teams/attachments.ts
index 271cd2ee3..50c5f57f4 100644
--- a/packages/eve/src/public/channels/teams/attachments.ts
+++ b/packages/eve/src/public/channels/teams/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 {
resolveTeamsAccessToken,
@@ -26,17 +27,32 @@ const BOT_CONNECTOR_HOSTS = new Set([
"smba.infra.gov.teams.microsoft.us",
"smba.infra.dod.teams.microsoft.us",
]);
+// Files a person uploads to a bot download from a pre-authenticated SharePoint URL.
+const SHAREPOINT_HOST_SUFFIXES = [".sharepoint.com", ".sharepoint.us"];
+// `fileType` on a Teams file upload is the file's extension.
+const FILE_TYPE_MEDIA_TYPES: Readonly<Record<string, string>> = {
+ csv: "text/csv",
+ gif: "image/gif",
+ jpeg: "image/jpeg",
+ jpg: "image/jpeg",
+ json: "application/json",
+ md: "text/markdown",
+ pdf: "application/pdf",
+ png: "image/png",
+ txt: "text/plain",
+ webp: "image/webp",
+};
const MAX_FILE_REDIRECTS = 5;
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
/** File handling options for the native Teams channel. */
export interface TeamsFilesConfig {
/**
- * Hosts whose file URLs may be fetched, or `"*"` for any host. Defaults to
- * `[]` (no hosts), so attachments are dropped until you allowlist their host.
+ * Hosts whose file URLs may be fetched besides Microsoft's Bot Connector and
+ * SharePoint hosts, which are always allowed, or `"*"` for any host.
*/
readonly allowedHosts?: readonly string[] | "*";
- /** Enable inbound attachment ingestion. Off unless explicitly `true`. */
+ /** Set to `false` to drop inbound attachments. On by default. */
readonly enabled?: boolean;
/** Size and type limits applied to accepted attachments. */
readonly uploadPolicy?: UploadPolicyInput;
@@ -53,12 +69,12 @@ export interface TeamsFilesPolicy {
export function normalizeTeamsFilesPolicy(config: TeamsFilesConfig | undefined): TeamsFilesPolicy {
return {
allowedHosts: config?.allowedHosts ?? [],
- enabled: config?.enabled === true,
+ enabled: config?.enabled !== false,
uploadPolicy: mergeUploadPolicy(config?.uploadPolicy),
};
}
-/** Collects Teams attachment file parts when file support is explicitly enabled. */
+/** Collects Teams attachment file parts unless file support is disabled. */
export function collectTeamsFileParts(
attachments: readonly TeamsAttachment[],
policy: TeamsFilesPolicy,
@@ -111,7 +127,7 @@ export function createTeamsFetchFile(
);
}
return {
- bytes: Buffer.from(await response.arrayBuffer()),
+ bytes: await readLimitedBytes(response, maxBytesOf(policy.uploadPolicy), "teams"),
mediaType: response.headers.get("content-type") ?? undefined,
};
};
@@ -205,7 +221,9 @@ function inferMediaType(attachment: TeamsAttachment): string {
isObject(attachment.content) && typeof attachment.content.fileType === "string"
? attachment.content.fileType
: undefined;
- if (fileType === "txt") return "text/plain";
+ const mediaType =
+ fileType === undefined ? undefined : FILE_TYPE_MEDIA_TYPES[fileType.toLowerCase()];
+ return mediaType ?? "application/octet-stream";
}
return attachment.contentType || "application/octet-stream";
}
@@ -219,7 +237,15 @@ function isAllowedUrl(url: string, allowedHosts: readonly string[] | "*"): boole
}
if (parsed.protocol !== "https:") return false;
if (allowedHosts === "*") return true;
+ if (parsed.port === "" && isMicrosoftFileHost(parsed.hostname)) return true;
return allowedHosts.some((host) =>
host.includes(":") ? parsed.host === host : parsed.port === "" && parsed.hostname === host,
);
}
+
+function isMicrosoftFileHost(hostname: string): boolean {
+ return (
+ BOT_CONNECTOR_HOSTS.has(hostname) ||
+ SHAREPOINT_HOST_SUFFIXES.some((suffix) => hostname.endsWith(suffix))
+ );
+}
diff --git a/packages/eve/src/setup/integrations/discord/api.test.ts b/packages/eve/src/setup/integrations/discord/api.test.ts
index e33b087f5..bc64a9a13 100644
--- a/packages/eve/src/setup/integrations/discord/api.test.ts
+++ b/packages/eve/src/setup/integrations/discord/api.test.ts
@@ -44,6 +44,12 @@ describe("Discord setup API", () => {
type: 3,
required: true,
},
+ {
+ name: "file",
+ description: "A file for the agent to read",
+ type: 11,
+ required: false,
+ },
],
}),
}),
diff --git a/packages/eve/src/setup/integrations/discord/api.ts b/packages/eve/src/setup/integrations/discord/api.ts
index 344fad7db..0fcf95595 100644
--- a/packages/eve/src/setup/integrations/discord/api.ts
+++ b/packages/eve/src/setup/integrations/discord/api.ts
@@ -71,6 +71,12 @@ export async function registerDiscordCommand(
type: 3,
required: true,
},
+ {
+ name: "file",
+ description: "A file for the agent to read",
+ type: 11,
+ required: false,
+ },
],
}),
},