llm-agent-plugin/main.js
2025-09-12 00:39:44 +03:00

105 lines
3.4 KiB
JavaScript

const { Plugin, ItemView, WorkspaceLeaf } = require('obsidian');
const { GraphView, GRAPH_VIEW_TYPE } = require('/src/views/GraphView.js');
const { ChatView, CHAT_VIEW_TYPE } = require('./src/views/ChatView.js');
const { EventBus } = require('./src/utils/EventBus.js');
// ----------------------------------------------- Settings ---------------------------------------------------------------
const DEFAULT_SETTINGS = {
apiBaseUrl: 'http://localhost:5000/api'
};
// ----------------------------------------------- Main Plugin Class ---------------------------------------------------------------
class LLMAgentPlugin extends Plugin {
constructor() {
super();
this.eventBus = new EventBus();
this.settings = DEFAULT_SETTINGS;
}
async onload() {
await this.loadSettings();
// Регистрируем кастомные views
this.registerView(GRAPH_VIEW_TYPE, (leaf) => new GraphView(leaf, this));
this.registerView(CHAT_VIEW_TYPE, (leaf) => new ChatView(leaf, this));
// Добавляем команды
this.addCommand({
id: 'open-llm-agent-graph',
name: 'Открыть граф диалогов LLM Agent',
callback: () => this.activateGraphView(),
});
this.addCommand({
id: 'open-llm-agent-chat',
name: 'Открыть чат LLM Agent',
callback: () => this.activateChatView(),
});
this.addCommand({
id: 'new-llm-agent-chat',
name: 'Новый чат LLM Agent',
callback: () => this.handleNewChat(),
});
// Добавляем иконку в ribbon
this.addRibbonIcon('brain-circuit', 'LLM Agent', () => {
this.activateGraphView();
this.activateChatView();
});
console.log('LLM Agent plugin загружен');
}
onunload() {
console.log('LLM Agent plugin выгружен');
}
// ----------------------------------------------- Settings Management ---------------------------------------------------------------
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
// ----------------------------------------------- View Management ---------------------------------------------------------------
async activateGraphView() {
const existingLeaf = this.app.workspace.getLeavesOfType(GRAPH_VIEW_TYPE)[0];
if (existingLeaf) {
this.app.workspace.revealLeaf(existingLeaf);
} else {
await this.app.workspace.getLeaf('tab').setViewState({
type: GRAPH_VIEW_TYPE,
active: true,
});
}
}
async activateChatView() {
const existingLeaf = this.app.workspace.getLeavesOfType(CHAT_VIEW_TYPE)[0];
if (existingLeaf) {
this.app.workspace.revealLeaf(existingLeaf);
} else {
await this.app.workspace.getRightLeaf(false).setViewState({
type: CHAT_VIEW_TYPE,
active: true,
});
}
}
// ----------------------------------------------- Event Handlers ---------------------------------------------------------------
handleNewChat() {
this.eventBus.emit('new-chat');
}
}
module.exports = LLMAgentPlugin;