@alexsun-top/opencode-chat-room 0.8.4 → 0.8.6
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 +17 -3
- package/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/cli.ts +20 -1
- package/dist/cli.js +3785 -1462
- package/package.json +19 -11
- package/packages/chat-room-channeld/README.md +16 -10
- package/packages/chat-room-channeld/src/delivery.ts +0 -4
- package/packages/chat-room-channeld/src/host.ts +48 -37
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/database.ts +141 -49
- 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 +148 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +201 -0
- package/packages/chat-room-channeld/src/state-rows.ts +132 -0
- package/packages/chat-room-channeld/src/state.ts +129 -157
- package/plugin.ts +21 -5
- package/server/app.ts +9 -0
- package/server/assets/base.css +22 -2
- package/server/assets/chat/admin.js +17 -1
- package/server/assets/chat/announcements.js +79 -34
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/confirm-dialog.js +3 -0
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/create-room-dialog.js +7 -0
- package/server/assets/chat/diagram-viewer.js +4 -8
- package/server/assets/chat/doc-preview.js +71 -31
- package/server/assets/chat/editor.js +49 -10
- package/server/assets/chat/events.js +13 -6
- package/server/assets/chat/files.js +33 -27
- package/server/assets/chat/history-loader.js +31 -15
- package/server/assets/chat/integration-editor.js +27 -1
- package/server/assets/chat/integrations.js +108 -16
- package/server/assets/chat/join-requests.js +40 -2
- package/server/assets/chat/locales/de.js +5 -3
- package/server/assets/chat/locales/es.js +5 -3
- package/server/assets/chat/locales/fr.js +5 -3
- package/server/assets/chat/locales/it.js +5 -3
- package/server/assets/chat/locales/ja.js +5 -3
- package/server/assets/chat/locales/ko.js +5 -3
- package/server/assets/chat/markdown-footnotes.js +27 -15
- package/server/assets/chat/markdown.js +6 -12
- package/server/assets/chat/members.js +34 -22
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +9 -3
- package/server/assets/chat/message-polling.js +10 -4
- package/server/assets/chat/message-window.js +58 -1
- package/server/assets/chat/messages.js +90 -37
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/operator.js +624 -0
- package/server/assets/chat/palette.js +7 -10
- package/server/assets/chat/panels.js +42 -0
- package/server/assets/chat/questions.js +75 -58
- package/server/assets/chat/recall.js +14 -5
- package/server/assets/chat/receipts.js +63 -7
- package/server/assets/chat/refresh-timing.js +6 -0
- package/server/assets/chat/reminders.js +47 -13
- package/server/assets/chat/replies.js +39 -25
- package/server/assets/chat/resource-format.js +6 -3
- package/server/assets/chat/room-list.js +202 -3
- package/server/assets/chat/room-watcher.js +3 -1
- package/server/assets/chat/rooms.js +29 -7
- package/server/assets/chat/settings.js +37 -6
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +51 -18
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +37 -14
- package/server/assets/chat/window-controls.js +25 -15
- package/server/assets/code-highlight.css +0 -13
- package/server/assets/conversation.css +45 -5
- package/server/assets/doc-preview.css +9 -0
- package/server/assets/files.css +4 -0
- package/server/assets/monaco/integration-rules.js +1 -1
- 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/assets/responsive.css +12 -2
- package/server/assets/settings.css +3 -1
- package/server/assets/tasks.css +6 -1
- package/server/assets/window-controls.css +3 -1
- package/server/channels.ts +3 -2
- package/server/chat.html +7 -2
- package/server/inbox.ts +3 -1
- package/server/join-requests.ts +8 -1
- package/server/member-routes.ts +16 -2
- package/server/newrooms.ts +2 -13
- package/server/operator-auth.ts +21 -0
- package/server/operator-routes.ts +150 -0
- package/server/operator-state.ts +64 -0
- package/server/operator.css +139 -0
- package/server/room-owner.ts +17 -0
- package/server/routes/inbox.ts +15 -5
- package/server/routes/pages.ts +12 -2
- package/server/routes/room-actions.ts +4 -3
- package/server/sw.js +3 -1
- package/server/system.html +109 -0
- package/server/wake.ts +1 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/acp/client.ts +254 -0
- package/src/acp/protocol.ts +300 -0
- package/src/acp/watch.ts +498 -0
- package/src/agent/agent-receipts.ts +98 -8
- package/src/center/center-client.ts +6 -1
- package/src/center/center-inbox.ts +54 -7
- package/src/center/center-wake.ts +57 -3
- package/src/center/forward-client.ts +48 -0
- package/src/center/mentions.ts +16 -3
- package/src/center/notification-delay.ts +139 -0
- package/src/center/queue.ts +3 -0
- package/src/config/file-config.ts +25 -0
- package/src/mcp/server.ts +389 -0
- package/src/protocol/config.ts +7 -0
- package/src/runtime/plugin-notifications.ts +43 -9
- package/src/runtime/v2-plugin.ts +14 -3
- package/src/runtime/v2-receiver.ts +10 -2
- package/src/runtime/watch.ts +5 -1
- package/src/state/pg-database.ts +133 -49
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +148 -99
- package/src/state/pg-worker.mts +201 -0
- package/src/state/receiver-state.ts +47 -0
- package/src/state/sqlite-database.ts +6 -2
- package/src/state/sqlite-members.ts +48 -4
- package/src/state/sqlite-messages.ts +25 -12
- package/src/state/sqlite-schema.ts +4 -1
- package/src/state/sqlite-state.ts +1 -1
- package/src/state/store.ts +31 -11
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +473 -423
- package/src/tui/app.mts +543 -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 +275 -242
- package/src/tui/room-settings-app.mts +318 -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/web/integration-rules-editor.ts +25 -2
package/src/tui/app.mts
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
// Headless chat state for the TUI. Kept free of terminal and palette code so
|
|
2
|
+
// the reducer can be tested against a fake api. Palette methods are attached
|
|
3
|
+
// from palette-app.mjs.
|
|
4
|
+
import { attachPalette } from "./palette-app.mjs";
|
|
5
|
+
import { attachInfo } from "./info-app.mjs";
|
|
6
|
+
import { attachSettings } from "./settings-app.mjs";
|
|
7
|
+
import { attachReminders } from "./reminder-app.mjs";
|
|
8
|
+
import { attachRoomSettings } from "./room-settings-app.mjs";
|
|
9
|
+
import { attachMessageActions } from "./message-app.mjs";
|
|
10
|
+
import { attachJoinRequests } from "./join-app.mjs";
|
|
11
|
+
import { canonicalizeMentions } from "./mentions.mjs";
|
|
12
|
+
import { mergeMessages, messageStamp, oldestSeq } from "./messages.mjs";
|
|
13
|
+
import type { ChatMessage } from "./messages.mjs";
|
|
14
|
+
import { normalizeLanguage, setLanguage, t } from "./i18n.mjs";
|
|
15
|
+
import { normalizeTheme } from "./prefs.mjs";
|
|
16
|
+
import type { ChatApi } from "./http.mjs";
|
|
17
|
+
import type { RoomWire } from "./wire.mjs";
|
|
18
|
+
import type {
|
|
19
|
+
AppListener,
|
|
20
|
+
AppPrefs,
|
|
21
|
+
AppSettings,
|
|
22
|
+
ChatAppOptions,
|
|
23
|
+
ChatState,
|
|
24
|
+
PendingSend,
|
|
25
|
+
RoomSummary,
|
|
26
|
+
SavePrefs,
|
|
27
|
+
Schedule,
|
|
28
|
+
StatusKind,
|
|
29
|
+
TimerHandle,
|
|
30
|
+
} from "./app-types.mjs";
|
|
31
|
+
|
|
32
|
+
const BACKOFF_MS = [1000, 2000, 4000, 8000, 15000];
|
|
33
|
+
const FIRST_BACKOFF = BACKOFF_MS[0] ?? 1000;
|
|
34
|
+
const MAX_BACKOFF = BACKOFF_MS.at(-1) ?? 15000;
|
|
35
|
+
|
|
36
|
+
const joinStatusOf = (result: unknown): unknown =>
|
|
37
|
+
typeof result === "string" ? result
|
|
38
|
+
: result !== null && typeof result === "object" && "status" in result ? result.status : undefined;
|
|
39
|
+
|
|
40
|
+
const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error ?? "unknown error"));
|
|
41
|
+
|
|
42
|
+
const normalizeRoom = (room: RoomWire): RoomSummary => ({
|
|
43
|
+
id: String(room.id ?? ""),
|
|
44
|
+
name: String(room.name ?? room.id ?? "room"),
|
|
45
|
+
participants: Array.isArray(room.participants) ? room.participants : [],
|
|
46
|
+
joined: room.joined === true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export class ChatApp {
|
|
50
|
+
declare api: ChatApi;
|
|
51
|
+
declare sessionID: string;
|
|
52
|
+
declare participant: string;
|
|
53
|
+
declare autoPoll: boolean;
|
|
54
|
+
declare schedule: Schedule;
|
|
55
|
+
declare savePrefs: SavePrefs | null;
|
|
56
|
+
declare settings: AppSettings;
|
|
57
|
+
declare token: string;
|
|
58
|
+
declare prefs: AppPrefs;
|
|
59
|
+
declare downloadDir: string;
|
|
60
|
+
declare closed: boolean;
|
|
61
|
+
declare pollToken: number;
|
|
62
|
+
declare timers: Set<TimerHandle>;
|
|
63
|
+
declare pollAbort: AbortController | null;
|
|
64
|
+
declare cache: Map<string, ChatMessage[]>;
|
|
65
|
+
declare historyCursor: Map<string, number>;
|
|
66
|
+
declare historyMore: Map<string, boolean>;
|
|
67
|
+
declare historyPages: Map<string, number>;
|
|
68
|
+
declare historyInflight: Set<string>;
|
|
69
|
+
declare listeners: Set<AppListener>;
|
|
70
|
+
declare state: ChatState;
|
|
71
|
+
declare drafts: Map<string, string>;
|
|
72
|
+
declare pendingSends: Map<string, PendingSend>;
|
|
73
|
+
declare inflightSends: Set<string>;
|
|
74
|
+
|
|
75
|
+
constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, savePrefs = null, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }: ChatAppOptions) {
|
|
76
|
+
this.api = api;
|
|
77
|
+
this.sessionID = sessionID;
|
|
78
|
+
this.participant = participant;
|
|
79
|
+
this.autoPoll = autoPoll;
|
|
80
|
+
this.schedule = schedule;
|
|
81
|
+
this.savePrefs = typeof savePrefs === "function" ? savePrefs : null;
|
|
82
|
+
this.settings = { ...settings };
|
|
83
|
+
this.token = this.settings.token ?? "";
|
|
84
|
+
delete this.settings.token;
|
|
85
|
+
this.prefs = {
|
|
86
|
+
language: normalizeLanguage(this.settings.language),
|
|
87
|
+
theme: normalizeTheme(this.settings.theme),
|
|
88
|
+
participant: this.settings.participant ?? participant,
|
|
89
|
+
url: this.settings.server ?? "",
|
|
90
|
+
};
|
|
91
|
+
setLanguage(this.prefs.language);
|
|
92
|
+
this.downloadDir = downloadDir;
|
|
93
|
+
this.closed = false;
|
|
94
|
+
this.pollToken = 0;
|
|
95
|
+
this.timers = new Set();
|
|
96
|
+
this.pollAbort = null;
|
|
97
|
+
this.cache = new Map();
|
|
98
|
+
this.historyCursor = new Map();
|
|
99
|
+
this.historyMore = new Map();
|
|
100
|
+
this.historyPages = new Map();
|
|
101
|
+
this.historyInflight = new Set();
|
|
102
|
+
this.listeners = new Set();
|
|
103
|
+
this.drafts = new Map();
|
|
104
|
+
this.pendingSends = new Map();
|
|
105
|
+
this.inflightSends = new Set();
|
|
106
|
+
this.state = {
|
|
107
|
+
host,
|
|
108
|
+
participant,
|
|
109
|
+
theme: this.prefs.theme,
|
|
110
|
+
language: this.prefs.language,
|
|
111
|
+
connection: "connecting",
|
|
112
|
+
rooms: [],
|
|
113
|
+
selected: 0,
|
|
114
|
+
current: null,
|
|
115
|
+
joined: false,
|
|
116
|
+
messages: [],
|
|
117
|
+
pending: null,
|
|
118
|
+
focus: "rooms",
|
|
119
|
+
draft: "",
|
|
120
|
+
highlight: null,
|
|
121
|
+
help: false,
|
|
122
|
+
status: "",
|
|
123
|
+
statusKind: "info",
|
|
124
|
+
palette: null,
|
|
125
|
+
quit: false,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
subscribe(listener: AppListener) {
|
|
130
|
+
this.listeners.add(listener);
|
|
131
|
+
return () => this.listeners.delete(listener);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
touch() {
|
|
135
|
+
for (const listener of this.listeners) listener(this.state);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
later(fn: () => unknown, ms: number) {
|
|
139
|
+
if (this.closed) return undefined;
|
|
140
|
+
let timer: TimerHandle | undefined;
|
|
141
|
+
const run = () => {
|
|
142
|
+
if (timer !== undefined) this.timers.delete(timer);
|
|
143
|
+
return fn();
|
|
144
|
+
};
|
|
145
|
+
timer = this.schedule(run, ms);
|
|
146
|
+
if (timer !== undefined) this.timers.add(timer);
|
|
147
|
+
return timer;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
setStatus(text: string, kind: StatusKind = "info") {
|
|
151
|
+
this.state.status = text;
|
|
152
|
+
this.state.statusKind = kind;
|
|
153
|
+
this.touch();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async init() {
|
|
157
|
+
await this.refreshRooms(true);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async refreshRooms(quiet = false) {
|
|
161
|
+
try {
|
|
162
|
+
const list = await this.api.listRooms(this.sessionID);
|
|
163
|
+
const rooms = (Array.isArray(list) ? list : []).map(normalizeRoom);
|
|
164
|
+
const selectedID = this.state.rooms[this.state.selected]?.id;
|
|
165
|
+
this.state.rooms = rooms;
|
|
166
|
+
const found = selectedID ? rooms.findIndex((room) => room.id === selectedID) : -1;
|
|
167
|
+
this.state.selected = found >= 0 ? found : Math.min(this.state.selected, Math.max(0, rooms.length - 1));
|
|
168
|
+
this.state.connection = "online";
|
|
169
|
+
if (!quiet) {
|
|
170
|
+
this.state.status = t("status.refreshed");
|
|
171
|
+
this.state.statusKind = "info";
|
|
172
|
+
}
|
|
173
|
+
this.touch();
|
|
174
|
+
return true;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
this.state.connection = "offline";
|
|
177
|
+
this.state.status = t("status.refreshFailed", { error: errorText(error) });
|
|
178
|
+
this.state.statusKind = "error";
|
|
179
|
+
this.touch();
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
isCurrent(roomID: string) {
|
|
185
|
+
return this.state.current?.id === roomID;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async openSelected() {
|
|
189
|
+
return this.openRoom(this.state.selected);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async openRoom(indexOrRoom: number | RoomSummary) {
|
|
193
|
+
const index = typeof indexOrRoom === "number" ? indexOrRoom : this.state.rooms.indexOf(indexOrRoom);
|
|
194
|
+
const summary = this.state.rooms[index];
|
|
195
|
+
if (!summary) return;
|
|
196
|
+
const previous = this.state.current;
|
|
197
|
+
if (previous) this.drafts.set(previous.id, this.state.draft);
|
|
198
|
+
this.state.selected = index;
|
|
199
|
+
this.stopPolling();
|
|
200
|
+
this.state.current = { id: summary.id, name: summary.name, participants: summary.participants };
|
|
201
|
+
// Restore the destination's draft so unsent text never follows the user.
|
|
202
|
+
// With no current room, prefer the destination's draft and otherwise keep
|
|
203
|
+
// text typed from the room list.
|
|
204
|
+
this.state.draft = this.drafts.get(summary.id)
|
|
205
|
+
?? (previous ? "" : String(this.state.draft ?? ""));
|
|
206
|
+
this.state.joined = summary.joined;
|
|
207
|
+
this.state.messages = [];
|
|
208
|
+
this.state.pending = null;
|
|
209
|
+
this.state.highlight = null;
|
|
210
|
+
this.state.status = t("status.opening", { name: summary.name });
|
|
211
|
+
this.state.statusKind = "info";
|
|
212
|
+
this.touch();
|
|
213
|
+
if (summary.joined) await this.loadRoom(summary.id);
|
|
214
|
+
else await this.joinRoom(summary.id);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async joinRoom(roomID: string) {
|
|
218
|
+
try {
|
|
219
|
+
const result = await this.api.join(roomID, this.sessionID, this.participant);
|
|
220
|
+
if (!this.isCurrent(roomID)) return;
|
|
221
|
+
if (result.pending) {
|
|
222
|
+
const message = String(result.message ?? t("status.joinPending"));
|
|
223
|
+
this.state.pending = { roomId: roomID, message };
|
|
224
|
+
this.state.joined = false;
|
|
225
|
+
this.state.status = message;
|
|
226
|
+
this.state.statusKind = "info";
|
|
227
|
+
this.touch();
|
|
228
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this.enterRoom(result.room, roomID, true);
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (this.isCurrent(roomID)) this.setStatus(t("status.joinFailed", { error: errorText(error) }), "error");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async checkJoin(roomID: string) {
|
|
238
|
+
if (this.closed || !this.isCurrent(roomID) || this.state.pending?.roomId !== roomID) return;
|
|
239
|
+
try {
|
|
240
|
+
const status = joinStatusOf(await this.api.joinStatus(roomID, this.sessionID));
|
|
241
|
+
if (!this.isCurrent(roomID) || this.state.pending?.roomId !== roomID) return;
|
|
242
|
+
if (status === "joined") {
|
|
243
|
+
this.state.pending = null;
|
|
244
|
+
this.setStatus(t("status.joinApproved"), "success");
|
|
245
|
+
await this.loadRoom(roomID);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (status === "pending") {
|
|
249
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
this.state.pending = null;
|
|
253
|
+
this.setStatus(t("status.joinStale"), "error");
|
|
254
|
+
} catch {
|
|
255
|
+
this.later(() => this.checkJoin(roomID), 1500);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async loadRoom(roomID: string) {
|
|
260
|
+
try {
|
|
261
|
+
const room = await this.api.getRoom(roomID);
|
|
262
|
+
if (!this.isCurrent(roomID) || !room) return;
|
|
263
|
+
if (room.joined === false) {
|
|
264
|
+
this.notJoined(roomID, t("status.notMember"));
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
this.enterRoom(room, roomID, false);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (this.isCurrent(roomID)) this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
enterRoom(room: RoomWire | null, roomID: string, joinedNow: boolean) {
|
|
274
|
+
if (!room || typeof room !== "object") {
|
|
275
|
+
this.setStatus(t("status.roomMissing"), "error");
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const summary = this.state.rooms.find((item) => item.id === roomID);
|
|
279
|
+
if (summary) {
|
|
280
|
+
summary.joined = true;
|
|
281
|
+
summary.name = String(room.name ?? summary.name);
|
|
282
|
+
if (Array.isArray(room.participants)) summary.participants = room.participants;
|
|
283
|
+
}
|
|
284
|
+
const cache = this.historyList(roomID);
|
|
285
|
+
mergeMessages(cache, room.messages);
|
|
286
|
+
this.state.current = {
|
|
287
|
+
id: roomID,
|
|
288
|
+
name: String(room.name ?? summary?.name ?? roomID),
|
|
289
|
+
participants: Array.isArray(room.participants) ? room.participants : summary?.participants ?? [],
|
|
290
|
+
};
|
|
291
|
+
this.state.joined = true;
|
|
292
|
+
this.state.pending = null;
|
|
293
|
+
this.state.messages = cache;
|
|
294
|
+
this.state.connection = "online";
|
|
295
|
+
this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
|
|
296
|
+
this.state.statusKind = "success";
|
|
297
|
+
if (typeof room.hasMore === "boolean") this.historyMore.set(roomID, room.hasMore);
|
|
298
|
+
const oldest = oldestSeq(cache);
|
|
299
|
+
if (oldest !== undefined) this.historyCursor.set(roomID, oldest);
|
|
300
|
+
this.touch();
|
|
301
|
+
this.markRead();
|
|
302
|
+
if (this.autoPoll) this.startPolling();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
historyList(roomID: string): ChatMessage[] {
|
|
306
|
+
let list = this.cache.get(roomID);
|
|
307
|
+
if (!list) {
|
|
308
|
+
list = [];
|
|
309
|
+
this.cache.set(roomID, list);
|
|
310
|
+
}
|
|
311
|
+
return list;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
merge(items: unknown) {
|
|
315
|
+
mergeMessages(this.state.messages, items);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
lastTs(): number {
|
|
319
|
+
return this.state.messages.reduce((max, message) => Math.max(max, messageStamp(message)), 0);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async pollOnce(): Promise<boolean> {
|
|
323
|
+
const room = this.state.current;
|
|
324
|
+
if (!room || !this.state.joined) return false;
|
|
325
|
+
const roomID = room.id;
|
|
326
|
+
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
327
|
+
this.pollAbort = controller;
|
|
328
|
+
try {
|
|
329
|
+
const result = await this.api.poll(
|
|
330
|
+
roomID,
|
|
331
|
+
this.sessionID,
|
|
332
|
+
this.lastTs(),
|
|
333
|
+
controller ? { signal: controller.signal } : undefined,
|
|
334
|
+
);
|
|
335
|
+
if (!this.isCurrent(roomID)) return false;
|
|
336
|
+
if (result === null) throw new Error("Invalid inbox response");
|
|
337
|
+
if (result.member === false) {
|
|
338
|
+
this.notJoined(roomID, t("status.memberLost"));
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
const wasOffline = this.state.connection !== "online";
|
|
342
|
+
this.merge(result.items);
|
|
343
|
+
this.state.connection = "online";
|
|
344
|
+
if (wasOffline) {
|
|
345
|
+
this.state.status = t("status.reconnected");
|
|
346
|
+
this.state.statusKind = "success";
|
|
347
|
+
}
|
|
348
|
+
this.touch();
|
|
349
|
+
this.markRead();
|
|
350
|
+
return true;
|
|
351
|
+
} catch (error) {
|
|
352
|
+
if (controller?.signal.aborted) return false;
|
|
353
|
+
this.state.connection = "reconnecting";
|
|
354
|
+
this.state.status = t("status.connectionLost", { error: errorText(error) });
|
|
355
|
+
this.state.statusKind = "error";
|
|
356
|
+
this.touch();
|
|
357
|
+
return false;
|
|
358
|
+
} finally {
|
|
359
|
+
if (this.pollAbort === controller) this.pollAbort = null;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
startPolling() {
|
|
364
|
+
const token = ++this.pollToken;
|
|
365
|
+
let delay = 0;
|
|
366
|
+
const step = async () => {
|
|
367
|
+
if (this.closed || token !== this.pollToken || !this.state.joined) return;
|
|
368
|
+
const ok = await this.pollOnce();
|
|
369
|
+
if (this.closed || token !== this.pollToken || !this.state.joined) return;
|
|
370
|
+
delay = ok ? 0 : Math.min(delay ? delay * 2 : FIRST_BACKOFF, MAX_BACKOFF);
|
|
371
|
+
this.later(step, delay);
|
|
372
|
+
};
|
|
373
|
+
void step();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
stopPolling() {
|
|
377
|
+
this.pollToken += 1;
|
|
378
|
+
this.pollAbort?.abort();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
notJoined(roomID: string, message: string) {
|
|
382
|
+
if (!this.isCurrent(roomID)) return;
|
|
383
|
+
this.state.joined = false;
|
|
384
|
+
const summary = this.state.rooms.find((room) => room.id === roomID);
|
|
385
|
+
if (summary) summary.joined = false;
|
|
386
|
+
this.setStatus(message, "error");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
markRead() {
|
|
390
|
+
const room = this.state.current;
|
|
391
|
+
const newest = this.lastTs();
|
|
392
|
+
if (!room || !newest) return;
|
|
393
|
+
try {
|
|
394
|
+
const result = this.api.markRead?.(room.id, this.sessionID, newest);
|
|
395
|
+
if (result && typeof result.catch === "function") result.catch(() => {});
|
|
396
|
+
} catch {
|
|
397
|
+
// A read receipt failure must not interrupt the UI.
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async send() {
|
|
402
|
+
const raw = String(this.state.draft ?? "").trim();
|
|
403
|
+
if (!raw) return;
|
|
404
|
+
const room = this.state.current;
|
|
405
|
+
if (!room || !this.state.joined) {
|
|
406
|
+
this.setStatus(t("status.joinBeforeSend"), "error");
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
// One unresolved send per room: a second trigger must leave its draft alone.
|
|
410
|
+
if (this.inflightSends.has(room.id)) return;
|
|
411
|
+
const text = canonicalizeMentions(raw, room.participants);
|
|
412
|
+
// Reuse the attempt's id when the same failed text is sent again, so a lost
|
|
413
|
+
// response cannot produce two messages. Attempts are keyed by room so a
|
|
414
|
+
// failure in one room cannot seed an id in another.
|
|
415
|
+
const attempt = this.pendingSends.get(room.id);
|
|
416
|
+
const pending = attempt && attempt.text === raw
|
|
417
|
+
? attempt
|
|
418
|
+
: { text: raw, clientMessageID: globalThis.crypto.randomUUID() };
|
|
419
|
+
this.pendingSends.set(room.id, pending);
|
|
420
|
+
this.inflightSends.add(room.id);
|
|
421
|
+
this.state.draft = "";
|
|
422
|
+
this.drafts.set(room.id, "");
|
|
423
|
+
this.touch();
|
|
424
|
+
try {
|
|
425
|
+
const result = await this.api.send(room.id, this.sessionID, text, { clientMessageID: pending.clientMessageID });
|
|
426
|
+
// The room may have been deleted while the request was in flight.
|
|
427
|
+
if (this.pendingSends.get(room.id) !== pending) return;
|
|
428
|
+
this.pendingSends.delete(room.id);
|
|
429
|
+
if (!this.isCurrent(room.id)) {
|
|
430
|
+
// A late success belongs to its room's cache, not the visible room.
|
|
431
|
+
const cache = this.historyList(room.id);
|
|
432
|
+
if (Array.isArray(result?.messages)) mergeMessages(cache, result.messages);
|
|
433
|
+
else if (result?.id) mergeMessages(cache, [result]);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (Array.isArray(result?.messages)) this.merge(result.messages);
|
|
437
|
+
else if (result?.id) this.merge([result]);
|
|
438
|
+
this.state.highlight = null;
|
|
439
|
+
this.state.connection = "online";
|
|
440
|
+
this.setStatus(t("status.sent"), "success");
|
|
441
|
+
this.markRead();
|
|
442
|
+
} catch (error) {
|
|
443
|
+
// The room may have been deleted while the request was in flight.
|
|
444
|
+
if (this.pendingSends.get(room.id) !== pending) return;
|
|
445
|
+
// Keep text typed while the request was in flight; it belongs to the
|
|
446
|
+
// sending room, so a late failure cannot edit another room's draft.
|
|
447
|
+
const typed = this.drafts.get(room.id) ?? "";
|
|
448
|
+
const restored = typed ? `${raw}\n${typed}` : raw;
|
|
449
|
+
this.drafts.set(room.id, restored);
|
|
450
|
+
if (this.isCurrent(room.id)) {
|
|
451
|
+
this.state.draft = restored;
|
|
452
|
+
this.setStatus(t("status.sendFailed", { error: errorText(error) }), "error");
|
|
453
|
+
}
|
|
454
|
+
} finally {
|
|
455
|
+
this.inflightSends.delete(room.id);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
rememberDraft() {
|
|
460
|
+
if (this.state.current) this.drafts.set(this.state.current.id, this.state.draft);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
setDraft(text: string) {
|
|
464
|
+
this.state.draft = String(text ?? "");
|
|
465
|
+
this.rememberDraft();
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
appendDraft(text: string) {
|
|
469
|
+
this.state.draft += String(text ?? "");
|
|
470
|
+
this.rememberDraft();
|
|
471
|
+
this.state.focus = "composer";
|
|
472
|
+
this.state.help = false;
|
|
473
|
+
this.touch();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
moveSelection(delta: number) {
|
|
477
|
+
if (!this.state.rooms.length) return;
|
|
478
|
+
this.state.selected = Math.min(
|
|
479
|
+
this.state.rooms.length - 1,
|
|
480
|
+
Math.max(0, this.state.selected + delta),
|
|
481
|
+
);
|
|
482
|
+
this.state.highlight = null;
|
|
483
|
+
this.touch();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
selectRoom(index: number) {
|
|
487
|
+
if (!this.state.rooms[index]) return;
|
|
488
|
+
this.state.selected = index;
|
|
489
|
+
this.state.highlight = null;
|
|
490
|
+
this.touch();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
toggleFocus() {
|
|
494
|
+
this.state.focus = this.state.focus === "rooms" ? "composer" : "rooms";
|
|
495
|
+
if (this.state.focus === "composer") this.state.help = false;
|
|
496
|
+
this.touch();
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
toggleHelp() {
|
|
500
|
+
this.state.help = !this.state.help;
|
|
501
|
+
this.touch();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
clearOrBlur() {
|
|
505
|
+
this.state.draft = "";
|
|
506
|
+
this.rememberDraft();
|
|
507
|
+
this.state.focus = "rooms";
|
|
508
|
+
this.state.highlight = null;
|
|
509
|
+
this.touch();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
clearHighlight() {
|
|
513
|
+
if (!this.state.highlight) return;
|
|
514
|
+
this.state.highlight = null;
|
|
515
|
+
this.touch();
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
quit() {
|
|
519
|
+
this.state.quit = true;
|
|
520
|
+
this.close();
|
|
521
|
+
this.touch();
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
close() {
|
|
525
|
+
this.closed = true;
|
|
526
|
+
this.pollToken += 1;
|
|
527
|
+
this.pollAbort?.abort();
|
|
528
|
+
for (const timer of this.timers) clearTimeout(timer);
|
|
529
|
+
this.timers.clear();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
attachPalette(ChatApp);
|
|
534
|
+
attachInfo(ChatApp);
|
|
535
|
+
attachSettings(ChatApp);
|
|
536
|
+
attachReminders(ChatApp);
|
|
537
|
+
attachRoomSettings(ChatApp);
|
|
538
|
+
attachMessageActions(ChatApp);
|
|
539
|
+
attachJoinRequests(ChatApp);
|
|
540
|
+
|
|
541
|
+
export function createApp(options: ChatAppOptions) {
|
|
542
|
+
return new ChatApp(options);
|
|
543
|
+
}
|