langchain.js
    Preparing search index...

    Class ReducedValue<Value, Input>

    Represents a state field whose value is computed and updated using a reducer function.

    ReducedValue allows you to define accumulators, counters, aggregators, or other fields whose value is determined incrementally by applying a reducer to incoming updates.

    Each time a new input is provided, the reducer function is called with the current output and the new input, producing an updated value. Input validation can be controlled separately from output validation by providing an explicit input schema.

    // Accumulator with distinct input validation
    const Sum = new ReducedValue(z.number(), {
    inputSchema: z.number().min(1),
    reducer: (total, toAdd) => total + toAdd
    });
    // Simple running max, using only the value schema
    const Max = new ReducedValue(z.number(), {
    reducer: (current, next) => Math.max(current, next)
    });

    Type Parameters

    • Value = unknown

      The type of the value stored in state and produced by reduction.

    • Input = Value

      The type of updates accepted by the reducer.

    Index

    Constructors

    • Constructs a ReducedValue instance, which combines a value schema and a reducer function (plus optional input schema).

      Type Parameters

      • Value = unknown
      • Input = Value

      Parameters

      • valueSchema: SerializableSchema<unknown, Value>

        The schema that describes the type of value stored in state (the "running total").

      • init: ReducedValueInitWithSchema<Value, Input>

        An object specifying the reducer function (required), inputSchema (optional), and jsonSchemaExtra (optional).

      Returns ReducedValue<Value, Input>

    • Type Parameters

      • Value = unknown
      • Input = Value

      Parameters

      • valueSchema: SerializableSchema<Input, Value>
      • init: ReducedValueInitBase<Value>

      Returns ReducedValue<Value, Input>

    Properties

    inputSchema: SerializableSchema<unknown, Value | Input>

    The schema used to validate reducer inputs. If not specified explicitly, this defaults to valueSchema.

    InputType: Input

    Represents the type that may be provided as input on each update.

    jsonSchemaExtra?: Record<string, unknown>

    Optional extra fields to merge into the generated JSON Schema (e.g., for documentation or constraints).

    reducer: (current: Value, next: Input) => Value

    The reducer function that combines a current output value and an incoming input.

    valueSchema: SerializableSchema<unknown, Value>

    The schema that describes the type of value stored in state (i.e., after reduction). Note: We use unknown for the input type to allow schemas with .default() wrappers, where the input type includes undefined.

    ValueType: Value

    Represents the value stored after all reductions.

    Methods

    • Type guard to check if a value is a ReducedValue instance.

      Type Parameters

      • Value = unknown
      • Input = Value

      Parameters

      Returns value is ReducedValue<Value, Input>

    • Type guard to check if a value is a ReducedValue instance.

      Parameters

      • value: unknown

      Returns value is ReducedValue<unknown, unknown>