@alexsun-top/opencode-chat-room 0.2.3 → 0.3.1
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 +21 -0
- package/README.md +44 -7
- package/README.zh-CN.md +44 -7
- package/package.json +6 -2
- package/plugin.ts +21 -6
- package/server/assets/DOMPURIFY-LICENSE +202 -0
- package/server/assets/MARKED-LICENSE +44 -0
- package/server/assets/NOTICE.txt +6 -0
- package/server/assets/base.css +225 -0
- package/server/assets/chat/events.js +148 -0
- package/server/assets/chat/index.js +12 -0
- package/server/assets/chat/markdown.js +65 -0
- package/server/assets/chat/members.js +201 -0
- package/server/assets/chat/mentions.js +131 -0
- package/server/assets/chat/messages.js +489 -0
- package/server/assets/chat/rooms.js +443 -0
- package/server/assets/chat/state.js +327 -0
- package/server/assets/conversation.css +364 -0
- package/server/assets/dompurify-3.4.7.js +3 -0
- package/server/assets/marked-18.0.4.js +79 -0
- package/server/assets/responsive.css +119 -0
- package/server/chat-server.ts +39 -43
- package/server/chat.html +27 -1984
- package/server/history-route.ts +2 -1
- package/server/inbox.ts +1 -1
- package/server/member-routes.ts +117 -0
- package/server/newrooms.ts +86 -0
- package/server/wake.ts +13 -14
- package/src/agent-presence.ts +60 -0
- package/src/agent-receipts.ts +107 -0
- package/src/background.ts +70 -25
- package/src/center-client.ts +25 -6
- package/src/center-inbox.ts +77 -37
- package/src/center-wake.ts +43 -9
- package/src/member-state.ts +25 -0
- package/src/mentions.ts +7 -0
- package/src/plugin-notifications.ts +75 -23
- package/src/queue-transport.ts +17 -1
- package/src/queue.ts +36 -2
- package/src/receiver-state.ts +108 -6
- package/src/room-service.ts +19 -1
- package/src/sqlite-state.ts +123 -16
- package/src/store.ts +28 -1
- package/src/v2-plugin.ts +10 -2
- package/src/v2-receiver.ts +67 -45
- package/src/watch.ts +17 -3
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { LS } from "./state.js";
|
|
2
|
+
|
|
3
|
+
export function initMembers(app) {
|
|
4
|
+
const $ = app.$;
|
|
5
|
+
|
|
6
|
+
app.mentionNames = () => {
|
|
7
|
+
const room = app.state.room, members = room && app.state.memberSessions.get(room.id);
|
|
8
|
+
return new Set((members || room?.participants || [])
|
|
9
|
+
.filter((person) => members ? person.sessionID !== LS.sessionID : person.name !== app.state.myName)
|
|
10
|
+
.map((person) => person.name));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
app.renderMembers = () => {
|
|
14
|
+
const people = app.state.room?.participants || [];
|
|
15
|
+
app.setText($("#btnMembers"), "membersCount", { count: people.length });
|
|
16
|
+
const list = $("#memberList");
|
|
17
|
+
list.textContent = "";
|
|
18
|
+
for (const person of people) {
|
|
19
|
+
const row = document.createElement("li"), name = document.createElement("span"), time = document.createElement("span");
|
|
20
|
+
name.textContent = person.name;
|
|
21
|
+
time.className = "m-time";
|
|
22
|
+
time.textContent = app.fmtTime(person.joinedAt);
|
|
23
|
+
row.append(name, time);
|
|
24
|
+
list.append(row);
|
|
25
|
+
}
|
|
26
|
+
$("#memberEmpty").hidden = people.length > 0;
|
|
27
|
+
if (app.mentionActive) app.updateMentionList();
|
|
28
|
+
void app.loadManageableMembers();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
app.loadManageableMembers = async () => {
|
|
32
|
+
const room = app.state.room;
|
|
33
|
+
if (!room || app.state.memberApiUnavailable.has(room.id)) return;
|
|
34
|
+
const generation = app.state.pollGeneration;
|
|
35
|
+
clearTimeout(app.state.memberRefreshTimer);
|
|
36
|
+
try {
|
|
37
|
+
const members = await app.api(`/rooms/${encodeURIComponent(room.id)}/members`);
|
|
38
|
+
if (!current(app, room.id, generation)) return;
|
|
39
|
+
const previous = app.state.memberSessions.get(room.id) || [];
|
|
40
|
+
const signature = (items) => items.map((member) => `${member.sessionID}\0${member.name}\0${member.type}`).sort().join("\u0001");
|
|
41
|
+
const changed = signature(previous) !== signature(members);
|
|
42
|
+
app.state.memberSessions.set(room.id, members);
|
|
43
|
+
if (changed && app.mentionActive) app.updateMentionList();
|
|
44
|
+
if (changed && room.kind !== "direct" && room.messages.some((message) => !message.senderSessionID && message.senderId === app.state.myName)) app.rerenderMessages();
|
|
45
|
+
const list = $("#memberList");
|
|
46
|
+
const rows = Array.from(list.querySelectorAll("li[data-member-session-id]"));
|
|
47
|
+
if (signature(rows.map((row) => ({ sessionID: row.dataset.memberSessionId, name: row.dataset.memberName, type: row.dataset.memberType }))) !== signature(members)) {
|
|
48
|
+
list.textContent = "";
|
|
49
|
+
for (const member of members) {
|
|
50
|
+
const row = document.createElement("li"), name = document.createElement("span"), actions = document.createElement("span");
|
|
51
|
+
row.dataset.memberSessionId = member.sessionID; row.dataset.memberName = member.name; row.dataset.memberType = member.type;
|
|
52
|
+
name.textContent = member.name; row.append(name);
|
|
53
|
+
if (member.type === "agent") row.append(activityBadge(member, app));
|
|
54
|
+
actions.className = "member-actions";
|
|
55
|
+
if (member.sessionID !== LS.sessionID) {
|
|
56
|
+
actions.append(actionButton(member, "messagePrivately", app.startDirect));
|
|
57
|
+
if (room.kind !== "direct") actions.append(actionButton(member, "removeMember", app.kickMember));
|
|
58
|
+
}
|
|
59
|
+
row.append(actions); list.append(row);
|
|
60
|
+
}
|
|
61
|
+
} else for (const member of members) updateActivity(list.querySelector(`[data-member-session-id="${CSS.escape(member.sessionID)}"]`), member, app);
|
|
62
|
+
$("#memberEmpty").hidden = members.length > 0;
|
|
63
|
+
app.queueReceiptObservation();
|
|
64
|
+
app.state.memberRefreshTimer = setTimeout(() => {
|
|
65
|
+
if (current(app, room.id, generation) && !document.hidden) void app.loadManageableMembers();
|
|
66
|
+
}, 5000);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error.status === 401) app.openChatSettings();
|
|
69
|
+
else if (error.status === 404) app.state.memberApiUnavailable.add(room.id);
|
|
70
|
+
else if (current(app, room.id, generation) && !document.hidden) app.state.memberRefreshTimer = setTimeout(() => void app.loadManageableMembers(), 5000);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
app.loadVisibleReceipts = async (roomId, generation = app.state.pollGeneration) => {
|
|
75
|
+
if (app.state.receiptApiUnavailable.has(roomId) || !current(app, roomId, generation)) return;
|
|
76
|
+
const ids = visibleMessageIDs(app);
|
|
77
|
+
if (!ids.length) return;
|
|
78
|
+
const query = new URLSearchParams({ messageIDs: ids.join(",") });
|
|
79
|
+
try {
|
|
80
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/receipts?${query}`);
|
|
81
|
+
if (!current(app, roomId, generation)) return;
|
|
82
|
+
app.renderReceiptData(roomId, result.receipts || []);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error.status === 404) app.state.receiptApiUnavailable.add(roomId);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
app.queueReceiptRefresh = () => {
|
|
88
|
+
clearTimeout(app.state.receiptRefreshTimer);
|
|
89
|
+
const roomId = app.state.room?.id, generation = app.state.pollGeneration;
|
|
90
|
+
app.state.receiptRefreshTimer = setTimeout(() => void app.loadVisibleReceipts(roomId, generation), 180);
|
|
91
|
+
};
|
|
92
|
+
app.queueReceiptObservation = () => {
|
|
93
|
+
app.state.receiptObservation = { roomId: app.state.room?.id, generation: app.state.pollGeneration };
|
|
94
|
+
if (app.state.receiptObservationFrame) return;
|
|
95
|
+
app.state.receiptObservationFrame = requestAnimationFrame(() => {
|
|
96
|
+
app.state.receiptObservationFrame = 0;
|
|
97
|
+
const target = app.state.receiptObservation;
|
|
98
|
+
app.state.receiptObservation = null;
|
|
99
|
+
if (!target || document.hidden || !document.hasFocus() || !current(app, target.roomId, target.generation)) return;
|
|
100
|
+
void app.loadVisibleReceipts(target.roomId, target.generation);
|
|
101
|
+
void app.ackVisibleMessages(target.roomId, target.generation);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
app.ackVisibleMessages = async (roomId = app.state.room?.id, generation = app.state.pollGeneration) => {
|
|
105
|
+
const room = app.state.room;
|
|
106
|
+
if (!room || room.id !== roomId || app.state.pollGeneration !== generation || app.state.receiptApiUnavailable.has(room.id)) return;
|
|
107
|
+
const self = app.state.memberSessions.get(room.id)?.find((member) => member.sessionID === LS.sessionID);
|
|
108
|
+
if (self?.type !== "human" || !self.membershipID) return;
|
|
109
|
+
const key = `${room.id}\0${self.membershipID}`;
|
|
110
|
+
const acknowledged = app.state.acknowledgedMessages.get(key) || new Set();
|
|
111
|
+
const pending = app.state.pendingAcknowledgements.get(key) || new Set();
|
|
112
|
+
const ids = visibleMessageIDs(app).filter((id) => !acknowledged.has(id) && !pending.has(id));
|
|
113
|
+
if (!ids.length) return;
|
|
114
|
+
app.state.pendingAcknowledgements.set(key, pending);
|
|
115
|
+
ids.forEach((id) => pending.add(id));
|
|
116
|
+
try {
|
|
117
|
+
const result = await app.api(`/rooms/${encodeURIComponent(room.id)}/receipts`, { method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, membershipID: self.membershipID, messageIDs: ids, evidence: "displayed" }) });
|
|
118
|
+
if (current(app, room.id, generation) && app.state.memberSessions.get(room.id)?.some((member) => member.sessionID === LS.sessionID && member.membershipID === self.membershipID)) {
|
|
119
|
+
acknowledgedMessages(app, key, acknowledged, result.acceptedIDs || []);
|
|
120
|
+
}
|
|
121
|
+
} catch (error) { if (error.status === 404) app.state.receiptApiUnavailable.add(room.id); }
|
|
122
|
+
finally { ids.forEach((id) => pending.delete(id)); }
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
app.startDirect = async (member) => {
|
|
126
|
+
const source = app.state.room;
|
|
127
|
+
if (!source) return;
|
|
128
|
+
const name = app.state.myName, intent = ++app.state.navigationGeneration;
|
|
129
|
+
try {
|
|
130
|
+
const room = await app.api("/direct", {
|
|
131
|
+
method: "POST",
|
|
132
|
+
body: JSON.stringify({ roomId: source.id, sessionID: LS.sessionID, targetSessionID: member.sessionID }),
|
|
133
|
+
});
|
|
134
|
+
app.enterRoom(room, name, intent);
|
|
135
|
+
} catch (error) { app.showSendError("sendFailed", { message: error.message }); }
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
app.kickMember = async (member) => {
|
|
139
|
+
const id = member.sessionID.slice(0, 8);
|
|
140
|
+
if (!confirm(app.t("kickConfirm", { name: member.name, id }))) return;
|
|
141
|
+
try {
|
|
142
|
+
await app.api(`/rooms/${encodeURIComponent(app.state.room.id)}/kick`, {
|
|
143
|
+
method: "POST",
|
|
144
|
+
body: JSON.stringify({ sessionID: LS.sessionID, targetSessionID: member.sessionID }),
|
|
145
|
+
});
|
|
146
|
+
await app.refreshRoomMeta();
|
|
147
|
+
} catch (error) { app.showSendError("sendFailed", { message: error.message }); }
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
function actionButton(member, label, action) {
|
|
151
|
+
const button = document.createElement("button");
|
|
152
|
+
button.type = "button";
|
|
153
|
+
button.className = "member-action";
|
|
154
|
+
button.textContent = app.t(label);
|
|
155
|
+
button.addEventListener("click", () => action(member));
|
|
156
|
+
return button;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function current(app, roomId, generation) {
|
|
161
|
+
return app.state.room?.id === roomId && app.state.pollGeneration === generation && !app.$("#chatView").hidden;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function visibleMessageIDs(app) {
|
|
165
|
+
const thread = app.$("#thread");
|
|
166
|
+
if (document.hidden || !document.hasFocus() || app.$("#chatView").hidden) return [];
|
|
167
|
+
const top = thread.getBoundingClientRect().top + thread.clientTop, bottom = top + thread.clientHeight;
|
|
168
|
+
const overlays = Array.from(document.querySelectorAll(".panel:not([hidden]), #mentionPicker:not([hidden]), #railRoomForm:not([hidden])"))
|
|
169
|
+
.map((node) => node.getBoundingClientRect());
|
|
170
|
+
return Array.from(thread.querySelectorAll("[data-msg-id]")).filter((row) => {
|
|
171
|
+
const rect = row.getBoundingClientRect();
|
|
172
|
+
return rect.bottom > top && rect.top < bottom && !overlays.some((overlay) => rect.left < overlay.right && rect.right > overlay.left && rect.top < overlay.bottom && rect.bottom > overlay.top);
|
|
173
|
+
}).map((row) => row.dataset.msgId).filter(Boolean);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function acknowledgedMessages(app, key, set, ids) {
|
|
177
|
+
ids.forEach((id) => set.add(id));
|
|
178
|
+
app.state.acknowledgedMessages.set(key, set);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function activityBadge(member, app) {
|
|
182
|
+
const badge = document.createElement("span");
|
|
183
|
+
updateActivityBadge(badge, member, app);
|
|
184
|
+
return badge;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function updateActivity(row, member, app) {
|
|
188
|
+
if (!row) return;
|
|
189
|
+
let badge = row.querySelector(".member-activity");
|
|
190
|
+
if (member.type !== "agent") { badge?.remove(); return; }
|
|
191
|
+
if (!badge) { badge = activityBadge(member, app); row.insertBefore(badge, row.querySelector(".member-actions")); }
|
|
192
|
+
updateActivityBadge(badge, member, app);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function updateActivityBadge(badge, member, app) {
|
|
196
|
+
const state = member.activity?.state || "unknown";
|
|
197
|
+
badge.className = `member-activity ${state}`;
|
|
198
|
+
badge.textContent = app.t(`activity_${state}`);
|
|
199
|
+
badge.title = app.t(`activityHint_${state}`);
|
|
200
|
+
badge.setAttribute("aria-label", `${badge.textContent}. ${badge.title}`);
|
|
201
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export function initMentions(app) {
|
|
2
|
+
const $ = app.$;
|
|
3
|
+
const mention = { active: false, start: 0, query: "", items: [], index: 0 };
|
|
4
|
+
app.mention = mention;
|
|
5
|
+
app.mentionActive = false;
|
|
6
|
+
app.updateMentionList = (query = mention.query) => {
|
|
7
|
+
mention.query = query;
|
|
8
|
+
mention.items = filterNames(query, app.mentionNames());
|
|
9
|
+
mention.index = mention.items.length ? 0 : -1;
|
|
10
|
+
const picker = $("#mentionPicker");
|
|
11
|
+
picker.textContent = "";
|
|
12
|
+
const list = document.createElement("ul");
|
|
13
|
+
mention.items.forEach((name, index) => list.append(createOption(name, index, query, app)));
|
|
14
|
+
picker.append(list);
|
|
15
|
+
if (!mention.items.length) {
|
|
16
|
+
const empty = document.createElement("p");
|
|
17
|
+
empty.className = "mention-empty";
|
|
18
|
+
empty.textContent = app.t("mentionEmpty");
|
|
19
|
+
picker.append(empty);
|
|
20
|
+
}
|
|
21
|
+
const hint = document.createElement("p");
|
|
22
|
+
hint.className = "mention-hint";
|
|
23
|
+
hint.textContent = app.t("mentionHint");
|
|
24
|
+
picker.append(hint);
|
|
25
|
+
highlightMention(app, mention);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
app.openMention = (start, query) => {
|
|
29
|
+
mention.active = app.mentionActive = true;
|
|
30
|
+
mention.start = start;
|
|
31
|
+
app.updateMentionList(query);
|
|
32
|
+
$("#mentionPicker").hidden = false;
|
|
33
|
+
$("#msgInput").setAttribute("aria-expanded", "true");
|
|
34
|
+
};
|
|
35
|
+
app.closeMention = () => {
|
|
36
|
+
mention.active = app.mentionActive = false;
|
|
37
|
+
mention.items = [];
|
|
38
|
+
$("#mentionPicker").hidden = true;
|
|
39
|
+
$("#msgInput").setAttribute("aria-expanded", "false");
|
|
40
|
+
$("#msgInput").removeAttribute("aria-activedescendant");
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
app.recomputeMention = () => {
|
|
44
|
+
const input = $("#msgInput"), match = currentMention(input);
|
|
45
|
+
if (match) {
|
|
46
|
+
mention.active ? app.updateMentionList(match.query) : app.openMention(match.start, match.query);
|
|
47
|
+
} else if (mention.active) {
|
|
48
|
+
const at = input.value.lastIndexOf("@");
|
|
49
|
+
if (at < 0) return app.closeMention();
|
|
50
|
+
mention.start = at;
|
|
51
|
+
app.updateMentionList(input.value.slice(at + 1, input.selectionStart));
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
app.moveMention = (direction) => {
|
|
55
|
+
if (!mention.items.length) return;
|
|
56
|
+
mention.index = (mention.index + direction + mention.items.length) % mention.items.length;
|
|
57
|
+
highlightMention(app, mention);
|
|
58
|
+
};
|
|
59
|
+
app.insertMention = (name) => {
|
|
60
|
+
const input = $("#msgInput"), end = mention.start + 1 + mention.query.length;
|
|
61
|
+
if (input.value.slice(mention.start, end) !== `@${mention.query}`) return app.closeMention();
|
|
62
|
+
input.setRangeText(`<at>@${name}</at>`, mention.start, end, "end");
|
|
63
|
+
app.closeMention();
|
|
64
|
+
app.autoGrow(input);
|
|
65
|
+
app.updateSendState();
|
|
66
|
+
app.updateDraftCount();
|
|
67
|
+
if (app.state.room) {
|
|
68
|
+
const id = app.state.room.id;
|
|
69
|
+
app.state.draftRevision.set(id, (app.state.draftRevision.get(id) || 0) + 1);
|
|
70
|
+
app.saveDraft(id, input.value);
|
|
71
|
+
}
|
|
72
|
+
input.focus({ preventScroll: true });
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function filterNames(query, names) {
|
|
77
|
+
const normalized = query.toLocaleLowerCase();
|
|
78
|
+
const starts = [], contains = [];
|
|
79
|
+
for (const name of names) {
|
|
80
|
+
if (!name) continue;
|
|
81
|
+
if (name.toLocaleLowerCase().startsWith(normalized)) starts.push(name);
|
|
82
|
+
else if (name.toLocaleLowerCase().includes(normalized)) contains.push(name);
|
|
83
|
+
}
|
|
84
|
+
const allMatches = !normalized || "all".includes(normalized) || "所有人".includes(normalized);
|
|
85
|
+
return (allMatches ? ["all"] : []).concat(starts, contains);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function createOption(name, index, query, app) {
|
|
89
|
+
const option = document.createElement("li");
|
|
90
|
+
option.className = "mention-opt";
|
|
91
|
+
option.id = `mention-opt-${index}`;
|
|
92
|
+
option.dataset.name = name;
|
|
93
|
+
option.setAttribute("role", "option");
|
|
94
|
+
const at = document.createElement("span");
|
|
95
|
+
at.className = "mention-at";
|
|
96
|
+
at.textContent = "@";
|
|
97
|
+
option.append(at);
|
|
98
|
+
const label = name === "all" ? app.t("allMembers") : name;
|
|
99
|
+
const offset = label.toLocaleLowerCase().indexOf(query.toLocaleLowerCase());
|
|
100
|
+
if (!query || offset < 0) option.append(document.createTextNode(label));
|
|
101
|
+
else {
|
|
102
|
+
option.append(document.createTextNode(label.slice(0, offset)));
|
|
103
|
+
const hit = document.createElement("span");
|
|
104
|
+
hit.className = "mention-hit";
|
|
105
|
+
hit.textContent = label.slice(offset, offset + query.length);
|
|
106
|
+
option.append(hit, document.createTextNode(label.slice(offset + query.length)));
|
|
107
|
+
}
|
|
108
|
+
return option;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function currentMention(input) {
|
|
112
|
+
const position = input.selectionStart, value = input.value;
|
|
113
|
+
let start = position;
|
|
114
|
+
while (start > 0 && /[\p{L}\p{N}_]/u.test(value[start - 1])) start--;
|
|
115
|
+
const at = start - 1;
|
|
116
|
+
if (at >= 0 && value[at] === "@" && (at === 0 || !/[A-Za-z0-9_]/.test(value[at - 1]))) {
|
|
117
|
+
return { start: at, query: value.slice(start, position) };
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function highlightMention(app, mention) {
|
|
123
|
+
const input = app.$("#msgInput"), options = app.$("#mentionPicker").querySelectorAll(".mention-opt");
|
|
124
|
+
options.forEach((option, index) => {
|
|
125
|
+
option.classList.toggle("active", index === mention.index);
|
|
126
|
+
option.setAttribute("aria-selected", String(index === mention.index));
|
|
127
|
+
});
|
|
128
|
+
const active = options[mention.index];
|
|
129
|
+
if (!active) { input.removeAttribute("aria-activedescendant"); return; }
|
|
130
|
+
input.setAttribute("aria-activedescendant", active.id);
|
|
131
|
+
}
|