File size: 8,206 Bytes
8c95c5b
 
 
 
 
 
 
 
04088fa
8c95c5b
04088fa
c53556a
a198e7f
2660e3b
adefabd
649b73c
 
 
2660e3b
649b73c
 
 
c53556a
 
8c95c5b
 
c53556a
8c95c5b
 
2660e3b
8c95c5b
c53556a
2660e3b
 
 
 
 
af7fd27
 
2660e3b
8c95c5b
c53556a
feece21
 
 
c53556a
a198e7f
c53556a
 
2660e3b
c53556a
 
 
2660e3b
c53556a
 
2660e3b
c53556a
 
 
 
0fd28d7
2660e3b
c53556a
2660e3b
c53556a
2660e3b
 
 
c53556a
 
2660e3b
2701cf1
2660e3b
 
c53556a
2660e3b
 
c53556a
2660e3b
c53556a
 
2701cf1
c53556a
 
2660e3b
 
 
 
c53556a
2660e3b
8c95c5b
649b73c
2660e3b
 
adefabd
2701cf1
 
0fd28d7
 
2701cf1
c53556a
2660e3b
 
 
 
adefabd
2701cf1
0fd28d7
2701cf1
 
16660d2
2660e3b
c53556a
2660e3b
c53556a
2660e3b
649b73c
2660e3b
 
c53556a
16660d2
adefabd
2660e3b
adefabd
2660e3b
649b73c
 
2701cf1
 
16660d2
2701cf1
 
 
0fd28d7
2701cf1
0fd28d7
 
2660e3b
 
 
 
 
2701cf1
 
0fd28d7
2660e3b
649b73c
2701cf1
2660e3b
0fd28d7
2701cf1
 
 
 
 
2660e3b
8c95c5b
c53556a
04088fa
 
2701cf1
04088fa
 
2701cf1
2660e3b
 
 
8c95c5b
04088fa
c53556a
 
8c95c5b
2660e3b
c53556a
 
 
 
2660e3b
c53556a
 
 
 
 
2701cf1
2660e3b
c53556a
 
 
2660e3b
 
c53556a
 
2660e3b
c53556a
 
2660e3b
c53556a
 
 
 
 
20a6eaf
2701cf1
c53556a
2701cf1
c53556a
 
20a6eaf
c53556a
 
04088fa
af7fd27
a198e7f
04088fa
feece21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import sys
import logging
import time
import json
import traceback
import threading
from concurrent.futures import ThreadPoolExecutor
import requests
from google import genai
from flask import Flask
from google.genai import types

# --- Global Storage ---
chat_histories = {}
user_settings = {}

def get_settings(chat_id):
    """Retrieve user settings or set defaults."""
    if chat_id not in user_settings:
        user_settings[chat_id] = {"thinking": "medium", "afc": False}
    return user_settings[chat_id]

# --- Configuration ---
BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
BALE_API_URL = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}"

if not BALE_BOT_TOKEN or not GOOGLE_API_KEY:
    sys.exit("Error: Please set BALE_BOT_TOKEN and GOOGLE_API_KEY environment variables.")

# --- Logging ---
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[logging.StreamHandler(sys.stdout)]
)
logger = logging.getLogger(__name__)

# --- AI Client Setup ---
ai_client = genai.Client(api_key=GOOGLE_API_KEY)
executor = ThreadPoolExecutor(max_workers=5)
app = Flask(__name__)

@app.route('/')
def health(): return "Bot is alive", 200

# --- Helper: Generate Keyboard ---
def get_keyboard(chat_id):
    """Generates the inline keyboard based on current user settings."""
    settings = get_settings(chat_id)
    think_label = f"🧠 Thinking: {settings['thinking'].upper()}"
    afc_label = f"πŸ€– AFC: {'ON' if settings['afc'] else 'OFF'}"
    
    return {
        "inline_keyboard": [
            [{"text": "πŸ—‘οΈ New Chat (Reset Memory)", "callback_data": "reset_chat"}],
            [{"text": think_label, "callback_data": "toggle_think"}],
            [{"text": afc_label, "callback_data": "toggle_afc"}]
        ]
    }

# --- Helper: Communication ---
def send_bale_message(chat_id, text):
    """Sends a new message to the user with the settings keyboard."""
    try:
        payload = {
            "chat_id": chat_id,
            "text": text,
            "reply_markup": get_keyboard(chat_id),
            "parse_mode": "Markdown"
        }
        requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=15)
    except Exception as e:
        logger.error(f"Error sending message: {e}")

def edit_bale_buttons(chat_id, message_id, original_text):
    """Updates only the buttons on an existing message to show status changes."""
    try:
        payload = {
            "chat_id": chat_id,
            "message_id": message_id,
            "text": original_text,
            "reply_markup": get_keyboard(chat_id),
            "parse_mode": "Markdown"
        }
        requests.post(f"{BALE_API_URL}/editMessageText", json=payload, timeout=10)
    except Exception as e:
        logger.error(f"Error editing buttons: {e}")

