langchain
Version:
Typescript bindings for langchain
75 lines (74 loc) • 3.13 kB
TypeScript
import { AgentMiddleware } from "./types.js";
import { ClientTool, ServerTool, StructuredToolInterface } from "@langchain/core/tools";
//#region src/agents/middleware/providerToolSearch.d.ts
type ToolName = string;
type ToolIdentifier = ToolName | StructuredToolInterface;
type ProviderToolSearchMiddlewareConfig = {
/**
* Which tools are deferred; withheld from the model until its tool search surfaces them.
*
* Tools already constructed with `extras.defer_loading === true` are deferred
* regardless of this option; if `searchableTools` is omitted, only those pre-marked
* tools are deferred.
*/
searchableTools?: ToolIdentifier[];
};
/**
* Provider-side tool search middleware.
*
* Leverages server-side tool search: the full client tool catalog is forwarded
* to the provider, with deferred tools marked `defer_loading` so the provider
* discloses them on demand via its own search. A tool is deferred when it is
* named in `searchableTools` or built with `extras.defer_loading: true`.
*
* Requires a model with server-side tool search support: OpenAI gpt-5.4+ or Anthropic
* Claude Sonnet 4+/Opus 4+/Haiku 4.5+. Non-Anthropic/OpenAI providers throw; an
* in-family model that is too old surfaces the provider's own API error rather
* than being gated here.
*
* @example
* ```ts
* import { createAgent, providerToolSearchMiddleware } from "langchain";
* import { ChatAnthropic } from "@langchain/anthropic";
*
* const agent = createAgent({
* model: new ChatAnthropic({ model: "claude-sonnet-4-5" }),
* tools: [getWeather, ...nicheTools],
* middleware: [
* // Defer the niche tools behind the provider's tool search; the model
* // discovers them on demand instead of receiving every schema up front.
* providerToolSearchMiddleware({ searchableTools: nicheTools }),
* ],
* });
* ```
*
* @example
* ```ts
* import { tool } from "@langchain/core/tools";
* import { createAgent, providerToolSearchMiddleware } from "langchain";
* import { ChatAnthropic } from "@langchain/anthropic";
*
* // A tool marked `defer_loading` at construction is deferred on its own —
* // no need to list it in `searchableTools`; the middleware honors the flag.
* const sendEmail = tool(sendEmailFn, {
* name: "send_email",
* description: "Send an email",
* schema: sendEmailSchema,
* extras: { defer_loading: true },
* });
*
* const agent = createAgent({
* model: new ChatAnthropic({ model: "claude-sonnet-4-5" }),
* tools: [getWeather, sendEmail],
* middleware: [providerToolSearchMiddleware()],
* });
* ```
*
* @param config - Configuration options for the middleware
* @param config.searchableTools - Tools to defer behind tool search
* @returns A middleware instance that can be used with `createAgent`
*/
declare function providerToolSearchMiddleware(config?: ProviderToolSearchMiddlewareConfig): AgentMiddleware<undefined, undefined, unknown, readonly (ServerTool | ClientTool)[], readonly []>;
//#endregion
export { ProviderToolSearchMiddlewareConfig, providerToolSearchMiddleware };
//# sourceMappingURL=providerToolSearch.d.ts.map