Open History to see the last commit
1import * as Schema from "effect/Schema";
2import {
3 NonNegativeInt,
4 PositiveInt,
5 ProjectId,
6 TrimmedNonEmptyString,
7 TrimmedString,
8} from "./baseSchemas.ts";
9
10const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
11const PROJECT_SEARCH_CONTENTS_MAX_LIMIT = 500;
12const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
13const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
14
15export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
16export type ProjectEntryKind = typeof ProjectEntryKind.Type;
17
18export const ProjectSearchEntriesInput = Schema.Struct({
19 cwd: TrimmedNonEmptyString,
20 // An empty query is a bounded browse: the index returns frecency-ordered
21 // entries, which the file picker uses for its initial results.
22 query: TrimmedString.check(Schema.isMaxLength(256)),
23 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
24 kind: Schema.optional(ProjectEntryKind),
25 imageOnly: Schema.optional(Schema.Boolean),
26});
27export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
28
29export const ProjectEntry = Schema.Struct({
30 path: TrimmedNonEmptyString,
31 kind: ProjectEntryKind,
32 ignored: Schema.optional(Schema.Boolean),
33});
34export type ProjectEntry = typeof ProjectEntry.Type;
35
36export const ProjectSearchEntriesResult = Schema.Struct({
37 entries: Schema.Array(ProjectEntry),
38 truncated: Schema.Boolean,
39});
40export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
41
42export const ProjectSearchContentsInput = Schema.Struct({
43 cwd: TrimmedNonEmptyString,
44 // Whitespace is significant in content queries (" foo", regex trailing
45 // spaces), so the query is deliberately not trimmed on the wire.
46 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
47 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
48 caseSensitive: Schema.Boolean,
49 wholeWord: Schema.Boolean,
50 useRegex: Schema.Boolean,
51});
52export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
53
54export const ProjectContentMatchRange = Schema.Struct({
55 start: NonNegativeInt,
56 end: NonNegativeInt,
57});
58export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
59
60export const ProjectContentMatch = Schema.Struct({
61 path: TrimmedNonEmptyString,
62 lineNumber: PositiveInt,
63 lineContent: Schema.String,
64 matchRanges: Schema.Array(ProjectContentMatchRange),
65});
66export type ProjectContentMatch = typeof ProjectContentMatch.Type;
67
68export const ProjectSearchContentsResult = Schema.Struct({
69 matches: Schema.Array(ProjectContentMatch),
70 truncated: Schema.Boolean,
71 regexFallbackError: Schema.optional(Schema.String),
72});
73export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
74
75export const ProjectListEntriesInput = Schema.Struct({
76 cwd: TrimmedNonEmptyString,
77 // Present for immediate filesystem children, including ignored entries; empty means root.
78 // Omitted preserves the indexed recursive listing used by older clients.
79 directoryPath: Schema.optional(TrimmedString),
80});
81export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
82
83export const ProjectListEntriesResult = Schema.Struct({
84 entries: Schema.Array(ProjectEntry),
85 truncated: Schema.Boolean,
86});
87export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
88
89export const ProjectEntriesFailure = Schema.Literals([
90 "workspace_root_not_found",
91 "workspace_root_create_failed",
92 "workspace_root_stat_failed",
93 "workspace_root_not_directory",
94 "search_index_create_failed",
95 "search_index_scan_timed_out",
96 "search_index_search_failed",
97 "directory_list_failed",
98]);
99export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
100
101type ProjectEntriesFailureContext = {
102 readonly failure: ProjectEntriesFailure;
103 readonly normalizedCwd?: string;
104 readonly timeout?: string;
105 readonly detail?: string;
106 readonly cause?: unknown;
107};
108
109function decodedProjectErrorMessage(props: object): string | undefined {
110 if (!("message" in props)) return undefined;
111 return typeof props.message === "string" ? props.message : undefined;
112}
113
114export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
115 "ProjectSearchEntriesError",
116 {
117 cwd: Schema.optional(TrimmedNonEmptyString),
118 queryLength: Schema.optional(NonNegativeInt),
119 limit: Schema.optional(PositiveInt),
120 failure: Schema.optional(ProjectEntriesFailure),
121 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
122 timeout: Schema.optional(TrimmedNonEmptyString),
123 detail: Schema.optional(TrimmedNonEmptyString),
124 message: TrimmedNonEmptyString,
125 cause: Schema.optional(Schema.Defect()),
126 },
127) {
128 // The structured fields are optional on the wire so newer peers can decode legacy message-only
129 // failures. New application code must provide them through this constructor.
130 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
131 constructor(
132 props: ProjectEntriesFailureContext & {
133 readonly cwd: string;
134 readonly queryLength: number;
135 readonly limit: number;
136 },
137 ) {
138 super({
139 ...props,
140 message:
141 decodedProjectErrorMessage(props) ??
142 `Failed to search workspace entries in '${props.cwd}'.`,
143 } as any);
144 }
145}
146
147export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
148 "ProjectSearchContentsError",
149 {
150 cwd: Schema.optional(TrimmedNonEmptyString),
151 queryLength: Schema.optional(NonNegativeInt),
152 limit: Schema.optional(PositiveInt),
153 failure: Schema.optional(ProjectEntriesFailure),
154 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
155 timeout: Schema.optional(TrimmedNonEmptyString),
156 detail: Schema.optional(TrimmedNonEmptyString),
157 message: TrimmedNonEmptyString,
158 cause: Schema.optional(Schema.Defect()),
159 },
160) {
161 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
162 constructor(
163 props: ProjectEntriesFailureContext & {
164 readonly cwd: string;
165 readonly queryLength: number;
166 readonly limit: number;
167 },
168 ) {
169 super({
170 ...props,
171 message:
172 decodedProjectErrorMessage(props) ??
173 `Failed to search workspace contents in '${props.cwd}'.`,
174 } as any);
175 }
176}
177
178export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
179 "ProjectListEntriesError",
180 {
181 cwd: Schema.optional(TrimmedNonEmptyString),
182 failure: Schema.optional(ProjectEntriesFailure),
183 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
184 timeout: Schema.optional(TrimmedNonEmptyString),
185 detail: Schema.optional(TrimmedNonEmptyString),
186 message: TrimmedNonEmptyString,
187 cause: Schema.optional(Schema.Defect()),
188 },
189) {
190 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
191 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
192 super({
193 ...props,
194 message:
195 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
196 } as any);
197 }
198}
199
200export const ProjectReadFileInput = Schema.Struct({
201 cwd: TrimmedNonEmptyString,
202 // Workspace-relative, or an absolute host path for a file outside the
203 // workspace. Only workspace-relative paths can be written back.
204 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
205});
206export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
207
208export const ProjectReadFileResult = Schema.Struct({
209 relativePath: TrimmedNonEmptyString,
210 contents: Schema.String,
211 byteLength: NonNegativeInt,
212 truncated: Schema.Boolean,
213});
214export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
215
216export const ProjectFileFailure = Schema.Literals([
217 "workspace_path_outside_root",
218 "resolved_path_outside_root",
219 "path_not_file",
220 "binary_file",
221 "operation_failed",
222]);
223export type ProjectFileFailure = typeof ProjectFileFailure.Type;
224
225export const ProjectFileOperation = Schema.Literals([
226 "realpath-workspace-root",
227 "realpath-target",
228 "open",
229 "stat",
230 "read",
231 "close",
232 "make-directory",
233 "write-file",
234]);
235export type ProjectFileOperation = typeof ProjectFileOperation.Type;
236
237type ProjectFileFailureContext = {
238 readonly cwd: string;
239 readonly relativePath: string;
240 readonly failure: ProjectFileFailure;
241 readonly resolvedPath?: string;
242 readonly resolvedWorkspaceRoot?: string;
243 readonly operation?: ProjectFileOperation;
244 readonly operationPath?: string;
245 readonly cause?: unknown;
246};
247
248export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
249 "ProjectReadFileError",
250 {
251 cwd: Schema.optional(TrimmedNonEmptyString),
252 relativePath: Schema.optional(TrimmedNonEmptyString),
253 failure: Schema.optional(ProjectFileFailure),
254 resolvedPath: Schema.optional(TrimmedNonEmptyString),
255 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
256 operation: Schema.optional(ProjectFileOperation),
257 operationPath: Schema.optional(TrimmedNonEmptyString),
258 message: TrimmedNonEmptyString,
259 cause: Schema.optional(Schema.Defect()),
260 },
261) {
262 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
263 constructor(props: ProjectFileFailureContext) {
264 super({
265 ...props,
266 message:
267 decodedProjectErrorMessage(props) ??
268 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
269 } as any);
270 }
271}
272
273export const ProjectWriteFileInput = Schema.Struct({
274 cwd: TrimmedNonEmptyString,
275 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
276 contents: Schema.String,
277});
278export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
279
280export const ProjectWriteFileResult = Schema.Struct({
281 relativePath: TrimmedNonEmptyString,
282});
283export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
284
285/** The environment's Scratch project, created on first request. */
286export const ProjectEnsureScratchResult = Schema.Struct({
287 projectId: ProjectId,
288});
289export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
290
291export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
292 "ProjectWriteFileError",
293 {
294 cwd: Schema.optional(TrimmedNonEmptyString),
295 relativePath: Schema.optional(TrimmedNonEmptyString),
296 failure: Schema.optional(ProjectFileFailure),
297 resolvedPath: Schema.optional(TrimmedNonEmptyString),
298 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
299 operation: Schema.optional(ProjectFileOperation),
300 operationPath: Schema.optional(TrimmedNonEmptyString),
301 message: TrimmedNonEmptyString,
302 cause: Schema.optional(Schema.Defect()),
303 },
304) {
305 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
306 constructor(props: ProjectFileFailureContext) {
307 super({
308 ...props,
309 message:
310 decodedProjectErrorMessage(props) ??
311 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
312 } as any);
313 }
314}