@@ -14,7 +14,7 @@ import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/con
14
14
import { sendResponse } from '../utils'
15
15
import { hasAnyRole , isNotEmptyString } from '../utils/is'
16
16
import type { ChatContext , ChatGPTUnofficialProxyAPIOptions , JWT , ModelConfig } from '../types'
17
- import { getChatByMessageId } from '../storage/mongo'
17
+ import { getChatByMessageId , updateRoomAccountId } from '../storage/mongo'
18
18
import type { RequestOptions } from './types'
19
19
20
20
const { HttpsProxyAgent } = httpsProxyAgent
@@ -84,13 +84,22 @@ export async function initApi(key: KeyConfig, chatModel: CHATMODEL) {
84
84
}
85
85
const processThreads : { userId : string ; abort : AbortController ; messageId : string } [ ] = [ ]
86
86
async function chatReplyProcess ( options : RequestOptions ) {
87
- const model = options . chatModel
88
- const key = await getRandomApiKey ( options . user , options . user . config . chatModel )
87
+ const model = options . user . config . chatModel
88
+ const key = await getRandomApiKey ( options . user , options . user . config . chatModel , options . room . accountId )
89
89
const userId = options . user . _id . toString ( )
90
90
const messageId = options . messageId
91
91
if ( key == null || key === undefined )
92
92
throw new Error ( '没有可用的配置。请再试一次 | No available configuration. Please try again.' )
93
93
94
+ if ( key . keyModel === 'ChatGPTUnofficialProxyAPI' ) {
95
+ if ( ! options . room . accountId )
96
+ updateRoomAccountId ( userId , options . room . roomId , getAccountId ( key . key ) )
97
+
98
+ if ( options . lastContext && ( ( options . lastContext . conversationId && ! options . lastContext . parentMessageId )
99
+ || ( ! options . lastContext . conversationId && options . lastContext . parentMessageId ) ) )
100
+ throw new Error ( '无法在一个房间同时使用 AccessToken 以及 Api,请联系管理员,或新开聊天室进行对话 | Unable to use AccessToken and Api at the same time in the same room, please contact the administrator or open a new chat room for conversation' )
101
+ }
102
+
94
103
const { message, lastContext, process, systemMessage, temperature, top_p } = options
95
104
96
105
try {
@@ -270,10 +279,10 @@ function formatDate(date) {
270
279
271
280
async function chatConfig ( ) {
272
281
const config = await getOriginConfig ( ) as ModelConfig
273
- if ( config . apiModel === 'ChatGPTAPI' )
274
- config . balance = await fetchBalance ( )
275
- else
276
- config . accessTokenExpiredTime = await fetchAccessTokenExpiredTime ( )
282
+ // if (config.apiModel === 'ChatGPTAPI')
283
+ // config.balance = await fetchBalance()
284
+ // else
285
+ // config.accessTokenExpiredTime = await fetchAccessTokenExpiredTime()
277
286
return sendResponse < ModelConfig > ( {
278
287
type : 'Success' ,
279
288
data : config ,
@@ -354,9 +363,23 @@ async function randomKeyConfig(keys: KeyConfig[]): Promise<KeyConfig | null> {
354
363
return thisKey
355
364
}
356
365
357
- async function getRandomApiKey ( user : UserInfo , chatModel : CHATMODEL ) : Promise < KeyConfig | undefined > {
358
- const keys = ( await getCacheApiKeys ( ) ) . filter ( d => hasAnyRole ( d . userRoles , user . roles ) )
359
- return randomKeyConfig ( keys . filter ( d => d . chatModels . includes ( chatModel ) ) )
366
+ async function getRandomApiKey ( user : UserInfo , chatModel : CHATMODEL , accountId ?: string ) : Promise < KeyConfig | undefined > {
367
+ let keys = ( await getCacheApiKeys ( ) ) . filter ( d => hasAnyRole ( d . userRoles , user . roles ) )
368
+ . filter ( d => d . chatModels . includes ( chatModel ) )
369
+ if ( accountId )
370
+ keys = keys . filter ( d => d . keyModel === 'ChatGPTUnofficialProxyAPI' && getAccountId ( d . key ) === accountId )
371
+
372
+ return randomKeyConfig ( keys )
373
+ }
374
+
375
+ function getAccountId ( accessToken : string ) : string {
376
+ try {
377
+ const jwt = jwt_decode ( accessToken ) as JWT
378
+ return jwt [ 'https://api.openai.com/auth' ] . user_id
379
+ }
380
+ catch ( error ) {
381
+ return ''
382
+ }
360
383
}
361
384
362
385
export type { ChatContext , ChatMessage }
0 commit comments