UNPKG

@langchain/langgraph

Version:
77 lines (76 loc) 2.45 kB
//#region src/state/values/delta.ts /** * Symbol for runtime identification of DeltaValue instances. */ const DELTA_VALUE_SYMBOL = Symbol.for("langgraph.state.delta_value"); /** * Represents a state field backed by a {@link DeltaChannel}. * * Unlike {@link ReducedValue} (which stores the full accumulated value in every * checkpoint blob via `BinaryOperatorAggregate`), a `DeltaValue` field persists * only per-step deltas (plus periodic snapshots) and reconstructs its state on * read by replaying ancestor writes through a batch reducer. This avoids * re-serializing large accumulators (e.g. long message histories) at every step. * * @remarks Beta. The on-disk representation backing `DeltaChannel` may change in * future releases. * * @template Value - The type of the value stored in state and produced by reduction. * @template Input - The type of updates accepted by the reducer. * * @example * ```ts * import { z } from "zod"; * import { StateSchema, DeltaValue } from "@langchain/langgraph"; * * const State = new StateSchema({ * history: new DeltaValue(z.array(z.string()).default(() => []), { * inputSchema: z.string(), * reducer: (current, writes) => [...current, ...writes], * }), * }); * ``` */ var DeltaValue = class { /** * Instance marker for runtime identification. * @internal */ [DELTA_VALUE_SYMBOL] = true; /** * The schema that describes the type of value stored in state (after * reduction). Its default (if any) seeds the channel's initial value. */ valueSchema; /** * The schema used to validate reducer inputs. Defaults to `valueSchema` when * not specified explicitly. */ inputSchema; /** * The batch reducer that folds a list of incoming writes into the current * accumulated value. */ reducer; /** * Snapshot cadence forwarded to the underlying {@link DeltaChannel}. */ snapshotFrequency; /** * Optional extra fields to merge into the generated JSON Schema. */ jsonSchemaExtra; constructor(valueSchema, init) { this.reducer = init.reducer; this.valueSchema = valueSchema; this.inputSchema = "inputSchema" in init ? init.inputSchema : valueSchema; this.snapshotFrequency = init.snapshotFrequency; this.jsonSchemaExtra = init.jsonSchemaExtra; } static isInstance(value) { return typeof value === "object" && value !== null && DELTA_VALUE_SYMBOL in value && value[DELTA_VALUE_SYMBOL] === true; } }; //#endregion export { DeltaValue }; //# sourceMappingURL=delta.js.map