hyperclaw 4.0.1 → 5.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/README.md +58 -4
- package/dist/a2ui-protocol-CT_jDEU9.js +75 -0
- package/dist/a2ui-protocol-CfBI44-Q.js +75 -0
- package/dist/agents-routing-683Q2JGp.js +129 -0
- package/dist/agents-routing-BpZBswBH.js +4 -0
- package/dist/agents-routing-ChHiZp36.js +327 -0
- package/dist/agents-routing-ChqZ6l2S.js +4 -0
- package/dist/api-keys-guide-BCcOl0Q7.js +149 -0
- package/dist/api-keys-guide-Dq5Obbp4.js +149 -0
- package/dist/audit-BYxPlnTQ.js +248 -0
- package/dist/audit-BaIiyWFu.js +441 -0
- package/dist/bounty-tools-C6LyzxM-.js +211 -0
- package/dist/bounty-tools-DWudyZie.js +211 -0
- package/dist/browser-tools-BsTeGMnX.js +5 -0
- package/dist/browser-tools-CQBSbIuO.js +5 -0
- package/dist/browser-tools-D8_rLe2p.js +179 -0
- package/dist/browser-tools-YQmwRLLM.js +179 -0
- package/dist/claw-tasks-BRLUvFRD.js +80 -0
- package/dist/claw-tasks-CgTsiNE8.js +80 -0
- package/dist/connector-3HnyH8fn.js +167 -0
- package/dist/connector-5N0-X_xs.js +194 -0
- package/dist/connector-6PMZo5Ky.js +189 -0
- package/dist/connector-B3v0qcXg.js +425 -0
- package/dist/connector-B6eoF3DD.js +181 -0
- package/dist/connector-B8R3iBY1.js +280 -0
- package/dist/connector-B9tLG8UZ.js +196 -0
- package/dist/connector-BAM-08NN.js +189 -0
- package/dist/connector-BC8FIVu4.js +181 -0
- package/dist/connector-BDmwwaVc.js +213 -0
- package/dist/connector-BGjbBy69.js +225 -0
- package/dist/connector-BO2SRzfG.js +218 -0
- package/dist/connector-BOlqjXWP.js +182 -0
- package/dist/connector-BP8zsbP8.js +189 -0
- package/dist/connector-BPoSevxp.js +286 -0
- package/dist/connector-BRHj773i.js +163 -0
- package/dist/connector-BToxU-jV.js +267 -0
- package/dist/connector-BfXky0L3.js +167 -0
- package/dist/connector-BiiSJpx3.js +192 -0
- package/dist/connector-BliDVsJQ.js +239 -0
- package/dist/connector-BnDmIhIu.js +85 -0
- package/dist/connector-Bv6s9oP7.js +88 -0
- package/dist/connector-By5wWGTR.js +343 -0
- package/dist/connector-C1BaFFgN.js +213 -0
- package/dist/connector-C1HSoUyk.js +189 -0
- package/dist/connector-CKQHZOXg.js +568 -0
- package/dist/connector-CRRWY5Wv.js +167 -0
- package/dist/connector-CRl-iidy.js +239 -0
- package/dist/connector-CXPQVGyI.js +85 -0
- package/dist/connector-Cdk1CXKi.js +194 -0
- package/dist/connector-Ci9glMD-.js +340 -0
- package/dist/connector-CjtZIEDj.js +181 -0
- package/dist/connector-Ck6JtOsX.js +531 -0
- package/dist/connector-CwlgFgjx.js +181 -0
- package/dist/connector-D8Kelee0.js +286 -0
- package/dist/connector-DAnRJ0oP.js +162 -0
- package/dist/connector-DFchk6l7.js +178 -0
- package/dist/connector-DKw7tRAy.js +192 -0
- package/dist/connector-DRv1ahC_.js +2 -2
- package/dist/connector-DU63KW94.js +165 -0
- package/dist/connector-DXTp5PE8.js +508 -0
- package/dist/connector-Dbvb1Cj9.js +280 -0
- package/dist/connector-DcZdQcgR.js +173 -0
- package/dist/connector-Dih6dUPP.js +173 -0
- package/dist/connector-DqTH_tPX.js +182 -0
- package/dist/connector-DrnEiiyP.js +419 -0
- package/dist/connector-DtR5GGTX.js +167 -0
- package/dist/connector-DxKL8VvZ.js +182 -0
- package/dist/connector-T_YdZtzv.js +162 -0
- package/dist/connector-Tky_qS_K.js +350 -0
- package/dist/connector-ZSc3oTTy.js +305 -0
- package/dist/connector-i4gOS9xL.js +154 -0
- package/dist/connector-rHXE1ZD2.js +167 -0
- package/dist/connector-sW5yhU1m.js +498 -0
- package/dist/connector-u3ICd3Ic.js +552 -0
- package/dist/connector-wdUXChwa.js +172 -0
- package/dist/cost-tracker-DD9wtWsr.js +103 -0
- package/dist/cost-tracker-pVE15Yq4.js +103 -0
- package/dist/credentials-store-BvnMPJwi.js +4 -0
- package/dist/credentials-store-C6ir0Dae.js +4 -0
- package/dist/credentials-store-H13LqOwJ.js +77 -0
- package/dist/credentials-store-sb-TRLwR.js +77 -0
- package/dist/cron-tasks-Bli7Kzd2.js +82 -0
- package/dist/cron-tasks-BvDFNyiE.js +82 -0
- package/dist/daemon-Bg4GtCmc.js +318 -0
- package/dist/daemon-DhmwY8k4.js +5 -0
- package/dist/delivery-BmIYy9VQ.js +4 -0
- package/dist/delivery-D5Z98EVq.js +95 -0
- package/dist/delivery-DCOXhXEO.js +5 -0
- package/dist/delivery-pWUPBp1F.js +95 -0
- package/dist/destructive-gate-D6vWOdEl.js +101 -0
- package/dist/destructive-gate-m-dWqUFg.js +101 -0
- package/dist/developer-keys-CPWT7Q6S.js +8 -0
- package/dist/developer-keys-DrrcUqFa.js +127 -0
- package/dist/developer-keys-JaJK3T27.js +127 -0
- package/dist/developer-keys-kyqmtWK3.js +8 -0
- package/dist/doctor-3oi89QIc.js +175 -0
- package/dist/doctor-BvCe8BBk.js +230 -0
- package/dist/doctor-Cf1XSfp9.js +4 -0
- package/dist/doctor-CxyPLYsJ.js +6 -0
- package/dist/engine-B4eMiTgl.js +7 -0
- package/dist/engine-B8M7dYul.js +7 -0
- package/dist/engine-BhT-1M9W.js +256 -0
- package/dist/engine-CEDSqXfw.js +256 -0
- package/dist/engine-D49jnSd_.js +256 -0
- package/dist/engine-Da4JMNpI.js +7 -0
- package/dist/env-resolve-CiXbWYwe.js +10 -0
- package/dist/env-resolve-CmGWhWXJ.js +115 -0
- package/dist/env-resolve-DWOQ45jG.js +9 -0
- package/dist/env-resolve-szSWl0UF.js +94 -0
- package/dist/extraction-tools-D3qDFBJ1.js +91 -0
- package/dist/extraction-tools-DLr_AEwq.js +5 -0
- package/dist/extraction-tools-HOZstZ0y.js +91 -0
- package/dist/extraction-tools-m4lmAv7l.js +5 -0
- package/dist/form_data-B_hIUrxU.js +8657 -0
- package/dist/form_data-Cz040rio.js +8657 -0
- package/dist/gmail-watch-setup-Czt8rXaX.js +40 -0
- package/dist/gmail-watch-setup-Du7DVV7S.js +40 -0
- package/dist/health-B-asI__D.js +6 -0
- package/dist/health-Ds2YlpTB.js +152 -0
- package/dist/heartbeat-engine-BYT5ayQH.js +83 -0
- package/dist/heartbeat-engine-CRqfPcFM.js +83 -0
- package/dist/hub-D0XwdjM-.js +515 -0
- package/dist/hub-DTsqe5Bt.js +6 -0
- package/dist/hub-FrPTA33j.js +515 -0
- package/dist/hub-LiD5Iztb.js +6 -0
- package/dist/hyperclawbot-D9KCtc4P.js +480 -0
- package/dist/hyperclawbot-Dw27pJo4.js +480 -0
- package/dist/hyperclawbot-zvczQgKx.js +505 -0
- package/dist/inference-BKVkBREb.js +6 -0
- package/dist/inference-CTWJeX9Q.js +922 -0
- package/dist/inference-DCXH4Q3x.js +922 -0
- package/dist/inference-ix607p7k.js +6 -0
- package/dist/knowledge-graph-DqA-Fztl.js +131 -0
- package/dist/knowledge-graph-iBG76fvm.js +131 -0
- package/dist/loader-CC45xGpC.js +4 -0
- package/dist/loader-CISCqBto.js +400 -0
- package/dist/loader-CYMQ8VOS.js +4 -0
- package/dist/loader-CnEdOyjT.js +400 -0
- package/dist/logger-8tEtAd3y.js +83 -0
- package/dist/logger-ybOp7VOC.js +83 -0
- package/dist/manager-03ipO9R0.js +105 -0
- package/dist/manager-BpDfbDjg.js +117 -0
- package/dist/manager-Bxl0sqlh.js +4 -0
- package/dist/manager-CPjeRe-6.js +4 -0
- package/dist/manager-CrVDn6eN.js +6 -0
- package/dist/manager-Cwzj7w5R.js +105 -0
- package/dist/manager-DLmZI-9R.js +6 -0
- package/dist/manager-DSGhn5i3.js +117 -0
- package/dist/manager-DgyF52mg.js +218 -0
- package/dist/manager-Dm8nrMFx.js +40 -0
- package/dist/manager-FCgF1plu.js +218 -0
- package/dist/manager-rgCsaWT1.js +40 -0
- package/dist/mcp-B_9Ber63.js +139 -0
- package/dist/mcp-CfoSU4Uz.js +139 -0
- package/dist/mcp-loader-DSM5UiFG.js +94 -0
- package/dist/mcp-loader-DkRBsLpk.js +94 -0
- package/dist/mcp-loader-j5ZLLw5O.js +94 -0
- package/dist/memory-BI1kPkAN.js +4 -0
- package/dist/memory-BVFGkxxK.js +270 -0
- package/dist/memory-BlHL7JCO.js +4 -0
- package/dist/memory-DsS_eFvJ.js +270 -0
- package/dist/memory-auto-Bc7euou4.js +306 -0
- package/dist/memory-auto-BkvtSFUw.js +5 -0
- package/dist/memory-auto-Bnz_-1wP.js +306 -0
- package/dist/memory-auto-DPfbkMVt.js +5 -0
- package/dist/memory-integration-DZExqWr4.js +91 -0
- package/dist/memory-integration-cSYkZyEo.js +91 -0
- package/dist/moltbook-B6ZeGN5_.js +81 -0
- package/dist/moltbook-BtLDZTfM.js +81 -0
- package/dist/node-Dw2Gi-cP.js +222 -0
- package/dist/node-pwL6O_KX.js +222 -0
- package/dist/nodes-registry-B8dmrlLv.js +52 -0
- package/dist/nodes-registry-CsPm_-CJ.js +52 -0
- package/dist/oauth-flow-CpWlgvNB.js +150 -0
- package/dist/oauth-flow-DQPvMHRH.js +150 -0
- package/dist/oauth-provider-BZb6qOw5.js +110 -0
- package/dist/oauth-provider-Uo4Nib_c.js +110 -0
- package/dist/observability-B43YvNQV.js +89 -0
- package/dist/observability-BV-Yx0V9.js +89 -0
- package/dist/onboard-0WoDxbv_.js +10 -0
- package/dist/onboard-BXNXCQp4.js +4070 -0
- package/dist/onboard-Bd_wsYdi.js +4086 -0
- package/dist/onboard-CAN7x3me.js +3026 -0
- package/dist/onboard-DnegOHMh.js +4 -4
- package/dist/onboard-RYtDlYBw.js +9 -0
- package/dist/onboard-aTwlQs-4.js +9 -0
- package/dist/orchestrator-BSp2M5EU.js +189 -0
- package/dist/orchestrator-C7ko5tWa.js +6 -0
- package/dist/orchestrator-DfPkIx2Z.js +6 -0
- package/dist/orchestrator-DmnEvMaL.js +189 -0
- package/dist/orchestrator-NJQsmiBE.js +189 -0
- package/dist/orchestrator-RI3bpqqc.js +6 -0
- package/dist/pairing-6iM27aD8.js +196 -0
- package/dist/pairing-DU0_J28n.js +87 -0
- package/dist/pairing-DWllbSbO.js +4 -0
- package/dist/pairing-dGoiGepK.js +4 -0
- package/dist/pc-access-CgCsYrpt.js +8 -0
- package/dist/pc-access-Ly-uA8mn.js +8 -0
- package/dist/pc-access-NxBvTrRj.js +819 -0
- package/dist/pc-access-_iH2aorG.js +819 -0
- package/dist/pending-approval-CUXjysAo.js +22 -0
- package/dist/pending-approval-DIHvwwWS.js +22 -0
- package/dist/puppeteer-2o3QOwAy.js +2 -2
- package/dist/puppeteer-BYTMp3BI.js +2 -2
- package/dist/puppeteer-DQ45qwWk.js +2 -2
- package/dist/reminders-store-D79qdfN0.js +58 -0
- package/dist/reminders-store-Drjed_-h.js +58 -0
- package/dist/renderer-BVQrd0_g.js +225 -0
- package/dist/renderer-pqlDRKbH.js +225 -0
- package/dist/rules-BE4GV6cV.js +103 -0
- package/dist/rules-BooT_qFP.js +103 -0
- package/dist/run-main.js +1649 -1227
- package/dist/runner-D1rjuMTJ.js +810 -0
- package/dist/runner-DatMMYYE.js +1271 -0
- package/dist/sdk/index.js +2 -2
- package/dist/sdk/index.mjs +2 -2
- package/dist/security-BqNyT4ID.js +4 -0
- package/dist/security-C-5URby1.js +73 -0
- package/dist/security-_xve79aq.js +4 -0
- package/dist/security-tpgqPWWH.js +73 -0
- package/dist/server-0kgyELx4.js +1047 -0
- package/dist/server-BIuTobTC.js +4 -0
- package/dist/server-BRlCEjyT.js +1047 -0
- package/dist/server-CCI1hv45.js +2 -2
- package/dist/server-D4wVHiX9.js +4 -0
- package/dist/server-DU9POoWc.js +4 -0
- package/dist/server-Dh3JlBFB.js +1255 -0
- package/dist/session-store-BUiPz0Vv.js +5 -0
- package/dist/session-store-CujxByI6.js +113 -0
- package/dist/session-store-is4B6qmD.js +113 -0
- package/dist/session-store-qpJUg2M1.js +5 -0
- package/dist/sessions-tools-CB2qbwIk.js +5 -0
- package/dist/sessions-tools-CbUTFe4i.js +5 -0
- package/dist/sessions-tools-CeqD7iil.js +95 -0
- package/dist/sessions-tools-DHMaTZIs.js +95 -0
- package/dist/skill-loader-BaNLVmJy.js +7 -0
- package/dist/skill-loader-BkceKkIg.js +7 -0
- package/dist/skill-loader-DhgIwK4J.js +159 -0
- package/dist/skill-loader-HgpF6Vqs.js +159 -0
- package/dist/skill-runtime--LqxWrp5.js +102 -0
- package/dist/skill-runtime-C5l0Tgt-.js +5 -0
- package/dist/skill-runtime-CJN24QPW.js +102 -0
- package/dist/skill-runtime-DsXK_HYG.js +102 -0
- package/dist/skill-runtime-IVTiqrMR.js +5 -0
- package/dist/skill-runtime-w1ig_lcw.js +5 -0
- package/dist/src-BEVLgaF1.js +63 -0
- package/dist/src-Bgu_OxTQ.js +458 -0
- package/dist/src-Bq-oKt7Z.js +458 -0
- package/dist/src-BxPHKO5x.js +63 -0
- package/dist/src-DIc-L2IG.js +20 -0
- package/dist/src-DWCUhnD4.js +20 -0
- package/dist/src-cfRTjFef.js +63 -0
- package/dist/src-g_rNx5rh.js +458 -0
- package/dist/sub-agent-tools-BD9DF8_g.js +39 -0
- package/dist/sub-agent-tools-CHQoHz9c.js +39 -0
- package/dist/sub-agent-tools-V7b3T9_s.js +39 -0
- package/dist/theme-DcxwcUgZ.js +180 -0
- package/dist/theme-cx0fkgWC.js +8 -0
- package/dist/tool-policy-CNT-mF2Z.js +189 -0
- package/dist/tool-policy-DNvNRnve.js +189 -0
- package/dist/tts-elevenlabs-BRosZv-f.js +61 -0
- package/dist/tts-elevenlabs-BUOGKL-k.js +61 -0
- package/dist/update-check-BD4qH7Am.js +81 -0
- package/dist/update-check-C2Dz85wJ.js +81 -0
- package/dist/vision-BMmiIKy7.js +121 -0
- package/dist/vision-DRq-f-Dj.js +121 -0
- package/dist/vision-tools-CFZEpQKm.js +5 -0
- package/dist/vision-tools-CQnBI9aa.js +51 -0
- package/dist/vision-tools-DVuYc17I.js +51 -0
- package/dist/vision-tools-U3YC4L-g.js +5 -0
- package/dist/voice-transcription-B555DbWR.js +138 -0
- package/dist/voice-transcription-CgWq54hn.js +138 -0
- package/dist/website-watch-tools-Bk_TnwuE.js +5 -0
- package/dist/website-watch-tools-DFMrJU-R.js +139 -0
- package/dist/website-watch-tools-DraMPxdl.js +139 -0
- package/dist/website-watch-tools-Du3W5sN7.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-jS-bbMI5.js');
|
|
2
|
+
const chalk = require_chunk.__toESM(require("chalk"));
|
|
3
|
+
const fs_extra = require_chunk.__toESM(require("fs-extra"));
|
|
4
|
+
const path = require_chunk.__toESM(require("path"));
|
|
5
|
+
const os = require_chunk.__toESM(require("os"));
|
|
6
|
+
const crypto = require_chunk.__toESM(require("crypto"));
|
|
7
|
+
const http = require_chunk.__toESM(require("http"));
|
|
8
|
+
const https = require_chunk.__toESM(require("https"));
|
|
9
|
+
const events = require_chunk.__toESM(require("events"));
|
|
10
|
+
|
|
11
|
+
//#region extensions/matrix/src/connector.ts
|
|
12
|
+
const HC_DIR = path.default.join(os.default.homedir(), ".hyperclaw");
|
|
13
|
+
const CREDS_BASE = path.default.join(HC_DIR, "credentials", "matrix");
|
|
14
|
+
const STATE_BASE = path.default.join(HC_DIR, "matrix", "accounts");
|
|
15
|
+
const DEFAULT_CHUNK = 16e3;
|
|
16
|
+
function matrixReq(homeserver, token, method, apiPath, body, query = {}) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const url = new URL(homeserver);
|
|
19
|
+
const isHttps = url.protocol === "https:";
|
|
20
|
+
const mod = isHttps ? https.default : http.default;
|
|
21
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
22
|
+
const qs = new URLSearchParams({
|
|
23
|
+
access_token: token,
|
|
24
|
+
...query
|
|
25
|
+
}).toString();
|
|
26
|
+
const reqPath = `/_matrix/client/v3${apiPath}?${qs}`;
|
|
27
|
+
const req = mod.request({
|
|
28
|
+
hostname: url.hostname,
|
|
29
|
+
port: url.port || (isHttps ? 443 : 80),
|
|
30
|
+
path: reqPath,
|
|
31
|
+
method,
|
|
32
|
+
headers: {
|
|
33
|
+
Authorization: `Bearer ${token}`,
|
|
34
|
+
...payload ? {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
37
|
+
} : {}
|
|
38
|
+
}
|
|
39
|
+
}, (res) => {
|
|
40
|
+
let data = "";
|
|
41
|
+
res.on("data", (c) => data += c);
|
|
42
|
+
res.on("end", () => {
|
|
43
|
+
try {
|
|
44
|
+
const r = JSON.parse(data);
|
|
45
|
+
if (r.errcode) reject(new Error(r.error || r.errcode));
|
|
46
|
+
else resolve(r);
|
|
47
|
+
} catch {
|
|
48
|
+
reject(new Error("Matrix: invalid JSON response"));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
req.on("error", reject);
|
|
53
|
+
req.setTimeout(6e4, () => {
|
|
54
|
+
req.destroy();
|
|
55
|
+
reject(new Error("Matrix: request timeout"));
|
|
56
|
+
});
|
|
57
|
+
if (payload) req.write(payload);
|
|
58
|
+
req.end();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function matrixLogin(homeserver, userId, password, deviceName) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const url = new URL(homeserver);
|
|
64
|
+
const isHttps = url.protocol === "https:";
|
|
65
|
+
const mod = isHttps ? https.default : http.default;
|
|
66
|
+
const body = JSON.stringify({
|
|
67
|
+
type: "m.login.password",
|
|
68
|
+
identifier: {
|
|
69
|
+
type: "m.id.user",
|
|
70
|
+
user: userId.replace(/^@/, "").split(":")[0]
|
|
71
|
+
},
|
|
72
|
+
password,
|
|
73
|
+
...deviceName ? { initial_device_display_name: deviceName } : {}
|
|
74
|
+
});
|
|
75
|
+
const req = mod.request({
|
|
76
|
+
hostname: url.hostname,
|
|
77
|
+
port: url.port || (isHttps ? 443 : 80),
|
|
78
|
+
path: "/_matrix/client/v3/login",
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: {
|
|
81
|
+
"Content-Type": "application/json",
|
|
82
|
+
"Content-Length": Buffer.byteLength(body)
|
|
83
|
+
}
|
|
84
|
+
}, (res) => {
|
|
85
|
+
let data = "";
|
|
86
|
+
res.on("data", (c) => data += c);
|
|
87
|
+
res.on("end", () => {
|
|
88
|
+
try {
|
|
89
|
+
resolve(JSON.parse(data));
|
|
90
|
+
} catch {
|
|
91
|
+
reject(new Error("Matrix login: invalid response"));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
req.on("error", reject);
|
|
96
|
+
req.write(body);
|
|
97
|
+
req.end();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function chunkText(text, limit, mode) {
|
|
101
|
+
if (mode === "newline") {
|
|
102
|
+
const paragraphs = text.split(/\n\n+/);
|
|
103
|
+
const chunks$1 = [];
|
|
104
|
+
let current = "";
|
|
105
|
+
for (const p of paragraphs) if ((current + "\n\n" + p).length > limit && current) {
|
|
106
|
+
chunks$1.push(current.trim());
|
|
107
|
+
current = p;
|
|
108
|
+
} else current = current ? current + "\n\n" + p : p;
|
|
109
|
+
if (current) chunks$1.push(current.trim());
|
|
110
|
+
return chunks$1.filter(Boolean);
|
|
111
|
+
}
|
|
112
|
+
const chunks = [];
|
|
113
|
+
for (let i = 0; i < text.length; i += limit) chunks.push(text.slice(i, i + limit));
|
|
114
|
+
return chunks.length ? chunks : [""];
|
|
115
|
+
}
|
|
116
|
+
var MatrixAccount = class extends events.EventEmitter {
|
|
117
|
+
cfg;
|
|
118
|
+
accountId;
|
|
119
|
+
token = "";
|
|
120
|
+
userId = "";
|
|
121
|
+
running = false;
|
|
122
|
+
nextBatch = null;
|
|
123
|
+
/** room ID → true if it is a DM room (populated from m.direct account data) */
|
|
124
|
+
directRooms = /* @__PURE__ */ new Set();
|
|
125
|
+
/** resolved room alias → room ID cache */
|
|
126
|
+
aliasCache = /* @__PURE__ */ new Map();
|
|
127
|
+
constructor(accountId, cfg) {
|
|
128
|
+
super();
|
|
129
|
+
this.accountId = accountId;
|
|
130
|
+
this.cfg = {
|
|
131
|
+
groupPolicy: "allowlist",
|
|
132
|
+
groupAllowFrom: [],
|
|
133
|
+
groups: {},
|
|
134
|
+
rooms: {},
|
|
135
|
+
threadReplies: "inbound",
|
|
136
|
+
replyToMode: "off",
|
|
137
|
+
textChunkLimit: DEFAULT_CHUNK,
|
|
138
|
+
chunkMode: "length",
|
|
139
|
+
mediaMaxMb: 10,
|
|
140
|
+
autoJoin: "always",
|
|
141
|
+
autoJoinAllowlist: [],
|
|
142
|
+
encryption: false,
|
|
143
|
+
...cfg,
|
|
144
|
+
dm: {
|
|
145
|
+
policy: cfg.dm?.policy ?? "pairing",
|
|
146
|
+
allowFrom: cfg.dm?.allowFrom ?? []
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
async authenticate() {
|
|
151
|
+
const hs = this.cfg.homeserver;
|
|
152
|
+
const credsFile = path.default.join(CREDS_BASE, `${this.accountId}.json`);
|
|
153
|
+
if (this.cfg.accessToken) this.token = this.cfg.accessToken;
|
|
154
|
+
else if (this.cfg.password) {
|
|
155
|
+
let cached = false;
|
|
156
|
+
try {
|
|
157
|
+
const c = await fs_extra.default.readJson(credsFile);
|
|
158
|
+
if (c.homeserver === hs && c.userId === this.cfg.userId && c.accessToken) {
|
|
159
|
+
this.token = c.accessToken;
|
|
160
|
+
cached = true;
|
|
161
|
+
}
|
|
162
|
+
} catch {}
|
|
163
|
+
if (!cached) {
|
|
164
|
+
if (!this.cfg.userId) throw new Error(`Matrix [${this.accountId}]: userId required for password login`);
|
|
165
|
+
const res = await matrixLogin(hs, this.cfg.userId, this.cfg.password, this.cfg.deviceName);
|
|
166
|
+
if (!res.access_token) throw new Error(`Matrix [${this.accountId}]: login failed — ${JSON.stringify(res)}`);
|
|
167
|
+
this.token = res.access_token;
|
|
168
|
+
await fs_extra.default.ensureDir(path.default.dirname(credsFile));
|
|
169
|
+
await fs_extra.default.writeJson(credsFile, {
|
|
170
|
+
homeserver: hs,
|
|
171
|
+
userId: this.cfg.userId,
|
|
172
|
+
accessToken: this.token
|
|
173
|
+
}, { spaces: 2 });
|
|
174
|
+
}
|
|
175
|
+
} else throw new Error(`Matrix [${this.accountId}]: accessToken or (userId + password) required`);
|
|
176
|
+
const whoami = await matrixReq(hs, this.token, "GET", "/account/whoami");
|
|
177
|
+
this.userId = this.cfg.userId || whoami.user_id;
|
|
178
|
+
if (this.cfg.encryption) try {
|
|
179
|
+
const tokenHash = crypto.default.createHash("sha256").update(this.token).digest("hex").slice(0, 12);
|
|
180
|
+
const _cryptoDir = path.default.join(STATE_BASE, this.accountId, `${new URL(hs).hostname}__${this.userId.replace(/[^a-z0-9_.-]/gi, "_")}`, tokenHash, "crypto");
|
|
181
|
+
await import("@matrix-org/matrix-sdk-crypto-nodejs").catch(() => {
|
|
182
|
+
console.log(chalk.default.yellow(` ⚠ Matrix [${this.accountId}]: crypto module not available — E2EE disabled. Run: pnpm rebuild @matrix-org/matrix-sdk-crypto-nodejs`));
|
|
183
|
+
this.cfg.encryption = false;
|
|
184
|
+
});
|
|
185
|
+
} catch {
|
|
186
|
+
this.cfg.encryption = false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
stateFile() {
|
|
190
|
+
return path.default.join(STATE_BASE, this.accountId, "bot-storage.json");
|
|
191
|
+
}
|
|
192
|
+
async loadState() {
|
|
193
|
+
try {
|
|
194
|
+
const s = await fs_extra.default.readJson(this.stateFile());
|
|
195
|
+
this.nextBatch = s.nextBatch || null;
|
|
196
|
+
if (s.p) this.cfg.pendingPairings = s.p;
|
|
197
|
+
if (s.a) this.cfg.approvedPairings = s.a;
|
|
198
|
+
if (s.directRooms) this.directRooms = new Set(s.directRooms);
|
|
199
|
+
} catch {}
|
|
200
|
+
}
|
|
201
|
+
async saveState() {
|
|
202
|
+
await fs_extra.default.ensureDir(path.default.dirname(this.stateFile()));
|
|
203
|
+
await fs_extra.default.writeJson(this.stateFile(), {
|
|
204
|
+
nextBatch: this.nextBatch,
|
|
205
|
+
p: this.cfg.pendingPairings,
|
|
206
|
+
a: this.cfg.approvedPairings,
|
|
207
|
+
directRooms: [...this.directRooms]
|
|
208
|
+
}, { spaces: 2 });
|
|
209
|
+
}
|
|
210
|
+
async connect() {
|
|
211
|
+
await this.authenticate();
|
|
212
|
+
await this.loadState();
|
|
213
|
+
this.running = true;
|
|
214
|
+
console.log(chalk.default.green(` 🔷 Matrix [${this.accountId}]: ${this.userId} connected`));
|
|
215
|
+
this.emit("connected", {
|
|
216
|
+
userId: this.userId,
|
|
217
|
+
accountId: this.accountId
|
|
218
|
+
});
|
|
219
|
+
this.syncLoop();
|
|
220
|
+
}
|
|
221
|
+
disconnect() {
|
|
222
|
+
this.running = false;
|
|
223
|
+
}
|
|
224
|
+
async syncLoop() {
|
|
225
|
+
while (this.running) try {
|
|
226
|
+
const query = this.nextBatch ? {
|
|
227
|
+
since: this.nextBatch,
|
|
228
|
+
timeout: "30000"
|
|
229
|
+
} : { timeout: "0" };
|
|
230
|
+
const sync = await matrixReq(this.cfg.homeserver, this.token, "GET", "/sync", void 0, query);
|
|
231
|
+
this.nextBatch = sync.next_batch;
|
|
232
|
+
const directEvent = (sync.account_data?.events || []).find((e) => e.type === "m.direct");
|
|
233
|
+
if (directEvent?.content) for (const rooms of Object.values(directEvent.content)) for (const r of rooms) this.directRooms.add(r);
|
|
234
|
+
await this.saveState();
|
|
235
|
+
for (const [roomId] of Object.entries(sync.rooms?.invite || {})) await this.handleInvite(roomId);
|
|
236
|
+
const joinedRooms = sync.rooms?.join || {};
|
|
237
|
+
for (const [roomId, rd] of Object.entries(joinedRooms)) for (const event of rd.timeline?.events || []) {
|
|
238
|
+
if (event.sender === this.userId) continue;
|
|
239
|
+
await this.handleEvent(roomId, event, rd);
|
|
240
|
+
}
|
|
241
|
+
} catch (e) {
|
|
242
|
+
if (this.running) {
|
|
243
|
+
console.log(chalk.default.yellow(` ⚠ Matrix [${this.accountId}]: ${e.message}`));
|
|
244
|
+
await new Promise((r) => setTimeout(r, 5e3));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async handleInvite(roomId) {
|
|
249
|
+
const autoJoin = this.cfg.autoJoin ?? "always";
|
|
250
|
+
if (autoJoin === "off") return;
|
|
251
|
+
if (autoJoin === "allowlist") {
|
|
252
|
+
const resolved = await this.resolveRoom(roomId);
|
|
253
|
+
const list = this.cfg.autoJoinAllowlist ?? [];
|
|
254
|
+
if (!list.includes(roomId) && !list.includes(resolved)) return;
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
await matrixReq(this.cfg.homeserver, this.token, "POST", `/rooms/${encodeURIComponent(roomId)}/join`, {});
|
|
258
|
+
} catch {}
|
|
259
|
+
}
|
|
260
|
+
async handleEvent(roomId, event, roomData) {
|
|
261
|
+
const isDM = this.directRooms.has(roomId);
|
|
262
|
+
const sender = event.sender || "";
|
|
263
|
+
if (!sender) return;
|
|
264
|
+
let text = "";
|
|
265
|
+
let eventThreadId;
|
|
266
|
+
switch (event.type) {
|
|
267
|
+
case "m.room.message": {
|
|
268
|
+
const msgtype = event.content?.msgtype || "";
|
|
269
|
+
const relatesTo = event.content?.["m.relates_to"];
|
|
270
|
+
eventThreadId = relatesTo?.rel_type === "m.thread" ? relatesTo.event_id : void 0;
|
|
271
|
+
switch (msgtype) {
|
|
272
|
+
case "m.text":
|
|
273
|
+
case "m.notice":
|
|
274
|
+
text = event.content?.body || "";
|
|
275
|
+
break;
|
|
276
|
+
case "m.image":
|
|
277
|
+
case "m.video":
|
|
278
|
+
case "m.audio":
|
|
279
|
+
case "m.file": {
|
|
280
|
+
const mxcUrl = event.content?.url || "";
|
|
281
|
+
const size = event.content?.info?.size || 0;
|
|
282
|
+
const maxBytes = (this.cfg.mediaMaxMb ?? 10) * 1024 * 1024;
|
|
283
|
+
if (size > maxBytes) return;
|
|
284
|
+
text = `[${msgtype.replace("m.", "")}:${mxcUrl}]`;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "m.location":
|
|
288
|
+
text = `[location:${event.content?.geo_uri || ""}]`;
|
|
289
|
+
break;
|
|
290
|
+
default: return;
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
case "org.matrix.msc3381.poll.start":
|
|
295
|
+
case "m.poll.start": {
|
|
296
|
+
const q = event.content?.["org.matrix.msc3381.poll.start"]?.question?.body || event.content?.["m.poll"]?.question?.text || "";
|
|
297
|
+
if (!q) return;
|
|
298
|
+
text = `[poll] ${q}`;
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case "m.reaction": {
|
|
302
|
+
const key = event.content?.["m.relates_to"]?.key || "";
|
|
303
|
+
const reacted = event.content?.["m.relates_to"]?.event_id || "";
|
|
304
|
+
if (!key) return;
|
|
305
|
+
text = `[reaction:${key}:${reacted}]`;
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
default: return;
|
|
309
|
+
}
|
|
310
|
+
if (!text) return;
|
|
311
|
+
if (isDM) {
|
|
312
|
+
const allowed = await this.checkDMPolicy(sender, text, roomId);
|
|
313
|
+
if (!allowed) return;
|
|
314
|
+
} else if (!this.checkGroupPolicy(roomId, sender, text, roomData)) return;
|
|
315
|
+
this.emit("message", {
|
|
316
|
+
channelId: "matrix",
|
|
317
|
+
accountId: this.accountId,
|
|
318
|
+
chatId: roomId,
|
|
319
|
+
from: sender,
|
|
320
|
+
text,
|
|
321
|
+
threadId: eventThreadId,
|
|
322
|
+
isDM,
|
|
323
|
+
timestamp: new Date(event.origin_server_ts || Date.now()).toISOString()
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
async checkDMPolicy(sender, text, roomId) {
|
|
327
|
+
const { policy, allowFrom } = this.cfg.dm;
|
|
328
|
+
switch (policy) {
|
|
329
|
+
case "disabled": return false;
|
|
330
|
+
case "open": return true;
|
|
331
|
+
case "allowlist":
|
|
332
|
+
if (allowFrom.includes(sender) || allowFrom.includes("*")) return true;
|
|
333
|
+
await this.sendMessage(roomId, "HyperClaw: Not on allowlist.");
|
|
334
|
+
return false;
|
|
335
|
+
case "pairing": {
|
|
336
|
+
if (this.cfg.approvedPairings.includes(sender)) return true;
|
|
337
|
+
const upper = text.trim().toUpperCase().match(/[A-Z0-9]{6}/)?.[0];
|
|
338
|
+
if (upper && this.cfg.pendingPairings[upper]) {
|
|
339
|
+
this.cfg.approvedPairings.push(sender);
|
|
340
|
+
delete this.cfg.pendingPairings[upper];
|
|
341
|
+
await this.saveState();
|
|
342
|
+
await this.sendMessage(roomId, "Paired!");
|
|
343
|
+
this.emit("pairing:approved", {
|
|
344
|
+
userId: sender,
|
|
345
|
+
channelId: "matrix",
|
|
346
|
+
accountId: this.accountId
|
|
347
|
+
});
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
351
|
+
this.cfg.pendingPairings[code] = sender;
|
|
352
|
+
await this.saveState();
|
|
353
|
+
await this.sendMessage(roomId, `Pairing code: \`${code}\`\nApprove: hyperclaw pairing approve matrix ${code}`);
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
checkGroupPolicy(roomId, sender, text, roomData) {
|
|
360
|
+
const policy = this.cfg.groupPolicy ?? "allowlist";
|
|
361
|
+
if (policy === "disabled") return false;
|
|
362
|
+
const allGroups = {
|
|
363
|
+
...this.cfg.rooms || {},
|
|
364
|
+
...this.cfg.groups || {}
|
|
365
|
+
};
|
|
366
|
+
const roomCfg = allGroups[roomId] || allGroups["*"] || {};
|
|
367
|
+
if (policy === "open" || roomCfg.allow === true) {
|
|
368
|
+
if (roomCfg.allowFrom?.length && !roomCfg.allowFrom.includes(sender)) return false;
|
|
369
|
+
const globalAllow$1 = this.cfg.groupAllowFrom ?? [];
|
|
370
|
+
if (globalAllow$1.length && !globalAllow$1.includes(sender)) return false;
|
|
371
|
+
if (roomCfg.requireMention !== false) {
|
|
372
|
+
const botMention = `${this.userId}`;
|
|
373
|
+
if (!text.includes(botMention) && !text.startsWith("!")) return false;
|
|
374
|
+
}
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
if (!allGroups[roomId] && !allGroups["*"]) return false;
|
|
378
|
+
if (roomCfg.allow === false) return false;
|
|
379
|
+
const globalAllow = this.cfg.groupAllowFrom ?? [];
|
|
380
|
+
if (globalAllow.length && !globalAllow.includes(sender)) return false;
|
|
381
|
+
if (roomCfg.allowFrom?.length && !roomCfg.allowFrom.includes(sender)) return false;
|
|
382
|
+
if (roomCfg.requireMention !== false) {
|
|
383
|
+
const botMention = this.userId;
|
|
384
|
+
if (!text.includes(botMention) && !text.startsWith("!")) return false;
|
|
385
|
+
}
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
async sendMessage(roomId, text, threadId) {
|
|
389
|
+
const limit = this.cfg.textChunkLimit ?? DEFAULT_CHUNK;
|
|
390
|
+
const mode = this.cfg.chunkMode ?? "length";
|
|
391
|
+
const chunks = chunkText(text, limit, mode);
|
|
392
|
+
const threadReplies = this.cfg.threadReplies ?? "inbound";
|
|
393
|
+
const useThread = threadId && (threadReplies === "inbound" || threadReplies === "always");
|
|
394
|
+
for (const chunk of chunks) {
|
|
395
|
+
const content = {
|
|
396
|
+
msgtype: "m.text",
|
|
397
|
+
body: chunk,
|
|
398
|
+
format: "org.matrix.custom.html",
|
|
399
|
+
formatted_body: chunk.replace(/\n/g, "<br/>")
|
|
400
|
+
};
|
|
401
|
+
if (useThread) content["m.relates_to"] = {
|
|
402
|
+
rel_type: "m.thread",
|
|
403
|
+
event_id: threadId,
|
|
404
|
+
is_falling_back: true
|
|
405
|
+
};
|
|
406
|
+
const txnId = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
407
|
+
await matrixReq(this.cfg.homeserver, this.token, "PUT", `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`, content);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async resolveRoom(idOrAlias) {
|
|
411
|
+
if (!idOrAlias.startsWith("#")) return idOrAlias;
|
|
412
|
+
if (this.aliasCache.has(idOrAlias)) return this.aliasCache.get(idOrAlias);
|
|
413
|
+
try {
|
|
414
|
+
const res = await matrixReq(this.cfg.homeserver, this.token, "GET", `/directory/room/${encodeURIComponent(idOrAlias)}`);
|
|
415
|
+
if (res.room_id) this.aliasCache.set(idOrAlias, res.room_id);
|
|
416
|
+
return res.room_id || idOrAlias;
|
|
417
|
+
} catch {
|
|
418
|
+
return idOrAlias;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
approvePairing(code) {
|
|
422
|
+
const upper = code.toUpperCase();
|
|
423
|
+
if (!this.cfg.pendingPairings[upper]) return false;
|
|
424
|
+
this.cfg.approvedPairings.push(this.cfg.pendingPairings[upper]);
|
|
425
|
+
delete this.cfg.pendingPairings[upper];
|
|
426
|
+
this.saveState();
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
isRunning() {
|
|
430
|
+
return this.running;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
var MatrixConnector = class extends events.EventEmitter {
|
|
434
|
+
config;
|
|
435
|
+
accounts = [];
|
|
436
|
+
constructor(config) {
|
|
437
|
+
super();
|
|
438
|
+
this.config = config;
|
|
439
|
+
}
|
|
440
|
+
async connect() {
|
|
441
|
+
const sharedState = {
|
|
442
|
+
approvedPairings: this.config.approvedPairings ?? [],
|
|
443
|
+
pendingPairings: this.config.pendingPairings ?? {}
|
|
444
|
+
};
|
|
445
|
+
const accountEntries = Object.entries(this.config.accounts || {});
|
|
446
|
+
if (accountEntries.length === 0) {
|
|
447
|
+
const acct = new MatrixAccount("default", {
|
|
448
|
+
...this.config,
|
|
449
|
+
...sharedState
|
|
450
|
+
});
|
|
451
|
+
this.wire(acct);
|
|
452
|
+
await acct.connect();
|
|
453
|
+
this.accounts.push(acct);
|
|
454
|
+
} else for (const [id, acctCfg] of accountEntries) {
|
|
455
|
+
const merged = {
|
|
456
|
+
...this.config,
|
|
457
|
+
...acctCfg,
|
|
458
|
+
homeserver: acctCfg.homeserver || this.config.homeserver,
|
|
459
|
+
...sharedState
|
|
460
|
+
};
|
|
461
|
+
if (!merged.homeserver) {
|
|
462
|
+
console.error(`[matrix] Account "${id}" has no homeserver — skipping`);
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const acct = new MatrixAccount(id, merged);
|
|
466
|
+
this.wire(acct);
|
|
467
|
+
try {
|
|
468
|
+
await acct.connect();
|
|
469
|
+
this.accounts.push(acct);
|
|
470
|
+
} catch (e) {
|
|
471
|
+
console.error(`[matrix] Account "${id}" failed: ${e.message}`);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
wire(acct) {
|
|
476
|
+
acct.on("message", (msg) => this.emit("message", msg));
|
|
477
|
+
acct.on("connected", (info) => this.emit("connected", info));
|
|
478
|
+
acct.on("pairing:approved", (info) => this.emit("pairing:approved", info));
|
|
479
|
+
}
|
|
480
|
+
async sendMessage(roomId, text, threadId) {
|
|
481
|
+
const acct = this.accounts[0];
|
|
482
|
+
if (!acct) throw new Error("Matrix: no connected account");
|
|
483
|
+
await acct.sendMessage(roomId, text, threadId);
|
|
484
|
+
}
|
|
485
|
+
disconnect() {
|
|
486
|
+
for (const a of this.accounts) a.disconnect();
|
|
487
|
+
this.accounts = [];
|
|
488
|
+
}
|
|
489
|
+
approvePairing(code) {
|
|
490
|
+
return this.accounts.some((a) => a.approvePairing(code));
|
|
491
|
+
}
|
|
492
|
+
isRunning() {
|
|
493
|
+
return this.accounts.some((a) => a.isRunning());
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
//#endregion
|
|
498
|
+
exports.MatrixConnector = MatrixConnector;
|