add user_session
This commit is contained in:
parent
9b4a6a3ad5
commit
b2eba19fb1
3 changed files with 83 additions and 7 deletions
|
|
@ -0,0 +1,45 @@
|
|||
"""add user_session
|
||||
|
||||
Revision ID: 31359fcda8a7
|
||||
Revises: 57f67bce2bec
|
||||
Create Date: 2025-09-19 12:03:40.032535
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '31359fcda8a7'
|
||||
down_revision: Union[str, Sequence[str], None] = '57f67bce2bec'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('user_session',
|
||||
sa.Column('id', sa.BigInteger(), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=True),
|
||||
sa.Column('session_id', sa.String(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
with op.batch_alter_table('user_session', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_user_session_id'), ['id'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_user_session_user_id'), ['user_id'], unique=False)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('user_session', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_user_session_user_id'))
|
||||
batch_op.drop_index(batch_op.f('ix_user_session_id'))
|
||||
|
||||
op.drop_table('user_session')
|
||||
# ### end Alembic commands ###
|
||||
|
|
@ -34,5 +34,13 @@ class Webpage(Base):
|
|||
description = Column(String)
|
||||
|
||||
|
||||
class UserSession(Base):
|
||||
__tablename__ = "user_session"
|
||||
|
||||
id = Column(BigInteger, primary_key=True, index=True)
|
||||
user_id = Column(String, index=True)
|
||||
session_id = Column(String)
|
||||
|
||||
|
||||
engine = create_engine(settings.DATABASE_URL)
|
||||
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from src.models import ApplicantParam, ChatHook, PlansParam, InsuranceChatContex
|
|||
from .session_service import session_service
|
||||
from ..api.v1.models import Source, HistoryItem
|
||||
from ..config import settings
|
||||
from ..database import Session, UserSession
|
||||
|
||||
|
||||
class ChatService:
|
||||
|
|
@ -135,14 +136,36 @@ class ChatService:
|
|||
# For now, return empty list - this would be populated when RAG is implemented
|
||||
return []
|
||||
|
||||
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[int] = None, current_page: Optional[str] = None, application: Optional[dict] = None) -> Dict[str, Any]:
|
||||
|
||||
async def get_user_session(self, uid: str) -> str | None:
|
||||
with Session() as session:
|
||||
session = session.query(UserSession).filter(UserSession.user_id == uid).first()
|
||||
if not session:
|
||||
return None
|
||||
return session.session_id
|
||||
|
||||
async def create_user_session(self, uid: str, session_id: str):
|
||||
with Session() as session:
|
||||
user_session = UserSession(
|
||||
user_id=uid,
|
||||
session_id=session_id,
|
||||
)
|
||||
session.add(user_session)
|
||||
session.commit()
|
||||
|
||||
|
||||
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[str] = None, current_page: Optional[str] = None, application: Optional[dict] = None) -> Dict[str, Any]:
|
||||
"""Process an insurance chat request"""
|
||||
try:
|
||||
if not session_id:
|
||||
if not session_id or not await session_service.validate_session(session_id):
|
||||
if uid:
|
||||
session_id = await self.get_user_session(uid)
|
||||
if not session_id or not await session_service.validate_session(session_id):
|
||||
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
|
||||
await self.create_user_session(uid, session_id)
|
||||
else:
|
||||
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
|
||||
|
||||
elif not await session_service.validate_session(session_id):
|
||||
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
|
||||
|
||||
instructions = ""
|
||||
if uid:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue