@uncarved/jade 0.3.0 → 0.4.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 +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +49 -24
- package/plugins/approval/policy.ts +48 -25
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +55 -12
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +10 -9
- package/plugins/models/index.ts +31 -4
- package/plugins/models/provider.ts +12 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +39 -9
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +231 -0
- package/plugins/screenshot/index.ts +3 -2
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +75 -22
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +93 -34
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +81 -81
- package/plugins/task/plugin.json +5 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +57 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/frontend.ts +161 -87
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mascot.ts +8 -7
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +356 -104
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -414
- package/plugins-dist/approval/policy.js +395 -497
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -244
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -188
- package/plugins-dist/models/provider.js +243 -295
- package/plugins-dist/models/saved.js +59 -71
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +81 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +134 -151
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +169 -0
- package/plugins-dist/screenshot/index.js +37 -45
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +101 -151
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -74
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +317 -320
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -148
- package/plugins-dist/task/plugin.json +5 -4
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +43 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +641 -655
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -353
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1645
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +489 -34
|
@@ -1,516 +1,435 @@
|
|
|
1
|
-
// /model:选服务、填地址和密钥、选模型、改输出上限、思考力度、上下文窗口、限速和能不能看图。
|
|
2
|
-
// 配置写进 $JADE_HOME/.env;用过的服务和模型记在 $JADE_HOME/models.json 里(saved.ts),可以直接切回去。
|
|
3
|
-
//
|
|
4
|
-
// 从旧 jade 的 ui/configure.ts 移植。省掉了旧 jade 的"插件提供的模型服务"(新的 jade 还没有这种服务),
|
|
5
|
-
// 其余流程一样。
|
|
6
1
|
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
7
2
|
import { hasProvider, parseTokens } from "./config.js";
|
|
8
3
|
import { contextWindow } from "./context.js";
|
|
9
4
|
import { EFFORTS, isBuiltinKind, jsonObject, KEY_KEY, listModels, MODEL_KEY, providerInfo, providerKind, rpmFromEnv, URL_KEY, visionFromEnv } from "./provider.js";
|
|
10
5
|
import { MODEL_SETTINGS, readSavedServices, updateEnvFile, writeSavedServices } from "./saved.js";
|
|
11
6
|
const PROVIDERS = [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
{ kind: "openai-compat", label: "OpenAI-compatible service", hint: "any /chat/completions endpoint" },
|
|
8
|
+
{ kind: "openai", label: "OpenAI", hint: "Responses API" },
|
|
9
|
+
{ kind: "anthropic", label: "Anthropic", hint: "Claude" }
|
|
15
10
|
];
|
|
16
|
-
/** JADE_VISION 设成了什么;没设或写错时 undefined */
|
|
17
11
|
function visionSetting(env) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
12
|
+
if (!env.JADE_VISION?.trim())
|
|
13
|
+
return;
|
|
14
|
+
try {
|
|
15
|
+
return visionFromEnv({ JADE_VISION: env.JADE_VISION });
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
26
19
|
}
|
|
27
20
|
function visionHint(env, kind) {
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
const v = visionSetting(env);
|
|
22
|
+
return v === undefined ? `${isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no"} (default)` : v ? "yes" : "no";
|
|
30
23
|
}
|
|
31
|
-
/** 一个服务包含的变量(不含模型名) */
|
|
32
24
|
const serviceKeys = (kind) => ["JADE_PROVIDER", URL_KEY[kind], KEY_KEY[kind]];
|
|
33
25
|
const pick = (env, keys) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
const out = {};
|
|
27
|
+
for (const k of keys)
|
|
28
|
+
if (env[k] !== undefined)
|
|
29
|
+
out[k] = env[k];
|
|
30
|
+
return out;
|
|
39
31
|
};
|
|
40
|
-
/** keys 里明确清掉(设成默认)的那些:空字符串表示"不设",写进 models.json 时删掉;env 里没提到的不算 */
|
|
41
32
|
const blank = (env, keys) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
const out = {};
|
|
34
|
+
for (const k of keys)
|
|
35
|
+
if (k in env && env[k] === undefined)
|
|
36
|
+
out[k] = "";
|
|
37
|
+
return out;
|
|
47
38
|
};
|
|
48
|
-
/** 服务的类型、地址和密钥都相同的算同一个;配置无效时返回 undefined */
|
|
49
39
|
function serviceId(env) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
40
|
+
try {
|
|
41
|
+
const i = providerInfo(env);
|
|
42
|
+
if (!isBuiltinKind(i.kind))
|
|
43
|
+
return;
|
|
44
|
+
return `${i.kind}|${i.baseURL ?? ""}|${env[KEY_KEY[i.kind]] ?? ""}`;
|
|
45
|
+
} catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
59
48
|
}
|
|
60
|
-
/** 选择列表里服务的说明:类型和地址的主机名 */
|
|
61
49
|
function describeService(service) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return [i.kind, host].filter(Boolean).join(" · ");
|
|
50
|
+
const i = providerInfo(service.env);
|
|
51
|
+
let host = i.baseURL ?? "";
|
|
52
|
+
try {
|
|
53
|
+
host = new URL(host).host;
|
|
54
|
+
} catch {}
|
|
55
|
+
return [i.kind, host].filter(Boolean).join(" · ");
|
|
69
56
|
}
|
|
70
|
-
/** 保存过的、不是当前的模型,按服务分组:当前服务的在前 */
|
|
71
57
|
function otherModels(services, currentId, currentModel) {
|
|
72
|
-
|
|
58
|
+
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 })));
|
|
73
59
|
}
|
|
74
|
-
/** Claude 不设输出上限时用的值:和 @uncarved/provider-anthropic 里的默认值一致(不从那里导入:加载它的 SDK 要一百多毫秒) */
|
|
75
60
|
const ANTHROPIC_DEFAULT_MAX_TOKENS = 64000;
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const errorText = (err) => (err instanceof Error ? err.message : String(err));
|
|
79
|
-
/** 模型设置那一项的简要说明,例如 "64K out · 1M ctx · images";settings 是这个模型自己的设置变量 */
|
|
61
|
+
const shorten = (text, max = 40) => text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
62
|
+
const errorText = (err) => err instanceof Error ? err.message : String(err);
|
|
80
63
|
function settingsSummary(settings, kind, window) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.filter(Boolean)
|
|
90
|
-
.join(" · ");
|
|
64
|
+
const images = visionSetting({ JADE_VISION: settings.JADE_VISION }) ?? (isBuiltinKind(kind) && kind !== "openai-compat");
|
|
65
|
+
return [
|
|
66
|
+
settings.JADE_MAX_TOKENS && `${settings.JADE_MAX_TOKENS} out`,
|
|
67
|
+
settings.JADE_EFFORT && `${settings.JADE_EFFORT} effort`,
|
|
68
|
+
`${settings.JADE_CONTEXT_WINDOW ?? window} ctx`,
|
|
69
|
+
settings.JADE_RPM && `${settings.JADE_RPM}/min`,
|
|
70
|
+
images ? "images" : "no images"
|
|
71
|
+
].filter(Boolean).join(" · ");
|
|
91
72
|
}
|
|
92
73
|
export function configurator(opts) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
74
|
+
const { ui, envFile, modelsFile } = opts;
|
|
75
|
+
const report = (text) => opts.report(text);
|
|
76
|
+
const savedServices = async () => (await readSavedServices(modelsFile)).filter((s) => serviceId(s.env) !== undefined);
|
|
77
|
+
const tokensInput = (name, text) => {
|
|
78
|
+
if (!text)
|
|
79
|
+
return true;
|
|
80
|
+
try {
|
|
81
|
+
parseTokens(name, text);
|
|
82
|
+
return true;
|
|
83
|
+
} catch (err) {
|
|
84
|
+
report(errorText(err));
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
async function remember(replacing) {
|
|
89
|
+
const env = opts.env();
|
|
90
|
+
if (!hasProvider(env))
|
|
91
|
+
return;
|
|
92
|
+
const id = serviceId(env);
|
|
93
|
+
const model = providerInfo(env).model;
|
|
94
|
+
const kind = providerKind(env);
|
|
95
|
+
if (!id || !model || !isBuiltinKind(kind))
|
|
96
|
+
return;
|
|
97
|
+
const all = await readSavedServices(modelsFile);
|
|
98
|
+
const old = all.filter((s) => {
|
|
99
|
+
const sid = serviceId(s.env);
|
|
100
|
+
return sid !== undefined && (sid === id || replacing !== undefined && sid === replacing);
|
|
101
|
+
});
|
|
102
|
+
const models = old.flatMap((s) => s.models).filter((m, i, list) => m.name !== model && list.findIndex((x) => x.name === m.name) === i);
|
|
103
|
+
const service = {
|
|
104
|
+
env: { ...pick(env, serviceKeys(kind)), JADE_PROVIDER: kind },
|
|
105
|
+
models: [{ name: model, settings: pick(env, MODEL_SETTINGS) }, ...models]
|
|
109
106
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// 写回时保留现在认不出的服务,不然它和它的密钥就丢了
|
|
124
|
-
const all = await readSavedServices(modelsFile);
|
|
125
|
-
const old = all.filter((s) => {
|
|
126
|
-
const sid = serviceId(s.env);
|
|
127
|
-
return sid !== undefined && (sid === id || (replacing !== undefined && sid === replacing));
|
|
128
|
-
});
|
|
129
|
-
const models = old.flatMap((s) => s.models).filter((m, i, list) => m.name !== model && list.findIndex((x) => x.name === m.name) === i);
|
|
130
|
-
const service = {
|
|
131
|
-
env: { ...pick(env, serviceKeys(kind)), JADE_PROVIDER: kind },
|
|
132
|
-
models: [{ name: model, settings: pick(env, MODEL_SETTINGS) }, ...models],
|
|
133
|
-
};
|
|
134
|
-
await writeSavedServices(modelsFile, [service, ...all.filter((s) => !old.includes(s))]);
|
|
107
|
+
await writeSavedServices(modelsFile, [service, ...all.filter((s) => !old.includes(s))]);
|
|
108
|
+
}
|
|
109
|
+
async function applyConfig(updates, replacing) {
|
|
110
|
+
const before = await readFile(envFile, "utf8").catch(() => {
|
|
111
|
+
return;
|
|
112
|
+
});
|
|
113
|
+
await updateEnvFile(envFile, updates, { ...opts.env() });
|
|
114
|
+
try {
|
|
115
|
+
await opts.apply();
|
|
116
|
+
} catch (err) {
|
|
117
|
+
await (before === undefined ? rm(envFile, { force: true }) : writeFile(envFile, before)).catch(() => {});
|
|
118
|
+
report(`Not saved: the configuration is incomplete: ${errorText(err)}`);
|
|
119
|
+
return false;
|
|
135
120
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
await (before === undefined ? rm(envFile, { force: true }) : writeFile(envFile, before)).catch(() => { });
|
|
147
|
-
report(`Not saved: the configuration is incomplete: ${errorText(err)}`);
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
const info = providerInfo(opts.env());
|
|
151
|
-
await remember(replacing).catch((err) => report(`Could not save the model list to ${modelsFile}: ${errorText(err)}`));
|
|
152
|
-
report(`Model: ${info.model} (${info.kind}${info.baseURL ? `, ${info.baseURL}` : ""}). Saved to ${envFile}.`);
|
|
153
|
-
return true;
|
|
121
|
+
const info = providerInfo(opts.env());
|
|
122
|
+
await remember(replacing).catch((err) => report(`Could not save the model list to ${modelsFile}: ${errorText(err)}`));
|
|
123
|
+
report(`Model: ${info.model} (${info.kind}${info.baseURL ? `, ${info.baseURL}` : ""}). Saved to ${envFile}.`);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
function useSaved(s, m) {
|
|
127
|
+
const kind = providerKind(s.env);
|
|
128
|
+
if (!isBuiltinKind(kind)) {
|
|
129
|
+
report(`The saved service ${describeService(s)} is not one of the built-in providers.`);
|
|
130
|
+
return Promise.resolve(false);
|
|
154
131
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
132
|
+
const updates = {};
|
|
133
|
+
for (const k of serviceKeys(kind))
|
|
134
|
+
updates[k] = s.env[k];
|
|
135
|
+
for (const k of MODEL_SETTINGS)
|
|
136
|
+
updates[k] = m.settings[k];
|
|
137
|
+
updates.JADE_PROVIDER = kind;
|
|
138
|
+
updates[MODEL_KEY[kind]] = m.name;
|
|
139
|
+
return applyConfig(updates);
|
|
140
|
+
}
|
|
141
|
+
async function switchModel(model) {
|
|
142
|
+
const env = opts.env();
|
|
143
|
+
const id = serviceId(env);
|
|
144
|
+
const service = (await savedServices()).find((s) => serviceId(s.env) === id);
|
|
145
|
+
const saved = service?.models.find((m) => m.name === model);
|
|
146
|
+
if (service && saved)
|
|
147
|
+
return useSaved(service, saved);
|
|
148
|
+
const kind = providerKind(env);
|
|
149
|
+
if (!isBuiltinKind(kind)) {
|
|
150
|
+
report(`JADE_PROVIDER must be one of ${PROVIDERS.map((p) => p.kind).join(", ")}; set the model in ${envFile}.`);
|
|
151
|
+
return false;
|
|
170
152
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
return applyConfig({ [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined });
|
|
153
|
+
return applyConfig({ [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined });
|
|
154
|
+
}
|
|
155
|
+
async function chooseModel(env, current, saved = []) {
|
|
156
|
+
const models = await listModels(env, opts.fetch);
|
|
157
|
+
const BACK = "\x00back";
|
|
158
|
+
if (models.length > 0) {
|
|
159
|
+
const OTHER = "\x00other";
|
|
160
|
+
const picked = await ui.select({
|
|
161
|
+
title: "Choose a model",
|
|
162
|
+
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 }]
|
|
163
|
+
}, opts.signal);
|
|
164
|
+
if (picked === BACK)
|
|
165
|
+
return;
|
|
166
|
+
if (picked !== OTHER)
|
|
167
|
+
return picked;
|
|
188
168
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
169
|
+
return await ui.input({ title: "Model name", ...current ? { initial: current } : {} }, opts.signal) || undefined;
|
|
170
|
+
}
|
|
171
|
+
async function configureProvider() {
|
|
172
|
+
const env = opts.env();
|
|
173
|
+
const current = hasProvider(env) ? providerInfo(env) : undefined;
|
|
174
|
+
const currentId = hasProvider(env) ? serviceId(env) : undefined;
|
|
175
|
+
const kindName = await ui.select({
|
|
176
|
+
title: "Which service should jade use?",
|
|
177
|
+
options: PROVIDERS.map((p) => ({ label: p.label, value: p.kind, hint: p.kind === current?.kind ? `${p.hint} · current` : p.hint }))
|
|
178
|
+
}, opts.signal);
|
|
179
|
+
if (!kindName || !isBuiltinKind(kindName))
|
|
180
|
+
return false;
|
|
181
|
+
const kind = kindName;
|
|
182
|
+
const same = kind === current?.kind;
|
|
183
|
+
let baseURL = env[URL_KEY[kind]];
|
|
184
|
+
const fallback = kind === "openai" ? "https://api.openai.com/v1" : "";
|
|
185
|
+
const placeholder = kind === "anthropic" ? "empty for https://api.anthropic.com (SDK adds /v1)" : "https://example.com/v1";
|
|
186
|
+
const url = await ui.input({ title: "Base URL", initial: baseURL ?? fallback, placeholder }, opts.signal);
|
|
187
|
+
if (url === undefined)
|
|
188
|
+
return false;
|
|
189
|
+
if (kind === "openai-compat" && !url) {
|
|
190
|
+
report("A base URL is required.");
|
|
191
|
+
return false;
|
|
205
192
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
193
|
+
baseURL = url || undefined;
|
|
194
|
+
const sameAddress = (e) => {
|
|
195
|
+
const i = providerInfo(e);
|
|
196
|
+
return i.kind === kind && (i.baseURL ?? "") === (providerInfo({ JADE_PROVIDER: kind, [URL_KEY[kind]]: baseURL }).baseURL ?? "");
|
|
197
|
+
};
|
|
198
|
+
const services = await savedServices();
|
|
199
|
+
const known = current && sameAddress(env) ? env[KEY_KEY[kind]] : services.find((s) => sameAddress(s.env))?.env[KEY_KEY[kind]];
|
|
200
|
+
const answer = await ui.input({
|
|
201
|
+
title: "API key",
|
|
202
|
+
mask: true,
|
|
203
|
+
placeholder: known ? "leave empty to keep the saved key" : kind === "openai-compat" ? "leave empty if the service needs none" : ""
|
|
204
|
+
}, opts.signal);
|
|
205
|
+
if (answer === undefined)
|
|
206
|
+
return false;
|
|
207
|
+
const key = answer || known;
|
|
208
|
+
const serviceEnv = { JADE_PROVIDER: kind, [URL_KEY[kind]]: baseURL, [KEY_KEY[kind]]: key };
|
|
209
|
+
const target = services.find((s) => serviceId(s.env) === serviceId(serviceEnv));
|
|
210
|
+
const model = await chooseModel({ ...env, ...serviceEnv }, same ? current?.model : undefined, target?.models.map((m) => m.name));
|
|
211
|
+
if (!model)
|
|
212
|
+
return false;
|
|
213
|
+
const saved = target?.models.find((m) => m.name === model);
|
|
214
|
+
if (target && saved)
|
|
215
|
+
return useSaved({ ...target, env: { ...target.env, ...pick(serviceEnv, serviceKeys(kind)) } }, saved);
|
|
216
|
+
const updates = { ...serviceEnv, [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined };
|
|
217
|
+
const sameService = same && sameAddress(env);
|
|
218
|
+
if (!sameService)
|
|
219
|
+
for (const k of MODEL_SETTINGS)
|
|
220
|
+
updates[k] = undefined;
|
|
221
|
+
if (kind !== "anthropic" && !(sameService && env.JADE_MAX_TOKENS)) {
|
|
222
|
+
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);
|
|
223
|
+
if (max === undefined || !tokensInput("Output limit", max))
|
|
224
|
+
return false;
|
|
225
|
+
if (max)
|
|
226
|
+
updates.JADE_MAX_TOKENS = max;
|
|
227
|
+
}
|
|
228
|
+
const rekeyed = sameService && currentId !== undefined && key !== env[KEY_KEY[kind]];
|
|
229
|
+
return applyConfig(updates, rekeyed ? currentId : undefined);
|
|
230
|
+
}
|
|
231
|
+
async function configureSettings(target) {
|
|
232
|
+
const { title, kind, model } = target;
|
|
233
|
+
const efforts = isBuiltinKind(kind) ? EFFORTS[kind] : undefined;
|
|
234
|
+
while (true) {
|
|
235
|
+
const env = await target.read();
|
|
236
|
+
const action = await ui.select({
|
|
237
|
+
title: `Settings for ${title}`,
|
|
238
|
+
options: [
|
|
239
|
+
{ 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" },
|
|
240
|
+
...efforts ? [{ label: "Reasoning effort", value: "effort", hint: env.JADE_EFFORT ?? (kind === "anthropic" ? "high (default)" : "service default") }] : [],
|
|
241
|
+
{ label: "Context window", value: "window", hint: `${contextWindow(model, env)} tokens${env.JADE_CONTEXT_WINDOW ? "" : " (default)"}` },
|
|
242
|
+
{ label: "Requests per minute", value: "rpm", hint: env.JADE_RPM ? `${env.JADE_RPM}/min` : "no limit (slows down after being rate limited)" },
|
|
243
|
+
{ label: "Can see images", value: "vision", hint: visionHint(env, kind) },
|
|
244
|
+
...kind === "openai-compat" ? [{ label: "Extra request body", value: "extra", hint: env.OPENAI_COMPAT_EXTRA_BODY ? shorten(env.OPENAI_COMPAT_EXTRA_BODY) : "none" }] : [],
|
|
245
|
+
{ label: "Back", value: "back" }
|
|
246
|
+
]
|
|
247
|
+
}, opts.signal);
|
|
248
|
+
if (action === undefined || action === "back")
|
|
249
|
+
return;
|
|
250
|
+
if (action === "max") {
|
|
251
|
+
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);
|
|
252
|
+
if (max !== undefined && tokensInput("Output limit", max))
|
|
253
|
+
await target.update({ JADE_MAX_TOKENS: max || undefined });
|
|
254
|
+
} else if (action === "effort" && efforts) {
|
|
255
|
+
const DEFAULT = "\x00default";
|
|
256
|
+
const effort = await ui.select({
|
|
257
|
+
title: "Reasoning effort",
|
|
258
|
+
options: [...efforts.map((e) => ({ label: e, value: e, ...e === env.JADE_EFFORT ? { hint: "current" } : {} })), { label: "Default", value: DEFAULT, hint: kind === "anthropic" ? "high" : "service default" }]
|
|
213
259
|
}, opts.signal);
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
report("A base URL is required.");
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
baseURL = answer || undefined;
|
|
260
|
+
if (effort !== undefined)
|
|
261
|
+
await target.update({ JADE_EFFORT: effort === DEFAULT ? undefined : effort });
|
|
262
|
+
} else if (action === "rpm") {
|
|
263
|
+
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);
|
|
264
|
+
if (n === undefined)
|
|
265
|
+
continue;
|
|
266
|
+
try {
|
|
267
|
+
rpmFromEnv({ JADE_RPM: n });
|
|
268
|
+
} catch (err) {
|
|
269
|
+
report(errorText(err));
|
|
270
|
+
continue;
|
|
229
271
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
272
|
+
await target.update({ JADE_RPM: n.trim() || undefined });
|
|
273
|
+
} else if (action === "vision") {
|
|
274
|
+
const DEFAULT = "\x00default";
|
|
275
|
+
const current = visionSetting(env);
|
|
276
|
+
const choice = await ui.select({
|
|
277
|
+
title: "Can this model see images (screenshots, image files)?",
|
|
278
|
+
options: [
|
|
279
|
+
{ label: "Yes", value: "on", hint: current === true ? "current" : "images are sent to the model" },
|
|
280
|
+
{ label: "No", value: "off", hint: current === false ? "current" : "images are replaced with a note" },
|
|
281
|
+
{ label: "Default", value: DEFAULT, hint: isBuiltinKind(kind) && kind !== "openai-compat" ? "yes" : "no" }
|
|
282
|
+
]
|
|
241
283
|
}, opts.signal);
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const model = await chooseModel({ ...env, ...serviceEnv }, same ? current?.model : undefined, target?.models.map((m) => m.name));
|
|
248
|
-
if (!model)
|
|
249
|
-
return false;
|
|
250
|
-
// 用过的模型带上它自己的设置
|
|
251
|
-
const saved = target?.models.find((m) => m.name === model);
|
|
252
|
-
if (target && saved)
|
|
253
|
-
return useSaved({ ...target, env: { ...target.env, ...pick(serviceEnv, serviceKeys(kind)) } }, saved);
|
|
254
|
-
const updates = { ...serviceEnv, [MODEL_KEY[kind]]: model, JADE_CONTEXT_WINDOW: undefined };
|
|
255
|
-
// 换了服务的新模型从新服务的默认值开始:思考力度、额外请求参数各服务写法不同,限速、输出上限、
|
|
256
|
-
// 能不能看图也是跟着服务和模型走的,沿用当前的容易错(例如免费服务的限速带到付费服务上)。
|
|
257
|
-
// 同一个服务(只换了密钥)照旧沿用
|
|
258
|
-
const sameService = same && sameAddress(env);
|
|
259
|
-
if (!sameService)
|
|
260
|
-
for (const k of MODEL_SETTINGS)
|
|
261
|
-
updates[k] = undefined;
|
|
262
|
-
// 其他服务常常默认只给 4096 个输出 token,带思考的模型很容易用完
|
|
263
|
-
if (kind !== "anthropic" && !(sameService && env.JADE_MAX_TOKENS)) {
|
|
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);
|
|
265
|
-
if (max === undefined || !tokensInput("Output limit", max))
|
|
266
|
-
return false;
|
|
267
|
-
if (max)
|
|
268
|
-
updates.JADE_MAX_TOKENS = max;
|
|
284
|
+
if (choice === undefined)
|
|
285
|
+
continue;
|
|
286
|
+
if (await target.update({ JADE_VISION: choice === DEFAULT ? undefined : choice })) {
|
|
287
|
+
const on = choice === DEFAULT ? isBuiltinKind(kind) && kind !== "openai-compat" : choice === "on";
|
|
288
|
+
report(on ? "Images will be sent to the model." : "Images will be replaced with a note.");
|
|
269
289
|
}
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
options: [
|
|
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" },
|
|
293
|
-
],
|
|
294
|
-
}, opts.signal);
|
|
295
|
-
if (action === undefined || action === "back")
|
|
296
|
-
return;
|
|
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 });
|
|
362
|
-
}
|
|
363
|
-
}
|
|
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;
|
|
290
|
+
} else if (action === "window") {
|
|
291
|
+
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);
|
|
292
|
+
if (n !== undefined && tokensInput("Context window", n))
|
|
293
|
+
await target.update({ JADE_CONTEXT_WINDOW: n || undefined });
|
|
294
|
+
} else if (action === "extra" && kind === "openai-compat") {
|
|
295
|
+
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);
|
|
296
|
+
if (text === undefined)
|
|
297
|
+
continue;
|
|
298
|
+
if (text.trim()) {
|
|
299
|
+
try {
|
|
300
|
+
jsonObject("Extra request body", text);
|
|
301
|
+
} catch (err) {
|
|
302
|
+
report(errorText(err));
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
373
305
|
}
|
|
374
|
-
|
|
375
|
-
|
|
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;
|
|
306
|
+
await target.update({ OPENAI_COMPAT_EXTRA_BODY: text.trim() || undefined });
|
|
307
|
+
}
|
|
383
308
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
await writeSavedServices(modelsFile, rest);
|
|
393
|
-
report(`Removed ${model.name} (${describeService(service)}).`);
|
|
309
|
+
}
|
|
310
|
+
async function writeSavedModel(service, model, settings) {
|
|
311
|
+
const id = serviceId(service.env);
|
|
312
|
+
const all = await readSavedServices(modelsFile);
|
|
313
|
+
const mine = id === undefined ? undefined : all.find((s) => serviceId(s.env) === id);
|
|
314
|
+
if (!mine) {
|
|
315
|
+
report(`Not saved: ${describeService(service)} is no longer in ${modelsFile}.`);
|
|
316
|
+
return false;
|
|
394
317
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
318
|
+
const mineIndex = all.indexOf(mine);
|
|
319
|
+
const modelIndex = mine.models.findIndex((m) => m.name === model.name);
|
|
320
|
+
const merged = { ...mine.models[modelIndex]?.settings ?? model.settings, ...pick(settings, MODEL_SETTINGS), ...blank(settings, MODEL_SETTINGS) };
|
|
321
|
+
const next = { name: model.name, settings: Object.fromEntries(Object.entries(merged).filter(([, v]) => v !== undefined && v !== "")) };
|
|
322
|
+
const models = modelIndex === -1 ? [next, ...mine.models] : [...mine.models.slice(0, modelIndex), next, ...mine.models.slice(modelIndex + 1)];
|
|
323
|
+
await writeSavedServices(modelsFile, [...all.slice(0, mineIndex), { ...mine, models }, ...all.slice(mineIndex + 1)]);
|
|
324
|
+
report(`Settings for ${model.name} (${describeService(mine)}) saved to ${modelsFile}.`);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
async function forgetModel(service, model) {
|
|
328
|
+
const id = serviceId(service.env);
|
|
329
|
+
if (!id)
|
|
330
|
+
return;
|
|
331
|
+
const rest = (await readSavedServices(modelsFile)).map((s) => serviceId(s.env) === id ? { ...s, models: s.models.filter((m) => m.name !== model.name) } : s).filter((s) => s.models.length > 0);
|
|
332
|
+
await writeSavedServices(modelsFile, rest);
|
|
333
|
+
report(`Removed ${model.name} (${describeService(service)}).`);
|
|
334
|
+
}
|
|
335
|
+
async function pickModelAction(o, isCurrent) {
|
|
336
|
+
const kind = providerKind(o.service.env);
|
|
337
|
+
const read = isCurrent ? async () => pick(opts.env(), MODEL_SETTINGS) : () => savedSettings(o.service, o.model);
|
|
338
|
+
while (true) {
|
|
339
|
+
const settings = await read();
|
|
340
|
+
const action = await ui.select({
|
|
341
|
+
title: o.model.name,
|
|
342
|
+
options: [
|
|
343
|
+
...isCurrent ? [] : [{ label: "Switch to it", value: "switch", hint: serviceId(o.service.env) === serviceId(opts.env()) ? "this service" : describeService(o.service) }],
|
|
344
|
+
{ label: "Settings…", value: "settings", hint: settingsSummary(settings, kind, contextWindow(o.model.name, settings)) },
|
|
345
|
+
...isCurrent ? [] : [{ label: "Remove", value: "forget" }],
|
|
346
|
+
{ label: "Back", value: "back" }
|
|
347
|
+
]
|
|
348
|
+
}, opts.signal);
|
|
349
|
+
if (action === "settings") {
|
|
350
|
+
await configureSettings({
|
|
351
|
+
title: o.model.name,
|
|
352
|
+
kind,
|
|
353
|
+
model: o.model.name,
|
|
354
|
+
read,
|
|
355
|
+
update: (updates) => isCurrent ? applyConfig(updates) : writeSavedModel(o.service, o.model, updates)
|
|
356
|
+
});
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (action === "switch")
|
|
360
|
+
await useSaved(o.service, { ...o.model, settings });
|
|
361
|
+
else if (action === "forget")
|
|
362
|
+
await forgetModel(o.service, o.model);
|
|
363
|
+
return;
|
|
435
364
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
365
|
+
}
|
|
366
|
+
async function savedSettings(service, fallback) {
|
|
367
|
+
const id = serviceId(service.env);
|
|
368
|
+
const mine = id === undefined ? undefined : (await readSavedServices(modelsFile)).find((s) => serviceId(s.env) === id);
|
|
369
|
+
return mine?.models.find((m) => m.name === fallback.name)?.settings ?? fallback.settings;
|
|
370
|
+
}
|
|
371
|
+
async function configureModel(arg) {
|
|
372
|
+
const env = opts.env();
|
|
373
|
+
if (!hasProvider(env))
|
|
374
|
+
return void await configureProvider();
|
|
375
|
+
await remember().catch(() => {});
|
|
376
|
+
if (arg) {
|
|
377
|
+
const saved = otherModels(await savedServices(), serviceId(env), providerInfo(env).model).find((o) => o.model.name === arg);
|
|
378
|
+
return void await (saved ? useSaved(saved.service, saved.model) : switchModel(arg));
|
|
441
379
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
},
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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();
|
|
512
|
-
}
|
|
513
|
-
}
|
|
380
|
+
while (true) {
|
|
381
|
+
const env = opts.env();
|
|
382
|
+
const id = serviceId(env);
|
|
383
|
+
const kind = providerKind(env);
|
|
384
|
+
const kindKnown = isBuiltinKind(kind);
|
|
385
|
+
const info = providerInfo(env);
|
|
386
|
+
const here = describeService({ env: kindKnown ? pick(env, serviceKeys(kind)) : {}, models: [] });
|
|
387
|
+
const services = await savedServices();
|
|
388
|
+
const multi = services.length > 1;
|
|
389
|
+
const others = otherModels(services, id, info.model);
|
|
390
|
+
const action = await ui.select({
|
|
391
|
+
title: "Switch model",
|
|
392
|
+
options: [
|
|
393
|
+
{
|
|
394
|
+
label: info.model,
|
|
395
|
+
value: "current",
|
|
396
|
+
hint: `${multi ? `${here} · ` : ""}current · ${settingsSummary(env, info.kind, contextWindow(info.model, env))}`
|
|
397
|
+
},
|
|
398
|
+
...others.map((o, i) => ({
|
|
399
|
+
label: o.model.name,
|
|
400
|
+
value: `saved:${i}`,
|
|
401
|
+
hint: `${multi ? `${describeService(o.service)} · ` : ""}${settingsSummary(o.model.settings, providerKind(o.service.env), contextWindow(o.model.name, o.model.settings))}`
|
|
402
|
+
})),
|
|
403
|
+
{ label: "Add a model…", value: "add", hint: "from this service or another one" }
|
|
404
|
+
]
|
|
405
|
+
}, opts.signal);
|
|
406
|
+
if (action === undefined)
|
|
407
|
+
return;
|
|
408
|
+
if (action === "current") {
|
|
409
|
+
const service = { env: kindKnown ? { ...pick(env, serviceKeys(kind)), JADE_PROVIDER: kind } : { JADE_PROVIDER: kind }, models: [] };
|
|
410
|
+
await pickModelAction({ service, model: { name: info.model, settings: pick(env, MODEL_SETTINGS) } }, true);
|
|
411
|
+
} else if (action.startsWith("saved:")) {
|
|
412
|
+
const o = others[Number(action.slice(6))];
|
|
413
|
+
if (o)
|
|
414
|
+
await pickModelAction(o, false);
|
|
415
|
+
} else if (action === "add") {
|
|
416
|
+
const where = await ui.select({
|
|
417
|
+
title: "Add a model from",
|
|
418
|
+
options: [
|
|
419
|
+
{ label: "This service", value: "model", hint: here },
|
|
420
|
+
{ label: "Another service…", value: "provider", hint: "or change this one's base URL / API key" },
|
|
421
|
+
{ label: "Back", value: "back" }
|
|
422
|
+
]
|
|
423
|
+
}, opts.signal);
|
|
424
|
+
if (where === "model") {
|
|
425
|
+
const saved = services.find((s) => serviceId(s.env) === id)?.models.map((m) => m.name);
|
|
426
|
+
const model = await chooseModel(env, info.model, saved);
|
|
427
|
+
if (model && model !== info.model)
|
|
428
|
+
await switchModel(model);
|
|
429
|
+
} else if (where === "provider")
|
|
430
|
+
await configureProvider();
|
|
431
|
+
}
|
|
514
432
|
}
|
|
515
|
-
|
|
433
|
+
}
|
|
434
|
+
return { provider: configureProvider, model: configureModel };
|
|
516
435
|
}
|