mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
Merge branch 'v1' of github.com:ikechan8370/chatgpt-plugin into v2
This commit is contained in:
commit
71704f3d35
4 changed files with 29 additions and 17 deletions
|
|
@ -19,7 +19,7 @@ export async function getConversations (qq = '', fetchFn = fetch) {
|
|||
}
|
||||
let conversations
|
||||
try {
|
||||
conversations = JSON.parse(json)
|
||||
conversations = JSON.parse(json).body
|
||||
} catch (e) {
|
||||
throw new Error(json)
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ export async function getConversations (qq = '', fetchFn = fetch) {
|
|||
map[item.id] = cachedConversationLastMessage
|
||||
} else {
|
||||
// 缓存中没有,就去查官方api
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${item.id}`, {
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/conversation/${item.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -44,16 +44,21 @@ export async function getConversations (qq = '', fetchFn = fetch) {
|
|||
if (Config.debug) {
|
||||
logger.mark('conversation detail for conversation ' + item.id, conversationDetail)
|
||||
}
|
||||
conversationDetail = JSON.parse(conversationDetail)
|
||||
try {
|
||||
conversationDetail = JSON.parse(conversationDetail).body
|
||||
} catch (err) {
|
||||
logger.warn('跳过')
|
||||
continue
|
||||
}
|
||||
let messages = Object.values(conversationDetail.mapping || {})
|
||||
|
||||
messages = messages
|
||||
.filter(message => message.message)
|
||||
.map(messages => messages.message)
|
||||
|
||||
let messagesAssistant = messages.filter(messages => messages.role === 'assistant')
|
||||
let messagesAssistant = messages.filter(messages => messages.author.role === 'assistant')
|
||||
.sort((a, b) => b.create_time - a.create_time)
|
||||
let messagesUser = messages.filter(messages => messages.role === 'user')
|
||||
let messagesUser = messages.filter(messages => messages.author.role === 'user')
|
||||
.sort((a, b) => b.create_time - a.create_time)
|
||||
await redis.set(`CHATGPT:CONVERSATION_LENGTH:${item.id}`, messagesUser?.length || 0)
|
||||
let lastMessage = null
|
||||
|
|
@ -101,7 +106,7 @@ export async function getLatestMessageIdByConversationId (conversationId, fetchF
|
|||
if (!accessToken) {
|
||||
throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token')
|
||||
}
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/conversation/${conversationId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -112,12 +117,12 @@ export async function getLatestMessageIdByConversationId (conversationId, fetchF
|
|||
if (Config.debug) {
|
||||
logger.mark('conversation detail for conversation ' + conversationId, conversationDetail)
|
||||
}
|
||||
conversationDetail = JSON.parse(conversationDetail)
|
||||
conversationDetail = JSON.parse(conversationDetail).body
|
||||
let messages = Object.values(conversationDetail.mapping)
|
||||
messages = messages
|
||||
.filter(message => message.message)
|
||||
.map(messages => messages.message)
|
||||
.filter(messages => messages.role === 'assistant')
|
||||
.filter(messages => messages.author.role === 'assistant')
|
||||
.sort((a, b) => b.create_time - a.create_time)
|
||||
await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_ID:${conversationId}`, messages[0].id)
|
||||
return messages[0].id
|
||||
|
|
@ -129,7 +134,7 @@ export async function deleteConversation (conversationId, fetchFn = fetch) {
|
|||
if (!accessToken) {
|
||||
throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token')
|
||||
}
|
||||
let response = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
let response = await fetchFn(`${Config.apiBaseUrl}/conversation/${conversationId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
|
|||
|
|
@ -86,6 +86,10 @@ export class OfficialChatGPTClient {
|
|||
referrer: 'https://chat.openai.com/chat'
|
||||
}
|
||||
const res = await this._fetch(url, option)
|
||||
if (res.status !== 200) {
|
||||
let body = await res.json()
|
||||
throw new Error(JSON.stringify(body, null, 2))
|
||||
}
|
||||
const decoder = new TextDecoder('utf-8')
|
||||
const bodyBytes = await res.arrayBuffer()
|
||||
const bodyText = decoder.decode(bodyBytes)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue