import { SECRET_REQUEST_DEFAULT_PLACEHOLDER, SECRET_REQUEST_PRIVACY_NOTE, secretRequestAnswerInput, secretRequestDisplay, secretRequestFailureMessage, type SecretRequestItem, } from "@t3tools/client-runtime/secret-request"; import { isAtomCommandInterrupted, squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts"; import { useRef, useState } from "react"; import { Pressable, View, type ColorValue } from "react-native"; import { SymbolView, type AppSymbolName } from "../../components/AppSymbol"; import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText"; import { serverEnvironment } from "../../state/server"; import { useAtomCommand } from "../../state/use-atom-command"; import { RequestActionButton } from "./RequestActionButton"; /** * Feed card for a secret an agent asked the user for. The typed value lives * only in this component's state and the RPC payload: it is never logged, * alerted, or persisted, and the field clears once the answer is sent. */ const LOCK_SYMBOL: AppSymbolName = { ios: "lock", android: "lock" }; const PRIVATE_SYMBOL: AppSymbolName = { ios: "checkmark.shield", android: "lock" }; export function SecretRequestCard(props: { readonly environmentId: EnvironmentId; readonly projectedItem: OrchestrationV2ProjectedTurnItem; readonly iconColor: ColorValue; }) { const { item, visibility } = props.projectedItem; if (item.type !== "secret_request") return null; const display = secretRequestDisplay(item, visibility); if (display.kind === "pending") { return ( ); } const icon: AppSymbolName = display.kind === "pending-elsewhere" ? LOCK_SYMBOL : display.outcome === "saved" ? "checkmark" : "minus"; return ( {item.label} ยท {display.label} ); } function PendingSecretRequestForm(props: { readonly environmentId: EnvironmentId; readonly item: SecretRequestItem; readonly iconColor: ColorValue; }) { const { item } = props; const answer = useAtomCommand(serverEnvironment.answerSecretRequest, { label: "answer secret request", // The failure cause holds the request; keep it out of the console. reportFailure: false, reportDefect: false, }); const [secret, setSecret] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); // Submit then a tap can both run before a re-render; this guard is synchronous. const inFlight = useRef(false); const send = async ( reply: { readonly type: "save"; readonly secret: string } | { readonly type: "decline" }, ) => { const input = secretRequestAnswerInput(item, reply); if (input === null || inFlight.current) return; inFlight.current = true; setSubmitting(true); setError(null); const result = await answer({ environmentId: props.environmentId, input }).finally(() => { inFlight.current = false; setSubmitting(false); }); if (result._tag === "Success") { // The card switches to its answered row once the item updates. setSecret(""); return; } if (!isAtomCommandInterrupted(result)) { setError(secretRequestFailureMessage(squashAtomCommandFailure(result))); } }; // Same hierarchy as web: what is asked, why, the field, then the promise // about where the value goes. return ( {item.label} {item.reason.trim() ? ( {item.reason} ) : null} void send({ type: "save", secret })} /> {error !== null ? ( {error} ) : null} void send({ type: "save", secret })} /> {SECRET_REQUEST_PRIVACY_NOTE} {/* Quiet like the web card's: the field and Save are the action. */} void send({ type: "decline" })} > Decline ); }