Files
leocrm/app/plugins/builtins/automation/schemas.py
T
Agent Zero abbe7a18fc fix(audit): P0-P3 audit fixes — 838 ruff errors → 0, 30 F821 bugs fixed, 118 files changed
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner
- P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var
- P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup
- P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns
- P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs)
- P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import
- P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default
- P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed
- P2: 28 frontend TODOs (hardcoded constants, deprecated notification API)
- P3: dead code, duplicates, deprecated imports, private attr, __import__ inline
- P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n)
- ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix)
- F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String)
- Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
2026-08-16 01:17:18 +02:00

329 lines
8.8 KiB
Python

"""Pydantic v2 schemas for the Automation & Agents plugin API."""
from __future__ import annotations
from typing import Any
from pydantic import BaseModel, Field
# ─── Agent Definition Schemas ───
class AgentDefinitionCreate(BaseModel):
"""Create a new agent definition."""
name: str = Field(..., min_length=1, max_length=120)
description: str = Field(default="", max_length=500)
llm_model: str = Field(default="ollama/deepseek-v4-flash", max_length=100)
system_prompt: str = Field(default="")
tool_ids: list[str] = Field(default_factory=list)
heartbeat_interval_seconds: int = Field(default=0, ge=0)
mode: str = Field(default="reactive", pattern="^(proactive|reactive)$")
is_active: bool = Field(default=True)
max_executions_per_hour: int = Field(default=10, ge=1, le=1000)
max_duration_seconds: int = Field(default=300, ge=1, le=86400)
budget_limit_usd: float = Field(default=1.0, ge=0.0, le=10000.0)
class AgentDefinitionUpdate(BaseModel):
"""Update an existing agent definition (partial)."""
name: str | None = Field(None, max_length=120)
description: str | None = Field(None, max_length=500)
llm_model: str | None = Field(None, max_length=100)
system_prompt: str | None = None
tool_ids: list[str] | None = None
heartbeat_interval_seconds: int | None = Field(None, ge=0)
mode: str | None = Field(None, pattern="^(proactive|reactive)$")
is_active: bool | None = None
max_executions_per_hour: int | None = Field(None, ge=1, le=1000)
max_duration_seconds: int | None = Field(None, ge=1, le=86400)
budget_limit_usd: float | None = Field(None, ge=0.0, le=10000.0)
class AgentDefinitionResponse(BaseModel):
"""Agent definition response."""
id: str
name: str
description: str = ""
llm_model: str
system_prompt: str = ""
tool_ids: list[str] = []
heartbeat_interval_seconds: int = 0
mode: str
is_active: bool = True
max_executions_per_hour: int = 10
max_duration_seconds: int = 300
budget_limit_usd: float = 1.0
created_by: str | None = None
created_at: str | None = None
updated_at: str | None = None
# ─── Automation Definition Schemas ───
class AutomationDefinitionCreate(BaseModel):
"""Create a new automation definition."""
name: str = Field(..., min_length=1, max_length=120)
description: str = Field(default="", max_length=500)
trigger_type: str = Field(default="manual", pattern="^(event|schedule|manual|ui)$")
trigger_config: dict[str, Any] = Field(default_factory=dict)
conditions: list[dict[str, Any]] = Field(default_factory=list)
actions: list[dict[str, Any]] = Field(default_factory=list)
is_active: bool = Field(default=True)
dry_run: bool = Field(default=False)
class AutomationDefinitionUpdate(BaseModel):
"""Update an existing automation definition (partial)."""
name: str | None = Field(None, max_length=120)
description: str | None = Field(None, max_length=500)
trigger_type: str | None = Field(None, pattern="^(event|schedule|manual|ui)$")
trigger_config: dict[str, Any] | None = None
conditions: list[dict[str, Any]] | None = None
actions: list[dict[str, Any]] | None = None
is_active: bool | None = None
dry_run: bool | None = None
class AutomationDefinitionResponse(BaseModel):
"""Automation definition response."""
id: str
name: str
description: str = ""
trigger_type: str
trigger_config: dict[str, Any] = {}
conditions: list[dict[str, Any]] = []
actions: list[dict[str, Any]] = []
is_active: bool = True
dry_run: bool = False
created_by: str | None = None
created_at: str | None = None
updated_at: str | None = None
# ─── Cron Job Schemas ───
class CronJobResponse(BaseModel):
"""Cron job response."""
id: str
name: str
cron_expression: str
job_type: str
target_id: str | None = None
plugin_name: str = ""
is_active: bool = True
last_run_at: str | None = None
next_run_at: str | None = None
created_at: str | None = None
updated_at: str | None = None
# ─── Run Log Schemas ───
class AgentRunResponse(BaseModel):
"""Agent run log response."""
id: str
agent_id: str
status: str
started_at: str
completed_at: str | None = None
duration_seconds: float | None = None
result: str | None = None
error: str | None = None
cost_usd: float | None = None
trigger_type: str = "manual"
trigger_data: dict[str, Any] = {}
created_at: str | None = None
class AutomationRunResponse(BaseModel):
"""Automation run log response."""
id: str
automation_id: str
status: str
started_at: str
completed_at: str | None = None
duration_seconds: float | None = None
result: str | None = None
error: str | None = None
trigger_type: str = "manual"
trigger_data: dict[str, Any] = {}
dry_run: bool = False
created_at: str | None = None
# ─── Version Schemas ───
class AgentVersionResponse(BaseModel):
"""Agent version response."""
id: str
agent_id: str
version_number: int
snapshot: dict[str, Any]
changed_by: str | None = None
created_at: str | None = None
class AutomationVersionResponse(BaseModel):
"""Automation version response."""
id: str
automation_id: str
version_number: int
snapshot: dict[str, Any]
changed_by: str | None = None
created_at: str | None = None
# ─── Settings Schemas ───
class AgentMessageRequest(BaseModel):
"""Request to send a message from one agent to another."""
to_agent_name: str = Field(..., min_length=1, max_length=120)
message: str = Field(..., min_length=1)
class MiniAppCreate(BaseModel):
"""Create a custom MiniApp definition."""
app_id: str = Field(..., min_length=1, max_length=80)
name: str = Field(..., min_length=1, max_length=120)
icon: str = Field(default="AppWindow")
description: str = Field(default="")
render_schema: dict[str, Any] = Field(default_factory=dict)
class MiniAppResponse(BaseModel):
"""MiniApp definition response."""
app_id: str
name: str
icon: str = "AppWindow"
description: str = ""
plugin_name: str = ""
render_schema: dict[str, Any] = {}
class AutomationSettingsResponse(BaseModel):
"""Automation settings response."""
default_llm_model: str = "ollama/deepseek-v4-flash"
heartbeat_default_interval: int = 300
max_concurrent_agents: int = 5
log_level: str = "INFO"
class AutomationSettingsUpdate(BaseModel):
"""Automation settings update (partial)."""
default_llm_model: str | None = None
heartbeat_default_interval: int | None = None
max_concurrent_agents: int | None = None
log_level: str | None = None
# ─── List Response Schemas ───
class AgentDefinitionListResponse(BaseModel):
"""Paginated agent definition list."""
items: list[AgentDefinitionResponse]
total: int
class AutomationDefinitionListResponse(BaseModel):
"""Paginated automation definition list."""
items: list[AutomationDefinitionResponse]
total: int
class AgentRunListResponse(BaseModel):
"""Paginated agent run list."""
items: list[AgentRunResponse]
total: int
class AutomationRunListResponse(BaseModel):
"""Paginated automation run list."""
items: list[AutomationRunResponse]
total: int
class CronJobListResponse(BaseModel):
"""Paginated cron job list."""
items: list[CronJobResponse]
total: int
class AgentVersionListResponse(BaseModel):
"""Paginated agent version list."""
items: list[AgentVersionResponse]
total: int
class AutomationVersionListResponse(BaseModel):
"""Paginated automation version list."""
items: list[AutomationVersionResponse]
total: int
# ─── Subtask Schemas (Phase 5.8) ───
class SubtaskCreate(BaseModel):
"""Create a new subtask for multi-agent orchestration."""
parent_agent_id: str = Field(..., description="UUID of the parent agent")
child_agent_id: str = Field(..., description="UUID of the child agent that will execute")
task_description: str = Field(..., min_length=1, description="Description of the task")
class SubtaskUpdate(BaseModel):
"""Update a subtask (status, result)."""
status: str | None = Field(None, pattern="^(pending|running|completed|failed|cancelled)$")
result: dict[str, Any] | None = None
class SubtaskRead(BaseModel):
"""Subtask response."""
id: str
parent_agent_id: str
child_agent_id: str
task_description: str
status: str = "pending"
result: dict[str, Any] = {}
created_at: str | None = None
completed_at: str | None = None
updated_at: str | None = None
class SubtaskListResponse(BaseModel):
"""Paginated subtask list."""
items: list[SubtaskRead]
total: int