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

project.ts

225 lines7.6 KB

1import * as Schema from "effect/Schema";
2import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3​
4const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
5const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
6const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
7​
8export const ProjectSearchEntriesInput = Schema.Struct({
9 cwd: TrimmedNonEmptyString,
10 query: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
11 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
12});
13export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
14​
15const ProjectEntryKind = Schema.Literals(["file", "directory"]);
16​
17export const ProjectEntry = Schema.Struct({
18 path: TrimmedNonEmptyString,
19 kind: ProjectEntryKind,
20});
21export type ProjectEntry = typeof ProjectEntry.Type;
22​
23export const ProjectSearchEntriesResult = Schema.Struct({
24 entries: Schema.Array(ProjectEntry),
25 truncated: Schema.Boolean,
26});
27export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
28​
29export const ProjectListEntriesInput = Schema.Struct({
30 cwd: TrimmedNonEmptyString,
31});
32export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
33​
34export const ProjectListEntriesResult = Schema.Struct({
35 entries: Schema.Array(ProjectEntry),
36 truncated: Schema.Boolean,
37});
38export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
39​
40export const ProjectEntriesFailure = Schema.Literals([
41 "workspace_root_not_found",
42 "workspace_root_create_failed",
43 "workspace_root_stat_failed",
44 "workspace_root_not_directory",
45 "search_index_create_failed",
46 "search_index_scan_timed_out",
47 "search_index_search_failed",
48]);
49export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
50​
51type ProjectEntriesFailureContext = {
52 readonly failure: ProjectEntriesFailure;
53 readonly normalizedCwd?: string;
54 readonly timeout?: string;
55 readonly detail?: string;
56 readonly cause?: unknown;
57};
58​
59function decodedProjectErrorMessage(props: object): string | undefined {
60 if (!("message" in props)) return undefined;
61 return typeof props.message === "string" ? props.message : undefined;
63​
64export class ProjectSearchEntriesError extends Schema.TaggedErrorClass<ProjectSearchEntriesError>()(
65 "ProjectSearchEntriesError",
66 {
67 cwd: Schema.optional(TrimmedNonEmptyString),
68 queryLength: Schema.optional(NonNegativeInt),
69 limit: Schema.optional(PositiveInt),
70 failure: Schema.optional(ProjectEntriesFailure),
71 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
72 timeout: Schema.optional(TrimmedNonEmptyString),
73 detail: Schema.optional(TrimmedNonEmptyString),
74 message: TrimmedNonEmptyString,
75 cause: Schema.optional(Schema.Defect()),
76 },
77) {
78 // The structured fields are optional on the wire so newer peers can decode legacy message-only
79 // failures. New application code must provide them through this constructor.
80 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
81 constructor(
82 props: ProjectEntriesFailureContext & {
83 readonly cwd: string;
84 readonly queryLength: number;
85 readonly limit: number;
86 },
87 ) {
88 super({
89 ...props,
90 message:
91 decodedProjectErrorMessage(props) ??
92 `Failed to search workspace entries in '${props.cwd}'.`,
93 } as any);
94 }
96​
97export class ProjectListEntriesError extends Schema.TaggedErrorClass<ProjectListEntriesError>()(
98 "ProjectListEntriesError",
99 {
100 cwd: Schema.optional(TrimmedNonEmptyString),
101 failure: Schema.optional(ProjectEntriesFailure),
102 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
103 timeout: Schema.optional(TrimmedNonEmptyString),
104 detail: Schema.optional(TrimmedNonEmptyString),
105 message: TrimmedNonEmptyString,
106 cause: Schema.optional(Schema.Defect()),
107 },
108) {
109 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
110 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
111 super({
112 ...props,
113 message:
114 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
115 } as any);
116 }
118​
119export const ProjectReadFileInput = Schema.Struct({
120 cwd: TrimmedNonEmptyString,
121 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
122});
123export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
124​
125export const ProjectReadFileResult = Schema.Struct({
126 relativePath: TrimmedNonEmptyString,
127 contents: Schema.String,
128 byteLength: NonNegativeInt,
129 truncated: Schema.Boolean,
130});
131export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
132​
133export const ProjectFileFailure = Schema.Literals([
134 "workspace_path_outside_root",
135 "resolved_path_outside_root",
136 "path_not_file",
137 "binary_file",
138 "operation_failed",
139]);
140export type ProjectFileFailure = typeof ProjectFileFailure.Type;
141​
142export const ProjectFileOperation = Schema.Literals([
143 "realpath-workspace-root",
144 "realpath-target",
145 "open",
146 "stat",
147 "read",
148 "close",
149 "make-directory",
150 "write-file",
151]);
152export type ProjectFileOperation = typeof ProjectFileOperation.Type;
153​
154type ProjectFileFailureContext = {
155 readonly cwd: string;
156 readonly relativePath: string;
157 readonly failure: ProjectFileFailure;
158 readonly resolvedPath?: string;
159 readonly resolvedWorkspaceRoot?: string;
160 readonly operation?: ProjectFileOperation;
161 readonly operationPath?: string;
162 readonly cause?: unknown;
163};
164​
165export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
166 "ProjectReadFileError",
167 {
168 cwd: Schema.optional(TrimmedNonEmptyString),
169 relativePath: Schema.optional(TrimmedNonEmptyString),
170 failure: Schema.optional(ProjectFileFailure),
171 resolvedPath: Schema.optional(TrimmedNonEmptyString),
172 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
173 operation: Schema.optional(ProjectFileOperation),
174 operationPath: Schema.optional(TrimmedNonEmptyString),
175 message: TrimmedNonEmptyString,
176 cause: Schema.optional(Schema.Defect()),
177 },
178) {
179 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
180 constructor(props: ProjectFileFailureContext) {
181 super({
182 ...props,
183 message:
184 decodedProjectErrorMessage(props) ??
185 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
186 } as any);
187 }
189​
190export const ProjectWriteFileInput = Schema.Struct({
191 cwd: TrimmedNonEmptyString,
192 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
193 contents: Schema.String,
194});
195export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
196​
197export const ProjectWriteFileResult = Schema.Struct({
198 relativePath: TrimmedNonEmptyString,
199});
200export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
201​
202export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
203 "ProjectWriteFileError",
204 {
205 cwd: Schema.optional(TrimmedNonEmptyString),
206 relativePath: Schema.optional(TrimmedNonEmptyString),
207 failure: Schema.optional(ProjectFileFailure),
208 resolvedPath: Schema.optional(TrimmedNonEmptyString),
209 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
210 operation: Schema.optional(ProjectFileOperation),
211 operationPath: Schema.optional(TrimmedNonEmptyString),
212 message: TrimmedNonEmptyString,
213 cause: Schema.optional(Schema.Defect()),
214 },
215) {
216 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
217 constructor(props: ProjectFileFailureContext) {
218 super({
219 ...props,
220 message:
221 decodedProjectErrorMessage(props) ??
222 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
223 } as any);
224 }