Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(search): 补全 Cindy AI 网页搜索结果摘要并修正部分失败判定
Dash1f8fbdb8eb07
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.tsindex 06273b7c2f..98d2159538 100644--- a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts+++ b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts@@ -110,6 +110,7 @@ describe('cindyProxySearch', () => {expect(JSON.parse(String(init.body))).toEqual({model: CINDY_SEARCH_MODEL_NAME,max_tokens: 2048,+ system: expect.stringContaining('output one line for each of the first 3 results'),messages: [{ role: 'user', content: 'Cindy' }],tools: [{@@ -143,6 +144,123 @@ describe('cindyProxySearch', () => {});});+ it('结果缺 cited_text 时按模型输出的 `<url> | 摘要` 行回填,并清理链接与标题', async () => {+ const service = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'test-key',+ fetchImpl: vi.fn(async () =>+ response({+ content: [+ {+ type: 'web_search_tool_result',+ content: [+ {+ type: 'web_search_result',+ title: 'Release',+ url: 'https://example.test/release#1',+ },+ {+ type: 'web_search_result',+ title: '<p align="center">',+ url: 'http://example.test/readme/',+ },+ { type: 'web_search_result', title: 'Channel', url: 'https://example.test/ch%0A' },+ { type: 'web_search_result', title: 'Cited', url: 'https://example.test/cited' },+ { type: 'web_search_result', title: 'Beyond limit', url: 'https://example.test/x' },+ ],+ },+ {+ type: 'text',+ text: [+ 'https://example.test/release#1 | Release notes for v1.',+ '- https://example.test/readme | The README describes Cindy.',+ '1. <https://example.test/ch> | Channel news | with a pipe.',+ 'https://example.test/cited | Model note that must not replace cited_text.',+ 'https://example.test/invented | A URL the search never returned.',+ 'Unformatted closing remark.',+ ].join('\n'),+ citations: [+ {+ type: 'web_search_result_location',+ url: 'https://example.test/cited',+ cited_text: 'Quoted from the page',+ },+ ],+ },+ ],+ }),+ ) as unknown as typeof fetch,+ });++ await expect(service.search({ query: 'Cindy', limit: 4 })).resolves.toEqual({+ ok: true,+ results: [+ {+ title: 'Release',+ url: 'https://example.test/release',+ snippet: 'Release notes for v1.',+ },+ {+ title: 'example.test',+ url: 'http://example.test/readme/',+ snippet: 'The README describes Cindy.',+ },+ {+ title: 'Channel',+ url: 'https://example.test/ch',+ snippet: 'Channel news | with a pipe.',+ },+ { title: 'Cited', url: 'https://example.test/cited', snippet: 'Quoted from the page' },+ ],+ });+ });++ it('已有结果时,后续搜索的工具级错误不使整次搜索失败', async () => {+ const result = { type: 'web_search_result', title: 'Kept', url: 'https://example.test/kept' };+ for (const errorBlock of [+ // Anthropic 标准形态:错误对象直接作为 content。+ { type: 'web_search_tool_result_error', error_code: 'max_uses_exceeded' },+ // 实测 Cindy 网关形态:错误项装在数组里。+ [{ type: 'web_search_tool_result_error', error_code: 'max_uses_exceeded' }],+ ]) {+ const service = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'test-key',+ fetchImpl: vi.fn(async () =>+ response({+ content: [+ { type: 'web_search_tool_result', content: [result] },+ { type: 'web_search_tool_result', content: errorBlock },+ ],+ }),+ ) as unknown as typeof fetch,+ });+ await expect(service.search({ query: 'Cindy', limit: 5 })).resolves.toMatchObject({+ ok: true,+ results: [{ title: 'Kept', url: 'https://example.test/kept', snippet: '' }],+ });+ }++ const onlyError = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'test-key',+ fetchImpl: vi.fn(async () =>+ response({+ content: [+ {+ type: 'web_search_tool_result',+ content: [{ type: 'web_search_tool_result_error', error_code: 'unavailable' }],+ },+ ],+ }),+ ) as unknown as typeof fetch,+ });+ await expect(onlyError.search({ query: 'Cindy', limit: 5 })).resolves.toMatchObject({+ ok: false,+ errorCode: 'UPSTREAM_UNAVAILABLE',+ });+ });+it('未配置 endpoint/key 时 fail closed,且不发请求', async () => {const fetchImpl = vi.fn() as unknown as typeof fetch;for (const [baseUrl, apiKey] of [@@ -189,7 +307,12 @@ describe('cindyProxySearch', () => {});it('网关 401 时上报本次请求携带的 Key,其他状态不上报', async () => {- for (const [status, expectedReports] of [[401, 1], [403, 0], [429, 0], [200, 0]] as const) {+ for (const [status, expectedReports] of [+ [401, 1],+ [403, 0],+ [429, 0],+ [200, 0],+ ] as const) {const onKeyRejected = vi.fn();const service = createCindyProxySearchService({getBaseUrl: () => 'https://gateway.example.test',diff --git a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.tsindex 9dbb14ea32..500f0df776 100644--- a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts+++ b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts@@ -13,6 +13,24 @@ export const CINDY_SEARCH_MODEL_NAME = 'cindy/web-search';const CINDY_SEARCH_WEB_TOOL_TYPE = 'web_search_20250305';const CINDY_SEARCH_MAX_TOKENS = 2_048;const CINDY_SEARCH_TIMEOUT_MS = 30_000;+const CINDY_SEARCH_MAX_SNIPPET_CHARS = 500;++/**+ * 搜索后端的结果只带标题、链接与加密正文,没有 cited_text / citations,摘要会全空;+ * 后端模型也会无视 max_uses 再搜一次。让模型只搜一次,并为前 limit 条结果各写一行+ * `<url> | 摘要`,由 parseSearchResponse 按链接回填 snippet。+ */+function buildSearchSystemPrompt(limit: number): string {+ return [+ 'You are a web search backend. Call web_search exactly once with the user query, then stop searching.',+ `After the search, output one line for each of the first ${limit} results, in result order, formatted exactly as:`,+ '<url> | <one or two sentences stating what that page says about the query>',+ 'Copy each URL verbatim from the search result.',+ // 只写「用查询的语言」时,后端模型会对英文查询输出中文摘要;写明映射后才稳定。+ 'Write each summary in the same language as the user query (English query -> English, Chinese query -> Chinese).',+ 'Output nothing else.',+ ].join('\n');+}/*** Join the Anthropic-compatible Messages endpoint for either a gateway root or@@ -238,12 +256,25 @@ function normalizedHttpUrl(value: unknown): string | null {try {const url = new URL(value.trim());if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;+ // 后端会给链接追加 `#1` 这类序号锚点、把换行编码进路径末尾,同一页面因此去重失败。+ if (/^#\d+$/.test(url.hash)) url.hash = '';+ url.pathname = url.pathname.replace(/(?:%0A|%0D|%09|%20)+$/i, '');return url.toString();} catch {return null;}}+/** 模型复述链接时可能改写协议或末尾斜杠,回填摘要按忽略这些差异的键匹配。 */+function sourceMatchKey(url: string): string {+ try {+ const parsed = new URL(url);+ return `${parsed.hostname.toLowerCase()}${parsed.pathname.replace(/\/+$/, '')}${parsed.search}`;+ } catch {+ return url;+ }+}+function fallbackTitle(url: string): string {try {return new URL(url).hostname;@@ -252,6 +283,33 @@ function fallbackTitle(url: string): string {}}+function cleanTitle(value: unknown, url: string): string {+ // 抓取到的标题偶尔是页面里的 HTML 片段(如 `<p align="center">`)。+ const title =+ typeof value === 'string'+ ? value+ .replace(/<[^>]*>/g, ' ')+ .replace(/\s+/g, ' ')+ .trim()+ : '';+ return title || fallbackTitle(url);+}++const SNIPPET_LINE_PATTERN =+ /^\s*(?:[-*>]|\d+[.)]|\[\d+\])?\s*<?(https?:\/\/[^\s|<>]+)>?\s*\|\s*(.+?)\s*$/;++/** 解析 buildSearchSystemPrompt 要求的 `<url> | 摘要` 行;只回填已有结果,不据此新增来源。 */+function collectSnippetLines(text: string, into: Map<string, string>): void {+ for (const line of text.split('\n')) {+ const match = SNIPPET_LINE_PATTERN.exec(line);+ if (!match) continue;+ const url = normalizedHttpUrl(match[1]);+ if (!url) continue;+ const key = sourceMatchKey(url);+ if (!into.has(key)) into.set(key, match[2].slice(0, CINDY_SEARCH_MAX_SNIPPET_CHARS));+ }+}+interface SearchSource {url: string;title: string;@@ -272,10 +330,7 @@ type ParsedSearchResponse =function sourceFromRecord(value: Record<string, unknown>): SearchSource | null {const url = normalizedHttpUrl(value.url);if (!url) return null;- const title =- typeof value.title === 'string' && value.title.trim().length > 0- ? value.title.trim()- : fallbackTitle(url);+ const title = cleanTitle(value.title, url);const snippet =typeof value.cited_text === 'string'? value.cited_text.trim()@@ -344,6 +399,11 @@ function parseSearchResponse(raw: unknown, limit: number): ParsedSearchResponseif (!existing.snippet && source.snippet) existing.snippet = source.snippet;};+ // 后端在一次响应里可能先给出结果、再因后续搜索报错(如 max_uses_exceeded)。+ // 工具级错误只在一条结果都没有时才代表本次搜索失败。+ let toolError: { errorCode: unknown } | null = null;+ const snippetLines = new Map<string, string>();+for (const block of raw.content) {if (!isRecord(block)) continue;@@ -351,32 +411,44 @@ function parseSearchResponse(raw: unknown, limit: number): ParsedSearchResponseif (Array.isArray(block.content)) {if (block.content.length > 0) sawCandidate = true;for (const item of block.content) {- if (!isRecord(item) || item.type !== 'web_search_result') continue;+ if (!isRecord(item)) continue;+ if (item.type === 'web_search_tool_result_error') {+ toolError ??= { errorCode: item.error_code };+ continue;+ }+ if (item.type !== 'web_search_result') continue;const source = sourceFromRecord(item);if (source) mergeSource(source);}} else if (isRecord(block.content) && block.content.type === 'web_search_tool_result_error') {- return searchToolFailure(block.content.error_code);+ toolError ??= { errorCode: block.content.error_code };} else {return invalidSearchResponse();}}- if (block.type === 'text' && Array.isArray(block.citations)) {- if (block.citations.length > 0) sawCandidate = true;- for (const citation of block.citations) {- if (!isRecord(citation)) continue;- const source = sourceFromRecord(citation);- if (source) mergeSource(source);+ if (block.type === 'text') {+ if (typeof block.text === 'string') collectSnippetLines(block.text, snippetLines);+ if (Array.isArray(block.citations)) {+ if (block.citations.length > 0) sawCandidate = true;+ for (const citation of block.citations) {+ if (!isRecord(citation)) continue;+ const source = sourceFromRecord(citation);+ if (source) mergeSource(source);+ }}}}- if (sawCandidate && sources.size === 0) return invalidSearchResponse();- return {- ok: true,- results: Array.from(sources.values()).slice(0, limit),- };+ if (sources.size === 0) {+ if (toolError) return searchToolFailure(toolError.errorCode);+ if (sawCandidate) return invalidSearchResponse();+ }+ const results = Array.from(sources.values()).slice(0, limit);+ for (const result of results) {+ if (!result.snippet) result.snippet = snippetLines.get(sourceMatchKey(result.url)) ?? '';+ }+ return { ok: true, results };}function webSearchRequestsOf(raw: unknown): number | undefined {@@ -415,6 +487,7 @@ export function createCindyProxySearchService(deps: CindyProxySearchDeps): Cindybody: JSON.stringify({model: CINDY_SEARCH_MODEL_NAME,max_tokens: CINDY_SEARCH_MAX_TOKENS,+ system: buildSearchSystemPrompt(limit),messages: [{ role: 'user', content: query }],tools: [{