Skip to content

An AI-powered search engine with a generative UI

feat: implement toolCallId-based citation system for multiple searches

Yoshiki Miura0357f26dbcdc
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index 458fbb30..8e6dc8d2 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -23,7 +23,7 @@ export type AnswerSectionProps = {
messageId: string,
options?: ChatRequestOptions
) => Promise<void | string | null | undefined>
- citationMap?: Record<number, SearchResultItem>
+ citationMaps?: Record<string, Record<number, SearchResultItem>>
}
export function AnswerSection({
@@ -35,7 +35,7 @@ export function AnswerSection({
messageId,
status,
reload,
- citationMap
+ citationMaps
}: AnswerSectionProps) {
const enableShare = process.env.NEXT_PUBLIC_SUPABASE_URL !== undefined
@@ -48,7 +48,7 @@ export function AnswerSection({
const message = content ? (
<div className="flex flex-col gap-1">
- <BotMessage message={content} citationMap={citationMap} />
+ <BotMessage message={content} citationMaps={citationMaps} />
{showActions && (
<MessageActions
message={content} // Keep original message content for copy
diff --git a/components/citation-context.tsx b/components/citation-context.tsx
index e264c58d..0869462e 100644
--- a/components/citation-context.tsx
+++ b/components/citation-context.tsx
@@ -5,7 +5,7 @@ import { createContext, ReactNode, useContext } from 'react'
import type { SearchResultItem } from '@/lib/types'
interface CitationContextValue {
- citationMap?: Record<number, SearchResultItem>
+ citationMaps?: Record<string, Record<number, SearchResultItem>>
}
const CitationContext = createContext<CitationContextValue | undefined>(
@@ -14,13 +14,13 @@ const CitationContext = createContext<CitationContextValue | undefined>(
export function CitationProvider({
children,
- citationMap
+ citationMaps
}: {
children: ReactNode
- citationMap?: Record<number, SearchResultItem>
+ citationMaps?: Record<string, Record<number, SearchResultItem>>
}) {
return (
- <CitationContext.Provider value={{ citationMap }}>
+ <CitationContext.Provider value={{ citationMaps }}>
{children}
</CitationContext.Provider>
)
@@ -28,5 +28,5 @@ export function CitationProvider({
export function useCitation() {
const context = useContext(CitationContext)
- return context || { citationMap: undefined }
+ return context || { citationMaps: undefined }
}
diff --git a/components/custom-link.tsx b/components/custom-link.tsx
index cc65dfdd..92ecb8b0 100644
--- a/components/custom-link.tsx
+++ b/components/custom-link.tsx
@@ -1,5 +1,6 @@
import { AnchorHTMLAttributes, DetailedHTMLProps } from 'react'
+import type { SearchResultItem } from '@/lib/types'
import { cn } from '@/lib/utils'
import { useCitation } from './citation-context'
@@ -16,14 +17,29 @@ export function Citing({
className,
...props
}: CustomLinkProps) {
- const { citationMap } = useCitation()
+ const { citationMaps } = useCitation()
const childrenText = children?.toString() || ''
const isNumber = /^\d+$/.test(childrenText)
// Get citation data if this is a numbered citation
- const citationNumber = isNumber ? parseInt(childrenText) : null
- const citationData =
- citationNumber && citationMap ? citationMap[citationNumber] : undefined
+ let citationData: SearchResultItem | undefined = undefined
+
+ if (isNumber && citationMaps && href) {
+ const citationNumber = parseInt(childrenText)
+
+ // 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]
+ if (citationMap[citationNumber]) {
+ // Check if this citation's URL matches the href
+ if (citationMap[citationNumber].url === decodeURI(href)) {
+ citationData = citationMap[citationNumber]
+ break
+ }
+ }
+ }
+ }
return (
<CitationLink
diff --git a/components/message.tsx b/components/message.tsx
index b4acad35..0fc426f6 100644
--- a/components/message.tsx
+++ b/components/message.tsx
@@ -19,14 +19,14 @@ import 'katex/dist/katex.min.css'
export function BotMessage({
message,
className,
- citationMap
+ citationMaps
}: {
message: string
className?: string
- citationMap?: Record<number, SearchResultItem>
+ citationMaps?: Record<string, Record<number, SearchResultItem>>
}) {
- // Process citations to replace [number](#) with [number](actual-url)
- const processedMessage = processCitations(message || '', citationMap)
+ // Process citations to replace [number](#toolCallId) with [number](actual-url)
+ const processedMessage = processCitations(message || '', citationMaps || {})
// Check if the content contains LaTeX patterns
const containsLaTeX = /\\\[([\s\S]*?)\\\]|\\\(([\s\S]*?)\\\)/.test(
@@ -79,7 +79,7 @@ export function BotMessage({
if (containsLaTeX) {
return (
- <CitationProvider citationMap={citationMap}>
+ <CitationProvider citationMaps={citationMaps}>
<div
className={cn(
'prose-sm prose-neutral prose-a:text-accent-foreground/50',
@@ -105,7 +105,7 @@ export function BotMessage({
}
return (
- <CitationProvider citationMap={citationMap}>
+ <CitationProvider citationMaps={citationMaps}>
<div
className={cn(
'prose-sm prose-neutral prose-a:text-accent-foreground/50',
diff --git a/components/render-message.tsx b/components/render-message.tsx
index e5813034..a717f670 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -2,7 +2,7 @@ import { UseChatHelpers } from '@ai-sdk/react'
import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import type { DynamicToolPart } from '@/lib/types/dynamic-tools'
-import { extractCitationMap } from '@/lib/utils/citation'
+import { extractCitationMaps } from '@/lib/utils/citation'
import { AnswerSection } from './answer-section'
import { DataSection } from './data-section'
@@ -37,8 +37,8 @@ export function RenderMessage({
onUpdateMessage,
reload
}: RenderMessageProps) {
- // Extract citation map from the message's tool outputs
- const citationMap = extractCitationMap(message)
+ // Extract citation maps from the message's tool outputs
+ const citationMaps = extractCitationMaps(message)
if (message.role === 'user') {
return (
<>
@@ -122,7 +122,7 @@ export function RenderMessage({
messageId={messageId}
reload={reload}
status={status}
- citationMap={citationMap}
+ citationMaps={citationMaps}
/>
)
case 'reasoning':
diff --git a/lib/agents/researcher-with-dynamic-tools.ts b/lib/agents/researcher-with-dynamic-tools.ts
index 2e088e0f..533c631d 100644
--- a/lib/agents/researcher-with-dynamic-tools.ts
+++ b/lib/agents/researcher-with-dynamic-tools.ts
@@ -23,7 +23,7 @@ When asked a question, you should:
5. Use the fetch tool to get detailed content from specific URLs
6. Use any available dynamic tools when they are relevant to the user's request
7. Analyze all search results to provide accurate, up-to-date information
-8. **CRITICAL: You MUST cite sources inline using the [number](#) format** (e.g., "According to recent data [1](#), AI adoption has increased..."). Use [1](#), [2](#), [3](#), etc., matching the order of search results. Always use # as the URL. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.
+8. **CRITICAL: You MUST cite sources inline using the [number](#toolCallId) format** (e.g., "According to recent data [1](#search-abc123), AI adoption has increased..."). Use [1](#toolCallId), [2](#toolCallId), [3](#toolCallId), etc., where number matches the order within each search result and toolCallId is the ID of the search that provided the result. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.
9. If results are not relevant or helpful, rely on your general knowledge (but do not add citations for general knowledge)
10. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question
11. Use markdown to structure your responses. Use headings to break up the content into sections.
@@ -57,9 +57,13 @@ When using dynamic tools:
- Handle any errors gracefully and inform the user if a tool fails
Citation Format:
-[number](#) - Always use this EXACT format with # as the URL, e.g., [1](#), [2](#), [3](#)
+[number](#toolCallId) - Always use this EXACT format, e.g., [1](#search-abc123), [2](#search-def456)
+- The number corresponds to the result order within each search (1, 2, 3, etc.)
+- The toolCallId is the unique identifier of the search that provided the result
+- Each search tool execution will have its own toolCallId
IMPORTANT: Citations must appear INLINE within your response text, not separately.
-Example: "Nvidia's stock has risen 200% [1](#) due to AI demand [2](#)."
+Example: "Nvidia's stock has risen 200% [1](#search-abc123) due to AI demand [2](#search-abc123)."
+Example with multiple searches: "Initial data shows [1](#search-abc123), while recent updates indicate [1](#search-def456)"
`
import type { DynamicToolConfig } from '@/lib/types/dynamic-tools'
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 29eab3f3..9913aa78 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -17,7 +17,7 @@ When asked a question, you should:
4. For video content, use the search tool with content_types: ['video'] or ['web', 'video']
5. Use the fetch tool to get detailed content from specific URLs
6. Analyze all search results to provide accurate, up-to-date information
-7. **CRITICAL: You MUST cite sources inline using the [number](#) format** (e.g., "According to recent data [1](#), AI adoption has increased..."). Use [1](#), [2](#), [3](#), etc., matching the order of search results. Always use # as the URL. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.
+7. **CRITICAL: You MUST cite sources inline using the [number](#toolCallId) format** (e.g., "According to recent data [1](#search-abc123), AI adoption has increased..."). Use [1](#toolCallId), [2](#toolCallId), [3](#toolCallId), etc., where number matches the order within each search result and toolCallId is the ID of the search that provided the result. Include citations immediately after statements that use information from that source. Every piece of information from search results MUST have a citation.
8. If results are not relevant or helpful, rely on your general knowledge (but do not add citations for general knowledge)
9. Provide comprehensive and detailed responses based on search results, ensuring thorough coverage of the user's question
10. Use markdown to structure your responses. Use headings to break up the content into sections.
@@ -46,9 +46,13 @@ When using the ask_question tool:
- Match the language to the user's language (except option values which must be in English)
Citation Format:
-[number](#) - Always use this EXACT format with # as the URL, e.g., [1](#), [2](#), [3](#)
+[number](#toolCallId) - Always use this EXACT format, e.g., [1](#search-abc123), [2](#search-def456)
+- The number corresponds to the result order within each search (1, 2, 3, etc.)
+- The toolCallId is the unique identifier of the search that provided the result
+- Each search tool execution will have its own toolCallId
IMPORTANT: Citations must appear INLINE within your response text, not separately.
-Example: "Nvidia's stock has risen 200% [1](#) due to AI demand [2](#)."
+Example: "Nvidia's stock has risen 200% [1](#search-abc123) due to AI demand [2](#search-abc123)."
+Example with multiple searches: "Initial data shows [1](#search-abc123), while recent updates indicate [1](#search-def456)"
`
export function researcher({
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index e8d331f0..59049cb0 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -26,7 +26,7 @@ export function createSearchTool(fullModel: string) {
search_depth = 'basic', // Default for standard schema
include_domains = [],
exclude_domains = []
- }) => {
+ }, context) => {
// Ensure max_results is at least 10
const minResults = 10
const effectiveMaxResults = Math.max(
@@ -123,7 +123,7 @@ export function createSearchTool(fullModel: string) {
}
}
- // Add citation mapping to search results
+ // Add citation mapping and toolCallId to search results
if (searchResult.results && searchResult.results.length > 0) {
const citationMap: Record<number, SearchResultItem> = {}
searchResult.results.forEach((result, index) => {
@@ -131,6 +131,11 @@ export function createSearchTool(fullModel: string) {
})
searchResult.citationMap = citationMap
}
+
+ // Add toolCallId from context
+ if (context?.toolCallId) {
+ searchResult.toolCallId = context.toolCallId
+ }
console.log('completed search')
return searchResult
diff --git a/lib/types/index.ts b/lib/types/index.ts
index ac1c507f..8698f2c2 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -4,6 +4,7 @@ export type SearchResults = {
videos?: SerperSearchResultItem[]
number_of_results?: number
query: string
+ toolCallId?: string // ID of the search tool call
citationMap?: Record<number, SearchResultItem> // Maps citation number to search result
}
diff --git a/lib/utils/citation.ts b/lib/utils/citation.ts
index 71f42631..db8a10d1 100644
--- a/lib/utils/citation.ts
+++ b/lib/utils/citation.ts
@@ -14,49 +14,50 @@ function isValidUrl(url: string): boolean {
}
/**
- * Extract citation map from a message's tool parts
- * Looks for search tool outputs and combines their citation maps
+ * Extract citation maps from a message's tool parts
+ * Returns a map of toolCallId to citation map
*/
-export function extractCitationMap(
+export function extractCitationMaps(
message: UIMessage
-): Record<number, SearchResultItem> | undefined {
- if (!message.parts) return undefined
+): Record<string, Record<number, SearchResultItem>> {
+ const citationMaps: Record<string, Record<number, SearchResultItem>> = {}
- const citationMap: Record<number, SearchResultItem> = {}
+ if (!message.parts) return citationMaps
message.parts.forEach((part: any) => {
// Check for search tool output
if (
part.type === 'tool-search' &&
part.state === 'output-available' &&
- part.output
+ part.output &&
+ part.toolCallId
) {
const searchResults = part.output as SearchResults
if (searchResults.citationMap) {
- // Merge citation maps from multiple search tools
- Object.assign(citationMap, searchResults.citationMap)
+ // Store citation map with toolCallId as key
+ citationMaps[part.toolCallId] = searchResults.citationMap
}
}
})
- return Object.keys(citationMap).length > 0 ? citationMap : undefined
+ return citationMaps
}
/**
- * Process citations in content, replacing [number](#) with proper URLs
+ * Process citations in content, replacing [number](#toolCallId) with proper URLs
* while keeping the display as just [number]
*/
export function processCitations(
content: string,
- citationMap?: Record<number, SearchResultItem>
+ citationMaps: Record<string, Record<number, SearchResultItem>>
): string {
- if (!citationMap || !content) {
+ if (!citationMaps || !content || Object.keys(citationMaps).length === 0) {
return content || ''
}
- // Replace [number](#) with [number](actual-url)
+ // Replace [number](#toolCallId) with [number](actual-url)
// Also handle cases with spaces: [ number ]
- return content.replace(/\[\s*(\d+)\s*\]\(#\)/g, (match, num) => {
+ return content.replace(/\[\s*(\d+)\s*\]\(#([^)]+)\)/g, (match, num, toolCallId) => {
const citationNum = parseInt(num, 10)
// Validate citation number bounds
@@ -64,6 +65,12 @@ export function processCitations(
return match
}
+ // Get the citation map for this toolCallId
+ const citationMap = citationMaps[toolCallId]
+ if (!citationMap) {
+ 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