Files
loyal_companion/src/daemon_boyfriend/models/user.py
latte 0d43b5b29a feat: Implement Living AI system
Complete implementation of the Living AI features:

Phase 1 - Foundation:
- MoodService: Valence-arousal mood model with time decay
- RelationshipService: Stranger→Close Friend progression
- Enhanced system prompt with personality modifiers

Phase 2 - Autonomous Learning:
- FactExtractionService: AI-powered fact extraction from conversations
- Rate-limited extraction (configurable, default 30%)
- Deduplication and importance scoring

Phase 3 - Personalization:
- CommunicationStyleService: Learn user preferences
- OpinionService: Bot opinion formation on topics
- SelfAwarenessService: Bot statistics and self-reflection

Phase 4 - Proactive Features:
- ProactiveService: Scheduled events (birthdays, follow-ups)
- Event detection from conversations
- Recurring event support

Phase 5 - Social Features:
- AssociationService: Cross-user memory connections
- Shared interest discovery
- Connection suggestions

New database tables:
- bot_states, bot_opinions, user_relationships
- user_communication_styles, scheduled_events
- fact_associations, mood_history

Configuration:
- Living AI feature toggles
- Individual command enable/disable
- All features work naturally through conversation when commands disabled
2026-01-12 19:51:48 +01:00

96 lines
3.7 KiB
Python

"""User-related database models."""
from datetime import datetime
from typing import TYPE_CHECKING
from sqlalchemy import BigInteger, Boolean, Float, ForeignKey, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .base import Base
if TYPE_CHECKING:
from .conversation import Conversation, Message
from .guild import GuildMember
from .living_ai import ScheduledEvent, UserCommunicationStyle, UserRelationship
class User(Base):
"""Discord user tracking."""
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
discord_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
discord_username: Mapped[str] = mapped_column(String(255))
discord_display_name: Mapped[str | None] = mapped_column(String(255))
custom_name: Mapped[str | None] = mapped_column(String(255))
first_seen_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
last_seen_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
# Relationships
preferences: Mapped[list["UserPreference"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
facts: Mapped[list["UserFact"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
conversations: Mapped[list["Conversation"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
messages: Mapped[list["Message"]] = relationship(back_populates="user")
guild_memberships: Mapped[list["GuildMember"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
# Living AI relationships
relationships: Mapped[list["UserRelationship"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
communication_style: Mapped["UserCommunicationStyle | None"] = relationship(
back_populates="user", cascade="all, delete-orphan", uselist=False
)
scheduled_events: Mapped[list["ScheduledEvent"]] = relationship(
back_populates="user", cascade="all, delete-orphan"
)
@property
def display_name(self) -> str:
"""Get the name to use when addressing this user."""
return self.custom_name or self.discord_display_name or self.discord_username
class UserPreference(Base):
"""Per-user preferences/settings."""
__tablename__ = "user_preferences"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
preference_key: Mapped[str] = mapped_column(String(100))
preference_value: Mapped[str | None] = mapped_column(Text)
# Relationships
user: Mapped["User"] = relationship(back_populates="preferences")
__table_args__ = (UniqueConstraint("user_id", "preference_key"),)
class UserFact(Base):
"""Facts the bot has learned about users."""
__tablename__ = "user_facts"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
fact_type: Mapped[str] = mapped_column(String(50), index=True) # general, preference, hobby
fact_content: Mapped[str] = mapped_column(Text)
confidence: Mapped[float] = mapped_column(Float, default=1.0)
source: Mapped[str] = mapped_column(String(50), default="conversation")
is_active: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
learned_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
last_referenced_at: Mapped[datetime | None] = mapped_column(default=None)
# Relationships
user: Mapped["User"] = relationship(back_populates="facts")