Open History to see the last commit
1import {
2 ConnectionPersistenceError,
3 EnvironmentCacheStore,
4 ORCHESTRATION_CACHE_SCHEMA_VERSION,
5 StoredOrchestrationShellSnapshot,
6 StoredOrchestrationThreadSnapshot,
7} from "@t3tools/client-runtime/platform";
8import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Schema from "effect/Schema";
13
14import * as MobileDatabase from "../persistence/mobile-database";
15import { encodeStoredShellSnapshot } from "./shell-cache-encoding";
16import {
17 attachProjectFaviconDatabase,
18 projectFaviconDatabaseCache,
19} from "../lib/projectFaviconDatabaseCache";
20
21const SERVER_CONFIG_CACHE_SCHEMA_VERSION = 1;
22const VCS_REFS_CACHE_SCHEMA_VERSION = 1;
23
24const StoredServerConfig = Schema.Struct({
25 schemaVersion: Schema.Literal(SERVER_CONFIG_CACHE_SCHEMA_VERSION),
26 environmentId: Schema.String,
27 config: ServerConfig,
28});
29const StoredVcsRefs = Schema.Struct({
30 schemaVersion: Schema.Literal(VCS_REFS_CACHE_SCHEMA_VERSION),
31 environmentId: Schema.String,
32 cwd: Schema.String,
33 refs: VcsListRefsResult,
34});
35
36const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(
37 Schema.fromJsonString(StoredOrchestrationShellSnapshot),
38);
39const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(
40 Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
41);
42const encodeStoredThreadSnapshot = Schema.encodeEffect(
43 Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
44);
45const decodeStoredServerConfig = Schema.decodeUnknownEffect(
46 Schema.fromJsonString(StoredServerConfig),
47);
48const encodeStoredServerConfig = Schema.encodeEffect(Schema.fromJsonString(StoredServerConfig));
49const decodeStoredVcsRefs = Schema.decodeUnknownEffect(Schema.fromJsonString(StoredVcsRefs));
50const encodeStoredVcsRefs = Schema.encodeEffect(Schema.fromJsonString(StoredVcsRefs));
51
52type CacheOperation = ConnectionPersistenceError["operation"];
53
54function persistenceError(operation: CacheOperation, cause: unknown) {
55 return new ConnectionPersistenceError({
56 operation,
57 message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
58 });
59}
60
61function mapDatabaseError(operation: CacheOperation) {
62 return (error: MobileDatabase.MobileDatabaseError) => persistenceError(operation, error);
63}
64
65function loadDecodedCache<A, B>(input: {
66 readonly database: MobileDatabase.MobileDatabase["Service"];
67 readonly environmentId: EnvironmentId;
68 readonly kind: MobileDatabase.ClientCacheKind;
69 readonly cacheKey: string;
70 readonly operation: CacheOperation;
71 readonly decode: (raw: string) => Effect.Effect<A, unknown>;
72 readonly select: (value: A) => Option.Option<B>;
73}): Effect.Effect<Option.Option<B>, ConnectionPersistenceError> {
74 return input.database.loadCache(input.environmentId, input.kind, input.cacheKey).pipe(
75 Effect.mapError(mapDatabaseError(input.operation)),
76 Effect.flatMap(
77 Option.match({
78 onNone: () => Effect.succeed(Option.none<B>()),
79 onSome: (raw) =>
80 input.decode(raw).pipe(
81 Effect.map(input.select),
82 Effect.catch((cause) =>
83 Effect.logWarning("Discarding corrupt mobile client cache record.", {
84 environmentId: input.environmentId,
85 kind: input.kind,
86 cacheKey: input.cacheKey,
87 cause: String(cause),
88 }).pipe(
89 Effect.andThen(
90 input.database
91 .removeCache(input.environmentId, input.kind, input.cacheKey)
92 .pipe(Effect.catch(() => Effect.void)),
93 ),
94 Effect.as(Option.none<B>()),
95 ),
96 ),
97 ),
98 }),
99 ),
100 );
101}
102
103export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
104 const database = yield* MobileDatabase.MobileDatabase;
105 attachProjectFaviconDatabase(database);
106 return EnvironmentCacheStore.of({
107 loadShell: Effect.fn("MobileEnvironmentCache.loadShell")((environmentId) =>
108 loadDecodedCache({
109 database,
110 environmentId,
111 kind: "shell",
112 cacheKey: "snapshot",
113 operation: "load-shell",
114 decode: decodeStoredShellSnapshot,
115 select: (stored) =>
116 stored.environmentId === environmentId ? Option.some(stored.snapshot) : Option.none(),
117 }).pipe(Effect.tap(() => Effect.promise(() => projectFaviconDatabaseCache.hydrate()))),
118 ),
119 saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) {
120 const payload = yield* encodeStoredShellSnapshot({
121 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
122 environmentId,
123 snapshot,
124 }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
125 yield* database
126 .saveCache(environmentId, "shell", "snapshot", ORCHESTRATION_CACHE_SCHEMA_VERSION, payload)
127 .pipe(Effect.mapError(mapDatabaseError("save-shell")));
128 }),
129 loadThread: Effect.fn("MobileEnvironmentCache.loadThread")((environmentId, threadId) =>
130 loadDecodedCache({
131 database,
132 environmentId,
133 kind: "thread",
134 cacheKey: threadId,
135 operation: "load-thread",
136 decode: decodeStoredThreadSnapshot,
137 select: (stored) =>
138 stored.environmentId === environmentId && stored.threadId === threadId
139 ? Option.some(stored.snapshot)
140 : Option.none(),
141 }),
142 ),
143 saveThread: Effect.fn("MobileEnvironmentCache.saveThread")(function* (environmentId, snapshot) {
144 const threadId = snapshot.projection.thread.id;
145 const payload = yield* encodeStoredThreadSnapshot({
146 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
147 environmentId,
148 threadId,
149 snapshot,
150 }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
151 yield* database
152 .saveCache(environmentId, "thread", threadId, ORCHESTRATION_CACHE_SCHEMA_VERSION, payload)
153 .pipe(Effect.mapError(mapDatabaseError("save-thread")));
154 }),
155 removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) =>
156 database
157 .removeCache(environmentId, "thread", threadId)
158 .pipe(Effect.mapError(mapDatabaseError("remove-thread"))),
159 ),
160 loadServerConfig: Effect.fn("MobileEnvironmentCache.loadServerConfig")((environmentId) =>
161 loadDecodedCache({
162 database,
163 environmentId,
164 kind: "server-config",
165 cacheKey: "config",
166 operation: "load-server-config",
167 decode: decodeStoredServerConfig,
168 select: (stored) =>
169 stored.environmentId === environmentId ? Option.some(stored.config) : Option.none(),
170 }),
171 ),
172 saveServerConfig: Effect.fn("MobileEnvironmentCache.saveServerConfig")(
173 function* (environmentId, config) {
174 const payload = yield* encodeStoredServerConfig({
175 schemaVersion: SERVER_CONFIG_CACHE_SCHEMA_VERSION,
176 environmentId,
177 config,
178 }).pipe(Effect.mapError((cause) => persistenceError("save-server-config", cause)));
179 yield* database
180 .saveCache(
181 environmentId,
182 "server-config",
183 "config",
184 SERVER_CONFIG_CACHE_SCHEMA_VERSION,
185 payload,
186 )
187 .pipe(Effect.mapError(mapDatabaseError("save-server-config")));
188 },
189 ),
190 loadVcsRefs: Effect.fn("MobileEnvironmentCache.loadVcsRefs")((environmentId, cwd) =>
191 loadDecodedCache({
192 database,
193 environmentId,
194 kind: "vcs-refs",
195 cacheKey: cwd,
196 operation: "load-vcs-refs",
197 decode: decodeStoredVcsRefs,
198 select: (stored) =>
199 stored.environmentId === environmentId && stored.cwd === cwd
200 ? Option.some(stored.refs)
201 : Option.none(),
202 }),
203 ),
204 saveVcsRefs: Effect.fn("MobileEnvironmentCache.saveVcsRefs")(
205 function* (environmentId, cwd, refs) {
206 const payload = yield* encodeStoredVcsRefs({
207 schemaVersion: VCS_REFS_CACHE_SCHEMA_VERSION,
208 environmentId,
209 cwd,
210 refs,
211 }).pipe(Effect.mapError((cause) => persistenceError("save-vcs-refs", cause)));
212 yield* database
213 .saveCache(environmentId, "vcs-refs", cwd, VCS_REFS_CACHE_SCHEMA_VERSION, payload)
214 .pipe(Effect.mapError(mapDatabaseError("save-vcs-refs")));
215 },
216 ),
217 removeVcsRefs: Effect.fn("MobileEnvironmentCache.removeVcsRefs")((environmentId, cwd) =>
218 database
219 .removeCache(environmentId, "vcs-refs", cwd)
220 .pipe(Effect.mapError(mapDatabaseError("remove-vcs-refs"))),
221 ),
222 clearVcsRefs: Effect.fn("MobileEnvironmentCache.clearVcsRefs")((environmentId) =>
223 database
224 .clearCacheKind(environmentId, "vcs-refs")
225 .pipe(Effect.mapError(mapDatabaseError("clear-vcs-refs"))),
226 ),
227 clear: Effect.fn("MobileEnvironmentCache.clear")((environmentId) =>
228 Effect.promise(() => projectFaviconDatabaseCache.clearEnvironment(environmentId)).pipe(
229 Effect.andThen(database.clearEnvironmentCache(environmentId)),
230 Effect.mapError(mapDatabaseError("clear-environment")),
231 ),
232 ),
233 });
234});
235
236export const layer = Layer.effect(EnvironmentCacheStore, make());