Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript
2.5 KB
Raw Blame
import { extractPreCompactRecords } from "../../../core/brain/pre-compact-extract.ts";
import { CliError, parse } from "../helpers.ts";
import { inspect } from "node:util";

export async function cmdBrainPreCompactExtract(argv: string[]): Promise<number> {
  const { flags } = parse(argv, {
    json: { type: "boolean" },
    vault: { type: "string" },
    "session-id": { type: "string" },
    "turn-start": { type: "string" },
    "turn-end": { type: "string" },
    text: { type: "string" },
    host: { type: "string" },
    "max-chars": { type: "string" },
    "dry-run": { type: "boolean" },
  });
  const vault = stringRequired(flags["vault"], "--vault");
  const sessionId = stringRequired(flags["session-id"], "--session-id");
  const turnStart = stringRequired(flags["turn-start"], "--turn-start");
  const turnEnd = stringRequired(flags["turn-end"], "--turn-end");
  const text = stringRequired(flags["text"], "--text");
  const maxChars = stringOptional(flags["max-chars"]);
  const result = extractPreCompactRecords(vault, {
    sessionId,
    turnStart,
    turnEnd,
    text,
    ...(stringOptional(flags["host"]) !== undefined ? { host: stringOptional(flags["host"]) } : {}),
    ...(maxChars !== undefined ? { maxChars: positiveInteger(maxChars, "--max-chars") } : {}),
    ...(flags["dry-run"] === true ? { dryRun: true } : {}),
  });
  writeOutput(
    { count: result.records.length, dry_run: flags["dry-run"] === true, ...result },
    flags["json"] === true,
  );
  return 0;
}

function stringRequired(value: string | boolean | string[] | undefined, label: string): string {
  const parsed = stringOptional(value);
  if (parsed === undefined) throw new CliError(`brain pre-compact-extract: ${label} is required`);
  return parsed;
}

function stringOptional(value: string | boolean | string[] | undefined): string | undefined {
  if (typeof value !== "string") return undefined;
  const trimmed = value.trim();
  return trimmed.length > 0 ? trimmed : undefined;
}

function positiveInteger(value: string, label: string): number {
  if (!/^[0-9]+$/.test(value)) throw new CliError(`${label} must be a positive integer`);
  const parsed = Number.parseInt(value, 10);
  if (parsed < 1) throw new CliError(`${label} must be a positive integer`);
  return parsed;
}

function writeOutput(value: unknown, json: boolean): void {
  if (json) {
    process.stdout.write(JSON.stringify(value) + "\n");
    return;
  }
  process.stdout.write(inspect(value, { colors: false, depth: null }) + "\n");
}