fix: filter valid references in buildFinalResult and repairMarkdownFootnotesOuter

This commit is contained in:
Han Xiao 2025-06-11 23:02:14 -07:00
parent 0922c64022
commit 1a7267124e
3 changed files with 11 additions and 9 deletions

View File

@ -592,7 +592,7 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => {
)
let finalAnswer = (finalStep as AnswerAction).mdAnswer;
const annotations = (finalStep as AnswerAction).references?.map(ref => ({
const annotations = (finalStep as AnswerAction).references.filter(ref => ref?.url && ref?.title && ref?.exactQuote && ref?.dateTime).map(ref => ({
type: 'url_citation' as const,
url_citation: {
title: ref.title,

View File

@ -290,10 +290,10 @@ function buildFinalResult(
chunkToSourceMap: any
): { answer: string, references: Array<Reference> } {
logDebug(`[buildFinalResult] Building final result with ${filteredMatches.length} references`);
// Build reference objects
const references: Reference[] = filteredMatches.map((match) => {
const references = filteredMatches.map((match) => {
const source = chunkToSourceMap[match.webChunkIndex];
if (!source.text || !source.url || !source.title) return null;
return {
exactQuote: source.text,
url: source.url,
@ -303,7 +303,7 @@ function buildFinalResult(
answerChunk: match.answerChunk,
answerChunkPosition: match.answerChunkPosition
};
});
}).filter(Boolean) as Reference[];
// Inject reference markers ([^1], [^2], etc.) into the answer
let modifiedAnswer = answer;

View File

@ -223,11 +223,13 @@ export function repairMarkdownFootnotesOuter(markdownString: string): string {
}
// Add to references array
references.push({
exactQuote: content,
url,
title
});
if (content && title && url) {
references.push({
exactQuote: content,
url,
title,
});
}
}
// Only process if we found valid references