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.
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.
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.
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.
| 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 |
/Comserv/lib/Comserv/Controller/AI.pm
sub chat :Local :Args(0) # Lines 492-731 (after fix)
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
};
| Field | Value |
|---|---|
| user_id | Current user's ID from session |
| title | First 80 chars of user prompt |
| status | 'active' |
| created_at | CURRENT_TIMESTAMP (automatic) |
User Message:
Assistant Message:
-- 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;
{
"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
}