from __future__ import annotations import uuid from datetime import datetime, timezone from sqlalchemy import String, Integer, Text, JSON, UniqueConstraint from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column def now(): return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") def uid(prefix): return prefix + "-" + uuid.uuid4().hex[:24] class RuntimeError(Exception): def __init__(self,status,code,message,details=None): self.status=status;self.code=code;self.message=message;self.details=details or {} super().__init__(message) def require_admin(user): if "admin" not in user.get("roles",[]):raise RuntimeError(403,"ADMIN_REQUIRED","需要管理员权限。") class Base(DeclarativeBase):pass class Definition(Base): __tablename__="ar_definition" tenant:Mapped[str]=mapped_column(String(80),primary_key=True) kind:Mapped[str]=mapped_column(String(24),primary_key=True) id:Mapped[str]=mapped_column(String(80),primary_key=True) revision:Mapped[int]=mapped_column(Integer,nullable=False) body:Mapped[dict]=mapped_column(JSON,nullable=False) updated_at:Mapped[str]=mapped_column(String(40),nullable=False) class Published(Base): __tablename__="ar_published" tenant:Mapped[str]=mapped_column(String(80),primary_key=True) workflow_id:Mapped[str]=mapped_column(String(80),primary_key=True) version:Mapped[int]=mapped_column(Integer,primary_key=True) body:Mapped[dict]=mapped_column(JSON,nullable=False) created_at:Mapped[str]=mapped_column(String(40),nullable=False) class Run(Base): __tablename__="ar_run" id:Mapped[str]=mapped_column(String(80),primary_key=True) tenant:Mapped[str]=mapped_column(String(80),nullable=False,index=True) owner:Mapped[str]=mapped_column(String(80),nullable=False,index=True) workspace:Mapped[str]=mapped_column(String(80),nullable=False,index=True) active_scope:Mapped[str|None]=mapped_column(String(200),unique=True) status:Mapped[str]=mapped_column(String(32),nullable=False,index=True) revision:Mapped[int]=mapped_column(Integer,nullable=False,default=1) body:Mapped[dict]=mapped_column(JSON,nullable=False) class Event(Base): __tablename__="ar_event" run_id:Mapped[str]=mapped_column(String(80),primary_key=True) sequence:Mapped[int]=mapped_column(Integer,primary_key=True) body:Mapped[dict]=mapped_column(JSON,nullable=False) class Command(Base): __tablename__="ar_command" tenant:Mapped[str]=mapped_column(String(80),primary_key=True) owner:Mapped[str]=mapped_column(String(80),primary_key=True) scope:Mapped[str]=mapped_column(String(200),primary_key=True) key:Mapped[str]=mapped_column(String(128),primary_key=True) fingerprint:Mapped[str]=mapped_column(String(64),nullable=False) response:Mapped[dict]=mapped_column(JSON,nullable=False) class Audit(Base): __tablename__="ar_audit" id:Mapped[str]=mapped_column(String(80),primary_key=True) tenant:Mapped[str]=mapped_column(String(80),nullable=False,index=True) actor:Mapped[str]=mapped_column(String(80),nullable=False) action:Mapped[str]=mapped_column(String(80),nullable=False) resource:Mapped[str]=mapped_column(String(80),nullable=False) at:Mapped[str]=mapped_column(String(40),nullable=False)