@mastra/core
Version:
215 lines • 12.9 kB
TypeScript
import { MastraBase } from '../base.js';
import type { Mastra } from '../mastra/index.js';
import type { MastraCompositeStore } from '../storage/index.js';
import type { KnowledgeReadScope, KnowledgeStorage } from '../storage/domains/knowledge/index.js';
import type { KnowledgeNote, KnowledgePage } from '../storage/domains/knowledge/pages.js';
import type { ToolAction } from '../tools/index.js';
import type { KnowledgeChangeListener } from './changes.js';
import type { KnowledgeConfig } from './config.js';
import type { KnowledgeRole, KnowledgeScopeDeclarationInfo } from './declarations.js';
import type { KnowledgeAddNoteInput, KnowledgeMovePageInput, KnowledgeRemovePageInput, KnowledgeUpdateNoteInput, KnowledgeUpsertPageInput, KnowledgeUpsertPageResult } from './host-writes.js';
import type { PinEntry } from './pinned.js';
import type { KnowledgeReembedOptions, KnowledgeReembedResult } from './reembed.js';
import type { KnowledgeRunContext } from './run.js';
import type { KnowledgeAccessExplanation } from './scope-runtime.js';
/** Overrides which tool groups `createTools` and `getInstructions` cover. */
type KnowledgeToolSelection = {
read?: boolean;
write?: boolean;
pinnedContext?: boolean;
};
/** @experimental Knowledge APIs are experimental and may change without notice. */
export declare class Knowledge extends MastraBase {
#private;
readonly id: string;
readonly hasOwnStorage: boolean;
readonly description?: string;
constructor(config: KnowledgeConfig);
/**
* What a holder of `principals` reads: the principals themselves plus every scope their declared
* rules reach in one hop. Undeclared principals throw; an empty list reads nothing. Never `'all'`.
* Pass the result as `read` to the store's read methods.
*/
readScopeFor(principals: readonly string[]): Promise<KnowledgeReadScope>;
/**
* Why a run holding `runScopes` can or can't reach a scope or page: its role there and the rules
* that give it. `runScopes` are canonical run scopes, as passed to an agent run; undeclared ones throw
* like they do at run start. `target` is a scope address, or a page id or `<scope>#<key>[/<key>…]`.
*
* **Host-only.** It tells a missing page from a hidden one, which runs must never learn. Never
* expose it through an agent tool, or through a server route without admin-level auth.
*
* @experimental
*/
explainAccess(runScopes: readonly string[], target: {
scope: string;
} | {
page: string;
}): Promise<KnowledgeAccessExplanation>;
/**
* Who the declared rules give a role on one scope: each principal scope address (bound for this
* scope's params) with its strongest role, `self` rules left out. `parentAddress` is the
* address of the scope page's tree parent, needed for rules that bind through `belongsTo`. Reads the
* declarations as last resolved, not storage. For browsing UIs that draw access between scopes.
*
* @experimental
*/
scopeGrants(scope: {
address: string;
parentAddress?: string | null;
}): KnowledgeScopeGrant[];
/**
* After renaming a scope declaration key in `scopes`, moves the scopes stored under the old key
* (and its template children, `from/<name>`) to the new one, in one atomic step. Until then this
* instance refuses to run: stored scopes under an undeclared key would otherwise silently lose
* prefix and `belongsTo` reach. Refuses when `from` is still declared, `to` isn't, or any stored
* address doesn't match the `to` pattern; nothing changes then. Returns how many scopes moved.
*
* In a rolling deploy, instances with the new config fail until the rename runs, and instances
* still on the old config lose reach to those scopes after it; deploy, then rename right away.
*
* @experimental
*/
renameScopeDeclaration(from: string, to: string): Promise<number>;
/** Tools for explicit wiring; defaults to the groups configured on this Knowledge. */
createTools(options?: KnowledgeToolSelection): Record<string, ToolAction<any, any, any>>;
/**
* Creates or updates the page at `scope#path`, for apps that sync content in from outside an agent
* run (a docs site, a CMS). The path is the page's identity, so running a sync again updates the
* same pages; an unchanged page is not written. Notes on the page are kept. The page is embedded
* when Knowledge has `vector` and `embedder`, and change listeners are told.
*
* **Host-only.** No run scopes and no access rules are checked; who reads the page is decided by
* the scope's access rules as usual (`explainAccess` explains run access, not host writes). Never
* expose it through an agent tool, or through a server route without admin-level auth. A scope
* first created here gets its `belongsTo` parent when a run holding that parent first uses it.
*
* @experimental
*/
upsertPage(input: KnowledgeUpsertPageInput): Promise<KnowledgeUpsertPageResult>;
/**
* Deletes the page at `scope#path`; `null` when there is none. Refuses a page with child pages
* and a page declared in config. Host-only, like {@link Knowledge.upsertPage}. Not retried: a
* concurrent write to the same page between the read and the delete throws, so remove from one
* sync worker or retry the call.
*
* @experimental
*/
removePage(input: KnowledgeRemovePageInput): Promise<KnowledgePage | null>;
/**
* Moves the page at `scope#path`, with its subtree, under the page at `parent` (a key path; left
* out, the top of the scope), in `toScope` when given. The page keeps its id, notes, pins and
* vectors; paths below it change, and links by path are re-resolved. Throws
* `KnowledgeNotFoundError` for a missing page or parent, and refuses a page declared in config, a
* move into its own subtree, a key the new parent already has, and a move past the depth limit.
* Host-only, like {@link Knowledge.upsertPage}. Not retried: a concurrent write to the page
* between the read and the move throws.
*
* @experimental
*/
movePage(input: KnowledgeMovePageInput): Promise<KnowledgePage>;
/**
* Adds a note to the page at `scope#path`; throws when there is no page there. Readers of the
* `stamps` scopes (default: the page's scope) see it. Keep the returned id to edit or remove it.
* Host-only, like {@link Knowledge.upsertPage}.
*
* @experimental
*/
addNote(input: KnowledgeAddNoteInput): Promise<KnowledgeNote>;
/**
* Replaces a note's text. Notes are immutable, so this adds the new note and then removes the old
* one: the result has a **new id**. If removing the old note fails after the add, both exist;
* call `removeNote` with the old id. Host-only, like {@link Knowledge.upsertPage}.
*
* @experimental
*/
updateNote(input: KnowledgeUpdateNoteInput): Promise<KnowledgeNote>;
/**
* Removes a note; `null` when it is already gone. Host-only, like {@link Knowledge.upsertPage}.
*
* @experimental
*/
removeNote(input: {
id: string;
author?: string;
}): Promise<KnowledgeNote | null>;
/**
* Rebuilds the page link index from every page body, for pages written before the index existed
* or straight to the store. Needs no vector store. Returns how many pages were indexed.
*
* @experimental
*/
reindexLinks(): Promise<number>;
/**
* Embeds pages and notes that already exist, without changing them: content written before a
* `vector` and `embedder` were configured, or before switching embedders. Each call handles up to
* `limit` pages (default 50, at most 500) with their notes and returns `nextCursor`; pass it to the
* next call until it's absent. Handling a page again overwrites its vectors, so a run can stop and
* resume. Failures are listed, not thrown; if the embedder keeps rate limiting, the call stops
* early with `rateLimited: true` and a cursor that resumes where it stopped, mid-page if need be; wait before resuming. The
* cursor is not a snapshot: pages added or removed during a run can be missed or handled twice, and
* a page read before a concurrent edit can overwrite the vectors that edit wrote, so rerun after
* concurrent writes finish. Throws without `vector` and `embedder`.
*
* @experimental
*/
reembed(options?: KnowledgeReembedOptions): Promise<KnowledgeReembedResult>;
/** The usage block that goes with `createTools(options)`. */
getInstructions(options?: KnowledgeToolSelection): string;
/** Whether attached agents get pinned context delivered every turn. */
get pinnedContextEnabled(): boolean;
/** The run's principals as a stable key; throws when the run has none or holds an undeclared scope. @internal */
__principalsKey(context: KnowledgeRunContext): string;
/** The pinned pages a run receives this turn. @internal */
__deliveredPins(context: KnowledgeRunContext): Promise<PinEntry[]>;
/** @internal */
__registerMastra(_mastra: Mastra): void;
/** @internal */
__sharesStorageWith(other: Knowledge): boolean;
/** @internal */
__usesStorage(storage: MastraCompositeStore): boolean;
/** @internal */
setStorage(storage: MastraCompositeStore, source?: MastraCompositeStore): void;
getStorage(): Promise<KnowledgeStorage>;
/**
* Calls `listener` after each committed write made through this instance's store
* (`getStorage()`): tool writes, host writes and scope creation. Writes made on a store obtained
* some other way aren't seen. Events carry ids, not content and not access: anything that relays
* them to people must re-read each page under that person's read scope. Returns an unsubscribe.
*/
onChange(listener: KnowledgeChangeListener): () => void;
/**
* The declaration a scope page was created from, by its `declaration` key (template children
* included, e.g. `account:$orgId:$accountId/internal`), for display. Access rules are left out;
* use `explainAccess` for those. `undefined` for an unknown key.
*/
scopeDeclaration(key: string): KnowledgeScopeDeclarationInfo | undefined;
/**
* The display label for a canonical scope address (template children included), with the
* declaration's `$params` filled in from the address's decoded values: `'$orgId policy'` on
* `policy:$orgId` gives `'acme policy'` for `policy:acme`. A concrete address's own `displayLabel`
* wins over its pattern's. `undefined` when the address matches no declaration or has no label.
*/
scopeLabel(address: string): string | undefined;
}
export * from '../storage/domains/knowledge/index.js';
export type { KnowledgeInstructionParts, KnowledgeInstructionsOption } from './instructions.js';
/** Read-only readers over the schema-4 page store, for browsing UIs. */
export { DEFAULT_KNOWLEDGE_PAGE_GRAPH_LIMITS, listKnowledgeTreeChildren, MAX_KNOWLEDGE_EXPLORER_PAGE_LINKS, MAX_KNOWLEDGE_LINK_COUNT_PAGES, readKnowledgePage, readKnowledgePageLinkCounts, readKnowledgePageLinks, readKnowledgePageActivity, readKnowledgePageGraph, readKnowledgeScopeGrants, searchKnowledgeExplorerPages, } from './explorer-pages.js';
export type { KnowledgeGraphScopeGrant, KnowledgePageGraph, KnowledgePageGraphEdge, KnowledgePageGraphLimits, KnowledgePageLinks, KnowledgePageView, KnowledgeTreeChildren, KnowledgeTreePage, } from './explorer-pages.js';
export type { KnowledgeConfig } from './config.js';
export { knowledgeScopeAddress, knowledgeMemoryScopes, KnowledgeDeclarationError } from './declarations.js';
export { decodeKnowledgeScopeId, encodeKnowledgeScopeId } from './scope-address.js';
export type { KnowledgePageTemplate, KnowledgePinnedList, KnowledgeScopeDeclaration, KnowledgeScopeDeclarationInfo, KnowledgeScopeDeclarations, KnowledgeAccessRule, KnowledgeRole, } from './declarations.js';
export type { KnowledgeAccessExplanation } from './scope-runtime.js';
export type { KnowledgeAccessGrant } from './access.js';
/** One principal scope's strongest declared role on a scope. See {@link Knowledge.scopeGrants}. @experimental */
export interface KnowledgeScopeGrant {
/** The principal scope address, e.g. `role:acme:support`. */
principal: string;
role: KnowledgeRole;
}
export type { KnowledgeChangeEvent, KnowledgeChangeListener } from './changes.js';
export type { KnowledgeReembedOptions, KnowledgeReembedResult } from './reembed.js';
export type { KnowledgeAddNoteInput, KnowledgeMovePageInput, KnowledgeRemovePageInput, KnowledgeUpdateNoteInput, KnowledgeUpsertPageInput, KnowledgeUpsertPageResult, } from './host-writes.js';
//# sourceMappingURL=index.d.ts.map