/** * Agents' memory as the hub keeps it (memory.rs): a set per repository and one * for the workspace, each note a Markdown file with a little frontmatter * (name, description, type). The page's Memory lists and searches them. */ /** A note as the hub sends it. */ export interface HubNote { text: string line: string ts: number by: string deleted: boolean } /** A note as the page shows it. */ export interface MemoryNote { file: string /** `workspace`, or the repository's `owner/name`. */ set: string name: string description: string /** user, feedback, project, reference; '' when it says none. */ type: string /** The text after the frontmatter. */ body: string ts: number by: string } const unquote = (v: string) => v.trim().replace(/^(["'])(.*)\1$/, '$2').replace(/\\"/g, '"') /** A note's frontmatter fields and its body; a note without frontmatter is all body. */ export function parse(text: string): {name: string; description: string; type: string; body: string} { const m = text.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/) if (!m) return {name: '', description: '', type: '', body: text} const field = (k: string) => { const f = m[1]!.match(new RegExp(`^\\s*${k}:[ \\t]*(.*)$`, 'm')) return f ? unquote(f[1]!) : '' } return {name: field('name'), description: field('description'), type: field('type'), body: m[2]!.trim()} } /** Every set's notes, removals left out, newest first. */ export function notes(sets: Record>): MemoryNote[] { const out: MemoryNote[] = [] for (const [set, files] of Object.entries(sets)) { for (const [file, n] of Object.entries(files)) { if (n.deleted) continue const p = parse(n.text) out.push({file, set, name: p.name || file.replace(/\.md$/, ''), description: p.description, type: p.type, body: p.body, ts: n.ts, by: n.by}) } } return out.sort((a, b) => b.ts - a.ts || a.file.localeCompare(b.file)) } /** * The notes that have every word of `query`, in their name, description, file * or text, regardless of case; a match in the name or description first. */ export function search(all: MemoryNote[], query: string): MemoryNote[] { const words = query.toLowerCase().split(/\s+/).filter(Boolean) if (!words.length) return all const head = (n: MemoryNote) => `${n.name} ${n.description} ${n.file}`.toLowerCase() const hits = all.filter((n) => { const text = `${head(n)} ${n.body.toLowerCase()}` return words.every((w) => text.includes(w)) }) const inHead = (n: MemoryNote) => words.filter((w) => head(n).includes(w)).length // Stable: newest first among equals. return hits.map((n, i) => ({n, i, h: inHead(n)})).sort((a, b) => b.h - a.h || a.i - b.i).map((x) => x.n) }