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

serverSettings.ts

226 lines8.3 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 ...patchForMerge
135 } = patch;
136 const currentBackgroundActivity = normalizeServerBackgroundActivitySettings(current);
137 const backgroundActivityPatch =
138 backgroundActivityProfile !== undefined
139 ? {
140 schemaVersion: 1 as const,
141 profile:
142 automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
143 ? ("custom" as const)
144 : backgroundActivityProfile,
145 ...(automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
146 ? { baseProfile: backgroundActivityProfile }
147 : {}),
148 overrides: {
149 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
150 ...(providerHealthRefreshInterval !== undefined
151 ? { providerHealthRefreshInterval }
152 : {}),
153 },
154 }
155 : automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
156 ? {
157 schemaVersion: 1 as const,
158 profile: "custom" as const,
159 baseProfile: getBackgroundActivityBaseProfile(currentBackgroundActivity),
160 overrides: {
161 ...(currentBackgroundActivity.profile === "custom"
162 ? currentBackgroundActivity.overrides
163 : {}),
164 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
165 ...(providerHealthRefreshInterval !== undefined
166 ? { providerHealthRefreshInterval }
167 : {}),
168 },
169 }
170 : undefined;
171 const next = deepMerge(current, patchForMerge);
172 const nextWithReplacementsBase = {
173 ...next,
174 ...(backgroundActivity !== undefined
175 ? {
176 backgroundActivity: {
177 ...deepMerge(currentBackgroundActivity, backgroundActivity),
178 ...(backgroundActivity.overrides !== undefined
179 ? { overrides: backgroundActivity.overrides }
180 : {}),
181 },
182 }
183 : { backgroundActivity: currentBackgroundActivity }),
184 ...(backgroundActivity === undefined && backgroundActivityPatch !== undefined
185 ? { backgroundActivity: backgroundActivityPatch }
186 : {}),
187 ...(patch.providerInstances !== undefined
188 ? { providerInstances: patch.providerInstances }
189 : {}),
190 ...(patch.sourceControlWriterModelSelection !== undefined
191 ? { sourceControlWriterModelSelection: patch.sourceControlWriterModelSelection }
192 : {}),
193 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
194 ...(providerHealthRefreshInterval !== undefined ? { providerHealthRefreshInterval } : {}),
195 };
196 const normalizedBackgroundActivity = normalizeBackgroundActivitySettings(
197 nextWithReplacementsBase.backgroundActivity,
198 );
199 const resolvedBackgroundActivity = resolveBackgroundActivitySettings(
200 normalizedBackgroundActivity,
201 );
202 const nextWithReplacements = {
203 ...nextWithReplacementsBase,
204 backgroundActivity: normalizedBackgroundActivity,
205 automaticGitFetchInterval: resolvedBackgroundActivity.automaticGitFetchInterval,
206 providerHealthRefreshInterval: resolvedBackgroundActivity.providerHealthRefreshInterval,
207 backgroundActivityProfile: resolvedBackgroundActivity.profile,
208 };
209 if (!selectionPatch) {
210 return nextWithReplacements;
211 }
212​
213 const instanceId = selectionPatch.instanceId ?? current.textGenerationModelSelection.instanceId;
214 const model = selectionPatch.model ?? current.textGenerationModelSelection.model;
215 const options = shouldReplaceTextGenerationModelSelection(selectionPatch)
216 ? selectionPatch.options
217 : mergeModelSelectionOptionsById({
218 current: current.textGenerationModelSelection.options,
219 patch: selectionPatch.options,
220 });
221​
222 return {
223 ...nextWithReplacements,
224 textGenerationModelSelection: createModelSelection(instanceId, model, options),
225 };