@uncarved/jade 0.3.1 → 0.4.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/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +10 -9
- package/plugins/models/index.ts +27 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +20 -25
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +243 -307
- package/plugins-dist/models/saved.js +59 -71
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +81 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +115 -135
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +489 -34
|
@@ -1,58 +1,47 @@
|
|
|
1
|
-
// 跨运行时的进程:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
1
|
import { spawn as nodeSpawn } from "node:child_process";
|
|
3
|
-
const EMPTY = { async
|
|
4
|
-
/**
|
|
5
|
-
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
6
|
-
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
7
|
-
*/
|
|
2
|
+
const EMPTY = { async* [Symbol.asyncIterator]() {} };
|
|
8
3
|
export function spawn(cmd, opts = {}) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
kill: () => void child.kill(),
|
|
30
|
-
});
|
|
31
|
-
});
|
|
4
|
+
const child = nodeSpawn(cmd[0], cmd.slice(1), {
|
|
5
|
+
cwd: opts.cwd,
|
|
6
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
7
|
+
detached: opts.detached ?? false,
|
|
8
|
+
env: opts.env,
|
|
9
|
+
windowsHide: true
|
|
10
|
+
});
|
|
11
|
+
const exited = new Promise((resolve) => child.once("exit", (code) => resolve(code)));
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
child.once("error", reject);
|
|
14
|
+
child.once("spawn", () => {
|
|
15
|
+
child.off("error", reject);
|
|
16
|
+
child.on("error", () => {});
|
|
17
|
+
resolve({
|
|
18
|
+
pid: child.pid,
|
|
19
|
+
stdout: child.stdout ?? EMPTY,
|
|
20
|
+
stderr: child.stderr ?? EMPTY,
|
|
21
|
+
exited,
|
|
22
|
+
kill: () => void child.kill()
|
|
23
|
+
});
|
|
32
24
|
});
|
|
25
|
+
});
|
|
33
26
|
}
|
|
34
|
-
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject。超过 timeoutMs 时杀掉,退出码为 null */
|
|
35
27
|
export async function run(cmd, opts = {}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
finally {
|
|
49
|
-
clearTimeout(timer);
|
|
50
|
-
}
|
|
28
|
+
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
29
|
+
let timedOut = false;
|
|
30
|
+
const timer = opts.timeoutMs === undefined ? undefined : setTimeout(() => {
|
|
31
|
+
timedOut = true;
|
|
32
|
+
proc.kill();
|
|
33
|
+
}, opts.timeoutMs);
|
|
34
|
+
try {
|
|
35
|
+
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
36
|
+
return { code: timedOut ? null : code, stdout };
|
|
37
|
+
} finally {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
}
|
|
51
40
|
}
|
|
52
41
|
export async function text(stream) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
42
|
+
const decoder = new TextDecoder;
|
|
43
|
+
let out = "";
|
|
44
|
+
for await (const chunk of stream)
|
|
45
|
+
out += decoder.decode(chunk, { stream: true });
|
|
46
|
+
return out + decoder.decode();
|
|
58
47
|
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
// jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
|
|
2
|
-
// 显示的文字可以带 ANSI 颜色,前端负责过滤掉颜色之外的控制序列。
|
|
3
1
|
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
-
const tools = new Map
|
|
5
|
-
const events = new Map
|
|
6
|
-
function add(map, key, renderer, plugin, what, onDispose) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
const tools = new Map;
|
|
3
|
+
const events = new Map;
|
|
4
|
+
function add(map, key, renderer, plugin, what, onDispose, report) {
|
|
5
|
+
const taken = map.get(key);
|
|
6
|
+
if (taken) {
|
|
7
|
+
report(`the renderer for ${what} ${key} from ${plugin} was skipped: ${taken.plugin} already registered one`);
|
|
8
|
+
return () => {};
|
|
9
|
+
}
|
|
10
|
+
const entry = { renderer, plugin };
|
|
11
|
+
map.set(key, entry);
|
|
12
|
+
const revoke = () => {
|
|
13
|
+
if (map.get(key) === entry)
|
|
14
|
+
map.delete(key);
|
|
15
|
+
};
|
|
16
|
+
onDispose(revoke);
|
|
17
|
+
return revoke;
|
|
18
18
|
}
|
|
19
19
|
export default definePlugin({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
20
|
+
process(ctx) {
|
|
21
|
+
ctx.provide("renderers@1", (caller) => ({
|
|
22
|
+
tool(renderer) {
|
|
23
|
+
if (typeof renderer?.tool !== "string" || !renderer.tool)
|
|
24
|
+
throw new Error("a tool renderer needs the tool name");
|
|
25
|
+
return add(tools, renderer.tool, renderer, caller.plugin, "tool", caller.onDispose, ctx.report);
|
|
26
|
+
},
|
|
27
|
+
event(renderer) {
|
|
28
|
+
if (typeof renderer?.event !== "string" || !renderer.event || typeof renderer.render !== "function")
|
|
29
|
+
throw new Error("an event renderer needs the event type and render");
|
|
30
|
+
return add(events, `${caller.plugin}/${renderer.event}`, renderer, caller.plugin, "event", caller.onDispose, ctx.report);
|
|
31
|
+
},
|
|
32
|
+
forTool: (tool) => tools.get(tool),
|
|
33
|
+
forEvent: (type) => events.get(type)
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
36
|
});
|
|
@@ -1,191 +1,148 @@
|
|
|
1
|
-
// jade/repeats:模型常常把同一个调用原样再发一遍。
|
|
2
|
-
//
|
|
3
|
-
// 同一次回复里参数完全相同的调用直接拒绝,不用让用户再批准一次;
|
|
4
|
-
// 同一个调用连续失败、其间什么都没变时,在结果后面提醒模型它已经失败过几次,别再原样重试
|
|
5
|
-
// (改了文件再跑同一条测试命令是正常的"修了再验证",从头计数);
|
|
6
|
-
// 同一个调用已经成功执行过,之后什么都没变(没有执行过只读之外的调用,用户也没有发新消息),
|
|
7
|
-
// 再执行一遍结果也完全一样时,在结果后面提醒模型这是重复的(反复读同一个文件、反复打开同一个网页)。
|
|
8
|
-
// 不拒绝:也可能是在等外面的东西变化,只提醒。压缩插件清理或总结了历史后,之前的结果可能已经不在上下文里,都不算重复。
|
|
9
|
-
// 打开文件给用户看(start、xdg-open……)会弹出新窗口:这一轮里打开过、之后什么都没改,再打开就直接拒绝。
|
|
10
|
-
// write_file 之后什么都没改又读回同一个文件,提醒模型内容就是它写的那些。
|
|
11
|
-
// 同一个文件里同一处文字被来回改(A 改成 B、B 改回 A、又改成 B):模型在凭不可靠的依据猜,第三次起提醒它停下来换个办法查,或者问用户。
|
|
12
|
-
// 这些记录只在本会话的作用域里:恢复会话后从零开始,最多多重试一轮。
|
|
13
|
-
//
|
|
14
|
-
// 从旧 jade 的 plugins/repeats.ts 移植。
|
|
15
1
|
import { createHash } from "node:crypto";
|
|
16
2
|
import path from "node:path";
|
|
17
3
|
import { definePlugin } from "@uncarved/jade-api";
|
|
18
4
|
const ID = "jade/repeats";
|
|
19
|
-
/** jade/compact 记录压缩结果用的派生数据键(旧 jade 的约定):记一次就说明历史被压缩过 */
|
|
20
5
|
export const COMPACT_KEY = "jade/compact";
|
|
21
|
-
/** 只打开一个文件或网址的命令:start "" x、start x、xdg-open x、open x、explorer x */
|
|
22
6
|
const OPEN = /^\s*(?:start\s+""|start|xdg-open|open|explorer(?:\.exe)?)\s+(?:"([^"]+)"|'([^']+)'|([^\s"'&|;<>]+))\s*$/i;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return /^[a-z][a-z0-9+.-]*:\/\//i.test(target) ? target : normalize(cwd, target);
|
|
7
|
+
export function openTarget(call, cwd, shell) {
|
|
8
|
+
if (shell === undefined)
|
|
9
|
+
return;
|
|
10
|
+
const command = call.input?.[shell];
|
|
11
|
+
if (typeof command !== "string")
|
|
12
|
+
return;
|
|
13
|
+
const m = OPEN.exec(command);
|
|
14
|
+
const target = m && (m[1] ?? m[2] ?? m[3]);
|
|
15
|
+
if (!target)
|
|
16
|
+
return;
|
|
17
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(target) ? target : normalize(cwd, target);
|
|
35
18
|
}
|
|
36
19
|
function normalize(cwd, p) {
|
|
37
|
-
|
|
38
|
-
|
|
20
|
+
const full = path.resolve(cwd, p);
|
|
21
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
39
22
|
}
|
|
40
|
-
/** 提醒模型的失败次数 */
|
|
41
23
|
const WARN_AFTER = 2;
|
|
42
|
-
/** 同一处文字来回改到第几次时提醒:改过去、改回来各算一次,改回来一次是正常的撤销 */
|
|
43
24
|
const FLIP_AFTER = 3;
|
|
44
|
-
/** edit_file、multi_edit 里的每一处替换 */
|
|
45
25
|
function replacements(call) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
26
|
+
const input = call.input;
|
|
27
|
+
const list = call.name === "edit_file" ? [input] : call.name === "multi_edit" && Array.isArray(input?.edits) ? input.edits : [];
|
|
28
|
+
return list.filter((e) => typeof e?.old_string === "string" && typeof e?.new_string === "string");
|
|
49
29
|
}
|
|
50
|
-
const hash = (content) => createHash("sha1").update(JSON.stringify(content.map((b) =>
|
|
51
|
-
/** 会话里的 hook:model.before 数回复次数,tool.gate 拒绝重复,tool.after 加提醒,event 在用户发消息、压缩后重新计 */
|
|
30
|
+
const hash = (content) => createHash("sha1").update(JSON.stringify(content.map((b) => b.kind === "text" ? b.text : b))).digest("hex");
|
|
52
31
|
export function repeatsHooks(opts) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return;
|
|
164
|
-
const text = `This exact call has now failed ${n} times. Do not repeat it unchanged: change the approach, or tell the user what is blocking you.`;
|
|
165
|
-
return { ...outcome, content: [...outcome.content, note(text)] };
|
|
166
|
-
},
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
kind: "observe",
|
|
170
|
-
point: "event",
|
|
171
|
-
// 用户发了新消息,可能就是要再来一遍("再打开看看"),之前的记录都不算了;
|
|
172
|
-
// 压缩之后模型可能已经看不到之前的结果,重新执行不算重复
|
|
173
|
-
fn: (e) => {
|
|
174
|
-
if (e.type === "derived.recorded" && e.data.key === COMPACT_KEY)
|
|
175
|
-
version++;
|
|
176
|
-
if (e.type === "input.received" && e.data.kind === "message") {
|
|
177
|
-
version++;
|
|
178
|
-
opened.clear();
|
|
179
|
-
written.clear();
|
|
180
|
-
flips.clear();
|
|
181
|
-
}
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
];
|
|
32
|
+
const id = opts.id ?? ID;
|
|
33
|
+
const cwd = opts.cwd;
|
|
34
|
+
let calls = 0;
|
|
35
|
+
let current = { calls: -1, keys: new Set, mutates: false };
|
|
36
|
+
const failures = new Map;
|
|
37
|
+
let version = 0;
|
|
38
|
+
const succeeded = new Map;
|
|
39
|
+
const readOnly = new Map;
|
|
40
|
+
let changes = 0;
|
|
41
|
+
const opened = new Map;
|
|
42
|
+
const written = new Map;
|
|
43
|
+
const flips = new Map;
|
|
44
|
+
const key = (call) => `${call.name}\x00${JSON.stringify(call.input)}`;
|
|
45
|
+
const note = (text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id }, trust: "trusted" });
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
kind: "transform",
|
|
49
|
+
point: "model.before",
|
|
50
|
+
fn: () => {
|
|
51
|
+
calls++;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
kind: "gate",
|
|
56
|
+
point: "tool.gate",
|
|
57
|
+
fn: (call) => {
|
|
58
|
+
if (current.calls !== calls)
|
|
59
|
+
current = { calls, keys: new Set, mutates: false };
|
|
60
|
+
const k = key(call);
|
|
61
|
+
if (current.keys.has(k)) {
|
|
62
|
+
return { decision: "deny", reason: "This call is identical to an earlier call in the same response and was not run; use that call's result." };
|
|
63
|
+
}
|
|
64
|
+
current.keys.add(k);
|
|
65
|
+
const target = openTarget(call, cwd, opts.shell(call.name));
|
|
66
|
+
if (target !== undefined && opened.get(target) === changes && !current.mutates) {
|
|
67
|
+
return {
|
|
68
|
+
decision: "deny",
|
|
69
|
+
reason: "This was already opened for the user earlier in this turn and nothing has changed since, so it was not opened again (it is still open). " + "End your turn and tell the user what you changed."
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (target === undefined && call.tool.effect !== "read")
|
|
73
|
+
current.mutates = true;
|
|
74
|
+
readOnly.set(call.toolUseId, call.tool.effect === "read");
|
|
75
|
+
return { decision: "abstain" };
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
kind: "transform",
|
|
80
|
+
point: "tool.after",
|
|
81
|
+
fn: (outcome) => {
|
|
82
|
+
const k = key(outcome.call);
|
|
83
|
+
const read = readOnly.get(outcome.call.toolUseId) ?? false;
|
|
84
|
+
readOnly.delete(outcome.call.toolUseId);
|
|
85
|
+
if (!outcome.isError) {
|
|
86
|
+
failures.delete(k);
|
|
87
|
+
const target = openTarget(outcome.call, cwd, opts.shell(outcome.call.name));
|
|
88
|
+
if (target !== undefined)
|
|
89
|
+
opened.set(target, changes);
|
|
90
|
+
else if (!read)
|
|
91
|
+
changes++;
|
|
92
|
+
const file = outcome.call.input?.path;
|
|
93
|
+
if (outcome.call.name === "write_file" && typeof file === "string")
|
|
94
|
+
written.set(normalize(cwd, file), changes);
|
|
95
|
+
const readBack = outcome.call.name === "read_file" && typeof file === "string" && written.get(normalize(cwd, file)) === changes ? note("You wrote this file with write_file earlier and nothing has changed since: its content is exactly what you gave. Do not read files back to check them.") : undefined;
|
|
96
|
+
let most = 0;
|
|
97
|
+
if (typeof file === "string") {
|
|
98
|
+
for (const e of replacements(outcome.call)) {
|
|
99
|
+
const pair = `${normalize(cwd, file)}\x00${[e.old_string, e.new_string].sort().join("\x00")}`;
|
|
100
|
+
const n = (flips.get(pair) ?? 0) + 1;
|
|
101
|
+
flips.set(pair, n);
|
|
102
|
+
most = Math.max(most, n);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const flipped = most >= FLIP_AFTER ? note(`You have now switched this same text back and forth ${most} times in this file. Going back and forth means what you are judging by is unreliable or incomplete. ` + "Do not flip it again: check your assumption another way (measure it, read the code involved), or tell the user what you see and ask.") : undefined;
|
|
106
|
+
const extra = [readBack, flipped].filter((b) => b !== undefined);
|
|
107
|
+
const before = succeeded.get(k);
|
|
108
|
+
if (!read)
|
|
109
|
+
version++;
|
|
110
|
+
const result = hash(outcome.content);
|
|
111
|
+
const repeated = before !== undefined && before.version === (read ? version : version - 1) && before.result === result;
|
|
112
|
+
succeeded.set(k, { version, result });
|
|
113
|
+
if (!repeated)
|
|
114
|
+
return extra.length > 0 ? { ...outcome, content: [...outcome.content, ...extra] } : undefined;
|
|
115
|
+
const text = "This exact call already ran earlier with the same result, and nothing has changed since (no edits, no other commands, no new message from the user). " + "Do not repeat it: use what you already have, or end your turn and tell the user where things stand.";
|
|
116
|
+
return { ...outcome, content: [...outcome.content, note(text)] };
|
|
117
|
+
}
|
|
118
|
+
const prev = failures.get(k);
|
|
119
|
+
const n = prev && prev.version === version ? prev.n + 1 : 1;
|
|
120
|
+
failures.set(k, { n, version });
|
|
121
|
+
if (n < WARN_AFTER)
|
|
122
|
+
return;
|
|
123
|
+
const text = `This exact call has now failed ${n} times. Do not repeat it unchanged: change the approach, or tell the user what is blocking you.`;
|
|
124
|
+
return { ...outcome, content: [...outcome.content, note(text)] };
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
kind: "observe",
|
|
129
|
+
point: "event",
|
|
130
|
+
fn: (e) => {
|
|
131
|
+
if (e.type === "derived.recorded" && e.data.key === COMPACT_KEY)
|
|
132
|
+
version++;
|
|
133
|
+
if (e.type === "input.received" && e.data.kind === "message") {
|
|
134
|
+
version++;
|
|
135
|
+
opened.clear();
|
|
136
|
+
written.clear();
|
|
137
|
+
flips.clear();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
];
|
|
185
142
|
}
|
|
186
143
|
export default definePlugin({
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
144
|
+
session(ctx) {
|
|
145
|
+
for (const h of repeatsHooks({ cwd: ctx.cwd, id: ctx.plugin, shell: (t) => ctx.session.tool(t)?.meta?.shell }))
|
|
146
|
+
ctx.kernel.hook(h);
|
|
147
|
+
}
|
|
191
148
|
});
|
|
@@ -1,67 +1,60 @@
|
|
|
1
|
-
// 五字段 cron(分 时 日 月 星期),按本机时区计算。支持 *、数字、a-b、/步长和逗号列表;
|
|
2
|
-
// 星期 0 和 7 都是周日。日和星期都受限时任一匹配即可(Vixie cron 的规则),有一个以 * 开头时两者都要匹配。
|
|
3
1
|
const FIELDS = [
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
["minute", 0, 59],
|
|
3
|
+
["hour", 0, 23],
|
|
4
|
+
["day of month", 1, 31],
|
|
5
|
+
["month", 1, 12],
|
|
6
|
+
["day of week", 0, 7]
|
|
9
7
|
];
|
|
10
8
|
function field(text, [name, min, max]) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
const out = new Set;
|
|
10
|
+
for (const item of text.split(",")) {
|
|
11
|
+
const m = /^(\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(item);
|
|
12
|
+
if (!m)
|
|
13
|
+
throw new Error(`invalid ${name} field "${text}"`);
|
|
14
|
+
const lo = m[1] === "*" ? min : Number(m[2]);
|
|
15
|
+
const hi = m[1] === "*" ? max : m[3] !== undefined ? Number(m[3]) : m[4] !== undefined ? max : lo;
|
|
16
|
+
const step = m[4] !== undefined ? Number(m[4]) : 1;
|
|
17
|
+
if (lo < min || hi > max || lo > hi || step < 1)
|
|
18
|
+
throw new Error(`${name} field "${text}" is out of range ${min}-${max}`);
|
|
19
|
+
for (let v = lo;v <= hi; v += step)
|
|
20
|
+
out.add(v);
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
25
23
|
}
|
|
26
24
|
export function parseCron(expr) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
const parts = expr.trim().split(/\s+/);
|
|
26
|
+
if (parts.length !== 5)
|
|
27
|
+
throw new Error(`cron needs 5 fields (minute hour day-of-month month day-of-week), got "${expr}"`);
|
|
28
|
+
const [minute, hour, dom, month, dow] = parts.map((p, i) => field(p, FIELDS[i]));
|
|
29
|
+
if (dow.delete(7))
|
|
30
|
+
dow.add(0);
|
|
31
|
+
return { minute, hour, dom, month, dow, domStar: parts[2].startsWith("*"), dowStar: parts[4].startsWith("*") };
|
|
34
32
|
}
|
|
35
33
|
function dayMatches(c, d) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const dom = c.dom.has(d.getDate());
|
|
35
|
+
const dow = c.dow.has(d.getDay());
|
|
36
|
+
return c.domStar || c.dowStar ? dom && dow : dom || dow;
|
|
39
37
|
}
|
|
40
|
-
/** from 之后(不含)第一个匹配的整分钟;五年内没有(例如 2 月 30 日)时 undefined */
|
|
41
38
|
export function nextRun(expr, from) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
d.setMinutes(d.getMinutes() + 1, 0, 0);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return d;
|
|
64
|
-
}
|
|
39
|
+
const c = typeof expr === "string" ? parseCron(expr) : expr;
|
|
40
|
+
const d = new Date(from);
|
|
41
|
+
d.setSeconds(0, 0);
|
|
42
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
43
|
+
const limit = from.getTime() + 5 * 366 * 86400000;
|
|
44
|
+
while (d.getTime() < limit) {
|
|
45
|
+
if (!c.month.has(d.getMonth() + 1)) {
|
|
46
|
+
d.setMonth(d.getMonth() + 1, 1);
|
|
47
|
+
d.setHours(0, 0, 0, 0);
|
|
48
|
+
} else if (!dayMatches(c, d)) {
|
|
49
|
+
d.setDate(d.getDate() + 1);
|
|
50
|
+
d.setHours(0, 0, 0, 0);
|
|
51
|
+
} else if (!c.hour.has(d.getHours())) {
|
|
52
|
+
d.setHours(d.getHours() + 1, 0, 0, 0);
|
|
53
|
+
} else if (!c.minute.has(d.getMinutes())) {
|
|
54
|
+
d.setMinutes(d.getMinutes() + 1, 0, 0);
|
|
55
|
+
} else {
|
|
56
|
+
return d;
|
|
65
57
|
}
|
|
66
|
-
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
67
60
|
}
|