autobyteus 1.1.9__py3-none-any.whl → 1.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- autobyteus/agent/context/agent_runtime_state.py +4 -0
- autobyteus/agent/events/notifiers.py +5 -1
- autobyteus/agent/message/send_message_to.py +5 -4
- autobyteus/agent/streaming/agent_event_stream.py +5 -0
- autobyteus/agent/streaming/stream_event_payloads.py +25 -0
- autobyteus/agent/streaming/stream_events.py +13 -1
- autobyteus/agent_team/bootstrap_steps/task_notifier_initialization_step.py +4 -4
- autobyteus/agent_team/bootstrap_steps/team_context_initialization_step.py +12 -12
- autobyteus/agent_team/context/agent_team_runtime_state.py +2 -2
- autobyteus/agent_team/streaming/agent_team_event_notifier.py +4 -4
- autobyteus/agent_team/streaming/agent_team_stream_event_payloads.py +3 -3
- autobyteus/agent_team/streaming/agent_team_stream_events.py +8 -8
- autobyteus/agent_team/task_notification/activation_policy.py +1 -1
- autobyteus/agent_team/task_notification/system_event_driven_agent_task_notifier.py +22 -22
- autobyteus/agent_team/task_notification/task_notification_mode.py +1 -1
- autobyteus/cli/agent_team_tui/app.py +4 -4
- autobyteus/cli/agent_team_tui/state.py +8 -8
- autobyteus/cli/agent_team_tui/widgets/focus_pane.py +3 -3
- autobyteus/cli/agent_team_tui/widgets/shared.py +1 -1
- autobyteus/cli/agent_team_tui/widgets/{task_board_panel.py → task_plan_panel.py} +5 -5
- autobyteus/clients/__init__.py +10 -0
- autobyteus/clients/autobyteus_client.py +318 -0
- autobyteus/clients/cert_utils.py +105 -0
- autobyteus/clients/certificates/cert.pem +34 -0
- autobyteus/events/event_types.py +4 -3
- autobyteus/llm/api/autobyteus_llm.py +1 -1
- autobyteus/llm/api/zhipu_llm.py +26 -0
- autobyteus/llm/autobyteus_provider.py +1 -1
- autobyteus/llm/llm_factory.py +23 -0
- autobyteus/llm/ollama_provider_resolver.py +1 -0
- autobyteus/llm/providers.py +1 -0
- autobyteus/llm/token_counter/token_counter_factory.py +3 -0
- autobyteus/llm/token_counter/zhipu_token_counter.py +24 -0
- autobyteus/multimedia/audio/api/__init__.py +3 -2
- autobyteus/multimedia/audio/api/autobyteus_audio_client.py +1 -1
- autobyteus/multimedia/audio/api/openai_audio_client.py +112 -0
- autobyteus/multimedia/audio/audio_client_factory.py +37 -0
- autobyteus/multimedia/audio/autobyteus_audio_provider.py +1 -1
- autobyteus/multimedia/image/api/autobyteus_image_client.py +1 -1
- autobyteus/multimedia/image/autobyteus_image_provider.py +1 -1
- autobyteus/multimedia/image/image_client_factory.py +1 -1
- autobyteus/task_management/__init__.py +44 -20
- autobyteus/task_management/{base_task_board.py → base_task_plan.py} +16 -13
- autobyteus/task_management/converters/__init__.py +2 -2
- autobyteus/task_management/converters/{task_board_converter.py → task_plan_converter.py} +13 -13
- autobyteus/task_management/events.py +7 -7
- autobyteus/task_management/{in_memory_task_board.py → in_memory_task_plan.py} +34 -22
- autobyteus/task_management/schemas/__init__.py +3 -0
- autobyteus/task_management/schemas/task_definition.py +1 -1
- autobyteus/task_management/schemas/task_status_report.py +3 -3
- autobyteus/task_management/schemas/todo_definition.py +15 -0
- autobyteus/task_management/todo.py +29 -0
- autobyteus/task_management/todo_list.py +75 -0
- autobyteus/task_management/tools/__init__.py +25 -7
- autobyteus/task_management/tools/task_tools/__init__.py +19 -0
- autobyteus/task_management/tools/task_tools/assign_task_to.py +125 -0
- autobyteus/task_management/tools/{publish_task.py → task_tools/create_task.py} +16 -18
- autobyteus/task_management/tools/{publish_tasks.py → task_tools/create_tasks.py} +19 -19
- autobyteus/task_management/tools/{get_my_tasks.py → task_tools/get_my_tasks.py} +15 -15
- autobyteus/task_management/tools/{get_task_board_status.py → task_tools/get_task_plan_status.py} +16 -16
- autobyteus/task_management/tools/{update_task_status.py → task_tools/update_task_status.py} +16 -16
- autobyteus/task_management/tools/todo_tools/__init__.py +18 -0
- autobyteus/task_management/tools/todo_tools/add_todo.py +78 -0
- autobyteus/task_management/tools/todo_tools/create_todo_list.py +79 -0
- autobyteus/task_management/tools/todo_tools/get_todo_list.py +55 -0
- autobyteus/task_management/tools/todo_tools/update_todo_status.py +85 -0
- autobyteus/tools/__init__.py +61 -21
- autobyteus/tools/bash/bash_executor.py +3 -3
- autobyteus/tools/browser/session_aware/browser_session_aware_navigate_to.py +5 -5
- autobyteus/tools/browser/session_aware/browser_session_aware_web_element_trigger.py +4 -4
- autobyteus/tools/browser/session_aware/browser_session_aware_webpage_reader.py +3 -3
- autobyteus/tools/browser/session_aware/browser_session_aware_webpage_screenshot_taker.py +3 -3
- autobyteus/tools/browser/standalone/navigate_to.py +13 -9
- autobyteus/tools/browser/standalone/web_page_pdf_generator.py +9 -5
- autobyteus/tools/browser/standalone/webpage_image_downloader.py +10 -6
- autobyteus/tools/browser/standalone/webpage_reader.py +13 -9
- autobyteus/tools/browser/standalone/webpage_screenshot_taker.py +9 -5
- autobyteus/tools/file/__init__.py +13 -0
- autobyteus/tools/file/edit_file.py +200 -0
- autobyteus/tools/file/list_directory.py +168 -0
- autobyteus/tools/file/{file_reader.py → read_file.py} +3 -3
- autobyteus/tools/file/search_files.py +188 -0
- autobyteus/tools/file/{file_writer.py → write_file.py} +3 -3
- autobyteus/tools/functional_tool.py +10 -8
- autobyteus/tools/mcp/tool.py +3 -3
- autobyteus/tools/mcp/tool_registrar.py +5 -2
- autobyteus/tools/multimedia/__init__.py +2 -1
- autobyteus/tools/multimedia/audio_tools.py +2 -2
- autobyteus/tools/multimedia/download_media_tool.py +136 -0
- autobyteus/tools/multimedia/image_tools.py +4 -4
- autobyteus/tools/multimedia/media_reader_tool.py +1 -1
- autobyteus/tools/registry/tool_definition.py +66 -13
- autobyteus/tools/registry/tool_registry.py +29 -0
- autobyteus/tools/search/__init__.py +17 -0
- autobyteus/tools/search/base_strategy.py +35 -0
- autobyteus/tools/search/client.py +24 -0
- autobyteus/tools/search/factory.py +81 -0
- autobyteus/tools/search/google_cse_strategy.py +68 -0
- autobyteus/tools/search/providers.py +10 -0
- autobyteus/tools/search/serpapi_strategy.py +65 -0
- autobyteus/tools/search/serper_strategy.py +87 -0
- autobyteus/tools/search_tool.py +83 -0
- autobyteus/tools/timer.py +4 -0
- autobyteus/tools/tool_meta.py +4 -24
- autobyteus/tools/usage/parsers/_string_decoders.py +18 -0
- autobyteus/tools/usage/parsers/default_json_tool_usage_parser.py +9 -1
- autobyteus/tools/usage/parsers/default_xml_tool_usage_parser.py +15 -1
- autobyteus/tools/usage/parsers/gemini_json_tool_usage_parser.py +4 -1
- autobyteus/tools/usage/parsers/openai_json_tool_usage_parser.py +4 -1
- autobyteus/workflow/bootstrap_steps/coordinator_prompt_preparation_step.py +1 -2
- {autobyteus-1.1.9.dist-info → autobyteus-1.2.1.dist-info}/METADATA +7 -6
- {autobyteus-1.1.9.dist-info → autobyteus-1.2.1.dist-info}/RECORD +117 -94
- examples/run_agentic_software_engineer.py +239 -0
- examples/run_poem_writer.py +3 -3
- autobyteus/person/__init__.py +0 -0
- autobyteus/person/examples/__init__.py +0 -0
- autobyteus/person/examples/sample_persons.py +0 -14
- autobyteus/person/examples/sample_roles.py +0 -14
- autobyteus/person/person.py +0 -29
- autobyteus/person/role.py +0 -14
- autobyteus/tools/google_search.py +0 -149
- autobyteus/tools/image_downloader.py +0 -99
- autobyteus/tools/pdf_downloader.py +0 -89
- {autobyteus-1.1.9.dist-info → autobyteus-1.2.1.dist-info}/WHEEL +0 -0
- {autobyteus-1.1.9.dist-info → autobyteus-1.2.1.dist-info}/licenses/LICENSE +0 -0
- {autobyteus-1.1.9.dist-info → autobyteus-1.2.1.dist-info}/top_level.txt +0 -0
|
@@ -20,18 +20,22 @@ if TYPE_CHECKING:
|
|
|
20
20
|
|
|
21
21
|
logger = logging.getLogger(__name__)
|
|
22
22
|
|
|
23
|
+
# A sentinel object to differentiate between a cached value of None and an unset cache.
|
|
24
|
+
_CACHE_NOT_SET = object()
|
|
25
|
+
|
|
23
26
|
class ToolDefinition:
|
|
24
27
|
"""
|
|
25
28
|
Represents the definition of a tool, containing its metadata and the means
|
|
26
29
|
to create an instance. It can generate provider-agnostic usage information on demand.
|
|
30
|
+
This class now supports dynamic schema generation with caching.
|
|
27
31
|
"""
|
|
28
32
|
def __init__(self,
|
|
29
33
|
name: str,
|
|
30
34
|
description: str,
|
|
31
|
-
argument_schema: Optional['ParameterSchema'],
|
|
32
35
|
origin: ToolOrigin,
|
|
33
36
|
category: str,
|
|
34
|
-
|
|
37
|
+
argument_schema_provider: Callable[[], Optional['ParameterSchema']],
|
|
38
|
+
config_schema_provider: Callable[[], Optional['ParameterSchema']],
|
|
35
39
|
tool_class: Optional[Type['BaseTool']] = None,
|
|
36
40
|
custom_factory: Optional[Callable[['ToolConfig'], 'BaseTool']] = None,
|
|
37
41
|
metadata: Optional[Dict[str, Any]] = None):
|
|
@@ -53,10 +57,10 @@ class ToolDefinition:
|
|
|
53
57
|
if custom_factory and not callable(custom_factory):
|
|
54
58
|
raise TypeError(f"ToolDefinition '{name}' requires a callable for 'custom_factory'.")
|
|
55
59
|
|
|
56
|
-
if
|
|
57
|
-
raise TypeError(f"ToolDefinition '{name}'
|
|
58
|
-
if
|
|
59
|
-
raise TypeError(f"ToolDefinition '{name}'
|
|
60
|
+
if not callable(argument_schema_provider):
|
|
61
|
+
raise TypeError(f"ToolDefinition '{name}' requires a callable for 'argument_schema_provider'.")
|
|
62
|
+
if not callable(config_schema_provider):
|
|
63
|
+
raise TypeError(f"ToolDefinition '{name}' requires a callable for 'config_schema_provider'.")
|
|
60
64
|
if not isinstance(origin, ToolOrigin):
|
|
61
65
|
raise TypeError(f"ToolDefinition '{name}' requires a ToolOrigin for 'origin'. Got {type(origin)}")
|
|
62
66
|
|
|
@@ -66,13 +70,17 @@ class ToolDefinition:
|
|
|
66
70
|
|
|
67
71
|
self._name = name
|
|
68
72
|
self._description = description
|
|
69
|
-
self._argument_schema: Optional['ParameterSchema'] = argument_schema
|
|
70
|
-
self._config_schema: Optional['ParameterSchema'] = config_schema
|
|
71
73
|
self._tool_class = tool_class
|
|
72
74
|
self._custom_factory = custom_factory
|
|
73
75
|
self._origin = origin
|
|
74
76
|
self._category = category
|
|
75
77
|
self._metadata = metadata or {}
|
|
78
|
+
|
|
79
|
+
# Store schema providers and initialize caches
|
|
80
|
+
self._argument_schema_provider = argument_schema_provider
|
|
81
|
+
self._config_schema_provider = config_schema_provider
|
|
82
|
+
self._cached_argument_schema: Any = _CACHE_NOT_SET
|
|
83
|
+
self._cached_config_schema: Any = _CACHE_NOT_SET
|
|
76
84
|
|
|
77
85
|
logger.debug(f"ToolDefinition created for tool '{self.name}'.")
|
|
78
86
|
|
|
@@ -85,16 +93,58 @@ class ToolDefinition:
|
|
|
85
93
|
def tool_class(self) -> Optional[Type['BaseTool']]: return self._tool_class
|
|
86
94
|
@property
|
|
87
95
|
def custom_factory(self) -> Optional[Callable[['ToolConfig'], 'BaseTool']]: return self._custom_factory
|
|
96
|
+
|
|
88
97
|
@property
|
|
89
|
-
def argument_schema(self) -> Optional['ParameterSchema']:
|
|
98
|
+
def argument_schema(self) -> Optional['ParameterSchema']:
|
|
99
|
+
"""On-demand schema generation and caching for argument_schema."""
|
|
100
|
+
if self._cached_argument_schema is _CACHE_NOT_SET:
|
|
101
|
+
logger.debug(f"Cache miss for argument_schema of tool '{self.name}'. Generating...")
|
|
102
|
+
try:
|
|
103
|
+
self._cached_argument_schema = self._argument_schema_provider()
|
|
104
|
+
except Exception as e:
|
|
105
|
+
logger.warning(
|
|
106
|
+
f"Failed to generate argument schema for tool '{self.name}' due to an error. "
|
|
107
|
+
f"The tool will have no arguments. Error: {e}",
|
|
108
|
+
exc_info=True
|
|
109
|
+
)
|
|
110
|
+
self._cached_argument_schema = None
|
|
111
|
+
return self._cached_argument_schema
|
|
112
|
+
|
|
90
113
|
@property
|
|
91
|
-
def config_schema(self) -> Optional['ParameterSchema']:
|
|
114
|
+
def config_schema(self) -> Optional['ParameterSchema']:
|
|
115
|
+
"""On-demand schema generation and caching for config_schema."""
|
|
116
|
+
if self._cached_config_schema is _CACHE_NOT_SET:
|
|
117
|
+
logger.debug(f"Cache miss for config_schema of tool '{self.name}'. Generating...")
|
|
118
|
+
try:
|
|
119
|
+
self._cached_config_schema = self._config_schema_provider()
|
|
120
|
+
except Exception as e:
|
|
121
|
+
logger.warning(
|
|
122
|
+
f"Failed to generate config schema for tool '{self.name}' due to an error. "
|
|
123
|
+
f"The tool will have no config. Error: {e}",
|
|
124
|
+
exc_info=True
|
|
125
|
+
)
|
|
126
|
+
self._cached_config_schema = None
|
|
127
|
+
return self._cached_config_schema
|
|
128
|
+
|
|
92
129
|
@property
|
|
93
130
|
def origin(self) -> ToolOrigin: return self._origin
|
|
94
131
|
@property
|
|
95
132
|
def category(self) -> str: return self._category
|
|
96
133
|
@property
|
|
97
134
|
def metadata(self) -> Dict[str, Any]: return self._metadata
|
|
135
|
+
|
|
136
|
+
def reload_cached_schema(self) -> None:
|
|
137
|
+
"""
|
|
138
|
+
Actively re-generates the schemas from their providers and updates the cache.
|
|
139
|
+
This is an eager operation.
|
|
140
|
+
"""
|
|
141
|
+
logger.info(f"Eagerly reloading schema cache for tool '{self.name}'.")
|
|
142
|
+
self._cached_argument_schema = _CACHE_NOT_SET
|
|
143
|
+
self._cached_config_schema = _CACHE_NOT_SET
|
|
144
|
+
# The schemas will be regenerated on the next property access.
|
|
145
|
+
# To make it fully eager, we can trigger the access here.
|
|
146
|
+
_ = self.argument_schema
|
|
147
|
+
_ = self.config_schema
|
|
98
148
|
|
|
99
149
|
# --- Convenience Schema/Example Generation API (using default formatters) ---
|
|
100
150
|
def get_usage_xml(self, provider: Optional[LLMProvider] = None) -> str:
|
|
@@ -132,14 +182,17 @@ class ToolDefinition:
|
|
|
132
182
|
# --- Other methods ---
|
|
133
183
|
@property
|
|
134
184
|
def has_instantiation_config(self) -> bool:
|
|
135
|
-
|
|
185
|
+
# Use the property to access the schema
|
|
186
|
+
return self.config_schema is not None and len(self.config_schema) > 0
|
|
136
187
|
|
|
137
188
|
def validate_instantiation_config(self, config_data: Dict[str, Any]) -> tuple[bool, TypingList[str]]:
|
|
138
|
-
|
|
189
|
+
# Use the property to access the schema
|
|
190
|
+
schema = self.config_schema
|
|
191
|
+
if not schema:
|
|
139
192
|
if config_data:
|
|
140
193
|
return False, [f"Tool '{self.name}' does not accept instantiation configuration parameters"]
|
|
141
194
|
return True, []
|
|
142
|
-
return
|
|
195
|
+
return schema.validate_config(config_data)
|
|
143
196
|
|
|
144
197
|
def __repr__(self) -> str:
|
|
145
198
|
creator_repr = f"class='{self._tool_class.__name__}'" if self._tool_class else "factory=True"
|
|
@@ -63,6 +63,35 @@ class ToolRegistry(metaclass=SingletonMeta):
|
|
|
63
63
|
logger.warning(f"Attempted to unregister tool '{name}', but it was not found in the registry.")
|
|
64
64
|
return False
|
|
65
65
|
|
|
66
|
+
def reload_tool_schema(self, name: str) -> bool:
|
|
67
|
+
"""
|
|
68
|
+
Actively reloads the schema for a specific tool by calling its schema provider.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
name: The unique name of the tool to reload the schema for.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
True if the tool was found and its schema was reloaded, False otherwise.
|
|
75
|
+
"""
|
|
76
|
+
definition = self.get_tool_definition(name)
|
|
77
|
+
if definition:
|
|
78
|
+
definition.reload_cached_schema()
|
|
79
|
+
return True
|
|
80
|
+
else:
|
|
81
|
+
logger.warning(f"Attempted to reload schema for tool '{name}', but it was not found in the registry.")
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
def reload_all_tool_schemas(self) -> None:
|
|
85
|
+
"""
|
|
86
|
+
Actively reloads the schemas for all registered tools.
|
|
87
|
+
"""
|
|
88
|
+
logger.info("Eagerly reloading schemas for all registered tools...")
|
|
89
|
+
count = 0
|
|
90
|
+
for definition in self._definitions.values():
|
|
91
|
+
definition.reload_cached_schema()
|
|
92
|
+
count += 1
|
|
93
|
+
logger.info(f"Schemas for {count} tool(s) have been reloaded.")
|
|
94
|
+
|
|
66
95
|
def get_tool_definition(self, name: str) -> Optional[ToolDefinition]:
|
|
67
96
|
"""
|
|
68
97
|
Retrieves the definition for a specific tool name.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from .providers import SearchProvider
|
|
2
|
+
from .base_strategy import SearchStrategy
|
|
3
|
+
from .serper_strategy import SerperSearchStrategy
|
|
4
|
+
from .serpapi_strategy import SerpApiSearchStrategy
|
|
5
|
+
from .google_cse_strategy import GoogleCSESearchStrategy
|
|
6
|
+
from .client import SearchClient
|
|
7
|
+
from .factory import SearchClientFactory
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"SearchProvider",
|
|
11
|
+
"SearchStrategy",
|
|
12
|
+
"SerperSearchStrategy",
|
|
13
|
+
"SerpApiSearchStrategy",
|
|
14
|
+
"GoogleCSESearchStrategy",
|
|
15
|
+
"SearchClient",
|
|
16
|
+
"SearchClientFactory",
|
|
17
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import Dict, Any
|
|
3
|
+
|
|
4
|
+
class SearchStrategy(ABC):
|
|
5
|
+
"""
|
|
6
|
+
Abstract base class for a search provider strategy.
|
|
7
|
+
Defines the common interface for performing a search and formatting results.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
@abstractmethod
|
|
11
|
+
async def search(self, query: str, num_results: int) -> str:
|
|
12
|
+
"""
|
|
13
|
+
Executes a search query against a specific provider.
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
query: The search query string.
|
|
17
|
+
num_results: The desired number of organic search results.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
A formatted string summarizing the search results, suitable for an LLM.
|
|
21
|
+
"""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def _format_results(self, data: Dict[str, Any]) -> str:
|
|
26
|
+
"""
|
|
27
|
+
Formats the raw JSON response from the search API into a clean string.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
data: The JSON data from the API response.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
A formatted summary string.
|
|
34
|
+
"""
|
|
35
|
+
pass
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from .base_strategy import SearchStrategy
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
class SearchClient:
|
|
10
|
+
"""
|
|
11
|
+
A client that uses a configured search strategy to perform searches.
|
|
12
|
+
This acts as the 'Context' in the Strategy pattern.
|
|
13
|
+
"""
|
|
14
|
+
def __init__(self, strategy: 'SearchStrategy'):
|
|
15
|
+
if not strategy:
|
|
16
|
+
raise ValueError("SearchClient must be initialized with a valid SearchStrategy.")
|
|
17
|
+
self._strategy = strategy
|
|
18
|
+
logger.debug(f"SearchClient initialized with strategy: {type(strategy).__name__}")
|
|
19
|
+
|
|
20
|
+
async def search(self, query: str, num_results: int) -> str:
|
|
21
|
+
"""
|
|
22
|
+
Delegates the search operation to the configured strategy.
|
|
23
|
+
"""
|
|
24
|
+
return await self._strategy.search(query, num_results)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import logging
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from autobyteus.utils.singleton import SingletonMeta
|
|
6
|
+
from .providers import SearchProvider
|
|
7
|
+
from .client import SearchClient
|
|
8
|
+
from .serper_strategy import SerperSearchStrategy
|
|
9
|
+
from .serpapi_strategy import SerpApiSearchStrategy
|
|
10
|
+
from .google_cse_strategy import GoogleCSESearchStrategy
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
class SearchClientFactory(metaclass=SingletonMeta):
|
|
15
|
+
"""
|
|
16
|
+
Factory for creating a SearchClient with the appropriate strategy
|
|
17
|
+
based on environment variable configuration.
|
|
18
|
+
"""
|
|
19
|
+
_instance: Optional[SearchClient] = None
|
|
20
|
+
|
|
21
|
+
def create_search_client(self) -> SearchClient:
|
|
22
|
+
"""
|
|
23
|
+
Creates and returns a singleton instance of the SearchClient, configured
|
|
24
|
+
with the appropriate search strategy.
|
|
25
|
+
"""
|
|
26
|
+
if self._instance:
|
|
27
|
+
return self._instance
|
|
28
|
+
|
|
29
|
+
provider_name = os.getenv("DEFAULT_SEARCH_PROVIDER", "").lower()
|
|
30
|
+
|
|
31
|
+
serper_key = os.getenv("SERPER_API_KEY")
|
|
32
|
+
serpapi_key = os.getenv("SERPAPI_API_KEY")
|
|
33
|
+
google_api_key = os.getenv("GOOGLE_CSE_API_KEY")
|
|
34
|
+
google_cse_id = os.getenv("GOOGLE_CSE_ID")
|
|
35
|
+
|
|
36
|
+
is_serper_configured = bool(serper_key)
|
|
37
|
+
is_serpapi_configured = bool(serpapi_key)
|
|
38
|
+
is_google_cse_configured = bool(google_api_key and google_cse_id)
|
|
39
|
+
|
|
40
|
+
strategy = None
|
|
41
|
+
|
|
42
|
+
if provider_name == SearchProvider.GOOGLE_CSE:
|
|
43
|
+
if is_google_cse_configured:
|
|
44
|
+
logger.info("DEFAULT_SEARCH_PROVIDER is 'google_cse', using Google CSE strategy.")
|
|
45
|
+
strategy = GoogleCSESearchStrategy()
|
|
46
|
+
else:
|
|
47
|
+
raise ValueError("DEFAULT_SEARCH_PROVIDER is 'google_cse', but Google CSE is not configured. "
|
|
48
|
+
"Set GOOGLE_CSE_API_KEY and GOOGLE_CSE_ID.")
|
|
49
|
+
|
|
50
|
+
elif provider_name == SearchProvider.SERPAPI:
|
|
51
|
+
if is_serpapi_configured:
|
|
52
|
+
logger.info("DEFAULT_SEARCH_PROVIDER is 'serpapi', using SerpApi strategy.")
|
|
53
|
+
strategy = SerpApiSearchStrategy()
|
|
54
|
+
else:
|
|
55
|
+
raise ValueError("DEFAULT_SEARCH_PROVIDER is 'serpapi', but SerpApi is not configured. "
|
|
56
|
+
"Set SERPAPI_API_KEY.")
|
|
57
|
+
|
|
58
|
+
# Default to Serper if explicitly set, or if not set and Serper is available.
|
|
59
|
+
# This handles the case where multiple providers are configured but no provider is specified.
|
|
60
|
+
elif provider_name == SearchProvider.SERPER or is_serper_configured:
|
|
61
|
+
if is_serper_configured:
|
|
62
|
+
logger.info("Using Serper search strategy (either as default or as first fallback).")
|
|
63
|
+
strategy = SerperSearchStrategy()
|
|
64
|
+
else:
|
|
65
|
+
# This branch is only taken if provider_name is 'serper' but it's not configured.
|
|
66
|
+
raise ValueError("DEFAULT_SEARCH_PROVIDER is 'serper', but Serper is not configured. Set SERPER_API_KEY.")
|
|
67
|
+
|
|
68
|
+
elif is_serpapi_configured:
|
|
69
|
+
logger.info("Serper not configured, falling back to available SerpApi strategy.")
|
|
70
|
+
strategy = SerpApiSearchStrategy()
|
|
71
|
+
|
|
72
|
+
elif is_google_cse_configured:
|
|
73
|
+
logger.info("Neither Serper nor SerpApi are configured, falling back to available Google CSE strategy.")
|
|
74
|
+
strategy = GoogleCSESearchStrategy()
|
|
75
|
+
|
|
76
|
+
else:
|
|
77
|
+
raise ValueError("No search provider is configured. Please set either SERPER_API_KEY, SERPAPI_API_KEY, "
|
|
78
|
+
"or both GOOGLE_CSE_API_KEY and GOOGLE_CSE_ID environment variables.")
|
|
79
|
+
|
|
80
|
+
self._instance = SearchClient(strategy=strategy)
|
|
81
|
+
return self._instance
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import logging
|
|
3
|
+
import aiohttp
|
|
4
|
+
from typing import Dict, Any, Optional
|
|
5
|
+
|
|
6
|
+
from .base_strategy import SearchStrategy
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
class GoogleCSESearchStrategy(SearchStrategy):
|
|
11
|
+
"""
|
|
12
|
+
A search strategy that uses the official Google Custom Search Engine (CSE) API.
|
|
13
|
+
"""
|
|
14
|
+
API_URL = "https://www.googleapis.com/customsearch/v1"
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
self.api_key: Optional[str] = os.getenv("GOOGLE_CSE_API_KEY")
|
|
18
|
+
self.cse_id: Optional[str] = os.getenv("GOOGLE_CSE_ID")
|
|
19
|
+
if not self.api_key or not self.cse_id:
|
|
20
|
+
raise ValueError(
|
|
21
|
+
"GoogleCSESearchStrategy requires both 'GOOGLE_CSE_API_KEY' and 'GOOGLE_CSE_ID' environment variables to be set."
|
|
22
|
+
)
|
|
23
|
+
logger.debug("GoogleCSESearchStrategy initialized.")
|
|
24
|
+
|
|
25
|
+
def _format_results(self, data: Dict[str, Any]) -> str:
|
|
26
|
+
"""Formats the JSON response from Google CSE API into a clean string for an LLM."""
|
|
27
|
+
if "items" not in data or not data["items"]:
|
|
28
|
+
return "No relevant information found for the query via Google CSE."
|
|
29
|
+
|
|
30
|
+
results = data["items"]
|
|
31
|
+
results_str = "\n".join(
|
|
32
|
+
f"{i+1}. {result.get('title', 'No Title')}\n"
|
|
33
|
+
f" Link: {result.get('link', 'No Link')}\n"
|
|
34
|
+
f" Snippet: {result.get('snippet', 'No Snippet')}"
|
|
35
|
+
for i, result in enumerate(results)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
return f"Search Results:\n{results_str}"
|
|
39
|
+
|
|
40
|
+
async def search(self, query: str, num_results: int) -> str:
|
|
41
|
+
logger.info(f"Executing search with Google CSE strategy for query: '{query}'")
|
|
42
|
+
|
|
43
|
+
params = {
|
|
44
|
+
'key': self.api_key,
|
|
45
|
+
'cx': self.cse_id,
|
|
46
|
+
'q': query,
|
|
47
|
+
'num': num_results
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try:
|
|
51
|
+
async with aiohttp.ClientSession() as session:
|
|
52
|
+
async with session.get(self.API_URL, params=params) as response:
|
|
53
|
+
if response.status == 200:
|
|
54
|
+
data = await response.json()
|
|
55
|
+
return self._format_results(data)
|
|
56
|
+
else:
|
|
57
|
+
error_text = await response.text()
|
|
58
|
+
logger.error(
|
|
59
|
+
f"Google CSE API returned a non-200 status code: {response.status}. "
|
|
60
|
+
f"Response: {error_text}"
|
|
61
|
+
)
|
|
62
|
+
raise RuntimeError(f"Google CSE API request failed with status {response.status}: {error_text}")
|
|
63
|
+
except aiohttp.ClientError as e:
|
|
64
|
+
logger.error(f"Network error during Google CSE API call: {e}", exc_info=True)
|
|
65
|
+
raise RuntimeError(f"A network error occurred during Google CSE search: {e}")
|
|
66
|
+
except Exception as e:
|
|
67
|
+
logger.error(f"An unexpected error occurred in Google CSE strategy: {e}", exc_info=True)
|
|
68
|
+
raise
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import logging
|
|
3
|
+
import aiohttp
|
|
4
|
+
from typing import Dict, Any, Optional
|
|
5
|
+
|
|
6
|
+
from .base_strategy import SearchStrategy
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
class SerpApiSearchStrategy(SearchStrategy):
|
|
11
|
+
"""
|
|
12
|
+
A search strategy that uses the SerpApi.com API.
|
|
13
|
+
"""
|
|
14
|
+
API_URL = "https://serpapi.com/search.json"
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
self.api_key: Optional[str] = os.getenv("SERPAPI_API_KEY")
|
|
18
|
+
if not self.api_key:
|
|
19
|
+
raise ValueError("SerpApiSearchStrategy requires the 'SERPAPI_API_KEY' environment variable to be set.")
|
|
20
|
+
logger.debug("SerpApiSearchStrategy initialized.")
|
|
21
|
+
|
|
22
|
+
def _format_results(self, data: Dict[str, Any]) -> str:
|
|
23
|
+
"""Formats the JSON response from SerpApi into a clean string for an LLM."""
|
|
24
|
+
if "organic_results" not in data or not data["organic_results"]:
|
|
25
|
+
return "No relevant information found for the query via SerpApi."
|
|
26
|
+
|
|
27
|
+
results = data["organic_results"]
|
|
28
|
+
results_str = "\n".join(
|
|
29
|
+
f"{i+1}. {result.get('title', 'No Title')}\n"
|
|
30
|
+
f" Link: {result.get('link', 'No Link')}\n"
|
|
31
|
+
f" Snippet: {result.get('snippet', 'No Snippet')}"
|
|
32
|
+
for i, result in enumerate(results)
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
return f"Search Results:\n{results_str}"
|
|
36
|
+
|
|
37
|
+
async def search(self, query: str, num_results: int) -> str:
|
|
38
|
+
logger.info(f"Executing search with SerpApi strategy for query: '{query}'")
|
|
39
|
+
|
|
40
|
+
params = {
|
|
41
|
+
'api_key': self.api_key,
|
|
42
|
+
'engine': 'google',
|
|
43
|
+
'q': query,
|
|
44
|
+
'num': num_results
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
async with aiohttp.ClientSession() as session:
|
|
49
|
+
async with session.get(self.API_URL, params=params) as response:
|
|
50
|
+
if response.status == 200:
|
|
51
|
+
data = await response.json()
|
|
52
|
+
return self._format_results(data)
|
|
53
|
+
else:
|
|
54
|
+
error_text = await response.text()
|
|
55
|
+
logger.error(
|
|
56
|
+
f"SerpApi API returned a non-200 status code: {response.status}. "
|
|
57
|
+
f"Response: {error_text}"
|
|
58
|
+
)
|
|
59
|
+
raise RuntimeError(f"SerpApi API request failed with status {response.status}: {error_text}")
|
|
60
|
+
except aiohttp.ClientError as e:
|
|
61
|
+
logger.error(f"Network error during SerpApi API call: {e}", exc_info=True)
|
|
62
|
+
raise RuntimeError(f"A network error occurred during SerpApi search: {e}")
|
|
63
|
+
except Exception as e:
|
|
64
|
+
logger.error(f"An unexpected error occurred in SerpApi strategy: {e}", exc_info=True)
|
|
65
|
+
raise
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
import aiohttp
|
|
5
|
+
from typing import Dict, Any, Optional
|
|
6
|
+
|
|
7
|
+
from .base_strategy import SearchStrategy
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
class SerperSearchStrategy(SearchStrategy):
|
|
12
|
+
"""
|
|
13
|
+
A search strategy that uses the Serper.dev API.
|
|
14
|
+
"""
|
|
15
|
+
API_URL = "https://google.serper.dev/search"
|
|
16
|
+
|
|
17
|
+
def __init__(self):
|
|
18
|
+
self.api_key: Optional[str] = os.getenv("SERPER_API_KEY")
|
|
19
|
+
if not self.api_key:
|
|
20
|
+
raise ValueError("SerperSearchStrategy requires the 'SERPER_API_KEY' environment variable to be set.")
|
|
21
|
+
logger.debug("SerperSearchStrategy initialized.")
|
|
22
|
+
|
|
23
|
+
def _format_results(self, data: Dict[str, Any]) -> str:
|
|
24
|
+
"""Formats the JSON response from Serper into a clean string for an LLM."""
|
|
25
|
+
summary_parts = []
|
|
26
|
+
|
|
27
|
+
# 1. Answer Box (most important for direct questions)
|
|
28
|
+
if "answerBox" in data:
|
|
29
|
+
answer_box = data["answerBox"]
|
|
30
|
+
title = answer_box.get("title", "")
|
|
31
|
+
snippet = answer_box.get("snippet") or answer_box.get("answer")
|
|
32
|
+
summary_parts.append(f"Direct Answer for '{title}':\n{snippet}")
|
|
33
|
+
|
|
34
|
+
# 2. Knowledge Graph (for entity information)
|
|
35
|
+
if "knowledgeGraph" in data:
|
|
36
|
+
kg = data["knowledgeGraph"]
|
|
37
|
+
title = kg.get("title", "")
|
|
38
|
+
description = kg.get("description")
|
|
39
|
+
summary_parts.append(f"Summary for '{title}':\n{description}")
|
|
40
|
+
|
|
41
|
+
# 3. Organic Results (the main search links)
|
|
42
|
+
if "organic" in data and data["organic"]:
|
|
43
|
+
organic_results = data["organic"]
|
|
44
|
+
results_str = "\n".join(
|
|
45
|
+
f"{i+1}. {result.get('title', 'No Title')}\n"
|
|
46
|
+
f" Link: {result.get('link', 'No Link')}\n"
|
|
47
|
+
f" Snippet: {result.get('snippet', 'No Snippet')}"
|
|
48
|
+
for i, result in enumerate(organic_results)
|
|
49
|
+
)
|
|
50
|
+
summary_parts.append(f"Search Results:\n{results_str}")
|
|
51
|
+
|
|
52
|
+
if not summary_parts:
|
|
53
|
+
return "No relevant information found for the query via Serper."
|
|
54
|
+
|
|
55
|
+
return "\n\n---\n\n".join(summary_parts)
|
|
56
|
+
|
|
57
|
+
async def search(self, query: str, num_results: int) -> str:
|
|
58
|
+
logger.info(f"Executing search with Serper strategy for query: '{query}'")
|
|
59
|
+
|
|
60
|
+
headers = {
|
|
61
|
+
'X-API-KEY': self.api_key,
|
|
62
|
+
'Content-Type': 'application/json'
|
|
63
|
+
}
|
|
64
|
+
payload = json.dumps({
|
|
65
|
+
"q": query,
|
|
66
|
+
"num": num_results
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
async with aiohttp.ClientSession() as session:
|
|
71
|
+
async with session.post(self.API_URL, headers=headers, data=payload) as response:
|
|
72
|
+
if response.status == 200:
|
|
73
|
+
data = await response.json()
|
|
74
|
+
return self._format_results(data)
|
|
75
|
+
else:
|
|
76
|
+
error_text = await response.text()
|
|
77
|
+
logger.error(
|
|
78
|
+
f"Serper API returned a non-200 status code: {response.status}. "
|
|
79
|
+
f"Response: {error_text}"
|
|
80
|
+
)
|
|
81
|
+
raise RuntimeError(f"Serper API request failed with status {response.status}: {error_text}")
|
|
82
|
+
except aiohttp.ClientError as e:
|
|
83
|
+
logger.error(f"Network error during Serper API call: {e}", exc_info=True)
|
|
84
|
+
raise RuntimeError(f"A network error occurred during Serper search: {e}")
|
|
85
|
+
except Exception as e:
|
|
86
|
+
logger.error(f"An unexpected error occurred in Serper strategy: {e}", exc_info=True)
|
|
87
|
+
raise
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Optional, TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
from autobyteus.tools.base_tool import BaseTool
|
|
5
|
+
from autobyteus.tools.tool_config import ToolConfig
|
|
6
|
+
from autobyteus.utils.parameter_schema import ParameterSchema, ParameterDefinition, ParameterType
|
|
7
|
+
from autobyteus.tools.tool_category import ToolCategory
|
|
8
|
+
from autobyteus.tools.search import SearchClientFactory, SearchClient
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from autobyteus.agent.context import AgentContext
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Search(BaseTool):
|
|
17
|
+
"""
|
|
18
|
+
Performs a web search using a configurable backend provider (e.g., Serper.dev, Google CSE).
|
|
19
|
+
Returns a structured summary of the results.
|
|
20
|
+
Configuration is managed via environment variables (see SearchClientFactory for details).
|
|
21
|
+
"""
|
|
22
|
+
CATEGORY = ToolCategory.WEB
|
|
23
|
+
|
|
24
|
+
def __init__(self, config: Optional[ToolConfig] = None):
|
|
25
|
+
super().__init__(config=config)
|
|
26
|
+
try:
|
|
27
|
+
factory = SearchClientFactory()
|
|
28
|
+
self.search_client: SearchClient = factory.create_search_client()
|
|
29
|
+
except ValueError as e:
|
|
30
|
+
logger.error(f"Failed to initialize search_web tool: {e}", exc_info=True)
|
|
31
|
+
# Re-raise to prevent tool from being used in a misconfigured state.
|
|
32
|
+
raise RuntimeError(
|
|
33
|
+
"Could not initialize Search tool. Please check your search provider configuration. "
|
|
34
|
+
f"Error: {e}"
|
|
35
|
+
)
|
|
36
|
+
logger.debug("search_web tool initialized with a configured search client.")
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def get_name(cls) -> str:
|
|
40
|
+
return "search_web"
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def get_description(cls) -> str:
|
|
44
|
+
return (
|
|
45
|
+
"Searches the web for a given query using the configured search provider. "
|
|
46
|
+
"Returns a concise, structured summary of search results, including direct answers (if available) and top organic links."
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
@classmethod
|
|
50
|
+
def get_argument_schema(cls) -> Optional[ParameterSchema]:
|
|
51
|
+
schema = ParameterSchema()
|
|
52
|
+
schema.add_parameter(ParameterDefinition(
|
|
53
|
+
name="query",
|
|
54
|
+
param_type=ParameterType.STRING,
|
|
55
|
+
description="The search query string.",
|
|
56
|
+
required=True
|
|
57
|
+
))
|
|
58
|
+
schema.add_parameter(ParameterDefinition(
|
|
59
|
+
name="num_results",
|
|
60
|
+
param_type=ParameterType.INTEGER,
|
|
61
|
+
description="The number of organic search results to return.",
|
|
62
|
+
required=False,
|
|
63
|
+
default_value=5,
|
|
64
|
+
min_value=1,
|
|
65
|
+
max_value=10
|
|
66
|
+
))
|
|
67
|
+
return schema
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def get_config_schema(cls) -> Optional[ParameterSchema]:
|
|
71
|
+
# Configuration is now handled by the factory via environment variables,
|
|
72
|
+
# so this tool no longer has instance-specific configuration.
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
async def _execute(self, context: 'AgentContext', query: str, num_results: int = 5) -> str:
|
|
76
|
+
logger.info(f"Executing search_web for agent {context.agent_id} with query: '{query}'")
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
return await self.search_client.search(query=query, num_results=num_results)
|
|
80
|
+
except Exception as e:
|
|
81
|
+
logger.error(f"An unexpected error occurred in search_web execution: {e}", exc_info=True)
|
|
82
|
+
# Re-raise to ensure the agent is aware of the failure.
|
|
83
|
+
raise
|
autobyteus/tools/timer.py
CHANGED
|
@@ -48,6 +48,10 @@ class Timer(BaseTool, EventEmitter):
|
|
|
48
48
|
self._task: Optional[asyncio.Task] = None
|
|
49
49
|
logger.debug(f"Timer initialized with duration: {self.duration}s, interval: {self.interval}s")
|
|
50
50
|
|
|
51
|
+
@classmethod
|
|
52
|
+
def get_name(cls) -> str:
|
|
53
|
+
return "start_timer"
|
|
54
|
+
|
|
51
55
|
@classmethod
|
|
52
56
|
def get_description(cls) -> str:
|
|
53
57
|
return "Sets and runs a timer. Emits TIMER_UPDATE events with remaining time at specified intervals."
|