Spaces:
Runtime error
Runtime error
Download src/db/models.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 32.4 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/db/models.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/src/db/models.py
-
curl -L -o models.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/db/models.py
32.4 kB
| """SQLAlchemy ORM models.""" | |
| from datetime import datetime | |
| from uuid import uuid4 | |
| from sqlalchemy import ( | |
| JSON, | |
| Boolean, | |
| Column, | |
| DateTime, | |
| Float, | |
| ForeignKey, | |
| Index, | |
| Integer, | |
| String, | |
| Text, | |
| UniqueConstraint, | |
| ) | |
| from sqlalchemy.orm import relationship | |
| from .database import Base | |
| def get_uuid_column(): | |
| """Restituisce colonna UUID compatibile SQLite/PostgreSQL.""" | |
| # Per ora usiamo String per SQLite, in futuro PGUUID per Postgres | |
| return Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| class WorkspaceModel(Base): | |
| """Logical multi-tenant workspace.""" | |
| __tablename__ = "workspaces" | |
| __table_args__ = ( | |
| UniqueConstraint("name", name="uq_workspaces_name"), | |
| Index("idx_workspaces_created_at", "created_at"), | |
| ) | |
| id = get_uuid_column() | |
| name = Column(String(255), nullable=False) | |
| description = Column(Text, nullable=True) | |
| is_active = Column(Boolean, default=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class WorkspaceMemberModel(Base): | |
| """Workspace user membership with role.""" | |
| __tablename__ = "workspace_members" | |
| __table_args__ = ( | |
| UniqueConstraint("workspace_id", "user_id", name="uq_workspace_member_unique"), | |
| Index("idx_workspace_members_workspace_role", "workspace_id", "role"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| user_id = Column(String(255), nullable=False) | |
| role = Column(String(64), nullable=False) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| class WorkspaceSettingsModel(Base): | |
| """Workspace-level runtime and policy settings.""" | |
| __tablename__ = "workspace_settings" | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), primary_key=True) | |
| llm_policy = Column(JSON, default=dict) | |
| feature_flags = Column(JSON, default=dict) | |
| runtime_limits = Column(JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| class LLMKeyCentralModel(Base): | |
| """Centralized encrypted LLM keys (platform-managed).""" | |
| __tablename__ = "llm_keys_central" | |
| __table_args__ = ( | |
| UniqueConstraint("provider", "key_alias", name="uq_llm_key_provider_alias"), | |
| Index("idx_llm_keys_provider_active", "provider", "is_active"), | |
| ) | |
| id = get_uuid_column() | |
| key_alias = Column(String(128), nullable=False) | |
| provider = Column(String(64), nullable=False) | |
| encrypted_secret = Column(Text, nullable=False) | |
| key_version = Column(Integer, default=1) | |
| is_active = Column(Boolean, default=True) | |
| last_test_status = Column(String(32), nullable=True) | |
| last_tested_at = Column(DateTime, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class UserIntegrationTokenModel(Base): | |
| """Per-user encrypted access tokens for external integrations (e.g. OpenProject). | |
| A user can have at most one token per provider (enforced by the unique | |
| constraint). Plaintext is never stored — the token is encrypted at rest | |
| via :func:`src.security.secrets.encrypt_secret`. | |
| """ | |
| __tablename__ = "user_integration_tokens" | |
| __table_args__ = ( | |
| UniqueConstraint("user_id", "provider", name="uq_user_integration_provider"), | |
| Index("idx_user_integrations_user", "user_id"), | |
| Index("idx_user_integrations_provider", "provider"), | |
| ) | |
| id = get_uuid_column() | |
| user_id = Column(String(255), nullable=False) | |
| provider = Column(String(64), nullable=False) | |
| encrypted_token = Column(Text, nullable=False) | |
| last_test_status = Column(String(32), nullable=True) | |
| last_tested_at = Column(DateTime, nullable=True) | |
| last_test_error = Column(Text, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| class AuditEventModel(Base): | |
| """Audit trail for sensitive changes.""" | |
| __tablename__ = "audit_events" | |
| __table_args__ = ( | |
| Index("idx_audit_events_workspace_created_at", "workspace_id", "created_at"), | |
| Index("idx_audit_events_resource", "resource_type", "resource_id"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| user_id = Column(String(255), nullable=True) | |
| event_type = Column(String(128), nullable=False) | |
| resource_type = Column(String(64), nullable=False) | |
| resource_id = Column(String(128), nullable=True) | |
| payload = Column(JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class BotModel(Base): | |
| """Modello SQLAlchemy per Bot.""" | |
| __tablename__ = "bots" | |
| __table_args__ = (Index("idx_bots_workspace_created_at", "workspace_id", "created_at"),) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| name = Column(String(255), nullable=False) | |
| description = Column(Text, nullable=True) | |
| url = Column(String(500), nullable=False) | |
| # Configurazioni JSON | |
| selectors = Column(JSON, default=dict) | |
| expected_tone = Column(JSON, default=dict) | |
| rag_config = Column(JSON, default=dict) | |
| is_active = Column(Boolean, default=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| tags = Column(JSON, default=list) | |
| auto_click_new_chat = Column(Boolean, default=False) | |
| knowledge_boundary_url = Column(String(500), nullable=True) | |
| knowledge_boundary_content = Column(Text, nullable=True) | |
| knowledge_boundary_updated_at = Column(DateTime, nullable=True) | |
| deterministic_fallbacks = Column(JSON, default=list) | |
| # Relazioni | |
| rag_documents = relationship( | |
| "RAGDocumentModel", back_populates="bot", cascade="all, delete-orphan" | |
| ) | |
| versions = relationship("BotVersionModel", back_populates="bot", cascade="all, delete-orphan") | |
| class RAGDocumentModel(Base): | |
| """Modello SQLAlchemy per RAG Document.""" | |
| __tablename__ = "rag_documents" | |
| __table_args__ = ( | |
| Index("idx_rag_documents_workspace_created_at", "workspace_id", "created_at"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| bot_id = Column(String(36), ForeignKey("bots.id"), nullable=False) | |
| filename = Column(String(255), nullable=False) | |
| content_hash = Column(String(64), nullable=False) | |
| file_size = Column(Integer, nullable=False) | |
| mime_type = Column(String(100), default="text/plain") | |
| storage_path = Column(String(500), nullable=False) | |
| extracted_text = Column(Text, nullable=True) | |
| description = Column(Text, nullable=True) | |
| doc_metadata = Column("metadata", JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| # Relazioni | |
| bot = relationship("BotModel", back_populates="rag_documents") | |
| class BotVersionModel(Base): | |
| """Modello SQLAlchemy per Bot Version.""" | |
| __tablename__ = "bot_versions" | |
| __table_args__ = (Index("idx_bot_versions_workspace_created_at", "workspace_id", "created_at"),) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| bot_id = Column(String(36), ForeignKey("bots.id"), nullable=False) | |
| version = Column(String(50), nullable=False) | |
| release_notes = Column(Text, nullable=True) | |
| config_snapshot = Column(JSON, default=dict) | |
| rag_documents_snapshot = Column(JSON, default=list) | |
| is_tested = Column(Boolean, default=False) | |
| is_approved = Column(Boolean, default=False) | |
| approved_at = Column(DateTime, nullable=True) | |
| approved_by = Column(String(255), nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| # Relazioni | |
| bot = relationship("BotModel", back_populates="versions") | |
| class PersonaModel(Base): | |
| """Modello SQLAlchemy per Persona.""" | |
| __tablename__ = "personas" | |
| __table_args__ = (Index("idx_personas_workspace_created_at", "workspace_id", "created_at"),) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| name = Column(String(255), nullable=False) | |
| mood = Column(String(50), default="neutral") | |
| goal = Column(Text, nullable=False) | |
| background = Column(Text, nullable=True) | |
| constraints = Column(JSON, default=list) | |
| language = Column(String(50), default="italian") | |
| communication_style = Column(String(50), default="normal") | |
| typing_speed = Column(String(50), default="normal") | |
| tech_proficiency = Column(String(50), default="average") | |
| patience_level = Column(Integer, default=5) | |
| # Tone of Voice fields | |
| tone_of_voice = Column(String(50), default="neutral") | |
| vocabulary_type = Column(String(50), default="standard") | |
| sentence_structure = Column(String(50), default="mixed") | |
| emotional_expression = Column(String(50), default="moderate") | |
| response_pacing = Column(String(50), default="balanced") | |
| is_template = Column(Boolean, default=False) | |
| tags = Column(JSON, default=list) | |
| created_at = Column(DateTime, default=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class ScenarioModel(Base): | |
| """Modello SQLAlchemy per Scenario.""" | |
| __tablename__ = "scenarios" | |
| __table_args__ = (Index("idx_scenarios_workspace_created_at", "workspace_id", "created_at"),) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| bot_id = Column(String(36), ForeignKey("bots.id"), nullable=False) | |
| name = Column(String(255), nullable=False) | |
| description = Column(Text, nullable=True) | |
| steps = Column(JSON, nullable=False) # Lista di step | |
| success_criteria = Column(JSON, default=dict) | |
| is_template = Column(Boolean, default=False) | |
| tags = Column(JSON, default=list) | |
| category = Column(String(100), nullable=True) | |
| priority = Column(Integer, default=1) | |
| mode = Column(String(32), default="deterministic") | |
| followup_policy = Column(String(32), default="limited") | |
| followup_max_turns = Column(Integer, default=1) | |
| seed_questions = Column(JSON, default=list) | |
| supports_context = Column(Boolean, default=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| # Relazioni | |
| bot = relationship("BotModel") | |
| class SubScenarioModel(Base): | |
| """Subscenario with optional overrides.""" | |
| __tablename__ = "subscenarios" | |
| __table_args__ = ( | |
| Index( | |
| "idx_subscenarios_workspace_scenario_priority", | |
| "workspace_id", | |
| "scenario_id", | |
| "priority", | |
| ), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| scenario_id = Column(String(36), ForeignKey("scenarios.id"), nullable=False) | |
| name = Column(String(255), nullable=False) | |
| goal = Column(Text, nullable=False) | |
| priority = Column(Integer, default=1) | |
| mode = Column(String(32), nullable=True) | |
| followup_policy = Column(String(32), nullable=True) | |
| followup_max_turns = Column(Integer, nullable=True) | |
| seed_questions = Column(JSON, default=list) | |
| success_criteria = Column(JSON, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| scenario = relationship("ScenarioModel") | |
| class TestSessionModel(Base): | |
| """Modello SQLAlchemy per Test Session.""" | |
| __tablename__ = "test_sessions" | |
| __table_args__ = ( | |
| Index("idx_test_sessions_workspace_created_at", "workspace_id", "created_at"), | |
| Index("idx_test_sessions_workspace_status", "workspace_id", "status"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| bot_id = Column(String(36), ForeignKey("bots.id"), nullable=False) | |
| bot_version = Column(String(50), nullable=False) | |
| session_name = Column(String(255), nullable=True) | |
| description = Column(Text, nullable=True) | |
| config = Column(JSON, default=dict) | |
| status = Column(String(50), default="pending") | |
| started_at = Column(DateTime, nullable=True) | |
| completed_at = Column(DateTime, nullable=True) | |
| total_tests = Column(Integer, default=0) | |
| passed = Column(Integer, default=0) | |
| failed = Column(Integer, default=0) | |
| skipped = Column(Integer, default=0) | |
| avg_response_time_ms = Column(Integer, nullable=True) | |
| rag_faithfulness_avg = Column(Float, nullable=True) | |
| tone_match_avg = Column(Float, nullable=True) | |
| error_message = Column(Text, nullable=True) | |
| # Session verdict (Wilson 95% CI on per-turn AIS correctness). | |
| # verdict_label: passed | failed | inconclusive | |
| verdict_label = Column(String(24), nullable=True) | |
| verdict_p_hat = Column(Float, nullable=True) | |
| verdict_lower_bound = Column(Float, nullable=True) | |
| verdict_upper_bound = Column(Float, nullable=True) | |
| verdict_threshold = Column(Float, nullable=True) | |
| verdict_n = Column(Integer, nullable=True) | |
| # Post-session AIS judge orchestration. judge_status uno tra: | |
| # idle | pending | running | done | failed. | |
| judge_status = Column(String(24), default="idle", nullable=True) | |
| judge_started_at = Column(DateTime, nullable=True) | |
| judge_completed_at = Column(DateTime, nullable=True) | |
| judge_model = Column(String(128), nullable=True) | |
| judge_error = Column(Text, nullable=True) | |
| judge_progress = Column(JSON, nullable=True) | |
| # Limite messaggi per sessione | |
| max_messages = Column(Integer, nullable=True) | |
| max_turns = Column(Integer, nullable=True) | |
| min_turns = Column(Integer, default=1) | |
| critical_questions = Column(JSON, default=list) | |
| termination_policy = Column(JSON, default=dict) | |
| messages_sent = Column(Integer, default=0) | |
| knowledge_snapshot_id = Column(String(36), nullable=True) | |
| memory_status = Column(JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| # Relazioni | |
| results = relationship( | |
| "TestResultModel", back_populates="session", cascade="all, delete-orphan" | |
| ) | |
| class TestResultModel(Base): | |
| """Modello SQLAlchemy per Test Result.""" | |
| __tablename__ = "test_results" | |
| __table_args__ = (Index("idx_test_results_workspace_created_at", "workspace_id", "created_at"),) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) | |
| session_id = Column(String(36), ForeignKey("test_sessions.id"), nullable=False) | |
| scenario_id = Column(String(36), ForeignKey("scenarios.id"), nullable=False) | |
| persona_id = Column(String(36), ForeignKey("personas.id"), nullable=False) | |
| steps_results = Column(JSON, default=list) | |
| status = Column(String(50), nullable=False) | |
| total_steps = Column(Integer, default=0) | |
| passed_steps = Column(Integer, default=0) | |
| failed_steps = Column(Integer, default=0) | |
| total_duration_ms = Column(Integer, nullable=True) | |
| avg_response_time_ms = Column(Integer, nullable=True) | |
| rag_faithfulness_avg = Column(Float, nullable=True) | |
| tone_match_avg = Column(Float, nullable=True) | |
| error_message = Column(Text, nullable=True) | |
| failure_reason = Column(Text, nullable=True) | |
| screenshot_urls = Column(JSON, default=list) | |
| conversation_log = Column(Text, nullable=True) | |
| scenario_results = Column(JSON, nullable=True) # Tracking per multi-scenario tests | |
| termination_reason = Column(String(128), nullable=True) | |
| started_at = Column(DateTime, nullable=True) | |
| completed_at = Column(DateTime, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| # Relazioni | |
| session = relationship("TestSessionModel", back_populates="results") | |
| class SessionScenarioLinkModel(Base): | |
| """Modello per link molti-a-molti sessione-scenario-persona.""" | |
| __tablename__ = "session_scenario_links" | |
| session_id = Column(String(36), ForeignKey("test_sessions.id"), primary_key=True) | |
| scenario_id = Column(String(36), ForeignKey("scenarios.id"), primary_key=True) | |
| persona_id = Column(String(36), ForeignKey("personas.id"), primary_key=True) | |
| priority = Column(Integer, default=1) | |
| config_override = Column(JSON, default=dict) | |
| class KnowledgeSourceModel(Base): | |
| """Knowledge source registry.""" | |
| __tablename__ = "knowledge_sources" | |
| __table_args__ = ( | |
| Index("idx_knowledge_sources_workspace_created_at", "workspace_id", "created_at"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| name = Column(String(255), nullable=False) | |
| source_type = Column(String(32), nullable=False) | |
| description = Column(Text, nullable=True) | |
| source_metadata = Column("metadata", JSON, default=dict) | |
| is_active = Column(Boolean, default=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class KnowledgeSourceVersionModel(Base): | |
| """Versioned source content.""" | |
| __tablename__ = "knowledge_source_versions" | |
| __table_args__ = ( | |
| Index("idx_knowledge_source_versions_workspace_state", "workspace_id", "state"), | |
| ) | |
| id = get_uuid_column() | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=False) | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| version_label = Column(String(64), nullable=False) | |
| state = Column(String(32), default="draft") | |
| content_hash = Column(String(128), nullable=True) | |
| storage_ref = Column(String(500), nullable=True) | |
| parsed_payload = Column(JSON, default=dict) | |
| validation_report = Column(JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class KnowledgeSourceAssignmentModel(Base): | |
| """Scenario/subscenario binding to a source version.""" | |
| __tablename__ = "knowledge_source_assignments" | |
| __table_args__ = ( | |
| Index( | |
| "idx_knowledge_assignments_workspace_scenario", | |
| "workspace_id", | |
| "scenario_id", | |
| "subscenario_id", | |
| ), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=False) | |
| source_version_id = Column( | |
| String(36), ForeignKey("knowledge_source_versions.id"), nullable=False | |
| ) | |
| scenario_id = Column(String(36), ForeignKey("scenarios.id"), nullable=False) | |
| subscenario_id = Column(String(36), ForeignKey("subscenarios.id"), nullable=True) | |
| required = Column(Boolean, default=True) | |
| weight = Column(Float, default=1.0) | |
| effective_from = Column(DateTime, nullable=True) | |
| effective_to = Column(DateTime, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class KnowledgeImportJobModel(Base): | |
| """Knowledge import and validation jobs.""" | |
| __tablename__ = "knowledge_import_jobs" | |
| __table_args__ = ( | |
| Index("idx_knowledge_jobs_workspace_created_at", "workspace_id", "created_at"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=True) | |
| filename = Column(String(255), nullable=True) | |
| source_type = Column(String(32), nullable=False, default="csv") | |
| status = Column(String(32), default="pending") | |
| row_count = Column(Integer, default=0) | |
| valid_count = Column(Integer, default=0) | |
| invalid_count = Column(Integer, default=0) | |
| validation_report = Column(JSON, default=dict) | |
| created_at = Column(DateTime, default=datetime.now) | |
| completed_at = Column(DateTime, nullable=True) | |
| created_by = Column(String(255), nullable=True) | |
| class KnowledgeCrawlRunModel(Base): | |
| """Crawl/index run status for a source version.""" | |
| __tablename__ = "knowledge_crawl_runs" | |
| __table_args__ = ( | |
| Index( | |
| "idx_knowledge_crawl_runs_workspace_source_version", | |
| "workspace_id", | |
| "source_id", | |
| "source_version_id", | |
| ), | |
| Index("idx_knowledge_crawl_runs_status_created_at", "status", "created_at"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=False) | |
| source_version_id = Column( | |
| String(36), ForeignKey("knowledge_source_versions.id"), nullable=False | |
| ) | |
| trigger = Column(String(64), nullable=False, default="commit") | |
| status = Column(String(64), nullable=False, default="queued") | |
| phase = Column(String(64), nullable=False, default="http_fetch") | |
| totals = Column(JSON, default=dict) | |
| started_at = Column(DateTime, nullable=True) | |
| completed_at = Column(DateTime, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| created_by = Column(String(255), nullable=True) | |
| class KnowledgePageModel(Base): | |
| """Fetched canonical pages for source versions.""" | |
| __tablename__ = "knowledge_pages" | |
| __table_args__ = ( | |
| Index( | |
| "idx_knowledge_pages_workspace_source_version", | |
| "workspace_id", | |
| "source_id", | |
| "source_version_id", | |
| ), | |
| UniqueConstraint( | |
| "source_version_id", | |
| "url_canonical", | |
| name="uq_knowledge_pages_version_url", | |
| ), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=False) | |
| source_version_id = Column( | |
| String(36), ForeignKey("knowledge_source_versions.id"), nullable=False | |
| ) | |
| url = Column(String(2000), nullable=False) | |
| url_canonical = Column(String(2000), nullable=False) | |
| title = Column(String(500), default="") | |
| content_type = Column(String(255), default="") | |
| http_status = Column(Integer, nullable=True) | |
| fetch_status = Column(String(64), default="unknown") | |
| fetch_method = Column(String(64), default="http") | |
| content_hash = Column(String(128), nullable=True) | |
| raw_chars = Column(Integer, default=0) | |
| cleaned_text = Column(Text, default="") | |
| fetched_at = Column(DateTime, nullable=True) | |
| error = Column(Text, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| class KnowledgeChunkModel(Base): | |
| """Chunked searchable memory from pages.""" | |
| __tablename__ = "knowledge_chunks" | |
| __table_args__ = ( | |
| Index( | |
| "idx_knowledge_chunks_workspace_source_version", | |
| "workspace_id", | |
| "source_id", | |
| "source_version_id", | |
| ), | |
| Index("idx_knowledge_chunks_page_index", "page_id", "chunk_index"), | |
| UniqueConstraint("page_id", "chunk_hash", name="uq_knowledge_chunks_page_hash"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| page_id = Column(String(36), ForeignKey("knowledge_pages.id"), nullable=False) | |
| source_id = Column(String(36), ForeignKey("knowledge_sources.id"), nullable=False) | |
| source_version_id = Column( | |
| String(36), ForeignKey("knowledge_source_versions.id"), nullable=False | |
| ) | |
| chunk_index = Column(Integer, default=0) | |
| chunk_text = Column(Text, nullable=False) | |
| token_estimate = Column(Integer, default=0) | |
| chunk_hash = Column(String(128), nullable=False) | |
| embedding_json = Column(JSON, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class SessionKnowledgeSnapshotModel(Base): | |
| """Frozen memory scope associated to a test session.""" | |
| __tablename__ = "session_knowledge_snapshots" | |
| __table_args__ = ( | |
| Index("idx_session_knowledge_snapshots_workspace_bot", "workspace_id", "bot_id"), | |
| Index("idx_session_knowledge_snapshots_session", "session_id"), | |
| ) | |
| id = get_uuid_column() | |
| session_id = Column(String(36), ForeignKey("test_sessions.id"), nullable=False) | |
| workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) | |
| bot_id = Column(String(36), ForeignKey("bots.id"), nullable=False) | |
| source_version_ids = Column(JSON, default=list) | |
| crawl_run_ids = Column(JSON, default=list) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class EvaluationModel(Base): | |
| """Structured per-dimension evaluation scores (FR7/FR8).""" | |
| __tablename__ = "evaluations" | |
| __table_args__ = (Index("idx_evaluations_result_dimension", "result_id", "dimension"),) | |
| id = get_uuid_column() | |
| result_id = Column( | |
| String(36), ForeignKey("test_results.id", ondelete="CASCADE"), nullable=False | |
| ) | |
| dimension = Column(String(32), nullable=False) | |
| score = Column(Float, nullable=False) | |
| confidence = Column(Float, nullable=True) | |
| details = Column(JSON, default=dict) | |
| human_override_score = Column(Float, nullable=True) | |
| human_reviewer_id = Column(String(255), nullable=True) | |
| reviewed_at = Column(DateTime, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class InteractionModel(Base): | |
| """Per-message interaction log for a test result (FR7).""" | |
| __tablename__ = "interactions" | |
| __table_args__ = (Index("idx_interactions_result_turn", "result_id", "turn_number"),) | |
| id = get_uuid_column() | |
| result_id = Column( | |
| String(36), ForeignKey("test_results.id", ondelete="CASCADE"), nullable=False | |
| ) | |
| turn_number = Column(Integer, nullable=False) | |
| sender = Column(String(16), nullable=False) | |
| message = Column(Text, nullable=False) | |
| response_time_ms = Column(Integer, nullable=True) | |
| rag_faithfulness = Column(Float, nullable=True) | |
| tone_match = Column(Float, nullable=True) | |
| screenshot_url = Column(String(500), nullable=True) | |
| # AIS (Attributable to Identified Sources) per-turn correctness. | |
| # Values: attributable | partial | not_attributable | |
| correctness_label = Column(String(24), nullable=True) | |
| correctness_reasoning = Column(Text, nullable=True) | |
| claims_json = Column(JSON, nullable=True) | |
| evidence_pack_json = Column(JSON, nullable=True) | |
| # Human override of the engine verdict for this turn. | |
| human_override_label = Column(String(24), nullable=True) | |
| # Override separato per intent satisfaction (correttezza AIS è human_override_label). | |
| # Valori attesi: satisfied | partial | not_satisfied | not_applicable. | |
| human_override_intent_label = Column(String(24), nullable=True) | |
| human_override_reason = Column(Text, nullable=True) | |
| human_override_reviewer_id = Column(String(255), nullable=True) | |
| human_override_at = Column(DateTime, nullable=True) | |
| retry_count = Column(Integer, nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class ReportModel(Base): | |
| """Persistent report metadata and inline content (FR8/FR9).""" | |
| __tablename__ = "reports" | |
| __table_args__ = ( | |
| Index("idx_reports_session", "session_id"), | |
| Index("idx_reports_result", "result_id"), | |
| ) | |
| id = get_uuid_column() | |
| session_id = Column( | |
| String(36), ForeignKey("test_sessions.id", ondelete="CASCADE"), nullable=True | |
| ) | |
| result_id = Column(String(36), ForeignKey("test_results.id", ondelete="CASCADE"), nullable=True) | |
| report_type = Column(String(32), nullable=False) | |
| format = Column(String(16), nullable=False) | |
| storage_path = Column(String(500), nullable=True) | |
| content = Column(JSON, default=dict) | |
| generated_by = Column(String(255), nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| class BugTicketModel(Base): | |
| """Bug tickets linked to test results (FR10).""" | |
| __tablename__ = "bug_tickets" | |
| __table_args__ = ( | |
| Index("idx_bug_tickets_workspace", "workspace_id"), | |
| Index("idx_bug_tickets_result", "result_id"), | |
| Index("idx_bug_tickets_session", "session_id"), | |
| Index("idx_bug_tickets_status", "status"), | |
| Index("idx_bug_tickets_sync_status", "sync_status"), | |
| ) | |
| id = get_uuid_column() | |
| workspace_id = Column( | |
| String(36), ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False | |
| ) | |
| result_id = Column(String(36), ForeignKey("test_results.id", ondelete="CASCADE"), nullable=True) | |
| session_id = Column( | |
| String(36), ForeignKey("test_sessions.id", ondelete="CASCADE"), nullable=True | |
| ) | |
| scenario_id = Column(String(36), nullable=True) | |
| subscenario_id = Column(String(36), nullable=True) | |
| # Optional link to the specific interaction (turn) that triggered the ticket. | |
| turn_id = Column( | |
| String(36), ForeignKey("interactions.id", ondelete="SET NULL"), nullable=True | |
| ) | |
| interaction_turn_number = Column(Integer, nullable=True) | |
| title = Column(String(255), nullable=False) | |
| description = Column(Text, nullable=True) | |
| severity = Column(String(16), nullable=False, default="medium") | |
| status = Column(String(16), nullable=False, default="open") | |
| bot_version_at_creation = Column(String(100), nullable=True) | |
| assigned_to = Column(String(255), nullable=True) | |
| created_by = Column(String(255), nullable=True) | |
| created_at = Column(DateTime, default=datetime.now) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| resolved_at = Column(DateTime, nullable=True) | |
| # User-authored optional fields used to compose the OP description. | |
| expected_behavior = Column(Text, nullable=True) | |
| # External parent (Summary task) work package the bug is filed under. | |
| external_parent_id = Column(String(64), nullable=True) | |
| # OpenProject category id chosen for this bug (project-scoped). | |
| external_category_id = Column(String(64), nullable=True) | |
| # OpenProject sync state — populated by background task after create. | |
| external_id = Column(String(64), nullable=True) | |
| external_url = Column(String(500), nullable=True) | |
| external_project_identifier = Column(String(128), nullable=True) | |
| sync_status = Column(String(16), nullable=True, default="pending") | |
| sync_error = Column(Text, nullable=True) | |
| sync_attempts = Column(Integer, nullable=False, default=0) | |
| synced_at = Column(DateTime, nullable=True) | |
| class PlatformSettingsOverrideModel(Base): | |
| """Override admin a livello piattaforma su env vars. | |
| Backing store dell'endpoint /admin/settings. Sostituisce il vecchio file | |
| `data/system_settings_overrides.json` che su HF Spaces (filesystem | |
| ephemeral) si perdeva ad ogni rebuild del container, causando il bug | |
| "modifico EVALUATOR_MODEL via UI, salvo, refresh → torna al default". | |
| Una riga per chiave: `key` è il nome dell'env (es. EVALUATOR_MODEL), | |
| `value` è il valore stringa (booleani / numeri serializzati come stringa | |
| coerentemente con `_serialize_value` in admin_settings.py). Al boot | |
| dell'app, `_load_overrides()` legge tutte le righe e fa | |
| `os.environ[key] = value`. | |
| """ | |
| __tablename__ = "platform_settings_overrides" | |
| key = Column(String(128), primary_key=True) | |
| value = Column(Text, nullable=False) | |
| updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) | |
| updated_by = Column(String(255), nullable=True) | |