@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,455 @@
|
|
|
1
|
+
// /model:选服务、填地址和密钥、选模型、改输出上限、思考力度、上下文窗口、限速和能不能看图。
|
|
2
|
+
// 配置写进 $JADE_HOME/.env;用过的服务和模型记在 $JADE_HOME/models.json 里(saved.ts),可以直接切回去。
|
|
3
|
+
//
|
|
4
|
+
// 从旧 jade 的 ui/configure.ts 移植。省掉了旧 jade 的"插件提供的模型服务"(新的 jade 还没有这种服务),
|
|
5
|
+
// 其余流程一样。
|
|
6
|
+
|
|
7
|
+
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
import { hasProvider, parseTokens } from "./config.ts";
|
|
9
|
+
import { contextWindow } from "./context.ts";
|
|
10
|
+
import { EFFORTS, isBuiltinKind, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv, type ProviderKind } from "./provider.ts";
|
|
11
|
+
import { MODEL_SETTINGS, readSavedServices, updateEnvFile, writeSavedServices, type Env, type SavedModel, type SavedService } from "./saved.ts";
|
|
12
|
+
import type { UI } from "../../src/api.ts";
|
|
13
|
+
|
|
14
|
+
export interface ConfigureOptions {
|
|
15
|
+
envFile: string;
|
|
16
|
+
modelsFile: string;
|
|
17
|
+
/** 当前的模型配置;updateEnvFile 会改它,apply 之后要用新的那个 */
|
|
18
|
+
env(): Env;
|
|
19
|
+
/** 按新的环境变量换上 provider;配置不完整时抛出 */
|
|
20
|
+
apply(): Promise<void>;
|
|
21
|
+
/** 在对话里显示一段文字 */
|
|
22
|
+
report(text: string): void;
|
|
23
|
+
ui: UI;
|
|
24
|
+
/** 用户中断时中止 */
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
/** 测试用:列模型时的 fetch */
|
|
27
|
+
fetch?: typeof fetch;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface Configure {
|
|
31
|
+
/** 选服务、填地址和密钥、选模型;返回是否配置成功 */
|
|
32
|
+
provider(): Promise<boolean>;
|
|
33
|
+
/** /model [name] */
|
|
34
|
+
model(arg: string | undefined): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const PROVIDERS: Array<{ kind: ProviderKind; label: string; hint: string }> = [
|
|
38
|
+
{ kind: "openai-compat", label: "OpenAI-compatible service", hint: "any /chat/completions endpoint" },
|
|
39
|
+
{ kind: "openai", label: "OpenAI", hint: "Responses API" },
|
|
40
|
+
{ kind: "anthropic", label: "Anthropic", hint: "Claude" },
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** JADE_VISION 设成了什么;没设或写错时 undefined */
|
|
44
|
+
function visionSetting(env: Env): boolean | undefined {
|
|
45
|
+
if (!env.JADE_VISION?.trim()) return undefined;
|
|
46
|
+
try {
|
|
47
|
+
return visionFromEnv({ JADE_VISION: env.JADE_VISION });
|
|
48
|
+
} catch {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function visionHint(env: Env, kind: string): string {
|
|
54
|
+
const v = visionSetting(env);
|
|
55
|
+
return v === undefined ? `${isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no"} (default)` : v ? "yes" : "no";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 一个服务包含的变量(不含模型名) */
|
|
59
|
+
const serviceKeys = (kind: ProviderKind) => ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind], ...(kind === "openai-compat" ? ["OPENAI_COMPAT_EXTRA_BODY"] : [])];
|
|
60
|
+
|
|
61
|
+
const pick = (env: Env, keys: string[]) => {
|
|
62
|
+
const out: Record<string, string> = {};
|
|
63
|
+
for (const k of keys) if (env[k] !== undefined) out[k] = env[k]!;
|
|
64
|
+
return out;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** 服务的类型、地址和密钥都相同的算同一个;配置无效时返回 undefined */
|
|
68
|
+
function serviceId(env: Env): string | undefined {
|
|
69
|
+
try {
|
|
70
|
+
const i = providerInfo(env);
|
|
71
|
+
if (!isBuiltinKind(i.kind)) return undefined;
|
|
72
|
+
return `${i.kind}|${i.baseURL ?? ""}|${env[KEY_KEY[i.kind]] ?? ""}`;
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 选择列表里服务的说明:类型和地址的主机名 */
|
|
79
|
+
function describeService(service: SavedService): string {
|
|
80
|
+
const i = providerInfo(service.env);
|
|
81
|
+
let host = i.baseURL ?? "";
|
|
82
|
+
try {
|
|
83
|
+
host = new URL(host).host;
|
|
84
|
+
} catch {}
|
|
85
|
+
return [i.kind, host].filter(Boolean).join(" · ");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 选择列表里一组模型的说明:前几个名字 */
|
|
89
|
+
function modelNames(models: SavedModel[]): string {
|
|
90
|
+
const names = models.slice(0, 3).map((m) => m.name).join(", ");
|
|
91
|
+
return models.length > 3 ? `${names} +${models.length - 3}` : names;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 主菜单里设置那一项的简要说明,例如 "64K out · 1M ctx · images" */
|
|
95
|
+
function settingsSummary(env: Env, kind: string, window: number): string {
|
|
96
|
+
const images = visionSetting(env) ?? (isBuiltinKind(kind) && kind !== "openai-compat");
|
|
97
|
+
return [
|
|
98
|
+
env.JADE_MAX_TOKENS && `${env.JADE_MAX_TOKENS} out`,
|
|
99
|
+
env.JADE_EFFORT && `${env.JADE_EFFORT} effort`,
|
|
100
|
+
`${env.JADE_CONTEXT_WINDOW ?? window} ctx`,
|
|
101
|
+
env.JADE_RPM && `${env.JADE_RPM}/min`,
|
|
102
|
+
images ? "images" : "no images",
|
|
103
|
+
]
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
.join(" · ");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const errorText = (err: unknown) => (err instanceof Error ? err.message : String(err));
|
|
109
|
+
|
|
110
|
+
export function configurator(opts: ConfigureOptions): Configure {
|
|
111
|
+
const { ui, envFile, modelsFile } = opts;
|
|
112
|
+
const report = (text: string) => opts.report(text);
|
|
113
|
+
|
|
114
|
+
/** 保存过的、有效的服务,最近用过的在前 */
|
|
115
|
+
const savedServices = async () => (await readSavedServices(modelsFile)).filter((s) => serviceId(s.env) !== undefined);
|
|
116
|
+
|
|
117
|
+
/** 输入的 token 数:空表示不设;写错时报告并返回 false */
|
|
118
|
+
const tokensInput = (name: string, text: string): boolean => {
|
|
119
|
+
if (!text) return true;
|
|
120
|
+
try {
|
|
121
|
+
parseTokens(name, text);
|
|
122
|
+
return true;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
report(errorText(err));
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 把当前的服务和模型记下来,都放到最前面。
|
|
131
|
+
* replacing:换了密钥的那个服务原来的 id,它下面的模型归到新的密钥下
|
|
132
|
+
*/
|
|
133
|
+
async function remember(replacing?: string) {
|
|
134
|
+
const env = opts.env();
|
|
135
|
+
if (!hasProvider(env)) return;
|
|
136
|
+
const id = serviceId(env);
|
|
137
|
+
const model = providerInfo(env).model;
|
|
138
|
+
const kind = providerKind(env);
|
|
139
|
+
if (!id || !model || !isBuiltinKind(kind)) return;
|
|
140
|
+
// 写回时保留现在认不出的服务,不然它和它的密钥就丢了
|
|
141
|
+
const all = await readSavedServices(modelsFile);
|
|
142
|
+
const old = all.filter((s) => {
|
|
143
|
+
const sid = serviceId(s.env);
|
|
144
|
+
return sid !== undefined && (sid === id || (replacing !== undefined && sid === replacing));
|
|
145
|
+
});
|
|
146
|
+
const models = old.flatMap((s) => s.models).filter((m, i, list) => m.name !== model && list.findIndex((x) => x.name === m.name) === i);
|
|
147
|
+
const service: SavedService = {
|
|
148
|
+
env: { ...pick(env, serviceKeys(kind)), JADE_PROVIDER: kind },
|
|
149
|
+
models: [{ name: model, settings: pick(env, MODEL_SETTINGS) }, ...models],
|
|
150
|
+
};
|
|
151
|
+
await writeSavedServices(modelsFile, [service, ...all.filter((s) => !old.includes(s))]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 保存配置并换上新的 provider;失败时报告原因,旧的配置和 provider 继续用 */
|
|
155
|
+
async function applyConfig(updates: Env, replacing?: string): Promise<boolean> {
|
|
156
|
+
const before = await readFile(envFile, "utf8").catch(() => undefined);
|
|
157
|
+
// 传一份拷贝:apply 成功之前插件用的还是旧的那份(失败时状态栏显示的就是实际在用的)
|
|
158
|
+
await updateEnvFile(envFile, updates, { ...opts.env() });
|
|
159
|
+
try {
|
|
160
|
+
await opts.apply();
|
|
161
|
+
} catch (err) {
|
|
162
|
+
// 放回原来的 .env:不然这次会话还用旧的,下次启动却读到这份用不了的配置
|
|
163
|
+
await (before === undefined ? rm(envFile, { force: true }) : writeFile(envFile, before)).catch(() => {});
|
|
164
|
+
report(`Not saved: the configuration is incomplete: ${errorText(err)}`);
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const info = providerInfo(opts.env());
|
|
168
|
+
await remember(replacing).catch((err) => report(`Could not save the model list to ${modelsFile}: ${errorText(err)}`));
|
|
169
|
+
report(`Model: ${info.model} (${info.kind}${info.baseURL ? `, ${info.baseURL}` : ""}). Saved to ${envFile}.`);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 换成保存过的服务 s 上的模型 m:服务的变量和模型的设置写进 .env,模型没设的设置删掉 */
|
|
174
|
+
function useSaved(s: SavedService, m: SavedModel): Promise<boolean> {
|
|
175
|
+
const kind = providerKind(s.env);
|
|
176
|
+
if (!isBuiltinKind(kind)) {
|
|
177
|
+
report(`The saved service ${describeService(s)} is not one of the built-in providers.`);
|
|
178
|
+
return Promise.resolve(false);
|
|
179
|
+
}
|
|
180
|
+
const updates: Env = {};
|
|
181
|
+
for (const k of serviceKeys(kind)) updates[k] = s.env[k];
|
|
182
|
+
for (const k of MODEL_SETTINGS) updates[k] = m.settings[k];
|
|
183
|
+
updates.JADE_PROVIDER = kind;
|
|
184
|
+
updates[MODEL_KEY[kind]] = m.name;
|
|
185
|
+
return applyConfig(updates);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* 在当前的服务上换成 model:用过的带上它自己的设置;新的模型沿用输出上限和思考力度,
|
|
190
|
+
* 上下文窗口不沿用(不同模型差别很大),按模型名推断
|
|
191
|
+
*/
|
|
192
|
+
async function switchModel(model: string): Promise<boolean> {
|
|
193
|
+
const env = opts.env();
|
|
194
|
+
const id = serviceId(env);
|
|
195
|
+
const service = (await savedServices()).find((s) => serviceId(s.env) === id);
|
|
196
|
+
const saved = service?.models.find((m) => m.name === model);
|
|
197
|
+
if (service && saved) return useSaved(service, saved);
|
|
198
|
+
const kind = providerKind(env);
|
|
199
|
+
if (!isBuiltinKind(kind)) {
|
|
200
|
+
report(`JADE_PROVIDER must be one of ${PROVIDERS.map((p) => p.kind).join(", ")}; set the model in ${envFile}.`);
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
return applyConfig({ [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** 选模型:服务能列出模型时从列表里选,否则手动输入 */
|
|
207
|
+
async function chooseModel(env: Env, current: string | undefined, saved: string[] = []): Promise<string | undefined> {
|
|
208
|
+
const models = await listModels(env, opts.fetch);
|
|
209
|
+
if (models.length > 0) {
|
|
210
|
+
const OTHER = "\0other";
|
|
211
|
+
const picked = await ui.select(
|
|
212
|
+
{
|
|
213
|
+
title: "Choose a model",
|
|
214
|
+
options: [...models.map((m) => ({ label: m, value: m, ...(m === current ? { hint: "current" } : saved.includes(m) ? { hint: "saved" } : {}) })), { label: "Enter a name…", value: OTHER }],
|
|
215
|
+
},
|
|
216
|
+
opts.signal,
|
|
217
|
+
);
|
|
218
|
+
if (picked !== OTHER) return picked;
|
|
219
|
+
}
|
|
220
|
+
return (await ui.input({ title: "Model name", ...(current ? { initial: current } : {}) }, opts.signal)) || undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function configureProvider(): Promise<boolean> {
|
|
224
|
+
const env = opts.env();
|
|
225
|
+
const current = hasProvider(env) ? providerInfo(env) : undefined;
|
|
226
|
+
const currentId = hasProvider(env) ? serviceId(env) : undefined;
|
|
227
|
+
const kindName = await ui.select(
|
|
228
|
+
{
|
|
229
|
+
title: "Which service should jade use?",
|
|
230
|
+
options: PROVIDERS.map((p) => ({ label: p.label, value: p.kind, hint: p.kind === current?.kind ? `${p.hint} · current` : p.hint })),
|
|
231
|
+
},
|
|
232
|
+
opts.signal,
|
|
233
|
+
);
|
|
234
|
+
if (!kindName || !isBuiltinKind(kindName)) return false;
|
|
235
|
+
const kind: ProviderKind = kindName;
|
|
236
|
+
const same = kind === current?.kind;
|
|
237
|
+
|
|
238
|
+
let baseURL: string | undefined = env[URL_KEY[kind]];
|
|
239
|
+
if (kind !== "anthropic") {
|
|
240
|
+
const fallback = kind === "openai" ? "https://api.openai.com/v1" : "";
|
|
241
|
+
const answer = await ui.input({ title: "Base URL", initial: baseURL ?? fallback, placeholder: "https://example.com/v1" }, opts.signal);
|
|
242
|
+
if (answer === undefined) return false;
|
|
243
|
+
if (kind === "openai-compat" && !answer) {
|
|
244
|
+
report("A base URL is required.");
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
baseURL = answer || undefined;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 已知的密钥:当前服务的,或者保存过的同一个地址的服务的(最近用过的那个)
|
|
251
|
+
const sameAddress = (e: Env) => {
|
|
252
|
+
const i = providerInfo(e);
|
|
253
|
+
return i.kind === kind && (i.baseURL ?? "") === (providerInfo({ JADE_PROVIDER: kind, [URL_KEY[kind]]: baseURL }).baseURL ?? "");
|
|
254
|
+
};
|
|
255
|
+
const services = await savedServices();
|
|
256
|
+
const known = current && sameAddress(env) ? env[KEY_KEY[kind]] : services.find((s) => sameAddress(s.env))?.env[KEY_KEY[kind]];
|
|
257
|
+
const answer = await ui.input(
|
|
258
|
+
{
|
|
259
|
+
title: "API key",
|
|
260
|
+
mask: true,
|
|
261
|
+
placeholder: known ? "leave empty to keep the saved key" : kind === "openai-compat" ? "leave empty if the service needs none" : "",
|
|
262
|
+
},
|
|
263
|
+
opts.signal,
|
|
264
|
+
);
|
|
265
|
+
if (answer === undefined) return false;
|
|
266
|
+
const key = answer || known;
|
|
267
|
+
|
|
268
|
+
const serviceEnv: Env = { JADE_PROVIDER: kind, [URL_KEY[kind]]: baseURL, [KEY_KEY[kind]]: key };
|
|
269
|
+
if (kind === "openai-compat") {
|
|
270
|
+
// 额外的请求参数跟着服务走:同一个服务保留,换了服务用保存过的那份
|
|
271
|
+
const saved = services.find((s) => serviceId(s.env) === serviceId(serviceEnv));
|
|
272
|
+
serviceEnv.OPENAI_COMPAT_EXTRA_BODY = saved ? saved.env.OPENAI_COMPAT_EXTRA_BODY : sameAddress(env) ? env.OPENAI_COMPAT_EXTRA_BODY : undefined;
|
|
273
|
+
}
|
|
274
|
+
const target = services.find((s) => serviceId(s.env) === serviceId(serviceEnv));
|
|
275
|
+
const model = await chooseModel({ ...env, ...serviceEnv }, same ? current?.model : undefined, target?.models.map((m) => m.name));
|
|
276
|
+
if (!model) return false;
|
|
277
|
+
|
|
278
|
+
// 用过的模型带上它自己的设置
|
|
279
|
+
const saved = target?.models.find((m) => m.name === model);
|
|
280
|
+
if (target && saved) return useSaved({ ...target, env: { ...target.env, ...pick(serviceEnv, serviceKeys(kind)) } }, saved);
|
|
281
|
+
|
|
282
|
+
const updates: Env = { ...serviceEnv, [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined };
|
|
283
|
+
// 思考力度的取值每种服务不同
|
|
284
|
+
if (!same) updates.JADE_EFFORT = undefined;
|
|
285
|
+
// 其他服务常常默认只给 4096 个输出 token,带思考的模型很容易用完
|
|
286
|
+
if (kind !== "anthropic" && !env.JADE_MAX_TOKENS) {
|
|
287
|
+
const max = await ui.input({ title: "Output limit per reply in tokens, e.g. 32000, 64K (empty for the service default)", initial: "32K" }, opts.signal);
|
|
288
|
+
if (max === undefined || !tokensInput("Output limit", max)) return false;
|
|
289
|
+
if (max) updates.JADE_MAX_TOKENS = max;
|
|
290
|
+
}
|
|
291
|
+
// 同一个地址只换了密钥:原来的服务下的模型归到新的密钥下
|
|
292
|
+
const rekeyed = same && currentId !== undefined && sameAddress(env) && key !== env[KEY_KEY[kind]];
|
|
293
|
+
return applyConfig(updates, rekeyed ? currentId : undefined);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** 当前模型自己的设置(切换模型时跟着换):输出上限、思考力度、上下文窗口、限速、能不能看图 */
|
|
297
|
+
async function configureSettings() {
|
|
298
|
+
const env = opts.env();
|
|
299
|
+
const info = providerInfo(env);
|
|
300
|
+
const efforts = isBuiltinKind(info.kind) ? EFFORTS[info.kind] : undefined;
|
|
301
|
+
const action = await ui.select(
|
|
302
|
+
{
|
|
303
|
+
title: `Settings for ${info.model}`,
|
|
304
|
+
options: [
|
|
305
|
+
{ label: "Output limit per reply", value: "max", hint: env.JADE_MAX_TOKENS ? `${env.JADE_MAX_TOKENS} tokens` : "service default" },
|
|
306
|
+
...(efforts ? [{ label: "Reasoning effort", value: "effort", hint: env.JADE_EFFORT ?? (info.kind === "anthropic" ? "high (default)" : "service default") }] : []),
|
|
307
|
+
{ label: "Context window", value: "window", hint: `${contextWindow(info.model, env)} tokens${env.JADE_CONTEXT_WINDOW ? "" : " (default)"}` },
|
|
308
|
+
{ label: "Requests per minute", value: "rpm", hint: env.JADE_RPM ? `${env.JADE_RPM}/min` : "no limit (slows down after being rate limited)" },
|
|
309
|
+
{ label: "Can see images", value: "vision", hint: visionHint(env, info.kind) },
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
opts.signal,
|
|
313
|
+
);
|
|
314
|
+
if (action === "max") {
|
|
315
|
+
const max = await ui.input({ title: "Output limit per reply in tokens, e.g. 32000, 64K (empty for the service default)", initial: env.JADE_MAX_TOKENS ?? "32K" }, opts.signal);
|
|
316
|
+
if (max !== undefined && tokensInput("Output limit", max)) await applyConfig({ JADE_MAX_TOKENS: max || undefined });
|
|
317
|
+
} else if (action === "effort" && efforts) {
|
|
318
|
+
const DEFAULT = "\0default";
|
|
319
|
+
const effort = await ui.select(
|
|
320
|
+
{
|
|
321
|
+
title: "Reasoning effort",
|
|
322
|
+
options: [...efforts.map((e) => ({ label: e, value: e, ...(e === env.JADE_EFFORT ? { hint: "current" } : {}) })), { label: "Default", value: DEFAULT, hint: info.kind === "anthropic" ? "high" : "service default" }],
|
|
323
|
+
},
|
|
324
|
+
opts.signal,
|
|
325
|
+
);
|
|
326
|
+
if (effort !== undefined) await applyConfig({ JADE_EFFORT: effort === DEFAULT ? undefined : effort });
|
|
327
|
+
} else if (action === "rpm") {
|
|
328
|
+
const n = await ui.input({ title: "Requests per minute, e.g. 20 for most free models (empty for no limit)", initial: env.JADE_RPM ?? "" }, opts.signal);
|
|
329
|
+
if (n === undefined) return;
|
|
330
|
+
try {
|
|
331
|
+
rpmFromEnv({ JADE_RPM: n });
|
|
332
|
+
} catch (err) {
|
|
333
|
+
return report(errorText(err));
|
|
334
|
+
}
|
|
335
|
+
await applyConfig({ JADE_RPM: n.trim() || undefined });
|
|
336
|
+
} else if (action === "vision") {
|
|
337
|
+
const DEFAULT = "\0default";
|
|
338
|
+
const current = visionSetting(env);
|
|
339
|
+
const choice = await ui.select(
|
|
340
|
+
{
|
|
341
|
+
title: "Can this model see images (screenshots, image files)?",
|
|
342
|
+
options: [
|
|
343
|
+
{ label: "Yes", value: "on", hint: current === true ? "current" : "images are sent to the model" },
|
|
344
|
+
{ label: "No", value: "off", hint: current === false ? "current" : "images are replaced with a note" },
|
|
345
|
+
{ label: "Default", value: DEFAULT, hint: isBuiltinKind(info.kind) && info.kind !== "openai-compat" ? "yes" : "no" },
|
|
346
|
+
],
|
|
347
|
+
},
|
|
348
|
+
opts.signal,
|
|
349
|
+
);
|
|
350
|
+
if (choice === undefined) return;
|
|
351
|
+
if (await applyConfig({ JADE_VISION: choice === DEFAULT ? undefined : choice })) {
|
|
352
|
+
report(visionFromEnv(opts.env()) ? "Images will be sent to the model." : "Images will be replaced with a note.");
|
|
353
|
+
}
|
|
354
|
+
} else if (action === "window") {
|
|
355
|
+
const n = await ui.input({ title: "Context window in tokens, e.g. 128000, 512K, 1M (empty to infer it from the model name, or 128K if unknown)", initial: env.JADE_CONTEXT_WINDOW ?? "" }, opts.signal);
|
|
356
|
+
if (n !== undefined && tokensInput("Context window", n)) await applyConfig({ JADE_CONTEXT_WINDOW: n || undefined });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 从几个服务里选一个保存过的模型:只有一个服务时直接列它的模型,否则先选服务。
|
|
362
|
+
* keep 过滤可选的模型;取消时返回 undefined
|
|
363
|
+
*/
|
|
364
|
+
async function pickSaved(from: SavedService[], title: string, keep: (s: SavedService, m: SavedModel) => boolean = () => true): Promise<{ service: SavedService; model: SavedModel } | undefined> {
|
|
365
|
+
const groups = from.map((s) => ({ service: s, models: s.models.filter((m) => keep(s, m)) })).filter((g) => g.models.length > 0);
|
|
366
|
+
let group = groups[0];
|
|
367
|
+
if (groups.length > 1) {
|
|
368
|
+
const chosen = await ui.select(
|
|
369
|
+
{ title, options: groups.map((g, i) => ({ label: describeService(g.service), value: String(i), hint: modelNames(g.models) })) },
|
|
370
|
+
opts.signal,
|
|
371
|
+
);
|
|
372
|
+
if (chosen === undefined) return undefined;
|
|
373
|
+
group = groups[Number(chosen)];
|
|
374
|
+
}
|
|
375
|
+
if (!group) return undefined;
|
|
376
|
+
const chosen = await ui.select(
|
|
377
|
+
{ title: `Model (${describeService(group.service)})`, options: group.models.map((m, i) => ({ label: m.name, value: String(i) })) },
|
|
378
|
+
opts.signal,
|
|
379
|
+
);
|
|
380
|
+
if (chosen === undefined) return undefined;
|
|
381
|
+
const model = group.models[Number(chosen)];
|
|
382
|
+
return model && { service: group.service, model };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async function configureModel(arg: string | undefined) {
|
|
386
|
+
const env = opts.env();
|
|
387
|
+
// 还没有配置模型:没有"当前的服务"可以换模型,先选服务
|
|
388
|
+
if (!hasProvider(env)) return void (await configureProvider());
|
|
389
|
+
const info = providerInfo(env);
|
|
390
|
+
// 之前只有 .env 时配置的模型也记下来,之后换了不会丢
|
|
391
|
+
await remember().catch(() => {});
|
|
392
|
+
const currentId = serviceId(env);
|
|
393
|
+
const services = await savedServices();
|
|
394
|
+
/** 保存过的、不是当前的模型,按服务分组:当前服务的在前 */
|
|
395
|
+
const others = [...services.filter((s) => serviceId(s.env) === currentId), ...services.filter((s) => serviceId(s.env) !== currentId)].flatMap((s) =>
|
|
396
|
+
s.models.filter((m) => !(serviceId(s.env) === currentId && m.name === info.model)).map((m) => ({ service: s, model: m })),
|
|
397
|
+
);
|
|
398
|
+
if (arg) {
|
|
399
|
+
// 保存过的模型按名字切换(当前服务的优先),否则换当前服务的模型
|
|
400
|
+
const saved = others.find((o) => o.model.name === arg);
|
|
401
|
+
return void (await (saved ? useSaved(saved.service, saved.model) : switchModel(arg)));
|
|
402
|
+
}
|
|
403
|
+
// 主菜单只管切换:保存过的模型全部直接列出(当前的在最前,然后是同一服务的),选中就换;
|
|
404
|
+
// 添加模型、当前模型的设置、删除各占一项,细节进子菜单再看
|
|
405
|
+
const here = describeService({ env: isBuiltinKind(providerKind(env)) ? pick(env, serviceKeys(providerKind(env) as ProviderKind)) : {}, models: [] });
|
|
406
|
+
const multi = services.length > 1;
|
|
407
|
+
const action = await ui.select(
|
|
408
|
+
{
|
|
409
|
+
title: "Switch model",
|
|
410
|
+
options: [
|
|
411
|
+
{ label: info.model, value: "current", hint: multi ? `current · ${here}` : "current" },
|
|
412
|
+
...others.map((o, i) => ({ label: o.model.name, value: `saved:${i}`, ...(multi ? { hint: describeService(o.service) } : {}) })),
|
|
413
|
+
{ label: "Add a model…", value: "add", hint: "from this service or another one" },
|
|
414
|
+
{ label: `Settings for ${info.model}…`, value: "settings", hint: settingsSummary(env, info.kind, contextWindow(info.model, env)) },
|
|
415
|
+
...(others.length ? [{ label: "Remove a saved model…", value: "forget" }] : []),
|
|
416
|
+
],
|
|
417
|
+
},
|
|
418
|
+
opts.signal,
|
|
419
|
+
);
|
|
420
|
+
if (action?.startsWith("saved:")) {
|
|
421
|
+
const o = others[Number(action.slice(6))]!;
|
|
422
|
+
await useSaved(o.service, o.model);
|
|
423
|
+
} else if (action === "add") {
|
|
424
|
+
const where = await ui.select(
|
|
425
|
+
{
|
|
426
|
+
title: "Add a model from",
|
|
427
|
+
options: [
|
|
428
|
+
{ label: "This service", value: "model", hint: here },
|
|
429
|
+
{ label: "Another service…", value: "provider", hint: "or change this one's base URL / API key" },
|
|
430
|
+
],
|
|
431
|
+
},
|
|
432
|
+
opts.signal,
|
|
433
|
+
);
|
|
434
|
+
if (where === "model") {
|
|
435
|
+
const saved = services.find((s) => serviceId(s.env) === currentId)?.models.map((m) => m.name);
|
|
436
|
+
const model = await chooseModel(env, info.model, saved);
|
|
437
|
+
if (model && model !== info.model) await switchModel(model);
|
|
438
|
+
} else if (where === "provider") await configureProvider();
|
|
439
|
+
} else if (action === "settings") await configureSettings();
|
|
440
|
+
else if (action === "forget") {
|
|
441
|
+
const owners = services.filter((s) => others.some((o) => o.service === s));
|
|
442
|
+
const gone = await pickSaved(owners, "Remove a model from which service?", (s, m) => !(serviceId(s.env) === currentId && m.name === info.model));
|
|
443
|
+
if (!gone) return;
|
|
444
|
+
const id = serviceId(gone.service.env);
|
|
445
|
+
// 服务下没有模型了就连服务(和它的密钥)一起删掉;认不出的服务原样保留
|
|
446
|
+
const rest = (await readSavedServices(modelsFile))
|
|
447
|
+
.map((s) => (serviceId(s.env) === id ? { ...s, models: s.models.filter((m) => m.name !== gone.model.name) } : s))
|
|
448
|
+
.filter((s) => s.models.length > 0);
|
|
449
|
+
await writeSavedServices(modelsFile, rest);
|
|
450
|
+
report(`Removed ${gone.model.name} (${describeService(gone.service)}).`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return { provider: configureProvider, model: configureModel };
|
|
455
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// 模型的上下文窗口(最大输入 token 数)。前端显示上下文用量,以后的 jade/compact 据此决定什么时候压缩。
|
|
2
|
+
//
|
|
3
|
+
// 窗口优先取 JADE_CONTEXT_WINDOW;不设时按模型名在一张小表里找(只收录确定的、取偏小的值),找不到用 128000。
|
|
4
|
+
|
|
5
|
+
import { parseTokens } from "./config.ts";
|
|
6
|
+
|
|
7
|
+
type Env = Record<string, string | undefined>;
|
|
8
|
+
|
|
9
|
+
/** 认不出模型时的窗口 */
|
|
10
|
+
export const DEFAULT_CONTEXT_WINDOW = 128_000;
|
|
11
|
+
|
|
12
|
+
/** 模型名(不区分大小写,可以带 "anthropic/" 这类前缀)→ 窗口;按顺序取第一个匹配的 */
|
|
13
|
+
const KNOWN_WINDOWS: [RegExp, number][] = [
|
|
14
|
+
[/claude/i, 200_000],
|
|
15
|
+
[/gemini/i, 1_000_000],
|
|
16
|
+
[/gpt-4\.1/i, 1_000_000],
|
|
17
|
+
// GPT-5 的总窗口是 400k,其中输入最多 272k
|
|
18
|
+
[/gpt-5/i, 272_000],
|
|
19
|
+
[/(^|\/)o[134](-|$)/i, 200_000],
|
|
20
|
+
[/gpt-4o/i, 128_000],
|
|
21
|
+
[/deepseek/i, 128_000],
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/** 写错的 JADE_CONTEXT_WINDOW 当作没设 */
|
|
25
|
+
export function contextWindow(model: string, env: Env): number {
|
|
26
|
+
if (env.JADE_CONTEXT_WINDOW) {
|
|
27
|
+
try {
|
|
28
|
+
return parseTokens("JADE_CONTEXT_WINDOW", env.JADE_CONTEXT_WINDOW);
|
|
29
|
+
} catch {}
|
|
30
|
+
}
|
|
31
|
+
return KNOWN_WINDOWS.find(([re]) => re.test(model))?.[1] ?? DEFAULT_CONTEXT_WINDOW;
|
|
32
|
+
}
|