feat(device): 新增 device 工具,远端设备执行 list/health/exec

设备主动出站连 wss://<公网前缀>/dev/ws,core 维护连接表;本地无公网前缀或未配
DEVICE_TOKEN 时端点不挂,行为与原来一致。
This commit is contained in:
2026-11-19 16:37:49 +08:00
parent 4ff8b93025
commit 54c33dd7f1
6 changed files with 431 additions and 7 deletions
+76 -7
View File
@@ -205,12 +205,35 @@
"description": "Maintain the current user's profile — stable facts that persist across conversations and get injected into the context of NEW sessions (the current session's injected copy is frozen). Ops: list (read the live profile), set (honorific or industry, single short value), add (append preference tags), remove (delete exact preference tags). Rules: only record what the user has clearly stated or asked to be remembered (form of address, domain, stable output/work preferences); never guess personality, never store one-time task parameters; brief tags beat essays. Preference is a 、-separated tag list (each tag ≤15 chars, ≤24 tags, ≤300 chars total; overflow is rejected — consolidate or remove first). When unsure what is already recorded, op=list returns live state — trust it over memory.",
"parameters": {
"type": "object",
"required": ["op"],
"required": [
"op"
],
"properties": {
"op": { "type": "string", "enum": ["list", "set", "add", "remove"], "description": "list = read live profile; set = single-value honorific/industry; add/remove = preference tags." },
"honorific": { "type": "string", "description": "op=set: how to address the user (≤20 chars, e.g. 王工). Pass empty string to clear. Exactly one of honorific/industry per call." },
"industry": { "type": "string", "description": "op=set: the user's industry/domain (≤20 chars, e.g. 金融). Pass empty string to clear." },
"tags": { "type": "array", "items": { "type": "string" }, "description": "op=add/remove: preference tag list, e.g. [\"偏好简洁回复\", \"图表中文标注\"]. Matching is exact text." }
"op": {
"type": "string",
"enum": [
"list",
"set",
"add",
"remove"
],
"description": "list = read live profile; set = single-value honorific/industry; add/remove = preference tags."
},
"honorific": {
"type": "string",
"description": "op=set: how to address the user (≤20 chars, e.g. 王工). Pass empty string to clear. Exactly one of honorific/industry per call."
},
"industry": {
"type": "string",
"description": "op=set: the user's industry/domain (≤20 chars, e.g. 金融). Pass empty string to clear."
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "op=add/remove: preference tag list, e.g. [\"偏好简洁回复\", \"图表中文标注\"]. Matching is exact text."
}
}
}
}
@@ -340,11 +363,17 @@
"description": "Run or inspect the skills installed on this server. Each skill describes its purpose and input_schema.\n\n{\"op\":\"list\"} list every skill with a one-line description\n{\"op\":\"get\",\"name\":\"<skill>\"} return the skill's full details; call it before the first run\n{\"op\":\"run\",\"name\":\"<skill>\",\"args\":{...}} execute it; args follow its input_schema\n\nop is required. op=get and op=run require name. Put skill arguments inside args, never at the top level.",
"parameters": {
"type": "object",
"required": ["op"],
"required": [
"op"
],
"properties": {
"op": {
"type": "string",
"enum": ["list", "get", "run"],
"enum": [
"list",
"get",
"run"
],
"description": "Operation to perform on skills."
},
"name": {
@@ -403,6 +432,46 @@
}
}
},
{
"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/caps/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\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": [
"action"
],
"properties": {
"action": {
"type": "string",
"enum": [
"list",
"health",
"exec"
],
"description": "Operation to perform."
},
"device": {
"type": "string",
"description": "Device name from action=list."
},
"command": {
"type": "string",
"description": "Shell command line (action=exec). Avoid interactive commands."
},
"cwd": {
"type": "string",
"description": "Working directory on the device; must be inside its work root (action=exec)."
},
"timeout_ms": {
"type": "integer",
"description": "Default 120000, max 600000 (action=exec)."
}
}
}
}
},
{
"type": "function",
"function": {
+54
View File
@@ -11,6 +11,7 @@ const skills = require('./skills');
const Database = require('better-sqlite3'); // db 工具:会话目录下的 data.db(同步 API,与单进程模型契合)
const TurndownService = require('turndown'); // fetch 工具:HTML→Markdown 用真解析器(内置 htmlparser2,容错截断/畸形文档)
const rexec = require('../remote-exec');
const devices = require('../devices'); // device 工具:远端执行节点(设备主动出站连 core,见 devices.js)
const publicFiles = require('../public-files');
const scheduler = require('../scheduler'); // 只依赖 log/cron-parser/time.js,不成环(engine→executor→scheduler 单向)
const profile = require('../user-profile'); // profile 工具的数据面(仅 llm/log 依赖,无环)
@@ -784,6 +785,58 @@ function drainUiCards(text, ctx) {
return keep.join('\n');
}
// device 工具:把命令送到远端设备执行(设备主动出站连 core,连接表在 devices.js)。
// 与 terminal 的分工:terminal 只在 core 所在机器/容器里跑,需要落在用户自己的电脑、盒子、
// Windows 上时用 device。设备不在线就明确报错,不自动降级到本机执行。
async function toolDevice(ctx, args) {
if (!devices.enabled()) {
return '设备接入未启用:需要配置公网前缀(PUBLIC_BASE_URL 或设置页「公网地址」)和 DEVICE_TOKEN,重启 core 后生效。';
}
const action = String(args.action || '').trim();
if (action === 'list') {
const list = devices.list();
if (!list.length) return '当前没有设备在线(设备需主动连 wss://<公网前缀>/dev/ws)。';
return JSON.stringify(list, null, 2);
}
if (action === 'health') {
const name = String(args.device || '').trim();
if (!name) return 'Error: action=health 需要 device 参数(先调 action=list 看设备名)';
try {
return JSON.stringify(await devices.health(name), null, 2);
} catch (e) {
return 'Error: ' + e.message;
}
}
if (action === 'exec') {
const name = String(args.device || '').trim();
if (!name) return 'Error: action=exec 需要 device 参数(先调 action=list 看设备名)';
if (!String(args.command || '').trim()) return 'Error: action=exec 需要 command 参数';
try {
const r = await devices.exec({
device: name,
command: args.command,
cwd: args.cwd,
timeout_ms: args.timeout_ms,
});
const head = `device=${r.device} exit=${r.code}`
+ (r.killed_by ? ` killed_by=${r.killed_by}` : '')
+ ` ${r.ms}ms` + (r.truncated ? '(输出已截断)' : '');
const parts = [head];
if (r.note) parts.push('note: ' + r.note);
if (r.stdout && r.stdout.trim()) parts.push('--- stdout ---\n' + r.stdout.replace(/\s+$/, ''));
if (r.stderr && r.stderr.trim()) parts.push('--- stderr ---\n' + r.stderr.replace(/\s+$/, ''));
return parts.join('\n');
} catch (e) {
return 'Error: ' + e.message;
}
}
return 'Error: action 必须是 list / health / exec';
}
async function toolTerminal(ctx, args) {
const cfg = ctx.cfg;
const timeout = args.timeout_ms || cfg.terminal.default_timeout_ms;
@@ -1789,6 +1842,7 @@ const HANDLERS = {
profile: toolProfile,
recall: toolRecall,
db: toolDb,
device: toolDevice,
skill: toolSkill,
lab: toolLab,
};