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.4 KB
Raw Blame
/**
 * Recall hint for brain_search results (v0.18.0).
 *
 * A computed-at-recall-time, never-stored summary that tells the calling
 * agent how to read the recalled set: how many matched, the per-type
 * breakdown, and the top hit. It complements each result's `reasons[]`
 * (why_retrieved) with a one-line orientation over the whole set.
 *
 * Language-agnostic by construction: the string is built from a single
 * English template plus numbers and identifiers already present in the
 * data (search type, score, title). There is no per-locale phrase table -
 * the project ships one authoring language and translation, if ever
 * wanted, is a host concern, not a stored taxonomy.
 */

const MAX_TITLE_CHARS = 80;

export interface RecallHintInput {
  readonly searchType: string;
  readonly score: number;
  readonly title: string | null;
}

function trimTitle(title: string | null): string {
  if (!title) return "(untitled)";
  if (title.length <= MAX_TITLE_CHARS) return title;
  return title.slice(0, MAX_TITLE_CHARS - 1) + "…";
}

/**
 * Build the recall hint, or `null` when there is nothing to summarise.
 * `results` is assumed already ranked (best first), matching the search
 * pipeline's output order.
 */
export function deriveRecallHint(
  results: ReadonlyArray<RecallHintInput>,
  total: number,
): string | null {
  if (results.length === 0) return null;

  const counts = new Map<string, number>();
  for (const r of results) {
    counts.set(r.searchType, (counts.get(r.searchType) ?? 0) + 1);
  }
  const breakdown = [...counts.entries()]
    .toSorted((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
    .map(([type, n]) => `${n} ${type}`)
    .join(", ");

  const top = results[0]!;
  // `total` is the ranked candidate pool the window was sliced from, NOT a
  // count of corpus matches: it is capped at roughly three times the
  // requested limit and link traversal can add a document that matched no
  // term. Calling it "matches" would state a corpus fact the number
  // cannot carry. It should be >= the returned slice; guard against a
  // caller passing a smaller total so the string never reads "10 of 3".
  const denom = Math.max(total, results.length);
  return (
    `Recalled ${results.length} of ${denom} ranked candidates (${breakdown}). ` +
    `Top hit "${trimTitle(top.title)}" (${top.searchType}, score ${top.score.toFixed(2)}). ` +
    `See each result's reasons[] for why it surfaced.`
  );
}