@uncarved/jade 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -150
- package/bin/jade.js +14 -25
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -30
- package/dist/index.js +11 -30
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +160 -1362
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +2 -0
- package/dist/update.js +6 -3
- package/dist/update.js.map +1 -1
- package/package.json +10 -8
- package/plugins/approval/index.ts +311 -0
- package/plugins/approval/plugin.json +22 -0
- package/{src/plugins → plugins/approval}/policy.ts +45 -104
- package/{src/plugins → plugins/approval}/review.ts +62 -16
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +48 -38
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +80 -39
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +2 -2
- package/{src/tools → plugins/fs}/fs.ts +89 -46
- package/{src/tools → plugins/fs}/grep.ts +36 -8
- package/plugins/fs/index.ts +37 -0
- package/plugins/fs/plugin.json +7 -0
- package/{src/ui → plugins/fs}/renderers.ts +25 -70
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +99 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +213 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +16 -119
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +160 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +111 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +258 -0
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +84 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +31 -20
- package/{src → plugins/shell}/sys.ts +2 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +168 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +343 -0
- package/plugins/todo/index.ts +93 -0
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +3 -0
- package/plugins/tui/frontend.ts +619 -0
- package/plugins/tui/index.ts +37 -0
- package/plugins/tui/mentions.ts +153 -0
- package/plugins/tui/plugin.json +10 -0
- package/{src/ui → plugins/tui}/render.ts +8 -2
- package/{src/ui → plugins/tui}/screen.ts +8 -3
- package/{src/ui → plugins/tui}/transcript.ts +79 -100
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +360 -392
- package/plugins/tui/vendor.ts +16 -0
- package/{src/tools → plugins/undo}/checkpoint.ts +45 -60
- package/plugins/undo/index.ts +143 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/{src/tools → plugins/web}/web.ts +64 -31
- package/plugins-dist/approval/index.js +287 -0
- package/plugins-dist/approval/plugin.json +28 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +41 -85
- package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +58 -34
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
- package/{dist/tools → plugins-dist/fs}/fs.js +82 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +36 -11
- package/plugins-dist/fs/index.js +39 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +87 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +181 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +18 -81
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +172 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +85 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +231 -0
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +58 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
- package/{dist → plugins-dist/shell}/sys.js +2 -43
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +152 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +284 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +31 -9
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
- package/plugins-dist/tui/frontend.js +609 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
- package/plugins-dist/tui/mentions.js +153 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/{dist/ui → plugins-dist/tui}/render.js +7 -3
- package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
- package/{dist/ui → plugins-dist/tui}/transcript.js +79 -99
- package/{dist/ui → plugins-dist/tui}/tui.js +185 -54
- package/plugins-dist/tui/vendor.js +13822 -0
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +48 -55
- package/plugins-dist/undo/index.js +143 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/{dist/tools → plugins-dist/web}/web.js +56 -29
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -30
- package/src/install.ts +355 -0
- package/src/main.ts +156 -1387
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -262
- package/LICENSE +0 -21
- package/dist/commands.d.ts +0 -50
- package/dist/commands.js +0 -126
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -141
- package/dist/distro.js +0 -462
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -294
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -25
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -71
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -47
- package/dist/plugins/core.js +0 -229
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/host.d.ts +0 -52
- package/dist/plugins/host.js +0 -115
- package/dist/plugins/host.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -410
- package/dist/plugins/loader.js +0 -407
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -98
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -43
- package/dist/plugins/prompts.js +0 -284
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -16
- package/dist/plugins/repeats.js +0 -151
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -18
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/plugins/sessions.d.ts +0 -7
- package/dist/plugins/sessions.js +0 -110
- package/dist/plugins/sessions.js.map +0 -1
- package/dist/provider.d.ts +0 -165
- package/dist/provider.js.map +0 -1
- package/dist/session-lifecycle.d.ts +0 -61
- package/dist/session-lifecycle.js +0 -167
- package/dist/session-lifecycle.js.map +0 -1
- package/dist/session-scope.d.ts +0 -53
- package/dist/session-scope.js +0 -116
- package/dist/session-scope.js.map +0 -1
- package/dist/sessions.d.ts +0 -28
- package/dist/sessions.js +0 -73
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -51
- package/dist/sys.js.map +0 -1
- package/dist/tools/ask.d.ts +0 -11
- package/dist/tools/ask.js +0 -62
- package/dist/tools/ask.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -39
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -39
- package/dist/tools/task.js +0 -117
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -53
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -447
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -41
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -22
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -14
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/renderers.d.ts +0 -8
- package/dist/ui/renderers.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -118
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -46
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -47
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -144
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -577
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -278
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -34
- package/src/plugins/core.ts +0 -257
- package/src/plugins/host.ts +0 -146
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -742
- package/src/plugins/prompts.ts +0 -327
- package/src/plugins/repeats.ts +0 -157
- package/src/plugins/sessions.ts +0 -109
- package/src/session-lifecycle.ts +0 -194
- package/src/session-scope.ts +0 -135
- package/src/sessions.ts +0 -78
- package/src/tools/ask.ts +0 -72
- package/src/tools/task.ts +0 -149
- package/src/tools/todo.ts +0 -67
- package/src/ui/configure.ts +0 -471
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
- /package/{src/ui → plugins/tui}/markdown.ts +0 -0
|
@@ -0,0 +1,213 @@
|
|
|
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, parseTokens } 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
|
+
/** 每次回复的输出上限(JADE_MAX_TOKENS,含 .env 里的);不设或写错时没有,用服务的默认值 */
|
|
29
|
+
maxTokens?: number;
|
|
30
|
+
} | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* 完成一次请求,带重试和限速。默认用当前配置的模型(限速和会话共用);
|
|
33
|
+
* model 是 /model 保存过的模型名($JADE_HOME/models.json),用它和它自己的设置,找不到时报错
|
|
34
|
+
*/
|
|
35
|
+
complete(request: ModelRequest, options: { signal: AbortSignal; onDelta?: (delta: import("uncarved").Delta) => void; model?: string }): Promise<ModelResponse>;
|
|
36
|
+
/** 服务提供的模型列表;不支持或出错时为空 */
|
|
37
|
+
list(): Promise<string[]>;
|
|
38
|
+
/** /model 保存过的模型名(complete 的 model 能用的那些),最近用过的在前 */
|
|
39
|
+
saved?(): Promise<string[]>;
|
|
40
|
+
/** 配置换了(/model)时调用,返回取消订阅。前端用它更新状态栏 */
|
|
41
|
+
subscribe?(fn: () => void): () => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default definePlugin({
|
|
45
|
+
async process(ctx) {
|
|
46
|
+
const envFile = path.join(ctx.home, ".env");
|
|
47
|
+
const modelsFile = path.join(ctx.home, "models.json");
|
|
48
|
+
// 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
|
|
49
|
+
let env = await envWithFile(envFile);
|
|
50
|
+
const ui = ctx.service<UI>("ui@1");
|
|
51
|
+
const watchers = new Set<() => void>();
|
|
52
|
+
const notify = () => {
|
|
53
|
+
for (const fn of watchers) {
|
|
54
|
+
try {
|
|
55
|
+
fn();
|
|
56
|
+
} catch {
|
|
57
|
+
// 订阅者的错不连累模型配置
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const notConfigured = () =>
|
|
63
|
+
new Error(
|
|
64
|
+
`no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:\n` +
|
|
65
|
+
" ANTHROPIC_API_KEY=... (Claude)\n" +
|
|
66
|
+
" OPENAI_API_KEY=... (OpenAI)\n" +
|
|
67
|
+
" OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
let made: Promise<Provider> | undefined;
|
|
71
|
+
// 用创建时的这份配置:之后换了配置,新的一份自己建 provider
|
|
72
|
+
const build = (snapshot: Env): Promise<Provider> => {
|
|
73
|
+
return (async () => {
|
|
74
|
+
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
75
|
+
return withRetry(await providerFromEnv(snapshot), {
|
|
76
|
+
gate,
|
|
77
|
+
onRetry: ({ attempt, retries, delayMs, error }) =>
|
|
78
|
+
ui?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
79
|
+
});
|
|
80
|
+
})();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// 第一次用时才创建:配置写错时错误出现在这一轮里,而不是让插件激活失败、连会话都打不开
|
|
84
|
+
const provider = (): Promise<Provider> => {
|
|
85
|
+
if (!hasProvider(env)) return Promise.reject(notConfigured());
|
|
86
|
+
if (made) return made;
|
|
87
|
+
const p = build(env);
|
|
88
|
+
made = p;
|
|
89
|
+
// 创建失败时下次重试(例如密钥刚写进 .env)
|
|
90
|
+
p.catch(() => {
|
|
91
|
+
if (made === p) made = undefined;
|
|
92
|
+
});
|
|
93
|
+
return p;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
|
|
97
|
+
const savedProviders = new Map<string, Promise<Provider>>();
|
|
98
|
+
const saved = async (name: string): Promise<Provider> => {
|
|
99
|
+
for (const s of await readSavedServices(modelsFile)) {
|
|
100
|
+
const m = s.models.find((x) => x.name === name);
|
|
101
|
+
const kind = providerKind(s.env);
|
|
102
|
+
if (!m || !isBuiltinKind(kind)) continue;
|
|
103
|
+
const snapshot: Env = { ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name };
|
|
104
|
+
const key = JSON.stringify(snapshot);
|
|
105
|
+
const cached = savedProviders.get(key);
|
|
106
|
+
if (cached) return cached;
|
|
107
|
+
const p = build(snapshot);
|
|
108
|
+
savedProviders.set(key, p);
|
|
109
|
+
// 创建失败时下次重试
|
|
110
|
+
p.catch(() => savedProviders.delete(key));
|
|
111
|
+
return p;
|
|
112
|
+
}
|
|
113
|
+
// 只写在 .env 里、没保存过的当前模型
|
|
114
|
+
if (hasProvider(env) && providerInfo(env).model === name) return provider();
|
|
115
|
+
throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const current: Provider["complete"] = (request, options) => provider().then((p) => p.complete(request, options));
|
|
119
|
+
const complete: Models["complete"] = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
|
|
120
|
+
|
|
121
|
+
// 转发的 provider:会话创建时固定的是这个壳,/model 之后下一次请求就用新的配置
|
|
122
|
+
ctx.provide("provider@1", { id: "jade/models", complete: current } satisfies Provider);
|
|
123
|
+
|
|
124
|
+
ctx.provide("models@1", {
|
|
125
|
+
current() {
|
|
126
|
+
if (!hasProvider(env)) return undefined;
|
|
127
|
+
const info = providerInfo(env);
|
|
128
|
+
let vision = true;
|
|
129
|
+
try {
|
|
130
|
+
vision = visionFromEnv(env);
|
|
131
|
+
} catch {
|
|
132
|
+
// 写错的 JADE_VISION 在创建 provider 时报错,这里当作不能看
|
|
133
|
+
vision = false;
|
|
134
|
+
}
|
|
135
|
+
let maxTokens: number | undefined;
|
|
136
|
+
try {
|
|
137
|
+
maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
|
|
138
|
+
} catch {
|
|
139
|
+
// 写错的在创建 provider 时报错
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
kind: info.kind,
|
|
143
|
+
model: info.model,
|
|
144
|
+
via: via(info.kind, info.baseURL),
|
|
145
|
+
contextWindow: contextWindow(info.model, env),
|
|
146
|
+
vision,
|
|
147
|
+
...(maxTokens === undefined ? {} : { maxTokens }),
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
complete,
|
|
151
|
+
list: () => listModels(env),
|
|
152
|
+
async saved() {
|
|
153
|
+
const names = (await readSavedServices(modelsFile)).flatMap((s) => (isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []));
|
|
154
|
+
return [...new Set(names)];
|
|
155
|
+
},
|
|
156
|
+
subscribe(fn: () => void) {
|
|
157
|
+
watchers.add(fn);
|
|
158
|
+
return () => void watchers.delete(fn);
|
|
159
|
+
},
|
|
160
|
+
} satisfies Models);
|
|
161
|
+
|
|
162
|
+
/** /model 写完配置之后:重新读 .env、换掉 provider;配置不完整时抛出,旧的配置和 provider 继续用 */
|
|
163
|
+
const apply = async () => {
|
|
164
|
+
const next = await envWithFile(envFile);
|
|
165
|
+
const before = { env, provider: made };
|
|
166
|
+
env = next;
|
|
167
|
+
made = undefined;
|
|
168
|
+
try {
|
|
169
|
+
await provider();
|
|
170
|
+
} catch (e) {
|
|
171
|
+
env = before.env;
|
|
172
|
+
made = before.provider;
|
|
173
|
+
throw e;
|
|
174
|
+
}
|
|
175
|
+
notify();
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
179
|
+
name: "model",
|
|
180
|
+
args: "[name]",
|
|
181
|
+
description: "switch models, or add one with its provider, API key and limits",
|
|
182
|
+
// 只改配置,不碰会话:模型工作时也立即执行
|
|
183
|
+
instant: true,
|
|
184
|
+
async run(args, c) {
|
|
185
|
+
const configure = configurator({
|
|
186
|
+
envFile,
|
|
187
|
+
modelsFile,
|
|
188
|
+
env: () => env,
|
|
189
|
+
apply,
|
|
190
|
+
report: (text) => c.print(text),
|
|
191
|
+
ui: c.ui,
|
|
192
|
+
signal: c.signal,
|
|
193
|
+
});
|
|
194
|
+
await configure.model(args.trim() || undefined);
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
function via(kind: string, baseURL: string | undefined): string {
|
|
201
|
+
const host = baseURL ? safeHost(baseURL) : undefined;
|
|
202
|
+
if (kind === "anthropic") return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
|
|
203
|
+
if (kind === "openai") return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
|
|
204
|
+
return host ? `an OpenAI-compatible API at ${host}` : `the ${kind} provider`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function safeHost(url: string): string | undefined {
|
|
208
|
+
try {
|
|
209
|
+
return new URL(url).host;
|
|
210
|
+
} catch {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -184,72 +184,10 @@ type Env = Record<string, string | undefined>;
|
|
|
184
184
|
|
|
185
185
|
export const isBuiltinKind = (kind: string): kind is ProviderKind => (PROVIDER_KINDS as readonly string[]).includes(kind);
|
|
186
186
|
|
|
187
|
-
/**
|
|
188
|
-
export
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
/** 当前配置的模型名(状态栏、/model 显示,上下文窗口按它推断);env 是 jade 的环境变量(含 $JADE_HOME/.env) */
|
|
192
|
-
model(env: Env): string;
|
|
193
|
-
/** 系统提示词里告诉模型它经什么服务运行,例如 "the Foo API at api.foo.com";不给时是 "the <kind> provider" */
|
|
194
|
-
via?(env: Env): string;
|
|
195
|
-
/** 按 env 创建 Provider(uncarved 的接口);缺配置时抛错,信息直接显示给用户。jade 在外面包一层重试和限速 */
|
|
196
|
-
create(env: Env): Provider | Promise<Provider>;
|
|
197
|
-
/** /model 里列出可选的模型;不给时 /model 不能列出模型 */
|
|
198
|
-
listModels?(env: Env): Promise<string[]>;
|
|
199
|
-
/** 服务的配置项:/model 添加这个服务时依次询问,写进 $JADE_HOME/.env;不给时只能手动写 .env */
|
|
200
|
-
settings?: ProviderSetting[];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** 插件服务的一个配置项,对应一个环境变量 */
|
|
204
|
-
export interface ProviderSetting {
|
|
205
|
-
/** 环境变量名,大写字母、数字、_,例如 "FOO_API_KEY";不能是 JADE_ 开头的 */
|
|
206
|
-
env: string;
|
|
207
|
-
/** /model 里显示的名字,例如 "API key" */
|
|
208
|
-
label: string;
|
|
209
|
-
/** 输入时遮住(密钥);留空时沿用已有的值 */
|
|
210
|
-
secret?: boolean;
|
|
211
|
-
/** 这一项是模型名:/model 从 listModels 里选、换模型时只改它。最多一项 */
|
|
212
|
-
model?: boolean;
|
|
213
|
-
/** 不能为空 */
|
|
214
|
-
required?: boolean;
|
|
215
|
-
/** 输入框里的提示文字 */
|
|
216
|
-
placeholder?: string;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export const SETTING_ENV = /^[A-Z][A-Z0-9_]*$/;
|
|
220
|
-
|
|
221
|
-
export const PROVIDER_KIND = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
222
|
-
|
|
223
|
-
/** 插件注册的服务;只在启动时注册一次,整个进程共用 */
|
|
224
|
-
const plugged = new Map<string, { plugin: string; def: ProviderDef }>();
|
|
225
|
-
|
|
226
|
-
/** 注册插件提供的服务;返回要告诉用户的问题(名字不合法、和内置的或先注册的重名) */
|
|
227
|
-
export function registerProviders(list: { plugin: string; def: ProviderDef }[]): string[] {
|
|
228
|
-
const warnings: string[] = [];
|
|
229
|
-
for (const { plugin, def } of list) {
|
|
230
|
-
const kind = def.kind;
|
|
231
|
-
const taken = plugged.get(kind);
|
|
232
|
-
if (isBuiltinKind(kind)) warnings.push(`plugin ${plugin}: provider ${kind} is ignored: it is a built-in provider`);
|
|
233
|
-
else if (taken) warnings.push(`plugin ${plugin}: provider ${kind} is ignored: plugin ${taken.plugin} already provides it`);
|
|
234
|
-
else plugged.set(kind, { plugin, def });
|
|
235
|
-
}
|
|
236
|
-
return warnings;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** JADE_PROVIDER 选的服务由哪个插件提供;内置的或没有插件提供时 undefined */
|
|
240
|
-
export function pluginProvider(kind: string): { plugin: string; def: ProviderDef } | undefined {
|
|
241
|
-
return plugged.get(kind);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/** 注册了的插件服务,按注册顺序 */
|
|
245
|
-
export function pluginProviders(): { plugin: string; def: ProviderDef }[] {
|
|
246
|
-
return [...plugged.values()];
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/** 测试用 */
|
|
250
|
-
export function clearProviders(): void {
|
|
251
|
-
plugged.clear();
|
|
252
|
-
}
|
|
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" };
|
|
253
191
|
|
|
254
192
|
/** JADE_EFFORT 可取的值;openai-compat 没有统一的字段,用 OPENAI_COMPAT_EXTRA_BODY 自己设 */
|
|
255
193
|
export const EFFORTS: Record<ProviderKind, readonly string[] | undefined> = {
|
|
@@ -285,7 +223,6 @@ export function idleTimeoutFromEnv(env: Env = process.env): number | undefined {
|
|
|
285
223
|
*/
|
|
286
224
|
export function visionFromEnv(env: Env = process.env): boolean {
|
|
287
225
|
const text = env.JADE_VISION?.trim().toLowerCase();
|
|
288
|
-
// 插件的服务也算不能看:不知道它的模型支不支持
|
|
289
226
|
if (!text) return providerKind(env) === "openai" || providerKind(env) === "anthropic";
|
|
290
227
|
if (["1", "true", "on", "yes"].includes(text)) return true;
|
|
291
228
|
if (["0", "false", "off", "no"].includes(text)) return false;
|
|
@@ -297,7 +234,6 @@ export function effortFromEnv(env: Env = process.env): string | undefined {
|
|
|
297
234
|
const effort = env.JADE_EFFORT;
|
|
298
235
|
if (!effort) return undefined;
|
|
299
236
|
const kind = providerKind(env);
|
|
300
|
-
// 插件的服务自己从 env 里读、自己检查
|
|
301
237
|
if (!isBuiltinKind(kind)) return effort;
|
|
302
238
|
const allowed = EFFORTS[kind];
|
|
303
239
|
if (!allowed) throw new Error(`JADE_EFFORT is not supported for the ${kind} provider; set it in OPENAI_COMPAT_EXTRA_BODY instead`);
|
|
@@ -305,11 +241,7 @@ export function effortFromEnv(env: Env = process.env): string | undefined {
|
|
|
305
241
|
return effort;
|
|
306
242
|
}
|
|
307
243
|
|
|
308
|
-
/** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY
|
|
309
|
-
/**
|
|
310
|
-
* 当前配置的服务:内置的三种之一,或者 JADE_PROVIDER 写的插件服务的名字。
|
|
311
|
-
* 写了没有插件提供的名字时也原样返回(插件可能还没加载),创建 provider 时才报错
|
|
312
|
-
*/
|
|
244
|
+
/** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY 的顺序。不认识的名字原样返回,创建时报错 */
|
|
313
245
|
export function providerKind(env: Env = process.env): string {
|
|
314
246
|
const named = env.JADE_PROVIDER;
|
|
315
247
|
if (named) return named;
|
|
@@ -329,17 +261,7 @@ export function providerInfo(env: Env = process.env): ProviderInfo {
|
|
|
329
261
|
const kind = providerKind(env);
|
|
330
262
|
if (kind === "openai") return { kind, model: env.OPENAI_MODEL ?? "gpt-5", baseURL: env.OPENAI_BASE_URL ?? "https://api.openai.com/v1" };
|
|
331
263
|
if (kind === "openai-compat") return { kind, model: env.OPENAI_COMPAT_MODEL ?? "", baseURL: env.OPENAI_COMPAT_BASE_URL };
|
|
332
|
-
|
|
333
|
-
return { kind, model: pluginModel(kind, env) };
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** 插件服务的模型名;插件还没注册或出错时为空 */
|
|
337
|
-
function pluginModel(kind: string, env: Env): string {
|
|
338
|
-
try {
|
|
339
|
-
return String(plugged.get(kind)?.def.model(env) ?? "");
|
|
340
|
-
} catch {
|
|
341
|
-
return "";
|
|
342
|
-
}
|
|
264
|
+
return { kind, model: env.ANTHROPIC_MODEL ?? "claude-opus-5-5", baseURL: env.ANTHROPIC_BASE_URL };
|
|
343
265
|
}
|
|
344
266
|
|
|
345
267
|
/**
|
|
@@ -357,20 +279,7 @@ function pluginModel(kind: string, env: Env): string {
|
|
|
357
279
|
*/
|
|
358
280
|
export async function providerFromEnv(env: Env = process.env): Promise<Provider> {
|
|
359
281
|
const kind = providerKind(env);
|
|
360
|
-
if (!isBuiltinKind(kind)) {
|
|
361
|
-
const found = plugged.get(kind);
|
|
362
|
-
if (!found) {
|
|
363
|
-
const all = [...PROVIDER_KINDS, ...plugged.keys()];
|
|
364
|
-
throw new Error(`JADE_PROVIDER must be one of ${all.join(", ")}, got ${JSON.stringify(kind)} (no plugin provides it)`);
|
|
365
|
-
}
|
|
366
|
-
// 限速、空闲超时这些 jade 自己的设置写错时同样在这里报错。空闲超时只对 OpenAI 和 OpenAI 兼容服务生效(见 README),
|
|
367
|
-
// 插件的服务要用的话自己从 env 读
|
|
368
|
-
rpmFromEnv(env);
|
|
369
|
-
idleTimeoutFromEnv(env);
|
|
370
|
-
const made = await found.def.create(env);
|
|
371
|
-
if (!made || typeof made.complete !== "function") throw new Error(`plugin ${found.plugin}: provider ${kind} must return an object with a complete function`);
|
|
372
|
-
return made;
|
|
373
|
-
}
|
|
282
|
+
if (!isBuiltinKind(kind)) throw new Error(`JADE_PROVIDER must be one of ${PROVIDER_KINDS.join(", ")}, got ${JSON.stringify(kind)}`);
|
|
374
283
|
const maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
|
|
375
284
|
const effort = effortFromEnv(env);
|
|
376
285
|
// 写错时在这里就报错,不等到第一次请求
|
|
@@ -407,32 +316,20 @@ export async function providerFromEnv(env: Env = process.env): Promise<Provider>
|
|
|
407
316
|
});
|
|
408
317
|
}
|
|
409
318
|
const { anthropicProvider } = await import("@uncarved/provider-anthropic");
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
export function switchable(initial: Provider): SwitchableProvider {
|
|
419
|
-
let current = initial;
|
|
420
|
-
return {
|
|
421
|
-
id: "switchable",
|
|
422
|
-
set: (next) => {
|
|
423
|
-
current = next;
|
|
424
|
-
},
|
|
425
|
-
complete: (request, options) => current.complete(request, options),
|
|
426
|
-
};
|
|
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
|
+
});
|
|
427
327
|
}
|
|
428
328
|
|
|
429
329
|
/** 服务提供的模型列表;不支持或出错时返回空数组 */
|
|
430
330
|
export async function listModels(env: Env = process.env, fetchImpl: typeof fetch = fetch): Promise<string[]> {
|
|
431
331
|
const kind = providerKind(env);
|
|
432
|
-
if (!isBuiltinKind(kind))
|
|
433
|
-
const list = plugged.get(kind)?.def.listModels;
|
|
434
|
-
return list ? list(env).then((m) => (Array.isArray(m) ? m.filter((x) => typeof x === "string") : []), () => []) : [];
|
|
435
|
-
}
|
|
332
|
+
if (!isBuiltinKind(kind)) return [];
|
|
436
333
|
const info = providerInfo(env);
|
|
437
334
|
const base = (info.baseURL ?? "https://api.anthropic.com").replace(/\/$/, "");
|
|
438
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
|
+
}
|