-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
convex.ts
210 lines (193 loc) Β· 5.9 KB
/
convex.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
/* eslint-disable @typescript-eslint/no-explicit-any */
// eslint-disable-next-line import/no-extraneous-dependencies
import {
DocumentByInfo,
DocumentByName,
FieldPaths,
FunctionReference,
GenericActionCtx,
GenericDataModel,
NamedTableInfo,
TableNamesInDataModel,
IndexNames,
makeFunctionReference,
} from "convex/server";
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "@langchain/core/messages";
/**
* Type that defines the config required to initialize the
* ConvexChatMessageHistory class. At minimum it needs a sessionId
* and an ActionCtx.
*/
export type ConvexChatMessageHistoryInput<
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel> = "messages",
IndexName extends IndexNames<
NamedTableInfo<DataModel, TableName>
> = "bySessionId",
SessionIdFieldName extends FieldPaths<
NamedTableInfo<DataModel, TableName>
> = "sessionId",
MessageTextFieldName extends FieldPaths<
NamedTableInfo<DataModel, TableName>
> = "message",
InsertMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; document: object }
> = any,
LookupQuery extends FunctionReference<
"query",
"internal",
{ table: string; index: string; keyField: string; key: string },
object[]
> = any,
DeleteManyMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; index: string; keyField: string; key: string }
> = any
> = {
readonly ctx: GenericActionCtx<DataModel>;
readonly sessionId: DocumentByName<DataModel, TableName>[SessionIdFieldName];
/**
* Defaults to "messages"
*/
readonly table?: TableName;
/**
* Defaults to "bySessionId"
*/
readonly index?: IndexName;
/**
* Defaults to "sessionId"
*/
readonly sessionIdField?: SessionIdFieldName;
/**
* Defaults to "message"
*/
readonly messageTextFieldName?: MessageTextFieldName;
/**
* Defaults to `internal.langchain.db.insert`
*/
readonly insert?: InsertMutation;
/**
* Defaults to `internal.langchain.db.lookup`
*/
readonly lookup?: LookupQuery;
/**
* Defaults to `internal.langchain.db.deleteMany`
*/
readonly deleteMany?: DeleteManyMutation;
};
export class ConvexChatMessageHistory<
DataModel extends GenericDataModel,
SessionIdFieldName extends FieldPaths<
NamedTableInfo<DataModel, TableName>
> = "sessionId",
TableName extends TableNamesInDataModel<DataModel> = "messages",
IndexName extends IndexNames<
NamedTableInfo<DataModel, TableName>
> = "bySessionId",
MessageTextFieldName extends FieldPaths<
NamedTableInfo<DataModel, TableName>
> = "message",
InsertMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; document: object }
> = any,
LookupQuery extends FunctionReference<
"query",
"internal",
{ table: string; index: string; keyField: string; key: string },
object[]
> = any,
DeleteManyMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; index: string; keyField: string; key: string }
> = any
> extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "convex"];
private readonly ctx: GenericActionCtx<DataModel>;
private readonly sessionId: DocumentByInfo<
NamedTableInfo<DataModel, TableName>
>[SessionIdFieldName];
private readonly table: TableName;
private readonly index: IndexName;
private readonly sessionIdField: SessionIdFieldName;
private readonly messageTextFieldName: MessageTextFieldName;
private readonly insert: InsertMutation;
private readonly lookup: LookupQuery;
private readonly deleteMany: DeleteManyMutation;
constructor(
config: ConvexChatMessageHistoryInput<
DataModel,
TableName,
IndexName,
SessionIdFieldName,
MessageTextFieldName,
InsertMutation,
LookupQuery,
DeleteManyMutation
>
) {
super();
this.ctx = config.ctx;
this.sessionId = config.sessionId;
this.table = config.table ?? ("messages" as TableName);
this.index = config.index ?? ("bySessionId" as IndexName);
this.sessionIdField =
config.sessionIdField ?? ("sessionId" as SessionIdFieldName);
this.messageTextFieldName =
config.messageTextFieldName ?? ("message" as MessageTextFieldName);
this.insert =
config.insert ?? (makeFunctionReference("langchain/db:insert") as any);
this.lookup =
config.lookup ?? (makeFunctionReference("langchain/db:lookup") as any);
this.deleteMany =
config.deleteMany ??
(makeFunctionReference("langchain/db:deleteMany") as any);
}
async getMessages(): Promise<BaseMessage[]> {
const convexDocuments: any[] = await this.ctx.runQuery(this.lookup, {
table: this.table,
index: this.index,
keyField: this.sessionIdField,
key: this.sessionId,
} as any);
return mapStoredMessagesToChatMessages(
convexDocuments.map((doc) => doc[this.messageTextFieldName])
);
}
async addMessage(message: BaseMessage): Promise<void> {
const messages = mapChatMessagesToStoredMessages([message]);
// TODO: Remove chunking when Convex handles the concurrent requests correctly
const PAGE_SIZE = 16;
for (let i = 0; i < messages.length; i += PAGE_SIZE) {
await Promise.all(
messages.slice(i, i + PAGE_SIZE).map((message) =>
this.ctx.runMutation(this.insert, {
table: this.table,
document: {
[this.sessionIdField]: this.sessionId,
[this.messageTextFieldName]: message,
},
} as any)
)
);
}
}
async clear(): Promise<void> {
await this.ctx.runMutation(this.deleteMany, {
table: this.table,
index: this.index,
keyField: this.sessionIdField,
key: this.sessionId,
} as any);
}
}