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