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

project.ts

394 lines13.1 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 TrimmedString,
12} from "./baseSchemas.ts";
13​
14const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
15const PROJECT_SEARCH_CONTENTS_MAX_LIMIT = 500;
16const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
17const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
18​
19export const ProjectScriptIcon = Schema.Literals([
20 "play",
21 "test",
22 "lint",
23 "configure",
24 "build",
25 "debug",
26]);
27export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
28​
29export const ProjectScript = Schema.Struct({
30 id: TrimmedNonEmptyString,
31 name: TrimmedNonEmptyString,
32 command: TrimmedNonEmptyString,
33 icon: ProjectScriptIcon,
34 runOnWorktreeCreate: Schema.Boolean,
35 previewUrl: Schema.optional(TrimmedNonEmptyString),
36 autoOpenPreview: Schema.optional(Schema.Boolean),
37});
38export type ProjectScript = typeof ProjectScript.Type;
39​
40export const Project = Schema.Struct({
41 id: ProjectId,
42 title: TrimmedNonEmptyString,
43 workspaceRoot: TrimmedNonEmptyString,
44 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
45 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
46 defaultModelSelection: Schema.NullOr(ModelSelection),
47 scripts: Schema.Array(ProjectScript),
48 createdAt: IsoDateTime,
49 updatedAt: IsoDateTime,
50 deletedAt: Schema.NullOr(IsoDateTime),
51});
52export type Project = typeof Project.Type;
53​
54export const ProjectSnapshot = Schema.Struct({
55 projects: Schema.Array(Project),
56 updatedAt: IsoDateTime,
57});
58export type ProjectSnapshot = typeof ProjectSnapshot.Type;
59​
60export const ProjectChange = Schema.Union([
61 Schema.Struct({ type: Schema.Literal("project.upserted"), project: Project }),
62 Schema.Struct({
63 type: Schema.Literal("project.deleted"),
64 projectId: ProjectId,
65 deletedAt: IsoDateTime,
66 }),
67]);
68export type ProjectChange = typeof ProjectChange.Type;
69​
70export const ProjectMutation = Schema.Union([
71 Schema.Struct({
72 type: Schema.Literal("project.create"),
73 commandId: CommandId,
74 projectId: ProjectId,
75 title: TrimmedNonEmptyString,
76 workspaceRoot: TrimmedNonEmptyString,
77 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
78 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
79 scripts: Schema.optional(Schema.Array(ProjectScript)),
80 }),
81 Schema.Struct({
82 type: Schema.Literal("project.update"),
83 commandId: CommandId,
84 projectId: ProjectId,
85 title: Schema.optional(TrimmedNonEmptyString),
86 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
87 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
88 scripts: Schema.optional(Schema.Array(ProjectScript)),
89 }),
90 Schema.Struct({
91 type: Schema.Literal("project.delete"),
92 commandId: CommandId,
93 projectId: ProjectId,
94 force: Schema.optional(Schema.Boolean),
95 }),
96]);
97export type ProjectMutation = typeof ProjectMutation.Type;
98​
99export class ProjectMutationError extends Schema.TaggedErrorClass<ProjectMutationError>()(
100 "ProjectMutationError",
101 {
102 commandId: CommandId,
103 message: Schema.String,
104 cause: Schema.optional(Schema.Defect()),
105 },
106) {}
107​
108export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
109export type ProjectEntryKind = typeof ProjectEntryKind.Type;
110​
111export const ProjectSearchEntriesInput = Schema.Struct({
112 cwd: TrimmedNonEmptyString,
113 // An empty query is a bounded browse: the index returns frecency-ordered
114 // entries, which the file picker uses for its initial results.
115 query: TrimmedString.check(Schema.isMaxLength(256)),
116 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
117 kind: Schema.optional(ProjectEntryKind),
118 imageOnly: Schema.optional(Schema.Boolean),
119});
120export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
121​
122export const ProjectEntry = Schema.Struct({
123 path: TrimmedNonEmptyString,
124 kind: ProjectEntryKind,
125});
126export type ProjectEntry = typeof ProjectEntry.Type;
127​
128export const ProjectSearchEntriesResult = Schema.Struct({
129 entries: Schema.Array(ProjectEntry),
130 truncated: Schema.Boolean,
131});
132export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
133​
134export const ProjectSearchContentsInput = Schema.Struct({
135 cwd: TrimmedNonEmptyString,
136 // Whitespace is significant in content queries (" foo", regex trailing
137 // spaces), so the query is deliberately not trimmed on the wire.
138 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
139 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
140 caseSensitive: Schema.Boolean,
141 wholeWord: Schema.Boolean,
142 useRegex: Schema.Boolean,
143});
144export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
145​
146export const ProjectContentMatchRange = Schema.Struct({
147 start: NonNegativeInt,
148 end: NonNegativeInt,
149});
150export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
151​
152export const ProjectContentMatch = Schema.Struct({
153 path: TrimmedNonEmptyString,
154 lineNumber: PositiveInt,
155 lineContent: Schema.String,
156 matchRanges: Schema.Array(ProjectContentMatchRange),
157});
158export type ProjectContentMatch = typeof ProjectContentMatch.Type;
159​
160export const ProjectSearchContentsResult = Schema.Struct({
161 matches: Schema.Array(ProjectContentMatch),
162 truncated: Schema.Boolean,
163 regexFallbackError: Schema.optional(Schema.String),
164});
165export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
166​
167export const ProjectListEntriesInput = Schema.Struct({
168 cwd: TrimmedNonEmptyString,
169});
170export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
171​
172export const ProjectListEntriesResult = Schema.Struct({
173 entries: Schema.Array(ProjectEntry),
174 truncated: Schema.Boolean,
175});
176export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
177​
178export const ProjectEntriesFailure = Schema.Literals([
179 "workspace_root_not_found",
180 "workspace_root_create_failed",
181 "workspace_root_stat_failed",
182 "workspace_root_not_directory",
183 "search_index_create_failed",
184 "search_index_scan_timed_out",
185 "search_index_search_failed",
186]);
187export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
188​
189type ProjectEntriesFailureContext = {
190 readonly failure: ProjectEntriesFailure;
191 readonly normalizedCwd?: string;
192 readonly timeout?: string;
193 readonly detail?: string;
194 readonly cause?: unknown;
195};
196​
197function decodedProjectErrorMessage(props: object): string | undefined {
198 if (!("message" in props)) return undefined;
199 return typeof props.message === "string" ? props.message : undefined;
201​
202export class ProjectSearchEntriesError extends Schema.TaggedErrorClass<ProjectSearchEntriesError>()(
203 "ProjectSearchEntriesError",
204 {
205 cwd: Schema.optional(TrimmedNonEmptyString),
206 queryLength: Schema.optional(NonNegativeInt),
207 limit: Schema.optional(PositiveInt),
208 failure: Schema.optional(ProjectEntriesFailure),
209 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
210 timeout: Schema.optional(TrimmedNonEmptyString),
211 detail: Schema.optional(TrimmedNonEmptyString),
212 message: TrimmedNonEmptyString,
213 cause: Schema.optional(Schema.Defect()),
214 },
215) {
216 // The structured fields are optional on the wire so newer peers can decode legacy message-only
217 // failures. New application code must provide them through this constructor.
218 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
219 constructor(
220 props: ProjectEntriesFailureContext & {
221 readonly cwd: string;
222 readonly queryLength: number;
223 readonly limit: number;
224 },
225 ) {
226 super({
227 ...props,
228 message:
229 decodedProjectErrorMessage(props) ??
230 `Failed to search workspace entries in '${props.cwd}'.`,
231 } as any);
232 }
234​
235export class ProjectSearchContentsError extends Schema.TaggedErrorClass<ProjectSearchContentsError>()(
236 "ProjectSearchContentsError",
237 {
238 cwd: Schema.optional(TrimmedNonEmptyString),
239 queryLength: Schema.optional(NonNegativeInt),
240 limit: Schema.optional(PositiveInt),
241 failure: Schema.optional(ProjectEntriesFailure),
242 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
243 timeout: Schema.optional(TrimmedNonEmptyString),
244 detail: Schema.optional(TrimmedNonEmptyString),
245 message: TrimmedNonEmptyString,
246 cause: Schema.optional(Schema.Defect()),
247 },
248) {
249 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
250 constructor(
251 props: ProjectEntriesFailureContext & {
252 readonly cwd: string;
253 readonly queryLength: number;
254 readonly limit: number;
255 },
256 ) {
257 super({
258 ...props,
259 message:
260 decodedProjectErrorMessage(props) ??
261 `Failed to search workspace contents in '${props.cwd}'.`,
262 } as any);
263 }
265​
266export class ProjectListEntriesError extends Schema.TaggedErrorClass<ProjectListEntriesError>()(
267 "ProjectListEntriesError",
268 {
269 cwd: Schema.optional(TrimmedNonEmptyString),
270 failure: Schema.optional(ProjectEntriesFailure),
271 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
272 timeout: Schema.optional(TrimmedNonEmptyString),
273 detail: Schema.optional(TrimmedNonEmptyString),
274 message: TrimmedNonEmptyString,
275 cause: Schema.optional(Schema.Defect()),
276 },
277) {
278 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
279 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
280 super({
281 ...props,
282 message:
283 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
284 } as any);
285 }
287​
288export const ProjectReadFileInput = Schema.Struct({
289 cwd: TrimmedNonEmptyString,
290 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
291});
292export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
293​
294export const ProjectReadFileResult = Schema.Struct({
295 relativePath: TrimmedNonEmptyString,
296 contents: Schema.String,
297 byteLength: NonNegativeInt,
298 truncated: Schema.Boolean,
299});
300export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
301​
302export const ProjectFileFailure = Schema.Literals([
303 "workspace_path_outside_root",
304 "resolved_path_outside_root",
305 "path_not_file",
306 "binary_file",
307 "operation_failed",
308]);
309export type ProjectFileFailure = typeof ProjectFileFailure.Type;
310​
311export const ProjectFileOperation = Schema.Literals([
312 "realpath-workspace-root",
313 "realpath-target",
314 "open",
315 "stat",
316 "read",
317 "close",
318 "make-directory",
319 "write-file",
320]);
321export type ProjectFileOperation = typeof ProjectFileOperation.Type;
322​
323type ProjectFileFailureContext = {
324 readonly cwd: string;
325 readonly relativePath: string;
326 readonly failure: ProjectFileFailure;
327 readonly resolvedPath?: string;
328 readonly resolvedWorkspaceRoot?: string;
329 readonly operation?: ProjectFileOperation;
330 readonly operationPath?: string;
331 readonly cause?: unknown;
332};
333​
334export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
335 "ProjectReadFileError",
336 {
337 cwd: Schema.optional(TrimmedNonEmptyString),
338 relativePath: Schema.optional(TrimmedNonEmptyString),
339 failure: Schema.optional(ProjectFileFailure),
340 resolvedPath: Schema.optional(TrimmedNonEmptyString),
341 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
342 operation: Schema.optional(ProjectFileOperation),
343 operationPath: Schema.optional(TrimmedNonEmptyString),
344 message: TrimmedNonEmptyString,
345 cause: Schema.optional(Schema.Defect()),
346 },
347) {
348 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
349 constructor(props: ProjectFileFailureContext) {
350 super({
351 ...props,
352 message:
353 decodedProjectErrorMessage(props) ??
354 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
355 } as any);
356 }
358​
359export const ProjectWriteFileInput = Schema.Struct({
360 cwd: TrimmedNonEmptyString,
361 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
362 contents: Schema.String,
363});
364export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
365​
366export const ProjectWriteFileResult = Schema.Struct({
367 relativePath: TrimmedNonEmptyString,
368});
369export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
370​
371export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
372 "ProjectWriteFileError",
373 {
374 cwd: Schema.optional(TrimmedNonEmptyString),
375 relativePath: Schema.optional(TrimmedNonEmptyString),
376 failure: Schema.optional(ProjectFileFailure),
377 resolvedPath: Schema.optional(TrimmedNonEmptyString),
378 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
379 operation: Schema.optional(ProjectFileOperation),
380 operationPath: Schema.optional(TrimmedNonEmptyString),
381 message: TrimmedNonEmptyString,
382 cause: Schema.optional(Schema.Defect()),
383 },
384) {
385 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
386 constructor(props: ProjectFileFailureContext) {
387 super({
388 ...props,
389 message:
390 decodedProjectErrorMessage(props) ??
391 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
392 } as any);
393 }