feat: add usage cost reporting

This commit is contained in:
Peter Steinberger
2026-01-09 02:21:17 +00:00
parent dfbee10377
commit 151523f47b
29 changed files with 696 additions and 184 deletions

View File

@@ -1,11 +1,5 @@
import {
ensureAuthProfileStore,
listProfilesForProvider,
} from "../../agents/auth-profiles.js";
import {
getCustomProviderApiKey,
resolveEnvApiKey,
} from "../../agents/model-auth.js";
import crypto from "node:crypto";
import { resolveModelAuthMode } from "../../agents/model-auth.js";
import { normalizeProviderId } from "../../agents/model-selection.js";
import {
abortEmbeddedPiRun,
@@ -55,8 +49,10 @@ import type {
ElevatedLevel,
ReasoningLevel,
ThinkLevel,
UsageDisplayLevel,
VerboseLevel,
} from "../thinking.js";
import { normalizeUsageDisplay } from "../thinking.js";
import type { ReplyPayload } from "../types.js";
import { isAbortTrigger, setAbortMemory } from "./abort.js";
import type { InlineDirectives } from "./directive-handling.js";
@@ -109,36 +105,6 @@ export type CommandContext = {
to?: string;
};
function resolveModelAuthLabel(
provider?: string,
cfg?: ClawdbotConfig,
): string | undefined {
const resolved = provider?.trim();
if (!resolved) return undefined;
const store = ensureAuthProfileStore();
const profiles = listProfilesForProvider(store, resolved);
if (profiles.length > 0) {
const modes = new Set(
profiles
.map((id) => store.profiles[id]?.type)
.filter((mode): mode is "api_key" | "oauth" => Boolean(mode)),
);
if (modes.has("oauth") && modes.has("api_key")) return "mixed";
if (modes.has("oauth")) return "oauth";
if (modes.has("api_key")) return "api-key";
}
const envKey = resolveEnvApiKey(resolved);
if (envKey?.apiKey) {
return envKey.source.includes("OAUTH_TOKEN") ? "oauth" : "api-key";
}
if (getCustomProviderApiKey(cfg, resolved)) return "api-key";
return "unknown";
}
function extractCompactInstructions(params: {
rawBody?: string;
ctx: MsgContext;
@@ -468,6 +434,7 @@ export async function handleCommands(params: {
defaultGroupActivation())
: undefined;
const statusText = buildStatusMessage({
config: cfg,
agent: {
...cfg.agent,
model: {
@@ -488,7 +455,7 @@ export async function handleCommands(params: {
resolvedVerbose: resolvedVerboseLevel,
resolvedReasoning: resolvedReasoningLevel,
resolvedElevated: resolvedElevatedLevel,
modelAuth: resolveModelAuthLabel(provider, cfg),
modelAuth: resolveModelAuthMode(provider, cfg),
usageLine: usageLine ?? undefined,
queue: {
mode: queueSettings.mode,
@@ -503,6 +470,51 @@ export async function handleCommands(params: {
return { shouldContinue: false, reply: { text: statusText } };
}
const costRequested =
command.commandBodyNormalized === "/cost" ||
command.commandBodyNormalized.startsWith("/cost ");
if (allowTextCommands && costRequested) {
if (!command.isAuthorizedSender) {
logVerbose(
`Ignoring /cost from unauthorized sender: ${command.senderE164 || "<unknown>"}`,
);
return { shouldContinue: false };
}
const rawArgs = command.commandBodyNormalized.slice("/cost".length).trim();
const normalized =
rawArgs.length > 0 ? normalizeUsageDisplay(rawArgs) : undefined;
if (rawArgs.length > 0 && !normalized) {
return {
shouldContinue: false,
reply: { text: "⚙️ Usage: /cost on|off" },
};
}
const current: UsageDisplayLevel =
sessionEntry?.responseUsage === "on" ? "on" : "off";
const next = normalized ?? (current === "on" ? "off" : "on");
if (sessionStore && sessionKey) {
const entry = sessionEntry ??
sessionStore[sessionKey] ?? {
sessionId: crypto.randomUUID(),
updatedAt: Date.now(),
};
if (next === "off") delete entry.responseUsage;
else entry.responseUsage = next;
entry.updatedAt = Date.now();
sessionStore[sessionKey] = entry;
if (storePath) {
await saveSessionStore(storePath, sessionStore);
}
}
return {
shouldContinue: false,
reply: {
text:
next === "on" ? "⚙️ Usage line enabled." : "⚙️ Usage line disabled.",
},
};
}
const stopRequested = command.commandBodyNormalized === "/stop";
if (allowTextCommands && stopRequested) {
if (!command.isAuthorizedSender) {