Files
saltbo__agent-kanban/server/adapters/d1/tasks/d1TaskEvents.ts
jarvis a2fa54a729 feat!: rebuild Agent Kanban for Realmroot Toolbox
Replace the v1 Agent/Machine runtime and CLI architecture with the v2 Resource Server, AMA-backed projections, task lifecycle, session observation, BDD specifications, and a single-package layered codebase.

BREAKING CHANGE: remove the v1 CLI, internal Agent/Machine runtime entities, workspace layout, and legacy runtime coordination APIs.
2026-09-02 00:17:52 -04:00

40 lines
1.4 KiB
TypeScript

import type { D1 } from "@server/db";
import type { TaskEventRepository, TaskEventSnapshot } from "@server/usecases/tasks/waitForTaskEvents";
import type { Task } from "@shared";
import { mapTaskRow } from "./taskRow";
type TaskEventRow = Task & { event_offset: number };
export function d1TaskEventRepository(db: D1): TaskEventRepository {
return {
async readSnapshot(ownerId: string, taskIds: string[]): Promise<TaskEventSnapshot | null> {
const result = await db
.prepare(`
WITH requested(id) AS (
SELECT CAST(value AS TEXT) FROM json_each(?)
)
SELECT t.*, COALESCE((
SELECT MAX(event.sequence)
FROM task_event_offsets event
WHERE event.task_id IN (SELECT id FROM requested)
), 0) AS event_offset
FROM tasks t
JOIN boards b ON b.id = t.board_id
WHERE b.owner_id = ? AND t.id IN (SELECT id FROM requested)
`)
.bind(JSON.stringify(taskIds), ownerId)
.all<TaskEventRow>();
if (result.results.length !== taskIds.length) return null;
const byId = new Map(
result.results.map((row) => {
const { event_offset: _eventOffset, ...task } = row;
return [row.id, task as Task] as const;
}),
);
const tasks = taskIds.map((taskId) => mapTaskRow(byId.get(taskId)!));
return { tasks, offset: result.results[0]?.event_offset ?? 0 };
},
};
}