import { join } from 'path'; import { getEntityDir } from '../paths.js'; import { getNextId } from '../counter.js'; import { readEntity, writeEntity } from '../files.js'; import { HandoffSchema, type Handoff } from '../schema.js'; import { Index } from '../index.js'; export function createHandoff(cwd: string, options: Partial = {}): Handoff { const now = new Date().toISOString(); const handoff: Handoff = HandoffSchema.parse({ id: getNextId(cwd, 'handoff'), fromRole: options.fromRole ?? 'user', toRole: options.toRole ?? 'user', fromAgent: options.fromAgent, toAgent: options.toAgent, taskId: options.taskId, summary: options.summary ?? 'Handoff', context: options.context ?? '', decisions: options.decisions ?? [], openQuestions: options.openQuestions ?? [], nextSteps: options.nextSteps ?? [], createdAt: now, }); const filePath = join(getEntityDir(cwd, 'handoffs'), `${handoff.id}.md`); writeEntity(filePath, handoff, `# ${handoff.summary}\n\n${handoff.context}`); const index = new Index(cwd); index.upsert({ id: handoff.id, type: 'handoff', title: handoff.summary, content: handoff.context, filePath, createdAt: handoff.createdAt, updatedAt: handoff.createdAt, }); index.close(); return handoff; } export function listHandoffs(cwd: string): ReturnType { const index = new Index(cwd); const handoffs = index.list('handoff'); index.close(); return handoffs; } export function getHandoff(cwd: string, id: string): { handoff: Handoff; body: string; filePath: string } { if (!id) throw new Error('Handoff ID is required'); const filePath = join(getEntityDir(cwd, 'handoffs'), `${id}.md`); const { frontmatter, body } = readEntity(filePath); return { handoff: HandoffSchema.parse(frontmatter), body, filePath }; }