@mastra/core
Version:
58 lines • 3.18 kB
TypeScript
import type { MessageList } from '../../../agent/message-list/index.js';
import type { ObservabilityContext } from '../../../observability/index.js';
import type { ProcessorState, ProcessorStreamWriter } from '../../../processors/index.js';
import type { ProcessorRunner } from '../../../processors/runner.js';
import type { RequestContext } from '../../../request-context/index.js';
import type { ChunkType } from '../../../stream/types.js';
export interface ProcessChunkDeps<OUTPUT = undefined> {
/**
* Pre-built runner sharing the run's processor states. `undefined` means no
* output processors are configured (main: none passed to the loop; durable:
* registry entry is empty after a process hop, so the live processor
* instances are unavailable) — the chunk is emitted unprocessed.
*/
runner: ProcessorRunner | undefined;
processorStates: Map<string, ProcessorState<OUTPUT>> | undefined;
observabilityContext?: ObservabilityContext;
requestContext?: RequestContext;
messageList?: MessageList;
/** Writer for processor-emitted custom chunks (routed to the engine's stream). */
streamWriter?: ProcessorStreamWriter;
/**
* Engine emission hook: main enqueues on the request-scoped stream
* controller; durable publishes via pubsub (no-op when pubsub is absent).
*/
emitChunk: (chunk: ChunkType<OUTPUT>) => void | Promise<void>;
/**
* Optional processor-error mapper. When omitted, processor errors propagate
* and fail the step. The mapper may throw an engine-specific error, suppress
* the chunk by returning `null`, or return a replacement chunk. A replacement
* is emitted unless a processed chunk already reached the stream.
*/
onProcessorError?: (error: unknown, originalChunk: ChunkType<OUTPUT>) => ChunkType<OUTPUT> | null;
/**
* Durable-only: the finish chunk that normally tears down stream-processor
* spans never reaches this pipeline, so spans opened for this chunk are
* ended right after processing. Main's spans end via the finish chunk in
* the inner stream pipeline that shares the same processor states.
*/
endSpansAfterProcessing?: boolean;
}
/**
* Shared per-chunk output-processor pipeline: run a
* tool-shaped chunk through the run's output processors, emit the processed
* chunk (or a tripwire when blocked), then drain any parts a processor
* stashed for reprocessing (e.g. the non-text part that triggered a
* BatchPartsProcessor flush), pushing each back through the whole chain.
*
* Returns the processed chunk, or `null` if it was blocked by a processor
* (a tripwire chunk is emitted instead).
*
* Adjudications:
* - `drainReprocessParts` (previously main-only, added a month after the
* durable engine forked): stashed parts were silently dropped on the
* durable engine; both engines now drain.
* - Error policy stays per-engine via `onProcessorError` (see the hook doc).
*/
export declare function processAndEmitChunk<OUTPUT = undefined>(chunk: ChunkType<OUTPUT>, deps: ProcessChunkDeps<OUTPUT>): Promise<ChunkType<OUTPUT> | null>;
//# sourceMappingURL=process-chunk-core.d.ts.map