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

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +66 -2
  3. package/package.json +28 -4
  4. package/src/index.js +294 -0
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,67 @@
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
+ Drive a second HarmonyOS phone through a local HTTP hdc bridge.
4
+
5
+ The bridge (custom ArkTS/HAP side, e.g. **HdcBridge** or DSH NEXT) owns `hdc` and
6
+ listens on loopback; this plugin only validates tool arguments and formats the
7
+ JSON answers.
8
+
9
+ ```
10
+ POST http://<host>:<port>/tool/<observe|tap|swipe|key|input|launch|health>
11
+ ```
12
+
13
+ ## Tools
14
+
15
+ | Tool | Purpose |
16
+ |---|---|
17
+ | `phone_bridge` | Show or change the bridge endpoint (`host`/`port`/`timeoutMs`) |
18
+ | `phone_observe` | Dump visible nodes of the TARGET phone (text + center coords) |
19
+ | `phone_tap` | Tap by fuzzy text or x/y; dangerous actions need `confirm:true` |
20
+ | `phone_swipe` | Swipe up / down / left / right |
21
+ | `phone_key` | Back / Home |
22
+ | `phone_input` | Type into the focused field |
23
+ | `phone_launch` | `aa start <bundle> <ability>` |
24
+ | `phone_health` | Bridge / target connectivity |
25
+
26
+ ## Bridge endpoint (port)
27
+
28
+ Resolution order:
29
+
30
+ 1. Environment: `DSH_HDC_PHONE_HOST`, `DSH_HDC_PHONE_PORT`
31
+ 2. Config file: `<DSH_HOME|HOME>/.dsh-hdc-phone.json`
32
+ ```json
33
+ { "host": "127.0.0.1", "port": 18791, "timeoutMs": 30000 }
34
+ ```
35
+ 3. Default: `127.0.0.1:18790`
36
+
37
+ Change it from the model side:
38
+
39
+ ```json
40
+ { "name": "phone_bridge", "arguments": { "port": 18791 } }
41
+ ```
42
+
43
+ The bridge app must listen on the same port. On the phone both the bridge app
44
+ and DSH NEXT cannot share one port, so give one of them a different one.
45
+
46
+ ## Install
47
+
48
+ ```sh
49
+ # from npm (after it is published)
50
+ dsh plugin --profile <name> add dsh-plugin-hdc-phone
51
+
52
+ # from a local tarball
53
+ dsh plugin --profile <name> add /path/to/dsh-plugin-hdc-phone-0.3.0.tgz
54
+ ```
55
+
56
+ ## Publish
57
+
58
+ ```sh
59
+ npm login
60
+ npm pack
61
+ npm publish --access public
62
+ ```
63
+
64
+ ## Security
65
+
66
+ The bridge binds loopback only and (in the current HdcBridge build) has no
67
+ authentication: any local app can call it. Do not expose the port to a LAN.
package/package.json CHANGED
@@ -1,6 +1,30 @@
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.0",
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
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "keywords": [
17
+ "dsh-plugin",
18
+ "dsh",
19
+ "deepseek-harness",
20
+ "harmonyos",
21
+ "openharmony",
22
+ "hdc",
23
+ "phone",
24
+ "remote-control"
25
+ ],
26
+ "license": "MIT",
27
+ "engines": {
28
+ "node": ">=24.9.0"
29
+ }
30
+ }
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
+ }