shellx-ai 1.0.12 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +586 -0
  3. package/dist/automation/element-finder.d.ts +189 -0
  4. package/dist/automation/element-finder.js +322 -0
  5. package/dist/automation/element-finder.js.map +1 -0
  6. package/dist/automation/ui-action-handler.d.ts +330 -0
  7. package/dist/automation/ui-action-handler.js +873 -0
  8. package/dist/automation/ui-action-handler.js.map +1 -0
  9. package/dist/cbor-compat.d.ts +27 -0
  10. package/dist/cbor-compat.js +108 -0
  11. package/dist/cbor-compat.js.map +1 -0
  12. package/dist/domain-manager.d.ts +80 -0
  13. package/dist/domain-manager.js +158 -0
  14. package/dist/domain-manager.js.map +1 -0
  15. package/dist/error-handler.d.ts +87 -0
  16. package/dist/error-handler.js +148 -0
  17. package/dist/error-handler.js.map +1 -0
  18. package/dist/errors.d.ts +114 -0
  19. package/dist/errors.js +139 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +163 -54
  22. package/dist/index.js +706 -480
  23. package/dist/index.js.map +1 -0
  24. package/dist/logger.d.ts +81 -0
  25. package/dist/logger.js +128 -0
  26. package/dist/logger.js.map +1 -0
  27. package/dist/protocol.d.ts +147 -31
  28. package/dist/protocol.js +2 -2
  29. package/dist/protocol.js.map +1 -0
  30. package/dist/shell/output-buffer.d.ts +152 -0
  31. package/dist/shell/output-buffer.js +163 -0
  32. package/dist/shell/output-buffer.js.map +1 -0
  33. package/dist/shell/shell-command-executor.d.ts +182 -0
  34. package/dist/shell/shell-command-executor.js +348 -0
  35. package/dist/shell/shell-command-executor.js.map +1 -0
  36. package/dist/shellx.d.ts +681 -178
  37. package/dist/shellx.js +762 -1159
  38. package/dist/shellx.js.map +1 -0
  39. package/dist/types.d.ts +132 -57
  40. package/dist/types.js +4 -4
  41. package/dist/types.js.map +1 -0
  42. package/dist/utils/retry-helper.d.ts +73 -0
  43. package/dist/utils/retry-helper.js +92 -0
  44. package/dist/utils/retry-helper.js.map +1 -0
  45. package/dist/utils.d.ts +3 -3
  46. package/dist/utils.js +17 -23
  47. package/dist/utils.js.map +1 -0
  48. package/package.json +95 -62
package/dist/index.js CHANGED
@@ -1,52 +1,41 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
1
+ import { v4 as uuidv4 } from "uuid";
2
+ import { wait } from "./utils.js";
3
+ import { buildDeviceApiUrl } from "./domain-manager.js";
4
+ import { createLogger } from "./logger.js";
5
+ // Add readable timestamp to all logs
6
+ const formatTs = () => {
7
+ const now = new Date();
8
+ return now.toISOString();
9
+ };
10
+ const executionLogStore = [];
11
+ const formatLogArgs = (args) => args
12
+ .map((arg) => {
13
+ if (typeof arg === "string")
14
+ return arg;
15
+ try {
16
+ return JSON.stringify(arg);
17
+ }
18
+ catch {
19
+ return String(arg);
20
+ }
21
+ })
22
+ .join(" ");
23
+ const pushExecutionLog = (ts, args) => {
24
+ executionLogStore.push(`[${ts}] ${formatLogArgs(args)}`);
25
+ };
26
+ const originalConsoleLog = console.log.bind(console);
27
+ const originalConsoleWarn = console.warn.bind(console);
28
+ const originalConsoleError = console.error.bind(console);
29
+ const wrapConsole = (originalFn) => (...args) => {
30
+ const ts = formatTs();
31
+ pushExecutionLog(ts, args);
32
+ originalFn(`[${ts}]`, ...args);
43
33
  };
