@alexsun-top/opencode-chat-room 0.1.1 → 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 +2 -1
- package/plugin.ts +268 -73
- package/server/chat-server.ts +230 -53
- package/server/chat.html +350 -95
- package/src/center-client.ts +412 -152
- package/src/queue.ts +191 -0
- package/src/room-service.ts +11 -4
- package/src/store.ts +19 -6
package/src/queue.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
export type QueueItem = {
|
|
4
|
+
id?: string;
|
|
5
|
+
senderId: string;
|
|
6
|
+
text: string;
|
|
7
|
+
createdAt: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type QueueBody = {
|
|
11
|
+
id: string;
|
|
12
|
+
prompt: { text: string };
|
|
13
|
+
delivery: "queue";
|
|
14
|
+
resume: true;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type PendingAdmission = {
|
|
18
|
+
id: string;
|
|
19
|
+
prompt: string;
|
|
20
|
+
itemCount: number;
|
|
21
|
+
maxTs: number;
|
|
22
|
+
capped: boolean;
|
|
23
|
+
state: "pending" | "admitted";
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type PendingEntry = {
|
|
27
|
+
admission: PendingAdmission;
|
|
28
|
+
expiresAt: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// ponytail: lazy expiry avoids one timer per admission; the hard cap keeps the
|
|
32
|
+
// module-level map bounded even when no later queue activity triggers pruning.
|
|
33
|
+
const PENDING_TTL_MS = 5 * 60_000;
|
|
34
|
+
const MAX_PENDING_ADMISSIONS = 1024;
|
|
35
|
+
const pending = new Map<string, PendingEntry>();
|
|
36
|
+
|
|
37
|
+
function keyFor(sessionID: string, roomId: string): string {
|
|
38
|
+
return `${sessionID}|${roomId}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function pruneExpired(now: number): void {
|
|
42
|
+
for (const [key, entry] of pending) {
|
|
43
|
+
if (entry.expiresAt <= now) {
|
|
44
|
+
pending.delete(key);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function pendingEntry(key: string, now: number): PendingEntry | undefined {
|
|
50
|
+
pruneExpired(now);
|
|
51
|
+
return pending.get(key);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function rememberPending(
|
|
55
|
+
key: string,
|
|
56
|
+
admission: PendingAdmission,
|
|
57
|
+
now: number,
|
|
58
|
+
): void {
|
|
59
|
+
pending.delete(key);
|
|
60
|
+
pending.set(key, {
|
|
61
|
+
admission,
|
|
62
|
+
expiresAt: now + PENDING_TTL_MS,
|
|
63
|
+
});
|
|
64
|
+
while (pending.size > MAX_PENDING_ADMISSIONS) {
|
|
65
|
+
const oldest = pending.keys().next();
|
|
66
|
+
if (oldest.done) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
pending.delete(oldest.value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function queueAdmissionId(
|
|
74
|
+
sessionID: string,
|
|
75
|
+
roomId: string,
|
|
76
|
+
items: ReadonlyArray<QueueItem>,
|
|
77
|
+
prompt = "",
|
|
78
|
+
): string {
|
|
79
|
+
const fingerprint = JSON.stringify({
|
|
80
|
+
sessionID,
|
|
81
|
+
roomId,
|
|
82
|
+
prompt,
|
|
83
|
+
items: items.map((item) => [
|
|
84
|
+
item.id ?? "",
|
|
85
|
+
item.senderId,
|
|
86
|
+
item.text,
|
|
87
|
+
item.createdAt,
|
|
88
|
+
]),
|
|
89
|
+
});
|
|
90
|
+
return `msg_${createHash("sha256").update(fingerprint).digest("hex")}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function pendingAdmission(
|
|
94
|
+
sessionID: string,
|
|
95
|
+
roomId: string,
|
|
96
|
+
): PendingAdmission | undefined {
|
|
97
|
+
return pendingEntry(keyFor(sessionID, roomId), Date.now())?.admission;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function getOrCreatePendingAdmission(
|
|
101
|
+
sessionID: string,
|
|
102
|
+
roomId: string,
|
|
103
|
+
items: ReadonlyArray<QueueItem>,
|
|
104
|
+
prompt: string,
|
|
105
|
+
maxTs: number,
|
|
106
|
+
capped: boolean,
|
|
107
|
+
): PendingAdmission {
|
|
108
|
+
const key = keyFor(sessionID, roomId);
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
const existing = pendingEntry(key, now)?.admission;
|
|
111
|
+
if (
|
|
112
|
+
existing &&
|
|
113
|
+
(existing.state !== "admitted" || maxTs <= existing.maxTs)
|
|
114
|
+
) {
|
|
115
|
+
return existing;
|
|
116
|
+
}
|
|
117
|
+
const admission: PendingAdmission = {
|
|
118
|
+
id: queueAdmissionId(sessionID, roomId, items, prompt),
|
|
119
|
+
prompt,
|
|
120
|
+
itemCount: items.length,
|
|
121
|
+
maxTs,
|
|
122
|
+
capped,
|
|
123
|
+
state: "pending",
|
|
124
|
+
};
|
|
125
|
+
if (maxTs > 0) {
|
|
126
|
+
rememberPending(key, admission, now);
|
|
127
|
+
}
|
|
128
|
+
return admission;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function rejectPendingAdmission(
|
|
132
|
+
sessionID: string,
|
|
133
|
+
roomId: string,
|
|
134
|
+
admissionID: string,
|
|
135
|
+
): void {
|
|
136
|
+
const key = keyFor(sessionID, roomId);
|
|
137
|
+
const admission = pendingEntry(key, Date.now())?.admission;
|
|
138
|
+
if (admission?.state === "pending" && admission.id === admissionID) {
|
|
139
|
+
pending.delete(key);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function markPendingAdmissionAdmitted(
|
|
144
|
+
sessionID: string,
|
|
145
|
+
roomId: string,
|
|
146
|
+
admissionID?: string,
|
|
147
|
+
): PendingAdmission | undefined {
|
|
148
|
+
const admission = pendingEntry(keyFor(sessionID, roomId), Date.now())?.admission;
|
|
149
|
+
if (admission && (admissionID === undefined || admission.id === admissionID)) {
|
|
150
|
+
admission.state = "admitted";
|
|
151
|
+
}
|
|
152
|
+
return admission;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function clearPendingAdmission(
|
|
156
|
+
sessionID: string,
|
|
157
|
+
roomId: string,
|
|
158
|
+
confirmedTs: number,
|
|
159
|
+
): void {
|
|
160
|
+
const key = keyFor(sessionID, roomId);
|
|
161
|
+
const admission = pendingEntry(key, Date.now())?.admission;
|
|
162
|
+
if (!admission || confirmedTs < admission.maxTs) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
pending.delete(key);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function admissionMatches(
|
|
169
|
+
value: unknown,
|
|
170
|
+
body: QueueBody,
|
|
171
|
+
sessionID: string,
|
|
172
|
+
): boolean {
|
|
173
|
+
if (typeof value !== "object" || value === null) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
const record = value as Record<string, unknown>;
|
|
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;
|
|
191
|
+
}
|
package/src/room-service.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type Message = {
|
|
|
10
10
|
id: string;
|
|
11
11
|
roomId: string;
|
|
12
12
|
senderId: string;
|
|
13
|
+
senderSessionID?: string;
|
|
13
14
|
text: string;
|
|
14
15
|
createdAt: number;
|
|
15
16
|
};
|
|
@@ -111,7 +112,12 @@ export default class RoomService {
|
|
|
111
112
|
}
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
sendMessage(
|
|
115
|
+
sendMessage(
|
|
116
|
+
roomId: string,
|
|
117
|
+
senderId: string,
|
|
118
|
+
text: string,
|
|
119
|
+
senderSessionID?: string,
|
|
120
|
+
): Message {
|
|
115
121
|
const room = this.getRoom(roomId);
|
|
116
122
|
if (!room.participants.has(senderId)) {
|
|
117
123
|
throw new RoomError("NOT_JOINED", `not joined: ${senderId}`);
|
|
@@ -124,8 +130,9 @@ export default class RoomService {
|
|
|
124
130
|
id: randomUUID(),
|
|
125
131
|
roomId,
|
|
126
132
|
senderId,
|
|
133
|
+
...(senderSessionID ? { senderSessionID } : {}),
|
|
127
134
|
text: trimmed,
|
|
128
|
-
createdAt: Date.now(),
|
|
135
|
+
createdAt: Math.max(Date.now(), (room.messages.at(-1)?.createdAt ?? 0) + 1),
|
|
129
136
|
};
|
|
130
137
|
room.messages.push(message);
|
|
131
138
|
this.trimMessages(room);
|
|
@@ -135,14 +142,14 @@ export default class RoomService {
|
|
|
135
142
|
// ponytail: membership events live in the message stream (senderId
|
|
136
143
|
// "system") so push notifications and the central inbox carry them for free
|
|
137
144
|
addEvent(roomId: string, text: string): Message {
|
|
145
|
+
const room = this.getRoom(roomId);
|
|
138
146
|
const message: Message = {
|
|
139
147
|
id: randomUUID(),
|
|
140
148
|
roomId,
|
|
141
149
|
senderId: "system",
|
|
142
150
|
text,
|
|
143
|
-
createdAt: Date.now(),
|
|
151
|
+
createdAt: Math.max(Date.now(), (room.messages.at(-1)?.createdAt ?? 0) + 1),
|
|
144
152
|
};
|
|
145
|
-
const room = this.getRoom(roomId);
|
|
146
153
|
room.messages.push(message);
|
|
147
154
|
this.trimMessages(room);
|
|
148
155
|
return message;
|
package/src/store.ts
CHANGED
|
@@ -85,7 +85,9 @@ export async function loadRooms(): Promise<Room[]> {
|
|
|
85
85
|
typeof m.roomId === "string" &&
|
|
86
86
|
typeof m.senderId === "string" &&
|
|
87
87
|
typeof m.text === "string" &&
|
|
88
|
-
typeof m.createdAt === "number"
|
|
88
|
+
typeof m.createdAt === "number" &&
|
|
89
|
+
(m.senderSessionID === undefined ||
|
|
90
|
+
typeof m.senderSessionID === "string"),
|
|
89
91
|
);
|
|
90
92
|
}
|
|
91
93
|
return [room];
|
|
@@ -112,6 +114,8 @@ export type RegistryEntry = {
|
|
|
112
114
|
apiPrefix: string;
|
|
113
115
|
name: string;
|
|
114
116
|
lastReadTs?: number;
|
|
117
|
+
lastQueuedTs?: number;
|
|
118
|
+
directory?: string;
|
|
115
119
|
// ora-1 #2: 归属主机——跨机 @-wake 只由所属机器执行;web 条目不填
|
|
116
120
|
host?: string;
|
|
117
121
|
};
|
|
@@ -180,24 +184,33 @@ export const NOTIFY_INSTRUCTION =
|
|
|
180
184
|
export const SERVE_WAKE_PROMPT =
|
|
181
185
|
"You were woken because you were mentioned (@) in the chat room. Use the room tool to poll the room(s) you are a member of, read the new messages, and reply to anything addressed to you. Then report what you did.";
|
|
182
186
|
|
|
187
|
+
export function formatServeWakePrompt(
|
|
188
|
+
roomName: string,
|
|
189
|
+
lines: ReadonlyArray<string>,
|
|
190
|
+
): string {
|
|
191
|
+
return `${SERVE_WAKE_PROMPT}\n\nNew messages in room ${roomName}:\n${lines.join("\n")}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
183
194
|
// ponytail: oversize protection for push payloads — truncate long texts and
|
|
184
195
|
// cap the number of unread lines per notification
|
|
185
196
|
const MAX_LINE = 300;
|
|
186
197
|
const MAX_LINES = 20;
|
|
187
198
|
|
|
188
|
-
// Display lines for a push. `capped` means
|
|
189
|
-
//
|
|
190
|
-
// watermark
|
|
199
|
+
// Display lines for a push. `capped` means the notification is lossy because
|
|
200
|
+
// lines were folded or message text was truncated; callers must not advance
|
|
201
|
+
// the watermark until a full poll confirms it.
|
|
191
202
|
export function formatNotificationLines(
|
|
192
203
|
items: ReadonlyArray<{ createdAt: number; senderId: string; text: string }>,
|
|
193
204
|
): { lines: string[]; capped: boolean } {
|
|
194
|
-
const
|
|
205
|
+
const countCapped = items.length > MAX_LINES;
|
|
206
|
+
const textCapped = items.some((it) => it.text.length > MAX_LINE);
|
|
207
|
+
const capped = countCapped || textCapped;
|
|
195
208
|
const lines = items.slice(-MAX_LINES).map((it) => {
|
|
196
209
|
const text =
|
|
197
210
|
it.text.length > MAX_LINE ? `${it.text.slice(0, MAX_LINE)}\u2026` : it.text;
|
|
198
211
|
return `- [${timeOf(it.createdAt)}] ${it.senderId}: ${text}`;
|
|
199
212
|
});
|
|
200
|
-
if (
|
|
213
|
+
if (countCapped) {
|
|
201
214
|
lines.unshift(`\u2026 and ${items.length - MAX_LINES} earlier messages`);
|
|
202
215
|
}
|
|
203
216
|
return { lines, capped };
|