The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 104 lines 4.12 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import { AuthOrchestrationOperateScope, type ScopedThreadRef } from "@t3tools/contracts";
3 import type { ChildThreadApproval } from "@t3tools/client-runtime/state/thread-requests";
4 import { fullAccessOverrideExplanation } from "@t3tools/client-runtime/state/thread-requests";
5 import {
6 isAtomCommandInterrupted,
7 squashAtomCommandFailure,
8 } from "@t3tools/client-runtime/state/runtime";
9 import { T, useGT, useMessages, Var } from "gt-react";
10 import { useState } from "react";
11 import { useEnvironmentScope } from "../../state/session";
12 import { childThreadApprovalsAtom, threadEnvironment } from "../../state/threads";
13 import { useAtomCommand } from "../../state/use-atom-command";
14 import { ComposerPendingApprovalActions } from "./ComposerPendingApprovalActions";
15
16 function ChildApproval({ entry }: { entry: ChildThreadApproval }) {
17 const canRespond = useEnvironmentScope(entry.thread.environmentId, AuthOrchestrationOperateScope);
18 const respond = useAtomCommand(threadEnvironment.respondToApproval, { reportFailure: false });
19 const gt = useGT();
20 const messages = useMessages();
21 const [responding, setResponding] = useState(false);
22 const [error, setError] = useState<string | null>(null);
23 const { approval } = entry;
24 return (
25 <section
26 aria-label={gt("Approval from {title}", { title: entry.title })}
27 className="rounded-lg border border-border bg-background p-3 text-xs"
28 >
29 <p className="mb-2 font-medium">
30 <T>
31 Approval needed from <Var>{entry.title}</Var>
32 </T>
33 </p>
34 <pre className="max-h-40 overflow-auto whitespace-pre-wrap break-words font-mono">
35 {approval.detail || approval.requestKind}
36 </pre>
37 {approval.appName ? <p className="mt-2 text-muted-foreground">{approval.appName}</p> : null}
38 {approval.responseCapability !== "live" ? (
39 <p className="mt-2 text-warning">
40 <T>Provider process is gone. Interrupt or restart the child thread to respond.</T>
41 </p>
42 ) : null}
43 {approval.reason ? <p className="mt-2 text-muted-foreground">{approval.reason}</p> : null}
44 {approval.fullAccessOverride ? (
45 <p className="mt-2 text-muted-foreground">
46 {fullAccessOverrideExplanation(approval.fullAccessOverride, messages)}
47 </p>
48 ) : null}
49 <div className="mt-2 flex flex-wrap gap-2">
50 <ComposerPendingApprovalActions
51 presentation="inline"
52 requestId={approval.requestId}
53 isResponding={responding}
54 canRespond={approval.responseCapability === "live"}
55 disabled={!canRespond}
56 options={approval.options}
57 onRespondToApproval={async (requestId, decision) => {
58 if (!canRespond || responding) return;
59 setResponding(true);
60 setError(null);
61 const result = await respond({
62 environmentId: entry.thread.environmentId,
63 input: { threadId: entry.thread.threadId, requestId, decision },
64 });
65 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
66 const failure = squashAtomCommandFailure(result);
67 setError(
68 failure instanceof Error
69 ? failure.message
70 : gt("Failed to submit approval decision."),
71 );
72 }
73 setResponding(false);
74 return result;
75 }}
76 />
77 </div>
78 {error !== null ? (
79 <p role="alert" className="mt-2 text-destructive">
80 {error}
81 </p>
82 ) : null}
83 </section>
84 );
85 }
86
87 export function ChildThreadApprovals({ threadRef }: { threadRef: ScopedThreadRef | null }) {
88 const approvals = useAtomValue(childThreadApprovalsAtom(threadRef));
89 if (approvals.length === 0) return null;
90 return (
91 <div className="mb-2 flex max-h-80 flex-col gap-2 overflow-auto">
92 {approvals.map((entry) => (
93 <ChildApproval
94 key={JSON.stringify([
95 entry.thread.environmentId,
96 entry.thread.threadId,
97 entry.approval.requestId,
98 ])}
99 entry={entry}
100 />
101 ))}
102 </div>
103 );
104 }