上一版把安装说明写成 INSTALL.md 并在 SKILL.md 里加段落,两处都不对:SKILL.md 是用法说明, 不该塞安装内容;启动器为 office 单独注入 PATH 也不成立,再加技能就得逐条改 start.sh。 - 删除 4 个 INSTALL.md,改为各技能目录下的 AGENTS.md(只写命令:下载/安装/验证、 平台资产名、字体路径),SKILL.md 恢复原样 - start.sh 去掉 office 的 PATH 注入;技能要命令入口时由 AGENTS.md 指导按次前缀 (PATH="$SK/scripts:$PATH" office ...)。启动器只保留 AGENT_SKILLS_DEPS 这个路径变量 - 找不到依赖时的指路不放在 core 里做特征匹配,而是各自报错带自己 AGENTS.md 的绝对路径 (office 封装层 ENOENT / x2pdf 缺包或无后端 / image-process 缺字体) - env-local 提示词加一句:技能失败就读其目录下的 AGENTS.md 实测:无 PATH 时 office 报 command not found,按 AGENTS.md 加前缀后 --version 正常; 三条缺依赖报错均给出 AGENTS.md 绝对路径。
148 lines
7.1 KiB
JavaScript
Executable File
148 lines
7.1 KiB
JavaScript
Executable File
#!/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 <command> [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 <file> outline' or 'view <file> issues' for structural/semantic checks.");
|
||
}
|
||
}
|
||
|
||
// get-pos:语义糖,改写为原生选中回读(用户没在预览里选过时 vendor 返回空结果,不是错误)。
|
||
if (first === 'get-pos') {
|
||
const file = argv[1];
|
||
if (!file) err('Usage: get-pos <file> (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);
|
||
}
|