mv to contract package

This commit is contained in:
Julius Marminge committed 2026-03-25 17:12:19 -07:00
1 parent c91bd26b3c
commit 619fd2027c
12 files changed
+216 -165

No files matched your search

-50
View File
@@ -1,50 +0,0 @@
/**
* Client-only settings - persisted in localStorage.
*
* These settings affect only the frontend UX and never need to be read by
* the server. Server-authoritative settings live in `ServerSettings`
* (defined in `@t3tools/contracts/server`).
*/
import { Option, Schema } from "effect";
export const CLIENT_SETTINGS_STORAGE_KEY = "t3code:client-settings:v1";
export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
export type TimestampFormat = typeof TimestampFormat.Type;
export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
const withDefaults =
<
S extends Schema.Top & Schema.WithoutConstructorDefault,
D extends S["~type.make.in"] & S["Encoded"],
>(
fallback: () => D,
) =>
(schema: S) =>
schema.pipe(
Schema.withConstructorDefault(() => Option.some(fallback())),
Schema.withDecodingDefault(() => fallback()),
);
export const ClientSettingsSchema = Schema.Struct({
confirmThreadDelete: Schema.Boolean.pipe(withDefaults(() => true)),
diffWordWrap: Schema.Boolean.pipe(withDefaults(() => false)),
sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
withDefaults(() => DEFAULT_SIDEBAR_PROJECT_SORT_ORDER),
),
sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
withDefaults(() => DEFAULT_SIDEBAR_THREAD_SORT_ORDER),
),
timestampFormat: TimestampFormat.pipe(withDefaults(() => DEFAULT_TIMESTAMP_FORMAT)),
});
export type ClientSettings = typeof ClientSettingsSchema.Type;
export const DEFAULT_CLIENT_SETTINGS: ClientSettings = ClientSettingsSchema.makeUnsafe({});
+1 -1
View File
@@ -31,7 +31,7 @@ import { isMacPlatform } from "../lib/utils";
import { getRouter } from "../router";
import { useStore } from "../store";
import { estimateTimelineMessageHeight } from "./timelineHeight";
import { DEFAULT_CLIENT_SETTINGS } from "~/clientSettings";
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
const THREAD_ID = "thread-browser-test" as ThreadId;
const UUID_ROUTE_RE = /^\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
+1 -1
View File
@@ -1,5 +1,5 @@
import { memo, useState, useCallback } from "react";
import { type TimestampFormat } from "../clientSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
import { Badge } from "./ui/badge";
import { Button } from "./ui/button";
import { ScrollArea } from "./ui/scroll-area";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "../clientSettings";
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
import type { Thread } from "../types";
import { cn } from "../lib/utils";
import {
+4 -1
View File
@@ -38,7 +38,10 @@ import {
} from "@t3tools/contracts";
import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query";
import { useLocation, useNavigate, useParams } from "@tanstack/react-router";
import { type SidebarProjectSortOrder, type SidebarThreadSortOrder } from "../clientSettings";
import {
type SidebarProjectSortOrder,
type SidebarThreadSortOrder,
} from "@t3tools/contracts/settings";
import { isElectron } from "../env";
import { APP_STAGE_LABEL, APP_VERSION } from "../branding";
import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils";
@@ -48,7 +48,7 @@ import {
type ParsedTerminalContextEntry,
} from "~/lib/terminalContext";
import { cn } from "~/lib/utils";
import { type TimestampFormat } from "../../clientSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
import { formatTimestamp } from "../../timestampFormat";
import {
buildInlineTerminalContextText,
@@ -22,7 +22,7 @@ import {
useComposerThreadDraft,
useEffectiveComposerModelState,
} from "../../composerDraftStore";
import { DEFAULT_CLIENT_SETTINGS } from "~/clientSettings";
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
// ── Claude TraitsPicker tests ─────────────────────────────────────────
+56 -16
View File
@@ -19,16 +19,19 @@ import {
ThreadEnvMode,
} from "@t3tools/contracts";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import {
type ClientSettings,
ClientSettingsSchema,
CLIENT_SETTINGS_STORAGE_KEY,
DEFAULT_CLIENT_SETTINGS,
SidebarProjectSortOrder,
SidebarThreadSortOrder,
TimestampFormat,
} from "@t3tools/contracts/settings";
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 { Predicate, Schema, Struct } from "effect";
import { DeepMutable } from "effect/Types";
import { deepMerge } from "@t3tools/shared/Struct";
@@ -199,9 +202,38 @@ export function buildLegacyServerSettingsMigrationPatch(legacySettings: Record<s
return patch;
}
export function buildLegacyClientSettingsMigrationPatch(
legacySettings: Record<string, unknown>,
): Partial<DeepMutable<ClientSettings>> {
const patch: Partial<DeepMutable<ClientSettings>> = {};
if (Predicate.isBoolean(legacySettings.confirmThreadDelete)) {
patch.confirmThreadDelete = legacySettings.confirmThreadDelete;
}
if (Predicate.isBoolean(legacySettings.diffWordWrap)) {
patch.diffWordWrap = legacySettings.diffWordWrap;
}
if (Schema.is(SidebarProjectSortOrder)(legacySettings.sidebarProjectSortOrder)) {
patch.sidebarProjectSortOrder = legacySettings.sidebarProjectSortOrder;
}
if (Schema.is(SidebarThreadSortOrder)(legacySettings.sidebarThreadSortOrder)) {
patch.sidebarThreadSortOrder = legacySettings.sidebarThreadSortOrder;
}
if (Schema.is(TimestampFormat)(legacySettings.timestampFormat)) {
patch.timestampFormat = legacySettings.timestampFormat;
}
return patch;
}
/**
* Call once on app startup. Migrates server-relevant settings from the
* old localStorage key to the server. Idempotent.
* Call once on app startup. Migrates settings from the old unified
* localStorage key to the server (server-relevant keys) and to the new
* client-settings localStorage key (client-only keys). Idempotent.
*/
export function migrateLocalSettingsToServer(): void {
if (typeof window === "undefined") return;
@@ -217,19 +249,27 @@ export function migrateLocalSettingsToServer(): void {
const old = JSON.parse(raw);
if (!Predicate.isObject(old)) return;
const api = ensureNativeApi();
// Migrate server-relevant keys via RPC
const serverPatch = buildLegacyServerSettingsMigrationPatch(old);
if (Object.keys(serverPatch).length === 0) {
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
return;
if (Object.keys(serverPatch).length > 0) {
const api = ensureNativeApi();
void api.server.updateSettings(serverPatch);
}
// Migrate client-only keys to the new localStorage key
const clientPatch = buildLegacyClientSettingsMigrationPatch(old);
if (Object.keys(clientPatch).length > 0) {
const existing = localStorage.getItem(CLIENT_SETTINGS_STORAGE_KEY);
const current = existing ? (JSON.parse(existing) as Record<string, unknown>) : {};
localStorage.setItem(
CLIENT_SETTINGS_STORAGE_KEY,
JSON.stringify({ ...current, ...clientPatch }),
);
}
void api.server.updateSettings(serverPatch);
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
} catch (error) {
console.error("[MIGRATION] Error migrating local settings to server:", error);
console.error("[MIGRATION] Error migrating local settings:", error);
} finally {
// Mark as migrated no matter what to avoid retrying
localStorage.setItem(MIGRATION_FLAG_KEY, "true");
+1 -1
View File
@@ -1,4 +1,4 @@
import { type TimestampFormat } from "./clientSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
export function getTimestampFormatOptions(
timestampFormat: TimestampFormat,