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

sharedSettings.ts

97 lines3.2 KB

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