@uncarved/jade 0.2.1 → 0.3.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.md +65 -2
- package/assets/logo.svg +1 -0
- package/bin/jade.js +4 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +26 -4
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +4 -2
- package/plugins/approval/index.ts +236 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +36 -17
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +8 -3
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +33 -6
- package/plugins/fs/grep.ts +19 -7
- package/plugins/fs/index.ts +6 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +1 -1
- package/plugins/hooks/index.ts +152 -0
- package/plugins/hooks/plugin.json +14 -0
- package/plugins/keys/index.ts +2 -18
- package/plugins/mcp/index.ts +43 -15
- package/plugins/mcp/install.ts +139 -0
- package/plugins/mcp/mcp.ts +2 -1
- package/plugins/mcp/plugin.json +1 -1
- package/plugins/mcp/project.ts +37 -1
- package/plugins/memory/index.ts +4 -9
- package/plugins/memory/plugin.json +1 -1
- package/plugins/mentions/index.ts +2 -2
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +39 -45
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +34 -5
- package/plugins/models/saved.ts +8 -4
- package/plugins/modes/index.ts +2 -17
- package/plugins/plugins/index.ts +2 -2
- package/plugins/print/index.ts +36 -6
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +22 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +5 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +32 -2
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/browser.ts +221 -0
- package/plugins/screenshot/index.ts +29 -15
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +115 -7
- package/plugins/shell/plugin.json +3 -1
- package/plugins/shell/renderers.ts +1 -1
- package/plugins/shell/service.ts +2 -13
- package/plugins/shell/shell.ts +119 -47
- package/plugins/skills/index.ts +172 -37
- package/plugins/skills/install.ts +212 -0
- package/plugins/skills/plugin.json +3 -2
- package/plugins/skills/project.ts +38 -1
- package/plugins/skills/skills.ts +28 -9
- package/plugins/steer/index.ts +142 -0
- package/plugins/steer/plugin.json +8 -0
- package/plugins/suggest/index.ts +16 -11
- package/plugins/suggest/plugin.json +1 -1
- package/plugins/task/index.ts +18 -57
- package/plugins/task/plugin.json +9 -3
- package/plugins/task/subagent.ts +25 -7
- package/plugins/todo/index.ts +2 -2
- package/plugins/tools/index.ts +50 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +129 -23
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +506 -0
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +17 -2
- package/plugins/tui/tui.ts +344 -97
- package/plugins/undo/checkpoint.ts +2 -2
- package/plugins/undo/index.ts +84 -26
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +8 -11
- package/plugins/web/plugin.json +1 -1
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +221 -51
- package/plugins-dist/approval/plugin.json +1 -1
- package/plugins-dist/approval/policy.js +30 -15
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +6 -1
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +34 -6
- package/plugins-dist/fs/grep.js +17 -6
- package/plugins-dist/fs/index.js +6 -6
- package/plugins-dist/fs/plugin.json +1 -1
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +45 -13
- package/plugins-dist/mcp/install.js +160 -0
- package/plugins-dist/mcp/mcp.js +2 -1
- package/plugins-dist/mcp/plugin.json +2 -1
- package/plugins-dist/mcp/project.js +42 -1
- package/plugins-dist/memory/index.js +3 -5
- package/plugins-dist/memory/plugin.json +1 -1
- package/plugins-dist/mentions/index.js +1 -1
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +41 -17
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +17 -3
- package/plugins-dist/models/saved.js +9 -4
- package/plugins-dist/modes/index.js +1 -1
- package/plugins-dist/plugins/index.js +1 -1
- package/plugins-dist/print/index.js +37 -3
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +22 -3
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +5 -4
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +28 -2
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/browser.js +187 -0
- package/plugins-dist/screenshot/index.js +28 -13
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/screenshot/tool.js +27 -49
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +119 -4
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +106 -36
- package/plugins-dist/skills/index.js +184 -35
- package/plugins-dist/skills/install.js +178 -0
- package/plugins-dist/skills/plugin.json +6 -2
- package/plugins-dist/skills/project.js +43 -1
- package/plugins-dist/skills/skills.js +27 -10
- package/plugins-dist/steer/index.js +135 -0
- package/plugins-dist/steer/plugin.json +12 -0
- package/plugins-dist/suggest/index.js +15 -9
- package/plugins-dist/suggest/plugin.json +1 -1
- package/plugins-dist/task/index.js +17 -46
- package/plugins-dist/task/plugin.json +11 -5
- package/plugins-dist/task/subagent.js +21 -6
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tools/index.js +49 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +134 -15
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +441 -0
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +16 -1
- package/plugins-dist/tui/tui.js +320 -88
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +1 -1
- package/plugins-dist/undo/index.js +91 -24
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +7 -7
- package/plugins-dist/web/plugin.json +1 -1
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +23 -5
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
|
@@ -1,50 +1,32 @@
|
|
|
1
|
-
// jade/approval:审批。tool.before 把路径统一成绝对路径,tool.gate
|
|
1
|
+
// jade/approval:审批。tool.before 把路径统一成绝对路径,tool.gate 按模式和规则决定:允许、拒绝,或者要问用户(ask)。
|
|
2
|
+
// 关卡不等用户:所有关卡合并的结果是 ask 时(日志里的 tool.decided),内核停下来等人工审批,这里才经 ui@1 问用户,
|
|
3
|
+
// 把回答作为 approval 输入交给会话。别的插件的关卡要问时也由这里问。谁回答的、选了什么记成 jade/approval/answered 事件。
|
|
2
4
|
// auto 模式下规则判断不了、本来要问的调用先让模型审一次(review.ts):它只能放行或转给用户,不能替用户拒绝。
|
|
3
5
|
//
|
|
4
6
|
// 没有这个插件(停用、坏掉、被隔离)时,核心的兜底关卡拒绝所有非只读工具(第 7 节),所以这里出错不会放行。
|
|
5
|
-
// 用户选的"总是允许"记进日志(derived
|
|
7
|
+
// 用户选的"总是允许"记进日志(derived),恢复会话后仍然有效;选"在这个目录里总是允许"的记在
|
|
8
|
+
// $JADE_HOME/state/approvals.json(按工作目录),以后在这个目录里开的会话都有效。模式是进程级的,来自配置,可以经 approval@1 改。
|
|
6
9
|
|
|
10
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
7
11
|
import path from "node:path";
|
|
8
|
-
import type { GatedToolCall, Message } from "uncarved";
|
|
9
|
-
import { definePlugin, type Caller, type SelectRequest, type UI } from "
|
|
10
|
-
import type { Models } from "
|
|
11
|
-
import type { Renderers } from "../renderers/index.ts";
|
|
12
|
+
import type { EventDataMap, GatedToolCall, Message } from "uncarved";
|
|
13
|
+
import { definePlugin, type Caller, type SelectRequest, type UI } from "@uncarved/jade-api";
|
|
14
|
+
import type { Answer, Approval, Approver, Models, Renderers } from "@uncarved/jade-api/services";
|
|
12
15
|
import { describeRule, MODES, policy, type AllowRule, type Mode } from "./policy.ts";
|
|
13
16
|
import { reviewer, type Review } from "./review.ts";
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
setMode(mode: Mode): void;
|
|
19
|
-
/** 读这个目录(绝对路径)里的文件不用问,例如 skill 的目录。返回撤销函数,调用方的作用域关闭时自动撤销。项目层插件不能用 */
|
|
20
|
-
allowRead(dir: string): () => void;
|
|
21
|
-
/** 这个目录(绝对路径)和工作目录同样对待:auto 模式下在里面改文件不用问,例如会话的临时目录。返回撤销函数,调用方的作用域关闭时自动撤销。项目层插件不能用 */
|
|
22
|
-
allowWrite(dir: string): () => void;
|
|
23
|
-
/**
|
|
24
|
-
* 登记一个审批渠道(例如聊天机器人):要问用户时,问题同时交给终端和所有渠道,先回答的算数,
|
|
25
|
-
* 其余的经 signal 收到中止。渠道返回 undefined、不认识的值或出错,算它不回答,不算拒绝;
|
|
26
|
-
* 终端里取消照旧算拒绝。有渠道时,不能提问的前端(-p)也会问。返回撤销函数,调用方的作用域关闭时自动撤销。
|
|
27
|
-
* 项目层插件不能用
|
|
28
|
-
*/
|
|
29
|
-
addApprover(approver: Approver): () => void;
|
|
30
|
-
/**
|
|
31
|
-
* 这个只读调用(例如子 Agent 的)在会话 session 里能不能不问就执行:按该会话"总是允许"的规则、
|
|
32
|
-
* allowRead/allowWrite 登记的目录和 yolo 模式判断。会话不存在或不是只读调用时为 false
|
|
33
|
-
*/
|
|
34
|
-
allows(session: string, call: GatedToolCall): boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** 审批的回答:允许一次、本会话里总是允许、拒绝 */
|
|
38
|
-
export type Answer = "once" | "always" | "deny";
|
|
39
|
-
export type Approver = (request: SelectRequest<Answer>, signal: AbortSignal) => Promise<Answer | undefined>;
|
|
40
|
-
const ANSWERS: readonly string[] = ["once", "always", "deny"] satisfies Answer[];
|
|
18
|
+
const ANSWERS: readonly string[] = ["once", "always", "forever", "deny"] satisfies Answer[];
|
|
19
|
+
/** 只有终端里有的一项:拒绝,并写一句话告诉模型该怎么做 */
|
|
20
|
+
const EXPLAIN = "explain";
|
|
41
21
|
|
|
42
22
|
/** derived 里保存"总是允许"规则的键 */
|
|
43
23
|
const RULES_KEY = "jade/approval/rules";
|
|
44
|
-
/**
|
|
24
|
+
/** 等用户回答的默认时限,过了按拒绝处理。配置 askTimeoutMinutes 可以改 */
|
|
45
25
|
const ASK_TIMEOUT_MS = 24 * 60 * 60 * 1000;
|
|
46
|
-
/** askTimeoutMinutes
|
|
26
|
+
/** askTimeoutMinutes 的上限:定时器最长约 24.8 天 */
|
|
47
27
|
const MAX_ASK_TIMEOUT_MINUTES = 7 * 24 * 60;
|
|
28
|
+
/** 关卡的时限:auto 模式的审查最长一分钟(review.ts),比 hook 默认的 30 秒长 */
|
|
29
|
+
const GATE_TIMEOUT_MS = 2 * 60_000;
|
|
48
30
|
const MAX_DETAIL_CHARS = 2000;
|
|
49
31
|
/** 等工具的审批预览的时限,过了就用默认的 detail */
|
|
50
32
|
const PREVIEW_TIMEOUT_MS = 5000;
|
|
@@ -69,8 +51,8 @@ let badTimeout: string | undefined;
|
|
|
69
51
|
const readRoots: string[] = [];
|
|
70
52
|
/** 经 allowWrite 登记的目录;policy 每次判断时读它 */
|
|
71
53
|
const writeRoots: string[] = [];
|
|
72
|
-
/** 经 addApprover 登记的审批渠道 */
|
|
73
|
-
const approvers = new
|
|
54
|
+
/** 经 addApprover 登记的审批渠道 → 登记它的插件 */
|
|
55
|
+
const approvers = new Map<Approver, string>();
|
|
74
56
|
/** 打开着的会话:会话 id → 判断只读调用能不能不问就执行(allows) */
|
|
75
57
|
const sessionChecks = new Map<string, (call: GatedToolCall) => boolean>();
|
|
76
58
|
|
|
@@ -95,12 +77,12 @@ export default definePlugin({
|
|
|
95
77
|
addApprover(approver: Approver) {
|
|
96
78
|
if (caller.layer === "project") throw new Error("approval.addApprover: project plugins cannot answer approvals");
|
|
97
79
|
if (typeof approver !== "function") throw new Error("approval.addApprover: approver must be a function");
|
|
98
|
-
approvers.
|
|
80
|
+
approvers.set(approver, caller.plugin);
|
|
99
81
|
const remove = () => void approvers.delete(approver);
|
|
100
82
|
caller.onDispose(remove);
|
|
101
83
|
return remove;
|
|
102
84
|
},
|
|
103
|
-
allows: (session: string, call: GatedToolCall) => call.tool
|
|
85
|
+
allows: (session: string, call: GatedToolCall) => call.tool !== undefined && (sessionChecks.get(session)?.(call) ?? false),
|
|
104
86
|
}));
|
|
105
87
|
},
|
|
106
88
|
|
|
@@ -135,15 +117,31 @@ export default definePlugin({
|
|
|
135
117
|
/** 本会话里审查过一次、放行了的调用 */
|
|
136
118
|
const reviewedCalls = new Set<string>();
|
|
137
119
|
let warned = false;
|
|
138
|
-
/** 本进程里刚选的"总是允许"
|
|
120
|
+
/** 本进程里刚选的"总是允许":记进日志之前(要等看到那条 approval 输入)就要生效 */
|
|
139
121
|
const added: AllowRule[] = [];
|
|
140
122
|
/** 一次只问一个问题;排在后面的问之前重新判断,前面刚选的"总是允许"可能已经覆盖它 */
|
|
141
123
|
let asking: Promise<unknown> = Promise.resolve();
|
|
142
124
|
/** 日志里记下的"总是允许"规则,每次调用工具前取一次:子 Agent 在工具执行期间经 allows 用它 */
|
|
143
125
|
let logged: AllowRule[] = [];
|
|
126
|
+
const merged = (from: readonly AllowRule[]) => [...from, ...added.filter((r) => !from.some((l) => JSON.stringify(l) === JSON.stringify(r)))];
|
|
127
|
+
/** "在这个目录里总是允许"的规则:会话打开时从文件读,本会话里新选的也加进来 */
|
|
128
|
+
const foreverFile = path.join(ctx.home, "state", "approvals.json");
|
|
129
|
+
const forever: AllowRule[] = (await readForever(foreverFile))[ctx.cwd] ?? [];
|
|
130
|
+
const saved = () => [...forever, ...merged(logged)];
|
|
131
|
+
/** 这一轮里关卡看过、还没有决定的调用 */
|
|
132
|
+
const seen = new Map<string, Asked>();
|
|
133
|
+
/** 已经交给会话的回答:在日志里看到那条 approval 输入时记一条 answered 事件 */
|
|
134
|
+
const answered = new Map<string, { answer: string; by: string }>();
|
|
135
|
+
/** 这一轮里已经在问、或者问过的调用 */
|
|
136
|
+
const handled = new Set<string>();
|
|
137
|
+
/** 这一轮结束(包括被中断)时中止还开着的问题 */
|
|
138
|
+
let turn = new AbortController();
|
|
139
|
+
const canAsk = () => ui?.interactive === true || approvers.size > 0;
|
|
140
|
+
const nobody = (name: string) => `${name} needs the user's approval, but nobody can approve it in this non-interactive run. Do not retry it; tell the user what you wanted to do.`;
|
|
141
|
+
|
|
144
142
|
const id = ctx.session.id;
|
|
145
143
|
const check = (call: GatedToolCall) =>
|
|
146
|
-
rules.decide(call, { mode: mode === "yolo" ? "yolo" : "readonly", rules: [...logged, ...added] }).decision === "allow";
|
|
144
|
+
rules.decide(call, { mode: mode === "yolo" && call.tool.effect === "read" ? "yolo" : "readonly", rules: [...forever, ...logged, ...added] }).decision === "allow";
|
|
147
145
|
sessionChecks.set(id, check);
|
|
148
146
|
await ctx.effect(() => () => {
|
|
149
147
|
if (sessionChecks.get(id) === check) sessionChecks.delete(id);
|
|
@@ -160,12 +158,9 @@ export default definePlugin({
|
|
|
160
158
|
ctx.kernel.hook({
|
|
161
159
|
kind: "gate",
|
|
162
160
|
point: "tool.gate",
|
|
163
|
-
timeoutMs:
|
|
161
|
+
timeoutMs: GATE_TIMEOUT_MS,
|
|
164
162
|
fn: async (call, hook) => {
|
|
165
|
-
|
|
166
|
-
const logged = (hook.derived[RULES_KEY] as AllowRule[] | undefined) ?? [];
|
|
167
|
-
return [...logged, ...added.filter((r) => !logged.some((l) => JSON.stringify(l) === JSON.stringify(r)))];
|
|
168
|
-
};
|
|
163
|
+
seen.set(call.toolUseId, { call });
|
|
169
164
|
const result = rules.decide(call, { mode, rules: saved() });
|
|
170
165
|
if (result.decision !== "ask") return result;
|
|
171
166
|
// auto 模式:规则判断不了、本来要问的调用先让模型审一次。审查只能放行或转给用户,不会替用户拒绝
|
|
@@ -182,7 +177,7 @@ export default definePlugin({
|
|
|
182
177
|
}
|
|
183
178
|
why = verdict.reason;
|
|
184
179
|
}
|
|
185
|
-
if (!
|
|
180
|
+
if (!canAsk()) {
|
|
186
181
|
if (!warned) {
|
|
187
182
|
warned = true;
|
|
188
183
|
ui?.notify(
|
|
@@ -191,42 +186,164 @@ export default definePlugin({
|
|
|
191
186
|
"warning",
|
|
192
187
|
);
|
|
193
188
|
}
|
|
194
|
-
return { decision: "deny", reason:
|
|
189
|
+
return { decision: "deny", reason: nobody(call.name) };
|
|
190
|
+
}
|
|
191
|
+
seen.set(call.toolUseId, { call, mine: call, ...(why ? { why } : {}) });
|
|
192
|
+
return { decision: "ask" };
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/** 问用户(或按刚加的规则直接决定),返回交给内核的回答 */
|
|
197
|
+
const decide = async (asked: Asked, signal: AbortSignal): Promise<Verdict> => {
|
|
198
|
+
const { call } = asked;
|
|
199
|
+
if (asked.mine) {
|
|
200
|
+
const again = rules.decide(asked.mine, { mode, rules: saved() });
|
|
201
|
+
if (again.decision === "allow") return { approved: true, answer: "allow", by: "rule" };
|
|
202
|
+
if (again.decision === "deny") return { approved: false, answer: "deny", by: "rule", reason: again.reason };
|
|
203
|
+
}
|
|
204
|
+
if (!canAsk()) return { approved: false, answer: "deny", by: "nobody", reason: nobody(call.name) };
|
|
205
|
+
const allow = rules.rulesFor(call);
|
|
206
|
+
const described = allow.map(describeRule).join(", ");
|
|
207
|
+
// "总是允许"的范围只在正文里写一次:写进选项里时,长命令会把两个选项句尾的区别(本会话 / 这个目录)挤出屏幕
|
|
208
|
+
const covers = asked.mine ? `"Always allow" covers: ${allow.length === 1 && allow[0]!.command !== undefined ? `${allow[0]!.tool}, this exact command only` : described}\n\n` : "";
|
|
209
|
+
const shown = covers + ((await preview(renderers, call)) ?? detail(call));
|
|
210
|
+
const expired = new AbortController();
|
|
211
|
+
const timer = setTimeout(() => expired.abort(), askTimeoutMs);
|
|
212
|
+
let got: { answer: Answer; by: string; said?: string } | undefined;
|
|
213
|
+
try {
|
|
214
|
+
got = await ask(
|
|
215
|
+
ui,
|
|
216
|
+
{
|
|
217
|
+
title: `Allow ${call.name}?`,
|
|
218
|
+
// 审查转给用户的:先说为什么
|
|
219
|
+
detail: asked.why ? `${asked.why}\n\n${shown}` : shown,
|
|
220
|
+
options: [
|
|
221
|
+
{ label: "Allow once", value: "once" },
|
|
222
|
+
// 别的插件要问的调用不能"总是允许":这里的规则管不到它的关卡
|
|
223
|
+
...(asked.mine
|
|
224
|
+
? [
|
|
225
|
+
{ label: "Always allow in this session", value: "always" as const },
|
|
226
|
+
{ label: "Always allow in this directory, in every session", value: "forever" as const },
|
|
227
|
+
]
|
|
228
|
+
: []),
|
|
229
|
+
{ label: "Deny", value: "deny" },
|
|
230
|
+
],
|
|
231
|
+
},
|
|
232
|
+
AbortSignal.any([signal, expired.signal, ctx.signal]),
|
|
233
|
+
);
|
|
234
|
+
} finally {
|
|
235
|
+
clearTimeout(timer);
|
|
236
|
+
}
|
|
237
|
+
if (expired.signal.aborted) {
|
|
238
|
+
return { approved: false, answer: "deny", by: "timeout", reason: `Nobody answered the approval request for ${call.name} in time. Do not retry it; tell the user what you wanted to do.` };
|
|
239
|
+
}
|
|
240
|
+
if (got?.answer === "always" && asked.mine) {
|
|
241
|
+
added.push(...allow);
|
|
242
|
+
return { approved: true, answer: "always", by: got.by };
|
|
243
|
+
}
|
|
244
|
+
if (got?.answer === "forever" && asked.mine) {
|
|
245
|
+
forever.push(...allow);
|
|
246
|
+
try {
|
|
247
|
+
await addForever(foreverFile, ctx.cwd, allow);
|
|
248
|
+
ui?.notify(`${described} is now always allowed in ${ctx.cwd}. To ask again, remove it from ${foreverFile}`, "info");
|
|
249
|
+
} catch (e) {
|
|
250
|
+
ui?.notify(`could not save the rule to ${foreverFile} (${e instanceof Error ? e.message : String(e)}); it only applies to this session`, "warning");
|
|
251
|
+
}
|
|
252
|
+
return { approved: true, answer: "forever", by: got.by };
|
|
253
|
+
}
|
|
254
|
+
if (got !== undefined && got.answer !== "deny") return { approved: true, answer: "once", by: got.by };
|
|
255
|
+
if (got?.said) return { approved: false, answer: "deny", by: got.by, reason: `The user denied this tool call and said: ${got.said}` };
|
|
256
|
+
return { approved: false, answer: "deny", by: got?.by ?? "nobody", reason: "The user denied this tool call. Do not retry it unchanged: adjust, or ask what to do instead." };
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/** 一个要问的调用:得出回答,交给会话。出错也要回答(拒绝),否则这一轮一直等着 */
|
|
260
|
+
const settle = async (asked: Asked, signal: AbortSignal) => {
|
|
261
|
+
const toolUseId = asked.call.toolUseId;
|
|
262
|
+
let verdict: Verdict;
|
|
263
|
+
try {
|
|
264
|
+
verdict = await decide(asked, signal);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
verdict = { approved: false, answer: "deny", by: "error", reason: `Asking for approval failed: ${e instanceof Error ? e.message : String(e)}` };
|
|
267
|
+
}
|
|
268
|
+
// 这一轮已经结束(被中断):回答作废
|
|
269
|
+
if (signal.aborted) return;
|
|
270
|
+
answered.set(toolUseId, { answer: verdict.answer, by: verdict.by });
|
|
271
|
+
// 不等它:它要到内核再次停下才 resolve,后面排着的问题可能就是内核在等的
|
|
272
|
+
void Promise.resolve()
|
|
273
|
+
.then(() => ctx.session.send({ kind: "approval", toolUseId, approved: verdict.approved, ...(verdict.reason ? { reason: verdict.reason } : {}) }))
|
|
274
|
+
.catch(() => void answered.delete(toolUseId));
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
ctx.kernel.hook({
|
|
278
|
+
kind: "observe",
|
|
279
|
+
point: "event",
|
|
280
|
+
fn: async (e, hook) => {
|
|
281
|
+
if (e.type === "tool.decided") {
|
|
282
|
+
const d = e.data as EventDataMap["tool.decided"];
|
|
283
|
+
const asked = seen.get(d.toolUseId);
|
|
284
|
+
seen.delete(d.toolUseId);
|
|
285
|
+
if (d.decision !== "ask" || !asked) return;
|
|
286
|
+
// 不在这里等回答:observe 要马上返回,内核才能停下来等
|
|
287
|
+
handled.add(d.toolUseId);
|
|
288
|
+
const signal = turn.signal;
|
|
289
|
+
asking = asking.then(() => settle(asked, signal));
|
|
290
|
+
} else if (e.type === "input.received") {
|
|
291
|
+
const d = e.data as EventDataMap["input.received"];
|
|
292
|
+
if (d.kind !== "approval") return;
|
|
293
|
+
const a = answered.get(d.toolUseId);
|
|
294
|
+
if (!a) return;
|
|
295
|
+
answered.delete(d.toolUseId);
|
|
296
|
+
// 内核只记同意或拒绝:选了什么、谁回答的(终端、哪个渠道、规则、超时)记在这里
|
|
297
|
+
await hook.emit("answered", { toolUseId: d.toolUseId, ...a });
|
|
298
|
+
if (a.answer === "always") await hook.record(RULES_KEY, merged((hook.derived[RULES_KEY] as AllowRule[] | undefined) ?? []));
|
|
299
|
+
} else if (e.type === "turn.ended") {
|
|
300
|
+
turn.abort(new Error("the turn ended"));
|
|
301
|
+
turn = new AbortController();
|
|
302
|
+
seen.clear();
|
|
303
|
+
answered.clear();
|
|
304
|
+
handled.clear();
|
|
195
305
|
}
|
|
196
|
-
const turn = asking.then(async () => {
|
|
197
|
-
const again = rules.decide(call, { mode, rules: saved() });
|
|
198
|
-
if (again.decision !== "ask") return again;
|
|
199
|
-
const rule = rules.ruleFor(call);
|
|
200
|
-
const shown = (await preview(renderers, call)) ?? detail(call);
|
|
201
|
-
const answer = await ask(
|
|
202
|
-
ui,
|
|
203
|
-
{
|
|
204
|
-
title: `Allow ${call.name}?`,
|
|
205
|
-
// 审查转给用户的:先说为什么
|
|
206
|
-
detail: why ? `${why}\n\n${shown}` : shown,
|
|
207
|
-
options: [
|
|
208
|
-
{ label: "Allow once", value: "once" },
|
|
209
|
-
{ label: `Always allow ${describeRule(rule)} in this session`, value: "always" },
|
|
210
|
-
{ label: "Deny", value: "deny" },
|
|
211
|
-
],
|
|
212
|
-
},
|
|
213
|
-
hook.signal,
|
|
214
|
-
);
|
|
215
|
-
if (answer === "always") {
|
|
216
|
-
added.push(rule);
|
|
217
|
-
await hook.record(RULES_KEY, saved());
|
|
218
|
-
return { decision: "allow" as const };
|
|
219
|
-
}
|
|
220
|
-
if (answer === "once") return { decision: "allow" as const };
|
|
221
|
-
return { decision: "deny" as const, reason: "The user denied this tool call. Do not retry it unchanged: adjust, or ask what to do instead." };
|
|
222
|
-
});
|
|
223
|
-
asking = turn.catch(() => {});
|
|
224
|
-
return turn;
|
|
225
306
|
},
|
|
226
307
|
});
|
|
308
|
+
|
|
309
|
+
// 恢复一个上次停在等审批的会话(进程被杀、崩溃):那些调用的关卡是上一个进程跑的,这里没见过。
|
|
310
|
+
// 内核恢复时再发一次 awaiting,据此重新问。只知道调用的名字和参数(工具的声明只有关卡拿得到),
|
|
311
|
+
// 所以当作别的关卡要问的:只有"允许一次"和"拒绝",不重新套规则
|
|
312
|
+
ctx.session.subscribe((n) => {
|
|
313
|
+
if (n.type !== "awaiting") return;
|
|
314
|
+
const waiting = n.toolUseIds.filter((t) => !handled.has(t) && !seen.has(t));
|
|
315
|
+
if (waiting.length === 0) return;
|
|
316
|
+
const batch = ctx.session.state().batch;
|
|
317
|
+
const signal = turn.signal;
|
|
318
|
+
for (const toolUseId of waiting) {
|
|
319
|
+
const c = batch.find((x) => x.toolUseId === toolUseId && x.decision === "ask");
|
|
320
|
+
if (!c) continue;
|
|
321
|
+
handled.add(toolUseId);
|
|
322
|
+
asking = asking.then(() => settle({ call: { toolUseId, name: c.name, input: c.input } }, signal));
|
|
323
|
+
}
|
|
324
|
+
});
|
|
227
325
|
},
|
|
228
326
|
});
|
|
229
327
|
|
|
328
|
+
/** 一个要问的调用。没有 mine 的是别的插件的关卡要问的,或者恢复会话时还在等的;why:auto 模式的审查转给用户时的理由 */
|
|
329
|
+
interface Asked {
|
|
330
|
+
call: AskedCall;
|
|
331
|
+
/** 这里的规则要问的:关卡看到的完整调用,问之前按它重新判断一次 */
|
|
332
|
+
mine?: GatedToolCall;
|
|
333
|
+
why?: string;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** 要问的调用里给用户看的部分;恢复的会话里只有这些 */
|
|
337
|
+
type AskedCall = Pick<GatedToolCall, "toolUseId" | "name" | "input">;
|
|
338
|
+
|
|
339
|
+
/** 交给内核的回答,和记进 answered 事件的部分(answer、by) */
|
|
340
|
+
interface Verdict {
|
|
341
|
+
approved: boolean;
|
|
342
|
+
answer: "allow" | Answer;
|
|
343
|
+
by: string;
|
|
344
|
+
reason?: string;
|
|
345
|
+
}
|
|
346
|
+
|
|
230
347
|
/** allowRead、allowWrite:把目录登记进 roots,调用方的作用域关闭时撤销 */
|
|
231
348
|
function allow(caller: Caller, name: string, roots: string[], dir: string): () => void {
|
|
232
349
|
if (caller.layer === "project") throw new Error(`approval.${name}: project plugins cannot widen what runs without asking`);
|
|
@@ -242,23 +359,33 @@ function allow(caller: Caller, name: string, roots: string[], dir: string): () =
|
|
|
242
359
|
}
|
|
243
360
|
|
|
244
361
|
/**
|
|
245
|
-
*
|
|
362
|
+
* 把问题同时交给终端(能提问时)和所有审批渠道,返回先到的回答和回答的人("terminal" 或渠道的插件 id),然后中止其余的。
|
|
246
363
|
* 终端里取消(undefined)算拒绝;渠道返回 undefined、不认识的值或出错算它不回答。都不回答时返回 undefined(拒绝)
|
|
247
364
|
*/
|
|
248
|
-
async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: AbortSignal): Promise<Answer | undefined> {
|
|
365
|
+
async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: AbortSignal): Promise<{ answer: Answer; by: string; said?: string } | undefined> {
|
|
249
366
|
const done = new AbortController();
|
|
250
367
|
const stop = () => done.abort(signal.reason);
|
|
251
368
|
signal.addEventListener("abort", stop, { once: true });
|
|
252
369
|
try {
|
|
253
|
-
return await new Promise<Answer | undefined>((resolve, reject) => {
|
|
254
|
-
const channels: Promise<Answer
|
|
255
|
-
if (ui?.interactive)
|
|
256
|
-
|
|
370
|
+
return await new Promise<{ answer: Answer; by: string; said?: string } | undefined>((resolve, reject) => {
|
|
371
|
+
const channels: Promise<{ answer: Answer; by: string; said?: string } | typeof NONE>[] = [];
|
|
372
|
+
if (ui?.interactive) {
|
|
373
|
+
// 终端里多一项:拒绝并说明。选了它再问一句话,取消或什么都没写就是普通的拒绝
|
|
374
|
+
const options = [...request.options, { label: "Deny, and say what to do instead", value: EXPLAIN }];
|
|
375
|
+
channels.push(
|
|
376
|
+
ui.select<string>({ ...request, options }, done.signal).then(async (a) => {
|
|
377
|
+
if (a !== EXPLAIN) return { answer: a !== undefined && ANSWERS.includes(a) ? (a as Answer) : "deny", by: "terminal" };
|
|
378
|
+
const said = (await ui.input({ title: "What should the agent do instead?" }, done.signal))?.trim();
|
|
379
|
+
return { answer: "deny" as const, by: "terminal", ...(said ? { said } : {}) };
|
|
380
|
+
}),
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
for (const [approver, by] of approvers) {
|
|
257
384
|
channels.push(
|
|
258
385
|
Promise.resolve()
|
|
259
386
|
.then(() => approver(request, done.signal))
|
|
260
387
|
.then(
|
|
261
|
-
(a) => (typeof a === "string" && ANSWERS.includes(a) ?
|
|
388
|
+
(a) => (typeof a === "string" && ANSWERS.includes(a) ? { answer: a as Answer, by } : NONE),
|
|
262
389
|
() => NONE,
|
|
263
390
|
),
|
|
264
391
|
);
|
|
@@ -279,11 +406,35 @@ async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: A
|
|
|
279
406
|
}
|
|
280
407
|
const NONE = Symbol("no answer");
|
|
281
408
|
|
|
409
|
+
/** approvals.json:工作目录 → 在这个目录里总是允许的规则。读不了或格式不对时当作没有 */
|
|
410
|
+
async function readForever(file: string): Promise<Record<string, AllowRule[]>> {
|
|
411
|
+
try {
|
|
412
|
+
const data = JSON.parse(await readFile(file, "utf8")) as unknown;
|
|
413
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) return {};
|
|
414
|
+
const out: Record<string, AllowRule[]> = {};
|
|
415
|
+
for (const [dir, rules] of Object.entries(data)) {
|
|
416
|
+
if (Array.isArray(rules)) out[dir] = rules.filter((r): r is AllowRule => !!r && typeof r === "object" && typeof (r as AllowRule).tool === "string");
|
|
417
|
+
}
|
|
418
|
+
return out;
|
|
419
|
+
} catch {
|
|
420
|
+
return {};
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async function addForever(file: string, cwd: string, add: AllowRule[]): Promise<void> {
|
|
425
|
+
const all = await readForever(file);
|
|
426
|
+
const rules = all[cwd] ?? [];
|
|
427
|
+
for (const rule of add) if (!rules.some((r) => JSON.stringify(r) === JSON.stringify(rule))) rules.push(rule);
|
|
428
|
+
all[cwd] = rules;
|
|
429
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
430
|
+
await writeFile(file, `${JSON.stringify(all, null, 2)}\n`);
|
|
431
|
+
}
|
|
432
|
+
|
|
282
433
|
/**
|
|
283
434
|
* 工具自己登记的审批预览(例如文件修改的 diff)。没有登记、返回的不是文字、或者出错时返回 undefined,
|
|
284
435
|
* 用下面的 detail:预览是别的插件的代码,它坏了不能让审批卡住或失败
|
|
285
436
|
*/
|
|
286
|
-
async function preview(renderers: Renderers | undefined, call:
|
|
437
|
+
async function preview(renderers: Renderers | undefined, call: AskedCall): Promise<string | undefined> {
|
|
287
438
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
288
439
|
try {
|
|
289
440
|
// 预览可能读文件、算 diff;一直不返回时也要能问用户
|
|
@@ -300,7 +451,7 @@ async function preview(renderers: Renderers | undefined, call: GatedToolCall): P
|
|
|
300
451
|
}
|
|
301
452
|
|
|
302
453
|
/** 给用户看的调用内容:命令、路径和要写的内容,太长的截断 */
|
|
303
|
-
function detail(call:
|
|
454
|
+
function detail(call: AskedCall): string {
|
|
304
455
|
const input = call.input as Record<string, unknown> | null;
|
|
305
456
|
let text: string;
|
|
306
457
|
if (typeof input?.command === "string") text = input.command;
|
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
// 纯判断,不碰界面和日志;那些在 index.ts。
|
|
3
3
|
//
|
|
4
4
|
// 带 url 参数的只读工具(web_fetch)也要问:网页里的提示注入可以让模型把读到的内容拼进 URL 发出去。
|
|
5
|
+
// 指向本机的 URL(localhost 上的开发服务器,截图最常见的用途)不问:数据不会离开这台机器。
|
|
5
6
|
|
|
6
7
|
import { realpathSync, statSync } from "node:fs";
|
|
7
8
|
import path from "node:path";
|
|
8
9
|
import type { GatedToolCall, GateResult } from "uncarved";
|
|
10
|
+
import type { ApprovalMode } from "@uncarved/jade-api/services";
|
|
9
11
|
import { validate } from "./schema.ts";
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* 权限模式。ask:改东西先问;auto:工作目录里的改动和只建目录、碰文件的命令不问;
|
|
13
15
|
* plan:不改任何东西,拒绝时告诉模型先给出计划;readonly:不改任何东西(子 Agent 用);yolo:什么都不问
|
|
14
16
|
*/
|
|
15
|
-
export const MODES = ["ask", "auto", "plan", "readonly", "yolo"] as const;
|
|
17
|
+
export const MODES = ["ask", "auto", "plan", "readonly", "yolo"] as const satisfies readonly ApprovalMode[];
|
|
16
18
|
export type Mode = (typeof MODES)[number];
|
|
17
19
|
|
|
18
20
|
/**
|
|
@@ -52,7 +54,8 @@ export interface PolicyOptions {
|
|
|
52
54
|
|
|
53
55
|
export interface Policy {
|
|
54
56
|
/** 给"总是允许"一个默认规则:bash 取命令的前两个词,工作目录之外的路径取所在目录,URL 取主机,其他取工具名 */
|
|
55
|
-
|
|
57
|
+
/** 用户选"总是允许"时记下的规则;bash 的复合命令可能是好几条 */
|
|
58
|
+
rulesFor(call: { name: string; input: unknown }): AllowRule[];
|
|
56
59
|
decide(call: GatedToolCall, settings: PolicySettings): GateResult;
|
|
57
60
|
/** 工具参数里的相对路径换成绝对路径,日志里记下的、关卡判断的都是确定的路径 */
|
|
58
61
|
absolutePath(input: unknown): unknown;
|
|
@@ -363,19 +366,31 @@ function prefixCovers(words: Word[], prefix: string): boolean {
|
|
|
363
366
|
|
|
364
367
|
/**
|
|
365
368
|
* bash 的"总是允许"默认规则:一般取前两个词("cargo build"、"git commit"),
|
|
366
|
-
* 运行脚本的命令取三个词("npm run build")。第二个词就是选项("rm -rf x"
|
|
367
|
-
*
|
|
369
|
+
* 运行脚本的命令取三个词("npm run build")。第二个词就是选项("rm -rf x")、只有一个词时,只允许这一条命令本身。
|
|
370
|
+
* 复合命令("git add -A && git commit -m …"、"bun test 2>&1 | tail -20")里每条不是只读的命令各记一条:
|
|
371
|
+
* 关卡对复合命令逐条判断,所以下次换了提交信息、换了组合也不用再问。
|
|
372
|
+
* 分不了词、有输出重定向、或者不是只读的命令带输入重定向时,只允许整条命令原文
|
|
368
373
|
*/
|
|
369
|
-
export function
|
|
370
|
-
const exact = { tool: "bash", command: command.trim() };
|
|
371
|
-
// 复合命令里只有一条不是只读命令("bun test 2>&1 | tail -20")时,规则取它;关卡对复合命令逐条判断
|
|
374
|
+
export function bashRules(command: string): AllowRule[] {
|
|
375
|
+
const exact = [{ tool: "bash", command: command.trim() }];
|
|
372
376
|
const cmds = shellCommands(command);
|
|
373
377
|
const unsafe = cmds?.filter((c) => c.words[0]!.value !== "cd" && commandKind(c.words.map((w) => w.value)) !== "read");
|
|
374
|
-
if (!cmds || !unsafe || unsafe.length
|
|
375
|
-
|
|
376
|
-
if (
|
|
378
|
+
if (!cmds || !unsafe || unsafe.length === 0 || !cmds.every(quietOutputs) || unsafe.some((c) => c.inputs.length > 0)) return exact;
|
|
379
|
+
// 只有一条命令、又只能按原文记时,记整条原文(和关卡比较的是同一个字符串)
|
|
380
|
+
if (cmds.length === 1 && !prefixOf(unsafe[0]!.words)) return exact;
|
|
381
|
+
const rules = unsafe.map((c): AllowRule => {
|
|
382
|
+
const prefix = prefixOf(c.words);
|
|
383
|
+
return prefix ? { tool: "bash", prefix } : { tool: "bash", command: joined(c.words) };
|
|
384
|
+
});
|
|
385
|
+
return rules.filter((r, i) => rules.findIndex((o) => o.prefix === r.prefix && o.command === r.command) === i);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** 一条简单命令的前缀规则;只能按原文记时返回 undefined */
|
|
389
|
+
function prefixOf(all: Word[]): string | undefined {
|
|
390
|
+
const words = all.map((w) => w.value);
|
|
391
|
+
if (words.length < 2 || words[1]!.startsWith("-") || words.some((w) => DANGEROUS_ARGS.test(w))) return undefined;
|
|
377
392
|
const n = RUNNERS.test(words.slice(0, 2).join(" ")) && words.length >= 3 && !words[2]!.startsWith("-") ? 3 : 2;
|
|
378
|
-
return
|
|
393
|
+
return words.slice(0, n).join(" ");
|
|
379
394
|
}
|
|
380
395
|
|
|
381
396
|
const hostOf = (url: string) => {
|
|
@@ -386,6 +401,10 @@ const hostOf = (url: string) => {
|
|
|
386
401
|
}
|
|
387
402
|
};
|
|
388
403
|
|
|
404
|
+
/** 本机的地址:localhost、127.x.x.x、::1(URL 会把 127.1 这类写法规范成 127.0.0.1) */
|
|
405
|
+
const isLoopback = (host: string | undefined) =>
|
|
406
|
+
host !== undefined && (host === "localhost" || host.endsWith(".localhost") || /^127(\.\d{1,3}){3}$/.test(host) || host === "[::1]");
|
|
407
|
+
|
|
389
408
|
/**
|
|
390
409
|
* Windows 上 bash 工具跑的是 Git Bash,模型常把路径写成 /c/Users/...(MSYS 的写法)。
|
|
391
410
|
* path.resolve 会把它当成当前盘符下的 \c\Users\...,判断成工作目录之外,只读命令也要确认。
|
|
@@ -495,12 +514,12 @@ export function policy(opts: PolicyOptions): Policy {
|
|
|
495
514
|
if (!input || typeof input !== "object" || typeof (input as { path?: unknown }).path !== "string") return input;
|
|
496
515
|
return { ...input, path: path.resolve(opts.cwd, (input as { path: string }).path) };
|
|
497
516
|
},
|
|
498
|
-
|
|
499
|
-
if (call.name === "bash") return
|
|
517
|
+
rulesFor(call) {
|
|
518
|
+
if (call.name === "bash") return bashRules(String((call.input as { command?: unknown }).command ?? ""));
|
|
500
519
|
const input = call.input as { path?: unknown; url?: unknown } | null;
|
|
501
520
|
if (typeof input?.url === "string") {
|
|
502
521
|
const host = hostOf(input.url);
|
|
503
|
-
if (host) return { tool: call.name, host };
|
|
522
|
+
if (host) return [{ tool: call.name, host }];
|
|
504
523
|
}
|
|
505
524
|
if (typeof input?.path === "string" && !inside(input.path)) {
|
|
506
525
|
const full = path.resolve(opts.cwd, input.path);
|
|
@@ -510,9 +529,9 @@ export function policy(opts: PolicyOptions): Policy {
|
|
|
510
529
|
} catch {
|
|
511
530
|
// 还不存在的文件
|
|
512
531
|
}
|
|
513
|
-
return { tool: call.name, dir: isDir ? full : path.dirname(full) };
|
|
532
|
+
return [{ tool: call.name, dir: isDir ? full : path.dirname(full) }];
|
|
514
533
|
}
|
|
515
|
-
return { tool: call.name };
|
|
534
|
+
return [{ tool: call.name }];
|
|
516
535
|
},
|
|
517
536
|
decide(call, { mode, rules }) {
|
|
518
537
|
const { tool } = call;
|
|
@@ -524,7 +543,7 @@ export function policy(opts: PolicyOptions): Policy {
|
|
|
524
543
|
(typeof input.path === "string" && !inside(input.path)) ||
|
|
525
544
|
(call.name === "glob" && typeof input.pattern === "string" && globEscapes(input.pattern));
|
|
526
545
|
/** 会把参数发到网络上的只读工具 */
|
|
527
|
-
const network = typeof input.url === "string";
|
|
546
|
+
const network = typeof input.url === "string" && !isLoopback(hostOf(input.url));
|
|
528
547
|
const covers = (r: AllowRule) => {
|
|
529
548
|
if (r.tool !== call.name) return false;
|
|
530
549
|
if (r.command !== undefined) return (input.command ?? "").trim() === r.command;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { readFile } from "node:fs/promises";
|
|
9
9
|
import path from "node:path";
|
|
10
10
|
import type { GatedToolCall, GateResult, Message, ModelRequest, Usage } from "uncarved";
|
|
11
|
-
import type { Models } from "
|
|
11
|
+
import type { Models } from "@uncarved/jade-api/services";
|
|
12
12
|
|
|
13
13
|
/** 审查的结论,加上这次审查用掉的 token(计入会话用量) */
|
|
14
14
|
export type Review = GateResult & { usage?: Usage };
|
package/plugins/ask/index.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
// 从旧 jade 的 tools/ask.ts 和 main.ts 的 askUser 移植。
|
|
6
6
|
|
|
7
7
|
import type { Tool } from "uncarved";
|
|
8
|
-
import { definePlugin, type UI } from "
|
|
9
|
-
import type { Renderers, ToolRenderer } from "
|
|
8
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
9
|
+
import type { Renderers, ToolRenderer } from "@uncarved/jade-api/services";
|
|
10
10
|
|
|
11
11
|
export interface Question {
|
|
12
12
|
question: string;
|
package/plugins/budget/index.ts
CHANGED
package/plugins/checks/checks.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { existsSync, realpathSync } from "node:fs";
|
|
|
18
18
|
import { readFile } from "node:fs/promises";
|
|
19
19
|
import path from "node:path";
|
|
20
20
|
import type { Block, HookContext, LocalHookSpec, Message } from "uncarved";
|
|
21
|
-
import type { RunCommand } from "
|
|
21
|
+
import type { RunCommand } from "@uncarved/jade-api/services";
|
|
22
22
|
|
|
23
23
|
/** 老版本把整个 ChecksState 记在这一个 key 下、每次检查整份重记;恢复老会话时还要读它 */
|
|
24
24
|
export const CHECKS_KEY = "jade/checks";
|
|
@@ -142,7 +142,8 @@ export interface Problem {
|
|
|
142
142
|
* 后面认不出路径的行(多行说明、代码片段)归到它下面;第一个问题之前的行单独算一个没有文件的问题
|
|
143
143
|
*/
|
|
144
144
|
export function parseProblems(output: string, cwd: string): Problem[] {
|
|
145
|
-
|
|
145
|
+
// 路径里可以有括号(Next.js 的 app/(group)/page.tsx),只是不能以括号开头
|
|
146
|
+
const START = /^\s*((?:[A-Za-z]:[\\/])?[^\s:()][^:]*?)(?:\(\d+,\d+\)|:\d+(?::\d+)?)(?=[:\s]|$)/;
|
|
146
147
|
const out: Problem[] = [];
|
|
147
148
|
let cur: Problem | undefined;
|
|
148
149
|
for (const line of output.split(/\r?\n/)) {
|
package/plugins/checks/index.ts
CHANGED
|
@@ -10,13 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
import { createHash } from "node:crypto";
|
|
12
12
|
import path from "node:path";
|
|
13
|
-
import { definePlugin, type SessionContext, type UI } from "
|
|
14
|
-
import type { Approval } from "
|
|
15
|
-
import type { Commands } from "../commands/index.ts";
|
|
16
|
-
import type { EventRenderer, Renderers } from "../renderers/index.ts";
|
|
13
|
+
import { definePlugin, type SessionContext, type UI } from "@uncarved/jade-api";
|
|
14
|
+
import type { Approval, Commands, EventRenderer, Renderers, Shell } from "@uncarved/jade-api/services";
|
|
17
15
|
import { changedFiles, checks, type Check, type Checks } from "./checks.ts";
|
|
18
16
|
import { bold, dim, display, realPath, repoRoot, TrustFile, yellow } from "./project.ts";
|
|
19
|
-
import type { Shell } from "../shell/service.ts";
|
|
20
17
|
|
|
21
18
|
/** 确认记录:同一个目录里的同一条命令只问一次。和旧 jade 的算法相同 */
|
|
22
19
|
const checkHash = (c: Check) => createHash("sha256").update(`jade-check\0${c.cwd}\0${c.command}`).digest("hex");
|