diff --git a/src/revolverEngine.ts b/src/revolverEngine.ts index b0579b8..279b5c0 100644 --- a/src/revolverEngine.ts +++ b/src/revolverEngine.ts @@ -123,60 +123,24 @@ export class RevolverEngine { return sorted; } - public convertToGeminiContent(messages: any[]): { + private convertToGeminiContent(messages: any[]): { systemInstruction?: string; geminiMessages: any[]; } { let systemInstruction = ''; - const rawMessages: { role: 'user' | 'model'; parts: { text: string }[] }[] = []; + const geminiMessages: any[] = []; for (const msg of messages) { if (msg.role === 'system') { - const text = typeof msg.content === 'string' ? msg.content : (msg.content != null ? JSON.stringify(msg.content) : ''); - systemInstruction += (systemInstruction ? '\n\n' : '') + text; + systemInstruction += (systemInstruction ? '\n\n' : '') + (typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content)); } else { - const text = typeof msg.content === 'string' ? msg.content : (msg.content != null ? JSON.stringify(msg.content) : ''); - rawMessages.push({ + geminiMessages.push({ role: msg.role === 'assistant' ? 'model' : 'user', - parts: [{ text }] + parts: [{ text: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content) }] }); } } - // Step 1: Prepend empty user placeholder if the first non-system message is from 'model' (Option 1) - if (rawMessages.length > 0 && rawMessages[0].role === 'model') { - rawMessages.unshift({ - role: 'user', - parts: [{ text: '' }] - }); - } - - // Step 2: Ensure normalization by merging consecutive messages with the same role (Option 3) - const geminiMessages: { role: 'user' | 'model'; parts: { text: string }[] }[] = []; - for (const msg of rawMessages) { - if (geminiMessages.length === 0) { - geminiMessages.push({ role: msg.role, parts: [{ text: msg.parts[0]?.text ?? '' }] }); - } else { - const prevMsg = geminiMessages[geminiMessages.length - 1]; - if (prevMsg.role === msg.role) { - const prevText = prevMsg.parts[0]?.text ?? ''; - const currText = msg.parts[0]?.text ?? ''; - const mergedText = !prevText ? currText : (!currText ? prevText : `${prevText}\n\n${currText}`); - prevMsg.parts = [{ text: mergedText }]; - } else { - geminiMessages.push({ role: msg.role, parts: [{ text: msg.parts[0]?.text ?? '' }] }); - } - } - } - - // Ensure the conversation ends with a user message for Gemini chat completion - if (geminiMessages.length > 0 && geminiMessages[geminiMessages.length - 1].role === 'model') { - geminiMessages.push({ - role: 'user', - parts: [{ text: '' }] - }); - } - return { systemInstruction, geminiMessages }; } diff --git a/test-revolver.ts b/test-revolver.ts index b50fc38..831a70f 100644 --- a/test-revolver.ts +++ b/test-revolver.ts @@ -102,52 +102,6 @@ async function runTests() { assert.strictEqual(maskedShort, '****'); console.log(' ✓ Key masking security verified.'); - // TEST 5: Gemini Message Normalization & Empty Placeholder Prepending - console.log('\n5. Testing Gemini message normalization & role alternation...'); - - // 5a. Assistant first with system instruction - const conv1 = engine.convertToGeminiContent([ - { role: 'system', content: 'You are a helpful assistant.' }, - { role: 'assistant', content: 'Hello! How can I help you today?' }, - { role: 'user', content: 'What is 2+2?' } - ]); - assert.strictEqual(conv1.systemInstruction, 'You are a helpful assistant.'); - assert.strictEqual(conv1.geminiMessages.length, 3); - assert.strictEqual(conv1.geminiMessages[0].role, 'user', 'First message must be normalized to user role'); - assert.strictEqual(conv1.geminiMessages[0].parts[0].text, '', 'Leading placeholder user message should be empty'); - assert.strictEqual(conv1.geminiMessages[1].role, 'model'); - assert.strictEqual(conv1.geminiMessages[1].parts[0].text, 'Hello! How can I help you today?'); - assert.strictEqual(conv1.geminiMessages[2].role, 'user'); - assert.strictEqual(conv1.geminiMessages[2].parts[0].text, 'What is 2+2?'); - - // 5b. Consecutive assistant messages merged, consecutive user messages merged - const conv2 = engine.convertToGeminiContent([ - { role: 'assistant', content: 'Part 1' }, - { role: 'assistant', content: 'Part 2' }, - { role: 'user', content: 'Question 1' }, - { role: 'user', content: 'Question 2' } - ]); - assert.strictEqual(conv2.geminiMessages.length, 3); - assert.strictEqual(conv2.geminiMessages[0].role, 'user'); - assert.strictEqual(conv2.geminiMessages[0].parts[0].text, ''); - assert.strictEqual(conv2.geminiMessages[1].role, 'model'); - assert.strictEqual(conv2.geminiMessages[1].parts[0].text, 'Part 1\n\nPart 2'); - assert.strictEqual(conv2.geminiMessages[2].role, 'user'); - assert.strictEqual(conv2.geminiMessages[2].parts[0].text, 'Question 1\n\nQuestion 2'); - - // 5c. Conversation ending with an assistant turn ensures trailing user turn - const conv3 = engine.convertToGeminiContent([ - { role: 'user', content: 'Tell a story' }, - { role: 'assistant', content: 'Once upon a time' } - ]); - assert.strictEqual(conv3.geminiMessages.length, 3); - assert.strictEqual(conv3.geminiMessages[0].role, 'user'); - assert.strictEqual(conv3.geminiMessages[1].role, 'model'); - assert.strictEqual(conv3.geminiMessages[2].role, 'user'); - assert.strictEqual(conv3.geminiMessages[2].parts[0].text, ''); - - console.log(' ✓ Empty placeholder prepending and turn alternation normalization verified.'); - // Clean up test file if (fs.existsSync(testConfigPath)) { fs.unlinkSync(testConfigPath);