ai-chatbot/lib/ai/runtime/agent-thread-state.ts
Your Name 37db7e2d3a
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix agent video runtime dependencies
2026-03-13 10:40:45 +08:00

42 lines
1.2 KiB
TypeScript

import {
normalizeAgentWorkflowState,
type AgentWorkflowState,
} from "@/lib/ai/runtime/agent-workflow-state";
export interface AgentThreadState {
workflowState: AgentWorkflowState | null;
memoryHints: string[];
contextSummary?: string;
metadata?: Record<string, unknown>;
}
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<AgentThreadState> | 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<string, unknown>)
: undefined,
};
}