Open History to see the last commit
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";
17import type { ShellCacheOrigin } from "./orchestrationCache.ts";
18
19export type { ShellCacheOrigin };
20
21export interface CachedShellSnapshot extends DeferredShellSnapshot {
22 readonly origin?: ShellCacheOrigin;
23}
24
25export class ConnectionPersistenceError extends Schema.TaggedError<ConnectionPersistenceError>()(
26 "ConnectionPersistenceError",
27 {
28 operation: Schema.Literals([
29 "list-targets",
30 "list-disabled-targets",
31 "register-connection",
32 "set-connection-routes",
33 "remove-connection",
34 "set-connection-enabled",
35 "load-shell",
36 "save-shell",
37 "load-thread",
38 "save-thread",
39 "remove-thread",
40 "load-server-config",
41 "save-server-config",
42 "load-vcs-refs",
43 "save-vcs-refs",
44 "remove-vcs-refs",
45 "clear-vcs-refs",
46 "clear-environment",
47 ]),
48 message: Schema.String,
49 },
50) {}
51
52export class ConnectionTargetStore extends Context.Service<
53 ConnectionTargetStore,
54 {
55 readonly list: Effect.Effect<ReadonlyArray<ConnectionTarget>, ConnectionPersistenceError>;
56 readonly listDisabled: Effect.Effect<ReadonlyArray<EnvironmentId>, ConnectionPersistenceError>;
57 }
58>()("@t3tools/client-runtime/platform/persistence/ConnectionTargetStore") {}
59
60export class ConnectionRegistrationStore extends Context.Service<
61 ConnectionRegistrationStore,
62 {
63 /**
64 * Records of routes missing from `routes` are dropped.
65 */
66 readonly register: (
67 registration: ConnectionRegistration,
68 routes: ReadonlyArray<PersistedConnectionTarget>,
69 sshTunnelClaim?: string,
70 ) => Effect.Effect<void, ConnectionPersistenceError>;
71 /**
72 * `routes` must not be empty.
73 */
74 readonly setRoutes: (
75 environmentId: EnvironmentId,
76 routes: ReadonlyArray<PersistedConnectionTarget>,
77 ) => Effect.Effect<void, ConnectionPersistenceError>;
78 readonly remove: (
79 environmentId: EnvironmentId,
80 ) => Effect.Effect<void, ConnectionPersistenceError>;
81 readonly setEnabled: (
82 environmentId: EnvironmentId,
83 enabled: boolean,
84 ) => Effect.Effect<void, ConnectionPersistenceError>;
85 }
86>()("@t3tools/client-runtime/platform/persistence/ConnectionRegistrationStore") {}
87
88export class EnvironmentCacheStore extends Context.Service<
89 EnvironmentCacheStore,
90 {
91 readonly loadShell: (
92 environmentId: EnvironmentId,
93 ) => Effect.Effect<Option.Option<CachedShellSnapshot>, ConnectionPersistenceError>;
94 /** `origin` only when the rows descend from a server snapshot. */
95 readonly saveShell: (
96 environmentId: EnvironmentId,
97 snapshot: OrchestrationV2ShellSnapshot,
98 origin?: ShellCacheOrigin,
99 ) => Effect.Effect<void, ConnectionPersistenceError>;
100 readonly loadThread: (
101 environmentId: EnvironmentId,
102 threadId: ThreadId,
103 ) => Effect.Effect<
104 Option.Option<OrchestrationV2ThreadDetailSnapshot>,
105 ConnectionPersistenceError
106 >;
107 readonly saveThread: (
108 environmentId: EnvironmentId,
109 snapshot: OrchestrationV2ThreadDetailSnapshot,
110 ) => Effect.Effect<void, ConnectionPersistenceError>;
111 readonly removeThread: (
112 environmentId: EnvironmentId,
113 threadId: ThreadId,
114 ) => Effect.Effect<void, ConnectionPersistenceError>;
115 readonly loadServerConfig: (
116 environmentId: EnvironmentId,
117 ) => Effect.Effect<Option.Option<ServerConfig>, ConnectionPersistenceError>;
118 readonly saveServerConfig: (
119 environmentId: EnvironmentId,
120 config: ServerConfig,
121 ) => Effect.Effect<void, ConnectionPersistenceError>;
122 /**
123 * Query-specific lists are not cached because they are incomplete and unsafe
124 * to present as a full picker.
125 */
126 readonly loadVcsRefs: (
127 environmentId: EnvironmentId,
128 cwd: string,
129 ) => Effect.Effect<Option.Option<VcsListRefsResult>, ConnectionPersistenceError>;
130 readonly saveVcsRefs: (
131 environmentId: EnvironmentId,
132 cwd: string,
133 refs: VcsListRefsResult,
134 ) => Effect.Effect<void, ConnectionPersistenceError>;
135 readonly removeVcsRefs: (
136 environmentId: EnvironmentId,
137 cwd: string,
138 ) => Effect.Effect<void, ConnectionPersistenceError>;
139 /**
140 * Git ref mutations are repository-wide, and linked worktrees may have
141 * cached the same refs under different working-directory keys.
142 */
143 readonly clearVcsRefs: (
144 environmentId: EnvironmentId,
145 ) => Effect.Effect<void, ConnectionPersistenceError>;
146 readonly clear: (
147 environmentId: EnvironmentId,
148 ) => Effect.Effect<void, ConnectionPersistenceError>;
149 }
150>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
151
152export class EnvironmentOwnedDataCleanup extends Context.Reference<{
153 readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
154}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {
155 defaultValue: () => ({
156 clear: () => Effect.void,
157 }),
158}) {}