def run_agent_once(user_message, client=None, collection=None, user_id=None, max_hits=5): """ Full agent pipeline for a single user message. Extract intent, plan the next step, call the tool, store the memory, and return the final formatted answer. """ intent = extract_intent(user_message) plan = planner_decide_tools(intent) raw_results = [] if plan.get("use_openlibrary"): raw_results = fetch_openlibrary_by_title_or_subject(user_message, limit=max_hits) if client and collection and raw_results: store_to_memory(client, collection, user_message, raw_results, user_id=user_id) final_message = format_recommendations(raw_results) return { "intent": intent, "plan": plan, "raw_results": raw_results, "formatted": final_message } # to test # from ai_agent import init_chromadb, run_agent_once client, collection = init_chromadb(".agent_memory_test") response = run_agent_once("Suggest books about habits", client, collection, user_id="test_user") print("Intent:", response["intent"]) print("Formatted:", response["formatted"])