import { normalizeAgentWorkflowState, type AgentWorkflowState, } from "@/lib/ai/runtime/agent-workflow-state"; export interface AgentThreadState { workflowState: AgentWorkflowState | null; memoryHints: string[]; contextSummary?: string; metadata?: Record; } function normalizeText(value: unknown, max = 300): string | undefined { const normalized = String(value || "").trim(); if (!normalized) return undefined; return normalized.slice(0, max); } function normalizeMemoryHints(value: unknown): string[] { if (!Array.isArray(value)) return []; return value .map((item) => normalizeText(item, 120)) .filter((item): item is string => Boolean(item)); } export function normalizeAgentThreadState( value?: Partial | null, ): AgentThreadState | null { if (!value || typeof value !== "object") return null; return { workflowState: normalizeAgentWorkflowState(value.workflowState), memoryHints: normalizeMemoryHints(value.memoryHints), contextSummary: normalizeText(value.contextSummary, 400), metadata: value.metadata && typeof value.metadata === "object" && !Array.isArray(value.metadata) ? (value.metadata as Record) : undefined, }; }