Open History to see the last commit
1import {
2 type ModelSelection,
3 PROJECT_SCOPED_SERVER_SETTING_KEYS,
4 type ProjectId,
5 type ProjectScopedServerSettingKey,
6 type ProjectSettingsOverrides,
7 type ServerSettings,
8 type ThreadEnvMode,
9} from "@t3tools/contracts";
10import { isModelSelectionProviderEnabled } from "./serverSettings.ts";
11
12export type ProjectSettingSource = "environment" | "project";
13
14export type ProjectSettingSources = Readonly<
15 Record<ProjectScopedServerSettingKey, ProjectSettingSource>
16>;
17
18export interface ResolvedProjectSettings {
19 /** Environment settings with the project's overrides applied. */
20 readonly settings: ServerSettings;
21 /** Where each scopable key's effective value came from. */
22 readonly sources: ProjectSettingSources;
23 /** The project's raw override entry; `{}` when it has none. */
24 readonly overrides: ProjectSettingsOverrides;
25}
26
27const EMPTY_OVERRIDES: ProjectSettingsOverrides = {};
28
29const ENVIRONMENT_SOURCES: ProjectSettingSources = Object.fromEntries(
30 PROJECT_SCOPED_SERVER_SETTING_KEYS.map((key) => [key, "environment"]),
31) as Record<ProjectScopedServerSettingKey, ProjectSettingSource>;
32
33/** Cheap check so hot paths skip the projectId lookup when nothing is overridden. */
34export function hasProjectSettingsOverrides(
35 settings: Pick<ServerSettings, "projectSettingsOverrides">,
36): boolean {
37 for (const entry of Object.values(settings.projectSettingsOverrides)) {
38 if (Object.keys(entry).length > 0) return true;
39 }
40 return false;
41}
42
43/**
44 * The project aggregate's own model and workspace fields. They remain the
45 * source of truth until the server has folded them into the override record;
46 * after the fold the record alone decides, so a reset there cannot be undone
47 * by a stale aggregate value.
48 */
49export interface LegacyProjectSettingsFields {
50 readonly defaultModelSelection?: ModelSelection | null | undefined;
51 readonly defaultThreadEnvMode?: ThreadEnvMode | null | undefined;
52}
53
54/**
55 * Apply one project's overrides on top of environment settings. A model
56 * override whose provider is disabled on this environment falls back to the
57 * environment value, the same guard the environment-level selection gets.
58 */
59export function resolveProjectSettings(
60 settings: ServerSettings,
61 projectId: ProjectId | null,
62 project?: LegacyProjectSettingsFields,
63): ResolvedProjectSettings {
64 const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId];
65 const overrides: ProjectSettingsOverrides =
66 project === undefined || settings.projectSettingsFolded
67 ? (stored ?? EMPTY_OVERRIDES)
68 : {
69 ...(project.defaultModelSelection != null
70 ? { defaultModelSelection: project.defaultModelSelection }
71 : {}),
72 ...(project.defaultThreadEnvMode != null
73 ? { defaultThreadEnvMode: project.defaultThreadEnvMode }
74 : {}),
75 ...stored,
76 };
77 if (Object.keys(overrides).length === 0) {
78 return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES };
79 }
80 const sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> = {
81 ...ENVIRONMENT_SOURCES,
82 };
83 const effective: Record<string, unknown> = { ...settings };
84 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
85 if (!Object.hasOwn(overrides, key)) continue;
86 const value = overrides[key];
87 // A model on a disabled provider falls back to the environment, like the
88 // environment-level guards do for these keys.
89 if (
90 (key === "textGenerationModelSelection" || key === "defaultModelSelection") &&
91 value !== undefined &&
92 value !== null &&
93 !isModelSelectionProviderEnabled(settings, value as ModelSelection)
94 ) {
95 continue;
96 }
97 effective[key] = value;
98 sources[key] = "project";
99 }
100 return { settings: effective as ServerSettings, sources, overrides };
101}
102
103/** Replace the project's entry, dropping it entirely when nothing is overridden. */
104export function withProjectSettingsOverrides(
105 settings: Pick<ServerSettings, "projectSettingsOverrides">,
106 projectId: ProjectId,
107 next: ProjectSettingsOverrides | null,
108): ServerSettings["projectSettingsOverrides"] {
109 const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides;
110 return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next };
111}
112
113/** The project's entry with `keys` removed; `null` when that leaves it empty. */
114export function clearProjectSettingsOverrides(
115 settings: Pick<ServerSettings, "projectSettingsOverrides">,
116 projectId: ProjectId,
117 keys: readonly ProjectScopedServerSettingKey[],
118): ProjectSettingsOverrides | null {
119 const current = settings.projectSettingsOverrides[projectId];
120 if (current === undefined) return null;
121 const next = { ...current };
122 for (const key of keys) delete next[key];
123 return Object.keys(next).length === 0 ? null : next;
124}