@langchain/langgraph 0.0.22 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +11 -28
  2. package/dist/graph/graph.d.ts +1 -1
  3. package/dist/graph/state.d.ts +2 -2
  4. package/dist/index.cjs +18 -18
  5. package/dist/index.d.ts +1 -4
  6. package/dist/index.js +4 -4
  7. package/dist/prebuilt/agent_executor.cjs +1 -1
  8. package/dist/prebuilt/agent_executor.d.ts +1 -1
  9. package/dist/prebuilt/agent_executor.js +1 -1
  10. package/dist/prebuilt/chat_agent_executor.cjs +1 -2
  11. package/dist/prebuilt/chat_agent_executor.d.ts +1 -1
  12. package/dist/prebuilt/chat_agent_executor.js +1 -2
  13. package/dist/prebuilt/react_agent_executor.cjs +7 -7
  14. package/dist/prebuilt/react_agent_executor.js +7 -7
  15. package/dist/prebuilt/tool_executor.cjs +1 -3
  16. package/dist/prebuilt/tool_executor.js +1 -3
  17. package/dist/pregel/index.cjs +0 -3
  18. package/dist/pregel/index.d.ts +1 -1
  19. package/dist/pregel/index.js +0 -3
  20. package/dist/tests/chatbot.int.test.js +7 -2
  21. package/dist/tests/prebuilt.int.test.js +7 -4
  22. package/dist/tests/prebuilt.test.d.ts +1 -20
  23. package/dist/tests/prebuilt.test.js +1 -60
  24. package/dist/tests/pregel.io.test.js +1 -1
  25. package/dist/tests/pregel.test.js +11 -11
  26. package/dist/tests/pregel.write.test.js +1 -1
  27. package/dist/tests/tracing.int.test.js +6 -5
  28. package/dist/tests/tracing.test.d.ts +1 -0
  29. package/dist/tests/tracing.test.js +266 -0
  30. package/dist/tests/utils.d.ts +15 -0
  31. package/dist/tests/utils.js +59 -0
  32. package/dist/utils.cjs +19 -6
  33. package/dist/utils.d.ts +2 -0
  34. package/dist/utils.js +20 -7
  35. package/dist/web.cjs +20 -0
  36. package/dist/web.d.ts +4 -0
  37. package/dist/web.js +4 -0
  38. package/package.json +15 -2
  39. package/web.cjs +1 -0
  40. package/web.d.cts +1 -0
  41. package/web.d.ts +1 -0
  42. package/web.js +1 -0
package/README.md CHANGED
@@ -46,7 +46,7 @@ And now we're ready! The graph below contains a single node called `"oracle"` th
46
46
  ```ts
47
47
  import { ChatOpenAI } from "@langchain/openai";
48
48
  import { HumanMessage, BaseMessage, } from "@langchain/core/messages";
49
- import { END, MessageGraph } from "@langchain/langgraph";
49
+ import { START, END, MessageGraph } from "@langchain/langgraph";
50
50
 
51
51
  const model = new ChatOpenAI({ temperature: 0 });
52
52
 
@@ -58,7 +58,7 @@ graph.addNode("oracle", async (state: BaseMessage[]) => {
58
58
 
59
59
  graph.addEdge("oracle", END);
60
60
 
61
- graph.setEntryPoint("oracle");
61
+ graph.addEdge(START, "oracle");
62
62
 
63
63
  const runnable = graph.compile();
64
64
  ```
@@ -90,7 +90,7 @@ So what did we do here? Let's break it down step by step:
90
90
  1. First, we initialize our model and a `MessageGraph`.
91
91
  2. Next, we add a single node to the graph, called `"oracle"`, which simply calls the model with the given input.
92
92
  3. We add an edge from this `"oracle"` node to the special value `END`. This means that execution will end after current node.
93
- 4. We set `"oracle"` as the entrypoint to the graph.
93
+ 4. We set `"oracle"` as the entrypoint to the graph by adding an edge from the special `START` value to it.
94
94
  5. We compile the graph, ensuring that no more modifications to it can be made.
95
95
 
96
96
  Then, when we execute the graph:
