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

projectSettings.ts

146 lines5.7 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 type WorktreeCleanupRules,
10} from "@t3tools/contracts";
11import { isModelSelectionProviderEnabled } from "./serverSettings.ts";
12​
13export type ProjectSettingSource = "environment" | "project";
14​
15export type ProjectSettingSources = Readonly<
16 Record<ProjectScopedServerSettingKey, ProjectSettingSource>
17>;
18​
19export interface ResolvedProjectSettings {
20 /** Environment settings with the project's overrides applied. */
21 readonly settings: ServerSettings;
22 /** Where each scopable key's effective value came from. */
23 readonly sources: ProjectSettingSources;
24 /** The project's raw override entry; `{}` when it has none. */
25 readonly overrides: ProjectSettingsOverrides;
27​
28const EMPTY_OVERRIDES: ProjectSettingsOverrides = {};
29​
30const ENVIRONMENT_SOURCES: ProjectSettingSources = Object.fromEntries(
31 PROJECT_SCOPED_SERVER_SETTING_KEYS.map((key) => [key, "environment"]),
32) as Record<ProjectScopedServerSettingKey, ProjectSettingSource>;
33​
34/** Cheap check so hot paths skip the projectId lookup when nothing is overridden. */
35export function hasProjectSettingsOverrides(
36 settings: Pick<ServerSettings, "projectSettingsOverrides">,
37): boolean {
38 for (const entry of Object.values(settings.projectSettingsOverrides)) {
39 if (Object.keys(entry).length > 0) return true;
40 }
41 return false;
43​
44/**
45 * The project aggregate's own model and workspace fields. They remain the
46 * source of truth until the server has folded them into the override record;
47 * after the fold the record alone decides, so a reset there cannot be undone
48 * by a stale aggregate value.
49 */
50export interface LegacyProjectSettingsFields {
51 readonly defaultModelSelection?: ModelSelection | null | undefined;
52 readonly defaultThreadEnvMode?: ThreadEnvMode | null | undefined;
54​
55/**
56 * Apply one project's overrides on top of environment settings. A model
57 * override whose provider is disabled on this environment falls back to the
58 * environment value, the same guard the environment-level selection gets.
59 */
60export function resolveProjectSettings(
61 settings: ServerSettings,
62 projectId: ProjectId | null,
63 // Nullable, not just optional: the mobile new-task flow passes its selected
64 // project straight through, and that is null until the shell snapshot lands.
65 project?: LegacyProjectSettingsFields | null,
66): ResolvedProjectSettings {
67 const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId];
68 const overrides: ProjectSettingsOverrides =
69 project == null || settings.projectSettingsFolded
70 ? (stored ?? EMPTY_OVERRIDES)
71 : {
72 ...(project.defaultModelSelection != null
73 ? { defaultModelSelection: project.defaultModelSelection }
74 : {}),
75 ...(project.defaultThreadEnvMode != null
76 ? { defaultThreadEnvMode: project.defaultThreadEnvMode }
77 : {}),
78 ...stored,
79 };
80 if (Object.keys(overrides).length === 0) {
81 return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES };
82 }
83 const sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> = {
84 ...ENVIRONMENT_SOURCES,
85 };
86 const effective: Record<string, unknown> = { ...settings };
87 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
88 if (!Object.hasOwn(overrides, key)) continue;
89 const value = overrides[key];
90 // A model on a disabled provider falls back to the environment, like the
91 // environment-level guards do for these keys.
92 if (
93 (key === "textGenerationModelSelection" || key === "defaultModelSelection") &&
94 value !== undefined &&
95 value !== null &&
96 !isModelSelectionProviderEnabled(settings, value as ModelSelection)
97 ) {
98 continue;
99 }
100 effective[key] = value;
101 sources[key] = "project";
102 }
103 return { settings: effective as ServerSettings, sources, overrides };
105​
106/** Replace the project's entry, dropping it entirely when nothing is overridden. */
107export function withProjectSettingsOverrides(
108 settings: Pick<ServerSettings, "projectSettingsOverrides">,
109 projectId: ProjectId,
110 next: ProjectSettingsOverrides | null,
111): ServerSettings["projectSettingsOverrides"] {
112 const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides;
113 return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next };
115​
116/** The project's entry with `keys` removed; `null` when that leaves it empty. */
117export function clearProjectSettingsOverrides(
118 settings: Pick<ServerSettings, "projectSettingsOverrides">,
119 projectId: ProjectId,
120 keys: readonly ProjectScopedServerSettingKey[],
121): ProjectSettingsOverrides | null {
122 const current = settings.projectSettingsOverrides[projectId];
123 if (current === undefined) return null;
124 const next = { ...current };
125 for (const key of keys) delete next[key];
126 return Object.keys(next).length === 0 ? null : next;
128​
129/** Worktree rules are project-scoped; artifact and log retention stays environment-wide. */
130export function resolveWorktreeCleanup(
131 settings: ServerSettings,
132 projectId: ProjectId | null,
133): WorktreeCleanupRules {
134 const policy = resolveProjectSettings(settings, projectId).settings.worktreeCleanup;
135 if (policy?.mode === "custom") return policy.rules;
136 if (policy?.mode === "off")
137 return {
138 worktreeAfterDays: null,
139 worktreeOnMerge: false,
140 worktreeOnDelete: false,
141 worktreeUnchanged: false,
142 };
143 const { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged } =
144 settings.storageCleanup;
145 return { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged };