Open History to see the last commit
1import { ServerSettings, type ServerSettingsPatch } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4import { deepMerge } from "./Struct.ts";
5import { fromLenientJson } from "./schemaJson.ts";
6import { createModelSelection } from "./model.ts";
7
8const ServerSettingsJson = fromLenientJson(ServerSettings);
9const decodeServerSettingsJson = Schema.decodeUnknownOption(ServerSettingsJson);
10
11export interface PersistedServerObservabilitySettings {
12 readonly otlpTracesUrl: string | undefined;
13 readonly otlpMetricsUrl: string | undefined;
14}
15
16export function normalizePersistedServerSettingString(
17 value: string | null | undefined,
18): string | undefined {
19 const trimmed = value?.trim();
20 return trimmed && trimmed.length > 0 ? trimmed : undefined;
21}
22
23export function extractPersistedServerObservabilitySettings(input: {
24 readonly observability?: {
25 readonly otlpTracesUrl?: string;
26 readonly otlpMetricsUrl?: string;
27 };
28}): PersistedServerObservabilitySettings {
29 return {
30 otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl),
31 otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl),
32 };
33}
34
35export function parsePersistedServerObservabilitySettings(
36 raw: string,
37): PersistedServerObservabilitySettings {
38 const decoded = decodeServerSettingsJson(raw);
39 if (Option.isSome(decoded)) {
40 return extractPersistedServerObservabilitySettings(decoded.value);
41 }
42 return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
43}
44
45function shouldReplaceTextGenerationModelSelection(
46 patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
47): boolean {
48 return Boolean(patch && (patch.instanceId !== undefined || patch.model !== undefined));
49}
50
51function mergeModelSelectionOptionsById(input: {
52 current: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
53 patch: ReadonlyArray<{ readonly id: string; readonly value: string | boolean }> | undefined;
54}): Array<{ id: string; value: string | boolean }> | undefined {
55 if (input.patch === undefined) {
56 return input.current ? [...input.current] : undefined;
57 }
58 if (input.patch.length === 0) {
59 return undefined;
60 }
61
62 const merged = new Map((input.current ?? []).map((selection) => [selection.id, selection.value]));
63 for (const selection of input.patch) {
64 merged.set(selection.id, selection.value);
65 }
66 return [...merged.entries()].map(([id, value]) => ({ id, value }));
67}
68
69/**
70 * Applies a server settings patch while treating textGenerationModelSelection as
71 * replace-on-provider/model updates. This prevents stale nested options from
72 * surviving a reset patch that intentionally omits options.
73 */
74export function applyServerSettingsPatch(
75 current: ServerSettings,
76 patch: ServerSettingsPatch,
77): ServerSettings {
78 const selectionPatch = patch.textGenerationModelSelection;
79 const { automaticGitFetchInterval, ...patchForMerge } = patch;
80 const next = deepMerge(current, patchForMerge);
81 const nextWithReplacements = {
82 ...next,
83 ...(patch.providerInstances !== undefined
84 ? { providerInstances: patch.providerInstances }
85 : {}),
86 ...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
87 };
88 if (!selectionPatch) {
89 return nextWithReplacements;
90 }
91
92 const instanceId = selectionPatch.instanceId ?? current.textGenerationModelSelection.instanceId;
93 const model = selectionPatch.model ?? current.textGenerationModelSelection.model;
94 const options = shouldReplaceTextGenerationModelSelection(selectionPatch)
95 ? selectionPatch.options
96 : mergeModelSelectionOptionsById({
97 current: current.textGenerationModelSelection.options,
98 patch: selectionPatch.options,
99 });
100
101 return {
102 ...nextWithReplacements,
103 textGenerationModelSelection: createModelSelection(instanceId, model, options),
104 };
105}