42 lines
1.2 KiB
TypeScript
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,
|
|
};
|
|
}
|
|
|