import { FileAsset, AgentLearningOverridePatch, AgentInstructionSaveRequest, AgentInstructionReviewRequest, WorkspaceInstructionPolicyTarget, KnowledgeEntryListRequest, KnowledgeReviewBatchListRequest, KnowledgeEntryBatchReviewRequest, KnowledgeTaskNotePromotionRequest, KnowledgeEntryRecord, KnowledgeEntryReach, KnowledgeEntryReachRecord, KnowledgeEntryReviewRequest, KnowledgeEntryRestoreRequest, KnowledgeEntrySaveRequest, type AgentLearningContext, type KnowledgeEntryScope } from "@opengeni/contracts"; export { KnowledgeLearningScopeChangedError } from "./knowledge-learning-errors.js"; import { type Database } from "./database.js"; /** Trusted host claims. Never deserialize this context from tool/request input. */ export type KnowledgeActor = { kind: "service"; principalKind: "service" | "api_key" | "configured_key" | "mcp_gateway"; subjectId: string; writeScopes: Array<"workspace" | "organization">; review: false; settingsScopes: []; } | { kind: "human"; principalKind: "human_session"; subjectId: string; writeScopes: KnowledgeEntryScope[]; settingsScopes: Array<"workspace" | "personal">; review: boolean; } | { kind: "agent"; sessionId: string; turnId: string; attemptId: string; executionGeneration: number; }; export type KnowledgeContext = { accountId: string; workspaceId: string; actor: KnowledgeActor; }; /** The supplied entry id belongs to an entry this caller cannot see or did not mean. */ export declare class KnowledgeEntryIdTakenError extends Error { readonly code = "knowledge_entry_id_taken"; constructor(); } /** A correction must name its entry; only a create may leave the id to the host. */ export declare class KnowledgeEntryIdRequiredError extends Error { readonly code = "knowledge_entry_id_required"; constructor(); } /** * Stable per operation, so an exact retry of a create resolves the same entry and * replays its receipt instead of creating a duplicate. */ export declare function knowledgeEntryIdForOperation(accountId: string, operationId: string): string; /** Freeze before model execution; retries of the logical turn recover the same policy. */ export declare function freezeAgentLearningPolicy(db: Database, context: KnowledgeContext): Promise<{ [x: string]: unknown; defaultScope: "personal" | "workspace"; subjectId: string | null; effective: { knowledge: "automatic" | "off" | "review_first"; instructions: "automatic" | "off" | "review_first"; skills: "automatic" | "off" | "review_first"; }; }>; export declare function saveKnowledgeEntry(db: Database, context: KnowledgeContext, input: KnowledgeEntrySaveRequest): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; export declare function promoteTaskNoteToKnowledge(db: Database, context: KnowledgeContext, input: KnowledgeTaskNotePromotionRequest): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; export declare function reviewKnowledgeEntry(db: Database, context: KnowledgeContext, input: KnowledgeEntryReviewRequest): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; /** Atomic review of exact revisions; dependencies publish before their findings. */ export declare function reviewKnowledgeEntries(db: Database, context: KnowledgeContext, input: KnowledgeEntryBatchReviewRequest): Promise<{ receipts: { operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }[]; }>; export declare function restoreKnowledgeEntry(db: Database, context: KnowledgeContext, input: KnowledgeEntryRestoreRequest): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; /** * Archiving a collection also archives what is only inside it: its * sub-collections and the entries that no live collection or entry outside it * holds or cites. Otherwise they surface at the top level and stay in use, so * the collection seems to come back. Undo and restore bring the same set back. */ export declare function archiveKnowledgeEntry(db: Database, context: KnowledgeContext, input: { operationId: string; entryId: string; expectedVersion: number; }): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; /** * Where an entry this workspace has works, and whether the person asking may * change it (migration 1196). Null when this workspace doesn't have the entry. * People and services only: agents use the entries their workspace has. */ export declare function getKnowledgeEntryReach(db: Database, context: KnowledgeContext, entryId: string): Promise; /** * Change where an organization or personal entry works. The caller has * checked the listed workspaces: shared workspaces of the organization for an * organization entry, the owner's own workspaces for a personal one. The * workspace it was added in always has it and is never stored. It is not a * revision, so the entry keeps its version; the change is audited in the same * transaction. People and services with write access to the entry's scope. */ export declare function setKnowledgeEntryReach(db: Database, context: KnowledgeContext, input: { entryId: string; reach: KnowledgeEntryReach; }): Promise; export declare function getKnowledgeEntry(db: Database, context: KnowledgeContext, entryId: string, options?: { revisionId?: string | undefined; view?: "published" | "needs_review" | "archived" | "rejected" | undefined; }): Promise; /** * Read a bounded set of current entries in input order. Every entry still goes * through the native actor, scope and transitive-evidence authorization seam; * only the transaction setup and round trips are shared. Missing or no longer * visible entries retain their null slot. This is not a historical revision read. */ export declare function getKnowledgeEntries(db: Database, context: KnowledgeContext, entryIds: string[], options?: { view?: "published" | "needs_review" | "archived" | "rejected" | undefined; }): Promise>; export declare function listKnowledgeEntries(db: Database, context: KnowledgeContext, input?: KnowledgeEntryListRequest, embedding?: { model: string; values: number[]; } | undefined): Promise<{ entries: { id: string; scope: "organization" | "personal" | "workspace"; version: number; publishedRevisionId: string | null; latestRevisionId: string; archived: boolean; createdAt: string; updatedAt: string; indexStatus?: "awaiting_funding" | "indexed" | "indexing" | "provider_failed" | "queued" | "saved" | "source_unavailable" | undefined; score?: number | undefined; lastChangedAt?: string | undefined; excerpts: { field: "content" | "title"; start: number; end: number; text: string; }[]; revision: { id: string; entryId: string; number: number; change: "archive" | "upsert"; previousRevisionId: string | null; restoredFromRevisionId: string | null; createdAt: string; createdBySessionId: string | null; reviewBatchId: string | null; outcome: "pending" | "published" | "rejected" | "superseded"; title: string; kind: "decision" | "fact" | "group" | "incident" | "note" | "requirement" | "source"; preview: string; groupIds: string[]; sourceKind: "connector" | "conversation" | "file" | "manual" | "repository" | "slack" | "task_note" | "web" | null; }; }[]; nextCursor: string | null; }>; export declare function listKnowledgeReviewBatches(db: Database, context: KnowledgeContext, input?: KnowledgeReviewBatchListRequest): Promise<{ batches: { id: string; sessionId: string | null; scheduledTaskId: string | null; scheduledTaskRunId: string | null; title: string | null; scope: "organization" | "personal" | "workspace"; pendingCount: number; createdAt: string; }[]; nextCursor: string | null; }>; export declare function listKnowledgeEntryHistory(db: Database, context: KnowledgeContext, entryId: string, beforeRevision?: number): Promise<{ entries: { id: string; scope: "organization" | "personal" | "workspace"; version: number; publishedRevisionId: string | null; latestRevisionId: string; archived: boolean; createdAt: string; updatedAt: string; indexStatus?: "awaiting_funding" | "indexed" | "indexing" | "provider_failed" | "queued" | "saved" | "source_unavailable" | undefined; score?: number | undefined; lastChangedAt?: string | undefined; excerpts: { field: "content" | "title"; start: number; end: number; text: string; }[]; revision: { id: string; entryId: string; number: number; change: "archive" | "upsert"; previousRevisionId: string | null; restoredFromRevisionId: string | null; createdAt: string; createdBySessionId: string | null; reviewBatchId: string | null; outcome: "pending" | "published" | "rejected" | "superseded"; title: string; kind: "decision" | "fact" | "group" | "incident" | "note" | "requirement" | "source"; preview: string; groupIds: string[]; sourceKind: "connector" | "conversation" | "file" | "manual" | "repository" | "slack" | "task_note" | "web" | null; }; }[]; beforeRevision: number | null; savedByYou: string[]; }>; export declare function getAgentLearningSettings(db: Database, context: KnowledgeContext, scope: "workspace" | "personal" | "context", source?: AgentLearningContext): Promise<{ ownerKey: string; contextKey: string; version: number; settings: { knowledge?: "automatic" | "inherit" | "off" | "review_first" | undefined; instructions?: "automatic" | "inherit" | "off" | "review_first" | undefined; skills?: "automatic" | "inherit" | "off" | "review_first" | undefined; }; }>; export declare function saveAgentLearningSettings(db: Database, context: KnowledgeContext, input: { scope: "workspace" | "personal"; source?: AgentLearningContext | undefined; operationId: string; expectedVersion: number; settings: AgentLearningOverridePatch; }): Promise<{ ownerKey: string; contextKey: string; version: number; settings: { knowledge?: "automatic" | "inherit" | "off" | "review_first" | undefined; instructions?: "automatic" | "inherit" | "off" | "review_first" | undefined; skills?: "automatic" | "inherit" | "off" | "review_first" | undefined; }; }>; export declare function listAgentLearningOverrides(db: Database, context: KnowledgeContext, scope: "workspace" | "personal"): Promise<{ contextKey: string; version: number; settings: { knowledge?: "automatic" | "inherit" | "off" | "review_first" | undefined; instructions?: "automatic" | "inherit" | "off" | "review_first" | undefined; skills?: "automatic" | "inherit" | "off" | "review_first" | undefined; }; updatedAt: string; label: string; }[]>; export declare function saveAgentInstruction(db: Database, context: KnowledgeContext, input: AgentInstructionSaveRequest): Promise<{ operationId: string; revisionId: string; outcome: "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; export declare function getAgentInstruction(db: Database, context: KnowledgeContext, target: WorkspaceInstructionPolicyTarget): Promise<{ target: { kind: "charter" | "policy"; scope: "global" | "role"; roleKey: string | null; }; expectedCurrentRevisionId: string | null; expectedActivationVersion: number; content: string | null; }>; export declare function reviewAgentInstruction(db: Database, context: KnowledgeContext, input: AgentInstructionReviewRequest): Promise<{ operationId: string; revisionId: string; outcome: "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; export declare function listAgentInstructionReviews(db: Database, context: KnowledgeContext, cursor?: string): Promise<{ entries: { revisionId: string; content: string; target: { kind: "charter" | "policy"; scope: "global" | "role"; roleKey: string | null; }; reviewBatchId: string | null; sessionId: string | null; reason: string; createdAt: string; }[]; nextCursor: string | null; }>; /** Host-only parser boundary. The file metadata is never returned by agent tools. */ export declare function inspectKnowledgeFilePreparation(db: Database, context: KnowledgeContext, fileId: string): Promise<{ status: "retained"; fileId: string; filename: string; receipt: { operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }; } | { status: "disabled"; fileId: string; } | { status: "prepare"; file: { scope?: "personal" | "workspace" | undefined; ownerSubjectId?: string | null | undefined; reachWorkspaceIds?: string[] | null | undefined; id: string; workspaceId: string; status: "deleted" | "expired" | "failed" | "pending_upload" | "ready"; filename: string; safeFilename: string; contentType: string; sizeBytes: number; sha256: string | null; bucket: string; objectKey: string; createdAt: string; updatedAt: string; access?: { visibility: "private" | "workspace"; whoCanOpen: string; } | undefined; appShot?: { app: string; bundleId?: string | undefined; title: string; capturedAt: string; platform: "linux" | "macos" | "windows"; text?: string | undefined; textTruncated?: boolean | undefined; window?: { processId: number; windowId: number; } | undefined; computer?: { name: string; sandboxId?: string | undefined; } | undefined; browser?: { url: string; profiles?: { deviceId: string; name: string; }[] | undefined; } | undefined; } | undefined; }; }>; /** Concurrent/recovered preparations converge on the first exact saved source. */ export declare function completeKnowledgeFilePreparation(db: Database, context: KnowledgeContext, input: { fileId: string; title: string; content: string; sourceVersion?: string; purpose?: "evidence" | "reference"; }): Promise<{ status: "retained"; fileId: string; filename: string; receipt: { operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }; } | { status: "disabled"; fileId: string; }>; export declare function knowledgeOriginalFileAsset(raw: Record): FileAsset; /** Exact entry visibility first; origin workspace metadata is not an access grant. */ export declare function getKnowledgeOriginalFile(db: Database, context: KnowledgeContext, entryId: string, revisionId?: string): Promise<{ scope?: "personal" | "workspace" | undefined; ownerSubjectId?: string | null | undefined; reachWorkspaceIds?: string[] | null | undefined; id: string; workspaceId: string; status: "deleted" | "expired" | "failed" | "pending_upload" | "ready"; filename: string; safeFilename: string; contentType: string; sizeBytes: number; sha256: string | null; bucket: string; objectKey: string; createdAt: string; updatedAt: string; access?: { visibility: "private" | "workspace"; whoCanOpen: string; } | undefined; appShot?: { app: string; bundleId?: string | undefined; title: string; capturedAt: string; platform: "linux" | "macos" | "windows"; text?: string | undefined; textTruncated?: boolean | undefined; window?: { processId: number; windowId: number; } | undefined; computer?: { name: string; sandboxId?: string | undefined; } | undefined; browser?: { url: string; profiles?: { deviceId: string; name: string; }[] | undefined; } | undefined; } | undefined; } | null>; /** Finish only a pre-cutover, human-answered remember confirmation. */ export declare function confirmLegacyKnowledge(db: Database, context: KnowledgeContext, request: { operationId: string; claimId: string; humanInputRequestId: string; }): Promise<{ operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; /** Compatibility only: finish an imported native instruction without rebaselining. */ export declare function confirmLegacyInstruction(db: Database, context: KnowledgeContext, request: { operationId: string; proposalId: string; decisionReceiptId: string; humanInputRequestId: string; }): Promise<{ operationId: string; revisionId: string; outcome: "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }>; /** Protocol recovery runs before the model, including when the retired tool is no longer selected. */ export declare function recoverLegacyKnowledgeConfirmations(db: Database, context: KnowledgeContext): Promise<{ receipts: { operationId: string; entryId: string; revisionId: string; version: number; outcome: "archived" | "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }[]; instructionReceipts: { operationId: string; revisionId: string; outcome: "pending" | "published" | "rejected"; reviewBatchId: string | null; replayed: boolean; }[]; unavailable: number; }>;