import type { ConnectivityTestResult, DeviceAction, ExecutionDump, } from '@midscene/core'; import { ReportActionDump, runConnectivityTest } from '@midscene/core'; import { ModelConfigManager, type TModelConfig, overrideAIConfig, } from '@midscene/shared/env'; import { uuid } from '@midscene/shared/utils'; import { executeAction, parseStructuredParams } from '../common'; import { type PlaygroundRuntimeInfo, buildRuntimeInfo, } from '../runtime-metadata'; import type { AgentFactory, ExecutionOptions, FormValue, PlaygroundAgent, } from '../types'; import { BasePlaygroundAdapter } from './base'; export class LocalExecutionAdapter extends BasePlaygroundAdapter { private agent: PlaygroundAgent | null; private agentFactory?: AgentFactory; // Factory function for recreating agent private dumpUpdateCallback?: ( dump: string, executionDump?: ExecutionDump, ) => void; private progressCallback?: (tip: string) => void; private readonly _id: string; // Unique identifier for this local adapter instance private currentRequestId?: string; // Track current request to prevent stale callbacks private executionController?: AbortController; private configDirty = false; constructor(agent?: PlaygroundAgent, agentFactory?: AgentFactory) { super(); this.agent = agent ?? null; this.agentFactory = agentFactory; this._id = uuid(); // Generate unique ID for local adapter } // Get adapter ID get id(): string { return this._id; } onDumpUpdate( callback: (dump: string, executionDump?: ExecutionDump) => void, ): void { // Clear any existing callback before setting new one this.dumpUpdateCallback = undefined; // Set the new callback this.dumpUpdateCallback = callback; } // Set progress callback for monitoring operation status setProgressCallback(callback: (tip: string) => void): void { this.progressCallback = undefined; this.progressCallback = callback; } async parseStructuredParams( action: DeviceAction, params: Record, options: ExecutionOptions, ): Promise { // Use shared implementation from common.ts return await parseStructuredParams(action, params, options); } formatErrorMessage(error: any): string { const errorMessage = error?.message || ''; if (errorMessage.includes('of different extension')) { return 'Conflicting extension detected. Please disable the suspicious plugins and refresh the page. Guide: https://www.midscenejs.com/platforms/web#chrome-extension-faq'; } return this.formatBasicErrorMessage(error); } // Local execution - use base implementation // (inherits default executeAction from BasePlaygroundAdapter) // Local execution gets actionSpace from internal agent (parameter is for backward compatibility) async getActionSpace(context?: unknown): Promise[]> { // If agent doesn't exist but we have a factory, create one temporarily to get actionSpace if (!this.agent && this.agentFactory) { try { this.agent = await this.agentFactory(); this.configDirty = false; } catch (error) { console.warn('Failed to create agent for actionSpace:', error); return []; } } // Priority 1: Use agent's getActionSpace method if (this.agent?.getActionSpace) { return await this.agent.getActionSpace(); } // Priority 2: Use agent's interface.actionSpace method if ( this.agent && 'interface' in this.agent && typeof this.agent.interface === 'object' ) { const page = this.agent.interface as { actionSpace?: () => DeviceAction[]; }; if (page?.actionSpace) { return page.actionSpace(); } } // Priority 3: Fallback to context parameter (for backward compatibility with tests) if (context && typeof context === 'object' && 'actionSpace' in context) { const contextPage = context as { actionSpace: () => DeviceAction[]; }; return contextPage.actionSpace(); } return []; } // Local execution doesn't use a server, so always return true async checkStatus(): Promise { return true; } async overrideConfig(aiConfig: Record): Promise { // For local execution, use the shared env override function overrideAIConfig(aiConfig); this.configDirty = true; console.log('Config updated. Agent will be recreated on next execution.'); } async runConnectivityTest( aiConfig: TModelConfig, ): Promise { const modelConfigManager = new ModelConfigManager(aiConfig); return runConnectivityTest({ defaultModelConfig: modelConfigManager.getModelConfig('default'), locateModelConfig: modelConfigManager.getModelConfig('locate'), planningModelConfig: modelConfigManager.getModelConfig('planning'), insightModelConfig: modelConfigManager.getModelConfig('insight'), }); } /** * Safely detaches the Chrome debugger without destroying the agent. * This removes the "Debugger attached" banner from the browser window * while keeping the agent instance intact for potential reuse. * Called on errors to improve user experience by cleaning up the UI. */ private async detachDebuggerSafely() { try { const page = this.agent?.interface as | { detachDebugger?: () => Promise } | undefined; await page?.detachDebugger?.(); } catch (error) { console.warn('Failed to detach debugger:', error); } } async executeAction( actionType: string, value: FormValue, options: ExecutionOptions, ): Promise { if (this.executionController) { throw new Error('Another task is already running'); } const controller = new AbortController(); this.executionController = controller; const abort = () => controller.abort(options.abortSignal?.reason); if (options.abortSignal?.aborted) abort(); else options.abortSignal?.addEventListener('abort', abort, { once: true }); try { return await this.executeWithReport(actionType, value, { ...options, abortSignal: controller.signal, }); } finally { options.abortSignal?.removeEventListener('abort', abort); this.executionController = undefined; } } private async executeWithReport( actionType: string, value: FormValue, options: ExecutionOptions, ): Promise { // Reuse the Agent between reports; rebuild only for initial setup or config changes. if (this.agentFactory && (!this.agent || this.configDirty)) { if (this.agent) { console.log('Destroying old agent before execution...'); try { await this.agent.destroy?.(); } catch (error) { console.warn('Failed to destroy old agent:', error); } this.agent = null; } // Create new agent with latest config await this.recreateAgent(); } // Agent must exist (either recreated or provided in constructor) if (!this.agent) { throw new Error( 'No agent available. Please provide either an agent instance or agentFactory.', ); } const agent = this.agent; // Get actionSpace using our simplified getActionSpace method const actionSpace = await this.getActionSpace(); let removeListener: (() => void) | undefined; // The adapter owns the execution boundary; the view must not reset again. await agent.resetDump(); // Setup dump update tracking if requestId is provided if (options.requestId) { // Track current request ID to prevent stale callbacks this.currentRequestId = options.requestId; // Add listener and save remove function removeListener = agent.addDumpUpdateListener( (dump: string, executionDump?: ExecutionDump) => { // Only process if this is still the current request if (this.currentRequestId !== options.requestId) { return; } // Forward to external callback if (this.dumpUpdateCallback) { this.dumpUpdateCallback(dump, executionDump); } }, ); } try { let result = null; let executionError = null; try { options.abortSignal?.throwIfAborted(); // Call the base implementation with the original signature result = await executeAction( agent, actionType, actionSpace, value, options, ); } catch (error: unknown) { // Capture error but don't throw yet - we need to get dump/reportHTML first executionError = error; } // Always construct response with dump and reportHTML, regardless of success/failure const response = { result, dump: null as unknown, reportHTML: null as string | null, error: executionError ? executionError instanceof Error ? executionError.message : String(executionError) : null, }; // Get dump data - separate try-catch to ensure dump is retrieved even if reportHTML fails try { if (agent.dumpDataString) { const dumpString = agent.dumpDataString(); if (dumpString) { const groupedDump = ReportActionDump.fromSerializedString(dumpString); response.dump = groupedDump; } } } catch (error: unknown) { console.warn('Failed to get dump from agent:', error); } // Try to get reportHTML - may fail in browser environment (fs not available) try { if (agent.reportHTMLString) { response.reportHTML = agent.reportHTMLString() || null; } } catch (error: unknown) { // reportHTMLString may throw in browser environment // This is expected in chrome-extension, continue without reportHTML } // In-browser exports must be captured before finalization releases memory. // A concurrent cancel may have destroyed this same Agent; finalization is // cached per report, so both paths still publish the same artifact. try { await agent.finalizeReport(); } catch (error: unknown) { response.error ??= this.formatErrorMessage(error); } // Don't throw the error - return it in response so caller can access dump/reportHTML // The caller (usePlaygroundExecution) will check response.error to determine success return response; } finally { // Remove listener to prevent accumulation if (removeListener) { removeListener(); } } } /** * Recreate agent instance using the factory function. * Called automatically when executeAction is called. */ private async recreateAgent(): Promise { if (!this.agentFactory) { throw new Error( 'Cannot recreate agent: factory function not provided. Please provide agentFactory in PlaygroundConfig to enable agent recreation.', ); } console.log('Creating new agent with latest config...'); try { this.agent = await this.agentFactory(); this.configDirty = false; console.log('Agent created successfully'); } catch (error) { console.error('Failed to create agent:', error); throw error; } } // Local execution task cancellation - returns dump and reportHTML before destroying async cancelTask(requestId: string): Promise<{ error?: string; success?: boolean; dump?: ExecutionDump | ReportActionDump | null; reportHTML?: string | null; }> { this.executionController?.abort( new Error(`Task cancelled by user: ${requestId}`), ); const agent = this.agent; if (!agent) { return { error: 'No active agent found for this requestId' }; } // Get execution data BEFORE destroying the agent let dump: ExecutionDump | ReportActionDump | null = null; let reportHTML: string | null = null; // Get dump data separately - don't let reportHTML errors affect dump retrieval // IMPORTANT: Must extract dump BEFORE agent.destroy(), as dump is stored in agent memory try { if (typeof agent.dumpDataString === 'function') { const dumpString = agent.dumpDataString(); if (dumpString) { const groupedDump = ReportActionDump.fromSerializedString(dumpString); dump = groupedDump; } } } catch (error) { console.warn( '[LocalExecutionAdapter] Failed to get dump data before cancel:', error, ); } // Try to get reportHTML separately so failures do not affect dump retrieval. try { if (typeof agent.reportHTMLString === 'function') { const html = agent.reportHTMLString(); if (html && typeof html === 'string') { reportHTML = html; } } } catch (error) { // Report generation is best-effort here; continue with dump data only. console.warn( '[LocalExecutionAdapter] reportHTMLString not available in this environment', ); } try { await agent.destroy?.(); return { success: true, dump, reportHTML }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; console.error( `[LocalExecutionAdapter] Failed to cancel agent: ${errorMessage}`, ); return { error: `Failed to cancel: ${errorMessage}`, dump, reportHTML }; } finally { // destroy() invalidates the Agent even when report or device cleanup fails. if (this.agent === agent) this.agent = null; } } /** * Get current execution data without resetting * This allows retrieving dump and report when execution is stopped */ async getCurrentExecutionData(): Promise<{ dump: ExecutionDump | ReportActionDump | null; reportHTML: string | null; }> { const response = { dump: null as ExecutionDump | ReportActionDump | null, reportHTML: null as string | null, }; try { // Get dump data if (this.agent?.dumpDataString) { const dumpString = this.agent.dumpDataString(); if (dumpString) { const groupedDump = ReportActionDump.fromSerializedString(dumpString); response.dump = groupedDump; } } // Get report HTML if (this.agent?.reportHTMLString) { response.reportHTML = this.agent.reportHTMLString() || null; } } catch (error: unknown) { console.error('Failed to get current execution data:', error); } return response; } // Get interface information from the agent async getInterfaceInfo(): Promise<{ type: string; description?: string; size?: { width: number; height: number }; actionTypes?: string[]; } | null> { if (!this.agent?.interface) { return null; } try { const type = this.agent.interface.interfaceType || 'Unknown'; const description = this.agent.interface.describe?.() || undefined; const size = typeof this.agent.interface.size === 'function' ? await this.agent.interface.size() : undefined; const actionTypes = typeof this.agent.interface.actionSpace === 'function' ? this.agent.interface .actionSpace() .map((action) => action?.name) .filter((name): name is string => typeof name === 'string') : undefined; return { type, description, ...(size ? { size } : {}), ...(actionTypes ? { actionTypes } : {}), }; } catch (error: unknown) { console.error('Failed to get interface info:', error); return null; } } async getRuntimeInfo(): Promise { if (!this.agent?.interface) { return null; } try { return buildRuntimeInfo({ interfaceType: this.agent.interface.interfaceType || 'Unknown', interfaceDescription: this.agent.interface.describe?.() || undefined, supportsScreenshot: typeof this.agent.interface.screenshotBase64 === 'function', mjpegStreamUrl: this.agent.interface.mjpegStreamUrl, }); } catch (error: unknown) { console.error('Failed to get runtime info:', error); return null; } } }