The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { managedRuntimeModeDenial } from "@t3tools/client-runtime/contract-labels"; |
| 2 | import type { ManagedPolicySummary, RuntimeMode } from "@t3tools/contracts"; |
| 3 | import { msg } from "gt-react-native"; |
| 4 | |
| 5 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 6 | |
| 7 | export const RUNTIME_MODE_CHOICES: ReadonlyArray<{ |
| 8 | readonly mode: RuntimeMode; |
| 9 | readonly label: string; |
| 10 | readonly description: string; |
| 11 | }> = [ |
| 12 | { |
| 13 | mode: "approval-required", |
| 14 | label: msg("Supervised"), |
| 15 | description: msg("Ask before commands and file changes."), |
| 16 | }, |
| 17 | { |
| 18 | mode: "auto-accept-edits", |
| 19 | label: msg("Auto-accept edits"), |
| 20 | description: msg("Auto-approve edits, ask before other actions."), |
| 21 | }, |
| 22 | { |
| 23 | mode: "auto", |
| 24 | label: msg("Auto"), |
| 25 | description: msg("Supported providers approve routine actions; others still ask."), |
| 26 | }, |
| 27 | { |
| 28 | mode: "full-access", |
| 29 | label: msg("Full access"), |
| 30 | description: msg("Allow commands and edits without prompts."), |
| 31 | }, |
| 32 | ]; |
| 33 | |
| 34 | export function runtimeModeChoicesForSupportedModes( |
| 35 | supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined, |
| 36 | ) { |
| 37 | return supportedRuntimeModes && supportedRuntimeModes.length > 0 |
| 38 | ? RUNTIME_MODE_CHOICES.filter((choice) => supportedRuntimeModes.includes(choice.mode)) |
| 39 | : RUNTIME_MODE_CHOICES; |
| 40 | } |
| 41 | |
| 42 | export type RuntimeModeChoice = (typeof RUNTIME_MODE_CHOICES)[number] & { |
| 43 | /** Set when a target environment's managed policy caps runtime modes below this one. */ |
| 44 | readonly disabledReason?: string; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Marks modes above a managed runtime ceiling. A control acting on several environments is |
| 49 | * managed when any of them is, so the first target that refuses a mode supplies its reason. |
| 50 | */ |
| 51 | export function withManagedRuntimeModeLimits( |
| 52 | choices: ReadonlyArray<RuntimeModeChoice>, |
| 53 | policies: ReadonlyArray<ManagedPolicySummary | undefined>, |
| 54 | resolve: MessageResolver = m, |
| 55 | ): ReadonlyArray<RuntimeModeChoice> { |
| 56 | return choices.map((choice) => { |
| 57 | const reason = policies |
| 58 | .map((policy) => managedRuntimeModeDenial(policy, choice.mode, resolve)) |
| 59 | .find((denial) => denial !== undefined); |
| 60 | return reason === undefined ? choice : { ...choice, disabledReason: reason }; |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * A new thread's mode: the draft's choice unless the managed ceiling refuses it, then the |
| 66 | * default, which the server already reads back at or below the ceiling. |
| 67 | */ |
| 68 | export function resolveNewThreadRuntimeMode( |
| 69 | draftMode: RuntimeMode | undefined, |
| 70 | defaultMode: RuntimeMode, |
| 71 | policy: ManagedPolicySummary | undefined, |
| 72 | ): RuntimeMode { |
| 73 | return draftMode !== undefined && managedRuntimeModeDenial(policy, draftMode) === undefined |
| 74 | ? draftMode |
| 75 | : defaultMode; |
| 76 | } |
| 77 | |
| 78 | export function compatibleRuntimeModeForChoices( |
| 79 | runtimeMode: RuntimeMode, |
| 80 | choices: ReadonlyArray<{ readonly mode: RuntimeMode }>, |
| 81 | ): RuntimeMode { |
| 82 | return choices.some((choice) => choice.mode === runtimeMode) |
| 83 | ? runtimeMode |
| 84 | : (choices[0]?.mode ?? runtimeMode); |
| 85 | } |
| 86 | |
| 87 | export { selectableProviderOptionChoices as selectableChoices } from "@t3tools/client-runtime/model-traits"; |