Skip to content

An open-source, faster Devin alternative

TypeScript

react: read the next page at once only when the last one was new

Igor Zalutski28f5a4b4ea0d
diff --git a/react/src/index.ts b/react/src/index.ts
index 9189306f..4b2fab71 100644
--- a/react/src/index.ts
+++ b/react/src/index.ts
@@ -158,13 +158,14 @@ export function useAgent(
);
// Applies a page of events to the timeline and runs the callbacks once per
- // event. Events the timeline already holds fire nothing.
+ // event. Events the timeline already holds fire nothing. Returns how many
+ // were new.
const ingest = useCallback(
- (events: AgentEvent[]) => {
+ (events: AgentEvent[]): number => {
const fresh = events.filter(
(event) => event.seq > timelineRef.current.cursor,
);
- if (!fresh.length) return;
+ if (!fresh.length) return 0;
commit(applyEvents(timelineRef.current, fresh));
for (const event of fresh) {
cursorRef.current = Math.max(cursorRef.current, event.seq);
@@ -178,6 +179,7 @@ export function useAgent(
const failure = failureMessage(event);
if (failure) setError(failure);
}
+ return fresh.length;
},
[commit],
);
@@ -205,10 +207,8 @@ export function useAgent(
if (signal.aborted) return;
if (failures) setError(undefined);
failures = 0;
- if (page.events.length) {
- ingest(page.events);
- continue;
- }
+ // A full page means more may follow: read on without waiting.
+ if (ingest(page.events) > 0) continue;
setIsReplaying(false);
} catch (cause) {
if (signal.aborted) return;
diff --git a/react/src/use-agent.test.ts b/react/src/use-agent.test.ts
index 1f6330dc..5784a8f3 100644
--- a/react/src/use-agent.test.ts
+++ b/react/src/use-agent.test.ts
@@ -217,6 +217,34 @@ test("attach resumes from its cursor after a failed poll instead of replaying",
await view.unmount();
});
+test("attach waits between polls when a page brings nothing new", async (t) => {
+ let polls = 0;
+ const stale: AgentEvent[] = [
+ { seq: 1, turnId: "t0", type: "message.received", data: { input: "hello" } },
+ { seq: 2, turnId: "t0", type: "turn.completed", data: {} },
+ ];
+ // A relay that ignores `after` and repeats the same page.
+ const view = mount(t, {
+ sessionId: "ses-stale",
+ basePath: "/app/agent",
+ fetch: async () => {
+ polls += 1;
+ return Response.json({ events: stale });
+ },
+ pollIntervalMs: 20,
+ });
+ await view.render();
+ const replayed = await view.until((result) => result.cursor === 2, "replay");
+ assert.equal(replayed.messages.length, 1);
+ const before = polls;
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ });
+ assert.ok(polls - before <= 8, `polled ${String(polls - before)} times in 100ms at a 20ms interval`);
+ assert.equal(view.result().messages.length, 1);
+ await view.unmount();
+});
+
test("attach sends turns and interrupts through the app's routes without duplicating the input", async (t) => {
const session = fakeSession("ses-3");
const view = mount(t, { sessionId: "ses-3", basePath: "/app/agent", fetch: session.fetch, pollIntervalMs: 5 });