andy-stack_vaultkeeper-ai/main.ts
Andrew Beal 72ddbba72e feat: add ListVaultFiles function and improve search behavior
- Add new ListVaultFiles AI function for listing vault contents
- Remove auto-fallback to listing all files when search returns 0 results
- Improve SearchVaultFiles description to clarify it searches content
- Add listFoldersInDirectory and listDirectoryContents methods to VaultService
- Update SanitiserService to normalize empty strings to "/" (vault root)
- Add allowAccessToPluginRoot parameter to searchVaultFiles
- Update tests to reflect new search behavior and list operations
- Add DeregisterAllServices call in plugin onunload
- Update dependencies (@google/genai, svelte, vitest, eslint)
2025-11-01 11:43:32 +00:00

86 lines
No EOL
2.1 KiB
TypeScript

import { WorkspaceLeaf, Plugin } from 'obsidian';
import { AIProviderModel } from './Enums/ApiProvider';
import { MainView, VIEW_TYPE_MAIN } from 'Views/MainView';
import { RegisterAiProvider, RegisterDependencies } from 'Services/ServiceRegistration';
import { AIAgentSettingTab } from 'AIAgentSettingTab';
import { Services } from 'Services/Services';
import type { StatusBarService } from 'Services/StatusBarService';
import { DeregisterAllServices, Resolve } from 'Services/DependencyService';
interface IAIAgentSettings {
model: string;
apiKey: string;
exclusions: string[];
}
const DEFAULT_SETTINGS: IAIAgentSettings = {
model: AIProviderModel.ClaudeSonnet_4_5,
apiKey: "",
exclusions: []
}
export default class AIAgentPlugin extends Plugin {
public settings: IAIAgentSettings;
async onload() {
// KaTeX CSS is bundled with the plugin to comply with CSP
require('katex/dist/katex.min.css');
// Plugin styles
require('./styles.css');
await this.loadSettings();
RegisterDependencies(this);
this.registerView(
VIEW_TYPE_MAIN,
(leaf) => new MainView(leaf)
);
this.addCommand({
id: 'ai-agent',
name: 'AI Agent',
callback: () => {
this.activateView();
}
});
this.addRibbonIcon('sparkles', 'AI Agent', (_: MouseEvent) => {
this.activateView();
});
this.addSettingTab(new AIAgentSettingTab(this.app, this));
}
async onunload() {
Resolve<StatusBarService>(Services.StatusBarService).removeStatusBarMessage();
DeregisterAllServices();
}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_MAIN);
if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getRightLeaf(false);
await leaf?.setViewState({ type: VIEW_TYPE_MAIN, active: true });
}
if (leaf != null) {
workspace.revealLeaf(leaf);
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
RegisterAiProvider(this);
}
}