An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
debug: add logging to trace citation system issues
Yoshiki Miurab4fa8da7a0f0
diff --git a/components/custom-link.tsx b/components/custom-link.tsxindex 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 citationlet citationData: SearchResultItem | undefined = undefinedif (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 processedfor (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.tsxindex 0fc426f6..67be39de 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -25,6 +25,9 @@ export function BotMessage({className?: stringcitationMaps?: 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.tsxindex 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 outputsconst 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.tsindex 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 contextif (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.tsindex 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 boundsif (isNaN(citationNum) || citationNum < 1 || citationNum > 100) {+ console.log(`processCitations - Invalid citation number: ${num}`)return match}// Get the citation map for this toolCallIdconst 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}