result ==> # Flowchart: Bale-Bot Backend System ## Main Application Flow ``` ┌─────────────────────────────────────┐ │ main.py │ └──────────┬────────────────────────┘ │ ▼ ┌─────────────────────────────────────┐ │ Lifespan Manager (Startup) │ │ ┌─────────────────────────────┐ │ │ │ Load .env variables │ │ │ │ Validate required vars │ │ │ │ Initialize ElasticHelper │ │ │ │ Set webhook URL │ │ │ │ Initialize UserManager │ │ │ │ Initialize Formatter │ │ │ │ Initialize RequestManager │ │ │ │ Initialize Operation │ │ │ │ Initialize BaleBotCore │ │ │ └─────────────────────────────┘ │ └──────────┬────────────────────────┘ │ ▼ ┌─────────────────────────────────────┐ │ Application Factory │ │ ┌─────────────────────────────┐ │ │ │ Create FastAPI app │ │ │ │ Add CORS middleware │ │ │ │ Add health routes (/, /ping)│ │ │ │ Include Bale router │ │ │ └─────────────────────────────┘ │ └──────────┬────────────────────────┘ │ ▼ ┌─────────────────────────────────────┐ │ FastAPI App Running │ └─────────────────────────────────────┘ ``` ## Core Classes Flow ### 1. ElasticHelper Class ``` ┌─────────────────────────────────────┐ │ ElasticHelper │ ├─────────────────────────────────────┤ │ __init__(es_url, es_pass, es_user) │ │ ┌─────────────────────────────┐ │ │ │ Connect to Elasticsearch │ │ │ │ Retry connection 10 times │ │ │ └─────────────────────────────┘ │ │ │ │ search(**params) │ │ get_document(index, id) │ │ exist_document(index, id) │ │ update_index_doc(is_update, ...) │ └─────────────────────────────────────┘ ``` ### 2. Formatter Class ``` ┌─────────────────────────────────────┐ │ Formatter │ ├─────────────────────────────────────┤ │ __init__(max_len) │ │ ┌─────────────────────────────┐ │ │ │ Initialize formatting tools │ │ │ │ Number mapping │ │ │ └─────────────────────────────┘ │ │ │ │ __getattr__(name) │ │ _bold(text) │ │ _number(value) │ │ _pretier1(text) │ │ __make_link_qq(src) │ │ __make_link_qs(src) │ │ │ │ form_search_in_law_rules(...) │ │ form_search_in_law(...) │ │ form_law_chat(answer_text) │ │ form_title_repeated(...) │ │ form_chat(llm_text, header) │ │ form_llm_answer_chat(...) │ │ form_subject_unity(...) │ │ structed_form_subject_unity(...) │ │ form_rule_making(...) │ │ get_asl(_in) │ │ get_in_form_single(asl, ...) │ │ form_constitution(input) │ │ form_constitution_low(...) │ │ form_ss_rules(...) │ │ form2_ss_rules(...) │ │ form_conflict_detection(...) │ │ form_conflict_type_detection(...) │ │ form_relation_identification(...) │ │ form_evaluation(...) │ │ from_law_writing_policy(...) │ └─────────────────────────────────────┘ ``` ### 3. RequestManager Class ``` ┌─────────────────────────────────────┐ │ RequestManager │ ├─────────────────────────────────────┤ │ __init__(host_url) │ │ ┌─────────────────────────────┐ │ │ │ Set base URL │ │ │ │ Define task URLs │ │ │ └─────────────────────────────┘ │ │ │ │ get_result(payload, url, ...) │ │ ┌─────────────────────────────┐ │ │ │ Async HTTP POST request │ │ │ │ Handle timeouts/errors │ │ │ └─────────────────────────────┘ │ │ │ │ stream_result(url, payload) │ │ ┌─────────────────────────────┐ │ │ │ Stream HTTP POST response │ │ │ │ Yield JSON lines │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘ ``` ### 4. Operation Class ``` ┌─────────────────────────────────────┐ │ Operation │ ├─────────────────────────────────────┤ │ __init__(request_manager) │ │ │ │ search_in_law(query, limit, ...) │ │ stream_search_in_law(...) │ │ stream_rule_making(...) │ │ stream_chat_in_law(...) │ │ stream_rule_semantic_search(...) │ │ chat_in_law(query, effort, ...) │ │ title_repeated(qanontitle, ...) │ │ talk(query) │ │ conflict_qanon_asasi_low(...) │ │ conflict_qanon_asasi_steps(...) │ │ stream_logical_chat_in_law(...) │ │ conflict_law_writing_policy(...) │ └─────────────────────────────────────┘ ``` ### 5. UserManager Class ``` ┌─────────────────────────────────────┐ │ UserManager │ ├─────────────────────────────────────┤ │ __init__() │ │ ┌─────────────────────────────┐ │ │ │ Load VIP usernames │ │ │ │ Load temporary data │ │ │ └─────────────────────────────┘ │ │ │ │ get_or_create(update) │ │ ┌─────────────────────────────┐ │ │ │ Extract user info │ │ │ │ Check VIP status │ │ │ │ Create/update user object │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘ ``` ### 6. BaleBotCore Class ``` ┌─────────────────────────────────────┐ │ BaleBotCore │ ├─────────────────────────────────────┤ │ __init__(...) │ │ ┌─────────────────────────────┐ │ │ │ Initialize UI handler │ │ │ │ Setup components │ │ │ └─────────────────────────────┘ │ │ │ │ render_user_state(user) │ │ ┌─────────────────────────────┐ │ │ │ Get user state │ │ │ │ Call appropriate handler │ │ │ └─────────────────────────────┘ │ │ │ │ render_update(update) │ │ ┌─────────────────────────────┐ │ │ │ Process incoming updates │ │ │ │ Route to handlers │ │ │ └─────────────────────────────┘ │ │ │ │ handle_main(user) │ │ handle_search_in_law(user) │ │ handle_rule_making(user) │ │ handle_chat_in_law(user) │ │ handle_qanon_title_repeat(user) │ │ handle_talk(user) │ │ handle_logical_chat_in_law(user) │ │ handle_conflict_qanon_asasi(user) │ │ handle_conflict_law_writing_policy(user) │ │ handle_subject_unities_to_evalution(user) │ │ handle_summary_conflict_all_qavanin(user) │ │ handle_full_conflict_all_qavanin(user) │ │ handle_conflict_general_policy(user) │ │ handle_advanced_check_conflict(user) │ │ handle_stream_chat(user) │ │ handle_beta(user) │ │ │ │ save_to_db(user, data) │ └─────────────────────────────────────┘ ``` ### 7. BaleBotBase Class ``` ┌─────────────────────────────────────┐ │ BaleBotBase │ ├─────────────────────────────────────┤ │ __init__(send_msg_url, ...) │ │ │ │ delete_bale_message(chat_id, message_id) │ │ update_bale_message(user, text, buttons) │ │ serialize(obj) │ │ normalize_messages(...) │ │ send_message_helper(...) │ │ send_bale_message(...) │ └─────────────────────────────────────┘ ``` ### 8. BaleBotUI Class ``` ┌─────────────────────────────────────┐ │ BaleBotUI │ ├─────────────────────────────────────┤ │ __init__(delay_time, ...) │ │ ┌─────────────────────────────┐ │ │ │ Initialize heartbeat settings │ │ │ └─────────────────────────────┘ │ │ │ │ active(user) │ │ cancel(user) │ │ create(user, main_text, waiting_list) │ │ heartbeat_loop(user, waiting_list) │ └─────────────────────────────────────┘ ``` ### 9. StackManager Class ``` ┌─────────────────────────────────────┐ │ StackManager │ ├─────────────────────────────────────┤ │ push(output_pure_data, ...) │ │ last_item(user) │ │ get_last_item_metadata(user) │ │ prev_item(user) │ │ get(user, name) │ │ cleanup(user) │ │ set_runtime(user, index, runtime_ms) │ └─────────────────────────────────────┘ ``` ## Data Flow Between Components ``` ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ FastAPI App │───▶│ BaleBotCore │───▶│ RequestManager │ └─────────────────┘ │ │ │ │ │ │ │ │ │ │ │ │