-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
140 lines (112 loc) · 3.35 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
import {
App,
Plugin,
PluginSettingTab,
Setting,
WorkspaceLeaf,
addIcon,
} from "obsidian";
import { BardObsidianView, VIEW_TYPE_BardObsidian } from "src/plugin/view";
import { BardObsidianSettings, DEFAULT_SETTINGS } from "src/settings";
import Bard from "bard-ai";
import Chats from "bard-ai/Chats";
// Remember to rename these classes and interfaces!
export default class BardObsidian extends Plugin {
// @ts-ignore
settings: BardObsidianSettings;
// @ts-ignore
prevSettings: BardObsidianSettings;
chatBardView?: BardObsidianView;
bard?: Bard;
// @ts-ignore
chats: Chats;
async onload() {
this.chats = new Chats();
await this.loadSettings();
this.chats.load();
addIcon(
"sparkles",
`<svg xmlns="http://www.w3.org/2000/svg" width="100%" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-sparkles"><path d="m12 3-1.912 5.813a2 2 0 0 1-1.275 1.275L3 12l5.813 1.912a2 2 0 0 1 1.275 1.275L12 21l1.912-5.813a2 2 0 0 1 1.275-1.275L21 12l-5.813-1.912a2 2 0 0 1-1.275-1.275L12 3Z"/><path d="M5 3v4"/><path d="M19 17v4"/><path d="M3 5h4"/><path d="M17 19h4"/></svg>`
);
this.registerView(
VIEW_TYPE_BardObsidian,
(leaf) => new BardObsidianView(leaf, this.settings, this.chats)
);
this.registerEvent(
//@ts-ignore
this.app.workspace.on("active-leaf-change", this.onActiveLeafChange)
);
this.addCommand({
id: "google-bard-assistant",
name: "Open Google Bard Assistant",
callback: () => {
this.activateView();
},
});
this.addRibbonIcon("sparkles", "Open Bard", () => {
this.activateView();
});
this.addSettingTab(new BardObsidianSettingTab(this.app, this));
// this.bard();
}
onActiveLeafChange = async (e: WorkspaceLeaf) => {
if (e?.view.getViewType() == VIEW_TYPE_BardObsidian) {
if (
this.bard == undefined ||
this.chats.prevSettings.Cookie != this.chats.settings.Cookie
) {
await this.chats.load();
}
// console.log("lets go");
}
};
/* COMMANDS */
newChat() {}
/* END of COMMANDS */
/* CONFIGURATIONS */
async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_BardObsidian);
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_BardObsidian,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE_BardObsidian)[0]
);
}
async loadSettings() {
const loadedData = await this.loadData();
this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedData);
this.prevSettings = Object.assign({}, DEFAULT_SETTINGS, loadedData);
this.chats.loadSettings({
settings: this.settings,
prevSettings: this.prevSettings,
});
}
async saveSettings() {
await this.saveData(this.settings);
}
}
export class BardObsidianSettingTab extends PluginSettingTab {
plugin: BardObsidian;
constructor(app: App, plugin: BardObsidian) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Cookie Key")
.setDesc("Your Cookie Google Bard Key")
.addText((text) =>
text
.setPlaceholder("The Key Is On Cookies")
.setValue(this.plugin.settings.Cookie)
.onChange(async (value) => {
this.plugin.settings.Cookie = value;
await this.plugin.saveSettings();
})
);
}
}