@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,74 @@
|
|
|
1
|
+
// 五字段 cron(分 时 日 月 星期),按本机时区计算。支持 *、数字、a-b、/步长和逗号列表;
|
|
2
|
+
// 星期 0 和 7 都是周日。日和星期都受限时任一匹配即可(Vixie cron 的规则),有一个以 * 开头时两者都要匹配。
|
|
3
|
+
|
|
4
|
+
export interface Cron {
|
|
5
|
+
minute: Set<number>;
|
|
6
|
+
hour: Set<number>;
|
|
7
|
+
dom: Set<number>;
|
|
8
|
+
month: Set<number>;
|
|
9
|
+
dow: Set<number>;
|
|
10
|
+
/** 日、星期字段以 * 开头 */
|
|
11
|
+
domStar: boolean;
|
|
12
|
+
dowStar: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const FIELDS = [
|
|
16
|
+
["minute", 0, 59],
|
|
17
|
+
["hour", 0, 23],
|
|
18
|
+
["day of month", 1, 31],
|
|
19
|
+
["month", 1, 12],
|
|
20
|
+
["day of week", 0, 7],
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
function field(text: string, [name, min, max]: (typeof FIELDS)[number]): Set<number> {
|
|
24
|
+
const out = new Set<number>();
|
|
25
|
+
for (const item of text.split(",")) {
|
|
26
|
+
const m = /^(\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(item);
|
|
27
|
+
if (!m) throw new Error(`invalid ${name} field "${text}"`);
|
|
28
|
+
const lo = m[1] === "*" ? min : Number(m[2]);
|
|
29
|
+
const hi = m[1] === "*" ? max : m[3] !== undefined ? Number(m[3]) : m[4] !== undefined ? max : lo;
|
|
30
|
+
const step = m[4] !== undefined ? Number(m[4]) : 1;
|
|
31
|
+
if (lo < min || hi > max || lo > hi || step < 1) throw new Error(`${name} field "${text}" is out of range ${min}-${max}`);
|
|
32
|
+
for (let v = lo; v <= hi; v += step) out.add(v);
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function parseCron(expr: string): Cron {
|
|
38
|
+
const parts = expr.trim().split(/\s+/);
|
|
39
|
+
if (parts.length !== 5) throw new Error(`cron needs 5 fields (minute hour day-of-month month day-of-week), got "${expr}"`);
|
|
40
|
+
const [minute, hour, dom, month, dow] = parts.map((p, i) => field(p, FIELDS[i]!));
|
|
41
|
+
if (dow!.delete(7)) dow!.add(0);
|
|
42
|
+
return { minute: minute!, hour: hour!, dom: dom!, month: month!, dow: dow!, domStar: parts[2]!.startsWith("*"), dowStar: parts[4]!.startsWith("*") };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function dayMatches(c: Cron, d: Date): boolean {
|
|
46
|
+
const dom = c.dom.has(d.getDate());
|
|
47
|
+
const dow = c.dow.has(d.getDay());
|
|
48
|
+
return c.domStar || c.dowStar ? dom && dow : dom || dow;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** from 之后(不含)第一个匹配的整分钟;五年内没有(例如 2 月 30 日)时 undefined */
|
|
52
|
+
export function nextRun(expr: string | Cron, from: Date): Date | undefined {
|
|
53
|
+
const c = typeof expr === "string" ? parseCron(expr) : expr;
|
|
54
|
+
const d = new Date(from);
|
|
55
|
+
d.setSeconds(0, 0);
|
|
56
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
57
|
+
const limit = from.getTime() + 5 * 366 * 86_400_000;
|
|
58
|
+
while (d.getTime() < limit) {
|
|
59
|
+
if (!c.month.has(d.getMonth() + 1)) {
|
|
60
|
+
d.setMonth(d.getMonth() + 1, 1);
|
|
61
|
+
d.setHours(0, 0, 0, 0);
|
|
62
|
+
} else if (!dayMatches(c, d)) {
|
|
63
|
+
d.setDate(d.getDate() + 1);
|
|
64
|
+
d.setHours(0, 0, 0, 0);
|
|
65
|
+
} else if (!c.hour.has(d.getHours())) {
|
|
66
|
+
d.setHours(d.getHours() + 1, 0, 0, 0);
|
|
67
|
+
} else if (!c.minute.has(d.getMinutes())) {
|
|
68
|
+
d.setMinutes(d.getMinutes() + 1, 0, 0);
|
|
69
|
+
} else {
|
|
70
|
+
return d;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// jade/schedule:定时任务。任务存在 $JADE_HOME/state/schedules.json,重启后还在;
|
|
2
|
+
// 到点时投到创建它的目录里正在运行的 jade 的当前会话(正在跑就排在本轮之后),没有 jade 开着就等着,
|
|
3
|
+
// 下次打开时错过的几次只补一次。不能提问的前端(jade -p)不投递。
|
|
4
|
+
// 管理入口:/schedule 命令和给模型的 schedule_create、schedule_list、schedule_edit 工具,共用同一份存储。
|
|
5
|
+
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import type { Tool, UserInput } from "uncarved";
|
|
8
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
9
|
+
import type { Commands } from "../commands/index.ts";
|
|
10
|
+
import { parseCron } from "./cron.ts";
|
|
11
|
+
import { claimDue, nextAt, sameDir, ScheduleStore, type Task } from "./store.ts";
|
|
12
|
+
|
|
13
|
+
const ID = "jade/schedule";
|
|
14
|
+
const TITLE_CHARS = 120;
|
|
15
|
+
|
|
16
|
+
function titleFrom(prompt: string): string {
|
|
17
|
+
const one = prompt.replace(/\s+/g, " ").trim();
|
|
18
|
+
return one.length > 50 ? `${one.slice(0, 50)}…` : one;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 检查 cron 并算出下一次;没有下一次时抛错 */
|
|
22
|
+
function firstRun(cron: string, now: Date): string {
|
|
23
|
+
parseCron(cron);
|
|
24
|
+
const next = nextAt(cron, now);
|
|
25
|
+
if (!next) throw new Error(`cron "${cron}" never fires`);
|
|
26
|
+
return next;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function createTask(
|
|
30
|
+
store: ScheduleStore,
|
|
31
|
+
opts: { prompt: string; cron: string; title?: string; recurring?: boolean; cwd: string },
|
|
32
|
+
now = new Date(),
|
|
33
|
+
): Promise<Task> {
|
|
34
|
+
const prompt = opts.prompt.trim();
|
|
35
|
+
if (!prompt) throw new Error("prompt is empty");
|
|
36
|
+
const cron = opts.cron.trim().split(/\s+/).join(" ");
|
|
37
|
+
const next = firstRun(cron, now);
|
|
38
|
+
const title = (opts.title?.trim() || titleFrom(prompt)).slice(0, TITLE_CHARS);
|
|
39
|
+
const at = now.toISOString();
|
|
40
|
+
const task: Task = {
|
|
41
|
+
id: crypto.randomUUID().slice(0, 8),
|
|
42
|
+
title,
|
|
43
|
+
prompt,
|
|
44
|
+
cron,
|
|
45
|
+
recurring: opts.recurring ?? true,
|
|
46
|
+
cwd: path.resolve(opts.cwd),
|
|
47
|
+
target: { kind: "cwd" },
|
|
48
|
+
status: "active",
|
|
49
|
+
next,
|
|
50
|
+
createdAt: at,
|
|
51
|
+
updatedAt: at,
|
|
52
|
+
};
|
|
53
|
+
await store.update((tasks) => void tasks.push(task));
|
|
54
|
+
return task;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type EditAction = "update" | "pause" | "resume" | "delete";
|
|
58
|
+
|
|
59
|
+
/** 改一个任务;删除时返回 undefined。恢复和改 cron 从现在起重新算下一次 */
|
|
60
|
+
export function editTask(
|
|
61
|
+
store: ScheduleStore,
|
|
62
|
+
id: string,
|
|
63
|
+
action: EditAction,
|
|
64
|
+
patch: { title?: string; prompt?: string; cron?: string; recurring?: boolean } = {},
|
|
65
|
+
now = new Date(),
|
|
66
|
+
): Promise<Task | undefined> {
|
|
67
|
+
return store.update((tasks) => {
|
|
68
|
+
const i = tasks.findIndex((t) => t.id === id);
|
|
69
|
+
if (i < 0) throw new Error(`no scheduled task ${id}`);
|
|
70
|
+
if (action === "delete") {
|
|
71
|
+
tasks.splice(i, 1);
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const t = tasks[i]!;
|
|
75
|
+
if (action === "pause") {
|
|
76
|
+
t.status = "paused";
|
|
77
|
+
delete t.next;
|
|
78
|
+
} else if (action === "resume") {
|
|
79
|
+
t.status = "active";
|
|
80
|
+
t.next = firstRun(t.cron, now);
|
|
81
|
+
} else if (action === "update") {
|
|
82
|
+
if (patch.prompt !== undefined) {
|
|
83
|
+
if (!patch.prompt.trim()) throw new Error("prompt is empty");
|
|
84
|
+
t.prompt = patch.prompt.trim();
|
|
85
|
+
}
|
|
86
|
+
if (patch.title !== undefined) t.title = (patch.title.trim() || titleFrom(t.prompt)).slice(0, TITLE_CHARS);
|
|
87
|
+
if (patch.recurring !== undefined) t.recurring = patch.recurring;
|
|
88
|
+
if (patch.cron !== undefined) {
|
|
89
|
+
t.cron = patch.cron.trim().split(/\s+/).join(" ");
|
|
90
|
+
const next = firstRun(t.cron, now);
|
|
91
|
+
if (t.status === "active") t.next = next;
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error(`unknown action ${String(action)}`);
|
|
95
|
+
}
|
|
96
|
+
t.updatedAt = now.toISOString();
|
|
97
|
+
return { ...t };
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 一行说明;任务不在 cwd 时写上它的目录 */
|
|
102
|
+
export function describe(t: Task, cwd: string): string {
|
|
103
|
+
const when = t.status === "active" && t.next ? `next ${new Date(t.next).toLocaleString()}` : t.status;
|
|
104
|
+
const once = t.recurring ? "" : " (once)";
|
|
105
|
+
const where = sameDir(t.cwd, cwd) ? "" : ` [in ${t.cwd}]`;
|
|
106
|
+
return `${t.id} ${t.cron}${once} ${when} ${t.title}${where}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function listing(tasks: Task[], cwd: string): string {
|
|
110
|
+
if (tasks.length === 0) return "No scheduled tasks.";
|
|
111
|
+
// 本目录的在前
|
|
112
|
+
const sorted = [...tasks].sort((a, b) => Number(sameDir(b.cwd, cwd)) - Number(sameDir(a.cwd, cwd)));
|
|
113
|
+
return sorted.map((t) => describe(t, cwd)).join("\n");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const messageText = (t: Task) => `[Scheduled task "${t.title}" (${t.id})]\n${t.prompt}`;
|
|
117
|
+
|
|
118
|
+
/** 到点时发给模型的消息 */
|
|
119
|
+
export function delivery(t: Task): UserInput {
|
|
120
|
+
return { kind: "message", content: [{ kind: "text", text: messageText(t), origin: { by: "plugin", id: ID }, trust: "trusted" }] };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const USAGE = `usage:
|
|
124
|
+
/schedule list tasks
|
|
125
|
+
/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
|
|
126
|
+
/schedule once <cron> <prompt> same, but only the next time it matches
|
|
127
|
+
/schedule pause|resume|rm <id>
|
|
128
|
+
/schedule run <id> send a task's prompt now (does not move its next run)
|
|
129
|
+
Tasks run in the current session of a jade that is open in the directory they were created in.`;
|
|
130
|
+
|
|
131
|
+
const err = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
|
132
|
+
|
|
133
|
+
function tools(store: ScheduleStore, cwd: string): Tool[] {
|
|
134
|
+
const result = async (fn: () => Promise<string>) => {
|
|
135
|
+
try {
|
|
136
|
+
return { content: await fn() };
|
|
137
|
+
} catch (e) {
|
|
138
|
+
return { content: err(e), isError: true };
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const where =
|
|
142
|
+
"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). " +
|
|
143
|
+
"Occurrences missed while jade was closed are run once when it opens again.";
|
|
144
|
+
return [
|
|
145
|
+
{
|
|
146
|
+
name: "schedule_create",
|
|
147
|
+
description:
|
|
148
|
+
`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} ` +
|
|
149
|
+
"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.",
|
|
150
|
+
inputSchema: {
|
|
151
|
+
type: "object",
|
|
152
|
+
properties: {
|
|
153
|
+
prompt: { type: "string", description: "the instruction to send when the task fires" },
|
|
154
|
+
cron: { type: "string", description: 'e.g. "0 9 * * 1-5" for weekdays at 09:00' },
|
|
155
|
+
title: { type: "string", description: "short name; defaults to the start of the prompt" },
|
|
156
|
+
recurring: { type: "boolean", description: "false: fire once, then end (default true)" },
|
|
157
|
+
},
|
|
158
|
+
required: ["prompt", "cron"],
|
|
159
|
+
additionalProperties: false,
|
|
160
|
+
},
|
|
161
|
+
// 任务在本会话之后、以用户的身份发出指令,所以和改工作区之外的东西一样要问
|
|
162
|
+
effect: "external",
|
|
163
|
+
concurrency: "exclusive",
|
|
164
|
+
idempotent: false,
|
|
165
|
+
execute: (input) =>
|
|
166
|
+
result(async () => {
|
|
167
|
+
const i = input as { prompt: string; cron: string; title?: string; recurring?: boolean };
|
|
168
|
+
const t = await createTask(store, { ...i, cwd });
|
|
169
|
+
return `Created scheduled task:\n${describe(t, cwd)}`;
|
|
170
|
+
}),
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: "schedule_list",
|
|
174
|
+
description: `List all scheduled tasks with their id, cron, next run time and title. ${where}`,
|
|
175
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
176
|
+
effect: "read",
|
|
177
|
+
concurrency: "safe",
|
|
178
|
+
idempotent: true,
|
|
179
|
+
execute: () => result(async () => listing(await store.list(), cwd)),
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "schedule_edit",
|
|
183
|
+
description: "Change a scheduled task (update its prompt, cron, title or recurring), pause it, resume it, or delete it. Get ids from schedule_list.",
|
|
184
|
+
inputSchema: {
|
|
185
|
+
type: "object",
|
|
186
|
+
properties: {
|
|
187
|
+
id: { type: "string" },
|
|
188
|
+
action: { type: "string", enum: ["update", "pause", "resume", "delete"] },
|
|
189
|
+
prompt: { type: "string" },
|
|
190
|
+
cron: { type: "string" },
|
|
191
|
+
title: { type: "string" },
|
|
192
|
+
recurring: { type: "boolean" },
|
|
193
|
+
},
|
|
194
|
+
required: ["id", "action"],
|
|
195
|
+
additionalProperties: false,
|
|
196
|
+
},
|
|
197
|
+
effect: "external",
|
|
198
|
+
concurrency: "exclusive",
|
|
199
|
+
idempotent: false,
|
|
200
|
+
execute: (input) =>
|
|
201
|
+
result(async () => {
|
|
202
|
+
const { id, action, ...patch } = input as { id: string; action: EditAction; prompt?: string; cron?: string; title?: string; recurring?: boolean };
|
|
203
|
+
const t = await editTask(store, id, action, patch);
|
|
204
|
+
return t ? `Updated:\n${describe(t, cwd)}` : `Deleted scheduled task ${id}.`;
|
|
205
|
+
}),
|
|
206
|
+
},
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export default definePlugin({
|
|
211
|
+
process(ctx) {
|
|
212
|
+
const store = new ScheduleStore(ctx.home);
|
|
213
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
214
|
+
name: "schedule",
|
|
215
|
+
args: "[add|once|pause|resume|rm|run] …",
|
|
216
|
+
description: "scheduled tasks: prompts sent on a cron schedule while jade is open here",
|
|
217
|
+
async run(args) {
|
|
218
|
+
const [sub = "list", ...rest] = args.trim().split(/\s+/).filter(Boolean);
|
|
219
|
+
try {
|
|
220
|
+
switch (sub) {
|
|
221
|
+
case "list":
|
|
222
|
+
return listing(await store.list(), ctx.cwd);
|
|
223
|
+
case "add":
|
|
224
|
+
case "once": {
|
|
225
|
+
if (rest.length < 6) return USAGE;
|
|
226
|
+
const t = await createTask(store, { cron: rest.slice(0, 5).join(" "), prompt: rest.slice(5).join(" "), recurring: sub === "add", cwd: ctx.cwd });
|
|
227
|
+
return `Scheduled:\n${describe(t, ctx.cwd)}`;
|
|
228
|
+
}
|
|
229
|
+
case "pause":
|
|
230
|
+
case "resume":
|
|
231
|
+
case "rm": {
|
|
232
|
+
if (rest.length !== 1) return USAGE;
|
|
233
|
+
const t = await editTask(store, rest[0]!, sub === "rm" ? "delete" : sub);
|
|
234
|
+
return t ? describe(t, ctx.cwd) : `Deleted ${rest[0]}.`;
|
|
235
|
+
}
|
|
236
|
+
case "run": {
|
|
237
|
+
const t = (await store.list()).find((x) => x.id === rest[0]);
|
|
238
|
+
if (!t) return rest.length ? `No scheduled task ${rest[0]}.` : USAGE;
|
|
239
|
+
return { prompt: messageText(t) };
|
|
240
|
+
}
|
|
241
|
+
default:
|
|
242
|
+
return USAGE;
|
|
243
|
+
}
|
|
244
|
+
} catch (e) {
|
|
245
|
+
return `schedule: ${err(e)}`;
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
async session(ctx) {
|
|
252
|
+
const store = new ScheduleStore(ctx.home);
|
|
253
|
+
for (const t of tools(store, ctx.cwd)) ctx.kernel.tool(t);
|
|
254
|
+
|
|
255
|
+
const ui = ctx.service<UI>("ui@1");
|
|
256
|
+
// jade -p 这类不能提问的运行只做用户要它做的那一件事
|
|
257
|
+
if (!ui?.interactive) return;
|
|
258
|
+
const seconds = Number(ctx.config.checkSeconds);
|
|
259
|
+
const every = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 30_000;
|
|
260
|
+
/** 已经发出、还没跑完的任务:跑完之前不再认领 */
|
|
261
|
+
const running = new Set<string>();
|
|
262
|
+
let warned = false;
|
|
263
|
+
const check = async () => {
|
|
264
|
+
if (ctx.signal.aborted) return;
|
|
265
|
+
let due: Task[];
|
|
266
|
+
try {
|
|
267
|
+
due = await claimDue(store, ctx.cwd, ctx.session.id, new Date(), running);
|
|
268
|
+
} catch (e) {
|
|
269
|
+
if (ctx.signal.aborted) return;
|
|
270
|
+
if (!warned) ui.notify(`schedule: ${err(e)}`, "warning");
|
|
271
|
+
warned = true;
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
warned = false;
|
|
275
|
+
for (const t of due) {
|
|
276
|
+
if (ctx.signal.aborted) return;
|
|
277
|
+
running.add(t.id);
|
|
278
|
+
ui.notify(`[scheduled task "${t.title}" (${t.id})]`);
|
|
279
|
+
ctx.session
|
|
280
|
+
.send(delivery(t))
|
|
281
|
+
// 会话已经离开(换会话、退出)时 ui 已经不能用了
|
|
282
|
+
.catch((e: unknown) => ctx.signal.aborted || ui.notify(`scheduled task ${t.id} failed: ${err(e)}`, "error"))
|
|
283
|
+
.finally(() => running.delete(t.id));
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
// 不在这里立即检查:session() 执行时会话还没建好,这时 send 会失败,认领了的任务就丢了
|
|
287
|
+
await ctx.effect(() => {
|
|
288
|
+
const timer = setInterval(() => void check(), every);
|
|
289
|
+
return () => clearInterval(timer);
|
|
290
|
+
});
|
|
291
|
+
},
|
|
292
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/schedule",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"optional": ["commands@1", "ui@1"],
|
|
7
|
+
"config": {
|
|
8
|
+
"checkSeconds": {
|
|
9
|
+
"default": 30,
|
|
10
|
+
"description": "how often to look for scheduled tasks that are due"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// 定时任务的存储:$JADE_HOME/state/schedules.json。同一台机器上可能开着好几个 jade,
|
|
2
|
+
// 所有读写都在一把目录锁(schedules.lock,mkdir 是原子的)里进行,到点的任务由先拿到锁的进程认领,只投递一次。
|
|
3
|
+
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { nextRun } from "./cron.ts";
|
|
8
|
+
|
|
9
|
+
export interface Task {
|
|
10
|
+
id: string;
|
|
11
|
+
title: string;
|
|
12
|
+
prompt: string;
|
|
13
|
+
/** 五字段 cron,本机时区 */
|
|
14
|
+
cron: string;
|
|
15
|
+
/** false:触发一次后结束 */
|
|
16
|
+
recurring: boolean;
|
|
17
|
+
/** 创建时的工作目录:只有在这个目录里运行的 jade 投递它 */
|
|
18
|
+
cwd: string;
|
|
19
|
+
/** 投递到哪里。现在只有 cwd:投到这个目录里正在运行的 jade 的当前会话 */
|
|
20
|
+
target: { kind: "cwd" };
|
|
21
|
+
status: "active" | "paused" | "ended";
|
|
22
|
+
/** 下一次触发的时间(ISO);只有 active 的任务有 */
|
|
23
|
+
next?: string;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
updatedAt: string;
|
|
26
|
+
/** 最近一次投递 */
|
|
27
|
+
last?: { at: string; session: string };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface File {
|
|
31
|
+
version: 1;
|
|
32
|
+
tasks: Task[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 锁超过这么久没释放,当作持有它的进程已经不在了 */
|
|
36
|
+
const STALE_MS = 10_000;
|
|
37
|
+
const WAIT_MS = 5_000;
|
|
38
|
+
|
|
39
|
+
export class ScheduleStore {
|
|
40
|
+
readonly file: string;
|
|
41
|
+
readonly #lock: string;
|
|
42
|
+
|
|
43
|
+
constructor(home: string) {
|
|
44
|
+
this.file = path.join(home, "state", "schedules.json");
|
|
45
|
+
this.#lock = path.join(home, "state", "schedules.lock");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 读全部任务 */
|
|
49
|
+
list(): Promise<Task[]> {
|
|
50
|
+
return this.#locked(() => this.#read());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 在锁里读出全部任务交给 fn,fn 改了就写回。fn 抛错时不写 */
|
|
54
|
+
update<T>(fn: (tasks: Task[]) => T): Promise<T> {
|
|
55
|
+
return this.#locked(async () => {
|
|
56
|
+
const tasks = await this.#read();
|
|
57
|
+
const before = JSON.stringify(tasks);
|
|
58
|
+
const out = fn(tasks);
|
|
59
|
+
if (JSON.stringify(tasks) !== before) {
|
|
60
|
+
const tmp = `${this.file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
61
|
+
await writeFile(tmp, `${JSON.stringify({ version: 1, tasks } satisfies File, null, 2)}\n`);
|
|
62
|
+
await rename(tmp, this.file);
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async #read(): Promise<Task[]> {
|
|
69
|
+
let text: string;
|
|
70
|
+
try {
|
|
71
|
+
text = await readFile(this.file, "utf8");
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
const data = JSON.parse(text) as Partial<File>;
|
|
77
|
+
if (data.version !== 1 || !Array.isArray(data.tasks)) throw new Error(`${this.file}: unsupported format`);
|
|
78
|
+
return data.tasks;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async #locked<T>(fn: () => Promise<T>): Promise<T> {
|
|
82
|
+
await mkdir(path.dirname(this.#lock), { recursive: true });
|
|
83
|
+
const deadline = Date.now() + WAIT_MS;
|
|
84
|
+
// 在锁目录里放一个随机标记,释放时只删自己的锁:持有太久被别人当成过期删掉、重新上了锁时,不去删别人的。
|
|
85
|
+
// 不用目录的文件号:Linux 上删掉目录马上重建,常常拿到同一个文件号
|
|
86
|
+
const mine = randomBytes(8).toString("hex");
|
|
87
|
+
for (;;) {
|
|
88
|
+
try {
|
|
89
|
+
await mkdir(this.#lock);
|
|
90
|
+
try {
|
|
91
|
+
await writeFile(path.join(this.#lock, "owner"), mine);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
await rm(this.#lock, { recursive: true, force: true });
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
} catch (e) {
|
|
98
|
+
if ((e as NodeJS.ErrnoException).code !== "EEXIST") throw e;
|
|
99
|
+
const age = await stat(this.#lock).then((s) => Date.now() - s.mtimeMs, () => 0);
|
|
100
|
+
if (age > STALE_MS) await rm(this.#lock, { recursive: true, force: true });
|
|
101
|
+
else if (Date.now() > deadline) throw new Error(`${this.#lock} is held by another jade; remove it if no jade is running`);
|
|
102
|
+
else await new Promise((r) => setTimeout(r, 20));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
return await fn();
|
|
107
|
+
} finally {
|
|
108
|
+
const owner = await readFile(path.join(this.#lock, "owner"), "utf8").catch(() => undefined);
|
|
109
|
+
if (owner === mine) await rm(this.#lock, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 两个目录是不是同一个:比较绝对路径(不看末尾的分隔符),Windows 上不分大小写。
|
|
116
|
+
* 和 jade/sessions 的 cwdKey 相同,插件之间不共享代码(只能共享类型),所以这里留一份
|
|
117
|
+
*/
|
|
118
|
+
export function sameDir(a: string, b: string): boolean {
|
|
119
|
+
const key = (p: string) => (process.platform === "win32" ? path.resolve(p).toLowerCase() : path.resolve(p));
|
|
120
|
+
return key(a) === key(b);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 下一次触发时间;没有了时 undefined */
|
|
124
|
+
export function nextAt(cron: string, from: Date): string | undefined {
|
|
125
|
+
return nextRun(cron, from)?.toISOString();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 认领 cwd 里到点的任务:记下这次投递,推进到 now 之后的下一次(错过的几次只算一次),
|
|
130
|
+
* 一次性的任务结束。skip 里的任务(上一次还没跑完)不认领,留到下次检查
|
|
131
|
+
*/
|
|
132
|
+
export function claimDue(store: ScheduleStore, cwd: string, session: string, now: Date, skip: ReadonlySet<string> = new Set()): Promise<Task[]> {
|
|
133
|
+
return store.update((tasks) => {
|
|
134
|
+
const due: Task[] = [];
|
|
135
|
+
for (const t of tasks) {
|
|
136
|
+
if (t.status !== "active" || !sameDir(t.cwd, cwd) || !t.next || Date.parse(t.next) > now.getTime() || skip.has(t.id)) continue;
|
|
137
|
+
t.last = { at: now.toISOString(), session };
|
|
138
|
+
t.updatedAt = now.toISOString();
|
|
139
|
+
const next = t.recurring ? nextAt(t.cron, now) : undefined;
|
|
140
|
+
if (next) t.next = next;
|
|
141
|
+
else {
|
|
142
|
+
t.status = "ended";
|
|
143
|
+
delete t.next;
|
|
144
|
+
}
|
|
145
|
+
due.push({ ...t });
|
|
146
|
+
}
|
|
147
|
+
return due;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// jade/scratch:每个会话一个临时目录,放不属于项目的临时文件(试跑的脚本、中间结果),免得弄脏工作目录。
|
|
2
|
+
// 目录在系统临时目录下、按会话 id 命名,恢复会话时还是同一个;不主动删除,由系统清理临时目录。
|
|
3
|
+
|
|
4
|
+
import { realpathSync } from "node:fs";
|
|
5
|
+
import { mkdir } from "node:fs/promises";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { definePlugin } from "../../src/api.ts";
|
|
9
|
+
import type { Approval } from "../approval/index.ts";
|
|
10
|
+
import type { Prompts } from "../prompts/index.ts";
|
|
11
|
+
|
|
12
|
+
/** 会话 id 对应的临时目录 */
|
|
13
|
+
export const scratchDir = (session: string): string => path.join(os.tmpdir(), "jade-scratch", session);
|
|
14
|
+
|
|
15
|
+
export default definePlugin({
|
|
16
|
+
async session(ctx) {
|
|
17
|
+
// 恢复时的 id 来自命令行,还没经过校验;只用单级、不含路径分隔符的名字,免得目录落到 jade-scratch 外面
|
|
18
|
+
if (!/^[A-Za-z0-9_-][A-Za-z0-9._-]*$/.test(ctx.session.id)) return;
|
|
19
|
+
const created = scratchDir(ctx.session.id);
|
|
20
|
+
await mkdir(created, { recursive: true });
|
|
21
|
+
// 提示词和审批都用真实路径:Windows 上 os.tmpdir() 可能是 8.3 短路径
|
|
22
|
+
const dir = realpathSync.native(created);
|
|
23
|
+
// 在里面改文件和在工作目录里一样对待;会话关闭时撤销
|
|
24
|
+
ctx.service<Approval>("approval@1")?.allowWrite(dir);
|
|
25
|
+
ctx.service<Prompts>("prompts@1")?.add({
|
|
26
|
+
text:
|
|
27
|
+
`- Scratch directory for this session: ${dir}\n` +
|
|
28
|
+
" Put temporary files there (throwaway scripts, intermediate output, files that do not belong in the project) instead of the working directory or /tmp.",
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// jade/screenshot:截图工具(见 tool.ts)。本机没有能无头截图的浏览器时不注册这个工具。
|
|
2
|
+
// 模型能不能看图片由 jade/models 的 models@1 给出(JADE_VISION):不能看时不截图。
|
|
3
|
+
// 访问网络的 URL 由 jade/approval 的关卡决定要不要问用户(effect 是 read,路径之外的 URL 会问)。
|
|
4
|
+
|
|
5
|
+
import { definePlugin } from "../../src/api.ts";
|
|
6
|
+
import type { Models } from "../models/index.ts";
|
|
7
|
+
import type { Prompts } from "../prompts/index.ts";
|
|
8
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
9
|
+
import { findBrowser, screenshotTool } from "./tool.ts";
|
|
10
|
+
|
|
11
|
+
/** 提示词里的一段:有截图工具时才加 */
|
|
12
|
+
const NOTE =
|
|
13
|
+
"- When the work is visual (a page, a drawing, styles), take a screenshot of the result and look at it instead of guessing from the source.";
|
|
14
|
+
|
|
15
|
+
export default definePlugin({
|
|
16
|
+
process(ctx) {
|
|
17
|
+
const renderers = ctx.service<Renderers>("renderers@1");
|
|
18
|
+
// 显示方式和会话无关,登记一次留到进程结束
|
|
19
|
+
renderers?.tool({
|
|
20
|
+
tool: "screenshot",
|
|
21
|
+
summary: (input) => String((input as { url?: unknown; path?: unknown } | null)?.url ?? (input as { path?: unknown } | null)?.path ?? ""),
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
session(ctx) {
|
|
26
|
+
const browser = findBrowser(process.env);
|
|
27
|
+
if (!browser) return;
|
|
28
|
+
const models = ctx.service<Models>("models@1");
|
|
29
|
+
const tool = screenshotTool({
|
|
30
|
+
cwd: ctx.cwd,
|
|
31
|
+
browser,
|
|
32
|
+
// 换了模型(/model)之后也按新的配置判断
|
|
33
|
+
available: () => models?.current()?.vision !== false,
|
|
34
|
+
});
|
|
35
|
+
if (!tool) return;
|
|
36
|
+
ctx.kernel.tool(tool);
|
|
37
|
+
const remove = ctx.service<Prompts>("prompts@1")?.add({ text: NOTE, stable: false });
|
|
38
|
+
if (remove) ctx.effect(() => () => remove());
|
|
39
|
+
},
|
|
40
|
+
});
|