Skip to content

An AI-powered search engine with a generative UI

debug: add logging to trace citation system issues

Yoshiki Miurab4fa8da7a0f0
diff --git a/components/custom-link.tsx b/components/custom-link.tsx
index 92ecb8b0..947fee47 100644
--- a/components/custom-link.tsx
+++ b/components/custom-link.tsx
@@ -21,20 +21,27 @@ export function Citing({
const childrenText = children?.toString() || ''
const isNumber = /^\d+$/.test(childrenText)
+ console.log('Citing - href:', href, 'children:', children, 'citationMaps:', citationMaps)
+
// Get citation data if this is a numbered citation
let citationData: SearchResultItem | undefined = undefined
if (isNumber && citationMaps && href) {
const citationNumber = parseInt(childrenText)
+ console.log('Citing - Looking for citation number:', citationNumber)
// Try to find the citation data by checking all citation maps
// This happens when the URL has already been processed
for (const toolCallId in citationMaps) {
const citationMap = citationMaps[toolCallId]
+ console.log(`Citing - Checking toolCallId ${toolCallId}:`, citationMap)
if (citationMap[citationNumber]) {
// Check if this citation's URL matches the href
- if (citationMap[citationNumber].url === decodeURI(href)) {
+ const decodedHref = decodeURI(href)
+ console.log(`Citing - Comparing URLs: ${citationMap[citationNumber].url} vs ${decodedHref}`)
+ if (citationMap[citationNumber].url === decodedHref) {
citationData = citationMap[citationNumber]
+ console.log('Citing - Found citation data:', citationData)
break
}
}
diff --git a/components/message.tsx b/components/message.tsx
index 0fc426f6..67be39de 100644
--- a/components/message.tsx
+++ b/components/message.tsx
@@ -25,6 +25,9 @@ export function BotMessage({
className?: string
citationMaps?: Record<string, Record<number, SearchResultItem>>
}) {
+ console.log('BotMessage - citationMaps:', citationMaps)
+ console.log('BotMessage - original message:', message)
+
// Process citations to replace [number](#toolCallId) with [number](actual-url)
const processedMessage = processCitations(message || '', citationMaps || {})
diff --git a/components/render-message.tsx b/components/render-message.tsx
index a717f670..7cc4a20d 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -39,6 +39,7 @@ export function RenderMessage({
}: RenderMessageProps) {
// Extract citation maps from the message's tool outputs
const citationMaps = extractCitationMaps(message)
+ console.log('RenderMessage - Extracted citationMaps:', citationMaps)
if (message.role === 'user') {
return (
<>
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 59049cb0..e56c99d6 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -135,9 +135,12 @@ export function createSearchTool(fullModel: string) {
// Add toolCallId from context
if (context?.toolCallId) {
searchResult.toolCallId = context.toolCallId
+ console.log('Search tool - toolCallId:', context.toolCallId)
+ } else {
+ console.log('Search tool - No toolCallId in context')
}
- console.log('completed search')
+ console.log('completed search with citationMap:', searchResult.citationMap)
return searchResult
}
})
diff --git a/lib/utils/citation.ts b/lib/utils/citation.ts
index db8a10d1..1ed7ba82 100644
--- a/lib/utils/citation.ts
+++ b/lib/utils/citation.ts
@@ -24,22 +24,34 @@ export function extractCitationMaps(
if (!message.parts) return citationMaps
- message.parts.forEach((part: any) => {
+ console.log('extractCitationMaps - Processing message parts:', message.parts.length)
+
+ message.parts.forEach((part: any, index: number) => {
// Check for search tool output
- if (
- part.type === 'tool-search' &&
- part.state === 'output-available' &&
- part.output &&
- part.toolCallId
- ) {
- const searchResults = part.output as SearchResults
- if (searchResults.citationMap) {
- // Store citation map with toolCallId as key
- citationMaps[part.toolCallId] = searchResults.citationMap
+ if (part.type === 'tool-search') {
+ console.log(`Part ${index} - tool-search:`, {
+ state: part.state,
+ hasOutput: !!part.output,
+ toolCallId: part.toolCallId,
+ hasCitationMap: !!(part.output?.citationMap)
+ })
+
+ if (
+ part.state === 'output-available' &&
+ part.output &&
+ part.toolCallId
+ ) {
+ const searchResults = part.output as SearchResults
+ if (searchResults.citationMap) {
+ // Store citation map with toolCallId as key
+ citationMaps[part.toolCallId] = searchResults.citationMap
+ console.log(`Stored citation map for toolCallId: ${part.toolCallId}`, searchResults.citationMap)
+ }
}
}
})
+ console.log('extractCitationMaps - Final citationMaps:', citationMaps)
return citationMaps
}
@@ -52,31 +64,43 @@ export function processCitations(
citationMaps: Record<string, Record<number, SearchResultItem>>
): string {
if (!citationMaps || !content || Object.keys(citationMaps).length === 0) {
+ console.log('processCitations - No citationMaps or content')
return content || ''
}
+ console.log('processCitations - Input content:', content)
+ console.log('processCitations - Available toolCallIds:', Object.keys(citationMaps))
+
// Replace [number](#toolCallId) with [number](actual-url)
// Also handle cases with spaces: [ number ]
- return content.replace(/\[\s*(\d+)\s*\]\(#([^)]+)\)/g, (match, num, toolCallId) => {
+ const processed = content.replace(/\[\s*(\d+)\s*\]\(#([^)]+)\)/g, (match, num, toolCallId) => {
+ console.log(`processCitations - Found citation: [${num}](#${toolCallId})`)
const citationNum = parseInt(num, 10)
// Validate citation number bounds
if (isNaN(citationNum) || citationNum < 1 || citationNum > 100) {
+ console.log(`processCitations - Invalid citation number: ${num}`)
return match
}
// Get the citation map for this toolCallId
const citationMap = citationMaps[toolCallId]
if (!citationMap) {
+ console.log(`processCitations - No citation map found for toolCallId: ${toolCallId}`)
return match // Keep original if no citation map found for this toolCallId
}
const citation = citationMap[citationNum]
if (citation && isValidUrl(citation.url)) {
- // Encode URI to prevent injection attacks
- return `[${num}](${encodeURI(citation.url)})`
+ const result = `[${num}](${encodeURI(citation.url)})`
+ console.log(`processCitations - Replaced with: ${result}`)
+ return result
}
+ console.log(`processCitations - No valid citation found for number ${citationNum} in toolCallId ${toolCallId}`)
return match // Keep original if no valid citation found
})
+
+ console.log('processCitations - Output content:', processed)
+ return processed
}