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

persistence.ts

155 lines5.7 KB

1import {
2 type EnvironmentId,
3 OrchestrationProjectShell,
4 type OrchestrationShellSnapshot,
5 type OrchestrationThreadDetailSnapshot,
6 type ServerConfig,
7 type ThreadId,
8 type VcsListRefsResult,
9} from "@t3tools/contracts";
10import * as Context from "effect/Context";
11import * as Effect from "effect/Effect";
12import * as Option from "effect/Option";
13import * as Schema from "effect/Schema";
14​
15import type { ConnectionRegistration } from "../connection/catalog.ts";
16import type { ConnectionTarget } from "../connection/model.ts";
17​
18export class ConnectionPersistenceError extends Schema.TaggedError<ConnectionPersistenceError>()(
19 "ConnectionPersistenceError",
20 {
21 operation: Schema.Literals([
22 "list-targets",
23 "list-disabled-targets",
24 "register-connection",
25 "remove-connection",
26 "set-connection-enabled",
27 "load-shell",
28 "save-shell",
29 "load-thread",
30 "save-thread",
31 "remove-thread",
32 "load-server-config",
33 "save-server-config",
34 "load-vcs-refs",
35 "save-vcs-refs",
36 "remove-vcs-refs",
37 "clear-vcs-refs",
38 "clear-environment",
39 ]),
40 message: Schema.String,
41 },
42) {}
43​
44export class ConnectionTargetStore extends Context.Service<
45 ConnectionTargetStore,
46 {
47 readonly list: Effect.Effect<ReadonlyArray<ConnectionTarget>, ConnectionPersistenceError>;
48 /** Saved environments the user switched off. See `ConnectionRegistrationStore.setEnabled`. */
49 readonly listDisabled: Effect.Effect<ReadonlyArray<EnvironmentId>, ConnectionPersistenceError>;
50 }
51>()("@t3tools/client-runtime/platform/persistence/ConnectionTargetStore") {}
52​
53export class ConnectionRegistrationStore extends Context.Service<
54 ConnectionRegistrationStore,
55 {
56 readonly register: (
57 registration: ConnectionRegistration,
58 ) => Effect.Effect<void, ConnectionPersistenceError>;
59 readonly remove: (target: ConnectionTarget) => Effect.Effect<void, ConnectionPersistenceError>;
60 readonly setEnabled: (
61 environmentId: EnvironmentId,
62 enabled: boolean,
63 ) => Effect.Effect<void, ConnectionPersistenceError>;
64 }
65>()("@t3tools/client-runtime/platform/persistence/ConnectionRegistrationStore") {}
66​
67export class EnvironmentCacheStore extends Context.Service<
68 EnvironmentCacheStore,
69 {
70 readonly loadShell: (
71 environmentId: EnvironmentId,
72 ) => Effect.Effect<Option.Option<OrchestrationShellSnapshot>, ConnectionPersistenceError>;
73 readonly saveShell: (
74 environmentId: EnvironmentId,
75 snapshot: OrchestrationShellSnapshot,
76 ) => Effect.Effect<void, ConnectionPersistenceError>;
77 readonly loadThread: (
78 environmentId: EnvironmentId,
79 threadId: ThreadId,
80 ) => Effect.Effect<
81 Option.Option<OrchestrationThreadDetailSnapshot>,
82 ConnectionPersistenceError
83 >;
84 readonly saveThread: (
85 environmentId: EnvironmentId,
86 snapshot: OrchestrationThreadDetailSnapshot,
87 ) => Effect.Effect<void, ConnectionPersistenceError>;
88 readonly removeThread: (
89 environmentId: EnvironmentId,
90 threadId: ThreadId,
91 ) => Effect.Effect<void, ConnectionPersistenceError>;
92 /**
93 * The last complete server configuration. This deliberately includes provider
94 * metadata so offline task creation can still offer the models a user last saw.
95 */
96 readonly loadServerConfig: (
97 environmentId: EnvironmentId,
98 ) => Effect.Effect<Option.Option<ServerConfig>, ConnectionPersistenceError>;
99 readonly saveServerConfig: (
100 environmentId: EnvironmentId,
101 config: ServerConfig,
102 ) => Effect.Effect<void, ConnectionPersistenceError>;
103 /**
104 * The unfiltered branch list for a workspace. Query-specific lists are not
105 * cached because they are incomplete and unsafe to present as a full picker.
106 */
107 readonly loadVcsRefs: (
108 environmentId: EnvironmentId,
109 cwd: string,
110 ) => Effect.Effect<Option.Option<VcsListRefsResult>, ConnectionPersistenceError>;
111 readonly saveVcsRefs: (
112 environmentId: EnvironmentId,
113 cwd: string,
114 refs: VcsListRefsResult,
115 ) => Effect.Effect<void, ConnectionPersistenceError>;
116 readonly removeVcsRefs: (
117 environmentId: EnvironmentId,
118 cwd: string,
119 ) => Effect.Effect<void, ConnectionPersistenceError>;
120 /**
121 * Removes every persisted branch-list snapshot for an environment. Git ref
122 * mutations are repository-wide, and linked worktrees may have cached the
123 * same refs under different working-directory keys.
124 */
125 readonly clearVcsRefs: (
126 environmentId: EnvironmentId,
127 ) => Effect.Effect<void, ConnectionPersistenceError>;
128 readonly clear: (
129 environmentId: EnvironmentId,
130 ) => Effect.Effect<void, ConnectionPersistenceError>;
131 }
132>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
133​
134const encodeProjectShells = Schema.encodeEffect(Schema.Array(OrchestrationProjectShell));
135​
136/**
137 * Encodes a shell snapshot for `EnvironmentCacheStore.saveShell`. The result
138 * equals `Schema.encode(OrchestrationShellSnapshot)`, so the cache format does
139 * not change. Walking thousands of threads through Schema blocks the UI
140 * thread, and a decoded thread shell is already in its encoded form, so only
141 * the projects go through Schema: their icon has a real encode transform.
142 */
143export const encodeShellSnapshotForCache = (snapshot: OrchestrationShellSnapshot) =>
144 Effect.map(
145 encodeProjectShells(snapshot.projects),
146 (projects) => ({ ...snapshot, projects }) satisfies typeof OrchestrationShellSnapshot.Encoded,
147 );
148​
149export class EnvironmentOwnedDataCleanup extends Context.Reference<{
150 readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
151}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {
152 defaultValue: () => ({
153 clear: () => Effect.void,
154 }),
155}) {}