@alexsun-top/opencode-chat-room 0.8.4 → 0.8.5
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/CHANGELOG.md +5 -1
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/cli.ts +1 -1
- package/dist/cli.js +35 -16
- package/package.json +14 -11
- package/packages/chat-room-channeld/README.md +3 -2
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
- package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
- package/plugin.ts +1 -1
- package/server/assets/chat/admin.js +4 -1
- package/server/assets/chat/announcements.js +3 -0
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/doc-preview.js +1 -1
- package/server/assets/chat/editor.js +11 -7
- package/server/assets/chat/events.js +7 -5
- package/server/assets/chat/files.js +5 -1
- package/server/assets/chat/integration-editor.js +2 -0
- package/server/assets/chat/integrations.js +1 -1
- package/server/assets/chat/join-requests.js +1 -1
- package/server/assets/chat/markdown-footnotes.js +8 -7
- package/server/assets/chat/markdown.js +6 -5
- package/server/assets/chat/members.js +3 -3
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +1 -1
- package/server/assets/chat/message-polling.js +2 -2
- package/server/assets/chat/message-window.js +1 -1
- package/server/assets/chat/messages.js +17 -3
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/palette.js +5 -3
- package/server/assets/chat/panels.js +19 -0
- package/server/assets/chat/questions.js +3 -2
- package/server/assets/chat/reminders.js +3 -0
- package/server/assets/chat/replies.js +6 -3
- package/server/assets/chat/resource-format.js +2 -2
- package/server/assets/chat/rooms.js +7 -4
- package/server/assets/chat/settings.js +4 -3
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +30 -11
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +4 -3
- package/server/assets/chat/window-controls.js +3 -2
- package/server/assets/conversation.css +14 -2
- package/server/assets/doc-preview.css +6 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
- package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
- package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
- package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
- package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
- package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
- package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
- package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
- package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
- package/server/sw.js +3 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/agent/agent-receipts.ts +12 -4
- package/src/center/center-client.ts +1 -1
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +107 -99
- package/src/state/pg-worker.mts +166 -0
- package/src/state/store.ts +21 -7
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +426 -423
- package/src/tui/app.mts +500 -0
- package/src/tui/config.mjs +166 -158
- package/src/tui/config.mts +247 -0
- package/src/tui/http.mjs +239 -206
- package/src/tui/http.mts +412 -0
- package/src/tui/i18n.mjs +17 -20
- package/src/tui/i18n.mts +42 -0
- package/src/tui/info-app.mjs +431 -427
- package/src/tui/info-app.mts +500 -0
- package/src/tui/info-content.mjs +205 -199
- package/src/tui/info-content.mts +341 -0
- package/src/tui/join-app.mjs +75 -64
- package/src/tui/join-app.mts +99 -0
- package/src/tui/locales/en.mjs +302 -320
- package/src/tui/locales/en.mts +349 -0
- package/src/tui/locales/zh-CN.mjs +302 -320
- package/src/tui/locales/zh-CN.mts +349 -0
- package/src/tui/main.mjs +219 -213
- package/src/tui/main.mts +255 -0
- package/src/tui/markdown.mjs +128 -272
- package/src/tui/markdown.mts +152 -0
- package/src/tui/mentions.mjs +181 -184
- package/src/tui/mentions.mts +251 -0
- package/src/tui/message-app.mjs +181 -173
- package/src/tui/message-app.mts +213 -0
- package/src/tui/message-view.mjs +90 -111
- package/src/tui/message-view.mts +151 -0
- package/src/tui/messages.mjs +55 -49
- package/src/tui/messages.mts +106 -0
- package/src/tui/palette-app.mjs +364 -344
- package/src/tui/palette-app.mts +388 -0
- package/src/tui/palette.mjs +100 -98
- package/src/tui/palette.mts +176 -0
- package/src/tui/prefs.mjs +56 -63
- package/src/tui/prefs.mts +126 -0
- package/src/tui/reminder-app.mjs +191 -181
- package/src/tui/reminder-app.mts +228 -0
- package/src/tui/room-settings-app.mjs +257 -242
- package/src/tui/room-settings-app.mts +301 -0
- package/src/tui/session.mjs +30 -29
- package/src/tui/session.mts +51 -0
- package/src/tui/settings-app.mjs +151 -138
- package/src/tui/settings-app.mts +179 -0
- package/src/tui/ui-content.mjs +426 -421
- package/src/tui/ui-content.mts +499 -0
- package/src/tui/ui-help.mjs +58 -67
- package/src/tui/ui-help.mts +87 -0
- package/src/tui/ui-mentions.mjs +34 -36
- package/src/tui/ui-mentions.mts +60 -0
- package/src/tui/ui-overlay-view.mjs +259 -267
- package/src/tui/ui-overlay-view.mts +322 -0
- package/src/tui/ui-suggest-view.mjs +54 -57
- package/src/tui/ui-suggest-view.mts +78 -0
- package/src/tui/ui-suggest.mjs +164 -167
- package/src/tui/ui-suggest.mts +248 -0
- package/src/tui/ui-theme.mjs +52 -57
- package/src/tui/ui-theme.mts +98 -0
- package/src/tui/ui-view.mjs +267 -275
- package/src/tui/ui-view.mts +308 -0
- package/src/tui/ui.mjs +372 -359
- package/src/tui/ui.mts +440 -0
- package/src/tui/wire.mjs +215 -0
- package/src/tui/wire.mts +383 -0
package/src/tui/app.mjs
CHANGED
|
@@ -12,436 +12,440 @@ import { canonicalizeMentions } from "./mentions.mjs";
|
|
|
12
12
|
import { mergeMessages, messageStamp, oldestSeq } from "./messages.mjs";
|
|
13
13
|
import { normalizeLanguage, setLanguage, t } from "./i18n.mjs";
|
|
14
14
|
import { normalizeTheme } from "./prefs.mjs";
|
|
15
|
-
|
|
16
15
|
const BACKOFF_MS = [1000, 2000, 4000, 8000, 15000];
|
|
17
|
-
|
|
16
|
+
const FIRST_BACKOFF = BACKOFF_MS[0] ?? 1000;
|
|
17
|
+
const MAX_BACKOFF = BACKOFF_MS.at(-1) ?? 15000;
|
|
18
|
+
const joinStatusOf = (result) => typeof result === "string" ? result
|
|
19
|
+
: result !== null && typeof result === "object" && "status" in result ? result.status : undefined;
|
|
18
20
|
const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
|
|
19
|
-
|
|
20
21
|
const normalizeRoom = (room) => ({
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
id: String(room.id ?? ""),
|
|
23
|
+
name: String(room.name ?? room.id ?? "room"),
|
|
24
|
+
participants: Array.isArray(room.participants) ? room.participants : [],
|
|
25
|
+
joined: room.joined === true,
|
|
25
26
|
});
|
|
26
|
-
|
|
27
27
|
export class ChatApp {
|
|
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
|
-
this.state.pending = null;
|
|
152
|
-
this.state.highlight = null;
|
|
153
|
-
this.state.status = t("status.opening", { name: summary.name });
|
|
154
|
-
this.state.statusKind = "info";
|
|
155
|
-
this.touch();
|
|
156
|
-
if (summary.joined) await this.loadRoom(summary.id);
|
|
157
|
-
else await this.joinRoom(summary.id);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async joinRoom(roomID) {
|
|
161
|
-
try {
|
|
162
|
-
const result = await this.api.join(roomID, this.sessionID, this.participant);
|
|
163
|
-
if (!this.isCurrent(roomID)) return;
|
|
164
|
-
if (result?.pending) {
|
|
165
|
-
const message = String(result.message ?? t("status.joinPending"));
|
|
166
|
-
this.state.pending = { roomId: roomID, message };
|
|
167
|
-
this.state.joined = false;
|
|
168
|
-
this.state.status = message;
|
|
28
|
+
constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, savePrefs = null, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }) {
|
|
29
|
+
this.api = api;
|
|
30
|
+
this.sessionID = sessionID;
|
|
31
|
+
this.participant = participant;
|
|
32
|
+
this.autoPoll = autoPoll;
|
|
33
|
+
this.schedule = schedule;
|
|
34
|
+
this.savePrefs = typeof savePrefs === "function" ? savePrefs : null;
|
|
35
|
+
this.settings = { ...settings };
|
|
36
|
+
this.token = this.settings.token ?? "";
|
|
37
|
+
delete this.settings.token;
|
|
38
|
+
this.prefs = {
|
|
39
|
+
language: normalizeLanguage(this.settings.language),
|
|
40
|
+
theme: normalizeTheme(this.settings.theme),
|
|
41
|
+
participant: this.settings.participant ?? participant,
|
|
42
|
+
url: this.settings.server ?? "",
|
|
43
|
+
};
|
|
44
|
+
setLanguage(this.prefs.language);
|
|
45
|
+
this.downloadDir = downloadDir;
|
|
46
|
+
this.closed = false;
|
|
47
|
+
this.pollToken = 0;
|
|
48
|
+
this.timers = new Set();
|
|
49
|
+
this.pollAbort = null;
|
|
50
|
+
this.cache = new Map();
|
|
51
|
+
this.historyCursor = new Map();
|
|
52
|
+
this.historyMore = new Map();
|
|
53
|
+
this.historyPages = new Map();
|
|
54
|
+
this.historyInflight = new Set();
|
|
55
|
+
this.listeners = new Set();
|
|
56
|
+
this.state = {
|
|
57
|
+
host,
|
|
58
|
+
participant,
|
|
59
|
+
theme: this.prefs.theme,
|
|
60
|
+
language: this.prefs.language,
|
|
61
|
+
connection: "connecting",
|
|
62
|
+
rooms: [],
|
|
63
|
+
selected: 0,
|
|
64
|
+
current: null,
|
|
65
|
+
joined: false,
|
|
66
|
+
messages: [],
|
|
67
|
+
pending: null,
|
|
68
|
+
focus: "rooms",
|
|
69
|
+
draft: "",
|
|
70
|
+
highlight: null,
|
|
71
|
+
help: false,
|
|
72
|
+
status: "",
|
|
73
|
+
statusKind: "info",
|
|
74
|
+
palette: null,
|
|
75
|
+
quit: false,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
subscribe(listener) {
|
|
79
|
+
this.listeners.add(listener);
|
|
80
|
+
return () => this.listeners.delete(listener);
|
|
81
|
+
}
|
|
82
|
+
touch() {
|
|
83
|
+
for (const listener of this.listeners)
|
|
84
|
+
listener(this.state);
|
|
85
|
+
}
|
|
86
|
+
later(fn, ms) {
|
|
87
|
+
if (this.closed)
|
|
88
|
+
return undefined;
|
|
89
|
+
let timer;
|
|
90
|
+
const run = () => {
|
|
91
|
+
if (timer !== undefined)
|
|
92
|
+
this.timers.delete(timer);
|
|
93
|
+
return fn();
|
|
94
|
+
};
|
|
95
|
+
timer = this.schedule(run, ms);
|
|
96
|
+
if (timer !== undefined)
|
|
97
|
+
this.timers.add(timer);
|
|
98
|
+
return timer;
|
|
99
|
+
}
|
|
100
|
+
setStatus(text, kind = "info") {
|
|
101
|
+
this.state.status = text;
|
|
102
|
+
this.state.statusKind = kind;
|
|
103
|
+
this.touch();
|
|
104
|
+
}
|
|
105
|
+
async init() {
|
|
106
|
+
await this.refreshRooms(true);
|
|
107
|
+
}
|
|
108
|
+
async refreshRooms(quiet = false) {
|
|
109
|
+
try {
|
|
110
|
+
const list = await this.api.listRooms(this.sessionID);
|
|
111
|
+
const rooms = (Array.isArray(list) ? list : []).map(normalizeRoom);
|
|
112
|
+
const selectedID = this.state.rooms[this.state.selected]?.id;
|
|
113
|
+
this.state.rooms = rooms;
|
|
114
|
+
const found = selectedID ? rooms.findIndex((room) => room.id === selectedID) : -1;
|
|
115
|
+
this.state.selected = found >= 0 ? found : Math.min(this.state.selected, Math.max(0, rooms.length - 1));
|
|
116
|
+
this.state.connection = "online";
|
|
117
|
+
if (!quiet) {
|
|
118
|
+
this.state.status = t("status.refreshed");
|
|
119
|
+
this.state.statusKind = "info";
|
|
120
|
+
}
|
|
121
|
+
this.touch();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
this.state.connection = "offline";
|
|
126
|
+
this.state.status = t("status.refreshFailed", { error: errorText(error) });
|
|
127
|
+
this.state.statusKind = "error";
|
|
128
|
+
this.touch();
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
isCurrent(roomID) {
|
|
133
|
+
return this.state.current?.id === roomID;
|
|
134
|
+
}
|
|
135
|
+
async openSelected() {
|
|
136
|
+
return this.openRoom(this.state.selected);
|
|
137
|
+
}
|
|
138
|
+
async openRoom(indexOrRoom) {
|
|
139
|
+
const index = typeof indexOrRoom === "number" ? indexOrRoom : this.state.rooms.indexOf(indexOrRoom);
|
|
140
|
+
const summary = this.state.rooms[index];
|
|
141
|
+
if (!summary)
|
|
142
|
+
return;
|
|
143
|
+
this.state.selected = index;
|
|
144
|
+
this.stopPolling();
|
|
145
|
+
this.state.current = { id: summary.id, name: summary.name, participants: summary.participants };
|
|
146
|
+
this.state.joined = summary.joined;
|
|
147
|
+
this.state.messages = [];
|
|
148
|
+
this.state.pending = null;
|
|
149
|
+
this.state.highlight = null;
|
|
150
|
+
this.state.status = t("status.opening", { name: summary.name });
|
|
169
151
|
this.state.statusKind = "info";
|
|
170
152
|
this.touch();
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
153
|
+
if (summary.joined)
|
|
154
|
+
await this.loadRoom(summary.id);
|
|
155
|
+
else
|
|
156
|
+
await this.joinRoom(summary.id);
|
|
157
|
+
}
|
|
158
|
+
async joinRoom(roomID) {
|
|
159
|
+
try {
|
|
160
|
+
const result = await this.api.join(roomID, this.sessionID, this.participant);
|
|
161
|
+
if (!this.isCurrent(roomID))
|
|
162
|
+
return;
|
|
163
|
+
if (result.pending) {
|
|
164
|
+
const message = String(result.message ?? t("status.joinPending"));
|
|
165
|
+
this.state.pending = { roomId: roomID, message };
|
|
166
|
+
this.state.joined = false;
|
|
167
|
+
this.state.status = message;
|
|
168
|
+
this.state.statusKind = "info";
|
|
169
|
+
this.touch();
|
|
170
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.enterRoom(result.room, roomID, true);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (this.isCurrent(roomID))
|
|
177
|
+
this.setStatus(t("status.joinFailed", { error: errorText(error) }), "error");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
async checkJoin(roomID) {
|
|
181
|
+
if (this.closed || !this.isCurrent(roomID) || this.state.pending?.roomId !== roomID)
|
|
182
|
+
return;
|
|
183
|
+
try {
|
|
184
|
+
const status = joinStatusOf(await this.api.joinStatus(roomID, this.sessionID));
|
|
185
|
+
if (!this.isCurrent(roomID) || this.state.pending?.roomId !== roomID)
|
|
186
|
+
return;
|
|
187
|
+
if (status === "joined") {
|
|
188
|
+
this.state.pending = null;
|
|
189
|
+
this.setStatus(t("status.joinApproved"), "success");
|
|
190
|
+
await this.loadRoom(roomID);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (status === "pending") {
|
|
194
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.state.pending = null;
|
|
198
|
+
this.setStatus(t("status.joinStale"), "error");
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async loadRoom(roomID) {
|
|
205
|
+
try {
|
|
206
|
+
const room = await this.api.getRoom(roomID);
|
|
207
|
+
if (!this.isCurrent(roomID) || !room)
|
|
208
|
+
return;
|
|
209
|
+
if (room.joined === false) {
|
|
210
|
+
this.notJoined(roomID, t("status.notMember"));
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
this.enterRoom(room, roomID, false);
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (this.isCurrent(roomID))
|
|
217
|
+
this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
enterRoom(room, roomID, joinedNow) {
|
|
221
|
+
if (!room || typeof room !== "object") {
|
|
222
|
+
this.setStatus(t("status.roomMissing"), "error");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const summary = this.state.rooms.find((item) => item.id === roomID);
|
|
226
|
+
if (summary) {
|
|
227
|
+
summary.joined = true;
|
|
228
|
+
summary.name = String(room.name ?? summary.name);
|
|
229
|
+
if (Array.isArray(room.participants))
|
|
230
|
+
summary.participants = room.participants;
|
|
231
|
+
}
|
|
232
|
+
const cache = this.historyList(roomID);
|
|
233
|
+
mergeMessages(cache, room.messages);
|
|
234
|
+
this.state.current = {
|
|
235
|
+
id: roomID,
|
|
236
|
+
name: String(room.name ?? summary?.name ?? roomID),
|
|
237
|
+
participants: Array.isArray(room.participants) ? room.participants : summary?.participants ?? [],
|
|
238
|
+
};
|
|
239
|
+
this.state.joined = true;
|
|
187
240
|
this.state.pending = null;
|
|
188
|
-
this.
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
if (status === "pending") {
|
|
193
|
-
this.later(() => this.checkJoin(roomID), 1500);
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
this.state.pending = null;
|
|
197
|
-
this.setStatus(t("status.joinStale"), "error");
|
|
198
|
-
} catch {
|
|
199
|
-
this.later(() => this.checkJoin(roomID), 1500);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async loadRoom(roomID) {
|
|
204
|
-
try {
|
|
205
|
-
const room = await this.api.getRoom(roomID);
|
|
206
|
-
if (!this.isCurrent(roomID) || !room) return;
|
|
207
|
-
if (room.joined === false) {
|
|
208
|
-
this.notJoined(roomID, t("status.notMember"));
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
this.enterRoom(room, roomID, false);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
if (this.isCurrent(roomID)) this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
enterRoom(room, roomID, joinedNow) {
|
|
218
|
-
if (!room || typeof room !== "object") {
|
|
219
|
-
this.setStatus(t("status.roomMissing"), "error");
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const summary = this.state.rooms.find((item) => item.id === roomID);
|
|
223
|
-
if (summary) {
|
|
224
|
-
summary.joined = true;
|
|
225
|
-
summary.name = String(room.name ?? summary.name);
|
|
226
|
-
if (Array.isArray(room.participants)) summary.participants = room.participants;
|
|
227
|
-
}
|
|
228
|
-
const cache = this.historyList(roomID);
|
|
229
|
-
mergeMessages(cache, room.messages);
|
|
230
|
-
this.state.current = {
|
|
231
|
-
id: roomID,
|
|
232
|
-
name: String(room.name ?? summary?.name ?? roomID),
|
|
233
|
-
participants: Array.isArray(room.participants) ? room.participants : summary?.participants ?? [],
|
|
234
|
-
};
|
|
235
|
-
this.state.joined = true;
|
|
236
|
-
this.state.pending = null;
|
|
237
|
-
this.state.messages = cache;
|
|
238
|
-
this.state.connection = "online";
|
|
239
|
-
this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
|
|
240
|
-
this.state.statusKind = "success";
|
|
241
|
-
if (typeof room.hasMore === "boolean") this.historyMore.set(roomID, room.hasMore);
|
|
242
|
-
const oldest = oldestSeq(cache);
|
|
243
|
-
if (oldest !== undefined) this.historyCursor.set(roomID, oldest);
|
|
244
|
-
this.touch();
|
|
245
|
-
this.markRead();
|
|
246
|
-
if (this.autoPoll) this.startPolling();
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
historyList(roomID) {
|
|
250
|
-
let list = this.cache.get(roomID);
|
|
251
|
-
if (!list) {
|
|
252
|
-
list = [];
|
|
253
|
-
this.cache.set(roomID, list);
|
|
254
|
-
}
|
|
255
|
-
return list;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
merge(items) {
|
|
259
|
-
mergeMessages(this.state.messages, items);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
lastTs() {
|
|
263
|
-
return this.state.messages.reduce((max, message) => Math.max(max, messageStamp(message)), 0);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async pollOnce() {
|
|
267
|
-
const room = this.state.current;
|
|
268
|
-
if (!room || !this.state.joined) return false;
|
|
269
|
-
const roomID = room.id;
|
|
270
|
-
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
271
|
-
this.pollAbort = controller;
|
|
272
|
-
try {
|
|
273
|
-
const result = await this.api.poll(
|
|
274
|
-
roomID,
|
|
275
|
-
this.sessionID,
|
|
276
|
-
this.lastTs(),
|
|
277
|
-
controller ? { signal: controller.signal } : undefined,
|
|
278
|
-
);
|
|
279
|
-
if (!this.isCurrent(roomID)) return false;
|
|
280
|
-
if (!result || result.member === false) {
|
|
281
|
-
this.notJoined(roomID, t("status.memberLost"));
|
|
282
|
-
return false;
|
|
283
|
-
}
|
|
284
|
-
const wasOffline = this.state.connection !== "online";
|
|
285
|
-
this.merge(result.items);
|
|
286
|
-
this.state.connection = "online";
|
|
287
|
-
if (wasOffline) {
|
|
288
|
-
this.state.status = t("status.reconnected");
|
|
241
|
+
this.state.messages = cache;
|
|
242
|
+
this.state.connection = "online";
|
|
243
|
+
this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
|
|
289
244
|
this.state.statusKind = "success";
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
245
|
+
if (typeof room.hasMore === "boolean")
|
|
246
|
+
this.historyMore.set(roomID, room.hasMore);
|
|
247
|
+
const oldest = oldestSeq(cache);
|
|
248
|
+
if (oldest !== undefined)
|
|
249
|
+
this.historyCursor.set(roomID, oldest);
|
|
250
|
+
this.touch();
|
|
251
|
+
this.markRead();
|
|
252
|
+
if (this.autoPoll)
|
|
253
|
+
this.startPolling();
|
|
254
|
+
}
|
|
255
|
+
historyList(roomID) {
|
|
256
|
+
let list = this.cache.get(roomID);
|
|
257
|
+
if (!list) {
|
|
258
|
+
list = [];
|
|
259
|
+
this.cache.set(roomID, list);
|
|
260
|
+
}
|
|
261
|
+
return list;
|
|
262
|
+
}
|
|
263
|
+
merge(items) {
|
|
264
|
+
mergeMessages(this.state.messages, items);
|
|
265
|
+
}
|
|
266
|
+
lastTs() {
|
|
267
|
+
return this.state.messages.reduce((max, message) => Math.max(max, messageStamp(message)), 0);
|
|
268
|
+
}
|
|
269
|
+
async pollOnce() {
|
|
270
|
+
const room = this.state.current;
|
|
271
|
+
if (!room || !this.state.joined)
|
|
272
|
+
return false;
|
|
273
|
+
const roomID = room.id;
|
|
274
|
+
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
275
|
+
this.pollAbort = controller;
|
|
276
|
+
try {
|
|
277
|
+
const result = await this.api.poll(roomID, this.sessionID, this.lastTs(), controller ? { signal: controller.signal } : undefined);
|
|
278
|
+
if (!this.isCurrent(roomID))
|
|
279
|
+
return false;
|
|
280
|
+
if (result === null)
|
|
281
|
+
throw new Error("Invalid inbox response");
|
|
282
|
+
if (result.member === false) {
|
|
283
|
+
this.notJoined(roomID, t("status.memberLost"));
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
const wasOffline = this.state.connection !== "online";
|
|
287
|
+
this.merge(result.items);
|
|
288
|
+
this.state.connection = "online";
|
|
289
|
+
if (wasOffline) {
|
|
290
|
+
this.state.status = t("status.reconnected");
|
|
291
|
+
this.state.statusKind = "success";
|
|
292
|
+
}
|
|
293
|
+
this.touch();
|
|
294
|
+
this.markRead();
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
if (controller?.signal.aborted)
|
|
299
|
+
return false;
|
|
300
|
+
this.state.connection = "reconnecting";
|
|
301
|
+
this.state.status = t("status.connectionLost", { error: errorText(error) });
|
|
302
|
+
this.state.statusKind = "error";
|
|
303
|
+
this.touch();
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
if (this.pollAbort === controller)
|
|
308
|
+
this.pollAbort = null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
startPolling() {
|
|
312
|
+
const token = ++this.pollToken;
|
|
313
|
+
let delay = 0;
|
|
314
|
+
const step = async () => {
|
|
315
|
+
if (this.closed || token !== this.pollToken || !this.state.joined)
|
|
316
|
+
return;
|
|
317
|
+
const ok = await this.pollOnce();
|
|
318
|
+
if (this.closed || token !== this.pollToken || !this.state.joined)
|
|
319
|
+
return;
|
|
320
|
+
delay = ok ? 0 : Math.min(delay ? delay * 2 : FIRST_BACKOFF, MAX_BACKOFF);
|
|
321
|
+
this.later(step, delay);
|
|
322
|
+
};
|
|
323
|
+
void step();
|
|
324
|
+
}
|
|
325
|
+
stopPolling() {
|
|
326
|
+
this.pollToken += 1;
|
|
327
|
+
this.pollAbort?.abort();
|
|
328
|
+
}
|
|
329
|
+
notJoined(roomID, message) {
|
|
330
|
+
if (!this.isCurrent(roomID))
|
|
331
|
+
return;
|
|
332
|
+
this.state.joined = false;
|
|
333
|
+
const summary = this.state.rooms.find((room) => room.id === roomID);
|
|
334
|
+
if (summary)
|
|
335
|
+
summary.joined = false;
|
|
336
|
+
this.setStatus(message, "error");
|
|
337
|
+
}
|
|
338
|
+
markRead() {
|
|
339
|
+
const room = this.state.current;
|
|
340
|
+
const newest = this.lastTs();
|
|
341
|
+
if (!room || !newest)
|
|
342
|
+
return;
|
|
343
|
+
try {
|
|
344
|
+
const result = this.api.markRead?.(room.id, this.sessionID, newest);
|
|
345
|
+
if (result && typeof result.catch === "function")
|
|
346
|
+
result.catch(() => { });
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
// A read receipt failure must not interrupt the UI.
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async send() {
|
|
353
|
+
const raw = String(this.state.draft ?? "").trim();
|
|
354
|
+
if (!raw)
|
|
355
|
+
return;
|
|
356
|
+
const room = this.state.current;
|
|
357
|
+
if (!room || !this.state.joined) {
|
|
358
|
+
this.setStatus(t("status.joinBeforeSend"), "error");
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const text = canonicalizeMentions(raw, room.participants);
|
|
362
|
+
// Reuse the attempt's id when the same failed text is sent again, so a lost
|
|
363
|
+
// response cannot produce two messages.
|
|
364
|
+
const attempt = this.pendingSend && this.pendingSend.text === raw
|
|
365
|
+
? this.pendingSend
|
|
366
|
+
: { text: raw, clientMessageID: globalThis.crypto.randomUUID() };
|
|
367
|
+
this.pendingSend = attempt;
|
|
368
|
+
this.state.draft = "";
|
|
369
|
+
this.touch();
|
|
370
|
+
try {
|
|
371
|
+
const result = await this.api.send(room.id, this.sessionID, text, { clientMessageID: attempt.clientMessageID });
|
|
372
|
+
if (!this.isCurrent(room.id))
|
|
373
|
+
return;
|
|
374
|
+
this.pendingSend = null;
|
|
375
|
+
if (Array.isArray(result?.messages))
|
|
376
|
+
this.merge(result.messages);
|
|
377
|
+
else if (result?.id)
|
|
378
|
+
this.merge([result]);
|
|
379
|
+
this.state.highlight = null;
|
|
380
|
+
this.state.connection = "online";
|
|
381
|
+
this.setStatus(t("status.sent"), "success");
|
|
382
|
+
this.markRead();
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
// Keep text typed while the request was in flight; dropping it loses work.
|
|
386
|
+
this.state.draft = this.state.draft ? `${raw}\n${this.state.draft}` : raw;
|
|
387
|
+
this.setStatus(t("status.sendFailed", { error: errorText(error) }), "error");
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
setDraft(text) {
|
|
391
|
+
this.state.draft = String(text ?? "");
|
|
392
|
+
}
|
|
393
|
+
appendDraft(text) {
|
|
394
|
+
this.state.draft += String(text ?? "");
|
|
395
|
+
this.state.focus = "composer";
|
|
396
|
+
this.state.help = false;
|
|
397
|
+
this.touch();
|
|
398
|
+
}
|
|
399
|
+
moveSelection(delta) {
|
|
400
|
+
if (!this.state.rooms.length)
|
|
401
|
+
return;
|
|
402
|
+
this.state.selected = Math.min(this.state.rooms.length - 1, Math.max(0, this.state.selected + delta));
|
|
403
|
+
this.state.highlight = null;
|
|
404
|
+
this.touch();
|
|
405
|
+
}
|
|
406
|
+
selectRoom(index) {
|
|
407
|
+
if (!this.state.rooms[index])
|
|
408
|
+
return;
|
|
409
|
+
this.state.selected = index;
|
|
410
|
+
this.state.highlight = null;
|
|
411
|
+
this.touch();
|
|
412
|
+
}
|
|
413
|
+
toggleFocus() {
|
|
414
|
+
this.state.focus = this.state.focus === "rooms" ? "composer" : "rooms";
|
|
415
|
+
if (this.state.focus === "composer")
|
|
416
|
+
this.state.help = false;
|
|
417
|
+
this.touch();
|
|
418
|
+
}
|
|
419
|
+
toggleHelp() {
|
|
420
|
+
this.state.help = !this.state.help;
|
|
421
|
+
this.touch();
|
|
422
|
+
}
|
|
423
|
+
clearOrBlur() {
|
|
424
|
+
this.state.draft = "";
|
|
425
|
+
this.state.focus = "rooms";
|
|
426
|
+
this.state.highlight = null;
|
|
427
|
+
this.touch();
|
|
428
|
+
}
|
|
429
|
+
clearHighlight() {
|
|
430
|
+
if (!this.state.highlight)
|
|
431
|
+
return;
|
|
432
|
+
this.state.highlight = null;
|
|
433
|
+
this.touch();
|
|
434
|
+
}
|
|
435
|
+
quit() {
|
|
436
|
+
this.state.quit = true;
|
|
437
|
+
this.close();
|
|
438
|
+
this.touch();
|
|
439
|
+
}
|
|
440
|
+
close() {
|
|
441
|
+
this.closed = true;
|
|
442
|
+
this.pollToken += 1;
|
|
443
|
+
this.pollAbort?.abort();
|
|
444
|
+
for (const timer of this.timers)
|
|
445
|
+
clearTimeout(timer);
|
|
446
|
+
this.timers.clear();
|
|
447
|
+
}
|
|
443
448
|
}
|
|
444
|
-
|
|
445
449
|
attachPalette(ChatApp);
|
|
446
450
|
attachInfo(ChatApp);
|
|
447
451
|
attachSettings(ChatApp);
|
|
@@ -449,7 +453,6 @@ attachReminders(ChatApp);
|
|
|
449
453
|
attachRoomSettings(ChatApp);
|
|
450
454
|
attachMessageActions(ChatApp);
|
|
451
455
|
attachJoinRequests(ChatApp);
|
|
452
|
-
|
|
453
456
|
export function createApp(options) {
|
|
454
|
-
|
|
455
|
-
}
|
|
457
|
+
return new ChatApp(options);
|
|
458
|
+
}
|