# --- AI Core Logic ---
def process_single_message(chat_id, text):
    settings = get_settings(chat_id)
    if chat_id not in chat_histories:
        chat_histories[chat_id] = []

    # Retry Strategy: 1 & 2 on 31B, 3 on 26B fallback
    attempts_list = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
    response = None
    selected_model = None
    final_attempt_num = 1 # Track which attempt actually worked

    # Build prompt with history context
    current_req_content = chat_histories[chat_id] + [
        types.Content(role="user", parts=[types.Part(text=text)])
    ]

    for idx, model_name in enumerate(attempts_list):
        selected_model = model_name
        final_attempt_num = idx + 1
        
        try:
            # Configure Thinking level
            t_cfg = types.ThinkingConfig(thinking_level=settings["thinking"]) if settings["thinking"] != "off" else None
            # Configure Internet Search (AFC)
            tools_cfg = [types.Tool(google_search=types.GoogleSearch())] if settings["afc"] else []

            config = types.GenerateContentConfig(
                thinking_config=t_cfg,
                tools=tools_cfg,
                automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=not settings["afc"])
            )
            
            response = ai_client.models.generate_content(model=selected_model, contents=current_req_content, config=config)
            
            # Save success to history
            chat_histories[chat_id].append(types.Content(role="user", parts=[types.Part(text=text)]))
            chat_histories[chat_id].append(response.candidates[0].content)
            break # Exit loop on success
            
        except Exception as e:
            logger.warning(f"Attempt {final_attempt_num}/3 failed for {selected_model}: {e}")
            if idx < 2: 
                time.sleep(2) # Wait before retry
            else:
                send_bale_message(chat_id, "❌ All AI attempts failed (Google Server Overload). Please try again in a few seconds.")
                return

    # Parse Thought vs Answer
    parts = response.candidates[0].content.parts
    thought_text = "".join([p.text for p in parts if getattr(p, 'thought', False)]).strip()
    answer_text = "".join([p.text for p in parts if not getattr(p, 'thought', False)]).strip()
    
    if not answer_text: 
        answer_text = response.text or "Model produced an empty result."

    # 1. Send Thinking Block (if applicable)
    if thought_text and settings["thinking"] != "off":
        send_bale_message(chat_id, f"🧠 *Thinking ({settings['thinking']}):*\n_{thought_text[:1500]}_")
        time.sleep(0.6)

    # 2. Send Final Answer with Attempt Metadata
    status_footer = (
        f"\n\nβš™οΈ _{selected_model} | **Attempt {final_attempt_num}/3** | "
        f"Think: {settings['thinking']} | AFC: {'On' if settings['afc'] else 'Off'}_"
    )
    send_bale_message(chat_id, answer_text + status_footer)

# --- Polling Loop ---
def poll_updates():
    offset = None
    logger.info("Bot is polling...")
    while True:
        try:
            resp = requests.post(f"{BALE_API_URL}/getUpdates", json={"timeout": 30, "offset": offset}, timeout=40)
            if resp.status_code != 200:
                time.sleep(5)
                continue
            
            data = resp.json()
            for upd in data.get("result", []):
                offset = upd["update_id"] + 1
                
                # A. Handle Buttons (Callback Queries)
                if "callback_query" in upd:
                    cb = upd["callback_query"]
                    chat_id = cb["message"]["chat"]["id"]
                    msg_id = cb["message"]["message_id"]
                    orig_text = cb["message"].get("text", "")
                    cmd = cb.get("data")
                    s = get_settings(chat_id)

                    if cmd == "reset_chat":
                        chat_histories[chat_id] = []
                        send_bale_message(chat_id, "βœ… *Memory Cleared!* Starting fresh.")
                    
                    elif cmd == "toggle_think":
                        modes = ["off", "low", "medium", "high"]
                        s["thinking"] = modes[(modes.index(s["thinking"]) + 1) % 4]
                        edit_bale_buttons(chat_id, msg_id, orig_text)
                    
                    elif cmd == "toggle_afc":
                        s["afc"] = not s["afc"]
                        edit_bale_buttons(chat_id, msg_id, orig_text)
                    continue

                # B. Handle Text Messages
                msg = upd.get("message")
                if not msg or "text" not in msg: continue
                
                chat_id = msg["chat"]["id"]
                text = msg["text"]

                if text in ["/start", "/reset"]:
                    chat_histories[chat_id] = []
                    send_bale_message(chat_id, "πŸ‘‹ Welcome! How can I help you today?")
                else:
                    executor.submit(process_single_message, chat_id, text)

        except Exception:
            logger.error(traceback.format_exc())
            time.sleep(5)

if __name__ == "__main__":
    threading.Thread(target=poll_updates, daemon=True).start()
    app.run(host="0.0.0.0", port=7860)