Open History to see the last commit
1import {
2 managedRuntimeModeDenial,
3 providerRuntimeModeLimitMessage,
4} from "@t3tools/client-runtime/contract-labels";
5import type {
6 ManagedPolicySummary,
7 ProviderRuntimeModeLimit,
8 RuntimeMode,
9} from "@t3tools/contracts";
10import { msg } from "gt-react";
11import { m, type MessageResolver } from "~/i18n/messages";
12import {
13 type LucideIcon,
14 LockIcon,
15 PenLineIcon,
16 ShieldCheckIcon,
17 ShieldOffIcon,
18} from "lucide-react";
19
20export const runtimeModeConfig: Record<
21 RuntimeMode,
22 { label: string; description: string; icon: LucideIcon }
23> = {
24 "approval-required": {
25 label: msg("Supervised", { $context: "permission mode, agent asks before acting" }),
26 description: msg("Ask before commands and file changes."),
27 icon: LockIcon,
28 },
29 "auto-accept-edits": {
30 label: msg("Auto-accept edits"),
31 description: msg("Auto-approve edits, ask before other actions."),
32 icon: PenLineIcon,
33 },
34 auto: {
35 label: msg("Auto", { $context: "permission mode, provider approves routine actions" }),
36 description: msg("Supported providers approve routine actions; others still ask."),
37 icon: ShieldCheckIcon,
38 },
39 "full-access": {
40 label: msg("Full access"),
41 description: msg("Allow commands and edits without prompts."),
42 // An open lock reads like a closed one at small sizes; a crossed shield does not.
43 icon: ShieldOffIcon,
44 },
45};
46
47export const runtimeModeOptions = Object.keys(runtimeModeConfig) as RuntimeMode[];
48
49/** The modes a provider supports (all when it reports none) and the mode a thread really runs in. */
50export function resolveCompatibleRuntimeMode(
51 runtimeMode: RuntimeMode,
52 supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
53) {
54 const modes =
55 supportedRuntimeModes && supportedRuntimeModes.length > 0
56 ? runtimeModeOptions.filter((mode) => supportedRuntimeModes.includes(mode))
57 : runtimeModeOptions;
58 return { modes, mode: modes.includes(runtimeMode) ? runtimeMode : (modes[0] ?? runtimeMode) };
59}
60
61/**
62 * The modes a draft's send can run in. A multi-model send gives every thread the draft's mode
63 * and only that thread's provider narrows it, so the composer offers what any of them supports.
64 */
65export function draftSupportedRuntimeModes(
66 activeProviderModes: ReadonlyArray<RuntimeMode> | undefined,
67 multipleModelSelections: ReadonlyArray<{ readonly instanceId: string }> | null,
68 providers: ReadonlyArray<{
69 readonly instanceId: string;
70 readonly supportedRuntimeModes?: ReadonlyArray<RuntimeMode> | undefined;
71 }>,
72) {
73 if (multipleModelSelections === null || multipleModelSelections.length === 0) {
74 return activeProviderModes;
75 }
76 const supported = multipleModelSelections.map(
77 (selection) =>
78 providers.find((provider) => provider.instanceId === selection.instanceId)
79 ?.supportedRuntimeModes ?? [],
80 );
81 // A provider that reports no modes supports them all.
82 if (supported.some((modes) => modes.length === 0)) return undefined;
83 return runtimeModeOptions.filter((mode) => supported.some((modes) => modes.includes(mode)));
84}
85
86/**
87 * The provider whose folder limits a draft's send is under: the one every thread it starts will
88 * use. Selected models on different providers share none, so no one provider's limits apply.
89 */
90export function draftRuntimeModeLimitsInstance<InstanceId extends string>(
91 activeInstanceId: InstanceId | undefined,
92 multipleModelSelections: ReadonlyArray<{ readonly instanceId: InstanceId }> | null,
93): InstanceId | undefined {
94 const first = multipleModelSelections?.[0];
95 if (multipleModelSelections === null || first === undefined) return activeInstanceId;
96 return multipleModelSelections.every((selection) => selection.instanceId === first.instanceId)
97 ? first.instanceId
98 : undefined;
99}
100
101export function resolveRuntimeModeOption(
102 mode: RuntimeMode,
103 resolve: MessageResolver = m,
104): {
105 mode: RuntimeMode;
106 label: string;
107 description: string;
108 icon: LucideIcon;
109 disabledReason?: string;
110} {
111 const config = runtimeModeConfig[mode];
112 return {
113 mode,
114 ...config,
115 label: resolve(config.label),
116 description: resolve(config.description),
117 };
118}
119
120/** Why `mode` is unavailable under `managedPolicy`, or undefined when it is allowed. */
121export function runtimeModeDenial(
122 managedPolicy: ManagedPolicySummary | undefined,
123 mode: RuntimeMode,
124 resolve: MessageResolver = m,
125) {
126 const ceiling = managedPolicy?.limits?.maxRuntimeMode;
127 if (ceiling === undefined || managedRuntimeModeDenial(managedPolicy, mode) === undefined) {
128 return undefined;
129 }
130 return resolve(
131 msg("{organization} limits threads to {mode}.", {
132 organization: managedPolicy!.organization,
133 mode: resolve(runtimeModeConfig[ceiling].label),
134 }),
135 );
136}
137
138/**
139 * `limits` are modes the provider cannot run in the current folder. They stay listed, disabled
140 * with the reason; a thread already in one keeps it and shows that reason as its description.
141 */
142export function resolveRuntimeModeOptions(
143 modes: ReadonlyArray<RuntimeMode>,
144 managedPolicy: ManagedPolicySummary | undefined,
145 resolve: MessageResolver = m,
146 limits?: ReadonlyArray<ProviderRuntimeModeLimit>,
147) {
148 return modes.map((mode) => {
149 const option = resolveRuntimeModeOption(mode, resolve);
150 const disabledReason =
151 runtimeModeDenial(managedPolicy, mode, resolve) ??
152 providerRuntimeModeLimitMessage(limits, mode, resolve);
153 return disabledReason === undefined ? option : { ...option, disabledReason };
154 });
155}