@@ -6,7 +6,7 @@ import type { ChatContext, ChatMessage } from './chatgpt'
66import { chatConfig , chatReplyProcess , currentModel , initApi } from './chatgpt'
77import { auth } from './middleware/auth'
88import { clearConfigCache , getCacheConfig , getOriginConfig } from './storage/config'
9- import type { ChatOptions , Config , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
9+ import type { ChatInfo , ChatOptions , Config , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
1010import { Status } from './storage/model'
1111import {
1212 clearChat ,
@@ -271,16 +271,21 @@ router.post('/chat', auth, async (req, res) => {
271271router . post ( '/chat-process' , [ auth , limiter ] , async ( req , res ) => {
272272 res . setHeader ( 'Content-type' , 'application/octet-stream' )
273273
274+ const { roomId, uuid, regenerate, prompt, options = { } , systemMessage, temperature, top_p } = req . body as RequestProps
275+
276+ let lastResponse
277+ let result
278+ let message : ChatInfo
274279 try {
275- const { roomId, uuid, regenerate, prompt, options = { } , systemMessage, temperature, top_p } = req . body as RequestProps
276- const message = regenerate
280+ message = regenerate
277281 ? await getChat ( roomId , uuid )
278282 : await insertChat ( uuid , prompt , roomId , options as ChatOptions )
279283 let firstChunk = true
280- const result = await chatReplyProcess ( {
284+ result = await chatReplyProcess ( {
281285 message : prompt ,
282286 lastContext : options ,
283287 process : ( chat : ChatMessage ) => {
288+ lastResponse = chat
284289 const chuck = {
285290 id : chat . id ,
286291 conversationId : chat . conversationId ,
@@ -300,7 +305,22 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
300305 temperature,
301306 top_p,
302307 } )
303- if ( result . status === 'Success' ) {
308+ // return the whole response including usage
309+ res . write ( `\n${ JSON . stringify ( result . data ) } ` )
310+ }
311+ catch ( error ) {
312+ res . write ( JSON . stringify ( error ) )
313+ }
314+ finally {
315+ res . end ( )
316+ try {
317+ if ( result == null || result === undefined || result . status !== 'Success' )
318+ result = { data : lastResponse }
319+
320+ if ( result . data === undefined )
321+ // eslint-disable-next-line no-unsafe-finally
322+ return
323+
304324 if ( regenerate && message . options . messageId ) {
305325 const previousResponse = message . previousResponse || [ ]
306326 previousResponse . push ( { response : message . response , options : message . options } )
@@ -325,15 +345,9 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
325345 result . data . detail . usage as UsageResponse )
326346 }
327347 }
328-
329- // return the whole response including usage
330- res . write ( `\n${ JSON . stringify ( result . data ) } ` )
331- }
332- catch ( error ) {
333- res . write ( JSON . stringify ( error ) )
334- }
335- finally {
336- res . end ( )
348+ catch ( error ) {
349+ global . console . log ( error )
350+ }
337351 }
338352} )
339353
0 commit comments