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

sharedSettings.ts

156 lines5.1 KB

1import type {
2 EnvironmentId,
3 ExecutionEnvironmentCapabilities,
4 ServerSettings,
5 ServerSettingsPatch,
6} from "@t3tools/contracts";
7import { isModelSelectionProviderEnabled } from "@t3tools/shared/serverSettings";
8import * as Equal from "effect/Equal";
9import * as Struct from "effect/Struct";
10​
11import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
12​
13const SHARED_SERVER_SETTING_KEYS = [
14 "continueThreadsAfterServerUpdate",
15 "sidebarAutoSettleAfterDays",
16 "sidebarAutoSettleOnMerge",
17 "autoResumeLimitedThreads",
18 "snoozeLimitedThreads",
19 "newWorktreesStartFromOrigin",
20 "sourceControlWritingStyle",
21 "textGenerationModelSelection",
22] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
23​
24export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
25​
26const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
27​
28export function splitSharedServerPatch(patch: ServerSettingsPatch): {
29 sharedPatch: ServerSettingsPatch;
30 localPatch: ServerSettingsPatch;
31} {
32 const sharedPatch: Record<string, unknown> = {};
33 const localPatch: Record<string, unknown> = {};
34 for (const [key, value] of Object.entries(patch)) {
35 if (SHARED_KEY_SET.has(key)) {
36 sharedPatch[key] = value;
37 } else {
38 localPatch[key] = value;
39 }
40 }
41 return {
42 sharedPatch: sharedPatch as ServerSettingsPatch,
43 localPatch: localPatch as ServerSettingsPatch,
44 };
46​
47export function filterSharedServerPatch(
48 patch: ServerSettingsPatch,
49 capabilities: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation"> | undefined,
50 settings?: ServerSettings,
51 sourceSettings = settings,
52 targetIsSource = false,
53): ServerSettingsPatch {
54 const instanceId =
55 patch.textGenerationModelSelection?.instanceId ??
56 sourceSettings?.textGenerationModelSelection.instanceId;
57 if (
58 !targetIsSource &&
59 patch.textGenerationModelSelection &&
60 (!settings ||
61 (instanceId !== undefined &&
62 (sourceSettings?.providerInstances[instanceId]?.driver ?? instanceId) !==
63 (settings.providerInstances[instanceId]?.driver ?? instanceId)) ||
64 !isModelSelectionProviderEnabled(settings, {
65 ...settings.textGenerationModelSelection,
66 ...patch.textGenerationModelSelection,
67 }))
68 ) {
69 patch = Struct.omit(patch, ["textGenerationModelSelection"]);
70 }
71 return capabilities?.threadRestartContinuation === true
72 ? patch
73 : Struct.omit(patch, ["continueThreadsAfterServerUpdate"]);
75​
76export function pickSharedServerSettings(
77 settings: ServerSettings,
78 capabilities?: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">,
79): ServerSettingsPatch {
80 return filterSharedServerPatch(
81 Struct.pick(settings, SHARED_SERVER_SETTING_KEYS),
82 capabilities,
83 settings,
84 );
86​
87export function supportsSharedSettingsSync(environment: {
88 readonly connection: { readonly phase: EnvironmentConnectionPhase };
89 readonly serverConfig: {
90 readonly environment: {
91 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
92 };
93 } | null;
94}): boolean {
95 return (
96 environment.connection.phase === "connected" &&
97 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
98 );
100​
101export interface SharedSettingsEnvironment {
102 readonly environmentId: EnvironmentId;
103 readonly label: string;
104 readonly syncEligible: boolean;
105 readonly settings: ServerSettings | null;
106 readonly capabilities?:
107 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
108 | undefined;
110​
111/**
112 * Callers must pass the real loaded settings, never a default fallback, or
113 * "apply to all" would push defaults over real values.
114 */
115export function findSharedSettingsMismatches(input: {
116 readonly primaryEnvironmentId: EnvironmentId | null;
117 readonly primarySettings: ServerSettings | null;
118 readonly primaryCapabilities?:
119 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
120 | undefined;
121 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
122}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
123 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
124 return [];
125 }
126 const primarySettings = pickSharedServerSettings(
127 input.primarySettings,
128 input.primaryCapabilities,
129 );
130 return input.environments.flatMap((environment) => {
131 if (
132 environment.environmentId === input.primaryEnvironmentId ||
133 !environment.syncEligible ||
134 environment.settings === null
135 ) {
136 return [];
137 }
138 const expected = filterSharedServerPatch(
139 primarySettings,
140 environment.capabilities,
141 environment.settings,
142 input.primarySettings ?? undefined,
143 );
144 let actual = filterSharedServerPatch(
145 pickSharedServerSettings(environment.settings, environment.capabilities),
146 input.primaryCapabilities,
147 environment.settings,
148 );
149 if (!expected.textGenerationModelSelection) {
150 actual = Struct.omit(actual, ["textGenerationModelSelection"]);
151 }
152 return Equal.equals(actual, expected)
153 ? []
154 : [{ environmentId: environment.environmentId, label: environment.label }];
155 });