Adds dynamic Claude model fetching and management #1
+54
-26
@@ -58,8 +58,12 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
private availableModels: ModelOption[] = [];
|
private availableModels: ModelOption[] = [];
|
||||||
/** Debounce timer for API key changes to avoid excessive API calls */
|
/** Debounce timer for API key changes to avoid excessive API calls */
|
||||||
private apiKeyDebounceTimer: number | null = null;
|
private apiKeyDebounceTimer: number | null = null;
|
||||||
|
/** Debounce timer for settings saves to reduce disk writes */
|
||||||
|
private settingsSaveTimer: number | null = null;
|
||||||
/** Debounce delay in milliseconds */
|
/** Debounce delay in milliseconds */
|
||||||
private readonly API_KEY_DEBOUNCE_MS = 500;
|
private readonly API_KEY_DEBOUNCE_MS = 500;
|
||||||
|
/** Debounce delay for settings saves in milliseconds */
|
||||||
|
private readonly SETTINGS_SAVE_DEBOUNCE_MS = 500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new settings tab instance.
|
* Creates a new settings tab instance.
|
||||||
@@ -81,6 +85,24 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
window.clearTimeout(this.apiKeyDebounceTimer);
|
window.clearTimeout(this.apiKeyDebounceTimer);
|
||||||
this.apiKeyDebounceTimer = null;
|
this.apiKeyDebounceTimer = null;
|
||||||
}
|
}
|
||||||
|
if (this.settingsSaveTimer !== null) {
|
||||||
|
window.clearTimeout(this.settingsSaveTimer);
|
||||||
|
this.settingsSaveTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debounces settings saves to reduce disk writes on rapid changes.
|
||||||
|
* Clears any pending save and schedules a new one after the debounce delay.
|
||||||
|
*/
|
||||||
|
private debounceSaveSettings(): void {
|
||||||
|
if (this.settingsSaveTimer !== null) {
|
||||||
|
window.clearTimeout(this.settingsSaveTimer);
|
||||||
|
}
|
||||||
|
this.settingsSaveTimer = window.setTimeout(() => {
|
||||||
|
void this.plugin.saveSettings();
|
||||||
|
this.settingsSaveTimer = null;
|
||||||
|
}, this.SETTINGS_SAVE_DEBOUNCE_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +112,7 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
* of model data without blocking the UI. The model dropdown starts with
|
* of model data without blocking the UI. The model dropdown starts with
|
||||||
* a "Loading models..." placeholder and updates when data arrives.
|
* a "Loading models..." placeholder and updates when data arrives.
|
||||||
*/
|
*/
|
||||||
async display(): Promise<void> {
|
display(): void {
|
||||||
const { containerEl } = this;
|
const { containerEl } = this;
|
||||||
containerEl.empty();
|
containerEl.empty();
|
||||||
|
|
||||||
@@ -106,11 +128,8 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.addText(text => text
|
.addText(text => text
|
||||||
.setPlaceholder('sk-ant-...')
|
.setPlaceholder('sk-ant-...')
|
||||||
.setValue(this.plugin.settings.apiKey)
|
.setValue(this.plugin.settings.apiKey)
|
||||||
.onChange(async (value) => {
|
.onChange((value) => {
|
||||||
this.plugin.settings.apiKey = value;
|
this.plugin.settings.apiKey = value;
|
||||||
await this.plugin.saveSettings();
|
|
||||||
// Note: updateApiKey is called by saveSettings only if key changed
|
|
||||||
// This avoids clearing the prompt cache twice
|
|
||||||
|
|
||||||
// Clear any existing debounce timer
|
// Clear any existing debounce timer
|
||||||
if (this.apiKeyDebounceTimer !== null) {
|
if (this.apiKeyDebounceTimer !== null) {
|
||||||
@@ -118,13 +137,20 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
this.apiKeyDebounceTimer = null;
|
this.apiKeyDebounceTimer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounce model refresh to avoid excessive API calls on every keystroke
|
// Debounce both saving and model refresh to reduce disk writes and API calls
|
||||||
if (value.trim().length > 0) {
|
this.apiKeyDebounceTimer = window.setTimeout(() => {
|
||||||
this.apiKeyDebounceTimer = window.setTimeout(async () => {
|
void (async () => {
|
||||||
await this.refreshModels();
|
await this.plugin.saveSettings();
|
||||||
|
// Note: updateApiKey is called by saveSettings only if key changed
|
||||||
|
// This avoids clearing the prompt cache twice
|
||||||
|
|
||||||
|
// Refresh models if API key has content
|
||||||
|
if (value.trim().length > 0) {
|
||||||
|
await this.refreshModels();
|
||||||
|
}
|
||||||
this.apiKeyDebounceTimer = null;
|
this.apiKeyDebounceTimer = null;
|
||||||
}, this.API_KEY_DEBOUNCE_MS);
|
})();
|
||||||
}
|
}, this.API_KEY_DEBOUNCE_MS);
|
||||||
}))
|
}))
|
||||||
.then(setting => {
|
.then(setting => {
|
||||||
// Make it a password field
|
// Make it a password field
|
||||||
@@ -136,8 +162,8 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.setDesc('Test your API key to verify it works correctly')
|
.setDesc('Test your API key to verify it works correctly')
|
||||||
.addButton(button => button
|
.addButton(button => button
|
||||||
.setButtonText('Test Connection')
|
.setButtonText('Test Connection')
|
||||||
.onClick(async () => {
|
.onClick(() => {
|
||||||
await this.testApiKey(button);
|
void this.testApiKey(button);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
@@ -146,16 +172,18 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.addDropdown(dropdown => {
|
.addDropdown(dropdown => {
|
||||||
this.modelDropdown = dropdown;
|
this.modelDropdown = dropdown;
|
||||||
this.populateModelDropdown(dropdown);
|
this.populateModelDropdown(dropdown);
|
||||||
dropdown
|
// Only set value if models are loaded to avoid setting non-existent options
|
||||||
.setValue(this.plugin.settings.model)
|
if (this.availableModels.length > 0) {
|
||||||
.onChange(async (value) => {
|
dropdown.setValue(this.plugin.settings.model);
|
||||||
this.plugin.settings.model = value;
|
}
|
||||||
await this.plugin.saveSettings();
|
dropdown.onChange(async (value) => {
|
||||||
});
|
this.plugin.settings.model = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load models asynchronously without blocking UI
|
// Load models asynchronously without blocking UI
|
||||||
this.loadModelsAsync();
|
void this.loadModelsAsync();
|
||||||
|
|
||||||
// MARK: - Command Configuration
|
// MARK: - Command Configuration
|
||||||
|
|
||||||
@@ -213,9 +241,9 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.addTextArea(text => text
|
.addTextArea(text => text
|
||||||
.setPlaceholder('You are a creative writing assistant...')
|
.setPlaceholder('You are a creative writing assistant...')
|
||||||
.setValue(this.plugin.settings.systemPrompt)
|
.setValue(this.plugin.settings.systemPrompt)
|
||||||
.onChange(async (value) => {
|
.onChange((value) => {
|
||||||
this.plugin.settings.systemPrompt = value;
|
this.plugin.settings.systemPrompt = value;
|
||||||
await this.plugin.saveSettings();
|
this.debounceSaveSettings();
|
||||||
}))
|
}))
|
||||||
.then(setting => {
|
.then(setting => {
|
||||||
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '3');
|
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '3');
|
||||||
@@ -227,9 +255,9 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.addTextArea(text => text
|
.addTextArea(text => text
|
||||||
.setPlaceholder('Write about a character who...')
|
.setPlaceholder('Write about a character who...')
|
||||||
.setValue(this.plugin.settings.timedExamplePrompt)
|
.setValue(this.plugin.settings.timedExamplePrompt)
|
||||||
.onChange(async (value) => {
|
.onChange((value) => {
|
||||||
this.plugin.settings.timedExamplePrompt = value;
|
this.plugin.settings.timedExamplePrompt = value;
|
||||||
await this.plugin.saveSettings();
|
this.debounceSaveSettings();
|
||||||
}))
|
}))
|
||||||
.then(setting => {
|
.then(setting => {
|
||||||
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '2');
|
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '2');
|
||||||
@@ -241,9 +269,9 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
|
|||||||
.addTextArea(text => text
|
.addTextArea(text => text
|
||||||
.setPlaceholder('Describe a world where...')
|
.setPlaceholder('Describe a world where...')
|
||||||
.setValue(this.plugin.settings.freewritingExamplePrompt)
|
.setValue(this.plugin.settings.freewritingExamplePrompt)
|
||||||
.onChange(async (value) => {
|
.onChange((value) => {
|
||||||
this.plugin.settings.freewritingExamplePrompt = value;
|
this.plugin.settings.freewritingExamplePrompt = value;
|
||||||
await this.plugin.saveSettings();
|
this.debounceSaveSettings();
|
||||||
}))
|
}))
|
||||||
.then(setting => {
|
.then(setting => {
|
||||||
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '2');
|
setting.controlEl.querySelector('textarea')?.setAttribute('rows', '2');
|
||||||
|
|||||||
Reference in new issue
Block a user