test ai chat

This commit is contained in:
2026-01-20 13:46:28 +09:00
parent 6f5290753d
commit 1d3aa51fb6
27 changed files with 1463 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
import { xrpcUrl, comAtprotoIdentity, comAtprotoRepo } from '../lexicons'
import type { AppConfig, Networks, Profile, Post, ListRecordsResponse } from '../types'
import type { AppConfig, Networks, Profile, Post, ListRecordsResponse, ChatMessage } from '../types'
// Cache
let configCache: AppConfig | null = null
@@ -368,3 +368,53 @@ export interface SearchPost {
}
record: unknown
}
// Load chat messages from both user and bot repos
export async function getChatMessages(
userDid: string,
botDid: string,
collection: string = 'ai.syui.log.chat'
): Promise<ChatMessage[]> {
const messages: ChatMessage[] = []
// Load from both DIDs
for (const did of [userDid, botDid]) {
// 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) {
const msgRes = await fetch(`/content/${did}/${collection}/${rkey}.json`)
if (msgRes.ok && isJsonResponse(msgRes)) {
messages.push(await msgRes.json())
}
}
continue
}
} catch {
// Try remote
}
// Remote fallback
const pds = await getPds(did)
if (!pds) continue
try {
const host = pds.replace('https://', '')
const url = `${xrpcUrl(host, comAtprotoRepo.listRecords)}?repo=${did}&collection=${collection}&limit=100`
const res = await fetch(url)
if (res.ok) {
const data: ListRecordsResponse<ChatMessage> = await res.json()
messages.push(...data.records)
}
} catch {
// Failed
}
}
// Sort by createdAt
return messages.sort((a, b) =>
new Date(a.value.createdAt).getTime() - new Date(b.value.createdAt).getTime()
)
}

View File

@@ -1,5 +1,5 @@
export interface Route {
type: 'home' | 'user' | 'post' | 'postpage' | 'atbrowser' | 'service' | 'collection' | 'record'
type: 'home' | 'user' | 'post' | 'postpage' | 'atbrowser' | 'service' | 'collection' | 'record' | 'chat' | 'chat-thread'
handle?: string
rkey?: string
service?: string
@@ -51,6 +51,18 @@ export function parseRoute(): Route {
return { type: 'postpage', handle: postPageMatch[1] }
}
// Chat thread: /@handle/at/chat/{rkey}
const chatThreadMatch = path.match(/^\/@([^/]+)\/at\/chat\/([^/]+)$/)
if (chatThreadMatch) {
return { type: 'chat-thread', handle: chatThreadMatch[1], rkey: chatThreadMatch[2] }
}
// Chat list: /@handle/at/chat
const chatMatch = path.match(/^\/@([^/]+)\/at\/chat\/?$/)
if (chatMatch) {
return { type: 'chat', handle: chatMatch[1] }
}
// Post detail page: /@handle/rkey (for config.collection)
const postMatch = path.match(/^\/@([^/]+)\/([^/]+)$/)
if (postMatch) {
@@ -79,6 +91,10 @@ export function navigate(route: Route): void {
path = `/@${route.handle}/at/collection/${route.collection}`
} else if (route.type === 'record' && route.handle && route.collection && route.rkey) {
path = `/@${route.handle}/at/collection/${route.collection}/${route.rkey}`
} else if (route.type === 'chat' && route.handle) {
path = `/@${route.handle}/at/chat`
} else if (route.type === 'chat-thread' && route.handle && route.rkey) {
path = `/@${route.handle}/at/chat/${route.rkey}`
}
window.history.pushState({}, '', path)