Files
t3code/packages/contracts/src/filesystem.ts
T

93 lines
3.7 KiB
TypeScript

import * as Schema from "effect/Schema";
import { NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
const FILESYSTEM_PATH_MAX_LENGTH = 512;
export const FILESYSTEM_METADATA_BATCH_LIMIT = 64;
export const FilesystemGetMetadataInput = Schema.Struct({
// Absolute host paths, including ~/ paths. Relative paths need a workspace
// anchor on the client; the server never resolves them against its own cwd.
paths: Schema.Array(
TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH)),
).check(Schema.isMinLength(1), Schema.isMaxLength(FILESYSTEM_METADATA_BATCH_LIMIT)),
});
export type FilesystemGetMetadataInput = typeof FilesystemGetMetadataInput.Type;
export const FilesystemEntryMetadata = Schema.Struct({
kind: Schema.Literals(["file", "directory", "other"]),
byteLength: Schema.optionalKey(NonNegativeInt),
mimeType: Schema.optionalKey(TrimmedNonEmptyString),
});
export type FilesystemEntryMetadata = typeof FilesystemEntryMetadata.Type;
// Results follow request order so paths do not have to travel back over the wire.
// A null entry means the path is missing, unreadable, or unsupported.
export const FilesystemGetMetadataResult = Schema.Struct({
entries: Schema.Array(Schema.NullOr(FilesystemEntryMetadata)),
});
export type FilesystemGetMetadataResult = typeof FilesystemGetMetadataResult.Type;
export const FilesystemBrowseInput = Schema.Struct({
partialPath: TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH)),
cwd: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH))),
});
export type FilesystemBrowseInput = typeof FilesystemBrowseInput.Type;
export const FilesystemBrowseEntry = Schema.Struct({
name: TrimmedNonEmptyString,
fullPath: TrimmedNonEmptyString,
});
export type FilesystemBrowseEntry = typeof FilesystemBrowseEntry.Type;
export const FilesystemBrowseResult = Schema.Struct({
parentPath: TrimmedNonEmptyString,
entries: Schema.Array(FilesystemBrowseEntry),
});
export type FilesystemBrowseResult = typeof FilesystemBrowseResult.Type;
export const FilesystemBrowseFailure = Schema.Literals([
"windows_path_unsupported",
"current_project_required",
"read_directory_failed",
]);
export type FilesystemBrowseFailure = typeof FilesystemBrowseFailure.Type;
function decodedFilesystemBrowseErrorMessage(props: object): string | undefined {
if (!("message" in props)) return undefined;
return typeof props.message === "string" ? props.message : undefined;
}
export class FilesystemBrowseError extends Schema.TaggedError<FilesystemBrowseError>()(
"FilesystemBrowseError",
{
partialPath: Schema.optional(TrimmedNonEmptyString),
cwd: Schema.optional(TrimmedNonEmptyString),
failure: Schema.optional(FilesystemBrowseFailure),
parentPath: Schema.optional(TrimmedNonEmptyString),
platform: Schema.optional(TrimmedNonEmptyString),
message: TrimmedNonEmptyString,
cause: Schema.optional(Schema.Defect()),
},
) {
// Structured diagnostics stay optional for rolling compatibility with legacy message-only
// payloads, while new call sites must provide the request context and failure classification.
// @effect-diagnostics-next-line overriddenSchemaConstructor:off
constructor(props: {
readonly partialPath: string;
readonly cwd?: string | undefined;
readonly failure: FilesystemBrowseFailure;
readonly parentPath?: string;
readonly platform?: string;
readonly cause?: unknown;
}) {
const cwd = props.cwd === undefined ? "" : ` from '${props.cwd}'`;
super({
...props,
message:
decodedFilesystemBrowseErrorMessage(props) ??
`Failed to browse filesystem path '${props.partialPath}'${cwd}.`,
} as any);
}
}