Skip to content

Commit 1331b2e

Browse files
committed
perf(client-runtime): skip question history scans without requests
1 parent 53896ae commit 1331b2e

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎packages/client-runtime/src/state/threadRequests.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
MessageId,
23
NodeId,
34
ProviderSessionId,
45
RuntimeRequestId,
@@ -236,6 +237,45 @@ describe("pending v2 questions", () => {
236237
});
237238
});
238239

240+
it("does not visit ordinary row items during streaming replacements without runtime requests", () => {
241+
const project = createQuestionHistoryProjector();
242+
let itemVisits = 0;
243+
const question = projection.turnItems.find((item) => item.type === "user_input_request")!;
244+
const makeRow = (position: number, text: string) => {
245+
const item = {
246+
...question,
247+
id: TurnItemId.make(`assistant-${position}`),
248+
type: "assistant_message" as const,
249+
messageId: MessageId.make(`message-${position}`),
250+
text,
251+
streaming: true,
252+
};
253+
return {
254+
get item() {
255+
itemVisits++;
256+
return item;
257+
},
258+
position,
259+
sourceItemId: item.id,
260+
sourceThreadId: item.threadId,
261+
visibility: "local" as const,
262+
};
263+
};
264+
let rows = Array.from({ length: 1_000 }, (_, index) => makeRow(index, "Hello"));
265+
const runtimeRequests = v2Projection.runtimeRequests;
266+
expect(project({ visibleTurnItems: rows, runtimeRequests })).toBe(rows);
267+
const initialItemVisits = itemVisits;
268+
itemVisits = 0;
269+
for (let index = 0; index < 100; index++) {
270+
rows = [...rows.slice(0, -1), makeRow(rows.length - 1, `Hello ${index}`)];
271+
const next = { visibleTurnItems: rows, runtimeRequests };
272+
expect(project(next)).toBe(rows);
273+
expect(project(next)).toBe(rows);
274+
}
275+
expect(itemVisits).toBe(0);
276+
expect(initialItemVisits).toBe(0);
277+
});
278+
239279
it("restores old text answers without mutating history or replacing unchanged rows", () => {
240280
const project = createQuestionHistoryProjector();
241281
const item = projection.turnItems[0]!;
@@ -247,6 +287,9 @@ it("restores old text answers without mutating history or replacing unchanged ro
247287
visibility: "local" as const,
248288
};
249289
const rows = [row];
290+
const empty = { visibleTurnItems: rows, runtimeRequests: [] };
291+
expect(project(empty)).toBe(rows);
292+
expect(project(empty)).toBe(rows);
250293
const pending = { visibleTurnItems: rows, runtimeRequests: projection.runtimeRequests };
251294
expect(project(pending)).toBe(rows);
252295
const answered = {
@@ -270,4 +313,22 @@ it("restores old text answers without mutating history or replacing unchanged ro
270313
expect(row.item).not.toHaveProperty("questionAnswer");
271314
expect(project(answered)).toBe(result);
272315
expect(project({ ...answered, visibleTurnItems: [...rows] })[0]).toBe(result[0]);
316+
expect(project(empty)).toBe(rows);
317+
expect(project(empty)).toBe(rows);
318+
319+
const changedRow = { ...row, item: { ...item, title: "Updated question" } };
320+
const inserted = [changedRow, row];
321+
expect(project({ ...empty, visibleTurnItems: inserted })).toBe(inserted);
322+
const restored = project({ ...answered, visibleTurnItems: inserted });
323+
expect(restored[0]?.item).toMatchObject({
324+
title: "Updated question",
325+
questionAnswer: { answers: { next: "Continue" } },
326+
});
327+
expect(restored[1]).toBe(result[0]);
328+
expect(changedRow.item).not.toHaveProperty("questionAnswer");
329+
expect(project({ ...empty, visibleTurnItems: inserted })).toBe(inserted);
330+
const removed = [changedRow];
331+
expect(project({ ...empty, visibleTurnItems: removed })).toBe(removed);
332+
expect(project({ ...answered, visibleTurnItems: removed })[0]).toBe(restored[0]);
333+
expect(project(empty)).toBe(rows);
273334
});

‎packages/client-runtime/src/state/threadRequests.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ export function createQuestionHistoryProjector<
181181
const { visibleTurnItems: rows, runtimeRequests: requests } = projection;
182182
if (rows === previousRows && requests === previousRequests && previousResult)
183183
return previousResult;
184+
if (requests.length === 0) {
185+
previousRows = rows;
186+
previousRequests = requests;
187+
previousResult = rows;
188+
return rows;
189+
}
184190
const byId = new Map(requests.map((request) => [request.id, request]));
185191
let result: Row[] | undefined;
186192
for (const [index, row] of rows.entries()) {

0 commit comments

Comments
 (0)