export type StateEntryType = "file" | "directory" | "symlink"; export interface StateCapabilities { chmod: boolean; utimes: boolean; hardLinks: boolean; } export interface StateDirent { name: string; type: StateEntryType; } export interface StateStat { type: StateEntryType; size: number; mtime: Date; mode?: number; } export interface StateMkdirOptions { recursive?: boolean; } export interface StateRmOptions { recursive?: boolean; force?: boolean; } export interface StateCopyOptions { recursive?: boolean; } export interface StateMoveOptions { recursive?: boolean; } export interface StateJsonWriteOptions { spaces?: number; } export interface StateSearchOptions { caseSensitive?: boolean; regex?: boolean; wholeWord?: boolean; contextBefore?: number; contextAfter?: number; maxMatches?: number; } export interface StateTextMatch { line: number; column: number; match: string; lineText: string; beforeLines?: string[]; afterLines?: string[]; } export interface StateFindOptions { name?: string; pathPattern?: string; type?: StateEntryType | StateEntryType[]; minDepth?: number; maxDepth?: number; empty?: boolean; sizeMin?: number; sizeMax?: number; mtimeAfter?: string | Date; mtimeBefore?: string | Date; } export interface StateFindEntry { path: string; name: string; type: StateEntryType; depth: number; size: number; mtime: Date; } export type StateJsonUpdateOperation = | { op: "set"; path: string; value: unknown; } | { op: "delete"; path: string; }; export interface StateJsonUpdateResult { value: unknown; content: string; diff: string; operationsApplied: number; } export interface StateArchiveEntry { path: string; type: "file" | "directory"; size: number; } export interface StateArchiveCreateResult { path: string; entries: StateArchiveEntry[]; bytesWritten: number; } export interface StateArchiveExtractResult { destination: string; entries: StateArchiveEntry[]; } export interface StateCompressionResult { path: string; destination: string; bytesWritten: number; } export interface StateTreeOptions { maxDepth?: number; } export interface StateTreeNode { path: string; name: string; type: StateEntryType; size: number; children?: StateTreeNode[]; } export interface StateTreeSummary { files: number; directories: number; symlinks: number; totalBytes: number; maxDepth: number; } export interface StateFileDetection { mime: string; description: string; extension?: string; binary: boolean; } export interface StateHashOptions { algorithm?: "md5" | "sha1" | "sha256"; } export interface StateReplaceResult { replaced: number; content: string; } export interface StateFileSearchResult { path: string; matches: StateTextMatch[]; } export interface StateReplaceInFilesOptions extends StateSearchOptions { dryRun?: boolean; rollbackOnError?: boolean; } export interface StateFileReplaceResult { path: string; replaced: number; content: string; diff: string; } export interface StateReplaceInFilesResult { dryRun: boolean; files: StateFileReplaceResult[]; totalFiles: number; totalReplacements: number; } export interface StateEdit { path: string; content: string; } export interface StateWriteEditInstruction { kind: "write"; path: string; content: string; } export interface StateReplaceEditInstruction { kind: "replace"; path: string; search: string; replacement: string; options?: StateSearchOptions; } export interface StateWriteJsonEditInstruction { kind: "writeJson"; path: string; value: unknown; options?: StateJsonWriteOptions; } export type StateEditInstruction = | StateWriteEditInstruction | StateReplaceEditInstruction | StateWriteJsonEditInstruction; export interface StateApplyEditsOptions { dryRun?: boolean; rollbackOnError?: boolean; } export interface StateAppliedEditResult { path: string; changed: boolean; content: string; diff: string; } export interface StateApplyEditsResult { dryRun: boolean; edits: StateAppliedEditResult[]; totalChanged: number; } export interface StatePlannedEdit { instruction: StateEditInstruction; path: string; changed: boolean; content: string; diff: string; } export interface StateEditPlan { edits: StatePlannedEdit[]; totalChanged: number; totalInstructions: number; } export interface StateExecuteResult { result: unknown; error?: string; logs?: string[]; } export class StateBatchOperationError extends Error { readonly operation: "replaceInFiles" | "applyEdits"; readonly rolledBack: boolean; readonly rollbackError?: string; constructor(options: { operation: "replaceInFiles" | "applyEdits"; message: string; rolledBack: boolean; rollbackError?: string; }) { super(options.message); this.name = "StateBatchOperationError"; this.operation = options.operation; this.rolledBack = options.rolledBack; this.rollbackError = options.rollbackError; } } export interface StateExecutor { execute(code: string, backend: StateBackend): Promise; } export interface StateBackend { getCapabilities(): Promise; readFile(path: string): Promise; readFileBytes(path: string): Promise; writeFile(path: string, content: string): Promise; writeFileBytes(path: string, content: Uint8Array): Promise; appendFile(path: string, content: string | Uint8Array): Promise; readJson(path: string): Promise; writeJson( path: string, value: unknown, options?: StateJsonWriteOptions ): Promise; queryJson(path: string, query: string): Promise; updateJson( path: string, operations: StateJsonUpdateOperation[] ): Promise; exists(path: string): Promise; stat(path: string): Promise; lstat(path: string): Promise; mkdir(path: string, options?: StateMkdirOptions): Promise; readdir(path: string): Promise; readdirWithFileTypes(path: string): Promise; find(path: string, options?: StateFindOptions): Promise; walkTree(path: string, options?: StateTreeOptions): Promise; summarizeTree( path: string, options?: StateTreeOptions ): Promise; searchText( path: string, query: string, options?: StateSearchOptions ): Promise; searchFiles( pattern: string, query: string, options?: StateSearchOptions ): Promise; replaceInFile( path: string, search: string, replacement: string, options?: StateSearchOptions ): Promise; replaceInFiles( pattern: string, search: string, replacement: string, options?: StateReplaceInFilesOptions ): Promise; rm(path: string, options?: StateRmOptions): Promise; cp(src: string, dest: string, options?: StateCopyOptions): Promise; mv(src: string, dest: string, options?: StateMoveOptions): Promise; symlink(target: string, linkPath: string): Promise; readlink(path: string): Promise; realpath(path: string): Promise; resolvePath(base: string, path: string): Promise; glob(pattern: string): Promise; diff(pathA: string, pathB: string): Promise; diffContent(path: string, newContent: string): Promise; createArchive( path: string, sources: string[] ): Promise; listArchive(path: string): Promise; extractArchive( path: string, destination: string ): Promise; compressFile( path: string, destination?: string ): Promise; decompressFile( path: string, destination?: string ): Promise; hashFile(path: string, options?: StateHashOptions): Promise; detectFile(path: string): Promise; removeTree(path: string): Promise; copyTree(src: string, dest: string): Promise; moveTree(src: string, dest: string): Promise; planEdits(instructions: StateEditInstruction[]): Promise; applyEditPlan( plan: StateEditPlan, options?: StateApplyEditsOptions ): Promise; applyEdits( edits: StateEdit[], options?: StateApplyEditsOptions ): Promise; } export const STATE_METHOD_NAMES = [ "getCapabilities", "readFile", "readFileBytes", "writeFile", "writeFileBytes", "appendFile", "readJson", "writeJson", "queryJson", "updateJson", "exists", "stat", "lstat", "mkdir", "readdir", "readdirWithFileTypes", "find", "walkTree", "summarizeTree", "searchText", "searchFiles", "replaceInFile", "replaceInFiles", "rm", "cp", "mv", "symlink", "readlink", "realpath", "resolvePath", "glob", "diff", "diffContent", "createArchive", "listArchive", "extractArchive", "compressFile", "decompressFile", "hashFile", "detectFile", "removeTree", "copyTree", "moveTree", "planEdits", "applyEditPlan", "applyEdits" ] as const satisfies readonly (keyof StateBackend)[]; export type StateMethodName = (typeof STATE_METHOD_NAMES)[number];