hxf/backend/th_agenter/models/conversation.py

39 lines
1.7 KiB
Python
Raw Normal View History

2025-12-04 14:48:38 +08:00
"""Conversation model."""
2025-12-16 13:55:16 +08:00
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import String, Integer, Text, Boolean, DateTime
from datetime import datetime
2025-12-04 14:48:38 +08:00
from ..db.base import BaseModel
class Conversation(BaseModel):
"""Conversation model."""
__tablename__ = "conversations"
2025-12-16 13:55:16 +08:00
title: Mapped[str] = mapped_column(String(200), nullable=False)
user_id: Mapped[int] = mapped_column(Integer, nullable=False) # Removed ForeignKey("users.id")
knowledge_base_id: Mapped[int | None] = mapped_column(Integer, nullable=True) # Removed ForeignKey("knowledge_bases.id")
system_prompt: Mapped[str | None] = mapped_column(Text, nullable=True)
model_name: Mapped[str] = mapped_column(String(100), nullable=False, default="gpt-3.5-turbo")
temperature: Mapped[str] = mapped_column(String(10), nullable=False, default="0.7")
max_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=2048)
is_archived: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
message_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
last_message_at: Mapped[datetime | None] = mapped_column(nullable=True)
2025-12-04 14:48:38 +08:00
# Relationships removed to eliminate foreign key constraints
def __repr__(self):
2025-12-16 13:55:16 +08:00
return f"<Conversation(id={self.id}, title='{self.title}', user_id={self.user_id})>"
2025-12-04 14:48:38 +08:00
@property
def message_count(self):
"""Get the number of messages in this conversation."""
return len(self.messages)
@property
def last_message_at(self):
"""Get the timestamp of the last message."""
2025-12-16 13:55:16 +08:00
return self.messages[-1].created_at or self.created_at