shellx-ai 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,335 @@
1
+ # ShellX
2
+
3
+ [![npm version](https://badge.fury.io/js/shellx.svg)](https://badge.fury.io/js/shellx)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ **ShellX** is a powerful automation framework for Android device control and UI automation. It provides seamless shell command execution, element finding, screen capture, and automated task execution through an intelligent WebSocket-based architecture.
7
+
8
+ ## ✨ Features
9
+
10
+ - 🔐 **Zero-Configuration Setup** - Automatic authentication and connection management
11
+ - 📱 **Android Device Control** - Execute shell commands with real-time output monitoring
12
+ - 🎯 **Smart UI Automation** - Advanced element finding with retry logic and multiple strategies
13
+ - 📸 **Screen Operations** - Screenshots, screen info, and visual change detection
14
+ - 🔄 **Intelligent Fallback** - Automatic switching between cloud and local services
15
+ - 🛡️ **Robust Error Handling** - Comprehensive error recovery and diagnostics
16
+ - 🧠 **Shell Output Monitoring** - Real-time command output processing and analysis
17
+ - 📊 **Rich Diagnostics** - Built-in element analysis and debugging tools
18
+
19
+ ## 🚀 Quick Start
20
+
21
+ ### Installation
22
+
23
+ ```bash
24
+ npm install shellx-ai
25
+ ```
26
+
27
+ ### Basic Setup
28
+
29
+ ```typescript
30
+ import { createShellXWithShellMonitoring } from 'shellx';
31
+
32
+ // Set your authentication key
33
+ process.env.SHELLX_AUTH_KEY = 'your-auth-key';
34
+
35
+ // Initialize ShellX with automatic authentication
36
+ const { client, shellx } = await createShellXWithShellMonitoring();
37
+
38
+ // Start automating!
39
+ await shellx.executeShellCommand('getprop ro.build.version.release');
40
+ await shellx.clickByText('Settings');
41
+ ```
42
+
43
+ ### Environment Setup
44
+
45
+ Set your authentication key as an environment variable:
46
+
47
+ ```bash
48
+ # Linux/macOS
49
+ export SHELLX_AUTH_KEY="your-auth-key"
50
+
51
+ # Windows
52
+ set SHELLX_AUTH_KEY=your-auth-key
53
+ ```
54
+
55
+ ## 📱 Common Use Cases
56
+
57
+ ### 1. Shell Command Execution
58
+
59
+ ```typescript
60
+ // Execute shell commands with real-time output
61
+ const result = await shellx.executeShellCommand('pm list packages', {
62
+ title: 'List installed packages',
63
+ timeout: 10000,
64
+ onOutput: (output) => {
65
+ console.log('Real-time output:', output);
66
+ }
67
+ });
68
+
69
+ console.log('Command result:', result.output);
70
+ console.log('Success:', result.success);
71
+ console.log('Duration:', result.duration, 'ms');
72
+ ```
73
+
74
+ ### 2. UI Element Operations
75
+
76
+ ```typescript
77
+ // Smart element finding with retry logic
78
+ const elements = await shellx.findElementsWithRetry({
79
+ className: 'android.widget.TextView',
80
+ textContains: 'Settings'
81
+ }, 3, 1000, {
82
+ maxResults: 10,
83
+ visibleOnly: true
84
+ });
85
+
86
+ // Print detailed element information
87
+ shellx.printElementsInfo(elements, 'Found Settings Elements');
88
+
89
+ // Click by text content
90
+ await shellx.clickByText('Wi-Fi', false);
91
+
92
+ // Input text with options
93
+ await shellx.inputText({
94
+ resourceId: 'search_field'
95
+ }, 'Hello ShellX', {
96
+ clear: true,
97
+ hideKeyboard: true
98
+ });
99
+ ```
100
+
101
+ ### 3. Screen Operations
102
+
103
+ ```typescript
104
+ // Capture screenshot with details
105
+ const screenshot = await shellx.captureScreen({
106
+ format: 'png',
107
+ quality: 90,
108
+ saveInfo: true
109
+ });
110
+
111
+ // Swipe gestures
112
+ await shellx.swipe('up', 400, 800); // direction, distance, duration
113
+ await shellx.swipe('left', 300, 600);
114
+ ```
115
+
116
+ ### 4. App Automation Workflow
117
+
118
+ ```typescript
119
+ // Login automation example
120
+ await shellx.performLogin(
121
+ { resourceId: 'username_field' }, // username selector
122
+ { resourceId: 'password_field' }, // password selector
123
+ { resourceId: 'login_button' }, // login button selector
124
+ 'user@example.com', // username
125
+ 'password123' // password
126
+ );
127
+
128
+ // Navigation workflow
129
+ await shellx.navigateByPath([
130
+ 'Settings',
131
+ 'Network & internet',
132
+ 'Wi-Fi'
133
+ ]);
134
+ ```
135
+
136
+ ### 5. Advanced Element Finding
137
+
138
+ ```typescript
139
+ // Wait for any of multiple elements to appear
140
+ const result = await shellx.waitForAnyElement([
141
+ { textContains: 'OK' },
142
+ { textContains: 'Cancel' },
143
+ { textContains: 'Skip' }
144
+ ], 10000);
145
+
146
+ if (result) {
147
+ console.log(`Found element: ${result.element.text} at index ${result.selectorIndex}`);
148
+ }
149
+
150
+ // Scroll to find element
151
+ const element = await shellx.scrollToFindElement({
152
+ textContains: 'Privacy Settings'
153
+ }, 5, 'down');
154
+ ```
155
+
156
+ ### 6. Batch Shell Commands
157
+
158
+ ```typescript
159
+ // Execute multiple commands in sequence
160
+ const commands = [
161
+ { command: 'am force-stop com.example.app', title: 'Stop app' },
162
+ { command: 'am start -n com.example.app/.MainActivity', title: 'Start app' },
163
+ { command: 'input keyevent KEYCODE_HOME', title: 'Go home', waitAfterMs: 1000 }
164
+ ];
165
+
166
+ const results = await shellx.executeShellCommands(commands, {
167
+ continueOnError: true,
168
+ timeout: 5000
169
+ });
170
+ ```
171
+
172
+ ### 7. Device Information
173
+
174
+ ```typescript
175
+ // Get comprehensive device information
176
+ const deviceInfo = await shellx.getDeviceInfo();
177
+ deviceInfo.forEach((info, index) => {
178
+ console.log(`${index + 1}. ${info.output}`);
179
+ });
180
+
181
+ // ADB commands with enhanced output
182
+ const result = await shellx.adbCommand('shell dumpsys battery', {
183
+ title: 'Get battery status',
184
+ successPattern: /level: \d+/,
185
+ errorPattern: /error|failed/i
186
+ });
187
+ ```
188
+
189
+ ## 🎯 ShellX Class API
190
+
191
+ ### Core Methods
192
+
193
+ | Method | Description | Example |
194
+ |--------|-------------|---------|
195
+ | `executeShellCommand(cmd, opts?)` | Execute shell command with monitoring | `shellx.executeShellCommand('ls -la')` |
196
+ | `clickByText(text, exact?)` | Click element by text content | `shellx.clickByText('OK', true)` |
197
+ | `inputText(selector, text, opts?)` | Input text into element | `shellx.inputText({resourceId: 'field'}, 'text')` |
198
+ | `findElementWithRetry(selector, retries?, delay?)` | Find single element with retry | `shellx.findElementWithRetry({text: 'Button'})` |
199
+ | `findElementsWithRetry(selector, retries?, delay?, opts?)` | Find multiple elements with retry | `shellx.findElementsWithRetry({className: 'TextView'})` |
200
+ | `swipe(direction, distance?, duration?)` | Perform swipe gesture | `shellx.swipe('up', 400, 800)` |
201
+ | `captureScreen(opts?)` | Take screenshot | `shellx.captureScreen({saveInfo: true})` |
202
+
203
+ ### Utility Methods
204
+
205
+ | Method | Description |
206
+ |--------|-------------|
207
+ | `printElementInfo(element, index?)` | Print detailed element information |
208
+ | `printElementsInfo(elements, title?)` | Print information for multiple elements |
209
+ | `waitForAnyElement(selectors, timeout?)` | Wait for any of multiple elements |
210
+ | `scrollToFindElement(selector, maxScrolls?, direction?)` | Scroll to find element |
211
+ | `performLogin(userSel, passSel, btnSel, user, pass)` | Automated login flow |
212
+ | `navigateByPath(textPath)` | Navigate through UI by text path |
213
+ | `getDeviceInfo()` | Get comprehensive device information |
214
+
215
+ ## 🛠️ Configuration
216
+
217
+ ### Environment Variables
218
+
219
+ ```bash
220
+ # Required: Your ShellX authentication key
221
+ SHELLX_AUTH_KEY=your-auth-key
222
+
223
+ # Optional: Timeout settings
224
+ SHELLX_TIMEOUT=20000
225
+ SHELLX_RETRY_ATTEMPTS=3
226
+ ```
227
+
228
+ ### Connection Options
229
+
230
+ ```typescript
231
+ const { client, shellx } = await createShellXWithShellMonitoring({
232
+ timeout: 15000,
233
+ reconnectMaxAttempts: 3,
234
+ onOpen: () => console.log('Connected'),
235
+ onClose: (event) => console.log('Disconnected'),
236
+ onError: (error) => console.error('Error:', error),
237
+ onMessage: (data) => {
238
+ // Handle custom messages
239
+ }
240
+ });
241
+ ```
242
+
243
+ ## 🔧 Advanced Features
244
+
245
+ ### Shell Output Monitoring
246
+
247
+ ShellX automatically monitors shell command output in real-time:
248
+
249
+ ```typescript
250
+ const result = await shellx.executeShellCommand('top -n 1', {
251
+ onOutput: (output) => {
252
+ console.log('Live output:', output);
253
+ },
254
+ successPattern: /Tasks:/,
255
+ errorPattern: /error|failed/i,
256
+ timeout: 10000
257
+ });
258
+ ```
259
+
260
+ ### Element Diagnostics
261
+
262
+ Built-in diagnostic tools for troubleshooting:
263
+
264
+ ```typescript
265
+ // Automatic element diagnosis when finding fails
266
+ const elements = await shellx.findElementsWithRetry({
267
+ textContains: 'NotFound'
268
+ });
269
+
270
+ if (elements.length === 0) {
271
+ // ShellX automatically runs diagnostics
272
+ // Shows available elements, device info, etc.
273
+ }
274
+ ```
275
+
276
+ ### Intelligent Fallback
277
+
278
+ ShellX automatically handles service availability:
279
+
280
+ ```typescript
281
+ // 1. Tries online authentication: https://shellx.ai/api/device/{authKey}
282
+ // 2. On success: Uses returned machine URL
283
+ // 3. On failure: Falls back to local: ws://127.0.0.1:9091/api/s/{authKey}
284
+ // 4. Seamless operation regardless of network conditions
285
+ ```
286
+
287
+ ## 📚 Complete Examples
288
+
289
+ Check out the [examples directory](./examples/) for comprehensive use cases:
290
+
291
+ - **[paytm.ts](./examples/paytm.ts)** - PayTM app automation with bill analysis
292
+ - **[basic.ts](./examples/basic.ts)** - Basic automation operations
293
+ - **[shell-commands-demo.ts](./examples/shell-commands-demo.ts)** - Shell command execution
294
+ - **[find-elements-demo.ts](./examples/find-elements-demo.ts)** - Element finding strategies
295
+
296
+ ## 🚨 Error Handling
297
+
298
+ ShellX provides comprehensive error handling with automatic recovery:
299
+
300
+ ```typescript
301
+ try {
302
+ await shellx.executeShellCommand('invalid-command');
303
+ } catch (error) {
304
+ // ShellX handles timeouts, connection issues, and command failures
305
+ console.error('Command failed:', error.message);
306
+
307
+ // Automatic diagnostics on failure
308
+ // Device info, available elements, etc.
309
+ }
310
+ ```
311
+
312
+ ## 🤝 Contributing
313
+
314
+ Contributions are welcome! Please feel free to submit a Pull Request.
315
+
316
+ ## 📄 License
317
+
318
+ MIT License - see the [LICENSE](LICENSE) file for details.
319
+
320
+ ## 🆘 Support
321
+
322
+ - 📚 [Documentation](https://shellx.ai/docs)
323
+ - 🐛 [GitHub Issues](https://github.com/your-org/shellx/issues)
324
+ - 💬 [Discord Community](https://discord.gg/shellx)
325
+
326
+ ## 🎉 Getting Started
327
+
328
+ 1. **Install ShellX**: `npm install shellx-ai`
329
+ 2. **Set your auth key**: `export SHELLX_AUTH_KEY="your-key"`
330
+ 3. **Run the example**: `ts-node examples/basic.ts`
331
+ 4. **Start automating!** 🚀
332
+
333
+ ---
334
+
335
+ *Made with ❤️ for the automation community*
@@ -0,0 +1,13 @@
1
+ export interface TaskClientConfig {
2
+ timeout: number;
3
+ reconnect: boolean;
4
+ reconnectMaxAttempts: number;
5
+ reconnectInterval: number;
6
+ pingInterval: number;
7
+ onOpen: () => void;
8
+ onClose: (event?: CloseEvent) => void;
9
+ onError: (error?: Event) => void;
10
+ onReconnectFailed: () => void;
11
+ onMessage?: (data: any) => void;
12
+ }
13
+ export declare const DEFAULT_CONFIG: TaskClientConfig;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_CONFIG = void 0;
4
+ exports.DEFAULT_CONFIG = {
5
+ timeout: 5000,
6
+ reconnect: true,
7
+ reconnectMaxAttempts: 5,
8
+ reconnectInterval: 1000,
9
+ pingInterval: 2000,
10
+ onOpen: () => { },
11
+ onClose: () => { },
12
+ onError: () => { },
13
+ onReconnectFailed: () => { },
14
+ };
@@ -0,0 +1,110 @@
1
+ import { TaskClientConfig } from './constants';
2
+ import type { WsClient, ActionSequence, ScreenShotOptions, ElementSelector, FindOptions, WaitOptions, UIHierarchy, WAITElement, ScreenShotResponse, ScreenInfoResponse, AppListResponse } from './protocol';
3
+ export interface TaskResponse<T = any> {
4
+ taskId?: string;
5
+ success?: boolean;
6
+ data?: T;
7
+ error?: {
8
+ message: string;
9
+ };
10
+ }
11
+ export interface PendingTask {
12
+ resolve: (data: any) => void;
13
+ reject: (err: Error) => void;
14
+ timer: NodeJS.Timeout;
15
+ type: string;
16
+ }
17
+ /**
18
+ * Enhanced WebSocket Task Client with protocol-aware task methods
19
+ */
20
+ export declare class WebSocketTaskClient {
21
+ private wsUrl;
22
+ private config;
23
+ ws: WebSocket | null;
24
+ private connected;
25
+ private pendingTasks;
26
+ private messageQueue;
27
+ private reconnectAttempts;
28
+ private pingIntervalId;
29
+ constructor(wsUrl: string, config?: Partial<TaskClientConfig>);
30
+ private init;
31
+ private handleMessage;
32
+ private processServerMessage;
33
+ private handleJsonDataResponse;
34
+ private sendMessage;
35
+ /**
36
+ * Find UI elements on the screen
37
+ */
38
+ findElement(selector: ElementSelector, options?: FindOptions): Promise<UIHierarchy>;
39
+ /**
40
+ * Wait for UI element to appear
41
+ */
42
+ waitElement(selector: ElementSelector, options?: WaitOptions): Promise<WAITElement>;
43
+ /**
44
+ * Take a screenshot
45
+ */
46
+ screenShot(options?: ScreenShotOptions): Promise<ScreenShotResponse>;
47
+ /**
48
+ * Get screen information
49
+ */
50
+ getScreenInfo(): Promise<ScreenInfoResponse>;
51
+ /**
52
+ * Get list of installed apps
53
+ */
54
+ getAppList(options?: {
55
+ includeSystemApps?: boolean;
56
+ includeDisabledApps?: boolean;
57
+ }): Promise<AppListResponse>;
58
+ /**
59
+ * Get specific app information
60
+ */
61
+ getAppInfo(packageName: string): Promise<any>;
62
+ /**
63
+ * Execute an action sequence
64
+ */
65
+ executeAction(actionSequence: ActionSequence): Promise<any>;
66
+ /**
67
+ * Execute promptflow actions
68
+ */
69
+ executePromptFlow(actionSequence: ActionSequence): Promise<any>;
70
+ /**
71
+ * Listen for display changes
72
+ */
73
+ screenChange(options?: {
74
+ interval?: number;
75
+ compareThreshold?: number;
76
+ includeScreenshot?: boolean;
77
+ enable?: boolean;
78
+ }): Promise<void>;
79
+ /**
80
+ * Switch to a different node
81
+ */
82
+ switchNode(nodeId: string): Promise<void>;
83
+ /**
84
+ * Send authentication data
85
+ */
86
+ authenticate(authData: Uint8Array): Promise<void>;
87
+ /**
88
+ * Set user name
89
+ */
90
+ setName(name: string): Promise<void>;
91
+ /**
92
+ * Send chat message
93
+ */
94
+ sendChat(message: string): Promise<void>;
95
+ /**
96
+ * Send raw message (for custom integrations)
97
+ */
98
+ sendRawMessage(message: WsClient): Promise<void>;
99
+ private startPing;
100
+ private stopPing;
101
+ private reconnect;
102
+ private flushQueue;
103
+ close(): void;
104
+ get isConnected(): boolean;
105
+ get pendingTaskCount(): number;
106
+ get queuedMessageCount(): number;
107
+ }
108
+ export default WebSocketTaskClient;
109
+ export { ShellX, createShellX, createShellXWithShellMonitoring } from './shellx';
110
+ export type { UIElement, ElementSelector, ActionSequence } from './shellx';