-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
341 lines (300 loc) · 8.39 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import {
App,
Setting,
Notice,
PluginSettingTab,
Plugin,
Modal,
ItemView,
TFile,
TFolder,
TAbstractFile,
MarkdownView,
WorkspaceLeaf,
} from "obsidian";
import { MainSelectionModal } from "./views/MainSelectionModal";
import { LLMView } from "./views/LLMView";
import { IndexNameModal } from "./views/IndexNameModal";
import { arrayBufferToBase64 } from "./util/misc";
import { extractPDFToMarkdown, extractAllPDFsToMarkdown } from "./util/pdf";
import { transcribeCurrentFileAndSave } from "./util/whisper";
import { addMissingEpisodes } from "./util/overview";
interface RhizomancerSettings {
serverAddress: string;
}
const DEFAULT_SETTINGS: RhizomancerSettings = {
serverAddress: "http://localhost:5000",
};
export const LLM_VIEW_TYPE = "llm-chat";
export default class Rhizomancer extends Plugin {
categories: { id: string; name: string }[];
indexedHashes: Set<string> = new Set();
settings: RhizomancerSettings;
async promptForIndexAndExecute(
action: (indexName: string) => Promise<void>,
) {
new IndexNameModal(this.app, async (indexName) => {
await action(indexName);
}).open();
}
async indexCurrentFile(indexName: string) {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile && activeFile.extension === "pdf") {
await this.indexPDF(activeFile, indexName);
} else {
new Notice("The current file is not a PDF.");
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
}
async saveSettings() {
await this.saveData(this.settings);
}
async onload() {
await this.loadSettings();
this.addSettingTab(new RhizomancerSettingTab(this.app, this));
console.log(this.settings.serverAddress);
this.addCommand({
id: "open-Rhizomancer-modal",
name: "Open Rhizomancer Modal",
hotkeys: [{ modifiers: ["Ctrl"], key: "R" }],
callback: () => {
new MainSelectionModal(
this.app,
this,
this.settings.serverAddress,
).open();
},
});
this.addCommand({
id: "extract-pdf-to-markdown",
name: "Extract PDF to Markdown",
callback: () => extractPDFToMarkdown(this.app, this),
});
this.addCommand({
id: "extract-all-pdfs-to-markdown",
name: "Extract All PDFs in Folder to Markdown",
callback: () => extractAllPDFsToMarkdown(this.app, this),
});
this.addCommand({
id: "transcribe-current-file",
name: "Transcribe Current Audio File",
callback: () => transcribeCurrentFileAndSave(this.app, this),
});
this.addRibbonIcon("dice", "Rhizomancer", () => {
new MainSelectionModal(
this.app,
this,
this.settings.serverAddress,
).open();
});
this.registerView(
LLM_VIEW_TYPE,
(leaf: WorkspaceLeaf) => new LLMView(leaf, this),
);
this.addRibbonIcon("message-circle", "LLM Chat", () => {
this.activateLLMView();
});
this.addCommand({
id: "index-all-pdfs",
name: "Index All PDFs in Vault",
callback: () => {
this.promptForIndexAndExecute((indexName) =>
this.indexAllPDFs(indexName),
);
},
});
this.addCommand({
id: "index-current-pdf",
name: "Index Current PDF",
checkCallback: (checking: boolean) => {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile && activeFile.extension === "pdf") {
if (!checking) {
this.promptForIndexAndExecute((indexName) =>
this.indexCurrentFile(indexName),
);
}
return true;
}
return false;
},
});
this.addCommand({
id: "index-current-markdown",
name: "Index Current Markdown File",
checkCallback: (checking: boolean) => {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile && activeFile.extension === "md") {
if (!checking) {
this.promptForIndexAndExecute((indexName) =>
this.indexMarkdown(activeFile, indexName),
);
}
return true;
}
return false;
},
});
this.addCommand({
id: "add-missing-episodes",
name: "Add Missing Episodes to Overview",
checkCallback: (checking: boolean) => {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile && activeFile.extension === "md") {
if (!checking) {
addMissingEpisodes(this.app);
}
return true;
}
return false;
},
});
}
async activateLLMView() {
const { workspace } = this.app;
let leaf = workspace.getLeavesOfType(LLM_VIEW_TYPE)[0];
if (!leaf) {
leaf = workspace.getRightLeaf(false);
await leaf.setViewState({ type: LLM_VIEW_TYPE, active: true });
}
workspace.revealLeaf(leaf);
}
async fetchIndexedHashes(): Promise<Set<string>> {
try {
const response = await fetch(
`${this.settings.serverAddress}/get_indexed_hashes`,
);
const hashes = await response.json();
console.log("Fetched indexed hashes:", hashes);
return new Set(hashes);
} catch (error) {
console.error("Failed to fetch indexed hashes:", error);
return new Set();
}
}
async indexAllPDFs() {
const files = this.app.vault.getFiles();
for (const file of files) {
if (file.extension === "pdf") {
await this.indexPDF(file);
}
}
}
async indexPDF(file: TFile, indexName: string = "universal") {
console.log(`Attempting to index file: ${file.name}`);
const fileContent = await this.app.vault.readBinary(file);
const base64Content = arrayBufferToBase64(fileContent);
const fileHash = await this.calculateSHA256(fileContent);
console.log(`Calculated hash for ${file.name}: ${fileHash}`);
const indexedHashes = await this.fetchIndexedHashes();
console.log(`Fetched ${indexedHashes.size} indexed hashes`);
if (indexedHashes.has(fileHash)) {
console.log(
`File ${file.name} found in indexed hashes. Not indexing.`,
);
return;
}
console.log(
`File ${file.name} not found in indexed hashes. Proceeding with indexing.`,
);
try {
const response = await fetch(
`${this.settings.serverAddress}/indexPDF`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
filename: file.name,
pdf_content: base64Content,
index_name: indexName,
}),
},
);
const result = await response.json();
console.log(`Server response for ${file.name}:`, result);
if (response.ok) {
console.log(`Successfully indexed ${file.name}`);
new Notice(`Successfully indexed ${file.name}`);
} else {
console.error(
`Failed to index ${file.name}. Server responded with status ${response.status}`,
);
new Notice(
`Failed to index ${file.name}. Server responded with status ${response.status}`,
);
}
} catch (error) {
console.error(`Error while indexing PDF ${file.name}:`, error);
new Notice(`Error while indexing PDF ${file.name}:`);
}
}
async indexMarkdown(file: TFile, indexName: string = "universal") {
const content = await this.app.vault.read(file);
try {
const response = await fetch(
`${this.settings.serverAddress}/indexText`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
filename: file.name,
text: content,
index_name: indexName,
}),
},
);
const result = await response.json();
console.log(result.message);
if (response.ok) {
new Notice(`Markdown indexed successfully to ${indexName}`);
}
} catch (error) {
console.error(
`Failed to index markdown ${file.name} to ${indexName}:`,
error,
);
new Notice("Failed to index markdown");
}
}
async calculateSHA256(arrayBuffer: ArrayBuffer): Promise<string> {
const hashBuffer = await crypto.subtle.digest("SHA-256", arrayBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
}
class RhizomancerSettingTab extends PluginSettingTab {
plugin: Rhizomancer;
constructor(app: App, plugin: Rhizomancer) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Rhizomancer Settings" });
new Setting(containerEl)
.setName("Server Address")
.setDesc(
"The address of your Flask server (including protocol and port)",
)
.addText((text) =>
text
.setPlaceholder("http://localhost:5000")
.setValue(this.plugin.settings.serverAddress)
.onChange(async (value) => {
this.plugin.settings.serverAddress = value;
await this.plugin.saveSettings();
}),
);
}
}