-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
160 lines (133 loc) · 4.1 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
import {
App, Editor, MarkdownView, FileSystemAdapter, Modal, Notice, Plugin, PluginSettingTab, Setting
} from 'obsidian';
import NanoBotsController from './nano-bots/controller';
import NanoBotState from './nano-bots/state';
interface NanoBotsSettings {
apiAddress: string;
endUser: string;
stream: boolean;
sendCartridgesPath: boolean;
cartridgesPath: string;
}
const DEFAULT_SETTINGS: NanoBotsSettings = {
apiAddress: 'https://api.nbots.io',
endUser: 'anonymous',
stream: true,
sendCartridgesPath: false,
cartridgesPath: '',
}
export default class NanoBots extends Plugin {
settings: NanoBotsSettings;
async onload() {
if (this.app.vault.adapter instanceof FileSystemAdapter) {
const vaultPath = this.app.vault.adapter.getBasePath();
if(navigator.platform.toLowerCase().includes('win')) {
DEFAULT_SETTINGS.cartridgesPath = `${vaultPath}\\cartridges:${vaultPath}\\Cartridges`;
} else {
DEFAULT_SETTINGS.cartridgesPath = `${vaultPath}/cartridges:${vaultPath}/Cartridges`;
}
}
await this.loadSettings();
const statusBar = this.addStatusBarItem();
statusBar.setText('🤖');
statusBar.toggleClass('status-hidden', true);
NanoBotState.instance().setStatusBar(statusBar);
this.addCommand({
id: 'apply',
name: 'Apply',
editorCallback: (editor: Editor, view: MarkdownView) => {
NanoBotsController.run({ action: 'apply' }, this, editor);
}
});
this.addCommand({
id: 'evaluate',
name: 'Evaluate',
editorCallback: (editor: Editor, view: MarkdownView) => {
NanoBotsController.run({ action: 'evaluate' }, this, editor);
}
});
this.addCommand({
id: 'prompt',
name: 'Prompt',
editorCallback: (editor: Editor, view: MarkdownView) => {
NanoBotsController.run({ action: 'prompt' }, this, editor);
}
});
this.addCommand({
id: 'stop',
name: 'Stop',
editorCallback: (editor: Editor, view: MarkdownView) => {
NanoBotsController.run({ action: 'stop' }, this, editor);
}
});
this.addSettingTab(new NanoBotSettingsTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class NanoBotSettingsTab extends PluginSettingTab {
plugin: NanoBots;
constructor(app: App, plugin: NanoBots) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('API Address')
.setDesc('Set the address for the Nano Bots API.')
.addText(text => text
.setPlaceholder('https://api.nbots.io')
.setValue(this.plugin.settings.apiAddress)
.onChange(async (value) => {
this.plugin.settings.apiAddress = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('End User')
.setDesc('Your custom user identifier (user-name).')
.addText(text => text
.setPlaceholder('anonymous')
.setValue(this.plugin.settings.endUser)
.onChange(async (value) => {
this.plugin.settings.endUser = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Stream?')
.setDesc('Enable or disable streaming.')
.addToggle(boolean => boolean
.setValue(this.plugin.settings.stream)
.onChange(async (value) => {
this.plugin.settings.stream = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Custom Cartridges?')
.setDesc('Enable or disable custom cartridges.')
.addToggle(boolean => boolean
.setValue(this.plugin.settings.sendCartridgesPath)
.onChange(async (value) => {
this.plugin.settings.sendCartridgesPath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Custom Cartridges Path')
.setDesc('The path where the API should search for custom cartridges.')
.addText(text => text
.setPlaceholder('anonymous')
.setValue(this.plugin.settings.cartridgesPath)
.onChange(async (value) => {
this.plugin.settings.cartridgesPath = value;
await this.plugin.saveSettings();
}));
}
}