Files
t3code/packages/shared/src/projectSettings.ts
T

147 lines
5.7 KiB
TypeScript

import {
type ModelSelection,
PROJECT_SCOPED_SERVER_SETTING_KEYS,
type ProjectId,
type ProjectScopedServerSettingKey,
type ProjectSettingsOverrides,
type ServerSettings,
type ThreadEnvMode,
type WorktreeCleanupRules,
} from "@t3tools/contracts";
import { isModelSelectionProviderEnabled } from "./serverSettings.ts";
export type ProjectSettingSource = "environment" | "project";
export type ProjectSettingSources = Readonly<
Record<ProjectScopedServerSettingKey, ProjectSettingSource>
>;
export interface ResolvedProjectSettings {
/** Environment settings with the project's overrides applied. */
readonly settings: ServerSettings;
/** Where each scopable key's effective value came from. */
readonly sources: ProjectSettingSources;
/** The project's raw override entry; `{}` when it has none. */
readonly overrides: ProjectSettingsOverrides;
}
const EMPTY_OVERRIDES: ProjectSettingsOverrides = {};
const ENVIRONMENT_SOURCES: ProjectSettingSources = Object.fromEntries(
PROJECT_SCOPED_SERVER_SETTING_KEYS.map((key) => [key, "environment"]),
) as Record<ProjectScopedServerSettingKey, ProjectSettingSource>;
/** Cheap check so hot paths skip the projectId lookup when nothing is overridden. */
export function hasProjectSettingsOverrides(
settings: Pick<ServerSettings, "projectSettingsOverrides">,
): boolean {
for (const entry of Object.values(settings.projectSettingsOverrides)) {
if (Object.keys(entry).length > 0) return true;
}
return false;
}
/**
* The project aggregate's own model and workspace fields. They remain the
* source of truth until the server has folded them into the override record;
* after the fold the record alone decides, so a reset there cannot be undone
* by a stale aggregate value.
*/
export interface LegacyProjectSettingsFields {
readonly defaultModelSelection?: ModelSelection | null | undefined;
readonly defaultThreadEnvMode?: ThreadEnvMode | null | undefined;
}
/**
* Apply one project's overrides on top of environment settings. A model
* override whose provider is disabled on this environment falls back to the
* environment value, the same guard the environment-level selection gets.
*/
export function resolveProjectSettings(
settings: ServerSettings,
projectId: ProjectId | null,
// Nullable, not just optional: the mobile new-task flow passes its selected
// project straight through, and that is null until the shell snapshot lands.
project?: LegacyProjectSettingsFields | null,
): ResolvedProjectSettings {
const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId];
const overrides: ProjectSettingsOverrides =
project == null || settings.projectSettingsFolded
? (stored ?? EMPTY_OVERRIDES)
: {
...(project.defaultModelSelection != null
? { defaultModelSelection: project.defaultModelSelection }
: {}),
...(project.defaultThreadEnvMode != null
? { defaultThreadEnvMode: project.defaultThreadEnvMode }
: {}),
...stored,
};
if (Object.keys(overrides).length === 0) {
return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES };
}
const sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> = {
...ENVIRONMENT_SOURCES,
};
const effective: Record<string, unknown> = { ...settings };
for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
if (!Object.hasOwn(overrides, key)) continue;
const value = overrides[key];
// A model on a disabled provider falls back to the environment, like the
// environment-level guards do for these keys.
if (
(key === "textGenerationModelSelection" || key === "defaultModelSelection") &&
value !== undefined &&
value !== null &&
!isModelSelectionProviderEnabled(settings, value as ModelSelection)
) {
continue;
}
effective[key] = value;
sources[key] = "project";
}
return { settings: effective as ServerSettings, sources, overrides };
}
/** Replace the project's entry, dropping it entirely when nothing is overridden. */
export function withProjectSettingsOverrides(
settings: Pick<ServerSettings, "projectSettingsOverrides">,
projectId: ProjectId,
next: ProjectSettingsOverrides | null,
): ServerSettings["projectSettingsOverrides"] {
const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides;
return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next };
}
/** The project's entry with `keys` removed; `null` when that leaves it empty. */
export function clearProjectSettingsOverrides(
settings: Pick<ServerSettings, "projectSettingsOverrides">,
projectId: ProjectId,
keys: readonly ProjectScopedServerSettingKey[],
): ProjectSettingsOverrides | null {
const current = settings.projectSettingsOverrides[projectId];
if (current === undefined) return null;
const next = { ...current };
for (const key of keys) delete next[key];
return Object.keys(next).length === 0 ? null : next;
}
/** Worktree rules are project-scoped; artifact and log retention stays environment-wide. */
export function resolveWorktreeCleanup(
settings: ServerSettings,
projectId: ProjectId | null,
): WorktreeCleanupRules {
const policy = resolveProjectSettings(settings, projectId).settings.worktreeCleanup;
if (policy?.mode === "custom") return policy.rules;
if (policy?.mode === "off")
return {
worktreeAfterDays: null,
worktreeOnMerge: false,
worktreeOnDelete: false,
worktreeUnchanged: false,
};
const { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged } =
settings.storageCleanup;
return { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged };
}