修复officecli预览的问题

This commit is contained in:
2026-10-19 20:04:32 +08:00
parent 491a9ffefe
commit a911ff2fe9
5 changed files with 477 additions and 86 deletions
+9 -10
View File
@@ -1,15 +1,15 @@
---
name: office
description: Office 文档(docx/xlsx/pptx)创建、精确编辑、结构检视与聊天实时预览
description: Office 文档(docx/xlsx/pptx)创建、精确编辑、结构检视
---
## 功能
Word / Excel / PPT(.docx/.xlsx/.pptx)的创建、读取、精确修改与结构检视。文档直接读写当前任务目录的文件,无需任何同步动作。
Word / Excel / PPT(.docx/.xlsx/.pptx)的创建、读取、精确修改与结构检视。
## 调用方式
所有操作通过 terminal 工具执行同一个入口脚本(cwd 已是任务目录,直接用相对文件名):
所有操作通过 terminal 工具执行同一个入口脚本(cwd 已是任务目录,直接用相对文件名;不要自行调用底层文件执行操作):
```
office <command> [args...]
@@ -19,19 +19,18 @@ office <command> [args...]
常用动线:
- `office create <file>` 新建文档(成功后聊天流出现预览卡片,用户点击即可实时预览)
- `office open <file>` 打开已有文档(同样出预览卡片)
- `office create <file>` 新建文档
- `office open <file>` 打开已有文档
- `office set / get / query <file> ...` 按 schema 精确改动与读取;每次改动自动落盘
- `office view <file> outline|stats|issues|text` 结构/语义自检(交付前建议跑一遍 issues)
- `office view <file> outline|stats|issues|text` 结构/语义自检
- `office save / close <file>` 收尾(改动已自动落盘,save 只是显式冲刷)
- `office get-pos <file>` 回读用户在预览里选中的内容(返回 JSON,path 里的 @id 可稳定用于后续 set/get 定位)
- `office get-pos <file>` 回读用户在预览里选中的文件内容(返回 JSON,path 里的 @id 可稳定用于后续 set/get 定位)
## 预览协作
create/open 成功后无需拼链接、无需等待:聊天流自动出可点击的预览卡片,用户点开即是该文档的实时预览(预览页上的选区、标注类交互之后都能被 get-pos 读到)。用户反馈"看第几段/选中了哪里"时,用 get-pos 读回再针对性改。
create/open 成功后,用户可自行查看该文档的实时预览(预览页上的选区、标注类交互之后都能被 get-pos 读到)。用户反馈"看第几段/选中了哪里/修改哪里"时,优先用 get-pos 读回选区再针对性改。
## 注意
- 文档就在任务目录里:文件面板、预览、下载指向同一份文件;不要用 curl 或中转链搬运待编辑的文档。
- 所有操作文档在任务目录里。
- `view` 的渲染族(html/screenshot/pdf 等)本环境不可用;要 PDF 用 x2pdf 技能。
- 命令报"文件不存在"时先确认文件是否已在任务目录(`ls`),不要猜路径。
+20 -6
View File
@@ -34,12 +34,26 @@ fi
mkdir -p "$HERE/bin"
for u in "https://github.com/iOfficeAI/OfficeCLI/releases/download/$VER/officecli-linux-x64" \
"https://gh-proxy.com/https://github.com/iOfficeAI/OfficeCLI/releases/download/$VER/officecli-linux-x64"; do
echo "[office] 拉取 $u"
if curl -fsSL --retry 2 -o "$ASSET.tmp" "$u" \
&& echo "$SHA $ASSET.tmp" | sha256sum -c - >/dev/null 2>&1; then
mv -f "$ASSET.tmp" "$ASSET"
chmod 755 "$ASSET"
break
echo "[office] 拉取 $u (connect<=10s, speed>=100KB/s, max 300s)"
t0=$(date +%s)
if curl -fL \
--connect-timeout 10 \
--max-time 300 \
--speed-limit 102400 \
--speed-time 20 \
--retry 1 \
--progress-bar \
-o "$ASSET.tmp" "$u"; then
got="$(sha256sum "$ASSET.tmp" | cut -d' ' -f1)"
if [ "$got" = "$SHA" ]; then
mv -f "$ASSET.tmp" "$ASSET"
chmod 755 "$ASSET"
echo "[office] officecli $VER installed -> $ASSET ($(( $(date +%s) - t0 ))s)"
break
fi
echo "[office] sha256 mismatch (got $got), trying next source"
else
echo "[office] $(( $(date +%s) - t0 ))s 后失败: $u"
fi
rm -f "$ASSET.tmp"
done
+35 -6
View File
@@ -8,6 +8,7 @@
'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');
@@ -19,7 +20,8 @@ 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 CARD_NOTE = { create: '已创建', open: '已打开' };
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); };
@@ -29,17 +31,25 @@ function scrub(text) {
}
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);每次改动命令返回前落盘。
const env = Object.assign({}, process.env, {
return Object.assign({}, process.env, {
OFFICECLI_NO_AUTO_RESIDENT: '1',
OFFICECLI_RESIDENT_FLUSH: 'each',
// slim 镜像无 ICU;executor 的 env 白名单不透传,必须在这里自己声明 .NET 不变式模式
DOTNET_SYSTEM_GLOBALIZATION_INVARIANT: '1',
});
return spawnSync(BIN, argv, { encoding: 'utf8', env, maxBuffer: 64 * 1024 * 1024 });
}
const argv = process.argv.slice(2);
// 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);
@@ -82,7 +92,7 @@ try {
}
if (r.error) {
if (r.error.code === 'ENOENT') {
err('officecli binary missing: ' + BIN + ' (skill asset not fetched: docker compose up -d agent-executor-init to retry, or place it per skills/office/install.sh)');
err('office.core binary missing: ' + BIN + ' (skill asset not fetched: docker compose up -d agent-executor-init to retry, or place it per skills/office/install.sh)');
}
err(String(r.error.message || r.error));
}
@@ -114,4 +124,23 @@ if (r.status === 0 && /^(create|open)$/.test(first)) {
}
}
process.exit(r.status === null ? 1 : r.status);
// 编辑成功后给预览网关事件事后一脚:编辑命令与 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);
}