UNPKG

@mastra/core

Version:
152 lines • 7.35 kB
import { RequestContext } from '../request-context/index.js'; import type { MastraUser } from '../types/user.js'; export type AgentScopesSnapshot = { resourceId?: string; threadId?: string; /** * The full scope set the suspended run resolved, including identity scopes. Present (even * empty) means this is a resume: the set is authoritative and Agent configuration is ignored. */ scopes?: readonly string[]; }; /** * The Agent `scopes` option: a static list added to the run's incoming scopes, or a function that * receives the incoming scopes and returns the run's final set. * @experimental */ export type AgentScopesConfig<TRequestContext = unknown> = string[] | ((args: { /** The run's incoming scopes: the call's `scopes`, `defaultOptions.scopes` and `mastra__scopes`. */ scopes: string[]; requestContext: RequestContext<TRequestContext>; mastra?: any; /** The authenticated end user, when the request has one. */ user?: MastraUser; }) => string[] | Promise<string[]>); /** * Applies an Agent's `scopes` option to a run's incoming scopes: no option keeps them, a static * list adds to them, a function replaces them with what it returns. */ export declare function applyAgentScopesConfig<TRequestContext>(config: AgentScopesConfig<TRequestContext> | undefined, args: { scopes: string[]; requestContext: RequestContext<TRequestContext>; mastra?: unknown; }): Promise<string[]>; export type ResolveAgentScopesInput = { requestContext?: RequestContext; /** * The run's scopes after the Agent's `scopes` option ran on the incoming scopes (see * `incomingAgentScopes`). When given, it is the run's final set: request-context and call scopes * are not added again, but every `resource:`/`thread:` scope among them must still be in it. * Ignored on resume. */ agentScopes?: readonly string[]; /** * The incoming scopes `agentScopes` was computed from (see {@link incomingAgentScopes}), including * `defaultOptions.scopes`. Every `resource:`/`thread:` scope in it must survive the Agent's * function. */ incomingScopes?: readonly string[]; /** Scopes passed on the call. */ callScopes?: readonly string[]; memory?: { resource?: string; thread?: string | { id: string; }; }; resourceId?: string; threadId?: string; snapshot?: AgentScopesSnapshot; /** * The Mastra instance running the agent. When it has a registered server that does not keep * request bodies from supplying scopes, scopes from the call and the request context are refused. */ mastra?: { getMastraServer?(): unknown; }; }; export type ResolvedAgentScopes = { /** The run's full scope set, identity scopes included. */ scopes: string[]; resourceId?: string; threadId?: string; }; /** Sets `mastra__scopes` to scopes a run derived, marking them as written by Core. */ export declare function setRunAgentScopes(requestContext: RequestContext<any>, scopes: readonly string[]): void; /** * The run's non-identity scopes, as Core wrote them onto the run's request context. Scopes a host * or server supplies (agent config, call `scopes`, a `mastra__scopes` key on the caller's request * context) are honored when the run starts, by `resolveAgentScopes`. This reader is for code * running inside the run (tools, processors): it returns `[]` unless `mastra__scopes` is the exact * array Core marked, so an array written or replaced after resolution gives the run nothing. */ export declare function getRunAgentScopes(requestContext: { get(key: string): unknown; } | undefined): string[]; /** * Re-marks `mastra__scopes` on a context Core rebuilt from its own persisted run state (durable * snapshots, durable step input). The mark does not survive JSON, and these scopes already passed * resolution when the run started. */ export declare function restoreRunAgentScopes(requestContext: RequestContext<any>): void; /** Splits a scope address on its first colon. Returns undefined for a malformed address. */ export declare function parseAgentScope(scope: string): { type: string; value: string; } | undefined; /** * The scopes a run starts from before the Agent's `scopes` option: the reserved `mastra__scopes` * request-context key, the call's `scopes` and `defaultOptions.scopes`. */ export declare function incomingAgentScopes(input: { requestContext?: RequestContext; callScopes?: readonly string[]; defaultScopes?: readonly string[]; mastra?: ResolveAgentScopesInput['mastra']; }): string[]; /** * Resolves the scopes an agent run acts with, and the Memory resource/thread they imply. * * The incoming scopes (the reserved `mastra__scopes` request-context key and the call's `scopes`) * go through the Agent's `scopes` option first (`agentScopes`): a static list adds to them, a * function replaces them. `resource:` and `thread:` scopes set the * run's Memory identity; every other source of resource/thread must agree with them or the * call throws. Resource and thread resolve independently: when scopes carry neither, the * existing precedence applies (reserved ID key, then options, then memory, then snapshot). * * On resume, the snapshot's scope set is authoritative. Scopes from the call or request * context may be any subset of it; a scope the snapshot does not hold throws. The Agent's * configured scopes are validated but not applied, so changing them never blocks a resume. */ export declare function resolveAgentScopes(input: ResolveAgentScopesInput): ResolvedAgentScopes; /** Returns true when the scope is a Memory identity scope (`resource:` or `thread:`). */ export declare function isIdentityAgentScope(scope: string): boolean; /** The scopes a run passes downstream: its resolved set without `resource:`/`thread:`. */ export declare function withoutIdentityAgentScopes(scopes: readonly string[]): string[]; /** * Builds the request context a run hands to tools, processors, sub-agents and snapshots. * * The caller's context is never modified. When the run has scopes, or the caller's context * carries `mastra__scopes`, the run works on a copy whose `mastra__scopes` holds only the * non-identity scopes, so nested calls that pick a different thread or resource do not * conflict with the parent's identity. Otherwise the caller's context is returned as-is. */ export declare function deriveAgentRunRequestContext<T extends RequestContext<any>>(requestContext: T, scopes: readonly string[]): T; type ScopedRunOptions = { requestContext?: RequestContext<any>; memory?: { resource?: string; thread?: string | { id: string; }; }; }; /** * Returns the options a run executes with once its scopes are resolved. When scopes carry * `resource:`/`thread:`, `memory` is set from them so every downstream reader sees the same * identity. When the run has scopes, `requestContext` is the run-scoped copy from * `deriveAgentRunRequestContext`. Options for a run without scopes are returned unchanged. */ export declare function applyResolvedAgentScopes<T extends ScopedRunOptions>(options: T, resolved: ResolvedAgentScopes, requestContext: RequestContext<any>): T; export {}; //# sourceMappingURL=scopes.d.ts.map