summaryrefslogtreecommitdiffhomepage
path: root/src/settings-modal.ts
blob: 8ffc3e790b08165e752092dfe8775b5a6800b915 (plain)
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
import { Modal, Setting } from "obsidian";
import type AIPulse from "./main";
import { showModel } from "./ollama-client";
import type { ModelInfo } from "./ollama-client";

export class SettingsModal extends Modal {
	private plugin: AIPulse;
	private modelInfo: ModelInfo | null = null;
	private ctxMaxEl: HTMLElement | null = null;
	private ctxInputEl: HTMLInputElement | null = null;

	constructor(plugin: AIPulse) {
		super(plugin.app);
		this.plugin = plugin;
	}

	onOpen(): void {
		const { contentEl } = this;
		contentEl.empty();
		contentEl.addClass("ai-pulse-settings-modal");

		this.setTitle("AI Settings");

		// Ollama URL setting
		new Setting(contentEl)
			.setName("Ollama URL")
			.setDesc("Base URL of the Ollama server.")
			.addText((text) =>
				text
					.setValue(this.plugin.settings.ollamaUrl)
					.onChange(async (value) => {
						this.plugin.settings.ollamaUrl = value;
						await this.plugin.saveSettings();
					}),
			);

		// Model dropdown
		let modelDropdownSelectEl: HTMLSelectElement | null = null;

		const modelSetting = new Setting(contentEl)
			.setName("Model")
			.setDesc("Select the model to use.")
			.addDropdown((dropdown) => {
				this.populateModelDropdown(dropdown.selectEl);
				dropdown.onChange(async (value) => {
					this.plugin.settings.model = value;
					await this.plugin.saveSettings();
					void this.fetchAndApplyModelInfo(value);
				});
				modelDropdownSelectEl = dropdown.selectEl;
			});

		// Connect button
		const connectSetting = new Setting(contentEl)
			.setName("Connect")
			.setDesc(this.plugin.connectionMessage);

		connectSetting.addButton((button) =>
			button.setButtonText("Connect").onClick(async () => {
				const descEl = connectSetting.descEl;
				descEl.setText("Connecting...");

				await this.plugin.connect();

				descEl.setText(this.plugin.connectionMessage);

				if (modelDropdownSelectEl !== null) {
					this.populateModelDropdown(modelDropdownSelectEl);
				}
			}),
		);

		// Move connect above model in the DOM
		contentEl.insertBefore(connectSetting.settingEl, modelSetting.settingEl);

		// --- System Prompt ---

		const promptHeader = contentEl.createEl("h4", { text: "System Prompt" });
		promptHeader.style.marginTop = "16px";
		promptHeader.style.marginBottom = "4px";

		// File path setting (disabled/enabled based on toggle)
		let fileInputEl: HTMLInputElement | null = null;
		const fileSetting = new Setting(contentEl)
			.setName("Prompt File")
			.setDesc("Vault path to a note whose content will be used as the system prompt.")
			.addText((text) => {
				text
					.setPlaceholder("agent.md")
					.setValue(this.plugin.settings.systemPromptFile)
					.onChange(async (value) => {
						this.plugin.settings.systemPromptFile = value;
						await this.plugin.saveSettings();
					});
				text.inputEl.style.width = "200px";
				fileInputEl = text.inputEl;
			});

		const updateFileSettingState = (enabled: boolean): void => {
			fileSetting.settingEl.toggleClass("ai-pulse-setting-disabled", !enabled);
			if (fileInputEl !== null) {
				fileInputEl.disabled = !enabled;
			}
		};

		// Toggle to enable/disable
		const toggleSetting = new Setting(contentEl)
			.setName("Use Custom Instructions")
			.setDesc("Read a vault note as persistent AI instructions (e.g. formatting rules, writing style).")
			.addToggle((toggle) => {
				toggle
					.setValue(this.plugin.settings.useSystemPromptFile)
					.onChange(async (value) => {
						this.plugin.settings.useSystemPromptFile = value;
						await this.plugin.saveSettings();
						updateFileSettingState(value);
					});
			});

		// Move toggle above file path in the DOM
		contentEl.insertBefore(toggleSetting.settingEl, fileSetting.settingEl);

		// Set initial state
		updateFileSettingState(this.plugin.settings.useSystemPromptFile);

		// --- Vault Context ---

		const ctxHeader = contentEl.createEl("h4", { text: "Vault Context" });
		ctxHeader.style.marginTop = "16px";
		ctxHeader.style.marginBottom = "4px";

		// Recent files count (disabled/enabled based on toggle)
		let recentFilesInputEl: HTMLInputElement | null = null;
		const recentFilesSetting = new Setting(contentEl)
			.setName("Recent Files Count")
			.setDesc("Number of recently modified files to include in the context.")
			.addText((text) => {
				text.inputEl.type = "number";
				text.inputEl.min = "0";
				text.inputEl.max = "100";
				text.inputEl.step = "5";
				text.setValue(String(this.plugin.settings.vaultContextRecentFiles));
				text.onChange(async (value) => {
					const num = parseInt(value, 10);
					if (!isNaN(num) && num >= 0) {
						this.plugin.settings.vaultContextRecentFiles = num;
						await this.plugin.saveSettings();
					}
				});
				text.inputEl.style.width = "60px";
				recentFilesInputEl = text.inputEl;
			});

		const updateVaultContextState = (enabled: boolean): void => {
			recentFilesSetting.settingEl.toggleClass("ai-pulse-setting-disabled", !enabled);
			if (recentFilesInputEl !== null) {
				recentFilesInputEl.disabled = !enabled;
			}
		};

		const vaultContextToggle = new Setting(contentEl)
			.setName("Inject Vault Context")
			.setDesc("Automatically inject a summary of the vault (folders, tags, recent files) into each conversation.")
			.addToggle((toggle) => {
				toggle
					.setValue(this.plugin.settings.injectVaultContext)
					.onChange(async (value) => {
						this.plugin.settings.injectVaultContext = value;
						await this.plugin.saveSettings();
						updateVaultContextState(value);
					});
			});

		// Move toggle above count in the DOM
		contentEl.insertBefore(vaultContextToggle.settingEl, recentFilesSetting.settingEl);

		// Set initial state
		updateVaultContextState(this.plugin.settings.injectVaultContext);

		// --- Generation Parameters ---

		const paramHeader = contentEl.createEl("h4", { text: "Generation Parameters" });
		paramHeader.style.marginTop = "16px";
		paramHeader.style.marginBottom = "4px";

		// Temperature
		const tempSetting = new Setting(contentEl)
			.setName("Temperature")
			.setDesc("Controls randomness. Lower = more focused, higher = more creative.");

		const tempValueEl = tempSetting.descEl.createSpan({
			cls: "ai-pulse-param-value",
			text: ` (${this.plugin.settings.temperature.toFixed(2)})`,
		});

		tempSetting.addSlider((slider) =>
			slider
				.setLimits(0, 2, 0.05)
				.setValue(this.plugin.settings.temperature)
				.setDynamicTooltip()
				.onChange(async (value) => {
					this.plugin.settings.temperature = value;
					tempValueEl.setText(` (${value.toFixed(2)})`);
					await this.plugin.saveSettings();
				}),
		);

		// Context Window
		const ctxSetting = new Setting(contentEl)
			.setName("Context Window")
			.setDesc("Max tokens the model sees (prompt + response).");

		let ctxInputEl: HTMLInputElement | null = null;

		ctxSetting.addText((text) => {
			text.inputEl.type = "number";
			text.inputEl.min = "256";
			text.inputEl.max = "1048576";
			text.inputEl.step = "256";
			text.setValue(String(this.plugin.settings.numCtx));
			text.onChange(async (value) => {
				const num = parseInt(value, 10);
				if (!isNaN(num) && num >= 256) {
					this.plugin.settings.numCtx = num;
					await this.plugin.saveSettings();
				}
				this.updateCtxMaxWarning();
			});
			text.inputEl.style.width = "80px";
			ctxInputEl = text.inputEl;
		});

		// Model max label placed below the input
		const ctxControlEl = ctxSetting.controlEl;
		ctxControlEl.style.flexDirection = "column";
		ctxControlEl.style.alignItems = "flex-end";
		this.ctxMaxEl = ctxControlEl.createDiv({ cls: "ai-pulse-ctx-max" });
		this.ctxMaxEl.style.cursor = "pointer";
		this.ctxMaxEl.addEventListener("click", async () => {
			if (this.modelInfo !== null && this.ctxInputEl !== null) {
				this.plugin.settings.numCtx = this.modelInfo.contextLength;
				this.ctxInputEl.value = String(this.modelInfo.contextLength);
				await this.plugin.saveSettings();
				this.updateCtxMaxWarning();
			}
		});
		this.ctxInputEl = ctxInputEl;

		// Max Output Tokens
		const predictSetting = new Setting(contentEl)
			.setName("Max Output Tokens")
			.setDesc("Maximum tokens to generate. -1 = unlimited.");

		predictSetting.addText((text) => {
			text.inputEl.type = "number";
			text.inputEl.min = "-1";
			text.inputEl.max = "1048576";
			text.inputEl.step = "256";
			text.setValue(String(this.plugin.settings.numPredict));
			text.onChange(async (value) => {
				const num = parseInt(value, 10);
				if (!isNaN(num) && num >= -1) {
					this.plugin.settings.numPredict = num;
					await this.plugin.saveSettings();
				}
			});
			text.inputEl.style.width = "80px";
		});

		// Fetch model info if a model is already selected
		if (this.plugin.settings.model !== "") {
			void this.fetchAndApplyModelInfo(this.plugin.settings.model);
		}
	}

