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

persistence.ts

148 lines5.1 KB

1import {
2 type EnvironmentId,
3 type OrchestrationV2ShellSnapshot,
4 type OrchestrationV2ThreadDetailSnapshot,
5 type ServerConfig,
6 type ThreadId,
7 type VcsListRefsResult,
8} from "@t3tools/contracts";
9import * as Context from "effect/Context";
10import * as Effect from "effect/Effect";
11import * as Option from "effect/Option";
12import * as Schema from "effect/Schema";
13​
14import type { ConnectionRegistration } from "../connection/catalog.ts";
15import type { DeferredShellSnapshot } from "../state/shellPullRequests.ts";
16import type { ConnectionTarget, PersistedConnectionTarget } 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 "set-connection-routes",
26 "remove-connection",
27 "set-connection-enabled",
28 "load-shell",
29 "save-shell",
30 "load-thread",
31 "save-thread",
32 "remove-thread",
33 "load-server-config",
34 "save-server-config",
35 "load-vcs-refs",
36 "save-vcs-refs",
37 "remove-vcs-refs",
38 "clear-vcs-refs",
39 "clear-environment",
40 ]),
41 message: Schema.String,
42 },
43) {}
44​
45export class ConnectionTargetStore extends Context.Service<
46 ConnectionTargetStore,
47 {
48 readonly list: Effect.Effect<ReadonlyArray<ConnectionTarget>, ConnectionPersistenceError>;
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 /**
57 * Records of routes missing from `routes` are dropped.
58 */
59 readonly register: (
60 registration: ConnectionRegistration,
61 routes: ReadonlyArray<PersistedConnectionTarget>,
62 ) => Effect.Effect<void, ConnectionPersistenceError>;
63 /**
64 * `routes` must not be empty.
65 */
66 readonly setRoutes: (
67 environmentId: EnvironmentId,
68 routes: ReadonlyArray<PersistedConnectionTarget>,
69 ) => Effect.Effect<void, ConnectionPersistenceError>;
70 readonly remove: (
71 environmentId: EnvironmentId,
72 ) => Effect.Effect<void, ConnectionPersistenceError>;
73 readonly setEnabled: (
74 environmentId: EnvironmentId,
75 enabled: boolean,
76 ) => Effect.Effect<void, ConnectionPersistenceError>;
77 }
78>()("@t3tools/client-runtime/platform/persistence/ConnectionRegistrationStore") {}
79​
80export class EnvironmentCacheStore extends Context.Service<
81 EnvironmentCacheStore,
82 {
83 readonly loadShell: (
84 environmentId: EnvironmentId,
85 ) => Effect.Effect<Option.Option<DeferredShellSnapshot>, ConnectionPersistenceError>;
86 readonly saveShell: (
87 environmentId: EnvironmentId,
88 snapshot: OrchestrationV2ShellSnapshot,
89 ) => Effect.Effect<void, ConnectionPersistenceError>;
90 readonly loadThread: (
91 environmentId: EnvironmentId,
92 threadId: ThreadId,
93 ) => Effect.Effect<
94 Option.Option<OrchestrationV2ThreadDetailSnapshot>,
95 ConnectionPersistenceError
96 >;
97 readonly saveThread: (
98 environmentId: EnvironmentId,
99 snapshot: OrchestrationV2ThreadDetailSnapshot,
100 ) => Effect.Effect<void, ConnectionPersistenceError>;
101 readonly removeThread: (
102 environmentId: EnvironmentId,
103 threadId: ThreadId,
104 ) => Effect.Effect<void, ConnectionPersistenceError>;
105 readonly loadServerConfig: (
106 environmentId: EnvironmentId,
107 ) => Effect.Effect<Option.Option<ServerConfig>, ConnectionPersistenceError>;
108 readonly saveServerConfig: (
109 environmentId: EnvironmentId,
110 config: ServerConfig,
111 ) => Effect.Effect<void, ConnectionPersistenceError>;
112 /**
113 * Query-specific lists are not cached because they are incomplete and unsafe
114 * to present as a full picker.
115 */
116 readonly loadVcsRefs: (
117 environmentId: EnvironmentId,
118 cwd: string,
119 ) => Effect.Effect<Option.Option<VcsListRefsResult>, ConnectionPersistenceError>;
120 readonly saveVcsRefs: (
121 environmentId: EnvironmentId,
122 cwd: string,
123 refs: VcsListRefsResult,
124 ) => Effect.Effect<void, ConnectionPersistenceError>;
125 readonly removeVcsRefs: (
126 environmentId: EnvironmentId,
127 cwd: string,
128 ) => Effect.Effect<void, ConnectionPersistenceError>;
129 /**
130 * Git ref mutations are repository-wide, and linked worktrees may have
131 * cached the same refs under different working-directory keys.
132 */
133 readonly clearVcsRefs: (
134 environmentId: EnvironmentId,
135 ) => Effect.Effect<void, ConnectionPersistenceError>;
136 readonly clear: (
137 environmentId: EnvironmentId,
138 ) => Effect.Effect<void, ConnectionPersistenceError>;
139 }
140>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
141​
142export class EnvironmentOwnedDataCleanup extends Context.Reference<{
143 readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
144}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {
145 defaultValue: () => ({
146 clear: () => Effect.void,
147 }),
148}) {}