@@ -91,44 +91,69 @@ router.post('/room-create', auth, async (req, res) => {
9191 try {
9292 const userId = req . headers . userId as string
9393 const user = await getUserById ( userId )
94- const { title, roomId } = req . body as { title : string , roomId : number }
95- const room = await createChatRoom ( userId , title , roomId , user . config ?. chatModel , user . config ?. maxContextCount )
94+ const { title, roomId, chatModel, modelId } = req . body as {
95+ title : string
96+ roomId : number
97+ chatModel ?: string
98+ modelId ?: string
99+ }
100+ const keys = ( await getCacheApiKeys ( ) ) . filter ( d => hasAnyRole ( d . userRoles , user . roles ) )
101+ let selectedKey = modelId ? keys . find ( key => key . _id . toString ( ) === modelId ) : undefined
102+ let resolvedChatModel = chatModel || user . config ?. chatModel
103+ if ( selectedKey ) {
104+ resolvedChatModel = `${ selectedKey . chatModel } |${ selectedKey . _id . toString ( ) } `
105+ }
106+ let actualModelName = resolvedChatModel || ''
107+ let specifiedKeyId : string | undefined
108+ if ( ! selectedKey && resolvedChatModel && resolvedChatModel . includes ( '|' ) ) {
109+ const parts = resolvedChatModel . split ( '|' )
110+ actualModelName = parts [ 0 ]
111+ specifiedKeyId = parts [ 1 ]
112+ selectedKey = keys . find ( key => key . _id . toString ( ) === specifiedKeyId && key . chatModel === actualModelName )
113+ }
114+ else if ( selectedKey ) {
115+ actualModelName = selectedKey . chatModel
116+ specifiedKeyId = selectedKey . _id . toString ( )
117+ }
118+ const fallbackKey = ! specifiedKeyId
119+ ? keys . find ( key => key . chatModel === actualModelName && ! key . toolsEnabled && ! key . imageUploadEnabled )
120+ : undefined
121+ const defaultThinkEnabled = selectedKey ?. defaultThinkEnabled || fallbackKey ?. defaultThinkEnabled || false
122+ const defaultSearchEnabled = selectedKey ?. defaultSearchEnabled || fallbackKey ?. defaultSearchEnabled || false
123+ const room = await createChatRoom (
124+ userId ,
125+ title ,
126+ roomId ,
127+ resolvedChatModel || '' ,
128+ user . config ?. maxContextCount ,
129+ defaultThinkEnabled ,
130+ defaultSearchEnabled ,
131+ )
96132 // Set imageUploadEnabled based on chatModel.
97133 if ( user && room . chatModel ) {
98134 // Parse model name, supporting "modelName|keyId".
99- let actualModelName = room . chatModel
100- let specifiedKeyId : string | undefined
135+ actualModelName = room . chatModel
136+ specifiedKeyId = undefined
101137 if ( room . chatModel . includes ( '|' ) ) {
102138 const parts = room . chatModel . split ( '|' )
103139 actualModelName = parts [ 0 ]
104140 specifiedKeyId = parts [ 1 ]
105141 }
106142
107- const keys = ( await getCacheApiKeys ( ) ) . filter ( d => hasAnyRole ( d . userRoles , user . roles ) )
108143 let imageUploadEnabled = false
109144 let toolsEnabled = false
110- if ( specifiedKeyId ) {
111- // Use the specified keyId configuration.
112- const specifiedKey = keys . find ( key => key . _id . toString ( ) === specifiedKeyId && key . chatModel === actualModelName )
113- if ( specifiedKey ) {
114- imageUploadEnabled = specifiedKey . imageUploadEnabled || false
115- toolsEnabled = specifiedKey . toolsEnabled || false
116- }
117- }
118- else {
119- // Fall back to the default logic when keyId is not specified.
120- imageUploadEnabled = false
121- toolsEnabled = false
145+ const specifiedKey = specifiedKeyId
146+ ? keys . find ( key => key . _id . toString ( ) === specifiedKeyId && key . chatModel === actualModelName )
147+ : selectedKey
148+ if ( specifiedKey ) {
149+ imageUploadEnabled = specifiedKey . imageUploadEnabled || false
150+ toolsEnabled = specifiedKey . toolsEnabled || false
122151 }
123152
124153 await updateRoomImageUploadEnabled ( userId , roomId , imageUploadEnabled || false )
125154 await updateRoomToolsEnabled ( userId , roomId , toolsEnabled || false )
126- if ( toolsEnabled ) {
127- await updateRoomThinkEnabled ( userId , roomId , false )
128- await updateRoomSearchEnabled ( userId , roomId , false )
129- room . thinkEnabled = false
130- room . searchEnabled = false
131- }
155+ room . thinkEnabled = defaultThinkEnabled
156+ room . searchEnabled = defaultSearchEnabled
132157 room . imageUploadEnabled = imageUploadEnabled || false
133158 room . toolsEnabled = toolsEnabled || false
134159 }
0 commit comments