diff --git a/app/package-lock.json b/app/package-lock.json index 5a4187f..24d3e1e 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -14,6 +14,7 @@ "multer": "1.4.5-lts.1", "sharp": "0.35.4", "turndown": "7.2.0", + "ws": "^8.23.0", "yaml": "2.6.1" } }, @@ -2002,6 +2003,27 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, + "node_modules/ws": { + "version": "8.23.0", + "resolved": "https://registry.npmmirror.com/ws/-/ws-8.23.0.tgz", + "integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/app/package.json b/app/package.json index b91289f..74449b3 100644 --- a/app/package.json +++ b/app/package.json @@ -13,6 +13,7 @@ "multer": "1.4.5-lts.1", "sharp": "0.35.4", "turndown": "7.2.0", + "ws": "^8.23.0", "yaml": "2.6.1" } } diff --git a/app/server/devices.js b/app/server/devices.js new file mode 100644 index 0000000..b535f5e --- /dev/null +++ b/app/server/devices.js @@ -0,0 +1,272 @@ +// 设备(远端执行节点)接入层。 +// +// 设备主动出站连 wss://<公网前缀>/dev/ws,core 维护连接表;工具调用时把命令推下去、 +// 把 stdout/stderr/退出码收回来。设备侧不需要任何入站端口,所以家里的盒子、公司的 +// Windows 都能直接在 NAT 后面接进来。 +// +// 启用条件:有公网前缀(config.yml public_link.base_url 或 env PUBLIC_BASE_URL) +// 且配了 DEVICE_TOKEN。本地部署两者都没有,设备也连不进来,端点干脆不挂。 +const crypto = require('crypto'); +const WebSocket = require('ws'); +const log = require('./log'); + +const PATH = '/dev/ws'; +const PING_MS = 20000; // nginx 默认 60s 空闲断,20s 足够安全 +const DEAD_SEC = 61; // 超过这么久没任何消息 → 判假死 +const HELLO_MS = 10000; // 连上后多久没 hello 就踢 +const DEFAULT_TIMEOUT_MS = 120000; +const MAX_TIMEOUT_MS = 600000; +const MAX_OUT_CHARS = 256 * 1024; // 单流回传上限,超出截断(免得一次把上下文塞爆) + +const state = { + enabled: false, + token: '', + wss: null, + byName: new Map(), // name -> device + pending: new Map(), // 请求 id -> { onChunk, onExit, onHealth } +}; + +function init(cfg) { + const base = String((cfg.public_link && cfg.public_link.base_url) || '').trim(); + state.token = String(process.env.DEVICE_TOKEN || '').trim(); + state.enabled = !!(base && state.token); + if (state.enabled) { + let host = base; + try { host = new URL(base).host; } catch (_) { /* 保留原文 */ } + log.info('devices', '设备接入已启用:wss://' + host + PATH); + } else { + log.info('devices', '设备接入未启用(' + (base ? '缺 DEVICE_TOKEN' : '无公网前缀') + ')'); + } + return state.enabled; +} + +function enabled() { + return state.enabled; +} + +function tokenOK(given) { + if (!state.token) return false; + const a = Buffer.from(String(given || '')); + const b = Buffer.from(state.token); + return a.length === b.length && crypto.timingSafeEqual(a, b); +} + +function attach(server) { + if (!state.enabled) return; + state.wss = new WebSocket.Server({ noServer: true, maxPayload: 8 * 1024 * 1024 }); + server.on('upgrade', (req, socket, head) => { + let url; + try { url = new URL(req.url, 'http://x'); } catch (_) { return; } + if (url.pathname !== PATH) return; // 不认识的 upgrade 不归这层管 + const hdr = String(req.headers.authorization || ''); + const bearer = hdr.toLowerCase().startsWith('bearer ') ? hdr.slice(7).trim() : ''; + if (!tokenOK(bearer || url.searchParams.get('token'))) { + log.warn('devices', '设备接入被拒:token 不匹配(来自 ' + (req.socket.remoteAddress || '?') + ')'); + socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); + socket.destroy(); + return; + } + state.wss.handleUpgrade(req, socket, head, ws => state.wss.emit('connection', ws, req)); + }); + state.wss.on('connection', onConnection); + setInterval(() => { + const now = Date.now(); + for (const dev of state.byName.values()) { + if (now - dev.lastSeen > DEAD_SEC * 1000) { + log.warn('devices', '设备无消息超时,断开:' + dev.name); + try { dev.ws.terminate(); } catch (_) {} + continue; + } + try { dev.ws.send(JSON.stringify({ t: 'ping' })); } catch (_) {} + } + }, PING_MS).unref(); +} + +function onConnection(ws) { + const dev = { + ws, name: null, os: '', arch: '', shell: '', root: '', caps: [], ver: '', + connectedAt: Date.now(), lastSeen: Date.now(), health: null, helloTimer: null, + }; + dev.helloTimer = setTimeout(() => { + if (!dev.name) { try { ws.close(4001, 'hello timeout'); } catch (_) {} } + }, HELLO_MS); + + ws.on('message', raw => { + let m; + try { m = JSON.parse(String(raw)); } catch (_) { return; } + dev.lastSeen = Date.now(); + if (m.t === 'hello') return onHello(dev, m); + if (m.t === 'chunk') { + const p = state.pending.get(m.id); + if (p && p.onChunk) p.onChunk(m); + return; + } + if (m.t === 'exit') { + const p = state.pending.get(m.id); + if (p && p.onExit) p.onExit(m); + return; + } + if (m.t === 'health') { + const p = state.pending.get(m.id); + if (p && p.onHealth) p.onHealth(m); + } + }); + + ws.on('close', () => { + clearTimeout(dev.helloTimer); + if (dev.name && state.byName.get(dev.name) === dev) { + state.byName.delete(dev.name); + log.info('devices', '设备下线:' + dev.name); + } + }); + ws.on('error', () => {}); +} + +function onHello(dev, m) { + clearTimeout(dev.helloTimer); + dev.name = String(m.name || '').trim() || ('dev-' + crypto.randomBytes(3).toString('hex')); + dev.os = String(m.os || ''); + dev.arch = String(m.arch || ''); + dev.shell = String(m.shell || ''); + dev.root = String(m.root || ''); + dev.caps = Array.isArray(m.caps) ? m.caps : []; + dev.ver = String(m.ver || ''); + + const old = state.byName.get(dev.name); + if (old && old !== dev) { + log.warn('devices', '同名设备重新接入,踢掉旧连接:' + dev.name); + try { old.ws.close(4000, 'replaced'); } catch (_) {} + } + state.byName.set(dev.name, dev); + log.info('devices', `设备上线:${dev.name}(${dev.os}/${dev.arch} shell=${dev.shell} root=${dev.root})`); + + // 上线即取一次健康快照,list 时能顺带看到负载;失败不影响接入 + health(dev.name).catch(() => {}); +} + +function pick(name) { + if (name) return state.byName.get(String(name).trim()) || null; + const first = state.byName.values().next(); + return first.done ? null : first.value; +} + +function list() { + const now = Date.now(); + return [...state.byName.values()].map(d => ({ + device: d.name, + os: d.os, + arch: d.arch, + shell: d.shell, + root: d.root, + caps: d.caps, + ver: d.ver, + connected_sec: Math.round((now - d.connectedAt) / 1000), + idle_sec: Math.round((now - d.lastSeen) / 1000), + health: d.health, + })); +} + +function health(name) { + const dev = pick(name); + if (!dev) return Promise.reject(new Error(name ? `设备 ${name} 不在线` : '当前没有设备在线')); + const id = newJobId(); + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + state.pending.delete(id); + reject(new Error(`设备 ${dev.name} 未在 5s 内回应 health`)); + }, 5000); + state.pending.set(id, { + onHealth(m) { + clearTimeout(timer); + state.pending.delete(id); + dev.health = { + 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 { + dev.ws.send(JSON.stringify({ t: 'health', id })); + } catch (e) { + clearTimeout(timer); + state.pending.delete(id); + reject(new Error('下发 health 失败:' + e.message)); + } + }); +} + +function newJobId() { + return crypto.randomBytes(6).toString('hex'); +} + +function exec({ device, command, cwd, timeout_ms }) { + return new Promise((resolve, reject) => { + const dev = pick(device); + if (!dev) return reject(new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线')); + const cmd = String(command || ''); + if (!cmd.trim()) return reject(new Error('command 不能为空')); + const tmo = Math.min(Math.max(Number(timeout_ms) || DEFAULT_TIMEOUT_MS, 1000), MAX_TIMEOUT_MS); + const id = newJobId(); + const started = Date.now(); + let out = ''; + let err = ''; + let truncated = false; + let settled = false; + + const done = payload => { + if (settled) return; + settled = true; + clearTimeout(watchdog); + state.pending.delete(id); + resolve(payload); + }; + + // 兜底看门狗:设备侧本来就会按 timeout 杀进程,这里再晚 10s 收尸,防止设备掉线后永远挂着 + const watchdog = setTimeout(() => { + done({ + device: dev.name, code: -1, killed_by: 'watchdog', ms: Date.now() - started, + stdout: out, stderr: err, truncated, + note: '设备未在超时后回传结果(可能已掉线)', + }); + }, tmo + 10000); + + state.pending.set(id, { + onChunk(m) { + const text = Buffer.from(String(m.chunk || ''), 'base64').toString('utf8'); + if (m.out === 'err') { + if (err.length + text.length > MAX_OUT_CHARS) { truncated = true; err += text.slice(0, Math.max(0, MAX_OUT_CHARS - err.length)); } + else err += text; + } else { + if (out.length + text.length > MAX_OUT_CHARS) { truncated = true; out += text.slice(0, Math.max(0, MAX_OUT_CHARS - out.length)); } + else out += text; + } + }, + onExit(m) { + done({ + device: dev.name, + code: typeof m.code === 'number' ? m.code : -1, + killed_by: m.killed_by || '', + ms: Number(m.ms) || Date.now() - started, + stdout: out, + stderr: err + (m.err ? String(m.err) : ''), + truncated: truncated || !!m.truncated, + }); + }, + }); + + try { + dev.ws.send(JSON.stringify({ + t: 'exec', id, cmd, cwd: cwd ? String(cwd) : '', timeout_ms: tmo, + })); + } catch (e) { + state.pending.delete(id); + clearTimeout(watchdog); + reject(new Error('下发命令失败:' + e.message)); + } + }); +} + +module.exports = { init, attach, enabled, list, health, exec, PATH }; diff --git a/app/server/server.js b/app/server/server.js index e70eabb..80ff827 100644 --- a/app/server/server.js +++ b/app/server/server.js @@ -13,6 +13,7 @@ const scheduler = require('./scheduler'); const wechat = require('./wechat'); const llm = require('./llm'); const mcp = require('./mcp'); +const devices = require('./devices'); const skills = require('./tools/skills'); const log = require('./log'); @@ -48,6 +49,11 @@ wechat.init(cfg, db, bus); const app = express(); const server = http.createServer(app); +// 设备接入(devices.js):设备主动出站连 wss://<公网前缀>/dev/ws。 +// 只在「有公网前缀 + 配了 DEVICE_TOKEN」时真正挂升级处理器,本地部署静默跳过。 +devices.init(cfg); +devices.attach(server); + // wxbot 内网回调(/wxhook/inbox|hook):凭共享服务 token(Bearer)把门,微信用户永远不该直接碰 // 这两个端点(wxbot 只挂内网)。必须挂在全局 express.json(2mb) 之前:媒体 base64 转发体远超 2mb, // 由 hookRouter 自带的 32mb 解析器先读掉并置 req._body,下游全局 json 解析器自动跳过(同 executor 不冲突)。 diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index b41a151..c992dc4 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -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\":\"\"} return the skill's full details; call it before the first run\n{\"op\":\"run\",\"name\":\"\",\"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": { diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index e94ce96..90b6f18 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -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, };