@uncarved/jade 0.2.1 → 0.3.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 +65 -2
- package/assets/logo.svg +1 -0
- package/bin/jade.js +4 -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 +26 -4
- 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 +4 -2
- package/plugins/approval/index.ts +236 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +36 -17
- 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 +8 -3
- 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 +33 -6
- package/plugins/fs/grep.ts +19 -7
- package/plugins/fs/index.ts +6 -7
- package/plugins/fs/plugin.json +1 -1
- 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 +43 -15
- 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 +2 -2
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +39 -45
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +34 -5
- 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 +36 -6
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +22 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +5 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +32 -2
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/browser.ts +221 -0
- package/plugins/screenshot/index.ts +29 -15
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +115 -7
- 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 +119 -47
- 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 +18 -57
- package/plugins/task/plugin.json +9 -3
- package/plugins/task/subagent.ts +25 -7
- package/plugins/todo/index.ts +2 -2
- package/plugins/tools/index.ts +50 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +129 -23
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +506 -0
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +17 -2
- package/plugins/tui/tui.ts +344 -97
- package/plugins/undo/checkpoint.ts +2 -2
- package/plugins/undo/index.ts +84 -26
- 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 +8 -11
- package/plugins/web/plugin.json +1 -1
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +221 -51
- package/plugins-dist/approval/plugin.json +1 -1
- package/plugins-dist/approval/policy.js +30 -15
- 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 +6 -1
- 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 +34 -6
- package/plugins-dist/fs/grep.js +17 -6
- package/plugins-dist/fs/index.js +6 -6
- package/plugins-dist/fs/plugin.json +1 -1
- 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 +45 -13
- 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 +1 -1
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +41 -17
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +17 -3
- 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 +37 -3
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +22 -3
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +5 -4
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +28 -2
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/browser.js +187 -0
- package/plugins-dist/screenshot/index.js +28 -13
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/screenshot/tool.js +27 -49
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +119 -4
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +106 -36
- 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 +17 -46
- package/plugins-dist/task/plugin.json +11 -5
- package/plugins-dist/task/subagent.js +21 -6
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tools/index.js +49 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +134 -15
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +441 -0
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +16 -1
- package/plugins-dist/tui/tui.js +320 -88
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +1 -1
- package/plugins-dist/undo/index.js +91 -24
- 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 +7 -7
- package/plugins-dist/web/plugin.json +1 -1
- 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 +23 -5
- 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
|
@@ -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 };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// 模型的上下文窗口(最大输入 token 数)。前端显示上下文用量,以后的 jade/compact 据此决定什么时候压缩。
|
|
2
2
|
//
|
|
3
|
-
// 窗口优先取 JADE_CONTEXT_WINDOW;不设时按模型名在一张小表里找(只收录确定的、取偏小的值),找不到用
|
|
3
|
+
// 窗口优先取 JADE_CONTEXT_WINDOW;不设时按模型名在一张小表里找(只收录确定的、取偏小的值),找不到用 256000。
|
|
4
4
|
import { parseTokens } from "./config.js";
|
|
5
5
|
/** 认不出模型时的窗口 */
|
|
6
|
-
export const DEFAULT_CONTEXT_WINDOW =
|
|
6
|
+
export const DEFAULT_CONTEXT_WINDOW = 256_000;
|
|
7
7
|
/** 模型名(不区分大小写,可以带 "anthropic/" 这类前缀)→ 窗口;按顺序取第一个匹配的 */
|
|
8
8
|
const KNOWN_WINDOWS = [
|
|
9
9
|
[/claude/i, 200_000],
|
|
@@ -4,19 +4,20 @@
|
|
|
4
4
|
// /model 改了配置之后不用重开会话:provider@1 给核心的是一个转发到当前 provider 的壳(设计第 10 节),
|
|
5
5
|
// 下一次请求就用新的。
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { definePlugin } from "
|
|
7
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
8
|
import { contextWindow } from "./context.js";
|
|
9
9
|
import { envWithFile, hasProvider, parseTokens } from "./config.js";
|
|
10
10
|
import { configurator } from "./configure.js";
|
|
11
11
|
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry } from "./provider.js";
|
|
12
|
-
import { readSavedServices } from "./saved.js";
|
|
12
|
+
import { MODEL_SETTINGS, readSavedServices } from "./saved.js";
|
|
13
13
|
export default definePlugin({
|
|
14
14
|
async process(ctx) {
|
|
15
15
|
const envFile = path.join(ctx.home, ".env");
|
|
16
16
|
const modelsFile = path.join(ctx.home, "models.json");
|
|
17
17
|
// 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
|
|
18
18
|
let env = await envWithFile(envFile);
|
|
19
|
-
|
|
19
|
+
// 用的时候再取:jade/tui 也依赖这个插件(models@1),它在这之后才激活,这里取到的是 undefined,重试的提示就没人显示
|
|
20
|
+
const ui = () => ctx.service("ui@1");
|
|
20
21
|
const watchers = new Set();
|
|
21
22
|
const notify = () => {
|
|
22
23
|
for (const fn of watchers) {
|
|
@@ -36,10 +37,10 @@ export default definePlugin({
|
|
|
36
37
|
// 用创建时的这份配置:之后换了配置,新的一份自己建 provider
|
|
37
38
|
const build = (snapshot) => {
|
|
38
39
|
return (async () => {
|
|
39
|
-
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
40
|
+
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui()?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
40
41
|
return withRetry(await providerFromEnv(snapshot), {
|
|
41
42
|
gate,
|
|
42
|
-
onRetry: ({ attempt, retries, delayMs, error }) => ui?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
43
|
+
onRetry: ({ attempt, retries, delayMs, error }) => ui()?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
43
44
|
});
|
|
44
45
|
})();
|
|
45
46
|
};
|
|
@@ -60,27 +61,38 @@ export default definePlugin({
|
|
|
60
61
|
};
|
|
61
62
|
/** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
|
|
62
63
|
const savedProviders = new Map();
|
|
64
|
+
const cachedBuild = (snapshot) => {
|
|
65
|
+
const key = JSON.stringify(snapshot);
|
|
66
|
+
const cached = savedProviders.get(key);
|
|
67
|
+
if (cached)
|
|
68
|
+
return cached;
|
|
69
|
+
const p = build(snapshot);
|
|
70
|
+
savedProviders.set(key, p);
|
|
71
|
+
// 创建失败时下次重试
|
|
72
|
+
p.catch(() => savedProviders.delete(key));
|
|
73
|
+
return p;
|
|
74
|
+
};
|
|
63
75
|
const saved = async (name) => {
|
|
64
76
|
for (const s of await readSavedServices(modelsFile)) {
|
|
65
77
|
const m = s.models.find((x) => x.name === name);
|
|
66
78
|
const kind = providerKind(s.env);
|
|
67
79
|
if (!m || !isBuiltinKind(kind))
|
|
68
80
|
continue;
|
|
69
|
-
|
|
70
|
-
const key = JSON.stringify(snapshot);
|
|
71
|
-
const cached = savedProviders.get(key);
|
|
72
|
-
if (cached)
|
|
73
|
-
return cached;
|
|
74
|
-
const p = build(snapshot);
|
|
75
|
-
savedProviders.set(key, p);
|
|
76
|
-
// 创建失败时下次重试
|
|
77
|
-
p.catch(() => savedProviders.delete(key));
|
|
78
|
-
return p;
|
|
81
|
+
return cachedBuild({ ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name });
|
|
79
82
|
}
|
|
83
|
+
if (!hasProvider(env))
|
|
84
|
+
throw notConfigured();
|
|
80
85
|
// 只写在 .env 里、没保存过的当前模型
|
|
81
|
-
if (
|
|
86
|
+
if (providerInfo(env).model === name)
|
|
82
87
|
return provider();
|
|
83
|
-
|
|
88
|
+
// 当前服务上的另一个模型(没保存过):用这个服务的地址和密钥,不带当前模型自己的设置,用服务的默认值
|
|
89
|
+
const kind = providerKind(env);
|
|
90
|
+
if (!isBuiltinKind(kind))
|
|
91
|
+
throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
|
|
92
|
+
const snapshot = { ...env, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: name };
|
|
93
|
+
for (const k of MODEL_SETTINGS)
|
|
94
|
+
delete snapshot[k];
|
|
95
|
+
return cachedBuild(snapshot);
|
|
84
96
|
};
|
|
85
97
|
const current = (request, options) => provider().then((p) => p.complete(request, options));
|
|
86
98
|
const complete = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
|
|
@@ -162,6 +174,18 @@ export default definePlugin({
|
|
|
162
174
|
},
|
|
163
175
|
});
|
|
164
176
|
},
|
|
177
|
+
session(ctx) {
|
|
178
|
+
// 重试过的请求:把每次失败写进会话日志,事后能查到为什么慢、为什么重新输出(插件事件,不进上下文)
|
|
179
|
+
ctx.kernel.hook({
|
|
180
|
+
kind: "observe",
|
|
181
|
+
point: "model.after",
|
|
182
|
+
fn: async (res, hook) => {
|
|
183
|
+
const retries = res.retries;
|
|
184
|
+
if (retries?.length)
|
|
185
|
+
await hook.emit("retried", { retries });
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
},
|
|
165
189
|
});
|
|
166
190
|
function via(kind, baseURL) {
|
|
167
191
|
const host = baseURL ? safeHost(baseURL) : undefined;
|