agenthub/src/core/services/decisionService.ts

54 lines
1.8 KiB
TypeScript

import { join } from 'path';
import { getEntityDir } from '../paths.js';
import { getNextId } from '../counter.js';
import { readEntity, writeEntity } from '../files.js';
import { DecisionSchema, type Decision } from '../schema.js';
import { Index } from '../index.js';
export function createDecision(cwd: string, options: Partial<Decision> = {}): Decision {
const now = new Date().toISOString();
const record: Decision = DecisionSchema.parse({
id: getNextId(cwd, 'decision'),
title: options.title ?? 'Decision',
context: options.context ?? '',
decision: options.decision ?? '',
status: options.status ?? 'accepted',
consequences: options.consequences ?? [],
alternatives: options.alternatives ?? [],
relatedDecisions: options.relatedDecisions ?? [],
createdAt: now,
updatedAt: now,
});
const filePath = join(getEntityDir(cwd, 'decisions'), `${record.id}.md`);
writeEntity(filePath, record, `# ${record.title}\n\n## Decision\n\n${record.decision}\n\n## Context\n\n${record.context}`);
const index = new Index(cwd);
index.upsert({
id: record.id,
type: 'decision',
title: record.title,
content: `${record.context} ${record.decision}`,
filePath,
createdAt: record.createdAt,
updatedAt: record.updatedAt,
});
index.close();
return record;
}
export function listDecisions(cwd: string): ReturnType<Index['list']> {
const index = new Index(cwd);
const decisions = index.list('decision');
index.close();
return decisions;
}
export function getDecision(cwd: string, id: string): { decision: Decision; body: string; filePath: string } {
if (!id) throw new Error('Decision ID is required');
const filePath = join(getEntityDir(cwd, 'decisions'), `${id}.md`);
const { frontmatter, body } = readEntity(filePath);
return { decision: DecisionSchema.parse(frontmatter), body, filePath };
}