maigp-microsoft-agent-framework 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.
- maigp_microsoft_agent_framework-1.0.0/.gitignore +56 -0
- maigp_microsoft_agent_framework-1.0.0/PKG-INFO +9 -0
- maigp_microsoft_agent_framework-1.0.0/aigp_microsoft_agent_framework/__init__.py +27 -0
- maigp_microsoft_agent_framework-1.0.0/aigp_microsoft_agent_framework/handler.py +109 -0
- maigp_microsoft_agent_framework-1.0.0/pyproject.toml +16 -0
- maigp_microsoft_agent_framework-1.0.0/tests/__init__.py +0 -0
- maigp_microsoft_agent_framework-1.0.0/tests/test_handler.py +99 -0
|
@@ -0,0 +1,56 @@
|
|
|
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/
|
|
47
|
+
|
|
48
|
+
# Generated navigable spec split — derived from the monoliths by
|
|
49
|
+
# specification/tools/spec_split.py; the manifest + monoliths are the
|
|
50
|
+
# source of truth, not this output. Same convention as Mars (spec/*/*/).
|
|
51
|
+
specification/*/*/
|
|
52
|
+
specification/split-index.md
|
|
53
|
+
|
|
54
|
+
# Local rendering/preview site — self-contained, not part of the
|
|
55
|
+
# published specification, never pushed.
|
|
56
|
+
site/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: maigp-microsoft-agent-framework
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: AIGP governance adapter for Microsoft Agent Framework (MAF) — the converged Semantic Kernel + AutoGen SDK (GA 2026-04-03)
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Requires-Dist: maigp-agent-core>=1.0.0
|
|
7
|
+
Provides-Extra: dev
|
|
8
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
9
|
+
Requires-Dist: pytest-asyncio; extra == 'dev'
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""AIGP adapter for Microsoft Agent Framework (MAF).
|
|
2
|
+
|
|
3
|
+
MAF 1.0 (GA 2026-04-03) is the converged successor to Semantic Kernel + AutoGen — a single
|
|
4
|
+
SDK for agents and multi-agent workflows in Python/.NET (chat clients, tools, MCP integrations,
|
|
5
|
+
context providers, middleware, workflows). This adapter brings MAF agents under AIGP governance
|
|
6
|
+
(CHECK before invoke, RECORD/TRACE after, tool-call governance) via aigp-agent-core.
|
|
7
|
+
|
|
8
|
+
It replaces the separate `aigp-autogen` and `aigp-semantic-kernel` adapters, which target the
|
|
9
|
+
now-merged predecessor frameworks.
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
from agent_framework import ChatAgent # MAF
|
|
13
|
+
from aigp_microsoft_agent_framework import AigpMafGovernance
|
|
14
|
+
|
|
15
|
+
gov = AigpMafGovernance(gov_url="https://gov.example.com", app_id="my-maf-agent",
|
|
16
|
+
hmac_secret="secret")
|
|
17
|
+
|
|
18
|
+
agent = ChatAgent(chat_client=..., tools=[...], middleware=[gov.middleware()])
|
|
19
|
+
result = await gov.run(agent, "Analyze this finding", agent_name="analyst")
|
|
20
|
+
# ✓ CHECK before run · token usage recorded · tool calls governed · RECORD+TRACE after
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
__version__ = "1.0.0"
|
|
24
|
+
|
|
25
|
+
from .handler import AigpMafGovernance, AigpMafMiddleware
|
|
26
|
+
|
|
27
|
+
__all__ = ["AigpMafGovernance", "AigpMafMiddleware"]
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Microsoft Agent Framework (MAF) governance handler.
|
|
2
|
+
|
|
3
|
+
MAF exposes agents (e.g. ChatAgent) with a `run`/`run_stream` surface, function tools, and a
|
|
4
|
+
middleware chain. This adapter governs a MAF agent run through aigp-agent-core:
|
|
5
|
+
|
|
6
|
+
- run start → CHECK (pre_invoke)
|
|
7
|
+
- each model reply → token accumulation (on_model_call)
|
|
8
|
+
- each tool call → tool governance (on_tool_call)
|
|
9
|
+
- run complete → RECORD + TRACE (post_invoke)
|
|
10
|
+
- failure → on_error
|
|
11
|
+
|
|
12
|
+
Two integration styles:
|
|
13
|
+
1. `AigpMafGovernance.run(agent, prompt, ...)` — wrap a run explicitly.
|
|
14
|
+
2. `AigpMafMiddleware` — register as MAF agent/function middleware for automatic coverage.
|
|
15
|
+
|
|
16
|
+
The framework object is duck-typed (usage read from the response), so this adapter does not
|
|
17
|
+
import MAF directly and works across MAF point releases.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import asyncio
|
|
21
|
+
import logging
|
|
22
|
+
import time
|
|
23
|
+
|
|
24
|
+
from aigp_agent_core import AgentGovernance
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _extract_usage(response) -> dict:
|
|
30
|
+
"""Best-effort token usage from a MAF run result / model response."""
|
|
31
|
+
usage = getattr(response, "usage", None) or getattr(response, "usage_details", None)
|
|
32
|
+
if usage is None and isinstance(response, dict):
|
|
33
|
+
usage = response.get("usage")
|
|
34
|
+
if usage is None:
|
|
35
|
+
return {}
|
|
36
|
+
# MAF/OpenAI-style attributes or dict keys
|
|
37
|
+
def _get(o, *names):
|
|
38
|
+
for n in names:
|
|
39
|
+
v = getattr(o, n, None) if not isinstance(o, dict) else o.get(n)
|
|
40
|
+
if v is not None:
|
|
41
|
+
return v
|
|
42
|
+
return 0
|
|
43
|
+
return {
|
|
44
|
+
"input_tokens": _get(usage, "input_token_count", "prompt_tokens", "input_tokens"),
|
|
45
|
+
"output_tokens": _get(usage, "output_token_count", "completion_tokens", "output_tokens"),
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class AigpMafGovernance:
|
|
50
|
+
"""Govern Microsoft Agent Framework agent runs with AIGP."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, gov_url: str, app_id: str, hmac_secret: str, **kwargs):
|
|
53
|
+
self._gov = AgentGovernance(gov_url, app_id, hmac_secret, **kwargs)
|
|
54
|
+
self._app_id = app_id
|
|
55
|
+
|
|
56
|
+
async def run(self, agent, prompt, *, agent_name: str = "", model_id: str = "",
|
|
57
|
+
user_id: str = "", **run_kwargs):
|
|
58
|
+
"""Governed MAF agent run. CHECK before, RECORD/TRACE after.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
agent: a MAF agent exposing an async ``run(prompt, **kwargs)``.
|
|
62
|
+
prompt: the input passed to the agent.
|
|
63
|
+
agent_name: use-case label (defaults to the agent's name or app_id).
|
|
64
|
+
model_id: model identifier for the RECORD (best-effort if omitted).
|
|
65
|
+
"""
|
|
66
|
+
name = agent_name or getattr(agent, "name", None) or self._app_id
|
|
67
|
+
model = model_id or getattr(agent, "model_id", "") or getattr(agent, "model", "")
|
|
68
|
+
self._gov.pre_invoke(name, str(model), user_id)
|
|
69
|
+
try:
|
|
70
|
+
result = await agent.run(prompt, **run_kwargs)
|
|
71
|
+
self._gov.on_model_call(usage=_extract_usage(result))
|
|
72
|
+
await self._gov.post_invoke()
|
|
73
|
+
return result
|
|
74
|
+
except Exception as e:
|
|
75
|
+
await self._gov.on_error(e)
|
|
76
|
+
raise
|
|
77
|
+
|
|
78
|
+
async def on_tool_call(self, tool_name: str, params: dict | None = None, duration_ms: int = 0):
|
|
79
|
+
"""Report a MAF function-tool invocation for governance."""
|
|
80
|
+
await self._gov.on_tool_call(tool_name, params=params, duration_ms=duration_ms)
|
|
81
|
+
|
|
82
|
+
def middleware(self) -> "AigpMafMiddleware":
|
|
83
|
+
"""Return a MAF-compatible middleware bound to this governance instance."""
|
|
84
|
+
return AigpMafMiddleware(self)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class AigpMafMiddleware:
|
|
88
|
+
"""MAF agent/function middleware that governs each run.
|
|
89
|
+
|
|
90
|
+
MAF middleware are callables invoked around agent runs and function calls with a `context`
|
|
91
|
+
and a `next` continuation. This class supports both by duck-typing the context.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
def __init__(self, governance: AigpMafGovernance):
|
|
95
|
+
self._g = governance
|
|
96
|
+
|
|
97
|
+
async def __call__(self, context, next): # noqa: A002 - MAF's middleware signature
|
|
98
|
+
agent = getattr(context, "agent", None)
|
|
99
|
+
name = getattr(agent, "name", None) or getattr(context, "agent_name", "") or self._g._app_id
|
|
100
|
+
model = getattr(agent, "model_id", "") or getattr(agent, "model", "")
|
|
101
|
+
self._g._gov.pre_invoke(name, str(model), getattr(context, "user_id", ""))
|
|
102
|
+
try:
|
|
103
|
+
result = await next(context)
|
|
104
|
+
self._g._gov.on_model_call(usage=_extract_usage(result))
|
|
105
|
+
await self._g._gov.post_invoke()
|
|
106
|
+
return result
|
|
107
|
+
except Exception as e:
|
|
108
|
+
await self._g._gov.on_error(e)
|
|
109
|
+
raise
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "maigp-microsoft-agent-framework"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "AIGP governance adapter for Microsoft Agent Framework (MAF) — the converged Semantic Kernel + AutoGen SDK (GA 2026-04-03)"
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = ["maigp-agent-core>=1.0.0"]
|
|
11
|
+
|
|
12
|
+
[project.optional-dependencies]
|
|
13
|
+
dev = ["pytest", "pytest-asyncio"]
|
|
14
|
+
|
|
15
|
+
[tool.hatch.build.targets.wheel]
|
|
16
|
+
packages = ["aigp_microsoft_agent_framework"]
|
|
File without changes
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Tests for the AIGP Microsoft Agent Framework adapter — mocked core + fake MAF agent."""
|
|
2
|
+
|
|
3
|
+
from unittest.mock import AsyncMock, MagicMock
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.fixture(autouse=True)
|
|
9
|
+
def mock_core(monkeypatch):
|
|
10
|
+
import sys
|
|
11
|
+
core = MagicMock()
|
|
12
|
+
|
|
13
|
+
class MockAgentGovernance:
|
|
14
|
+
def __init__(self, *a, **k):
|
|
15
|
+
self._app_id = "maf-1"
|
|
16
|
+
self.pre_invoke = MagicMock(return_value="req-maf-1")
|
|
17
|
+
self.on_model_call = MagicMock()
|
|
18
|
+
self.post_invoke = AsyncMock()
|
|
19
|
+
self.on_error = AsyncMock()
|
|
20
|
+
self.on_tool_call = AsyncMock()
|
|
21
|
+
|
|
22
|
+
core.AgentGovernance = MockAgentGovernance
|
|
23
|
+
monkeypatch.setitem(sys.modules, "aigp_agent_core", core)
|
|
24
|
+
return core
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.fixture
|
|
28
|
+
def gov(mock_core):
|
|
29
|
+
from aigp_microsoft_agent_framework import AigpMafGovernance
|
|
30
|
+
return AigpMafGovernance(gov_url="http://gov.test", app_id="maf-1", hmac_secret="s")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class FakeUsage:
|
|
34
|
+
input_token_count = 12
|
|
35
|
+
output_token_count = 34
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class FakeAgent:
|
|
39
|
+
name = "analyst"
|
|
40
|
+
model_id = "gpt-5.6"
|
|
41
|
+
|
|
42
|
+
def __init__(self, fail=False):
|
|
43
|
+
self._fail = fail
|
|
44
|
+
|
|
45
|
+
async def run(self, prompt, **kw):
|
|
46
|
+
if self._fail:
|
|
47
|
+
raise RuntimeError("model error")
|
|
48
|
+
r = MagicMock()
|
|
49
|
+
r.usage = FakeUsage()
|
|
50
|
+
return r
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.mark.asyncio
|
|
54
|
+
async def test_run_checks_records_and_extracts_usage(gov):
|
|
55
|
+
agent = FakeAgent()
|
|
56
|
+
result = await gov.run(agent, "analyze this")
|
|
57
|
+
gov._gov.pre_invoke.assert_called_once()
|
|
58
|
+
# usage extracted from MAF-style input_token_count/output_token_count
|
|
59
|
+
gov._gov.on_model_call.assert_called_once()
|
|
60
|
+
usage = gov._gov.on_model_call.call_args.kwargs["usage"]
|
|
61
|
+
assert usage == {"input_tokens": 12, "output_tokens": 34}
|
|
62
|
+
gov._gov.post_invoke.assert_awaited_once()
|
|
63
|
+
assert result is not None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.mark.asyncio
|
|
67
|
+
async def test_run_uses_agent_name_and_model(gov):
|
|
68
|
+
await gov.run(FakeAgent(), "x")
|
|
69
|
+
args = gov._gov.pre_invoke.call_args[0]
|
|
70
|
+
assert args[0] == "analyst" and args[1] == "gpt-5.6"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.mark.asyncio
|
|
74
|
+
async def test_run_reports_error(gov):
|
|
75
|
+
with pytest.raises(RuntimeError):
|
|
76
|
+
await gov.run(FakeAgent(fail=True), "x")
|
|
77
|
+
gov._gov.on_error.assert_awaited_once()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@pytest.mark.asyncio
|
|
81
|
+
async def test_tool_call_governed(gov):
|
|
82
|
+
await gov.on_tool_call("search", params={"q": "x"}, duration_ms=5)
|
|
83
|
+
gov._gov.on_tool_call.assert_awaited_once()
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@pytest.mark.asyncio
|
|
87
|
+
async def test_middleware_governs_run(gov):
|
|
88
|
+
mw = gov.middleware()
|
|
89
|
+
ctx = MagicMock()
|
|
90
|
+
ctx.agent = FakeAgent()
|
|
91
|
+
ctx.user_id = "u1"
|
|
92
|
+
|
|
93
|
+
async def _next(c):
|
|
94
|
+
r = MagicMock(); r.usage = FakeUsage(); return r
|
|
95
|
+
|
|
96
|
+
result = await mw(ctx, _next)
|
|
97
|
+
gov._gov.pre_invoke.assert_called_once()
|
|
98
|
+
gov._gov.post_invoke.assert_awaited_once()
|
|
99
|
+
assert result is not None
|