dbeadd8ab1
Check Cross-Plugin Imports / check (push) Has been cancelled
- F-CTX: app/ai/context_builder.py (282 lines) — build_agent_context() + ReActSystemPromptBuilder - F-STR: app/ai/agent_stream.py (155 lines) — stream_react_loop() with SSE events (step, status, done, error) - F-DEF: AgentDefinition fields added (temperature, max_tokens, max_steps, trace_mode, skill_ids, trigger_config, ai_use_case_metadata) + migration 0122 - F-SKILL: app/ai/skill_registry.py (82 lines) — SkillDefinition + SkillRegistry singleton - F-TOOL: app/ai/agent_tools.py (117 lines) — get_agent_tools() with permission intersection - Skill CRUD routes: app/plugins/builtins/automation/skill_routes.py - Tests: test_skill_registry.py (97 lines), test_agent_tools.py (219 lines) - All Python compile checks pass, tests require PostgreSQL (infra issue, not code bug)
365 lines
14 KiB
Python
365 lines
14 KiB
Python
"""SQLAlchemy models for the Automation & Agents plugin."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
DateTime,
|
|
Float,
|
|
ForeignKey,
|
|
Index,
|
|
Integer,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.core.db import Base, TenantMixin
|
|
from app.models.owned_mixin import OwnedMixin
|
|
|
|
|
|
class AgentDefinition(Base, TenantMixin, OwnedMixin):
|
|
"""An AI agent definition — configures an LLM-powered agent with tools and behavior."""
|
|
|
|
__tablename__ = "automation_agent_definitions"
|
|
__table_args__ = (
|
|
UniqueConstraint("tenant_id", "name", name="uq_agent_def_name"),
|
|
Index("ix_agent_def_tenant_active", "tenant_id", "is_active"),
|
|
Index("ix_agent_def_tenant_mode", "tenant_id", "mode"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
description: Mapped[str] = mapped_column(String(500), nullable=False, default="")
|
|
llm_model: Mapped[str] = mapped_column(
|
|
String(100), nullable=False, default="ollama/deepseek-v4-flash"
|
|
)
|
|
system_prompt: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
tool_ids: Mapped[list[Any]] = mapped_column(JSONB, nullable=False, default=list)
|
|
heartbeat_interval_seconds: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=0
|
|
)
|
|
mode: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="reactive"
|
|
)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
|
max_executions_per_hour: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=10
|
|
)
|
|
max_duration_seconds: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=300
|
|
)
|
|
budget_limit_usd: Mapped[float] = mapped_column(
|
|
Float, nullable=False, default=1.0
|
|
)
|
|
temperature: Mapped[float] = mapped_column(Float, nullable=False, default=0.3)
|
|
max_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=1000)
|
|
max_steps: Mapped[int] = mapped_column(Integer, nullable=False, default=20)
|
|
trace_mode: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="standard"
|
|
)
|
|
skill_ids: Mapped[list[Any]] = mapped_column(JSONB, nullable=False, default=list)
|
|
trigger_config: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
ai_use_case_metadata: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
|
|
class AgentVersion(Base, TenantMixin):
|
|
"""Versioned snapshots of agent definitions."""
|
|
|
|
__tablename__ = "automation_agent_versions"
|
|
__table_args__ = (
|
|
Index("ix_agent_versions_agent", "tenant_id", "agent_id"),
|
|
UniqueConstraint("agent_id", "version_number", name="uq_agent_version"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
agent_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_agent_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
version_number: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
snapshot: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
|
changed_by: Mapped[uuid.UUID | None] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
|
|
class AutomationDefinition(Base, TenantMixin, OwnedMixin):
|
|
"""An automation workflow definition — event/schedule/manual triggered with conditions and actions."""
|
|
|
|
__tablename__ = "automation_definitions"
|
|
__table_args__ = (
|
|
UniqueConstraint("tenant_id", "name", name="uq_automation_def_name"),
|
|
Index("ix_automation_def_tenant_active", "tenant_id", "is_active"),
|
|
Index("ix_automation_def_tenant_trigger", "tenant_id", "trigger_type"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
description: Mapped[str] = mapped_column(String(500), nullable=False, default="")
|
|
trigger_type: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="manual"
|
|
)
|
|
trigger_config: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
conditions: Mapped[list[Any]] = mapped_column(JSONB, nullable=False, default=list)
|
|
actions: Mapped[list[Any]] = mapped_column(JSONB, nullable=False, default=list)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
|
dry_run: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
|
|
class AutomationVersion(Base, TenantMixin):
|
|
"""Versioned snapshots of automation definitions."""
|
|
|
|
__tablename__ = "automation_versions"
|
|
__table_args__ = (
|
|
Index("ix_automation_versions_def", "tenant_id", "automation_id"),
|
|
UniqueConstraint("automation_id", "version_number", name="uq_automation_version"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
automation_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
version_number: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
snapshot: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
|
changed_by: Mapped[uuid.UUID | None] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
|
|
class AutomationCronJob(Base, TenantMixin):
|
|
"""Cron job schedule entries for agent heartbeats, automation triggers, or custom jobs."""
|
|
|
|
__tablename__ = "automation_cron_jobs"
|
|
__table_args__ = (
|
|
Index("ix_cron_jobs_tenant_active", "tenant_id", "is_active"),
|
|
Index("ix_cron_jobs_next_run", "next_run_at"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
cron_expression: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
job_type: Mapped[str] = mapped_column(
|
|
String(50), nullable=False, default="automation_trigger"
|
|
)
|
|
target_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
PGUUID(as_uuid=True), nullable=True
|
|
)
|
|
plugin_name: Mapped[str] = mapped_column(String(80), nullable=False, default="")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
|
last_run_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
next_run_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True, index=True
|
|
)
|
|
|
|
|
|
class AgentRun(Base, TenantMixin):
|
|
"""Execution log for agent runs."""
|
|
|
|
__tablename__ = "automation_agent_runs"
|
|
__table_args__ = (
|
|
Index("ix_agent_runs_agent_status", "tenant_id", "agent_id", "status"),
|
|
Index("ix_agent_runs_started", "tenant_id", "started_at"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
agent_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_agent_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="pending"
|
|
)
|
|
started_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False
|
|
)
|
|
completed_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
duration_seconds: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
result: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
cost_usd: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
trigger_type: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="manual"
|
|
)
|
|
trigger_data: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
|
|
|
|
class AutomationRun(Base, TenantMixin):
|
|
"""Execution log for automation runs."""
|
|
|
|
__tablename__ = "automation_runs"
|
|
__table_args__ = (
|
|
Index("ix_automation_runs_def_status", "tenant_id", "automation_id", "status"),
|
|
Index("ix_automation_runs_started", "tenant_id", "started_at"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
automation_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="pending"
|
|
)
|
|
started_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False
|
|
)
|
|
completed_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
duration_seconds: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
result: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
trigger_type: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="manual"
|
|
)
|
|
trigger_data: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
dry_run: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
|
|
|
|
class AgentRunStep(Base, TenantMixin):
|
|
"""Individual step in a ReAct loop execution (Thought → Action → Observation)."""
|
|
|
|
__tablename__ = "automation_agent_run_steps"
|
|
__table_args__ = (
|
|
Index("ix_agent_run_steps_run", "tenant_id", "agent_run_id"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
agent_run_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_agent_runs.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
step_number: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
thought: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
action: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
action_input: Mapped[dict[str, Any] | None] = mapped_column(JSONB, nullable=True)
|
|
observation: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
cost_usd: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class AgentSubtask(Base, TenantMixin):
|
|
"""A subtask delegated from one agent to another for multi-agent orchestration."""
|
|
|
|
__tablename__ = "agent_subtasks"
|
|
__table_args__ = (
|
|
Index("ix_agent_subtasks_parent", "tenant_id", "parent_agent_id"),
|
|
Index("ix_agent_subtasks_child", "tenant_id", "child_agent_id"),
|
|
Index("ix_agent_subtasks_status", "tenant_id", "status"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
parent_agent_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_agent_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
child_agent_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("automation_agent_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
task_description: Mapped[str] = mapped_column(Text, nullable=False)
|
|
status: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="pending"
|
|
)
|
|
result: Mapped[dict[str, Any]] = mapped_column(
|
|
JSONB, nullable=False, default=dict
|
|
)
|
|
completed_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
|
|
|
|
class SkillDefinitionDB(Base, TenantMixin):
|
|
"""A skill definition persisted per tenant.
|
|
|
|
Skills are orchestration metadata, NOT a permission source. They reference
|
|
tool IDs that the agent and the user must already be permitted to use.
|
|
"""
|
|
|
|
__tablename__ = "automation_skill_definitions"
|
|
__table_args__ = (
|
|
Index("ix_skill_defs_tenant_active", "tenant_id", "is_active"),
|
|
Index("ix_skill_defs_tenant_category", "tenant_id", "category"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
|
description: Mapped[str] = mapped_column(Text, nullable=False)
|
|
instructions: Mapped[str] = mapped_column(Text, nullable=False)
|
|
allowed_tool_ids: Mapped[list[Any]] = mapped_column(JSONB, nullable=False, default=list)
|
|
context_policy: Mapped[dict[str, Any] | None] = mapped_column(JSONB, nullable=True)
|
|
category: Mapped[str] = mapped_column(String(100), nullable=False, default="general")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now(), onupdate=func.now()
|
|
)
|