Open History to see the last commit
1import { type CheckpointRef } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import type { CheckpointStoreError } from "./Errors.ts";
7import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
8import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
9
10export interface CaptureCheckpointInput {
11 readonly cwd: string;
12 readonly checkpointRef: CheckpointRef;
13}
14
15export interface RestoreCheckpointInput {
16 readonly cwd: string;
17 readonly checkpointRef: CheckpointRef;
18 readonly fallbackToHead?: boolean;
19}
20
21export interface DiffCheckpointsInput {
22 readonly cwd: string;
23 readonly fromCheckpointRef: CheckpointRef;
24 readonly toCheckpointRef: CheckpointRef;
25 readonly fallbackFromToHead?: boolean;
26 readonly ignoreWhitespace: boolean;
27 readonly format?: "patch" | "numstat";
28 /** Maximum bytes collected from the diff command. */
29 readonly maxOutputBytes?: number;
30 /** Limits the diff to these exact paths. An empty list yields an empty diff. */
31 readonly filePaths?: ReadonlyArray<string>;
32}
33
34export interface ListAuthoredPathsInput {
35 readonly cwd: string;
36 readonly fromCheckpointRef: CheckpointRef;
37 readonly toCheckpointRef: CheckpointRef;
38}
39
40export interface DeleteCheckpointRefsInput {
41 readonly cwd: string;
42 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
43}
44
45export class CheckpointStore extends Context.Service<
46 CheckpointStore,
47 {
48 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
49
50 readonly captureCheckpoint: (
51 input: CaptureCheckpointInput,
52 ) => Effect.Effect<void, CheckpointStoreError>;
53
54 readonly hasCheckpointRef: (
55 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
56 ) => Effect.Effect<boolean, CheckpointStoreError>;
57
58 readonly restoreCheckpoint: (
59 input: RestoreCheckpointInput,
60 ) => Effect.Effect<boolean, CheckpointStoreError>;
61
62 readonly diffCheckpoints: (
63 input: DiffCheckpointsInput,
64 ) => Effect.Effect<string, CheckpointStoreError>;
65
66 /** Null means HEAD is unchanged or unrecorded; all changed paths then belong to the turn. */
67 readonly listAuthoredPaths: (
68 input: ListAuthoredPathsInput,
69 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
70
71 readonly deleteCheckpointRefs: (
72 input: DeleteCheckpointRefsInput,
73 ) => Effect.Effect<void, CheckpointStoreError>;
74 }
75>()("t3/checkpointing/CheckpointStore") {}
76
77/** @public */
78export const make = Effect.gen(function* () {
79 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
80
81 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
82 cwd: string,
83 ) {
84 const handle = yield* vcsRegistry.resolve({ cwd });
85 return handle.driver.checkpoints satisfies VcsCheckpointOps;
86 });
87
88 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
89 vcsRegistry
90 .detect({ cwd, requestedKind: "git" })
91 .pipe(Effect.map((repository) => repository !== null));
92
93 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
94 "captureCheckpoint",
95 )(function* (input) {
96 const checkpoints = yield* resolveCheckpoints(input.cwd);
97 return yield* checkpoints.captureCheckpoint(input);
98 });
99
100 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
101 "hasCheckpointRef",
102 )(function* (input) {
103 const checkpoints = yield* resolveCheckpoints(input.cwd);
104 return yield* checkpoints.hasCheckpointRef(input);
105 });
106
107 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
108 "restoreCheckpoint",
109 )(function* (input) {
110 const checkpoints = yield* resolveCheckpoints(input.cwd);
111 return yield* checkpoints.restoreCheckpoint(input);
112 });
113
114 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
115 "diffCheckpoints",
116 )(function* (input) {
117 const checkpoints = yield* resolveCheckpoints(input.cwd);
118 return yield* checkpoints.diffCheckpoints(input);
119 });
120
121 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
122 "listAuthoredPaths",
123 )(function* (input) {
124 const checkpoints = yield* resolveCheckpoints(input.cwd);
125 return yield* checkpoints.listAuthoredPaths(input);
126 });
127
128 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
129 "deleteCheckpointRefs",
130 )(function* (input) {
131 const checkpoints = yield* resolveCheckpoints(input.cwd);
132 return yield* checkpoints.deleteCheckpointRefs(input);
133 });
134
135 return CheckpointStore.of({
136 isGitRepository,
137 captureCheckpoint,
138 hasCheckpointRef,
139 restoreCheckpoint,
140 diffCheckpoints,
141 listAuthoredPaths,
142 deleteCheckpointRefs,
143 });
144});
145
146export const layer = Layer.effect(CheckpointStore, make);