/** * LLM-driven memory extraction gate. * * Replaces the legacy keyword regex in `auto-extract.ts`. Takes a session * transcript and uses an Anthropic model to extract typed, structured * memories (decisions, patterns, learnings, constraints, corrections) * with justification, importance score, and referenced entities. * * Design: * - Runs only when `brain.llmExtraction.enabled` is true AND an Anthropic * API key is available (from ANTHROPIC_API_KEY env var OR Claude Code * credentials at ~/.claude/.credentials.json). Otherwise returns empty. * - Uses structured output (JSON schema) for reliable parsing — no regex * post-processing. * - Never throws — all errors are caught and logged. The pipeline must * never block session end. * - Store routing: each extracted memory is sent through the existing * verify-and-store pipeline. Decisions → brain_decisions, patterns → * brain_patterns, learnings/constraints/corrections → brain_learnings. * - Source is tagged `agent-llm-extracted` so downstream dedup, quality * scoring, and consolidation can distinguish LLM output from other * pipelines. * * Research basis: `.cleo/agent-outputs/R-llm-memory-systems-research.md` * (Mem0, Hindsight, Letta, Mastra Observational Memory). */ import type Anthropic from '@anthropic-ai/sdk'; /** * Allowed extraction types. Each maps to a downstream BRAIN store: * * - `decision` → brain_decisions (architectural/design decisions) * - `pattern` → brain_patterns (procedural how-to knowledge) * - `learning` → brain_learnings (semantic facts with confidence) * - `constraint` → brain_learnings (rules to follow, high-confidence) * - `correction` → brain_patterns (anti-patterns stored with antiPattern field) */ export type ExtractionType = 'decision' | 'pattern' | 'learning' | 'constraint' | 'correction'; /** * A single memory extracted by the LLM. Fields match the research-approved * prompt pattern so downstream storage can route without additional parsing. */ export interface ExtractedMemory { /** Category of knowledge — routes to the correct BRAIN table. */ type: ExtractionType; /** Declarative knowledge content. Capped at 500 characters by the schema. */ content: string; /** Importance score 0.0–1.0; only values ≥ minImportance are persisted. */ importance: number; /** Code symbols, file paths, or concepts referenced by this memory. */ entities: string[]; /** Why the model thinks this memory is worth keeping. Capped at 200 chars. */ justification: string; } /** Options for extracting memories from a transcript. */ export interface ExtractFromTranscriptOptions { /** CLEO project root. */ projectRoot: string; /** Session ID used as the source tag on stored memories. */ sessionId: string; /** Full session transcript to extract from. */ transcript: string; /** * Optional injected Anthropic client — used by tests to mock the SDK * without touching the real network. Production callers should omit this * and let the function construct its own client from `ANTHROPIC_API_KEY`. */ client?: Pick; } /** Summary of what the LLM extraction pipeline produced. */ export interface ExtractionReport { /** Count of extractions returned by the model before filtering. */ extractedCount: number; /** Count persisted after importance filter and verify-and-store gate. */ storedCount: number; /** Count merged into existing entries by the verify-and-store gate. */ mergedCount: number; /** Count rejected (below minImportance, gate-rejected, or errored). */ rejectedCount: number; /** Non-fatal warnings collected during extraction. */ warnings: string[]; } /** * Extract and persist structured memories from a session transcript using * an Anthropic model. * * This is the sole entry point replacing the legacy keyword-regex extractor. * Callers (e.g. the session-end hook) invoke this and the function handles: * 1. Config lookup (brain.llmExtraction.enabled, model, thresholds) * 2. API key detection (skips extraction if missing) * 3. Transcript clipping to stay within token budget * 4. Structured-output LLM call with the research-approved prompt * 5. Importance filtering (below minImportance → dropped) * 6. Storage routing through the existing verify-and-store pipeline * * Never throws. Returns an ExtractionReport describing what was stored. * * @example * ```ts * await extractFromTranscript({ * projectRoot: '/path/to/project', * sessionId: 'ses_20260413192026_519188', * transcript: rawTranscript, * }); * ``` */ export declare function extractFromTranscript(options: ExtractFromTranscriptOptions): Promise; //# sourceMappingURL=llm-extraction.d.ts.map