diff --git a/src/context/engine.py b/src/context/engine.py index ccd6db4..767dd03 100644 --- a/src/context/engine.py +++ b/src/context/engine.py @@ -557,22 +557,36 @@ class ContextEngine: messages: list[dict[str, Any]] = [] - # Include previous task exchanges as conversation history - # (so the model remembers what was said in earlier turns) - for entry in session.task_history[-10:]: - summary = entry.get("summary", "") - objective = entry.get("objective", "") - if summary.startswith("User: "): - # Direct response format: "User: X → Agent: Y" - parts = summary.split(" → Agent: ", 1) - user_msg = objective or parts[0].replace("User: ", "", 1) - agent_msg = parts[1] if len(parts) > 1 else summary - messages.append({"role": "user", "content": user_msg}) - messages.append({"role": "assistant", "content": agent_msg}) - elif objective: - # Task with tools — include as compact exchange - messages.append({"role": "user", "content": objective}) - messages.append({"role": "assistant", "content": summary[:500] if summary else "Tarea completada."}) + # Include previous task exchanges as compact conversation history + if session.task_history: + history_lines = ["[HISTORIAL DE CONVERSACIÓN ANTERIOR — NO ejecutar de nuevo, solo contexto]"] + for entry in session.task_history[-10:]: + objective = entry.get("objective", "")[:200] + summary = entry.get("summary", "") + key_data = entry.get("key_data", {}) + tools = entry.get("tools_used", []) + + history_lines.append(f"Usuario pidió: {objective}") + if tools: + history_lines.append(f" Tools usadas: {', '.join(tools[:5])}") + if key_data: + kd_parts = [] + for table, nums in key_data.get("tables", {}).items(): + kd_parts.append(f"{table}: records {nums}") + if key_data.get("sections"): + kd_parts.append(f"sections: {key_data['sections'][:5]}") + if key_data.get("modules"): + kd_parts.append(f"modules: {key_data['modules'][:5]}") + if kd_parts: + history_lines.append(f" Datos clave: {'; '.join(kd_parts)}") + # Extract agent response from summary + if " → Agent: " in summary: + agent_part = summary.split(" → Agent: ", 1)[1][:200] + history_lines.append(f" Resultado: {agent_part}") + history_lines.append("") + + messages.append({"role": "user", "content": "\n".join(history_lines)}) + messages.append({"role": "assistant", "content": "Entendido, tengo el contexto del historial. ¿En qué puedo ayudarte ahora?"}) # Current user message messages.append({"role": "user", "content": user_content})