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