/** * Retrieves complete authorized context before PII classification and selects relevant * pseudonym assignments. Absent sources are recorded; failures from configured sources * propagate. Conversation search requires explicit room IDs. Retrieved fragments go only to * the PII scrub model; the secret alias map never enters the prompt, and assignments contain * only cluster ID, surrogate, and kind. */ import type { PiiScrubRequestPayload } from "../types/events.js"; import { type PiiPseudonymAssignment } from "../types/model.js"; import type { UUID } from "../types/primitives.js"; import type { IAgentRuntime } from "../types/runtime.js"; import type { CorpusPseudonymMap } from "./pii-pseudonym-map.js"; /** * One mined candidate from the candidate-mining stage — the issue's input * shape: `{surfaceForm, kind, sourceRef: {memoryId|documentId, tableName, * fragment position}, span}`. */ export interface PiiScrubCandidate { /** The surface form as it appears in the chunk ("Dr. K", "@jsmith"). */ readonly surfaceForm: string; /** Mined entity class guess (`person`, `org`, `location`, …). */ readonly kind: string; /** Where the candidate was mined from. */ readonly sourceRef?: { readonly memoryId?: string; readonly documentId?: string; readonly tableName?: string; /** Fragment position within the parent document, when applicable. */ readonly position?: number; }; /** Offsets of the surface form within the chunk. */ readonly span?: { readonly start: number; readonly end: number; }; /** * Platform identity, when the candidate IS a handle mined from a platform * mirror (e.g. `{platform: "discord", handle: "jsmith"}`). Drives exact * identity-based entity resolution. */ readonly identity?: { readonly platform: string; readonly handle: string; }; } /** One retrieved context fragment included completely in source order. */ export interface PiiContextFragment { readonly text: string; readonly origin: "document" | "memory" | "message" | "attachment"; /** Source row/document id, for the audit trail. */ readonly ref?: string; /** Relevance in [0,1] when the source measured one (never fabricated). */ readonly score?: number; } /** * A resolved entity candidate, normalized for the scrub pipeline. `clusterId` * is the stable corpus-map key (`entity:` for EntityStore-backed * resolution). */ export interface PiiResolvedEntity { readonly clusterId: string; readonly kind: string; /** Known surface forms: preferred name, full name, identity handles. */ readonly aliases: readonly string[]; readonly identities: readonly { readonly platform: string; readonly handle: string; }[]; readonly confidence: number; readonly evidence: readonly string[]; } /** * Structural subset of `EntityStore` (the alias backbone) that this pass * consumes — field-for-field the shape of * `EntityStore.resolve({name?, identity?, type?}) → EntityResolveCandidate[]` * so the real store satisfies it with zero adaptation. */ export interface PiiEntityResolverStore { resolve(query: { name?: string; identity?: { platform: string; handle: string; }; type?: string; }): Promise; } /** The retrieval seams the pass draws from. Absent = not available (audited). */ export interface PiiContextSources { readonly resolveEntity?: (candidate: PiiScrubCandidate) => Promise; readonly searchDocuments?: (query: string) => Promise; readonly searchMemories?: (query: string) => Promise; readonly searchMessages?: (query: string) => Promise; } /** Output 1 of the stage: the context pack for the LLM-pass. */ export interface PiiContextPack { /** * Complete, human-readable context text for the `PII_SCRUB` model call: * resolved-entity summaries (with the assigned pseudonym marker when the * cluster is already mapped) + nearest fragments. Never the secret map. */ readonly contextPack: string; /** The per-chunk cluster→surrogate slice (never the whole map). */ readonly assignments: readonly PiiPseudonymAssignment[]; /** Entity candidates that resolved with sufficient confidence. */ readonly resolvedEntities: readonly PiiResolvedEntity[]; /** Candidate surface forms in their original order — the seam's `candidateSpans`. */ readonly candidateSpans: readonly string[]; /** Which sources were queried vs structurally absent (audit). */ readonly sourcesQueried: readonly string[]; } export interface AssembleContextPackRequest { /** The chunk of text the LLM-pass will judge. */ readonly chunk: string; /** Mined candidates for this chunk. */ readonly candidates: readonly PiiScrubCandidate[]; /** The corpus pseudonym map (read + upserted for confident resolutions). */ readonly map: CorpusPseudonymMap; /** Active ruleset version (threaded into map assignments). */ readonly rulesetVersion: string; /** * Minimum resolution confidence for a candidate to be clustered into the * map. Below it, the entity still appears in the pack (as context) but no * assignment is made. Default 0.6 — the EntityStore's exact-name match * scores 0.9, substring 0.55, so defaults cluster exact/identity matches * and leave fuzzy ones to the model. */ readonly minEntityConfidence?: number; } /** * Assemble the context pack + pseudonym-assignment slice for one chunk. * See the module doc for the source/failure/secrecy contract. */ export declare function assembleContextPack(sources: PiiContextSources, request: AssembleContextPackRequest): Promise; /** * Adapt anything with `EntityStore.resolve`'s shape into the pass's * `resolveEntity` seam. The stable corpus-map cluster id is * `entity:`; kinds are canonicalized to the pseudonymizer's * vocabulary (`organization` → `org`, …) via {@link canonicalKind}. */ export declare function entityResolverFromStore(store: PiiEntityResolverStore): (candidate: PiiScrubCandidate) => Promise; export interface RuntimeContextSourceOptions { /** * Rooms for conversation FTS (`adapter.searchMessages` requires explicit * roomIds — enumerate via `getRoomsByWorld` / `getRoomsForParticipant`). * When omitted, the messages source is structurally absent. */ readonly roomIds?: readonly UUID[]; /** * The entity resolver, wired by the pipeline from the knowledge-graph * service (`entityResolverFromStore(kg.getEntityStore())`). Core does not * reach into `@elizaos/agent`, so this is injected. */ readonly resolveEntity?: PiiContextSources["resolveEntity"]; } /** * Wire {@link PiiContextSources} from a live runtime using only existing * surfaces: the documents service (hybrid search, keyword fallback built-in), * `runtime.searchMemories` (only when a TEXT_EMBEDDING model is registered — * the embeddings doctrine throws on failure, never fabricates), and * `adapter.searchMessages` (only when `roomIds` are supplied). */ export declare function sourcesFromRuntime(runtime: IAgentRuntime, options?: RuntimeContextSourceOptions): PiiContextSources; /** * Fold a chunk + its assembled context pack into the scrub-rails request * payload (`PII_SCRUB_REQUESTED`, drained by `PiiScrubService`). The payload * carries the pack text and the per-chunk assignment slice into the merged * seam (`scrubWithEscalation`) with ZERO changes to the landed rails. The * emitter supplies `runtime` at `emitEvent` time. */ export declare function buildScrubRequestDraft(input: { readonly content: string; readonly rulesetVersion: string; readonly pack: PiiContextPack; readonly priority?: PiiScrubRequestPayload["priority"]; readonly inferencePriority?: PiiScrubRequestPayload["inferencePriority"]; readonly jobId?: PiiScrubRequestPayload["jobId"]; readonly itemRef?: string; }): Omit;