Files
obsidian_ollama/main.ts
T
2026-05-04 22:05:10 +02:00

78 lines
2.0 KiB
TypeScript

import { Plugin, WorkspaceLeaf, App, Setting, Notice, PluginSettingTab } from 'obsidian';
import { ChatView } from './src/chat-view';
import { PluginSettings } from './src/types';
export default class OllamaPlugin extends Plugin {
settings: PluginSettings = {
ollamaUrl: 'http://localhost:11434',
model: 'llama3',
lastIndexTime: 0,
};
async onload() {
await this.loadSettings();
this.registerView(
'ollama-chat-view',
(leaf: WorkspaceLeaf) => new ChatView(leaf, this.settings)
);
this.addRibbonIcon('message-square', 'Ollama Chat', async () => {
const leaf = this.app.workspace.getLeaf();
await leaf.setViewState({
type: 'ollama-chat-view',
active: true,
});
this.app.workspace.revealLeaf(leaf);
});
this.addSettingTab(new OllamaSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, this.settings, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class OllamaSettingTab extends PluginSettingTab {
private plugin: OllamaPlugin;
constructor(app: App, plugin: OllamaPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const container = this.containerEl.createDiv() as HTMLElement;
container.empty();
new Setting(container)
.setName('Ollama URL')
.setDesc('URL of your Ollama instance')
.addText((text) =>
text.setValue(this.plugin.settings.ollamaUrl).onChange(async (value) => {
this.plugin.settings.ollamaUrl = value;
await this.plugin.saveSettings();
})
);
new Setting(container)
.setName('Model')
.setDesc('Model to use for chat')
.addText((text) =>
text.setValue(this.plugin.settings.model).onChange(async (value) => {
this.plugin.settings.model = value;
await this.plugin.saveSettings();
})
);
}
hide(): void {
this.containerEl.empty();
}
}