Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

20.8 KB
Raw Blame
import { completeCommunityPublication } from "@/lib/community-db/sync"
import { COMMUNITY_RESOURCE_CONTRACT_VERSION } from "@alook/shared"
import { COMMUNITY_CONTRACT_HEADER, CommunityMessageResourceSchema } from "@alook/shared"
import { CancelledError, hashKey, queryOptions, type InfiniteData, type Query, type QueryClient, type QueryKey } from "@tanstack/react-query"
import { apiFetchCommunity } from "@/lib/community/account-cache-lifecycle"
import { getCommunityDbRegistry } from "@/lib/community-db/collections"
import { assertCommunityLiveSnapshotTokenCurrent, captureCommunityLiveSnapshotToken, publishCommunityMessages, retireCommunityChannelReading, getCanonicalCommunityMessages } from "@/lib/community-db/sync"
import { ConversationReadTimeoutError, withConversationReadDeadline, isConversationAnchorMissingError } from "@/lib/community/conversation-read"
import { ApiError, isAbortError } from "@/lib/errors"
import { communityKeys } from "@/lib/query-keys"
import { getCommunityRuntime } from "@/stores/community/runtime"
import { messageScopeKey } from "@/stores/community/message-stream-store"
import type { ApiRequestOptions } from "@/lib/api/client"
import { actionAttributes, currentPageObservation } from "@/lib/observability/context"
import { emitTelemetry, isTelemetryEligible, telemetryGeneration } from "@/lib/observability/telemetry"
import { channelHref } from "@/lib/community/community-route"
import { messageWindowPage, type MessageInput, type MessagesPage, type MessagesReadPage, type MessagesPageParam, type MessagesWindowPage } from "@/lib/community/models/message"
import { buildMessagesUrl, messageWindowIdentity } from "@/lib/community/message-window"

type MessageCache = InfiniteData<MessagesWindowPage, MessagesPageParam>
type GapRepairScope = { kind: "channel" | "dm"; scopeId: string; serverId?: string }
function isMessageCache(value: unknown): value is MessageCache { return !!value && typeof value === "object" && Array.isArray((value as MessageCache).pages) && Array.isArray((value as MessageCache).pageParams) }
function messagesKey(scope: GapRepairScope) { return scope.kind === "channel" ? communityKeys.channelMessages(scope.scopeId) : communityKeys.dmMessages(scope.scopeId) }
function targetKey(key: QueryKey) { return [...key, "reconcile-target"] }
function isWindowKey(key: QueryKey) { return messageWindowIdentity(key) !== undefined }
function latest(cache: MessageCache, includeWatermark = true) { return Math.max(0, ...cache.pages.flatMap((page) => [...page.messages.map((message) => message.seq ?? 0), ...(includeWatermark ? [page.latestSeq ?? 0] : [])])) }
function cursor(cache: MessageCache) { return cache.pages.flatMap((page) => page.newestCursor ? [page.newestCursor] : []).sort().at(-1) ?? null }
function url(scopeId: string, param: MessagesPageParam, tag: string | null) {
  return buildMessagesUrl(`/api/community/channels/${scopeId}/messages`, param, tag)
}

function mergeWindow(cache: MessageCache, refreshed: MessagesPage, catchUp: MessagesPage[]): MessageCache {
  if (!cache.pages.length) return cache
  const incoming = new Map(messageWindowPage({ ...refreshed, messages: [refreshed, ...catchUp].flatMap((page) => page.messages) }).messages.map((message) => [message.id, message]))
  const pages = cache.pages.map((page) => ({ ...page, messages: page.messages.map((message) => { const replacement = incoming.get(message.id); incoming.delete(message.id); return replacement ?? message }) }))
  const newest = catchUp.at(-1) ?? refreshed
  const window = messageWindowPage(newest)
  pages[0] = { ...pages[0], messages: [...pages[0].messages, ...incoming.values()].sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0)), latestSeq: Math.max(latest(cache), refreshed.latestSeq ?? 0, ...catchUp.map((page) => page.latestSeq ?? 0)), hasMoreNewer: newest.hasMoreNewer ?? false, newerCursor: newest.newerCursor, newestCursor: [pages[0].newestCursor, window.newestCursor].filter((value): value is string => !!value).sort().at(-1) }
  return { ...cache, pages }
}

