@nhtio/rocket-chat-openclaw-integration 0.1.0-master-7f84cdcb

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/index.mjs ADDED
@@ -0,0 +1,630 @@
1
+ import { defineChannelPluginEntry as e } from "openclaw/plugin-sdk/core";
2
+ //#region src/rocketchat/config/defaults.ts
3
+ var t = "pairing", n = "allowlist", r = !0;
4
+ function i(e) {
5
+ return {
6
+ ...e,
7
+ dmPolicy: e?.dmPolicy ?? "pairing",
8
+ groupPolicy: e?.groupPolicy ?? "allowlist"
9
+ };
10
+ }
11
+ //#endregion
12
+ //#region src/rocketchat/config/resolve_account.ts
13
+ function a(e) {
14
+ let t = e.trim();
15
+ if (!t) throw Error("rocketchat: accountId is required");
16
+ return t;
17
+ }
18
+ function o(e, t) {
19
+ let n = e.trim();
20
+ if (!n) throw Error(`rocketchat: ${t} is required`);
21
+ if (!n.startsWith("https://")) throw Error(`rocketchat: ${t} must start with https:// (got ${n})`);
22
+ return n.replace(/\/$/, "");
23
+ }
24
+ function s(e) {
25
+ return (e ?? []).map((e) => e.trim()).filter((e) => e.length > 0);
26
+ }
27
+ function c(e, t) {
28
+ return {
29
+ ...e ?? {},
30
+ ...t ?? {}
31
+ };
32
+ }
33
+ function l(e) {
34
+ let t = a(e.accountId), n = e.cfg.channels?.rocketchat, r = n?.enabled !== !1, i = n?.accounts?.[t], l = i?.enabled !== !1, u = r && l, d = i?.serverUrl ?? n?.serverUrl, f = i?.userId, p = i?.username, m = i?.pat;
35
+ if (!u) return {
36
+ accountId: t,
37
+ enabled: !1,
38
+ serverUrl: d?.trim() || "",
39
+ userId: f?.trim() || "",
40
+ username: p?.trim() || "",
41
+ pat: m?.trim() || "",
42
+ dmPolicy: i?.dmPolicy ?? n?.dmPolicy ?? "pairing",
43
+ groupPolicy: i?.groupPolicy ?? n?.groupPolicy ?? "allowlist",
44
+ allowFrom: s(i?.allowFrom ?? n?.allowFrom),
45
+ commandAllowFrom: s(i?.commandAllowFrom ?? n?.commandAllowFrom),
46
+ rooms: c(n?.rooms, i?.rooms),
47
+ defaultRequireMention: !0
48
+ };
49
+ let h = o(d ?? "", "serverUrl"), g = (f ?? "").trim(), _ = (p ?? "").trim(), v = (m ?? "").trim();
50
+ if (!g) throw Error(`rocketchat: userId is required for ${t}`);
51
+ if (!_) throw Error(`rocketchat: username is required for ${t}`);
52
+ if (!v) throw Error(`rocketchat: pat is required for ${t}`);
53
+ return {
54
+ accountId: t,
55
+ enabled: !0,
56
+ serverUrl: h,
57
+ userId: g,
58
+ username: _,
59
+ pat: v,
60
+ dmPolicy: i?.dmPolicy ?? n?.dmPolicy ?? "pairing",
61
+ groupPolicy: i?.groupPolicy ?? n?.groupPolicy ?? "allowlist",
62
+ allowFrom: s(i?.allowFrom ?? n?.allowFrom),
63
+ commandAllowFrom: s(i?.commandAllowFrom ?? n?.commandAllowFrom),
64
+ rooms: c(n?.rooms, i?.rooms),
65
+ defaultRequireMention: !0
66
+ };
67
+ }
68
+ //#endregion
69
+ //#region src/rocketchat/policy/allowlist.ts
70
+ function u(e) {
71
+ return e.trim().toLowerCase();
72
+ }
73
+ function d(e) {
74
+ let t = [];
75
+ return e.id && t.push(u(e.id)), e.username && t.push(u(e.username)), t;
76
+ }
77
+ function f(e) {
78
+ let t = e.allowFrom.map(u);
79
+ if (t.includes("*")) return { allowed: !0 };
80
+ let n = d(e.sender);
81
+ return n.length === 0 ? {
82
+ allowed: !1,
83
+ reason: "missing-sender-id"
84
+ } : n.some((e) => t.includes(e)) ? { allowed: !0 } : {
85
+ allowed: !1,
86
+ reason: "not-allowlisted"
87
+ };
88
+ }
89
+ //#endregion
90
+ //#region src/rocketchat/policy/mentions.ts
91
+ function p(e) {
92
+ let t = e.text, n = e.botUsername.trim().replace(/^@/, "");
93
+ return n ? t.includes(`@${n}`) : !1;
94
+ }
95
+ //#endregion
96
+ //#region src/rocketchat/policy/policy.ts
97
+ function m(e) {
98
+ if (!e.account.enabled) return {
99
+ allowed: !1,
100
+ reason: "account-disabled"
101
+ };
102
+ let t = f({
103
+ allowFrom: e.account.allowFrom,
104
+ sender: e.sender
105
+ });
106
+ if (e.chatType === "direct") return e.account.dmPolicy === "disabled" ? {
107
+ allowed: !1,
108
+ reason: "dm-disabled"
109
+ } : e.account.dmPolicy === "open" || e.account.dmPolicy === "allowlist" ? t.allowed ? { allowed: !0 } : {
110
+ allowed: !1,
111
+ reason: t.reason
112
+ } : t.allowed ? { allowed: !0 } : {
113
+ allowed: !1,
114
+ reason: t.reason ?? "pairing-required"
115
+ };
116
+ if (e.account.groupPolicy === "disabled") return {
117
+ allowed: !1,
118
+ reason: "group-disabled"
119
+ };
120
+ let n = e.account.rooms[e.roomId];
121
+ if ((n?.requireMention ?? e.account.defaultRequireMention) && !p({
122
+ text: e.text,
123
+ botUsername: e.account.username
124
+ })) return {
125
+ allowed: !1,
126
+ reason: "missing-mention"
127
+ };
128
+ if (e.account.groupPolicy === "allowlist" && !n) return {
129
+ allowed: !1,
130
+ reason: "room-not-allowlisted"
131
+ };
132
+ if (!t.allowed) return {
133
+ allowed: !1,
134
+ reason: t.reason
135
+ };
136
+ let r = n?.users ?? [];
137
+ if (r.length > 0) {
138
+ let t = f({
139
+ allowFrom: r,
140
+ sender: e.sender
141
+ });
142
+ if (!t.allowed) return {
143
+ allowed: !1,
144
+ reason: t.reason ?? "room-user-not-allowlisted"
145
+ };
146
+ }
147
+ return { allowed: !0 };
148
+ }
149
+ function h(e) {
150
+ if (!e.account.enabled) return {
151
+ allowed: !1,
152
+ reason: "account-disabled"
153
+ };
154
+ let t = f({
155
+ allowFrom: e.account.allowFrom,
156
+ sender: e.sender
157
+ });
158
+ if (!t.allowed) return {
159
+ allowed: !1,
160
+ reason: t.reason
161
+ };
162
+ if (e.command === "status") return { allowed: !0 };
163
+ let n = e.account.commandAllowFrom;
164
+ if (e.chatType === "group" && n.length === 0) return {
165
+ allowed: !1,
166
+ reason: "operator-allowlist-empty"
167
+ };
168
+ let r = f({
169
+ allowFrom: n,
170
+ sender: e.sender
171
+ });
172
+ return r.allowed ? { allowed: !0 } : {
173
+ allowed: !1,
174
+ reason: r.reason ?? "not-operator"
175
+ };
176
+ }
177
+ //#endregion
178
+ //#region src/rocketchat/ddp/fake_transport.ts
179
+ var g = class {
180
+ messageHandlers = [];
181
+ closeHandlers = [];
182
+ sent = [];
183
+ closed = !1;
184
+ onMessage(e) {
185
+ this.messageHandlers.push(e);
186
+ }
187
+ onClose(e) {
188
+ this.closeHandlers.push(e);
189
+ }
190
+ send(e) {
191
+ this.sent.push(e);
192
+ }
193
+ close(e) {
194
+ this.closed = !0;
195
+ for (let t of this.closeHandlers) t(e);
196
+ }
197
+ inject(e) {
198
+ for (let t of this.messageHandlers) t(e);
199
+ }
200
+ };
201
+ //#endregion
202
+ //#region src/rocketchat/ddp/protocol.ts
203
+ function _() {
204
+ return {
205
+ msg: "connect",
206
+ version: "1",
207
+ support: ["1"]
208
+ };
209
+ }
210
+ function v(e) {
211
+ return e.transport.onMessage((t) => {
212
+ let n = t;
213
+ n && n.msg === "ping" && e.transport.send({ msg: "pong" });
214
+ }), { sendConnect: () => {
215
+ e.transport.send(_());
216
+ } };
217
+ }
218
+ //#endregion
219
+ //#region src/rocketchat/ddp/methods.ts
220
+ function y(e) {
221
+ if (!e || typeof e != "object") return !1;
222
+ let t = e;
223
+ return t.msg === "result" && typeof t.id == "string";
224
+ }
225
+ var b = class {
226
+ nextId = 1;
227
+ pending = /* @__PURE__ */ new Map();
228
+ constructor(e) {
229
+ this.transport = e, this.transport.onMessage((e) => {
230
+ if (!y(e)) return;
231
+ let t = this.pending.get(e.id);
232
+ if (t) {
233
+ if (this.pending.delete(e.id), e.error !== void 0) {
234
+ t.reject(e.error);
235
+ return;
236
+ }
237
+ t.resolve(e.result);
238
+ }
239
+ }), this.transport.onClose((e) => {
240
+ let t = /* @__PURE__ */ Error(`DDP transport closed${e?.code ? ` (code=${e.code})` : ""}${e?.reason ? ` reason=${e.reason}` : ""}`);
241
+ for (let [, e] of this.pending) e.reject(t);
242
+ this.pending.clear();
243
+ });
244
+ }
245
+ call(e, t) {
246
+ let n = String(this.nextId++), r = {
247
+ msg: "method",
248
+ method: e,
249
+ id: n
250
+ };
251
+ t && (r.params = t);
252
+ let i = new Promise((e, t) => {
253
+ this.pending.set(n, {
254
+ resolve: e,
255
+ reject: t
256
+ });
257
+ });
258
+ return this.transport.send(r), i;
259
+ }
260
+ };
261
+ //#endregion
262
+ //#region src/rocketchat/ddp/auth.ts
263
+ async function x(e) {
264
+ return await e.ddp.call("login", [{ resume: e.resumeToken }]);
265
+ }
266
+ //#endregion
267
+ //#region src/rocketchat/ddp/subscriptions.ts
268
+ function S(e) {
269
+ if (!e || typeof e != "object") return !1;
270
+ let t = e;
271
+ return t.msg === "ready" && Array.isArray(t.subs);
272
+ }
273
+ var C = class {
274
+ nextId = 1;
275
+ constructor(e) {
276
+ this.transport = e;
277
+ }
278
+ subscribe(e, t) {
279
+ let n = String(this.nextId++), r = {
280
+ msg: "sub",
281
+ id: n,
282
+ name: e,
283
+ params: t
284
+ }, i = new Promise((e) => {
285
+ this.transport.onMessage((t) => {
286
+ S(t) && t.subs.includes(n) && e({ subId: n });
287
+ });
288
+ });
289
+ return this.transport.send(r), i;
290
+ }
291
+ subscribeRoomMessages(e) {
292
+ return this.subscribe("stream-room-messages", [e, !1]);
293
+ }
294
+ }, w = class {
295
+ ddp;
296
+ subs;
297
+ protocol;
298
+ constructor(e) {
299
+ this.ddp = new b(e), this.subs = new C(e), this.protocol = v({ transport: e });
300
+ }
301
+ connect() {
302
+ this.protocol.sendConnect();
303
+ }
304
+ async loginWithResume(e) {
305
+ await x({
306
+ ddp: this.ddp,
307
+ resumeToken: e
308
+ });
309
+ }
310
+ async subscribeRoomMessages(e) {
311
+ for (let t of e) await this.subs.subscribeRoomMessages(t);
312
+ }
313
+ async start(e) {
314
+ this.connect(), await this.loginWithResume(e.resumeToken), await this.subscribeRoomMessages(e.roomIds);
315
+ }
316
+ };
317
+ //#endregion
318
+ //#region src/rocketchat/inbound/decode_message.ts
319
+ function T(e) {
320
+ return !!e && typeof e == "object";
321
+ }
322
+ function E(e) {
323
+ return {
324
+ id: (e?._id ?? "").trim(),
325
+ username: e?.username?.trim() || void 0
326
+ };
327
+ }
328
+ function D(e) {
329
+ let t = (e.rid ?? "").trim(), n = (e._id ?? "").trim(), r = (e.msg ?? "").toString(), i = E(e.u), a = e.tmid?.trim() || void 0;
330
+ return !t || !n || !i.id ? null : {
331
+ roomId: t,
332
+ messageId: n,
333
+ text: r,
334
+ sender: i,
335
+ threadId: a
336
+ };
337
+ }
338
+ function O(e) {
339
+ if (!T(e)) return null;
340
+ if ("rid" in e || "_id" in e && "u" in e) return D(e);
341
+ let t = e;
342
+ if (t.msg !== "changed" || t.collection && t.collection !== "stream-room-messages" && t.collection !== "stream-room-messages-realtime") return null;
343
+ let n = t.fields?.args, r = Array.isArray(n) ? n[0] : void 0;
344
+ return T(r) ? D(r) : null;
345
+ }
346
+ //#endregion
347
+ //#region src/rocketchat/inbound/classify_chat_type.ts
348
+ function k(e) {
349
+ return e.roomTypeById?.[e.roomId] ?? "group";
350
+ }
351
+ //#endregion
352
+ //#region src/rocketchat/inbound/session_keys.ts
353
+ function A(e) {
354
+ let t = e.roomId.trim(), n = e.threadId?.trim() || void 0;
355
+ return n ? {
356
+ baseConversationId: t,
357
+ threadId: n
358
+ } : { baseConversationId: t };
359
+ }
360
+ //#endregion
361
+ //#region src/rocketchat/inbound/process_inbound.ts
362
+ function j(e) {
363
+ let t = O(e.rawEvent);
364
+ if (!t) return {
365
+ ok: !1,
366
+ reason: "unrecognized-message-shape"
367
+ };
368
+ if (t.sender.id === e.account.userId) return {
369
+ ok: !1,
370
+ reason: "self-message"
371
+ };
372
+ let n = k({
373
+ roomId: t.roomId,
374
+ roomTypeById: e.roomTypeById
375
+ }), r = m({
376
+ account: e.account,
377
+ chatType: n,
378
+ roomId: t.roomId,
379
+ sender: t.sender,
380
+ text: t.text
381
+ });
382
+ return r.allowed ? {
383
+ ok: !0,
384
+ value: {
385
+ roomId: t.roomId,
386
+ messageId: t.messageId,
387
+ text: t.text,
388
+ sender: t.sender,
389
+ chatType: n,
390
+ threadId: t.threadId
391
+ }
392
+ } : {
393
+ ok: !1,
394
+ reason: r.reason ?? "blocked"
395
+ };
396
+ }
397
+ //#endregion
398
+ //#region src/rocketchat/rest/post_message.ts
399
+ function M(e) {
400
+ let t = e.trim().replace(/\/$/, "");
401
+ if (!t.startsWith("https://")) throw Error(`rocketchat: serverUrl must start with https:// (got ${t})`);
402
+ return t;
403
+ }
404
+ function N(e) {
405
+ let t = `${M(e.serverUrl)}/api/v1/chat.postMessage`, n = {
406
+ rid: e.roomId,
407
+ msg: e.text
408
+ };
409
+ return e.threadId && (n.tmid = e.threadId), {
410
+ url: t,
411
+ method: "POST",
412
+ headers: {
413
+ "Content-Type": "application/json",
414
+ "X-User-Id": e.userId,
415
+ "X-Auth-Token": e.authToken
416
+ },
417
+ body: JSON.stringify(n)
418
+ };
419
+ }
420
+ async function P(e) {
421
+ let t = N(e), n = await (e.fetcher ?? fetch)(t.url, {
422
+ method: t.method,
423
+ headers: t.headers,
424
+ body: t.body
425
+ }), r = await n.text();
426
+ if (!n.ok) throw Error(`rocketchat: chat.postMessage failed (${n.status}): ${r}`);
427
+ try {
428
+ return JSON.parse(r);
429
+ } catch {
430
+ return r;
431
+ }
432
+ }
433
+ //#endregion
434
+ //#region src/rocketchat/rest/react.ts
435
+ function F(e) {
436
+ let t = e.trim().replace(/\/$/, "");
437
+ if (!t.startsWith("https://")) throw Error(`rocketchat: serverUrl must start with https:// (got ${t})`);
438
+ return t;
439
+ }
440
+ function I(e) {
441
+ return {
442
+ url: `${F(e.serverUrl)}/api/v1/chat.react`,
443
+ method: "POST",
444
+ headers: {
445
+ "Content-Type": "application/json",
446
+ "X-User-Id": e.userId,
447
+ "X-Auth-Token": e.authToken
448
+ },
449
+ body: JSON.stringify({
450
+ messageId: e.messageId,
451
+ emoji: e.emoji,
452
+ shouldReact: e.shouldReact
453
+ })
454
+ };
455
+ }
456
+ async function L(e) {
457
+ let t = I(e), n = await (e.fetcher ?? fetch)(t.url, {
458
+ method: t.method,
459
+ headers: t.headers,
460
+ body: t.body
461
+ }), r = await n.text();
462
+ if (!n.ok) throw Error(`rocketchat: chat.react failed (${n.status}): ${r}`);
463
+ try {
464
+ return JSON.parse(r);
465
+ } catch {
466
+ return r;
467
+ }
468
+ }
469
+ //#endregion
470
+ //#region src/rocketchat/rest/me.ts
471
+ function R(e) {
472
+ let t = e.trim().replace(/\/$/, "");
473
+ if (!t.startsWith("https://")) throw Error(`rocketchat: serverUrl must start with https:// (got ${t})`);
474
+ return t;
475
+ }
476
+ function z(e) {
477
+ return {
478
+ url: `${R(e.serverUrl)}/api/v1/me`,
479
+ method: "GET",
480
+ headers: {
481
+ "X-User-Id": e.userId,
482
+ "X-Auth-Token": e.authToken
483
+ }
484
+ };
485
+ }
486
+ async function B(e) {
487
+ let t = z(e), n = await (e.fetcher ?? fetch)(t.url, {
488
+ method: t.method,
489
+ headers: t.headers
490
+ }), r = await n.text();
491
+ if (!n.ok) throw Error(`rocketchat: /api/v1/me failed (${n.status}): ${r}`);
492
+ try {
493
+ return JSON.parse(r);
494
+ } catch {
495
+ return r;
496
+ }
497
+ }
498
+ //#endregion
499
+ //#region src/rocketchat/commands/parse.ts
500
+ function V(e) {
501
+ return e.trim().split(/\s+/).map((e) => e.trim()).filter((e) => e.length > 0);
502
+ }
503
+ function H(e) {
504
+ let t = e.trim();
505
+ if (!t.startsWith("!")) return { kind: "not-a-command" };
506
+ let n = V(t.slice(1)), r = (n[0] ?? "").toLowerCase();
507
+ if (r === "status") return {
508
+ kind: "command",
509
+ name: "status",
510
+ raw: t
511
+ };
512
+ if (r === "stop") return {
513
+ kind: "command",
514
+ name: "stop",
515
+ raw: t
516
+ };
517
+ if (r === "model") {
518
+ let e = n.slice(1).join(" ").trim();
519
+ return e ? {
520
+ kind: "command",
521
+ name: "model",
522
+ raw: t,
523
+ model: e
524
+ } : { kind: "not-a-command" };
525
+ }
526
+ return { kind: "not-a-command" };
527
+ }
528
+ //#endregion
529
+ //#region src/rocketchat/commands/dispatch.ts
530
+ function U(e) {
531
+ if (e.parsed.kind !== "command") return {
532
+ ok: !1,
533
+ reason: "not-a-command"
534
+ };
535
+ let t = h({
536
+ account: e.account,
537
+ chatType: e.chatType,
538
+ sender: e.sender,
539
+ command: e.parsed.name
540
+ });
541
+ return t.allowed ? e.parsed.name === "status" ? {
542
+ ok: !0,
543
+ kind: "openclaw-command",
544
+ commandText: "/status"
545
+ } : e.parsed.name === "stop" ? {
546
+ ok: !0,
547
+ kind: "openclaw-command",
548
+ commandText: "/stop"
549
+ } : {
550
+ ok: !0,
551
+ kind: "openclaw-command",
552
+ commandText: `/model ${e.parsed.model}`
553
+ } : {
554
+ ok: !1,
555
+ reason: t.reason ?? "blocked"
556
+ };
557
+ }
558
+ //#endregion
559
+ //#region src/rocketchat/security/revocation.ts
560
+ function W(e) {
561
+ e.transport.close({ reason: `unauthorized: ${e.reason}` });
562
+ }
563
+ //#endregion
564
+ //#region src/rocketchat/security/revalidate.ts
565
+ function G(e) {
566
+ let t = !1, n = setInterval(async () => {
567
+ if (!t) try {
568
+ let t = await e.validateOnce();
569
+ t.ok || W({
570
+ transport: e.transport,
571
+ reason: t.reason
572
+ });
573
+ } catch (t) {
574
+ let n = t instanceof Error ? t.message : String(t);
575
+ W({
576
+ transport: e.transport,
577
+ reason: n
578
+ });
579
+ }
580
+ }, e.options.intervalMs);
581
+ return { stop: () => {
582
+ t = !0, clearInterval(n);
583
+ } };
584
+ }
585
+ //#endregion
586
+ //#region src/openclaw/runtime/account_runner.ts
587
+ async function K(e) {
588
+ let t = e.createTransport({ account: e.account }), n = new w(t);
589
+ await n.start({
590
+ resumeToken: e.account.pat,
591
+ roomIds: e.roomIds
592
+ });
593
+ let r = G({
594
+ transport: t,
595
+ options: { intervalMs: e.revalidateIntervalMs ?? 300 * 1e3 },
596
+ validateOnce: async () => {
597
+ try {
598
+ return await B({
599
+ serverUrl: e.account.serverUrl,
600
+ userId: e.account.userId,
601
+ authToken: e.account.pat,
602
+ fetcher: e.fetcher
603
+ }), { ok: !0 };
604
+ } catch (e) {
605
+ return {
606
+ ok: !1,
607
+ reason: e instanceof Error ? e.message : String(e)
608
+ };
609
+ }
610
+ }
611
+ });
612
+ return {
613
+ client: n,
614
+ stop: () => {
615
+ r.stop(), t.close({ reason: "stopped" });
616
+ }
617
+ };
618
+ }
619
+ //#endregion
620
+ //#region src/index.ts
621
+ var q = "0.1.0-master-7f84cdcb", J = e({
622
+ id: "rocketchat",
623
+ name: "Rocket.Chat",
624
+ description: "Rocket.Chat channel plugin",
625
+ plugin: null
626
+ });
627
+ //#endregion
628
+ export { t as DEFAULT_DM_POLICY, n as DEFAULT_GROUP_POLICY, r as DEFAULT_REQUIRE_MENTION, b as DdpMethodClient, C as DdpSubscriptionClient, g as FakeDdpTransport, w as RocketChatClient, h as authorizeCommand, m as authorizeInboundMessage, z as buildMeRequest, N as buildPostMessageRequest, I as buildReactRequest, k as classifyChatType, A as computeSessionConversation, x as ddpLoginWithResume, O as decodeRoomMessageEvent, J as default, U as dispatchPrefixCommand, B as getMe, v as installDdpProtocol, _ as makeConnectFrame, H as parsePrefixCommand, P as postMessage, j as processInboundRoomMessage, L as reactToMessage, i as resolveChannelDefaults, l as resolveRocketChatAccount, W as revokeOnUnauthorized, G as startCredentialRevalidation, K as startRocketChatAccount, q as version };
629
+
630
+ //# sourceMappingURL=index.mjs.map