From 54d7551b53ca829204788d25a3a021959992fc48 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 18 Jan 2026 05:54:39 +0000 Subject: [PATCH] refactor(usage): centralize responseUsage mode --- src/auto-reply/reply/agent-runner.ts | 9 ++------- src/auto-reply/reply/commands-session.ts | 9 ++------- src/auto-reply/thinking.ts | 4 ++++ src/tui/tui-command-handlers.ts | 13 ++++++------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/auto-reply/reply/agent-runner.ts b/src/auto-reply/reply/agent-runner.ts index 364bb0d61..80fef7f07 100644 --- a/src/auto-reply/reply/agent-runner.ts +++ b/src/auto-reply/reply/agent-runner.ts @@ -20,7 +20,7 @@ import { logVerbose } from "../../globals.js"; import { defaultRuntime } from "../../runtime.js"; import { resolveModelCostConfig } from "../../utils/usage-format.js"; import type { OriginatingChannelType, TemplateContext } from "../templating.js"; -import type { VerboseLevel } from "../thinking.js"; +import { resolveResponseUsageMode, type VerboseLevel } from "../thinking.js"; import type { GetReplyOptions, ReplyPayload } from "../types.js"; import { runAgentTurnWithFallback } from "./agent-runner-execution.js"; import { @@ -460,12 +460,7 @@ export async function runReplyAgent(params: { const responseUsageRaw = activeSessionEntry?.responseUsage ?? (sessionKey ? activeSessionStore?.[sessionKey]?.responseUsage : undefined); - const responseUsageMode = - responseUsageRaw === "full" - ? "full" - : responseUsageRaw === "tokens" || responseUsageRaw === "on" - ? "tokens" - : "off"; + const responseUsageMode = resolveResponseUsageMode(responseUsageRaw); if (responseUsageMode !== "off" && hasNonzeroUsage(usage)) { const authMode = resolveModelAuthMode(providerUsed, cfg); const showCost = authMode === "api-key"; diff --git a/src/auto-reply/reply/commands-session.ts b/src/auto-reply/reply/commands-session.ts index 2fe5e41f0..724228dad 100644 --- a/src/auto-reply/reply/commands-session.ts +++ b/src/auto-reply/reply/commands-session.ts @@ -6,7 +6,7 @@ import { createInternalHookEvent, triggerInternalHook } from "../../hooks/intern import { scheduleGatewaySigusr1Restart, triggerClawdbotRestart } from "../../infra/restart.js"; import { parseActivationCommand } from "../group-activation.js"; import { parseSendPolicyCommand } from "../send-policy.js"; -import { normalizeUsageDisplay } from "../thinking.js"; +import { normalizeUsageDisplay, resolveResponseUsageMode } from "../thinking.js"; import { formatAbortReplyText, isAbortTrigger, @@ -151,12 +151,7 @@ export const handleUsageCommand: CommandHandler = async (params, allowTextComman const currentRaw = params.sessionEntry?.responseUsage ?? (params.sessionKey ? params.sessionStore?.[params.sessionKey]?.responseUsage : undefined); - const current = - currentRaw === "full" - ? "full" - : currentRaw === "tokens" || currentRaw === "on" - ? "tokens" - : "off"; + const current = resolveResponseUsageMode(currentRaw); const next = requested ?? (current === "off" ? "tokens" : current === "tokens" ? "full" : "off"); if (params.sessionEntry && params.sessionStore && params.sessionKey) { diff --git a/src/auto-reply/thinking.ts b/src/auto-reply/thinking.ts index 75862f91c..6f9637dbd 100644 --- a/src/auto-reply/thinking.ts +++ b/src/auto-reply/thinking.ts @@ -103,6 +103,10 @@ export function normalizeUsageDisplay(raw?: string | null): UsageDisplayLevel | return undefined; } +export function resolveResponseUsageMode(raw?: string | null): UsageDisplayLevel { + return normalizeUsageDisplay(raw) ?? "off"; +} + // Normalize elevated flags used to toggle elevated bash permissions. export function normalizeElevatedLevel(raw?: string | null): ElevatedLevel | undefined { if (!raw) return undefined; diff --git a/src/tui/tui-command-handlers.ts b/src/tui/tui-command-handlers.ts index d9f12696e..1d5e34eaf 100644 --- a/src/tui/tui-command-handlers.ts +++ b/src/tui/tui-command-handlers.ts @@ -1,5 +1,9 @@ import type { Component, TUI } from "@mariozechner/pi-tui"; -import { formatThinkingLevels, normalizeUsageDisplay } from "../auto-reply/thinking.js"; +import { + formatThinkingLevels, + normalizeUsageDisplay, + resolveResponseUsageMode, +} from "../auto-reply/thinking.js"; import { normalizeAgentId } from "../routing/session-key.js"; import { helpText, parseCommand } from "./commands.js"; import type { ChatLog } from "./components/chat-log.js"; @@ -324,12 +328,7 @@ export function createCommandHandlers(context: CommandHandlerContext) { break; } const currentRaw = state.sessionInfo.responseUsage; - const current = - currentRaw === "full" - ? "full" - : currentRaw === "tokens" || currentRaw === "on" - ? "tokens" - : "off"; + const current = resolveResponseUsageMode(currentRaw); const next = normalized ?? (current === "off" ? "tokens" : current === "tokens" ? "full" : "off"); try {