AlejandroMorgante commented on code in PR #68799:
URL: https://github.com/apache/airflow/pull/68799#discussion_r3559448403


##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/ai_agents.py:
##########
@@ -0,0 +1,483 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from functools import cached_property
+from typing import TYPE_CHECKING, Any, cast
+from urllib.parse import quote
+
+from azure.ai.projects import AIProjectClient
+from azure.ai.projects.aio import AIProjectClient as AsyncAIProjectClient
+from azure.core.exceptions import ResourceNotFoundError
+from azure.core.rest import HttpRequest
+from azure.identity import ClientSecretCredential
+from azure.identity.aio import ClientSecretCredential as 
AsyncClientSecretCredential
+
+from airflow.providers.common.compat.connection import get_async_connection
+from airflow.providers.common.compat.sdk import BaseHook
+from airflow.providers.microsoft.azure.hooks.base_azure import 
_AZURE_CLOUD_ENVIRONMENTS
+from airflow.providers.microsoft.azure.utils import (
+    add_managed_identity_connection_widgets,
+    get_async_default_azure_credential,
+    get_field,
+    get_sync_default_azure_credential,
+)
+
+if TYPE_CHECKING:
+    from azure.ai.projects.models import (
+        AgentBlueprintReference,
+        AgentDefinition,
+        AgentDetails,
+        AgentVersionDetails,
+        DeleteAgentResponse,
+        DeleteAgentVersionResponse,
+    )
+    from azure.core.credentials import TokenCredential
+    from azure.core.credentials_async import AsyncTokenCredential
+
+    from airflow.sdk import Connection
+
+
+DEFAULT_REQUEST_TIMEOUT = 60.0
+VERSION_INTERMEDIATE_STATUSES = {"creating", "deleting"}
+VERSION_SUCCESS_STATUSES = {"active"}
+VERSION_FAILURE_STATUSES = {"failed"}
+VERSION_DELETED_STATUS = "deleted"
+
+
+def _serialize_resource(resource: Any) -> Any:
+    """Serialize an SDK model or response object into XCom-safe primitives."""
+    if resource is None or isinstance(resource, str | int | float | bool):
+        return resource
+    if isinstance(resource, list | tuple):
+        return [_serialize_resource(item) for item in resource]
+    if isinstance(resource, dict):
+        return {key: _serialize_resource(value) for key, value in 
resource.items()}
+    if hasattr(resource, "as_dict"):
+        return _serialize_resource(resource.as_dict())
+    if hasattr(resource, "model_dump"):
+        return _serialize_resource(resource.model_dump())
+    return resource
+
+
+def _get_resource_attr(resource: Any, attr: str) -> Any:
+    """Get an attribute from an SDK resource or mapping."""
+    if isinstance(resource, dict):
+        return resource.get(attr)
+    return getattr(resource, attr, None)
+
+
+def _get_version_status(version: Any) -> str:
+    """Return a normalized Hosted agent version status string."""
+    status = _get_resource_attr(version, "status")
+    if hasattr(status, "value"):
+        status = status.value
+    if status is None:
+        raise ValueError("Azure AI Hosted agent version did not include a 
status.")
+    return str(status).lower()
+
+
+def _get_agent_version(version: Any) -> str:
+    """Return the version identifier from a Hosted agent version payload."""
+    agent_version = _get_resource_attr(version, "version")
+    if agent_version is None:
+        raise ValueError("Azure AI Hosted agent response did not include a 
version.")
+    return str(agent_version)
+
+
+class AzureAIAgentsHook(BaseHook):
+    """
+    Hook for Microsoft Foundry Hosted agents, backed by the 
``azure-ai-projects`` SDK.
+
+    Wraps the Agents operations of :class:`azure.ai.projects.AIProjectClient`:
+    
https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/deploy-hosted-agent
+
+    :param azure_ai_agents_conn_id: The Azure AI Agents connection id.
+        Default is ``azure_ai_agents_default``.
+    :param endpoint: Optional Azure AI Foundry project endpoint. If not 
provided, the hook uses the
+        connection host or the ``endpoint`` connection extra. Default is 
``None``.
+    :param api_version: Foundry Agent Service API version. Default is ``v1``.
+    :param timeout: Optional connection/read timeout for service requests, in 
seconds.
+        Default is ``60.0``.
+    """
+
+    conn_name_attr = "azure_ai_agents_conn_id"
+    default_conn_name = "azure_ai_agents_default"
+    conn_type = "azure_ai_agents"
+    hook_name = "Azure AI Foundry Hosted Agents"
+
+    def __init__(
+        self,
+        azure_ai_agents_conn_id: str = default_conn_name,
+        endpoint: str | None = None,
+        api_version: str = "v1",
+        timeout: float | None = DEFAULT_REQUEST_TIMEOUT,
+    ) -> None:
+        super().__init__()
+        self.conn_id = azure_ai_agents_conn_id
+        self.endpoint = endpoint
+        self.api_version = api_version
+        self.timeout = timeout
+
+    @classmethod
+    @add_managed_identity_connection_widgets
+    def get_connection_form_widgets(cls) -> dict[str, Any]:
+        """Return connection widgets to add to connection form."""
+        from flask_appbuilder.fieldwidgets import BS3TextFieldWidget
+        from flask_babel import lazy_gettext
+        from wtforms import StringField
+
+        return {
+            "tenantId": StringField(lazy_gettext("Azure Tenant ID"), 
widget=BS3TextFieldWidget()),
+            "cloud_environment": StringField(
+                lazy_gettext("Azure Cloud Environment"), 
widget=BS3TextFieldWidget()
+            ),
+            "endpoint": StringField(lazy_gettext("Project Endpoint"), 
widget=BS3TextFieldWidget()),
+        }
+
+    @classmethod
+    def get_ui_field_behaviour(cls) -> dict[str, Any]:
+        """Return custom field behaviour."""
+        return {
+            "hidden_fields": ["schema", "port"],
+            "relabeling": {
+                "host": "Project Endpoint",
+                "login": "Azure Client ID",
+                "password": "Azure Secret",
+            },
+            "placeholders": {
+                "host": 
"https://<aiservices-id>.services.ai.azure.com/api/projects/<project-name>",
+                "login": "client_id (token credentials auth)",
+                "password": "secret (token credentials auth)",
+                "tenantId": "tenantId (token credentials auth)",
+                "cloud_environment": "AzurePublicCloud (default) | 
AzureUSGovernment | AzureChinaCloud",
+                "endpoint": "Overrides Project Endpoint from host",
+            },
+        }
+
+    @cached_property
+    def _connection(self) -> Connection:
+        return self.get_connection(self.conn_id)
+
+    @cached_property
+    def _client(self) -> AIProjectClient:

Review Comment:
   Done! 
   
   Removed the `_client` cached property and moved client creation and caching 
directly into `get_conn()` using `_sync_client`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to