Repository navigation
📊 feat: Feed a Side Chat's Usage to Its Composer's Gauge #18488
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
Draft
danny-avila
wants to merge
3
commits into
danny-avila/side-chat-composer
Choose a base branch
from
danny-avila/side-chat-live-usage
base: danny-avila/side-chat-composer
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+406
−44
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,66 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { | ||
| replyText, | ||
| replyPrompt, | ||
| messagesView, | ||
| NEW_CHAT_PATH, | ||
| MOCK_ENDPOINTS, | ||
| selectMockEndpoint, | ||
| sendMessageAndWaitForCompletion, | ||
| } from '../helpers'; | ||
|
|
||
| /** | ||
| * The context gauge counts a reply as it is written: the reading the server sends when the | ||
| * reply starts, plus the output that has arrived since. | ||
| */ | ||
| test.use({ viewport: { width: 1440, height: 900 }, isMobile: false, hasTouch: false }); | ||
|
|
||
| const uniqueLabel = (prefix: string) => | ||
| `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`; | ||
|
|
||
| /** The gauge's used-token count, read from its accessible name. */ | ||
| const used = (label: string | null): number => { | ||
| const match = /Context window: ([\d.,]+)(K?) of/.exec(label ?? ''); | ||
| if (match == null) { | ||
| throw new Error(`The gauge does not say how much is used: ${label}`); | ||
| } | ||
| return Number(match[1].replace(/,/g, '')) * (match[2] === 'K' ? 1000 : 1); | ||
| }; | ||
|
|
||
| test('the gauge keeps counting while a reply streams @scenario:context-gauge-follows-a-streaming-reply', async ({ | ||
| page, | ||
| }) => { | ||
| test.setTimeout(120_000); | ||
| const label = uniqueLabel('gauge'); | ||
| await page.goto(NEW_CHAT_PATH, { timeout: 10_000 }); | ||
| await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); | ||
| const first = await sendMessageAndWaitForCompletion(page, replyPrompt(label)); | ||
| expect(first.ok()).toBeTruthy(); | ||
| await expect(messagesView(page).getByText(replyText(label))).toBeVisible(); | ||
|
|
||
| const form = page.locator('main form').first(); | ||
| const gauge = form.getByTestId('token-usage'); | ||
| await form.getByRole('textbox', { name: 'Message input' }).click(); | ||
| await page.keyboard.type(`E2E_SLOW_REPLY:${label}`); | ||
| await page.keyboard.press('Enter'); | ||
|
|
||
| /** | ||
| * Early in the reply, and again well into it, both while it is still being written: more has | ||
| * arrived, so more is used. Read as they stand, with no waiting that could run past the end | ||
| * of the reply, where the count settles whatever it did meanwhile. | ||
| */ | ||
| const stop = page.getByRole('button', { name: 'Stop generating' }); | ||
| await expect(messagesView(page)).toContainText('chunk-020', { timeout: 30_000 }); | ||
| const early = used(await gauge.getAttribute('aria-label')); | ||
| await expect(messagesView(page)).toContainText('chunk-100', { timeout: 60_000 }); | ||
| const late = used(await gauge.getAttribute('aria-label')); | ||
| await expect(stop).toBeVisible(); | ||
| expect(late).toBeGreaterThan(early); | ||
|
|
||
| /** The settled count is not below what it showed on the way. */ | ||
| await expect(messagesView(page)).toContainText('chunk-159', { timeout: 60_000 }); | ||
| await expect(stop).toHaveCount(0); | ||
| await expect | ||
| .poll(async () => used(await gauge.getAttribute('aria-label')), { timeout: 10_000 }) | ||
| .toBeGreaterThanOrEqual(late - 5); | ||
| }); |
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,41 @@ | ||
| import { Constants, StepEvents } from 'librechat-data-provider'; | ||
| import type { TSubmission } from 'librechat-data-provider'; | ||
| import type { StepEvent } from '../reducer'; | ||
| import { nameStepResponse } from '../resumable/events'; | ||
|
|
||
| const submission = { userMessage: { messageId: 'u1' } } as TSubmission; | ||
| const runStep = (runId: string | undefined): StepEvent => | ||
| ({ event: StepEvents.ON_RUN_STEP, data: { id: 's0', runId, index: 0 } }) as StepEvent; | ||
|
|
||
| describe('nameStepResponse', () => { | ||
| it('tells the usage writers the reply id a run step carries', () => { | ||
| const nameResponse = jest.fn(); | ||
|
|
||
| nameStepResponse({ nameResponse }, runStep('stored-reply'), submission); | ||
|
|
||
| expect(nameResponse).toHaveBeenCalledWith('stored-reply', submission); | ||
| }); | ||
|
|
||
| it('says nothing for a step that names no row of its own, or for any other event', () => { | ||
| const nameResponse = jest.fn(); | ||
|
|
||
| nameStepResponse({ nameResponse }, runStep(undefined), submission); | ||
| nameStepResponse( | ||
| { nameResponse }, | ||
| runStep(Constants.USE_PRELIM_RESPONSE_MESSAGE_ID as string), | ||
| submission, | ||
| ); | ||
| nameStepResponse( | ||
| { nameResponse }, | ||
| { event: StepEvents.ON_MESSAGE_DELTA, data: { id: 's0', delta: {} } } as StepEvent, | ||
| submission, | ||
| ); | ||
|
|
||
| expect(nameResponse).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('is harmless where no usage is kept', () => { | ||
| expect(() => nameStepResponse(undefined, runStep('stored-reply'), submission)).not.toThrow(); | ||
| expect(() => nameStepResponse({}, runStep('stored-reply'), submission)).not.toThrow(); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a turn sent from the side composer receives a terminal
errorframe after streaming output or token usage, this reset is never reached:createEventApplieronly flushes message writes for errors, anduseFollowChat'sconclude()does not callonSettledfor an owned turn without a final frame. The newly connected writers therefore leave the active response, live counters, and pending usage populated.useTokenUsagecontinues adding that pending usage to totals, and the next turn inherits it because neitherbindResponsenorcontextHandlerclears pending usage. Wire terminal failure cleanup separately from history settlement, with a regression test covering an error after usage has arrived.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid, fixed in 8c883b3.
A turn sent from the side composer that ends with no final frame (an
errorframe, or a stop before the server kept anything of it) now drops what was counted for its reply:useFollowChat'sconclude()callsusage.resetLiveon that path, whichtoResumableUsagemaps to the sameresetLivemain chat's session runs on its error frame (core/resumable/failure.ts). A turn that ends on its final frame is still settled byfinalizeUsagealone.Test:
driven.spec.tsx› "drops what was counted for a reply that fails, and keeps it for one that ends" runs the real usage writers against a store, sends a turn, folds atoken_usageframe, then anerrorframe, and reads the pending usage and the active response back. Without the call it fails with the 40 output tokens and one folded event still pending; with it, both are cleared, and a following turn that ends on its final frame never triggers the reset.