Files
nanobot/webui/src/lib/format.ts
T
chengyongru 328e933d36 refactor: isolate runtime contracts and enforce import boundaries (#6166)
* test: enforce Python import ownership boundaries

* test: protect client import boundaries and rehome shared UI contracts

* refactor: remove gateway and client dependency inversions

* fix: detect dynamic imports in conditional expressions

* refactor(tools): keep configuration contracts with their owners

* docs: keep design guidance focused on ownership and checks

* fix: preserve default threaded turn delivery
2026-10-13 13:56:28 +08:00

63 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Compact token counts for dense runtime metadata (for example, 74.9K). */
export function formatCompactTokenCount(value: number): string {
if (value < 1_000) return Math.round(value).toLocaleString();
if (value < 1_000_000) {
const digits = value < 100_000 ? 1 : 0;
return `${Number((value / 1_000).toFixed(digits))}K`;
}
const digits = value < 100_000_000 ? 1 : 0;
return `${Number((value / 1_000_000).toFixed(digits))}M`;
}
const LOW_INFORMATION_TITLE_PREVIEWS = new Set([
"hi",
"hello",
"hey",
"hello nano",
"hello nanobot",
"hi nano",
"hi nanobot",
"你好",
"您好",
"嗨",
"哈喽",
"哈啰",
"在吗",
]);
export function isModelCommandText(text: string | null | undefined): boolean {
return /^\/model(?:@[A-Za-z0-9_]+)?(?:\s|$)/i.test(text?.trim() ?? "");
}
export function isModelCommandResponseText(text: string | null | undefined): boolean {
const normalized = text?.trim() ?? "";
return (
/^## Model\s+- Current (?:model|selection error):/.test(normalized)
|| normalized.startsWith("Switched model preset to ")
|| normalized.startsWith("Could not switch model preset:")
|| normalized === "Usage: `/model [preset]`"
);
}
export function visibleSessionPreview(preview: string | null | undefined): string {
const normalized = preview?.trim() ?? "";
return isModelCommandText(normalized) || isModelCommandResponseText(normalized) ? "" : normalized;
}
function isLowInformationTitlePreview(text: string): boolean {
const normalized = text.toLowerCase().replace(/[.!?。!?~~\s]+$/g, "").trim();
return (
normalized.startsWith("/") ||
LOW_INFORMATION_TITLE_PREVIEWS.has(normalized)
);
}
/** Truncate the first user message into a chat title. */
export function deriveTitle(preview: string | undefined, fallback: string): string {
if (!preview) return fallback;
const oneLine = visibleSessionPreview(preview).replace(/\s+/g, " ").trim();
if (!oneLine) return fallback;
if (isLowInformationTitlePreview(oneLine)) return fallback;
return oneLine.length > 60 ? `${oneLine.slice(0, 57)}…` : oneLine;
}