-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathchat.ts
More file actions
136 lines (115 loc) · 3.77 KB
/
chat.ts
File metadata and controls
136 lines (115 loc) · 3.77 KB
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
import { writeTextFile } from "@tauri-apps/plugin-fs";
import { createCustomPersister } from "tinybase/persisters/with-schemas";
import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas";
import type { ChatMessageStorage } from "@hypr/store";
import { StoreOrMergeableStore } from "../store/shared";
import {
ensureDirsExist,
getChatDir,
getDataDir,
iterateTableRows,
type PersisterMode,
type TablesContent,
} from "./utils";
type ChatGroupData = {
id: string;
user_id: string;
created_at: string;
title: string;
};
type ChatMessageWithId = ChatMessageStorage & { id: string };
type ChatJson = {
chat_group: ChatGroupData;
messages: ChatMessageWithId[];
};
function collectMessagesByChatGroup(
tables: TablesContent | undefined,
): Map<string, { chatGroup: ChatGroupData; messages: ChatMessageWithId[] }> {
const messagesByChatGroup = new Map<
string,
{ chatGroup: ChatGroupData; messages: ChatMessageWithId[] }
>();
const chatGroups = iterateTableRows(tables, "chat_groups");
const chatMessages = iterateTableRows(tables, "chat_messages");
const chatGroupMap = new Map<string, ChatGroupData>();
for (const group of chatGroups) {
chatGroupMap.set(group.id, group);
}
for (const message of chatMessages) {
const chatGroupId = message.chat_group_id;
if (!chatGroupId) continue;
const chatGroup = chatGroupMap.get(chatGroupId);
if (!chatGroup) continue;
const existing = messagesByChatGroup.get(chatGroupId);
if (existing) {
existing.messages.push(message);
} else {
messagesByChatGroup.set(chatGroupId, {
chatGroup,
messages: [message],
});
}
}
return messagesByChatGroup;
}
export function createChatPersister<Schemas extends OptionalSchemas>(
store: MergeableStore<Schemas>,
config: { mode: PersisterMode } = { mode: "save-only" },
) {
const loadFn =
config.mode === "save-only" ? async () => undefined : async () => undefined;
const saveFn =
config.mode === "load-only"
? async () => {}
: async (getContent: () => unknown) => {
const [tables] = getContent() as [TablesContent | undefined, unknown];
const dataDir = await getDataDir();
const messagesByChatGroup = collectMessagesByChatGroup(tables);
if (messagesByChatGroup.size === 0) {
return;
}
const dirs = new Set<string>();
const writeOperations: Array<{ path: string; content: string }> = [];
for (const [
chatGroupId,
{ chatGroup, messages },
] of messagesByChatGroup) {
const chatDir = getChatDir(dataDir, chatGroupId);
dirs.add(chatDir);
const json: ChatJson = {
chat_group: chatGroup,
messages: messages.sort(
(a, b) =>
new Date(a.created_at || 0).getTime() -
new Date(b.created_at || 0).getTime(),
),
};
writeOperations.push({
path: `${chatDir}/_messages.json`,
content: JSON.stringify(json, null, 2),
});
}
try {
await ensureDirsExist(dirs);
} catch (e) {
console.error("Failed to ensure dirs exist:", e);
return;
}
for (const op of writeOperations) {
try {
await writeTextFile(op.path, op.content);
} catch (e) {
console.error(`Failed to write ${op.path}:`, e);
}
}
};
return createCustomPersister(
store,
loadFn,
saveFn,
(listener) => setInterval(listener, 1000),
(interval) => clearInterval(interval),
(error) => console.error("[ChatPersister]:", error),
StoreOrMergeableStore,
);
}