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

project.ts

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