* Build query context from recent chat messages * * @param {number} messageCount - Number of recent messages to include * @returns {string} Query text for RAG
(messageCount = 3)
| 3404 | crosslinked: crosslinked.length, |
| 3405 | linkedChunks: linkedChunks.length, |
| 3406 | beforeInclusion: combined.length, |
| 3407 | afterInclusion: filteredByInclusion.length, |
| 3408 | afterTopKLimit: finalResults.length, |
| 3409 | delivered: finalResults.length, |
| 3410 | fallback: fallbackCount, |
| 3411 | }); |
| 3412 | |
| 3413 | return finalResults; |
| 3414 | } catch (error) { |
| 3415 | CarrotDebug.error(`Failed to query RAG for ${characterName}:`, error); |
| 3416 | return []; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | // ============================================================================ |
| 3421 | // QUERY CONTEXT BUILDING |
| 3422 | // ============================================================================ |
| 3423 | |
| 3424 | /** |
| 3425 | * Build query context from recent chat messages |
| 3426 | * |
| 3427 | * @param {number} messageCount - Number of recent messages to include |
| 3428 | * @returns {string} Query text for RAG |
| 3429 | */ |
| 3430 | function buildQueryContext(messageCount = 3) { |
| 3431 | if (!chat || chat.length === 0) { |
| 3432 | return ''; |
| 3433 | } |
| 3434 | |
| 3435 | // Filter to only non-system messages (matching ST's native lorebook behavior) |
| 3436 | // This excludes: system messages, narrator messages, and any other is_system=true messages |
| 3437 | const activeMessages = chat.filter(x => !x.is_system); |
| 3438 | |
| 3439 | // Get last N active messages |
| 3440 | const recentMessages = activeMessages.slice(-messageCount); |
| 3441 | |
| 3442 | // Combine message text |
| 3443 | const queryText = recentMessages |
| 3444 | .map(msg => msg.mes || '') |
| 3445 | .filter(text => text.length > 0) |
| 3446 | .join('\n\n'); |
| 3447 | |
| 3448 | // Enhanced debug logging |
| 3449 | CarrotDebug.ui('🔍 [CarrotKernel RAG] Building query context:', { |
no test coverage detected