refactor(subagents): share formatting helpers

This commit is contained in:
Peter Steinberger
2026-02-15 06:15:30 +00:00
parent 2c5e24cbb5
commit bdc3e447e9
3 changed files with 55 additions and 88 deletions

View File

@@ -27,6 +27,11 @@ import { callGateway } from "../../gateway/call.js";
import { logVerbose } from "../../globals.js";
import { formatTimeAgo } from "../../infra/format-time/format-relative.ts";
import { parseAgentSessionKey } from "../../routing/session-key.js";
import {
formatDurationCompact,
formatTokenShort,
truncateLine,
} from "../../shared/subagents-format.js";
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
import { stopSubagentsForRequester } from "./abort.js";
import { clearSessionQueues } from "./queue.js";
@@ -46,48 +51,6 @@ const RECENT_WINDOW_MINUTES = 30;
const SUBAGENT_TASK_PREVIEW_MAX = 110;
const STEER_ABORT_SETTLE_TIMEOUT_MS = 5_000;
function formatDurationCompact(valueMs?: number) {
if (!valueMs || !Number.isFinite(valueMs) || valueMs <= 0) {
return "n/a";
}
const minutes = Math.max(1, Math.round(valueMs / 60_000));
if (minutes < 60) {
return `${minutes}m`;
}
const hours = Math.floor(minutes / 60);
const minutesRemainder = minutes % 60;
if (hours < 24) {
return minutesRemainder > 0 ? `${hours}h${minutesRemainder}m` : `${hours}h`;
}
const days = Math.floor(hours / 24);
const hoursRemainder = hours % 24;
return hoursRemainder > 0 ? `${days}d${hoursRemainder}h` : `${days}d`;
}
function formatTokenShort(value?: number) {
if (!value || !Number.isFinite(value) || value <= 0) {
return undefined;
}
const n = Math.floor(value);
if (n < 1_000) {
return `${n}`;
}
if (n < 10_000) {
return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
}
if (n < 1_000_000) {
return `${Math.round(n / 1_000)}k`;
}
return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}m`;
}
function truncateLine(value: string, maxLength: number) {
if (value.length <= maxLength) {
return value;
}
return `${value.slice(0, maxLength).trimEnd()}...`;
}
function compactLine(value: string) {
return value.replace(/\s+/g, " ").trim();
}