AI Service: Database Schema¶
This document defines the data structures used by the ai_service for session management and long-term data persistence.
1. Redis: Session State¶
Redis is used to maintain the state of active conversations for fast retrieval.
- Key Format:
session:<session_id> - Type: Hash
- Value: A JSON object serializing the session's current state.
Session Object Schema¶
{
"sessionId": "z9y8-x7w6-v5u4-t3s2",
"userId": "c7a8b9d0-e1f2-g3h4-i5j6-k7l8m9n0o1p2",
"messages": [
{ "speaker": "user", "content": "add jdoe to auditors" },
{ "speaker": "ai", "content": "This will add user 'jdoe' to the 'auditors' group.", "type": "plan_confirmation_required", "planId": "a1b2-c3d4-e5f6-g7h8" }
],
"pendingPlanId": "a1b2-c3d4-e5f6-g7h8",
"createdAt": "2025-09-17T10:00:00Z"
}
pendingPlanId: Stores the ID of a plan that has been presented to the user and is awaiting confirmation. It is cleared after confirmation or cancellation.
2. MongoDB: Long-Term History¶
MongoDB is used for the archival of completed or inactive chat sessions.
- Database:
ai_enhanced_workflow - Collection:
chat_sessions
Chat Session Document Schema¶
{
"_id": "z9y8-x7w6-v5u4-t3s2", // Same as sessionId
"userId": "c7a8b9d0-e1f2-g3h4-i5j6-k7l8m9n0o1p2",
"createdAt": "2025-09-17T10:00:00Z",
"updatedAt": "2025-09-17T10:05:00Z",
"title": "User Group Management", // Optional: A generated title for the conversation
"messages": [
{
"speaker": "user",
"content": "add jdoe to auditors",
"timestamp": "2025-09-17T10:00:15Z"
},
{
"speaker": "ai",
"content": "This will add user 'jdoe' to the 'auditors' group.",
"type": "plan_confirmation_required",
"planId": "a1b2-c3d4-e5f6-g7h8",
"timestamp": "2025-09-17T10:00:20Z"
},
{
"speaker": "user",
"content": "confirm",
"isConfirmation": true,
"timestamp": "2025-09-17T10:00:25Z"
},
{
"speaker": "ai",
"content": "Success: User 'jdoe' has been added to the 'auditors' group.",
"type": "final_answer",
"timestamp": "2025-09-17T10:00:30Z"
}
]
}