@vincentbanana/bananacli 0.8.4 → 0.8.5

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 CHANGED
@@ -228,8 +228,8 @@ export default {
228
228
  | `glob` | – | `**/*.ts`、`{a,b}` 风格 glob |
229
229
  | `grep` | – | 正则搜代码,返回 `file:line` |
230
230
  | `shell` | shell | 执行命令,回传 stdout/stderr/退出码,带超时与输出截断 |
231
- | `web_search` | network | 优先 Tavily / Brave,回退 DuckDuckGo |
232
- | `web_fetch` | network | 抓 URL 转纯文本(HTML 去标签去脚本) |
231
+ | `web_search` | network | 联网搜索;默认合并 Bing / DuckDuckGo,配置 Tavily / Brave Key 自动升级;支持 depth=deep 读正文 |
232
+ | `web_fetch` | network | 抓 URL 转纯文本(HTML 去导航、脚本,保留正文段落) |
233
233
  | `todo_write` | – | 写入/更新任务清单(整表覆盖,随会话保存) |
234
234
  | `todo_read` | – | 读取当前任务清单 |
235
235
  | 插件工具 | 自定义 | `.banana/tools/` 下的脚本自动注册,权限类别由插件声明 |
@@ -257,7 +257,7 @@ export default {
257
257
 
258
258
  Anthropic 协议默认给 `system` 和工具定义打 `cache_control: ephemeral`(prompt caching,长对话能省不少钱);如果你的网关不认这个字段,在对应 provider 里设 `"promptCaching": false`。
259
259
 
260
- 环境变量:`ANTHROPIC_API_KEY` / `OPENAI_API_KEY` / `DEEPSEEK_API_KEY` / `MOONSHOT_API_KEY` / `DASHSCOPE_API_KEY` / `GEMINI_API_KEY` / `OPENROUTER_API_KEY`;搜索可选 `TAVILY_API_KEY` 或 `BRAVE_API_KEY`;临时覆盖用 `BANANA_PROVIDER` / `BANANA_MODEL` / `BANANA_BASE_URL` / `BANANA_SHOW_REASONING` / `BANANA_HOME` / `BANANA_CONFIG`。
260
+ 环境变量:`ANTHROPIC_API_KEY` / `OPENAI_API_KEY` / `DEEPSEEK_API_KEY` / `MOONSHOT_API_KEY` / `DASHSCOPE_API_KEY` / `GEMINI_API_KEY` / `OPENROUTER_API_KEY`;搜索可选 `TAVILY_API_KEY` 或 `BRAVE_API_KEY`(也可以直接在 GUI 设置 → 联网搜索里填);临时覆盖用 `BANANA_PROVIDER` / `BANANA_MODEL` / `BANANA_BASE_URL` / `BANANA_SHOW_REASONING` / `BANANA_HOME` / `BANANA_CONFIG`。
261
261
 
262
262
  `protocol` 只有两个值:`openai`(OpenAI 兼容)和 `anthropic`(Claude 原生)。绝大多数第三方服务都能用 `openai` 协议接进来。
263
263
 
@@ -323,7 +323,7 @@ npm publish
323
323
 
324
324
  - 模型名和 provider baseUrl 是内置默认值,不同服务可能需要 `/model` 或配置改一下。
325
325
  - 上下文压缩按字符数估算 token,超长会话建议调小 `maxContextChars`。
326
- - `web_search` 在没有 Tavily/Brave key 时走 DuckDuckGo HTML 抓取,无外网环境下会失败(会明确报错,不会假装成功)。
326
+ - `web_search` 在没有 Tavily/Brave key 时会同时抓 Bing 和 DuckDuckGo,合并去重后返回;两个源都不可用时明确报错,不会假装成功。开启「深度搜索」或传 `depth=deep` 会额外读取前几页正文。
327
327
  - 图片以 data URL 存进会话 JSON,多张大图会让会话文件变大(模型侧仍受单张 4MB 限制)。
328
328
  - `/undo` 只覆盖 `write_file` / `edit_file`;`shell` 命令和插件工具的副作用无法回滚。
329
329
  - 插件以当前进程权限运行,等同用户自己的代码,只应放可信脚本。
@@ -57,6 +57,7 @@ export async function runTurn(deps, messages, input, images, attachments, attach
57
57
  permissions: deps.permissions,
58
58
  todos,
59
59
  recordChange: deps.recordChange,
60
+ search: deps.search,
60
61
  };
61
62
  const tryCompact = async (aggressive) => {
62
63
  deps.onNotice?.(aggressive ? '上游报告上下文超限,正在压缩历史对话后重试…' : '上下文接近上限,正在压缩历史对话…');
package/dist/cli/repl.js CHANGED
@@ -364,6 +364,7 @@ export async function startRepl(options) {
364
364
  allowOutsideWorkspace: config.allowOutsideWorkspace,
365
365
  signal: controller.signal,
366
366
  todos: session.todos,
367
+ search: config.search,
367
368
  recordChange: (path, before) => recorder.record(path, before),
368
369
  onText: (chunk) => {
369
370
  clearStatus();
@@ -156,6 +156,12 @@ function defaultConfig(workspace) {
156
156
  allowOutsideWorkspace: false,
157
157
  showReasoning: true,
158
158
  disabledPlugins: [],
159
+ search: {
160
+ provider: 'auto',
161
+ deep: false,
162
+ deepPages: 3,
163
+ language: 'en',
164
+ },
159
165
  };
160
166
  }
161
167
  async function readConfigFile(path) {
@@ -177,9 +183,10 @@ function merge(base, patch) {
177
183
  }
178
184
  return {
179
185
  ...base,
180
- ...Object.fromEntries(Object.entries(patch).filter(([key]) => key !== 'providers' && key !== 'permissions')),
186
+ ...Object.fromEntries(Object.entries(patch).filter(([key]) => key !== 'providers' && key !== 'permissions' && key !== 'search')),
181
187
  providers,
182
188
  permissions: { ...base.permissions, ...(patch.permissions ?? {}) },
189
+ search: { ...base.search, ...(patch.search ?? {}) },
183
190
  };
184
191
  }
185
192
  function applyEnvOverrides(config) {
@@ -320,7 +320,7 @@ export async function startUiServer(options) {
320
320
  const provider = () => createProvider(config.current, config);
321
321
  const backups = (sessionId) => new BackupStore(join(bananaHome(), 'backups'), sessionId);
322
322
  const info = async (session) => ({
323
- version: '0.8.4',
323
+ version: '0.8.5',
324
324
  provider: { name: config.current, model: config.providers[config.current]?.model ?? '', protocol: config.providers[config.current]?.protocol ?? 'openai' },
325
325
  reasoning: {
326
326
  level: session.reasoning ?? 'standard',
@@ -342,6 +342,22 @@ export async function startUiServer(options) {
342
342
  permissions: config.permissions,
343
343
  systemPrompt: config.systemPrompt,
344
344
  showReasoning: config.showReasoning,
345
+ search: {
346
+ provider: config.search?.provider ?? 'auto',
347
+ deep: Boolean(config.search?.deep),
348
+ deepPages: Math.min(Math.max(Number(config.search?.deepPages) || 3, 1), 5),
349
+ language: config.search?.language ?? 'en',
350
+ tavily: {
351
+ saved: Boolean(config.search?.tavilyKey),
352
+ ready: Boolean(config.search?.tavilyKey || process.env['TAVILY_API_KEY'] || process.env['BANANA_TAVILY_KEY']),
353
+ env: 'TAVILY_API_KEY',
354
+ },
355
+ brave: {
356
+ saved: Boolean(config.search?.braveKey),
357
+ ready: Boolean(config.search?.braveKey || process.env['BRAVE_API_KEY'] || process.env['BANANA_BRAVE_KEY']),
358
+ env: 'BRAVE_API_KEY',
359
+ },
360
+ },
345
361
  disabledPlugins: config.disabledPlugins,
346
362
  tools: options.tools.map((tool) => ({ name: tool.name, approval: tool.approval, permission: permissionFor(config.permissions, tool) })),
347
363
  session: {
@@ -826,6 +842,36 @@ export async function startUiServer(options) {
826
842
  else if (body.action === 'showReasoning') {
827
843
  config.showReasoning = body.value === 'true' || body.value === '1';
828
844
  }
845
+ else if (body.action === 'searchProvider') {
846
+ const value = String(body.value ?? '');
847
+ if (value === 'auto' || value === 'tavily' || value === 'brave' || value === 'free') {
848
+ config.search.provider = value;
849
+ }
850
+ }
851
+ else if (body.action === 'searchKey') {
852
+ const name = String(body.name ?? '');
853
+ const value = String(body.value ?? '').trim();
854
+ if (name === 'tavily') {
855
+ if (value)
856
+ config.search.tavilyKey = value;
857
+ else
858
+ delete config.search.tavilyKey;
859
+ }
860
+ else if (name === 'brave') {
861
+ if (value)
862
+ config.search.braveKey = value;
863
+ else
864
+ delete config.search.braveKey;
865
+ }
866
+ }
867
+ else if (body.action === 'searchDeep') {
868
+ config.search.deep = body.value === 'true' || body.value === '1';
869
+ }
870
+ else if (body.action === 'searchLanguage') {
871
+ const value = String(body.value ?? '');
872
+ if (value === 'en' || value === 'auto' || value === 'zh')
873
+ config.search.language = value;
874
+ }
829
875
  else if (body.action === 'workspace' && body.value) {
830
876
  const next = resolve(String(body.value).trim());
831
877
  if (!existsSync(next)) {
@@ -1279,6 +1325,7 @@ export async function startUiServer(options) {
1279
1325
  allowOutsideWorkspace: config.allowOutsideWorkspace,
1280
1326
  permissions: config.permissions,
1281
1327
  todos: session.todos,
1328
+ search: config.search,
1282
1329
  };
1283
1330
  const started = Date.now();
1284
1331
  const toolStartedAt = new Map();
@@ -1296,6 +1343,7 @@ export async function startUiServer(options) {
1296
1343
  signal: controller.signal,
1297
1344
  todos: session.todos,
1298
1345
  reasoning: session.reasoning ?? 'standard',
1346
+ search: config.search,
1299
1347
  recordChange: (path, beforeContent) => recorder.record(path, beforeContent),
1300
1348
  onText: (chunk) => send('text', { chunk }),
1301
1349
  onReasoning: (chunk) => send('reasoning', { chunk }),
package/dist/tools/web.js CHANGED
@@ -1,24 +1,31 @@
1
1
  import { fail, ok } from "./types.js";
2
2
  const DEFAULT_TIMEOUT_MS = 30_000;
3
+ const SEARCH_TIMEOUT_MS = 20_000;
4
+ const DEEP_TIMEOUT_MS = 15_000;
3
5
  const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
4
- async function fetchText(url, init, ctx) {
6
+ /** 统一带超时的 fetch:外部取消、单次请求超时都能及时停下。 */
7
+ async function fetchWithTimeout(url, init, ctx, timeoutMs = DEFAULT_TIMEOUT_MS) {
5
8
  const controller = new AbortController();
6
- const timeoutMs = init.timeoutMs ?? DEFAULT_TIMEOUT_MS;
7
9
  const timer = setTimeout(() => controller.abort(), timeoutMs);
8
10
  const onAbort = () => controller.abort();
11
+ if (ctx.signal.aborted)
12
+ controller.abort();
9
13
  ctx.signal.addEventListener('abort', onAbort, { once: true });
10
14
  try {
11
- const response = await fetch(url, { ...init, signal: controller.signal });
12
- if (!response.ok) {
13
- throw new Error(`HTTP ${response.status} ${response.statusText}`);
14
- }
15
- return await response.text();
15
+ return await fetch(url, { ...init, signal: controller.signal });
16
16
  }
17
17
  finally {
18
18
  clearTimeout(timer);
19
19
  ctx.signal.removeEventListener('abort', onAbort);
20
20
  }
21
21
  }
22
+ async function fetchText(url, init, ctx) {
23
+ const response = await fetchWithTimeout(url, init, ctx, init.timeoutMs ?? DEFAULT_TIMEOUT_MS);
24
+ if (!response.ok) {
25
+ throw new Error(`HTTP ${response.status} ${response.statusText}`);
26
+ }
27
+ return await response.text();
28
+ }
22
29
  function decodeEntities(text) {
23
30
  return text
24
31
  .replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)))
@@ -47,22 +54,84 @@ function stripTags(html) {
47
54
  .replace(/\n\s*\n\s*\n+/g, '\n\n')
48
55
  .trim();
49
56
  }
57
+ /** 把网页正文转成可读文本:保留段落,去掉导航、页脚、脚本等噪声。 */
58
+ export function extractMainText(html) {
59
+ const withoutNoise = html
60
+ .replace(/<!--[\s\S]*?-->/g, ' ')
61
+ .replace(/<(script|style|noscript|svg|canvas|template|iframe|nav|footer|header|aside|form)[^>]*>[\s\S]*?<\/\1>/gi, ' ')
62
+ .replace(/<br\s*\/?>/gi, '\n')
63
+ .replace(/<\/(p|div|li|h[1-6]|tr|section|article|main|blockquote|pre)>/gi, '\n')
64
+ .replace(/<[^>]+>/g, ' ');
65
+ // 优先只取 article/main,避免整页导航把正文淹没;太短就退回全文。
66
+ const article = /<article[^>]*>[\s\S]*?<\/article>/i.exec(html)?.[0] ?? /<main[^>]*>[\s\S]*?<\/main>/i.exec(html)?.[0];
67
+ const candidate = article && stripTags(article).length >= 300
68
+ ? article
69
+ .replace(/<!--[\s\S]*?-->/g, ' ')
70
+ .replace(/<(script|style|noscript|svg|canvas|template|iframe|nav|footer|header|aside|form)[^>]*>[\s\S]*?<\/\1>/gi, ' ')
71
+ .replace(/<br\s*\/?>/gi, '\n')
72
+ .replace(/<\/(p|div|li|h[1-6]|section|article|blockquote|pre)>/gi, '\n')
73
+ .replace(/<[^>]+>/g, ' ')
74
+ : withoutNoise;
75
+ const lines = decodeEntities(candidate)
76
+ .replace(/[ \t\u00a0]+/g, ' ')
77
+ .split('\n')
78
+ .map((line) => line.trim())
79
+ .filter((line) => line.length > 1);
80
+ // 连续重复的行(菜单、面包屑)只留一次。
81
+ const out = [];
82
+ for (const line of lines) {
83
+ if (out[out.length - 1] === line)
84
+ continue;
85
+ out.push(line);
86
+ }
87
+ return out.join('\n').replace(/\n{3,}/g, '\n\n').trim();
88
+ }
50
89
  function unwrapDuckDuckGo(href) {
90
+ if (href.startsWith('//'))
91
+ href = `https:${href}`;
51
92
  const match = /[?&]uddg=([^&]+)/.exec(href);
52
93
  if (!match)
53
94
  return href;
54
- return decodeURIComponent(match[1]);
95
+ try {
96
+ return decodeURIComponent(match[1]);
97
+ }
98
+ catch {
99
+ return href;
100
+ }
101
+ }
102
+ /** Tavily 支持时间范围;免费源不支持时就算了,不让它影响主流程。 */
103
+ function tavilyTimeRange(freshness) {
104
+ return freshness;
105
+ }
106
+ function braveFreshness(freshness) {
107
+ return freshness === 'day' ? 'pd' : freshness === 'week' ? 'pw' : freshness === 'month' ? 'pm' : freshness === 'year' ? 'py' : undefined;
55
108
  }
56
- async function searchTavily(query, count, ctx) {
57
- const apiKey = process.env['TAVILY_API_KEY'] ?? process.env['BANANA_TAVILY_KEY'];
58
- if (!apiKey)
59
- return null;
60
- const response = await fetch('https://api.tavily.com/search', {
109
+ function languageHeader(language) {
110
+ return language === 'zh' ? 'zh-CN,zh;q=0.9,en;q=0.7' : 'en-US,en;q=0.9,zh-CN;q=0.5';
111
+ }
112
+ function scopedQuery(query, site) {
113
+ const host = site?.trim().replace(/^https?:\/\//i, '').replace(/\/.*$/, '');
114
+ if (!host || !/^[a-z0-9.-]+\.[a-z]{2,}$/i.test(host))
115
+ return query;
116
+ return `${query} site:${host}`;
117
+ }
118
+ async function searchTavily(query, count, ctx, apiKey, options) {
119
+ const body = {
120
+ api_key: apiKey,
121
+ query: scopedQuery(query, options.site),
122
+ max_results: count,
123
+ search_depth: options.deep ? 'advanced' : 'basic',
124
+ include_answer: false,
125
+ include_raw_content: false,
126
+ };
127
+ const timeRange = tavilyTimeRange(options.freshness);
128
+ if (timeRange)
129
+ body.time_range = timeRange;
130
+ const response = await fetchWithTimeout('https://api.tavily.com/search', {
61
131
  method: 'POST',
62
132
  headers: { 'content-type': 'application/json' },
63
- body: JSON.stringify({ api_key: apiKey, query, max_results: count, search_depth: 'basic' }),
64
- signal: ctx.signal,
65
- });
133
+ body: JSON.stringify(body),
134
+ }, ctx, SEARCH_TIMEOUT_MS);
66
135
  if (!response.ok)
67
136
  throw new Error(`Tavily HTTP ${response.status}`);
68
137
  const payload = (await response.json());
@@ -72,15 +141,23 @@ async function searchTavily(query, count, ctx) {
72
141
  snippet: (item.content ?? '').slice(0, 500),
73
142
  }));
74
143
  }
75
- async function searchBrave(query, count, ctx) {
76
- const apiKey = process.env['BRAVE_API_KEY'] ?? process.env['BANANA_BRAVE_KEY'];
77
- if (!apiKey)
78
- return null;
79
- const url = `https://api.search.brave.com/res/v1/web/search?q=${encodeURIComponent(query)}&count=${count}`;
80
- const response = await fetch(url, {
81
- headers: { 'x-subscription-token': apiKey, accept: 'application/json' },
82
- signal: ctx.signal,
144
+ async function searchBrave(query, count, ctx, apiKey, options) {
145
+ const params = new URLSearchParams({
146
+ q: scopedQuery(query, options.site),
147
+ count: String(Math.min(Math.max(count, 1), 20)),
148
+ search_lang: options.language === 'zh' ? 'zh-hans' : 'en',
149
+ ui_lang: options.language === 'zh' ? 'zh-CN' : 'en-US',
83
150
  });
151
+ const freshness = braveFreshness(options.freshness);
152
+ if (freshness)
153
+ params.set('freshness', freshness);
154
+ const response = await fetchWithTimeout(`https://api.search.brave.com/res/v1/web/search?${params.toString()}`, {
155
+ headers: {
156
+ 'x-subscription-token': apiKey,
157
+ accept: 'application/json',
158
+ 'accept-language': languageHeader(options.language),
159
+ },
160
+ }, ctx, SEARCH_TIMEOUT_MS);
84
161
  if (!response.ok)
85
162
  throw new Error(`Brave HTTP ${response.status}`);
86
163
  const payload = (await response.json());
@@ -90,14 +167,20 @@ async function searchBrave(query, count, ctx) {
90
167
  snippet: stripTags(item.description ?? '').slice(0, 500),
91
168
  }));
92
169
  }
93
- async function searchBing(query, count, ctx) {
94
- const html = await fetchText(`https://www.bing.com/search?q=${encodeURIComponent(query)}&count=${count}`, {
170
+ async function searchBing(query, count, ctx, options) {
171
+ const params = new URLSearchParams({
172
+ q: scopedQuery(query, options.site),
173
+ count: String(Math.min(Math.max(count, 1), 20)),
174
+ setlang: options.language === 'zh' ? 'zh-hans' : 'en',
175
+ cc: options.language === 'zh' ? 'cn' : 'us',
176
+ });
177
+ const html = await fetchText(`https://www.bing.com/search?${params.toString()}`, {
95
178
  headers: {
96
179
  'user-agent': USER_AGENT,
97
180
  accept: 'text/html,application/xhtml+xml',
98
- 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
181
+ 'accept-language': languageHeader(options.language),
99
182
  },
100
- timeoutMs: 20_000,
183
+ timeoutMs: SEARCH_TIMEOUT_MS,
101
184
  }, ctx);
102
185
  return parseBing(html, count);
103
186
  }
@@ -120,7 +203,7 @@ export function unwrapBing(href) {
120
203
  }
121
204
  export function parseBing(html, count) {
122
205
  const hits = [];
123
- for (const block of html.matchAll(/<li class="b_algo"[\s\S]*?<\/li>/gi)) {
206
+ for (const block of html.matchAll(/<li[^>]*class="[^"]*\bb_algo\b[^"]*"[\s\S]*?<\/li>/gi)) {
124
207
  const item = block[0];
125
208
  const link = item.match(/<h2[^>]*>\s*<a[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/i);
126
209
  if (!link)
@@ -139,15 +222,36 @@ export function parseBing(html, count) {
139
222
  }
140
223
  return hits;
141
224
  }
142
- async function searchDuckDuckGo(query, count, ctx) {
225
+ export function parseDuckDuckGo(html, count) {
226
+ const hits = [];
227
+ const linkRegex = /<a[^>]*class="[^"]*(?:result__a|result-link)[^"]*"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/gi;
228
+ const snippetRegex = /<(?:a|td)[^>]*class="[^"]*(?:result__snippet|result-snippet)[^"]*"[^>]*>([\s\S]*?)<\/(?:a|td)>/gi;
229
+ const snippets = [...html.matchAll(snippetRegex)].map((match) => stripTags(match[1] ?? ''));
230
+ for (const match of html.matchAll(linkRegex)) {
231
+ const href = decodeEntities(match[1] ?? '');
232
+ const title = stripTags(match[2] ?? '');
233
+ const url = unwrapDuckDuckGo(href);
234
+ if (!/^https?:/i.test(url))
235
+ continue;
236
+ hits.push({ title, url, snippet: snippets[hits.length] ?? '' });
237
+ if (hits.length >= count)
238
+ break;
239
+ }
240
+ return hits;
241
+ }
242
+ async function searchDuckDuckGo(query, count, ctx, options) {
143
243
  const errors = [];
244
+ const scoped = scopedQuery(query, options.site);
144
245
  const endpoints = [
145
- `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`,
146
- `https://lite.duckduckgo.com/lite/?q=${encodeURIComponent(query)}`,
246
+ `https://html.duckduckgo.com/html/?q=${encodeURIComponent(scoped)}`,
247
+ `https://lite.duckduckgo.com/lite/?q=${encodeURIComponent(scoped)}`,
147
248
  ];
148
249
  for (const endpoint of endpoints) {
149
250
  try {
150
- const html = await fetchText(endpoint, { headers: { 'user-agent': USER_AGENT, accept: 'text/html' }, timeoutMs: 20_000 }, ctx);
251
+ const html = await fetchText(endpoint, {
252
+ headers: { 'user-agent': USER_AGENT, accept: 'text/html', 'accept-language': languageHeader(options.language) },
253
+ timeoutMs: SEARCH_TIMEOUT_MS,
254
+ }, ctx);
151
255
  const hits = parseDuckDuckGo(html, count);
152
256
  if (hits.length)
153
257
  return hits;
@@ -157,24 +261,7 @@ async function searchDuckDuckGo(query, count, ctx) {
157
261
  errors.push(`${new URL(endpoint).hostname}: ${error.message}`);
158
262
  }
159
263
  }
160
- throw new Error(`DuckDuckGo 搜索失败 (${errors.join('; ')})。可设置 TAVILY_API_KEY 或 BRAVE_API_KEY 获得更稳定的结果。`);
161
- }
162
- function parseDuckDuckGo(html, count) {
163
- const hits = [];
164
- const linkRegex = /<a[^>]*class="[^"]*result__a[^"]*"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/gi;
165
- const snippetRegex = /<a[^>]*class="[^"]*result__snippet[^"]*"[^>]*>([\s\S]*?)<\/a>/gi;
166
- const snippets = [...html.matchAll(snippetRegex)].map((match) => stripTags(match[1] ?? ''));
167
- for (const match of html.matchAll(linkRegex)) {
168
- const href = match[1] ?? '';
169
- const title = stripTags(match[2] ?? '');
170
- const url = unwrapDuckDuckGo(href);
171
- if (!url.startsWith('http'))
172
- continue;
173
- hits.push({ title, url, snippet: snippets[hits.length] ?? '' });
174
- if (hits.length >= count)
175
- break;
176
- }
177
- return hits;
264
+ throw new Error(`DuckDuckGo 搜索失败 (${errors.join('; ')})`);
178
265
  }
179
266
  /** 归一化 URL:忽略大小写、www. 前缀、末尾斜杠和 #hash,用来去重。 */
180
267
  export function normalizeUrl(href) {
@@ -213,11 +300,158 @@ export function sanitizeHits(hits, count) {
213
300
  }
214
301
  return out;
215
302
  }
303
+ const TECH_HOSTS = [
304
+ /(^|\.)github\.com$/i,
305
+ /(^|\.)github\.io$/i,
306
+ /(^|\.)stackoverflow\.com$/i,
307
+ /(^|\.)stackexchange\.com$/i,
308
+ /(^|\.)developer\.mozilla\.org$/i,
309
+ /(^|\.)npmjs\.com$/i,
310
+ /(^|\.)pypi\.org$/i,
311
+ /(^|\.)readthedocs\.io$/i,
312
+ /(^|\.)rust-lang\.org$/i,
313
+ /(^|\.)python\.org$/i,
314
+ /(^|\.)nodejs\.org$/i,
315
+ /(^|\.)go\.dev$/i,
316
+ /(^|\.)w3\.org$/i,
317
+ /(^|\.)ietf\.org$/i,
318
+ /docs?\./i,
319
+ ];
320
+ export function isTechSource(href) {
321
+ try {
322
+ const host = new URL(href).hostname;
323
+ return TECH_HOSTS.some((pattern) => pattern.test(host));
324
+ }
325
+ catch {
326
+ return false;
327
+ }
328
+ }
329
+ /**
330
+ * 合并多个搜索引擎的结果:相同链接只保留一次,按“在两个引擎里都靠前”排序。
331
+ * 技术/官方文档来源给一个很小的加分,只在分数接近时起作用。
332
+ */
333
+ export function mergeSearchHits(lists, count) {
334
+ const merged = new Map();
335
+ let order = 0;
336
+ lists.forEach((list) => {
337
+ list.forEach((hit, rank) => {
338
+ if (!/^https?:\/\//i.test(hit.url ?? ''))
339
+ return;
340
+ const key = normalizeUrl(hit.url);
341
+ const base = 1 / (rank + 1);
342
+ const score = base + (isTechSource(hit.url) ? 0.015 : 0);
343
+ const previous = merged.get(key);
344
+ if (!previous) {
345
+ merged.set(key, { hit: { ...hit }, score, order: order++ });
346
+ }
347
+ else {
348
+ previous.score += base;
349
+ if ((hit.snippet?.length ?? 0) > (previous.hit.snippet?.length ?? 0)) {
350
+ previous.hit.snippet = hit.snippet;
351
+ }
352
+ if (!previous.hit.title && hit.title)
353
+ previous.hit.title = hit.title;
354
+ }
355
+ });
356
+ });
357
+ return [...merged.values()]
358
+ .sort((a, b) => b.score - a.score || a.order - b.order)
359
+ .slice(0, count)
360
+ .map((item) => item.hit);
361
+ }
362
+ async function searchFree(query, count, ctx, options) {
363
+ const settled = await Promise.allSettled([
364
+ searchBing(query, count, ctx, options),
365
+ searchDuckDuckGo(query, count, ctx, options),
366
+ ]);
367
+ const results = [];
368
+ const problems = [];
369
+ for (const [index, item] of settled.entries()) {
370
+ const engine = index === 0 ? 'Bing' : 'DuckDuckGo';
371
+ if (item.status === 'fulfilled' && item.value.length) {
372
+ results.push({ engine, hits: item.value });
373
+ }
374
+ else if (item.status === 'rejected') {
375
+ problems.push(`${engine}: ${item.reason?.message ?? item.reason}`);
376
+ }
377
+ else {
378
+ problems.push(`${engine}: 没有结果`);
379
+ }
380
+ }
381
+ if (!results.length) {
382
+ throw new Error(`免费搜索源都不可用 (${problems.join('; ')})`);
383
+ }
384
+ if (problems.length)
385
+ ctx.log(`部分搜索源不可用: ${problems.join('; ')}`);
386
+ return results;
387
+ }
388
+ async function fetchPageExcerpt(hit, maxChars, ctx) {
389
+ const response = await fetchWithTimeout(hit.url, {
390
+ headers: {
391
+ 'user-agent': USER_AGENT,
392
+ accept: 'text/html,application/xhtml+xml,text/plain,application/json;q=0.9,*/*;q=0.8',
393
+ 'accept-language': 'en-US,en;q=0.8,zh-CN;q=0.5',
394
+ },
395
+ redirect: 'follow',
396
+ }, ctx, DEEP_TIMEOUT_MS);
397
+ if (!response.ok)
398
+ throw new Error(`HTTP ${response.status}`);
399
+ const contentType = response.headers.get('content-type') ?? '';
400
+ const length = Number(response.headers.get('content-length') ?? 0);
401
+ if (length > 8_000_000)
402
+ throw new Error('页面太大,跳过正文抓取');
403
+ if (contentType && !/html|text|json|xml/i.test(contentType)) {
404
+ throw new Error(`不支持的内容类型 ${contentType.split(';')[0]}`);
405
+ }
406
+ const body = await response.text();
407
+ const text = /html/i.test(contentType) || /^\s*<(?:!doctype|html)/i.test(body.slice(0, 200))
408
+ ? extractMainText(body)
409
+ : body.trim();
410
+ if (!text)
411
+ throw new Error('正文为空');
412
+ return text.length > maxChars ? `${text.slice(0, maxChars)}\n...[正文已截断,原长 ${text.length} 字符]` : text;
413
+ }
414
+ async function attachDeepContent(hits, search, ctx) {
415
+ const pages = Math.min(Math.max(Number(search.deepPages) || 3, 1), 5);
416
+ const selected = hits.slice(0, pages);
417
+ const settled = await Promise.allSettled(selected.map((hit) => fetchPageExcerpt(hit, 3500, ctx)));
418
+ const excerpts = [];
419
+ let failed = 0;
420
+ for (const item of settled) {
421
+ if (item.status === 'fulfilled')
422
+ excerpts.push(item.value);
423
+ else {
424
+ failed += 1;
425
+ excerpts.push('');
426
+ }
427
+ }
428
+ return { hits, excerpts, failed };
429
+ }
430
+ function pickSettings(ctx) {
431
+ return (ctx.search ?? {
432
+ provider: 'auto',
433
+ deep: false,
434
+ deepPages: 3,
435
+ language: 'en',
436
+ });
437
+ }
438
+ function searchKey(configured, envNames) {
439
+ const own = configured?.trim();
440
+ if (own)
441
+ return own;
442
+ for (const name of envNames) {
443
+ const value = process.env[name]?.trim();
444
+ if (value)
445
+ return value;
446
+ }
447
+ return undefined;
448
+ }
216
449
  export const webSearchTool = {
217
450
  name: 'web_search',
218
451
  description: [
219
- '联网搜索,返回标题 + 链接 + 摘要。',
220
- '优先使用 TAVILY_API_KEY / BRAVE_API_KEY;没有配置时自动回退到 Bing、DuckDuckGo。',
452
+ '联网搜索,返回标题、链接和摘要。',
453
+ '默认优先英文/技术源,并同时合并 Bing 与 DuckDuckGo 的结果;配置 TAVILY_API_KEY / BRAVE_API_KEY 会自动升级到搜索 API。',
454
+ 'depth=deep 时会自动读取前几页正文,适合查文档、对比方案;日常事实核对用默认的 quick 即可。',
221
455
  '搜不到就换关键词再试,别靠猜。',
222
456
  ].join(''),
223
457
  parameters: {
@@ -225,6 +459,22 @@ export const webSearchTool = {
225
459
  properties: {
226
460
  query: { type: 'string', description: '搜索关键词' },
227
461
  count: { type: 'number', description: '返回结果数量,默认 5,最大 20' },
462
+ site: { type: 'string', description: '只搜某个站点,例如 github.com 或 docs.python.org' },
463
+ freshness: {
464
+ type: 'string',
465
+ enum: ['day', 'week', 'month', 'year'],
466
+ description: '只看最近一段时间的结果,可选',
467
+ },
468
+ depth: {
469
+ type: 'string',
470
+ enum: ['quick', 'deep'],
471
+ description: 'deep 会抓取前几页正文;不传则跟随设置里的深度搜索开关',
472
+ },
473
+ language: {
474
+ type: 'string',
475
+ enum: ['en', 'zh', 'auto'],
476
+ description: '结果语言偏好,默认跟随设置(英文/技术优先)',
477
+ },
228
478
  },
229
479
  required: ['query'],
230
480
  additionalProperties: false,
@@ -235,36 +485,98 @@ export const webSearchTool = {
235
485
  if (!query)
236
486
  return fail('query 不能为空。');
237
487
  const count = Math.min(Math.max(Number(input.count) || 5, 1), 20);
488
+ const settings = pickSettings(ctx);
489
+ const languageSetting = input.language === 'zh' || input.language === 'en' || input.language === 'auto'
490
+ ? input.language
491
+ : settings.language;
492
+ const language = languageSetting === 'auto'
493
+ ? /[\u4e00-\u9fff]/.test(query) ? 'zh' : 'en'
494
+ : languageSetting === 'zh' ? 'zh' : 'en';
495
+ const options = {
496
+ freshness: ['day', 'week', 'month', 'year'].includes(String(input.freshness)) ? input.freshness : undefined,
497
+ site: typeof input.site === 'string' ? input.site : undefined,
498
+ language,
499
+ deep: input.depth === 'deep' || (input.depth !== 'quick' && Boolean(settings.deep)),
500
+ };
501
+ const tavilyKey = searchKey(settings.tavilyKey, ['TAVILY_API_KEY', 'BANANA_TAVILY_KEY']);
502
+ const braveKey = searchKey(settings.braveKey, ['BRAVE_API_KEY', 'BANANA_BRAVE_KEY']);
503
+ const provider = settings.provider ?? 'auto';
504
+ if (provider === 'tavily' && !tavilyKey) {
505
+ return fail('已选择 Tavily,但没有配置 Key。请在设置 → 联网搜索里填写,或设置 TAVILY_API_KEY。');
506
+ }
507
+ if (provider === 'brave' && !braveKey) {
508
+ return fail('已选择 Brave,但没有配置 Key。请在设置 → 联网搜索里填写,或设置 BRAVE_API_KEY。');
509
+ }
238
510
  try {
239
- let hits = null;
240
- const order = [
241
- () => searchTavily(query, count, ctx),
242
- () => searchBrave(query, count, ctx),
243
- () => searchBing(query, count, ctx),
244
- ];
511
+ const engines = [];
245
512
  const problems = [];
246
- for (const attempt of order) {
513
+ const tryApi = async (name) => {
247
514
  try {
248
- hits = await attempt();
249
- if (hits && hits.length)
250
- break;
251
- hits = null;
515
+ const hits = name === 'Tavily'
516
+ ? await searchTavily(query, count, ctx, tavilyKey, options)
517
+ : await searchBrave(query, count, ctx, braveKey, options);
518
+ if (hits.length)
519
+ engines.push({ engine: name, hits });
520
+ else
521
+ problems.push(`${name}: 没有结果`);
522
+ }
523
+ catch (error) {
524
+ problems.push(`${name}: ${error.message}`);
525
+ }
526
+ };
527
+ if (provider === 'tavily' || (provider === 'auto' && tavilyKey))
528
+ await tryApi('Tavily');
529
+ if (!engines.length && (provider === 'brave' || (provider === 'auto' && braveKey)))
530
+ await tryApi('Brave');
531
+ if (!engines.length && (provider === 'free' || provider === 'auto')) {
532
+ try {
533
+ engines.push(...(await searchFree(query, count, ctx, options)));
252
534
  }
253
535
  catch (error) {
254
536
  problems.push(error.message);
255
537
  }
256
538
  }
257
- if (!hits)
258
- hits = await searchDuckDuckGo(query, count, ctx);
539
+ if (!engines.length) {
540
+ return fail(`搜索失败: ${problems.join('; ') || '没有可用的搜索源'}`);
541
+ }
259
542
  if (problems.length)
260
- ctx.log(`搜索 API 降级: ${problems.join('; ')}`);
261
- hits = sanitizeHits(hits, count);
543
+ ctx.log(`搜索降级: ${problems.join('; ')}`);
544
+ let hits = sanitizeHits(mergeSearchHits(engines.map((item) => item.hits), count), count);
545
+ // 关键词可能太窄:去掉引号之类的修饰再试一次,只限免费源。
546
+ if (!hits.length && provider !== 'tavily' && provider !== 'brave') {
547
+ const simplified = query.replace(/["'“”‘’]/g, ' ').replace(/\s+/g, ' ').trim();
548
+ if (simplified && simplified !== query) {
549
+ try {
550
+ const retry = await searchFree(simplified, count, ctx, options);
551
+ hits = sanitizeHits(mergeSearchHits(retry.map((item) => item.hits), count), count);
552
+ }
553
+ catch {
554
+ /* 第一次的失败信息已经足够 */
555
+ }
556
+ }
557
+ }
262
558
  if (!hits.length)
263
559
  return fail(`没有找到 "${query}" 的结果,换个关键词试试。`);
560
+ let excerpts = [];
561
+ let failedPages = 0;
562
+ if (options.deep) {
563
+ const deep = await attachDeepContent(hits, settings, ctx);
564
+ excerpts = deep.excerpts;
565
+ failedPages = deep.failed;
566
+ }
567
+ const engineNames = [...new Set(engines.map((item) => item.engine))].join(' + ');
264
568
  const body = hits
265
- .map((hit, index) => `[${index + 1}] ${hit.title}\n ${hit.url}\n ${hit.snippet}`)
569
+ .map((hit, index) => {
570
+ const lines = [`[${index + 1}] ${hit.title}`, ` ${hit.url}`, ` ${hit.snippet}`];
571
+ const excerpt = excerpts[index];
572
+ if (excerpt)
573
+ lines.push(` --- 正文摘录 ---\n${excerpt.replace(/^/gm, ' ')}`);
574
+ return lines.join('\n');
575
+ })
266
576
  .join('\n');
267
- return ok(`搜索 "${query}" 的结果:\n${body}`, `web_search: ${hits.length} 条结果`);
577
+ const mode = options.deep ? ` · 深度 ${hits.slice(0, Math.min(hits.length, Math.max(1, Number(settings.deepPages) || 3))).length} 页${failedPages ? `(${failedPages} 页失败)` : ''}` : '';
578
+ const footer = options.deep && failedPages ? `\n\n注:有 ${failedPages} 个页面正文没取到,以摘要为准。` : '';
579
+ return ok(`搜索 "${query}" 的结果(${engineNames}${language === 'zh' ? ' · 中文优先' : ' · 英文/技术优先'}${mode}):\n${body}${footer}`, `web_search: ${hits.length} 条结果${options.deep ? ' · 深度搜索' : ''}`);
268
580
  }
269
581
  catch (error) {
270
582
  return fail(`搜索失败: ${error.message}`);
@@ -273,7 +585,7 @@ export const webSearchTool = {
273
585
  };
274
586
  export const webFetchTool = {
275
587
  name: 'web_fetch',
276
- description: '抓取一个 URL 的内容并转成纯文本(HTML 会去掉标签和脚本)。适合读文档、issue、API 说明。',
588
+ description: '抓取一个 URL 的内容并转成纯文本(HTML 会去导航、脚本,保留正文段落)。适合读文档、issue、API 说明。',
277
589
  parameters: {
278
590
  type: 'object',
279
591
  properties: {
@@ -297,16 +609,24 @@ export const webFetchTool = {
297
609
  }
298
610
  const maxChars = Math.min(Math.max(Number(input.max_chars) || 20_000, 200), 200_000);
299
611
  try {
300
- const response = await fetch(url, {
301
- headers: { 'user-agent': USER_AGENT, accept: 'text/html,application/json,text/plain,*/*' },
612
+ const response = await fetchWithTimeout(url, {
613
+ headers: {
614
+ 'user-agent': USER_AGENT,
615
+ accept: 'text/html,application/xhtml+xml,application/json,text/plain,*/*',
616
+ 'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.5',
617
+ },
302
618
  redirect: 'follow',
303
- signal: ctx.signal,
304
- });
619
+ }, ctx);
305
620
  if (!response.ok)
306
621
  return fail(`HTTP ${response.status} ${response.statusText} (${url.href})`);
307
622
  const contentType = response.headers.get('content-type') ?? '';
623
+ const length = Number(response.headers.get('content-length') ?? 0);
624
+ if (length > 12_000_000)
625
+ return fail(`页面太大(${Math.round(length / 1_000_000)} MB),不读取。`);
308
626
  const body = await response.text();
309
- const text = contentType.includes('html') ? stripTags(body) : body;
627
+ const text = /html/i.test(contentType) || /^\s*<(?:!doctype|html)/i.test(body.slice(0, 200))
628
+ ? extractMainText(body)
629
+ : body;
310
630
  const clipped = text.length > maxChars ? `${text.slice(0, maxChars)}\n...[内容被截断,共 ${text.length} 字符]` : text;
311
631
  const header = `# ${url.href}\n# content-type: ${contentType || 'unknown'} | ${body.length} 字节\n`;
312
632
  return ok(`${header}\n${clipped || '(空内容)'}`, `fetch ${url.host}`);
package/dist/ui/app.js CHANGED
@@ -2644,6 +2644,7 @@ async function attach(file) {
2644
2644
  // ---------------- 控制中心 ----------------
2645
2645
  const SETTINGS_NAV = [
2646
2646
  { id: 'model', label: '模型', icon: 'chip' },
2647
+ { id: 'search', label: '联网搜索', icon: 'globe' },
2647
2648
  { id: 'appearance', label: '外观', icon: 'sun' },
2648
2649
  { id: 'terminal', label: '终端', icon: 'terminal' },
2649
2650
  { id: 'workspace', label: '工作区', icon: 'folder' },
@@ -2939,6 +2940,7 @@ function renderSettings({ resetScroll = false } = {}) {
2939
2940
  body.innerHTML = '';
2940
2941
  const category = state.settingsCategory;
2941
2942
  if (category === 'model') renderModelSettings(body);
2943
+ else if (category === 'search') renderSearchSettings(body);
2942
2944
  else if (category === 'appearance') renderAppearanceSettings(body);
2943
2945
  else if (category === 'terminal') renderTerminalSettings(body);
2944
2946
  else if (category === 'workspace') renderWorkspaceSettings(body);
@@ -3043,6 +3045,97 @@ function renderModelSettings(body) {
3043
3045
  body.appendChild(section);
3044
3046
  }
3045
3047
 
3048
+ function renderSearchSettings(body) {
3049
+ const info = state.info;
3050
+ const search = info?.search ?? {
3051
+ provider: 'auto',
3052
+ deep: false,
3053
+ language: 'en',
3054
+ tavily: { saved: false, ready: false, env: 'TAVILY_API_KEY' },
3055
+ brave: { saved: false, ready: false, env: 'BRAVE_API_KEY' },
3056
+ };
3057
+ const section = settingsSection('联网搜索', '免费源开箱即用;填入 Key 后自动升级到更稳定的搜索 API。');
3058
+
3059
+ const card = settingsCard();
3060
+ const providerSelect = document.createElement('select');
3061
+ providerSelect.innerHTML = [
3062
+ ['auto', '自动(有 Key 用 API,否则免费源)'],
3063
+ ['tavily', 'Tavily'],
3064
+ ['brave', 'Brave'],
3065
+ ['free', '只用免费源'],
3066
+ ]
3067
+ .map(([value, label]) => `<option value="${value}" ${search.provider === value ? 'selected' : ''}>${label}</option>`)
3068
+ .join('');
3069
+ providerSelect.addEventListener('change', async () => {
3070
+ await saveSetting('searchProvider', { value: providerSelect.value });
3071
+ await refresh(false);
3072
+ renderSettings();
3073
+ toast('搜索方式已更新');
3074
+ });
3075
+ card.appendChild(
3076
+ settingsRow('搜索方式', '自动模式优先用已配置的 Key;API 失败时会回退到免费源。', providerSelect),
3077
+ );
3078
+
3079
+ const keyRow = (name, label, spec) => {
3080
+ const input = settingsInput('', spec.saved ? '已保存,留空表示不修改' : `粘贴 ${label} API Key`, 'password');
3081
+ const save = settingsButton('保存', async () => {
3082
+ const value = input.value.trim();
3083
+ if (!value) return toast('留空表示不修改');
3084
+ await saveSetting('searchKey', { name, value });
3085
+ input.value = '';
3086
+ await refresh(false);
3087
+ renderSettings();
3088
+ toast(label + ' Key 已保存');
3089
+ }, true);
3090
+ input.addEventListener('keydown', (event) => {
3091
+ if (event.key === 'Enter') save.click();
3092
+ });
3093
+ const box = document.createElement('div');
3094
+ box.style.display = 'flex';
3095
+ box.style.gap = '6px';
3096
+ box.append(input, save);
3097
+ const hint = `也可以只设置环境变量 ${spec.env},当前${spec.ready ? '已就绪' : '未设置'}。`;
3098
+ card.appendChild(settingsRow(label, hint, box));
3099
+ };
3100
+ keyRow('tavily', 'Tavily', search.tavily);
3101
+ keyRow('brave', 'Brave', search.brave);
3102
+
3103
+ card.appendChild(
3104
+ settingsRow(
3105
+ '深度搜索',
3106
+ '开启后每次搜索会自动读前 3 页正文,答案更准,但会多花几秒。默认关闭;对话里也可以临时要求 deep。',
3107
+ settingsSwitch(Boolean(search.deep), async (on) => {
3108
+ await saveSetting('searchDeep', { value: String(on) });
3109
+ await refresh(false);
3110
+ toast(on ? '已开启深度搜索' : '已关闭深度搜索');
3111
+ }),
3112
+ ),
3113
+ );
3114
+
3115
+ card.appendChild(
3116
+ settingsRow(
3117
+ '结果语言',
3118
+ '英文/技术优先会更多命中官方文档、GitHub 和 Stack Overflow;自动会跟随问题语言。',
3119
+ settingsSeg(
3120
+ [
3121
+ { value: 'en', label: '英文/技术优先' },
3122
+ { value: 'auto', label: '自动' },
3123
+ { value: 'zh', label: '中文优先' },
3124
+ ],
3125
+ search.language ?? 'en',
3126
+ async (value) => {
3127
+ await saveSetting('searchLanguage', { value });
3128
+ await refresh(false);
3129
+ toast('搜索语言偏好已更新');
3130
+ },
3131
+ ),
3132
+ ),
3133
+ );
3134
+
3135
+ section.appendChild(card);
3136
+ body.appendChild(section);
3137
+ }
3138
+
3046
3139
  function renderAppearanceSettings(body) {
3047
3140
  const saved = (() => {
3048
3141
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vincentbanana/bananacli",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "description": "A multi-provider AI agent for your terminal: file tools, shell, web search, persistent sessions, diff preview and undo",
5
5
  "type": "module",
6
6
  "engines": {