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

main
ts 101 lines 3.25 KB
Raw
1 import { managedRuntimeModeDenial } from "@t3tools/client-runtime/contract-labels";
2 import type { ManagedPolicySummary, RuntimeMode } from "@t3tools/contracts";
3 import { msg } from "gt-react";
4 import { m, type MessageResolver } from "~/i18n/messages";
5 import {
6 type LucideIcon,
7 LockIcon,
8 PenLineIcon,
9 ShieldCheckIcon,
10 ShieldOffIcon,
11 } from "lucide-react";
12
13 export const runtimeModeConfig: Record<
14 RuntimeMode,
15 { label: string; description: string; icon: LucideIcon }
16 > = {
17 "approval-required": {
18 label: msg("Supervised", { $context: "permission mode, agent asks before acting" }),
19 description: msg("Ask before commands and file changes."),
20 icon: LockIcon,
21 },
22 "auto-accept-edits": {
23 label: msg("Auto-accept edits"),
24 description: msg("Auto-approve edits, ask before other actions."),
25 icon: PenLineIcon,
26 },
27 auto: {
28 label: msg("Auto", { $context: "permission mode, provider approves routine actions" }),
29 description: msg("Supported providers approve routine actions; others still ask."),
30 icon: ShieldCheckIcon,
31 },
32 "full-access": {
33 label: msg("Full access"),
34 description: msg("Allow commands and edits without prompts."),
35 // An open lock reads like a closed one at small sizes; a crossed shield does not.
36 icon: ShieldOffIcon,
37 },
38 };
39
40 export const runtimeModeOptions = Object.keys(runtimeModeConfig) as RuntimeMode[];
41
42 /** The modes a provider supports (all when it reports none) and the mode a thread really runs in. */
43 export function resolveCompatibleRuntimeMode(
44 runtimeMode: RuntimeMode,
45 supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
46 ) {
47 const modes =
48 supportedRuntimeModes && supportedRuntimeModes.length > 0
49 ? runtimeModeOptions.filter((mode) => supportedRuntimeModes.includes(mode))
50 : runtimeModeOptions;
51 return { modes, mode: modes.includes(runtimeMode) ? runtimeMode : (modes[0] ?? runtimeMode) };
52 }
53
54 export function resolveRuntimeModeOption(
55 mode: RuntimeMode,
56 resolve: MessageResolver = m,
57 ): {
58 mode: RuntimeMode;
59 label: string;
60 description: string;
61 icon: LucideIcon;
62 disabledReason?: string;
63 } {
64 const config = runtimeModeConfig[mode];
65 return {
66 mode,
67 ...config,
68 label: resolve(config.label),
69 description: resolve(config.description),
70 };
71 }
72
73 /** Why `mode` is unavailable under `managedPolicy`, or undefined when it is allowed. */
74 export function runtimeModeDenial(
75 managedPolicy: ManagedPolicySummary | undefined,
76 mode: RuntimeMode,
77 resolve: MessageResolver = m,
78 ) {
79 const ceiling = managedPolicy?.limits?.maxRuntimeMode;
80 if (ceiling === undefined || managedRuntimeModeDenial(managedPolicy, mode) === undefined) {
81 return undefined;
82 }
83 return resolve(
84 msg("{organization} limits threads to {mode}.", {
85 organization: managedPolicy!.organization,
86 mode: resolve(runtimeModeConfig[ceiling].label),
87 }),
88 );
89 }
90
91 export function resolveRuntimeModeOptions(
92 modes: ReadonlyArray<RuntimeMode>,
93 managedPolicy: ManagedPolicySummary | undefined,
94 resolve: MessageResolver = m,
95 ) {
96 return modes.map((mode) => {
97 const option = resolveRuntimeModeOption(mode, resolve);
98 const disabledReason = runtimeModeDenial(managedPolicy, mode, resolve);
99 return disabledReason === undefined ? option : { ...option, disabledReason };
100 });
101 }