-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
xata.ts
241 lines (221 loc) Β· 6.61 KB
/
xata.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
BaseClient,
BaseClientOptions,
GetTableSchemaResponse,
Schemas,
XataApiClient,
parseWorkspacesUrlParts,
} from "@xata.io/client";
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
StoredMessage,
StoredMessageData,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "@langchain/core/messages";
/**
* An object type that represents the input for the XataChatMessageHistory
* class.
*/
export type XataChatMessageHistoryInput<XataClient> = {
sessionId: string;
config?: BaseClientOptions;
client?: XataClient;
table?: string;
createTable?: boolean;
apiKey?: string;
};
/**
* An interface that represents the data transfer object for stored
* messages.
*/
interface storedMessagesDTO {
id: string;
sessionId: string;
type: string;
content: string;
role?: string;
name?: string;
additionalKwargs: string;
}
const chatMemoryColumns: Schemas.Column[] = [
{ name: "sessionId", type: "string" },
{ name: "type", type: "string" },
{ name: "role", type: "string" },
{ name: "content", type: "text" },
{ name: "name", type: "string" },
{ name: "additionalKwargs", type: "text" },
];
/**
* A class for managing chat message history using Xata.io client. It
* extends the BaseListChatMessageHistory class and provides methods to
* get, add, and clear messages. It also ensures the existence of a table
* where the chat messages are stored.
* @example
* ```typescript
* const chatHistory = new XataChatMessageHistory({
* table: "messages",
* sessionId: new Date().toISOString(),
* client: new BaseClient({
* databaseURL: process.env.XATA_DB_URL,
* apiKey: process.env.XATA_API_KEY,
* branch: "main",
* }),
* apiKey: process.env.XATA_API_KEY,
* });
*
* const chain = new ConversationChain({
* llm: new ChatOpenAI(),
* memory: new BufferMemory({ chatHistory }),
* });
*
* const response = await chain.invoke({
* input: "What did I just say my name was?",
* });
* console.log({ response });
* ```
*/
export class XataChatMessageHistory<
XataClient extends BaseClient
> extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "xata"];
public client: XataClient;
private sessionId: string;
private table: string;
private tableInitialized: boolean;
private createTable: boolean;
private apiClient: XataApiClient;
constructor(fields: XataChatMessageHistoryInput<XataClient>) {
super(fields);
const { sessionId, config, client, table } = fields;
this.sessionId = sessionId;
this.table = table || "memory";
if (client) {
this.client = client;
} else if (config) {
this.client = new BaseClient(config) as XataClient;
} else {
throw new Error(
"Either a client or a config must be provided to XataChatMessageHistoryInput"
);
}
if (fields.createTable !== false) {
this.createTable = true;
const apiKey = fields.apiKey || fields.config?.apiKey;
if (!apiKey) {
throw new Error(
"If createTable is set, an apiKey must be provided to XataChatMessageHistoryInput, either directly or through the config object"
);
}
this.apiClient = new XataApiClient({ apiKey });
} else {
this.createTable = false;
}
this.tableInitialized = false;
}
/**
* Retrieves all messages associated with the session ID, ordered by
* creation time.
* @returns A promise that resolves to an array of BaseMessage instances.
*/
async getMessages(): Promise<BaseMessage[]> {
await this.ensureTable();
const records = await this.client.db[this.table]
.filter({ sessionId: this.sessionId })
.sort("xata.createdAt", "asc")
.getAll();
const rawStoredMessages = records as unknown as storedMessagesDTO[];
const orderedMessages: StoredMessage[] = rawStoredMessages.map(
(message: storedMessagesDTO) => {
const data = {
content: message.content,
additional_kwargs: JSON.parse(message.additionalKwargs),
} as StoredMessageData;
if (message.role) {
data.role = message.role;
}
if (message.name) {
data.name = message.name;
}
return {
type: message.type,
data,
};
}
);
return mapStoredMessagesToChatMessages(orderedMessages);
}
/**
* Adds a new message to the database.
* @param message The BaseMessage instance to be added.
* @returns A promise that resolves when the message has been added.
*/
async addMessage(message: BaseMessage): Promise<void> {
await this.ensureTable();
const messageToAdd = mapChatMessagesToStoredMessages([message]);
await this.client.db[this.table].create({
sessionId: this.sessionId,
type: messageToAdd[0].type,
content: messageToAdd[0].data.content,
role: messageToAdd[0].data.role,
name: messageToAdd[0].data.name,
additionalKwargs: JSON.stringify(messageToAdd[0].data.additional_kwargs),
});
}
/**
* Deletes all messages associated with the session ID.
* @returns A promise that resolves when the messages have been deleted.
*/
async clear(): Promise<void> {
await this.ensureTable();
const records = await this.client.db[this.table]
.select(["id"])
.filter({ sessionId: this.sessionId })
.getAll();
const ids = records.map((m) => m.id);
await this.client.db[this.table].delete(ids);
}
/**
* Checks if the table exists and creates it if it doesn't. This method is
* called before any operation on the table.
* @returns A promise that resolves when the table has been ensured.
*/
private async ensureTable(): Promise<void> {
if (!this.createTable) {
return;
}
if (this.tableInitialized) {
return;
}
const { databaseURL, branch } = await this.client.getConfig();
const [, , host, , database] = databaseURL.split("/");
const urlParts = parseWorkspacesUrlParts(host);
if (urlParts == null) {
throw new Error("Invalid databaseURL");
}
const { workspace, region } = urlParts;
const tableParams = {
workspace,
region,
database,
branch,
table: this.table,
};
let schema: GetTableSchemaResponse | null = null;
try {
schema = await this.apiClient.tables.getTableSchema(tableParams);
} catch (e) {
// pass
}
if (schema == null) {
await this.apiClient.tables.createTable(tableParams);
await this.apiClient.tables.setTableSchema({
...tableParams,
schema: {
columns: chatMemoryColumns,
},
});
}
}
}