@@ -7,6 +7,7 @@ import { z } from 'zod'
77import { getSession } from '@/lib/auth'
88import { generateChatTitle } from '@/lib/copilot/chat-title'
99import { getCopilotModel } from '@/lib/copilot/config'
10+ import { COPILOT_MODEL_IDS , COPILOT_REQUEST_MODES } from '@/lib/copilot/models'
1011import { SIM_AGENT_API_URL_DEFAULT , SIM_AGENT_VERSION } from '@/lib/copilot/constants'
1112import {
1213 authenticateCopilotRequestSessionOnly ,
@@ -40,34 +41,8 @@ const ChatMessageSchema = z.object({
4041 userMessageId : z . string ( ) . optional ( ) , // ID from frontend for the user message
4142 chatId : z . string ( ) . optional ( ) ,
4243 workflowId : z . string ( ) . min ( 1 , 'Workflow ID is required' ) ,
43- model : z
44- . enum ( [
45- 'gpt-5-fast' ,
46- 'gpt-5' ,
47- 'gpt-5-medium' ,
48- 'gpt-5-high' ,
49- 'gpt-5.1-fast' ,
50- 'gpt-5.1' ,
51- 'gpt-5.1-medium' ,
52- 'gpt-5.1-high' ,
53- 'gpt-5-codex' ,
54- 'gpt-5.1-codex' ,
55- 'gpt-5.2' ,
56- 'gpt-5.2-codex' ,
57- 'gpt-5.2-pro' ,
58- 'gpt-4o' ,
59- 'gpt-4.1' ,
60- 'o3' ,
61- 'claude-4-sonnet' ,
62- 'claude-4.5-haiku' ,
63- 'claude-4.5-sonnet' ,
64- 'claude-4.5-opus' ,
65- 'claude-4.1-opus' ,
66- 'gemini-3-pro' ,
67- ] )
68- . optional ( )
69- . default ( 'claude-4.5-opus' ) ,
70- mode : z . enum ( [ 'ask' , 'agent' , 'plan' ] ) . optional ( ) . default ( 'agent' ) ,
44+ model : z . enum ( COPILOT_MODEL_IDS ) . optional ( ) . default ( 'claude-4.5-opus' ) ,
45+ mode : z . enum ( COPILOT_REQUEST_MODES ) . optional ( ) . default ( 'agent' ) ,
7146 prefetch : z . boolean ( ) . optional ( ) ,
7247 createNewChat : z . boolean ( ) . optional ( ) . default ( false ) ,
7348 stream : z . boolean ( ) . optional ( ) . default ( true ) ,
@@ -295,7 +270,8 @@ export async function POST(req: NextRequest) {
295270 }
296271
297272 const defaults = getCopilotModel ( 'chat' )
298- const modelToUse = env . COPILOT_MODEL || defaults . model
273+ const selectedModel = model || defaults . model
274+ const envModel = env . COPILOT_MODEL || defaults . model
299275
300276 let providerConfig : CopilotProviderConfig | undefined
301277 const providerEnv = env . COPILOT_PROVIDER as any
@@ -304,28 +280,31 @@ export async function POST(req: NextRequest) {
304280 if ( providerEnv === 'azure-openai' ) {
305281 providerConfig = {
306282 provider : 'azure-openai' ,
307- model : modelToUse ,
283+ model : envModel ,
308284 apiKey : env . AZURE_OPENAI_API_KEY ,
309285 apiVersion : 'preview' ,
310286 endpoint : env . AZURE_OPENAI_ENDPOINT ,
311287 }
312288 } else if ( providerEnv === 'vertex' ) {
313289 providerConfig = {
314290 provider : 'vertex' ,
315- model : modelToUse ,
291+ model : envModel ,
316292 apiKey : env . COPILOT_API_KEY ,
317293 vertexProject : env . VERTEX_PROJECT ,
318294 vertexLocation : env . VERTEX_LOCATION ,
319295 }
320296 } else {
321297 providerConfig = {
322298 provider : providerEnv ,
323- model : modelToUse ,
299+ model : selectedModel ,
324300 apiKey : env . COPILOT_API_KEY ,
325301 }
326302 }
327303 }
328304
305+ const effectiveMode = mode === 'agent' ? 'build' : mode
306+ const transportMode = effectiveMode === 'build' ? 'agent' : effectiveMode
307+
329308 // Determine conversationId to use for this request
330309 const effectiveConversationId =
331310 ( currentChat ?. conversationId as string | undefined ) || conversationId
@@ -345,7 +324,7 @@ export async function POST(req: NextRequest) {
345324 }
346325 } | null = null
347326
348- if ( mode === 'agent ' ) {
327+ if ( effectiveMode === 'build ' ) {
349328 // Build base tools (executed locally, not deferred)
350329 // Include function_execute for code execution capability
351330 baseTools = [
@@ -452,8 +431,8 @@ export async function POST(req: NextRequest) {
452431 userId : authenticatedUserId ,
453432 stream : stream ,
454433 streamToolCalls : true ,
455- model : model ,
456- mode : mode ,
434+ model : selectedModel ,
435+ mode : transportMode ,
457436 messageId : userMessageIdToUse ,
458437 version : SIM_AGENT_VERSION ,
459438 ...( providerConfig ? { provider : providerConfig } : { } ) ,
@@ -477,7 +456,7 @@ export async function POST(req: NextRequest) {
477456 hasConversationId : ! ! effectiveConversationId ,
478457 hasFileAttachments : processedFileContents . length > 0 ,
479458 messageLength : message . length ,
480- mode,
459+ mode : effectiveMode ,
481460 hasTools : integrationTools . length > 0 ,
482461 toolCount : integrationTools . length ,
483462 hasBaseTools : baseTools . length > 0 ,
0 commit comments