The Open Framework for Building Agents
fix(eve): Gracefully handle empty initial user message (e.g. plain @) (#980)
Ben Pankowde917a6e52d4
diff --git a/.changeset/teams-blank-mention.md b/.changeset/teams-blank-mention.mdnew file mode 100644index 000000000..49726ff25--- /dev/null+++ b/.changeset/teams-blank-mention.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Fix a crash when an agent is triggered by a bare mention with no text (e.g. sending just `@bot` in a Microsoft Teams channel). The agent now responds normally instead of failing.diff --git a/packages/eve/src/harness/messages.test.ts b/packages/eve/src/harness/messages.test.tsindex d3ff967a1..579fcb000 100644--- a/packages/eve/src/harness/messages.test.ts+++ b/packages/eve/src/harness/messages.test.ts@@ -1,6 +1,10 @@import type { FilePart, ModelMessage, UserContent } from "ai";import { describe, expect, it } from "vitest";-import { coalesceTurnInputs, resolveAssistantStepText } from "#harness/messages.js";+import {+ coalesceTurnInputs,+ normalizeUserContent,+ resolveAssistantStepText,+} from "#harness/messages.js";import type { StepInput } from "#harness/types.js";function textFilePart(overrides: {@@ -113,13 +117,49 @@ describe("coalesceTurnInputs", () => {expect(merged[3]).toBe(second);});- it("drops an empty string when the other side is a UserContent array", () => {+ it("drops blank text when coalescing structured content", () => {const attachment = textFilePart({ filename: "notes.txt", payload: "hi" });- const result = coalesceTurnInputs({ message: "" }, { message: [attachment] });+ const result = coalesceTurnInputs(+ { message: [{ text: " \n\t", type: "text" }, attachment] },+ { message: " " },+ );- const merged = result.message as UserContent;- expect(merged).toHaveLength(1);- expect(merged[0]).toBe(attachment);+ expect(result.message).toEqual([attachment]);+ });+});++describe("normalizeUserContent", () => {+ it.each([+ ["empty string", ""],+ ["whitespace-only string", " \n\t "],+ ["empty content array", []],+ ["structured empty text", [{ text: "", type: "text" }]],+ ["structured whitespace-only text", [{ text: " \n\t", type: "text" }]],+ ] satisfies ReadonlyArray<readonly [string, string | UserContent]>)(+ "returns undefined for %s",+ (_name, content) => {+ expect(normalizeUserContent(content)).toBeUndefined();+ },+ );++ it("preserves visible text", () => {+ expect(normalizeUserContent("hello")).toBe("hello");+ });++ it("keeps file parts while removing blank text parts", () => {+ const attachment = textFilePart({ filename: "notes.txt", payload: "contents" });++ expect(+ normalizeUserContent([{ text: "", type: "text" }, attachment, { text: " ", type: "text" }]),+ ).toEqual([attachment]);+ });++ it("removes a blank string coalesced with structured content", () => {+ const attachment = textFilePart({ filename: "notes.txt", payload: "contents" });++ expect(coalesceTurnInputs({ message: [attachment] }, { message: " " }).message).toEqual([+ attachment,+ ]);});});diff --git a/packages/eve/src/harness/messages.ts b/packages/eve/src/harness/messages.tsindex 6928a594c..a70a6c9ef 100644--- a/packages/eve/src/harness/messages.ts+++ b/packages/eve/src/harness/messages.ts@@ -52,6 +52,30 @@ export function coalesceTurnInputs(a: StepInput, b: StepInput): StepInput {return result;}+/**+ * Removes text parts with no model-visible content from a user message.+ *+ * Returns `undefined` when no parts remain, allowing callers to omit the user+ * turn entirely rather than create an empty model prompt block.+ */+export function normalizeUserContent(+ content: string | UserContent | undefined,+): string | UserContent | undefined {+ if (content === undefined) {+ return undefined;+ }++ if (typeof content === "string") {+ return content.trim().length > 0 ? content : undefined;+ }++ const parts = content.filter((part) => part.type !== "text" || part.text.trim().length > 0);+ if (parts.length === 0) {+ return undefined;+ }+ return parts.length === content.length ? content : parts;+}+/*** Extracts the final visible assistant text from model response messages.*@@ -134,26 +158,24 @@ function coalesceContext(input: {}/**- * Merges two optional turn messages into one.- *- * When both sides are strings, concatenates with a blank line. When- * either side is a structured {@link UserContent} array, promotes both- * to arrays and concatenates their parts so attachments carried on the- * deferred or newer input are preserved end-to-end.+ * Merges two optional turn messages into one after removing blank content.*/function coalesceMessage(input: {readonly a?: string | UserContent;readonly b?: string | UserContent;}): string | UserContent | undefined {- if (input.a === undefined) {- return input.b;+ const a = normalizeUserContent(input.a);+ const b = normalizeUserContent(input.b);++ if (a === undefined) {+ return b;}- if (input.b === undefined) {- return input.a;+ if (b === undefined) {+ return a;}- return appendUserContent({ appended: input.b, existing: input.a });+ return appendUserContent({ appended: b, existing: a });}/**diff --git a/packages/eve/src/harness/stale-input-responses.ts b/packages/eve/src/harness/stale-input-responses.tsindex 985a42009..0f50808f6 100644--- a/packages/eve/src/harness/stale-input-responses.ts+++ b/packages/eve/src/harness/stale-input-responses.ts@@ -2,7 +2,7 @@ import type { ModelMessage, UserContent } from "ai";import { extractHistoricalInputRequests } from "#harness/input-extraction.js";import { isApprovalRequest } from "#harness/input-requests.js";-import { appendUserContent } from "#harness/messages.js";+import { appendUserContent, normalizeUserContent } from "#harness/messages.js";import type { StepInput } from "#harness/types.js";import type { InputRequest, InputResponse } from "#runtime/input/types.js";@@ -158,9 +158,10 @@ function appendOptionalUserContent(existing: string | UserContent | undefined,appended: string,): string | UserContent {- if (existing === undefined) {+ const normalizedExisting = normalizeUserContent(existing);+ if (normalizedExisting === undefined) {return appended;}- return appendUserContent({ appended, existing });+ return appendUserContent({ appended, existing: normalizedExisting });}diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.tsindex b743e0c61..b7b7d2ce9 100644--- a/packages/eve/src/harness/tool-loop.test.ts+++ b/packages/eve/src/harness/tool-loop.test.ts@@ -590,6 +590,36 @@ describe("createToolLoopHarness", () => {]);});+ it("omits user messages with no model-visible content", async () => {+ setupMockAgent({+ finishReason: "stop",+ response: { messages: [{ content: "Hello!", role: "assistant" }] },+ text: "Hello!",+ toolCalls: [],+ toolResults: [],+ });++ const runStep = createToolLoopHarness(createTestConfig("conversation"));+ const blankMessages: Array<string | UserContent> = [+ "",+ " \n\t",+ [{ text: "", type: "text" }],+ [{ text: " \n\t", type: "text" }],+ ];++ for (const message of blankMessages) {+ const result = await runStep(createTestSession(), {+ context: ["channel context"],+ message,+ });++ expect(result.session.history).toEqual([+ { content: "channel context", role: "user" },+ { content: "Hello!", role: "assistant" },+ ]);+ }+ });+it("parks without delivery when a terminal response contains the empty-delivery sentinel", async () => {setupMockAgent({finishReason: "stop",diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex 1d3381ca4..0f97d67a0 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -112,7 +112,7 @@ import {} from "#harness/input-requests.js";import { convertStaleResponsesToUserMessage } from "#harness/stale-input-responses.js";import { getInstrumentationConfig } from "#harness/instrumentation-config.js";-import { resolveAssistantStepText } from "#harness/messages.js";+import { normalizeUserContent, resolveAssistantStepText } from "#harness/messages.js";import { normalizeProviderToolHistory } from "#harness/provider-tool-history.js";import {type AuthorizationSignal,@@ -640,17 +640,14 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {}}- if (- effectiveStepInput?.message !== undefined &&- !pending.deferredMessage &&- !pending.consumedMessage- ) {+ const userContent = normalizeUserContent(effectiveStepInput?.message);+ if (userContent !== undefined && !pending.deferredMessage && !pending.consumedMessage) {// Staging writes FilePart bytes into the sandbox and replaces// each part's `data` with a compact `eve-sandbox:` URL. The// `messages` array — and everything that flows into// `session.history` from it — therefore never carries raw// attachment bytes across step boundaries.- const content = await stageAttachmentsToSandbox(effectiveStepInput.message);+ const content = await stageAttachmentsToSandbox(userContent);messages.push({ content, role: "user" });}