Verified: - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: vishaltandale00 <9222298+vishaltandale00@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { formatDurationPrecise } from "../infra/format-time/format-duration.ts";
|
|
import { formatRuntimeStatusWithDetails } from "../infra/runtime-status.ts";
|
|
import type { SessionStatus } from "./status.types.js";
|
|
|
|
export const formatKTokens = (value: number) =>
|
|
`${(value / 1000).toFixed(value >= 10_000 ? 0 : 1)}k`;
|
|
|
|
export const formatDuration = (ms: number | null | undefined) => {
|
|
if (ms == null || !Number.isFinite(ms)) {
|
|
return "unknown";
|
|
}
|
|
return formatDurationPrecise(ms, { decimals: 1 });
|
|
};
|
|
|
|
export const shortenText = (value: string, maxLen: number) => {
|
|
const chars = Array.from(value);
|
|
if (chars.length <= maxLen) {
|
|
return value;
|
|
}
|
|
return `${chars.slice(0, Math.max(0, maxLen - 1)).join("")}…`;
|
|
};
|
|
|
|
export const formatTokensCompact = (
|
|
sess: Pick<
|
|
SessionStatus,
|
|
"totalTokens" | "contextTokens" | "percentUsed" | "cacheRead" | "cacheWrite"
|
|
>,
|
|
) => {
|
|
const used = sess.totalTokens;
|
|
const ctx = sess.contextTokens;
|
|
const cacheRead = sess.cacheRead;
|
|
const cacheWrite = sess.cacheWrite;
|
|
|
|
let result = "";
|
|
if (used == null) {
|
|
result = ctx ? `unknown/${formatKTokens(ctx)} (?%)` : "unknown used";
|
|
} else if (!ctx) {
|
|
result = `${formatKTokens(used)} used`;
|
|
} else {
|
|
const pctLabel = sess.percentUsed != null ? `${sess.percentUsed}%` : "?%";
|
|
result = `${formatKTokens(used)}/${formatKTokens(ctx)} (${pctLabel})`;
|
|
}
|
|
|
|
// Add cache hit rate if there are cached reads
|
|
if (typeof cacheRead === "number" && cacheRead > 0) {
|
|
const total =
|
|
typeof used === "number"
|
|
? used
|
|
: cacheRead + (typeof cacheWrite === "number" ? cacheWrite : 0);
|
|
const hitRate = Math.round((cacheRead / total) * 100);
|
|
result += ` · 🗄️ ${hitRate}% cached`;
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
export const formatDaemonRuntimeShort = (runtime?: {
|
|
status?: string;
|
|
pid?: number;
|
|
state?: string;
|
|
detail?: string;
|
|
missingUnit?: boolean;
|
|
}) => {
|
|
if (!runtime) {
|
|
return null;
|
|
}
|
|
const details: string[] = [];
|
|
const detail = runtime.detail?.replace(/\s+/g, " ").trim() || "";
|
|
const noisyLaunchctlDetail =
|
|
runtime.missingUnit === true && detail.toLowerCase().includes("could not find service");
|
|
if (detail && !noisyLaunchctlDetail) {
|
|
details.push(detail);
|
|
}
|
|
return formatRuntimeStatusWithDetails({
|
|
status: runtime.status,
|
|
pid: runtime.pid,
|
|
state: runtime.state,
|
|
details,
|
|
});
|
|
};
|