agenthub/src/core/counter.ts
2026-06-24 23:31:14 +02:00

28 lines
794 B
TypeScript

import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { getAgentHubDir, getCounterPath } from './paths.js';
const prefixes: Record<string, string> = {
task: 'TSK',
handoff: 'HOF',
decision: 'DEC',
memory: 'MEM',
};
export type CounterType = keyof typeof prefixes;
export function getNextId(cwd: string, type: CounterType): string {
const path = getCounterPath(cwd);
const counters: Record<string, number> = existsSync(path)
? JSON.parse(readFileSync(path, 'utf-8'))
: {};
const current = counters[type] ?? 0;
const next = current + 1;
counters[type] = next;
mkdirSync(getAgentHubDir(cwd), { recursive: true });
writeFileSync(path, JSON.stringify(counters, null, 2));
return `${prefixes[type]}-${String(next).padStart(4, '0')}`;
}