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

main
ts 55 lines 2.49 KB
Raw
1 import { msg } from "gt-react";
2 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
3 import { m } from "~/i18n/messages";
4 import { RegistryContext } from "@effect/atom-react";
5 import { scopedThreadKey } from "@t3tools/client-runtime/environment";
6 import {
7 isAtomCommandInterrupted,
8 squashAtomCommandFailure,
9 } from "@t3tools/client-runtime/state/runtime";
10 import { useContext } from "react";
11
12 import { type ComposerThreadTarget, useComposerDraftStore } from "../../composerDraftStore";
13 import { environmentThreadShells, threadEnvironment } from "../../state/threads";
14 import { useOrchestrationCommand } from "../../state/use-orchestration-command";
15 import { stackedThreadToast, toastManager } from "../ui/toast";
16
17 /** Sets an existing thread's composer pick on the thread, so any next turn runs on it. */
18 export function useApplyThreadModelPick() {
19 const registry = useContext(RegistryContext);
20 const setModelSelection = useOrchestrationCommand(threadEnvironment.setModelSelection, {
21 label: "change model",
22 });
23 return async (target: ComposerThreadTarget) => {
24 if (typeof target === "string") return;
25 const store = useComposerDraftStore.getState();
26 const draft = store.draftsByThreadKey[scopedThreadKey(target)];
27 if (!draft?.modelSelectionExplicit || !draft.activeProvider) return;
28 const pick = draft.modelSelectionByProvider[draft.activeProvider];
29 const thread = registry.get(environmentThreadShells.threadShellAtom(target));
30 if (!pick || !thread || store.draftThreadsByThreadKey[scopedThreadKey(target)]) return;
31 // Another instance replaces the live session, which may hold an open question; that pick
32 // still waits for a sent message.
33 if (pick.instanceId !== thread.modelSelection.instanceId) return;
34 const result = await setModelSelection({
35 environmentId: target.environmentId,
36 input: { threadId: target.threadId, modelSelection: pick },
37 });
38 if (result._tag === "Success") {
39 useComposerDraftStore.getState().releaseModelSelection(target, pick);
40 } else if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
41 const error = squashAtomCommandFailure(result);
42 toastManager.add(
43 stackedThreadToast({
44 type: "error",
45 title: m(msg("Could not apply model selection")),
46 description: m(
47 msg("This model pick was not saved. {detail}", {
48 detail: thrownErrorMessage(error, m) ?? m(msg("Try again.")),
49 }),
50 ),
51 }),
52 );
53 }
54 };
55 }