From 37d8c356a064ced150f70889316b9647239187af Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 28 Sep 2026 11:24:42 +0800 Subject: [PATCH] =?UTF-8?q?sheet-edit=20=E6=9B=B4=E5=90=8D=20sheet=20?= =?UTF-8?q?=E5=B9=B6=E6=89=A9=E8=AF=AD=E4=B9=89=E4=B8=BA=E3=80=8C=E8=AF=BB?= =?UTF-8?q?+=E5=AE=89=E5=85=A8=E4=BF=AE=E6=94=B9=E3=80=8D=EF=BC=9A?= =?UTF-8?q?=E2=91=A0=20=E7=9B=AE=E5=BD=95/frontmatter/=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E6=94=B9=E5=90=8D=EF=BC=88=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E9=9D=A2=E9=9B=B6=E6=AE=8B=E7=95=99=EF=BC=89=EF=BC=9Bdescripti?= =?UTF-8?q?on=20=E9=87=8D=E5=86=99=E4=B8=BA=E8=B7=AF=E7=94=B1=E4=BC=98?= =?UTF-8?q?=E5=85=88=E2=80=94=E2=80=94=E5=88=97=E5=85=A8=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=20xls/xlsx/csv/ods=E3=80=81inspect=20=E8=AF=BB=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=B8=8E=E5=AE=9A=E4=BD=8D=E3=80=81apply/cells=20?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E6=94=B9=E5=8A=A8=E3=80=81=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E5=9B=9B=E9=87=8D=E4=BF=9D=E9=9A=9C=E3=80=81=E6=98=8E=E7=A1=AE?= =?UTF-8?q?=E3=80=8C=E4=B8=8D=E7=94=A8=E4=BA=8E=E6=96=B0=E5=BB=BA=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E7=B0=BF=E3=80=8D=E3=80=82=E5=91=BD=E5=90=8D=E8=AE=A8?= =?UTF-8?q?=E8=AE=BA=E5=AE=9A=E7=A8=BF=EF=BC=9A=E5=BC=83=20excel/xlsx?= =?UTF-8?q?=EF=BC=88=E5=90=8D=E5=AD=97=E8=B6=8A=E5=BC=BA=EF=BC=8C=E3=80=8C?= =?UTF-8?q?=E5=B8=AE=E6=88=91=E7=94=9F=E6=88=90=E4=B8=AAexcel=E3=80=8D?= =?UTF-8?q?=E7=9A=84=E8=AF=AF=E5=8F=AC=E5=94=A4=E8=B6=8A=E9=9A=BE=E7=94=A8?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=E5=8E=8B=E4=BD=8F=EF=BC=89=EF=BC=8Csheet=20?= =?UTF-8?q?=E4=B8=AD=E6=80=A7=E3=80=81=E5=8F=AC=E5=9B=9E=E9=9D=A0=20descri?= =?UTF-8?q?ption=20=E8=A1=8C=E8=A1=A5=E8=B6=B3=E3=80=82=E2=91=A1=20set/cel?= =?UTF-8?q?ls=20=E7=9A=84=20old=20=E6=94=B9=E4=B8=BA=E5=8F=AF=E9=80=89?= =?UTF-8?q?=EF=BC=9A=E7=9C=81=E7=95=A5=3D=E6=96=AD=E8=A8=80=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E6=A0=BC=E4=B8=BA=E7=A9=BA=E5=86=8D=E5=86=99=E5=85=A5?= =?UTF-8?q?=EF=BC=88=E5=A1=AB=E7=A9=BA/=E8=A1=A5=E5=88=97/=E7=BB=99?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E8=A1=8C=E8=A1=A5=E5=85=AC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E6=AD=A3=E7=BB=8F=E5=87=BA=E5=8F=A3=EF=BC=89=EF=BC=8C=E9=9D=9E?= =?UTF-8?q?=E7=A9=BA=E6=8B=92=E5=86=99=E5=B9=B6=E6=8A=A5=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=80=BC=EF=BC=9B=E6=94=B9=E5=B7=B2=E6=9C=89=E5=80=BC=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E7=BB=99=20old=E2=80=94=E2=80=94=E4=BF=9D=E9=9A=9C?= =?UTF-8?q?=E9=93=BE=E4=B8=8D=E5=8F=98=EF=BC=8C=E5=8F=AA=E6=98=AF=E6=8A=8A?= =?UTF-8?q?=E3=80=8C=E5=86=99=E6=96=B0=E5=86=85=E5=AE=B9=E3=80=8D=E7=BA=B3?= =?UTF-8?q?=E5=85=A5=E6=8A=80=E8=83=BD=EF=BC=8C=E6=9D=9C=E7=BB=9D=20AI=20?= =?UTF-8?q?=E4=B8=BA=E6=AD=A4=E7=BB=95=E9=81=93=E6=89=8B=E6=94=B9=E3=80=82?= =?UTF-8?q?=E2=91=A2=20skills.js=20=E8=90=BD=E5=9C=B0=20required=20?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=EF=BC=9Afrontmatter=20required:true=20?= =?UTF-8?q?=E2=86=92=20prompt=20=E5=88=97=E8=A1=A8=E8=A1=8C=E9=A6=96?= =?UTF-8?q?=E6=A0=87=20**required**=EF=BC=8C=E7=94=A8=E6=88=B7=E5=9C=A8=20?= =?UTF-8?q?system-prompt=20=E6=A8=A1=E6=9D=BF=E5=86=99=E7=9A=84=E3=80=8CSk?= =?UTF-8?q?ills=20marked=20**required**=20must=20be=20used=E3=80=8D?= =?UTF-8?q?=E6=9D=A1=E6=AC=BE=E8=87=AA=E6=AD=A4=E6=9C=89=E4=BA=86=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E5=AF=B9=E8=B1=A1=EF=BC=88sheet=20=E9=A6=96=E4=B8=AA?= =?UTF-8?q?=E6=A0=87=E8=AE=B0=EF=BC=89=EF=BC=9B=E6=AD=A4=E5=89=8D=E8=AF=A5?= =?UTF-8?q?=E6=9D=A1=E6=AC=BE=E7=A9=BA=E8=BD=AC=E3=80=82=E2=91=A3=20?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E9=AA=8C=E8=AF=81=EF=BC=9Askills=3Ddata-page?= =?UTF-8?q?,img-utils,sheet,x2pdf=EF=BC=8Cprompt=20=E5=BF=AB=E7=85=A7?= =?UTF-8?q?=E5=90=AB=20**required**=20sheet=20=E4=B8=94=E9=9B=B6=20sheet-e?= =?UTF-8?q?dit=20=E6=AE=8B=E7=95=99=EF=BC=9B=E5=AE=B9=E5=99=A8=208=20?= =?UTF-8?q?=E9=A1=B9=E5=8A=9F=E8=83=BD=E5=9B=9E=E5=BD=92=E5=85=A8=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/tools/skills.js | 9 ++++--- skills/{sheet-edit => sheet}/SKILL.md | 14 ++++++----- skills/{sheet-edit => sheet}/lib/__init__.py | 2 +- skills/{sheet-edit => sheet}/lib/convert.py | 0 skills/{sheet-edit => sheet}/lib/formulas.py | 0 skills/{sheet-edit => sheet}/lib/locate.py | 0 skills/{sheet-edit => sheet}/lib/ops.py | 26 ++++++++++++++------ skills/{sheet-edit => sheet}/run.py | 2 +- 8 files changed, 35 insertions(+), 18 deletions(-) rename skills/{sheet-edit => sheet}/SKILL.md (92%) rename skills/{sheet-edit => sheet}/lib/__init__.py (78%) rename skills/{sheet-edit => sheet}/lib/convert.py (100%) rename skills/{sheet-edit => sheet}/lib/formulas.py (100%) rename skills/{sheet-edit => sheet}/lib/locate.py (100%) rename skills/{sheet-edit => sheet}/lib/ops.py (96%) rename skills/{sheet-edit => sheet}/run.py (92%) diff --git a/app/server/tools/skills.js b/app/server/tools/skills.js index feaa7ed..a51e4db 100644 --- a/app/server/tools/skills.js +++ b/app/server/tools/skills.js @@ -63,14 +63,17 @@ async function listSkills(ctx) { if (!fs.existsSync(mdPath)) continue; let content; try { content = await fsp.readFile(mdPath, 'utf8'); } catch (_) { continue; } - out.push({ name: e.name, description: extractDescription(content) }); + // frontmatter 可选 required: true —— 进 system prompt 时行首标 **required**(配合模板里的强制条款) + let required = false; + try { required = parseFrontMatter(content).required === true; } catch (_) {} + out.push({ name: e.name, description: extractDescription(content), required: required }); } out.sort((a, b) => a.name.localeCompare(b.name)); return out; } // ---------- system prompt 注入:可用技能列表 ---------- -// 把技能列表格式化成 “## Available Skills” 段落(每行 `- 名字: 描述`; +// 把技能列表格式化成 “## Available Skills” 段落(每行 `- [**required** ]名字: 描述`; // listSkills 已按名字 localeCompare 排序,行序恒定,不破坏前缀缓存; // 参数查 op=get,执行 op=run;描述如需实时复核仍可查 op=list)。 // 由 llm.setSkillListText 拼入 system prompt:启动时快照(server.js)与技能变更后 @@ -81,7 +84,7 @@ function formatSkillListForPrompt(list) { const lines = list.map(s => { // 描述压成单行:YAML 块标量可能带换行,不能让它撞破列表结构 const desc = String(s.description || '').replace(/\s*\n\s*/g, ' ').trim(); - return '- ' + s.name + (desc ? ': ' + desc : ''); + return (s.required ? '- **required** ' : '- ') + s.name + (desc ? ': ' + desc : ''); }); return '## Available Skills\n' + lines.join('\n'); } diff --git a/skills/sheet-edit/SKILL.md b/skills/sheet/SKILL.md similarity index 92% rename from skills/sheet-edit/SKILL.md rename to skills/sheet/SKILL.md index 2909392..d7a8a54 100644 --- a/skills/sheet-edit/SKILL.md +++ b/skills/sheet/SKILL.md @@ -1,16 +1,17 @@ --- -name: sheet-edit -description: 结构化修改表格文件。内容定位(列名 + 值),最小改动。支持改值、改公式、插入/删除行列,公式引用自动调整。 +name: sheet +description: 表格文件(xls/xlsx/csv/ods)的读取与安全修改。inspect 看结构/公式/表头定位;apply 按「列名+值」或 cells 地址最小改动,自带 old 校验、全簿逐格自检、公式引用调整与自动重算。不用于新建工作簿、图表生成。 +required: true --- -# sheet-edit +# sheet ## 调用形式(重要) 本技能通过 `skill` 工具执行,完整调用是: ```json -{"op": "run", "name": "sheet-edit", "input_args": {"action": "inspect", "file": "预算表.xlsx"}} +{"op": "run", "name": "sheet", "input_args": {"action": "inspect", "file": "预算表.xlsx"}} ``` 外层 `op`(固定为 run)属于 skill 工具;**下文所有 JSON 示例都是 `input_args` 的内容**,其动作键是 `action`,不要与外层 `op` 混淆。 @@ -112,13 +113,14 @@ description: 结构化修改表格文件。内容定位(列名 + 值),最 "changes": [ {"sheet": "总表", "cells": { "D4": {"old": 157, "new": 190}, - "F4": {"old": 120, "new": 150} + "H4": {"new": "=D4*0.9"} }} ] } ``` - 地址从 inspect 的 `preview`(含列坐标提示)/`formulas` 里数出来;允许可选 `$`(`$D$4` 同 `D4`) +- 每个地址的 `old` 同样可选(省略 = 空格断言),与 `set` 语义一致 - old 校验、合并区防护、全簿逐格自检、LO 重算与列名模式**完全同源**,不是降级通道 ### 定位 @@ -135,7 +137,7 @@ description: 结构化修改表格文件。内容定位(列名 + 值),最 {"set": {"单价": {"old": 157, "new": 180}}} ``` -`old` 必须与实际值一致,否则整体报错,不做任何改动。 +`old` 可省:**省略 = 断言目标格当前为空再写入**(填空/补列/补公式场景);改已有值必须给 `old`,不一致则整体报错、不做任何改动。 ### 改公式 diff --git a/skills/sheet-edit/lib/__init__.py b/skills/sheet/lib/__init__.py similarity index 78% rename from skills/sheet-edit/lib/__init__.py rename to skills/sheet/lib/__init__.py index ca5a8f5..276757a 100644 --- a/skills/sheet-edit/lib/__init__.py +++ b/skills/sheet/lib/__init__.py @@ -1,4 +1,4 @@ -"""sheet-edit 内部库。""" +"""sheet 内部库。""" class UserError(Exception): diff --git a/skills/sheet-edit/lib/convert.py b/skills/sheet/lib/convert.py similarity index 100% rename from skills/sheet-edit/lib/convert.py rename to skills/sheet/lib/convert.py diff --git a/skills/sheet-edit/lib/formulas.py b/skills/sheet/lib/formulas.py similarity index 100% rename from skills/sheet-edit/lib/formulas.py rename to skills/sheet/lib/formulas.py diff --git a/skills/sheet-edit/lib/locate.py b/skills/sheet/lib/locate.py similarity index 100% rename from skills/sheet-edit/lib/locate.py rename to skills/sheet/lib/locate.py diff --git a/skills/sheet-edit/lib/ops.py b/skills/sheet/lib/ops.py similarity index 96% rename from skills/sheet-edit/lib/ops.py rename to skills/sheet/lib/ops.py index 18b12f1..d68aa4d 100644 --- a/skills/sheet-edit/lib/ops.py +++ b/skills/sheet/lib/ops.py @@ -1,4 +1,4 @@ -"""sheet-edit 的各个 op 实现。""" +"""sheet 的各个 op 实现。""" import datetime import re import shutil @@ -128,11 +128,20 @@ def apply(args): plan.extend(_resolve_change(wb, ch, header_row)) for item in plan: - if not locate.value_eq(item['expected_old'], item['actual_old']): + # csv 往返的空串视同空格;省略 old = 空格断言,给了 old = 精确校验 + act = None if item['actual_old'] == '' else item['actual_old'] + exp = item['expected_old'] + if exp is _MISSING: + if act is not None: + raise UserError( + f"目标格非空,拒绝无 old 写入: {item['sheet']} 第 {item['row']} 行 " + f"'{item['col_name']}' 当前为 {act!r}(填空应保证目标为空;改已有值必须提供 old)" + ) + elif not locate.value_eq(exp, act): raise UserError( f"旧值不匹配: {item['sheet']} 第 {item['row']} 行 " - f"'{item['col_name']}': 期望 {item['expected_old']!r}, " - f"实际 {_json_safe(item['actual_old'])!r}" + f"'{item['col_name']}': 期望 {exp!r}, " + f"实际 {_json_safe(act)!r}" ) before = _snapshot(wb) @@ -209,6 +218,9 @@ def _resolve_change(wb, ch, header_row): _ADDR_RE = re.compile(r'^\$?([A-Za-z]{1,3})\$?(\d{1,7})$') +# set/cells 的 old 缺省哨兵:省略 old = 断言目标格当前为空再写入(填空);改已有值必须显式 old。 +_MISSING = object() + def _resolve_cells(ws, sheet, cells_spec): """地址模式:cells = {"C5": {"old": ..., "new": ...}, ...}。多级表头等列名不可用场景的正经出口, @@ -227,8 +239,8 @@ def _resolve_cells(ws, sheet, cells_spec): def _mk_item(ws, sheet, row, col, label, spec, match_info): - if not isinstance(spec, dict) or 'old' not in spec or 'new' not in spec: - raise UserError(f"'{label}' 的设值必须是 {{\"old\": ..., \"new\": ...}} 格式") + if not isinstance(spec, dict) or 'new' not in spec: + raise UserError(f"'{label}' 的设值必须是 {{\"new\": ..., \"old\": 可选...}} 格式") actual = ws.cell(row, col).value if isinstance(actual, ArrayFormula): raise UserError( @@ -244,7 +256,7 @@ def _mk_item(ws, sheet, row, col, label, spec, match_info): 'col': col, 'col_name': label, 'match': match_info, - 'expected_old': spec['old'], + 'expected_old': spec['old'] if 'old' in spec else _MISSING, 'actual_old': actual, 'new': new, } diff --git a/skills/sheet-edit/run.py b/skills/sheet/run.py similarity index 92% rename from skills/sheet-edit/run.py rename to skills/sheet/run.py index 3c7912b..fb28f9b 100644 --- a/skills/sheet-edit/run.py +++ b/skills/sheet/run.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -"""sheet-edit 技能入口。接收一个 JSON 参数,按其中 action 键分发(旧键名 op 仍兼容)。""" +"""sheet 技能入口。接收一个 JSON 参数,按其中 action 键分发(旧键名 op 仍兼容)。""" import json import sys from pathlib import Path