Skip to content

An AI-powered search engine with a generative UI

feat: add reasoning data to chat messages

Yoshiki Miura862292e751ce
diff --git a/components/chat.tsx b/components/chat.tsx
index 5d46f788..557dd5de 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -38,7 +38,8 @@ export function Chat({
},
onError: error => {
toast.error(`Error in chat: ${error.message}`)
- }
+ },
+ sendExtraMessageFields: true
})
useEffect(() => {
diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.ts
index bdc14750..ae2ebeb3 100644
--- a/lib/streaming/create-manual-tool-stream.ts
+++ b/lib/streaming/create-manual-tool-stream.ts
@@ -46,7 +46,7 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {
<tool>tool_name</tool>
<parameters>
<query>search query text</query>
- <max_results>number - 20 by default</max_results>
+ <max_results>number - 5 by default</max_results>
<search_depth>basic or advanced</search_depth>
<include_domains>domain1,domain2</include_domains>
<exclude_domains>domain1,domain2</exclude_domains>
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index b7870042..92a0bfc3 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -1,5 +1,4 @@
-import { type ClassValue, clsx } from 'clsx'
-import { twMerge } from 'tailwind-merge'
+import { type Model } from '@/lib/types/models'
import {
convertToCoreMessages,
CoreMessage,
@@ -9,7 +8,8 @@ import {
Message,
ToolInvocation
} from 'ai'
-import { type Model } from '@/lib/types/models'
+import { type ClassValue, clsx } from 'clsx'
+import { twMerge } from 'tailwind-merge'
import { ExtendedCoreMessage } from '../types'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -93,6 +93,7 @@ export function convertToUIMessages(
messages: Array<ExtendedCoreMessage>
): Array<Message> {
let pendingAnnotations: JSONValue[] = []
+ let pendingReasoning: string | undefined
return messages.reduce((chatMessages: Array<Message>, message) => {
// Handle tool messages
@@ -112,7 +113,19 @@ export function convertToUIMessages(
typeof message.content !== 'number' &&
typeof message.content !== 'boolean'
) {
- pendingAnnotations.push(message.content as JSONValue)
+ const content = message.content as JSONValue
+ if (
+ content &&
+ typeof content === 'object' &&
+ 'type' in content &&
+ 'data' in content
+ ) {
+ if (content.type === 'reasoning') {
+ pendingReasoning = content.data as string
+ } else {
+ pendingAnnotations.push(content)
+ }
+ }
}
return chatMessages
}
@@ -153,17 +166,21 @@ export function convertToUIMessages(
role: message.role,
content: textContent,
toolInvocations: toolInvocations.length > 0 ? toolInvocations : undefined,
- // Add pending annotations if this is an assistant message
- ...(message.role === 'assistant' && pendingAnnotations.length > 0
- ? { annotations: pendingAnnotations }
- : {})
+ // Add pending annotations and reasoning if this is an assistant message
+ ...(message.role === 'assistant' && {
+ ...(pendingAnnotations.length > 0 && {
+ annotations: pendingAnnotations
+ }),
+ ...(pendingReasoning && { reasoning: pendingReasoning })
+ })
}
chatMessages.push(newMessage)
- // Clear pending annotations after adding them
+ // Clear pending data after adding them
if (message.role === 'assistant') {
pendingAnnotations = []
+ pendingReasoning = undefined
}
return chatMessages
@@ -186,6 +203,17 @@ export function convertToExtendedCoreMessages(
})
}
+ // Convert reasoning to data message
+ if (message.reasoning) {
+ result.push({
+ role: 'data',
+ content: {
+ type: 'reasoning',
+ data: message.reasoning
+ } as JSONValue
+ })
+ }
+
// Convert current message
const converted = convertToCoreMessages([message])
result.push(...converted)