@mastra/core
Version:
53 lines • 3.15 kB
TypeScript
/**
* Implementation of `DurableAgent.streamUntilIdle` and
* `DurableAgent.resume(..., { untilIdle })`. Mirrors the regular agent's
* `stream-until-idle.ts` (shared `runWithIdleWrapper` + thin delegates per
* entry point) but adapted for durable execution:
* - `DurableAgent.stream()` returns `DurableAgentStreamResult` (not `MastraModelOutput`)
* - Each continuation starts a new durable workflow (new runId)
* - Cleanup functions from each inner stream are tracked and called on close
* - Inner `abort()` handles are fanned out so the outer `result.abort()`
* cancels every active durable run
*
* High-level flow:
* 1. Resolve memory scope (threadId, resourceId) -- falls through to the
* plain underlying call if no memory or bgManager.
* 2. Register this call as the active wrapper for the scope, aborting any
* prior wrapper.
* 3. Run initial turn via the `firstTurn` callback (either `agent.stream(...)`
* or `agent.resume(...)`) and pipe its `fullStream` into a combined outer
* stream.
* 4. Subscribe to `bgManager.stream(...)` for background task lifecycle
* events. On terminal events, queue a continuation (always via
* `agent.stream([], continuationOpts)` — we're back in normal multi-turn
* flow at that point).
* 5. `maxIdleMs` fires only between turns when nothing is happening.
*/
import type { BackgroundTaskManager } from '../../background-tasks/manager.js';
import type { MessageListInput } from '../message-list/index.js';
import type { DurableAgent, DurableAgentStreamOptions, DurableAgentStreamResult } from './durable-agent.js';
export interface DurableStreamUntilIdleDeps {
activeStreams: Map<string, () => void>;
bgManager: BackgroundTaskManager | undefined;
}
/**
* Run `DurableAgent.streamUntilIdle` (or `DurableAgent.stream({ untilIdle })`).
* Initial turn invokes `agent.stream(messages, ...)`; continuations triggered
* by background-task completions run as fresh `agent.stream([], ...)` calls
* against the same memory thread.
*/
export declare function runDurableStreamUntilIdle<OUTPUT = undefined>(agent: DurableAgent<any, any, OUTPUT>, messages: MessageListInput, streamOptions: (DurableAgentStreamOptions<OUTPUT> & {
maxIdleMs?: number;
}) | undefined, deps: DurableStreamUntilIdleDeps): Promise<DurableAgentStreamResult<OUTPUT>>;
/**
* Run `DurableAgent.resume(..., { untilIdle })`. Same idle-loop semantics as
* `runDurableStreamUntilIdle` — initial turn calls `agent.resume(runId,
* resumeData, ...)` against the existing run snapshot, and subsequent
* continuations triggered by background-task completions use
* `agent.stream([], continuationOpts)` (a normal multi-turn agent stream)
* since the resume completes and we're back in regular conversation flow.
*/
export declare function runResumeDurableStreamUntilIdle<OUTPUT = undefined>(agent: DurableAgent<any, any, OUTPUT>, runId: string, resumeData: unknown, streamOptions: (DurableAgentStreamOptions<OUTPUT> & {
maxIdleMs?: number;
}) | undefined, deps: DurableStreamUntilIdleDeps): Promise<DurableAgentStreamResult<OUTPUT>>;
//# sourceMappingURL=durable-stream-until-idle.d.ts.map