forked from jcollingj/caret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.ts
352 lines (322 loc) · 15.2 KB
/
settings.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
ItemView,
WorkspaceLeaf,
setTooltip,
setIcon,
requestUrl,
} from "obsidian";
type ModelDropDownSettings = {
openai: string;
groq: string;
ollama: string;
anthropic?: string;
custom?: string; // Make 'custom' optional
};
import { Models, CustomModels, LLMProviderOptions } from "./types";
import CaretPlugin, { DEFAULT_SETTINGS } from "./main";
export class CaretSettingTab extends PluginSettingTab {
plugin: CaretPlugin;
constructor(app: App, plugin: CaretPlugin) {
super(app, plugin);
this.plugin = plugin;
}
api_settings_tab(containerEl: HTMLElement): void {
// API settings logic here
const default_llm_providers = DEFAULT_SETTINGS.llm_provider_options;
const current_llm_providers = this.plugin.settings.llm_provider_options;
const current_custom = current_llm_providers.custom;
this.plugin.settings.llm_provider_options = { ...default_llm_providers, custom: { ...current_custom } };
const custom_endpoints = this.plugin.settings.custom_endpoints;
// @ts-ignore
let model_drop_down_settings: ModelDropDownSettings = DEFAULT_SETTINGS.provider_dropdown_options;
if (Object.keys(custom_endpoints).length > 0) {
for (const [key, value] of Object.entries(custom_endpoints)) {
if (value.known_provider) {
if (!this.plugin.settings.llm_provider_options[value.known_provider]) {
this.plugin.settings.llm_provider_options[value.known_provider] = {};
}
this.plugin.settings.llm_provider_options[value.known_provider][key] = value;
} else {
this.plugin.settings.llm_provider_options.custom[key] = value;
}
}
}
let context_window = null;
try {
const llm_provider = this.plugin.settings.llm_provider;
const model = this.plugin.settings.model;
if (
this.plugin.settings.llm_provider_options[llm_provider] &&
this.plugin.settings.llm_provider_options[llm_provider][model]
) {
const model_details = this.plugin.settings.llm_provider_options[llm_provider][model];
if (model_details && model_details.context_window) {
const context_window_value = model_details.context_window;
context_window = parseInt(context_window_value.toLocaleString());
}
}
} catch (error) {
console.error("Error retrieving model details:", error);
context_window = null;
}
if (!this.plugin.settings.llm_provider || this.plugin.settings.llm_provider.length === 0) {
this.plugin.settings.llm_provider = "openai";
this.plugin.settings.model = "gpt-4-turbo";
this.plugin.settings.context_window = 128000;
this.plugin.saveSettings();
}
const model_options_data = Object.fromEntries(
Object.entries(
this.plugin.settings.llm_provider_options[
this.plugin.settings.llm_provider as keyof typeof this.plugin.settings.llm_provider_options
]
).map(([key, value]) => [key, value.name])
);
// LLM Provider Settings
new Setting(containerEl)
.setName("LLM Provider")
.setDesc("")
.addDropdown((dropdown) => {
dropdown
.addOptions(model_drop_down_settings)
.setValue(this.plugin.settings.llm_provider)
.onChange(async (provider) => {
this.plugin.settings.llm_provider = provider;
this.plugin.settings.model = Object.keys(
this.plugin.settings.llm_provider_options[provider]
)[0];
this.plugin.settings.context_window =
this.plugin.settings.llm_provider_options[provider][
this.plugin.settings.model
].context_window;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
this.display();
});
});
const setting = new Setting(containerEl).setName("Model").addDropdown((modelDropdown) => {
modelDropdown.addOptions(model_options_data);
modelDropdown.setValue(this.plugin.settings.model);
modelDropdown.onChange(async (value) => {
this.plugin.settings.model = value;
this.plugin.settings.context_window =
this.plugin.settings.llm_provider_options[this.plugin.settings.llm_provider][value].context_window;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
this.display();
});
});
if (this.plugin.settings.model === "gpt-4o") {
new Setting(containerEl)
.setName("GPT-4o")
.setDesc(
"You are are using the new model! If you check errors it might be because your API key doesn't have access."
);
}
if (context_window) {
setting.setDesc(`FYI your selected model has a context window of ${context_window}`);
}
if (this.plugin.settings.llm_provider === "ollama") {
const ollama_info_container = containerEl.createEl("div", {
cls: "settings_container",
});
ollama_info_container.createEl("strong", { text: "You're using Ollama!" });
ollama_info_container.createEl("p", { text: "Remember to do the following:" });
ollama_info_container.createEl("p", { text: "Make sure you have downloaded the model you want to use:" });
const second_code_block_container = ollama_info_container.createEl("div", {
cls: "settings_code_block",
});
second_code_block_container.createEl("code", { text: `ollama run ${this.plugin.settings.model}` });
ollama_info_container.createEl("p", {
text: "After running the model, kill that command and close the ollama app.",
});
ollama_info_container.createEl("p", {
text: "Then run this command to start the Ollama server and make it accessible from Obsidian:",
});
const code_block_container = ollama_info_container.createEl("div", {
cls: "settings_code_block",
});
code_block_container.createEl("code", {
text: "OLLAMA_ORIGINS=app://obsidian.md* ollama serve",
});
ollama_info_container.createEl("br"); // Adds a line break for spacing
}
new Setting(containerEl)
.setName("OpenAI API Key")
.setDesc("")
.addText((text) => {
text.setPlaceholder("OpenAI API Key")
.setValue(this.plugin.settings.openai_api_key)
.onChange(async (value: string) => {
this.plugin.settings.openai_api_key = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
text.inputEl.addClass("hidden-value-unsecure");
});
new Setting(containerEl)
.setName("Groq API Key")
.setDesc("")
.addText((text) => {
text.setPlaceholder("Grok API Key")
.setValue(this.plugin.settings.groq_api_key)
.onChange(async (value: string) => {
this.plugin.settings.groq_api_key = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
text.inputEl.addClass("hidden-value-unsecure");
});
new Setting(containerEl)
.setName("Anthropic API Key")
.setDesc("")
.addText((text) => {
text.setPlaceholder("Anthropic API Key")
.setValue(this.plugin.settings.anthropic_api_key)
.onChange(async (value: string) => {
this.plugin.settings.anthropic_api_key = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
text.inputEl.addClass("hidden-value-unsecure");
});
new Setting(containerEl)
.setName("Open Router API Key")
.setDesc("")
.addText((text) => {
text.setPlaceholder("OpenRouter API Key")
.setValue(this.plugin.settings.open_router_key)
.onChange(async (value: string) => {
this.plugin.settings.open_router_key = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
text.inputEl.addClass("hidden-value-unsecure");
});
new Setting(containerEl)
.setName("Reload after adding API Keys!")
.setDesc(
"After you added API keys for the first time you will need to reload the plugin for those changes to take effect. \n This only needs to be done the first time or when you change your keys."
);
new Setting(containerEl).setName("Save Settings").addButton((button) => {
button
.setButtonText("Save")
.setClass("save-button")
.onClick(async (evt: MouseEvent) => {
await this.plugin.saveSettings();
await this.plugin.loadSettings();
new Notice("Settings Saved!");
});
});
}
chat_settings_tab(containerEl: HTMLElement): void {
let tempChatFolderPath = this.plugin.settings.chat_logs_folder; // Temporary storage for input value
new Setting(containerEl)
.setName("Chat Folder Path")
.setDesc("Specify the folder path where chat logs will be stored.")
.addText((text) => {
text.setPlaceholder("Enter folder path")
.setValue(this.plugin.settings.chat_logs_folder)
.onChange((value: string) => {
tempChatFolderPath = value; // Store the value temporarily
});
});
new Setting(containerEl)
.setName("Use Date Format for Subfolders")
.setDesc("Use Year-Month-Date as subfolders for the chat logs.")
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.chat_logs_date_format_bool).onChange(async (value: boolean) => {
this.plugin.settings.chat_logs_date_format_bool = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
});
new Setting(containerEl)
.setName("Rename Chats")
.setDesc("Chats will be given a descriptive name using your default set provider/model")
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.chat_logs_rename_bool).onChange(async (value: boolean) => {
this.plugin.settings.chat_logs_rename_bool = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
});
// LLM Provider Settings
const send_chat_shortcut_options: { [key: string]: string } = {
enter: "Enter",
shift_enter: "Shift + Enter",
// cmd_enter: "CMD + Enter",
};
new Setting(containerEl)
.setName("Send Chat Shortcut")
.setDesc("Select which shortcut will be used to send messages.")
.addDropdown((dropdown) => {
dropdown.addOptions(send_chat_shortcut_options).onChange(async (selected) => {
this.plugin.settings.chat_send_chat_shortcut = selected;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
});
new Setting(containerEl)
.setName("Save Settings")
.setDesc("Save the chat settings")
.addButton((button) => {
button.setButtonText("Save").onClick(async () => {
// Validate the path when the save button is clicked
if (tempChatFolderPath.length <= 1) {
new Notice("The folder path must be longer than one character.");
return;
}
if (tempChatFolderPath.endsWith("/")) {
new Notice("The folder path must not end with a trailing slash.");
return;
}
if (tempChatFolderPath !== this.plugin.settings.chat_logs_folder) {
this.plugin.settings.chat_logs_folder = tempChatFolderPath;
}
await this.plugin.saveSettings();
await this.plugin.loadSettings();
new Notice("Chat settings saved!");
});
});
}
display(): void {
const { containerEl } = this;
containerEl.empty();
if (this.plugin.settings.caret_version !== DEFAULT_SETTINGS.caret_version) {
this.plugin.settings.caret_version = DEFAULT_SETTINGS.caret_version;
}
// LLM Provider Settings
new Setting(containerEl)
// .setName("LLM Provider")
.setDesc(`Caret Version: ${this.plugin.settings.caret_version}`);
const tabContainer = containerEl.createEl("div", { cls: "tab-container" });
const apiTab = tabContainer.createEl("button", { text: "API Settings", cls: "tab" });
const chatTab = tabContainer.createEl("button", { text: "Chat Settings", cls: "tab" });
const apiSettingsContainer = containerEl.createEl("div", { cls: "api-settings-container hidden" });
const chatSettingsContainer = containerEl.createEl("div", { cls: "chat-settings-container hidden" });
this.api_settings_tab(apiSettingsContainer);
this.chat_settings_tab(chatSettingsContainer);
apiTab.addEventListener("click", () => {
apiSettingsContainer.classList.remove("hidden");
chatSettingsContainer.classList.add("hidden");
});
chatTab.addEventListener("click", () => {
chatSettingsContainer.classList.remove("hidden");
apiSettingsContainer.classList.add("hidden");
// Placeholder for chat settings rendering function
// this.chat_settings_tab(chatSettingsContainer);
});
// Initially load API settings tab
apiTab.click();
// this.api_settings_tab(containerEl);
}
}