ALag commited on
Commit
d4ea4e5
·
verified ·
1 Parent(s): 9a85672

Upload 10 files

Browse files
Files changed (10) hide show
  1. .gitignore +23 -0
  2. README.md +315 -12
  3. alitaDiagram.svg +1 -0
  4. app.py +266 -64
  5. app_modal.py +195 -0
  6. manager_agent.py +689 -0
  7. manager_agent2.py +663 -0
  8. requirements.txt +22 -1
  9. task_prompt.py +9 -0
  10. test_research.py +63 -0
.gitignore ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # System files
2
+ .DS_Store
3
+ .lprof
4
+
5
+ # Environment files
6
+ .env
7
+ .env.*
8
+
9
+ # Python cache files
10
+ __pycache__/
11
+ *.py[cod]
12
+ *$py.class
13
+ .pytest_cache/
14
+
15
+ # Virtual environments
16
+ .venv/
17
+ venv/
18
+ ENV/
19
+ env/
20
+
21
+ # Project specific
22
+ .alita_envs/
23
+ temp_downloads/
README.md CHANGED
@@ -1,12 +1,315 @@
1
- ---
2
- title: GALITA2
3
- emoji: 💬
4
- colorFrom: yellow
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.0.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio-hackathon : Generalist self-evolving ai agent inspired by Alita
2
+ This is my team project for the gradio hackathon 2025
3
+ This Project is inspired by research paper : `https://arxiv.org/abs/2505.20286`
4
+
5
+ # 📁 Structure du projet
6
+ ```bash
7
+ alita_agent/
8
+
9
+ ├── main.py # Point d'entrée principal : exécute un TaskPrompt via le ManagerAgent
10
+ ├── manager_agent.py # Logique de coordination centrale, il orchestre tous les composants
11
+ ├── task_prompt.py # Définit la classe TaskPrompt, contenant la requête utilisateur initiale
12
+
13
+ ├── components/ # Contient tous les composants fonctionnels modulaires
14
+ │ ├── __init__.py # Rends le dossier importable comme un package
15
+ │ ├── script_generator.py # Génère dynamiquement du code Python à partir d'un MCPToolSpec
16
+ │ ├── code_runner.py # Exécute un script dans un environnement isolé et capture le résultat
17
+ │ ├── mcp_registry.py # Gère l'enregistrement, la recherche et la réutilisation des outils MCP
18
+ │ ├── web_agent.py # Effectue des recherches web ou GitHub pour aider à la génération de code
19
+ │ └── mcp_brainstormer.py # Génère des MCPToolSpec en analysant la tâche utilisateur
20
+
21
+ ├── models/ # Contient les classes de données (dataclasses) utilisées dans tout le système
22
+ │ ├── __init__.py # Rends le dossier importable comme un package
23
+ │ ├── mcp_tool_spec.py # Définition de MCPToolSpec (dataclass) : nom, schémas I/O, description, pseudo-code, etc.
24
+ │ └── mcp_execution_result.py # Définition de MCPExecutionResult (dataclass) : succès, sortie, logs, erreur
25
+
26
+ ├── tests/ # Contient les tests unitaires pour chaque module
27
+ │ ├── __init__.py # Rends le dossier importable comme un package
28
+ │ ├── test_script_generator.py # Tests pour vérifier la génération correcte de code et d'environnements
29
+ │ ├── test_code_runner.py # Tests pour s'assurer de la bonne exécution des scripts et gestion d'erreurs
30
+ │ ├── test_mcp_registry.py # Tests de l'enregistrement, recherche et appel d'outils dans le registre MCP
31
+ │ └── test_manager_agent.py # Tests d'intégration sur le comportement global du ManagerAgent
32
+
33
+ └── README.md # Documentation du projet, instructions, pipeline, inspirations et lien vers le papier
34
+ ```
35
+
36
+ # Project Pipeline
37
+
38
+ #### 🔄 Le flux complet avec vérification de l'existence
39
+ 1. L'utilisateur envoie un TaskPrompt
40
+ 2. Le Manager Agent demande au MCPBrainstormer : "Quels outils faudrait-il pour résoudre cette tâche ?"
41
+ 3. Le Brainstormer propose une ou plusieurs specs (MCPToolSpec)
42
+ 4. Le Manager Agent consulte le MCPRegistry : "Ai-je déjà un outil enregistré dont le nom + I/O matchent cette spec ?"
43
+ - Oui ? ➜ réutilise l'outil existant
44
+ - Non ? ➜ il appel le web agent pour une recherche d'outils open-source pour implementer. Puis, le Manager prend la recherche et la donne a Brainstormer pour commencer la construction.
45
+
46
+ #### 🔍 Comment détecter que l'outil existe déjà ?
47
+ Par matching sur la spec MCPToolSpec :
48
+ - Nom exact (ou identifiant unique comme un hash)
49
+
50
+ - Ou plus intelligemment :
51
+ - même structure input_schema
52
+ - même output_schema
53
+ - mêmes rôles ou description proche (avec embedding / vector search)
54
+
55
+ ```python
56
+ def check_existing_tool(spec: MCPToolSpec, registry: MCPRegistry) -> Optional[str]:
57
+ for registered_spec in registry.list_tools():
58
+ if registered_spec.input_schema == spec.input_schema and \
59
+ registered_spec.output_schema == spec.output_schema:
60
+ return registry.get_tool_endpoint(registered_spec.name)
61
+ return None
62
+ ```
63
+
64
+ #### 💬 Que fait l'agent s'il le trouve ?
65
+ Il ne régénère rien :
66
+ - Il ajoute l'appel de l'outil MCP existant dans son plan
67
+ - Il formate l'entrée JSON
68
+ - Il appelle POST /predict directement
69
+ - Il utilise la réponse dans la suite de son raisonnement
70
+
71
+ #### 💡 Cas pratiques
72
+ Differents cas et Réaction attendue de l'agent
73
+
74
+ | Situation réelle | Réaction de l'agent |
75
+ | ----------------------------------------- | ------------------------------------------------------------------------ |
76
+ | L'outil `"SubtitleExtractor"` existe déjà | L'agent appelle directement l'endpoint |
77
+ | Le spec est proche mais pas identique | L'agent peut quand même le réutiliser (avec adaptation) |
78
+ | L'outil existe mais a échoué | L'agent peut **fallback** vers génération d'un nouvel outil MCP |
79
+ | L'outil existe mais est obsolète | Le Registry peut signaler une mise à jour ou déclencher une régénération |
80
+
81
+
82
+ #### Fonctions attendues
83
+
84
+ | Classe | Méthode attendue | Présente ? | Commentaire |
85
+ | -------------------- | ------------------------------------------ | ---------- | ----------- |
86
+ | `ManagerAgent` | `run_task(prompt)` | ✅ | OK |
87
+ | `MCPBrainstormer` | `brainstorm(prompt)` | ✅ | OK |
88
+ | `WebAgent` | `search_github`, `retrieve_readme` | ✅ | OK |
89
+ | `ScriptGenerator` | `generate_code`, `generate_env_script` | ✅ | OK |
90
+ | `CodeRunner` | `execute`, `setup_environment` | ✅ | OK |
91
+ | `MCPRegistry` | `register_tool`, `list_tools`, `call_tool` | ✅ | OK |
92
+ | `MCPExecutionResult` | attributs `success`, `output`, `logs` | ✅ | OK |
93
+ | `MCPToolSpec` | `name`, `input_schema`, etc. | ✅ | OK |
94
+
95
+ Ici Le ManagerAgent coordonne tout. Il délègue à :
96
+ - MCPBrainstormer → pour générer des specs d'outils.
97
+ - ScriptGenerator → pour générer du code.
98
+ - CodeRunner → pour tester le code.
99
+ - WebAgent → pour récupérer du contexte externe.
100
+ - MCPRegistry → pour enregistrer et réutiliser les outils.
101
+
102
+
103
+ ![](alitaDiagram.svg)
104
+
105
+
106
+ ```sh
107
+ plantuml -tsvg README.md
108
+ ```
109
+
110
+ <div hidden>
111
+ <details>
112
+ <summary>Voir le script PlantUML</summary>
113
+ ```plantuml
114
+ @startuml alitaDiagram
115
+
116
+ skinparam classAttributeIconSize 0
117
+
118
+ ' === Classes de données ===
119
+ class TaskPrompt {
120
+ - text: str
121
+ }
122
+
123
+ class MCPToolSpec {
124
+ - name: str
125
+ - input_schema: dict
126
+ - output_schema: dict
127
+ - description: str
128
+ - pseudo_code: str
129
+ - source_hint: str
130
+ }
131
+
132
+ class MCPExecutionResult {
133
+ - success: bool
134
+ - output: dict
135
+ - logs: str
136
+ - error_message: str
137
+ }
138
+
139
+ class ToolCall {
140
+ - tool_name: str
141
+ - input_data: dict
142
+ - result: dict
143
+ }
144
+
145
+
146
+ ' === Agents principaux ===
147
+ class ManagerAgent {
148
+ - brainstormer: MCPBrainstormer
149
+ - web_agent: WebAgent
150
+ - generator: ScriptGenerator
151
+ - runner: CodeRunner
152
+ - registry: MCPRegistry
153
+ + run_task(prompt: TaskPrompt): dict
154
+ + check_existing_tool(spec: MCPToolSpec) -> Optional[str]
155
+ }
156
+
157
+ class MCPBrainstormer {
158
+ + brainstorm(prompt: TaskPrompt): List<MCPToolSpec>
159
+ }
160
+
161
+ class WebAgent {
162
+ + search_github(query: str): str
163
+ + retrieve_readme(repo_url: str): str
164
+ }
165
+
166
+ class ScriptGenerator {
167
+ + generate_code(spec: MCPToolSpec): str
168
+ + generate_env_script(spec: MCPToolSpec): str
169
+ }
170
+
171
+ class CodeRunner {
172
+ + execute(script: str): MCPExecutionResult
173
+ + setup_environment(env_script: str): bool
174
+ }
175
+
176
+ class MCPRegistry {
177
+ + register_tool(spec: MCPToolSpec, endpoint_url: str): void
178
+ + list_tools(): List<MCPToolSpec>
179
+ + call_tool(tool: str): object
180
+ }
181
+
182
+
183
+
184
+ ' === Relations avec types + cardinalités ===
185
+
186
+ ' Le Manager reçoit une tâche utilisateur
187
+ TaskPrompt --> "1" ManagerAgent : provides query
188
+
189
+ ' Manager appelle le Brainstormer
190
+ ManagerAgent --> "1" MCPBrainstormer : calls
191
+
192
+ ' Manager utilise WebAgent
193
+ ManagerAgent "1" <--> "1" WebAgent : queries/answers
194
+
195
+ ' Brainstormer appelle ScriptGenerator et CodeRunner
196
+ MCPBrainstormer --> "1" ScriptGenerator : plans
197
+ MCPBrainstormer --> "1" CodeRunner : validates
198
+
199
+ ' Manager consulte ou enregistre dans le Registry
200
+ ManagerAgent --> "1" MCPRegistry : checks/updates
201
+
202
+ ' Manager construit un plan d'appel d'outils
203
+ ManagerAgent --> "0..*" ToolCall : creates
204
+
205
+ ' Brainstormer retourne des MCPToolSpec
206
+ MCPBrainstormer --> "1..*" MCPToolSpec : returns
207
+
208
+ ' ScriptGenerator utilise MCPToolSpec pour générer
209
+ ScriptGenerator --> "1" MCPToolSpec : consumes
210
+
211
+ ' Registry enregistre des ToolSpecs
212
+ MCPRegistry --> "0..*" MCPToolSpec : stores
213
+
214
+ ' CodeRunner renvoie un résultat d'exécution
215
+ CodeRunner --> "1" MCPExecutionResult : returns
216
+
217
+ ' CodeRunner peut utiliser des outils enregistrés
218
+ CodeRunner --> "0..*" MCPRegistry : queries
219
+
220
+
221
+ @enduml
222
+ ```
223
+ </details>
224
+ </div>
225
+
226
+ # ALITA Research Functionality
227
+
228
+ This README explains how to use the comprehensive research capabilities of the ALITA ManagerAgent.
229
+
230
+ ## Overview
231
+
232
+ ALITA can now perform deep, autonomous web research using the WebAgent's research functionality. This allows ALITA to gather information from multiple sources, analyze it, and synthesize a comprehensive report on any topic.
233
+
234
+ ## Usage Methods
235
+
236
+ There are two ways to use the research functionality:
237
+
238
+ ### 1. Direct Research Method
239
+
240
+ Call the `research` method directly on the ManagerAgent instance:
241
+
242
+ ```python
243
+ from manager_agent2 import ManagerAgent
244
+ from llama_index.llms.anthropic import Anthropic
245
+
246
+ # Initialize the LLM and ManagerAgent
247
+ llm = Anthropic(model="claude-3-5-sonnet-20241022", api_key="your-api-key")
248
+ manager = ManagerAgent(llm=llm)
249
+
250
+ # Perform research directly
251
+ report = manager.research(
252
+ query="What are the latest developments in quantum computing?",
253
+ max_iterations=50, # Optional: limit the number of research steps
254
+ verbose=True # Optional: show detailed progress
255
+ )
256
+
257
+ # The report variable now contains a comprehensive research report
258
+ print(report)
259
+ ```
260
+
261
+ ### 2. Tool-Based Research through ReActAgent
262
+
263
+ Let the ManagerAgent's internal ReActAgent decide when to use research:
264
+
265
+ ```python
266
+ from manager_agent2 import ManagerAgent
267
+ from models import TaskPrompt
268
+ from llama_index.llms.anthropic import Anthropic
269
+
270
+ # Initialize the LLM and ManagerAgent
271
+ llm = Anthropic(model="claude-3-5-sonnet-20241022", api_key="your-api-key")
272
+ manager = ManagerAgent(llm=llm)
273
+
274
+ # Create a task prompt
275
+ task_prompt = TaskPrompt(text="I need a comprehensive report on recent developments in quantum computing.")
276
+
277
+ # Run the task through the agent
278
+ response = manager.run_task(task_prompt)
279
+
280
+ # The response will include the research report if the agent determined research was needed
281
+ print(response)
282
+ ```
283
+
284
+ The agent will automatically detect when deep research is required based on keywords like "comprehensive," "thorough," "research," etc.
285
+
286
+ ## Running the Test Script
287
+
288
+ A test script is provided to demonstrate both usage methods:
289
+
290
+ ```bash
291
+ python test_research.py
292
+ ```
293
+
294
+ Make sure to set your Anthropic API key in the environment or in a `.env` file before running the script.
295
+
296
+ ## System Prompt Configuration
297
+
298
+ The ManagerAgent's system prompt has been updated to include guidance on when to use the research tool:
299
+
300
+ - For simple information needs: use 'web_search' for quick answers
301
+ - For complex research topics: use 'perform_web_research' for comprehensive autonomous research
302
+
303
+ ## How Research Works
304
+
305
+ When ALITA performs research:
306
+
307
+ 1. It first analyzes the research query to understand what information is needed
308
+ 2. It uses web search to gather relevant sources
309
+ 3. It visits and reads the content of each source
310
+ 4. It downloads and analyzes relevant documents if needed
311
+ 5. It evaluates the credibility and relevance of each source
312
+ 6. It synthesizes the information into a comprehensive report
313
+ 7. It includes citations and references to the sources used
314
+
315
+ This enables ALITA to provide high-quality, well-researched answers to complex questions.
alitaDiagram.svg ADDED
app.py CHANGED
@@ -1,64 +1,266 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import gradio as gr
4
+ import os
5
+ import traceback
6
+ import asyncio
7
+ from dotenv import load_dotenv
8
+ from models.task_prompt import TaskPrompt
9
+ import time
10
+ from llama_index.core import Settings as LlamaSettings # Import at top level
11
+ from llama_index.llms.anthropic import Anthropic # Import at top level
12
+ from manager_agent2 import ManagerAgent # Ensure this path is correct
13
+ import concurrent.futures # For running blocking code in a separate thread
14
+
15
+ # Load environment variables from .env file
16
+ load_dotenv()
17
+
18
+ # --- Configuration ---
19
+ LLM_MODEL = "claude-sonnet-4-20250514"
20
+
21
+ # --- Global variables ---
22
+ current_status = "Ready"
23
+ llm_global = None
24
+ manager_agent_global = None
25
+ # Settings_global is not strictly needed as a global if LlamaSettings is imported directly
26
+
27
+ # Thread pool executor for running blocking agent tasks
28
+ thread_pool_executor = concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count() or 1)
29
+
30
+
31
+ # --- LlamaIndex LLM Initialization ---
32
+ def initialize_components():
33
+ global llm_global, manager_agent_global
34
+
35
+ api_key = os.environ.get("ANTHROPIC_API_KEY")
36
+ if not api_key:
37
+ print("\n" + "="*60)
38
+ print("⚠️ ERROR: ANTHROPIC_API_KEY not found in environment variables!")
39
+ print("Please set your API key (e.g., in a .env file).")
40
+ print("="*60 + "\n")
41
+ return
42
+
43
+ try:
44
+ llm_global = Anthropic(
45
+ model=LLM_MODEL,
46
+ temperature=0.2,
47
+ max_tokens=4096
48
+ )
49
+ LlamaSettings.llm = llm_global # Use the imported LlamaSettings directly
50
+ print(f"Successfully initialized LlamaIndex with Anthropic model: {LLM_MODEL} (temperature=0.2)")
51
+
52
+ manager_agent_global = ManagerAgent(
53
+ llm_global,
54
+ max_iterations=30, # Keep this reasonable for testing
55
+ update_callback=update_status_callback
56
+ )
57
+ print(" ManagerAgent initialized successfully")
58
+
59
+ except Exception as e:
60
+ print(f"Error initializing Anthropic LLM or ManagerAgent: {e}")
61
+ traceback.print_exc()
62
+
63
+ # --- Update callback function (called by ManagerAgent) ---
64
+ def update_status_callback(message):
65
+ global current_status
66
+ # This function is called from the ManagerAgent's thread (potentially)
67
+ # or the ReAct agent's execution context.
68
+ # It needs to update the global variable, which the Gradio polling thread will pick up.
69
+ current_status = message
70
+ print(f"✅ UI_STATUS_UPDATE (via callback): {message}") # Differentiate console log
71
+
72
+ # --- Status retrieval function for Gradio polling ---
73
+ def get_current_status_for_ui():
74
+ global current_status
75
+ timestamp = time.time()
76
+ return f"{current_status}<span style='display:none;'>{timestamp}</span>"
77
+
78
+ # --- Gradio Interface Setup ---
79
+ def create_gradio_interface():
80
+ if "ANTHROPIC_API_KEY" not in os.environ:
81
+ gr.Warning("ANTHROPIC_API_KEY not found in environment variables! ALITA may not function correctly.")
82
+
83
+ with gr.Blocks(theme="soft") as demo:
84
+ gr.Markdown("# GALITA")
85
+ gr.Markdown("GALITA is a self-learning AI agent that can search for information, analyze data, create tools, and orchestrate complex tasks.")
86
+
87
+ chatbot_component = gr.Chatbot(
88
+ label="Chat",
89
+ height=500,
90
+ show_label=False,
91
+ # type='messages' # For Gradio 4.x+
92
+ )
93
+ gr.Markdown("Gradio version: " + gr.__version__ + " (Chatbot type defaults to 'tuples' for older versions. Consider `type='messages'` for newer Gradio if issues persist with chat display).")
94
+
95
+
96
+ with gr.Row():
97
+ message_textbox = gr.Textbox(
98
+ placeholder="Tapez votre message ici...",
99
+ scale=7,
100
+ show_label=False,
101
+ container=False
102
+ )
103
+
104
+ gr.Examples(
105
+ examples=[
106
+ "🔍 Recherche des informations sur l'intelligence artificielle",
107
+ "📊 Analyse les tendances du marché technologique",
108
+ "⚡ Crée un script pour automatiser une tâche répétitive",
109
+ "🌐 Trouve des ressources open source pour machine learning",
110
+ "what is the temperature in paris now"
111
+ ],
112
+ inputs=message_textbox,
113
+ )
114
+
115
+ status_box_component = gr.Textbox(
116
+ label="Agent Status",
117
+ value=get_current_status_for_ui(),
118
+ interactive=False,
119
+ # elem_id="status_box_alita" # For potential direct JS manipulation if desperate (avoid)
120
+ )
121
+
122
+ def add_user_msg(user_input_text, chat_history_list):
123
+ if not user_input_text.strip():
124
+ return gr.update(), chat_history_list
125
+ # For older Gradio, history is list of [user_msg, bot_msg] tuples
126
+ chat_history_list.append((user_input_text, None))
127
+ return gr.update(value=""), chat_history_list
128
+
129
+ async def generate_bot_reply(chat_history_list):
130
+ if not chat_history_list or chat_history_list[-1][0] is None:
131
+ # This case should ideally not be reached if add_user_msg works correctly
132
+ yield chat_history_list
133
+ return
134
+
135
+ user_message = chat_history_list[-1][0]
136
+
137
+ if manager_agent_global is None or LlamaSettings.llm is None:
138
+ # This update_status_callback will set current_status
139
+ # The polling mechanism (continuous_status_updater) should pick it up.
140
+ update_status_callback("⚠️ Error: Agent or LLM not initialized. Check API key and logs.")
141
+ # For older Gradio, update the last tuple's second element
142
+ chat_history_list[-1] = (chat_history_list[-1][0], "❌ Critical Error: ALITA is not properly initialized. Please check server logs and API key.")
143
+ yield chat_history_list
144
+ return
145
+
146
+ try:
147
+ print(f"\n🤖 GRADIOLOG: Processing user message: '{user_message[:100]}{'...' if len(user_message) > 100 else ''}'")
148
+ update_status_callback(f"💬 Processing: '{user_message[:50]}{'...' if len(user_message) > 50 else ''}'")
149
+ await asyncio.sleep(0.01) # Allow UI to briefly update with "Processing..."
150
+
151
+ task_prompt = TaskPrompt(text=user_message)
152
+
153
+ update_status_callback("🔄 Analyzing request and determining optimal workflow...")
154
+ await asyncio.sleep(0.01) # Allow UI to briefly update
155
+
156
+ # Run the blocking manager_agent_global.run_task in a separate thread
157
+ loop = asyncio.get_event_loop()
158
+ response_text_from_agent = await loop.run_in_executor(
159
+ thread_pool_executor,
160
+ manager_agent_global.run_task, # The function to run
161
+ task_prompt # Arguments to the function
162
+ )
163
+ # By this point, run_task has completed, and all its internal
164
+ # calls to update_status_callback (via send_update) should have occurred.
165
+ # The polling mechanism should have picked up these changes.
166
+
167
+ update_status_callback("✨ Generating final response stream...")
168
+ await asyncio.sleep(0.01)
169
+ final_bot_response = response_text_from_agent
170
+
171
+ words = final_bot_response.split()
172
+ accumulated_response_stream = ""
173
+ total_words = len(words)
174
+
175
+ # Initialize bot's part of the message in history for older Gradio
176
+ current_user_message = chat_history_list[-1][0]
177
+ chat_history_list[-1] = (current_user_message, "")
178
+
179
+
180
+ if not words:
181
+ chat_history_list[-1] = (current_user_message, final_bot_response.strip())
182
+ yield chat_history_list
183
+ else:
184
+ for i, word in enumerate(words):
185
+ accumulated_response_stream += word + " "
186
+ # These status updates are for the streaming part,
187
+ # agent's internal updates should have already happened.
188
+ if total_words > 0: # Avoid division by zero
189
+ if i == total_words // 4: update_status_callback("🔄 Streaming response (25%)...")
190
+ elif i == total_words // 2: update_status_callback("🔄 Streaming response (50%)...")
191
+ elif i == (total_words * 3) // 4: update_status_callback("🔄 Streaming response (75%)...")
192
+
193
+ if i % 3 == 0 or i == len(words) - 1:
194
+ chat_history_list[-1] = (current_user_message, accumulated_response_stream.strip())
195
+ yield chat_history_list
196
+ await asyncio.sleep(0.01) # For streaming effect
197
+
198
+ # Ensure final complete response is set
199
+ if chat_history_list[-1][1] != final_bot_response.strip():
200
+ chat_history_list[-1] = (current_user_message, final_bot_response.strip())
201
+ yield chat_history_list
202
+
203
+ print("✅ GRADIOLOG: Task processing and streaming completed.")
204
+ update_status_callback("✅ Ready for your next request")
205
+
206
+ except Exception as e:
207
+ error_message_for_ui = f"❌ Gradio/Agent Error: {str(e)}"
208
+ print(f"\n🚨 GRADIOLOG: Error in generate_bot_reply: {e}")
209
+ traceback.print_exc()
210
+ update_status_callback(f"❌ Error: {str(e)[:100]}...")
211
+ chat_history_list[-1] = (chat_history_list[-1][0], error_message_for_ui)
212
+ yield chat_history_list
213
+
214
+ message_textbox.submit(
215
+ add_user_msg,
216
+ inputs=[message_textbox, chatbot_component],
217
+ outputs=[message_textbox, chatbot_component],
218
+ show_progress="hidden", # Gradio 3.x might not have this, can be ignored
219
+ ).then(
220
+ generate_bot_reply,
221
+ inputs=[chatbot_component],
222
+ outputs=[chatbot_component],
223
+ api_name=False, # Good practice
224
+ # show_progress="hidden", # Gradio 3.x might not have this
225
+ )
226
+
227
+ async def continuous_status_updater(update_interval_seconds=0.3): # Slightly faster poll
228
+ """Continuously yields status updates for the status_box_component."""
229
+ print("GRADIOLOG: Starting continuous_status_updater loop.")
230
+ while True:
231
+ # print(f"POLL: Fetching status: {current_status}") # DEBUG: very verbose
232
+ yield get_current_status_for_ui()
233
+ await asyncio.sleep(update_interval_seconds)
234
+
235
+ demo.load(continuous_status_updater, inputs=None, outputs=status_box_component)
236
+ print("GRADIOLOG: Continuous status updater loaded.")
237
+ return demo
238
+
239
+ # Initialize LLM and Agent components
240
+ initialize_components()
241
+
242
+ # --- Launch the Application ---
243
+ if __name__ == "__main__":
244
+ print(f"Gradio version: {gr.__version__}")
245
+
246
+ print("🚀 Starting Gradio ALITA Chat Application...")
247
+ alita_interface = create_gradio_interface()
248
+
249
+ try:
250
+ alita_interface.launch(
251
+ share=False,
252
+ server_name="127.0.0.1",
253
+ server_port=5126,
254
+ show_error=True,
255
+ # debug=True # Can be helpful
256
+ )
257
+ except KeyboardInterrupt:
258
+ print("\n👋 Application stopped by user")
259
+ except Exception as e:
260
+ print(f"\n❌ Error launching application: {e}")
261
+ traceback.print_exc()
262
+ finally:
263
+ print("Shutting down thread pool executor...")
264
+ thread_pool_executor.shutdown(wait=True) # Clean up threads
265
+
266
+ print("✅ Gradio application stopped.")
app_modal.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import modal
2
+ import os
3
+
4
+ # Create Modal app
5
+ app = modal.App("alita-chat-app")
6
+
7
+ # Define the image with all required dependencies
8
+ image = (
9
+ modal.Image.debian_slim(python_version="3.11")
10
+ .pip_install([
11
+ "gradio>=4.0.0",
12
+ "llama-index-core",
13
+ "llama-index-llms-anthropic",
14
+ "python-dotenv",
15
+ "openai",
16
+ "llama-index",
17
+ "anthropic",
18
+ "requests",
19
+ "dataclasses",
20
+ "beautifulsoup4",
21
+ "duckduckgo-search",
22
+ "llama-index-tools-duckduckgo"
23
+ ])
24
+ # Main script
25
+ .add_local_file("manager_agent2.py", "/app/manager_agent2.py")
26
+
27
+ # Models
28
+ .add_local_file("models/__init__.py", "/app/models/__init__.py")
29
+ .add_local_file("models/mcp_tool_spec.py", "/app/models/mcp_tool_spec.py")
30
+ .add_local_file("models/mcp_execution_result.py", "/app/models/mcp_execution_result.py")
31
+ .add_local_file("models/task_prompt.py", "/app/models/task_prompt.py")
32
+
33
+ # Components
34
+ .add_local_file("components/__init__.py", "/app/components/__init__.py")
35
+ .add_local_file("components/mcp_brainstormer.py", "/app/components/mcp_brainstormer.py")
36
+ .add_local_file("components/web_agent.py", "/app/components/web_agent.py")
37
+ .add_local_file("components/script_generator.py", "/app/components/script_generator.py")
38
+ .add_local_file("components/code_runner.py", "/app/components/code_runner.py")
39
+ .add_local_file("components/mcp_registry.py", "/app/components/mcp_registry.py")
40
+ )
41
+
42
+ # Global variables to store initialized components
43
+ llm = None
44
+ manager_agent = None
45
+
46
+ @app.function(
47
+ image=image,
48
+ secrets=[modal.Secret.from_name("anthropic")],
49
+ max_containers=10,
50
+ timeout=300,
51
+ min_containers=1,
52
+ cpu=2,
53
+ memory=2048
54
+ )
55
+ def initialize_components():
56
+ """Initialize LLM and Manager Agent"""
57
+ global llm, manager_agent
58
+
59
+ import sys
60
+ sys.path.append("/app")
61
+
62
+ try:
63
+ # Import required modules
64
+ from llama_index.core import Settings
65
+ from llama_index.llms.anthropic import Anthropic
66
+ from models import TaskPrompt
67
+ from manager_agent import ManagerAgent
68
+
69
+ # Get API key from environment
70
+ api_key = os.environ.get("ANTHROPIC_API_KEY")
71
+ if not api_key:
72
+ raise ValueError("ANTHROPIC_API_KEY not found in environment variables")
73
+
74
+ # Initialize LLM
75
+ llm = Anthropic(model="claude-3-5-sonnet-20241022", api_key=api_key)
76
+ Settings.llm = llm
77
+ print("Successfully initialized LlamaIndex with Anthropic model")
78
+
79
+ # Initialize the ManagerAgent
80
+ manager_agent = ManagerAgent(llm)
81
+ print("✅ ManagerAgent initialized successfully")
82
+
83
+ return True
84
+
85
+ except Exception as e:
86
+ print(f"Error initializing components: {e}")
87
+ import traceback
88
+ traceback.print_exc()
89
+ return False
90
+
91
+ @app.function(
92
+ image=image,
93
+ secrets=[modal.Secret.from_name("anthropic-api-key")],
94
+ max_containers=10,
95
+ timeout=60,
96
+ min_containers=1,
97
+ cpu=2,
98
+ memory=2048
99
+ )
100
+ def process_message(message: str):
101
+ """Process a single message through the ManagerAgent"""
102
+ import sys
103
+ sys.path.append("/app")
104
+
105
+ try:
106
+ from models import TaskPrompt
107
+ from manager_agent import ManagerAgent
108
+ from llama_index.core import Settings
109
+ from llama_index.llms.anthropic import Anthropic
110
+
111
+ # Initialize components if needed
112
+ api_key = os.environ.get("ANTHROPIC_API_KEY")
113
+ if not api_key:
114
+ return "❌ ANTHROPIC_API_KEY not found in environment variables"
115
+
116
+ llm = Anthropic(model="claude-3-5-sonnet-20241022", api_key=api_key)
117
+ Settings.llm = llm
118
+ manager_agent = ManagerAgent(llm)
119
+
120
+ # Process the message
121
+ task_prompt = TaskPrompt(text=message)
122
+
123
+ response = manager_agent.run_task(task_prompt)
124
+
125
+ return response
126
+
127
+ except Exception as e:
128
+ import traceback
129
+ error_msg = f"❌ Error processing message: {str(e)}\n{traceback.format_exc()}"
130
+ print(error_msg)
131
+ return error_msg
132
+
133
+ # FIXED: Simple web server approach
134
+ @app.function(
135
+ image=image,
136
+ secrets=[modal.Secret.from_name("anthropic-api-key")],
137
+ max_containers=10,
138
+ timeout=300,
139
+ min_containers=1,
140
+ cpu=2,
141
+ memory=2048
142
+ )
143
+ @modal.web_server(port=7860, startup_timeout=180)
144
+ def gradio_app():
145
+ """Simple Gradio app without complex initialization"""
146
+ import gradio as gr
147
+ import asyncio
148
+
149
+ async def chat_function(message, history):
150
+ """Simple chat function that calls the Modal function"""
151
+ try:
152
+ # Call the Modal function to process the message
153
+ response = process_message.remote(message)
154
+
155
+ # Stream the response word by word for better UX
156
+ words = response.split()
157
+ partial_response = ""
158
+
159
+ for i, word in enumerate(words):
160
+ partial_response += word + " "
161
+ if i % 3 == 0 or i == len(words) - 1:
162
+ yield partial_response.strip()
163
+ await asyncio.sleep(0.01)
164
+
165
+ except Exception as e:
166
+ yield f"❌ Error: {str(e)}"
167
+
168
+ # Create simple Gradio interface
169
+ interface = gr.ChatInterface(
170
+ fn=chat_function,
171
+ type="messages",
172
+ title="ALITA",
173
+ description="ALITA: the self learning AI",
174
+ examples=[
175
+ "🔍 search for information about AI",
176
+ "🛠️ Analyse this csv file",
177
+ "⚡ Generate a script to automate a repetitive task",
178
+ "🌐 Find open source resources for machine learning",
179
+ ],
180
+ theme="soft"
181
+ )
182
+
183
+ # Launch the interface with Modal-compatible settings
184
+ interface.launch(
185
+ server_name="0.0.0.0", # Must bind to all interfaces for Modal
186
+ server_port=7840, # Must match the port in @modal.web_server
187
+ share=False, # Don't create public links
188
+ quiet=True, # Reduce logging noise
189
+ show_error=True,
190
+ prevent_thread_lock=True # Important: prevents blocking Modal's event loop
191
+ )
192
+
193
+ # For local development and testing
194
+ if __name__ == "__main__":
195
+ app.deploy("alita-chat-app")
manager_agent.py ADDED
@@ -0,0 +1,689 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from typing import Optional, Dict, Any, List, Generator, Callable
5
+ from models import TaskPrompt, MCPToolSpec, MCPExecutionResult
6
+ from components import (
7
+ WebAgent,
8
+ ScriptGenerator,
9
+ CodeRunner,
10
+ Registry,
11
+ Brainstormer,
12
+ )
13
+ from llama_index.core.llms import LLM
14
+ from llama_index.core.agent import ReActAgent
15
+ from llama_index.core.tools import FunctionTool
16
+
17
+
18
+ # Load environment variables from .env file
19
+ load_dotenv()
20
+
21
+ class ManagerAgent:
22
+ """
23
+ The central orchestrator of the Alita agent - Revised for Gradio integration.
24
+
25
+ Workflow:
26
+ 1. Analyze user prompt to understand the request
27
+ 2. Check existing tools in registry first
28
+ 3. If research needed, formulate search queries and use WebAgent
29
+ 4. If tool needed but not found, brainstorm new tool requirements
30
+ 5. Search for open source tools/solutions via WebAgent
31
+ 6. Create implementation plan via Brainstormer
32
+ 7. Return comprehensive response
33
+ """
34
+
35
+ def __init__(self, llm: LLM, max_iterations: int = 10000000, update_callback: Optional[Callable[[str], None]] = None):
36
+
37
+
38
+ self.llm = llm
39
+ self.registry = Registry()
40
+ self.web_agent = WebAgent(llm=llm, max_research_iterations=10000000)
41
+ self.code_runner = CodeRunner()
42
+ self.brainstormer = Brainstormer(model_name="claude-sonnet-4-0")
43
+ self.script_generator = ScriptGenerator(llm=self.llm)
44
+ self.max_iterations = max_iterations
45
+ self.update_callback = update_callback
46
+
47
+ # Define the tools available to the internal LlamaIndex Agent
48
+ self._agent_tools = self._define_agent_tools()
49
+
50
+ # Initialize the internal LlamaIndex ReAct Agent with improved system prompt
51
+ self.agent = ReActAgent.from_tools(
52
+ tools=self._agent_tools,
53
+ llm=self.llm,
54
+ verbose=True,
55
+ system_prompt=self._get_system_prompt(),
56
+ max_iterations=self.max_iterations # Use the configurable max_iterations parameter
57
+ )
58
+ print("🤖 ManagerAgent initialized with ReActAgent and enhanced workflow.")
59
+
60
+ def send_update(self, message: str) -> None:
61
+ """
62
+ Send an update message to the user about the agent's progress.
63
+
64
+ Args:
65
+ message: The update message to send
66
+
67
+ Returns:
68
+ None
69
+ """
70
+ print(f"📢 Update: {message}")
71
+
72
+ # If a callback function is provided, use it to send the update to the user
73
+ if self.update_callback:
74
+ try:
75
+ self.update_callback(message)
76
+ except Exception as e:
77
+ print(f"Error sending update via callback: {e}")
78
+
79
+ def _get_system_prompt(self) -> str:
80
+ """Enhanced system prompt for better workflow orchestration"""
81
+ return """You are ALITA, an advanced generalist agent. You are here to help people with their requests. you can do many tasks like research, tool creation, automation, analysis, and much more. What is unique about you is that you can create tools to help people with their requests, even if they are not in your capabilities.
82
+
83
+ Your primary workflow for ANY user request:
84
+
85
+ 1. **ANALYZE PHASE**:
86
+ - Understand the user's request deeply
87
+ - Identify if it's: information request, tool request, task automation, research, or creative work.
88
+ - here you decide wether to answer the request or to create a tool to answer the request, or to search the web only.
89
+ - if you decide to answer directly, give your answer right away.
90
+ - if you decide to search the web, use 'web_search' with specific queries. give a first answer to the user saying you are searching the web, then take the action of 'web_search'.
91
+ - if you there is a thing that needs something more than a text generation or search, then look for existing tool here in the next steps.
92
+ - Use 'send_user_update' to inform the user about what you're doing and your progress, if you didnt answer direclty to the prompt.
93
+ - Do not apologize quickly for not being able to answer the prompt, until you do the next steps: EXISTING TOOLS CHECK, TOOL ANALYSIS PHASE, RESEARCH PHASE, TOOL CREATION PHASE. if not successful then apologize.
94
+
95
+ 2. **EXISTING TOOLS CHECK**:
96
+ - ALWAYS first use 'get_available_tools' to list all tools in your registry
97
+ - If suitable tools exist but are not deployed, use 'deploy_tool' to activate them
98
+ - Once tools are active, use 'run_registered_mcp' to execute them OR use 'use_registry_tool' for direct invocation
99
+ - Keep the user informed of your progress with 'send_user_update'
100
+
101
+ 3. **TOOL ANALYSIS PHASE**:
102
+ - If you need to determine whether existing tools are sufficient or new tools are needed, use 'brainstorm_tools'
103
+ - This will analyze the user request against available tools and recommend which tools to use or what new tools to create
104
+ - Follow the recommendations from the brainstorming phase
105
+ - Send an update to the user with 'send_user_update' about your findings
106
+
107
+ 4. **RESEARCH PHASE** (if needed):
108
+ - For information requests: use 'web_search' with specific queries
109
+ - For in-depth research topics: use 'perform_web_research' for comprehensive autonomous research
110
+ - For technical solutions: use 'github_search' for open source tools
111
+ - Use 'retrieve_url_content' to get detailed information from promising results
112
+ - Send updates to the user with 'send_user_update' about your research progress
113
+
114
+ 5. **TOOL CREATION PHASE** (if no existing tool works):
115
+ - Use 'brainstorm_tools' to identify what kind of tool is needed
116
+ - Use 'web_search' and 'github_search' to find existing open source solutions
117
+ - Use 'generate_mcp_script' to create implementation based on research
118
+ - Use 'execute_and_register_mcp' to validate and register the new tool
119
+ - Keep the user informed of your progress with 'send_user_update'
120
+
121
+ 6. **EXECUTION PHASE**:
122
+ - Use appropriate registered tools via 'run_registered_mcp' or 'use_registry_tool'
123
+ - Provide comprehensive results with explanations
124
+ - Send a final update to the user with 'send_user_update' about the results
125
+
126
+ **Key Principles**:
127
+ - Be proactive in tool discovery and creation
128
+ - Always search for existing solutions before creating new ones
129
+ - Provide detailed explanations of your reasoning process
130
+ - Focus on practical, actionable results
131
+ - Leverage open source resources extensively
132
+ - Keep the user informed of your progress with regular updates
133
+
134
+ **Tool Management Capabilities**:
135
+ - Use 'get_available_tools' to see all tools in your registry
136
+ - Use 'brainstorm_tools' to analyze if existing tools are sufficient or new ones are needed
137
+ - Check tool states to determine if they are active ('activated') or inactive ('deactivated')
138
+ - Use 'deploy_tool' to activate any inactive tools before running them
139
+ - Remember that tools must be deployed before they can be executed
140
+ - Use 'use_registry_tool' for direct tool invocation with automatic deployment
141
+
142
+ **Tool Usage Options**:
143
+ - 'run_registered_mcp': Traditional method that requires separate deployment and execution steps
144
+ - 'use_registry_tool': Streamlined method that handles deployment automatically and provides direct invocation
145
+
146
+ **Research Capabilities**:
147
+ - For simple information needs, use 'web_search' for quick answers
148
+ - For complex research topics requiring in-depth analysis, use 'perform_web_research'
149
+ - The 'perform_web_research' tool conducts autonomous research across multiple sources and synthesizes findings
150
+
151
+ **Response Style**:
152
+ - Structure your responses clearly with headers
153
+ - Explain what you're doing and why
154
+ - Provide context and next steps
155
+ - Be conversational but informative
156
+ - Use 'send_user_update' to keep the user informed throughout the process
157
+ """
158
+
159
+ def _define_agent_tools(self) -> List[FunctionTool]:
160
+ """Enhanced tool definition with better descriptions"""
161
+ tools = []
162
+
163
+ # User update tool
164
+ tools.append(
165
+ FunctionTool.from_defaults(
166
+ self.send_update,
167
+ name="send_user_update",
168
+ description="Send an update message to the user about your current progress or actions. Takes 'message' (string) containing the update information. Use this tool frequently to keep the user informed about what you're doing."
169
+ )
170
+ )
171
+
172
+ # Add research tool
173
+ tools.append(
174
+ FunctionTool.from_defaults(
175
+ self.research,
176
+ name="perform_web_research",
177
+ description="Performs comprehensive web research on a given topic. Takes 'query' (string) containing the research question or topic to investigate. Returns a detailed research report with findings and sources."
178
+ )
179
+ )
180
+
181
+ # Get all available tools
182
+ tools.append(
183
+ FunctionTool.from_defaults(
184
+ self.get_available_tools,
185
+ name="get_available_tools",
186
+ description="Get a list of all tools currently available in the registry. Returns a list of tool specifications with names, descriptions, and states."
187
+ )
188
+ )
189
+
190
+ # Use a registered tool
191
+ tools.append(
192
+ FunctionTool.from_defaults(
193
+ self.use_registry_tool,
194
+ name="use_registry_tool",
195
+ description="Use a registered tool directly by invoking its endpoint. Takes 'tool_name' (string) and any additional arguments required by the tool. Automatically deploys the tool if needed. Returns the response from the tool."
196
+ )
197
+ )
198
+
199
+ # Tool brainstorming
200
+ tools.append(
201
+ FunctionTool.from_defaults(
202
+ self.brainstorm_tools,
203
+ name="brainstorm_tools",
204
+ description="Analyze the user request against available tools to determine if existing tools are sufficient or new tools are needed. Takes 'user_task' (string) containing the user's request and optionally 'available_tools' (string) with comma-separated tool names. Returns recommendations on which tools to use or what new tools to create."
205
+ )
206
+ )
207
+
208
+ # Deploy a specific tool
209
+ tools.append(
210
+ FunctionTool.from_defaults(
211
+ self.deploy_tool,
212
+ name="deploy_tool",
213
+ description="Deploy and activate a specific tool from the registry. Takes 'tool_name' (string) containing the name of the tool to deploy. Returns the URL of the deployed tool if successful, or an error message if deployment fails."
214
+ )
215
+ )
216
+
217
+ # # Enhanced execution and registration tool
218
+ # tools.append(
219
+ # FunctionTool.from_defaults(
220
+ # self._run_and_register_mcp,
221
+ # name="execute_and_register_mcp",
222
+ # description="Execute a generated MCP script in an isolated environment and register it if successful. Takes 'spec' (MCPToolSpec as dict), 'python_script' (string), 'env_script' (string), and optional 'input_data' (dict). Returns execution result."
223
+ # )
224
+ # )
225
+
226
+ # # Enhanced registered tool execution
227
+ # tools.append(
228
+ # FunctionTool.from_defaults(
229
+ # self._run_registered_mcp,
230
+ # name="run_registered_mcp",
231
+ # description="Execute a previously registered MCP tool. Takes 'tool_name' (string) and optional 'input_data' (dict). Returns execution result with output data."
232
+ # )
233
+ # )
234
+
235
+ # Add analysis tool for better decision making
236
+ tools.append(
237
+ FunctionTool.from_defaults(
238
+ self._analyze_user_request,
239
+ name="analyze_user_request",
240
+ description="Analyze user request to determine the best approach (research, existing tool, new tool creation). Takes 'user_message' (string). Returns analysis with recommended actions."
241
+ )
242
+ )
243
+
244
+ return tools
245
+
246
+ def _analyze_user_request(self, user_message: str) -> Dict[str, Any]:
247
+ """Analyze user request to determine optimal workflow path"""
248
+ analysis = {
249
+ "request_type": "unknown",
250
+ "complexity": "medium",
251
+ "requires_research": False,
252
+ "requires_tools": False,
253
+ "suggested_approach": [],
254
+ "key_concepts": []
255
+ }
256
+
257
+ message_lower = user_message.lower()
258
+
259
+ # Look for comprehensive research indicators
260
+ research_terms = ["comprehensive", "thorough", "in-depth", "detailed", "extensive",
261
+ "research", "investigate", "analyze", "report", "study"]
262
+
263
+ # Determine request type
264
+ if any(word in message_lower for word in research_terms):
265
+ analysis["request_type"] = "deep_research"
266
+ analysis["requires_research"] = True
267
+ analysis["complexity"] = "high"
268
+ analysis["suggested_approach"].append("research")
269
+
270
+ elif any(word in message_lower for word in ["recherche", "search", "find", "lookup", "information", "what is", "explain"]):
271
+ analysis["request_type"] = "information_request"
272
+ analysis["requires_research"] = True
273
+ analysis["suggested_approach"].append("web_search")
274
+
275
+ elif any(word in message_lower for word in ["outil", "tool", "script", "automatise", "automate", "create", "build"]):
276
+ analysis["request_type"] = "tool_request"
277
+ analysis["requires_tools"] = True
278
+ analysis["suggested_approach"].extend(["find_existing_tools", "brainstorm_if_needed"])
279
+
280
+ elif any(word in message_lower for word in ["analyse", "analyze", "process", "calculate", "compute"]):
281
+ analysis["request_type"] = "analysis_task"
282
+ analysis["requires_tools"] = True
283
+ analysis["suggested_approach"].extend(["find_existing_tools", "research_methods"])
284
+
285
+ elif any(word in message_lower for word in ["tendance", "trend", "market", "news", "current"]):
286
+ analysis["request_type"] = "research_task"
287
+ analysis["requires_research"] = True
288
+ analysis["complexity"] = "high"
289
+ analysis["suggested_approach"].extend(["web_search", "github_search"])
290
+
291
+ # Extract key concepts for better tool matching
292
+ concepts = []
293
+ tech_keywords = ["python", "javascript", "api", "database", "csv", "json", "web", "scraping", "ml", "ai"]
294
+ for keyword in tech_keywords:
295
+ if keyword in message_lower:
296
+ concepts.append(keyword)
297
+ analysis["key_concepts"] = concepts
298
+
299
+ return analysis
300
+
301
+ def _run_and_register_mcp(self, spec: Dict[str, Any], python_script: str, env_script: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
302
+ """Enhanced MCP execution and registration with better error handling"""
303
+ print(f"🔧 ManagerAgent: Executing and registering MCP: {spec.get('name', 'Unnamed Tool')}")
304
+
305
+ try:
306
+ mcp_spec_obj = MCPToolSpec.from_dict(spec)
307
+ env_name_suffix = mcp_spec_obj.name.lower().replace(' ', '-')[:10]
308
+ env_name = f"alita-{env_name_suffix}-{uuid.uuid4().hex[:8]}"
309
+
310
+ print(f"🔄 Setting up environment: {env_name}")
311
+ env_success = self.code_runner.setup_environment(env_script, env_name)
312
+
313
+ if not env_success:
314
+ result = MCPExecutionResult(
315
+ success=False,
316
+ error_message=f"Environment setup failed for '{env_name}'. Check dependencies in env_script."
317
+ )
318
+ return result.to_dict()
319
+
320
+ print(f"▶️ Executing script in environment: {env_name}")
321
+ execution_result = self.code_runner.execute(python_script, env_name, input_data)
322
+
323
+ if execution_result.success:
324
+ print(f"✅ Script execution successful. Registering tool: {mcp_spec_obj.name}")
325
+ mcp_spec_obj.validated_script = python_script
326
+ mcp_spec_obj.environment_script = env_script
327
+ self.registry.register_tool(mcp_spec_obj)
328
+ print(f"🎯 Tool '{mcp_spec_obj.name}' successfully registered in registry")
329
+
330
+ # Add success message to result
331
+ execution_result.output_data = execution_result.output_data or {}
332
+ execution_result.output_data["registration_status"] = "Successfully registered"
333
+
334
+ else:
335
+ print(f"❌ Script execution failed for '{mcp_spec_obj.name}': {execution_result.error_message}")
336
+
337
+ # Always cleanup after validation
338
+ self.code_runner.cleanup_environment(env_name)
339
+ return execution_result.to_dict()
340
+
341
+ except Exception as e:
342
+ error_msg = f"Unexpected error in MCP execution: {str(e)}"
343
+ print(f"🚨 {error_msg}")
344
+
345
+ # Cleanup on error
346
+ try:
347
+ if 'env_name' in locals():
348
+ self.code_runner.cleanup_environment(env_name)
349
+ except:
350
+ pass
351
+
352
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
353
+
354
+ def _run_registered_mcp(self, tool_name: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
355
+ """Enhanced registered tool execution with better logging"""
356
+ print(f"🎯 ManagerAgent: Running registered tool: {tool_name}")
357
+
358
+ spec = self.registry.get_tool(tool_name)
359
+ if not spec:
360
+ error_msg = f"Tool '{tool_name}' not found in registry. Available tools: {list(self.registry.tools.keys())}"
361
+ print(f"❌ {error_msg}")
362
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
363
+
364
+ if not spec.validated_script or not spec.environment_script:
365
+ error_msg = f"Tool '{tool_name}' missing validated script or environment configuration"
366
+ print(f"❌ {error_msg}")
367
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
368
+
369
+ # Create fresh environment for execution
370
+ env_name_suffix = spec.name.lower().replace(' ', '-')[:10]
371
+ env_name = f"alita-run-{env_name_suffix}-{uuid.uuid4().hex[:8]}"
372
+
373
+ try:
374
+ print(f"🔄 Setting up execution environment: {env_name}")
375
+ env_success = self.code_runner.setup_environment(spec.environment_script, env_name)
376
+
377
+ if not env_success:
378
+ return MCPExecutionResult(
379
+ success=False,
380
+ error_message=f"Failed to setup environment for tool '{tool_name}'"
381
+ ).to_dict()
382
+
383
+ print(f"▶️ Executing registered tool: {tool_name}")
384
+ execution_result = self.code_runner.execute(spec.validated_script, env_name, input_data)
385
+
386
+ print(f"{'✅' if execution_result.success else '❌'} Tool execution completed. Success: {execution_result.success}")
387
+
388
+ return execution_result.to_dict()
389
+
390
+ except Exception as e:
391
+ error_msg = f"Error executing registered tool '{tool_name}': {str(e)}"
392
+ print(f"🚨 {error_msg}")
393
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
394
+
395
+ finally:
396
+ # Always cleanup
397
+ try:
398
+ self.code_runner.cleanup_environment(env_name)
399
+ except:
400
+ pass
401
+
402
+ def run_task(self, prompt: TaskPrompt) -> str:
403
+ """
404
+ Enhanced task execution with detailed logging and structured workflow
405
+ Optimized for Gradio integration with comprehensive responses
406
+ """
407
+ print(f"\n{'='*60}")
408
+ print(f"🚀 ALITA ManagerAgent: Starting task execution")
409
+ print(f"📝 User prompt: {prompt.text[:100]}{'...' if len(prompt.text) > 100 else ''}")
410
+ print(f"{'='*60}")
411
+
412
+ # Send initial update to the user
413
+ self.send_update(f"Starting to process your request: '{prompt.text[:50]}{'...' if len(prompt.text) > 50 else ''}'")
414
+
415
+ try:
416
+ # Use the internal ReAct agent to handle the complete workflow
417
+ print("🧠 Engaging ReAct Agent for intelligent task orchestration...")
418
+
419
+ # The ReAct agent will use its tools to:
420
+ # 1. Analyze the request
421
+ # 2. Search existing tools
422
+ # 3. Perform web research if needed
423
+ # 4. Brainstorm solutions
424
+ # 5. Create/execute tools as necessary
425
+ # 6. Provide comprehensive response
426
+
427
+ response = self.agent.chat(prompt.text)
428
+
429
+ print("✅ Task execution completed successfully")
430
+ print(f"{'='*60}\n")
431
+
432
+ # Send final update to the user
433
+ self.send_update("Task completed successfully! Here's your response.")
434
+
435
+ # Format response for better Gradio presentation
436
+ formatted_response = self._format_response_for_gradio(response.response)
437
+ return formatted_response
438
+
439
+ except Exception as e:
440
+ error_msg = f"🚨 ManagerAgent encountered an error during task execution:\n\n**Error Details:**\n{str(e)}\n\n**Next Steps:**\n- Check your API key and network connection\n- Verify all components are properly initialized\n- Try a simpler request to test basic functionality"
441
+
442
+ print(f"❌ Task execution failed: {e}")
443
+ print(f"{'='*60}\n")
444
+
445
+ # Send error update to the user
446
+ self.send_update(f"An error occurred while processing your request: {str(e)}")
447
+
448
+ return error_msg
449
+
450
+ def _format_response_for_gradio(self, response: str) -> str:
451
+ """Format the agent response for better presentation in Gradio"""
452
+
453
+ # Add header if not present
454
+ if not response.startswith("##") and not response.startswith("#"):
455
+ response = f"## 🤖 {response}"
456
+
457
+ # Add footer with capabilities reminder (occasionally)
458
+ if "capabilities" not in response.lower():
459
+ footer = "\n\n---\n💡 **Tip**: I can help you with research, tool creation, automation, analysis, and much more. Just ask!"
460
+ response += footer
461
+
462
+ return response
463
+
464
+ def get_registry_status(self) -> Dict[str, Any]:
465
+ """Get current status of the tool registry"""
466
+ return {
467
+ "total_tools": len(self.registry.tools),
468
+ "tool_names": list(self.registry.tools.keys()),
469
+ "registry_ready": len(self.registry.tools) > 0
470
+ }
471
+
472
+ def reset_registry(self):
473
+ """Reset the tool registry (useful for testing)"""
474
+ self.registry = Registry()
475
+ print("🔄 Tool registry has been reset")
476
+
477
+ def __str__(self):
478
+ return f"ManagerAgent(llm={type(self.llm).__name__}, tools_registered={len(self.registry.tools)})"
479
+
480
+ def research(self, query: str, max_iterations: int = None, verbose: bool = None) -> str:
481
+ """
482
+ Performs autonomous web research on the given query using the WebAgent's research function.
483
+
484
+ Args:
485
+ query: The research question or topic
486
+ max_iterations: Optional override for the maximum number of research iterations
487
+ verbose: Optional override for verbose mode
488
+
489
+ Returns:
490
+ A comprehensive textual report based on web research
491
+ """
492
+ print(f"\n{'='*60}")
493
+ print(f"🌐 ALITA ManagerAgent: Starting web research")
494
+ print(f"📝 Research query: {query[:100]}{'...' if len(query) > 100 else ''}")
495
+ print(f"{'='*60}")
496
+
497
+ try:
498
+ # Configure WebAgent for this research session
499
+ if max_iterations is not None:
500
+ self.web_agent.max_research_iterations = max_iterations
501
+
502
+ if verbose is not None:
503
+ self.web_agent.verbose = verbose
504
+
505
+ # Perform the research
506
+ print("🔍 Initiating autonomous web research. This may take some time... here is the query: ", query)
507
+ report = self.web_agent.research(query)
508
+ print("🔍 here is the report: ", report)
509
+
510
+ print("✅ Research completed successfully")
511
+ print(f"{'='*60}\n")
512
+
513
+ return report
514
+
515
+ except Exception as e:
516
+ error_msg = f"🚨 Error during web research: {str(e)}"
517
+ print(f"❌ Research failed: {e}")
518
+ print(f"{'='*60}\n")
519
+
520
+ import traceback
521
+ print(traceback.format_exc())
522
+
523
+ return error_msg
524
+
525
+ def get_available_tools(self) -> List[Dict[str, Any]]:
526
+ """
527
+ Get a list of all tools currently available in the registry.
528
+
529
+ Returns:
530
+ List of dictionaries containing tool information (name, description, state)
531
+ """
532
+ print("📋 ManagerAgent: Retrieving list of all available tools")
533
+ tools = self.registry.list_tools()
534
+
535
+ # Format the tools for easier consumption by the agent
536
+ formatted_tools = []
537
+ for tool in tools:
538
+ formatted_tools.append({
539
+ "name": tool.name,
540
+ "description": tool.description,
541
+ "state": getattr(tool, "state", "unknown"),
542
+ "input_schema": tool.input_schema if hasattr(tool, "input_schema") else {},
543
+ "output_schema": tool.output_schema if hasattr(tool, "output_schema") else {}
544
+ })
545
+
546
+ print(f"🔍 Found {len(formatted_tools)} tools in registry")
547
+ return formatted_tools
548
+
549
+ def deploy_tool(self, tool_name: str) -> Dict[str, Any]:
550
+ """
551
+ Deploy and activate a specific tool from the registry.
552
+
553
+ Args:
554
+ tool_name: Name of the tool to deploy
555
+
556
+ Returns:
557
+ Dictionary with deployment status and URL (if successful)
558
+ """
559
+ print(f"🚀 ManagerAgent: Deploying tool '{tool_name}'")
560
+
561
+ # Check if tool exists in registry
562
+ if not self.registry.get_tool(tool_name):
563
+ error_msg = f"Tool '{tool_name}' not found in registry"
564
+ print(f"❌ {error_msg}")
565
+ return {"success": False, "error": error_msg}
566
+
567
+ # Attempt to deploy the tool
568
+ try:
569
+ url = self.registry.deploy_tool(tool_name)
570
+
571
+ if url:
572
+ print(f"✅ Successfully deployed tool '{tool_name}' at {url}")
573
+ return {
574
+ "success": True,
575
+ "tool_name": tool_name,
576
+ "url": url,
577
+ "message": f"Tool '{tool_name}' successfully deployed"
578
+ }
579
+ else:
580
+ error_msg = f"Failed to deploy tool '{tool_name}'"
581
+ print(f"❌ {error_msg}")
582
+ return {"success": False, "error": error_msg}
583
+
584
+ except Exception as e:
585
+ error_msg = f"Error deploying tool '{tool_name}': {str(e)}"
586
+ print(f"🚨 {error_msg}")
587
+ return {"success": False, "error": error_msg}
588
+
589
+ def brainstorm_tools(self, user_task: str, available_tools: str = "") -> Dict[str, Any]:
590
+ """
591
+ Use the Brainstormer to analyze if existing tools are sufficient or new tools are needed.
592
+
593
+ Args:
594
+ user_task: The user's request or task
595
+ available_tools: Optional comma-separated list of available tool names
596
+
597
+ Returns:
598
+ Dictionary with tool recommendations or specifications for new tools
599
+ """
600
+ print(f"🧠 ManagerAgent: Brainstorming tools for task: {user_task[:100]}{'...' if len(user_task) > 100 else ''}")
601
+
602
+ # If available_tools is not provided, get them from the registry
603
+ if not available_tools:
604
+ tools = self.get_available_tools()
605
+ available_tools = ", ".join([tool["name"] for tool in tools])
606
+
607
+ try:
608
+ # Call the brainstormer to analyze the task and available tools
609
+ result = self.brainstormer.generate_mcp_specs_to_fulfill_user_task(
610
+ task=user_task,
611
+ tools_list=available_tools
612
+ )
613
+
614
+ if isinstance(result, dict) and "error" in result:
615
+ print(f"❌ Brainstorming failed: {result['error']}")
616
+ return {
617
+ "success": False,
618
+ "error": result["error"],
619
+ "recommendations": "Unable to analyze tools for this task."
620
+ }
621
+
622
+ print(f"✅ Brainstorming complete. Found {len(result)} tool recommendations.")
623
+
624
+ # Format the result for better consumption by the agent
625
+ return {
626
+ "success": True,
627
+ "recommendations": result,
628
+ "summary": f"Analysis complete. Found {len(result)} tool recommendations."
629
+ }
630
+
631
+ except Exception as e:
632
+ error_msg = f"Error during tool brainstorming: {str(e)}"
633
+ print(f"🚨 {error_msg}")
634
+ return {
635
+ "success": False,
636
+ "error": error_msg,
637
+ "recommendations": "Unable to analyze tools due to an error."
638
+ }
639
+
640
+ def use_registry_tool(self, tool_name: str, *args, **kwargs) -> Dict[str, Any]:
641
+ """
642
+ Use a registered tool directly by invoking its endpoint.
643
+
644
+ This method utilizes the Registry's use_tool method to invoke a registered tool.
645
+ It handles tool deployment if needed and provides proper error handling and user feedback.
646
+
647
+ Args:
648
+ tool_name: Name of the tool to use
649
+ *args: Positional arguments to pass to the tool
650
+ **kwargs: Keyword arguments to pass to the tool
651
+
652
+ Returns:
653
+ The response from the tool as a Python object
654
+ """
655
+ try:
656
+ # Send update to user
657
+ self.send_update(f"Using tool: {tool_name}")
658
+
659
+ # Check if tool exists in registry
660
+ if not self.registry.get_tool(tool_name):
661
+ error_msg = f"Tool '{tool_name}' not found in registry"
662
+ self.send_update(error_msg)
663
+ return {"error": error_msg, "success": False}
664
+
665
+ # Use the tool via Registry's use_tool method
666
+ self.send_update(f"Executing tool: {tool_name}")
667
+ result = self.registry.use_tool(tool_name, *args, **kwargs)
668
+
669
+ # Send success update
670
+ self.send_update(f"Tool '{tool_name}' executed successfully")
671
+
672
+ # Return result with success flag
673
+ if isinstance(result, dict):
674
+ result["success"] = True
675
+ return result
676
+ else:
677
+ return {"result": result, "success": True}
678
+
679
+ except ValueError as e:
680
+ # Handle expected errors (tool not found, deployment failed)
681
+ error_msg = str(e)
682
+ self.send_update(f"Error: {error_msg}")
683
+ return {"error": error_msg, "success": False}
684
+
685
+ except Exception as e:
686
+ # Handle unexpected errors
687
+ error_msg = f"Unexpected error using tool '{tool_name}': {str(e)}"
688
+ self.send_update(f"Error: {error_msg}")
689
+ return {"error": error_msg, "success": False}
manager_agent2.py ADDED
@@ -0,0 +1,663 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from typing import Optional, Dict, Any, List, Generator, Callable
5
+ from models import TaskPrompt, MCPToolSpec, MCPExecutionResult
6
+ from components import (
7
+ WebAgent,
8
+ ScriptGenerator,
9
+ CodeRunner,
10
+ Registry,
11
+ Brainstormer,
12
+ )
13
+ from llama_index.core.llms import LLM
14
+ from llama_index.core.agent import ReActAgent
15
+ from llama_index.core.tools import FunctionTool
16
+
17
+
18
+ # Load environment variables from .env file
19
+ load_dotenv()
20
+
21
+ class ManagerAgent:
22
+ """
23
+ The central orchestrator of the Alita agent - Revised for Gradio integration.
24
+
25
+ Workflow:
26
+ 1. Analyze user prompt to understand the request
27
+ 2. Check existing tools in registry first
28
+ 3. If research needed, formulate search queries and use WebAgent
29
+ 4. If tool needed but not found, brainstorm new tool requirements
30
+ 5. Search for open source tools/solutions via WebAgent
31
+ 6. Create implementation plan via Brainstormer
32
+ 7. Return comprehensive response
33
+ """
34
+
35
+ def __init__(self, llm: LLM, max_iterations: int = 10000000, update_callback: Optional[Callable[[str], None]] = None):
36
+
37
+
38
+ self.llm = llm
39
+ self.registry = Registry()
40
+ self.web_agent = WebAgent(llm=llm, max_research_iterations=10000000)
41
+ self.code_runner = CodeRunner()
42
+ self.brainstormer = Brainstormer(model_name="claude-sonnet-4-0")
43
+ self.script_generator = ScriptGenerator(task_prompt="", claude_api_key=os.getenv("CLAUDE_API_KEY", ""))
44
+ self.max_iterations = max_iterations
45
+ self.update_callback = update_callback
46
+
47
+ # Define the tools available to the internal LlamaIndex Agent
48
+ self._agent_tools = self._define_agent_tools()
49
+
50
+ # Initialize the internal LlamaIndex ReAct Agent with improved system prompt
51
+ self.agent = ReActAgent.from_tools(
52
+ tools=self._agent_tools,
53
+ llm=self.llm,
54
+ verbose=True,
55
+ system_prompt=self._get_system_prompt(),
56
+ max_iterations=self.max_iterations, # Use the configurable max_iterations parameter
57
+ temperature=0.2 # Lower temperature for more focused responses
58
+ )
59
+ print("🤖 ManagerAgent initialized with ReActAgent and enhanced workflow (temperature=0.2).")
60
+
61
+ def send_update(self, message: str) -> None:
62
+ """
63
+ Send an update message to the user about the agent's progress.
64
+ """
65
+ if not any(emoji in message[:2] for emoji in ["📢", "🔄", "✅", "❌", "⚠️", "💬", "🔍", "🚀", "✨"]):
66
+ message = f"📢 {message}"
67
+
68
+ print(f"📣 AGENT: ManagerAgent.send_update CALLED with message: {message}") # DEBUG
69
+ print(f"📣 AGENT: self.update_callback is: {self.update_callback}") # DEBUG
70
+
71
+ if self.update_callback:
72
+ try:
73
+ self.update_callback(message) # This should call update_status_callback in app.py
74
+ print(f"📣 AGENT: Callback invoked successfully.") # DEBUG
75
+ except Exception as e:
76
+ print(f"❌ AGENT: Error sending update via callback: {e}")
77
+ import traceback
78
+ traceback.print_exc()
79
+ else:
80
+ print("📣 AGENT: No update_callback configured for ManagerAgent.") # DEBUG
81
+ # Return a string confirmation, as ReAct tools often expect a string output
82
+ return f"Update sent: {message}" # MODIFICATION: Return a string
83
+
84
+ def _get_system_prompt(self) -> str:
85
+ """Enhanced system prompt for better workflow orchestration"""
86
+ return """You are ALITA, an advanced generalist agent. You are here to help people with their requests. You can do many tasks like research, tool creation, automation, analysis, and much more. What is unique about you is that you can create tools to help people with their requests, even if they are not in your capabilities.
87
+
88
+ Your primary workflow for ANY user request:
89
+
90
+ 1. **ANALYZE PHASE**:
91
+ * Understand the user's request deeply.
92
+ * Identify if it's: an information request, a tool request, task automation, research, or creative work.
93
+ * Decide whether to answer the request directly, create a new tool, or perform web research.
94
+ * If you decide to answer directly, provide your answer right away.
95
+ * If you decide to perform web research, use the `perform_web_research` tool with specific queries. Inform the user you are starting research before taking this action.
96
+ * If the task requires more than simple text generation or basic web research, proceed to check for existing tools.
97
+ * Use `send_user_update` to inform the user about what you're doing and your progress if you don't answer directly.
98
+ * Do not apologize for not being able to answer the prompt until you have attempted all subsequent steps (EXISTING TOOLS CHECK, TOOL ANALYSIS PHASE, RESEARCH PHASE, TOOL CREATION PHASE). If all fail, then apologize.
99
+
100
+ 2. **EXISTING TOOLS CHECK**:
101
+ * ALWAYS first use `get_available_tools` to list all tools in your registry.
102
+ * If suitable tools exist but are not deployed (check their 'state'), use `deploy_tool` to activate them.
103
+ * Once tools are active and deployed, use `use_registry_tool` to execute them with the necessary inputs.
104
+ * Keep the user informed of your progress with `send_user_update`.
105
+
106
+ 3. **TOOL ANALYSIS PHASE**:
107
+ * If you need to determine whether existing tools are sufficient or new tools are needed, use `brainstorm_tools`.
108
+ * Provide the `brainstorm_tools` function with the `user_task` and the `available_tools` (a comma-separated string of tool names from `get_available_tools`).
109
+ * If there are no tools available, provide "none" as the input for `available_tools` to the `brainstorm_tools` function.
110
+ * Follow the recommendations from the brainstorming phase.
111
+ * Send an update to the user with `send_user_update` about your findings.
112
+
113
+ 4. **RESEARCH PHASE** (if needed for information or tool creation):
114
+ * Use the `perform_web_research` tool for all web-based information gathering.
115
+ * For general information or in-depth research on a topic, provide a clear query to `perform_web_research`.
116
+ * If you are looking for open-source code, libraries, or technical solutions (including from GitHub), instruct `perform_web_research` in your query to focus on finding code examples or repositories. For instance: "perform_web_research: Find Python code snippets for parsing CSV files from GitHub."
117
+ * Send updates to the user with `send_user_update` about your research progress.
118
+
119
+ 5. **TOOL CREATION PHASE** (if no existing tool works or can be adapted):
120
+ * First, use `brainstorm_tools` to define the specifications of the new tool needed.
121
+ * Next, use `perform_web_research` to find existing open-source solutions, code examples, or libraries that can help build the tool. Be specific in your query to `perform_web_research` about looking for implementation details.
122
+ * Then, use `generate_mcp_script` to create the Python code and environment script for the tool, using the specification from `brainstorm_tools` and insights from your research.
123
+ * Finally, use `execute_and_register_mcp` to test the new tool in a safe environment and, if successful, register it in your tool registry.
124
+ * Keep the user informed of your progress with `send_user_update`.
125
+
126
+ 6. **EXECUTION PHASE** (after a tool is ready, either existing or newly created):
127
+ * Ensure the required tool is deployed using `deploy_tool` if it's not already active.
128
+ * Use `use_registry_tool` to run the active tool with the appropriate inputs.
129
+ * Provide comprehensive results with explanations.
130
+ * Send a final update to the user with `send_user_update` about the results.
131
+
132
+ **Key Principles**:
133
+ * Be proactive in tool discovery and creation.
134
+ * Always search for existing solutions before creating new ones.
135
+ * Provide detailed explanations of your reasoning process.
136
+ * Focus on practical, actionable results.
137
+ * Leverage open-source resources extensively via `perform_web_research`.
138
+ * Keep the user informed of your progress with regular updates using `send_user_update`.
139
+
140
+ **Tool Management Capabilities**:
141
+ * Use `get_available_tools` to see all tools in your registry.
142
+ * Use `brainstorm_tools` to analyze if existing tools are sufficient or new ones are needed.
143
+ * Check tool 'state' from `get_available_tools` to determine if they are active ('activated' or similar) or inactive.
144
+ * Use `deploy_tool` to activate any inactive tools before running them. Tools must be deployed before they can be executed by `use_registry_tool`.
145
+
146
+ **Response Style**:
147
+ * Structure your responses clearly with headers where appropriate.
148
+ * Explain what you're doing and why.
149
+ * Provide context and next steps.
150
+ * Be conversational but informative.
151
+ * Use `send_user_update` to keep the user informed throughout the process.
152
+ """
153
+
154
+ def _define_agent_tools(self) -> List[FunctionTool]:
155
+ """Enhanced tool definition with better descriptions"""
156
+ tools = []
157
+
158
+ # User update tool
159
+ tools.append(
160
+ FunctionTool.from_defaults(
161
+ self.send_update,
162
+ name="send_user_update",
163
+ description="Send an update message to the user about your current progress or actions. Takes 'message' (string) containing the update information. Use this tool frequently to keep the user informed about what you're doing."
164
+ )
165
+ )
166
+
167
+ # Add research tool
168
+ tools.append(
169
+ FunctionTool.from_defaults(
170
+ self.research,
171
+ name="perform_web_research",
172
+ description="Performs comprehensive web research on a given topic. Takes 'query' (string) containing the research question or topic to investigate. Returns a detailed research report with findings and sources."
173
+ )
174
+ )
175
+
176
+ # Get all available tools
177
+ tools.append(
178
+ FunctionTool.from_defaults(
179
+ self.get_available_tools,
180
+ name="get_available_tools",
181
+ description="Get a list of all tools currently available in the registry. Returns a list of tool specifications with names, descriptions, and states."
182
+ )
183
+ )
184
+
185
+ # Use a registered tool
186
+ tools.append(
187
+ FunctionTool.from_defaults(
188
+ self.use_registry_tool,
189
+ name="use_registry_tool",
190
+ description="Use a registered tool directly by invoking its endpoint. Takes 'tool_name' (string) and any additional arguments required by the tool. Automatically deploys the tool if needed. Returns the response from the tool."
191
+ )
192
+ )
193
+
194
+ # Tool brainstorming
195
+ tools.append(
196
+ FunctionTool.from_defaults(
197
+ self.brainstorm_tools,
198
+ name="brainstorm_tools",
199
+ description="Analyze the user request against available tools to determine if existing tools are sufficient or new tools are needed. Takes 'user_task' (string) containing the user's request and optionally 'available_tools' (string) with comma-separated tool names. Returns recommendations on which tools to use or what new tools to create."
200
+ )
201
+ )
202
+
203
+ # Deploy a specific tool
204
+ tools.append(
205
+ FunctionTool.from_defaults(
206
+ self.deploy_tool,
207
+ name="deploy_tool",
208
+ description="Deploy and activate a specific tool from the registry. Takes 'tool_name' (string) containing the name of the tool to deploy. Returns the URL of the deployed tool if successful, or an error message if deployment fails."
209
+ )
210
+ )
211
+
212
+ # Add analysis tool for better decision making
213
+ tools.append(
214
+ FunctionTool.from_defaults(
215
+ self._analyze_user_request,
216
+ name="analyze_user_request",
217
+ description="Analyze user request to determine the best approach (research, existing tool, new tool creation). Takes 'user_message' (string). Returns analysis with recommended actions."
218
+ )
219
+ )
220
+
221
+ return tools
222
+
223
+ def _analyze_user_request(self, user_message: str) -> Dict[str, Any]:
224
+ """Analyze user request to determine optimal workflow path"""
225
+ analysis = {
226
+ "request_type": "unknown",
227
+ "complexity": "medium",
228
+ "requires_research": False,
229
+ "requires_tools": False,
230
+ "suggested_approach": [],
231
+ "key_concepts": []
232
+ }
233
+
234
+ message_lower = user_message.lower()
235
+
236
+ # Look for comprehensive research indicators
237
+ research_terms = ["comprehensive", "thorough", "in-depth", "detailed", "extensive",
238
+ "research", "investigate", "analyze", "report", "study"]
239
+
240
+ # Determine request type
241
+ if any(word in message_lower for word in research_terms):
242
+ analysis["request_type"] = "deep_research"
243
+ analysis["requires_research"] = True
244
+ analysis["complexity"] = "high"
245
+ analysis["suggested_approach"].append("research")
246
+
247
+ elif any(word in message_lower for word in ["recherche", "search", "find", "lookup", "information", "what is", "explain"]):
248
+ analysis["request_type"] = "information_request"
249
+ analysis["requires_research"] = True
250
+ analysis["suggested_approach"].append("web_search")
251
+
252
+ elif any(word in message_lower for word in ["outil", "tool", "script", "automatise", "automate", "create", "build"]):
253
+ analysis["request_type"] = "tool_request"
254
+ analysis["requires_tools"] = True
255
+ analysis["suggested_approach"].extend(["find_existing_tools", "brainstorm_if_needed"])
256
+
257
+ elif any(word in message_lower for word in ["analyse", "analyze", "process", "calculate", "compute"]):
258
+ analysis["request_type"] = "analysis_task"
259
+ analysis["requires_tools"] = True
260
+ analysis["suggested_approach"].extend(["find_existing_tools", "research_methods"])
261
+
262
+ elif any(word in message_lower for word in ["tendance", "trend", "market", "news", "current"]):
263
+ analysis["request_type"] = "research_task"
264
+ analysis["requires_research"] = True
265
+ analysis["complexity"] = "high"
266
+ analysis["suggested_approach"].extend(["web_search", "github_search"])
267
+
268
+ # Extract key concepts for better tool matching
269
+ concepts = []
270
+ tech_keywords = ["python", "javascript", "api", "database", "csv", "json", "web", "scraping", "ml", "ai"]
271
+ for keyword in tech_keywords:
272
+ if keyword in message_lower:
273
+ concepts.append(keyword)
274
+ analysis["key_concepts"] = concepts
275
+
276
+ return analysis
277
+
278
+ def _run_and_register_mcp(self, spec: Dict[str, Any], python_script: str, env_script: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
279
+ """Enhanced MCP execution and registration with better error handling"""
280
+ print(f"🔧 ManagerAgent: Executing and registering MCP: {spec.get('name', 'Unnamed Tool')}")
281
+
282
+ try:
283
+ mcp_spec_obj = MCPToolSpec.from_dict(spec)
284
+ env_name_suffix = mcp_spec_obj.name.lower().replace(' ', '-')[:10]
285
+ env_name = f"alita-{env_name_suffix}-{uuid.uuid4().hex[:8]}"
286
+
287
+ print(f"🔄 Setting up environment: {env_name}")
288
+ env_success = self.code_runner.setup_environment(env_script, env_name)
289
+
290
+ if not env_success:
291
+ result = MCPExecutionResult(
292
+ success=False,
293
+ error_message=f"Environment setup failed for '{env_name}'. Check dependencies in env_script."
294
+ )
295
+ return result.to_dict()
296
+
297
+ print(f"▶️ Executing script in environment: {env_name}")
298
+ execution_result = self.code_runner.execute(python_script, env_name, input_data)
299
+
300
+ if execution_result.success:
301
+ print(f"✅ Script execution successful. Registering tool: {mcp_spec_obj.name}")
302
+ mcp_spec_obj.validated_script = python_script
303
+ mcp_spec_obj.environment_script = env_script
304
+ self.registry.register_tool(mcp_spec_obj)
305
+ print(f"🎯 Tool '{mcp_spec_obj.name}' successfully registered in registry")
306
+
307
+ # Add success message to result
308
+ execution_result.output_data = execution_result.output_data or {}
309
+ execution_result.output_data["registration_status"] = "Successfully registered"
310
+
311
+ else:
312
+ print(f"❌ Script execution failed for '{mcp_spec_obj.name}': {execution_result.error_message}")
313
+
314
+ # Always cleanup after validation
315
+ self.code_runner.cleanup_environment(env_name)
316
+ return execution_result.to_dict()
317
+
318
+ except Exception as e:
319
+ error_msg = f"Unexpected error in MCP execution: {str(e)}"
320
+ print(f"🚨 {error_msg}")
321
+
322
+ # Cleanup on error
323
+ try:
324
+ if 'env_name' in locals():
325
+ self.code_runner.cleanup_environment(env_name)
326
+ except:
327
+ pass
328
+
329
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
330
+
331
+ def _run_registered_mcp(self, tool_name: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
332
+ """Enhanced registered tool execution with better logging"""
333
+ print(f"🎯 ManagerAgent: Running registered tool: {tool_name}")
334
+
335
+ spec = self.registry.get_tool(tool_name)
336
+ if not spec:
337
+ error_msg = f"Tool '{tool_name}' not found in registry. Available tools: {list(self.registry.tools.keys())}"
338
+ print(f"❌ {error_msg}")
339
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
340
+
341
+ if not spec.validated_script or not spec.environment_script:
342
+ error_msg = f"Tool '{tool_name}' missing validated script or environment configuration"
343
+ print(f"❌ {error_msg}")
344
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
345
+
346
+ # Create fresh environment for execution
347
+ env_name_suffix = spec.name.lower().replace(' ', '-')[:10]
348
+ env_name = f"alita-run-{env_name_suffix}-{uuid.uuid4().hex[:8]}"
349
+
350
+ try:
351
+ print(f"🔄 Setting up execution environment: {env_name}")
352
+ env_success = self.code_runner.setup_environment(spec.environment_script, env_name)
353
+
354
+ if not env_success:
355
+ return MCPExecutionResult(
356
+ success=False,
357
+ error_message=f"Failed to setup environment for tool '{tool_name}'"
358
+ ).to_dict()
359
+
360
+ print(f"▶️ Executing registered tool: {tool_name}")
361
+ execution_result = self.code_runner.execute(spec.validated_script, env_name, input_data)
362
+
363
+ print(f"{'✅' if execution_result.success else '❌'} Tool execution completed. Success: {execution_result.success}")
364
+
365
+ return execution_result.to_dict()
366
+
367
+ except Exception as e:
368
+ error_msg = f"Error executing registered tool '{tool_name}': {str(e)}"
369
+ print(f"🚨 {error_msg}")
370
+ return MCPExecutionResult(success=False, error_message=error_msg).to_dict()
371
+
372
+ finally:
373
+ # Always cleanup
374
+ try:
375
+ self.code_runner.cleanup_environment(env_name)
376
+ except:
377
+ pass
378
+
379
+ def run_task(self, prompt: TaskPrompt) -> str:
380
+ """
381
+ Enhanced task execution with detailed logging and structured workflow
382
+ Optimized for Gradio integration with comprehensive responses
383
+ """
384
+ print(f"\n{'='*60}")
385
+ print(f"🚀 ALITA ManagerAgent: Starting task execution")
386
+ print(f"📝 User prompt: {prompt.text[:100]}{'...' if len(prompt.text) > 100 else ''}")
387
+ print(f"{'='*60}")
388
+
389
+ # Send initial update to the user
390
+ self.send_update(f"Starting to process your request: '{prompt.text[:50]}{'...' if len(prompt.text) > 50 else ''}'")
391
+
392
+ try:
393
+ # Use the internal ReAct agent to handle the complete workflow
394
+ print("🧠 Engaging ReAct Agent for intelligent task orchestration...")
395
+
396
+ # The ReAct agent will use its tools to:
397
+ # 1. Analyze the request
398
+ # 2. Search existing tools
399
+ # 3. Perform web research if needed
400
+ # 4. Brainstorm solutions
401
+ # 5. Create/execute tools as necessary
402
+ # 6. Provide comprehensive response
403
+
404
+ response = self.agent.chat(prompt.text)
405
+
406
+ print("✅ Task execution completed successfully")
407
+ print(f"{'='*60}\n")
408
+
409
+ # Send final update to the user
410
+ self.send_update("Task completed successfully! Here's your response.")
411
+
412
+ # Format response for better Gradio presentation
413
+ formatted_response = self._format_response_for_gradio(response.response)
414
+ return formatted_response
415
+
416
+ except Exception as e:
417
+ error_msg = f"🚨 ManagerAgent encountered an error during task execution:\n\n**Error Details:**\n{str(e)}\n\n**Next Steps:**\n- Check your API key and network connection\n- Verify all components are properly initialized\n- Try a simpler request to test basic functionality"
418
+
419
+ print(f"❌ Task execution failed: {e}")
420
+ print(f"{'='*60}\n")
421
+
422
+ # Send error update to the user
423
+ self.send_update(f"An error occurred while processing your request: {str(e)}")
424
+
425
+ return error_msg
426
+
427
+ def _format_response_for_gradio(self, response: str) -> str:
428
+ """Format the agent response for better presentation in Gradio"""
429
+
430
+ # Add header if not present
431
+ if not response.startswith("##") and not response.startswith("#"):
432
+ response = f"## 🤖 {response}"
433
+
434
+
435
+
436
+ return response
437
+
438
+ def get_registry_status(self) -> Dict[str, Any]:
439
+ """Get current status of the tool registry"""
440
+ return {
441
+ "total_tools": len(self.registry.tools),
442
+ "tool_names": list(self.registry.tools.keys()),
443
+ "registry_ready": len(self.registry.tools) > 0
444
+ }
445
+
446
+ def reset_registry(self):
447
+ """Reset the tool registry (useful for testing)"""
448
+ self.registry = Registry()
449
+ print("🔄 Tool registry has been reset")
450
+
451
+ def __str__(self):
452
+ return f"ManagerAgent(llm={type(self.llm).__name__}, tools_registered={len(self.registry.tools)})"
453
+
454
+ def research(self, query: str, max_iterations: int = None, verbose: bool = None) -> str:
455
+ """
456
+ Performs autonomous web research on the given query using the WebAgent's research function.
457
+
458
+ Args:
459
+ query: The research question or topic
460
+ max_iterations: Optional override for the maximum number of research iterations
461
+ verbose: Optional override for verbose mode
462
+
463
+ Returns:
464
+ A comprehensive textual report based on web research
465
+ """
466
+ print(f"\n{'='*60}")
467
+ print(f"🌐 ALITA ManagerAgent: Starting web research")
468
+ print(f"📝 Research query: {query[:100]}{'...' if len(query) > 100 else ''}")
469
+ print(f"{'='*60}")
470
+
471
+ try:
472
+ # Configure WebAgent for this research session
473
+ if max_iterations is not None:
474
+ self.web_agent.max_research_iterations = max_iterations
475
+
476
+ if verbose is not None:
477
+ self.web_agent.verbose = verbose
478
+
479
+ # Perform the research
480
+ print("🔍 Initiating autonomous web research. This may take some time... here is the query: ", query)
481
+ report = self.web_agent.research(query)
482
+ print("🔍 here is the report: ", report)
483
+
484
+ print("✅ Research completed successfully")
485
+ print(f"{'='*60}\n")
486
+
487
+ return report
488
+
489
+ except Exception as e:
490
+ error_msg = f"���� Error during web research: {str(e)}"
491
+ print(f"❌ Research failed: {e}")
492
+ print(f"{'='*60}\n")
493
+
494
+ import traceback
495
+ print(traceback.format_exc())
496
+
497
+ return error_msg
498
+
499
+ def get_available_tools(self) -> List[Dict[str, Any]]:
500
+ """
501
+ Get a list of all tools currently available in the registry.
502
+
503
+ Returns:
504
+ List of dictionaries containing tool information (name, description, state)
505
+ """
506
+ print("📋 ManagerAgent: Retrieving list of all available tools")
507
+ tools = self.registry.list_tools()
508
+
509
+ # Format the tools for easier consumption by the agent
510
+ formatted_tools = []
511
+ for tool in tools:
512
+ formatted_tools.append({
513
+ "name": tool.name,
514
+ "description": tool.description,
515
+ "state": getattr(tool, "state", "unknown"),
516
+ "input_schema": tool.input_schema if hasattr(tool, "input_schema") else {},
517
+ "output_schema": tool.output_schema if hasattr(tool, "output_schema") else {}
518
+ })
519
+
520
+ print(f"🔍 Found {len(formatted_tools)} tools in registry")
521
+ return formatted_tools
522
+
523
+ def deploy_tool(self, tool_name: str) -> Dict[str, Any]:
524
+ """
525
+ Deploy and activate a specific tool from the registry.
526
+
527
+ Args:
528
+ tool_name: Name of the tool to deploy
529
+
530
+ Returns:
531
+ Dictionary with deployment status and URL (if successful)
532
+ """
533
+ print(f"🚀 ManagerAgent: Deploying tool '{tool_name}'")
534
+
535
+ # Check if tool exists in registry
536
+ if not self.registry.get_tool(tool_name):
537
+ error_msg = f"Tool '{tool_name}' not found in registry"
538
+ print(f"❌ {error_msg}")
539
+ return {"success": False, "error": error_msg}
540
+
541
+ # Attempt to deploy the tool
542
+ try:
543
+ url = self.registry.deploy_tool(tool_name)
544
+
545
+ if url:
546
+ print(f"✅ Successfully deployed tool '{tool_name}' at {url}")
547
+ return {
548
+ "success": True,
549
+ "tool_name": tool_name,
550
+ "url": url,
551
+ "message": f"Tool '{tool_name}' successfully deployed"
552
+ }
553
+ else:
554
+ error_msg = f"Failed to deploy tool '{tool_name}'"
555
+ print(f"❌ {error_msg}")
556
+ return {"success": False, "error": error_msg}
557
+
558
+ except Exception as e:
559
+ error_msg = f"Error deploying tool '{tool_name}': {str(e)}"
560
+ print(f"🚨 {error_msg}")
561
+ return {"success": False, "error": error_msg}
562
+
563
+ def brainstorm_tools(self, user_task: str, available_tools: str = "") -> Dict[str, Any]:
564
+ """
565
+ Use the Brainstormer to analyze if existing tools are sufficient or new tools are needed.
566
+
567
+ Args:
568
+ user_task: The user's request or task
569
+ available_tools: Optional comma-separated list of available tool names
570
+
571
+ Returns:
572
+ Dictionary with tool recommendations or specifications for new tools
573
+ """
574
+ print(f"🧠 ManagerAgent: Brainstorming tools for task: {user_task[:100]}{'...' if len(user_task) > 100 else ''}")
575
+
576
+ # If available_tools is not provided, get them from the registry
577
+ if not available_tools:
578
+ tools = self.get_available_tools()
579
+ available_tools = ", ".join([tool["name"] for tool in tools])
580
+
581
+ try:
582
+ # Call the brainstormer to analyze the task and available tools
583
+ result = self.brainstormer.generate_mcp_specs_to_fulfill_user_task(
584
+ task=user_task,
585
+ tools_list=available_tools
586
+ )
587
+
588
+ if isinstance(result, dict) and "error" in result:
589
+ print(f"❌ Brainstorming failed: {result['error']}")
590
+ return {
591
+ "success": False,
592
+ "error": result["error"],
593
+ "recommendations": "Unable to analyze tools for this task."
594
+ }
595
+
596
+ print(f"✅ Brainstorming complete. Found {len(result)} tool recommendations.")
597
+
598
+ # Format the result for better consumption by the agent
599
+ return {
600
+ "success": True,
601
+ "recommendations": result,
602
+ "summary": f"Analysis complete. Found {len(result)} tool recommendations."
603
+ }
604
+
605
+ except Exception as e:
606
+ error_msg = f"Error during tool brainstorming: {str(e)}"
607
+ print(f"🚨 {error_msg}")
608
+ return {
609
+ "success": False,
610
+ "error": error_msg,
611
+ "recommendations": "Unable to analyze tools due to an error."
612
+ }
613
+
614
+ def use_registry_tool(self, tool_name: str, *args, **kwargs) -> Dict[str, Any]:
615
+ """
616
+ Use a registered tool directly by invoking its endpoint.
617
+
618
+ This method utilizes the Registry's use_tool method to invoke a registered tool.
619
+ It handles tool deployment if needed and provides proper error handling and user feedback.
620
+
621
+ Args:
622
+ tool_name: Name of the tool to use
623
+ *args: Positional arguments to pass to the tool
624
+ **kwargs: Keyword arguments to pass to the tool
625
+
626
+ Returns:
627
+ The response from the tool as a Python object
628
+ """
629
+ try:
630
+ # Send update to user
631
+ self.send_update(f"Using tool: {tool_name}")
632
+
633
+ # Check if tool exists in registry
634
+ if not self.registry.get_tool(tool_name):
635
+ error_msg = f"Tool '{tool_name}' not found in registry"
636
+ self.send_update(error_msg)
637
+ return {"error": error_msg, "success": False}
638
+
639
+ # Use the tool via Registry's use_tool method
640
+ self.send_update(f"Executing tool: {tool_name}")
641
+ result = self.registry.use_tool(tool_name, *args, **kwargs)
642
+
643
+ # Send success update
644
+ self.send_update(f"Tool '{tool_name}' executed successfully")
645
+
646
+ # Return result with success flag
647
+ if isinstance(result, dict):
648
+ result["success"] = True
649
+ return result
650
+ else:
651
+ return {"result": result, "success": True}
652
+
653
+ except ValueError as e:
654
+ # Handle expected errors (tool not found, deployment failed)
655
+ error_msg = str(e)
656
+ self.send_update(f"Error: {error_msg}")
657
+ return {"error": error_msg, "success": False}
658
+
659
+ except Exception as e:
660
+ # Handle unexpected errors
661
+ error_msg = f"Unexpected error using tool '{tool_name}': {str(e)}"
662
+ self.send_update(f"Error: {error_msg}")
663
+ return {"error": error_msg, "success": False}
requirements.txt CHANGED
@@ -1 +1,22 @@
1
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ openai
3
+ llama-index>=0.11.0
4
+ anthropic
5
+ requests
6
+ python-dotenv
7
+ dataclasses
8
+ beautifulsoup4
9
+ duckduckgo-search
10
+ llama-index-llms-anthropic
11
+ modal
12
+ llama-index-core>=0.10.0
13
+ llama-index-readers-web>=0.1.0
14
+ google-api-python-client>=2.70.0
15
+ PyGithub>=1.58.0
16
+ PyPDF2>=3.0.0
17
+ python-docx>=0.8.11
18
+ python-pptx>=0.6.21
19
+ urllib3>=1.26.0
20
+ pathlib>=1.0.1
21
+ argparse>=1.4.0
22
+ llama-index-tools-mcp
task_prompt.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+
3
+ @dataclass
4
+ class TaskPrompt:
5
+ """
6
+ Represents the initial user query or task description.
7
+ """
8
+ text: str
9
+ # Potentially add other fields like context, constraints, etc.
test_research.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Test script to demonstrate using the ManagerAgent's research function
3
+ """
4
+ import os
5
+ from dotenv import load_dotenv
6
+ from llama_index.llms.anthropic import Anthropic
7
+ from manager_agent import ManagerAgent
8
+ from models import TaskPrompt
9
+
10
+ # Load environment variables
11
+ load_dotenv()
12
+
13
+ # ANSI color codes for prettier output
14
+ COLOR_RESET = "\033[0m"
15
+ COLOR_CYAN = "\033[96m"
16
+ COLOR_GREEN = "\033[92m"
17
+ COLOR_YELLOW = "\033[93m"
18
+ COLOR_RED = "\033[91m"
19
+
20
+ def color_text(text, color):
21
+ return f"{color}{text}{COLOR_RESET}"
22
+
23
+ def main():
24
+ # Check if API key is available
25
+ api_key = os.environ.get("ANTHROPIC_API_KEY")
26
+ if not api_key:
27
+ print(color_text("Error: ANTHROPIC_API_KEY not found in environment variables.", COLOR_RED))
28
+ print("Please set your Anthropic API key with:")
29
+ print(" export ANTHROPIC_API_KEY='your-api-key'")
30
+ print(" or create a .env file with ANTHROPIC_API_KEY=your-api-key")
31
+ return
32
+
33
+ # Initialize LLM
34
+ print(color_text("Initializing Anthropic Claude...", COLOR_CYAN))
35
+ llm = Anthropic(model="claude-3-5-sonnet-20241022", api_key=api_key)
36
+
37
+ # Initialize ManagerAgent
38
+ print(color_text("Creating ManagerAgent...", COLOR_CYAN))
39
+ manager = ManagerAgent(llm=llm)
40
+
41
+ print(color_text("\nTest 1: Using research function directly", COLOR_GREEN))
42
+ query = "What are the latest developments in AI agents and autonomous systems?"
43
+ print(color_text(f"Research Query: {query}", COLOR_YELLOW))
44
+
45
+ # Call research function directly
46
+ report = manager.research(query=query, verbose=True)
47
+ print(color_text("\n=== Research Report ===", COLOR_GREEN))
48
+ print(report)
49
+
50
+ print(color_text("\nTest 2: Using research as a tool through the agent", COLOR_GREEN))
51
+ prompt_text = "I need a comprehensive report on recent developments in quantum computing. Please research this topic thoroughly."
52
+ print(color_text(f"User Prompt: {prompt_text}", COLOR_YELLOW))
53
+
54
+ # Create task prompt
55
+ task_prompt = TaskPrompt(text=prompt_text)
56
+
57
+ # Run through agent
58
+ response = manager.run_task(task_prompt)
59
+ print(color_text("\n=== Agent Response ===", COLOR_GREEN))
60
+ print(response)
61
+
62
+ if __name__ == "__main__":
63
+ main()