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

project.ts

299 lines10.1 KB

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