import type { PluginTheme } from "@getpaseo/plugin"; import { openExternalUrl, usePaseo, type PluginTimelineItemProps } from "@getpaseo/plugin/client"; import { Icon, copyText, useRevealedText, useToast } from "@getpaseo/plugin/client/react-native"; import { Markdown } from "@getpaseo/plugin/client/ui"; import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Pressable, StyleSheet, Text, View } from "react-native"; import { collectLoads, compileStreaming, dataKey, evaluate, initialState, type LoadedData, type Program, } from "../shared/core"; import type { Action, Diagnostic, Value } from "../shared/core/types"; import type { GenUiBlockData, GenUiTextData } from "../shared/rpc"; import { useAudioLoader, useImageLoader, useLoadedData, useTextLoader, type FileScope } from "./loaders"; import { composeReply } from "../shared/logic/reply"; import { RenderNodes } from "./render"; import { clearQueue, queueOf, setQueued as setQueuedFor, useReplyQueue } from "./reply-queue"; import { UiRuntimeContext, type UiRuntime } from "./runtime"; import { canRenderWebIframe, mountSandboxedAppIframe, openWorkspaceFile, readStorage, writeStorage } from "./web"; function readStoredValues(storageKey: string): Record { const raw = readStorage(storageKey); if (raw === null) return {}; try { const saved: unknown = JSON.parse(raw); if (typeof saved !== "object" || saved === null || Array.isArray(saved)) return {}; return Object.fromEntries(Object.entries(saved as Record)); } catch { return {}; } } export function LiveUi({ program, compileDiagnostics, source, stateKey, data, send, openFile, loadImage, readText, loadAudio, playAudio, superseded = false, agentId, blockKey, theme, compact, platform, footerLabel, }: { program: Program; compileDiagnostics: readonly Diagnostic[]; source: string; stateKey: string; data?: ReadonlyMap; send(text: string): Promise; openFile: UiRuntime["openFile"]; loadImage: UiRuntime["loadImage"]; readText: UiRuntime["readText"]; loadAudio: UiRuntime["loadAudio"]; playAudio: UiRuntime["playAudio"]; superseded?: boolean; agentId: string | null; blockKey: string | null; theme: PluginTheme; compact: boolean; platform: "ios" | "android" | "web"; footerLabel?: string; }) { const toast = useToast(); const initial = useMemo(() => initialState(program, data), [program, data]); const [changed, setChanged] = useState(() => readStoredValues(stateKey)); const state = useMemo( () => Object.fromEntries( Object.entries(initial).map(([name, value]) => [ name, Object.hasOwn(changed, name) ? changed[name]! : value, ]), ), [initial, changed], ); useEffect(() => { if (Object.keys(changed).length === 0) return; writeStorage(stateKey, JSON.stringify(changed)); }, [stateKey, changed]); const answerKey = `${stateKey}:answer`; const [answer, setAnswer] = useState(() => readStorage(answerKey)); const setValue = useCallback( (name: string, value: Value) => { if (Object.hasOwn(initial, name)) { setChanged((prev) => ({ ...prev, [name]: value })); } }, [initial], ); // The gallery has no agent; its UIs share one local queue. const queueAgent = agentId ?? "local"; const queue = useReplyQueue(queueAgent); const queuePrefix = blockKey ?? stateKey; const setQueued = useCallback( (id: string, text: string | null) => setQueuedFor(queueAgent, `${queuePrefix}:${id}`, text), [queueAgent, queuePrefix], ); const runAction = useCallback( async (action: Action) => { switch (action.type) { case "set": setValue(action.name, action.value); return; case "queue": { const id = `queue:${action.text}`; const queued = queueOf(queueAgent).some((item) => item.id === `${queuePrefix}:${id}`); setQueued(id, queued ? null : action.text); return; } case "send": await send(composeReply(queueOf(queueAgent).map((item) => item.text), action.text)); clearQueue(queueAgent); setAnswer(action.text); writeStorage(answerKey, action.text); return; case "openUrl": await openExternalUrl(action.url); return; case "copy": await copyText(action.text); toast.show("Copied", { variant: "success" }); return; } }, [answerKey, queueAgent, queuePrefix, send, setQueued, setValue, toast], ); const runtime = useMemo( () => ({ setValue, openFile, loadImage, readText, loadAudio, playAudio, answer, queue, setQueued, queued: (id) => queue.find((item) => item.id === `${queuePrefix}:${id}`)?.text ?? null, superseded, agentId, blockKey, theme, compact, platform, async run(actions) { try { for (const action of actions) await runAction(action); } catch (cause) { toast.error(cause instanceof Error ? cause.message : String(cause)); } }, }), [ setValue, openFile, loadImage, readText, loadAudio, playAudio, answer, queue, queuePrefix, setQueued, superseded, agentId, blockKey, theme, compact, platform, runAction, toast, ], ); const { roots, diagnostics } = useMemo( () => evaluate(program, state, data), [program, state, data], ); const allDiagnostics = useMemo( () => [...compileDiagnostics, ...diagnostics], [compileDiagnostics, diagnostics], ); const files = useMemo(() => collectLoads(program), [program]); return ( ); } function SourceFooter({ source, diagnostics, fileCount, theme, label, }: { source: string; diagnostics: readonly Diagnostic[]; fileCount: number; theme: PluginTheme; label?: string; }) { const [open, setOpen] = useState(false); const toast = useToast(); return ( {label ? ( {label} ) : ( )} {diagnostics.length > 0 ? ( {diagnostics.length} warning{diagnostics.length === 1 ? "" : "s"} ) : null} setOpen((o) => !o)} style={{ flexDirection: "row", alignItems: "center", gap: 4 }} > {open ? "Hide source" : "Source"} {fileCount > 0 ? ` (${fileCount} file${fileCount === 1 ? "" : "s"})` : ""} {open ? ( Spec source { void copyText(source).then( () => toast.show("Source copied", { variant: "success" }), () => toast.error("Could not copy"), ); }} > {source} {diagnostics.length > 0 ? ( {diagnostics.map((d, i) => ( {d.line}:{d.column} {d.severity}: {d.message} ))} ) : null} ) : null} ); } export function AppBlockView({ html, complete, theme, }: { html: string; complete: boolean; theme: PluginTheme; }) { const containerRef = useRef(null); const [height, setHeight] = useState(180); const [error, setError] = useState(null); const webSupported = canRenderWebIframe(); useEffect(() => { if (!complete || !webSupported || !containerRef.current) return; setError(null); return mountSandboxedAppIframe(containerRef.current, { html, theme, height, onHeight: setHeight, onError: setError, }); }, [complete, html, theme, webSupported]); if (!complete) { return ( Building app… ); } return ( {webSupported ? ( ) : ( Interactive HTML app (open on desktop or web to run): {html} )} {error ? ( {error} ) : null} ); } export function SpecBlockView({ code, complete, stateKey, scope, send, openFile, playAudio, theme, compact, platform, footerLabel, }: { code: string; complete: boolean; stateKey: string; scope: FileScope; send(text: string): Promise; openFile: UiRuntime["openFile"]; playAudio: UiRuntime["playAudio"]; theme: PluginTheme; compact: boolean; platform: "ios" | "android" | "web"; footerLabel?: string; }) { const result = useMemo(() => compileStreaming(code), [code]); const lastGood = useRef(result.program === null ? null : result); if (result.program !== null) lastGood.current = result; const shown = lastGood.current; const data = useLoadedData(scope, shown?.program ?? null); const loadImage = useImageLoader(scope); const readText = useTextLoader(scope); const loadAudio = useAudioLoader(scope); if (!shown?.program) { if (!complete) { return ( Building UI… ); } return ( This UI couldn't be rendered {result.diagnostics.map((d, idx) => ( {d.line}:{d.column} {d.severity}: {d.message} ))} ); } const agentId = scope === "gallery" ? null : scope.agentId; return ( ); } export function TimelineBlockItem({ agentId, item, timestamp, theme, layout, playAudio, }: PluginTimelineItemProps & { playAudio: UiRuntime["playAudio"]; }) { const paseo = usePaseo(); const { language, code, complete, fenceIndex, messageId } = item.data; const stateKey = `paseo.genui:${agentId}:${messageId ?? timestamp.getTime()}:${fenceIndex}`; const send = useCallback( async (text: string) => { await paseo.agents.ref(agentId).send(text); }, [paseo, agentId], ); if (language === "app") { return ; } return ( ); } export function TimelineTextItem({ item, theme, layout, }: PluginTimelineItemProps) { const revealed = useRevealedText(item.data.text, item.data.phase ?? "complete"); // Paseo's own renderer and testID give this text the host's links, selection and copy, and plugin styles such as Chat Polish. return ( {revealed} ); }