Update main.py
Browse files11 unmodified lines1213141516171812 unmodified lines313233343536373839404142434445464748495051525354555657585960617 unmodified lines69707172737475767778798081828311 unmodified linesapp = FastAPI()# --- MEMORY BANK ---# This dictionary remembers which engine you are currently using.user_engines = {}# --- NATIVE AGENT ENGINE ---12 unmodified linesasync def process_and_reply(chat_id: int, text: str):user_msg = text.strip().lower()# 1. Check if the user is pulling the Gear Shifter (Slash Commands)if user_msg == "/pro":user_engines[chat_id] = "gemini/gemini-1.5-pro"response_text = "🧠 **Engine Switched: Gemini 1.5 Pro**\n_You are now using the heavy-duty coding engine (50 free requests/day)._"elif user_msg == "/flash":user_engines[chat_id] = "gemini/gemini-1.5-flash"response_text = "⚡ **Engine Switched: Gemini 1.5 Flash**\n_You are now using the ultra-fast casual engine (1,500 free requests/day)._"# 2. If it's a normal message, route it to the currently active engineelse:# Default to Flash if we haven't set an engine yet to protect the Pro quotacurrent_model = user_engines.get(chat_id, "gemini/gemini-1.5-flash")response_text = await run_agent(text, current_model)# Send the final response back to Telegramasync with httpx.AsyncClient() as client:await client.post(f"{TELEGRAM_API_URL}/sendMessage",json={"chat_id": chat_id,"text": response_text,"parse_mode": "Markdown"})# --- ENDPOINTS ---@app.get("/health")7 unmodified linestry:data = await request.json()# Extract the message from Telegram's JSON payloadif "message" in data and "text" in data["message"]:chat_id = data["message"]["chat"]["id"]user_msg = data["message"]["text"]# Run in the backgroundbackground_tasks.add_task(process_and_reply, chat_id, user_msg)return {"status": "ok"}except Exception as e:return {"status": f"error reading json: {str(e)}"}11 unmodified lines12131415161712 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465667 unmodified lines7475767778798081828311 unmodified linesapp = FastAPI()# --- MEMORY BANK ---user_engines = {}# --- NATIVE AGENT ENGINE ---12 unmodified linesasync def process_and_reply(chat_id: int, text: str):user_msg = text.strip().lower()async with httpx.AsyncClient() as client:# 1. IMMEDIATE UX FEEDBACK: Trigger "Phoebus is typing..."await client.post(f"{TELEGRAM_API_URL}/sendChatAction",json={"chat_id": chat_id, "action": "typing"})# 2. SHIFT GEARSif user_msg == "/pro":user_engines[chat_id] = "gemini/gemini-1.5-pro"response_text = "🧠 Engine Switched: Gemini 1.5 Pro\n(50 free requests/day active)."elif user_msg == "/flash":user_engines[chat_id] = "gemini/gemini-1.5-flash"response_text = "⚡ Engine Switched: Gemini 1.5 Flash\n(1,500 free requests/day active)."# 3. NORMAL CHATelse:current_model = user_engines.get(chat_id, "gemini/gemini-1.5-flash")response_text = await run_agent(text, current_model)# 4. SEND REPLY (Removed markdown parser to stop Telegram from crashing)try:await client.post(f"{TELEGRAM_API_URL}/sendMessage",json={"chat_id": chat_id,"text": response_text})except Exception as e:print(f"DEBUG SEND ERROR: {e}")# --- ENDPOINTS ---@app.get("/health")7 unmodified linestry:data = await request.json()if "message" in data and "text" in data["message"]:chat_id = data["message"]["chat"]["id"]user_msg = data["message"]["text"]background_tasks.add_task(process_and_reply, chat_id, user_msg)return {"status": "ok"}except Exception as e:return {"status": f"error reading json: {str(e)}"}
main.py+28-28