2025-01-31 13:37:06 +00:00
|
|
|
import { ExampleView, VIEW_TYPE_EXAMPLE } from "CustomView";
|
2025-01-31 12:17:05 +00:00
|
|
|
import {
|
2025-01-31 13:07:22 +00:00
|
|
|
addIcon,
|
2025-01-31 12:17:05 +00:00
|
|
|
App,
|
|
|
|
|
Editor,
|
|
|
|
|
MarkdownView,
|
|
|
|
|
Modal,
|
|
|
|
|
Notice,
|
|
|
|
|
Plugin,
|
|
|
|
|
PluginSettingTab,
|
|
|
|
|
Setting,
|
2025-01-31 13:37:06 +00:00
|
|
|
WorkspaceLeaf,
|
2025-01-31 12:17:05 +00:00
|
|
|
} from "obsidian";
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
// Remember to rename these classes and interfaces!
|
2025-01-31 13:37:06 +00:00
|
|
|
//
|
|
|
|
|
// Custom views need to be registered when the plugin is enabled,
|
|
|
|
|
// and cleaned up when the plugin is disabled:
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
interface MyPluginSettings {
|
|
|
|
|
mySetting: string;
|
2025-01-31 12:36:09 +00:00
|
|
|
openAiKey: string;
|
2022-04-15 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DEFAULT_SETTINGS: MyPluginSettings = {
|
2025-01-31 12:17:05 +00:00
|
|
|
mySetting: "default",
|
2025-01-31 12:36:09 +00:00
|
|
|
openAiKey: "openAiKeyTBD",
|
2025-01-31 12:17:05 +00:00
|
|
|
};
|
2022-04-15 18:13:31 +00:00
|
|
|
|
2025-01-31 14:23:55 +00:00
|
|
|
|
2025-01-31 12:17:05 +00:00
|
|
|
export default class NemesisPlugin extends Plugin {
|
2025-01-31 14:23:55 +00:00
|
|
|
settings: MyPluginSettings;
|
2022-04-15 18:13:31 +00:00
|
|
|
|
2025-01-31 14:23:55 +00:00
|
|
|
async testFunction() {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${this.settings.openAiKey}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
model: "gpt-3.5-turbo",
|
|
|
|
|
messages: [{ role: "user", content: "Hello!" }]
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(data.error?.message || 'API request failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new Notice("OpenAI Response: " + data.choices[0].message.content);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
new Notice("OpenAI Error: " + error.message);
|
|
|
|
|
console.error("OpenAI Error:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 14:15:50 +00:00
|
|
|
|
2025-01-31 13:37:06 +00:00
|
|
|
async activateView() {
|
|
|
|
|
const { workspace } = this.app;
|
|
|
|
|
let leaf: WorkspaceLeaf | null = null;
|
|
|
|
|
const leaves = workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE);
|
|
|
|
|
if (leaves.length > 0) {
|
|
|
|
|
// A leaf with our view already exists, use that
|
|
|
|
|
leaf = leaves[0];
|
|
|
|
|
} else {
|
|
|
|
|
// Create a new leaf in the right sidebar
|
|
|
|
|
leaf = workspace.getRightLeaf(false);
|
|
|
|
|
if (leaf) {
|
|
|
|
|
await leaf.setViewState({
|
|
|
|
|
type: VIEW_TYPE_EXAMPLE,
|
|
|
|
|
active: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 14:15:50 +00:00
|
|
|
if (leaf) {
|
|
|
|
|
workspace.revealLeaf(leaf);
|
|
|
|
|
workspace.setActiveLeaf(leaf);
|
|
|
|
|
}
|
2025-01-31 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 18:13:31 +00:00
|
|
|
async onload() {
|
|
|
|
|
await this.loadSettings();
|
|
|
|
|
|
2025-01-31 12:27:12 +00:00
|
|
|
console.log("loading plugin");
|
|
|
|
|
|
2025-01-31 12:17:05 +00:00
|
|
|
// This creates an icon in the left ribbon.
|
2025-01-31 13:07:22 +00:00
|
|
|
addIcon(
|
|
|
|
|
"logo",
|
2025-01-31 13:26:08 +00:00
|
|
|
'<path d="M10 90V14.8284C10 13.0466 12.1543 12.1543 13.4142 13.4142L86.5858 86.5858C87.8457 87.8457 90 86.9534 90 85.1716V10" stroke="#FF8A8A" stroke-width="4" stroke-linecap="round"/>'
|
2025-01-31 13:07:22 +00:00
|
|
|
);
|
2025-01-31 12:17:05 +00:00
|
|
|
const ribbonIconEl = this.addRibbonIcon(
|
2025-01-31 13:07:22 +00:00
|
|
|
"logo",
|
2025-01-31 12:17:05 +00:00
|
|
|
"Sample Plugin",
|
|
|
|
|
(evt: MouseEvent) => {
|
|
|
|
|
// Called when the user clicks the icon.
|
|
|
|
|
new Notice("This is a notice!");
|
2025-01-31 13:26:08 +00:00
|
|
|
console.log("click");
|
2025-01-31 13:37:06 +00:00
|
|
|
this.activateView();
|
2025-01-31 14:15:50 +00:00
|
|
|
this.testFunction();
|
2025-01-31 13:26:08 +00:00
|
|
|
// console.log(`My openai key: ${this.settings.openAiKey}`);
|
2025-01-31 12:17:05 +00:00
|
|
|
}
|
|
|
|
|
);
|
2025-01-31 13:26:08 +00:00
|
|
|
// todo create and open new view to display the open ai nemesis result
|
2025-01-31 13:37:06 +00:00
|
|
|
// it currently does not pop open the view
|
|
|
|
|
this.registerView(VIEW_TYPE_EXAMPLE, (leaf) => new ExampleView(leaf));
|
2025-01-31 13:26:08 +00:00
|
|
|
|
|
|
|
|
// use this class to perform additional things with the ribbon
|
|
|
|
|
// ribbonIconEl.addClass("my-plugin-ribbon-class");
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
2025-01-31 13:26:08 +00:00
|
|
|
// const statusBarItemEl = this.addStatusBarItem();
|
|
|
|
|
// statusBarItemEl.setText("asdfasdfasdfasdfs");
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
// This adds a simple command that can be triggered anywhere
|
2025-01-31 13:26:08 +00:00
|
|
|
// this.addCommand({
|
|
|
|
|
// id: "open-sample-modal-simple",
|
|
|
|
|
// name: "Open sample modal (simple)",
|
|
|
|
|
// callback: () => {
|
|
|
|
|
// new SampleModal(this.app).open();
|
|
|
|
|
// },
|
|
|
|
|
// });
|
2022-04-15 18:13:31 +00:00
|
|
|
// This adds an editor command that can perform some operation on the current editor instance
|
2025-01-31 13:26:08 +00:00
|
|
|
// get the current text selection in editor and replace it with "Sample Editor Command"
|
|
|
|
|
// this.addCommand({
|
|
|
|
|
// id: "sample-editor-command",
|
|
|
|
|
// name: "Sample editor command",
|
|
|
|
|
// editorCallback: (editor: Editor, view: MarkdownView) => {
|
|
|
|
|
// console.log(editor.getSelection());
|
|
|
|
|
// editor.replaceSelection("Sample Editor Command");
|
|
|
|
|
// },
|
|
|
|
|
// });
|
2022-04-15 18:13:31 +00:00
|
|
|
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
2025-01-31 13:26:08 +00:00
|
|
|
// this.addCommand({
|
|
|
|
|
// id: "open-sample-modal-complex",
|
|
|
|
|
// name: "Open sample modal (complex)",
|
|
|
|
|
// checkCallback: (checking: boolean) => {
|
|
|
|
|
// // Conditions to check
|
|
|
|
|
// const markdownView =
|
|
|
|
|
// this.app.workspace.getActiveViewOfType(MarkdownView);
|
|
|
|
|
// if (markdownView) {
|
|
|
|
|
// // If checking is true, we're simply "checking" if the command can be run.
|
|
|
|
|
// // If checking is false, then we want to actually perform the operation.
|
|
|
|
|
// if (!checking) {
|
|
|
|
|
// new SampleModal(this.app).open();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // This command will only show up in Command Palette when the check function returns true
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// });
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
// This adds a settings tab so the user can configure various aspects of the plugin
|
|
|
|
|
this.addSettingTab(new SampleSettingTab(this.app, this));
|
|
|
|
|
|
|
|
|
|
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
|
|
|
|
// Using this function will automatically remove the event listener when this plugin is disabled.
|
2025-01-31 12:17:05 +00:00
|
|
|
this.registerDomEvent(document, "click", (evt: MouseEvent) => {
|
|
|
|
|
console.log("click", evt);
|
2022-04-15 18:13:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
2025-01-31 12:17:05 +00:00
|
|
|
this.registerInterval(
|
|
|
|
|
window.setInterval(() => console.log("setInterval"), 5 * 60 * 1000)
|
|
|
|
|
);
|
2022-04-15 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 12:17:05 +00:00
|
|
|
onunload() {}
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
async loadSettings() {
|
2025-01-31 12:17:05 +00:00
|
|
|
this.settings = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
DEFAULT_SETTINGS,
|
|
|
|
|
await this.loadData()
|
|
|
|
|
);
|
2022-04-15 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async saveSettings() {
|
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SampleModal extends Modal {
|
|
|
|
|
constructor(app: App) {
|
|
|
|
|
super(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onOpen() {
|
2025-01-31 12:17:05 +00:00
|
|
|
const { contentEl } = this;
|
|
|
|
|
contentEl.setText("Woah!");
|
2022-04-15 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onClose() {
|
2025-01-31 12:17:05 +00:00
|
|
|
const { contentEl } = this;
|
2022-04-15 18:13:31 +00:00
|
|
|
contentEl.empty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SampleSettingTab extends PluginSettingTab {
|
2025-01-31 12:17:05 +00:00
|
|
|
plugin: NemesisPlugin;
|
2022-04-15 18:13:31 +00:00
|
|
|
|
2025-01-31 12:17:05 +00:00
|
|
|
constructor(app: App, plugin: NemesisPlugin) {
|
2022-04-15 18:13:31 +00:00
|
|
|
super(app, plugin);
|
|
|
|
|
this.plugin = plugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display(): void {
|
2025-01-31 12:17:05 +00:00
|
|
|
const { containerEl } = this;
|
2022-04-15 18:13:31 +00:00
|
|
|
|
|
|
|
|
containerEl.empty();
|
|
|
|
|
|
|
|
|
|
new Setting(containerEl)
|
2025-01-31 12:17:05 +00:00
|
|
|
.setName("Setting #1")
|
|
|
|
|
.setDesc("It's a secret")
|
|
|
|
|
.addText((text) =>
|
|
|
|
|
text
|
|
|
|
|
.setPlaceholder("Enter your secret")
|
|
|
|
|
.setValue(this.plugin.settings.mySetting)
|
|
|
|
|
.onChange(async (value) => {
|
|
|
|
|
this.plugin.settings.mySetting = value;
|
|
|
|
|
await this.plugin.saveSettings();
|
|
|
|
|
})
|
|
|
|
|
);
|
2025-01-31 13:26:08 +00:00
|
|
|
new Setting(containerEl)
|
|
|
|
|
.setName("OpenAI API Key")
|
|
|
|
|
.setDesc("Set your OpenAI API key here.")
|
|
|
|
|
.addText((text) =>
|
|
|
|
|
text
|
|
|
|
|
.setPlaceholder("Enter OpenAI API key here")
|
|
|
|
|
.setValue(this.plugin.settings.mySetting)
|
|
|
|
|
.onChange(async (value) => {
|
|
|
|
|
this.plugin.settings.openAiKey = value;
|
|
|
|
|
await this.plugin.saveSettings();
|
|
|
|
|
})
|
|
|
|
|
);
|
2022-04-15 18:13:31 +00:00
|
|
|
}
|
|
|
|
|
}
|