@uncarved/jade 0.2.1 → 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/README.md +64 -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 +233 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +2 -1
- 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 +6 -4
- package/plugins/fs/index.ts +2 -3
- 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 +40 -12
- 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 +35 -42
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +22 -4
- 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 +1 -1
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/index.ts +26 -13
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +56 -6
- 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 +31 -16
- 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 +7 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +23 -5
- package/plugins/todo/index.ts +2 -2
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +103 -21
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -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 +321 -90
- 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 +2 -5
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +217 -51
- package/plugins-dist/approval/plugin.json +1 -1
- 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 +5 -3
- package/plugins-dist/fs/index.js +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 +41 -9
- 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 +37 -14
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +4 -2
- 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 +1 -1
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/index.js +25 -11
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +55 -3
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +26 -8
- 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 +6 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +20 -5
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +107 -13
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +439 -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 +302 -81
- 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 +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,161 @@ 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 rule = rules.ruleFor(call);
|
|
206
|
+
const shown = (await preview(renderers, call)) ?? detail(call);
|
|
207
|
+
const expired = new AbortController();
|
|
208
|
+
const timer = setTimeout(() => expired.abort(), askTimeoutMs);
|
|
209
|
+
let got: { answer: Answer; by: string; said?: string } | undefined;
|
|
210
|
+
try {
|
|
211
|
+
got = await ask(
|
|
212
|
+
ui,
|
|
213
|
+
{
|
|
214
|
+
title: `Allow ${call.name}?`,
|
|
215
|
+
// 审查转给用户的:先说为什么
|
|
216
|
+
detail: asked.why ? `${asked.why}\n\n${shown}` : shown,
|
|
217
|
+
options: [
|
|
218
|
+
{ label: "Allow once", value: "once" },
|
|
219
|
+
// 别的插件要问的调用不能"总是允许":这里的规则管不到它的关卡
|
|
220
|
+
...(asked.mine
|
|
221
|
+
? [
|
|
222
|
+
{ label: `Always allow ${describeRule(rule)} in this session`, value: "always" as const },
|
|
223
|
+
{ label: `Always allow ${describeRule(rule)} in this directory, in every session`, value: "forever" as const },
|
|
224
|
+
]
|
|
225
|
+
: []),
|
|
226
|
+
{ label: "Deny", value: "deny" },
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
AbortSignal.any([signal, expired.signal, ctx.signal]),
|
|
230
|
+
);
|
|
231
|
+
} finally {
|
|
232
|
+
clearTimeout(timer);
|
|
233
|
+
}
|
|
234
|
+
if (expired.signal.aborted) {
|
|
235
|
+
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.` };
|
|
236
|
+
}
|
|
237
|
+
if (got?.answer === "always" && asked.mine) {
|
|
238
|
+
added.push(rule);
|
|
239
|
+
return { approved: true, answer: "always", by: got.by };
|
|
240
|
+
}
|
|
241
|
+
if (got?.answer === "forever" && asked.mine) {
|
|
242
|
+
forever.push(rule);
|
|
243
|
+
try {
|
|
244
|
+
await addForever(foreverFile, ctx.cwd, rule);
|
|
245
|
+
ui?.notify(`${describeRule(rule)} is now always allowed in ${ctx.cwd}. To ask again, remove it from ${foreverFile}`, "info");
|
|
246
|
+
} catch (e) {
|
|
247
|
+
ui?.notify(`could not save the rule to ${foreverFile} (${e instanceof Error ? e.message : String(e)}); it only applies to this session`, "warning");
|
|
248
|
+
}
|
|
249
|
+
return { approved: true, answer: "forever", by: got.by };
|
|
250
|
+
}
|
|
251
|
+
if (got !== undefined && got.answer !== "deny") return { approved: true, answer: "once", by: got.by };
|
|
252
|
+
if (got?.said) return { approved: false, answer: "deny", by: got.by, reason: `The user denied this tool call and said: ${got.said}` };
|
|
253
|
+
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." };
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/** 一个要问的调用:得出回答,交给会话。出错也要回答(拒绝),否则这一轮一直等着 */
|
|
257
|
+
const settle = async (asked: Asked, signal: AbortSignal) => {
|
|
258
|
+
const toolUseId = asked.call.toolUseId;
|
|
259
|
+
let verdict: Verdict;
|
|
260
|
+
try {
|
|
261
|
+
verdict = await decide(asked, signal);
|
|
262
|
+
} catch (e) {
|
|
263
|
+
verdict = { approved: false, answer: "deny", by: "error", reason: `Asking for approval failed: ${e instanceof Error ? e.message : String(e)}` };
|
|
264
|
+
}
|
|
265
|
+
// 这一轮已经结束(被中断):回答作废
|
|
266
|
+
if (signal.aborted) return;
|
|
267
|
+
answered.set(toolUseId, { answer: verdict.answer, by: verdict.by });
|
|
268
|
+
// 不等它:它要到内核再次停下才 resolve,后面排着的问题可能就是内核在等的
|
|
269
|
+
void Promise.resolve()
|
|
270
|
+
.then(() => ctx.session.send({ kind: "approval", toolUseId, approved: verdict.approved, ...(verdict.reason ? { reason: verdict.reason } : {}) }))
|
|
271
|
+
.catch(() => void answered.delete(toolUseId));
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
ctx.kernel.hook({
|
|
275
|
+
kind: "observe",
|
|
276
|
+
point: "event",
|
|
277
|
+
fn: async (e, hook) => {
|
|
278
|
+
if (e.type === "tool.decided") {
|
|
279
|
+
const d = e.data as EventDataMap["tool.decided"];
|
|
280
|
+
const asked = seen.get(d.toolUseId);
|
|
281
|
+
seen.delete(d.toolUseId);
|
|
282
|
+
if (d.decision !== "ask" || !asked) return;
|
|
283
|
+
// 不在这里等回答:observe 要马上返回,内核才能停下来等
|
|
284
|
+
handled.add(d.toolUseId);
|
|
285
|
+
const signal = turn.signal;
|
|
286
|
+
asking = asking.then(() => settle(asked, signal));
|
|
287
|
+
} else if (e.type === "input.received") {
|
|
288
|
+
const d = e.data as EventDataMap["input.received"];
|
|
289
|
+
if (d.kind !== "approval") return;
|
|
290
|
+
const a = answered.get(d.toolUseId);
|
|
291
|
+
if (!a) return;
|
|
292
|
+
answered.delete(d.toolUseId);
|
|
293
|
+
// 内核只记同意或拒绝:选了什么、谁回答的(终端、哪个渠道、规则、超时)记在这里
|
|
294
|
+
await hook.emit("answered", { toolUseId: d.toolUseId, ...a });
|
|
295
|
+
if (a.answer === "always") await hook.record(RULES_KEY, merged((hook.derived[RULES_KEY] as AllowRule[] | undefined) ?? []));
|
|
296
|
+
} else if (e.type === "turn.ended") {
|
|
297
|
+
turn.abort(new Error("the turn ended"));
|
|
298
|
+
turn = new AbortController();
|
|
299
|
+
seen.clear();
|
|
300
|
+
answered.clear();
|
|
301
|
+
handled.clear();
|
|
195
302
|
}
|
|
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
303
|
},
|
|
226
304
|
});
|
|
305
|
+
|
|
306
|
+
// 恢复一个上次停在等审批的会话(进程被杀、崩溃):那些调用的关卡是上一个进程跑的,这里没见过。
|
|
307
|
+
// 内核恢复时再发一次 awaiting,据此重新问。只知道调用的名字和参数(工具的声明只有关卡拿得到),
|
|
308
|
+
// 所以当作别的关卡要问的:只有"允许一次"和"拒绝",不重新套规则
|
|
309
|
+
ctx.session.subscribe((n) => {
|
|
310
|
+
if (n.type !== "awaiting") return;
|
|
311
|
+
const waiting = n.toolUseIds.filter((t) => !handled.has(t) && !seen.has(t));
|
|
312
|
+
if (waiting.length === 0) return;
|
|
313
|
+
const batch = ctx.session.state().batch;
|
|
314
|
+
const signal = turn.signal;
|
|
315
|
+
for (const toolUseId of waiting) {
|
|
316
|
+
const c = batch.find((x) => x.toolUseId === toolUseId && x.decision === "ask");
|
|
317
|
+
if (!c) continue;
|
|
318
|
+
handled.add(toolUseId);
|
|
319
|
+
asking = asking.then(() => settle({ call: { toolUseId, name: c.name, input: c.input } }, signal));
|
|
320
|
+
}
|
|
321
|
+
});
|
|
227
322
|
},
|
|
228
323
|
});
|
|
229
324
|
|
|
325
|
+
/** 一个要问的调用。没有 mine 的是别的插件的关卡要问的,或者恢复会话时还在等的;why:auto 模式的审查转给用户时的理由 */
|
|
326
|
+
interface Asked {
|
|
327
|
+
call: AskedCall;
|
|
328
|
+
/** 这里的规则要问的:关卡看到的完整调用,问之前按它重新判断一次 */
|
|
329
|
+
mine?: GatedToolCall;
|
|
330
|
+
why?: string;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** 要问的调用里给用户看的部分;恢复的会话里只有这些 */
|
|
334
|
+
type AskedCall = Pick<GatedToolCall, "toolUseId" | "name" | "input">;
|
|
335
|
+
|
|
336
|
+
/** 交给内核的回答,和记进 answered 事件的部分(answer、by) */
|
|
337
|
+
interface Verdict {
|
|
338
|
+
approved: boolean;
|
|
339
|
+
answer: "allow" | Answer;
|
|
340
|
+
by: string;
|
|
341
|
+
reason?: string;
|
|
342
|
+
}
|
|
343
|
+
|
|
230
344
|
/** allowRead、allowWrite:把目录登记进 roots,调用方的作用域关闭时撤销 */
|
|
231
345
|
function allow(caller: Caller, name: string, roots: string[], dir: string): () => void {
|
|
232
346
|
if (caller.layer === "project") throw new Error(`approval.${name}: project plugins cannot widen what runs without asking`);
|
|
@@ -242,23 +356,33 @@ function allow(caller: Caller, name: string, roots: string[], dir: string): () =
|
|
|
242
356
|
}
|
|
243
357
|
|
|
244
358
|
/**
|
|
245
|
-
*
|
|
359
|
+
* 把问题同时交给终端(能提问时)和所有审批渠道,返回先到的回答和回答的人("terminal" 或渠道的插件 id),然后中止其余的。
|
|
246
360
|
* 终端里取消(undefined)算拒绝;渠道返回 undefined、不认识的值或出错算它不回答。都不回答时返回 undefined(拒绝)
|
|
247
361
|
*/
|
|
248
|
-
async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: AbortSignal): Promise<Answer | undefined> {
|
|
362
|
+
async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: AbortSignal): Promise<{ answer: Answer; by: string; said?: string } | undefined> {
|
|
249
363
|
const done = new AbortController();
|
|
250
364
|
const stop = () => done.abort(signal.reason);
|
|
251
365
|
signal.addEventListener("abort", stop, { once: true });
|
|
252
366
|
try {
|
|
253
|
-
return await new Promise<Answer | undefined>((resolve, reject) => {
|
|
254
|
-
const channels: Promise<Answer
|
|
255
|
-
if (ui?.interactive)
|
|
256
|
-
|
|
367
|
+
return await new Promise<{ answer: Answer; by: string; said?: string } | undefined>((resolve, reject) => {
|
|
368
|
+
const channels: Promise<{ answer: Answer; by: string; said?: string } | typeof NONE>[] = [];
|
|
369
|
+
if (ui?.interactive) {
|
|
370
|
+
// 终端里多一项:拒绝并说明。选了它再问一句话,取消或什么都没写就是普通的拒绝
|
|
371
|
+
const options = [...request.options, { label: "Deny, and tell the agent what to do instead", value: EXPLAIN }];
|
|
372
|
+
channels.push(
|
|
373
|
+
ui.select<string>({ ...request, options }, done.signal).then(async (a) => {
|
|
374
|
+
if (a !== EXPLAIN) return { answer: a !== undefined && ANSWERS.includes(a) ? (a as Answer) : "deny", by: "terminal" };
|
|
375
|
+
const said = (await ui.input({ title: "What should the agent do instead?" }, done.signal))?.trim();
|
|
376
|
+
return { answer: "deny" as const, by: "terminal", ...(said ? { said } : {}) };
|
|
377
|
+
}),
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
for (const [approver, by] of approvers) {
|
|
257
381
|
channels.push(
|
|
258
382
|
Promise.resolve()
|
|
259
383
|
.then(() => approver(request, done.signal))
|
|
260
384
|
.then(
|
|
261
|
-
(a) => (typeof a === "string" && ANSWERS.includes(a) ?
|
|
385
|
+
(a) => (typeof a === "string" && ANSWERS.includes(a) ? { answer: a as Answer, by } : NONE),
|
|
262
386
|
() => NONE,
|
|
263
387
|
),
|
|
264
388
|
);
|
|
@@ -279,11 +403,35 @@ async function ask(ui: UI | undefined, request: SelectRequest<Answer>, signal: A
|
|
|
279
403
|
}
|
|
280
404
|
const NONE = Symbol("no answer");
|
|
281
405
|
|
|
406
|
+
/** approvals.json:工作目录 → 在这个目录里总是允许的规则。读不了或格式不对时当作没有 */
|
|
407
|
+
async function readForever(file: string): Promise<Record<string, AllowRule[]>> {
|
|
408
|
+
try {
|
|
409
|
+
const data = JSON.parse(await readFile(file, "utf8")) as unknown;
|
|
410
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) return {};
|
|
411
|
+
const out: Record<string, AllowRule[]> = {};
|
|
412
|
+
for (const [dir, rules] of Object.entries(data)) {
|
|
413
|
+
if (Array.isArray(rules)) out[dir] = rules.filter((r): r is AllowRule => !!r && typeof r === "object" && typeof (r as AllowRule).tool === "string");
|
|
414
|
+
}
|
|
415
|
+
return out;
|
|
416
|
+
} catch {
|
|
417
|
+
return {};
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
async function addForever(file: string, cwd: string, rule: AllowRule): Promise<void> {
|
|
422
|
+
const all = await readForever(file);
|
|
423
|
+
const rules = all[cwd] ?? [];
|
|
424
|
+
if (!rules.some((r) => JSON.stringify(r) === JSON.stringify(rule))) rules.push(rule);
|
|
425
|
+
all[cwd] = rules;
|
|
426
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
427
|
+
await writeFile(file, `${JSON.stringify(all, null, 2)}\n`);
|
|
428
|
+
}
|
|
429
|
+
|
|
282
430
|
/**
|
|
283
431
|
* 工具自己登记的审批预览(例如文件修改的 diff)。没有登记、返回的不是文字、或者出错时返回 undefined,
|
|
284
432
|
* 用下面的 detail:预览是别的插件的代码,它坏了不能让审批卡住或失败
|
|
285
433
|
*/
|
|
286
|
-
async function preview(renderers: Renderers | undefined, call:
|
|
434
|
+
async function preview(renderers: Renderers | undefined, call: AskedCall): Promise<string | undefined> {
|
|
287
435
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
288
436
|
try {
|
|
289
437
|
// 预览可能读文件、算 diff;一直不返回时也要能问用户
|
|
@@ -300,7 +448,7 @@ async function preview(renderers: Renderers | undefined, call: GatedToolCall): P
|
|
|
300
448
|
}
|
|
301
449
|
|
|
302
450
|
/** 给用户看的调用内容:命令、路径和要写的内容,太长的截断 */
|
|
303
|
-
function detail(call:
|
|
451
|
+
function detail(call: AskedCall): string {
|
|
304
452
|
const input = call.input as Record<string, unknown> | null;
|
|
305
453
|
let text: string;
|
|
306
454
|
if (typeof input?.command === "string") text = input.command;
|
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
import { realpathSync, statSync } from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import type { GatedToolCall, GateResult } from "uncarved";
|
|
9
|
+
import type { ApprovalMode } from "@uncarved/jade-api/services";
|
|
9
10
|
import { validate } from "./schema.ts";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* 权限模式。ask:改东西先问;auto:工作目录里的改动和只建目录、碰文件的命令不问;
|
|
13
14
|
* plan:不改任何东西,拒绝时告诉模型先给出计划;readonly:不改任何东西(子 Agent 用);yolo:什么都不问
|
|
14
15
|
*/
|
|
15
|
-
export const MODES = ["ask", "auto", "plan", "readonly", "yolo"] as const;
|
|
16
|
+
export const MODES = ["ask", "auto", "plan", "readonly", "yolo"] as const satisfies readonly ApprovalMode[];
|
|
16
17
|
export type Mode = (typeof MODES)[number];
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -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");
|
|
@@ -1,50 +1,8 @@
|
|
|
1
1
|
// jade/commands:斜杠命令的注册表。只登记,不执行:前端解析输入、带着自己的上下文调用 run。
|
|
2
2
|
// 每个调用方一个实例,调用方关闭(停用、被隔离、换会话时的会话作用域)时它注册的命令自动撤销。
|
|
3
3
|
|
|
4
|
-
import { definePlugin
|
|
5
|
-
|
|
6
|
-
export interface Command {
|
|
7
|
-
/** 不带斜杠,字母、数字、- 和 _ */
|
|
8
|
-
name: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
/** 参数提示,例如 "<file>" */
|
|
11
|
-
args?: string;
|
|
12
|
-
/**
|
|
13
|
-
* 不碰会话(只查看、只改插件自己的设置)的命令设为 true:模型工作时也立即执行,不排队。
|
|
14
|
-
* 这种命令返回 { prompt } 无效
|
|
15
|
-
*/
|
|
16
|
-
instant?: boolean;
|
|
17
|
-
/** 字符串:显示给用户;{ prompt }:作为用户消息发给模型;不返回:什么也不做 */
|
|
18
|
-
run(args: string, ctx: CommandContext): CommandResult | Promise<CommandResult>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type CommandResult = string | { prompt: string } | void | undefined;
|
|
22
|
-
|
|
23
|
-
/** 前端执行命令时给的上下文 */
|
|
24
|
-
export interface CommandContext {
|
|
25
|
-
/** 用户中断(esc)或退出时中止 */
|
|
26
|
-
signal: AbortSignal;
|
|
27
|
-
ui: UI;
|
|
28
|
-
/** 当前会话、新建、恢复(/clear、/resume 用它) */
|
|
29
|
-
sessions: SessionManager;
|
|
30
|
-
/** 在对话里显示一段文字,可以带 ANSI 颜色 */
|
|
31
|
-
print(text: string): void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface CommandEntry {
|
|
35
|
-
command: Command;
|
|
36
|
-
/** 注册它的插件 */
|
|
37
|
-
plugin: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface Commands {
|
|
41
|
-
/** 注册一条命令,返回撤销函数。名字已被占用时抛错 */
|
|
42
|
-
register(command: Command): () => void;
|
|
43
|
-
/** 按名字找(不分大小写) */
|
|
44
|
-
get(name: string): CommandEntry | undefined;
|
|
45
|
-
/** 全部命令,按名字排序 */
|
|
46
|
-
list(): { name: string; args?: string; description?: string; instant?: boolean; plugin: string }[];
|
|
47
|
-
}
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
|
+
import type { CommandEntry, Commands } from "@uncarved/jade-api/services";
|
|
48
6
|
|
|
49
7
|
const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
50
8
|
const registry = new Map<string, CommandEntry>();
|
|
@@ -39,8 +39,8 @@ export interface Compactor {
|
|
|
39
39
|
project(messages: Message[], ctx: HookContext): Promise<Message[]>;
|
|
40
40
|
/** event 的 observe:记下真实用量 */
|
|
41
41
|
observe(e: LogEvent): void;
|
|
42
|
-
/**
|
|
43
|
-
request(): void;
|
|
42
|
+
/** 下一次请求前无论是否超过阈值都总结一次;focus 是用户要总结特别留意的事(/compact 后面写的) */
|
|
43
|
+
request(focus?: string): void;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
const SUMMARY_SYSTEM =
|
|
@@ -125,6 +125,7 @@ const total = (u: Usage | undefined) => (u?.inputTokens === undefined ? undefine
|
|
|
125
125
|
export function compactor(opts: CompactorOptions): Compactor {
|
|
126
126
|
const id = opts.id ?? "jade/compact";
|
|
127
127
|
let forced = false;
|
|
128
|
+
let focus: string | undefined;
|
|
128
129
|
/** 最近一次投影:它对应的消息条数和压缩状态;收到回复后补上真实用量 */
|
|
129
130
|
let last: { length: number; state: string; tokens?: number } | undefined;
|
|
130
131
|
let pending: { length: number; state: string } | undefined;
|
|
@@ -189,6 +190,8 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
189
190
|
}
|
|
190
191
|
}
|
|
191
192
|
forced = false;
|
|
193
|
+
const asked = focus;
|
|
194
|
+
focus = undefined;
|
|
192
195
|
|
|
193
196
|
const res = await opts.complete(
|
|
194
197
|
{
|
|
@@ -205,6 +208,7 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
205
208
|
origin: { by: "plugin", id },
|
|
206
209
|
trust: "untrusted",
|
|
207
210
|
},
|
|
211
|
+
...(asked ? [{ kind: "text" as const, text: `The user asked that the summary pay particular attention to: ${asked}`, origin: { by: "plugin" as const, id }, trust: "trusted" as const }] : []),
|
|
208
212
|
],
|
|
209
213
|
},
|
|
210
214
|
],
|
|
@@ -235,8 +239,9 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
235
239
|
last = { ...pending, tokens: total((e as LogEvent<"model.responded">).data.usage) };
|
|
236
240
|
pending = undefined;
|
|
237
241
|
},
|
|
238
|
-
request() {
|
|
242
|
+
request(what) {
|
|
239
243
|
forced = true;
|
|
244
|
+
focus = what;
|
|
240
245
|
},
|
|
241
246
|
};
|
|
242
247
|
}
|