Repository navigation
fix(chat): bound pages around the saved position #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7aa2bdd
fix(chat): bound pages around the saved position
klNuno d3b6426
fix(chat): align history validation and previews
klNuno 1e8fb58
fix(chat): honor a single-message reading window
klNuno 6c5fe22
fix(chat): reuse each fake page preview
klNuno 348f58c
fix(history): measure structured message windows exactly
klNuno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { Message } from './index.ts'; | ||
| import { utf8Bytes } from './transport.ts'; | ||
|
|
||
| /** | ||
| * A contiguous slice around the anchor, measured as the client receives it. | ||
| * Drop the farthest edge until the two halves share one page's byte and | ||
| * message budgets. Keep a single complete anchor even when it alone exceeds | ||
| * the page budget, as ordinary pages do. Callers provide the index of an | ||
| * existing anchor and preserve cursors for either edge that was cut. | ||
| */ | ||
| export function boundedMessageWindow(messages: Message[], anchorIndex: number, maxBytes: number, maxMessages: number): { start: number; end: number } { | ||
| let start = 0, end = messages.length; | ||
| const sizes = messages.map(message => utf8Bytes(JSON.stringify(message))); | ||
| let bytes = 2 + sizes.reduce((sum, size) => sum + size, 0) + Math.max(0, end - 1); | ||
| while ((bytes > maxBytes || end - start > maxMessages) && end - start > 1) { | ||
| const older = anchorIndex - start; | ||
| const newer = end - 1 - anchorIndex; | ||
| const removed = older > newer ? start++ : --end; | ||
| // Removing an edge from at least two messages removes exactly one comma. | ||
| bytes -= sizes[removed]! + 1; | ||
| } | ||
| return { start, end }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { expect, test } from 'bun:test'; | ||
| import { boundedMessageWindow, type Message } from '@boite/contracts'; | ||
|
|
||
| test('a centred page counts empty object syntax in tool inputs against its byte budget', () => { | ||
| const messages: Message[] = ['older', 'anchor', 'newer'].map(id => ({ | ||
| id, threadId: 'thread', turnId: 'turn', role: 'assistant', state: 'complete', createdAt: 0, | ||
| parts: [{ type: 'tool', toolId: id, name: 'Read', status: 'done', output: null, input: Array.from({ length: 5000 }, () => ({})) }], | ||
| })); | ||
| const budget = 40_000; | ||
| expect(Buffer.byteLength(JSON.stringify(messages))).toBeGreaterThan(budget); | ||
| const { start, end } = boundedMessageWindow(messages, 1, budget, messages.length); | ||
| const page = messages.slice(start, end); | ||
| expect(Buffer.byteLength(JSON.stringify(page))).toBeLessThanOrEqual(budget); | ||
| expect(page.map(message => message.id)).toContain('anchor'); | ||
| expect(page).toHaveLength(2); | ||
|
klNuno marked this conversation as resolved.
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.