Files
t3code/packages/shared/src/serverSettings.ts
T
ce94feeea1 feat: add opencode provider support (#1758)
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: codex <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-04-17 13:31:41 -07:00

102 lines
3.2 KiB
TypeScript

import {
ServerSettings,
type ClaudeModelOptions,
type CodexModelOptions,
type OpenCodeModelOptions,
type ServerSettingsPatch,
} from "@t3tools/contracts";
import { Schema } from "effect";
import { deepMerge } from "./Struct.ts";
import { fromLenientJson } from "./schemaJson.ts";
const ServerSettingsJson = fromLenientJson(ServerSettings);
export interface PersistedServerObservabilitySettings {
readonly otlpTracesUrl: string | undefined;
readonly otlpMetricsUrl: string | undefined;
}
export function normalizePersistedServerSettingString(
value: string | null | undefined,
): string | undefined {
const trimmed = value?.trim();
return trimmed && trimmed.length > 0 ? trimmed : undefined;
}
export function extractPersistedServerObservabilitySettings(input: {
readonly observability?: {
readonly otlpTracesUrl?: string;
readonly otlpMetricsUrl?: string;
};
}): PersistedServerObservabilitySettings {
return {
otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl),
otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl),
};
}
export function parsePersistedServerObservabilitySettings(
raw: string,
): PersistedServerObservabilitySettings {
try {
const decoded = Schema.decodeUnknownSync(ServerSettingsJson)(raw);
return extractPersistedServerObservabilitySettings(decoded);
} catch {
return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
}
}
function shouldReplaceTextGenerationModelSelection(
patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
): boolean {
return Boolean(patch && (patch.provider !== undefined || patch.model !== undefined));
}
const withModelSelectionOptions = <Options>(options: Options | undefined) =>
options ? { options } : {};
/**
* Applies a server settings patch while treating textGenerationModelSelection as
* replace-on-provider/model updates. This prevents stale nested options from
* surviving a reset patch that intentionally omits options.
*/
export function applyServerSettingsPatch(
current: ServerSettings,
patch: ServerSettingsPatch,
): ServerSettings {
const selectionPatch = patch.textGenerationModelSelection;
const next = deepMerge(current, patch);
if (!selectionPatch || !shouldReplaceTextGenerationModelSelection(selectionPatch)) {
return next;
}
const provider = selectionPatch.provider ?? current.textGenerationModelSelection.provider;
const model = selectionPatch.model ?? current.textGenerationModelSelection.model;
return {
...next,
textGenerationModelSelection:
provider === "codex"
? {
provider,
model,
...withModelSelectionOptions(selectionPatch.options as CodexModelOptions | undefined),
}
: provider === "claudeAgent"
? {
provider,
model,
...withModelSelectionOptions(
selectionPatch.options as ClaudeModelOptions | undefined,
),
}
: {
provider,
model,
...withModelSelectionOptions(
selectionPatch.options as OpenCodeModelOptions | undefined,
),
},
};
}