From d1bcb7ce16115628c14ccd37670f3a599f067cac Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 14 Mar 2026 13:00:42 +0800 Subject: [PATCH] fix: use agent default for agent replies --- app/(chat)/api/chat/route.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index d59d702fd..c7e04701b 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -66,6 +66,7 @@ import { saveMessages, updateMessageById, updateChatLastContextById, + getAgentAiModelWithProvider, getAiModelById, getEnabledAiModelsWithProvider, getSubModelsByParentModelId, @@ -3034,6 +3035,7 @@ export async function POST(request: Request) { let confirmedPromptForConversationState: string | undefined; let smartAutoHistoryMessagesFromDb: DBMessage[] = []; let preferredAgentModelId: string | undefined; + let configuredAgentConversationModelId: string | undefined; let pendingGridConfirmation: | PendingGridConfirmation | null = null; @@ -3107,6 +3109,8 @@ export async function POST(request: Request) { if (isAiModelAgentAuto(selectedModelConfig)) { shouldRunAgentConversation = true; shouldKeepSelectedModel = false; + preferredAgentModelId = selectedModelConfig.id; + configuredAgentConversationModelId = selectedModelConfig.id; console.log( `[Smart Auto Mode] 🧠 Agent-only model detected, force auto routing: ${selectedModelConfig.displayName}` ); @@ -3130,6 +3134,21 @@ export async function POST(request: Request) { } } + if (shouldRunAgentConversation && !preferredAgentModelId) { + const configuredAgentModel = await getAgentAiModelWithProvider().catch((agentModelError) => { + console.warn("[Chat API] Failed to load configured agent model:", agentModelError); + return null; + }); + + if (configuredAgentModel?.model?.id) { + preferredAgentModelId = configuredAgentModel.model.id; + configuredAgentConversationModelId = configuredAgentModel.model.id; + console.log( + `[Smart Auto Mode] 🧠 Loaded configured agent conversation model: ${configuredAgentModel.model.displayName}`, + ); + } + } + const allowAgentVideoGeneration = Boolean(requestBody.videoParams) || isVideoProviderType(requestedModelProviderType); let smartAutoWorkflowDecision: @@ -4306,6 +4325,20 @@ export async function POST(request: Request) { smartAutoFriendlyMessage = normalizeAgentReportForDisplay(smartAutoFriendlyMessage); } + if ( + shouldRunAgentConversation && + !shouldKeepSelectedModel && + smartAutoExecution?.shouldStayConversational && + preferredAgentModelId + ) { + if (finalModelId !== preferredAgentModelId) { + finalModelId = preferredAgentModelId; + console.log( + `[Smart Auto Mode] 🧠 Conversational reply follows agent default model: ${configuredAgentConversationModelId || preferredAgentModelId}`, + ); + } + } + const session = await auth(); if (!session?.user) {