Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -393,27 +393,42 @@ def format_tool_result(tool_name, result):
|
|
| 393 |
async def chat_with_agent(message, history):
|
| 394 |
history = history or []
|
| 395 |
|
| 396 |
-
# Agregar mensaje del usuario al historial
|
| 397 |
history.append((message, ""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
|
| 399 |
# Procesar con el agente
|
| 400 |
full_response = ""
|
| 401 |
async for event in mcp_agent.stream_response(message):
|
| 402 |
if event["type"] == "tool_call":
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
| 406 |
elif event["type"] == "tool_result":
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
| 410 |
elif event["type"] == "final_response":
|
| 411 |
full_response = event["content"]
|
| 412 |
|
| 413 |
-
#
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
|
| 419 |
|
|
|
|
| 393 |
async def chat_with_agent(message, history):
|
| 394 |
history = history or []
|
| 395 |
|
| 396 |
+
# Agregar solo el mensaje del usuario al historial
|
| 397 |
history.append((message, ""))
|
| 398 |
+
yield history # Mostrar inmediatamente el mensaje del usuario
|
| 399 |
+
|
| 400 |
+
# Variables para rastrear el último evento mostrado
|
| 401 |
+
last_tool_call = None
|
| 402 |
+
last_tool_result = None
|
| 403 |
|
| 404 |
# Procesar con el agente
|
| 405 |
full_response = ""
|
| 406 |
async for event in mcp_agent.stream_response(message):
|
| 407 |
if event["type"] == "tool_call":
|
| 408 |
+
if last_tool_call != (event["tool_name"], event["arguments"]):
|
| 409 |
+
tool_msg = format_tool_call(event["tool_name"], event["arguments"])
|
| 410 |
+
history.append((None, tool_msg))
|
| 411 |
+
last_tool_call = (event["tool_name"], event["arguments"])
|
| 412 |
+
yield history
|
| 413 |
+
|
| 414 |
elif event["type"] == "tool_result":
|
| 415 |
+
if last_tool_result != (event["tool_name"], event["result"]):
|
| 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 |
elif event["type"] == "final_response":
|
| 422 |
full_response = event["content"]
|
| 423 |
|
| 424 |
+
# Actualizar solo si hay una respuesta final
|
| 425 |
+
if full_response:
|
| 426 |
+
# Buscar y reemplazar el último mensaje vacío
|
| 427 |
+
for i in reversed(range(len(history))):
|
| 428 |
+
if history[i][1] == "":
|
| 429 |
+
history[i] = (history[i][0], full_response)
|
| 430 |
+
break
|
| 431 |
+
yield history
|
| 432 |
|
| 433 |
|
| 434 |
|