AlejandroMorgante commented on code in PR #68799: URL: https://github.com/apache/airflow/pull/68799#discussion_r3509556201
########## providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/ai_agents.py: ########## @@ -0,0 +1,457 @@ +# +# 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 + +import asyncio +import json +from functools import cached_property +from typing import TYPE_CHECKING, Any, cast +from urllib.parse import quote + +from azure.core.exceptions import ResourceNotFoundError +from azure.identity import ClientSecretCredential +from requests import Session +from requests.exceptions import HTTPError + +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_field, + get_sync_default_azure_credential, +) + +if TYPE_CHECKING: + from azure.core.credentials import TokenCredential + from requests import Response + + from airflow.sdk import Connection + + +HOSTED_AGENT_FEATURE_HEADER = "HostedAgents=V1Preview" +TOKEN_SCOPE = "https://ai.azure.com/.default" +VERSION_INTERMEDIATE_STATUSES = {"creating", "deleting"} +VERSION_SUCCESS_STATUSES = {"active"} +VERSION_FAILURE_STATUSES = {"failed"} + + +def serialize_resource(resource: Any) -> Any: + """Serialize an SDK or HTTP 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()) + if hasattr(resource, "__dict__"): + return { + key: serialize_resource(value) for key, value in vars(resource).items() if not key.startswith("_") + } + 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 or agent payload. + + Accepts both a ``agent.version`` object (returned by POST /agents/{name}/versions and + GET /agents/{name}/versions/{version}) and a top-level ``agent`` object (returned by + POST /agents), extracting the version from ``versions.latest.version`` in the latter case. + """ + agent_version = get_resource_attr(version, "version") or get_resource_attr(version, "agent_version") + if agent_version is None: + # POST /agents returns an agent object; the initial version lives under versions.latest + versions = get_resource_attr(version, "versions") + latest = get_resource_attr(versions, "latest") if versions is not None else None + agent_version = get_resource_attr(latest, "version") if latest is not None else None + if agent_version is None: + raise ValueError("Azure AI Hosted agent response did not include a version.") + return str(agent_version) Review Comment: Good catch, done. I transformed the module-level helper functions to private functions -- 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]
