File size: 2,811 Bytes
8d88d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Migration } from ".";
import { collections } from "$lib/server/database";
import { Collection, FindCursor, ObjectId } from "mongodb";
import { logger } from "$lib/server/logger";
import type { Conversation } from "$lib/types/Conversation";

const BATCH_SIZE = 1000;
const DELETE_THRESHOLD_MS = 60 * 60 * 1000;

async function deleteBatch(conversations: Collection<Conversation>, ids: ObjectId[]) {
	if (ids.length === 0) return 0;
	const deleteResult = await conversations.deleteMany({ _id: { $in: ids } });
	return deleteResult.deletedCount;
}

async function processCursor<T>(
	cursor: FindCursor<T>,
	processBatchFn: (batch: T[]) => Promise<void>
) {
	let batch = [];
	while (await cursor.hasNext()) {
		const doc = await cursor.next();
		if (doc) {
			batch.push(doc);
		}
		if (batch.length >= BATCH_SIZE) {
			await processBatchFn(batch);
			batch = [];
		}
	}
	if (batch.length > 0) {
		await processBatchFn(batch);
	}
}

export async function deleteConversations(
	collections: typeof import("$lib/server/database").collections
) {
	let deleteCount = 0;
	const { conversations, sessions } = collections;

	// First criteria: Delete conversations with no user/assistant messages older than 1 hour
	const emptyConvCursor = conversations
		.find({
			"messages.from": { $not: { $in: ["user", "assistant"] } },
			createdAt: { $lt: new Date(Date.now() - DELETE_THRESHOLD_MS) },
		})
		.batchSize(BATCH_SIZE);

	await processCursor(emptyConvCursor, async (batch) => {
		const ids = batch.map((doc) => doc._id);
		deleteCount += await deleteBatch(conversations, ids);
	});

	// Second criteria: Process conversations without users in batches and check sessions
	const noUserCursor = conversations.find({ userId: { $exists: false } }).batchSize(BATCH_SIZE);

	await processCursor(noUserCursor, async (batch) => {
		const sessionIds = [
			...new Set(batch.map((conv) => conv.sessionId).filter((id): id is string => !!id)),
		];

		const existingSessions = await sessions.find({ sessionId: { $in: sessionIds } }).toArray();
		const validSessionIds = new Set(existingSessions.map((s) => s.sessionId));

		const invalidConvs = batch.filter(
			(conv) => !conv.sessionId || !validSessionIds.has(conv.sessionId)
		);
		const idsToDelete = invalidConvs.map((conv) => conv._id);
		deleteCount += await deleteBatch(conversations, idsToDelete);
	});

	logger.info(`[MIGRATIONS] Deleted ${deleteCount} conversations in total.`);
	return deleteCount;
}

const deleteEmptyConversations: Migration = {
	_id: new ObjectId("000000000000000000000009"),
	name: "Delete conversations with no user or assistant messages or valid sessions",
	up: async () => {
		await deleteConversations(collections);
		return true;
	},
	runEveryTime: false,
	runForHuggingChat: "only",
};

export default deleteEmptyConversations;