@alexsun-top/opencode-chat-room 0.1.0 → 0.1.2
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 +16 -8
- package/README.zh-CN.md +16 -8
- package/cli.ts +95 -1
- package/package.json +2 -1
- package/plugin.ts +230 -73
- package/server/chat-server.ts +149 -43
- package/server/chat.html +350 -95
- package/src/center-client.ts +324 -122
- package/src/queue.ts +186 -0
- package/src/room-service.ts +11 -4
- package/src/store.ts +18 -6
package/src/center-client.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ponytail: central-server mode client. All room state lives on the central
|
|
2
|
-
// server (CHAT_ROOM_SERVER_URL); this machine
|
|
3
|
-
//
|
|
4
|
-
|
|
2
|
+
// server (CHAT_ROOM_SERVER_URL); this machine pushes queue notifications to
|
|
3
|
+
// sessions owned by this OpenCode process before using an HTTP fallback.
|
|
4
|
+
import { OpencodeClient } from "@opencode-ai/sdk/v2";
|
|
5
5
|
import RoomService, {
|
|
6
6
|
type Message,
|
|
7
7
|
type Participant,
|
|
@@ -10,16 +10,72 @@ import RoomService, {
|
|
|
10
10
|
import os from "node:os";
|
|
11
11
|
import {
|
|
12
12
|
authHeader,
|
|
13
|
+
formatServeWakePrompt,
|
|
13
14
|
formatNotificationLines,
|
|
14
15
|
formatRoom,
|
|
15
16
|
identity,
|
|
16
17
|
NOTIFY_INSTRUCTION,
|
|
17
|
-
SERVE_WAKE_PROMPT,
|
|
18
18
|
timeOf,
|
|
19
19
|
} from "./store";
|
|
20
|
+
import {
|
|
21
|
+
admissionMatches,
|
|
22
|
+
clearPendingAdmission,
|
|
23
|
+
getOrCreatePendingAdmission,
|
|
24
|
+
markPendingAdmissionAdmitted,
|
|
25
|
+
pendingAdmission,
|
|
26
|
+
rejectPendingAdmission,
|
|
27
|
+
type QueueBody,
|
|
28
|
+
type QueueItem,
|
|
29
|
+
type PendingAdmission,
|
|
30
|
+
} from "./queue";
|
|
31
|
+
export { queueAdmissionId } from "./queue";
|
|
20
32
|
|
|
21
33
|
type Json = Record<string, unknown>;
|
|
22
34
|
|
|
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
|
+
|
|
23
79
|
function toRoom(r: Json): Room {
|
|
24
80
|
const participants = new Map<string, Participant>();
|
|
25
81
|
if (Array.isArray(r.participants)) {
|
|
@@ -45,7 +101,7 @@ function svcOf(room: Json): RoomService {
|
|
|
45
101
|
return svc;
|
|
46
102
|
}
|
|
47
103
|
|
|
48
|
-
type CenterOpts = { center: string; token: string };
|
|
104
|
+
type CenterOpts = { center: string; token: string; directory?: string };
|
|
49
105
|
|
|
50
106
|
async function request(
|
|
51
107
|
opts: CenterOpts,
|
|
@@ -110,6 +166,7 @@ export async function centerAction(
|
|
|
110
166
|
sessionID,
|
|
111
167
|
// ponytail: ora-1 #2 — 记录归属主机,跨机 @-wake 只由所属机器唤醒
|
|
112
168
|
host: os.hostname(),
|
|
169
|
+
directory: opts.directory,
|
|
113
170
|
});
|
|
114
171
|
return formatRoom(svcOf(room), toRoom(room));
|
|
115
172
|
}
|
|
@@ -128,6 +185,7 @@ export async function centerAction(
|
|
|
128
185
|
participantName: me,
|
|
129
186
|
// ponytail: ora-1 #2 — 记录归属主机,跨机 @-wake 只由所属机器唤醒
|
|
130
187
|
host: os.hostname(),
|
|
188
|
+
directory: opts.directory,
|
|
131
189
|
},
|
|
132
190
|
);
|
|
133
191
|
return formatRoom(svcOf(room), toRoom(room));
|
|
@@ -202,7 +260,9 @@ export async function centerAction(
|
|
|
202
260
|
roomId: args.roomId,
|
|
203
261
|
sessionID,
|
|
204
262
|
ts: maxTs,
|
|
205
|
-
})
|
|
263
|
+
})
|
|
264
|
+
.then(() => clearPendingAdmission(sessionID, args.roomId!, maxTs))
|
|
265
|
+
.catch(() => {});
|
|
206
266
|
return lines.join("\n");
|
|
207
267
|
}
|
|
208
268
|
default:
|
|
@@ -210,26 +270,146 @@ export async function centerAction(
|
|
|
210
270
|
}
|
|
211
271
|
}
|
|
212
272
|
|
|
213
|
-
type InboxItem = {
|
|
273
|
+
type InboxItem = QueueItem & {
|
|
214
274
|
roomId: string;
|
|
215
275
|
roomName: string;
|
|
216
|
-
senderId: string;
|
|
217
|
-
text: string;
|
|
218
|
-
createdAt: number;
|
|
219
276
|
};
|
|
220
277
|
|
|
221
|
-
|
|
222
|
-
|
|
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.
|
|
223
407
|
const inboxChecks = new Set<string>();
|
|
224
408
|
|
|
225
|
-
// ponytail: central mode has no cross-host push; each session pulls its inbox
|
|
226
|
-
// on every hook/tool call and self-pushes queue notifications (localhost).
|
|
227
|
-
// Items are grouped by room: a room's watermark advances only when every item
|
|
228
|
-
// in that room was pushed successfully, so a failed push is retried next time.
|
|
229
409
|
export async function checkCenterInbox(
|
|
230
410
|
opts: CenterOpts,
|
|
231
411
|
sessionID: string,
|
|
232
|
-
|
|
412
|
+
transport: QueueTransport,
|
|
233
413
|
): Promise<void> {
|
|
234
414
|
if (inboxChecks.has(sessionID)) {
|
|
235
415
|
return;
|
|
@@ -243,7 +423,7 @@ export async function checkCenterInbox(
|
|
|
243
423
|
)) as unknown as { member: boolean; items: InboxItem[] };
|
|
244
424
|
const items = Array.isArray(result.items) ? result.items : [];
|
|
245
425
|
if (items.length > 0) {
|
|
246
|
-
await pushItemsToSession(opts, sessionID, items,
|
|
426
|
+
await pushItemsToSession(opts, sessionID, items, transport);
|
|
247
427
|
}
|
|
248
428
|
} catch {
|
|
249
429
|
// best-effort: next check retries
|
|
@@ -251,26 +431,16 @@ export async function checkCenterInbox(
|
|
|
251
431
|
inboxChecks.delete(sessionID);
|
|
252
432
|
}
|
|
253
433
|
}
|
|
254
|
-
// ponytail: push a batch of inbox items to one session (grouped per room —
|
|
255
|
-
// a room's watermark advances only when every item in it was pushed
|
|
256
|
-
// successfully, so a failed push is retried next time).
|
|
257
|
-
// Result: "delivered" = all 2xx; "unreachable" = connection refused or 404
|
|
258
|
-
// (the session's process is very likely gone — the only case worth spawning
|
|
259
|
-
// a replacement process for); "rejected" = server alive but refused (401/5xx
|
|
260
|
-
// — do NOT spawn, the session may live in another window).
|
|
261
|
-
export type PushResult = "delivered" | "unreachable" | "rejected";
|
|
262
434
|
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
// deliberately don't advance the watermark) would otherwise re-notify every
|
|
266
|
-
// sweep forever (orc-1 #4)
|
|
435
|
+
// Items are grouped by room. A room watermark advances only after queue
|
|
436
|
+
// admission and a successful /read, so failures remain retryable.
|
|
267
437
|
const lastPushed = new Map<string, number>();
|
|
268
438
|
|
|
269
439
|
export async function pushItemsToSession(
|
|
270
440
|
opts: CenterOpts,
|
|
271
441
|
sessionID: string,
|
|
272
442
|
items: InboxItem[],
|
|
273
|
-
|
|
443
|
+
transport: QueueTransport,
|
|
274
444
|
): Promise<PushResult> {
|
|
275
445
|
if (items.length === 0) {
|
|
276
446
|
return "delivered";
|
|
@@ -281,8 +451,6 @@ export async function pushItemsToSession(
|
|
|
281
451
|
list.push(it);
|
|
282
452
|
byRoom.set(it.roomId, list);
|
|
283
453
|
}
|
|
284
|
-
const auth = authHeader();
|
|
285
|
-
const base = selfServerUrl.replace(/\/+$/, "");
|
|
286
454
|
let worst: PushResult = "delivered";
|
|
287
455
|
for (const [roomId, roomItems] of byRoom) {
|
|
288
456
|
const maxTs = roomItems.reduce(
|
|
@@ -290,56 +458,75 @@ export async function pushItemsToSession(
|
|
|
290
458
|
0,
|
|
291
459
|
);
|
|
292
460
|
const key = `${sessionID}|${roomId}`;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
// ponytail: network errors (e.g. connection refused — the session's
|
|
302
|
-
// process has exited) count as push failure, NOT as a crash: a rejected
|
|
303
|
-
// fetch here would abort the whole wake sweep and skip every other
|
|
304
|
-
// session, and the @-wake spawn would never run
|
|
305
|
-
let res: Response;
|
|
306
|
-
try {
|
|
307
|
-
res = await fetch(`${base}/api/session/${sessionID}/prompt`, {
|
|
308
|
-
method: "POST",
|
|
309
|
-
headers: {
|
|
310
|
-
"Content-Type": "application/json",
|
|
311
|
-
...(auth ? { Authorization: auth } : {}),
|
|
312
|
-
},
|
|
313
|
-
body: JSON.stringify({ prompt, delivery: "queue" }),
|
|
314
|
-
});
|
|
315
|
-
} catch {
|
|
316
|
-
worst = "unreachable";
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
// ponytail: a capped push must not advance the watermark — the
|
|
320
|
-
// folded-out messages would become permanently unreadable; the
|
|
321
|
-
// uncapped poll/inbox path remains the lossless catch-up
|
|
322
|
-
if (res.ok) {
|
|
323
|
-
if (capped) {
|
|
324
|
-
lastPushed.set(key, maxTs);
|
|
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;
|
|
325
469
|
} else {
|
|
326
|
-
// orc-3 #6——/read 失败时不得记 lastPushed:水位未推进,下次 sweep
|
|
327
|
-
// 重推(否则推送成功但水位未动 → 推送路径对该批消息永久静默)
|
|
328
470
|
const advanced = await request(opts, "/read", "POST", {
|
|
329
471
|
roomId,
|
|
330
472
|
sessionID,
|
|
331
|
-
ts: maxTs,
|
|
473
|
+
ts: existing.maxTs,
|
|
332
474
|
})
|
|
333
475
|
.then(() => true)
|
|
334
476
|
.catch(() => false);
|
|
335
|
-
if (advanced)
|
|
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;
|
|
336
512
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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);
|
|
343
530
|
}
|
|
344
531
|
}
|
|
345
532
|
return worst;
|
|
@@ -352,16 +539,15 @@ export async function pushItemsToSession(
|
|
|
352
539
|
// knowledge, this is purely client-side.
|
|
353
540
|
let wakeChecking = false;
|
|
354
541
|
|
|
355
|
-
//
|
|
356
|
-
//
|
|
357
|
-
// 对被 @ 的已退出 session 通过 serve 的 /session/:id/message 进程内唤起;
|
|
358
|
-
// serve 未配置/不可达 → 不唤醒(未读由水位保留,session 下次运行时 poll
|
|
359
|
-
// 自动拿到)
|
|
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.
|
|
360
544
|
async function wakeViaServe(
|
|
361
545
|
serveUrl: string,
|
|
362
546
|
sessionID: string,
|
|
547
|
+
roomId: string,
|
|
363
548
|
opts: CenterOpts,
|
|
364
|
-
|
|
549
|
+
transport: QueueTransport,
|
|
550
|
+
admission: PendingAdmission,
|
|
365
551
|
): Promise<boolean> {
|
|
366
552
|
// ponytail: 与多窗口插件共用服务器原子去重,避免重复唤起同一 session
|
|
367
553
|
try {
|
|
@@ -380,26 +566,24 @@ async function wakeViaServe(
|
|
|
380
566
|
} catch {
|
|
381
567
|
return false;
|
|
382
568
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
} catch {
|
|
395
|
-
// serve 未启动/不可达——按用户要求不唤醒(无 spawn、无重试)
|
|
396
|
-
return false;
|
|
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);
|
|
397
580
|
}
|
|
581
|
+
return result === "delivered";
|
|
398
582
|
}
|
|
399
583
|
|
|
400
584
|
export async function wakeSessions(
|
|
401
585
|
opts: CenterOpts,
|
|
402
|
-
|
|
586
|
+
transport: QueueTransport,
|
|
403
587
|
serveUrl = "",
|
|
404
588
|
): Promise<void> {
|
|
405
589
|
if (wakeChecking) {
|
|
@@ -429,7 +613,12 @@ export async function wakeSessions(
|
|
|
429
613
|
"GET",
|
|
430
614
|
)) as unknown as Record<
|
|
431
615
|
string,
|
|
432
|
-
{
|
|
616
|
+
{
|
|
617
|
+
name: string;
|
|
618
|
+
lastReadTs?: number;
|
|
619
|
+
host?: string;
|
|
620
|
+
directory?: string;
|
|
621
|
+
}
|
|
433
622
|
>;
|
|
434
623
|
if (!reg || typeof reg !== "object") {
|
|
435
624
|
continue;
|
|
@@ -451,11 +640,17 @@ export async function wakeSessions(
|
|
|
451
640
|
}
|
|
452
641
|
const result = (await request(
|
|
453
642
|
opts,
|
|
454
|
-
`/inbox?sessionID=${encodeURIComponent(sessionID)}&excludeSelf=1&timeout=0`,
|
|
643
|
+
`/inbox?sessionID=${encodeURIComponent(sessionID)}&roomId=${encodeURIComponent(roomId)}&excludeSelf=1&timeout=0`,
|
|
455
644
|
"GET",
|
|
456
645
|
)) as unknown as { member: boolean; items: InboxItem[] };
|
|
457
|
-
const items = Array.isArray(result.items)
|
|
646
|
+
const items = Array.isArray(result.items)
|
|
647
|
+
? result.items.filter((item) => item.roomId === roomId)
|
|
648
|
+
: [];
|
|
458
649
|
if (items.length > 0) {
|
|
650
|
+
const targetTransport = {
|
|
651
|
+
...transport,
|
|
652
|
+
directory: entry.directory ?? transport.directory,
|
|
653
|
+
};
|
|
459
654
|
// ponytail: ora-1 #2 — @-wake 只由 session 归属主机执行;web 条目
|
|
460
655
|
// (host 为空)与异机条目绝不 spawn(否则错机 spawn 一个不存在
|
|
461
656
|
// 的 session,且 5 分钟 claim 窗口会挡住真正正确的那台机器)
|
|
@@ -463,15 +658,8 @@ export async function wakeSessions(
|
|
|
463
658
|
opts,
|
|
464
659
|
sessionID,
|
|
465
660
|
items,
|
|
466
|
-
|
|
661
|
+
targetTransport,
|
|
467
662
|
);
|
|
468
|
-
if (pushed === "delivered") {
|
|
469
|
-
// ponytail: 进程内自推成功 = 该 session 真实活跃——上报服务器,
|
|
470
|
-
// 避免服务器 serve 双唤起
|
|
471
|
-
void request(opts, "/active", "POST", { sessionID }).catch(
|
|
472
|
-
() => {},
|
|
473
|
-
);
|
|
474
|
-
}
|
|
475
663
|
// 被 @ 的已退出 session:serve 配置了才唤起(进程内,零 spawn)。
|
|
476
664
|
// 唤起成功即推进水位——消息已进入该 session 的对话(不丢),
|
|
477
665
|
// 避免同一未读反复唤起导致循环回复。
|
|
@@ -490,28 +678,42 @@ export async function wakeSessions(
|
|
|
490
678
|
);
|
|
491
679
|
});
|
|
492
680
|
if (mentioned) {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
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
|
+
);
|
|
499
698
|
const woke = await wakeViaServe(
|
|
500
699
|
serveUrl,
|
|
501
700
|
sessionID,
|
|
701
|
+
roomId,
|
|
502
702
|
opts,
|
|
503
|
-
|
|
703
|
+
targetTransport,
|
|
704
|
+
admission,
|
|
504
705
|
);
|
|
505
|
-
if (woke) {
|
|
506
|
-
const
|
|
507
|
-
(mx, it) => Math.max(mx, it.createdAt),
|
|
508
|
-
0,
|
|
509
|
-
);
|
|
510
|
-
await request(opts, "/read", "POST", {
|
|
706
|
+
if (woke && !admission.capped && admission.maxTs > 0) {
|
|
707
|
+
const advanced = await request(opts, "/read", "POST", {
|
|
511
708
|
roomId,
|
|
512
709
|
sessionID,
|
|
513
|
-
ts: maxTs,
|
|
514
|
-
})
|
|
710
|
+
ts: admission.maxTs,
|
|
711
|
+
})
|
|
712
|
+
.then(() => true)
|
|
713
|
+
.catch(() => false);
|
|
714
|
+
if (advanced) {
|
|
715
|
+
clearPendingAdmission(sessionID, roomId, admission.maxTs);
|
|
716
|
+
}
|
|
515
717
|
}
|
|
516
718
|
}
|
|
517
719
|
}
|