UNPKG

@mastra/core

Version:
147 lines • 5.77 kB
/** * Factory function to create a DurableAgent that wraps an existing Agent. * * This is the recommended way to add durable execution capabilities to an agent. * The factory creates a DurableAgent instance with resumable streams. * * @example * ```typescript * import { Agent } from '@mastra/core/agent'; * import { createDurableAgent } from '@mastra/core/agent/durable'; * * const agent = new Agent({ * id: 'my-agent', * name: 'My Agent', * instructions: 'You are a helpful assistant', * model: openai('gpt-4'), * }); * * const durableAgent = createDurableAgent({ agent }); * * const mastra = new Mastra({ * agents: { myAgent: durableAgent }, * }); * ``` */ import type { MastraServerCache } from '../../cache/base.js'; import type { PubSub } from '../../events/pubsub.js'; import type { ShouldPersistSnapshotFn } from '../../workflows/types.js'; import type { Agent } from '../agent.js'; import { DurableAgent } from './durable-agent.js'; /** * Options for createDurableAgent factory function. */ export interface CreateDurableAgentOptions<TAgentId extends string = string, TTools extends Record<string, any> = Record<string, any>, TOutput = undefined> { /** The Agent to wrap with durable execution capabilities */ agent: Agent<TAgentId, TTools, TOutput>; /** Optional ID override (defaults to agent.id) */ id?: TAgentId; /** Optional name override (defaults to agent.name) */ name?: string; /** * Cache instance for storing stream events. * Enables resumable streams - clients can disconnect and reconnect * without missing events. * * - If not provided: Inherits from Mastra instance, or uses InMemoryServerCache * - If provided: Uses the provided cache backend (e.g., Redis) * - If set to `false`: Disables caching (streams are not resumable) */ cache?: MastraServerCache | false; /** * PubSub instance for streaming events. * Optional - if not provided, defaults to EventEmitterPubSub. */ pubsub?: PubSub; /** Maximum steps for agentic loop */ maxSteps?: number; /** * Auto-cleanup timer for durable stream state (ms). * Set to `0` to disable auto-cleanup. Defaults to `30_000` (30 seconds). */ cleanupTimeoutMs?: number; /** * Overrides the snapshot-persistence policy for the agent's durable * workflows. By default `pending | paused | suspended` snapshots are always * persisted (required for human-in-the-loop `resume()`), and `running` * checkpoints are persisted only when the Mastra instance sets * `recovery: { durableAgents: 'auto' }`. See * {@link DurableAgentConfig.shouldPersistSnapshot} for caveats. */ shouldPersistSnapshot?: ShouldPersistSnapshotFn; /** * How the agent's new runs are stored (`1` or `2`). Defaults to `1`. See * {@link DurableAgentConfig.storageVersion}. * * @experimental */ storageVersion?: 1 | 2; /** * Per-topic opt-out of the replay cache. * * Return `false` to publish a topic straight through to the underlying * PubSub without recording it in the cache. Subscribers of that topic then * receive live events only and cannot resume from an offset. Use this to * trade replay for minimum publish latency on hot topics when the cache is * remote (e.g. cross-region Redis). Run-local topics are always excluded, * regardless of this option. */ shouldCache?: (topic: string) => boolean; } /** * Create a DurableAgent that wraps an existing Agent. * * This factory function is the recommended way to add checkpoint-backed durable * execution to an agent. The `durable` option on `AgentConfig` only configures * resumable streams and never wraps the agent. * * Wrapping does not change the wrapped Agent's direct runs, including runs already * in flight. Without explicit stream settings, the wrapper shares the Agent's * stream bus; pubsub, cache, cleanupTimeoutMs, and shouldCache overrides apply * only to the wrapper's runs. Evented wrapper runs use Mastra's shared run log * without changing the wrapped Agent's native stream routing. * * @param options - Configuration options * @returns A DurableAgent instance * * @example * ```typescript * const agent = new Agent({ * id: 'my-agent', * instructions: 'You are helpful', * model: openai('gpt-4'), * }); * * const durableAgent = createDurableAgent({ agent }); * * const mastra = new Mastra({ * agents: { myAgent: durableAgent }, * }); * ``` * * @example * ```typescript * // Equivalent, using the durable config flag: * const agent = new Agent({ * id: 'my-agent', * instructions: 'You are helpful', * model: openai('gpt-4'), * durable: true, * }); * * const mastra = new Mastra({ agents: { myAgent: agent } }); * ``` */ export declare function createDurableAgent<TAgentId extends string = string, TTools extends Record<string, any> = Record<string, any>, TOutput = undefined>(options: CreateDurableAgentOptions<TAgentId, TTools, TOutput>): DurableAgent<TAgentId, TTools, TOutput>; /** * Check if an object is a DurableAgent */ export declare function isDurableAgent(obj: any): obj is DurableAgent; /** * Alias for isDurableAgent for backwards compatibility * @deprecated Use isDurableAgent instead */ export declare const isLocalDurableAgent: typeof isDurableAgent; export type { DurableAgentConfig, DurableAgentStreamOptions, DurableAgentStreamResult } from './durable-agent.js'; export type LocalDurableAgent<TAgentId extends string = string, TTools extends Record<string, any> = Record<string, any>, TOutput = undefined> = DurableAgent<TAgentId, TTools, TOutput>; //# sourceMappingURL=create-durable-agent.d.ts.map