|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from google.adk.agents import SequentialAgent as GoogleADKSequentialAgent |
| 18 | +from google.adk.agents.base_agent import BaseAgent |
| 19 | +from pydantic import ConfigDict, Field |
| 20 | +from typing_extensions import Any |
| 21 | + |
| 22 | +from veadk.prompts.agent_default_prompt import DEFAULT_DESCRIPTION, DEFAULT_INSTRUCTION |
| 23 | +from veadk.tracing.base_tracer import BaseTracer |
| 24 | +from veadk.utils.logger import get_logger |
| 25 | +from veadk.utils.patches import patch_asyncio |
| 26 | + |
| 27 | +patch_asyncio() |
| 28 | +logger = get_logger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +class SequentialAgent(GoogleADKSequentialAgent): |
| 32 | + """LLM-based Agent with Volcengine capabilities.""" |
| 33 | + |
| 34 | + model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow") |
| 35 | + """The model config""" |
| 36 | + |
| 37 | + name: str = "veSequentialAgent" |
| 38 | + """The name of the agent.""" |
| 39 | + |
| 40 | + description: str = DEFAULT_DESCRIPTION |
| 41 | + """The description of the agent. This will be helpful in A2A scenario.""" |
| 42 | + |
| 43 | + instruction: str = DEFAULT_INSTRUCTION |
| 44 | + """The instruction for the agent, such as principles of function calling.""" |
| 45 | + |
| 46 | + sub_agents: list[BaseAgent] = Field(default_factory=list, exclude=True) |
| 47 | + """The sub agents provided to agent.""" |
| 48 | + |
| 49 | + tracers: list[BaseTracer] = [] |
| 50 | + """The tracers provided to agent.""" |
| 51 | + |
| 52 | + def model_post_init(self, __context: Any) -> None: |
| 53 | + super().model_post_init(None) # for sub_agents init |
| 54 | + |
| 55 | + if self.tracers: |
| 56 | + for tracer in self.tracers: |
| 57 | + for sub_agent in self.sub_agents: |
| 58 | + try: |
| 59 | + tracer.do_hooks(sub_agent) |
| 60 | + except Exception as e: |
| 61 | + logger.warning( |
| 62 | + f"Failed to add hooks for sub_agent `{sub_agent.name}`: {e}" |
| 63 | + ) |
| 64 | + |
| 65 | + logger.info(f"{self.__class__.__name__} `{self.name}` init done.") |
0 commit comments