Skip to content

An AI-powered search engine with a generative UI

feat: improve annotations handling in chat stream

Yoshiki Miura205940a2f7b7
diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.ts
index 66e26ef5..bdc14750 100644
--- a/lib/streaming/create-manual-tool-stream.ts
+++ b/lib/streaming/create-manual-tool-stream.ts
@@ -6,12 +6,14 @@ import {
DataStreamWriter,
generateId,
generateText,
+ JSONValue,
smoothStream,
streamText
} from 'ai'
import { z } from 'zod'
import { searchSchema } from '../schema/search'
import { search } from '../tools/search'
+import { ExtendedCoreMessage } from '../types'
import { getModel } from '../utils/registry'
import { handleStreamFinish } from './handle-stream-finish'
import { parseToolCallXml } from './parse-tool-call'
@@ -71,6 +73,8 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {
console.log('Parsed tool call:', toolCall)
let toolCallMessages: CoreMessage[] = []
+ let toolCallDataAnnotation: ExtendedCoreMessage | null = null
+
if (toolCall && toolCall.tool !== '') {
const toolCallAnnotation = {
type: 'tool_call',
@@ -83,28 +87,41 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {
}
dataStream.writeData(toolCallAnnotation)
- const searchResults = await search(
- toolCall.parameters?.query ?? '',
- toolCall.parameters?.max_results,
- toolCall.parameters?.search_depth as 'basic' | 'advanced',
- toolCall.parameters?.include_domains,
- toolCall.parameters?.exclude_domains
- )
+ let toolCallResult: JSONValue = {}
+ if (toolCall.tool === 'search') {
+ toolCallResult = await search(
+ toolCall.parameters?.query ?? '',
+ toolCall.parameters?.max_results,
+ toolCall.parameters?.search_depth as 'basic' | 'advanced',
+ toolCall.parameters?.include_domains,
+ toolCall.parameters?.exclude_domains
+ )
+ } else {
+ throw new Error('Invalid tool name')
+ }
const updatedToolCallAnnotation = {
...toolCallAnnotation,
data: {
...toolCallAnnotation.data,
- result: JSON.stringify(searchResults),
+ result: JSON.stringify(toolCallResult),
state: 'result'
}
}
dataStream.writeMessageAnnotation(updatedToolCallAnnotation)
+ toolCallDataAnnotation = {
+ role: 'data',
+ content: {
+ type: 'tool_call',
+ data: updatedToolCallAnnotation.data
+ }
+ }
+
toolCallMessages = [
{
role: 'assistant' as const,
- content: `Tool call result: ${JSON.stringify(searchResults)}`
+ content: `Tool call result: ${JSON.stringify(toolCallResult)}`
},
{
role: 'user' as const,
@@ -119,15 +136,29 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {
system:
'You are a helpful assistant. You received a search results. You must use the search results to answer the user question.',
messages: [...coreMessages, ...toolCallMessages],
- experimental_transform: smoothStream({ chunking: 'word' }),
+ experimental_transform: smoothStream({
+ chunking: 'word'
+ }),
onFinish: async result => {
+ const annotations: ExtendedCoreMessage[] = [
+ ...(toolCallDataAnnotation ? [toolCallDataAnnotation] : []),
+ {
+ role: 'data',
+ content: {
+ type: 'reasoning',
+ data: result.reasoning
+ } as JSONValue
+ }
+ ]
+
await handleStreamFinish({
responseMessages: result.response.messages,
originalMessages: config.messages,
model: config.model,
chatId: config.chatId,
dataStream,
- skipRelatedQuestions: true
+ skipRelatedQuestions: true,
+ annotations
})
}
})
diff --git a/lib/streaming/handle-stream-finish.ts b/lib/streaming/handle-stream-finish.ts
index fa2c2b9b..b106e686 100644
--- a/lib/streaming/handle-stream-finish.ts
+++ b/lib/streaming/handle-stream-finish.ts
@@ -11,6 +11,7 @@ interface HandleStreamFinishParams {
chatId: string
dataStream: DataStreamWriter
skipRelatedQuestions?: boolean
+ annotations?: ExtendedCoreMessage[]
}
export async function handleStreamFinish({
@@ -19,21 +20,20 @@ export async function handleStreamFinish({
model,
chatId,
dataStream,
- skipRelatedQuestions = false
+ skipRelatedQuestions = false,
+ annotations = []
}: HandleStreamFinishParams) {
try {
const extendedCoreMessages = convertToExtendedCoreMessages(originalMessages)
-
- let annotation: JSONValue = {
- type: 'related-questions',
- data: {
- items: []
- }
- }
+ let allAnnotations = [...annotations]
if (!skipRelatedQuestions) {
// Notify related questions loading
- dataStream.writeMessageAnnotation(annotation)
+ const relatedQuestionsAnnotation: JSONValue = {
+ type: 'related-questions',
+ data: { items: [] }
+ }
+ dataStream.writeMessageAnnotation(relatedQuestionsAnnotation)
// Generate related questions
const relatedQuestions = await generateRelatedQuestions(
@@ -41,28 +41,25 @@ export async function handleStreamFinish({
model
)
- // Update the annotation with the related questions
- annotation = {
- ...annotation,
- data: relatedQuestions.object
+ // Create and add related questions annotation
+ const updatedRelatedQuestionsAnnotation: ExtendedCoreMessage = {
+ role: 'data',
+ content: {
+ type: 'related-questions',
+ data: relatedQuestions.object
+ } as JSONValue
}
- // Send related questions to client
- dataStream.writeMessageAnnotation(annotation)
+ dataStream.writeMessageAnnotation(
+ updatedRelatedQuestionsAnnotation.content as JSONValue
+ )
+ allAnnotations.push(updatedRelatedQuestionsAnnotation)
}
// Create the message to save
const generatedMessages = [
...extendedCoreMessages,
- ...responseMessages.slice(0, -1),
- ...(skipRelatedQuestions
- ? []
- : [
- {
- role: 'data',
- content: annotation
- } as ExtendedCoreMessage
- ]),
+ ...allAnnotations,
responseMessages[responseMessages.length - 1]
] as ExtendedCoreMessage[]