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

serverSettings.ts

72 lines2.5 KB

1import { ServerSettings, type ServerSettingsPatch } from "@t3tools/contracts";
2import { Schema } from "effect";
3import { deepMerge } from "./Struct";
4import { fromLenientJson } from "./schemaJson";
5​
6const ServerSettingsJson = fromLenientJson(ServerSettings);
7​
8export interface PersistedServerObservabilitySettings {
9 readonly otlpTracesUrl: string | undefined;
10 readonly otlpMetricsUrl: string | undefined;
12​
13export function normalizePersistedServerSettingString(
14 value: string | null | undefined,
15): string | undefined {
16 const trimmed = value?.trim();
17 return trimmed && trimmed.length > 0 ? trimmed : undefined;
19​
20export function extractPersistedServerObservabilitySettings(input: {
21 readonly observability?: {
22 readonly otlpTracesUrl?: string;
23 readonly otlpMetricsUrl?: string;
24 };
25}): PersistedServerObservabilitySettings {
26 return {
27 otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl),
28 otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl),
29 };
31​
32export function parsePersistedServerObservabilitySettings(
33 raw: string,
34): PersistedServerObservabilitySettings {
35 try {
36 const decoded = Schema.decodeUnknownSync(ServerSettingsJson)(raw);
37 return extractPersistedServerObservabilitySettings(decoded);
38 } catch {
39 return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
40 }
42​
43function shouldReplaceTextGenerationModelSelection(
44 patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
45): boolean {
46 return Boolean(patch && (patch.provider !== undefined || patch.model !== undefined));
48​
49/**
50 * Applies a server settings patch while treating textGenerationModelSelection as
51 * replace-on-provider/model updates. This prevents stale nested options from
52 * surviving a reset patch that intentionally omits options.
53 */
54export function applyServerSettingsPatch(
55 current: ServerSettings,
56 patch: ServerSettingsPatch,
57): ServerSettings {
58 const selectionPatch = patch.textGenerationModelSelection;
59 const next = deepMerge(current, patch);
60 if (!selectionPatch || !shouldReplaceTextGenerationModelSelection(selectionPatch)) {
61 return next;
62 }
63​
64 return {
65 ...next,
66 textGenerationModelSelection: {
67 provider: selectionPatch.provider ?? current.textGenerationModelSelection.provider,
68 model: selectionPatch.model ?? current.textGenerationModelSelection.model,
69 ...(selectionPatch.options ? { options: selectionPatch.options } : {}),
70 },
71 };