langchain.js
    Preparing search index...

    Function createMiddleware

    LangChain Agents

    • Creates a middleware instance with automatic schema inference.

      Type Parameters

      • TSchema extends any = undefined
      • TContextSchema extends any = undefined

      Parameters

      • config: {
            afterModel?: (
                state: (
                    TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                ) & AgentBuiltInState,
                runtime: Runtime<
                    (
                        TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                    ) & AgentBuiltInState,
                    TContextSchema extends InteropZodObject
                        ? InferInteropZodOutput<TContextSchema>
                        : TContextSchema extends InteropZodDefault<any>
                            ? InferInteropZodOutput<TContextSchema>
                            : TContextSchema extends InteropZodOptional<any>
                                ? InferInteropZodOutput<TContextSchema>
                                : never,
                >,
            ) =>
                | Promise<
                    MiddlewareResult<
                        Partial<
                            TSchema extends InteropZodObject
                                ? InferInteropZodInput<TSchema>
                                : {},
                        >,
                    >,
                >
                | MiddlewareResult<
                    Partial<
                        TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                    >,
                >;
            afterModelJumpTo?: ("tools" | "model" | "end")[];
            beforeModel?: (
                state: (
                    TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                ) & AgentBuiltInState,
                runtime: Runtime<
                    (
                        TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                    ) & AgentBuiltInState,
                    TContextSchema extends InteropZodObject
                        ? InferInteropZodOutput<TContextSchema>
                        : TContextSchema extends InteropZodDefault<any>
                            ? InferInteropZodOutput<TContextSchema>
                            : TContextSchema extends InteropZodOptional<any>
                                ? InferInteropZodOutput<TContextSchema>
                                : never,
                >,
            ) =>
                | Promise<
                    MiddlewareResult<
                        Partial<
                            TSchema extends InteropZodObject
                                ? InferInteropZodInput<TSchema>
                                : {},
                        >,
                    >,
                >
                | MiddlewareResult<
                    Partial<
                        TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                    >,
                >;
            beforeModelJumpTo?: ("tools" | "model" | "end")[];
            contextSchema?: TContextSchema;
            modifyModelRequest?: (
                options: ModelRequest,
                state: (
                    TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                ) & AgentBuiltInState,
                runtime: Runtime<
                    (
                        TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
                    ) & AgentBuiltInState,
                    TContextSchema extends InteropZodObject
                        ? InferInteropZodOutput<TContextSchema>
                        : TContextSchema extends InteropZodDefault<any>
                            ? InferInteropZodOutput<TContextSchema>
                            : TContextSchema extends InteropZodOptional<any>
                                ? InferInteropZodOutput<TContextSchema>
                                : never,
                >,
            ) => void | ModelRequest | Promise<void | ModelRequest>;
            name: string;
            stateSchema?: TSchema;
            tools?: any[];
        }

        Middleware configuration

        • OptionalafterModel?: (
              state: (
                  TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
              ) & AgentBuiltInState,
              runtime: Runtime<
                  (TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}) & AgentBuiltInState,
                  TContextSchema extends InteropZodObject
                      ? InferInteropZodOutput<TContextSchema>
                      : TContextSchema extends InteropZodDefault<any>
                          ? InferInteropZodOutput<TContextSchema>
                          : TContextSchema extends InteropZodOptional<any>
                              ? InferInteropZodOutput<TContextSchema>
                              : never,
              >,
          ) =>
              | Promise<
                  MiddlewareResult<
                      Partial<
                          TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                      >,
                  >,
              >
              | MiddlewareResult<
                  Partial<
                      TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                  >,
              >

          The function to run after the model call. This function is called after the model is invoked and before any tools are called. It allows to modify the state of the agent after the model is invoked, e.g. to update tool call parameters.

        • OptionalafterModelJumpTo?: ("tools" | "model" | "end")[]

          Explitictly defines which targets are allowed to be jumped to from the afterModel hook.

        • OptionalbeforeModel?: (
              state: (
                  TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
              ) & AgentBuiltInState,
              runtime: Runtime<
                  (TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}) & AgentBuiltInState,
                  TContextSchema extends InteropZodObject
                      ? InferInteropZodOutput<TContextSchema>
                      : TContextSchema extends InteropZodDefault<any>
                          ? InferInteropZodOutput<TContextSchema>
                          : TContextSchema extends InteropZodOptional<any>
                              ? InferInteropZodOutput<TContextSchema>
                              : never,
              >,
          ) =>
              | Promise<
                  MiddlewareResult<
                      Partial<
                          TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                      >,
                  >,
              >
              | MiddlewareResult<
                  Partial<
                      TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {},
                  >,
              >

          The function to run before the model call. This function is called before the model is invoked and before the modifyModelRequest hook. It allows to modify the state of the agent.

        • OptionalbeforeModelJumpTo?: ("tools" | "model" | "end")[]

          Explitictly defines which targets are allowed to be jumped to from the beforeModel hook.

        • OptionalcontextSchema?: TContextSchema

          The schema of the middleware context. Middleware context is read-only and not persisted between multiple invocations. It can be either:

          • A Zod object
          • A Zod optional object
          • A Zod default object
          • Undefined
        • OptionalmodifyModelRequest?: (
              options: ModelRequest,
              state: (
                  TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}
              ) & AgentBuiltInState,
              runtime: Runtime<
                  (TSchema extends InteropZodObject ? InferInteropZodInput<TSchema> : {}) & AgentBuiltInState,
                  TContextSchema extends InteropZodObject
                      ? InferInteropZodOutput<TContextSchema>
                      : TContextSchema extends InteropZodDefault<any>
                          ? InferInteropZodOutput<TContextSchema>
                          : TContextSchema extends InteropZodOptional<any>
                              ? InferInteropZodOutput<TContextSchema>
                              : never,
              >,
          ) => void | ModelRequest | Promise<void | ModelRequest>

          The function to modify the model request. This function is called after the beforeModel hook of this middleware and before the model is invoked. It allows to modify the model request before it is passed to the model.

        • name: string

          The name of the middleware

        • OptionalstateSchema?: TSchema

          The schema of the middleware state. Middleware state is persisted between multiple invocations. It can be either:

          • A Zod object
          • A Zod optional object
          • A Zod default object
          • Undefined
        • Optionaltools?: any[]

          Additional tools registered by the middleware.

      Returns AgentMiddleware<TSchema, TContextSchema, any>

      A middleware instance

      const authMiddleware = createMiddleware({
      name: "AuthMiddleware",
      stateSchema: z.object({
      isAuthenticated: z.boolean().default(false),
      }),
      contextSchema: z.object({
      userId: z.string(),
      }),
      beforeModel: async (state, runtime, controls) => {
      if (!state.isAuthenticated) {
      return controls.terminate(new Error("Not authenticated"));
      }
      },
      });