Open History to see the last commit
1import { Button } from "../../components/ui/Button";
2import {
3 AuthSettingsWriteScope,
4 isManagedSettingPath,
5 type ServerSettingsPatch,
6} from "@t3tools/contracts";
7import { useEnvironmentsWithScope } from "../../state/session";
8import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
9import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
10import { useAtomSet, useAtomValue } from "@effect/atom-react";
11import { AsyncResult } from "effect/reactivity";
12import { View } from "react-native";
13import { useSafeAreaInsets } from "react-native-safe-area-context";
14
15import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
16import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
17import { AppText as Text } from "../../components/ui/AppText";
18import { useEnvironments } from "../../state/environments";
19import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
20import { serverEnvironment } from "../../state/server";
21import { useAtomCommand } from "../../state/use-atom-command";
22import { SettingsSection } from "./components/SettingsSection";
23import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
24import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
25import type { ToolCallVisibility } from "@t3tools/contracts/settings";
26import { SettingsScreen } from "./components/SettingsScreen";
27import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
28import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
29import {
30 managedSettingReason,
31 resolveMobileSettingsTargets,
32 uniformMobileSetting,
33} from "./settings-scoped-server";
34
35export function SettingsLabsRouteScreen() {
36 const insets = useSafeAreaInsets();
37
38 return (
39 <SettingsScreen title="Labs">
40 <ScrollView
41 contentInsetAdjustmentBehavior="automatic"
42 showsVerticalScrollIndicator={false}
43 className="flex-1"
44 contentContainerClassName="gap-6 px-5 pt-4"
45 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
46 >
47 <ToolCallVisibilitySection />
48 <BetaSettingsSection />
49 <LegacySettingsSection />
50 </ScrollView>
51 </SettingsScreen>
52 );
53}
54
55const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
56
57/**
58 * Usage-limit and auto-settle rows. The Threads page owns their writes, so they share its queue
59 * with the page's other project overrides, and their project override reset.
60 */
61export function AutoSettleSettingsRows(props: {
62 readonly write: (patch: ServerSettingsPatch) => void;
63 readonly pending: boolean;
64}) {
65 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
66 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
67 const writableEnvironments = useEnvironmentsWithScope(selectedTargets, AuthSettingsWriteScope);
68 const projectSelected = selectedProjectKey !== null;
69
70 const projectMembers = projectSelected
71 ? (selectedProject?.members.map((member) => member.project) ?? [])
72 : null;
73 const displayTargets = resolveMobileSettingsTargets(
74 selectedTargets.filter(supportsSharedSettingsSync),
75 projectMembers,
76 );
77 // The page saves to all its targets, so any read-only one blocks the save.
78 const canWriteSettings =
79 displayTargets.length > 0 &&
80 resolveMobileSettingsTargets(selectedTargets, projectMembers).every((target) =>
81 writableEnvironments.has(target.environment.environmentId),
82 );
83 const reference = displayTargets[0] ?? null;
84 const referenceSettings = reference?.settings ?? null;
85
86 if (reference === null || referenceSettings === null) {
87 return null;
88 }
89
90 const writeToAll = (
91 patch: Partial<AutoSettleSettings> & {
92 autoResumeLimitedThreads?: boolean;
93 snoozeLimitedThreads?: boolean;
94 },
95 ) => props.write(patch);
96
97 const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
98 {
99 environmentId: reference.environment.environmentId,
100 projectId: reference.projectId,
101 settings: referenceSettings,
102 },
103 displayTargets.map((target) => ({
104 environmentId: target.environment.environmentId,
105 projectId: target.projectId,
106 label: target.environment.label,
107 settings: target.settings,
108 })),
109 );
110
111 const disabled = !canWriteSettings || props.pending;
112 const disabledFor = (...keys: string[]) =>
113 disabled || managedSettingReason(displayTargets, ...keys) !== undefined;
114 const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
115
116 return (
117 <View className="gap-6">
118 {!projectSelected ? (
119 <SettingsSection title="Usage limits">
120 <SettingsSwitchRow
121 icon="clock"
122 label="Resume after usage limits"
123 subtitle="Automatically resume threads when their usage limits reset. You can cancel auto-resume in each thread."
124 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
125 disabled={disabledFor("autoResumeLimitedThreads")}
126 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
127 />
128 <SettingsSwitchRow
129 icon="clock"
130 label="Snooze until limits reset"
131 subtitle="Snooze threads until their usage limits reset. Enable auto-resume to continue the agent after the reset."
132 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
133 disabled={disabledFor("snoozeLimitedThreads")}
134 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
135 />
136 </SettingsSection>
137 ) : null}
138 <SettingsSection title="Auto-settle">
139 <SettingsSwitchRow
140 icon="arrow.triangle.branch"
141 label="Auto-settle merged threads"
142 value={referenceSettings.sidebarAutoSettleOnMerge}
143 disabled={disabledFor("sidebarAutoSettleOnMerge")}
144 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
145 />
146 <SettingsSwitchRow
147 icon="clock"
148 label="Auto-settle inactive threads"
149 value={afterDays !== null}
150 disabled={disabledFor("sidebarAutoSettleAfterDays")}
151 onValueChange={(value) =>
152 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
153 }
154 />
155 {afterDays !== null ? (
156 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
157 <View className="w-[22px] android:w-6" />
158 <Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
159 <AutoSettleDaysField
160 value={afterDays}
161 disabled={disabledFor("sidebarAutoSettleAfterDays")}
162 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
163 />
164 </View>
165 ) : null}
166 </SettingsSection>
167 {!props.pending && mismatches.length > 0 ? (
168 <SettingsSection title="Across environments">
169 <View className="gap-3 p-4">
170 <Text className="text-base text-foreground">Auto-settle defaults differ</Text>
171 <Text className="text-sm text-foreground-muted">
172 {mismatches.map((mismatch) => mismatch.label).join(", ")}
173 </Text>
174 <Button
175 label="Apply auto-settle defaults"
176 tone="secondary"
177 disabled={disabledFor("sidebarAutoSettleOnMerge", "sidebarAutoSettleAfterDays")}
178 onPress={() => writeToAll(autoSettlePatch)}
179 />
180 </View>
181 </SettingsSection>
182 ) : null}
183 </View>
184 );
185}
186
187export function WorkingSectionSettings() {
188 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
189 const preferences = useAtomValue(mobilePreferencesAtom);
190 const workingShelfEnabled =
191 AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled !== false;
192
193 return (
194 <View className="gap-3">
195 <SettingsSection title="Sidebar">
196 <SettingsSwitchRow
197 icon="bolt.circle"
198 label="Working section"
199 value={workingShelfEnabled}
200 onValueChange={(value) => savePreferences({ workingShelfEnabled: value })}
201 />
202 </SettingsSection>
203 <Text className="px-2 text-sm text-foreground-muted">
204 Fold working and monitoring threads into a Working section. They return to the top of the
205 list when they need you. While this is on, active threads are ordered by time and cannot be
206 moved.
207 </Text>
208 </View>
209 );
210}
211
212export function NewThreadSettingsSection() {
213 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
214 const preferences = useAtomValue(mobilePreferencesAtom);
215 const useLastProject =
216 AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false;
217
218 return (
219 <SettingsSection title="New threads">
220 <SettingsSwitchRow
221 icon="square.and.pencil"
222 label="Use last project"
223 subtitle="Start new threads in the last project you used on this device."
224 value={useLastProject}
225 onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })}
226 />
227 </SettingsSection>
228 );
229}
230
231/** Per machine, since each machine's own setting decides whether it links and answers links. */
232function BetaSettingsSection() {
233 const { environments } = useEnvironments();
234 const supported = environments.filter(
235 (environment) =>
236 environment.connection.phase === "connected" &&
237 environment.serverConfig?.environment.capabilities.peerLinks === true,
238 );
239 const writable = useEnvironmentsWithScope(supported, AuthSettingsWriteScope);
240 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
241 label: "linked environments beta update",
242 reportFailure: true,
243 });
244 if (supported.length === 0) return null;
245 return (
246 <View className="gap-3">
247 <SettingsSection title="Beta: Linked environments">
248 {supported.map((environment) => {
249 const policy = environment.serverConfig?.managedPolicy;
250 const managed = isManagedSettingPath(policy, "linkedEnvironmentsEnabled");
251 return (
252 <SettingsSwitchRow
253 key={environment.environmentId}
254 icon="link"
255 label={environment.label}
256 {...(managed ? { subtitle: `Managed by ${policy?.organization}.` } : {})}
257 value={environment.serverConfig?.settings.linkedEnvironmentsEnabled === true}
258 disabled={managed || !writable.has(environment.environmentId)}
259 onValueChange={(value) =>
260 void updateSettings({
261 environmentId: environment.environmentId,
262 input: { patch: { linkedEnvironmentsEnabled: value } },
263 })
264 }
265 />
266 );
267 })}
268 </SettingsSection>
269 <Text className="px-2 text-sm text-foreground-muted">
270 Let agents on a machine work on your other machines, and theirs on it. Turn it on for each
271 machine. While off, nothing new starts over a link; running tasks finish.
272 </Text>
273 </View>
274 );
275}
276
277const TOOL_CALL_VISIBILITY_OPTIONS: ReadonlyArray<{
278 readonly value: ToolCallVisibility;
279 readonly label: string;
280}> = [
281 { value: "showAll", label: "Show all" },
282 { value: "hideSuccessful", label: "Hide successful" },
283 { value: "hideAll", label: "Hide all" },
284 { value: "zen", label: "Zen" },
285];
286
287function ToolCallVisibilitySection() {
288 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
289 const preferences = useAtomValue(mobilePreferencesAtom);
290 const selected = AsyncResult.isSuccess(preferences)
291 ? (preferences.value.toolCallVisibility ?? "showAll")
292 : null;
293
294 return (
295 <View className="gap-3">
296 <SettingsSection title="Beta: Tool calls in chat">
297 {TOOL_CALL_VISIBILITY_OPTIONS.map((option, index) => (
298 <ListChoiceRow
299 key={option.value}
300 label={option.label}
301 selected={selected === option.value}
302 separated={index > 0}
303 disabled={selected === null}
304 onPress={() => savePreferences({ toolCallVisibility: option.value })}
305 />
306 ))}
307 </SettingsSection>
308 <Text className="px-2 text-sm text-foreground-muted">
309 Hide the commands, edits, and searches agents run. Hide successful keeps failures. Zen shows
310 only final answers. Approvals, questions, and errors always show.
311 </Text>
312 </View>
313 );
314}
315
316function LegacySettingsSection() {
317 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
318 const preferences = useAtomValue(mobilePreferencesAtom);
319 const planModeEnabled =
320 AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
321
322 return (
323 <View className="gap-3">
324 <SettingsSection title="Legacy">
325 <SettingsSwitchRow
326 icon="hammer"
327 label="Plan Mode"
328 value={planModeEnabled}
329 onValueChange={(value) => savePreferences({ planModeEnabled: value })}
330 />
331 </SettingsSection>
332 <Text className="px-2 text-sm text-foreground-muted">
333 Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
334 control; otherwise every task runs in Build mode.
335 </Text>
336 </View>
337 );
338}