Skip to content

Commit

Permalink
langgraph[minor]: Expose MessagesState annotation (#333)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
bracesproul and jacoblee93 authored Aug 22, 2024
1 parent d9d219f commit a196347
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion langgraph/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion langgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions langgraph/src/graph/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,3 @@ export class MessageGraph extends StateGraph<
});
}
}

export interface MessagesState {
messages: BaseMessage[];
}
12 changes: 12 additions & 0 deletions langgraph/src/graph/messages_state.ts
Original file line number Diff line number Diff line change
@@ -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<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => [],
}),
});
1 change: 1 addition & 0 deletions langgraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import { initializeAsyncLocalStorageSingleton } from "./setup/async_local_storag
initializeAsyncLocalStorageSingleton();

export * from "./web.js";
export { MessagesState } from "./graph/messages_state.js";
4 changes: 2 additions & 2 deletions langgraph/src/prebuilt/react_agent_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -40,7 +40,7 @@ export type N = typeof START | "agent" | "tools";
export type CreateReactAgentParams = {
llm: BaseChatModel;
tools:
| ToolNode<MessagesState>
| ToolNode<typeof MessagesState.State>
| (StructuredToolInterface | RunnableToolLike)[];
messageModifier?:
| SystemMessage
Expand Down
10 changes: 5 additions & 5 deletions langgraph/src/prebuilt/tool_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +17,7 @@ export type ToolNodeOptions = {
};

export class ToolNode<
T extends BaseMessage[] | MessagesState
T extends BaseMessage[] | typeof MessagesState.State
> extends RunnableCallable<T, T> {
/**
A node that runs the tools requested in the last AIMessage. It can be used
Expand All @@ -41,9 +41,9 @@ export class ToolNode<
}

private async run(
input: BaseMessage[] | MessagesState,
input: BaseMessage[] | typeof MessagesState.State,
config: RunnableConfig
): Promise<BaseMessage[] | MessagesState> {
): Promise<BaseMessage[] | typeof MessagesState.State> {
const message = Array.isArray(input)
? input[input.length - 1]
: input.messages[input.messages.length - 1];
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit a196347

Please sign in to comment.