Skip to content

Commit 03dc279

Browse files
Add hostings SDK (#92)
* add agents hosting and auto populate functionality * rename folder * ensure cycle dependencies are resolved * add tests * add token cache * add tests * remove unused tests * rename test file due to conflict * address pr comments --------- Co-authored-by: Nikhil Chitlur Navakiran (from Dev Box) <nikhilc@microsoft.com>
1 parent 3d097c4 commit 03dc279

22 files changed

Lines changed: 850 additions & 468 deletions

File tree

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/middleware/baggage_builder.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
)
3232
from ..models.operation_source import OperationSource
3333
from ..utils import deprecated, validate_and_normalize_ip
34-
from .turn_context_baggage import from_turn_context
3534

3635
logger = logging.getLogger(__name__)
3736

@@ -235,14 +234,6 @@ def channel_links(self, value: str | None) -> "BaggageBuilder":
235234
self._set(GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY, value)
236235
return self
237236

238-
def from_turn_context(self, turn_context: Any) -> "BaggageBuilder":
239-
"""
240-
Populate baggage from a turn_context (duck-typed).
241-
Delegates to baggage_turn_context.from_turn_context.
242-
"""
243-
244-
return self.set_pairs(from_turn_context(turn_context))
245-
246237
def set_pairs(self, pairs: Any) -> "BaggageBuilder":
247238
"""
248239
Accept dict or iterable of (k,v).

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/middleware/turn_context_baggage.py

Lines changed: 0 additions & 192 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Microsoft Agent 365 Observability Hosting Library
2+
3+
This library provides hosting components for Agent 365 observability.
4+
5+
## Installation
6+
7+
```bash
8+
pip install microsoft-agents-a365-observability-hosting
9+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
Microsoft Agent 365 Observability Hosting Library.
6+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Iterator
4+
from typing import Any
5+
6+
from microsoft_agents.hosting.core.turn_context import TurnContext
7+
from microsoft_agents_a365.observability.core.middleware.baggage_builder import BaggageBuilder
8+
9+
from .utils import (
10+
get_caller_pairs,
11+
get_conversation_pairs,
12+
get_execution_type_pair,
13+
get_source_metadata_pairs,
14+
get_target_agent_pairs,
15+
get_tenant_id_pair,
16+
)
17+
18+
19+
def _iter_all_pairs(turn_context: TurnContext) -> Iterator[tuple[str, Any]]:
20+
activity = turn_context.activity
21+
if not activity:
22+
return
23+
yield from get_caller_pairs(activity)
24+
yield from get_execution_type_pair(activity)
25+
yield from get_target_agent_pairs(activity)
26+
yield from get_tenant_id_pair(activity)
27+
yield from get_source_metadata_pairs(activity)
28+
yield from get_conversation_pairs(activity)
29+
30+
31+
def populate(builder: BaggageBuilder, turn_context: TurnContext) -> BaggageBuilder:
32+
"""Populate BaggageBuilder with baggage values extracted from a turn context.
33+
34+
Args:
35+
builder: The BaggageBuilder instance to populate
36+
turn_context: The TurnContext containing activity information
37+
38+
Returns:
39+
The updated BaggageBuilder instance (for method chaining)
40+
"""
41+
builder.set_pairs(_iter_all_pairs(turn_context))
42+
return builder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from microsoft_agents_a365.observability.core.invoke_agent_scope import InvokeAgentScope
6+
7+
from .utils import (
8+
get_caller_pairs,
9+
get_conversation_pairs,
10+
get_execution_type_pair,
11+
get_source_metadata_pairs,
12+
get_target_agent_pairs,
13+
get_tenant_id_pair,
14+
)
15+
16+
if TYPE_CHECKING:
17+
from microsoft_agents.hosting.core.turn_context import TurnContext
18+
19+
20+
def populate(scope: InvokeAgentScope, turn_context: TurnContext) -> InvokeAgentScope:
21+
"""
22+
Populate all supported InvokeAgentScope tags from the provided TurnContext.
23+
:param scope: The InvokeAgentScope instance to populate.
24+
:param turn_context: The TurnContext containing activity information.
25+
:return: The updated InvokeAgentScope instance.
26+
"""
27+
if not turn_context:
28+
raise ValueError("turn_context is required")
29+
30+
if not turn_context.activity:
31+
return scope
32+
33+
activity = turn_context.activity
34+
35+
scope.record_attributes(get_caller_pairs(activity))
36+
scope.record_attributes(get_execution_type_pair(activity))
37+
scope.record_attributes(get_target_agent_pairs(activity))
38+
scope.record_attributes(get_tenant_id_pair(activity))
39+
scope.record_attributes(get_source_metadata_pairs(activity))
40+
scope.record_attributes(get_conversation_pairs(activity))
41+
42+
if activity.text:
43+
scope.record_input_messages([activity.text])
44+
45+
return scope

0 commit comments

Comments
 (0)