function cachedCoverage(queryClient: QueryClient, scopeId: string, cache: MessageCache, refreshed: MessagesPage, target: number, tag: string | null) {
  if (tag !== null || !Number.isSafeInteger(target)) return
  const byId = new Map<string, MessageInput>(getCanonicalCommunityMessages(queryClient, scopeId).map(row => [row.id, row]))
  for (const message of refreshed.messages) {
    const parsed = CommunityMessageResourceSchema.strip().safeParse(message)
    if (parsed.success && parsed.data.channelId === scopeId) byId.set(message.id, parsed.data)
  }
  const rows = [...byId.values()]
  const combined = mergeWindow(cache, refreshed, [])
  const loaded = [...new Map(combined.pages.flatMap(page => page.messages).map(row => [row.id, row])).values()]
  if (!loaded.length) return
  const references = loaded.map(reference => {
    const row = byId.get(reference.id)
    return row && Number.isSafeInteger(row.seq) && row.seq! >= 0
      && (reference.seq === undefined || row.seq === reference.seq) ? row : undefined
  })
  if (references.some(row => !row)) return
  const first = Math.min(...references.map(row => row!.seq!))
  const required = Math.max(target, ...references.map(row => row!.seq!))
  const bySeq = new Map<number, typeof rows[number] | null>()
  for (const row of rows) {
    if (row.seq === undefined || !Number.isSafeInteger(row.seq) || row.seq < first || row.seq > required) continue
    bySeq.set(row.seq, bySeq.has(row.seq) ? null : row)
  }
  const present = new Set(loaded.map(row => row.id))
  const messages: MessagesPage["messages"] = []
  let nextCursor: string | null = null, covered = first - 1
  for (let seq = first; seq <= required; seq++) {
    const row = bySeq.get(seq)
    if (!row?.createdAt || !Number.isFinite(Date.parse(row.createdAt)) || row.id.includes("|")) break
    const currentCursor = `${row.createdAt}|${row.id}`
    if (nextCursor !== null && currentCursor <= nextCursor) break
    nextCursor = currentCursor
    covered = seq
    if (!present.has(row.id)) messages.push(row)
  }
  if (!nextCursor) return
  return { cursor: nextCursor, covered, page: {
    messages, latestSeq: Math.max(latest(cache), refreshed.latestSeq ?? 0, covered), hasMoreNewer: covered < required,
    newerCursor: covered < required ? nextCursor : undefined,
  } satisfies MessagesPage }
}

type MessageRepairOwner = { query: Query; signal: AbortSignal; lifetime?: "query"; observation?: ApiRequestOptions["observation"] }
type MessageRepairReceipt = {
  token: ReturnType<typeof captureCommunityLiveSnapshotToken>
  window: Query | undefined
  scope: GapRepairScope
  key: QueryKey
  owner?: MessageRepairOwner
}

function repairReceipt(query: Query): MessageRepairReceipt | undefined {
  return query.meta?.communityMessageRepair as MessageRepairReceipt | undefined
}

function assertRepairCurrent(queryClient: QueryClient, receipt: MessageRepairReceipt, signal?: AbortSignal) {
  assertCommunityLiveSnapshotTokenCurrent(queryClient, receipt.token, signal)
  if (!receipt.window || queryClient.getQueryCache().find({ queryKey: receipt.key, exact: true }) !== receipt.window) {
    throw new DOMException("Retired message window", "AbortError")
  }
  if (receipt.window.isActive()) return
  const owner = receipt.owner
  if (!owner || owner.signal.aborted || queryClient.getQueryCache().get(owner.query.queryHash) !== owner.query
    || owner.lifetime !== "query" && !owner.query.isActive() && owner.query.state.fetchStatus === "idle") {
    throw new DOMException("Retired message repair owner", "AbortError")
  }
}

export function isFailedMessageRepair(queryClient: QueryClient, query: Query): boolean {
  const receipt = repairReceipt(query)
  if (!receipt?.window?.isActive() || query.state.status !== "error" || query.state.fetchStatus !== "idle"
    || isAbortError(query.state.error) || query.state.error instanceof CancelledError
    || isConversationAnchorMissingError(query.state.error)) return false
  try { assertRepairCurrent(queryClient, receipt); return true } catch { return false }
}

export function cancelMessageRepairs(queryClient: QueryClient, inactiveOnly = false) {
  return queryClient.cancelQueries({ predicate: (query) => {
    const receipt = repairReceipt(query)
    if (!receipt || query.state.fetchStatus === "idle") return false
    if (!inactiveOnly) return true
    try { assertRepairCurrent(queryClient, receipt); return false } catch { return true }
  } })
}

