@langchain/langgraph
Version:
99 lines (98 loc) • 4.48 kB
TypeScript
import { BaseChannel } from "./base.js";
import { OverwriteValue } from "../constants.js";
import { CheckpointPendingWrite, DeltaSnapshot } from "@langchain/langgraph-checkpoint";
//#region src/channels/delta.d.ts
/**
* A batch reducer for use with {@link DeltaChannel}.
*
* Receives the current accumulated value and a batch of writes in one call,
* returning the new accumulated value:
* `reducer(state, [write1, write2, ...]) -> newState`.
*
* Reducers must be deterministic and batching-invariant (associative across
* folds): applying two consecutive write batches separately must produce the
* same state as applying their concatenation once:
*
* ```text
* reducer(reducer(state, xs), ys) === reducer(state, xs.concat(ys))
* ```
*
* This lets LangGraph replay checkpointed writes in larger batches than they
* were originally produced without changing reconstructed state. If your
* reducer is not associative, use {@link BinaryOperatorAggregate} instead —
* `DeltaChannel` is not a drop-in replacement for every reducer.
*/
type DeltaReducer<ValueType, UpdateType = unknown> = (state: ValueType, writes: UpdateType[]) => ValueType;
type OverwriteOrValue<ValueType, UpdateType> = OverwriteValue<ValueType> | UpdateType;
/**
* Reducer channel that stores only a sentinel in checkpoint blobs and
* reconstructs state by replaying ancestor writes through the reducer.
*
* `DeltaChannel` avoids re-serializing the full accumulated value at every
* step. Instead of writing the value into `channel_values`, the channel is
* omitted entirely and its state is reconstructed on read by walking the
* ancestor chain and replaying the per-step writes through the reducer (see
* {@link BaseCheckpointSaver.getDeltaChannelHistory}).
*
* Snapshot cadence is driven by two counters: a per-channel update count and
* the total supersteps since the last snapshot. A full {@link DeltaSnapshot}
* blob is written when EITHER the update count reaches `snapshotFrequency` OR
* the supersteps count reaches the system-wide
* `DELTA_MAX_SUPERSTEPS_SINCE_SNAPSHOT` bound (default 5000), bounding replay
* depth even for channels that stop receiving writes.
*
* @remarks Beta. The API and on-disk representation may change in future
* releases. Threads written with `DeltaChannel` today are expected to remain
* readable, but the surrounding contract (`getDeltaChannelHistory`, the
* `DeltaSnapshot` blob shape, the `counters_since_delta_snapshot` metadata
* field) is not yet stable.
*
* @example
* ```typescript
* import { Annotation } from "@langchain/langgraph";
* import { DeltaChannel, messagesDeltaReducer } from "@langchain/langgraph";
*
* const State = Annotation.Root({
* messages: Annotation<BaseMessage[]>({
* reducer: () => [], // ignored; DeltaChannel is supplied below
* }),
* });
* ```
*/
declare class DeltaChannel<ValueType = unknown, UpdateType = unknown> extends BaseChannel<ValueType, OverwriteOrValue<ValueType, UpdateType>, undefined> {
lc_graph_name: string;
/** `undefined` represents the Python `MISSING` sentinel (empty channel). */
value: ValueType | undefined;
reducer: DeltaReducer<ValueType, UpdateType>;
snapshotFrequency: number;
initialValueFactory: () => ValueType;
constructor(reducer: DeltaReducer<ValueType, UpdateType>, options?: {
snapshotFrequency?: number;
initialValueFactory?: () => ValueType;
});
fromCheckpoint(checkpoint?: undefined | DeltaSnapshot | ValueType): this;
/**
* Apply ancestor writes oldest-to-newest via a single reducer call.
*
* If any write is an Overwrite, the last one in the sequence acts as the
* reset point: its value becomes the new base and only writes after it are
* passed to the reducer.
*/
replayWrites(writes: CheckpointPendingWrite[]): void;
update(values: OverwriteOrValue<ValueType, UpdateType>[]): boolean;
get(): ValueType;
/**
* Always returns `undefined` (the Python `MISSING` sentinel). Snapshot
* decisions live in `createCheckpoint`, which has the channel version and
* writes a {@link DeltaSnapshot} directly into `channel_values`. For
* non-snapshot steps the channel does not appear in `channel_values`;
* reconstruction walks ancestor writes via the saver's
* `getDeltaChannelHistory`.
*/
checkpoint(): undefined;
isAvailable(): boolean;
equals(other: BaseChannel): boolean;
}
//#endregion
export { DeltaChannel, DeltaReducer };
//# sourceMappingURL=delta.d.ts.map