Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

perf(move): group imported conversations without repeated copies

Afloatecfc7eb0b399
diff --git a/server/cloud-chat-merge-grouping.test.ts b/server/cloud-chat-merge-grouping.test.ts
new file mode 100644
index 0000000000..9a5270c4ba
--- /dev/null
+++ b/server/cloud-chat-merge-grouping.test.ts
@@ -0,0 +1,28 @@
+import { expect, it } from "vitest";
+import { applyChatMerge, type ChatMergePlan, type ChatStore } from "./cloud-chat-merge.ts";
+
+it("keeps each owner's incoming order and registers every task before importing rows", () => {
+ const bots = [{ id: "a", tasks: [{ threadId: "old-a" }] }, { id: "b", tasks: [] as Array<{ threadId: string }> }];
+ const groups = [{ id: "a", tasks: [{ threadId: "old-room" }] }];
+ const owners = [{ kind: "bot", id: "a" }, { kind: "group", id: "a" }, { kind: "bot", id: "b" }] as const;
+ const plan = { threads: Array.from({ length: 12 }, (_, i) => ({
+ owner: owners[i % owners.length], threadId: `thread-${i}`, task: { threadId: `thread-${i}` }, added: [], relinked: [], leaf: null,
+ })) } as unknown as ChatMergePlan;
+ const original = JSON.stringify(plan), imported: string[] = [], patched: string[] = [];
+ const store = {
+ bots, groups,
+ patchBot(id: string, patch: { tasks: Array<{ threadId: string }> }) { bots.find((bot) => bot.id === id)!.tasks = patch.tasks; patched.push(`bot:${id}`); },
+ patchGroup(id: string) { patched.push(`group:${id}`); },
+ importTranscript(threadId: string) {
+ expect(patched).toEqual(["bot:a", "bot:b", "group:a"]);
+ imported.push(threadId);
+ },
+ } as unknown as ChatStore;
+
+ applyChatMerge(store, plan);
+ expect(bots[0]!.tasks.map((task) => task.threadId)).toEqual(["old-a", "thread-0", "thread-3", "thread-6", "thread-9"]);
+ expect(bots[1]!.tasks.map((task) => task.threadId)).toEqual(["thread-2", "thread-5", "thread-8", "thread-11"]);
+ expect(groups[0]!.tasks.map((task) => task.threadId)).toEqual(["old-room", "thread-1", "thread-4", "thread-7", "thread-10"]);
+ expect(imported).toEqual(plan.threads.map((thread) => thread.threadId));
+ expect(JSON.stringify(plan)).toBe(original);
+});
diff --git a/server/cloud-chat-merge.ts b/server/cloud-chat-merge.ts
index 8a2c9d71d6..4bea827c39 100644
--- a/server/cloud-chat-merge.ts
+++ b/server/cloud-chat-merge.ts
@@ -229,7 +229,12 @@ function carryAttachment(data: string, dataDir: string, name: string): boolean {
/** Owners first, so a conversation's rows never exist without its entry in the list. */
export function applyChatMerge(store: ChatStore, plan: ChatMergePlan): void {
const tasks = new Map<string, Array<TaskRecord | GroupTaskRecord>>();
- for (const thread of plan.threads) if (thread.task) tasks.set(ownerKey(thread.owner), [...tasks.get(ownerKey(thread.owner)) ?? [], thread.task]);
+ for (const thread of plan.threads) {
+ if (!thread.task) continue;
+ const key = ownerKey(thread.owner), added = tasks.get(key);
+ if (added) added.push(thread.task);
+ else tasks.set(key, [thread.task]);
+ }
for (const bot of store.bots) {
const added = tasks.get(`bot:${bot.id}`);
if (added) store.patchBot(bot.id, { tasks: [...bot.tasks ?? [], ...added as TaskRecord[]] });