Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

1.9 KB
Raw Blame
import { mkdir, open, readFile } from 'node:fs/promises';
import { dirname } from 'node:path';

/** A single host owns each journal. Never publish an entry before append resolves. */
export class Journal<T> {
  private tail: Promise<unknown> = Promise.resolve();
  private poisoned = false;
  private constructor(
    private readonly path: string,
    readonly records: T[]
  ) {}

  static async load<T>(path: string, parse: (input: unknown) => T): Promise<Journal<T>> {
    await mkdir(dirname(path), { recursive: true, mode: 0o700 });
    const file = await open(path, 'a', 0o600);
    await file.sync();
    await file.close();
    if (process.platform !== 'win32') {
      const directory = await open(dirname(path), 'r');
      try {
        await directory.sync();
      } finally {
        await directory.close();
      }
    }
    const text = await readFile(path, 'utf8');
    if (text && !text.endsWith('\n'))
      throw new Error('Journal has an incomplete write; explicit recovery is required.');
    return new Journal(
      path,
      text
        .split('\n')
        .filter(Boolean)
        .map((line) => parse(JSON.parse(line)))
    );
  }

  append(value: T): Promise<void> {
    return this.appendAll([value]);
  }

  /** Appends several entries with one write and one flush to disk. */
  appendAll(values: T[]): Promise<void> {
    const copies = structuredClone(values);
    const pending = this.tail.then(async () => {
      if (this.poisoned) throw new Error('Journal write failed; reload before continuing.');
      const file = await open(this.path, 'a', 0o600);
      try {
        await file.writeFile(copies.map((copy) => `${JSON.stringify(copy)}\n`).join(''));
        await file.sync();
        this.records.push(...copies);
      } catch (error) {
        this.poisoned = true;
        throw error;
      } finally {
        await file.close();
      }
    });
    this.tail = pending.catch(() => {});
    return pending;
  }
}