Open History to see the last commit
1import {
2 isProviderDriverKind,
3 isProviderAvailable,
4 resolveProviderInstanceEnabled,
5 type ModelSelection,
6 type ProviderDriverKind,
7 type ServerProvider,
8 ServerSettings,
9 type ServerSettingsPatch,
10} from "@t3tools/contracts";
11import * as Option from "effect/Option";
12import * as Schema from "effect/Schema";
13import { deepMerge } from "./Struct.ts";
14import { fromLenientJson } from "./schemaJson.ts";
15import { createModelSelection } from "./model.ts";
16import {
17 getBackgroundActivityBaseProfile,
18 normalizeBackgroundActivitySettings,
19 normalizeServerBackgroundActivitySettings,
20 resolveBackgroundActivitySettings,
21} from "./backgroundActivitySettings.ts";
22
23const ServerSettingsJson = fromLenientJson(ServerSettings);
24const decodeServerSettingsJson = Schema.decodeUnknownOption(ServerSettingsJson);
25
26type LegacyProviderSettings = ServerSettings["providers"][keyof ServerSettings["providers"]];
27
28const getLegacyProviderSettings = (
29 settings: ServerSettings,
30 provider: ProviderDriverKind,
31): LegacyProviderSettings | undefined =>
32 (settings.providers as Record<string, LegacyProviderSettings | undefined>)[provider];
33
34export function isModelSelectionProviderEnabled(
35 settings: ServerSettings,
36 selection: ModelSelection,
37): boolean {
38 const instanceConfig = settings.providerInstances[selection.instanceId];
39 if (instanceConfig !== undefined) {
40 return resolveProviderInstanceEnabled(instanceConfig);
41 }
42
43 return (
44 isProviderDriverKind(selection.instanceId) &&
45 getLegacyProviderSettings(settings, selection.instanceId)?.enabled === true
46 );
47}
48
49export function resolveSourceControlWriterModelSelection(
50 settings: ServerSettings,
51 providers?: ReadonlyArray<ServerProvider>,
52): ModelSelection {
53 const selection = settings.sourceControlWriterModelSelection;
54 if (!selection || !isModelSelectionProviderEnabled(settings, selection)) {
55 return settings.textGenerationModelSelection;
56 }
57 if (providers === undefined) {
58 return selection;
59 }
60
61 const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
62 return provider?.enabled === true && isProviderAvailable(provider)
63 ? selection
64 : settings.textGenerationModelSelection;
65}
66
67export interface PersistedServerObservabilitySettings {
68 readonly otlpTracesUrl: string | undefined;
69 readonly otlpMetricsUrl: string | undefined;
70}
71
72export function normalizePersistedServerSettingString(
73 value: string | null | undefined,
74): string | undefined {
75 const trimmed = value?.trim();
76 return trimmed && trimmed.length > 0 ? trimmed : undefined;
77}
78
79export function extractPersistedServerObservabilitySettings(input: {
80 readonly observability?: {
81 readonly otlpTracesUrl?: string;
82 readonly otlpMetricsUrl?: string;
83 };
84}): PersistedServerObservabilitySettings {
85 return {
86 otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl),
87 otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl),
88 };
89}
90
91export function parsePersistedServerObservabilitySettings(
92 raw: string,
93): PersistedServerObservabilitySettings {
94 const decoded = decodeServerSettingsJson(raw);
95 if (Option.isSome(decoded)) {
96 return extractPersistedServerObservabilitySettings(decoded.value);
97 }
98 return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
99}
100
101function shouldReplaceTextGenerationModelSelection(
102 patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
103): boolean {
104 return Boolean(patch && (patch.instanceId !== undefined || patch.model !== undefined));
105}
106
107function mergeModelSelectionOptionsById(input: {
108 current: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
109 patch: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
110}): Array<{ id: string; value: string | boolean }> | undefined {
111 if (input.patch === undefined) {
112 return input.current ? [...input.current] : undefined;
113 }
114 if (input.patch.length === 0) {
115 return undefined;
116 }
117
118 const merged = new Map((input.current ?? []).map((selection) => [selection.id, selection.value]));
119 for (const selection of input.patch) {
120 merged.set(selection.id, selection.value);
121 }
122 return [...merged.entries()].map(([id, value]) => ({ id, value }));
123}
124
125export function applyServerSettingsPatch(
126 current: ServerSettings,
127 patch: ServerSettingsPatch,
128): ServerSettings {
129 const selectionPatch = patch.textGenerationModelSelection;
130 const {
131 automaticGitFetchInterval,
132 providerHealthRefreshInterval,
133 backgroundActivityProfile,
134 backgroundActivity,
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 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
195 ...(providerHealthRefreshInterval !== undefined ? { providerHealthRefreshInterval } : {}),
196 };
197 const normalizedBackgroundActivity = normalizeBackgroundActivitySettings(
198 nextWithReplacementsBase.backgroundActivity,
199 );
200 const resolvedBackgroundActivity = resolveBackgroundActivitySettings(
201 normalizedBackgroundActivity,
202 );
203 const nextWithReplacements = {
204 ...nextWithReplacementsBase,
205 backgroundActivity: normalizedBackgroundActivity,
206 automaticGitFetchInterval: resolvedBackgroundActivity.automaticGitFetchInterval,
207 providerHealthRefreshInterval: resolvedBackgroundActivity.providerHealthRefreshInterval,
208 backgroundActivityProfile: resolvedBackgroundActivity.profile,
209 };
210 if (!selectionPatch) {
211 return nextWithReplacements;
212 }
213
214 const instanceId = selectionPatch.instanceId ?? current.textGenerationModelSelection.instanceId;
215 const model = selectionPatch.model ?? current.textGenerationModelSelection.model;
216 const options = shouldReplaceTextGenerationModelSelection(selectionPatch)
217 ? selectionPatch.options
218 : mergeModelSelectionOptionsById({
219 current: current.textGenerationModelSelection.options,
220 patch: selectionPatch.options,
221 });
222
223 return {
224 ...nextWithReplacements,
225 textGenerationModelSelection: createModelSelection(instanceId, model, options),
226 };
227}