From e2646ad2b0c28bd64ad4efd6b154f97f8a35e0ad Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Fri, 12 Jun 2026 00:43:55 +0900 Subject: feat(metrics): expose current context size to the frontend contextSize = the turn's FINAL step inputTokens+outputTokens (true context occupancy; NOT the aggregate usage, which sums per-step prompts and overcounts multi-step turns). Stamped on both the live done event (kernel) and persisted TurnMetrics (session-orchestrator); a client reads the latest turn's value. - @dispatch/wire 0.4.0->0.5.0: optional contextSize on TurnDoneEvent + TurnMetrics - @dispatch/transport-contract 0.5.0->0.6.0 (re-export only) - glossary: context size (reserve 'context window' for the model limit, later) - FE courier: frontend-context-size-handoff.md 881 vitest pass; tsc -b EXIT 0; biome clean. --- packages/kernel/src/runtime/events.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'packages/kernel/src/runtime/events.ts') diff --git a/packages/kernel/src/runtime/events.ts b/packages/kernel/src/runtime/events.ts index 300e711..b194577 100644 --- a/packages/kernel/src/runtime/events.ts +++ b/packages/kernel/src/runtime/events.ts @@ -127,16 +127,29 @@ export function doneEvent( reason: string, durationMs?: number, usage?: Usage, + contextSize?: number, ): AgentEvent { + if (durationMs !== undefined && usage !== undefined && contextSize !== undefined) { + return { type: "done", conversationId, turnId, reason, durationMs, usage, contextSize }; + } if (durationMs !== undefined && usage !== undefined) { return { type: "done", conversationId, turnId, reason, durationMs, usage }; } + if (durationMs !== undefined && contextSize !== undefined) { + return { type: "done", conversationId, turnId, reason, durationMs, contextSize }; + } + if (usage !== undefined && contextSize !== undefined) { + return { type: "done", conversationId, turnId, reason, usage, contextSize }; + } if (durationMs !== undefined) { return { type: "done", conversationId, turnId, reason, durationMs }; } if (usage !== undefined) { return { type: "done", conversationId, turnId, reason, usage }; } + if (contextSize !== undefined) { + return { type: "done", conversationId, turnId, reason, contextSize }; + } return { type: "done", conversationId, turnId, reason }; } -- cgit v1.2.3