UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

61 lines • 2.24 kB
import { isRecord } from "./types.js"; const USAGE_FIELDS = ["input", "output", "cacheRead", "cacheWrite", "totalTokens"]; export function emptyUsage() { return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 }; } export function collectAssistantUsage(messages) { const usage = emptyUsage(); for (const message of messages) addAssistantMessageUsage(usage, message); return usage; } /** * Accumulates assistant token usage streamed during the current agent run so * goal accounting can checkpoint mid-turn (tool calls, commands, shutdown) * without waiting for `agent_end` and without double counting when it fires. */ export class TurnUsageTracker { constructor() { this.pending = emptyUsage(); this.flushed = emptyUsage(); } reset() { this.pending = emptyUsage(); this.flushed = emptyUsage(); } noteMessageEnd(message) { addAssistantMessageUsage(this.pending, message); } takePending() { const taken = this.pending; this.pending = emptyUsage(); for (const field of USAGE_FIELDS) this.flushed[field] += taken[field]; return taken; } /** Drop usage streamed before a new accounting window opened mid-turn. */ discardPending() { this.takePending(); } /** Return the run's total usage minus what checkpoints already accounted. */ takeRemaining(agentRunMessages) { const collected = collectAssistantUsage(agentRunMessages); const remaining = emptyUsage(); for (const field of USAGE_FIELDS) { remaining[field] = Math.max(0, collected[field] - this.flushed[field]); this.flushed[field] = Math.max(this.flushed[field], collected[field]); } this.pending = emptyUsage(); return remaining; } } function addAssistantMessageUsage(target, message) { if (!isRecord(message) || message.role !== "assistant" || !isRecord(message.usage)) return; for (const field of USAGE_FIELDS) { const value = message.usage[field]; if (typeof value === "number" && Number.isFinite(value)) target[field] += value; } } //# sourceMappingURL=turn-usage.js.map