import type { AiMiddleware, MemoryEntry, RemembersSpec } from './types.js'; import type { UserMemoryLookup } from './memory.js'; export interface MemoryExtractOptions { /** * Override the {@link UserMemory} lookup. Defaults to the module-level * `resolveUserMemory()` registry that `AiProvider` writes to from * `AiConfig.memory`. Tests pass a closure to inject a fake. */ lookup?: UserMemoryLookup; /** * Confidence floor for the small model's self-rated `score`. Facts * with a score < threshold are dropped before any `remember()` call. * Default `0.7`. * * **Tuning note (poisoning mitigation):** the threshold is the v1 * defense against a malicious user planting adversarial "facts." A * low threshold accepts more spam; a high threshold filters useful * signal. Pair with `MemoryExtractOptions.onExtracted` for an audit * log when you ship extract to production. */ threshold?: number; /** * Called after a successful extract with the entries that survived * the threshold filter and were written to the store. Use this to * stream entries into telescope, an audit log, or test assertions. */ onExtracted?: (entries: MemoryEntry[]) => void; /** * Called when extract fails — small-model network error, JSON parse * failure, schema-validation rejection, or `mem.remember()` throw. * Errors are otherwise swallowed; the parent run never breaks. */ onError?: (err: unknown) => void; } /** * Post-conversation {@link AiMiddleware} that asks a small model to * distill the latest `[user, assistant]` turn into durable facts and * writes the survivors (above `threshold`) to the registered * {@link UserMemory}. Auto-installed by `Agent.prompt` / * `Agent.stream` when `Agent.remembers().extract === 'auto'` and * `extractWith` is set; can also be dropped into `Agent.middleware()` * manually. * * Runs in `onFinish` — only fires on a successful loop, so failed * runs don't pollute memory. Failures inside the extract path * (network, JSON parse, zod validation, `remember()` throw) are * routed through `MemoryExtractOptions.onError` and otherwise * swallowed; the parent prompt never breaks because of memory work. * * **Auto-installed extracts skip continuation calls** (`options.messages` * set) at the host level — the same way auto-inject does. Manually * installed extracts always run. * * **Pitfall — memory poisoning:** auto-extraction lets a malicious * user plant adversarial "facts." The threshold (default 0.7) is the * baseline defense; pair with `onExtracted` for an audit log when you * ship to production. A content-filter middleware is a follow-up. */ export declare function withMemoryExtract(spec: RemembersSpec, opts?: MemoryExtractOptions): AiMiddleware; //# sourceMappingURL=memory-extract.d.ts.map