@alexsun-top/opencode-chat-room 0.1.1 → 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 +5 -5
- package/README.zh-CN.md +5 -5
- package/package.json +2 -1
- package/plugin.ts +230 -73
- package/server/chat-server.ts +131 -41
- 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/server/chat-server.ts
CHANGED
|
@@ -4,16 +4,31 @@
|
|
|
4
4
|
import { promises as fs } from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import os from "node:os";
|
|
7
|
-
import RoomService, {
|
|
7
|
+
import RoomService, {
|
|
8
|
+
RoomError,
|
|
9
|
+
type Message,
|
|
10
|
+
type Room,
|
|
11
|
+
} from "../src/room-service";
|
|
8
12
|
import {
|
|
13
|
+
authHeader,
|
|
9
14
|
enqueue,
|
|
15
|
+
formatServeWakePrompt,
|
|
16
|
+
formatNotificationLines,
|
|
10
17
|
loadRegistry,
|
|
11
18
|
loadRooms,
|
|
12
19
|
saveRegistry,
|
|
13
20
|
saveRooms,
|
|
14
|
-
SERVE_WAKE_PROMPT,
|
|
15
21
|
type RegistryEntry,
|
|
16
22
|
} from "../src/store";
|
|
23
|
+
import {
|
|
24
|
+
admissionMatches,
|
|
25
|
+
clearPendingAdmission,
|
|
26
|
+
getOrCreatePendingAdmission,
|
|
27
|
+
markPendingAdmissionAdmitted,
|
|
28
|
+
rejectPendingAdmission,
|
|
29
|
+
type QueueBody,
|
|
30
|
+
type QueueItem,
|
|
31
|
+
} from "../src/queue";
|
|
17
32
|
|
|
18
33
|
function parsePort(value: string): number {
|
|
19
34
|
const port = Number(value);
|
|
@@ -75,10 +90,9 @@ function touchActive(sessionID: string): void {
|
|
|
75
90
|
async function wakeMentionedSessions(
|
|
76
91
|
roomId: string,
|
|
77
92
|
roomName: string,
|
|
78
|
-
senderName: string,
|
|
79
93
|
text: string,
|
|
80
94
|
senderSessionID: string,
|
|
81
|
-
|
|
95
|
+
triggeringMessage: Message,
|
|
82
96
|
): Promise<void> {
|
|
83
97
|
if (!serveWakeUrl) {
|
|
84
98
|
return;
|
|
@@ -121,38 +135,91 @@ async function wakeMentionedSessions(
|
|
|
121
135
|
// maxTs;否则更早未读被标记已读但从未投递(插件 sweep 也会因
|
|
122
136
|
// 水位已推进而跳过——永久丢失)
|
|
123
137
|
const inbox = await readInbox(sid, roomId, true);
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
const triggerItem: InboxItem = {
|
|
139
|
+
id: triggeringMessage.id,
|
|
140
|
+
roomId,
|
|
141
|
+
roomName,
|
|
142
|
+
senderId: triggeringMessage.senderId,
|
|
143
|
+
text: triggeringMessage.text,
|
|
144
|
+
createdAt: triggeringMessage.createdAt,
|
|
145
|
+
};
|
|
146
|
+
const items = inbox.items.some((item) => item.id === triggerItem.id)
|
|
147
|
+
? inbox.items
|
|
148
|
+
: [...inbox.items, triggerItem].sort(
|
|
149
|
+
(a, b) => a.createdAt - b.createdAt,
|
|
150
|
+
);
|
|
151
|
+
const { lines, capped } = formatNotificationLines(items);
|
|
152
|
+
const prompt = formatServeWakePrompt(roomName, lines);
|
|
153
|
+
const maxTs = items.reduce(
|
|
154
|
+
(mx, it) => Math.max(mx, it.createdAt),
|
|
155
|
+
0,
|
|
141
156
|
);
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
157
|
+
const admissionItems: QueueItem[] = items;
|
|
158
|
+
const admission = getOrCreatePendingAdmission(
|
|
159
|
+
sid,
|
|
160
|
+
roomId,
|
|
161
|
+
admissionItems,
|
|
162
|
+
prompt,
|
|
163
|
+
maxTs,
|
|
164
|
+
capped,
|
|
165
|
+
);
|
|
166
|
+
const body: QueueBody = {
|
|
167
|
+
id: admission.id,
|
|
168
|
+
prompt: { text: admission.prompt },
|
|
169
|
+
delivery: "queue",
|
|
170
|
+
resume: true,
|
|
171
|
+
};
|
|
172
|
+
const confirmRead = async (): Promise<boolean> =>
|
|
173
|
+
enqueue(async () => {
|
|
149
174
|
const r = await loadRegistry();
|
|
150
175
|
const e = r[roomId]?.[sid];
|
|
151
|
-
if (e) {
|
|
152
|
-
|
|
153
|
-
await saveRegistry(r);
|
|
176
|
+
if (!e) {
|
|
177
|
+
return false;
|
|
154
178
|
}
|
|
179
|
+
e.lastReadTs = Math.max(e.lastReadTs ?? 0, admission.maxTs);
|
|
180
|
+
await saveRegistry(r);
|
|
181
|
+
return true;
|
|
155
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;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const directory =
|
|
196
|
+
(entry as RegistryEntry & { directory?: string }).directory ??
|
|
197
|
+
process.env.OPENCODE_DIRECTORY;
|
|
198
|
+
const auth = authHeader();
|
|
199
|
+
const res = await fetch(
|
|
200
|
+
`${serveWakeUrl.replace(/\/+$/, "")}/api/session/${encodeURIComponent(sid)}/prompt`,
|
|
201
|
+
{
|
|
202
|
+
method: "POST",
|
|
203
|
+
headers: {
|
|
204
|
+
"Content-Type": "application/json",
|
|
205
|
+
...(auth ? { Authorization: auth } : {}),
|
|
206
|
+
...(directory
|
|
207
|
+
? { "x-opencode-directory": encodeURIComponent(directory) }
|
|
208
|
+
: {}),
|
|
209
|
+
},
|
|
210
|
+
body: JSON.stringify(body),
|
|
211
|
+
signal: AbortSignal.timeout(10_000),
|
|
212
|
+
},
|
|
213
|
+
);
|
|
214
|
+
const data = await res.json().catch(() => undefined);
|
|
215
|
+
if (res.status >= 200 && res.status < 300 && admissionMatches(data, body, sid)) {
|
|
216
|
+
markPendingAdmissionAdmitted(sid, roomId, admission.id);
|
|
217
|
+
touchActive(sid);
|
|
218
|
+
if (!admission.capped && admission.maxTs > 0 && (await confirmRead())) {
|
|
219
|
+
clearPendingAdmission(sid, roomId, admission.maxTs);
|
|
220
|
+
}
|
|
221
|
+
} else if (res.status !== 404) {
|
|
222
|
+
rejectPendingAdmission(sid, roomId, admission.id);
|
|
156
223
|
}
|
|
157
224
|
} catch {
|
|
158
225
|
// serve 不可达——静默,插件 10s 定时器兜底
|
|
@@ -173,7 +240,7 @@ const serializeRoom = (r: Room) => ({
|
|
|
173
240
|
ownerId: r.ownerId,
|
|
174
241
|
createdAt: r.createdAt,
|
|
175
242
|
participants: [...r.participants.values()],
|
|
176
|
-
messages: r.messages,
|
|
243
|
+
messages: r.messages.map(({ senderSessionID: _senderSessionID, ...message }) => message),
|
|
177
244
|
});
|
|
178
245
|
|
|
179
246
|
async function mutate<T>(
|
|
@@ -231,9 +298,13 @@ async function readInbox(
|
|
|
231
298
|
member = true;
|
|
232
299
|
const lastRead = entry.lastReadTs ?? 0;
|
|
233
300
|
for (const m of room.messages) {
|
|
301
|
+
const isSelf =
|
|
302
|
+
m.senderSessionID !== undefined
|
|
303
|
+
? m.senderSessionID === sessionID
|
|
304
|
+
: m.senderId === entry.name;
|
|
234
305
|
if (
|
|
235
306
|
m.createdAt > lastRead &&
|
|
236
|
-
(!excludeSelf ||
|
|
307
|
+
(!excludeSelf || !isSelf)
|
|
237
308
|
) {
|
|
238
309
|
out.push({
|
|
239
310
|
id: m.id,
|
|
@@ -253,6 +324,9 @@ async function readInbox(
|
|
|
253
324
|
async function handle(req: Request): Promise<Response> {
|
|
254
325
|
const url = new URL(req.url);
|
|
255
326
|
const p = url.pathname;
|
|
327
|
+
if (req.method === "GET" && p === "/") {
|
|
328
|
+
return Response.redirect(new URL("/chat", url).toString(), 302);
|
|
329
|
+
}
|
|
256
330
|
if (req.method === "GET" && p === "/chat") {
|
|
257
331
|
// ponytail: the human-facing chat page; APIs stay token-gated, the page
|
|
258
332
|
// itself is a static shell
|
|
@@ -323,9 +397,13 @@ async function handle(req: Request): Promise<Response> {
|
|
|
323
397
|
serverUrl: "",
|
|
324
398
|
apiPrefix: "/api",
|
|
325
399
|
name: owner,
|
|
400
|
+
directory: body.directory || undefined,
|
|
326
401
|
// ora-1 #2: 归属主机——跨机 @-wake 只由所属机器执行
|
|
327
402
|
host: body.host ?? "",
|
|
328
|
-
lastReadTs:
|
|
403
|
+
lastReadTs: Math.max(
|
|
404
|
+
Date.now(),
|
|
405
|
+
created.messages.at(-1)?.createdAt ?? 0,
|
|
406
|
+
),
|
|
329
407
|
};
|
|
330
408
|
await saveRegistry(reg);
|
|
331
409
|
}
|
|
@@ -362,14 +440,17 @@ async function handle(req: Request): Promise<Response> {
|
|
|
362
440
|
// accepted as a limitation until real session auth exists
|
|
363
441
|
const body = (await req.json()) as Record<string, unknown>;
|
|
364
442
|
const ts = Number(body.ts ?? 0);
|
|
365
|
-
|
|
443
|
+
const roomId = String(body.roomId ?? "");
|
|
444
|
+
const sessionID = String(body.sessionID ?? "");
|
|
445
|
+
touchActive(sessionID); // poll 推进 = 活跃
|
|
366
446
|
await enqueue(async () => {
|
|
367
447
|
const reg = await loadRegistry();
|
|
368
|
-
const entry = reg[
|
|
448
|
+
const entry = reg[roomId]?.[sessionID];
|
|
369
449
|
// ponytail: NaN would poison the watermark (all comparisons false)
|
|
370
450
|
if (entry && Number.isFinite(ts)) {
|
|
371
451
|
entry.lastReadTs = Math.max(entry.lastReadTs ?? 0, ts);
|
|
372
452
|
await saveRegistry(reg);
|
|
453
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
373
454
|
}
|
|
374
455
|
});
|
|
375
456
|
return json({ ok: true });
|
|
@@ -440,6 +521,8 @@ async function handle(req: Request): Promise<Response> {
|
|
|
440
521
|
if (!already) {
|
|
441
522
|
svc.addEvent(roomId, `${name} joined the room`);
|
|
442
523
|
}
|
|
524
|
+
const latestMessageTs =
|
|
525
|
+
svc.getMessages(roomId).at(-1)?.createdAt ?? 0;
|
|
443
526
|
regRoom[sessionID] = {
|
|
444
527
|
// ponytail: central mode self-pulls the inbox; no cross-host URL.
|
|
445
528
|
// Re-join must not rewind the read watermark (unread messages
|
|
@@ -447,9 +530,12 @@ async function handle(req: Request): Promise<Response> {
|
|
|
447
530
|
serverUrl: "",
|
|
448
531
|
apiPrefix: "/api",
|
|
449
532
|
name,
|
|
533
|
+
directory: body.directory || undefined,
|
|
450
534
|
// ora-1 #2: 归属主机——跨机 @-wake 只由所属机器执行
|
|
451
535
|
host: body.host ?? "",
|
|
452
|
-
lastReadTs:
|
|
536
|
+
lastReadTs:
|
|
537
|
+
regRoom[sessionID]?.lastReadTs ??
|
|
538
|
+
Math.max(Date.now(), latestMessageTs),
|
|
453
539
|
};
|
|
454
540
|
await saveRegistry(reg);
|
|
455
541
|
return svc.getRoom(roomId);
|
|
@@ -492,7 +578,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
492
578
|
const room = await mutate(async (svc) => {
|
|
493
579
|
const reg = await loadRegistry();
|
|
494
580
|
const sender = reg[roomId]?.[sessionID]?.name ?? sessionID;
|
|
495
|
-
svc.sendMessage(roomId, sender, text);
|
|
581
|
+
svc.sendMessage(roomId, sender, text, sessionID);
|
|
496
582
|
return svc.getRoom(roomId);
|
|
497
583
|
});
|
|
498
584
|
touchActive(sessionID); // 发送者活跃
|
|
@@ -502,10 +588,9 @@ async function handle(req: Request): Promise<Response> {
|
|
|
502
588
|
void wakeMentionedSessions(
|
|
503
589
|
roomId,
|
|
504
590
|
room.name,
|
|
505
|
-
sent.senderId,
|
|
506
591
|
text,
|
|
507
592
|
sessionID,
|
|
508
|
-
sent
|
|
593
|
+
sent,
|
|
509
594
|
);
|
|
510
595
|
}
|
|
511
596
|
return json(serializeRoom(room), 201);
|
|
@@ -530,7 +615,12 @@ async function handle(req: Request): Promise<Response> {
|
|
|
530
615
|
return Object.fromEntries(
|
|
531
616
|
Object.entries(entries).map(([sid, e]) => [
|
|
532
617
|
sid,
|
|
533
|
-
{
|
|
618
|
+
{
|
|
619
|
+
name: e.name,
|
|
620
|
+
lastReadTs: e.lastReadTs ?? 0,
|
|
621
|
+
host: e.host ?? "",
|
|
622
|
+
directory: e.directory,
|
|
623
|
+
},
|
|
534
624
|
]),
|
|
535
625
|
);
|
|
536
626
|
});
|