初始化原生 Pi 示例与确定性验证命令
This commit is contained in:
commit
a2def106fb
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
.env
|
||||
.pi-example-note.txt
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Mario Zechner
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
20
README.md
Normal file
20
README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# d-pi-02:原生 Pi 示例
|
||||
|
||||
当前版本:`repo-02-v1`,运行 `/version` 可获得确定性标记,不调用模型。
|
||||
|
||||
## 本地开发与发布
|
||||
|
||||
1. 在本地 IDE 克隆本仓库,修改 `SYSTEM.md`、扩展或其他资源。
|
||||
2. 在本地检查修改,提交并推送到 Git。
|
||||
3. 在平台绑定此仓库,点击同步并查看新提交,再发布新版本。
|
||||
4. 打开对应版本会话,用 `/version` 检查实际加载的版本。修改版本时同步更新提示词和命令中的版本标记。
|
||||
|
||||
模型、模型服务地址和密钥在平台 Web 界面配置;不要写入 Git。仓库不含任何密钥。
|
||||
|
||||
`package.json` 使用 Pi 原生 `pi` manifest。运行时提供 Pi 0.86.1 peer dependencies,无需在仓库安装第二份 Pi。平台从仓库根目录读取 `SYSTEM.md`;在独立 Pi CLI 中使用时显式指定该系统提示词。
|
||||
|
||||
## 扩展与交互
|
||||
|
||||
`hello.ts`、`protected-paths.ts`、`permission-gate.ts` 原样来自 Pi 0.86.1 官方示例(MIT),提供 hello 工具、敏感路径拦截和危险 shell 命令确认;这些是示例策略,不是完整安全沙箱。
|
||||
|
||||
`/ask` 展示确认框并返回 `ask:yes` 或 `ask:no`,不调用模型。`/block [毫秒]` 在 worker 内同步占用 CPU,默认 30000、范围 1–60000 毫秒,用于验证平台停止运行时能终止 worker。请仅在隔离测试会话使用;单独 Pi CLI 中它会阻塞所在进程。正常完成返回 `block:done`。
|
||||
2
SYSTEM.md
Normal file
2
SYSTEM.md
Normal file
@ -0,0 +1,2 @@
|
||||
你是简洁、可靠的中文助手。当前示例版本标记:repo-02-v1。
|
||||
先核实事实,再回答;没有执行的操作不要宣称成功。
|
||||
26
extensions/hello.ts
Normal file
26
extensions/hello.ts
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Hello Tool - Minimal custom tool example
|
||||
*/
|
||||
|
||||
import { Type } from "@earendil-works/pi-ai";
|
||||
import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
|
||||
const helloTool = defineTool({
|
||||
name: "hello",
|
||||
label: "Hello",
|
||||
description: "A simple greeting tool",
|
||||
parameters: Type.Object({
|
||||
name: Type.String({ description: "Name to greet" }),
|
||||
}),
|
||||
|
||||
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
||||
return {
|
||||
content: [{ type: "text", text: `Hello, ${params.name}!` }],
|
||||
details: { greeted: params.name },
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
pi.registerTool(helloTool);
|
||||
}
|
||||
34
extensions/permission-gate.ts
Normal file
34
extensions/permission-gate.ts
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Permission Gate Extension
|
||||
*
|
||||
* Prompts for confirmation before running potentially dangerous bash commands.
|
||||
* Patterns checked: rm -rf, sudo, chmod/chown 777
|
||||
*/
|
||||
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
const dangerousPatterns = [/\brm\s+(-rf?|--recursive)/i, /\bsudo\b/i, /\b(chmod|chown)\b.*777/i];
|
||||
|
||||
pi.on("tool_call", async (event, ctx) => {
|
||||
if (event.toolName !== "bash") return undefined;
|
||||
|
||||
const command = event.input.command as string;
|
||||
const isDangerous = dangerousPatterns.some((p) => p.test(command));
|
||||
|
||||
if (isDangerous) {
|
||||
if (!ctx.hasUI) {
|
||||
// In non-interactive mode, block by default
|
||||
return { block: true, reason: "Dangerous command blocked (no UI for confirmation)" };
|
||||
}
|
||||
|
||||
const choice = await ctx.ui.select(`⚠️ Dangerous command:\n\n ${command}\n\nAllow?`, ["Yes", "No"]);
|
||||
|
||||
if (choice !== "Yes") {
|
||||
return { block: true, reason: "Blocked by user" };
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
30
extensions/protected-paths.ts
Normal file
30
extensions/protected-paths.ts
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Protected Paths Extension
|
||||
*
|
||||
* Blocks write and edit operations to protected paths.
|
||||
* Useful for preventing accidental modifications to sensitive files.
|
||||
*/
|
||||
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
const protectedPaths = [".env", ".git/", "node_modules/"];
|
||||
|
||||
pi.on("tool_call", async (event, ctx) => {
|
||||
if (event.toolName !== "write" && event.toolName !== "edit") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const path = event.input.path as string;
|
||||
const isProtected = protectedPaths.some((p) => path.includes(p));
|
||||
|
||||
if (isProtected) {
|
||||
if (ctx.hasUI) {
|
||||
ctx.ui.notify(`Blocked write to protected path: ${path}`, "warning");
|
||||
}
|
||||
return { block: true, reason: `Path "${path}" is protected` };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
25
extensions/testing.ts
Normal file
25
extensions/testing.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
const reply = (content: string) => pi.sendMessage({ customType: "repo-test", content, display: true }, { triggerTurn: false });
|
||||
pi.registerCommand("ask", {
|
||||
description: "测试原生确认框(不调用模型)",
|
||||
handler: async (_args, ctx) => {
|
||||
reply(await ctx.ui.confirm("确认测试", "是否继续?") ? "ask:yes" : "ask:no");
|
||||
},
|
||||
});
|
||||
pi.registerCommand("block", {
|
||||
description: "同步阻塞 worker 测试取消,默认 30000 毫秒",
|
||||
handler: async (args) => {
|
||||
const duration = args.trim() ? Number(args.trim()) : 30000;
|
||||
if (!Number.isInteger(duration) || duration < 1 || duration > 60000) {
|
||||
reply("block:invalid-duration (1–60000 ms)");
|
||||
return;
|
||||
}
|
||||
const deadline = performance.now() + duration;
|
||||
// 故意同步阻塞,用来验证宿主终止 worker;不要用于生产任务。
|
||||
while (performance.now() < deadline) {}
|
||||
reply("block:done");
|
||||
},
|
||||
});
|
||||
}
|
||||
10
extensions/version.ts
Normal file
10
extensions/version.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
pi.registerCommand("version", {
|
||||
description: "显示仓库版本(不调用模型)",
|
||||
handler: async () => {
|
||||
pi.sendMessage({ customType: "repo-version", content: "repo-02-v1", display: true }, { triggerTurn: false });
|
||||
},
|
||||
});
|
||||
}
|
||||
18
package.json
Normal file
18
package.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "d-pi-02",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"keywords": [
|
||||
"pi-package"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@earendil-works/pi-coding-agent": "0.86.1",
|
||||
"@earendil-works/pi-ai": "0.86.1"
|
||||
},
|
||||
"pi": {
|
||||
"extensions": [
|
||||
"./extensions"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user