@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

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