-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
chat_history.ts
80 lines (68 loc) · 2.61 KB
/
chat_history.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Serializable } from "./load/serializable.js";
import { type BaseMessage, HumanMessage, AIMessage } from "./messages/index.js";
/**
* Base class for all chat message histories. All chat message histories
* should extend this class.
*/
export abstract class BaseChatMessageHistory extends Serializable {
public abstract getMessages(): Promise<BaseMessage[]>;
public abstract addMessage(message: BaseMessage): Promise<void>;
public abstract addUserMessage(message: string): Promise<void>;
public abstract addAIChatMessage(message: string): Promise<void>;
public abstract clear(): Promise<void>;
}
/**
* Base class for all list chat message histories. All list chat message
* histories should extend this class.
*/
export abstract class BaseListChatMessageHistory extends Serializable {
/** Returns a list of messages stored in the store. */
public abstract getMessages(): Promise<BaseMessage[]>;
/**
* Add a message object to the store.
*/
public abstract addMessage(message: BaseMessage): Promise<void>;
/**
* This is a convenience method for adding a human message string to the store.
* Please note that this is a convenience method. Code should favor the
* bulk addMessages interface instead to save on round-trips to the underlying
* persistence layer.
* This method may be deprecated in a future release.
*/
public addUserMessage(message: string): Promise<void> {
return this.addMessage(new HumanMessage(message));
}
/** @deprecated Use addAIMessage instead */
public addAIChatMessage(message: string): Promise<void> {
return this.addMessage(new AIMessage(message));
}
/**
* This is a convenience method for adding an AI message string to the store.
* Please note that this is a convenience method. Code should favor the bulk
* addMessages interface instead to save on round-trips to the underlying
* persistence layer.
* This method may be deprecated in a future release.
*/
public addAIMessage(message: string): Promise<void> {
return this.addMessage(new AIMessage(message));
}
/**
* Add a list of messages.
*
* Implementations should override this method to handle bulk addition of messages
* in an efficient manner to avoid unnecessary round-trips to the underlying store.
*
* @param messages - A list of BaseMessage objects to store.
*/
public async addMessages(messages: BaseMessage[]): Promise<void> {
for (const message of messages) {
await this.addMessage(message);
}
}
/**
* Remove all messages from the store.
*/
public clear(): Promise<void> {
throw new Error("Not implemented.");
}
}