@uncarved/jade 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -150
- package/bin/jade.js +14 -25
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -30
- package/dist/index.js +11 -30
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +160 -1362
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +2 -0
- package/dist/update.js +6 -3
- package/dist/update.js.map +1 -1
- package/package.json +10 -8
- package/plugins/approval/index.ts +311 -0
- package/plugins/approval/plugin.json +22 -0
- package/{src/plugins → plugins/approval}/policy.ts +45 -104
- package/{src/plugins → plugins/approval}/review.ts +62 -16
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +48 -38
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +80 -39
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +2 -2
- package/{src/tools → plugins/fs}/fs.ts +89 -46
- package/{src/tools → plugins/fs}/grep.ts +36 -8
- package/plugins/fs/index.ts +37 -0
- package/plugins/fs/plugin.json +7 -0
- package/{src/ui → plugins/fs}/renderers.ts +25 -70
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +99 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +213 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +16 -119
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +160 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +111 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +258 -0
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +84 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +31 -20
- package/{src → plugins/shell}/sys.ts +2 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +168 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +343 -0
- package/plugins/todo/index.ts +93 -0
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +3 -0
- package/plugins/tui/frontend.ts +619 -0
- package/plugins/tui/index.ts +37 -0
- package/plugins/tui/mentions.ts +153 -0
- package/plugins/tui/plugin.json +10 -0
- package/{src/ui → plugins/tui}/render.ts +8 -2
- package/{src/ui → plugins/tui}/screen.ts +8 -3
- package/{src/ui → plugins/tui}/transcript.ts +79 -100
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +360 -392
- package/plugins/tui/vendor.ts +16 -0
- package/{src/tools → plugins/undo}/checkpoint.ts +45 -60
- package/plugins/undo/index.ts +143 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/{src/tools → plugins/web}/web.ts +64 -31
- package/plugins-dist/approval/index.js +287 -0
- package/plugins-dist/approval/plugin.json +28 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +41 -85
- package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +58 -34
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
- package/{dist/tools → plugins-dist/fs}/fs.js +82 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +36 -11
- package/plugins-dist/fs/index.js +39 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +87 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +181 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +18 -81
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +172 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +85 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +231 -0
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +58 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
- package/{dist → plugins-dist/shell}/sys.js +2 -43
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +152 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +284 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +31 -9
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
- package/plugins-dist/tui/frontend.js +609 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
- package/plugins-dist/tui/mentions.js +153 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/{dist/ui → plugins-dist/tui}/render.js +7 -3
- package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
- package/{dist/ui → plugins-dist/tui}/transcript.js +79 -99
- package/{dist/ui → plugins-dist/tui}/tui.js +185 -54
- package/plugins-dist/tui/vendor.js +13822 -0
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +48 -55
- package/plugins-dist/undo/index.js +143 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/{dist/tools → plugins-dist/web}/web.js +56 -29
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -30
- package/src/install.ts +355 -0
- package/src/main.ts +156 -1387
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -262
- package/LICENSE +0 -21
- package/dist/commands.d.ts +0 -50
- package/dist/commands.js +0 -126
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -141
- package/dist/distro.js +0 -462
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -294
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -25
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -71
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -47
- package/dist/plugins/core.js +0 -229
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/host.d.ts +0 -52
- package/dist/plugins/host.js +0 -115
- package/dist/plugins/host.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -410
- package/dist/plugins/loader.js +0 -407
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -98
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -43
- package/dist/plugins/prompts.js +0 -284
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -16
- package/dist/plugins/repeats.js +0 -151
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -18
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/plugins/sessions.d.ts +0 -7
- package/dist/plugins/sessions.js +0 -110
- package/dist/plugins/sessions.js.map +0 -1
- package/dist/provider.d.ts +0 -165
- package/dist/provider.js.map +0 -1
- package/dist/session-lifecycle.d.ts +0 -61
- package/dist/session-lifecycle.js +0 -167
- package/dist/session-lifecycle.js.map +0 -1
- package/dist/session-scope.d.ts +0 -53
- package/dist/session-scope.js +0 -116
- package/dist/session-scope.js.map +0 -1
- package/dist/sessions.d.ts +0 -28
- package/dist/sessions.js +0 -73
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -51
- package/dist/sys.js.map +0 -1
- package/dist/tools/ask.d.ts +0 -11
- package/dist/tools/ask.js +0 -62
- package/dist/tools/ask.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -39
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -39
- package/dist/tools/task.js +0 -117
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -53
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -447
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -41
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -22
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -14
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/renderers.d.ts +0 -8
- package/dist/ui/renderers.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -118
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -46
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -47
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -144
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -577
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -278
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -34
- package/src/plugins/core.ts +0 -257
- package/src/plugins/host.ts +0 -146
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -742
- package/src/plugins/prompts.ts +0 -327
- package/src/plugins/repeats.ts +0 -157
- package/src/plugins/sessions.ts +0 -109
- package/src/session-lifecycle.ts +0 -194
- package/src/session-scope.ts +0 -135
- package/src/sessions.ts +0 -78
- package/src/tools/ask.ts +0 -72
- package/src/tools/task.ts +0 -149
- package/src/tools/todo.ts +0 -67
- package/src/ui/configure.ts +0 -471
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
- /package/{src/ui → plugins/tui}/markdown.ts +0 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// jade/repeats:模型常常把同一个调用原样再发一遍。
|
|
2
|
+
//
|
|
3
|
+
// 同一次回复里参数完全相同的调用直接拒绝,不用让用户再批准一次;
|
|
4
|
+
// 同一个调用连续失败、其间什么都没变时,在结果后面提醒模型它已经失败过几次,别再原样重试
|
|
5
|
+
// (改了文件再跑同一条测试命令是正常的"修了再验证",从头计数);
|
|
6
|
+
// 同一个调用已经成功执行过,之后什么都没变(没有执行过只读之外的调用,用户也没有发新消息),
|
|
7
|
+
// 再执行一遍结果也完全一样时,在结果后面提醒模型这是重复的(反复读同一个文件、反复打开同一个网页)。
|
|
8
|
+
// 不拒绝:也可能是在等外面的东西变化,只提醒。压缩插件清理或总结了历史后,之前的结果可能已经不在上下文里,都不算重复。
|
|
9
|
+
// 打开文件给用户看(start、xdg-open……)会弹出新窗口:这一轮里打开过、之后什么都没改,再打开就直接拒绝。
|
|
10
|
+
// write_file 之后什么都没改又读回同一个文件,提醒模型内容就是它写的那些。
|
|
11
|
+
// 这些记录只在本会话的作用域里:恢复会话后从零开始,最多多重试一轮。
|
|
12
|
+
//
|
|
13
|
+
// 从旧 jade 的 plugins/repeats.ts 移植。
|
|
14
|
+
import { createHash } from "node:crypto";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { definePlugin } from "../../dist/api.js";
|
|
17
|
+
const ID = "jade/repeats";
|
|
18
|
+
/** jade/compact 记录压缩结果用的派生数据键(旧 jade 的约定):记一次就说明历史被压缩过 */
|
|
19
|
+
export const COMPACT_KEY = "jade/compact";
|
|
20
|
+
/** 只打开一个文件或网址的命令:start "" x、start x、xdg-open x、open x、explorer x */
|
|
21
|
+
const OPEN = /^\s*(?:start\s+""|start|xdg-open|open|explorer(?:\.exe)?)\s+(?:"([^"]+)"|'([^']+)'|([^\s"'&|;<>]+))\s*$/i;
|
|
22
|
+
/** bash 调用打开的目标(规范化后),不是打开命令时是 undefined */
|
|
23
|
+
export function openTarget(call, cwd) {
|
|
24
|
+
if (call.name !== "bash")
|
|
25
|
+
return undefined;
|
|
26
|
+
const command = call.input?.command;
|
|
27
|
+
if (typeof command !== "string")
|
|
28
|
+
return undefined;
|
|
29
|
+
const m = OPEN.exec(command);
|
|
30
|
+
const target = m && (m[1] ?? m[2] ?? m[3]);
|
|
31
|
+
if (!target)
|
|
32
|
+
return undefined;
|
|
33
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(target) ? target : normalize(cwd, target);
|
|
34
|
+
}
|
|
35
|
+
function normalize(cwd, p) {
|
|
36
|
+
const full = path.resolve(cwd, p);
|
|
37
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
38
|
+
}
|
|
39
|
+
/** 提醒模型的失败次数 */
|
|
40
|
+
const WARN_AFTER = 2;
|
|
41
|
+
const hash = (content) => createHash("sha1").update(JSON.stringify(content.map((b) => (b.kind === "text" ? b.text : b)))).digest("hex");
|
|
42
|
+
/** 会话里的 hook:model.before 数回复次数,tool.gate 拒绝重复,tool.after 加提醒,event 在用户发消息、压缩后重新计 */
|
|
43
|
+
export function repeatsHooks(opts) {
|
|
44
|
+
const id = opts.id ?? ID;
|
|
45
|
+
const cwd = opts.cwd;
|
|
46
|
+
/** 模型调用的次数,每次调用模型前加一 */
|
|
47
|
+
let calls = 0;
|
|
48
|
+
/** 本次回复里已经出现的调用;mutates:其中有只读之外、也不是打开文件的调用 */
|
|
49
|
+
let current = { calls: -1, keys: new Set(), mutates: false };
|
|
50
|
+
/** 调用 → 连续失败的次数和最近一次失败时的 version;成功一次就清掉 */
|
|
51
|
+
const failures = new Map();
|
|
52
|
+
/** 每执行成功一个只读之外的调用、用户每发一条消息、历史每压缩一次就加一 */
|
|
53
|
+
let version = 0;
|
|
54
|
+
/** 调用 → 上次成功执行后的 version 和结果 */
|
|
55
|
+
const succeeded = new Map();
|
|
56
|
+
/** 已放行、还没执行完的调用是不是只读的(tool.after 里拿不到工具的声明) */
|
|
57
|
+
const readOnly = new Map();
|
|
58
|
+
/** 每执行成功一个只读之外、也不是打开文件的调用就加一(可能改了文件) */
|
|
59
|
+
let changes = 0;
|
|
60
|
+
/** 本轮打开过的目标 → 打开时的 changes;用户发消息时清空 */
|
|
61
|
+
const opened = new Map();
|
|
62
|
+
/** 本轮用 write_file 写过的文件 → 写完时的 changes;用户发消息时清空 */
|
|
63
|
+
const written = new Map();
|
|
64
|
+
const key = (call) => `${call.name}\0${JSON.stringify(call.input)}`;
|
|
65
|
+
const note = (text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id }, trust: "trusted" });
|
|
66
|
+
return [
|
|
67
|
+
{
|
|
68
|
+
kind: "transform",
|
|
69
|
+
point: "model.before",
|
|
70
|
+
fn: () => {
|
|
71
|
+
calls++;
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
kind: "gate",
|
|
76
|
+
point: "tool.gate",
|
|
77
|
+
fn: (call) => {
|
|
78
|
+
if (current.calls !== calls)
|
|
79
|
+
current = { calls, keys: new Set(), mutates: false };
|
|
80
|
+
const k = key(call);
|
|
81
|
+
if (current.keys.has(k)) {
|
|
82
|
+
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." };
|
|
83
|
+
}
|
|
84
|
+
current.keys.add(k);
|
|
85
|
+
const target = openTarget(call, cwd);
|
|
86
|
+
// 同一次回复里排在前面的调用可能改了它,还没执行,不拒绝
|
|
87
|
+
if (target !== undefined && opened.get(target) === changes && !current.mutates) {
|
|
88
|
+
return {
|
|
89
|
+
decision: "deny",
|
|
90
|
+
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). " +
|
|
91
|
+
"End your turn and tell the user what you changed.",
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (target === undefined && call.tool.effect !== "read")
|
|
95
|
+
current.mutates = true;
|
|
96
|
+
readOnly.set(call.toolUseId, call.tool.effect === "read");
|
|
97
|
+
return { decision: "abstain" };
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
kind: "transform",
|
|
102
|
+
point: "tool.after",
|
|
103
|
+
fn: (outcome) => {
|
|
104
|
+
const k = key(outcome.call);
|
|
105
|
+
const read = readOnly.get(outcome.call.toolUseId) ?? false;
|
|
106
|
+
readOnly.delete(outcome.call.toolUseId);
|
|
107
|
+
if (!outcome.isError) {
|
|
108
|
+
failures.delete(k);
|
|
109
|
+
const target = openTarget(outcome.call, cwd);
|
|
110
|
+
if (target !== undefined)
|
|
111
|
+
opened.set(target, changes);
|
|
112
|
+
else if (!read)
|
|
113
|
+
changes++;
|
|
114
|
+
const file = outcome.call.input?.path;
|
|
115
|
+
if (outcome.call.name === "write_file" && typeof file === "string")
|
|
116
|
+
written.set(normalize(cwd, file), changes);
|
|
117
|
+
const readBack = outcome.call.name === "read_file" && typeof file === "string" && written.get(normalize(cwd, file)) === changes
|
|
118
|
+
? 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.")
|
|
119
|
+
: undefined;
|
|
120
|
+
const before = succeeded.get(k);
|
|
121
|
+
// 只读之外的调用(包括这一个)可能改了东西,之后的调用都不算"什么都没变"
|
|
122
|
+
if (!read)
|
|
123
|
+
version++;
|
|
124
|
+
const result = hash(outcome.content);
|
|
125
|
+
const repeated = before !== undefined && before.version === (read ? version : version - 1) && before.result === result;
|
|
126
|
+
succeeded.set(k, { version, result });
|
|
127
|
+
if (!repeated)
|
|
128
|
+
return readBack ? { ...outcome, content: [...outcome.content, readBack] } : undefined;
|
|
129
|
+
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). " +
|
|
130
|
+
"Do not repeat it: use what you already have, or end your turn and tell the user where things stand.";
|
|
131
|
+
return { ...outcome, content: [...outcome.content, note(text)] };
|
|
132
|
+
}
|
|
133
|
+
const prev = failures.get(k);
|
|
134
|
+
// 上次失败之后改过东西(修了代码再跑测试),重新计数
|
|
135
|
+
const n = prev && prev.version === version ? prev.n + 1 : 1;
|
|
136
|
+
failures.set(k, { n, version });
|
|
137
|
+
if (n < WARN_AFTER)
|
|
138
|
+
return;
|
|
139
|
+
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.`;
|
|
140
|
+
return { ...outcome, content: [...outcome.content, note(text)] };
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
kind: "observe",
|
|
145
|
+
point: "event",
|
|
146
|
+
// 用户发了新消息,可能就是要再来一遍("再打开看看"),之前的记录都不算了;
|
|
147
|
+
// 压缩之后模型可能已经看不到之前的结果,重新执行不算重复
|
|
148
|
+
fn: (e) => {
|
|
149
|
+
if (e.type === "derived.recorded" && e.data.key === COMPACT_KEY)
|
|
150
|
+
version++;
|
|
151
|
+
if (e.type === "input.received" && e.data.kind === "message") {
|
|
152
|
+
version++;
|
|
153
|
+
opened.clear();
|
|
154
|
+
written.clear();
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
export default definePlugin({
|
|
161
|
+
session(ctx) {
|
|
162
|
+
for (const h of repeatsHooks({ cwd: ctx.cwd, id: ctx.plugin }))
|
|
163
|
+
ctx.kernel.hook(h);
|
|
164
|
+
},
|
|
165
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// 五字段 cron(分 时 日 月 星期),按本机时区计算。支持 *、数字、a-b、/步长和逗号列表;
|
|
2
|
+
// 星期 0 和 7 都是周日。日和星期都受限时任一匹配即可(Vixie cron 的规则),有一个以 * 开头时两者都要匹配。
|
|
3
|
+
const FIELDS = [
|
|
4
|
+
["minute", 0, 59],
|
|
5
|
+
["hour", 0, 23],
|
|
6
|
+
["day of month", 1, 31],
|
|
7
|
+
["month", 1, 12],
|
|
8
|
+
["day of week", 0, 7],
|
|
9
|
+
];
|
|
10
|
+
function field(text, [name, min, max]) {
|
|
11
|
+
const out = new Set();
|
|
12
|
+
for (const item of text.split(",")) {
|
|
13
|
+
const m = /^(\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(item);
|
|
14
|
+
if (!m)
|
|
15
|
+
throw new Error(`invalid ${name} field "${text}"`);
|
|
16
|
+
const lo = m[1] === "*" ? min : Number(m[2]);
|
|
17
|
+
const hi = m[1] === "*" ? max : m[3] !== undefined ? Number(m[3]) : m[4] !== undefined ? max : lo;
|
|
18
|
+
const step = m[4] !== undefined ? Number(m[4]) : 1;
|
|
19
|
+
if (lo < min || hi > max || lo > hi || step < 1)
|
|
20
|
+
throw new Error(`${name} field "${text}" is out of range ${min}-${max}`);
|
|
21
|
+
for (let v = lo; v <= hi; v += step)
|
|
22
|
+
out.add(v);
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
export function parseCron(expr) {
|
|
27
|
+
const parts = expr.trim().split(/\s+/);
|
|
28
|
+
if (parts.length !== 5)
|
|
29
|
+
throw new Error(`cron needs 5 fields (minute hour day-of-month month day-of-week), got "${expr}"`);
|
|
30
|
+
const [minute, hour, dom, month, dow] = parts.map((p, i) => field(p, FIELDS[i]));
|
|
31
|
+
if (dow.delete(7))
|
|
32
|
+
dow.add(0);
|
|
33
|
+
return { minute: minute, hour: hour, dom: dom, month: month, dow: dow, domStar: parts[2].startsWith("*"), dowStar: parts[4].startsWith("*") };
|
|
34
|
+
}
|
|
35
|
+
function dayMatches(c, d) {
|
|
36
|
+
const dom = c.dom.has(d.getDate());
|
|
37
|
+
const dow = c.dow.has(d.getDay());
|
|
38
|
+
return c.domStar || c.dowStar ? dom && dow : dom || dow;
|
|
39
|
+
}
|
|
40
|
+
/** from 之后(不含)第一个匹配的整分钟;五年内没有(例如 2 月 30 日)时 undefined */
|
|
41
|
+
export function nextRun(expr, from) {
|
|
42
|
+
const c = typeof expr === "string" ? parseCron(expr) : expr;
|
|
43
|
+
const d = new Date(from);
|
|
44
|
+
d.setSeconds(0, 0);
|
|
45
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
46
|
+
const limit = from.getTime() + 5 * 366 * 86_400_000;
|
|
47
|
+
while (d.getTime() < limit) {
|
|
48
|
+
if (!c.month.has(d.getMonth() + 1)) {
|
|
49
|
+
d.setMonth(d.getMonth() + 1, 1);
|
|
50
|
+
d.setHours(0, 0, 0, 0);
|
|
51
|
+
}
|
|
52
|
+
else if (!dayMatches(c, d)) {
|
|
53
|
+
d.setDate(d.getDate() + 1);
|
|
54
|
+
d.setHours(0, 0, 0, 0);
|
|
55
|
+
}
|
|
56
|
+
else if (!c.hour.has(d.getHours())) {
|
|
57
|
+
d.setHours(d.getHours() + 1, 0, 0, 0);
|
|
58
|
+
}
|
|
59
|
+
else if (!c.minute.has(d.getMinutes())) {
|
|
60
|
+
d.setMinutes(d.getMinutes() + 1, 0, 0);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
return d;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// jade/schedule:定时任务。任务存在 $JADE_HOME/state/schedules.json,重启后还在;
|
|
2
|
+
// 到点时投到创建它的目录里正在运行的 jade 的当前会话(正在跑就排在本轮之后),没有 jade 开着就等着,
|
|
3
|
+
// 下次打开时错过的几次只补一次。不能提问的前端(jade -p)不投递。
|
|
4
|
+
// 管理入口:/schedule 命令和给模型的 schedule_create、schedule_list、schedule_edit 工具,共用同一份存储。
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { definePlugin } from "../../dist/api.js";
|
|
7
|
+
import { parseCron } from "./cron.js";
|
|
8
|
+
import { claimDue, nextAt, sameDir, ScheduleStore } from "./store.js";
|
|
9
|
+
const ID = "jade/schedule";
|
|
10
|
+
const TITLE_CHARS = 120;
|
|
11
|
+
function titleFrom(prompt) {
|
|
12
|
+
const one = prompt.replace(/\s+/g, " ").trim();
|
|
13
|
+
return one.length > 50 ? `${one.slice(0, 50)}…` : one;
|
|
14
|
+
}
|
|
15
|
+
/** 检查 cron 并算出下一次;没有下一次时抛错 */
|
|
16
|
+
function firstRun(cron, now) {
|
|
17
|
+
parseCron(cron);
|
|
18
|
+
const next = nextAt(cron, now);
|
|
19
|
+
if (!next)
|
|
20
|
+
throw new Error(`cron "${cron}" never fires`);
|
|
21
|
+
return next;
|
|
22
|
+
}
|
|
23
|
+
export async function createTask(store, opts, now = new Date()) {
|
|
24
|
+
const prompt = opts.prompt.trim();
|
|
25
|
+
if (!prompt)
|
|
26
|
+
throw new Error("prompt is empty");
|
|
27
|
+
const cron = opts.cron.trim().split(/\s+/).join(" ");
|
|
28
|
+
const next = firstRun(cron, now);
|
|
29
|
+
const title = (opts.title?.trim() || titleFrom(prompt)).slice(0, TITLE_CHARS);
|
|
30
|
+
const at = now.toISOString();
|
|
31
|
+
const task = {
|
|
32
|
+
id: crypto.randomUUID().slice(0, 8),
|
|
33
|
+
title,
|
|
34
|
+
prompt,
|
|
35
|
+
cron,
|
|
36
|
+
recurring: opts.recurring ?? true,
|
|
37
|
+
cwd: path.resolve(opts.cwd),
|
|
38
|
+
target: { kind: "cwd" },
|
|
39
|
+
status: "active",
|
|
40
|
+
next,
|
|
41
|
+
createdAt: at,
|
|
42
|
+
updatedAt: at,
|
|
43
|
+
};
|
|
44
|
+
await store.update((tasks) => void tasks.push(task));
|
|
45
|
+
return task;
|
|
46
|
+
}
|
|
47
|
+
/** 改一个任务;删除时返回 undefined。恢复和改 cron 从现在起重新算下一次 */
|
|
48
|
+
export function editTask(store, id, action, patch = {}, now = new Date()) {
|
|
49
|
+
return store.update((tasks) => {
|
|
50
|
+
const i = tasks.findIndex((t) => t.id === id);
|
|
51
|
+
if (i < 0)
|
|
52
|
+
throw new Error(`no scheduled task ${id}`);
|
|
53
|
+
if (action === "delete") {
|
|
54
|
+
tasks.splice(i, 1);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const t = tasks[i];
|
|
58
|
+
if (action === "pause") {
|
|
59
|
+
t.status = "paused";
|
|
60
|
+
delete t.next;
|
|
61
|
+
}
|
|
62
|
+
else if (action === "resume") {
|
|
63
|
+
t.status = "active";
|
|
64
|
+
t.next = firstRun(t.cron, now);
|
|
65
|
+
}
|
|
66
|
+
else if (action === "update") {
|
|
67
|
+
if (patch.prompt !== undefined) {
|
|
68
|
+
if (!patch.prompt.trim())
|
|
69
|
+
throw new Error("prompt is empty");
|
|
70
|
+
t.prompt = patch.prompt.trim();
|
|
71
|
+
}
|
|
72
|
+
if (patch.title !== undefined)
|
|
73
|
+
t.title = (patch.title.trim() || titleFrom(t.prompt)).slice(0, TITLE_CHARS);
|
|
74
|
+
if (patch.recurring !== undefined)
|
|
75
|
+
t.recurring = patch.recurring;
|
|
76
|
+
if (patch.cron !== undefined) {
|
|
77
|
+
t.cron = patch.cron.trim().split(/\s+/).join(" ");
|
|
78
|
+
const next = firstRun(t.cron, now);
|
|
79
|
+
if (t.status === "active")
|
|
80
|
+
t.next = next;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
throw new Error(`unknown action ${String(action)}`);
|
|
85
|
+
}
|
|
86
|
+
t.updatedAt = now.toISOString();
|
|
87
|
+
return { ...t };
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** 一行说明;任务不在 cwd 时写上它的目录 */
|
|
91
|
+
export function describe(t, cwd) {
|
|
92
|
+
const when = t.status === "active" && t.next ? `next ${new Date(t.next).toLocaleString()}` : t.status;
|
|
93
|
+
const once = t.recurring ? "" : " (once)";
|
|
94
|
+
const where = sameDir(t.cwd, cwd) ? "" : ` [in ${t.cwd}]`;
|
|
95
|
+
return `${t.id} ${t.cron}${once} ${when} ${t.title}${where}`;
|
|
96
|
+
}
|
|
97
|
+
function listing(tasks, cwd) {
|
|
98
|
+
if (tasks.length === 0)
|
|
99
|
+
return "No scheduled tasks.";
|
|
100
|
+
// 本目录的在前
|
|
101
|
+
const sorted = [...tasks].sort((a, b) => Number(sameDir(b.cwd, cwd)) - Number(sameDir(a.cwd, cwd)));
|
|
102
|
+
return sorted.map((t) => describe(t, cwd)).join("\n");
|
|
103
|
+
}
|
|
104
|
+
const messageText = (t) => `[Scheduled task "${t.title}" (${t.id})]\n${t.prompt}`;
|
|
105
|
+
/** 到点时发给模型的消息 */
|
|
106
|
+
export function delivery(t) {
|
|
107
|
+
return { kind: "message", content: [{ kind: "text", text: messageText(t), origin: { by: "plugin", id: ID }, trust: "trusted" }] };
|
|
108
|
+
}
|
|
109
|
+
const USAGE = `usage:
|
|
110
|
+
/schedule list tasks
|
|
111
|
+
/schedule add <cron> <prompt> run a prompt on a 5-field cron (local time), e.g. /schedule add 0 9 * * 1-5 summarize yesterday's commits
|
|
112
|
+
/schedule once <cron> <prompt> same, but only the next time it matches
|
|
113
|
+
/schedule pause|resume|rm <id>
|
|
114
|
+
/schedule run <id> send a task's prompt now (does not move its next run)
|
|
115
|
+
Tasks run in the current session of a jade that is open in the directory they were created in.`;
|
|
116
|
+
const err = (e) => (e instanceof Error ? e.message : String(e));
|
|
117
|
+
function tools(store, cwd) {
|
|
118
|
+
const result = async (fn) => {
|
|
119
|
+
try {
|
|
120
|
+
return { content: await fn() };
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
return { content: err(e), isError: true };
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const where = "A task runs only while jade is open in the directory it was created in: its prompt is sent as a message to that jade's current session (after the current turn if one is running). " +
|
|
127
|
+
"Occurrences missed while jade was closed are run once when it opens again.";
|
|
128
|
+
return [
|
|
129
|
+
{
|
|
130
|
+
name: "schedule_create",
|
|
131
|
+
description: `Create a scheduled task that sends a prompt to the session at times given by a 5-field cron expression in the user's local time zone (minute hour day-of-month month day-of-week). ${where} ` +
|
|
132
|
+
"Write the prompt as a self-contained instruction: it is read later, possibly in a different session. Use recurring: false for a one-time reminder.",
|
|
133
|
+
inputSchema: {
|
|
134
|
+
type: "object",
|
|
135
|
+
properties: {
|
|
136
|
+
prompt: { type: "string", description: "the instruction to send when the task fires" },
|
|
137
|
+
cron: { type: "string", description: 'e.g. "0 9 * * 1-5" for weekdays at 09:00' },
|
|
138
|
+
title: { type: "string", description: "short name; defaults to the start of the prompt" },
|
|
139
|
+
recurring: { type: "boolean", description: "false: fire once, then end (default true)" },
|
|
140
|
+
},
|
|
141
|
+
required: ["prompt", "cron"],
|
|
142
|
+
additionalProperties: false,
|
|
143
|
+
},
|
|
144
|
+
// 任务在本会话之后、以用户的身份发出指令,所以和改工作区之外的东西一样要问
|
|
145
|
+
effect: "external",
|
|
146
|
+
concurrency: "exclusive",
|
|
147
|
+
idempotent: false,
|
|
148
|
+
execute: (input) => result(async () => {
|
|
149
|
+
const i = input;
|
|
150
|
+
const t = await createTask(store, { ...i, cwd });
|
|
151
|
+
return `Created scheduled task:\n${describe(t, cwd)}`;
|
|
152
|
+
}),
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "schedule_list",
|
|
156
|
+
description: `List all scheduled tasks with their id, cron, next run time and title. ${where}`,
|
|
157
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
158
|
+
effect: "read",
|
|
159
|
+
concurrency: "safe",
|
|
160
|
+
idempotent: true,
|
|
161
|
+
execute: () => result(async () => listing(await store.list(), cwd)),
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
name: "schedule_edit",
|
|
165
|
+
description: "Change a scheduled task (update its prompt, cron, title or recurring), pause it, resume it, or delete it. Get ids from schedule_list.",
|
|
166
|
+
inputSchema: {
|
|
167
|
+
type: "object",
|
|
168
|
+
properties: {
|
|
169
|
+
id: { type: "string" },
|
|
170
|
+
action: { type: "string", enum: ["update", "pause", "resume", "delete"] },
|
|
171
|
+
prompt: { type: "string" },
|
|
172
|
+
cron: { type: "string" },
|
|
173
|
+
title: { type: "string" },
|
|
174
|
+
recurring: { type: "boolean" },
|
|
175
|
+
},
|
|
176
|
+
required: ["id", "action"],
|
|
177
|
+
additionalProperties: false,
|
|
178
|
+
},
|
|
179
|
+
effect: "external",
|
|
180
|
+
concurrency: "exclusive",
|
|
181
|
+
idempotent: false,
|
|
182
|
+
execute: (input) => result(async () => {
|
|
183
|
+
const { id, action, ...patch } = input;
|
|
184
|
+
const t = await editTask(store, id, action, patch);
|
|
185
|
+
return t ? `Updated:\n${describe(t, cwd)}` : `Deleted scheduled task ${id}.`;
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
188
|
+
];
|
|
189
|
+
}
|
|
190
|
+
export default definePlugin({
|
|
191
|
+
process(ctx) {
|
|
192
|
+
const store = new ScheduleStore(ctx.home);
|
|
193
|
+
ctx.service("commands@1")?.register({
|
|
194
|
+
name: "schedule",
|
|
195
|
+
args: "[add|once|pause|resume|rm|run] …",
|
|
196
|
+
description: "scheduled tasks: prompts sent on a cron schedule while jade is open here",
|
|
197
|
+
async run(args) {
|
|
198
|
+
const [sub = "list", ...rest] = args.trim().split(/\s+/).filter(Boolean);
|
|
199
|
+
try {
|
|
200
|
+
switch (sub) {
|
|
201
|
+
case "list":
|
|
202
|
+
return listing(await store.list(), ctx.cwd);
|
|
203
|
+
case "add":
|
|
204
|
+
case "once": {
|
|
205
|
+
if (rest.length < 6)
|
|
206
|
+
return USAGE;
|
|
207
|
+
const t = await createTask(store, { cron: rest.slice(0, 5).join(" "), prompt: rest.slice(5).join(" "), recurring: sub === "add", cwd: ctx.cwd });
|
|
208
|
+
return `Scheduled:\n${describe(t, ctx.cwd)}`;
|
|
209
|
+
}
|
|
210
|
+
case "pause":
|
|
211
|
+
case "resume":
|
|
212
|
+
case "rm": {
|
|
213
|
+
if (rest.length !== 1)
|
|
214
|
+
return USAGE;
|
|
215
|
+
const t = await editTask(store, rest[0], sub === "rm" ? "delete" : sub);
|
|
216
|
+
return t ? describe(t, ctx.cwd) : `Deleted ${rest[0]}.`;
|
|
217
|
+
}
|
|
218
|
+
case "run": {
|
|
219
|
+
const t = (await store.list()).find((x) => x.id === rest[0]);
|
|
220
|
+
if (!t)
|
|
221
|
+
return rest.length ? `No scheduled task ${rest[0]}.` : USAGE;
|
|
222
|
+
return { prompt: messageText(t) };
|
|
223
|
+
}
|
|
224
|
+
default:
|
|
225
|
+
return USAGE;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch (e) {
|
|
229
|
+
return `schedule: ${err(e)}`;
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
async session(ctx) {
|
|
235
|
+
const store = new ScheduleStore(ctx.home);
|
|
236
|
+
for (const t of tools(store, ctx.cwd))
|
|
237
|
+
ctx.kernel.tool(t);
|
|
238
|
+
const ui = ctx.service("ui@1");
|
|
239
|
+
// jade -p 这类不能提问的运行只做用户要它做的那一件事
|
|
240
|
+
if (!ui?.interactive)
|
|
241
|
+
return;
|
|
242
|
+
const seconds = Number(ctx.config.checkSeconds);
|
|
243
|
+
const every = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 30_000;
|
|
244
|
+
/** 已经发出、还没跑完的任务:跑完之前不再认领 */
|
|
245
|
+
const running = new Set();
|
|
246
|
+
let warned = false;
|
|
247
|
+
const check = async () => {
|
|
248
|
+
if (ctx.signal.aborted)
|
|
249
|
+
return;
|
|
250
|
+
let due;
|
|
251
|
+
try {
|
|
252
|
+
due = await claimDue(store, ctx.cwd, ctx.session.id, new Date(), running);
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
if (ctx.signal.aborted)
|
|
256
|
+
return;
|
|
257
|
+
if (!warned)
|
|
258
|
+
ui.notify(`schedule: ${err(e)}`, "warning");
|
|
259
|
+
warned = true;
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
warned = false;
|
|
263
|
+
for (const t of due) {
|
|
264
|
+
if (ctx.signal.aborted)
|
|
265
|
+
return;
|
|
266
|
+
running.add(t.id);
|
|
267
|
+
ui.notify(`[scheduled task "${t.title}" (${t.id})]`);
|
|
268
|
+
ctx.session
|
|
269
|
+
.send(delivery(t))
|
|
270
|
+
// 会话已经离开(换会话、退出)时 ui 已经不能用了
|
|
271
|
+
.catch((e) => ctx.signal.aborted || ui.notify(`scheduled task ${t.id} failed: ${err(e)}`, "error"))
|
|
272
|
+
.finally(() => running.delete(t.id));
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
// 不在这里立即检查:session() 执行时会话还没建好,这时 send 会失败,认领了的任务就丢了
|
|
276
|
+
await ctx.effect(() => {
|
|
277
|
+
const timer = setInterval(() => void check(), every);
|
|
278
|
+
return () => clearInterval(timer);
|
|
279
|
+
});
|
|
280
|
+
},
|
|
281
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/schedule",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"optional": [
|
|
7
|
+
"commands@1",
|
|
8
|
+
"ui@1"
|
|
9
|
+
],
|
|
10
|
+
"config": {
|
|
11
|
+
"checkSeconds": {
|
|
12
|
+
"default": 30,
|
|
13
|
+
"description": "how often to look for scheduled tasks that are due"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// 定时任务的存储:$JADE_HOME/state/schedules.json。同一台机器上可能开着好几个 jade,
|
|
2
|
+
// 所有读写都在一把目录锁(schedules.lock,mkdir 是原子的)里进行,到点的任务由先拿到锁的进程认领,只投递一次。
|
|
3
|
+
import { randomBytes } from "node:crypto";
|
|
4
|
+
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { nextRun } from "./cron.js";
|
|
7
|
+
/** 锁超过这么久没释放,当作持有它的进程已经不在了 */
|
|
8
|
+
const STALE_MS = 10_000;
|
|
9
|
+
const WAIT_MS = 5_000;
|
|
10
|
+
export class ScheduleStore {
|
|
11
|
+
file;
|
|
12
|
+
#lock;
|
|
13
|
+
constructor(home) {
|
|
14
|
+
this.file = path.join(home, "state", "schedules.json");
|
|
15
|
+
this.#lock = path.join(home, "state", "schedules.lock");
|
|
16
|
+
}
|
|
17
|
+
/** 读全部任务 */
|
|
18
|
+
list() {
|
|
19
|
+
return this.#locked(() => this.#read());
|
|
20
|
+
}
|
|
21
|
+
/** 在锁里读出全部任务交给 fn,fn 改了就写回。fn 抛错时不写 */
|
|
22
|
+
update(fn) {
|
|
23
|
+
return this.#locked(async () => {
|
|
24
|
+
const tasks = await this.#read();
|
|
25
|
+
const before = JSON.stringify(tasks);
|
|
26
|
+
const out = fn(tasks);
|
|
27
|
+
if (JSON.stringify(tasks) !== before) {
|
|
28
|
+
const tmp = `${this.file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
29
|
+
await writeFile(tmp, `${JSON.stringify({ version: 1, tasks }, null, 2)}\n`);
|
|
30
|
+
await rename(tmp, this.file);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async #read() {
|
|
36
|
+
let text;
|
|
37
|
+
try {
|
|
38
|
+
text = await readFile(this.file, "utf8");
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
if (e.code === "ENOENT")
|
|
42
|
+
return [];
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
const data = JSON.parse(text);
|
|
46
|
+
if (data.version !== 1 || !Array.isArray(data.tasks))
|
|
47
|
+
throw new Error(`${this.file}: unsupported format`);
|
|
48
|
+
return data.tasks;
|
|
49
|
+
}
|
|
50
|
+
async #locked(fn) {
|
|
51
|
+
await mkdir(path.dirname(this.#lock), { recursive: true });
|
|
52
|
+
const deadline = Date.now() + WAIT_MS;
|
|
53
|
+
// 在锁目录里放一个随机标记,释放时只删自己的锁:持有太久被别人当成过期删掉、重新上了锁时,不去删别人的。
|
|
54
|
+
// 不用目录的文件号:Linux 上删掉目录马上重建,常常拿到同一个文件号
|
|
55
|
+
const mine = randomBytes(8).toString("hex");
|
|
56
|
+
for (;;) {
|
|
57
|
+
try {
|
|
58
|
+
await mkdir(this.#lock);
|
|
59
|
+
try {
|
|
60
|
+
await writeFile(path.join(this.#lock, "owner"), mine);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
await rm(this.#lock, { recursive: true, force: true });
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e.code !== "EEXIST")
|
|
70
|
+
throw e;
|
|
71
|
+
const age = await stat(this.#lock).then((s) => Date.now() - s.mtimeMs, () => 0);
|
|
72
|
+
if (age > STALE_MS)
|
|
73
|
+
await rm(this.#lock, { recursive: true, force: true });
|
|
74
|
+
else if (Date.now() > deadline)
|
|
75
|
+
throw new Error(`${this.#lock} is held by another jade; remove it if no jade is running`);
|
|
76
|
+
else
|
|
77
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
return await fn();
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
const owner = await readFile(path.join(this.#lock, "owner"), "utf8").catch(() => undefined);
|
|
85
|
+
if (owner === mine)
|
|
86
|
+
await rm(this.#lock, { recursive: true, force: true });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 两个目录是不是同一个:比较绝对路径(不看末尾的分隔符),Windows 上不分大小写。
|
|
92
|
+
* 和 jade/sessions 的 cwdKey 相同,插件之间不共享代码(只能共享类型),所以这里留一份
|
|
93
|
+
*/
|
|
94
|
+
export function sameDir(a, b) {
|
|
95
|
+
const key = (p) => (process.platform === "win32" ? path.resolve(p).toLowerCase() : path.resolve(p));
|
|
96
|
+
return key(a) === key(b);
|
|
97
|
+
}
|
|
98
|
+
/** 下一次触发时间;没有了时 undefined */
|
|
99
|
+
export function nextAt(cron, from) {
|
|
100
|
+
return nextRun(cron, from)?.toISOString();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 认领 cwd 里到点的任务:记下这次投递,推进到 now 之后的下一次(错过的几次只算一次),
|
|
104
|
+
* 一次性的任务结束。skip 里的任务(上一次还没跑完)不认领,留到下次检查
|
|
105
|
+
*/
|
|
106
|
+
export function claimDue(store, cwd, session, now, skip = new Set()) {
|
|
107
|
+
return store.update((tasks) => {
|
|
108
|
+
const due = [];
|
|
109
|
+
for (const t of tasks) {
|
|
110
|
+
if (t.status !== "active" || !sameDir(t.cwd, cwd) || !t.next || Date.parse(t.next) > now.getTime() || skip.has(t.id))
|
|
111
|
+
continue;
|
|
112
|
+
t.last = { at: now.toISOString(), session };
|
|
113
|
+
t.updatedAt = now.toISOString();
|
|
114
|
+
const next = t.recurring ? nextAt(t.cron, now) : undefined;
|
|
115
|
+
if (next)
|
|
116
|
+
t.next = next;
|
|
117
|
+
else {
|
|
118
|
+
t.status = "ended";
|
|
119
|
+
delete t.next;
|
|
120
|
+
}
|
|
121
|
+
due.push({ ...t });
|
|
122
|
+
}
|
|
123
|
+
return due;
|
|
124
|
+
});
|
|
125
|
+
}
|