@uncarved/jade 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/README.md +277 -0
- package/assets/logo.svg +1 -0
- package/bin/jade.js +15 -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 +28 -5
- 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 +6 -4
- package/plugins/approval/index.ts +246 -84
- package/plugins/approval/plugin.json +5 -1
- package/plugins/approval/policy.ts +27 -6
- 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 +15 -4
- 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 +110 -17
- package/plugins/fs/grep.ts +41 -11
- package/plugins/fs/index.ts +9 -7
- 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 +33 -13
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +50 -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 +60 -12
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +50 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +14 -8
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +16 -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 +8 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +119 -15
- package/plugins/todo/index.ts +4 -4
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +130 -25
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -0
- package/plugins/tui/mentions.ts +48 -8
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +22 -3
- package/plugins/tui/tui.ts +323 -95
- package/plugins/tui/vendor.ts +16 -0
- package/plugins/undo/checkpoint.ts +26 -2
- package/plugins/undo/index.ts +86 -24
- 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/web/web.ts +6 -5
- package/plugins-dist/approval/index.js +232 -50
- package/plugins-dist/approval/plugin.json +5 -1
- package/plugins-dist/approval/policy.js +27 -5
- 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 +13 -2
- 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 +106 -17
- package/plugins-dist/fs/grep.js +40 -12
- package/plugins-dist/fs/index.js +8 -5
- 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 +29 -11
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +53 -16
- 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 +62 -9
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +49 -2
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +13 -8
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +15 -3
- 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 +7 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +106 -15
- package/plugins-dist/todo/index.js +3 -3
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +135 -18
- 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/mentions.js +44 -6
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +19 -2
- package/plugins-dist/tui/tui.js +303 -83
- package/plugins-dist/tui/vendor.js +13822 -0
- package/plugins-dist/undo/checkpoint.js +28 -1
- package/plugins-dist/undo/index.js +92 -22
- 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/plugins-dist/web/web.js +6 -5
- 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 +25 -6
- 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,17 +1,27 @@
|
|
|
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 改。
|
|
9
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
6
10
|
import path from "node:path";
|
|
7
|
-
import { definePlugin } from "
|
|
11
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
12
|
import { describeRule, MODES, policy } from "./policy.js";
|
|
9
13
|
import { reviewer } from "./review.js";
|
|
10
|
-
const ANSWERS = ["once", "always", "deny"];
|
|
14
|
+
const ANSWERS = ["once", "always", "forever", "deny"];
|
|
15
|
+
/** 只有终端里有的一项:拒绝,并写一句话告诉模型该怎么做 */
|
|
16
|
+
const EXPLAIN = "explain";
|
|
11
17
|
/** derived 里保存"总是允许"规则的键 */
|
|
12
18
|
const RULES_KEY = "jade/approval/rules";
|
|
13
|
-
/**
|
|
19
|
+
/** 等用户回答的默认时限,过了按拒绝处理。配置 askTimeoutMinutes 可以改 */
|
|
14
20
|
const ASK_TIMEOUT_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
/** askTimeoutMinutes 的上限:定时器最长约 24.8 天 */
|
|
22
|
+
const MAX_ASK_TIMEOUT_MINUTES = 7 * 24 * 60;
|
|
23
|
+
/** 关卡的时限:auto 模式的审查最长一分钟(review.ts),比 hook 默认的 30 秒长 */
|
|
24
|
+
const GATE_TIMEOUT_MS = 2 * 60_000;
|
|
15
25
|
const MAX_DETAIL_CHARS = 2000;
|
|
16
26
|
/** 等工具的审批预览的时限,过了就用默认的 detail */
|
|
17
27
|
const PREVIEW_TIMEOUT_MS = 5000;
|
|
@@ -28,12 +38,15 @@ const reviewedKey = (call) => {
|
|
|
28
38
|
let mode = "ask";
|
|
29
39
|
/** 配置里写错的模式:第一个会话打开时告诉用户(process 里可能还拿不到 ui@1:它的提供者可能排在后面激活) */
|
|
30
40
|
let badMode;
|
|
41
|
+
let askTimeoutMs = ASK_TIMEOUT_MS;
|
|
42
|
+
/** 配置里写错的 askTimeoutMinutes,和 badMode 一样在第一个会话打开时告诉用户 */
|
|
43
|
+
let badTimeout;
|
|
31
44
|
/** 经 allowRead 登记的目录;policy 每次判断时读它 */
|
|
32
45
|
const readRoots = [];
|
|
33
46
|
/** 经 allowWrite 登记的目录;policy 每次判断时读它 */
|
|
34
47
|
const writeRoots = [];
|
|
35
|
-
/** 经 addApprover 登记的审批渠道 */
|
|
36
|
-
const approvers = new
|
|
48
|
+
/** 经 addApprover 登记的审批渠道 → 登记它的插件 */
|
|
49
|
+
const approvers = new Map();
|
|
37
50
|
/** 打开着的会话:会话 id → 判断只读调用能不能不问就执行(allows) */
|
|
38
51
|
const sessionChecks = new Map();
|
|
39
52
|
export default definePlugin({
|
|
@@ -43,6 +56,13 @@ export default definePlugin({
|
|
|
43
56
|
mode = configured;
|
|
44
57
|
else if (configured !== undefined)
|
|
45
58
|
badMode = JSON.stringify(configured);
|
|
59
|
+
const minutes = ctx.config.askTimeoutMinutes;
|
|
60
|
+
askTimeoutMs = ASK_TIMEOUT_MS;
|
|
61
|
+
badTimeout = undefined;
|
|
62
|
+
if (typeof minutes === "number" && Number.isFinite(minutes) && minutes > 0 && minutes <= MAX_ASK_TIMEOUT_MINUTES)
|
|
63
|
+
askTimeoutMs = minutes * 60_000;
|
|
64
|
+
else if (minutes !== undefined)
|
|
65
|
+
badTimeout = JSON.stringify(minutes);
|
|
46
66
|
ctx.provide("approval@1", (caller) => ({
|
|
47
67
|
mode: () => mode,
|
|
48
68
|
setMode(next) {
|
|
@@ -57,12 +77,12 @@ export default definePlugin({
|
|
|
57
77
|
throw new Error("approval.addApprover: project plugins cannot answer approvals");
|
|
58
78
|
if (typeof approver !== "function")
|
|
59
79
|
throw new Error("approval.addApprover: approver must be a function");
|
|
60
|
-
approvers.
|
|
80
|
+
approvers.set(approver, caller.plugin);
|
|
61
81
|
const remove = () => void approvers.delete(approver);
|
|
62
82
|
caller.onDispose(remove);
|
|
63
83
|
return remove;
|
|
64
84
|
},
|
|
65
|
-
allows: (session, call) => call.tool
|
|
85
|
+
allows: (session, call) => call.tool !== undefined && (sessionChecks.get(session)?.(call) ?? false),
|
|
66
86
|
}));
|
|
67
87
|
},
|
|
68
88
|
async session(ctx) {
|
|
@@ -72,6 +92,10 @@ export default definePlugin({
|
|
|
72
92
|
ui?.notify(`jade/approval: mode must be one of ${MODES.join(", ")}, got ${badMode}; using ask`, "warning");
|
|
73
93
|
badMode = undefined;
|
|
74
94
|
}
|
|
95
|
+
if (badTimeout !== undefined) {
|
|
96
|
+
ui?.notify(`jade/approval: askTimeoutMinutes must be a number of minutes between 0 and ${MAX_ASK_TIMEOUT_MINUTES}, got ${badTimeout}; using 24 hours`, "warning");
|
|
97
|
+
badTimeout = undefined;
|
|
98
|
+
}
|
|
75
99
|
const renderers = ctx.service("renderers@1");
|
|
76
100
|
// auto 模式的审查用当前配置的模型;没有 models@1(模型插件被停用)或者配置里关掉时不做审查
|
|
77
101
|
const models = ctx.service("models@1");
|
|
@@ -92,14 +116,29 @@ export default definePlugin({
|
|
|
92
116
|
/** 本会话里审查过一次、放行了的调用 */
|
|
93
117
|
const reviewedCalls = new Set();
|
|
94
118
|
let warned = false;
|
|
95
|
-
/** 本进程里刚选的"总是允许"
|
|
119
|
+
/** 本进程里刚选的"总是允许":记进日志之前(要等看到那条 approval 输入)就要生效 */
|
|
96
120
|
const added = [];
|
|
97
121
|
/** 一次只问一个问题;排在后面的问之前重新判断,前面刚选的"总是允许"可能已经覆盖它 */
|
|
98
122
|
let asking = Promise.resolve();
|
|
99
123
|
/** 日志里记下的"总是允许"规则,每次调用工具前取一次:子 Agent 在工具执行期间经 allows 用它 */
|
|
100
124
|
let logged = [];
|
|
125
|
+
const merged = (from) => [...from, ...added.filter((r) => !from.some((l) => JSON.stringify(l) === JSON.stringify(r)))];
|
|
126
|
+
/** "在这个目录里总是允许"的规则:会话打开时从文件读,本会话里新选的也加进来 */
|
|
127
|
+
const foreverFile = path.join(ctx.home, "state", "approvals.json");
|
|
128
|
+
const forever = (await readForever(foreverFile))[ctx.cwd] ?? [];
|
|
129
|
+
const saved = () => [...forever, ...merged(logged)];
|
|
130
|
+
/** 这一轮里关卡看过、还没有决定的调用 */
|
|
131
|
+
const seen = new Map();
|
|
132
|
+
/** 已经交给会话的回答:在日志里看到那条 approval 输入时记一条 answered 事件 */
|
|
133
|
+
const answered = new Map();
|
|
134
|
+
/** 这一轮里已经在问、或者问过的调用 */
|
|
135
|
+
const handled = new Set();
|
|
136
|
+
/** 这一轮结束(包括被中断)时中止还开着的问题 */
|
|
137
|
+
let turn = new AbortController();
|
|
138
|
+
const canAsk = () => ui?.interactive === true || approvers.size > 0;
|
|
139
|
+
const nobody = (name) => `${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.`;
|
|
101
140
|
const id = ctx.session.id;
|
|
102
|
-
const check = (call) => rules.decide(call, { mode: mode === "yolo" ? "yolo" : "readonly", rules: [...logged, ...added] }).decision === "allow";
|
|
141
|
+
const check = (call) => rules.decide(call, { mode: mode === "yolo" && call.tool.effect === "read" ? "yolo" : "readonly", rules: [...forever, ...logged, ...added] }).decision === "allow";
|
|
103
142
|
sessionChecks.set(id, check);
|
|
104
143
|
await ctx.effect(() => () => {
|
|
105
144
|
if (sessionChecks.get(id) === check)
|
|
@@ -116,12 +155,9 @@ export default definePlugin({
|
|
|
116
155
|
ctx.kernel.hook({
|
|
117
156
|
kind: "gate",
|
|
118
157
|
point: "tool.gate",
|
|
119
|
-
timeoutMs:
|
|
158
|
+
timeoutMs: GATE_TIMEOUT_MS,
|
|
120
159
|
fn: async (call, hook) => {
|
|
121
|
-
|
|
122
|
-
const logged = hook.derived[RULES_KEY] ?? [];
|
|
123
|
-
return [...logged, ...added.filter((r) => !logged.some((l) => JSON.stringify(l) === JSON.stringify(r)))];
|
|
124
|
-
};
|
|
160
|
+
seen.set(call.toolUseId, { call });
|
|
125
161
|
const result = rules.decide(call, { mode, rules: saved() });
|
|
126
162
|
if (result.decision !== "ask")
|
|
127
163
|
return result;
|
|
@@ -142,43 +178,155 @@ export default definePlugin({
|
|
|
142
178
|
}
|
|
143
179
|
why = verdict.reason;
|
|
144
180
|
}
|
|
145
|
-
if (!
|
|
181
|
+
if (!canAsk()) {
|
|
146
182
|
if (!warned) {
|
|
147
183
|
warned = true;
|
|
148
184
|
ui?.notify(`${call.name} needs approval, but this jade cannot ask. To let it make changes in the working directory, ` +
|
|
149
185
|
`set {"jade/approval": {"mode": "auto"}} in ${ctx.home}/config.json`, "warning");
|
|
150
186
|
}
|
|
151
|
-
return { decision: "deny", reason:
|
|
187
|
+
return { decision: "deny", reason: nobody(call.name) };
|
|
188
|
+
}
|
|
189
|
+
seen.set(call.toolUseId, { call, mine: call, ...(why ? { why } : {}) });
|
|
190
|
+
return { decision: "ask" };
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
/** 问用户(或按刚加的规则直接决定),返回交给内核的回答 */
|
|
194
|
+
const decide = async (asked, signal) => {
|
|
195
|
+
const { call } = asked;
|
|
196
|
+
if (asked.mine) {
|
|
197
|
+
const again = rules.decide(asked.mine, { mode, rules: saved() });
|
|
198
|
+
if (again.decision === "allow")
|
|
199
|
+
return { approved: true, answer: "allow", by: "rule" };
|
|
200
|
+
if (again.decision === "deny")
|
|
201
|
+
return { approved: false, answer: "deny", by: "rule", reason: again.reason };
|
|
202
|
+
}
|
|
203
|
+
if (!canAsk())
|
|
204
|
+
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;
|
|
210
|
+
try {
|
|
211
|
+
got = await ask(ui, {
|
|
212
|
+
title: `Allow ${call.name}?`,
|
|
213
|
+
// 审查转给用户的:先说为什么
|
|
214
|
+
detail: asked.why ? `${asked.why}\n\n${shown}` : shown,
|
|
215
|
+
options: [
|
|
216
|
+
{ label: "Allow once", value: "once" },
|
|
217
|
+
// 别的插件要问的调用不能"总是允许":这里的规则管不到它的关卡
|
|
218
|
+
...(asked.mine
|
|
219
|
+
? [
|
|
220
|
+
{ label: `Always allow ${describeRule(rule)} in this session`, value: "always" },
|
|
221
|
+
{ label: `Always allow ${describeRule(rule)} in this directory, in every session`, value: "forever" },
|
|
222
|
+
]
|
|
223
|
+
: []),
|
|
224
|
+
{ label: "Deny", value: "deny" },
|
|
225
|
+
],
|
|
226
|
+
}, AbortSignal.any([signal, expired.signal, ctx.signal]));
|
|
227
|
+
}
|
|
228
|
+
finally {
|
|
229
|
+
clearTimeout(timer);
|
|
230
|
+
}
|
|
231
|
+
if (expired.signal.aborted) {
|
|
232
|
+
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.` };
|
|
233
|
+
}
|
|
234
|
+
if (got?.answer === "always" && asked.mine) {
|
|
235
|
+
added.push(rule);
|
|
236
|
+
return { approved: true, answer: "always", by: got.by };
|
|
237
|
+
}
|
|
238
|
+
if (got?.answer === "forever" && asked.mine) {
|
|
239
|
+
forever.push(rule);
|
|
240
|
+
try {
|
|
241
|
+
await addForever(foreverFile, ctx.cwd, rule);
|
|
242
|
+
ui?.notify(`${describeRule(rule)} is now always allowed in ${ctx.cwd}. To ask again, remove it from ${foreverFile}`, "info");
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
ui?.notify(`could not save the rule to ${foreverFile} (${e instanceof Error ? e.message : String(e)}); it only applies to this session`, "warning");
|
|
246
|
+
}
|
|
247
|
+
return { approved: true, answer: "forever", by: got.by };
|
|
248
|
+
}
|
|
249
|
+
if (got !== undefined && got.answer !== "deny")
|
|
250
|
+
return { approved: true, answer: "once", by: got.by };
|
|
251
|
+
if (got?.said)
|
|
252
|
+
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
|
+
const settle = async (asked, signal) => {
|
|
257
|
+
const toolUseId = asked.call.toolUseId;
|
|
258
|
+
let verdict;
|
|
259
|
+
try {
|
|
260
|
+
verdict = await decide(asked, signal);
|
|
261
|
+
}
|
|
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)
|
|
267
|
+
return;
|
|
268
|
+
answered.set(toolUseId, { answer: verdict.answer, by: verdict.by });
|
|
269
|
+
// 不等它:它要到内核再次停下才 resolve,后面排着的问题可能就是内核在等的
|
|
270
|
+
void Promise.resolve()
|
|
271
|
+
.then(() => ctx.session.send({ kind: "approval", toolUseId, approved: verdict.approved, ...(verdict.reason ? { reason: verdict.reason } : {}) }))
|
|
272
|
+
.catch(() => void answered.delete(toolUseId));
|
|
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;
|
|
280
|
+
const asked = seen.get(d.toolUseId);
|
|
281
|
+
seen.delete(d.toolUseId);
|
|
282
|
+
if (d.decision !== "ask" || !asked)
|
|
283
|
+
return;
|
|
284
|
+
// 不在这里等回答:observe 要马上返回,内核才能停下来等
|
|
285
|
+
handled.add(d.toolUseId);
|
|
286
|
+
const signal = turn.signal;
|
|
287
|
+
asking = asking.then(() => settle(asked, signal));
|
|
288
|
+
}
|
|
289
|
+
else if (e.type === "input.received") {
|
|
290
|
+
const d = e.data;
|
|
291
|
+
if (d.kind !== "approval")
|
|
292
|
+
return;
|
|
293
|
+
const a = answered.get(d.toolUseId);
|
|
294
|
+
if (!a)
|
|
295
|
+
return;
|
|
296
|
+
answered.delete(d.toolUseId);
|
|
297
|
+
// 内核只记同意或拒绝:选了什么、谁回答的(终端、哪个渠道、规则、超时)记在这里
|
|
298
|
+
await hook.emit("answered", { toolUseId: d.toolUseId, ...a });
|
|
299
|
+
if (a.answer === "always")
|
|
300
|
+
await hook.record(RULES_KEY, merged(hook.derived[RULES_KEY] ?? []));
|
|
301
|
+
}
|
|
302
|
+
else if (e.type === "turn.ended") {
|
|
303
|
+
turn.abort(new Error("the turn ended"));
|
|
304
|
+
turn = new AbortController();
|
|
305
|
+
seen.clear();
|
|
306
|
+
answered.clear();
|
|
307
|
+
handled.clear();
|
|
152
308
|
}
|
|
153
|
-
const turn = asking.then(async () => {
|
|
154
|
-
const again = rules.decide(call, { mode, rules: saved() });
|
|
155
|
-
if (again.decision !== "ask")
|
|
156
|
-
return again;
|
|
157
|
-
const rule = rules.ruleFor(call);
|
|
158
|
-
const shown = (await preview(renderers, call)) ?? detail(call);
|
|
159
|
-
const answer = await ask(ui, {
|
|
160
|
-
title: `Allow ${call.name}?`,
|
|
161
|
-
// 审查转给用户的:先说为什么
|
|
162
|
-
detail: why ? `${why}\n\n${shown}` : shown,
|
|
163
|
-
options: [
|
|
164
|
-
{ label: "Allow once", value: "once" },
|
|
165
|
-
{ label: `Always allow ${describeRule(rule)} in this session`, value: "always" },
|
|
166
|
-
{ label: "Deny", value: "deny" },
|
|
167
|
-
],
|
|
168
|
-
}, hook.signal);
|
|
169
|
-
if (answer === "always") {
|
|
170
|
-
added.push(rule);
|
|
171
|
-
await hook.record(RULES_KEY, saved());
|
|
172
|
-
return { decision: "allow" };
|
|
173
|
-
}
|
|
174
|
-
if (answer === "once")
|
|
175
|
-
return { decision: "allow" };
|
|
176
|
-
return { decision: "deny", reason: "The user denied this tool call. Do not retry it unchanged: adjust, or ask what to do instead." };
|
|
177
|
-
});
|
|
178
|
-
asking = turn.catch(() => { });
|
|
179
|
-
return turn;
|
|
180
309
|
},
|
|
181
310
|
});
|
|
311
|
+
// 恢复一个上次停在等审批的会话(进程被杀、崩溃):那些调用的关卡是上一个进程跑的,这里没见过。
|
|
312
|
+
// 内核恢复时再发一次 awaiting,据此重新问。只知道调用的名字和参数(工具的声明只有关卡拿得到),
|
|
313
|
+
// 所以当作别的关卡要问的:只有"允许一次"和"拒绝",不重新套规则
|
|
314
|
+
ctx.session.subscribe((n) => {
|
|
315
|
+
if (n.type !== "awaiting")
|
|
316
|
+
return;
|
|
317
|
+
const waiting = n.toolUseIds.filter((t) => !handled.has(t) && !seen.has(t));
|
|
318
|
+
if (waiting.length === 0)
|
|
319
|
+
return;
|
|
320
|
+
const batch = ctx.session.state().batch;
|
|
321
|
+
const signal = turn.signal;
|
|
322
|
+
for (const toolUseId of waiting) {
|
|
323
|
+
const c = batch.find((x) => x.toolUseId === toolUseId && x.decision === "ask");
|
|
324
|
+
if (!c)
|
|
325
|
+
continue;
|
|
326
|
+
handled.add(toolUseId);
|
|
327
|
+
asking = asking.then(() => settle({ call: { toolUseId, name: c.name, input: c.input } }, signal));
|
|
328
|
+
}
|
|
329
|
+
});
|
|
182
330
|
},
|
|
183
331
|
});
|
|
184
332
|
/** allowRead、allowWrite:把目录登记进 roots,调用方的作用域关闭时撤销 */
|
|
@@ -198,7 +346,7 @@ function allow(caller, name, roots, dir) {
|
|
|
198
346
|
return remove;
|
|
199
347
|
}
|
|
200
348
|
/**
|
|
201
|
-
*
|
|
349
|
+
* 把问题同时交给终端(能提问时)和所有审批渠道,返回先到的回答和回答的人("terminal" 或渠道的插件 id),然后中止其余的。
|
|
202
350
|
* 终端里取消(undefined)算拒绝;渠道返回 undefined、不认识的值或出错算它不回答。都不回答时返回 undefined(拒绝)
|
|
203
351
|
*/
|
|
204
352
|
async function ask(ui, request, signal) {
|
|
@@ -208,12 +356,20 @@ async function ask(ui, request, signal) {
|
|
|
208
356
|
try {
|
|
209
357
|
return await new Promise((resolve, reject) => {
|
|
210
358
|
const channels = [];
|
|
211
|
-
if (ui?.interactive)
|
|
212
|
-
|
|
213
|
-
|
|
359
|
+
if (ui?.interactive) {
|
|
360
|
+
// 终端里多一项:拒绝并说明。选了它再问一句话,取消或什么都没写就是普通的拒绝
|
|
361
|
+
const options = [...request.options, { label: "Deny, and tell the agent what to do instead", value: EXPLAIN }];
|
|
362
|
+
channels.push(ui.select({ ...request, options }, done.signal).then(async (a) => {
|
|
363
|
+
if (a !== EXPLAIN)
|
|
364
|
+
return { answer: a !== undefined && ANSWERS.includes(a) ? a : "deny", by: "terminal" };
|
|
365
|
+
const said = (await ui.input({ title: "What should the agent do instead?" }, done.signal))?.trim();
|
|
366
|
+
return { answer: "deny", by: "terminal", ...(said ? { said } : {}) };
|
|
367
|
+
}));
|
|
368
|
+
}
|
|
369
|
+
for (const [approver, by] of approvers) {
|
|
214
370
|
channels.push(Promise.resolve()
|
|
215
371
|
.then(() => approver(request, done.signal))
|
|
216
|
-
.then((a) => (typeof a === "string" && ANSWERS.includes(a) ? a : NONE), () => NONE));
|
|
372
|
+
.then((a) => (typeof a === "string" && ANSWERS.includes(a) ? { answer: a, by } : NONE), () => NONE));
|
|
217
373
|
}
|
|
218
374
|
let pending = channels.length;
|
|
219
375
|
if (pending === 0)
|
|
@@ -234,6 +390,32 @@ async function ask(ui, request, signal) {
|
|
|
234
390
|
}
|
|
235
391
|
}
|
|
236
392
|
const NONE = Symbol("no answer");
|
|
393
|
+
/** approvals.json:工作目录 → 在这个目录里总是允许的规则。读不了或格式不对时当作没有 */
|
|
394
|
+
async function readForever(file) {
|
|
395
|
+
try {
|
|
396
|
+
const data = JSON.parse(await readFile(file, "utf8"));
|
|
397
|
+
if (!data || typeof data !== "object" || Array.isArray(data))
|
|
398
|
+
return {};
|
|
399
|
+
const out = {};
|
|
400
|
+
for (const [dir, rules] of Object.entries(data)) {
|
|
401
|
+
if (Array.isArray(rules))
|
|
402
|
+
out[dir] = rules.filter((r) => !!r && typeof r === "object" && typeof r.tool === "string");
|
|
403
|
+
}
|
|
404
|
+
return out;
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
return {};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async function addForever(file, cwd, rule) {
|
|
411
|
+
const all = await readForever(file);
|
|
412
|
+
const rules = all[cwd] ?? [];
|
|
413
|
+
if (!rules.some((r) => JSON.stringify(r) === JSON.stringify(rule)))
|
|
414
|
+
rules.push(rule);
|
|
415
|
+
all[cwd] = rules;
|
|
416
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
417
|
+
await writeFile(file, `${JSON.stringify(all, null, 2)}\n`);
|
|
418
|
+
}
|
|
237
419
|
/**
|
|
238
420
|
* 工具自己登记的审批预览(例如文件修改的 diff)。没有登记、返回的不是文字、或者出错时返回 undefined,
|
|
239
421
|
* 用下面的 detail:预览是别的插件的代码,它坏了不能让审批卡住或失败
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "jade/approval",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.js",
|
|
6
6
|
"provides": [
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
"default": "ask",
|
|
17
17
|
"description": "ask: ask before changes; auto: changes inside the working directory and harmless commands go through; plan: change nothing, present a plan; readonly: change nothing; yolo: never ask"
|
|
18
18
|
},
|
|
19
|
+
"askTimeoutMinutes": {
|
|
20
|
+
"default": 1440,
|
|
21
|
+
"description": "how long to wait for an answer to an approval question before denying the call (at most 10080, one week)"
|
|
22
|
+
},
|
|
19
23
|
"review": {
|
|
20
24
|
"default": true,
|
|
21
25
|
"description": "in auto mode, let the model review the calls the rules cannot decide (it can only allow or pass them to you); false turns the review off"
|
|
@@ -375,14 +375,36 @@ function real(p) {
|
|
|
375
375
|
}
|
|
376
376
|
}
|
|
377
377
|
export function policy(opts) {
|
|
378
|
-
/**
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
378
|
+
/**
|
|
379
|
+
* 作为范围的目录(工作目录、登记的目录、规则里的目录)的真实路径,本会话内记住:复合命令的每个参数都要和它们比,
|
|
380
|
+
* 每次都 realpathSync 是一串同步 IO。只记已经存在的;要判断的路径本身每次都重新解析,符号链接可能随时被换掉
|
|
381
|
+
*/
|
|
382
|
+
const bases = new Map();
|
|
383
|
+
const realBase = (dir) => {
|
|
384
|
+
let r = bases.get(dir);
|
|
385
|
+
if (r !== undefined)
|
|
386
|
+
return r;
|
|
387
|
+
try {
|
|
388
|
+
r = realpathSync(dir);
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
return real(dir);
|
|
392
|
+
}
|
|
393
|
+
bases.set(dir, r);
|
|
394
|
+
return r;
|
|
395
|
+
};
|
|
396
|
+
/** 真实路径 target 在真实路径 base 里 */
|
|
397
|
+
const contains = (base, target) => {
|
|
398
|
+
const rel = path.relative(base, target);
|
|
382
399
|
// 只有 ".." 这一段才表示上级目录,名为 "..foo" 的目录仍在里面
|
|
383
400
|
return rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
|
|
384
401
|
};
|
|
385
|
-
|
|
402
|
+
/** p 在 base 目录里。按真实路径比较:目录里指向外面的符号链接也算外面 */
|
|
403
|
+
const within = (base, p) => contains(realBase(base), real(path.resolve(opts.cwd, p)));
|
|
404
|
+
const inside = (p) => {
|
|
405
|
+
const target = real(path.resolve(opts.cwd, p));
|
|
406
|
+
return contains(realBase(opts.cwd), target) || (opts.writeRoots ?? []).some((r) => contains(realBase(r), target));
|
|
407
|
+
};
|
|
386
408
|
/** glob 的 pattern 是绝对路径或含 ".." 段时,匹配范围会超出 path */
|
|
387
409
|
const globEscapes = (pattern) => path.isAbsolute(pattern) || pattern.split(/[\\/]/).includes("..");
|
|
388
410
|
/** 以 "." 或 "[" 开头的通配段可能匹配到 ".."(取决于 bash 版本和选项),按越界处理 */
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 每个问题给几个选项让用户选,也可以自己输入回答。对话框经 ui@1,不再由宿主私下传入。
|
|
4
4
|
// 前端不能提问(-p、非终端)时不提供这个工具,提示词里也就不会提到它(jade/prompts 按工具是否存在取舍)。
|
|
5
5
|
// 从旧 jade 的 tools/ask.ts 和 main.ts 的 askUser 移植。
|
|
6
|
-
import { definePlugin } from "
|
|
6
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
7
7
|
const MAX_QUESTIONS = 4;
|
|
8
8
|
/** 选项的值是序号,"其他" 的值不会和它重复 */
|
|
9
9
|
const OTHER = "other";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade/budget:一轮里模型回复的次数达到上限就停下(turn.continue 关卡拒绝,这一轮以 stopped 结束)。
|
|
2
2
|
// 从旧 jade 的 plugins/budget.ts 移植;上限来自配置 maxSteps。
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
4
|
const DEFAULT_STEPS = 100;
|
|
5
5
|
/** 本轮(最近一条用户输入之后)模型已经回复了几次 */
|
|
6
6
|
export function stepsThisTurn(s) {
|
|
@@ -75,7 +75,8 @@ function findUp(dir, root, name) {
|
|
|
75
75
|
* 后面认不出路径的行(多行说明、代码片段)归到它下面;第一个问题之前的行单独算一个没有文件的问题
|
|
76
76
|
*/
|
|
77
77
|
export function parseProblems(output, cwd) {
|
|
78
|
-
|
|
78
|
+
// 路径里可以有括号(Next.js 的 app/(group)/page.tsx),只是不能以括号开头
|
|
79
|
+
const START = /^\s*((?:[A-Za-z]:[\\/])?[^\s:()][^:]*?)(?:\(\d+,\d+\)|:\d+(?::\d+)?)(?=[:\s]|$)/;
|
|
79
80
|
const out = [];
|
|
80
81
|
let cur;
|
|
81
82
|
for (const line of output.split(/\r?\n/)) {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// yolo 模式不问;不能提问时不运行没确认过的命令。
|
|
10
10
|
import { createHash } from "node:crypto";
|
|
11
11
|
import path from "node:path";
|
|
12
|
-
import { definePlugin } from "
|
|
12
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
13
13
|
import { changedFiles, checks } from "./checks.js";
|
|
14
14
|
import { bold, dim, display, realPath, repoRoot, TrustFile, yellow } from "./project.js";
|
|
15
15
|
/** 确认记录:同一个目录里的同一条命令只问一次。和旧 jade 的算法相同 */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade/commands:斜杠命令的注册表。只登记,不执行:前端解析输入、带着自己的上下文调用 run。
|
|
2
2
|
// 每个调用方一个实例,调用方关闭(停用、被隔离、换会话时的会话作用域)时它注册的命令自动撤销。
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
4
|
const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
5
5
|
const registry = new Map();
|
|
6
6
|
export default definePlugin({
|
|
@@ -36,10 +36,16 @@ function mediaTokens(b) {
|
|
|
36
36
|
const bytes = typeof data === "string" ? (data.length * 3) / 4 : 0;
|
|
37
37
|
return Math.max(IMAGE_TOKENS, Math.ceil(bytes / PDF_BYTES_PER_TOKEN));
|
|
38
38
|
}
|
|
39
|
+
/** 块上不发给模型的字段:来源、信任标记,以及 provider 原样保存的原始块(ext,text 和 thinking 会被算两遍) */
|
|
40
|
+
const UNSENT = new Set(["origin", "trust", "ext"]);
|
|
39
41
|
/** 粗略估算:英文和代码 4 个字符约 1 个 token,中日韩文字每个字约 1 个 token,图片和 PDF 单独估算 */
|
|
40
42
|
export function estimateTokens(messages) {
|
|
41
43
|
let media = 0;
|
|
42
|
-
const text = JSON.stringify(messages, (
|
|
44
|
+
const text = JSON.stringify(messages, function (key, value) {
|
|
45
|
+
// 只去掉块自己的这些字段:工具参数里同名的键照常计入。opaque 块的内容全在 ext 里,要算
|
|
46
|
+
const holder = this;
|
|
47
|
+
if (UNSENT.has(key) && typeof holder?.kind === "string" && holder.kind !== "opaque")
|
|
48
|
+
return undefined;
|
|
43
49
|
const b = value;
|
|
44
50
|
if (typeof b === "object" && b !== null && (b.kind === "image" || b.kind === "document")) {
|
|
45
51
|
media += mediaTokens(b);
|
|
@@ -77,6 +83,7 @@ const total = (u) => (u?.inputTokens === undefined ? undefined : u.inputTokens +
|
|
|
77
83
|
export function compactor(opts) {
|
|
78
84
|
const id = opts.id ?? "jade/compact";
|
|
79
85
|
let forced = false;
|
|
86
|
+
let focus;
|
|
80
87
|
/** 最近一次投影:它对应的消息条数和压缩状态;收到回复后补上真实用量 */
|
|
81
88
|
let last;
|
|
82
89
|
let pending;
|
|
@@ -137,6 +144,8 @@ export function compactor(opts) {
|
|
|
137
144
|
}
|
|
138
145
|
}
|
|
139
146
|
forced = false;
|
|
147
|
+
const asked = focus;
|
|
148
|
+
focus = undefined;
|
|
140
149
|
const res = await opts.complete({
|
|
141
150
|
system: [{ text: SUMMARY_SYSTEM, stability: "stable", origin: { by: "plugin", id }, trust: "trusted" }],
|
|
142
151
|
messages: [
|
|
@@ -150,6 +159,7 @@ export function compactor(opts) {
|
|
|
150
159
|
origin: { by: "plugin", id },
|
|
151
160
|
trust: "untrusted",
|
|
152
161
|
},
|
|
162
|
+
...(asked ? [{ kind: "text", text: `The user asked that the summary pay particular attention to: ${asked}`, origin: { by: "plugin", id }, trust: "trusted" }] : []),
|
|
153
163
|
],
|
|
154
164
|
},
|
|
155
165
|
],
|
|
@@ -179,8 +189,9 @@ export function compactor(opts) {
|
|
|
179
189
|
last = { ...pending, tokens: total(e.data.usage) };
|
|
180
190
|
pending = undefined;
|
|
181
191
|
},
|
|
182
|
-
request() {
|
|
192
|
+
request(what) {
|
|
183
193
|
forced = true;
|
|
194
|
+
focus = what;
|
|
184
195
|
},
|
|
185
196
|
};
|
|
186
197
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade/compact:历史接近上下文窗口时压缩投影(见 compact.ts);/compact 让下一次请求前总结一次;
|
|
2
2
|
// 请求因为超出上下文窗口失败时,压缩后用 { kind: "retry" } 重试一次(session.afterTurn,设计 4.3)。
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
4
|
import { compactor, compactThreshold, isContextOverflow, parseTokens } from "./compact.js";
|
|
5
5
|
import { dim } from "./style.js";
|
|
6
6
|
/** 认不出窗口时的阈值 */
|
|
@@ -22,20 +22,21 @@ function thresholdFor(config, models) {
|
|
|
22
22
|
return compactThreshold(m.contextWindow, m.kind === "anthropic" ? ANTHROPIC_REPLY_TOKENS : 0);
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
-
/** /compact
|
|
26
|
-
const requested = new
|
|
25
|
+
/** /compact 请求过的会话:下一次投影时无论是否超过阈值都总结一次。值是命令后面写的、要总结特别留意的事 */
|
|
26
|
+
const requested = new Map();
|
|
27
27
|
export default definePlugin({
|
|
28
28
|
process(ctx) {
|
|
29
29
|
ctx.provide("compact@1", { threshold: thresholdFor(ctx.config, ctx.require("models@1")) });
|
|
30
30
|
const commands = ctx.service("commands@1");
|
|
31
31
|
commands?.register({
|
|
32
32
|
name: "compact",
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
args: "[what to keep]",
|
|
34
|
+
description: "compact the history before the next request; text after the command tells the summary what to pay attention to",
|
|
35
|
+
run(args, c) {
|
|
35
36
|
const s = c.sessions.current;
|
|
36
37
|
if (!s)
|
|
37
38
|
return "No session is open.";
|
|
38
|
-
requested.
|
|
39
|
+
requested.set(s.id, args.trim());
|
|
39
40
|
return dim("History will be compacted before the next request.");
|
|
40
41
|
},
|
|
41
42
|
});
|
|
@@ -65,8 +66,11 @@ export default definePlugin({
|
|
|
65
66
|
order: 100,
|
|
66
67
|
timeoutMs: SUMMARY_TIMEOUT_MS,
|
|
67
68
|
fn: (messages, hook) => {
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
const focus = requested.get(id);
|
|
70
|
+
if (focus !== undefined) {
|
|
71
|
+
requested.delete(id);
|
|
72
|
+
c.request(focus || undefined);
|
|
73
|
+
}
|
|
70
74
|
return c.project(messages, hook);
|
|
71
75
|
},
|
|
72
76
|
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// 每次用户输入之后最多续写 maxContinues 次。从旧 jade 的 plugins/turns.ts 的 continueTurns 移植。
|
|
5
5
|
//
|
|
6
6
|
// 旧 jade 的"上下文溢出后压缩重试"不在这里:不压缩就重试只会再溢出一次,整件事归 jade/compact。
|
|
7
|
-
import { definePlugin } from "
|
|
7
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
8
|
export const DEFAULT_CONTINUES = 3;
|
|
9
9
|
export const CONTINUE_NOTE = "Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " +
|
|
10
10
|
"If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
|