@uncarved/jade 0.3.1 → 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.
Files changed (199) hide show
  1. package/dist/api.d.ts +1 -1
  2. package/dist/api.js +1 -1
  3. package/dist/host.d.ts +2 -0
  4. package/dist/host.js +39 -5
  5. package/dist/host.js.map +1 -1
  6. package/dist/remote.js +5 -0
  7. package/dist/remote.js.map +1 -1
  8. package/dist/session.d.ts +7 -0
  9. package/dist/session.js +482 -27
  10. package/dist/session.js.map +1 -1
  11. package/package.json +4 -4
  12. package/plugins/approval/index.ts +36 -14
  13. package/plugins/approval/policy.ts +15 -10
  14. package/plugins/approval/review.ts +9 -5
  15. package/plugins/checks/checks.ts +10 -7
  16. package/plugins/checks/index.ts +13 -13
  17. package/plugins/checks/plugin.json +1 -1
  18. package/plugins/checks/project.ts +37 -38
  19. package/plugins/commands/index.ts +27 -3
  20. package/plugins/compact/compact.ts +13 -5
  21. package/plugins/compact/index.ts +20 -3
  22. package/plugins/compact/style.ts +13 -14
  23. package/plugins/completions/index.ts +35 -0
  24. package/plugins/completions/plugin.json +7 -0
  25. package/plugins/copy/index.ts +46 -0
  26. package/plugins/copy/plugin.json +7 -0
  27. package/plugins/editor/index.ts +10 -4
  28. package/plugins/fs/diff.ts +7 -6
  29. package/plugins/fs/fs.ts +8 -4
  30. package/plugins/fs/grep.ts +42 -9
  31. package/plugins/fs/index.ts +2 -7
  32. package/plugins/fs/plugin.json +1 -1
  33. package/plugins/fs/renderers.ts +16 -15
  34. package/plugins/fs/style.ts +16 -15
  35. package/plugins/hooks/index.ts +3 -2
  36. package/plugins/keys/index.ts +5 -1
  37. package/plugins/mcp/index.ts +197 -78
  38. package/plugins/mcp/install.ts +5 -3
  39. package/plugins/mcp/mcp.ts +163 -11
  40. package/plugins/mcp/plugin.json +2 -1
  41. package/plugins/mcp/project.ts +47 -50
  42. package/plugins/memory/index.ts +4 -4
  43. package/plugins/memory/style.ts +13 -14
  44. package/plugins/mentions/candidates.ts +67 -0
  45. package/plugins/mentions/index.ts +12 -5
  46. package/plugins/mentions/plugin.json +1 -1
  47. package/plugins/mentions/style.ts +13 -14
  48. package/plugins/models/configure.ts +10 -9
  49. package/plugins/models/index.ts +27 -1
  50. package/plugins/modes/index.ts +13 -2
  51. package/plugins/print/index.ts +7 -4
  52. package/plugins/prompts/index.ts +0 -0
  53. package/plugins/renderers/index.ts +10 -5
  54. package/plugins/repeats/index.ts +8 -8
  55. package/plugins/schedule/index.ts +2 -0
  56. package/plugins/screenshot/browser.ts +10 -0
  57. package/plugins/sessions/index.ts +48 -2
  58. package/plugins/shell/index.ts +20 -25
  59. package/plugins/shell/plugin.json +1 -2
  60. package/plugins/shell/renderers.ts +9 -7
  61. package/plugins/shell/shell.ts +3 -1
  62. package/plugins/skills/index.ts +28 -19
  63. package/plugins/skills/project.ts +12 -7
  64. package/plugins/steer/index.ts +5 -4
  65. package/plugins/task/index.ts +80 -50
  66. package/plugins/task/plugin.json +6 -2
  67. package/plugins/task/style.ts +13 -14
  68. package/plugins/task/subagent.ts +67 -184
  69. package/plugins/todo/index.ts +39 -9
  70. package/plugins/tools/index.ts +9 -2
  71. package/plugins/tui/frontend.ts +139 -89
  72. package/plugins/tui/index.ts +2 -3
  73. package/plugins/tui/mentions.ts +11 -80
  74. package/plugins/tui/plugin.json +1 -1
  75. package/plugins/tui/screen.ts +55 -5
  76. package/plugins/tui/transcript.ts +17 -7
  77. package/plugins/tui/tui.ts +337 -101
  78. package/plugins/tui/vendor.ts +1 -1
  79. package/plugins/undo/checkpoint.ts +5 -3
  80. package/plugins/undo/index.ts +43 -14
  81. package/plugins/undo/style.ts +13 -14
  82. package/plugins/web/index.ts +7 -18
  83. package/plugins/web/plugin.json +1 -1
  84. package/plugins-dist/approval/index.js +363 -418
  85. package/plugins-dist/approval/policy.js +391 -508
  86. package/plugins-dist/approval/review.js +132 -158
  87. package/plugins-dist/approval/schema.js +50 -55
  88. package/plugins-dist/ask/index.js +115 -129
  89. package/plugins-dist/budget/index.js +27 -31
  90. package/plugins-dist/checks/checks.js +293 -329
  91. package/plugins-dist/checks/index.js +119 -139
  92. package/plugins-dist/checks/plugin.json +2 -1
  93. package/plugins-dist/checks/project.js +57 -65
  94. package/plugins-dist/commands/index.js +47 -29
  95. package/plugins-dist/compact/compact.js +233 -249
  96. package/plugins-dist/compact/index.js +98 -95
  97. package/plugins-dist/compact/style.js +9 -14
  98. package/plugins-dist/completions/index.js +30 -0
  99. package/plugins-dist/completions/plugin.json +9 -0
  100. package/plugins-dist/continue/index.js +40 -52
  101. package/plugins-dist/copy/index.js +40 -0
  102. package/plugins-dist/copy/plugin.json +9 -0
  103. package/plugins-dist/cost/index.js +98 -115
  104. package/plugins-dist/custom-commands/index.js +55 -72
  105. package/plugins-dist/editor/index.js +64 -72
  106. package/plugins-dist/fs/diff.js +111 -124
  107. package/plugins-dist/fs/fs.js +426 -469
  108. package/plugins-dist/fs/grep.js +262 -253
  109. package/plugins-dist/fs/index.js +20 -34
  110. package/plugins-dist/fs/plugin.json +1 -2
  111. package/plugins-dist/fs/renderers.js +100 -112
  112. package/plugins-dist/fs/style.js +11 -15
  113. package/plugins-dist/fs/sys.js +31 -43
  114. package/plugins-dist/hooks/index.js +107 -127
  115. package/plugins-dist/keys/index.js +27 -31
  116. package/plugins-dist/mcp/index.js +271 -199
  117. package/plugins-dist/mcp/install.js +123 -137
  118. package/plugins-dist/mcp/mcp.js +638 -577
  119. package/plugins-dist/mcp/plugin.json +3 -1
  120. package/plugins-dist/mcp/project.js +98 -115
  121. package/plugins-dist/memory/index.js +83 -88
  122. package/plugins-dist/memory/memory.js +111 -128
  123. package/plugins-dist/memory/style.js +9 -14
  124. package/plugins-dist/mentions/attach.js +91 -99
  125. package/plugins-dist/mentions/candidates.js +49 -0
  126. package/plugins-dist/mentions/index.js +71 -81
  127. package/plugins-dist/mentions/plugin.json +2 -1
  128. package/plugins-dist/mentions/style.js +9 -14
  129. package/plugins-dist/models/config.js +29 -43
  130. package/plugins-dist/models/configure.js +392 -473
  131. package/plugins-dist/models/context.js +14 -22
  132. package/plugins-dist/models/index.js +182 -189
  133. package/plugins-dist/models/provider.js +243 -307
  134. package/plugins-dist/models/saved.js +59 -71
  135. package/plugins-dist/modes/index.js +138 -165
  136. package/plugins-dist/plugins/index.js +34 -34
  137. package/plugins-dist/print/index.js +192 -175
  138. package/plugins-dist/prompts/index.js +81 -89
  139. package/plugins-dist/prompts/notes.js +31 -37
  140. package/plugins-dist/prompts/prompts.js +155 -193
  141. package/plugins-dist/prompts/results.js +40 -53
  142. package/plugins-dist/prompts/sys.js +39 -50
  143. package/plugins-dist/renderers/index.js +32 -32
  144. package/plugins-dist/repeats/index.js +131 -174
  145. package/plugins-dist/schedule/cron.js +49 -56
  146. package/plugins-dist/schedule/index.js +229 -247
  147. package/plugins-dist/schedule/store.js +93 -109
  148. package/plugins-dist/scratch/index.js +12 -18
  149. package/plugins-dist/screenshot/browser.js +136 -154
  150. package/plugins-dist/screenshot/index.js +37 -46
  151. package/plugins-dist/screenshot/sys.js +53 -65
  152. package/plugins-dist/screenshot/tool.js +98 -126
  153. package/plugins-dist/sessions/index.js +149 -117
  154. package/plugins-dist/shell/index.js +115 -135
  155. package/plugins-dist/shell/plugin.json +1 -2
  156. package/plugins-dist/shell/renderers.js +28 -33
  157. package/plugins-dist/shell/service.js +17 -23
  158. package/plugins-dist/shell/shell.js +307 -362
  159. package/plugins-dist/shell/sys.js +64 -79
  160. package/plugins-dist/skills/index.js +193 -211
  161. package/plugins-dist/skills/install.js +122 -149
  162. package/plugins-dist/skills/project.js +65 -73
  163. package/plugins-dist/skills/skills.js +135 -171
  164. package/plugins-dist/steer/index.js +108 -125
  165. package/plugins-dist/suggest/index.js +235 -259
  166. package/plugins-dist/task/index.js +130 -117
  167. package/plugins-dist/task/plugin.json +5 -2
  168. package/plugins-dist/task/style.js +9 -14
  169. package/plugins-dist/task/subagent.js +156 -279
  170. package/plugins-dist/todo/index.js +79 -63
  171. package/plugins-dist/tools/index.js +36 -42
  172. package/plugins-dist/tui/editor.js +145 -160
  173. package/plugins-dist/tui/frontend.js +639 -678
  174. package/plugins-dist/tui/history.js +34 -37
  175. package/plugins-dist/tui/index.js +16 -18
  176. package/plugins-dist/tui/markdown.js +203 -247
  177. package/plugins-dist/tui/mascot.js +266 -355
  178. package/plugins-dist/tui/mentions.js +67 -144
  179. package/plugins-dist/tui/plugin.json +1 -2
  180. package/plugins-dist/tui/render.js +35 -45
  181. package/plugins-dist/tui/screen.js +197 -180
  182. package/plugins-dist/tui/transcript.js +436 -468
  183. package/plugins-dist/tui/tui.js +1554 -1656
  184. package/plugins-dist/undo/checkpoint.js +95 -118
  185. package/plugins-dist/undo/index.js +185 -192
  186. package/plugins-dist/undo/snapshot.js +83 -99
  187. package/plugins-dist/undo/style.js +9 -14
  188. package/plugins-dist/update/index.js +26 -28
  189. package/plugins-dist/web/env.js +55 -68
  190. package/plugins-dist/web/index.js +80 -110
  191. package/plugins-dist/web/plugin.json +1 -2
  192. package/plugins-dist/web/renderers.js +4 -5
  193. package/plugins-dist/web/web.js +514 -593
  194. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  195. package/sdk/python/jade_plugin.py +9 -2
  196. package/src/api.ts +1 -1
  197. package/src/host.ts +39 -4
  198. package/src/remote.ts +6 -1
  199. package/src/session.ts +489 -34
