@@ -5,6 +5,9 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
55import { SocksProxyAgent } from 'socks-proxy-agent'
66import httpsProxyAgent from 'https-proxy-agent'
77import fetch from 'node-fetch'
8+ import type { AuditConfig } from 'src/storage/model'
9+ import type { TextAuditService } from '../utils/textAudit'
10+ import { textAuditServices } from '../utils/textAudit'
811import { getCacheConfig , getOriginConfig } from '../storage/config'
912import { sendResponse } from '../utils'
1013import { isNotEmptyString } from '../utils/is'
@@ -26,6 +29,7 @@ const ErrorCodeMessage: Record<string, string> = {
2629
2730let apiModel : ApiModel
2831let api : ChatGPTAPI | ChatGPTUnofficialProxyAPI
32+ let auditService : TextAuditService
2933
3034export async function initApi ( ) {
3135 // More Info: https://github.com/transitive-bullshit/chatgpt-api
@@ -85,6 +89,10 @@ async function chatReplyProcess(options: RequestOptions) {
8589 const config = await getCacheConfig ( )
8690 const model = isNotEmptyString ( config . apiModel ) ? config . apiModel : 'gpt-3.5-turbo'
8791 const { message, lastContext, process, systemMessage, temperature, top_p } = options
92+
93+ if ( ( config . auditConfig ?. enabled ?? false ) && ! await auditText ( config . auditConfig , message ) )
94+ return sendResponse ( { type : 'Fail' , message : '含有敏感词 | Contains sensitive words' } )
95+
8896 try {
8997 const timeoutMs = ( await getCacheConfig ( ) ) . timeoutMs
9098 let options : SendMessageOptions = { timeoutMs }
@@ -120,9 +128,28 @@ async function chatReplyProcess(options: RequestOptions) {
120128 }
121129}
122130
131+ export function initAuditService ( audit : AuditConfig ) {
132+ if ( ! audit || ! audit . options || ! audit . options . apiKey || ! audit . options . apiSecret )
133+ throw new Error ( '未配置 | Not configured.' )
134+ const Service = textAuditServices [ audit . provider ]
135+ auditService = new Service ( audit . options )
136+ }
137+
138+ async function auditText ( audit : AuditConfig , text : string ) : Promise < boolean > {
139+ if ( ! auditService )
140+ initAuditService ( audit )
141+
142+ return await auditService . audit ( text )
143+ }
144+ let cachedBanlance : number | undefined
145+ let cacheExpiration = 0
146+
123147async function fetchBalance ( ) {
124- // 计算起始日期和结束日期
125148 const now = new Date ( ) . getTime ( )
149+ if ( cachedBanlance && cacheExpiration > now )
150+ return Promise . resolve ( cachedBanlance . toFixed ( 3 ) )
151+
152+ // 计算起始日期和结束日期
126153 const startDate = new Date ( now - 90 * 24 * 60 * 60 * 1000 )
127154 const endDate = new Date ( now + 24 * 60 * 60 * 1000 )
128155
@@ -165,9 +192,10 @@ async function fetchBalance() {
165192 const totalUsage = usageData . total_usage / 100
166193
167194 // 计算剩余额度
168- const balance = totalAmount - totalUsage
195+ cachedBanlance = totalAmount - totalUsage
196+ cacheExpiration = now + 10 * 60 * 1000
169197
170- return Promise . resolve ( balance . toFixed ( 3 ) )
198+ return Promise . resolve ( cachedBanlance . toFixed ( 3 ) )
171199 }
172200 catch {
173201 return Promise . resolve ( '-' )
@@ -226,4 +254,4 @@ initApi()
226254
227255export type { ChatContext , ChatMessage }
228256
229- export { chatReplyProcess , chatConfig , currentModel }
257+ export { chatReplyProcess , chatConfig , currentModel , auditText }
0 commit comments