Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /ParisNeo_lollms /backend /db /migration.py
# Standard Library Imports
import json
import re
import shutil
from datetime import datetime
from pathlib import Path
from sqlalchemy import text
from sqlalchemy.exc import OperationalError, IntegrityError
from sqlalchemy.schema import DropTable
from sqlalchemy.ext.compiler import compiles
from backend.config import SERVER_CONFIG, APP_SETTINGS, SAFE_STORE_DEFAULTS, APP_DATA_DIR, USERS_DIR_NAME
from backend.db.base import CURRENT_DB_VERSION
from backend.db.models.config import GlobalConfig, LLMBinding, TTIBinding, TTSBinding, DatabaseVersion, RAGBinding, STTBinding
from backend.db.models.service import App
from backend.db.models.prompt import SavedPrompt
from backend.db.models.fun_fact import FunFactCategory, FunFact
from backend.db.models.user import User
from backend.db.models.group import Group
from backend.db.models.memory import UserMemory
from backend.db.models.connections import WebSocketConnection
from backend.db.models.image import UserImage, ImageAlbum
from backend.db.models.dm import Conversation, ConversationMember, DirectMessage
from backend.db.models.note import Note, NoteGroup
from backend.db.models.notebook import Notebook
from backend.db.models.flow import Flow, FlowNodeDefinition
from backend.db.models.saved_artefact import SavedArtefact
from backend.db.models.db_task import ScheduledTask # Assuming we will add this
from ascii_colors import ASCIIColors, trace_exception
@compiles(DropTable, "sqlite")
def _drop_table(element, compiler, **kw):
return "DROP TABLE %s;" % compiler.preparer.format_table(element.element)
def _get_all_existing_app_ports(connection) -> set[int]:
"""Retrieves all non-null ports currently used by apps in the database."""
return {r[0] for r in connection.execute(text("SELECT port FROM apps WHERE port IS NOT NULL")).fetchall()}
def _find_unique_port_for_migration(connection, preferred_port: int, already_used_in_batch: set) -> int:
"""
Finds a unique port during migration by checking against DB and in-batch used ports.
Starts searching from preferred_port upwards.
"""
all_db_used_ports_at_start = _get_all_existing_app_ports(connection)
current_port = preferred_port if preferred_port is not None and preferred_port >= 1024 else 9601
while True:
if current_port in already_used_in_batch or current_port in all_db_used_ports_at_start:
current_port += 1
else:
return current_port
def _bootstrap_global_settings(connection):
"""
Ensures all necessary global settings exist in the database.
"""
print("INFO: Checking and bootstrapping global settings in the database.")
all_possible_settings = {
"allow_dangerous_flow_execution": { "value": False, "type": "boolean", "description": "Enable Flow Studio Python code execution. Enabling this allows arbitrary code execution on the server host.", "category": "Security" },
"host": { "value": SERVER_CONFIG.get("host", "0.0.0.0"), "type": "string", "description": "Server host address. Requires a restart to take effect.", "category": "Server" },
"port": { "value": SERVER_CONFIG.get("port", 9642), "type": "integer", "description": "Server port. Requires a restart to take effect.", "category": "Server" },
"https_enabled": { "value": SERVER_CONFIG.get("https_enabled", False), "type": "boolean", "description": "Enable HTTPS for the server. Requires a restart to take effect.", "category": "Server" },
"ssl_certfile": { "value": SERVER_CONFIG.get("ssl_certfile", ""), "type": "string", "description": "Path to the SSL certificate file (e.g., cert.pem). Requires a restart.", "category": "Server" },
"ssl_keyfile": { "value": SERVER_CONFIG.get("ssl_keyfile", ""), "type": "string", "description": "Path to the SSL private key file (e.g., key.pem). Requires a restart.", "category": "Server" },
"public_domain_name": { "value": APP_SETTINGS.get("public_domain_name", ""), "type": "string", "description": "Public domain or IP to use for links when server is on 0.0.0.0. If empty, auto-detects IP.", "category": "Server" },
"cors_origins_exceptions": { "value": "", "type": "string", "description": "A comma-separated list of additional origins to allow for CORS (e.g., http://localhost:8000,https://my-app.com). Requires restart.", "category": "Server" },
"data_path": { "value": str(APP_DATA_DIR.resolve()), "type": "string", "description": "The root directory for all application data.", "category": "Server" },
"huggingface_cache_path": { "value": str((APP_DATA_DIR / "huggingface_cache").resolve()), "type": "string", "description": "Path for Hugging Face model cache. Requires a restart to change.", "category": "Server" },
"backup_password": { "value": "", "type": "string", "description": "Password to encrypt application backups.", "category": "Backup" },
"allow_new_registrations": { "value": APP_SETTINGS.get("allow_new_registrations", True), "type": "boolean", "description": "Allow new users to register an account.", "category": "Registration" },
"registration_mode": { "value": APP_SETTINGS.get("registration_mode", "admin_approval"), "type": "string", "description": "Method for new user activation: 'direct' or 'admin_approval'.", "category": "Registration" },
"access_token_expire_minutes": { "value": APP_SETTINGS.get("access_token_expires_minutes", 43200), "type": "integer", "description": "Duration in minutes a user's login session remains valid.", "category": "Authentication" },
"password_recovery_mode": { "value": "manual", "type": "string", "description": "Password recovery mode: 'manual', 'automatic' (SMTP), or 'system_mail' (uses server's 'mail' command).", "category": "Authentication" },
"email_verification_enabled": { "value": False, "type": "boolean", "description": "Require email verification code (2FA) upon user login.", "category": "Authentication" },
"email_verification_code_expiry_minutes": { "value": 10, "type": "integer", "description": "Expiry time in minutes for email login verification codes.", "category": "Authentication" },
"email_verification_bypass_admins": { "value": False, "type": "boolean", "description": "Allow administrator accounts to bypass email login verification.", "category": "Authentication" },
"sso_client_enabled": { "value": False, "type": "boolean", "description": "Enable Single Sign-On (SSO) for users to log in via an external provider.", "category": "SSO Client" },
"sso_client_provider_url": { "value": "", "type": "string", "description": "The OpenID Connect (OIDC) provider's discovery URL (e.g., https://accounts.google.com/.well-known/openid-configuration).", "category": "SSO Client" },
"sso_client_id": { "value": "", "type": "string", "description": "The Client ID provided by your SSO provider.", "category": "SSO Client" },
"sso_client_secret": { "value": "", "type": "string", "description": "The Client Secret provided by your SSO provider.", "category": "SSO Client" },
"sso_client_display_name": { "value": "Login with SSO", "type": "string", "description": "The text displayed on the SSO login button.", "category": "SSO Client" },
"sso_client_icon_url": { "value": "", "type": "string", "description": "URL for an icon to display on the SSO login button.", "category": "SSO Client" },
"sso_client_auto_create_users": { "value": True, "type": "boolean", "description": "Automatically create a lollms account for new users who sign in via SSO.", "category": "SSO Client" },
"scim_enabled": { "value": False, "type": "boolean", "description": "Enable SCIM 2.0 provisioning for users and groups.", "category": "SCIM Provisioning" },
"scim_token": { "value": "", "type": "string", "description": "The bearer token for authenticating SCIM requests. A new one will be generated if left empty.", "category": "SCIM Provisioning" },
"smtp_host": { "value": "", "type": "string", "description": "SMTP server address for sending password recovery emails.", "category": "Email Settings" },
"smtp_port": { "value": 587, "type": "integer", "description": "SMTP server port.", "category": "Email Settings" },
"smtp_user": { "value": "", "type": "string", "description": "Username for SMTP authentication.", "category": "Email Settings" },
"smtp_password": { "value": "", "type": "string", "description": "Password for SMTP authentication. (Stored in plaintext, use with caution)", "category": "Email Settings" },
"smtp_from_email": { "value": "", "type": "string", "description": "The 'From' email address for password recovery emails.", "category": "Email Settings" },
"smtp_use_tls": { "value": True, "type": "boolean", "description": "Use TLS for the SMTP connection.", "category": "Email Settings" },
"default_lollms_model_name": { "value": "", "type": "string", "description": "Default model name assigned to newly created users.", "category": "Defaults" },
"default_llm_ctx_size": { "value": 32000, "type": "integer", "description": "Default context size (in tokens) for new users.", "category": "Defaults" },
"default_llm_temperature": { "value": 0.7, "type": "float", "description": "Default generation temperature for new users.", "category": "Defaults" },
"default_user_ui_level": { "value": 0, "type": "integer", "description": "Default UI level for new users (0: Beginner, 2: Intermediate, 4: Expert).", "category": "Defaults" },
"default_auto_title": { "value": False, "type": "boolean", "description": "Enable automatic discussion title generation for new users by default.", "category": "Defaults" },
"default_safe_store_vectorizer": { "value": "default_st", "type": "string", "description": "Default vectorizer alias or name for newly created datastores.", "category": "RAG" },
"restrict_vectorizers_to_aliases": { "value": False, "type": "boolean", "description": "If enabled, users can only choose from the admin-defined RAG Bindings when creating a new Data Store.", "category": "RAG" },
"default_chunk_size": { "value": 2048, "type": "integer", "description": "The default number of characters per text chunk for RAG indexing.", "category": "RAG" },
"default_chunk_overlap": { "value": 256, "type": "integer", "description": "The default number of overlapping characters between adjacent text chunks.", "category": "RAG" },
"default_rag_chunking_strategy": { "value": "recursive", "type": "string", "description": "Default chunking strategy for new DataStores ('recursive', 'structure', 'token', 'semantic', 'contextual', 'late', 'paragraph', 'character').", "category": "RAG" },
"allow_user_chunking_config": { "value": True, "type": "boolean", "description": "If enabled, users can specify their own chunk size, overlap, and strategy when creating a Data Store.", "category": "RAG" },
"default_rag_retrieval_mode": { "value": "hybrid", "type": "string", "description": "Default RAG retrieval mode ('dense', 'hybrid', or 'graph_hybrid').", "category": "RAG" },
"default_rag_dense_weight": { "value": 0.5, "type": "float", "description": "Default weight for dense semantic embeddings in hybrid retrieval.", "category": "RAG" },
"default_rag_bm25_weight": { "value": 0.5, "type": "float", "description": "Default weight for BM25 lexical search in hybrid retrieval.", "category": "RAG" },
"default_rag_rrf_k": { "value": 60, "type": "integer", "description": "Default Reciprocal Rank Fusion constant k.", "category": "RAG" },
"force_rag_settings_mode": { "value": "disabled", "type": "string", "description": "Global RAG override mode: 'disabled' or 'force_always' (locks all RAG settings for users).", "category": "RAG" },
"force_rag_vectorizer": { "value": "", "type": "string", "description": "The vectorizer alias/name to enforce on all users when God Mode is enabled.", "category": "RAG" },
"force_rag_chunk_size": { "value": 2048, "type": "integer", "description": "Forced chunk size in characters.", "category": "RAG" },
"force_rag_chunk_overlap": { "value": 256, "type": "integer", "description": "Forced chunk overlap in characters.", "category": "RAG" },
"force_rag_top_k": { "value": 10, "type": "integer", "description": "Forced top-k chunks retrieved.", "category": "RAG" },
"force_rag_max_rag_len": { "value": 80000, "type": "integer", "description": "Forced maximum total RAG context length.", "category": "RAG" },
"force_rag_n_hops": { "value": 0, "type": "integer", "description": "Forced expansion hops for RAG chunks.", "category": "RAG" },
"force_rag_min_sim_percent": { "value": 50.0, "type": "float", "description": "Forced minimum similarity percentage.", "category": "RAG" },
"force_rag_retrieval_mode": { "value": "hybrid", "type": "string", "description": "Forced retrieval mode strategy.", "category": "RAG" },
"force_rag_dense_weight": { "value": 0.5, "type": "float", "description": "Forced dense semantic vector weight.", "category": "RAG" },
"force_rag_bm25_weight": { "value": 0.5, "type": "float", "description": "Forced BM25 lexical sparse weight.", "category": "RAG" },
"force_rag_rrf_k": { "value": 60, "type": "integer", "description": "Forced RRF constant k.", "category": "RAG" },
"force_rag_use_graph": { "value": False, "type": "boolean", "description": "Forced knowledge graph inclusion.", "category": "RAG" },
"force_rag_graph_response_type": { "value": "chunks_summary", "type": "string", "description": "Forced knowledge graph response type.", "category": "RAG" },
"force_model_mode": { "value": "disabled", "type": "string", "description": "Global model override mode: 'disabled', 'force_once' (sets user pref), 'force_always' (overrides session).", "category": "Global LLM Overrides" },
"force_model_name": { "value": "", "type": "string", "description": "The model name to force on all users. (e.g., 'ollama/llama3').", "category": "Global LLM Overrides" },
"force_tti_model_mode": { "value": "disabled", "type": "string", "description": "Global TTI model override mode: 'disabled', 'force_once' (sets user pref), 'force_always' (overrides session).", "category": "Global TTI Overrides" },
"force_tti_model_name": { "value": "", "type": "string", "description": "The TTI model name to force on all users. (e.g., 'diffusers/stable-diffusion-v1-5').", "category": "Global TTI Overrides" },
"force_iti_model_mode": { "value": "disabled", "type": "string", "description": "Global Image Editing model override mode: 'disabled', 'force_once' (sets user pref), 'force_always' (overrides session).", "category": "Global ITI Overrides" },
"force_iti_model_name": { "value": "", "type": "string", "description": "The Image Editing model name to force on all users. (e.g., 'diffusers/instruct-pix2pix').", "category": "Global ITI Overrides" },
"force_context_size": { "value": 4096, "type": "integer", "description": "The context size (in tokens) to force on all users.", "category": "Global LLM Overrides" },
"openai_api_service_enabled": { "value": False, "type": "boolean", "description": "Enable the OpenAI-compatible v1 API endpoint for users.", "category": "Services" },
"openai_api_require_key": { "value": True, "type": "boolean", "description": "Require an API key for the OpenAI-compatible v1 API endpoint. If disabled, requests without a key will be handled by the primary admin account.", "category": "Services" },
"ollama_service_enabled": { "value": False, "type": "boolean", "description": "Enable the Ollama service endpoint for users (OpenAI compatible).", "category": "Services" },
"ollama_require_key": { "value": True, "type": "boolean", "description": "Require an API key for the Ollama service endpoint. If disabled, requests without a key will be handled by the primary admin account.", "category": "Services" },
"lollms_services_enabled": { "value": True, "type": "boolean", "description": "Enable the exclusive LoLLMs Services endpoint (tokenizer, long-context, rag, advanced image edit).", "category": "Services" },
"lollms_services_require_key": { "value": True, "type": "boolean", "description": "Require an API key for exclusive LoLLMs Services.", "category": "Services" },
"rate_limit_enabled": { "value": False, "type": "boolean", "description": "Enable optional rate limiting for all service endpoints.", "category": "Services" },
"rate_limit_max_requests": { "value": 60, "type": "integer", "description": "Maximum number of requests allowed per window.", "category": "Services" },
"rate_limit_window_seconds": { "value": 60, "type": "integer", "description": "The window size in seconds for rate limiting.", "category": "Services" },
"model_display_mode": { "value": "mixed", "type": "string", "description": "How models are displayed to users: 'original' (shows raw names), 'aliased' (shows only models with aliases), 'mixed' (shows aliases where available, originals otherwise).", "category": "Models" },
"tti_model_display_mode": { "value": "mixed", "type": "string", "description": "How TTI models are displayed to users: 'original' (shows raw names), 'aliased' (shows only models with aliases), 'mixed' (shows aliases where available, originals otherwise).", "category": "Models" },
"tts_model_display_mode": { "value": "mixed", "type": "string", "description": "How TTS models are displayed to users: 'original' (shows raw names), 'aliased' (shows only models with aliases), 'mixed' (shows aliases where available, originals otherwise).", "category": "Models" },
"stt_model_display_mode": { "value": "mixed", "type": "string", "description": "How STT models are displayed to users: 'original' (shows raw names), 'aliased' (shows only models with aliases), 'mixed' (shows aliases where available, originals otherwise).", "category": "Models" },
"rag_model_display_mode": { "value": "mixed", "type": "string", "description": "How RAG models are displayed to users: 'original' (shows raw names), 'aliased' (shows only models with aliases), 'mixed' (shows aliases where available, originals otherwise).", "category": "Models" },
"lock_all_context_sizes": { "value": False, "type": "boolean", "description": "Lock context size for all aliased models, preventing users from changing it.", "category": "Models" },
"ai_bot_enabled": { "value": False, "type": "boolean", "description": "Enable the @lollms AI bot to respond to mentions in the social feed.", "category": "AI Bot" },
"ai_bot_system_prompt": { "value": "You are lollms, a helpful AI assistant integrated into this social platform. When a user mentions you using '@lollms', you should respond to their post helpfully and concisely. Your goal is to be a friendly and informative presence in the community.", "type": "text", "description": "The system prompt to use for the bot if no personality is selected.", "category": "AI Bot" },
"ai_bot_binding_model": { "value": "", "type": "string", "description": "The model used by the AI Bot.", "category": "AI Bot" },
"ai_bot_personality_id": { "value": "", "type": "string", "description": "The personality used by the AI Bot (optional).", "category": "AI Bot" },
"ai_bot_moderation_enabled": { "value": False, "type": "boolean", "description": "Enable moderation for AI Bot.", "category": "AI Bot" },
"ai_bot_moderation_criteria": { "value": "Be polite and respectful. No hate speech, spam, or explicit content.", "type": "text", "description": "Criteria for AI Bot moderation.", "category": "AI Bot" },
"ai_bot_telegram_enabled": { "value": False, "type": "boolean", "description": "Enable Telegram Bot integration for AI Bot.", "category": "AI Bot" },
"ai_bot_telegram_token": { "value": "", "type": "string", "description": "Telegram Bot token.", "category": "AI Bot" },
"ai_bot_discord_enabled": { "value": False, "type": "boolean", "description": "Enable Discord Bot integration for AI Bot.", "category": "AI Bot" },
"ai_bot_discord_token": { "value": "", "type": "string", "description": "Discord Bot token.", "category": "AI Bot" },
"ai_bot_slack_enabled": { "value": False, "type": "boolean", "description": "Enable Slack Bot integration for AI Bot.", "category": "AI Bot" },
"ai_bot_slack_app_token": { "value": "", "type": "string", "description": "Slack App Token.", "category": "AI Bot" },
"ai_bot_slack_bot_token": { "value": "", "type": "string", "description": "Slack Bot User OAuth Token.", "category": "AI Bot" },
"ai_bot_whatsapp_enabled": { "value": False, "type": "boolean", "description": "Enable WhatsApp Cloud API integration for AI Bot.", "category": "AI Bot" },
"ai_bot_whatsapp_token": { "value": "", "type": "string", "description": "WhatsApp Cloud API Permanent Access Token.", "category": "AI Bot" },
"ai_bot_whatsapp_phone_number_id": { "value": "", "type": "string", "description": "WhatsApp Phone Number ID.", "category": "AI Bot" },
"welcome_text": { "value": "lollms", "type": "string", "description": "The main text displayed on the welcome page.", "category": "Welcome Page" },
"welcome_slogan": { "value": "One tool to rule them all", "type": "string", "description": "The slogan displayed under the main text on the welcome page.", "category": "Welcome Page" },
"welcome_logo_url": { "value": "", "type": "string", "description": "URL to a custom logo for the welcome page. Leave empty for default.", "category": "Welcome Page" },
"force_welcome_message": { "value": False, "type": "boolean", "description": "When enabled, the specified announcement message will replace the random fun facts for all users.", "category": "Welcome Page" },
"forced_welcome_message_title": { "value": "System Announcement", "type": "string", "description": "The title displayed above the forced welcome message.", "category": "Welcome Page" },
"forced_welcome_message_content": { "value": "", "type": "text", "description": "The content of the forced announcement displayed on the welcome/loading screen.", "category": "Welcome Page" },
"latex_builder_enabled": { "value": False, "type": "boolean", "description": "Enable the LaTeX builder to compile LaTeX code blocks into PDFs.", "category": "Builders" },
"latex_builder_path": { "value": "pdflatex", "type": "string", "description": "Path to the pdflatex executable. On Windows, this might be 'C:\\texlive\\2023\\bin\\win32\\pdflatex.exe'. On Linux, 'pdflatex' should suffice if it's in the system's PATH.", "category": "Builders" },
"export_to_txt_enabled": { "value": True, "type": "boolean", "description": "Allow users to export messages as plain text (.txt) files.", "category": "Builders" },
"export_to_markdown_enabled": { "value": True, "type": "boolean", "description": "Allow users to export messages as Markdown (.md) files.", "category": "Builders" },
"export_to_html_enabled": { "value": True, "type": "boolean", "description": "Allow users to export messages as HTML (.html) files.", "category": "Builders" },
"export_to_pdf_enabled": { "value": False, "type": "boolean", "description": "Allow users to export messages as PDF (.pdf) files. Requires PyMuPDF (fitz).", "category": "Builders" },
"export_to_docx_enabled": { "value": False, "type": "boolean", "description": "Allow users to export messages as Word (.docx) files. Requires python-docx.", "category": "Builders" },
"export_to_xlsx_enabled": { "value": False, "type": "boolean", "description": "Allow users to export messages as Excel (.xlsx) files. Requires openpyxl.", "category": "Builders" },
"export_to_pptx_enabled": { "value": False, "type": "boolean", "description": "Allow users to export messages as PowerPoint (.pptx) files. Requires python-pptx.", "category": "Builders" },
"rss_feed_enabled": { "value": False, "type": "boolean", "description": "Enable the periodic fetching of RSS feeds to generate news and fun facts.", "category": "News Feed" },
"rss_feed_check_interval_minutes": { "value": 60, "type": "integer", "description": "How often (in minutes) to check for new articles in the RSS feeds.", "category": "News Feed" },
"rss_generate_fun_facts": { "value": False, "type": "boolean", "description": "When enabled, an AI will generate a 'fun fact' from the content of each new RSS article.", "category": "News Feed" },
"rss_news_retention_days": { "value": 1, "type": "integer", "description": "How many days to keep news articles. Older articles will be deleted daily. Set to 0 to disable cleanup.", "category": "News Feed" },
"tasks_auto_cleanup": { "value": True, "type": "boolean", "description": "Automatically delete completed, failed, or cancelled tasks from the database.", "category": "Task Manager" },
"tasks_retention_days": { "value": 7, "type": "integer", "description": "How many days to keep finished tasks in the database before auto-cleanup.", "category": "Task Manager" },
"com_hub_port": { "value": SERVER_CONFIG.get("com_hub_port", 8042), "type": "integer", "description": "Port for the inter-worker Communication Hub. Requires a restart.", "category": "Task Manager" },
"maintenance_mode": { "value": False, "type": "boolean", "description": "Put the system in maintenance mode. Only admins can access.", "category": "System" },
"maintenance_message": { "value": "We are performing scheduled maintenance. We'll be back shortly.", "type": "text", "description": "Message displayed to users during maintenance.", "category": "System" },
}
select_keys_query = text("SELECT key FROM global_configs")
existing_keys = {row[0] for row in connection.execute(select_keys_query).fetchall()}
missing_keys = set(all_possible_settings.keys()) - existing_keys
if missing_keys:
print(f"INFO: Found {len(missing_keys)} missing global settings. Adding them to the database.")
insert_stmt = GlobalConfig.__table__.insert()
configs_to_insert = [
{
"key": key,
"value": json.dumps({
"value": all_possible_settings[key]["value"],
"type": all_possible_settings[key]["type"]
}),
"description": all_possible_settings[key]["description"],
"category": all_possible_settings[key]["category"]
}
for key in missing_keys
]
connection.execute(insert_stmt, configs_to_insert)
connection.commit()
def _bootstrap_fun_facts(connection):
"""
Reads fun facts from the JSON file and populates the database if the tables are empty.
"""
print("INFO: Checking and bootstrapping fun facts in the database.")
try:
category_count = connection.execute(text("SELECT COUNT(id) FROM fun_fact_categories")).scalar_one()
if category_count > 0:
return
fun_facts_path = Path(__file__).parent.parent / "assets" / "fun_facts.json"
if not fun_facts_path.exists():
return
with open(fun_facts_path, "r", encoding="utf-8") as f:
categorized_facts = json.load(f)
category_insert_stmt = FunFactCategory.__table__.insert()
fact_insert_stmt = FunFact.__table__.insert()
default_colors = ["#3B82F6", "#10B981", "#F59E0B", "#EF4444", "#8B5CF6", "#EC4899"]
color_index = 0
for category_name, facts in categorized_facts.items():
color = default_colors[color_index % len(default_colors)]
color_index += 1
result = connection.execute(category_insert_stmt.values(name=category_name, is_active=True, color=color))
category_id = result.inserted_primary_key[0]
facts_to_insert = [
{"content": fact_content, "category_id": category_id}
for fact_content in facts if fact_content.strip()
]
if facts_to_insert:
connection.execute(fact_insert_stmt, facts_to_insert)
connection.commit()
except Exception as e:
print(f"CRITICAL: Failed to bootstrap fun facts. Error: {e}")
trace_exception(e)
connection.rollback()
def _bootstrap_lollms_user(connection):
"""
Ensures the special @lollms user exists in the database.
"""
from backend.security import get_password_hash
import secrets
lollms_user_exists = connection.execute(text("SELECT 1 FROM users WHERE username = 'lollms'")).first()
if not lollms_user_exists:
print("INFO: Creating special AI user '@lollms'.")
dummy_password = secrets.token_hex(32)
hashed_password = get_password_hash(dummy_password[:72])
connection.execute(
text("""\
INSERT INTO users (
username, hashed_password, is_admin, is_active, is_searchable,
first_login_done, receive_notification_emails, is_moderator,
put_thoughts_in_context, auto_title, chat_active, first_page,
show_token_counter, rag_use_graph, tell_llm_os, share_dynamic_info_with_llm,
include_memory_date_in_context, message_font_size,
image_generation_enabled, image_annotation_enabled, image_editing_enabled,
force_ai_response_language, share_personal_info_with_llm, note_generation_enabled,
memory_enabled, auto_memory_enabled,
default_rag_chunk_size, default_rag_chunk_overlap, default_rag_metadata_mode, status,
max_image_width, max_image_height,
slide_maker_enabled, activate_generated_images, compress_images, image_compression_quality,
web_search_enabled, web_search_deep_analysis, artefacts_enabled
)
VALUES (
:username, :hashed_password, :is_admin, :is_active, :is_searchable,
:first_login_done, :receive_notification_emails, :is_moderator,
:put_thoughts_in_context, :auto_title, :chat_active, :first_page,
:show_token_counter, :rag_use_graph, :tell_llm_os, :share_dynamic_info_with_llm,
:include_memory_date_in_context, :message_font_size,
:image_generation_enabled, :image_annotation_enabled, :image_editing_enabled,
:force_ai_response_language, :share_personal_info_with_llm, :note_generation_enabled,
:memory_enabled, :auto_memory_enabled,
:default_rag_chunk_size, :default_rag_chunk_overlap, :default_rag_metadata_mode, :status,
-1, -1,
:slide_maker_enabled, :activate_generated_images, :compress_images, :image_compression_quality,
:web_search_enabled, :web_search_deep_analysis, :artefacts_enabled
)
"""),
{
"username": "lollms",
"hashed_password": hashed_password,
"is_admin": False,
"is_active": True,
"is_searchable": True,
"first_login_done": True,
"receive_notification_emails": False,
"is_moderator": False,
"put_thoughts_in_context": False,
"auto_title": False,
"chat_active": False,
"first_page": "feed",
"show_token_counter": True,
"rag_use_graph": False,
"tell_llm_os": False,
"share_dynamic_info_with_llm": True,
"include_memory_date_in_context": False,
"message_font_size": 14,
"image_generation_enabled": False,
"image_annotation_enabled": False,
"image_editing_enabled": False,
"force_ai_response_language": False,
"share_personal_info_with_llm": False,
"note_generation_enabled": False,
"memory_enabled": False,
"auto_memory_enabled": False,
"default_rag_chunk_size": 1024,
"default_rag_chunk_overlap": 256,
"default_rag_metadata_mode": "none",
"status": "active",
"slide_maker_enabled": False,
"activate_generated_images": False,
"compress_images": False,
"image_compression_quality": 85,
"web_search_enabled": False,
"web_search_providers": "JSON DEFAULT '[\"google\"]'", # NEW COLUMN
"web_search_deep_analysis": False,
"artefacts_enabled": True
}
)
connection.commit()
print("INFO: AI user '@lollms' created successfully.")
else:
print("INFO: AI user '@lollms' already exists.")
def run_schema_migrations_and_bootstrap(connection, inspector):
# --- STAGE 0: NUCLEAR CLEANUP FOR STABILITY ---
# We clear these immediately to prevent the manager from attempting to
# broadcast massive stale payloads during the boot sequence.
if inspector.has_table("broadcast_messages"):
connection.execute(text("DELETE FROM broadcast_messages"))
if inspector.has_table("tasks"):
# We don't just mark them failed; we strip the heavy data (results/logs)
# to ensure any accidental broadcast is lightweight.
connection.execute(text("UPDATE tasks SET status='failed', error='Interrupted by server restart', result=NULL, logs='[]' WHERE status IN ('running', 'pending')"))
# Optional: connection.execute(text("DELETE FROM tasks")) # Uncomment to wipe all history every time
connection.commit()
_migrate_user_data_folders(connection)
if inspector.has_table("global_configs"):
_bootstrap_global_settings(connection)
keys_to_remove_str = "('default_mcps', 'default_personalities')"
try:
result = connection.execute(text(f"DELETE FROM global_configs WHERE key IN {keys_to_remove_str}"))
if result.rowcount > 0:
connection.commit()
except Exception as e:
print(f"WARNING: Could not remove deprecated global settings. Error: {e}")
connection.rollback()
# ------------------ NEW MIGRATION FOR FLOWS ------------------
if not inspector.has_table("flows"):
print("INFO: Creating 'flows' table.")
Flow.__table__.create(connection)
connection.commit()
if not inspector.has_table("flow_node_definitions"):
print("INFO: Creating 'flow_node_definitions' table.")
FlowNodeDefinition.__table__.create(connection)
connection.commit()
# Bootstrap default nodes if empty
_bootstrap_default_nodes(connection)
else:
# Check for new columns in flow_node_definitions
node_cols = [c['name'] for c in inspector.get_columns("flow_node_definitions")]
if "category" not in node_cols:
print("INFO: Adding 'category' column to 'flow_node_definitions' table.")
connection.execute(text("ALTER TABLE flow_node_definitions ADD COLUMN category VARCHAR DEFAULT 'General'"))
connection.execute(text("CREATE INDEX IF NOT EXISTS ix_flow_node_definitions_category ON flow_node_definitions (category)"))
connection.commit()
if "requirements" not in node_cols:
print("INFO: Adding 'requirements' column to 'flow_node_definitions' table.")
connection.execute(text("ALTER TABLE flow_node_definitions ADD COLUMN requirements JSON"))
connection.commit()
# [FIX] Backfill NULLs to prevent Pydantic validation errors
connection.execute(text("UPDATE flow_node_definitions SET requirements = '[]' WHERE requirements IS NULL"))
connection.execute(text("UPDATE flow_node_definitions SET category = 'General' WHERE category IS NULL"))
connection.commit()
# Re-bootstrap to apply new node types
_bootstrap_default_nodes(connection)
# -------------------------------------------------------------
# ------------------ NEW MIGRATION FOR IMAGE ALBUMS ------------------
if not inspector.has_table("image_albums"):
print("INFO: Creating 'image_albums' table.")
ImageAlbum.__table__.create(connection)
connection.commit()
if inspector.has_table("user_images"):
img_cols = [c['name'] for c in inspector.get_columns("user_images")]
if "album_id" not in img_cols:
print("INFO: Adding 'album_id' column to 'user_images' table.")
connection.execute(text("ALTER TABLE user_images ADD COLUMN album_id VARCHAR REFERENCES image_albums(id) ON DELETE SET NULL"))
connection.execute(text("CREATE INDEX IF NOT EXISTS ix_user_images_album_id ON user_images (album_id)"))
connection.commit()
# --------------------------------------------------------------------
if inspector.has_table("tasks"):
task_columns_db = [col['name'] for col in inspector.get_columns('tasks')]
if 'updated_at' not in task_columns_db:
try:
connection.execute(text("ALTER TABLE tasks ADD COLUMN updated_at DATETIME"))
connection.execute(text("UPDATE tasks SET updated_at = created_at WHERE updated_at IS NULL"))
connection.commit()
except Exception as e:
connection.rollback()
if inspector.has_table("saved_prompts"):
columns_db = [col['name'] for col in inspector.get_columns('saved_prompts')]
new_cols_defs = { "category": "VARCHAR", "author": "VARCHAR", "description": "TEXT", "icon": "TEXT", "version": "VARCHAR", "repository": "VARCHAR", "folder_name": "VARCHAR" }
for col_name, col_sql_def in new_cols_defs.items():
if col_name not in columns_db:
connection.execute(text(f"ALTER TABLE saved_prompts ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
full_columns_info = inspector.get_columns('saved_prompts')
owner_user_id_col_info_full = next((col for col in full_columns_info if col['name'] == 'owner_user_id'), None)
if owner_user_id_col_info_full and owner_user_id_col_info_full.get('nullable') == False:
try:
data = connection.execute(text("SELECT * FROM saved_prompts")).mappings().all()
connection.execute(text("DROP TABLE saved_prompts;"))
SavedPrompt.__table__.create(connection)
if data:
new_cols_names = [c.name for c in SavedPrompt.__table__.columns]
for row in data:
row_data = {k: v for k, v in row.items() if k in new_cols_names}
connection.execute(SavedPrompt.__table__.insert().values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
raise e
if inspector.has_table("direct_messages"):
direct_messages_columns_db = [col['name'] for col in inspector.get_columns('direct_messages')]
new_direct_messages_cols_defs = {
"image_references": "JSON",
"media": "JSON",
"reply_to_id": "INTEGER REFERENCES direct_messages(id) ON DELETE SET NULL",
"reactions": "JSON"
}
for col_name, col_sql_def in new_direct_messages_cols_defs.items():
if col_name not in direct_messages_columns_db:
try:
connection.execute(text(f"ALTER TABLE direct_messages ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
except Exception:
connection.rollback()
connection.commit()
dm_cols_full = inspector.get_columns('direct_messages')
receiver_col_info = next((c for c in dm_cols_full if c['name'] == 'receiver_id'), None)
if receiver_col_info and not receiver_col_info.get('nullable'):
try:
data = connection.execute(text("SELECT * FROM direct_messages")).mappings().all()
connection.execute(text("DROP TABLE direct_messages;"))
DirectMessage.__table__.create(connection)
if data:
new_cols = [c.name for c in DirectMessage.__table__.columns]
insert_stmt = DirectMessage.__table__.insert()
for row in data:
row_data = {k: v for k, v in row.items() if k in new_cols}
for date_field in ['sent_at', 'read_at']:
if date_field in row_data and isinstance(row_data[date_field], str):
try: row_data[date_field] = datetime.fromisoformat(row_data[date_field])
except: pass
connection.execute(insert_stmt.values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
if inspector.has_table("conversation_members"):
cm_cols = [c['name'] for c in inspector.get_columns('conversation_members')]
if 'id' not in cm_cols:
try:
data = connection.execute(text("SELECT * FROM conversation_members")).mappings().all()
connection.execute(text("DROP TABLE conversation_members;"))
ConversationMember.__table__.create(connection)
if data:
insert_stmt = ConversationMember.__table__.insert()
for row in data:
row_data = {k: v for k, v in row.items() if k in cm_cols}
connection.execute(insert_stmt.values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
if inspector.has_table("database_version"):
db_version_cols = inspector.get_columns('database_version')
version_col_info = next((col for col in db_version_cols if col['name'] == 'version'), None)
if version_col_info and "VARCHAR" not in str(version_col_info['type']).upper() and "TEXT" not in str(version_col_info['type']).upper():
try:
old_version_data = connection.execute(text("SELECT id, version FROM database_version")).mappings().first()
connection.execute(text("DROP TABLE database_version;"))
DatabaseVersion.__table__.create(connection)
if old_version_data:
version_val = str(old_version_data['version']) if isinstance(old_version_data['version'], (int, float)) else old_version_data['version']
connection.execute(DatabaseVersion.__table__.insert().values(id=old_version_data['id'], version=version_val))
connection.commit()
except Exception as e:
connection.rollback()
raise
if inspector.has_table("llm_bindings"):
llm_bindings_columns_db = [col['name'] for col in inspector.get_columns('llm_bindings')]
columns_requiring_rebuild = ['verify_ssl_certificate', 'host_address', 'service_key', 'models_path', 'ctx_size', 'user_name', 'ai_name', 'temperature', 'top_k', 'top_p', 'repeat_penalty', 'repeat_last_n']
needs_rebuild = any(col_name in llm_bindings_columns_db for col_name in columns_requiring_rebuild)
if needs_rebuild:
try:
data_proxy = connection.execute(text("SELECT * FROM llm_bindings"))
column_names = data_proxy.keys()
rows = data_proxy.fetchall()
data_to_migrate = [dict(zip(column_names, row)) for row in rows]
connection.execute(text("PRAGMA foreign_keys=off;"))
new_rows_data = []
if data_to_migrate:
for row in data_to_migrate:
existing_config = row.get('config')
if isinstance(existing_config, str):
try: existing_config = json.loads(existing_config)
except (json.JSONDecodeError, TypeError): existing_config = {}
elif not isinstance(existing_config, dict): existing_config = {}
json_to_add = {}
if 'verify_ssl_certificate' in row and row['verify_ssl_certificate'] is not None: json_to_add['verify_ssl_certificate'] = bool(row['verify_ssl_certificate'])
if 'host_address' in row and row['host_address'] is not None: json_to_add['host_address'] = row['host_address']
created_at_dt = None
if 'created_at' in row and row['created_at'] is not None:
if isinstance(row['created_at'], str):
try: created_at_dt = datetime.fromisoformat(row['created_at'])
except ValueError: pass
else: created_at_dt = row['created_at']
updated_at_dt = None
if 'updated_at' in row and row['updated_at'] is not None:
if isinstance(row['updated_at'], str):
try: updated_at_dt = datetime.fromisoformat(row['updated_at'])
except ValueError: pass
else: updated_at_dt = row['updated_at']
new_row_data = {
'id': row['id'],
'alias': row.get('alias') or row.get('name') or f"binding_{row['id']}",
'name': row.get('name') or row.get('alias') or f"binding_{row['id']}",
'config': {**existing_config, **json_to_add},
'default_model_name': row.get('default_model_name'),
'is_active': row.get('is_active', True),
'created_at': created_at_dt,
'updated_at': updated_at_dt,
'model_aliases': row.get('model_aliases')
}
if new_row_data['alias'] is None or new_row_data['name'] is None: continue
new_rows_data.append(new_row_data)
connection.execute(text("DROP TABLE llm_bindings;"))
connection.commit()
LLMBinding.__table__.create(connection)
for new_row_data in new_rows_data:
connection.execute(LLMBinding.__table__.insert().values(new_row_data))
connection.commit()
except Exception as e:
connection.rollback()
raise e
finally:
connection.execute(text("PRAGMA foreign_keys=on;"))
if inspector.has_table("personalities"):
personality_columns_db = [col['name'] for col in inspector.get_columns('personalities')]
new_personality_cols_defs = { "tools": "JSON", "data_source_type": "VARCHAR DEFAULT 'none' NOT NULL", "data_source": "TEXT", "version": "VARCHAR", "repository": "VARCHAR", "folder_name": "VARCHAR", "required_context_options": "JSON" }
for col_name, col_sql_def in new_personality_cols_defs.items():
if col_name not in personality_columns_db:
connection.execute(text(f"ALTER TABLE personalities ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
if not inspector.has_table("user_voices"):
from backend.db.models.voice import UserVoice
UserVoice.__table__.create(connection)
connection.commit()
else:
voice_columns_db = [col['name'] for col in inspector.get_columns('user_voices')]
new_voice_cols_defs = { "speed": "FLOAT DEFAULT 1.0 NOT NULL", "gain": "FLOAT DEFAULT 0.0 NOT NULL", "reverb_params": "JSON" }
for col_name, col_sql_def in new_voice_cols_defs.items():
if col_name not in voice_columns_db:
connection.execute(text(f"ALTER TABLE user_voices ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
if not inspector.has_table("user_images"):
from backend.db.models.image import UserImage
UserImage.__table__.create(connection)
connection.commit()
else:
image_columns_db = [col['name'] for col in inspector.get_columns('user_images')]
if 'width' not in image_columns_db: connection.execute(text("ALTER TABLE user_images ADD COLUMN width INTEGER"))
if 'height' not in image_columns_db: connection.execute(text("ALTER TABLE user_images ADD COLUMN height INTEGER"))
if 'negative_prompt' not in image_columns_db: connection.execute(text("ALTER TABLE user_images ADD COLUMN negative_prompt TEXT"))
if 'seed' not in image_columns_db: connection.execute(text("ALTER TABLE user_images ADD COLUMN seed INTEGER"))
if 'generation_params' not in image_columns_db: connection.execute(text("ALTER TABLE user_images ADD COLUMN generation_params JSON"))
connection.commit()
if inspector.has_table("users"):
user_columns_db = [col['name'] for col in inspector.get_columns('users')]
if 'external_id' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN external_id VARCHAR"))
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS ix_users_external_id ON users (external_id)"))
connection.commit()
except Exception: connection.rollback()
if 'password_changed_at' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN password_changed_at DATETIME"))
connection.execute(text("UPDATE users SET password_changed_at = created_at WHERE password_changed_at IS NULL"))
connection.commit()
except Exception: connection.rollback()
if 'email_verification_code' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN email_verification_code VARCHAR"))
connection.commit()
except Exception: connection.rollback()
if 'email_verification_code_expiry' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN email_verification_code_expiry DATETIME"))
connection.commit()
except Exception: connection.rollback()
if 'email_verification_token' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN email_verification_token VARCHAR"))
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS ix_users_email_verification_token ON users (email_verification_token) WHERE email_verification_token IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'scratchpad' in user_columns_db and 'data_zone' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users RENAME COLUMN scratchpad TO data_zone"))
user_columns_db = [col['name'] for col in inspector.get_columns('users')]
connection.commit()
except Exception: connection.rollback()
if 'slide_maker_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN slide_maker_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
# --- Migration for new fields ---
if 'note_generation_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN note_generation_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'book_generation_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN book_generation_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'artefacts_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN artefacts_enabled BOOLEAN DEFAULT 1 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'image_editing_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN image_editing_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'memory_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN memory_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'auto_memory_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN auto_memory_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'skills_library_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN skills_library_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'skills_building_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN skills_building_enabled BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'form_building_enabled' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN form_building_enabled BOOLEAN DEFAULT 1 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'preferred_name' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN preferred_name VARCHAR"))
connection.commit()
except Exception: connection.rollback()
if 'user_personal_info' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN user_personal_info TEXT"))
connection.commit()
except Exception: connection.rollback()
if 'share_personal_info_with_llm' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN share_personal_info_with_llm BOOLEAN DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'status' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN status VARCHAR DEFAULT 'active' NOT NULL"))
connection.execute(text("CREATE INDEX IF NOT EXISTS ix_users_status ON users (status)"))
# Backfill status based on is_active and activation_token
connection.execute(text("UPDATE users SET status = 'active' WHERE is_active = 1 OR status IS NULL"))
connection.execute(text("UPDATE users SET status = 'pending_admin_validation' WHERE is_active = 0 AND activation_token IS NOT NULL"))
connection.execute(text("UPDATE users SET status = 'inactivated_by_admin' WHERE is_active = 0 AND activation_token IS NULL"))
connection.commit()
except Exception: connection.rollback()
if 'total_logins' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN total_logins INTEGER DEFAULT 0 NOT NULL"))
connection.commit()
except Exception: connection.rollback()
# New RAG fields migration
new_rag_cols = {
"default_rag_chunk_size": "INTEGER DEFAULT 1024",
"default_rag_chunk_overlap": "INTEGER DEFAULT 256",
"default_rag_metadata_mode": "VARCHAR DEFAULT 'none'",
"rag_retrieval_mode": "VARCHAR DEFAULT 'hybrid'",
"rag_dense_weight": "FLOAT DEFAULT 0.5",
"rag_bm25_weight": "FLOAT DEFAULT 0.5",
"rag_graph_weight": "FLOAT DEFAULT 0.3",
"rag_rrf_k": "INTEGER DEFAULT 60"
}
for col_name, col_sql_def in new_rag_cols.items():
if col_name not in user_columns_db:
try:
connection.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add column {col_name} to users table: {e}")
connection.rollback()
new_herd_cols = {
"herd_mode_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"herd_participants": "JSON",
"herd_precode_participants": "JSON",
"herd_postcode_participants": "JSON",
"herd_rounds": "INTEGER DEFAULT 2 NOT NULL",
"herd_dynamic_mode": "BOOLEAN DEFAULT 0 NOT NULL",
"herd_model_pool": "JSON"
}
for col_name, col_sql_def in new_herd_cols.items():
if col_name not in user_columns_db:
try:
connection.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add column {col_name} to users table: {e}")
connection.rollback()
# Backfill JSON default if needed
for json_col in ["herd_participants", "herd_precode_participants", "herd_postcode_participants", "herd_model_pool"]:
if json_col in new_herd_cols:
try:
connection.execute(text(f"UPDATE users SET {json_col} = '[]' WHERE {json_col} IS NULL"))
connection.commit()
except Exception: pass
for col_name, col_sql_def in new_herd_cols.items():
if col_name not in user_columns_db:
try:
connection.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add column {col_name} to users table: {e}")
connection.rollback()
# Backfill JSON default if needed
if "herd_participants" in new_herd_cols:
try:
connection.execute(text("UPDATE users SET herd_participants = '[]' WHERE herd_participants IS NULL"))
connection.commit()
except Exception: pass
# New Google Search & Web Search fields
new_web_search_cols = {
"google_api_key": "VARCHAR",
"google_cse_id": "VARCHAR",
"web_search_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"web_search_providers": "JSON DEFAULT '[]'",
"web_search_deep_analysis": "BOOLEAN DEFAULT 0 NOT NULL"
}
for col_name, col_sql_def in new_web_search_cols.items():
if col_name not in user_columns_db:
try:
connection.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add column {col_name} to users table: {e}")
connection.rollback()
new_user_cols_defs = {
"is_active": "BOOLEAN DEFAULT 1 NOT NULL", "created_at": "DATETIME",
"last_activity_at": "DATETIME", "activation_token": "VARCHAR",
"password_reset_token": "VARCHAR", "reset_token_expiry": "DATETIME",
"icon": "TEXT",
"active_personality_id": "VARCHAR", "first_name": "VARCHAR", "family_name": "VARCHAR",
"email": "VARCHAR", "birth_date": "DATE", "llm_ctx_size": "INTEGER",
"rag_top_k": "INTEGER", "max_rag_len": "INTEGER", "rag_n_hops": "INTEGER",
"rag_min_sim_percent": "FLOAT", "rag_use_graph": "BOOLEAN DEFAULT 0",
"rag_graph_response_type": "VARCHAR DEFAULT 'chunks_summary'",
"put_thoughts_in_context": "BOOLEAN DEFAULT 0 NOT NULL",
"auto_title": "BOOLEAN DEFAULT 0 NOT NULL",
"user_ui_level":"INTEGER", "ai_response_language":"VARCHAR DEFAULT 'auto'",
"force_ai_response_language": "BOOLEAN DEFAULT 0 NOT NULL",
"fun_mode": "BOOLEAN DEFAULT 0 NOT NULL",
"chat_active": "BOOLEAN DEFAULT 1 NOT NULL",
"first_page": "VARCHAR DEFAULT 'feed' NOT NULL",
"receive_notification_emails": "BOOLEAN DEFAULT 1 NOT NULL",
"show_token_counter": "BOOLEAN DEFAULT 1 NOT NULL",
"is_searchable": "BOOLEAN DEFAULT 1 NOT NULL",
"first_login_done": "BOOLEAN DEFAULT 0 NOT NULL",
"data_zone": "TEXT", "memory": "TEXT",
"is_moderator": "BOOLEAN DEFAULT 0 NOT NULL",
"tti_binding_model_name": "VARCHAR",
"iti_binding_model_name": "VARCHAR",
"tti_models_config": "JSON",
"tts_binding_model_name": "VARCHAR",
"tts_models_config": "JSON",
"stt_binding_model_name": "VARCHAR",
"stt_models_config": "JSON",
"active_voice_id": "VARCHAR REFERENCES user_voices(id) ON DELETE SET NULL",
"include_memory_date_in_context": "BOOLEAN DEFAULT 0 NOT NULL",
"coding_style_constraints": "TEXT",
"programming_language_preferences": "TEXT",
"tell_llm_os": "BOOLEAN DEFAULT 0 NOT NULL",
"share_dynamic_info_with_llm": "BOOLEAN DEFAULT 1 NOT NULL",
"message_font_size": "INTEGER DEFAULT 14 NOT NULL",
"last_discussion_id": "VARCHAR",
"image_studio_prompt": "TEXT",
"image_studio_negative_prompt": "TEXT",
"image_studio_image_size": "VARCHAR DEFAULT '1024x1024'",
"image_studio_n_images": "INTEGER DEFAULT 1",
"image_studio_seed": "INTEGER DEFAULT -1",
"image_studio_generation_params": "JSON",
"image_generation_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"image_generation_system_prompt": "TEXT",
"image_annotation_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"image_editing_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"inline_widgets_enabled": "BOOLEAN DEFAULT 1 NOT NULL",
"discussion_sorting_mode": "VARCHAR DEFAULT 'date' NOT NULL",
"discussion_sorting_order": "VARCHAR DEFAULT 'desc' NOT NULL",
"reasoning_activation": "BOOLEAN DEFAULT 0",
"reasoning_effort": "VARCHAR",
"reasoning_summary": "BOOLEAN DEFAULT 0",
"max_image_width": "INTEGER DEFAULT -1",
"max_image_height": "INTEGER DEFAULT -1",
"activate_generated_images": "BOOLEAN DEFAULT 0 NOT NULL",
"compress_images": "BOOLEAN DEFAULT 0 NOT NULL",
"image_compression_quality": "INTEGER DEFAULT 85 NOT NULL",
"rlm_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"artefacts_enabled": "BOOLEAN DEFAULT 1 NOT NULL",
"street_view_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"google_drive_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"google_calendar_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"google_gmail_enabled": "BOOLEAN DEFAULT 0 NOT NULL",
"google_client_secret_json": "TEXT",
"scheduler_enabled": "BOOLEAN DEFAULT 0 NOT NULL"
}
added_cols = []
for col_name, col_sql_def in new_user_cols_defs.items():
if col_name not in user_columns_db:
try:
connection.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_sql_def}"))
added_cols.append(col_name)
connection.commit()
except Exception: connection.rollback()
if 'chat_active' in added_cols:
connection.execute(text("UPDATE users SET chat_active = 1 WHERE chat_active IS NULL"))
connection.commit()
if 'artefacts_enabled' in added_cols:
connection.execute(text("UPDATE users SET artefacts_enabled = 1 WHERE artefacts_enabled IS NULL"))
connection.commit()
if 'discussion_sorting_mode' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN discussion_sorting_mode VARCHAR DEFAULT 'date' NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'discussion_sorting_order' not in user_columns_db:
try:
connection.execute(text("ALTER TABLE users ADD COLUMN discussion_sorting_order VARCHAR DEFAULT 'desc' NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'chat_active' in user_columns_db:
connection.execute(text("UPDATE users SET chat_active = 1 WHERE chat_active = 0"))
connection.commit()
if 'is_active' in added_cols:
connection.execute(text("UPDATE users SET is_active = 1 WHERE is_active IS NULL"))
connection.commit()
if 'is_admin' in added_cols:
connection.execute(text("UPDATE users SET is_admin = 0 WHERE is_admin IS NULL"))
connection.commit()
if 'is_moderator' in added_cols:
connection.execute(text("UPDATE users SET is_moderator = 0 WHERE is_moderator IS NULL"))
connection.commit()
if 'put_thoughts_in_context' not in user_columns_db:
try:
connection.execute(text("UPDATE users SET put_thoughts_in_context = 0 WHERE put_thoughts_in_context IS NULL"))
connection.commit()
except Exception: connection.rollback()
if 'first_login_done' in added_cols:
try:
connection.execute(text("UPDATE users SET first_login_done = 1 WHERE first_login_done IS NULL"))
connection.commit()
except Exception: connection.rollback()
user_constraints = inspector.get_unique_constraints('users')
if 'email' in new_user_cols_defs and not any(c['name'] == 'uq_user_email' for c in user_constraints):
try:
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_user_email ON users (email) WHERE email IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'password_reset_token' in new_user_cols_defs and not any(c['name'] == 'uq_password_reset_token' for c in user_constraints):
try:
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_password_reset_token ON users (password_reset_token) WHERE password_reset_token IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if inspector.has_table("posts"):
posts_columns_db = [col['name'] for col in inspector.get_columns('posts')]
if 'group_id' not in posts_columns_db:
try:
connection.execute(text("ALTER TABLE posts ADD COLUMN group_id INTEGER REFERENCES user_groups(id) ON DELETE CASCADE"))
connection.commit()
except Exception: connection.rollback()
if 'is_pinned' not in posts_columns_db:
try:
connection.execute(text("ALTER TABLE posts ADD COLUMN is_pinned BOOLEAN DEFAULT 0 NOT NULL"))
connection.execute(text("CREATE INDEX IF NOT EXISTS ix_posts_is_pinned ON posts (is_pinned)"))
connection.commit()
except Exception: connection.rollback()
if 'moderation_status' not in posts_columns_db:
try:
connection.execute(text("ALTER TABLE posts ADD COLUMN moderation_status VARCHAR DEFAULT 'pending' NOT NULL"))
connection.execute(text("UPDATE posts SET moderation_status = 'pending'"))
connection.commit()
except Exception: connection.rollback()
try:
result = connection.execute(text("UPDATE posts SET visibility = LOWER(visibility) WHERE visibility != LOWER(visibility)"))
if result.rowcount > 0: connection.commit()
except Exception: connection.rollback()
if inspector.has_table("comments"):
comments_columns_db = [col['name'] for col in inspector.get_columns('comments')]
if 'moderation_status' not in comments_columns_db:
try:
connection.execute(text("ALTER TABLE comments ADD COLUMN moderation_status VARCHAR DEFAULT 'pending' NOT NULL"))
connection.execute(text("UPDATE comments SET moderation_status = 'pending'"))
connection.commit()
except Exception: connection.rollback()
if inspector.has_table("discussion_groups"):
discussion_groups_columns = [col['name'] for col in inspector.get_columns('discussion_groups')]
if 'parent_id' not in discussion_groups_columns:
try:
connection.execute(text("ALTER TABLE discussion_groups ADD COLUMN parent_id VARCHAR REFERENCES discussion_groups(id) ON DELETE SET NULL"))
connection.commit()
except Exception: connection.rollback()
if not inspector.has_table("user_memories"):
UserMemory.__table__.create(connection)
connection.commit()
else:
user_memories_columns = [col['name'] for col in inspector.get_columns('user_memories')]
if 'owner_user_id' not in user_memories_columns:
try:
connection.execute(text("ALTER TABLE user_memories ADD COLUMN owner_user_id INTEGER REFERENCES users(id) ON DELETE CASCADE"))
admin_user = connection.execute(text("SELECT id FROM users WHERE is_admin = 1 ORDER BY id ASC LIMIT 1")).first()
if admin_user:
connection.execute(text(f"UPDATE user_memories SET owner_user_id = {admin_user} WHERE owner_user_id IS NULL"))
connection.commit()
except Exception: connection.rollback()
if 'updated_at' not in user_memories_columns:
try:
connection.execute(text("ALTER TABLE user_memories ADD COLUMN updated_at DATETIME"))
connection.execute(text("UPDATE user_memories SET updated_at = created_at WHERE updated_at IS NULL"))
connection.commit()
except Exception: connection.rollback()
try:
result = connection.execute(text("DELETE FROM user_memories WHERE title IS NULL OR title = '' OR content IS NULL OR content = ''"))
if result.rowcount > 0: connection.commit()
except Exception: connection.rollback()
if not inspector.has_table("user_groups"):
if inspector.has_table("scim_groups"):
connection.execute(text("ALTER TABLE scim_groups RENAME TO user_groups"))
else:
Group.__table__.create(connection)
group_columns = [col['name'] for col in inspector.get_columns('user_groups')]
new_group_cols = { "description": "TEXT", "icon": "TEXT", "owner_id": "INTEGER REFERENCES users(id) ON DELETE SET NULL" }
for col_name, col_type in new_group_cols.items():
if col_name not in group_columns:
connection.execute(text(f"ALTER TABLE user_groups ADD COLUMN {col_name} {col_type}"))
connection.commit()
if not inspector.has_table("user_group_link"):
from backend.db.base import user_group_link as user_group_link_table
user_group_link_table.create(connection)
connection.commit()
_bootstrap_lollms_user(connection)
# --- TASK PURGE ---
# Delete all background tasks from the DB on startup.
# This ensures the frontend doesn't download a massive history of logs/results.
if inspector.has_table("tasks"):
try:
connection.execute(text("DELETE FROM tasks"))
connection.commit()
print("INFO: Task history purged for stability.")
except Exception as e:
print(f"WARNING: Could not purge tasks: {e}")
if inspector.has_table("mcps"):
mcp_columns_db = [col['name'] for col in inspector.get_columns('mcps')]
new_mcp_cols_defs = { "active": "BOOLEAN DEFAULT 1 NOT NULL", "type": "VARCHAR", "icon": "TEXT", "authentication_type": "VARCHAR", "authentication_key": "VARCHAR", "sso_redirect_uri": "VARCHAR", "sso_user_infos_to_share": "JSON", "client_id": "VARCHAR" }
for col_name, col_sql_def in new_mcp_cols_defs.items():
if col_name not in mcp_columns_db:
connection.execute(text(f"ALTER TABLE mcps ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
if 'client_id' in new_mcp_cols_defs:
mcps_to_update = connection.execute(text("SELECT id, name FROM mcps WHERE client_id IS NULL")).fetchall()
if mcps_to_update:
app_columns_db = []
if inspector.has_table('apps'): app_columns_db = [col['name'] for col in inspector.get_columns('apps')]
for mcp_id, mcp_name in mcps_to_update:
base_slug = re.sub(r'[^a-z0-9_]+', '', mcp_name.lower().replace(' ', '_'))
new_client_id = base_slug
counter = 1
while True:
app_conflict = None
if 'client_id' in app_columns_db:
app_conflict = connection.execute(text("SELECT 1 FROM apps WHERE client_id = :cid"), {"cid": new_client_id}).first()
mcp_conflict = connection.execute(text("SELECT 1 FROM mcps WHERE client_id = :cid AND id != :mid"), {"cid": new_client_id, "mid": mcp_id}).first()
if not app_conflict and not mcp_conflict: break
new_client_id = f"{base_slug}_{counter}"
counter += 1
connection.execute(text("UPDATE mcps SET client_id = :cid WHERE id = :mid"), {"cid": new_client_id, "mid": mcp_id})
connection.commit()
mcp_constraints = inspector.get_unique_constraints('mcps')
if 'client_id' in new_mcp_cols_defs and not any(c['name'] == 'uq_mcp_client_id' for c in mcp_constraints):
try:
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_mcp_client_id ON mcps (client_id) WHERE client_id IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if inspector.has_table("apps"):
app_columns_db = [col['name'] for col in inspector.get_columns('apps')]
url_col_info = next((col for col in inspector.get_columns('apps') if col['name'] == 'url'), None)
if url_col_info and url_col_info.get('nullable') == 0:
try:
data = connection.execute(text("SELECT * FROM apps")).mappings().all()
connection.execute(text("DROP TABLE apps;"))
App.__table__.create(connection)
if data:
new_cols_names = [c.name for c in App.__table__.columns]
used_ports_during_migration_batch = set()
for row in data:
row_data = {k: v for k, v in row.items() if k in new_cols_names}
if 'created_at' in row_data and isinstance(row_data['created_at'], str):
try: row_data['created_at'] = datetime.fromisoformat(row_data['created_at'])
except ValueError: row_data['created_at'] = None
if 'updated_at' in row_data and isinstance(row_data['updated_at'], str):
try: row_data['updated_at'] = datetime.fromisoformat(row_data['updated_at'])
except ValueError: row_data['updated_at'] = None
if 'port' in row_data and row_data['port'] is not None:
original_port = row_data['port']
new_port = _find_unique_port_for_migration(connection, original_port, used_ports_during_migration_batch)
row_data['port'] = new_port
used_ports_during_migration_batch.add(new_port)
if 'client_id' not in row_data or row_data['client_id'] is None:
base_slug = re.sub(r'[^a-z0-9_]+', '', (row_data.get('name', 'app')).lower().replace(' ', '_'))
new_client_id = base_slug
counter = 1
while True:
app_conflict = connection.execute(text("SELECT 1 FROM apps WHERE client_id = :cid"), {"cid": new_client_id}).first()
mcp_conflict = connection.execute(text("SELECT 1 FROM mcps WHERE client_id = :cid"), {"cid": new_client_id}).first()
if not app_conflict and not mcp_conflict: break
new_client_id = f"{base_slug}_{counter}"
counter += 1
row_data['client_id'] = new_client_id
connection.execute(App.__table__.insert().values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
raise e
new_app_cols_defs = { "host": "VARCHAR DEFAULT 'localhost'", "icon": "TEXT", "active": "BOOLEAN DEFAULT 1 NOT NULL", "type": "VARCHAR", "authentication_type": "VARCHAR", "authentication_key": "VARCHAR", "sso_redirect_uri": "VARCHAR", "sso_user_infos_to_share": "JSON", "client_id": "VARCHAR", "description": "TEXT", "author": "VARCHAR", "version": "VARCHAR", "category": "VARCHAR", "tags": "JSON", "is_installed": "BOOLEAN DEFAULT 0 NOT NULL", "status": "VARCHAR DEFAULT 'stopped' NOT NULL", "autostart": "BOOLEAN DEFAULT 0 NOT NULL", "port": "INTEGER", "pid": "INTEGER", "app_metadata": "JSON", "folder_name": "VARCHAR", "allow_openai_api_access": "BOOLEAN DEFAULT 0 NOT NULL" }
app_columns_db_after_rebuild = [col['name'] for col in inspector.get_columns('apps')]
for col_name, col_sql_def in new_app_cols_defs.items():
if col_name not in app_columns_db_after_rebuild:
connection.execute(text(f"ALTER TABLE apps ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
if 'client_id' in new_app_cols_defs:
apps_to_update = connection.execute(text("SELECT id, name FROM mcps WHERE client_id IS NULL")).fetchall()
if apps_to_update:
for app_id, app_name in apps_to_update:
base_slug = re.sub(r'[^a-z0-9_]+', '', app_name.lower().replace(' ', '_'))
new_client_id = base_slug
counter = 1
while True:
app_conflict = connection.execute(text("SELECT 1 FROM apps WHERE client_id = :cid AND id != :aid"), {"cid": new_client_id, "aid": app_id}).first()
mcp_conflict = connection.execute(text("SELECT 1 FROM mcps WHERE client_id = :cid"), {"cid": new_client_id}).first()
if not app_conflict and not mcp_conflict: break
new_client_id = f"{base_slug}_{counter}"
counter += 1
connection.execute(text("UPDATE apps SET client_id = :cid WHERE id = :aid"), {"cid": new_client_id, "aid": app_id})
connection.commit()
app_constraints = inspector.get_unique_constraints('apps')
if 'port' in new_app_cols_defs and not any(c['name'] == 'uq_app_port' for c in app_constraints):
try:
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_app_port ON apps (port) WHERE port IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if 'client_id' in new_app_cols_defs and not any(c['name'] == 'uq_app_client_id' for c in app_constraints):
try:
connection.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_app_client_id ON apps (client_id) WHERE client_id IS NOT NULL"))
connection.commit()
except Exception: connection.rollback()
if inspector.has_table("system_apps"):
system_app_columns_db = [col['name'] for col in inspector.get_columns('system_apps')]
new_system_app_cols_defs = { "sso_redirect_uri": "VARCHAR", "sso_user_infos_to_share": "JSON" }
for col_name, col_sql_def in new_system_app_cols_defs.items():
if col_name not in system_app_columns_db:
connection.execute(text(f"ALTER TABLE system_apps ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
if inspector.has_table("app_zoo_repositories"):
columns_db = [col['name'] for col in inspector.get_columns('app_zoo_repositories')]
if 'is_deletable' not in columns_db: connection.execute(text("ALTER TABLE app_zoo_repositories ADD COLUMN is_deletable BOOLEAN DEFAULT 1 NOT NULL"))
if 'type' not in columns_db: connection.execute(text("ALTER TABLE app_zoo_repositories ADD COLUMN type VARCHAR DEFAULT 'git' NOT NULL"))
connection.commit()
if inspector.has_table("mcp_zoo_repositories"):
columns_db = [col['name'] for col in inspector.get_columns('mcp_zoo_repositories')]
if 'is_deletable' not in columns_db: connection.execute(text("ALTER TABLE mcp_zoo_repositories ADD COLUMN is_deletable BOOLEAN DEFAULT 1 NOT NULL"))
if 'type' not in columns_db: connection.execute(text("ALTER TABLE mcp_zoo_repositories ADD COLUMN type VARCHAR DEFAULT 'git' NOT NULL"))
connection.commit()
if inspector.has_table("prompt_zoo_repositories"):
columns_db = [col['name'] for col in inspector.get_columns('prompt_zoo_repositories')]
if 'is_deletable' not in columns_db: connection.execute(text("ALTER TABLE prompt_zoo_repositories ADD COLUMN is_deletable BOOLEAN DEFAULT 1 NOT NULL"))
if 'type' not in columns_db: connection.execute(text(f"ALTER TABLE prompt_zoo_repositories ADD COLUMN type VARCHAR DEFAULT 'git' NOT NULL"))
connection.commit()
if not inspector.has_table("personality_zoo_repositories"):
from backend.db.models.service import PersonalityZooRepository
PersonalityZooRepository.__table__.create(connection)
connection.commit()
if not inspector.has_table("broadcast_messages"):
from backend.db.models.broadcast import BroadcastMessage
BroadcastMessage.__table__.create(connection)
connection.commit()
else:
# --- STARTUP CLEANUP ---
# Clear the broadcast queue on every server start.
# This prevents "Sync Storms" where a new worker process tries to broadcast
# massive stale messages (like base64 images) from a previous crashed session.
try:
connection.execute(text("DELETE FROM broadcast_messages"))
connection.commit()
print("INFO: Broadcast message sync queue cleared for clean startup.")
except Exception as e:
print(f"WARNING: Could not clear broadcast queue: {e}")
connection.rollback()
if not inspector.has_table("shared_discussion_links"):
from backend.db.models.discussion import SharedDiscussionLink
SharedDiscussionLink.__table__.create(connection)
connection.commit()
if inspector.has_table("fun_fact_categories"):
fun_fact_cat_cols = [col['name'] for col in inspector.get_columns('fun_fact_categories')]
if 'color' not in fun_fact_cat_cols:
connection.execute(text("ALTER TABLE fun_fact_categories ADD COLUMN color VARCHAR(7) DEFAULT '#4299e1' NOT NULL"))
connection.commit()
if not inspector.has_table("fun_fact_categories"):
FunFactCategory.__table__.create(connection)
if not inspector.has_table("fun_facts"):
FunFact.__table__.create(connection)
# NEW: Scheduled Tasks Table
if not inspector.has_table("scheduled_tasks"):
from backend.db.models.db_task import ScheduledTask
ScheduledTask.__table__.create(connection)
connection.commit()
if not inspector.has_table("rss_feed_sources"):
from backend.db.models.news import RSSFeedSource
RSSFeedSource.__table__.create(connection)
connection.commit()
if not inspector.has_table("news_articles"):
from backend.db.models.news import NewsArticle
NewsArticle.__table__.create(connection)
connection.commit()
if not inspector.has_table("tts_bindings"):
TTSBinding.__table__.create(connection)
connection.commit()
if not inspector.has_table("stt_bindings"):
STTBinding.__table__.create(connection)
connection.commit()
if not inspector.has_table("rag_bindings"):
RAGBinding.__table__.create(connection)
connection.commit()
if not inspector.has_table("websocket_connections"):
WebSocketConnection.__table__.create(connection)
connection.commit()
if inspector.has_table("fun_fact_categories"):
_bootstrap_fun_facts(connection)
_migrate_model_aliases_to_universal_profiles(connection)
if not inspector.has_table("datastores"):
from backend.db.models.datastore import DataStore, SharedDataStoreLink
DataStore.__table__.create(connection)
SharedDataStoreLink.__table__.create(connection)
connection.commit()
else:
datastore_columns_db = [col['name'] for col in inspector.get_columns('datastores')]
if 'vectorizer_name' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN vectorizer_name VARCHAR"))
if 'vectorizer_config' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN vectorizer_config JSON"))
if 'chunk_size' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN chunk_size INTEGER"))
if 'chunk_overlap' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN chunk_overlap INTEGER"))
if 'chunking_strategy' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN chunking_strategy VARCHAR DEFAULT 'recursive'"))
if 'chunking_kwargs' not in datastore_columns_db: connection.execute(text("ALTER TABLE datastores ADD COLUMN chunking_kwargs JSON DEFAULT '{}'"))
connection.execute(text("UPDATE datastores SET vectorizer_name = 'st' WHERE vectorizer_name IS NULL"))
connection.execute(text("UPDATE datastores SET vectorizer_config = '{}' WHERE vectorizer_config IS NULL"))
connection.execute(text("UPDATE datastores SET chunking_strategy = 'recursive' WHERE chunking_strategy IS NULL"))
connection.execute(text("UPDATE datastores SET chunking_kwargs = '{}' WHERE chunking_kwargs IS NULL"))
connection.execute(text(f"UPDATE datastores SET chunk_size = {SAFE_STORE_DEFAULTS.get('default_chunk_size', 2048)} WHERE chunk_size IS NULL"))
connection.execute(text(f"UPDATE datastores SET chunk_overlap = {SAFE_STORE_DEFAULTS.get('default_chunk_overlap', 256)} WHERE chunk_overlap IS NULL"))
connection.commit()
# Migrate all existing SQLite database files for all user DataStores
from backend.session import _migrate_datastore_sqlite_schema
users_dir = APP_DATA_DIR / USERS_DIR_NAME
if users_dir.exists() and users_dir.is_dir():
for user_folder in users_dir.iterdir():
if user_folder.is_dir():
stores_folder = user_folder / "safestores"
if stores_folder.exists() and stores_folder.is_dir():
for db_file in stores_folder.glob("*.db"):
_migrate_datastore_sqlite_schema(db_file)
if not inspector.has_table("conversations"):
Conversation.__table__.create(connection)
if not inspector.has_table("conversation_members"):
ConversationMember.__table__.create(connection)
else:
cm_cols = [c['name'] for c in inspector.get_columns('conversation_members')]
if 'id' not in cm_cols:
try:
data = connection.execute(text("SELECT * FROM conversation_members")).mappings().all()
connection.execute(text("DROP TABLE conversation_members;"))
ConversationMember.__table__.create(connection)
if data:
insert_stmt = ConversationMember.__table__.insert()
for row in data:
row_data = {k: v for k, v in row.items() if k in cm_cols}
connection.execute(insert_stmt.values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
if inspector.has_table("direct_messages"):
dm_columns = [col['name'] for col in inspector.get_columns('direct_messages')]
if 'conversation_id' not in dm_columns:
try:
connection.execute(text("ALTER TABLE direct_messages ADD COLUMN conversation_id INTEGER REFERENCES conversations(id) ON DELETE CASCADE"))
except Exception as e:
pass
dm_cols_full = inspector.get_columns('direct_messages')
receiver_col_info = next((c for c in dm_cols_full if c['name'] == 'receiver_id'), None)
if receiver_col_info and not receiver_col_info.get('nullable'):
try:
data = connection.execute(text("SELECT * FROM direct_messages")).mappings().all()
connection.execute(text("DROP TABLE direct_messages;"))
DirectMessage.__table__.create(connection)
if data:
new_cols = [c.name for c in DirectMessage.__table__.columns]
insert_stmt = DirectMessage.__table__.insert()
for row in data:
row_data = {k: v for k, v in row.items() if k in new_cols}
for date_field in ['sent_at', 'read_at']:
if date_field in row_data and isinstance(row_data[date_field], str):
try: row_data[date_field] = datetime.fromisoformat(row_data[date_field])
except: pass
connection.execute(insert_stmt.values(row_data))
connection.commit()
except Exception as e:
connection.rollback()
if not inspector.has_table("email_topics"):
from backend.db.models.email_marketing import EmailTopic
EmailTopic.__table__.create(connection)
connection.commit()
if not inspector.has_table("email_proposals"):
from backend.db.models.email_marketing import EmailProposal
EmailProposal.__table__.create(connection)
connection.commit()
else:
# Check for recipients column
ep_cols = [c['name'] for c in inspector.get_columns("email_proposals")]
if "recipients" not in ep_cols:
print("INFO: Adding 'recipients' column to 'email_proposals' table.")
connection.execute(text("ALTER TABLE email_proposals ADD COLUMN recipients JSON"))
connection.execute(text("UPDATE email_proposals SET recipients = '[]'"))
connection.commit()
if not inspector.has_table("note_groups"):
NoteGroup.__table__.create(connection)
connection.commit()
if not inspector.has_table("notes"):
Note.__table__.create(connection)
connection.commit()
if not inspector.has_table("skills"):
from backend.db.models.skill import Skill
Skill.__table__.create(connection)
connection.commit()
else:
# --- UPGRADE SKILLS TABLE ---
skills_columns_db = [col['name'] for col in inspector.get_columns('skills')]
new_skill_cols = {
"author": "VARCHAR",
"version": "VARCHAR",
"language": "VARCHAR DEFAULT 'markdown'",
"created_at": "DATETIME",
"updated_at": "DATETIME"
}
for col_name, col_sql_def in new_skill_cols.items():
if col_name not in skills_columns_db:
try:
connection.execute(text(f"ALTER TABLE skills ADD COLUMN {col_name} {col_sql_def}"))
connection.commit()
print(f"INFO: Added '{col_name}' column to 'skills' table.")
except Exception as e:
connection.rollback()
print(f"WARNING: Failed to add column {col_name} to skills table: {e}")
# Ensure index on name exists
connection.execute(text("CREATE INDEX IF NOT EXISTS ix_skills_name ON skills (name)"))
connection.commit()
if not inspector.has_table("saved_artefacts"):
from backend.db.models.saved_artefact import SavedArtefact
SavedArtefact.__table__.create(connection)
connection.commit()
else:
saved_artefact_columns_db = [col['name'] for col in inspector.get_columns('saved_artefacts')]
if 'version' not in saved_artefact_columns_db:
try:
connection.execute(text("ALTER TABLE saved_artefacts ADD COLUMN version INTEGER DEFAULT 1 NOT NULL"))
connection.commit()
except Exception:
connection.rollback()
if not inspector.has_table("shared_artefact_links"):
from backend.db.models.saved_artefact import SharedArtefactLink
SharedArtefactLink.__table__.create(connection)
connection.commit()
if not inspector.has_table("notebooks"):
try:
Notebook.__table__.create(connection)
connection.commit()
print("INFO: Created 'notebooks' table.")
except Exception as e:
connection.rollback()
print(f"ERROR: Failed to create notebooks table: {e}")
else:
# Check for 'tabs' column in notebooks
notebook_columns = [col['name'] for col in inspector.get_columns('notebooks')]
# Add 'language' column if missing
if 'language' not in notebook_columns:
ASCIIColors.info("Migrating database: Adding 'language' column to 'notebooks' table")
try:
# SQLite supports ADD COLUMN. We provide a default 'en'
connection.execute(text("ALTER TABLE notebooks ADD COLUMN language VARCHAR DEFAULT 'en'"))
connection.commit()
ASCIIColors.success("Successfully added 'language' column to 'notebooks'.")
except Exception as e:
ASCIIColors.error(f"Failed to migrate notebooks table: {e}")
# Check for any other missing notebook notebook_columns if necessary
if 'artefacts' not in notebook_columns:
ASCIIColors.info("Migrating database: Adding 'artefacts' column to 'notebooks' table")
try:
connection.execute(text("ALTER TABLE notebooks ADD COLUMN artefacts JSON DEFAULT '[]'"))
connection.commit()
except Exception as e:
ASCIIColors.error(f"Failed to add artefacts column: {e}")
if 'tabs' not in notebook_columns:
try:
connection.execute(text("ALTER TABLE notebooks ADD COLUMN tabs JSON"))
connection.execute(text("UPDATE notebooks SET tabs = '[]' WHERE tabs IS NULL"))
connection.commit()
print("INFO: Added 'tabs' column to 'notebooks' table.")
except Exception as e:
print(f"WARNING: Failed to add 'tabs' column to notebooks table: {e}")
connection.rollback()
# NEW: Check for 'type' column in notebooks
if 'type' not in notebook_columns:
try:
connection.execute(text("ALTER TABLE notebooks ADD COLUMN type VARCHAR DEFAULT 'generic'"))
connection.commit()
print("INFO: Added 'type' column to 'notebooks' table.")
except Exception as e:
print(f"WARNING: Failed to add 'type' column to notebooks table: {e}")
connection.rollback()
if "google_search_queries" not in notebook_columns:
try:
print("Migrating notebooks: adding google_search_queries column")
connection.execute(text("ALTER TABLE notebooks ADD COLUMN google_search_queries JSON"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add 'google_search_queries' column to notebooks table: {e}")
connection.rollback()
if "arxiv_queries" not in notebook_columns:
try:
print("Migrating notebooks: adding arxiv_queries column")
connection.execute(text("ALTER TABLE notebooks ADD COLUMN arxiv_queries JSON"))
connection.commit()
except Exception as e:
print(f"WARNING: Failed to add 'arxiv_queries' column to notebooks table: {e}")
connection.rollback()
connection.commit()
def _migrate_model_aliases_to_universal_profiles(connection):
"""
Migrates legacy model aliases to the Universal Two-Tier Profile Architecture across all binding tables.
Normalizes vision_enabled, forced_context_size, routing_config, and vlm_model_profile.
Also heals orphaned model references in the users table.
"""
print("INFO: Checking and migrating model aliases to Universal Model Profiles...")
binding_tables = [
"llm_bindings", "tti_bindings", "tts_bindings",
"stt_bindings", "ttv_bindings", "ttm_bindings", "rag_bindings"
]
active_llm_profiles = set()
for tbl in binding_tables:
try:
records = connection.execute(text(f"SELECT id, alias, name, model_aliases, default_model_name FROM {tbl}")).fetchall()
for row in records:
b_id, b_alias, b_name, raw_aliases, def_model = row[0], row[1], row[2], row[3], row[4]
aliases_dict = {}
if raw_aliases:
if isinstance(raw_aliases, str):
try:
aliases_dict = json.loads(raw_aliases)
except Exception:
aliases_dict = {}
elif isinstance(raw_aliases, dict):
aliases_dict = raw_aliases
modified = False
upgraded_aliases = {}
for orig_name, data in aliases_dict.items():
if not isinstance(data, dict):
data = {"title": str(data)}
cfg = data.get("alias", {}) if "alias" in data else data
# 1. Normalize Vision flag
is_vision = bool(cfg.get("vision_enabled") if cfg.get("vision_enabled") is not None else cfg.get("has_vision", False))
# 2. Normalize Forced Context Size
forced_ctx = cfg.get("forced_context_size") if cfg.get("forced_context_size") is not None else cfg.get("ctx_size")
if forced_ctx is not None:
try: forced_ctx = int(forced_ctx)
except (ValueError, TypeError): forced_ctx = None
# 3. Normalize Smart Routing Config
routing_cfg = cfg.get("routing_config", {})
if not isinstance(routing_cfg, dict):
routing_cfg = {}
default_routing = {
"description": routing_cfg.get("description") or cfg.get("description") or cfg.get("title") or orig_name,
"complexity_tier": int(routing_cfg.get("complexity_tier", 2)),
"cost_per_1k_tokens": float(routing_cfg.get("cost_per_1k_tokens", 0.0)),
"avg_latency_ms": int(routing_cfg.get("avg_latency_ms", 200)),
"priority": int(routing_cfg.get("priority", 1))
}
# 4. Normalize VLM companion model profile
vlm_prof = cfg.get("vlm_model_profile", None)
upgraded_entry = {
"binding_profile_name": b_alias,
"model_name": orig_name,
"title": cfg.get("title") or cfg.get("name") or orig_name,
"name": cfg.get("name") or cfg.get("title") or orig_name,
"description": cfg.get("description", ""),
"vision_enabled": is_vision,
"has_vision": is_vision,
"forced_context_size": forced_ctx,
"ctx_size": forced_ctx,
"temperature": cfg.get("temperature"),
"top_k": cfg.get("top_k"),
"top_p": cfg.get("top_p"),
"repeat_penalty": cfg.get("repeat_penalty"),
"repeat_last_n": cfg.get("repeat_last_n"),
"allow_parameters_override": cfg.get("allow_parameters_override", True),
"reasoning_activation": cfg.get("reasoning_activation", False),
"reasoning_effort": cfg.get("reasoning_effort"),
"reasoning_summary": cfg.get("reasoning_summary", False),
"icon": cfg.get("icon"),
"routing_config": default_routing,
"vlm_model_profile": vlm_prof
}
upgraded_aliases[orig_name] = upgraded_entry
modified = True
if tbl == "llm_bindings":
active_llm_profiles.add(f"{b_alias}/{orig_name}")
active_llm_profiles.add(upgraded_entry["title"])
if tbl == "llm_bindings" and def_model:
active_llm_profiles.add(f"{b_alias}/{def_model}")
if modified:
connection.execute(
text(f"UPDATE {tbl} SET model_aliases = :aliases WHERE id = :id"),
{"aliases": json.dumps(upgraded_aliases), "id": b_id}
)
connection.commit()
except Exception as e:
print(f"WARNING: Profile migration error on table {tbl}: {e}")
connection.rollback()
# Self-heal orphaned user models
try:
if active_llm_profiles:
def_llm_rec = connection.execute(text("SELECT alias, default_model_name FROM llm_bindings WHERE is_active = 1 ORDER BY id ASC LIMIT 1")).fetchone()
if def_llm_rec and def_llm_rec[0] and def_llm_rec[1]:
fallback_full_model = f"{def_llm_rec[0]}/{def_llm_rec[1]}"
users = connection.execute(text("SELECT id, username, lollms_model_name FROM users")).fetchall()
healed_count = 0
for u in users:
u_id, u_name, u_model = u[0], u[1], u[2]
if not u_model or (u_model not in active_llm_profiles and "/" in u_model and u_model.split("/")[0] not in [r[1] for r in records if tbl == "llm_bindings"]):
connection.execute(
text("UPDATE users SET lollms_model_name = :fallback WHERE id = :id"),
{"fallback": fallback_full_model, "id": u_id}
)
healed_count += 1
if healed_count > 0:
connection.commit()
print(f"INFO: Self-healed {healed_count} user(s) with fallback model '{fallback_full_model}'.")
except Exception as e:
print(f"WARNING: User self-healing fallback warning: {e}")
connection.rollback()
def check_and_update_db_version(SessionLocal):
session = SessionLocal()
try:
version_record = session.query(DatabaseVersion).filter_by(id=1).first()
if not version_record:
new_version_record = DatabaseVersion(id=1, version=CURRENT_DB_VERSION)
session.add(new_version_record)
session.commit()
elif version_record.version != CURRENT_DB_VERSION:
version_record.version = CURRENT_DB_VERSION
session.commit()
except Exception:
session.rollback()
finally:
session.close()
def _migrate_user_data_folders(connection):
users_root_path = APP_DATA_DIR / USERS_DIR_NAME
users_root_path.mkdir(exist_ok=True)
try:
usernames = {row[0] for row in connection.execute(text("SELECT username FROM users")).fetchall()}
non_user_folders = {USERS_DIR_NAME, "cache", "zoo", "apps", "mcps", "custom_apps", "apps_zoo", "mcps_zoo", "prompts_zoo", "personalities_zoo", "huggingface_cache"}
for item in APP_DATA_DIR.iterdir():
if item.is_dir() and item.name in usernames and item.name not in non_user_folders:
old_path = item
new_path = users_root_path / item.name
if not new_path.exists():
try: shutil.move(str(old_path), str(new_path))
except Exception: pass
except Exception: pass
def _bootstrap_default_nodes(connection):
"""Inserts base node types and enhanced multimedia types."""
from sqlalchemy import text
import uuid
# ------------------ DEFINE NODES ------------------
nodes = [
# --- GENERAL ---
{
"name": "input_text", "label": "Text Input", "category": "General", "class_name": "CustomNode", "is_public": True,
"description": "Standard node to provide manual text input to the workflow.",
"inputs": [], "outputs": [{"name": "text", "type": "string"}],
"color": "bg-blue-100 dark:bg-blue-900 border-blue-500", "requirements": [],
"code": "class CustomNode:\n def execute(self, inputs, ctx):\n return {'text': inputs.get('value', '')}"
},
# --- AI GENERATION ---
{
"name": "llm_generate", "label": "Text Generator", "category": "AI Generation", "class_name": "CustomNode", "is_public": True,
"description": "Generates text using the primary LLM binding.",
"inputs": [{"name": "prompt", "type": "string"}, {"name": "system_prompt", "type": "string", "optional": True}],
"outputs": [{"name": "generated_text", "type": "markdown"}],
"color": "bg-purple-100 dark:bg-purple-900 border-purple-500", "requirements": [],
"code": """
class CustomNode:
def execute(self, inputs, context):
prompt = inputs.get("prompt", "")
system = inputs.get("system_prompt", "")
if not prompt: return {"generated_text": ""}
client = context.lollms_client
return {"generated_text": client.generate_text(prompt, system_prompt=system)}
"""
},
{
"name": "text_to_image", "label": "Image Generator", "category": "AI Generation", "class_name": "CustomNode", "is_public": True,
"description": "Generates images from text using the configured TTI binding.",
"inputs": [{"name": "prompt", "type": "string"}, {"name": "width", "type": "int", "default": 1024}, {"name": "height", "type": "int", "default": 1024}],
"outputs": [{"name": "image_b64", "type": "image"}],
"color": "bg-pink-100 dark:bg-pink-900 border-pink-500", "requirements": [],
"code": """
class CustomNode:
def execute(self, inputs, context):
prompt = inputs.get("prompt", "")
if not prompt: return {"image_b64": ""}
client = context.lollms_client
if not client.tti: raise Exception("No TTI binding configured")
img_bytes = client.tti.generate_image(prompt, width=inputs.get('width', 1024), height=inputs.get('height', 1024))
import base64
return {"image_b64": "data:image/png;base64," + base64.b64encode(img_bytes).decode('utf-8')}
"""
},
# --- VIDEO GENERATION ---
{
"name": "image_to_video", "label": "Image to Video", "category": "Video Generation", "class_name": "CustomNode", "is_public": True,
"description": "Generates a short video clip from a base image using Diffusers SVD.",
"inputs": [{"name": "image_b64", "type": "image"}, {"name": "num_frames", "type": "int", "default": 14}],
"outputs": [{"name": "video_url", "type": "string"}],
"color": "bg-indigo-100 dark:bg-indigo-900 border-indigo-500", "requirements": ["diffusers", "accelerate", "opencv-python"],
"code": """
class CustomNode:
def execute(self, inputs, context):
# Implementation would use Diffusers StableVideoDiffusionPipeline
return {"video_url": "video_generation_simulated.mp4"}
"""
},
# --- AUDIO EFFECTS ---
{
"name": "audio_echo", "label": "Audio Echo", "category": "Audio Effects", "class_name": "CustomNode", "is_public": True,
"description": "Applies an echo effect to a base64 encoded wav audio stream.",
"inputs": [{"name": "audio_b64", "type": "string"}, {"name": "delay_ms", "type": "int", "default": 300}],
"outputs": [{"name": "audio_b64", "type": "string"}],
"color": "bg-orange-100 dark:bg-orange-900 border-orange-500", "requirements": ["pydub"],
"code": """
class CustomNode:
def execute(self, inputs, context):
import base64, io
from pydub import AudioSegment
audio_data = base64.b64decode(inputs.get('audio_b64', ''))
sound = AudioSegment.from_file(io.BytesIO(audio_data), format="wav")
# Simple echo by overlaying
echo = sound - 10 # quieter
combined = sound.overlay(echo, position=inputs.get('delay_ms', 300))
out = io.BytesIO()
combined.export(out, format="wav")
return {"audio_b64": base64.b64encode(out.getvalue()).decode()}
"""
},
# --- IMAGE EFFECTS ---
{
"name": "image_edge_detect", "label": "Edge Detection", "category": "Image Effects", "class_name": "CustomNode", "is_public": True,
"description": "Detects edges in an image using Canny filter.",
"inputs": [{"name": "image_b64", "type": "image"}],
"outputs": [{"name": "image_b64", "type": "image"}],
"color": "bg-teal-100 dark:bg-teal-900 border-teal-500", "requirements": ["opencv-python", "numpy"],
"code": """
class CustomNode:
def execute(self, inputs, context):
import cv2, numpy as np, base64
img_data = base64.b64decode(inputs.get('image_b64', '').split(',')[-1])
nparr = np.frombuffer(img_data, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
edges = cv2.Canny(img, 100, 200)
_, buffer = cv2.imencode('.png', edges)
return {"image_b64": "data:image/png;base64," + base64.b64encode(buffer).decode()}
"""
},
# --- CUSTOM TRANSFORMERS ---
{
"name": "hf_object_detect", "label": "HF Object Detector", "category": "Transformers", "class_name": "CustomNode", "is_public": True,
"description": "Detects objects in an image using a Hugging Face pipeline.",
"inputs": [{"name": "image_b64", "type": "image"}],
"outputs": [{"name": "detections", "type": "list"}],
"color": "bg-yellow-100 dark:bg-yellow-900 border-yellow-500", "requirements": ["transformers", "torch", "timm"],
"code": """
class CustomNode:
def execute(self, inputs, context):
from transformers import pipeline
from PIL import Image
import io, base64
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
img_data = base64.b64decode(inputs.get('image_b64', '').split(',')[-1])
img = Image.open(io.BytesIO(img_data))
results = detector(img)
return {"detections": results}
"""
},
# --- RAG ---
{
"name": "datastore_query", "label": "RAG Query", "category": "Knowledge", "class_name": "CustomNode", "is_public": True,
"description": "Queries a Data Store and returns relevant document chunks.",
"inputs": [{"name": "datastore_id", "type": "string"}, {"name": "query", "type": "string"}, {"name": "top_k", "type": "int", "default": 5}],
"outputs": [{"name": "context", "type": "markdown"}, {"name": "sources", "type": "list"}],
"color": "bg-green-100 dark:bg-green-900 border-green-500", "requirements": [],
"code": """
class CustomNode:
def execute(self, inputs, context):
ds_id = inputs.get("datastore_id")
query = inputs.get("query", "")
if not ds_id or not query: return {"context": "", "sources": []}
from backend.session import get_safe_store_instance
db = next(context.engine.db_session_factory())
try:
ss = get_safe_store_instance(context.owner_username, ds_id, db)
results = ss.query(query, top_k=inputs.get('top_k', 5))
chunks = [r['chunk_text'] for r in results]
return {"context": "\\n---\\n".join(chunks), "sources": results}
finally:
db.close()
"""
}
]
# ------------------ SYNC WITH DB ------------------
for node in nodes:
# Check if exists
existing = connection.execute(
text("SELECT id FROM flow_node_definitions WHERE name = :name"),
{"name": node["name"]}
).first()
if existing:
# Update
connection.execute(
text("""
UPDATE flow_node_definitions
SET label = :label, category = :category, class_name = :class_name, description = :description, is_public = :is_public,
inputs = :inputs, outputs = :outputs, color = :color, code = :code, requirements = :requirements
WHERE name = :name
"""),
{
"name": node["name"],
"label": node["label"],
"category": node["category"],
"class_name": node["class_name"],
"description": node["description"],
"is_public": node["is_public"],
"inputs": json.dumps(node["inputs"]),
"outputs": json.dumps(node["outputs"]),
"color": node["color"],
"code": node["code"],
"requirements": json.dumps(node["requirements"])
}
)
else:
# Insert
connection.execute(
text("""
INSERT INTO flow_node_definitions (id, name, label, category, class_name, description, is_public, inputs, outputs, color, code, requirements)
VALUES (:id, :name, :label, :category, :class_name, :description, :is_public, :inputs, :outputs, :color, :code, :requirements)
"""),
{
"id": str(uuid.uuid4()),
"name": node["name"],
"label": node["label"],
"category": node["category"],
"class_name": node["class_name"],
"description": node["description"],
"is_public": node["is_public"],
"inputs": json.dumps(node["inputs"]),
"outputs": json.dumps(node["outputs"]),
"color": node["color"],
"code": node["code"],
"requirements": json.dumps(node["requirements"])
}
)
connection.commit()
# Standard Library Imports
import json
import re
import shutil
from datetime import datetime
from pathlib import Path
from sqlalchemy import text
from sqlalchemy.exc import OperationalError, IntegrityError
from sqlalchemy.schema import DropTable
from sqlalchemy.ext.compiler import compiles
from backend.config import SERVER_CONFIG, APP_SETTINGS, SAFE_STORE_DEFAULTS, APP_DATA_DIR, USERS_DIR_NAME
from backend.db.base import CURRENT_DB_VERSION
from backend.db.models.config import GlobalConfig, LLMBinding, TTIBinding, TTSBinding, DatabaseVersion, RAGBinding, STTBinding
from backend.db.models.service import App
from backend.db.models.prompt import SavedPrompt
from backend.db.models.fun_fact import FunFactCategory, FunFact
from backend.db.models.user import User
from backend.db.models.group import Group
from backend.db.models.memory import UserMemory
from backend.db.models.connections import WebSocketConnection
from backend.db.models.image import UserImage, ImageAlbum
from backend.db.models.dm import Conversation, ConversationMember, DirectMessage
from backend.db.models.note import Note, NoteGroup
from backend.db.models.notebook import Notebook
from backend.db.models.flow import Flow, FlowNodeDefinition
from backend.db.models.saved_artefact import SavedArtefact
from backend.db.models.db_task import ScheduledTask # Assuming we will add this
from ascii_colors import ASCIIColors, trace_exception
def _drop_table(element, compiler, **kw):
return "DROP TABLE %s;" % compiler.preparer.format_table(element.element)
def _get_all_existing_app_ports(connection) -> set[int]:
"""Retrieves all non-null ports currently used by apps in the database."""
return {r[0] for r in connection.execute(text("SELECT port FROM apps WHERE port IS NOT NULL")).fetchall()}
def _find_unique_port_for_migration(connection, preferred_port: int, already_used_in_batch: set) -> int:
"""
Finds a unique port during migration by checking against DB and in-batch used ports.
Starts searching from preferred_port upwards.
"""
all_db_used_ports_at_start = _get_all_existing_app_ports(connection)
current_port = preferred_port if preferred_port is not None and preferred_port >= 1024 else 9601
while True:
if current_port in already_used_in_batch or current_port in all_db_used_ports_at_start:
current_port += 1
else:
return current_port
def _bootstrap_global_settings(connection):
"""
Ensures all necessary global settings exist in the database.
"""
print("INFO: Checking and bootstrapping global settings in the database.")
all_possible_settings = {
"allow_dangerous_flow_execution": { "value": False, "type": "boolean", "description": "Enable Flow Studio Python code execution. Enabling this allows arbitrary code execution on the server host.", "category": "Security" },
"host": { "value": SERVER_CONFIG.get("host", "0.0.0.0"), "type": "string", "description": "Server host address. Requires a restart to take effect.", "category": "Server" },
"port": { "value": SERVER_CONFIG.get("port", 9642), "type": "integer", "description": "Server port. Requires a restart to take effect.", "category": "Server" },
"https_enabled": { "value": SERVER_CONFIG.get("https_enabled", False), "type": "boolean", "description": "Enable HTTPS for the server. Requires a restart to take effect.", "category": "Server" },
"ssl_certfile": { "value": SERVER_CONFIG.get("ssl_certfile", ""), "type": "string", "description": "Path to the SSL certificate file (e.g., cert.pem). Requires a restart.", "category": "Server" },
"ssl_keyfile": { "value": SERVER_CONFIG.get("ssl_keyfile", ""), "type": "string", "description": "Path to the SSL private key file (e.g., key.pem). Requires a restart.", "category": "Server" },
"public_domain_name": { "value": APP_SETTINGS.get("public_domain_name", ""), "type": "string", "description": "Public domain or IP to use for links when server is on 0.0.0.0. If empty, auto-detects IP.", "category": "Server" },
"cors_origins_exceptions": { "value": "", "type": "string", "description": "A comma-separated list of additional origins to allow for CORS (e.g., http://localhost:8000,https://my-app.com). Requires restart.", "category": "Server" },
"data_path": { "value": str(APP_DATA_DIR.resolve()), "type": "string", "description": "The root directory for all application data.", "category": "Server" },
"huggingface_cache_path": { "value": str((APP_DATA_DIR / "huggingface_cache").resolve()), "type": "string", "description": "Path for Hugging Face model cache. Requires a restart to change.", "category": "Server" },
"backup_password": { "value": "", "type": "string", "description": "Password to encrypt application backups.", "category": "Backup" },
"allow_new_registrations": { "value": APP_SETTINGS.get("allow_new_registrations", True), "type": "boolean", "description": "Allow new users to register an account.", "category": "Registration" },
"registration_mode": { "value": APP_SETTINGS.get("registration_mode", "admin_approval"), "type": "string", "description": "Method for new user activation: 'direct' or 'admin_approval'.", "category": "Registration" },
"access_token_expire_minutes": { "value": APP_SETTINGS.get("access_token_expires_minutes", 43200), "type": "integer", "description": "Duration in minutes a user's login session remains valid.", "category": "Authentication" },
"password_recovery_mode": { "value": "manual", "type": "string", "description": "Password recovery mode: 'manual', 'automatic' (SMTP), or 'system_mail' (uses server's 'mail' command).", "category": "Authentication" },
"email_verification_enabled": { "value": False, "type": "boolean", "description": "Require email verification code (2FA) upon user login.", "category": "Authentication" },
"email_verification_code_expiry_minutes": { "value": 10, "type": "integer", "description": "Expiry time in minutes for email login verification codes.", "category": "Authentication" },
"email_verification_bypass_admins": { "value": False, "type": "boolean", "description": "Allow administrator accounts to bypass email login verification.", "category": "Authentication" },
"sso_client_enabled": { "value": False, "type": "boolean", "description": "Enable Single Sign-On (SSO) for users to log in via an external provider.", "category": "SSO Client" },
"sso_client_provider_url": { "value": "", "type": "string", "description": "The OpenID Connect (OIDC) provider's discovery URL (e.g., https://accounts.google.com/.well-known/openid-configuration).", "category": "SSO Client" },
"sso_client_id": { "value": "", "type": "string", "description": "The Client ID provided by your SSO provider.", "category": "SSO Client" },
"sso_client_secret": { "value": "", "type": "string", "description": "The Client Secret provided by your SSO provider.", "category": "SSO Client" },
"sso_client_display_name": { "value": "Login with SSO", "type": "string", "description": "The text displayed on the SSO login button.", "category": "SSO Client" },
"sso_client_icon_url": { "value": "", "type": "string", "description": "URL for an icon to display on the SSO login button.", "category": "SSO Client" },
"sso_client_auto_create_users": { "value": True, "type": "boolean", "description": "Automatically create a lollms account for new users who sign in via SSO.", "category": "SSO Client" },
"scim_enabled": { "value": False, "type": "boolean", "description": "Enable SCIM 2.0 provisioning for users and groups.", "category": "SCIM Provisioning" },
"scim_token": { "value": "", "type": "string", "description": "The bearer token for authenticating SCIM requests. A new one will be generated if left empty.", "category": "SCIM Provisioning" },
"smtp_host": { "value": "", "type": "string", "description": "SMTP server address for sending password recovery emails.", "category": "Email Settings" },
"smtp_port": { "value": 587, "type": "integer", "description": "SMTP server port.", "category": "Email Settings" },
"smtp_user": { "value": "", "type": "string", "description": "Username for SMTP authentication.", "category": "Email Settings" },
"smtp_password": { "value": "", "type": "string", "description": "Password for SMTP authentication. (Stored in plaintext, use with caution)", "category": "Email Settings" },
"smtp_from_email": { "value": "", "type": "string", "description": "The 'From' email address for password recovery emails.", "category": "Email Settings" },
"smtp_use_tls": { "value": True, "type": "boolean", "description": "Use TLS for the SMTP connection.", "category": "Email Settings" },
"default_lollms_model_name": { "value": "", "type": "string", "description": "Default model name assigned to newly created users.", "category": "Defaults" },
"default_llm_ctx_size": { "value": 32000, "type": "integer", "description": "Default context size (in tokens) for new users.", "category": "Defaults" },
"default_llm_temperature": { "value": 0.7, "type": "float", "description": "Default generation temperature for new users.", "category": "Defaults" },
"default_user_ui_level": { "value": 0, "type": "integer", "description": "Default UI level for new users (0: Beginner, 2: Intermediate, 4: Expert).", "category": "Defaults" },
"default_auto_title": { "value": False, "type": "boolean", "description": "Enable automatic discussion title generation for new users by default.", "category": "Defaults" },
"default_safe_store_vectorizer": { "value": "default_st", "type": "string", "description": "Default vectorizer alias or name for newly created datastores.", "category": "RAG" },
"restrict_vectorizers_to_aliases": { "value": False, "type": "boolean", "description": "If enabled, users can only choose from the admin-defined RAG Bindings when creating a new Data Store.", "category": "RAG" },
"default_chunk_size": { "value": 2048, "type": "integer", "description": "The default number of characters per text chunk for RAG indexing.", "category": "RAG" },
Xet Storage Details
- Size:
- 118 kB
- Xet hash:
- bc0c9134b6f407fcfe31064c9a0cded46ea4a8eb4291ad7a8d9e63259bb21962
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.