1- import type { AnnounceConfig , AuditConfig , Config , GiftCard , KeyConfig , MailConfig , SiteConfig , UserConfig , UserInfo } from './storage/model'
1+ import type { AnnounceConfig , AuditConfig , Config , GiftCard , KeyConfig , MailConfig , SiteConfig , UserInfo } from './storage/model'
22import type { AuthJwtPayload } from './types'
33import * as path from 'node:path'
44import * as process from 'node:process'
@@ -16,7 +16,7 @@ import { router as promptRouter } from './routes/prompt'
1616import { router as roomRouter } from './routes/room'
1717import { router as uploadRouter } from './routes/upload'
1818import { clearApiKeyCache , clearConfigCache , getApiKeys , getCacheApiKeys , getCacheConfig , getOriginConfig } from './storage/config'
19- import { AdvancedConfig , Status , UserRole } from './storage/model'
19+ import { AdvancedConfig , Status , UserConfig , UserRole } from './storage/model'
2020import {
2121 createUser ,
2222 disableUser2FA ,
@@ -35,6 +35,7 @@ import {
3535 updateUserAmount ,
3636 updateUserChatModel ,
3737 updateUserInfo ,
38+ updateUserMaxContextCount ,
3839 updateUserPassword ,
3940 updateUserPasswordWithVerifyOld ,
4041 updateUserStatus ,
@@ -183,6 +184,16 @@ router.post('/session', async (req, res) => {
183184 return
184185 }
185186
187+ if ( ! user ?. config ) {
188+ user . config = new UserConfig ( )
189+ }
190+ if ( ! user . config ?. chatModel ) {
191+ user . config . chatModel = config ?. siteConfig ?. chatModels . split ( ',' ) [ 0 ]
192+ }
193+ if ( user . config ?. maxContextCount === undefined ) {
194+ user . config . maxContextCount = 10
195+ }
196+
186197 userInfo = {
187198 name : user . name ,
188199 description : user . description ,
@@ -451,6 +462,22 @@ router.post('/user-chat-model', auth, async (req, res) => {
451462 }
452463} )
453464
465+ router . post ( '/user-max-context-count' , auth , async ( req , res ) => {
466+ try {
467+ const { maxContextCount } = req . body as { maxContextCount : number }
468+ const userId = req . headers . userId . toString ( )
469+
470+ const user = await getUserById ( userId )
471+ if ( user == null || user . status !== Status . Normal )
472+ throw new Error ( '用户不存在 | User does not exist.' )
473+ await updateUserMaxContextCount ( userId , maxContextCount )
474+ res . send ( { status : 'Success' , message : '更新成功 | Update successfully' } )
475+ }
476+ catch ( error ) {
477+ res . send ( { status : 'Fail' , message : error . message , data : null } )
478+ }
479+ } )
480+
454481router . get ( '/users' , rootAuth , async ( req , res ) => {
455482 try {
456483 const page = + req . query . page
@@ -811,7 +838,6 @@ router.post('/setting-advanced', auth, async (req, res) => {
811838 systemMessage : string
812839 temperature : number
813840 top_p : number
814- maxContextCount : number
815841 sync : boolean
816842 }
817843 if ( config . sync ) {
@@ -824,7 +850,6 @@ router.post('/setting-advanced', auth, async (req, res) => {
824850 config . systemMessage ,
825851 config . temperature ,
826852 config . top_p ,
827- config . maxContextCount ,
828853 )
829854 await updateConfig ( thisConfig )
830855 clearConfigCache ( )
@@ -834,7 +859,6 @@ router.post('/setting-advanced', auth, async (req, res) => {
834859 config . systemMessage ,
835860 config . temperature ,
836861 config . top_p ,
837- config . maxContextCount ,
838862 ) )
839863 res . send ( { status : 'Success' , message : '操作成功 | Successfully' } )
840864 }
0 commit comments