Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import annotations

import logging
from typing import Optional, Any
from typing import Any

from pydantic import (
ModelWrapValidatorHandler,
Expand All @@ -25,12 +25,12 @@
class _ChannelIdFieldMixin:
"""A mixin to add a computed field channel_id of type ChannelId to a Pydantic model."""

_channel_id: Optional[ChannelId] = None
_channel_id: ChannelId | None = None

# required to define the setter below
@computed_field(return_type=Optional[ChannelId], alias="channelId")
@computed_field(return_type=ChannelId | None, alias="channelId")
@property
def channel_id(self) -> Optional[ChannelId]:
def channel_id(self) -> ChannelId | None:
"""Gets the _channel_id field"""
return self._channel_id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from copy import copy
from datetime import datetime, timezone
from typing import Optional, Any
from typing import Any, cast

from pydantic import (
Field,
Expand All @@ -15,7 +15,6 @@
model_validator,
SerializerFunctionWrapHandler,
ModelWrapValidatorHandler,
computed_field,
ValidationError,
)

Expand Down Expand Up @@ -152,7 +151,7 @@ class Activity(AgentsModel, _ChannelIdFieldMixin):
"""

type: NonEmptyString
id: Optional[NonEmptyString] = None
id: NonEmptyString | None = None
timestamp: datetime = None
local_timestamp: datetime = None
local_timezone: NonEmptyString = None
Expand Down Expand Up @@ -497,7 +496,7 @@ def create_message_activity():
"""
return Activity(type=ActivityTypes.message)

def create_reply(self, text: str = None, locale: str = None):
def create_reply(self, text: str | None = None, locale: str | None = None):
"""
Creates a new message activity as a response to this activity.

Expand Down Expand Up @@ -539,7 +538,11 @@ def create_reply(self, text: str = None, locale: str = None):
)

def create_trace(
self, name: str, value: object = None, value_type: str = None, label: str = None
self,
name: str,
value: object | None = None,
value_type: str | None = None,
label: str | None = None,
):
"""
Creates a new trace activity based on this activity.
Expand All @@ -551,41 +554,47 @@ def create_trace(

:returns: The new trace activity.
"""
if not value_type and value:
if not value_type and value is not None:
value_type = type(value).__name__

return pick_model(
return cast(
Activity,
type=ActivityTypes.trace,
timestamp=datetime.now(timezone.utc),
from_property=SkipNone(
ChannelAccount.pick_properties(self.recipient, ["id", "name"])
),
recipient=SkipNone(
ChannelAccount.pick_properties(self.from_property, ["id", "name"])
),
reply_to_id=(
SkipNone(self.id) # preserve unset
if type != ActivityTypes.conversation_update
or self.channel_id not in ["directline", "webchat"]
else None
),
service_url=self.service_url,
channel_id=self.channel_id,
conversation=SkipNone(
ConversationAccount.pick_properties(
self.conversation, ["is_group", "id", "name"]
)
pick_model(
Activity,
type=ActivityTypes.trace,
timestamp=datetime.now(timezone.utc),
from_property=SkipNone(
ChannelAccount.pick_properties(self.recipient, ["id", "name"])
),
recipient=SkipNone(
ChannelAccount.pick_properties(self.from_property, ["id", "name"])
),
reply_to_id=(
SkipNone(self.id) # preserve unset
if type != ActivityTypes.conversation_update
or self.channel_id not in ["directline", "webchat"]
else None
),
service_url=self.service_url,
channel_id=self.channel_id,
conversation=SkipNone(
ConversationAccount.pick_properties(
self.conversation, ["is_group", "id", "name"]
)
),
name=SkipNone(name),
label=SkipNone(label),
value_type=SkipNone(value_type),
value=SkipNone(value),
),
name=SkipNone(name),
label=SkipNone(label),
value_type=SkipNone(value_type),
value=SkipNone(value),
).as_trace_activity()

@staticmethod
def create_trace_activity(
name: str, value: object = None, value_type: str = None, label: str = None
name: str,
value: object | None = None,
value_type: str | None = None,
label: str | None = None,
):
"""
Creates an instance of the :class:`Activity` class as a TraceActivity object.
Expand All @@ -597,7 +606,7 @@ def create_trace_activity(

:returns: The new trace activity.
"""
if not value_type and value:
if not value_type and value is not None:
value_type = type(value).__name__

return pick_model(
Expand All @@ -624,23 +633,26 @@ def get_conversation_reference(self) -> ConversationReference:

:returns: A conversation reference for the conversation that contains this activity.
"""
return pick_model(
return cast(
ConversationReference,
activity_id=(
SkipNone(self.id)
if self.type != ActivityTypes.conversation_update
or self.channel_id not in ["directline", "webchat"]
else None
pick_model(
ConversationReference,
activity_id=(
SkipNone(self.id)
if self.type != ActivityTypes.conversation_update
or self.channel_id not in ["directline", "webchat"]
else None
),
user=copy(self.from_property),
agent=copy(self.recipient),
conversation=copy(self.conversation),
channel_id=self.channel_id,
locale=self.locale,
service_url=self.service_url,
),
user=copy(self.from_property),
agent=copy(self.recipient),
conversation=copy(self.conversation),
channel_id=self.channel_id,
locale=self.locale,
service_url=self.service_url,
)

def get_product_info_entity(self) -> Optional[ProductInfo]:
def get_product_info_entity(self) -> ProductInfo | None:
if not self.entities:
return None
target = EntityTypes.PRODUCT_INFO.lower()
Expand Down Expand Up @@ -741,8 +753,8 @@ def __is_activity(self, activity_type: str) -> bool:

def add_ai_metadata(
self,
citations: Optional[list[ClientCitation]] = None,
usage_info: Optional[SensitivityUsageInfo] = None,
citations: list[ClientCitation] | None = None,
usage_info: SensitivityUsageInfo | None = None,
) -> None:
"""
Adds AI entity to an activity to indicate AI-generated content.
Expand Down Expand Up @@ -771,19 +783,19 @@ def is_agentic_request(self) -> bool:
RoleTypes.agentic_user,
]

def get_agentic_instance_id(self) -> Optional[str]:
def get_agentic_instance_id(self) -> str | None:
"""Gets the agent instance ID from the context if it's an agentic request."""
if not self.is_agentic_request() or not self.recipient:
return None
return self.recipient.agentic_app_id

def get_agentic_user(self) -> Optional[str]:
def get_agentic_user(self) -> str | None:
"""Gets the agentic user (agenticUserId) from the context if it's an agentic request."""
if not self.is_agentic_request() or not self.recipient:
return None
return self.recipient.agentic_user_id

def get_agentic_tenant_id(self) -> Optional[str]:
def get_agentic_tenant_id(self) -> str | None:
"""Gets the agentic tenant ID from the context if it's an agentic request."""
if self.is_agentic_request():
if self.recipient and self.recipient.tenant_id:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Optional
from .agents_model import AgentsModel
from ._type_aliases import NonEmptyString

Expand Down Expand Up @@ -38,5 +37,5 @@ class CardAction(AgentsModel):
text: str = None
display_text: str = None
value: object = None
channel_data: Optional[object] = None
channel_data: object | None = None
image_alt_text: str = None
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Any, Optional
from typing import Any

from pydantic import ConfigDict
from .agents_model import AgentsModel
Expand All @@ -27,11 +27,11 @@ class ChannelAccount(AgentsModel):

id: NonEmptyString = None
name: str = None
aad_object_id: Optional[NonEmptyString] = None
role: Optional[NonEmptyString] = None
agentic_user_id: Optional[NonEmptyString] = None
agentic_app_id: Optional[NonEmptyString] = None
tenant_id: Optional[NonEmptyString] = None
aad_object_id: NonEmptyString | None = None
role: NonEmptyString | None = None
agentic_user_id: NonEmptyString | None = None
agentic_app_id: NonEmptyString | None = None
tenant_id: NonEmptyString | None = None

@property
def properties(self) -> dict[str, Any]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the MIT License.

from abc import abstractmethod
from typing import Protocol, List, Callable, Awaitable, Optional
from typing import Protocol, Callable, Awaitable

from .turn_context_protocol import TurnContextProtocol
from microsoft_agents.activity import (
Expand All @@ -14,12 +14,12 @@


class ChannelAdapterProtocol(Protocol):
on_turn_error: Optional[Callable[[TurnContextProtocol, Exception], Awaitable]]
on_turn_error: Callable[[TurnContextProtocol, Exception], Awaitable] | None

@abstractmethod
async def send_activities(
self, context: TurnContextProtocol, activities: List[Activity]
) -> List[ResourceResponse]:
self, context: TurnContextProtocol, activities: list[Activity]
) -> list[ResourceResponse]:
pass

@abstractmethod
Expand Down Expand Up @@ -54,7 +54,7 @@ async def continue_conversation_with_claims(
claims_identity: dict,
continuation_activity: Activity,
callback: Callable[[TurnContextProtocol], Awaitable],
audience: str = None,
audience: str | None = None,
):
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from typing import Optional, Any
from typing import Any

from pydantic_core import CoreSchema, core_schema
from pydantic import GetCoreSchemaHandler
Expand All @@ -16,10 +16,10 @@ class ChannelId(str):

def __init__(
self,
value: Optional[str] = None,
value: str | None = None,
*,
channel: Optional[str] = None,
sub_channel: Optional[str] = None,
channel: str | None = None,
sub_channel: str | None = None,
) -> None:
"""Initialize a ChannelId instance.

Expand All @@ -39,10 +39,10 @@ def __init__(

def __new__(
cls,
value: Optional[str] = None,
value: str | None = None,
*,
channel: Optional[str] = None,
sub_channel: Optional[str] = None,
channel: str | None = None,
sub_channel: str | None = None,
) -> ChannelId:
"""Create a new ChannelId instance.

Expand Down Expand Up @@ -83,7 +83,7 @@ def channel(self) -> str:
return self._channel # type: ignore[return-value]

@property
def sub_channel(self) -> Optional[str]:
def sub_channel(self) -> str | None:
"""The sub-channel, e.g. 'work' in 'email:work'. May be None."""
return self._sub_channel

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Optional
from .agents_model import AgentsModel
from ._type_aliases import NonEmptyString

Expand Down Expand Up @@ -31,11 +30,11 @@ class ConversationAccount(AgentsModel):
:type properties: object
"""

is_group: Optional[bool] = None
conversation_type: Optional[NonEmptyString] = None
is_group: bool | None = None
conversation_type: NonEmptyString | None = None
id: NonEmptyString
name: Optional[NonEmptyString] = None
aad_object_id: Optional[NonEmptyString] = None
role: Optional[NonEmptyString] = None
tenant_id: Optional[NonEmptyString] = None
name: NonEmptyString | None = None
aad_object_id: NonEmptyString | None = None
role: NonEmptyString | None = None
tenant_id: NonEmptyString | None = None
properties: object = None
Loading
Loading