diff --git a/README.md b/README.md index 0e79171f55..c5ca2a511e 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,12 @@ Optional diagnostics: - `T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK=1` - In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.catchUp` responses and logs drift diagnostics. +Web client state-source selection: + +- `VITE_T3CODE_STATE_SOURCE_MODE=legacy-api|livestore-read-pilot` + - Selects the web state-source adapter mode. + - Current behavior is protocol-compatible in both modes (delegates to `api.state.*`) while preserving a client-side seam for future LiveStore-native source wiring. + ## Provider architecture The web app communicates with the server via WebSocket using a simple JSON-RPC-style protocol: diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index 2e62415ac8..0755543873 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -13,7 +13,7 @@ import { AnchoredToastProvider, ToastProvider } from "../components/ui/toast"; import { isElectron } from "../env"; import { useNativeApi } from "../hooks/useNativeApi"; import { invalidateGitQueries } from "../lib/gitReactQuery"; -import { createStateSource } from "../stateSource"; +import { createStateSource, resolveStateSourceMode } from "../stateSource"; import { useStore } from "../store"; import { onServerWelcome } from "../wsNativeApi"; @@ -130,9 +130,19 @@ function StateSyncRouter() { strict: false, select: (params) => params.threadId, }); + const stateSourceMode = useMemo( + () => + resolveStateSourceMode( + (import.meta.env.VITE_T3CODE_STATE_SOURCE_MODE as string | undefined) ?? undefined, + ), + [], + ); const lastStateSeqRef = useRef(0); const stateQueueRef = useRef(Promise.resolve()); - const stateSource = useMemo(() => (api ? createStateSource(api) : null), [api]); + const stateSource = useMemo( + () => (api ? createStateSource(api, { mode: stateSourceMode }) : null), + [api, stateSourceMode], + ); useEffect(() => { if (!api || !stateSource) return; diff --git a/apps/web/src/stateSource.test.ts b/apps/web/src/stateSource.test.ts index 186c9f2c93..6b9c5c3bc4 100644 --- a/apps/web/src/stateSource.test.ts +++ b/apps/web/src/stateSource.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import type { NativeApi, StateEvent } from "@t3tools/contracts"; -import { createStateSource } from "./stateSource"; +import { createStateSource, resolveStateSourceMode } from "./stateSource"; function makeApi(): NativeApi { const onStateEvent = vi.fn<(callback: (event: StateEvent) => void) => () => void>((callback) => { @@ -164,6 +164,7 @@ describe("createStateSource", () => { it("delegates bootstrap/catchUp/onEvent to api.state", async () => { const api = makeApi(); const source = createStateSource(api); + expect(source.mode).toBe("legacy-api"); await expect(source.bootstrap()).resolves.toEqual({ projects: [], @@ -194,4 +195,23 @@ describe("createStateSource", () => { expect(api.state.catchUp).toHaveBeenCalledWith({ afterSeq: 1 }); expect(api.state.onEvent).toHaveBeenCalledTimes(1); }); + + it("supports explicit read-pilot mode selection", async () => { + const api = makeApi(); + const source = createStateSource(api, { mode: "livestore-read-pilot" }); + expect(source.mode).toBe("livestore-read-pilot"); + await source.bootstrap(); + expect(api.state.bootstrap).toHaveBeenCalledTimes(1); + }); +}); + +describe("resolveStateSourceMode", () => { + it("defaults to legacy-api for missing/unknown values", () => { + expect(resolveStateSourceMode(undefined)).toBe("legacy-api"); + expect(resolveStateSourceMode("unknown")).toBe("legacy-api"); + }); + + it("accepts livestore-read-pilot value", () => { + expect(resolveStateSourceMode("livestore-read-pilot")).toBe("livestore-read-pilot"); + }); }); diff --git a/apps/web/src/stateSource.ts b/apps/web/src/stateSource.ts index ed541d74a6..b055d92ea0 100644 --- a/apps/web/src/stateSource.ts +++ b/apps/web/src/stateSource.ts @@ -6,14 +6,31 @@ import type { StateEvent, } from "@t3tools/contracts"; +export type StateSourceMode = "legacy-api" | "livestore-read-pilot"; + export interface StateSource { + mode: StateSourceMode; bootstrap(): Promise; catchUp(input: StateCatchUpInput): Promise; onEvent(listener: (event: StateEvent) => void): () => void; } -export function createStateSource(api: NativeApi): StateSource { +export function resolveStateSourceMode(rawMode: string | undefined): StateSourceMode { + if (rawMode === "livestore-read-pilot") { + return "livestore-read-pilot"; + } + return "legacy-api"; +} + +export function createStateSource( + api: NativeApi, + options: { + mode?: StateSourceMode; + } = {}, +): StateSource { + const mode = options.mode ?? "legacy-api"; return { + mode, bootstrap: () => api.state.bootstrap(), catchUp: (input) => api.state.catchUp(input), onEvent: (listener) => api.state.onEvent(listener),