forked from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
168 lines (154 loc) · 5.23 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
import {
App,
Editor,
MarkdownView,
Notice,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import { YoutubeVideoSummaryModal } from "src/Modals/YoutubeVideoSummaryModal";
import { PluginSettings, DEFAULT_SETTINGS, DEFAULT_TEMPLATE, DEFAULT_SUMMARY_SIZE, MAX_SUMMARY_SIZE, OPEN_AI_MODEL_CHOICES, DEFAULT_MODEL, OPEN_AI_MODELS_MAX_TOKEN_SIZES, DEFAULT_DATE_FORMAT } from "settings";
export default class MyPlugin extends Plugin {
public settings: PluginSettings;
async onload() {
await this.loadSettings();
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: "generate-video-summary",
name: "Generate video summary",
editorCallback: (editor: Editor, view: MarkdownView) => {
const openAiApiKey = this.settings.openAIApiKey;
if (openAiApiKey == undefined || openAiApiKey == "") {
new Notice(
"OpenAI Api key is not added. Please, go to the settings and add it.",
);
} else {
new YoutubeVideoSummaryModal(
this.app,
editor,
this.settings,
).open();
}
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
Object.assign(DEFAULT_SETTINGS, (await this.loadData()) ?? {})
);
}
async saveSettings() {
await this.saveData(this.settings);
}
/** Update plugin settings. */
async updateSettings(settings: Partial<PluginSettings>) {
Object.assign(this.settings, settings);
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("OpenAI key")
.setDesc(
"Enter your OpenAI API Key to be able to generate summaries from transcripts.",
)
.addText((text) =>
text
.setPlaceholder("Enter your API key")
.setValue(this.plugin.settings.openAIApiKey)
.onChange(async (value) => {
await this.plugin.updateSettings({openAIApiKey: value});
}),
);
new Setting(containerEl)
.setName("OpenAI Model")
.setDesc("Choose the OpenAI model to use.")
.addDropdown((dropdown) => {
// Explicitly define the accumulator type in the reduce function
const options = OPEN_AI_MODEL_CHOICES.reduce<Record<string, string>>((acc, model) => {
acc[model] = model; // Set both key and value to the model string
return acc;
}, {});
dropdown
.addOptions(options)
.setValue(this.plugin.settings.openAIModel)
.onChange(async (value) => {
await this.plugin.updateSettings({openAIModel: value});
await this.plugin.updateSettings({maxTokenSize: OPEN_AI_MODELS_MAX_TOKEN_SIZES[value as keyof typeof OPEN_AI_MODELS_MAX_TOKEN_SIZES]});
});
})
;
new Setting(containerEl)
.setName("Minimum Summary Size")
.setDesc("Minimum number of key points per video chunk to include in the generated summary. Note: The plugin divides long videos into chunks, extracts key points from each, and then combines them for the final summary.")
.addText((text) =>
text
.setPlaceholder("3")
.setValue("" + this.plugin.settings.summarySize)
.onChange(async (value) => {
let parsed = parseInt(value);
if (isNaN(parsed)) return;
parsed = parsed > MAX_SUMMARY_SIZE ? MAX_SUMMARY_SIZE :
parsed < DEFAULT_SUMMARY_SIZE ? DEFAULT_SUMMARY_SIZE : parsed;
await this.plugin.updateSettings({summarySize: parsed});
})
);
new Setting(containerEl)
.setName("Template format")
.setDesc(
"Enter format of template to be used when inserting summary. Supported fields are {{Date}}, {{Title}}, {{ImageURL}}, {{Description}}, and {{VideoUrl}}.",
)
.addTextArea((text) =>
text
.setPlaceholder(DEFAULT_TEMPLATE)
.setValue(this.plugin.settings.templateFormat)
.onChange(async (value) => {
await this.plugin.updateSettings({templateFormat: value});
}),
);
new Setting(containerEl)
.setName("Date format")
.setDesc("The default date format.")
.addTextArea((text) =>
text
.setPlaceholder(DEFAULT_DATE_FORMAT)
.setValue(this.plugin.settings.dateFormat)
.onChange(async (value) => {
await this.plugin.updateSettings({dateFormat: value});
}),
);
new Setting(containerEl)
.setName("Reset defaults")
.setDesc(
`This will reset to the default settings (without removing your API key).
The settings panel will be closed after pressing.
`
)
.addButton((cb) => {
cb.setWarning()
.setButtonText("Reset defaults")
.onClick(() => {
this.plugin.settings.summarySize = DEFAULT_SUMMARY_SIZE;
this.plugin.settings.templateFormat = DEFAULT_TEMPLATE;
this.plugin.settings.dateFormat = DEFAULT_DATE_FORMAT;
this.plugin.settings.openAIModel = OPEN_AI_MODEL_CHOICES[0];
this.plugin.settings.maxTokenSize = OPEN_AI_MODELS_MAX_TOKEN_SIZES[DEFAULT_MODEL as keyof typeof OPEN_AI_MODELS_MAX_TOKEN_SIZES];
this.plugin.saveSettings();
this.plugin.unload();
this.plugin.load();
});
});
}
}