Open History to see the last commit
1import { ProjectId } from "@t3tools/contracts";
2import {
3 projectScriptRuntimeEnv,
4 resolveProjectScripts,
5 setupProjectScript,
6} from "@t3tools/shared/projectScripts";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12
13import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
14import * as ServerSettings from "../serverSettings.ts";
15import * as TerminalManager from "../terminal/Manager.ts";
16
17export interface ProjectSetupScriptRunnerResultNoScript {
18 readonly status: "no-script";
19}
20
21export interface ProjectSetupScriptRunnerResultStarted {
22 readonly status: "started";
23 readonly scriptId: string;
24 readonly scriptName: string;
25 readonly terminalId: string;
26 readonly cwd: string;
27}
28
29export type ProjectSetupScriptRunnerResult =
30 | ProjectSetupScriptRunnerResultNoScript
31 | ProjectSetupScriptRunnerResultStarted;
32
33export interface ProjectSetupScriptRunnerInput {
34 readonly threadId: string;
35 readonly projectId?: string;
36 readonly projectCwd?: string;
37 readonly worktreePath: string;
38 readonly preferredTerminalId?: string;
39}
40
41export class ProjectSetupScriptOperationError extends Schema.TaggedError<ProjectSetupScriptOperationError>()(
42 "ProjectSetupScriptOperationError",
43 {
44 threadId: Schema.String,
45 projectId: Schema.optional(Schema.String),
46 projectCwd: Schema.optional(Schema.String),
47 worktreePath: Schema.String,
48 operation: Schema.Literals(["resolveProject", "readSettings", "openTerminal", "writeCommand"]),
49 cause: Schema.Defect(),
50 },
51) {
52 override get message(): string {
53 return `Project setup script operation '${this.operation}' failed for thread '${this.threadId}' in '${this.worktreePath}'.`;
54 }
55}
56
57export class ProjectSetupScriptProjectNotFoundError extends Schema.TaggedError<ProjectSetupScriptProjectNotFoundError>()(
58 "ProjectSetupScriptProjectNotFoundError",
59 {
60 threadId: Schema.String,
61 projectId: Schema.optional(Schema.String),
62 projectCwd: Schema.optional(Schema.String),
63 worktreePath: Schema.String,
64 },
65) {
66 override get message(): string {
67 return `Project was not found for setup script execution for thread '${this.threadId}' in '${this.worktreePath}'.`;
68 }
69}
70
71export const ProjectSetupScriptRunnerError = Schema.Union([
72 ProjectSetupScriptOperationError,
73 ProjectSetupScriptProjectNotFoundError,
74]);
75export type ProjectSetupScriptRunnerError = typeof ProjectSetupScriptRunnerError.Type;
76
77export class ProjectSetupScriptRunner extends Context.Service<
78 ProjectSetupScriptRunner,
79 {
80 readonly runForThread: (
81 input: ProjectSetupScriptRunnerInput,
82 ) => Effect.Effect<ProjectSetupScriptRunnerResult, ProjectSetupScriptRunnerError>;
83 }
84>()("t3/project/ProjectSetupScriptRunner") {}
85
86/** @public Service construction is part of the canonical Effect module API. */
87export const make = Effect.gen(function* () {
88 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
89 const terminalManager = yield* TerminalManager.TerminalManager;
90 const serverSettings = yield* ServerSettings.ServerSettingsService;
91
92 const runForThread: ProjectSetupScriptRunner["Service"]["runForThread"] = Effect.fn(
93 "ProjectSetupScriptRunner.runForThread",
94 )(function* (input) {
95 const errorContext = {
96 threadId: input.threadId,
97 worktreePath: input.worktreePath,
98 ...(input.projectId === undefined ? {} : { projectId: input.projectId }),
99 ...(input.projectCwd === undefined ? {} : { projectCwd: input.projectCwd }),
100 };
101 const projectById = input.projectId
102 ? yield* projectionSnapshotQuery.getProjectShellById(ProjectId.make(input.projectId)).pipe(
103 Effect.map(Option.getOrUndefined),
104 Effect.mapError(
105 (cause) =>
106 new ProjectSetupScriptOperationError({
107 ...errorContext,
108 operation: "resolveProject",
109 cause,
110 }),
111 ),
112 )
113 : null;
114 const project =
115 projectById ??
116 (input.projectCwd
117 ? yield* projectionSnapshotQuery.getActiveProjectByWorkspaceRoot(input.projectCwd).pipe(
118 Effect.map(Option.getOrUndefined),
119 Effect.mapError(
120 (cause) =>
121 new ProjectSetupScriptOperationError({
122 ...errorContext,
123 operation: "resolveProject",
124 cause,
125 }),
126 ),
127 )
128 : null);
129
130 if (!project) {
131 return yield* new ProjectSetupScriptProjectNotFoundError(errorContext);
132 }
133
134 const settings = yield* serverSettings.getSettings.pipe(
135 Effect.mapError(
136 (cause) =>
137 new ProjectSetupScriptOperationError({
138 ...errorContext,
139 operation: "readSettings",
140 cause,
141 }),
142 ),
143 );
144 const script = setupProjectScript(resolveProjectScripts(settings, project));
145 if (!script) {
146 return {
147 status: "no-script",
148 } as const;
149 }
150
151 const terminalId = input.preferredTerminalId ?? `setup-${script.id}`;
152 const cwd = input.worktreePath;
153 const env = projectScriptRuntimeEnv({
154 project: { cwd: project.workspaceRoot },
155 worktreePath: input.worktreePath,
156 });
157
158 yield* terminalManager
159 .open({
160 threadId: input.threadId,
161 terminalId,
162 cwd,
163 worktreePath: input.worktreePath,
164 env,
165 })
166 .pipe(
167 Effect.mapError(
168 (cause) =>
169 new ProjectSetupScriptOperationError({
170 ...errorContext,
171 operation: "openTerminal",
172 cause,
173 }),
174 ),
175 );
176 yield* terminalManager
177 .write({
178 threadId: input.threadId,
179 terminalId,
180 data: `${script.command}\r`,
181 })
182 .pipe(
183 Effect.mapError(
184 (cause) =>
185 new ProjectSetupScriptOperationError({
186 ...errorContext,
187 operation: "writeCommand",
188 cause,
189 }),
190 ),
191 );
192
193 return {
194 status: "started",
195 scriptId: script.id,
196 scriptName: script.name,
197 terminalId,
198 cwd,
199 } as const;
200 });
201
202 return ProjectSetupScriptRunner.of({ runForThread });
203});
204
205export const layer = Layer.effect(ProjectSetupScriptRunner, make);