@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,33 +1,31 @@
1
- // jade/update:启动时(每天最多一次)查 npm 上有没有更新的 jade,有就在后台升级,下次启动生效;
2
- // 配置 auto 为 false 或 JADE_AUTO_UPDATE=off 时只提示。仓库里的开发版和 npm link 既不提示也不升级。
3
- // 查询和升级经核心的 update@1 服务,和 jade update 是同一套;不想要它就 jade plugins disable jade/update。
4
1
  import { definePlugin } from "@uncarved/jade-api";
5
- /** 查一次,按结果提示;出什么错都不打扰用户 */
6
2
  export async function checkForUpdate(o) {
7
- const { update } = o;
8
- if (update.install === "dev")
9
- return;
10
- const latest = await update.newer().catch(() => undefined);
11
- if (!latest)
12
- return;
13
- const manual = () => o.notify(`jade ${latest} is available (you have ${update.version}); run jade update`);
14
- if (!o.auto || !update.autoEnabled())
15
- return manual();
16
- const result = await update.upgrade(latest).catch(() => undefined);
17
- if (result?.status === "installed")
18
- o.notify(`jade ${latest} was installed in the background; restart jade to use it`);
19
- else if (result?.status === "failed")
20
- o.notify(`automatic update to jade ${latest} failed (see ${result.log}); run jade update`, "warning");
21
- else if (result?.status !== "skipped" || result.why !== "busy")
22
- manual();
3
+ const { update } = o;
4
+ if (update.install === "dev")
5
+ return;
6
+ const latest = await update.newer().catch(() => {
7
+ return;
8
+ });
9
+ if (!latest)
10
+ return;
11
+ const manual = () => o.notify(`jade ${latest} is available (you have ${update.version}); run jade update`);
12
+ if (!o.auto || !update.autoEnabled())
13
+ return manual();
14
+ const result = await update.upgrade(latest).catch(() => {
15
+ return;
16
+ });
17
+ if (result?.status === "installed")
18
+ o.notify(`jade ${latest} was installed in the background; restart jade to use it`);
19
+ else if (result?.status === "failed")
20
+ o.notify(`automatic update to jade ${latest} failed (see ${result.log}); run jade update`, "warning");
21
+ else if (result?.status !== "skipped" || result.why !== "busy")
22
+ manual();
23
23
  }
24
24
  export default definePlugin({
25
- process(ctx) {
26
- const ui = ctx.service("ui@1");
27
- // 不能提问的前端(jade -p)是脚本在用,不提示也不升级
28
- if (!ui?.interactive)
29
- return;
30
- // 作用域关闭后 ui 失效,再提示会抛错:那时也没人看了
31
- void checkForUpdate({ update: ctx.require("update@1"), auto: ctx.config.auto !== false, notify: (m, level) => ui.notify(m, level) }).catch(() => { });
32
- },
25
+ process(ctx) {
26
+ const ui = ctx.service("ui@1");
27
+ if (!ui?.interactive)
28
+ return;
29
+ checkForUpdate({ update: ctx.require("update@1"), auto: ctx.config.auto !== false, notify: (m, level) => ui.notify(m, level) }).catch(() => {});
30
+ }
33
31
  });
@@ -1,80 +1,67 @@
1
- // 搜索服务的 API key:shell 里的环境变量 + $JADE_HOME/.env(shell 里已经设置的优先),和 jade/models 读同一个文件。
2
- // 从旧 jade 的 config.ts 复制:插件之间不共享代码,jade/models 也有一份 parseEnv。
3
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
4
2
  import path from "node:path";
