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

serverSettings.ts

228 lines8.4 KB

1import {
2 isProviderDriverKind,
3 isProviderAvailable,
4 type ModelSelection,
5 type ProviderDriverKind,
6 type ServerProvider,
7 ServerSettings,
8 type ServerSettingsPatch,
9} from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import { deepMerge } from "./Struct.ts";
13import { fromLenientJson } from "./schemaJson.ts";
14import { createModelSelection } from "./model.ts";
15import {
16 getBackgroundActivityBaseProfile,
17 normalizeBackgroundActivitySettings,
18 normalizeServerBackgroundActivitySettings,
19 resolveBackgroundActivitySettings,
20} from "./backgroundActivitySettings.ts";
21​
22const ServerSettingsJson = fromLenientJson(ServerSettings);
23const decodeServerSettingsJson = Schema.decodeUnknownOption(ServerSettingsJson);
24​
25type LegacyProviderSettings = ServerSettings["providers"][keyof ServerSettings["providers"]];
26​
27const getLegacyProviderSettings = (
28 settings: ServerSettings,
29 provider: ProviderDriverKind,
30): LegacyProviderSettings | undefined =>
31 (settings.providers as Record<string, LegacyProviderSettings | undefined>)[provider];
32​
33export function isModelSelectionProviderEnabled(
34 settings: ServerSettings,
35 selection: ModelSelection,
36): boolean {
37 const instanceConfig = settings.providerInstances[selection.instanceId];
38 if (instanceConfig !== undefined) {
39 return instanceConfig.enabled ?? true;
40 }
41​
42 return (
43 isProviderDriverKind(selection.instanceId) &&
44 getLegacyProviderSettings(settings, selection.instanceId)?.enabled === true
45 );
47​
48export function resolveSourceControlWriterModelSelection(
49 settings: ServerSettings,
50 providers?: ReadonlyArray<ServerProvider>,
51): ModelSelection {
52 const selection = settings.sourceControlWriterModelSelection;
53 if (!selection || !isModelSelectionProviderEnabled(settings, selection)) {
54 return settings.textGenerationModelSelection;
55 }
56 if (providers === undefined) {
57 return selection;
58 }
59​
60 const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
61 return provider?.enabled === true && isProviderAvailable(provider)
62 ? selection
63 : settings.textGenerationModelSelection;
65​
66export interface PersistedServerObservabilitySettings {
67 readonly otlpTracesUrl: string | undefined;
68 readonly otlpMetricsUrl: string | undefined;
70​
71export function normalizePersistedServerSettingString(
72 value: string | null | undefined,
73): string | undefined {
74 const trimmed = value?.trim();
75 return trimmed && trimmed.length > 0 ? trimmed : undefined;
77​
78export function extractPersistedServerObservabilitySettings(input: {
79 readonly observability?: {
80 readonly otlpTracesUrl?: string;
81 readonly otlpMetricsUrl?: string;
82 };
83}): PersistedServerObservabilitySettings {
84 return {
85 otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl),
86 otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl),
87 };
89​
90export function parsePersistedServerObservabilitySettings(
91 raw: string,
92): PersistedServerObservabilitySettings {
93 const decoded = decodeServerSettingsJson(raw);
94 if (Option.isSome(decoded)) {
95 return extractPersistedServerObservabilitySettings(decoded.value);
96 }
97 return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
99​
100function shouldReplaceTextGenerationModelSelection(
101 patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
102): boolean {
103 return Boolean(patch && (patch.instanceId !== undefined || patch.model !== undefined));
105​
106function mergeModelSelectionOptionsById(input: {
107 current: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
108 patch: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
109}): Array<{ id: string; value: string | boolean }> | undefined {
110 if (input.patch === undefined) {
111 return input.current ? [...input.current] : undefined;
112 }
113 if (input.patch.length === 0) {
114 return undefined;
115 }
116​
117 const merged = new Map((input.current ?? []).map((selection) => [selection.id, selection.value]));
118 for (const selection of input.patch) {
119 merged.set(selection.id, selection.value);
120 }
121 return [...merged.entries()].map(([id, value]) => ({ id, value }));
123​
124export function applyServerSettingsPatch(
125 current: ServerSettings,
126 patch: ServerSettingsPatch,
127): ServerSettings {
128 const selectionPatch = patch.textGenerationModelSelection;
129 const {
130 automaticGitFetchInterval,
131 providerHealthRefreshInterval,
132 backgroundActivityProfile,
133 backgroundActivity,
134 githubAccountRouting,
135 ...patchForMerge
136 } = patch;
137 const currentBackgroundActivity = normalizeServerBackgroundActivitySettings(current);
138 const backgroundActivityPatch =
139 backgroundActivityProfile !== undefined
140 ? {
141 schemaVersion: 1 as const,
142 profile:
143 automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
144 ? ("custom" as const)
145 : backgroundActivityProfile,
146 ...(automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
147 ? { baseProfile: backgroundActivityProfile }
148 : {}),
149 overrides: {
150 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
151 ...(providerHealthRefreshInterval !== undefined
152 ? { providerHealthRefreshInterval }
153 : {}),
154 },
155 }
156 : automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
157 ? {
158 schemaVersion: 1 as const,
159 profile: "custom" as const,
160 baseProfile: getBackgroundActivityBaseProfile(currentBackgroundActivity),
161 overrides: {
162 ...(currentBackgroundActivity.profile === "custom"
163 ? currentBackgroundActivity.overrides
164 : {}),
165 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
166 ...(providerHealthRefreshInterval !== undefined
167 ? { providerHealthRefreshInterval }
168 : {}),
169 },
170 }
171 : undefined;
172 const next = deepMerge(current, patchForMerge);
173 const nextWithReplacementsBase = {
174 ...next,
175 ...(backgroundActivity !== undefined
176 ? {
177 backgroundActivity: {
178 ...deepMerge(currentBackgroundActivity, backgroundActivity),
179 ...(backgroundActivity.overrides !== undefined
180 ? { overrides: backgroundActivity.overrides }
181 : {}),
182 },
183 }
184 : { backgroundActivity: currentBackgroundActivity }),
185 ...(backgroundActivity === undefined && backgroundActivityPatch !== undefined
186 ? { backgroundActivity: backgroundActivityPatch }
187 : {}),
188 ...(patch.providerInstances !== undefined
189 ? { providerInstances: patch.providerInstances }
190 : {}),
191 ...(patch.sourceControlWriterModelSelection !== undefined
192 ? { sourceControlWriterModelSelection: patch.sourceControlWriterModelSelection }
193 : {}),
194 ...(githubAccountRouting !== undefined ? { githubAccountRouting } : {}),
195 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
196 ...(providerHealthRefreshInterval !== undefined ? { providerHealthRefreshInterval } : {}),
197 };
198 const normalizedBackgroundActivity = normalizeBackgroundActivitySettings(
199 nextWithReplacementsBase.backgroundActivity,
200 );
201 const resolvedBackgroundActivity = resolveBackgroundActivitySettings(
202 normalizedBackgroundActivity,
203 );
204 const nextWithReplacements = {
205 ...nextWithReplacementsBase,
206 backgroundActivity: normalizedBackgroundActivity,
207 automaticGitFetchInterval: resolvedBackgroundActivity.automaticGitFetchInterval,
208 providerHealthRefreshInterval: resolvedBackgroundActivity.providerHealthRefreshInterval,
209 backgroundActivityProfile: resolvedBackgroundActivity.profile,
210 };
211 if (!selectionPatch) {
212 return nextWithReplacements;
213 }
214​
215 const instanceId = selectionPatch.instanceId ?? current.textGenerationModelSelection.instanceId;
216 const model = selectionPatch.model ?? current.textGenerationModelSelection.model;
217 const options = shouldReplaceTextGenerationModelSelection(selectionPatch)
218 ? selectionPatch.options
219 : mergeModelSelectionOptionsById({
220 current: current.textGenerationModelSelection.options,
221 patch: selectionPatch.options,
222 });
223​
224 return {
225 ...nextWithReplacements,
226 textGenerationModelSelection: createModelSelection(instanceId, model, options),
227 };