Files
agent_delta/backend/app/db/models/user_slash_command.py
T

53 lines
1.9 KiB
Python

"""User-scoped slash command overrides + custom prompts (SQLModel + PostgreSQL).
Each row is either:
- A user-defined custom command (``prompt`` is set) — fires as
``send-as-message`` in chat, replacing ``/<name>`` with the stored prompt.
- An override entry for a built-in command (``prompt`` is NULL) — exists
only to record ``is_enabled=False`` for that built-in's name.
The unique ``(user_id, name)`` constraint prevents a user from shadowing a
built-in's name with a custom command of the same name.
"""
import uuid
from typing import TYPE_CHECKING
from sqlalchemy import Boolean, Column, ForeignKey, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlmodel import Field, Relationship, SQLModel
from app.db.base import TimestampMixin
if TYPE_CHECKING:
from app.db.models.user import User
class UserSlashCommand(TimestampMixin, SQLModel, table=True):
"""A custom or overridden slash command, scoped to one user."""
__tablename__ = "user_slash_commands"
__table_args__ = (UniqueConstraint("user_id", "name", name="uq_user_slash_commands_user_name"),)
id: uuid.UUID = Field(
default_factory=uuid.uuid4,
sa_column=Column(PG_UUID(as_uuid=True), primary_key=True),
)
user_id: uuid.UUID = Field(
sa_column=Column(
PG_UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
index=True,
),
)
name: str = Field(max_length=64)
prompt: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
is_enabled: bool = Field(default=True, sa_column=Column(Boolean, nullable=False, default=True))
user: "User" = Relationship(sa_relationship_kwargs={"lazy": "joined"})
def __repr__(self) -> str:
kind = "custom" if self.prompt is not None else "builtin-override"
return f"<UserSlashCommand({kind} name={self.name} enabled={self.is_enabled})>"