70 lines
1.8 KiB
Markdown
70 lines
1.8 KiB
Markdown
# Шаблон кода агента
|
|
|
|
Используйте этот шаблон для создания нового системного агента.
|
|
|
|
```python
|
|
"""Agent: {name} — {description}."""
|
|
|
|
from datetime import datetime, timezone
|
|
from typing import Any
|
|
|
|
from app.agents.base import BaseAgent, AgentResult, AgentTrigger
|
|
|
|
|
|
class {Name}Agent(BaseAgent):
|
|
"""{Description} agent.
|
|
|
|
Triggers: {triggers}
|
|
"""
|
|
|
|
name = "{name}"
|
|
version = "1.0.0"
|
|
description = "{description}"
|
|
triggers = [AgentTrigger.MANUAL]
|
|
|
|
async def run(self, context: dict[str, Any] | None = None) -> AgentResult:
|
|
"""Execute agent task.
|
|
|
|
Args:
|
|
context: Optional context with execution parameters
|
|
|
|
Returns:
|
|
AgentResult with execution outcome
|
|
"""
|
|
start = datetime.now(timezone.utc)
|
|
errors: list[str] = []
|
|
data: dict[str, Any] = {}
|
|
|
|
try:
|
|
# === AGENT LOGIC HERE ===
|
|
# 1. Do the work
|
|
# 2. Collect results
|
|
# 3. Handle errors
|
|
pass
|
|
|
|
except Exception as e:
|
|
errors.append(str(e))
|
|
|
|
duration = int((datetime.now(timezone.utc) - start).total_seconds() * 1000)
|
|
|
|
result = AgentResult(
|
|
success=len(errors) == 0,
|
|
message=f"{self.name} completed with {len(errors)} errors",
|
|
data=data,
|
|
errors=errors,
|
|
duration_ms=duration,
|
|
)
|
|
|
|
# Auto-version check
|
|
new_version = await self._check_version()
|
|
if new_version:
|
|
result.data["version_bumped"] = True
|
|
result.data["new_version"] = new_version
|
|
|
|
return result
|
|
|
|
async def health_check(self) -> bool:
|
|
"""Check if agent can execute."""
|
|
return True
|
|
```
|