AlejandroMorgante commented on code in PR #74386: URL: https://github.com/apache/airflow/pull/74386#discussion_r4343962628
########## providers/databricks/src/airflow/providers/databricks/hooks/agent.py: ########## @@ -0,0 +1,435 @@ +# 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. +"""Invoke agents served by Databricks DurableAgentServer.""" + +from __future__ import annotations + +import json +import time +from typing import TYPE_CHECKING, Any, cast +from urllib.parse import urlsplit +from uuid import UUID, uuid4 + +import aiohttp +import requests +from tenacity import RetryError, stop_before_delay + +from airflow.providers.common.compat.sdk import AirflowException, AirflowOptionalProviderFeatureException +from airflow.providers.databricks.exceptions import DatabricksAgentInvocationTimeout, DatabricksApiError +from airflow.providers.databricks.hooks.databricks_base import BaseDatabricksHook, _TokenAuth + +if TYPE_CHECKING: + from tenacity.stop import stop_base + + from airflow.providers.common.ai.exceptions import ManagedAgentInvocationError + from airflow.providers.common.ai.managed_agents.base import ( + BaseManagedAgentHook, + ManagedAgentCapabilities, + ManagedAgentRef, + ManagedAgentRequest, + ManagedAgentResponse, + ) +else: + try: + from airflow.providers.common.ai.exceptions import ManagedAgentInvocationError + from airflow.providers.common.ai.managed_agents.base import ( + BaseManagedAgentHook, + ManagedAgentCapabilities, + ManagedAgentRef, + ManagedAgentResponse, + ) + except ImportError as e: + _common_ai_import_error = e + + def _needs_common_ai(*args: Any, **kwargs: Any) -> Any: + raise AirflowOptionalProviderFeatureException( + "Consulting a Databricks agent needs the 'common.ai' extra: " + "pip install 'apache-airflow-providers-databricks[common.ai]'." + ) from _common_ai_import_error + + class BaseManagedAgentHook: + """Keep background invocation available without the Common AI extra.""" + + agent = _needs_common_ai + + ManagedAgentCapabilities = ManagedAgentRef = ManagedAgentResponse = _needs_common_ai + ManagedAgentInvocationError = _needs_common_ai + + +_AGENT_OPTIONS = frozenset({"input_key", "invocation_id"}) + + +class DatabricksAgentHook(BaseDatabricksHook, BaseManagedAgentHook): + """ + Invoke a DurableAgentServer deployed on Databricks Apps. + + With the ``common.ai`` extra installed, ``hook.agent(app_url)`` exposes the synchronous + invocation API through the managed-agent contract. Background invocations remain available + through ``create_invocation`` and ``get_invocation`` without that extra. + + :param app_url: HTTPS base URL for background invocation methods. Defaults to ``None``; + managed-agent calls receive the app URL as their ``agent`` argument. + :param databricks_conn_id: Databricks workspace connection configured with + ``service_principal_oauth=true``, a client ID in login and a client secret in password. + Defaults to ``databricks_default``. + :param timeout_seconds: HTTP request timeout in seconds. Defaults to ``180``. + :param retry_limit: Maximum number of attempts for transient HTTP failures. Defaults to ``3``. + :param retry_delay: Minimum delay between HTTP retries in seconds. Defaults to ``1``. + """ + + def __init__( + self, + app_url: str | None = None, Review Comment: Done, went with the first option: the background methods now take `app_url` per call, keeping the hook scoped to the connection and aligning with the Common AI interface. Updated the operator, trigger, docs and tests accordingly -- 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]
