@mastra/core
Version:
215 lines • 8.77 kB
TypeScript
/**
* Knowledge scope declarations: the scope types (patterns such as `team:$orgId:$teamId`) and concrete
* addresses a Knowledge instance knows, who gets which role on them, where their pages sit in the
* tree, and which child scopes, pages and pins come with them. Compiled and checked once at
* construction so typos fail at startup instead of silently granting nothing.
*
* @experimental Knowledge APIs are experimental and may change without notice.
*/
/** @experimental */
export type KnowledgeRole = 'viewer' | 'contributor' | 'editor' | 'owner';
/** Roles from weakest to strongest; a role implies every role before it. @experimental */
export declare const KNOWLEDGE_ROLES: readonly KnowledgeRole[];
/** @experimental */
export declare function knowledgeRoleAtLeast(role: KnowledgeRole | null | undefined, min: KnowledgeRole): boolean;
/** @experimental */
export declare function strongerKnowledgeRole(a: KnowledgeRole | null | undefined, b: KnowledgeRole): KnowledgeRole;
/**
* Runs holding `principal` get `role` on the declared scope. `self` is the scope itself; `$params` in
* the principal are bound from the scope's address, or through its `belongsTo`.
*
* @experimental
*/
export interface KnowledgeAccessRule {
principal: 'self' | (string & {});
role: KnowledgeRole;
}
/** @experimental */
export interface KnowledgeScopeDeclaration {
/** Shown to the model and in UIs. */
description?: string;
/** A short name for the scope type in UIs, e.g. `Organization`. Not shown to the model. */
displayLabel?: string;
access?: KnowledgeAccessRule[];
/** Tree placement only; grants nothing. A scope pattern, e.g. `org:$orgId`. */
belongsTo?: string;
/** Child scopes created with this scope, at `<address>/<name>`. */
children?: Array<{
name: string;
description?: string;
displayLabel?: string;
access?: KnowledgeAccessRule[];
}>;
/** Keyed pages created with this scope; they may nest (`pages` inside a page, same scope). */
pages?: KnowledgePageTemplate[];
/**
* Pinned into the context of every run holding this scope: pages by ref (`<scope>#<key path>`), or
* listings of the page tree under a scope or page ({@link KnowledgePinnedList}).
*/
pinnedContext?: Array<string | KnowledgePinnedList>;
/** Ordering of stored pins delivered through this scope; higher first. Default 0. */
pinnedContextPriority?: number;
}
/**
* What `knowledge.scopeDeclaration(key)` returns: a declaration's display fields, without access rules.
* @experimental
*/
export interface KnowledgeScopeDeclarationInfo {
key: string;
description?: string;
displayLabel?: string;
belongsTo?: string;
children: Array<{
key: string;
name: string;
description?: string;
displayLabel?: string;
}>;
}
/**
* A pinned listing: the readable pages under `list`, as `knowledge_list` returns them (ref, name and
* child count each), kept current in the run's pinned context.
* @experimental
*/
export interface KnowledgePinnedList {
/** A scope (`docs:$orgId`) or a keyed page under one (`docs:$orgId#guides`). */
list: string;
/** Levels below `list` to include, 1 to 3. Default 1. */
depth?: number;
}
/**
* A pinnedContext entry as scope pattern and key path (empty for a listing of the scope itself), or
* null when it isn't one.
*/
export declare function knowledgePinTarget(entry: string | KnowledgePinnedList): {
scope: string;
path: string[];
depth?: number;
} | null;
/**
* A keyed page created with its scope. `key` is unique among its siblings and can't contain `/`, `#`
* or `%`; nested `pages` sit under it, addressed `<scope>#<key>/<child key>`.
* @experimental
*/
export interface KnowledgePageTemplate {
key: string;
name: string;
description?: string;
pages?: KnowledgePageTemplate[];
}
/** @experimental */
export type KnowledgeScopeDeclarations = Record<string, KnowledgeScopeDeclaration>;
/** @experimental */
export declare class KnowledgeDeclarationError extends Error {
constructor(message: string);
}
/**
* Declarations for Memory's identity scopes and a tenant scope: `thread:$threadId` (self owner,
* placed under the resource), `resource:$resourceId` (self owner, placed under the tenant) and the
* tenant pattern (default `org:$orgId`, self editor). `org: false` leaves the tenant out; `orgRole:
* 'contributor'` makes the tenant append-only.
*
* @experimental
*/
export declare function knowledgeMemoryScopes(options?: {
org?: string | false;
orgRole?: 'editor' | 'contributor';
}): KnowledgeScopeDeclarations;
type Segment = {
param: string;
} | {
literal: string;
};
/** A parsed scope pattern or concrete address. */
export interface KnowledgeScopePattern {
key: string;
type: string;
segments: Segment[];
params: string[];
}
/** How a rule's principal reaches its target scopes. */
export type KnowledgeRuleReach =
/** The principal is the scope itself. */
{
kind: 'self';
}
/** The target's params bound from the principal give an exact address (concrete target or every param bound). */
| {
kind: 'exact';
}
/** The principal binds a leading run of the target's params: every target address with that prefix. */
| {
kind: 'prefix';
}
/** The principal binds the target's `belongsTo`: every target scope placed under that scope. */
| {
kind: 'belongsTo';
};
export interface CompiledKnowledgeRule {
/** Null for `self`. */
principal: KnowledgeScopePattern | null;
role: KnowledgeRole;
reach: KnowledgeRuleReach;
}
export interface CompiledKnowledgeDeclaration {
/** The declaration key, stored on materialized scope pages. */
key: string;
pattern: KnowledgeScopePattern;
declaration: KnowledgeScopeDeclaration;
rules: CompiledKnowledgeRule[];
belongsTo?: KnowledgeScopePattern;
/** Set on template children: the parent declaration and the child's name. */
child?: {
parent: CompiledKnowledgeDeclaration;
name: string;
};
children: CompiledKnowledgeDeclaration[];
}
export interface KnowledgeScopeMatch {
declaration: string;
compiled: CompiledKnowledgeDeclaration;
params: Record<string, string>;
/** Extra rules from a concrete-address declaration of this exact address. */
concreteRules: CompiledKnowledgeRule[];
concrete?: KnowledgeScopeDeclaration;
}
export interface CompiledKnowledgeScopes {
/** Every declaration, including template children, by key. */
declarations: Map<string, CompiledKnowledgeDeclaration>;
/** Concrete-address declarations and their compiled rules, by address. */
concrete: Map<string, {
declaration: KnowledgeScopeDeclaration;
rules: CompiledKnowledgeRule[];
}>;
match(address: string): KnowledgeScopeMatch | null;
}
/** Binds raw param values from a canonical address; null when it isn't canonical or doesn't fit. */
export declare function matchKnowledgePattern(pattern: KnowledgeScopePattern, address: string): Record<string, string> | null;
/**
* A canonical address from raw param values, the form Knowledge APIs, pins, tools and Agent scopes take.
* @experimental
*/
export declare function knowledgeScopeAddress(pattern: string, params: Record<string, string>): string;
/**
* Agent scopes are canonical addresses, the form Knowledge stores and `encodeKnowledgeScopeId` builds, so
* each scope has one spelling: `%` is always an escape and a raw `:`, `/` or `#` inside a value is refused. @internal
*/
export declare function knowledgeRunScopes(compiled: CompiledKnowledgeScopes, scopes: readonly string[]): string[];
/** Encodes raw param values into a pattern; null when a value is missing or empty. */
export declare function bindKnowledgePattern(pattern: KnowledgeScopePattern, params: Record<string, string>): string | null;
/**
* The address prefix a partial binding reaches: segments up to the last bound param, ending at a
* segment boundary. Only valid for a leading run of bound params; reserved characters in raw
* values (including colons) are encoded so they cannot change tenant or segment boundaries.
*/
export declare function knowledgePatternPrefix(pattern: KnowledgeScopePattern, params: Record<string, string>): string | null;
/**
* Compiles and checks declarations: disjoint patterns per type, valid keys, every rule principal of a
* declared type with params the target can bind, and every rule reach indexable (an address prefix
* or a `belongsTo` parent).
*
* @experimental
*/
export declare function compileKnowledgeScopes(declarations: KnowledgeScopeDeclarations): CompiledKnowledgeScopes;
export {};
//# sourceMappingURL=declarations.d.ts.map