@mastra/core
Version:
45 lines • 2.04 kB
TypeScript
/**
* One change to a storage v2 run's state. `path` addresses a value from the state's root: a
* number is an array index and a string is an object key. An empty path is the root.
*
* Ops are absolute (there is no relative append), so replaying a list of ops onto a state that
* already contains them changes nothing.
* @experimental
*/
export type StatePatchOp = {
op: 'set';
path: (string | number)[];
value: unknown;
} | {
op: 'delete';
path: (string | number)[];
};
/**
* The ops that turn `prev` into `next`. Both must be JSON values (no `undefined`, Dates or class
* instances). Unchanged values produce no ops.
* @experimental
*/
export declare function diffState(prev: unknown, next: unknown): StatePatchOp[];
/**
* `diffState(prev, JSON.parse(JSON.stringify(next)))` without copying `next`: `next` can be any
* JSON-serializable value and is compared as its JSON form, and only the values of `set` ops are
* copied. `prev` must be a JSON value. Throws a `TypeError`, as `JSON.stringify` does, when
* `next` has a cycle or a BigInt, or has no JSON form at all.
* @experimental
*/
export declare function diffStateAsJson(prev: unknown, next: unknown): StatePatchOp[];
/**
* Apply `ops` to `root` in order and return the result: `root` itself, mutated, unless an op
* replaces the root. A `set` creates missing or non-container intermediates (`[]` before a number
* segment, `{}` before a string); a `delete` whose path doesn't exist does nothing. Set values are
* copied, so the result shares nothing with `ops`.
* @experimental
*/
export declare function applyStatePatch<T = Record<string, any>>(root: T, ops: readonly StatePatchOp[]): T;
/**
* `applyStatePatch` that leaves `root` as it is: each object or array an op changes is copied
* (once) first, so the result shares only what the ops didn't change with `root`.
* @experimental
*/
export declare function patchState<T = Record<string, any>>(root: T, ops: readonly StatePatchOp[]): T;
//# sourceMappingURL=state-patch.d.ts.map