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

project.ts

300 lines10.2 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 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;
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 }
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 }
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 }
193​
194export const ProjectReadFileInput = Schema.Struct({
195 cwd: TrimmedNonEmptyString,
196 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
197});
198export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
199​
200export const ProjectReadFileResult = Schema.Struct({
201 relativePath: TrimmedNonEmptyString,
202 contents: Schema.String,
203 byteLength: NonNegativeInt,
204 truncated: Schema.Boolean,
205});
206export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
207​
208export const ProjectFileFailure = Schema.Literals([
209 "workspace_path_outside_root",
210 "resolved_path_outside_root",
211 "path_not_file",
212 "binary_file",
213 "operation_failed",
214]);
215export type ProjectFileFailure = typeof ProjectFileFailure.Type;
216​
217export const ProjectFileOperation = Schema.Literals([
218 "realpath-workspace-root",
219 "realpath-target",
220 "open",
221 "stat",
222 "read",
223 "close",
224 "make-directory",
225 "write-file",
226]);
227export type ProjectFileOperation = typeof ProjectFileOperation.Type;
228​
229type ProjectFileFailureContext = {
230 readonly cwd: string;
231 readonly relativePath: string;
232 readonly failure: ProjectFileFailure;
233 readonly resolvedPath?: string;
234 readonly resolvedWorkspaceRoot?: string;
235 readonly operation?: ProjectFileOperation;
236 readonly operationPath?: string;
237 readonly cause?: unknown;
238};
239​
240export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
241 "ProjectReadFileError",
242 {
243 cwd: Schema.optional(TrimmedNonEmptyString),
244 relativePath: Schema.optional(TrimmedNonEmptyString),
245 failure: Schema.optional(ProjectFileFailure),
246 resolvedPath: Schema.optional(TrimmedNonEmptyString),
247 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
248 operation: Schema.optional(ProjectFileOperation),
249 operationPath: Schema.optional(TrimmedNonEmptyString),
250 message: TrimmedNonEmptyString,
251 cause: Schema.optional(Schema.Defect()),
252 },
253) {
254 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
255 constructor(props: ProjectFileFailureContext) {
256 super({
257 ...props,
258 message:
259 decodedProjectErrorMessage(props) ??
260 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
261 } as any);
262 }
264​
265export const ProjectWriteFileInput = Schema.Struct({
266 cwd: TrimmedNonEmptyString,
267 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
268 contents: Schema.String,
269});
270export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
271​
272export const ProjectWriteFileResult = Schema.Struct({
273 relativePath: TrimmedNonEmptyString,
274});
275export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
276​
277export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
278 "ProjectWriteFileError",
279 {
280 cwd: Schema.optional(TrimmedNonEmptyString),
281 relativePath: Schema.optional(TrimmedNonEmptyString),
282 failure: Schema.optional(ProjectFileFailure),
283 resolvedPath: Schema.optional(TrimmedNonEmptyString),
284 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
285 operation: Schema.optional(ProjectFileOperation),
286 operationPath: Schema.optional(TrimmedNonEmptyString),
287 message: TrimmedNonEmptyString,
288 cause: Schema.optional(Schema.Defect()),
289 },
290) {
291 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
292 constructor(props: ProjectFileFailureContext) {
293 super({
294 ...props,
295 message:
296 decodedProjectErrorMessage(props) ??
297 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
298 } as any);
299 }