agenthub/frontend/lib/format.ts

116 lines
4.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Rendering helpers shared by every page.
export function relTime(iso: string | null | undefined): string {
if (!iso) return "never";
const then = new Date(iso).getTime();
if (Number.isNaN(then)) return "never";
const seconds = Math.max(0, (Date.now() - then) / 1000);
if (seconds < 45) return "just now";
if (seconds < 3600) return Math.round(seconds / 60) + "m ago";
if (seconds < 86400) return Math.round(seconds / 3600) + "h ago";
return Math.round(seconds / 86400) + "d ago";
}
/** Agent 在哪台机器上:connector 自报的 host 优先,其次手工填的 deployment_ref。
*
* 手工 add + 直接填 agent_token 的行永远不走 register/join,register 期字段
* (deployment_ref、labels)一直是空的,机器名只存在于心跳带来的 capabilities。
*/
export function agentHost(agent: {
deployment_ref?: string;
capabilities?: Record<string, unknown>;
}): string {
const reported = agent.capabilities?.host;
return agent.deployment_ref || (typeof reported === "string" ? reported : "");
}
export function secondsAgo(value: number | null | undefined): string {
if (value === null || value === undefined) return "never";
if (value < 45) return Math.round(value) + "s ago";
if (value < 3600) return Math.round(value / 60) + "m ago";
return Math.round(value / 3600) + "h ago";
}
export function duration(seconds: number | null | undefined): string {
if (seconds === null || seconds === undefined) return "-";
if (seconds < 1) return "<1s";
if (seconds < 60) return seconds.toFixed(1) + "s";
return Math.floor(seconds / 60) + "m" + String(Math.round(seconds % 60)).padStart(2, "0") + "s";
}
export function uptime(seconds: number): string {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days) return days + "d " + hours + "h";
if (hours) return hours + "h " + minutes + "m";
return minutes + "m";
}
export function shortId(id: string, keep = 8): string {
if (!id) return "";
return id.length <= keep * 2 + 3 ? id : id.slice(0, keep) + "..." + id.slice(-4);
}
export const STATUS_TONE: Record<string, string> = {
online: "green",
completed: "green",
ok: "green",
running: "yellow",
queued: "yellow",
degraded: "yellow",
busy: "yellow",
failed: "red",
cancelled: "gray",
offline: "gray",
unknown: "gray",
};
// Agent 侧的接入方式:谁去连 Gateway。见 backend/agenthub/db/models.py。
export const CONNECTION_MODE_LABEL: Record<string, string> = {
// hub 内部的连接模式;「接入」页只出两类(插件 / 渠道、桥接),direct 那一类不在向导里。
embedded: "宿主插件",
connector: "桥接 / 渠道",
direct: "Hub 外呼",
};
// Agent 类型:这一行到底是哪个产品(决定像素小人的形象)。
// 四个具名产品 + custom 兜底,见 backend/agenthub/db/models.py: AGENT_TYPES。
export const AGENT_TYPE_LABEL: Record<string, string> = {
nanobot: "nanobot",
deepseek_harness: "DeepSeek Harness",
openclaw: "OpenClaw",
hermes: "Hermes",
custom: "其它",
};
export function agentTypeLabel(type: string | undefined): string {
return AGENT_TYPE_LABEL[(type ?? "").toLowerCase()] ?? (type || "-");
}
/**
* 桥接进程在 hub 眼里都是 custom + connector,真正区分它们的是适配器名
* (bridge.py --cli codex / claude / workbuddy),由桥接进程上报在 capabilities.cli。
*/
export function agentCli(agent: { capabilities?: Record<string, unknown> }): string {
const reported = agent.capabilities?.cli;
return typeof reported === "string" ? reported : "";
}
export function connectionMode(mode: string | undefined): string {
return CONNECTION_MODE_LABEL[(mode ?? "direct").toLowerCase()] ?? (mode || "直连");
}
export function tone(status: string | undefined): string {
return STATUS_TONE[(status ?? "unknown").toLowerCase()] ?? "gray";
}
export async function copyText(text: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}