import { useRpc } from "@getpaseo/plugin/client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { collectLoads, dataKey, type DataRequest, type LoadedData, type Program } from "../shared/core"; import type { EnvironmentDetailResult } from "../shared/core/environments"; import type { ChangesResult, CommitsResult, EnvironmentsResult, PatchesResult, PullRequestResult, ThreadsResult, } from "../shared/core/project"; import { galleryAudioRpc, galleryDataRpc, galleryImageRpc, loadAudioRpc, loadDataRpc, loadImageRpc, projectChangesRpc, projectCommitsRpc, projectEnvironmentRpc, projectEnvironmentsRpc, projectPatchesRpc, projectPullRequestRpc, projectThreadsRpc, webGeocodeRpc, webImageRpc, webSearchRpc, webTileRpc, type FoundImage, type Located, } from "../shared/rpc"; import type { ImageUrl, TextFile } from "./runtime"; export type FileScope = { readonly agentId: string } | "gallery"; type LoadedMedia = { ok: true; dataUrl: string } | { ok: false; error: string; off?: boolean }; interface Loaders { data(path: string, raw: boolean | undefined): Promise; image(path: string): Promise; audio(path: string): Promise; } const LOADED_CACHE = new Map(); const MAX_LOADED = 200; function rememberLoaded(key: string, loaded: LoadedData): void { if (!loaded.ok) return; LOADED_CACHE.delete(key); LOADED_CACHE.set(key, loaded); if (LOADED_CACHE.size > MAX_LOADED) { const first = LOADED_CACHE.keys().next().value; if (first !== undefined) LOADED_CACHE.delete(first); } } export function useLoaders(scope: FileScope): Loaders { const loadData = useRpc(loadDataRpc); const loadImage = useRpc(loadImageRpc); const loadAudio = useRpc(loadAudioRpc); const galleryData = useRpc(galleryDataRpc); const galleryImage = useRpc(galleryImageRpc); const galleryAudio = useRpc(galleryAudioRpc); const webImage = useRpc(webImageRpc); const agentId = scope === "gallery" ? null : scope.agentId; return useMemo(() => { const fileImage: Loaders["image"] = agentId === null ? (path) => galleryImage({ path }) : (path) => loadImage({ agentId, path }); return { data: agentId === null ? (path, raw) => galleryData({ path, raw }) : (path, raw) => loadData({ agentId, path, raw }), image: (src) => (/^https?:\/\//i.test(src) ? webImage({ url: src }) : fileImage(src)), audio: agentId === null ? (path) => galleryAudio({ path }) : (path) => loadAudio({ agentId, path }), }; }, [agentId, galleryAudio, galleryData, galleryImage, loadAudio, loadData, loadImage, webImage]); } export function useLoadedData(scope: FileScope, program: Program | null): ReadonlyMap { const loaders = useLoaders(scope); const scopeKey = scope === "gallery" ? "gallery" : scope.agentId; const requestsKey = useMemo( () => (program === null ? "[]" : JSON.stringify(collectLoads(program))), [program], ); const [data, setData] = useState>(() => { const found = new Map(); for (const request of JSON.parse(requestsKey) as DataRequest[]) { const hit = LOADED_CACHE.get(`${scopeKey}|${dataKey(request)}`); if (hit !== undefined) found.set(dataKey(request), hit); } return found; }); useEffect(() => { const requests = JSON.parse(requestsKey) as DataRequest[]; const missing = requests.filter((request) => !data.has(dataKey(request))); if (missing.length === 0) return; let live = true; for (const request of missing) { const key = dataKey(request); loaders.data(request.path, request.raw).then( (loaded) => { rememberLoaded(`${scopeKey}|${key}`, loaded); if (live) setData((previous) => new Map(previous).set(key, loaded)); }, (cause: unknown) => { const error = cause instanceof Error ? cause.message : String(cause); if (live) setData((previous) => new Map(previous).set(key, { ok: false, error })); }, ); } return () => { live = false; }; }, [loaders, requestsKey, scopeKey]); return data; } export function useImageLoader(scope: FileScope): (src: string) => Promise { const loaders = useLoaders(scope); const images = useRef(new Map>()); return useCallback( (src: string) => { const cached = images.current.get(src); if (cached !== undefined) return cached; const pending = loaders.image(src).then( (loaded): ImageUrl => (loaded.ok ? { ok: true, url: loaded.dataUrl } : loaded), (cause: unknown): ImageUrl => ({ ok: false, error: cause instanceof Error ? cause.message : String(cause), }), ); images.current.set(src, pending); void pending.then((image) => { if (!image.ok) images.current.delete(src); }); return pending; }, [loaders], ); } export function useAudioLoader(scope: FileScope): (src: string) => Promise { const loaders = useLoaders(scope); const audios = useRef(new Map>()); return useCallback( (src: string) => { const cached = audios.current.get(src); if (cached !== undefined) return cached; const pending = loaders.audio(src).then( (loaded): ImageUrl => (loaded.ok ? { ok: true, url: loaded.dataUrl } : loaded), (cause: unknown): ImageUrl => ({ ok: false, error: cause instanceof Error ? cause.message : String(cause), }), ); audios.current.set(src, pending); void pending.then((audio) => { if (!audio.ok) audios.current.delete(src); }); return pending; }, [loaders], ); } export function useTextLoader(scope: FileScope): (path: string) => Promise { const loaders = useLoaders(scope); const scopeKey = scope === "gallery" ? "gallery" : scope.agentId; return useCallback( async (path: string): Promise => { const key = `${scopeKey}|${dataKey({ path, raw: true })}`; const toText = (loaded: LoadedData): TextFile => !loaded.ok ? loaded : typeof loaded.value === "string" ? { ok: true, text: loaded.value } : { ok: false, error: `${path} is not a text file` }; const cached = LOADED_CACHE.get(key); if (cached !== undefined) return toText(cached); try { const loaded = await loaders.data(path, true); rememberLoaded(key, loaded); return toText(loaded); } catch (cause) { return { ok: false, error: cause instanceof Error ? cause.message : String(cause) }; } }, [loaders, scopeKey], ); } export type SearchResult = | { readonly ok: true; readonly image: FoundImage } | { readonly ok: false; readonly error: string; readonly off?: boolean }; const searchCache = new Map>(); export function useImageSearch(query: string): SearchResult | null { const webSearch = useRpc(webSearchRpc); const key = query.trim().replace(/\s+/g, " ").toLowerCase(); const [result, setResult] = useState<{ readonly key: string; readonly result: SearchResult } | null>(null); useEffect(() => { if (key === "") return; let live = true; let pending = searchCache.get(key); if (!pending) { pending = webSearch({ query: key }).catch((cause: unknown) => ({ ok: false as const, error: cause instanceof Error ? cause.message : String(cause), })); searchCache.set(key, pending); void pending.then((res) => { if (!res.ok) searchCache.delete(key); }); } void pending.then((found) => { if (live) setResult({ key, result: found }); }); return () => { live = false; }; }, [webSearch, key]); return result !== null && result.key === key ? result.result : null; } export type PlaceResult = | { readonly ok: true; readonly lat: number; readonly lng: number } | { readonly ok: false; readonly error: string; readonly off?: boolean }; const placeCache = new Map>(); export function useGeocodePlace() { const webGeocode = useRpc(webGeocodeRpc); return useCallback( (name: string, near: Located | null): Promise => { const bias = near === null ? "" : `${near.lat.toFixed(1)},${near.lng.toFixed(1)}`; const key = `${name.trim().toLowerCase()}|${bias}`; const existing = placeCache.get(key); if (existing !== undefined) return existing; const pending = webGeocode({ name: name.trim(), near }).catch((cause: unknown) => ({ ok: false as const, error: cause instanceof Error ? cause.message : String(cause), })); placeCache.set(key, pending); void pending.then((res) => { if (!res.ok) placeCache.delete(key); }); return pending; }, [webGeocode], ); } const tileCache = new Map(); const tileRequests = new Map>(); export function useMapTileLoader() { const webTile = useRpc(webTileRpc); return useCallback( async (z: number, x: number, y: number) => { const key = `${z}/${x}/${y}`; const known = tileCache.get(key); if (known !== undefined) return { ok: true as const, dataUrl: known }; let pending = tileRequests.get(key); if (!pending) { pending = webTile({ z, x, y }).catch((cause: unknown) => ({ ok: false as const, error: cause instanceof Error ? cause.message : String(cause), })); tileRequests.set(key, pending); } const res = await pending; tileRequests.delete(key); if (res.ok) { tileCache.set(key, res.dataUrl); if (tileCache.size > 400) { const oldest = tileCache.keys().next().value; if (oldest !== undefined) tileCache.delete(oldest); } } return res; }, [webTile], ); } export function useProjectRpc() { const changes = useRpc(projectChangesRpc); const patches = useRpc(projectPatchesRpc); const commits = useRpc(projectCommitsRpc); const pullRequest = useRpc(projectPullRequestRpc); const threads = useRpc(projectThreadsRpc); const environments = useRpc(projectEnvironmentsRpc); const environment = useRpc(projectEnvironmentRpc); return useMemo( () => ({ changes: (input: { agentId: string; target: string | null; paths: string[] | null; blockKey: string | null; refresh: boolean; }): Promise => changes(input), patches: (input: { agentId: string; target: string | null; paths: string[]; }): Promise => patches(input), commits: (input: { agentId: string; blockKey: string | null; refresh: boolean; }): Promise => commits(input), pullRequest: (input: { agentId: string }): Promise => pullRequest(input), threads: (input: { agentId: string; ids: string[] | null }): Promise => threads(input), environments: (input: { agentId: string }): Promise => environments(input), environment: (input: { agentId: string; environmentId: string; }): Promise => environment(input), }), [changes, commits, environment, environments, patches, pullRequest, threads], ); }