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