@@ -3,7 +3,7 @@ import jwt from 'jsonwebtoken'
33import * as dotenv from 'dotenv'
44import type { RequestProps } from './types'
55import type { ChatContext , ChatMessage } from './chatgpt'
6- import { auditText , chatConfig , chatReplyProcess , currentModel , initApi , initAuditService } from './chatgpt'
6+ import { chatConfig , chatReplyProcess , containsSensitiveWords , currentModel , initApi , initAuditService } from './chatgpt'
77import { auth } from './middleware/auth'
88import { clearConfigCache , getCacheConfig , getOriginConfig } from './storage/config'
99import type { AuditConfig , ChatInfo , ChatOptions , Config , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
@@ -277,6 +277,16 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
277277 let result
278278 let message : ChatInfo
279279 try {
280+ const config = await getCacheConfig ( )
281+ if ( config . auditConfig . enabled || config . auditConfig . customizeEnabled ) {
282+ const userId = req . headers . userId . toString ( )
283+ const user = await getUserById ( userId )
284+ if ( user . email . toLowerCase ( ) !== process . env . ROOT_USER && await containsSensitiveWords ( config . auditConfig , prompt ) ) {
285+ res . send ( { status : 'Fail' , message : '含有敏感词 | Contains sensitive words' , data : null } )
286+ return
287+ }
288+ }
289+
280290 message = regenerate
281291 ? await getChat ( roomId , uuid )
282292 : await insertChat ( uuid , prompt , roomId , options as ChatOptions )
@@ -602,7 +612,8 @@ router.post('/setting-audit', rootAuth, async (req, res) => {
602612 thisConfig . auditConfig = config
603613 const result = await updateConfig ( thisConfig )
604614 clearConfigCache ( )
605- initAuditService ( config )
615+ if ( config . enabled )
616+ initAuditService ( config )
606617 res . send ( { status : 'Success' , message : '操作成功 | Successfully' , data : result . auditConfig } )
607618 }
608619 catch ( error ) {
@@ -614,10 +625,12 @@ router.post('/audit-test', rootAuth, async (req, res) => {
614625 try {
615626 const { audit, text } = req . body as { audit : AuditConfig ; text : string }
616627 const config = await getCacheConfig ( )
617- initAuditService ( audit )
618- const result = await auditText ( audit , text )
619- initAuditService ( config . auditConfig )
620- res . send ( { status : 'Success' , message : ! result ? '含敏感词 | Contains sensitive words' : '不含敏感词 | Does not contain sensitive words.' , data : null } )
628+ if ( audit . enabled )
629+ initAuditService ( audit )
630+ const result = await containsSensitiveWords ( audit , text )
631+ if ( audit . enabled )
632+ initAuditService ( config . auditConfig )
633+ res . send ( { status : 'Success' , message : result ? '含敏感词 | Contains sensitive words' : '不含敏感词 | Does not contain sensitive words.' , data : null } )
621634 }
622635 catch ( error ) {
623636 res . send ( { status : 'Fail' , message : error . message , data : null } )
0 commit comments