Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -385,49 +385,51 @@ mcp_agent = MCPAgent(sse_url, HF_TOKEN, llm)
|
|
| 385 |
chat_history = []
|
| 386 |
|
| 387 |
def format_tool_call(tool_name, arguments):
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
|
| 390 |
def format_tool_result(tool_name, result):
|
| 391 |
-
|
|
|
|
|
|
|
| 392 |
|
| 393 |
async def chat_with_agent(message, history):
|
| 394 |
history = history or []
|
| 395 |
|
| 396 |
-
# Agregar
|
| 397 |
-
history.append((message,
|
| 398 |
-
yield history
|
| 399 |
|
| 400 |
-
# Variables para
|
| 401 |
-
|
| 402 |
-
last_tool_result = None
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
if
|
| 409 |
-
|
| 410 |
-
history.append((None, tool_msg))
|
| 411 |
-
last_tool_call = (event["tool_name"], event["arguments"])
|
| 412 |
-
yield history
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
result_msg = format_tool_result(event["tool_name"], event["result"])
|
| 417 |
-
history.append((None, result_msg))
|
| 418 |
-
last_tool_result = (event["tool_name"], event["result"])
|
| 419 |
-
yield history
|
| 420 |
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
yield history
|
| 432 |
|
| 433 |
|
|
|
|
| 385 |
chat_history = []
|
| 386 |
|
| 387 |
def format_tool_call(tool_name, arguments):
|
| 388 |
+
message = f"🔧 **Llamando a herramienta**: `{tool_name}`\n```json\n{arguments}\n```"
|
| 389 |
+
print(message) # Solo imprime en consola
|
| 390 |
+
return "" # Devuelve cadena vacía para no mostrar en chat
|
| 391 |
|
| 392 |
def format_tool_result(tool_name, result):
|
| 393 |
+
message = f"✅ **Resultado de `{tool_name}`**\n```\n{result}\n```"
|
| 394 |
+
print(message) # Solo imprime en consola
|
| 395 |
+
return "" # Devuelve cadena vacía para no mostrar en chat
|
| 396 |
|
| 397 |
async def chat_with_agent(message, history):
|
| 398 |
history = history or []
|
| 399 |
|
| 400 |
+
# Agregar mensaje del usuario y marcador de "escribiendo"
|
| 401 |
+
history.append((message, None)) # None activa el indicador de typing
|
| 402 |
+
yield history
|
| 403 |
|
| 404 |
+
# Variables para seguimiento
|
| 405 |
+
processing_message = None
|
|
|
|
| 406 |
|
| 407 |
+
try:
|
| 408 |
+
# Procesar con el agente
|
| 409 |
+
full_response = ""
|
| 410 |
+
async for event in mcp_agent.stream_response(message):
|
| 411 |
+
if event["type"] == "tool_call":
|
| 412 |
+
format_tool_call(event["tool_name"], event["arguments"])
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
+
elif event["type"] == "tool_result":
|
| 415 |
+
format_tool_result(event["tool_name"], event["result"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
+
elif event["type"] == "final_response":
|
| 418 |
+
full_response = event["content"]
|
| 419 |
+
|
| 420 |
+
# Actualizar indicador de actividad periódicamente
|
| 421 |
+
if not processing_message or len(history[-1][1] or "") > len(processing_message):
|
| 422 |
+
processing_message = "✍️ Generando respuesta..."
|
| 423 |
+
history[-1] = (history[-1][0], processing_message)
|
| 424 |
+
yield history
|
| 425 |
+
|
| 426 |
+
# Mostrar respuesta final
|
| 427 |
+
if full_response:
|
| 428 |
+
history[-1] = (history[-1][0], full_response)
|
| 429 |
+
yield history
|
| 430 |
+
|
| 431 |
+
except Exception as e:
|
| 432 |
+
history[-1] = (history[-1][0], f"⚠️ Error: {str(e)}")
|
| 433 |
yield history
|
| 434 |
|
| 435 |
|