Skip to content

Commit 24cecfe

Browse files
authored
Merge pull request #669 from miurla/feat/enhance-message-metadata
feat: enhance message metadata with searchMode and modelId
2 parents ce2572f + 3cddd78 commit 24cecfe

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

lib/streaming/create-chat-stream-response.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,6 @@ export async function createChatStreamResponse(
128128
searchMode
129129
})
130130

131-
// Write metadata including traceId at the start of streaming
132-
if (parentTraceId) {
133-
writer.write({
134-
type: 'message-metadata',
135-
messageMetadata: { traceId: parentTraceId }
136-
})
137-
}
138-
139131
// Filter out reasoning parts from messages before converting to model messages
140132
// OpenAI API requires reasoning messages to be followed by assistant messages
141133
const filteredMessages = filterReasoningParts(messagesToModel)
@@ -215,7 +207,9 @@ export async function createChatStreamResponse(
215207
chatId,
216208
userId,
217209
titlePromise,
218-
parentTraceId
210+
parentTraceId,
211+
searchMode,
212+
context.modelId
219213
)
220214
}
221215
})

lib/streaming/helpers/persist-stream-results.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UIMessage } from 'ai'
22

33
import { upsertMessage } from '@/lib/actions/chat'
44
import { updateChatTitle } from '@/lib/db/actions'
5+
import { SearchMode } from '@/lib/types/search'
56
import { perfTime } from '@/lib/utils/perf-logging'
67
import { retryDatabaseOperation } from '@/lib/utils/retry'
78

@@ -12,14 +13,16 @@ export async function persistStreamResults(
1213
chatId: string,
1314
userId: string,
1415
titlePromise?: Promise<string>,
15-
parentTraceId?: string
16+
parentTraceId?: string,
17+
searchMode?: SearchMode,
18+
modelId?: string
1619
) {
17-
// Attach metadata to the response message if we have a traceId
18-
if (parentTraceId) {
19-
responseMessage.metadata = {
20-
...(responseMessage.metadata || {}),
21-
traceId: parentTraceId
22-
}
20+
// Attach metadata to the response message
21+
responseMessage.metadata = {
22+
...(responseMessage.metadata || {}),
23+
...(parentTraceId && { traceId: parentTraceId }),
24+
...(searchMode && { searchMode }),
25+
...(modelId && { modelId })
2326
}
2427

2528
// Wait for title generation if it was started

lib/types/ai.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fetchTool } from '@/lib/tools/fetch'
55
import { askQuestionTool } from '@/lib/tools/question'
66
import { searchTool } from '@/lib/tools/search'
77
import { createTodoTools, type TodoItem } from '@/lib/tools/todo'
8+
import type { SearchMode } from '@/lib/types/search'
89

910
// Re-export TodoItem for external use
1011
export type { TodoItem }
@@ -13,6 +14,8 @@ export type { TodoItem }
1314
export interface UIMessageMetadata {
1415
traceId?: string
1516
feedbackScore?: number | null
17+
searchMode?: SearchMode
18+
modelId?: string
1619
[key: string]: any
1720
}
1821

0 commit comments

Comments
 (0)