import { Icon } from "@getpaseo/plugin/client/react-native"; import { ExternalLink } from "@getpaseo/plugin/client/ui"; import React, { useEffect, useMemo, useState } from "react"; import { Pressable, StyleSheet, Text, View } from "react-native"; import { acceptanceVerdict, checkLabel, readAcceptance, verdictLine } from "../../shared/core/acceptance"; import { readCompare } from "../../shared/core/compare"; import { changeTotals, checkCounts, parseChangesTarget, parseCommitLimit, parsePathFilter, parseThreadIds, scopeLabel, threadStatusLabel, type ChangesResult, type CommitsResult, type EnvironmentsResult, type PullRequestResult, type ThreadsResult, } from "../../shared/core/project"; import { chainSummary, mismatchText, readProvenance } from "../../shared/core/provenance"; import { analyse, countsText as werCountsText, formatWer, readTracks } from "../../shared/core/transcript"; import { optionalText, type Props } from "../../shared/logic/values"; import { useProjectRpc } from "../loaders"; import { ErrorLine } from "../node-renderer"; import { useUiRuntime } from "../runtime"; import { LoadedImageView } from "./content"; import { Diff } from "./data"; const CHANGES_ROWS = 50; export function Changes({ props }: { props: Props }) { const { theme, agentId, blockKey } = useUiRuntime(); const projectRpc = useProjectRpc(); const rawTarget = optionalText(props.target); const parsedPaths = parsePathFilter(props.paths); const [state, setState] = useState(null); const [expandedFile, setExpandedFile] = useState(null); // Keyed by the request so a new target or path filter starts collapsed again. const requestKey = `${rawTarget ?? ""}|${JSON.stringify(parsedPaths.ok ? parsedPaths.value : null)}`; const [expandedFor, setExpandedFor] = useState(null); const showAll = expandedFor === requestKey; useEffect(() => { const parsedTarget = parseChangesTarget(rawTarget); if (!parsedTarget.ok || !parsedPaths.ok) return; let live = true; void projectRpc .changes({ agentId: agentId ?? "gallery", target: rawTarget, paths: parsedPaths.value ? [...parsedPaths.value] : null, blockKey, refresh: false, }) .then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [agentId, blockKey, parsedPaths, projectRpc, rawTarget]); if (!parsedPaths.ok) return ; if (state === null) { return ( Reading changes… ); } if (!state.ok) return ; const totals = changeTotals(state.files); const patchByPath = new Map(state.patches.map((p) => [p.path, p.patch])); const shownFiles = showAll ? state.files : state.files.slice(0, CHANGES_ROWS); const hiddenFiles = state.files.length - shownFiles.length; return ( {scopeLabel(state.scope)} {totals.files} files ·{" "} +{totals.additions}{" "} −{totals.deletions} {shownFiles.map((file, idx) => { const patch = patchByPath.get(file.path); const isOpen = expandedFile === file.path; return ( setExpandedFile(isOpen ? null : file.path)} style={{ flexDirection: "row", justifyContent: "space-between", paddingHorizontal: 10, paddingVertical: 7, }} > {file.path} +{file.additions}{" "} −{file.deletions} {isOpen && patch ? ( ) : null} ); })} {hiddenFiles > 0 ? ( setExpandedFor(requestKey)} style={{ paddingHorizontal: 10, paddingVertical: 8, borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: theme.colors.border, }} > Show {hiddenFiles} more file{hiddenFiles === 1 ? "" : "s"} ) : null} ); } export function Commits({ props }: { props: Props }) { const { theme, agentId, blockKey } = useUiRuntime(); const projectRpc = useProjectRpc(); const limitParsed = parseCommitLimit(props.limit); const [state, setState] = useState(null); useEffect(() => { if (!limitParsed.ok) return; let live = true; void projectRpc .commits({ agentId: agentId ?? "gallery", blockKey, refresh: false }) .then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [agentId, blockKey, limitParsed.ok, projectRpc]); if (!limitParsed.ok) return ; if (state === null) { return ( Reading commits… ); } if (!state.ok) return ; const commits = limitParsed.value ? state.commits.slice(0, limitParsed.value) : state.commits; return ( {state.branch ?? "HEAD"} vs {state.base} {commits.length} commit{commits.length === 1 ? "" : "s"} {commits.map((c, idx) => ( {c.shortSha} {c.subject} {c.author} ))} ); } export function PullRequest() { const { theme, agentId } = useUiRuntime(); const projectRpc = useProjectRpc(); const [state, setState] = useState(null); useEffect(() => { let live = true; void projectRpc.pullRequest({ agentId: agentId ?? "gallery" }).then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [agentId, projectRpc]); if (state === null) { return ( Reading pull request… ); } if (!state.ok) return ; if (!state.pullRequest) { return ( No open pull request for {state.branch ?? "this branch"} ); } const pr = state.pullRequest; return ( #{pr.number} {pr.title} {pr.state} {pr.headRef} → {pr.baseRef} · Checks: {checkCounts(pr.checks)} · Review: {pr.review.state} ); } export function Threads({ props }: { props: Props }) { const { theme, agentId } = useUiRuntime(); const projectRpc = useProjectRpc(); const parsedIds = parseThreadIds(props.ids); const [state, setState] = useState(null); useEffect(() => { if (!parsedIds.ok) return; let live = true; void projectRpc .threads({ agentId: agentId ?? "gallery", ids: parsedIds.value ? [...parsedIds.value] : null, }) .then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [agentId, parsedIds, projectRpc]); if (!parsedIds.ok) return ; if (state === null) { return ( Reading threads… ); } if (!state.ok) return ; return ( {state.threads.map((t) => t.found ? ( {t.title} {threadStatusLabel(t)} {t.lastMessage ? ( {t.lastMessage.text} ) : null} ) : ( ), )} ); } export function Environments() { const { theme, agentId } = useUiRuntime(); const projectRpc = useProjectRpc(); const [state, setState] = useState(null); useEffect(() => { let live = true; void projectRpc.environments({ agentId: agentId ?? "gallery" }).then((res) => { if (live) setState(res); }); return () => { live = false; }; }, [agentId, projectRpc]); if (state === null) { return ( Reading environments… ); } if (!state.ok) return ; return ( {state.environments.map((env) => ( {env.name ?? env.id} {env.branch ? `(${env.branch})` : ""} {env.kind} · {env.threadCounts.live} active {env.path ? ( {env.path} ) : null} ))} ); } export function Acceptance({ props }: { props: Props }) { const { theme } = useUiRuntime(); const parsed = useMemo( () => readAcceptance(props.criteria, props.columns), [props.criteria, props.columns], ); if (!parsed.ok) return ; const { report } = parsed; const verdict = acceptanceVerdict(report); return ( {verdictLine(verdict)} {report.criteria.map((crit, idx) => ( {crit.name} {(crit.cells ?? [crit.own]).map((chk, cIdx) => { const color = chk.status === "passed" ? theme.colors.statusSuccess : chk.status === "failed" ? theme.colors.statusDanger : chk.status === "claimed" || chk.status === "blocked" ? theme.colors.statusWarning : theme.colors.foregroundMuted; return ( {report.columns[cIdx] ? `${report.columns[cIdx]}: ` : ""} {checkLabel(chk)} ); })} ))} ); } export function Compare({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const parsed = useMemo(() => readCompare(props.images), [props.images]); if (!parsed.ok) return ; const { images } = parsed; return ( {images.map((img, idx) => ( {img.label} ))} ); } export function Transcript({ props }: { props: Props }) { const { theme, loadAudio, playAudio } = useUiRuntime(); const parsed = useMemo(() => readTracks(props.tracks), [props.tracks]); const audioPath = optionalText(props.audio); if (!parsed.ok) return ; const analysis = useMemo(() => analyse(parsed.tracks), [parsed.tracks]); return ( {audioPath && loadAudio && playAudio ? ( { void loadAudio(audioPath).then((res) => { if (res.ok) void playAudio(res.url); }); }} style={{ alignSelf: "flex-start", flexDirection: "row", alignItems: "center", gap: 6, paddingHorizontal: 10, paddingVertical: 5, borderRadius: 6, backgroundColor: theme.colors.accent, }} > Play recording ) : null} {parsed.tracks.map((track, idx) => { const comp = idx > 0 && analysis ? analysis.comparisons[idx - 1] : null; return ( {track.name} {comp ? ( {comp.wer !== null ? `WER ${formatWer(comp.wer)} · ` : ""} {werCountsText(comp.counts)} ) : ( Reference )} {track.words.map((w) => w.text).join(" ")} ); })} ); } export function Provenance({ props }: { props: Props }) { const { theme } = useUiRuntime(); const parsed = useMemo( () => readProvenance(props.stages, props.expected), [props.stages, props.expected], ); if (!parsed.ok) return ; const { provenance } = parsed; return ( {chainSummary(provenance)} {provenance.mismatches.map((m, idx) => ( {mismatchText(provenance.stages, m)} ))} {provenance.stages.map((stage, idx) => ( {idx + 1}. {stage.name} {stage.status} {stage.facts.length > 0 ? ( {stage.facts.map((f) => `${f.key}: ${f.value}`).join(" · ")} ) : null} ))} ); }