	onClose(): void {
		this.contentEl.empty();
	}

	private async fetchAndApplyModelInfo(model: string): Promise<void> {
		if (model === "") return;
		try {
			this.modelInfo = await showModel(this.plugin.settings.ollamaUrl, model);
			if (this.modelInfo !== null && this.ctxMaxEl !== null) {
				this.ctxMaxEl.setText(`Model max: ${this.modelInfo.contextLength.toLocaleString()}`);
				this.updateCtxMaxWarning();
			}
		} catch {
			// Silently ignore — model info is optional enhancement
			if (this.ctxMaxEl !== null) {
				this.ctxMaxEl.setText("");
			}
		}
	}

	private updateCtxMaxWarning(): void {
		if (this.ctxMaxEl === null || this.modelInfo === null) return;
		const exceeds = this.plugin.settings.numCtx > this.modelInfo.contextLength;
		this.ctxMaxEl.toggleClass("ai-pulse-ctx-max-warn", exceeds);
	}

	private populateModelDropdown(selectEl: HTMLSelectElement): void {
		const models = this.plugin.availableModels;

		selectEl.empty();

		if (models.length === 0) {
			const placeholderOpt = selectEl.createEl("option", {
				text: "Connect first",
				attr: { value: "" },
			});
			placeholderOpt.value = "";
			selectEl.disabled = true;
			return;
		}

		const placeholderOpt = selectEl.createEl("option", {
			text: "Select a model...",
			attr: { value: "" },
		});
		placeholderOpt.value = "";

		for (const modelName of models) {
			const opt = selectEl.createEl("option", {
				text: modelName,
				attr: { value: modelName },
			});
			opt.value = modelName;
		}

		if (
			this.plugin.settings.model !== "" &&
			models.includes(this.plugin.settings.model)
		) {
			selectEl.value = this.plugin.settings.model;
		}

		selectEl.disabled = false;
	}
}