Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

environment-cache-store.ts

232 lines8.9 KB

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