44
- Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.ShellX = exports.createShellXWithShellMonitoring = exports.ConnectionTaskClient = exports.DEFAULT_CONFIG = void 0;
46
- const uuid_1 = require("uuid");
47
- const utils_1 = require("./utils");
48
- // 定义默认配置
49
- exports.DEFAULT_CONFIG = {
34
+ console.log = wrapConsole(originalConsoleLog);
35
+ console.warn = wrapConsole(originalConsoleWarn);
36
+ console.error = wrapConsole(originalConsoleError);
37
+ // Define default configuration
38
+ export const DEFAULT_CONFIG = {
50
39
  timeout: 5000,
51
40
  reconnect: true,
52
41
  reconnectMaxAttempts: 5,
@@ -57,230 +46,316 @@ exports.DEFAULT_CONFIG = {
57
46
  onError: () => { },
58
47
  onReconnectFailed: () => { },
59
48
  };
60
- const cbor_1 = require("cbor");
61
- // 安全地获取环境变量,兼容浏览器和Node.js环境
62
- let authKey = (0, utils_1.getEnvVar)('SHELLX_AUTH_KEY');
49
+ import { initCbor, cborEncode, cborDecode } from "./cbor-compat.js";
63
50
  /**
64
- * 获取适合当前环境的WebSocket构造函数
51
+ * Get WebSocket constructor for current environment
65
52
  */
66
- function getWebSocket() {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- // 检查是否已有全局WebSocket(浏览器环境或Node.js 20+)
69
- if (typeof globalThis.WebSocket !== 'undefined') {
70
- return globalThis.WebSocket;
71
- }
72
- // Node.js环境下动态导入ws模块
73
- try {
74
- const wsModule = yield Promise.resolve().then(() => __importStar(require('ws')));
75
- return wsModule.default || wsModule;
76
- }
77
- catch (error) {
78
- console.error('❌ [WebSocket] ws模块不可用,请安装: npm install ws');
79
- throw new Error('WebSocket not available. Please install ws module: npm install ws');
80
- }
81
- });
53
+ async function getWebSocket() {
54
+ // Check if global WebSocket exists (browser environment or Node.js 20+)
55
+ if (typeof globalThis.WebSocket !== "undefined") {
56
+ return globalThis.WebSocket;
57
+ }
58
+ // Dynamically import ws module in Node.js environment
59
+ try {
60
+ const wsModule = await import("ws");
61
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
62
+ return (wsModule.default || wsModule);
63
+ }
64
+ catch {
65
+ console.error("❌ [WebSocket] ws module is not available, please install: npm install ws");
66
+ throw new Error("WebSocket not available. Please install ws module: npm install ws");
67
+ }
82
68
  }
83
69
  function isPendingTask(taskType) {
84
- return taskType && taskType !== "command";
70
+ return taskType && taskType !== "command" && taskType !== "oneway";
85
71
  }
86
72
  /**
87
- * Enhanced WebSocket Task Client with protocol-aware task methods
73
+ * Low-level WebSocket Client for direct protocol communication
74
+ *
75
+ * @warning This is a low-level API intended for advanced use cases only.
76
+ * Most users should use the ShellX class instead, which provides a simpler,
77
+ * more intuitive interface with automatic error handling and retry logic.
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * // For most users, use ShellX instead:
82
+ * import { ShellX } from './shellx';
83
+ * const shellx = new ShellX({ deviceId: 'your-device-id' });
84
+ * await shellx.connect();
85
+ * await shellx.click({ text: 'Submit' });
86
+ * ```
87
+ *
88
+ * @see Use ShellX for a high-level API unless you need low-level WebSocket access
88
89
  */
89
- class ConnectionTaskClient {
90
- constructor(deviceId, config = {}) {
91
- this.ws = null; // Use any to avoid WebSocket type issues
92
- this.shellxConnected = false; // ShellX.ai 连接状态
93
- this.wsConnected = false; // WebSocket 连接状态
94
- this.authenticated = false; // 认证状态
95
- this.pendingTasks = new Map();
96
- this.messageQueue = [];
97
- this.reconnectAttempts = 0;
98
- this.pingIntervalId = null;
99
- this.initializationPromise = null;
100
- this.shellx = null; // 关联的 ShellX 实例
90
+ export class ConnectionClient {
91
+ wsUrl;
92
+ deviceId;
93
+ config;
94
+ ws = null;
95
+ shellxConnected = false; // ShellX.ai connection status
96
+ wsConnected = false; // WebSocket connection status
97
+ authenticated = false; // Authentication status
98
+ pendingTasks = new Map();
99
+ messageQueue = [];
100
+ reconnectAttempts = 0;
101
+ pingIntervalId = null;
102
+ initializationPromise = null;
103
+ shellx = null; // Associated ShellX instance
104
+ executionLogsRef = executionLogStore; // Current execution log
105
+ logger;
106
+ constructor(deviceId, config = {}, logger) {
101
107
  this.deviceId = deviceId;
102
- this.config = Object.assign(Object.assign({}, exports.DEFAULT_CONFIG), config);
108
+ this.config = { ...DEFAULT_CONFIG, ...config };
109
+ // Use provided logger or create default one
110
+ this.logger = logger || createLogger("ConnectionClient");
103
111
  this.initializationPromise = this.init();
104
112
  }
105
- init() {
106
- return __awaiter(this, void 0, void 0, function* () {
107
- var _a, _b;
108
- try {
109
- this.wsUrl = yield this.authenticateDevice(this.deviceId);
110
- if (!this.wsUrl) {
113
+ async init() {
114
+ await initCbor(); // load the right CBOR codec for this environment
115
+ return new Promise((resolve, reject) => {
116
+ this.authenticateDevice()
117
+ .then((wsUrl) => {
118
+ if (!wsUrl) {
111
119
  this.config.onError(new Event("connection"));
120
+ void this.reconnect();
121
+ reject(new Error("Failed to authenticate device"));
112
122
  return;
113
123
  }
114
- console.log('Initializing ShellX client wsUrl:' + this.wsUrl + " deviceId: " + this.deviceId);
115
- // 获取适合当前环境的WebSocket构造函数
116
- const WebSocketConstructor = yield getWebSocket();
117
- this.ws = new WebSocketConstructor(this.wsUrl);
118
- // 设置二进制类型(如果支持)
119
- if (this.ws.binaryType !== undefined) {
120
- this.ws.binaryType = 'arraybuffer';
121
- }
122
- this.ws.onopen = () => {
123
- console.log('🔗 [ShellX] WebSocket连接已建立,发送认证消息...' + this.wsUrl);
124
- this.wsConnected = true;
125
- this.reconnectAttempts = 0;
126
- // 连接建立后立即发送认证消息
127
- this.sendAuthenticationMessage();
128
- };
129
- this.ws.onmessage = (event) => {
130
- this.handleMessage(event);
131
- };
132
- this.ws.onclose = (event) => {
133
- var _a, _b;
134
- console.log('❌ [ShellX] WebSocket连接已关闭,正在尝试重新连接...' +
135
- event.code +
136
- ' ' +
137
- this.wsUrl);
138
- this.wsConnected = false;
139
- this.shellxConnected = false;
140
- this.authenticated = false;
141
- (_b = (_a = this.config).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, event);
142
- this.stopPing();
143
- this.reconnect();
144
- };
145
- this.ws.onerror = (error) => {
146
- var _a, _b;
147
- (_b = (_a = this.config).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
148
- };
149
- }
150
- catch (error) {
151
- console.error('❌ [WebSocket] 初始化失败:', error);
152
- (_b = (_a = this.config).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
153
- throw error;
154
- }
124
+ // Store the WebSocket URL for later use
125
+ this.wsUrl = wsUrl;
126
+ const separator = wsUrl.includes("?") ? "&" : "?";
127
+ const wsUrlWithSessionId = `${wsUrl}${separator}session_id=${this.deviceId}`;
128
+ this.logger.info("Initializing ShellX client wsUrl:" + wsUrl + " deviceId: " + this.deviceId);
129
+ this.logger.info("WebSocket URL with session_id: " + wsUrlWithSessionId);
130
+ // Get WebSocket constructor for current environment
131
+ getWebSocket()
132
+ .then((WebSocketConstructor) => {
133
+ try {
134
+ this.ws = new WebSocketConstructor(wsUrlWithSessionId);
135
+ // Set binary type (if supported)
136
+ if (this.ws.binaryType !== undefined) {
137
+ this.ws.binaryType = "arraybuffer";
138
+ }
139
+ this.ws.onopen = () => {
140
+ this.logger.info("🔗 [ShellX] WebSocket connection established:" + this.wsUrl);
141
+ this.wsConnected = true;
142
+ this.reconnectAttempts = 0;
143
+ // Mark as connected after WebSocket open
144
+ this.shellxConnected = true;
145
+ this.config.onOpen?.(this.deviceId);
146
+ void this.flushQueue();
147
+ void this.startPing();
148
+ // Resolve initialization promise after WebSocket is connected
149
+ resolve();
150
+ // Check if we're in Android sandbox environment
151
+ const isAndroidSandbox = process.env["SANDBOX"] === "android";
152
+ if (isAndroidSandbox) {
153
+ void this.getScreenInfo();
154
+ }
155
+ else {
156
+ void this.getAndWakeScreen();
157
+ }
158
+ };
159
+ this.ws.onmessage = (event) => {
160
+ void this.handleMessage(event);
161
+ };
162
+ this.ws.onclose = (event) => {
163
+ this.logger.info("❌ [ShellX] WebSocket connection closed, attempting to reconnect..." +
164
+ event.code +
165
+ " " +
166
+ this.wsUrl);
167
+ this.wsConnected = false;
168
+ this.shellxConnected = false;
169
+ this.authenticated = false;
170
+ this.config.onClose?.(event);
171
+ this.stopPing();
172
+ void this.reconnect();
173
+ };
174
+ this.ws.onerror = (error) => {
175
+ this.logger.error("❌ [ShellX] WebSocket error:", error);
176
+ this.config.onError?.(error);
177
+ reject(new Error("WebSocket connection failed"));
178
+ };
179
+ }
180
+ catch (error) {
181
+ this.logger.error("❌ [WebSocket] Initialization failed:", error);
182
+ const err = error instanceof Error ? error : new Error(String(error));
183
+ this.config.onError?.(err);
184
+ reject(err);
185
+ }
186
+ })
187
+ .catch((error) => {
188
+ this.logger.error("❌ [WebSocket] Failed to get constructor:", error);
189
+ const err = error instanceof Error ? error : new Error(String(error));
190
+ reject(err);
191
+ });
192
+ })
193
+ .catch((error) => {
194
+ this.logger.error("❌ [Auth] Device authentication failed:", error);
195
+ const err = error instanceof Error ? error : new Error(String(error));
196
+ reject(err);
197
+ });
155
198
  });
156
199
  }
157
- authenticateDevice(deviceId) {
158
- return __awaiter(this, void 0, void 0, function* () {
159
- let fallbackUrl = undefined;
160
- // 1. 优先检测本地服务
161
- try {
162
- // fetch超时实现
163
- const fetchWithTimeout = (url, options, timeout = 1000) => Promise.race([
164
- fetch(url, options),
165
- new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)),
166
- ]);
167
- const localResp = (yield fetchWithTimeout('http://127.0.0.1:9091/info', { method: 'GET', headers: { Accept: 'application/json' } }, 1000));
168
- if (localResp.ok) {
169
- const info = yield localResp.json();
170
- if (info && (info.status === 'ok' || info.status === 1) && info.uuid) {
171
- if (deviceId == undefined || deviceId == info.uuid) {
172
- const localUuid = info.uuid;
173
- fallbackUrl = `ws://127.0.0.1:9091/api/s/${localUuid}`;
174
- console.log('✅ [Auth] 本地ShellX服务可用,使用本地 /info 返回的 uuid:', localUuid, ',服务地址:', fallbackUrl);
175
- return fallbackUrl;
176
- }
200
+ async authenticateDevice() {
201
+ let fallbackUrl = undefined;
202
+ // 1. Prioritize local service detection
203
+ try {
204
+ // fetch timeout implementation
205
+ const fetchWithTimeout = (url, options, timeout = 1000) => Promise.race([
206
+ fetch(url, options),
207
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout)),
208
+ ]);
209
+ const localResp = await fetchWithTimeout("http://127.0.0.1:9091/info", { method: "GET", headers: { Accept: "application/json" } }, 1000);
210
+ if (localResp.ok) {
211
+ const info = (await localResp.json());
212
+ if (info && (info.status === "ok" || info.status === 1) && info.uuid) {
213
+ if (this.deviceId === undefined || this.deviceId === info.uuid) {
214
+ const localUuid = info.uuid;
215
+ this.deviceId = localUuid;
216
+ fallbackUrl = `ws://127.0.0.1:9091/api/s/${localUuid}`;
217
+ this.logger.info("✅ [Auth] Local ShellX service available, using local /info returned uuid:", localUuid, ", Service address:", fallbackUrl);
218
+ return fallbackUrl;
177
219
  }
178
220
  }
179
221
  }
180
- catch (e) {
181
- // 本地不可用,继续走远程
182
- }
183
- if (deviceId == undefined) {
184
- console.warn('❌ [Auth] 设备ID未设置,本地未连接USB,请设置环境变量 SHELLX_DEVICE_ID 或者 USB连接设备');
185
- return undefined;
186
- }
187
- // 2. 远程认证逻辑
188
- authKey = (0, utils_1.getEnvVar)('SHELLX_AUTH_KEY');
189
- if (!authKey) {
190
- authKey = (0, uuid_1.v4)();
191
- console.log('✅ [Auth] SHELLX_AUTH_KEY 环境变量未设置, 生成新的authKey: ' + authKey);
192
- }
193
- try {
194
- console.log('🔑 [Auth] 正在认证设备...');
195
- const featGlobal = this.getFetch();
196
- const jsonData = yield featGlobal(`https://shellx.ai/api/device/${deviceId}`, {
197
- method: 'GET',
198
- headers: {
199
- 'Content-Type': 'application/json',
200
- },
201
- });
202
- console.log('ShellX.ai设备认证响应:', jsonData);
203
- // 验证认证响应是否有效
204
- // 如果接口返回 null 或者缺少必要字段,说明设备未注册
205
- if (!jsonData || jsonData === null || !jsonData.machine || !jsonData.authenticate) {
206
- console.error('❌ [Auth] ShellX.ai设备未注册或认证信息无效');
207
- return `wss://shellx.ai/api/s/${deviceId}`;
208
- }
209
- // const jsonData = JSON.parse(data);
210
- console.log('✅ [Auth] ShellX.ai设备认证成功');
211
- console.log(`📡 [Auth] 设备ID: ${jsonData.authenticate}`);
212
- console.log(`📡 [Auth] ShellX.ai服务地址: ${jsonData.machine}`);
213
- console.log(`📡 [Auth] 注册时间: ${jsonData.registered_at}`);
214
- console.log(`📡 [Auth] 最后更新: ${jsonData.last_updated}`);
215
- return jsonData.machine + '/api/s/' + jsonData.authenticate;
222
+ }
223
+ catch {
224
+ // Local unavailable, proceeding with remote
225
+ }
226
+ if (this.deviceId === undefined) {
227
+ this.logger.warn("❌ [Auth] Device ID not set, local USB not connected, please set environment variable SHELLX_DEVICE_ID or connect USB device");
228
+ return undefined;
229
+ }
230
+ // 2. Remote authentication logic
231
+ try {
232
+ this.logger.info("🔑 [Auth] Authenticating device...");
233
+ const featGlobal = this.getFetch();
234
+ const deviceApiUrl = buildDeviceApiUrl(this.deviceId);
235
+ const jsonData = (await featGlobal(deviceApiUrl, {
236
+ method: "GET",
237
+ headers: {
238
+ "Content-Type": "application/json",
239
+ },
240
+ }));
241
+ this.logger.info("ShellX.ai device authentication response:", jsonData);
242
+ // Check if device is not registered
243
+ if (jsonData?.status === "not_registered") {
244
+ this.logger.error("❌ [Auth] Device not registered");
245
+ this.logger.error(`📝 [Auth] Status: ${jsonData.message || "Device not registered"}`);
246
+ this.logger.error("💡 [Auth] Please register this device on ShellX.ai platform first");
247
+ throw new Error(`Device "${this.deviceId}" not registered on ShellX.ai platform. Please visit https://shellx.ai to register the device first.`);
216
248
  }
217
- catch (error) {
218
- const errorMessage = error instanceof Error ? error.message : String(error);
219
- console.warn('⚠️ [Auth] ShellX.ai在线认证失败,使用本地服务:', JSON.stringify(errorMessage));
220
- return fallbackUrl;
249
+ // Verify authentication response is valid
250
+ // If interface returns null or missing required fields, device is not registered
251
+ if (!jsonData || jsonData === null || !jsonData.machine || !jsonData.authenticate) {
252
+ this.logger.error("❌ [Auth] ShellX.ai device not registered or invalid authentication information");
253
+ throw new Error(`Device "${this.deviceId}" has invalid authentication information. Please check device ID or re-register on ShellX.ai platform.`);
221
254
  }
222
- });
255
+ // const jsonData = JSON.parse(data);
256
+ this.logger.info("✅ [Auth] ShellX.ai device authentication successful");
257
+ this.logger.info(`📡 [Auth] Device ID: ${jsonData.authenticate}`);
258
+ this.logger.info(`📡 [Auth] ShellX.ai service address: ${jsonData.machine}`);
259
+ this.logger.info(`📡 [Auth] Registration time: ${jsonData.registered_at}`);
260
+ this.logger.info(`📡 [Auth] Last update: ${jsonData.last_updated}`);
261
+ this.logger.info(`📡 [Auth] Device status: ${jsonData.status || "registered"}`);
262
+ // UUID is the deviceId, no need to extract from URL
263
+ const wsUrl = `${jsonData.machine}/api/s/${this.deviceId}`;
264
+ this.logger.info(`🔗 [Auth] WebSocket URL: ${wsUrl}`);
265
+ return wsUrl;
266
+ }
267
+ catch (error) {
268
+ const errorMessage = error instanceof Error ? error.message : String(error);
269
+ this.logger.warn("⚠️ [Auth] Online authentication failed, using local service:", JSON.stringify(errorMessage));
270
+ return fallbackUrl;
271
+ }
223
272
  }
224
273
  getFetch() {
225
- return (url, options) => __awaiter(this, void 0, void 0, function* () {
226
- // 尝试使用全局fetch
227
- if (typeof globalThis.fetch !== 'undefined') {
228
- console.log(`🌐 [Fetch] 请求: ${(options === null || options === void 0 ? void 0 : options.method) || 'GET'} ${url}`);
229
- const response = yield globalThis.fetch(url, options);
230
- console.log(`📡 [Fetch] 响应: ${response.status} ${response.statusText}`);
274
+ return async (url, options) => {
275
+ // Try using global fetch
276
+ if (typeof globalThis.fetch !== "undefined") {
277
+ this.logger.info(`🌐 [Fetch] Request: ${options?.method || "GET"} ${url}`);
278
+ const response = await globalThis.fetch(url, options);
279
+ this.logger.info(`📡 [Fetch] Response: ${response.status} ${response.statusText}`);
231
280
  if (!response.ok) {
232
281
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
233
282
  }
234
283
  return response.json();
235
284
  }
236
285
  try {
237
- const { default: fetch } = yield Promise.resolve().then(() => __importStar(require('node-fetch')));
238
- console.log(`🌐 [Fetch] 请求: ${(options === null || options === void 0 ? void 0 : options.method) || 'GET'} ${url}`);
239
- const response = yield fetch(url, options);
240
- console.log(`📡 [Fetch] 响应: ${response.status} ${response.statusText}`);
286
+ const { default: fetch } = await import("node-fetch");
287
+ this.logger.info(`🌐 [Fetch] Request: ${options?.method || "GET"} ${url}`);
288
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
289
+ const response = await fetch(url, options);
290
+ this.logger.info(`📡 [Fetch] Response: ${response.status} ${response.statusText}`);
241
291
  if (!response.ok) {
242
292
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
243
293
  }
244
294
  return response.json();
245
295
  }
246
296
  catch (fetchError) {
247
- throw new Error(`Fetch not available: ${fetchError.message}`);
297
+ const errorMessage = fetchError instanceof Error ? fetchError.message : String(fetchError);
298
+ throw new Error(`Fetch not available: ${errorMessage}`);
248
299
  }
249
- });
300
+ };
250
301
  }
251
302
  /**
252
- * 等待WebSocket初始化完成
303
+ * Wait for WebSocket initialization complete
253
304
  */
254
- waitForInitialization() {
255
- return __awaiter(this, void 0, void 0, function* () {
256
- if (this.initializationPromise) {
257
- yield this.initializationPromise;
258
- }
259
- });
305
+ async waitForInitialization() {
306
+ if (this.initializationPromise) {
307
+ await this.initializationPromise;
308
+ }
260
309
  }
261
310
  /**
262
- * 发送认证消息
311
+ * Ensure connection is ready before executing device commands
312
+ * If not connected, wait for connection with timeout
313
+ *
314
+ * @param timeout - Timeout in milliseconds (default: 10000ms)
315
+ * @throws Error if connection timeout or connection failed
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * await client.ensureConnected(10000);
320
+ * console.log('Connection ready');
321
+ * ```
263
322
  */
264
- sendAuthenticationMessage() {
265
- return __awaiter(this, void 0, void 0, function* () {
266
- try {
267
- authKey = (0, utils_1.getEnvVar)('SHELLX_AUTH_KEY');
268
- if (!authKey) {
269
- authKey = (0, uuid_1.v4)();
270
- console.log('✅ [Auth] SHELLX_AUTH_KEY 环境变量未设置, 生成新的authKey: ' + authKey);
323
+ async ensureConnected(timeout = 10000) {
324
+ // If already connected, return immediately
325
+ if (this.shellxConnected && this.wsConnected) {
326
+ return;
327
+ }
328
+ this.logger.info(`⏳ [ConnectionClient] Waiting for connection to be ready (timeout: ${timeout}ms)...`);
329
+ const startTime = Date.now();
330
+ const checkInterval = 100; // Check every 100ms
331
+ return new Promise((resolve, reject) => {
332
+ const intervalId = setInterval(() => {
333
+ const elapsed = Date.now() - startTime;
334
+ // Check if connected
335
+ if (this.shellxConnected && this.wsConnected) {
336
+ clearInterval(intervalId);
337
+ this.logger.info(`✅ [ConnectionClient] Connection ready (took ${elapsed}ms)`);
338
+ resolve();
339
+ return;
271
340
  }
272
- const authMessage = { authenticate: authKey };
273
- console.log('📤 [Auth] 发送认证消息:', { authenticate: authKey });
274
- if (this.ws && this.wsConnected) {
275
- this.ws.send((0, cbor_1.encode)(authMessage));
341
+ // Check timeout
342
+ if (elapsed >= timeout) {
343
+ clearInterval(intervalId);
344
+ const errorMsg = `Connection timeout after ${timeout}ms. ` +
345
+ `Device "${this.deviceId}" is not connected. ` +
346
+ `Please check: ` +
347
+ `1. Device is online and registered on ShellX.ai ` +
348
+ `2. Network connection is stable ` +
349
+ `3. Device ID is correct`;
350
+ this.logger.error(`❌ [ConnectionClient] ${errorMsg}`);
351
+ reject(new Error(errorMsg));
352
+ return;
276
353
  }
277
- else {
278
- console.error('❌ [Auth] ShellX 未连接,无法发送认证消息');
354
+ // Log progress every 2 seconds
355
+ if (elapsed > 0 && elapsed % 2000 < checkInterval) {
356
+ this.logger.info(`⏳ [ConnectionClient] Still waiting for connection... (${elapsed}/${timeout}ms)`);
279
357
  }
280
- }
281
- catch (error) {
282
- console.error('❌ [Auth] 发送认证消息失败:', error);
283
- }
358
+ }, checkInterval);
284
359
  });
285
360
  }
286
361
  handleMessage(event) {
@@ -300,11 +375,11 @@ class ConnectionTaskClient {
300
375
  this.processServerMessage(serverMessage);
301
376
  return;
302
377
  }
303
- serverMessage = (0, cbor_1.decode)(binaryData);
378
+ serverMessage = cborDecode(binaryData);
304
379
  this.processServerMessage(serverMessage);
305
380
  }
306
381
  catch (cborError) {
307
- console.log('CBOR decode failed, trying JSON fallback:', cborError);
382
+ this.logger.info("CBOR decode failed, trying JSON fallback:", cborError);
308
383
  try {
309
384
  let textData;
310
385
  if (event.data instanceof ArrayBuffer) {
@@ -317,316 +392,359 @@ class ConnectionTaskClient {
317
392
  this.processServerMessage(serverMessage);
318
393
  }
319
394
  catch (jsonError) {
320
- console.error('Failed to parse message:', { cborError, jsonError });
395
+ this.logger.error("Failed to parse message:", { cborError, jsonError });
321
396
  }
322
397
  }
323
398
  }
324
399
  processServerMessage(message) {
325
- var _a, _b, _c, _d;
326
- //console.log('📨 [ShellX] 收到服务器消息:', message);
327
- this.authenticated = true;
328
- // 只有在认证成功后才处理其他消息并认为连接成功
329
- if (this.authenticated && !this.shellxConnected) {
330
- this.shellxConnected = true;
331
- console.log('✅ [ShellX] ShellX.ai服务连接成功!');
332
- (_b = (_a = this.config).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a);
333
- this.flushQueue();
334
- this.startPing();
335
- }
400
+ //console.log('📨 [ShellX] Received server message:', message);
336
401
  // Call user-defined message handler
337
- (_d = (_c = this.config).onMessage) === null || _d === void 0 ? void 0 : _d.call(_c, message);
402
+ this.config.onMessage?.(message);
338
403
  // Handle specific message types and resolve pending tasks
339
404
  if (message.jsonData) {
340
405
  this.handleJsonDataResponse(message.jsonData);
341
406
  }
342
407
  // Handle other server message types (hello, users, shells, etc.)
343
408
  if (message.hello !== undefined) {
344
- console.log('👋 [ShellX] 服务器问候,用户ID:', message.hello);
409
+ this.logger.info("👋 [ShellX] Server greeting, user ID:", message.hello);
345
410
  }
346
411
  if (message.pong !== undefined) {
347
- //console.log('🏓 [ShellX] 收到心跳响应:', message.pong);
412
+ //console.log('🏓 [ShellX] Heartbeat response:', message.pong);
348
413
  }
349
414
  if (message.error) {
350
- console.error('❌ [ShellX] 服务器错误:', message.error);
415
+ this.logger.error("❌ [ShellX] Server error:", message.error);
416
+ }
417
+ }
418
+ matchResponseByType(jsonData, taskType) {
419
+ switch (taskType) {
420
+ case "findElement":
421
+ if (jsonData.findElement) {
422
+ return jsonData.findElement;
423
+ }
424
+ break;
425
+ case "waitElement":
426
+ if (jsonData.waitElement) {
427
+ return jsonData.waitElement;
428
+ }
429
+ break;
430
+ case "screenShot":
431
+ if (jsonData.screenShot) {
432
+ return jsonData.screenShot;
433
+ }
434
+ break;
435
+ case "screenInfo":
436
+ if (jsonData.screenInfo) {
437
+ return jsonData.screenInfo;
438
+ }
439
+ break;
440
+ case "appList":
441
+ console.log("appList");
442
+ console.log(jsonData);
443
+ if (jsonData.appList) {
444
+ return jsonData.appList;
445
+ }
446
+ break;
447
+ case "action":
448
+ if (jsonData.action_event) {
449
+ return jsonData.action_event;
450
+ }
451
+ break;
452
+ case "promptflowx":
453
+ if (jsonData.promptflowx) {
454
+ return jsonData.promptflowx;
455
+ }
456
+ break;
457
+ case "clipboard":
458
+ if (jsonData.clipboard) {
459
+ return jsonData.clipboard;
460
+ }
461
+ break;
462
+ case "asrControl":
463
+ // ASR control doesn't expect a specific response
464
+ return { success: true };
351
465
  }
466
+ return undefined;
352
467
  }
353
468
  handleJsonDataResponse(jsonData) {
354
- // Match responses to pending tasks based on response type
355
- for (const [taskId, task] of this.pendingTasks.entries()) {
356
- let responseData = null;
357
- let shouldResolve = false;
358
- switch (task.type) {
359
- case 'findElement':
360
- if (jsonData.findElement) {
361
- responseData = jsonData.findElement;
362
- shouldResolve = true;
363
- }
364
- break;
365
- case 'waitElement':
366
- if (jsonData.waitElement) {
367
- responseData = jsonData.waitElement;
368
- shouldResolve = true;
369
- }
370
- break;
371
- case 'screenShot':
372
- if (jsonData.screenShot) {
373
- responseData = jsonData.screenShot;
374
- shouldResolve = true;
469
+ const responseTaskId = jsonData?.taskId;
470
+ if (responseTaskId) {
471
+ const task = this.pendingTasks.get(responseTaskId);
472
+ if (task) {
473
+ const responseData = this.matchResponseByType(jsonData, task.type);
474
+ if (responseData !== undefined) {
475
+ clearTimeout(task.timer);
476
+ this.pendingTasks.delete(responseTaskId);
477
+ if (task.type != "findElement") {
478
+ this.logger.info("✅ [ShellX] Response processing complete:", responseTaskId, jsonData);
375
479
  }
376
- break;
377
- case 'screenInfo':
378
- if (jsonData.screenInfo) {
379
- responseData = jsonData.screenInfo;
380
- shouldResolve = true;
381
- }
382
- break;
383
- case 'appList':
384
- if (jsonData.appList) {
385
- responseData = jsonData.appList;
386
- shouldResolve = true;
387
- }
388
- break;
389
- case 'action':
390
- if (jsonData.action_event) {
391
- responseData = jsonData.action_event;
392
- shouldResolve = true;
393
- }
394
- break;
395
- case 'actions':
396
- if (jsonData.actions) {
397
- responseData = jsonData.actions;
398
- shouldResolve = true;
399
- }
400
- break;
480
+ task.resolve(responseData);
481
+ return;
482
+ }
483
+ else {
484
+ this.logger.warn(`⚠️ [ShellX] Task ${responseTaskId} (type: ${task.type}) found but no matching response data in:`, jsonData);
485
+ }
486
+ }
487
+ else {
488
+ this.logger.warn(`⚠️ [ShellX] Received taskId ${responseTaskId} but no pending task found`);
401
489
  }
402
- if (shouldResolve) {
490
+ }
491
+ else {
492
+ this.logger.debug(`📨 [ShellX] Received response without taskId, trying fallback match:`, jsonData);
493
+ }
494
+ // Fallback to legacy matching when taskId is missing or not tracked
495
+ for (const [taskId, task] of this.pendingTasks.entries()) {
496
+ const responseData = this.matchResponseByType(jsonData, task.type);
497
+ if (responseData !== undefined) {
403
498
  clearTimeout(task.timer);
404
499
  this.pendingTasks.delete(taskId);
405
- // 始终 resolve,保持原始响应数据结构不变
500
+ this.logger.info(`✅ [ShellX] Fallback match: completed task ${taskId} (type: ${task.type})`);
406
501
  task.resolve(responseData);
407
- break; // Only resolve the first matching task
502
+ break;
408
503
  }
409
504
  }
410
505
  }
411
- sendMessage(message, taskType, timeout) {
412
- return __awaiter(this, void 0, void 0, function* () {
413
- return new Promise((resolve, reject) => {
414
- const taskId = (0, uuid_1.v4)();
415
- if (taskType) {
416
- const timer = setTimeout(() => {
417
- this.pendingTasks.delete(taskId);
418
- // 超时时返回 undefined 而不是 reject
419
- console.log(`⏰ [ShellX] 任务超时: ${taskType}, 返回 undefined`);
420
- resolve(undefined);
421
- }, timeout ? timeout : this.config.timeout);
422
- this.pendingTasks.set(taskId, {
423
- resolve,
424
- reject,
425
- timer,
426
- type: taskType
427
- });
428
- console.log(`📋 [ShellX] 创建任务: ${taskId}, 类型: ${taskType}}`);
429
- }
430
- if (this.shellxConnected && this.ws) {
431
- try {
432
- console.log('📤 [ShellX] 发送消息:', JSON.stringify(message));
433
- this.ws.send((0, cbor_1.encode)(message));
434
- if (!taskType)
435
- resolve(undefined); // For fire-and-forget messages
436
- }
437
- catch (error) {
438
- if (taskType) {
439
- this.pendingTasks.delete(taskId);
440
- }
441
- // 发送失败时返回 undefined 而不是 reject
442
- console.log(`❌ [ShellX] 发送消息失败,返回 undefined:`, error);
443
- resolve(undefined);
444
- }
445
- }
446
- else {
447
- console.log('⏳ [ShellX] 连接未就绪,消息已加入队列');
448
- this.reconnect();
449
- this.messageQueue.push(message);
450
- if (!taskType)
451
- resolve(undefined);
452
- }
453
- });
454
- });
506
+ async sendMessage(message, taskType) {
507
+ if (!taskType) {
508
+ taskType = "oneway";
509
+ }
510
+ const { promise } = await this.sendMessageWithTaskId(message, taskType);
511
+ return await promise;
455
512
  }
456
513
  // ===== PROTOCOL-AWARE TASK METHODS =====
457
514
  /**
458
515
  * Find UI elements on the screen
459
516
  */
460
- findElement(selector, options) {
461
- return __awaiter(this, void 0, void 0, function* () {
462
- const findAction = {
463
- type: 'find',
464
- selector,
465
- options,
466
- };
467
- return this.sendMessage({ findElement: findAction }, 'findElement');
468
- });
517
+ async findElement(selector, options) {
518
+ const findAction = {
519
+ type: "find",
520
+ selector,
521
+ options,
522
+ };
523
+ return this.sendMessage({ findElement: findAction }, "findElement");
469
524
  }
470
525
  /**
471
526
  * Wait for UI element to appear
472
527
  */
473
- waitElement(selector, options) {
474
- return __awaiter(this, void 0, void 0, function* () {
475
- const waitAction = {
476
- type: 'wait',
477
- selector,
478
- options,
479
- };
480
- return this.sendMessage({ waitElement: waitAction }, 'waitElement');
481
- });
528
+ async waitElement(selector, options) {
529
+ const waitAction = {
530
+ type: "wait",
531
+ selector,
532
+ options,
533
+ };
534
+ return this.sendMessage({ waitElement: waitAction }, "waitElement");
482
535
  }
483
536
  /**
484
537
  * Take a screenshot
538
+ * @deprecated Use takeScreenshot() instead for consistency
485
539
  */
486
- screenShot(options) {
487
- return __awaiter(this, void 0, void 0, function* () {
488
- return this.sendMessage({
489
- screenShot: options || {
490
- format: 'png',
491
- quality: 30,
492
- scale: 1.0,
493
- region: {
494
- left: 0,
495
- top: 0,
496
- width: 1080,
497
- height: 2340,
498
- },
540
+ async screenShot(options) {
541
+ return this.takeScreenshot(options);
542
+ }
543
+ /**
544
+ * Take a screenshot
545
+ */
546
+ async takeScreenshot(options) {
547
+ return this.sendMessage({
548
+ screenShot: options || {
549
+ format: "png",
550
+ quality: 30,
551
+ scale: 1.0,
552
+ region: {
553
+ left: 0,
554
+ top: 0,
555
+ width: 1080,
556
+ height: 2340,
499
557
  },
500
- }, 'screenShot');
501
- });
558
+ },
559
+ }, "screenShot");
502
560
  }
503
561
  /**
504
562
  * Get screen information
505
563
  */
506
- getScreenInfo() {
507
- return __awaiter(this, void 0, void 0, function* () {
508
- return this.sendMessage({ screenInfo: {} }, 'screenInfo');
509
- });
564
+ async getScreenInfo() {
565
+ return this.sendMessage({ screenInfo: { keepScreenOn: false, wakeApp: true } }, "screenInfo");
510
566
  }
511
567
  /**
512
- * Get list of installed apps
568
+ * Get screen information
569
+ * @deprecated Use wakeScreenAndGetInfo() instead for clarity
513
570
  */
514
- getAppList(options) {
515
- return __awaiter(this, void 0, void 0, function* () {
516
- return this.sendMessage({ appList: options || {} }, 'appList');
517
- });
571
+ async getAndWakeScreen() {
572
+ return this.wakeScreenAndGetInfo();
518
573
  }
519
574
  /**
520
- * Get specific app information
575
+ * Wake screen and get screen information
521
576
  */
522
- getAppInfo(packageName) {
523
- return __awaiter(this, void 0, void 0, function* () {
524
- return this.sendMessage({ appInfo: { packageName } }, 'appInfo');
525
- });
577
+ async wakeScreenAndGetInfo() {
578
+ return this.sendMessage({ screenInfo: { keepScreenOn: true, wakeApp: true } }, "screenInfo");
526
579
  }
527
580
  /**
528
581
  * Execute an action sequence
529
582
  */
530
- executeAction(actionSequence, taskId, timeeout) {
531
- return __awaiter(this, void 0, void 0, function* () {
532
- return this.sendMessageWithTaskId({ actions: actionSequence }, 'action', taskId, timeeout);
533
- });
583
+ async executeAction(actionSequence, taskId, timeout) {
584
+ const { promise } = await this.sendMessageWithTaskId({ actions: actionSequence }, "action", { taskId, timeout });
585
+ return await promise;
534
586
  }
535
587
  /**
536
588
  * Execute promptflow actions
537
589
  */
538
- executePromptFlow(actionSequence) {
539
- return __awaiter(this, void 0, void 0, function* () {
540
- return this.sendMessage({ promptflowx: actionSequence });
541
- });
590
+ async executePromptFlow(actionSequence) {
591
+ return this.sendMessage({ promptflowx: actionSequence });
542
592
  }
543
593
  /**
544
594
  * Listen for display changes
545
595
  */
546
- screenChange(options) {
547
- return __awaiter(this, void 0, void 0, function* () {
548
- return this.sendMessage({
549
- screenChange: options || { enable: true },
550
- });
596
+ async screenChange(options) {
597
+ return this.sendMessage({
598
+ screenChange: options || { enable: true },
551
599
  });
552
600
  }
553
601
  /**
554
602
  * Switch to a different node
555
603
  */
556
- switchNode(nodeId) {
557
- return __awaiter(this, void 0, void 0, function* () {
558
- return this.sendMessage({ switchNode: nodeId });
559
- });
604
+ async switchNode(nodeId) {
605
+ return this.sendMessage({ switchNode: nodeId });
560
606
  }
561
607
  /**
562
608
  * Send authentication data
563
609
  */
564
- authenticate(authData) {
565
- return __awaiter(this, void 0, void 0, function* () {
566
- return this.sendMessage({ authenticate: authData });
567
- });
610
+ async authenticate(authData) {
611
+ return this.sendMessage({ authenticate: authData });
568
612
  }
569
613
  /**
570
614
  * Set user name
571
615
  */
572
- setName(name) {
573
- return __awaiter(this, void 0, void 0, function* () {
574
- return this.sendMessage({ setName: name });
575
- });
616
+ async setName(name) {
617
+ return this.sendMessage({ setName: name });
576
618
  }
577
619
  /**
578
620
  * Send chat message
579
621
  */
580
- sendChat(message) {
581
- return __awaiter(this, void 0, void 0, function* () {
582
- return this.sendMessage({ chat: message });
583
- });
622
+ async sendChat(message) {
623
+ return this.sendMessage({ chat: message });
624
+ }
625
+ /**
626
+ * Start ASR (Automatic Speech Recognition)
627
+ */
628
+ async startAsr() {
629
+ return this.sendMessage({ asrControl: { action: "start_asr" } }, "oneway");
630
+ }
631
+ /**
632
+ * Stop ASR (Automatic Speech Recognition)
633
+ */
634
+ async stopAsr() {
635
+ return this.sendMessage({ asrControl: { action: "stop_asr" } }, "oneway");
636
+ }
637
+ /**
638
+ * Speak text using TTS (Text-to-Speech)
639
+ */
640
+ async speak(text) {
641
+ return this.sendMessage({ ttsControl: { action: "tts_speak", text } }, "oneway");
642
+ }
643
+ /**
644
+ * Stop TTS (Text-to-Speech)
645
+ */
646
+ async stopTts() {
647
+ return this.sendMessage({ ttsControl: { action: "tts_stop" } }, "oneway");
584
648
  }
585
649
  /**
586
650
  * Send raw message (for custom integrations)
587
651
  */
588
- sendRawMessage(message) {
589
- return __awaiter(this, void 0, void 0, function* () {
590
- return this.sendMessage(message);
591
- });
652
+ async sendRawMessage(message) {
653
+ // Auto-detect task type from message content
654
+ const taskType = this.detectTaskType(message);
655
+ return this.sendMessage(message, taskType);
592
656
  }
593
657
  /**
594
- * 发送消息并返回 taskId,允许手动控制任务完成
595
- * @param message 要发送的消息
596
- * @param taskType 任务类型
597
- * @returns Promise<{taskId: string, promise: Promise<any>}>
658
+ * Detect task type from message content
598
659
  */
599
- sendMessageWithTaskId(message, taskType, definedTaskId, timeout) {
660
+ detectTaskType(message) {
661
+ if (message.findElement)
662
+ return "findElement";
663
+ if (message.waitElement)
664
+ return "waitElement";
665
+ if (message.screenShot)
666
+ return "screenShot";
667
+ if (message.screenInfo)
668
+ return "screenInfo";
669
+ if (message.appList)
670
+ return "appList";
671
+ if (message.action)
672
+ return "action";
673
+ if (message.actions)
674
+ return "action";
675
+ if (message.promptflowx)
676
+ return "promptflowx";
677
+ if (message.asrControl)
678
+ return "asrControl";
679
+ // Oneway messages (fire-and-forget, no response expected)
680
+ if (message.ttsControl)
681
+ return "oneway";
682
+ if (message.screenChange)
683
+ return "oneway";
684
+ if (message.switchNode)
685
+ return "oneway";
686
+ if (message.authenticate)
687
+ return "oneway";
688
+ if (message.setName)
689
+ return "oneway";
690
+ if (message.chat)
691
+ return "oneway";
692
+ if (message.ping)
693
+ return "oneway";
694
+ // Default to oneway for unknown message types
695
+ return "oneway";
696
+ }
697
+ /**
698
+ * Send message and return taskId
699
+ * @param message Message to send
700
+ * @param taskType Task type
701
+ * @param options Optional parameters { timeout, taskId }
702
+ * @returns Promise<{taskId: string, promise: Promise<T>>}
703
+ */
704
+ async sendMessageWithTaskId(message, taskType, options) {
600
705
  return new Promise((resolve) => {
706
+ const { timeout, taskId: definedTaskId } = options || {};
601
707
  let taskId = definedTaskId;
602
708
  if (taskId == null) {
603
- taskId = (0, uuid_1.v4)();
709
+ taskId = uuidv4();
604
710
  }
711
+ this.logger.info(`📋 [ShellX] Created task: ${taskId}, Task type: ${taskType}`);
712
+ this.logger.info(message);
713
+ const outgoingMessage = taskId ? { ...message, taskId } : message;
605
714
  if (isPendingTask(taskType)) {
606
715
  const timer = setTimeout(() => {
607
716
  if (taskId != null) {
717
+ const task = this.pendingTasks.get(taskId);
718
+ if (task) {
719
+ // Create a timeout result based on task type
720
+ const timeoutResult = this.createTimeoutResult(taskType, timeout ?? this.config.timeout);
721
+ // Resolve with timeout result instead of rejecting
722
+ task.resolve(timeoutResult);
723
+ }
608
724
  this.pendingTasks.delete(taskId);
609
725
  }
610
- console.log(`⏰ [ShellX] 任务超时: ${taskId}, 类型: ${taskType}`);
611
- }, timeout ? timeout : this.config.timeout);
726
+ this.logger.info(`⏰ [ShellX] Task timeout: ${taskId}, Task type: ${taskType}`);
727
+ }, timeout ?? this.config.timeout);
612
728
  this.pendingTasks.set(taskId, {
613
729
  resolve: () => { },
614
730
  reject: () => { },
615
731
  timer,
616
732
  type: taskType,
617
733
  });
618
- console.log(`📋 [ShellX] 创建可手动控制的任务: ${taskId}, 类型: ${taskType}}`);
734
+ this.logger.info(`📋 [ShellX] Created manually controllable task: ${taskId}, Task type: ${taskType}}`);
619
735
  }
620
736
  if (this.shellxConnected && this.ws) {
621
737
  try {
622
- console.log(Date.now() + ' 📤 [ShellX] 发送消息:', JSON.stringify(message));
623
- this.ws.send((0, cbor_1.encode)(message));
738
+ this.logger.info(" 📤 [ShellX] Sending message:", JSON.stringify(outgoingMessage));
739
+ this.ws.send(cborEncode(outgoingMessage));
624
740
  const promise = new Promise((promiseResolve, promiseReject) => {
625
741
  if (isPendingTask(taskType)) {
626
742
  if (taskId != null) {
627
743
  const task = this.pendingTasks.get(taskId);
628
744
  if (task) {
745
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
629
746
  task.resolve = promiseResolve;
747
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
630
748
  task.reject = promiseReject;
631
749
  }
632
750
  }
@@ -641,12 +759,14 @@ class ConnectionTaskClient {
641
759
  if (isPendingTask(taskType)) {
642
760
  this.pendingTasks.delete(taskId);
643
761
  }
644
- resolve({ taskId, promise: Promise.reject(error) });
762
+ const err = error instanceof Error ? error : new Error(String(error));
763
+ resolve({ taskId, promise: Promise.reject(err) });
645
764
  }
646
765
  }
647
766
  else {
648
- console.log('⏳ [ShellX] 连接未就绪,消息已加入队列');
649
- this.messageQueue.push(message);
767
+ this.logger.info("⏳ [ShellX] Connection not ready, message queued");
768
+ this.messageQueue.push(outgoingMessage);
769
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
650
770
  resolve({ taskId, promise: Promise.resolve(undefined) });
651
771
  }
652
772
  });
@@ -654,10 +774,10 @@ class ConnectionTaskClient {
654
774
  // ===== CONNECTION MANAGEMENT =====
655
775
  startPing() {
656
776
  this.stopPing();
657
- console.log('🏓 [ShellX] 开始心跳检测...');
777
+ this.logger.info("🏓 [ShellX] Starting heartbeat detection...");
658
778
  this.pingIntervalId = setInterval(() => {
659
779
  if (this.ws && this.shellxConnected) {
660
- this.ws.send((0, cbor_1.encode)({ ping: Date.now() }));
780
+ this.ws.send(cborEncode({ ping: Date.now() }));
661
781
  }
662
782
  }, this.config.pingInterval);
663
783
  }
@@ -667,32 +787,37 @@ class ConnectionTaskClient {
667
787
  this.pingIntervalId = null;
668
788
  }
669
789
  }
670
- reconnect() {
671
- return __awaiter(this, void 0, void 0, function* () {
672
- var _a, _b;
673
- // TODO: 本地的需要重连
674
- console.log(`🔄 [ShellX] 重连中... (${this.reconnectAttempts}/${this.config.reconnectMaxAttempts})` +
675
- this.wsUrl);
676
- if (this.reconnectAttempts >= this.config.reconnectMaxAttempts) {
677
- console.error('❌ [ShellX] 重连失败,达到最大重连次数');
678
- (_b = (_a = this.config).onReconnectFailed) === null || _b === void 0 ? void 0 : _b.call(_a);
679
- return;
680
- }
681
- this.reconnectAttempts++;
682
- yield (0, utils_1.wait)(this.config.reconnectInterval * this.reconnectAttempts);
683
- this.init();
684
- });
790
+ async reconnect() {
791
+ // TODO: Local needs reconnection
792
+ this.logger.info(`🔄 [ShellX] Reconnecting... (${this.reconnectAttempts}/${this.config.reconnectMaxAttempts})` +
793
+ this.wsUrl);
794
+ if (this.reconnectAttempts >= this.config.reconnectMaxAttempts) {
795
+ this.logger.error("❌ [ShellX] Reconnection failed, max attempts reached");
796
+ this.config.onReconnectFailed?.();
797
+ return;
798
+ }
799
+ this.reconnectAttempts++;
800
+ // Exponential backoff strategy: baseInterval * 2^(attempts-1), with maximum delay cap and random jitter
801
+ const baseInterval = this.config.reconnectInterval || 1000;
802
+ const maxDelay = 30000; // Maximum delay 30 seconds
803
+ const exponentialDelay = baseInterval * Math.pow(2, this.reconnectAttempts - 1);
804
+ const delay = Math.min(exponentialDelay, maxDelay);
805
+ // Add random jitter (±20%) to avoid multiple clients reconnecting simultaneously
806
+ const jitter = delay * 0.2 * (Math.random() * 2 - 1);
807
+ const finalDelay = Math.max(0, delay + jitter);
808
+ await wait(Math.floor(finalDelay));
809
+ void this.init();
685
810
  }
686
811
  flushQueue() {
687
812
  while (this.messageQueue.length > 0 && this.ws && this.shellxConnected) {
688
813
  const message = this.messageQueue.shift();
689
814
  if (message) {
690
815
  try {
691
- console.log('flushQueue Sending message:', message);
692
- this.ws.send((0, cbor_1.encode)(message));
816
+ this.logger.info("flushQueue Sending message:", message);
817
+ this.ws.send(cborEncode(message));
693
818
  }
694
819
  catch (error) {
695
- console.error('Failed to send queued message:', error);
820
+ this.logger.error("Failed to send queued message:", error);
696
821
  // Re-queue the message if sending fails
697
822
  this.messageQueue.unshift(message);
698
823
  break;
@@ -701,11 +826,10 @@ class ConnectionTaskClient {
701
826
  }
702
827
  }
703
828
  close() {
704
- var _a;
705
829
  this.shellxConnected = false;
706
830
  this.wsConnected = false;
707
831
  this.authenticated = false;
708
- (_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
832
+ this.ws?.close();
709
833
  this.pendingTasks.clear();
710
834
  this.messageQueue = [];
711
835
  this.stopPing();
@@ -720,6 +844,12 @@ class ConnectionTaskClient {
720
844
  get isAuthenticated() {
721
845
  return this.authenticated;
722
846
  }
847
+ /**
848
+ * Get the authenticated device ID (UUID)
849
+ */
850
+ getDeviceId() {
851
+ return this.deviceId;
852
+ }
723
853
  get pendingTaskCount() {
724
854
  return this.pendingTasks.size;
725
855
  }
@@ -727,36 +857,128 @@ class ConnectionTaskClient {
727
857
  return this.messageQueue.length;
728
858
  }
729
859
  /**
730
- * 设置关联的 ShellX 实例
860
+ * Get current execution log
861
+ */
862
+ getExecutionLogs() {
863
+ return [...this.executionLogsRef];
864
+ }
865
+ /**
866
+ * Clear current execution log
867
+ */
868
+ clearExecutionLogs() {
869
+ this.executionLogsRef.length = 0;
870
+ }
871
+ /**
872
+ * Manually append an execution log
873
+ */
874
+ appendExecutionLog(log) {
875
+ this.logger.info("Append log:", log);
876
+ this.executionLogsRef.push(log);
877
+ }
878
+ /**
879
+ * Set associated ShellX instance
731
880
  */
732
881
  setShellX(shellx) {
733
882
  this.shellx = shellx;
734
- console.log('🔗 [ShellX] 已关联 ShellX 实例');
883
+ this.logger.info("🔗 [ShellX] ShellX instance associated");
884
+ }
885
+ /**
886
+ * Create a timeout result object based on task type
887
+ * @param taskType Type of the task that timed out
888
+ * @param timeoutMs Timeout duration in milliseconds
889
+ * @returns A result object with success: false
890
+ */
891
+ createTimeoutResult(taskType, timeoutMs) {
892
+ const baseResult = {
893
+ success: false,
894
+ error: `Task timed out after ${timeoutMs}ms`,
895
+ duration: timeoutMs,
896
+ timestamp: Date.now(),
897
+ };
898
+ // Return type-specific result based on task type
899
+ switch (taskType) {
900
+ case "action":
901
+ // For action tasks, return a basic result
902
+ return baseResult;
903
+ case "screenShot":
904
+ return {
905
+ ...baseResult,
906
+ imageData: "",
907
+ imagePath: "",
908
+ format: "png",
909
+ };
910
+ case "screenInfo":
911
+ return {
912
+ ...baseResult,
913
+ width: 0,
914
+ height: 0,
915
+ density: 0,
916
+ screenOn: false,
917
+ screenUnlocked: false,
918
+ };
919
+ case "findElement":
920
+ case "waitElement":
921
+ return {
922
+ ...baseResult,
923
+ elements: [],
924
+ };
925
+ case "appList":
926
+ return {
927
+ ...baseResult,
928
+ apps: [],
929
+ totalCount: 0,
930
+ systemAppCount: 0,
931
+ userAppCount: 0,
932
+ };
933
+ case "input":
934
+ return baseResult;
935
+ case "swipe":
936
+ return {
937
+ ...baseResult,
938
+ fromX: 0,
939
+ fromY: 0,
940
+ toX: 0,
941
+ toY: 0,
942
+ };
943
+ case "press":
944
+ return {
945
+ ...baseResult,
946
+ key: "",
947
+ };
948
+ case "clipboard":
949
+ return {
950
+ ...baseResult,
951
+ text: "",
952
+ };
953
+ default:
954
+ // Default fallback result
955
+ return baseResult;
956
+ }
735
957
  }
736
958
  /**
737
- * 获取关联的 ShellX 实例
959
+ * Get associated ShellX instance
738
960
  */
739
961
  getShellX() {
740
962
  return this.shellx;
741
963
  }
742
964
  /**
743
- * 手动完成指定 taskId 的任务
744
- * @param taskId 任务ID
745
- * @param result 任务结果
746
- * @param success 是否成功
965
+ * Manually complete task by taskId
966
+ * @param taskId Task ID
967
+ * @param result Task result
968
+ * @param success Success status
747
969
  */
748
970
  completeTask(taskId, result, success = true) {
749
971
  const task = this.pendingTasks.get(taskId);
750
972
  if (!task) {
751
- console.log(`⚠️ [ShellX] 未找到任务: ${taskId}`);
973
+ this.logger.info(`⚠️ [ShellX] Task not found: ${taskId}`);
752
974
  return false;
753
975
  }
754
- console.log(`✅ [ShellX] 手动完成任务: ${taskId}, 类型: ${task.type}, 成功: ${success}`);
755
- // 清除超时定时器
976
+ this.logger.info(`✅ [ShellX] Manually completed task: ${taskId}, Task type: ${task.type}, Success: ${success}`);
977
+ // Clear timeout timer
756
978
  clearTimeout(task.timer);
757
- // 从待处理任务中移除
979
+ // Remove from pending tasks
758
980
  this.pendingTasks.delete(taskId);
759
- // 根据成功状态调用相应的回调
981
+ // Call appropriate callback based on success status
760
982
  if (success) {
761
983
  task.resolve(result || { success: true, taskId });
762
984
  }
@@ -766,13 +988,13 @@ class ConnectionTaskClient {
766
988
  return true;
767
989
  }
768
990
  /**
769
- * 获取所有待处理任务的ID列表
991
+ * Get all pending task IDs
770
992
  */
771
993
  getPendingTaskIds() {
772
994
  return Array.from(this.pendingTasks.keys());
773
995
  }
774
996
  /**
775
- * 获取指定任务的信息
997
+ * Get task info by taskId
776
998
  */
777
999
  getTaskInfo(taskId) {
778
1000
  const task = this.pendingTasks.get(taskId);
@@ -780,14 +1002,14 @@ class ConnectionTaskClient {
780
1002
  return null;
781
1003
  }
782
1004
  return {
783
- type: task.type
1005
+ type: task.type,
784
1006
  };
785
1007
  }
786
1008
  /**
787
- * 批量完成指定类型的任务
788
- * @param taskType 任务类型
789
- * @param result 任务结果
790
- * @param success 是否成功
1009
+ * Batch complete tasks by type
1010
+ * @param taskType Task type
1011
+ * @param result Task result
1012
+ * @param success Success status
791
1013
  */
792
1014
  completeTasksByType(taskType, result, success = true) {
793
1015
  const taskIds = this.getPendingTaskIds();
@@ -799,12 +1021,16 @@ class ConnectionTaskClient {
799
1021
  completedCount++;
800
1022
  }
801
1023
  }
802
- console.log(`📦 [ShellX] 批量完成 ${completedCount} ${taskType} 类型的任务`);
1024
+ this.logger.info(`📦 [ShellX] Batch completed ${completedCount} tasks of type ${taskType}`);
803
1025
  return completedCount;
804
1026
  }
805
1027
  }
806
- exports.ConnectionTaskClient = ConnectionTaskClient;
807
- exports.default = ConnectionTaskClient;
808
- var shellx_1 = require("./shellx");
809
- Object.defineProperty(exports, "createShellXWithShellMonitoring", { enumerable: true, get: function () { return shellx_1.createShellXWithShellMonitoring; } });
810
- Object.defineProperty(exports, "ShellX", { enumerable: true, get: function () { return shellx_1.ShellX; } });
1028
+ export default ConnectionClient;
1029
+ export { ShellX } from "./shellx.js";
1030
+ // Error handling exports
1031
+ export { createErrorResponse, createOperationResult, ErrorCode, ShellXError, ConnectionError, OperationError, ElementError, ValidationError, } from "./errors.js";
1032
+ // Error handler utilities
1033
+ export { extractErrorMessage, createErrorResult, createSuccessResult, handleOperation, handleOperationWithRetry, validateRequired, validateOneOfRequired, wrapErrorWithContext, } from "./error-handler.js";
1034
+ // Domain management exports
1035
+ export { buildDeviceApiUrl, buildDeviceConfigUrl, buildDeviceRegisterUrl, buildTokenVerifyUrl, buildTokenRefreshUrl, getApiBaseUrl, getWsServerUrl, getCurrentDomainInfo, createDomainConfig, isChineseUser, } from "./domain-manager.js";
1036
+ //# sourceMappingURL=index.js.map