Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(search): 摘要回填保留端口与锚点,标题仅剥离真实 HTML 标签

Dash306848d98966
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts
index 98d2159538..c35049d946 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts
@@ -215,6 +215,74 @@ describe('cindyProxySearch', () => {
});
});
+ it('摘要按完整页面标识回填:不同锚点/端口不串用,泛型标题不被当作 HTML 删除', 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: 'Install',
+ url: 'https://example.test/guide#install',
+ },
+ {
+ type: 'web_search_result',
+ title: 'Upgrade',
+ url: 'https://example.test/guide#upgrade',
+ },
+ {
+ type: 'web_search_result',
+ title: 'Dev server',
+ url: 'https://example.test:8080/guide',
+ },
+ {
+ type: 'web_search_result',
+ title: 'Using <T> in TypeScript',
+ url: 'https://example.test/t',
+ },
+ {
+ type: 'web_search_result',
+ title: 'std::vector<T> and Map<K, V>',
+ url: 'https://example.test/v',
+ },
+ {
+ type: 'web_search_result',
+ title: '<b>Bold</b> <p align="center">Centered',
+ url: 'https://example.test/h',
+ },
+ ],
+ },
+ {
+ type: 'text',
+ text: [
+ 'https://example.test/guide#install | Install section.',
+ 'https://example.test/guide#upgrade | Upgrade section.',
+ 'https://example.test:8080/guide | Dev server on port 8080.',
+ ].join('\n'),
+ },
+ ],
+ }),
+ ) as unknown as typeof fetch,
+ });
+
+ await expect(service.search({ query: 'guide', limit: 6 })).resolves.toMatchObject({
+ ok: true,
+ results: [
+ { title: 'Install', snippet: 'Install section.' },
+ { title: 'Upgrade', snippet: 'Upgrade section.' },
+ { title: 'Dev server', snippet: 'Dev server on port 8080.' },
+ { title: 'Using <T> in TypeScript', snippet: '' },
+ { title: 'std::vector<T> and Map<K, V>', snippet: '' },
+ { title: 'Bold Centered', snippet: '' },
+ ],
+ });
+ });
+
it('已有结果时,后续搜索的工具级错误不使整次搜索失败', async () => {
const result = { type: 'web_search_result', title: 'Kept', url: 'https://example.test/kept' };
for (const errorBlock of [
diff --git a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts
index 500f0df776..1cfd53fcff 100644
--- a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts
+++ b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts
@@ -265,11 +265,15 @@ function normalizedHttpUrl(value: unknown): string | null {
}
}
-/** 模型复述链接时可能改写协议或末尾斜杠,回填摘要按忽略这些差异的键匹配。 */
+/**
+ * 模型复述链接时可能改写协议或末尾斜杠,回填摘要只忽略这两项差异;端口、查询串与
+ * 有意义的锚点(`#install` / `#upgrade`)标识不同的页面或章节,必须保留。序号锚点
+ * 已在 normalizedHttpUrl 去掉。
+ */
function sourceMatchKey(url: string): string {
try {
const parsed = new URL(url);
- return `${parsed.hostname.toLowerCase()}${parsed.pathname.replace(/\/+$/, '')}${parsed.search}`;
+ return `${parsed.host}${parsed.pathname.replace(/\/+$/, '')}${parsed.search}${parsed.hash}`;
} catch {
return url;
}
@@ -283,14 +287,21 @@ function fallbackTitle(url: string): string {
}
}
+// 只剥真实的 HTML 标签:带属性的开标签,或常见标签名的开/闭/自闭标签。
+// `<T>`、`<K, V>` 这类泛型/代码文本不在其中,必须保留。
+const HTML_TAG_PATTERN = new RegExp(
+ '<[a-z][a-z0-9-]*\\s+[^<>]*=[^<>]*>|' +
+ '</?(?:p|div|span|a|b|i|u|em|strong|br|hr|img|center|h[1-6]|ul|ol|li|table|tr|td|th|' +
+ 'section|article|header|footer|nav|main|font|small|code|pre|html|head|body|title|meta|link|script|style)' +
+ '(?:\\s[^<>]*)?/?>',
+ 'gi',
+);
+
function cleanTitle(value: unknown, url: string): string {
// 抓取到的标题偶尔是页面里的 HTML 片段(如 `<p align="center">`)。
const title =
typeof value === 'string'
- ? value
- .replace(/<[^>]*>/g, ' ')
- .replace(/\s+/g, ' ')
- .trim()
+ ? value.replace(HTML_TAG_PATTERN, ' ').replace(/\s+/g, ' ').trim()
: '';
return title || fallbackTitle(url);
}