Skip to content

Commit 42be798

Browse files
authored
Merge pull request #350 from zhujunsan/kerwin
bugfix: jumps over deleted messages in history when calling api
2 parents 4c33187 + 492d267 commit 42be798

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

service/src/chatgpt/index.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
55
import { SocksProxyAgent } from 'socks-proxy-agent'
66
import httpsProxyAgent from 'https-proxy-agent'
77
import 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'
910
import jwt_decode from 'jwt-decode'
1011
import dayjs from 'dayjs'
1112
import 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

Comments
 (0)