diff --git a/src/web/lib/api.ts b/src/web/lib/api.ts index bfd0c40..16a2f51 100644 --- a/src/web/lib/api.ts +++ b/src/web/lib/api.ts @@ -375,22 +375,23 @@ export async function getChatMessages( botDid: string, collection: string = 'ai.syui.log.chat' ): Promise { - const messages: ChatMessage[] = [] - - // Load from both DIDs - for (const did of [userDid, botDid]) { + // Load messages for a single DID + async function loadForDid(did: string): Promise { // Try local first try { const res = await fetch(`/content/${did}/${collection}/index.json`) if (res.ok && isJsonResponse(res)) { const rkeys: string[] = await res.json() - for (const rkey of rkeys) { + // Load all messages in parallel + const msgPromises = rkeys.map(async (rkey) => { const msgRes = await fetch(`/content/${did}/${collection}/${rkey}.json`) if (msgRes.ok && isJsonResponse(msgRes)) { - messages.push(await msgRes.json()) + return msgRes.json() as Promise } - } - continue + return null + }) + const results = await Promise.all(msgPromises) + return results.filter((m): m is ChatMessage => m !== null) } } catch { // Try remote @@ -398,7 +399,7 @@ export async function getChatMessages( // Remote fallback const pds = await getPds(did) - if (!pds) continue + if (!pds) return [] try { const host = pds.replace('https://', '') @@ -406,13 +407,22 @@ export async function getChatMessages( const res = await fetch(url) if (res.ok) { const data: ListRecordsResponse = await res.json() - messages.push(...data.records) + return data.records } } catch { // Failed } + return [] } + // Load from both DIDs in parallel + const [userMessages, botMessages] = await Promise.all([ + loadForDid(userDid), + loadForDid(botDid) + ]) + + const messages = [...userMessages, ...botMessages] + // Sort by createdAt return messages.sort((a, b) => new Date(a.value.createdAt).getTime() - new Date(b.value.createdAt).getTime() diff --git a/src/web/main.ts b/src/web/main.ts index 943c6a6..4e38266 100644 --- a/src/web/main.ts +++ b/src/web/main.ts @@ -233,10 +233,12 @@ async function render(route: Route): Promise { const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai const aiHandle = 'ai.syui.ai' - // Load messages for the current user (did) and bot - const chatMessages = await getChatMessages(did, aiDid, 'ai.syui.log.chat') - const aiProfile = await getProfile(aiDid, false) - const pds = await getPds(did) + // Load messages and profiles in parallel + const [chatMessages, aiProfile, pds] = await Promise.all([ + getChatMessages(did, aiDid, 'ai.syui.log.chat'), + getProfile(aiDid, false), + getPds(did) + ]) // Collect available languages from chat messages const chatLangs = new Set() @@ -260,9 +262,12 @@ async function render(route: Route): Promise { const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai const aiHandle = 'ai.syui.ai' - const chatMessages = await getChatMessages(did, aiDid, 'ai.syui.log.chat') - const aiProfile = await getProfile(aiDid, false) - const pds = await getPds(did) + // Load messages and profiles in parallel + const [chatMessages, aiProfile, pds] = await Promise.all([ + getChatMessages(did, aiDid, 'ai.syui.log.chat'), + getProfile(aiDid, false), + getPds(did) + ]) // Collect available languages from chat messages const chatLangs = new Set()