import { getLocalStorage, setLocalStorage } from "@/lib/local-storage"; export type LastAgentError = { message: string; occurredAt?: string; agentExecutionId?: string; dismissedAt?: string; }; // --- Agent error visibility state (localStorage, global) --- // // `dismissedAgentErrors` tracks explicit chat-banner dismissals and hides both // the banner and task-row badge. `acknowledgedAgentErrors` tracks sidebar-only // stale-error acknowledgements and hides task-row badges without hiding chat. // Bounded growth: one entry per session that ever had an error. const DISMISSED_AGENT_ERRORS_KEY = "kandev.dismissedAgentErrors"; const ACKNOWLEDGED_AGENT_ERRORS_KEY = "kandev.acknowledgedAgentErrors"; export function getStoredDismissedAgentErrors(): Record { return getLocalStorage>(DISMISSED_AGENT_ERRORS_KEY, {}); } export function getStoredAcknowledgedAgentErrors(): Record { return getLocalStorage>(ACKNOWLEDGED_AGENT_ERRORS_KEY, {}); } /** * Merge `map` into whatever is currently in localStorage so concurrent writes * from other tabs (or older versions of this tab's state) are not clobbered. * Entries in `map` win over the on-disk values for the same session. */ export function setStoredDismissedAgentErrors(map: Record): void { const current = getStoredDismissedAgentErrors(); setLocalStorage(DISMISSED_AGENT_ERRORS_KEY, { ...current, ...map }); } export function setStoredAcknowledgedAgentErrors(map: Record): void { const current = getStoredAcknowledgedAgentErrors(); setLocalStorage(ACKNOWLEDGED_AGENT_ERRORS_KEY, { ...current, ...map }); } export function readLastAgentError(metadata: Record | null | undefined) { if (!metadata) return null; const raw = metadata.last_agent_error; if (!raw || typeof raw !== "object") return null; const record = raw as Record; const message = typeof record.message === "string" ? record.message : ""; if (!message) return null; const dismissedAt = readOptionalString(record.dismissed_at) ?? readOptionalString(record.dismissedAt); if (dismissedAt) return null; return { message, occurredAt: readOptionalString(record.occurred_at) ?? readOptionalString(record.occurredAt), agentExecutionId: readOptionalString(record.agent_execution_id) ?? readOptionalString(record.agentExecutionId), } satisfies LastAgentError; } /** * Stable identifier for a specific error event. Two errors share a stamp iff * they have the same occurredAt timestamp and message. Used to decide whether * a prior dismissal still applies after a fresh failure replaces the * `last_agent_error` metadata. */ export function lastAgentErrorStamp(error: LastAgentError) { return `${error.occurredAt ?? ""}:${error.message}`; } function readOptionalString(value: unknown) { return typeof value === "string" && value !== "" ? value : undefined; }