pi-command-guardian 0.2.0 → 0.3.0
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/CHANGELOG.md +10 -1
- package/README.md +2 -3
- package/package.json +5 -6
- package/src/command-parser.ts +178 -0
- package/src/deletion-guard.ts +433 -0
- package/src/index.ts +70 -0
- package/dist/command-parser.d.ts +0 -43
- package/dist/command-parser.js +0 -109
- package/dist/deletion-guard.d.ts +0 -16
- package/dist/deletion-guard.js +0 -414
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -56
package/dist/index.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { tmpdir } from "node:os";
|
|
2
|
-
import { parseShellCommand } from "./command-parser.js";
|
|
3
|
-
import { assessDeletionCommand } from "./deletion-guard.js";
|
|
4
|
-
const EXPLICIT_DELETION_PATTERN = /\b(?:rm|rmdir|unlink|del|erase|Remove-Item|mkfs(?:\.[A-Za-z0-9_-]+)?|wipefs|diskpart|DROP\s+(?:DATABASE|TABLE)|TRUNCATE)\b|\bdd\b[^\r\n]*\bof=(?:\/dev\/|\\\\\.\\PhysicalDrive)/i;
|
|
5
|
-
const MANUAL_REVIEW_GUIDANCE = "请审阅操作和目标后,自行决定是否手动删除。";
|
|
6
|
-
const STATIC_TARGET_GUIDANCE = "请先确定所有实际删除目标,再将命令简化为语法完整、直接包含静态目标的 Bash 删除命令后重新提交。";
|
|
7
|
-
function invalidInputReason(code) {
|
|
8
|
-
return `Command Guardian 阻止了本次操作,操作尚未执行:${code}`;
|
|
9
|
-
}
|
|
10
|
-
/** 创建只审查显式破坏性 Bash 命令的 Pi Extension。 */
|
|
11
|
-
export function createCommandGuardianExtension(dependencies = {}) {
|
|
12
|
-
const parseCommand = dependencies.parseCommand ?? parseShellCommand;
|
|
13
|
-
const tempDirectory = dependencies.tempDirectory ?? tmpdir();
|
|
14
|
-
return (pi) => {
|
|
15
|
-
pi.on("tool_call", async (event, ctx) => {
|
|
16
|
-
if (event.toolName !== "bash")
|
|
17
|
-
return undefined;
|
|
18
|
-
const input = event.input;
|
|
19
|
-
if (typeof input.command !== "string" || input.command.trim().length === 0) {
|
|
20
|
-
return { block: true, reason: invalidInputReason("PCG_INVALID_BASH_INPUT") };
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
const parsed = await parseCommand(input.command);
|
|
24
|
-
if (parsed.status !== "parsed") {
|
|
25
|
-
return EXPLICIT_DELETION_PATTERN.test(input.command)
|
|
26
|
-
? {
|
|
27
|
-
block: true,
|
|
28
|
-
reason: `Command Guardian 阻止了本次删除操作,操作尚未执行:命令结构无法可靠确定。${STATIC_TARGET_GUIDANCE}`,
|
|
29
|
-
}
|
|
30
|
-
: undefined;
|
|
31
|
-
}
|
|
32
|
-
const result = await assessDeletionCommand(parsed, ctx.cwd, tempDirectory);
|
|
33
|
-
if (result.decision === "allow")
|
|
34
|
-
return undefined;
|
|
35
|
-
if (result.decision === "forbidden") {
|
|
36
|
-
return {
|
|
37
|
-
block: true,
|
|
38
|
-
reason: `Command Guardian 永久阻止了本次操作,操作尚未执行:${result.reason}。该判断无法由模型重试覆盖。`,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
const guidance = result.guidance === "static-target"
|
|
42
|
-
? STATIC_TARGET_GUIDANCE
|
|
43
|
-
: MANUAL_REVIEW_GUIDANCE;
|
|
44
|
-
return {
|
|
45
|
-
block: true,
|
|
46
|
-
reason: `Command Guardian 阻止了本次删除操作,操作尚未执行:${result.reason}。${guidance}`,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
catch {
|
|
50
|
-
return { block: true, reason: invalidInputReason("PCG_INTERNAL_ERROR") };
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
/** Pi package 加载入口。 */
|
|
56
|
-
export default createCommandGuardianExtension();
|