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