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

sharedSettings.ts

98 lines3.2 KB

1import type {
2 EnvironmentId,
3 ExecutionEnvironmentCapabilities,
4 ServerSettings,
5 ServerSettingsPatch,
6} from "@t3tools/contracts";
7import { isModelSelectionProviderEnabled } from "@t3tools/shared/serverSettings";
8import * as Struct from "effect/Struct";
9​
10import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
11​
12const SHARED_SERVER_SETTING_KEYS = [
13 "continueThreadsAfterServerUpdate",
14 "sidebarAutoSettleAfterDays",
15 "sidebarAutoSettleOnMerge",
16 "autoResumeLimitedThreads",
17 "snoozeLimitedThreads",
18 "newWorktreesStartFromOrigin",
19 "sourceControlWritingStyle",
20 "textGenerationModelSelection",
21] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
22​
23export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
24​
25const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
26​
27export function splitSharedServerPatch(patch: ServerSettingsPatch): {
28 sharedPatch: ServerSettingsPatch;
29 localPatch: ServerSettingsPatch;
30} {
31 const sharedPatch: Record<string, unknown> = {};
32 const localPatch: Record<string, unknown> = {};
33 for (const [key, value] of Object.entries(patch)) {
34 if (SHARED_KEY_SET.has(key)) {
35 sharedPatch[key] = value;
36 } else {
37 localPatch[key] = value;
38 }
39 }
40 return {
41 sharedPatch: sharedPatch as ServerSettingsPatch,
42 localPatch: localPatch as ServerSettingsPatch,
43 };
45​
46export function filterSharedServerPatch(
47 patch: ServerSettingsPatch,
48 capabilities: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation"> | undefined,
49 settings?: ServerSettings,
50 sourceSettings = settings,
51 targetIsSource = false,
52): ServerSettingsPatch {
53 const instanceId =
54 patch.textGenerationModelSelection?.instanceId ??
55 sourceSettings?.textGenerationModelSelection.instanceId;
56 if (
57 !targetIsSource &&
58 patch.textGenerationModelSelection &&
59 (!settings ||
60 (instanceId !== undefined &&
61 (sourceSettings?.providerInstances[instanceId]?.driver ?? instanceId) !==
62 (settings.providerInstances[instanceId]?.driver ?? instanceId)) ||
63 !isModelSelectionProviderEnabled(settings, {
64 ...settings.textGenerationModelSelection,
65 ...patch.textGenerationModelSelection,
66 }))
67 ) {
68 patch = Struct.omit(patch, ["textGenerationModelSelection"]);
69 }
70 return capabilities?.threadRestartContinuation === true
71 ? patch
72 : Struct.omit(patch, ["continueThreadsAfterServerUpdate"]);
74​
75export function pickSharedServerSettings(
76 settings: ServerSettings,
77 capabilities?: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">,
78): ServerSettingsPatch {
79 return filterSharedServerPatch(
80 Struct.pick(settings, SHARED_SERVER_SETTING_KEYS),
81 capabilities,
82 settings,
83 );
85​
86export function supportsSharedSettingsSync(environment: {
87 readonly connection: { readonly phase: EnvironmentConnectionPhase };
88 readonly serverConfig: {
89 readonly environment: {
90 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
91 };
92 } | null;
93}): boolean {
94 return (
95 environment.connection.phase === "connected" &&
96 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
97 );