@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.
Files changed (207) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +482 -27
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +49 -24
  14. package/plugins/approval/policy.ts +48 -25
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +55 -12
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +10 -9
  50. package/plugins/models/index.ts +31 -4
  51. package/plugins/models/provider.ts +12 -1
  52. package/plugins/modes/index.ts +13 -2
  53. package/plugins/print/index.ts +7 -4
  54. package/plugins/prompts/index.ts +0 -0
  55. package/plugins/renderers/index.ts +10 -5
  56. package/plugins/repeats/index.ts +39 -9
  57. package/plugins/schedule/index.ts +2 -0
  58. package/plugins/screenshot/browser.ts +231 -0
  59. package/plugins/screenshot/index.ts +3 -2
  60. package/plugins/screenshot/tool.ts +27 -51
  61. package/plugins/sessions/index.ts +48 -2
  62. package/plugins/shell/index.ts +75 -22
  63. package/plugins/shell/plugin.json +1 -2
  64. package/plugins/shell/renderers.ts +9 -7
  65. package/plugins/shell/shell.ts +93 -34
  66. package/plugins/skills/index.ts +28 -19
  67. package/plugins/skills/project.ts +12 -7
  68. package/plugins/steer/index.ts +5 -4
  69. package/plugins/task/index.ts +81 -81
  70. package/plugins/task/plugin.json +5 -2
  71. package/plugins/task/style.ts +13 -14
  72. package/plugins/task/subagent.ts +67 -184
  73. package/plugins/todo/index.ts +39 -9
  74. package/plugins/tools/index.ts +57 -0
  75. package/plugins/tools/plugin.json +7 -0
  76. package/plugins/tui/frontend.ts +161 -87
  77. package/plugins/tui/index.ts +2 -3
  78. package/plugins/tui/mascot.ts +8 -7
  79. package/plugins/tui/mentions.ts +11 -80
  80. package/plugins/tui/plugin.json +1 -1
  81. package/plugins/tui/screen.ts +55 -5
  82. package/plugins/tui/transcript.ts +17 -7
  83. package/plugins/tui/tui.ts +356 -104
  84. package/plugins/tui/vendor.ts +1 -1
  85. package/plugins/undo/checkpoint.ts +5 -3
  86. package/plugins/undo/index.ts +43 -14
  87. package/plugins/undo/style.ts +13 -14
  88. package/plugins/web/index.ts +7 -18
  89. package/plugins/web/plugin.json +1 -1
  90. package/plugins-dist/approval/index.js +363 -414
  91. package/plugins-dist/approval/policy.js +395 -497
  92. package/plugins-dist/approval/review.js +132 -158
  93. package/plugins-dist/approval/schema.js +50 -55
  94. package/plugins-dist/ask/index.js +115 -129
  95. package/plugins-dist/budget/index.js +27 -31
  96. package/plugins-dist/checks/checks.js +293 -329
  97. package/plugins-dist/checks/index.js +119 -139
  98. package/plugins-dist/checks/plugin.json +2 -1
  99. package/plugins-dist/checks/project.js +57 -65
  100. package/plugins-dist/commands/index.js +47 -29
  101. package/plugins-dist/compact/compact.js +233 -249
  102. package/plugins-dist/compact/index.js +98 -95
  103. package/plugins-dist/compact/style.js +9 -14
  104. package/plugins-dist/completions/index.js +30 -0
  105. package/plugins-dist/completions/plugin.json +9 -0
  106. package/plugins-dist/continue/index.js +40 -52
  107. package/plugins-dist/copy/index.js +40 -0
  108. package/plugins-dist/copy/plugin.json +9 -0
  109. package/plugins-dist/cost/index.js +98 -115
  110. package/plugins-dist/custom-commands/index.js +55 -72
  111. package/plugins-dist/editor/index.js +64 -72
  112. package/plugins-dist/fs/diff.js +111 -124
  113. package/plugins-dist/fs/fs.js +426 -469
  114. package/plugins-dist/fs/grep.js +262 -244
  115. package/plugins-dist/fs/index.js +20 -34
  116. package/plugins-dist/fs/plugin.json +1 -2
  117. package/plugins-dist/fs/renderers.js +100 -112
  118. package/plugins-dist/fs/style.js +11 -15
  119. package/plugins-dist/fs/sys.js +31 -43
  120. package/plugins-dist/hooks/index.js +107 -127
  121. package/plugins-dist/keys/index.js +27 -31
  122. package/plugins-dist/mcp/index.js +271 -199
  123. package/plugins-dist/mcp/install.js +123 -137
  124. package/plugins-dist/mcp/mcp.js +638 -577
  125. package/plugins-dist/mcp/plugin.json +3 -1
  126. package/plugins-dist/mcp/project.js +98 -115
  127. package/plugins-dist/memory/index.js +83 -88
  128. package/plugins-dist/memory/memory.js +111 -128
  129. package/plugins-dist/memory/style.js +9 -14
  130. package/plugins-dist/mentions/attach.js +91 -99
  131. package/plugins-dist/mentions/candidates.js +49 -0
  132. package/plugins-dist/mentions/index.js +71 -81
  133. package/plugins-dist/mentions/plugin.json +2 -1
  134. package/plugins-dist/mentions/style.js +9 -14
  135. package/plugins-dist/models/config.js +29 -43
  136. package/plugins-dist/models/configure.js +392 -473
  137. package/plugins-dist/models/context.js +14 -22
  138. package/plugins-dist/models/index.js +182 -188
  139. package/plugins-dist/models/provider.js +243 -295
  140. package/plugins-dist/models/saved.js +59 -71
  141. package/plugins-dist/modes/index.js +138 -165
  142. package/plugins-dist/plugins/index.js +34 -34
  143. package/plugins-dist/print/index.js +192 -175
  144. package/plugins-dist/prompts/index.js +81 -89
  145. package/plugins-dist/prompts/notes.js +31 -37
  146. package/plugins-dist/prompts/prompts.js +155 -193
  147. package/plugins-dist/prompts/results.js +40 -53
  148. package/plugins-dist/prompts/sys.js +39 -50
  149. package/plugins-dist/renderers/index.js +32 -32
  150. package/plugins-dist/repeats/index.js +134 -151
  151. package/plugins-dist/schedule/cron.js +49 -56
  152. package/plugins-dist/schedule/index.js +229 -247
  153. package/plugins-dist/schedule/store.js +93 -109
  154. package/plugins-dist/scratch/index.js +12 -18
  155. package/plugins-dist/screenshot/browser.js +169 -0
  156. package/plugins-dist/screenshot/index.js +37 -45
  157. package/plugins-dist/screenshot/sys.js +53 -65
  158. package/plugins-dist/screenshot/tool.js +101 -151
  159. package/plugins-dist/sessions/index.js +149 -117
  160. package/plugins-dist/shell/index.js +117 -74
  161. package/plugins-dist/shell/plugin.json +1 -2
  162. package/plugins-dist/shell/renderers.js +28 -33
  163. package/plugins-dist/shell/service.js +17 -23
  164. package/plugins-dist/shell/shell.js +317 -320
  165. package/plugins-dist/shell/sys.js +64 -79
  166. package/plugins-dist/skills/index.js +193 -211
  167. package/plugins-dist/skills/install.js +122 -149
  168. package/plugins-dist/skills/project.js +65 -73
  169. package/plugins-dist/skills/skills.js +135 -171
  170. package/plugins-dist/steer/index.js +108 -125
  171. package/plugins-dist/suggest/index.js +235 -259
  172. package/plugins-dist/task/index.js +130 -148
  173. package/plugins-dist/task/plugin.json +5 -4
  174. package/plugins-dist/task/style.js +9 -14
  175. package/plugins-dist/task/subagent.js +156 -279
  176. package/plugins-dist/todo/index.js +79 -63
  177. package/plugins-dist/tools/index.js +43 -0
  178. package/plugins-dist/tools/plugin.json +9 -0
  179. package/plugins-dist/tui/editor.js +145 -160
  180. package/plugins-dist/tui/frontend.js +641 -655
  181. package/plugins-dist/tui/history.js +34 -37
  182. package/plugins-dist/tui/index.js +16 -18
  183. package/plugins-dist/tui/markdown.js +203 -247
  184. package/plugins-dist/tui/mascot.js +266 -353
  185. package/plugins-dist/tui/mentions.js +67 -144
  186. package/plugins-dist/tui/plugin.json +1 -2
  187. package/plugins-dist/tui/render.js +35 -45
  188. package/plugins-dist/tui/screen.js +197 -180
  189. package/plugins-dist/tui/transcript.js +436 -468
  190. package/plugins-dist/tui/tui.js +1554 -1645
  191. package/plugins-dist/tui/vendor.js +1388 -1088
  192. package/plugins-dist/undo/checkpoint.js +95 -118
  193. package/plugins-dist/undo/index.js +185 -192
  194. package/plugins-dist/undo/snapshot.js +83 -99
  195. package/plugins-dist/undo/style.js +9 -14
  196. package/plugins-dist/update/index.js +26 -28
  197. package/plugins-dist/web/env.js +55 -68
  198. package/plugins-dist/web/index.js +80 -110
  199. package/plugins-dist/web/plugin.json +1 -2
  200. package/plugins-dist/web/renderers.js +4 -5
  201. package/plugins-dist/web/web.js +514 -593
  202. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  203. package/sdk/python/jade_plugin.py +9 -2
  204. package/src/api.ts +1 -1
  205. package/src/host.ts +39 -4
  206. package/src/remote.ts +6 -1
  207. 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,195 +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
