Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3
4const FILESYSTEM_PATH_MAX_LENGTH = 512;
5
6export const FilesystemBrowseInput = Schema.Struct({
7 partialPath: TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH)),
8 cwd: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH))),
9});
10export type FilesystemBrowseInput = typeof FilesystemBrowseInput.Type;
11
12export const FilesystemBrowseEntry = Schema.Struct({
13 name: TrimmedNonEmptyString,
14 fullPath: TrimmedNonEmptyString,
15});
16export type FilesystemBrowseEntry = typeof FilesystemBrowseEntry.Type;
17
18export const FilesystemBrowseResult = Schema.Struct({
19 parentPath: TrimmedNonEmptyString,
20 entries: Schema.Array(FilesystemBrowseEntry),
21});
22export type FilesystemBrowseResult = typeof FilesystemBrowseResult.Type;
23
24export const FilesystemBrowseFailure = Schema.Literals([
25 "windows_path_unsupported",
26 "current_project_required",
27 "read_directory_failed",
28]);
29export type FilesystemBrowseFailure = typeof FilesystemBrowseFailure.Type;
30
31function decodedFilesystemBrowseErrorMessage(props: object): string | undefined {
32 if (!("message" in props)) return undefined;
33 return typeof props.message === "string" ? props.message : undefined;
34}
35
36export class FilesystemBrowseError extends Schema.TaggedError<FilesystemBrowseError>()(
37 "FilesystemBrowseError",
38 {
39 partialPath: Schema.optional(TrimmedNonEmptyString),
40 cwd: Schema.optional(TrimmedNonEmptyString),
41 failure: Schema.optional(FilesystemBrowseFailure),
42 parentPath: Schema.optional(TrimmedNonEmptyString),
43 platform: Schema.optional(TrimmedNonEmptyString),
44 message: TrimmedNonEmptyString,
45 cause: Schema.optional(Schema.Defect()),
46 },
47) {
48 // Structured diagnostics stay optional for rolling compatibility with legacy message-only
49 // payloads, while new call sites must provide the request context and failure classification.
50 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
51 constructor(props: {
52 readonly partialPath: string;
53 readonly cwd?: string | undefined;
54 readonly failure: FilesystemBrowseFailure;
55 readonly parentPath?: string;
56 readonly platform?: string;
57 readonly cause?: unknown;
58 }) {
59 const cwd = props.cwd === undefined ? "" : ` from '${props.cwd}'`;
60 super({
61 ...props,
62 message:
63 decodedFilesystemBrowseErrorMessage(props) ??
64 `Failed to browse filesystem path '${props.partialPath}'${cwd}.`,
65 } as any);
66 }
67}