Skip to content

Commit f741ed7

Browse files
committed
perf:deduplication reference document
1 parent 72e1ed5 commit f741ed7

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

electron/src/modules/chat/components/reference-manager.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,23 @@
7373
if (!Array.isArray(raw) || !raw.length) {
7474
return [];
7575
}
76-
return raw
76+
const seen = new Set();
77+
const result = [];
78+
raw
7779
.filter((item) => item && typeof item === 'object')
78-
.filter((item) => (item.selected === undefined) || Boolean(item.selected));
80+
.filter((item) => (item.selected === undefined) || Boolean(item.selected))
81+
.forEach((reference) => {
82+
const key = this.getReferenceKey(reference);
83+
if (key) {
84+
if (!seen.has(key)) {
85+
seen.add(key);
86+
result.push(reference);
87+
}
88+
} else {
89+
result.push(reference);
90+
}
91+
});
92+
return result;
7993
}
8094

8195
renderReferenceSection(references, metadata) {
@@ -129,13 +143,21 @@
129143
return null;
130144
}
131145

146+
const seen = new Set();
132147
const matches = rawMatches.map((match, index) => {
133148
if (!match || typeof match !== 'object') {
134149
return null;
135150
}
136151
const summaryText = typeof match.summary_text === 'string'
137152
? match.summary_text
138153
: (match.summary_preview || '');
154+
const key = this.getSummaryMatchKey(match, index);
155+
if (key && seen.has(key)) {
156+
return null;
157+
}
158+
if (key) {
159+
seen.add(key);
160+
}
139161
return {
140162
name: (match.filename || '').trim() || `文档-${match.rank || index + 1}`,
141163
summary: summaryText,
@@ -217,6 +239,50 @@
217239
return card;
218240
}
219241

242+
getReferenceKey(reference) {
243+
if (!reference || typeof reference !== 'object') {
244+
return '';
245+
}
246+
const absolute = ChatUtils.normalizePath(reference.absolute_path);
247+
if (absolute) {
248+
return `abs:${absolute}`;
249+
}
250+
const projectPath = ChatUtils.normalizePath(reference.project_relative_path);
251+
if (projectPath) {
252+
return `proj:${projectPath}`;
253+
}
254+
const filePath = ChatUtils.normalizePath(reference.file_path);
255+
if (filePath) {
256+
return `file:${filePath}`;
257+
}
258+
if (reference.reference_id) {
259+
return `id:${reference.reference_id}`;
260+
}
261+
const displayName = (reference.display_name || reference.filename || '').trim();
262+
if (displayName) {
263+
return `name:${displayName}`;
264+
}
265+
if (reference.snippet) {
266+
return `snippet:${String(reference.snippet).slice(0, 64)}`;
267+
}
268+
return '';
269+
}
270+
271+
getSummaryMatchKey(match, index) {
272+
if (!match || typeof match !== 'object') {
273+
return `idx:${index}`;
274+
}
275+
const filename = (match.filename || '').trim();
276+
const summary = typeof match.summary_text === 'string'
277+
? match.summary_text.trim()
278+
: (typeof match.summary_preview === 'string' ? match.summary_preview.trim() : '');
279+
const scorePart = Number.isFinite(match.score) ? match.score.toFixed(3) : 'na';
280+
if (filename || summary) {
281+
return `summary:${filename || summary}:${scorePart}`;
282+
}
283+
return `idx:${index}`;
284+
}
285+
220286
createReferenceItem(reference, metadata) {
221287
if (!reference || typeof reference !== 'object') {
222288
return null;

0 commit comments

Comments
 (0)