Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend/chat/message-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ describe('getMessagePage', () => {
expect(collected).toEqual(ids);
});

it('reaches back to a requested message in one page, contiguous with the loaded ones', () => {
const session = newSession();
const ids = buildBranch(session, 12, 2); // 6 messages per turn

const latest = getMessagePage(session, { minCount: 7 });
const before = (latest.messages[0] as { messageId: string }).messageId;
const target = ids[7]; // inside turn 1, far older than one page
const page = getMessagePage(session, { beforeId: before, minCount: 7, untilId: target });
const pageIds = page.messages.map((message) => (message as { messageId: string }).messageId);

expect(pageIds).toContain(target);
const start = ids.indexOf(pageIds[0]);
expect(start).toBeLessThanOrEqual(7);
expect([...pageIds, ...latest.messages.map((m) => (m as { messageId: string }).messageId)]).toEqual(ids.slice(start));
expect(page.hasMore).toBe(true);
});

it('answers an empty page for a session with no messages, or an unknown cursor', () => {
const session = newSession();
expect(getMessagePage(session)).toEqual({ messages: [], hasMore: false });
Expand Down
8 changes: 6 additions & 2 deletions backend/chat/message-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ function headOf(sessionId: string): string | null {
/**
* One page of the active branch, ending just before `beforeId` (or at the head
* when absent). Holds at least `minCount` messages and always begins at a turn.
* With `untilId` it also reaches back to that message, so a jump to an old
* prompt is one request instead of a page-by-page walk.
*/
export function getMessagePage(
sessionId: string,
options: { beforeId?: string; minCount?: number } = {}
options: { beforeId?: string; minCount?: number; untilId?: string } = {}
): MessagePage {
const minCount = Math.max(1, options.minCount ?? DEFAULT_PAGE_SIZE);
const byId = getDatabase().prepare('SELECT * FROM messages WHERE id = ? AND session_id = ?');
Expand All @@ -107,6 +109,7 @@ export function getMessagePage(

const messages: UnifiedMessage[] = [];
const boundary = new BoundaryTracker();
let reachedUntil = !options.untilId;
while (cursor) {
const row = byId.get(cursor, sessionId) as DatabaseMessage | null;
if (!row) {
Expand All @@ -116,8 +119,9 @@ export function getMessagePage(
const message = JSON.parse(row.data) as UnifiedMessage;
messages.push(message);
cursor = row.parent_message_id ?? null;
if (row.id === options.untilId) reachedUntil = true;
const mayStartHere = boundary.add(message);
if (messages.length >= minCount && mayStartHere) break;
if (messages.length >= minCount && reachedUntil && mayStartHere) break;
}

return {
Expand Down
4 changes: 3 additions & 1 deletion backend/ws/messages/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const crudHandler = createRouter()
session_id: t.String({ minLength: 1 }),
/** Return the page that ends just before this message; omit for the latest. */
before_id: t.Optional(t.String()),
/** Also reach back to this message (a jump to an old prompt). */
until_id: t.Optional(t.String()),
/** Minimum page size; the page is extended back to the start of a turn. */
limit: t.Optional(t.Number({ minimum: 1, maximum: 500 }))
}),
Expand All @@ -52,7 +54,7 @@ export const crudHandler = createRouter()
})
}, async ({ data }) => {
requireSessionAccess(data.session_id);
return getMessagePage(data.session_id, { beforeId: data.before_id, minCount: data.limit });
return getMessagePage(data.session_id, { beforeId: data.before_id, untilId: data.until_id, minCount: data.limit });
})

// Every user prompt on the active branch — the chat's navigation rail.
Expand Down
Loading
Loading