@@ -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
- [/claude/i, 200_000],
10
- [/gemini/i, 1_000_000],
11
- [/gpt-4\.1/i, 1_000_000],
12
- // GPT-5 的总窗口是 400k,其中输入最多 272k
13
- [/gpt-5/i, 272_000],
14
- [/(^|\/)o[134](-|$)/i, 200_000],
15
- [/gpt-4o/i, 128_000],
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
- if (env.JADE_CONTEXT_WINDOW) {
21
- try {
22
- return parseTokens("JADE_CONTEXT_WINDOW", env.JADE_CONTEXT_WINDOW);
23
- }
24
- catch { }
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
- async process(ctx) {
15
- const envFile = path.join(ctx.home, ".env");
16
- const modelsFile = path.join(ctx.home, "models.json");
17
- // 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
18
- let env = await envWithFile(envFile);
19
- // 用的时候再取:jade/tui 也依赖这个插件(models@1),它在这之后才激活,这里取到的是 undefined,重试的提示就没人显示
20
- const ui = () => ctx.service("ui@1");
21
- const watchers = new Set();
22
- const notify = () => {
23
- for (const fn of watchers) {
24
- try {
25
- fn();
26
- }
27
- catch {
28
- // 订阅者的错不连累模型配置
29
- }
30
- }
31
- };
32
- const notConfigured = () => new Error(`no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:\n` +
33
- " ANTHROPIC_API_KEY=... (Claude)\n" +
34
- " OPENAI_API_KEY=... (OpenAI)\n" +
35
- " OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)");
36
- let made;
37
- // 用创建时的这份配置:之后换了配置,新的一份自己建 provider
38
- const build = (snapshot) => {
39
- return (async () => {
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
- /** /model 写完配置之后:重新读 .env、换掉 provider;配置不完整时抛出,旧的配置和 provider 继续用 */
142
- const apply = async () => {
143
- const next = await envWithFile(envFile);
144
- const before = { env, provider: made };
145
- env = next;
146
- made = undefined;
147
- try {
148
- await provider();
149
- }
150
- catch (e) {
151
- env = before.env;
152
- made = before.provider;
153
- throw e;
154
- }
155
- notify();
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
- ctx.service("commands@1")?.register({
158
- name: "model",
159
- args: "[name]",
160
- description: "switch models, or add one with its provider, API key and limits",
161
- // 只改配置,不碰会话:模型工作时也立即执行
162
- instant: true,
163
- async run(args, c) {
164
- const configure = configurator({
165
- envFile,
166
- modelsFile,
167
- env: () => env,
168
- apply,
169
- report: (text) => c.print(text),
170
- ui: c.ui,
171
- signal: c.signal,
172
- });
173
- await configure.model(args.trim() || undefined);
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
- 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
- },
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
- const host = baseURL ? safeHost(baseURL) : undefined;
192
- if (kind === "anthropic")
193
- return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
194
- if (kind === "openai")
195
- return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
196
- return host ? `an OpenAI-compatible API at ${host}` : `the ${kind} provider`;
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
- try {
200
- return new URL(url).host;
201
- }
202
- catch {
203
- return undefined;
204
- }
193
+ try {
194
+ return new URL(url).host;
195
+ } catch {
196
+ return;
197
+ }
205
198
  }