Skip to content

Commit ffbb4b9

Browse files
committed
Jumps over deleted messages in history when calling api
(cherry picked from commit 7a0f3f3)
1 parent 4c33187 commit ffbb4b9

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

service/src/chatgpt/index.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SocksProxyAgent } from 'socks-proxy-agent'
66
import httpsProxyAgent from 'https-proxy-agent'
77
import fetch from 'node-fetch'
88
import type { AuditConfig, CHATMODEL, KeyConfig, UserInfo } from 'src/storage/model'
9+
import { Status } from "src/storage/model";
910
import jwt_decode from 'jwt-decode'
1011
import dayjs from 'dayjs'
1112
import type { TextAuditService } from '../utils/textAudit'
@@ -346,22 +347,30 @@ 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 getMessageById(parentMessageId)
356+
} else {
357+
if (isPrompt) { // prompt
358+
return {
359+
id,
360+
conversationId: chatInfo.options.conversationId,
361+
parentMessageId: parentMessageId,
362+
role: 'user',
363+
text: chatInfo.prompt,
364+
}
356365
}
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,
366+
else {
367+
return { // completion
368+
id,
369+
conversationId: chatInfo.options.conversationId,
370+
parentMessageId: parentMessageId,
371+
role: 'assistant',
372+
text: chatInfo.response,
373+
}
365374
}
366375
}
367376
}

0 commit comments

Comments
 (0)