import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; import { v4 as uuidv4 } from "uuid"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } export function esc(str: string | null | undefined): string { if (!str) return ""; const value = String(str); const div = document.createElement("div"); div.textContent = value; return div.innerHTML; } export function formatTime(ts: number | null | undefined): string { if (!ts) return ""; return new Date(ts * 1000).toLocaleString(); } export function statusClass(status: string | null | undefined): string { const map: Record = { active: "active", running: "running", idle: "idle", inactive: "inactive", requires_action: "requires_action", archived: "archived", error: "error", }; return map[status || ""] || "default"; } export function isClosedSessionStatus(status: string | null | undefined): boolean { return status === "archived" || status === "inactive"; } export function truncate(str: string | null | undefined, max: number): string { if (!str) return ""; const s = String(str); return s.length > max ? s.slice(0, max) + "..." : s; } export function generateMessageUuid(): string { return uuidv4(); } export function extractEventText(payload: Record | null | undefined): string { if (!payload || typeof payload !== "object") return ""; if (typeof payload.content === "string") return payload.content; const msg = payload.message as Record | undefined; if (msg && typeof msg === "object" && Array.isArray(msg.content)) { const texts = msg.content .filter((b: Record) => b && b.type === "text" && typeof b.text === "string") .map((b: Record) => b.text as string); if (texts.length > 0) return texts.join("\n"); } return ""; } export function isConversationClearedStatus( payload: Record | null | undefined, ): boolean { if (!payload || typeof payload !== "object") return false; if (payload.status === "conversation_cleared") return true; const raw = payload.raw as Record | undefined; return !!raw && typeof raw === "object" && raw.status === "conversation_cleared"; }