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.
class ReducedValue// 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)
});The schema used to validate reducer inputs.
If not specified explicitly, this defaults to valueSchema.
Represents the type that may be provided as input on each update.
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.