@alexsun-top/opencode-chat-room 0.1.2 → 0.2.0
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 +15 -0
- package/README.md +17 -12
- package/README.zh-CN.md +17 -12
- package/index.ts +2 -0
- package/package.json +9 -4
- package/plugin.ts +147 -301
- package/server/chat-server.ts +128 -41
- package/src/center-client.ts +24 -528
- package/src/center-inbox.ts +106 -0
- package/src/center-wake.ts +118 -0
- package/src/plugin-notifications.ts +163 -0
- package/src/queue-transport.ts +129 -0
- package/src/queue.ts +14 -9
- package/src/store.ts +2 -0
- package/src/v1-plugin.ts +55 -0
- package/src/v2-plugin.ts +120 -0
package/src/center-client.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// sessions owned by this OpenCode process before using an HTTP fallback.
|
|
4
|
-
import { OpencodeClient } from "@opencode-ai/sdk/v2";
|
|
1
|
+
// Central mode room API client. Recipient runtimes poll the server and deliver
|
|
2
|
+
// inbox items through their host's supported notification transport.
|
|
5
3
|
import RoomService, {
|
|
6
4
|
type Message,
|
|
7
5
|
type Participant,
|
|
@@ -9,73 +7,20 @@ import RoomService, {
|
|
|
9
7
|
} from "./room-service";
|
|
10
8
|
import os from "node:os";
|
|
11
9
|
import {
|
|
12
|
-
authHeader,
|
|
13
|
-
formatServeWakePrompt,
|
|
14
|
-
formatNotificationLines,
|
|
15
10
|
formatRoom,
|
|
16
11
|
identity,
|
|
17
|
-
NOTIFY_INSTRUCTION,
|
|
18
12
|
timeOf,
|
|
19
13
|
} from "./store";
|
|
20
14
|
import {
|
|
21
|
-
admissionMatches,
|
|
22
15
|
clearPendingAdmission,
|
|
23
|
-
getOrCreatePendingAdmission,
|
|
24
|
-
markPendingAdmissionAdmitted,
|
|
25
|
-
pendingAdmission,
|
|
26
|
-
rejectPendingAdmission,
|
|
27
|
-
type QueueBody,
|
|
28
|
-
type QueueItem,
|
|
29
|
-
type PendingAdmission,
|
|
30
16
|
} from "./queue";
|
|
17
|
+
import type { InboxItem } from "./center-inbox";
|
|
31
18
|
export { queueAdmissionId } from "./queue";
|
|
19
|
+
export { createQueueTransport, sendAsyncNotification, sendQueue } from "./queue-transport";
|
|
20
|
+
export type { QueueTransport, PushResult } from "./queue-transport";
|
|
32
21
|
|
|
33
22
|
type Json = Record<string, unknown>;
|
|
34
23
|
|
|
35
|
-
const LOCAL_SERVER_URL = "http://localhost:4096";
|
|
36
|
-
|
|
37
|
-
export type PushResult = "delivered" | "unreachable" | "rejected";
|
|
38
|
-
|
|
39
|
-
export type QueueTransport = {
|
|
40
|
-
client?: OpencodeClient;
|
|
41
|
-
serverUrl?: string;
|
|
42
|
-
directory?: string;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
type QueueTransportInput = {
|
|
46
|
-
client?: unknown;
|
|
47
|
-
directory?: string;
|
|
48
|
-
serverUrl?: URL | string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// Compatibility bridge for OpenCode 1.18.16. The plugin exposes the embedded
|
|
52
|
-
// transport through a private field; HTTP remains the compatibility fallback.
|
|
53
|
-
export function createQueueTransport(
|
|
54
|
-
input: QueueTransportInput = {},
|
|
55
|
-
): QueueTransport {
|
|
56
|
-
const raw =
|
|
57
|
-
input.client && typeof input.client === "object"
|
|
58
|
-
? (input.client as { _client?: unknown })._client
|
|
59
|
-
: undefined;
|
|
60
|
-
let client: OpencodeClient | undefined;
|
|
61
|
-
if (raw !== undefined && raw !== null) {
|
|
62
|
-
try {
|
|
63
|
-
type ClientArgs = NonNullable<ConstructorParameters<typeof OpencodeClient>[0]>;
|
|
64
|
-
client = new OpencodeClient({
|
|
65
|
-
client: raw as NonNullable<ClientArgs["client"]>,
|
|
66
|
-
});
|
|
67
|
-
} catch {
|
|
68
|
-
client = undefined;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const serverUrl = String(input.serverUrl ?? "").trim();
|
|
72
|
-
return {
|
|
73
|
-
client,
|
|
74
|
-
serverUrl: serverUrl || LOCAL_SERVER_URL,
|
|
75
|
-
directory: input.directory,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
24
|
function toRoom(r: Json): Room {
|
|
80
25
|
const participants = new Map<string, Participant>();
|
|
81
26
|
if (Array.isArray(r.participants)) {
|
|
@@ -101,9 +46,9 @@ function svcOf(room: Json): RoomService {
|
|
|
101
46
|
return svc;
|
|
102
47
|
}
|
|
103
48
|
|
|
104
|
-
type CenterOpts = { center: string; token: string; directory?: string };
|
|
49
|
+
export type CenterOpts = { center: string; token: string; directory?: string };
|
|
105
50
|
|
|
106
|
-
async function request(
|
|
51
|
+
export async function request(
|
|
107
52
|
opts: CenterOpts,
|
|
108
53
|
path: string,
|
|
109
54
|
method: string,
|
|
@@ -130,6 +75,17 @@ async function request(
|
|
|
130
75
|
return data;
|
|
131
76
|
}
|
|
132
77
|
|
|
78
|
+
export async function markQueued(
|
|
79
|
+
opts: CenterOpts,
|
|
80
|
+
roomId: string,
|
|
81
|
+
sessionID: string,
|
|
82
|
+
ts: number,
|
|
83
|
+
): Promise<boolean> {
|
|
84
|
+
return request(opts, "/queued", "POST", { roomId, sessionID, ts })
|
|
85
|
+
.then(() => true)
|
|
86
|
+
.catch(() => false);
|
|
87
|
+
}
|
|
88
|
+
|
|
133
89
|
export async function centerAction(
|
|
134
90
|
opts: CenterOpts,
|
|
135
91
|
args: {
|
|
@@ -164,7 +120,7 @@ export async function centerAction(
|
|
|
164
120
|
name: args.name.trim(),
|
|
165
121
|
ownerId: identity(),
|
|
166
122
|
sessionID,
|
|
167
|
-
//
|
|
123
|
+
// Record the owning host so only that host wakes this session.
|
|
168
124
|
host: os.hostname(),
|
|
169
125
|
directory: opts.directory,
|
|
170
126
|
});
|
|
@@ -183,7 +139,7 @@ export async function centerAction(
|
|
|
183
139
|
sessionID,
|
|
184
140
|
participantId: me,
|
|
185
141
|
participantName: me,
|
|
186
|
-
//
|
|
142
|
+
// Record the owning host so only that host wakes this session.
|
|
187
143
|
host: os.hostname(),
|
|
188
144
|
directory: opts.directory,
|
|
189
145
|
},
|
|
@@ -253,10 +209,11 @@ export async function centerAction(
|
|
|
253
209
|
const lines = items.map(
|
|
254
210
|
(it) => `[${timeOf(it.createdAt)}] ${it.senderId}: ${it.text}`,
|
|
255
211
|
);
|
|
256
|
-
// ponytail: F7 —
|
|
257
|
-
//
|
|
212
|
+
// ponytail: F7 — serialize the acknowledgment before returning so a
|
|
213
|
+
// following action cannot race the read watermark update. A failure only
|
|
214
|
+
// repeats the messages, never loses them.
|
|
258
215
|
const maxTs = items.reduce((mx, it) => Math.max(mx, it.createdAt), 0);
|
|
259
|
-
|
|
216
|
+
await request(opts, "/read", "POST", {
|
|
260
217
|
roomId: args.roomId,
|
|
261
218
|
sessionID,
|
|
262
219
|
ts: maxTs,
|
|
@@ -269,464 +226,3 @@ export async function centerAction(
|
|
|
269
226
|
return "error: unknown action";
|
|
270
227
|
}
|
|
271
228
|
}
|
|
272
|
-
|
|
273
|
-
type InboxItem = QueueItem & {
|
|
274
|
-
roomId: string;
|
|
275
|
-
roomName: string;
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
function isRecord(value: unknown): value is Json {
|
|
279
|
-
return typeof value === "object" && value !== null;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function queueBody(admissionID: string, text: string): QueueBody {
|
|
283
|
-
return {
|
|
284
|
-
id: admissionID,
|
|
285
|
-
prompt: { text },
|
|
286
|
-
delivery: "queue",
|
|
287
|
-
resume: true,
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function statusOf(value: unknown): number | undefined {
|
|
292
|
-
if (!isRecord(value)) {
|
|
293
|
-
return undefined;
|
|
294
|
-
}
|
|
295
|
-
if (typeof value.status === "number") {
|
|
296
|
-
return value.status;
|
|
297
|
-
}
|
|
298
|
-
if (isRecord(value.response) && typeof value.response.status === "number") {
|
|
299
|
-
return value.response.status;
|
|
300
|
-
}
|
|
301
|
-
if (isRecord(value.cause) && typeof value.cause.status === "number") {
|
|
302
|
-
return value.cause.status;
|
|
303
|
-
}
|
|
304
|
-
return undefined;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function httpHeaders(transport: QueueTransport): Record<string, string> {
|
|
308
|
-
const auth = authHeader();
|
|
309
|
-
return {
|
|
310
|
-
"Content-Type": "application/json",
|
|
311
|
-
...(auth ? { Authorization: auth } : {}),
|
|
312
|
-
...(transport.directory
|
|
313
|
-
? { "x-opencode-directory": encodeURIComponent(transport.directory) }
|
|
314
|
-
: {}),
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
async function sendQueueInProcess(
|
|
319
|
-
client: OpencodeClient,
|
|
320
|
-
sessionID: string,
|
|
321
|
-
body: QueueBody,
|
|
322
|
-
): Promise<PushResult> {
|
|
323
|
-
let result: unknown;
|
|
324
|
-
try {
|
|
325
|
-
result = await client.v2.session.prompt(
|
|
326
|
-
{ sessionID, ...body },
|
|
327
|
-
{ responseStyle: "fields", signal: AbortSignal.timeout(10_000) },
|
|
328
|
-
);
|
|
329
|
-
} catch (error) {
|
|
330
|
-
const status = statusOf(error);
|
|
331
|
-
return status === undefined || status === 404
|
|
332
|
-
? "unreachable"
|
|
333
|
-
: "rejected";
|
|
334
|
-
}
|
|
335
|
-
if (!isRecord(result)) {
|
|
336
|
-
return "unreachable";
|
|
337
|
-
}
|
|
338
|
-
if (!isRecord(result.response)) {
|
|
339
|
-
if (admissionMatches(result, body, sessionID)) {
|
|
340
|
-
return "delivered";
|
|
341
|
-
}
|
|
342
|
-
return "unreachable";
|
|
343
|
-
}
|
|
344
|
-
const status = result.response.status;
|
|
345
|
-
if (typeof status !== "number") {
|
|
346
|
-
return "unreachable";
|
|
347
|
-
}
|
|
348
|
-
if (status === 404) {
|
|
349
|
-
return "unreachable";
|
|
350
|
-
}
|
|
351
|
-
if (status < 200 || status >= 300) {
|
|
352
|
-
return "rejected";
|
|
353
|
-
}
|
|
354
|
-
return admissionMatches(result.data, body, sessionID)
|
|
355
|
-
? "delivered"
|
|
356
|
-
: "rejected";
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
async function sendQueueHttp(
|
|
360
|
-
transport: QueueTransport,
|
|
361
|
-
sessionID: string,
|
|
362
|
-
body: QueueBody,
|
|
363
|
-
): Promise<PushResult> {
|
|
364
|
-
const base = (transport.serverUrl || LOCAL_SERVER_URL).replace(/\/+$/, "");
|
|
365
|
-
let response: Response;
|
|
366
|
-
try {
|
|
367
|
-
response = await fetch(
|
|
368
|
-
`${base}/api/session/${encodeURIComponent(sessionID)}/prompt`,
|
|
369
|
-
{
|
|
370
|
-
method: "POST",
|
|
371
|
-
headers: httpHeaders(transport),
|
|
372
|
-
body: JSON.stringify(body),
|
|
373
|
-
signal: AbortSignal.timeout(10_000),
|
|
374
|
-
},
|
|
375
|
-
);
|
|
376
|
-
} catch {
|
|
377
|
-
return "unreachable";
|
|
378
|
-
}
|
|
379
|
-
const data = await response.json().catch(() => undefined);
|
|
380
|
-
if (response.status === 404) {
|
|
381
|
-
return "unreachable";
|
|
382
|
-
}
|
|
383
|
-
if (!response.ok) {
|
|
384
|
-
return "rejected";
|
|
385
|
-
}
|
|
386
|
-
return admissionMatches(data, body, sessionID) ? "delivered" : "rejected";
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
export async function sendQueue(
|
|
390
|
-
transport: QueueTransport,
|
|
391
|
-
sessionID: string,
|
|
392
|
-
text: string,
|
|
393
|
-
admissionID: string,
|
|
394
|
-
): Promise<PushResult> {
|
|
395
|
-
const body = queueBody(admissionID, text);
|
|
396
|
-
if (transport.client) {
|
|
397
|
-
const result = await sendQueueInProcess(transport.client, sessionID, body);
|
|
398
|
-
if (result !== "unreachable") {
|
|
399
|
-
return result;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
return sendQueueHttp(transport, sessionID, body);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// Overlapping hook and tool triggers must not double-deliver; keep the lock
|
|
406
|
-
// per session so a slow check cannot starve another session.
|
|
407
|
-
const inboxChecks = new Set<string>();
|
|
408
|
-
|
|
409
|
-
export async function checkCenterInbox(
|
|
410
|
-
opts: CenterOpts,
|
|
411
|
-
sessionID: string,
|
|
412
|
-
transport: QueueTransport,
|
|
413
|
-
): Promise<void> {
|
|
414
|
-
if (inboxChecks.has(sessionID)) {
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
inboxChecks.add(sessionID);
|
|
418
|
-
try {
|
|
419
|
-
const result = (await request(
|
|
420
|
-
opts,
|
|
421
|
-
`/inbox?sessionID=${encodeURIComponent(sessionID)}&excludeSelf=1`,
|
|
422
|
-
"GET",
|
|
423
|
-
)) as unknown as { member: boolean; items: InboxItem[] };
|
|
424
|
-
const items = Array.isArray(result.items) ? result.items : [];
|
|
425
|
-
if (items.length > 0) {
|
|
426
|
-
await pushItemsToSession(opts, sessionID, items, transport);
|
|
427
|
-
}
|
|
428
|
-
} catch {
|
|
429
|
-
// best-effort: next check retries
|
|
430
|
-
} finally {
|
|
431
|
-
inboxChecks.delete(sessionID);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// Items are grouped by room. A room watermark advances only after queue
|
|
436
|
-
// admission and a successful /read, so failures remain retryable.
|
|
437
|
-
const lastPushed = new Map<string, number>();
|
|
438
|
-
|
|
439
|
-
export async function pushItemsToSession(
|
|
440
|
-
opts: CenterOpts,
|
|
441
|
-
sessionID: string,
|
|
442
|
-
items: InboxItem[],
|
|
443
|
-
transport: QueueTransport,
|
|
444
|
-
): Promise<PushResult> {
|
|
445
|
-
if (items.length === 0) {
|
|
446
|
-
return "delivered";
|
|
447
|
-
}
|
|
448
|
-
const byRoom = new Map<string, InboxItem[]>();
|
|
449
|
-
for (const it of items) {
|
|
450
|
-
const list = byRoom.get(it.roomId) ?? [];
|
|
451
|
-
list.push(it);
|
|
452
|
-
byRoom.set(it.roomId, list);
|
|
453
|
-
}
|
|
454
|
-
let worst: PushResult = "delivered";
|
|
455
|
-
for (const [roomId, roomItems] of byRoom) {
|
|
456
|
-
const maxTs = roomItems.reduce(
|
|
457
|
-
(mx, it) => Math.max(mx, it.createdAt),
|
|
458
|
-
0,
|
|
459
|
-
);
|
|
460
|
-
const key = `${sessionID}|${roomId}`;
|
|
461
|
-
const existing = pendingAdmission(sessionID, roomId);
|
|
462
|
-
if (existing?.state === "admitted") {
|
|
463
|
-
if (existing.capped) {
|
|
464
|
-
if (maxTs <= existing.maxTs) {
|
|
465
|
-
continue;
|
|
466
|
-
}
|
|
467
|
-
} else if (existing.maxTs <= 0) {
|
|
468
|
-
continue;
|
|
469
|
-
} else {
|
|
470
|
-
const advanced = await request(opts, "/read", "POST", {
|
|
471
|
-
roomId,
|
|
472
|
-
sessionID,
|
|
473
|
-
ts: existing.maxTs,
|
|
474
|
-
})
|
|
475
|
-
.then(() => true)
|
|
476
|
-
.catch(() => false);
|
|
477
|
-
if (advanced) {
|
|
478
|
-
lastPushed.set(key, existing.maxTs);
|
|
479
|
-
clearPendingAdmission(sessionID, roomId, existing.maxTs);
|
|
480
|
-
}
|
|
481
|
-
continue;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
if (!existing && maxTs <= (lastPushed.get(key) ?? 0)) {
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
if (lastPushed.size > 1000) {
|
|
488
|
-
lastPushed.clear();
|
|
489
|
-
}
|
|
490
|
-
const { lines, capped } = formatNotificationLines(roomItems);
|
|
491
|
-
const prompt = `<notification>\nNew messages in room ${roomItems[0]!.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
492
|
-
const admission = getOrCreatePendingAdmission(
|
|
493
|
-
sessionID,
|
|
494
|
-
roomId,
|
|
495
|
-
roomItems,
|
|
496
|
-
prompt,
|
|
497
|
-
maxTs,
|
|
498
|
-
capped,
|
|
499
|
-
);
|
|
500
|
-
const result = await sendQueue(
|
|
501
|
-
transport,
|
|
502
|
-
sessionID,
|
|
503
|
-
admission.prompt,
|
|
504
|
-
admission.id,
|
|
505
|
-
);
|
|
506
|
-
if (result !== "delivered") {
|
|
507
|
-
if (result === "rejected") {
|
|
508
|
-
rejectPendingAdmission(sessionID, roomId, admission.id);
|
|
509
|
-
}
|
|
510
|
-
if (result === "rejected" || worst === "delivered") {
|
|
511
|
-
worst = result;
|
|
512
|
-
}
|
|
513
|
-
continue;
|
|
514
|
-
}
|
|
515
|
-
markPendingAdmissionAdmitted(sessionID, roomId, admission.id);
|
|
516
|
-
void request(opts, "/active", "POST", { sessionID }).catch(() => {});
|
|
517
|
-
if (admission.capped || admission.maxTs <= 0) {
|
|
518
|
-
continue;
|
|
519
|
-
}
|
|
520
|
-
const advanced = await request(opts, "/read", "POST", {
|
|
521
|
-
roomId,
|
|
522
|
-
sessionID,
|
|
523
|
-
ts: admission.maxTs,
|
|
524
|
-
})
|
|
525
|
-
.then(() => true)
|
|
526
|
-
.catch(() => false);
|
|
527
|
-
if (advanced) {
|
|
528
|
-
lastPushed.set(key, admission.maxTs);
|
|
529
|
-
clearPendingAdmission(sessionID, roomId, admission.maxTs);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
return worst;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
// ponytail: in-plugin wake watcher — sweep all rooms on the central server,
|
|
536
|
-
// find sessions with unread messages, and self-push queue notifications to
|
|
537
|
-
// them (opencode wakes the session in-process; dead sessions just fail
|
|
538
|
-
// silently). Runs on a timer; the central server itself has no opencode
|
|
539
|
-
// knowledge, this is purely client-side.
|
|
540
|
-
let wakeChecking = false;
|
|
541
|
-
|
|
542
|
-
// No process is spawned here. A configured opencode serve is only a fallback
|
|
543
|
-
// for mentioned sessions that cannot be admitted by the current process.
|
|
544
|
-
async function wakeViaServe(
|
|
545
|
-
serveUrl: string,
|
|
546
|
-
sessionID: string,
|
|
547
|
-
roomId: string,
|
|
548
|
-
opts: CenterOpts,
|
|
549
|
-
transport: QueueTransport,
|
|
550
|
-
admission: PendingAdmission,
|
|
551
|
-
): Promise<boolean> {
|
|
552
|
-
// ponytail: 与多窗口插件共用服务器原子去重,避免重复唤起同一 session
|
|
553
|
-
try {
|
|
554
|
-
const claim = (await fetch(`${opts.center}/wake-claim`, {
|
|
555
|
-
method: "POST",
|
|
556
|
-
headers: {
|
|
557
|
-
"Content-Type": "application/json",
|
|
558
|
-
...(opts.token ? { Authorization: `Bearer ${opts.token}` } : {}),
|
|
559
|
-
},
|
|
560
|
-
body: JSON.stringify({ sessionID }),
|
|
561
|
-
signal: AbortSignal.timeout(10_000),
|
|
562
|
-
}).then((r) => r.json().catch(() => ({})))) as { ok?: boolean };
|
|
563
|
-
if (!claim.ok) {
|
|
564
|
-
return false;
|
|
565
|
-
}
|
|
566
|
-
} catch {
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
|
-
const result = await sendQueue(
|
|
570
|
-
{ ...transport, client: undefined, serverUrl: serveUrl },
|
|
571
|
-
sessionID,
|
|
572
|
-
admission.prompt,
|
|
573
|
-
admission.id,
|
|
574
|
-
);
|
|
575
|
-
if (result === "delivered") {
|
|
576
|
-
void request(opts, "/active", "POST", { sessionID }).catch(() => {});
|
|
577
|
-
markPendingAdmissionAdmitted(sessionID, roomId, admission.id);
|
|
578
|
-
} else if (result === "rejected") {
|
|
579
|
-
rejectPendingAdmission(sessionID, roomId, admission.id);
|
|
580
|
-
}
|
|
581
|
-
return result === "delivered";
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
export async function wakeSessions(
|
|
585
|
-
opts: CenterOpts,
|
|
586
|
-
transport: QueueTransport,
|
|
587
|
-
serveUrl = "",
|
|
588
|
-
): Promise<void> {
|
|
589
|
-
if (wakeChecking) {
|
|
590
|
-
return;
|
|
591
|
-
}
|
|
592
|
-
wakeChecking = true;
|
|
593
|
-
try {
|
|
594
|
-
// ponytail: 轻量摘要(orc-1 #5)——不下载全部房间历史
|
|
595
|
-
const rooms = (await request(
|
|
596
|
-
opts,
|
|
597
|
-
"/rooms/summary",
|
|
598
|
-
"GET",
|
|
599
|
-
)) as unknown as Array<{ id: string; name: string; latestMsgTs: number }>;
|
|
600
|
-
if (!Array.isArray(rooms)) {
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
for (const room of rooms) {
|
|
604
|
-
try {
|
|
605
|
-
const roomId = String(room.id ?? "");
|
|
606
|
-
const latest = Number(room.latestMsgTs ?? 0);
|
|
607
|
-
if (!roomId || latest === 0) {
|
|
608
|
-
continue;
|
|
609
|
-
}
|
|
610
|
-
const reg = (await request(
|
|
611
|
-
opts,
|
|
612
|
-
`/rooms/${encodeURIComponent(roomId)}/registry`,
|
|
613
|
-
"GET",
|
|
614
|
-
)) as unknown as Record<
|
|
615
|
-
string,
|
|
616
|
-
{
|
|
617
|
-
name: string;
|
|
618
|
-
lastReadTs?: number;
|
|
619
|
-
host?: string;
|
|
620
|
-
directory?: string;
|
|
621
|
-
}
|
|
622
|
-
>;
|
|
623
|
-
if (!reg || typeof reg !== "object") {
|
|
624
|
-
continue;
|
|
625
|
-
}
|
|
626
|
-
for (const [sessionID, entry] of Object.entries(reg)) {
|
|
627
|
-
// ponytail: 网页/模拟条目(host 空)不是 opencode 会话——不扫描
|
|
628
|
-
// 不推送(否则死条目每 10s 一次 404 推送,永远 churn,orc-1 #7)
|
|
629
|
-
if (!entry.host) {
|
|
630
|
-
continue;
|
|
631
|
-
}
|
|
632
|
-
// ponytail: orc-2 #6——跨 host 条目由各自机器上的插件负责,本机
|
|
633
|
-
// 扫描+推送只会 404(lastPushed 只在 res.ok 时设置,永不去重)
|
|
634
|
-
if (entry.host !== os.hostname()) {
|
|
635
|
-
continue;
|
|
636
|
-
}
|
|
637
|
-
// quick skip: nothing newer than this session's watermark
|
|
638
|
-
if (latest <= (entry.lastReadTs ?? 0)) {
|
|
639
|
-
continue;
|
|
640
|
-
}
|
|
641
|
-
const result = (await request(
|
|
642
|
-
opts,
|
|
643
|
-
`/inbox?sessionID=${encodeURIComponent(sessionID)}&roomId=${encodeURIComponent(roomId)}&excludeSelf=1&timeout=0`,
|
|
644
|
-
"GET",
|
|
645
|
-
)) as unknown as { member: boolean; items: InboxItem[] };
|
|
646
|
-
const items = Array.isArray(result.items)
|
|
647
|
-
? result.items.filter((item) => item.roomId === roomId)
|
|
648
|
-
: [];
|
|
649
|
-
if (items.length > 0) {
|
|
650
|
-
const targetTransport = {
|
|
651
|
-
...transport,
|
|
652
|
-
directory: entry.directory ?? transport.directory,
|
|
653
|
-
};
|
|
654
|
-
// ponytail: ora-1 #2 — @-wake 只由 session 归属主机执行;web 条目
|
|
655
|
-
// (host 为空)与异机条目绝不 spawn(否则错机 spawn 一个不存在
|
|
656
|
-
// 的 session,且 5 分钟 claim 窗口会挡住真正正确的那台机器)
|
|
657
|
-
const pushed = await pushItemsToSession(
|
|
658
|
-
opts,
|
|
659
|
-
sessionID,
|
|
660
|
-
items,
|
|
661
|
-
targetTransport,
|
|
662
|
-
);
|
|
663
|
-
// 被 @ 的已退出 session:serve 配置了才唤起(进程内,零 spawn)。
|
|
664
|
-
// 唤起成功即推进水位——消息已进入该 session 的对话(不丢),
|
|
665
|
-
// 避免同一未读反复唤起导致循环回复。
|
|
666
|
-
// 仅唤起本机 opencode session(host === 本机主机名)——web 条目
|
|
667
|
-
// 与 curl 模拟成员(host 空)没有可恢复的 opencode 会话
|
|
668
|
-
if (
|
|
669
|
-
pushed === "unreachable" &&
|
|
670
|
-
serveUrl &&
|
|
671
|
-
entry.host === os.hostname()
|
|
672
|
-
) {
|
|
673
|
-
const mentioned = items.some((it) => {
|
|
674
|
-
if (it.text.includes(`<at>@${entry.name}</at>`)) return true;
|
|
675
|
-
const esc = entry.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
676
|
-
return new RegExp(`@${esc}(?![\\p{L}\\p{N}_])`, "u").test(
|
|
677
|
-
it.text,
|
|
678
|
-
);
|
|
679
|
-
});
|
|
680
|
-
if (mentioned) {
|
|
681
|
-
const { lines, capped } = formatNotificationLines(items);
|
|
682
|
-
const prompt = formatServeWakePrompt(
|
|
683
|
-
items[0]!.roomName,
|
|
684
|
-
lines,
|
|
685
|
-
);
|
|
686
|
-
const maxTs = items.reduce(
|
|
687
|
-
(mx, it) => Math.max(mx, it.createdAt),
|
|
688
|
-
0,
|
|
689
|
-
);
|
|
690
|
-
const admission = getOrCreatePendingAdmission(
|
|
691
|
-
sessionID,
|
|
692
|
-
roomId,
|
|
693
|
-
items,
|
|
694
|
-
prompt,
|
|
695
|
-
maxTs,
|
|
696
|
-
capped,
|
|
697
|
-
);
|
|
698
|
-
const woke = await wakeViaServe(
|
|
699
|
-
serveUrl,
|
|
700
|
-
sessionID,
|
|
701
|
-
roomId,
|
|
702
|
-
opts,
|
|
703
|
-
targetTransport,
|
|
704
|
-
admission,
|
|
705
|
-
);
|
|
706
|
-
if (woke && !admission.capped && admission.maxTs > 0) {
|
|
707
|
-
const advanced = await request(opts, "/read", "POST", {
|
|
708
|
-
roomId,
|
|
709
|
-
sessionID,
|
|
710
|
-
ts: admission.maxTs,
|
|
711
|
-
})
|
|
712
|
-
.then(() => true)
|
|
713
|
-
.catch(() => false);
|
|
714
|
-
if (advanced) {
|
|
715
|
-
clearPendingAdmission(sessionID, roomId, admission.maxTs);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
} catch {
|
|
723
|
-
// ponytail: orc-3 #4——单房间错误(registry/inbox 请求失败)只跳过
|
|
724
|
-
// 该房间,不中止整个 sweep
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
} catch {
|
|
728
|
-
// best-effort: next sweep retries
|
|
729
|
-
} finally {
|
|
730
|
-
wakeChecking = false;
|
|
731
|
-
}
|
|
732
|
-
}
|