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.TaggedErrorClass<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.TaggedErrorClass<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
86export const make = Effect.gen(function* () {
87 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
88 const terminalManager = yield* TerminalManager.TerminalManager;
89 const serverSettings = yield* ServerSettings.ServerSettingsService;
90
91 const runForThread: ProjectSetupScriptRunner["Service"]["runForThread"] = Effect.fn(
92 "ProjectSetupScriptRunner.runForThread",
93 )(function* (input) {
94 const errorContext = {
95 threadId: input.threadId,
96 worktreePath: input.worktreePath,
97 ...(input.projectId === undefined ? {} : { projectId: input.projectId }),
98 ...(input.projectCwd === undefined ? {} : { projectCwd: input.projectCwd }),
99 };
100 const projectById = input.projectId
101 ? yield* projectionSnapshotQuery.getProjectShellById(ProjectId.make(input.projectId)).pipe(
102 Effect.map(Option.getOrUndefined),
103 Effect.mapError(
104 (cause) =>
105 new ProjectSetupScriptOperationError({
106 ...errorContext,
107 operation: "resolveProject",
108 cause,
109 }),
110 ),
111 )
112 : null;
113 const project =
114 projectById ??
115 (input.projectCwd
116 ? yield* projectionSnapshotQuery.getActiveProjectByWorkspaceRoot(input.projectCwd).pipe(
117 Effect.map(Option.getOrUndefined),
118 Effect.mapError(
119 (cause) =>
120 new ProjectSetupScriptOperationError({
121 ...errorContext,
122 operation: "resolveProject",
123 cause,
124 }),
125 ),
126 )
127 : null);
128
129 if (!project) {
130 return yield* new ProjectSetupScriptProjectNotFoundError(errorContext);
131 }
132
133 const settings = yield* serverSettings.getSettings.pipe(
134 Effect.mapError(
135 (cause) =>
136 new ProjectSetupScriptOperationError({
137 ...errorContext,
138 operation: "readSettings",
139 cause,
140 }),
141 ),
142 );
143 const script = setupProjectScript(resolveProjectScripts(settings, project));
144 if (!script) {
145 return {
146 status: "no-script",
147 } as const;
148 }
149
150 const terminalId = input.preferredTerminalId ?? `setup-${script.id}`;
151 const cwd = input.worktreePath;
152 const env = projectScriptRuntimeEnv({
153 project: { cwd: project.workspaceRoot },
154 worktreePath: input.worktreePath,
155 });
156
157 yield* terminalManager
158 .open({
159 threadId: input.threadId,
160 terminalId,
161 cwd,
162 worktreePath: input.worktreePath,
163 env,
164 })
165 .pipe(
166 Effect.mapError(
167 (cause) =>
168 new ProjectSetupScriptOperationError({
169 ...errorContext,
170 operation: "openTerminal",
171 cause,
172 }),
173 ),
174 );
175 yield* terminalManager
176 .write({
177 threadId: input.threadId,
178 terminalId,
179 data: `${script.command}\r`,
180 })
181 .pipe(
182 Effect.mapError(
183 (cause) =>
184 new ProjectSetupScriptOperationError({
185 ...errorContext,
186 operation: "writeCommand",
187 cause,
188 }),
189 ),
190 );
191
192 return {
193 status: "started",
194 scriptId: script.id,
195 scriptName: script.name,
196 terminalId,
197 cwd,
198 } as const;
199 });
200
201 return ProjectSetupScriptRunner.of({ runForThread });
202});
203
204export const layer = Layer.effect(ProjectSetupScriptRunner, make);