* Automatically wrap existing tag libraries on load (if not already wrapped)
()
| 911 | CarrotDebug.error(`Failed to backup lorebook ${lorebookName}:`, error); |
| 912 | return false; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * Automatically wrap existing tag libraries on load (if not already wrapped) |
| 918 | */ |
| 919 | async function wrapExistingTagLibraries() { |
| 920 | if (!extension_settings[extensionName].bunnymoTagWrapping) { |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | const tagLibsList = Array.from(tagLibraries); |
| 925 | if (tagLibsList.length === 0) { |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | CarrotDebug.repo(`🔄 Checking ${tagLibsList.length} tag libraries for wrapping...`); |
| 930 | |
| 931 | for (const lorebookName of tagLibsList) { |
| 932 | try { |
| 933 | // Load the lorebook to check if it needs wrapping |
| 934 | const lorebook = await loadWorldInfo(lorebookName); |
| 935 | if (!lorebook) { |
| 936 | continue; |
| 937 | } |
| 938 | |
| 939 | // Get entries |
| 940 | let entries = []; |
| 941 | if (Array.isArray(lorebook.entries)) { |
| 942 | entries = lorebook.entries; |
| 943 | } else if (lorebook.entries && typeof lorebook.entries === 'object') { |
| 944 | entries = Object.values(lorebook.entries); |
| 945 | } |
| 946 | |
| 947 | if (entries.length === 0) { |
| 948 | continue; |
| 949 | } |
| 950 | |
| 951 | // Check if any entry needs wrapping |
| 952 | let needsWrapping = false; |
| 953 | for (const entry of entries) { |
| 954 | if (!entry.content || !entry.content.trim()) { |
| 955 | continue; |
| 956 | } |
| 957 | |
| 958 | const entryName = (entry.comment || entry.key?.[0] || 'Entry').replace(/[<>]/g, ''); |
| 959 | const expectedWrapper = `<BunnymoTags:${entryName}>`; |
| 960 | |
| 961 | // If any entry is not wrapped, the lorebook needs wrapping |
| 962 | if (!entry.content.trim().startsWith(expectedWrapper)) { |
| 963 | needsWrapping = true; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | // Wrap if needed |
| 969 | if (needsWrapping) { |
| 970 | CarrotDebug.repo(`📦 Wrapping tag library: ${lorebookName}`); |
no test coverage detected