Open History to see the last commit
1import { managedRuntimeModeDenial } from "@t3tools/client-runtime/contract-labels";
2import type { ManagedPolicySummary, RuntimeMode } from "@t3tools/contracts";
3import { msg } from "gt-react";
4import { m, type MessageResolver } from "~/i18n/messages";
5import {
6 type LucideIcon,
7 LockIcon,
8 PenLineIcon,
9 ShieldCheckIcon,
10 ShieldOffIcon,
11} from "lucide-react";
12
13export 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
40export 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. */
43export 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/**
55 * The modes a draft's send can run in. A multi-model send gives every thread the draft's mode
56 * and only that thread's provider narrows it, so the composer offers what any of them supports.
57 */
58export function draftSupportedRuntimeModes(
59 activeProviderModes: ReadonlyArray<RuntimeMode> | undefined,
60 multipleModelSelections: ReadonlyArray<{ readonly instanceId: string }> | null,
61 providers: ReadonlyArray<{
62 readonly instanceId: string;
63 readonly supportedRuntimeModes?: ReadonlyArray<RuntimeMode> | undefined;
64 }>,
65) {
66 if (multipleModelSelections === null || multipleModelSelections.length === 0) {
67 return activeProviderModes;
68 }
69 const supported = multipleModelSelections.map(
70 (selection) =>
71 providers.find((provider) => provider.instanceId === selection.instanceId)
72 ?.supportedRuntimeModes ?? [],
73 );
74 // A provider that reports no modes supports them all.
75 if (supported.some((modes) => modes.length === 0)) return undefined;
76 return runtimeModeOptions.filter((mode) => supported.some((modes) => modes.includes(mode)));
77}
78
79export function resolveRuntimeModeOption(
80 mode: RuntimeMode,
81 resolve: MessageResolver = m,
82): {
83 mode: RuntimeMode;
84 label: string;
85 description: string;
86 icon: LucideIcon;
87 disabledReason?: string;
88} {
89 const config = runtimeModeConfig[mode];
90 return {
91 mode,
92 ...config,
93 label: resolve(config.label),
94 description: resolve(config.description),
95 };
96}
97
98/** Why `mode` is unavailable under `managedPolicy`, or undefined when it is allowed. */
99export function runtimeModeDenial(
100 managedPolicy: ManagedPolicySummary | undefined,
101 mode: RuntimeMode,
102 resolve: MessageResolver = m,
103) {
104 const ceiling = managedPolicy?.limits?.maxRuntimeMode;
105 if (ceiling === undefined || managedRuntimeModeDenial(managedPolicy, mode) === undefined) {
106 return undefined;
107 }
108 return resolve(
109 msg("{organization} limits threads to {mode}.", {
110 organization: managedPolicy!.organization,
111 mode: resolve(runtimeModeConfig[ceiling].label),
112 }),
113 );
114}
115
116export function resolveRuntimeModeOptions(
117 modes: ReadonlyArray<RuntimeMode>,
118 managedPolicy: ManagedPolicySummary | undefined,
119 resolve: MessageResolver = m,
120) {
121 return modes.map((mode) => {
122 const option = resolveRuntimeModeOption(mode, resolve);
123 const disabledReason = runtimeModeDenial(managedPolicy, mode, resolve);
124 return disabledReason === undefined ? option : { ...option, disabledReason };
125 });
126}