HarmonyOS JavaScript SDK
@midscene/harmony controls HarmonyOS devices over HDC. HarmonyAgent inherits all AI methods from the base Agent.
Imports and API overview
In most cases, use the agentFromHdcDevice() factory to create and connect a Device in one step. If you need to configure and manage the HarmonyDevice and HarmonyAgent instances separately, construct them explicitly.
agentFromHdcDevice()
Connects to a HarmonyOS device and returns a HarmonyAgent bound to the connected Device. deviceId is optional; when omitted, Midscene selects the first connected device and throws an error if none is found. options accepts both Agent and Device settings (including HDC connection settings used during device discovery).
HarmonyDevice / HarmonyAgent
When constructing HarmonyDevice explicitly, call await device.connect() before passing the Device to HarmonyAgent. Each Device instance belongs to a single Agent.
Configuration options
Agent options
Accepts all shared Agent options, plus the following platform option:
appNameMapping?: Record<string, string>— Maps friendly app names to HarmonyOS bundle names or explicitbundle/Abilitytargets so you can reference apps by name inlaunch(),terminate(), or natural-language prompts. User-provided mappings take precedence over built-in defaults.
Device options
hdcPath?: string— Custom path to thehdcexecutable. If omitted, Midscene searches theHDC_HOMEenvironment variable and common installation paths.autoDismissKeyboard?: boolean— Whether to automatically dismiss the on-screen keyboard after text input. Default:true.keyboardDismissStrategy?: 'esc-first' | 'back-first'— Key strategy used when dismissing the on-screen keyboard. Default:'esc-first'(on HarmonyOS,'esc-first'sends the Escape key, while'back-first'sends the Back key).keyboardTypeDelay?: number— Delay in milliseconds between keystrokes (must be a finite non-negative number). When set to a positive value,'legacy'input sends characters one Unicode code point at a time viauitest uiInput inputText. Useful when an input field drops characters during fast typing.inputStrategy?: 'legacy' | 'sequential' | 'bulk'— Default text input strategy forInputactions. Default:'legacy':'sequential': Makes one HDC input call per Unicode code point. Use this strategy when setting a positivekeyboardTypeDelay.'bulk': Sends the entire string in a single HDC text input call. RequireskeyboardTypeDelayto be omitted or set to0.
screenshotResizeScale?: number— Deprecated (removed and no longer has any effect). UsescreenshotShrinkFactorinAgentOptinstead to control the screenshot size sent to the AI model.customActions?: DeviceAction[]— Custom actions registered for the Agent planner to invoke. Pass this option tonew HarmonyDevice()oragentFromHdcDevice(); see Integrate with any interface for details.
Platform methods
launch()
Open a web URL or launch a HarmonyOS app.
uri: string— App bundle name (such ascom.huawei.hmos.settings), explicitbundle/Abilitytarget, app name registered inappNameMapping, orhttp:///https://URL (web URLs are opened in the system browser).
runHdcShell()
Execute a raw hdc shell command on the connected device.
command: string— Command string passed directly tohdc shell.
terminate()
Force-stop a running HarmonyOS app.
uri: string— Bundle name, app name registered inappNameMapping, orbundle/Abilitystring (only the bundle name portion is used).
System navigation
agent.back(): Promise<void>— Trigger the system Back action.agent.home(): Promise<void>— Return to the home screen.agent.recentApps(): Promise<void>— Open the recent apps (multitasking) screen.
Device discovery
Returns the list of devices discovered by HDC. Pass hdcPath to specify a custom hdc executable path.
Lifecycle
Calling agent.destroy() waits for pending operations to complete, calls the underlying Device's destroy(), and finalizes the execution report. Do not share a single Device instance across multiple Agents or reuse a Device after cleanup.
On HarmonyOS, device.destroy() clears screen caches and HDC client state. It does not disconnect the physical device from HDC or close apps on the device.
Troubleshooting
Keyboard is not dismissed or the page navigates back after typing
By default, Midscene automatically dismisses the on-screen keyboard after entering text. On HarmonyOS, it sends the ESC key ('esc-first') by default to avoid accidentally navigating back. If ESC does not close the keyboard in your app, switch keyboardDismissStrategy to 'back-first':
If your input field also reacts to the Back key by clearing its text or closing a modal, set autoDismissKeyboard to false to disable automatic dismissal:
With automatic dismissal disabled, the keyboard stays on screen. You can use aiAct to collapse it when needed, e.g., await agent.aiAct('dismiss the keyboard').
How do I specify a custom HDC path?
Point the HDC_HOME environment variable to the directory containing the hdc executable:
Or pass the full executable path via hdcPath in the HarmonyDevice constructor (or agentFromHdcDevice):

