Open History to see the last commit
1import type {
2 ProviderDriverKind,
3 ProviderInstanceConfig,
4 ProviderInstanceId,
5 ServerSettings,
6 SidebarProjectGroupingMode,
7 UnifiedSettings,
8} from "@t3tools/contracts";
9import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
10
11export function isProjectGroupingEnabled(mode: SidebarProjectGroupingMode): boolean {
12 return mode !== "separate";
13}
14
15export function projectGroupingModeFromToggle(
16 enabled: boolean,
17 lastEnabledMode: SidebarProjectGroupingMode = "repository",
18): SidebarProjectGroupingMode {
19 if (!enabled) return "separate";
20 return lastEnabledMode === "repository_path" ? "repository_path" : "repository";
21}
22
23const LAST_ENABLED_PROJECT_GROUPING_MODE_KEY = "t3code:last-enabled-project-grouping-mode";
24
25export function readLastEnabledProjectGroupingMode(): SidebarProjectGroupingMode {
26 try {
27 return localStorage.getItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY) === "repository_path"
28 ? "repository_path"
29 : "repository";
30 } catch {
31 return "repository";
32 }
33}
34
35export function rememberEnabledProjectGroupingMode(mode: SidebarProjectGroupingMode): void {
36 if (mode === "separate") return;
37 try {
38 localStorage.setItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY, mode);
39 } catch {
40 // Storage can be unavailable in restricted browser contexts.
41 }
42}
43
44function collapseOtelSignalsUrl(input: {
45 readonly tracesUrl: string;
46 readonly metricsUrl: string;
47}): string | null {
48 const tracesSuffix = "/traces";
49 const metricsSuffix = "/metrics";
50 if (!input.tracesUrl.endsWith(tracesSuffix) || !input.metricsUrl.endsWith(metricsSuffix)) {
51 return null;
52 }
53
54 const tracesBase = input.tracesUrl.slice(0, -tracesSuffix.length);
55 const metricsBase = input.metricsUrl.slice(0, -metricsSuffix.length);
56 if (tracesBase !== metricsBase) {
57 return null;
58 }
59
60 return `${tracesBase}/{traces,metrics}`;
61}
62
63export function formatDiagnosticsDescription(input: {
64 readonly localTracingEnabled: boolean;
65 readonly otlpTracesEnabled: boolean;
66 readonly otlpTracesUrl?: string | undefined;
67 readonly otlpMetricsEnabled: boolean;
68 readonly otlpMetricsUrl?: string | undefined;
69}): string {
70 const mode = input.localTracingEnabled ? "Local trace file" : "Terminal logs only";
71 const tracesUrl = input.otlpTracesEnabled ? input.otlpTracesUrl : undefined;
72 const metricsUrl = input.otlpMetricsEnabled ? input.otlpMetricsUrl : undefined;
73
74 if (tracesUrl && metricsUrl) {
75 const collapsedUrl = collapseOtelSignalsUrl({ tracesUrl, metricsUrl });
76 return collapsedUrl
77 ? `${mode}. Exporting OTEL to ${collapsedUrl}.`
78 : `${mode}. Exporting OTEL traces to ${tracesUrl} and metrics to ${metricsUrl}.`;
79 }
80
81 if (tracesUrl) {
82 return `${mode}. Exporting OTEL traces to ${tracesUrl}.`;
83 }
84
85 if (metricsUrl) {
86 return `${mode}. Exporting OTEL metrics to ${metricsUrl}.`;
87 }
88
89 return `${mode}.`;
90}
91
92export function buildProviderInstanceUpdatePatch(input: {
93 readonly settings: Pick<ServerSettings, "providers" | "providerInstances">;
94 readonly instanceId: ProviderInstanceId;
95 readonly instance: ProviderInstanceConfig;
96 readonly driver: ProviderDriverKind;
97 readonly isDefault: boolean;
98 readonly textGenerationModelSelection?:
99 | ServerSettings["textGenerationModelSelection"]
100 | undefined;
101}): Partial<UnifiedSettings> {
102 type LegacyProviderSettings = ServerSettings["providers"][keyof ServerSettings["providers"]];
103 const legacyProviderDefaults = DEFAULT_UNIFIED_SETTINGS.providers as Record<
104 string,
105 LegacyProviderSettings | undefined
106 >;
107 const legacyProviderDefault = input.isDefault ? legacyProviderDefaults[input.driver] : undefined;
108 return {
109 ...(legacyProviderDefault !== undefined
110 ? {
111 providers: {
112 ...input.settings.providers,
113 [input.driver]: legacyProviderDefault,
114 } as ServerSettings["providers"],
115 }
116 : {}),
117 providerInstances: {
118 ...input.settings.providerInstances,
119 [input.instanceId]: input.instance,
120 },
121 ...(input.textGenerationModelSelection !== undefined
122 ? { textGenerationModelSelection: input.textGenerationModelSelection }
123 : {}),
124 };
125}