Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectSetupScriptRunner.ts

220 lines6.7 KB

1import { ProjectId, type ProjectScript } 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 ServerSettings from "../serverSettings.ts";
14import * as TerminalManager from "../terminal/Manager.ts";
15import * as ProjectService from "./ProjectService.ts";
16​
17export interface ProjectSetupScriptRunnerResultNoScript {
18 readonly status: "no-script";
20​
21export interface ProjectSetupScriptRunnerResultStarted {
22 readonly status: "started";
23 readonly scriptId: string;
24 readonly scriptName: string;
25 readonly terminalId: string;
26 readonly cwd: string;
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 readonly project?: {
40 readonly id: ProjectId;
41 readonly workspaceRoot: string;
42 readonly scripts: ReadonlyArray<ProjectScript>;
43 };
45​
46export class ProjectSetupScriptOperationError extends Schema.TaggedError<ProjectSetupScriptOperationError>()(
47 "ProjectSetupScriptOperationError",
48 {
49 threadId: Schema.String,
50 projectId: Schema.optional(Schema.String),
51 projectCwd: Schema.optional(Schema.String),
52 worktreePath: Schema.String,
53 operation: Schema.Literals(["resolveProject", "readSettings", "openTerminal", "writeCommand"]),
54 cause: Schema.Defect(),
55 },
56) {
57 override get message(): string {
58 return `Project setup script operation '${this.operation}' failed for thread '${this.threadId}' in '${this.worktreePath}'.`;
59 }
61​
62export class ProjectSetupScriptProjectNotFoundError extends Schema.TaggedError<ProjectSetupScriptProjectNotFoundError>()(
63 "ProjectSetupScriptProjectNotFoundError",
64 {
65 threadId: Schema.String,
66 projectId: Schema.optional(Schema.String),
67 projectCwd: Schema.optional(Schema.String),
68 worktreePath: Schema.String,
69 },
70) {
71 override get message(): string {
72 return `Project was not found for setup script execution for thread '${this.threadId}' in '${this.worktreePath}'.`;
73 }
75​
76export const ProjectSetupScriptRunnerError = Schema.Union([
77 ProjectSetupScriptOperationError,
78 ProjectSetupScriptProjectNotFoundError,
79]);
80export type ProjectSetupScriptRunnerError = typeof ProjectSetupScriptRunnerError.Type;
81​
82export class ProjectSetupScriptRunner extends Context.Service<
83 ProjectSetupScriptRunner,
84 {
85 readonly runForThread: (
86 input: ProjectSetupScriptRunnerInput,
87 ) => Effect.Effect<ProjectSetupScriptRunnerResult, ProjectSetupScriptRunnerError>;
88 }
89>()("t3/project/ProjectSetupScriptRunner") {}
90​
91/** @public Service construction is part of the canonical Effect module API. */
92export const make = Effect.gen(function* () {
93 const projects = yield* ProjectService.ProjectService;
94 const terminalManager = yield* TerminalManager.TerminalManager;
95 const serverSettings = yield* ServerSettings.ServerSettingsService;
96​
97 const runForThread: ProjectSetupScriptRunner["Service"]["runForThread"] = Effect.fn(
98 "ProjectSetupScriptRunner.runForThread",
99 )(function* (input) {
100 const errorContext = {
101 threadId: input.threadId,
102 worktreePath: input.worktreePath,
103 ...(input.projectId === undefined ? {} : { projectId: input.projectId }),
104 ...(input.projectCwd === undefined ? {} : { projectCwd: input.projectCwd }),
105 };
106 const suppliedProject = input.project;
107 const projectById =
108 suppliedProject ??
109 (input.projectId
110 ? yield* projects.getById(ProjectId.make(input.projectId)).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 const project =
123 suppliedProject ??
124 projectById ??
125 (input.projectCwd
126 ? yield* projects.getByWorkspaceRoot(input.projectCwd).pipe(
127 Effect.map(Option.getOrUndefined),
128 Effect.mapError(
129 (cause) =>
130 new ProjectSetupScriptOperationError({
131 ...errorContext,
132 operation: "resolveProject",
133 cause,
134 }),
135 ),
136 )
137 : null);
138​
139 if (!project) {
140 return yield* new ProjectSetupScriptProjectNotFoundError(errorContext);
141 }
142​
143 const settings = yield* serverSettings.getSettings.pipe(
144 Effect.mapError(
145 (cause) =>
146 new ProjectSetupScriptOperationError({
147 ...errorContext,
148 operation: "readSettings",
149 cause,
150 }),
151 ),
152 );
153 const script = setupProjectScript(resolveProjectScripts(settings, project));
154 if (!script) {
155 return {
156 status: "no-script",
157 } as const;
158 }
159​
160 const terminalId = input.preferredTerminalId ?? `setup-${script.id}`;
161 const cwd = input.worktreePath;
162 const env = {
163 ...projectScriptRuntimeEnv({
164 project: { cwd: project.workspaceRoot },
165 worktreePath: input.worktreePath,
166 }),
167 // Setup can run before a client attaches. Truecolor probes in tools such
168 // as Vite+ wait for terminal replies that nobody can send at that point.
169 // Keep TERM's 256-color support without advertising truecolor here.
170 COLORTERM: "",
171 };
172​
173 yield* terminalManager
174 .open({
175 threadId: input.threadId,
176 terminalId,
177 cwd,
178 worktreePath: input.worktreePath,
179 env,
180 })
181 .pipe(
182 Effect.mapError(
183 (cause) =>
184 new ProjectSetupScriptOperationError({
185 ...errorContext,
186 operation: "openTerminal",
187 cause,
188 }),
189 ),
190 );
191 yield* terminalManager
192 .write({
193 threadId: input.threadId,
194 terminalId,
195 data: `${script.command}\r`,
196 })
197 .pipe(
198 Effect.mapError(
199 (cause) =>
200 new ProjectSetupScriptOperationError({
201 ...errorContext,
202 operation: "writeCommand",
203 cause,
204 }),
205 ),
206 );
207​
208 return {
209 status: "started",
210 scriptId: script.id,
211 scriptName: script.name,
212 terminalId,
213 cwd,
214 } as const;
215 });
216​
217 return ProjectSetupScriptRunner.of({ runForThread });
218});
219​
220export const layer = Layer.effect(ProjectSetupScriptRunner, make);