Asynchronously fetch chats
This commit is contained in:
parent
2b3055a84a
commit
23b08173ff
|
@ -5172,39 +5172,46 @@ export async function getChatsFromFiles(data, isGroupChat) {
|
|||
let chat_dict = {};
|
||||
let chat_list = Object.values(data).sort((a, b) => a["file_name"].localeCompare(b["file_name"])).reverse();
|
||||
|
||||
for (const { file_name } of chat_list) {
|
||||
try {
|
||||
const endpoint = isGroupChat ? '/getgroupchat' : '/getchat';
|
||||
const requestBody = isGroupChat
|
||||
? JSON.stringify({ id: file_name })
|
||||
: JSON.stringify({
|
||||
ch_name: characters[context.characterId].name,
|
||||
file_name: file_name.replace('.jsonl', ''),
|
||||
avatar_url: characters[context.characterId].avatar
|
||||
let chat_promise = chat_list.map(({ file_name}) => {
|
||||
return new Promise(async (res, rej) => {
|
||||
try {
|
||||
const endpoint = isGroupChat ? '/getgroupchat' : '/getchat';
|
||||
const requestBody = isGroupChat
|
||||
? JSON.stringify({ id: file_name })
|
||||
: JSON.stringify({
|
||||
ch_name: characters[context.characterId].name,
|
||||
file_name: file_name.replace('.jsonl', ''),
|
||||
avatar_url: characters[context.characterId].avatar
|
||||
});
|
||||
|
||||
const chatResponse = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: requestBody,
|
||||
cache: 'no-cache',
|
||||
});
|
||||
|
||||
const chatResponse = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: requestBody,
|
||||
cache: 'no-cache',
|
||||
});
|
||||
if (!chatResponse.ok) {
|
||||
return res();
|
||||
// continue;
|
||||
}
|
||||
|
||||
if (!chatResponse.ok) {
|
||||
continue;
|
||||
const currentChat = await chatResponse.json();
|
||||
if (!isGroupChat) {
|
||||
// remove the first message, which is metadata, only for individual chats
|
||||
currentChat.shift();
|
||||
}
|
||||
chat_dict[file_name] = currentChat;
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
const currentChat = await chatResponse.json();
|
||||
if (!isGroupChat) {
|
||||
// remove the first message, which is metadata, only for individual chats
|
||||
currentChat.shift();
|
||||
}
|
||||
chat_dict[file_name] = currentChat;
|
||||
return res();
|
||||
})
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
await Promise.all(chat_promise)
|
||||
|
||||
return chat_dict;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue