mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-01 18:48:16 +00:00
refactor(subagents): share token usage formatting
This commit is contained in:
@@ -14,7 +14,8 @@ import {
|
|||||||
} from "../../routing/session-key.js";
|
} from "../../routing/session-key.js";
|
||||||
import {
|
import {
|
||||||
formatDurationCompact,
|
formatDurationCompact,
|
||||||
formatTokenShort,
|
formatTokenUsageDisplay,
|
||||||
|
resolveTotalTokens,
|
||||||
truncateLine,
|
truncateLine,
|
||||||
} from "../../shared/subagents-format.js";
|
} from "../../shared/subagents-format.js";
|
||||||
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
||||||
@@ -138,55 +139,6 @@ function resolveModelDisplay(entry?: SessionEntry, fallbackModel?: string) {
|
|||||||
return modelRef;
|
return modelRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveTotalTokens(entry?: SessionEntry) {
|
|
||||||
if (!entry) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (typeof entry.totalTokens === "number" && Number.isFinite(entry.totalTokens)) {
|
|
||||||
return entry.totalTokens;
|
|
||||||
}
|
|
||||||
const input = typeof entry.inputTokens === "number" ? entry.inputTokens : 0;
|
|
||||||
const output = typeof entry.outputTokens === "number" ? entry.outputTokens : 0;
|
|
||||||
const total = input + output;
|
|
||||||
return total > 0 ? total : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveIoTokens(entry?: SessionEntry) {
|
|
||||||
if (!entry) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const input =
|
|
||||||
typeof entry.inputTokens === "number" && Number.isFinite(entry.inputTokens)
|
|
||||||
? entry.inputTokens
|
|
||||||
: 0;
|
|
||||||
const output =
|
|
||||||
typeof entry.outputTokens === "number" && Number.isFinite(entry.outputTokens)
|
|
||||||
? entry.outputTokens
|
|
||||||
: 0;
|
|
||||||
const total = input + output;
|
|
||||||
if (total <= 0) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return { input, output, total };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveUsageDisplay(entry?: SessionEntry) {
|
|
||||||
const io = resolveIoTokens(entry);
|
|
||||||
const promptCache = resolveTotalTokens(entry);
|
|
||||||
const parts: string[] = [];
|
|
||||||
if (io) {
|
|
||||||
const input = formatTokenShort(io.input) ?? "0";
|
|
||||||
const output = formatTokenShort(io.output) ?? "0";
|
|
||||||
parts.push(`tokens ${formatTokenShort(io.total)} (in ${input} / out ${output})`);
|
|
||||||
} else if (typeof promptCache === "number" && promptCache > 0) {
|
|
||||||
parts.push(`tokens ${formatTokenShort(promptCache)} prompt/cache`);
|
|
||||||
}
|
|
||||||
if (typeof promptCache === "number" && io && promptCache > io.total) {
|
|
||||||
parts.push(`prompt/cache ${formatTokenShort(promptCache)}`);
|
|
||||||
}
|
|
||||||
return parts.join(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveSubagentTarget(
|
function resolveSubagentTarget(
|
||||||
runs: SubagentRunRecord[],
|
runs: SubagentRunRecord[],
|
||||||
token: string | undefined,
|
token: string | undefined,
|
||||||
@@ -470,7 +422,7 @@ export function createSubagentsTool(opts?: { agentSessionKey?: string }): AnyAge
|
|||||||
cache,
|
cache,
|
||||||
}).entry;
|
}).entry;
|
||||||
const totalTokens = resolveTotalTokens(sessionEntry);
|
const totalTokens = resolveTotalTokens(sessionEntry);
|
||||||
const usageText = resolveUsageDisplay(sessionEntry);
|
const usageText = formatTokenUsageDisplay(sessionEntry);
|
||||||
const status = resolveRunStatus(entry);
|
const status = resolveRunStatus(entry);
|
||||||
const runtime = formatDurationCompact(now - (entry.startedAt ?? entry.createdAt));
|
const runtime = formatDurationCompact(now - (entry.startedAt ?? entry.createdAt));
|
||||||
const label = truncateLine(resolveRunLabel(entry), 48);
|
const label = truncateLine(resolveRunLabel(entry), 48);
|
||||||
@@ -501,7 +453,7 @@ export function createSubagentsTool(opts?: { agentSessionKey?: string }): AnyAge
|
|||||||
cache,
|
cache,
|
||||||
}).entry;
|
}).entry;
|
||||||
const totalTokens = resolveTotalTokens(sessionEntry);
|
const totalTokens = resolveTotalTokens(sessionEntry);
|
||||||
const usageText = resolveUsageDisplay(sessionEntry);
|
const usageText = formatTokenUsageDisplay(sessionEntry);
|
||||||
const status = resolveRunStatus(entry);
|
const status = resolveRunStatus(entry);
|
||||||
const runtime = formatDurationCompact(
|
const runtime = formatDurationCompact(
|
||||||
(entry.endedAt ?? now) - (entry.startedAt ?? entry.createdAt),
|
(entry.endedAt ?? now) - (entry.startedAt ?? entry.createdAt),
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import { formatTimeAgo } from "../../infra/format-time/format-relative.ts";
|
|||||||
import { parseAgentSessionKey } from "../../routing/session-key.js";
|
import { parseAgentSessionKey } from "../../routing/session-key.js";
|
||||||
import {
|
import {
|
||||||
formatDurationCompact,
|
formatDurationCompact,
|
||||||
formatTokenShort,
|
formatTokenUsageDisplay,
|
||||||
truncateLine,
|
truncateLine,
|
||||||
} from "../../shared/subagents-format.js";
|
} from "../../shared/subagents-format.js";
|
||||||
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
||||||
@@ -97,63 +97,6 @@ function resolveModelDisplay(
|
|||||||
return combined;
|
return combined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveTotalTokens(entry?: {
|
|
||||||
totalTokens?: unknown;
|
|
||||||
inputTokens?: unknown;
|
|
||||||
outputTokens?: unknown;
|
|
||||||
}) {
|
|
||||||
if (!entry || typeof entry !== "object") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (typeof entry.totalTokens === "number" && Number.isFinite(entry.totalTokens)) {
|
|
||||||
return entry.totalTokens;
|
|
||||||
}
|
|
||||||
const input = typeof entry.inputTokens === "number" ? entry.inputTokens : 0;
|
|
||||||
const output = typeof entry.outputTokens === "number" ? entry.outputTokens : 0;
|
|
||||||
const total = input + output;
|
|
||||||
return total > 0 ? total : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveIoTokens(entry?: { inputTokens?: unknown; outputTokens?: unknown }) {
|
|
||||||
if (!entry || typeof entry !== "object") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const input =
|
|
||||||
typeof entry.inputTokens === "number" && Number.isFinite(entry.inputTokens)
|
|
||||||
? entry.inputTokens
|
|
||||||
: 0;
|
|
||||||
const output =
|
|
||||||
typeof entry.outputTokens === "number" && Number.isFinite(entry.outputTokens)
|
|
||||||
? entry.outputTokens
|
|
||||||
: 0;
|
|
||||||
const total = input + output;
|
|
||||||
if (total <= 0) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return { input, output, total };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveUsageDisplay(entry?: {
|
|
||||||
totalTokens?: unknown;
|
|
||||||
inputTokens?: unknown;
|
|
||||||
outputTokens?: unknown;
|
|
||||||
}) {
|
|
||||||
const io = resolveIoTokens(entry);
|
|
||||||
const promptCache = resolveTotalTokens(entry);
|
|
||||||
const parts: string[] = [];
|
|
||||||
if (io) {
|
|
||||||
const input = formatTokenShort(io.input) ?? "0";
|
|
||||||
const output = formatTokenShort(io.output) ?? "0";
|
|
||||||
parts.push(`tokens ${formatTokenShort(io.total)} (in ${input} / out ${output})`);
|
|
||||||
} else if (typeof promptCache === "number" && promptCache > 0) {
|
|
||||||
parts.push(`tokens ${formatTokenShort(promptCache)} prompt/cache`);
|
|
||||||
}
|
|
||||||
if (typeof promptCache === "number" && io && promptCache > io.total) {
|
|
||||||
parts.push(`prompt/cache ${formatTokenShort(promptCache)}`);
|
|
||||||
}
|
|
||||||
return parts.join(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveDisplayStatus(entry: SubagentRunRecord) {
|
function resolveDisplayStatus(entry: SubagentRunRecord) {
|
||||||
const status = formatRunStatus(entry);
|
const status = formatRunStatus(entry);
|
||||||
return status === "error" ? "failed" : status;
|
return status === "error" ? "failed" : status;
|
||||||
@@ -394,7 +337,7 @@ export const handleSubagentsCommand: CommandHandler = async (params, allowTextCo
|
|||||||
entry.childSessionKey,
|
entry.childSessionKey,
|
||||||
storeCache,
|
storeCache,
|
||||||
);
|
);
|
||||||
const usageText = resolveUsageDisplay(sessionEntry);
|
const usageText = formatTokenUsageDisplay(sessionEntry);
|
||||||
const label = truncateLine(formatRunLabel(entry, { maxLength: 48 }), 48);
|
const label = truncateLine(formatRunLabel(entry, { maxLength: 48 }), 48);
|
||||||
const task = formatTaskPreview(entry.task);
|
const task = formatTaskPreview(entry.task);
|
||||||
const runtime = formatDurationCompact(now - (entry.startedAt ?? entry.createdAt));
|
const runtime = formatDurationCompact(now - (entry.startedAt ?? entry.createdAt));
|
||||||
@@ -411,7 +354,7 @@ export const handleSubagentsCommand: CommandHandler = async (params, allowTextCo
|
|||||||
entry.childSessionKey,
|
entry.childSessionKey,
|
||||||
storeCache,
|
storeCache,
|
||||||
);
|
);
|
||||||
const usageText = resolveUsageDisplay(sessionEntry);
|
const usageText = formatTokenUsageDisplay(sessionEntry);
|
||||||
const label = truncateLine(formatRunLabel(entry, { maxLength: 48 }), 48);
|
const label = truncateLine(formatRunLabel(entry, { maxLength: 48 }), 48);
|
||||||
const task = formatTaskPreview(entry.task);
|
const task = formatTaskPreview(entry.task);
|
||||||
const runtime = formatDurationCompact(
|
const runtime = formatDurationCompact(
|
||||||
|
|||||||
@@ -39,3 +39,58 @@ export function truncateLine(value: string, maxLength: number) {
|
|||||||
}
|
}
|
||||||
return `${value.slice(0, maxLength).trimEnd()}...`;
|
return `${value.slice(0, maxLength).trimEnd()}...`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TokenUsageLike = {
|
||||||
|
totalTokens?: unknown;
|
||||||
|
inputTokens?: unknown;
|
||||||
|
outputTokens?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function resolveTotalTokens(entry?: TokenUsageLike) {
|
||||||
|
if (!entry || typeof entry !== "object") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (typeof entry.totalTokens === "number" && Number.isFinite(entry.totalTokens)) {
|
||||||
|
return entry.totalTokens;
|
||||||
|
}
|
||||||
|
const input = typeof entry.inputTokens === "number" ? entry.inputTokens : 0;
|
||||||
|
const output = typeof entry.outputTokens === "number" ? entry.outputTokens : 0;
|
||||||
|
const total = input + output;
|
||||||
|
return total > 0 ? total : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveIoTokens(entry?: TokenUsageLike) {
|
||||||
|
if (!entry || typeof entry !== "object") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const input =
|
||||||
|
typeof entry.inputTokens === "number" && Number.isFinite(entry.inputTokens)
|
||||||
|
? entry.inputTokens
|
||||||
|
: 0;
|
||||||
|
const output =
|
||||||
|
typeof entry.outputTokens === "number" && Number.isFinite(entry.outputTokens)
|
||||||
|
? entry.outputTokens
|
||||||
|
: 0;
|
||||||
|
const total = input + output;
|
||||||
|
if (total <= 0) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return { input, output, total };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatTokenUsageDisplay(entry?: TokenUsageLike) {
|
||||||
|
const io = resolveIoTokens(entry);
|
||||||
|
const promptCache = resolveTotalTokens(entry);
|
||||||
|
const parts: string[] = [];
|
||||||
|
if (io) {
|
||||||
|
const input = formatTokenShort(io.input) ?? "0";
|
||||||
|
const output = formatTokenShort(io.output) ?? "0";
|
||||||
|
parts.push(`tokens ${formatTokenShort(io.total)} (in ${input} / out ${output})`);
|
||||||
|
} else if (typeof promptCache === "number" && promptCache > 0) {
|
||||||
|
parts.push(`tokens ${formatTokenShort(promptCache)} prompt/cache`);
|
||||||
|
}
|
||||||
|
if (typeof promptCache === "number" && io && promptCache > io.total) {
|
||||||
|
parts.push(`prompt/cache ${formatTokenShort(promptCache)}`);
|
||||||
|
}
|
||||||
|
return parts.join(", ");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user