-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
cloudflare_d1.ts
199 lines (170 loc) Β· 5.61 KB
/
cloudflare_d1.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
import { v4 } from "uuid";
import type { D1Database } from "@cloudflare/workers-types";
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
StoredMessage,
StoredMessageData,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "@langchain/core/messages";
/**
* @deprecated Install and import from "@langchain/cloudflare" instead.
*
* Type definition for the input parameters required when instantiating a
* CloudflareD1MessageHistory object.
*/
export type CloudflareD1MessageHistoryInput = {
tableName?: string;
sessionId: string;
database?: D1Database;
};
/**
* @deprecated Install and import from "@langchain/cloudflare" instead.
*
* Interface for the data transfer object used when selecting stored
* messages from the Cloudflare D1 database.
*/
interface selectStoredMessagesDTO {
id: string;
session_id: string;
type: string;
content: string;
role: string | null;
name: string | null;
additional_kwargs: string;
}
/**
* @deprecated Install and import from "@langchain/cloudflare" instead.
*
* Class for storing and retrieving chat message history from a
* Cloudflare D1 database. Extends the BaseListChatMessageHistory class.
* @example
* ```typescript
* const memory = new BufferMemory({
* returnMessages: true,
* chatHistory: new CloudflareD1MessageHistory({
* tableName: "stored_message",
* sessionId: "example",
* database: env.DB,
* }),
* });
*
* const chainInput = { input };
*
* const res = await memory.chatHistory.invoke(chainInput);
* await memory.saveContext(chainInput, {
* output: res,
* });
* ```
*/
export class CloudflareD1MessageHistory extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "cloudflare_d1"];
public database: D1Database;
private tableName: string;
private sessionId: string;
private tableInitialized: boolean;
constructor(fields: CloudflareD1MessageHistoryInput) {
super(fields);
const { sessionId, database, tableName } = fields;
if (database) {
this.database = database;
} else {
throw new Error(
"Either a client or config must be provided to CloudflareD1MessageHistory"
);
}
this.tableName = tableName || "langchain_chat_histories";
this.tableInitialized = false;
this.sessionId = sessionId;
}
/**
* Private method to ensure that the necessary table exists in the
* Cloudflare D1 database before performing any operations. If the table
* does not exist, it is created.
* @returns Promise that resolves to void.
*/
private async ensureTable(): Promise<void> {
if (this.tableInitialized) {
return;
}
const query = `CREATE TABLE IF NOT EXISTS ${this.tableName} (id TEXT PRIMARY KEY, session_id TEXT, type TEXT, content TEXT, role TEXT, name TEXT, additional_kwargs TEXT);`;
await this.database.prepare(query).bind().all();
const idIndexQuery = `CREATE INDEX IF NOT EXISTS id_index ON ${this.tableName} (id);`;
await this.database.prepare(idIndexQuery).bind().all();
const sessionIdIndexQuery = `CREATE INDEX IF NOT EXISTS session_id_index ON ${this.tableName} (session_id);`;
await this.database.prepare(sessionIdIndexQuery).bind().all();
this.tableInitialized = true;
}
/**
* Method to retrieve all messages from the Cloudflare D1 database for the
* current session.
* @returns Promise that resolves to an array of BaseMessage objects.
*/
async getMessages(): Promise<BaseMessage[]> {
await this.ensureTable();
const query = `SELECT * FROM ${this.tableName} WHERE session_id = ?`;
const rawStoredMessages = await this.database
.prepare(query)
.bind(this.sessionId)
.all();
const storedMessagesObject =
rawStoredMessages.results as unknown as selectStoredMessagesDTO[];
const orderedMessages: StoredMessage[] = storedMessagesObject.map(
(message) => {
const data = {
content: message.content,
additional_kwargs: JSON.parse(message.additional_kwargs),
} as StoredMessageData;
if (message.role) {
data.role = message.role;
}
if (message.name) {
data.name = message.name;
}
return {
type: message.type,
data,
};
}
);
return mapStoredMessagesToChatMessages(orderedMessages);
}
/**
* Method to add a new message to the Cloudflare D1 database for the current
* session.
* @param message The BaseMessage object to be added to the database.
* @returns Promise that resolves to void.
*/
async addMessage(message: BaseMessage): Promise<void> {
await this.ensureTable();
const messageToAdd = mapChatMessagesToStoredMessages([message]);
const query = `INSERT INTO ${this.tableName} (id, session_id, type, content, role, name, additional_kwargs) VALUES(?, ?, ?, ?, ?, ?, ?)`;
const id = v4();
await this.database
.prepare(query)
.bind(
id,
this.sessionId,
messageToAdd[0].type || null,
messageToAdd[0].data.content || null,
messageToAdd[0].data.role || null,
messageToAdd[0].data.name || null,
JSON.stringify(messageToAdd[0].data.additional_kwargs)
)
.all();
}
/**
* Method to delete all messages from the Cloudflare D1 database for the
* current session.
* @returns Promise that resolves to void.
*/
async clear(): Promise<void> {
await this.ensureTable();
const query = `DELETE FROM ? WHERE session_id = ? `;
await this.database
.prepare(query)
.bind(this.tableName, this.sessionId)
.all();
}
}