export function retryFailedMessageRepairs(queryClient: QueryClient) {
  return Promise.all(queryClient.getQueryCache().getAll()
    .filter((query) => isFailedMessageRepair(queryClient, query))
    .map((query) => {
      const receipt = repairReceipt(query)!
      return reconcileWindow(queryClient, receipt.scope, receipt.key)
    }))
}

export function messageReconcileOptions(queryClient: QueryClient, scope: GapRepairScope, key: QueryKey, landing?: { page: MessagesReadPage; param: MessagesPageParam }, owner?: MessageRepairOwner) {
  const scopeId = scope.scopeId
  const token = captureCommunityLiveSnapshotToken(queryClient, scopeId), registry = getCommunityDbRegistry(queryClient)
  const channel = registry?.collections.channels.get(scopeId)
  const serverId = scope.kind === "dm" ? null : channel ? channel.serverId : scope.serverId
  const originalQuery = queryClient.getQueryCache().find({ queryKey: key, exact: true })
  const receipt: MessageRepairReceipt = { token, window: originalQuery, scope, key, owner }
  const pendingDirection = originalQuery?.state.fetchStatus === "fetching" ? originalQuery.state.fetchMeta?.fetchMore?.direction : undefined
  let paginationReplayed = false
  let landingPage = landing
  let prefetched: { param: MessagesPageParam; page: MessagesReadPage } | undefined
  const repairKey: QueryKey = [...key, "reconcile"]
  return queryOptions({
    queryKey: repairKey, staleTime: 0,
    retry: (failureCount, error) => failureCount < 2 && (error instanceof ConversationReadTimeoutError
      || (error instanceof ApiError && [0, 500, 502, 503, 504].includes(error.status))),
    retryDelay: (attempt) => Math.min(1000 * 2 ** attempt, 4000) + Math.floor(Math.random() * 250),
    networkMode: "online",
    meta: { communityMessageRepair: receipt },
    queryFn: async ({ signal }) => {
      const path = scope.kind === "dm" ? `/c/me/${encodeURIComponent(scopeId)}` : serverId ? channelHref(serverId, scopeId) : undefined
      const observation = owner?.observation ?? (typeof window !== "undefined" && window.location.pathname === path
        ? { page: currentPageObservation(), reason: "background" as const } : { page: undefined, reason: "unknown" as const })
      const generation = telemetryGeneration()
      const observed = isTelemetryEligible()
      const record = (reconcile_stage: "start" | "settled" | "error" | "cancelled" | "retired") => {
        if (observed && generation === telemetryGeneration()) emitTelemetry("conversation.reconcile", {
          ...actionAttributes(observation?.action ?? observation?.page), reconcile_stage, request_role: "reconcile",
        })
      }
      record("start")
      const assert = () => assertRepairCurrent(queryClient, receipt, signal)
      try {
        await registry?.ready
        assert()
        const data = originalQuery?.state.data
        if (!isMessageCache(data) || !data.pageParams[0]) { record("retired"); return { latestSeq: 0 } }
        await queryClient.cancelQueries({ queryKey: key, exact: true }, { revert: true, silent: true })
        assert()
        const tag = messageWindowIdentity(key)?.tag ?? null
        const load = async (param: MessagesPageParam) => {
          assert()
          const page = await withConversationReadDeadline(signal, (readSignal) => apiFetchCommunity<MessagesReadPage>(
            url(scopeId, param, tag), { signal: readSignal, assertActive: assert, headers: { [COMMUNITY_CONTRACT_HEADER]: String(COMMUNITY_RESOURCE_CONTRACT_VERSION) },
              observation: { ...observation, reason: "background", role: "reconcile", windowMode: param.mode } }, token,
          ))
          assert()
          return page
        }
        const target = () => queryClient.getQueryData<number>(targetKey(key)) ?? 0
        let cached: MessagesPage | undefined
        let cachedCovered = 0
        const published = new Set<MessagesReadPage>()
        const prefetch = async (current: MessageCache, refreshed: MessagesReadPage) => {
          if (prefetched) return
          const required = Math.max(refreshed.latestSeq ?? 0, target())
          const coverage = cachedCoverage(queryClient, scopeId, current, refreshed, required, tag)
          cached = coverage?.page
          cachedCovered = coverage?.covered ?? 0
          if (coverage ? coverage.covered >= required : required <= latest(current, false)) return
          if (cached?.messages.length) await publish(refreshed)
          const nextCursor = coverage?.cursor ?? cursor(current)
          if (!nextCursor && (refreshed.latestSeq ?? 0) >= target()) return
          const param: MessagesPageParam = nextCursor ? { mode: "since", since: nextCursor } : current.pageParams[0]
          prefetched = { param, page: await load(param) }
          const after = originalQuery?.state.data
          if (isMessageCache(after)) {
            const remaining = cachedCoverage(queryClient, scopeId, after, {
              ...prefetched.page, messages: [...refreshed.messages, ...prefetched.page.messages],
            }, Math.max(required, target(), prefetched.page.latestSeq ?? 0), tag)
            if (remaining) { cached = remaining.page; cachedCovered = remaining.covered }
          }
        }
        const publish = async (refreshed: MessagesReadPage) => {
          assert()
          const fetchedEnd = Math.max(0, ...(prefetched?.page.messages.map(message => message.seq ?? 0) ?? []))
          const catchUp = cached && prefetched && cachedCovered >= fetchedEnd
            ? [prefetched.page, cached] : [...(cached ? [cached] : []), ...(prefetched ? [prefetched.page] : [])]
          const pending = [refreshed, ...(prefetched ? [prefetched.page] : [])].filter(page => !published.has(page))
          if (pending.length) {
            await publishCommunityMessages(queryClient, { channelId: scopeId, messages: pending.flatMap(page => page.messages), relatedMessages: pending.flatMap(page => page.relatedMessages ?? []), profiles: [...new Map(pending.flatMap(page => page.profiles).map(profile => [profile.id, profile])).values()], proof: { token, signal } })
            for (const page of pending) published.add(page)
          }
          queryClient.setQueryData<MessageCache>(key, (cache) => isMessageCache(cache) ? mergeWindow(cache, refreshed, catchUp) : cache)
        }
        const current = originalQuery?.state.data
        if (!isMessageCache(current) || !current.pageParams[0]) { record("retired"); return { latestSeq: 0 } }
        if (!landingPage || hashKey([landingPage.param]) !== hashKey([current.pageParams[0]])) {
          landingPage = { param: current.pageParams[0], page: await load(current.pageParams[0]) }
        }
        const refreshed = landingPage.page
        if (!isMessageCache(originalQuery?.state.data)) { record("retired"); return { latestSeq: 0 } }
        await prefetch(current, refreshed)
        await publish(refreshed)
        if (pendingDirection && !paginationReplayed) {
          paginationReplayed = true
          await originalQuery?.fetch(undefined, { meta: { fetchMore: { direction: pendingDirection } } })
          assert()
        }
        const afterPagination = originalQuery?.state.data
        if (!prefetched && isMessageCache(afterPagination) && target() > latest(afterPagination, false)) {
          await prefetch(afterPagination, refreshed)
          await publish(refreshed)
        }
        assert()
        const latestSeq = Math.max(refreshed.latestSeq ?? 0, prefetched?.page.latestSeq ?? 0, cachedCovered)
        if (latestSeq < target() && !(prefetched?.page.hasMoreNewer && prefetched.page.newerCursor)) {
          throw new Error("Messages have not caught up yet")
        }
        record("settled")
        return { latestSeq }
      } catch (error) {
        record(signal.aborted ? "cancelled" : isAbortError(error) ? "retired" : "error")
        assert()
        if (!(error instanceof ApiError) || (error.status !== 403 && error.status !== 404) || isConversationAnchorMissingError(error)) throw error
        if (registry) await completeCommunityPublication(registry, () => retireCommunityChannelReading(registry, scopeId, {
          reason: "read-denied", serverId,
          preserveQuery: queryClient.getQueryCache().find({ queryKey: [...key, "reconcile"], exact: true }),
        }))
        return { latestSeq: 0 }
      }
    },
  })
}

