Skip to content

Commit 082a8ff

Browse files
Python: refactor(core): one declaration factory, per-call registry
Review of the host-declaration factories asked for two changes. The helpers had grown to five or six functions that did roughly the same thing, and the shared helper registered agent_framework.interceptor on the caller-supplied HostRegistry before validation. HostRegistry.kind() mutates in place, so a refused document or a raising kind resolver left the caller's registry holding the kind: the next factory call failed as already registered, or reused the first call's resolver. create_agent_hooks_middleware_from_declaration is now the only declaration factory. It accepts a built HostDeclaration, any Mapping (copied into a dict for the SDK), or a file path as str or os.PathLike, read once through HostDeclaration.from_path. JSON text is not accepted directly; HostDeclaration.from_json covers it. The registry keyword is replaced by configure_registry, a callback that receives a fresh HostRegistry built for that call over this framework's surface, after agent_framework.interceptor is registered when interceptors is given and before the document is validated. The registry never leaves the factory and nothing caller-owned is mutated, so a refused document or a raising callback cannot affect a later call. A callback that registers agent_framework.interceptor while interceptors is also given hits the SDK's own "registered twice" ValueError. The callback's return value is ignored (Callable[[HostRegistry], object]), so a lambda over the chaining registry methods works. A document with no bindings is refused at construction with ValueError, the rule create_agent_hooks_middleware already applies, since an emitter with zero interceptors fails closed on every emission. This mirrors the .NET factories, which take Action<HostRegistry> configureRegistry and refuse zero bindings the same way. Removed: create_agent_hooks_middleware_from_declaration_path, create_agent_hooks_middleware_from_declaration_json and create_agent_hooks_host_registry, with the surface check they needed, from the module, the lazy export map, the stub and PACKAGE_STATUS.md. Tests cover reuse of one callback after a refused document and after the callback itself raised, two factories sharing one callback and one interceptors mapping, the registered-twice clash, str and os.PathLike paths, a missing file, JSON text given as a path, a lambda callback, a document with no bindings, and a kind, identity provider, approval resolver and redactor registered through the callback. The per-run reload and the from_emitter single-session tests stay. Signed-off-by: MohammadHaroonAbuomar <[email protected]>
1 parent 38f3e4e commit 082a8ff

5 files changed

Lines changed: 455 additions & 294 deletions

File tree

‎python/PACKAGE_STATUS.md‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,13 @@ listed below.
8383
#### `AGENT_HOOKS`
8484

8585
- `agent-framework-core`: `create_agent_hooks_middleware`,
86-
`create_agent_hooks_middleware_from_emitter`, the host-declaration factories
87-
`create_agent_hooks_middleware_from_declaration`,
88-
`create_agent_hooks_middleware_from_declaration_path` and
89-
`create_agent_hooks_middleware_from_declaration_json`, and
90-
`create_agent_hooks_host_registry` from `agent_framework/_agent_hooks.py`,
86+
`create_agent_hooks_middleware_from_emitter` and the host-declaration factory
87+
`create_agent_hooks_middleware_from_declaration` from `agent_framework/_agent_hooks.py`,
9188
the AGENT-HOOKS-0.1 enforcement middleware bundle, and the `MiddlewareBundle`
9289
container from `agent_framework/_middleware.py` that the factories produce
9390
(`MiddlewareBundle` itself needs no optional dependency). Requires installing the
9491
`agent-hooks-sdk` package separately (0.1.0b2 or later for the host-declaration
95-
factories). Known limitation: service-side (hosted) tool
92+
factory). Known limitation: service-side (hosted) tool
9693
execution never passes through the framework's function-invocation seam, so the
9794
`pre_tool_call`/`post_tool_call` points cannot intercept it; hosted tool calls and
9895
outputs are surfaced in the `post_model_call` content projection instead.

‎python/packages/core/agent_framework/__init__.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@
4343

4444
_LAZY_MODULE_EXPORTS: Final[Mapping[str, tuple[str, ...]]] = {
4545
"._agent_hooks": (
46-
"create_agent_hooks_host_registry",
4746
"create_agent_hooks_middleware",
4847
"create_agent_hooks_middleware_from_declaration",
49-
"create_agent_hooks_middleware_from_declaration_json",
50-
"create_agent_hooks_middleware_from_declaration_path",
5148
"create_agent_hooks_middleware_from_emitter",
5249
),
5350
"._agents": ("Agent", "BaseAgent", "RawAgent", "SupportsAgentRun"),
@@ -688,11 +685,8 @@
688685
"background_tasks_running",
689686
"background_tasks_running_message",
690687
"chat_middleware",
691-
"create_agent_hooks_host_registry",
692688
"create_agent_hooks_middleware",
693689
"create_agent_hooks_middleware_from_declaration",
694-
"create_agent_hooks_middleware_from_declaration_json",
695-
"create_agent_hooks_middleware_from_declaration_path",
696690
"create_agent_hooks_middleware_from_emitter",
697691
"create_always_approve_tool_response",
698692
"create_always_approve_tool_with_arguments_response",

‎python/packages/core/agent_framework/__init__.pyi‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ from typing import Final
55
__version__: Final[str]
66

77
from ._agent_hooks import (
8-
create_agent_hooks_host_registry,
98
create_agent_hooks_middleware,
109
create_agent_hooks_middleware_from_declaration,
11-
create_agent_hooks_middleware_from_declaration_json,
12-
create_agent_hooks_middleware_from_declaration_path,
1310
create_agent_hooks_middleware_from_emitter,
1411
)
1512
from ._agents import Agent, BaseAgent, RawAgent, SupportsAgentRun
@@ -643,11 +640,8 @@ __all__ = [
643640
"background_tasks_running",
644641
"background_tasks_running_message",
645642
"chat_middleware",
646-
"create_agent_hooks_host_registry",
647643
"create_agent_hooks_middleware",
648644
"create_agent_hooks_middleware_from_declaration",
649-
"create_agent_hooks_middleware_from_declaration_json",
650-
"create_agent_hooks_middleware_from_declaration_path",
651645
"create_agent_hooks_middleware_from_emitter",
652646
"create_always_approve_tool_response",
653647
"create_always_approve_tool_with_arguments_response",

0 commit comments

Comments
 (0)