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

project.ts

323 lines10.5 KB

1import * as Schema from "effect/Schema";
2import { RepositoryIdentity } from "./environment.ts";
3import { ModelSelection } from "./modelSelection.ts";
4import {
5 CommandId,
6 IsoDateTime,
7 NonNegativeInt,
8 PositiveInt,
9 ProjectId,
10 TrimmedNonEmptyString,
11} from "./baseSchemas.ts";
12​
13const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
14const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
15const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
16​
17export const ProjectScriptIcon = Schema.Literals([
18 "play",
19 "test",
20 "lint",
21 "configure",
22 "build",
23 "debug",
24]);
25export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
26​
27export const ProjectScript = Schema.Struct({
28 id: TrimmedNonEmptyString,
29 name: TrimmedNonEmptyString,
30 command: TrimmedNonEmptyString,
31 icon: ProjectScriptIcon,
32 runOnWorktreeCreate: Schema.Boolean,
33 previewUrl: Schema.optional(TrimmedNonEmptyString),
34 autoOpenPreview: Schema.optional(Schema.Boolean),
35});
36export type ProjectScript = typeof ProjectScript.Type;
37​
38export const Project = Schema.Struct({
39 id: ProjectId,
40 title: TrimmedNonEmptyString,
41 workspaceRoot: TrimmedNonEmptyString,
42 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
43 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
44 defaultModelSelection: Schema.NullOr(ModelSelection),
45 scripts: Schema.Array(ProjectScript),
46 createdAt: IsoDateTime,
47 updatedAt: IsoDateTime,
48 deletedAt: Schema.NullOr(IsoDateTime),
49});
50export type Project = typeof Project.Type;
51​
52export const ProjectSnapshot = Schema.Struct({
53 projects: Schema.Array(Project),
54 updatedAt: IsoDateTime,
55});
56export type ProjectSnapshot = typeof ProjectSnapshot.Type;
57​
58export const ProjectChange = Schema.Union([
59 Schema.Struct({ type: Schema.Literal("project.upserted"), project: Project }),
60 Schema.Struct({
61 type: Schema.Literal("project.deleted"),
62 projectId: ProjectId,
63 deletedAt: IsoDateTime,
64 }),
65]);
66export type ProjectChange = typeof ProjectChange.Type;
67​
68export const ProjectMutation = Schema.Union([
69 Schema.Struct({
70 type: Schema.Literal("project.create"),
71 commandId: CommandId,
72 projectId: ProjectId,
73 title: TrimmedNonEmptyString,
74 workspaceRoot: TrimmedNonEmptyString,
75 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
76 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
77 scripts: Schema.optional(Schema.Array(ProjectScript)),
78 }),
79 Schema.Struct({
80 type: Schema.Literal("project.update"),
81 commandId: CommandId,
82 projectId: ProjectId,
83 title: Schema.optional(TrimmedNonEmptyString),
84 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
85 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
86 scripts: Schema.optional(Schema.Array(ProjectScript)),
87 }),
88 Schema.Struct({
89 type: Schema.Literal("project.delete"),
90 commandId: CommandId,
91 projectId: ProjectId,
92 force: Schema.optional(Schema.Boolean),
93 }),
94]);
95export type ProjectMutation = typeof ProjectMutation.Type;
96​
97export class ProjectMutationError extends Schema.TaggedErrorClass<ProjectMutationError>()(
98 "ProjectMutationError",
99 {
100 commandId: CommandId,
101 message: Schema.String,
102 cause: Schema.optional(Schema.Defect()),
103 },
104) {}
105​
106export const ProjectSearchEntriesInput = Schema.Struct({
107 cwd: TrimmedNonEmptyString,
108 query: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
109 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
110});
111export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
112​
113const ProjectEntryKind = Schema.Literals(["file", "directory"]);
114​
115export const ProjectEntry = Schema.Struct({
116 path: TrimmedNonEmptyString,
117 kind: ProjectEntryKind,
118});
119export type ProjectEntry = typeof ProjectEntry.Type;
120​
121export const ProjectSearchEntriesResult = Schema.Struct({
122 entries: Schema.Array(ProjectEntry),
123 truncated: Schema.Boolean,
124});
125export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
126​
127export const ProjectListEntriesInput = Schema.Struct({
128 cwd: TrimmedNonEmptyString,
129});
130export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
131​
132export const ProjectListEntriesResult = Schema.Struct({
133 entries: Schema.Array(ProjectEntry),
134 truncated: Schema.Boolean,
135});
136export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
137​
138export const ProjectEntriesFailure = Schema.Literals([
139 "workspace_root_not_found",
140 "workspace_root_create_failed",
141 "workspace_root_stat_failed",
142 "workspace_root_not_directory",
143 "search_index_create_failed",
144 "search_index_scan_timed_out",
145 "search_index_search_failed",
146]);
147export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
148​
149type ProjectEntriesFailureContext = {
150 readonly failure: ProjectEntriesFailure;
151 readonly normalizedCwd?: string;
152 readonly timeout?: string;
153 readonly detail?: string;
154 readonly cause?: unknown;
155};
156​
157function decodedProjectErrorMessage(props: object): string | undefined {
158 if (!("message" in props)) return undefined;
159 return typeof props.message === "string" ? props.message : undefined;
161​
162export class ProjectSearchEntriesError extends Schema.TaggedErrorClass<ProjectSearchEntriesError>()(
163 "ProjectSearchEntriesError",
164 {
165 cwd: Schema.optional(TrimmedNonEmptyString),
166 queryLength: Schema.optional(NonNegativeInt),
167 limit: Schema.optional(PositiveInt),
168 failure: Schema.optional(ProjectEntriesFailure),
169 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
170 timeout: Schema.optional(TrimmedNonEmptyString),
171 detail: Schema.optional(TrimmedNonEmptyString),
172 message: TrimmedNonEmptyString,
173 cause: Schema.optional(Schema.Defect()),
174 },
175) {
176 // The structured fields are optional on the wire so newer peers can decode legacy message-only
177 // failures. New application code must provide them through this constructor.
178 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
179 constructor(
180 props: ProjectEntriesFailureContext & {
181 readonly cwd: string;
182 readonly queryLength: number;
183 readonly limit: number;
184 },
185 ) {
186 super({
187 ...props,
188 message:
189 decodedProjectErrorMessage(props) ??
190 `Failed to search workspace entries in '${props.cwd}'.`,
191 } as any);
192 }
194​
195export class ProjectListEntriesError extends Schema.TaggedErrorClass<ProjectListEntriesError>()(
196 "ProjectListEntriesError",
197 {
198 cwd: Schema.optional(TrimmedNonEmptyString),
199 failure: Schema.optional(ProjectEntriesFailure),
200 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
201 timeout: Schema.optional(TrimmedNonEmptyString),
202 detail: Schema.optional(TrimmedNonEmptyString),
203 message: TrimmedNonEmptyString,
204 cause: Schema.optional(Schema.Defect()),
205 },
206) {
207 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
208 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
209 super({
210 ...props,
211 message:
212 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
213 } as any);
214 }
216​
217export const ProjectReadFileInput = Schema.Struct({
218 cwd: TrimmedNonEmptyString,
219 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
220});
221export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
222​
223export const ProjectReadFileResult = Schema.Struct({
224 relativePath: TrimmedNonEmptyString,
225 contents: Schema.String,
226 byteLength: NonNegativeInt,
227 truncated: Schema.Boolean,
228});
229export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
230​
231export const ProjectFileFailure = Schema.Literals([
232 "workspace_path_outside_root",
233 "resolved_path_outside_root",
234 "path_not_file",
235 "binary_file",
236 "operation_failed",
237]);
238export type ProjectFileFailure = typeof ProjectFileFailure.Type;
239​
240export const ProjectFileOperation = Schema.Literals([
241 "realpath-workspace-root",
242 "realpath-target",
243 "open",
244 "stat",
245 "read",
246 "close",
247 "make-directory",
248 "write-file",
249]);
250export type ProjectFileOperation = typeof ProjectFileOperation.Type;
251​
252type ProjectFileFailureContext = {
253 readonly cwd: string;
254 readonly relativePath: string;
255 readonly failure: ProjectFileFailure;
256 readonly resolvedPath?: string;
257 readonly resolvedWorkspaceRoot?: string;
258 readonly operation?: ProjectFileOperation;
259 readonly operationPath?: string;
260 readonly cause?: unknown;
261};
262​
263export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
264 "ProjectReadFileError",
265 {
266 cwd: Schema.optional(TrimmedNonEmptyString),
267 relativePath: Schema.optional(TrimmedNonEmptyString),
268 failure: Schema.optional(ProjectFileFailure),
269 resolvedPath: Schema.optional(TrimmedNonEmptyString),
270 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
271 operation: Schema.optional(ProjectFileOperation),
272 operationPath: Schema.optional(TrimmedNonEmptyString),
273 message: TrimmedNonEmptyString,
274 cause: Schema.optional(Schema.Defect()),
275 },
276) {
277 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
278 constructor(props: ProjectFileFailureContext) {
279 super({
280 ...props,
281 message:
282 decodedProjectErrorMessage(props) ??
283 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
284 } as any);
285 }
287​
288export const ProjectWriteFileInput = Schema.Struct({
289 cwd: TrimmedNonEmptyString,
290 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
291 contents: Schema.String,
292});
293export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
294​
295export const ProjectWriteFileResult = Schema.Struct({
296 relativePath: TrimmedNonEmptyString,
297});
298export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
299​
300export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
301 "ProjectWriteFileError",
302 {
303 cwd: Schema.optional(TrimmedNonEmptyString),
304 relativePath: Schema.optional(TrimmedNonEmptyString),
305 failure: Schema.optional(ProjectFileFailure),
306 resolvedPath: Schema.optional(TrimmedNonEmptyString),
307 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
308 operation: Schema.optional(ProjectFileOperation),
309 operationPath: Schema.optional(TrimmedNonEmptyString),
310 message: TrimmedNonEmptyString,
311 cause: Schema.optional(Schema.Defect()),
312 },
313) {
314 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
315 constructor(props: ProjectFileFailureContext) {
316 super({
317 ...props,
318 message:
319 decodedProjectErrorMessage(props) ??
320 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
321 } as any);
322 }