Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

223 linesCodeBlameRaw
1import type { ChatMessage, MessageCard } from "@g1t/contracts";
2import * as Haptics from "expo-haptics";
3import { router } from "expo-router";
4import * as WebBrowser from "expo-web-browser";
5import { AlertCircle, Clock, MessageSquareReply } from "lucide-react-native";
6import { memo } from "react";
7import { Pressable, StyleSheet, View } from "react-native";
8
9import { Markdown } from "@/components/chat/markdown";
10import { Avatar, Badge, Text } from "@/components/ui";
11import type { Shown } from "@/lib/chat";
12import { routeFor } from "@/lib/links";
13import { siteUrl } from "@/lib/server";
14import { useSignedIn } from "@/lib/session";
15import { clock } from "@/lib/time";
16import { radius, space, useColors } from "@/theme";
17
18type Props = {
19 message: Shown;
20 /** The first of a run by one author: draws the avatar and name. */
21 head: boolean;
22 /** In a thread: no "replies" line under it. */
23 inThread?: boolean;
24 onLongPress: (message: Shown) => void;
25 onReact: (message: ChatMessage, emoji: string, add: boolean) => void;
26 onRetry: (message: Shown) => void;
27};
28
29/**
30 * One message in a conversation. Messages by the same author within a few
31 * minutes run together under one name, as on the site. A message an agent
32 * posted before g1t's own agents were retired still shows, under the name
33 * chat gives it, with its marker and a Deleted agent badge.
34 */
35export const Message = memo(function Message({ message, head, inThread, onLongPress, onReact, onRetry }: Props) {
36 const colors = useColors();
37 const { server } = useSignedIn();
38 const { author } = message;
39 const agent = author.kind === "agent";
40 // @g1t: its answers, with the agent marker but never a Deleted agent badge.
41 const g1t = author.kind === "system";
42 const failed = message.local === "failed";
43
44 const openThread = () => router.push({ pathname: "/chat/thread", params: { channel: message.channel_id, root: message.id } });
45
46 return (
47 <Pressable
48 onLongPress={() => {
49 if (message.local) return;
50 void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium).catch(() => undefined);
51 onLongPress(message);
52 }}
53 onPress={failed ? () => onRetry(message) : undefined}
54 delayLongPress={300}
55 style={({ pressed }) => [styles.row, { paddingTop: head ? space.md : 2, backgroundColor: pressed ? colors.raised : "transparent" }]}
56 >
57 <View style={styles.gutter}>
58 {head ? (
59 <Avatar name={author.display_name} uri={author.avatar ? `${server.site}/avatars/${author.avatar}` : null} agent={agent || g1t} size={36} />
60 ) : null}
61 </View>
62 <View style={{ flex: 1, gap: 4, opacity: message.local === "sending" ? 0.6 : 1 }}>
63 {head ? (
64 <View style={styles.header}>
65 <Text style={{ fontWeight: "700" }} numberOfLines={1}>
66 {author.display_name}
67 </Text>
68 {agent ? <Badge label="Deleted agent" tone="neutral" /> : null}
69 <Text size="small" tone="faint">
70 {clock(message.created_at)}
71 </Text>
72 </View>
73 ) : null}
74 {message.body ? (
75 <Markdown text={message.body} />
76 ) : g1t && message.answer && (message.answer.state === "thinking" || message.answer.state === "streaming") ? (
77 <Text tone="muted">Thinking…</Text>
78 ) : null}
79 {message.edited_at ? (
80 <Text size="small" tone="faint">
81 (edited)
82 </Text>
83 ) : null}
84 {message.kind === "card" && message.card ? <Card card={message.card} site={server.site} /> : null}
85 {message.reactions && message.reactions.length > 0 ? (
86 <View style={styles.reactions}>
87 {message.reactions.map((reaction) => (
88 <Pressable
89 key={reaction.emoji}
90 accessibilityLabel={`${reaction.emoji} ${reaction.count}${reaction.me ? ", yours" : ""}`}
91 onPress={() => {
92 void Haptics.selectionAsync().catch(() => undefined);
93 onReact(message, reaction.emoji, !reaction.me);
94 }}
95 style={[
96 styles.reaction,
97 { borderColor: reaction.me ? colors.accent : colors.line, backgroundColor: reaction.me ? colors.accentLow : colors.surface },
98 ]}
99 >
100 <Text size="small">{reaction.emoji}</Text>
101 <Text size="small" style={{ color: reaction.me ? colors.accent : colors.muted, fontWeight: "600" }}>
102 {reaction.count}
103 </Text>
104 </Pressable>
105 ))}
106 </View>
107 ) : null}
108 {!inThread && message.reply_count > 0 ? (
109 <Pressable onPress={openThread} style={styles.replies} hitSlop={8}>
110 <MessageSquareReply size={14} color={colors.accent} />
111 <Text size="small" tone="accent" style={{ fontWeight: "600" }}>
112 {message.reply_count === 1 ? "1 reply" : `${message.reply_count} replies`}
113 </Text>
114 </Pressable>
115 ) : null}
116 {message.local === "sending" ? (
117 <View style={styles.status}>
118 <Clock size={12} color={colors.faint} />
119 <Text size="small" tone="faint">
120 Sending
121 </Text>
122 </View>
123 ) : failed ? (
124 <View style={styles.status}>
125 <AlertCircle size={12} color={colors.danger} />
126 <Text size="small" tone="danger">
127 Not sent. Tap to try again.
128 </Text>
129 </View>
130 ) : null}
131 </View>
132 </Pressable>
133 );
134});
135
136/**
137 * A card an agent posted before g1t's own agents were retired: what it was
138 * about, its state, and its links. Its other buttons no longer do anything,
139 * so they are not drawn.
140 */
141function Card({ card, site }: { card: MessageCard; site: string }) {
142 const colors = useColors();
143 const links = (card.actions ?? []).filter((action) => !!action.href);
144
145 const open = (href: string) => {
146 const route = href.startsWith("/") ? routeFor(href) : null;
147 if (route) router.push(route as never);
148 else void WebBrowser.openBrowserAsync(href.startsWith("/") ? siteUrl({ site, api: "" }, href) : href);
149 };
150
151 return (
152 <View style={[styles.card, { borderColor: colors.lineStrong, backgroundColor: colors.surface }]}>
153 <Pressable disabled={!card.href} onPress={() => card.href && open(card.href)} style={{ gap: 4 }}>
154 <View style={{ flexDirection: "row", gap: space.sm, alignItems: "flex-start" }}>
155 <Text style={{ flex: 1, fontWeight: "700" }}>{card.title}</Text>
156 {card.state ? (
157 <Badge
158 label={card.state}
159 tone={/fail|stop|denied|error/i.test(card.state) ? "danger" : /merged|done|approved|passed/i.test(card.state) ? "success" : "accent"}
160 />
161 ) : null}
162 </View>
163 {card.detail ? (
164 <Text size="small" tone="muted">
165 {card.detail}
166 </Text>
167 ) : null}
168 {card.body ? <Markdown text={card.body} muted /> : null}
169 {card.fields?.length ? (
170 <View style={styles.fields}>
171 {card.fields.map((field) => (
172 <View key={field.label} style={{ width: "48%" }}>
173 <Text size="small" tone="faint">
174 {field.label}
175 </Text>
176 <Text size="small" numberOfLines={2}>
177 {field.value}
178 </Text>
179 </View>
180 ))}
181 </View>
182 ) : null}
183 </Pressable>
184 {links.length ? (
185 <View style={styles.actions}>
186 {links.map((action) => (
187 <Pressable
188 key={action.id}
189 accessibilityRole="link"
190 onPress={() => action.href && open(action.href)}
191 style={({ pressed }) => [styles.action, { backgroundColor: colors.bg, borderColor: colors.lineStrong, opacity: pressed ? 0.6 : 1 }]}
192 >
193 <Text size="small" style={{ fontWeight: "700", color: colors.fg }}>
194 {action.label}
195 </Text>
196 </Pressable>
197 ))}
198 </View>
199 ) : null}
200 </View>
201 );
202}
203
204/** Whether two messages run together: the same author, within five minutes, neither a card. */
205export function runsOn(previous: ChatMessage | undefined, message: ChatMessage): boolean {
206 if (!previous || previous.kind === "card" || message.kind === "card") return false;
207 if (previous.author.id !== message.author.id || previous.author.kind !== message.author.kind) return false;
208 return Date.parse(message.created_at) - Date.parse(previous.created_at) < 5 * 60_000;
209}
210
211const styles = StyleSheet.create({
212 row: { flexDirection: "row", gap: space.md, paddingHorizontal: space.lg, paddingBottom: 2 },
213 gutter: { width: 36 },
214 header: { flexDirection: "row", alignItems: "center", gap: space.sm },
215 reactions: { flexDirection: "row", flexWrap: "wrap", gap: 6, marginTop: 2 },
216 reaction: { flexDirection: "row", alignItems: "center", gap: 4, borderWidth: 1, borderRadius: radius.full, paddingHorizontal: 8, paddingVertical: 3 },
217 replies: { flexDirection: "row", alignItems: "center", gap: 6, marginTop: 2 },
218 status: { flexDirection: "row", alignItems: "center", gap: 4 },
219 card: { borderWidth: 1, borderRadius: radius.md, padding: space.md, gap: space.sm, marginTop: 2 },
220 fields: { flexDirection: "row", flexWrap: "wrap", gap: space.sm, justifyContent: "space-between" },
221 actions: { flexDirection: "row", flexWrap: "wrap", gap: space.sm },
222 action: { minHeight: 36, minWidth: 72, paddingHorizontal: space.md, borderRadius: radius.sm, borderWidth: 1, alignItems: "center", justifyContent: "center" },
223});