@alexsun-top/opencode-chat-room 0.1.2 → 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 +1 -1
- package/plugin.ts +58 -20
- package/server/chat-server.ts +128 -41
- package/src/center-client.ts +142 -84
- package/src/queue.ts +14 -9
- package/src/store.ts +1 -0
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
package/plugin.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
clearPendingAdmission,
|
|
32
32
|
getOrCreatePendingAdmission,
|
|
33
33
|
markPendingAdmissionAdmitted,
|
|
34
|
+
pendingAdmission,
|
|
34
35
|
rejectPendingAdmission,
|
|
35
36
|
} from "./src/queue";
|
|
36
37
|
|
|
@@ -97,6 +98,7 @@ async function registerSelf(
|
|
|
97
98
|
lastReadTs:
|
|
98
99
|
regRoom[sessionID]?.lastReadTs ??
|
|
99
100
|
Math.max(Date.now(), latestMessageTs),
|
|
101
|
+
lastQueuedTs: regRoom[sessionID]?.lastQueuedTs,
|
|
100
102
|
};
|
|
101
103
|
await saveRegistry(reg);
|
|
102
104
|
} catch {
|
|
@@ -172,6 +174,28 @@ async function bumpLastRead(
|
|
|
172
174
|
});
|
|
173
175
|
}
|
|
174
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;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
175
199
|
// ponytail: one push carries EVERY unread message for the target (not just
|
|
176
200
|
// the latest), so advancing the watermark on success never skips a message
|
|
177
201
|
// whose earlier push failed. Event-only pushes (empty messages) don't bump.
|
|
@@ -209,21 +233,44 @@ async function notifyTarget(
|
|
|
209
233
|
}
|
|
210
234
|
const unread = batch.messages.filter(
|
|
211
235
|
(m) =>
|
|
212
|
-
m.createdAt >
|
|
236
|
+
m.createdAt >
|
|
237
|
+
Math.max(entry.lastReadTs ?? 0, entry.lastQueuedTs ?? 0) &&
|
|
213
238
|
(m.senderSessionID !== undefined
|
|
214
239
|
? m.senderSessionID !== sessionID
|
|
215
240
|
: m.senderId !== entry.name),
|
|
216
241
|
);
|
|
217
|
-
const
|
|
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);
|
|
218
264
|
const prompt =
|
|
219
|
-
|
|
265
|
+
deltaItems.length > 0
|
|
220
266
|
? `<notification>\nNew messages in room ${batch.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`
|
|
221
267
|
: `<notification>\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
222
|
-
const maxTs =
|
|
268
|
+
const maxTs = deltaItems.reduce(
|
|
223
269
|
(mx, message) => Math.max(mx, message.createdAt),
|
|
224
270
|
0,
|
|
225
271
|
);
|
|
226
|
-
const admissionItems =
|
|
272
|
+
const admissionItems =
|
|
273
|
+
deltaItems.length > 0 ? deltaItems : batch.messages.slice(-1);
|
|
227
274
|
const admission = getOrCreatePendingAdmission(
|
|
228
275
|
sessionID,
|
|
229
276
|
batch.roomId,
|
|
@@ -232,21 +279,12 @@ async function notifyTarget(
|
|
|
232
279
|
maxTs,
|
|
233
280
|
capped,
|
|
234
281
|
);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const url = `${base.origin}/${prefix}/session/${sessionID}/prompt`;
|
|
238
|
-
if (admission.state === "admitted") {
|
|
239
|
-
if (admission.capped) {
|
|
240
|
-
if (maxTs <= admission.maxTs) {
|
|
241
|
-
return { url, status: 200 };
|
|
242
|
-
}
|
|
243
|
-
} else {
|
|
244
|
-
if (admission.maxTs <= 0) {
|
|
245
|
-
return { url, status: 200 };
|
|
246
|
-
}
|
|
247
|
-
await bumpLastRead(batch.roomId, sessionID, admission.maxTs);
|
|
282
|
+
if (admission.state === "admitted" && maxTs <= admission.maxTs) {
|
|
283
|
+
if (admission.maxTs <= 0) {
|
|
248
284
|
return { url, status: 200 };
|
|
249
285
|
}
|
|
286
|
+
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
287
|
+
return { url, status: 200 };
|
|
250
288
|
}
|
|
251
289
|
const result = await sendQueue(
|
|
252
290
|
{
|
|
@@ -265,8 +303,8 @@ async function notifyTarget(
|
|
|
265
303
|
return { url, status: result === "unreachable" ? 0 : 400 };
|
|
266
304
|
}
|
|
267
305
|
markPendingAdmissionAdmitted(sessionID, batch.roomId, admission.id);
|
|
268
|
-
if (admission.maxTs > 0
|
|
269
|
-
await
|
|
306
|
+
if (admission.maxTs > 0) {
|
|
307
|
+
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
270
308
|
}
|
|
271
309
|
return { url, status: 200 };
|
|
272
310
|
}
|
package/server/chat-server.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
clearPendingAdmission,
|
|
26
26
|
getOrCreatePendingAdmission,
|
|
27
27
|
markPendingAdmissionAdmitted,
|
|
28
|
+
pendingAdmission,
|
|
28
29
|
rejectPendingAdmission,
|
|
29
30
|
type QueueBody,
|
|
30
31
|
type QueueItem,
|
|
@@ -122,7 +123,9 @@ async function wakeMentionedSessions(
|
|
|
122
123
|
sid === senderSessionID ||
|
|
123
124
|
!mentions.has(entry.name) || // 只唤起被 @ 点名的人
|
|
124
125
|
entry.host !== os.hostname() || // 只唤起本机 session(serve 只能恢复本机)
|
|
125
|
-
(activeSessions.get(sid) ?? 0) > now - WAKE_INACTIVE_MS // 活跃中——插件推送处理
|
|
126
|
+
(activeSessions.get(sid) ?? 0) > now - WAKE_INACTIVE_MS || // 活跃中——插件推送处理
|
|
127
|
+
triggeringMessage.createdAt <= (entry.lastReadTs ?? 0) ||
|
|
128
|
+
triggeringMessage.createdAt <= (entry.lastQueuedTs ?? 0)
|
|
126
129
|
) {
|
|
127
130
|
continue;
|
|
128
131
|
}
|
|
@@ -131,10 +134,29 @@ async function wakeMentionedSessions(
|
|
|
131
134
|
}
|
|
132
135
|
void (async () => {
|
|
133
136
|
try {
|
|
134
|
-
// orc-3 #2——带该 session
|
|
135
|
-
// maxTs
|
|
136
|
-
// 水位已推进而跳过——永久丢失)
|
|
137
|
+
// orc-3 #2——带该 session 的全部待通知消息(含更早的),队列游标
|
|
138
|
+
// 推进到 maxTs;lastReadTs 保留给 poll,避免未读消息被跳过
|
|
137
139
|
const inbox = await readInbox(sid, roomId, true);
|
|
140
|
+
const currentEntry = await enqueue(async () => {
|
|
141
|
+
const r = await loadRegistry();
|
|
142
|
+
return r[roomId]?.[sid];
|
|
143
|
+
});
|
|
144
|
+
if (!currentEntry || !inbox.member) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const queuedTs = Math.max(
|
|
148
|
+
currentEntry.lastQueuedTs ?? 0,
|
|
149
|
+
inbox.queuedTs[roomId] ?? 0,
|
|
150
|
+
);
|
|
151
|
+
if (
|
|
152
|
+
triggeringMessage.createdAt <= (currentEntry.lastReadTs ?? 0) ||
|
|
153
|
+
triggeringMessage.createdAt <= queuedTs
|
|
154
|
+
) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const queuedItems = inbox.items.filter(
|
|
158
|
+
(item) => item.createdAt > queuedTs,
|
|
159
|
+
);
|
|
138
160
|
const triggerItem: InboxItem = {
|
|
139
161
|
id: triggeringMessage.id,
|
|
140
162
|
roomId,
|
|
@@ -143,18 +165,47 @@ async function wakeMentionedSessions(
|
|
|
143
165
|
text: triggeringMessage.text,
|
|
144
166
|
createdAt: triggeringMessage.createdAt,
|
|
145
167
|
};
|
|
146
|
-
const items =
|
|
147
|
-
?
|
|
148
|
-
: [...
|
|
168
|
+
const items = queuedItems.some((item) => item.id === triggerItem.id)
|
|
169
|
+
? queuedItems
|
|
170
|
+
: [...queuedItems, triggerItem].sort(
|
|
149
171
|
(a, b) => a.createdAt - b.createdAt,
|
|
150
172
|
);
|
|
151
|
-
const
|
|
173
|
+
const currentMaxTs = items.reduce(
|
|
174
|
+
(mx, it) => Math.max(mx, it.createdAt),
|
|
175
|
+
0,
|
|
176
|
+
);
|
|
177
|
+
const existing = pendingAdmission(sid, roomId);
|
|
178
|
+
const deltaItems =
|
|
179
|
+
existing?.state === "admitted" && currentMaxTs > existing.maxTs
|
|
180
|
+
? items.filter((item) => item.createdAt > existing.maxTs)
|
|
181
|
+
: items;
|
|
182
|
+
const confirmQueued = async (ts: number): Promise<boolean> =>
|
|
183
|
+
enqueue(async () => {
|
|
184
|
+
const r = await loadRegistry();
|
|
185
|
+
const e = r[roomId]?.[sid];
|
|
186
|
+
if (!e) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
e.lastQueuedTs = Math.max(e.lastQueuedTs ?? 0, ts);
|
|
190
|
+
await saveRegistry(r);
|
|
191
|
+
return true;
|
|
192
|
+
});
|
|
193
|
+
if (
|
|
194
|
+
existing?.state === "admitted" &&
|
|
195
|
+
(currentMaxTs <= existing.maxTs || deltaItems.length === 0)
|
|
196
|
+
) {
|
|
197
|
+
if (existing.maxTs > 0 && (await confirmQueued(existing.maxTs))) {
|
|
198
|
+
clearPendingAdmission(sid, roomId, existing.maxTs);
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const { lines, capped } = formatNotificationLines(deltaItems);
|
|
152
203
|
const prompt = formatServeWakePrompt(roomName, lines);
|
|
153
|
-
const maxTs =
|
|
204
|
+
const maxTs = deltaItems.reduce(
|
|
154
205
|
(mx, it) => Math.max(mx, it.createdAt),
|
|
155
206
|
0,
|
|
156
207
|
);
|
|
157
|
-
const admissionItems: QueueItem[] =
|
|
208
|
+
const admissionItems: QueueItem[] = deltaItems;
|
|
158
209
|
const admission = getOrCreatePendingAdmission(
|
|
159
210
|
sid,
|
|
160
211
|
roomId,
|
|
@@ -169,28 +220,11 @@ async function wakeMentionedSessions(
|
|
|
169
220
|
delivery: "queue",
|
|
170
221
|
resume: true,
|
|
171
222
|
};
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const e = r[roomId]?.[sid];
|
|
176
|
-
if (!e) {
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
|
-
e.lastReadTs = Math.max(e.lastReadTs ?? 0, admission.maxTs);
|
|
180
|
-
await saveRegistry(r);
|
|
181
|
-
return true;
|
|
182
|
-
});
|
|
183
|
-
if (admission.state === "admitted") {
|
|
184
|
-
if (admission.capped) {
|
|
185
|
-
if (maxTs <= admission.maxTs) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
} else {
|
|
189
|
-
if (admission.maxTs > 0 && (await confirmRead())) {
|
|
190
|
-
clearPendingAdmission(sid, roomId, admission.maxTs);
|
|
191
|
-
}
|
|
192
|
-
return;
|
|
223
|
+
if (admission.state === "admitted" && maxTs <= admission.maxTs) {
|
|
224
|
+
if (admission.maxTs > 0 && (await confirmQueued(admission.maxTs))) {
|
|
225
|
+
clearPendingAdmission(sid, roomId, admission.maxTs);
|
|
193
226
|
}
|
|
227
|
+
return;
|
|
194
228
|
}
|
|
195
229
|
const directory =
|
|
196
230
|
(entry as RegistryEntry & { directory?: string }).directory ??
|
|
@@ -215,7 +249,7 @@ async function wakeMentionedSessions(
|
|
|
215
249
|
if (res.status >= 200 && res.status < 300 && admissionMatches(data, body, sid)) {
|
|
216
250
|
markPendingAdmissionAdmitted(sid, roomId, admission.id);
|
|
217
251
|
touchActive(sid);
|
|
218
|
-
if (
|
|
252
|
+
if (admission.maxTs > 0 && (await confirmQueued(admission.maxTs))) {
|
|
219
253
|
clearPendingAdmission(sid, roomId, admission.maxTs);
|
|
220
254
|
}
|
|
221
255
|
} else if (res.status !== 404) {
|
|
@@ -271,7 +305,11 @@ type InboxItem = {
|
|
|
271
305
|
text: string;
|
|
272
306
|
createdAt: number;
|
|
273
307
|
};
|
|
274
|
-
type InboxResult = {
|
|
308
|
+
type InboxResult = {
|
|
309
|
+
member: boolean;
|
|
310
|
+
items: InboxItem[];
|
|
311
|
+
queuedTs: Record<string, number>;
|
|
312
|
+
};
|
|
275
313
|
|
|
276
314
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
277
315
|
|
|
@@ -286,6 +324,7 @@ async function readInbox(
|
|
|
286
324
|
return read(async (svc) => {
|
|
287
325
|
const reg = await loadRegistry();
|
|
288
326
|
const out: InboxItem[] = [];
|
|
327
|
+
const queuedTs: Record<string, number> = Object.create(null);
|
|
289
328
|
let member = false;
|
|
290
329
|
for (const room of svc.listRooms()) {
|
|
291
330
|
if (roomFilter && room.id !== roomFilter) {
|
|
@@ -296,6 +335,7 @@ async function readInbox(
|
|
|
296
335
|
continue;
|
|
297
336
|
}
|
|
298
337
|
member = true;
|
|
338
|
+
queuedTs[room.id] = entry.lastQueuedTs ?? 0;
|
|
299
339
|
const lastRead = entry.lastReadTs ?? 0;
|
|
300
340
|
for (const m of room.messages) {
|
|
301
341
|
const isSelf =
|
|
@@ -317,7 +357,7 @@ async function readInbox(
|
|
|
317
357
|
}
|
|
318
358
|
}
|
|
319
359
|
}
|
|
320
|
-
return { member, items: out };
|
|
360
|
+
return { member, items: out, queuedTs };
|
|
321
361
|
});
|
|
322
362
|
}
|
|
323
363
|
|
|
@@ -404,6 +444,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
404
444
|
Date.now(),
|
|
405
445
|
created.messages.at(-1)?.createdAt ?? 0,
|
|
406
446
|
),
|
|
447
|
+
lastQueuedTs: 0,
|
|
407
448
|
};
|
|
408
449
|
await saveRegistry(reg);
|
|
409
450
|
}
|
|
@@ -439,20 +480,64 @@ async function handle(req: Request): Promise<Response> {
|
|
|
439
480
|
// watermark (sessionIDs are unguessable UUIDs and never exposed);
|
|
440
481
|
// accepted as a limitation until real session auth exists
|
|
441
482
|
const body = (await req.json()) as Record<string, unknown>;
|
|
442
|
-
const ts = Number(body.ts
|
|
483
|
+
const ts = Number(body.ts);
|
|
443
484
|
const roomId = String(body.roomId ?? "");
|
|
444
485
|
const sessionID = String(body.sessionID ?? "");
|
|
486
|
+
if (
|
|
487
|
+
body.ts === undefined ||
|
|
488
|
+
body.ts === null ||
|
|
489
|
+
!roomId ||
|
|
490
|
+
!sessionID ||
|
|
491
|
+
!Number.isFinite(ts)
|
|
492
|
+
) {
|
|
493
|
+
return json({ error: "roomId/sessionID/finite ts are required" }, 400);
|
|
494
|
+
}
|
|
495
|
+
const updated = await enqueue(async () => {
|
|
496
|
+
const reg = await loadRegistry();
|
|
497
|
+
const entry = reg[roomId]?.[sessionID];
|
|
498
|
+
if (!entry) {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
entry.lastReadTs = Math.max(entry.lastReadTs ?? 0, ts);
|
|
502
|
+
await saveRegistry(reg);
|
|
503
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
504
|
+
return true;
|
|
505
|
+
});
|
|
506
|
+
if (!updated) {
|
|
507
|
+
return json({ error: "registry entry not found" }, 404);
|
|
508
|
+
}
|
|
445
509
|
touchActive(sessionID); // poll 推进 = 活跃
|
|
446
|
-
|
|
510
|
+
return json({ ok: true });
|
|
511
|
+
}
|
|
512
|
+
if (req.method === "POST" && p === "/queued") {
|
|
513
|
+
const body = (await req.json()) as Record<string, unknown>;
|
|
514
|
+
const ts = Number(body.ts);
|
|
515
|
+
const roomId = String(body.roomId ?? "");
|
|
516
|
+
const sessionID = String(body.sessionID ?? "");
|
|
517
|
+
if (
|
|
518
|
+
body.ts === undefined ||
|
|
519
|
+
body.ts === null ||
|
|
520
|
+
!roomId ||
|
|
521
|
+
!sessionID ||
|
|
522
|
+
!Number.isFinite(ts)
|
|
523
|
+
) {
|
|
524
|
+
return json({ error: "roomId/sessionID/finite ts are required" }, 400);
|
|
525
|
+
}
|
|
526
|
+
const updated = await enqueue(async () => {
|
|
447
527
|
const reg = await loadRegistry();
|
|
448
528
|
const entry = reg[roomId]?.[sessionID];
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
entry.lastReadTs = Math.max(entry.lastReadTs ?? 0, ts);
|
|
452
|
-
await saveRegistry(reg);
|
|
453
|
-
clearPendingAdmission(sessionID, roomId, ts);
|
|
529
|
+
if (!entry) {
|
|
530
|
+
return false;
|
|
454
531
|
}
|
|
532
|
+
entry.lastQueuedTs = Math.max(entry.lastQueuedTs ?? 0, ts);
|
|
533
|
+
await saveRegistry(reg);
|
|
534
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
535
|
+
return true;
|
|
455
536
|
});
|
|
537
|
+
if (!updated) {
|
|
538
|
+
return json({ error: "registry entry not found" }, 404);
|
|
539
|
+
}
|
|
540
|
+
touchActive(sessionID);
|
|
456
541
|
return json({ ok: true });
|
|
457
542
|
}
|
|
458
543
|
if (req.method === "POST" && p === "/active") {
|
|
@@ -533,6 +618,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
533
618
|
directory: body.directory || undefined,
|
|
534
619
|
// ora-1 #2: 归属主机——跨机 @-wake 只由所属机器执行
|
|
535
620
|
host: body.host ?? "",
|
|
621
|
+
lastQueuedTs: regRoom[sessionID]?.lastQueuedTs,
|
|
536
622
|
lastReadTs:
|
|
537
623
|
regRoom[sessionID]?.lastReadTs ??
|
|
538
624
|
Math.max(Date.now(), latestMessageTs),
|
|
@@ -618,6 +704,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
618
704
|
{
|
|
619
705
|
name: e.name,
|
|
620
706
|
lastReadTs: e.lastReadTs ?? 0,
|
|
707
|
+
lastQueuedTs: e.lastQueuedTs ?? 0,
|
|
621
708
|
host: e.host ?? "",
|
|
622
709
|
directory: e.directory,
|
|
623
710
|
},
|
package/src/center-client.ts
CHANGED
|
@@ -130,6 +130,17 @@ async function request(
|
|
|
130
130
|
return data;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
async function markQueued(
|
|
134
|
+
opts: CenterOpts,
|
|
135
|
+
roomId: string,
|
|
136
|
+
sessionID: string,
|
|
137
|
+
ts: number,
|
|
138
|
+
): Promise<boolean> {
|
|
139
|
+
return request(opts, "/queued", "POST", { roomId, sessionID, ts })
|
|
140
|
+
.then(() => true)
|
|
141
|
+
.catch(() => false);
|
|
142
|
+
}
|
|
143
|
+
|
|
133
144
|
export async function centerAction(
|
|
134
145
|
opts: CenterOpts,
|
|
135
146
|
args: {
|
|
@@ -253,10 +264,11 @@ export async function centerAction(
|
|
|
253
264
|
const lines = items.map(
|
|
254
265
|
(it) => `[${timeOf(it.createdAt)}] ${it.senderId}: ${it.text}`,
|
|
255
266
|
);
|
|
256
|
-
// ponytail: F7 —
|
|
257
|
-
//
|
|
267
|
+
// ponytail: F7 — serialize the acknowledgment before returning so a
|
|
268
|
+
// following action cannot race the read watermark update. A failure only
|
|
269
|
+
// repeats the messages, never loses them.
|
|
258
270
|
const maxTs = items.reduce((mx, it) => Math.max(mx, it.createdAt), 0);
|
|
259
|
-
|
|
271
|
+
await request(opts, "/read", "POST", {
|
|
260
272
|
roomId: args.roomId,
|
|
261
273
|
sessionID,
|
|
262
274
|
ts: maxTs,
|
|
@@ -405,6 +417,23 @@ export async function sendQueue(
|
|
|
405
417
|
// Overlapping hook and tool triggers must not double-deliver; keep the lock
|
|
406
418
|
// per session so a slow check cannot starve another session.
|
|
407
419
|
const inboxChecks = new Set<string>();
|
|
420
|
+
const sessionPushes = new Map<string, Promise<void>>();
|
|
421
|
+
|
|
422
|
+
function serializeSessionPush(
|
|
423
|
+
sessionID: string,
|
|
424
|
+
task: () => Promise<void>,
|
|
425
|
+
): Promise<void> {
|
|
426
|
+
const previous = sessionPushes.get(sessionID) ?? Promise.resolve();
|
|
427
|
+
const run = previous.catch(() => {}).then(task);
|
|
428
|
+
let current: Promise<void>;
|
|
429
|
+
current = run.finally(() => {
|
|
430
|
+
if (sessionPushes.get(sessionID) === current) {
|
|
431
|
+
sessionPushes.delete(sessionID);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
sessionPushes.set(sessionID, current);
|
|
435
|
+
return current;
|
|
436
|
+
}
|
|
408
437
|
|
|
409
438
|
export async function checkCenterInbox(
|
|
410
439
|
opts: CenterOpts,
|
|
@@ -416,15 +445,24 @@ export async function checkCenterInbox(
|
|
|
416
445
|
}
|
|
417
446
|
inboxChecks.add(sessionID);
|
|
418
447
|
try {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
448
|
+
await serializeSessionPush(sessionID, async () => {
|
|
449
|
+
const result = (await request(
|
|
450
|
+
opts,
|
|
451
|
+
`/inbox?sessionID=${encodeURIComponent(sessionID)}&excludeSelf=1`,
|
|
452
|
+
"GET",
|
|
453
|
+
)) as unknown as {
|
|
454
|
+
member: boolean;
|
|
455
|
+
items: InboxItem[];
|
|
456
|
+
queuedTs?: Record<string, number>;
|
|
457
|
+
};
|
|
458
|
+
const queuedTs = result.queuedTs ?? {};
|
|
459
|
+
const items = (Array.isArray(result.items) ? result.items : []).filter(
|
|
460
|
+
(item) => item.createdAt > (queuedTs[item.roomId] ?? 0),
|
|
461
|
+
);
|
|
462
|
+
if (items.length > 0) {
|
|
463
|
+
await pushItemsToSession(opts, sessionID, items, transport, queuedTs);
|
|
464
|
+
}
|
|
465
|
+
});
|
|
428
466
|
} catch {
|
|
429
467
|
// best-effort: next check retries
|
|
430
468
|
} finally {
|
|
@@ -432,21 +470,24 @@ export async function checkCenterInbox(
|
|
|
432
470
|
}
|
|
433
471
|
}
|
|
434
472
|
|
|
435
|
-
// Items are grouped by room.
|
|
436
|
-
//
|
|
437
|
-
const lastPushed = new Map<string, number>();
|
|
473
|
+
// Items are grouped by room. Queue admission advances only the durable queued
|
|
474
|
+
// cursor; poll remains the only path that advances the read cursor.
|
|
438
475
|
|
|
439
476
|
export async function pushItemsToSession(
|
|
440
477
|
opts: CenterOpts,
|
|
441
478
|
sessionID: string,
|
|
442
479
|
items: InboxItem[],
|
|
443
480
|
transport: QueueTransport,
|
|
481
|
+
queuedTs: Readonly<Record<string, number>> = {},
|
|
444
482
|
): Promise<PushResult> {
|
|
445
483
|
if (items.length === 0) {
|
|
446
484
|
return "delivered";
|
|
447
485
|
}
|
|
448
486
|
const byRoom = new Map<string, InboxItem[]>();
|
|
449
487
|
for (const it of items) {
|
|
488
|
+
if (it.createdAt <= (queuedTs[it.roomId] ?? 0)) {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
450
491
|
const list = byRoom.get(it.roomId) ?? [];
|
|
451
492
|
list.push(it);
|
|
452
493
|
byRoom.set(it.roomId, list);
|
|
@@ -457,44 +498,40 @@ export async function pushItemsToSession(
|
|
|
457
498
|
(mx, it) => Math.max(mx, it.createdAt),
|
|
458
499
|
0,
|
|
459
500
|
);
|
|
460
|
-
const key = `${sessionID}|${roomId}`;
|
|
461
501
|
const existing = pendingAdmission(sessionID, roomId);
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
502
|
+
const deltaItems =
|
|
503
|
+
existing?.state === "admitted" && maxTs > existing.maxTs
|
|
504
|
+
? roomItems.filter((item) => item.createdAt > existing.maxTs)
|
|
505
|
+
: roomItems;
|
|
506
|
+
if (
|
|
507
|
+
existing?.state === "admitted" &&
|
|
508
|
+
(maxTs <= existing.maxTs || deltaItems.length === 0)
|
|
509
|
+
) {
|
|
510
|
+
if (existing.maxTs > 0) {
|
|
511
|
+
const queued = await markQueued(
|
|
512
|
+
opts,
|
|
471
513
|
roomId,
|
|
472
514
|
sessionID,
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
.catch(() => false);
|
|
477
|
-
if (advanced) {
|
|
478
|
-
lastPushed.set(key, existing.maxTs);
|
|
515
|
+
existing.maxTs,
|
|
516
|
+
);
|
|
517
|
+
if (queued) {
|
|
479
518
|
clearPendingAdmission(sessionID, roomId, existing.maxTs);
|
|
480
519
|
}
|
|
481
|
-
continue;
|
|
482
520
|
}
|
|
483
|
-
}
|
|
484
|
-
if (!existing && maxTs <= (lastPushed.get(key) ?? 0)) {
|
|
485
521
|
continue;
|
|
486
522
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
const
|
|
523
|
+
const deltaMaxTs = deltaItems.reduce(
|
|
524
|
+
(mx, it) => Math.max(mx, it.createdAt),
|
|
525
|
+
0,
|
|
526
|
+
);
|
|
527
|
+
const { lines, capped } = formatNotificationLines(deltaItems);
|
|
528
|
+
const prompt = `<notification>\nNew messages in room ${deltaItems[0]!.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
492
529
|
const admission = getOrCreatePendingAdmission(
|
|
493
530
|
sessionID,
|
|
494
531
|
roomId,
|
|
495
|
-
|
|
532
|
+
deltaItems,
|
|
496
533
|
prompt,
|
|
497
|
-
|
|
534
|
+
deltaMaxTs,
|
|
498
535
|
capped,
|
|
499
536
|
);
|
|
500
537
|
const result = await sendQueue(
|
|
@@ -514,19 +551,11 @@ export async function pushItemsToSession(
|
|
|
514
551
|
}
|
|
515
552
|
markPendingAdmissionAdmitted(sessionID, roomId, admission.id);
|
|
516
553
|
void request(opts, "/active", "POST", { sessionID }).catch(() => {});
|
|
517
|
-
if (admission.
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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);
|
|
554
|
+
if (admission.maxTs > 0) {
|
|
555
|
+
const queued = await markQueued(opts, roomId, sessionID, admission.maxTs);
|
|
556
|
+
if (queued) {
|
|
557
|
+
clearPendingAdmission(sessionID, roomId, admission.maxTs);
|
|
558
|
+
}
|
|
530
559
|
}
|
|
531
560
|
}
|
|
532
561
|
return worst;
|
|
@@ -575,6 +604,12 @@ async function wakeViaServe(
|
|
|
575
604
|
if (result === "delivered") {
|
|
576
605
|
void request(opts, "/active", "POST", { sessionID }).catch(() => {});
|
|
577
606
|
markPendingAdmissionAdmitted(sessionID, roomId, admission.id);
|
|
607
|
+
if (admission.maxTs > 0) {
|
|
608
|
+
const queued = await markQueued(opts, roomId, sessionID, admission.maxTs);
|
|
609
|
+
if (queued) {
|
|
610
|
+
clearPendingAdmission(sessionID, roomId, admission.maxTs);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
578
613
|
} else if (result === "rejected") {
|
|
579
614
|
rejectPendingAdmission(sessionID, roomId, admission.id);
|
|
580
615
|
}
|
|
@@ -616,6 +651,7 @@ export async function wakeSessions(
|
|
|
616
651
|
{
|
|
617
652
|
name: string;
|
|
618
653
|
lastReadTs?: number;
|
|
654
|
+
lastQueuedTs?: number;
|
|
619
655
|
host?: string;
|
|
620
656
|
directory?: string;
|
|
621
657
|
}
|
|
@@ -630,23 +666,40 @@ export async function wakeSessions(
|
|
|
630
666
|
continue;
|
|
631
667
|
}
|
|
632
668
|
// ponytail: orc-2 #6——跨 host 条目由各自机器上的插件负责,本机
|
|
633
|
-
// 扫描+推送只会 404
|
|
669
|
+
// 扫描+推送只会 404;队列游标由中心服务器持久化
|
|
634
670
|
if (entry.host !== os.hostname()) {
|
|
635
671
|
continue;
|
|
636
672
|
}
|
|
637
673
|
// quick skip: nothing newer than this session's watermark
|
|
638
|
-
if (
|
|
674
|
+
if (
|
|
675
|
+
latest <=
|
|
676
|
+
Math.max(entry.lastReadTs ?? 0, entry.lastQueuedTs ?? 0)
|
|
677
|
+
) {
|
|
639
678
|
continue;
|
|
640
679
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
680
|
+
await serializeSessionPush(sessionID, async () => {
|
|
681
|
+
const result = (await request(
|
|
682
|
+
opts,
|
|
683
|
+
`/inbox?sessionID=${encodeURIComponent(sessionID)}&roomId=${encodeURIComponent(roomId)}&excludeSelf=1&timeout=0`,
|
|
684
|
+
"GET",
|
|
685
|
+
)) as unknown as {
|
|
686
|
+
member: boolean;
|
|
687
|
+
items: InboxItem[];
|
|
688
|
+
queuedTs?: Record<string, number>;
|
|
689
|
+
};
|
|
690
|
+
const queuedTs = Math.max(
|
|
691
|
+
entry.lastQueuedTs ?? 0,
|
|
692
|
+
result.queuedTs?.[roomId] ?? 0,
|
|
693
|
+
);
|
|
694
|
+
const items = Array.isArray(result.items)
|
|
695
|
+
? result.items.filter(
|
|
696
|
+
(item) =>
|
|
697
|
+
item.roomId === roomId && item.createdAt > queuedTs,
|
|
698
|
+
)
|
|
699
|
+
: [];
|
|
700
|
+
if (items.length === 0) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
650
703
|
const targetTransport = {
|
|
651
704
|
...transport,
|
|
652
705
|
directory: entry.directory ?? transport.directory,
|
|
@@ -659,9 +712,10 @@ export async function wakeSessions(
|
|
|
659
712
|
sessionID,
|
|
660
713
|
items,
|
|
661
714
|
targetTransport,
|
|
715
|
+
result.queuedTs,
|
|
662
716
|
);
|
|
663
717
|
// 被 @ 的已退出 session:serve 配置了才唤起(进程内,零 spawn)。
|
|
664
|
-
//
|
|
718
|
+
// 唤起成功即推进队列游标——消息已进入该 session 的对话(不丢),
|
|
665
719
|
// 避免同一未读反复唤起导致循环回复。
|
|
666
720
|
// 仅唤起本机 opencode session(host === 本机主机名)——web 条目
|
|
667
721
|
// 与 curl 模拟成员(host 空)没有可恢复的 opencode 会话
|
|
@@ -678,24 +732,40 @@ export async function wakeSessions(
|
|
|
678
732
|
);
|
|
679
733
|
});
|
|
680
734
|
if (mentioned) {
|
|
681
|
-
const
|
|
735
|
+
const currentMaxTs = items.reduce(
|
|
736
|
+
(mx, it) => Math.max(mx, it.createdAt),
|
|
737
|
+
0,
|
|
738
|
+
);
|
|
739
|
+
const existing = pendingAdmission(sessionID, roomId);
|
|
740
|
+
const deltaItems =
|
|
741
|
+
existing?.state === "admitted" &&
|
|
742
|
+
currentMaxTs > existing.maxTs
|
|
743
|
+
? items.filter((item) => item.createdAt > existing.maxTs)
|
|
744
|
+
: items;
|
|
745
|
+
if (
|
|
746
|
+
existing?.state === "admitted" &&
|
|
747
|
+
(currentMaxTs <= existing.maxTs || deltaItems.length === 0)
|
|
748
|
+
) {
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
const { lines, capped } = formatNotificationLines(deltaItems);
|
|
682
752
|
const prompt = formatServeWakePrompt(
|
|
683
|
-
|
|
753
|
+
deltaItems[0]!.roomName,
|
|
684
754
|
lines,
|
|
685
755
|
);
|
|
686
|
-
const maxTs =
|
|
756
|
+
const maxTs = deltaItems.reduce(
|
|
687
757
|
(mx, it) => Math.max(mx, it.createdAt),
|
|
688
758
|
0,
|
|
689
759
|
);
|
|
690
760
|
const admission = getOrCreatePendingAdmission(
|
|
691
761
|
sessionID,
|
|
692
762
|
roomId,
|
|
693
|
-
|
|
763
|
+
deltaItems,
|
|
694
764
|
prompt,
|
|
695
765
|
maxTs,
|
|
696
766
|
capped,
|
|
697
767
|
);
|
|
698
|
-
|
|
768
|
+
await wakeViaServe(
|
|
699
769
|
serveUrl,
|
|
700
770
|
sessionID,
|
|
701
771
|
roomId,
|
|
@@ -703,21 +773,9 @@ export async function wakeSessions(
|
|
|
703
773
|
targetTransport,
|
|
704
774
|
admission,
|
|
705
775
|
);
|
|
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
776
|
}
|
|
719
777
|
}
|
|
720
|
-
}
|
|
778
|
+
});
|
|
721
779
|
}
|
|
722
780
|
} catch {
|
|
723
781
|
// ponytail: orc-3 #4——单房间错误(registry/inbox 请求失败)只跳过
|
package/src/queue.ts
CHANGED
|
@@ -174,13 +174,18 @@ export function admissionMatches(
|
|
|
174
174
|
return false;
|
|
175
175
|
}
|
|
176
176
|
const record = value as Record<string, unknown>;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
177
|
+
if (
|
|
178
|
+
record.id === body.id &&
|
|
179
|
+
record.sessionID === sessionID &&
|
|
180
|
+
record.delivery === "queue"
|
|
181
|
+
) {
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
return isRecord(record.data)
|
|
185
|
+
? admissionMatches(record.data, body, sessionID)
|
|
186
|
+
: false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
190
|
+
return typeof value === "object" && value !== null;
|
|
186
191
|
}
|