前端分析
- 作者仓库星标 235
- 作者更新于 实时读取
- 作者仓库 midscene-skills
- 领域
- 通用
- 兼容 Agent
-
- Claude Code
- Cursor
- Cline
- Codex
- Windsurf
- Gemini CLI
- +20
- 信任分
- 88 / 100 · 社区维护
- 作者 / 版本 / 许可
- @web-infra-dev · 未声明 license
- Token 消耗评级
- 中等消耗
- 接入复杂程度
- 需手动接入
- 是否需要外部 API Key
- 需要 · Google
- 兼容的系统
- macOS · Linux · Windows
- 底层运行要求
- 无特殊要求
- 文件与系统权限
-
- 只读
- 允许写入 / 修改
- Shell 执行
- 读取环境变量
- 网络行为
- 允许外网请求
- 安装命令数
- 26 条
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
---
name: harmonyos-device-automation
description: > Automate HarmonyOS NEXT devices using npx -y @midscene/harmony@1. Each CLI command maps direct…
category: 通用
runtime: 无特殊运行时
---
# harmonyos-device-automation 输出预览
## PART A: 任务判断
- 适用问题:通用任务拆解、检查和交付。
- 输入要求:目标材料、限制条件、期望输出和验收方式。
- 证据边界:围绕“What act Can Do / Prerequisites / HDC Setup”读取原文规则,不把推断写成作者承诺。
## PART B: 执行结果
- **01** 任务判断:确认你的需求是否属于通用任务拆解、检查和交付,并标出输入、限制和预期结果。
- **02** 执行计划:优先按“What act Can Do / Prerequisites / HDC Setup”拆成步骤,说明每一步会读取什么、修改什么、产出什么。
- **03** 交付结果:给出可复制的命令、文件改动、检查清单或内容草稿,并说明如何继续迭代。
- **04** 风险边界:结合 读取文件、写入/修改文件、执行终端命令、读取环境变量、会按任务需要访问外部网络、需要准备 Google API Key 给出执行前确认项。
## Running Rules
- 读取文件、写入/修改文件、执行终端命令、读取环境变量;会按任务需要访问外部网络;需要准备 Google API Key。
- 先小样例验证,再放大到真实任务。
- 交付时同时给结果、检查口径和下一步迭代建议。 原文没有稳定的斜杠命令要求。安装验证后通常全局生效,直接在对话里点名这个 Skill 并描述任务即可。
告诉 Agent 目标文件或材料、期望结果、不可改范围、是否允许联网或执行命令。本 Skill 的权限画像是:读取文件、写入/修改文件、执行终端命令、读取环境变量。
先用一个小任务确认它会围绕“What act Can Do / Prerequisites / HDC Setup”工作;涉及文件或命令时,先看 diff、日志、预览或测试结果。
检查最终产物是否包含明确结果、必要证据和下一步动作;如果输出泛泛而谈,就补充输入、边界和验收标准后重跑。
---
name: harmonyos-device-automation
description: > Automate HarmonyOS NEXT devices using npx -y @midscene/harmony@1. Each CLI command maps direct…
category: 通用
source: web-infra-dev/midscene-skills
---
# harmonyos-device-automation
## 什么时候使用
- 把通用方向的常用动作沉淀成 Agent 可调用的技能 适合处理通用任务拆解、检查、交付和复盘,核心价值是把输入、判断、执行、验证和交付边界固定下来,避免 Agent 泛泛回答。 把任务拆成可执行、可检查、可继续迭代的步骤;使用前要准备…
- 面向通用任务拆解、检查和交付,优先处理能明确输入、步骤和验收标准的工作。
## 需要提供什么
- 目标材料、目录范围、期望结果和不可改动内容。
- 是否允许联网、执行命令、读写文件或调用外部服务。
## 执行规则
- 围绕「What act Can Do / Prerequisites / HDC Setup」组织步骤,不把推断写成作者事实。
- 读取文件、写入/修改文件、执行终端命令、读取环境变量;会按任务需要访问外部网络;需要准备 Google API Key。
- 先跑小样例,确认结果可检查后再扩大任务范围。
## 输出要求
- 给出最终产物、关键证据、验证方式和下一步动作。
- 信息不足时标记 unknown,不编造命令、平台或依赖。 作者原文负责流程事实;仓库文件负责来源和命令;流狐只补充适用场景、限制和质量判断。
skill "harmonyos-device-automation" {
输入层 -> 用户目标 + 目标文件 + 禁止范围 + 验收标准
上下文层 -> What act Can Do / Prerequisites / HDC Setup
规则层 -> SKILL.md 触发条件 / 执行顺序 / 输出格式
运行层 -> 无特殊运行时 | 读取文件、写入/修改文件、执行终端命令、读取环境变量 | 会按任务需要访问外部网络
安全层 -> 需要准备 Google API Key + 小任务验证 + diff / 日志复核
输出层 -> 可复制结果 + 检查清单 + 下一步迭代
} HarmonyOS Device Automation
CRITICAL RULES — VIOLATIONS WILL BREAK THE WORKFLOW:
- Never run midscene commands in the background. Each command must run synchronously so you can read its output (especially screenshots) before deciding the next action. Background execution breaks the screenshot-analyze-act loop.
- Run only one midscene command at a time. Wait for the previous command to finish, read the screenshot, then decide the next action. Never chain multiple commands together.
- Allow enough time for each command to complete. Midscene commands involve AI inference and screen interaction, which can take longer than typical shell commands. A typical command needs about 1 minute; complex
actcommands may need even longer.
Automate HarmonyOS NEXT devices using npx -y @midscene/harmony@1. Each CLI command maps directly to an MCP tool — you (the AI agent) act as the brain, deciding which actions to take based on screenshots.
What act Can Do
Inside a single act call on HarmonyOS, Midscene can tap, double-tap, long-press, type, clear text, scroll, drag items, press keys, and use system navigation such as Back, Home, or recent apps while working from the current visible screen. Two-finger zoom is not available because the underlying HarmonyOS automation layer does not expose multi-touch input.
Prerequisites
Midscene requires models with strong visual grounding capabilities. The following environment variables must be configured — either as system environment variables or in a .env file in the current working directory (Midscene loads .env automatically):
MIDSCENE_MODEL_API_KEY="your-api-key"
MIDSCENE_MODEL_NAME="model-name"
MIDSCENE_MODEL_BASE_URL="https://..."
MIDSCENE_MODEL_FAMILY="family-identifier"
Example: Gemini (Gemini-3-Flash)
MIDSCENE_MODEL_API_KEY="your-google-api-key"
MIDSCENE_MODEL_NAME="gemini-3-flash"
MIDSCENE_MODEL_BASE_URL="https://generativelanguage.googleapis.com/v1beta/openai/"
MIDSCENE_MODEL_FAMILY="gemini"
Example: Qwen 3.5
MIDSCENE_MODEL_API_KEY="your-aliyun-api-key"
MIDSCENE_MODEL_NAME="qwen3.5-plus"
MIDSCENE_MODEL_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
MIDSCENE_MODEL_FAMILY="qwen3.5"
MIDSCENE_MODEL_REASONING_ENABLED="false"
# If using OpenRouter, set:
# MIDSCENE_MODEL_API_KEY="your-openrouter-api-key"
# MIDSCENE_MODEL_NAME="qwen/qwen3.5-plus"
# MIDSCENE_MODEL_BASE_URL="https://openrouter.ai/api/v1"
Example: Doubao Seed 2.0 Lite
MIDSCENE_MODEL_API_KEY="your-doubao-api-key"
MIDSCENE_MODEL_NAME="doubao-seed-2-0-lite"
MIDSCENE_MODEL_BASE_URL="https://ark.cn-beijing.volces.com/api/v3"
MIDSCENE_MODEL_FAMILY="doubao-seed"
Commonly used models: Doubao Seed 2.0 Lite, Qwen 3.5, Zhipu GLM-4.6V, Gemini-3-Pro, Gemini-3-Flash.
If the model is not configured, ask the user to set it up. See Model Configuration for supported providers.
HDC Setup
HDC (HarmonyOS Device Connector) must be installed and accessible. Common setup:
- Install via DevEco Studio
- Or set
HDC_HOMEenvironment variable to point to the HDC directory
Verify HDC is working:
hdc version
hdc list targets
Commands
Connect to Device
npx -y @midscene/harmony@1 connect
npx -y @midscene/harmony@1 connect --deviceId 0123456789ABCDEF
Launch an App or URL
Use the dedicated launch step when you want a deterministic starting point before the rest of the task:
npx -y @midscene/harmony@1 launch --uri com.huawei.hmos.settings
npx -y @midscene/harmony@1 launch --uri com.huawei.hmos.camera
npx -y @midscene/harmony@1 launch --uri https://www.example.com
Run a Raw HarmonyOS Shell Command
Use this when the task needs lower-level device control that is not best expressed as a visible UI interaction:
npx -y @midscene/harmony@1 runhdcshell --command "hidumper -s RenderService -a screen"
This is forwarded to hdc shell on the connected device. In practice, the underlying command is hdc -t <deviceId> shell hidumper -s RenderService -a screen.
Take Screenshot
npx -y @midscene/harmony@1 take_screenshot
After taking a screenshot, read the saved image file to understand the current screen state before deciding the next action.
Perform Action
Use act to interact with the device and get the result. It autonomously handles all UI interactions internally — tapping, typing, scrolling, swiping, waiting, and navigating — so you should give it complex, high-level tasks as a whole rather than breaking them into small steps. Describe what you want to do and the desired effect in natural language:
# specific instructions
npx -y @midscene/harmony@1 act --prompt "type hello world in the search field and press Enter"
npx -y @midscene/harmony@1 act --prompt "long press the message bubble and tap Delete in the popup menu"
# or target-driven instructions
npx -y @midscene/harmony@1 act --prompt "open Settings and navigate to Wi-Fi settings, tell me the connected network name"
Assert Current Screen State
Use assert to verify that the current screen satisfies a natural language condition. It does not perform UI actions; it checks the visible screen state and passes only when the assertion is true. Use this for validation, QA checks, and final state verification after act.
npx -y @midscene/harmony@1 assert --prompt "there is a login button visible"
npx -y @midscene/harmony@1 assert --prompt "the settings screen shows Wi-Fi and Bluetooth options"
npx -y @midscene/harmony@1 assert --deviceId 0123456789ABCDEF --prompt "the app shows a successful login message"
When the assertion needs to compare against a reference image (icon, logo, screenshot), pass --image for the URL/path and --image-name for its display name. Each --image may be an http(s) link, a data: URI, or a local file path. Repeat both flags in matching order when you need to attach more than one image. Add --convertHttpImage2Base64 true when the model cannot reach the URL directly. Requires @midscene/harmony@1.9.0+.
npx -y @midscene/harmony@1 assert \
--prompt "the visible app icon matches the supplied reference image" \
--image "https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png" \
--image-name "icon" \
--convertHttpImage2Base64 true
# or with a local file
npx -y @midscene/harmony@1 assert \
--prompt "the header on screen matches the local screenshot" \
--image "./fixtures/header.png" \
--image-name "header"
# multiple reference images — pair --image and --image-name by order
npx -y @midscene/harmony@1 assert \
--prompt "the screen shows both the app icon and the header" \
--image "./fixtures/icon.png" --image-name "icon" \
--image "./fixtures/header.png" --image-name "header"
Use a Reference Image for Precise Targeting
When the user provides a screenshot, icon, logo, or reference image and wants an exact visual match, prefer tap --locate instead of a generic act --prompt. Pass --locate as JSON. The prompt describes the target, images supplies named reference images, and convertHttpImage2Base64: true is useful when the image URL may not be directly accessible to the model.
npx -y @midscene/harmony@1 tap --locate '{
"prompt": "tap the area contains the image",
"images": [
{
"name": "target image",
"url": "https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png"
}
],
"convertHttpImage2Base64": true
}'
The same locate JSON shape also works for other commands that accept a locate parameter.
Disconnect
npx -y @midscene/harmony@1 disconnect
Consume Report Files
The generated HTML report is recommended for human reading first. It includes step-by-step execution details and replay videos for each operation, which makes it much easier to understand what happened and troubleshoot problems.
If another skill or tool needs to consume the report, first convert it with report-tool from the same platform CLI package. Prefer Markdown for LLM-based workflows. Use JSON when the report needs to be processed programmatically.
npx -y @midscene/harmony@1 report-tool --action to-markdown --htmlPath ./midscene_run/report/.../index.html --outputDir ./output-markdown
npx -y @midscene/harmony@1 report-tool --action split --htmlPath ./midscene_run/report/.../index.html --outputDir ./output-data
Workflow Pattern
Since CLI commands are stateless between invocations, follow this pattern:
- Connect to establish a session
- Launch the target app and take screenshot to see the current state, make sure the app is launched and visible on the screen.
- Execute action using
actto perform the desired action or target-driven instructions, and useassertwhen you need to verify the resulting screen state. - Disconnect when done
Best Practices
- Bring the target app to the foreground before using this skill: For best efficiency, launch the app using HDC (e.g.,
hdc shell aa start -a EntryAbility -b <bundleName>) before invoking any midscene commands. Then take a screenshot to confirm the app is actually in the foreground. Only after visual confirmation should you proceed with UI automation using this skill. HDC commands are significantly faster than using midscene to navigate to and open apps. - Be specific about UI elements: Instead of vague descriptions, provide clear, specific details. Say
"the Wi-Fi toggle switch on the right side"instead of"the toggle". - Describe locations when possible: Help target elements by describing their position (e.g.,
"the search icon at the top right","the third item in the list"). - Never run in background: Every midscene command must run synchronously — background execution breaks the screenshot-analyze-act loop.
- Batch related operations into a single
actcommand: When performing consecutive operations within the same app, combine them into oneactprompt instead of splitting them into separate commands. For example, "open Settings, tap Wi-Fi, and toggle it on" should be a singleactcall, not three. This reduces round-trips, avoids unnecessary screenshot-analyze cycles, and is significantly faster. - Use
assertfor verification: When the goal is to confirm that a screen state is true, useassert --prompt "..."instead of anactprompt. Keep assertions observable and specific, such as"the permission dialog is visible"or"the Save button is disabled". - Summarize report files after completion: After finishing the automation task, collect and summarize all report files (screenshots, logs, output files, etc.) for the user. Present a clear summary of what was accomplished, what files were generated, and where they are located, making it easy for the user to review the results.
- Prefer
tap --locatewhen a reference image is provided: If the user shares a screenshot, icon, or logo and wants that exact visual target, usetap --locatewith a multimodallocateJSON object such as{ "prompt": "...", "images": [...] }instead of relying only onact --prompt.
Example — App launch and interaction:
hdc shell aa start -a EntryAbility -b com.huawei.hmos.settings
npx -y @midscene/harmony@1 connect
npx -y @midscene/harmony@1 take_screenshot
npx -y @midscene/harmony@1 act --prompt "scroll down the settings list and tap About device"
npx -y @midscene/harmony@1 take_screenshot
npx -y @midscene/harmony@1 disconnect
Example — Form interaction:
npx -y @midscene/harmony@1 act --prompt "fill in the username field with 'testuser' and the password field with 'pass123', then tap the Login button"
npx -y @midscene/harmony@1 take_screenshot
Common HarmonyOS Bundle Names
| App | Bundle Name |
|---|---|
| Settings | com.huawei.hmos.settings |
| Camera | com.huawei.hmos.camera |
| Gallery | com.huawei.hmos.photos |
| Calendar | com.huawei.hmos.calendar |
| Clock | com.huawei.hmos.clock |
| Calculator | com.huawei.hmos.calculator |
| Browser | com.huawei.hmos.browser |
| Weather | com.huawei.hmos.weather |
Troubleshooting
| Problem | Solution |
|---|---|
| HDC not found | Install via DevEco Studio or set HDC_HOME environment variable. |
| Device not listed | Check USB connection, ensure USB debugging is enabled in Developer Options, and run hdc list targets. |
| Command timeout | The device screen may be off or locked. Wake the device and unlock it. |
| API key error | Check .env file contains MIDSCENE_MODEL_API_KEY=<your-key>. See Model Configuration. |
@midscene/* dependency version is outdated |
Check local versions with npm ls @midscene/harmony @midscene/core @midscene/shared (or pnpm why @midscene/harmony). Compare with latest via npm view @midscene/harmony version, npm view @midscene/core version, and npm view @midscene/shared version. Upgrade if needed: npm i @midscene/harmony@latest @midscene/core@latest @midscene/shared@latest. |
| Wrong device targeted | If multiple devices are connected, use --deviceId <id> flag with the connect command. |
先判断是否适合
作者设计意图
作者的方法与取舍
边界和复核