From 0e2f7c219fc11e660dcc8833deb0fab7c3d4d55f Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 19 Nov 2026 19:50:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(device):=20list=20=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E5=B8=A6=20health=20=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 那份值是设备上线时取的一次性快照,会随连接时长过期,放在 list 里容易被当成实时状态。 health 改为纯按需查询(action=health),list 只留设备身份与在线信息。 --- app/server/devices.js | 12 ++++-------- app/server/tools/definitions.json | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/server/devices.js b/app/server/devices.js index 13bfad9..13416a2 100644 --- a/app/server/devices.js +++ b/app/server/devices.js @@ -86,7 +86,7 @@ function attach(server) { function onConnection(ws) { const dev = { ws, name: null, os: '', arch: '', shell: '', shellPath: '', shellVer: '', root: '', ver: '', - connectedAt: Date.now(), lastSeen: Date.now(), health: null, helloTimer: null, + connectedAt: Date.now(), lastSeen: Date.now(), helloTimer: null, }; dev.helloTimer = setTimeout(() => { if (!dev.name) { try { ws.close(4001, 'hello timeout'); } catch (_) {} } @@ -151,9 +151,6 @@ function onHello(dev, m) { } state.byName.set(dev.name, dev); log.info('devices', `设备上线:${dev.name}(${dev.os}/${dev.arch} shell=${dev.shellPath || dev.shell} ${dev.shellVer} root=${dev.root})`); - - // 上线即取一次健康快照,list 时能顺带看到负载;失败不影响接入 - health(dev.name).catch(() => {}); } function pick(name) { @@ -177,7 +174,6 @@ function list() { ver: d.ver, connected_sec: Math.round((now - d.connectedAt) / 1000), idle_sec: Math.round((now - d.lastSeen) / 1000), - health: d.health, })); } @@ -194,13 +190,13 @@ function health(name) { onHealth(m) { clearTimeout(timer); state.pending.delete(id); - dev.health = { + resolve({ + device: dev.name, os: dev.os, arch: dev.arch, uptime_sec: m.uptime_sec, host_uptime_sec: m.host_uptime_sec, load1: m.load1, load5: m.load5, load15: m.load15, mem_total_mb: m.mem_total_mb, mem_free_mb: m.mem_free_mb, disk_total_gb: m.disk_total_gb, disk_free_gb: m.disk_free_gb, temp_c: m.temp_c, - }; - resolve({ device: dev.name, os: dev.os, arch: dev.arch, ...dev.health }); + }); }, }); try { diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index eca129c..17cd542 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -436,7 +436,7 @@ "type": "function", "function": { "name": "device", - "description": "Operate the remote devices (other machines) connected to this server. Devices dial out to this server, so they need no inbound port; they stay connected until closed.\n\n- action=list: no other params. Returns every online device with os/arch/shell/root/health. Always call this first when you don't know device names.\n- action=health: requires device. Returns host uptime, load, memory, disk and temperature (fields the platform can't provide are -1).\n- action=exec: requires device and command. Runs the command through a shell on that device (sh -c on Unix, cmd /c on Windows) and returns stdout, stderr and the exit code. `cwd` must be inside that device's work root; omit it to use the root.\n\n- action=pull: requires device and remote_path; copies a file from the device into the session directory so read_file / skills can use it.\n- action=push: requires device, local_path and remote_path; sends a session file to the device.\n\nUse it when the work has to happen on the user's own machine (laptop, box, Windows PC). For the machine running this server use `terminal` instead. If a device is offline, say so explicitly — do not silently run the command elsewhere.", + "description": "Operate the remote devices (other machines) connected to this server. Devices dial out to this server, so they need no inbound port; they stay connected until closed.\n\n- action=list: no other params. Returns every online device with os/arch/shell/root. Always call this first when you don't know device names.\n- action=health: requires device. Returns host uptime, load, memory, disk and temperature (fields the platform can't provide are -1).\n- action=exec: requires device and command. Runs the command through a shell on that device (sh -c on Unix, cmd /c on Windows) and returns stdout, stderr and the exit code. `cwd` must be inside that device's work root; omit it to use the root.\n\n- action=pull: requires device and remote_path; copies a file from the device into the session directory so read_file / skills can use it.\n- action=push: requires device, local_path and remote_path; sends a session file to the device.\n\nUse it when the work has to happen on the user's own machine (laptop, box, Windows PC). For the machine running this server use `terminal` instead. If a device is offline, say so explicitly — do not silently run the command elsewhere.", "parameters": { "type": "object", "required": [