Open History to see the last commit
1import { isElectron } from "~/env";
2import { isMacPlatform, isWindowsPlatform, normalizeSearchText } from "~/lib/utils";
3import { STATIC_KEYBINDING_COMMANDS, type KeybindingCommand } from "@t3tools/contracts";
4import type { EnvironmentId } from "@t3tools/contracts";
5import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
6import { DEFAULT_KEYBINDINGS } from "@t3tools/shared/keybindings";
7import { commandLabel } from "./KeybindingsSettings.logic";
8import {
9 validateSettingsScopeSearch,
10 type ResolvedSettingsScope,
11 type SettingsScopeSearch,
12} from "./settingsScope";
13
14export type SettingsPath =
15 | "/settings/projects"
16 | "/settings/general"
17 | "/settings/appearance"
18 | "/settings/keybindings"
19 | "/settings/snap-shot"
20 | "/settings/providers"
21 | "/settings/integrations"
22 | "/settings/source-control"
23 | "/settings/storage"
24 | "/settings/connections"
25 | "/settings/archived";
26
27/**
28 * Where a setting can be edited. Device-local rows have no scope: they render
29 * at every selection. `project-defaults` rows accept project overrides, so
30 * they are reachable from any server-backed selection.
31 */
32export type SettingsSearchScope =
33 | "environment"
34 | "environment-defaults"
35 | "project-defaults"
36 | "project"
37 | "checkout"
38 | "connections";
39
40export interface SettingsSearchItem {
41 readonly id: string;
42 readonly title: string;
43 readonly to: SettingsPath;
44 readonly targetId?: string;
45 /** Descriptions, option labels, and aliases people may remember instead of the title. */
46 readonly searchTerms?: ReadonlyArray<string>;
47 readonly scope?: SettingsSearchScope;
48 // Its row only renders in the desktop app, so a browser result would land on
49 // an anchor that isn't there.
50 readonly desktopOnly?: boolean;
51 readonly macOnly?: boolean;
52 // Its row only renders on Windows desktop, so other desktop platforms must
53 // not expose a result that points to a missing anchor.
54 readonly windowsOnly?: boolean;
55 readonly cloudOnly?: boolean;
56 readonly environmentOnly?: boolean;
57 readonly providerSettingsOnly?: boolean;
58 readonly localBackendManagementOnly?: boolean;
59 readonly localEnvironmentOnly?: boolean;
60 readonly wslAvailableOnly?: boolean;
61 /**
62 * Sorts after every other match. Keybinding commands mirror rows on other
63 * surfaces, so "model" must still lead with Default model, not Model Picker.
64 */
65 readonly secondary?: boolean;
66 readonly requiresThreadAutoSettlement?: boolean;
67}
68
69export interface SettingsSearchAvailability {
70 readonly localEnvironmentDisabled?: boolean;
71 readonly hasCloudPublicConfig: boolean;
72 readonly hasEnvironment: boolean;
73 readonly hasProviderSettingsEnvironment: boolean;
74 readonly canManageLocalBackend: boolean;
75 readonly isWslSettingsRowVisible: boolean;
76 readonly hasThreadAutoSettlement: boolean;
77}
78
79/**
80 * Section labels in sidebar order. The sidebar nav and the search-result
81 * subtitles both render from this record, so each label exists once.
82 */
83export const SETTINGS_SECTION_LABELS: Readonly<Record<SettingsPath, string>> = {
84 "/settings/projects": "Project",
85 "/settings/general": "General",
86 "/settings/appearance": "Appearance",
87 "/settings/keybindings": "Keybindings",
88 "/settings/snap-shot": "SnapShots",
89 "/settings/providers": "Providers",
90 "/settings/integrations": "Integrations",
91 "/settings/source-control": "Source Control",
92 "/settings/storage": "Storage",
93 "/settings/connections": "Connections",
94 "/settings/archived": "Archive",
95};
96
97/** Anchor id of the first row bound to `command` on the Keybindings page. */
98export function keybindingSearchAnchorId<Command extends KeybindingCommand>(command: Command) {
99 return `keybinding-${command}` as const;
100}
101
102/**
103 * One result per built-in command, alphabetical by label. The anchor is
104 * the command's first row; default keys are searchable so "mod+b" lands on
105 * Sidebar: Toggle. A command with no default binding may have no row, so it
106 * points at the section instead.
107 */
108const KEYBINDING_SEARCH_ITEMS = STATIC_KEYBINDING_COMMANDS.toSorted((left, right) =>
109 commandLabel(left).localeCompare(commandLabel(right)),
110).map((command) => {
111 const defaultKeys = DEFAULT_KEYBINDINGS.filter((binding) => binding.command === command).map(
112 (binding) => binding.key,
113 );
114 return {
115 id: keybindingSearchAnchorId(command),
116 title: commandLabel(command),
117 to: "/settings/keybindings" as const,
118 searchTerms: [command, ...defaultKeys],
119 secondary: true,
120 ...(defaultKeys.length === 0 ? { targetId: "keybindings" } : {}),
121 };
122});
123
124/**
125 * Searchable settings and stable destinations, in result order. Rows with a
126 * dedicated anchor render their id and title via `searchableSetting`; items
127 * that may not be mounted point at their nearest stable section instead.
128 */
129export const SETTINGS_SEARCH_ITEMS = [
130 {
131 id: "storage-worktrees",
132 title: "Worktree cleanup",
133 to: "/settings/storage",
134 scope: "project-defaults",
135 searchTerms: [
136 "disk storage delete deleted archived threads old inactive merged unchanged worktrees retention days project inherit off custom",
137 ],
138 },
139 {
140 id: "storage-artifacts",
141 title: "Artifacts and logs",
142 to: "/settings/storage",
143 scope: "environment-defaults",
144 searchTerms: ["disk storage browser screenshots captures rotated logs cleanup retention"],
145 },
146 {
147 id: "project-defaults",
148 title: "Project defaults and overrides",
149 to: "/settings/general",
150 scope: "project-defaults",
151 searchTerms: ["model workspace environments projects inheritance checkout"],
152 },
153 {
154 id: "project-overview",
155 title: "Project overview",
156 to: "/settings/projects",
157 searchTerms: ["name icon emoji image checkout remove delete"],
158 },
159 {
160 id: "default-model",
161 title: "Default model",
162 to: "/settings/general",
163 scope: "project-defaults",
164 searchTerms: ["new thread project provider reasoning effort"],
165 },
166 {
167 id: "default-permissions",
168 title: "Permissions",
169 to: "/settings/general",
170 scope: "project-defaults",
171 searchTerms: [
172 "new thread default runtime mode supervised approvals auto accept edits full access",
173 ],
174 },
175 {
176 id: "color-scheme",
177 title: "Color scheme",
178 to: "/settings/appearance",
179 searchTerms: ["appearance light dark system mode"],
180 // The scheme tiles sit at the top of the Appearance section.
181 targetId: "appearance",
182 },
183 {
184 id: "theme",
185 title: "Themes",
186 to: "/settings/appearance",
187 searchTerms: ["appearance colors palette custom import"],
188 // Theme cards live directly under the scheme tiles; the section is the
189 // stable scroll destination for both.
190 targetId: "appearance",
191 },
192 {
193 // Prefixed because the slider control already owns the `appearance-contrast` id.
194 id: "setting-appearance-contrast",
195 title: "Contrast",
196 to: "/settings/appearance",
197 searchTerms: ["colors borders interface"],
198 },
199 {
200 // Prefixed because the slider control already owns the `glass-opacity` id.
201 id: "setting-glass-opacity",
202 title: "Glass opacity",
203 to: "/settings/appearance",
204 searchTerms: ["transparent transparency solid menus dialogs composer"],
205 },
206 {
207 id: "diff-color-scheme",
208 title: "Diff colors",
209 to: "/settings/appearance",
210 searchTerms: ["red green blue orange additions deletions changes counts palette colorblind"],
211 },
212 {
213 id: "panel-animations",
214 title: "Panel animations",
215 to: "/settings/appearance",
216 },
217 {
218 id: "environment-identification",
219 title: "Environment identification",
220 to: "/settings/appearance",
221 searchTerms: ["dev nightly artwork pill label hide none"],
222 // The setting is stage-dependent, so its parent section is the stable destination.
223 targetId: "appearance-interface",
224 },
225 {
226 id: "interface-font",
227 title: "Interface font",
228 to: "/settings/appearance",
229 searchTerms: ["typography family size system sans"],
230 },
231 {
232 id: "prompt-font",
233 title: "Prompt font",
234 to: "/settings/appearance",
235 searchTerms: ["typography family size composer input"],
236 },
237 {
238 id: "code-font",
239 title: "Code font",
240 to: "/settings/appearance",
241 searchTerms: ["typography family size monospace code blocks diffs file previews"],
242 },
243 {
244 id: "terminal-font",
245 title: "Terminal font",
246 to: "/settings/appearance",
247 searchTerms: ["typography family size monospace output"],
248 },
249 {
250 id: "font-smoothing",
251 title: "Font smoothing",
252 to: "/settings/appearance",
253 searchTerms: ["typography text grayscale anti aliasing macos thin"],
254 macOnly: true,
255 },
256 {
257 id: "word-wrap",
258 title: "Word wrap",
259 to: "/settings/appearance",
260 searchTerms: ["long lines code blocks tables diffs file previews"],
261 },
262 {
263 id: "project-grouping",
264 title: "Project grouping",
265 to: "/settings/general",
266 searchTerms: ["combine matching repositories environments sidebar"],
267 },
268 {
269 id: "auto-settle-inactive-threads",
270 title: "Auto-settle inactive threads",
271 to: "/settings/general",
272 searchTerms: ["sidebar inactivity days no activity automatically"],
273 requiresThreadAutoSettlement: true,
274 scope: "project-defaults",
275 },
276 {
277 id: "auto-settle-merged-threads",
278 title: "Auto-settle merged threads",
279 to: "/settings/general",
280 searchTerms: ["pull request merge closed automatically sidebar"],
281 requiresThreadAutoSettlement: true,
282 scope: "project-defaults",
283 },
284 {
285 id: "days-before-auto-settle",
286 title: "Days of inactivity before auto-settle",
287 to: "/settings/general",
288 targetId: "auto-settle-inactive-threads",
289 searchTerms: ["thread timeout activity sidebar"],
290 requiresThreadAutoSettlement: true,
291 scope: "project-defaults",
292 },
293 {
294 id: "thread-notifications",
295 title: "Thread notifications",
296 to: "/settings/general",
297 searchTerms: ["notification sound alert completion input approval desktop"],
298 },
299 {
300 id: "in-app-notifications",
301 title: "In-app notifications",
302 to: "/settings/general",
303 searchTerms: ["notification toast popup completion input approval failure"],
304 },
305 {
306 id: "time-format",
307 title: "Time format",
308 to: "/settings/general",
309 searchTerms: ["timestamp clock locale system browser os 12 hour 24 hour"],
310 },
311 {
312 id: "response-streaming",
313 title: "Response streaming",
314 to: "/settings/general",
315 scope: "project-defaults",
316 searchTerms: ["output token paragraph buffered wait turn legacy"],
317 },
318 {
319 id: "hide-whitespace-changes",
320 title: "Hide whitespace changes",
321 to: "/settings/general",
322 searchTerms: ["diff ignore spaces edits default"],
323 },
324 {
325 id: "default-diff-file-state",
326 title: "Default diff file state",
327 to: "/settings/general",
328 searchTerms: ["collapsed expanded collapse expand files pull request pr code tab"],
329 },
330 {
331 id: "diff-layout",
332 title: "Diff layout",
333 to: "/settings/general",
334 searchTerms: ["stacked split side by side unified inline view"],
335 },
336 {
337 id: "proactive-panels",
338 title: "Proactive panels",
339 to: "/settings/general",
340 searchTerms: ["automatically open diff pull request pr right panel agent completion"],
341 },
342 {
343 id: "skills-in-slash-menu",
344 title: "Show skills in slash menu",
345 to: "/settings/general",
346 searchTerms: ["command menu dollar $ slash /"],
347 },
348 {
349 id: "composer-rich-text",
350 title: "Rich text composer",
351 to: "/settings/general",
352 searchTerms: ["composer rich text tiptap bold italic markdown styled wysiwyg"],
353 },
354 {
355 id: "composer-collapse",
356 title: "Collapse composer on scroll",
357 to: "/settings/general",
358 searchTerms: ["composer rest resting scroll wheel conversation timeline shrink minimize"],
359 },
360 {
361 id: "send-shortcut",
362 title: "Send shortcut",
363 to: "/settings/general",
364 searchTerms: ["enter return command ctrl multiline prompt new line composer"],
365 },
366 {
367 id: "follow-up-behavior",
368 title: "Follow-up behavior",
369 to: "/settings/general",
370 searchTerms: ["queue steer running turn send default behavior composer"],
371 },
372 {
373 id: "provider-update-checks",
374 title: "Provider update checks",
375 to: "/settings/general",
376 searchTerms: ["installed cli versions newer available codex claude cursor grok opencode"],
377 scope: "environment-defaults",
378 },
379 {
380 id: "continue-threads-after-server-update",
381 title: "Continue threads after restarts",
382 to: "/settings/general",
383 scope: "project-defaults",
384 searchTerms: [
385 "resume running active interrupted work restart reboot machine crash desktop update automatically",
386 ],
387 },
388 {
389 id: "background-activity",
390 title: "Background activity",
391 to: "/settings/general",
392 scope: "environment-defaults",
393 searchTerms: [
394 "balanced performance battery saver advanced git fetch provider health refresh host power monitor idle policy",
395 ],
396 },
397 {
398 id: "new-threads",
399 title: "New threads",
400 to: "/settings/general",
401 scope: "project-defaults",
402 searchTerms: ["default workspace mode draft local worktree"],
403 },
404 {
405 id: "worktree-submodules",
406 title: "Submodules",
407 to: "/settings/general",
408 scope: "project-defaults",
409 searchTerms: ["git submodule init recursive top-level none worktree t3.json"],
410 },
411 {
412 id: "start-from-origin",
413 title: "Start from origin",
414 to: "/settings/general",
415 scope: "project-defaults",
416 searchTerms: ["new worktrees latest matching remote branch local"],
417 },
418 {
419 id: "add-project-starts-in",
420 title: "Add project starts in",
421 to: "/settings/general",
422 scope: "environment-defaults",
423 searchTerms: ["base directory folder browser path home"],
424 },
425 {
426 id: "unpin-confirmation",
427 title: "Unpin confirmation",
428 to: "/settings/general",
429 searchTerms: ["ask before thread pinned section"],
430 },
431 {
432 id: "archive-confirmation",
433 title: "Archive confirmation",
434 to: "/settings/general",
435 searchTerms: ["ask before thread second click inline action"],
436 },
437 {
438 id: "delete-confirmation",
439 title: "Delete confirmation",
440 to: "/settings/general",
441 searchTerms: ["ask before thread chat history"],
442 },
443 {
444 id: "quit-confirmation",
445 title: "Quit shortcut",
446 to: "/settings/general",
447 searchTerms: ["confirmation desktop app exit direct hold double click press twice"],
448 desktopOnly: true,
449 },
450 {
451 id: "text-generation-model",
452 title: "Text generation model",
453 to: "/settings/general",
454 scope: "project-defaults",
455 searchTerms: ["generated thread titles source control content default provider"],
456 },
457 {
458 id: "diagnostics",
459 title: "Diagnostics",
460 to: "/settings/general",
461 searchTerms: ["logs traces processes resource history failures spans cpu memory"],
462 },
463 {
464 id: "open-source-licenses",
465 title: "Open source licenses",
466 to: "/settings/general",
467 },
468 {
469 id: "legacy-plan-mode",
470 title: "Plan mode (legacy)",
471 to: "/settings/general",
472 searchTerms: ["build plan composer old"],
473 },
474 {
475 id: "legacy-context-window-indicator",
476 title: "Context window indicator (legacy)",
477 to: "/settings/general",
478 searchTerms: ["composer meter usage tokens circle old"],
479 },
480 {
481 id: "legacy-sidebar",
482 title: "Sidebar (legacy)",
483 to: "/settings/general",
484 searchTerms: ["project thread tree old flat list"],
485 },
486 {
487 id: "keybindings",
488 title: "Keybindings",
489 to: "/settings/keybindings",
490 searchTerms: ["keyboard shortcuts hotkeys commands bindings json"],
491 },
492 ...KEYBINDING_SEARCH_ITEMS,
493 {
494 id: "snap-shot-enabled",
495 title: "SnapShots",
496 searchTerms: ["window capture screenshot"],
497 to: "/settings/snap-shot",
498 },
499 {
500 id: "snap-shot-accessibility",
501 title: "Include app text",
502 to: "/settings/snap-shot",
503 targetId: "snap-shot-enabled",
504 searchTerms: [
505 "capture accessibility data text UI structure elements privacy omit agent context",
506 ],
507 },
508 {
509 id: "snap-shot-shortcut",
510 title: "Capture shortcut",
511 to: "/settings/snap-shot",
512 targetId: "snap-shot-enabled",
513 },
514 {
515 id: "snap-shot-sound",
516 title: "Capture sound",
517 to: "/settings/snap-shot",
518 targetId: "snap-shot-enabled",
519 },
520 {
521 id: "snap-shot-flash",
522 title: "Capture flash",
523 to: "/settings/snap-shot",
524 targetId: "snap-shot-enabled",
525 },
526 {
527 id: "snap-shot-animations",
528 title: "Capture animations",
529 to: "/settings/snap-shot",
530 targetId: "snap-shot-enabled",
531 },
532 {
533 id: "providers",
534 title: "Providers",
535 to: "/settings/providers",
536 searchTerms: [
537 "agents cli codex claude cursor grok opencode antigravity google sign in sign out install subscription instances authentication api key models configuration binary path config directory endpoint arguments environment variables display name accent color custom favorite hidden auto compact",
538 ],
539 },
540 {
541 id: "usage-providers",
542 title: "Usage providers",
543 to: "/settings/providers",
544 searchTerms: [
545 "usage sources CLIProxyAPI CLI proxy hub quota subscription limits management key add remove",
546 ],
547 providerSettingsOnly: true,
548 },
549 {
550 id: "provider-health-check-interval",
551 title: "Health check interval",
552 to: "/settings/providers",
553 searchTerms: ["refresh availability versions auth state models background probes seconds off"],
554 providerSettingsOnly: true,
555 },
556 {
557 id: "agent-browser-access",
558 title: "Agent browser access",
559 to: "/settings/integrations",
560 scope: "project-defaults",
561 searchTerms: ["allow disable enable open drive preview tools sessions project override"],
562 },
563 {
564 id: "device-hosts",
565 title: "Device hosts",
566 to: "/settings/integrations",
567 searchTerms: ["ssh remote simulator emulator ios android mac mini identity key connection"],
568 },
569 {
570 id: "agent-device-access",
571 title: "Agent device access",
572 to: "/settings/integrations",
573 targetId: "devices",
574 searchTerms: ["allow simulator emulator ios android drive tools sessions"],
575 },
576 {
577 id: "device-hub",
578 title: "Device hub",
579 to: "/settings/integrations",
580 targetId: "devices",
581 searchTerms: ["simulator emulator ios android install start"],
582 },
583 {
584 id: "device-platform-support",
585 title: "Simulator support",
586 to: "/settings/integrations",
587 targetId: "devices",
588 searchTerms: ["xcode android studio sdk avd runtime"],
589 },
590 {
591 id: "browser-profiles",
592 title: "Browser profiles",
593 to: "/settings/integrations",
594 targetId: "browser",
595 },
596 {
597 id: "browser-default-profile",
598 title: "Default browser profile",
599 to: "/settings/integrations",
600 targetId: "browser-profiles",
601 },
602 {
603 id: "browser-default-viewport",
604 title: "Default browser viewport",
605 to: "/settings/integrations",
606 searchTerms: ["preview size width height device desktop mobile rotate"],
607 },
608 {
609 id: "browser-default-zoom",
610 title: "Default browser zoom",
611 to: "/settings/integrations",
612 searchTerms: ["preview page scale tabs percent"],
613 },
614 {
615 id: "browser-default-appearance",
616 title: "Default browser appearance",
617 to: "/settings/integrations",
618 searchTerms: ["preview color scheme light dark system os"],
619 },
620 {
621 id: "browser-recording-frame-rate",
622 title: "Browser recording frame rate",
623 to: "/settings/integrations",
624 },
625 {
626 id: "browser-recording-key-presses",
627 title: "Show key presses in recordings",
628 to: "/settings/integrations",
629 searchTerms: ["browser preview keyboard shortcuts keystrokes overlay capture"],
630 },
631 {
632 id: "browser-recording-mouse-presses",
633 title: "Show mouse presses in recordings",
634 to: "/settings/integrations",
635 searchTerms: ["browser preview clicks buttons drag overlay capture"],
636 },
637 {
638 id: "browser-link-target",
639 title: "Open links in",
640 to: "/settings/integrations",
641 searchTerms: ["links default browser in-app browser external open"],
642 },
643 {
644 id: "browser-auto-show-floating-preview",
645 title: "Auto-show floating preview",
646 to: "/settings/integrations",
647 searchTerms: ["agent opens browser device simulator pop into view hide"],
648 },
649 {
650 id: "automatic-pull",
651 title: "Automatically pull",
652 to: "/settings/source-control",
653 scope: "project-defaults",
654 searchTerms: ["auto pull default branch current checkout fast forward upstream"],
655 },
656 {
657 id: "pull-request-merge-method",
658 title: "Default merge method",
659 to: "/settings/source-control",
660 scope: "project-defaults",
661 searchTerms: ["pull request merge squash rebase last selected"],
662 },
663 {
664 id: "source-control",
665 title: "Source control",
666 to: "/settings/source-control",
667 scope: "environment-defaults",
668 searchTerms: [
669 "version control git github gitlab forgejo gitea tea codeberg bitbucket azure devops hosting integrations credentials scan server environment",
670 ],
671 },
672 {
673 id: "git-fetch-interval",
674 title: "Git fetch interval",
675 to: "/settings/source-control",
676 searchTerms: [
677 "automatic remote branch refresh background credentials security keys seconds off",
678 ],
679 environmentOnly: true,
680 scope: "environment-defaults",
681 },
682 {
683 id: "source-control-writing-style",
684 title: "Source control writing style",
685 to: "/settings/source-control",
686 searchTerms: [
687 "repository conventions conventional commits custom instructions change descriptions request titles",
688 ],
689 environmentOnly: true,
690 },
691 {
692 id: "follow-change-request-templates",
693 title: "Follow change request templates",
694 to: "/settings/source-control",
695 searchTerms: ["repository pr pull request description structure"],
696 environmentOnly: true,
697 },
698 {
699 id: "source-control-writer-model",
700 title: "Source control writer model",
701 to: "/settings/source-control",
702 searchTerms: [
703 "override generated commit change request pr titles descriptions branch bookmark",
704 ],
705 environmentOnly: true,
706 scope: "project-defaults",
707 },
708 {
709 id: "project-actions",
710 title: "Actions",
711 to: "/settings/projects",
712 searchTerms: ["commands scripts setup run dev server checkout worktree t3.json import"],
713 },
714 {
715 id: "environment-icon",
716 title: "Environment icon",
717 to: "/settings/connections",
718 targetId: "connections-environment",
719 searchTerms: ["machine glyph sidebar mac mini studio laptop desktop server cloud vm"],
720 localBackendManagementOnly: true,
721 },
722 {
723 id: "local-environment",
724 title: "Local environment",
725 to: "/settings/connections",
726 targetId: "connections-environment",
727 searchTerms: ["turn off on disable enable local server agents remote only restart"],
728 desktopOnly: true,
729 },
730 {
731 id: "network-access",
732 title: "Network access",
733 to: "/settings/connections",
734 targetId: "connections-environment",
735 searchTerms: ["expose backend remote pairing local machine interfaces host restart"],
736 localBackendManagementOnly: true,
737 },
738 {
739 id: "tailscale-https",
740 title: "Tailscale HTTPS",
741 to: "/settings/connections",
742 targetId: "connections-environment",
743 searchTerms: ["serve magicdns endpoint remote secure network"],
744 desktopOnly: true,
745 localBackendManagementOnly: true,
746 },
747 {
748 id: "wsl-backend",
749 title: "WSL backend",
750 to: "/settings/connections",
751 searchTerms: [
752 "windows subsystem linux distro second server projects stop windows backend restart",
753 ],
754 desktopOnly: true,
755 windowsOnly: true,
756 localBackendManagementOnly: true,
757 wslAvailableOnly: true,
758 },
759 {
760 id: "t3-connect",
761 localEnvironmentOnly: true,
762 title: "T3 Connect",
763 to: "/settings/connections",
764 targetId: "connections-environment",
765 searchTerms: ["managed tunnel cloud other devices remote"],
766 desktopOnly: true,
767 cloudOnly: true,
768 },
769 {
770 id: "publish-agent-activity",
771 localEnvironmentOnly: true,
772 title: "Publish agent activity",
773 to: "/settings/connections",
774 targetId: "connections-environment",
775 searchTerms: ["mobile push notifications live activities cloud tunnel"],
776 cloudOnly: true,
777 },
778 {
779 id: "connections-environment",
780 title: "This machine",
781 to: "/settings/connections",
782 searchTerms: [
783 "connections server backend local remote access administrative permissions scope pairing links qr code authorized clients sessions revoke endpoint",
784 ],
785 },
786 {
787 id: "remote-environments",
788 title: "Environments",
789 to: "/settings/connections",
790 searchTerms: ["add pair backend host code ssh config agent tunnel saved t3 connect"],
791 },
792 {
793 id: "load-balancing",
794 title: "Load balancing",
795 to: "/settings/connections",
796 searchTerms: [
797 "automatic machine environment resources cpu memory capacity preference weight shared projects",
798 ],
799 },
800 {
801 id: "github-routing",
802 title: "GitHub sharing",
803 to: "/settings/connections",
804 searchTerms: ["pull request trusted environments shared credentials permissions read actions"],
805 },
806 {
807 id: "archive",
808 title: "Archived threads",
809 to: "/settings/archived",
810 searchTerms: ["restore reopen deleted history projects"],
811 },
812] as const satisfies ReadonlyArray<SettingsSearchItem>;
813
814export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
815
816const SEARCH_ITEMS_BY_ID = new Map(SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item] as const));
817
818const SETTINGS_CATEGORY_SCOPES: Readonly<Record<SettingsPath, SettingsSearchScope | null>> = {
819 "/settings/projects": "project",
820 "/settings/general": null,
821 "/settings/appearance": null,
822 "/settings/snap-shot": null,
823 // Keybindings fan out to the selection; Providers shows the representative
824 // environment at any selection. Neither needs a particular scope to render.
825 "/settings/keybindings": null,
826 "/settings/providers": null,
827 "/settings/integrations": null,
828 "/settings/source-control": "environment-defaults",
829 "/settings/storage": "project-defaults",
830 "/settings/connections": "connections",
831 "/settings/archived": "project-defaults",
832};
833
834/** Search keeps the selected target. A missing row can explain its owning scope instead. */
835export function getSettingsSearchTargetScope(targetId: string) {
836 const items: readonly SettingsSearchItem[] = SETTINGS_SEARCH_ITEMS;
837 const item =
838 items.find((candidate) => candidate.id === targetId) ??
839 items.find((candidate) => candidate.targetId === targetId);
840 return item
841 ? {
842 title: item.title,
843 scope: item.scope ?? SETTINGS_CATEGORY_SCOPES[item.to],
844 ...(item.requiresThreadAutoSettlement ? { requiresThreadAutoSettlement: true } : {}),
845 }
846 : null;
847}
848
849interface AutoSettlementSearchEnvironment {
850 readonly environmentId: EnvironmentId;
851 readonly connection: { readonly phase: EnvironmentConnectionPhase };
852 readonly serverConfig: {
853 readonly environment: {
854 readonly capabilities: { readonly threadAutoSettlement?: boolean };
855 };
856 } | null;
857}
858
859/** Discovery needs one capable environment; the selected page needs every connected target to support it. */
860export function getThreadAutoSettlementSearchAvailability(
861 environments: readonly AutoSettlementSearchEnvironment[],
862 scope?: Pick<ResolvedSettingsScope, "kind" | "environmentIds">,
863) {
864 const connected = environments.filter(
865 (environment) =>
866 environment.connection.phase === "connected" && environment.serverConfig !== null,
867 );
868 const eligibleEnvironmentIds = connected
869 .filter(
870 (environment) =>
871 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
872 )
873 .map((environment) => environment.environmentId);
874 const selected = connected.filter((environment) =>
875 scope?.environmentIds.includes(environment.environmentId),
876 );
877 return {
878 eligibleEnvironmentIds,
879 isTargetAvailable:
880 scope !== undefined &&
881 scope.kind !== "unavailable" &&
882 selected.length > 0 &&
883 selected.every((environment) => eligibleEnvironmentIds.includes(environment.environmentId)),
884 };
885}
886
887export function isSettingsSearchScopeAvailable(
888 requiredScope: SettingsSearchScope | null,
889 scopeKind: ResolvedSettingsScope["kind"],
890): boolean {
891 switch (requiredScope) {
892 case null:
893 case "connections":
894 return true;
895 case "environment":
896 case "checkout":
897 return requiredScope === scopeKind;
898 case "project":
899 return scopeKind === "project" || scopeKind === "checkout";
900 case "environment-defaults":
901 return scopeKind === "environment" || scopeKind === "all";
902 case "project-defaults":
903 return (
904 scopeKind === "environment" ||
905 scopeKind === "all" ||
906 scopeKind === "project" ||
907 scopeKind === "checkout"
908 );
909 }
910}
911
912function settingsScopeKindFromSearch(search: SettingsScopeSearch): ResolvedSettingsScope["kind"] {
913 const target = validateSettingsScopeSearch({ ...search });
914 if (target.checkout && !target.project) return "unavailable";
915 if (target.project) return target.checkout ? "checkout" : "project";
916 return target.machine ? "environment" : "all";
917}
918
919export function isSettingsOverviewVisible(search: SettingsScopeSearch): boolean {
920 const kind = settingsScopeKindFromSearch(search);
921 return kind === "project" || kind === "checkout";
922}
923
924/**
925 * `id` and `title` props for the element a search item anchors to. Panels
926 * spread (or pick from) this instead of restating the strings, so the catalog
927 * and the rendered settings cannot drift apart.
928 */
929export function searchableSetting(id: SettingsSearchItemId): {
930 readonly id: string;
931 readonly title: string;
932} {
933 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID.get(id)!;
934 return { id: anchorId, title };
935}
936
937export function filterAvailableSettingsSearchItems(
938 availability: SettingsSearchAvailability,
939): ReadonlyArray<SettingsSearchItem> {
940 const items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS;
941 return items.filter(
942 (item) =>
943 (!item.cloudOnly || availability.hasCloudPublicConfig) &&
944 (!item.environmentOnly || availability.hasEnvironment) &&
945 (!item.providerSettingsOnly || availability.hasProviderSettingsEnvironment) &&
946 (!item.localBackendManagementOnly || availability.canManageLocalBackend) &&
947 (!item.localEnvironmentOnly || !availability.localEnvironmentDisabled) &&
948 (!item.wslAvailableOnly || availability.isWslSettingsRowVisible) &&
949 (!item.requiresThreadAutoSettlement || availability.hasThreadAutoSettlement),
950 );
951}
952
953export function searchSettings(
954 query: string,
955 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
956): ReadonlyArray<SettingsSearchItem> {
957 const normalizedQuery = normalizeSearchText(query);
958 if (normalizedQuery.length === 0) return [];
959 const queryTokens = normalizedQuery.split(" ");
960 const platform = typeof navigator === "undefined" ? "" : navigator.platform;
961
962 return items
963 .flatMap((item, index) => {
964 if (!isElectron && item.desktopOnly === true) return [];
965 if (item.macOnly && !isMacPlatform(platform)) return [];
966 if (item.windowsOnly && !isWindowsPlatform(platform)) return [];
967
968 const title = normalizeSearchText(item.title);
969 const fields = [
970 title,
971 normalizeSearchText(SETTINGS_SECTION_LABELS[item.to]),
972 ...(item.searchTerms ?? []).map(normalizeSearchText),
973 ];
974 if (!queryTokens.every((token) => fields.some((field) => field.includes(token)))) return [];
975
976 const exactPhraseField = fields.findIndex((field) => field.includes(normalizedQuery));
977 const rank =
978 title === normalizedQuery
979 ? 5
980 : title.startsWith(normalizedQuery)
981 ? 4
982 : title.includes(normalizedQuery)
983 ? 3
984 : queryTokens.every((token) => title.includes(token))
985 ? 2
986 : exactPhraseField >= 0
987 ? 1
988 : 0;
989 return [{ item, index, rank }];
990 })
991 .toSorted(
992 (left, right) =>
993 Number(left.item.secondary ?? false) - Number(right.item.secondary ?? false) ||
994 right.rank - left.rank ||
995 left.index - right.index,
996 )
997 .map(({ item }) => item);
998}