Fixed multi-turn conversation support and improved conversation preview in /ai/conversations list. Users can now ask follow-up questions in the same conversation. Fixed multi-turn conversation support and improved conversation preview in /ai/conversations list. Users can now ask follow-up questions in the same conversation. https://feedthebees.beemaster.ca: : AI Chat Multi Turn Conversation Fix

Welcome Guest to Defaut site!

AI Chat Multi-Turn Conversation & Preview Fix

December 27, 2025

Created: 2025-12-27 01:27 UTC | Status: ✅ IMPLEMENTED | Related: Conversation Saving Fix | View Conversations

✅ Multi-Turn Conversations Now Work

Issues Fixed:

Problems Addressed

Issue #1: Each Prompt Created New Conversation

What Was Happening:

Impact:

Issue #2: Conversations List Shows Only Numbers

What Was Happening:

Example of Old View:

Conversation #1234
Status: active
Messages: 2
Created: 2025-12-27 01:15
[Preview text - possibly just AI response]
        

Example of New View:

What is machine learning?
Status: active
Messages: 4
Created: 2025-12-27 01:15
Q: What is machine learning? | A: Machine learning is a subset of...
        

Solutions Implemented

Fix #1: Support Multi-Turn Conversations

Backend Changes (/Comserv/lib/Comserv/Controller/AI.pm)

1. Accept conversation_id parameter:

my $conversation_id = $json_data->{conversation_id} || 
                   $c->request->params->{conversation_id};

2. Reuse existing conversation OR create new:

unless ($final_conversation_id) {
    # Create new conversation (first message)
    my $conversation = $schema->resultset('AiConversation')->create({...});
    $final_conversation_id = $conversation->id;
} else {
    # Reuse existing conversation (follow-up message)
    # Just log and continue
}

3. Add messages to the same conversation:

# Both user and AI messages use the SAME conversation_id
$schema->resultset('AiMessage')->create({
    conversation_id => $final_conversation_id,
    role => 'user',
    content => $prompt,
    ...
});

$schema->resultset('AiMessage')->create({
    conversation_id => $final_conversation_id,
    role => 'assistant',
    content => $ai_response,
    ...
});

Frontend Changes (/Comserv/root/ai/index.tt)

1. Track current conversation ID:

let currentConversationId = null;

2. Include conversation_id in requests:

const requestBody = {
    prompt: prompt,
    model: selectedModel,
    history: conversationHistory.slice(-10)
};

if (currentConversationId) {
    requestBody.conversation_id = currentConversationId;
}

3. Update conversation ID when response received:

if (data.conversation_id) {
    currentConversationId = data.conversation_id;
    // Display it and show "saved" notification
}

4. Display current conversation ID:

document.getElementById('conversation-id-display').textContent = 
    data.conversation_id;
document.getElementById('conversation-info').style.display = 'block';

Fix #2: Improve Conversation Preview

Backend Changes (/Comserv/lib/Comserv/Controller/AI.pm)

Updated the conversations action to generate better preview text:

# Get all messages for conversation
my @messages = $conv->ai_messages->all;

# Find first user message (question) and AI response (answer)
foreach my $msg (@messages) {
    if ($msg->role eq 'user' && !$user_msg) {
        $user_msg = $msg->content;
    }
    if ($msg->role eq 'assistant' && !$ai_msg) {
        $ai_msg = $msg->content;
    }
}

# Create readable preview
$preview = "Q: " . substr($user_msg, 0, 60) . "...";
$preview .= " | A: " . substr($ai_msg, 0, 40) . "...";
            

Result Format:

Q: What is the difference between AI and machine learning? | A: AI is the broader field, while machine learning is...
        

User Experience Flow

How It Works Now

Step 1: First Message

  1. User types prompt and sends
  2. Server creates new conversation
  3. Server saves both user message and AI response
  4. Server returns conversation_id: 42
  5. Client displays: "Conversation #42 saved"
  6. Conversation ID "42" appears in header

Step 2: Follow-Up Question (Same Conversation)

  1. User types follow-up prompt
  2. Client sends request WITH conversation_id: 42
  3. Server finds existing conversation #42
  4. Server adds new message pair to conversation #42
  5. Conversation now has 4 messages (2 Q&A pairs)
  6. No new "saved" notification (same conversation)

Step 3: View Conversation

  1. User clicks "Conversations" button
  2. Sees conversation list with improved previews
  3. Preview shows: Q: [first question] | A: [first answer snippet]
  4. Message count shows "4" (all Q&A pairs)
  5. User can click "View" to see full conversation with all exchanges

Technical Details

Files Modified

File Changes
Controller/AI.pm - Added conversation_id parameter parsing
- Modified conversation saving logic to support reuse
- Updated conversations action preview generation
root/ai/index.tt - Added currentConversationId state variable
- Updated fetch request to include conversation_id
- Added conversation ID display in header
- Show "saved" notification on first message

Database Impact

API Changes

/ai/chat Request (NEW):

{
  "prompt": "What is Python?",
  "model": "qwen2.5-coder:1.5b-base",
  "history": [...],
  "conversation_id": 42  // ← NEW: Include for follow-up
}

/ai/chat Response (SAME):

{
  "success": true,
  "response": "Python is a high-level programming language...",
  "model": "qwen2.5-coder:1.5b-base",
  "conversation_id": 42  // ← Still returned (now reused)
}

Testing the Fix

Verification Steps

  1. Start fresh conversation:
    • Go to /ai
    • Type first prompt and send
    • Verify: "Conversation #[N] saved" appears
    • Verify: "Conversation #[N]" shows in header
  2. Ask follow-up question:
    • Type another prompt in same chat
    • Send it
    • Verify: NO new "saved" notification (same conversation)
    • Verify: Conversation ID in header stays the same
  3. Check conversation list:
    • Click "Conversations" button
    • Verify: Only ONE conversation shows (not multiple)
    • Verify: Preview shows "Q: [first question] | A: [answer snippet]"
    • Verify: Message count shows "4" (2 Q&A pairs)
  4. View conversation details:
    • Click "View" on conversation
    • Verify: All 4 messages show (2 from user, 2 from AI)
    • Verify: Messages appear in order (chronological)
  5. Test persistence:
    • Go back to /ai
    • Do NOT select the conversation
    • Start new conversation (different topic)
    • Verify: NEW conversation_id appears in header
    • Check conversations list
    • Verify: BOTH conversations exist with different previews

Expected Results

Test Expected Behavior Result
First message saved Notification shows conversation ID
Follow-up in same conversation Same conversation ID, no new notification
Conversation list preview Shows Q & A format, not just numbers
View full conversation Shows all message pairs in order

Related Fixes