Merge consecutive Messages of the same type.
NOTE: ToolMessages are not merged, as each has a distinct tool call id that can't be merged.
mergeMessageRuns(
): Runnable<BaseMessage<MessageStructure<MessageToolSet>, MessageType>[], BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]>import { mergeMessageRuns, AIMessage, HumanMessage, SystemMessage, ToolCall } from "@langchain/core/messages";
const messages = [
new SystemMessage("you're a good assistant."),
new HumanMessage({ content: "what's your favorite color", id: "foo" }),
new HumanMessage({ content: "wait your favorite food", id: "bar" }),
new AIMessage({
content: "my favorite colo",
tool_calls: [{ name: "blah_tool", args: { x: 2 }, id: "123" }],
id: "baz",
}),
new AIMessage({
content: [{ type: "text", text: "my favorite dish is lasagna" }],
tool_calls: [{ name: "blah_tool", args: { x: -10 }, id: "456" }],
id: "blur",
}),
];
mergeMessageRuns(messages);