From 49f4b7186686f0a31b31451f482e0434413d8359 Mon Sep 17 00:00:00 2001 From: syui Date: Tue, 20 Jan 2026 19:54:47 +0900 Subject: [PATCH] fix loading chat --- .../ai.syui.log.chat/index.json | 4 +-- src/web/lib/api.ts | 30 ++++++++++++------- src/web/main.ts | 19 +++++++----- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/public/content/did:plc:6qyecktefllvenje24fcxnie/ai.syui.log.chat/index.json b/public/content/did:plc:6qyecktefllvenje24fcxnie/ai.syui.log.chat/index.json index ffb418b..63d5928 100644 --- a/public/content/did:plc:6qyecktefllvenje24fcxnie/ai.syui.log.chat/index.json +++ b/public/content/did:plc:6qyecktefllvenje24fcxnie/ai.syui.log.chat/index.json @@ -1,5 +1,5 @@ [ - "kr5ig7vlgl276", + "d3dmreieiynnd", "evknoqtr7uyey", - "d3dmreieiynnd" + "kr5ig7vlgl276" ] \ No newline at end of file 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()