@@ -4,7 +4,7 @@ import * as dotenv from 'dotenv'
44import { ObjectId } from 'mongodb'
55import type { RequestProps } from './types'
66import type { ChatContext , ChatMessage } from './chatgpt'
7- import { chatConfig , chatReplyProcess , containsSensitiveWords , getRandomApiKey , initAuditService } from './chatgpt'
7+ import { abortChatProcess , chatConfig , chatReplyProcess , containsSensitiveWords , getRandomApiKey , initAuditService } from './chatgpt'
88import { auth , getUserId } from './middleware/auth'
99import { clearApiKeyCache , clearConfigCache , getApiKeys , getCacheApiKeys , getCacheConfig , getOriginConfig } from './storage/config'
1010import type { AuditConfig , CHATMODEL , ChatInfo , ChatOptions , Config , KeyConfig , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
@@ -431,6 +431,8 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
431431 top_p,
432432 chatModel : user . config . chatModel ,
433433 key : await getRandomApiKey ( user , user . config . chatModel ) ,
434+ userId,
435+ messageId : message . _id . toString ( ) ,
434436 } )
435437 // return the whole response including usage
436438 res . write ( `\n${ JSON . stringify ( result . data ) } ` )
@@ -457,13 +459,15 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
457459 await updateChat ( message . _id as unknown as string ,
458460 result . data . text ,
459461 result . data . id ,
462+ result . data . conversationId ,
460463 result . data . detail ?. usage as UsageResponse ,
461464 previousResponse as [ ] )
462465 }
463466 else {
464467 await updateChat ( message . _id as unknown as string ,
465468 result . data . text ,
466469 result . data . id ,
470+ result . data . conversationId ,
467471 result . data . detail ?. usage as UsageResponse )
468472 }
469473
@@ -481,6 +485,23 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
481485 }
482486} )
483487
488+ router . post ( '/chat-abort' , [ auth , limiter ] , async ( req , res ) => {
489+ try {
490+ const userId = req . headers . userId . toString ( )
491+ const { text, messageId, conversationId } = req . body as { text : string ; messageId : string ; conversationId : string }
492+ const msgId = await abortChatProcess ( userId )
493+ await updateChat ( msgId ,
494+ text ,
495+ messageId ,
496+ conversationId ,
497+ null )
498+ res . send ( { status : 'Success' , message : 'OK' , data : null } )
499+ }
500+ catch ( error ) {
501+ res . send ( { status : 'Fail' , message : '重置邮件已发送 | Reset email has been sent' , data : null } )
502+ }
503+ } )
504+
484505router . post ( '/user-register' , async ( req , res ) => {
485506 try {
486507 const { username, password } = req . body as { username : string ; password : string }
0 commit comments