@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.
@@ -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;否则更早未读被标记已读但从未投递(插件 sweep 也会因
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 = inbox.items.some((item) => item.id === triggerItem.id)
147
- ? inbox.items
148
- : [...inbox.items, triggerItem].sort(
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 { lines, capped } = formatNotificationLines(items);
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 = items.reduce(
204
+ const maxTs = deltaItems.reduce(
154
205
  (mx, it) => Math.max(mx, it.createdAt),
155
206
  0,
156
207
  );
157
- const admissionItems: QueueItem[] = items;
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
- const confirmRead = async (): Promise<boolean> =>
173
- enqueue(async () => {
174
- const r = await loadRegistry();
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 (!admission.capped && admission.maxTs > 0 && (await confirmRead())) {
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 = { member: boolean; items: InboxItem[] };
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 ?? 0);
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
- await enqueue(async () => {
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
- // ponytail: NaN would poison the watermark (all comparisons false)
450
- if (entry && Number.isFinite(ts)) {
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
  },