The Open Framework for Building Agents
fix(eve): support AbortSignal without throwIfAborted in eve/client (#4789)
Ben Pankow2e45423acff2
diff --git a/.changeset/calm-signal-client.md b/.changeset/calm-signal-client.mdnew file mode 100644index 000000000..dde93a2cc--- /dev/null+++ b/.changeset/calm-signal-client.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Allow `eve/client` to use an `AbortSignal` that does not implement `throwIfAborted`, including the signal provided by React Native.diff --git a/packages/eve/src/client/abort-signal.test.ts b/packages/eve/src/client/abort-signal.test.tsnew file mode 100644index 000000000..013423b97--- /dev/null+++ b/packages/eve/src/client/abort-signal.test.ts@@ -0,0 +1,35 @@+import { describe, expect, it, vi } from "vitest";++import { throwIfAborted } from "#client/abort-signal.js";++describe("throwIfAborted", () => {+ it("does nothing for an active signal without a native implementation", () => {+ expect(() => throwIfAborted({ aborted: false } as AbortSignal)).not.toThrow();+ });++ it("uses a native implementation when present", () => {+ const nativeThrowIfAborted = vi.fn();+ const signal = Object.assign(new AbortController().signal, {+ throwIfAborted: nativeThrowIfAborted,+ });++ throwIfAborted(signal);++ expect(nativeThrowIfAborted).toHaveBeenCalledOnce();+ });++ it("throws the abort reason when an aborted signal has no native implementation", () => {+ const controller = new AbortController();+ const reason = new Error("cancelled");+ controller.abort(reason);+ Object.defineProperty(controller.signal, "throwIfAborted", { value: undefined });++ expect(() => throwIfAborted(controller.signal)).toThrow(reason);+ });++ it("creates an AbortError when an aborted signal has neither implementation nor reason", () => {+ expect(() => throwIfAborted({ aborted: true } as AbortSignal)).toThrow(+ expect.objectContaining({ name: "AbortError" }),+ );+ });+});diff --git a/packages/eve/src/client/abort-signal.ts b/packages/eve/src/client/abort-signal.tsnew file mode 100644index 000000000..d4efc8691--- /dev/null+++ b/packages/eve/src/client/abort-signal.ts@@ -0,0 +1,17 @@+/** Throws the signal's abort reason without requiring AbortSignal#throwIfAborted support. */+export function throwIfAborted(signal: AbortSignal | undefined): void {+ if (signal === undefined) return;++ const nativeThrowIfAborted = (signal as { readonly throwIfAborted?: unknown }).throwIfAborted;+ if (typeof nativeThrowIfAborted === "function") {+ nativeThrowIfAborted.call(signal);+ return;+ }++ if (!signal.aborted) return;+ if (signal.reason !== undefined) throw signal.reason;++ const error = new Error("The operation was aborted.");+ error.name = "AbortError";+ throw error;+}diff --git a/packages/eve/src/client/eve-agent-store.ts b/packages/eve/src/client/eve-agent-store.tsindex 6823a1f48..fb69ff12d 100644--- a/packages/eve/src/client/eve-agent-store.ts+++ b/packages/eve/src/client/eve-agent-store.ts@@ -1,3 +1,4 @@+import { throwIfAborted } from "#client/abort-signal.js";import { Client } from "#client/client.js";import type {ActiveTurn,@@ -525,7 +526,7 @@ export class EveAgentStore<TData> {}),input.signal,);- input.signal?.throwIfAborted();+ throwIfAborted(input.signal);}const session = this.#session;let reader: SessionEventReader | undefined;diff --git a/packages/eve/src/client/ndjson.ts b/packages/eve/src/client/ndjson.tsindex 2bfb4084c..6397b956a 100644--- a/packages/eve/src/client/ndjson.ts+++ b/packages/eve/src/client/ndjson.ts@@ -1,3 +1,4 @@+import { throwIfAborted } from "#client/abort-signal.js";import { EVE_STREAM_LEASE_ENDED_CONTROL, type MessageStreamEvent } from "#protocol/message.js";import {normalizeMessageStreamEvent,@@ -63,9 +64,9 @@ export async function* readNdjsonStream(try {while (true) {- options.signal?.throwIfAborted();+ throwIfAborted(options.signal);const result = await readWithIdleTimeout(reader, options?.idleTimeoutMs);- options.signal?.throwIfAborted();+ throwIfAborted(options.signal);if (result.done) {reachedEof = true;diff --git a/packages/eve/src/client/open-stream.ts b/packages/eve/src/client/open-stream.tsindex c2d4fa375..134d28007 100644--- a/packages/eve/src/client/open-stream.ts+++ b/packages/eve/src/client/open-stream.ts@@ -1,3 +1,4 @@+import { throwIfAborted } from "#client/abort-signal.js";import type { MessageStreamEvent } from "#protocol/message.js";import {EVE_STREAM_CONTROL_VERSION,@@ -297,7 +298,7 @@ export async function openStreamBody(}for (let attempt = 0; attempt < openRetryPolicy.maxAttempts; attempt += 1) {- input.signal?.throwIfAborted();+ throwIfAborted(input.signal);const url = createClientUrl(input.host,input.path,@@ -305,7 +306,7 @@ export async function openStreamBody();const headers = await input.resolveHeaders();- input.signal?.throwIfAborted();+ throwIfAborted(input.signal);const connectionController = new AbortController();const signal = input.signal? AbortSignal.any([input.signal, connectionController.signal])diff --git a/packages/eve/src/client/session-turn-dispatch.ts b/packages/eve/src/client/session-turn-dispatch.tsindex 09f4d2528..cc3b1692e 100644--- a/packages/eve/src/client/session-turn-dispatch.ts+++ b/packages/eve/src/client/session-turn-dispatch.ts@@ -1,3 +1,4 @@+import { throwIfAborted } from "#client/abort-signal.js";import type { Client } from "#client/client.js";import type { MessageResponse } from "#client/message-response.js";import type { ClientSession } from "#client/session.js";@@ -17,7 +18,7 @@ export async function dispatchSessionTurn<TOutput>(input: {if (turn.message === undefined)throw new Error("Cannot answer an input request before the session starts.");const created = await client.sessions.create({ ...turn, message: turn.message });- turn.signal?.throwIfAborted();+ throwIfAborted(turn.signal);return { ...created, created: true };}diff --git a/packages/eve/src/client/session.test.ts b/packages/eve/src/client/session.test.tsindex 30eb39065..c4aae36e3 100644--- a/packages/eve/src/client/session.test.ts+++ b/packages/eve/src/client/session.test.ts@@ -273,6 +273,23 @@ describe("ClientSession", () => {expect(session.state).toBe(initialState);});+ it("snapshots with an AbortSignal that has no throwIfAborted", async () => {+ vi.spyOn(globalThis, "fetch").mockResolvedValue(createBoundedStreamResponse([]));+ const session = createSession();+ const descriptor = Object.getOwnPropertyDescriptor(AbortSignal.prototype, "throwIfAborted");+ expect(descriptor).toBeDefined();+ delete (AbortSignal.prototype as { throwIfAborted?: unknown }).throwIfAborted;++ try {+ await expect(session.snapshot({ signal: new AbortController().signal })).resolves.toEqual({+ events: [],+ session: { sessionId: "session_1", streamIndex: 0 },+ });+ } finally {+ Object.defineProperty(AbortSignal.prototype, "throwIfAborted", descriptor!);+ }+ });+it("does not mutate the handle when a snapshot is aborted", async () => {vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) => {if (init?.signal?.aborted) {diff --git a/packages/eve/src/client/session.ts b/packages/eve/src/client/session.tsindex 061fdc5a1..33df352bc 100644--- a/packages/eve/src/client/session.ts+++ b/packages/eve/src/client/session.ts@@ -1,3 +1,4 @@+import { throwIfAborted } from "#client/abort-signal.js";import { TurnSegment } from "#client/session-utils.js";import type { AgentStartedStreamEvent, MessageStreamEvent } from "#protocol/message.js";import { EVE_SESSION_ID_HEADER } from "#protocol/message.js";@@ -100,7 +101,7 @@ export class ClientSession {/** Reads a finite prefix through the durable tail without advancing this handle. */async snapshot(options?: { readonly signal?: AbortSignal }): Promise<SessionSnapshot> {- options?.signal?.throwIfAborted();+ throwIfAborted(options?.signal);const events: MessageStreamEvent[] = [];for await (const event of this.#readStream({@@ -111,7 +112,7 @@ export class ClientSession {events.push(event);}- options?.signal?.throwIfAborted();+ throwIfAborted(options?.signal);return {events,session: { sessionId: this.#state.sessionId, streamIndex: events.length },@@ -373,7 +374,7 @@ async function postSessionSend(await sleep(Math.min(retryDelayMs, remainingMs), input.signal);}- input.signal?.throwIfAborted();+ throwIfAborted(input.signal);retryDelayMs = Math.min(retryDelayMs * 2, SESSION_SEND_RETRY_MAX_DELAY_MS);}}