from __future__ import annotations
from typing import Any, Dict, Optional
from sqlalchemy import BigInteger, Boolean, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from openjiuwen_studio.models.db_fun_base import Base, DBFunBase
from openjiuwen_studio.ops.config import settings
class TriggerDB(Base, DBFunBase):
"""
One row per trigger definition.
Type-specific config lives in the JSON `config` column to avoid sparse columns.
"""
__tablename__ = "trigger"
__table_args__ = (
Index("idx_trigger_space_type", "space_id", "trigger_type"),
Index("idx_trigger_target", "target_type", "target_id"),
)
if settings.DB_TYPE.lower() == "sqlite":
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
else:
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
space_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
create_user: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
update_user: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
trigger_id: Mapped[str] = mapped_column(
String(36), nullable=False, unique=True, index=True
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[Optional[str]] = mapped_column(String(2000), nullable=True)
trigger_type: Mapped[str] = mapped_column(String(32), nullable=False)
target_type: Mapped[str] = mapped_column(String(32), nullable=False)
target_id: Mapped[str] = mapped_column(String(64), nullable=False)
target_version: Mapped[str] = mapped_column(String(64), nullable=False, default="draft")
input_payload: Mapped[Optional[Dict[str, Any]]] = mapped_column(JSON, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
config: Mapped[Optional[Dict[str, Any]]] = mapped_column(JSON, nullable=True)
webhook_token: Mapped[Optional[str]] = mapped_column(
String(128), nullable=True, unique=True, index=True
)
scheduler_job_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
create_time: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
update_time: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
class TriggerExecutionLogDB(Base, DBFunBase):
"""
One row per trigger fire attempt. Append-only after the final status write.
Linked to existing trace infrastructure via trace_id.
"""
__tablename__ = "trigger_execution_log"
__table_args__ = (
Index("idx_tel_trigger_time", "trigger_id", "started_at"),
Index("idx_tel_trace", "trace_id"),
Index("idx_tel_space", "space_id"),
)
if settings.DB_TYPE.lower() == "sqlite":
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
else:
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
space_id: Mapped[str] = mapped_column(String(64), nullable=False)
trigger_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
trace_id: Mapped[Optional[str]] = mapped_column(String(36), nullable=True)
conversation_id: Mapped[Optional[str]] = mapped_column(String(36), nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="running")
fired_by: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
trigger_type: Mapped[str] = mapped_column(String(32), nullable=False)
started_at: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
finished_at: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
duration_ms: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
inputs_snapshot: Mapped[Optional[Dict[str, Any]]] = mapped_column(JSON, nullable=True)
outputs: Mapped[Optional[Dict[str, Any]]] = mapped_column(JSON, nullable=True)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
poll_hash_seen: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
create_time: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
update_time: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)