Fix OpenCode context window meter after first turn

This commit is contained in:
Mohamed Boudra
2026-04-07 14:53:00 +07:00
parent 161b2c2378
commit 022eb33234
@@ -68,6 +68,22 @@ export type {
const HISTORY_STALE_AFTER_MS = 60_000;
const AUTHORITATIVE_REVALIDATION_DEBOUNCE_MS = 300;
function hasAgentUsageChanged(
incomingUsage: Agent["lastUsage"] | undefined,
currentUsage: Agent["lastUsage"] | undefined,
): boolean {
const keys: Array<keyof NonNullable<Agent["lastUsage"]>> = [
"inputTokens",
"outputTokens",
"cachedInputTokens",
"totalCostUsd",
"contextWindowMaxTokens",
"contextWindowUsedTokens",
];
return keys.some((key) => incomingUsage?.[key] !== currentUsage?.[key]);
}
type AudioOutputPayload = Extract<SessionOutboundMessage, { type: "audio_output" }>["payload"];
interface BufferedAudioChunk {
@@ -350,6 +366,15 @@ function SessionProviderInternal({ children, serverId, client }: SessionProvider
setAgents(serverId, (prev) => {
const current = prev.get(agent.id);
if (current && agent.updatedAt.getTime() < current.updatedAt.getTime()) {
const hasUsageUpdate = hasAgentUsageChanged(agent.lastUsage, current.lastUsage);
if (hasUsageUpdate) {
const next = new Map(prev);
next.set(agent.id, {
...current,
lastUsage: agent.lastUsage,
});
return next;
}
return prev;
}
const next = new Map(prev);