fix: harden resume and SSE replay
This commit is contained in:
@@ -70,16 +70,46 @@ test("closes the stream on unmount", () => {
|
||||
expect(es.closed).toBe(true);
|
||||
});
|
||||
|
||||
test("reconnects the same session when the generation changes", () => {
|
||||
test("a same-session generation reconnect includes the last consumed SSE id", () => {
|
||||
const { rerender } = renderHook(
|
||||
({ generation }) => useSessionStream("s1", generation),
|
||||
{ initialProps: { generation: 0 } },
|
||||
);
|
||||
const first = FakeEventSource.instances[0];
|
||||
act(() => first.emitNamed("text_delta", { type: "text_delta", text: "hello" }, "7"));
|
||||
|
||||
rerender({ generation: 1 });
|
||||
|
||||
expect(first.closed).toBe(true);
|
||||
expect(FakeEventSource.instances).toHaveLength(2);
|
||||
expect(FakeEventSource.instances[1].url).toBe(first.url);
|
||||
expect(FakeEventSource.instances[1].url).toBe(
|
||||
"http://localhost:8787/sessions/s1/events?lastEventId=7",
|
||||
);
|
||||
|
||||
act(() =>
|
||||
FakeEventSource.instances[1].emitNamed(
|
||||
"text_delta",
|
||||
{ type: "text_delta", text: " world" },
|
||||
"8",
|
||||
),
|
||||
);
|
||||
expect(useSessionStore.getState().transcript).toEqual([
|
||||
{ role: "assistant", text: "hello world" },
|
||||
]);
|
||||
expect(useSessionStore.getState().activityLog.filter((entry) => entry.kind === "assistant"))
|
||||
.toEqual([{ kind: "assistant", phase: null, text: "hello world" }]);
|
||||
});
|
||||
|
||||
test("changing the session resets the manual reconnect cursor", () => {
|
||||
const { rerender } = renderHook(
|
||||
({ sessionId }) => useSessionStream(sessionId),
|
||||
{ initialProps: { sessionId: "s1" as string | null } },
|
||||
);
|
||||
const first = FakeEventSource.instances[0];
|
||||
act(() => first.emitNamed("info", { type: "info", text: "ready" }, "12"));
|
||||
|
||||
rerender({ sessionId: "s2" });
|
||||
|
||||
expect(first.closed).toBe(true);
|
||||
expect(FakeEventSource.instances[1].url).toBe("http://localhost:8787/sessions/s2/events");
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { BASE } from "../api/client";
|
||||
import { joinBackendPath } from "../api/runtime-config";
|
||||
import { useSessionStore } from "../store/sessionStore";
|
||||
@@ -7,15 +7,24 @@ import type { StreamEvent } from "../api/types";
|
||||
export function useSessionStream(sessionId: string | null, generation = 0) {
|
||||
const [connected, setConnected] = useState(false);
|
||||
const applyEvent = useSessionStore((s) => s.applyEvent);
|
||||
const cursor = useRef({ sessionId: null as string | null, lastEventId: "" });
|
||||
|
||||
if (cursor.current.sessionId !== sessionId) {
|
||||
cursor.current = { sessionId, lastEventId: "" };
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!sessionId) return;
|
||||
|
||||
const es = new EventSource(joinBackendPath(BASE, `/sessions/${sessionId}/events`));
|
||||
const query = cursor.current.lastEventId
|
||||
? `?lastEventId=${encodeURIComponent(cursor.current.lastEventId)}`
|
||||
: "";
|
||||
const es = new EventSource(joinBackendPath(BASE, `/sessions/${sessionId}/events${query}`));
|
||||
es.onopen = () => setConnected(true);
|
||||
es.onerror = () => setConnected(false);
|
||||
|
||||
const handle = (ev: MessageEvent) => {
|
||||
if (ev.lastEventId) cursor.current.lastEventId = ev.lastEventId;
|
||||
try {
|
||||
applyEvent(JSON.parse(ev.data) as StreamEvent);
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user