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

projectSettings.ts

126 lines4.9 KB

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;
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;
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;
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 // Nullable, not just optional: the mobile new-task flow passes its selected
63 // project straight through, and that is null until the shell snapshot lands.
64 project?: LegacyProjectSettingsFields | null,
65): ResolvedProjectSettings {
66 const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId];
67 const overrides: ProjectSettingsOverrides =
68 project == null || settings.projectSettingsFolded
69 ? (stored ?? EMPTY_OVERRIDES)
70 : {
71 ...(project.defaultModelSelection != null
72 ? { defaultModelSelection: project.defaultModelSelection }
73 : {}),
74 ...(project.defaultThreadEnvMode != null
75 ? { defaultThreadEnvMode: project.defaultThreadEnvMode }
76 : {}),
77 ...stored,
78 };
79 if (Object.keys(overrides).length === 0) {
80 return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES };
81 }
82 const sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> = {
83 ...ENVIRONMENT_SOURCES,
84 };
85 const effective: Record<string, unknown> = { ...settings };
86 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
87 if (!Object.hasOwn(overrides, key)) continue;
88 const value = overrides[key];
89 // A model on a disabled provider falls back to the environment, like the
90 // environment-level guards do for these keys.
91 if (
92 (key === "textGenerationModelSelection" || key === "defaultModelSelection") &&
93 value !== undefined &&
94 value !== null &&
95 !isModelSelectionProviderEnabled(settings, value as ModelSelection)
96 ) {
97 continue;
98 }
99 effective[key] = value;
100 sources[key] = "project";
101 }
102 return { settings: effective as ServerSettings, sources, overrides };
104​
105/** Replace the project's entry, dropping it entirely when nothing is overridden. */
106export function withProjectSettingsOverrides(
107 settings: Pick<ServerSettings, "projectSettingsOverrides">,
108 projectId: ProjectId,
109 next: ProjectSettingsOverrides | null,
110): ServerSettings["projectSettingsOverrides"] {
111 const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides;
112 return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next };
114​
115/** The project's entry with `keys` removed; `null` when that leaves it empty. */
116export function clearProjectSettingsOverrides(
117 settings: Pick<ServerSettings, "projectSettingsOverrides">,
118 projectId: ProjectId,
119 keys: readonly ProjectScopedServerSettingKey[],
120): ProjectSettingsOverrides | null {
121 const current = settings.projectSettingsOverrides[projectId];
122 if (current === undefined) return null;
123 const next = { ...current };
124 for (const key of keys) delete next[key];
125 return Object.keys(next).length === 0 ? null : next;