The Open Framework for Building Agents
fix(eve): resume web clients after connection auth (#2341)
Allen Zhou9e19fa419201
diff --git a/.changeset/tidy-plums-resume.md b/.changeset/tidy-plums-resume.mdnew file mode 100644index 000000000..f831486ea--- /dev/null+++ b/.changeset/tidy-plums-resume.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Keep active client streams attached across authorization parking boundaries so connection callbacks resume browser chats automatically.diff --git a/docs/guides/client/streaming.mdx b/docs/guides/client/streaming.mdxindex e3a079507..13ce0b706 100644--- a/docs/guides/client/streaming.mdx+++ b/docs/guides/client/streaming.mdx@@ -124,7 +124,7 @@ client-resolvable `http(s)` and `data:` URLs.`authorization.required` is different from the normal `session.waiting` boundary. It means a connection needs OAuth or another authorization challenge before the parked turn can continue. Chat UIs should render the authorization prompt, disable ordinary text input for that session, and persist the event with the rest of the chat history.-If you support refresh while an authorization prompt is pending, keep the session cursor from the started session and rehydrate the saved events on load. Do not treat a missing `session.waiting` after `authorization.required` as a finished conversation; the callback or a structured decline should resume the same eve session.+The stream can emit an interim `session.waiting` while the authorization callback is pending. An active `send()` or `respond()` response stays attached across that parking boundary until the authorization resolves and the resumed turn reaches its next boundary. If you support refresh while an authorization prompt is pending, keep the session cursor from the started session and rehydrate the saved events on load; the callback or a structured decline resumes the same eve session.## Reconnectiondiff --git a/packages/eve/src/client/session.test.ts b/packages/eve/src/client/session.test.tsindex f08b83171..b607591df 100644--- a/packages/eve/src/client/session.test.ts+++ b/packages/eve/src/client/session.test.ts@@ -800,7 +800,7 @@ describe("ClientSession", () => {]);});- it("keeps following an active turn while authorization is pending", async () => {+ it("keeps following an active turn across an authorization parking boundary", async () => {let streamRequest = 0;const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) => {if ((init?.method ?? "GET") === "POST") {@@ -809,13 +809,22 @@ describe("ClientSession", () => {streamRequest += 1;if (streamRequest === 1) {- return createStreamResponse([{ type: "authorization.required", data: {} }]);+ return createStreamResponse([+ {+ type: "authorization.required",+ data: { name: "linear", webhookUrl: "https://agent.example.com/auth/linear" },+ },+ {+ type: "session.waiting",+ data: { continuationToken: "session-id", wait: "next-user-message" },+ },+ ]);}if (streamRequest <= 7) {return createStreamResponse([]);}return createStreamResponse([- { type: "authorization.completed", data: {} },+ { type: "authorization.completed", data: { name: "linear", outcome: "authorized" } },{type: "session.waiting",data: { continuationToken: "session-id", wait: "next-user-message" },@@ -829,6 +838,7 @@ describe("ClientSession", () => {const eventTypes = await collectEventTypes(await session.send("first"));expect(eventTypes).toEqual(["authorization.required",+ "session.waiting","authorization.completed","session.waiting",]);@@ -837,7 +847,36 @@ describe("ClientSession", () => {}expect(fetchMock).toHaveBeenCalledTimes(9);- expect(session.state.streamIndex).toBe(3);+ expect(session.state.streamIndex).toBe(4);+ });++ it("stops at a non-blocking authorization parking boundary", async () => {+ const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) => {+ if ((init?.method ?? "GET") === "POST") {+ return createAcceptedResponse();+ }++ return createStreamResponse([+ { type: "authorization.required", data: { name: "linear" } },+ {+ type: "session.waiting",+ data: { continuationToken: "session-id", wait: "next-user-message" },+ },+ ]);+ });+ const session = createSession();++ vi.useFakeTimers();+ let eventTypes: string[];+ try {+ eventTypes = await collectEventTypes(await session.send("first"));+ } finally {+ vi.useRealTimers();+ }++ expect(eventTypes).toEqual(["authorization.required", "session.waiting"]);+ expect(fetchMock).toHaveBeenCalledTimes(2);+ expect(session.state.streamIndex).toBe(2);});it("honors an explicit idle reconnect limit for an active turn", async () => {diff --git a/packages/eve/src/client/session.ts b/packages/eve/src/client/session.tsindex 8e120dbd3..3099112b5 100644--- a/packages/eve/src/client/session.ts+++ b/packages/eve/src/client/session.ts@@ -181,6 +181,7 @@ export class ClientSession {input: SendTurnPayload,): AsyncGenerator<MessageStreamEvent> {let eventCount = 0;+ const pendingAuthorizations = new Set<string>();try {for await (const event of this.#readStream({headers: input.headers,@@ -190,8 +191,18 @@ export class ClientSession {streamReconnectPolicy: input.streamReconnectPolicy,})) {eventCount += 1;+ if (event.type === "authorization.required" && event.data.webhookUrl !== undefined) {+ pendingAuthorizations.add(event.data.name);+ } else if (event.type === "authorization.completed") {+ pendingAuthorizations.delete(event.data.name);+ }yield event;- if (isCurrentTurnBoundaryEvent(event)) break;+ if (+ isCurrentTurnBoundaryEvent(event) &&+ (event.type !== "session.waiting" || pendingAuthorizations.size === 0)+ ) {+ break;+ }}} finally {this.#state = {