andy-stack_vaultkeeper-ai/Services/FileSystemService.ts
Andrew Beal 2d5a1b52bf feat: add interactive diff viewer with user approval workflow
Implement a comprehensive diff viewing system that allows users to review and approve/reject file changes before they're applied. The system includes event-driven architecture for managing diff lifecycle and integrates diff2html for rich visual diffs.

Key changes:
- Add DiffService for managing diff approval workflow with accept/reject/suggest actions
- Create EventService for type-safe event handling (DiffOpened/DiffClosed)
- Add DiffView component with diff2html integration for visual diff rendering
- Modify VaultService to propose changes and require confirmation before file operations
- Update FileSystemService to support optional confirmation for write operations
- Add Event enum for centralized event type definitions
- Import custom styles and diff2html styles for proper diff rendering
- Update ConversationContent validation to support optional toolId field
- Remove FileManager from dependency injection (now accessed directly from app)
- Update .gitignore to track styles.css instead of main.css
2025-11-24 21:29:54 +00:00

90 lines
No EOL
4.5 KiB
TypeScript

import { TAbstractFile, TFile, TFolder } from "obsidian";
import { Resolve } from "./DependencyService";
import { Services } from "./Services";
import type { VaultService } from "./VaultService";
import type { ISearchMatch } from "../Helpers/SearchTypes";
import { Exception } from "Helpers/Exception";
export class FileSystemService {
private readonly vaultService: VaultService;
public constructor() {
this.vaultService = Resolve<VaultService>(Services.VaultService);
}
public async readFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<string | Error> {
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
if (file && file instanceof TFile) {
return await this.vaultService.read(file, allowAccessToPluginRoot);
}
return Exception.new(`Path is a folder, not a file: ${filePath}`);
}
public async writeFile(filePath: string, content: string, allowAccessToPluginRoot: boolean = false): Promise<TFile | Error> {
try {
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
if (file == null || !(file instanceof TFile)) {
return await this.vaultService.create(filePath, content, allowAccessToPluginRoot);
}
return await this.vaultService.modify(file, content, allowAccessToPluginRoot);
}
catch (error) {
Exception.log(error);
return Exception.new(error);
}
}
public async deleteFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<Error | void> {
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
if (!file) {
return Exception.new(`File does not exist: ${filePath}`);
}
return await this.vaultService.delete(file, allowAccessToPluginRoot);
}
public async moveFile(sourcePath: string, destinationPath: string, allowAccessToPluginRoot: boolean = false): Promise<void | Error> {
return await this.vaultService.move(sourcePath, destinationPath, allowAccessToPluginRoot);
}
public async listFilesInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFile[]> {
return await this.vaultService.listFilesInDirectory(dirPath, recursive, allowAccessToPluginRoot);
}
public async listFoldersInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFolder[]> {
return await this.vaultService.listFoldersInDirectory(dirPath, recursive, allowAccessToPluginRoot);
}
public async listDirectoryContents(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TAbstractFile[]> {
return await this.vaultService.listDirectoryContents(dirPath, recursive, allowAccessToPluginRoot);
}
public async readObjectFromFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<object | Error> {
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
if (file && file instanceof TFile) {
const result = await this.vaultService.read(file, allowAccessToPluginRoot);
return typeof result === "string" ? JSON.parse(result) as object : result;
}
return Exception.new(`File not found: ${filePath}`);
}
public async writeObjectToFile(filePath: string, data: object, allowAccessToPluginRoot: boolean = false, requiresConfirmation: boolean = true): Promise<TFile | Error> {
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
let result: TFile | Error;
if (file && file instanceof TFile) {
result = await this.vaultService.modify(file, JSON.stringify(data, null, 4), allowAccessToPluginRoot, requiresConfirmation);
}
else {
result = await this.vaultService.create(filePath, JSON.stringify(data, null, 4), allowAccessToPluginRoot, requiresConfirmation);
}
return result;
}
public async searchVaultFiles(searchTerm: string, allowAccessToPluginRoot: boolean = false): Promise<ISearchMatch[]> {
return await this.vaultService.searchVaultFiles(searchTerm, allowAccessToPluginRoot);
}
}