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

project.ts

393 lines13.0 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});
119export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
120​
121export const ProjectEntry = Schema.Struct({
122 path: TrimmedNonEmptyString,
123 kind: ProjectEntryKind,
124});
125export type ProjectEntry = typeof ProjectEntry.Type;
126​
127export const ProjectSearchEntriesResult = Schema.Struct({
128 entries: Schema.Array(ProjectEntry),
129 truncated: Schema.Boolean,
130});
131export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
132​
133export const ProjectSearchContentsInput = Schema.Struct({
134 cwd: TrimmedNonEmptyString,
135 // Whitespace is significant in content queries (" foo", regex trailing
136 // spaces), so the query is deliberately not trimmed on the wire.
137 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
138 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
139 caseSensitive: Schema.Boolean,
140 wholeWord: Schema.Boolean,
141 useRegex: Schema.Boolean,
142});
143export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
144​
145export const ProjectContentMatchRange = Schema.Struct({
146 start: NonNegativeInt,
147 end: NonNegativeInt,
148});
149export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
150​
151export const ProjectContentMatch = Schema.Struct({
152 path: TrimmedNonEmptyString,
153 lineNumber: PositiveInt,
154 lineContent: Schema.String,
155 matchRanges: Schema.Array(ProjectContentMatchRange),
156});
157export type ProjectContentMatch = typeof ProjectContentMatch.Type;
158​
159export const ProjectSearchContentsResult = Schema.Struct({
160 matches: Schema.Array(ProjectContentMatch),
161 truncated: Schema.Boolean,
162 regexFallbackError: Schema.optional(Schema.String),
163});
164export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
165​
166export const ProjectListEntriesInput = Schema.Struct({
167 cwd: TrimmedNonEmptyString,
168});
169export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
170​
171export const ProjectListEntriesResult = Schema.Struct({
172 entries: Schema.Array(ProjectEntry),
173 truncated: Schema.Boolean,
174});
175export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
176​
177export const ProjectEntriesFailure = Schema.Literals([
178 "workspace_root_not_found",
179 "workspace_root_create_failed",
180 "workspace_root_stat_failed",
181 "workspace_root_not_directory",
182 "search_index_create_failed",
183 "search_index_scan_timed_out",
184 "search_index_search_failed",
185]);
186export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
187​
188type ProjectEntriesFailureContext = {
189 readonly failure: ProjectEntriesFailure;
190 readonly normalizedCwd?: string;
191 readonly timeout?: string;
192 readonly detail?: string;
193 readonly cause?: unknown;
194};
195​
196function decodedProjectErrorMessage(props: object): string | undefined {
197 if (!("message" in props)) return undefined;
198 return typeof props.message === "string" ? props.message : undefined;
200​
201export class ProjectSearchEntriesError extends Schema.TaggedErrorClass<ProjectSearchEntriesError>()(
202 "ProjectSearchEntriesError",
203 {
204 cwd: Schema.optional(TrimmedNonEmptyString),
205 queryLength: Schema.optional(NonNegativeInt),
206 limit: Schema.optional(PositiveInt),
207 failure: Schema.optional(ProjectEntriesFailure),
208 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
209 timeout: Schema.optional(TrimmedNonEmptyString),
210 detail: Schema.optional(TrimmedNonEmptyString),
211 message: TrimmedNonEmptyString,
212 cause: Schema.optional(Schema.Defect()),
213 },
214) {
215 // The structured fields are optional on the wire so newer peers can decode legacy message-only
216 // failures. New application code must provide them through this constructor.
217 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
218 constructor(
219 props: ProjectEntriesFailureContext & {
220 readonly cwd: string;
221 readonly queryLength: number;
222 readonly limit: number;
223 },
224 ) {
225 super({
226 ...props,
227 message:
228 decodedProjectErrorMessage(props) ??
229 `Failed to search workspace entries in '${props.cwd}'.`,
230 } as any);
231 }
233​
234export class ProjectSearchContentsError extends Schema.TaggedErrorClass<ProjectSearchContentsError>()(
235 "ProjectSearchContentsError",
236 {
237 cwd: Schema.optional(TrimmedNonEmptyString),
238 queryLength: Schema.optional(NonNegativeInt),
239 limit: Schema.optional(PositiveInt),
240 failure: Schema.optional(ProjectEntriesFailure),
241 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
242 timeout: Schema.optional(TrimmedNonEmptyString),
243 detail: Schema.optional(TrimmedNonEmptyString),
244 message: TrimmedNonEmptyString,
245 cause: Schema.optional(Schema.Defect()),
246 },
247) {
248 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
249 constructor(
250 props: ProjectEntriesFailureContext & {
251 readonly cwd: string;
252 readonly queryLength: number;
253 readonly limit: number;
254 },
255 ) {
256 super({
257 ...props,
258 message:
259 decodedProjectErrorMessage(props) ??
260 `Failed to search workspace contents in '${props.cwd}'.`,
261 } as any);
262 }
264​
265export class ProjectListEntriesError extends Schema.TaggedErrorClass<ProjectListEntriesError>()(
266 "ProjectListEntriesError",
267 {
268 cwd: Schema.optional(TrimmedNonEmptyString),
269 failure: Schema.optional(ProjectEntriesFailure),
270 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
271 timeout: Schema.optional(TrimmedNonEmptyString),
272 detail: Schema.optional(TrimmedNonEmptyString),
273 message: TrimmedNonEmptyString,
274 cause: Schema.optional(Schema.Defect()),
275 },
276) {
277 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
278 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
279 super({
280 ...props,
281 message:
282 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
283 } as any);
284 }
286​
287export const ProjectReadFileInput = Schema.Struct({
288 cwd: TrimmedNonEmptyString,
289 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
290});
291export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
292​
293export const ProjectReadFileResult = Schema.Struct({
294 relativePath: TrimmedNonEmptyString,
295 contents: Schema.String,
296 byteLength: NonNegativeInt,
297 truncated: Schema.Boolean,
298});
299export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
300​
301export const ProjectFileFailure = Schema.Literals([
302 "workspace_path_outside_root",
303 "resolved_path_outside_root",
304 "path_not_file",
305 "binary_file",
306 "operation_failed",
307]);
308export type ProjectFileFailure = typeof ProjectFileFailure.Type;
309​
310export const ProjectFileOperation = Schema.Literals([
311 "realpath-workspace-root",
312 "realpath-target",
313 "open",
314 "stat",
315 "read",
316 "close",
317 "make-directory",
318 "write-file",
319]);
320export type ProjectFileOperation = typeof ProjectFileOperation.Type;
321​
322type ProjectFileFailureContext = {
323 readonly cwd: string;
324 readonly relativePath: string;
325 readonly failure: ProjectFileFailure;
326 readonly resolvedPath?: string;
327 readonly resolvedWorkspaceRoot?: string;
328 readonly operation?: ProjectFileOperation;
329 readonly operationPath?: string;
330 readonly cause?: unknown;
331};
332​
333export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
334 "ProjectReadFileError",
335 {
336 cwd: Schema.optional(TrimmedNonEmptyString),
337 relativePath: Schema.optional(TrimmedNonEmptyString),
338 failure: Schema.optional(ProjectFileFailure),
339 resolvedPath: Schema.optional(TrimmedNonEmptyString),
340 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
341 operation: Schema.optional(ProjectFileOperation),
342 operationPath: Schema.optional(TrimmedNonEmptyString),
343 message: TrimmedNonEmptyString,
344 cause: Schema.optional(Schema.Defect()),
345 },
346) {
347 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
348 constructor(props: ProjectFileFailureContext) {
349 super({
350 ...props,
351 message:
352 decodedProjectErrorMessage(props) ??
353 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
354 } as any);
355 }
357​
358export const ProjectWriteFileInput = Schema.Struct({
359 cwd: TrimmedNonEmptyString,
360 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
361 contents: Schema.String,
362});
363export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
364​
365export const ProjectWriteFileResult = Schema.Struct({
366 relativePath: TrimmedNonEmptyString,
367});
368export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
369​
370export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
371 "ProjectWriteFileError",
372 {
373 cwd: Schema.optional(TrimmedNonEmptyString),
374 relativePath: Schema.optional(TrimmedNonEmptyString),
375 failure: Schema.optional(ProjectFileFailure),
376 resolvedPath: Schema.optional(TrimmedNonEmptyString),
377 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
378 operation: Schema.optional(ProjectFileOperation),
379 operationPath: Schema.optional(TrimmedNonEmptyString),
380 message: TrimmedNonEmptyString,
381 cause: Schema.optional(Schema.Defect()),
382 },
383) {
384 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
385 constructor(props: ProjectFileFailureContext) {
386 super({
387 ...props,
388 message:
389 decodedProjectErrorMessage(props) ??
390 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
391 } as any);
392 }