shellx-ai
Version:
shellx is a powerful WebSocket-based client for controlling shell commands and UI automation on remote devices.
181 lines (180 loc) • 5.38 kB
TypeScript
export interface TaskClientConfig {
timeout: number;
reconnect: boolean;
reconnectMaxAttempts: number;
reconnectInterval: number;
pingInterval: number;
onOpen: () => void;
onClose: (event?: CloseEvent) => void;
onError: (error?: Event) => void;
onReconnectFailed: () => void;
onMessage?: (data: any) => void;
}
export declare const DEFAULT_CONFIG: TaskClientConfig;
import type { WsClient, ActionSequence, ScreenShotOptions, ElementSelector, FindOptions, WaitOptions, UIHierarchy, WAITElement, ScreenShotResponse, ScreenInfoResponse, AppListResponse } from './protocol';
export interface TaskResponse<T = any> {
taskId?: string;
success?: boolean;
data?: T;
error?: {
message: string;
};
}
export interface PendingTask {
resolve: (data: any) => void;
reject: (err: Error) => void;
timer: number;
type: string;
commandType?: string;
}
/**
* Enhanced WebSocket Task Client with protocol-aware task methods
*/
export declare class WebSocketTaskClient {
private wsUrl;
private config;
ws: any;
private shellxConnected;
private wsConnected;
private authenticated;
private pendingTasks;
private messageQueue;
private reconnectAttempts;
private pingIntervalId;
private initializationPromise;
private shellx;
constructor(wsUrl: string, config?: Partial<TaskClientConfig>);
private init;
/**
* 等待WebSocket初始化完成
*/
waitForInitialization(): Promise<void>;
/**
* 发送认证消息
*/
private sendAuthenticationMessage;
private handleMessage;
private processServerMessage;
private handleJsonDataResponse;
private sendMessage;
/**
* Find UI elements on the screen
*/
findElement(selector: ElementSelector, options?: FindOptions): Promise<UIHierarchy>;
/**
* Wait for UI element to appear
*/
waitElement(selector: ElementSelector, options?: WaitOptions): Promise<WAITElement>;
/**
* Take a screenshot
*/
screenShot(options?: ScreenShotOptions): Promise<ScreenShotResponse>;
/**
* Get screen information
*/
getScreenInfo(): Promise<ScreenInfoResponse>;
/**
* Get list of installed apps
*/
getAppList(options?: {
includeSystemApps?: boolean;
includeDisabledApps?: boolean;
}): Promise<AppListResponse>;
/**
* Get specific app information
*/
getAppInfo(packageName: string): Promise<any>;
/**
* Execute an action sequence
*/
executeAction(actionSequence: ActionSequence, taskId?: string): Promise<any>;
/**
* Execute promptflow actions
*/
executePromptFlow(actionSequence: ActionSequence): Promise<any>;
/**
* Listen for display changes
*/
screenChange(options?: {
interval?: number;
compareThreshold?: number;
includeScreenshot?: boolean;
enable?: boolean;
}): Promise<void>;
/**
* Switch to a different node
*/
switchNode(nodeId: string): Promise<void>;
/**
* Send authentication data
*/
authenticate(authData: string): Promise<void>;
/**
* Set user name
*/
setName(name: string): Promise<void>;
/**
* Send chat message
*/
sendChat(message: string): Promise<void>;
/**
* Send raw message (for custom integrations)
*/
sendRawMessage(message: WsClient): Promise<void>;
/**
* 发送消息并返回 taskId,允许手动控制任务完成
* @param message 要发送的消息
* @param taskType 任务类型
* @returns Promise<{taskId: string, promise: Promise<any>}>
*/
sendMessageWithTaskId(message: WsClient, taskType?: string, definedTaskId?: string): Promise<{
taskId: string;
promise: Promise<any>;
}>;
private startPing;
private stopPing;
private reconnect;
private flushQueue;
close(): void;
get isConnected(): boolean;
get isWebSocketConnected(): boolean;
get isAuthenticated(): boolean;
get pendingTaskCount(): number;
get queuedMessageCount(): number;
/**
* 设置关联的 ShellX 实例
*/
setShellX(shellx: any): void;
/**
* 获取关联的 ShellX 实例
*/
getShellX(): any;
/**
* 手动完成指定 taskId 的任务
* @param taskId 任务ID
* @param result 任务结果
* @param success 是否成功
*/
completeTask(taskId: string, result?: any, success?: boolean): boolean;
/**
* 获取所有待处理任务的ID列表
*/
getPendingTaskIds(): string[];
/**
* 获取指定任务的信息
*/
getTaskInfo(taskId: string): {
type: string;
commandType?: string;
} | null;
/**
* 批量完成指定类型的任务
* @param taskType 任务类型
* @param result 任务结果
* @param success 是否成功
*/
completeTasksByType(taskType: string, result?: any, success?: boolean): number;
}
export default WebSocketTaskClient;
export type { UIElement, ElementSelector, ActionSequence, WsClient, WsServer, ScreenShotOptions, FindOptions, WaitOptions, UIHierarchy, WAITElement, ScreenShotResponse, ScreenInfoResponse, AppListResponse } from './protocol';
export { createShellXWithShellMonitoring, ShellX } from './shellx';