AlejandroMorgante commented on code in PR #68799: URL: https://github.com/apache/airflow/pull/68799#discussion_r3559473210
########## 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: Review Comment: Moved to `_ai_agents.py` -- 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]
