@mastra/core
Version:
98 lines • 5.19 kB
TypeScript
import type { PubSub } from '../../events/pubsub.js';
import type { Event } from '../../events/types.js';
import type { IMastraLogger } from '../../logger/index.js';
import type { ChunkType } from '../../stream/types.js';
import type { MessageList } from '../message-list/index.js';
import type { AgentRunRecordTarget } from './run-records.js';
/**
* Publishes a run's chunks on its stream topic as the loop produces them, so
* readers other than the caller (`Agent.observe()`, a reconnecting client) can
* follow the run. Publishing never blocks or fails the run: events are queued
* in order and publish errors are logged.
*/
export interface AgentStreamPublisher {
readonly runId: string;
/**
* Aborts when the caller's signal does, or when an abort request for this
* run arrives on its control topic (from `observe().abort()` or
* `abortRunStream()` in any process sharing the bus). Pass it to the loop.
*/
readonly abortSignal: AbortSignal;
/** Abort the run in this process. */
abort(reason?: unknown): void;
/**
* Queue a chunk the loop emitted to its caller. `messageList` is the run's
* message list, which a `step-finish` event takes the step's content from.
*/
publish(chunk: ChunkType<any>, messageList?: MessageList): void;
/** Publish an event whose durable payload and response-message diff are already shaped. */
publishEvent(event: Omit<Event, 'id' | 'createdAt'>, transport?: PubSub, options?: Parameters<PubSub['publish']>[2]): Promise<void>;
/** Resolves once the abort subscription is installed; rejects if installation fails. */
readonly ready: Promise<void>;
/** Settle an accepted terminal in the owning reader without republishing it or scheduling cleanup. */
settle(status: 'suspended' | 'finished'): Promise<void>;
/** Clear this execution's shared stream state, using the same ownership check as automatic cleanup. */
cleanup(): Promise<void>;
/** Flush local publications and release local listeners without ending the shared run. */
detach(): Promise<void>;
/**
* The loop closed its stream. Publishes the terminal event readers wait for
* when the loop didn't end with `finish`: `suspended` when the run paused,
* otherwise `error`.
*/
close(): Promise<void>;
/** The loop's stream errored. */
fail(reason: unknown): Promise<void>;
}
export interface CreateAgentStreamPublisherOptions {
pubsub: PubSub;
runId: string;
/** Grace period before a finished run's topic is cleared (0 = never). */
cleanupTimeoutMs: number;
logger?: IMastraLogger;
/** The caller's own abort signal, folded into {@link AgentStreamPublisher.abortSignal}. */
abortSignal?: AbortSignal;
/** Called once when the run stops publishing (finished, failed, or suspended). */
onEnd?: () => void;
/**
* Where to keep the run's shared ownership record (see `run-records.ts`).
* It is written before the first event, marked `suspended` or `finished`
* after the run's last event, and deleted with the topic.
*/
record?: AgentRunRecordTarget;
/** Durable workers share the current execution claim's identity, not a per-worker identity. */
publisherId?: string;
/** Verify the execution claim before publishing or changing shared stream state. */
assertOwnership?: () => Promise<void>;
/** Additional authority required for shared record mutations, not event publication. */
assertRecordOwnership?: () => Promise<void>;
/** Cleanup can retain topic authority after an owned deadline revokes execution writes. */
assertCleanupOwnership?: () => Promise<void>;
/** Forward a remote abort to the worker's execution controller. */
onAbortRequested?: () => void;
/** Disable when the execution already owns a shared abort listener. */
listenForAbort?: boolean;
}
export interface AgentStreamProducer {
publishEvent(event: Omit<Event, 'id' | 'createdAt'>, transport?: PubSub, options?: Parameters<PubSub['publish']>[2]): Promise<void>;
/** Release this producer handle, not the run owner's handle or abort listener. */
detach(): Promise<void>;
}
export interface AgentStreamPublisherCoordinator {
attachEventProducer(options: {
publisherId: string;
pubsub: PubSub;
assertOwnership: () => Promise<void>;
}): AgentStreamProducer;
attachOwner(options: Omit<CreateAgentStreamPublisherOptions, 'runId'>): AgentStreamPublisher;
listenForAbort(pubsub: PubSub, onAbortRequested: () => boolean | void): {
ready: Promise<void>;
detach(): Promise<void>;
};
detach(): Promise<void>;
}
/** One local publisher per run; execution-bound handles retain their own authority and release state. */
export declare function createAgentStreamPublisherCoordinator(runId: string, logger?: IMastraLogger): AgentStreamPublisherCoordinator;
/** Plain agents hold the full handle; durable workers only receive the capabilities they need. */
export declare function createAgentStreamPublisher(options: CreateAgentStreamPublisherOptions): AgentStreamPublisher;
//# sourceMappingURL=publisher.d.ts.map