feat(device): 新增 device 工具,远端设备执行 list/health/exec
设备主动出站连 wss://<公网前缀>/dev/ws,core 维护连接表;本地无公网前缀或未配 DEVICE_TOKEN 时端点不挂,行为与原来一致。
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user