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