/** * flow-engine 存储 adapter —— Drizzle 只准出现在 adapter(编码规范 §5.3)。 */ import { eq } from 'drizzle-orm'; import type { FlowModel, TaskId } from '@contracts'; import { flowModelSchema } from '@contracts'; import type { Db } from '@shared/db.ts'; import type { FlowStateStore } from '../../port.ts'; import type { FlowPosition } from '../domain/step-sequence.ts'; import { flowStateTable } from './flow-state-schema.ts'; export function createFlowStateStore(db: Db): FlowStateStore { return { async save(taskId: TaskId, model: FlowModel, position: FlowPosition) { await db .insert(flowStateTable) .values({ taskId, model, position, updatedAt: new Date() }) .onConflictDoUpdate({ target: flowStateTable.taskId, set: { model, position, updatedAt: new Date() }, }); }, async load(taskId: TaskId) { const rows = await db.select().from(flowStateTable).where(eq(flowStateTable.taskId, taskId)).limit(1); const row = rows[0]; if (row === undefined) return undefined; return { model: flowModelSchema.parse(row.model), position: row.position as FlowPosition }; }, }; }