diff --git a/src/agent.py b/src/agent.py index e3ac710..52195c5 100644 --- a/src/agent.py +++ b/src/agent.py @@ -108,6 +108,7 @@ async def get_latest_user_representation( .where(models.App.public_id == app_id) .where(models.User.public_id == user_id) .where(models.Metamessage.metamessage_type == "user_representation") + .order_by(models.Metamessage.id.desc()) # get the most recent .limit(1) ) result = await db.execute(stmt) diff --git a/src/deriver/__main__.py b/src/deriver/__main__.py index df2a178..340ae42 100644 --- a/src/deriver/__main__.py +++ b/src/deriver/__main__.py @@ -1,5 +1,4 @@ import asyncio -import argparse import uvloop @@ -7,4 +6,5 @@ if __name__ == "__main__": asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) - asyncio.run(main()) \ No newline at end of file + asyncio.run(main()) + diff --git a/src/deriver/consumer.py b/src/deriver/consumer.py index 114e101..1ca9ed2 100644 --- a/src/deriver/consumer.py +++ b/src/deriver/consumer.py @@ -151,14 +151,26 @@ async def process_user_message( f"Tom Inference: {tom_inference_metamessage.content}", style="orange1" ) - # Fetch the existing user representation + # Fetch the latest user representation user_representation_stmt = ( select(models.Metamessage) - .where(models.Metamessage.message_id == ai_message.public_id) + .join( + models.Message, + models.Message.public_id == models.Metamessage.message_id, + ) + .join( + models.Session, + models.Message.session_id == models.Session.public_id, + ) + .join(models.User, models.User.public_id == models.Session.user_id) + .join(models.App, models.App.public_id == models.User.app_id) + .where(models.App.public_id == app_id) + .where(models.User.public_id == user_id) .where(models.Metamessage.metamessage_type == "user_representation") - .order_by(models.Metamessage.id.desc()) + .order_by(models.Metamessage.id.desc()) # get the most recent .limit(1) ) + response = await db.execute(user_representation_stmt) existing_representation = response.scalar_one_or_none() diff --git a/src/models.py b/src/models.py index b81115e..8ad7c5c 100644 --- a/src/models.py +++ b/src/models.py @@ -12,7 +12,7 @@ String, UniqueConstraint, ) -from sqlalchemy.dialects.postgresql import JSONB +from sqlalchemy.dialects.postgresql import JSONB, TEXT from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.sql import func