The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId, ManagedPolicySummary, ProviderDriverKind } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../i18n/messages"; |
| 5 | |
| 6 | import { useServerConfigs } from "./entities"; |
| 7 | import { usePrimaryEnvironmentId } from "./environments"; |
| 8 | |
| 9 | /** The policy an environment's administrator applies, or undefined when it is unmanaged. */ |
| 10 | export function useManagedPolicy( |
| 11 | environmentId: EnvironmentId | null | undefined, |
| 12 | ): ManagedPolicySummary | undefined { |
| 13 | const serverConfigs = useServerConfigs(); |
| 14 | return environmentId == null ? undefined : serverConfigs.get(environmentId)?.managedPolicy; |
| 15 | } |
| 16 | |
| 17 | export function usePrimaryManagedPolicy(): ManagedPolicySummary | undefined { |
| 18 | return useManagedPolicy(usePrimaryEnvironmentId()); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Why a control acting on several environments is unavailable: the first denial any target's |
| 23 | * policy gives, since one refusing target refuses the whole action. |
| 24 | */ |
| 25 | export function firstManagedDenial( |
| 26 | policies: Iterable<ManagedPolicySummary | undefined>, |
| 27 | deny: (policy: ManagedPolicySummary | undefined) => string | undefined, |
| 28 | ): string | undefined { |
| 29 | for (const policy of policies) { |
| 30 | const denial = deny(policy); |
| 31 | if (denial !== undefined) return denial; |
| 32 | } |
| 33 | return undefined; |
| 34 | } |
| 35 | |
| 36 | /** What usage-data notices say when the policy turns product analytics off. */ |
| 37 | export function managedTelemetryOffMessage( |
| 38 | policy: ManagedPolicySummary | undefined, |
| 39 | resolve: MessageResolver = m, |
| 40 | ): string | undefined { |
| 41 | return policy?.limits?.telemetry === false |
| 42 | ? resolve( |
| 43 | msg("{organization} turned off anonymous usage data.", { |
| 44 | organization: policy.organization, |
| 45 | }), |
| 46 | ) |
| 47 | : undefined; |
| 48 | } |
| 49 | |
| 50 | export interface ManagedInstanceLocks { |
| 51 | /** Keys inside the instance's `config` users cannot change. */ |
| 52 | readonly configKeys: ReadonlySet<string>; |
| 53 | /** Environment variables users cannot edit, rename, or remove. */ |
| 54 | readonly environmentNames: ReadonlySet<string>; |
| 55 | /** Under a runtime-mode ceiling every variable is the administrator's, not only named ones. */ |
| 56 | readonly wholeEnvironment: boolean; |
| 57 | readonly reason: string; |
| 58 | } |
| 59 | |
| 60 | /** What the policy pins on instances of `driver`, or undefined when nothing is. */ |
| 61 | export function managedInstanceLocks( |
| 62 | policy: ManagedPolicySummary | undefined, |
| 63 | driver: ProviderDriverKind, |
| 64 | resolve: MessageResolver = m, |
| 65 | ): ManagedInstanceLocks | undefined { |
| 66 | const paths = policy?.limits?.providers?.lockedInstanceFields?.[driver]; |
| 67 | if (policy === undefined || paths === undefined || paths.length === 0) return undefined; |
| 68 | const fieldsUnder = (prefix: string) => |
| 69 | new Set(paths.flatMap((path) => (path.startsWith(prefix) ? [path.slice(prefix.length)] : []))); |
| 70 | return { |
| 71 | configKeys: fieldsUnder("config."), |
| 72 | environmentNames: fieldsUnder("environment."), |
| 73 | wholeEnvironment: paths.includes("environment"), |
| 74 | reason: resolve(msg("Managed by {organization}.", { organization: policy.organization })), |
| 75 | }; |
| 76 | } |