hyperclaw 4.0.2 → 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.
Files changed (134) hide show
  1. package/README.md +54 -3
  2. package/dist/a2ui-protocol-CfBI44-Q.js +75 -0
  3. package/dist/agents-routing-ChHiZp36.js +327 -0
  4. package/dist/agents-routing-ChqZ6l2S.js +4 -0
  5. package/dist/api-keys-guide-BCcOl0Q7.js +149 -0
  6. package/dist/audit-BaIiyWFu.js +441 -0
  7. package/dist/bounty-tools-DWudyZie.js +211 -0
  8. package/dist/browser-tools-BsTeGMnX.js +5 -0
  9. package/dist/browser-tools-D8_rLe2p.js +179 -0
  10. package/dist/claw-tasks-CgTsiNE8.js +80 -0
  11. package/dist/connector-5N0-X_xs.js +194 -0
  12. package/dist/connector-B3v0qcXg.js +425 -0
  13. package/dist/connector-B8R3iBY1.js +280 -0
  14. package/dist/connector-BAM-08NN.js +189 -0
  15. package/dist/connector-BC8FIVu4.js +181 -0
  16. package/dist/connector-BDmwwaVc.js +213 -0
  17. package/dist/connector-BGjbBy69.js +225 -0
  18. package/dist/connector-BO2SRzfG.js +218 -0
  19. package/dist/connector-BfXky0L3.js +167 -0
  20. package/dist/connector-BiiSJpx3.js +192 -0
  21. package/dist/connector-BnDmIhIu.js +85 -0
  22. package/dist/connector-C1HSoUyk.js +189 -0
  23. package/dist/connector-CKQHZOXg.js +568 -0
  24. package/dist/connector-CRl-iidy.js +239 -0
  25. package/dist/connector-Ci9glMD-.js +340 -0
  26. package/dist/connector-CjtZIEDj.js +181 -0
  27. package/dist/connector-Ck6JtOsX.js +531 -0
  28. package/dist/connector-D8Kelee0.js +286 -0
  29. package/dist/connector-DAnRJ0oP.js +162 -0
  30. package/dist/connector-DXTp5PE8.js +508 -0
  31. package/dist/connector-Dih6dUPP.js +173 -0
  32. package/dist/connector-DqTH_tPX.js +182 -0
  33. package/dist/connector-DrnEiiyP.js +419 -0
  34. package/dist/connector-DtR5GGTX.js +167 -0
  35. package/dist/connector-Tky_qS_K.js +350 -0
  36. package/dist/connector-ZSc3oTTy.js +305 -0
  37. package/dist/connector-sW5yhU1m.js +498 -0
  38. package/dist/connector-u3ICd3Ic.js +552 -0
  39. package/dist/cost-tracker-DD9wtWsr.js +103 -0
  40. package/dist/credentials-store-C6ir0Dae.js +4 -0
  41. package/dist/credentials-store-H13LqOwJ.js +77 -0
  42. package/dist/cron-tasks-Bli7Kzd2.js +82 -0
  43. package/dist/daemon-Bg4GtCmc.js +318 -0
  44. package/dist/daemon-DhmwY8k4.js +5 -0
  45. package/dist/delivery-BmIYy9VQ.js +4 -0
  46. package/dist/delivery-pWUPBp1F.js +95 -0
  47. package/dist/destructive-gate-D6vWOdEl.js +101 -0
  48. package/dist/developer-keys-CPWT7Q6S.js +8 -0
  49. package/dist/developer-keys-DrrcUqFa.js +127 -0
  50. package/dist/doctor-BvCe8BBk.js +230 -0
  51. package/dist/doctor-CxyPLYsJ.js +6 -0
  52. package/dist/engine-CEDSqXfw.js +256 -0
  53. package/dist/engine-Da4JMNpI.js +7 -0
  54. package/dist/env-resolve-CiXbWYwe.js +10 -0
  55. package/dist/env-resolve-CmGWhWXJ.js +115 -0
  56. package/dist/extraction-tools-HOZstZ0y.js +91 -0
  57. package/dist/extraction-tools-m4lmAv7l.js +5 -0
  58. package/dist/form_data-Cz040rio.js +8657 -0
  59. package/dist/gmail-watch-setup-Du7DVV7S.js +40 -0
  60. package/dist/health-B-asI__D.js +6 -0
  61. package/dist/health-Ds2YlpTB.js +152 -0
  62. package/dist/heartbeat-engine-BYT5ayQH.js +83 -0
  63. package/dist/hub-D0XwdjM-.js +515 -0
  64. package/dist/hub-LiD5Iztb.js +6 -0
  65. package/dist/hyperclawbot-zvczQgKx.js +505 -0
  66. package/dist/inference-BKVkBREb.js +6 -0
  67. package/dist/inference-DCXH4Q3x.js +922 -0
  68. package/dist/knowledge-graph-iBG76fvm.js +131 -0
  69. package/dist/loader-CC45xGpC.js +4 -0
  70. package/dist/loader-CnEdOyjT.js +400 -0
  71. package/dist/logger-ybOp7VOC.js +83 -0
  72. package/dist/manager-03ipO9R0.js +105 -0
  73. package/dist/manager-BpDfbDjg.js +117 -0
  74. package/dist/manager-Bxl0sqlh.js +4 -0
  75. package/dist/manager-CrVDn6eN.js +6 -0
  76. package/dist/manager-FCgF1plu.js +218 -0
  77. package/dist/manager-rgCsaWT1.js +40 -0
  78. package/dist/mcp-CfoSU4Uz.js +139 -0
  79. package/dist/mcp-loader-DkRBsLpk.js +94 -0
  80. package/dist/memory-BlHL7JCO.js +4 -0
  81. package/dist/memory-DsS_eFvJ.js +270 -0
  82. package/dist/memory-auto-BkvtSFUw.js +5 -0
  83. package/dist/memory-auto-Bnz_-1wP.js +306 -0
  84. package/dist/memory-integration-cSYkZyEo.js +91 -0
  85. package/dist/moltbook-BtLDZTfM.js +81 -0
  86. package/dist/node-Dw2Gi-cP.js +222 -0
  87. package/dist/nodes-registry-B8dmrlLv.js +52 -0
  88. package/dist/oauth-flow-DQPvMHRH.js +150 -0
  89. package/dist/oauth-provider-Uo4Nib_c.js +110 -0
  90. package/dist/observability-BV-Yx0V9.js +89 -0
  91. package/dist/onboard-0WoDxbv_.js +10 -0
  92. package/dist/onboard-BXNXCQp4.js +4070 -0
  93. package/dist/orchestrator-DmnEvMaL.js +189 -0
  94. package/dist/orchestrator-RI3bpqqc.js +6 -0
  95. package/dist/pairing-6iM27aD8.js +196 -0
  96. package/dist/pairing-dGoiGepK.js +4 -0
  97. package/dist/pc-access-CgCsYrpt.js +8 -0
  98. package/dist/pc-access-_iH2aorG.js +819 -0
  99. package/dist/pending-approval-CUXjysAo.js +22 -0
  100. package/dist/reminders-store-Drjed_-h.js +58 -0
  101. package/dist/renderer-BVQrd0_g.js +225 -0
  102. package/dist/rules-BE4GV6cV.js +103 -0
  103. package/dist/run-main.js +1607 -443
  104. package/dist/runner-DatMMYYE.js +1271 -0
  105. package/dist/sdk/index.js +2 -2
  106. package/dist/sdk/index.mjs +2 -2
  107. package/dist/security-BqNyT4ID.js +4 -0
  108. package/dist/security-tpgqPWWH.js +73 -0
  109. package/dist/server-D4wVHiX9.js +4 -0
  110. package/dist/server-Dh3JlBFB.js +1255 -0
  111. package/dist/session-store-BUiPz0Vv.js +5 -0
  112. package/dist/session-store-is4B6qmD.js +113 -0
  113. package/dist/sessions-tools-CbUTFe4i.js +5 -0
  114. package/dist/sessions-tools-CeqD7iil.js +95 -0
  115. package/dist/skill-loader-BaNLVmJy.js +7 -0
  116. package/dist/skill-loader-HgpF6Vqs.js +159 -0
  117. package/dist/skill-runtime-CJN24QPW.js +102 -0
  118. package/dist/skill-runtime-w1ig_lcw.js +5 -0
  119. package/dist/src-BxPHKO5x.js +63 -0
  120. package/dist/src-DIc-L2IG.js +20 -0
  121. package/dist/src-g_rNx5rh.js +458 -0
  122. package/dist/sub-agent-tools-CHQoHz9c.js +39 -0
  123. package/dist/theme-DcxwcUgZ.js +180 -0
  124. package/dist/theme-cx0fkgWC.js +8 -0
  125. package/dist/tool-policy-CNT-mF2Z.js +189 -0
  126. package/dist/tts-elevenlabs-BRosZv-f.js +61 -0
  127. package/dist/update-check-C2Dz85wJ.js +81 -0
  128. package/dist/vision-BMmiIKy7.js +121 -0
  129. package/dist/vision-tools-DVuYc17I.js +51 -0
  130. package/dist/vision-tools-U3YC4L-g.js +5 -0
  131. package/dist/voice-transcription-B555DbWR.js +138 -0
  132. package/dist/website-watch-tools-DFMrJU-R.js +139 -0
  133. package/dist/website-watch-tools-Du3W5sN7.js +5 -0
  134. package/package.json +1 -1
