Skip to content

Commit

Permalink
fix: fix mapStoredMessageToChatMessage to check on generic role when …
Browse files Browse the repository at this point in the history
…creating ChatMessage
  • Loading branch information
karlomedallo committed Jul 29, 2024
1 parent e09b66f commit 71294dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
23 changes: 22 additions & 1 deletion langchain-core/src/messages/tests/message_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import {
trimMessages,
} from "../transformers.js";
import { AIMessage } from "../ai.js";
import { ChatMessage } from "../chat.js";
import { HumanMessage } from "../human.js";
import { SystemMessage } from "../system.js";
import { BaseMessage } from "../base.js";
import { getBufferString } from "../utils.js";
import {
getBufferString,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "../utils.js";

describe("filterMessage", () => {
const getMessages = () => [
Expand Down Expand Up @@ -499,3 +504,19 @@ test("getBufferString can handle complex messages", () => {
)}`
);
});

describe("chat message conversions", () => {
it("can convert a chat message to a stored message and back", () => {
const originalMessages = [
new ChatMessage("I'm a generic message!", "human"),
new HumanMessage("I'm a human message!"),
];

const storedMessages = mapChatMessagesToStoredMessages(originalMessages);

const convertedBackMessages =
mapStoredMessagesToChatMessages(storedMessages);

expect(convertedBackMessages).toEqual(originalMessages);
});
});
2 changes: 1 addition & 1 deletion langchain-core/src/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function mapStoredMessageToChatMessage(message: StoredMessage) {
return new ToolMessage(
storedMessage.data as ToolMessageFieldsWithToolCallId
);
case "chat": {
case "generic": {
if (storedMessage.data.role === undefined) {
throw new Error("Role must be defined for chat messages");
}
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/runnables/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function revive(obj: any): any {
content: obj.content,
});
}
if (obj.type === "ChatMessage" || obj.type === "chat") {
if (obj.type === "ChatMessage" || obj.type === "generic") {
return new ChatMessage({
content: obj.content,
role: obj.role,
Expand Down

0 comments on commit 71294dd

Please sign in to comment.