@johnhenry/browsermesh-apps 0.0.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.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +36 -0
- package/src/apps.mjs +1351 -0
- package/src/audit.mjs +622 -0
- package/src/chat.mjs +595 -0
- package/src/compat.mjs +30 -0
- package/src/consensus.mjs +784 -0
- package/src/devtools.mjs +255 -0
- package/src/gpu.mjs +732 -0
- package/src/index.mjs +44 -0
- package/src/marketplace-ui.mjs +245 -0
- package/src/marketplace.mjs +754 -0
- package/src/orchestrator.mjs +1575 -0
- package/src/payments.mjs +909 -0
- package/src/peer-agent-swarm.mjs +569 -0
- package/src/peer-agent.mjs +547 -0
- package/src/peer-chat.mjs +381 -0
- package/src/peer-compute.mjs +687 -0
- package/src/peer-encrypted-store.mjs +514 -0
- package/src/peer-escrow.mjs +573 -0
- package/src/peer-files.mjs +470 -0
- package/src/peer-health.mjs +727 -0
- package/src/peer-ipfs.mjs +374 -0
- package/src/peer-node.mjs +793 -0
- package/src/peer-payments.mjs +613 -0
- package/src/peer-registry.mjs +595 -0
- package/src/peer-routing.mjs +555 -0
- package/src/peer-services.mjs +478 -0
- package/src/peer-session.mjs +917 -0
- package/src/peer-terminal.mjs +500 -0
- package/src/peer-timestamp.mjs +403 -0
- package/src/peer-torrent.mjs +628 -0
- package/src/peer-verification.mjs +522 -0
- package/src/quotas.mjs +621 -0
- package/src/resources.mjs +679 -0
- package/src/scheduler.mjs +846 -0
- package/src/silent-catch.mjs +55 -0
- package/src/tools.mjs +668 -0
- package/src/visualizations.mjs +902 -0
package/src/chat.mjs
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
/**
|
|
2
|
+
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
3
|
+
* clawser-mesh-chat.js -- CRDT-backed chat rooms for BrowserMesh.
|
|
4
|
+
*
|
|
5
|
+
* ChatMessage envelopes, ChatRoom with ORSet membership, LWWMap
|
|
6
|
+
* presence, moderation, and a multi-room MeshChat manager.
|
|
7
|
+
*
|
|
8
|
+
* No browser-only imports at module level.
|
|
9
|
+
*
|
|
10
|
+
* Run tests:
|
|
11
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-chat.test.mjs
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
ORSet,
|
|
16
|
+
LWWMap,
|
|
17
|
+
} from '@johnhenry/browsermesh-primitives';
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Constants
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
export const MESSAGE_TYPES = Object.freeze([
|
|
24
|
+
'text', 'file', 'reply', 'reaction', 'edit', 'redaction', 'system',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/** Maximum message body size in bytes */
|
|
28
|
+
export const MAX_MESSAGE_SIZE = 32768;
|
|
29
|
+
|
|
30
|
+
/** Maximum members per room */
|
|
31
|
+
export const MAX_ROOM_MEMBERS = 256;
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// ID generation
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
let _msgSeq = 0;
|
|
38
|
+
|
|
39
|
+
function generateMessageId() {
|
|
40
|
+
return `msg_${Date.now().toString(36)}_${(++_msgSeq).toString(36)}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let _roomSeq = 0;
|
|
44
|
+
|
|
45
|
+
function generateRoomId() {
|
|
46
|
+
return `room_${Date.now().toString(36)}_${(++_roomSeq).toString(36)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
// ChatMessage
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Chat message envelope.
|
|
55
|
+
*/
|
|
56
|
+
export class ChatMessage {
|
|
57
|
+
/**
|
|
58
|
+
* @param {object} opts
|
|
59
|
+
* @param {string} opts.roomId
|
|
60
|
+
* @param {string} opts.sender - Fingerprint of the sender
|
|
61
|
+
* @param {string} opts.type - One of MESSAGE_TYPES
|
|
62
|
+
* @param {string} opts.body
|
|
63
|
+
* @param {string} [opts.parentId] - For replies/threads
|
|
64
|
+
* @param {string} [opts.editOf] - For edits
|
|
65
|
+
* @param {number} [opts.timestamp]
|
|
66
|
+
* @param {string} [opts.id]
|
|
67
|
+
*/
|
|
68
|
+
constructor({ roomId, sender, type, body, parentId, editOf, timestamp, id }) {
|
|
69
|
+
if (!MESSAGE_TYPES.includes(type)) {
|
|
70
|
+
throw new Error(`Invalid message type: "${type}"`);
|
|
71
|
+
}
|
|
72
|
+
if (typeof body === 'string' && body.length > MAX_MESSAGE_SIZE) {
|
|
73
|
+
throw new Error(`Message body exceeds maximum size of ${MAX_MESSAGE_SIZE}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.id = id ?? generateMessageId();
|
|
77
|
+
this.roomId = roomId;
|
|
78
|
+
this.sender = sender;
|
|
79
|
+
this.type = type;
|
|
80
|
+
this.body = body;
|
|
81
|
+
this.parentId = parentId ?? null;
|
|
82
|
+
this.editOf = editOf ?? null;
|
|
83
|
+
this.timestamp = timestamp ?? Date.now();
|
|
84
|
+
/** @type {boolean} */
|
|
85
|
+
this._redacted = false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
isRedacted() {
|
|
89
|
+
return this._redacted;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
toJSON() {
|
|
93
|
+
return {
|
|
94
|
+
id: this.id,
|
|
95
|
+
roomId: this.roomId,
|
|
96
|
+
sender: this.sender,
|
|
97
|
+
type: this.type,
|
|
98
|
+
body: this.body,
|
|
99
|
+
parentId: this.parentId,
|
|
100
|
+
editOf: this.editOf,
|
|
101
|
+
timestamp: this.timestamp,
|
|
102
|
+
redacted: this._redacted,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static fromJSON(data) {
|
|
107
|
+
const msg = new ChatMessage({
|
|
108
|
+
id: data.id,
|
|
109
|
+
roomId: data.roomId,
|
|
110
|
+
sender: data.sender,
|
|
111
|
+
type: data.type,
|
|
112
|
+
body: data.body,
|
|
113
|
+
parentId: data.parentId,
|
|
114
|
+
editOf: data.editOf,
|
|
115
|
+
timestamp: data.timestamp,
|
|
116
|
+
});
|
|
117
|
+
msg._redacted = data.redacted ?? false;
|
|
118
|
+
return msg;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
// ChatRoom
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Single chat room with message log, CRDT-backed membership, and moderation.
|
|
128
|
+
*/
|
|
129
|
+
export class ChatRoom {
|
|
130
|
+
/**
|
|
131
|
+
* @param {object} opts
|
|
132
|
+
* @param {string} opts.id
|
|
133
|
+
* @param {string} opts.name
|
|
134
|
+
* @param {string} opts.creator - Fingerprint of the room creator
|
|
135
|
+
* @param {object} [opts.opts]
|
|
136
|
+
* @param {number} [opts.opts.maxMembers]
|
|
137
|
+
* @param {number} [opts.opts.retentionMs]
|
|
138
|
+
*/
|
|
139
|
+
constructor({ id, name, creator, opts: roomOpts }) {
|
|
140
|
+
this.id = id;
|
|
141
|
+
this.name = name;
|
|
142
|
+
this.creator = creator;
|
|
143
|
+
this.maxMembers = roomOpts?.maxMembers ?? MAX_ROOM_MEMBERS;
|
|
144
|
+
this.retentionMs = roomOpts?.retentionMs ?? null;
|
|
145
|
+
|
|
146
|
+
/** @type {Set<string>} Simple set for membership (ORSet for sync later) */
|
|
147
|
+
this._members = new Set();
|
|
148
|
+
/** @type {Set<string>} Banned fingerprints */
|
|
149
|
+
this._banned = new Set();
|
|
150
|
+
/** @type {ChatMessage[]} */
|
|
151
|
+
this._messages = [];
|
|
152
|
+
/** @type {Map<string, { status: string, lastSeen: number }>} */
|
|
153
|
+
this._presence = new Map();
|
|
154
|
+
|
|
155
|
+
// Event listeners
|
|
156
|
+
this._onMessageCbs = [];
|
|
157
|
+
this._onJoinCbs = [];
|
|
158
|
+
this._onLeaveCbs = [];
|
|
159
|
+
this._onPresenceCbs = [];
|
|
160
|
+
|
|
161
|
+
// Creator auto-joins
|
|
162
|
+
this._members.add(creator);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── Membership ─────────────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {string} fingerprint
|
|
169
|
+
* @returns {boolean} True if successfully joined
|
|
170
|
+
*/
|
|
171
|
+
join(fingerprint) {
|
|
172
|
+
if (this._banned.has(fingerprint)) return false;
|
|
173
|
+
if (this._members.has(fingerprint)) return false;
|
|
174
|
+
if (this._members.size >= this.maxMembers) return false;
|
|
175
|
+
this._members.add(fingerprint);
|
|
176
|
+
for (const cb of this._onJoinCbs) cb(fingerprint);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param {string} fingerprint
|
|
182
|
+
* @returns {boolean}
|
|
183
|
+
*/
|
|
184
|
+
leave(fingerprint) {
|
|
185
|
+
if (!this._members.has(fingerprint)) return false;
|
|
186
|
+
this._members.delete(fingerprint);
|
|
187
|
+
for (const cb of this._onLeaveCbs) cb(fingerprint);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @param {string} fingerprint
|
|
193
|
+
* @returns {boolean}
|
|
194
|
+
*/
|
|
195
|
+
isMember(fingerprint) {
|
|
196
|
+
return this._members.has(fingerprint);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @returns {string[]} */
|
|
200
|
+
listMembers() {
|
|
201
|
+
return [...this._members];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** @returns {number} */
|
|
205
|
+
get memberCount() {
|
|
206
|
+
return this._members.size;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ── Messages ───────────────────────────────────────────────────────
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Add a message to the room.
|
|
213
|
+
* @param {ChatMessage} msg
|
|
214
|
+
* @returns {ChatMessage}
|
|
215
|
+
*/
|
|
216
|
+
addMessage(msg) {
|
|
217
|
+
if (!this._members.has(msg.sender)) {
|
|
218
|
+
throw new Error(`Sender "${msg.sender}" is not a member of room "${this.id}"`);
|
|
219
|
+
}
|
|
220
|
+
if (this._banned.has(msg.sender)) {
|
|
221
|
+
throw new Error(`Sender "${msg.sender}" is banned from room "${this.id}"`);
|
|
222
|
+
}
|
|
223
|
+
this._messages.push(msg);
|
|
224
|
+
for (const cb of this._onMessageCbs) cb(msg);
|
|
225
|
+
return msg;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {object} [opts]
|
|
230
|
+
* @param {number} [opts.limit]
|
|
231
|
+
* @param {number} [opts.before] - Timestamp
|
|
232
|
+
* @param {number} [opts.after] - Timestamp
|
|
233
|
+
* @param {string} [opts.type]
|
|
234
|
+
* @returns {ChatMessage[]}
|
|
235
|
+
*/
|
|
236
|
+
getMessages(opts = {}) {
|
|
237
|
+
let msgs = this._messages;
|
|
238
|
+
|
|
239
|
+
if (opts.type) {
|
|
240
|
+
msgs = msgs.filter(m => m.type === opts.type);
|
|
241
|
+
}
|
|
242
|
+
if (opts.before) {
|
|
243
|
+
msgs = msgs.filter(m => m.timestamp < opts.before);
|
|
244
|
+
}
|
|
245
|
+
if (opts.after) {
|
|
246
|
+
msgs = msgs.filter(m => m.timestamp > opts.after);
|
|
247
|
+
}
|
|
248
|
+
if (opts.limit && opts.limit > 0) {
|
|
249
|
+
msgs = msgs.slice(-opts.limit);
|
|
250
|
+
}
|
|
251
|
+
return msgs;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @param {string} messageId
|
|
256
|
+
* @returns {ChatMessage|null}
|
|
257
|
+
*/
|
|
258
|
+
getMessage(messageId) {
|
|
259
|
+
return this._messages.find(m => m.id === messageId) ?? null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ── Moderation ─────────────────────────────────────────────────────
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Redact a message. Only creator can moderate.
|
|
266
|
+
* // Note: redaction is permanent by CRDT design. See OUTSTANDING.md 3.6 for discussion.
|
|
267
|
+
* @param {string} messageId
|
|
268
|
+
* @param {string} moderator - Fingerprint of the moderator
|
|
269
|
+
* @returns {boolean}
|
|
270
|
+
*/
|
|
271
|
+
redactMessage(messageId, moderator) {
|
|
272
|
+
if (moderator !== this.creator) return false;
|
|
273
|
+
const msg = this.getMessage(messageId);
|
|
274
|
+
if (!msg) return false;
|
|
275
|
+
msg._redacted = true;
|
|
276
|
+
msg.body = '[redacted]';
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Ban a member. Only creator can ban.
|
|
282
|
+
* @param {string} fingerprint
|
|
283
|
+
* @param {string} moderator
|
|
284
|
+
* @returns {boolean}
|
|
285
|
+
*/
|
|
286
|
+
ban(fingerprint, moderator) {
|
|
287
|
+
if (moderator !== this.creator) return false;
|
|
288
|
+
if (fingerprint === this.creator) return false; // can't ban self
|
|
289
|
+
this._banned.add(fingerprint);
|
|
290
|
+
this._members.delete(fingerprint);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Unban a member. Only creator can unban.
|
|
296
|
+
* @param {string} fingerprint
|
|
297
|
+
* @param {string} moderator
|
|
298
|
+
* @returns {boolean}
|
|
299
|
+
*/
|
|
300
|
+
unban(fingerprint, moderator) {
|
|
301
|
+
if (moderator !== this.creator) return false;
|
|
302
|
+
return this._banned.delete(fingerprint);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* @param {string} fingerprint
|
|
307
|
+
* @returns {boolean}
|
|
308
|
+
*/
|
|
309
|
+
isBanned(fingerprint) {
|
|
310
|
+
return this._banned.has(fingerprint);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ── Presence ───────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* @param {string} fingerprint
|
|
317
|
+
* @param {string} status - 'online'|'typing'|'idle'|'offline'
|
|
318
|
+
*/
|
|
319
|
+
setPresence(fingerprint, status) {
|
|
320
|
+
const entry = { status, lastSeen: Date.now() };
|
|
321
|
+
this._presence.set(fingerprint, entry);
|
|
322
|
+
for (const cb of this._onPresenceCbs) cb({ fingerprint, status });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* @returns {Map<string, { status: string, lastSeen: number }>}
|
|
327
|
+
*/
|
|
328
|
+
getPresence() {
|
|
329
|
+
return new Map(this._presence);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ── Events ─────────────────────────────────────────────────────────
|
|
333
|
+
|
|
334
|
+
/** @param {function} cb */
|
|
335
|
+
onMessage(cb) { this._onMessageCbs.push(cb); }
|
|
336
|
+
/** @param {function} cb */
|
|
337
|
+
onJoin(cb) { this._onJoinCbs.push(cb); }
|
|
338
|
+
/** @param {function} cb */
|
|
339
|
+
onLeave(cb) { this._onLeaveCbs.push(cb); }
|
|
340
|
+
/** @param {function} cb */
|
|
341
|
+
onPresence(cb) { this._onPresenceCbs.push(cb); }
|
|
342
|
+
|
|
343
|
+
// ── Serialization ──────────────────────────────────────────────────
|
|
344
|
+
|
|
345
|
+
toJSON() {
|
|
346
|
+
return {
|
|
347
|
+
id: this.id,
|
|
348
|
+
name: this.name,
|
|
349
|
+
creator: this.creator,
|
|
350
|
+
maxMembers: this.maxMembers,
|
|
351
|
+
retentionMs: this.retentionMs,
|
|
352
|
+
members: [...this._members],
|
|
353
|
+
banned: [...this._banned],
|
|
354
|
+
messages: this._messages.map(m => m.toJSON()),
|
|
355
|
+
presence: Object.fromEntries(this._presence),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static fromJSON(data) {
|
|
360
|
+
const room = new ChatRoom({
|
|
361
|
+
id: data.id,
|
|
362
|
+
name: data.name,
|
|
363
|
+
creator: data.creator,
|
|
364
|
+
opts: {
|
|
365
|
+
maxMembers: data.maxMembers,
|
|
366
|
+
retentionMs: data.retentionMs,
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// Restore members (creator already added in constructor)
|
|
371
|
+
if (data.members) {
|
|
372
|
+
for (const m of data.members) {
|
|
373
|
+
room._members.add(m);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Restore banned
|
|
378
|
+
if (data.banned) {
|
|
379
|
+
for (const b of data.banned) {
|
|
380
|
+
room._banned.add(b);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Restore messages
|
|
385
|
+
if (data.messages) {
|
|
386
|
+
room._messages = data.messages.map(m => ChatMessage.fromJSON(m));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Restore presence
|
|
390
|
+
if (data.presence) {
|
|
391
|
+
for (const [fp, p] of Object.entries(data.presence)) {
|
|
392
|
+
room._presence.set(fp, p);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return room;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
// MeshChat
|
|
402
|
+
// ---------------------------------------------------------------------------
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Multi-room chat manager.
|
|
406
|
+
*/
|
|
407
|
+
export class MeshChat {
|
|
408
|
+
/**
|
|
409
|
+
* @param {object} opts
|
|
410
|
+
* @param {{ fingerprint: string }} opts.identity - Local user identity
|
|
411
|
+
* @param {function} [opts.onLog]
|
|
412
|
+
*/
|
|
413
|
+
constructor({ identity, onLog }) {
|
|
414
|
+
this._identity = identity;
|
|
415
|
+
this._onLog = onLog || (() => {});
|
|
416
|
+
/** @type {Map<string, ChatRoom>} */
|
|
417
|
+
this._rooms = new Map();
|
|
418
|
+
/** @type {Map<string, function[]>} room-specific subscriptions */
|
|
419
|
+
this._subscriptions = new Map();
|
|
420
|
+
/** @type {function[]} global subscriptions */
|
|
421
|
+
this._globalSubs = [];
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ── Room management ────────────────────────────────────────────────
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* @param {string} name
|
|
428
|
+
* @param {object} [opts]
|
|
429
|
+
* @returns {ChatRoom}
|
|
430
|
+
*/
|
|
431
|
+
createRoom(name, opts = {}) {
|
|
432
|
+
const id = generateRoomId();
|
|
433
|
+
const room = new ChatRoom({
|
|
434
|
+
id,
|
|
435
|
+
name,
|
|
436
|
+
creator: this._identity.fingerprint,
|
|
437
|
+
opts,
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// Wire up message subscriptions
|
|
441
|
+
room.onMessage(msg => {
|
|
442
|
+
const subs = this._subscriptions.get(room.id);
|
|
443
|
+
if (subs) {
|
|
444
|
+
for (const cb of subs) cb(msg);
|
|
445
|
+
}
|
|
446
|
+
for (const cb of this._globalSubs) cb(msg);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
this._rooms.set(id, room);
|
|
450
|
+
return room;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* @param {string} roomId
|
|
455
|
+
* @returns {ChatRoom|null}
|
|
456
|
+
*/
|
|
457
|
+
getRoom(roomId) {
|
|
458
|
+
return this._rooms.get(roomId) ?? null;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* @returns {Array<{ id: string, name: string, memberCount: number, lastActivity: number|null }>}
|
|
463
|
+
*/
|
|
464
|
+
listRooms() {
|
|
465
|
+
return [...this._rooms.values()].map(room => {
|
|
466
|
+
const msgs = room.getMessages();
|
|
467
|
+
const lastMsg = msgs.length > 0 ? msgs[msgs.length - 1] : null;
|
|
468
|
+
return {
|
|
469
|
+
id: room.id,
|
|
470
|
+
name: room.name,
|
|
471
|
+
memberCount: room.memberCount,
|
|
472
|
+
lastActivity: lastMsg ? lastMsg.timestamp : null,
|
|
473
|
+
};
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Delete a room. Only the creator can delete.
|
|
479
|
+
* @param {string} roomId
|
|
480
|
+
* @returns {boolean}
|
|
481
|
+
*/
|
|
482
|
+
deleteRoom(roomId) {
|
|
483
|
+
const room = this._rooms.get(roomId);
|
|
484
|
+
if (!room) return false;
|
|
485
|
+
this._rooms.delete(roomId);
|
|
486
|
+
this._subscriptions.delete(roomId);
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ── Convenience ────────────────────────────────────────────────────
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Send a message to a room.
|
|
494
|
+
* @param {string} roomId
|
|
495
|
+
* @param {string} type
|
|
496
|
+
* @param {string} body
|
|
497
|
+
* @param {object} [opts]
|
|
498
|
+
* @returns {ChatMessage}
|
|
499
|
+
*/
|
|
500
|
+
send(roomId, type, body, opts = {}) {
|
|
501
|
+
const room = this._rooms.get(roomId);
|
|
502
|
+
if (!room) throw new Error(`Room "${roomId}" not found`);
|
|
503
|
+
|
|
504
|
+
const msg = new ChatMessage({
|
|
505
|
+
roomId,
|
|
506
|
+
sender: this._identity.fingerprint,
|
|
507
|
+
type,
|
|
508
|
+
body,
|
|
509
|
+
parentId: opts.parentId,
|
|
510
|
+
editOf: opts.editOf,
|
|
511
|
+
});
|
|
512
|
+
return room.addMessage(msg);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// ── Subscriptions ──────────────────────────────────────────────────
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Subscribe to messages in a specific room.
|
|
519
|
+
* @param {string} roomId
|
|
520
|
+
* @param {function} cb
|
|
521
|
+
* @returns {function} Unsubscribe function
|
|
522
|
+
*/
|
|
523
|
+
subscribe(roomId, cb) {
|
|
524
|
+
if (!this._subscriptions.has(roomId)) {
|
|
525
|
+
this._subscriptions.set(roomId, []);
|
|
526
|
+
}
|
|
527
|
+
this._subscriptions.get(roomId).push(cb);
|
|
528
|
+
return () => {
|
|
529
|
+
const subs = this._subscriptions.get(roomId);
|
|
530
|
+
if (subs) {
|
|
531
|
+
const idx = subs.indexOf(cb);
|
|
532
|
+
if (idx !== -1) subs.splice(idx, 1);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Subscribe to messages from all rooms.
|
|
539
|
+
* @param {function} cb
|
|
540
|
+
* @returns {function} Unsubscribe function
|
|
541
|
+
*/
|
|
542
|
+
subscribeAll(cb) {
|
|
543
|
+
this._globalSubs.push(cb);
|
|
544
|
+
return () => {
|
|
545
|
+
const idx = this._globalSubs.indexOf(cb);
|
|
546
|
+
if (idx !== -1) this._globalSubs.splice(idx, 1);
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// ── Stats ──────────────────────────────────────────────────────────
|
|
551
|
+
|
|
552
|
+
getStats() {
|
|
553
|
+
let totalMessages = 0;
|
|
554
|
+
let totalMembers = 0;
|
|
555
|
+
for (const room of this._rooms.values()) {
|
|
556
|
+
totalMessages += room.getMessages().length;
|
|
557
|
+
totalMembers += room.memberCount;
|
|
558
|
+
}
|
|
559
|
+
return {
|
|
560
|
+
rooms: this._rooms.size,
|
|
561
|
+
totalMessages,
|
|
562
|
+
totalMembers,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// ── Serialization ──────────────────────────────────────────────────
|
|
567
|
+
|
|
568
|
+
toJSON() {
|
|
569
|
+
return {
|
|
570
|
+
identity: { fingerprint: this._identity.fingerprint },
|
|
571
|
+
rooms: [...this._rooms.values()].map(r => r.toJSON()),
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
static fromJSON(data) {
|
|
576
|
+
const chat = new MeshChat({ identity: data.identity });
|
|
577
|
+
if (data.rooms) {
|
|
578
|
+
for (const rd of data.rooms) {
|
|
579
|
+
const room = ChatRoom.fromJSON(rd);
|
|
580
|
+
|
|
581
|
+
// Wire up message subscriptions
|
|
582
|
+
room.onMessage(msg => {
|
|
583
|
+
const subs = chat._subscriptions.get(room.id);
|
|
584
|
+
if (subs) {
|
|
585
|
+
for (const cb of subs) cb(msg);
|
|
586
|
+
}
|
|
587
|
+
for (const cb of chat._globalSubs) cb(msg);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
chat._rooms.set(room.id, room);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return chat;
|
|
594
|
+
}
|
|
595
|
+
}
|
package/src/compat.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* compat.mjs -- BrowserTool shim for standalone use.
|
|
3
|
+
*
|
|
4
|
+
* In clawser, BrowserTool is the base class for AI-agent-callable tools.
|
|
5
|
+
* Subclasses override name/description/parameters/permission as GETTERS
|
|
6
|
+
* (not constructor-assigned properties) — this shim mirrors that exactly
|
|
7
|
+
* (see web/clawser-tools.js). An earlier version of this shim tried to
|
|
8
|
+
* assign `this.name = ...` in the constructor, which threw
|
|
9
|
+
* "Cannot set property name of #<Tool> which has only a getter" for
|
|
10
|
+
* every real subclass, since none of them define a setter (same bug
|
|
11
|
+
* found and fixed in browsermesh-core's compat.mjs).
|
|
12
|
+
*/
|
|
13
|
+
export class BrowserTool {
|
|
14
|
+
/** @returns {object} ToolSpec-compatible object */
|
|
15
|
+
get spec() {
|
|
16
|
+
return {
|
|
17
|
+
name: this.name,
|
|
18
|
+
description: this.description,
|
|
19
|
+
parameters: this.parameters,
|
|
20
|
+
required_permission: this.permission,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get name() { throw new Error('implement name'); }
|
|
25
|
+
get description() { throw new Error('implement description'); }
|
|
26
|
+
get parameters() { return { type: 'object', properties: {} }; }
|
|
27
|
+
get permission() { return 'internal'; }
|
|
28
|
+
|
|
29
|
+
async execute(_params) { throw new Error('Not implemented'); }
|
|
30
|
+
}
|