@@ -4,10 +4,10 @@ import * as dotenv from 'dotenv'
44import { ObjectId } from 'mongodb'
55import type { RequestProps } from './types'
66import type { ChatContext , ChatMessage } from './chatgpt'
7- import { chatConfig , chatReplyProcess , containsSensitiveWords , initApi , initAuditService } from './chatgpt'
7+ import { chatConfig , chatReplyProcess , containsSensitiveWords , initAuditService } from './chatgpt'
88import { auth } from './middleware/auth'
99import { clearConfigCache , getCacheConfig , getOriginConfig } from './storage/config'
10- import type { AuditConfig , ChatInfo , ChatOptions , Config , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
10+ import type { AuditConfig , CHATMODEL , ChatInfo , ChatOptions , Config , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
1111import { Status } from './storage/model'
1212import {
1313 clearChat ,
@@ -31,6 +31,7 @@ import {
3131 updateConfig ,
3232 updateRoomPrompt ,
3333 updateRoomUsingContext ,
34+ updateUserChatModel ,
3435 updateUserInfo ,
3536 updateUserPassword ,
3637 verifyUser ,
@@ -384,9 +385,9 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
384385 let message : ChatInfo
385386 try {
386387 const config = await getCacheConfig ( )
388+ const userId = req . headers . userId . toString ( )
389+ const user = await getUserById ( userId )
387390 if ( config . auditConfig . enabled || config . auditConfig . customizeEnabled ) {
388- const userId = req . headers . userId . toString ( )
389- const user = await getUserById ( userId )
390391 if ( user . email . toLowerCase ( ) !== process . env . ROOT_USER && await containsSensitiveWords ( config . auditConfig , prompt ) ) {
391392 res . send ( { status : 'Fail' , message : '含有敏感词 | Contains sensitive words' , data : null } )
392393 return
@@ -423,6 +424,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
423424 systemMessage,
424425 temperature,
425426 top_p,
427+ chatModel : user . config . chatModel ,
426428 } )
427429 // return the whole response including usage
428430 res . write ( `\n${ JSON . stringify ( result . data ) } ` )
@@ -581,6 +583,7 @@ router.post('/user-login', async (req, res) => {
581583 description : user . description ,
582584 userId : user . _id ,
583585 root : username . toLowerCase ( ) === process . env . ROOT_USER ,
586+ config : user . config ,
584587 } , config . siteConfig . loginSalt . trim ( ) )
585588 res . send ( { status : 'Success' , message : '登录成功 | Login successfully' , data : { token } } )
586589 }
@@ -642,6 +645,22 @@ router.post('/user-info', auth, async (req, res) => {
642645 }
643646} )
644647
648+ router . post ( '/user-chat-model' , auth , async ( req , res ) => {
649+ try {
650+ const { chatModel } = req . body as { chatModel : CHATMODEL }
651+ const userId = req . headers . userId . toString ( )
652+
653+ const user = await getUserById ( userId )
654+ if ( user == null || user . status !== Status . Normal )
655+ throw new Error ( '用户不存在 | User does not exist.' )
656+ await updateUserChatModel ( userId , chatModel )
657+ res . send ( { status : 'Success' , message : '更新成功 | Update successfully' } )
658+ }
659+ catch ( error ) {
660+ res . send ( { status : 'Fail' , message : error . message , data : null } )
661+ }
662+ } )
663+
645664router . post ( '/verify' , async ( req , res ) => {
646665 try {
647666 const { token } = req . body as { token : string }
@@ -692,7 +711,7 @@ router.post('/verifyadmin', async (req, res) => {
692711
693712router . post ( '/setting-base' , rootAuth , async ( req , res ) => {
694713 try {
695- const { apiKey, apiModel, chatModel , apiBaseUrl, accessToken, timeoutMs, reverseProxy, socksProxy, socksAuth, httpsProxy } = req . body as Config
714+ const { apiKey, apiModel, apiBaseUrl, accessToken, timeoutMs, reverseProxy, socksProxy, socksAuth, httpsProxy } = req . body as Config
696715
697716 if ( apiModel === 'ChatGPTAPI' && ! isNotEmptyString ( apiKey ) )
698717 throw new Error ( 'Missing OPENAI_API_KEY environment variable.' )
@@ -702,7 +721,6 @@ router.post('/setting-base', rootAuth, async (req, res) => {
702721 const thisConfig = await getOriginConfig ( )
703722 thisConfig . apiKey = apiKey
704723 thisConfig . apiModel = apiModel
705- thisConfig . chatModel = chatModel
706724 thisConfig . apiBaseUrl = apiBaseUrl
707725 thisConfig . accessToken = accessToken
708726 thisConfig . reverseProxy = reverseProxy
@@ -712,7 +730,6 @@ router.post('/setting-base', rootAuth, async (req, res) => {
712730 thisConfig . httpsProxy = httpsProxy
713731 await updateConfig ( thisConfig )
714732 clearConfigCache ( )
715- initApi ( )
716733 const response = await chatConfig ( )
717734 res . send ( { status : 'Success' , message : '操作成功 | Successfully' , data : response . data } )
718735 }
0 commit comments