- const ui = ctx.service("ui@1");
20
- const watchers = new Set();
21
- const notify = () => {
22
- for (const fn of watchers) {
23
- try {
24
- fn();
25
- }
26
- catch {
27
- // 订阅者的错不连累模型配置
28
- }
29
- }
30
- };
31
- const notConfigured = () => new Error(`no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:\n` +
32
- " ANTHROPIC_API_KEY=... (Claude)\n" +
33
- " OPENAI_API_KEY=... (OpenAI)\n" +
34
- " OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)");
35
- let made;
36
- // 用创建时的这份配置:之后换了配置,新的一份自己建 provider
37
- const build = (snapshot) => {
38
- 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
- return withRetry(await providerFromEnv(snapshot), {
41
- 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
- });
44
- })();
45
- };
46
- // 第一次用时才创建:配置写错时错误出现在这一轮里,而不是让插件激活失败、连会话都打不开
47
- const provider = () => {
48
- if (!hasProvider(env))
49
- return Promise.reject(notConfigured());
50
- if (made)
51
- return made;
52
- const p = build(env);
53
- made = p;
54
- // 创建失败时下次重试(例如密钥刚写进 .env)
55
- p.catch(() => {
56
- if (made === p)
57
- made = undefined;
58
- });
59
- return p;
60
- };
61
- /** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
62
- const savedProviders = new Map();
63
- const cachedBuild = (snapshot) => {
64
- const key = JSON.stringify(snapshot);
65
- const cached = savedProviders.get(key);
66
- if (cached)
67
- return cached;
68
- const p = build(snapshot);
69
- savedProviders.set(key, p);
70
- // 创建失败时下次重试
71
- p.catch(() => savedProviders.delete(key));
72
- return p;
73
- };
74
- const saved = async (name) => {
75
- for (const s of await readSavedServices(modelsFile)) {
76
- const m = s.models.find((x) => x.name === name);
77
- const kind = providerKind(s.env);
78
- if (!m || !isBuiltinKind(kind))
79
- continue;
80
- return cachedBuild({ ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name });
81
- }
82
- if (!hasProvider(env))
83
- throw notConfigured();
84
- // 只写在 .env 里、没保存过的当前模型
85
- if (providerInfo(env).model === name)
86
- return provider();
87
- // 当前服务上的另一个模型(没保存过):用这个服务的地址和密钥,不带当前模型自己的设置,用服务的默认值
88
- const kind = providerKind(env);
89
- if (!isBuiltinKind(kind))
90
- throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
91
- const snapshot = { ...env, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: name };
92
- for (const k of MODEL_SETTINGS)
93
- delete snapshot[k];
94
- return cachedBuild(snapshot);
95
- };
96
- const current = (request, options) => provider().then((p) => p.complete(request, options));
97
- const complete = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
98
- // 转发的 provider:会话创建时固定的是这个壳,/model 之后下一次请求就用新的配置
99
- ctx.provide("provider@1", { id: "jade/models", complete: current });
100
- ctx.provide("models@1", {
101
- current() {
102
- if (!hasProvider(env))
103
- return undefined;
104
- const info = providerInfo(env);
105
- let vision = true;
106
- try {
107
- vision = visionFromEnv(env);
108
- }
109
- catch {
110
- // 写错的 JADE_VISION 在创建 provider 时报错,这里当作不能看
111
- vision = false;
112
- }
113
- let maxTokens;
114
- try {
115
- maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
116
- }
117
- catch {
118
- // 写错的在创建 provider 时报错
119
- }
120
- return {
121
- kind: info.kind,
122
- model: info.model,
123
- via: via(info.kind, info.baseURL),
124
- contextWindow: contextWindow(info.model, env),
125
- vision,
126
- ...(maxTokens === undefined ? {} : { maxTokens }),
127
- };
128
- },
129
- complete,
130
- list: () => listModels(env),
131
- async saved() {
132
- const names = (await readSavedServices(modelsFile)).flatMap((s) => (isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []));
133
- return [...new Set(names)];
134
- },
135
- subscribe(fn) {
136
- watchers.add(fn);
137
- return () => void watchers.delete(fn);
138
- },
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")
139
37
  });
140
- /** /model 写完配置之后:重新读 .env、换掉 provider;配置不完整时抛出,旧的配置和 provider 继续用 */
141
- const apply = async () => {
142
- const next = await envWithFile(envFile);
143
- const before = { env, provider: made };
144
- env = next;
145
- made = undefined;
146
- try {
147
- await provider();
148
- }
149
- catch (e) {
150
- env = before.env;
151
- made = before.provider;
152
- throw e;
153
- }
154
- 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 }
155
109
  };
