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

main
ts 61 lines 2.37 KB
Raw
1 import { runAtomCommand, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
3 import {
4 AuthOrchestrationOperateScope,
5 type EnvironmentId,
6 type ModelSelection,
7 type ThreadId,
8 } from "@t3tools/contracts";
9
10 import { scopedThreadKey } from "../lib/scopedEntities";
11 import * as Dialog from "../components/ui/Dialog";
12 import { msg } from "gt-react-native";
13 import { m } from "../i18n/messages";
14 import { appAtomRegistry } from "./atom-registry";
15 import { readEnvironmentScope } from "./session";
16 import { environmentThreadShells, threadEnvironment } from "./threads";
17 import {
18 releaseComposerDraftModelSelection,
19 updateComposerDraftSettings,
20 } from "./use-composer-drafts";
21
22 /** Sets an existing thread's pick on the thread, so any next turn runs on it. */
23 export function selectThreadModel(
24 ref: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId },
25 value: ModelSelection,
26 ): void {
27 const threadKey = scopedThreadKey(ref.environmentId, ref.threadId);
28 // Its own object, so an older reply for an equal pick cannot release this one.
29 const modelSelection = { ...value };
30 updateComposerDraftSettings(threadKey, { modelSelection });
31 const thread = appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
32 // Another instance replaces the live session, which may hold an open question; that pick
33 // still waits for a sent message.
34 if (
35 thread?.modelSelection.instanceId !== modelSelection.instanceId ||
36 !readEnvironmentScope(ref.environmentId, AuthOrchestrationOperateScope)
37 ) {
38 return;
39 }
40 void runAtomCommand(
41 appAtomRegistry,
42 threadEnvironment.setModelSelection,
43 { environmentId: ref.environmentId, input: { threadId: ref.threadId, modelSelection } },
44 { label: "change model", reportFailure: true, reportDefect: true },
45 ).then((result) => {
46 // Released on the reply, so a later change from another client shows.
47 if (result._tag === "Success") {
48 releaseComposerDraftModelSelection(threadKey, modelSelection);
49 } else {
50 const error = squashAtomCommandFailure(result);
51 Dialog.alert(
52 m(msg("Could not apply model selection")),
53 m(
54 msg("This model pick was not saved. {detail}", {
55 detail: errorDisplayMessage(error, m) ?? m(msg("Try again.")),
56 }),
57 ),
58 );
59 }
60 });
61 }