The best way to control Claude Code, Codex and more from any computer or phone

main
ts 132 lines 5.4 KB
Raw
1 import {
2 ConnectionCatalogDocument,
3 type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
4 EMPTY_CONNECTION_CATALOG_DOCUMENT,
5 } from "@t3tools/client-runtime/platform";
6 import { ConnectionTransientError } from "@t3tools/client-runtime/connection";
7 import * as Effect from "effect/Effect";
8 import { msg } from "gt-react-native";
9 import * as Option from "effect/Option";
10 import * as Ref from "effect/Ref";
11 import * as Schema from "effect/Schema";
12 import * as Semaphore from "effect/Semaphore";
13
14 import { m } from "../i18n/messages";
15 import * as MobileSecureStorage from "../persistence/mobile-secure-storage";
16 import { migrateLegacyConnectionCatalog } from "./migration";
17
18 export const CONNECTION_CATALOG_KEY = "t3code.connection-catalog.v1";
19 export const LEGACY_CONNECTIONS_KEY = "t3code.connections";
20
21 const CATALOG_ERROR_MESSAGES = {
22 decode: msg("Could not decode the local connection catalog: {cause}"),
23 encode: msg("Could not encode the local connection catalog: {cause}"),
24 load: msg("Could not load the local connection catalog: {cause}"),
25 save: msg("Could not save the local connection catalog: {cause}"),
26 delete: msg("Could not delete the local connection catalog: {cause}"),
27 migrate: msg("Could not migrate the local connection catalog: {cause}"),
28 };
29
30 function catalogError(operation: keyof typeof CATALOG_ERROR_MESSAGES, cause: unknown) {
31 return new ConnectionTransientError({
32 reason: "remote-unavailable",
33 detail: m(CATALOG_ERROR_MESSAGES[operation], { cause: String(cause) }),
34 });
35 }
36
37 const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDocument);
38 const decodeConnectionCatalogDocument = Schema.decodeEffect(ConnectionCatalogDocumentJson);
39 const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
40
41 const decodeCatalog = Effect.fn("mobile.connectionStorage.decodeCatalog")(function* (raw: string) {
42 return yield* decodeConnectionCatalogDocument(raw).pipe(
43 Effect.mapError((cause) => catalogError("decode", cause)),
44 );
45 });
46
47 const encodeCatalog = Effect.fn("mobile.connectionStorage.encodeCatalog")(function* (
48 catalog: ConnectionCatalogDocumentType,
49 ) {
50 return yield* encodeConnectionCatalogDocument(catalog).pipe(
51 Effect.mapError((cause) => catalogError("encode", cause)),
52 );
53 });
54
55 interface CatalogStore {
56 readonly read: Effect.Effect<ConnectionCatalogDocumentType, ConnectionTransientError>;
57 readonly update: (
58 transform: (catalog: ConnectionCatalogDocumentType) => ConnectionCatalogDocumentType,
59 ) => Effect.Effect<void, ConnectionTransientError>;
60 }
61
62 export const make = Effect.fn("mobile.connectionStorage.makeCatalogStore")(function* () {
63 const storage = yield* MobileSecureStorage.MobileSecureStorage;
64 const getItem = (key: string) =>
65 storage.getItem(key).pipe(Effect.mapError((cause) => catalogError("load", cause)));
66 const setItem = (key: string, value: string) =>
67 storage.setItem(key, value).pipe(Effect.mapError((cause) => catalogError("save", cause)));
68 const deleteItem = (key: string) =>
69 storage.removeItem(key).pipe(Effect.mapError((cause) => catalogError("delete", cause)));
70 const state = yield* Ref.make<Option.Option<ConnectionCatalogDocumentType>>(Option.none());
71 const lock = yield* Semaphore.make(1);
72
73 const loadLegacyCatalog = Effect.fn("mobile.connectionStorage.loadLegacyCatalog")(function* () {
74 const legacyRaw = yield* getItem(LEGACY_CONNECTIONS_KEY);
75 const catalog =
76 legacyRaw === null || legacyRaw.trim() === ""
77 ? EMPTY_CONNECTION_CATALOG_DOCUMENT
78 : yield* migrateLegacyConnectionCatalog(legacyRaw).pipe(
79 Effect.mapError((cause) => catalogError("migrate", cause)),
80 Effect.catch((error) =>
81 Effect.logWarning("Discarding corrupt legacy mobile connections", error).pipe(
82 Effect.as(EMPTY_CONNECTION_CATALOG_DOCUMENT),
83 ),
84 ),
85 );
86 if (legacyRaw !== null && legacyRaw.trim() !== "") {
87 const encoded = yield* encodeCatalog(catalog);
88 yield* setItem(CONNECTION_CATALOG_KEY, encoded);
89 yield* deleteItem(LEGACY_CONNECTIONS_KEY);
90 }
91 return catalog;
92 });
93
94 const loadUnlocked = Effect.fn("mobile.connectionStorage.loadCatalog")(function* () {
95 const cached = yield* Ref.get(state);
96 if (Option.isSome(cached)) {
97 return cached.value;
98 }
99 const raw = yield* getItem(CONNECTION_CATALOG_KEY);
100 let catalog: ConnectionCatalogDocumentType;
101 if (raw !== null && raw.trim() !== "") {
102 catalog = yield* decodeCatalog(raw).pipe(
103 Effect.catch((error) =>
104 Effect.logWarning("Discarding corrupt mobile connection catalog", error).pipe(
105 Effect.andThen(deleteItem(CONNECTION_CATALOG_KEY)),
106 Effect.andThen(loadLegacyCatalog()),
107 ),
108 ),
109 );
110 } else {
111 catalog = yield* loadLegacyCatalog();
112 }
113 yield* Ref.set(state, Option.some(catalog));
114 return catalog;
115 });
116
117 const read = lock.withPermits(1)(loadUnlocked());
118 const update: CatalogStore["update"] = Effect.fn("mobile.connectionStorage.updateCatalog")(
119 function* (transform) {
120 yield* lock.withPermits(1)(
121 Effect.gen(function* () {
122 const next = transform(yield* loadUnlocked());
123 const encoded = yield* encodeCatalog(next);
124 yield* setItem(CONNECTION_CATALOG_KEY, encoded);
125 yield* Ref.set(state, Option.some(next));
126 }),
127 );
128 },
129 );
130
131 return { read, update } satisfies CatalogStore;
132 });