-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
143 lines (120 loc) · 4.24 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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault } from 'obsidian';
import { OpenAI } from "openai";
interface MLSummarySettings {
OpenAIAPIKey: string;
newConceptPrompt: string;
pdfFolder: string;
summaryOutputFolder: string;
}
const DEFAULT_SETTINGS: MLSummarySettings = {
OpenAIAPIKey: "",
newConceptPrompt: "ML",
pdfFolder: "Files/Raw PDFs",
summaryOutputFolder: "Concepts"
}
export default class MLSummary extends Plugin {
settings: MLSummarySettings;
async onload() {
await this.loadSettings();
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('LLM ready');
this.addCommand({
id: 'init-llm-summary-folders',
name: 'Initialize notes folders',
callback: () => {
const { vault } = this.app;
vault.createFolder('Topics');
vault.createFolder('Notes');
vault.createFolder('Notes/Read');
vault.createFolder('Concepts');
vault.createFolder('Attachments');
vault.createFolder('Files');
vault.createFolder('Files/PDFs');
// TODO: also add a readme file for instructions
}
});
this.addCommand({
id: 'create-new-concept-from-selected',
name: 'New concept from selected',
editorCallback: async (editor, view) => {
const selectedText = editor.getSelection();
const { vault } = this.app;
if (selectedText) {
// Sanitize the selected text to create a valid file name
const sanitizedFileName = selectedText.replace(/[<>:"\/\\|?*]/g, '').trim();
if (sanitizedFileName) {
// Initialize OpenAI configuration
const openai = new OpenAI({
apiKey: this.settings.OpenAIAPIKey,
dangerouslyAllowBrowser: true,
});
new Notice(`Defining new concept: ${sanitizedFileName}`)
const filePath = `Concepts/${sanitizedFileName}.md`;
editor.replaceSelection(`[[${sanitizedFileName}]]`);
statusBarItemEl.setText('Defining new concept..');
try {
// Query OpenAI API to generate text based on selected text
const response = await openai.chat.completions.create({
messages: [{ role: 'user', content: `Define and explain this ${this.settings.newConceptPrompt} concept: ${selectedText}` }],
model: 'gpt-4o-mini',
});
const generatedContent = response.choices[0]?.message?.content?.trim() || '';
// Create a new file in the vault with the selected text as its name
await vault.create(filePath, `This is a LLM Concept.\n\n${generatedContent}`);
new Notice(`New created note at Concepts/${sanitizedFileName}`);
} catch (error) {
new Notice(`Error creating note: ${error.message}`);
}
} else {
new Notice('Selected text is not valid for a file name');
}
} else {
new Notice('No text selected');
}
statusBarItemEl.setText('LLM ready');
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new MLSummarySettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class MLSummarySettingTab extends PluginSettingTab {
plugin: MLSummary;
constructor(app: App, plugin: MLSummary) {
super(app, plugin);
this.plugin = plugin;
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('OpenAI API Key (Required)')
.setDesc('Enter your OpenAI API Key (stored at local only).')
.addText(text => text
.setPlaceholder('sk-...')
.setValue(this.plugin.settings.OpenAIAPIKey)
.onChange(async (value) => {
this.plugin.settings.OpenAIAPIKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('New concept fields (required)')
.setDesc('Your expert fields for notes. Left empty to not specify (and got vague explains). Modify carefully.')
.addText(text => text
.setPlaceholder('ML')
.setValue(this.plugin.settings.newConceptPrompt)
.onChange(async (value) => {
this.plugin.settings.newConceptPrompt = value;
await this.plugin.saveSettings();
}));
}
display(): void {
}
}