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