forked from Artel250/Obsidian-Gemini-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
167 lines (138 loc) · 4.34 KB
/
main.ts
File metadata and controls
167 lines (138 loc) · 4.34 KB
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
159
160
161
162
163
164
165
166
167
import { App, Editor, FileView, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, Vault, View, ViewState, WorkspaceLeaf, addIcon, loadMermaid } from 'obsidian';
import { OpenChatModal } from 'src/Modals/OpenChatModal';
import { GeminiChatView, VIEW_TYPE_GEMINI_CHAT } from 'src/Views/GeminiChatView';
interface GeminiPluginSettings {
Gemini_Api_Key: string;
DeveloperMode: boolean;
DefaultSavePath: string;
}
const DEFAULT_SETTINGS: GeminiPluginSettings = {
Gemini_Api_Key: "",
DeveloperMode: false,
DefaultSavePath: "Gemini Chats"
}
export default class GeminiPlugin extends Plugin {
settings: GeminiPluginSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new SettingsTab(this.app, this));
this.registerView(
VIEW_TYPE_GEMINI_CHAT,
(leaf) => {
return new GeminiChatView(leaf, this.app, this);
}
);
this.registerExtensions(["gemini"], VIEW_TYPE_GEMINI_CHAT);
this.registerEvent(this.app.workspace.on("file-open", (file) => {
if (file?.extension == "gemini") {
this.activateChatView(file);
}
}))
this.addRibbonIcon("sparkles", "New Gemini Chat", () => { this.newChatView() });
this.addCommand({
id: 'gemini-new-chat',
name: 'New Gemini Chat',
callback: () => { this.newChatView() },
})
this.addCommand({
id: 'gemini-open-chat',
name: 'Open Gemini Chat',
callback: () => {
new OpenChatModal(this.app, (file: TFile) => {
this.app.workspace.getLeaf(false).openFile(file);
}).open()
}
})
}
async onunload() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_GEMINI_CHAT);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateChatView(file: TFile) {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_GEMINI_CHAT).filter((leaf) => {
if (leaf.view instanceof FileView) {
const view = leaf.view as FileView;
if (view.file == file) {
return true;
}
}
return false;
});
if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getLeaf(false);
await leaf.setViewState({ type: VIEW_TYPE_GEMINI_CHAT, active: true });
}
if (leaf) {
workspace.setActiveLeaf(leaf);
}
}
async newChatView() {
let path = `${this.settings.DefaultSavePath}`;
if (!path.endsWith("/")) path += "/";
if (this.settings.DefaultSavePath == "") path = "";
this.app.vault.adapter.mkdir(path.endsWith('/') ? path.slice(0, -1) : path);
let name = "New Chat.gemini";
let index = 0;
while (this.app.vault.getAbstractFileByPath(path + name) != null) {
index += 1;
name = `New Chat ${index}.gemini`;
if (index >= 100) {
// Exit condition to avoid infinite loop
new Notice("Failed to create a new chat");
return;
}
}
console.log(`new chat: ${path + name}`)
let file = await this.app.vault.create(path + name, "");
await this.app.workspace.getLeaf(false).openFile(file);
}
}
class SettingsTab extends PluginSettingTab {
plugin: GeminiPlugin;
constructor(app: App, plugin: GeminiPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Gemini API Key")
.addText(text => text
.setPlaceholder("Your API key here")
.setValue(this.plugin.settings.Gemini_Api_Key)
.onChange(async (value) => {
this.plugin.settings.Gemini_Api_Key = value;
await this.plugin.saveSettings();
}))
new Setting(containerEl)
.setName("Developer mode")
.setDesc("Fakes the sending of requests, leave this off unless you want fake answers for some reason...")
.addToggle(value => value
.setValue(this.plugin.settings.DeveloperMode)
.onChange(async (value) => {
this.plugin.settings.DeveloperMode = value;
await this.plugin.saveSettings();
}))
new Setting(containerEl)
.setName("Default File Path")
.setDesc("The default folder for saved Gemini Chats. Leave empty to have new chats appear at vault root.")
.addText(text => text
.setPlaceholder("Folder path")
.setValue(this.plugin.settings.DefaultSavePath)
.onChange(async (value) => {
this.plugin.settings.DefaultSavePath = value;
await this.plugin.saveSettings();
})
)
}
}