@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,198 @@
|
|
|
1
|
+
// jade/models:按 $JADE_HOME/.env 和环境变量选模型服务(和旧 jade 的变量相同),外面包一层重试和限速。
|
|
2
|
+
// provider@1 给核心建会话用;models@1 给其他插件(压缩、子 Agent、提示词)调用模型、查看当前配置。
|
|
3
|
+
//
|
|
4
|
+
// /model 改了配置之后不用重开会话:provider@1 给核心的是一个转发到当前 provider 的壳(设计第 10 节),
|
|
5
|
+
// 下一次请求就用新的。
|
|
6
|
+
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import type { ModelRequest, ModelResponse, Provider } from "uncarved";
|
|
9
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
10
|
+
import type { Commands } from "../commands/index.ts";
|
|
11
|
+
import { contextWindow } from "./context.ts";
|
|
12
|
+
import { envWithFile, hasProvider } from "./config.ts";
|
|
13
|
+
import { configurator } from "./configure.ts";
|
|
14
|
+
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry } from "./provider.ts";
|
|
15
|
+
import { readSavedServices, type Env } from "./saved.ts";
|
|
16
|
+
|
|
17
|
+
/** models@1 */
|
|
18
|
+
export interface Models {
|
|
19
|
+
/** 当前配置的服务和模型;没有配置时 undefined。via 是给人看的服务说明,例如 "the Anthropic API" */
|
|
20
|
+
current(): {
|
|
21
|
+
kind: string;
|
|
22
|
+
model: string;
|
|
23
|
+
via: string;
|
|
24
|
+
/** 上下文窗口(token) */
|
|
25
|
+
contextWindow: number;
|
|
26
|
+
/** 模型能不能看图片(JADE_VISION;不设时按服务推断) */
|
|
27
|
+
vision: boolean;
|
|
28
|
+
} | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* 完成一次请求,带重试和限速。默认用当前配置的模型(限速和会话共用);
|
|
31
|
+
* model 是 /model 保存过的模型名($JADE_HOME/models.json),用它和它自己的设置,找不到时报错
|
|
32
|
+
*/
|
|
33
|
+
complete(request: ModelRequest, options: { signal: AbortSignal; onDelta?: (delta: import("uncarved").Delta) => void; model?: string }): Promise<ModelResponse>;
|
|
34
|
+
/** 服务提供的模型列表;不支持或出错时为空 */
|
|
35
|
+
list(): Promise<string[]>;
|
|
36
|
+
/** /model 保存过的模型名(complete 的 model 能用的那些),最近用过的在前 */
|
|
37
|
+
saved?(): Promise<string[]>;
|
|
38
|
+
/** 配置换了(/model)时调用,返回取消订阅。前端用它更新状态栏 */
|
|
39
|
+
subscribe?(fn: () => void): () => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default definePlugin({
|
|
43
|
+
async process(ctx) {
|
|
44
|
+
const envFile = path.join(ctx.home, ".env");
|
|
45
|
+
const modelsFile = path.join(ctx.home, "models.json");
|
|
46
|
+
// 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
|
|
47
|
+
let env = await envWithFile(envFile);
|
|
48
|
+
const ui = ctx.service<UI>("ui@1");
|
|
49
|
+
const watchers = new Set<() => void>();
|
|
50
|
+
const notify = () => {
|
|
51
|
+
for (const fn of watchers) {
|
|
52
|
+
try {
|
|
53
|
+
fn();
|
|
54
|
+
} catch {
|
|
55
|
+
// 订阅者的错不连累模型配置
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const notConfigured = () =>
|
|
61
|
+
new Error(
|
|
62
|
+
`no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:\n` +
|
|
63
|
+
" ANTHROPIC_API_KEY=... (Claude)\n" +
|
|
64
|
+
" OPENAI_API_KEY=... (OpenAI)\n" +
|
|
65
|
+
" OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)",
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
let made: Promise<Provider> | undefined;
|
|
69
|
+
// 用创建时的这份配置:之后换了配置,新的一份自己建 provider
|
|
70
|
+
const build = (snapshot: Env): Promise<Provider> => {
|
|
71
|
+
return (async () => {
|
|
72
|
+
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
73
|
+
return withRetry(await providerFromEnv(snapshot), {
|
|
74
|
+
gate,
|
|
75
|
+
onRetry: ({ attempt, retries, delayMs, error }) =>
|
|
76
|
+
ui?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
77
|
+
});
|
|
78
|
+
})();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// 第一次用时才创建:配置写错时错误出现在这一轮里,而不是让插件激活失败、连会话都打不开
|
|
82
|
+
const provider = (): Promise<Provider> => {
|
|
83
|
+
if (!hasProvider(env)) return Promise.reject(notConfigured());
|
|
84
|
+
if (made) return made;
|
|
85
|
+
const p = build(env);
|
|
86
|
+
made = p;
|
|
87
|
+
// 创建失败时下次重试(例如密钥刚写进 .env)
|
|
88
|
+
p.catch(() => {
|
|
89
|
+
if (made === p) made = undefined;
|
|
90
|
+
});
|
|
91
|
+
return p;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
|
|
95
|
+
const savedProviders = new Map<string, Promise<Provider>>();
|
|
96
|
+
const saved = async (name: string): Promise<Provider> => {
|
|
97
|
+
for (const s of await readSavedServices(modelsFile)) {
|
|
98
|
+
const m = s.models.find((x) => x.name === name);
|
|
99
|
+
const kind = providerKind(s.env);
|
|
100
|
+
if (!m || !isBuiltinKind(kind)) continue;
|
|
101
|
+
const snapshot: Env = { ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name };
|
|
102
|
+
const key = JSON.stringify(snapshot);
|
|
103
|
+
const cached = savedProviders.get(key);
|
|
104
|
+
if (cached) return cached;
|
|
105
|
+
const p = build(snapshot);
|
|
106
|
+
savedProviders.set(key, p);
|
|
107
|
+
// 创建失败时下次重试
|
|
108
|
+
p.catch(() => savedProviders.delete(key));
|
|
109
|
+
return p;
|
|
110
|
+
}
|
|
111
|
+
// 只写在 .env 里、没保存过的当前模型
|
|
112
|
+
if (hasProvider(env) && providerInfo(env).model === name) return provider();
|
|
113
|
+
throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const current: Provider["complete"] = (request, options) => provider().then((p) => p.complete(request, options));
|
|
117
|
+
const complete: Models["complete"] = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
|
|
118
|
+
|
|
119
|
+
// 转发的 provider:会话创建时固定的是这个壳,/model 之后下一次请求就用新的配置
|
|
120
|
+
ctx.provide("provider@1", { id: "jade/models", complete: current } satisfies Provider);
|
|
121
|
+
|
|
122
|
+
ctx.provide("models@1", {
|
|
123
|
+
current() {
|
|
124
|
+
if (!hasProvider(env)) return undefined;
|
|
125
|
+
const info = providerInfo(env);
|
|
126
|
+
let vision = true;
|
|
127
|
+
try {
|
|
128
|
+
vision = visionFromEnv(env);
|
|
129
|
+
} catch {
|
|
130
|
+
// 写错的 JADE_VISION 在创建 provider 时报错,这里当作不能看
|
|
131
|
+
vision = false;
|
|
132
|
+
}
|
|
133
|
+
return { kind: info.kind, model: info.model, via: via(info.kind, info.baseURL), contextWindow: contextWindow(info.model, env), vision };
|
|
134
|
+
},
|
|
135
|
+
complete,
|
|
136
|
+
list: () => listModels(env),
|
|
137
|
+
async saved() {
|
|
138
|
+
const names = (await readSavedServices(modelsFile)).flatMap((s) => (isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []));
|
|
139
|
+
return [...new Set(names)];
|
|
140
|
+
},
|
|
141
|
+
subscribe(fn: () => void) {
|
|
142
|
+
watchers.add(fn);
|
|
143
|
+
return () => void watchers.delete(fn);
|
|
144
|
+
},
|
|
145
|
+
} satisfies Models);
|
|
146
|
+
|
|
147
|
+
/** /model 写完配置之后:重新读 .env、换掉 provider;配置不完整时抛出,旧的配置和 provider 继续用 */
|
|
148
|
+
const apply = async () => {
|
|
149
|
+
const next = await envWithFile(envFile);
|
|
150
|
+
const before = { env, provider: made };
|
|
151
|
+
env = next;
|
|
152
|
+
made = undefined;
|
|
153
|
+
try {
|
|
154
|
+
await provider();
|
|
155
|
+
} catch (e) {
|
|
156
|
+
env = before.env;
|
|
157
|
+
made = before.provider;
|
|
158
|
+
throw e;
|
|
159
|
+
}
|
|
160
|
+
notify();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
164
|
+
name: "model",
|
|
165
|
+
args: "[name]",
|
|
166
|
+
description: "switch models, or add one with its provider, API key and limits",
|
|
167
|
+
// 只改配置,不碰会话:模型工作时也立即执行
|
|
168
|
+
instant: true,
|
|
169
|
+
async run(args, c) {
|
|
170
|
+
const configure = configurator({
|
|
171
|
+
envFile,
|
|
172
|
+
modelsFile,
|
|
173
|
+
env: () => env,
|
|
174
|
+
apply,
|
|
175
|
+
report: (text) => c.print(text),
|
|
176
|
+
ui: c.ui,
|
|
177
|
+
signal: c.signal,
|
|
178
|
+
});
|
|
179
|
+
await configure.model(args.trim() || undefined);
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
function via(kind: string, baseURL: string | undefined): string {
|
|
186
|
+
const host = baseURL ? safeHost(baseURL) : undefined;
|
|
187
|
+
if (kind === "anthropic") return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
|
|
188
|
+
if (kind === "openai") return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
|
|
189
|
+
return host ? `an OpenAI-compatible API at ${host}` : `the ${kind} provider`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function safeHost(url: string): string | undefined {
|
|
193
|
+
try {
|
|
194
|
+
return new URL(url).host;
|
|
195
|
+
} catch {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -182,6 +182,13 @@ export const PROVIDER_KINDS: readonly ProviderKind[] = ["openai-compat", "openai
|
|
|
182
182
|
|
|
183
183
|
type Env = Record<string, string | undefined>;
|
|
184
184
|
|
|
185
|
+
export const isBuiltinKind = (kind: string): kind is ProviderKind => (PROVIDER_KINDS as readonly string[]).includes(kind);
|
|
186
|
+
|
|
187
|
+
/** 每种内置服务的模型名、密钥、地址各放在哪个环境变量里 */
|
|
188
|
+
export const MODEL_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_MODEL", openai: "OPENAI_MODEL", anthropic: "ANTHROPIC_MODEL" };
|
|
189
|
+
export const KEY_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_API_KEY", openai: "OPENAI_API_KEY", anthropic: "ANTHROPIC_API_KEY" };
|
|
190
|
+
export const URL_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_BASE_URL", openai: "OPENAI_BASE_URL", anthropic: "ANTHROPIC_BASE_URL" };
|
|
191
|
+
|
|
185
192
|
/** JADE_EFFORT 可取的值;openai-compat 没有统一的字段,用 OPENAI_COMPAT_EXTRA_BODY 自己设 */
|
|
186
193
|
export const EFFORTS: Record<ProviderKind, readonly string[] | undefined> = {
|
|
187
194
|
anthropic: ["low", "medium", "high", "xhigh", "max"],
|
|
@@ -216,7 +223,7 @@ export function idleTimeoutFromEnv(env: Env = process.env): number | undefined {
|
|
|
216
223
|
*/
|
|
217
224
|
export function visionFromEnv(env: Env = process.env): boolean {
|
|
218
225
|
const text = env.JADE_VISION?.trim().toLowerCase();
|
|
219
|
-
if (!text) return providerKind(env)
|
|
226
|
+
if (!text) return providerKind(env) === "openai" || providerKind(env) === "anthropic";
|
|
220
227
|
if (["1", "true", "on", "yes"].includes(text)) return true;
|
|
221
228
|
if (["0", "false", "off", "no"].includes(text)) return false;
|
|
222
229
|
throw new Error(`JADE_VISION must be on or off, got ${JSON.stringify(env.JADE_VISION)}`);
|
|
@@ -227,28 +234,24 @@ export function effortFromEnv(env: Env = process.env): string | undefined {
|
|
|
227
234
|
const effort = env.JADE_EFFORT;
|
|
228
235
|
if (!effort) return undefined;
|
|
229
236
|
const kind = providerKind(env);
|
|
237
|
+
if (!isBuiltinKind(kind)) return effort;
|
|
230
238
|
const allowed = EFFORTS[kind];
|
|
231
239
|
if (!allowed) throw new Error(`JADE_EFFORT is not supported for the ${kind} provider; set it in OPENAI_COMPAT_EXTRA_BODY instead`);
|
|
232
240
|
if (!allowed.includes(effort)) throw new Error(`JADE_EFFORT must be one of ${allowed.join(", ")} for the ${kind} provider, got ${JSON.stringify(effort)}`);
|
|
233
241
|
return effort;
|
|
234
242
|
}
|
|
235
243
|
|
|
236
|
-
/** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY
|
|
237
|
-
export function providerKind(env: Env = process.env):
|
|
244
|
+
/** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY 的顺序。不认识的名字原样返回,创建时报错 */
|
|
245
|
+
export function providerKind(env: Env = process.env): string {
|
|
238
246
|
const named = env.JADE_PROVIDER;
|
|
239
|
-
if (named)
|
|
240
|
-
if (!(PROVIDER_KINDS as readonly string[]).includes(named)) {
|
|
241
|
-
throw new Error(`JADE_PROVIDER must be one of ${PROVIDER_KINDS.join(", ")}, got ${JSON.stringify(named)}`);
|
|
242
|
-
}
|
|
243
|
-
return named as ProviderKind;
|
|
244
|
-
}
|
|
247
|
+
if (named) return named;
|
|
245
248
|
if (env.OPENAI_API_KEY) return "openai";
|
|
246
249
|
if (env.OPENAI_COMPAT_BASE_URL) return "openai-compat";
|
|
247
250
|
return "anthropic";
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
export interface ProviderInfo {
|
|
251
|
-
kind:
|
|
254
|
+
kind: string;
|
|
252
255
|
model: string;
|
|
253
256
|
baseURL?: string;
|
|
254
257
|
}
|
|
@@ -276,6 +279,7 @@ export function providerInfo(env: Env = process.env): ProviderInfo {
|
|
|
276
279
|
*/
|
|
277
280
|
export async function providerFromEnv(env: Env = process.env): Promise<Provider> {
|
|
278
281
|
const kind = providerKind(env);
|
|
282
|
+
if (!isBuiltinKind(kind)) throw new Error(`JADE_PROVIDER must be one of ${PROVIDER_KINDS.join(", ")}, got ${JSON.stringify(kind)}`);
|
|
279
283
|
const maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
|
|
280
284
|
const effort = effortFromEnv(env);
|
|
281
285
|
// 写错时在这里就报错,不等到第一次请求
|
|
@@ -312,28 +316,20 @@ export async function providerFromEnv(env: Env = process.env): Promise<Provider>
|
|
|
312
316
|
});
|
|
313
317
|
}
|
|
314
318
|
const { anthropicProvider } = await import("@uncarved/provider-anthropic");
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
export function switchable(initial: Provider): SwitchableProvider {
|
|
324
|
-
let current = initial;
|
|
325
|
-
return {
|
|
326
|
-
id: "switchable",
|
|
327
|
-
set: (next) => {
|
|
328
|
-
current = next;
|
|
329
|
-
},
|
|
330
|
-
complete: (request, options) => current.complete(request, options),
|
|
331
|
-
};
|
|
319
|
+
// 地址和密钥从 env 取,不让 SDK 自己读 process.env:ctx.complete 指定的保存过的模型,env 不是 process.env
|
|
320
|
+
return anthropicProvider({
|
|
321
|
+
apiKey: env.ANTHROPIC_API_KEY,
|
|
322
|
+
baseURL: env.ANTHROPIC_BASE_URL ?? "https://api.anthropic.com",
|
|
323
|
+
model: env.ANTHROPIC_MODEL,
|
|
324
|
+
effort: (effort ?? "high") as "low" | "medium" | "high" | "xhigh" | "max",
|
|
325
|
+
maxTokens,
|
|
326
|
+
});
|
|
332
327
|
}
|
|
333
328
|
|
|
334
329
|
/** 服务提供的模型列表;不支持或出错时返回空数组 */
|
|
335
330
|
export async function listModels(env: Env = process.env, fetchImpl: typeof fetch = fetch): Promise<string[]> {
|
|
336
331
|
const kind = providerKind(env);
|
|
332
|
+
if (!isBuiltinKind(kind)) return [];
|
|
337
333
|
const info = providerInfo(env);
|
|
338
334
|
const base = (info.baseURL ?? "https://api.anthropic.com").replace(/\/$/, "");
|
|
339
335
|
const headers: Record<string, string> =
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
|
|
2
|
+
// 每个模型带自己的设置(输出上限、思考力度、上下文窗口)。.env 里始终是当前用的那一个,这里记着用过的全部,
|
|
3
|
+
// /model 可以切回去。
|
|
4
|
+
|
|
5
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
export type Env = Record<string, string | undefined>;
|
|
9
|
+
|
|
10
|
+
/** 每个模型自己的设置:切换模型时一起换掉 */
|
|
11
|
+
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION"];
|
|
12
|
+
|
|
13
|
+
export interface SavedModel {
|
|
14
|
+
name: string;
|
|
15
|
+
/** 这个模型自己的设置变量 */
|
|
16
|
+
settings: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SavedService {
|
|
20
|
+
/** 服务的变量:JADE_PROVIDER、地址、密钥等 */
|
|
21
|
+
env: Record<string, string>;
|
|
22
|
+
/** 最近用过的在前 */
|
|
23
|
+
models: SavedModel[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const strings = (v: unknown): Record<string, string> =>
|
|
27
|
+
typeof v === "object" && v !== null ? (Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) as Record<string, string>) : {};
|
|
28
|
+
|
|
29
|
+
/** 读不了或格式不对时返回空数组 */
|
|
30
|
+
export async function readSavedServices(file: string): Promise<SavedService[]> {
|
|
31
|
+
try {
|
|
32
|
+
const data = JSON.parse(await readFile(file, "utf8")) as { services?: unknown };
|
|
33
|
+
if (!Array.isArray(data.services)) return [];
|
|
34
|
+
return data.services.flatMap((raw: unknown) => {
|
|
35
|
+
const s = raw as { env?: unknown; models?: unknown } | null;
|
|
36
|
+
if (!s || !Array.isArray(s.models)) return [];
|
|
37
|
+
const models = s.models.flatMap((m: unknown) => {
|
|
38
|
+
const name = (m as { name?: unknown } | null)?.name;
|
|
39
|
+
return typeof name === "string" && name ? [{ name, settings: strings((m as { settings?: unknown }).settings) }] : [];
|
|
40
|
+
});
|
|
41
|
+
return models.length ? [{ env: strings(s.env), models }] : [];
|
|
42
|
+
});
|
|
43
|
+
} catch {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function writeSavedServices(file: string, services: SavedService[]): Promise<void> {
|
|
49
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
50
|
+
// 里面有 API 密钥
|
|
51
|
+
await writeFile(file, JSON.stringify({ services }, null, 2) + "\n", { mode: 0o600 });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
|
|
56
|
+
* 同时更新 env,让改动在本次运行中生效
|
|
57
|
+
*/
|
|
58
|
+
export async function updateEnvFile(file: string, updates: Env, env: Env): Promise<void> {
|
|
59
|
+
let text = "";
|
|
60
|
+
try {
|
|
61
|
+
text = await readFile(file, "utf8");
|
|
62
|
+
} catch {
|
|
63
|
+
// 还没有配置文件
|
|
64
|
+
}
|
|
65
|
+
const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
|
|
66
|
+
const out: string[] = [];
|
|
67
|
+
const written = new Set<string>();
|
|
68
|
+
for (const raw of lines) {
|
|
69
|
+
const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
|
|
70
|
+
if (!key || !(key in updates)) {
|
|
71
|
+
out.push(raw);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// 同一个键出现多次时只保留第一处
|
|
75
|
+
const value = updates[key];
|
|
76
|
+
if (value !== undefined && !written.has(key)) out.push(format(key, value));
|
|
77
|
+
written.add(key);
|
|
78
|
+
}
|
|
79
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
80
|
+
if (value !== undefined && !written.has(key)) out.push(format(key, value));
|
|
81
|
+
}
|
|
82
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
83
|
+
await writeFile(file, out.join("\n") + "\n");
|
|
84
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
85
|
+
if (value === undefined) delete env[key];
|
|
86
|
+
else env[key] = value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function format(key: string, value: string): string {
|
|
91
|
+
if (!/[\s#"']/.test(value)) return `${key}=${value}`;
|
|
92
|
+
// 单引号里的内容原样读回(引号、反斜杠都不转义);只有换行要用双引号里的 \n
|
|
93
|
+
return value.includes("\n") ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}='${value}'`;
|
|
94
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// jade/modes:权限模式的切换。模式本身归 jade/approval(approval@1),这里提供切换方式和给前端显示的状态:
|
|
2
|
+
// Shift+Tab 在 ask → auto → plan 之间轮换,/mode 查看或选择,modes@1 让前端显示当前模式、在模式变化时更新。
|
|
3
|
+
//
|
|
4
|
+
// 计划模式的两件事也在这里:
|
|
5
|
+
// - 在 plan 模式时经 prompts@1 往系统提示词里加一段,告诉模型先给计划、不要改东西(切出去时去掉)
|
|
6
|
+
// - propose_plan 工具:模型写好要执行的计划后调用它,问用户要不要执行;批准了就换模式,工具结果告诉模型开始做。
|
|
7
|
+
// 由模型声明"这是要执行的计划",追问、回答问题、只要计划本身(比如健身计划)的回复不会被当成计划去问。
|
|
8
|
+
// 用户的选择在工具结果里,会话日志里看得到
|
|
9
|
+
|
|
10
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import type { Tool } from "uncarved";
|
|
13
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
14
|
+
import type { Approval } from "../approval/index.ts";
|
|
15
|
+
import type { Commands } from "../commands/index.ts";
|
|
16
|
+
import type { Keys } from "../keys/index.ts";
|
|
17
|
+
import type { Prompts } from "../prompts/index.ts";
|
|
18
|
+
|
|
19
|
+
/** modes@1 */
|
|
20
|
+
export interface Modes {
|
|
21
|
+
current(): string;
|
|
22
|
+
list(): { name: string; description: string }[];
|
|
23
|
+
/** 换模式,从下一个工具调用起生效 */
|
|
24
|
+
set(mode: string): void;
|
|
25
|
+
/** 换到轮换顺序里的下一个 */
|
|
26
|
+
cycle(): string;
|
|
27
|
+
/** 模式变化时调用,返回取消订阅的函数;调用方关闭时自动取消 */
|
|
28
|
+
subscribe(fn: (mode: string) => void): () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const DESCRIPTIONS: Record<string, string> = {
|
|
32
|
+
ask: "ask before edits and commands",
|
|
33
|
+
auto: "edits in this directory and harmless commands run without asking; others still ask",
|
|
34
|
+
plan: "change nothing: research read-only and propose a plan",
|
|
35
|
+
readonly: "change nothing",
|
|
36
|
+
yolo: "never ask",
|
|
37
|
+
};
|
|
38
|
+
const CYCLE = ["ask", "auto", "plan"];
|
|
39
|
+
|
|
40
|
+
/** 计划模式时加进系统提示词的段落;不能问用户(-p、非终端)时没有 propose_plan,只让模型给出计划后停下 */
|
|
41
|
+
export function planNote(canPropose: boolean): string {
|
|
42
|
+
return (
|
|
43
|
+
"The session is in plan mode. Investigate with read-only tools (and the task tool for broad searches); do not try to modify files " +
|
|
44
|
+
"or run commands that change anything, such calls are denied. If the request is unclear in a way that changes the plan, ask the user first. " +
|
|
45
|
+
"When the task needs changes made (files edited, commands run), reply with a concise, concrete plan: the steps in order (for code, the files to change and how), " +
|
|
46
|
+
"any open questions, and how you will verify the result. " +
|
|
47
|
+
(canPropose
|
|
48
|
+
? "Then call propose_plan so the user can approve it. If nothing needs to be executed (a question, or the plan itself is the answer the user wants), " +
|
|
49
|
+
"just answer and do not call propose_plan."
|
|
50
|
+
: "Then stop and wait for the user's approval.")
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 批准计划之后工具结果里的那句 */
|
|
55
|
+
export const APPROVED_NOTE = "The user approved the plan. Implement it now.";
|
|
56
|
+
/** 没批准时工具结果里的那句 */
|
|
57
|
+
export const NOT_APPROVED = "The user did not approve the plan yet. End your turn and wait for their feedback.";
|
|
58
|
+
|
|
59
|
+
export const PROPOSE_PLAN = "propose_plan";
|
|
60
|
+
|
|
61
|
+
/** propose_plan:问用户要不要执行刚写好的计划,批准了就换到用户选的模式 */
|
|
62
|
+
export function proposePlanTool(approval: Approval, ui: UI): Tool {
|
|
63
|
+
return {
|
|
64
|
+
name: PROPOSE_PLAN,
|
|
65
|
+
description:
|
|
66
|
+
"In plan mode, ask the user to approve the plan you just wrote in your reply, and wait for the answer. Write the full plan in your reply text first; " +
|
|
67
|
+
"call this only for a plan of changes to execute, not for questions, clarifications or answers that need nothing executed.",
|
|
68
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
69
|
+
// 不碰文件和进程:plan 模式下不用批准
|
|
70
|
+
effect: "read",
|
|
71
|
+
concurrency: "exclusive",
|
|
72
|
+
idempotent: false,
|
|
73
|
+
async execute(_input, ctx) {
|
|
74
|
+
if (approval.mode() !== "plan") return { content: "The session is not in plan mode; no approval is needed.", isError: true };
|
|
75
|
+
const choice = await ui.select(
|
|
76
|
+
{
|
|
77
|
+
title: "Execute this plan?",
|
|
78
|
+
options: [
|
|
79
|
+
{ label: "Yes, in auto mode", value: "auto", hint: "edits and safe commands run without asking" },
|
|
80
|
+
{ label: "Yes, and ask before each edit", value: "ask", hint: "mode ask" },
|
|
81
|
+
{ label: "No, keep planning", value: "plan", hint: "type what to change" },
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
ctx.signal,
|
|
85
|
+
);
|
|
86
|
+
ctx.signal.throwIfAborted();
|
|
87
|
+
if (choice !== "auto" && choice !== "ask") return { content: NOT_APPROVED };
|
|
88
|
+
setMode(approval, choice);
|
|
89
|
+
return { content: `${APPROVED_NOTE} (mode: ${choice})` };
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 模式变化时通知前端和会话里的订阅者(模块每个进程只加载一次,进程级和会话级共用) */
|
|
95
|
+
const listeners = new Set<(mode: string) => void>();
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 用户选的模式记在 $JADE_HOME/mode.json,下次启动接着用(比配置里的 mode 优先)。
|
|
99
|
+
* plan 是一次任务里的,yolo 不该悄悄带到下一次启动:这两个不记
|
|
100
|
+
*/
|
|
101
|
+
const REMEMBERED = new Set(["ask", "auto", "readonly"]);
|
|
102
|
+
let modeFile: string | undefined;
|
|
103
|
+
/** 记下的模式只在第一个可交互的会话里恢复一次:jade -p 这类不能问用户的运行一直用配置的模式 */
|
|
104
|
+
let restored = false;
|
|
105
|
+
|
|
106
|
+
function readRemembered(file: string): string | undefined {
|
|
107
|
+
try {
|
|
108
|
+
const mode = (JSON.parse(readFileSync(file, "utf8")) as { mode?: unknown }).mode;
|
|
109
|
+
return typeof mode === "string" && REMEMBERED.has(mode) ? mode : undefined;
|
|
110
|
+
} catch {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function remember(mode: string): void {
|
|
116
|
+
if (!modeFile || !REMEMBERED.has(mode)) return;
|
|
117
|
+
try {
|
|
118
|
+
// 同步写:连按几次 Shift+Tab 时,文件里留下的一定是最后一次
|
|
119
|
+
mkdirSync(path.dirname(modeFile), { recursive: true });
|
|
120
|
+
writeFileSync(modeFile, `${JSON.stringify({ mode })}\n`);
|
|
121
|
+
} catch {
|
|
122
|
+
// 记不下只是下次从配置的模式开始
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function setMode(approval: Approval, mode: string): void {
|
|
127
|
+
if (mode === approval.mode()) return;
|
|
128
|
+
approval.setMode(mode as ReturnType<Approval["mode"]>);
|
|
129
|
+
remember(mode);
|
|
130
|
+
for (const fn of [...listeners]) {
|
|
131
|
+
try {
|
|
132
|
+
fn(mode);
|
|
133
|
+
} catch {
|
|
134
|
+
// 订阅者的错误经过膜归它自己,不影响别的订阅者
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default definePlugin({
|
|
140
|
+
process(ctx) {
|
|
141
|
+
const approval = ctx.require<Approval>("approval@1");
|
|
142
|
+
modeFile = path.join(ctx.home, "mode.json");
|
|
143
|
+
restored = false;
|
|
144
|
+
const set = (mode: string) => setMode(approval, mode);
|
|
145
|
+
const cycle = () => {
|
|
146
|
+
const next = CYCLE[(CYCLE.indexOf(approval.mode()) + 1) % CYCLE.length]!;
|
|
147
|
+
set(next);
|
|
148
|
+
return next;
|
|
149
|
+
};
|
|
150
|
+
const list = () => Object.entries(DESCRIPTIONS).map(([name, description]) => ({ name, description }));
|
|
151
|
+
|
|
152
|
+
ctx.provide("modes@1", (caller): Modes => ({
|
|
153
|
+
current: () => approval.mode(),
|
|
154
|
+
list,
|
|
155
|
+
set,
|
|
156
|
+
cycle,
|
|
157
|
+
subscribe(fn) {
|
|
158
|
+
listeners.add(fn);
|
|
159
|
+
const off = () => void listeners.delete(fn);
|
|
160
|
+
caller.onDispose(off);
|
|
161
|
+
return off;
|
|
162
|
+
},
|
|
163
|
+
}));
|
|
164
|
+
|
|
165
|
+
ctx.service<Keys>("keys@1")?.register({ key: "shift+tab", description: "cycle ask/auto/plan", run: () => void cycle() });
|
|
166
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
167
|
+
name: "mode",
|
|
168
|
+
args: `[${Object.keys(DESCRIPTIONS).join("|")}]`,
|
|
169
|
+
description: "show or change the permission mode",
|
|
170
|
+
instant: true,
|
|
171
|
+
async run(args, c) {
|
|
172
|
+
let want: string | undefined = args.trim() || undefined;
|
|
173
|
+
if (!want) {
|
|
174
|
+
want = await c.ui.select(
|
|
175
|
+
{ title: "Permission mode", options: list().map((m) => ({ label: m.name, value: m.name, hint: m.name === approval.mode() ? "current" : m.description })) },
|
|
176
|
+
c.signal,
|
|
177
|
+
);
|
|
178
|
+
if (!want) return;
|
|
179
|
+
}
|
|
180
|
+
if (!(want in DESCRIPTIONS)) return `unknown mode ${want}; one of ${Object.keys(DESCRIPTIONS).join(", ")}`;
|
|
181
|
+
set(want);
|
|
182
|
+
return `mode: ${approval.mode()}`;
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
session(ctx) {
|
|
188
|
+
const approval = ctx.require<Approval>("approval@1");
|
|
189
|
+
const prompts = ctx.service<Prompts>("prompts@1");
|
|
190
|
+
const ui = ctx.service<UI>("ui@1");
|
|
191
|
+
|
|
192
|
+
if (!restored && ui?.interactive && modeFile) {
|
|
193
|
+
restored = true;
|
|
194
|
+
const saved = readRemembered(modeFile);
|
|
195
|
+
if (saved) setMode(approval, saved);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 能问用户时才有 propose_plan;它只在 plan 模式下发给模型
|
|
199
|
+
const canPropose = !!ui?.interactive;
|
|
200
|
+
if (canPropose) ctx.kernel.tool(proposePlanTool(approval, ui!));
|
|
201
|
+
ctx.kernel.hook({
|
|
202
|
+
kind: "transform",
|
|
203
|
+
point: "context.build",
|
|
204
|
+
fn: (req) => (approval.mode() === "plan" ? req : { ...req, tools: req.tools.filter((t) => t.name !== PROPOSE_PLAN) }),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// 计划模式的说明跟着模式进出系统提示词
|
|
208
|
+
let note: (() => void) | undefined;
|
|
209
|
+
const sync = () => {
|
|
210
|
+
const plan = approval.mode() === "plan";
|
|
211
|
+
if (plan && !note && prompts) note = prompts.add({ text: planNote(canPropose), stable: false });
|
|
212
|
+
else if (!plan && note) {
|
|
213
|
+
note();
|
|
214
|
+
note = undefined;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
sync();
|
|
218
|
+
const onChange = () => sync();
|
|
219
|
+
listeners.add(onChange);
|
|
220
|
+
ctx.effect(() => () => void listeners.delete(onChange));
|
|
221
|
+
},
|
|
222
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// jade/plugins:/plugins 命令。列出这次启动里每个插件的状态和原因(failed、skipped、conflicted……),
|
|
2
|
+
// 以及每个服务由谁提供。数据来自核心自己的 jade@1 服务,和救援 CLI `jade plugins list` 是同一份状态。
|
|
3
|
+
|
|
4
|
+
import { definePlugin, type Jade } from "../../src/api.ts";
|
|
5
|
+
import type { Commands } from "../commands/index.ts";
|
|
6
|
+
|
|
7
|
+
export default definePlugin({
|
|
8
|
+
process(ctx) {
|
|
9
|
+
const commands = ctx.require<Commands>("commands@1");
|
|
10
|
+
const jade = ctx.require<Jade>("jade@1");
|
|
11
|
+
|
|
12
|
+
commands.register({
|
|
13
|
+
name: "plugins",
|
|
14
|
+
args: "[id]",
|
|
15
|
+
description: "every plugin's state and why it is not active, and who provides each service",
|
|
16
|
+
// 只读,不碰会话
|
|
17
|
+
instant: true,
|
|
18
|
+
run(args) {
|
|
19
|
+
const all = jade.plugins();
|
|
20
|
+
const query = args.trim();
|
|
21
|
+
if (query) {
|
|
22
|
+
const found = all.filter((p) => p.id === query || p.id?.endsWith(`/${query}`) || p.dir === query);
|
|
23
|
+
if (found.length === 0) return `No plugin matches ${query}. /plugins lists them all.`;
|
|
24
|
+
const lines = found.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}\n ${p.dir}`);
|
|
25
|
+
for (const s of jade.services().filter((s) => found.some((p) => p.id === s.provider))) lines.push(` provides ${s.service}`);
|
|
26
|
+
return lines.join("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const lines = all.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}`);
|
|
30
|
+
const broken = all.filter((p) => p.state !== "active");
|
|
31
|
+
if (broken.length > 0) lines.push("", `${broken.length} of ${all.length} plugins are not active; /plugins <id> shows where one lives.`);
|
|
32
|
+
const services = jade.services();
|
|
33
|
+
if (services.length > 0) lines.push("", "services:", ...services.map((s) => ` ${s.service} ${s.provider}`));
|
|
34
|
+
return lines.join("\n");
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
});
|