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