@@ -0,0 +1,189 @@
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 https = require_chunk.__toESM(require("https"));
7
+ const events = require_chunk.__toESM(require("events"));
8
+
9
+ //#region extensions/msteams/src/connector.ts
10
+ const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "msteams-state.json");
11
+ let cachedToken = null;
12
+ async function getBotToken(appId, appPassword) {
13
+ if (cachedToken && cachedToken.expiresAt > Date.now() + 6e4) return cachedToken.token;
14
+ return new Promise((resolve, reject) => {
15
+ const body = `grant_type=client_credentials&client_id=${encodeURIComponent(appId)}&client_secret=${encodeURIComponent(appPassword)}&scope=https%3A%2F%2Fapi.botframework.com%2F.default`;
16
+ const req = https.default.request({
17
+ hostname: "login.microsoftonline.com",
18
+ port: 443,
19
+ path: "/botframework.com/oauth2/v2.0/token",
20
+ method: "POST",
21
+ headers: {
22
+ "Content-Type": "application/x-www-form-urlencoded",
23
+ "Content-Length": Buffer.byteLength(body)
24
+ }
25
+ }, (res) => {
26
+ let data = "";
27
+ res.on("data", (c) => data += c);
28
+ res.on("end", () => {
29
+ try {
30
+ const r = JSON.parse(data);
31
+ cachedToken = {
32
+ token: r.access_token,
33
+ expiresAt: Date.now() + r.expires_in * 1e3
34
+ };
35
+ resolve(r.access_token);
36
+ } catch {
37
+ reject(new Error("Token parse error"));
38
+ }
39
+ });
40
+ });
41
+ req.on("error", reject);
42
+ req.write(body);
43
+ req.end();
44
+ });
45
+ }
46
+ async function sendActivity(token, serviceUrl, conversationId, activity) {
47
+ const url = new URL(`${serviceUrl}v3/conversations/${conversationId}/activities`);
48
+ return new Promise((resolve, reject) => {
49
+ const payload = JSON.stringify(activity);
50
+ const req = https.default.request({
51
+ hostname: url.hostname,
52
+ port: 443,
53
+ path: url.pathname,
54
+ method: "POST",
55
+ headers: {
56
+ "Authorization": `Bearer ${token}`,
57
+ "Content-Type": "application/json",
58
+ "Content-Length": Buffer.byteLength(payload)
59
+ }
60
+ }, (res) => {
61
+ res.on("data", () => {});
62
+ res.on("end", resolve);
63
+ });
64
+ req.on("error", reject);
65
+ req.write(payload);
66
+ req.end();
67
+ });
68
+ }
69
+ var MSTeamsConnector = class extends events.EventEmitter {
70
+ config;
71
+ running = false;
72
+ constructor(config) {
73
+ super();
74
+ this.config = {
75
+ dmPolicy: "allowlist",
76
+ allowFrom: [],
77
+ approvedPairings: [],
78
+ pendingPairings: {},
79
+ ...config
80
+ };
81
+ }
82
+ async connect() {
83
+ await getBotToken(this.config.appId, this.config.appPassword);
84
+ await this.loadState();
85
+ this.running = true;
86
+ console.log(chalk.default.green(` 🦅 MS Teams: Bot ${this.config.appId} connected`));
87
+ this.emit("connected", { appId: this.config.appId });
88
+ }
89
+ disconnect() {
90
+ this.running = false;
91
+ }
92
+ async handleWebhook(body) {
93
+ let activity;
94
+ try {
95
+ activity = JSON.parse(body);
96
+ } catch {
97
+ return;
98
+ }
99
+ if (activity.type !== "message") return;
100
+ const from = activity.from?.aadObjectId || activity.from?.id;
101
+ const text = activity.text?.replace(/<at>[^<]+<\/at>/g, "").trim();
102
+ const serviceUrl = activity.serviceUrl;
103
+ const conversationId = activity.conversation?.id;
104
+ if (!from || !text || !serviceUrl || !conversationId) return;
105
+ const allowed = await this.checkDMPolicy(from, text, serviceUrl, conversationId, activity);
106
+ if (!allowed) return;
107
+ this.emit("message", {
108
+ id: activity.id,
109
+ channelId: "msteams",
110
+ from,
111
+ chatId: conversationId,
112
+ fromName: activity.from?.name,
113
+ text,
114
+ timestamp: activity.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
115
+ isDM: activity.conversation?.conversationType === "personal",
116
+ serviceUrl,
117
+ activity
118
+ });
119
+ }
120
+ async checkDMPolicy(from, text, serviceUrl, convId, activity) {
121
+ if (this.config.dmPolicy === "none") return false;
122
+ if (this.config.dmPolicy === "open") return true;
123
+ if (this.config.dmPolicy === "allowlist") {
124
+ if (this.config.allowFrom.includes(from)) return true;
125
+ await this.reply(serviceUrl, convId, activity, "🦅 HyperClaw: Not on allowlist.");
126
+ return false;
127
+ }
128
+ if (this.config.dmPolicy === "pairing") {
129
+ if (this.config.approvedPairings.includes(from)) return true;
130
+ const upper = text.trim().toUpperCase();
131
+ if (this.config.pendingPairings[upper]) {
132
+ this.config.approvedPairings.push(from);
133
+ delete this.config.pendingPairings[upper];
134
+ await this.saveState();
135
+ await this.reply(serviceUrl, convId, activity, "🦅 Paired!");
136
+ this.emit("pairing:approved", {
137
+ userId: from,
138
+ channelId: "msteams"
139
+ });
140
+ return true;
141
+ }
142
+ const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
143
+ this.config.pendingPairings[code] = from;
144
+ await this.saveState();
145
+ await this.reply(serviceUrl, convId, activity, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve msteams ${code}`);
146
+ return false;
147
+ }
148
+ return false;
149
+ }
150
+ async reply(serviceUrl, conversationId, originalActivity, text) {
151
+ const token = await getBotToken(this.config.appId, this.config.appPassword);
152
+ await sendActivity(token, serviceUrl, conversationId, {
153
+ type: "message",
154
+ text: text.slice(0, 4e3),
155
+ replyToId: originalActivity.id,
156
+ from: { id: this.config.appId },
157
+ conversation: originalActivity.conversation,
158
+ recipient: originalActivity.from
159
+ });
160
+ }
161
+ approvePairing(code) {
162
+ const upper = code.toUpperCase();
163
+ if (!this.config.pendingPairings[upper]) return false;
164
+ this.config.approvedPairings.push(this.config.pendingPairings[upper]);
165
+ delete this.config.pendingPairings[upper];
166
+ this.saveState();
167
+ return true;
168
+ }
169
+ async loadState() {
170
+ try {
171
+ const s = await fs_extra.default.readJson(STATE_FILE);
172
+ if (s.p) this.config.pendingPairings = s.p;
173
+ if (s.a) this.config.approvedPairings = s.a;
174
+ } catch {}
175
+ }
176
+ async saveState() {
177
+ await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
178
+ await fs_extra.default.writeJson(STATE_FILE, {
179
+ p: this.config.pendingPairings,
180
+ a: this.config.approvedPairings
181
+ }, { spaces: 2 });
182
+ }
183
+ isRunning() {
184
+ return this.running;
185
+ }
186
+ };
187
+
188
+ //#endregion
189
+ exports.MSTeamsConnector = MSTeamsConnector;
@@ -0,0 +1,181 @@
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 http = require_chunk.__toESM(require("http"));
7
+ const ws = require_chunk.__toESM(require("ws"));
8
+ const https = require_chunk.__toESM(require("https"));
9
+ const events = require_chunk.__toESM(require("events"));
10
+
11
+ //#region extensions/bluebubbles/src/connector.ts
12
+ const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "bluebubbles-state.json");
13
+ function bbReq(serverUrl, password, method, endpoint, body) {
14
+ return new Promise((resolve, reject) => {
15
+ const url = new URL(`${serverUrl}/api/v1${endpoint}`);
16
+ url.searchParams.set("password", password);
17
+ const isHttps = url.protocol === "https:";
18
+ const mod = isHttps ? https.default : http.default;
19
+ const payload = body ? JSON.stringify(body) : null;
20
+ const req = mod.request({
21
+ hostname: url.hostname,
22
+ port: url.port || (isHttps ? 443 : 1234),
23
+ path: url.pathname + url.search,
24
+ method,
25
+ headers: payload ? {
26
+ "Content-Type": "application/json",
27
+ "Content-Length": Buffer.byteLength(payload)
28
+ } : {},
29
+ rejectUnauthorized: false
30
+ }, (res) => {
31
+ let data = "";
32
+ res.on("data", (c) => data += c);
33
+ res.on("end", () => {
34
+ try {
35
+ const r = JSON.parse(data);
36
+ if (r.status !== 200) reject(new Error(r.error?.message || "BB error"));
37
+ else resolve(r.data);
38
+ } catch {
39
+ reject(new Error("Invalid JSON"));
40
+ }
41
+ });
42
+ });
43
+ req.on("error", reject);
44
+ if (payload) req.write(payload);
45
+ req.end();
46
+ });
47
+ }
48
+ var BlueBubblesConnector = class extends events.EventEmitter {
49
+ config;
50
+ running = false;
51
+ ws = null;
52
+ lastMessageTs = 0;
53
+ constructor(config) {
54
+ super();
55
+ this.config = {
56
+ dmPolicy: "allowlist",
57
+ allowFrom: [],
58
+ approvedPairings: [],
59
+ pendingPairings: {},
60
+ ...config
61
+ };
62
+ }
63
+ async connect() {
64
+ const info = await bbReq(this.config.serverUrl, this.config.password, "GET", "/server/info");
65
+ await this.loadState();
66
+ this.running = true;
67
+ console.log(chalk.default.green(` 🦅 BlueBubbles (iMessage): ${info?.os_version || "macOS"} server connected`));
68
+ this.emit("connected", { serverInfo: info });
69
+ this.connectWebSocket();
70
+ }
71
+ connectWebSocket() {
72
+ const url = new URL(this.config.serverUrl);
73
+ const wsUrl = `${url.protocol === "https:" ? "wss" : "ws"}://${url.hostname}:${url.port || 1234}`;
74
+ this.ws = new ws.WebSocket(`${wsUrl}?password=${encodeURIComponent(this.config.password)}`, { rejectUnauthorized: false });
75
+ this.ws.on("message", async (data) => {
76
+ try {
77
+ const event = JSON.parse(data.toString());
78
+ if (event.type === "new-message" || event.type === "updated-message") await this.handleMessage(event.data);
79
+ } catch {}
80
+ });
81
+ this.ws.on("close", () => {
82
+ if (this.running) setTimeout(() => this.connectWebSocket(), 5e3);
83
+ });
84
+ this.ws.on("error", () => {});
85
+ setInterval(() => {
86
+ if (this.ws?.readyState === ws.WebSocket.OPEN) this.ws.send(JSON.stringify({ type: "ping" }));
87
+ }, 3e4);
88
+ }
89
+ async handleMessage(msg) {
90
+ if (!msg || msg.isFromMe) return;
91
+ const text = msg.text;
92
+ const from = msg.handle?.address;
93
+ if (!text || !from) return;
94
+ const ts = msg.dateCreated || 0;
95
+ if (ts <= this.lastMessageTs) return;
96
+ this.lastMessageTs = ts;
97
+ await this.saveState();
98
+ const allowed = await this.checkDMPolicy(from, text);
99
+ if (!allowed) return;
100
+ this.emit("message", {
101
+ id: msg.guid,
102
+ channelId: "bluebubbles",
103
+ from,
104
+ chatId: msg.chats?.[0]?.guid || from,
105
+ text,
106
+ timestamp: new Date(ts).toISOString(),
107
+ isDM: !msg.isGroup
108
+ });
109
+ }
110
+ async checkDMPolicy(from, text) {
111
+ if (this.config.dmPolicy === "none") return false;
112
+ if (this.config.dmPolicy === "open") return true;
113
+ if (this.config.dmPolicy === "allowlist") {
114
+ if (this.config.allowFrom.includes(from)) return true;
115
+ await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.");
116
+ return false;
117
+ }
118
+ if (this.config.dmPolicy === "pairing") {
119
+ if (this.config.approvedPairings.includes(from)) return true;
120
+ const upper = text.trim().toUpperCase();
121
+ if (this.config.pendingPairings[upper]) {
122
+ this.config.approvedPairings.push(from);
123
+ delete this.config.pendingPairings[upper];
124
+ await this.saveState();
125
+ await this.sendMessage(from, "🦅 Paired!");
126
+ this.emit("pairing:approved", {
127
+ userId: from,
128
+ channelId: "bluebubbles"
129
+ });
130
+ return true;
131
+ }
132
+ const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
133
+ this.config.pendingPairings[code] = from;
134
+ await this.saveState();
135
+ await this.sendMessage(from, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve bluebubbles ${code}`);
136
+ return false;
137
+ }
138
+ return false;
139
+ }
140
+ async sendMessage(address, text) {
141
+ await bbReq(this.config.serverUrl, this.config.password, "POST", "/message/text", {
142
+ chatGuid: `iMessage;-;${address}`,
143
+ message: text.slice(0, 65536),
144
+ method: "apple-script"
145
+ });
146
+ }
147
+ disconnect() {
148
+ this.running = false;
149
+ this.ws?.close();
150
+ }
151
+ approvePairing(code) {
152
+ const upper = code.toUpperCase();
153
+ if (!this.config.pendingPairings[upper]) return false;
154
+ this.config.approvedPairings.push(this.config.pendingPairings[upper]);
155
+ delete this.config.pendingPairings[upper];
156
+ this.saveState();
157
+ return true;
158
+ }
159
+ async loadState() {
160
+ try {
161
+ const s = await fs_extra.default.readJson(STATE_FILE);
162
+ this.lastMessageTs = s.lastTs || 0;
163
+ if (s.p) this.config.pendingPairings = s.p;
164
+ if (s.a) this.config.approvedPairings = s.a;
165
+ } catch {}
166
+ }
167
+ async saveState() {
168
+ await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
169
+ await fs_extra.default.writeJson(STATE_FILE, {
170
+ lastTs: this.lastMessageTs,
171
+ p: this.config.pendingPairings,
172
+ a: this.config.approvedPairings
173
+ }, { spaces: 2 });
174
+ }
175
+ isRunning() {
176
+ return this.running;
177
+ }
178
+ };
179
+
180
+ //#endregion
181
+ exports.BlueBubblesConnector = BlueBubblesConnector;
@@ -0,0 +1,213 @@
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 https = require_chunk.__toESM(require("https"));
7
+ const events = require_chunk.__toESM(require("events"));
8
+
9
+ //#region extensions/feishu/src/connector.ts
10
+ const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "feishu-state.json");
11
+ let feishuToken = null;
12
+ async function getAccessToken(appId, appSecret) {
13
+ if (feishuToken && feishuToken.expiresAt > Date.now() + 6e4) return feishuToken.token;
14
+ return new Promise((resolve, reject) => {
15
+ const payload = JSON.stringify({
16
+ app_id: appId,
17
+ app_secret: appSecret
18
+ });
19
+ const req = https.default.request({
20
+ hostname: "open.feishu.cn",
21
+ port: 443,
22
+ path: "/open-apis/auth/v3/tenant_access_token/internal",
23
+ method: "POST",
24
+ headers: {
25
+ "Content-Type": "application/json",
26
+ "Content-Length": Buffer.byteLength(payload)
27
+ }
28
+ }, (res) => {
29
+ let data = "";
30
+ res.on("data", (c) => data += c);
31
+ res.on("end", () => {
32
+ try {
33
+ const r = JSON.parse(data);
34
+ feishuToken = {
35
+ token: r.tenant_access_token,
36
+ expiresAt: Date.now() + r.expire * 1e3
37
+ };
38
+ resolve(r.tenant_access_token);
39
+ } catch {
40
+ reject(new Error("Token error"));
41
+ }
42
+ });
43
+ });
44
+ req.on("error", reject);
45
+ req.write(payload);
46
+ req.end();
47
+ });
48
+ }
49
+ function feishuReq(token, method, endpoint, body) {
50
+ return new Promise((resolve, reject) => {
51
+ const payload = body ? JSON.stringify(body) : null;
52
+ const req = https.default.request({
53
+ hostname: "open.feishu.cn",
54
+ port: 443,
55
+ path: `/open-apis${endpoint}`,
56
+ method,
57
+ headers: {
58
+ "Authorization": `Bearer ${token}`,
59
+ ...payload ? {
60
+ "Content-Type": "application/json",
61
+ "Content-Length": Buffer.byteLength(payload)
62
+ } : {}
63
+ }
64
+ }, (res) => {
65
+ let data = "";
66
+ res.on("data", (c) => data += c);
67
+ res.on("end", () => {
68
+ try {
69
+ const r = JSON.parse(data);
70
+ if (r.code !== 0) reject(new Error(r.msg));
71
+ else resolve(r);
72
+ } catch {
73
+ reject(new Error("Invalid JSON"));
74
+ }
75
+ });
76
+ });
77
+ req.on("error", reject);
78
+ if (payload) req.write(payload);
79
+ req.end();
80
+ });
81
+ }
82
+ var FeishuConnector = class extends events.EventEmitter {
83
+ config;
84
+ running = false;
85
+ botOpenId = "";
86
+ constructor(config) {
87
+ super();
88
+ this.config = {
89
+ dmPolicy: "allowlist",
90
+ allowFrom: [],
91
+ approvedPairings: [],
92
+ pendingPairings: {},
93
+ ...config
94
+ };
95
+ }
96
+ async connect() {
97
+ const token = await getAccessToken(this.config.appId, this.config.appSecret);
98
+ const botInfo = await feishuReq(token, "GET", "/bot/v3/info");
99
+ this.botOpenId = botInfo.bot?.open_id || "";
100
+ await this.loadState();
101
+ this.running = true;
102
+ console.log(chalk.default.green(` 🦅 Feishu: ${botInfo.bot?.app_name || this.config.appId} connected`));
103
+ this.emit("connected", { openId: this.botOpenId });
104
+ }
105
+ disconnect() {
106
+ this.running = false;
107
+ }
108
+ handleChallenge(body) {
109
+ try {
110
+ const r = JSON.parse(body);
111
+ if (r.type === "url_verification") return JSON.stringify({ challenge: r.challenge });
112
+ } catch {}
113
+ return null;
114
+ }
115
+ async handleWebhook(body) {
116
+ let payload;
117
+ try {
118
+ payload = JSON.parse(body);
119
+ } catch {
120
+ return;
121
+ }
122
+ const event = payload.event;
123
+ if (!event || payload.header?.event_type !== "im.message.receive_v1") return;
124
+ const senderId = event.sender?.sender_id?.open_id;
125
+ const msgType = event.message?.message_type;
126
+ if (msgType !== "text" || !senderId) return;
127
+ let text = "";
128
+ try {
129
+ text = JSON.parse(event.message.content).text;
130
+ } catch {
131
+ return;
132
+ }
133
+ if (!text) return;
134
+ const chatId = event.message?.chat_id || senderId;
135
+ const allowed = await this.checkDMPolicy(senderId, text, chatId);
136
+ if (!allowed) return;
137
+ this.emit("message", {
138
+ id: event.message?.message_id,
139
+ channelId: "feishu",
140
+ from: senderId,
141
+ chatId,
142
+ text,
143
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
144
+ isDM: true
145
+ });
146
+ }
147
+ async checkDMPolicy(from, text, chatId) {
148
+ if (this.config.dmPolicy === "none") return false;
149
+ if (this.config.dmPolicy === "open") return true;
150
+ if (this.config.dmPolicy === "allowlist") {
151
+ if (this.config.allowFrom.includes(from)) return true;
152
+ await this.sendMessage(chatId, "🦅 HyperClaw: Not on allowlist.");
153
+ return false;
154
+ }
155
+ if (this.config.dmPolicy === "pairing") {
156
+ if (this.config.approvedPairings.includes(from)) return true;
157
+ const upper = text.trim().toUpperCase();
158
+ if (this.config.pendingPairings[upper]) {
159
+ this.config.approvedPairings.push(from);
160
+ delete this.config.pendingPairings[upper];
161
+ await this.saveState();
162
+ await this.sendMessage(chatId, "🦅 Paired!");
163
+ this.emit("pairing:approved", {
164
+ userId: from,
165
+ channelId: "feishu"
166
+ });
167
+ return true;
168
+ }
169
+ const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
170
+ this.config.pendingPairings[code] = from;
171
+ await this.saveState();
172
+ await this.sendMessage(chatId, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve feishu ${code}`);
173
+ return false;
174
+ }
175
+ return false;
176
+ }
177
+ async sendMessage(chatId, text) {
178
+ const token = await getAccessToken(this.config.appId, this.config.appSecret);
179
+ await feishuReq(token, "POST", "/im/v1/messages?receive_id_type=chat_id", {
180
+ receive_id: chatId,
181
+ msg_type: "text",
182
+ content: JSON.stringify({ text: text.slice(0, 4e3) })
183
+ });
184
+ }
185
+ approvePairing(code) {
186
+ const upper = code.toUpperCase();
187
+ if (!this.config.pendingPairings[upper]) return false;
188
+ this.config.approvedPairings.push(this.config.pendingPairings[upper]);
189
+ delete this.config.pendingPairings[upper];
190
+ this.saveState();
191
+ return true;
192
+ }
193
+ async loadState() {
194
+ try {
195
+ const s = await fs_extra.default.readJson(STATE_FILE);
196
+ if (s.p) this.config.pendingPairings = s.p;
197
+ if (s.a) this.config.approvedPairings = s.a;
198
+ } catch {}
199
+ }
200
+ async saveState() {
201
+ await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
202
+ await fs_extra.default.writeJson(STATE_FILE, {
203
+ p: this.config.pendingPairings,
204
+ a: this.config.approvedPairings
205
+ }, { spaces: 2 });
206
+ }
207
+ isRunning() {
208
+ return this.running;
209
+ }
210
+ };
211
+
212
+ //#endregion
213
+ exports.FeishuConnector = FeishuConnector;