Skip to content

Commit

Permalink
core[minor]: Add RemoveMessage class (#6089)
Browse files Browse the repository at this point in the history
* core[minor]: Add RemoveMessage class

* add comment
  • Loading branch information
bracesproul authored Jul 16, 2024
1 parent 33c359b commit 7bad0f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export type MessageType =
| "generic"
| "system"
| "function"
| "tool";
| "tool"
| "remove";

export type ImageDetail = "auto" | "low" | "high";

Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./human.js";
export * from "./system.js";
export * from "./utils.js";
export * from "./transformers.js";
export * from "./modifier.js";
// TODO: Use a star export when we deprecate the
// existing "ToolCall" type in "base.js".
export {
Expand Down
31 changes: 31 additions & 0 deletions langchain-core/src/messages/modifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { BaseMessage, BaseMessageFields, MessageType } from "./base.js";

export interface RemoveMessageFields
extends Omit<BaseMessageFields, "content"> {
/**
* The ID of the message to remove.
*/
id: string;
}

/**
* Message responsible for deleting other messages.
*/
export class RemoveMessage extends BaseMessage {
/**
* The ID of the message to remove.
*/
id: string;

constructor(fields: RemoveMessageFields) {
super({
...fields,
content: "",
});
this.id = fields.id;
}

_getType(): MessageType {
return "remove";
}
}
11 changes: 9 additions & 2 deletions langchain-core/src/messages/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
FunctionMessageFieldsWithName,
} from "./function.js";
import { HumanMessage, HumanMessageChunk } from "./human.js";
import { RemoveMessage } from "./modifier.js";
import { SystemMessage, SystemMessageChunk } from "./system.js";
import {
ToolMessage,
Expand All @@ -33,14 +34,16 @@ export type MessageUnion =
| typeof SystemMessage
| typeof ChatMessage
| typeof FunctionMessage
| typeof ToolMessage;
| typeof ToolMessage
| typeof RemoveMessage;
export type MessageChunkUnion =
| typeof HumanMessageChunk
| typeof AIMessageChunk
| typeof SystemMessageChunk
| typeof FunctionMessageChunk
| typeof ToolMessageChunk
| typeof ChatMessageChunk;
| typeof ChatMessageChunk
| typeof RemoveMessage; // RemoveMessage does not have a chunk class.
export type MessageTypeOrClass = MessageType | MessageUnion | MessageChunkUnion;

const _isMessageType = (msg: BaseMessage, types: MessageTypeOrClass[]) => {
Expand Down Expand Up @@ -915,6 +918,10 @@ const _MSG_CHUNK_MAP: Record<
message: ChatMessage,
messageChunk: ChatMessageChunk,
},
remove: {
message: RemoveMessage,
messageChunk: RemoveMessage, // RemoveMessage does not have a chunk class.
},
};

function _switchTypeToMessage(
Expand Down

0 comments on commit 7bad0f8

Please sign in to comment.