amoghrajesh commented on code in PR #69003:
URL: https://github.com/apache/airflow/pull/69003#discussion_r3479201926


##########
providers/anthropic/src/airflow/providers/anthropic/operators/anthropic.py:
##########
@@ -0,0 +1,236 @@
+# 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 logging
+import time
+from collections.abc import Sequence
+from datetime import timedelta
+from functools import cached_property
+from typing import TYPE_CHECKING, Any
+
+from airflow.providers.anthropic.exceptions import AnthropicBatchJobError, 
AnthropicBatchTimeout
+from airflow.providers.anthropic.hooks.anthropic import AnthropicHook
+from airflow.providers.anthropic.triggers.anthropic import 
AnthropicBatchTrigger
+from airflow.providers.common.compat.sdk import AirflowSkipException, 
BaseOperator, conf
+
+if TYPE_CHECKING:
+    from airflow.providers.common.compat.sdk import Context
+
+logger = logging.getLogger(__name__)
+
+
+def evaluate_batch_counts(

Review Comment:
   Should this live in the hook instead? It's used across operators and 
sensors, avoids cross imports.



##########
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py:
##########
@@ -0,0 +1,531 @@
+# 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 time
+from enum import Enum
+from functools import cached_property
+from typing import TYPE_CHECKING, Any, cast
+
+from anthropic import (
+    Anthropic,
+    AnthropicAWS,
+    AnthropicBedrock,
+    AnthropicFoundry,
+    AnthropicVertex,
+    IdentityTokenFile,
+    WorkloadIdentityCredentials,
+)
+
+from airflow.providers.anthropic.exceptions import (
+    AnthropicAgentSessionError,
+    AnthropicAgentSessionTimeout,
+    AnthropicBatchTimeout,
+    AnthropicError,
+)
+from airflow.providers.common.compat.sdk import BaseHook
+
+if TYPE_CHECKING:
+    from collections.abc import Iterable, Iterator
+
+    from anthropic.types import Message
+    from anthropic.types.beta import (
+        BetaEnvironment,
+        BetaManagedAgentsAgent,
+        BetaManagedAgentsSession,
+        environment_create_params,
+    )
+    from anthropic.types.beta.sessions import BetaManagedAgentsEventParams
+    from anthropic.types.messages import MessageBatch, 
MessageBatchIndividualResponse
+    from anthropic.types.messages.batch_create_params import Request
+
+#: Default model used when an operator or hook caller does not specify one.
+DEFAULT_MODEL = "claude-opus-4-8"

Review Comment:
   ```suggestion
   #: Default model used when an operator or hook caller does not specify one.
   #: Prefer configuring the model on the connection so it can be updated 
without
   #: a provider release when this model ID is retired.
   DEFAULT_MODEL = "claude-opus-4-8"
   ```



##########
providers/anthropic/tests/unit/anthropic/operators/test_agent.py:
##########
@@ -0,0 +1,178 @@
+# 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 unittest import mock
+
+import pytest
+
+from airflow.exceptions import TaskDeferred
+from airflow.providers.anthropic.exceptions import AnthropicAgentSessionError, 
AnthropicAgentSessionTimeout
+from airflow.providers.anthropic.operators.agent import 
AnthropicAgentSessionOperator
+from airflow.providers.anthropic.triggers.agent import 
AnthropicAgentSessionTrigger
+
+pytest.importorskip("anthropic")
+
+
+def _context():
+    return {"ti": mock.MagicMock()}
+
+
+def test_requires_exactly_one_of_message_or_outcome():
+    with pytest.raises(ValueError, match="exactly one"):
+        AnthropicAgentSessionOperator(task_id="a", agent_id="ag", 
environment_id="env")
+    with pytest.raises(ValueError, match="exactly one"):
+        AnthropicAgentSessionOperator(
+            task_id="a", agent_id="ag", environment_id="env", message="hi", 
outcome={"description": "x"}
+        )
+
+
+def test_outcome_requires_rubric():
+    with pytest.raises(ValueError, match="rubric"):
+        AnthropicAgentSessionOperator(
+            task_id="a", agent_id="ag", environment_id="env", 
outcome={"description": "x"}
+        )
+
+
+class TestExecute:
+    @mock.patch.object(AnthropicAgentSessionOperator, "hook", 
new_callable=mock.PropertyMock)
+    def test_message_sends_user_message_and_waits(self, mock_hook_prop):
+        hook = mock.MagicMock()
+        hook.create_session.return_value.id = "sess_1"
+        mock_hook_prop.return_value = hook
+
+        op = AnthropicAgentSessionOperator(
+            task_id="a", agent_id="ag", environment_id="env", 
message="summarize", deferrable=False
+        )
+        context = _context()
+        assert op.execute(context) == "sess_1"
+        hook.create_session.assert_called_once_with(agent="ag", 
environment_id="env")
+        hook.send_event.assert_called_once_with(
+            "sess_1", {"type": "user.message", "content": [{"type": "text", 
"text": "summarize"}]}
+        )
+        hook.wait_for_session.assert_called_once()
+        context["ti"].xcom_push.assert_called_once_with(key="session_id", 
value="sess_1")
+
+    @mock.patch.object(AnthropicAgentSessionOperator, "hook", 
new_callable=mock.PropertyMock)
+    def test_outcome_sends_define_outcome(self, mock_hook_prop):
+        hook = mock.MagicMock()

Review Comment:
   Here and elsewhere, can we add a spec or autospec please?



-- 
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