generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
147 lines (125 loc) · 3.27 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
import {
App,
Editor,
MarkdownView,
Plugin,
PluginSettingTab,
Setting,
TFile,
} from "obsidian";
import OpenAI from "openai";
interface PluginSettings {
apiKey: string;
statusName: string;
}
const DEFAULT_SETTINGS: PluginSettings = {
apiKey: "",
statusName: "Definition by AI",
};
export default class ChatGptDefinitions extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: "generate-definition",
name: "Generate Definition",
editorCallback: async (editor: Editor, view: MarkdownView) => {
if (view.file) {
const filename = view.file?.basename;
let oldFrontmatter =
this.app.metadataCache.getFileCache(view.file)
?.frontmatter || {};
let processedFrontmatter;
if (filename) {
const originalText = editor.getValue();
editor.setValue(originalText + "Loading...");
const definition = await getDefinition(
filename,
oldFrontmatter?.tags,
this.settings.apiKey
);
const textWithDefinition = originalText.includes(
"Loading..."
)
? originalText.replace("Loading...", definition)
: originalText + definition;
editor.setValue(textWithDefinition);
this.app.fileManager.processFrontMatter(
view.file,
(frontmatter) => {
frontmatter["status"] =
this.settings.statusName;
processedFrontmatter = frontmatter;
}
);
}
}
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingsTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingsTab extends PluginSettingTab {
plugin: ChatGptDefinitions;
constructor(app: App, plugin: ChatGptDefinitions) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("API_KEY")
.setDesc("API Key for OpenAI")
.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("Status name")
.setDesc("Status name for generated definitions")
.addText((text) =>
text
.setPlaceholder("Enter your status name")
.setValue(this.plugin.settings.statusName)
.onChange(async (value) => {
this.plugin.settings.statusName = value;
await this.plugin.saveSettings();
})
);
}
}
async function getDefinition(title: string, tags: string, apiKey: string) {
const openai = new OpenAI({
apiKey: apiKey,
dangerouslyAllowBrowser: true,
});
const prompt = `
Write me one coherent short definition of ${title} in the context of ${tags}.
Write the definition in a way i can use it for my flashcards.
`;
return await openai.chat.completions
.create({
messages: [{ role: "user", content: prompt }],
model: "gpt-3.5-turbo",
})
.then((res) => res.choices[0].message.content)
.catch((err) => err);
}