-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(core): support for headless tools #10430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Christian Bromann (christian-bromann)
wants to merge
16
commits into
main
Choose a base branch
from
cb/browser-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−5
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b8f67da
feat(langchain): browser tools
christian-bromann cf51b44
bring interface closer to original tool function
christian-bromann 8e657c3
format
christian-bromann 47f3f76
Create weak-kings-notice.md
christian-bromann 0380ffd
better typing
christian-bromann e93f5ce
add type test for createAgent
christian-bromann e5086fc
also export from browser
christian-bromann 883e1eb
import only on server side
christian-bromann ecc4536
make it headless
christian-bromann 914cd1c
format
christian-bromann 17cad1a
properly overload
christian-bromann 01063d0
no new tools export
christian-bromann 81841a6
Update libs/langchain/package.json
christian-bromann 77b2ffe
Update .changeset/weak-kings-notice.md
christian-bromann 72fe1f9
Update .changeset/weak-kings-notice.md
christian-bromann 87f2e6e
format
christian-bromann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@langchain/core": patch | ||
| "langchain": minor | ||
| --- | ||
|
|
||
| feat(langchain): support for browser tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,4 +219,4 @@ | |
| "./package.json": "./package.json" | ||
| }, | ||
| "module": "./dist/index.js" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| /** | ||
| * Unified Tool Primitive for LangChain Agents | ||
| * | ||
| * This module re-exports the `tool` primitive from `@langchain/core/tools` with | ||
| * an additional overload: when called without an implementation function, it | ||
| * creates a **headless tool** that interrupts agent execution and delegates the | ||
| * implementation to the client (e.g. via `useStream({ tools: [...] })`). | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import { | ||
| tool as coreTool, | ||
| DynamicStructuredTool, | ||
| type ToolRunnableConfig, | ||
| } from "@langchain/core/tools"; | ||
| import type { | ||
| InteropZodObject, | ||
| InferInteropZodInput, | ||
| InferInteropZodOutput, | ||
| } from "@langchain/core/utils/types"; | ||
|
|
||
| /** | ||
| * Configuration fields for creating a headless tool. | ||
| */ | ||
| export type HeadlessToolFields< | ||
| SchemaT extends InteropZodObject, | ||
| NameT extends string = string, | ||
| > = { | ||
| /** The name of the tool. Used by the client to match implementations. */ | ||
| name: NameT; | ||
| /** Description of what the tool does. */ | ||
| description: string; | ||
| /** The Zod schema defining the tool's input. */ | ||
| schema: SchemaT; | ||
| }; | ||
|
|
||
| /** | ||
| * A tool implementation that pairs a headless tool with its execution function. | ||
| * | ||
| * Created by calling `.implement()` on a {@link HeadlessTool}. | ||
| * Pass to `useStream({ tools: [...] })` on the client side. | ||
| */ | ||
| export type HeadlessToolImplementation< | ||
| SchemaT extends InteropZodObject = InteropZodObject, | ||
| OutputT = unknown, | ||
| NameT extends string = string, | ||
| > = { | ||
| tool: HeadlessTool<SchemaT, NameT>; | ||
| execute: (args: InferInteropZodOutput<SchemaT>) => Promise<OutputT>; | ||
| }; | ||
|
|
||
| /** | ||
| * A headless tool that always interrupts agent execution on the server. | ||
| * | ||
| * The implementation is provided separately on the client via | ||
| * `useStream({ tools: [...] })` using `.implement()`. | ||
| */ | ||
| export type HeadlessTool< | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think we should call this
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already put it on the v2 wish list. Happy to clean this up when the time is right 😉 |
||
| SchemaT extends InteropZodObject = InteropZodObject, | ||
| NameT extends string = string, | ||
| > = DynamicStructuredTool< | ||
| SchemaT, | ||
| InferInteropZodOutput<SchemaT>, | ||
| InferInteropZodInput<SchemaT>, | ||
| unknown, | ||
| unknown, | ||
| NameT | ||
| > & { | ||
| /** | ||
| * Pairs this headless tool with a client-side implementation. | ||
| * | ||
| * The returned object should be passed to `useStream({ tools: [...] })`. | ||
| * The SDK matches the implementation to the tool by name and calls | ||
| * `execute` with the typed arguments from the interrupt payload. | ||
| * | ||
| * @param execute - The function that implements the tool on the client | ||
| */ | ||
| implement: <OutputT>( | ||
| execute: (args: InferInteropZodOutput<SchemaT>) => Promise<OutputT> | ||
| ) => HeadlessToolImplementation<SchemaT, OutputT, NameT>; | ||
| }; | ||
|
|
||
| function createHeadlessTool< | ||
| SchemaT extends InteropZodObject, | ||
| NameT extends string, | ||
| >(fields: HeadlessToolFields<SchemaT, NameT>): HeadlessTool<SchemaT, NameT> { | ||
| const { name, description, schema } = fields; | ||
|
|
||
| const wrappedTool = coreTool( | ||
| async ( | ||
| args: InferInteropZodOutput<SchemaT>, | ||
| config?: ToolRunnableConfig | ||
| ) => { | ||
| const { interrupt } = await import("@langchain/langgraph"); | ||
| return interrupt({ | ||
| type: "tool", | ||
| toolCall: { | ||
| id: config?.toolCall?.id, | ||
| name, | ||
| args, | ||
| }, | ||
| }); | ||
| }, | ||
| { | ||
| name, | ||
| description, | ||
| schema, | ||
| metadata: { | ||
| headlessTool: true, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| const headlessTool: HeadlessTool<SchemaT, NameT> = Object.assign( | ||
| wrappedTool, | ||
| { | ||
| implement: <OutputT>( | ||
| execute: (args: InferInteropZodOutput<SchemaT>) => Promise<OutputT> | ||
| ): HeadlessToolImplementation<SchemaT, OutputT, NameT> => ({ | ||
| tool: headlessTool, | ||
| execute, | ||
| }), | ||
| } | ||
| ) as HeadlessTool<SchemaT, NameT>; | ||
|
|
||
| return headlessTool; | ||
| } | ||
|
|
||
| /** | ||
| * The headless overload signature added to the core `tool` function. | ||
| * | ||
| * When called **without** an implementation function — just `tool({ name, description, schema })` — | ||
| * returns a {@link HeadlessTool} that interrupts on every agent invocation. | ||
| * The client provides the implementation via `useStream({ tools: [...] })`. | ||
| */ | ||
| type HeadlessToolOverload = { | ||
| <SchemaT extends InteropZodObject, NameT extends string>( | ||
| fields: HeadlessToolFields<SchemaT, NameT> | ||
| ): HeadlessTool<SchemaT, NameT>; | ||
| }; | ||
|
|
||
| /** | ||
| * Unified tool primitive for LangChain agents. | ||
| * | ||
| * Enhances the `tool` function from `@langchain/core/tools` with a headless | ||
| * overload: when called **without** an implementation function, the tool | ||
| * interrupts agent execution and lets the client supply the implementation. | ||
| * | ||
| * --- | ||
| * | ||
| * **Normal tool** — pass an implementation function as the first argument: | ||
| * | ||
| * ```typescript | ||
| * import { tool } from "langchain/tools"; | ||
| * import { z } from "zod"; | ||
| * | ||
| * const getWeather = tool( | ||
| * async ({ city }) => `The weather in ${city} is sunny.`, | ||
| * { | ||
| * name: "get_weather", | ||
| * description: "Get the weather for a city", | ||
| * schema: z.object({ city: z.string() }), | ||
| * } | ||
| * ); | ||
| * ``` | ||
| * | ||
| * --- | ||
| * | ||
| * **Headless tool** — omit the implementation; the client provides it later: | ||
| * | ||
| * ```typescript | ||
| * import { tool } from "langchain/tools"; | ||
| * import { z } from "zod"; | ||
| * | ||
| * // Server: define the tool shape — no implementation needed | ||
| * export const getLocation = tool({ | ||
| * name: "get_location", | ||
| * description: "Get the user's current GPS location", | ||
| * schema: z.object({ | ||
| * highAccuracy: z.boolean().optional().describe("Request high accuracy GPS"), | ||
| * }), | ||
| * }); | ||
| * | ||
| * // Server: register with the agent | ||
| * const agent = createAgent({ | ||
| * model: "openai:gpt-4o", | ||
| * tools: [getLocation], | ||
| * }); | ||
| * | ||
| * // Client: provide the implementation in useStream | ||
| * const stream = useStream({ | ||
| * assistantId: "agent", | ||
| * tools: [ | ||
| * getLocation.implement(async ({ highAccuracy }) => { | ||
| * return new Promise((resolve, reject) => { | ||
| * navigator.geolocation.getCurrentPosition( | ||
| * (pos) => resolve({ | ||
| * latitude: pos.coords.latitude, | ||
| * longitude: pos.coords.longitude, | ||
| * }), | ||
| * (err) => reject(new Error(err.message)), | ||
| * { enableHighAccuracy: highAccuracy } | ||
| * ); | ||
| * }); | ||
| * }), | ||
| * ], | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export const tool: HeadlessToolOverload & typeof coreTool = (( | ||
| funcOrFields: unknown, | ||
| fields?: unknown | ||
| ) => { | ||
| if (typeof funcOrFields !== "function") { | ||
| return createHeadlessTool( | ||
| funcOrFields as HeadlessToolFields<InteropZodObject, string> | ||
| ); | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| return (coreTool as any)(funcOrFields, fields); | ||
| }) as HeadlessToolOverload & typeof coreTool; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * LangChain Tools | ||
| * | ||
| * This module provides tool utilities for LangChain agents. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| export { | ||
| tool, | ||
| type HeadlessTool, | ||
| type HeadlessToolFields, | ||
| type HeadlessToolImplementation, | ||
| } from "./headless.js"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does this need to be its own type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's consumed by
useStreamand represents a tool definition with implementation. This is NOT a traditionalDynamicStructuredToolas it represents the pairing between a tool definition and its implementation.