@@ -5,7 +5,8 @@ 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 , CHATMODEL , KeyConfig , UserInfo } from 'src/storage/model'
8+ import type { AuditConfig , CHATMODEL , KeyConfig , UserInfo } from '../storage/model'
9+ import { Status } from '../storage/model'
910import jwt_decode from 'jwt-decode'
1011import dayjs from 'dayjs'
1112import type { TextAuditService } from '../utils/textAudit'
@@ -346,22 +347,32 @@ async function getMessageById(id: string): Promise<ChatMessage | undefined> {
346347 const chatInfo = await getChatByMessageId ( isPrompt ? id . substring ( 7 ) : id )
347348
348349 if ( chatInfo ) {
349- if ( isPrompt ) { // prompt
350- return {
351- id,
352- conversationId : chatInfo . options . conversationId ,
353- parentMessageId : chatInfo . options . parentMessageId ,
354- role : 'user' ,
355- text : chatInfo . prompt ,
350+ const parentMessageId = isPrompt
351+ ? chatInfo . options . parentMessageId
352+ : `prompt_${ id } ` // parent message is the prompt
353+
354+ if ( chatInfo . status !== Status . Normal ) { // jumps over deleted messages
355+ return parentMessageId
356+ ? getMessageById ( parentMessageId )
357+ : undefined
358+ } else {
359+ if ( isPrompt ) { // prompt
360+ return {
361+ id,
362+ conversationId : chatInfo . options . conversationId ,
363+ parentMessageId : parentMessageId ,
364+ role : 'user' ,
365+ text : chatInfo . prompt ,
366+ }
356367 }
357- }
358- else {
359- return { // completion
360- id ,
361- conversationId : chatInfo . options . conversationId ,
362- parentMessageId : `prompt_ ${ id } ` , // parent message is the prompt
363- role : 'assistant' ,
364- text : chatInfo . response ,
368+ else {
369+ return { // completion
370+ id ,
371+ conversationId : chatInfo . options . conversationId ,
372+ parentMessageId : parentMessageId ,
373+ role : 'assistant' ,
374+ text : chatInfo . response ,
375+ }
365376 }
366377 }
367378 }
0 commit comments