@alexsun-top/opencode-chat-room 0.1.1 → 0.1.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/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/package.json +2 -1
- package/plugin.ts +268 -73
- package/server/chat-server.ts +230 -53
- package/server/chat.html +350 -95
- package/src/center-client.ts +412 -152
- package/src/queue.ts +191 -0
- package/src/room-service.ts +11 -4
- package/src/store.ts +19 -6
package/README.md
CHANGED
|
@@ -57,17 +57,17 @@ On the server machine, install Bun first. `npx`, `pnpx`, and `bunx` only execute
|
|
|
57
57
|
|
|
58
58
|
```bash
|
|
59
59
|
# Show CLI options:
|
|
60
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.
|
|
60
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.3 -- opencode-chat-room --help
|
|
61
61
|
|
|
62
62
|
# Start the local central server (choose any one runner):
|
|
63
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.
|
|
64
|
-
pnpx --package=@alexsun-top/opencode-chat-room@0.1.
|
|
65
|
-
bunx @alexsun-top/opencode-chat-room@0.1.
|
|
63
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.3 -- opencode-chat-room --host 0.0.0.0 --port 4399
|
|
64
|
+
pnpx --package=@alexsun-top/opencode-chat-room@0.1.3 opencode-chat-room --host 0.0.0.0 --port 4399
|
|
65
|
+
bunx @alexsun-top/opencode-chat-room@0.1.3 --host 0.0.0.0 --port 4399 --token secret
|
|
66
66
|
|
|
67
67
|
# Environment variables also work; CLI options take precedence:
|
|
68
68
|
CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
|
|
69
69
|
CHAT_ROOM_SERVER_TOKEN=secret \
|
|
70
|
-
bunx @alexsun-top/opencode-chat-room@0.1.
|
|
70
|
+
bunx @alexsun-top/opencode-chat-room@0.1.3
|
|
71
71
|
|
|
72
72
|
# Local checkout:
|
|
73
73
|
bun install
|
package/README.zh-CN.md
CHANGED
|
@@ -57,17 +57,17 @@
|
|
|
57
57
|
|
|
58
58
|
```bash
|
|
59
59
|
# 查看 CLI 选项:
|
|
60
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.
|
|
60
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.3 -- opencode-chat-room --help
|
|
61
61
|
|
|
62
62
|
# 启动本地中心服务器(三选一):
|
|
63
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.
|
|
64
|
-
pnpx --package=@alexsun-top/opencode-chat-room@0.1.
|
|
65
|
-
bunx @alexsun-top/opencode-chat-room@0.1.
|
|
63
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.1.3 -- opencode-chat-room --host 0.0.0.0 --port 4399
|
|
64
|
+
pnpx --package=@alexsun-top/opencode-chat-room@0.1.3 opencode-chat-room --host 0.0.0.0 --port 4399
|
|
65
|
+
bunx @alexsun-top/opencode-chat-room@0.1.3 --host 0.0.0.0 --port 4399 --token secret
|
|
66
66
|
|
|
67
67
|
# 也可以使用环境变量;CLI 参数优先:
|
|
68
68
|
CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
|
|
69
69
|
CHAT_ROOM_SERVER_TOKEN=secret \
|
|
70
|
-
bunx @alexsun-top/opencode-chat-room@0.1.
|
|
70
|
+
bunx @alexsun-top/opencode-chat-room@0.1.3
|
|
71
71
|
|
|
72
72
|
# 本地 checkout:
|
|
73
73
|
bun install
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexsun-top/opencode-chat-room",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A chat-room plugin and central server for OpenCode.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@opencode-ai/plugin": "^1.18.16",
|
|
45
|
+
"@opencode-ai/sdk": "1.18.16",
|
|
45
46
|
"zod": "4.1.8"
|
|
46
47
|
}
|
|
47
48
|
}
|
package/plugin.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { tool } from "@opencode-ai/plugin";
|
|
1
|
+
import { tool, type PluginInput } from "@opencode-ai/plugin";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { promises as fs } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import RoomService, { RoomError, type Message } from "./src/room-service";
|
|
6
6
|
import {
|
|
7
|
-
authHeader,
|
|
8
7
|
enqueue,
|
|
9
8
|
formatNotificationLines,
|
|
10
9
|
formatRoom,
|
|
@@ -20,10 +19,26 @@ import {
|
|
|
20
19
|
type Registry,
|
|
21
20
|
type RegistryEntry,
|
|
22
21
|
} from "./src/store";
|
|
23
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
centerAction,
|
|
24
|
+
checkCenterInbox,
|
|
25
|
+
createQueueTransport,
|
|
26
|
+
sendQueue,
|
|
27
|
+
wakeSessions,
|
|
28
|
+
type QueueTransport,
|
|
29
|
+
} from "./src/center-client";
|
|
30
|
+
import {
|
|
31
|
+
clearPendingAdmission,
|
|
32
|
+
getOrCreatePendingAdmission,
|
|
33
|
+
markPendingAdmissionAdmitted,
|
|
34
|
+
pendingAdmission,
|
|
35
|
+
rejectPendingAdmission,
|
|
36
|
+
} from "./src/queue";
|
|
24
37
|
|
|
25
38
|
// captured once at server start; registry push notifications route to it
|
|
26
39
|
let selfServerUrl = "";
|
|
40
|
+
let selfDirectory = "";
|
|
41
|
+
let queueTransport: QueueTransport = createQueueTransport();
|
|
27
42
|
|
|
28
43
|
// ponytail: central-mode in-process wake timer (see server() below)
|
|
29
44
|
let wakeTimer: ReturnType<typeof setInterval> | null = null;
|
|
@@ -32,7 +47,7 @@ let wakeTimer: ReturnType<typeof setInterval> | null = null;
|
|
|
32
47
|
// central server); unset = standalone local-file mode
|
|
33
48
|
const centerUrl = process.env.CHAT_ROOM_SERVER_URL ?? "";
|
|
34
49
|
const centerToken = process.env.CHAT_ROOM_SERVER_TOKEN ?? "";
|
|
35
|
-
const centerOpts = { center: centerUrl, token: centerToken };
|
|
50
|
+
const centerOpts = { center: centerUrl, token: centerToken, directory: "" };
|
|
36
51
|
// ponytail: optional常驻 opencode serve 地址——配置了才能对已退出 session
|
|
37
52
|
// 做进程内唤起;未配置则不唤起(用户要求)
|
|
38
53
|
const serveWakeUrl = process.env.CHAT_ROOM_WAKE_SERVE_URL ?? "";
|
|
@@ -67,16 +82,23 @@ async function registerSelf(
|
|
|
67
82
|
return enqueue(async () => {
|
|
68
83
|
try {
|
|
69
84
|
const reg = await loadRegistry();
|
|
85
|
+
const latestMessageTs =
|
|
86
|
+
(await loadRooms()).find((room) => room.id === roomId)?.messages.at(-1)
|
|
87
|
+
?.createdAt ?? 0;
|
|
70
88
|
const regRoom = (reg[roomId] =
|
|
71
89
|
reg[roomId] ?? Object.create(null) as Record<string, RegistryEntry>);
|
|
72
90
|
regRoom[sessionID] = {
|
|
73
91
|
serverUrl: selfServerUrl,
|
|
74
92
|
apiPrefix: "/api",
|
|
75
93
|
name,
|
|
94
|
+
directory: selfDirectory || undefined,
|
|
76
95
|
// ponytail: re-join must not rewind the read watermark (unread
|
|
77
96
|
// messages before the re-join would be silently dropped); a first
|
|
78
97
|
// join starts reading at join time
|
|
79
|
-
lastReadTs:
|
|
98
|
+
lastReadTs:
|
|
99
|
+
regRoom[sessionID]?.lastReadTs ??
|
|
100
|
+
Math.max(Date.now(), latestMessageTs),
|
|
101
|
+
lastQueuedTs: regRoom[sessionID]?.lastQueuedTs,
|
|
80
102
|
};
|
|
81
103
|
await saveRegistry(reg);
|
|
82
104
|
} catch {
|
|
@@ -115,9 +137,12 @@ async function logNotify(
|
|
|
115
137
|
): Promise<void> {
|
|
116
138
|
const lines = results.map((r) => {
|
|
117
139
|
const head = `[${timeOf(Date.now())}] room=${roomId}`;
|
|
118
|
-
|
|
140
|
+
if (r.status === "rejected") {
|
|
141
|
+
return `${head} FAIL ${r.reason instanceof Error ? r.reason.message : String(r.reason)}`;
|
|
142
|
+
}
|
|
143
|
+
return r.value.status >= 200 && r.value.status < 300
|
|
119
144
|
? `${head} OK ${r.value.status} ${r.value.url}`
|
|
120
|
-
: `${head} FAIL ${r.
|
|
145
|
+
: `${head} FAIL ${r.value.status} ${r.value.url}`;
|
|
121
146
|
});
|
|
122
147
|
await fs
|
|
123
148
|
.appendFile(notifyPath(), lines.join("\n") + "\n", "utf8")
|
|
@@ -129,7 +154,7 @@ async function bumpLastRead(
|
|
|
129
154
|
roomId: string,
|
|
130
155
|
sessionID: string,
|
|
131
156
|
ts: number,
|
|
132
|
-
): Promise<
|
|
157
|
+
): Promise<boolean> {
|
|
133
158
|
return enqueue(async () => {
|
|
134
159
|
try {
|
|
135
160
|
const reg = await loadRegistry();
|
|
@@ -138,9 +163,35 @@ async function bumpLastRead(
|
|
|
138
163
|
// never rewind: an older in-flight push must not lower the watermark
|
|
139
164
|
entry.lastReadTs = Math.max(entry.lastReadTs ?? 0, ts);
|
|
140
165
|
await saveRegistry(reg);
|
|
166
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
167
|
+
return true;
|
|
141
168
|
}
|
|
169
|
+
return false;
|
|
142
170
|
} catch {
|
|
143
171
|
// best-effort: an unpromoted read position only means poll repeats
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function bumpLastQueued(
|
|
178
|
+
roomId: string,
|
|
179
|
+
sessionID: string,
|
|
180
|
+
ts: number,
|
|
181
|
+
): Promise<boolean> {
|
|
182
|
+
return enqueue(async () => {
|
|
183
|
+
try {
|
|
184
|
+
const reg = await loadRegistry();
|
|
185
|
+
const entry = reg[roomId]?.[sessionID];
|
|
186
|
+
if (entry) {
|
|
187
|
+
entry.lastQueuedTs = Math.max(entry.lastQueuedTs ?? 0, ts);
|
|
188
|
+
await saveRegistry(reg);
|
|
189
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
144
195
|
}
|
|
145
196
|
});
|
|
146
197
|
}
|
|
@@ -148,69 +199,205 @@ async function bumpLastRead(
|
|
|
148
199
|
// ponytail: one push carries EVERY unread message for the target (not just
|
|
149
200
|
// the latest), so advancing the watermark on success never skips a message
|
|
150
201
|
// whose earlier push failed. Event-only pushes (empty messages) don't bump.
|
|
151
|
-
|
|
202
|
+
type NotifyBatch = {
|
|
203
|
+
roomId: string;
|
|
204
|
+
roomName: string;
|
|
205
|
+
messages: Message[];
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
type NotifyAttempt = { url: string; status: number };
|
|
209
|
+
|
|
210
|
+
type NotifyTargetQueue = {
|
|
211
|
+
latest: NotifyBatch | undefined;
|
|
212
|
+
latestTs: number;
|
|
213
|
+
running: Promise<NotifyAttempt[]>;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const notifyQueues = new Map<string, NotifyTargetQueue>();
|
|
217
|
+
|
|
218
|
+
function notifyBatchMaxTs(batch: NotifyBatch): number {
|
|
219
|
+
return batch.messages.reduce(
|
|
220
|
+
(maxTs, message) => Math.max(maxTs, message.createdAt),
|
|
221
|
+
0,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function notifyTarget(
|
|
226
|
+
sessionID: string,
|
|
227
|
+
batch: NotifyBatch,
|
|
228
|
+
): Promise<NotifyAttempt | undefined> {
|
|
229
|
+
const reg = await loadRegistry();
|
|
230
|
+
const entry = reg[batch.roomId]?.[sessionID];
|
|
231
|
+
if (!entry?.serverUrl) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
const unread = batch.messages.filter(
|
|
235
|
+
(m) =>
|
|
236
|
+
m.createdAt >
|
|
237
|
+
Math.max(entry.lastReadTs ?? 0, entry.lastQueuedTs ?? 0) &&
|
|
238
|
+
(m.senderSessionID !== undefined
|
|
239
|
+
? m.senderSessionID !== sessionID
|
|
240
|
+
: m.senderId !== entry.name),
|
|
241
|
+
);
|
|
242
|
+
const currentMaxTs = unread.reduce(
|
|
243
|
+
(mx, message) => Math.max(mx, message.createdAt),
|
|
244
|
+
0,
|
|
245
|
+
);
|
|
246
|
+
const existing = pendingAdmission(sessionID, batch.roomId);
|
|
247
|
+
const deltaItems =
|
|
248
|
+
existing?.state === "admitted" && currentMaxTs > existing.maxTs
|
|
249
|
+
? unread.filter((message) => message.createdAt > existing.maxTs)
|
|
250
|
+
: unread;
|
|
251
|
+
const base = new URL(entry.serverUrl);
|
|
252
|
+
const prefix = (entry.apiPrefix || "/api").replace(/^\/+|\/+$/g, "");
|
|
253
|
+
const url = `${base.origin}/${prefix}/session/${sessionID}/prompt`;
|
|
254
|
+
if (
|
|
255
|
+
existing?.state === "admitted" &&
|
|
256
|
+
(currentMaxTs <= existing.maxTs || deltaItems.length === 0)
|
|
257
|
+
) {
|
|
258
|
+
if (existing.maxTs > 0) {
|
|
259
|
+
await bumpLastQueued(batch.roomId, sessionID, existing.maxTs);
|
|
260
|
+
}
|
|
261
|
+
return { url, status: 200 };
|
|
262
|
+
}
|
|
263
|
+
const { lines, capped } = formatNotificationLines(deltaItems);
|
|
264
|
+
const prompt =
|
|
265
|
+
deltaItems.length > 0
|
|
266
|
+
? `<notification>\nNew messages in room ${batch.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`
|
|
267
|
+
: `<notification>\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
268
|
+
const maxTs = deltaItems.reduce(
|
|
269
|
+
(mx, message) => Math.max(mx, message.createdAt),
|
|
270
|
+
0,
|
|
271
|
+
);
|
|
272
|
+
const admissionItems =
|
|
273
|
+
deltaItems.length > 0 ? deltaItems : batch.messages.slice(-1);
|
|
274
|
+
const admission = getOrCreatePendingAdmission(
|
|
275
|
+
sessionID,
|
|
276
|
+
batch.roomId,
|
|
277
|
+
admissionItems,
|
|
278
|
+
prompt,
|
|
279
|
+
maxTs,
|
|
280
|
+
capped,
|
|
281
|
+
);
|
|
282
|
+
if (admission.state === "admitted" && maxTs <= admission.maxTs) {
|
|
283
|
+
if (admission.maxTs <= 0) {
|
|
284
|
+
return { url, status: 200 };
|
|
285
|
+
}
|
|
286
|
+
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
287
|
+
return { url, status: 200 };
|
|
288
|
+
}
|
|
289
|
+
const result = await sendQueue(
|
|
290
|
+
{
|
|
291
|
+
...queueTransport,
|
|
292
|
+
serverUrl: entry.serverUrl,
|
|
293
|
+
directory: entry.directory ?? queueTransport.directory,
|
|
294
|
+
},
|
|
295
|
+
sessionID,
|
|
296
|
+
admission.prompt,
|
|
297
|
+
admission.id,
|
|
298
|
+
);
|
|
299
|
+
if (result !== "delivered") {
|
|
300
|
+
if (result === "rejected") {
|
|
301
|
+
rejectPendingAdmission(sessionID, batch.roomId, admission.id);
|
|
302
|
+
}
|
|
303
|
+
return { url, status: result === "unreachable" ? 0 : 400 };
|
|
304
|
+
}
|
|
305
|
+
markPendingAdmissionAdmitted(sessionID, batch.roomId, admission.id);
|
|
306
|
+
if (admission.maxTs > 0) {
|
|
307
|
+
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
308
|
+
}
|
|
309
|
+
return { url, status: 200 };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function queueNotifyTarget(
|
|
313
|
+
sessionID: string,
|
|
314
|
+
batch: NotifyBatch,
|
|
315
|
+
): Promise<NotifyAttempt[]> {
|
|
316
|
+
const key = JSON.stringify([batch.roomId, sessionID]);
|
|
317
|
+
const existing = notifyQueues.get(key);
|
|
318
|
+
if (existing) {
|
|
319
|
+
const maxTs = notifyBatchMaxTs(batch);
|
|
320
|
+
if (maxTs > existing.latestTs) {
|
|
321
|
+
existing.latest = batch;
|
|
322
|
+
existing.latestTs = maxTs;
|
|
323
|
+
}
|
|
324
|
+
return existing.running;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const state: NotifyTargetQueue = {
|
|
328
|
+
latest: batch,
|
|
329
|
+
latestTs: notifyBatchMaxTs(batch),
|
|
330
|
+
running: Promise.resolve([] as NotifyAttempt[]),
|
|
331
|
+
};
|
|
332
|
+
const running = (async (): Promise<NotifyAttempt[]> => {
|
|
333
|
+
const attempts: NotifyAttempt[] = [];
|
|
334
|
+
let failed = false;
|
|
335
|
+
let firstError: unknown;
|
|
336
|
+
while (state.latest) {
|
|
337
|
+
const current = state.latest;
|
|
338
|
+
state.latest = undefined;
|
|
339
|
+
try {
|
|
340
|
+
const attempt = await notifyTarget(sessionID, current);
|
|
341
|
+
if (attempt) {
|
|
342
|
+
attempts.push(attempt);
|
|
343
|
+
}
|
|
344
|
+
} catch (error) {
|
|
345
|
+
if (!failed) {
|
|
346
|
+
firstError = error;
|
|
347
|
+
failed = true;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (failed) {
|
|
352
|
+
throw firstError;
|
|
353
|
+
}
|
|
354
|
+
return attempts;
|
|
355
|
+
})();
|
|
356
|
+
state.running = running;
|
|
357
|
+
notifyQueues.set(key, state);
|
|
358
|
+
const cleanup = () => {
|
|
359
|
+
if (notifyQueues.get(key) === state) {
|
|
360
|
+
notifyQueues.delete(key);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
void running.then(cleanup, cleanup);
|
|
364
|
+
return running;
|
|
365
|
+
}
|
|
366
|
+
|
|
152
367
|
async function notifyRoom(
|
|
153
368
|
roomId: string,
|
|
154
369
|
roomName: string,
|
|
155
|
-
|
|
370
|
+
_body: string,
|
|
156
371
|
selfSessionID: string,
|
|
157
372
|
messages: Message[],
|
|
158
373
|
): Promise<void> {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
headers: {
|
|
181
|
-
"Content-Type": "application/json",
|
|
182
|
-
...(auth ? { Authorization: auth } : {}),
|
|
183
|
-
},
|
|
184
|
-
body: JSON.stringify({ prompt, delivery: "queue" }),
|
|
185
|
-
});
|
|
186
|
-
return {
|
|
187
|
-
url,
|
|
188
|
-
status: res.status,
|
|
189
|
-
sessionID,
|
|
190
|
-
// ponytail: a capped push must not advance the watermark — the
|
|
191
|
-
// folded-out messages would become permanently unreadable
|
|
192
|
-
maxTs:
|
|
193
|
-
unread.length > 0 && !capped
|
|
194
|
-
? Math.max(...unread.map((m) => m.createdAt))
|
|
195
|
-
: undefined,
|
|
196
|
-
};
|
|
197
|
-
}),
|
|
198
|
-
);
|
|
199
|
-
// mark pushed messages as read only on 2xx; a failed push stays unread
|
|
200
|
-
// so the receiver's next poll picks it up
|
|
201
|
-
for (const r of results) {
|
|
202
|
-
if (
|
|
203
|
-
r.status === "fulfilled" &&
|
|
204
|
-
r.value.status >= 200 &&
|
|
205
|
-
r.value.status < 300 &&
|
|
206
|
-
r.value.maxTs !== undefined
|
|
207
|
-
) {
|
|
208
|
-
void bumpLastRead(roomId, r.value.sessionID, r.value.maxTs);
|
|
374
|
+
const reg = await loadRegistry();
|
|
375
|
+
const targets = Object.entries(reg[roomId] ?? {}).filter(
|
|
376
|
+
([sessionID, entry]) => sessionID !== selfSessionID && entry.serverUrl,
|
|
377
|
+
);
|
|
378
|
+
const batch: NotifyBatch = { roomId, roomName, messages };
|
|
379
|
+
const results = await Promise.allSettled(
|
|
380
|
+
targets.map(([sessionID]) => queueNotifyTarget(sessionID, batch)),
|
|
381
|
+
);
|
|
382
|
+
const logResults: PromiseSettledResult<NotifyAttempt>[] = [];
|
|
383
|
+
let failed = false;
|
|
384
|
+
let firstError: unknown;
|
|
385
|
+
for (const result of results) {
|
|
386
|
+
if (result.status === "fulfilled") {
|
|
387
|
+
for (const attempt of result.value) {
|
|
388
|
+
logResults.push({ status: "fulfilled", value: attempt });
|
|
389
|
+
}
|
|
390
|
+
} else {
|
|
391
|
+
logResults.push({ status: "rejected", reason: result.reason });
|
|
392
|
+
if (!failed) {
|
|
393
|
+
firstError = result.reason;
|
|
394
|
+
failed = true;
|
|
209
395
|
}
|
|
210
396
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
397
|
+
}
|
|
398
|
+
await logNotify(roomId, logResults);
|
|
399
|
+
if (failed) {
|
|
400
|
+
throw firstError;
|
|
214
401
|
}
|
|
215
402
|
}
|
|
216
403
|
|
|
@@ -232,7 +419,7 @@ const room = tool({
|
|
|
232
419
|
// (skipped for poll: poll itself pulls the inbox, so racing the
|
|
233
420
|
// fire-and-forget check would read the position it just advanced)
|
|
234
421
|
if (args.action !== "poll") {
|
|
235
|
-
void checkCenterInbox(centerOpts, context.sessionID,
|
|
422
|
+
void checkCenterInbox(centerOpts, context.sessionID, queueTransport);
|
|
236
423
|
}
|
|
237
424
|
return await centerAction(centerOpts, args, context.sessionID);
|
|
238
425
|
}
|
|
@@ -316,7 +503,7 @@ const room = tool({
|
|
|
316
503
|
NOTIFY_INSTRUCTION,
|
|
317
504
|
context.sessionID,
|
|
318
505
|
room.messages,
|
|
319
|
-
);
|
|
506
|
+
).catch(() => {});
|
|
320
507
|
}
|
|
321
508
|
return formatRoom(svc, room);
|
|
322
509
|
}
|
|
@@ -338,7 +525,7 @@ const room = tool({
|
|
|
338
525
|
NOTIFY_INSTRUCTION,
|
|
339
526
|
context.sessionID,
|
|
340
527
|
room.messages,
|
|
341
|
-
);
|
|
528
|
+
).catch(() => {});
|
|
342
529
|
}
|
|
343
530
|
leftRoomId = args.roomId;
|
|
344
531
|
return `left room ${args.roomId}`;
|
|
@@ -371,7 +558,12 @@ const room = tool({
|
|
|
371
558
|
if (!args.text?.trim()) {
|
|
372
559
|
return "error: text is required";
|
|
373
560
|
}
|
|
374
|
-
const msg = svc.sendMessage(
|
|
561
|
+
const msg = svc.sendMessage(
|
|
562
|
+
args.roomId,
|
|
563
|
+
selfName,
|
|
564
|
+
args.text,
|
|
565
|
+
context.sessionID,
|
|
566
|
+
);
|
|
375
567
|
const room = svc.getRoom(args.roomId);
|
|
376
568
|
void notifyRoom(
|
|
377
569
|
args.roomId,
|
|
@@ -379,7 +571,7 @@ const room = tool({
|
|
|
379
571
|
NOTIFY_INSTRUCTION,
|
|
380
572
|
context.sessionID,
|
|
381
573
|
room.messages,
|
|
382
|
-
);
|
|
574
|
+
).catch(() => {});
|
|
383
575
|
return `sent: ${args.text}\n${formatRoom(svc, room)}`;
|
|
384
576
|
}
|
|
385
577
|
case "poll": {
|
|
@@ -446,8 +638,11 @@ async function config(cfg: any) {
|
|
|
446
638
|
|
|
447
639
|
export default {
|
|
448
640
|
id: "chat-room",
|
|
449
|
-
server: async (input:
|
|
450
|
-
|
|
641
|
+
server: async (input: PluginInput) => {
|
|
642
|
+
queueTransport = createQueueTransport(input);
|
|
643
|
+
selfServerUrl = queueTransport.serverUrl ?? "";
|
|
644
|
+
selfDirectory = input.directory;
|
|
645
|
+
centerOpts.directory = selfDirectory;
|
|
451
646
|
if (centerUrl) {
|
|
452
647
|
// ponytail: in-process wake watcher — sweep the central server every
|
|
453
648
|
// 10s and self-push queue notifications to any session (including
|
|
@@ -457,9 +652,9 @@ export default {
|
|
|
457
652
|
if (wakeTimer) {
|
|
458
653
|
clearInterval(wakeTimer); // HMR/reload must not accumulate timers
|
|
459
654
|
}
|
|
460
|
-
void wakeSessions(centerOpts,
|
|
655
|
+
void wakeSessions(centerOpts, queueTransport, serveWakeUrl);
|
|
461
656
|
wakeTimer = setInterval(() => {
|
|
462
|
-
void wakeSessions(centerOpts,
|
|
657
|
+
void wakeSessions(centerOpts, queueTransport, serveWakeUrl);
|
|
463
658
|
}, 10000);
|
|
464
659
|
}
|
|
465
660
|
return {
|
|
@@ -477,7 +672,7 @@ export default {
|
|
|
477
672
|
void checkCenterInbox(
|
|
478
673
|
centerOpts,
|
|
479
674
|
input.sessionID,
|
|
480
|
-
|
|
675
|
+
queueTransport,
|
|
481
676
|
);
|
|
482
677
|
},
|
|
483
678
|
}
|