export function reconcileWindow(queryClient: QueryClient, scope: GapRepairScope, key: QueryKey, target = 0, landing?: { page: MessagesReadPage; param: MessagesPageParam }, owner?: MessageRepairOwner) {
  const query = queryClient.getQueryCache().find({ queryKey: key, exact: true })
  if (!query) return Promise.resolve()
  queryClient.setQueryData<number>(targetKey(key), (current) => Math.max(current ?? 0, target))
  if (!isMessageCache(query.state.data)) return query.fetch().then(() => undefined)
  const repairKey = [...key, "reconcile"]
  const repair = queryClient.getQueryCache().find({ queryKey: repairKey, exact: true })
  const receipt = repair && repairReceipt(repair)
  if (receipt) {
    let current = receipt.window === query
    try { assertRepairCurrent(queryClient, receipt) } catch { current = false }
    if (!current) queryClient.removeQueries({ queryKey: repairKey, exact: true })
    else if (repair.state.fetchStatus !== "idle" && repair.promise) {
      return repair.promise.then(() => undefined).catch((error: unknown) => {
        if (!isAbortError(error) && !(error instanceof CancelledError)) throw error
      })
    }
  }
  const options = messageReconcileOptions(queryClient, scope, key, landing, owner)
  const cancelRetiredOwner = () => {
    const current = queryClient.getQueryCache().find({ queryKey: repairKey, exact: true })
    if (current?.meta?.communityMessageRepair === options.meta?.communityMessageRepair && !query.isActive()) {
      void queryClient.cancelQueries({ queryKey: repairKey, exact: true })
    }
  }
  owner?.signal.addEventListener("abort", cancelRetiredOwner, { once: true })
  const unsubscribe = owner?.lifetime === "query" ? queryClient.getQueryCache().subscribe(event => {
    if (event.type === "removed" && event.query === owner.query) cancelRetiredOwner()
  }) : undefined
  return queryClient.query({ ...options, select: undefined }).then(() => undefined).catch((error: unknown) => {
    if (!isAbortError(error) && !(error instanceof CancelledError)) throw error
  }).finally(() => {
    owner?.signal.removeEventListener("abort", cancelRetiredOwner)
    unsubscribe?.()
    const current = queryClient.getQueryCache().find({ queryKey: repairKey, exact: true })
    if (current?.meta?.communityMessageRepair === options.meta?.communityMessageRepair && current?.state.status !== "error") {
      queryClient.removeQueries({ queryKey: targetKey(key), exact: true })
    }
  })
}

