Backs the schema added in 210d310 with SQLAlchemy 2.0 models.
- SessionFact: "What we know" facts with polymorphic source_ref pointing
at task-lane item UUIDs inside ai_sessions.pending_task_lane (not a FK
per Section 4.2).
- SessionSuggestedFix: AI-proposed resolutions with supersession tracking
and the full user_decision state machine.
- DraftTemplate: post-resolve templatization queue with promotion to
script_templates.
- AccountSettings: per-account JSONB preferences grab-bag with async
classmethod helpers — get_setting(db, account_id, key, default) reads
without creating, set_setting(db, account_id, key, value) upserts via
Postgres ON CONFLICT + jsonb `||` merge so existing keys are preserved.
Lazy row creation matches the Phase 1 design.
Column additions on existing models to mirror the migration:
- AISession: resolution_note_* / escalation_package_* / state_version
(the preview-cache-invalidation counter consumed by Phase 3).
- ScriptTemplate: source_session_id / source_user_id / source_ticket_ref
(provenance for templates promoted from DraftTemplate).
All four new models registered in app.models.__init__ and __all__.
TYPE_CHECKING-guarded relationship imports throughout, matching the
repo's existing model style.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
92 lines
3.3 KiB
Python
92 lines
3.3 KiB
Python
"""Draft template model — scripts generated during a session, pending templatization.
|
|
|
|
Created when an engineer picks "Run now, templatize after resolve" in the
|
|
three-option dialog. Post-resolve, the TemplatizePrompt component reads pending
|
|
drafts and lets the engineer accept (promotes to `script_templates`) or reject.
|
|
"""
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import Any, TYPE_CHECKING
|
|
|
|
from sqlalchemy import (
|
|
Text, DateTime, ForeignKey, String, CheckConstraint,
|
|
)
|
|
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.account import Account
|
|
from app.models.ai_session import AISession
|
|
from app.models.user import User
|
|
from app.models.script_template import ScriptCategory, ScriptTemplate
|
|
|
|
|
|
class DraftTemplate(Base):
|
|
"""A session-generated script pending conversion to a reusable template."""
|
|
__tablename__ = "draft_templates"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"status IN ('pending', 'accepted', 'rejected')",
|
|
name="ck_draft_templates_status",
|
|
),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
account_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("accounts.id"),
|
|
nullable=False,
|
|
)
|
|
source_session_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("ai_sessions.id"),
|
|
nullable=False,
|
|
)
|
|
source_user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("users.id"),
|
|
nullable=False,
|
|
)
|
|
script_body: Mapped[str] = mapped_column(Text, nullable=False)
|
|
proposed_parameters: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False
|
|
)
|
|
proposed_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
|
|
proposed_category_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("script_categories.id"),
|
|
nullable=True,
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default="pending"
|
|
)
|
|
resolved_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
# Set when status transitions to 'accepted' and the draft is promoted
|
|
# to a real script_templates row.
|
|
promoted_template_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("script_templates.id"),
|
|
nullable=True,
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
|
)
|
|
|
|
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
|
source_session: Mapped["AISession"] = relationship(
|
|
"AISession", foreign_keys=[source_session_id]
|
|
)
|
|
source_user: Mapped["User"] = relationship("User", foreign_keys=[source_user_id])
|
|
proposed_category: Mapped["ScriptCategory | None"] = relationship(
|
|
"ScriptCategory", foreign_keys=[proposed_category_id]
|
|
)
|
|
promoted_template: Mapped["ScriptTemplate | None"] = relationship(
|
|
"ScriptTemplate", foreign_keys=[promoted_template_id]
|
|
)
|