geronimo-pericoli commited on
Commit
70b37bc
·
verified ·
1 Parent(s): 61a6edd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
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
- return f"🔧 **Llamando a herramienta**: `{tool_name}`\n```json\n{arguments}\n```"
 
 
389
 
390
  def format_tool_result(tool_name, result):
391
- return f"✅ **Resultado de `{tool_name}`**\n```\n{result}\n```"
 
 
392
 
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
 
 
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