-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
convex.ts
224 lines (209 loc) Β· 6.71 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// eslint-disable-next-line import/no-extraneous-dependencies
import {
FieldPaths,
FunctionReference,
GenericActionCtx,
GenericDataModel,
NamedTableInfo,
TableNamesInDataModel,
VectorIndexNames,
makeFunctionReference,
} from "convex/server";
// eslint-disable-next-line import/no-extraneous-dependencies
import { Value } from "convex/values";
import { BaseStore } from "@langchain/core/stores";
/**
* Type that defines the config required to initialize the
* ConvexKVStore class. It includes the table name,
* index name, field name.
*/
export type ConvexKVStoreConfig<
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>,
KeyFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>,
ValueFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>,
UpsertMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; document: object }
>,
LookupQuery extends FunctionReference<
"query",
"internal",
{ table: string; index: string; keyField: string; key: string },
object[]
>,
DeleteManyMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; index: string; keyField: string; key: string }
>
> = {
readonly ctx: GenericActionCtx<DataModel>;
/**
* Defaults to "cache"
*/
readonly table?: TableName;
/**
* Defaults to "byKey"
*/
readonly index?: IndexName;
/**
* Defaults to "key"
*/
readonly keyField?: KeyFieldName;
/**
* Defaults to "value"
*/
readonly valueField?: ValueFieldName;
/**
* Defaults to `internal.langchain.db.upsert`
*/
readonly upsert?: UpsertMutation;
/**
* Defaults to `internal.langchain.db.lookup`
*/
readonly lookup?: LookupQuery;
/**
* Defaults to `internal.langchain.db.deleteMany`
*/
readonly deleteMany?: DeleteManyMutation;
};
/**
* Class that extends the BaseStore class to interact with a Convex
* database. It provides methods for getting, setting, and deleting key value pairs,
* as well as yielding keys from the database.
*/
export class ConvexKVStore<
T extends Value,
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>,
KeyFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>,
ValueFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>,
UpsertMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; document: object }
>,
LookupQuery extends FunctionReference<
"query",
"internal",
{ table: string; index: string; keyField: string; key: string },
object[]
>,
DeleteManyMutation extends FunctionReference<
"mutation",
"internal",
{ table: string; index: string; keyField: string; key: string }
>
> extends BaseStore<string, T> {
lc_namespace = ["langchain", "storage", "convex"];
private readonly ctx: GenericActionCtx<DataModel>;
private readonly table: TableName;
private readonly index: IndexName;
private readonly keyField: KeyFieldName;
private readonly valueField: ValueFieldName;
private readonly upsert: UpsertMutation;
private readonly lookup: LookupQuery;
private readonly deleteMany: DeleteManyMutation;
constructor(
config: ConvexKVStoreConfig<
DataModel,
TableName,
IndexName,
KeyFieldName,
ValueFieldName,
UpsertMutation,
LookupQuery,
DeleteManyMutation
>
) {
super(config);
this.ctx = config.ctx;
this.table = config.table ?? ("cache" as TableName);
this.index = config.index ?? ("byKey" as IndexName);
this.keyField = config.keyField ?? ("key" as KeyFieldName);
this.valueField = config.valueField ?? ("value" as ValueFieldName);
this.upsert =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config.upsert ?? (makeFunctionReference("langchain/db:upsert") as any);
this.lookup =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config.lookup ?? (makeFunctionReference("langchain/db:lookup") as any);
this.deleteMany =
config.deleteMany ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(makeFunctionReference("langchain/db:deleteMany") as any);
}
/**
* Gets multiple keys from the Convex database.
* @param keys Array of keys to be retrieved.
* @returns An array of retrieved values.
*/
async mget(keys: string[]) {
return (await Promise.all(
keys.map(async (key) => {
const found = (await this.ctx.runQuery(this.lookup, {
table: this.table,
index: this.index,
keyField: this.keyField,
key,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)) as any;
return found.length > 0 ? found[0][this.valueField] : undefined;
})
)) as (T | undefined)[];
}
/**
* Sets multiple keys in the Convex database.
* @param keyValuePairs Array of key-value pairs to be set.
* @returns Promise that resolves when all keys have been set.
*/
async mset(keyValuePairs: [string, T][]): Promise<void> {
// TODO: Remove chunking when Convex handles the concurrent requests correctly
const PAGE_SIZE = 16;
for (let i = 0; i < keyValuePairs.length; i += PAGE_SIZE) {
await Promise.all(
keyValuePairs.slice(i, i + PAGE_SIZE).map(([key, value]) =>
this.ctx.runMutation(this.upsert, {
table: this.table,
index: this.index,
keyField: this.keyField,
key,
document: { [this.keyField]: key, [this.valueField]: value },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
)
);
}
}
/**
* Deletes multiple keys from the Convex database.
* @param keys Array of keys to be deleted.
* @returns Promise that resolves when all keys have been deleted.
*/
async mdelete(keys: string[]): Promise<void> {
await Promise.all(
keys.map((key) =>
this.ctx.runMutation(this.deleteMany, {
table: this.table,
index: this.index,
keyField: this.keyField,
key,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
)
);
}
/**
* Yields keys from the Convex database.
* @param prefix Optional prefix to filter the keys.
* @returns An AsyncGenerator that yields keys from the Convex database.
*/
// eslint-disable-next-line require-yield
async *yieldKeys(_prefix?: string): AsyncGenerator<string> {
throw new Error("yieldKeys not implemented yet for ConvexKVStore");
}
}