@mastra/core
Version:
104 lines • 7.49 kB
TypeScript
/**
* Durable Agent Module
*
* This module provides durable execution patterns for AI agents with
* resumable streams. If a client disconnects and reconnects, they can
* receive missed events from the cache.
*
* ## Factory Functions
*
* - `createDurableAgent({ agent })` - Local execution with resumable streams
* - `createEventedAgent({ agent })` - Built-in evented workflow engine (fire-and-forget)
* - `createInngestAgent({ agent, inngest })` - Inngest durable execution (from @mastra/inngest)
*
* ## Class Hierarchy
*
* - `DurableAgent` extends `Agent` - Base durable agent with resumable streams
* - `EventedAgent` extends `DurableAgent` - Fire-and-forget execution
* - `InngestAgent` extends `DurableAgent` - Inngest-powered execution (from @mastra/inngest)
*
* ## Features
*
* 1. **Resumable Streams**: Events are cached, allowing reconnection without missing data
* 2. **Pluggable Cache**: Use InMemoryServerCache (default) or custom backends (Redis, etc.)
* 3. **Cache Inheritance**: Durable agents inherit cache from Mastra if not explicitly provided
* 4. **Durable Execution**: Run agentic loops on workflow engines (Inngest, evented, etc.)
* 5. **Crash Recovery**: In-flight runs persist their state; a fresh process over the
* same storage can resume them via `recover(runId)` / `recoverActiveRuns()`
*
* @example Basic usage with resumable streams
* ```typescript
* import { Agent } from '@mastra/core/agent';
* import { createDurableAgent } from '@mastra/core/agent/durable';
*
* const agent = new Agent({
* id: 'my-agent',
* instructions: 'You are a helpful assistant',
* model: openai('gpt-4'),
* });
*
* // Wrap with resumable streams
* const durableAgent = createDurableAgent({ agent });
*
* const { output, runId, cleanup } = await durableAgent.stream('Hello!');
* const text = await output.text;
* cleanup();
* ```
*
* @example Custom cache backend (e.g., Redis)
* ```typescript
* import { RedisServerCache } from '@mastra/redis';
* import Redis from 'ioredis';
*
* const durableAgent = createDurableAgent({
* agent,
* cache: new RedisServerCache({ client: new Redis('redis://...') }),
* });
* ```
*
* @example Cache inheritance from Mastra
* ```typescript
* const mastra = new Mastra({
* cache: new RedisServerCache({ client: new Redis('redis://...') }),
* agents: {
* myAgent: createDurableAgent({ agent }), // Inherits Redis cache from Mastra
* },
* });
* ```
*/
export { createDurableAgent, isDurableAgent, isLocalDurableAgent, // Backwards compatibility alias
type CreateDurableAgentOptions, type LocalDurableAgent, } from './create-durable-agent.js';
export { DurableAgent, applyReservedMemoryContextKeys, type DurableAgentConfig, type DurableAgentStreamOptions, type DurableAgentStreamResult, } from './durable-agent.js';
export { EventedAgent, isEventedAgentClass, type EventedAgentConfig } from './evented-agent.js';
export { createEventedAgent, isEventedAgent, type CreateEventedAgentOptions } from './create-evented-agent.js';
export { runDurableStreamUntilIdle, runResumeDurableStreamUntilIdle, type DurableStreamUntilIdleDeps, } from './durable-stream-until-idle.js';
export { getRunStreamSlot, getScopeStreamSlot, untilIdleHandlesBackgroundTask, } from '../../loop/shared/stream-until-idle-helpers.js';
export { prepareForDurableExecution, type PreparationOptions, type PreparationResult } from './preparation.js';
export { persistDurableRunInput } from './persist-run-input.js';
export { validateModelTimeoutSettings } from '../../llm/model/model-settings.js';
export { RunRegistry, ExtendedRunRegistry, globalRunRegistry, getRunRegistryEntry, setRunRegistryEntry, deleteRunRegistryEntry, takeRunScorerInstances, type ExtendedRunRegistryEntry, } from './run-registry.js';
export { normalizeResumeGenerateArgs } from './utils/resume-generate-args.js';
export { __resetExecutionFencesForTests } from './execution-fence.js';
export { agentThreadStreamRuntime } from '../thread-stream-runtime.js';
export { createRunAbortController, type RunAbortController } from './run-abort.js';
export { AgentStreamBus } from '../streams/bus.js';
export { diffResponseMessages, type ResponseMessagesCursor } from '../streams/response-messages.js';
export { createDurableAgentStream, emitChunkEvent, emitStepStartEvent, emitStepFinishEvent, emitFinishEvent, emitErrorEvent, emitAbortEvent, emitSuspendedEvent, type DurableAgentStreamOptions as StreamAdapterOptions, type DurableAgentStreamResult as StreamAdapterResult, } from './stream-adapter.js';
export { detachFullStreamOnEnd } from './observe-full-stream.js';
export { AGENT_STREAM_TOPIC, AGENT_CONTROL_TOPIC, AgentStreamEventTypes, AgentControlEventTypes, DURABLE_STOP_WHEN_ERROR_ID, DurableAgentDefaults, DurableStepIds, } from './constants.js';
export { publishAbortRequest, requestAcknowledgedAbort, subscribeToAbortRequests, ensureRemoteAbortListener, readRunDeadline, RUN_DEADLINE_KEY, } from './abort-transport.js';
export type { RunDeadline } from './abort-transport.js';
export type { SerializableToolMetadata, SerializableModelConfig, SerializableDurableState, SerializableDurableOptions, SerializableModelSettings, DurableAgenticWorkflowInput, DurableLLMStepOutput, DurableToolCallInput, DurableToolCallOutput, DurableAgenticExecutionOutput, DurableAgenticLoopOutput, AgentStreamEventType, AgentStreamEvent, AgentChunkEventData, AgentStepFinishEventData, DurableAgentStepFinishResult, AgentFinishEventData, AgentErrorEventData, AgentSuspendedEventData, RunRegistryEntry, DurableStepContext, } from './types.js';
export { createWorkflowInput, serializeToolsMetadata, serializeModelConfig, serializeDurableState, serializeDurableOptions, } from './utils/serialize-state.js';
export { resolveRuntimeDependencies, rebuildRunToolsFromMastra, resolveModel, resolveInternalState, resolveTool, toolRequiresApproval, type ResolvedRuntimeDependencies, type ResolveRuntimeOptions, type RebuiltRunTools, } from './utils/resolve-runtime.js';
export { createDurableAgenticWorkflow, runDurableFinishSideEffects, type DurableAgenticWorkflowOptions, type DurableFinishSideEffectsOptions, type DurableFinishSideEffectsResult, } from './workflows/index.js';
export { createDurableBackgroundTaskCheckStep, createDurableLLMExecutionStep, createDurableToolCallStep, createDurableLLMMappingStep, createDurableIsTaskCompleteStep, createDurableGoalStep, } from './workflows/steps/index.js';
export { executeDurableAgentScorers, loadRunStoredMessages, modelConfigSchema, modelListEntrySchema, accumulatedUsageSchema, durableAgenticOutputSchema, baseDurableAgenticInputSchema, baseIterationStateSchema, calculateAccumulatedUsage, buildStepRecord, createBaseIterationStateUpdate, resolveDurableToolCallConcurrency, } from './workflows/shared/index.js';
export type { ExecuteDurableAgentScorersParams, BaseIterationState, AccumulatedUsage, IterationStateUpdateInput, StepRecord, } from './workflows/shared/index.js';
export { executeDurableToolCalls } from './workflows/deprecated-tool-execution.js';
export type { ToolExecutionContext, ToolExecutionError } from './workflows/deprecated-tool-execution.js';
export { pruneAgentLoopSnapshot } from '../../loop/workflows/prune-snapshot.js';
export { endRunSpansWithError } from './run-registry.js';
/** @internal Runtime instrumentation helpers. */
export { agentLoopTermination, executionFailureTermination } from '../../loop/shared/execution-outcome.js';
//# sourceMappingURL=index.d.ts.map