## 机制 - nodeagentd 常驻进程监听 UDS($TMPDIR/nodeagentd.sock),持有与被控端的 长连接池(autoReconnect,每设备一条,LRU 上限 8,空闲 5min 释放) - CLI withClient 检测 socket 存活 → 经 daemon JSON-lines 转发 invoke(虚拟 client); daemon 自身错误静默回退直连,业务错误原样透传 —— 零行为变化,纯加速层 - CLI 新命令: nodeagent daemon start|stop|status;打包新增 nodeagentd 产物 ## 实测(真机) - 轻调用(info): 752ms -> 527ms(省 TLS+hello/challenge/auth,约 30%) - 轻调用 5 连发(clip.get): 2.58s -> 2.41s - 重调用(ps 远端 CPU 采样 1.4s)提速不显著 —— 瓶颈在远端执行而非握手,符合预期 - unit 26/26、e2e 21/21 全绿
66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 构建 macOS 控制端(免 node_modules 的单文件)。
|
||
*
|
||
* 用法: node scripts/pack-macos.mjs [--out release]
|
||
*
|
||
* 产物: <out>/nodeagent-macos/
|
||
* ├── nodeagent 控制端 CLI(可执行,含 shebang)
|
||
* └── nodeagent-mcp MCP server(可执行,含 shebang)
|
||
*
|
||
* 说明: Mac 作为控制端,通常本机已装 Node.js,故不打运行时(体积从 ~50MB 降到 ~1MB)。
|
||
*/
|
||
import { execFileSync, execSync } from 'node:child_process';
|
||
import { chmodSync, mkdirSync, rmSync, statSync } from 'node:fs';
|
||
import { dirname, join } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
const root = join(__dirname, '..');
|
||
|
||
const args = process.argv.slice(2);
|
||
const getArg = (k, d) => {
|
||
const i = args.indexOf(k);
|
||
return i >= 0 && args[i + 1] ? args[i + 1] : d;
|
||
};
|
||
|
||
const OUT_DIR = getArg('--out', join(root, 'release'));
|
||
const PKG_DIR = join(OUT_DIR, 'nodeagent-macos');
|
||
const esbuild = join(root, 'node_modules', '.bin', 'esbuild');
|
||
|
||
const SHEBANG = '#!/usr/bin/env node';
|
||
// MCP server 经 stdio 通信,日志必须走 stderr;banner 同时补 createRequire 兼容 CJS 依赖
|
||
const BANNER = 'import { createRequire } from "node:module"; const require = createRequire(import.meta.url);';
|
||
|
||
function bundle(entry, outfile) {
|
||
execFileSync(
|
||
esbuild,
|
||
[
|
||
join(root, entry),
|
||
'--bundle',
|
||
'--platform=node',
|
||
'--target=node22',
|
||
'--format=esm',
|
||
`--banner:js=${SHEBANG}\n${BANNER}`,
|
||
`--outfile=${outfile}`,
|
||
],
|
||
{ stdio: 'inherit' },
|
||
);
|
||
chmodSync(outfile, 0o755);
|
||
const kb = (statSync(outfile).size / 1024).toFixed(0);
|
||
console.log(` ✓ ${outfile.replace(`${root}/`, '')} (${kb}KB)`);
|
||
}
|
||
|
||
console.log('=== ① 构建 workspace ===');
|
||
execSync('pnpm -r build', { cwd: root, stdio: 'inherit' });
|
||
|
||
console.log('\n=== ② 打包控制端 ===');
|
||
rmSync(PKG_DIR, { recursive: true, force: true });
|
||
mkdirSync(PKG_DIR, { recursive: true });
|
||
bundle('apps/cli/dist/index.js', join(PKG_DIR, 'nodeagent'));
|
||
bundle('apps/cli/dist/daemon.js', join(PKG_DIR, 'nodeagentd'));
|
||
bundle('apps/mcp/dist/index.js', join(PKG_DIR, 'nodeagent-mcp'));
|
||
|
||
console.log(`\n✓ 完成: ${PKG_DIR}`);
|
||
console.log(' 下一步: bash scripts/install-macos.sh(安装到 ~/.local/bin)');
|