maigp-autogen 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,6 @@
1
+ Metadata-Version: 2.4
2
+ Name: maigp-autogen
3
+ Version: 1.0.0
4
+ Summary: AIGP governance adapter for Microsoft AutoGen multi-agent conversations
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: maigp-agent-core>=1.0.0
@@ -0,0 +1,7 @@
1
+ """AIGP-AutoGen — Governance adapter for Microsoft AutoGen multi-agent conversations."""
2
+
3
+ __version__ = "1.0.0"
4
+
5
+ from .handler import AigpAutoGenHook
6
+
7
+ __all__ = ["AigpAutoGenHook"]
@@ -0,0 +1,117 @@
1
+ """AutoGen Hook — governs multi-agent conversations via message interception.
2
+
3
+ AutoGen has:
4
+ - ConversableAgent: base agent that sends/receives messages
5
+ - GroupChat: orchestrates multi-agent conversations
6
+ - Each message exchange = one "turn" (agent speaks)
7
+
8
+ Governance model:
9
+ - GroupChat start → parent session CHECK
10
+ - Each agent turn → child TRACE span (S24: inter_agent_communication)
11
+ - Model calls within turns → token accumulation
12
+ - GroupChat end → aggregate RECORD
13
+ """
14
+
15
+ import asyncio
16
+ import logging
17
+ import time
18
+
19
+ from aigp_agent_core import AgentGovernance
20
+ from aigp_client import SessionContext, TokenAccumulator
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+
25
+ class AigpAutoGenHook:
26
+ """AutoGen governance hook — intercepts messages for AIGP governance.
27
+
28
+ Usage:
29
+ from aigp_autogen import AigpAutoGenHook
30
+
31
+ hook = AigpAutoGenHook(gov_url=..., app_id="my-group-chat", hmac_secret=...)
32
+
33
+ # Register with agents
34
+ for agent in [agent_a, agent_b, agent_c]:
35
+ agent.register_hook("process_message_before_send", hook.before_send)
36
+
37
+ # Or use with GroupChat
38
+ hook.on_chat_start(group_chat)
39
+ result = group_chat.run()
40
+ hook.on_chat_end()
41
+ """
42
+
43
+ def __init__(self, gov_url: str, app_id: str, hmac_secret: str, **kwargs):
44
+ self._gov = AgentGovernance(gov_url, app_id, hmac_secret, **kwargs)
45
+ self._session: SessionContext | None = None
46
+ self._tokens = TokenAccumulator()
47
+ self._turn_count = 0
48
+ self._agents_seen: set[str] = set()
49
+ self._t0 = 0.0
50
+
51
+ def on_chat_start(self, group_chat=None) -> str:
52
+ """Call before group chat starts. Sends CHECK."""
53
+ chat_name = getattr(group_chat, "name", self._gov._app_id) if group_chat else self._gov._app_id
54
+ self._t0 = time.time()
55
+ self._session = SessionContext(user_id="group_chat")
56
+ self._turn_count = 0
57
+ self._agents_seen.clear()
58
+ return self._gov.pre_invoke(chat_name, "group_chat")
59
+
60
+ def before_send(self, sender: str, message: dict, recipient: str, **kwargs) -> dict:
61
+ """Message hook — called before each agent sends a message.
62
+
63
+ Register with: agent.register_hook("process_message_before_send", hook.before_send)
64
+ Returns the message (unmodified — governance is observational).
65
+ """
66
+ self._turn_count += 1
67
+ self._agents_seen.add(sender)
68
+ self._agents_seen.add(recipient)
69
+
70
+ # Record as inter-agent communication span
71
+ if self._gov._trace:
72
+ self._gov._trace.start(24)
73
+ self._gov._trace.end(24, attributes={
74
+ "sender": sender, "recipient": recipient,
75
+ "turn": self._turn_count, "msg_type": message.get("role", "assistant"),
76
+ })
77
+
78
+ return message
79
+
80
+ def on_model_response(self, agent_name: str, usage: dict = None) -> None:
81
+ """Call after an agent's model call returns. Accumulates tokens."""
82
+ if usage:
83
+ self._tokens.add_from_usage(usage)
84
+ self._gov.on_model_call(usage=usage or {})
85
+
86
+ def on_tool_call(self, agent_name: str, tool_name: str, duration_ms: int = 0) -> None:
87
+ """Call when an agent uses a tool."""
88
+ self._run_async(self._gov.on_tool_call(tool_name, duration_ms=duration_ms))
89
+
90
+ def on_chat_end(self, result=None) -> None:
91
+ """Call after group chat completes. Sends aggregate RECORD + TRACE."""
92
+ self._gov._tokens = self._tokens
93
+ self._run_async(self._gov.post_invoke())
94
+
95
+ def on_chat_error(self, error: Exception) -> None:
96
+ """Call on group chat failure."""
97
+ self._run_async(self._gov.on_error(error))
98
+
99
+ @property
100
+ def summary(self) -> dict:
101
+ """Current conversation summary."""
102
+ return {
103
+ "turns": self._turn_count,
104
+ "agents": list(self._agents_seen),
105
+ "tokens": self._tokens.to_dict(),
106
+ "duration_ms": int((time.time() - self._t0) * 1000) if self._t0 else 0,
107
+ }
108
+
109
+ def _run_async(self, coro):
110
+ try:
111
+ loop = asyncio.get_event_loop()
112
+ if loop.is_running():
113
+ asyncio.ensure_future(coro)
114
+ else:
115
+ loop.run_until_complete(coro)
116
+ except RuntimeError:
117
+ asyncio.run(coro)
@@ -0,0 +1,13 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "maigp-autogen"
7
+ version = "1.0.0"
8
+ description = "AIGP governance adapter for Microsoft AutoGen multi-agent conversations"
9
+ requires-python = ">=3.11"
10
+ dependencies = ["maigp-agent-core>=1.0.0"]
11
+
12
+ [tool.hatch.build.targets.wheel]
13
+ packages = ["aigp_autogen"]