UNPKG

@mastra/core

Version:
123 lines • 6.54 kB
import type { ToolSet } from '../_types/@internal_ai-sdk-v5@0.0.98/dist/index.js'; import type { ChunkType } from '../stream/types.js'; import type { createEventedWorkflow } from '../workflows/create.js'; import { createWorkflow } from '../workflows/create.js'; import type { Step } from '../workflows/step.js'; import type { OutputWriter } from '../workflows/types.js'; import type { Workflow } from '../workflows/workflow.js'; import type { LoopRuntime } from './loop-runtime.js'; import type { LoopRun } from './types.js'; import { EagerToolExecutionCoordinator } from './workflows/agentic-execution/eager-tool-execution.js'; import type { ToolCallForeachOptions } from './workflows/agentic-execution/tool-call-concurrency.js'; export declare const AGENTIC_EXECUTION_WORKFLOW_ID = "executionWorkflow"; export declare const AGENTIC_LOOP_WORKFLOW_ID = "agentic-loop"; /** * Per-run params the main loop is built from. Mirrors the shape historically * accepted by `createAgenticLoopWorkflow` (a `LoopRun` plus the live stream * controller and output writer). */ export interface AgenticLoopBuilderParams<Tools extends ToolSet = ToolSet, OUTPUT = undefined> extends LoopRun<Tools, OUTPUT> { controller: ReadableStreamDefaultController<ChunkType<OUTPUT>>; outputWriter: OutputWriter; } /** * Predicate evaluated by the outer dowhile after every iteration. Receives the * engine's dowhile callback params; typed loosely because the two engines * consume different subsets of it (the main loop only reads `inputData`, the * durable loop also reads `getInitData`/`mastra`/the pubsub symbol). */ export type LoopContinuationPredicate = (params: any) => Promise<boolean>; /** * Engine-agnostic handle for a loop step. The two engines build steps with * different ids and schemas (state flows through workflow context on the main * loop vs. serialized iteration state on the durable loop), so the overridable * factory surface is typed loosely; each engine's composition works with its * own concrete step types internally. */ export type LoopStep = Step<string, any, any, any, any, any, any, any>; /** Engine-agnostic handle for a composed loop workflow (see {@link LoopStep}). */ export type LoopWorkflow = Workflow<any, any, any, any, any, any, any, any>; /** * Builds the agentic loop: an outer `dowhile` workflow wrapping a * single-iteration workflow of ten steps (LLM execution → tool calls → * mapping → background check → signal drain → isTaskComplete → goal). * * This concrete base class IS the main (in-process) agent loop. Every step is * constructed through an overridable method so engine variants (the durable * loop) can subclass it and override behavior surgically instead of * maintaining a parallel copy of the topology. During the incremental * migration the durable subclass still overrides whole methods that * delegate to its own step files; the end state is that only runtime hooks * (state resolution, chunk transport, workflow engine) remain overridden. */ export declare class AgenticLoopBuilder<Tools extends ToolSet = ToolSet, OUTPUT = undefined> { #private; /** * The main loop resolves everything from per-run params captured at * construction time. Engine subclasses that resolve their runtime elsewhere * (e.g. the durable loop's run registry + serialized state) construct * without params — any base method that then touches `this.params` throws, * so an incompletely-overridden subclass fails loudly instead of silently * running main-loop plumbing. */ constructor(params?: AgenticLoopBuilderParams<Tools, OUTPUT>); protected get params(): AgenticLoopBuilderParams<Tools, OUTPUT>; /** * Which workflow engine the loop is composed on. The durable subclass * returns `createEventedWorkflow` when the evented engine is selected. */ protected workflowFactory(): typeof createWorkflow | typeof createEventedWorkflow; /** * Resolve the live, non-serializable view of the run that predicate (and, * as they hoist, step) bodies operate through — see {@link LoopRuntime}. * The main loop resolves everything from per-run params and RunScope; the * durable subclass resolves from serialized iteration state + the run * registry. * * `drainPendingSignals` is normalized here: each engine * pre-binds `runId`, so consumers call `rt.drainPendingSignals?.()` and the * underlying runtime defaults the scope to `'pending'`. */ protected resolveRuntime(_predicateParams: unknown): LoopRuntime; /** * Transport hook: deliver a chunk to the run's client-facing stream. The * main loop enqueues onto the live stream controller (closed-controller * safe); the durable subclass publishes through pubsub. */ protected emitChunk(_runtime: LoopRuntime, chunk: unknown): void | Promise<void>; protected llmExecutionStep(toolCallForeachOptions: ToolCallForeachOptions, eager?: { coordinator: EagerToolExecutionCoordinator; toolCallStep: LoopStep; }): LoopStep; protected toolCallStep(): LoopStep; protected llmMappingStep(llmExecutionStep: LoopStep): LoopStep; protected backgroundTaskCheckStep(): LoopStep; /** * Step ⑦ — drain signals queued while the iteration was running, seal and * rotate the response message, and force continuation so the LLM sees them. * Behavior lives in `drainSignalsToTranscript` (shared with the durable * override and both predicates' inline drains); this method * owns the main loop's live-`MessageList` plumbing and output projection. */ protected signalDrainStep(): LoopStep; protected isTaskCompleteStep(): LoopStep; protected goalStep(): LoopStep; /** * The single-iteration workflow: LLM execution → tool-call foreach → * mapping → background check → signal drain → isTaskComplete → goal. */ buildIterationWorkflow(): LoopWorkflow; /** * The dowhile predicate: decides after each iteration whether the loop * continues. Owns resume seal-and-rotate (#19445), inter-iteration signal * draining, stopWhen evaluation, the onIterationComplete hook, delegation * bail, and step accumulation. */ protected buildContinuationPredicate(): LoopContinuationPredicate; /** * The outer loop workflow: dowhile(iteration workflow, continuation * predicate). */ build(): LoopWorkflow; } //# sourceMappingURL=loop-builder.d.ts.map