* Process Tag Library entries for template-based injections * When Tag Library entries activate, check for associated templates and inject them
(tagEntries)
| 2464 | * When Tag Library entries activate, check for associated templates and inject them |
| 2465 | */ |
| 2466 | async function processTagLibraryInjections(tagEntries) { |
| 2467 | if (!tagEntries || tagEntries.length === 0) { |
| 2468 | return; |
| 2469 | } |
| 2470 | |
| 2471 | CarrotDebug.inject(`🏷️ Starting Tag Library injection processing`, { |
| 2472 | totalEntries: tagEntries.length, |
| 2473 | entries: tagEntries.map(e => ({ |
| 2474 | world: e.world, |
| 2475 | key: e.key, |
| 2476 | comment: e.comment |
| 2477 | })) |
| 2478 | }); |
| 2479 | |
| 2480 | const settings = extension_settings[extensionName]; |
| 2481 | |
| 2482 | for (const entry of tagEntries) { |
| 2483 | try { |
| 2484 | // Extract tag name from entry |
| 2485 | const tagName = entry.comment || entry.key?.[0] || 'Unknown'; |
| 2486 | const lorebookName = entry.world; |
| 2487 | |
| 2488 | // Look for a template matching this tag entry |
| 2489 | // Template name should match the entry name or have a trigger keyword |
| 2490 | const matchingTemplate = findTemplateForEntry(entry); |
| 2491 | |
| 2492 | if (matchingTemplate) { |
| 2493 | // Only log when we actually find and process a template |
| 2494 | CarrotDebug.inject(`💉 Injecting tag template: "${tagName}" using ${matchingTemplate.name}`); |
| 2495 | |
| 2496 | // Process template with entry data |
| 2497 | const templateData = { |
| 2498 | entry: entry, |
| 2499 | tagName: tagName, |
| 2500 | content: entry.content, |
| 2501 | lorebookName: lorebookName, |
| 2502 | key: entry.key, |
| 2503 | comment: entry.comment |
| 2504 | }; |
| 2505 | |
| 2506 | let injectionText = await CarrotTemplateManager.processTemplate(matchingTemplate, templateData); |
| 2507 | |
| 2508 | // Ensure string output |
| 2509 | if (typeof injectionText !== 'string') { |
| 2510 | injectionText = String(injectionText || ''); |
| 2511 | } |
| 2512 | |
| 2513 | // Use template settings for injection |
| 2514 | const depth = matchingTemplate.depth !== undefined ? matchingTemplate.depth : settings.injectionDepth || 4; |
| 2515 | const role = matchingTemplate.role || settings.injectionRole || 'system'; |
| 2516 | const position = matchingTemplate.position || 'chat'; |
| 2517 | |
| 2518 | // Create unique ID for this injection |
| 2519 | const injectionId = `carrot-tag-${lorebookName.replace(/[^a-zA-Z0-9]/g, '_')}-${tagName.replace(/[^a-zA-Z0-9]/g, '_')}`; |
| 2520 | |
| 2521 | const injectionCommand = `/inject id=${injectionId} position=${position} ephemeral=true scan=true depth=${depth} role=${role} ${injectionText}`; |
| 2522 | |
| 2523 | await executeSlashCommandsWithOptions(injectionCommand, { displayCommand: false, showOutput: false }); |
no test coverage detected