@mastra/core
Version:
107 lines • 5.21 kB
TypeScript
import type { WorkflowsStorage } from '../storage/domains/workflows/base.js';
import type { StateRef, WorkflowRunHeader } from '../storage/domains/workflows/journal.js';
import type { StatePatchOp } from '../storage/domains/workflows/state-patch.js';
import type { UpdateWorkflowStateOptions } from '../storage/types.js';
import type { WorkflowStepCompletionGuard, WorkflowStepStartGuard } from '../storage/workflow-step-guard.js';
import type { StepResult, WorkflowRunState } from './types.js';
/**
* Every store call a run makes, for the run's storage version. Call sites make the same call for
* both versions, so a storage v2 run never reaches a v1 store method.
*
* @internal
*/
export interface RunStorage {
loadHeader(): Promise<WorkflowRunHeader | null>;
/** The run's header as stored after the update, or null when the compare-and-set didn't apply. */
updateHeader(opts: UpdateWorkflowStateOptions): Promise<WorkflowRunHeader | null>;
/** What resume needs. v2 returns successful steps as stubs. `wait` polls while running/pending. */
loadForResume(opts: {
wait: boolean;
}): Promise<WorkflowRunState | null>;
/** Fully materialized, for restart and time travel. */
loadFull(): Promise<WorkflowRunState | null>;
/**
* Replaces the stored run: the pending/waiting snapshot written before the engine runs, or a
* pruned one. With `stateBase` (a storage v2 nested run starting from a stored version of its
* parent's state), the state is stored as that version instead of a copy.
*/
writeSnapshot(snapshot: WorkflowRunState, resourceId?: string, opts?: {
stateBase?: StateRef;
}): Promise<void>;
/** After a claim: has nothing been written to the run since `claimed` was loaded? */
isClaimUnused(claimed: WorkflowRunState, resumedStepId: string): Promise<boolean>;
/**
* What the store reported for the state of a snapshot loaded by `loadForResume` or `loadFull`:
* the run's `lastSeq` it is as of, its `statePatchBytes` and its `statePatchCount`.
*/
storedState(snapshot: WorkflowRunState): {
seq: number;
patchBytes: number;
patchCount: number;
runToken?: string;
} | undefined;
/**
* The state the run's current drive started from: as of its latest checkpoint, or as of the
* resume that started the drive. Undefined for v1, or when the run or that version isn't stored.
*/
loadDriveStartState(): Promise<Record<string, any> | undefined>;
loadResourceId(): Promise<string | undefined>;
/** The header and `stepId`'s result. v1 loads every step. */
loadStep(stepId: string): Promise<StepSnapshot | null>;
/**
* The latest results of `stepIds` (steps without one left out), among the run's writes up to
* `upToSeq` (a `seq` a write returned) on v2. v1 loads every step and ignores `upToSeq`.
*/
loadSteps(stepIds: string[], opts?: {
upToSeq?: number;
}): Promise<StepResults>;
/** The run's latest stored state, with only the top-level `keys` when given. `{}` when the run isn't stored. */
loadState(keys?: string[]): Promise<Record<string, any>>;
/** `state` (v1) or `statePatch` (v2) is written in the same write. */
writeStepResult(write: StepResultWrite): Promise<StepWriteResult | undefined>;
/** A foreach step's write: the store merges `result.output` into the stored array by index. */
mergeStepResult(write: StepResultWrite): Promise<StepWriteResult | undefined>;
writeState(write: StateWrite & {
requestContext: Record<string, any>;
}): Promise<void>;
delete(): Promise<void>;
}
type StepResults = Record<string, StepResult<any, any, any, any>>;
type StepSnapshot = WorkflowRunHeader & {
context: WorkflowRunState['context'];
};
/** v1 writes the full state, v2 a patch on the stored state, so concurrent writers keep each other's keys. */
type StateWrite = {
state?: Record<string, any>;
statePatch?: StatePatchOp[];
};
type StepResultWrite = StateWrite & {
stepId: string;
result: StepResult<any, any, any, any>;
requestContext: Record<string, any>;
/** The store rejects the write unless this delivery may start the step (not on v2's `mergeStepResult`). */
startGuard?: WorkflowStepStartGuard;
/** The store rejects the write unless this delivery owns the running step (not on v2's `mergeStepResult`). */
completionGuard?: WorkflowStepCompletionGuard;
};
/**
* After a step write: v1 returns every step's result, v2 the written step's (merged, for
* `mergeStepResult`) and the run's `lastSeq`. `results` is `{}` when the run isn't stored; the
* whole result is undefined without a store.
*/
type StepWriteResult = {
results: StepResults;
seq?: number;
};
type RunStorageArgs = {
workflowName: string;
runId: string;
/** Resolved per call, so a run built before Mastra has storage still finds it later. */
getStore(): Promise<WorkflowsStorage | undefined>;
};
/** @internal */
export declare function createRunStorage({ storageVersion, ...args }: RunStorageArgs & {
storageVersion: 1 | 2;
}): RunStorage;
export {};
//# sourceMappingURL=run-storage.d.ts.map