dsh-plugin-hdc-phone 0.0.0-stage → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DSH NEXT contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,99 @@
1
- # Temporary Holding Version
1
+ # dsh-plugin-hdc-phone
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/dsh-plugin-hdc-phone?color=4c6ef5)](https://www.npmjs.com/package/dsh-plugin-hdc-phone)
4
+ [![license](https://img.shields.io/npm/l/dsh-plugin-hdc-phone?color=2f9e44)](./LICENSE)
5
+
6
+ Drive a target HarmonyOS phone through the local HTTP bridge that a
7
+ companion app (e.g. **HdcBridge** or DSH NEXT) runs on the host. The bridge
8
+ owns `hdc`; this plugin only validates tool arguments and formats the JSON
9
+ answers, so the agent never has to shell out to `hdc` itself.
10
+
11
+ <!-- TODO: drop a screenshot of the target phone + tool call here, then uncomment
12
+ ![screenshot](./assets/screenshot.png)
13
+ -->
14
+
15
+ ## Features
16
+
17
+ - **7 tools**: observe the UI tree, tap by text or coordinates, swipe, back/home,
18
+ type text, launch an ability, health-check the bridge.
19
+ - **Destructive actions are gated**: tapping 删除 / 支付 / 转账 / 卸载 / 清空 /
20
+ 退出登录 requires an explicit `confirm: true`.
21
+ - **Configurable endpoint**: env vars, a JSON config file, or the
22
+ `phone_bridge` tool — nothing is hard-coded to one port.
23
+ - **Zero dependencies** beyond the host's `@deepseek-ai/dsh-tools`.
24
+
25
+ ## Requirements
26
+
27
+ - DeepSeek Harness **>= 0.2.0-rc.1** (installed as a bundle plugin).
28
+ - Node.js **>= 24.9.0** (the DSH runtime satisfies this).
29
+ - The bridge app must be running on the host and listening on loopback.
30
+ - Target phone connected to that bridge via `hdc` (`hdc list targets` works).
31
+
32
+ ## Install
33
+
34
+ ```sh
35
+ # from npm
36
+ dsh plugin --profile <name> add dsh-plugin-hdc-phone
37
+
38
+ # from a local tarball
39
+ dsh plugin --profile <name> add /path/to/dsh-plugin-hdc-phone-0.3.1.tgz
40
+ ```
41
+
42
+ The package ships a `cordis.patch.yml`, so installing it as a bundle
43
+ auto-registers the `hdc-phone` plugin row — no manual `cordis.yml` edit.
44
+
45
+ ## Tools
46
+
47
+ | Tool | Purpose |
48
+ |---|---|
49
+ | `phone_bridge` | Show or change the bridge endpoint (`host`/`port`/`timeoutMs`) |
50
+ | `phone_observe` | Dump visible nodes of the TARGET phone (text + center coords) |
51
+ | `phone_tap` | Tap by fuzzy text or x/y; dangerous actions need `confirm:true` |
52
+ | `phone_swipe` | Swipe up / down / left / right |
53
+ | `phone_key` | Back / Home |
54
+ | `phone_input` | Type into the focused field |
55
+ | `phone_launch` | `aa start <bundle> <ability>` |
56
+ | `phone_health` | Bridge / target connectivity |
57
+
58
+ Wire protocol — the plugin POSTs to the bridge, the bridge answers:
59
+
60
+ ```
61
+ POST http://<host>:<port>/tool/<observe|tap|swipe|key|input|launch|health>
62
+ ```
63
+
64
+ ## Bridge endpoint (port)
65
+
66
+ Resolution order:
67
+
68
+ 1. Environment: `DSH_HDC_PHONE_HOST`, `DSH_HDC_PHONE_PORT`
69
+ 2. Config file: `<DSH_HOME|HOME>/.dsh-hdc-phone.json`
70
+ ```json
71
+ { "host": "127.0.0.1", "port": 18791, "timeoutMs": 30000 }
72
+ ```
73
+ 3. Default: `127.0.0.1:18790`
74
+
75
+ Change it from the model side:
76
+
77
+ ```json
78
+ { "name": "phone_bridge", "arguments": { "port": 18791 } }
79
+ ```
80
+
81
+ The bridge app must listen on the same port. On the phone both the bridge app
82
+ and DSH NEXT cannot share one port, so give one of them a different one.
83
+
84
+ ## Security
85
+
86
+ The bridge binds loopback only and (in the current HdcBridge build) has no
87
+ authentication: any local app can call it. Do not expose the port to a LAN.
88
+
89
+ ## Publish
90
+
91
+ ```sh
92
+ npm login
93
+ npm pack
94
+ npm publish --access public
95
+ ```
96
+
97
+ ## License
98
+
99
+ MIT — see [LICENSE](./LICENSE).
@@ -0,0 +1,8 @@
1
+ # dsh-plugin-hdc-phone —— DSH Bundle patch
2
+ #
3
+ # 作为 bundle 合入 profile 时,自动插入本插件行。
4
+ # 插件本身通过 env(DSH_HDC_PHONE_HOST / DSH_HDC_PHONE_PORT)或
5
+ # <DSH_HOME|HOME>/.dsh-hdc-phone.json 读取桥接地址,因此这里不需要 config。
6
+ - insert:
7
+ - id: hdc-phone
8
+ name: dsh-plugin-hdc-phone
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "dsh-plugin-hdc-phone",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.1",
4
+ "description": "Drive a second phone through a local HTTP hdc bridge (HdcBridge / DSH NEXT): observe/tap/swipe/key/input/launch, with configurable bridge host/port.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "files": [
12
+ "src",
13
+ "cordis.patch.yml",
14
+ "README.md",
15
+ "LICENSE"
16
+ ],
17
+ "keywords": [
18
+ "dsh-plugin",
19
+ "dsh",
20
+ "deepseek-harness",
21
+ "cordis-plugin",
22
+ "harmonyos",
23
+ "openharmony",
24
+ "hdc",
25
+ "phone",
26
+ "remote-control"
27
+ ],
28
+ "license": "MIT",
29
+ "author": "moyuge123",
30
+ "homepage": "https://github.com/Yunfung/dsh-plugin-hdc-phone#readme",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/Yunfung/dsh-plugin-hdc-phone.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/Yunfung/dsh-plugin-hdc-phone/issues"
37
+ },
38
+ "engines": {
39
+ "node": ">=24.9.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@deepseek-ai/dsh-tools": ">=0.1.0-rc.1 <0.2.0-0 || >=0.2.0-rc.1 <0.3.0-0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "@deepseek-ai/dsh-tools": {
46
+ "optional": true
47
+ }
48
+ },
49
+ "dsh": {
50
+ "bundle": {
51
+ "patch": "./cordis.patch.yml"
52
+ },
53
+ "engines": {
54
+ "dsh": ">=0.2.0-rc.1"
55
+ }
56
+ }
57
+ }
package/src/index.js ADDED
@@ -0,0 +1,294 @@
1
+ /**
2
+ * dsh-plugin-hdc-phone (on-device bridge edition)
3
+ *
4
+ * Drives the TARGET phone (another device) through the local HTTP bridge that
5
+ * the HdcBridge / DSH NEXT app runs (default 127.0.0.1:18790). The bridge owns
6
+ * hdc; this plugin only validates arguments and formats results.
7
+ *
8
+ * POST http://<host>:<port>/tool/<name> {"ok":true,"data":{...}} | {"ok":false,"error":"..."}
9
+ *
10
+ * Bridge endpoint is configurable:
11
+ * 1) env DSH_HDC_PHONE_HOST / DSH_HDC_PHONE_PORT
12
+ * 2) file <DSH_HOME|HOME>/.dsh-hdc-phone.json {"host":"127.0.0.1","port":18791,"timeoutMs":30000}
13
+ * 3) tool call phone_bridge {port: 18791}
14
+ */
15
+ import { defineTool } from '@deepseek-ai/dsh-tools'
16
+ import http from 'node:http'
17
+ import fs from 'node:fs'
18
+ import os from 'node:os'
19
+ import path from 'node:path'
20
+
21
+ export const name = 'hdc-phone'
22
+ export const inject = ['tools']
23
+
24
+ const DEFAULT_HOST = '127.0.0.1'
25
+ const DEFAULT_PORT = 18790
26
+ const DEFAULT_TIMEOUT_MS = 30000
27
+ const CONFIG_FILE = '.dsh-hdc-phone.json'
28
+
29
+ const DANGER = /(\u5220\u9664|\u652f\u4ed8|\u8f6c\u8d26|\u5378\u8f7d|\u6e05\u7a7a|\u9000\u51fa\u767b\u5f55)/
30
+
31
+ const output = {
32
+ schema: {
33
+ type: 'object',
34
+ additionalProperties: false,
35
+ properties: {
36
+ ok: { type: 'boolean' },
37
+ text: { type: 'string' },
38
+ data: { type: 'json' },
39
+ error: { type: 'string' },
40
+ needs_confirmation: { type: 'boolean' },
41
+ hit: { type: 'string' },
42
+ },
43
+ },
44
+ render: (_args, value) => [{ type: 'text', text: value.text ?? (value.ok ? 'ok' : (value.error ?? 'error')) }],
45
+ }
46
+
47
+ function configPath() {
48
+ const home = process.env.DSH_HOME || process.env.HOME || os.homedir()
49
+ return path.join(home, CONFIG_FILE)
50
+ }
51
+
52
+ function readConfig() {
53
+ const cfg = { host: DEFAULT_HOST, port: DEFAULT_PORT, timeoutMs: DEFAULT_TIMEOUT_MS }
54
+ try {
55
+ const file = configPath()
56
+ if (fs.existsSync(file)) {
57
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'))
58
+ if (typeof raw.host === 'string' && raw.host.length > 0) cfg.host = raw.host
59
+ if (Number.isInteger(raw.port) && raw.port > 0 && raw.port < 65536) cfg.port = raw.port
60
+ if (Number.isInteger(raw.timeoutMs) && raw.timeoutMs > 0) cfg.timeoutMs = raw.timeoutMs
61
+ }
62
+ } catch {
63
+ }
64
+ const envHost = process.env.DSH_HDC_PHONE_HOST
65
+ const envPort = process.env.DSH_HDC_PHONE_PORT
66
+ if (typeof envHost === 'string' && envHost.length > 0) cfg.host = envHost
67
+ if (typeof envPort === 'string' && envPort.length > 0) {
68
+ const port = Number(envPort)
69
+ if (Number.isInteger(port) && port > 0 && port < 65536) cfg.port = port
70
+ }
71
+ return cfg
72
+ }
73
+
74
+ function writeConfig(patch) {
75
+ const cfg = readConfig()
76
+ if (typeof patch.host === 'string' && patch.host.trim().length > 0) cfg.host = patch.host.trim()
77
+ if (Number.isInteger(patch.port) && patch.port > 0 && patch.port < 65536) cfg.port = patch.port
78
+ if (Number.isInteger(patch.timeoutMs) && patch.timeoutMs > 0) cfg.timeoutMs = patch.timeoutMs
79
+ fs.writeFileSync(configPath(), JSON.stringify(cfg, null, 2), { encoding: 'utf8', mode: 0o600 })
80
+ return cfg
81
+ }
82
+
83
+ function endpointOf(cfg) {
84
+ return 'http://' + cfg.host + ':' + String(cfg.port)
85
+ }
86
+
87
+ function callTool(tool, body) {
88
+ return new Promise((resolve, reject) => {
89
+ const cfg = readConfig()
90
+ const payload = Buffer.from(JSON.stringify(body ?? {}), 'utf8')
91
+ const req = http.request({
92
+ host: cfg.host,
93
+ port: cfg.port,
94
+ method: 'POST',
95
+ path: '/tool/' + tool,
96
+ headers: {
97
+ 'Content-Type': 'application/json',
98
+ 'Content-Length': payload.length,
99
+ },
100
+ timeout: cfg.timeoutMs,
101
+ }, (res) => {
102
+ const chunks = []
103
+ res.on('data', (chunk) => chunks.push(chunk))
104
+ res.on('end', () => {
105
+ const raw = Buffer.concat(chunks).toString('utf8')
106
+ try {
107
+ resolve(JSON.parse(raw))
108
+ } catch (error) {
109
+ reject(new Error('bridge returned non-JSON: ' + raw.slice(0, 200)))
110
+ }
111
+ })
112
+ })
113
+ req.on('timeout', () => {
114
+ req.destroy(new Error('bridge timeout at ' + endpointOf(cfg)))
115
+ })
116
+ req.on('error', (error) => reject(new Error('bridge unreachable at ' + endpointOf(cfg) + ': ' + String(error && error.message || error))))
117
+ req.write(payload)
118
+ req.end()
119
+ })
120
+ }
121
+
122
+ function toText(data) {
123
+ if (data === undefined || data === null) return ''
124
+ if (typeof data === 'string') return data
125
+ return JSON.stringify(data)
126
+ }
127
+
128
+ async function runTool(tool, body) {
129
+ try {
130
+ const json = await callTool(tool, body)
131
+ if (json.ok !== true) {
132
+ return { ok: false, error: String(json.error ?? 'bridge_error'), text: 'ERROR: ' + String(json.error ?? 'bridge_error') }
133
+ }
134
+ const data = json.data ?? {}
135
+ return { ok: true, data, text: toText(data) }
136
+ } catch (error) {
137
+ const message = String(error && error.message || error)
138
+ return { ok: false, error: message, text: 'ERROR: ' + message }
139
+ }
140
+ }
141
+
142
+ export function apply(ctx) {
143
+ ctx.tools.register(defineTool({
144
+ name: 'phone_bridge',
145
+ description:
146
+ 'USED FOR: reading or changing the local hdc bridge endpoint used by the phone_* tools. ' +
147
+ 'Call with no arguments to show the current endpoint; pass port/host/timeoutMs to persist a new endpoint ' +
148
+ '(the HdcBridge app must listen on the same port).',
149
+ parameters: {
150
+ host: { type: 'string', description: 'bridge host (default 127.0.0.1)' },
151
+ port: { type: 'number', description: 'bridge port (default 18790)' },
152
+ timeoutMs: { type: 'number', description: 'request timeout in ms (default 30000)' },
153
+ },
154
+ output,
155
+ async execute(args) {
156
+ const hasPatch = typeof args.host === 'string' || typeof args.port === 'number' || typeof args.timeoutMs === 'number'
157
+ if (!hasPatch) {
158
+ const cfg = readConfig()
159
+ return { ok: true, data: cfg, text: 'endpoint=' + endpointOf(cfg) + ' timeoutMs=' + String(cfg.timeoutMs) }
160
+ }
161
+ try {
162
+ const cfg = writeConfig({
163
+ host: args.host,
164
+ port: args.port,
165
+ timeoutMs: args.timeoutMs,
166
+ })
167
+ return { ok: true, data: cfg, text: 'saved endpoint=' + endpointOf(cfg) + ' timeoutMs=' + String(cfg.timeoutMs) }
168
+ } catch (error) {
169
+ const message = String(error && error.message || error)
170
+ return { ok: false, error: message, text: 'ERROR: ' + message }
171
+ }
172
+ },
173
+ }))
174
+
175
+ ctx.tools.register(defineTool({
176
+ name: 'phone_observe',
177
+ description:
178
+ 'USED FOR: reading the TARGET phone screen (hdc + uitest dumpLayout) - not this phone. ' +
179
+ 'Returns visible nodes with text and center coordinates; call it before any tap/input.',
180
+ parameters: {
181
+ max: { type: 'number', description: 'max nodes to return (default 60, max 300)' },
182
+ },
183
+ output,
184
+ async execute(args) {
185
+ const max = typeof args.max === 'number' ? Math.max(1, Math.min(300, Math.floor(args.max))) : 60
186
+ const res = await runTool('observe', { max })
187
+ if (!res.ok && String(res.error) === 'target_unreachable') {
188
+ res.text = 'ERROR: target_unreachable - the configured target phone is offline or its wireless-debug address changed. ' +
189
+ 'Tell the user to enable wireless debugging on the target phone and update the address (host auto-rediscovers it every 60s when the target is reachable).'
190
+ } else if (!res.ok && String(res.error) === 'dump_empty') {
191
+ res.text = 'ERROR: dump_empty - target screen may be OFF or LOCKED, or the hdc session went stale. ' +
192
+ 'Ask the user to wake and unlock the target phone, then retry (host auto-reconnects hdc within 60s).'
193
+ }
194
+ return res
195
+ },
196
+ }))
197
+
198
+ ctx.tools.register(defineTool({
199
+ name: 'phone_tap',
200
+ description:
201
+ 'USED FOR: tapping the TARGET phone. Give text to fuzzy-match a node and tap its center, ' +
202
+ 'or x/y for absolute coordinates. Dangerous actions (delete/pay/uninstall) return ' +
203
+ 'needs_confirmation first; resend with confirm:true after the user agrees.',
204
+ parameters: {
205
+ text: { type: 'string', description: 'node text (fuzzy match)' },
206
+ x: { type: 'number', description: 'absolute X (with y)' },
207
+ y: { type: 'number', description: 'absolute Y (with x)' },
208
+ confirm: { type: 'boolean', description: 'user confirmed a dangerous action' },
209
+ },
210
+ output,
211
+ async execute(args) {
212
+ const text = typeof args.text === 'string' ? args.text.trim() : ''
213
+ const hasPoint = typeof args.x === 'number' && typeof args.y === 'number'
214
+ if (text === '' && !hasPoint) return { ok: false, error: 'need text or x/y' }
215
+ if (text !== '' && !args.confirm && DANGER.test(text)) {
216
+ return {
217
+ ok: false,
218
+ needs_confirmation: true,
219
+ hit: text,
220
+ error: 'needs_confirmation',
221
+ text: 'dangerous action - resend with confirm:true after the user agrees',
222
+ }
223
+ }
224
+ const body = {}
225
+ if (text !== '') body.text = text
226
+ if (hasPoint) { body.x = args.x; body.y = args.y }
227
+ if (args.confirm === true) body.confirm = true
228
+ return runTool('tap', body)
229
+ },
230
+ }))
231
+
232
+ ctx.tools.register(defineTool({
233
+ name: 'phone_swipe',
234
+ description: 'USED FOR: swiping the TARGET phone screen (up/down/left/right). No auto-retry.',
235
+ parameters: {
236
+ dir: { type: 'string', required: true, enum: ['up', 'down', 'left', 'right'], description: 'direction' },
237
+ },
238
+ output,
239
+ async execute(args) {
240
+ return runTool('swipe', { dir: String(args.dir ?? '') })
241
+ },
242
+ }))
243
+
244
+ ctx.tools.register(defineTool({
245
+ name: 'phone_key',
246
+ description: 'USED FOR: sending a system key (Back / Home) to the TARGET phone.',
247
+ parameters: {
248
+ key: { type: 'string', required: true, enum: ['Back', 'Home'], description: 'key name' },
249
+ },
250
+ output,
251
+ async execute(args) {
252
+ return runTool('key', { key: String(args.key ?? '') })
253
+ },
254
+ }))
255
+
256
+ ctx.tools.register(defineTool({
257
+ name: 'phone_input',
258
+ description:
259
+ 'USED FOR: typing text into the focused field on the TARGET phone. ' +
260
+ 'If nothing is focused, tap the input box first with phone_tap.',
261
+ parameters: {
262
+ text: { type: 'string', required: true, description: 'text to type' },
263
+ },
264
+ output,
265
+ async execute(args) {
266
+ const text = typeof args.text === 'string' ? args.text : ''
267
+ if (text === '') return { ok: false, error: 'text is required' }
268
+ return runTool('input', { text })
269
+ },
270
+ }))
271
+
272
+ ctx.tools.register(defineTool({
273
+ name: 'phone_launch',
274
+ description: 'USED FOR: launching an app on the TARGET phone via aa start (bundle + ability).',
275
+ parameters: {
276
+ bundle: { type: 'string', required: true, description: 'bundle name, e.g. com.example.app' },
277
+ ability: { type: 'string', required: true, description: 'ability name, e.g. EntryAbility' },
278
+ },
279
+ output,
280
+ async execute(args) {
281
+ return runTool('launch', { bundle: String(args.bundle ?? ''), ability: String(args.ability ?? '') })
282
+ },
283
+ }))
284
+
285
+ ctx.tools.register(defineTool({
286
+ name: 'phone_health',
287
+ description: 'Checks whether the target phone is connected through the hdc bridge.',
288
+ parameters: {},
289
+ output,
290
+ async execute() {
291
+ return runTool('health', {})
292
+ },
293
+ }))
294
+ }