@beeos-ai/cli 1.0.4 → 1.0.5

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/dist/index.js CHANGED
@@ -1,8 +1,14 @@
1
- // src/index.ts
2
- import { Command } from "commander";
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
3
10
 
4
11
  // ../core/dist/platform-adapter.js
5
- var _adapter = null;
6
12
  function setPlatformAdapter(adapter) {
7
13
  _adapter = adapter;
8
14
  }
@@ -12,21 +18,30 @@ function getPlatformAdapter() {
12
18
  }
13
19
  return _adapter;
14
20
  }
21
+ var _adapter;
22
+ var init_platform_adapter = __esm({
23
+ "../core/dist/platform-adapter.js"() {
24
+ "use strict";
25
+ _adapter = null;
26
+ }
27
+ });
15
28
 
16
29
  // ../core/dist/progress.js
17
- var noopReporter = {
18
- onStatus() {
19
- },
20
- onComplete() {
30
+ var noopReporter;
31
+ var init_progress = __esm({
32
+ "../core/dist/progress.js"() {
33
+ "use strict";
34
+ noopReporter = {
35
+ onStatus() {
36
+ },
37
+ onComplete() {
38
+ }
39
+ };
21
40
  }
22
- };
41
+ });
23
42
 
24
43
  // ../core/dist/config.js
25
44
  import * as TOML from "smol-toml";
26
- var DEFAULT_API_URL = "https://api.beeos.ai";
27
- var DEFAULT_AGENT_GATEWAY_URL = "https://agent-gw.beeos.ai";
28
- var DEFAULT_DASHBOARD_URL = "https://beeos.ai";
29
- var DEFAULT_BRIDGE_URL = "wss://bridge-sg.beeos.ai";
30
45
  function defaultPlatformConfig() {
31
46
  return {
32
47
  api_url: DEFAULT_API_URL,
@@ -47,6 +62,30 @@ function defaultConfig() {
47
62
  device: defaultDeviceConfig()
48
63
  };
49
64
  }
65
+ function resolveAgentGatewayUrl(cfg) {
66
+ const envOverride = globalThis.process?.env?.BEEOS_AGENT_GATEWAY_URL;
67
+ if (envOverride && envOverride.trim() !== "")
68
+ return envOverride.trim();
69
+ const configured = cfg.platform.agent_gateway_url?.trim() ?? "";
70
+ const apiUrl = cfg.platform.api_url?.trim() ?? "";
71
+ const apiIsLocal = isLocalhostUrl(apiUrl);
72
+ const configuredIsDefault = configured === DEFAULT_AGENT_GATEWAY_URL || configured === "";
73
+ if (apiIsLocal && configuredIsDefault) {
74
+ return "http://localhost:8083";
75
+ }
76
+ return configured || DEFAULT_AGENT_GATEWAY_URL;
77
+ }
78
+ function isLocalhostUrl(url) {
79
+ if (!url)
80
+ return false;
81
+ try {
82
+ const u = new URL(url);
83
+ const h = u.hostname;
84
+ return h === "localhost" || h === "127.0.0.1" || h === "0.0.0.0" || h.endsWith(".local") || h.endsWith(".localhost");
85
+ } catch {
86
+ return false;
87
+ }
88
+ }
50
89
  function beeoHome() {
51
90
  const p = getPlatformAdapter();
52
91
  return p.joinPath(p.homeDir(), ".beeos");
@@ -81,9 +120,9 @@ function pidFile(agentFramework) {
81
120
  }
82
121
  async function loadOrCreateConfig() {
83
122
  const p = getPlatformAdapter();
84
- const path2 = configPath();
85
- if (await p.exists(path2)) {
86
- const raw = await p.readFile(path2);
123
+ const path9 = configPath();
124
+ if (await p.exists(path9)) {
125
+ const raw = await p.readFile(path9);
87
126
  const parsed = TOML.parse(raw);
88
127
  const cfg2 = mergeWithDefaults(parsed);
89
128
  return cfg2;
@@ -94,8 +133,8 @@ async function loadOrCreateConfig() {
94
133
  }
95
134
  async function saveConfig(cfg) {
96
135
  const p = getPlatformAdapter();
97
- const path2 = configPath();
98
- await p.mkdir(p.dirname(path2));
136
+ const path9 = configPath();
137
+ await p.mkdir(p.dirname(path9));
99
138
  const serializable = {
100
139
  platform: {
101
140
  api_url: cfg.platform.api_url,
@@ -109,7 +148,7 @@ async function saveConfig(cfg) {
109
148
  }
110
149
  };
111
150
  const raw = TOML.stringify(serializable);
112
- await p.writeFile(path2, raw);
151
+ await p.writeFile(path9, raw);
113
152
  }
114
153
  function mergeWithDefaults(parsed) {
115
154
  const platform = parsed.platform ?? {};
@@ -133,23 +172,23 @@ function mergeWithDefaults(parsed) {
133
172
  }
134
173
  async function loadBindingInfo() {
135
174
  const p = getPlatformAdapter();
136
- const path2 = bindingPath();
137
- if (!await p.exists(path2)) {
175
+ const path9 = bindingPath();
176
+ if (!await p.exists(path9)) {
138
177
  return migratePairedToBinding();
139
178
  }
140
- const raw = await p.readFile(path2);
179
+ const raw = await p.readFile(path9);
141
180
  return JSON.parse(raw);
142
181
  }
143
182
  async function saveBindingInfo(info) {
144
183
  const p = getPlatformAdapter();
145
- const path2 = bindingPath();
146
- await p.writeFile(path2, JSON.stringify(info, null, 2));
184
+ const path9 = bindingPath();
185
+ await p.writeFile(path9, JSON.stringify(info, null, 2));
147
186
  }
148
187
  async function removeBindingInfo() {
149
188
  const p = getPlatformAdapter();
150
- const path2 = bindingPath();
151
- if (await p.exists(path2)) {
152
- await p.rm(path2);
189
+ const path9 = bindingPath();
190
+ if (await p.exists(path9)) {
191
+ await p.rm(path9);
153
192
  }
154
193
  }
155
194
  async function migratePairedToBinding() {
@@ -174,26 +213,26 @@ async function migratePairedToBinding() {
174
213
  }
175
214
  async function loadOrCreateGatewayToken() {
176
215
  const p = getPlatformAdapter();
177
- const path2 = p.joinPath(beeoHome(), "gateway_token");
178
- if (await p.exists(path2)) {
179
- const token2 = (await p.readFile(path2)).trim();
216
+ const path9 = p.joinPath(beeoHome(), "gateway_token");
217
+ if (await p.exists(path9)) {
218
+ const token2 = (await p.readFile(path9)).trim();
180
219
  if (token2)
181
220
  return token2;
182
221
  }
183
222
  const token = crypto.randomUUID();
184
- await p.mkdir(p.dirname(path2));
185
- await p.writeFile(path2, token);
223
+ await p.mkdir(p.dirname(path9));
224
+ await p.writeFile(path9, token);
186
225
  if (p.platform() !== "win32") {
187
- await p.chmod(path2, 384);
226
+ await p.chmod(path9, 384);
188
227
  }
189
228
  return token;
190
229
  }
191
230
  async function readPid(agentFramework) {
192
231
  const p = getPlatformAdapter();
193
- const path2 = pidFile(agentFramework);
194
- if (!await p.exists(path2))
232
+ const path9 = pidFile(agentFramework);
233
+ if (!await p.exists(path9))
195
234
  return null;
196
- const raw = (await p.readFile(path2)).trim();
235
+ const raw = (await p.readFile(path9)).trim();
197
236
  const pid = parseInt(raw, 10);
198
237
  return isNaN(pid) ? null : pid;
199
238
  }
@@ -203,18 +242,28 @@ async function writePid(agentFramework, pid) {
203
242
  }
204
243
  async function removePid(agentFramework) {
205
244
  const p = getPlatformAdapter();
206
- const path2 = pidFile(agentFramework);
207
- if (await p.exists(path2)) {
208
- await p.rm(path2);
245
+ const path9 = pidFile(agentFramework);
246
+ if (await p.exists(path9)) {
247
+ await p.rm(path9);
209
248
  }
210
249
  }
250
+ var DEFAULT_API_URL, DEFAULT_AGENT_GATEWAY_URL, DEFAULT_DASHBOARD_URL, DEFAULT_BRIDGE_URL;
251
+ var init_config = __esm({
252
+ "../core/dist/config.js"() {
253
+ "use strict";
254
+ init_platform_adapter();
255
+ DEFAULT_API_URL = "https://api.beeos.ai";
256
+ DEFAULT_AGENT_GATEWAY_URL = "https://agent-gw.beeos.ai";
257
+ DEFAULT_DASHBOARD_URL = "https://beeos.ai";
258
+ DEFAULT_BRIDGE_URL = "wss://bridge-sg.beeos.ai";
259
+ }
260
+ });
211
261
 
212
262
  // ../core/dist/identity/keypair.js
213
263
  import * as ed from "@noble/ed25519";
214
264
  import { sha512 } from "@noble/hashes/sha512";
215
265
  import { sha256 } from "@noble/hashes/sha256";
216
266
  import { bytesToHex } from "@noble/hashes/utils";
217
- ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
218
267
  function toBase64(bytes) {
219
268
  let binary = "";
220
269
  for (let i = 0; i < bytes.length; i++)
@@ -239,9 +288,9 @@ function fingerprintFromB64(pubkeyB64) {
239
288
  }
240
289
  async function loadOrCreateIdentity() {
241
290
  const p = getPlatformAdapter();
242
- const path2 = keypairPath();
243
- if (await p.exists(path2)) {
244
- return loadFromFile(path2);
291
+ const path9 = keypairPath();
292
+ if (await p.exists(path9)) {
293
+ return loadFromFile(path9);
245
294
  }
246
295
  const id = generate();
247
296
  await save(id);
@@ -278,9 +327,9 @@ function fingerprintFilePath() {
278
327
  const p = getPlatformAdapter();
279
328
  return p.joinPath(beeoHome(), "identity", "fingerprint");
280
329
  }
281
- async function loadFromFile(path2) {
330
+ async function loadFromFile(path9) {
282
331
  const p = getPlatformAdapter();
283
- const raw = await p.readFile(path2);
332
+ const raw = await p.readFile(path9);
284
333
  const stored = JSON.parse(raw);
285
334
  const privateKey = fromBase64(stored.privateKey);
286
335
  if (privateKey.length !== 32) {
@@ -309,6 +358,14 @@ async function save(id) {
309
358
  const fpPath = fingerprintFilePath();
310
359
  await p.writeFile(fpPath, fingerprint(id));
311
360
  }
361
+ var init_keypair = __esm({
362
+ "../core/dist/identity/keypair.js"() {
363
+ "use strict";
364
+ init_platform_adapter();
365
+ init_config();
366
+ ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
367
+ }
368
+ });
312
369
 
313
370
  // ../core/dist/identity/qr.js
314
371
  import QRCode from "qrcode";
@@ -360,6 +417,11 @@ async function qrToTerminal(url) {
360
417
  lines.push(blankLine);
361
418
  return lines.join("\n");
362
419
  }
420
+ var init_qr = __esm({
421
+ "../core/dist/identity/qr.js"() {
422
+ "use strict";
423
+ }
424
+ });
363
425
 
364
426
  // ../core/dist/platform/client.js
365
427
  function buildBindUrl(dashboardBaseUrl, bindId) {
@@ -418,6 +480,13 @@ async function getBindStatus(apiUrl, bindId) {
418
480
  }
419
481
  return await resp.json();
420
482
  }
483
+ var init_client = __esm({
484
+ "../core/dist/platform/client.js"() {
485
+ "use strict";
486
+ init_platform_adapter();
487
+ init_keypair();
488
+ }
489
+ });
421
490
 
422
491
  // ../core/dist/platform/npm.js
423
492
  async function fetchNpmPackageInfo(packageName) {
@@ -441,6 +510,12 @@ async function downloadTarball(url) {
441
510
  const buf = await resp.arrayBuffer();
442
511
  return new Uint8Array(buf);
443
512
  }
513
+ var init_npm = __esm({
514
+ "../core/dist/platform/npm.js"() {
515
+ "use strict";
516
+ init_platform_adapter();
517
+ }
518
+ });
444
519
 
445
520
  // ../core/dist/process.js
446
521
  function killProcess(pid) {
@@ -464,6 +539,12 @@ function shouldOpenBrowser() {
464
539
  return true;
465
540
  return !!(p.env("DISPLAY") || p.env("WAYLAND_DISPLAY"));
466
541
  }
542
+ var init_process = __esm({
543
+ "../core/dist/process.js"() {
544
+ "use strict";
545
+ init_platform_adapter();
546
+ }
547
+ });
467
548
 
468
549
  // ../core/dist/bind.js
469
550
  async function presentBindUrl(bindUrl, forceHeadless, log = console.log) {
@@ -523,10 +604,17 @@ async function printQr(url, log) {
523
604
  function sleep(ms) {
524
605
  return new Promise((resolve) => setTimeout(resolve, ms));
525
606
  }
607
+ var init_bind = __esm({
608
+ "../core/dist/bind.js"() {
609
+ "use strict";
610
+ init_platform_adapter();
611
+ init_client();
612
+ init_process();
613
+ init_qr();
614
+ }
615
+ });
526
616
 
527
617
  // ../core/dist/device-setup.js
528
- var MIN_PYTHON_VERSION = [3, 11];
529
- var RECOMMENDED_AGENT_VERSION = "0.2.7";
530
618
  function agentBinCommandAndArgs(bin) {
531
619
  if (bin.type === "executable") {
532
620
  return { cmd: bin.path, args: [] };
@@ -771,117 +859,257 @@ async function runCmd(cmd, args) {
771
859
  ${stderr}`);
772
860
  }
773
861
  }
862
+ var MIN_PYTHON_VERSION, RECOMMENDED_AGENT_VERSION;
863
+ var init_device_setup = __esm({
864
+ "../core/dist/device-setup.js"() {
865
+ "use strict";
866
+ init_platform_adapter();
867
+ init_config();
868
+ MIN_PYTHON_VERSION = [3, 11];
869
+ RECOMMENDED_AGENT_VERSION = "0.2.7";
870
+ }
871
+ });
774
872
 
775
- // ../core/dist/runtime/device.js
776
- var deviceRuntime = {
777
- agentFramework() {
778
- return "device";
779
- },
780
- displayName() {
781
- return "Device Agent";
782
- },
783
- async detect() {
873
+ // ../core/dist/adb-setup.js
874
+ function managedAdbPath() {
875
+ const p = getPlatformAdapter();
876
+ const exe = p.platform() === "win32" ? "adb.exe" : "adb";
877
+ return p.joinPath(beeoHome(), "bin", "platform-tools", exe);
878
+ }
879
+ async function findAdb() {
880
+ const p = getPlatformAdapter();
881
+ const override = process.env?.BEEOS_ADB_BIN;
882
+ if (override && await p.exists(override))
883
+ return override;
884
+ const whichCmd = p.platform() === "win32" ? "where" : "which";
885
+ try {
886
+ const result = await p.exec(whichCmd, ["adb"]);
887
+ if (result.code === 0 && result.stdout.trim()) {
888
+ return result.stdout.trim().split("\n")[0].trim();
889
+ }
890
+ } catch {
891
+ }
892
+ const managed = managedAdbPath();
893
+ if (await p.exists(managed))
894
+ return managed;
895
+ return null;
896
+ }
897
+ async function installAdb(progress) {
898
+ const p = getPlatformAdapter();
899
+ const zipName = adbZipName();
900
+ if (!zipName) {
901
+ throw new Error(`No platform-tools archive for ${p.platform()}/${process.arch}. Install 'adb' manually and set $BEEOS_ADB_BIN, or add it to PATH.`);
902
+ }
903
+ const url = `${PLATFORM_TOOLS_BASE}/${zipName}`;
904
+ progress.onStatus(`Downloading Android platform-tools (${zipName})...`);
905
+ const resp = await p.fetch(url);
906
+ if (!resp.ok) {
907
+ throw new Error(`platform-tools download failed: ${resp.status} ${resp.statusText}`);
908
+ }
909
+ const data = new Uint8Array(await resp.arrayBuffer());
910
+ const binDir = p.joinPath(beeoHome(), "bin");
911
+ await p.mkdir(binDir);
912
+ const tmpZip = p.joinPath(binDir, `platform-tools.download.${Date.now()}.zip`);
913
+ await p.writeFileBytes(tmpZip, data);
914
+ try {
915
+ progress.onStatus("Extracting platform-tools...");
916
+ if (p.platform() === "win32") {
917
+ const exec2 = await p.exec("powershell", [
918
+ "-NoProfile",
919
+ "-Command",
920
+ `Expand-Archive -Force -Path "${tmpZip}" -DestinationPath "${binDir}"`
921
+ ]);
922
+ if (exec2.code !== 0) {
923
+ throw new Error(`Expand-Archive failed: ${exec2.stderr || exec2.stdout}`.trim());
924
+ }
925
+ } else {
926
+ const exec2 = await p.exec("unzip", ["-o", "-q", tmpZip, "-d", binDir]);
927
+ if (exec2.code !== 0) {
928
+ throw new Error(`unzip failed: ${exec2.stderr || exec2.stdout}`.trim());
929
+ }
930
+ }
931
+ const adb = managedAdbPath();
932
+ if (!await p.exists(adb)) {
933
+ throw new Error(`platform-tools zip did not contain ${p.basename(adb)}`);
934
+ }
935
+ if (p.platform() !== "win32") {
936
+ try {
937
+ await p.chmod(adb, 493);
938
+ } catch {
939
+ }
940
+ }
941
+ progress.onComplete(`adb installed: ${adb}`);
942
+ return adb;
943
+ } finally {
784
944
  try {
785
- const bin = await ensureDeviceAgent(noopReporter);
786
- const { cmd } = agentBinCommandAndArgs(bin);
787
- return { binary: cmd, version: "", home: beeoHome() };
945
+ await p.rm(tmpZip);
788
946
  } catch {
789
- return null;
790
947
  }
791
- },
792
- async ensure(_version, progress) {
793
- const bin = await ensureDeviceAgent(progress);
794
- const { cmd } = agentBinCommandAndArgs(bin);
795
- return { binary: cmd, version: "", home: beeoHome() };
796
- },
797
- async launch(_ctx) {
798
- throw new Error("DeviceRuntime.launch() should not be called directly \u2014 use spawnForDevice() instead");
799
- },
800
- async isHealthy() {
801
- return false;
802
- },
803
- async stop() {
804
- },
805
- async update(progress) {
806
- await upgradeDeviceAgent(progress);
807
- },
808
- // ── Device-specific extensions ──────────────────────────
809
- async ensureAgent(progress) {
810
- return ensureDeviceAgent(progress);
811
- },
812
- async upgradeAgent(progress) {
813
- return upgradeDeviceAgent(progress);
814
- },
815
- async listAdbDevices() {
816
- const p = getPlatformAdapter();
817
- const result = await p.exec("adb", ["devices"]);
818
- if (result.code !== 0) {
819
- throw new Error("adb not found \u2014 install Android platform-tools");
820
- }
821
- const devices = [];
822
- const lines = result.stdout.split("\n").slice(1);
823
- for (const line of lines) {
824
- const parts = line.trim().split(/\s+/);
825
- if (parts.length >= 2 && parts[1] === "device") {
826
- devices.push({ serial: parts[0] });
827
- }
828
- }
829
- return devices;
830
- },
831
- async detectSingleDevice() {
832
- const devices = await this.listAdbDevices();
833
- if (devices.length === 0) {
834
- throw new Error("No ADB devices found. Connect a device and try again.");
835
- }
836
- if (devices.length > 1) {
837
- const serials = devices.map((d) => d.serial).join(", ");
838
- throw new Error(`Multiple ADB devices found: ${serials}. Use --serial to specify one.`);
839
- }
840
- return devices[0].serial;
841
- },
842
- async spawnForDevice(serial, agentBin, bridgeUrl, httpEnabled, httpPort) {
843
- const p = getPlatformAdapter();
844
- const keyFile = this.deviceKeyPath(serial);
845
- await ensureDeviceKey(keyFile);
846
- if (!bridgeUrl) {
847
- throw new Error("bridge_url is not configured in ~/.beeos/config.toml.\nSet [platform] bridge_url (e.g. wss://bridge.beeos.ai).");
848
- }
849
- const { cmd, args: baseArgs } = agentBinCommandAndArgs(agentBin);
850
- const args = [
851
- ...baseArgs,
852
- "--key",
853
- keyFile,
854
- "--adb",
855
- serial,
856
- "--bridge",
857
- bridgeUrl
858
- ];
859
- if (httpEnabled) {
860
- args.push("--http", "--http-port", String(httpPort));
861
- }
862
- const logDir = p.joinPath(beeoHome(), "logs");
863
- await p.mkdir(logDir);
864
- const logFile = p.joinPath(logDir, `device-${serial}.log`);
865
- const result = await p.spawn(cmd, args, {
866
- detached: true,
867
- stdoutFile: logFile,
868
- stderrFile: logFile
869
- });
870
- return result.pid;
871
- },
872
- deviceKeyPath(serial) {
873
- const p = getPlatformAdapter();
874
- return p.joinPath(beeoHome(), "identity", `device-${serial}.key.json`);
875
- },
876
- async ensureKeyAndGetPubkey(serial) {
877
- const keyFile = this.deviceKeyPath(serial);
878
- await ensureDeviceKey(keyFile);
879
- return readPubkeyFromKeyFile(keyFile);
880
948
  }
881
- };
949
+ }
950
+ async function ensureAdb(progress, options = {}) {
951
+ const existing = await findAdb();
952
+ if (existing)
953
+ return existing;
954
+ if (options.autoInstall)
955
+ return installAdb(progress);
956
+ return null;
957
+ }
958
+ function adbZipName() {
959
+ const p = getPlatformAdapter();
960
+ switch (p.platform()) {
961
+ case "darwin":
962
+ return "platform-tools-latest-darwin.zip";
963
+ case "linux":
964
+ return "platform-tools-latest-linux.zip";
965
+ case "win32":
966
+ return "platform-tools-latest-windows.zip";
967
+ default:
968
+ return null;
969
+ }
970
+ }
971
+ var PLATFORM_TOOLS_BASE;
972
+ var init_adb_setup = __esm({
973
+ "../core/dist/adb-setup.js"() {
974
+ "use strict";
975
+ init_platform_adapter();
976
+ init_config();
977
+ PLATFORM_TOOLS_BASE = "https://dl.google.com/android/repository";
978
+ }
979
+ });
980
+
981
+ // ../core/dist/runtime/types.js
982
+ var init_types = __esm({
983
+ "../core/dist/runtime/types.js"() {
984
+ "use strict";
985
+ }
986
+ });
987
+
988
+ // ../core/dist/runtime/device.js
989
+ var deviceRuntime;
990
+ var init_device = __esm({
991
+ "../core/dist/runtime/device.js"() {
992
+ "use strict";
993
+ init_platform_adapter();
994
+ init_config();
995
+ init_keypair();
996
+ init_device_setup();
997
+ init_progress();
998
+ deviceRuntime = {
999
+ agentFramework() {
1000
+ return "device";
1001
+ },
1002
+ displayName() {
1003
+ return "Device Agent";
1004
+ },
1005
+ async detect() {
1006
+ try {
1007
+ const bin = await ensureDeviceAgent(noopReporter);
1008
+ const { cmd } = agentBinCommandAndArgs(bin);
1009
+ return { binary: cmd, version: "", home: beeoHome() };
1010
+ } catch {
1011
+ return null;
1012
+ }
1013
+ },
1014
+ async ensure(_version, progress) {
1015
+ const bin = await ensureDeviceAgent(progress);
1016
+ const { cmd } = agentBinCommandAndArgs(bin);
1017
+ return { binary: cmd, version: "", home: beeoHome() };
1018
+ },
1019
+ async launch(_ctx) {
1020
+ throw new Error("DeviceRuntime.launch() should not be called directly \u2014 use spawnForDevice() instead");
1021
+ },
1022
+ async isHealthy() {
1023
+ return false;
1024
+ },
1025
+ async stop() {
1026
+ },
1027
+ async update(progress) {
1028
+ await upgradeDeviceAgent(progress);
1029
+ },
1030
+ // ── Device-specific extensions ──────────────────────────
1031
+ async ensureAgent(progress) {
1032
+ return ensureDeviceAgent(progress);
1033
+ },
1034
+ async upgradeAgent(progress) {
1035
+ return upgradeDeviceAgent(progress);
1036
+ },
1037
+ async listAdbDevices() {
1038
+ const p = getPlatformAdapter();
1039
+ const result = await p.exec("adb", ["devices"]);
1040
+ if (result.code !== 0) {
1041
+ throw new Error("adb not found \u2014 install Android platform-tools");
1042
+ }
1043
+ const devices = [];
1044
+ const lines = result.stdout.split("\n").slice(1);
1045
+ for (const line of lines) {
1046
+ const parts = line.trim().split(/\s+/);
1047
+ if (parts.length >= 2 && parts[1] === "device") {
1048
+ devices.push({ serial: parts[0] });
1049
+ }
1050
+ }
1051
+ return devices;
1052
+ },
1053
+ async detectSingleDevice() {
1054
+ const devices = await this.listAdbDevices();
1055
+ if (devices.length === 0) {
1056
+ throw new Error("No ADB devices found. Connect a device and try again.");
1057
+ }
1058
+ if (devices.length > 1) {
1059
+ const serials = devices.map((d) => d.serial).join(", ");
1060
+ throw new Error(`Multiple ADB devices found: ${serials}. Use --serial to specify one.`);
1061
+ }
1062
+ return devices[0].serial;
1063
+ },
1064
+ async spawnForDevice(serial, agentBin, bridgeUrl, httpEnabled, httpPort, agentGatewayUrl) {
1065
+ const p = getPlatformAdapter();
1066
+ const keyFile = this.deviceKeyPath(serial);
1067
+ await ensureDeviceKey(keyFile);
1068
+ if (!bridgeUrl) {
1069
+ throw new Error("bridge_url is not configured in ~/.beeos/config.toml.\nSet [platform] bridge_url (e.g. wss://bridge.beeos.ai).");
1070
+ }
1071
+ const { cmd, args: baseArgs } = agentBinCommandAndArgs(agentBin);
1072
+ const args = [
1073
+ ...baseArgs,
1074
+ "--key",
1075
+ keyFile,
1076
+ "--adb",
1077
+ serial,
1078
+ "--bridge",
1079
+ bridgeUrl
1080
+ ];
1081
+ if (httpEnabled) {
1082
+ args.push("--http", "--http-port", String(httpPort));
1083
+ }
1084
+ const logDir = p.joinPath(beeoHome(), "logs");
1085
+ await p.mkdir(logDir);
1086
+ const logFile = p.joinPath(logDir, `device-${serial}.log`);
1087
+ const env = {};
1088
+ if (agentGatewayUrl) {
1089
+ env.AGENT_GATEWAY_URL = agentGatewayUrl;
1090
+ }
1091
+ const result = await p.spawn(cmd, args, {
1092
+ detached: true,
1093
+ stdoutFile: logFile,
1094
+ stderrFile: logFile,
1095
+ env: Object.keys(env).length > 0 ? env : void 0
1096
+ });
1097
+ return result.pid;
1098
+ },
1099
+ deviceKeyPath(serial) {
1100
+ const p = getPlatformAdapter();
1101
+ return p.joinPath(beeoHome(), "identity", `device-${serial}.key.json`);
1102
+ },
1103
+ async ensureKeyAndGetPubkey(serial) {
1104
+ const keyFile = this.deviceKeyPath(serial);
1105
+ await ensureDeviceKey(keyFile);
1106
+ return readPubkeyFromKeyFile(keyFile);
1107
+ }
1108
+ };
1109
+ }
1110
+ });
882
1111
 
883
1112
  // ../core/dist/agent/detector.js
884
- var GATEWAY_PORT = 18789;
885
1113
  async function isGatewayRunning() {
886
1114
  return getPlatformAdapter().tcpProbe("127.0.0.1", GATEWAY_PORT, 500);
887
1115
  }
@@ -950,6 +1178,15 @@ async function findAgent(driver) {
950
1178
  }
951
1179
  return { type: "notFound" };
952
1180
  }
1181
+ var GATEWAY_PORT;
1182
+ var init_detector = __esm({
1183
+ "../core/dist/agent/detector.js"() {
1184
+ "use strict";
1185
+ init_platform_adapter();
1186
+ init_config();
1187
+ GATEWAY_PORT = 18789;
1188
+ }
1189
+ });
953
1190
 
954
1191
  // ../core/dist/agent/downloader.js
955
1192
  async function downloadAgent(npmPackage, requestedVersion, agentFramework, progress) {
@@ -1059,11 +1296,18 @@ function semverGte(version, minVersion) {
1059
1296
  }
1060
1297
  return true;
1061
1298
  }
1299
+ var init_downloader = __esm({
1300
+ "../core/dist/agent/downloader.js"() {
1301
+ "use strict";
1302
+ init_platform_adapter();
1303
+ init_config();
1304
+ init_npm();
1305
+ }
1306
+ });
1062
1307
 
1063
1308
  // ../core/dist/agent/launcher.js
1064
1309
  import { sha256 as sha2562 } from "@noble/hashes/sha256";
1065
1310
  import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils";
1066
- var MAX_BACKUPS = 5;
1067
1311
  async function backupOpenclawConfig(agentHome2) {
1068
1312
  const p = getPlatformAdapter();
1069
1313
  const configPath2 = p.joinPath(agentHome2, "openclaw.json");
@@ -1127,12 +1371,12 @@ async function generateOpenclawConfig(ctx) {
1127
1371
  const env = { OPENCLAW_HOME: ctx.agentHome };
1128
1372
  const setupResult = await p.exec(ctx.agentBinary, ["setup", "--non-interactive", "--mode", "local"], { env });
1129
1373
  if (setupResult.code === 0) {
1130
- for (const [path2, value] of [
1374
+ for (const [path9, value] of [
1131
1375
  ["gateway.auth.token", ctx.gatewayToken],
1132
1376
  ["gateway.remote.token", ctx.gatewayToken],
1133
1377
  ["gateway.bind", "lan"]
1134
1378
  ]) {
1135
- await runConfigSet(ctx.agentBinary, ctx.agentHome, path2, value, false, false);
1379
+ await runConfigSet(ctx.agentBinary, ctx.agentHome, path9, value, false, false);
1136
1380
  }
1137
1381
  await configurePluginViaCli(ctx);
1138
1382
  } else {
@@ -1163,9 +1407,9 @@ async function generateConfigFallback(ctx) {
1163
1407
  await p.writeFile(configPath2, JSON.stringify(config, null, 2));
1164
1408
  await writePairedJson(ctx.agentHome, ctx.keyFile);
1165
1409
  }
1166
- async function runConfigSet(bin, home, path2, value, json, isSystemHome) {
1410
+ async function runConfigSet(bin, home, path9, value, json, isSystemHome) {
1167
1411
  const p = getPlatformAdapter();
1168
- const args = ["config", "set", path2, value];
1412
+ const args = ["config", "set", path9, value];
1169
1413
  if (json)
1170
1414
  args.push("--json");
1171
1415
  const env = {};
@@ -1173,7 +1417,7 @@ async function runConfigSet(bin, home, path2, value, json, isSystemHome) {
1173
1417
  env.OPENCLAW_HOME = home;
1174
1418
  const result = await p.exec(bin, args, { env });
1175
1419
  if (result.code !== 0) {
1176
- throw new Error(`\`openclaw config set ${path2}\` exited with ${result.code}`);
1420
+ throw new Error(`\`openclaw config set ${path9}\` exited with ${result.code}`);
1177
1421
  }
1178
1422
  }
1179
1423
  async function restartGatewayViaCli(ctx) {
@@ -1266,92 +1510,506 @@ async function writePairedJson(home, keyFile) {
1266
1510
  const pairedPath = p.joinPath(devicesDir, "paired.json");
1267
1511
  await p.writeFile(pairedPath, JSON.stringify(paired, null, 2));
1268
1512
  }
1513
+ var MAX_BACKUPS;
1514
+ var init_launcher = __esm({
1515
+ "../core/dist/agent/launcher.js"() {
1516
+ "use strict";
1517
+ init_platform_adapter();
1518
+ MAX_BACKUPS = 5;
1519
+ }
1520
+ });
1269
1521
 
1270
1522
  // ../core/dist/agent/registry.js
1271
- var openClawDriver = {
1272
- agentFramework() {
1273
- return "openclaw";
1274
- },
1275
- npmPackage() {
1276
- return "openclaw";
1277
- },
1278
- pluginPackage() {
1279
- return "beeos-claw";
1280
- },
1281
- async detectLocal() {
1282
- const p = getPlatformAdapter();
1283
- const cmd = p.platform() === "win32" ? "where" : "which";
1284
- let binary;
1523
+ var openClawDriver;
1524
+ var init_registry = __esm({
1525
+ "../core/dist/agent/registry.js"() {
1526
+ "use strict";
1527
+ init_platform_adapter();
1528
+ openClawDriver = {
1529
+ agentFramework() {
1530
+ return "openclaw";
1531
+ },
1532
+ npmPackage() {
1533
+ return "openclaw";
1534
+ },
1535
+ pluginPackage() {
1536
+ return "beeos-claw";
1537
+ },
1538
+ async detectLocal() {
1539
+ const p = getPlatformAdapter();
1540
+ const cmd = p.platform() === "win32" ? "where" : "which";
1541
+ let binary;
1542
+ try {
1543
+ const result = await p.exec(cmd, ["openclaw"]);
1544
+ if (result.code !== 0)
1545
+ return null;
1546
+ binary = result.stdout.trim().split("\n")[0].trim();
1547
+ } catch {
1548
+ return null;
1549
+ }
1550
+ let version = "";
1551
+ try {
1552
+ const verResult = await p.exec(binary, ["--version"]);
1553
+ version = verResult.stdout.trim();
1554
+ } catch {
1555
+ }
1556
+ const homeDir = p.joinPath(p.homeDir(), ".openclaw");
1557
+ return { binary, version, homeDir };
1558
+ }
1559
+ };
1560
+ }
1561
+ });
1562
+
1563
+ // ../core/dist/runtime/openclaw.js
1564
+ var GATEWAY_PORT2, openclawRuntime;
1565
+ var init_openclaw = __esm({
1566
+ "../core/dist/runtime/openclaw.js"() {
1567
+ "use strict";
1568
+ init_platform_adapter();
1569
+ init_config();
1570
+ init_process();
1571
+ init_detector();
1572
+ init_downloader();
1573
+ init_launcher();
1574
+ init_registry();
1575
+ GATEWAY_PORT2 = 18789;
1576
+ openclawRuntime = {
1577
+ agentFramework() {
1578
+ return "openclaw";
1579
+ },
1580
+ displayName() {
1581
+ return "OpenClaw";
1582
+ },
1583
+ async detect() {
1584
+ const location = await findAgent(openClawDriver);
1585
+ if (location.type === "notFound")
1586
+ return null;
1587
+ return {
1588
+ binary: location.binary,
1589
+ version: location.type === "system" ? location.version : "",
1590
+ home: location.home
1591
+ };
1592
+ },
1593
+ async ensure(version, progress) {
1594
+ const location = await findAgent(openClawDriver);
1595
+ if (location.type === "managed") {
1596
+ return { binary: location.binary, version: "", home: location.home };
1597
+ }
1598
+ if (location.type === "system") {
1599
+ return { binary: location.binary, version: location.version, home: location.home };
1600
+ }
1601
+ const dest = await downloadAgent(openClawDriver.npmPackage(), version ?? void 0, "openclaw", progress);
1602
+ const home = agentHome("openclaw");
1603
+ await getPlatformAdapter().mkdir(home);
1604
+ await downloadPlugin(openClawDriver.pluginPackage(), "openclaw", progress);
1605
+ return { binary: dest, version: "", home };
1606
+ },
1607
+ async launch(ctx) {
1608
+ return spawnOpenclaw(ctx);
1609
+ },
1610
+ async isHealthy() {
1611
+ return getPlatformAdapter().tcpProbe("127.0.0.1", GATEWAY_PORT2, 500);
1612
+ },
1613
+ async stop(agentFramework) {
1614
+ const pid = await readPid(agentFramework);
1615
+ if (pid != null) {
1616
+ killProcess(pid);
1617
+ await removePid(agentFramework);
1618
+ }
1619
+ },
1620
+ async update(progress) {
1621
+ await downloadAgent(openClawDriver.npmPackage(), void 0, "openclaw", progress);
1622
+ await downloadPlugin(openClawDriver.pluginPackage(), "openclaw", progress);
1623
+ }
1624
+ };
1625
+ }
1626
+ });
1627
+
1628
+ // ../core/dist/sidecars/cargo-dist.js
1629
+ import { createHash } from "crypto";
1630
+ function managedBinaryPath(cfg) {
1631
+ const p = getPlatformAdapter();
1632
+ const name = p.platform() === "win32" ? `${cfg.name}.exe` : cfg.name;
1633
+ return p.joinPath(beeoHome(), "bin", name);
1634
+ }
1635
+ async function findSidecarBinary(cfg) {
1636
+ const p = getPlatformAdapter();
1637
+ const override = process.env?.[cfg.binaryEnv];
1638
+ if (override && await p.exists(override))
1639
+ return override;
1640
+ const whichCmd = p.platform() === "win32" ? "where" : "which";
1641
+ try {
1642
+ const result = await p.exec(whichCmd, [cfg.name]);
1643
+ if (result.code === 0 && result.stdout.trim()) {
1644
+ return result.stdout.trim().split("\n")[0].trim();
1645
+ }
1646
+ } catch {
1647
+ }
1648
+ const managed = managedBinaryPath(cfg);
1649
+ if (await p.exists(managed))
1650
+ return managed;
1651
+ return null;
1652
+ }
1653
+ async function ensureSidecarInstalled(cfg, progress) {
1654
+ const existing = await findSidecarBinary(cfg);
1655
+ if (existing) {
1656
+ progress.onStatus(`${cfg.name} found: ${existing}`);
1657
+ return existing;
1658
+ }
1659
+ const target = cargoDistTarget();
1660
+ if (!target) {
1661
+ reportUnsupportedTarget(cfg, progress);
1662
+ return null;
1663
+ }
1664
+ return downloadManagedBinary(cfg, target, progress);
1665
+ }
1666
+ async function upgradeSidecar(cfg, progress) {
1667
+ const target = cargoDistTarget();
1668
+ if (!target) {
1669
+ reportUnsupportedTarget(cfg, progress);
1670
+ throw new Error(`No prebuilt ${cfg.name} archive available for ${platformKey()}. See the log above for fallback options.`);
1671
+ }
1672
+ const p = getPlatformAdapter();
1673
+ const managed = managedBinaryPath(cfg);
1674
+ if (await p.exists(managed)) {
1285
1675
  try {
1286
- const result = await p.exec(cmd, ["openclaw"]);
1287
- if (result.code !== 0)
1288
- return null;
1289
- binary = result.stdout.trim().split("\n")[0].trim();
1676
+ await p.rm(managed);
1290
1677
  } catch {
1678
+ }
1679
+ }
1680
+ const installed = await downloadManagedBinary(cfg, target, progress);
1681
+ if (!installed) {
1682
+ throw new Error(`${cfg.name} upgrade failed`);
1683
+ }
1684
+ return installed;
1685
+ }
1686
+ function cargoDistTarget() {
1687
+ const p = getPlatformAdapter();
1688
+ const plat = p.platform();
1689
+ const arch = process.arch;
1690
+ switch (plat) {
1691
+ case "darwin":
1692
+ if (arch === "arm64")
1693
+ return "aarch64-apple-darwin";
1694
+ if (arch === "x64")
1695
+ return "x86_64-apple-darwin";
1696
+ return null;
1697
+ case "linux":
1698
+ if (arch === "arm64")
1699
+ return "aarch64-unknown-linux-gnu";
1700
+ if (arch === "x64")
1701
+ return "x86_64-unknown-linux-gnu";
1702
+ return null;
1703
+ case "win32":
1704
+ if (arch === "x64")
1705
+ return "x86_64-pc-windows-msvc";
1706
+ return null;
1707
+ default:
1708
+ return null;
1709
+ }
1710
+ }
1711
+ function platformKey() {
1712
+ const p = getPlatformAdapter();
1713
+ return `${p.platform()}/${process.arch}`;
1714
+ }
1715
+ function reportUnsupportedTarget(cfg, progress) {
1716
+ const repoHint = cfg.defaultReleaseUrl.replace(/\/releases\/(latest\/download|download\/[^/]+).*$/, "");
1717
+ const lines = [
1718
+ `${cfg.name}: no prebuilt archive for ${platformKey()}.`,
1719
+ `Supported targets: darwin arm64/x64, linux arm64/x64, windows x64.`,
1720
+ `Fallbacks:`,
1721
+ ` 1. Build from source and put the binary on PATH (see ${repoHint || cfg.defaultReleaseUrl}).`,
1722
+ ` 2. Copy an existing binary to ~/.beeos/bin/${cfg.name}${process.platform === "win32" ? ".exe" : ""}.`,
1723
+ ` 3. Point $${cfg.binaryEnv} at an absolute path to skip discovery entirely.`
1724
+ ];
1725
+ if (cfg.releaseUrlEnv) {
1726
+ lines.push(` 4. Set $${cfg.releaseUrlEnv} to a custom release URL (e.g. an internal mirror).`);
1727
+ }
1728
+ for (const line of lines)
1729
+ progress.onStatus(line);
1730
+ }
1731
+ function releaseBaseUrl(cfg) {
1732
+ if (cfg.releaseUrlEnv) {
1733
+ const v = process.env?.[cfg.releaseUrlEnv];
1734
+ if (v)
1735
+ return v;
1736
+ }
1737
+ return cfg.defaultReleaseUrl;
1738
+ }
1739
+ function archiveName(cfg, target) {
1740
+ const suffix = target.includes("windows") ? "zip" : "tar.gz";
1741
+ return `${cfg.name}-${target}.${suffix}`;
1742
+ }
1743
+ async function downloadManagedBinary(cfg, target, progress) {
1744
+ const p = getPlatformAdapter();
1745
+ const base = releaseBaseUrl(cfg);
1746
+ const url = `${base}/${archiveName(cfg, target)}`;
1747
+ progress.onStatus(`Downloading ${cfg.name} (${target})...`);
1748
+ let resp;
1749
+ try {
1750
+ resp = await p.fetch(url);
1751
+ } catch (e) {
1752
+ progress.onStatus(`${cfg.name} download failed: ${String(e)}`);
1753
+ return null;
1754
+ }
1755
+ if (!resp.ok) {
1756
+ progress.onStatus(`${cfg.name} download: ${resp.status} ${resp.statusText} (${url})`);
1757
+ if (resp.status === 404) {
1758
+ progress.onStatus(`Hint: the current release may not include ${target}. Try setting $${cfg.binaryEnv} to a locally built binary, ` + (cfg.releaseUrlEnv ? `or $${cfg.releaseUrlEnv} to a different release URL.` : `or check for a newer release.`));
1759
+ }
1760
+ return null;
1761
+ }
1762
+ const data = new Uint8Array(await resp.arrayBuffer());
1763
+ const digestUrl = `${url}.sha256`;
1764
+ let expected = null;
1765
+ try {
1766
+ const digResp = await p.fetch(digestUrl);
1767
+ if (digResp.ok) {
1768
+ const text = (await digResp.text()).trim();
1769
+ const first = text.split(/\s+/)[0] ?? "";
1770
+ if (/^[a-f0-9]{64}$/i.test(first))
1771
+ expected = first.toLowerCase();
1772
+ }
1773
+ } catch {
1774
+ }
1775
+ if (expected) {
1776
+ const actual = createHash("sha256").update(data).digest("hex");
1777
+ if (actual !== expected) {
1778
+ progress.onStatus(`${cfg.name} checksum mismatch (expected ${expected.slice(0, 12)}\u2026, got ${actual.slice(0, 12)}\u2026) \u2014 aborting install.`);
1291
1779
  return null;
1292
1780
  }
1293
- let version = "";
1781
+ progress.onStatus(`${cfg.name} checksum ok (${expected.slice(0, 12)}\u2026).`);
1782
+ } else {
1783
+ progress.onStatus(`${cfg.name}: no .sha256 sidecar at ${digestUrl} \u2014 continuing without integrity check`);
1784
+ }
1785
+ const binDir = p.joinPath(beeoHome(), "bin");
1786
+ await p.mkdir(binDir);
1787
+ const finalPath = managedBinaryPath(cfg);
1788
+ const isZip = target.includes("windows");
1789
+ const tmpArchive = p.joinPath(binDir, `${cfg.name}.download.${Date.now()}.${isZip ? "zip" : "tar.gz"}`);
1790
+ await p.writeFileBytes(tmpArchive, data);
1791
+ try {
1792
+ progress.onStatus(`Extracting ${cfg.name}...`);
1793
+ if (isZip) {
1794
+ const exec2 = await p.exec("powershell", [
1795
+ "-NoProfile",
1796
+ "-Command",
1797
+ `Expand-Archive -Force -Path "${tmpArchive}" -DestinationPath "${binDir}"`
1798
+ ]);
1799
+ if (exec2.code !== 0) {
1800
+ throw new Error(`Expand-Archive failed: ${exec2.stderr || exec2.stdout}`.trim());
1801
+ }
1802
+ } else {
1803
+ try {
1804
+ const tar = await import("tar");
1805
+ await tar.extract({ file: tmpArchive, cwd: binDir, strip: 1 });
1806
+ } catch {
1807
+ const exec2 = await p.exec("tar", [
1808
+ "-xzf",
1809
+ tmpArchive,
1810
+ "--strip-components=1",
1811
+ "-C",
1812
+ binDir
1813
+ ]);
1814
+ if (exec2.code !== 0) {
1815
+ throw new Error(`tar extract failed: ${exec2.stderr}`.trim());
1816
+ }
1817
+ }
1818
+ }
1819
+ if (!await p.exists(finalPath)) {
1820
+ throw new Error(`extracted archive did not contain ${p.basename(finalPath)}`);
1821
+ }
1822
+ if (p.platform() !== "win32") {
1823
+ try {
1824
+ await p.chmod(finalPath, 493);
1825
+ } catch {
1826
+ }
1827
+ }
1828
+ if (p.platform() === "darwin") {
1829
+ try {
1830
+ await p.exec("xattr", ["-d", "com.apple.quarantine", finalPath]);
1831
+ } catch {
1832
+ }
1833
+ }
1834
+ progress.onComplete(`${cfg.name} installed: ${finalPath}`);
1835
+ return finalPath;
1836
+ } catch (e) {
1837
+ progress.onStatus(`${cfg.name} install failed: ${String(e)}`);
1838
+ return null;
1839
+ } finally {
1294
1840
  try {
1295
- const verResult = await p.exec(binary, ["--version"]);
1296
- version = verResult.stdout.trim();
1841
+ await p.rm(tmpArchive);
1297
1842
  } catch {
1298
1843
  }
1299
- const homeDir = p.joinPath(p.homeDir(), ".openclaw");
1300
- return { binary, version, homeDir };
1301
1844
  }
1302
- };
1845
+ }
1846
+ var init_cargo_dist = __esm({
1847
+ "../core/dist/sidecars/cargo-dist.js"() {
1848
+ "use strict";
1849
+ init_platform_adapter();
1850
+ init_config();
1851
+ }
1852
+ });
1303
1853
 
1304
- // ../core/dist/runtime/openclaw.js
1305
- var GATEWAY_PORT2 = 18789;
1306
- var openclawRuntime = {
1307
- agentFramework() {
1308
- return "openclaw";
1309
- },
1310
- displayName() {
1311
- return "OpenClaw";
1312
- },
1313
- async detect() {
1314
- const location = await findAgent(openClawDriver);
1315
- if (location.type === "notFound")
1316
- return null;
1317
- return {
1318
- binary: location.binary,
1319
- version: location.type === "system" ? location.version : "",
1320
- home: location.home
1854
+ // ../core/dist/runtime/scrcpy-bridge.js
1855
+ var CONFIG, scrcpyBridgeRuntime;
1856
+ var init_scrcpy_bridge = __esm({
1857
+ "../core/dist/runtime/scrcpy-bridge.js"() {
1858
+ "use strict";
1859
+ init_platform_adapter();
1860
+ init_config();
1861
+ init_cargo_dist();
1862
+ CONFIG = {
1863
+ name: "scrcpy-bridge",
1864
+ defaultReleaseUrl: "https://github.com/beeos-ai/scrcpy-bridge/releases/latest/download",
1865
+ releaseUrlEnv: "BEEOS_SCRCPY_BRIDGE_RELEASE_URL",
1866
+ binaryEnv: "BEEOS_SCRCPY_BRIDGE_BIN"
1867
+ };
1868
+ scrcpyBridgeRuntime = {
1869
+ async findBinary() {
1870
+ return findSidecarBinary(CONFIG);
1871
+ },
1872
+ async ensureInstalled(progress) {
1873
+ return ensureSidecarInstalled(CONFIG, progress);
1874
+ },
1875
+ async upgrade(progress) {
1876
+ return upgradeSidecar(CONFIG, progress);
1877
+ },
1878
+ /**
1879
+ * Build the environment map used when launching scrcpy-bridge for a
1880
+ * device. Exposed separately so the supervisor target-spec builder
1881
+ * can reuse the same logic without duplicating it.
1882
+ *
1883
+ * scrcpy-bridge authenticates to EMQX exclusively via Agent Gateway
1884
+ * bootstrap. `MQTT_URL` / `MQTT_TOKEN` / `MQTT_USERNAME` are
1885
+ * intentionally NOT forwarded — the binary rejects a static token
1886
+ * path because a Runtime-issued 10-minute JWT would silently expire
1887
+ * and `rumqttc` reuses the initial password on reconnect.
1888
+ */
1889
+ buildEnv(opts) {
1890
+ return {
1891
+ DEVICE_ID: opts.deviceId,
1892
+ ADB_SERIAL: opts.adbSerial,
1893
+ AGENT_GATEWAY_URL: opts.agentGatewayUrl,
1894
+ BRIDGE_PRIVATE_KEY_FILE: opts.bridgePrivateKeyFile,
1895
+ BRIDGE_PUBLIC_KEY_FILE: opts.bridgePublicKeyFile,
1896
+ LOG_FORMAT: "json",
1897
+ ...opts.jwtRefreshLeadSecs != null ? { JWT_REFRESH_LEAD_SECS: String(opts.jwtRefreshLeadSecs) } : {},
1898
+ ...opts.jwtRefreshMinIntervalSecs != null ? { JWT_REFRESH_MIN_INTERVAL_SECS: String(opts.jwtRefreshMinIntervalSecs) } : {},
1899
+ ...opts.videoBitrateKbps ? { VIDEO_BITRATE_KBPS: String(opts.videoBitrateKbps) } : {},
1900
+ ...opts.maxFps ? { MAX_FPS: String(opts.maxFps) } : {},
1901
+ ...opts.iceUrls?.length ? { ICE_URLS: opts.iceUrls.join(",") } : {},
1902
+ ...opts.turnUrls?.length ? { TURN_URLS: opts.turnUrls.join(",") } : {},
1903
+ ...opts.turnUsername ? { TURN_USERNAME: opts.turnUsername } : {},
1904
+ ...opts.turnCredential ? { TURN_CREDENTIAL: opts.turnCredential } : {},
1905
+ ...opts.extraEnv ?? {}
1906
+ };
1907
+ },
1908
+ /**
1909
+ * Spawn scrcpy-bridge detached for a single device. Kept for callers
1910
+ * that haven't migrated to the supervisor yet — new code should use
1911
+ * `buildScrcpyBridgeTargetSpec` + supervisor `upsertTarget` instead.
1912
+ */
1913
+ async spawnForDevice(opts, progress) {
1914
+ const binary = await this.ensureInstalled(progress);
1915
+ if (!binary) {
1916
+ progress.onStatus("scrcpy-bridge not installed \u2014 falling back to Python video path.");
1917
+ return null;
1918
+ }
1919
+ const p = getPlatformAdapter();
1920
+ const logDir = p.joinPath(beeoHome(), "logs");
1921
+ await p.mkdir(logDir);
1922
+ const logFile = p.joinPath(logDir, `scrcpy-bridge-${opts.deviceId}.log`);
1923
+ const env = this.buildEnv(opts);
1924
+ const result = await p.spawn(binary, [], {
1925
+ detached: true,
1926
+ env,
1927
+ stdoutFile: logFile,
1928
+ stderrFile: logFile
1929
+ });
1930
+ return { binary, pid: result.pid, logFile };
1931
+ }
1321
1932
  };
1322
- },
1323
- async ensure(version, progress) {
1324
- const location = await findAgent(openClawDriver);
1325
- if (location.type === "managed") {
1326
- return { binary: location.binary, version: "", home: location.home };
1327
- }
1328
- if (location.type === "system") {
1329
- return { binary: location.binary, version: location.version, home: location.home };
1330
- }
1331
- const dest = await downloadAgent(openClawDriver.npmPackage(), version ?? void 0, "openclaw", progress);
1332
- const home = agentHome("openclaw");
1333
- await getPlatformAdapter().mkdir(home);
1334
- await downloadPlugin(openClawDriver.pluginPackage(), "openclaw", progress);
1335
- return { binary: dest, version: "", home };
1336
- },
1337
- async launch(ctx) {
1338
- return spawnOpenclaw(ctx);
1339
- },
1340
- async isHealthy() {
1341
- return getPlatformAdapter().tcpProbe("127.0.0.1", GATEWAY_PORT2, 500);
1342
- },
1343
- async stop(agentFramework) {
1344
- const pid = await readPid(agentFramework);
1345
- if (pid != null) {
1346
- killProcess(pid);
1347
- await removePid(agentFramework);
1348
- }
1349
- },
1350
- async update(progress) {
1351
- await downloadAgent(openClawDriver.npmPackage(), void 0, "openclaw", progress);
1352
- await downloadPlugin(openClawDriver.pluginPackage(), "openclaw", progress);
1353
1933
  }
1354
- };
1934
+ });
1935
+
1936
+ // ../core/dist/runtime/vnc-bridge.js
1937
+ var CONFIG2, vncBridgeRuntime;
1938
+ var init_vnc_bridge = __esm({
1939
+ "../core/dist/runtime/vnc-bridge.js"() {
1940
+ "use strict";
1941
+ init_platform_adapter();
1942
+ init_config();
1943
+ init_cargo_dist();
1944
+ CONFIG2 = {
1945
+ name: "vnc-bridge",
1946
+ defaultReleaseUrl: "https://github.com/beeos-ai/vnc-bridge/releases/latest/download",
1947
+ releaseUrlEnv: "BEEOS_VNC_BRIDGE_RELEASE_URL",
1948
+ binaryEnv: "BEEOS_VNC_BRIDGE_BIN"
1949
+ };
1950
+ vncBridgeRuntime = {
1951
+ async findBinary() {
1952
+ return findSidecarBinary(CONFIG2);
1953
+ },
1954
+ async ensureInstalled(progress) {
1955
+ return ensureSidecarInstalled(CONFIG2, progress);
1956
+ },
1957
+ async upgrade(progress) {
1958
+ return upgradeSidecar(CONFIG2, progress);
1959
+ },
1960
+ /**
1961
+ * Build the environment map used when launching vnc-bridge for a
1962
+ * device. Exposed separately so the supervisor target-spec builder
1963
+ * can reuse the same logic without duplicating it.
1964
+ */
1965
+ buildEnv(opts) {
1966
+ return {
1967
+ DEVICE_ID: opts.deviceId,
1968
+ VNC_HOST: opts.vncHost,
1969
+ VNC_PORT: String(opts.vncPort ?? 5900),
1970
+ AGENT_GATEWAY_URL: opts.agentGatewayUrl,
1971
+ BRIDGE_PRIVATE_KEY_FILE: opts.bridgePrivateKeyFile,
1972
+ BRIDGE_PUBLIC_KEY_FILE: opts.bridgePublicKeyFile,
1973
+ LOG_FORMAT: "json",
1974
+ ...opts.vncPassword ? { VNC_PASSWORD: opts.vncPassword } : {},
1975
+ ...opts.jwtRefreshLeadSecs != null ? { JWT_REFRESH_LEAD_SECS: String(opts.jwtRefreshLeadSecs) } : {},
1976
+ ...opts.jwtRefreshMinIntervalSecs != null ? { JWT_REFRESH_MIN_INTERVAL_SECS: String(opts.jwtRefreshMinIntervalSecs) } : {},
1977
+ ...opts.videoBitrateKbps ? { VIDEO_BITRATE_KBPS: String(opts.videoBitrateKbps) } : {},
1978
+ ...opts.maxFps ? { MAX_FPS: String(opts.maxFps) } : {},
1979
+ ...opts.iceUrls?.length ? { ICE_URLS: opts.iceUrls.join(",") } : {},
1980
+ ...opts.turnUrls?.length ? { TURN_URLS: opts.turnUrls.join(",") } : {},
1981
+ ...opts.turnUsername ? { TURN_USERNAME: opts.turnUsername } : {},
1982
+ ...opts.turnCredential ? { TURN_CREDENTIAL: opts.turnCredential } : {},
1983
+ ...opts.extraEnv ?? {}
1984
+ };
1985
+ },
1986
+ /**
1987
+ * Spawn vnc-bridge detached for a single device. Kept for callers
1988
+ * that haven't migrated to the supervisor yet — new code should use
1989
+ * `buildVncBridgeTargetSpec` + supervisor `upsertTarget` instead.
1990
+ */
1991
+ async spawnForDevice(opts, progress) {
1992
+ const binary = await this.ensureInstalled(progress);
1993
+ if (!binary) {
1994
+ progress.onStatus("vnc-bridge not installed \u2014 VNC streaming disabled for this device.");
1995
+ return null;
1996
+ }
1997
+ const p = getPlatformAdapter();
1998
+ const logDir = p.joinPath(beeoHome(), "logs");
1999
+ await p.mkdir(logDir);
2000
+ const logFile = p.joinPath(logDir, `vnc-bridge-${opts.deviceId}.log`);
2001
+ const env = this.buildEnv(opts);
2002
+ const result = await p.spawn(binary, [], {
2003
+ detached: true,
2004
+ env,
2005
+ stdoutFile: logFile,
2006
+ stderrFile: logFile
2007
+ });
2008
+ return { binary, pid: result.pid, logFile };
2009
+ }
2010
+ };
2011
+ }
2012
+ });
1355
2013
 
1356
2014
  // ../core/dist/service/local-agent.js
1357
2015
  async function ensurePlugin(agentFramework, agentHomeDir, progress) {
@@ -1394,28 +2052,2013 @@ async function bind(options) {
1394
2052
  }
1395
2053
  throw new Error(`unexpected bind status: ${resp.status}`);
1396
2054
  }
1397
-
1398
- // src/node-adapter.ts
1399
- import {
1400
- execFile,
1401
- spawn as nodeSpawn
1402
- } from "child_process";
1403
- import fs from "fs";
1404
- import fsp from "fs/promises";
1405
- import net from "net";
1406
- import os from "os";
1407
- import path from "path";
1408
- var NodePlatformAdapter = class {
1409
- // ── Filesystem ──────────────────────────────────────────
1410
- async readFile(filePath) {
1411
- return fsp.readFile(filePath, "utf-8");
2055
+ var init_local_agent = __esm({
2056
+ "../core/dist/service/local-agent.js"() {
2057
+ "use strict";
2058
+ init_platform_adapter();
2059
+ init_keypair();
2060
+ init_config();
2061
+ init_detector();
2062
+ init_downloader();
2063
+ init_launcher();
2064
+ init_registry();
2065
+ init_client();
2066
+ init_bind();
1412
2067
  }
1413
- async readFileBytes(filePath) {
1414
- const buf = await fsp.readFile(filePath);
1415
- return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
2068
+ });
2069
+
2070
+ // ../core/dist/detect.js
2071
+ async function detectExistingInstall() {
2072
+ const p = getPlatformAdapter();
2073
+ const home = beeoHome();
2074
+ const beeosHomeExists = await p.exists(home);
2075
+ const keypairPath2 = p.joinPath(home, "identity", "keypair.json");
2076
+ const fpPath = p.joinPath(home, "identity", "fingerprint");
2077
+ const hasIdentity = await p.exists(keypairPath2);
2078
+ let fingerprint2 = null;
2079
+ if (await p.exists(fpPath)) {
2080
+ try {
2081
+ fingerprint2 = (await p.readFile(fpPath)).trim() || null;
2082
+ } catch {
2083
+ fingerprint2 = null;
2084
+ }
1416
2085
  }
1417
- async writeFile(filePath, content) {
1418
- await fsp.writeFile(filePath, content, "utf-8");
2086
+ let binding = null;
2087
+ try {
2088
+ binding = await loadBindingInfo();
2089
+ } catch {
2090
+ binding = null;
2091
+ }
2092
+ const openclaw = await detectOpenclaw();
2093
+ const devices = await detectDevices();
2094
+ const supervisor = await detectSupervisor();
2095
+ return {
2096
+ beeosHomeExists,
2097
+ beeosHome: home,
2098
+ hasIdentity,
2099
+ fingerprint: fingerprint2,
2100
+ binding,
2101
+ openclaw,
2102
+ devices,
2103
+ supervisor
2104
+ };
2105
+ }
2106
+ async function detectOpenclaw() {
2107
+ const location = await findAgent(openClawDriver);
2108
+ const running = await isGatewayRunning();
2109
+ if (location.type === "managed") {
2110
+ return {
2111
+ found: true,
2112
+ source: "managed",
2113
+ binary: location.binary,
2114
+ home: location.home,
2115
+ version: null,
2116
+ gatewayRunning: running
2117
+ };
2118
+ }
2119
+ if (location.type === "system") {
2120
+ return {
2121
+ found: true,
2122
+ source: "system",
2123
+ binary: location.binary,
2124
+ home: location.home,
2125
+ version: location.version || null,
2126
+ gatewayRunning: running
2127
+ };
2128
+ }
2129
+ return {
2130
+ found: false,
2131
+ source: null,
2132
+ binary: null,
2133
+ home: null,
2134
+ version: null,
2135
+ gatewayRunning: running
2136
+ };
2137
+ }
2138
+ async function detectDevices() {
2139
+ const p = getPlatformAdapter();
2140
+ const identityDir = p.joinPath(beeoHome(), "identity");
2141
+ const legacyFile = p.joinPath(beeoHome(), "devices.json");
2142
+ let keyedSerials = [];
2143
+ try {
2144
+ if (await p.exists(identityDir)) {
2145
+ const entries2 = await p.readdir(identityDir);
2146
+ keyedSerials = entries2.filter((name) => name.startsWith("device-") && name.endsWith(".key.json")).map((name) => name.replace(/^device-/, "").replace(/\.key\.json$/, ""));
2147
+ }
2148
+ } catch {
2149
+ keyedSerials = [];
2150
+ }
2151
+ let entries = [];
2152
+ if (await p.exists(legacyFile)) {
2153
+ try {
2154
+ const raw = await p.readFile(legacyFile);
2155
+ const parsed = JSON.parse(raw);
2156
+ entries = (parsed.devices ?? []).map((d) => ({
2157
+ serial: String(d.serial ?? ""),
2158
+ name: String(d.name ?? d.serial ?? ""),
2159
+ pid: Number(d.pid ?? 0),
2160
+ instanceId: d.instance_id ?? null,
2161
+ bridgePid: typeof d.bridge_pid === "number" ? d.bridge_pid : void 0
2162
+ }));
2163
+ } catch {
2164
+ entries = [];
2165
+ }
2166
+ }
2167
+ return { legacyFile, keyedSerials, entries };
2168
+ }
2169
+ async function detectSupervisor() {
2170
+ const p = getPlatformAdapter();
2171
+ const stateFile = p.joinPath(beeoHome(), "supervisor", "state.json");
2172
+ const ipcPath = supervisorSocketPath();
2173
+ let ipcReachable = false;
2174
+ try {
2175
+ ipcReachable = await p.exists(ipcPath);
2176
+ } catch {
2177
+ ipcReachable = false;
2178
+ }
2179
+ let targets = [];
2180
+ if (await p.exists(stateFile)) {
2181
+ try {
2182
+ const raw = await p.readFile(stateFile);
2183
+ const parsed = JSON.parse(raw);
2184
+ targets = (parsed.targets ?? []).map((t) => ({
2185
+ id: String(t.id ?? ""),
2186
+ kind: String(t.kind ?? ""),
2187
+ restart: t.restart ?? void 0
2188
+ }));
2189
+ } catch {
2190
+ targets = [];
2191
+ }
2192
+ }
2193
+ return { stateFile, ipcPath, ipcReachable, targets };
2194
+ }
2195
+ function supervisorSocketPath() {
2196
+ const p = getPlatformAdapter();
2197
+ if (p.platform() === "win32") {
2198
+ return "\\\\.\\pipe\\beeos-supervisor";
2199
+ }
2200
+ return p.joinPath(beeoHome(), "supervisor", "supervisor.sock");
2201
+ }
2202
+ function summarizeExistingInstall(state) {
2203
+ const lines = [];
2204
+ lines.push(`BeeOS home : ${state.beeosHome}${state.beeosHomeExists ? "" : " (not created)"}`);
2205
+ lines.push(`Identity : ${state.hasIdentity ? state.fingerprint ?? "(keypair present)" : "not created"}`);
2206
+ lines.push(`Binding : ${state.binding ? `bound \u2192 instance ${state.binding.instance_id}` : "not bound"}`);
2207
+ lines.push(`OpenClaw : ${state.openclaw.found ? `${state.openclaw.source}${state.openclaw.version ? ` v${state.openclaw.version}` : ""} (gateway ${state.openclaw.gatewayRunning ? "running" : "stopped"})` : "not installed"}`);
2208
+ lines.push(`Devices : ${state.devices.entries.length === 0 && state.devices.keyedSerials.length === 0 ? "none attached" : `${state.devices.entries.length} attached, ${state.devices.keyedSerials.length} key(s)`}`);
2209
+ lines.push(`Supervisor : ${state.supervisor.ipcReachable ? `running (${state.supervisor.targets.length} target(s))` : state.supervisor.targets.length > 0 ? `stopped, ${state.supervisor.targets.length} persisted target(s)` : "not installed"}`);
2210
+ return lines;
2211
+ }
2212
+ function inferInitChoices(state) {
2213
+ if (!state.hasIdentity && !state.openclaw.found && state.devices.entries.length === 0) {
2214
+ return { defaultDecision: "fresh", options: ["fresh"] };
2215
+ }
2216
+ if (state.hasIdentity && state.binding) {
2217
+ return {
2218
+ defaultDecision: "upgrade",
2219
+ options: ["upgrade", "rebind-keep-key", "rebind-new-key", "skip"]
2220
+ };
2221
+ }
2222
+ if (state.hasIdentity) {
2223
+ return {
2224
+ defaultDecision: "rebind-keep-key",
2225
+ options: ["upgrade", "rebind-keep-key", "rebind-new-key", "skip"]
2226
+ };
2227
+ }
2228
+ return { defaultDecision: "fresh", options: ["fresh", "skip"] };
2229
+ }
2230
+ function initDecisionLabel(decision) {
2231
+ switch (decision) {
2232
+ case "fresh":
2233
+ return "Install + bind";
2234
+ case "upgrade":
2235
+ return "Upgrade CLI & agents (keep binding)";
2236
+ case "rebind-keep-key":
2237
+ return "Re-bind (keep existing Ed25519 key)";
2238
+ case "rebind-new-key":
2239
+ return "Re-bind (rotate Ed25519 key)";
2240
+ case "skip":
2241
+ return "Skip (do nothing)";
2242
+ }
2243
+ }
2244
+ var init_detect = __esm({
2245
+ "../core/dist/detect.js"() {
2246
+ "use strict";
2247
+ init_platform_adapter();
2248
+ init_config();
2249
+ init_registry();
2250
+ init_detector();
2251
+ }
2252
+ });
2253
+
2254
+ // ../core/dist/supervisor-spec.js
2255
+ function buildOpenclawTargetSpec(ctx) {
2256
+ const isMjs = ctx.agentBinary.endsWith(".mjs") || ctx.agentBinary.endsWith(".js");
2257
+ const command = isMjs ? "node" : ctx.agentBinary;
2258
+ const baseArgs = isMjs ? [ctx.agentBinary] : [];
2259
+ const env = {
2260
+ AGENT_GATEWAY_URL: ctx.agentGatewayUrl
2261
+ };
2262
+ if (!ctx.isSystemHome) {
2263
+ env.OPENCLAW_HOME = ctx.agentHome;
2264
+ env.OPENCLAW_GATEWAY_TOKEN = ctx.gatewayToken;
2265
+ }
2266
+ return {
2267
+ id: "openclaw",
2268
+ kind: "openclaw",
2269
+ command,
2270
+ args: [...baseArgs, "gateway", "--allow-unconfigured", "--bind", "lan"],
2271
+ env,
2272
+ cwd: ctx.agentHome,
2273
+ restart: "on-failure",
2274
+ label: "OpenClaw Gateway"
2275
+ };
2276
+ }
2277
+ function buildDeviceAgentTargetSpec(input) {
2278
+ const args = [
2279
+ ...input.args,
2280
+ "--key",
2281
+ input.keyFile,
2282
+ "--adb",
2283
+ input.serial,
2284
+ "--bridge",
2285
+ input.bridgeUrl
2286
+ ];
2287
+ if (input.httpEnabled) {
2288
+ args.push("--http", "--http-port", String(input.httpPort));
2289
+ }
2290
+ const env = {};
2291
+ if (input.agentGatewayUrl)
2292
+ env.AGENT_GATEWAY_URL = input.agentGatewayUrl;
2293
+ return {
2294
+ id: `device-agent-${input.serial}`,
2295
+ kind: "device-agent",
2296
+ command: input.command,
2297
+ args,
2298
+ env: Object.keys(env).length > 0 ? env : void 0,
2299
+ restart: "on-failure",
2300
+ label: `device-agent (${input.serial})`
2301
+ };
2302
+ }
2303
+ function buildScrcpyBridgeTargetSpec(binary, opts) {
2304
+ return {
2305
+ id: `scrcpy-bridge-${opts.deviceId}`,
2306
+ kind: "scrcpy-bridge",
2307
+ command: binary,
2308
+ args: [],
2309
+ env: scrcpyBridgeRuntime.buildEnv(opts),
2310
+ restart: "on-failure",
2311
+ label: `scrcpy-bridge (${opts.deviceId})`
2312
+ };
2313
+ }
2314
+ function buildVncBridgeTargetSpec(binary, opts) {
2315
+ return {
2316
+ id: `vnc-bridge-${opts.deviceId}`,
2317
+ kind: "vnc-bridge",
2318
+ command: binary,
2319
+ args: [],
2320
+ env: vncBridgeRuntime.buildEnv(opts),
2321
+ restart: "on-failure",
2322
+ label: `vnc-bridge (${opts.deviceId})`
2323
+ };
2324
+ }
2325
+ var init_supervisor_spec = __esm({
2326
+ "../core/dist/supervisor-spec.js"() {
2327
+ "use strict";
2328
+ init_scrcpy_bridge();
2329
+ init_vnc_bridge();
2330
+ }
2331
+ });
2332
+
2333
+ // ../core/dist/services/types.js
2334
+ var init_types2 = __esm({
2335
+ "../core/dist/services/types.js"() {
2336
+ "use strict";
2337
+ }
2338
+ });
2339
+
2340
+ // ../core/dist/services/ids.js
2341
+ import path from "path";
2342
+ function safeId(id) {
2343
+ return id.replace(/[:/]+/g, "-").replace(/[^A-Za-z0-9._-]/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
2344
+ }
2345
+ function launchdLabel(id) {
2346
+ return `${LABEL_PREFIX}${safeId(id)}`;
2347
+ }
2348
+ function systemdUnit(id) {
2349
+ return `${SYSTEMD_PREFIX}${safeId(id)}.service`;
2350
+ }
2351
+ function windowsTaskName(id) {
2352
+ return `${WIN_TASK_PREFIX}${safeId(id)}`;
2353
+ }
2354
+ function serviceLogPath(id) {
2355
+ return path.join(beeoHome(), "logs", "services", `${safeId(id)}.log`);
2356
+ }
2357
+ function idFromLaunchdLabel(label) {
2358
+ if (!label.startsWith(LABEL_PREFIX))
2359
+ return null;
2360
+ return label.slice(LABEL_PREFIX.length);
2361
+ }
2362
+ function idFromSystemdUnit(unit) {
2363
+ if (!unit.startsWith(SYSTEMD_PREFIX))
2364
+ return null;
2365
+ const base = unit.endsWith(".service") ? unit.slice(0, -".service".length) : unit;
2366
+ return base.slice(SYSTEMD_PREFIX.length);
2367
+ }
2368
+ function idFromWindowsTaskName(task) {
2369
+ if (!task.startsWith(WIN_TASK_PREFIX))
2370
+ return null;
2371
+ return task.slice(WIN_TASK_PREFIX.length);
2372
+ }
2373
+ var LABEL_PREFIX, SYSTEMD_PREFIX, WIN_TASK_PREFIX;
2374
+ var init_ids = __esm({
2375
+ "../core/dist/services/ids.js"() {
2376
+ "use strict";
2377
+ init_config();
2378
+ LABEL_PREFIX = "ai.beeos.";
2379
+ SYSTEMD_PREFIX = "beeos-";
2380
+ WIN_TASK_PREFIX = "BeeOS-";
2381
+ }
2382
+ });
2383
+
2384
+ // ../core/dist/services/launchd.js
2385
+ import fs from "fs/promises";
2386
+ import fsSync from "fs";
2387
+ import os from "os";
2388
+ import path2 from "path";
2389
+ import { execFile } from "child_process";
2390
+ import { promisify } from "util";
2391
+ function renderPlist(spec, label, logFile) {
2392
+ const args = [spec.command, ...spec.args].map((a) => ` <string>${escapeXml(a)}</string>`);
2393
+ const envEntries = Object.entries(spec.env ?? {});
2394
+ if (!envEntries.some(([k]) => k === "PATH")) {
2395
+ envEntries.unshift([
2396
+ "PATH",
2397
+ "/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin"
2398
+ ]);
2399
+ }
2400
+ const envXml = envEntries.map(([k, v]) => ` <key>${escapeXml(k)}</key>
2401
+ <string>${escapeXml(v)}</string>`).join("\n");
2402
+ const restart = spec.restart ?? "on-failure";
2403
+ let keepAliveXml = "";
2404
+ if (restart === "always") {
2405
+ keepAliveXml = ` <key>KeepAlive</key>
2406
+ <true/>
2407
+ `;
2408
+ } else if (restart === "on-failure") {
2409
+ keepAliveXml = ` <key>KeepAlive</key>
2410
+ <dict>
2411
+ <key>SuccessfulExit</key>
2412
+ <false/>
2413
+ </dict>
2414
+ `;
2415
+ }
2416
+ const cwdXml = spec.cwd ? ` <key>WorkingDirectory</key>
2417
+ <string>${escapeXml(spec.cwd)}</string>
2418
+ ` : "";
2419
+ return `<?xml version="1.0" encoding="UTF-8"?>
2420
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
2421
+ <plist version="1.0">
2422
+ <dict>
2423
+ <key>Label</key>
2424
+ <string>${escapeXml(label)}</string>
2425
+ <key>ProgramArguments</key>
2426
+ <array>
2427
+ ${args.join("\n")}
2428
+ </array>
2429
+ <key>RunAtLoad</key>
2430
+ <true/>
2431
+ ${keepAliveXml} <key>ThrottleInterval</key>
2432
+ <integer>10</integer>
2433
+ <key>ProcessType</key>
2434
+ <string>Background</string>
2435
+ <key>StandardOutPath</key>
2436
+ <string>${escapeXml(logFile)}</string>
2437
+ <key>StandardErrorPath</key>
2438
+ <string>${escapeXml(logFile)}</string>
2439
+ ${cwdXml} <key>EnvironmentVariables</key>
2440
+ <dict>
2441
+ ${envXml}
2442
+ </dict>
2443
+ </dict>
2444
+ </plist>
2445
+ `;
2446
+ }
2447
+ function escapeXml(s) {
2448
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
2449
+ }
2450
+ function detectKind(id) {
2451
+ if (id === "openclaw")
2452
+ return "openclaw";
2453
+ if (id.startsWith("device-agent-"))
2454
+ return "device-agent";
2455
+ if (id.startsWith("scrcpy-bridge-"))
2456
+ return "scrcpy-bridge";
2457
+ if (id.startsWith("vnc-bridge-"))
2458
+ return "vnc-bridge";
2459
+ return "unknown";
2460
+ }
2461
+ function fallbackStatus(spec, _label, logFile, running) {
2462
+ return {
2463
+ id: spec.id,
2464
+ kind: spec.kind,
2465
+ installed: true,
2466
+ running,
2467
+ pid: null,
2468
+ lastExitCode: null,
2469
+ logFile,
2470
+ label: spec.label ?? null
2471
+ };
2472
+ }
2473
+ var execFileP, LaunchdServiceManager;
2474
+ var init_launchd = __esm({
2475
+ "../core/dist/services/launchd.js"() {
2476
+ "use strict";
2477
+ init_ids();
2478
+ execFileP = promisify(execFile);
2479
+ LaunchdServiceManager = class {
2480
+ kind = "launchd";
2481
+ launchAgentsDir = path2.join(os.homedir(), "Library", "LaunchAgents");
2482
+ logPath(id) {
2483
+ return serviceLogPath(id);
2484
+ }
2485
+ async install(spec) {
2486
+ const label = launchdLabel(spec.id);
2487
+ const plist = this.plistPath(label);
2488
+ const logFile = this.logPath(spec.id);
2489
+ await fs.mkdir(path2.dirname(plist), { recursive: true });
2490
+ await fs.mkdir(path2.dirname(logFile), { recursive: true });
2491
+ await fs.writeFile(plist, renderPlist(spec, label, logFile), {
2492
+ mode: 420
2493
+ });
2494
+ const uid = process.getuid?.();
2495
+ if (uid !== void 0) {
2496
+ try {
2497
+ await execFileP("launchctl", ["bootout", `gui/${uid}/${label}`]);
2498
+ } catch {
2499
+ }
2500
+ try {
2501
+ await execFileP("launchctl", ["bootstrap", `gui/${uid}`, plist]);
2502
+ } catch (e) {
2503
+ const msg = e instanceof Error ? e.message : String(e);
2504
+ try {
2505
+ await execFileP("launchctl", ["load", "-w", plist]);
2506
+ } catch {
2507
+ throw new Error(`launchctl bootstrap failed for ${label}: ${msg}`);
2508
+ }
2509
+ }
2510
+ try {
2511
+ await execFileP("launchctl", ["kickstart", "-k", `gui/${uid}/${label}`]);
2512
+ } catch {
2513
+ }
2514
+ } else {
2515
+ await execFileP("launchctl", ["load", "-w", plist]);
2516
+ }
2517
+ const status2 = await this.status(spec.id);
2518
+ return status2 ?? fallbackStatus(spec, label, logFile, false);
2519
+ }
2520
+ async uninstall(id) {
2521
+ const label = launchdLabel(id);
2522
+ const plist = this.plistPath(label);
2523
+ const existed = fsSync.existsSync(plist);
2524
+ const uid = process.getuid?.();
2525
+ if (uid !== void 0) {
2526
+ try {
2527
+ await execFileP("launchctl", ["bootout", `gui/${uid}/${label}`]);
2528
+ } catch {
2529
+ try {
2530
+ await execFileP("launchctl", ["unload", "-w", plist]);
2531
+ } catch {
2532
+ }
2533
+ }
2534
+ } else {
2535
+ try {
2536
+ await execFileP("launchctl", ["unload", "-w", plist]);
2537
+ } catch {
2538
+ }
2539
+ }
2540
+ if (existed) {
2541
+ try {
2542
+ await fs.unlink(plist);
2543
+ } catch {
2544
+ }
2545
+ }
2546
+ return existed;
2547
+ }
2548
+ async restart(id) {
2549
+ const label = launchdLabel(id);
2550
+ const uid = process.getuid?.();
2551
+ if (uid === void 0) {
2552
+ throw new Error("launchctl restart requires a valid uid");
2553
+ }
2554
+ await execFileP("launchctl", ["kickstart", "-k", `gui/${uid}/${label}`]);
2555
+ }
2556
+ async status(id) {
2557
+ const label = launchdLabel(id);
2558
+ const plist = this.plistPath(label);
2559
+ if (!fsSync.existsSync(plist))
2560
+ return null;
2561
+ const logFile = this.logPath(id);
2562
+ try {
2563
+ const { stdout } = await execFileP("launchctl", ["list", label]);
2564
+ const pidMatch = /"PID"\s*=\s*(\d+)/.exec(stdout);
2565
+ const exitMatch = /"LastExitStatus"\s*=\s*(-?\d+)/.exec(stdout);
2566
+ const pid = pidMatch ? Number(pidMatch[1]) : null;
2567
+ return {
2568
+ id,
2569
+ kind: detectKind(id),
2570
+ installed: true,
2571
+ running: pid !== null && pid > 0,
2572
+ pid,
2573
+ lastExitCode: exitMatch ? Number(exitMatch[1]) : null,
2574
+ logFile,
2575
+ label: null
2576
+ };
2577
+ } catch {
2578
+ return {
2579
+ id,
2580
+ kind: detectKind(id),
2581
+ installed: true,
2582
+ running: false,
2583
+ pid: null,
2584
+ lastExitCode: null,
2585
+ logFile,
2586
+ label: null
2587
+ };
2588
+ }
2589
+ }
2590
+ async list() {
2591
+ let stdout = "";
2592
+ try {
2593
+ ({ stdout } = await execFileP("launchctl", ["list"]));
2594
+ } catch {
2595
+ return [];
2596
+ }
2597
+ const results = [];
2598
+ for (const raw of stdout.split("\n")) {
2599
+ const line = raw.trim();
2600
+ if (!line)
2601
+ continue;
2602
+ const cols = line.split(/\s+/);
2603
+ if (cols.length < 3)
2604
+ continue;
2605
+ const label = cols[cols.length - 1];
2606
+ const id = idFromLaunchdLabel(label);
2607
+ if (!id)
2608
+ continue;
2609
+ const pidCol = cols[0];
2610
+ const pid = pidCol === "-" ? null : Number(pidCol);
2611
+ const exitCol = cols[1];
2612
+ const lastExitCode = exitCol === "-" ? null : Number(exitCol);
2613
+ results.push({
2614
+ id,
2615
+ kind: detectKind(id),
2616
+ installed: true,
2617
+ running: pid !== null && !Number.isNaN(pid) && pid > 0,
2618
+ pid: pid !== null && !Number.isNaN(pid) ? pid : null,
2619
+ lastExitCode: lastExitCode !== null && !Number.isNaN(lastExitCode) ? lastExitCode : null,
2620
+ logFile: this.logPath(id),
2621
+ label: null
2622
+ });
2623
+ }
2624
+ try {
2625
+ const files = await fs.readdir(this.launchAgentsDir);
2626
+ for (const f of files) {
2627
+ if (!f.startsWith(LABEL_PREFIX) || !f.endsWith(".plist"))
2628
+ continue;
2629
+ const label = f.slice(0, -".plist".length);
2630
+ const id = idFromLaunchdLabel(label);
2631
+ if (!id)
2632
+ continue;
2633
+ if (results.some((r) => r.id === id))
2634
+ continue;
2635
+ results.push({
2636
+ id,
2637
+ kind: detectKind(id),
2638
+ installed: true,
2639
+ running: false,
2640
+ pid: null,
2641
+ lastExitCode: null,
2642
+ logFile: this.logPath(id),
2643
+ label: null
2644
+ });
2645
+ }
2646
+ } catch {
2647
+ }
2648
+ return results;
2649
+ }
2650
+ async selfCheck() {
2651
+ try {
2652
+ await execFileP("launchctl", ["help"]);
2653
+ return { available: true, warnings: [], hints: [] };
2654
+ } catch {
2655
+ return {
2656
+ available: false,
2657
+ warnings: ["launchctl is not available on this system"],
2658
+ hints: ["launchd is only available on macOS"]
2659
+ };
2660
+ }
2661
+ }
2662
+ plistPath(label) {
2663
+ return path2.join(this.launchAgentsDir, `${label}.plist`);
2664
+ }
2665
+ };
2666
+ }
2667
+ });
2668
+
2669
+ // ../core/dist/services/systemd.js
2670
+ import fs2 from "fs/promises";
2671
+ import fsSync2 from "fs";
2672
+ import os2 from "os";
2673
+ import path3 from "path";
2674
+ import { execFile as execFile2 } from "child_process";
2675
+ import { promisify as promisify2 } from "util";
2676
+ function renderUnit(spec, logFile) {
2677
+ const restart = spec.restart ?? "on-failure";
2678
+ const restartValue = restart === "always" ? "always" : restart === "never" ? "no" : "on-failure";
2679
+ const execStart = [shellQuote(spec.command), ...spec.args.map(shellQuote)].join(" ");
2680
+ const envLines = Object.entries(spec.env ?? {}).map(([k, v]) => `Environment=${k}=${escapeSystemdValue(v)}`).join("\n");
2681
+ const cwdLine = spec.cwd ? `WorkingDirectory=${spec.cwd}
2682
+ ` : "";
2683
+ const labelLine = spec.label ? spec.label : `BeeOS ${spec.kind} (${spec.id})`;
2684
+ return `[Unit]
2685
+ Description=${labelLine}
2686
+ After=network-online.target
2687
+ Wants=network-online.target
2688
+
2689
+ [Service]
2690
+ Type=simple
2691
+ ExecStart=${execStart}
2692
+ Restart=${restartValue}
2693
+ RestartSec=5
2694
+ StartLimitBurst=10
2695
+ StartLimitIntervalSec=60
2696
+ StandardOutput=append:${logFile}
2697
+ StandardError=append:${logFile}
2698
+ # Minimal PATH so spawned children can find interpreters.
2699
+ Environment=PATH=/usr/local/bin:/usr/bin:/bin
2700
+ ${envLines ? envLines + "\n" : ""}${cwdLine}NoNewPrivileges=true
2701
+
2702
+ [Install]
2703
+ WantedBy=default.target
2704
+ `;
2705
+ }
2706
+ function escapeSystemdValue(v) {
2707
+ if (/[\s"'$]/.test(v))
2708
+ return `"${v.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
2709
+ return v;
2710
+ }
2711
+ function shellQuote(s) {
2712
+ if (s === "")
2713
+ return '""';
2714
+ if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(s))
2715
+ return s;
2716
+ return `"${s.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
2717
+ }
2718
+ function parseShowOutput(stdout) {
2719
+ const out = {};
2720
+ for (const line of stdout.split("\n")) {
2721
+ const eq = line.indexOf("=");
2722
+ if (eq < 0)
2723
+ continue;
2724
+ out[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
2725
+ }
2726
+ return out;
2727
+ }
2728
+ function detectKind2(id) {
2729
+ if (id === "openclaw")
2730
+ return "openclaw";
2731
+ if (id.startsWith("device-agent-"))
2732
+ return "device-agent";
2733
+ if (id.startsWith("scrcpy-bridge-"))
2734
+ return "scrcpy-bridge";
2735
+ if (id.startsWith("vnc-bridge-"))
2736
+ return "vnc-bridge";
2737
+ return "unknown";
2738
+ }
2739
+ var execFileP2, SystemdServiceManager;
2740
+ var init_systemd = __esm({
2741
+ "../core/dist/services/systemd.js"() {
2742
+ "use strict";
2743
+ init_ids();
2744
+ execFileP2 = promisify2(execFile2);
2745
+ SystemdServiceManager = class {
2746
+ kind = "systemd-user";
2747
+ unitDir = path3.join(os2.homedir(), ".config", "systemd", "user");
2748
+ logPath(id) {
2749
+ return serviceLogPath(id);
2750
+ }
2751
+ async install(spec) {
2752
+ const unit = systemdUnit(spec.id);
2753
+ const unitFile = this.unitPath(unit);
2754
+ const logFile = this.logPath(spec.id);
2755
+ await fs2.mkdir(path3.dirname(unitFile), { recursive: true });
2756
+ await fs2.mkdir(path3.dirname(logFile), { recursive: true });
2757
+ await fs2.writeFile(unitFile, renderUnit(spec, logFile), { mode: 420 });
2758
+ try {
2759
+ await execFileP2("systemctl", ["--user", "daemon-reload"]);
2760
+ } catch (e) {
2761
+ throw new Error(`systemctl --user daemon-reload failed (is systemd --user available?): ${e}`);
2762
+ }
2763
+ await execFileP2("systemctl", ["--user", "enable", "--now", unit]);
2764
+ try {
2765
+ await execFileP2("systemctl", ["--user", "restart", unit]);
2766
+ } catch {
2767
+ }
2768
+ const status2 = await this.status(spec.id);
2769
+ return status2 ?? {
2770
+ id: spec.id,
2771
+ kind: spec.kind,
2772
+ installed: true,
2773
+ running: false,
2774
+ pid: null,
2775
+ lastExitCode: null,
2776
+ logFile,
2777
+ label: spec.label ?? null
2778
+ };
2779
+ }
2780
+ async uninstall(id) {
2781
+ const unit = systemdUnit(id);
2782
+ const unitFile = this.unitPath(unit);
2783
+ const existed = fsSync2.existsSync(unitFile);
2784
+ if (existed) {
2785
+ try {
2786
+ await execFileP2("systemctl", ["--user", "disable", "--now", unit]);
2787
+ } catch {
2788
+ }
2789
+ try {
2790
+ await fs2.unlink(unitFile);
2791
+ } catch {
2792
+ }
2793
+ try {
2794
+ await execFileP2("systemctl", ["--user", "daemon-reload"]);
2795
+ } catch {
2796
+ }
2797
+ } else {
2798
+ try {
2799
+ await execFileP2("systemctl", ["--user", "disable", "--now", unit]);
2800
+ } catch {
2801
+ }
2802
+ }
2803
+ return existed;
2804
+ }
2805
+ async restart(id) {
2806
+ const unit = systemdUnit(id);
2807
+ await execFileP2("systemctl", ["--user", "restart", unit]);
2808
+ }
2809
+ async status(id) {
2810
+ const unit = systemdUnit(id);
2811
+ const unitFile = this.unitPath(unit);
2812
+ if (!fsSync2.existsSync(unitFile))
2813
+ return null;
2814
+ const logFile = this.logPath(id);
2815
+ let active = false;
2816
+ let pid = null;
2817
+ let exitCode = null;
2818
+ try {
2819
+ const { stdout } = await execFileP2("systemctl", [
2820
+ "--user",
2821
+ "show",
2822
+ unit,
2823
+ "--property=ActiveState,MainPID,ExecMainStatus"
2824
+ ]);
2825
+ const props = parseShowOutput(stdout);
2826
+ active = props.ActiveState === "active";
2827
+ const mp = Number(props.MainPID);
2828
+ pid = Number.isFinite(mp) && mp > 0 ? mp : null;
2829
+ const ex = Number(props.ExecMainStatus);
2830
+ exitCode = Number.isFinite(ex) ? ex : null;
2831
+ } catch {
2832
+ }
2833
+ return {
2834
+ id,
2835
+ kind: detectKind2(id),
2836
+ installed: true,
2837
+ running: active && pid !== null,
2838
+ pid,
2839
+ lastExitCode: exitCode,
2840
+ logFile,
2841
+ label: null
2842
+ };
2843
+ }
2844
+ async list() {
2845
+ const results = [];
2846
+ let unitFiles = [];
2847
+ try {
2848
+ unitFiles = (await fs2.readdir(this.unitDir)).filter((f) => f.startsWith(SYSTEMD_PREFIX) && f.endsWith(".service"));
2849
+ } catch {
2850
+ return results;
2851
+ }
2852
+ for (const f of unitFiles) {
2853
+ const id = idFromSystemdUnit(f);
2854
+ if (!id)
2855
+ continue;
2856
+ const s = await this.status(id);
2857
+ if (s)
2858
+ results.push(s);
2859
+ }
2860
+ return results;
2861
+ }
2862
+ async selfCheck() {
2863
+ const warnings = [];
2864
+ const hints = [];
2865
+ try {
2866
+ await execFileP2("systemctl", ["--user", "--version"]);
2867
+ } catch {
2868
+ return {
2869
+ available: false,
2870
+ warnings: ["systemctl --user is not available"],
2871
+ hints: [
2872
+ "Install systemd or use a distro with systemd --user enabled."
2873
+ ]
2874
+ };
2875
+ }
2876
+ try {
2877
+ const user = os2.userInfo().username;
2878
+ const { stdout } = await execFileP2("loginctl", ["show-user", user]);
2879
+ if (!/Linger=yes/i.test(stdout)) {
2880
+ warnings.push("systemd --user linger is OFF \u2014 BeeOS services will stop when you log out.");
2881
+ hints.push(`sudo loginctl enable-linger ${user}`);
2882
+ }
2883
+ } catch {
2884
+ hints.push("Could not query loginctl; linger status unknown.");
2885
+ }
2886
+ return { available: true, warnings, hints };
2887
+ }
2888
+ unitPath(unit) {
2889
+ return path3.join(this.unitDir, unit);
2890
+ }
2891
+ };
2892
+ }
2893
+ });
2894
+
2895
+ // ../core/dist/services/task-scheduler.js
2896
+ import fs3 from "fs/promises";
2897
+ import os3 from "os";
2898
+ import path4 from "path";
2899
+ import { execFile as execFile3 } from "child_process";
2900
+ import { promisify as promisify3 } from "util";
2901
+ function renderTaskXml(spec, logFile) {
2902
+ const restart = spec.restart ?? "on-failure";
2903
+ const restartXml = restart === "never" ? "" : ` <RestartOnFailure>
2904
+ <Interval>PT1M</Interval>
2905
+ <Count>999</Count>
2906
+ </RestartOnFailure>`;
2907
+ const cwdXml = spec.cwd ? ` <WorkingDirectory>${escapeXml2(spec.cwd)}</WorkingDirectory>
2908
+ ` : "";
2909
+ const author = (() => {
2910
+ try {
2911
+ return os3.userInfo().username;
2912
+ } catch {
2913
+ return "BeeOS";
2914
+ }
2915
+ })();
2916
+ const innerArgs = spec.args.map(quoteCmdArg).join(" ");
2917
+ const envPrefix = Object.entries(spec.env ?? {}).map(([k, v]) => `set ${cmdEscapeVar(k)}=${cmdEscapeValue(v)}&& `).join("");
2918
+ const inner = `${envPrefix}${quoteCmdArg(spec.command)}${innerArgs ? " " + innerArgs : ""} >> ${quoteCmdArg(logFile)} 2>&1`;
2919
+ const command = "cmd.exe";
2920
+ const argsAttr = `/d /c "${inner}"`;
2921
+ return `<?xml version="1.0" encoding="UTF-16"?>
2922
+ <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
2923
+ <RegistrationInfo>
2924
+ <Author>${escapeXml2(author)}</Author>
2925
+ <Description>${escapeXml2(spec.label ?? `BeeOS ${spec.kind} (${spec.id})`)}</Description>
2926
+ </RegistrationInfo>
2927
+ <Triggers>
2928
+ <LogonTrigger>
2929
+ <Enabled>true</Enabled>
2930
+ <UserId>${escapeXml2(author)}</UserId>
2931
+ </LogonTrigger>
2932
+ </Triggers>
2933
+ <Principals>
2934
+ <Principal id="Author">
2935
+ <UserId>${escapeXml2(author)}</UserId>
2936
+ <LogonType>InteractiveToken</LogonType>
2937
+ <RunLevel>LeastPrivilege</RunLevel>
2938
+ </Principal>
2939
+ </Principals>
2940
+ <Settings>
2941
+ <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
2942
+ <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
2943
+ <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
2944
+ <AllowHardTerminate>true</AllowHardTerminate>
2945
+ <StartWhenAvailable>true</StartWhenAvailable>
2946
+ <AllowStartOnDemand>true</AllowStartOnDemand>
2947
+ <Enabled>true</Enabled>
2948
+ <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
2949
+ ${restartXml ? restartXml + "\n" : ""} </Settings>
2950
+ <Actions Context="Author">
2951
+ <Exec>
2952
+ <Command>${escapeXml2(command)}</Command>
2953
+ <Arguments>${escapeXml2(argsAttr)}</Arguments>
2954
+ ${cwdXml} </Exec>
2955
+ </Actions>
2956
+ </Task>`;
2957
+ }
2958
+ function quoteCmdArg(s) {
2959
+ return `"${s.replace(/"/g, '""')}"`;
2960
+ }
2961
+ function cmdEscapeVar(k) {
2962
+ return k.replace(/[=]/g, "");
2963
+ }
2964
+ function cmdEscapeValue(v) {
2965
+ return v.replace(/(["&|<>^])/g, "^$1");
2966
+ }
2967
+ function escapeXml2(s) {
2968
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
2969
+ }
2970
+ function detectKind3(id) {
2971
+ if (id === "openclaw")
2972
+ return "openclaw";
2973
+ if (id.startsWith("device-agent-"))
2974
+ return "device-agent";
2975
+ if (id.startsWith("scrcpy-bridge-"))
2976
+ return "scrcpy-bridge";
2977
+ if (id.startsWith("vnc-bridge-"))
2978
+ return "vnc-bridge";
2979
+ return "unknown";
2980
+ }
2981
+ var execFileP3, TaskSchedulerServiceManager;
2982
+ var init_task_scheduler = __esm({
2983
+ "../core/dist/services/task-scheduler.js"() {
2984
+ "use strict";
2985
+ init_ids();
2986
+ execFileP3 = promisify3(execFile3);
2987
+ TaskSchedulerServiceManager = class {
2988
+ kind = "task-scheduler";
2989
+ logPath(id) {
2990
+ return serviceLogPath(id);
2991
+ }
2992
+ async install(spec) {
2993
+ const task = windowsTaskName(spec.id);
2994
+ const logFile = this.logPath(spec.id);
2995
+ await fs3.mkdir(path4.dirname(logFile), { recursive: true });
2996
+ const xml = renderTaskXml(spec, logFile);
2997
+ const tmp = path4.join(os3.tmpdir(), `beeos-task-${Date.now()}-${spec.id.replace(/[^a-z0-9]/gi, "_")}.xml`);
2998
+ await fs3.writeFile(tmp, "\uFEFF" + xml, { encoding: "utf16le" });
2999
+ try {
3000
+ await execFileP3("schtasks", ["/Create", "/TN", task, "/XML", tmp, "/F"]);
3001
+ try {
3002
+ await execFileP3("schtasks", ["/Run", "/TN", task]);
3003
+ } catch {
3004
+ }
3005
+ } finally {
3006
+ try {
3007
+ await fs3.unlink(tmp);
3008
+ } catch {
3009
+ }
3010
+ }
3011
+ const status2 = await this.status(spec.id);
3012
+ return status2 ?? {
3013
+ id: spec.id,
3014
+ kind: spec.kind,
3015
+ installed: true,
3016
+ running: false,
3017
+ pid: null,
3018
+ lastExitCode: null,
3019
+ logFile,
3020
+ label: spec.label ?? null
3021
+ };
3022
+ }
3023
+ async uninstall(id) {
3024
+ const task = windowsTaskName(id);
3025
+ try {
3026
+ await execFileP3("schtasks", ["/Delete", "/TN", task, "/F"]);
3027
+ return true;
3028
+ } catch {
3029
+ return false;
3030
+ }
3031
+ }
3032
+ async restart(id) {
3033
+ const task = windowsTaskName(id);
3034
+ try {
3035
+ await execFileP3("schtasks", ["/End", "/TN", task]);
3036
+ } catch {
3037
+ }
3038
+ await execFileP3("schtasks", ["/Run", "/TN", task]);
3039
+ }
3040
+ async status(id) {
3041
+ const task = windowsTaskName(id);
3042
+ const logFile = this.logPath(id);
3043
+ try {
3044
+ const { stdout } = await execFileP3("schtasks", [
3045
+ "/Query",
3046
+ "/TN",
3047
+ task,
3048
+ "/V",
3049
+ "/FO",
3050
+ "CSV"
3051
+ ]);
3052
+ const running = /"\s*Running\s*"/i.test(stdout);
3053
+ const exitMatch = /"Last Result"\s*,\s*"(-?\d+)"/i.exec(stdout);
3054
+ return {
3055
+ id,
3056
+ kind: detectKind3(id),
3057
+ installed: true,
3058
+ running,
3059
+ pid: null,
3060
+ lastExitCode: exitMatch ? Number(exitMatch[1]) : null,
3061
+ logFile,
3062
+ label: null
3063
+ };
3064
+ } catch {
3065
+ return null;
3066
+ }
3067
+ }
3068
+ async list() {
3069
+ let stdout = "";
3070
+ try {
3071
+ ({ stdout } = await execFileP3("schtasks", ["/Query", "/FO", "CSV"]));
3072
+ } catch {
3073
+ return [];
3074
+ }
3075
+ const results = [];
3076
+ const lines = stdout.split(/\r?\n/).slice(1);
3077
+ for (const line of lines) {
3078
+ const m = /^"([^"]+)","([^"]*)","([^"]*)"/.exec(line);
3079
+ if (!m)
3080
+ continue;
3081
+ const rawName = m[1];
3082
+ const bare = rawName.startsWith("\\") ? rawName.slice(1) : rawName;
3083
+ if (!bare.startsWith(WIN_TASK_PREFIX))
3084
+ continue;
3085
+ const id = idFromWindowsTaskName(bare);
3086
+ if (!id)
3087
+ continue;
3088
+ results.push({
3089
+ id,
3090
+ kind: detectKind3(id),
3091
+ installed: true,
3092
+ running: /Running/i.test(m[3]),
3093
+ pid: null,
3094
+ lastExitCode: null,
3095
+ logFile: this.logPath(id),
3096
+ label: null
3097
+ });
3098
+ }
3099
+ return results;
3100
+ }
3101
+ async selfCheck() {
3102
+ try {
3103
+ await execFileP3("schtasks", ["/?"]);
3104
+ return {
3105
+ available: true,
3106
+ warnings: ["Windows Task Scheduler support is in preview \u2014 restart-on-crash is best-effort."],
3107
+ hints: []
3108
+ };
3109
+ } catch {
3110
+ return {
3111
+ available: false,
3112
+ warnings: ["schtasks.exe is not available"],
3113
+ hints: ["Windows Task Scheduler is only available on Windows."]
3114
+ };
3115
+ }
3116
+ }
3117
+ };
3118
+ }
3119
+ });
3120
+
3121
+ // ../core/dist/services/fallback.js
3122
+ import fs4 from "fs/promises";
3123
+ import fsSync3 from "fs";
3124
+ import path5 from "path";
3125
+ import { spawn } from "child_process";
3126
+ function isAlive(pid) {
3127
+ try {
3128
+ process.kill(pid, 0);
3129
+ return true;
3130
+ } catch {
3131
+ return false;
3132
+ }
3133
+ }
3134
+ var FallbackServiceManager;
3135
+ var init_fallback = __esm({
3136
+ "../core/dist/services/fallback.js"() {
3137
+ "use strict";
3138
+ init_ids();
3139
+ init_config();
3140
+ FallbackServiceManager = class {
3141
+ kind = "fallback";
3142
+ stateDir = path5.join(beeoHome(), "services", "fallback");
3143
+ logPath(id) {
3144
+ return serviceLogPath(id);
3145
+ }
3146
+ async install(spec) {
3147
+ const logFile = this.logPath(spec.id);
3148
+ await fs4.mkdir(path5.dirname(logFile), { recursive: true });
3149
+ await fs4.mkdir(this.stateDir, { recursive: true });
3150
+ await this.uninstall(spec.id);
3151
+ const out = fsSync3.openSync(logFile, "a");
3152
+ const err = fsSync3.openSync(logFile, "a");
3153
+ const child = spawn(spec.command, spec.args, {
3154
+ cwd: spec.cwd,
3155
+ env: { ...process.env, ...spec.env ?? {} },
3156
+ detached: true,
3157
+ stdio: ["ignore", out, err]
3158
+ });
3159
+ child.unref();
3160
+ if (child.pid) {
3161
+ await fs4.writeFile(this.pidPath(spec.id), `${child.pid}
3162
+ ${spec.id}
3163
+ `);
3164
+ await fs4.writeFile(this.specPath(spec.id), JSON.stringify({ spec, startedAt: Date.now() }, null, 2));
3165
+ }
3166
+ return {
3167
+ id: spec.id,
3168
+ kind: spec.kind,
3169
+ installed: true,
3170
+ running: child.pid !== void 0,
3171
+ pid: child.pid ?? null,
3172
+ lastExitCode: null,
3173
+ logFile,
3174
+ label: spec.label ?? null
3175
+ };
3176
+ }
3177
+ async uninstall(id) {
3178
+ const pidFile2 = this.pidPath(id);
3179
+ const specFile = this.specPath(id);
3180
+ const existed = fsSync3.existsSync(pidFile2) || fsSync3.existsSync(specFile);
3181
+ try {
3182
+ const pid = await this.readPid(id);
3183
+ if (pid !== null) {
3184
+ try {
3185
+ process.kill(pid, "SIGTERM");
3186
+ } catch {
3187
+ }
3188
+ }
3189
+ } catch {
3190
+ }
3191
+ try {
3192
+ await fs4.unlink(pidFile2);
3193
+ } catch {
3194
+ }
3195
+ try {
3196
+ await fs4.unlink(specFile);
3197
+ } catch {
3198
+ }
3199
+ return existed;
3200
+ }
3201
+ async restart(id) {
3202
+ const specFile = this.specPath(id);
3203
+ if (!fsSync3.existsSync(specFile)) {
3204
+ throw new Error(`Fallback service '${id}' is not installed`);
3205
+ }
3206
+ const raw = JSON.parse(await fs4.readFile(specFile, "utf-8"));
3207
+ await this.install(raw.spec);
3208
+ }
3209
+ async status(id) {
3210
+ const specFile = this.specPath(id);
3211
+ if (!fsSync3.existsSync(specFile))
3212
+ return null;
3213
+ const pid = await this.readPid(id);
3214
+ const running = pid !== null && isAlive(pid);
3215
+ const logFile = this.logPath(id);
3216
+ let spec = null;
3217
+ try {
3218
+ const raw = JSON.parse(await fs4.readFile(specFile, "utf-8"));
3219
+ spec = raw.spec;
3220
+ } catch {
3221
+ }
3222
+ return {
3223
+ id,
3224
+ kind: spec?.kind ?? "unknown",
3225
+ installed: true,
3226
+ running,
3227
+ pid: running ? pid : null,
3228
+ lastExitCode: null,
3229
+ logFile,
3230
+ label: spec?.label ?? null
3231
+ };
3232
+ }
3233
+ async list() {
3234
+ let files = [];
3235
+ try {
3236
+ files = (await fs4.readdir(this.stateDir)).filter((f) => f.endsWith(".json"));
3237
+ } catch {
3238
+ return [];
3239
+ }
3240
+ const results = [];
3241
+ for (const f of files) {
3242
+ const id = path5.basename(f, ".json");
3243
+ const s = await this.status(id);
3244
+ if (s)
3245
+ results.push(s);
3246
+ }
3247
+ return results;
3248
+ }
3249
+ async selfCheck() {
3250
+ return {
3251
+ available: true,
3252
+ warnings: [
3253
+ "Using fallback process manager \u2014 services will NOT restart on crash or reboot."
3254
+ ],
3255
+ hints: [
3256
+ "Install a real service manager (systemd, launchd, or Task Scheduler) for production use."
3257
+ ]
3258
+ };
3259
+ }
3260
+ pidPath(id) {
3261
+ return path5.join(this.stateDir, `${safeId(id)}.pid`);
3262
+ }
3263
+ specPath(id) {
3264
+ return path5.join(this.stateDir, `${safeId(id)}.json`);
3265
+ }
3266
+ async readPid(id) {
3267
+ try {
3268
+ const raw = await fs4.readFile(this.pidPath(id), "utf-8");
3269
+ const pid = Number(raw.split("\n")[0]?.trim());
3270
+ return Number.isFinite(pid) && pid > 0 ? pid : null;
3271
+ } catch {
3272
+ return null;
3273
+ }
3274
+ }
3275
+ };
3276
+ }
3277
+ });
3278
+
3279
+ // ../core/dist/services/factory.js
3280
+ import { execFile as execFile4 } from "child_process";
3281
+ import { promisify as promisify4 } from "util";
3282
+ async function getServiceManager() {
3283
+ if (cached)
3284
+ return cached;
3285
+ if (process.platform === "darwin") {
3286
+ cached = new LaunchdServiceManager();
3287
+ return cached;
3288
+ }
3289
+ if (process.platform === "win32") {
3290
+ const mgr = new TaskSchedulerServiceManager();
3291
+ const check = await mgr.selfCheck();
3292
+ if (check.available) {
3293
+ cached = mgr;
3294
+ return cached;
3295
+ }
3296
+ fallbackReason = "schtasks.exe is not available";
3297
+ cached = new FallbackServiceManager();
3298
+ return cached;
3299
+ }
3300
+ if (process.platform === "linux") {
3301
+ try {
3302
+ await execFileP4("systemctl", ["--user", "--version"]);
3303
+ cached = new SystemdServiceManager();
3304
+ return cached;
3305
+ } catch {
3306
+ fallbackReason = "systemctl --user is not available (systemd-free distro?)";
3307
+ }
3308
+ } else {
3309
+ fallbackReason = `no native service manager for platform '${process.platform}'`;
3310
+ }
3311
+ cached = new FallbackServiceManager();
3312
+ return cached;
3313
+ }
3314
+ function activeFallbackReason() {
3315
+ return fallbackReason;
3316
+ }
3317
+ var execFileP4, cached, fallbackReason;
3318
+ var init_factory = __esm({
3319
+ "../core/dist/services/factory.js"() {
3320
+ "use strict";
3321
+ init_launchd();
3322
+ init_systemd();
3323
+ init_task_scheduler();
3324
+ init_fallback();
3325
+ execFileP4 = promisify4(execFile4);
3326
+ cached = null;
3327
+ fallbackReason = null;
3328
+ }
3329
+ });
3330
+
3331
+ // ../core/dist/services/migrate.js
3332
+ import fs5 from "fs/promises";
3333
+ import fsSync4 from "fs";
3334
+ import path6 from "path";
3335
+ import os4 from "os";
3336
+ import { execFile as execFile5 } from "child_process";
3337
+ import { promisify as promisify5 } from "util";
3338
+ async function migrateLegacySupervisor(mgr) {
3339
+ const home = beeoHome();
3340
+ const supervisorDir = path6.join(home, "supervisor");
3341
+ const stateFile = path6.join(supervisorDir, "state.json");
3342
+ const flagFile = path6.join(home, MIGRATION_FLAG);
3343
+ if (fsSync4.existsSync(flagFile)) {
3344
+ return { ran: false, migrated: 0, errors: [], backupPath: null };
3345
+ }
3346
+ if (!fsSync4.existsSync(stateFile)) {
3347
+ try {
3348
+ await fs5.writeFile(flagFile, (/* @__PURE__ */ new Date()).toISOString());
3349
+ } catch {
3350
+ }
3351
+ return { ran: false, migrated: 0, errors: [], backupPath: null };
3352
+ }
3353
+ const backupPath = `${stateFile}.migration-backup-${Date.now()}`;
3354
+ try {
3355
+ await fs5.copyFile(stateFile, backupPath);
3356
+ } catch {
3357
+ }
3358
+ let parsed;
3359
+ try {
3360
+ const raw = await fs5.readFile(stateFile, "utf-8");
3361
+ parsed = JSON.parse(raw);
3362
+ } catch (e) {
3363
+ return {
3364
+ ran: true,
3365
+ migrated: 0,
3366
+ errors: [{ id: "<state.json>", error: `unreadable: ${e}` }],
3367
+ backupPath
3368
+ };
3369
+ }
3370
+ const targets = parsed.targets ?? [];
3371
+ const errors = [];
3372
+ let migrated = 0;
3373
+ for (const t of targets) {
3374
+ const canonicalId = safeId(t.id);
3375
+ try {
3376
+ const spec = {
3377
+ id: canonicalId,
3378
+ kind: t.kind,
3379
+ command: t.command,
3380
+ args: t.args ?? [],
3381
+ env: t.env,
3382
+ cwd: t.cwd,
3383
+ restart: t.restart ?? "on-failure",
3384
+ label: t.label
3385
+ };
3386
+ await mgr.install(spec);
3387
+ migrated++;
3388
+ } catch (e) {
3389
+ errors.push({ id: canonicalId, error: e instanceof Error ? e.message : String(e) });
3390
+ }
3391
+ }
3392
+ await stopLegacyDaemon();
3393
+ if (errors.length === 0) {
3394
+ try {
3395
+ await fs5.writeFile(flagFile, (/* @__PURE__ */ new Date()).toISOString());
3396
+ } catch {
3397
+ }
3398
+ try {
3399
+ await fs5.rm(supervisorDir, { recursive: true, force: true });
3400
+ } catch {
3401
+ }
3402
+ }
3403
+ return { ran: true, migrated, errors, backupPath };
3404
+ }
3405
+ async function stopLegacyDaemon() {
3406
+ if (process.platform === "darwin") {
3407
+ const uid = process.getuid?.();
3408
+ if (uid !== void 0) {
3409
+ try {
3410
+ await execFileP5("launchctl", ["bootout", `gui/${uid}/${LEGACY_LABEL}`]);
3411
+ } catch {
3412
+ }
3413
+ }
3414
+ const legacyPlist = path6.join(os4.homedir(), "Library", "LaunchAgents", `${LEGACY_LABEL}.plist`);
3415
+ try {
3416
+ await fs5.unlink(legacyPlist);
3417
+ } catch {
3418
+ }
3419
+ }
3420
+ if (process.platform === "linux") {
3421
+ try {
3422
+ await execFileP5("systemctl", ["--user", "disable", "--now", LEGACY_SYSTEMD_UNIT]);
3423
+ } catch {
3424
+ }
3425
+ const legacyUnit = path6.join(os4.homedir(), ".config", "systemd", "user", LEGACY_SYSTEMD_UNIT);
3426
+ try {
3427
+ await fs5.unlink(legacyUnit);
3428
+ } catch {
3429
+ }
3430
+ }
3431
+ if (process.platform !== "win32") {
3432
+ try {
3433
+ await execFileP5("pkill", ["-f", "beeos-supervisor"]);
3434
+ } catch {
3435
+ }
3436
+ }
3437
+ }
3438
+ var execFileP5, LEGACY_LABEL, LEGACY_SYSTEMD_UNIT, MIGRATION_FLAG;
3439
+ var init_migrate = __esm({
3440
+ "../core/dist/services/migrate.js"() {
3441
+ "use strict";
3442
+ init_config();
3443
+ init_ids();
3444
+ execFileP5 = promisify5(execFile5);
3445
+ LEGACY_LABEL = "ai.beeos.supervisor";
3446
+ LEGACY_SYSTEMD_UNIT = "beeos-supervisor.service";
3447
+ MIGRATION_FLAG = "migrated-to-services.flag";
3448
+ }
3449
+ });
3450
+
3451
+ // ../core/dist/services/index.js
3452
+ var init_services = __esm({
3453
+ "../core/dist/services/index.js"() {
3454
+ "use strict";
3455
+ init_types2();
3456
+ init_ids();
3457
+ init_launchd();
3458
+ init_systemd();
3459
+ init_task_scheduler();
3460
+ init_fallback();
3461
+ init_factory();
3462
+ init_migrate();
3463
+ }
3464
+ });
3465
+
3466
+ // ../core/dist/index.js
3467
+ var init_dist = __esm({
3468
+ "../core/dist/index.js"() {
3469
+ "use strict";
3470
+ init_platform_adapter();
3471
+ init_progress();
3472
+ init_config();
3473
+ init_keypair();
3474
+ init_qr();
3475
+ init_client();
3476
+ init_npm();
3477
+ init_process();
3478
+ init_bind();
3479
+ init_device_setup();
3480
+ init_adb_setup();
3481
+ init_types();
3482
+ init_device();
3483
+ init_openclaw();
3484
+ init_scrcpy_bridge();
3485
+ init_vnc_bridge();
3486
+ init_cargo_dist();
3487
+ init_detector();
3488
+ init_downloader();
3489
+ init_launcher();
3490
+ init_registry();
3491
+ init_local_agent();
3492
+ init_detect();
3493
+ init_supervisor_spec();
3494
+ init_services();
3495
+ }
3496
+ });
3497
+
3498
+ // src/progress.ts
3499
+ import ora from "ora";
3500
+ var CliReporter;
3501
+ var init_progress2 = __esm({
3502
+ "src/progress.ts"() {
3503
+ "use strict";
3504
+ CliReporter = class {
3505
+ spinner;
3506
+ constructor() {
3507
+ this.spinner = ora({ stream: process.stderr });
3508
+ }
3509
+ onStatus(message) {
3510
+ if (this.spinner.isSpinning) {
3511
+ this.spinner.text = message;
3512
+ } else {
3513
+ this.spinner.start(message);
3514
+ }
3515
+ }
3516
+ onComplete(message) {
3517
+ if (this.spinner.isSpinning) {
3518
+ this.spinner.succeed(message);
3519
+ } else {
3520
+ console.log(`\u2713 ${message}`);
3521
+ }
3522
+ }
3523
+ stop() {
3524
+ if (this.spinner.isSpinning) {
3525
+ this.spinner.stop();
3526
+ }
3527
+ }
3528
+ };
3529
+ }
3530
+ });
3531
+
3532
+ // src/commands/fallback-banner.ts
3533
+ function maybePrintFallbackBanner(kind) {
3534
+ if (kind !== "fallback") return;
3535
+ if (printed) return;
3536
+ const reason = activeFallbackReason();
3537
+ if (!reason) return;
3538
+ printed = true;
3539
+ const bar = "!".repeat(70);
3540
+ console.log("");
3541
+ console.log(` ${bar}`);
3542
+ console.log(" !! WARNING: no native OS service manager available");
3543
+ console.log(` !! Reason : ${reason}`);
3544
+ console.log(" !! Impact : services will NOT restart on crash or reboot.");
3545
+ console.log(" !! Fix : use a distro with systemd --user (and run");
3546
+ console.log(" !! `loginctl enable-linger $USER` once), or");
3547
+ console.log(" !! launchd (macOS), or Task Scheduler (Windows).");
3548
+ console.log(` ${bar}`);
3549
+ console.log("");
3550
+ }
3551
+ var printed;
3552
+ var init_fallback_banner = __esm({
3553
+ "src/commands/fallback-banner.ts"() {
3554
+ "use strict";
3555
+ init_dist();
3556
+ printed = false;
3557
+ }
3558
+ });
3559
+
3560
+ // src/commands/device.ts
3561
+ var device_exports = {};
3562
+ __export(device_exports, {
3563
+ attach: () => attach,
3564
+ detach: () => detach,
3565
+ exec: () => exec,
3566
+ list: () => list,
3567
+ upgrade: () => upgrade
3568
+ });
3569
+ import lockfile from "proper-lockfile";
3570
+ function deviceAgentTargetId(serial) {
3571
+ return `device-agent-${serial}`;
3572
+ }
3573
+ function scrcpyTargetId(serial) {
3574
+ return `scrcpy-bridge-${serial}`;
3575
+ }
3576
+ function vncTargetId(serial) {
3577
+ return `vnc-bridge-${serial}`;
3578
+ }
3579
+ function devicesPath() {
3580
+ const p = getPlatformAdapter();
3581
+ return p.joinPath(beeoHome(), "devices.json");
3582
+ }
3583
+ async function loadDeviceState() {
3584
+ const p = getPlatformAdapter();
3585
+ const path9 = devicesPath();
3586
+ if (!await p.exists(path9)) return { devices: [] };
3587
+ try {
3588
+ const raw = await p.readFile(path9);
3589
+ return JSON.parse(raw);
3590
+ } catch {
3591
+ return { devices: [] };
3592
+ }
3593
+ }
3594
+ async function saveDeviceState(state) {
3595
+ const p = getPlatformAdapter();
3596
+ await p.writeFile(devicesPath(), JSON.stringify(state, null, 2));
3597
+ }
3598
+ async function withDeviceLock(fn) {
3599
+ const p = getPlatformAdapter();
3600
+ const path9 = devicesPath();
3601
+ await p.mkdir(p.dirname(path9));
3602
+ if (!await p.exists(path9)) {
3603
+ await p.writeFile(path9, JSON.stringify({ devices: [] }));
3604
+ }
3605
+ const release = await lockfile.lock(path9, { retries: 3 });
3606
+ try {
3607
+ return await fn();
3608
+ } finally {
3609
+ await release();
3610
+ }
3611
+ }
3612
+ async function ensureAdbAvailable(reporter) {
3613
+ const existing = await findAdb();
3614
+ if (existing) return;
3615
+ if (process.env.BEEOS_SKIP_ADB_INSTALL === "1") {
3616
+ throw new Error(
3617
+ "adb not found on PATH and BEEOS_SKIP_ADB_INSTALL=1 is set.\nInstall Android platform-tools manually and re-run, or unset the env var."
3618
+ );
3619
+ }
3620
+ console.log("adb not found \u2014 downloading Android platform-tools (one-time, ~10MB)...");
3621
+ try {
3622
+ await ensureAdb(reporter, { autoInstall: true });
3623
+ } catch (e) {
3624
+ throw new Error(
3625
+ "Failed to install adb automatically: " + String(e) + "\nWorkarounds:\n \u2022 macOS: brew install android-platform-tools\n \u2022 Linux: apt install android-tools-adb (or equivalent)\n \u2022 Windows: install Android SDK Platform-Tools from https://developer.android.com/tools/releases/platform-tools\n \u2022 Or point $BEEOS_ADB_BIN at an existing adb binary.",
3626
+ { cause: e }
3627
+ );
3628
+ }
3629
+ }
3630
+ async function installService(mgr, spec) {
3631
+ maybePrintFallbackBanner(mgr.kind);
3632
+ await mgr.install(spec);
3633
+ }
3634
+ async function removeTargetsForSerial(mgr, serial) {
3635
+ for (const id of [
3636
+ deviceAgentTargetId(serial),
3637
+ scrcpyTargetId(serial),
3638
+ vncTargetId(serial)
3639
+ ]) {
3640
+ try {
3641
+ await mgr.uninstall(id);
3642
+ } catch {
3643
+ }
3644
+ }
3645
+ }
3646
+ async function attach(options) {
3647
+ const cfg = await loadOrCreateConfig();
3648
+ const reporter = new CliReporter();
3649
+ const withVideo = options.video !== false;
3650
+ const isDesktopAttach = Boolean(options.vncHost);
3651
+ if (!isDesktopAttach) {
3652
+ await ensureAdbAvailable(reporter);
3653
+ }
3654
+ if (options.all) {
3655
+ return attachAll(cfg, reporter, withVideo, options);
3656
+ }
3657
+ const serial = options.serial ?? await deviceRuntime.detectSingleDevice();
3658
+ const name = options.name ?? serial;
3659
+ const agentBin = await deviceRuntime.ensureAgent(reporter);
3660
+ reporter.stop();
3661
+ const pubkeyB64 = await deviceRuntime.ensureKeyAndGetPubkey(serial);
3662
+ const instanceId = await tryBindDevice(pubkeyB64, name, cfg);
3663
+ if (!instanceId) {
3664
+ console.error("Bind was not completed \u2014 device-agent will not start without a platform binding.");
3665
+ console.error("Run `beeos device attach` again to retry.");
3666
+ return;
3667
+ }
3668
+ const mgr = await getServiceManager();
3669
+ await withDeviceLock(async () => {
3670
+ const state = await loadDeviceState();
3671
+ const existingIdx = state.devices.findIndex((d) => d.serial === serial);
3672
+ if (existingIdx >= 0) {
3673
+ await removeTargetsForSerial(mgr, serial);
3674
+ state.devices.splice(existingIdx, 1);
3675
+ }
3676
+ const httpPort = nextHttpPort(state, cfg.device.http_port);
3677
+ const keyFile = deviceRuntime.deviceKeyPath(serial);
3678
+ const agentGatewayUrl = resolveAgentGatewayUrl(cfg);
3679
+ const agentSpec = buildDeviceAgentTargetSpecFromBin(
3680
+ agentBin,
3681
+ {
3682
+ serial,
3683
+ bridgeUrl: cfg.platform.bridge_url,
3684
+ httpEnabled: cfg.device.http_enabled,
3685
+ httpPort,
3686
+ agentGatewayUrl,
3687
+ keyFile
3688
+ }
3689
+ );
3690
+ await installService(mgr, agentSpec);
3691
+ const bridgeInfo = await registerVideoBridge(mgr, reporter, {
3692
+ cfg,
3693
+ serial,
3694
+ instanceId,
3695
+ keyFile,
3696
+ withVideo,
3697
+ vncHost: options.vncHost,
3698
+ vncPort: options.vncPort,
3699
+ vncPassword: options.vncPassword
3700
+ });
3701
+ state.devices.push({
3702
+ serial,
3703
+ name,
3704
+ instance_id: instanceId,
3705
+ key_file: keyFile,
3706
+ http_port: httpPort,
3707
+ video_mode: bridgeInfo.mode,
3708
+ vnc_host: options.vncHost,
3709
+ vnc_port: options.vncPort
3710
+ });
3711
+ await saveDeviceState(state);
3712
+ const logPath = mgr.logPath(deviceAgentTargetId(serial));
3713
+ console.log(`Attached device ${serial} (${name}) \u2014 instance ${instanceId}`);
3714
+ console.log(` local http: :${httpPort} (if enabled)`);
3715
+ console.log(` logs: ${logPath}`);
3716
+ if (bridgeInfo.mode !== "none") {
3717
+ console.log(` video: ${bridgeInfo.mode}-bridge (managed by ${mgr.kind})`);
3718
+ } else {
3719
+ console.log(" video: disabled");
3720
+ }
3721
+ });
3722
+ }
3723
+ async function attachAll(cfg, reporter, withVideo, options) {
3724
+ const devices = await deviceRuntime.listAdbDevices();
3725
+ if (devices.length === 0) {
3726
+ console.log("No ADB devices found");
3727
+ return;
3728
+ }
3729
+ const agentBin = await deviceRuntime.ensureAgent(reporter);
3730
+ reporter.stop();
3731
+ const agentGatewayUrl = resolveAgentGatewayUrl(cfg);
3732
+ const mgr = await getServiceManager();
3733
+ const binds = [];
3734
+ for (const device of devices) {
3735
+ try {
3736
+ const pubkeyB64 = await deviceRuntime.ensureKeyAndGetPubkey(device.serial);
3737
+ const instanceId = await tryBindDevice(pubkeyB64, device.serial, cfg);
3738
+ if (!instanceId) {
3739
+ console.error(` Skipping ${device.serial} \u2014 bind not completed`);
3740
+ continue;
3741
+ }
3742
+ binds.push({
3743
+ serial: device.serial,
3744
+ instanceId,
3745
+ keyFile: deviceRuntime.deviceKeyPath(device.serial)
3746
+ });
3747
+ } catch (e) {
3748
+ console.error(` Failed to bind ${device.serial}: ${e}`);
3749
+ }
3750
+ }
3751
+ if (binds.length === 0) {
3752
+ console.log("No devices attached \u2014 all binds failed or were skipped.");
3753
+ return;
3754
+ }
3755
+ await withDeviceLock(async () => {
3756
+ const state = await loadDeviceState();
3757
+ const alreadyAttached = new Set(state.devices.map((d) => d.serial));
3758
+ await Promise.all(
3759
+ binds.map(async ({ serial, instanceId, keyFile }) => {
3760
+ try {
3761
+ const existingIdx = state.devices.findIndex((d) => d.serial === serial);
3762
+ if (existingIdx >= 0) {
3763
+ await removeTargetsForSerial(mgr, serial);
3764
+ state.devices.splice(existingIdx, 1);
3765
+ }
3766
+ const httpPort = nextHttpPort(state, cfg.device.http_port);
3767
+ const agentSpec = buildDeviceAgentTargetSpecFromBin(agentBin, {
3768
+ serial,
3769
+ bridgeUrl: cfg.platform.bridge_url,
3770
+ httpEnabled: cfg.device.http_enabled,
3771
+ httpPort,
3772
+ agentGatewayUrl,
3773
+ keyFile
3774
+ });
3775
+ await installService(mgr, agentSpec);
3776
+ const bridgeInfo = await registerVideoBridge(mgr, reporter, {
3777
+ cfg,
3778
+ serial,
3779
+ instanceId,
3780
+ keyFile,
3781
+ withVideo,
3782
+ vncHost: options.vncHost,
3783
+ vncPort: options.vncPort,
3784
+ vncPassword: options.vncPassword
3785
+ });
3786
+ state.devices.push({
3787
+ serial,
3788
+ name: serial,
3789
+ instance_id: instanceId,
3790
+ key_file: keyFile,
3791
+ http_port: httpPort,
3792
+ video_mode: bridgeInfo.mode,
3793
+ vnc_host: options.vncHost,
3794
+ vnc_port: options.vncPort
3795
+ });
3796
+ alreadyAttached.add(serial);
3797
+ console.log(
3798
+ `Attached device ${serial} \u2014 instance ${instanceId}` + (bridgeInfo.mode !== "none" ? ` (video: ${bridgeInfo.mode})` : "")
3799
+ );
3800
+ } catch (e) {
3801
+ console.error(`Failed to attach ${serial}: ${e}`);
3802
+ }
3803
+ })
3804
+ );
3805
+ await saveDeviceState(state);
3806
+ console.log(`Attached ${binds.length} device(s) via ${mgr.kind}.`);
3807
+ });
3808
+ }
3809
+ function buildDeviceAgentTargetSpecFromBin(agentBin, input) {
3810
+ if (!input.bridgeUrl) {
3811
+ throw new Error(
3812
+ "bridge_url is not configured in ~/.beeos/config.toml.\nSet [platform] bridge_url (e.g. wss://bridge.beeos.ai)."
3813
+ );
3814
+ }
3815
+ const { cmd, args: baseArgs } = agentBinCommandAndArgs(agentBin);
3816
+ return buildDeviceAgentTargetSpec({
3817
+ serial: input.serial,
3818
+ command: cmd,
3819
+ args: baseArgs,
3820
+ bridgeUrl: input.bridgeUrl,
3821
+ keyFile: input.keyFile,
3822
+ httpEnabled: input.httpEnabled,
3823
+ httpPort: input.httpPort,
3824
+ agentGatewayUrl: input.agentGatewayUrl
3825
+ });
3826
+ }
3827
+ async function registerVideoBridge(mgr, reporter, params) {
3828
+ if (!params.withVideo) return { mode: "none" };
3829
+ const agentGatewayUrl = resolveAgentGatewayUrl(params.cfg);
3830
+ if (params.vncHost) {
3831
+ const binary2 = await vncBridgeRuntime.ensureInstalled(reporter);
3832
+ if (!binary2) {
3833
+ console.error(
3834
+ ` vnc-bridge binary unavailable \u2014 ${params.serial} will run without VNC streaming.`
3835
+ );
3836
+ return { mode: "none" };
3837
+ }
3838
+ const spec2 = buildVncBridgeTargetSpec(binary2, {
3839
+ deviceId: `device-${params.instanceId}`,
3840
+ vncHost: params.vncHost,
3841
+ vncPort: params.vncPort ?? 5900,
3842
+ ...params.vncPassword ? { vncPassword: params.vncPassword } : {},
3843
+ agentGatewayUrl,
3844
+ bridgePrivateKeyFile: params.keyFile,
3845
+ bridgePublicKeyFile: params.keyFile
3846
+ });
3847
+ await installService(mgr, spec2);
3848
+ return { mode: "vnc" };
3849
+ }
3850
+ const binary = await scrcpyBridgeRuntime.ensureInstalled(reporter);
3851
+ if (!binary) {
3852
+ console.error(
3853
+ ` scrcpy-bridge binary unavailable \u2014 ${params.serial} will run without WebRTC video.
3854
+ Install from https://github.com/beeos-ai/scrcpy-bridge or set BEEOS_SCRCPY_BRIDGE_BIN.`
3855
+ );
3856
+ return { mode: "none" };
3857
+ }
3858
+ const spec = buildScrcpyBridgeTargetSpec(binary, {
3859
+ deviceId: `device-${params.instanceId}`,
3860
+ adbSerial: params.serial,
3861
+ agentGatewayUrl,
3862
+ bridgePrivateKeyFile: params.keyFile,
3863
+ bridgePublicKeyFile: params.keyFile
3864
+ });
3865
+ await installService(mgr, spec);
3866
+ return { mode: "scrcpy" };
3867
+ }
3868
+ async function detach(options) {
3869
+ const cfg = await loadOrCreateConfig();
3870
+ const mgr = await getServiceManager();
3871
+ await withDeviceLock(async () => {
3872
+ const state = await loadDeviceState();
3873
+ if (options.all) {
3874
+ for (const entry2 of state.devices) {
3875
+ await removeTargetsForSerial(mgr, entry2.serial);
3876
+ legacyKillDeviceEntry(entry2);
3877
+ await notifyOffline(cfg.platform.api_url, entry2.instance_id);
3878
+ }
3879
+ const count = state.devices.length;
3880
+ state.devices = [];
3881
+ await saveDeviceState(state);
3882
+ console.log(`Detached ${count} device(s)`);
3883
+ return;
3884
+ }
3885
+ if (!options.serial) {
3886
+ throw new Error("Specify --serial or use --all");
3887
+ }
3888
+ const idx = state.devices.findIndex((d) => d.serial === options.serial);
3889
+ if (idx < 0) throw new Error(`Device ${options.serial} not found`);
3890
+ const entry = state.devices.splice(idx, 1)[0];
3891
+ await removeTargetsForSerial(mgr, entry.serial);
3892
+ legacyKillDeviceEntry(entry);
3893
+ await notifyOffline(cfg.platform.api_url, entry.instance_id);
3894
+ await saveDeviceState(state);
3895
+ console.log(`Detached device ${options.serial}`);
3896
+ });
3897
+ }
3898
+ function legacyKillDeviceEntry(entry) {
3899
+ if (entry.pid && isProcessAlive(entry.pid)) killProcess(entry.pid);
3900
+ if (entry.bridge_pid && isProcessAlive(entry.bridge_pid)) killProcess(entry.bridge_pid);
3901
+ }
3902
+ async function notifyOffline(apiUrl, instanceId) {
3903
+ if (!instanceId) return;
3904
+ try {
3905
+ await markInstanceOffline(apiUrl, instanceId);
3906
+ } catch {
3907
+ }
3908
+ }
3909
+ async function list(options) {
3910
+ if (!options.local) {
3911
+ console.log("Remote device listing requires platform API (use --local for local devices)");
3912
+ return;
3913
+ }
3914
+ const state = await loadDeviceState();
3915
+ if (state.devices.length === 0) {
3916
+ console.log("No attached devices");
3917
+ return;
3918
+ }
3919
+ const mgr = await getServiceManager();
3920
+ const statusById = /* @__PURE__ */ new Map();
3921
+ try {
3922
+ const services = await mgr.list();
3923
+ for (const s of services) {
3924
+ statusById.set(s.id, s.running ? "running" : "stopped");
3925
+ }
3926
+ } catch {
3927
+ }
3928
+ console.log(
3929
+ `${"SERIAL".padEnd(16)} ${"NAME".padEnd(20)} ${"STATUS".padEnd(12)} ${"HTTP".padEnd(6)} ${"VIDEO".padEnd(10)} INSTANCE`
3930
+ );
3931
+ for (const entry of state.devices) {
3932
+ const agentState = statusById.get(deviceAgentTargetId(entry.serial)) ?? "unknown";
3933
+ const videoId = entry.video_mode === "vnc" ? vncTargetId(entry.serial) : scrcpyTargetId(entry.serial);
3934
+ const videoState = entry.video_mode === "none" ? "disabled" : statusById.get(videoId) ?? "unknown";
3935
+ console.log(
3936
+ `${entry.serial.padEnd(16)} ${entry.name.padEnd(20)} ${agentState.padEnd(
3937
+ 12
3938
+ )} ${String(entry.http_port).padEnd(6)} ${videoState.padEnd(10)} ${entry.instance_id ?? "-"}`
3939
+ );
3940
+ }
3941
+ }
3942
+ async function exec(prompt2, options) {
3943
+ const state = await loadDeviceState();
3944
+ const entry = options.serial ? state.devices.find((d) => d.serial === options.serial) : state.devices[0];
3945
+ if (!entry) {
3946
+ throw new Error(options.serial ? `Device ${options.serial} not attached` : "No attached devices");
3947
+ }
3948
+ if (!entry.http_port) {
3949
+ throw new Error(
3950
+ `Device ${entry.serial} has no local HTTP port configured.
3951
+ Enable http_enabled in ~/.beeos/config.toml and re-attach.`
3952
+ );
3953
+ }
3954
+ const p = getPlatformAdapter();
3955
+ const url = `http://127.0.0.1:${entry.http_port}/prompt`;
3956
+ console.log(`-> ${entry.serial} (local :${entry.http_port}): ${prompt2}`);
3957
+ const resp = await p.fetch(url, {
3958
+ method: "POST",
3959
+ headers: { "Content-Type": "application/json" },
3960
+ body: JSON.stringify({ prompt: prompt2 })
3961
+ });
3962
+ if (!resp.ok) {
3963
+ const body2 = await resp.text().catch(() => "");
3964
+ throw new Error(`device-agent returned ${resp.status} \u2014 ${body2}`);
3965
+ }
3966
+ const body = await resp.json();
3967
+ console.log(JSON.stringify(body, null, 2));
3968
+ }
3969
+ async function upgrade(options = {}) {
3970
+ const reporter = new CliReporter();
3971
+ await deviceRuntime.upgradeAgent(reporter);
3972
+ if (options.bridges !== false) {
3973
+ try {
3974
+ await scrcpyBridgeRuntime.upgrade(reporter);
3975
+ } catch (e) {
3976
+ console.error(`scrcpy-bridge upgrade skipped: ${e}`);
3977
+ }
3978
+ try {
3979
+ await vncBridgeRuntime.upgrade(reporter);
3980
+ } catch (e) {
3981
+ console.error(`vnc-bridge upgrade skipped: ${e}`);
3982
+ }
3983
+ }
3984
+ }
3985
+ async function tryBindDevice(pubkeyB64, name, cfg) {
3986
+ const fp = fingerprintFromB64(pubkeyB64);
3987
+ try {
3988
+ const resp = await agentBind(
3989
+ cfg.platform.api_url,
3990
+ pubkeyB64,
3991
+ fp,
3992
+ "device",
3993
+ name
3994
+ );
3995
+ if (resp.status === "bound") {
3996
+ console.log(` Already bound \u2014 instance: ${resp.instance_id}`);
3997
+ return resp.instance_id ?? void 0;
3998
+ }
3999
+ if (resp.bind_id) {
4000
+ const bindUrl = buildBindUrl(cfg.platform.dashboard_base_url, resp.bind_id);
4001
+ await presentBindUrl(bindUrl, false);
4002
+ console.log("");
4003
+ console.log(" Waiting for bind approval (timeout: 10min)...");
4004
+ try {
4005
+ const instanceId = await pollUntilBound(
4006
+ cfg.platform.api_url,
4007
+ resp.bind_id,
4008
+ 6e5
4009
+ );
4010
+ console.log(` Bind confirmed! Instance: ${instanceId}`);
4011
+ return instanceId;
4012
+ } catch (e) {
4013
+ console.error(` Bind polling stopped: ${e}`);
4014
+ }
4015
+ }
4016
+ return void 0;
4017
+ } catch (e) {
4018
+ console.error(` Platform bind failed: ${e}`);
4019
+ return void 0;
4020
+ }
4021
+ }
4022
+ function nextHttpPort(state, base) {
4023
+ const used = new Set(state.devices.map((d) => d.http_port));
4024
+ let port = base;
4025
+ while (used.has(port)) port++;
4026
+ return port;
4027
+ }
4028
+ var init_device2 = __esm({
4029
+ "src/commands/device.ts"() {
4030
+ "use strict";
4031
+ init_dist();
4032
+ init_progress2();
4033
+ init_fallback_banner();
4034
+ }
4035
+ });
4036
+
4037
+ // src/index.ts
4038
+ init_dist();
4039
+ import { Command } from "commander";
4040
+
4041
+ // src/node-adapter.ts
4042
+ import {
4043
+ execFile as execFile6,
4044
+ spawn as nodeSpawn
4045
+ } from "child_process";
4046
+ import fs6 from "fs";
4047
+ import fsp from "fs/promises";
4048
+ import net from "net";
4049
+ import os5 from "os";
4050
+ import path7 from "path";
4051
+ var NodePlatformAdapter = class {
4052
+ // ── Filesystem ──────────────────────────────────────────
4053
+ async readFile(filePath) {
4054
+ return fsp.readFile(filePath, "utf-8");
4055
+ }
4056
+ async readFileBytes(filePath) {
4057
+ const buf = await fsp.readFile(filePath);
4058
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
4059
+ }
4060
+ async writeFile(filePath, content) {
4061
+ await fsp.writeFile(filePath, content, "utf-8");
1419
4062
  }
1420
4063
  async writeFileBytes(filePath, content) {
1421
4064
  await fsp.writeFile(filePath, content);
@@ -1478,7 +4121,7 @@ var NodePlatformAdapter = class {
1478
4121
  exec(cmd, args, options) {
1479
4122
  return new Promise((resolve) => {
1480
4123
  const env = options?.env ? { ...process.env, ...options.env } : process.env;
1481
- execFile(
4124
+ execFile6(
1482
4125
  cmd,
1483
4126
  args,
1484
4127
  {
@@ -1506,12 +4149,12 @@ var NodePlatformAdapter = class {
1506
4149
  let stdoutFd;
1507
4150
  let stderrFd;
1508
4151
  if (options?.stdoutFile) {
1509
- await fsp.mkdir(path.dirname(options.stdoutFile), { recursive: true });
1510
- stdoutFd = fs.openSync(options.stdoutFile, "a");
4152
+ await fsp.mkdir(path7.dirname(options.stdoutFile), { recursive: true });
4153
+ stdoutFd = fs6.openSync(options.stdoutFile, "a");
1511
4154
  }
1512
4155
  if (options?.stderrFile) {
1513
- await fsp.mkdir(path.dirname(options.stderrFile), { recursive: true });
1514
- stderrFd = options.stderrFile === options.stdoutFile ? stdoutFd : fs.openSync(options.stderrFile, "a");
4156
+ await fsp.mkdir(path7.dirname(options.stderrFile), { recursive: true });
4157
+ stderrFd = options.stderrFile === options.stdoutFile ? stdoutFd : fs6.openSync(options.stderrFile, "a");
1515
4158
  }
1516
4159
  const child = nodeSpawn(cmd, args, {
1517
4160
  cwd: options?.cwd,
@@ -1528,8 +4171,8 @@ var NodePlatformAdapter = class {
1528
4171
  if (options?.detached) {
1529
4172
  child.unref();
1530
4173
  }
1531
- if (stdoutFd != null) fs.closeSync(stdoutFd);
1532
- if (stderrFd != null && stderrFd !== stdoutFd) fs.closeSync(stderrFd);
4174
+ if (stdoutFd != null) fs6.closeSync(stdoutFd);
4175
+ if (stderrFd != null && stderrFd !== stdoutFd) fs6.closeSync(stderrFd);
1533
4176
  const pid = child.pid;
1534
4177
  if (pid == null) {
1535
4178
  throw new Error(`Failed to spawn process: ${cmd} ${args.join(" ")}`);
@@ -1575,10 +4218,10 @@ var NodePlatformAdapter = class {
1575
4218
  }
1576
4219
  // ── OS info ─────────────────────────────────────────────
1577
4220
  homeDir() {
1578
- return os.homedir();
4221
+ return os5.homedir();
1579
4222
  }
1580
4223
  hostname() {
1581
- return os.hostname();
4224
+ return os5.hostname();
1582
4225
  }
1583
4226
  platform() {
1584
4227
  return process.platform;
@@ -1596,48 +4239,21 @@ var NodePlatformAdapter = class {
1596
4239
  }
1597
4240
  // ── Path utilities ──────────────────────────────────────
1598
4241
  joinPath(...segments) {
1599
- return path.join(...segments);
4242
+ return path7.join(...segments);
1600
4243
  }
1601
4244
  dirname(p) {
1602
- return path.dirname(p);
4245
+ return path7.dirname(p);
1603
4246
  }
1604
4247
  basename(p) {
1605
- return path.basename(p);
1606
- }
1607
- };
1608
-
1609
- // src/commands/start.ts
1610
- import os2 from "os";
1611
-
1612
- // src/progress.ts
1613
- import ora from "ora";
1614
- var CliReporter = class {
1615
- spinner;
1616
- constructor() {
1617
- this.spinner = ora({ stream: process.stderr });
1618
- }
1619
- onStatus(message) {
1620
- if (this.spinner.isSpinning) {
1621
- this.spinner.text = message;
1622
- } else {
1623
- this.spinner.start(message);
1624
- }
1625
- }
1626
- onComplete(message) {
1627
- if (this.spinner.isSpinning) {
1628
- this.spinner.succeed(message);
1629
- } else {
1630
- console.log(`\u2713 ${message}`);
1631
- }
1632
- }
1633
- stop() {
1634
- if (this.spinner.isSpinning) {
1635
- this.spinner.stop();
1636
- }
4248
+ return path7.basename(p);
1637
4249
  }
1638
4250
  };
1639
4251
 
1640
4252
  // src/commands/start.ts
4253
+ init_dist();
4254
+ init_progress2();
4255
+ init_fallback_banner();
4256
+ import os6 from "os";
1641
4257
  async function run(agentFramework, options) {
1642
4258
  const p = getPlatformAdapter();
1643
4259
  await ensureDirs();
@@ -1675,7 +4291,7 @@ async function run(agentFramework, options) {
1675
4291
  effectiveToken = systemToken ?? gwToken;
1676
4292
  await backupOpenclawConfig(home);
1677
4293
  if (await isGatewayRunning()) {
1678
- const ctx = {
4294
+ const ctx2 = {
1679
4295
  agentHome: home,
1680
4296
  agentBinary: binary,
1681
4297
  keyFile,
@@ -1685,11 +4301,11 @@ async function run(agentFramework, options) {
1685
4301
  isSystemHome: true
1686
4302
  };
1687
4303
  try {
1688
- await configurePluginViaCli(ctx);
4304
+ await configurePluginViaCli(ctx2);
1689
4305
  } catch {
1690
4306
  }
1691
4307
  try {
1692
- await restartGatewayViaCli(ctx);
4308
+ await restartGatewayViaCli(ctx2);
1693
4309
  } catch {
1694
4310
  }
1695
4311
  }
@@ -1704,27 +4320,25 @@ async function run(agentFramework, options) {
1704
4320
  binary = managedBin;
1705
4321
  }
1706
4322
  reporter.stop();
4323
+ const ctx = {
4324
+ agentHome: home,
4325
+ agentBinary: binary,
4326
+ keyFile,
4327
+ bridgeUrl: cfg.platform.bridge_url,
4328
+ agentGatewayUrl: cfg.platform.api_url,
4329
+ gatewayToken: effectiveToken,
4330
+ isSystemHome
4331
+ };
1707
4332
  let gatewayPid;
1708
4333
  if (!await isGatewayRunning()) {
1709
- const ctx = {
1710
- agentHome: home,
1711
- agentBinary: binary,
1712
- keyFile,
1713
- bridgeUrl: cfg.platform.bridge_url,
1714
- agentGatewayUrl: cfg.platform.api_url,
1715
- gatewayToken: effectiveToken,
1716
- isSystemHome
1717
- };
1718
4334
  if (isSystemHome) {
1719
4335
  await configurePluginViaCli(ctx);
1720
4336
  } else {
1721
4337
  await generateOpenclawConfig(ctx);
1722
4338
  }
1723
- const pid = await spawnOpenclaw(ctx);
1724
- await writePid(agentFramework, pid);
1725
- gatewayPid = pid;
4339
+ gatewayPid = await installOpenclawService(ctx);
1726
4340
  let alive = false;
1727
- for (let i = 0; i < 10; i++) {
4341
+ for (let i = 0; i < 20; i++) {
1728
4342
  await sleep2(500);
1729
4343
  if (await isGatewayRunning()) {
1730
4344
  alive = true;
@@ -1732,10 +4346,15 @@ async function run(agentFramework, options) {
1732
4346
  }
1733
4347
  }
1734
4348
  if (!alive) {
1735
- throw new Error("Agent gateway did not start within 5s");
4349
+ throw new Error(
4350
+ "Agent gateway did not start within 10s. Check the service logs: `beeos doctor` or ~/.beeos/logs/services/openclaw.log"
4351
+ );
1736
4352
  }
1737
4353
  } else {
1738
- gatewayPid = await readPid(agentFramework) ?? void 0;
4354
+ try {
4355
+ gatewayPid = await installOpenclawService(ctx);
4356
+ } catch {
4357
+ }
1739
4358
  }
1740
4359
  const hostname = buildHostname();
1741
4360
  try {
@@ -1758,33 +4377,28 @@ async function run(agentFramework, options) {
1758
4377
  bridge_url: cfg.platform.bridge_url,
1759
4378
  instance_id: result.instanceId
1760
4379
  };
1761
- if (options.json) {
1762
- console.log(JSON.stringify(out, null, 2));
1763
- } else {
1764
- console.log(`Agent bound to instance: ${result.instanceId}`);
1765
- }
4380
+ if (options.json) console.log(JSON.stringify(out, null, 2));
4381
+ else console.log(`Agent bound to instance: ${result.instanceId}`);
1766
4382
  return;
1767
4383
  }
1768
- if (result.status === "pending") {
1769
- if (options.json) {
1770
- const out = {
1771
- status: "pending",
1772
- public_key: pubkey,
1773
- fingerprint: fp,
1774
- bind_id: result.bindId,
1775
- bind_url: result.bindUrl,
1776
- qr_data: result.bindUrl,
1777
- gateway_url: "http://127.0.0.1:18789",
1778
- gateway_pid: gatewayPid,
1779
- bridge_url: cfg.platform.bridge_url
1780
- };
1781
- console.log(JSON.stringify(out, null, 2));
1782
- return;
1783
- }
4384
+ if (result.status === "pending" && options.json) {
4385
+ const out = {
4386
+ status: "pending",
4387
+ public_key: pubkey,
4388
+ fingerprint: fp,
4389
+ bind_id: result.bindId,
4390
+ bind_url: result.bindUrl,
4391
+ qr_data: result.bindUrl,
4392
+ gateway_url: "http://127.0.0.1:18789",
4393
+ gateway_pid: gatewayPid,
4394
+ bridge_url: cfg.platform.bridge_url
4395
+ };
4396
+ console.log(JSON.stringify(out, null, 2));
4397
+ return;
1784
4398
  }
1785
4399
  } catch (e) {
1786
- const cached = await loadBindingInfo();
1787
- if (cached && cached.fingerprint === fp) {
4400
+ const cached2 = await loadBindingInfo();
4401
+ if (cached2 && cached2.fingerprint === fp) {
1788
4402
  if (options.json) {
1789
4403
  console.log(JSON.stringify({
1790
4404
  status: "bound_offline",
@@ -1793,18 +4407,31 @@ async function run(agentFramework, options) {
1793
4407
  gateway_url: "http://127.0.0.1:18789",
1794
4408
  gateway_pid: gatewayPid,
1795
4409
  bridge_url: cfg.platform.bridge_url,
1796
- instance_id: cached.instance_id
4410
+ instance_id: cached2.instance_id
1797
4411
  }, null, 2));
1798
4412
  } else {
1799
- console.log(`Agent running (offline, cached instance: ${cached.instance_id})`);
4413
+ console.log(`Agent running (offline, cached instance: ${cached2.instance_id})`);
1800
4414
  }
1801
4415
  return;
1802
4416
  }
1803
- throw new Error(`Cannot reach platform to bind agent: ${e}`);
4417
+ throw new Error(`Cannot reach platform to bind agent: ${e}`, { cause: e });
4418
+ }
4419
+ }
4420
+ async function installOpenclawService(ctx) {
4421
+ const mgr = await getServiceManager();
4422
+ maybePrintFallbackBanner(mgr.kind);
4423
+ const spec = buildOpenclawTargetSpec(ctx);
4424
+ const status2 = await mgr.install(spec);
4425
+ if (status2.pid) {
4426
+ try {
4427
+ await writePid("openclaw", status2.pid);
4428
+ } catch {
4429
+ }
1804
4430
  }
4431
+ return status2.pid ?? void 0;
1805
4432
  }
1806
4433
  function buildHostname() {
1807
- const machine = os2.hostname();
4434
+ const machine = os6.hostname();
1808
4435
  const user = process.env.USER || process.env.USERNAME || "";
1809
4436
  return user ? `${user}@${machine}` : machine;
1810
4437
  }
@@ -1813,18 +4440,37 @@ function sleep2(ms) {
1813
4440
  }
1814
4441
 
1815
4442
  // src/commands/stop.ts
4443
+ init_dist();
1816
4444
  async function run2(agentFramework) {
4445
+ const mgr = await getServiceManager();
4446
+ let handled = false;
4447
+ try {
4448
+ const status2 = await mgr.status(agentFramework);
4449
+ if (status2?.installed) {
4450
+ await mgr.uninstall(agentFramework);
4451
+ console.log(
4452
+ `Stopped ${agentFramework} agent${status2.pid ? ` (pid ${status2.pid})` : ""}`
4453
+ );
4454
+ handled = true;
4455
+ }
4456
+ } catch (err) {
4457
+ const msg = err instanceof Error ? err.message : String(err);
4458
+ console.log(`Service stop failed: ${msg} \u2014 falling back to legacy PID kill`);
4459
+ }
1817
4460
  const pid = await readPid(agentFramework);
1818
4461
  if (pid == null) {
1819
- console.log(`No running ${agentFramework} agent found`);
4462
+ if (!handled) console.log(`No running ${agentFramework} agent found`);
4463
+ await removePid(agentFramework).catch(() => {
4464
+ });
1820
4465
  return;
1821
4466
  }
1822
4467
  killProcess(pid);
1823
4468
  await removePid(agentFramework);
1824
- console.log(`Stopped ${agentFramework} agent (pid ${pid})`);
4469
+ if (!handled) console.log(`Stopped ${agentFramework} agent (pid ${pid})`);
1825
4470
  }
1826
4471
 
1827
4472
  // src/commands/status.ts
4473
+ init_dist();
1828
4474
  var KNOWN_RUNTIMES = ["openclaw", "device"];
1829
4475
  async function run3() {
1830
4476
  const home = beeoHome();
@@ -1842,10 +4488,27 @@ async function run3() {
1842
4488
  console.log("Identity: not yet created");
1843
4489
  }
1844
4490
  console.log("");
4491
+ const mgr = await getServiceManager();
4492
+ const reason = activeFallbackReason();
4493
+ console.log(`Service manager: ${mgr.kind}${reason ? ` (${reason})` : ""}`);
4494
+ let services = [];
4495
+ try {
4496
+ services = await mgr.list();
4497
+ } catch {
4498
+ services = [];
4499
+ }
4500
+ if (services.length === 0) {
4501
+ console.log(" (no services registered)");
4502
+ } else {
4503
+ for (const s of services) console.log(` ${formatService(s)}`);
4504
+ }
4505
+ console.log("");
4506
+ const knownIds = new Set(services.map((s) => s.id));
1845
4507
  for (const rt of KNOWN_RUNTIMES) {
4508
+ if (knownIds.has(rt)) continue;
1846
4509
  const pid = await readPid(rt);
1847
4510
  if (pid != null && isProcessAlive(pid)) {
1848
- console.log(`${rt}: running (pid ${pid})`);
4511
+ console.log(`${rt}: running (pid ${pid}, legacy)`);
1849
4512
  } else if (pid != null) {
1850
4513
  console.log(`${rt}: stale pid ${pid} (process not alive)`);
1851
4514
  } else {
@@ -1853,8 +4516,16 @@ async function run3() {
1853
4516
  }
1854
4517
  }
1855
4518
  }
4519
+ function formatService(s) {
4520
+ const state = s.running ? `\x1B[32mrunning\x1B[0m` : s.installed ? `\x1B[33mstopped\x1B[0m` : `\x1B[31muninstalled\x1B[0m`;
4521
+ const pid = s.pid != null ? ` pid=${s.pid}` : "";
4522
+ const exit = s.lastExitCode != null && s.lastExitCode !== 0 ? ` last_exit=${s.lastExitCode}` : "";
4523
+ return `${s.id.padEnd(28)} ${state}${pid}${exit}`;
4524
+ }
1856
4525
 
1857
4526
  // src/commands/update.ts
4527
+ init_dist();
4528
+ init_progress2();
1858
4529
  async function run4(agentFramework) {
1859
4530
  const reporter = new CliReporter();
1860
4531
  const pid = await readPid(agentFramework);
@@ -1878,6 +4549,7 @@ async function run4(agentFramework) {
1878
4549
  }
1879
4550
 
1880
4551
  // src/commands/bind-status.ts
4552
+ init_dist();
1881
4553
  async function run5(bindId, options) {
1882
4554
  const cfg = await loadOrCreateConfig();
1883
4555
  const resp = await getBindStatus(cfg.platform.api_url, bindId);
@@ -1911,320 +4583,483 @@ async function run5(bindId, options) {
1911
4583
  }
1912
4584
  }
1913
4585
 
1914
- // src/commands/device.ts
1915
- import lockfile from "proper-lockfile";
1916
- function devicesPath() {
1917
- const p = getPlatformAdapter();
1918
- return p.joinPath(beeoHome(), "devices.json");
1919
- }
1920
- async function loadDeviceState() {
4586
+ // src/index.ts
4587
+ init_device2();
4588
+
4589
+ // src/commands/init.ts
4590
+ init_dist();
4591
+ import readline from "readline";
4592
+
4593
+ // src/commands/doctor.ts
4594
+ init_dist();
4595
+ import fs7 from "fs/promises";
4596
+ import path8 from "path";
4597
+ var LOG_SIZE_WARN_BYTES = 50 * 1024 * 1024;
4598
+ async function run6(options) {
4599
+ const state = await detectExistingInstall();
4600
+ const cfg = await loadOrCreateConfig();
1921
4601
  const p = getPlatformAdapter();
1922
- const path2 = devicesPath();
1923
- if (!await p.exists(path2)) return { devices: [] };
4602
+ const mgr = await getServiceManager();
4603
+ const fallbackReason2 = activeFallbackReason();
4604
+ const serviceCheck = await mgr.selfCheck();
1924
4605
  try {
1925
- const raw = await p.readFile(path2);
1926
- return JSON.parse(raw);
4606
+ await migrateLegacySupervisor(mgr);
1927
4607
  } catch {
1928
- return { devices: [] };
1929
- }
1930
- }
1931
- async function saveDeviceState(state) {
1932
- const p = getPlatformAdapter();
1933
- await p.writeFile(devicesPath(), JSON.stringify(state, null, 2));
1934
- }
1935
- async function withDeviceLock(fn) {
1936
- const p = getPlatformAdapter();
1937
- const path2 = devicesPath();
1938
- await p.mkdir(p.dirname(path2));
1939
- if (!await p.exists(path2)) {
1940
- await p.writeFile(path2, JSON.stringify({ devices: [] }));
1941
4608
  }
1942
- const release = await lockfile.lock(path2, { retries: 3 });
4609
+ let services = [];
1943
4610
  try {
1944
- return await fn();
1945
- } finally {
1946
- await release();
4611
+ services = await mgr.list();
4612
+ } catch {
4613
+ }
4614
+ const tools = await collectToolStatus(p);
4615
+ const warnings = [];
4616
+ const hints = [];
4617
+ if (!state.hasIdentity) {
4618
+ warnings.push("identity keypair missing \u2014 will be generated on next bind");
1947
4619
  }
1948
- }
1949
- async function attach(options) {
1950
- const cfg = await loadOrCreateConfig();
1951
- const reporter = new CliReporter();
1952
- if (options.all) {
1953
- return attachAll(cfg, reporter);
4620
+ if (state.openclaw.gatewayRunning && !state.openclaw.found) {
4621
+ warnings.push("port 18789 is in use but no OpenClaw install detected \u2014 another app may conflict");
1954
4622
  }
1955
- const serial = options.serial ?? await deviceRuntime.detectSingleDevice();
1956
- const name = options.name ?? serial;
1957
- const agentBin = await deviceRuntime.ensureAgent(reporter);
1958
- reporter.stop();
1959
- const pubkeyB64 = await deviceRuntime.ensureKeyAndGetPubkey(serial);
1960
- const instanceId = await tryBindDevice(pubkeyB64, name, cfg);
1961
- if (!instanceId) {
1962
- console.error("Bind was not completed \u2014 device-agent will not start without a platform binding.");
1963
- console.error("Run `beeos device attach` again to retry.");
1964
- return;
4623
+ if (state.devices.entries.length > 0 && services.length === 0) {
4624
+ warnings.push(
4625
+ "devices tracked via legacy devices.json \u2014 no OS services registered. Re-run `beeos device attach` to migrate them to the OS service manager."
4626
+ );
1965
4627
  }
1966
- await withDeviceLock(async () => {
1967
- const state = await loadDeviceState();
1968
- const idx = state.devices.findIndex((d) => d.serial === serial);
1969
- if (idx >= 0) {
1970
- if (isProcessAlive(state.devices[idx].pid)) {
1971
- console.log(`Device ${serial} is already attached (pid ${state.devices[idx].pid})`);
1972
- return;
1973
- }
1974
- console.error(` Cleaning stale entry for ${serial} (pid ${state.devices[idx].pid} no longer alive)`);
1975
- state.devices.splice(idx, 1);
1976
- }
1977
- const httpPort = nextHttpPort(state, cfg.device.http_port);
1978
- const pid = await deviceRuntime.spawnForDevice(
1979
- serial,
1980
- agentBin,
1981
- cfg.platform.bridge_url,
1982
- cfg.device.http_enabled,
1983
- httpPort
4628
+ if (fallbackReason2) {
4629
+ warnings.push(
4630
+ `No native OS service manager available (${fallbackReason2}) \u2014 using fallback, services WON'T persist across reboots.`
1984
4631
  );
1985
- await sleep3(2e3);
1986
- if (!isProcessAlive(pid)) {
1987
- const p2 = getPlatformAdapter();
1988
- const logPath2 = p2.joinPath(beeoHome(), "logs", `device-${serial}.log`);
1989
- const tail = await readLogTail(logPath2, 20);
1990
- throw new Error(
1991
- `device-agent for ${serial} exited immediately (pid ${pid}). Check logs:
1992
- ${logPath2}
1993
-
1994
- ${tail}`
4632
+ }
4633
+ for (const w of serviceCheck.warnings) warnings.push(w);
4634
+ for (const h of serviceCheck.hints) hints.push(h);
4635
+ for (const s of services) {
4636
+ if (!s.running && s.installed) {
4637
+ warnings.push(
4638
+ `service '${s.id}' is installed but not running \u2014 see log ${s.logFile}`
1995
4639
  );
1996
4640
  }
1997
- const keyFile = deviceRuntime.deviceKeyPath(serial);
1998
- state.devices.push({
1999
- serial,
2000
- name,
2001
- pid,
2002
- instance_id: instanceId,
2003
- key_file: keyFile,
2004
- http_port: httpPort
2005
- });
2006
- await saveDeviceState(state);
2007
- const p = getPlatformAdapter();
2008
- const logPath = p.joinPath(beeoHome(), "logs", `device-${serial}.log`);
2009
- console.log(` logs: ${logPath}`);
4641
+ if (s.lastExitCode !== null && s.lastExitCode !== 0) {
4642
+ warnings.push(
4643
+ `service '${s.id}' last exited with code ${s.lastExitCode} \u2014 see log ${s.logFile}`
4644
+ );
4645
+ }
4646
+ }
4647
+ if (!tools.adb.path) {
4648
+ warnings.push(
4649
+ "adb not found \u2014 `beeos device attach` will download Android platform-tools on first use"
4650
+ );
4651
+ }
4652
+ if (!tools.python.path) {
4653
+ warnings.push(
4654
+ "python3 not found on PATH \u2014 device-agent uses a uv-managed interpreter; install python 3.11+ if uv is unavailable"
4655
+ );
4656
+ }
4657
+ const bloatedLogs = await checkLogFileSizes();
4658
+ for (const l of bloatedLogs) {
4659
+ warnings.push(
4660
+ `Service log large: ${path8.basename(l.path)} = ${formatBytes(l.size)}`
4661
+ );
4662
+ hints.push(`truncate safely: : > ${l.path}`);
4663
+ }
4664
+ if (options.json) {
2010
4665
  console.log(
2011
- `Attached device ${serial} (${name}) \u2014 pid ${pid} (http :${httpPort}) \u2014 instance ${instanceId}`
4666
+ JSON.stringify(
4667
+ {
4668
+ platform: p.platform(),
4669
+ arch: process.arch,
4670
+ node: process.version,
4671
+ beeosHome: state.beeosHome,
4672
+ config: cfg,
4673
+ state,
4674
+ serviceManager: {
4675
+ kind: mgr.kind,
4676
+ available: serviceCheck.available,
4677
+ fallbackReason: fallbackReason2,
4678
+ services
4679
+ },
4680
+ tools,
4681
+ warnings,
4682
+ hints
4683
+ },
4684
+ null,
4685
+ 2
4686
+ )
2012
4687
  );
2013
- });
2014
- }
2015
- async function attachAll(cfg, reporter) {
2016
- const devices = await deviceRuntime.listAdbDevices();
2017
- if (devices.length === 0) {
2018
- console.log("No ADB devices found");
2019
4688
  return;
2020
4689
  }
2021
- const agentBin = await deviceRuntime.ensureAgent(reporter);
2022
- reporter.stop();
2023
- await withDeviceLock(async () => {
2024
- const state = await loadDeviceState();
2025
- state.devices = state.devices.filter((d) => isProcessAlive(d.pid));
2026
- const alreadyAttached = new Set(state.devices.map((d) => d.serial));
2027
- let count = 0;
2028
- for (const device of devices) {
2029
- if (alreadyAttached.has(device.serial)) continue;
2030
- const httpPort = nextHttpPort(state, cfg.device.http_port);
2031
- try {
2032
- const pubkeyB64 = await deviceRuntime.ensureKeyAndGetPubkey(device.serial);
2033
- const instanceId = await tryBindDevice(pubkeyB64, device.serial, cfg);
2034
- if (!instanceId) {
2035
- console.error(` Skipping ${device.serial} \u2014 bind not completed`);
2036
- continue;
2037
- }
2038
- const pid = await deviceRuntime.spawnForDevice(
2039
- device.serial,
2040
- agentBin,
2041
- cfg.platform.bridge_url,
2042
- cfg.device.http_enabled,
2043
- httpPort
2044
- );
2045
- await sleep3(2e3);
2046
- if (!isProcessAlive(pid)) {
2047
- const p = getPlatformAdapter();
2048
- const logPath = p.joinPath(beeoHome(), "logs", `device-${device.serial}.log`);
2049
- const tail = await readLogTail(logPath, 10);
2050
- console.error(`device-agent for ${device.serial} exited immediately (pid ${pid}): ${tail}`);
2051
- continue;
2052
- }
2053
- const keyFile = deviceRuntime.deviceKeyPath(device.serial);
2054
- state.devices.push({
2055
- serial: device.serial,
2056
- name: device.serial,
2057
- pid,
2058
- instance_id: instanceId,
2059
- key_file: keyFile,
2060
- http_port: httpPort
2061
- });
2062
- console.log(`Attached device ${device.serial} \u2014 pid ${pid} (http :${httpPort})`);
2063
- count++;
2064
- } catch (e) {
2065
- console.error(`Failed to attach ${device.serial}: ${e}`);
2066
- }
4690
+ console.log("");
4691
+ console.log(` OS/Arch : ${p.platform()}/${process.arch}`);
4692
+ console.log(` Node.js : ${process.version}`);
4693
+ console.log(` API URL : ${cfg.platform.api_url}`);
4694
+ console.log(` Bridge : ${cfg.platform.bridge_url}`);
4695
+ console.log("");
4696
+ for (const line of summarizeExistingInstall(state)) {
4697
+ console.log(` ${line}`);
4698
+ }
4699
+ console.log(
4700
+ ` Service manager: ${mgr.kind}${fallbackReason2 ? " (fallback)" : ""} \u2014 ${services.length} service(s) registered`
4701
+ );
4702
+ for (const s of services) {
4703
+ const status2 = s.running ? "running" : s.installed ? "stopped" : "uninstalled";
4704
+ const pid = s.pid != null ? ` pid=${s.pid}` : "";
4705
+ console.log(` - ${s.id.padEnd(28)} ${status2}${pid}`);
4706
+ }
4707
+ console.log(` adb : ${tools.adb.path ?? "not found"}`);
4708
+ console.log(` python3 : ${tools.python.path ?? "not found"}`);
4709
+ console.log(` scrcpy-bridge: ${tools.scrcpyBridge.path ?? "not installed (auto on attach)"}`);
4710
+ console.log(` vnc-bridge : ${tools.vncBridge.path ?? "not installed (auto on --vnc-host)"}`);
4711
+ console.log("");
4712
+ if (warnings.length > 0) {
4713
+ console.log(" Warnings:");
4714
+ for (const w of warnings) {
4715
+ console.log(` - ${w}`);
2067
4716
  }
2068
- await saveDeviceState(state);
2069
- console.log(`Attached ${count} new device(s)`);
2070
- });
4717
+ console.log("");
4718
+ } else {
4719
+ console.log(" No warnings \u2014 machine looks good.\n");
4720
+ }
4721
+ if (hints.length > 0) {
4722
+ console.log(" Hints:");
4723
+ for (const h of hints) {
4724
+ console.log(` $ ${h}`);
4725
+ }
4726
+ console.log("");
4727
+ }
2071
4728
  }
2072
- async function detach(options) {
2073
- const cfg = await loadOrCreateConfig();
2074
- await withDeviceLock(async () => {
2075
- const state = await loadDeviceState();
2076
- if (options.all) {
2077
- for (const entry2 of state.devices) {
2078
- killProcess(entry2.pid);
2079
- await notifyOffline(cfg.platform.api_url, entry2.instance_id);
4729
+ async function collectToolStatus(p) {
4730
+ const [adbPath, pythonPath, scrcpyPath, vncPath] = await Promise.all([
4731
+ findAdb().catch(() => null),
4732
+ findPython2(p).catch(() => null),
4733
+ scrcpyBridgeRuntime.findBinary().catch(() => null),
4734
+ vncBridgeRuntime.findBinary().catch(() => null)
4735
+ ]);
4736
+ return {
4737
+ adb: { path: adbPath },
4738
+ python: { path: pythonPath },
4739
+ scrcpyBridge: { path: scrcpyPath },
4740
+ vncBridge: { path: vncPath }
4741
+ };
4742
+ }
4743
+ async function findPython2(p) {
4744
+ const whichCmd = p.platform() === "win32" ? "where" : "which";
4745
+ for (const bin of ["python3", "python"]) {
4746
+ try {
4747
+ const r = await p.exec(whichCmd, [bin]);
4748
+ if (r.code === 0 && r.stdout.trim()) {
4749
+ return r.stdout.trim().split("\n")[0].trim();
2080
4750
  }
2081
- const count = state.devices.length;
2082
- state.devices = [];
2083
- await saveDeviceState(state);
2084
- console.log(`Detached ${count} device(s)`);
2085
- return;
2086
- }
2087
- if (!options.serial) {
2088
- throw new Error("Specify --serial or use --all");
4751
+ } catch {
2089
4752
  }
2090
- const idx = state.devices.findIndex((d) => d.serial === options.serial);
2091
- if (idx < 0) throw new Error(`Device ${options.serial} not found`);
2092
- const entry = state.devices.splice(idx, 1)[0];
2093
- killProcess(entry.pid);
2094
- await notifyOffline(cfg.platform.api_url, entry.instance_id);
2095
- await saveDeviceState(state);
2096
- console.log(`Detached device ${options.serial}`);
2097
- });
4753
+ }
4754
+ return null;
2098
4755
  }
2099
- async function notifyOffline(apiUrl, instanceId) {
2100
- if (!instanceId) return;
4756
+ async function checkLogFileSizes() {
4757
+ const dir = path8.join(beeoHome(), "logs", "services");
4758
+ let entries;
2101
4759
  try {
2102
- await markInstanceOffline(apiUrl, instanceId);
4760
+ entries = await fs7.readdir(dir);
2103
4761
  } catch {
4762
+ return [];
4763
+ }
4764
+ const bloated = [];
4765
+ for (const name of entries) {
4766
+ if (!name.endsWith(".log")) continue;
4767
+ const full = path8.join(dir, name);
4768
+ try {
4769
+ const st = await fs7.stat(full);
4770
+ if (st.isFile() && st.size >= LOG_SIZE_WARN_BYTES) {
4771
+ bloated.push({ path: full, size: st.size });
4772
+ }
4773
+ } catch {
4774
+ }
2104
4775
  }
4776
+ return bloated;
2105
4777
  }
2106
- async function list(options) {
2107
- if (options.local) {
2108
- const state = await loadDeviceState();
2109
- if (state.devices.length === 0) {
2110
- console.log("No attached devices");
2111
- return;
4778
+ function formatBytes(n) {
4779
+ if (n >= 1024 * 1024 * 1024) return `${(n / (1024 * 1024 * 1024)).toFixed(1)}GB`;
4780
+ if (n >= 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(0)}MB`;
4781
+ if (n >= 1024) return `${(n / 1024).toFixed(0)}KB`;
4782
+ return `${n}B`;
4783
+ }
4784
+
4785
+ // src/commands/init.ts
4786
+ init_fallback_banner();
4787
+ async function run7(options) {
4788
+ await ensureDirs();
4789
+ try {
4790
+ const mgr = await getServiceManager();
4791
+ const mig = await migrateLegacySupervisor(mgr);
4792
+ if (!options.json && mig.ran) {
4793
+ if (mig.errors.length === 0) {
4794
+ console.log(
4795
+ ` Migrated ${mig.migrated} service(s) from the legacy supervisor to ${mgr.kind}.`
4796
+ );
4797
+ } else {
4798
+ console.log(
4799
+ ` Migrated ${mig.migrated} service(s) with ${mig.errors.length} error(s); backup at ${mig.backupPath ?? "(unknown)"}.`
4800
+ );
4801
+ for (const e of mig.errors) console.log(` - ${e.id}: ${e.error}`);
4802
+ }
2112
4803
  }
2113
- console.log(`${"SERIAL".padEnd(16)} ${"NAME".padEnd(20)} ${"STATUS".padEnd(12)} PID`);
2114
- for (const entry of state.devices) {
2115
- const status = isProcessAlive(entry.pid) ? "running" : "stopped";
2116
- console.log(
2117
- `${entry.serial.padEnd(16)} ${entry.name.padEnd(20)} ${status.padEnd(12)} ${entry.pid}`
2118
- );
4804
+ } catch (e) {
4805
+ if (!options.json) {
4806
+ console.log(` (legacy supervisor migration skipped: ${e instanceof Error ? e.message : String(e)})`);
2119
4807
  }
4808
+ }
4809
+ if (options.device) {
4810
+ const { attach: attach2 } = await Promise.resolve().then(() => (init_device2(), device_exports));
4811
+ await attach2({});
4812
+ return;
4813
+ }
4814
+ const state = await detectExistingInstall();
4815
+ if (options.json) {
4816
+ await run6({ json: true });
2120
4817
  } else {
2121
- console.log("Remote device listing requires platform API (use --local for local devices)");
4818
+ printBanner();
4819
+ for (const line of summarizeExistingInstall(state)) {
4820
+ console.log(` ${line}`);
4821
+ }
4822
+ console.log("");
2122
4823
  }
2123
- }
2124
- async function exec(prompt, options) {
2125
- const state = await loadDeviceState();
2126
- const entry = options.serial ? state.devices.find((d) => d.serial === options.serial) : state.devices[0];
2127
- if (!entry) {
2128
- throw new Error(options.serial ? `Device ${options.serial} not attached` : "No attached devices");
4824
+ const decision = await decideAction(state, options);
4825
+ if (decision === "skip") {
4826
+ console.log("Nothing to do. Run `beeos init` again or `beeos device attach` when ready.");
4827
+ return;
2129
4828
  }
2130
- if (!entry.http_port) {
4829
+ if (decision === "upgrade") {
4830
+ console.log("Upgrading / ensuring OpenClaw is running...\n");
4831
+ }
4832
+ if (decision === "rebind-new-key") {
4833
+ await rotateIdentity();
4834
+ }
4835
+ if (decision === "rebind-keep-key" || decision === "rebind-new-key") {
4836
+ await removeBindingInfo();
4837
+ }
4838
+ const framework = options.framework ?? "openclaw";
4839
+ if (framework !== "openclaw") {
2131
4840
  throw new Error(
2132
- `Device ${entry.serial} has no local HTTP port configured.
2133
- Enable http_enabled in ~/.beeos/config.toml and re-attach.`
4841
+ `Agent framework '${framework}' is not yet supported. Only 'openclaw' is available today.`
2134
4842
  );
2135
4843
  }
2136
- const p = getPlatformAdapter();
2137
- const url = `http://127.0.0.1:${entry.http_port}/prompt`;
2138
- console.log(`-> ${entry.serial} (local :${entry.http_port}): ${prompt}`);
2139
- const resp = await p.fetch(url, {
2140
- method: "POST",
2141
- headers: { "Content-Type": "application/json" },
2142
- body: JSON.stringify({ prompt })
4844
+ await run(framework, {
4845
+ force: true,
4846
+ json: options.json,
4847
+ browser: options.browser !== false
2143
4848
  });
2144
- if (!resp.ok) {
2145
- const body2 = await resp.text().catch(() => "");
2146
- throw new Error(`device-agent returned ${resp.status} \u2014 ${body2}`);
4849
+ if (!options.json && !options.skipServicePrompt) {
4850
+ await maybePromptServiceInstall();
4851
+ }
4852
+ if (!options.json) {
4853
+ printNextSteps();
2147
4854
  }
2148
- const body = await resp.json();
2149
- console.log(JSON.stringify(body, null, 2));
2150
4855
  }
2151
- async function upgrade() {
2152
- const reporter = new CliReporter();
2153
- await deviceRuntime.upgradeAgent(reporter);
4856
+ async function decideAction(state, options) {
4857
+ const choice = inferInitChoices(state);
4858
+ if (choice.options.length === 1) {
4859
+ return choice.defaultDecision;
4860
+ }
4861
+ if (options.yes || options.json) {
4862
+ return choice.defaultDecision;
4863
+ }
4864
+ if (!process.stdin.isTTY) {
4865
+ return choice.defaultDecision;
4866
+ }
4867
+ console.log("Detected existing install. Choose an action:");
4868
+ choice.options.forEach((d, i) => {
4869
+ const marker = d === choice.defaultDecision ? "*" : " ";
4870
+ console.log(` ${marker} [${i + 1}] ${initDecisionLabel(d)}`);
4871
+ });
4872
+ console.log("");
4873
+ const answer = await prompt(`Choose [1-${choice.options.length}] (default ${choice.options.indexOf(choice.defaultDecision) + 1}): `);
4874
+ const trimmed = answer.trim();
4875
+ if (!trimmed) return choice.defaultDecision;
4876
+ const idx = parseInt(trimmed, 10);
4877
+ if (!Number.isFinite(idx) || idx < 1 || idx > choice.options.length) {
4878
+ return choice.defaultDecision;
4879
+ }
4880
+ return choice.options[idx - 1];
2154
4881
  }
2155
- async function tryBindDevice(pubkeyB64, name, cfg) {
2156
- const fp = fingerprintFromB64(pubkeyB64);
4882
+ async function maybePromptServiceInstall() {
2157
4883
  try {
2158
- const resp = await agentBind(
2159
- cfg.platform.api_url,
2160
- pubkeyB64,
2161
- fp,
2162
- "device",
2163
- name
2164
- );
2165
- if (resp.status === "bound") {
2166
- console.log(` Already bound \u2014 instance: ${resp.instance_id}`);
2167
- return resp.instance_id ?? void 0;
2168
- }
2169
- if (resp.bind_id) {
2170
- const bindUrl = buildBindUrl(cfg.platform.dashboard_base_url, resp.bind_id);
2171
- await presentBindUrl(bindUrl, false);
2172
- console.log("");
2173
- console.log(" Waiting for bind approval (timeout: 10min)...");
2174
- try {
2175
- const instanceId = await pollUntilBound(
2176
- cfg.platform.api_url,
2177
- resp.bind_id,
2178
- 6e5
2179
- );
2180
- console.log(` Bind confirmed! Instance: ${instanceId}`);
2181
- return instanceId;
2182
- } catch (e) {
2183
- console.error(` Bind polling stopped: ${e}`);
2184
- }
4884
+ const mgr = await getServiceManager();
4885
+ maybePrintFallbackBanner(mgr.kind);
4886
+ const check = await mgr.selfCheck();
4887
+ if (!check.available) {
4888
+ console.log(` \u26A0 Service manager '${mgr.kind}' is not fully available \u2014 persistence limited.`);
2185
4889
  }
2186
- return void 0;
4890
+ for (const w of check.warnings) console.log(` ! ${w}`);
4891
+ for (const h of check.hints) console.log(` $ ${h}`);
2187
4892
  } catch (e) {
2188
- console.error(` Platform bind failed: ${e}`);
2189
- return void 0;
4893
+ console.log(` (service-manager self-check skipped: ${e instanceof Error ? e.message : String(e)})`);
2190
4894
  }
2191
4895
  }
2192
- function nextHttpPort(state, base) {
2193
- const used = new Set(state.devices.map((d) => d.http_port));
2194
- let port = base;
2195
- while (used.has(port)) port++;
2196
- return port;
2197
- }
2198
- async function readLogTail(path2, lines) {
4896
+ async function rotateIdentity() {
2199
4897
  const p = getPlatformAdapter();
4898
+ const home = beeoHome();
4899
+ const identityDir = p.joinPath(home, "identity");
4900
+ const keypair = p.joinPath(identityDir, "keypair.json");
4901
+ const fp = p.joinPath(identityDir, "fingerprint");
4902
+ if (!await p.exists(keypair)) return;
4903
+ const ts = Math.floor(Date.now() / 1e3);
4904
+ const backup = p.joinPath(identityDir, `keypair.json.backup-${ts}`);
4905
+ try {
4906
+ await p.copyFile(keypair, backup);
4907
+ } catch {
4908
+ }
2200
4909
  try {
2201
- const content = await p.readFile(path2);
2202
- const allLines = content.split("\n");
2203
- const start = Math.max(0, allLines.length - lines);
2204
- return allLines.slice(start).join("\n");
4910
+ await p.rm(keypair);
2205
4911
  } catch {
2206
- return "(could not read log file)";
2207
4912
  }
4913
+ try {
4914
+ await p.rm(fp);
4915
+ } catch {
4916
+ }
4917
+ console.log(`Rotated identity. Previous keypair saved at ${backup}`);
2208
4918
  }
2209
- function sleep3(ms) {
2210
- return new Promise((resolve) => setTimeout(resolve, ms));
4919
+ function printBanner() {
4920
+ console.log("");
4921
+ console.log(" BeeOS init");
4922
+ console.log("");
4923
+ }
4924
+ function printNextSteps() {
4925
+ console.log("");
4926
+ console.log("Next steps:");
4927
+ console.log(" beeos status # show running agents");
4928
+ console.log(" beeos device attach # bind a physical Android device");
4929
+ console.log(" beeos doctor # health check & diagnostics");
4930
+ console.log("");
4931
+ }
4932
+ function prompt(question) {
4933
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
4934
+ return new Promise((resolve) => {
4935
+ rl.question(question, (answer) => {
4936
+ rl.close();
4937
+ resolve(answer);
4938
+ });
4939
+ });
4940
+ }
4941
+
4942
+ // src/commands/service.ts
4943
+ init_dist();
4944
+ async function install2(options) {
4945
+ const mgr = await getServiceManager();
4946
+ const check = await mgr.selfCheck();
4947
+ const reason = activeFallbackReason();
4948
+ if (options.json) {
4949
+ console.log(
4950
+ JSON.stringify(
4951
+ {
4952
+ ok: check.available,
4953
+ manager: mgr.kind,
4954
+ fallbackReason: reason,
4955
+ warnings: check.warnings,
4956
+ hints: check.hints
4957
+ },
4958
+ null,
4959
+ 2
4960
+ )
4961
+ );
4962
+ return;
4963
+ }
4964
+ console.log(`Service manager: ${mgr.kind}${reason ? ` (fallback \u2014 ${reason})` : ""}`);
4965
+ if (check.available) {
4966
+ console.log(" \u2713 available \u2014 BeeOS services will persist across reboots.");
4967
+ } else {
4968
+ console.log(" \u2717 not available \u2014 using fallback process runner (NO persistence).");
4969
+ }
4970
+ for (const w of check.warnings) console.log(` ! ${w}`);
4971
+ for (const h of check.hints) console.log(` $ ${h}`);
4972
+ console.log(
4973
+ "\nNote: per-target services are registered automatically by `beeos start` / `beeos device attach`."
4974
+ );
4975
+ }
4976
+ async function uninstall(options) {
4977
+ const mgr = await getServiceManager();
4978
+ let services = [];
4979
+ try {
4980
+ services = await mgr.list();
4981
+ } catch {
4982
+ services = [];
4983
+ }
4984
+ const removed = [];
4985
+ const errors = [];
4986
+ for (const s of services) {
4987
+ try {
4988
+ await mgr.uninstall(s.id);
4989
+ removed.push(s.id);
4990
+ } catch (e) {
4991
+ errors.push({ id: s.id, error: e instanceof Error ? e.message : String(e) });
4992
+ }
4993
+ }
4994
+ if (options.json) {
4995
+ console.log(JSON.stringify({ removed, errors }, null, 2));
4996
+ return;
4997
+ }
4998
+ if (removed.length === 0 && errors.length === 0) {
4999
+ console.log("No BeeOS services registered.");
5000
+ } else {
5001
+ for (const id of removed) console.log(` uninstalled ${id}`);
5002
+ for (const err of errors) console.log(` ! failed to uninstall ${err.id}: ${err.error}`);
5003
+ }
5004
+ }
5005
+ async function status(options) {
5006
+ const mgr = await getServiceManager();
5007
+ const reason = activeFallbackReason();
5008
+ let services = [];
5009
+ try {
5010
+ services = await mgr.list();
5011
+ } catch {
5012
+ services = [];
5013
+ }
5014
+ if (options.json) {
5015
+ console.log(
5016
+ JSON.stringify(
5017
+ { manager: mgr.kind, fallbackReason: reason, services },
5018
+ null,
5019
+ 2
5020
+ )
5021
+ );
5022
+ return;
5023
+ }
5024
+ console.log(`Service manager: ${mgr.kind}${reason ? ` (fallback)` : ""}`);
5025
+ if (services.length === 0) {
5026
+ console.log(" (no services registered)");
5027
+ return;
5028
+ }
5029
+ for (const s of services) {
5030
+ const state = s.running ? "running" : s.installed ? "stopped" : "uninstalled";
5031
+ const pid = s.pid != null ? ` pid=${s.pid}` : "";
5032
+ console.log(` ${s.id.padEnd(28)} ${state}${pid}`);
5033
+ }
2211
5034
  }
2212
5035
 
2213
5036
  // src/index.ts
2214
5037
  setPlatformAdapter(new NodePlatformAdapter());
2215
5038
  var program = new Command();
2216
5039
  program.name("beeos").version("1.0.0").description("BeeOS \u2014 run AI agents from your desktop");
5040
+ program.command("init").description("One-shot install + bind flow (default entry from the curl installer)").option("--framework <name>", "Agent framework to install (default: openclaw)").option("--yes", "Non-interactive \u2014 accept the default action", false).option("--json", "Output machine-readable JSON (implies --yes)", false).option("--no-browser", "Don't auto-open a browser for bind confirmation").option("--headless", "Never open a browser (use terminal QR only)", false).option("--skip-service-prompt", "Skip the system-service install prompt", false).option("--device", "Jump straight into `beeos device attach` instead", false).action((opts) => run7(opts));
5041
+ program.command("doctor").description("Inspect local BeeOS state (install, binding, services, warnings)").option("--json", "Output machine-readable JSON", false).action((opts) => run6(opts));
5042
+ var serviceCmd = program.command("service").description("Inspect and manage the OS-native service manager (launchd / systemd --user / Task Scheduler)");
5043
+ serviceCmd.command("install").description("Verify the OS service manager is available (per-target services install automatically)").option("--json", "Output machine-readable JSON", false).action((opts) => install2(opts));
5044
+ serviceCmd.command("uninstall").description("Uninstall every BeeOS-managed OS service").option("--json", "Output machine-readable JSON", false).action((opts) => uninstall(opts));
5045
+ serviceCmd.command("status").description("List every BeeOS-managed OS service").option("--json", "Output machine-readable JSON", false).action((opts) => status(opts));
2217
5046
  program.command("start").description("Install and start an agent").argument("<agent_type>", "Agent type to start (e.g. openclaw)").option("--version <version>", "Pin to a specific version instead of latest").option("--force", "Skip confirmation prompts", false).option("--json", "Output machine-readable JSON to stdout (implies --force)", false).option("--no-browser", "Don't auto-open browser for bind confirmation").action((agentFramework, options) => run(agentFramework, options));
2218
5047
  program.command("stop").description("Stop a running agent").argument("<agent_type>", "Agent type to stop").action((agentFramework) => run2(agentFramework));
2219
5048
  program.command("status").description("Show status of managed agents").action(run3);
2220
5049
  program.command("update").description("Update an agent to the latest version").argument("<agent_type>", "Agent type to update").action((agentFramework) => run4(agentFramework));
2221
5050
  program.command("bind-status").description("Check agent binding status").argument("<bind_id>", "Bind session ID returned by `beeos start --json`").option("--json", "Output machine-readable JSON", false).action(run5);
2222
5051
  var deviceCmd = program.command("device").description("Manage device agents (Android/Desktop/ChromeOS)");
2223
- deviceCmd.command("attach").description("Attach an ADB-connected device as an AI agent").option("--serial <serial>", "ADB device serial number").option("--name <name>", "Human-readable device name").option("--all", "Attach all connected ADB devices", false).action(attach);
5052
+ deviceCmd.command("attach").description("Attach an ADB-connected device as an AI agent").option("--serial <serial>", "ADB device serial number").option("--name <name>", "Human-readable device name").option("--all", "Attach all connected ADB devices", false).option(
5053
+ "--no-video",
5054
+ "Skip the scrcpy-bridge WebRTC sidecar; only run device-agent. Useful when iterating on ACP / Python code without the video stream."
5055
+ ).option(
5056
+ "--vnc-host <host>",
5057
+ "Use vnc-bridge against a VNC server at this host (desktop/Linux/macOS). Implies video mode = vnc instead of scrcpy."
5058
+ ).option("--vnc-port <port>", "VNC TCP port (default 5900)", (v) => Number(v)).option("--vnc-password <password>", "VNC password (forwarded via env)").action(attach);
2224
5059
  deviceCmd.command("detach").description("Detach a device agent").option("--serial <serial>", "ADB device serial number").option("--all", "Detach all devices", false).action(detach);
2225
5060
  deviceCmd.command("list").description("List attached devices").option("--local", "Only show locally running device agents", false).action(list);
2226
5061
  deviceCmd.command("exec").description("Send a natural language command to a device").option("--serial <serial>", "ADB device serial number").argument("<prompt>", "The command/prompt to execute").action(exec);
2227
- deviceCmd.command("upgrade").description("Upgrade the device-agent to the latest version").action(upgrade);
5062
+ deviceCmd.command("upgrade").description("Upgrade the device-agent (and scrcpy/vnc bridges) to the latest version").option("--no-bridges", "Skip upgrading scrcpy-bridge / vnc-bridge binaries").action(upgrade);
2228
5063
  program.parseAsync(process.argv).catch((err) => {
2229
5064
  console.error(err.message);
2230
5065
  process.exit(1);