nestjs-ai-middleware-adapter-mongodb 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -1
- package/dist/index.js +79 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MongoClient } from "mongodb";
|
|
2
|
-
import type { AiCaller, AiConversationListQuery, AiConversationSession, AiConversationSummary, AiConversationTurnQuery, AiConversationTurnRecord, AiDatabaseAdapter, AiRunEventRecord, AiRunPage } from "nestjs-ai-middleware";
|
|
2
|
+
import type { AiCaller, AiConversationListQuery, AiConversationSearchMatch, AiConversationSearchQuery, AiConversationSession, AiConversationSummary, AiConversationTurnQuery, AiConversationTurnRecord, AiDatabaseAdapter, AiRunEventRecord, AiRunPage } from "nestjs-ai-middleware";
|
|
3
3
|
export declare class MongoDatabaseAdapter implements AiDatabaseAdapter {
|
|
4
4
|
private readonly client;
|
|
5
5
|
private readonly eventPageSize;
|
|
@@ -21,6 +21,7 @@ export declare class MongoDatabaseAdapter implements AiDatabaseAdapter {
|
|
|
21
21
|
finishConversation({ session, runId, turn, discardIfEmpty, }: Parameters<AiDatabaseAdapter["finishConversation"]>[0], signal: AbortSignal): Promise<void>;
|
|
22
22
|
listConversationTurns(session: AiConversationSession, query: AiConversationTurnQuery, signal: AbortSignal): Promise<AiConversationTurnRecord[] | undefined>;
|
|
23
23
|
listConversations(caller: AiCaller, { before, limit }: AiConversationListQuery, signal: AbortSignal): Promise<AiConversationSummary[]>;
|
|
24
|
+
searchConversationTurns(caller: AiCaller, { excludeSessionId, keywords, limit }: AiConversationSearchQuery, signal: AbortSignal): Promise<AiConversationSearchMatch[]>;
|
|
24
25
|
startRun(runId: string, startedAt: number, signal: AbortSignal): Promise<void>;
|
|
25
26
|
attachRunSession(runId: string, sessionId: string, signal: AbortSignal): Promise<void>;
|
|
26
27
|
appendRunEvent(event: AiRunEventRecord, signal: AbortSignal): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -195,6 +195,85 @@ export class MongoDatabaseAdapter {
|
|
|
195
195
|
], { signal })
|
|
196
196
|
.toArray();
|
|
197
197
|
}
|
|
198
|
+
async searchConversationTurns(caller, { excludeSessionId, keywords, limit }, signal) {
|
|
199
|
+
signal.throwIfAborted();
|
|
200
|
+
const matches = keywords.map((keyword) => ({
|
|
201
|
+
$cond: [
|
|
202
|
+
{
|
|
203
|
+
$regexMatch: {
|
|
204
|
+
input: { $concat: ["$user", "\n", "$assistant"] },
|
|
205
|
+
regex: keyword.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"),
|
|
206
|
+
options: "i",
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
1,
|
|
210
|
+
0,
|
|
211
|
+
],
|
|
212
|
+
}));
|
|
213
|
+
return this.sessions
|
|
214
|
+
.aggregate([
|
|
215
|
+
{
|
|
216
|
+
$match: {
|
|
217
|
+
_id: { $ne: excludeSessionId },
|
|
218
|
+
subjectId: caller.subjectId,
|
|
219
|
+
tenantId: caller.tenantId ?? null,
|
|
220
|
+
turnCount: { $gt: 0 },
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
$lookup: {
|
|
225
|
+
from: this.turns.collectionName,
|
|
226
|
+
localField: "_id",
|
|
227
|
+
foreignField: "sessionId",
|
|
228
|
+
as: "turn",
|
|
229
|
+
pipeline: [
|
|
230
|
+
{ $set: { score: { $sum: matches } } },
|
|
231
|
+
{ $match: { score: { $gt: 0 } } },
|
|
232
|
+
{
|
|
233
|
+
$lookup: {
|
|
234
|
+
from: this.runs.collectionName,
|
|
235
|
+
localField: "_id",
|
|
236
|
+
foreignField: "_id",
|
|
237
|
+
as: "run",
|
|
238
|
+
pipeline: [{ $project: { startedAt: 1 } }],
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
$set: {
|
|
243
|
+
startedAt: {
|
|
244
|
+
$ifNull: [{ $first: "$run.startedAt" }, null],
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
{ $sort: { score: -1, startedAt: -1, _id: -1 } },
|
|
249
|
+
{ $limit: limit },
|
|
250
|
+
{ $project: { score: 1, startedAt: 1, user: 1, assistant: 1 } },
|
|
251
|
+
],
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
{ $unwind: "$turn" },
|
|
255
|
+
{
|
|
256
|
+
$sort: {
|
|
257
|
+
"turn.score": -1,
|
|
258
|
+
"turn.startedAt": -1,
|
|
259
|
+
_id: -1,
|
|
260
|
+
"turn._id": -1,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{ $limit: limit },
|
|
264
|
+
{
|
|
265
|
+
$project: {
|
|
266
|
+
_id: 0,
|
|
267
|
+
sessionId: "$_id",
|
|
268
|
+
turnId: "$turn._id",
|
|
269
|
+
startedAt: "$turn.startedAt",
|
|
270
|
+
user: "$turn.user",
|
|
271
|
+
assistant: "$turn.assistant",
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
], { signal })
|
|
275
|
+
.toArray();
|
|
276
|
+
}
|
|
198
277
|
async startRun(runId, startedAt, signal) {
|
|
199
278
|
signal.throwIfAborted();
|
|
200
279
|
await this.runs.insertOne({ _id: runId, sessionId: null, status: "running", startedAt }, this.bookkeepingOptions);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-ai-middleware-adapter-mongodb",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Mongodb storage adapter for NestJS AI middleware",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"prepack": "npm run build"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"nestjs-ai-middleware": "^0.3.
|
|
32
|
+
"nestjs-ai-middleware": "^0.3.1",
|
|
33
33
|
"mongodb": "^7.9.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"nestjs-ai-middleware": "0.3.
|
|
36
|
+
"nestjs-ai-middleware": "0.3.1"
|
|
37
37
|
}
|
|
38
38
|
}
|