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
3.8 KB
Raw Blame
/**
 * `o2b brain intention <set|show|list|move>` (Agent Surface Suite,
 * t_6d78f69e): scoped current-intention chains with a move-to-history
 * lifecycle.
 */

import { resolveAgentName } from "../../../core/config.ts";
import {
  listIntentions,
  moveIntentionToHistory,
  setIntention,
  showIntention,
} from "../../../core/brain/intentions.ts";
import { nextCommandField } from "../../../core/brain/next-step.ts";
import { emitNextStep, type AdvisoryStream } from "../../advisory-rail.ts";
import { brainVerbContext, fail, normalizeFlagString, ok, okJson, parse } from "../helpers.ts";

export async function cmdBrainIntention(argv: string[]): Promise<number> {
  const action = argv[0];
  if (!action || !["set", "show", "list", "move"].includes(action)) {
    return fail("usage: o2b brain intention <set|show|list|move> [--scope S] [--text T]");
  }
  const { flags } = parse(argv.slice(1), {
    vault: { type: "string" },
    scope: { type: "string" },
    text: { type: "string" },
    json: { type: "boolean" },
  });
  const { config, vault } = brainVerbContext(flags);
  const scope = normalizeFlagString(flags["scope"]);
  const json = flags["json"] === true;

  if (action === "list") {
    let intentions;
    try {
      intentions = listIntentions(vault);
    } catch (err) {
      return fail((err as Error).message ?? String(err));
    }
    if (json) {
      okJson({
        ok: true,
        intentions: intentions.map((c) => ({
          scope: c.scope,
          version: c.version,
          updated_at: c.updatedAt,
          text: c.text,
        })),
        // no-dead-ends, phase 3: the exit the human stream prints as a
        // rail line, carried here as a field.
        ...(intentions.length === 0 ? nextCommandField("intentions-absent") : {}),
      });
      return 0;
    }
    if (intentions.length === 0) {
      ok("no active intentions");
      // no-dead-ends, task 5: the exit is this verb's own setter.
      // The `--json` branch above returned; this stream is human.
      const stream: AdvisoryStream = { command: "brain", argv, jsonRequested: false };
      emitNextStep("intentions-absent", stream);
      return 0;
    }
    for (const c of intentions) ok(`${c.scope} v${c.version} (${c.updatedAt}): ${c.text}`);
    return 0;
  }

  if (scope === null) return fail(`brain intention ${action} requires --scope`);

  try {
    if (action === "set") {
      const text = normalizeFlagString(flags["text"]);
      if (text === null) return fail("brain intention set requires --text");
      const chain = setIntention(vault, { scope, text, agent: resolveAgentName(config) });
      if (json) okJson({ ok: true, scope: chain.scope, version: chain.version, path: chain.path });
      else ok(`intention ${chain.scope} v${chain.version}: ${chain.text}`);
      return 0;
    }
    if (action === "show") {
      const chain = showIntention(vault, scope);
      if (chain === null) {
        if (json) okJson({ ok: true, present: false, scope });
        else ok(`no active intention for scope: ${scope}`);
        return 0;
      }
      if (json) {
        okJson({
          ok: true,
          present: true,
          scope: chain.scope,
          version: chain.version,
          updated_at: chain.updatedAt,
          text: chain.text,
          history: chain.history,
          path: chain.path,
        });
      } else {
        ok(`${chain.scope} v${chain.version} (${chain.updatedAt}): ${chain.text}`);
        for (const line of chain.history) ok(`  history: ${line}`);
      }
      return 0;
    }
    // move
    const moved = moveIntentionToHistory(vault, { scope });
    if (json) okJson({ ok: true, scope: moved.scope, archive_path: moved.archivePath });
    else ok(`intention ${moved.scope} moved to history: ${moved.archivePath}`);
    return 0;
  } catch (err) {
    return fail((err as Error).message);
  }
}