77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
"""Script Builder session model.
|
|
|
|
Tracks AI-powered script generation conversations.
|
|
"""
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import Optional, Any, TYPE_CHECKING
|
|
|
|
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
|
|
|
from app.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.user import User
|
|
|
|
|
|
class ScriptBuilderSession(Base):
|
|
"""A conversation session in the AI Script Builder."""
|
|
__tablename__ = "script_builder_sessions"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
team_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("teams.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
language: Mapped[str] = mapped_column(
|
|
String(30), nullable=False, default="powershell",
|
|
comment="Script language: powershell, bash, python",
|
|
)
|
|
title: Mapped[Optional[str]] = mapped_column(
|
|
String(200), nullable=True,
|
|
comment="Auto-generated from first AI response",
|
|
)
|
|
messages: Mapped[list[dict[str, Any]]] = mapped_column(
|
|
JSONB, nullable=False, default=list,
|
|
comment="Array of {role, content, script?, script_filename?, timestamp}",
|
|
)
|
|
latest_script: Mapped[Optional[str]] = mapped_column(
|
|
Text, nullable=True,
|
|
comment="Most recent generated script for quick access",
|
|
)
|
|
latest_script_filename: Mapped[Optional[str]] = mapped_column(
|
|
String(200), nullable=True,
|
|
comment="Filename of the latest generated script",
|
|
)
|
|
message_count: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=0,
|
|
)
|
|
ai_session_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("ai_sessions.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
comment="Link to FlowPilot session if launched from there",
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(timezone.utc),
|
|
onupdate=lambda: datetime.now(timezone.utc),
|
|
)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship("User")
|