The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Ref from "effect/Ref"; |
| 8 | |
| 9 | import * as ElectronProtocol from "../electron/ElectronProtocol.ts"; |
| 10 | import { readChromiumLocalStorage } from "./chromiumLocalStorage.ts"; |
| 11 | import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 12 | |
| 13 | /** Reads the V1 profile without opening it in Chromium, even while V1 runs. */ |
| 14 | export class DesktopLegacyLocalStorage extends Context.Service< |
| 15 | DesktopLegacyLocalStorage, |
| 16 | { |
| 17 | readonly load: (userDataPath: string) => Effect.Effect<void>; |
| 18 | readonly take: Effect.Effect<Option.Option<Readonly<Record<string, string>>>>; |
| 19 | readonly complete: Effect.Effect<void>; |
| 20 | } |
| 21 | >()("@t3tools/desktop/app/DesktopLegacyLocalStorage") {} |
| 22 | |
| 23 | const MARKER_FILE_NAME = "v1-local-storage-imported"; |
| 24 | const V1_PROFILE_NAMES = ["T3 Code (Alpha)", "t3code"]; |
| 25 | |
| 26 | const make = Effect.gen(function* () { |
| 27 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 28 | const fs = yield* FileSystem.FileSystem; |
| 29 | const path = yield* Path.Path; |
| 30 | const items = yield* Ref.make(Option.none<Readonly<Record<string, string>>>()); |
| 31 | const markerPath = yield* Ref.make(Option.none<string>()); |
| 32 | |
| 33 | /** Newest file mtime in a directory; appending to a log leaves the directory's own mtime alone. */ |
| 34 | const newestFileMtime = Effect.fn("DesktopLegacyLocalStorage.newestFileMtime")( |
| 35 | function* (directory: string) { |
| 36 | let newest = 0; |
| 37 | for (const name of yield* fs.readDirectory(directory)) { |
| 38 | const info = yield* fs.stat(path.join(directory, name)).pipe(Effect.option); |
| 39 | if (Option.isNone(info)) continue; |
| 40 | const mtime = Option.match(info.value.mtime, { |
| 41 | onNone: () => 0, |
| 42 | onSome: (date) => date.getTime(), |
| 43 | }); |
| 44 | newest = Math.max(newest, mtime); |
| 45 | } |
| 46 | return newest; |
| 47 | }, |
| 48 | (effect, _directory) => effect.pipe(Effect.option), |
| 49 | ); |
| 50 | |
| 51 | const findV1LocalStorage = Effect.gen(function* () { |
| 52 | let newest: { readonly directory: string; readonly mtime: number } | null = null; |
| 53 | for (const name of V1_PROFILE_NAMES) { |
| 54 | const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb"); |
| 55 | const mtime = yield* newestFileMtime(directory); |
| 56 | if (Option.isNone(mtime)) continue; |
| 57 | if (newest === null || mtime.value > newest.mtime) newest = { directory, mtime: mtime.value }; |
| 58 | } |
| 59 | return newest?.directory ?? null; |
| 60 | }); |
| 61 | |
| 62 | const writeMarker = Effect.gen(function* () { |
| 63 | const marker = yield* Ref.get(markerPath); |
| 64 | if (Option.isNone(marker)) return; |
| 65 | yield* fs |
| 66 | .writeFileString(marker.value, "") |
| 67 | .pipe( |
| 68 | Effect.catch((error) => |
| 69 | Effect.logWarning("Could not record the V1 Local Storage import", error), |
| 70 | ), |
| 71 | ); |
| 72 | }); |
| 73 | |
| 74 | const load = Effect.fn("desktop.legacyLocalStorage.load")(function* (userDataPath: string) { |
| 75 | if (environment.isDevelopment) return; |
| 76 | const marker = path.join(userDataPath, MARKER_FILE_NAME); |
| 77 | if (yield* fs.exists(marker).pipe(Effect.orElseSucceed(() => true))) return; |
| 78 | yield* Ref.set(markerPath, Option.some(marker)); |
| 79 | const directory = yield* findV1LocalStorage; |
| 80 | if (directory === null) return yield* writeMarker; |
| 81 | const origin = ElectronProtocol.getDesktopUrl(false).replace(/\/$/, ""); |
| 82 | const read = yield* readChromiumLocalStorage(directory, origin).pipe( |
| 83 | Effect.provideService(FileSystem.FileSystem, fs), |
| 84 | Effect.provideService(Path.Path, path), |
| 85 | Effect.tapError((error) => |
| 86 | Effect.logWarning("Could not read V1 Local Storage; will retry next launch", error), |
| 87 | ), |
| 88 | Effect.option, |
| 89 | ); |
| 90 | if (Option.isNone(read)) return; |
| 91 | yield* Effect.logInfo("V1 Local Storage ready to import", { |
| 92 | directory, |
| 93 | keys: read.value.size, |
| 94 | }); |
| 95 | yield* Ref.set(items, Option.some(Object.fromEntries(read.value))); |
| 96 | }); |
| 97 | |
| 98 | return DesktopLegacyLocalStorage.of({ |
| 99 | load, |
| 100 | take: Ref.getAndSet(items, Option.none()), |
| 101 | complete: writeMarker, |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | export const layer = Layer.effect(DesktopLegacyLocalStorage, make); |