@@ -372,7 +372,8 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
372372 res . setHeader ( 'Content-type' , 'application/octet-stream' )
373373
374374 let { roomId, uuid, regenerate, prompt, options = { } , systemMessage, temperature, top_p } = req . body as RequestProps
375- const userId = req . headers . userId as string
375+ const userId = req . headers . userId . toString ( )
376+ const config = await getCacheConfig ( )
376377 const room = await getChatRoom ( userId , roomId )
377378 if ( room == null )
378379 globalThis . console . error ( `Unable to get chat room \t ${ userId } \t ${ roomId } ` )
@@ -383,19 +384,19 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
383384 let message : ChatInfo
384385 let user = await getUserById ( userId )
385386 try {
386- const config = await getCacheConfig ( )
387- const userId = req . headers . userId . toString ( )
388- // 在调用前判断对话额度是否够用
389- const useAmount = user ? ( user . useAmount ?? 0 ) : 0
390-
391- // if use the fixed fakeuserid(some probability of duplicated with real ones), redefine user which is send to chatReplyProcess
392- if ( userId === '6406d8c50aedd633885fa16f' )
387+ // If use the fixed fakeuserid(some probability of duplicated with real ones), redefine user which is send to chatReplyProcess
388+ if ( userId === '6406d8c50aedd633885fa16f' ) {
393389 user = { _id : userId , roles : [ UserRole . User ] , useAmount : 999 , advanced : { maxContextCount : 999 } , limit_switch : false } as UserInfo
394-
395- // report if useamount is 0, 6406d8c50aedd633885fa16f is the fakeuserid in nologin situation
396- if ( userId !== '6406d8c50aedd633885fa16f' && Number ( useAmount ) <= 0 && user . limit_switch ) {
397- res . send ( { status : 'Fail' , message : '提问次数用完啦 | Question limit reached' , data : null } )
398- return
390+ }
391+ else {
392+ // If global usage count limit is enabled, check can use amount before process chat.
393+ if ( config . siteConfig ?. usageCountLimit ) {
394+ const useAmount = user ? ( user . useAmount ?? 0 ) : 0
395+ if ( Number ( useAmount ) <= 0 && user . limit_switch ) {
396+ res . send ( { status : 'Fail' , message : '提问次数用完啦 | Question limit reached' , data : null } )
397+ return
398+ }
399+ }
399400 }
400401
401402 if ( config . auditConfig . enabled || config . auditConfig . customizeEnabled ) {
@@ -496,8 +497,10 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
496497 }
497498 // update personal useAmount moved here
498499 // if not fakeuserid, and has valid user info and valid useAmount set by admin nut null and limit is enabled
499- if ( userId !== '6406d8c50aedd633885fa16f' && user && user . useAmount && user . limit_switch )
500- await updateAmountMinusOne ( userId )
500+ if ( config . siteConfig ?. usageCountLimit ) {
501+ if ( userId !== '6406d8c50aedd633885fa16f' && user && user . useAmount && user . limit_switch )
502+ await updateAmountMinusOne ( userId )
503+ }
501504 }
502505 catch ( error ) {
503506 globalThis . console . error ( error )
@@ -683,6 +686,7 @@ router.post('/session', async (req, res) => {
683686 title : config . siteConfig . siteTitle ,
684687 chatModels,
685688 allChatModels : chatModelOptions ,
689+ usageCountLimit : config . siteConfig ?. usageCountLimit ,
686690 userInfo,
687691 } ,
688692 } )
@@ -830,10 +834,11 @@ router.get('/user-getamtinfo', auth, async (req, res) => {
830834 try {
831835 const userId = req . headers . userId as string
832836 const user = await getUserById ( userId )
833- if ( user . limit_switch )
834- res . send ( { status : 'Success' , message : null , data : user . useAmount } )
835- else
836- res . send ( { status : 'Success' , message : null , data : 99999 } )
837+ const data = {
838+ amount : user . useAmount ,
839+ limit : user . limit_switch ,
840+ }
841+ res . send ( { status : 'Success' , message : null , data } )
837842 }
838843 catch ( error ) {
839844 console . error ( error )
0 commit comments