@mastra/core
Version:
100 lines • 4.57 kB
TypeScript
import type { StepFinishPayload } from '../../stream/types.js';
type ResponseMessage = NonNullable<StepFinishPayload['messages']>['nonUser'][number];
/**
* A step's response messages as published on a run's stream topic: the run's
* list from index `from` on, replacing whatever a reader holds from there.
* Earlier entries can change between steps (a step's assistant message grows,
* a retried attempt is rolled back), so this is not just the new messages.
*/
export interface ResponseMessagesDiff {
/** Identifies the publisher, e.g. one execution of a run; each producer numbers its own diffs. */
producer: string;
/** Increases by one per published step-finish of the producer. */
seq: number;
from: number;
/** Identifies the list this diff applies to (absent when `from` is 0). */
base?: string;
/** Identifies the list that applying this diff gives. */
hash: string;
messages: ResponseMessage[];
}
/** What a producer remembers about the response messages it has published. */
export interface ResponseMessagesCursor {
producer: string;
seq: number;
fingerprints: string[];
/** Size of the last whole list it sent (JSON characters). */
wholeListSize?: number;
/** Size of the diffs it sent since then. */
sentSinceWholeList?: number;
}
/**
* The diff to publish for `messages`, the run's response messages so far, and
* the cursor to keep for the next one.
*
* A producer's first diff holds the whole list (`from: 0`), and so does the
* next one once the diffs sent since the last whole list add up to its size.
* A reader that missed a diff (a lost delivery) recovers at the next whole
* list, while whole lists cost at most about as much as the diffs: the log
* stays proportional to the run's messages instead of growing with the square
* of its steps.
*/
export declare function diffResponseMessages(cursor: ResponseMessagesCursor | {
producer: string;
}, messages: ResponseMessage[]): {
diff: ResponseMessagesDiff;
cursor: ResponseMessagesCursor;
};
/** The response messages a reader rebuilt from the diffs it applied. */
export interface ResponseMessagesReplay {
/** The list of the last diff applied. */
messages: ResponseMessage[];
/**
* The list rebuilt per producer, with the hash and `seq` of its last diff, in
* the order the producers first appeared. A run resumed in a new producer can
* still get a late step-finish from the previous one; each producer's diffs
* apply to its own list, so that event doesn't break the new producer's next
* diff.
*/
producers?: Map<string, {
messages: ResponseMessage[];
hash: string;
seq: number;
}>;
/** Producers whose list was dropped for a newer one; their late events never take a newer one's place. */
retired?: Set<string>;
}
/**
* Apply a step-finish payload's response messages to what a reader holds.
* Returns the step's full list, or undefined when the payload can't be applied:
* the reader doesn't hold the exact list the diff was made against (it missed
* a diff, or a step was published twice with different content), or the diff
* is a late copy of one it is already past. Such a reader reports nothing
* until a diff fits its list or the producer's next whole list arrives,
* instead of guessing. Payloads from producers that publish the whole list
* (`_durableResponseMessages`) report it as is.
*/
export declare function applyResponseMessages(replay: ResponseMessagesReplay, payload: {
_durableResponseMessagesDiff?: ResponseMessagesDiff;
_durableResponseMessages?: ResponseMessage[];
}): ResponseMessage[] | undefined;
/**
* A copy of a published step-finish payload with the run's full response
* message list in `_durableResponseMessages`, rebuilt into `replay`, as readers
* of the run (and its callers' public frames) see it.
*/
export declare function withRebuiltResponseMessages<T extends Parameters<typeof applyResponseMessages>[1]>(replay: ResponseMessagesReplay, payload: T): Omit<T, '_durableResponseMessagesDiff'> & {
_durableResponseMessages?: ResponseMessage[];
};
/**
* Fold the step-finish events of a run log, in order, into a reader's starting
* state. Like a reader, it drops events of an execution that a newer claim
* generation already superseded.
*/
export declare function replayResponseMessages(events: {
type?: string;
data?: unknown;
generation?: number;
}[]): ResponseMessagesReplay;
export {};
//# sourceMappingURL=response-messages.d.ts.map