-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
158 lines (128 loc) · 5.02 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { App, Plugin, PluginSettingTab, Setting, WorkspaceLeaf, ItemView, Notice } from 'obsidian';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ChatView } from './ChatView';
export const NOTE_GENERATOR_VIEW = "note-generator-view";
export interface NoteGeneratorSettings {
apiKey: string;
geminiModel: 'gemini-2.0-flash-exp' | 'gemini-1.5-flash' | 'gemini-1.5-flash-8b' | 'gemini-1.5-pro';
}
export const DEFAULT_SETTINGS: NoteGeneratorSettings = {
apiKey: '',
geminiModel: 'gemini-2.0-flash-exp'
}
export default class NoteGeneratorPlugin extends Plugin {
settings: NoteGeneratorSettings;
async onload() {
await this.loadSettings();
// Register Custom View
this.registerView(
NOTE_GENERATOR_VIEW,
(leaf) => new NoteGeneratorView(leaf, this)
);
// Add ribbon icon
this.addRibbonIcon('message-square', 'Note Generator', async () => {
if (!this.settings.apiKey) {
new Notice('Please set your Gemini API key in the plugin settings first.');
this.openSettings();
return;
}
await this.activateView();
});
// Add settings tab
this.addSettingTab(new NoteGeneratorSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(NOTE_GENERATOR_VIEW);
if (leaves.length > 0) {
// View already exists, show it
leaf = leaves[0];
} else {
// Create new leaf
leaf = workspace.getRightLeaf(false);
}
if (leaf) {
await leaf.setViewState({
type: NOTE_GENERATOR_VIEW,
active: true,
});
workspace.revealLeaf(leaf);
}
}
async openSettings() {
new Notice('Please enter your Gemini API key in the plugin settings (Settings -> Community Plugins -> Note Generator -> Settings)');
}
}
class NoteGeneratorView extends ItemView {
plugin: NoteGeneratorPlugin;
constructor(leaf: WorkspaceLeaf, plugin: NoteGeneratorPlugin) {
super(leaf);
this.plugin = plugin;
}
getViewType(): string {
return NOTE_GENERATOR_VIEW;
}
getDisplayText(): string {
return "Note Generator";
}
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
container.createEl("div", { attr: { id: "note-generator-root" } });
ReactDOM.render(
React.createElement(ChatView, { plugin: this.plugin }),
container.querySelector("#note-generator-root")
);
}
async onClose() {
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
}
}
class NoteGeneratorSettingTab extends PluginSettingTab {
plugin: NoteGeneratorPlugin;
constructor(app: App, plugin: NoteGeneratorPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Note Generator Settings' });
new Setting(containerEl)
.setName('Gemini API Key')
.setDesc('Enter your Gemini API key. You can get one from Google AI Studio.')
.addText(text => text
.setPlaceholder('Enter your API key')
.setValue(this.plugin.settings.apiKey)
.onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Gemini Model')
.setDesc('Select which Gemini model to use')
.addDropdown(dropdown => dropdown
.addOption('gemini-2.0-flash-exp', 'Gemini 2.0 Flash (Recommended) - Next gen features & multimodal')
.addOption('gemini-1.5-flash', 'Gemini 1.5 Flash - Fast & versatile')
.addOption('gemini-1.5-flash-8b', 'Gemini 1.5 Flash-8B - High volume tasks')
.addOption('gemini-1.5-pro', 'Gemini 1.5 Pro - Complex reasoning')
.setValue(this.plugin.settings.geminiModel)
.onChange(async (value: 'gemini-2.0-flash-exp' | 'gemini-1.5-flash' | 'gemini-1.5-flash-8b' | 'gemini-1.5-pro') => {
this.plugin.settings.geminiModel = value;
await this.plugin.saveSettings();
}));
const linkDiv = containerEl.createEl('div', { cls: 'note-generator-settings-links' });
linkDiv.createEl('a', {
text: 'Get Gemini API Key',
href: 'https://makersuite.google.com/app/apikey'
});
}
}