@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
92 lines • 3.69 kB
TypeScript
import type { PubSub } from '../../events/pubsub.js';
import type { IMastraLogger } from '../../logger/index.js';
import { MastraModelOutput } from '../../stream/base/output.js';
import type { ChunkType } from '../../stream/types.js';
import type { AgentStepFinishEventData, AgentFinishEventData, AgentSuspendedEventData } from './types.js';
/**
* Options for creating a durable agent stream
*/
export interface DurableAgentStreamOptions<OUTPUT = undefined> {
/** Pubsub instance to subscribe to */
pubsub: PubSub;
/** Run identifier */
runId: string;
/** Message ID for this execution */
messageId: string;
/** Model information for the output */
model: {
modelId: string | undefined;
provider: string | undefined;
version: 'v2' | 'v3';
};
/** Thread ID for memory */
threadId?: string;
/** Resource ID for memory */
resourceId?: string;
/**
* Start replay from this index (0-based).
* If undefined, uses full replay (subscribeWithReplay).
* If specified, uses efficient indexed replay (subscribeFromOffset).
*/
offset?: number;
/** Callback when chunk is received */
onChunk?: (chunk: ChunkType<OUTPUT>) => void | Promise<void>;
/** Callback when step finishes */
onStepFinish?: (result: AgentStepFinishEventData) => void | Promise<void>;
/** Callback when execution finishes */
onFinish?: (result: AgentFinishEventData) => void | Promise<void>;
/** Callback on error */
onError?: (error: Error) => void | Promise<void>;
/** Callback when workflow suspends */
onSuspended?: (data: AgentSuspendedEventData) => void | Promise<void>;
/** Optional logger for structured logging */
logger?: IMastraLogger;
}
/**
* Result from creating a durable agent stream
*/
export interface DurableAgentStreamResult<OUTPUT = undefined> {
/** The MastraModelOutput that streams from pubsub events */
output: MastraModelOutput<OUTPUT>;
/** Cleanup function to unsubscribe from pubsub */
cleanup: () => void;
/** Promise that resolves when subscription is established */
ready: Promise<void>;
}
/**
* Create a MastraModelOutput that streams from pubsub events.
*
* This adapter subscribes to the agent stream pubsub channel and converts
* pubsub events into a ReadableStream that MastraModelOutput can consume.
* Callbacks are invoked as events arrive.
*/
export declare function createDurableAgentStream<OUTPUT = undefined>(options: DurableAgentStreamOptions<OUTPUT>): DurableAgentStreamResult<OUTPUT>;
/**
* Helper to emit a chunk event to pubsub
*/
export declare function emitChunkEvent<OUTPUT = undefined>(pubsub: PubSub, runId: string, chunk: ChunkType<OUTPUT>): Promise<void>;
/**
* Helper to emit a step start event to pubsub
*/
export declare function emitStepStartEvent(pubsub: PubSub, runId: string, data: {
stepId?: string;
request?: unknown;
warnings?: unknown[];
}): Promise<void>;
/**
* Helper to emit a step finish event to pubsub
*/
export declare function emitStepFinishEvent(pubsub: PubSub, runId: string, data: AgentStepFinishEventData): Promise<void>;
/**
* Helper to emit a finish event to pubsub
*/
export declare function emitFinishEvent(pubsub: PubSub, runId: string, data: AgentFinishEventData): Promise<void>;
/**
* Helper to emit an error event to pubsub
*/
export declare function emitErrorEvent(pubsub: PubSub, runId: string, error: Error): Promise<void>;
/**
* Helper to emit a suspended event to pubsub
*/
export declare function emitSuspendedEvent(pubsub: PubSub, runId: string, data: AgentSuspendedEventData): Promise<void>;
//# sourceMappingURL=stream-adapter.d.ts.map