rea
morluto
Reverse engineer anything with agents, from app behavior down to native binaries.
PROJECT TOPICS
INSTALL REFERENCE
dsh plugin --profile web add github:tututuhehehe/dsh-notion
该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。
PROJECT README
Six consolidated Notion tools for DeepSeek Harness — built on Notion's official ntn CLI instead of the official Notion MCP server.
简体中文 | English
Notion 官方 MCP 暴露数十个工具(数量随版本变动,60+),其中大量能力重叠、命名分散。工具越多,模型每轮吃的 schema 越多、选错工具的概率越高,实际干活反而更慢。
dsh-notion 的做法是收敛工具面:
action 参数:notion_page_create / notion_page_edit / notion_page_trash 各自只声明自己真正接受的参数,建页时不可能误带 old_str,改页时也不可能误带 database:<id> 当父级ntn CLI,直连稳定能力,不额外维护一套 API 客户端doublecheck 复核工具数从 4 变成 6,是因为 notion_page 一个工具塞了四个动作,模型光读它就要吃掉一大段描述,还得先用 action 选分支才能看到自己真正关心的规则。拆开之后每个描述都短,且只讲自己那件事。
| Tool | 作用 | 典型场景 |
|---|---|---|
notion_search |
按标题搜索工作区(page / database) | 用户说"找一下某页",还没有 id |
notion_get |
读页面 Markdown,或读数据库/data source 的 schema | 读正文、看有哪些列可写 |
notion_page_create |
新建页面或数据库行 | 新建页、写列、挂父级 |
notion_page_edit |
查找替换正文、改标题/列/图标、移动父级 | 改一句话、补一段、改状态、搬家 |
notion_page_trash |
删除页面或行(有子页面时先复核) | 删掉废弃页 |
notion_query |
对已知数据库/data source 做 filter / sort / 分页 | "这个库里未完成的任务有哪些" |
search找对象,query查表里的行;两者职责不混。
# macOS (Homebrew)
brew install --cask notion-cli
# 或 npm
npm i -g ntn
ntn --version
登录方式二选一:ntn login,或设置 NOTION_API_TOKEN(见下)。
把仓库放到固定目录后,让 DSH 安装(需要 profile 可写):
plugin_manager install_bundle target=/absolute/path/to/dsh-notion
也可以直接用 git 地址安装。装好后插件行 id 是 notion-tools,六个工具出现在工具列表里。
token 不写进插件代码,按优先级读取:
NOTION_API_TOKENnotion-token 文件(首个非注释行)ntn login 保存的登录态export NOTION_API_TOKEN=secret_xxx
# 或
echo 'secret_xxx' > <插件目录>/notion-token
Integration 需要在 Notion 里 Share 给目标页面/数据库,否则 API 返回 404。
| 字段 | 默认 | 说明 |
|---|---|---|
tokenFile |
notion-token |
token 文件;相对路径按插件目录解析 |
workspaceId |
空 | 多 workspace token 时指定(NOTION_WORKSPACE_ID) |
ntnPath |
自动发现 | ntn 可执行文件路径,自动兜底 /opt/homebrew/bin、/usr/local/bin、PATH |
timeoutMs |
60000 |
单次 ntn 调用超时(毫秒) |
{ "query": "项目计划", "limit": 10, "filter_object": "page" }
{ "id_or_url": "https://www.notion.so/<id>", "format": "md" } // 读页面
{ "id_or_url": "<database-id>" } // 读 schema
// 新建普通子页:body_content 和 icon 都是顶层参数,与 title 平级
{ "parent": "page:<page-id>", "title": "标题",
"body_content": "# 正文标题\n\n正文", "icon": "🎯" }
// 新建数据库行(props 只放真实列)
{ "parent": "data-source:<ds-id>", "title": "任务 A",
"body_content": "说明",
"props": { "Status": "进行中", "标签": ["验收"],
"截止日期": "2026-01-31", "Done": false } }
// 查找替换:改一段话
{ "id": "<page-id>", "edits": [
{ "old_str": "旧的那句话", "new_str": "新的那句话" } ] }
// 一次改多处(顺序执行,同一个请求)
{ "id": "<page-id>", "edits": [
{ "old_str": "状态:待办", "new_str": "状态:完成" },
{ "old_str": "旧标题", "new_str": "新标题" } ] }
// 追加一段:edits 省略或为空 + body_content = 追加到页尾
{ "id": "<page-id>", "body_content": "## 附:补充说明\n\n后补的内容" }
// 删除一段:new_str 传空串
{ "id": "<page-id>", "edits": [ { "old_str": "这段不要了", "new_str": "" } ] }
// 命中多处时全部替换:replace_all_matches 放在那一条 edit 里面
{ "id": "<page-id>", "edits": [
{ "old_str": "TODO", "new_str": "DONE", "replace_all_matches": true } ] }
// 只改列 / 只改图标 / 只改标题
{ "id": "<page-id>", "props": { "Status": "完成" } }
{ "id": "<page-id>", "icon": "https://example.com/logo.png" }
{ "id": "<page-id>", "icon": null } // 清除图标
{ "id": "<page-id>", "title": "新标题" }
// 移动父级(原 attach):与内容修改可以同时给,按 正文 → 属性 → 移动 的顺序执行
{ "id": "<page-id>", "parent": "page:<parent-id>" }
{ "id": "<page-id>", "parent": "page:<parent-id>",
"edits": [ { "old_str": "搬家了", "new_str": "已归档" } ] }
// 行→行移动且目标已有不同的父级:需要 yes 显式同意覆盖
{ "id": "<row-id>", "parent": "<other-row-id>", "yes": true }
// 没有子页面:这一次调用就删完
{ "id": "<page-id>" }
// 有子页面:这一次调用**不删**,返回 pending(列出每个子页面的 id 和标题),
// 在同一轮的**后续模型步骤**里回答:
double_check({ "id": "<pending-id>", "yes": true }) // 连同整棵子树一起删
double_check({ "id": "<pending-id>", "yes": false }) // 不删
{
"database_or_data_source": "<ds-id 或 url>",
"limit": 25,
"filter": { "property": "Status", "status": { "equals": "进行中" } },
"sort": "截止日期 asc",
"under": "<父行 id>" // 只查某个父行下的子行(走 Parent-item 关系列)
}
| 工具 | 参数 | 说明 |
|---|---|---|
notion_page_create |
parent(可省略)、title、props、icon、body_content |
省略 parent = 建在工作区根级;props 只放真实列 |
notion_page_edit |
id(必填)、edits、body_content、props、title、icon、parent、yes |
至少给一项;edits 与 body_content 互斥;yes 只在行→行覆盖父级时与 parent 一起用 |
notion_page_trash |
id(必填) |
只有这一个参数;有子页面的删除要过一次复核,见下 |
三个写工具的 timeoutMs 都是 180 秒,isConcurrencySafe 都是 false(每个动作都可能写入)。
notion_page_edit 一次调用可以同时做多件事,按固定顺序执行:(1) 正文 → (2) 标题/列/图标 → (3) 移动父级。
三步互相独立:一步失败不会取消其它步。 old_str 匹配不上是参数问题,不是页面坏了——没有理由因此拒绝一个合法的标题修改或移动。所以三步都会执行,失败项被收集起来,最后如实报告哪些生效、哪些没有:
部分成功。已经生效的部分:属性(title)、icon(已写入,不会回滚)。
失败的部分:
- 正文(edits):No matches found for ...
这些步骤互不依赖,所以其余步骤照常执行了。重试时请只重发失败的部分;
重发全部会把已生效的部分重复应用一遍。
如果所有步骤都失败,则返回普通错误(不会虚假声称「已写入」)。
props 现在只放真实列:属性名 → 值 的对象 map,按列类型自动转换(title/rich_text、number、select/status、multi_select、date、checkbox、url/email/phone、people/relation)。正文和图标不再是 props 的保留键,而是顶层参数 body_content / icon。
这意味着 props.icon / props.body_content 没有任何特殊含义了:如果某个 data source 里真有一列叫 icon 或 body_content,那 props.icon / props.body_content 写的就是那一列。想改页面图标/正文,用顶层参数。
兼容别名:
notion_page_edit还认一个props.block_update(拆分前查找替换列表的旧拼法),只在没传edits时生效,两者同时传会报错。它不在参数 schema 里,属于运行期兼容,新代码请直接用顶层edits。
失败时分两种:未知列名 / 只读列会返回可写列清单(写进消息文本);值类型不符只指出属性名与期望类型,不列清单。
icon 的取值:单个 emoji("🎯")、http(s) 图片 URL、已构好的 Notion icon 对象({"type":"emoji","emoji":"🎯"})、或 null 清除。只改图标时不会碰正文和其他列。
parent 的写法(create 与 edit 接受的范围不同):
| 写法 | notion_page_create |
notion_page_edit(移动) |
|---|---|---|
省略 parent |
建在工作区根级(Notion 的默认位置;这种页只有 title,props 不能带列) |
—(移动必须给 parent) |
page:<id> 或 page:<URL> |
该页面下的子页面 | 移到该页面下(POST /v1/pages/{id}/move) |
<裸 page id> |
同上(先确认它确实不是数据库行) | 同上 |
<裸 row id> |
该行所在 data source 下的子行,并设好 Parent-item 关系 | 重新指向该行的 Parent-item 关系 |
data-source:<id> |
该 data source 里的同级行 | ❌ 不接受(加行请用 create) |
database:<id> |
通过 ntn datasources resolve 解析到它唯一的 data source(多个则报错列出 id,绝不猜) |
❌ 不接受 |
notion_page_edit 没有「整页覆盖」这种模式。改正文只有两条路,二选一:
edits —— 查找替换(主要方式)edits 为空或不传,但传了 body_content —— 追加到页尾同时传 edits 和 body_content 是错误([INVALID_INPUT],什么都没写):这两种是不同性质的内容操作,插件不会猜你想先做哪个。既要改又要追加,就分两次调用。
也就是说:想动哪段就只发哪段,页面上其它内容永远不会因为你没提到而消失。 这条约束是设计层面的,不是靠守卫救回来的。
{ "id": "<page-id>", "edits": [
{ "old_str": "要找到的那段原文", "new_str": "替换成这段" },
{ "old_str": "第二处", "new_str": "第二次替换" } ] }
每次编辑最多三个字段:
| 字段 | 含义 |
|---|---|
old_str |
要查找的原文(必填、非空),必须命中 |
new_str |
替换内容(必填);传 "" 就是删除命中的那段 |
replace_all_matches |
可选,默认 false。命中多处时是否全部替换 |
replace_all_matches 是放在每一条 edit 里面的,不是顶层参数——你可以让一条全部替换、另一条只替换唯一的一处。
一次调用最多 100 条 edits。
1. 一次调用的三种结局,取决于「有几条命中」。 这是 Notion 那条 markdown 接口自己的规则(不是插件在本地预检):
| 情况 | 结果 |
|---|---|
| 全部命中 | 成功,无告警 |
| 部分命中 | 成功 + warnings(code: "no_match"),已命中的照常生效、不回滚 |
| 一条都没命中 | 失败(Notion 返回 validation_error,插件报 [NTN_FAILED]),什么都没写 |
No matches found for "<old_str>". Fetch the page again and base old_str on the current page text.
看到「一条都没命中」的失败,不要原样重试(同样的 old_str 只会再失败一次),也不要瞎猜着改:用 notion_get 重新读一遍页面,从当前正文里重建 old_str。页面被别人改过、或你自己前面的编辑已经改掉了它(见规则 3),都会导致匹配不到。
一句话记法:还有别的命中 → miss 只是告警;全都 miss → 整次失败。
2. 匹配到多处时,若没设 replace_all_matches: true,整次调用被拒绝([NTN_FAILED]),一个字节都不写:
Multiple matches found for "<old_str>". Found N matches...
改全部位置就设那条 edit 的 replace_all_matches: true;只改一处就扩大 old_str 的上下文让它唯一(多带上前后几个字)。
注意这条和规则 1 的区别:多处命中和一条都没命中都会拒绝整次调用(两个都来自 Notion 的
validation_error);只有部分命中才是"成功 + 告警"。命中多处时优先扩大上下文而不是直接开replace_all_matches——它可能改到你以为只出现一次的地方。而且只有 Notion 知道哪一条是歧义的:插件不做本地预检,所以它没法提前告诉你"是第几条
old_str命中多处"。看到拒绝后要自己按old_str判断是哪一条不够独特。
3. 多条 edits 按顺序执行,且只发一个请求。 第 2 条编辑看到的是第 1 条改完之后的结果——所以后面那条的 old_str 甚至可以命中前面刚插入的文字。反过来说,如果第 1 条改掉了第 2 条要匹配的原文,第 2 条就会 miss:只要还有别的命中就只是告警,全都 miss 则整次失败。全部编辑在一次 Notion 请求里提交。
4. 部分成功:命中的都生效,没命中的进 warnings,调用仍算成功。 这很容易被忽略:
{ "id": "<page-id>", "edits": [
{ "old_str": "存在的文本", "new_str": "改好了" },
{ "old_str": "不存在的文本", "new_str": "改不动" } ] }
返回里 warnings 会带上「不存在的文本」那条(code: "no_match"),而整体仍是 ok: true,且不会回滚已生效的编辑。返回里还有几个计数,可以直接核对:
| 字段 | 含义 |
|---|---|
edits_sent |
发出的编辑条数 |
edits_applied |
实际生效的条数(= 发出数 − miss 数) |
edits_missed |
没命中的条数(= code: "no_match" 的告警数) |
一定要读 warnings / edits_missed,别看到 ok 就以为全部都改上了。想把「全部命中」当成硬性要求,就一次只发一条编辑,或核对 edits_missed === 0。
warnings在一切正常时整个字段都不出现(不是空数组),并且告警文本是 Notion 原样返回的。
5. new_str: "" 是删除。 这是删掉一段正文的正式做法——把命中的文本替换成空串。没有单独的 delete-block 参数。
old_str 的两种合法写法。 页面是 Alpha **bold** omega 时:
// ✅ 方式一:照抄 notion_get 显示的 Markdown(带记号)。任意长度、跨任意格式都能命中
{ "old_str": "**bold**" }
{ "old_str": "Alpha **bold** omega" }
// ✅ 方式二:纯文本片段,但必须完整落在同一个格式片段内
{ "old_str": "bold" }
{ "old_str": "Alpha " }
// ❌ 唯一的失败写法:纯文本,但跨过了格式边界
{ "old_str": "Alpha bold" }
规矩一句话:不确定就照抄记号。 剥掉记号只在「整段都落在同一个格式片段内」时才行。
实测确认(5 个格式片段的段落 A first **B bold** C middleD codeE last):带记号的 **B bold**、`D code`、A first **B bold** C middle 以及整段全部命中;纯文本 B bold、C middle 也命中;但 first B、bold C、middle D 这类跨边界的纯文本全部失败。
# 标题、- 列表项、代码块内的文字同理:整个块照抄或块内单片段都能命中,跨片段的纯文本不行。
new_str 里的换行决定块结构。 单个 \n 是同一个块内的换行(soft break);\n\n 是分成两个块。想生成新的段落、标题、列表项,就用 \n\n 隔开。
old_str 可以跨块。 用 \n 或 \n\n 把相邻两块的文本连起来就能命中跨块的一段,替换时整段按 new_str 的块结构重写。
truncated: true 时不要用它来写 old_str。 返回里带 truncated: true 表示 Notion 截断了返回的正文——那不是完整页面,从里面复制出来的 old_str 很可能匹配不到,或匹配到你以为只有一处的地方。补救办法:format="json" 看 unknown_block_ids;子页面各有自己的 id,可以单独读;notion_page_edit 只要有准确的 old_str 就能改,不需要先读完整页;数据库改用 notion_query 按 limit / start_cursor 取行。实测截断很少见——229 KB、1000 块的页面照样整页返回。
old_str 不要覆盖到子页面/子数据库块。 如果 old_str 太宽、把嵌入的 <page url="…">标题</page> 或 <database> 块也包了进去,new_str 就会删掉那个子页面,Notion 会直接拒绝:
[NTN_FAILED] 这次编辑会删除页面 <id> 里的子页面或子数据库,Notion 因此拒绝执行(本次没有写入任何内容)。
old_str 匹配的是整段文本,很容易连带覆盖 `<page url="...">…</page>` 或 `<database ...>` 这类块。
请把 old_str 缩小到那段文字本身(不要包含子页面/子数据库块)…
这条拒绝来自 Notion 的 allow_deleting_content 保护(插件故意不打开它),报成 [NTN_FAILED] 但消息是重写过的、会直接告诉你怎么改——把 old_str 缩小到文字本身,别去换错误码或重试。
注意:new_str: "" 删文字不受这条限制(删文本不需要那个开关)。被拦住的只有"连带删掉子页面/子数据库"这种情况。
$ 的转义notion_get 返回的 Markdown 里,$ 会被转义成 \$(避免被当成公式定界符)。但 old_str 要用原始的 $,不要带反斜杠:
// 页面上是 $100
{ "old_str": "$100", "new_str": "$200" } // ✅
{ "old_str": "\\$100", "new_str": "$200" } // ❌
| 想做的事 | 用法 |
|---|---|
| 改一句话 / 改数字 / 改状态文字 | edits,old_str 带足够上下文 |
| 删掉一段 | edits 加 new_str: "" |
| 在结尾补一段 | body_content,edits 省略 |
| 在中间插一段 | edits:把插入点前后的文本当 old_str,new_str 里带上新内容 |
| 既改又要追加 | 分两次调用(edits 与 body_content 不能同时传) |
| 整页重写 | 没有这种操作。先建新页再 trash 旧页,或逐段替换 |
notion_page_trash 不是对 ntn pages trash 的薄包装:它先判定目标类型,再决定这一次调用能不能删。
| 情况 | 行为 |
|---|---|
| 目标是 database / data source | 直接拒绝。删除数据库会连带它的数据源和全部行,破坏性远超删页面,复核也不能授权它——交给用户手动确认。 |
| 目标已在回收站/已归档 | 返回成功(already_archived: true,changed: []),不报错。ntn 对已归档对象返回 400 Can't edit block that is archived,那看起来像故障并诱导重试,而删除本来就应当幂等。什么都没有写,所以 changed 为空。 |
| 目标没有子页面 | 这一次调用直接删除。没有连带损失,也就没有要复核的东西。 |
| 目标有子页面 | 这一次调用不删:返回 kind: "pending",正文列出每个会被连带删掉的子页面(标题 + id),并要求在同一轮的后续模型步骤里用 double_check 回答。 |
// 有子页面时,调用本身不删;答复 pending 才删
double_check({ "id": "0003", "yes": true }) // 连同整棵子树
double_check({ "id": "0003", "yes": false }) // 取消
为什么不在参数里给个「我确认」的开关:参数可以在同一个调用里填,模型还没看到子页面清单就已经点头了。 Pending 必须等到下一步才能答(doublecheck 会拒绝同一步骤里的自我确认),所以「先看到范围、再决定删不删」是流程强制的,不是靠自觉。
复核服务不可用时 fail closed:doublecheck 插件没装或没启用,有子页面的删除会报 [REVIEW_UNAVAILABLE] 并且什么都没删;没有子页面的删除不受影响,插件可以单独使用。
复核不是事务锁:放行之后到真正 spawn ntn pages trash 之间还有约一秒的窗口,期间别人往这个页面里新加的子页面不会被这次复核看到。这是已知残差——Notion 没有提供带版本号的删除前置条件。
和 doublecheck 的版本关系:pending 那个值的字段集合归 doublecheck 所有,所以我们的输出 schema 对它放开(只要求 kind / id / reason)——他们加字段不会让这个工具报「自己的返回值不符合自己的 schema」。反过来,如果我们依赖的四条契约变了(checkpoint 变异步、pending 改名、服务名变、风险校验变严),我们会 fail closed 而不是静默放行。
子页面检查走 GET v1/blocks/{id}/children,会顺着 next_cursor 翻到最后一页再判断。不能只翻一页:该接口是按块分页的(默认每页 100 个块),一个大页面里大量段落会把子页面挤到后面的页,漏读就会漏报子页面、进而误判可以删除。翻页上限只作为失控保护(100 页 × 100 块 ≈ 10000 块),真到达上限时拒绝删除而不是猜。子块列表读不出来/解析不了时同样拒绝——"没能枚举"不等于"没有子页面",范围说不清就不能让模型签。
yes / recursive 这两个旧参数已经删掉。调用里仍然带着它们不会报错,但也不会有任何作用——「我确认要删」这句话现在只能由 pending 的答复表达。
模型不需要记住精确拼写。插件在参数层做类型宽容,只有无法对应到任何明确意图的输入才报错。
类型宽容:yes(只剩 notion_page_edit 的行→行覆盖在用)接受 true / "true" / "yes" / 1 / "1";limit 接受数字字符串,小数会被截断(2.5 → 2,但 <= 0 仍报错);id 接受 UUID、32 位 hex 或整条 Notion URL。notion_page_trash 的旧参数 yes / recursive 会被忽略而不报错,避免带着上一版记忆的调用白丢一轮。
不做的事:拼写错误不做模糊匹配,yes 不接受语义含糊的字符串。猜错可能执行成另一个真实存在的写操作,风险大于收益。删除的确认不在这套宽容范围内——它是 pending + double_check,不看参数。
工具拆分之后不再有
action的同义词表(delete→trash之类):工具名就是动作,模型直接调用正确的工具,不存在"选错分支"这回事。
id 类型基本不用记:notion_get 按 page → database → data source 三级回退,notion_query 对 database id 自动 resolve,所以 notion_search 返回的 id 可以直接给这两个工具。但 notion_page_edit / notion_page_trash 只接受页面 id(或数据库行 id):拿 database/data source id 去改会失败,要把容器写成 database:<id> / data-source:<id> 传给 create 或 edit 的 parent。
create / edit / trash 拆成三个工具,各自只声明自己接受的参数,非法组合在 schema 层就不存在。notion_page_create 是两步写(建页 + 属性 PATCH),第二步失败会自动把新页 trash 掉,并报告 partial / rollback。edit 的 parent 在行→行时会做 Parent-item 环检测;覆盖已有父级必须显式 yes: true。内容修改与移动可以同时给,内容先应用:内容改完了移动才失败,你会看到内容已改、位置未变的报告,而不是两者都白做。notion_get 标出可写列和 Parent-item 候选列,写页工具和 notion_query under 直接复用同一套选择逻辑。--content:改正文是 PATCH v1/pages/{id}/markdown,不是 ntn pages edit --content(后者的 --content 语义就是"整页替换",正是被移除的那个能力)。查找替换语义由 Notion 这个接口原生提供。pages create/edit 没有 --icon,插件把顶层 icon 归一化后并入同一次属性 PATCH(只有 icon 时发 icon-only 请求),create 时图标也参与回滚判定。三个写工具的返回值结构一致:必有 ok: true、properties、changed;另有 id、url 等。
changed 的取值只有这几个:body_content / properties / icon / title / parent / parent_item_relation / trashed。用它来判断"到底写成了什么"——例如同时给了内容和 parent 时,changed 里会同时出现 body_content 和 parent。空数组 = 有意为之的空操作(例如已归档页再 trash 一次)。
notion_page_trash 是唯一可能返回 kind: "pending" 的写工具:那个值不是成功,也不是错误——什么都没写,只是把范围摆出来等复核。它带着 id(交给 double_check)、reason(删什么、连带什么)和 details(同样的证据,排序后的 JSON)。看到 kind: "pending" 就不要汇报「已删除」。
| 只在对应场景出现的字段 | 含义 |
|---|---|
created: true |
create 成功 |
appended: true |
edit 走了追加路径 |
already_archived: true |
trash 空操作(页本来就在回收站) |
already_linked: true |
移动空操作(本来就在那个父级下) |
replaced_parent_ids |
行→行移动覆盖掉的旧父级 |
cycle_check |
页面移动的环检测结果(walked / partial) |
edits_sent / edits_applied / edits_missed |
仅在传了 edits 时出现 |
warnings |
{code, message} 数组,一切正常时整个字段缺席(不是空数组) |
truncated: true |
返回的正文被截断,不是完整页面 |
warnings 里的文本是 Notion 原样返回的,不加工。
每个工具都是一个原生 DSH ToolDefinition,直接 ctx.tools.register:
{
name, description, parameters, // 模型可见:名称 + 描述 + JSON Schema(DSH 强制子集)
output: { schema, render }, // 返回值的规范 JSON 契约 + 渲染成文本块
execute(args, exec) // 工具体:返回规范值,抛错即失败结果
}
因此模型的全部认知只来自 description + 参数描述——单个工具定义没有独立的 prompt/guideline 字段。DSH 另有 ctx.systemPrompt.section() 可以在系统提示里加段落,本插件故意不用:6 个工具的规则放在各自的 description 里更贴近使用现场。插件把使用规则(查找替换语义、必填项、回滚语义、分页方式)写在这两处,而不是散落在别处。
execute 返回的是领域值(如 { ok, id, url, changed, warnings }),文本由 output.render 派生,因此同一个定义在原生 function calling 和 PTC(run_code)两种呈现方式下都成立。
注意 details 不会到模型:DSH 只把抛出的 message 渲染成 Error: <message>,错误对象上的 .details(含 hint_properties)仅用于测试和日志。所以可写列清单是被写进消息文本的,不是靠 details 传递。同理,部分成功的告警写进返回值的 warnings,因为它们必须被模型看到。
DSH 把抛出的错误渲染成 Error: <message>,只对它自己的 HarnessError 保留结构化 { name, code };普通 Error 的 code 会被丢弃。所以本插件把错误类别写进消息开头,确保模型与日志都能看到:
| 前缀 | 含义 | 模型该怎么做 |
|---|---|---|
[INVALID_INPUT] |
参数不可用(未知列名、只读列、id 非法、edits 格式不对…) |
读消息里的可写列清单 / 提示,改参数重试 |
[NTN_FAILED] |
ntn 本身失败(未登录、无效 token、Notion API 报错、exit ≠ 0) |
检查 token / workspace / 权限,或原样上报 |
[ABORTED] |
调用被取消(子进程已被 SIGTERM)。注意:DSH 层超时不走这个码——dsh-tool-call-timeout-policy 会报 TOOL_TIMEOUT;ntn 自己的进程超时归入 [NTN_FAILED] |
通常无需重试,或换更小的查询 |
[PARTIAL_WRITE] |
后续写入失败且回滚也失败,工作区留下了半成品 | 用消息中给出的 id 手动 trash 或 edit 清理 |
[REVIEW_UNAVAILABLE] |
有子页面的删除需要 doublecheck 复核,但复核通道不可用或返回了读不懂的结果。什么都没删,参数也不是问题 |
告诉用户启用 doublecheck 插件,或让用户在 Notion 里删;不要改参数重试 |
[NTN_FAILED](details.refused = "deleting_child_content") |
这次编辑会连带删掉页面里嵌入的子页面/子数据库,Notion 的 allow_deleting_content 保护拒绝执行(插件故意不开这个开关)。消息是重写过的,不是原样 ntn 输出 |
把 old_str 缩小到那段文字本身,避开 <page>/<database> 块 |
成功回滚的 create 不报 [PARTIAL_WRITE],而是保留真正失败的那个类别([NTN_FAILED] / [INVALID_INPUT]),并附带 partial: false。
notion_page_edit 的三种命中结局里只有「部分命中」不是错误:命中多处和一条都不命中,都是由 Notion 返回 validation_error、插件原样报成 [NTN_FAILED],且整次无写入(插件不做本地预检,所以这两种都来自 Notion 本身);部分命中是成功 + warnings。
notion_file)icon)npm install
npm run build # 打包 dist/index.js(esbuild)
npm test # 单元测试(mock,不需要 token、不联网)
npm run typecheck # tsc --noEmit
插件源码结构:
src/dsh/ # DSH Host 层:tool.ts 工具契约、index.ts 注册、doublecheck.ts 复核适配、config/runner(token 与 ntn 调用)
src/tools/ # 工具实现:search / get / query / page(create、edit、trash 三个工具)
src/text/ # ★ 模型可见的全部文案:工具描述 / 参数描述 / 待复核正文 / 结果句子 / 错误消息
src/lib/ # 共享工具库(id 解析、schema 提取、ntn runner、失败诊断等)
test/ # 单元测试(不依赖真实 Notion)
src/text/模型看到的每一个字都在这一个文件夹里,不用翻实现代码:
| 文件 | 装什么 | 模型什么时候读到 |
|---|---|---|
tools.ts |
六个工具的 description | 选工具之前 |
params.ts |
参数说明、props 说明、icon 三种形式 |
填参数时 |
checks.ts |
「待复核」正文与答复行(pending) | 删除被挂起、等复核时 |
results.ts |
结果里的句子(发生了什么) | 成功后 |
errors.ts |
全部错误消息 | 失败后 |
失败原因的提示表在 src/lib/failures.ts(errors.ts 说「哪一步失败」,那里说「大概是为什么」)。
写作约定和「什么故意不放在这里」见 src/text/README.md。
test/dsh-contract.test.ts 有两条守卫测试盯着:throw 不许自己拼长句、description: 不许内联字面量。
MIT
Six consolidated Notion tools for DeepSeek Harness, built on Notion's official ntn CLI instead of the official Notion MCP server.
The official Notion MCP ships dozens of overlapping tools. This plugin keeps the high-frequency surface small — search, get, query, and the page write trio create / edit / trash — so the model carries less schema, picks the right call more reliably, and still covers everyday page/database work. Each tool is named for the action it performs; there is no action parameter, so notion_page_create cannot accidentally receive an old_str and notion_page_edit cannot accidentally receive a create-only argument. Writes are safe by construction: property values are validated against the data-source schema, a failed post-create PATCH trashes the new page, edits are find-and-replace so untouched text can never vanish, and moves refuse cycles and silent parent overwrites.
brew install --cask notion-cli # or: npm i -g ntn
plugin_manager install_bundle target=/absolute/path/to/dsh-notion
NOTION_API_TOKEN env var, or ./notion-token beside the plugin, or ntn login. Share target pages/databases with your Notion integration.
{ "tool": "notion_search", "args": { "query": "roadmap", "limit": 10 } }
{ "tool": "notion_get", "args": { "id_or_url": "<page-or-db-id>" } }
{ "tool": "notion_page_create", "args": { "parent": "page:<id>", "title": "Title",
"body_content": "# Title\n\nBody" } }
{ "tool": "notion_page_edit", "args": { "id": "<page-id>", "edits": [
{ "old_str": "Status: TODO", "new_str": "Status: DONE" } ] } }
{ "tool": "notion_page_trash", "args": { "id": "<page-id>" } }
{ "tool": "notion_query", "args": { "database_or_data_source": "<ds-id>",
"filter": { "property": "Done", "checkbox": { "equals": false } } } }
body_content (the Markdown body) and icon (an emoji such as "🎯", an http(s) image URL, or null to clear) are top-level parameters, siblings of title. props is only a map of real database columns: props.icon and props.body_content carry no special meaning any more, so on a data source that really has a column named icon or body_content those keys write that column. Use the top-level parameters for the page's icon and body.
parent means different things to the two tools. For notion_page_create: page:<id> (child page), a bare page id (same, after checking it is not a database row), a bare row id (child row in that row's data source, with the Parent-item relation set), data-source:<id> (peer row), or database:<id> (resolved through ntn datasources resolve to its unique data source — several ids are an error, never a guess). For notion_page_edit (a move) only page forms are accepted — page:<id>, a bare page id, or a bare row id to re-link that row's Parent-item relation; database:<id> and data-source:<id> are refused, because adding a row is create's job.
notion_page_create takes an OPTIONAL parent — omit it to create the page at the workspace root (Notion's own default when there is no parent; such a page only has a title, so props cannot carry columns). Otherwise: page:<id>, database:<id>, data-source:<id>, or a bare database row id (a child row linked through the data source's Parent-item / self-relation column). Properties, icon and Parent-item link are applied by a second request; if that request fails the new page is trashed so no half-made page is left behind, and the error reports rollback (trashed / trash_failed / unknown_id) plus partial: true if something was left behind. A page in a database also needs a title: the title parameter, the title property in props, or a leading # Heading of body_content.
notion_page_edit has no whole-page replace mode. There are exactly two ways to change a body, and they are mutually exclusive:
edits — find and replace (the normal path).edits (or pass it empty) and pass body_content — append to the end of the page.Passing both edits and body_content is an [INVALID_INPUT] error with nothing written: they are different content operations, and the plugin will not guess which one you meant. To change and append, make two calls.
So you only ever send the part you mean to touch, and text you did not mention can never be lost.
{ "id": "<page-id>", "edits": [
{ "old_str": "the exact old text", "new_str": "the replacement" },
{ "old_str": "text that appears often", "new_str": "replaced everywhere",
"replace_all_matches": true } ] }
Each edit is { old_str, new_str, replace_all_matches? }. old_str and new_str are both required; old_str must be non-empty and new_str may be "", which deletes the matched text. replace_all_matches belongs on the individual edit, not the top level, so one edit can replace every occurrence while another replaces a unique one. At most 100 edits per call.
old_str must match exactly, and the outcome depends on how many edits match (this is Notion's rule for the markdown endpoint, not a local pre-check):
| How many match | Result |
|---|---|
| All of them | Success, no warnings |
| Some of them | Success + warnings (code: "no_match"); the matches are applied, nothing is rolled back |
| None of them | Failure — Notion returns validation_error, surfaced as [NTN_FAILED]; nothing is written |
A total miss reads No matches found for "<old_str>". Fetch the page again and base old_str on the current page text. Do not simply retry the same old_str — it will fail identically. Re-read with notion_get and rebuild old_str from the current body; the page may have changed, or an earlier edit may already have consumed that text.
More than one match refuses the whole call ([NTN_FAILED]); nothing is written. Multiple matches found for "<old_str>". Found N matches... Either add surrounding context to make old_str unique, or set replace_all_matches: true on that edit. This is a separate case from a total miss.
Multiple edits run sequentially in one request. Edit 2 sees edit 1's result — a later old_str can match text an earlier edit just inserted, and conversely an earlier edit can consume the text a later one was going to match (which then misses; fatal only if nothing else matches). All edits are submitted in a single request.
Partial success is easy to miss. When some edits match, the result carries edits_sent, edits_applied (= sent − misses) and edits_missed (= count of no_match warnings). Always read warnings / edits_missed — ok does not mean every edit landed. If you need all-or-nothing, send one edit per call or assert edits_missed === 0.
warnings is omitted entirely when everything matched (it is absent, never an empty array), and its text is echoed verbatim from Notion.
So three outcomes, three different signals: multiple matches refuses everything, no match at all fails everything, and only partial is a success that still needs checking.
And only Notion knows which edit was ambiguous — the plugin does no local pre-check, so it cannot tell you in advance that "edit #2 matches more than once". After a refusal, work out from your own old_str values which one was insufficiently unique.
Five details that bite:
old_str matches the block's plain text, not the markdown. old_str: "boldtext" matches inside **boldtext** and the bold formatting survives. Do not include **, _, #, > or - markers — and remember that notion_get returns markdown, so strip the markers when copying an old_str out of it.new_str decide block structure. A single \n is a line break inside one block; \n\n starts a separate block. old_str may span blocks using \n or \n\n.$ is escaped as \$ in the markdown notion_get returns, but old_str must use the plain $.old_str from a truncated: true result. That marks a body Notion truncated, so it is not the full page and a copied old_str may match nothing — or match somewhere you did not expect.old_str must not span a child-page or child-database block. If it is broad enough to swallow an embedded <page url="…">title</page> or <database> block, the new_str would delete that child page, and Notion refuses the edit. The plugin deliberately does not send allow_deleting_content, so the refusal arrives as [NTN_FAILED] with a rewritten message telling you to narrow old_str to the text itself — do that; do not change the error code or retry. Note that new_str: "" deleting plain text is unaffected (that needs no such flag); only deleting nested pages/databases is blocked.notion_page_edit needs id plus at least one of edits / body_content / title / props / icon / parent. props carries real columns only. A lone icon change never disturbs the body or other properties.
One call can combine all of these. They run in a fixed order — (1) body, (2) title/props/icon, (3) parent move — and changed reports each one that actually happened. If step (2) or (3) fails after the body was already written, the error says exactly what already landed; read it before retrying.
parent moves the page, absorbing the old attach action: page:<id> (or a bare page id) re-parents via POST /v1/pages/{id}/move, and a bare row id re-links that row's Parent-item relation. It is not a file upload, and database/data-source parents are not accepted here — add a row with notion_page_create. Cycle-forming moves are refused. Re-linking a row that already has a different parent requires yes: true (otherwise it refuses and shows the current parent ids); yes without parent is an error.
notion_page_trash classifies its target before calling ntn pages trash, and it takes exactly one argument: id.
already_archived: true, changed: []) instead of surfacing Notion's 400 Can't edit block that is archived, so a retry is harmless. Nothing is written, so changed stays empty.kind: "pending": the reason names every child page (id + title) that would go with it, and answering that pending needs a double_check in a later model step (yes: true deletes the whole subtree, yes: false cancels). double_check refuses to be answered inside the step that produced the pending, so "see the scope, then decide" is enforced by the flow rather than by good behaviour.// the call itself deletes nothing; answering the pending is what deletes
double_check({ "id": "0003", "yes": true }) // the whole subtree
double_check({ "id": "0003", "yes": false }) // cancel
There is deliberately no yes/recursive parameter any more: a parameter can be filled in the SAME call that names the page, before the child list was ever read. A call that still carries them is accepted and they are ignored — they no longer express anything.
The review fails closed: if the doublecheck plugin is not installed or not enabled, a delete that would take child pages with it reports [REVIEW_UNAVAILABLE] and deletes nothing. A leaf delete does not need the service, so the plugin stays usable on its own.
The review is not a transaction lock: about a second passes between "proceed" and the actual spawn, and a child page created in that window is not covered by the review that approved it. That is a known residual — Notion offers no versioned precondition for a delete.
The child list is read from GET v1/blocks/{id}/children following next_cursor to the end: that endpoint pages over blocks, so one page of 100 blocks can hide child pages behind paragraphs, and a truncated read would under-report the subtree. The page cap (100 pages × 100 blocks ≈ 10 000 blocks) is a runaway guard only, and reaching it refuses the delete rather than guessing. An unreadable or unparseable child list also refuses: "could not enumerate" is not "has no children" — a scope that cannot be stated cannot be signed off.
The model does not have to memorize exact spellings. "true"/"yes"/1 for yes (which only notion_page_edit still takes, for a row→row parent overwrite), numeric strings for limit, and full Notion URLs for id are all accepted. notion_page_trash still accepts the removed yes/recursive keys and ignores them, so a caller carrying the previous contract does not lose a round trip. Misspellings are not fuzzy-matched: guessing could perform a different real write. With the action split there is no longer an action-synonym table — the tool name is the action.
Any id from notion_search works with any tool: notion_get falls back page → database → data source, and notion_query resolves a database id automatically.
Each tool is a native DSH ToolDefinition registered through ctx.tools.register: name, description and parameters are what the model sees, output.schema / output.render define how a successful canonical value is validated and rendered, and execute returns that value (a thrown error becomes the call's error result). DSH has no separate prompt/guideline channel, so usage rules live in the description and the parameter descriptions.
Each tool declares a per-call timeoutMs (enforced by dsh-tool-call-timeout-policy) and isConcurrencySafe (the read-only tools — notion_search, notion_get, notion_query — run in parallel; the three page write tools stay exclusive, all with a 180 s budget), and every ntn spawn observes exec.signal, so a cancelled or timed-out call kills the child process instead of leaving it running.
Body writes go through PATCH v1/pages/{id}/markdown rather than ntn pages edit --content — the CLI flag's semantics are "replace the page with", which is exactly the capability that no longer exists here. Property, icon and Parent-item writes remain PATCH v1/pages/{id}.
notion_page_trash is the only write tool that can return kind: "pending" instead: that value is neither a success nor an error — nothing was written, and it carries id (answer it with double_check), reason (what goes, and what goes with it) and details (the same evidence as sorted JSON). Do not report a pending as a deletion.
Otherwise all three write tools return the same shape: always ok: true, properties and changed; plus id, url and the other fields listed above where they apply. changed uses a closed vocabulary — body_content, properties, icon, title, parent, parent_item_relation, trashed — so it is how you tell what actually landed (a combined content edit plus move lists both body_content and parent); an empty array is a deliberate no-op. warnings is an array of { code, message }, echoed verbatim from Notion, and the field is omitted entirely when there is nothing to say — never an empty array.
DSH renders a thrown error as Error: <message> and keeps a structured { name, code } only for its own HarnessError; a plain Error's code is dropped. This plugin therefore puts the category at the front of the message: [INVALID_INPUT], [NTN_FAILED], [ABORTED], [PARTIAL_WRITE] (a failed write whose rollback also failed, so a half-made page remains — the message carries the id to clean up), and [REVIEW_UNAVAILABLE] (a subtree delete needed a review and the review channel was unusable: nothing was deleted and the arguments are not the problem). One [NTN_FAILED] sub-case is worth recognizing: an edit that would delete a nested child page or database comes back with details.refused = "deleting_child_content" and a rewritten message (not raw ntn output) telling you to narrow old_str past the <page>/<database> block.
Of the three find-and-replace outcomes only the partial one is not an error: a multiple match and a total miss both come back from Notion as validation_error and are surfaced as [NTN_FAILED] with nothing written (the plugin does no local pre-check, so both originate with Notion). A partial match is not an error at all — it is a success carrying warnings.
MIT
CLASSIFICATION EVIDENCE
系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: deepseek-harness。