feat: add usage cost reporting
This commit is contained in:
@@ -27,6 +27,13 @@ const CHAT_COMMANDS: ChatCommandDefinition[] = [
|
||||
description: "Show current status.",
|
||||
textAliases: ["/status"],
|
||||
},
|
||||
{
|
||||
key: "cost",
|
||||
nativeName: "cost",
|
||||
description: "Toggle per-response usage line.",
|
||||
textAliases: ["/cost"],
|
||||
acceptsArgs: true,
|
||||
},
|
||||
{
|
||||
key: "stop",
|
||||
nativeName: "stop",
|
||||
|
||||
@@ -212,7 +212,7 @@ describe("trigger handling", () => {
|
||||
makeCfg(home),
|
||||
);
|
||||
const text = Array.isArray(res) ? res[0]?.text : res?.text;
|
||||
expect(text).toContain("ClawdBot");
|
||||
expect(text).toContain("status");
|
||||
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,12 +2,13 @@ import crypto from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import { lookupContextTokens } from "../../agents/context.js";
|
||||
import { DEFAULT_CONTEXT_TOKENS } from "../../agents/defaults.js";
|
||||
import { resolveModelAuthMode } from "../../agents/model-auth.js";
|
||||
import { runWithModelFallback } from "../../agents/model-fallback.js";
|
||||
import {
|
||||
queueEmbeddedPiMessage,
|
||||
runEmbeddedPiAgent,
|
||||
} from "../../agents/pi-embedded.js";
|
||||
import { hasNonzeroUsage } from "../../agents/usage.js";
|
||||
import { hasNonzeroUsage, type NormalizedUsage } from "../../agents/usage.js";
|
||||
import {
|
||||
loadSessionStore,
|
||||
resolveSessionTranscriptPath,
|
||||
@@ -18,6 +19,12 @@ import type { TypingMode } from "../../config/types.js";
|
||||
import { logVerbose } from "../../globals.js";
|
||||
import { registerAgentRunContext } from "../../infra/agent-events.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import {
|
||||
estimateUsageCost,
|
||||
formatTokenCount,
|
||||
formatUsd,
|
||||
resolveModelCostConfig,
|
||||
} from "../../utils/usage-format.js";
|
||||
import { stripHeartbeatToken } from "../heartbeat.js";
|
||||
import type { OriginatingChannelType, TemplateContext } from "../templating.js";
|
||||
import { normalizeVerboseLevel, type VerboseLevel } from "../thinking.js";
|
||||
@@ -62,6 +69,65 @@ const formatBunFetchSocketError = (message: string) => {
|
||||
].join("\n");
|
||||
};
|
||||
|
||||
const formatResponseUsageLine = (params: {
|
||||
usage?: NormalizedUsage;
|
||||
showCost: boolean;
|
||||
costConfig?: {
|
||||
input: number;
|
||||
output: number;
|
||||
cacheRead: number;
|
||||
cacheWrite: number;
|
||||
};
|
||||
}): string | null => {
|
||||
const usage = params.usage;
|
||||
if (!usage) return null;
|
||||
const input = usage.input;
|
||||
const output = usage.output;
|
||||
if (typeof input !== "number" && typeof output !== "number") return null;
|
||||
const inputLabel = typeof input === "number" ? formatTokenCount(input) : "?";
|
||||
const outputLabel =
|
||||
typeof output === "number" ? formatTokenCount(output) : "?";
|
||||
const cost =
|
||||
params.showCost && typeof input === "number" && typeof output === "number"
|
||||
? estimateUsageCost({
|
||||
usage: {
|
||||
input,
|
||||
output,
|
||||
cacheRead: usage.cacheRead,
|
||||
cacheWrite: usage.cacheWrite,
|
||||
},
|
||||
cost: params.costConfig,
|
||||
})
|
||||
: undefined;
|
||||
const costLabel = params.showCost ? formatUsd(cost) : undefined;
|
||||
const suffix = costLabel ? ` · est ${costLabel}` : "";
|
||||
return `Usage: ${inputLabel} in / ${outputLabel} out${suffix}`;
|
||||
};
|
||||
|
||||
const appendUsageLine = (
|
||||
payloads: ReplyPayload[],
|
||||
line: string,
|
||||
): ReplyPayload[] => {
|
||||
let index = -1;
|
||||
for (let i = payloads.length - 1; i >= 0; i -= 1) {
|
||||
if (payloads[i]?.text) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index === -1) return [...payloads, { text: line }];
|
||||
const existing = payloads[index];
|
||||
const existingText = existing.text ?? "";
|
||||
const separator = existingText.endsWith("\n") ? "" : "\n";
|
||||
const next = {
|
||||
...existing,
|
||||
text: `${existingText}${separator}${line}`,
|
||||
};
|
||||
const updated = payloads.slice();
|
||||
updated[index] = next;
|
||||
return updated;
|
||||
};
|
||||
|
||||
const withTimeout = async <T>(
|
||||
promise: Promise<T>,
|
||||
timeoutMs: number,
|
||||
@@ -191,6 +257,7 @@ export async function runReplyAgent(params: {
|
||||
replyToChannel,
|
||||
);
|
||||
const applyReplyToMode = createReplyToModeFilter(replyToMode);
|
||||
const cfg = followupRun.run.config;
|
||||
|
||||
if (shouldSteer && isStreaming) {
|
||||
const steered = queueEmbeddedPiMessage(
|
||||
@@ -242,6 +309,7 @@ export async function runReplyAgent(params: {
|
||||
|
||||
let didLogHeartbeatStrip = false;
|
||||
let autoCompactionCompleted = false;
|
||||
let responseUsageLine: string | undefined;
|
||||
try {
|
||||
const runId = crypto.randomUUID();
|
||||
if (sessionKey) {
|
||||
@@ -641,20 +709,20 @@ export async function runReplyAgent(params: {
|
||||
await typingSignals.signalRunStart();
|
||||
}
|
||||
|
||||
if (sessionStore && sessionKey) {
|
||||
const usage = runResult.meta.agentMeta?.usage;
|
||||
const modelUsed =
|
||||
runResult.meta.agentMeta?.model ?? fallbackModel ?? defaultModel;
|
||||
const providerUsed =
|
||||
runResult.meta.agentMeta?.provider ??
|
||||
fallbackProvider ??
|
||||
followupRun.run.provider;
|
||||
const contextTokensUsed =
|
||||
agentCfgContextTokens ??
|
||||
lookupContextTokens(modelUsed) ??
|
||||
sessionEntry?.contextTokens ??
|
||||
DEFAULT_CONTEXT_TOKENS;
|
||||
const usage = runResult.meta.agentMeta?.usage;
|
||||
const modelUsed =
|
||||
runResult.meta.agentMeta?.model ?? fallbackModel ?? defaultModel;
|
||||
const providerUsed =
|
||||
runResult.meta.agentMeta?.provider ??
|
||||
fallbackProvider ??
|
||||
followupRun.run.provider;
|
||||
const contextTokensUsed =
|
||||
agentCfgContextTokens ??
|
||||
lookupContextTokens(modelUsed) ??
|
||||
sessionEntry?.contextTokens ??
|
||||
DEFAULT_CONTEXT_TOKENS;
|
||||
|
||||
if (sessionStore && sessionKey) {
|
||||
if (hasNonzeroUsage(usage)) {
|
||||
const entry = sessionEntry ?? sessionStore[sessionKey];
|
||||
if (entry) {
|
||||
@@ -694,6 +762,29 @@ export async function runReplyAgent(params: {
|
||||
}
|
||||
}
|
||||
|
||||
const responseUsageEnabled =
|
||||
(sessionEntry?.responseUsage ??
|
||||
(sessionKey
|
||||
? sessionStore?.[sessionKey]?.responseUsage
|
||||
: undefined)) === "on";
|
||||
if (responseUsageEnabled && hasNonzeroUsage(usage)) {
|
||||
const authMode = resolveModelAuthMode(providerUsed, cfg);
|
||||
const showCost = authMode === "api-key";
|
||||
const costConfig = showCost
|
||||
? resolveModelCostConfig({
|
||||
provider: providerUsed,
|
||||
model: modelUsed,
|
||||
config: cfg,
|
||||
})
|
||||
: undefined;
|
||||
const formatted = formatResponseUsageLine({
|
||||
usage,
|
||||
showCost,
|
||||
costConfig,
|
||||
});
|
||||
if (formatted) responseUsageLine = formatted;
|
||||
}
|
||||
|
||||
// If verbose is enabled and this is a new session, prepend a session hint.
|
||||
let finalPayloads = replyPayloads;
|
||||
if (autoCompactionCompleted) {
|
||||
@@ -717,6 +808,9 @@ export async function runReplyAgent(params: {
|
||||
...finalPayloads,
|
||||
];
|
||||
}
|
||||
if (responseUsageLine) {
|
||||
finalPayloads = appendUsageLine(finalPayloads, responseUsageLine);
|
||||
}
|
||||
|
||||
return finalizeWithFollowup(
|
||||
finalPayloads.length === 1 ? finalPayloads[0] : finalPayloads,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -194,6 +194,7 @@ export async function initSessionState(params: {
|
||||
// Persist previously stored thinking/verbose levels when present.
|
||||
thinkingLevel: persistedThinking ?? baseEntry?.thinkingLevel,
|
||||
verboseLevel: persistedVerbose ?? baseEntry?.verboseLevel,
|
||||
responseUsage: baseEntry?.responseUsage,
|
||||
modelOverride: persistedModelOverride ?? baseEntry?.modelOverride,
|
||||
providerOverride: persistedProviderOverride ?? baseEntry?.providerOverride,
|
||||
sendPolicy: baseEntry?.sendPolicy,
|
||||
|
||||
@@ -63,20 +63,18 @@ describe("buildStatusMessage", () => {
|
||||
resolvedThink: "medium",
|
||||
resolvedVerbose: "off",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
now: 10 * 60_000, // 10 minutes later
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("🦞 ClawdBot");
|
||||
expect(text).toContain("🧠 Model:");
|
||||
expect(text).toContain("Runtime: direct");
|
||||
expect(text).toContain("Context: 16k/32k (50%)");
|
||||
expect(text).toContain("🧹 Compactions: 2");
|
||||
expect(text).toContain("Session: agent:main:main");
|
||||
expect(text).toContain("updated 10m ago");
|
||||
expect(text).toContain("Think: medium");
|
||||
expect(text).not.toContain("Verbose");
|
||||
expect(text).toContain("Elevated");
|
||||
expect(text).toContain("Queue: collect");
|
||||
expect(text).toContain("status agent:main:main");
|
||||
expect(text).toContain("model anthropic/pi:opus (api-key)");
|
||||
expect(text).toContain("Context 16k/32k (50%)");
|
||||
expect(text).toContain("compactions 2");
|
||||
expect(text).toContain("think medium");
|
||||
expect(text).toContain("verbose off");
|
||||
expect(text).toContain("reasoning off");
|
||||
expect(text).toContain("elevated on");
|
||||
expect(text).toContain("queue collect");
|
||||
});
|
||||
|
||||
it("shows verbose/elevated labels only when enabled", () => {
|
||||
@@ -91,10 +89,8 @@ describe("buildStatusMessage", () => {
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
});
|
||||
|
||||
expect(text).toContain("Verbose");
|
||||
expect(text).toContain("Elevated");
|
||||
expect(text).not.toContain("Verbose:");
|
||||
expect(text).not.toContain("Elevated:");
|
||||
expect(text).toContain("verbose on");
|
||||
expect(text).toContain("elevated on");
|
||||
});
|
||||
|
||||
it("prefers model overrides over last-run model", () => {
|
||||
@@ -115,9 +111,10 @@ describe("buildStatusMessage", () => {
|
||||
sessionKey: "agent:main:main",
|
||||
sessionScope: "per-sender",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("🧠 Model: openai/gpt-4.1-mini");
|
||||
expect(text).toContain("model openai/gpt-4.1-mini");
|
||||
});
|
||||
|
||||
it("keeps provider prefix from configured model", () => {
|
||||
@@ -127,21 +124,23 @@ describe("buildStatusMessage", () => {
|
||||
},
|
||||
sessionScope: "per-sender",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("🧠 Model: google-antigravity/claude-sonnet-4-5");
|
||||
expect(text).toContain("model google-antigravity/claude-sonnet-4-5");
|
||||
});
|
||||
|
||||
it("handles missing agent config gracefully", () => {
|
||||
const text = buildStatusMessage({
|
||||
agent: {},
|
||||
sessionScope: "per-sender",
|
||||
webLinked: false,
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("🧠 Model:");
|
||||
expect(text).toContain("Context:");
|
||||
expect(text).toContain("Queue:");
|
||||
expect(text).toContain("model");
|
||||
expect(text).toContain("Context");
|
||||
expect(text).toContain("queue collect");
|
||||
});
|
||||
|
||||
it("includes group activation for group sessions", () => {
|
||||
@@ -156,9 +155,10 @@ describe("buildStatusMessage", () => {
|
||||
sessionKey: "agent:main:whatsapp:group:123@g.us",
|
||||
sessionScope: "per-sender",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("Activation: always");
|
||||
expect(text).toContain("activation always");
|
||||
});
|
||||
|
||||
it("shows queue details when overridden", () => {
|
||||
@@ -175,10 +175,11 @@ describe("buildStatusMessage", () => {
|
||||
dropPolicy: "old",
|
||||
showDetails: true,
|
||||
},
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain(
|
||||
"Queue: collect (depth 3 · debounce 2s · cap 5 · drop old)",
|
||||
"queue collect (depth 3 · debounce 2s · cap 5 · drop old)",
|
||||
);
|
||||
});
|
||||
|
||||
@@ -190,12 +191,10 @@ describe("buildStatusMessage", () => {
|
||||
sessionScope: "per-sender",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
usageLine: "📊 Usage: Claude 80% left (5h)",
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
const lines = text.split("\n");
|
||||
const contextIndex = lines.findIndex((line) => line.startsWith("📚 "));
|
||||
expect(contextIndex).toBeGreaterThan(-1);
|
||||
expect(lines[contextIndex + 1]).toBe("📊 Usage: Claude 80% left (5h)");
|
||||
expect(text).toContain("📊 Usage: Claude 80% left (5h)");
|
||||
});
|
||||
|
||||
it("prefers cached prompt tokens from the session log", async () => {
|
||||
@@ -255,9 +254,10 @@ describe("buildStatusMessage", () => {
|
||||
sessionScope: "per-sender",
|
||||
queue: { mode: "collect", depth: 0 },
|
||||
includeTranscriptUsage: true,
|
||||
modelAuth: "api-key",
|
||||
});
|
||||
|
||||
expect(text).toContain("Context: 1.0k/32k");
|
||||
expect(text).toContain("Context 1.0k/32k");
|
||||
} finally {
|
||||
restoreHomeEnv(previousHome);
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
DEFAULT_MODEL,
|
||||
DEFAULT_PROVIDER,
|
||||
} from "../agents/defaults.js";
|
||||
import { resolveModelAuthMode } from "../agents/model-auth.js";
|
||||
import { resolveConfiguredModelRef } from "../agents/model-selection.js";
|
||||
import {
|
||||
derivePromptTokens,
|
||||
@@ -14,13 +15,16 @@ import {
|
||||
} from "../agents/usage.js";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import {
|
||||
resolveMainSessionKey,
|
||||
resolveSessionFilePath,
|
||||
type SessionEntry,
|
||||
type SessionScope,
|
||||
} from "../config/sessions.js";
|
||||
import { resolveCommitHash } from "../infra/git-commit.js";
|
||||
import { VERSION } from "../version.js";
|
||||
import {
|
||||
estimateUsageCost,
|
||||
formatTokenCount as formatTokenCountShared,
|
||||
formatUsd,
|
||||
resolveModelCostConfig,
|
||||
} from "../utils/usage-format.js";
|
||||
import type {
|
||||
ElevatedLevel,
|
||||
ReasoningLevel,
|
||||
@@ -30,6 +34,8 @@ import type {
|
||||
|
||||
type AgentConfig = NonNullable<ClawdbotConfig["agent"]>;
|
||||
|
||||
export const formatTokenCount = formatTokenCountShared;
|
||||
|
||||
type QueueStatus = {
|
||||
mode?: string;
|
||||
depth?: number;
|
||||
@@ -40,6 +46,7 @@ type QueueStatus = {
|
||||
};
|
||||
|
||||
type StatusArgs = {
|
||||
config?: ClawdbotConfig;
|
||||
agent: AgentConfig;
|
||||
sessionEntry?: SessionEntry;
|
||||
sessionKey?: string;
|
||||
@@ -53,37 +60,20 @@ type StatusArgs = {
|
||||
usageLine?: string;
|
||||
queue?: QueueStatus;
|
||||
includeTranscriptUsage?: boolean;
|
||||
now?: number;
|
||||
};
|
||||
|
||||
const formatAge = (ms?: number | null) => {
|
||||
if (!ms || ms < 0) return "unknown";
|
||||
const minutes = Math.round(ms / 60_000);
|
||||
if (minutes < 1) return "just now";
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
const hours = Math.round(minutes / 60);
|
||||
if (hours < 48) return `${hours}h ago`;
|
||||
const days = Math.round(hours / 24);
|
||||
return `${days}d ago`;
|
||||
};
|
||||
|
||||
const formatKTokens = (value: number) =>
|
||||
`${(value / 1000).toFixed(value >= 10_000 ? 0 : 1)}k`;
|
||||
|
||||
export const formatTokenCount = (value: number) => formatKTokens(value);
|
||||
|
||||
const formatTokens = (
|
||||
total: number | null | undefined,
|
||||
contextTokens: number | null,
|
||||
) => {
|
||||
const ctx = contextTokens ?? null;
|
||||
if (total == null) {
|
||||
const ctxLabel = ctx ? formatKTokens(ctx) : "?";
|
||||
return `unknown/${ctxLabel}`;
|
||||
const ctxLabel = ctx ? formatTokenCount(ctx) : "?";
|
||||
return `?/${ctxLabel}`;
|
||||
}
|
||||
const pct = ctx ? Math.min(999, Math.round((total / ctx) * 100)) : null;
|
||||
const totalLabel = formatKTokens(total);
|
||||
const ctxLabel = ctx ? formatKTokens(ctx) : "?";
|
||||
const totalLabel = formatTokenCount(total);
|
||||
const ctxLabel = ctx ? formatTokenCount(ctx) : "?";
|
||||
return `${totalLabel}/${ctxLabel}${pct !== null ? ` (${pct}%)` : ""}`;
|
||||
};
|
||||
|
||||
@@ -171,8 +161,15 @@ const readUsageFromSessionLog = (
|
||||
}
|
||||
};
|
||||
|
||||
const formatUsagePair = (input?: number | null, output?: number | null) => {
|
||||
if (input == null && output == null) return null;
|
||||
const inputLabel = typeof input === "number" ? formatTokenCount(input) : "?";
|
||||
const outputLabel =
|
||||
typeof output === "number" ? formatTokenCount(output) : "?";
|
||||
return `usage ${inputLabel} in / ${outputLabel} out`;
|
||||
};
|
||||
|
||||
export function buildStatusMessage(args: StatusArgs): string {
|
||||
const now = args.now ?? Date.now();
|
||||
const entry = args.sessionEntry;
|
||||
const resolved = resolveConfiguredModelRef({
|
||||
cfg: { agent: args.agent ?? {} },
|
||||
@@ -188,6 +185,8 @@ export function buildStatusMessage(args: StatusArgs): string {
|
||||
lookupContextTokens(model) ??
|
||||
DEFAULT_CONTEXT_TOKENS;
|
||||
|
||||
let inputTokens = entry?.inputTokens;
|
||||
let outputTokens = entry?.outputTokens;
|
||||
let totalTokens =
|
||||
entry?.totalTokens ??
|
||||
(entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
|
||||
@@ -205,6 +204,8 @@ export function buildStatusMessage(args: StatusArgs): string {
|
||||
if (!contextTokens && logUsage.model) {
|
||||
contextTokens = lookupContextTokens(logUsage.model) ?? contextTokens;
|
||||
}
|
||||
if (!inputTokens || inputTokens === 0) inputTokens = logUsage.input;
|
||||
if (!outputTokens || outputTokens === 0) outputTokens = logUsage.output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,33 +219,6 @@ export function buildStatusMessage(args: StatusArgs): string {
|
||||
args.agent?.elevatedDefault ??
|
||||
"on";
|
||||
|
||||
const runtime = (() => {
|
||||
const sandboxMode = args.agent?.sandbox?.mode ?? "off";
|
||||
if (sandboxMode === "off") return { label: "direct" };
|
||||
const sessionScope = args.sessionScope ?? "per-sender";
|
||||
const mainKey = resolveMainSessionKey({
|
||||
session: { scope: sessionScope },
|
||||
});
|
||||
const sessionKey = args.sessionKey?.trim();
|
||||
const sandboxed = sessionKey
|
||||
? sandboxMode === "all" || sessionKey !== mainKey.trim()
|
||||
: false;
|
||||
const runtime = sandboxed ? "docker" : sessionKey ? "direct" : "unknown";
|
||||
return {
|
||||
label: `${runtime}/${sandboxMode}`,
|
||||
};
|
||||
})();
|
||||
|
||||
const updatedAt = entry?.updatedAt;
|
||||
const sessionLine = [
|
||||
`Session: ${args.sessionKey ?? "unknown"}`,
|
||||
typeof updatedAt === "number"
|
||||
? `updated ${formatAge(now - updatedAt)}`
|
||||
: "no activity",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" • ");
|
||||
|
||||
const isGroupSession =
|
||||
entry?.chatType === "group" ||
|
||||
entry?.chatType === "room" ||
|
||||
@@ -255,52 +229,66 @@ export function buildStatusMessage(args: StatusArgs): string {
|
||||
? (args.groupActivation ?? entry?.groupActivation ?? "mention")
|
||||
: undefined;
|
||||
|
||||
const contextLine = [
|
||||
`Context: ${formatTokens(totalTokens, contextTokens ?? null)}`,
|
||||
`🧹 Compactions: ${entry?.compactionCount ?? 0}`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" · ");
|
||||
const authMode =
|
||||
args.modelAuth ?? resolveModelAuthMode(provider, args.config);
|
||||
const showCost = authMode === "api-key";
|
||||
const costConfig = showCost
|
||||
? resolveModelCostConfig({
|
||||
provider,
|
||||
model,
|
||||
config: args.config,
|
||||
})
|
||||
: undefined;
|
||||
const hasUsage =
|
||||
typeof inputTokens === "number" || typeof outputTokens === "number";
|
||||
const cost =
|
||||
showCost && hasUsage
|
||||
? estimateUsageCost({
|
||||
usage: {
|
||||
input: inputTokens ?? undefined,
|
||||
output: outputTokens ?? undefined,
|
||||
},
|
||||
cost: costConfig,
|
||||
})
|
||||
: undefined;
|
||||
const costLabel = showCost && hasUsage ? formatUsd(cost) : undefined;
|
||||
|
||||
const parts: Array<string | null> = [];
|
||||
parts.push(`status ${args.sessionKey ?? "unknown"}`);
|
||||
|
||||
const modelLabel = model ? `${provider}/${model}` : "unknown";
|
||||
const authLabel = authMode && authMode !== "unknown" ? ` (${authMode})` : "";
|
||||
parts.push(`model ${modelLabel}${authLabel}`);
|
||||
|
||||
const usagePair = formatUsagePair(inputTokens, outputTokens);
|
||||
if (usagePair) parts.push(usagePair);
|
||||
if (costLabel) parts.push(`cost ${costLabel}`);
|
||||
|
||||
const contextSummary = formatContextUsageShort(
|
||||
totalTokens && totalTokens > 0 ? totalTokens : null,
|
||||
contextTokens ?? null,
|
||||
);
|
||||
parts.push(contextSummary);
|
||||
parts.push(`compactions ${entry?.compactionCount ?? 0}`);
|
||||
parts.push(`think ${thinkLevel}`);
|
||||
parts.push(`verbose ${verboseLevel}`);
|
||||
parts.push(`reasoning ${reasoningLevel}`);
|
||||
parts.push(`elevated ${elevatedLevel}`);
|
||||
if (groupActivationValue) parts.push(`activation ${groupActivationValue}`);
|
||||
|
||||
const queueMode = args.queue?.mode ?? "unknown";
|
||||
const queueDetails = formatQueueDetails(args.queue);
|
||||
const optionParts = [
|
||||
`Runtime: ${runtime.label}`,
|
||||
`Think: ${thinkLevel}`,
|
||||
verboseLevel === "on" ? "Verbose" : null,
|
||||
reasoningLevel !== "off" ? `Reasoning: ${reasoningLevel}` : null,
|
||||
elevatedLevel === "on" ? "Elevated" : null,
|
||||
];
|
||||
const optionsLine = optionParts.filter(Boolean).join(" · ");
|
||||
const activationParts = [
|
||||
groupActivationValue ? `👥 Activation: ${groupActivationValue}` : null,
|
||||
`🪢 Queue: ${queueMode}${queueDetails}`,
|
||||
];
|
||||
const activationLine = activationParts.filter(Boolean).join(" · ");
|
||||
parts.push(`queue ${queueMode}${queueDetails}`);
|
||||
|
||||
const modelLabel = model ? `${provider}/${model}` : "unknown";
|
||||
const authLabel = args.modelAuth ? ` · 🔑 ${args.modelAuth}` : "";
|
||||
const modelLine = `🧠 Model: ${modelLabel}${authLabel}`;
|
||||
const commit = resolveCommitHash();
|
||||
const versionLine = `🦞 ClawdBot ${VERSION}${commit ? ` (${commit})` : ""}`;
|
||||
if (args.usageLine) parts.push(args.usageLine);
|
||||
|
||||
return [
|
||||
versionLine,
|
||||
modelLine,
|
||||
`📚 ${contextLine}`,
|
||||
args.usageLine,
|
||||
`🧵 ${sessionLine}`,
|
||||
`⚙️ ${optionsLine}`,
|
||||
activationLine,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
return parts.filter(Boolean).join(" · ");
|
||||
}
|
||||
|
||||
export function buildHelpMessage(): string {
|
||||
return [
|
||||
"ℹ️ Help",
|
||||
"Shortcuts: /new reset | /compact [instructions] | /restart relink",
|
||||
"Options: /think <level> | /verbose on|off | /reasoning on|off | /elevated on|off | /model <id>",
|
||||
"Options: /think <level> | /verbose on|off | /reasoning on|off | /elevated on|off | /model <id> | /cost on|off",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ export type ThinkLevel = "off" | "minimal" | "low" | "medium" | "high";
|
||||
export type VerboseLevel = "off" | "on";
|
||||
export type ElevatedLevel = "off" | "on";
|
||||
export type ReasoningLevel = "off" | "on" | "stream";
|
||||
export type UsageDisplayLevel = "off" | "on";
|
||||
|
||||
// Normalize user-provided thinking level strings to the canonical enum.
|
||||
export function normalizeThinkLevel(
|
||||
@@ -46,6 +47,19 @@ export function normalizeVerboseLevel(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Normalize response-usage display flags used to toggle cost/token lines.
|
||||
export function normalizeUsageDisplay(
|
||||
raw?: string | null,
|
||||
): UsageDisplayLevel | undefined {
|
||||
if (!raw) return undefined;
|
||||
const key = raw.toLowerCase();
|
||||
if (["off", "false", "no", "0", "disable", "disabled"].includes(key))
|
||||
return "off";
|
||||
if (["on", "true", "yes", "1", "enable", "enabled"].includes(key))
|
||||
return "on";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Normalize elevated flags used to toggle elevated bash permissions.
|
||||
export function normalizeElevatedLevel(
|
||||
raw?: string | null,
|
||||
|
||||
Reference in New Issue
Block a user