Open History to see the last commit
1import * as Context from "effect/Context";
2import * as DateTime from "effect/DateTime";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7
8import {
9 VcsRepositoryDetectionError,
10 VcsUnsupportedOperationError,
11 type ReviewDiffFileContentsInput,
12 type ReviewDiffFileContentsResult,
13 type ReviewDiffPreviewError,
14 type ReviewDiffPreviewInput,
15 type ReviewDiffPreviewResult,
16} from "@t3tools/contracts";
17
18import * as ServerConfig from "../config.ts";
19import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
20import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
21
22export class ReviewService extends Context.Service<
23 ReviewService,
24 {
25 readonly getDiffPreview: (
26 input: ReviewDiffPreviewInput,
27 ) => Effect.Effect<ReviewDiffPreviewResult, ReviewDiffPreviewError>;
28 readonly getDiffFileContents: (
29 input: ReviewDiffFileContentsInput,
30 ) => Effect.Effect<ReviewDiffFileContentsResult, ReviewDiffPreviewError>;
31 }
32>()("t3/review/ReviewService") {}
33
34/** @public Service construction is part of the canonical Effect module API. */
35export const make = Effect.gen(function* () {
36 const config = yield* ServerConfig.ServerConfig;
37 const fileSystem = yield* FileSystem.FileSystem;
38 const path = yield* Path.Path;
39 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
40 const git = yield* GitVcsDriver.GitVcsDriver;
41
42 const canonicalizePath = (value: string) => {
43 const resolvedPath = path.resolve(value);
44 return fileSystem.realPath(resolvedPath).pipe(
45 Effect.catchTags({
46 PlatformError: (cause) =>
47 cause.reason._tag === "NotFound"
48 ? Effect.succeed(resolvedPath)
49 : Effect.fail(
50 new VcsRepositoryDetectionError({
51 operation: "ReviewService.assertWorkspaceBoundCwd.canonicalizePath",
52 cwd: resolvedPath,
53 detail: "Failed to resolve a path while validating the review workspace.",
54 cause,
55 }),
56 ),
57 }),
58 );
59 };
60
61 const isWithinRoot = (candidate: string, root: string) => {
62 const relative = path.relative(root, candidate);
63 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
64 };
65
66 const assertWorkspaceBoundCwd = Effect.fn("ReviewService.assertWorkspaceBoundCwd")(function* (
67 operation: "ReviewService.getDiffPreview" | "ReviewService.getDiffFileContents",
68 cwd: string,
69 ) {
70 const [candidate, workspaceRoot, worktreesRoot] = yield* Effect.all([
71 canonicalizePath(cwd),
72 canonicalizePath(config.cwd),
73 canonicalizePath(config.worktreesDir),
74 ]);
75
76 if (isWithinRoot(candidate, workspaceRoot) || isWithinRoot(candidate, worktreesRoot)) {
77 return;
78 }
79
80 return yield* new VcsRepositoryDetectionError({
81 operation,
82 cwd,
83 detail:
84 operation === "ReviewService.getDiffPreview"
85 ? "Review diff preview cwd must stay within the configured workspace root."
86 : "Review diff file contents cwd must stay within the configured workspace root.",
87 });
88 });
89
90 const getDiffPreview: ReviewService["Service"]["getDiffPreview"] = Effect.fn(
91 "ReviewService.getDiffPreview",
92 )(function* (input) {
93 yield* assertWorkspaceBoundCwd("ReviewService.getDiffPreview", input.cwd);
94
95 const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" });
96 if (!handle) {
97 return {
98 cwd: input.cwd,
99 generatedAt: yield* DateTime.now,
100 sources: [],
101 };
102 }
103
104 const getDriverDiffPreview = handle.driver.getDiffPreview;
105 if (!getDriverDiffPreview) {
106 if (handle.kind === "git") {
107 return yield* git.getReviewDiffPreview(input);
108 }
109 return yield* new VcsUnsupportedOperationError({
110 operation: "ReviewService.getDiffPreview",
111 kind: handle.kind,
112 detail: `The ${handle.kind} VCS driver does not support review diff previews.`,
113 });
114 }
115
116 return yield* getDriverDiffPreview(input);
117 });
118
119 const getDiffFileContents: ReviewService["Service"]["getDiffFileContents"] = Effect.fn(
120 "ReviewService.getDiffFileContents",
121 )(function* (input) {
122 yield* assertWorkspaceBoundCwd("ReviewService.getDiffFileContents", input.cwd);
123
124 const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" });
125 if (handle?.kind !== "git") {
126 return yield* new VcsUnsupportedOperationError({
127 operation: "ReviewService.getDiffFileContents",
128 kind: handle?.kind ?? "unknown",
129 detail: "Unchanged diff expansion currently requires a Git repository.",
130 });
131 }
132
133 return yield* git.getReviewDiffFileContents(input);
134 });
135
136 return ReviewService.of({
137 getDiffPreview,
138 getDiffFileContents,
139 });
140});
141
142export const layer = Layer.effect(ReviewService, make);