156
- ctx.service("commands@1")?.register({
157
- name: "model",
158
- args: "[name]",
159
- description: "switch models, or add one with its provider, API key and limits",
160
- // 只改配置,不碰会话:模型工作时也立即执行
161
- instant: true,
162
- async run(args, c) {
163
- const configure = configurator({
164
- envFile,
165
- modelsFile,
166
- env: () => env,
167
- apply,
168
- report: (text) => c.print(text),
169
- ui: c.ui,
170
- signal: c.signal,
171
- });
172
- await configure.model(args.trim() || undefined);
173
- },
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
174
152
  });
175
- },
176
- session(ctx) {
177
- // 重试过的请求:把每次失败写进会话日志,事后能查到为什么慢、为什么重新输出(插件事件,不进上下文)
178
- ctx.kernel.hook({
179
- kind: "observe",
180
- point: "model.after",
181
- fn: async (res, hook) => {
182
- const retries = res.retries;
183
- if (retries?.length)
184
- await hook.emit("retried", { retries });
185
- },
186
- });
187
- },
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
+ }
188
183
  });
189
184
  function via(kind, baseURL) {
190
- const host = baseURL ? safeHost(baseURL) : undefined;
191
- if (kind === "anthropic")
192
- return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
193
- if (kind === "openai")
194
- return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
195
- 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`;
196
191
  }
197
192
  function safeHost(url) {
198
- try {
199
- return new URL(url).host;
200
- }
201
- catch {
202
- return undefined;
203
- }
193
+ try {
194
+ return new URL(url).host;
195
+ } catch {
196
+ return;
197
+ }
204
198
  }