This commit is contained in:
Julius Marminge committed 2026-03-25 17:00:57 -07:00
1 parent da0eaff9d4
commit c91bd26b3c
8 files changed
+265 -28

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import { Effect, Layer, Schema } from "effect";
import { ServerConfig } from "./config";
import { ServerSettingsLive, ServerSettingsService } from "./serverSettings";
const makeServerSettingsLayer = () =>
ServerSettingsLive.pipe(
Layer.provideMerge(
Layer.fresh(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-server-settings-test-",
}),
),
),
);
it.layer(NodeServices.layer)("server settings", (it) => {
it.effect("decodes nested settings patches", () =>
Effect.sync(() => {
const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);
assert.deepEqual(decodePatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }), {
providers: { codex: { binaryPath: "/tmp/codex" } },
});
assert.deepEqual(
decodePatch({
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
}),
{
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
},
);
}),
);
it.effect("deep merges nested settings updates without dropping siblings", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/usr/local/bin/codex",
homePath: "/Users/julius/.codex",
},
claudeAgent: {
binaryPath: "/usr/local/bin/claude",
customModels: ["claude-custom"],
},
},
textGenerationModelSelection: {
provider: "codex",
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
options: {
reasoningEffort: "high",
fastMode: true,
},
},
});
const next = yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/opt/homebrew/bin/codex",
},
},
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
});
assert.deepEqual(next.providers.codex, {
binaryPath: "/opt/homebrew/bin/codex",
homePath: "/Users/julius/.codex",
customModels: [],
});
assert.deepEqual(next.providers.claudeAgent, {
binaryPath: "/usr/local/bin/claude",
customModels: ["claude-custom"],
});
assert.deepEqual(next.textGenerationModelSelection, {
provider: "codex",
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
options: {
reasoningEffort: "high",
fastMode: false,
},
});
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
});
+2 -1
View File
@@ -35,6 +35,7 @@ import {
} from "effect";
import * as Semaphore from "effect/Semaphore";
import { ServerConfig } from "./config";
import { deepMerge } from "@t3tools/shared/Struct";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
@@ -282,7 +283,7 @@ const makeServerSettings = Effect.gen(function* () {
writeSemaphore.withPermits(1)(
Effect.gen(function* () {
const current = yield* getSettingsFromCache;
const next: ServerSettings = { ...current, ...patch };
const next = deepMerge(current, patch);
yield* writeSettingsAtomically(next);
yield* Cache.set(settingsCache, cacheKey, next);
yield* emitChange(next);
@@ -114,7 +114,7 @@ async function mountClaudePicker(props?: {
? ({
provider: "claudeAgent",
model,
options: props.fallbackModelOptions ?? undefined,
...(props.fallbackModelOptions ? { options: props.fallbackModelOptions } : {}),
} satisfies ModelSelection)
: null;
const screen = await render(
+83 -21
View File
@@ -11,18 +11,27 @@
*/
import { useCallback, useMemo } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ServerSettings, ServerSettingsPatch, ServerConfig } from "@t3tools/contracts";
import {
ServerSettings,
ServerSettingsPatch,
ServerConfig,
ModelSelection,
ThreadEnvMode,
} from "@t3tools/contracts";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import { serverConfigQueryOptions, serverQueryKeys } from "~/lib/serverReactQuery";
import { ensureNativeApi } from "~/nativeApi";
import { useLocalStorage } from "./useLocalStorage";
import { normalizeCustomModelSlugs } from "~/modelSelection";
import {
ClientSettingsSchema,
type ClientSettings,
DEFAULT_CLIENT_SETTINGS,
CLIENT_SETTINGS_STORAGE_KEY,
} from "~/clientSettings";
import { Struct } from "effect";
import { Predicate, Schema, Struct } from "effect";
import { DeepMutable } from "effect/Types";
import { deepMerge } from "@t3tools/shared/Struct";
// ── Unified type ─────────────────────────────────────────────────────
@@ -103,7 +112,7 @@ export function useUpdateSettings() {
if (!old) return old;
return {
...old,
settings: { ...old.settings, ...serverPatch },
settings: deepMerge(old.settings, serverPatch),
};
});
// Fire-and-forget RPC — push will reconcile on success
@@ -133,9 +142,63 @@ export function useUpdateSettings() {
// ── One-time migration from localStorage ─────────────────────────────
const MIGRATION_FLAG_KEY = "t3code:settings-migrated:v1";
const MIGRATION_FLAG_KEY = "t3code:settings-migrated";
const OLD_SETTINGS_KEY = "t3code:app-settings:v1";
export function buildLegacyServerSettingsMigrationPatch(legacySettings: Record<string, unknown>) {
const patch: DeepMutable<ServerSettingsPatch> = {};
if (Predicate.isBoolean(legacySettings.enableAssistantStreaming)) {
patch.enableAssistantStreaming = legacySettings.enableAssistantStreaming;
}
if (Schema.is(ThreadEnvMode)(legacySettings.defaultThreadEnvMode)) {
patch.defaultThreadEnvMode = legacySettings.defaultThreadEnvMode;
}
if (Schema.is(ModelSelection)(legacySettings.textGenerationModelSelection)) {
patch.textGenerationModelSelection = legacySettings.textGenerationModelSelection;
}
if (typeof legacySettings.codexBinaryPath === "string") {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.binaryPath = legacySettings.codexBinaryPath;
}
if (typeof legacySettings.codexHomePath === "string") {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.homePath = legacySettings.codexHomePath;
}
if (Array.isArray(legacySettings.customCodexModels)) {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.customModels = normalizeCustomModelSlugs(
legacySettings.customCodexModels,
"codex",
);
}
if (Predicate.isString(legacySettings.claudeBinaryPath)) {
patch.providers ??= {};
patch.providers.claudeAgent ??= {};
patch.providers.claudeAgent.binaryPath = legacySettings.claudeBinaryPath;
}
if (Array.isArray(legacySettings.customClaudeModels)) {
patch.providers ??= {};
patch.providers.claudeAgent ??= {};
patch.providers.claudeAgent.customModels = normalizeCustomModelSlugs(
legacySettings.customClaudeModels,
"claudeAgent",
);
}
return patch;
}
/**
* Call once on app startup. Migrates server-relevant settings from the
* old localStorage key to the server. Idempotent.
@@ -151,25 +214,24 @@ export function migrateLocalSettingsToServer(): void {
}
try {
const old = JSON.parse(raw) as Record<string, unknown>;
const serverPatch: Record<string, unknown> = {};
for (const key of SERVER_SETTINGS_KEYS) {
if (key in old && old[key] !== undefined) {
serverPatch[key] = old[key];
}
const old = JSON.parse(raw);
if (!Predicate.isObject(old)) return;
const api = ensureNativeApi();
const serverPatch = buildLegacyServerSettingsMigrationPatch(old);
if (Object.keys(serverPatch).length === 0) {
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
return;
}
if (Object.keys(serverPatch).length > 0) {
void ensureNativeApi()
.server.updateSettings(serverPatch as ServerSettingsPatch)
.then(() => {
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
});
} else {
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
}
} catch {
// If parsing fails, mark as migrated to avoid retrying
void api.server.updateSettings(serverPatch);
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
} catch (error) {
console.error("[MIGRATION] Error migrating local settings to server:", error);
} finally {
// Mark as migrated no matter what to avoid retrying
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
}
}
+2 -2
View File
@@ -47,14 +47,14 @@ export const DEFAULT_PROVIDER_KIND: ProviderKind = "codex";
export const CodexModelSelection = Schema.Struct({
provider: Schema.Literal("codex"),
model: TrimmedNonEmptyString,
options: Schema.optional(CodexModelOptions),
options: Schema.optionalKey(CodexModelOptions),
});
export type CodexModelSelection = typeof CodexModelSelection.Type;
export const ClaudeModelSelection = Schema.Struct({
provider: Schema.Literal("claudeAgent"),
model: TrimmedNonEmptyString,
options: Schema.optional(ClaudeModelOptions),
options: Schema.optionalKey(ClaudeModelOptions),
});
export type ClaudeModelSelection = typeof ClaudeModelSelection.Type;
+52 -3
View File
@@ -1,8 +1,12 @@
import { Schema, Struct } from "effect";
import { Schema } from "effect";
import { IsoDateTime, TrimmedNonEmptyString } from "./baseSchemas";
import { KeybindingRule, ResolvedKeybindingsConfig } from "./keybindings";
import { EditorId } from "./editor";
import { DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER } from "./model";
import {
ClaudeModelOptions,
CodexModelOptions,
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
} from "./model";
import { ModelSelection, ProviderKind } from "./orchestration";
// ── Server Settings (server-authoritative) ───────────────────────────
@@ -45,7 +49,52 @@ export type ServerSettings = typeof ServerSettings.Type;
export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeUnknownSync(ServerSettings)({});
export const ServerSettingsPatch = ServerSettings.mapFields(Struct.map(Schema.optionalKey));
const CodexModelOptionsPatch = Schema.Struct({
reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort),
fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode),
});
const ClaudeModelOptionsPatch = Schema.Struct({
thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking),
effort: Schema.optionalKey(ClaudeModelOptions.fields.effort),
fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode),
});
const ModelSelectionPatch = Schema.Union([
Schema.Struct({
provider: Schema.optionalKey(Schema.Literal("codex")),
model: Schema.optionalKey(TrimmedNonEmptyString),
options: Schema.optionalKey(CodexModelOptionsPatch),
}),
Schema.Struct({
provider: Schema.optionalKey(Schema.Literal("claudeAgent")),
model: Schema.optionalKey(TrimmedNonEmptyString),
options: Schema.optionalKey(ClaudeModelOptionsPatch),
}),
]);
const CodexSettingsPatch = Schema.Struct({
binaryPath: Schema.optionalKey(Schema.String),
homePath: Schema.optionalKey(Schema.String),
customModels: Schema.optionalKey(Schema.Array(Schema.String)),
});
const ClaudeSettingsPatch = Schema.Struct({
binaryPath: Schema.optionalKey(Schema.String),
customModels: Schema.optionalKey(Schema.Array(Schema.String)),
});
export const ServerSettingsPatch = Schema.Struct({
enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
providers: Schema.optionalKey(
Schema.Struct({
codex: Schema.optionalKey(CodexSettingsPatch),
claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
}),
),
});
export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;
const KeybindingsMalformedConfigIssue = Schema.Struct({
+4
View File
@@ -31,6 +31,10 @@
"./schemaJson": {
"types": "./src/schemaJson.ts",
"import": "./src/schemaJson.ts"
},
"./Struct": {
"types": "./src/Struct.ts",
"import": "./src/Struct.ts"
}
},
"scripts": {
+17
View File
@@ -0,0 +1,17 @@
import * as P from "effect/Predicate";
export function deepMerge<T>(current: T, patch: unknown): T {
if (!P.isObject(current) || !P.isObject(patch)) {
return patch as T;
}
const next = { ...current } as Record<string, unknown>;
for (const [key, value] of Object.entries(patch)) {
if (value === undefined) continue;
const existing = next[key];
next[key] = P.isObject(existing) && P.isObject(value) ? deepMerge(existing, value) : value;
}
return next as T;
}