| 1 | import { Stack, router, useFocusEffect, useLocalSearchParams } from "expo-router"; |
| 2 | import * as WebBrowser from "expo-web-browser"; |
| 3 | import { ExternalLink, FileCode, GitPullRequest, ListTodo, MessageSquareText, Sparkles, StickyNote, Terminal, User, Wrench } from "lucide-react-native"; |
| 4 | import type { LucideIcon } from "lucide-react-native"; |
| 5 | import { useCallback, useState } from "react"; |
| 6 | import { Pressable, RefreshControl, ScrollView, StyleSheet, View } from "react-native"; |
| 7 | |
| 8 | import { StateDot, openItem, openPull, stateName, useCode } from "@/components/sessions"; |
| 9 | import { Badge, Button, ErrorView, Loading, Row, Screen, SectionHeader, Text } from "@/components/ui"; |
| 10 | import { Chip, repoName } from "@/components/work"; |
| 11 | import { siteUrl } from "@/lib/server"; |
| 12 | import { useSignedIn } from "@/lib/session"; |
| 13 | import { type ApiSessionEntry, type ApiSessionLink, type EntryKind, harnessName, sessionTitle, subagentTree, touchedFiles } from "@/lib/sessions"; |
| 14 | import { ago, clock } from "@/lib/time"; |
| 15 | import { useWorkspaceSession } from "@/lib/work"; |
| 16 | import { radius, space, useColors } from "@/theme"; |
| 17 | |
| 18 | /** How many files show before "Show all". */ |
| 19 | const FILES = 20; |
| 20 | |
| 21 | /** |
| 22 | * One session: who runs it in what, where, and its state; the work item |
| 23 | * and pull request it is on; its subagents as a tree; the files it |
| 24 | * touched; and what it recorded, step by step. While it is live it is |
| 25 | * read again every 15 s. |
| 26 | */ |
| 27 | export default function SessionScreen() { |
| 28 | const params = useLocalSearchParams<{ id: string; workspace?: string }>(); |
| 29 | const { server, workspace: current } = useSignedIn(); |
| 30 | const workspace = params.workspace ?? current?.slug ?? ""; |
| 31 | const colors = useColors(); |
| 32 | const code = useCode(); |
| 33 | const [focused, setFocused] = useState(false); |
| 34 | useFocusEffect( |
| 35 | useCallback(() => { |
| 36 | setFocused(true); |
| 37 | return () => setFocused(false); |
| 38 | }, []), |
| 39 | ); |
| 40 | const query = useWorkspaceSession(workspace, params.id, focused); |
| 41 | const [allFiles, setAllFiles] = useState(false); |
| 42 | const sitePath = `/${workspace}/-/work/sessions/${params.id}`; |
| 43 | |
| 44 | const title = ( |
| 45 | <Stack.Screen |
| 46 | options={{ |
| 47 | title: "Session", |
| 48 | headerRight: () => ( |
| 49 | <Pressable accessibilityLabel="Open on g1t" hitSlop={10} onPress={() => void WebBrowser.openBrowserAsync(siteUrl(server, sitePath))}> |
| 50 | <ExternalLink size={20} color={colors.fg} /> |
| 51 | </Pressable> |
| 52 | ), |
| 53 | }} |
| 54 | /> |
| 55 | ); |
| 56 | if (query.isPending || query.error || !query.detail) { |
| 57 | return ( |
| 58 | <Screen> |
| 59 | {title} |
| 60 | {query.error ? <ErrorView error={query.error} retry={() => query.refetch()} /> : <Loading />} |
| 61 | </Screen> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const { session, repo, children = [], touched, skills = [], root, limited } = query.detail; |
| 66 | const links: ApiSessionLink[] = query.detail.links?.length |
| 67 | ? query.detail.links |
| 68 | : [ |
| 69 | ...(session.item ? [{ kind: "item" as const, number: session.item, title: "", state: "", via: ["attached"], session_ids: [session.id] }] : []), |
| 70 | ...(session.pull ? [{ kind: "pull" as const, number: session.pull, title: "", state: "", via: ["attached"], session_ids: [session.id] }] : []), |
| 71 | ]; |
| 72 | const tree = subagentTree(session.id, children); |
| 73 | const files = touchedFiles(touched); |
| 74 | const facts = [harnessName(session.harness), `@${session.actor.username}`, session.model].filter(Boolean).join(" · "); |
| 75 | const openSession = (id: string) => router.push({ pathname: "/work/sessions/[id]", params: { id, workspace } }); |
| 76 | |
| 77 | return ( |
| 78 | <Screen> |
| 79 | {title} |
| 80 | <ScrollView |
| 81 | contentContainerStyle={styles.content} |
| 82 | refreshControl={ |
| 83 | <RefreshControl refreshing={query.isRefetching && !query.isFetchingNextPage} onRefresh={() => query.refetch()} tintColor={colors.accent} /> |
| 84 | } |
| 85 | > |
| 86 | <View style={{ gap: space.sm }}> |
| 87 | <Text size="heading" selectable> |
| 88 | {sessionTitle(session)} |
| 89 | </Text> |
| 90 | <View style={styles.wrap}> |
| 91 | <StateDot state={session.state} /> |
| 92 | <Text |
| 93 | size="small" |
| 94 | style={{ fontWeight: "600", color: session.state === "active" ? colors.success : session.state === "idle" ? colors.warn : colors.muted }} |
| 95 | > |
| 96 | {stateName(session.state)} |
| 97 | </Text> |
| 98 | <Text size="small" tone="muted"> |
| 99 | {facts} |
| 100 | </Text> |
| 101 | </View> |
| 102 | <Text size="small" tone="muted"> |
| 103 | {repoName(repo, workspace)} |
| 104 | {session.branch ? ` · ${session.branch}` : ""} |
| 105 | </Text> |
| 106 | <Text size="small" tone="muted"> |
| 107 | Started {ago(session.started_at)} · last heard from {ago(session.last_seen_at)} |
| 108 | {session.ended_at ? ` · ended ${ago(session.ended_at)}${session.end_reason ? ` (${session.end_reason.replace(/_/g, " ")})` : ""}` : ""} |
| 109 | </Text> |
| 110 | {session.agent_type || session.task ? ( |
| 111 | <Text size="small" tone="muted"> |
| 112 | {[session.agent_type, session.task].filter(Boolean).join(": ")} |
| 113 | </Text> |
| 114 | ) : null} |
| 115 | {root && root.id !== session.id ? ( |
| 116 | <View style={styles.wrap}> |
| 117 | <Text size="small" tone="muted"> |
| 118 | A subagent of |
| 119 | </Text> |
| 120 | <Chip label={sessionTitle(root)} onPress={() => openSession(root.id)} /> |
| 121 | </View> |
| 122 | ) : null} |
| 123 | {skills.length ? ( |
| 124 | <View style={styles.wrap}> |
| 125 | {skills.map((skill) => ( |
| 126 | <Chip |
| 127 | key={`${skill.session_id}:${skill.name}`} |
| 128 | label={skill.uses > 1 ? `${skill.name} ×${skill.uses}` : skill.name} |
| 129 | leading={<Sparkles size={12} color={colors.muted} />} |
| 130 | /> |
| 131 | ))} |
| 132 | </View> |
| 133 | ) : null} |
| 134 | {limited ? ( |
| 135 | <Text size="small" tone="faint"> |
| 136 | You are not a member of this workspace: you see that it runs, who runs it and what it is tied to, nothing more. |
| 137 | </Text> |
| 138 | ) : null} |
| 139 | </View> |
| 140 | |
| 141 | {links.length ? ( |
| 142 | <View style={{ marginHorizontal: -space.lg }}> |
| 143 | <SectionHeader title="Working on" /> |
| 144 | {links.map((link) => ( |
| 145 | <Row |
| 146 | key={`${link.kind}:${link.number}`} |
| 147 | leading={link.kind === "item" ? <ListTodo size={20} color={colors.muted} /> : <GitPullRequest size={20} color={colors.muted} />} |
| 148 | title={`#${link.number}${link.title ? ` ${link.title}` : ""}`} |
| 149 | subtitle={[link.kind === "item" ? "Work item" : "Pull request", link.state, link.via.join(", ")].filter(Boolean).join(" · ")} |
| 150 | onPress={() => (link.kind === "item" ? openItem(workspace, link.number) : openPull(repo, link.number, code))} |
| 151 | /> |
| 152 | ))} |
| 153 | </View> |
| 154 | ) : null} |
| 155 | |
| 156 | {tree.length ? ( |
| 157 | <View style={{ marginHorizontal: -space.lg }}> |
| 158 | <SectionHeader title={`Subagents · ${tree.length}`} /> |
| 159 | {tree.map(({ session: child, depth }) => ( |
| 160 | <View key={child.id} style={{ paddingLeft: (depth - 1) * space.lg }}> |
| 161 | <Row |
| 162 | leading={<StateDot state={child.state} />} |
| 163 | title={sessionTitle(child)} |
| 164 | subtitle={[ |
| 165 | child.agent_type, |
| 166 | child.state === "ended" ? `ended ${ago(child.ended_at ?? child.last_seen_at)}` : `heard from ${ago(child.last_seen_at)}`, |
| 167 | ] |
| 168 | .filter(Boolean) |
| 169 | .join(" · ")} |
| 170 | onPress={() => openSession(child.id)} |
| 171 | /> |
| 172 | </View> |
| 173 | ))} |
| 174 | </View> |
| 175 | ) : null} |
| 176 | |
| 177 | {files.length ? ( |
| 178 | <View style={{ gap: space.xs }}> |
| 179 | <View style={{ marginHorizontal: -space.lg }}> |
| 180 | <SectionHeader title={`Touched · ${files.length}`} /> |
| 181 | </View> |
| 182 | {(allFiles ? files : files.slice(0, FILES)).map((path) => ( |
| 183 | <View key={path} style={styles.file}> |
| 184 | <FileCode size={14} color={colors.faint} /> |
| 185 | <Text size="mono" tone="muted" numberOfLines={1} style={{ flex: 1 }}> |
| 186 | {path} |
| 187 | </Text> |
| 188 | </View> |
| 189 | ))} |
| 190 | {files.length > FILES && !allFiles ? ( |
| 191 | <Pressable onPress={() => setAllFiles(true)} hitSlop={8}> |
| 192 | <Text size="small" tone="accent"> |
| 193 | Show all {files.length} |
| 194 | </Text> |
| 195 | </Pressable> |
| 196 | ) : null} |
| 197 | </View> |
| 198 | ) : null} |
| 199 | |
| 200 | <View style={{ gap: space.sm }}> |
| 201 | <View style={{ marginHorizontal: -space.lg }}> |
| 202 | <SectionHeader title="Timeline" /> |
| 203 | </View> |
| 204 | {query.entries.length === 0 ? ( |
| 205 | <Text tone="muted">{limited ? "Its steps are not shown to you." : "Nothing recorded yet."}</Text> |
| 206 | ) : ( |
| 207 | query.entries.map((entry) => <Entry key={entry.seq} entry={entry} />) |
| 208 | )} |
| 209 | {query.hasNextPage ? ( |
| 210 | <Button title="Show later steps" variant="secondary" busy={query.isFetchingNextPage} onPress={() => void query.fetchNextPage()} /> |
| 211 | ) : null} |
| 212 | </View> |
| 213 | </ScrollView> |
| 214 | </Screen> |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | const ENTRY_KINDS: Record<EntryKind, { label: string; icon: LucideIcon }> = { |
| 219 | prompt: { label: "Prompt", icon: User }, |
| 220 | message: { label: "Message", icon: MessageSquareText }, |
| 221 | tool_call: { label: "Tool", icon: Wrench }, |
| 222 | tool_result: { label: "Result", icon: Terminal }, |
| 223 | note: { label: "Note", icon: StickyNote }, |
| 224 | skill: { label: "Skill", icon: Sparkles }, |
| 225 | }; |
| 226 | |
| 227 | /** One recorded step: its kind, tool and time, then its text, a few lines until tapped. */ |
| 228 | function Entry({ entry }: { entry: ApiSessionEntry }) { |
| 229 | const colors = useColors(); |
| 230 | const [open, setOpen] = useState(false); |
| 231 | const kind = ENTRY_KINDS[entry.kind] ?? { label: entry.kind, icon: StickyNote }; |
| 232 | const Icon = kind.icon; |
| 233 | const result = entry.kind === "tool_result" || entry.kind === "tool_call"; |
| 234 | return ( |
| 235 | <Pressable |
| 236 | onPress={() => setOpen(!open)} |
| 237 | style={[styles.entry, { borderColor: colors.line, backgroundColor: entry.kind === "prompt" ? colors.raised : colors.surface }]} |
| 238 | > |
| 239 | <View style={styles.wrap}> |
| 240 | <Icon size={13} color={entry.kind === "prompt" ? colors.accent : colors.muted} /> |
| 241 | <Text size="small" style={{ fontWeight: "600", color: colors.fgSoft }}> |
| 242 | {kind.label} |
| 243 | </Text> |
| 244 | {entry.tool ? <Badge label={entry.tool} /> : null} |
| 245 | <Text size="small" tone="faint" style={{ marginLeft: "auto" }}> |
| 246 | {clock(entry.at)} |
| 247 | </Text> |
| 248 | </View> |
| 249 | {entry.withheld ? ( |
| 250 | <Text size="small" tone="faint" style={{ fontStyle: "italic" }}> |
| 251 | Not shown to you. |
| 252 | </Text> |
| 253 | ) : entry.text ? ( |
| 254 | <Text size={result ? "mono" : "small"} tone={result ? "muted" : "default"} numberOfLines={open ? undefined : 4} selectable={open}> |
| 255 | {entry.text} |
| 256 | </Text> |
| 257 | ) : null} |
| 258 | </Pressable> |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | const styles = StyleSheet.create({ |
| 263 | content: { padding: space.lg, gap: space.lg, paddingBottom: space.xl }, |
| 264 | wrap: { flexDirection: "row", flexWrap: "wrap", alignItems: "center", gap: 6 }, |
| 265 | file: { flexDirection: "row", alignItems: "center", gap: space.sm }, |
| 266 | entry: { borderWidth: 1, borderRadius: radius.md, padding: space.md, gap: space.xs }, |
| 267 | }); |