2026-08-22 19:41:37 +08:00
|
|
|
|
/** 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`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-17 23:52:50 +08:00
|
|
|
|
const LOW_INFORMATION_TITLE_PREVIEWS = new Set([
|
|
|
|
|
|
"hi",
|
|
|
|
|
|
"hello",
|
|
|
|
|
|
"hey",
|
|
|
|
|
|
"hello nano",
|
|
|
|
|
|
"hello nanobot",
|
|
|
|
|
|
"hi nano",
|
|
|
|
|
|
"hi nanobot",
|
|
|
|
|
|
"你好",
|
|
|
|
|
|
"您好",
|
|
|
|
|
|
"嗨",
|
|
|
|
|
|
"哈喽",
|
|
|
|
|
|
"哈啰",
|
|
|
|
|
|
"在吗",
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-07-24 17:34:26 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-17 23:52:50 +08:00
|
|
|
|
function isLowInformationTitlePreview(text: string): boolean {
|
|
|
|
|
|
const normalized = text.toLowerCase().replace(/[.!?。!?~~\s]+$/g, "").trim();
|
|
|
|
|
|
return (
|
|
|
|
|
|
normalized.startsWith("/") ||
|
|
|
|
|
|
LOW_INFORMATION_TITLE_PREVIEWS.has(normalized)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
|
/** Truncate the first user message into a chat title. */
|
|
|
|
|
|
export function deriveTitle(preview: string | undefined, fallback: string): string {
|
|
|
|
|
|
if (!preview) return fallback;
|
2026-07-24 17:34:26 +08:00
|
|
|
|
const oneLine = visibleSessionPreview(preview).replace(/\s+/g, " ").trim();
|
2026-04-18 18:51:53 +00:00
|
|
|
|
if (!oneLine) return fallback;
|
2026-05-17 23:52:50 +08:00
|
|
|
|
if (isLowInformationTitlePreview(oneLine)) return fallback;
|
2026-04-18 18:51:53 +00:00
|
|
|
|
return oneLine.length > 60 ? `${oneLine.slice(0, 57)}…` : oneLine;
|
|
|
|
|
|
}
|