Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: jumps over deleted messages in history when calling api #350

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import { SocksProxyAgent } from 'socks-proxy-agent'
import httpsProxyAgent from 'https-proxy-agent'
import fetch from 'node-fetch'
import type { AuditConfig, CHATMODEL, KeyConfig, UserInfo } from 'src/storage/model'
import type { AuditConfig, CHATMODEL, KeyConfig, UserInfo } from '../storage/model'
import { Status } from '../storage/model'
import jwt_decode from 'jwt-decode'
import dayjs from 'dayjs'
import type { TextAuditService } from '../utils/textAudit'
Expand Down Expand Up @@ -202,7 +203,7 @@
return false
}

async function fetchAccessTokenExpiredTime() {

Check warning on line 206 in service/src/chatgpt/index.ts

View workflow job for this annotation

GitHub Actions / lint

'fetchAccessTokenExpiredTime' is defined but never used. Allowed unused vars must match /^_/u
const config = await getCacheConfig()
const jwt = jwt_decode(config.accessToken) as JWT
if (jwt.exp)
Expand All @@ -213,7 +214,7 @@
let cachedBalance: number | undefined
let cacheExpiration = 0

async function fetchBalance() {

Check warning on line 217 in service/src/chatgpt/index.ts

View workflow job for this annotation

GitHub Actions / lint

'fetchBalance' is defined but never used. Allowed unused vars must match /^_/u
const now = new Date().getTime()
if (cachedBalance && cacheExpiration > now)
return Promise.resolve(cachedBalance.toFixed(3))
Expand Down Expand Up @@ -346,22 +347,32 @@
const chatInfo = await getChatByMessageId(isPrompt ? id.substring(7) : id)

if (chatInfo) {
if (isPrompt) { // prompt
return {
id,
conversationId: chatInfo.options.conversationId,
parentMessageId: chatInfo.options.parentMessageId,
role: 'user',
text: chatInfo.prompt,
const parentMessageId = isPrompt
? chatInfo.options.parentMessageId
: `prompt_${id}` // parent message is the prompt

if (chatInfo.status !== Status.Normal) { // jumps over deleted messages
return parentMessageId
? getMessageById(parentMessageId)
: undefined
} else {
if (isPrompt) { // prompt
return {
id,
conversationId: chatInfo.options.conversationId,
parentMessageId: parentMessageId,
role: 'user',
text: chatInfo.prompt,
}
}
}
else {
return { // completion
id,
conversationId: chatInfo.options.conversationId,
parentMessageId: `prompt_${id}`, // parent message is the prompt
role: 'assistant',
text: chatInfo.response,
else {
return { // completion
id,
conversationId: chatInfo.options.conversationId,
parentMessageId: parentMessageId,
role: 'assistant',
text: chatInfo.response,
}
}
}
}
Expand Down
Loading