#!/usr/bin/env node // office: officecli 的封装层(技能唯一入口)。 // - 二进制不进 PATH、SKILL.md 不提及:可见面 = 可调用面,模型只认识本脚本;路径可用 OFFICECLI_BIN 覆盖。 // - 命令治理移植自原 MCP 桥:隐去命令报原生风格 unrecognized;view 渲染族拦截给替代路径; // watch 族不透传(预览由平台托管)。 // - create/open 成功在 stdout 尾行发 ui_card marker(::ui_card {json}::):core 剥离后转聊天流卡片, // 模型面不感知 marker;arg 为相对 cwd(会话目录)的文件路径。 'use strict'; const { spawnSync } = require('node:child_process'); const path = require('node:path'); const http = require('node:http'); const BIN = process.env.OFFICECLI_BIN || path.resolve(__dirname, '..', 'bin', 'officecli'); // 本部署不合适的命令(自锁死 / 装配环境族):原生风格报错,help 清单同步删行(见 help 段)。 const unrecognized = (v) => "Unrecognized command or argument '" + v + "'."; const HIDDEN_TOP = /^(mcp|skills|install|plugins)(?:\s|$)/; const HIDDEN_LINE = /^ {2}(?:mcp|skills|install|plugins)\b/; // '\b' 绝不能用字符串拼 RegExp(会变退格符) const HELP_STRIP = /^ {2}(?:watch|unwatch)\b/; const HIDDEN_SUBS = new Set(['mark', 'unmark', 'marks', 'goto']); // 标注/滚动族:预览交互由 get-pos 单点覆盖 const VIEW_BLOCKED = new Set(['html', 'screenshot', 'svg', 'pdf', 'forms']); // 需无头浏览器/导出插件,本环境没有 const OFFICE_EXT_RE = /\.(?:docx|xlsx|pptx)$/i; const EDIT_RE = /^(set|add|remove|move|swap|raw-set|add-part|batch|import|merge|refresh)$/; const CARD_NOTE = { create: '新加', open: '打开' }; const err = (msg) => { process.stderr.write(msg + '\n'); process.exit(1); }; // vendor 异常包装拆掉只留内层真错(与原桥 scrubErr 同源) function scrub(text) { return String(text).replace(/Resident process exited\.\s*Unhandled exception:\s*[\w.]*Exception:\s*/g, ''); } function runVendor(argv) { const opts = { encoding: 'utf8', env: vendorEnv(), maxBuffer: 64 * 1024 * 1024 }; // 命令要么整体已在会话 watch 牢笼里(server.js 把沙箱 nest 进牢笼,OFFICE_JAIL=1),要么完全独立; // 两种情形都直接执行——通知链路要么原生生效(牢笼内),要么事后由 touch/switch 降级刷新(牢笼外)。 return spawnSync(BIN, argv, opts); } function vendorEnv() { // 关自动 resident(实测时延零增益,还会堆常驻进程/撞 file_locked);每次改动命令返回前落盘。 return Object.assign({}, process.env, { OFFICECLI_NO_AUTO_RESIDENT: '1', OFFICECLI_RESIDENT_FLUSH: 'each', // slim 镜像无 ICU;executor 的 env 白名单不透传,必须在这里自己声明 .NET 不变式模式 DOTNET_SYSTEM_GLOBALIZATION_INVARIANT: '1', }); } // watch 自报的 pid ns 句柄文件:key = 相对会话目录路径的短 hash(与 preview.js 同一算法)。 let argv = process.argv.slice(2); if (!argv.length) { process.stdout.write('Usage: office [args...]\nRun `office help` for the full command list.\n'); process.exit(0); } const cmd = argv.join(' '); const first = String(argv[0] || ''); // ---------- 命令面闸门 ---------- let m = HIDDEN_TOP.exec(cmd); if (m || (first === 'help' && HIDDEN_TOP.test(argv.slice(1).join(' ')))) err(unrecognized(m ? m[1] : argv[1])); if (HIDDEN_SUBS.has(first)) err(unrecognized(first)); if (first === 'watch' || first === 'unwatch') { err('Preview slots are managed by the platform: create/open a document, then click the preview card in the chat.'); } const helpCmd = first === 'help' || /(?:^|\s)(?:--help|-h)(?:\s|$)/.test(cmd); // view 放开结构/语义模式(outline/stats/issues/text/annotated),只拦渲染族,并给替代路径。 if (!helpCmd && first === 'view') { const vpos = argv.slice(1).filter((t) => !String(t).startsWith('-')); // [file, mode, ...] const vmode = String(vpos[1] || '').toLowerCase(); if (VIEW_BLOCKED.has(vmode)) { err("view " + vmode + " is disabled by environment policy. Use 'view outline' or 'view issues' for structural/semantic checks."); } } // get-pos:语义糖,改写为原生选中回读(用户没在预览里选过时 vendor 返回空结果,不是错误)。 if (first === 'get-pos') { const file = argv[1]; if (!file) err('Usage: get-pos (works once the user opens the preview and selects something)'); argv = ['get', file, 'selected', '--json']; } // ---------- 透传 ---------- let r; try { r = runVendor(argv); } catch (e) { err(String(e && e.message || e)); } if (r.error) { if (r.error.code === 'ENOENT') { err('office.core binary missing: ' + BIN + '\nLocal env: ' + path.resolve(__dirname, '..', 'AGENTS.md') + ' (server: docker compose up -d agent-executor-init)'); } err(String(r.error.message || r.error)); } let outRaw = scrub(r.stdout || ''); // help 文本对齐可调用面:隐去的命令删行(原生 cwd 已是会话目录,无扩展命令需补录) if (helpCmd && outRaw.includes('Commands:')) { outRaw = outRaw.split('\n').filter((ln) => !HIDDEN_LINE.test(ln) && !HELP_STRIP.test(ln)).join('\n'); } if (outRaw) process.stdout.write(outRaw); if (r.stderr) process.stderr.write(scrub(r.stderr || '')); // create/open 成功尾行发预览卡片 marker(core 解析后剥离;模型面无感) if (r.status === 0 && /^(create|open)$/.test(first)) { let file = null; for (const t of argv) { const s = String(t); if (s.startsWith('-') || s.includes('=')) continue; if (OFFICE_EXT_RE.test(s)) { const rel = path.relative(process.cwd(), path.resolve(s)); if (rel && rel !== '.' && !rel.startsWith('..')) file = rel; break; } } if (file) { process.stdout.write('\n::ui_card ' + JSON.stringify({ action: 'open_preview', arg: file, title: file, note: CARD_NOTE[first] || '', }) + '::\n'); } } // 编辑成功后给预览网关事件事后一脚:编辑命令与 watch 的命名管道通知被沙箱的 --pid 隔绝 //(SO_PEERCRED 跨 pid ns 拿不到对端 pid,vendor 静默退化独立模式),TCP touch 链路则跨得过去: // 网关命中活槽位后 POST watch /api/switch 就地重读,页面靠 vendor 自带的 doc-switched 监听自刷。 // fire-and-forget:800ms 封顶,一切失败静默——预览永远不是命令成功的前提。 const CODE = r.status === null ? 1 : r.status; if (CODE === 0 && !process.env.OFFICE_JAIL && EDIT_RE.test(first) && argv[1]) { let done = false; const finish = () => { if (done) return; done = true; process.exit(CODE); }; try { const q = http.request({ host: '127.0.0.1', port: Number(process.env.OFFICE_GATEWAY_PORT || 3100), path: '/__pv-touch', method: 'POST', timeout: 400, headers: { 'content-type': 'application/json' } }, (res) => { res.resume(); res.on('end', finish); }); q.on('error', finish); q.on('timeout', () => { q.destroy(); finish(); }); q.end(JSON.stringify({ file: path.resolve(String(argv[1])) })); setTimeout(finish, 800); } catch (_) { finish(); } } else { process.exit(CODE); }