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

main
ts 67 lines 2.78 KB
Raw
1 import { msg } from "gt-react";
2 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
3 import { m } from "~/i18n/messages";
4 import { useAtomValue } from "@effect/atom-react";
5 import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
6 import { useState } from "react";
7
8 import { useServerConfigs } from "../../state/entities";
9 import { orchestrationEnvironment } from "../../state/orchestration";
10 import {
11 isAtomCommandInterrupted,
12 squashAtomCommandFailure,
13 } from "@t3tools/client-runtime/state/runtime";
14 import { useAtomCommand } from "../../state/use-atom-command";
15 import { toastManager } from "../ui/toast";
16
17 /** Force stop for an agent that no longer answers Stop; shared by every place that offers it. */
18 export function useForceStopThread(environmentId: EnvironmentId) {
19 const forceStop = useAtomCommand(orchestrationEnvironment.forceStopThread);
20 const permitted = useAtomValue(
21 orchestrationEnvironment.forceStopThread.permissionAtom(environmentId),
22 );
23 const supported =
24 useServerConfigs().get(environmentId)?.environment.capabilities.threadForceStop === true;
25 const canForceStop = permitted && supported;
26 const [stoppingThreadId, setStoppingThreadId] = useState<ThreadId | null>(null);
27 /** Pass the run that ignored Stop, so a Force stop left open cannot hit a newer run. */
28 const forceStopThread = async (threadId: ThreadId, runId?: RunId) => {
29 if (!canForceStop || stoppingThreadId !== null) return;
30 setStoppingThreadId(threadId);
31 const result = await forceStop({
32 environmentId,
33 input: runId === undefined ? { threadId } : { threadId, runId },
34 });
35 setStoppingThreadId(null);
36 if (result._tag === "Failure") {
37 if (isAtomCommandInterrupted(result)) return;
38 const error = squashAtomCommandFailure(result);
39 toastManager.add({
40 type: "error",
41 title: m(msg("Could not force stop the agent")),
42 description: thrownErrorMessage(error, m) ?? undefined,
43 });
44 return;
45 }
46 const others = result.value.sharedWithThreadIds.length;
47 if (result.value.outcome === "releasing") {
48 toastManager.add({
49 type: "info",
50 title: m(msg("Turn stopped; the agent is still shutting down")),
51 });
52 }
53 if (result.value.outcome === "shared") {
54 toastManager.add({
55 type: "info",
56 title: m(msg("Turn stopped; the agent process kept running")),
57 description: m(
58 msg(
59 "{others, plural, =0 {It is shared with other working threads. Stop those to free it.} one {It is shared with {others} working thread. Stop those to free it.} other {It is shared with {others} working threads. Stop those to free it.}}",
60 { others },
61 ),
62 ),
63 });
64 }
65 };
66 return { canForceStop, stoppingThreadId, forceStopThread };
67 }