maigp-crewai 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.venv/
|
|
8
|
+
.eggs/
|
|
9
|
+
|
|
10
|
+
# Node
|
|
11
|
+
node_modules/
|
|
12
|
+
dist/
|
|
13
|
+
|
|
14
|
+
# Go
|
|
15
|
+
bin/
|
|
16
|
+
|
|
17
|
+
# Rust
|
|
18
|
+
target/
|
|
19
|
+
|
|
20
|
+
# .NET
|
|
21
|
+
bin/
|
|
22
|
+
obj/
|
|
23
|
+
|
|
24
|
+
# Java
|
|
25
|
+
*.class
|
|
26
|
+
out/
|
|
27
|
+
|
|
28
|
+
# IDE
|
|
29
|
+
.idea/
|
|
30
|
+
.vscode/
|
|
31
|
+
.kiro/
|
|
32
|
+
*.swp
|
|
33
|
+
|
|
34
|
+
# OS
|
|
35
|
+
.DS_Store
|
|
36
|
+
Thumbs.db
|
|
37
|
+
|
|
38
|
+
# Secrets (never commit)
|
|
39
|
+
.env
|
|
40
|
+
*.pem
|
|
41
|
+
*.key
|
|
42
|
+
*.pdf
|
|
43
|
+
BACKLOG/
|
|
44
|
+
patent/
|
|
45
|
+
docs/.archive/
|
|
46
|
+
review/
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""CrewAI Callbacks — maps CrewAI task/agent lifecycle to AIGP governance.
|
|
2
|
+
|
|
3
|
+
CrewAI has:
|
|
4
|
+
- Crew: orchestrator that runs tasks
|
|
5
|
+
- Task: unit of work assigned to an agent
|
|
6
|
+
- Agent: individual AI worker with role/goal/tools
|
|
7
|
+
|
|
8
|
+
Governance model:
|
|
9
|
+
- Crew start → parent session CHECK
|
|
10
|
+
- Each task → child session with DelegationToken
|
|
11
|
+
- Each agent model call → token accumulation
|
|
12
|
+
- Crew end → aggregate RECORD + linked TRACEs
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import asyncio
|
|
16
|
+
import logging
|
|
17
|
+
import time
|
|
18
|
+
|
|
19
|
+
from aigp_agent_core import AgentGovernance
|
|
20
|
+
from aigp_agent_core.governance import GovernanceBlockedError
|
|
21
|
+
from aigp_client import SessionContext, DelegationToken, TokenAccumulator
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AigpCrewCallbacks:
|
|
27
|
+
"""CrewAI governance callbacks.
|
|
28
|
+
|
|
29
|
+
Usage:
|
|
30
|
+
from aigp_crewai import AigpCrewCallbacks
|
|
31
|
+
|
|
32
|
+
callbacks = AigpCrewCallbacks(gov_url=..., app_id="my-crew", hmac_secret=...)
|
|
33
|
+
|
|
34
|
+
crew = Crew(
|
|
35
|
+
agents=[...],
|
|
36
|
+
tasks=[...],
|
|
37
|
+
step_callback=callbacks.on_step,
|
|
38
|
+
task_callback=callbacks.on_task_end,
|
|
39
|
+
)
|
|
40
|
+
callbacks.on_crew_start(crew)
|
|
41
|
+
result = crew.kickoff()
|
|
42
|
+
callbacks.on_crew_end(result)
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(self, gov_url: str, app_id: str, hmac_secret: str, **kwargs):
|
|
46
|
+
self._gov = AgentGovernance(gov_url, app_id, hmac_secret, **kwargs)
|
|
47
|
+
self._app_id = app_id
|
|
48
|
+
self._session: SessionContext | None = None
|
|
49
|
+
self._task_sessions: dict[str, SessionContext] = {}
|
|
50
|
+
self._task_tokens: dict[str, TokenAccumulator] = {}
|
|
51
|
+
self._t0 = 0.0
|
|
52
|
+
self._total_tokens = TokenAccumulator()
|
|
53
|
+
|
|
54
|
+
def on_crew_start(self, crew=None) -> str:
|
|
55
|
+
"""Call before crew.kickoff(). Sends parent CHECK."""
|
|
56
|
+
crew_name = getattr(crew, "name", self._app_id) if crew else self._app_id
|
|
57
|
+
self._t0 = time.time()
|
|
58
|
+
self._session = SessionContext(user_id="crew_orchestrator")
|
|
59
|
+
request_id = self._gov.pre_invoke(crew_name, "crew_orchestration", session=self._session)
|
|
60
|
+
return request_id
|
|
61
|
+
|
|
62
|
+
def on_task_start(self, task_name: str, agent_name: str = "") -> SessionContext:
|
|
63
|
+
"""Call when a task begins. Creates child session."""
|
|
64
|
+
child = self._session.child(child_user_id=agent_name, scope=[task_name])
|
|
65
|
+
self._task_sessions[task_name] = child
|
|
66
|
+
self._task_tokens[task_name] = TokenAccumulator()
|
|
67
|
+
return child
|
|
68
|
+
|
|
69
|
+
def on_step(self, step_output) -> None:
|
|
70
|
+
"""CrewAI step_callback — called after each agent step.
|
|
71
|
+
|
|
72
|
+
Accumulates tokens from the step's LLM response.
|
|
73
|
+
"""
|
|
74
|
+
usage = {}
|
|
75
|
+
if hasattr(step_output, "result") and hasattr(step_output.result, "usage"):
|
|
76
|
+
usage = step_output.result.usage or {}
|
|
77
|
+
elif hasattr(step_output, "output") and isinstance(step_output.output, dict):
|
|
78
|
+
usage = step_output.output.get("usage", {})
|
|
79
|
+
|
|
80
|
+
# Try to identify which task this belongs to
|
|
81
|
+
task_name = getattr(step_output, "task", {})
|
|
82
|
+
if hasattr(task_name, "description"):
|
|
83
|
+
task_name = task_name.description[:50]
|
|
84
|
+
else:
|
|
85
|
+
task_name = str(task_name)[:50]
|
|
86
|
+
|
|
87
|
+
if task_name in self._task_tokens:
|
|
88
|
+
self._task_tokens[task_name].add_from_usage(usage)
|
|
89
|
+
self._total_tokens.add_from_usage(usage)
|
|
90
|
+
|
|
91
|
+
def on_task_end(self, task_output) -> None:
|
|
92
|
+
"""CrewAI task_callback — called when a task completes."""
|
|
93
|
+
task_name = ""
|
|
94
|
+
if hasattr(task_output, "description"):
|
|
95
|
+
task_name = task_output.description[:50]
|
|
96
|
+
elif hasattr(task_output, "task"):
|
|
97
|
+
task_name = str(task_output.task)[:50]
|
|
98
|
+
|
|
99
|
+
if task_name in self._task_sessions:
|
|
100
|
+
del self._task_sessions[task_name]
|
|
101
|
+
|
|
102
|
+
def on_tool_use(self, tool_name: str, params: dict = None, duration_ms: int = 0) -> None:
|
|
103
|
+
"""Optional: call when an agent uses a tool."""
|
|
104
|
+
self._run_async(self._gov.on_tool_call(tool_name, params=params, duration_ms=duration_ms))
|
|
105
|
+
|
|
106
|
+
def on_crew_end(self, result=None) -> None:
|
|
107
|
+
"""Call after crew.kickoff() completes. Sends aggregate RECORD + TRACE."""
|
|
108
|
+
# Update governance token counts
|
|
109
|
+
self._gov._tokens = self._total_tokens
|
|
110
|
+
self._run_async(self._gov.post_invoke())
|
|
111
|
+
|
|
112
|
+
def on_crew_error(self, error: Exception) -> None:
|
|
113
|
+
"""Call on crew failure."""
|
|
114
|
+
self._run_async(self._gov.on_error(error))
|
|
115
|
+
|
|
116
|
+
def _run_async(self, coro):
|
|
117
|
+
try:
|
|
118
|
+
loop = asyncio.get_event_loop()
|
|
119
|
+
if loop.is_running():
|
|
120
|
+
asyncio.ensure_future(coro)
|
|
121
|
+
else:
|
|
122
|
+
loop.run_until_complete(coro)
|
|
123
|
+
except RuntimeError:
|
|
124
|
+
asyncio.run(coro)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "maigp-crewai"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "AIGP governance adapter for CrewAI multi-agent orchestration"
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = ["maigp-agent-core>=1.0.0"]
|
|
11
|
+
|
|
12
|
+
[tool.hatch.build.targets.wheel]
|
|
13
|
+
packages = ["aigp_crewai"]
|