-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt-saver.js
More file actions
523 lines (451 loc) · 18.6 KB
/
prompt-saver.js
File metadata and controls
523 lines (451 loc) · 18.6 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Main Prompt Saver Extension Class
*/
import { extension_settings } from '../../../extensions.js';
import { LOG_PREFIX, EXTENSION_NAME, generateId, sanitizeHtml, formatDate, showToast, createModal, getExtensionSettings, saveExtensionSettings } from './utils.js';
export class PromptSaverExtension {
constructor() {
this.extensionName = EXTENSION_NAME;
this.isInitialized = false;
this.prompts = {};
this.currentModal = null;
}
/**
* Initialize the extension
*/
async initialize() {
try {
console.log(`${LOG_PREFIX} Initializing extension...`);
// Load saved prompts
await this.loadPrompts();
// Setup storage
this.setupStorage();
this.isInitialized = true;
console.log(`${LOG_PREFIX} Extension initialized successfully`);
} catch (error) {
console.error(`${LOG_PREFIX} Failed to initialize:`, error);
throw error;
}
}
/**
* Load prompts from storage
*/
async loadPrompts() {
try {
const settings = getExtensionSettings();
this.prompts = settings.prompts || {};
console.log(`${LOG_PREFIX} Loaded ${Object.keys(this.prompts).length} prompts`);
} catch (error) {
console.error(`${LOG_PREFIX} Failed to load prompts:`, error);
this.prompts = {};
}
}
/**
* Save prompts to storage
*/
async savePrompts() {
try {
saveExtensionSettings({ prompts: this.prompts });
console.log(`${LOG_PREFIX} Saved ${Object.keys(this.prompts).length} prompts`);
} catch (error) {
console.error(`${LOG_PREFIX} Failed to save prompts:`, error);
}
}
/**
* Setup storage mechanisms
*/
setupStorage() {
// Auto-save every 30 seconds
setInterval(() => {
this.savePrompts();
}, 30000);
}
/**
* Add navigation button to SillyTavern
*/
async addNavigationButton() {
try {
// Look for existing navigation buttons to understand the structure
const existingNavButton = document.querySelector('#left-nav-panel .nav_button');
if (!existingNavButton) {
console.warn(`${LOG_PREFIX} No existing navigation buttons found to copy structure from`);
return;
}
// Check if button already exists
if (document.querySelector('#prompt-saver-nav-button')) {
return;
}
// Create button using the same structure as existing nav buttons
const button = document.createElement('div');
button.id = 'prompt-saver-nav-button';
button.className = existingNavButton.className; // Copy classes from existing button
button.title = 'Open Prompt Library';
button.innerHTML = `
<i class="fa-solid fa-bookmark"></i>
<span>Prompt Library</span>
`;
button.addEventListener('click', () => {
this.showPromptLibrary();
});
// Insert after the last navigation button
const navPanel = document.querySelector('#left-nav-panel');
const lastNavButton = navPanel.querySelector('.nav_button:last-of-type');
if (lastNavButton) {
lastNavButton.parentNode.insertBefore(button, lastNavButton.nextSibling);
} else {
navPanel.appendChild(button);
}
console.log(`${LOG_PREFIX} Navigation button added`);
} catch (error) {
console.error(`${LOG_PREFIX} Failed to add navigation button:`, error);
}
}
/**
* Integrate with SillyTavern's prompt manager
*/
async integrateWithPromptManager() {
try {
const promptManager = document.querySelector('#completion_prompt_manager');
if (!promptManager) {
console.warn(`${LOG_PREFIX} Prompt manager not found`);
return;
}
// Add save button to prompt manager
this.addSaveButton();
console.log(`${LOG_PREFIX} Integrated with prompt manager`);
} catch (error) {
console.error(`${LOG_PREFIX} Failed to integrate with prompt manager:`, error);
}
}
/**
* Add save button to prompt manager
*/
addSaveButton() {
// Check if button already exists
if (document.querySelector('#prompt-saver-save-button')) {
return;
}
// Look for the prompt manager list container
const promptList = document.querySelector('#completion_prompt_manager_list');
if (!promptList) {
console.warn(`${LOG_PREFIX} Prompt manager list not found`);
return;
}
// Create a toolbar container if it doesn't exist
let toolbar = document.querySelector('#prompt-saver-toolbar');
if (!toolbar) {
toolbar = document.createElement('div');
toolbar.id = 'prompt-saver-toolbar';
toolbar.className = 'prompt-saver-toolbar';
toolbar.innerHTML = `
<button id="prompt-saver-save-button" title="Save current prompts to library" class="menu_button interactable" tabindex="0">
<i class="fa-solid fa-bookmark"></i> Save Current Prompts
</button>
<button id="prompt-saver-library-button" title="Open prompt library" class="menu_button interactable" tabindex="0">
<i class="fa-solid fa-book"></i> Open Library
</button>
`;
// Insert toolbar before the prompt list
promptList.parentElement.insertBefore(toolbar, promptList);
}
// Add event listeners
const saveButton = document.querySelector('#prompt-saver-save-button');
const libraryButton = document.querySelector('#prompt-saver-library-button');
if (saveButton) {
saveButton.addEventListener('click', () => {
this.saveCurrentPrompts();
});
}
if (libraryButton) {
libraryButton.addEventListener('click', () => {
this.showPromptLibrary();
});
}
console.log(`${LOG_PREFIX} Prompt manager buttons added`);
}
/**
* Save current prompts from the active preset
*/
async saveCurrentPrompts() {
try {
console.log(`${LOG_PREFIX} Saving current prompts...`);
// Get current preset data
const currentPreset = this.getCurrentPreset();
if (!currentPreset || !currentPreset.prompts) {
showToast('No prompts found in current preset', 'warning');
return;
}
let savedCount = 0;
for (const prompt of currentPreset.prompts) {
if (prompt.content && prompt.content.trim()) {
const promptId = generateId();
const promptData = {
id: promptId,
name: prompt.name || `Prompt from ${currentPreset.name || 'Unknown Preset'}`,
content: prompt.content,
role: prompt.role || 'user',
metadata: {
created_at: new Date().toISOString(),
source_preset: currentPreset.name || 'Unknown',
favorite: false,
usage_count: 0
}
};
this.prompts[promptId] = promptData;
savedCount++;
}
}
await this.savePrompts();
showToast(`Saved ${savedCount} prompts successfully`, 'success');
} catch (error) {
console.error(`${LOG_PREFIX} Failed to save current prompts:`, error);
showToast('Failed to save prompts', 'error');
}
}
/**
* Get current preset data
*/
getCurrentPreset() {
// Try to get from SillyTavern's global state
if (typeof power_user !== 'undefined' && power_user.completion_preset) {
return power_user.completion_preset;
}
// Fallback: try to extract from UI
return this.extractPresetFromUI();
}
/**
* Extract preset data from UI elements
*/
extractPresetFromUI() {
try {
const promptElements = document.querySelectorAll('.completion_prompt_manager_prompt');
const prompts = [];
promptElements.forEach(element => {
const nameElement = element.querySelector('.completion_prompt_manager_prompt_name');
const contentElement = element.querySelector('.completion_prompt_manager_prompt_text');
const roleElement = element.querySelector('.completion_prompt_manager_prompt_role');
if (contentElement && contentElement.value) {
prompts.push({
name: nameElement ? nameElement.value : 'Unnamed Prompt',
content: contentElement.value,
role: roleElement ? roleElement.value : 'user'
});
}
});
return {
name: 'Current Preset',
prompts: prompts
};
} catch (error) {
console.error(`${LOG_PREFIX} Failed to extract preset from UI:`, error);
return { prompts: [] };
}
}
/**
* Show the prompt library modal
*/
showPromptLibrary() {
try {
const content = this.generateLibraryHTML();
// Use SillyTavern's popup system if available
if (typeof callGenericPopup === 'function') {
callGenericPopup(content, 'Prompt Library', '', '', 'prompt-library-popup');
} else {
// Fallback to custom modal
this.currentModal = createModal('Prompt Library', content, {
footer: `
<button type="button" class="menu_button" onclick="document.querySelector('.modal').style.display='none'">Close</button>
<button type="button" class="menu_button" onclick="window.promptSaver.exportPrompts()">
<i class="fa-solid fa-download"></i> Export
</button>
`
});
// Show modal
this.currentModal.style.display = 'block';
}
} catch (error) {
console.error(`${LOG_PREFIX} Failed to show prompt library:`, error);
}
}
/**
* Generate HTML for the prompt library
*/
generateLibraryHTML() {
const promptList = Object.values(this.prompts);
if (promptList.length === 0) {
return '<p>No saved prompts found. Save some prompts from the prompt manager to get started!</p>';
}
let html = `
<div class="prompt-library-container">
<div class="prompt-library-header">
<input type="text" class="text_pole" placeholder="Search prompts..." id="prompt-search">
<div class="prompt-library-filters">
<button class="menu_button interactable" title="Show favorites only" onclick="window.promptSaver.filterFavorites()" tabindex="0">
<i class="fa-solid fa-star"></i> Favorites
</button>
<button class="menu_button interactable" title="Show all prompts" onclick="window.promptSaver.clearFilters()" tabindex="0">
<i class="fa-solid fa-list"></i> All
</button>
<button class="menu_button interactable" title="Export prompts" onclick="window.promptSaver.exportPrompts()" tabindex="0">
<i class="fa-solid fa-download"></i> Export
</button>
</div>
</div>
<div class="prompt-library-list" id="prompt-library-list">
`;
promptList.forEach(prompt => {
html += `
<div class="prompt-card" data-prompt-id="${prompt.id}">
<div class="prompt-card-header">
<h6>${sanitizeHtml(prompt.name)}</h6>
<div class="prompt-card-actions">
<button class="menu_button interactable" title="Toggle favorite" onclick="window.promptSaver.toggleFavorite('${prompt.id}')" tabindex="0">
<i class="fa-${prompt.metadata.favorite ? 'solid' : 'regular'} fa-star"></i>
</button>
<button class="menu_button interactable" title="Apply prompt" onclick="window.promptSaver.applyPrompt('${prompt.id}')" tabindex="0">
<i class="fa-solid fa-play"></i> Apply
</button>
<button class="menu_button interactable" title="Delete prompt" onclick="window.promptSaver.deletePrompt('${prompt.id}')" tabindex="0">
<i class="fa-solid fa-trash"></i> Delete
</button>
</div>
</div>
<div class="prompt-card-content">
<p>${sanitizeHtml(prompt.content.substring(0, 200))}${prompt.content.length > 200 ? '...' : ''}</p>
<small class="text-muted">
Role: ${prompt.role} | Created: ${formatDate(prompt.metadata.created_at)}
${prompt.metadata.usage_count ? ` | Used: ${prompt.metadata.usage_count} times` : ''}
</small>
</div>
</div>
`;
});
html += '</div></div>';
return html;
}
/**
* Refresh the extension
*/
refresh() {
console.log(`${LOG_PREFIX} Refreshing extension...`);
this.loadPrompts();
}
/**
* Update context when character changes
*/
updateContext() {
console.log(`${LOG_PREFIX} Updating context...`);
// Implementation for character-specific context
}
/**
* Update UI when preset changes
*/
updateUI() {
console.log(`${LOG_PREFIX} Updating UI...`);
// Implementation for UI updates
}
/**
* Add settings panel
*/
async addSettingsPanel() {
// Implementation for settings panel
console.log(`${LOG_PREFIX} Settings panel integration not yet implemented`);
}
/**
* Toggle favorite status of a prompt
*/
toggleFavorite(promptId) {
if (this.prompts[promptId]) {
this.prompts[promptId].metadata.favorite = !this.prompts[promptId].metadata.favorite;
this.savePrompts();
showToast(`Prompt ${this.prompts[promptId].metadata.favorite ? 'added to' : 'removed from'} favorites`, 'success');
// Refresh the library if it's open
if (this.currentModal) {
const listContainer = document.getElementById('prompt-library-list');
if (listContainer) {
listContainer.innerHTML = this.generateLibraryHTML().match(/<div class="prompt-library-list"[^>]*>(.*)<\/div>/s)[1];
}
}
}
}
/**
* Apply a prompt to the current preset
*/
async applyPrompt(promptId) {
try {
const prompt = this.prompts[promptId];
if (!prompt) {
showToast('Prompt not found', 'error');
return;
}
// Update usage count
prompt.metadata.usage_count = (prompt.metadata.usage_count || 0) + 1;
prompt.metadata.last_used = new Date().toISOString();
// Try to apply to current preset
// This is a simplified implementation - in a real scenario, you'd need to
// integrate with SillyTavern's preset system
console.log(`${LOG_PREFIX} Applying prompt: ${prompt.name}`);
this.savePrompts();
showToast(`Applied prompt: ${prompt.name}`, 'success');
} catch (error) {
console.error(`${LOG_PREFIX} Failed to apply prompt:`, error);
showToast('Failed to apply prompt', 'error');
}
}
/**
* Delete a prompt
*/
deletePrompt(promptId) {
if (this.prompts[promptId]) {
const promptName = this.prompts[promptId].name;
delete this.prompts[promptId];
this.savePrompts();
showToast(`Deleted prompt: ${promptName}`, 'success');
// Refresh the library if it's open
if (this.currentModal) {
const listContainer = document.getElementById('prompt-library-list');
if (listContainer) {
listContainer.innerHTML = this.generateLibraryHTML().match(/<div class="prompt-library-list"[^>]*>(.*)<\/div>/s)[1];
}
}
}
}
/**
* Filter to show only favorites
*/
filterFavorites() {
// Implementation for filtering favorites
console.log(`${LOG_PREFIX} Filtering favorites...`);
}
/**
* Clear all filters
*/
clearFilters() {
// Implementation for clearing filters
console.log(`${LOG_PREFIX} Clearing filters...`);
}
/**
* Export prompts
*/
exportPrompts() {
try {
const exportData = {
version: '1.0.0',
exported_at: new Date().toISOString(),
prompts: this.prompts
};
const dataStr = JSON.stringify(exportData, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(dataBlob);
link.download = `prompt-library-${new Date().toISOString().split('T')[0]}.json`;
link.click();
showToast('Prompts exported successfully', 'success');
} catch (error) {
console.error(`${LOG_PREFIX} Failed to export prompts:`, error);
showToast('Failed to export prompts', 'error');
}
}
}
// Make extension available globally for HTML onclick handlers
window.promptSaver = new PromptSaverExtension();