@langchain/langgraph 1.0.14 → 1.1.0
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.
- package/dist/channels/base.cjs +11 -0
- package/dist/channels/base.cjs.map +1 -1
- package/dist/channels/base.d.cts +9 -0
- package/dist/channels/base.d.cts.map +1 -1
- package/dist/channels/base.d.ts +9 -0
- package/dist/channels/base.d.ts.map +1 -1
- package/dist/channels/base.js +11 -0
- package/dist/channels/base.js.map +1 -1
- package/dist/channels/binop.cjs +13 -0
- package/dist/channels/binop.cjs.map +1 -1
- package/dist/channels/binop.d.cts +6 -0
- package/dist/channels/binop.d.cts.map +1 -1
- package/dist/channels/binop.d.ts +6 -0
- package/dist/channels/binop.d.ts.map +1 -1
- package/dist/channels/binop.js +13 -0
- package/dist/channels/binop.js.map +1 -1
- package/dist/channels/index.cjs +2 -0
- package/dist/channels/index.d.cts +2 -1
- package/dist/channels/index.d.ts +2 -1
- package/dist/channels/index.js +2 -1
- package/dist/channels/untracked_value.cjs +83 -0
- package/dist/channels/untracked_value.cjs.map +1 -0
- package/dist/channels/untracked_value.d.cts +64 -0
- package/dist/channels/untracked_value.d.cts.map +1 -0
- package/dist/channels/untracked_value.d.ts +64 -0
- package/dist/channels/untracked_value.d.ts.map +1 -0
- package/dist/channels/untracked_value.js +83 -0
- package/dist/channels/untracked_value.js.map +1 -0
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +2 -2
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.ts +2 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js.map +1 -1
- package/dist/errors.cjs +36 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +30 -1
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.ts +30 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +36 -1
- package/dist/errors.js.map +1 -1
- package/dist/func/index.cjs +1 -1
- package/dist/func/index.cjs.map +1 -1
- package/dist/func/index.js +1 -1
- package/dist/func/index.js.map +1 -1
- package/dist/graph/index.cjs +1 -0
- package/dist/graph/index.js +2 -1
- package/dist/graph/message.cjs +2 -47
- package/dist/graph/message.cjs.map +1 -1
- package/dist/graph/message.d.cts +4 -11
- package/dist/graph/message.d.cts.map +1 -1
- package/dist/graph/message.d.ts +4 -11
- package/dist/graph/message.d.ts.map +1 -1
- package/dist/graph/message.js +2 -45
- package/dist/graph/message.js.map +1 -1
- package/dist/graph/messages_annotation.cjs +3 -3
- package/dist/graph/messages_annotation.cjs.map +1 -1
- package/dist/graph/messages_annotation.d.cts +2 -2
- package/dist/graph/messages_annotation.d.cts.map +1 -1
- package/dist/graph/messages_annotation.d.ts +1 -1
- package/dist/graph/messages_annotation.d.ts.map +1 -1
- package/dist/graph/messages_annotation.js +1 -1
- package/dist/graph/messages_annotation.js.map +1 -1
- package/dist/graph/messages_reducer.cjs +85 -0
- package/dist/graph/messages_reducer.cjs.map +1 -0
- package/dist/graph/messages_reducer.d.cts +54 -0
- package/dist/graph/messages_reducer.d.cts.map +1 -0
- package/dist/graph/messages_reducer.d.ts +54 -0
- package/dist/graph/messages_reducer.d.ts.map +1 -0
- package/dist/graph/messages_reducer.js +84 -0
- package/dist/graph/messages_reducer.js.map +1 -0
- package/dist/graph/state.cjs +30 -10
- package/dist/graph/state.cjs.map +1 -1
- package/dist/graph/state.d.cts +24 -15
- package/dist/graph/state.d.cts.map +1 -1
- package/dist/graph/state.d.ts +24 -15
- package/dist/graph/state.d.ts.map +1 -1
- package/dist/graph/state.js +31 -11
- package/dist/graph/state.js.map +1 -1
- package/dist/graph/types.d.cts +127 -0
- package/dist/graph/types.d.cts.map +1 -0
- package/dist/graph/types.d.ts +127 -0
- package/dist/graph/types.d.ts.map +1 -0
- package/dist/graph/zod/plugin.cjs +2 -2
- package/dist/graph/zod/plugin.cjs.map +1 -1
- package/dist/graph/zod/plugin.js +3 -3
- package/dist/graph/zod/plugin.js.map +1 -1
- package/dist/graph/zod/zod-registry.d.cts.map +1 -1
- package/dist/hash.cjs +4 -4
- package/dist/hash.cjs.map +1 -1
- package/dist/hash.js +4 -4
- package/dist/hash.js.map +1 -1
- package/dist/index.cjs +21 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/dist/prebuilt/agent_executor.d.cts +5 -5
- package/dist/prebuilt/agent_executor.d.cts.map +1 -1
- package/dist/prebuilt/agent_executor.d.ts +5 -5
- package/dist/prebuilt/agent_executor.d.ts.map +1 -1
- package/dist/prebuilt/react_agent_executor.cjs +7 -6
- package/dist/prebuilt/react_agent_executor.cjs.map +1 -1
- package/dist/prebuilt/react_agent_executor.d.cts +4 -4
- package/dist/prebuilt/react_agent_executor.d.cts.map +1 -1
- package/dist/prebuilt/react_agent_executor.d.ts +4 -4
- package/dist/prebuilt/react_agent_executor.d.ts.map +1 -1
- package/dist/prebuilt/react_agent_executor.js +5 -4
- package/dist/prebuilt/react_agent_executor.js.map +1 -1
- package/dist/pregel/algo.cjs +19 -0
- package/dist/pregel/algo.cjs.map +1 -1
- package/dist/pregel/algo.d.cts.map +1 -1
- package/dist/pregel/algo.d.ts.map +1 -1
- package/dist/pregel/algo.js +19 -1
- package/dist/pregel/algo.js.map +1 -1
- package/dist/pregel/loop.cjs +18 -3
- package/dist/pregel/loop.cjs.map +1 -1
- package/dist/pregel/loop.js +20 -5
- package/dist/pregel/loop.js.map +1 -1
- package/dist/pregel/runner.cjs +1 -1
- package/dist/pregel/runner.cjs.map +1 -1
- package/dist/pregel/runner.js +1 -1
- package/dist/pregel/runner.js.map +1 -1
- package/dist/state/adapter.cjs +53 -0
- package/dist/state/adapter.cjs.map +1 -0
- package/dist/state/adapter.d.cts +33 -0
- package/dist/state/adapter.d.cts.map +1 -0
- package/dist/state/adapter.d.ts +33 -0
- package/dist/state/adapter.d.ts.map +1 -0
- package/dist/state/adapter.js +52 -0
- package/dist/state/adapter.js.map +1 -0
- package/dist/state/index.cjs +8 -0
- package/dist/state/index.js +10 -0
- package/dist/state/prebuilt/index.cjs +1 -0
- package/dist/state/prebuilt/index.js +3 -0
- package/dist/state/prebuilt/messages.cjs +19 -0
- package/dist/state/prebuilt/messages.cjs.map +1 -0
- package/dist/state/prebuilt/messages.d.cts +10 -0
- package/dist/state/prebuilt/messages.d.cts.map +1 -0
- package/dist/state/prebuilt/messages.d.ts +10 -0
- package/dist/state/prebuilt/messages.d.ts.map +1 -0
- package/dist/state/prebuilt/messages.js +19 -0
- package/dist/state/prebuilt/messages.js.map +1 -0
- package/dist/state/schema.cjs +171 -0
- package/dist/state/schema.cjs.map +1 -0
- package/dist/state/schema.d.cts +208 -0
- package/dist/state/schema.d.cts.map +1 -0
- package/dist/state/schema.d.ts +208 -0
- package/dist/state/schema.d.ts.map +1 -0
- package/dist/state/schema.js +171 -0
- package/dist/state/schema.js.map +1 -0
- package/dist/state/types.cjs +17 -0
- package/dist/state/types.cjs.map +1 -0
- package/dist/state/types.d.cts +128 -0
- package/dist/state/types.d.cts.map +1 -0
- package/dist/state/types.d.ts +128 -0
- package/dist/state/types.d.ts.map +1 -0
- package/dist/state/types.js +14 -0
- package/dist/state/types.js.map +1 -0
- package/dist/state/values/index.cjs +2 -0
- package/dist/state/values/index.js +4 -0
- package/dist/state/values/reduced.cjs +72 -0
- package/dist/state/values/reduced.cjs.map +1 -0
- package/dist/state/values/reduced.d.cts +155 -0
- package/dist/state/values/reduced.d.cts.map +1 -0
- package/dist/state/values/reduced.d.ts +155 -0
- package/dist/state/values/reduced.d.ts.map +1 -0
- package/dist/state/values/reduced.js +71 -0
- package/dist/state/values/reduced.js.map +1 -0
- package/dist/state/values/untracked.cjs +64 -0
- package/dist/state/values/untracked.cjs.map +1 -0
- package/dist/state/values/untracked.d.cts +74 -0
- package/dist/state/values/untracked.d.cts.map +1 -0
- package/dist/state/values/untracked.d.ts +74 -0
- package/dist/state/values/untracked.d.ts.map +1 -0
- package/dist/state/values/untracked.js +63 -0
- package/dist/state/values/untracked.js.map +1 -0
- package/dist/web.cjs +26 -7
- package/dist/web.d.cts +12 -3
- package/dist/web.d.ts +12 -3
- package/dist/web.js +13 -4
- package/package.json +5 -4
package/dist/graph/message.d.ts
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { StateGraph } from "./state.js";
|
|
2
|
+
import { Messages } from "./messages_reducer.js";
|
|
2
3
|
import { RunnableConfig } from "@langchain/core/runnables";
|
|
3
|
-
import * as
|
|
4
|
+
import * as _langchain_core_messages0 from "@langchain/core/messages";
|
|
4
5
|
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
5
6
|
|
|
6
7
|
//#region src/graph/message.d.ts
|
|
7
|
-
declare const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
8
|
-
type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
9
|
-
/**
|
|
10
|
-
* Prebuilt reducer that combines returned messages.
|
|
11
|
-
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
12
|
-
* instances.
|
|
13
|
-
*/
|
|
14
|
-
declare function messagesStateReducer(left: Messages, right: Messages): BaseMessage[];
|
|
15
8
|
/** @ignore */
|
|
16
9
|
declare class MessageGraph extends StateGraph<BaseMessage[], BaseMessage[], Messages> {
|
|
17
10
|
constructor();
|
|
@@ -34,7 +27,7 @@ declare function pushMessage(message: BaseMessage | BaseMessageLike, options?: R
|
|
|
34
27
|
* @default "messages"
|
|
35
28
|
*/
|
|
36
29
|
stateKey?: string | null;
|
|
37
|
-
}): BaseMessage<
|
|
30
|
+
}): BaseMessage<_langchain_core_messages0.MessageStructure<_langchain_core_messages0.MessageToolSet>, _langchain_core_messages0.MessageType>;
|
|
38
31
|
//#endregion
|
|
39
|
-
export { MessageGraph,
|
|
32
|
+
export { MessageGraph, pushMessage };
|
|
40
33
|
//# sourceMappingURL=message.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message.d.ts","names":["BaseMessage","BaseMessageLike","RunnableConfig","StateGraph","
|
|
1
|
+
{"version":3,"file":"message.d.ts","names":["BaseMessage","BaseMessageLike","RunnableConfig","StateGraph","Messages","MessageGraph","pushMessage","_langchain_core_messages0","MessageToolSet","MessageStructure","MessageType"],"sources":["../../src/graph/message.d.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\nimport type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { StateGraph } from \"./state.js\";\nimport { type Messages } from \"./messages_reducer.js\";\n/** @ignore */\nexport declare class MessageGraph extends StateGraph<BaseMessage[], BaseMessage[], Messages> {\n constructor();\n}\n/**\n * Manually push a message to a message stream.\n *\n * This is useful when you need to push a manually created message before the node\n * has finished executing.\n *\n * When a message is pushed, it will be automatically persisted to the state after the node has finished executing.\n * To disable persisting, set `options.stateKey` to `null`.\n *\n * @param message The message to push. The message must have an ID set, otherwise an error will be thrown.\n * @param options RunnableConfig / Runtime coming from node context.\n */\nexport declare function pushMessage(message: BaseMessage | BaseMessageLike, options?: RunnableConfig & {\n /**\n * The key of the state to push the message to. Set to `null` to avoid persisting.\n * @default \"messages\"\n */\n stateKey?: string | null;\n}): BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>;\n"],"mappings":";;;;;;;;cAKqBK,YAAAA,SAAqBF,WAAWH,eAAeA,eAAeI;;AAAnF;;;;;;;AAeA;;;;;;AAMqHG,iBAN7FD,WAAAA,CAM2BG,OAAAA,EANNT,WAMMS,GANQR,eAMRQ,EAAAA,QAAAA,EANmCP,cAMnCO,GAAAA;;;;;;IAA/CT,YAAiHO,yBAAAA,CAAlEE,iBANiDF,yBAAAA,CAMGC,cAAAA,GAApCD,yBAAAA,CAAwFG,WAAAA"}
|
package/dist/graph/message.js
CHANGED
|
@@ -1,52 +1,9 @@
|
|
|
1
1
|
import { ensureLangGraphConfig } from "../pregel/utils/config.js";
|
|
2
|
+
import { messagesStateReducer } from "./messages_reducer.js";
|
|
2
3
|
import { StateGraph } from "./state.js";
|
|
3
|
-
import { v4 } from "uuid";
|
|
4
4
|
import { coerceMessageLikeToMessage } from "@langchain/core/messages";
|
|
5
5
|
|
|
6
6
|
//#region src/graph/message.ts
|
|
7
|
-
const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
8
|
-
/**
|
|
9
|
-
* Prebuilt reducer that combines returned messages.
|
|
10
|
-
* Can handle standard messages and special modifiers like {@link RemoveMessage}
|
|
11
|
-
* instances.
|
|
12
|
-
*/
|
|
13
|
-
function messagesStateReducer(left, right) {
|
|
14
|
-
const leftArray = Array.isArray(left) ? left : [left];
|
|
15
|
-
const rightArray = Array.isArray(right) ? right : [right];
|
|
16
|
-
const leftMessages = leftArray.map(coerceMessageLikeToMessage);
|
|
17
|
-
const rightMessages = rightArray.map(coerceMessageLikeToMessage);
|
|
18
|
-
for (const m of leftMessages) if (m.id === null || m.id === void 0) {
|
|
19
|
-
m.id = v4();
|
|
20
|
-
m.lc_kwargs.id = m.id;
|
|
21
|
-
}
|
|
22
|
-
let removeAllIdx;
|
|
23
|
-
for (let i = 0; i < rightMessages.length; i += 1) {
|
|
24
|
-
const m = rightMessages[i];
|
|
25
|
-
if (m.id === null || m.id === void 0) {
|
|
26
|
-
m.id = v4();
|
|
27
|
-
m.lc_kwargs.id = m.id;
|
|
28
|
-
}
|
|
29
|
-
if (m.getType() === "remove" && m.id === REMOVE_ALL_MESSAGES) removeAllIdx = i;
|
|
30
|
-
}
|
|
31
|
-
if (removeAllIdx != null) return rightMessages.slice(removeAllIdx + 1);
|
|
32
|
-
const merged = [...leftMessages];
|
|
33
|
-
const mergedById = new Map(merged.map((m, i) => [m.id, i]));
|
|
34
|
-
const idsToRemove = /* @__PURE__ */ new Set();
|
|
35
|
-
for (const m of rightMessages) {
|
|
36
|
-
const existingIdx = mergedById.get(m.id);
|
|
37
|
-
if (existingIdx !== void 0) if (m.getType() === "remove") idsToRemove.add(m.id);
|
|
38
|
-
else {
|
|
39
|
-
idsToRemove.delete(m.id);
|
|
40
|
-
merged[existingIdx] = m;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
if (m.getType() === "remove") throw new Error(`Attempting to delete a message with an ID that doesn't exist ('${m.id}')`);
|
|
44
|
-
mergedById.set(m.id, merged.length);
|
|
45
|
-
merged.push(m);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return merged.filter((m) => !idsToRemove.has(m.id));
|
|
49
|
-
}
|
|
50
7
|
/** @ignore */
|
|
51
8
|
var MessageGraph = class extends StateGraph {
|
|
52
9
|
constructor() {
|
|
@@ -90,5 +47,5 @@ function pushMessage(message, options) {
|
|
|
90
47
|
}
|
|
91
48
|
|
|
92
49
|
//#endregion
|
|
93
|
-
export { MessageGraph,
|
|
50
|
+
export { MessageGraph, pushMessage };
|
|
94
51
|
//# sourceMappingURL=message.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message.js","names":[],"sources":["../../src/graph/message.ts"],"sourcesContent":["import {\n BaseMessage,\n BaseMessageLike,\n coerceMessageLikeToMessage,\n} from \"@langchain/core/messages\";\nimport type { RunnableConfig } from \"@langchain/core/runnables\";\nimport {
|
|
1
|
+
{"version":3,"file":"message.js","names":[],"sources":["../../src/graph/message.ts"],"sourcesContent":["import {\n BaseMessage,\n BaseMessageLike,\n coerceMessageLikeToMessage,\n} from \"@langchain/core/messages\";\nimport type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { StateGraph } from \"./state.js\";\nimport { ensureLangGraphConfig } from \"../pregel/utils/config.js\";\nimport type { StreamMessagesHandler } from \"../pregel/messages.js\";\nimport { messagesStateReducer, type Messages } from \"./messages_reducer.js\";\n\n/** @ignore */\nexport class MessageGraph extends StateGraph<\n BaseMessage[],\n BaseMessage[],\n Messages\n> {\n constructor() {\n super({\n channels: {\n __root__: {\n reducer: messagesStateReducer,\n default: () => [],\n },\n },\n });\n }\n}\n\n/**\n * Manually push a message to a message stream.\n *\n * This is useful when you need to push a manually created message before the node\n * has finished executing.\n *\n * When a message is pushed, it will be automatically persisted to the state after the node has finished executing.\n * To disable persisting, set `options.stateKey` to `null`.\n *\n * @param message The message to push. The message must have an ID set, otherwise an error will be thrown.\n * @param options RunnableConfig / Runtime coming from node context.\n */\nexport function pushMessage(\n message: BaseMessage | BaseMessageLike,\n options?: RunnableConfig & {\n /**\n * The key of the state to push the message to. Set to `null` to avoid persisting.\n * @default \"messages\"\n */\n stateKey?: string | null;\n }\n) {\n const { stateKey: userStateKey, ...userConfig } = options ?? {};\n const config = ensureLangGraphConfig(userConfig);\n\n let stateKey: string | undefined = userStateKey ?? \"messages\";\n if (userStateKey === null) stateKey = undefined;\n\n // coerce to message\n const validMessage = coerceMessageLikeToMessage(message);\n if (!validMessage.id) throw new Error(\"Message ID is required.\");\n\n const callbacks = (() => {\n if (Array.isArray(config.callbacks)) {\n return config.callbacks;\n }\n\n if (typeof config.callbacks !== \"undefined\") {\n return config.callbacks.handlers;\n }\n\n return [];\n })();\n\n const messagesHandler = callbacks.find(\n (cb): cb is StreamMessagesHandler =>\n \"name\" in cb && cb.name === \"StreamMessagesHandler\"\n );\n\n if (messagesHandler) {\n const metadata = config.metadata ?? {};\n const namespace = (\n (metadata.langgraph_checkpoint_ns ?? \"\") as string\n ).split(\"|\");\n\n messagesHandler._emit(\n [namespace, metadata],\n validMessage,\n undefined,\n false\n );\n }\n\n if (stateKey) {\n config.configurable?.__pregel_send?.([[stateKey, validMessage]]);\n }\n\n return validMessage;\n}\n"],"mappings":";;;;;;;AAYA,IAAa,eAAb,cAAkC,WAIhC;CACA,cAAc;AACZ,QAAM,EACJ,UAAU,EACR,UAAU;GACR,SAAS;GACT,eAAe,EAAE;GAClB,EACF,EACF,CAAC;;;;;;;;;;;;;;;AAgBN,SAAgB,YACd,SACA,SAOA;CACA,MAAM,EAAE,UAAU,cAAc,GAAG,eAAe,WAAW,EAAE;CAC/D,MAAM,SAAS,sBAAsB,WAAW;CAEhD,IAAI,WAA+B,gBAAgB;AACnD,KAAI,iBAAiB,KAAM,YAAW;CAGtC,MAAM,eAAe,2BAA2B,QAAQ;AACxD,KAAI,CAAC,aAAa,GAAI,OAAM,IAAI,MAAM,0BAA0B;CAchE,MAAM,yBAZmB;AACvB,MAAI,MAAM,QAAQ,OAAO,UAAU,CACjC,QAAO,OAAO;AAGhB,MAAI,OAAO,OAAO,cAAc,YAC9B,QAAO,OAAO,UAAU;AAG1B,SAAO,EAAE;KACP,CAE8B,MAC/B,OACC,UAAU,MAAM,GAAG,SAAS,wBAC/B;AAED,KAAI,iBAAiB;EACnB,MAAM,WAAW,OAAO,YAAY,EAAE;EACtC,MAAM,aACH,SAAS,2BAA2B,IACrC,MAAM,IAAI;AAEZ,kBAAgB,MACd,CAAC,WAAW,SAAS,EACrB,cACA,QACA,MACD;;AAGH,KAAI,SACF,QAAO,cAAc,gBAAgB,CAAC,CAAC,UAAU,aAAa,CAAC,CAAC;AAGlE,QAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_annotation = require('./annotation.cjs');
|
|
2
|
+
const require_messages_reducer = require('./messages_reducer.cjs');
|
|
2
3
|
const require_meta = require('./zod/meta.cjs');
|
|
3
|
-
const require_message = require('./message.cjs');
|
|
4
4
|
let zod_v3 = require("zod/v3");
|
|
5
5
|
|
|
6
6
|
//#region src/graph/messages_annotation.ts
|
|
@@ -40,7 +40,7 @@ let zod_v3 = require("zod/v3");
|
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
42
|
const MessagesAnnotation = require_annotation.Annotation.Root({ messages: require_annotation.Annotation({
|
|
43
|
-
reducer:
|
|
43
|
+
reducer: require_messages_reducer.messagesStateReducer,
|
|
44
44
|
default: () => []
|
|
45
45
|
}) });
|
|
46
46
|
/**
|
|
@@ -57,7 +57,7 @@ const MessagesAnnotation = require_annotation.Annotation.Root({ messages: requir
|
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
59
|
const MessagesZodMeta = {
|
|
60
|
-
reducer: { fn:
|
|
60
|
+
reducer: { fn: require_messages_reducer.messagesStateReducer },
|
|
61
61
|
jsonSchemaExtra: { langgraph_type: "messages" },
|
|
62
62
|
default: () => []
|
|
63
63
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages_annotation.cjs","names":["Annotation","messagesStateReducer","z","withLangGraph"],"sources":["../../src/graph/messages_annotation.ts"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\nimport { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Annotation } from \"./annotation.js\";\nimport { Messages, messagesStateReducer } from \"./
|
|
1
|
+
{"version":3,"file":"messages_annotation.cjs","names":["Annotation","messagesStateReducer","z","withLangGraph"],"sources":["../../src/graph/messages_annotation.ts"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\nimport { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Annotation } from \"./annotation.js\";\nimport { Messages, messagesStateReducer } from \"./messages_reducer.js\";\nimport { SchemaMeta, withLangGraph } from \"./zod/meta.js\";\n\n/**\n * Prebuilt state annotation that combines returned messages.\n * Can handle standard messages and special modifiers like {@link RemoveMessage}\n * instances.\n *\n * Specifically, importing and using the prebuilt MessagesAnnotation like this:\n *\n * @example\n * ```ts\n * import { MessagesAnnotation, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(...)\n * ...\n * ```\n *\n * Is equivalent to initializing your state manually like this:\n *\n * @example\n * ```ts\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { Annotation, StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n *\n * export const StateAnnotation = Annotation.Root({\n * messages: Annotation<BaseMessage[]>({\n * reducer: messagesStateReducer,\n * default: () => [],\n * }),\n * });\n *\n * const graph = new StateGraph(StateAnnotation)\n * .addNode(...)\n * ...\n * ```\n */\nexport const MessagesAnnotation = Annotation.Root({\n messages: Annotation<BaseMessage[], Messages>({\n reducer: messagesStateReducer,\n default: () => [],\n }),\n});\n\n/**\n * Prebuilt schema meta for Zod state definition.\n *\n * @example\n * ```ts\n * import { z } from \"zod/v4-mini\";\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const AgentState = z.object({\n * messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),\n * });\n * ```\n */\nexport const MessagesZodMeta: SchemaMeta<BaseMessage[], Messages> = {\n reducer: { fn: messagesStateReducer },\n jsonSchemaExtra: { langgraph_type: \"messages\" },\n default: () => [],\n};\n\n/**\n * Prebuilt state object that uses Zod to combine returned messages.\n * This utility is synonymous with the `MessagesAnnotation` annotation,\n * but uses Zod as the way to express messages state.\n *\n * You can use import and use this prebuilt schema like this:\n *\n * @example\n * ```ts\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesZodState)\n * .addNode(...)\n * ...\n * ```\n *\n * Which is equivalent to initializing the schema object manually like this:\n *\n * @example\n * ```ts\n * import { z } from \"zod\";\n * import type { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n * import { StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n * import \"@langchain/langgraph/zod\";\n *\n * const AgentState = z.object({\n * messages: z\n * .custom<BaseMessage[]>()\n * .default(() => [])\n * .langgraph.reducer(\n * messagesStateReducer,\n * z.custom<BaseMessageLike | BaseMessageLike[]>()\n * ),\n * });\n * const graph = new StateGraph(AgentState)\n * .addNode(...)\n * ...\n * ```\n */\nexport const MessagesZodState = z.object({\n messages: withLangGraph(z.custom<BaseMessage[]>(), MessagesZodMeta),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAa,qBAAqBA,8BAAW,KAAK,EAChD,UAAUA,8BAAoC;CAC5C,SAASC;CACT,eAAe,EAAE;CAClB,CAAC,EACH,CAAC;;;;;;;;;;;;;;AAeF,MAAa,kBAAuD;CAClE,SAAS,EAAE,IAAIA,+CAAsB;CACrC,iBAAiB,EAAE,gBAAgB,YAAY;CAC/C,eAAe,EAAE;CAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,MAAa,mBAAmBC,SAAE,OAAO,EACvC,UAAUC,2BAAcD,SAAE,QAAuB,EAAE,gBAAgB,EACpE,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { BinaryOperatorAggregate } from "../channels/binop.cjs";
|
|
2
2
|
import { ReducedZodChannel, SchemaMeta } from "./zod/meta.cjs";
|
|
3
3
|
import { AnnotationRoot } from "./annotation.cjs";
|
|
4
|
-
import { Messages } from "./
|
|
4
|
+
import { Messages } from "./messages_reducer.cjs";
|
|
5
|
+
import { z } from "zod/v3";
|
|
5
6
|
import * as _langchain_core_utils_types0 from "@langchain/core/utils/types";
|
|
6
7
|
import * as _langchain_core_messages29 from "@langchain/core/messages";
|
|
7
8
|
import { BaseMessage } from "@langchain/core/messages";
|
|
8
|
-
import { z } from "zod/v3";
|
|
9
9
|
|
|
10
10
|
//#region src/graph/messages_annotation.d.ts
|
|
11
11
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages_annotation.d.cts","names":["___web_js7","__annotation_js0","_langchain_core_utils_types0","__zod_meta_js0","BaseMessage","z","Messages","SchemaMeta","MessagesAnnotation","_langchain_core_messages29","MessageToolSet","MessageStructure","MessageType","BinaryOperatorAggregate","AnnotationRoot","MessagesZodMeta","MessagesZodState","ZodTypeDef","ZodType","InteropZodType","ReducedZodChannel","ZodTypeAny","ZodObject"],"sources":["../../src/graph/messages_annotation.d.ts"],"sourcesContent":["import { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Messages } from \"./
|
|
1
|
+
{"version":3,"file":"messages_annotation.d.cts","names":["___web_js7","__annotation_js0","_langchain_core_utils_types0","__zod_meta_js0","BaseMessage","z","Messages","SchemaMeta","MessagesAnnotation","_langchain_core_messages29","MessageToolSet","MessageStructure","MessageType","BinaryOperatorAggregate","AnnotationRoot","MessagesZodMeta","MessagesZodState","ZodTypeDef","ZodType","InteropZodType","ReducedZodChannel","ZodTypeAny","ZodObject"],"sources":["../../src/graph/messages_annotation.d.ts"],"sourcesContent":["import { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Messages } from \"./messages_reducer.js\";\nimport { SchemaMeta } from \"./zod/meta.js\";\n/**\n * Prebuilt state annotation that combines returned messages.\n * Can handle standard messages and special modifiers like {@link RemoveMessage}\n * instances.\n *\n * Specifically, importing and using the prebuilt MessagesAnnotation like this:\n *\n * @example\n * ```ts\n * import { MessagesAnnotation, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(...)\n * ...\n * ```\n *\n * Is equivalent to initializing your state manually like this:\n *\n * @example\n * ```ts\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { Annotation, StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n *\n * export const StateAnnotation = Annotation.Root({\n * messages: Annotation<BaseMessage[]>({\n * reducer: messagesStateReducer,\n * default: () => [],\n * }),\n * });\n *\n * const graph = new StateGraph(StateAnnotation)\n * .addNode(...)\n * ...\n * ```\n */\nexport declare const MessagesAnnotation: import(\"./annotation.js\").AnnotationRoot<{\n messages: import(\"../web.js\").BinaryOperatorAggregate<BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[], Messages>;\n}>;\n/**\n * Prebuilt schema meta for Zod state definition.\n *\n * @example\n * ```ts\n * import { z } from \"zod/v4-mini\";\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const AgentState = z.object({\n * messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),\n * });\n * ```\n */\nexport declare const MessagesZodMeta: SchemaMeta<BaseMessage[], Messages>;\n/**\n * Prebuilt state object that uses Zod to combine returned messages.\n * This utility is synonymous with the `MessagesAnnotation` annotation,\n * but uses Zod as the way to express messages state.\n *\n * You can use import and use this prebuilt schema like this:\n *\n * @example\n * ```ts\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesZodState)\n * .addNode(...)\n * ...\n * ```\n *\n * Which is equivalent to initializing the schema object manually like this:\n *\n * @example\n * ```ts\n * import { z } from \"zod\";\n * import type { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n * import { StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n * import \"@langchain/langgraph/zod\";\n *\n * const AgentState = z.object({\n * messages: z\n * .custom<BaseMessage[]>()\n * .default(() => [])\n * .langgraph.reducer(\n * messagesStateReducer,\n * z.custom<BaseMessageLike | BaseMessageLike[]>()\n * ),\n * });\n * const graph = new StateGraph(AgentState)\n * .addNode(...)\n * ...\n * ```\n */\nexport declare const MessagesZodState: z.ZodObject<{\n messages: import(\"./zod/meta.js\").ReducedZodChannel<z.ZodType<BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[], z.ZodTypeDef, BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[]>, import(\"@langchain/core/utils/types\").InteropZodType<Messages>>;\n}, \"strip\", z.ZodTypeAny, {\n messages: BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[];\n}, {\n messages: BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[];\n}>;\n"],"mappings":";;;;;;;;;;;;;;;;;AAuCA;;;;;;;;;;AAgBA;;;;;;AAwCA;;;;;;;;;;;;;AAC4dE,cAzDvcM,kBAyDgbW,EAxD5YlB,cAwD4YkB,CAAAA;UAAchB,EAxD1OH,uBAwD0OG,CAxDzZC,WAwDyZD,CAxDxSM,0BAAAA,CAAlEE,gBAwD0WR,CAvDjdM,0BAAAA,CAD2JC,cAAAA,CAwDsTP,EAxD1VM,0BAAAA,CAAwFG,WAAAA,CAwDkQT,EAAAA,EAxDlPG,QAwDkPH,CAAAA;;;;;;;;;;;;;;;cAzC9bY,iBAAiBR,WAAWH,eAAeE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAwC3CU,kBAAkBX,CAAAA,CAAEiB;YAC0anB,kBAA3ZE,CAAAA,CAAEa,QAAQd,YAAiHK,0BAAAA,CAAlEE,iBAK/GF,0BAAAA,CALmKC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA,KAAgBP,CAAAA,CAAEY,YAAYb,YAAiHK,0BAAAA,CAAlEE,iBAAjDF,0BAAAA,CAAqGC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA,MAA8EV,4BAAAA,CAAvBiB,eAAeb;YACxcD,CAAAA,CAAEgB;YACAjB,YAAiHK,0BAAAA,CAAlEE,iBADrCF,0BAAAA,CACyFC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA;;YAEvJR,YAAiHK,0BAAAA,CAAlEE,iBAFpCF,0BAAAA,CAEwFC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryOperatorAggregate } from "../channels/binop.js";
|
|
2
2
|
import { ReducedZodChannel, SchemaMeta } from "./zod/meta.js";
|
|
3
3
|
import { AnnotationRoot } from "./annotation.js";
|
|
4
|
-
import { Messages } from "./
|
|
4
|
+
import { Messages } from "./messages_reducer.js";
|
|
5
5
|
import * as _langchain_core_messages29 from "@langchain/core/messages";
|
|
6
6
|
import { BaseMessage } from "@langchain/core/messages";
|
|
7
7
|
import * as _langchain_core_utils_types0 from "@langchain/core/utils/types";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages_annotation.d.ts","names":["___web_js7","__annotation_js0","_langchain_core_utils_types0","__zod_meta_js0","BaseMessage","z","Messages","SchemaMeta","MessagesAnnotation","_langchain_core_messages29","MessageToolSet","MessageStructure","MessageType","BinaryOperatorAggregate","AnnotationRoot","MessagesZodMeta","MessagesZodState","ZodTypeDef","ZodType","InteropZodType","ReducedZodChannel","ZodTypeAny","ZodObject"],"sources":["../../src/graph/messages_annotation.d.ts"],"sourcesContent":["import { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Messages } from \"./
|
|
1
|
+
{"version":3,"file":"messages_annotation.d.ts","names":["___web_js7","__annotation_js0","_langchain_core_utils_types0","__zod_meta_js0","BaseMessage","z","Messages","SchemaMeta","MessagesAnnotation","_langchain_core_messages29","MessageToolSet","MessageStructure","MessageType","BinaryOperatorAggregate","AnnotationRoot","MessagesZodMeta","MessagesZodState","ZodTypeDef","ZodType","InteropZodType","ReducedZodChannel","ZodTypeAny","ZodObject"],"sources":["../../src/graph/messages_annotation.d.ts"],"sourcesContent":["import { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Messages } from \"./messages_reducer.js\";\nimport { SchemaMeta } from \"./zod/meta.js\";\n/**\n * Prebuilt state annotation that combines returned messages.\n * Can handle standard messages and special modifiers like {@link RemoveMessage}\n * instances.\n *\n * Specifically, importing and using the prebuilt MessagesAnnotation like this:\n *\n * @example\n * ```ts\n * import { MessagesAnnotation, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(...)\n * ...\n * ```\n *\n * Is equivalent to initializing your state manually like this:\n *\n * @example\n * ```ts\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { Annotation, StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n *\n * export const StateAnnotation = Annotation.Root({\n * messages: Annotation<BaseMessage[]>({\n * reducer: messagesStateReducer,\n * default: () => [],\n * }),\n * });\n *\n * const graph = new StateGraph(StateAnnotation)\n * .addNode(...)\n * ...\n * ```\n */\nexport declare const MessagesAnnotation: import(\"./annotation.js\").AnnotationRoot<{\n messages: import(\"../web.js\").BinaryOperatorAggregate<BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[], Messages>;\n}>;\n/**\n * Prebuilt schema meta for Zod state definition.\n *\n * @example\n * ```ts\n * import { z } from \"zod/v4-mini\";\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const AgentState = z.object({\n * messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),\n * });\n * ```\n */\nexport declare const MessagesZodMeta: SchemaMeta<BaseMessage[], Messages>;\n/**\n * Prebuilt state object that uses Zod to combine returned messages.\n * This utility is synonymous with the `MessagesAnnotation` annotation,\n * but uses Zod as the way to express messages state.\n *\n * You can use import and use this prebuilt schema like this:\n *\n * @example\n * ```ts\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesZodState)\n * .addNode(...)\n * ...\n * ```\n *\n * Which is equivalent to initializing the schema object manually like this:\n *\n * @example\n * ```ts\n * import { z } from \"zod\";\n * import type { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n * import { StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n * import \"@langchain/langgraph/zod\";\n *\n * const AgentState = z.object({\n * messages: z\n * .custom<BaseMessage[]>()\n * .default(() => [])\n * .langgraph.reducer(\n * messagesStateReducer,\n * z.custom<BaseMessageLike | BaseMessageLike[]>()\n * ),\n * });\n * const graph = new StateGraph(AgentState)\n * .addNode(...)\n * ...\n * ```\n */\nexport declare const MessagesZodState: z.ZodObject<{\n messages: import(\"./zod/meta.js\").ReducedZodChannel<z.ZodType<BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[], z.ZodTypeDef, BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[]>, import(\"@langchain/core/utils/types\").InteropZodType<Messages>>;\n}, \"strip\", z.ZodTypeAny, {\n messages: BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[];\n}, {\n messages: BaseMessage<import(\"@langchain/core/messages\").MessageStructure<import(\"@langchain/core/messages\").MessageToolSet>, import(\"@langchain/core/messages\").MessageType>[];\n}>;\n"],"mappings":";;;;;;;;;;;;;;;;;AAuCA;;;;;;;;;;AAgBA;;;;;;AAwCA;;;;;;;;;;;;;AAC4dE,cAzDvcM,kBAyDgbW,EAxD5YlB,cAwD4YkB,CAAAA;UAAchB,EAxD1OH,uBAwD0OG,CAxDzZC,WAwDyZD,CAxDxSM,0BAAAA,CAAlEE,gBAwD0WR,CAvDjdM,0BAAAA,CAD2JC,cAAAA,CAwDsTP,EAxD1VM,0BAAAA,CAAwFG,WAAAA,CAwDkQT,EAAAA,EAxDlPG,QAwDkPH,CAAAA;;;;;;;;;;;;;;;cAzC9bY,iBAAiBR,WAAWH,eAAeE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAwC3CU,kBAAkBX,CAAAA,CAAEiB;YAC0anB,kBAA3ZE,CAAAA,CAAEa,QAAQd,YAAiHK,0BAAAA,CAAlEE,iBAK/GF,0BAAAA,CALmKC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA,KAAgBP,CAAAA,CAAEY,YAAYb,YAAiHK,0BAAAA,CAAlEE,iBAAjDF,0BAAAA,CAAqGC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA,MAA8EV,4BAAAA,CAAvBiB,eAAeb;YACxcD,CAAAA,CAAEgB;YACAjB,YAAiHK,0BAAAA,CAAlEE,iBADrCF,0BAAAA,CACyFC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA;;YAEvJR,YAAiHK,0BAAAA,CAAlEE,iBAFpCF,0BAAAA,CAEwFC,cAAAA,GAApCD,0BAAAA,CAAwFG,WAAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Annotation } from "./annotation.js";
|
|
2
|
+
import { messagesStateReducer } from "./messages_reducer.js";
|
|
2
3
|
import { withLangGraph } from "./zod/meta.js";
|
|
3
|
-
import { messagesStateReducer } from "./message.js";
|
|
4
4
|
import { z } from "zod/v3";
|
|
5
5
|
|
|
6
6
|
//#region src/graph/messages_annotation.ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages_annotation.js","names":[],"sources":["../../src/graph/messages_annotation.ts"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\nimport { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Annotation } from \"./annotation.js\";\nimport { Messages, messagesStateReducer } from \"./
|
|
1
|
+
{"version":3,"file":"messages_annotation.js","names":[],"sources":["../../src/graph/messages_annotation.ts"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\nimport { BaseMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport { Annotation } from \"./annotation.js\";\nimport { Messages, messagesStateReducer } from \"./messages_reducer.js\";\nimport { SchemaMeta, withLangGraph } from \"./zod/meta.js\";\n\n/**\n * Prebuilt state annotation that combines returned messages.\n * Can handle standard messages and special modifiers like {@link RemoveMessage}\n * instances.\n *\n * Specifically, importing and using the prebuilt MessagesAnnotation like this:\n *\n * @example\n * ```ts\n * import { MessagesAnnotation, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(...)\n * ...\n * ```\n *\n * Is equivalent to initializing your state manually like this:\n *\n * @example\n * ```ts\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { Annotation, StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n *\n * export const StateAnnotation = Annotation.Root({\n * messages: Annotation<BaseMessage[]>({\n * reducer: messagesStateReducer,\n * default: () => [],\n * }),\n * });\n *\n * const graph = new StateGraph(StateAnnotation)\n * .addNode(...)\n * ...\n * ```\n */\nexport const MessagesAnnotation = Annotation.Root({\n messages: Annotation<BaseMessage[], Messages>({\n reducer: messagesStateReducer,\n default: () => [],\n }),\n});\n\n/**\n * Prebuilt schema meta for Zod state definition.\n *\n * @example\n * ```ts\n * import { z } from \"zod/v4-mini\";\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const AgentState = z.object({\n * messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),\n * });\n * ```\n */\nexport const MessagesZodMeta: SchemaMeta<BaseMessage[], Messages> = {\n reducer: { fn: messagesStateReducer },\n jsonSchemaExtra: { langgraph_type: \"messages\" },\n default: () => [],\n};\n\n/**\n * Prebuilt state object that uses Zod to combine returned messages.\n * This utility is synonymous with the `MessagesAnnotation` annotation,\n * but uses Zod as the way to express messages state.\n *\n * You can use import and use this prebuilt schema like this:\n *\n * @example\n * ```ts\n * import { MessagesZodState, StateGraph } from \"@langchain/langgraph\";\n *\n * const graph = new StateGraph(MessagesZodState)\n * .addNode(...)\n * ...\n * ```\n *\n * Which is equivalent to initializing the schema object manually like this:\n *\n * @example\n * ```ts\n * import { z } from \"zod\";\n * import type { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n * import { StateGraph, messagesStateReducer } from \"@langchain/langgraph\";\n * import \"@langchain/langgraph/zod\";\n *\n * const AgentState = z.object({\n * messages: z\n * .custom<BaseMessage[]>()\n * .default(() => [])\n * .langgraph.reducer(\n * messagesStateReducer,\n * z.custom<BaseMessageLike | BaseMessageLike[]>()\n * ),\n * });\n * const graph = new StateGraph(AgentState)\n * .addNode(...)\n * ...\n * ```\n */\nexport const MessagesZodState = z.object({\n messages: withLangGraph(z.custom<BaseMessage[]>(), MessagesZodMeta),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAa,qBAAqB,WAAW,KAAK,EAChD,UAAU,WAAoC;CAC5C,SAAS;CACT,eAAe,EAAE;CAClB,CAAC,EACH,CAAC;;;;;;;;;;;;;;AAeF,MAAa,kBAAuD;CAClE,SAAS,EAAE,IAAI,sBAAsB;CACrC,iBAAiB,EAAE,gBAAgB,YAAY;CAC/C,eAAe,EAAE;CAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,MAAa,mBAAmB,EAAE,OAAO,EACvC,UAAU,cAAc,EAAE,QAAuB,EAAE,gBAAgB,EACpE,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
let uuid = require("uuid");
|
|
2
|
+
let _langchain_core_messages = require("@langchain/core/messages");
|
|
3
|
+
|
|
4
|
+
//#region src/graph/messages_reducer.ts
|
|
5
|
+
/**
|
|
6
|
+
* Special value that signifies the intent to remove all previous messages in the state reducer.
|
|
7
|
+
* Used as the unique identifier for a `RemoveMessage` instance which, when encountered,
|
|
8
|
+
* causes all prior messages to be discarded, leaving only those following this marker.
|
|
9
|
+
*/
|
|
10
|
+
const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
11
|
+
/**
|
|
12
|
+
* Reducer function for combining two sets of messages in LangGraph's state system.
|
|
13
|
+
*
|
|
14
|
+
* This reducer handles several tasks:
|
|
15
|
+
* 1. Normalizes both `left` and `right` message inputs to arrays.
|
|
16
|
+
* 2. Coerces any message-like objects into real `BaseMessage` instances.
|
|
17
|
+
* 3. Ensures all messages have unique, stable IDs by generating missing ones.
|
|
18
|
+
* 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,
|
|
19
|
+
* all previous messages are discarded and only the subsequent messages in `right` are returned.
|
|
20
|
+
* 5. Otherwise, merges `left` and `right` messages together following these rules:
|
|
21
|
+
* - If a message in `right` shares an ID with a message in `left`:
|
|
22
|
+
* - If it is a `RemoveMessage`, that message (by ID) is marked for removal.
|
|
23
|
+
* - If it is a normal message, it replaces the message with the same ID from `left`.
|
|
24
|
+
* - If a message in `right` **does not exist** in `left`:
|
|
25
|
+
* - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).
|
|
26
|
+
* - Otherwise, the message is appended.
|
|
27
|
+
* - Messages flagged for removal are omitted from the final output.
|
|
28
|
+
*
|
|
29
|
+
* @param left - The existing array (or single message) of messages from current state.
|
|
30
|
+
* @param right - The new array (or single message) of messages to be applied.
|
|
31
|
+
* @returns A new array of `BaseMessage` objects representing the updated state.
|
|
32
|
+
*
|
|
33
|
+
* @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const msg1 = new AIMessage("hello");
|
|
38
|
+
* const msg2 = new HumanMessage("hi");
|
|
39
|
+
* const removal = new RemoveMessage({ id: msg1.id });
|
|
40
|
+
* const newState = messagesStateReducer([msg1], [msg2, removal]);
|
|
41
|
+
* // newState will only contain msg2 (msg1 is removed)
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function messagesStateReducer(left, right) {
|
|
45
|
+
const leftArray = Array.isArray(left) ? left : [left];
|
|
46
|
+
const rightArray = Array.isArray(right) ? right : [right];
|
|
47
|
+
const leftMessages = leftArray.map(_langchain_core_messages.coerceMessageLikeToMessage);
|
|
48
|
+
const rightMessages = rightArray.map(_langchain_core_messages.coerceMessageLikeToMessage);
|
|
49
|
+
for (const m of leftMessages) if (m.id === null || m.id === void 0) {
|
|
50
|
+
m.id = (0, uuid.v4)();
|
|
51
|
+
m.lc_kwargs.id = m.id;
|
|
52
|
+
}
|
|
53
|
+
let removeAllIdx;
|
|
54
|
+
for (let i = 0; i < rightMessages.length; i += 1) {
|
|
55
|
+
const m = rightMessages[i];
|
|
56
|
+
if (m.id === null || m.id === void 0) {
|
|
57
|
+
m.id = (0, uuid.v4)();
|
|
58
|
+
m.lc_kwargs.id = m.id;
|
|
59
|
+
}
|
|
60
|
+
if (_langchain_core_messages.RemoveMessage.isInstance(m) && m.id === REMOVE_ALL_MESSAGES) removeAllIdx = i;
|
|
61
|
+
}
|
|
62
|
+
if (removeAllIdx != null) return rightMessages.slice(removeAllIdx + 1);
|
|
63
|
+
const merged = [...leftMessages];
|
|
64
|
+
const mergedById = new Map(merged.map((m, i) => [m.id, i]));
|
|
65
|
+
const idsToRemove = /* @__PURE__ */ new Set();
|
|
66
|
+
for (const m of rightMessages) {
|
|
67
|
+
const existingIdx = mergedById.get(m.id);
|
|
68
|
+
if (existingIdx !== void 0) if (_langchain_core_messages.RemoveMessage.isInstance(m)) idsToRemove.add(m.id);
|
|
69
|
+
else {
|
|
70
|
+
idsToRemove.delete(m.id);
|
|
71
|
+
merged[existingIdx] = m;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
if (_langchain_core_messages.RemoveMessage.isInstance(m)) throw new Error(`Attempting to delete a message with an ID that doesn't exist ('${m.id}')`);
|
|
75
|
+
mergedById.set(m.id, merged.length);
|
|
76
|
+
merged.push(m);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return merged.filter((m) => !idsToRemove.has(m.id));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
exports.REMOVE_ALL_MESSAGES = REMOVE_ALL_MESSAGES;
|
|
84
|
+
exports.messagesStateReducer = messagesStateReducer;
|
|
85
|
+
//# sourceMappingURL=messages_reducer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages_reducer.cjs","names":["coerceMessageLikeToMessage","RemoveMessage"],"sources":["../../src/graph/messages_reducer.ts"],"sourcesContent":["import {\n BaseMessage,\n BaseMessageLike,\n coerceMessageLikeToMessage,\n RemoveMessage,\n} from \"@langchain/core/messages\";\nimport { v4 } from \"uuid\";\n\n/**\n * Special value that signifies the intent to remove all previous messages in the state reducer.\n * Used as the unique identifier for a `RemoveMessage` instance which, when encountered,\n * causes all prior messages to be discarded, leaving only those following this marker.\n */\nexport const REMOVE_ALL_MESSAGES = \"__remove_all__\";\n\n/**\n * Type that represents an acceptable input for the messages state reducer.\n *\n * - Can be a single `BaseMessage` or `BaseMessageLike`.\n * - Can be an array of `BaseMessage` or `BaseMessageLike`.\n */\nexport type Messages =\n | Array<BaseMessage | BaseMessageLike>\n | BaseMessage\n | BaseMessageLike;\n\n/**\n * Reducer function for combining two sets of messages in LangGraph's state system.\n *\n * This reducer handles several tasks:\n * 1. Normalizes both `left` and `right` message inputs to arrays.\n * 2. Coerces any message-like objects into real `BaseMessage` instances.\n * 3. Ensures all messages have unique, stable IDs by generating missing ones.\n * 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,\n * all previous messages are discarded and only the subsequent messages in `right` are returned.\n * 5. Otherwise, merges `left` and `right` messages together following these rules:\n * - If a message in `right` shares an ID with a message in `left`:\n * - If it is a `RemoveMessage`, that message (by ID) is marked for removal.\n * - If it is a normal message, it replaces the message with the same ID from `left`.\n * - If a message in `right` **does not exist** in `left`:\n * - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).\n * - Otherwise, the message is appended.\n * - Messages flagged for removal are omitted from the final output.\n *\n * @param left - The existing array (or single message) of messages from current state.\n * @param right - The new array (or single message) of messages to be applied.\n * @returns A new array of `BaseMessage` objects representing the updated state.\n *\n * @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.\n *\n * @example\n * ```ts\n * const msg1 = new AIMessage(\"hello\");\n * const msg2 = new HumanMessage(\"hi\");\n * const removal = new RemoveMessage({ id: msg1.id });\n * const newState = messagesStateReducer([msg1], [msg2, removal]);\n * // newState will only contain msg2 (msg1 is removed)\n * ```\n */\nexport function messagesStateReducer(\n left: Messages,\n right: Messages\n): BaseMessage[] {\n // Ensure both left and right are arrays\n const leftArray = Array.isArray(left) ? left : [left];\n const rightArray = Array.isArray(right) ? right : [right];\n\n // Convert all input to BaseMessage instances\n const leftMessages = (leftArray as BaseMessageLike[]).map(\n coerceMessageLikeToMessage\n );\n const rightMessages = (rightArray as BaseMessageLike[]).map(\n coerceMessageLikeToMessage\n );\n\n // Assign missing IDs to any message in the left array\n for (const m of leftMessages) {\n if (m.id === null || m.id === undefined) {\n m.id = v4();\n m.lc_kwargs.id = m.id;\n }\n }\n\n // Assign missing IDs and check for \"remove all\" marker in right array\n let removeAllIdx: number | undefined;\n for (let i = 0; i < rightMessages.length; i += 1) {\n const m = rightMessages[i];\n if (m.id === null || m.id === undefined) {\n m.id = v4();\n m.lc_kwargs.id = m.id;\n }\n\n // If RemoveMessage with special REMOVE_ALL_MESSAGES id is found\n if (RemoveMessage.isInstance(m) && m.id === REMOVE_ALL_MESSAGES) {\n removeAllIdx = i;\n }\n }\n\n // If remove-all is present, all previous messages are wiped; return only subsequent ones\n if (removeAllIdx != null) return rightMessages.slice(removeAllIdx + 1);\n\n // Begin normal merging logic\n const merged = [...leftMessages];\n const mergedById = new Map(merged.map((m, i) => [m.id, i]));\n const idsToRemove = new Set();\n\n for (const m of rightMessages) {\n const existingIdx = mergedById.get(m.id);\n if (existingIdx !== undefined) {\n // Case: updating or removing an existing message by id\n if (RemoveMessage.isInstance(m)) {\n idsToRemove.add(m.id);\n } else {\n idsToRemove.delete(m.id);\n merged[existingIdx] = m;\n }\n } else {\n // Case: inserting a completely new message\n if (RemoveMessage.isInstance(m)) {\n throw new Error(\n `Attempting to delete a message with an ID that doesn't exist ('${m.id}')`\n );\n }\n mergedById.set(m.id, merged.length);\n merged.push(m);\n }\n }\n\n // Remove any messages whose IDs are marked for removal\n return merged.filter((m) => !idsToRemove.has(m.id));\n}\n"],"mappings":";;;;;;;;;AAaA,MAAa,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CnC,SAAgB,qBACd,MACA,OACe;CAEf,MAAM,YAAY,MAAM,QAAQ,KAAK,GAAG,OAAO,CAAC,KAAK;CACrD,MAAM,aAAa,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM;CAGzD,MAAM,eAAgB,UAAgC,IACpDA,oDACD;CACD,MAAM,gBAAiB,WAAiC,IACtDA,oDACD;AAGD,MAAK,MAAM,KAAK,aACd,KAAI,EAAE,OAAO,QAAQ,EAAE,OAAO,QAAW;AACvC,IAAE,mBAAS;AACX,IAAE,UAAU,KAAK,EAAE;;CAKvB,IAAI;AACJ,MAAK,IAAI,IAAI,GAAG,IAAI,cAAc,QAAQ,KAAK,GAAG;EAChD,MAAM,IAAI,cAAc;AACxB,MAAI,EAAE,OAAO,QAAQ,EAAE,OAAO,QAAW;AACvC,KAAE,mBAAS;AACX,KAAE,UAAU,KAAK,EAAE;;AAIrB,MAAIC,uCAAc,WAAW,EAAE,IAAI,EAAE,OAAO,oBAC1C,gBAAe;;AAKnB,KAAI,gBAAgB,KAAM,QAAO,cAAc,MAAM,eAAe,EAAE;CAGtE,MAAM,SAAS,CAAC,GAAG,aAAa;CAChC,MAAM,aAAa,IAAI,IAAI,OAAO,KAAK,GAAG,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;CAC3D,MAAM,8BAAc,IAAI,KAAK;AAE7B,MAAK,MAAM,KAAK,eAAe;EAC7B,MAAM,cAAc,WAAW,IAAI,EAAE,GAAG;AACxC,MAAI,gBAAgB,OAElB,KAAIA,uCAAc,WAAW,EAAE,CAC7B,aAAY,IAAI,EAAE,GAAG;OAChB;AACL,eAAY,OAAO,EAAE,GAAG;AACxB,UAAO,eAAe;;OAEnB;AAEL,OAAIA,uCAAc,WAAW,EAAE,CAC7B,OAAM,IAAI,MACR,kEAAkE,EAAE,GAAG,IACxE;AAEH,cAAW,IAAI,EAAE,IAAI,OAAO,OAAO;AACnC,UAAO,KAAK,EAAE;;;AAKlB,QAAO,OAAO,QAAQ,MAAM,CAAC,YAAY,IAAI,EAAE,GAAG,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
2
|
+
|
|
3
|
+
//#region src/graph/messages_reducer.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Special value that signifies the intent to remove all previous messages in the state reducer.
|
|
7
|
+
* Used as the unique identifier for a `RemoveMessage` instance which, when encountered,
|
|
8
|
+
* causes all prior messages to be discarded, leaving only those following this marker.
|
|
9
|
+
*/
|
|
10
|
+
declare const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
11
|
+
/**
|
|
12
|
+
* Type that represents an acceptable input for the messages state reducer.
|
|
13
|
+
*
|
|
14
|
+
* - Can be a single `BaseMessage` or `BaseMessageLike`.
|
|
15
|
+
* - Can be an array of `BaseMessage` or `BaseMessageLike`.
|
|
16
|
+
*/
|
|
17
|
+
type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
18
|
+
/**
|
|
19
|
+
* Reducer function for combining two sets of messages in LangGraph's state system.
|
|
20
|
+
*
|
|
21
|
+
* This reducer handles several tasks:
|
|
22
|
+
* 1. Normalizes both `left` and `right` message inputs to arrays.
|
|
23
|
+
* 2. Coerces any message-like objects into real `BaseMessage` instances.
|
|
24
|
+
* 3. Ensures all messages have unique, stable IDs by generating missing ones.
|
|
25
|
+
* 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,
|
|
26
|
+
* all previous messages are discarded and only the subsequent messages in `right` are returned.
|
|
27
|
+
* 5. Otherwise, merges `left` and `right` messages together following these rules:
|
|
28
|
+
* - If a message in `right` shares an ID with a message in `left`:
|
|
29
|
+
* - If it is a `RemoveMessage`, that message (by ID) is marked for removal.
|
|
30
|
+
* - If it is a normal message, it replaces the message with the same ID from `left`.
|
|
31
|
+
* - If a message in `right` **does not exist** in `left`:
|
|
32
|
+
* - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).
|
|
33
|
+
* - Otherwise, the message is appended.
|
|
34
|
+
* - Messages flagged for removal are omitted from the final output.
|
|
35
|
+
*
|
|
36
|
+
* @param left - The existing array (or single message) of messages from current state.
|
|
37
|
+
* @param right - The new array (or single message) of messages to be applied.
|
|
38
|
+
* @returns A new array of `BaseMessage` objects representing the updated state.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const msg1 = new AIMessage("hello");
|
|
45
|
+
* const msg2 = new HumanMessage("hi");
|
|
46
|
+
* const removal = new RemoveMessage({ id: msg1.id });
|
|
47
|
+
* const newState = messagesStateReducer([msg1], [msg2, removal]);
|
|
48
|
+
* // newState will only contain msg2 (msg1 is removed)
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function messagesStateReducer(left: Messages, right: Messages): BaseMessage[];
|
|
52
|
+
//#endregion
|
|
53
|
+
export { Messages, REMOVE_ALL_MESSAGES, messagesStateReducer };
|
|
54
|
+
//# sourceMappingURL=messages_reducer.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages_reducer.d.cts","names":["BaseMessage","BaseMessageLike","REMOVE_ALL_MESSAGES","Messages","Array","messagesStateReducer"],"sources":["../../src/graph/messages_reducer.d.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n/**\n * Special value that signifies the intent to remove all previous messages in the state reducer.\n * Used as the unique identifier for a `RemoveMessage` instance which, when encountered,\n * causes all prior messages to be discarded, leaving only those following this marker.\n */\nexport declare const REMOVE_ALL_MESSAGES = \"__remove_all__\";\n/**\n * Type that represents an acceptable input for the messages state reducer.\n *\n * - Can be a single `BaseMessage` or `BaseMessageLike`.\n * - Can be an array of `BaseMessage` or `BaseMessageLike`.\n */\nexport type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;\n/**\n * Reducer function for combining two sets of messages in LangGraph's state system.\n *\n * This reducer handles several tasks:\n * 1. Normalizes both `left` and `right` message inputs to arrays.\n * 2. Coerces any message-like objects into real `BaseMessage` instances.\n * 3. Ensures all messages have unique, stable IDs by generating missing ones.\n * 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,\n * all previous messages are discarded and only the subsequent messages in `right` are returned.\n * 5. Otherwise, merges `left` and `right` messages together following these rules:\n * - If a message in `right` shares an ID with a message in `left`:\n * - If it is a `RemoveMessage`, that message (by ID) is marked for removal.\n * - If it is a normal message, it replaces the message with the same ID from `left`.\n * - If a message in `right` **does not exist** in `left`:\n * - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).\n * - Otherwise, the message is appended.\n * - Messages flagged for removal are omitted from the final output.\n *\n * @param left - The existing array (or single message) of messages from current state.\n * @param right - The new array (or single message) of messages to be applied.\n * @returns A new array of `BaseMessage` objects representing the updated state.\n *\n * @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.\n *\n * @example\n * ```ts\n * const msg1 = new AIMessage(\"hello\");\n * const msg2 = new HumanMessage(\"hi\");\n * const removal = new RemoveMessage({ id: msg1.id });\n * const newState = messagesStateReducer([msg1], [msg2, removal]);\n * // newState will only contain msg2 (msg1 is removed)\n * ```\n */\nexport declare function messagesStateReducer(left: Messages, right: Messages): BaseMessage[];\n"],"mappings":";;;;;;AAMA;AAOA;;AAA6BA,cAPRE,mBAAAA,GAOQF,gBAAAA;;;;;;AAkC7B;AAA4C,KAlChCG,QAAAA,GAAWC,KAkCqB,CAlCfJ,WAkCe,GAlCDC,eAkCC,CAAA,GAlCkBD,WAkClB,GAlCgCC,eAkChC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAApBI,oBAAAA,OAA2BF,iBAAiBA,WAAWH"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
|
2
|
+
|
|
3
|
+
//#region src/graph/messages_reducer.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Special value that signifies the intent to remove all previous messages in the state reducer.
|
|
7
|
+
* Used as the unique identifier for a `RemoveMessage` instance which, when encountered,
|
|
8
|
+
* causes all prior messages to be discarded, leaving only those following this marker.
|
|
9
|
+
*/
|
|
10
|
+
declare const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
11
|
+
/**
|
|
12
|
+
* Type that represents an acceptable input for the messages state reducer.
|
|
13
|
+
*
|
|
14
|
+
* - Can be a single `BaseMessage` or `BaseMessageLike`.
|
|
15
|
+
* - Can be an array of `BaseMessage` or `BaseMessageLike`.
|
|
16
|
+
*/
|
|
17
|
+
type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;
|
|
18
|
+
/**
|
|
19
|
+
* Reducer function for combining two sets of messages in LangGraph's state system.
|
|
20
|
+
*
|
|
21
|
+
* This reducer handles several tasks:
|
|
22
|
+
* 1. Normalizes both `left` and `right` message inputs to arrays.
|
|
23
|
+
* 2. Coerces any message-like objects into real `BaseMessage` instances.
|
|
24
|
+
* 3. Ensures all messages have unique, stable IDs by generating missing ones.
|
|
25
|
+
* 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,
|
|
26
|
+
* all previous messages are discarded and only the subsequent messages in `right` are returned.
|
|
27
|
+
* 5. Otherwise, merges `left` and `right` messages together following these rules:
|
|
28
|
+
* - If a message in `right` shares an ID with a message in `left`:
|
|
29
|
+
* - If it is a `RemoveMessage`, that message (by ID) is marked for removal.
|
|
30
|
+
* - If it is a normal message, it replaces the message with the same ID from `left`.
|
|
31
|
+
* - If a message in `right` **does not exist** in `left`:
|
|
32
|
+
* - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).
|
|
33
|
+
* - Otherwise, the message is appended.
|
|
34
|
+
* - Messages flagged for removal are omitted from the final output.
|
|
35
|
+
*
|
|
36
|
+
* @param left - The existing array (or single message) of messages from current state.
|
|
37
|
+
* @param right - The new array (or single message) of messages to be applied.
|
|
38
|
+
* @returns A new array of `BaseMessage` objects representing the updated state.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const msg1 = new AIMessage("hello");
|
|
45
|
+
* const msg2 = new HumanMessage("hi");
|
|
46
|
+
* const removal = new RemoveMessage({ id: msg1.id });
|
|
47
|
+
* const newState = messagesStateReducer([msg1], [msg2, removal]);
|
|
48
|
+
* // newState will only contain msg2 (msg1 is removed)
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function messagesStateReducer(left: Messages, right: Messages): BaseMessage[];
|
|
52
|
+
//#endregion
|
|
53
|
+
export { Messages, REMOVE_ALL_MESSAGES, messagesStateReducer };
|
|
54
|
+
//# sourceMappingURL=messages_reducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages_reducer.d.ts","names":["BaseMessage","BaseMessageLike","REMOVE_ALL_MESSAGES","Messages","Array","messagesStateReducer"],"sources":["../../src/graph/messages_reducer.d.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\n/**\n * Special value that signifies the intent to remove all previous messages in the state reducer.\n * Used as the unique identifier for a `RemoveMessage` instance which, when encountered,\n * causes all prior messages to be discarded, leaving only those following this marker.\n */\nexport declare const REMOVE_ALL_MESSAGES = \"__remove_all__\";\n/**\n * Type that represents an acceptable input for the messages state reducer.\n *\n * - Can be a single `BaseMessage` or `BaseMessageLike`.\n * - Can be an array of `BaseMessage` or `BaseMessageLike`.\n */\nexport type Messages = Array<BaseMessage | BaseMessageLike> | BaseMessage | BaseMessageLike;\n/**\n * Reducer function for combining two sets of messages in LangGraph's state system.\n *\n * This reducer handles several tasks:\n * 1. Normalizes both `left` and `right` message inputs to arrays.\n * 2. Coerces any message-like objects into real `BaseMessage` instances.\n * 3. Ensures all messages have unique, stable IDs by generating missing ones.\n * 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,\n * all previous messages are discarded and only the subsequent messages in `right` are returned.\n * 5. Otherwise, merges `left` and `right` messages together following these rules:\n * - If a message in `right` shares an ID with a message in `left`:\n * - If it is a `RemoveMessage`, that message (by ID) is marked for removal.\n * - If it is a normal message, it replaces the message with the same ID from `left`.\n * - If a message in `right` **does not exist** in `left`:\n * - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).\n * - Otherwise, the message is appended.\n * - Messages flagged for removal are omitted from the final output.\n *\n * @param left - The existing array (or single message) of messages from current state.\n * @param right - The new array (or single message) of messages to be applied.\n * @returns A new array of `BaseMessage` objects representing the updated state.\n *\n * @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.\n *\n * @example\n * ```ts\n * const msg1 = new AIMessage(\"hello\");\n * const msg2 = new HumanMessage(\"hi\");\n * const removal = new RemoveMessage({ id: msg1.id });\n * const newState = messagesStateReducer([msg1], [msg2, removal]);\n * // newState will only contain msg2 (msg1 is removed)\n * ```\n */\nexport declare function messagesStateReducer(left: Messages, right: Messages): BaseMessage[];\n"],"mappings":";;;;;;AAMA;AAOA;;AAA6BA,cAPRE,mBAAAA,GAOQF,gBAAAA;;;;;;AAkC7B;AAA4C,KAlChCG,QAAAA,GAAWC,KAkCqB,CAlCfJ,WAkCe,GAlCDC,eAkCC,CAAA,GAlCkBD,WAkClB,GAlCgCC,eAkChC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAApBI,oBAAAA,OAA2BF,iBAAiBA,WAAWH"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
import { RemoveMessage, coerceMessageLikeToMessage } from "@langchain/core/messages";
|
|
3
|
+
|
|
4
|
+
//#region src/graph/messages_reducer.ts
|
|
5
|
+
/**
|
|
6
|
+
* Special value that signifies the intent to remove all previous messages in the state reducer.
|
|
7
|
+
* Used as the unique identifier for a `RemoveMessage` instance which, when encountered,
|
|
8
|
+
* causes all prior messages to be discarded, leaving only those following this marker.
|
|
9
|
+
*/
|
|
10
|
+
const REMOVE_ALL_MESSAGES = "__remove_all__";
|
|
11
|
+
/**
|
|
12
|
+
* Reducer function for combining two sets of messages in LangGraph's state system.
|
|
13
|
+
*
|
|
14
|
+
* This reducer handles several tasks:
|
|
15
|
+
* 1. Normalizes both `left` and `right` message inputs to arrays.
|
|
16
|
+
* 2. Coerces any message-like objects into real `BaseMessage` instances.
|
|
17
|
+
* 3. Ensures all messages have unique, stable IDs by generating missing ones.
|
|
18
|
+
* 4. If a `RemoveMessage` instance is encountered in `right` with the ID `REMOVE_ALL_MESSAGES`,
|
|
19
|
+
* all previous messages are discarded and only the subsequent messages in `right` are returned.
|
|
20
|
+
* 5. Otherwise, merges `left` and `right` messages together following these rules:
|
|
21
|
+
* - If a message in `right` shares an ID with a message in `left`:
|
|
22
|
+
* - If it is a `RemoveMessage`, that message (by ID) is marked for removal.
|
|
23
|
+
* - If it is a normal message, it replaces the message with the same ID from `left`.
|
|
24
|
+
* - If a message in `right` **does not exist** in `left`:
|
|
25
|
+
* - If it is a `RemoveMessage`, this is considered an error (cannot remove non-existent ID).
|
|
26
|
+
* - Otherwise, the message is appended.
|
|
27
|
+
* - Messages flagged for removal are omitted from the final output.
|
|
28
|
+
*
|
|
29
|
+
* @param left - The existing array (or single message) of messages from current state.
|
|
30
|
+
* @param right - The new array (or single message) of messages to be applied.
|
|
31
|
+
* @returns A new array of `BaseMessage` objects representing the updated state.
|
|
32
|
+
*
|
|
33
|
+
* @throws Error if a `RemoveMessage` is used to delete a message with an ID that does not exist in the merged list.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const msg1 = new AIMessage("hello");
|
|
38
|
+
* const msg2 = new HumanMessage("hi");
|
|
39
|
+
* const removal = new RemoveMessage({ id: msg1.id });
|
|
40
|
+
* const newState = messagesStateReducer([msg1], [msg2, removal]);
|
|
41
|
+
* // newState will only contain msg2 (msg1 is removed)
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function messagesStateReducer(left, right) {
|
|
45
|
+
const leftArray = Array.isArray(left) ? left : [left];
|
|
46
|
+
const rightArray = Array.isArray(right) ? right : [right];
|
|
47
|
+
const leftMessages = leftArray.map(coerceMessageLikeToMessage);
|
|
48
|
+
const rightMessages = rightArray.map(coerceMessageLikeToMessage);
|
|
49
|
+
for (const m of leftMessages) if (m.id === null || m.id === void 0) {
|
|
50
|
+
m.id = v4();
|
|
51
|
+
m.lc_kwargs.id = m.id;
|
|
52
|
+
}
|
|
53
|
+
let removeAllIdx;
|
|
54
|
+
for (let i = 0; i < rightMessages.length; i += 1) {
|
|
55
|
+
const m = rightMessages[i];
|
|
56
|
+
if (m.id === null || m.id === void 0) {
|
|
57
|
+
m.id = v4();
|
|
58
|
+
m.lc_kwargs.id = m.id;
|
|
59
|
+
}
|
|
60
|
+
if (RemoveMessage.isInstance(m) && m.id === REMOVE_ALL_MESSAGES) removeAllIdx = i;
|
|
61
|
+
}
|
|
62
|
+
if (removeAllIdx != null) return rightMessages.slice(removeAllIdx + 1);
|
|
63
|
+
const merged = [...leftMessages];
|
|
64
|
+
const mergedById = new Map(merged.map((m, i) => [m.id, i]));
|
|
65
|
+
const idsToRemove = /* @__PURE__ */ new Set();
|
|
66
|
+
for (const m of rightMessages) {
|
|
67
|
+
const existingIdx = mergedById.get(m.id);
|
|
68
|
+
if (existingIdx !== void 0) if (RemoveMessage.isInstance(m)) idsToRemove.add(m.id);
|
|
69
|
+
else {
|
|
70
|
+
idsToRemove.delete(m.id);
|
|
71
|
+
merged[existingIdx] = m;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
if (RemoveMessage.isInstance(m)) throw new Error(`Attempting to delete a message with an ID that doesn't exist ('${m.id}')`);
|
|
75
|
+
mergedById.set(m.id, merged.length);
|
|
76
|
+
merged.push(m);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return merged.filter((m) => !idsToRemove.has(m.id));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
export { REMOVE_ALL_MESSAGES, messagesStateReducer };
|
|
84
|
+
//# sourceMappingURL=messages_reducer.js.map
|