import { Config } from '../utils/types.js';
import { FilesystemProvider } from '../providers/filesystem-provider.js';
import { RestApiProvider } from '../providers/rest-api-provider.js';
import { CredentialManager } from '../security/credential-manager.js';
import { RateLimiter } from '../security/rate-limiter.js';
export class OperationRouter {
constructor(
_config: Config,
private filesystemProvider: FilesystemProvider,
private restApiProvider: RestApiProvider,
private credentialManager: CredentialManager,
private rateLimiter: RateLimiter
) {}
needsRestAPI(operation: string): boolean {
const restApiOperations = [
'execute_command',
'open_file',
'get_active_file',
'open_graph',
];
return restApiOperations.includes(operation);
}
async execute(operation: string, vaultPath: string, params: any): Promise<any> {
// Check rate limits
this.rateLimiter.checkLimit(operation, vaultPath);
// Determine if REST API is needed
if (this.needsRestAPI(operation)) {
const credentials = await this.credentialManager.getVaultCredentials(vaultPath);
if (!credentials) {
throw new Error(
`REST API required for operation ${operation}, but plugin not installed in vault`
);
}
// Execute via REST API
return this.executeViaRestAPI(operation, credentials, params);
}
// Execute via filesystem
return this.executeViaFilesystem(operation, vaultPath, params);
}
private async executeViaRestAPI(
operation: string,
credentials: any,
params: any
): Promise<any> {
switch (operation) {
case 'execute_command':
return this.restApiProvider.executeCommand(
credentials,
params.commandId,
params.args
);
case 'open_file':
return this.restApiProvider.openFile(credentials, params.filepath);
case 'get_active_file':
return this.restApiProvider.getActiveFile(credentials);
case 'open_graph':
return this.restApiProvider.openGraph(credentials);
default:
throw new Error(`Unknown REST API operation: ${operation}`);
}
}
private async executeViaFilesystem(
operation: string,
vaultPath: string,
params: any
): Promise<any> {
switch (operation) {
case 'get_file':
return this.filesystemProvider.readFile(
vaultPath,
params.filepath,
params.parseFrontmatter
);
case 'write_file':
return this.filesystemProvider.writeFile(
vaultPath,
params.filepath,
params.content,
params.expectedModTime
);
case 'list_files':
return this.filesystemProvider.listFiles(
vaultPath,
params.path,
params.recursive,
params.includeHidden
);
case 'search_files':
return this.filesystemProvider.searchFiles(
vaultPath,
params.query,
params.path,
params.caseSensitive,
params.regex,
params.contextLength
);
case 'get_metadata':
return this.filesystemProvider.getMetadata(vaultPath, params.filepath);
case 'append_content':
return this.filesystemProvider.appendContent(
vaultPath,
params.filepath,
params.content
);
default:
throw new Error(`Unknown filesystem operation: ${operation}`);
}
}
}