Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-settings-options.ts

111 lines3.8 KB

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-native";
11​
12import { m, type MessageResolver } from "../../i18n/messages";
13​
14export const RUNTIME_MODE_CHOICES: ReadonlyArray<{
15 readonly mode: RuntimeMode;
16 readonly label: string;
17 readonly description: string;
18}> = [
19 {
20 mode: "approval-required",
21 label: msg("Supervised"),
22 description: msg("Ask before commands and file changes."),
23 },
24 {
25 mode: "auto-accept-edits",
26 label: msg("Auto-accept edits"),
27 description: msg("Auto-approve edits, ask before other actions."),
28 },
29 {
30 mode: "auto",
31 label: msg("Auto"),
32 description: msg("Supported providers approve routine actions; others still ask."),
33 },
34 {
35 mode: "full-access",
36 label: msg("Full access"),
37 description: msg("Allow commands and edits without prompts."),
38 },
39];
40​
41export function runtimeModeChoicesForSupportedModes(
42 supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
43) {
44 return supportedRuntimeModes && supportedRuntimeModes.length > 0
45 ? RUNTIME_MODE_CHOICES.filter((choice) => supportedRuntimeModes.includes(choice.mode))
46 : RUNTIME_MODE_CHOICES;
48​
49export type RuntimeModeChoice = (typeof RUNTIME_MODE_CHOICES)[number] & {
50 /** Set when a managed policy caps modes below this one, or the provider cannot run it here. */
51 readonly disabledReason?: string;
52};
53​
54/**
55 * Marks modes above a managed runtime ceiling. A control acting on several environments is
56 * managed when any of them is, so the first target that refuses a mode supplies its reason.
57 */
58export function withManagedRuntimeModeLimits(
59 choices: ReadonlyArray<RuntimeModeChoice>,
60 policies: ReadonlyArray<ManagedPolicySummary | undefined>,
61 resolve: MessageResolver = m,
62): ReadonlyArray<RuntimeModeChoice> {
63 return choices.map((choice) => {
64 const reason = policies
65 .map((policy) => managedRuntimeModeDenial(policy, choice.mode, resolve))
66 .find((denial) => denial !== undefined);
67 return reason === undefined ? choice : { ...choice, disabledReason: reason };
68 });
70​
71/**
72 * Marks modes the provider cannot run in the thread's folder. They stay listed, and a thread
73 * already in one keeps it, with the reason in place of the description.
74 */
75export function withProviderRuntimeModeLimits(
76 choices: ReadonlyArray<RuntimeModeChoice>,
77 limits: ReadonlyArray<ProviderRuntimeModeLimit> | undefined,
78 resolve: MessageResolver = m,
79): ReadonlyArray<RuntimeModeChoice> {
80 if (limits === undefined || limits.length === 0) return choices;
81 return choices.map((choice) => {
82 if (choice.disabledReason !== undefined) return choice;
83 const reason = providerRuntimeModeLimitMessage(limits, choice.mode, resolve);
84 return reason === undefined ? choice : { ...choice, disabledReason: reason };
85 });
87​
88/**
89 * A new thread's mode: the draft's choice unless the managed ceiling refuses it, then the
90 * default, which the server already reads back at or below the ceiling.
91 */
92export function resolveNewThreadRuntimeMode(
93 draftMode: RuntimeMode | undefined,
94 defaultMode: RuntimeMode,
95 policy: ManagedPolicySummary | undefined,
96): RuntimeMode {
97 return draftMode !== undefined && managedRuntimeModeDenial(policy, draftMode) === undefined
98 ? draftMode
99 : defaultMode;
101​
102export function compatibleRuntimeModeForChoices(
103 runtimeMode: RuntimeMode,
104 choices: ReadonlyArray<{ readonly mode: RuntimeMode }>,
105): RuntimeMode {
106 return choices.some((choice) => choice.mode === runtimeMode)
107 ? runtimeMode
108 : (choices[0]?.mode ?? runtimeMode);
110​
111export { selectableProviderOptionChoices as selectableChoices } from "@t3tools/client-runtime/model-traits";