@uncarved/jade 0.3.1 → 0.4.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 +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 +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- 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 +42 -9
- 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 +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -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 +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- 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 +80 -50
- package/plugins/task/plugin.json +6 -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 +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- 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 +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -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 -418
- package/plugins-dist/approval/policy.js +391 -508
- 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 -253
- 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 +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- 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 +86 -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 +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- 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 -117
- package/plugins-dist/task/plugin.json +5 -2
- 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 +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- 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 -355
- 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 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -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 +496 -36
|
@@ -1,27 +1,19 @@
|
|
|
1
|
-
// 模型的上下文窗口(最大输入 token 数)。前端显示上下文用量,以后的 jade/compact 据此决定什么时候压缩。
|
|
2
|
-
//
|
|
3
|
-
// 窗口优先取 JADE_CONTEXT_WINDOW;不设时按模型名在一张小表里找(只收录确定的、取偏小的值),找不到用 256000。
|
|
4
1
|
import { parseTokens } from "./config.js";
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_CONTEXT_WINDOW = 256_000;
|
|
7
|
-
/** 模型名(不区分大小写,可以带 "anthropic/" 这类前缀)→ 窗口;按顺序取第一个匹配的 */
|
|
2
|
+
export const DEFAULT_CONTEXT_WINDOW = 256000;
|
|
8
3
|
const KNOWN_WINDOWS = [
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
[/deepseek/i, 128_000],
|
|
4
|
+
[/claude/i, 200000],
|
|
5
|
+
[/gemini/i, 1e6],
|
|
6
|
+
[/gpt-4\.1/i, 1e6],
|
|
7
|
+
[/gpt-5/i, 272000],
|
|
8
|
+
[/(^|\/)o[134](-|$)/i, 200000],
|
|
9
|
+
[/gpt-4o/i, 128000],
|
|
10
|
+
[/deepseek/i, 128000]
|
|
17
11
|
];
|
|
18
|
-
/** 写错的 JADE_CONTEXT_WINDOW 当作没设 */
|
|
19
12
|
export function contextWindow(model, env) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return KNOWN_WINDOWS.find(([re]) => re.test(model))?.[1] ?? DEFAULT_CONTEXT_WINDOW;
|
|
13
|
+
if (env.JADE_CONTEXT_WINDOW) {
|
|
14
|
+
try {
|
|
15
|
+
return parseTokens("JADE_CONTEXT_WINDOW", env.JADE_CONTEXT_WINDOW);
|
|
16
|
+
} catch {}
|
|
17
|
+
}
|
|
18
|
+
return KNOWN_WINDOWS.find(([re]) => re.test(model))?.[1] ?? DEFAULT_CONTEXT_WINDOW;
|
|
27
19
|
}
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
// jade/models:按 $JADE_HOME/.env 和环境变量选模型服务(和旧 jade 的变量相同),外面包一层重试和限速。
|
|
2
|
-
// provider@1 给核心建会话用;models@1 给其他插件(压缩、子 Agent、提示词)调用模型、查看当前配置。
|
|
3
|
-
//
|
|
4
|
-
// /model 改了配置之后不用重开会话:provider@1 给核心的是一个转发到当前 provider 的壳(设计第 10 节),
|
|
5
|
-
// 下一次请求就用新的。
|
|
6
1
|
import path from "node:path";
|
|
7
2
|
import { definePlugin } from "@uncarved/jade-api";
|
|
8
3
|
import { contextWindow } from "./context.js";
|
|
@@ -10,196 +5,194 @@ import { envWithFile, hasProvider, parseTokens } from "./config.js";
|
|
|
10
5
|
import { configurator } from "./configure.js";
|
|
11
6
|
import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry } from "./provider.js";
|
|
12
7
|
import { MODEL_SETTINGS, readSavedServices } from "./saved.js";
|
|
8
|
+
let setup;
|
|
9
|
+
let hinted = false;
|
|
13
10
|
export default definePlugin({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui()?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
41
|
-
return withRetry(await providerFromEnv(snapshot), {
|
|
42
|
-
gate,
|
|
43
|
-
onRetry: ({ attempt, retries, delayMs, error }) => ui()?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
|
|
44
|
-
});
|
|
45
|
-
})();
|
|
46
|
-
};
|
|
47
|
-
// 第一次用时才创建:配置写错时错误出现在这一轮里,而不是让插件激活失败、连会话都打不开
|
|
48
|
-
const provider = () => {
|
|
49
|
-
if (!hasProvider(env))
|
|
50
|
-
return Promise.reject(notConfigured());
|
|
51
|
-
if (made)
|
|
52
|
-
return made;
|
|
53
|
-
const p = build(env);
|
|
54
|
-
made = p;
|
|
55
|
-
// 创建失败时下次重试(例如密钥刚写进 .env)
|
|
56
|
-
p.catch(() => {
|
|
57
|
-
if (made === p)
|
|
58
|
-
made = undefined;
|
|
59
|
-
});
|
|
60
|
-
return p;
|
|
61
|
-
};
|
|
62
|
-
/** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
|
|
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
|
-
};
|
|
75
|
-
const saved = async (name) => {
|
|
76
|
-
for (const s of await readSavedServices(modelsFile)) {
|
|
77
|
-
const m = s.models.find((x) => x.name === name);
|
|
78
|
-
const kind = providerKind(s.env);
|
|
79
|
-
if (!m || !isBuiltinKind(kind))
|
|
80
|
-
continue;
|
|
81
|
-
return cachedBuild({ ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name });
|
|
82
|
-
}
|
|
83
|
-
if (!hasProvider(env))
|
|
84
|
-
throw notConfigured();
|
|
85
|
-
// 只写在 .env 里、没保存过的当前模型
|
|
86
|
-
if (providerInfo(env).model === name)
|
|
87
|
-
return provider();
|
|
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);
|
|
96
|
-
};
|
|
97
|
-
const current = (request, options) => provider().then((p) => p.complete(request, options));
|
|
98
|
-
const complete = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
|
|
99
|
-
// 转发的 provider:会话创建时固定的是这个壳,/model 之后下一次请求就用新的配置
|
|
100
|
-
ctx.provide("provider@1", { id: "jade/models", complete: current });
|
|
101
|
-
ctx.provide("models@1", {
|
|
102
|
-
current() {
|
|
103
|
-
if (!hasProvider(env))
|
|
104
|
-
return undefined;
|
|
105
|
-
const info = providerInfo(env);
|
|
106
|
-
let vision = true;
|
|
107
|
-
try {
|
|
108
|
-
vision = visionFromEnv(env);
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
// 写错的 JADE_VISION 在创建 provider 时报错,这里当作不能看
|
|
112
|
-
vision = false;
|
|
113
|
-
}
|
|
114
|
-
let maxTokens;
|
|
115
|
-
try {
|
|
116
|
-
maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
|
|
117
|
-
}
|
|
118
|
-
catch {
|
|
119
|
-
// 写错的在创建 provider 时报错
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
kind: info.kind,
|
|
123
|
-
model: info.model,
|
|
124
|
-
via: via(info.kind, info.baseURL),
|
|
125
|
-
contextWindow: contextWindow(info.model, env),
|
|
126
|
-
vision,
|
|
127
|
-
...(maxTokens === undefined ? {} : { maxTokens }),
|
|
128
|
-
};
|
|
129
|
-
},
|
|
130
|
-
complete,
|
|
131
|
-
list: () => listModels(env),
|
|
132
|
-
async saved() {
|
|
133
|
-
const names = (await readSavedServices(modelsFile)).flatMap((s) => (isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []));
|
|
134
|
-
return [...new Set(names)];
|
|
135
|
-
},
|
|
136
|
-
subscribe(fn) {
|
|
137
|
-
watchers.add(fn);
|
|
138
|
-
return () => void watchers.delete(fn);
|
|
139
|
-
},
|
|
11
|
+
async process(ctx) {
|
|
12
|
+
const envFile = path.join(ctx.home, ".env");
|
|
13
|
+
const modelsFile = path.join(ctx.home, "models.json");
|
|
14
|
+
let env = await envWithFile(envFile);
|
|
15
|
+
hinted = false;
|
|
16
|
+
setup = { configured: () => hasProvider(env), command: false, envFile };
|
|
17
|
+
const ui = () => ctx.service("ui@1");
|
|
18
|
+
const watchers = new Set;
|
|
19
|
+
const notify = () => {
|
|
20
|
+
for (const fn of watchers) {
|
|
21
|
+
try {
|
|
22
|
+
fn();
|
|
23
|
+
} catch {}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const notConfigured = () => new Error(`no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:
|
|
27
|
+
` + ` ANTHROPIC_API_KEY=... (Claude)
|
|
28
|
+
` + ` OPENAI_API_KEY=... (OpenAI)
|
|
29
|
+
` + " OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)");
|
|
30
|
+
let made;
|
|
31
|
+
const build = (snapshot) => {
|
|
32
|
+
return (async () => {
|
|
33
|
+
const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui()?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
|
|
34
|
+
return withRetry(await providerFromEnv(snapshot), {
|
|
35
|
+
gate,
|
|
36
|
+
onRetry: ({ attempt, retries, delayMs, error }) => ui()?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning")
|
|
140
37
|
});
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
38
|
+
})();
|
|
39
|
+
};
|
|
40
|
+
const provider = () => {
|
|
41
|
+
if (!hasProvider(env))
|
|
42
|
+
return Promise.reject(notConfigured());
|
|
43
|
+
if (made)
|
|
44
|
+
return made;
|
|
45
|
+
const p = build(env);
|
|
46
|
+
made = p;
|
|
47
|
+
p.catch(() => {
|
|
48
|
+
if (made === p)
|
|
49
|
+
made = undefined;
|
|
50
|
+
});
|
|
51
|
+
return p;
|
|
52
|
+
};
|
|
53
|
+
const savedProviders = new Map;
|
|
54
|
+
const cachedBuild = (snapshot) => {
|
|
55
|
+
const key = JSON.stringify(snapshot);
|
|
56
|
+
const cached = savedProviders.get(key);
|
|
57
|
+
if (cached)
|
|
58
|
+
return cached;
|
|
59
|
+
const p = build(snapshot);
|
|
60
|
+
savedProviders.set(key, p);
|
|
61
|
+
p.catch(() => savedProviders.delete(key));
|
|
62
|
+
return p;
|
|
63
|
+
};
|
|
64
|
+
const saved = async (name) => {
|
|
65
|
+
for (const s of await readSavedServices(modelsFile)) {
|
|
66
|
+
const m = s.models.find((x) => x.name === name);
|
|
67
|
+
const kind = providerKind(s.env);
|
|
68
|
+
if (!m || !isBuiltinKind(kind))
|
|
69
|
+
continue;
|
|
70
|
+
return cachedBuild({ ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name });
|
|
71
|
+
}
|
|
72
|
+
if (!hasProvider(env))
|
|
73
|
+
throw notConfigured();
|
|
74
|
+
if (providerInfo(env).model === name)
|
|
75
|
+
return provider();
|
|
76
|
+
const kind = providerKind(env);
|
|
77
|
+
if (!isBuiltinKind(kind))
|
|
78
|
+
throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
|
|
79
|
+
const snapshot = { ...env, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: name };
|
|
80
|
+
for (const k of MODEL_SETTINGS)
|
|
81
|
+
delete snapshot[k];
|
|
82
|
+
return cachedBuild(snapshot);
|
|
83
|
+
};
|
|
84
|
+
const current = (request, options) => provider().then((p) => p.complete(request, options));
|
|
85
|
+
const complete = (request, { model, ...options }) => model ? saved(model).then((p) => p.complete(request, options)) : current(request, options);
|
|
86
|
+
ctx.provide("provider@1", { id: "jade/models", complete: current });
|
|
87
|
+
ctx.provide("models@1", {
|
|
88
|
+
current() {
|
|
89
|
+
if (!hasProvider(env))
|
|
90
|
+
return;
|
|
91
|
+
const info = providerInfo(env);
|
|
92
|
+
let vision = true;
|
|
93
|
+
try {
|
|
94
|
+
vision = visionFromEnv(env);
|
|
95
|
+
} catch {
|
|
96
|
+
vision = false;
|
|
97
|
+
}
|
|
98
|
+
let maxTokens;
|
|
99
|
+
try {
|
|
100
|
+
maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
|
|
101
|
+
} catch {}
|
|
102
|
+
return {
|
|
103
|
+
kind: info.kind,
|
|
104
|
+
model: info.model,
|
|
105
|
+
via: via(info.kind, info.baseURL),
|
|
106
|
+
contextWindow: contextWindow(info.model, env),
|
|
107
|
+
vision,
|
|
108
|
+
...maxTokens === undefined ? {} : { maxTokens }
|
|
156
109
|
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
110
|
+
},
|
|
111
|
+
complete,
|
|
112
|
+
list: () => listModels(env),
|
|
113
|
+
async saved() {
|
|
114
|
+
const names = (await readSavedServices(modelsFile)).flatMap((s) => isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []);
|
|
115
|
+
return [...new Set(names)];
|
|
116
|
+
},
|
|
117
|
+
subscribe(fn) {
|
|
118
|
+
watchers.add(fn);
|
|
119
|
+
return () => void watchers.delete(fn);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const apply = async () => {
|
|
123
|
+
const next = await envWithFile(envFile);
|
|
124
|
+
const before = { env, provider: made };
|
|
125
|
+
env = next;
|
|
126
|
+
made = undefined;
|
|
127
|
+
try {
|
|
128
|
+
await provider();
|
|
129
|
+
} catch (e) {
|
|
130
|
+
env = before.env;
|
|
131
|
+
made = before.provider;
|
|
132
|
+
throw e;
|
|
133
|
+
}
|
|
134
|
+
notify();
|
|
135
|
+
};
|
|
136
|
+
const commands = ctx.service("commands@1");
|
|
137
|
+
setup.command = commands !== undefined;
|
|
138
|
+
commands?.register({
|
|
139
|
+
name: "model",
|
|
140
|
+
args: "[name]",
|
|
141
|
+
description: "switch models, or add one with its provider, API key and limits",
|
|
142
|
+
instant: true,
|
|
143
|
+
async run(args, c) {
|
|
144
|
+
const configure = configurator({
|
|
145
|
+
envFile,
|
|
146
|
+
modelsFile,
|
|
147
|
+
env: () => env,
|
|
148
|
+
apply,
|
|
149
|
+
report: (text) => c.print(text),
|
|
150
|
+
ui: c.ui,
|
|
151
|
+
signal: c.signal
|
|
175
152
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
153
|
+
await configure.model(args.trim() || undefined);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
session(ctx) {
|
|
158
|
+
const ui = ctx.service("ui@1");
|
|
159
|
+
if (setup && !setup.configured() && !hinted && ui?.interactive) {
|
|
160
|
+
hinted = true;
|
|
161
|
+
const file = `put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${setup.envFile}, then restart jade`;
|
|
162
|
+
ui.notify(setup.command ? `No model is configured yet. Type /model to set one up, or ${file}.` : `No model is configured yet. To set one up, ${file}.`, "warning");
|
|
163
|
+
}
|
|
164
|
+
ctx.kernel.hook({
|
|
165
|
+
kind: "observe",
|
|
166
|
+
point: "event",
|
|
167
|
+
fn: (e) => {
|
|
168
|
+
if (e.type === "turn.ended" && e.data.reason === "max_tokens" && setup?.command) {
|
|
169
|
+
ui?.notify("if replies keep getting cut off, raise the output limit with /model", "info");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
ctx.kernel.hook({
|
|
174
|
+
kind: "observe",
|
|
175
|
+
point: "model.after",
|
|
176
|
+
fn: async (res, hook) => {
|
|
177
|
+
const retries = res.retries;
|
|
178
|
+
if (retries?.length)
|
|
179
|
+
await hook.emit("retried", { retries });
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
189
183
|
});
|
|
190
184
|
function via(kind, baseURL) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
185
|
+
const host = baseURL ? safeHost(baseURL) : undefined;
|
|
186
|
+
if (kind === "anthropic")
|
|
187
|
+
return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
|
|
188
|
+
if (kind === "openai")
|
|
189
|
+
return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
|
|
190
|
+
return host ? `an OpenAI-compatible API at ${host}` : `the ${kind} provider`;
|
|
197
191
|
}
|
|
198
192
|
function safeHost(url) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
193
|
+
try {
|
|
194
|
+
return new URL(url).host;
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
205
198
|
}
|