Open History to see the last commit
1import { pipe } from "effect/Function";
2import { useAtomValue } from "@effect/atom-react";
3import {
4 ANOTHER_ADDRESS,
5 type LinkableMachine,
6 linkDestination,
7 linkRequestAnswerInput,
8 linkRequestDefaultAccess,
9 linkRequestDisplay,
10 linkRequestFailureMessage,
11 linkRequestInitialMachine,
12 type LinkRequestItem,
13 linkRequestTarget,
14 linkRequestTargetName,
15 rankMachinesByHint,
16} from "@t3tools/client-runtime/link-request";
17import {
18 isAtomCommandInterrupted,
19 squashAtomCommandFailure,
20} from "@t3tools/client-runtime/state/runtime";
21import {
22 AuthAccessWriteScope,
23 type AuthMcpClientAccess,
24 type EnvironmentId,
25 type OrchestrationV2ProjectedTurnItem,
26} from "@t3tools/contracts";
27import * as Option from "effect/Option";
28import * as Result from "effect/Result";
29import { useMemo, useRef, useState } from "react";
30import { Pressable, ScrollView, View, type ColorValue } from "react-native";
31import { useSafeAreaInsets } from "react-native-safe-area-context";
32
33import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
34import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText";
35import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
36import { peerLinkEnvironment, useLinkableMachines } from "../../state/peerLinks";
37import { mintPairingCodeOn } from "../../state/peerPairingCode";
38import { useEnvironmentScope, usePreparedConnection } from "../../state/session";
39import { useAtomCommand } from "../../state/use-atom-command";
40import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoiceRow";
41import { SettingsSection } from "../settings/components/SettingsSection";
42import { Button } from "../../components/ui/Button";
43import { Sheet } from "../../components/ui/Sheet";
44import { RUNTIME_MODE_CHOICES } from "./thread-settings-options";
45
46/** The same five levels as web's access picker: read-only, then the runtime modes. */
47const ACCESS_CHOICES: ReadonlyArray<{
48 readonly access: AuthMcpClientAccess;
49 readonly label: string;
50 readonly description: string;
51}> = [
52 {
53 access: "read-only",
54 label: "Read only",
55 description: "Read projects and threads. Cannot start, message or change anything.",
56 },
57 ...RUNTIME_MODE_CHOICES.map((choice) => ({ ...choice, access: choice.mode })),
58];
59const accessLabel = (access: AuthMcpClientAccess) =>
60 ACCESS_CHOICES.find((choice) => choice.access === access)?.label ?? access;
61
62const LINK_SYMBOL: AppSymbolName = { ios: "link", android: "desktop_windows" };
63
64/** Whether the agent left the machine for the user to pick. */
65const picksMachine = (item: LinkRequestItem) =>
66 item.environmentId === undefined && item.url === undefined;
67
68/**
69 * Feed card for a link an agent asked the user to make. The user picks the
70 * access in a sheet; the pairing code lives only in that sheet's state and
71 * the RPC payload, and is never logged, alerted, or persisted.
72 */
73export function LinkRequestCard(props: {
74 readonly environmentId: EnvironmentId;
75 readonly projectedItem: OrchestrationV2ProjectedTurnItem;
76 readonly iconColor: ColorValue;
77}) {
78 const { item, visibility } = props.projectedItem;
79 if (item.type !== "link_request") return null;
80 const display = linkRequestDisplay(item, visibility, accessLabel);
81 if (display.kind === "pending") {
82 return <PendingLinkRequest environmentId={props.environmentId} item={item} />;
83 }
84 const icon: AppSymbolName =
85 display.kind === "pending-elsewhere"
86 ? LINK_SYMBOL
87 : display.outcome === "linked"
88 ? "checkmark"
89 : display.outcome === "failed"
90 ? "xmark"
91 : "minus";
92 return (
93 <View className="mb-3 min-h-9 flex-row items-center gap-2 px-1">
94 <SymbolView name={icon} size={13} tintColor={props.iconColor} type="monochrome" />
95 <Text className="flex-1 font-sans text-sm text-foreground-muted" numberOfLines={2}>
96 {display.label}
97 </Text>
98 </View>
99 );
100}
101
102function PendingLinkRequest(props: {
103 readonly environmentId: EnvironmentId;
104 readonly item: LinkRequestItem;
105}) {
106 const { item } = props;
107 const [open, setOpen] = useState(false);
108 const [declining, setDeclining] = useState(false);
109 const [error, setError] = useState<string | null>(null);
110 const answer = useAtomCommand(peerLinkEnvironment.answerRequest, {
111 label: "decline link request",
112 reportFailure: false,
113 reportDefect: false,
114 });
115 const canAnswer = useAtomValue(
116 peerLinkEnvironment.answerRequest.permissionAtom(props.environmentId),
117 );
118 const target = linkRequestTargetName(item);
119
120 const decline = async () => {
121 const input = linkRequestAnswerInput(item, { type: "decline" });
122 if (input === null) return;
123 setDeclining(true);
124 setError(null);
125 const result = await answer({ environmentId: props.environmentId, input }).finally(() =>
126 setDeclining(false),
127 );
128 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
129 pipe(squashAtomCommandFailure(result), linkRequestFailureMessage, setError);
130 }
131 };
132
133 return (
134 <View className="mb-3 gap-3 rounded-[20px] border border-border bg-card-alt p-4">
135 <View className="gap-1">
136 <Text className="font-t3-bold text-base text-foreground">
137 {picksMachine(item)
138 ? "Link a machine to this environment"
139 : `Link ${target} to this environment`}
140 </Text>
141 {item.reason.trim() ? (
142 <Text className="font-sans text-sm leading-5 text-foreground-muted">{item.reason}</Text>
143 ) : null}
144 </View>
145 {error !== null ? (
146 <Text
147 accessibilityRole="alert"
148 accessibilityLiveRegion="polite"
149 className="font-sans text-sm text-danger-foreground"
150 >
151 {error}
152 </Text>
153 ) : null}
154 {canAnswer ? (
155 <>
156 <Button
157 label={picksMachine(item) ? "Choose machine…" : "Review link…"}
158 disabled={declining}
159 onPress={() => setOpen(true)}
160 tone="primary"
161 fullWidth
162 />
163 <View className="flex-row justify-end">
164 <Pressable
165 accessibilityRole="button"
166 accessibilityState={{ disabled: declining }}
167 disabled={declining}
168 hitSlop={8}
169 className="px-1 py-1 active:opacity-60 disabled:opacity-50"
170 onPress={() => void decline()}
171 >
172 <Text className="font-sans text-xs text-foreground-muted">Decline</Text>
173 </Pressable>
174 </View>
175 </>
176 ) : (
177 <Text className="font-sans text-xs text-foreground-muted">
178 Only a session that manages access here can answer this.
179 </Text>
180 )}
181 {open ? (
182 <LinkRequestSheet
183 environmentId={props.environmentId}
184 item={item}
185 onClose={() => setOpen(false)}
186 />
187 ) : null}
188 </View>
189 );
190}
191
192function LinkRequestSheet(props: {
193 readonly environmentId: EnvironmentId;
194 readonly item: LinkRequestItem;
195 readonly onClose: () => void;
196}) {
197 const { item } = props;
198 const insets = useSafeAreaInsets();
199 const pickMachine = picksMachine(item);
200 const listed = useLinkableMachines(props.environmentId, true);
201 const machines = useMemo(() => rankMachinesByHint(listed, item.hint), [listed, item.hint]);
202 const [choice, setChoice] = useState<string | null>(null);
203 const [address, setAddress] = useState(item.url ?? "");
204 const [access, setAccess] = useState<AuthMcpClientAccess>(() => linkRequestDefaultAccess(item));
205 const [pairingCode, setPairingCode] = useState("");
206 const [busy, setBusy] = useState(false);
207 const [error, setError] = useState<string | null>(null);
208 const [mintFailed, setMintFailed] = useState<ReadonlySet<EnvironmentId>>(new Set());
209 // Submit then a tap can both run before a re-render; this guard is synchronous.
210 const inFlight = useRef(false);
211 const answer = useAtomCommand(peerLinkEnvironment.answerRequest, {
212 label: "answer link request",
213 // The failure cause holds the request, and with it the code; keep it out of the console.
214 reportFailure: false,
215 reportDefect: false,
216 });
217 const selected = choice ?? linkRequestInitialMachine(item, machines);
218 const target = linkRequestTarget(item, machines, selected);
219 const targetName = target.kind === "machine" ? target.label : linkRequestTargetName(item);
220 const targetId = target.kind === "machine" ? target.environmentId : null;
221 const connected = usePreparedConnection(targetId);
222 const mayMint = useEnvironmentScope(targetId, AuthAccessWriteScope);
223 const needsAddress =
224 target.kind === "address" || (target.kind === "machine" && target.urls.length === 0);
225 // This phone mints a code there when it is connected with a session that may
226 // create pairing links, and only for its own addresses: anything can answer
227 // as that machine at one typed in, so that one gets a code the user fetched.
228 const canMint =
229 targetId !== null &&
230 !needsAddress &&
231 Option.isSome(connected) &&
232 mayMint &&
233 !mintFailed.has(targetId);
234 const useExisting = target.kind === "machine" && target.linked;
235 const ready =
236 target.kind !== "none" &&
237 (useExisting ||
238 ((!needsAddress || address.trim().length > 0) && (canMint || pairingCode.trim().length > 0)));
239
240 const prepareAnswer = async () => {
241 if (target.kind === "none") return null;
242 if (target.kind === "machine" && target.linked) {
243 return linkRequestAnswerInput(item, {
244 type: "use-existing",
245 environmentId: target.environmentId,
246 });
247 }
248 let code = pairingCode;
249 if (canMint && targetId !== null) {
250 const minted = await mintPairingCodeOn(targetId, access);
251 if (Result.isFailure(minted)) {
252 setMintFailed((failed) => new Set(failed).add(targetId));
253 setError(linkRequestFailureMessage(minted.failure));
254 return null;
255 }
256 code = minted.success;
257 }
258 return linkRequestAnswerInput(item, {
259 type: "link",
260 target: target.kind === "machine" && target.urls.length > 0 ? target : { url: address },
261 access,
262 pairingCode: code,
263 });
264 };
265
266 const submit = async () => {
267 if (inFlight.current || target.kind === "none") return;
268 inFlight.current = true;
269 setBusy(true);
270 setError(null);
271 const input = await prepareAnswer();
272 const result =
273 input === null ? null : await answer({ environmentId: props.environmentId, input });
274 inFlight.current = false;
275 setBusy(false);
276 if (result === null) return;
277 if (result._tag === "Success") {
278 setPairingCode("");
279 props.onClose();
280 } else if (!isAtomCommandInterrupted(result)) {
281 pipe(squashAtomCommandFailure(result), linkRequestFailureMessage, setError);
282 }
283 };
284
285 return (
286 <Sheet
287 title={pickMachine ? "Link a machine" : `Link ${targetName}`}
288 onClose={props.onClose}
289 // An answer that lands after a close would still link, and would close a reopened sheet.
290 dismissDisabled={busy}
291 >
292 <ScrollView
293 className="flex-1 bg-sheet-solid"
294 contentInsetAdjustmentBehavior="automatic"
295 contentContainerClassName="gap-5 p-5"
296 contentContainerStyle={{ paddingBottom: insets.bottom + 20 }}
297 keyboardShouldPersistTaps="handled"
298 >
299 <View className="gap-1">
300 <Text className="font-sans text-sm leading-5 text-foreground-muted">
301 Agents here can then launch, message and wait on threads there. It appears in that
302 machine's Connections, where it can be revoked.
303 </Text>
304 {item.url !== undefined && target.kind === "machine" ? (
305 <Text className="font-sans text-xs text-foreground-muted" numberOfLines={1}>
306 {item.url}
307 </Text>
308 ) : null}
309 </View>
310 {needsAddress && !useExisting && address.trim() ? (
311 <View
312 accessibilityRole="alert"
313 className="gap-1 rounded-xl border border-warning-border bg-warning p-3"
314 >
315 <Text className="font-t3-bold text-sm text-warning-foreground">Check this address</Text>
316 <Text className="font-sans text-xs leading-snug text-warning-foreground">
317 This app cannot confirm it is one of your machines. The pairing code you paste is sent
318 there.
319 </Text>
320 <Text selectable className="font-sans text-xs leading-snug text-warning-foreground">
321 {linkDestination(address)}
322 </Text>
323 </View>
324 ) : null}
325 {pickMachine ? (
326 <SettingsSection title="Machine">
327 {machines.map((machine, index) => (
328 <MachineRow
329 key={machine.environmentId}
330 machine={machine}
331 selected={selected === machine.environmentId}
332 separated={index > 0}
333 disabled={busy}
334 onPress={() => {
335 setChoice(machine.environmentId);
336 setError(null);
337 }}
338 />
339 ))}
340 <SettingsChoiceRow
341 label="Another address…"
342 description="Link a machine this app does not know, by its https or Tailscale address."
343 selected={selected === ANOTHER_ADDRESS}
344 separated={machines.length > 0}
345 disabled={busy}
346 onPress={() => {
347 setChoice(ANOTHER_ADDRESS);
348 setError(null);
349 }}
350 />
351 </SettingsSection>
352 ) : null}
353 {needsAddress && item.url === undefined ? (
354 <View className="gap-2">
355 <TextInput
356 accessibilityLabel="Address"
357 placeholder="https://machine.tailnet.ts.net"
358 value={address}
359 onChangeText={setAddress}
360 editable={!busy}
361 autoCorrect={false}
362 autoCapitalize="none"
363 keyboardType="url"
364 spellCheck={false}
365 />
366 {target.kind === "machine" ? (
367 <Text className="font-sans text-xs text-foreground-muted">
368 This app has no address for {target.label} that another machine can reach. Enter its
369 https or Tailscale address.
370 </Text>
371 ) : null}
372 </View>
373 ) : null}
374 {useExisting ? (
375 <Text className="font-sans text-sm leading-5 text-foreground-muted">
376 This environment is already linked to {targetName}. The agent can use that link as it
377 is; change its access in Settings → Connections.
378 </Text>
379 ) : target.kind === "none" ? null : (
380 <>
381 {canMint ? null : (
382 <View className="gap-2">
383 <TextInput
384 accessibilityLabel="Pairing code"
385 placeholder="Paste the code"
386 value={pairingCode}
387 onChangeText={setPairingCode}
388 editable={!busy}
389 autoCorrect={false}
390 autoCapitalize="none"
391 autoComplete="one-time-code"
392 textContentType="oneTimeCode"
393 spellCheck={false}
394 />
395 <Text className="font-sans text-xs text-foreground-muted">
396 Create a link on {targetName}: Settings → Connections → Create link, then paste
397 its code here.
398 </Text>
399 </View>
400 )}
401 <SettingsSection title="What agents here may do there">
402 {ACCESS_CHOICES.map((choice, index) => (
403 <SettingsChoiceRow
404 key={choice.access}
405 label={choice.label}
406 description={choice.description}
407 selected={choice.access === access}
408 separated={index > 0}
409 disabled={busy}
410 onPress={() => setAccess(choice.access)}
411 />
412 ))}
413 </SettingsSection>
414 <Text className="font-sans text-xs text-foreground-muted">
415 An agent here never gets more there than its own mode here either. Threads it starts
416 there cannot change that environment's own threads, projects or settings.
417 </Text>
418 </>
419 )}
420 {error !== null ? (
421 <Text
422 accessibilityRole="alert"
423 accessibilityLiveRegion="polite"
424 className="font-sans text-sm text-danger-foreground"
425 >
426 {error}
427 </Text>
428 ) : null}
429 <Button
430 label={busy ? "Linking…" : useExisting ? "Use this link" : "Link"}
431 disabled={busy || !ready}
432 onPress={() => void submit()}
433 tone="primary"
434 fullWidth
435 />
436 <Button label="Cancel" tone="secondary" disabled={busy} onPress={props.onClose} fullWidth />
437 </ScrollView>
438 </Sheet>
439 );
440}
441
442/** One machine in the sheet's list: its glyph, name, and whether it is linked or reachable. */
443function MachineRow(props: {
444 readonly machine: LinkableMachine;
445 readonly selected: boolean;
446 readonly separated: boolean;
447 readonly disabled: boolean;
448 readonly onPress: () => void;
449}) {
450 const { machine } = props;
451 const status = machine.linked
452 ? "Linked"
453 : machine.connected
454 ? null
455 : machine.connectOnly
456 ? "T3 Connect"
457 : "Not connected";
458 return (
459 <Pressable
460 accessibilityRole="radio"
461 accessibilityState={{ checked: props.selected, disabled: props.disabled }}
462 className={
463 props.separated
464 ? "flex-row items-center gap-3 border-t border-border-subtle p-4 active:opacity-70"
465 : "flex-row items-center gap-3 p-4 active:opacity-70"
466 }
467 disabled={props.disabled}
468 onPress={props.onPress}
469 >
470 <EnvironmentMachineSymbol
471 kind={machine.machine}
472 size={18}
473 tintColorClassName="accent-foreground-muted"
474 />
475 <View className="min-w-0 flex-1 gap-0.5">
476 <Text className="text-lg text-foreground android:text-base" numberOfLines={1}>
477 {machine.label}
478 </Text>
479 {status === null ? null : <Text className="text-sm text-foreground-muted">{status}</Text>}
480 </View>
481 {props.selected ? (
482 <SymbolView
483 name="checkmark"
484 size={18}
485 tintColorClassName="accent-icon"
486 type="monochrome"
487 weight="semibold"
488 />
489 ) : null}
490 </Pressable>
491 );
492}