Skip to content

Commit

Permalink
langgraph[minor]: Expose MessagesState annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 20, 2024
1 parent bcefdd0 commit 292c09b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
6 changes: 5 additions & 1 deletion langgraph/src/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export {
StateGraph,
type CompiledStateGraph,
} from "./state.js";
export { MessageGraph, messagesStateReducer } from "./message.js";
export {
MessageGraph,
messagesStateReducer,
MessagesState,
} from "./message.js";
10 changes: 7 additions & 3 deletions langgraph/src/graph/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@langchain/core/messages";
import { v4 } from "uuid";
import { StateGraph } from "./state.js";
import { Annotation } from "./annotation.js";

type Messages =
| Array<BaseMessage | BaseMessageLike>
Expand Down Expand Up @@ -76,6 +77,9 @@ export class MessageGraph extends StateGraph<
}
}

export interface MessagesState {
messages: BaseMessage[];
}
export const MessagesState = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => [],
}),
});
2 changes: 1 addition & 1 deletion langgraph/src/prebuilt/react_agent_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions langgraph/src/prebuilt/tool_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions langgraph/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
MessageGraph,
messagesStateReducer,
Annotation,
MessagesState,
type StateType,
type UpdateType,
type CompiledGraph,
Expand Down

0 comments on commit 292c09b

Please sign in to comment.