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 { ConnectionTarget, PersistedConnectionTarget } from "../connection/model.ts";
16
17export class ConnectionPersistenceError extends Schema.TaggedError<ConnectionPersistenceError>()(
18 "ConnectionPersistenceError",
19 {
20 operation: Schema.Literals([
21 "list-targets",
22 "list-disabled-targets",
23 "register-connection",
24 "set-connection-routes",
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 /**
57 * Saves one route's records and sets the environment's full route list,
58 * preferred first. Records of routes missing from `routes` are dropped.
59 */
60 readonly register: (
61 registration: ConnectionRegistration,
62 routes: ReadonlyArray<PersistedConnectionTarget>,
63 ) => Effect.Effect<void, ConnectionPersistenceError>;
64 /** Reorders or drops routes without adding one. `routes` must not be empty. */
65 readonly setRoutes: (
66 environmentId: EnvironmentId,
67 routes: ReadonlyArray<PersistedConnectionTarget>,
68 ) => Effect.Effect<void, ConnectionPersistenceError>;
69 /** Forgets the environment and every route it had. */
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<OrchestrationV2ShellSnapshot>, 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 /**
106 * The last complete server configuration. This deliberately includes provider
107 * metadata so offline task creation can still offer the models a user last saw.
108 */
109 readonly loadServerConfig: (
110 environmentId: EnvironmentId,
111 ) => Effect.Effect<Option.Option<ServerConfig>, ConnectionPersistenceError>;
112 readonly saveServerConfig: (
113 environmentId: EnvironmentId,
114 config: ServerConfig,
115 ) => Effect.Effect<void, ConnectionPersistenceError>;
116 /**
117 * The unfiltered branch list for a workspace. Query-specific lists are not
118 * cached because they are incomplete and unsafe to present as a full picker.
119 */
120 readonly loadVcsRefs: (
121 environmentId: EnvironmentId,
122 cwd: string,
123 ) => Effect.Effect<Option.Option<VcsListRefsResult>, ConnectionPersistenceError>;
124 readonly saveVcsRefs: (
125 environmentId: EnvironmentId,
126 cwd: string,
127 refs: VcsListRefsResult,
128 ) => Effect.Effect<void, ConnectionPersistenceError>;
129 readonly removeVcsRefs: (
130 environmentId: EnvironmentId,
131 cwd: string,
132 ) => Effect.Effect<void, ConnectionPersistenceError>;
133 /**
134 * Removes every persisted branch-list snapshot for an environment. Git ref
135 * mutations are repository-wide, and linked worktrees may have cached the
136 * same refs under different working-directory keys.
137 */
138 readonly clearVcsRefs: (
139 environmentId: EnvironmentId,
140 ) => Effect.Effect<void, ConnectionPersistenceError>;
141 readonly clear: (
142 environmentId: EnvironmentId,
143 ) => Effect.Effect<void, ConnectionPersistenceError>;
144 }
145>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
146
147export class EnvironmentOwnedDataCleanup extends Context.Reference<{
148 readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
149}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {
150 defaultValue: () => ({
151 clear: () => Effect.void,
152 }),
153}) {}