Repository navigation
fix(grok): show context-window usage in the composer #9206
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,12 +55,16 @@ import { | |
| makeAcpPlanUpdatedEvent, | ||
| makeAcpRequestOpenedEvent, | ||
| makeAcpRequestResolvedEvent, | ||
| makeAcpTokenUsageEvent, | ||
| makeAcpToolCallEvent, | ||
| } from "../acp/AcpCoreRuntimeEvents.ts"; | ||
| import { parsePermissionRequest } from "../acp/AcpRuntimeModel.ts"; | ||
| import { makeAcpNativeLoggerFactory } from "../acp/AcpNativeLogging.ts"; | ||
| import { | ||
| applyGrokAcpModelSelection, | ||
| contextWindowForGrokModelId, | ||
| contextWindowFromGrokSessionSetup, | ||
| contextWindowsFromGrokSessionModels, | ||
| currentGrokModelIdFromSessionSetup, | ||
| currentGrokReasoningEffortFromSessionSetup, | ||
| makeGrokAcpRuntime, | ||
|
|
@@ -169,6 +173,10 @@ interface GrokSessionContext { | |
| promptResponsesReady: number; | ||
| currentModelId: string | undefined; | ||
| currentReasoningEffort: string | undefined; | ||
| contextWindows: ReadonlyMap<string, number>; | ||
| lastContextTokens: number | undefined; | ||
| lastKnownMaxTokens: number | undefined; | ||
| lastEmittedMaxTokens: number | undefined; | ||
| stopped: boolean; | ||
| } | ||
|
|
||
|
|
@@ -314,6 +322,37 @@ function selectAutoApprovedPermissionOption( | |
| ); | ||
| } | ||
|
|
||
| export const GROK_DEFAULT_CONTEXT_WINDOW = 500_000; | ||
|
|
||
| export function resolveGrokContextUsage(input: { | ||
| readonly used: number; | ||
| readonly eventSize: number; | ||
| readonly lastUsed: number | undefined; | ||
| readonly lastKnownMaxTokens: number | undefined; | ||
| readonly lastEmittedMaxTokens: number | undefined; | ||
| }): { readonly used: number; readonly size: number } | undefined { | ||
| const size = | ||
| input.eventSize > 0 | ||
| ? input.eventSize | ||
| : (input.lastKnownMaxTokens ?? GROK_DEFAULT_CONTEXT_WINDOW); | ||
| if (input.lastUsed === input.used && input.lastEmittedMaxTokens === size) { | ||
| return undefined; | ||
| } | ||
| return { used: input.used, size }; | ||
| } | ||
|
|
||
| export function grokContextWindowAfterModelChange(input: { | ||
| readonly previousModelId: string | undefined; | ||
| readonly nextModelId: string | undefined; | ||
| readonly windows: ReadonlyMap<string, number>; | ||
| readonly lastKnownMaxTokens: number | undefined; | ||
| }): number | undefined { | ||
| if (input.nextModelId === input.previousModelId) { | ||
| return input.lastKnownMaxTokens; | ||
| } | ||
| return contextWindowForGrokModelId(input.windows, input.nextModelId); | ||
| } | ||
|
|
||
| function completedStopReasonFromPromptResponse( | ||
| response: EffectAcpSchema.PromptResponse | undefined, | ||
| ): EffectAcpSchema.StopReason | null { | ||
|
|
@@ -1300,6 +1339,13 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | |
| requestedStartReasoningEffort !== undefined | ||
| ? normalizeGrokReasoningEffort(requestedStartReasoningEffort) | ||
| : currentStartReasoningEffort, | ||
| contextWindows: contextWindowsFromGrokSessionModels(started.sessionSetupResult.models), | ||
| lastContextTokens: undefined, | ||
| lastKnownMaxTokens: contextWindowFromGrokSessionSetup( | ||
| started.sessionSetupResult, | ||
| boundModelId, | ||
| ), | ||
| lastEmittedMaxTokens: undefined, | ||
| stopped: false, | ||
| }; | ||
|
|
||
|
|
@@ -1313,7 +1359,8 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | |
| if ( | ||
| event._tag === "PlanUpdated" || | ||
| event._tag === "ToolCallUpdated" || | ||
| event._tag === "ContentDelta" | ||
| event._tag === "ContentDelta" || | ||
| event._tag === "UsageUpdated" | ||
| ) { | ||
| yield* logNative(ctx.threadId, "session/update", event.rawPayload); | ||
| } | ||
|
|
@@ -1322,6 +1369,41 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | |
| return; | ||
| } | ||
|
|
||
| if (event._tag === "UsageUpdated") { | ||
| const usageTurnId = resolveNotificationTurnId(ctx); | ||
| if (usageTurnId !== undefined && ctx.interruptedTurnIds.has(usageTurnId)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium A queued 🤖 Copy this AI Prompt to have your agent fix this: |
||
| return; | ||
| } | ||
| const snapshot = resolveGrokContextUsage({ | ||
| used: event.used, | ||
| eventSize: event.size, | ||
| lastUsed: ctx.lastContextTokens, | ||
| lastKnownMaxTokens: ctx.lastKnownMaxTokens, | ||
| lastEmittedMaxTokens: ctx.lastEmittedMaxTokens, | ||
| }); | ||
| if (!snapshot) { | ||
| return; | ||
| } | ||
| ctx.lastContextTokens = snapshot.used; | ||
| if (event.size > 0) { | ||
| ctx.lastKnownMaxTokens = event.size; | ||
| } | ||
| ctx.lastEmittedMaxTokens = snapshot.size; | ||
| const usageEvent = makeAcpTokenUsageEvent({ | ||
| stamp: yield* makeEventStamp(), | ||
| provider: PROVIDER, | ||
| threadId: ctx.threadId, | ||
| turnId: usageTurnId, | ||
| used: snapshot.used, | ||
| size: snapshot.size, | ||
| rawPayload: event.rawPayload, | ||
| }); | ||
| if (usageEvent) { | ||
| yield* offerRuntimeEvent(usageEvent); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const notificationTurnId = resolveNotificationTurnId(ctx); | ||
| if ( | ||
| notificationTurnId === undefined || | ||
|
|
@@ -1574,6 +1656,12 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | |
| mapError: (cause) => | ||
| mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_model", cause), | ||
| }); | ||
| ctx.lastKnownMaxTokens = grokContextWindowAfterModelChange({ | ||
| previousModelId: ctx.currentModelId, | ||
| nextModelId: currentModelId, | ||
| windows: ctx.contextWindows, | ||
| lastKnownMaxTokens: ctx.lastKnownMaxTokens, | ||
| }); | ||
| ctx.currentModelId = currentModelId; | ||
| if (requestedTurnReasoningEffort !== undefined) { | ||
| ctx.currentReasoningEffort = normalizeGrokReasoningEffort( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.