@duanz/pi-skill-trigger 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +58 -0
- package/README.md +57 -2
- package/index.ts +182 -0
- package/package.json +31 -4
package/README.en.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# skill-trigger
|
|
2
|
+
|
|
3
|
+
<p align="center"><strong>English</strong> | <a href="./README.md">中文</a></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/skill-trigger.png" alt="skill-trigger cover" width="880"></p>
|
|
6
|
+
|
|
7
|
+
<p align="center"><strong>When a task should route through one skill, do not let the model bypass it</strong><br><sub>Soft trigger injection · hard gate on built-in tools · escape hatch kept</sub></p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@duanz/pi-skill-trigger"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-skill-trigger.svg?style=flat-square" alt="npm"></a>
|
|
11
|
+
<a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-skill-trigger.svg?style=flat-square" alt="license"></a>
|
|
12
|
+
<a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<code>pi install npm:@duanz/pi-skill-trigger</code>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## Why
|
|
21
|
+
|
|
22
|
+
pi skills are progressively disclosed: the system prompt carries only name + description, and the model decides whether to `read` the body. The docs say it plainly: "models don't always do this; use prompting or /skill:name to force it".
|
|
23
|
+
|
|
24
|
+
A real miss: the user said "go look this up across the web", and the assistant called the built-in `web_search` / `fetch_content` instead of the unified search router. Quota accounting drifted, no free channels were used, and there was no follow-up verification round.
|
|
25
|
+
|
|
26
|
+
## Two layers
|
|
27
|
+
|
|
28
|
+
**Layer 1 — soft injection**: `before_agent_start` matches trigger phrases and injects an invisible reminder for this turn ("this task should go through d-reach; read its SKILL.md first").
|
|
29
|
+
|
|
30
|
+
**Layer 2 — hard gate**: `tool_call` blocks the built-in `web_search` / `fetch_content` until the matching SKILL.md has been read in this session, and names the file to read.
|
|
31
|
+
|
|
32
|
+
## Layer 2 is deliberately narrow
|
|
33
|
+
|
|
34
|
+
1. **Only those two built-in tools; bash untouched.** Daily `curl` runs through bash, and gating it would need an error-prone domain allowlist.
|
|
35
|
+
2. **Local resources pass**: input without `http(s)://` is allowed (`file://`, local images and videos).
|
|
36
|
+
3. **Escape hatch**: any input field containing `#d-reach:allow` passes (explain to the user first); or `PI_SKILL_TRIGGER_GATE=off` disables the layer.
|
|
37
|
+
|
|
38
|
+
## Adding a skill
|
|
39
|
+
|
|
40
|
+
Add one entry to `RULES`; both layers read the same table:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
{
|
|
44
|
+
name: "d-reach",
|
|
45
|
+
path: "/home/you/.pi/agent/skills/d-reach/SKILL.md",
|
|
46
|
+
patterns: /search|look up|find out/i,
|
|
47
|
+
hint: "This task needs the web: read d-reach's SKILL.md first.",
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Limits
|
|
52
|
+
|
|
53
|
+
- Patterns are regexes; rewording can miss. It is a safety net, not a guarantee.
|
|
54
|
+
- The hard gate covers built-in tools only. If the model uses `curl` in bash, this extension stays out of the way (intentionally).
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT © DuanZ
|
package/README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# skill-trigger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center"><a href="./README.en.md">English</a> | <strong>中文</strong></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/skill-trigger.png" alt="skill-trigger cover" width="880"></p>
|
|
6
|
+
|
|
7
|
+
<p align="center"><strong>该走统一路由的时候,别让模型绕过去</strong><br><sub>触发词软注入 · 内置工具硬门禁 · 逃生舱保留</sub></p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@duanz/pi-skill-trigger"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-skill-trigger.svg?style=flat-square" alt="npm"></a>
|
|
11
|
+
<a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-skill-trigger.svg?style=flat-square" alt="license"></a>
|
|
12
|
+
<a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<code>pi install npm:@duanz/pi-skill-trigger</code>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## 它解决什么
|
|
21
|
+
|
|
22
|
+
pi 的技能是渐进披露:系统提示里只有 name + description,正文要模型自己决定去 `read`。官方文档原话是 "models don't always do this; use prompting or /skill:name to force it"。
|
|
23
|
+
|
|
24
|
+
2026-09-25 实测漏过一次:用户说「你去全网看一下」,助手直接调了内置 `web_search` / `fetch_content`,没走 d-reach。后果是额度记账失真、免费通道一个没用、只跑了一轮没有追证轮。
|
|
25
|
+
|
|
26
|
+
## 两层防线
|
|
27
|
+
|
|
28
|
+
**层 1 — 软注入**:`before_agent_start` 里检测触发词,命中就往本轮注入一条不显示的提醒(「这个任务该走 d-reach,先读 SKILL.md」)。
|
|
29
|
+
|
|
30
|
+
**层 2 — 硬门禁**:`tool_call` 里拦住内置 `web_search` / `fetch_content`。本会话没读过对应 SKILL.md 就不放行,并给出该读哪个文件。
|
|
31
|
+
|
|
32
|
+
## 层 2 刻意做窄
|
|
33
|
+
|
|
34
|
+
1. **只拦这两个内置工具,不碰 bash**。日常 `curl` 全走 bash,拦它需要域名白名单那种易错设计。
|
|
35
|
+
2. **本地资源不拦**:输入里没有 `http(s)://` 就放行(`file://`、本地图片/视频)。
|
|
36
|
+
3. **逃生舱**:任一输入字段带 `#d-reach:allow` 即放行(需先向用户说明理由);或 `PI_SKILL_TRIGGER_GATE=off` 整层关掉。
|
|
37
|
+
|
|
38
|
+
## 扩展新技能
|
|
39
|
+
|
|
40
|
+
往 `RULES` 加一条即可,软注入与硬门禁共用同一张表:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
{
|
|
44
|
+
name: "d-reach",
|
|
45
|
+
path: "/home/you/.pi/agent/skills/d-reach/SKILL.md",
|
|
46
|
+
patterns: /搜一下|全网|search|看看有没有/i,
|
|
47
|
+
hint: "这次要联网查东西,先读 d-reach 的 SKILL.md 再做。",
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 限制
|
|
52
|
+
|
|
53
|
+
- 规则是正则,措辞变化可能漏命中——它是兜底,不是保险。
|
|
54
|
+
- 硬门禁只对内置工具生效;模型如果改用 bash 里的 `curl`,本扩展不会拦(这是有意的)。
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT © DuanZ
|
package/index.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* skill-trigger — 技能触发兜底 + 硬门禁(pi 扩展)
|
|
3
|
+
*
|
|
4
|
+
* 问题:pi 的技能是「渐进披露」——系统提示里只有 name + description,
|
|
5
|
+
* 正文要模型自己决定去 read。官方文档原话:"models don't always do this;
|
|
6
|
+
* use prompting or /skill:name to force it"。
|
|
7
|
+
*
|
|
8
|
+
* 2026-09-25 实测漏过一次:用户说「你去全网看一下」,助手直接调了内置
|
|
9
|
+
* web_search / fetch_content,没走 d-reach(搜索与爬取统一路由)。
|
|
10
|
+
* 后果:额度记账可能失真、免费通道一个没用、只有一轮没有追证轮。
|
|
11
|
+
*
|
|
12
|
+
* 两层防线:
|
|
13
|
+
* 层1 软注入 before_agent_start —— 触发词命中就往本轮注入一条不显示的提醒
|
|
14
|
+
* 层2 硬门禁 tool_call —— 拦 web_search / fetch_content 两个内置工具,
|
|
15
|
+
* 本会话没读过对应 SKILL.md 就不放行
|
|
16
|
+
*
|
|
17
|
+
* 层2 的三条边界(刻意做窄,别随手放宽):
|
|
18
|
+
* 1) **只拦这两个内置工具,不碰 bash**。日常 curl(sev.duanz.xin /
|
|
19
|
+
* api.siliconflow.cn)全走 bash,拦它才需要域名白名单那种易错设计;
|
|
20
|
+
* 而这两个内置工具正是上次绕开 d-reach 的路径。
|
|
21
|
+
* 2) **本地资源不拦**:输入里没有 http(s):// 就放行(file:// 、本地图片/视频)。
|
|
22
|
+
* 3) **逃生舱**:任一输入字段带 `#d-reach:allow` 即放行(需先向用户说明理由);
|
|
23
|
+
* 或设环境变量 PI_SKILL_TRIGGER_GATE=off 整层关掉。
|
|
24
|
+
*
|
|
25
|
+
* 可扩展:往 RULES 里加一条即可覆盖新技能(软注入与硬门禁共用同一张表)。
|
|
26
|
+
*
|
|
27
|
+
* 修改本文件后需新开会话才生效。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
31
|
+
|
|
32
|
+
interface Rule {
|
|
33
|
+
/** 技能名,用于排障 */
|
|
34
|
+
name: string;
|
|
35
|
+
/** SKILL.md 绝对路径 */
|
|
36
|
+
path: string;
|
|
37
|
+
/** 软注入的触发词;中英混合,忽略大小写 */
|
|
38
|
+
patterns: RegExp;
|
|
39
|
+
/** 软注入内容。用条件式措辞:误命中也无害(本地操作忽略本条即可) */
|
|
40
|
+
hint: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 硬门禁盯死的内置工具:绕过 d-reach 路由表的那两个 */
|
|
44
|
+
const GATED_TOOLS = ["web_search", "fetch_content"] as const;
|
|
45
|
+
|
|
46
|
+
/** 逃生舱标记:出现在任一输入字段里就放行 */
|
|
47
|
+
const ALLOW_MARKER = /#\s*d-reach\s*:\s*allow/i;
|
|
48
|
+
|
|
49
|
+
/** 层2 总开关:PI_SKILL_TRIGGER_GATE=off/1/true/no 关闭 */
|
|
50
|
+
const GATE_OFF = /^(1|true|off|no|disabled)$/i.test(
|
|
51
|
+
process.env.PI_SKILL_TRIGGER_GATE ?? "",
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
/** 每个技能每个会话最多软注入几次(防历史膨胀) */
|
|
55
|
+
const MAX_REMINDERS_PER_SESSION = 5;
|
|
56
|
+
|
|
57
|
+
const RULES: Rule[] = [
|
|
58
|
+
{
|
|
59
|
+
name: "d-reach",
|
|
60
|
+
path: "/home/duanz/.agents/skills/d-reach/SKILL.md",
|
|
61
|
+
patterns:
|
|
62
|
+
/搜|搜索|检索|查一下|查查|查找|帮我搜|找个|找一下|看看有没有|有没有类似|类似项目|爬|抓取|抓一下|读一下|这个链接|打开看看|调研|全网|上网|search|look up|scrape/i,
|
|
63
|
+
hint:
|
|
64
|
+
"本次请求可能涉及「上网找信息」(搜索 / 爬取 / 读外部链接 / 调研)。" +
|
|
65
|
+
"按纪律先 read /home/duanz/.agents/skills/d-reach/SKILL.md," +
|
|
66
|
+
"再用它的路由表选通道(免费免 Key 优先:cn.bing / gh CLI / OpenAlex / Crossref / HN Algolia)," +
|
|
67
|
+
"限额通道调用前先跑 scripts/quota.py use <svc>," +
|
|
68
|
+
"不要直接调内置 web_search / fetch_content(那会绕开额度记账)。" +
|
|
69
|
+
"若本次其实是查本地文件或自有服务(127.0.0.1 / *.duanz.xin),忽略本条。",
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
/** 每个技能已软注入次数;session_start 时清空 */
|
|
74
|
+
const reminderCount = new Map<string, number>();
|
|
75
|
+
|
|
76
|
+
/** 本会话已经读过哪些 SKILL.md(由 tool_execution_start 观察得到) */
|
|
77
|
+
const skillRead = new Set<string>();
|
|
78
|
+
|
|
79
|
+
/** 从工具输入里取全部字符串值,用于路径/标记匹配 */
|
|
80
|
+
function inputStrings(input: unknown): string {
|
|
81
|
+
if (input == null) return "";
|
|
82
|
+
if (typeof input === "string") return input;
|
|
83
|
+
try {
|
|
84
|
+
return JSON.stringify(input);
|
|
85
|
+
} catch {
|
|
86
|
+
return "";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default function (pi: ExtensionAPI) {
|
|
91
|
+
pi.on("session_start", () => {
|
|
92
|
+
reminderCount.clear();
|
|
93
|
+
skillRead.clear();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// 观察所有工具调用:一旦读到某个 SKILL.md,就认为该技能已加载
|
|
97
|
+
pi.on("tool_execution_start", async (event) => {
|
|
98
|
+
const raw = inputStrings(event?.input);
|
|
99
|
+
if (!raw) return;
|
|
100
|
+
for (const rule of RULES) {
|
|
101
|
+
if (raw.includes(rule.path)) skillRead.add(rule.name);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ── 层 2:硬门禁 ───────────────────────────────────────────
|
|
106
|
+
pi.on("tool_call", async (event) => {
|
|
107
|
+
if (GATE_OFF) return undefined;
|
|
108
|
+
|
|
109
|
+
const toolName = event?.toolName;
|
|
110
|
+
if (typeof toolName !== "string") return undefined;
|
|
111
|
+
if (!(GATED_TOOLS as readonly string[]).includes(toolName)) return undefined;
|
|
112
|
+
|
|
113
|
+
const raw = inputStrings(event.input);
|
|
114
|
+
|
|
115
|
+
// 本地资源豁免只对 fetch_content 成立:它的 url 可能指向 file:// 或本地
|
|
116
|
+
// 图片/视频。web_search 的 query 本来就不含 URL,若也套这条规则,门禁
|
|
117
|
+
// 会对自己最该管的工具失效(实测踩过:query="siliconflow jev" 被放行)。
|
|
118
|
+
const localOnly =
|
|
119
|
+
toolName === "fetch_content" && !/https?:\/\//i.test(raw);
|
|
120
|
+
if (localOnly) return undefined;
|
|
121
|
+
|
|
122
|
+
// 逃生舱:调用方显式声明要走直连
|
|
123
|
+
if (ALLOW_MARKER.test(raw)) return undefined;
|
|
124
|
+
|
|
125
|
+
// 本会话已读过任一受管技能 → 放行
|
|
126
|
+
for (const rule of RULES) {
|
|
127
|
+
if (skillRead.has(rule.name)) return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const rule = RULES[0];
|
|
131
|
+
const reason =
|
|
132
|
+
`【skill-trigger 门禁】本会话还没有读过 ${rule.name} 技能,` +
|
|
133
|
+
`按纪律先 read ${rule.path},用它的路由表选通道` +
|
|
134
|
+
`(免费免 Key 优先:cn.bing / gh CLI / OpenAlex / Crossref / HN Algolia;` +
|
|
135
|
+
`限额通道调用前先跑 scripts/quota.py use <svc>)。` +
|
|
136
|
+
`内置 ${toolName} 会绕开额度记账与免费优先原则,所以先被拦下。` +
|
|
137
|
+
`若你确认要直连(例如用户给了明确 URL、或只要一条事实且两个免费源都拿不到),` +
|
|
138
|
+
`在任一输入字段里带上 \`#d-reach:allow\` 再调一次,并先向用户说明理由。`;
|
|
139
|
+
|
|
140
|
+
return { block: true, reason };
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// ── 层 1:软注入 ───────────────────────────────────────────
|
|
144
|
+
pi.on("before_agent_start", async (event) => {
|
|
145
|
+
const prompt = typeof event?.prompt === "string" ? event.prompt : "";
|
|
146
|
+
if (!prompt) return;
|
|
147
|
+
|
|
148
|
+
for (const rule of RULES) {
|
|
149
|
+
if (!rule.patterns.test(prompt)) continue;
|
|
150
|
+
|
|
151
|
+
// 该技能已经在系统提示里加载过(如项目技能),不必再提醒
|
|
152
|
+
let alreadyLoaded = false;
|
|
153
|
+
try {
|
|
154
|
+
const skills = event.systemPromptOptions?.skills;
|
|
155
|
+
if (Array.isArray(skills)) {
|
|
156
|
+
alreadyLoaded = skills.some((s) =>
|
|
157
|
+
JSON.stringify(s ?? "").includes(rule.name),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
/* 形状不符就当没加载,继续提醒 */
|
|
162
|
+
}
|
|
163
|
+
if (alreadyLoaded) continue;
|
|
164
|
+
|
|
165
|
+
// 会话内上限:同一个会话里同一个技能最多提醒 N 次
|
|
166
|
+
const used = reminderCount.get(rule.name) ?? 0;
|
|
167
|
+
if (used >= MAX_REMINDERS_PER_SESSION) continue;
|
|
168
|
+
reminderCount.set(rule.name, used + 1);
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
message: {
|
|
172
|
+
customType: "skill-trigger",
|
|
173
|
+
content: `[skill-trigger] ${rule.hint}`,
|
|
174
|
+
// 不显示在对话里:这是给模型的提示,不是给用户看的通知
|
|
175
|
+
display: false,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
});
|
|
182
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duanz/pi-skill-trigger",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "按任务内容自动匹配技能,省掉手动挑的一步",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"pi-web"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "DuanZ",
|
|
13
|
+
"homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/skill-trigger",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/DuanZGit/D-Hub.git",
|
|
17
|
+
"directory": "extensions/skill-trigger"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"README.en.md"
|
|
23
|
+
],
|
|
24
|
+
"pi": {
|
|
25
|
+
"extensions": [
|
|
26
|
+
"./index.ts"
|
|
27
|
+
],
|
|
28
|
+
"image": "https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/skill-trigger.png"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20"
|
|
32
|
+
}
|
|
33
|
+
}
|