@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
137 lines • 5.92 kB
TypeScript
type RecordToTuple<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T][];
/**
* Reserved key for setting resourceId from middleware.
* When set in RequestContext, this takes precedence over client-provided values
* for security (prevents attackers from hijacking another user's memory).
*
* @example
* ```typescript
* // In your auth middleware:
* const requestContext = c.get('requestContext');
* requestContext.set(MASTRA_RESOURCE_ID_KEY, authenticatedUser.id);
* ```
*/
export declare const MASTRA_RESOURCE_ID_KEY = "mastra__resourceId";
/**
* Reserved key for setting threadId from middleware.
* When set in RequestContext, this takes precedence over client-provided values
* for security (prevents attackers from hijacking another user's memory).
*
* @example
* ```typescript
* // In your auth middleware:
* const requestContext = c.get('requestContext');
* requestContext.set(MASTRA_THREAD_ID_KEY, threadId);
* ```
*/
export declare const MASTRA_THREAD_ID_KEY = "mastra__threadId";
/**
* Reserved key for storing version overrides on RequestContext.
* When set, sub-agent delegation resolves versioned agents from these overrides.
*
* @example
* ```typescript
* requestContext.set(MASTRA_VERSIONS_KEY, {
* agents: { 'researcher-agent': { versionId: '123' } },
* });
* ```
*/
export declare const MASTRA_VERSIONS_KEY = "mastra__versions";
/**
* Reserved key for storing the raw auth token from the incoming request.
* Used by the editor to forward authentication when connecting to MCP servers
* that require the same auth as the Mastra server itself.
*/
export declare const MASTRA_AUTH_TOKEN_KEY = "mastra__authToken";
export type { VersionOverrides, VersionSelector } from '../mastra/types.js';
export { mergeVersionOverrides } from '../mastra/types.js';
export declare class RequestContext<Values extends Record<string, any> | unknown = unknown> {
private registry;
constructor(iterable?: Values extends Record<string, any> ? RecordToTuple<Partial<Values>> : Iterable<readonly [string, unknown]>);
/**
* set a value with strict typing if `Values` is a Record and the key exists in it.
*/
set<K extends Values extends Record<string, any> ? keyof Values : string>(key: K, value: Values extends Record<string, any> ? (K extends keyof Values ? Values[K] : never) : unknown): void;
/**
* Get a value with its type
*/
get<K extends Values extends Record<string, any> ? keyof Values : string, R = Values extends Record<string, any> ? (K extends keyof Values ? Values[K] : never) : unknown>(key: K): R;
/**
* Check if a key exists in the container
*/
has<K extends Values extends Record<string, any> ? keyof Values : string>(key: K): boolean;
/**
* Delete a value by key
*/
delete<K extends Values extends Record<string, any> ? keyof Values : string>(key: K): boolean;
/**
* Clear all values from the container
*/
clear(): void;
/**
* Get all keys in the container
*/
keys(): IterableIterator<Values extends Record<string, any> ? keyof Values : string>;
/**
* Get all values in the container
*/
values(): IterableIterator<Values extends Record<string, any> ? Values[keyof Values] : unknown>;
/**
* Get all entries in the container.
* Returns a discriminated union of tuples for proper type narrowing when iterating.
*/
entries(): IterableIterator<Values extends Record<string, any> ? {
[K in keyof Values]: [K, Values[K]];
}[keyof Values] : [string, unknown]>;
/**
* Get the size of the container
*/
size(): number;
/**
* Execute a function for each entry in the container.
* The callback receives properly typed key-value pairs.
*/
forEach<K extends Values extends Record<string, any> ? keyof Values : string>(callbackfn: (value: Values extends Record<string, any> ? (K extends keyof Values ? Values[K] : unknown) : unknown, key: K, map: Map<string, unknown>) => void): void;
/**
* Custom JSON serialization method.
* Converts the internal Map to a plain object for proper JSON serialization.
* Non-serializable values (functions, symbols, RPC proxies, in-value
* circular references, and values whose serialization re-enters this
* `toJSON` via cross-context back-references) are skipped to prevent
* serialization errors when storing to database.
*
* Reentry safety: if a stored value's `isSerializable` probe re-enters
* `toJSON()` on this same instance (through a chain of RequestContexts
* holding references to each other), we throw `CyclicRequestContextToJSONError`.
* Inner `isSerializable` calls re-throw the marker; the outermost
* `isSerializable` swallows it and filters the offending key, the same
* way it filters in-value circular references today.
*/
toJSON(): Record<string, any>;
/**
* Check if a value can be safely serialized to JSON.
*
* Re-throws `CyclicRequestContextToJSONError` when called from a nested
* `toJSON()` (`_toJSONDepth > 1`), so the marker propagates up to the
* outermost `toJSON()`'s `isSerializable`, which then swallows it and
* filters the offending key. This is what lets the outermost call return
* a clean JSON-safe dict for cross-context cycles.
*/
private isSerializable;
/**
* Get all values as a typed object for destructuring.
* Returns Record<string, any> when untyped, or the Values type when typed.
*
* @example
* ```typescript
* const ctx = new RequestContext<{ userId: string; apiKey: string }>();
* ctx.set('userId', 'user-123');
* ctx.set('apiKey', 'key-456');
* const { userId, apiKey } = ctx.all;
* ```
*/
get all(): Values extends Record<string, any> ? Values : Record<string, any>;
}
//# sourceMappingURL=index.d.ts.map