@uncarved/jade 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/jade.js +5 -4
- 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 -28
- package/dist/index.js +11 -28
- 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 +156 -1223
- 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 +55 -0
- package/dist/update.js +258 -0
- package/dist/update.js.map +1 -0
- package/package.json +12 -9
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -84
- package/plugins/approval/review.ts +192 -0
- 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 +68 -39
- 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 +112 -38
- 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 +3 -4
- package/{src/tools → plugins/fs}/fs.ts +64 -46
- package/{src/tools → plugins/fs}/grep.ts +4 -3
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/plugins/fs/renderers.ts +138 -0
- 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 +79 -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 +198 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +23 -27
- 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 +142 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +83 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +253 -0
- package/plugins/prompts/sys.ts +72 -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 +36 -21
- package/{src → plugins/shell}/sys.ts +19 -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 +167 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +257 -0
- package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +53 -1
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/{src/ui → plugins/tui}/markdown.ts +258 -219
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/plugins/tui/render.ts +67 -0
- package/plugins/tui/screen.ts +268 -0
- package/{src/ui → plugins/tui}/transcript.ts +179 -111
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +684 -418
- package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
- package/plugins/undo/index.ts +139 -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/plugins/web/web.ts +713 -0
- package/plugins-dist/approval/index.js +271 -0
- package/plugins-dist/approval/plugin.json +24 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +15 -65
- package/plugins-dist/approval/review.js +176 -0
- 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 +65 -34
- 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 +95 -33
- 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 +3 -5
- package/{dist/tools → plugins-dist/fs}/fs.js +62 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +4 -4
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/plugins-dist/fs/renderers.js +132 -0
- 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 +69 -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 +167 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +22 -19
- 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 +153 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +57 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +227 -0
- package/plugins-dist/prompts/sys.js +46 -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 +36 -23
- package/{dist → plugins-dist/shell}/sys.js +20 -44
- 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 +151 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +208 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +53 -2
- package/plugins-dist/tui/frontend.js +586 -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 +57 -20
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/plugins-dist/tui/render.js +61 -0
- package/plugins-dist/tui/screen.js +201 -0
- package/{dist/ui → plugins-dist/tui}/transcript.js +179 -111
- package/{dist/ui → plugins-dist/tui}/tui.js +484 -101
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
- package/plugins-dist/undo/index.js +140 -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/plugins-dist/web/web.js +660 -0
- 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 -28
- package/src/install.ts +355 -0
- package/src/main.ts +152 -1263
- 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 -0
- package/LICENSE +0 -21
- package/README.md +0 -198
- package/dist/commands.d.ts +0 -30
- package/dist/commands.js +0 -106
- 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 -104
- package/dist/distro.js +0 -312
- 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 -251
- 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 -22
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -70
- 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 -42
- package/dist/plugins/core.js +0 -190
- package/dist/plugins/core.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 -182
- package/dist/plugins/loader.js +0 -253
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -96
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -38
- package/dist/plugins/prompts.js +0 -196
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -9
- package/dist/plugins/repeats.js +0 -90
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -16
- package/dist/plugins/review.js +0 -92
- 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/provider.d.ts +0 -111
- package/dist/provider.js.map +0 -1
- package/dist/sessions.d.ts +0 -22
- package/dist/sessions.js +0 -53
- 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 -48
- package/dist/sys.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 -35
- 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 -35
- package/dist/tools/task.js +0 -89
- 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 -23
- package/dist/tools/web.js +0 -426
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -378
- 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 -33
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -17
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -15
- package/dist/ui/render.js +0 -94
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -98
- package/dist/ui/screen.js +0 -108
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -33
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -45
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -120
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -400
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -239
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -31
- package/src/plugins/core.ts +0 -217
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -386
- package/src/plugins/prompts.ts +0 -234
- package/src/plugins/repeats.ts +0 -96
- package/src/plugins/review.ts +0 -109
- package/src/sessions.ts +0 -61
- package/src/tools/task.ts +0 -109
- package/src/tools/web.ts +0 -451
- package/src/ui/configure.ts +0 -408
- package/src/ui/render.ts +0 -83
- package/src/ui/screen.ts +0 -169
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// 跨运行时的进程和可执行文件查找:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
|
+
// 和 jade/shell 里的 sys.ts 是同一份:插件之间不共享代码(见 docs/design/jade.md 3.5)。
|
|
3
|
+
|
|
4
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
5
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
export interface Proc {
|
|
9
|
+
pid: number;
|
|
10
|
+
/** 进程退出时 resolve 退出码;被信号杀掉时为 null */
|
|
11
|
+
exited: Promise<number | null>;
|
|
12
|
+
kill(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SpawnOptions {
|
|
16
|
+
stdout?: "pipe" | "ignore";
|
|
17
|
+
stderr?: "pipe" | "ignore";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
22
|
+
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
23
|
+
*/
|
|
24
|
+
export function spawn(cmd: string[], opts: SpawnOptions = {}): Promise<Proc> {
|
|
25
|
+
const child = nodeSpawn(cmd[0]!, cmd.slice(1), {
|
|
26
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
27
|
+
// Windows 上不弹出控制台窗口
|
|
28
|
+
windowsHide: true,
|
|
29
|
+
});
|
|
30
|
+
const exited = new Promise<number | null>((resolve) => child.once("exit", (code) => resolve(code)));
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
child.once("error", reject);
|
|
33
|
+
child.once("spawn", () => {
|
|
34
|
+
child.off("error", reject);
|
|
35
|
+
// 启动之后的错误(例如 kill 一个已经退出的进程)不应该变成未处理的异常
|
|
36
|
+
child.on("error", () => {});
|
|
37
|
+
resolve({ pid: child.pid!, exited, kill: () => void child.kill() });
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** which 的结果,按 PATH 和 PATHEXT 记;找不到的过一会儿再查 */
|
|
43
|
+
const found = new Map<string, { file: string | undefined; at: number }>();
|
|
44
|
+
const MISS_TTL_MS = 30_000;
|
|
45
|
+
|
|
46
|
+
/** 在 PATH 里找可执行文件,找不到返回 undefined。Windows 上按 PATHEXT 补扩展名;带路径分隔符时只检查那个文件 */
|
|
47
|
+
export function which(bin: string): string | undefined {
|
|
48
|
+
if (bin.includes("/") || (process.platform === "win32" && bin.includes("\\"))) return lookup(bin);
|
|
49
|
+
const key = `${bin}\0${process.env.PATH ?? process.env.Path ?? ""}\0${process.env.PATHEXT ?? ""}`;
|
|
50
|
+
const hit = found.get(key);
|
|
51
|
+
if (hit && (hit.file !== undefined || Date.now() - hit.at < MISS_TTL_MS)) return hit.file;
|
|
52
|
+
const file = lookup(bin);
|
|
53
|
+
found.set(key, { file, at: Date.now() });
|
|
54
|
+
return file;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function lookup(bin: string): string | undefined {
|
|
58
|
+
const win = process.platform === "win32";
|
|
59
|
+
const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
60
|
+
const ok = (file: string) => {
|
|
61
|
+
try {
|
|
62
|
+
if (!statSync(file).isFile()) return false;
|
|
63
|
+
if (!win) accessSync(file, constants.X_OK);
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const tryAll = (base: string) => {
|
|
70
|
+
for (const ext of exts) {
|
|
71
|
+
// Windows 上没有扩展名的文件不能直接执行
|
|
72
|
+
if (win && ext === "" && !path.extname(base)) continue;
|
|
73
|
+
if (ok(base + ext)) return base + ext;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
};
|
|
77
|
+
if (bin.includes("/") || (win && bin.includes("\\"))) return tryAll(path.resolve(bin));
|
|
78
|
+
const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter).filter(Boolean);
|
|
79
|
+
for (const dir of dirs) {
|
|
80
|
+
const hit = tryAll(path.join(dir.replace(/^"(.*)"$/, "$1"), bin));
|
|
81
|
+
if (hit) return hit;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
// 截图工具:用本机的 Chrome / Edge / Chromium 无头模式渲染网页或本地文件,把截图交给模型看。
|
|
2
2
|
//
|
|
3
3
|
// 模型写网页、SVG、样式时看不到效果,只能读源码猜,常常越改越乱,或者反复打开给用户看却说不出结果。
|
|
4
|
-
//
|
|
4
|
+
// 有了截图它能自己检查。模型不能看图片时(JADE_VISION)不给截图,说明原因。
|
|
5
5
|
//
|
|
6
|
-
// effect "read":本地文件按路径、URL 按主机由
|
|
6
|
+
// effect "read":本地文件按路径、URL 按主机由 jade/approval 判断要不要问(URL 本身就能把数据带出去)。
|
|
7
7
|
// 允许 localhost:开发服务器是最常见的用途;访问网络的 URL 会先问用户。
|
|
8
8
|
// 每次用新的临时用户目录,不碰用户自己的浏览器配置和登录状态。
|
|
9
|
+
//
|
|
10
|
+
// 从旧 jade 的 tools/screenshot.ts 移植。
|
|
9
11
|
|
|
10
12
|
import { mkdtemp, readFile, rm, stat } from "node:fs/promises";
|
|
11
13
|
import os from "node:os";
|
|
12
14
|
import path from "node:path";
|
|
13
15
|
import { pathToFileURL } from "node:url";
|
|
14
16
|
import type { Block, Tool } from "uncarved";
|
|
15
|
-
import { spawn as spawnProc, which, type Proc, type SpawnOptions } from "
|
|
17
|
+
import { spawn as spawnProc, which, type Proc, type SpawnOptions } from "./sys.ts";
|
|
16
18
|
|
|
17
19
|
const DEFAULT_WIDTH = 1280;
|
|
18
20
|
const DEFAULT_HEIGHT = 800;
|
|
@@ -35,9 +37,7 @@ export interface ScreenshotOptions {
|
|
|
35
37
|
/** 常见的安装位置,按优先级。Windows 上 Edge 总是有的 */
|
|
36
38
|
function candidates(env: Record<string, string | undefined>): string[] {
|
|
37
39
|
if (process.platform === "win32") {
|
|
38
|
-
const roots = [env.ProgramFiles ?? "C:\\Program Files", env["ProgramFiles(x86)"] ?? "C:\\Program Files (x86)", env.LOCALAPPDATA].filter(
|
|
39
|
-
(r): r is string => !!r,
|
|
40
|
-
);
|
|
40
|
+
const roots = [env.ProgramFiles ?? "C:\\Program Files", env["ProgramFiles(x86)"] ?? "C:\\Program Files (x86)", env.LOCALAPPDATA].filter((r): r is string => !!r);
|
|
41
41
|
const rel = ["Google\\Chrome\\Application\\chrome.exe", "Microsoft\\Edge\\Application\\msedge.exe", "Chromium\\Application\\chrome.exe"];
|
|
42
42
|
return rel.flatMap((r) => roots.map((root) => path.join(root, r)));
|
|
43
43
|
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// jade/sessions:/clear、/resume、/retry,以及 /resume 用的会话索引。
|
|
2
|
+
// 索引是 $JADE_HOME/state/sessions.jsonl,每行 { id, cwd, title, at }:会话在本进程里第一次收到消息时追加一行。
|
|
3
|
+
// 读的时候按 id 合并:标题取最早的一行,时间取最晚的一行;目录按 cwdKey 比较。
|
|
4
|
+
|
|
5
|
+
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import type { UserInput } from "uncarved";
|
|
8
|
+
import { definePlugin } from "../../src/api.ts";
|
|
9
|
+
import type { Commands } from "../commands/index.ts";
|
|
10
|
+
|
|
11
|
+
export interface SessionSummary {
|
|
12
|
+
id: string;
|
|
13
|
+
cwd: string;
|
|
14
|
+
/** 第一条消息的开头 */
|
|
15
|
+
title: string;
|
|
16
|
+
/** 最近一次使用的时间(ISO) */
|
|
17
|
+
at: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TITLE_CHARS = 80;
|
|
21
|
+
|
|
22
|
+
/** 比较目录用的键:绝对路径、去掉末尾的分隔符;Windows 上不分大小写 */
|
|
23
|
+
export function cwdKey(cwd: string): string {
|
|
24
|
+
const p = path.resolve(cwd);
|
|
25
|
+
return process.platform === "win32" ? p.toLowerCase() : p;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 索引里的全部会话按目录分组,键是 cwdKey;每组最近用过的在前 */
|
|
29
|
+
export async function listAllSessions(file: string): Promise<Map<string, SessionSummary[]>> {
|
|
30
|
+
let text = "";
|
|
31
|
+
try {
|
|
32
|
+
text = await readFile(file, "utf8");
|
|
33
|
+
} catch {
|
|
34
|
+
return new Map();
|
|
35
|
+
}
|
|
36
|
+
const byId = new Map<string, SessionSummary>();
|
|
37
|
+
for (const line of text.split("\n")) {
|
|
38
|
+
if (!line.trim()) continue;
|
|
39
|
+
let e: SessionSummary;
|
|
40
|
+
try {
|
|
41
|
+
e = JSON.parse(line);
|
|
42
|
+
} catch {
|
|
43
|
+
continue; // 写到一半的行
|
|
44
|
+
}
|
|
45
|
+
if (typeof e?.id !== "string" || typeof e.cwd !== "string" || typeof e.at !== "string") continue;
|
|
46
|
+
const seen = byId.get(e.id);
|
|
47
|
+
byId.set(e.id, seen ? { ...seen, at: e.at > seen.at ? e.at : seen.at } : e);
|
|
48
|
+
}
|
|
49
|
+
const byCwd = new Map<string, SessionSummary[]>();
|
|
50
|
+
for (const s of byId.values()) {
|
|
51
|
+
const key = cwdKey(s.cwd);
|
|
52
|
+
const group = byCwd.get(key);
|
|
53
|
+
if (group) group.push(s);
|
|
54
|
+
else byCwd.set(key, [s]);
|
|
55
|
+
}
|
|
56
|
+
for (const group of byCwd.values()) group.sort((a, b) => b.at.localeCompare(a.at));
|
|
57
|
+
return byCwd;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 索引里 cwd 下的会话,最近用过的在前 */
|
|
61
|
+
export async function listSessions(file: string, cwd: string): Promise<SessionSummary[]> {
|
|
62
|
+
return (await listAllSessions(file)).get(cwdKey(cwd)) ?? [];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 多久以前,例如 "5m ago" */
|
|
66
|
+
export function ago(iso: string, now = Date.now()): string {
|
|
67
|
+
const s = Math.max(0, Math.round((now - Date.parse(iso)) / 1000));
|
|
68
|
+
if (s < 60) return "just now";
|
|
69
|
+
if (s < 3600) return `${Math.floor(s / 60)}m ago`;
|
|
70
|
+
if (s < 86400) return `${Math.floor(s / 3600)}h ago`;
|
|
71
|
+
return `${Math.floor(s / 86400)}d ago`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function titleOf(input: UserInput): string | undefined {
|
|
75
|
+
if (input.kind !== "message") return undefined;
|
|
76
|
+
const text = input.content.flatMap((b) => (b.kind === "text" && b.origin.by === "user" ? [b.text] : [])).join(" ").replace(/\s+/g, " ").trim();
|
|
77
|
+
if (!text) return undefined;
|
|
78
|
+
return text.length > TITLE_CHARS ? `${text.slice(0, TITLE_CHARS)}…` : text;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** sessions@1 */
|
|
82
|
+
export interface SessionIndex {
|
|
83
|
+
/** cwd 里的会话,最近用过的在前(jade --continue 取第一个) */
|
|
84
|
+
list(cwd: string): Promise<SessionSummary[]>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default definePlugin({
|
|
88
|
+
process(ctx) {
|
|
89
|
+
const file = path.join(ctx.home, "state", "sessions.jsonl");
|
|
90
|
+
ctx.provide("sessions@1", { list: (cwd: string) => listSessions(file, cwd) } satisfies SessionIndex);
|
|
91
|
+
const commands = ctx.require<Commands>("commands@1");
|
|
92
|
+
|
|
93
|
+
commands.register({
|
|
94
|
+
name: "clear",
|
|
95
|
+
description: "start a new session (the current one stays resumable)",
|
|
96
|
+
async run(_args, c) {
|
|
97
|
+
const previous = c.sessions.current;
|
|
98
|
+
await c.sessions.open();
|
|
99
|
+
return previous && previous.state.messages.length > 0 ? `Started a new session. Resume the previous one with /resume or: jade --resume ${previous.id}` : "Started a new session.";
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
commands.register({
|
|
104
|
+
name: "resume",
|
|
105
|
+
args: "[session id]",
|
|
106
|
+
description: "continue an earlier session in this directory",
|
|
107
|
+
async run(args, c) {
|
|
108
|
+
let id: string | undefined = args.trim() || undefined;
|
|
109
|
+
if (!id) {
|
|
110
|
+
const all = (await listSessions(file, ctx.cwd)).filter((s) => s.id !== c.sessions.current?.id).slice(0, 50);
|
|
111
|
+
if (all.length === 0) return "No other sessions in this directory.";
|
|
112
|
+
id = await c.ui.select({ title: "Resume which session?", options: all.map((s) => ({ label: s.title || "(untitled)", value: s.id, hint: `${ago(s.at)} · ${s.id}` })) }, c.signal);
|
|
113
|
+
if (!id) return;
|
|
114
|
+
}
|
|
115
|
+
// 前端在会话换掉之后重新显示它的对话
|
|
116
|
+
await c.sessions.open({ resume: id });
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
commands.register({
|
|
121
|
+
name: "retry",
|
|
122
|
+
description: "ask the model again after a failed request",
|
|
123
|
+
async run(_args, c) {
|
|
124
|
+
const s = c.sessions.current;
|
|
125
|
+
if (!s || s.state.messages.at(-1)?.role !== "user") return "Nothing to retry: the last request did not fail before the model answered.";
|
|
126
|
+
await s.send({ kind: "retry" });
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
session(ctx) {
|
|
132
|
+
const file = path.join(ctx.home, "state", "sessions.jsonl");
|
|
133
|
+
let indexed = false;
|
|
134
|
+
ctx.kernel.hook({
|
|
135
|
+
kind: "observe",
|
|
136
|
+
point: "event",
|
|
137
|
+
fn: async (e) => {
|
|
138
|
+
if (indexed || e.type !== "input.received") return;
|
|
139
|
+
const title = titleOf(e.data as UserInput);
|
|
140
|
+
if (title === undefined) return;
|
|
141
|
+
indexed = true;
|
|
142
|
+
const entry: SessionSummary = { id: ctx.session.id, cwd: path.resolve(ctx.cwd), title, at: new Date().toISOString() };
|
|
143
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
144
|
+
await appendFile(file, `${JSON.stringify(entry)}\n`);
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// jade/shell:bash 工具,以及后台命令的 bash_output、kill_shell;shell@1 给其他插件执行命令。
|
|
2
|
+
|
|
3
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
4
|
+
import type { Prompts } from "../prompts/index.ts";
|
|
5
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
6
|
+
import { shellRenderers } from "./renderers.ts";
|
|
7
|
+
import { shellService } from "./service.ts";
|
|
8
|
+
import { shellTools } from "./shell.ts";
|
|
9
|
+
|
|
10
|
+
export default definePlugin({
|
|
11
|
+
process(ctx) {
|
|
12
|
+
// 显示方式和会话无关,登记一次留到进程结束;没有 renderers@1(例如 print)时用前端的默认显示
|
|
13
|
+
const renderers = ctx.service<Renderers>("renderers@1");
|
|
14
|
+
if (renderers) for (const r of shellRenderers) renderers.tool(r);
|
|
15
|
+
// shell@1:找 shell 和杀进程树只有一份(见 service.ts)
|
|
16
|
+
ctx.provide("shell@1", shellService(typeof ctx.config.shell === "string" ? { shell: ctx.config.shell } : {}));
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
async session(ctx) {
|
|
20
|
+
const shell = typeof ctx.config.shell === "string" ? ctx.config.shell : undefined;
|
|
21
|
+
const { tools, executable, unavailable, shutdown } = shellTools({ cwd: ctx.cwd, ...(shell ? { shell } : {}) });
|
|
22
|
+
if (unavailable) ctx.service<UI>("ui@1")?.notify(unavailable, "warning");
|
|
23
|
+
for (const tool of tools) ctx.kernel.tool(tool);
|
|
24
|
+
if (executable) {
|
|
25
|
+
const lines = [`- Shell for the bash tool: ${executable} (POSIX syntax, also on Windows)`];
|
|
26
|
+
// 模型常用 xdg-open、open 打开文件,Git Bash 里没有;start 是 Git 自带的 /usr/bin/start
|
|
27
|
+
if (process.platform === "win32") lines.push('- To open a file or URL for the user, run: start "" <path-or-url> (xdg-open and open do not exist)');
|
|
28
|
+
ctx.service<Prompts>("prompts@1")?.add({ text: lines.join("\n") });
|
|
29
|
+
}
|
|
30
|
+
// 会话关闭(换会话、退出)时杀掉还在运行的后台命令
|
|
31
|
+
await ctx.effect(() => shutdown);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/shell",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"provides": ["shell@1"],
|
|
7
|
+
"optional": [
|
|
8
|
+
"ui@1",
|
|
9
|
+
"prompts@1",
|
|
10
|
+
"renderers@1"
|
|
11
|
+
],
|
|
12
|
+
"config": {
|
|
13
|
+
"shell": {
|
|
14
|
+
"description": "the shell the bash tool and shell@1 run; default $JADE_SHELL, else bash (Git Bash on Windows)"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// bash、bash_output、kill_shell 在对话和审批里怎么显示(登记到 renderers@1)。从旧 jade 的 ui/renderers.ts 移植。
|
|
2
|
+
//
|
|
3
|
+
// 运行中的输出不登记 progress:bash 推送的是 { kind, stream, text },前端对 { text } 形式的进度默认逐行显示,
|
|
4
|
+
// 正是想要的;自己写 progress 反而只能把一次推送(可能是好几行、半行)显示成一行。
|
|
5
|
+
|
|
6
|
+
import type { ToolRenderer } from "../renderers/index.ts";
|
|
7
|
+
|
|
8
|
+
// 截断和颜色从旧 jade 的 ui/render.ts、ui/screen.ts 复制:插件之间不共享代码,fs 插件也有一份
|
|
9
|
+
const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
10
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
11
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
12
|
+
const dim = (s: string) => (colors ? `\x1b[2m${s.replaceAll("\x1b[22m", "\x1b[22m\x1b[2m")}\x1b[22m` : s);
|
|
13
|
+
|
|
14
|
+
type Args = Record<string, unknown>;
|
|
15
|
+
const args = (input: unknown) => (input !== null && typeof input === "object" ? input : {}) as Args;
|
|
16
|
+
const command = (input: unknown) => String(args(input).command ?? "");
|
|
17
|
+
|
|
18
|
+
/** 摘要最多这么多个字符 */
|
|
19
|
+
const MAX_SUMMARY = 120;
|
|
20
|
+
|
|
21
|
+
/** 预览里的多行文字:每行前面加上标记,最多 max 行 */
|
|
22
|
+
function marked(s: string, mark: string, max = 30): string {
|
|
23
|
+
const all = s.split("\n");
|
|
24
|
+
const shown = all.slice(0, max).map((l) => `${mark} ${l}`);
|
|
25
|
+
if (all.length > max) shown.push(dim(` … ${all.length - max} more lines`));
|
|
26
|
+
return shown.join("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const shellRenderers: ToolRenderer[] = [
|
|
30
|
+
{
|
|
31
|
+
tool: "bash",
|
|
32
|
+
summary: (input) => clip(command(input).split("\n")[0]!, MAX_SUMMARY),
|
|
33
|
+
// 摘要省略了内容时(多行或很长的命令),展开后先显示完整的命令
|
|
34
|
+
details: (input) => {
|
|
35
|
+
const lines = command(input).split("\n");
|
|
36
|
+
if (lines.length === 1 && lines[0]!.length <= MAX_SUMMARY) return undefined;
|
|
37
|
+
const shown = lines.slice(0, 200).map((l, i) => dim(`${i === 0 ? "$" : " "} ${l}`));
|
|
38
|
+
if (lines.length > 200) shown.push(dim(`… ${lines.length - 200} more lines`));
|
|
39
|
+
return shown.join("\n");
|
|
40
|
+
},
|
|
41
|
+
preview: (input) => marked(command(input), "$"),
|
|
42
|
+
},
|
|
43
|
+
{ tool: "bash_output", summary: (input) => String(args(input).id ?? "") },
|
|
44
|
+
{ tool: "kill_shell", summary: (input) => String(args(input).id ?? "") },
|
|
45
|
+
];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// shell@1:给其他插件执行命令的入口(例如 jade/checks 跑项目的检查)。
|
|
2
|
+
//
|
|
3
|
+
// 找 shell、补 Git Bash 的 PATH、杀进程树这些只有一份,在 shell.ts 里;这里只把它包成服务。
|
|
4
|
+
// 和 bash 工具不同,这里的命令不经过模型和审批:调用方(插件)自己决定要不要问用户。
|
|
5
|
+
// 没有 shell 时 executable 是 undefined,run 会抛错;调用方据此说明"在这台机器上做不了"。
|
|
6
|
+
|
|
7
|
+
import { capture, findShell, shellEnv, type RunCommand, type RunResult } from "./shell.ts";
|
|
8
|
+
import { which } from "./sys.ts";
|
|
9
|
+
|
|
10
|
+
/** shell@1 */
|
|
11
|
+
export interface Shell {
|
|
12
|
+
/** 实际执行的 shell 程序;这台机器上没有时为 undefined */
|
|
13
|
+
readonly executable: string | undefined;
|
|
14
|
+
/** 找不到 shell 时的说明 */
|
|
15
|
+
readonly unavailable?: string;
|
|
16
|
+
/** 执行一条命令并收集输出;超时或中止时连同子进程一起杀掉 */
|
|
17
|
+
run: RunCommand;
|
|
18
|
+
/** 在 PATH 里找可执行文件(Windows 上按 PATHEXT 补扩展名),找不到返回 undefined */
|
|
19
|
+
which(bin: string): string | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 默认的 shell:JADE_SHELL,否则 bash(Windows 上是 Git Bash) */
|
|
23
|
+
export const shellName = (configured: unknown): string => (typeof configured === "string" && configured.trim() ? configured : (process.env.JADE_SHELL ?? "bash"));
|
|
24
|
+
|
|
25
|
+
export function shellService(opts: { shell?: string } = {}): Shell {
|
|
26
|
+
const name = shellName(opts.shell);
|
|
27
|
+
const executable = findShell(name);
|
|
28
|
+
if (!executable) {
|
|
29
|
+
const hint = process.platform === "win32" ? "install Git for Windows, or set JADE_SHELL to the path of a bash executable" : "set JADE_SHELL to the path of a shell";
|
|
30
|
+
const unavailable = `${name} was not found; ${hint}`;
|
|
31
|
+
return {
|
|
32
|
+
executable: undefined,
|
|
33
|
+
unavailable,
|
|
34
|
+
run: () => Promise.reject(new Error(unavailable)),
|
|
35
|
+
which,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const env = shellEnv(executable);
|
|
39
|
+
return {
|
|
40
|
+
executable,
|
|
41
|
+
run: (command, o): Promise<RunResult> => capture(executable, env, command, o),
|
|
42
|
+
which,
|
|
43
|
+
};
|
|
44
|
+
}
|