@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
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { existsSync } from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import type { Tool } from "uncarved";
|
|
9
|
-
import { runSync, spawn as spawnProc, which, type Proc } from "
|
|
9
|
+
import { runSync, spawn as spawnProc, which, type Proc } from "./sys.ts";
|
|
10
10
|
|
|
11
11
|
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
12
12
|
const MAX_TIMEOUT_MS = 600_000;
|
|
@@ -143,7 +143,11 @@ export function shellTools(opts: ShellOptions): ShellTools {
|
|
|
143
143
|
proc.exited,
|
|
144
144
|
]);
|
|
145
145
|
signal.throwIfAborted();
|
|
146
|
-
const parts = [
|
|
146
|
+
const parts = [
|
|
147
|
+
timedOut
|
|
148
|
+
? `Timed out after ${timeout}ms (killed). If it needs longer, raise timeout_ms (max ${MAX_TIMEOUT_MS / 1000}s); for servers and watchers use run_in_background.`
|
|
149
|
+
: `Exit code ${code}`,
|
|
150
|
+
];
|
|
147
151
|
if (stdout) parts.push(`<stdout>\n${stdout}\n</stdout>`);
|
|
148
152
|
if (stderr) parts.push(`<stderr>\n${stderr}\n</stderr>`);
|
|
149
153
|
return { content: parts.join("\n"), isError: timedOut || code !== 0 };
|
|
@@ -210,25 +214,7 @@ export function shellTools(opts: ShellOptions): ShellTools {
|
|
|
210
214
|
},
|
|
211
215
|
};
|
|
212
216
|
|
|
213
|
-
const run: RunCommand =
|
|
214
|
-
signal?.throwIfAborted();
|
|
215
|
-
const proc = await spawn(command, cwd);
|
|
216
|
-
let timedOut = false;
|
|
217
|
-
const kill = () => killTree(executable, env, proc.pid);
|
|
218
|
-
const timer = setTimeout(() => {
|
|
219
|
-
timedOut = true;
|
|
220
|
-
kill();
|
|
221
|
-
}, timeoutMs);
|
|
222
|
-
signal?.addEventListener("abort", kill, { once: true });
|
|
223
|
-
try {
|
|
224
|
-
const [stdout, stderr, code] = await Promise.all([collect(proc.stdout, () => {}), collect(proc.stderr, () => {}), proc.exited]);
|
|
225
|
-
signal?.throwIfAborted();
|
|
226
|
-
return { code: timedOut ? null : code, output: [stdout, stderr].filter(Boolean).join("\n"), timedOut };
|
|
227
|
-
} finally {
|
|
228
|
-
clearTimeout(timer);
|
|
229
|
-
signal?.removeEventListener("abort", kill);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
217
|
+
const run: RunCommand = (command, opts) => capture(executable, env, command, opts);
|
|
232
218
|
|
|
233
219
|
return {
|
|
234
220
|
tools: [bash, bashOutput, killShell],
|
|
@@ -240,6 +226,35 @@ export function shellTools(opts: ShellOptions): ShellTools {
|
|
|
240
226
|
};
|
|
241
227
|
}
|
|
242
228
|
|
|
229
|
+
/**
|
|
230
|
+
* 执行一条命令并把输出收下来(不经过模型、不推进度);超时或中止时连同子进程一起杀掉。
|
|
231
|
+
* 后台命令和别的插件(`shell@1`,例如 jade/checks)都用它
|
|
232
|
+
*/
|
|
233
|
+
export async function capture(executable: string, env: NodeJS.ProcessEnv | undefined, command: string, opts: { cwd?: string; timeoutMs: number; signal?: AbortSignal }): Promise<RunResult> {
|
|
234
|
+
opts.signal?.throwIfAborted();
|
|
235
|
+
const proc = await spawnProc([executable, "-c", command], {
|
|
236
|
+
cwd: opts.cwd,
|
|
237
|
+
env,
|
|
238
|
+
// 自成进程组,超时/中断时能连同子进程一起杀掉
|
|
239
|
+
detached: process.platform !== "win32",
|
|
240
|
+
});
|
|
241
|
+
let timedOut = false;
|
|
242
|
+
const kill = () => killTree(executable, env, proc.pid);
|
|
243
|
+
const timer = setTimeout(() => {
|
|
244
|
+
timedOut = true;
|
|
245
|
+
kill();
|
|
246
|
+
}, opts.timeoutMs);
|
|
247
|
+
opts.signal?.addEventListener("abort", kill, { once: true });
|
|
248
|
+
try {
|
|
249
|
+
const [stdout, stderr, code] = await Promise.all([collect(proc.stdout, () => {}), collect(proc.stderr, () => {}), proc.exited]);
|
|
250
|
+
opts.signal?.throwIfAborted();
|
|
251
|
+
return { code: timedOut ? null : code, output: [stdout, stderr].filter(Boolean).join("\n"), timedOut };
|
|
252
|
+
} finally {
|
|
253
|
+
clearTimeout(timer);
|
|
254
|
+
opts.signal?.removeEventListener("abort", kill);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
243
258
|
/** ctx.progress 推送的内容 */
|
|
244
259
|
export interface ShellProgress {
|
|
245
260
|
kind: "output";
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 跨运行时的进程和可执行文件查找:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
2
|
|
|
3
3
|
import { spawn as nodeSpawn, spawnSync as nodeSpawnSync } from "node:child_process";
|
|
4
|
-
import { accessSync, constants,
|
|
4
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
|
-
import picomatch from "picomatch";
|
|
7
|
-
|
|
8
|
-
/** jade 自己的版本号,src 和 dist 下都从包根目录的 package.json 读 */
|
|
9
|
-
export const VERSION: string = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
10
6
|
|
|
11
7
|
type Stdio = "pipe" | "ignore";
|
|
12
8
|
|
|
@@ -63,27 +59,30 @@ export function spawn(cmd: string[], opts: SpawnOptions = {}): Promise<Proc> {
|
|
|
63
59
|
});
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
|
|
67
|
-
export async function run(cmd: string[], opts: { cwd?: string } = {}): Promise<{ code: number | null; stdout: string }> {
|
|
68
|
-
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
69
|
-
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
70
|
-
return { code, stdout };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
62
|
/** 同步运行,忽略输出和错误;用于退出前的清理 */
|
|
74
63
|
export function runSync(cmd: string[], opts: { env?: NodeJS.ProcessEnv } = {}): void {
|
|
75
64
|
nodeSpawnSync(cmd[0]!, cmd.slice(1), { stdio: "ignore", windowsHide: true, env: opts.env });
|
|
76
65
|
}
|
|
77
66
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
for await (const chunk of stream) out += decoder.decode(chunk, { stream: true });
|
|
82
|
-
return out + decoder.decode();
|
|
83
|
-
}
|
|
67
|
+
/** which 的结果,按 PATH 和 PATHEXT 记;找不到的过一会儿再查(会话中途装上的工具能用上) */
|
|
68
|
+
const found = new Map<string, { file: string | undefined; at: number }>();
|
|
69
|
+
const MISS_TTL_MS = 30_000;
|
|
84
70
|
|
|
85
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* 在 PATH 里找可执行文件,找不到返回 undefined。Windows 上按 PATHEXT 补扩展名;带路径分隔符时只检查那个文件。
|
|
73
|
+
* 结果会记住:编辑后检查每批修改都要找 ruff、go,每次同步扫一遍 PATH 要几毫秒,期间事件循环停住
|
|
74
|
+
*/
|
|
86
75
|
export function which(bin: string): string | undefined {
|
|
76
|
+
if (bin.includes("/") || (process.platform === "win32" && bin.includes("\\"))) return lookup(bin);
|
|
77
|
+
const key = `${bin}\0${process.env.PATH ?? process.env.Path ?? ""}\0${process.env.PATHEXT ?? ""}`;
|
|
78
|
+
const hit = found.get(key);
|
|
79
|
+
if (hit && (hit.file !== undefined || Date.now() - hit.at < MISS_TTL_MS)) return hit.file;
|
|
80
|
+
const file = lookup(bin);
|
|
81
|
+
found.set(key, { file, at: Date.now() });
|
|
82
|
+
return file;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function lookup(bin: string): string | undefined {
|
|
87
86
|
const win = process.platform === "win32";
|
|
88
87
|
const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
89
88
|
const ok = (file: string) => {
|
|
@@ -111,26 +110,3 @@ export function which(bin: string): string | undefined {
|
|
|
111
110
|
}
|
|
112
111
|
return undefined;
|
|
113
112
|
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 编译 glob,返回匹配函数。路径用正斜杠、相对于搜索根目录。
|
|
117
|
-
* 和原先的 Bun.Glob#match 一致:* 也匹配点开头的文件,反斜杠是转义符而不是分隔符
|
|
118
|
-
*/
|
|
119
|
-
export function globMatcher(pattern: string): (rel: string) => boolean {
|
|
120
|
-
return picomatch(pattern, { dot: true, windows: false });
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** 找 npm 的 CLI 脚本,用当前的 node 运行它:Windows 上 npm 是 .cmd,不经过 shell 不能直接执行 */
|
|
124
|
-
export function npmCommand(): string[] | undefined {
|
|
125
|
-
const dirs = [path.dirname(process.execPath)];
|
|
126
|
-
const onPath = which(process.platform === "win32" ? "npm.cmd" : "npm");
|
|
127
|
-
if (onPath) dirs.push(path.dirname(onPath));
|
|
128
|
-
for (const dir of dirs) {
|
|
129
|
-
for (const cli of [path.join(dir, "node_modules", "npm", "bin", "npm-cli.js"), path.join(dir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js")]) {
|
|
130
|
-
if (existsSync(cli)) return [process.execPath, cli];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// POSIX 上 npm 本身就是可执行的脚本
|
|
134
|
-
if (onPath && process.platform !== "win32") return [onPath];
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// jade/skills:skill 工具、系统提示词里的 skill 列表、/skills,以及每个 skill 一条 /<skill 名> 命令。
|
|
2
|
+
//
|
|
3
|
+
// 每个会话打开时重新查找 skill(/clear 之后能用上新装的)。随仓库分发的 skill(项目层)不用确认:
|
|
4
|
+
// 它只是给模型看的文字,不执行代码;和旧 jade 一样,列表里有项目层的 skill 时整段标为不可信。
|
|
5
|
+
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
8
|
+
import type { Approval } from "../approval/index.ts";
|
|
9
|
+
import type { Commands } from "../commands/index.ts";
|
|
10
|
+
import type { Prompts } from "../prompts/index.ts";
|
|
11
|
+
import { bold, clip, dim, display, realPath, repoRoot } from "./project.ts";
|
|
12
|
+
import { loadSkills, skillsPrompt, skillTool, type Skill } from "./skills.ts";
|
|
13
|
+
|
|
14
|
+
const ID = "jade/skills";
|
|
15
|
+
|
|
16
|
+
/** /<skill> 发给模型的消息:让它先加载这个 skill */
|
|
17
|
+
export function skillMessage(name: string, args: string): string {
|
|
18
|
+
return `Use the "${name}" skill${args.trim() ? ` for this: ${args.trim()}` : "."}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function find(home: string, cwd: string) {
|
|
22
|
+
const here = realPath(cwd);
|
|
23
|
+
return loadSkills({ home, root: realPath(repoRoot(here)), cwd: here });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 最近一个会话找到的 skill,/skills 显示它 */
|
|
27
|
+
let latest: Skill[] | undefined;
|
|
28
|
+
/** 最近一个会话注册的 /<skill> 命令 → 撤销函数 */
|
|
29
|
+
const skillCommands = new Map<string, () => void>();
|
|
30
|
+
/** 已经告诉过用户的警告:每个会话都会重新查找,同样的警告不重复说 */
|
|
31
|
+
const warned = new Set<string>();
|
|
32
|
+
|
|
33
|
+
export default definePlugin({
|
|
34
|
+
process(ctx) {
|
|
35
|
+
latest = undefined;
|
|
36
|
+
skillCommands.clear();
|
|
37
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
38
|
+
name: "skills",
|
|
39
|
+
description: "skills the agent can use",
|
|
40
|
+
instant: true,
|
|
41
|
+
async run() {
|
|
42
|
+
const skills = latest ?? (await find(ctx.home, ctx.cwd)).skills;
|
|
43
|
+
if (skills.length === 0) {
|
|
44
|
+
return dim(`(no skills) put them in ${display(ctx.cwd, path.join(ctx.home, "skills"))}/<name>/SKILL.md or .jade/skills/<name>/SKILL.md`);
|
|
45
|
+
}
|
|
46
|
+
return skills
|
|
47
|
+
.map((sk) => `${bold(sk.name)} ${dim(`${sk.layer} · ${display(ctx.cwd, sk.dir)}`)}\n ${clip(sk.description.replace(/\s+/g, " "), 300)}`)
|
|
48
|
+
.join("\n");
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
async session(ctx) {
|
|
54
|
+
const { skills, warnings } = await find(ctx.home, ctx.cwd);
|
|
55
|
+
latest = skills;
|
|
56
|
+
const ui = ctx.service<UI>("ui@1");
|
|
57
|
+
for (const w of warnings) {
|
|
58
|
+
if (warned.has(w)) continue;
|
|
59
|
+
warned.add(w);
|
|
60
|
+
ui?.notify(`${ID}: ${w}`, "warning");
|
|
61
|
+
}
|
|
62
|
+
if (skills.length === 0) return;
|
|
63
|
+
|
|
64
|
+
ctx.kernel.tool(skillTool(skills));
|
|
65
|
+
const text = skillsPrompt(skills)!;
|
|
66
|
+
// 读 skill 目录里的文件(脚本、参考资料)不用问;会话关闭时撤销
|
|
67
|
+
const approval = ctx.service<Approval>("approval@1");
|
|
68
|
+
for (const sk of skills) approval?.allowRead(sk.dir);
|
|
69
|
+
// 有随仓库分发的 skill 时整段不可信
|
|
70
|
+
ctx.service<Prompts>("prompts@1")?.add({ text, untrusted: !skills.every((s) => s.layer === "user") });
|
|
71
|
+
|
|
72
|
+
// skill 也可以当命令用:/<skill 名> [要做的事]。挂在会话作用域上,随会话撤销。
|
|
73
|
+
// 新会话在旧会话关闭之前打开:旧会话注册的同名命令先撤销,换成这个会话的。
|
|
74
|
+
// 名字已被别的插件的命令占用时让给它(会话级的注册总在进程级的命令之后)
|
|
75
|
+
const commands = ctx.service<Commands>("commands@1");
|
|
76
|
+
if (commands) {
|
|
77
|
+
for (const sk of skills) {
|
|
78
|
+
const key = sk.name.toLowerCase();
|
|
79
|
+
const taken = commands.get(sk.name);
|
|
80
|
+
if (taken && taken.plugin !== ID) continue;
|
|
81
|
+
if (taken) skillCommands.get(key)?.();
|
|
82
|
+
try {
|
|
83
|
+
const revoke = commands.register({
|
|
84
|
+
name: sk.name,
|
|
85
|
+
args: "[task]",
|
|
86
|
+
description: `${clip(sk.description.replace(/\s+/g, " "), 80)} (skill)`,
|
|
87
|
+
run: (args) => ({ prompt: skillMessage(sk.name, args) }),
|
|
88
|
+
});
|
|
89
|
+
skillCommands.set(key, revoke);
|
|
90
|
+
} catch {
|
|
91
|
+
// 名字不能当命令名(commands@1 的规则更严)或刚被占用:只是少一条快捷命令
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// 仓库目录、显示用的小工具。插件之间不共享代码(只能共享类型),jade/mcp、jade/checks 各有一份。
|
|
2
|
+
|
|
3
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
export function realPath(p: string): string {
|
|
7
|
+
try {
|
|
8
|
+
return realpathSync.native(p);
|
|
9
|
+
} catch {
|
|
10
|
+
return path.resolve(p);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** git 仓库的根目录(往上找 .git,目录或文件都算);不在仓库里时是 cwd 本身 */
|
|
15
|
+
export function repoRoot(cwd: string): string {
|
|
16
|
+
let d = path.resolve(cwd);
|
|
17
|
+
for (;;) {
|
|
18
|
+
if (existsSync(path.join(d, ".git"))) return d;
|
|
19
|
+
const parent = path.dirname(d);
|
|
20
|
+
if (parent === d) return path.resolve(cwd);
|
|
21
|
+
d = parent;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 从仓库根目录到工作目录的每一级目录,远的在前;工作目录不在仓库里时只有工作目录 */
|
|
26
|
+
export function projectDirs(root: string, cwd: string): string[] {
|
|
27
|
+
const rel = path.relative(root, cwd);
|
|
28
|
+
const inside = rel === "" || !(rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
|
|
29
|
+
if (!inside) return [cwd];
|
|
30
|
+
const dirs = [root];
|
|
31
|
+
if (rel) for (const seg of rel.split(path.sep)) dirs.push(path.join(dirs.at(-1)!, seg));
|
|
32
|
+
return dirs;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 工作目录里的显示成相对路径,外面的显示绝对路径 */
|
|
36
|
+
export function display(cwd: string, full: string): string {
|
|
37
|
+
const rel = path.relative(cwd, full);
|
|
38
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
39
|
+
return shown.split(path.sep).join("/");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
43
|
+
|
|
44
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
45
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
46
|
+
const paint = (open: string, close: string) => (s: string) =>
|
|
47
|
+
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
48
|
+
export const dim = paint("2", "22");
|
|
49
|
+
export const bold = paint("1", "22");
|
|
@@ -11,12 +11,13 @@
|
|
|
11
11
|
// 查找位置(同名时后面的覆盖前面的):
|
|
12
12
|
// ~/.claude/skills、$JADE_HOME/skills(用户层);
|
|
13
13
|
// 仓库根目录到工作目录每一级的 .claude/skills、.jade/skills(项目层)。
|
|
14
|
+
// 从旧 jade 的 src/skills.ts 移植;旧 jade 的"插件自带 skills 目录"(PluginBundle)没有移植:新的插件宿主没有内容包的概念。
|
|
14
15
|
|
|
15
16
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
16
17
|
import os from "node:os";
|
|
17
18
|
import path from "node:path";
|
|
18
19
|
import type { Tool } from "uncarved";
|
|
19
|
-
import { projectDirs } from "./
|
|
20
|
+
import { projectDirs } from "./project.ts";
|
|
20
21
|
|
|
21
22
|
export interface Skill {
|
|
22
23
|
name: string;
|
|
@@ -26,8 +27,6 @@ export interface Skill {
|
|
|
26
27
|
layer: "user" | "project";
|
|
27
28
|
/** 前一个同名 skill 被它覆盖 */
|
|
28
29
|
overrides?: string;
|
|
29
|
-
/** 来自哪个插件 */
|
|
30
|
-
plugin?: string;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
@@ -42,15 +41,10 @@ export async function loadSkills(opts: {
|
|
|
42
41
|
cwd: string;
|
|
43
42
|
/** 用户的主目录,读 ~/.claude/skills 用;测试时可以换掉 */
|
|
44
43
|
userHome?: string;
|
|
45
|
-
/** 插件带的 skills 目录,优先级低于同一层的 skills 目录 */
|
|
46
|
-
plugins?: { plugin: string; dir: string; layer: Skill["layer"] }[];
|
|
47
44
|
}): Promise<{ skills: Skill[]; warnings: string[] }> {
|
|
48
|
-
const
|
|
49
|
-
const dirs: { dir: string; layer: Skill["layer"]; plugin?: string }[] = [
|
|
50
|
-
...fromPlugins("user"),
|
|
45
|
+
const dirs: { dir: string; layer: Skill["layer"] }[] = [
|
|
51
46
|
{ dir: path.join(opts.userHome ?? os.homedir(), ".claude", "skills"), layer: "user" },
|
|
52
47
|
{ dir: path.join(opts.home, "skills"), layer: "user" },
|
|
53
|
-
...fromPlugins("project"),
|
|
54
48
|
];
|
|
55
49
|
for (const d of projectDirs(opts.root, opts.cwd)) {
|
|
56
50
|
dirs.push({ dir: path.join(d, ".claude", "skills"), layer: "project" }, { dir: path.join(d, ".jade", "skills"), layer: "project" });
|
|
@@ -58,7 +52,7 @@ export async function loadSkills(opts: {
|
|
|
58
52
|
const out = new Map<string, Skill>();
|
|
59
53
|
const warnings: string[] = [];
|
|
60
54
|
const seen = new Set<string>();
|
|
61
|
-
for (const { dir, layer
|
|
55
|
+
for (const { dir, layer } of dirs) {
|
|
62
56
|
// JADE_HOME 就是 ~/.claude 之类的情况,同一个目录不读两次
|
|
63
57
|
if (seen.has(path.resolve(dir))) continue;
|
|
64
58
|
seen.add(path.resolve(dir));
|
|
@@ -96,7 +90,6 @@ export async function loadSkills(opts: {
|
|
|
96
90
|
dir: skillDir,
|
|
97
91
|
layer,
|
|
98
92
|
overrides: prev?.dir,
|
|
99
|
-
...(plugin ? { plugin } : {}),
|
|
100
93
|
});
|
|
101
94
|
}
|
|
102
95
|
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// jade/suggest:每一轮正常结束后,预测用户下一条想发的消息,灰色显示在输入框里,Tab 或 → 接受。默认关着(enabled)。
|
|
2
|
+
//
|
|
3
|
+
// 模型经 models@1:配置 model 是 /model 保存过的模型名,不设时用会话的模型。
|
|
4
|
+
// /suggest 在 TUI 里开关、选模型,马上生效,记在 $JADE_HOME/state/suggest.json,盖过 config.json 里的 enabled 和 model。
|
|
5
|
+
// 对话从 context.project(每次请求模型前的消息)和 model.after(这一轮最后的回复)取,轮次结束从 turn.ended 事件知道。
|
|
6
|
+
// 从旧 jade 的 examples/plugins/suggest 移植;提示词、对话的整理和截取、回复的清理参考 dsh-ghost-prompt。
|
|
7
|
+
|
|
8
|
+
import { randomBytes } from "node:crypto";
|
|
9
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import type { Delta, Message, ModelRequest } from "uncarved";
|
|
12
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
13
|
+
import type { Commands } from "../commands/index.ts";
|
|
14
|
+
import type { Models } from "../models/index.ts";
|
|
15
|
+
|
|
16
|
+
export const SYSTEM = [
|
|
17
|
+
"You predict the next message the USER will type to an AI coding assistant.",
|
|
18
|
+
"You are given the recent conversation. Write the single message the user is most likely to send next, exactly as they would type it.",
|
|
19
|
+
"Rules:",
|
|
20
|
+
'- Write from the user\'s point of view, addressed to the assistant (e.g. "run the tests", "commit this", "yes, go ahead").',
|
|
21
|
+
"- Match the language and tone the user has been writing in. If the user writes Chinese, answer in Chinese.",
|
|
22
|
+
"- Keep it short: one sentence, at most about 20 words or 40 CJK characters.",
|
|
23
|
+
"- Prefer the obvious next step: answering a question the assistant just asked, approving a proposed plan, verifying or continuing the work.",
|
|
24
|
+
"- Never repeat a request the user already made, and never invent facts, file names or numbers not in the conversation.",
|
|
25
|
+
"- If no next message is reasonably predictable, output exactly: NONE",
|
|
26
|
+
'Output only the message text on one line. No quotes, no prefix like "User:", no explanation, no Markdown.',
|
|
27
|
+
].join("\n");
|
|
28
|
+
|
|
29
|
+
/** 送给预测模型的最近对话(字符),从最新的往前取 */
|
|
30
|
+
const MAX_CONTEXT_CHARS = 8000;
|
|
31
|
+
/** 每条消息最多这么多字,超出时留头尾 */
|
|
32
|
+
const PER_MESSAGE_CHARS = 2400;
|
|
33
|
+
/** 一次预测的时限 */
|
|
34
|
+
const TIMEOUT_MS = 30_000;
|
|
35
|
+
|
|
36
|
+
export type Entry = { role: "user" | "assistant"; text: string };
|
|
37
|
+
|
|
38
|
+
/** 太长的消息留头和尾,中间最不要紧 */
|
|
39
|
+
function clip(text: string, max: number): string {
|
|
40
|
+
if (text.length <= max) return text;
|
|
41
|
+
const half = Math.floor((max - 5) / 2);
|
|
42
|
+
return `${text.slice(0, half)}\n…\n${text.slice(-half)}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 一条消息里给预测模型看的文字:用户消息只取用户自己打的字(工具结果、插件附的说明不算),回复里的工具调用只留名字 */
|
|
46
|
+
function textOf(m: Message): string {
|
|
47
|
+
const parts: string[] = [];
|
|
48
|
+
for (const b of m.content) {
|
|
49
|
+
if (b.kind === "text" && b.text.trim() && (m.role === "assistant" || b.origin.by === "user")) parts.push(b.text.trim());
|
|
50
|
+
else if (b.kind === "tool_use" && m.role === "assistant") parts.push(`[called tool: ${b.name}]`);
|
|
51
|
+
}
|
|
52
|
+
return parts.join("\n");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 整理成用户、助手交替的对话:没有文字的消息去掉,相邻同一方的合并(一轮里助手的几步并成一条) */
|
|
56
|
+
export function transcriptOf(messages: readonly Message[]): Entry[] {
|
|
57
|
+
const out: Entry[] = [];
|
|
58
|
+
for (const m of messages) {
|
|
59
|
+
const text = textOf(m);
|
|
60
|
+
if (!text) continue;
|
|
61
|
+
const last = out.at(-1);
|
|
62
|
+
if (last?.role === m.role) last.text += `\n${text}`;
|
|
63
|
+
else out.push({ role: m.role, text });
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const frameLine = (e: Entry) => `<${e.role}>\n${clip(e.text, PER_MESSAGE_CHARS)}\n</${e.role}>`;
|
|
69
|
+
|
|
70
|
+
/** 从最新的往前放,放不下就停;最后一条总是放进去 */
|
|
71
|
+
export function frame(transcript: readonly Entry[], budget = MAX_CONTEXT_CHARS): string {
|
|
72
|
+
const lines: string[] = [];
|
|
73
|
+
let used = 0;
|
|
74
|
+
for (let i = transcript.length - 1; i >= 0; i--) {
|
|
75
|
+
const line = frameLine(transcript[i]!);
|
|
76
|
+
if (lines.length > 0 && used + line.length > budget) break;
|
|
77
|
+
lines.unshift(line);
|
|
78
|
+
used += line.length;
|
|
79
|
+
}
|
|
80
|
+
return ["Recent conversation, oldest first:", "", lines.join("\n\n"), "", "Predict the user's next message."].join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const LEADING_PREFIX = /^(?:user|用户)\s*[::]\s*/i;
|
|
84
|
+
const LEADING_QUOTES = /^["'“”‘’`「『]+/;
|
|
85
|
+
const TRAILING_QUOTES = /["'“”‘’`」』]+$/;
|
|
86
|
+
|
|
87
|
+
/** 模型回复的第一行整理成建议:去掉 "User:" 前缀和包着的引号;NONE 或空的时候 undefined */
|
|
88
|
+
export function tidy(reply: string): string | undefined {
|
|
89
|
+
let line = reply.trimStart().split(/\r?\n/)[0]!.trim().replace(LEADING_PREFIX, "");
|
|
90
|
+
if (LEADING_QUOTES.test(line)) line = line.replace(LEADING_QUOTES, "").replace(TRAILING_QUOTES, "").trim();
|
|
91
|
+
if (!line || /^none\.?$/i.test(line)) return undefined;
|
|
92
|
+
return line.length > 300 ? `${line.slice(0, 299)}…` : line;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 生效的设置:model 为 undefined 时用会话的模型 */
|
|
96
|
+
export interface Settings {
|
|
97
|
+
enabled: boolean;
|
|
98
|
+
model: string | undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** process() 里读好、/suggest 改的设置,session() 里每次预测时读 */
|
|
102
|
+
let settings: Settings = { enabled: false, model: undefined };
|
|
103
|
+
/** 设置改了时通知每个会话(关掉时去掉已经显示的建议) */
|
|
104
|
+
const changed = new Set<() => void>();
|
|
105
|
+
|
|
106
|
+
/** state/suggest.json 里 /suggest 记下的设置;没有、读不了或格式不对的键不算 */
|
|
107
|
+
async function readState(file: string): Promise<Partial<Settings>> {
|
|
108
|
+
try {
|
|
109
|
+
const data = JSON.parse(await readFile(file, "utf8")) as { enabled?: unknown; model?: unknown };
|
|
110
|
+
return {
|
|
111
|
+
...(typeof data.enabled === "boolean" ? { enabled: data.enabled } : {}),
|
|
112
|
+
// 空字符串表示用会话的模型
|
|
113
|
+
...(typeof data.model === "string" ? { model: data.model.trim() || undefined } : {}),
|
|
114
|
+
};
|
|
115
|
+
} catch {
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function writeState(file: string, s: Settings): Promise<void> {
|
|
121
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
122
|
+
const tmp = `${file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
123
|
+
await writeFile(tmp, `${JSON.stringify({ enabled: s.enabled, model: s.model ?? "" }, null, 2)}\n`);
|
|
124
|
+
await rename(tmp, file);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default definePlugin({
|
|
128
|
+
async process(ctx) {
|
|
129
|
+
const file = path.join(ctx.home, "state", "suggest.json");
|
|
130
|
+
const raw = ctx.config.model;
|
|
131
|
+
if (raw !== undefined && typeof raw !== "string") ctx.service<UI>("ui@1")?.notify(`jade/suggest: model must be a model name, got ${JSON.stringify(raw)}; using the session's model`, "warning");
|
|
132
|
+
settings = {
|
|
133
|
+
enabled: ctx.config.enabled === true,
|
|
134
|
+
model: typeof raw === "string" && raw.trim() ? raw.trim() : undefined,
|
|
135
|
+
...(await readState(file)),
|
|
136
|
+
};
|
|
137
|
+
const models = ctx.require<Models>("models@1");
|
|
138
|
+
const describe = () => `suggestions are ${settings.enabled ? "on" : "off"}, predicted by ${settings.model ? `the saved model ${settings.model}` : "the session's model"}`;
|
|
139
|
+
const update = async (next: Partial<Settings>) => {
|
|
140
|
+
settings = { ...settings, ...next };
|
|
141
|
+
for (const fn of changed) fn();
|
|
142
|
+
await writeState(file, settings);
|
|
143
|
+
return `${describe()}.`;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
147
|
+
name: "suggest",
|
|
148
|
+
args: "[on|off|model [name]]",
|
|
149
|
+
description: "predict your next message after each turn (one more model request per turn), and which model predicts",
|
|
150
|
+
// 只改插件自己的设置
|
|
151
|
+
instant: true,
|
|
152
|
+
async run(args, c) {
|
|
153
|
+
const [sub = "", ...rest] = args.trim().split(/\s+/);
|
|
154
|
+
if (sub === "on" || sub === "off") return update({ enabled: sub === "on" });
|
|
155
|
+
if (sub === "model") {
|
|
156
|
+
const saved = (await models.saved?.()) ?? [];
|
|
157
|
+
let name: string | undefined = rest.join(" ");
|
|
158
|
+
if (!name) {
|
|
159
|
+
const SESSION = "\0session";
|
|
160
|
+
const picked = await c.ui.select(
|
|
161
|
+
{
|
|
162
|
+
title: "Which model predicts your next message?",
|
|
163
|
+
options: [
|
|
164
|
+
{ label: "The session's model", value: SESSION },
|
|
165
|
+
...saved.map((m) => ({ label: m, value: m, ...(m === settings.model ? { hint: "current" } : {}) })),
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
c.signal,
|
|
169
|
+
);
|
|
170
|
+
if (picked === undefined) return;
|
|
171
|
+
name = picked === SESSION ? undefined : picked;
|
|
172
|
+
} else if (models.saved && !saved.includes(name)) {
|
|
173
|
+
return `${name} is not a model saved by /model. ${saved.length ? `Saved: ${saved.join(", ")}.` : "Add one with /model first."}`;
|
|
174
|
+
}
|
|
175
|
+
return update({ model: name });
|
|
176
|
+
}
|
|
177
|
+
if (sub) return "usage: /suggest [on|off|model [name]]";
|
|
178
|
+
return `${describe()}. /suggest on|off turns them on or off; /suggest model picks the model.`;
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
session(ctx) {
|
|
184
|
+
const input = ctx.input;
|
|
185
|
+
// 没有输入框的前端(-p)用不上
|
|
186
|
+
if (!input) return;
|
|
187
|
+
const models = ctx.require<Models>("models@1");
|
|
188
|
+
|
|
189
|
+
/** 最近一次发给模型的消息,加上这一轮最后的回复 */
|
|
190
|
+
let messages: Message[] = [];
|
|
191
|
+
let reply: Message | undefined;
|
|
192
|
+
/** 正在进行的预测:新的一次请求开始、会话结束时中止 */
|
|
193
|
+
let pending: AbortController | undefined;
|
|
194
|
+
ctx.signal.addEventListener("abort", () => pending?.abort());
|
|
195
|
+
// /suggest off:停下正在进行的预测,去掉已经显示的建议
|
|
196
|
+
const onChange = () => {
|
|
197
|
+
if (settings.enabled) return;
|
|
198
|
+
pending?.abort();
|
|
199
|
+
input.hint(undefined);
|
|
200
|
+
};
|
|
201
|
+
changed.add(onChange);
|
|
202
|
+
ctx.signal.addEventListener("abort", () => changed.delete(onChange));
|
|
203
|
+
/** 上一次提示过的错误:同样的错误每轮都提示太吵,成功一次后清掉 */
|
|
204
|
+
let warned: string | undefined;
|
|
205
|
+
const warn = (e: unknown) => {
|
|
206
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
207
|
+
if (message === warned) return;
|
|
208
|
+
warned = message;
|
|
209
|
+
ctx.service<UI>("ui@1")?.notify(`jade/suggest: prediction failed (${settings.model ? `model ${settings.model}` : "the session's model"}): ${message}`, "warning");
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
async function predict() {
|
|
213
|
+
if (!settings.enabled) return;
|
|
214
|
+
const { model } = settings;
|
|
215
|
+
const transcript = transcriptOf([...messages, ...(reply ? [reply] : [])]);
|
|
216
|
+
// 用户还没说过话,或者最后不是助手的回复:没有可回答的
|
|
217
|
+
if (!transcript.some((e) => e.role === "user") || transcript.at(-1)?.role !== "assistant") return;
|
|
218
|
+
pending?.abort();
|
|
219
|
+
const mine = (pending = new AbortController());
|
|
220
|
+
// 第一行完整了就停:建议只要一行,后面的都会丢掉
|
|
221
|
+
const firstLine = new AbortController();
|
|
222
|
+
let streamed = "";
|
|
223
|
+
const onDelta = (d: Delta) => {
|
|
224
|
+
if (d.kind === "reset") streamed = "";
|
|
225
|
+
else if (d.kind === "text") streamed += d.text;
|
|
226
|
+
if (/\S[^\n]*\n/.test(streamed)) firstLine.abort();
|
|
227
|
+
};
|
|
228
|
+
const origin = { by: "plugin" as const, id: ctx.plugin };
|
|
229
|
+
const request: ModelRequest = {
|
|
230
|
+
system: [{ text: SYSTEM, stability: "stable", origin, trust: "trusted" }],
|
|
231
|
+
messages: [{ role: "user", content: [{ kind: "text", text: frame(transcript), origin, trust: "trusted" }] }],
|
|
232
|
+
tools: [],
|
|
233
|
+
};
|
|
234
|
+
let text: string;
|
|
235
|
+
const timeout = AbortSignal.timeout(TIMEOUT_MS);
|
|
236
|
+
try {
|
|
237
|
+
const res = await models.complete(request, {
|
|
238
|
+
signal: AbortSignal.any([mine.signal, firstLine.signal, timeout]),
|
|
239
|
+
onDelta,
|
|
240
|
+
...(model ? { model } : {}),
|
|
241
|
+
});
|
|
242
|
+
text = res.content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("");
|
|
243
|
+
} catch (e) {
|
|
244
|
+
// 第一行完整后自己停下的,用已经收到的
|
|
245
|
+
if (firstLine.signal.aborted && !mine.signal.aborted) text = streamed;
|
|
246
|
+
else {
|
|
247
|
+
// 超时、被中止不打扰用户;模型出错(配置不对之类)说一次,否则开着却什么都不显示,看不出为什么
|
|
248
|
+
if (!mine.signal.aborted && !timeout.aborted && !ctx.signal.aborted) warn(e);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
warned = undefined;
|
|
253
|
+
const hint = tidy(text);
|
|
254
|
+
if (hint && !mine.signal.aborted) input!.hint(hint);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
ctx.kernel.hook({
|
|
258
|
+
// 每次请求模型前:记下这次发出的消息,不改它们。新的请求说明新的一轮(或下一步)开始了,之前的建议作废
|
|
259
|
+
kind: "transform",
|
|
260
|
+
point: "context.project",
|
|
261
|
+
optional: true,
|
|
262
|
+
fn: (msgs) => {
|
|
263
|
+
messages = msgs;
|
|
264
|
+
reply = undefined;
|
|
265
|
+
pending?.abort();
|
|
266
|
+
input.hint(undefined);
|
|
267
|
+
return undefined;
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
ctx.kernel.hook({
|
|
271
|
+
kind: "observe",
|
|
272
|
+
point: "model.after",
|
|
273
|
+
fn: (res) => {
|
|
274
|
+
reply = { role: "assistant", content: res.content };
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
ctx.kernel.hook({
|
|
278
|
+
// 这一轮正常结束(不是中断、出错或被截断)时预测。不 await:hook 有时限,预测在后台进行
|
|
279
|
+
kind: "observe",
|
|
280
|
+
point: "event",
|
|
281
|
+
fn: (e) => {
|
|
282
|
+
if (e.type === "turn.ended" && (e.data as { reason?: unknown }).reason === "end") void predict();
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
},
|
|
286
|
+
});
|