Skip to content

An AI-powered search engine with a generative UI

refactor: address PR review feedback

Yoshiki Miura8d383d89c800
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
index 23360b9f..4d1540be 100644
--- a/lib/agents/generate-related-questions.ts
+++ b/lib/agents/generate-related-questions.ts
@@ -2,6 +2,7 @@ import { convertToModelMessages, generateObject, type UIMessage } from 'ai'
import { z } from 'zod'
import { convertMessagesForAnthropic } from '../utils/anthropic-message-conversion'
+import { isAnthropicModel } from '../utils/model-detection'
import { getModel } from '../utils/registry'
const relatedQuestionsSchema = z.object({
@@ -23,7 +24,7 @@ export async function generateRelatedQuestions(
let modelMessages = convertToModelMessages(messages)
// Apply Anthropic-specific conversion if needed
- if (model.startsWith('anthropic:')) {
+ if (isAnthropicModel(model)) {
modelMessages = convertMessagesForAnthropic(modelMessages)
}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index ea1f2312..b04af77a 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -18,6 +18,7 @@ import {
truncateMessages
} from '../utils/context-window'
import { getTextFromParts } from '../utils/message-utils'
+import { isAnthropicModel } from '../utils/model-detection'
import { handleStreamFinish } from './helpers/handle-stream-finish'
import { prepareMessages } from './helpers/prepare-messages'
@@ -90,7 +91,7 @@ export async function createChatStreamResponse(
let modelMessages = convertToModelMessages(messagesToModel)
// Apply Anthropic-specific conversion if needed
- if (model.providerId === 'anthropic') {
+ if (isAnthropicModel(model)) {
modelMessages = convertMessagesForAnthropic(modelMessages)
}
diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.ts
index 86d05364..2654c721 100644
--- a/lib/utils/message-mapping.ts
+++ b/lib/utils/message-mapping.ts
@@ -369,7 +369,7 @@ export function mapDBPartToUIMessagePart(
// Special handling for tool parts that maintain their type
if (['search', 'fetch', 'question'].includes(toolName)) {
if (!part.tool_state) {
- throw new Error(`${toolName}_state is undefined`)
+ throw new Error(`tool_state is undefined for ${toolName}`)
}
switch (part.tool_state) {
diff --git a/lib/utils/model-detection.ts b/lib/utils/model-detection.ts
new file mode 100644
index 00000000..7130c7b0
--- /dev/null
+++ b/lib/utils/model-detection.ts
@@ -0,0 +1,15 @@
+import type { Model } from '@/lib/types/models'
+
+/**
+ * Check if a model is an Anthropic model
+ * @param model - Either a Model object or a model ID string
+ * @returns true if the model is from Anthropic provider
+ */
+export function isAnthropicModel(model: Model | string): boolean {
+ if (typeof model === 'string') {
+ // Handle model ID string format (e.g., 'anthropic:claude-3-opus')
+ return model.startsWith('anthropic:')
+ }
+ // Handle Model object
+ return model.providerId === 'anthropic'
+}