@uncarved/jade 0.2.0 → 0.3.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/README.md +277 -0
- package/assets/logo.svg +1 -0
- package/bin/jade.js +15 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +28 -5
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +6 -4
- package/plugins/approval/index.ts +246 -84
- package/plugins/approval/plugin.json +5 -1
- package/plugins/approval/policy.ts +27 -6
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +15 -4
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +110 -17
- package/plugins/fs/grep.ts +41 -11
- package/plugins/fs/index.ts +9 -7
- package/plugins/fs/renderers.ts +1 -1
- package/plugins/hooks/index.ts +152 -0
- package/plugins/hooks/plugin.json +14 -0
- package/plugins/keys/index.ts +2 -18
- package/plugins/mcp/index.ts +40 -12
- package/plugins/mcp/install.ts +139 -0
- package/plugins/mcp/mcp.ts +2 -1
- package/plugins/mcp/plugin.json +1 -1
- package/plugins/mcp/project.ts +37 -1
- package/plugins/memory/index.ts +4 -9
- package/plugins/memory/plugin.json +1 -1
- package/plugins/mentions/index.ts +33 -13
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +50 -42
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +22 -4
- package/plugins/models/saved.ts +8 -4
- package/plugins/modes/index.ts +2 -17
- package/plugins/plugins/index.ts +2 -2
- package/plugins/print/index.ts +60 -12
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +50 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +14 -8
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +16 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +1 -1
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/index.ts +26 -13
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +56 -6
- package/plugins/shell/plugin.json +3 -1
- package/plugins/shell/renderers.ts +1 -1
- package/plugins/shell/service.ts +2 -13
- package/plugins/shell/shell.ts +31 -16
- package/plugins/skills/index.ts +172 -37
- package/plugins/skills/install.ts +212 -0
- package/plugins/skills/plugin.json +3 -2
- package/plugins/skills/project.ts +38 -1
- package/plugins/skills/skills.ts +28 -9
- package/plugins/steer/index.ts +142 -0
- package/plugins/steer/plugin.json +8 -0
- package/plugins/suggest/index.ts +16 -11
- package/plugins/suggest/plugin.json +1 -1
- package/plugins/task/index.ts +8 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +119 -15
- package/plugins/todo/index.ts +4 -4
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +130 -25
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -0
- package/plugins/tui/mentions.ts +48 -8
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +22 -3
- package/plugins/tui/tui.ts +323 -95
- package/plugins/tui/vendor.ts +16 -0
- package/plugins/undo/checkpoint.ts +26 -2
- package/plugins/undo/index.ts +86 -24
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +2 -5
- package/plugins/web/renderers.ts +1 -1
- package/plugins/web/web.ts +6 -5
- package/plugins-dist/approval/index.js +232 -50
- package/plugins-dist/approval/plugin.json +5 -1
- package/plugins-dist/approval/policy.js +27 -5
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +13 -2
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +106 -17
- package/plugins-dist/fs/grep.js +40 -12
- package/plugins-dist/fs/index.js +8 -5
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +41 -9
- package/plugins-dist/mcp/install.js +160 -0
- package/plugins-dist/mcp/mcp.js +2 -1
- package/plugins-dist/mcp/plugin.json +2 -1
- package/plugins-dist/mcp/project.js +42 -1
- package/plugins-dist/memory/index.js +3 -5
- package/plugins-dist/memory/plugin.json +1 -1
- package/plugins-dist/mentions/index.js +29 -11
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +53 -16
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +4 -2
- package/plugins-dist/models/saved.js +9 -4
- package/plugins-dist/modes/index.js +1 -1
- package/plugins-dist/plugins/index.js +1 -1
- package/plugins-dist/print/index.js +62 -9
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +49 -2
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +13 -8
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +15 -3
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +1 -1
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/index.js +25 -11
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +55 -3
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +26 -8
- package/plugins-dist/skills/index.js +184 -35
- package/plugins-dist/skills/install.js +178 -0
- package/plugins-dist/skills/plugin.json +6 -2
- package/plugins-dist/skills/project.js +43 -1
- package/plugins-dist/skills/skills.js +27 -10
- package/plugins-dist/steer/index.js +135 -0
- package/plugins-dist/steer/plugin.json +12 -0
- package/plugins-dist/suggest/index.js +15 -9
- package/plugins-dist/suggest/plugin.json +1 -1
- package/plugins-dist/task/index.js +7 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +106 -15
- package/plugins-dist/todo/index.js +3 -3
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +135 -18
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +439 -0
- package/plugins-dist/tui/mentions.js +44 -6
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +19 -2
- package/plugins-dist/tui/tui.js +303 -83
- package/plugins-dist/tui/vendor.js +13822 -0
- package/plugins-dist/undo/checkpoint.js +28 -1
- package/plugins-dist/undo/index.js +92 -22
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +1 -1
- package/plugins-dist/web/web.js +6 -5
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +25 -6
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// jade/memory:跨会话的记忆(见 memory.ts)。memory 工具给模型增删,/memory 给用户查看和管理,
|
|
2
2
|
// 会话开始时经 prompts@1 把记忆放进系统提示词(会话中途的改动下一个会话才进提示词,这样提示缓存不失效;
|
|
3
3
|
// 用户在 /memory 里改了,下一次请求时附一条说明告诉模型)。
|
|
4
|
-
import { definePlugin } from "
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
5
|
import { Memory, MEMORY_SCOPES, memoryTool, projectRoot } from "./memory.js";
|
|
6
|
-
import { Notes } from "./notes.js";
|
|
7
6
|
import { clip, dim, green, red } from "./style.js";
|
|
8
|
-
const notes = new Notes();
|
|
9
7
|
const args = (input) => (input ?? {});
|
|
10
8
|
export default definePlugin({
|
|
11
9
|
process(ctx) {
|
|
@@ -29,8 +27,9 @@ export default definePlugin({
|
|
|
29
27
|
async run(input, { ui, sessions, signal }) {
|
|
30
28
|
const note = (text) => {
|
|
31
29
|
const id = sessions.current?.id;
|
|
30
|
+
// 用到的时候再取:可选服务在 process() 开头不一定已经有了
|
|
32
31
|
if (id)
|
|
33
|
-
|
|
32
|
+
ctx.service("prompts@1")?.note(id, text);
|
|
34
33
|
};
|
|
35
34
|
let text = input.trim();
|
|
36
35
|
if (!text) {
|
|
@@ -85,7 +84,6 @@ export default definePlugin({
|
|
|
85
84
|
async session(ctx) {
|
|
86
85
|
const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
|
|
87
86
|
ctx.kernel.tool(memoryTool(memory));
|
|
88
|
-
ctx.kernel.hook(notes.hook(ctx.session.id, ctx.plugin, ctx.plugin));
|
|
89
87
|
const text = await memory.prompt();
|
|
90
88
|
// 段落随会话作用域撤销
|
|
91
89
|
if (text)
|
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
// 内核没有改写用户输入的扩展点:input.received 原样进日志。所以在 context.project 里把附件加到引用它的那条用户消息后面。
|
|
4
4
|
// 附件在消息之后第一次投影时读取,先用 ctx.record 写进日志(derived)再使用(I-5):之后每次投影都从 derived 里取,
|
|
5
5
|
// 文件之后变了也不影响已经发出的消息(提示缓存不失效),恢复会话后也一样。
|
|
6
|
+
// 每条消息的附件单独记一个 key(attachedKey):derived.recorded 是整值替换,记成一张表的话,
|
|
7
|
+
// 每次有新的 @ 都要把之前附过的全部内容再写一遍,日志按平方增长。
|
|
6
8
|
// 输入框里 @ 的补全不在这里,在 jade/tui。
|
|
7
|
-
import { definePlugin } from "
|
|
9
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
10
|
import { attachMentions } from "./attach.js";
|
|
9
11
|
import { clip, dim, yellow } from "./style.js";
|
|
12
|
+
/** 附在第 i 条消息后面的内容记在这个 key 下 */
|
|
13
|
+
export const attachedKey = (plugin, i) => `${plugin}/${i}`;
|
|
10
14
|
/** 读文件的时限 */
|
|
11
15
|
const READ_TIMEOUT_MS = 30_000;
|
|
12
16
|
/** 用户自己写的文字;工具结果、插件加的说明不算 */
|
|
@@ -39,11 +43,20 @@ export default definePlugin({
|
|
|
39
43
|
optional: true,
|
|
40
44
|
timeoutMs: READ_TIMEOUT_MS + 5_000,
|
|
41
45
|
fn: async (messages, hook) => {
|
|
42
|
-
|
|
43
|
-
const start = Math.max(state
|
|
44
|
-
|
|
46
|
+
const state = hook.derived[key];
|
|
47
|
+
const start = Math.max(state?.upTo ?? 0, scanned);
|
|
48
|
+
/** 这次记下的;hook.derived 是调用时的快照,刚记下的不在里面 */
|
|
49
|
+
const fresh = new Map();
|
|
50
|
+
if (state?.attached) {
|
|
51
|
+
// 旧格式的会话:先拆成每条消息一个 key,进度里去掉整表,之后不再随新的 @ 重写
|
|
52
|
+
for (const [i, blocks] of Object.entries(state.attached)) {
|
|
53
|
+
await hook.record(attachedKey(key, Number(i)), blocks);
|
|
54
|
+
fresh.set(Number(i), blocks);
|
|
55
|
+
}
|
|
56
|
+
await hook.record(key, { upTo: state.upTo });
|
|
57
|
+
}
|
|
45
58
|
if (messages.length > start) {
|
|
46
|
-
|
|
59
|
+
let changed = false;
|
|
47
60
|
const signal = AbortSignal.any([hook.signal, AbortSignal.timeout(READ_TIMEOUT_MS)]);
|
|
48
61
|
for (let i = start; i < messages.length; i++) {
|
|
49
62
|
const text = userText(messages[i]);
|
|
@@ -51,18 +64,23 @@ export default definePlugin({
|
|
|
51
64
|
continue;
|
|
52
65
|
changed = true;
|
|
53
66
|
const refs = await attachMentions(text, { cwd: ctx.cwd, plugin: ctx.plugin, signal });
|
|
54
|
-
if (refs.blocks.length)
|
|
55
|
-
|
|
67
|
+
if (refs.blocks.length) {
|
|
68
|
+
await hook.record(attachedKey(key, i), refs.blocks);
|
|
69
|
+
fresh.set(i, refs.blocks);
|
|
70
|
+
}
|
|
56
71
|
if (refs.attached.length || refs.failed.length)
|
|
57
72
|
await hook.emit("attached", { attached: refs.attached, failed: refs.failed });
|
|
58
73
|
}
|
|
59
|
-
|
|
60
|
-
//
|
|
74
|
+
// 处理过带 @ 的消息才记:恢复的会话不再重新读这些消息引用的文件(文件可能已经变了)
|
|
75
|
+
// 附件先于进度记下:两次记录之间崩溃时请求还没发出,恢复后重新处理这条消息即可
|
|
61
76
|
if (changed)
|
|
62
|
-
await hook.record(key,
|
|
77
|
+
await hook.record(key, { upTo: messages.length });
|
|
63
78
|
scanned = messages.length;
|
|
64
79
|
}
|
|
65
|
-
return messages.map((m, i) =>
|
|
80
|
+
return messages.map((m, i) => {
|
|
81
|
+
const extra = fresh.get(i) ?? hook.derived[attachedKey(key, i)];
|
|
82
|
+
return extra ? { ...m, content: [...m.content, ...extra] } : m;
|
|
83
|
+
});
|
|
66
84
|
},
|
|
67
85
|
});
|
|
68
86
|
},
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
7
7
|
import { hasProvider, parseTokens } from "./config.js";
|
|
8
8
|
import { contextWindow } from "./context.js";
|
|
9
|
-
import { EFFORTS, isBuiltinKind, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv } from "./provider.js";
|
|
9
|
+
import { EFFORTS, isBuiltinKind, jsonObject, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv } from "./provider.js";
|
|
10
10
|
import { MODEL_SETTINGS, readSavedServices, updateEnvFile, writeSavedServices } from "./saved.js";
|
|
11
11
|
const PROVIDERS = [
|
|
12
12
|
{ kind: "openai-compat", label: "OpenAI-compatible service", hint: "any /chat/completions endpoint" },
|
|
@@ -29,7 +29,7 @@ function visionHint(env, kind) {
|
|
|
29
29
|
return v === undefined ? `${isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no"} (default)` : v ? "yes" : "no";
|
|
30
30
|
}
|
|
31
31
|
/** 一个服务包含的变量(不含模型名) */
|
|
32
|
-
const serviceKeys = (kind) => ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind]
|
|
32
|
+
const serviceKeys = (kind) => ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind]];
|
|
33
33
|
const pick = (env, keys) => {
|
|
34
34
|
const out = {};
|
|
35
35
|
for (const k of keys)
|
|
@@ -37,6 +37,14 @@ const pick = (env, keys) => {
|
|
|
37
37
|
out[k] = env[k];
|
|
38
38
|
return out;
|
|
39
39
|
};
|
|
40
|
+
/** keys 里明确清掉(设成默认)的那些:空字符串表示"不设",写进 models.json 时删掉;env 里没提到的不算 */
|
|
41
|
+
const blank = (env, keys) => {
|
|
42
|
+
const out = {};
|
|
43
|
+
for (const k of keys)
|
|
44
|
+
if (k in env && env[k] === undefined)
|
|
45
|
+
out[k] = "";
|
|
46
|
+
return out;
|
|
47
|
+
};
|
|
40
48
|
/** 服务的类型、地址和密钥都相同的算同一个;配置无效时返回 undefined */
|
|
41
49
|
function serviceId(env) {
|
|
42
50
|
try {
|
|
@@ -59,25 +67,28 @@ function describeService(service) {
|
|
|
59
67
|
catch { }
|
|
60
68
|
return [i.kind, host].filter(Boolean).join(" · ");
|
|
61
69
|
}
|
|
62
|
-
/**
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
return models.length > 3 ? `${names} +${models.length - 3}` : names;
|
|
70
|
+
/** 保存过的、不是当前的模型,按服务分组:当前服务的在前 */
|
|
71
|
+
function otherModels(services, currentId, currentModel) {
|
|
72
|
+
return [...services.filter((s) => serviceId(s.env) === currentId), ...services.filter((s) => serviceId(s.env) !== currentId)].flatMap((s) => s.models.filter((m) => !(serviceId(s.env) === currentId && m.name === currentModel)).map((m) => ({ service: s, model: m })));
|
|
66
73
|
}
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
/** Claude 不设输出上限时用的值:和 @uncarved/provider-anthropic 里的默认值一致(不从那里导入:加载它的 SDK 要一百多毫秒) */
|
|
75
|
+
const ANTHROPIC_DEFAULT_MAX_TOKENS = 64000;
|
|
76
|
+
/** 选项说明里放不下的长文本截短 */
|
|
77
|
+
const shorten = (text, max = 40) => (text.length > max ? `${text.slice(0, max - 1)}…` : text);
|
|
78
|
+
const errorText = (err) => (err instanceof Error ? err.message : String(err));
|
|
79
|
+
/** 模型设置那一项的简要说明,例如 "64K out · 1M ctx · images";settings 是这个模型自己的设置变量 */
|
|
80
|
+
function settingsSummary(settings, kind, window) {
|
|
81
|
+
const images = visionSetting({ JADE_VISION: settings.JADE_VISION }) ?? (isBuiltinKind(kind) && kind !== "openai-compat");
|
|
70
82
|
return [
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
`${
|
|
74
|
-
|
|
83
|
+
settings.JADE_MAX_TOKENS && `${settings.JADE_MAX_TOKENS} out`,
|
|
84
|
+
settings.JADE_EFFORT && `${settings.JADE_EFFORT} effort`,
|
|
85
|
+
`${settings.JADE_CONTEXT_WINDOW ?? window} ctx`,
|
|
86
|
+
settings.JADE_RPM && `${settings.JADE_RPM}/min`,
|
|
75
87
|
images ? "images" : "no images",
|
|
76
88
|
]
|
|
77
89
|
.filter(Boolean)
|
|
78
90
|
.join(" · ");
|
|
79
91
|
}
|
|
80
|
-
const errorText = (err) => (err instanceof Error ? err.message : String(err));
|
|
81
92
|
export function configurator(opts) {
|
|
82
93
|
const { ui, envFile, modelsFile } = opts;
|
|
83
94
|
const report = (text) => opts.report(text);
|
|
@@ -158,7 +169,7 @@ export function configurator(opts) {
|
|
|
158
169
|
return applyConfig(updates);
|
|
159
170
|
}
|
|
160
171
|
/**
|
|
161
|
-
* 在当前的服务上换成 model
|
|
172
|
+
* 在当前的服务上换成 model:用过的带上它自己的设置;新的模型沿用输出上限、思考力度和额外请求参数,
|
|
162
173
|
* 上下文窗口不沿用(不同模型差别很大),按模型名推断
|
|
163
174
|
*/
|
|
164
175
|
async function switchModel(model) {
|
|
@@ -175,15 +186,18 @@ export function configurator(opts) {
|
|
|
175
186
|
}
|
|
176
187
|
return applyConfig({ [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined });
|
|
177
188
|
}
|
|
178
|
-
/**
|
|
189
|
+
/** 选模型:服务能列出模型时从列表里选,否则手动输入。选 Back 和取消一样返回 undefined */
|
|
179
190
|
async function chooseModel(env, current, saved = []) {
|
|
180
191
|
const models = await listModels(env, opts.fetch);
|
|
192
|
+
const BACK = "\0back";
|
|
181
193
|
if (models.length > 0) {
|
|
182
194
|
const OTHER = "\0other";
|
|
183
195
|
const picked = await ui.select({
|
|
184
196
|
title: "Choose a model",
|
|
185
|
-
options: [...models.map((m) => ({ label: m, value: m, ...(m === current ? { hint: "current" } : saved.includes(m) ? { hint: "saved" } : {}) })), { label: "Enter a name…", value: OTHER }],
|
|
197
|
+
options: [...models.map((m) => ({ label: m, value: m, ...(m === current ? { hint: "current" } : saved.includes(m) ? { hint: "saved" } : {}) })), { label: "Enter a name…", value: OTHER }, { label: "Back", value: BACK }],
|
|
186
198
|
}, opts.signal);
|
|
199
|
+
if (picked === BACK)
|
|
200
|
+
return undefined;
|
|
187
201
|
if (picked !== OTHER)
|
|
188
202
|
return picked;
|
|
189
203
|
}
|
|
@@ -229,11 +243,6 @@ export function configurator(opts) {
|
|
|
229
243
|
return false;
|
|
230
244
|
const key = answer || known;
|
|
231
245
|
const serviceEnv = { JADE_PROVIDER: kind, [URL_KEY[kind]]: baseURL, [KEY_KEY[kind]]: key };
|
|
232
|
-
if (kind === "openai-compat") {
|
|
233
|
-
// 额外的请求参数跟着服务走:同一个服务保留,换了服务用保存过的那份
|
|
234
|
-
const saved = services.find((s) => serviceId(s.env) === serviceId(serviceEnv));
|
|
235
|
-
serviceEnv.OPENAI_COMPAT_EXTRA_BODY = saved ? saved.env.OPENAI_COMPAT_EXTRA_BODY : sameAddress(env) ? env.OPENAI_COMPAT_EXTRA_BODY : undefined;
|
|
236
|
-
}
|
|
237
246
|
const target = services.find((s) => serviceId(s.env) === serviceId(serviceEnv));
|
|
238
247
|
const model = await chooseModel({ ...env, ...serviceEnv }, same ? current?.model : undefined, target?.models.map((m) => m.name));
|
|
239
248
|
if (!model)
|
|
@@ -243,11 +252,15 @@ export function configurator(opts) {
|
|
|
243
252
|
if (target && saved)
|
|
244
253
|
return useSaved({ ...target, env: { ...target.env, ...pick(serviceEnv, serviceKeys(kind)) } }, saved);
|
|
245
254
|
const updates = { ...serviceEnv, [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined };
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
|
|
255
|
+
// 换了服务的新模型从新服务的默认值开始:思考力度、额外请求参数各服务写法不同,限速、输出上限、
|
|
256
|
+
// 能不能看图也是跟着服务和模型走的,沿用当前的容易错(例如免费服务的限速带到付费服务上)。
|
|
257
|
+
// 同一个服务(只换了密钥)照旧沿用
|
|
258
|
+
const sameService = same && sameAddress(env);
|
|
259
|
+
if (!sameService)
|
|
260
|
+
for (const k of MODEL_SETTINGS)
|
|
261
|
+
updates[k] = undefined;
|
|
249
262
|
// 其他服务常常默认只给 4096 个输出 token,带思考的模型很容易用完
|
|
250
|
-
if (kind !== "anthropic" && !env.JADE_MAX_TOKENS) {
|
|
263
|
+
if (kind !== "anthropic" && !(sameService && env.JADE_MAX_TOKENS)) {
|
|
251
264
|
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);
|
|
252
265
|
if (max === undefined || !tokensInput("Output limit", max))
|
|
253
266
|
return false;
|
|
@@ -255,160 +268,248 @@ export function configurator(opts) {
|
|
|
255
268
|
updates.JADE_MAX_TOKENS = max;
|
|
256
269
|
}
|
|
257
270
|
// 同一个地址只换了密钥:原来的服务下的模型归到新的密钥下
|
|
258
|
-
const rekeyed =
|
|
271
|
+
const rekeyed = sameService && currentId !== undefined && key !== env[KEY_KEY[kind]];
|
|
259
272
|
return applyConfig(updates, rekeyed ? currentId : undefined);
|
|
260
273
|
}
|
|
261
|
-
/**
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
{ label: "Requests per minute", value: "rpm", hint: env.JADE_RPM ? `${env.JADE_RPM}/min` : "no limit (slows down after being rate limited)" },
|
|
273
|
-
{ label: "Can see images", value: "vision", hint: visionHint(env, info.kind) },
|
|
274
|
-
],
|
|
275
|
-
}, opts.signal);
|
|
276
|
-
if (action === "max") {
|
|
277
|
-
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);
|
|
278
|
-
if (max !== undefined && tokensInput("Output limit", max))
|
|
279
|
-
await applyConfig({ JADE_MAX_TOKENS: max || undefined });
|
|
280
|
-
}
|
|
281
|
-
else if (action === "effort" && efforts) {
|
|
282
|
-
const DEFAULT = "\0default";
|
|
283
|
-
const effort = await ui.select({
|
|
284
|
-
title: "Reasoning effort",
|
|
285
|
-
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" }],
|
|
286
|
-
}, opts.signal);
|
|
287
|
-
if (effort !== undefined)
|
|
288
|
-
await applyConfig({ JADE_EFFORT: effort === DEFAULT ? undefined : effort });
|
|
289
|
-
}
|
|
290
|
-
else if (action === "rpm") {
|
|
291
|
-
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);
|
|
292
|
-
if (n === undefined)
|
|
293
|
-
return;
|
|
294
|
-
try {
|
|
295
|
-
rpmFromEnv({ JADE_RPM: n });
|
|
296
|
-
}
|
|
297
|
-
catch (err) {
|
|
298
|
-
return report(errorText(err));
|
|
299
|
-
}
|
|
300
|
-
await applyConfig({ JADE_RPM: n.trim() || undefined });
|
|
301
|
-
}
|
|
302
|
-
else if (action === "vision") {
|
|
303
|
-
const DEFAULT = "\0default";
|
|
304
|
-
const current = visionSetting(env);
|
|
305
|
-
const choice = await ui.select({
|
|
306
|
-
title: "Can this model see images (screenshots, image files)?",
|
|
274
|
+
/**
|
|
275
|
+
* 某个模型的设置(输出上限、思考力度、上下文窗口、限速、能不能看图,openai-compat 还有额外请求参数)。
|
|
276
|
+
* 改完一项回到这个菜单接着改(重新读一遍,显示的是改过的值);Back 或 Esc 才离开
|
|
277
|
+
*/
|
|
278
|
+
async function configureSettings(target) {
|
|
279
|
+
const { title, kind, model } = target;
|
|
280
|
+
const efforts = isBuiltinKind(kind) ? EFFORTS[kind] : undefined;
|
|
281
|
+
while (true) {
|
|
282
|
+
const env = await target.read();
|
|
283
|
+
const action = await ui.select({
|
|
284
|
+
title: `Settings for ${title}`,
|
|
307
285
|
options: [
|
|
308
|
-
{ label: "
|
|
309
|
-
{ label: "
|
|
310
|
-
{ label: "
|
|
286
|
+
{ label: "Output limit per reply", value: "max", hint: env.JADE_MAX_TOKENS ? `${env.JADE_MAX_TOKENS} tokens` : kind === "anthropic" ? `${ANTHROPIC_DEFAULT_MAX_TOKENS} tokens (default)` : "service default" },
|
|
287
|
+
...(efforts ? [{ label: "Reasoning effort", value: "effort", hint: env.JADE_EFFORT ?? (kind === "anthropic" ? "high (default)" : "service default") }] : []),
|
|
288
|
+
{ label: "Context window", value: "window", hint: `${contextWindow(model, env)} tokens${env.JADE_CONTEXT_WINDOW ? "" : " (default)"}` },
|
|
289
|
+
{ label: "Requests per minute", value: "rpm", hint: env.JADE_RPM ? `${env.JADE_RPM}/min` : "no limit (slows down after being rate limited)" },
|
|
290
|
+
{ label: "Can see images", value: "vision", hint: visionHint(env, kind) },
|
|
291
|
+
...(kind === "openai-compat" ? [{ label: "Extra request body", value: "extra", hint: env.OPENAI_COMPAT_EXTRA_BODY ? shorten(env.OPENAI_COMPAT_EXTRA_BODY) : "none" }] : []),
|
|
292
|
+
{ label: "Back", value: "back" },
|
|
311
293
|
],
|
|
312
294
|
}, opts.signal);
|
|
313
|
-
if (
|
|
295
|
+
if (action === undefined || action === "back")
|
|
314
296
|
return;
|
|
315
|
-
|
|
316
|
-
|
|
297
|
+
// 每一项里按 Esc 或者输入有误:什么都不改,回到这个菜单
|
|
298
|
+
if (action === "max") {
|
|
299
|
+
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);
|
|
300
|
+
if (max !== undefined && tokensInput("Output limit", max))
|
|
301
|
+
await target.update({ JADE_MAX_TOKENS: max || undefined });
|
|
302
|
+
}
|
|
303
|
+
else if (action === "effort" && efforts) {
|
|
304
|
+
const DEFAULT = "\0default";
|
|
305
|
+
const effort = await ui.select({
|
|
306
|
+
title: "Reasoning effort",
|
|
307
|
+
options: [...efforts.map((e) => ({ label: e, value: e, ...(e === env.JADE_EFFORT ? { hint: "current" } : {}) })), { label: "Default", value: DEFAULT, hint: kind === "anthropic" ? "high" : "service default" }],
|
|
308
|
+
}, opts.signal);
|
|
309
|
+
if (effort !== undefined)
|
|
310
|
+
await target.update({ JADE_EFFORT: effort === DEFAULT ? undefined : effort });
|
|
311
|
+
}
|
|
312
|
+
else if (action === "rpm") {
|
|
313
|
+
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);
|
|
314
|
+
if (n === undefined)
|
|
315
|
+
continue;
|
|
316
|
+
try {
|
|
317
|
+
rpmFromEnv({ JADE_RPM: n });
|
|
318
|
+
}
|
|
319
|
+
catch (err) {
|
|
320
|
+
report(errorText(err));
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
await target.update({ JADE_RPM: n.trim() || undefined });
|
|
324
|
+
}
|
|
325
|
+
else if (action === "vision") {
|
|
326
|
+
const DEFAULT = "\0default";
|
|
327
|
+
const current = visionSetting(env);
|
|
328
|
+
const choice = await ui.select({
|
|
329
|
+
title: "Can this model see images (screenshots, image files)?",
|
|
330
|
+
options: [
|
|
331
|
+
{ label: "Yes", value: "on", hint: current === true ? "current" : "images are sent to the model" },
|
|
332
|
+
{ label: "No", value: "off", hint: current === false ? "current" : "images are replaced with a note" },
|
|
333
|
+
{ label: "Default", value: DEFAULT, hint: isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no" },
|
|
334
|
+
],
|
|
335
|
+
}, opts.signal);
|
|
336
|
+
if (choice === undefined)
|
|
337
|
+
continue;
|
|
338
|
+
if (await target.update({ JADE_VISION: choice === DEFAULT ? undefined : choice })) {
|
|
339
|
+
const on = choice === DEFAULT ? isBuiltinKind(kind) && kind !== "openai-compat" : choice === "on";
|
|
340
|
+
report(on ? "Images will be sent to the model." : "Images will be replaced with a note.");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else if (action === "window") {
|
|
344
|
+
const n = await ui.input({ title: "Context window in tokens, e.g. 128000, 512K, 1M (empty to infer it from the model name, or 256K if unknown)", initial: env.JADE_CONTEXT_WINDOW ?? "" }, opts.signal);
|
|
345
|
+
if (n !== undefined && tokensInput("Context window", n))
|
|
346
|
+
await target.update({ JADE_CONTEXT_WINDOW: n || undefined });
|
|
347
|
+
}
|
|
348
|
+
else if (action === "extra" && kind === "openai-compat") {
|
|
349
|
+
const text = await ui.input({ title: 'JSON merged into every request for this model, e.g. {"enable_thinking":true} (empty for none)', initial: env.OPENAI_COMPAT_EXTRA_BODY ?? "" }, opts.signal);
|
|
350
|
+
if (text === undefined)
|
|
351
|
+
continue;
|
|
352
|
+
if (text.trim()) {
|
|
353
|
+
try {
|
|
354
|
+
jsonObject("Extra request body", text);
|
|
355
|
+
}
|
|
356
|
+
catch (err) {
|
|
357
|
+
report(errorText(err));
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
await target.update({ OPENAI_COMPAT_EXTRA_BODY: text.trim() || undefined });
|
|
317
362
|
}
|
|
318
363
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
364
|
+
}
|
|
365
|
+
/** 把模型的设置写进 models.json 里那一个模型;.env 不变(当前用的还是原来的模型)。改回默认值的设置从 settings 里删掉 */
|
|
366
|
+
async function writeSavedModel(service, model, settings) {
|
|
367
|
+
const id = serviceId(service.env);
|
|
368
|
+
const all = await readSavedServices(modelsFile);
|
|
369
|
+
const mine = id === undefined ? undefined : all.find((s) => serviceId(s.env) === id);
|
|
370
|
+
if (!mine) {
|
|
371
|
+
report(`Not saved: ${describeService(service)} is no longer in ${modelsFile}.`);
|
|
372
|
+
return false;
|
|
323
373
|
}
|
|
374
|
+
const mineIndex = all.indexOf(mine);
|
|
375
|
+
const modelIndex = mine.models.findIndex((m) => m.name === model.name);
|
|
376
|
+
// 在文件里现在的设置上改:菜单打开之后 models.json 可能被别的会话改过
|
|
377
|
+
const merged = { ...(mine.models[modelIndex]?.settings ?? model.settings), ...pick(settings, MODEL_SETTINGS), ...blank(settings, MODEL_SETTINGS) };
|
|
378
|
+
const next = { name: model.name, settings: Object.fromEntries(Object.entries(merged).filter(([, v]) => v !== undefined && v !== "")) };
|
|
379
|
+
const models = modelIndex === -1 ? [next, ...mine.models] : [...mine.models.slice(0, modelIndex), next, ...mine.models.slice(modelIndex + 1)];
|
|
380
|
+
await writeSavedServices(modelsFile, [...all.slice(0, mineIndex), { ...mine, models }, ...all.slice(mineIndex + 1)]);
|
|
381
|
+
report(`Settings for ${model.name} (${describeService(mine)}) saved to ${modelsFile}.`);
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
/** 把保存过的模型从 models.json 里删掉;服务下没有模型了就连服务(和它的密钥)一起删掉 */
|
|
385
|
+
async function forgetModel(service, model) {
|
|
386
|
+
const id = serviceId(service.env);
|
|
387
|
+
if (!id)
|
|
388
|
+
return;
|
|
389
|
+
const rest = (await readSavedServices(modelsFile))
|
|
390
|
+
.map((s) => (serviceId(s.env) === id ? { ...s, models: s.models.filter((m) => m.name !== model.name) } : s))
|
|
391
|
+
.filter((s) => s.models.length > 0);
|
|
392
|
+
await writeSavedServices(modelsFile, rest);
|
|
393
|
+
report(`Removed ${model.name} (${describeService(service)}).`);
|
|
324
394
|
}
|
|
325
395
|
/**
|
|
326
|
-
*
|
|
327
|
-
*
|
|
396
|
+
* 选中主菜单里一个模型后:切换、配置它(不用先切到它)、把它删掉或返回主菜单。
|
|
397
|
+
* 当前的模型只能配置(切换没意义;删掉了下次进 /model 又会被记回来)。从设置里出来回到这个菜单;
|
|
398
|
+
* 切换、删除、Back 或 Esc 回到主菜单
|
|
328
399
|
*/
|
|
329
|
-
async function
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
400
|
+
async function pickModelAction(o, isCurrent) {
|
|
401
|
+
const kind = providerKind(o.service.env);
|
|
402
|
+
// 当前的模型改 .env 就好——applyConfig 里的 remember 会把改完的 .env 写回 models.json;
|
|
403
|
+
// 其他保存过的模型只改 models.json 里的那一个
|
|
404
|
+
const read = isCurrent ? async () => pick(opts.env(), MODEL_SETTINGS) : () => savedSettings(o.service, o.model);
|
|
405
|
+
while (true) {
|
|
406
|
+
const settings = await read();
|
|
407
|
+
const action = await ui.select({
|
|
408
|
+
title: o.model.name,
|
|
409
|
+
options: [
|
|
410
|
+
...(isCurrent
|
|
411
|
+
? []
|
|
412
|
+
: [{ label: "Switch to it", value: "switch", hint: serviceId(o.service.env) === serviceId(opts.env()) ? "this service" : describeService(o.service) }]),
|
|
413
|
+
{ label: "Settings…", value: "settings", hint: settingsSummary(settings, kind, contextWindow(o.model.name, settings)) },
|
|
414
|
+
...(isCurrent ? [] : [{ label: "Remove", value: "forget" }]),
|
|
415
|
+
{ label: "Back", value: "back" },
|
|
416
|
+
],
|
|
417
|
+
}, opts.signal);
|
|
418
|
+
if (action === "settings") {
|
|
419
|
+
await configureSettings({
|
|
420
|
+
title: o.model.name,
|
|
421
|
+
kind,
|
|
422
|
+
model: o.model.name,
|
|
423
|
+
read,
|
|
424
|
+
update: (updates) => (isCurrent ? applyConfig(updates) : writeSavedModel(o.service, o.model, updates)),
|
|
425
|
+
});
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
// 用刚读到的设置切换:在这个菜单里改过的也带上
|
|
429
|
+
if (action === "switch")
|
|
430
|
+
await useSaved(o.service, { ...o.model, settings });
|
|
431
|
+
else if (action === "forget")
|
|
432
|
+
await forgetModel(o.service, o.model);
|
|
433
|
+
return;
|
|
337
434
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
return model && { service: group.service, model };
|
|
435
|
+
}
|
|
436
|
+
/** 保存过的模型在 models.json 里现在的设置;找不到时用 fallback 那份 */
|
|
437
|
+
async function savedSettings(service, fallback) {
|
|
438
|
+
const id = serviceId(service.env);
|
|
439
|
+
const mine = id === undefined ? undefined : (await readSavedServices(modelsFile)).find((s) => serviceId(s.env) === id);
|
|
440
|
+
return mine?.models.find((m) => m.name === fallback.name)?.settings ?? fallback.settings;
|
|
345
441
|
}
|
|
346
442
|
async function configureModel(arg) {
|
|
347
443
|
const env = opts.env();
|
|
348
444
|
// 还没有配置模型:没有"当前的服务"可以换模型,先选服务
|
|
349
445
|
if (!hasProvider(env))
|
|
350
446
|
return void (await configureProvider());
|
|
351
|
-
const info = providerInfo(env);
|
|
352
447
|
// 之前只有 .env 时配置的模型也记下来,之后换了不会丢
|
|
353
448
|
await remember().catch(() => { });
|
|
354
|
-
const currentId = serviceId(env);
|
|
355
|
-
const services = await savedServices();
|
|
356
|
-
/** 保存过的、不是当前的模型,按服务分组:当前服务的在前 */
|
|
357
|
-
const others = [...services.filter((s) => serviceId(s.env) === currentId), ...services.filter((s) => serviceId(s.env) !== currentId)].flatMap((s) => s.models.filter((m) => !(serviceId(s.env) === currentId && m.name === info.model)).map((m) => ({ service: s, model: m })));
|
|
358
449
|
if (arg) {
|
|
359
450
|
// 保存过的模型按名字切换(当前服务的优先),否则换当前服务的模型
|
|
360
|
-
const saved =
|
|
451
|
+
const saved = otherModels(await savedServices(), serviceId(env), providerInfo(env).model).find((o) => o.model.name === arg);
|
|
361
452
|
return void (await (saved ? useSaved(saved.service, saved.model) : switchModel(arg)));
|
|
362
453
|
}
|
|
363
|
-
//
|
|
364
|
-
//
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
const o = others[Number(action.slice(6))];
|
|
379
|
-
await useSaved(o.service, o.model);
|
|
380
|
-
}
|
|
381
|
-
else if (action === "add") {
|
|
382
|
-
const where = await ui.select({
|
|
383
|
-
title: "Add a model from",
|
|
454
|
+
// 主菜单循环:保存过的模型全部直接列出(当前的在最前,然后是同一服务的),选中出一个子菜单
|
|
455
|
+
// (切换 / 设置 / 删除 / 返回);配置某个模型、把它删掉都不用先切到它,做完回到主菜单再选。
|
|
456
|
+
// 每一轮都重读当前配置:切换、配置之后模型和它的设置可能都变了
|
|
457
|
+
while (true) {
|
|
458
|
+
const env = opts.env();
|
|
459
|
+
const id = serviceId(env);
|
|
460
|
+
const kind = providerKind(env);
|
|
461
|
+
const kindKnown = isBuiltinKind(kind);
|
|
462
|
+
const info = providerInfo(env);
|
|
463
|
+
const here = describeService({ env: kindKnown ? pick(env, serviceKeys(kind)) : {}, models: [] });
|
|
464
|
+
const services = await savedServices();
|
|
465
|
+
const multi = services.length > 1;
|
|
466
|
+
const others = otherModels(services, id, info.model);
|
|
467
|
+
const action = await ui.select({
|
|
468
|
+
title: "Switch model",
|
|
384
469
|
options: [
|
|
385
|
-
{
|
|
386
|
-
|
|
470
|
+
{
|
|
471
|
+
label: info.model,
|
|
472
|
+
value: "current",
|
|
473
|
+
hint: `${multi ? `${here} · ` : ""}current · ${settingsSummary(env, info.kind, contextWindow(info.model, env))}`,
|
|
474
|
+
},
|
|
475
|
+
...others.map((o, i) => ({
|
|
476
|
+
label: o.model.name,
|
|
477
|
+
value: `saved:${i}`,
|
|
478
|
+
hint: `${multi ? `${describeService(o.service)} · ` : ""}${settingsSummary(o.model.settings, providerKind(o.service.env), contextWindow(o.model.name, o.model.settings))}`,
|
|
479
|
+
})),
|
|
480
|
+
{ label: "Add a model…", value: "add", hint: "from this service or another one" },
|
|
387
481
|
],
|
|
388
482
|
}, opts.signal);
|
|
389
|
-
if (
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
483
|
+
if (action === undefined)
|
|
484
|
+
return; // Esc:整个 /model 结束
|
|
485
|
+
if (action === "current") {
|
|
486
|
+
const service = { env: kindKnown ? { ...pick(env, serviceKeys(kind)), JADE_PROVIDER: kind } : { JADE_PROVIDER: kind }, models: [] };
|
|
487
|
+
await pickModelAction({ service, model: { name: info.model, settings: pick(env, MODEL_SETTINGS) } }, true);
|
|
488
|
+
}
|
|
489
|
+
else if (action.startsWith("saved:")) {
|
|
490
|
+
const o = others[Number(action.slice(6))];
|
|
491
|
+
if (o)
|
|
492
|
+
await pickModelAction(o, false);
|
|
493
|
+
}
|
|
494
|
+
else if (action === "add") {
|
|
495
|
+
const where = await ui.select({
|
|
496
|
+
title: "Add a model from",
|
|
497
|
+
options: [
|
|
498
|
+
{ label: "This service", value: "model", hint: here },
|
|
499
|
+
{ label: "Another service…", value: "provider", hint: "or change this one's base URL / API key" },
|
|
500
|
+
{ label: "Back", value: "back" },
|
|
501
|
+
],
|
|
502
|
+
}, opts.signal);
|
|
503
|
+
// 做完、Back、Esc 或没填完,都回到主菜单
|
|
504
|
+
if (where === "model") {
|
|
505
|
+
const saved = services.find((s) => serviceId(s.env) === id)?.models.map((m) => m.name);
|
|
506
|
+
const model = await chooseModel(env, info.model, saved);
|
|
507
|
+
if (model && model !== info.model)
|
|
508
|
+
await switchModel(model);
|
|
509
|
+
}
|
|
510
|
+
else if (where === "provider")
|
|
511
|
+
await configureProvider();
|
|
394
512
|
}
|
|
395
|
-
else if (where === "provider")
|
|
396
|
-
await configureProvider();
|
|
397
|
-
}
|
|
398
|
-
else if (action === "settings")
|
|
399
|
-
await configureSettings();
|
|
400
|
-
else if (action === "forget") {
|
|
401
|
-
const owners = services.filter((s) => others.some((o) => o.service === s));
|
|
402
|
-
const gone = await pickSaved(owners, "Remove a model from which service?", (s, m) => !(serviceId(s.env) === currentId && m.name === info.model));
|
|
403
|
-
if (!gone)
|
|
404
|
-
return;
|
|
405
|
-
const id = serviceId(gone.service.env);
|
|
406
|
-
// 服务下没有模型了就连服务(和它的密钥)一起删掉;认不出的服务原样保留
|
|
407
|
-
const rest = (await readSavedServices(modelsFile))
|
|
408
|
-
.map((s) => (serviceId(s.env) === id ? { ...s, models: s.models.filter((m) => m.name !== gone.model.name) } : s))
|
|
409
|
-
.filter((s) => s.models.length > 0);
|
|
410
|
-
await writeSavedServices(modelsFile, rest);
|
|
411
|
-
report(`Removed ${gone.model.name} (${describeService(gone.service)}).`);
|
|
412
513
|
}
|
|
413
514
|
}
|
|
414
515
|
return { provider: configureProvider, model: configureModel };
|