shellx-ai
Version:
shellx is a powerful WebSocket-based client for controlling shell commands and UI automation on remote devices.
187 lines (186 loc) • 5.73 kB
TypeScript
import type { ElementSelector, ActionSequence, UIElement, ScreenShotOptions, WsServer } from './protocol';
import WebSocketTaskClient from './index';
export type { UIElement, ElementSelector, ActionSequence };
export { ShellX as AutomationHelpers };
export { createShellX as createHelpers };
export { createShellXWithShellMonitoring as createHelpersWithShellMonitoring };
interface ShellCommandResult {
success: boolean;
output: string;
error?: string;
exitCode?: number;
duration: number;
}
interface ShellCommandOptions {
title?: string;
timeout?: number;
waitAfterMs?: number;
onOutput?: (output: string) => void;
onError?: (error: string) => void;
expectedOutput?: string | RegExp;
successPattern?: string | RegExp;
allowPartialResult?: boolean;
}
/**
* ShellX automation utilities for common patterns
*/
export declare class ShellX {
private client;
private shellOutputBuffers;
private shellCommandPromises;
constructor(client: WebSocketTaskClient);
/**
* Get the WebSocket client instance
*/
getClient(): WebSocketTaskClient;
/**
* 设置 shell 输出监听器(需要在创建 WebSocketTaskClient 时调用)
*/
static createShellOutputHandler(helpers: ShellX): (message: WsServer) => void;
/**
* 处理 shell 输出数据
*/
handleShellOutput(chunks: [any, number, Uint8Array[]]): void;
/**
* 判断输出是否属于特定命令
*/
private isOutputForCommand;
/**
* 清理命令输出,去除输入的命令内容
*/
private cleanCommandOutput;
/**
* 合并多个 session 的输出
*/
private combineSessionOutputs;
/**
* 转义正则表达式特殊字符
*/
private escapeRegExp;
/**
* 检查命令是否完成
*/
private checkCommandCompletion;
/**
* 检查输出是否包含错误指示器
*/
private hasErrorIndicators;
/**
* 判断命令是否完成(基于时间)
*/
private isCommandComplete;
/**
* 解析命令 Promise
*/
private resolveCommand;
/**
* 生成命令唯一标识
*/
private generateCommandKey;
/**
* Smart element finder with retry logic
*/
findElementWithRetry(selector: ElementSelector, maxRetries?: number, retryDelay?: number): Promise<UIElement | null>;
/**
* Smart multiple elements finder with retry logic
*/
findElementsWithRetry(selector: ElementSelector, maxRetries?: number, retryDelay?: number, options?: {
maxResults?: number;
visibleOnly?: boolean;
clickableOnly?: boolean;
}): Promise<UIElement[]>;
/**
* 打印元素信息的工具方法
*/
printElementInfo(element: UIElement, index?: number): void;
/**
* 打印多个元素信息的工具方法
*/
printElementsInfo(elements: UIElement[], title?: string): void;
/**
* Click element by text content
*/
clickByText(text: string, exact?: boolean): Promise<boolean>;
/**
* Input text into field
*/
inputText(selector: ElementSelector, text: string, options?: {
clear?: boolean;
hideKeyboard?: boolean;
}): Promise<boolean>;
/**
* Swipe in a direction
*/
swipe(direction: 'up' | 'down' | 'left' | 'right', distance?: number, duration?: number): Promise<void>;
/**
* Take screenshot and save info
*/
captureScreen(options?: ScreenShotOptions & {
saveInfo?: boolean;
}): Promise<import("./protocol").ScreenShotResponse>;
/**
* Wait for any of multiple elements to appear
*/
waitForAnyElement(selectors: ElementSelector[], timeout?: number): Promise<{
element: UIElement;
selectorIndex: number;
} | null>;
/**
* Navigate through app using a series of clicks
*/
navigateByPath(textPath: string[]): Promise<boolean>;
/**
* Scroll to find element
*/
scrollToFindElement(selector: ElementSelector, maxScrolls?: number, direction?: 'up' | 'down'): Promise<UIElement | null>;
/**
* Execute shell command action with output monitoring
*/
executeShellCommand(command: string, options?: ShellCommandOptions): Promise<ShellCommandResult>;
/**
* Execute shell command with simple output (for backward compatibility)
*/
executeSimpleShellCommand(command: string, options?: {
title?: string;
timeout?: number;
waitAfterMs?: number;
}): Promise<ShellCommandResult>;
/**
* Execute multiple shell commands in sequence
*/
executeShellCommands(commands: Array<{
command: string;
title?: string;
waitAfterMs?: number;
}>, options?: {
continueOnError?: boolean;
timeout?: number;
}): Promise<ShellCommandResult[]>;
/**
* Common ADB commands helper
*/
adbCommand(command: string, options?: ShellCommandOptions): Promise<ShellCommandResult>;
/**
* Device info commands
*/
getDeviceInfo(): Promise<ShellCommandResult[]>;
/**
* Execute key action (press a key)
*/
executeKeyAction(keyCode: string, options?: {
longPress?: boolean;
waitAfterMs?: number;
}): Promise<boolean>;
}
/**
* Create ShellX instance
*/
export declare function createShellX(client: WebSocketTaskClient): ShellX;
/**
* Create ShellX instance with automatic authentication and shell output monitoring
* 自动处理ShellX.ai认证和连接,无需外部提供连接地址
*/
export declare function createShellXWithShellMonitoring(config?: any): Promise<{
client: WebSocketTaskClient;
shellx: ShellX;
}>;