Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Show context-window usage in the last-turn summary (OpenCode parity)

rcourtman8ce0f4a872a9
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 2d3aed4429..7e4db274eb 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -2388,6 +2388,12 @@ deriving an older display status from `workflowStatusHistory`.
## Current State
+The done event carries `context_limit_tokens` (the active model's context
+window from `providers.ContextWindowTokens`) alongside token usage, and the
+drawer's last-turn summary renders input tokens as a percentage of that
+window ("8,500 tokens (6% of context)") so operators can see compaction
+coming; when the limit is absent the summary renders exactly as before.
+
Post-tool model turns mark the status handoff: every agentic turn after the
first emits a `model_processing` workflow state ("Working on the response
with the gathered results.") before the provider call, so a provider that
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/assistantTurnSummary.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/assistantTurnSummary.test.ts
index 3b1cd27ab3..bdbb03d60f 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/assistantTurnSummary.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/assistantTurnSummary.test.ts
@@ -51,6 +51,23 @@ describe('assistantTurnSummary', () => {
});
});
+ it('shows how full the context window was when the done event carries the limit', () => {
+ expect(
+ getAssistantTurnSummary(
+ makeAssistantMessage({
+ tokens: { input: 8200, output: 300, contextLimit: 131072 },
+ }),
+ {
+ getModelRouteLabel: () => 'Qwen via OpenRouter',
+ },
+ ),
+ ).toEqual({
+ label: 'Last turn: Qwen via OpenRouter · 3s · 8,500 tokens (6% of context)',
+ title:
+ 'Last assistant turn summary: Model: Qwen via OpenRouter. Duration: 3s. Usage: 8,500 total, 8,200 input, 300 output, context 8,200 of 131,072 (6%)',
+ });
+ });
+
it('keeps route and duration visible when provider usage is missing', () => {
expect(
getAssistantTurnSummary(
diff --git a/frontend-modern/src/components/AI/Chat/assistantTurnSummary.ts b/frontend-modern/src/components/AI/Chat/assistantTurnSummary.ts
index 74ed8e7e10..a15ea5db8f 100644
--- a/frontend-modern/src/components/AI/Chat/assistantTurnSummary.ts
+++ b/frontend-modern/src/components/AI/Chat/assistantTurnSummary.ts
@@ -39,11 +39,21 @@ const assistantTokenSummary = (message: ChatMessage) => {
if (output <= 0) return null;
const total = input + output;
+ // How full the model's context window was this turn (input side carries
+ // the conversation), so the operator can see compaction coming.
+ const contextLimit = normalizeAssistantTokenCount(message.tokens?.contextLimit);
+ const contextPercent =
+ contextLimit > 0 && input > 0 ? Math.min(100, Math.round((input / contextLimit) * 100)) : 0;
+ const contextLabel = contextPercent > 0 ? ` (${contextPercent}% of context)` : '';
+ const contextDetail =
+ contextPercent > 0
+ ? `, context ${formatAssistantTokenCount(input)} of ${formatAssistantTokenCount(contextLimit)} (${contextPercent}%)`
+ : '';
return {
- label: `${formatAssistantTokenCount(total)} ${total === 1 ? 'token' : 'tokens'}`,
+ label: `${formatAssistantTokenCount(total)} ${total === 1 ? 'token' : 'tokens'}${contextLabel}`,
detail: `${formatAssistantTokenCount(total)} total, ${formatAssistantTokenCount(
input,
- )} input, ${formatAssistantTokenCount(output)} output`,
+ )} input, ${formatAssistantTokenCount(output)} output${contextDetail}`,
};
};
diff --git a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
index 511adda839..18ab26ca8c 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -1050,15 +1050,19 @@ export function useChat(options: UseChatOptions = {}) {
return '';
};
- const extractTokens = (data: unknown): { input: number; output: number } | null => {
+ const extractTokens = (
+ data: unknown,
+ ): { input: number; output: number; contextLimit?: number } | null => {
if (!data || typeof data !== 'object') return null;
const record = data as Record<string, unknown>;
const input = Number(record.input_tokens ?? record.inputTokens ?? record.input);
const output = Number(record.output_tokens ?? record.outputTokens ?? record.output);
if (!Number.isFinite(input) && !Number.isFinite(output)) return null;
+ const contextLimit = Number(record.context_limit_tokens ?? record.contextLimitTokens);
return {
input: Number.isFinite(input) && input > 0 ? input : 0,
output: Number.isFinite(output) && output > 0 ? output : 0,
+ ...(Number.isFinite(contextLimit) && contextLimit > 0 ? { contextLimit } : {}),
};
};
diff --git a/frontend-modern/src/components/AI/Chat/types.ts b/frontend-modern/src/components/AI/Chat/types.ts
index a7c993ae58..6d6a570dd8 100644
--- a/frontend-modern/src/components/AI/Chat/types.ts
+++ b/frontend-modern/src/components/AI/Chat/types.ts
@@ -180,7 +180,7 @@ export interface ChatMessage {
timestamp: Date;
completedAt?: Date;
model?: string;
- tokens?: { input: number; output: number };
+ tokens?: { input: number; output: number; contextLimit?: number };
toolCalls?: ToolExecution[];
isStreaming?: boolean;
workflowStatus?: WorkflowStatus;
diff --git a/internal/ai/chat/interaction_scenario_corpus_test.go b/internal/ai/chat/interaction_scenario_corpus_test.go
index 41c1e196a8..c03e71d8a5 100644
--- a/internal/ai/chat/interaction_scenario_corpus_test.go
+++ b/internal/ai/chat/interaction_scenario_corpus_test.go
@@ -105,13 +105,13 @@ func interactionScenarios() []interactionScenario {
return []interactionScenario{
{
name: "plain answer turn",
- promise: "a simple question streams an answer and a terminal done stamped with the model route — no errors, no tool noise",
+ promise: "a simple question streams an answer and a terminal done stamped with the model route and context window — no errors, no tool noise",
prompt: "is pulse healthy",
calls: []scriptedProviderCall{providerContentDone("Pulse is healthy.")},
orderedTypes: []string{"session", "workflow_state", "content", "done"},
forbiddenTypes: []string{"error", "tool_start", "tool_end"},
answerMustContain: []string{"Pulse is healthy."},
- streamMustContain: []string{`"model":"openai:test"`},
+ streamMustContain: []string{`"model":"openai:test"`, `"context_limit_tokens"`},
},
{
name: "greeting answers directly without tool noise",
diff --git a/internal/ai/chat/service.go b/internal/ai/chat/service.go
index cd61682eff..12ccfa9fd9 100644
--- a/internal/ai/chat/service.go
+++ b/internal/ai/chat/service.go
@@ -957,10 +957,11 @@ func (s *Service) ExecuteStream(ctx context.Context, req ExecuteRequest, callbac
// Send done event with token usage for this request.
doneData, _ := json.Marshal(DoneData{
- SessionID: session.ID,
- Model: selectedModel,
- InputTokens: loop.GetTotalInputTokens(),
- OutputTokens: loop.GetTotalOutputTokens(),
+ SessionID: session.ID,
+ Model: selectedModel,
+ InputTokens: loop.GetTotalInputTokens(),
+ OutputTokens: loop.GetTotalOutputTokens(),
+ ContextLimitTokens: providers.ContextWindowTokens(selectedModel),
})
streamCallback(StreamEvent{Type: "done", Data: doneData})
diff --git a/internal/ai/chat/types.go b/internal/ai/chat/types.go
index 98d0ff6f77..a21c0eb25c 100644
--- a/internal/ai/chat/types.go
+++ b/internal/ai/chat/types.go
@@ -414,6 +414,9 @@ type DoneData struct {
Model string `json:"model,omitempty"`
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
+ // ContextLimitTokens is the active model's context window, so the client
+ // can show how full the turn's context was without a model table of its own.
+ ContextLimitTokens int `json:"context_limit_tokens,omitempty"`
}
// Control level constants