fix loading chat
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
[
|
[
|
||||||
"kr5ig7vlgl276",
|
"d3dmreieiynnd",
|
||||||
"evknoqtr7uyey",
|
"evknoqtr7uyey",
|
||||||
"d3dmreieiynnd"
|
"kr5ig7vlgl276"
|
||||||
]
|
]
|
||||||
@@ -375,22 +375,23 @@ export async function getChatMessages(
|
|||||||
botDid: string,
|
botDid: string,
|
||||||
collection: string = 'ai.syui.log.chat'
|
collection: string = 'ai.syui.log.chat'
|
||||||
): Promise<ChatMessage[]> {
|
): Promise<ChatMessage[]> {
|
||||||
const messages: ChatMessage[] = []
|
// Load messages for a single DID
|
||||||
|
async function loadForDid(did: string): Promise<ChatMessage[]> {
|
||||||
// Load from both DIDs
|
|
||||||
for (const did of [userDid, botDid]) {
|
|
||||||
// Try local first
|
// Try local first
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/content/${did}/${collection}/index.json`)
|
const res = await fetch(`/content/${did}/${collection}/index.json`)
|
||||||
if (res.ok && isJsonResponse(res)) {
|
if (res.ok && isJsonResponse(res)) {
|
||||||
const rkeys: string[] = await res.json()
|
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`)
|
const msgRes = await fetch(`/content/${did}/${collection}/${rkey}.json`)
|
||||||
if (msgRes.ok && isJsonResponse(msgRes)) {
|
if (msgRes.ok && isJsonResponse(msgRes)) {
|
||||||
messages.push(await msgRes.json())
|
return msgRes.json() as Promise<ChatMessage>
|
||||||
}
|
}
|
||||||
}
|
return null
|
||||||
continue
|
})
|
||||||
|
const results = await Promise.all(msgPromises)
|
||||||
|
return results.filter((m): m is ChatMessage => m !== null)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Try remote
|
// Try remote
|
||||||
@@ -398,7 +399,7 @@ export async function getChatMessages(
|
|||||||
|
|
||||||
// Remote fallback
|
// Remote fallback
|
||||||
const pds = await getPds(did)
|
const pds = await getPds(did)
|
||||||
if (!pds) continue
|
if (!pds) return []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const host = pds.replace('https://', '')
|
const host = pds.replace('https://', '')
|
||||||
@@ -406,13 +407,22 @@ export async function getChatMessages(
|
|||||||
const res = await fetch(url)
|
const res = await fetch(url)
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data: ListRecordsResponse<ChatMessage> = await res.json()
|
const data: ListRecordsResponse<ChatMessage> = await res.json()
|
||||||
messages.push(...data.records)
|
return data.records
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Failed
|
// 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
|
// Sort by createdAt
|
||||||
return messages.sort((a, b) =>
|
return messages.sort((a, b) =>
|
||||||
new Date(a.value.createdAt).getTime() - new Date(b.value.createdAt).getTime()
|
new Date(a.value.createdAt).getTime() - new Date(b.value.createdAt).getTime()
|
||||||
|
|||||||
@@ -233,10 +233,12 @@ async function render(route: Route): Promise<void> {
|
|||||||
const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai
|
const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai
|
||||||
const aiHandle = 'ai.syui.ai'
|
const aiHandle = 'ai.syui.ai'
|
||||||
|
|
||||||
// Load messages for the current user (did) and bot
|
// Load messages and profiles in parallel
|
||||||
const chatMessages = await getChatMessages(did, aiDid, 'ai.syui.log.chat')
|
const [chatMessages, aiProfile, pds] = await Promise.all([
|
||||||
const aiProfile = await getProfile(aiDid, false)
|
getChatMessages(did, aiDid, 'ai.syui.log.chat'),
|
||||||
const pds = await getPds(did)
|
getProfile(aiDid, false),
|
||||||
|
getPds(did)
|
||||||
|
])
|
||||||
|
|
||||||
// Collect available languages from chat messages
|
// Collect available languages from chat messages
|
||||||
const chatLangs = new Set<string>()
|
const chatLangs = new Set<string>()
|
||||||
@@ -260,9 +262,12 @@ async function render(route: Route): Promise<void> {
|
|||||||
const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai
|
const aiDid = 'did:plc:6qyecktefllvenje24fcxnie' // ai.syui.ai
|
||||||
const aiHandle = 'ai.syui.ai'
|
const aiHandle = 'ai.syui.ai'
|
||||||
|
|
||||||
const chatMessages = await getChatMessages(did, aiDid, 'ai.syui.log.chat')
|
// Load messages and profiles in parallel
|
||||||
const aiProfile = await getProfile(aiDid, false)
|
const [chatMessages, aiProfile, pds] = await Promise.all([
|
||||||
const pds = await getPds(did)
|
getChatMessages(did, aiDid, 'ai.syui.log.chat'),
|
||||||
|
getProfile(aiDid, false),
|
||||||
|
getPds(did)
|
||||||
|
])
|
||||||
|
|
||||||
// Collect available languages from chat messages
|
// Collect available languages from chat messages
|
||||||
const chatLangs = new Set<string>()
|
const chatLangs = new Set<string>()
|
||||||
|
|||||||
Reference in New Issue
Block a user