@@ -185,7 +185,7 @@ graph.addNode("calculator", async (state: BaseMessage[]) => {
185
185
 
186
186
  graph.addEdge("calculator", END);
187
187
 
188
- graph.setEntryPoint("oracle");
188
+ graph.addEdge(START, "oracle");
189
189
  ```
190
190
 
191
191
  Now let's think - what do we want to have happen?
@@ -477,7 +477,7 @@ const callTool = async (
477
477
  We can now put it all together and define the graph!
478
478
 
479
479
  ```typescript
480
- import { StateGraph, END } from "@langchain/langgraph";
480
+ import { StateGraph, START, END } from "@langchain/langgraph";
481
481
  import { RunnableLambda } from "@langchain/core/runnables";
482
482
 
483
483
  // Define a new graph
@@ -491,7 +491,7 @@ workflow.addNode("action", callTool);
491
491
 
492
492
  // Set the entrypoint as `agent`
493
493
  // This means that this node is the first one called
494
- workflow.setEntryPoint("agent");
494
+ workflow.addEdge(START, "agent");
495
495
 
496
496
  // We now add a conditional edge
497
497
  workflow.addConditionalEdges(
@@ -723,31 +723,14 @@ This takes three arguments:
723
723
  - `condition`: A function to call to decide what to do next. The input will be the output of the start node. It should return a string that is present in `conditionalEdgeMapping` and represents the edge to take.
724
724
  - `conditionalEdgeMapping`: A mapping of string to string. The keys should be strings that may be returned by `condition`. The values should be the downstream node to call if that condition is returned.
725
725
 
726
- ### `.setEntryPoint`
726
+ ### `START`
727
727
 
728
728
  ```typescript
729
- setEntryPoint(key: string): void
729
+ import { START } from "@langchain/langgraph";
730
730
  ```
731
731
 
732
- The entrypoint to the graph.
733
- This is the node that is first called.
734
- It only takes one argument:
735
-
736
- - `key`: The name of the node that should be called first.
737
-
738
- ### `.setFinishPoint`
739
-
740
- ```typescript
741
- setFinishPoint(key: string): void
742
- ```
743
-
744
- This is the exit point of the graph.
745
- When this node is called, the results will be the final result from the graph.
746
- It only has one argument:
747
-
748
- - `key`: The name of the node that, when called, will return the results of calling it as the final output
749
-
750
- Note: This does not need to be called if at any point you previously created an edge (conditional or normal) to `END`
732
+ This is a special node representing the start of the graph.
733
+ This means that anything with an edge from this node will be the entrypoint of the graph.
751
734
 
752
735
  ### `END`
753
736
 
@@ -827,7 +810,7 @@ workflow.addNode("agent", agent);
827
810
  workflow.addNode("tools", executeTools);
828
811
 
829
812
  // We now set the entry point to be this first agent
830
- workflow.setEntryPoint("firstAgent");
813
+ workflow.addEdge(START, "firstAgent");
831
814
 
832
815
  // We define the same edges as before
833
816
  workflow.addConditionalEdges("agent", shouldContinue, {
@@ -19,7 +19,7 @@ export declare class Branch<IO, N extends string> {
19
19
  compile(writer: (dests: string[]) => Runnable | undefined, reader?: (config: RunnableConfig) => IO): RunnableCallable<unknown, unknown>;
20
20
  _route(input: IO, config: RunnableConfig, writer: (dests: string[]) => Runnable | undefined, reader?: (config: RunnableConfig) => IO): Promise<Runnable | undefined>;
21
21
  }
22
- export declare class Graph<const N extends string = typeof END, RunInput = any, RunOutput = any> {
22
+ export declare class Graph<N extends string = typeof END, RunInput = any, RunOutput = any> {
23
23
  nodes: Record<N, Runnable<RunInput, RunOutput>>;
24
24
  edges: Set<[N | typeof START, N | typeof END]>;
25
25
  branches: Record<string, Record<string, Branch<RunInput, N>>>;
@@ -24,7 +24,7 @@ export interface StateGraphArgs<Channels extends object | unknown> {
24
24
  __root__: Channels;
25
25
  }>;
26
26
  }
27
- export declare class StateGraph<const State extends object | unknown, const Update extends object | unknown = Partial<State>, const N extends string = typeof START> extends Graph<N, State, Update> {
27
+ export declare class StateGraph<State extends object | unknown, Update extends object | unknown = Partial<State>, N extends string = typeof START> extends Graph<N, State, Update> {
28
28
  channels: Record<string, BaseChannel>;
29
29
  waitingEdges: Set<[N[], N]>;
30
30
  constructor(fields: StateGraphArgs<State>);
@@ -37,7 +37,7 @@ export declare class StateGraph<const State extends object | unknown, const Upda
37
37
  interruptAfter?: N[] | All;
38
38
  }): CompiledStateGraph<State, Update, N>;
39
39
  }
40
- export declare class CompiledStateGraph<const State extends object | unknown, const Update extends object | unknown = Partial<State>, const N extends string = typeof START> extends CompiledGraph<N, State, Update> {
40
+ export declare class CompiledStateGraph<State extends object | unknown, Update extends object | unknown = Partial<State>, N extends string = typeof START> extends CompiledGraph<N, State, Update> {
41
41
  builder: StateGraph<State, Update, N>;
42
42
  attachNode(key: typeof START, node?: never): void;
43
43
  attachNode(key: N, node: Runnable<State, Update, RunnableConfig>): void;
package/dist/index.cjs CHANGED
@@ -1,20 +1,20 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EmptyChannelError = exports.InvalidUpdateError = exports.GraphValueError = exports.GraphRecursionError = exports.BaseCheckpointSaver = exports.emptyCheckpoint = exports.copyCheckpoint = exports.MemorySaver = exports.MessageGraph = exports.StateGraph = exports.START = exports.Graph = exports.END = void 0;
4
- var index_js_1 = require("./graph/index.cjs");
5
- Object.defineProperty(exports, "END", { enumerable: true, get: function () { return index_js_1.END; } });
6
- Object.defineProperty(exports, "Graph", { enumerable: true, get: function () { return index_js_1.Graph; } });
7
- Object.defineProperty(exports, "START", { enumerable: true, get: function () { return index_js_1.START; } });
8
- Object.defineProperty(exports, "StateGraph", { enumerable: true, get: function () { return index_js_1.StateGraph; } });
9
- Object.defineProperty(exports, "MessageGraph", { enumerable: true, get: function () { return index_js_1.MessageGraph; } });
10
- var memory_js_1 = require("./checkpoint/memory.cjs");
11
- Object.defineProperty(exports, "MemorySaver", { enumerable: true, get: function () { return memory_js_1.MemorySaver; } });
12
- var base_js_1 = require("./checkpoint/base.cjs");
13
- Object.defineProperty(exports, "copyCheckpoint", { enumerable: true, get: function () { return base_js_1.copyCheckpoint; } });
14
- Object.defineProperty(exports, "emptyCheckpoint", { enumerable: true, get: function () { return base_js_1.emptyCheckpoint; } });
15
- Object.defineProperty(exports, "BaseCheckpointSaver", { enumerable: true, get: function () { return base_js_1.BaseCheckpointSaver; } });
16
- var errors_js_1 = require("./errors.cjs");
17
- Object.defineProperty(exports, "GraphRecursionError", { enumerable: true, get: function () { return errors_js_1.GraphRecursionError; } });
18
- Object.defineProperty(exports, "GraphValueError", { enumerable: true, get: function () { return errors_js_1.GraphValueError; } });
19
- Object.defineProperty(exports, "InvalidUpdateError", { enumerable: true, get: function () { return errors_js_1.InvalidUpdateError; } });
20
- Object.defineProperty(exports, "EmptyChannelError", { enumerable: true, get: function () { return errors_js_1.EmptyChannelError; } });
17
+ const async_local_storage_js_1 = require("./setup/async_local_storage.cjs");
18
+ // Initialize global async local storage instance for tracing
19
+ /* #__PURE__ */ (0, async_local_storage_js_1.initializeAsyncLocalStorageSingleton)();
20
+ __exportStar(require("./web.cjs"), exports);
package/dist/index.d.ts CHANGED
@@ -1,4 +1 @@
1
- export { END, Graph, type StateGraphArgs, START, StateGraph, type CompiledStateGraph, MessageGraph, } from "./graph/index.js";
2
- export { MemorySaver } from "./checkpoint/memory.js";
3
- export { type Checkpoint, type CheckpointMetadata, copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./checkpoint/base.js";
4
- export { GraphRecursionError, GraphValueError, InvalidUpdateError, EmptyChannelError, } from "./errors.js";
1
+ export * from "./web.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { END, Graph, START, StateGraph, MessageGraph, } from "./graph/index.js";
2
- export { MemorySaver } from "./checkpoint/memory.js";
3
- export { copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./checkpoint/base.js";
4
- export { GraphRecursionError, GraphValueError, InvalidUpdateError, EmptyChannelError, } from "./errors.js";
1
+ import { initializeAsyncLocalStorageSingleton } from "./setup/async_local_storage.js";
2
+ // Initialize global async local storage instance for tracing
3
+ /* #__PURE__ */ initializeAsyncLocalStorageSingleton();
4
+ export * from "./web.js";
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createAgentExecutor = void 0;
4
4
  const tool_executor_js_1 = require("./tool_executor.cjs");
5
5
  const state_js_1 = require("../graph/state.cjs");
6
- const index_js_1 = require("../index.cjs");
6
+ const index_js_1 = require("../graph/index.cjs");
7
7
  function createAgentExecutor({ agentRunnable, tools, }) {
8
8
  let toolExecutor;
9
9
  if (!Array.isArray(tools)) {
@@ -16,5 +16,5 @@ export interface AgentExecutorState {
16
16
  export declare function createAgentExecutor({ agentRunnable, tools, }: {
17
17
  agentRunnable: Runnable;
18
18
  tools: Array<Tool> | ToolExecutor;
19
- }): import("../index.js").CompiledStateGraph<AgentExecutorState, Partial<AgentExecutorState>, "__start__" | "agent" | "action">;
19
+ }): import("../graph/state.js").CompiledStateGraph<AgentExecutorState, Partial<AgentExecutorState>, "__start__" | "agent" | "action">;
20
20
  export {};
@@ -1,6 +1,6 @@
1
1
  import { ToolExecutor } from "./tool_executor.js";
2
2
  import { StateGraph } from "../graph/state.js";
3
- import { END, START } from "../index.js";
3
+ import { END, START } from "../graph/index.js";
4
4
  export function createAgentExecutor({ agentRunnable, tools, }) {
5
5
  let toolExecutor;
6
6
  if (!Array.isArray(tools)) {
@@ -6,7 +6,7 @@ const messages_1 = require("@langchain/core/messages");
6
6
  const runnables_1 = require("@langchain/core/runnables");
7
7
  const tool_executor_js_1 = require("./tool_executor.cjs");
8
8
  const state_js_1 = require("../graph/state.cjs");
9
- const index_js_1 = require("../index.cjs");
9
+ const index_js_1 = require("../graph/index.cjs");
10
10
  function createFunctionCallingExecutor({ model, tools, }) {
11
11
  let toolExecutor;
12
12
  let toolClasses;
@@ -26,7 +26,6 @@ function createFunctionCallingExecutor({ model, tools, }) {
26
26
  const toolsAsOpenAIFunctions = toolClasses.map((tool) => (0, function_calling_1.convertToOpenAIFunction)(tool));
27
27
  const newModel = model.bind({
28
28
  functions: toolsAsOpenAIFunctions,
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
29
  });
31
30
  // Define the function that determines whether to continue or not
32
31
  const shouldContinue = (state) => {
@@ -2,7 +2,7 @@ import { StructuredTool } from "@langchain/core/tools";
2
2
  import { BaseMessage } from "@langchain/core/messages";
3
3
  import { ToolExecutor } from "./tool_executor.js";
4
4
  import { CompiledStateGraph } from "../graph/state.js";
5
- import { START } from "../index.js";
5
+ import { START } from "../graph/index.js";
6
6
  export type FunctionCallingExecutorState = {
7
7
  messages: Array<BaseMessage>;
8
8
  };
@@ -3,7 +3,7 @@ import { FunctionMessage } from "@langchain/core/messages";
3
3
  import { RunnableLambda } from "@langchain/core/runnables";
4
4
  import { ToolExecutor } from "./tool_executor.js";
5
5
  import { StateGraph, } from "../graph/state.js";
6
- import { END, START } from "../index.js";
6
+ import { END, START } from "../graph/index.js";
7
7
  export function createFunctionCallingExecutor({ model, tools, }) {
8
8
  let toolExecutor;
9
9
  let toolClasses;
@@ -23,7 +23,6 @@ export function createFunctionCallingExecutor({ model, tools, }) {
23
23
  const toolsAsOpenAIFunctions = toolClasses.map((tool) => convertToOpenAIFunction(tool));
24
24
  const newModel = model.bind({
25
25
  functions: toolsAsOpenAIFunctions,
26
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
26
  });
28
27
  // Define the function that determines whether to continue or not
29
28
  const shouldContinue = (state) => {
@@ -48,15 +48,15 @@ function createReactAgent(props) {
48
48
  return "continue";
49
49
  }
50
50
  };
51
- const callModel = async (state) => {
51
+ const callModel = async (state, config) => {
52
52
  const { messages } = state;
53
53
  // TODO: Auto-promote streaming.
54
- return { messages: [await modelRunnable.invoke(messages)] };
54
+ return { messages: [await modelRunnable.invoke(messages, config)] };
55
55
  };
56
56
  const workflow = new index_js_1.StateGraph({
57
57
  channels: schema,
58
58
  })
59
- .addNode("agent", new runnables_1.RunnableLambda({ func: callModel }).withConfig({ runName: "agent" }))
59
+ .addNode("agent", runnables_1.RunnableLambda.from(callModel).withConfig({ runName: "agent" }))
60
60
  .addNode("tools", new tool_node_js_1.ToolNode(toolClasses))
61
61
  .addEdge(index_js_1.START, "agent")
62
62
  .addConditionalEdges("agent", shouldContinue, {
@@ -75,9 +75,9 @@ function _createModelWrapper(modelWithTools, messageModifier) {
75
75
  if (!messageModifier) {
76
76
  return modelWithTools;
77
77
  }
78
- const endict = new runnables_1.RunnableLambda({
79
- func: (messages) => ({ messages }),
80
- });
78
+ const endict = runnables_1.RunnableLambda.from((messages) => ({
79
+ messages,
80
+ }));
81
81
  if (typeof messageModifier === "string") {
82
82
  const systemMessage = new messages_1.SystemMessage(messageModifier);
83
83
  const prompt = prompts_1.ChatPromptTemplate.fromMessages([
@@ -87,7 +87,7 @@ function _createModelWrapper(modelWithTools, messageModifier) {
87
87
  return endict.pipe(prompt).pipe(modelWithTools);
88
88
  }
89
89
  if (typeof messageModifier === "function") {
90
- const lambda = new runnables_1.RunnableLambda({ func: messageModifier }).withConfig({
90
+ const lambda = runnables_1.RunnableLambda.from(messageModifier).withConfig({
91
91
  runName: "message_modifier",
92
92
  });
93
93
  return lambda.pipe(modelWithTools);
@@ -45,15 +45,15 @@ export function createReactAgent(props) {
45
45
  return "continue";
46
46
  }
47
47
  };
48
- const callModel = async (state) => {
48
+ const callModel = async (state, config) => {
49
49
  const { messages } = state;
50
50
  // TODO: Auto-promote streaming.
51
- return { messages: [await modelRunnable.invoke(messages)] };
51
+ return { messages: [await modelRunnable.invoke(messages, config)] };
52
52
  };
53
53
  const workflow = new StateGraph({
54
54
  channels: schema,
55
55
  })
56
- .addNode("agent", new RunnableLambda({ func: callModel }).withConfig({ runName: "agent" }))
56
+ .addNode("agent", RunnableLambda.from(callModel).withConfig({ runName: "agent" }))
57
57
  .addNode("tools", new ToolNode(toolClasses))
58
58
  .addEdge(START, "agent")
59
59
  .addConditionalEdges("agent", shouldContinue, {
@@ -71,9 +71,9 @@ function _createModelWrapper(modelWithTools, messageModifier) {
71
71
  if (!messageModifier) {
72
72
  return modelWithTools;
73
73
  }
74
- const endict = new RunnableLambda({
75
- func: (messages) => ({ messages }),
76
- });
74
+ const endict = RunnableLambda.from((messages) => ({
75
+ messages,
76
+ }));
77
77
  if (typeof messageModifier === "string") {
78
78
  const systemMessage = new SystemMessage(messageModifier);
79
79
  const prompt = ChatPromptTemplate.fromMessages([
@@ -83,7 +83,7 @@ function _createModelWrapper(modelWithTools, messageModifier) {
83
83
  return endict.pipe(prompt).pipe(modelWithTools);
84
84
  }
85
85
  if (typeof messageModifier === "function") {
86
- const lambda = new RunnableLambda({ func: messageModifier }).withConfig({
86
+ const lambda = RunnableLambda.from(messageModifier).withConfig({
87
87
  runName: "message_modifier",
88
88
  });
89
89
  return lambda.pipe(modelWithTools);
@@ -9,9 +9,7 @@ class ToolExecutor extends runnables_1.RunnableBinding {
9
9
  invalidToolMsgTemplate: INVALID_TOOL_MSG_TEMPLATE,
10
10
  ...fields,
11
11
  };
12
- const bound = new runnables_1.RunnableLambda({
13
- func: async (input, options) => this._execute(input, options?.config),
14
- });
12
+ const bound = runnables_1.RunnableLambda.from(async (input, config) => this._execute(input, config));
15
13
  super({
16
14
  bound,
17
15
  config: {},
@@ -6,9 +6,7 @@ export class ToolExecutor extends RunnableBinding {
6
6
  invalidToolMsgTemplate: INVALID_TOOL_MSG_TEMPLATE,
7
7
  ...fields,
8
8
  };
9
- const bound = new RunnableLambda({
10
- func: async (input, options) => this._execute(input, options?.config),
11
- });
9
+ const bound = RunnableLambda.from(async (input, config) => this._execute(input, config));
12
10
  super({
13
11
  bound,
14
12
  config: {},
@@ -11,7 +11,6 @@ const validate_js_1 = require("./validate.cjs");
11
11
  const io_js_1 = require("./io.cjs");
12
12
  const write_js_1 = require("./write.cjs");
13
13
  const constants_js_1 = require("../constants.cjs");
14
- const async_local_storage_js_1 = require("../setup/async_local_storage.cjs");
15
14
  const errors_js_1 = require("../errors.cjs");
16
15
  const DEFAULT_LOOP_LIMIT = 25;
17
16
  function isString(value) {
@@ -157,8 +156,6 @@ class Pregel extends runnables_1.Runnable {
157
156
  writable: true,
158
157
  value: void 0
159
158
  });
160
- // Initialize global async local storage instance for tracing
161
- (0, async_local_storage_js_1.initializeAsyncLocalStorageSingleton)();
162
159
  this.nodes = fields.nodes;
163
160
  this.channels = fields.channels;
164
161
  this.autoValidate = fields.autoValidate ?? this.autoValidate;
@@ -66,7 +66,7 @@ export interface PregelOptions<Nn extends StrRecord<string, PregelNode>, Cc exte
66
66
  }
67
67
  export type PregelInputType = any;
68
68
  export type PregelOutputType = any;
69
- export declare class Pregel<const Nn extends StrRecord<string, PregelNode>, const Cc extends StrRecord<string, BaseChannel>> extends Runnable<PregelInputType, PregelOutputType, PregelOptions<Nn, Cc>> implements PregelInterface<Nn, Cc> {
69
+ export declare class Pregel<Nn extends StrRecord<string, PregelNode>, Cc extends StrRecord<string, BaseChannel>> extends Runnable<PregelInputType, PregelOutputType, PregelOptions<Nn, Cc>> implements PregelInterface<Nn, Cc> {
70
70
  static lc_name(): string;
71
71
  lc_namespace: string[];
72
72
  nodes: Nn;
@@ -8,7 +8,6 @@ import { validateGraph, validateKeys } from "./validate.js";
8
8
  import { mapInput, mapOutputUpdates, mapOutputValues, readChannel, readChannels, single, } from "./io.js";
9
9
  import { ChannelWrite, PASSTHROUGH } from "./write.js";
10
10
  import { CONFIG_KEY_READ, CONFIG_KEY_SEND, INTERRUPT, TAG_HIDDEN, } from "../constants.js";
11
- import { initializeAsyncLocalStorageSingleton } from "../setup/async_local_storage.js";
12
11
  import { EmptyChannelError, GraphRecursionError, GraphValueError, InvalidUpdateError, } from "../errors.js";
13
12
  const DEFAULT_LOOP_LIMIT = 25;
14
13
  function isString(value) {
@@ -153,8 +152,6 @@ export class Pregel extends Runnable {
153
152
  writable: true,
154
153
  value: void 0
155
154
  });
156
- // Initialize global async local storage instance for tracing
157
- initializeAsyncLocalStorageSingleton();
158
155
  this.nodes = fields.nodes;
159
156
  this.channels = fields.channels;
160
157
  this.autoValidate = fields.autoValidate ?? this.autoValidate;
@@ -1,16 +1,21 @@
1
- import { describe, it } from "@jest/globals";
1
+ import { beforeAll, describe, it } from "@jest/globals";
2
2
  import { ChatOpenAI } from "@langchain/openai";
3
3
  import { HumanMessage, ToolMessage, } from "@langchain/core/messages";
4
4
  import { Calculator } from "@langchain/community/tools/calculator";
5
5
  import { convertToOpenAITool } from "@langchain/core/utils/function_calling";
6
6
  import { END, MessageGraph, START } from "../index.js";
7
+ import { initializeAsyncLocalStorageSingleton } from "../setup/async_local_storage.js";
7
8
  describe("Chatbot", () => {
9
+ beforeAll(() => {
10
+ // Will occur naturally if user imports from main `@langchain/langgraph` endpoint.
11
+ initializeAsyncLocalStorageSingleton();
12
+ });
8
13
  it("Simple chat use-case", async () => {
9
14
  const model = new ChatOpenAI({ temperature: 0 });
10
15
  const graph = new MessageGraph()
11
16
  .addNode("oracle", async (state) => model.invoke(state))
12
17
  .addEdge("oracle", END)
13
- .setEntryPoint("oracle")
18
+ .addEdge(START, "oracle")
14
19
  .compile();
15
20
  const res = await graph.invoke(new HumanMessage("What is 1 + 1?"));
16
21
  console.log(res);
@@ -4,12 +4,15 @@ import { Tool } from "@langchain/core/tools";
4
4
  import { ChatOpenAI } from "@langchain/openai";
5
5
  import { HumanMessage } from "@langchain/core/messages";
6
6
  import { createReactAgent, createFunctionCallingExecutor, } from "../prebuilt/index.js";
7
+ import { initializeAsyncLocalStorageSingleton } from "../setup/async_local_storage.js";
7
8
  // Tracing slows down the tests
8
9
  beforeAll(() => {
9
- process.env.LANGCHAIN_TRACING_V2 = "false";
10
- process.env.LANGCHAIN_ENDPOINT = "";
11
- process.env.LANGCHAIN_API_KEY = "";
12
- process.env.LANGCHAIN_PROJECT = "";
10
+ // process.env.LANGCHAIN_TRACING_V2 = "false";
11
+ // process.env.LANGCHAIN_ENDPOINT = "";
12
+ // process.env.LANGCHAIN_API_KEY = "";
13
+ // process.env.LANGCHAIN_PROJECT = "";
14
+ // Will occur naturally if user imports from main `@langchain/langgraph` endpoint.
15
+ initializeAsyncLocalStorageSingleton();
13
16
  });
14
17
  describe("createFunctionCallingExecutor", () => {
15
18
  it("can call a function", async () => {
@@ -1,20 +1 @@
1
- import { Tool } from "@langchain/core/tools";
2
- import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
- import { BaseChatModel } from "@langchain/core/language_models/chat_models";
4
- import { BaseLLMParams } from "@langchain/core/language_models/llms";
5
- import { BaseMessage } from "@langchain/core/messages";
6
- import { ChatResult } from "@langchain/core/outputs";
7
- export declare class FakeToolCallingChatModel extends BaseChatModel {
8
- sleep?: number;
9
- responses?: BaseMessage[];
10
- thrownErrorString?: string;
11
- idx: number;
12
- constructor(fields: {
13
- sleep?: number;
14
- responses?: BaseMessage[];
15
- thrownErrorString?: string;
16
- } & BaseLLMParams);
17
- _llmType(): string;
18
- _generate(messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
19
- bindTools(_: Tool[]): FakeToolCallingChatModel;
20
- }
1
+ export {};
@@ -3,10 +3,10 @@ import { beforeAll, describe, expect, it } from "@jest/globals";
3
3
  import { PromptTemplate } from "@langchain/core/prompts";
4
4
  import { StructuredTool, Tool } from "@langchain/core/tools";
5
5
  import { FakeStreamingLLM } from "@langchain/core/utils/testing";
6
- import { BaseChatModel } from "@langchain/core/language_models/chat_models";
7
6
  import { AIMessage, HumanMessage, SystemMessage, ToolMessage, } from "@langchain/core/messages";
8
7
  import { RunnableLambda } from "@langchain/core/runnables";
9
8
  import { z } from "zod";
9
+ import { FakeToolCallingChatModel } from "./utils.js";
10
10
  import { createAgentExecutor, createReactAgent } from "../prebuilt/index.js";
11
11
  // Tracing slows down the tests
12
12
  beforeAll(() => {
@@ -197,65 +197,6 @@ describe("PreBuilt", () => {
197
197
  ]);
198
198
  });
199
199
  });
200
- export class FakeToolCallingChatModel extends BaseChatModel {
201
- constructor(fields) {
202
- super(fields);
203
- Object.defineProperty(this, "sleep", {
204
- enumerable: true,
205
- configurable: true,
206
- writable: true,
207
- value: 50
208
- });
209
- Object.defineProperty(this, "responses", {
210
- enumerable: true,
211
- configurable: true,
212
- writable: true,
213
- value: void 0
214
- });
215
- Object.defineProperty(this, "thrownErrorString", {
216
- enumerable: true,
217
- configurable: true,
218
- writable: true,
219
- value: void 0
220
- });
221
- Object.defineProperty(this, "idx", {
222
- enumerable: true,
223
- configurable: true,
224
- writable: true,
225
- value: void 0
226
- });
227
- this.sleep = fields.sleep ?? this.sleep;
228
- this.responses = fields.responses;
229
- this.thrownErrorString = fields.thrownErrorString;
230
- this.idx = 0;
231
- }
232
- _llmType() {
233
- return "fake";
234
- }
235
- async _generate(messages, _options, _runManager) {
236
- if (this.thrownErrorString) {
237
- throw new Error(this.thrownErrorString);
238
- }
239
- const msg = this.responses?.[this.idx] ?? messages[this.idx];
240
- const generation = {
241
- generations: [
242
- {
243
- text: "",
244
- message: msg,
245
- },
246
- ],
247
- };
248
- this.idx += 1;
249
- return generation;
250
- }
251
- bindTools(_) {
252
- return new FakeToolCallingChatModel({
253
- sleep: this.sleep,
254
- responses: this.responses,
255
- thrownErrorString: this.thrownErrorString,
256
- });
257
- }
258
- }
259
200
  describe("createReactAgent", () => {
260
201
  const searchSchema = z.object({
261
202
  query: z.string().describe("The query to search for."),
@@ -269,7 +269,7 @@ describe("mapOutputUpdates", () => {
269
269
  input: null,
270
270
  proc: new RunnablePassthrough(),
271
271
  // @ts-expect-error invalid write
272
- writes: [["someOutputChannelNameThatDoesntMatch", 3]], // this task should be filtered out
272
+ writes: [["someOutputChannelNameThatDoesntMatch", 3]],
273
273
  config: undefined,
274
274
  },
275
275
  ];
@@ -155,21 +155,21 @@ describe("Pregel", () => {
155
155
  });
156
156
  // call method / assertions
157
157
  const expectedDefaults1 = [
158
- false, // debug
159
- "values", // stream mode
160
- "outputKey", // input keys
161
- ["inputKey", "outputKey", "channel3"], // output keys,
158
+ false,
159
+ "values",
160
+ "outputKey",
161
+ ["inputKey", "outputKey", "channel3"],
162
162
  {},
163
- ["one"], // interrupt before
163
+ ["one"],
164
164
  ["one"], // interrupt after
165
165
  ];
166
166
  const expectedDefaults2 = [
167
- true, // debug
168
- "updates", // stream mode
169
- "inputKey", // input keys
170
- "outputKey", // output keys
167
+ true,
168
+ "updates",
169
+ "inputKey",
170
+ "outputKey",
171
171
  { tags: ["hello"] },
172
- "*", // interrupt before
172
+ "*",
173
173
  ["one"], // interrupt after
174
174
  ];
175
175
  expect(pregel._defaults(config1)).toEqual(expectedDefaults1);
@@ -290,7 +290,7 @@ describe("_shouldInterrupt", () => {
290
290
  // call method / assertions
291
291
  expect(_shouldInterrupt(checkpoint, interruptNodes, snapshotChannels, [
292
292
  {
293
- name: "node2", // node2 is not in interrupt nodes
293
+ name: "node2",
294
294
  input: undefined,
295
295
  proc: new RunnablePassthrough(),
296
296
  writes: [],
@@ -34,7 +34,7 @@ describe("ChannelWrite", () => {
34
34
  const writeValues = await channelWrite._getWriteValues(input, config);
35
35
  const expectedWriteValues = {
36
36
  someChannel1: 1,
37
- someChannel2: 2, // value is set to input value since PASSTHROUGH value was specified (with mapper)
37
+ someChannel2: 2,
38
38
  // someChannel3 should be filtered out
39
39
  someChannel4: 2, // value is set to input value since PASSTHROUGH value was specified
40
40
  };
@@ -12,9 +12,10 @@ import { JsonOutputFunctionsParser, JsonOutputToolsParser, } from "langchain/out
12
12
  import { createOpenAIFnRunnable } from "langchain/chains/openai_functions";
13
13
  import { zodToJsonSchema } from "zod-to-json-schema";
14
14
  import { z } from "zod";
15
- import { StateGraph, END } from "../index.js";
16
15
  import { ToolExecutor } from "../prebuilt/tool_executor.js";
17
16
  import { createAgentExecutor } from "../prebuilt/agent_executor.js";
17
+ // Import from main `@langchain/langgraph` endpoint to turn on automatic config passing
18
+ import { StateGraph, END, START } from "../index.js";
18
19
  test.skip("Can invoke with tracing", async () => {
19
20
  const tools = [new TavilySearchResults({ maxResults: 1 })];
20
21
  // Get the prompt to use - you can modify this!
@@ -72,7 +73,7 @@ test.skip("Can invoke with tracing", async () => {
72
73
  .addNode("action", new RunnableLambda({ func: executeTools }))
73
74
  // Set the entrypoint as `agent`
74
75
  // This means that this node is the first one called
75
- .setEntryPoint("agent")
76
+ .addEdge(START, "agent")
76
77
  // We now add a conditional edge
77
78
  .addConditionalEdges(
78
79
  // First, we define the start node. We use `agent`.
@@ -187,7 +188,7 @@ test.skip("Can nest an agent executor", async () => {
187
188
  // Or end work if done
188
189
  FINISH: END,
189
190
  })
190
- .setEntryPoint("supervisor");
191
+ .addEdge(START, "supervisor");
191
192
  const graph = workflow.compile();
192
193
  const streamResults = graph.stream({
193
194
  messages: [
@@ -286,7 +287,7 @@ test.skip("Can nest a graph within a graph", async () => {
286
287
  researcher: "researcher",
287
288
  FINISH: END,
288
289
  })
289
- .setEntryPoint("supervisor");
290
+ .addEdge(START, "supervisor");
290
291
  const graph = workflow.compile();
291
292
  const streamResults = graph.stream({
292
293
  messages: [
@@ -424,7 +425,7 @@ Only add steps to the plan that still NEED to be done. Do not return previously
424
425
  .addNode("agent", executeStep)
425
426
  // Add a replan node
426
427
  .addNode("replan", replanStep)
427
- .setEntryPoint("planner")
428
+ .addEdge(START, "planner")
428
429
  // From plan we go to agent
429
430
  .addEdge("planner", "agent")
430
431
  // From agent, we replan
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,266 @@
1
+ import { expect, it } from "@jest/globals";
2
+ import { AIMessage, HumanMessage } from "@langchain/core/messages";
3
+ import { FakeToolCallingChatModel } from "./utils.js";
4
+ // Import from main `@langchain/langgraph` endpoint to turn on automatic config passing
5
+ import { END, START, StateGraph } from "../index.js";
6
+ it("should pass config through if importing from the primary entrypoint", async () => {
7
+ const stateGraph = new StateGraph({
8
+ channels: { messages: null },
9
+ });
10
+ const graph = stateGraph
11
+ .addNode("testnode", async (_) => {
12
+ const model = new FakeToolCallingChatModel({
13
+ responses: [new AIMessage("hey!")],
14
+ }).withConfig({ runName: "model_call" });
15
+ // Don't explicitly pass config here
16
+ const res = await model.invoke("hello!");
17
+ return { messages: [res] };
18
+ })
19
+ .addEdge(START, "testnode")
20
+ .addConditionalEdges("testnode", async (_state) => {
21
+ const model = new FakeToolCallingChatModel({
22
+ responses: [new AIMessage("hey!")],
23
+ }).withConfig({ runName: "conditional_edge_call" });
24
+ await model.invoke("testing but should be traced");
25
+ return END;
26
+ })
27
+ .compile();
28
+ const eventStream = graph.streamEvents({ messages: [] }, { version: "v2" });
29
+ const events = [];
30
+ for await (const event of eventStream) {
31
+ events.push(event);
32
+ }
33
+ expect(events).toEqual([
34
+ {
35
+ event: "on_chain_start",
36
+ data: {
37
+ input: {
38
+ messages: [],
39
+ },
40
+ },
41
+ name: "LangGraph",
42
+ tags: [],
43
+ run_id: expect.any(String),
44
+ metadata: {},
45
+ },
46
+ {
47
+ event: "on_chain_start",
48
+ data: {
49
+ input: {
50
+ messages: [],
51
+ },
52
+ },
53
+ name: "__start__",
54
+ tags: ["graph:step:0", "langsmith:hidden"],
55
+ run_id: expect.any(String),
56
+ metadata: {},
57
+ },
58
+ {
59
+ event: "on_chain_end",
60
+ data: {
61
+ output: { output: undefined },
62
+ input: {
63
+ messages: [],
64
+ },
65
+ },
66
+ run_id: expect.any(String),
67
+ name: "__start__",
68
+ tags: ["graph:step:0", "langsmith:hidden"],
69
+ metadata: {},
70
+ },
71
+ {
72
+ event: "on_chain_start",
73
+ data: {
74
+ input: {
75
+ messages: [],
76
+ },
77
+ },
78
+ name: "testnode",
79
+ tags: ["graph:step:1"],
80
+ run_id: expect.any(String),
81
+ metadata: {},
82
+ },
83
+ {
84
+ event: "on_chain_start",
85
+ data: {
86
+ input: {
87
+ messages: [],
88
+ },
89
+ },
90
+ name: "RunnableLambda",
91
+ tags: ["seq:step:1"],
92
+ run_id: expect.any(String),
93
+ metadata: {},
94
+ },
95
+ {
96
+ event: "on_chat_model_start",
97
+ data: {
98
+ input: {
99
+ messages: [[new HumanMessage("hello!")]],
100
+ },
101
+ },
102
+ name: "model_call",
103
+ tags: [],
104
+ run_id: expect.any(String),
105
+ metadata: {
106
+ ls_model_type: "chat",
107
+ ls_stop: undefined,
108
+ },
109
+ },
110
+ {
111
+ event: "on_chat_model_end",
112
+ data: {
113
+ output: new AIMessage("hey!"),
114
+ input: {
115
+ messages: [[new HumanMessage("hello!")]],
116
+ },
117
+ },
118
+ run_id: expect.any(String),
119
+ name: "model_call",
120
+ tags: [],
121
+ metadata: {
122
+ ls_model_type: "chat",
123
+ ls_stop: undefined,
124
+ },
125
+ },
126
+ {
127
+ event: "on_chain_end",
128
+ data: {
129
+ output: {
130
+ messages: [new AIMessage("hey!")],
131
+ },
132
+ input: {
133
+ messages: [],
134
+ },
135
+ },
136
+ run_id: expect.any(String),
137
+ name: "RunnableLambda",
138
+ tags: ["seq:step:1"],
139
+ metadata: {},
140
+ },
141
+ {
142
+ event: "on_chain_start",
143
+ data: {
144
+ input: {
145
+ messages: [new AIMessage("hey!")],
146
+ },
147
+ },
148
+ name: "ChannelWrite<messages,testnode>",
149
+ tags: ["seq:step:2", "langsmith:hidden"],
150
+ run_id: expect.any(String),
151
+ metadata: {},
152
+ },
153
+ {
154
+ event: "on_chain_end",
155
+ data: {
156
+ output: { output: undefined },
157
+ input: {
158
+ messages: [new AIMessage("hey!")],
159
+ },
160
+ },
161
+ run_id: expect.any(String),
162
+ name: "ChannelWrite<messages,testnode>",
163
+ tags: ["seq:step:2", "langsmith:hidden"],
164
+ metadata: {},
165
+ },
166
+ {
167
+ event: "on_chain_start",
168
+ data: {
169
+ input: {
170
+ input: undefined,
171
+ },
172
+ },
173
+ name: "func",
174
+ tags: ["seq:step:3"],
175
+ run_id: expect.any(String),
176
+ metadata: {},
177
+ },
178
+ {
179
+ event: "on_chat_model_start",
180
+ data: {
181
+ input: {
182
+ messages: [[new HumanMessage("testing but should be traced")]],
183
+ },
184
+ },
185
+ name: "conditional_edge_call",
186
+ tags: [],
187
+ run_id: expect.any(String),
188
+ metadata: {
189
+ ls_model_type: "chat",
190
+ ls_stop: undefined,
191
+ },
192
+ },
193
+ {
194
+ event: "on_chat_model_end",
195
+ data: {
196
+ output: new AIMessage("hey!"),
197
+ input: {
198
+ messages: [[new HumanMessage("testing but should be traced")]],
199
+ },
200
+ },
201
+ run_id: expect.any(String),
202
+ name: "conditional_edge_call",
203
+ tags: [],
204
+ metadata: {
205
+ ls_model_type: "chat",
206
+ ls_stop: undefined,
207
+ },
208
+ },
209
+ {
210
+ event: "on_chain_end",
211
+ data: {
212
+ output: {
213
+ output: undefined,
214
+ },
215
+ input: {
216
+ input: undefined,
217
+ },
218
+ },
219
+ run_id: expect.any(String),
220
+ name: "func",
221
+ tags: ["seq:step:3"],
222
+ metadata: {},
223
+ },
224
+ {
225
+ event: "on_chain_end",
226
+ data: {
227
+ output: { output: undefined },
228
+ input: {
229
+ messages: [],
230
+ },
231
+ },
232
+ run_id: expect.any(String),
233
+ name: "testnode",
234
+ tags: ["graph:step:1"],
235
+ metadata: {},
236
+ },
237
+ {
238
+ event: "on_chain_stream",
239
+ run_id: expect.any(String),
240
+ name: "LangGraph",
241
+ tags: [],
242
+ metadata: {},
243
+ data: {
244
+ chunk: {
245
+ testnode: {
246
+ messages: [new AIMessage("hey!")],
247
+ },
248
+ },
249
+ },
250
+ },
251
+ {
252
+ event: "on_chain_end",
253
+ data: {
254
+ output: {
255
+ testnode: {
256
+ messages: [new AIMessage("hey!")],
257
+ },
258
+ },
259
+ },
260
+ run_id: expect.any(String),
261
+ name: "LangGraph",
262
+ tags: [],
263
+ metadata: {},
264
+ },
265
+ ]);
266
+ });
@@ -3,6 +3,7 @@ import { BaseChatModel, BaseChatModelParams } from "@langchain/core/language_mod
3
3
  import { BaseMessage } from "@langchain/core/messages";
4
4
  import { ChatResult } from "@langchain/core/outputs";
5
5
  import { RunnableConfig } from "@langchain/core/runnables";
6
+ import { Tool } from "@langchain/core/tools";
6
7
  import { MemorySaver } from "../checkpoint/memory.js";
7
8
  import { Checkpoint, CheckpointMetadata } from "../checkpoint/base.js";
8
9
  export interface FakeChatModelArgs extends BaseChatModelParams {
@@ -15,6 +16,20 @@ export declare class FakeChatModel extends BaseChatModel {
15
16
  _llmType(): string;
16
17
  _generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
17
18
  }
19
+ export declare class FakeToolCallingChatModel extends BaseChatModel {
20
+ sleep?: number;
21
+ responses?: BaseMessage[];
22
+ thrownErrorString?: string;
23
+ idx: number;
24
+ constructor(fields: {
25
+ sleep?: number;
26
+ responses?: BaseMessage[];
27
+ thrownErrorString?: string;
28
+ } & BaseChatModelParams);
29
+ _llmType(): string;
30
+ _generate(messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
31
+ bindTools(_: Tool[]): FakeToolCallingChatModel;
32
+ }
18
33
  export declare class MemorySaverAssertImmutable extends MemorySaver {
19
34
  storageForCopies: Record<string, Record<string, string>>;
20
35
  constructor();
@@ -44,6 +44,65 @@ export class FakeChatModel extends BaseChatModel {
44
44
  };
45
45
  }
46
46
  }
47
+ export class FakeToolCallingChatModel extends BaseChatModel {
48
+ constructor(fields) {
49
+ super(fields);
50
+ Object.defineProperty(this, "sleep", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: 50
55
+ });
56
+ Object.defineProperty(this, "responses", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "thrownErrorString", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ Object.defineProperty(this, "idx", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+ this.sleep = fields.sleep ?? this.sleep;
75
+ this.responses = fields.responses;
76
+ this.thrownErrorString = fields.thrownErrorString;
77
+ this.idx = 0;
78
+ }
79
+ _llmType() {
80
+ return "fake";
81
+ }
82
+ async _generate(messages, _options, _runManager) {
83
+ if (this.thrownErrorString) {
84
+ throw new Error(this.thrownErrorString);
85
+ }
86
+ const msg = this.responses?.[this.idx] ?? messages[this.idx];
87
+ const generation = {
88
+ generations: [
89
+ {
90
+ text: "",
91
+ message: msg,
92
+ },
93
+ ],
94
+ };
95
+ this.idx += 1;
96
+ return generation;
97
+ }
98
+ bindTools(_) {
99
+ return new FakeToolCallingChatModel({
100
+ sleep: this.sleep,
101
+ responses: this.responses,
102
+ thrownErrorString: this.thrownErrorString,
103
+ });
104
+ }
105
+ }
47
106
  export class MemorySaverAssertImmutable extends MemorySaver {
48
107
  constructor() {
49
108
  super();
package/dist/utils.cjs CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RunnableCallable = void 0;
4
4
  const runnables_1 = require("@langchain/core/runnables");
5
+ const singletons_1 = require("@langchain/core/singletons");
5
6
  class RunnableCallable extends runnables_1.Runnable {
6
7
  constructor(fields) {
7
8
  super();
@@ -48,24 +49,36 @@ class RunnableCallable extends runnables_1.Runnable {
48
49
  this.trace = fields.trace ?? this.trace;
49
50
  this.recurse = fields.recurse ?? this.recurse;
50
51
  }
52
+ async _tracedInvoke(input, config, runManager) {
53
+ return new Promise((resolve, reject) => {
54
+ const childConfig = (0, runnables_1.patchConfig)(config, {
55
+ callbacks: runManager?.getChild(),
56
+ });
57
+ void singletons_1.AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
58
+ try {
59
+ const output = await this.func(input, childConfig);
60
+ resolve(output);
61
+ }
62
+ catch (e) {
63
+ reject(e);
64
+ }
65
+ });
66
+ });
67
+ }
51
68
  async invoke(
52
69
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
70
  input, options
54
71
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
72
  ) {
56
- if (this.func === undefined) {
57
- return this.invoke(input, options);
58
- }
59
73
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
74
  let returnValue;
61
75
  if (this.trace) {
62
- returnValue = await this._callWithConfig(this.func, input, (0, runnables_1.mergeConfigs)(this.config, options));
76
+ returnValue = await this._callWithConfig(this._tracedInvoke, input, (0, runnables_1.mergeConfigs)(this.config, options));
63
77
  }
64
78
  else {
65
79
  returnValue = await this.func(input, (0, runnables_1.mergeConfigs)(this.config, options));
66
80
  }
67
- // eslint-disable-next-line no-instanceof/no-instanceof
68
- if (returnValue instanceof runnables_1.Runnable && this.recurse) {
81
+ if (runnables_1.Runnable.isRunnable(returnValue) && this.recurse) {
69
82
  return await returnValue.invoke(input, options);
70
83
  }
71
84
  return returnValue;
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
1
2
  import { Runnable, RunnableConfig } from "@langchain/core/runnables";
2
3
  export interface RunnableCallableArgs extends Partial<any> {
3
4
  name?: string;
@@ -14,5 +15,6 @@ export declare class RunnableCallable<I = unknown, O = unknown> extends Runnable
14
15
  trace: boolean;
15
16
  recurse: boolean;
16
17
  constructor(fields: RunnableCallableArgs);
18
+ protected _tracedInvoke(input: I, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<O>;
17
19
  invoke(input: any, options?: Partial<RunnableConfig> | undefined): Promise<any>;
18
20
  }
package/dist/utils.js CHANGED
@@ -1,4 +1,5 @@
1
- import { mergeConfigs, Runnable, } from "@langchain/core/runnables";
1
+ import { mergeConfigs, patchConfig, Runnable, } from "@langchain/core/runnables";
2
+ import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons";
2
3
  export class RunnableCallable extends Runnable {
3
4
  constructor(fields) {
4
5
  super();
@@ -45,24 +46,36 @@ export class RunnableCallable extends Runnable {
45
46
  this.trace = fields.trace ?? this.trace;
46
47
  this.recurse = fields.recurse ?? this.recurse;
47
48
  }
49
+ async _tracedInvoke(input, config, runManager) {
50
+ return new Promise((resolve, reject) => {
51
+ const childConfig = patchConfig(config, {
52
+ callbacks: runManager?.getChild(),
53
+ });
54
+ void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
55
+ try {
56
+ const output = await this.func(input, childConfig);
57
+ resolve(output);
58
+ }
59
+ catch (e) {
60
+ reject(e);
61
+ }
62
+ });
63
+ });
64
+ }
48
65
  async invoke(
49
66
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
67
  input, options
51
68
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
69
  ) {
53
- if (this.func === undefined) {
54
- return this.invoke(input, options);
55
- }
56
70
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
71
  let returnValue;
58
72
  if (this.trace) {
59
- returnValue = await this._callWithConfig(this.func, input, mergeConfigs(this.config, options));
73
+ returnValue = await this._callWithConfig(this._tracedInvoke, input, mergeConfigs(this.config, options));
60
74
  }
61
75
  else {
62
76
  returnValue = await this.func(input, mergeConfigs(this.config, options));
63
77
  }
64
- // eslint-disable-next-line no-instanceof/no-instanceof
65
- if (returnValue instanceof Runnable && this.recurse) {
78
+ if (Runnable.isRunnable(returnValue) && this.recurse) {
66
79
  return await returnValue.invoke(input, options);
67
80
  }
68
81
  return returnValue;
package/dist/web.cjs ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmptyChannelError = exports.InvalidUpdateError = exports.GraphValueError = exports.GraphRecursionError = exports.BaseCheckpointSaver = exports.emptyCheckpoint = exports.copyCheckpoint = exports.MemorySaver = exports.MessageGraph = exports.StateGraph = exports.START = exports.Graph = exports.END = void 0;
4
+ var index_js_1 = require("./graph/index.cjs");
5
+ Object.defineProperty(exports, "END", { enumerable: true, get: function () { return index_js_1.END; } });
6
+ Object.defineProperty(exports, "Graph", { enumerable: true, get: function () { return index_js_1.Graph; } });
7
+ Object.defineProperty(exports, "START", { enumerable: true, get: function () { return index_js_1.START; } });
8
+ Object.defineProperty(exports, "StateGraph", { enumerable: true, get: function () { return index_js_1.StateGraph; } });
9
+ Object.defineProperty(exports, "MessageGraph", { enumerable: true, get: function () { return index_js_1.MessageGraph; } });
10
+ var memory_js_1 = require("./checkpoint/memory.cjs");
11
+ Object.defineProperty(exports, "MemorySaver", { enumerable: true, get: function () { return memory_js_1.MemorySaver; } });
12
+ var base_js_1 = require("./checkpoint/base.cjs");
13
+ Object.defineProperty(exports, "copyCheckpoint", { enumerable: true, get: function () { return base_js_1.copyCheckpoint; } });
14
+ Object.defineProperty(exports, "emptyCheckpoint", { enumerable: true, get: function () { return base_js_1.emptyCheckpoint; } });
15
+ Object.defineProperty(exports, "BaseCheckpointSaver", { enumerable: true, get: function () { return base_js_1.BaseCheckpointSaver; } });
16
+ var errors_js_1 = require("./errors.cjs");
17
+ Object.defineProperty(exports, "GraphRecursionError", { enumerable: true, get: function () { return errors_js_1.GraphRecursionError; } });
18
+ Object.defineProperty(exports, "GraphValueError", { enumerable: true, get: function () { return errors_js_1.GraphValueError; } });
19
+ Object.defineProperty(exports, "InvalidUpdateError", { enumerable: true, get: function () { return errors_js_1.InvalidUpdateError; } });
20
+ Object.defineProperty(exports, "EmptyChannelError", { enumerable: true, get: function () { return errors_js_1.EmptyChannelError; } });
package/dist/web.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { END, Graph, type StateGraphArgs, START, StateGraph, type CompiledStateGraph, MessageGraph, } from "./graph/index.js";
2
+ export { MemorySaver } from "./checkpoint/memory.js";
3
+ export { type Checkpoint, type CheckpointMetadata, copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./checkpoint/base.js";
4
+ export { GraphRecursionError, GraphValueError, InvalidUpdateError, EmptyChannelError, } from "./errors.js";
package/dist/web.js ADDED
@@ -0,0 +1,4 @@
1
+ export { END, Graph, START, StateGraph, MessageGraph, } from "./graph/index.js";
2
+ export { MemorySaver } from "./checkpoint/memory.js";
3
+ export { copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./checkpoint/base.js";
4
+ export { GraphRecursionError, GraphValueError, InvalidUpdateError, EmptyChannelError, } from "./errors.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "LangGraph",
5
5
  "type": "module",
6
6
  "engines": {
@@ -70,7 +70,7 @@
70
70
  "rollup": "^4.5.2",
71
71
  "ts-jest": "^29.1.0",
72
72
  "tsx": "^4.7.0",
73
- "typescript": "^5.4.5",
73
+ "typescript": "^4.9.5 || ^5.4.5",
74
74
  "zod": "^3.22.4",
75
75
  "zod-to-json-schema": "^3.22.4"
76
76
  },
@@ -96,6 +96,15 @@
96
96
  "import": "./index.js",
97
97
  "require": "./index.cjs"
98
98
  },
99
+ "./web": {
100
+ "types": {
101
+ "import": "./web.d.ts",
102
+ "require": "./web.d.cts",
103
+ "default": "./web.d.ts"
104
+ },
105
+ "import": "./web.js",
106
+ "require": "./web.cjs"
107
+ },
99
108
  "./pregel": {
100
109
  "types": {
101
110
  "import": "./pregel.d.ts",
@@ -131,6 +140,10 @@
131
140
  "index.js",
132
141
  "index.d.ts",
133
142
  "index.d.cts",
143
+ "web.cjs",
144
+ "web.js",
145
+ "web.d.ts",
146
+ "web.d.cts",
134
147
  "pregel.cjs",
135
148
  "pregel.js",
136
149
  "pregel.d.ts",
package/web.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/web.cjs');
package/web.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/web.js'
package/web.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/web.js'
package/web.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/web.js'