5
- /** 解析 KEY=VALUE 格式:忽略空行和 # 注释,允许 export 前缀,去掉成对的引号(双引号里的 \n 换成换行) */
6
3
  export function parseEnv(text) {
7
- const out = {};
8
- for (const raw of text.split(/\r?\n/)) {
9
- const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
10
- if (!m)
11
- continue;
12
- let value = m[2];
13
- const quote = value[0];
14
- if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
15
- value = value.slice(1, -1);
16
- if (quote === '"')
17
- value = value.replaceAll("\\n", "\n");
18
- }
19
- else {
20
- // 未加引号的值里,空白之后的 # 是注释
21
- value = value.replace(/\s+#.*$/, "");
22
- }
23
- out[m[1]] = value;
4
+ const out = {};
5
+ for (const raw of text.split(/\r?\n/)) {
6
+ const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
7
+ if (!m)
8
+ continue;
9
+ let value = m[2];
10
+ const quote = value[0];
11
+ if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
12
+ value = value.slice(1, -1);
13
+ if (quote === '"')
14
+ value = value.replaceAll("\\n", `
15
+ `);
16
+ } else {
17
+ value = value.replace(/\s+#.*$/, "");
24
18
  }
25
- return out;
19
+ out[m[1]] = value;
20
+ }
21
+ return out;
26
22
  }
27
- /** 配置文件里的变量,env 里已有的优先;文件不存在时就是 env */
28
23
  export async function envWithFile(file, env = process.env) {
29
- let text = "";
30
- try {
31
- text = await readFile(file, "utf8");
32
- }
33
- catch {
34
- // 没有配置文件
35
- }
36
- return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
24
+ let text = "";
25
+ try {
26
+ text = await readFile(file, "utf8");
27
+ } catch {}
28
+ return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
37
29
  }
38
- /**
39
- * 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
40
- * 同时更新 env,让改动在本次运行中生效
41
- */
42
30
  export async function updateEnvFile(file, updates, env) {
43
- let text = "";
44
- try {
45
- text = await readFile(file, "utf8");
46
- }
47
- catch {
48
- // 还没有配置文件
49
- }
50
- const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
51
- const out = [];
52
- const written = new Set();
53
- for (const raw of lines) {
54
- const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
55
- if (!key || !(key in updates)) {
56
- out.push(raw);
57
- continue;
58
- }
59
- // 同一个键出现多次时只保留第一处
60
- const value = updates[key];
61
- if (value !== undefined && !written.has(key))
62
- out.push(format(key, value));
63
- written.add(key);
64
- }
65
- for (const [key, value] of Object.entries(updates)) {
66
- if (value !== undefined && !written.has(key))
67
- out.push(format(key, value));
68
- }
69
- await mkdir(path.dirname(file), { recursive: true });
70
- await writeFile(file, out.join("\n") + "\n");
71
- for (const [key, value] of Object.entries(updates)) {
72
- if (value === undefined)
73
- delete env[key];
74
- else
75
- env[key] = value;
31
+ let text = "";
32
+ try {
33
+ text = await readFile(file, "utf8");
34
+ } catch {}
35
+ const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
36
+ const out = [];
37
+ const written = new Set;
38
+ for (const raw of lines) {
39
+ const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
40
+ if (!key || !(key in updates)) {
41
+ out.push(raw);
42
+ continue;
76
43
  }
44
+ const value = updates[key];
45
+ if (value !== undefined && !written.has(key))
46
+ out.push(format(key, value));
47
+ written.add(key);
48
+ }
49
+ for (const [key, value] of Object.entries(updates)) {
50
+ if (value !== undefined && !written.has(key))
51
+ out.push(format(key, value));
52
+ }
53
+ await mkdir(path.dirname(file), { recursive: true });
54
+ await writeFile(file, out.join(`
55
+ `) + `
56
+ `);
57
+ for (const [key, value] of Object.entries(updates)) {
58
+ if (value === undefined)
59
+ delete env[key];
60
+ else
61
+ env[key] = value;
62
+ }
77
63
  }
78
64
  function format(key, value) {
79
- return /[\s#"']/.test(value) ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}=${value}`;
65
+ return /[\s#"']/.test(value) ? `${key}="${value.replaceAll(`
66
+ `, "\\n")}"` : `${key}=${value}`;
80
67
  }
@@ -1,124 +1,94 @@
1
- // jade/web:web_fetch、web_search 工具和 /search 命令。
2
- //
3
- // 搜索服务的 API key 在 shell 的环境变量或 $JADE_HOME/.env 里(和 jade/models 同一个文件),/search 写进这个文件。
4
- // web_search 总是注册,但只在配置了搜索服务时发给模型(context.build 里去掉):这样 /search 设置之后当前会话立即能用,
5
- // 不用重开会话;会话的工具清单在创建时就定了。
6
1
  import path from "node:path";
7
2
  import { definePlugin } from "@uncarved/jade-api";
8
3
  import { envWithFile, updateEnvFile } from "./env.js";
9
4
  import { webRenderers } from "./renderers.js";
10
5
  import { parseOrder, parseResults, SEARCH_PROVIDERS, webTools } from "./web.js";
11
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
12
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
13
- const dim = (s) => (colors ? `\x1b[2m${s}\x1b[22m` : s);
14
- const green = (s) => (colors ? `\x1b[32m${s}\x1b[39m` : s);
15
- /**
16
- * API key 所在的环境:每次开会话时重新读,/search 改了之后原地更新。模块每个进程只加载一次,进程级的命令和会话级的工具共用
17
- */
6
+ const dim = (text) => ({ text, style: "dim" });
7
+ const ok = (text) => ({ text, style: "ok" });
18
8
  let keys;
19
- /** 撤销当前会话里"web_search 没打开"的提示词段落;/search 打开搜索后调用 */
20
9
  let offNote;
21
10
  const envFile = (ctx) => path.join(ctx.home, ".env");
22
- /** 配置项,旧 jade 的环境变量 JADE_SEARCH、JADE_SEARCH_RESULTS 仍然有效(配置项优先);不合法时抛错 */
23
11
  function settings(ctx, env) {
24
- const fromConfig = ctx.config.search !== undefined && ctx.config.search !== null;
25
- const order = parseOrder(fromConfig ? ctx.config.search : env.JADE_SEARCH, fromConfig ? "jade/web config search" : "JADE_SEARCH");
26
- const counted = ctx.config.results !== undefined && ctx.config.results !== null;
27
- const results = parseResults(counted ? ctx.config.results : env.JADE_SEARCH_RESULTS, counted ? "jade/web config results" : "JADE_SEARCH_RESULTS");
28
- return { ...(order ? { order } : {}), results };
12
+ const fromConfig = ctx.config.search !== undefined && ctx.config.search !== null;
13
+ const order = parseOrder(fromConfig ? ctx.config.search : env.JADE_SEARCH, fromConfig ? "jade/web config search" : "JADE_SEARCH");
14
+ const counted = ctx.config.results !== undefined && ctx.config.results !== null;
15
+ const results = parseResults(counted ? ctx.config.results : env.JADE_SEARCH_RESULTS, counted ? "jade/web config results" : "JADE_SEARCH_RESULTS");
16
+ return { ...order ? { order } : {}, results };
29
17
  }
30
18
  export default definePlugin({
31
- process(ctx) {
32
- const renderers = ctx.service("renderers@1");
33
- if (renderers)
34
- for (const r of webRenderers)
35
- renderers.tool(r);
36
- ctx.service("commands@1")?.register({
37
- name: "search",
38
- description: "set up web search services (TinyFish, AnySearch, Tavily, Exa, Firecrawl, Jina)",
39
- async run(_args, c) {
40
- const file = envFile(ctx);
41
- const env = (keys ??= await envWithFile(file));
42
- const on = SEARCH_PROVIDERS.filter((p) => env[p.env]);
43
- const name = await c.ui.select({
44
- title: on.length ? `Web search is on (${on.map((p) => p.name).join(", ")}; tried in order, the next one takes over on failure)` : "Web search is off",
45
- options: SEARCH_PROVIDERS.map((p) => ({ label: `${p.name}${env[p.env] ? " ✓" : ""}`, value: p.name, hint: p.signup })),
46
- }, c.signal);
47
- const p = SEARCH_PROVIDERS.find((x) => x.name === name);
48
- if (!p)
49
- return;
50
- // shell 里的变量盖过文件:改文件只在这个会话里看起来生效,下一个会话又变回 shell 里的值
51
- if (process.env[p.env])
52
- return `${p.env} is set in your shell environment, which takes precedence over ${file}; change or unset it there.`;
53
- if (env[p.env]) {
54
- const choice = await c.ui.select({
55
- title: p.name,
56
- options: [
57
- { label: "Change the API key…", value: "set" },
58
- { label: "Remove", value: "off", hint: `removes ${p.env} from ${file}` },
59
- ],
60
- }, c.signal);
61
- if (!choice)
62
- return;
63
- if (choice === "off") {
64
- await updateEnvFile(file, { [p.env]: undefined }, env);
65
- return dim(`${p.name} removed.${on.length > 1 ? "" : " Web search is off."}`);
66
- }
67
- }
68
- const key = (await c.ui.input({ title: `${p.name} API key`, mask: true }, c.signal))?.trim();
69
- if (!key)
70
- return;
71
- await updateEnvFile(file, { [p.env]: key }, env);
72
- offNote?.();
73
- offNote = undefined;
74
- return green(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
75
- },
76
- });
77
- },
78
- async session(ctx) {
79
- const env = (keys = await envWithFile(envFile(ctx)));
80
- let opts;
81
- try {
82
- opts = settings(ctx, env);
83
- }
84
- catch (e) {
85
- // 写错的配置不让 web_fetch 也用不了:搜索按默认设置
86
- ctx.service("ui@1")?.notify(`${e.message}; using the default search settings`, "warning");
87
- opts = { results: parseResults(undefined, "") };
88
- }
89
- // keys 换成新的对象(新会话、/search 之前)时也取当前的那个
90
- const web = webTools({ ...opts, env: () => keys ?? env });
91
- for (const tool of web.tools)
92
- ctx.kernel.tool(tool);
93
- // 子 Agent(jade/task)也能查网页:另一套实例;web_search 只在配置了搜索服务时给它。
94
- // web_fetch 带 url 参数,子 Agent 每次都要问用户(URL 能把读到的内容带出去)。
95
- // jade/task 被隔离时它的服务已失效:子 Agent 没有 web 工具,web 工具本身照常
96
- try {
97
- const task = ctx.service("task@1");
98
- if (task) {
99
- const sub = webTools({ ...opts, env: () => keys ?? env });
100
- for (const tool of sub.tools)
101
- task.tool(tool, tool.name === "web_search" ? { when: () => sub.search().length > 0 } : {});
102
- }
19
+ process(ctx) {
20
+ const renderers = ctx.service("renderers@1");
21
+ if (renderers)
22
+ for (const r of webRenderers)
23
+ renderers.tool(r);
24
+ ctx.service("commands@1")?.register({
25
+ name: "search",
26
+ description: "set up web search services (TinyFish, AnySearch, Tavily, Exa, Firecrawl, Jina)",
27
+ async run(_args, c) {
28
+ const file = envFile(ctx);
29
+ const env = keys ??= await envWithFile(file);
30
+ const on = SEARCH_PROVIDERS.filter((p) => env[p.env]);
31
+ const name = await c.ui.select({
32
+ title: on.length ? `Web search is on (${on.map((p) => p.name).join(", ")}; tried in order, the next one takes over on failure)` : "Web search is off",
33
+ options: SEARCH_PROVIDERS.map((p) => ({ label: `${p.name}${env[p.env] ? " ✓" : ""}`, value: p.name, hint: p.signup }))
34
+ }, c.signal);
35
+ const p = SEARCH_PROVIDERS.find((x) => x.name === name);
36
+ if (!p)
37
+ return;
38
+ if (process.env[p.env])
39
+ return `${p.env} is set in your shell environment, which takes precedence over ${file}; change or unset it there.`;
40
+ if (env[p.env]) {
41
+ const choice = await c.ui.select({
42
+ title: p.name,
43
+ options: [
44
+ { label: "Change the API key…", value: "set" },
45
+ { label: "Remove", value: "off", hint: `removes ${p.env} from ${file}` }
46
+ ]
47
+ }, c.signal);
48
+ if (!choice)
49
+ return;
50
+ if (choice === "off") {
51
+ await updateEnvFile(file, { [p.env]: undefined }, env);
52
+ return dim(`${p.name} removed.${on.length > 1 ? "" : " Web search is off."}`);
53
+ }
103
54
  }
104
- catch { }
105
- ctx.kernel.hook({
106
- kind: "transform",
107
- point: "context.build",
108
- fn: (req) => (web.search().length ? req : { ...req, tools: req.tools.filter((t) => t.name !== "web_search") }),
109
- });
110
- // 没有搜索时告诉模型用户可以打开它;有搜索时不用说
55
+ const key = (await c.ui.input({ title: `${p.name} API key`, mask: true }, c.signal))?.trim();
56
+ if (!key)
57
+ return;
58
+ await updateEnvFile(file, { [p.env]: key }, env);
59
+ offNote?.();
111
60
  offNote = undefined;
112
- if (web.search().length === 0 && ctx.service("commands@1")) {
113
- const remove = ctx.service("prompts@1")?.add({ text: "- web_search is off; if you need to search the web, the user can turn it on with /search." });
114
- if (remove) {
115
- offNote = remove;
116
- // 会话关闭时段落随作用域撤销,这里只忘掉它
117
- await ctx.effect(() => () => {
118
- if (offNote === remove)
119
- offNote = undefined;
120
- });
121
- }
122
- }
123
- },
61
+ return ok(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
62
+ }
63
+ });
64
+ },
65
+ async session(ctx) {
66
+ const env = keys = await envWithFile(envFile(ctx));
67
+ let opts;
68
+ try {
69
+ opts = settings(ctx, env);
70
+ } catch (e) {
71
+ ctx.service("ui@1")?.notify(`${e.message}; using the default search settings`, "warning");
72
+ opts = { results: parseResults(undefined, "") };
73
+ }
74
+ const web = webTools({ ...opts, env: () => keys ?? env });
75
+ for (const tool of web.tools)
76
+ ctx.kernel.tool(tool);
77
+ ctx.kernel.hook({
78
+ kind: "transform",
79
+ point: "context.build",
80
+ fn: (req) => web.search().length ? req : { ...req, tools: req.tools.filter((t) => t.name !== "web_search") }
81
+ });
82
+ offNote = undefined;
83
+ if (web.search().length === 0 && ctx.service("commands@1")) {
84
+ const remove = ctx.service("prompts@1")?.add({ text: "- web_search is off; if you need to search the web, the user can turn it on with /search." });
85
+ if (remove) {
86
+ offNote = remove;
87
+ await ctx.effect(() => () => {
88
+ if (offNote === remove)
89
+ offNote = undefined;
90
+ });
91
+ }
92
+ }
93
+ }
124
94
  });
@@ -7,8 +7,7 @@
7
7
  "ui@1",
8
8
  "commands@1",
9
9
  "prompts@1",
10
- "renderers@1",
11
- "task@1"
10
+ "renderers@1"
12
11
  ],
13
12
  "config": {
14
13
  "search": {
@@ -1,7 +1,6 @@
1
- // web_fetch、web_search 在对话里怎么显示(登记到 renderers@1)。从旧 jade 的 ui/renderers.ts 移植。
2
- const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
3
- const args = (input) => (input !== null && typeof input === "object" ? input : {});
1
+ const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
2
+ const args = (input) => input !== null && typeof input === "object" ? input : {};
4
3
  export const webRenderers = [
5
- { tool: "web_fetch", summary: (input) => clip(String(args(input).url ?? ""), 120) },
6
- { tool: "web_search", summary: (input) => clip(String(args(input).query ?? ""), 120) },
4
+ { tool: "web_fetch", summary: (input) => clip(String(args(input).url ?? ""), 120) },
5
+ { tool: "web_search", summary: (input) => clip(String(args(input).query ?? ""), 120) }
7
6
  ];