import { BaseChannel } from "../channels/base.js"; import { OverwriteValue } from "../constants.js"; import { RunnableLike } from "../pregel/runnable_types.js"; import { SerializableSchema } from "./types.js"; import { ReducedValue } from "./values/reduced.js"; import { UntrackedValue } from "./values/untracked.js"; import { JSONSchema } from "@langchain/core/utils/json_schema"; //#region src/state/schema.d.ts declare const STATE_SCHEMA_SYMBOL: unique symbol; /** * Maps a single StateSchema field definition to its corresponding Channel type. * * This utility type inspects the type of the field and returns an appropriate * `BaseChannel` type, parameterized with the state "value" and "input" types according to the field's shape. * * Rules: * - If the field (`F`) is a `ReducedValue`, the channel will store values of type `V` * and accept input of type `I`. * - If the field is a `UntrackedValue`, the channel will store and accept values of type `V`. * - If the field is a `SerializableSchema`, the channel will store values of type `O` * (the schema's output/validated value) and accept input of type `I`. * - For all other types, a generic `BaseChannel` is used as fallback. * * @template F - The StateSchema field type to map to a Channel type. * * @example * ```typescript * type MyField = ReducedValue; * type ChannelType = StateSchemaFieldToChannel; * // ChannelType is BaseChannel * ``` */ type StateSchemaFieldToChannel = F extends ReducedValue ? BaseChannel | I> : F extends UntrackedValue ? BaseChannel : F extends SerializableSchema ? BaseChannel : BaseChannel; /** * Converts StateSchema fields into a strongly-typed * State Definition object, where each field is mapped to its channel type. * * This utility type is used internally to create the shape of the state channels for a given schema, * substituting each field with the result of `StateSchemaFieldToChannel`. * * If you define a state schema as: * ```typescript * const fields = { * a: ReducedValue(), * b: UntrackedValue(), * c: SomeSerializableSchemaType, // SerializableSchema * } * ``` * then `StateSchemaFieldsToStateDefinition` yields: * ```typescript * { * a: BaseChannel; * b: BaseChannel; * c: BaseChannel; * } * ``` * * @template TFields - The mapping of field names to StateSchema field types. * @returns An object type mapping field names to channel types. * * @see StateSchemaFieldToChannel */ type StateSchemaFieldsToStateDefinition = { [K in keyof TFields]: StateSchemaFieldToChannel }; /** * Valid field types for StateSchema. * Either a LangGraph state value type or a raw schema (e.g., Zod schema). */ type StateSchemaField = ReducedValue | UntrackedValue | SerializableSchema; /** * Init object for StateSchema constructor. * Uses `any` to allow variance in generic types (e.g., ReducedValue). */ type StateSchemaFields = { [key: string]: StateSchemaField; }; /** * Infer the State type from a StateSchemaFields. * This is the type of the full state object. * * - ReducedValue → Value (the stored type) * - UntrackedValue → Value * - SerializableSchema → Output (the validated type) */ type InferStateSchemaValue = { [K in keyof TFields]: TFields[K] extends ReducedValue ? TFields[K]["ValueType"] : TFields[K] extends UntrackedValue ? TFields[K]["ValueType"] : TFields[K] extends SerializableSchema ? TOutput : never }; /** * Infer the Update type from a StateSchemaFields. * This is the type for partial updates to state. * * - ReducedValue → Input (the reducer input type) * - UntrackedValue → Value * - SerializableSchema → Input (what you provide) */ type InferStateSchemaUpdate = { [K in keyof TFields]?: TFields[K] extends ReducedValue ? OverwriteValue | I : TFields[K] extends UntrackedValue ? TFields[K]["ValueType"] : TFields[K] extends SerializableSchema ? TInput : never }; /** * StateSchema provides a unified API for defining LangGraph state schemas. * * @example * ```ts * import { z } from "zod"; * import { StateSchema, ReducedValue, MessagesValue } from "@langchain/langgraph"; * * const AgentState = new StateSchema({ * // Prebuilt messages value * messages: MessagesValue, * // Basic LastValue channel from any standard schema * currentStep: z.string(), * // LastValue with native default * count: z.number().default(0), * // ReducedValue for fields needing reducers * history: new ReducedValue( * z.array(z.string()).default(() => []), * { * inputSchema: z.string(), * reducer: (current, next) => [...current, next], * } * ), * }); * * // Extract types * type State = typeof AgentState.State; * type Update = typeof AgentState.Update; * * // Use in StateGraph * const graph = new StateGraph(AgentState); * ``` */ declare class StateSchema { readonly fields: TFields; /** * Symbol for runtime identification. * @internal Used by isInstance for runtime type checking */ private readonly [STATE_SCHEMA_SYMBOL]; /** * Type declaration for the full state type. * Use: `typeof myState.State` */ State: InferStateSchemaValue; /** * Type declaration for the update type. * Use: `typeof myState.Update` */ Update: InferStateSchemaUpdate; /** * Type declaration for node functions. * Use: `typeof myState.Node` to type node functions outside the graph builder. * * @example * ```typescript * const AgentState = new StateSchema({ * count: z.number().default(0), * }); * * const myNode: typeof AgentState.Node = (state) => { * return { count: state.count + 1 }; * }; * ``` */ Node: RunnableLike, InferStateSchemaUpdate>; constructor(fields: TFields); /** * Get the channel definitions for use with StateGraph. * This converts the StateSchema fields into BaseChannel instances. */ getChannels(): Record; /** * Get the JSON schema for the full state type. * Used by Studio and API for schema introspection. */ getJsonSchema(): JSONSchema; /** * Get the JSON schema for the update/input type. * All fields are optional in updates. */ getInputJsonSchema(): JSONSchema; /** * Get the list of channel keys (excluding managed values). */ getChannelKeys(): string[]; /** * Get all keys (channels + managed values). */ getAllKeys(): string[]; /** * Validate input data against the schema. * This validates each field using its corresponding schema. * * @param data - The input data to validate * @returns The validated data with coerced types */ validateInput(data: T): Promise; /** * Type guard to check if a value is a StateSchema instance. * * @param value - The value to check. * @returns True if the value is a StateSchema instance with the correct runtime tag. */ static isInstance(value: StateSchema): value is StateSchema; static isInstance(value: unknown): value is StateSchema; } type AnyStateSchema = StateSchema; //#endregion export { AnyStateSchema, InferStateSchemaUpdate, InferStateSchemaValue, StateSchema, StateSchemaField, StateSchemaFieldToChannel, StateSchemaFields, StateSchemaFieldsToStateDefinition }; //# sourceMappingURL=schema.d.ts.map