@alexsun-top/opencode-chat-room 0.2.1 → 0.2.3
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 +20 -1
- package/README.md +29 -20
- package/README.zh-CN.md +27 -21
- package/cli.ts +26 -1
- package/package.json +4 -1
- package/plugin.ts +111 -135
- package/server/chat-server.ts +48 -328
- package/server/history-route.ts +25 -0
- package/server/inbox.ts +9 -0
- package/server/wake.ts +91 -0
- package/src/background.ts +67 -8
- package/src/center-client.ts +8 -2
- package/src/center-inbox.ts +99 -21
- package/src/center-wake.ts +2 -2
- package/src/plugin-notifications.ts +13 -6
- package/src/queue-transport.ts +7 -1
- package/src/queue.ts +17 -55
- package/src/receiver-owner.ts +37 -0
- package/src/receiver-state.ts +92 -0
- package/src/room-service.ts +2 -4
- package/src/session-delivery.ts +51 -0
- package/src/sqlite-state.ts +265 -0
- package/src/store.ts +29 -141
- package/src/v2-plugin.ts +17 -32
- package/src/v2-receiver.ts +109 -0
- package/src/watch.ts +355 -0
package/plugin.ts
CHANGED
|
@@ -4,14 +4,14 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import RoomService, { RoomError, type Message } from "./src/room-service";
|
|
6
6
|
import {
|
|
7
|
-
enqueue,
|
|
8
7
|
formatRoom,
|
|
9
8
|
identity,
|
|
10
9
|
loadRegistry,
|
|
11
10
|
loadRooms,
|
|
12
11
|
NOTIFY_INSTRUCTION,
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
writeState,
|
|
13
|
+
readState,
|
|
14
|
+
readUnreadMessages,
|
|
15
15
|
senderName,
|
|
16
16
|
timeOf,
|
|
17
17
|
type Registry,
|
|
@@ -20,11 +20,13 @@ import {
|
|
|
20
20
|
import { centerAction } from "./src/center-client";
|
|
21
21
|
import { createQueueTransport, type QueueTransport } from "./src/queue-transport";
|
|
22
22
|
import { checkCenterInbox } from "./src/center-inbox";
|
|
23
|
+
import { assertDeliveryLease } from "./src/session-delivery";
|
|
23
24
|
import { wakeSessions } from "./src/center-wake";
|
|
24
25
|
import {
|
|
25
26
|
clearPendingAdmission,
|
|
26
27
|
} from "./src/queue";
|
|
27
28
|
import { createNotifier } from "./src/plugin-notifications";
|
|
29
|
+
import { explicitlyMentions } from "./src/mentions";
|
|
28
30
|
import { createV2Setup, type V2Runtime } from "./src/v2-plugin";
|
|
29
31
|
import { createV1Server } from "./src/v1-plugin";
|
|
30
32
|
|
|
@@ -47,45 +49,39 @@ const defaultRuntime: PluginRuntime = {
|
|
|
47
49
|
const currentRuntime = (): PluginRuntime => runtimeContext.getStore() ?? defaultRuntime;
|
|
48
50
|
|
|
49
51
|
async function withRooms<T>(
|
|
50
|
-
fn: (svc: RoomService) => T,
|
|
52
|
+
fn: (svc: RoomService, registry: Registry) => T,
|
|
51
53
|
readOnly = false,
|
|
52
54
|
): Promise<T> {
|
|
53
|
-
|
|
54
|
-
const svc = new RoomService();
|
|
55
|
-
svc.hydrate(await loadRooms());
|
|
55
|
+
const run = (svc: RoomService, registry: Registry): T => {
|
|
56
56
|
try {
|
|
57
|
-
|
|
58
|
-
if (!readOnly) {
|
|
59
|
-
await saveRooms(svc);
|
|
60
|
-
}
|
|
61
|
-
return result;
|
|
57
|
+
return fn(svc, registry);
|
|
62
58
|
} catch (err) {
|
|
63
59
|
if (err instanceof RoomError) {
|
|
64
60
|
return `error: ${err.message}` as unknown as T;
|
|
65
61
|
}
|
|
66
62
|
throw err;
|
|
67
63
|
}
|
|
68
|
-
}
|
|
64
|
+
};
|
|
65
|
+
return readOnly ? readState(run) : writeState(run);
|
|
69
66
|
}
|
|
70
67
|
|
|
71
|
-
|
|
68
|
+
function registerSelfInState(
|
|
69
|
+
service: RoomService,
|
|
70
|
+
reg: Registry,
|
|
71
|
+
runtime: PluginRuntime,
|
|
72
72
|
roomId: string,
|
|
73
73
|
sessionID: string,
|
|
74
74
|
name = identity(),
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const reg = await loadRegistry();
|
|
80
|
-
const latestMessageTs =
|
|
81
|
-
(await loadRooms()).find((room) => room.id === roomId)?.messages.at(-1)
|
|
82
|
-
?.createdAt ?? 0;
|
|
75
|
+
receiverOwnerID?: string,
|
|
76
|
+
): void {
|
|
77
|
+
const latestMessageTs = service.listRooms().find((room) => room.id === roomId)?.messages.at(-1)?.createdAt ?? 0;
|
|
83
78
|
const regRoom = (reg[roomId] =
|
|
84
79
|
reg[roomId] ?? Object.create(null) as Record<string, RegistryEntry>);
|
|
85
80
|
regRoom[sessionID] = {
|
|
86
81
|
serverUrl: runtime.selfServerUrl,
|
|
87
82
|
apiPrefix: runtime.protocol === "v1" ? "" : "/api",
|
|
88
83
|
protocol: runtime.protocol,
|
|
84
|
+
...(receiverOwnerID ? { receiverOwnerID } : regRoom[sessionID]?.receiverOwnerID ? { receiverOwnerID: regRoom[sessionID]!.receiverOwnerID } : {}),
|
|
89
85
|
name,
|
|
90
86
|
directory: runtime.selfDirectory || undefined,
|
|
91
87
|
// ponytail: re-join must not rewind the read watermark (unread
|
|
@@ -96,69 +92,46 @@ async function registerSelf(
|
|
|
96
92
|
Math.max(Date.now(), latestMessageTs),
|
|
97
93
|
lastQueuedTs: regRoom[sessionID]?.lastQueuedTs,
|
|
98
94
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function unregisterSelfInState(reg: Registry, roomId: string, sessionID: string): void {
|
|
98
|
+
if (!reg[roomId]) return;
|
|
99
|
+
delete reg[roomId][sessionID];
|
|
100
|
+
if (!Object.keys(reg[roomId]).length) delete reg[roomId];
|
|
104
101
|
}
|
|
105
102
|
|
|
106
103
|
async function refreshSelf(runtime: PluginRuntime, sessionID: string): Promise<void> {
|
|
107
|
-
await
|
|
104
|
+
await writeState((_svc, registry) => {
|
|
108
105
|
try {
|
|
109
|
-
const registry = await loadRegistry();
|
|
110
|
-
let changed = false;
|
|
111
106
|
for (const members of Object.values(registry)) {
|
|
112
107
|
const entry = members[sessionID];
|
|
113
108
|
if (!entry) continue;
|
|
109
|
+
if (runtime.protocol === "v2" && (entry.directory !== runtime.selfDirectory ||
|
|
110
|
+
entry.receiverOwnerID && entry.receiverOwnerID !== runtime.transport.receiverOwnerID)) continue;
|
|
114
111
|
entry.serverUrl = runtime.selfServerUrl;
|
|
115
112
|
entry.apiPrefix = runtime.protocol === "v1" ? "" : "/api";
|
|
116
113
|
entry.protocol = runtime.protocol;
|
|
117
|
-
entry.directory = runtime.selfDirectory || undefined;
|
|
118
|
-
changed = true;
|
|
114
|
+
if (runtime.protocol !== "v2") entry.directory = runtime.selfDirectory || undefined;
|
|
119
115
|
}
|
|
120
|
-
if (changed) await saveRegistry(registry);
|
|
121
116
|
} catch {
|
|
122
117
|
// Existing watermarks and identities remain untouched if refresh fails.
|
|
123
118
|
}
|
|
124
119
|
});
|
|
125
120
|
}
|
|
126
121
|
|
|
127
|
-
async function unregisterSelf(
|
|
128
|
-
roomId: string,
|
|
129
|
-
sessionID: string,
|
|
130
|
-
): Promise<void> {
|
|
131
|
-
return enqueue(async () => {
|
|
132
|
-
try {
|
|
133
|
-
const reg = await loadRegistry();
|
|
134
|
-
if (reg[roomId]) {
|
|
135
|
-
delete reg[roomId][sessionID];
|
|
136
|
-
if (Object.keys(reg[roomId]).length === 0) {
|
|
137
|
-
delete reg[roomId];
|
|
138
|
-
}
|
|
139
|
-
await saveRegistry(reg);
|
|
140
|
-
}
|
|
141
|
-
} catch {
|
|
142
|
-
// ponytail: registry failure only disables push, never blocks room ops
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
122
|
// ponytail: push is best-effort; poll remains the fallback; log every attempt for debugging
|
|
148
123
|
async function bumpLastRead(
|
|
149
124
|
roomId: string,
|
|
150
125
|
sessionID: string,
|
|
151
126
|
ts: number,
|
|
152
127
|
): Promise<boolean> {
|
|
153
|
-
|
|
128
|
+
assertDeliveryLease();
|
|
129
|
+
const updated = await writeState((_svc, reg) => {
|
|
154
130
|
try {
|
|
155
|
-
const reg = await loadRegistry();
|
|
156
131
|
const entry = reg[roomId]?.[sessionID];
|
|
157
132
|
if (entry) {
|
|
158
133
|
// never rewind: an older in-flight push must not lower the watermark
|
|
159
134
|
entry.lastReadTs = Math.max(entry.lastReadTs ?? 0, ts);
|
|
160
|
-
await saveRegistry(reg);
|
|
161
|
-
clearPendingAdmission(sessionID, roomId, ts);
|
|
162
135
|
return true;
|
|
163
136
|
}
|
|
164
137
|
return false;
|
|
@@ -167,6 +140,8 @@ async function bumpLastRead(
|
|
|
167
140
|
return false;
|
|
168
141
|
}
|
|
169
142
|
});
|
|
143
|
+
if (updated) clearPendingAdmission(sessionID, roomId, ts);
|
|
144
|
+
return updated;
|
|
170
145
|
}
|
|
171
146
|
|
|
172
147
|
async function bumpLastQueued(
|
|
@@ -174,14 +149,12 @@ async function bumpLastQueued(
|
|
|
174
149
|
sessionID: string,
|
|
175
150
|
ts: number,
|
|
176
151
|
): Promise<boolean> {
|
|
177
|
-
|
|
152
|
+
assertDeliveryLease();
|
|
153
|
+
const updated = await writeState((_svc, reg) => {
|
|
178
154
|
try {
|
|
179
|
-
const reg = await loadRegistry();
|
|
180
155
|
const entry = reg[roomId]?.[sessionID];
|
|
181
156
|
if (entry) {
|
|
182
157
|
entry.lastQueuedTs = Math.max(entry.lastQueuedTs ?? 0, ts);
|
|
183
|
-
await saveRegistry(reg);
|
|
184
|
-
clearPendingAdmission(sessionID, roomId, ts);
|
|
185
158
|
return true;
|
|
186
159
|
}
|
|
187
160
|
return false;
|
|
@@ -189,6 +162,8 @@ async function bumpLastQueued(
|
|
|
189
162
|
return false;
|
|
190
163
|
}
|
|
191
164
|
});
|
|
165
|
+
if (updated) clearPendingAdmission(sessionID, roomId, ts);
|
|
166
|
+
return updated;
|
|
192
167
|
}
|
|
193
168
|
|
|
194
169
|
const notifier = createNotifier({
|
|
@@ -196,6 +171,8 @@ const notifier = createNotifier({
|
|
|
196
171
|
bumpQueued: (roomId, sessionID, ts) => bumpLastQueued(roomId, sessionID, ts),
|
|
197
172
|
});
|
|
198
173
|
const { notifyRoom, notifyTarget } = notifier;
|
|
174
|
+
const restoreScans = new Map<string, { afterTs: number; untilTs?: number }>();
|
|
175
|
+
const restoreKey = (sid: string, roomID: string, ownerID = "") => JSON.stringify([ownerID, sid, roomID]);
|
|
199
176
|
|
|
200
177
|
export async function pollKnownSessions(
|
|
201
178
|
sessions: ReadonlySet<string>,
|
|
@@ -210,13 +187,45 @@ export async function pollKnownSessions(
|
|
|
210
187
|
}
|
|
211
188
|
const [rooms, registry] = await Promise.all([loadRooms(), loadRegistry()]);
|
|
212
189
|
for (const sessionID of sessions) {
|
|
190
|
+
const mentionOnly = transport.mentionOnlySessions?.has(sessionID) ?? false;
|
|
213
191
|
for (const room of rooms) {
|
|
214
|
-
|
|
192
|
+
const entry = registry[room.id]?.[sessionID];
|
|
193
|
+
if (!entry) continue;
|
|
194
|
+
if (transport.protocol === "v2" && entry.receiverOwnerID &&
|
|
195
|
+
(entry.protocol !== "v2" || entry.receiverOwnerID !== transport.receiverOwnerID)) continue;
|
|
196
|
+
if (mentionOnly && (entry.protocol !== "v2" || entry.receiverOwnerID !== transport.receiverOwnerID)) continue;
|
|
197
|
+
const cursor = Math.max(entry.lastReadTs ?? 0, entry.lastQueuedTs ?? 0);
|
|
198
|
+
const key = restoreKey(sessionID, room.id, transport.receiverOwnerID);
|
|
199
|
+
let scan = restoreScans.get(key) ?? { afterTs: cursor };
|
|
200
|
+
if (scan.afterTs < cursor) scan = { ...scan, afterTs: cursor };
|
|
201
|
+
if (mentionOnly && (scan.untilTs === undefined || scan.untilTs <= cursor)) {
|
|
202
|
+
scan.untilTs = undefined;
|
|
203
|
+
const tail = await readUnreadMessages(room.id, cursor, 20, sessionID, entry.name, true);
|
|
204
|
+
if (tail.items.some((message) => explicitlyMentions(message, entry.name, sessionID))) {
|
|
205
|
+
scan.untilTs = tail.items.at(-1)!.createdAt;
|
|
206
|
+
scan.afterTs = Math.max(scan.afterTs, scan.untilTs);
|
|
207
|
+
} else {
|
|
208
|
+
const candidates = await readUnreadMessages(room.id, scan.afterTs, 20, sessionID, entry.name);
|
|
209
|
+
if (!candidates.items.length) { restoreScans.set(key, scan); continue; }
|
|
210
|
+
scan.afterTs = candidates.items.at(-1)!.createdAt;
|
|
211
|
+
if (candidates.items.some((message) => explicitlyMentions(message, entry.name, sessionID))) scan.untilTs = scan.afterTs;
|
|
212
|
+
restoreScans.set(key, scan);
|
|
213
|
+
if (scan.untilTs === undefined) continue;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (mentionOnly) restoreScans.set(key, scan);
|
|
217
|
+
const page = await readUnreadMessages(room.id, cursor, 20, sessionID, entry.name);
|
|
218
|
+
if (!page.items.length) continue;
|
|
219
|
+
const target = mentionOnly ? { ...transport, mentionOnlySessions: new Set([...transport.mentionOnlySessions!].filter((id) => id !== sessionID)) } : transport;
|
|
215
220
|
await notifyTarget(sessionID, {
|
|
216
221
|
roomId: room.id,
|
|
217
222
|
roomName: room.name,
|
|
218
|
-
messages:
|
|
219
|
-
},
|
|
223
|
+
messages: page.items,
|
|
224
|
+
}, target);
|
|
225
|
+
if (mentionOnly && scan.untilTs !== undefined) {
|
|
226
|
+
const queued = (await loadRegistry())[room.id]?.[sessionID]?.lastQueuedTs ?? cursor;
|
|
227
|
+
if (queued >= scan.untilTs) restoreScans.delete(key);
|
|
228
|
+
}
|
|
220
229
|
}
|
|
221
230
|
}
|
|
222
231
|
}
|
|
@@ -237,6 +246,9 @@ const roomExecute = async (
|
|
|
237
246
|
) => {
|
|
238
247
|
try {
|
|
239
248
|
const runtime = currentRuntime();
|
|
249
|
+
const explicitV2Join = runtime.protocol === "v2" && (args.action === "create" || args.action === "join");
|
|
250
|
+
const receiverOwnerID = explicitV2Join && runtime.transport.verifySession
|
|
251
|
+
? await runtime.transport.verifySession(context.sessionID) : undefined;
|
|
240
252
|
if (runtime.center.center) {
|
|
241
253
|
// ponytail: central mode — self-pull inbox first, then act via HTTP
|
|
242
254
|
// (skipped for poll: poll itself pulls the inbox, so racing the
|
|
@@ -244,7 +256,10 @@ const roomExecute = async (
|
|
|
244
256
|
if (args.action !== "poll") {
|
|
245
257
|
void checkCenterInbox(runtime.center, context.sessionID, runtime.transport);
|
|
246
258
|
}
|
|
247
|
-
return await centerAction(runtime.center,
|
|
259
|
+
return await centerAction(runtime.center, {
|
|
260
|
+
...args,
|
|
261
|
+
...(runtime.protocol === "v2" ? { protocol: "v2" as const, receiverOwnerID } : {}),
|
|
262
|
+
}, context.sessionID);
|
|
248
263
|
}
|
|
249
264
|
// ponytail: act as the identity this session registered with on join
|
|
250
265
|
let selfName = identity();
|
|
@@ -253,46 +268,34 @@ const roomExecute = async (
|
|
|
253
268
|
let leaveNameShared = false;
|
|
254
269
|
let pollLastRead = 0;
|
|
255
270
|
let pollEntry = false;
|
|
256
|
-
|
|
257
|
-
const reg = await loadRegistry().catch(() => ({} as Registry));
|
|
258
|
-
if (args.action === "send" || args.action === "leave") {
|
|
259
|
-
selfName = reg[args.roomId]?.[context.sessionID]?.name ?? selfName;
|
|
260
|
-
}
|
|
261
|
-
if (args.action === "leave") {
|
|
262
|
-
// Keep the participant while another session still uses this name.
|
|
263
|
-
leaveNameShared = Object.entries(reg[args.roomId] ?? {}).some(
|
|
264
|
-
([sid, e]) => sid !== context.sessionID && e.name === selfName,
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
if (args.action === "join") {
|
|
268
|
-
joinPrevName = reg[args.roomId]?.[context.sessionID]?.name;
|
|
269
|
-
// ponytail: don't retire the old participant if another session
|
|
270
|
-
// still uses it (default same-host identity is shared)
|
|
271
|
-
joinPrevShared =
|
|
272
|
-
joinPrevName !== undefined &&
|
|
273
|
-
Object.entries(reg[args.roomId] ?? {}).some(
|
|
274
|
-
([sid, e]) => sid !== context.sessionID && e.name === joinPrevName,
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
if (args.action === "poll") {
|
|
278
|
-
pollEntry = reg[args.roomId]?.[context.sessionID] !== undefined;
|
|
279
|
-
selfName = reg[args.roomId]?.[context.sessionID]?.name ?? selfName;
|
|
280
|
-
pollLastRead = reg[args.roomId]?.[context.sessionID]?.lastReadTs ?? 0;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
let actedRoomId: string | undefined;
|
|
284
|
-
let newMemberName: string | undefined;
|
|
285
|
-
let leftRoomId: string | undefined;
|
|
271
|
+
let pollPage: Awaited<ReturnType<typeof readUnreadMessages>> | undefined;
|
|
286
272
|
let pollMaxTs: number | undefined;
|
|
273
|
+
const notifications: Array<{ roomId: string; roomName: string; messages: Message[] }> = [];
|
|
274
|
+
if (args.action === "poll" && args.roomId) {
|
|
275
|
+
const entry = (await loadRegistry())[args.roomId]?.[context.sessionID];
|
|
276
|
+
pollEntry = entry !== undefined;
|
|
277
|
+
selfName = entry?.name ?? selfName;
|
|
278
|
+
pollLastRead = entry?.lastReadTs ?? 0;
|
|
279
|
+
if (entry) pollPage = await readUnreadMessages(args.roomId, pollLastRead, 100);
|
|
280
|
+
}
|
|
287
281
|
const result = await withRooms<string>(
|
|
288
|
-
(svc) => {
|
|
282
|
+
(svc, registry) => {
|
|
283
|
+
notifications.length = 0;
|
|
284
|
+
if (args.roomId) {
|
|
285
|
+
const members = registry[args.roomId] ?? {};
|
|
286
|
+
if (args.action === "send" || args.action === "leave" || args.action === "poll") selfName = members[context.sessionID]?.name ?? selfName;
|
|
287
|
+
if (args.action === "join") joinPrevName = members[context.sessionID]?.name;
|
|
288
|
+
if (args.action === "join" && joinPrevName) joinPrevShared = Object.entries(members).some(([sid, entry]) => sid !== context.sessionID && entry.name === joinPrevName);
|
|
289
|
+
if (args.action === "leave") leaveNameShared = Object.entries(members).some(([sid, entry]) => sid !== context.sessionID && entry.name === selfName);
|
|
290
|
+
if (args.action === "poll") pollEntry = members[context.sessionID] !== undefined;
|
|
291
|
+
}
|
|
289
292
|
switch (args.action) {
|
|
290
293
|
case "create": {
|
|
291
294
|
if (!args.name?.trim()) {
|
|
292
295
|
return "error: name is required";
|
|
293
296
|
}
|
|
294
297
|
const created = svc.createRoom(args.name.trim(), identity());
|
|
295
|
-
|
|
298
|
+
registerSelfInState(svc, registry, runtime, created.id, context.sessionID, undefined, receiverOwnerID);
|
|
296
299
|
return formatRoom(svc, created);
|
|
297
300
|
}
|
|
298
301
|
case "join": {
|
|
@@ -314,19 +317,12 @@ const roomExecute = async (
|
|
|
314
317
|
const already = svc.containsParticipant(args.roomId, me);
|
|
315
318
|
svc.joinRoom(args.roomId, { id: me, name: me });
|
|
316
319
|
const room = svc.getRoom(args.roomId);
|
|
317
|
-
|
|
318
|
-
newMemberName = me;
|
|
320
|
+
registerSelfInState(svc, registry, runtime, args.roomId, context.sessionID, me, receiverOwnerID);
|
|
319
321
|
if (!already) {
|
|
320
322
|
// ponytail: membership events live in the message stream, so
|
|
321
323
|
// the join notification rides the normal unread push
|
|
322
324
|
svc.addEvent(args.roomId, `${me} joined the room`);
|
|
323
|
-
|
|
324
|
-
args.roomId,
|
|
325
|
-
room.name,
|
|
326
|
-
NOTIFY_INSTRUCTION,
|
|
327
|
-
context.sessionID,
|
|
328
|
-
room.messages,
|
|
329
|
-
).catch(() => {});
|
|
325
|
+
notifications.push({ roomId: args.roomId, roomName: room.name, messages: [...room.messages] });
|
|
330
326
|
}
|
|
331
327
|
return formatRoom(svc, room);
|
|
332
328
|
}
|
|
@@ -342,15 +338,9 @@ const roomExecute = async (
|
|
|
342
338
|
svc.leaveRoom(args.roomId, selfName);
|
|
343
339
|
const room = svc.getRoom(args.roomId);
|
|
344
340
|
svc.addEvent(args.roomId, `${selfName} left the room`);
|
|
345
|
-
|
|
346
|
-
args.roomId,
|
|
347
|
-
room.name,
|
|
348
|
-
NOTIFY_INSTRUCTION,
|
|
349
|
-
context.sessionID,
|
|
350
|
-
room.messages,
|
|
351
|
-
).catch(() => {});
|
|
341
|
+
notifications.push({ roomId: args.roomId, roomName: room.name, messages: [...room.messages] });
|
|
352
342
|
}
|
|
353
|
-
|
|
343
|
+
unregisterSelfInState(registry, args.roomId, context.sessionID);
|
|
354
344
|
return `left room ${args.roomId}`;
|
|
355
345
|
}
|
|
356
346
|
case "list": {
|
|
@@ -388,13 +378,7 @@ const roomExecute = async (
|
|
|
388
378
|
context.sessionID,
|
|
389
379
|
);
|
|
390
380
|
const room = svc.getRoom(args.roomId);
|
|
391
|
-
|
|
392
|
-
args.roomId,
|
|
393
|
-
room.name,
|
|
394
|
-
NOTIFY_INSTRUCTION,
|
|
395
|
-
context.sessionID,
|
|
396
|
-
room.messages,
|
|
397
|
-
).catch(() => {});
|
|
381
|
+
notifications.push({ roomId: args.roomId, roomName: room.name, messages: [...room.messages] });
|
|
398
382
|
return `sent: ${args.text}\n${formatRoom(svc, room)}`;
|
|
399
383
|
}
|
|
400
384
|
case "poll": {
|
|
@@ -412,9 +396,7 @@ const roomExecute = async (
|
|
|
412
396
|
}
|
|
413
397
|
// ponytail: only messages newer than this session's last read (or
|
|
414
398
|
// last successful push) — queue-notified messages don't repeat
|
|
415
|
-
const messages =
|
|
416
|
-
.getMessages(args.roomId)
|
|
417
|
-
.filter((m) => m.createdAt > pollLastRead);
|
|
399
|
+
const messages = pollPage?.items ?? [];
|
|
418
400
|
if (messages.length === 0) {
|
|
419
401
|
return "no new messages";
|
|
420
402
|
}
|
|
@@ -427,17 +409,13 @@ const roomExecute = async (
|
|
|
427
409
|
(m) =>
|
|
428
410
|
`[${timeOf(m.createdAt)}] ${senderName(svc, m)}: ${m.text}`,
|
|
429
411
|
)
|
|
430
|
-
.join("\n");
|
|
412
|
+
.join("\n") + (pollPage?.hasMore ? "\nMore unread messages remain; poll again." : "");
|
|
431
413
|
}
|
|
432
414
|
}
|
|
433
|
-
},
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
await registerSelf(joinedRoom, context.sessionID, newMemberName);
|
|
438
|
-
}
|
|
439
|
-
if (leftRoomId !== undefined) {
|
|
440
|
-
await unregisterSelf(leftRoomId, context.sessionID);
|
|
415
|
+
}, READ_ONLY_ACTIONS.has(args.action));
|
|
416
|
+
for (const notification of notifications) {
|
|
417
|
+
void notifyRoom(notification.roomId, notification.roomName, NOTIFY_INSTRUCTION,
|
|
418
|
+
context.sessionID, notification.messages).catch(() => {});
|
|
441
419
|
}
|
|
442
420
|
if (args.action === "poll" && pollMaxTs !== undefined && pollMaxTs > 0) {
|
|
443
421
|
await bumpLastRead(args.roomId!, context.sessionID, pollMaxTs);
|
|
@@ -482,8 +460,6 @@ export const setup = createV2Setup({
|
|
|
482
460
|
execute: (args, sessionID) => roomExecute(args, { sessionID }),
|
|
483
461
|
observe: refreshSelf,
|
|
484
462
|
poll: pollKnownSessions,
|
|
485
|
-
wake: (center, transport, sessions) => wakeSessions(center, transport, serveWakeUrl, sessions),
|
|
486
|
-
central: Boolean(centerUrl),
|
|
487
463
|
});
|
|
488
464
|
|
|
489
465
|
export const id = "chat-room";
|