From a19634783831a5028bacc00974779de534d0f647 Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Thu, 22 Aug 2024 10:59:47 -0700 Subject: [PATCH 1/2] langgraph[minor]: Expose MessagesState annotation (#333) * langgraph[minor]: Expose MessagesState annotation * fix issues * format * side effects * add new entrypoint file * message to messages * message_state -> messages_state * bump * add to entrypoint * format n lint * Fix --------- Co-authored-by: jacoblee93 --- langgraph/langchain.config.js | 2 +- langgraph/package.json | 2 +- langgraph/src/graph/message.ts | 4 ---- langgraph/src/graph/messages_state.ts | 12 ++++++++++++ langgraph/src/index.ts | 1 + langgraph/src/prebuilt/react_agent_executor.ts | 4 ++-- langgraph/src/prebuilt/tool_node.ts | 10 +++++----- yarn.lock | 10 +++++----- 8 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 langgraph/src/graph/messages_state.ts diff --git a/langgraph/langchain.config.js b/langgraph/langchain.config.js index 747852a1..a6510cb2 100644 --- a/langgraph/langchain.config.js +++ b/langgraph/langchain.config.js @@ -16,7 +16,7 @@ export const config = { web: "web", pregel: "pregel/index", prebuilt: "prebuilt/index", - "checkpoint/sqlite": "checkpoint/sqlite", + "checkpoint/sqlite": "checkpoint/sqlite" }, tsConfigPath: resolve("./tsconfig.json"), cjsSource: "./dist-cjs", diff --git a/langgraph/package.json b/langgraph/package.json index fe3a67d3..ac7273de 100644 --- a/langgraph/package.json +++ b/langgraph/package.json @@ -39,7 +39,7 @@ "@langchain/anthropic": "^0.2.12", "@langchain/community": "^0.2.25", "@langchain/openai": "^0.2.4", - "@langchain/scripts": "^0.0.22", + "@langchain/scripts": "^0.0.23", "@swc/core": "^1.3.90", "@swc/jest": "^0.2.29", "@tsconfig/recommended": "^1.0.3", diff --git a/langgraph/src/graph/message.ts b/langgraph/src/graph/message.ts index 1c0393c6..48fc423a 100644 --- a/langgraph/src/graph/message.ts +++ b/langgraph/src/graph/message.ts @@ -75,7 +75,3 @@ export class MessageGraph extends StateGraph< }); } } - -export interface MessagesState { - messages: BaseMessage[]; -} diff --git a/langgraph/src/graph/messages_state.ts b/langgraph/src/graph/messages_state.ts new file mode 100644 index 00000000..5c91b7c8 --- /dev/null +++ b/langgraph/src/graph/messages_state.ts @@ -0,0 +1,12 @@ +/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */ + +import { BaseMessage } from "@langchain/core/messages"; +import { Annotation } from "./annotation.js"; +import { messagesStateReducer } from "./message.js"; + +export const MessagesState = Annotation.Root({ + messages: Annotation({ + reducer: messagesStateReducer, + default: () => [], + }), +}); diff --git a/langgraph/src/index.ts b/langgraph/src/index.ts index 8a6e33af..f57f4e4f 100644 --- a/langgraph/src/index.ts +++ b/langgraph/src/index.ts @@ -6,3 +6,4 @@ import { initializeAsyncLocalStorageSingleton } from "./setup/async_local_storag initializeAsyncLocalStorageSingleton(); export * from "./web.js"; +export { MessagesState } from "./graph/messages_state.js"; diff --git a/langgraph/src/prebuilt/react_agent_executor.ts b/langgraph/src/prebuilt/react_agent_executor.ts index 4860bac0..27a8e952 100644 --- a/langgraph/src/prebuilt/react_agent_executor.ts +++ b/langgraph/src/prebuilt/react_agent_executor.ts @@ -21,7 +21,7 @@ import { import { ChatPromptTemplate } from "@langchain/core/prompts"; import { BaseCheckpointSaver } from "../checkpoint/base.js"; import { END, START, StateGraph } from "../graph/index.js"; -import { MessagesState } from "../graph/message.js"; +import { MessagesState } from "../graph/messages_state.js"; import { CompiledStateGraph, StateGraphArgs } from "../graph/state.js"; import { All } from "../pregel/types.js"; import { ToolNode } from "./tool_node.js"; @@ -40,7 +40,7 @@ export type N = typeof START | "agent" | "tools"; export type CreateReactAgentParams = { llm: BaseChatModel; tools: - | ToolNode + | ToolNode | (StructuredToolInterface | RunnableToolLike)[]; messageModifier?: | SystemMessage diff --git a/langgraph/src/prebuilt/tool_node.ts b/langgraph/src/prebuilt/tool_node.ts index fdd71090..5ff3fdbb 100644 --- a/langgraph/src/prebuilt/tool_node.ts +++ b/langgraph/src/prebuilt/tool_node.ts @@ -8,7 +8,7 @@ import { RunnableConfig, RunnableToolLike } from "@langchain/core/runnables"; import { StructuredToolInterface } from "@langchain/core/tools"; import { RunnableCallable } from "../utils.js"; import { END } from "../graph/graph.js"; -import { MessagesState } from "../graph/message.js"; +import { MessagesState } from "../graph/messages_state.js"; export type ToolNodeOptions = { name?: string; @@ -17,7 +17,7 @@ export type ToolNodeOptions = { }; export class ToolNode< - T extends BaseMessage[] | MessagesState + T extends BaseMessage[] | typeof MessagesState.State > extends RunnableCallable { /** A node that runs the tools requested in the last AIMessage. It can be used @@ -41,9 +41,9 @@ export class ToolNode< } private async run( - input: BaseMessage[] | MessagesState, + input: BaseMessage[] | typeof MessagesState.State, config: RunnableConfig - ): Promise { + ): Promise { const message = Array.isArray(input) ? input[input.length - 1] : input.messages[input.messages.length - 1]; @@ -92,7 +92,7 @@ export class ToolNode< } export function toolsCondition( - state: BaseMessage[] | MessagesState + state: BaseMessage[] | typeof MessagesState.State ): "tools" | typeof END { const message = Array.isArray(state) ? state[state.length - 1] diff --git a/yarn.lock b/yarn.lock index 980f5b13..670653ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1881,7 +1881,7 @@ __metadata: "@langchain/community": ^0.2.25 "@langchain/core": ">=0.2.20 <0.3.0" "@langchain/openai": ^0.2.4 - "@langchain/scripts": ^0.0.22 + "@langchain/scripts": ^0.0.23 "@swc/core": ^1.3.90 "@swc/jest": ^0.2.29 "@tsconfig/recommended": ^1.0.3 @@ -1960,9 +1960,9 @@ __metadata: languageName: node linkType: hard -"@langchain/scripts@npm:^0.0.22": - version: 0.0.22 - resolution: "@langchain/scripts@npm:0.0.22" +"@langchain/scripts@npm:^0.0.23": + version: 0.0.23 + resolution: "@langchain/scripts@npm:0.0.23" dependencies: "@rollup/wasm-node": ^4.19.0 axios: ^1.6.7 @@ -1978,7 +1978,7 @@ __metadata: lc-build: bin/build.js lc_build_v2: bin/build_v2.js notebook_validate: bin/validate_notebook.js - checksum: d44cd14e83b895959cb8692a8308eb1f740556de359de3f16a7722445c5adaafc0606b9f078743030b17bb633efccb344c8f156d8e57e3221fcc73377427a021 + checksum: 975ac6fea3832353bc15b48ae5e66087dcda9e3f8be02c3f2f5c93b2545cacb803b4d160a04b33ab14da75808909de6077d775fb51d2960d9a769fc968342e54 languageName: node linkType: hard From 8c8d0a9f21589126f4aea260fa4500083d84d26d Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Thu, 22 Aug 2024 11:09:36 -0700 Subject: [PATCH 2/2] Rename (#343) --- .../{messages_state.ts => messages_annotation.ts} | 2 +- langgraph/src/index.ts | 2 +- langgraph/src/prebuilt/react_agent_executor.ts | 4 ++-- langgraph/src/prebuilt/tool_node.ts | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) rename langgraph/src/graph/{messages_state.ts => messages_annotation.ts} (85%) diff --git a/langgraph/src/graph/messages_state.ts b/langgraph/src/graph/messages_annotation.ts similarity index 85% rename from langgraph/src/graph/messages_state.ts rename to langgraph/src/graph/messages_annotation.ts index 5c91b7c8..9f299365 100644 --- a/langgraph/src/graph/messages_state.ts +++ b/langgraph/src/graph/messages_annotation.ts @@ -4,7 +4,7 @@ import { BaseMessage } from "@langchain/core/messages"; import { Annotation } from "./annotation.js"; import { messagesStateReducer } from "./message.js"; -export const MessagesState = Annotation.Root({ +export const MessagesAnnotation = Annotation.Root({ messages: Annotation({ reducer: messagesStateReducer, default: () => [], diff --git a/langgraph/src/index.ts b/langgraph/src/index.ts index f57f4e4f..5a2c0920 100644 --- a/langgraph/src/index.ts +++ b/langgraph/src/index.ts @@ -6,4 +6,4 @@ import { initializeAsyncLocalStorageSingleton } from "./setup/async_local_storag initializeAsyncLocalStorageSingleton(); export * from "./web.js"; -export { MessagesState } from "./graph/messages_state.js"; +export { MessagesAnnotation } from "./graph/messages_annotation.js"; diff --git a/langgraph/src/prebuilt/react_agent_executor.ts b/langgraph/src/prebuilt/react_agent_executor.ts index 27a8e952..c28e3ce3 100644 --- a/langgraph/src/prebuilt/react_agent_executor.ts +++ b/langgraph/src/prebuilt/react_agent_executor.ts @@ -21,7 +21,7 @@ import { import { ChatPromptTemplate } from "@langchain/core/prompts"; import { BaseCheckpointSaver } from "../checkpoint/base.js"; import { END, START, StateGraph } from "../graph/index.js"; -import { MessagesState } from "../graph/messages_state.js"; +import { MessagesAnnotation } from "../graph/messages_annotation.js"; import { CompiledStateGraph, StateGraphArgs } from "../graph/state.js"; import { All } from "../pregel/types.js"; import { ToolNode } from "./tool_node.js"; @@ -40,7 +40,7 @@ export type N = typeof START | "agent" | "tools"; export type CreateReactAgentParams = { llm: BaseChatModel; tools: - | ToolNode + | ToolNode | (StructuredToolInterface | RunnableToolLike)[]; messageModifier?: | SystemMessage diff --git a/langgraph/src/prebuilt/tool_node.ts b/langgraph/src/prebuilt/tool_node.ts index 5ff3fdbb..9cb6bf7e 100644 --- a/langgraph/src/prebuilt/tool_node.ts +++ b/langgraph/src/prebuilt/tool_node.ts @@ -8,7 +8,7 @@ import { RunnableConfig, RunnableToolLike } from "@langchain/core/runnables"; import { StructuredToolInterface } from "@langchain/core/tools"; import { RunnableCallable } from "../utils.js"; import { END } from "../graph/graph.js"; -import { MessagesState } from "../graph/messages_state.js"; +import { MessagesAnnotation } from "../graph/messages_annotation.js"; export type ToolNodeOptions = { name?: string; @@ -17,7 +17,7 @@ export type ToolNodeOptions = { }; export class ToolNode< - T extends BaseMessage[] | typeof MessagesState.State + T extends BaseMessage[] | typeof MessagesAnnotation.State > extends RunnableCallable { /** A node that runs the tools requested in the last AIMessage. It can be used @@ -41,9 +41,9 @@ export class ToolNode< } private async run( - input: BaseMessage[] | typeof MessagesState.State, + input: BaseMessage[] | typeof MessagesAnnotation.State, config: RunnableConfig - ): Promise { + ): Promise { const message = Array.isArray(input) ? input[input.length - 1] : input.messages[input.messages.length - 1]; @@ -92,7 +92,7 @@ export class ToolNode< } export function toolsCondition( - state: BaseMessage[] | typeof MessagesState.State + state: BaseMessage[] | typeof MessagesAnnotation.State ): "tools" | typeof END { const message = Array.isArray(state) ? state[state.length - 1]