export function reconcileConversationLanding(queryClient: QueryClient, scope: GapRepairScope, key: QueryKey, page: MessagesReadPage, param: MessagesPageParam, owner: MessageRepairOwner) {
  const data = queryClient.getQueryData(key)
  const highWater = page.latestSeq ?? 0
  if (!isMessageCache(data)) return Promise.resolve()
  const ids = new Set(data.pages.flatMap(part => part.messages.map(message => message.id)))
  if (highWater <= latest(data, false) && page.messages.every(message => ids.has(message.id))) return Promise.resolve()
  return reconcileWindow(queryClient, scope, key, highWater, { page, param }, owner)
}

function reconcile(queryClient: QueryClient, scope: GapRepairScope, target = 0) {
  const queries = queryClient.getQueryCache().findAll({ queryKey: messagesKey(scope), type: "active" }).filter((query) => isWindowKey(query.queryKey))
  return Promise.all(queries.map(query => reconcileWindow(queryClient, scope, query.queryKey, target))).then(() => undefined)
}

export function scheduleFocusedMessageGapRepair(queryClient: QueryClient, scope: GapRepairScope, incomingSeq: number): Promise<void> | null {
  const queries = queryClient.getQueryCache().findAll({ queryKey: messagesKey(scope), type: "active" }).filter((query) => isWindowKey(query.queryKey))
  const windowSeq = Math.max(0, ...queries.flatMap((query) => isMessageCache(query.state.data) ? [latest(query.state.data, false)] : []))
  const ids = getCommunityRuntime(queryClient).messageStream.get().entries.get(messageScopeKey({ kind: scope.kind, id: scope.scopeId }))?.state.liveIds ?? []
  const messages = getCommunityDbRegistry(queryClient)?.collections.messages
  const known = Math.max(windowSeq, ...ids.map((id) => messages?.get(id)?.seq ?? 0), 0)
  return incomingSeq > known + 1 ? reconcile(queryClient, scope, incomingSeq).catch(() => undefined) : null
}

export function reconcileFocusedMessageQueries(queryClient: QueryClient, kind: "channel" | "dm", scopeId: string, serverId?: string): Promise<void> { return reconcile(queryClient, { kind, scopeId, serverId }) }