| 82 | } |
| 83 | |
| 84 | export class CarrotStorageManager { |
| 85 | constructor(contextManager) { |
| 86 | this.contextManager = contextManager; |
| 87 | this.defaultSettings = { |
| 88 | enabledRepos: new Set(), |
| 89 | autoScanEnabled: false, |
| 90 | scanOnStartup: false, |
| 91 | displaySettings: { |
| 92 | showCards: true, |
| 93 | groupByCharacter: true, |
| 94 | compactMode: false |
| 95 | } |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | // Get settings with proper hierarchy: chat > character > global |
| 100 | async getSettings() { |
| 101 | const context = this.contextManager.getCurrentContext(); |
| 102 | |
| 103 | // Start with global settings |
| 104 | let settings = { ...this.defaultSettings }; |
| 105 | const globalSettings = extension_settings[extensionName] || {}; |
| 106 | Object.assign(settings, globalSettings); |
| 107 | |
| 108 | // Override with character-specific settings if available |
| 109 | if (context.characterId && context.characters) { |
| 110 | const character = context.characters[context.characterId]; |
| 111 | if (character?.data?.extensions?.[extensionName]) { |
| 112 | const charSettings = character.data.extensions[extensionName]; |
| 113 | Object.assign(settings, charSettings); |
| 114 | CarrotDebug.scan(`Applied character-specific settings for ${character.name}`); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Override with chat-specific settings (highest priority) |
| 119 | if (context.chatId && chat_metadata?.[extensionName]) { |
| 120 | const chatSettings = chat_metadata[extensionName]; |
| 121 | Object.assign(settings, chatSettings); |
| 122 | CarrotDebug.scan(`Applied chat-specific settings for chat ${context.chatId}`); |
| 123 | } |
| 124 | |
| 125 | // Convert enabledRepos to Set if it's an array |
| 126 | if (Array.isArray(settings.enabledRepos)) { |
| 127 | settings.enabledRepos = new Set(settings.enabledRepos); |
| 128 | } |
| 129 | |
| 130 | CarrotDebug.scan('Settings hierarchy resolved:', settings); |
| 131 | return settings; |
| 132 | } |
| 133 | |
| 134 | // Save settings at the appropriate level |
| 135 | async saveSettings(settings, level = 'global') { |
| 136 | const context = this.contextManager.getCurrentContext(); |
| 137 | |
| 138 | // Convert Set to Array for JSON serialization |
| 139 | const serializableSettings = { ...settings }; |
| 140 | if (serializableSettings.enabledRepos instanceof Set) { |
| 141 | serializableSettings.enabledRepos = Array.from(serializableSettings.enabledRepos); |
nothing calls this directly
no outgoing calls
no test coverage detected