Fixed the /ai/chat endpoint to save conversations to the database. Enables conversation persistence and history in the new chat interface. Updated Jan 2 with metadata column schema fix. Fixed the /ai/chat endpoint to save conversations to the database. Enables conversation persistence and history in the new chat interface. Updated Jan 2 with metadata column schema fix. https://feedthebees.beemaster.ca: : AIChatConversationSavingFix

Welcome Guest to Defaut site!

AI Chat Conversation Saving Fix

December 27, 2025

Created: 2025-12-27 01:20 UTC | Updated: 2026-01-02 15:43 UTC | Status: ✅ IMPLEMENTED & SCHEMA FIXED | Related: Schema Fix Dec 26 | Daily Plan Dec 27

📋 Update: Jan 2, 2026 - Metadata Column Schema Fix

Regression Fixed: On Dec 27-30, conversations were created but messages failed to save. Root cause: Missing metadata column in ai_conversations table.

Fix Applied: ALTER TABLE ai_conversations ADD COLUMN metadata longtext NULL. Production database updated 2026-01-02 at 15:05 UTC.

Impact: This fix enables the conversation saving functionality described in this document to work correctly. Future message creation will now persist metadata properly.

✅ Chat Endpoint Now Saves Conversations

Issue Fixed: The /ai/chat endpoint (used by the new chat interface) was NOT saving conversations to the database, even though the form-based /ai/result endpoint did.

Impact: Users could chat with AI but conversations disappeared after page reload and weren't visible in the Conversations list.

Problem Description

What Was Happening

Root Cause

The /ai/chat endpoint (added for the modern chat interface) was designed to return JSON responses quickly, but it was missing the database save logic. The older /ai/result endpoint (form-based) did include database saving, but the new endpoint did not.

Comparison

Feature /ai/result (Form) /ai/chat (New) - BEFORE /ai/chat (New) - AFTER
Saves Conversation ✅ Yes ❌ No ✅ Yes
Saves User Message ✅ Yes ❌ No ✅ Yes
Saves AI Response ✅ Yes ❌ No ✅ Yes
Returns conversation_id ✅ In stash ❌ Not available ✅ In JSON

Fix Applied

File Modified

/Comserv/lib/Comserv/Controller/AI.pm

Method

sub chat :Local :Args(0)  # Lines 492-731 (after fix)

Changes

Added database conversation saving logic after successful Ollama response (lines 636-700):

# Save conversation to database
my $conversation_id;
try {
    my $user_id = $c->session->{user_id};
    my $schema = $c->model('DBEncy')->schema;
    
    # Generate title from first prompt
    my $title = substr($prompt, 0, 80) || 'Chat Conversation';
    
    # Create conversation
    my $conversation = $schema->resultset('AiConversation')->create({
        user_id => $user_id,
        title => $title,
        status => 'active'
    });
    
    $conversation_id = $conversation->id;
    
    # Save user message
    $schema->resultset('AiMessage')->create({
        conversation_id => $conversation_id,
        user_id => $user_id,
        role => 'user',
        content => $prompt,
        # ... additional fields ...
    });
    
    # Save AI response message
    $schema->resultset('AiMessage')->create({
        conversation_id => $conversation_id,
        user_id => $user_id,
        role => 'assistant',
        content => $ai_response,
        # ... additional fields ...
    });
} catch {
    # Log error but don't fail the response
};

Key Features of Fix

Data Saved Per Chat Message

AiConversation Record

Field Value
user_id Current user's ID from session
title First 80 chars of user prompt
status 'active'
created_at CURRENT_TIMESTAMP (automatic)

AiMessage Records (2 created per chat)

User Message:

Assistant Message:

Testing the Fix

Verification Steps

  1. Navigate to AI Chat: Go to /ai in your browser
  2. Send a Message: Type a prompt and click Send
  3. Verify Response: AI should respond normally
  4. Check Conversations: Navigate to /ai/conversations
  5. Verify Persistence: Your chat should appear in the list
  6. Click View: Click on the conversation to see both messages
  7. Check Page Reload: Refresh the page - conversation should still be there

Expected Results

Database Impact

Tables Modified

Query Example

-- Find conversations created via /ai/chat interface
SELECT c.id, c.user_id, c.title, COUNT(m.id) as message_count
FROM ai_conversations c
LEFT JOIN ai_messages m ON c.id = m.conversation_id
WHERE c.user_id = 1
GROUP BY c.id
ORDER BY c.created_at DESC;

JSON Response Example

Success Response

{
  "success": true,
  "response": "Hello! This is an AI response...",
  "model": "qwen2.5-coder:1.5b-base",
  "conversation_id": 42,
  "created_at": "2025-12-27T01:20:00Z",
  "total_duration": 2450,
  "eval_count": 256
}

Related Changes

Future Improvements