Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Feature: Add resource and settings service modules

hashedviking9fa543a4f7d5
diff --git a/src/local_deep_research/web/services/resource_service.py b/src/local_deep_research/web/services/resource_service.py
new file mode 100644
index 0000000000..91a9b9073c
--- /dev/null
+++ b/src/local_deep_research/web/services/resource_service.py
@@ -0,0 +1,149 @@
+import json
+import logging
+from datetime import datetime
+
+from ..models.database import get_db_connection
+
+logger = logging.getLogger(__name__)
+
+
+def get_resources_for_research(research_id):
+ """
+ Retrieve resources associated with a specific research project
+
+ Args:
+ research_id (int): The ID of the research
+
+ Returns:
+ list: List of resource objects for the research
+ """
+ try:
+ conn = get_db_connection()
+ cursor = conn.cursor()
+
+ # Query to get resources for the research
+ cursor.execute(
+ "SELECT id, research_id, title, url, content_preview, source_type, metadata "
+ "FROM research_resources WHERE research_id = ? ORDER BY id ASC",
+ (research_id,),
+ )
+
+ resources = []
+ for row in cursor.fetchall():
+ id, research_id, title, url, content_preview, source_type, metadata_str = (
+ row
+ )
+
+ # Parse metadata if available
+ metadata = {}
+ if metadata_str:
+ try:
+ metadata = json.loads(metadata_str)
+ except json.JSONDecodeError:
+ logger.warning(f"Invalid JSON in metadata for resource {id}")
+
+ resources.append(
+ {
+ "id": id,
+ "research_id": research_id,
+ "title": title,
+ "url": url,
+ "content_preview": content_preview,
+ "source_type": source_type,
+ "metadata": metadata,
+ }
+ )
+
+ conn.close()
+ return resources
+
+ except Exception as e:
+ logger.error(f"Error retrieving resources for research {research_id}: {str(e)}")
+ raise
+
+
+def add_resource(
+ research_id, title, url, content_preview=None, source_type="web", metadata=None
+):
+ """
+ Add a new resource to the research_resources table
+
+ Args:
+ research_id (int): The ID of the research
+ title (str): The title of the resource
+ url (str): The URL of the resource
+ content_preview (str, optional): A preview or snippet of the resource content
+ source_type (str, optional): The type of resource (web, pdf, etc.)
+ metadata (dict, optional): Additional metadata for the resource
+
+ Returns:
+ int: The ID of the newly created resource
+ """
+ try:
+ conn = get_db_connection()
+ cursor = conn.cursor()
+
+ created_at = datetime.utcnow().isoformat()
+ metadata_json = json.dumps(metadata) if metadata else None
+
+ cursor.execute(
+ "INSERT INTO research_resources (research_id, title, url, content_preview, source_type, metadata, created_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)",
+ (
+ research_id,
+ title,
+ url,
+ content_preview,
+ source_type,
+ metadata_json,
+ created_at,
+ ),
+ )
+
+ resource_id = cursor.lastrowid
+ conn.commit()
+ conn.close()
+
+ logger.info(f"Added resource {resource_id} for research {research_id}: {title}")
+ return resource_id
+
+ except Exception as e:
+ logger.error(f"Error adding resource for research {research_id}: {str(e)}")
+ raise
+
+
+def delete_resource(resource_id):
+ """
+ Delete a resource from the database
+
+ Args:
+ resource_id (int): The ID of the resource to delete
+
+ Returns:
+ bool: True if the resource was deleted successfully, False otherwise
+ """
+ try:
+ conn = get_db_connection()
+ cursor = conn.cursor()
+
+ # First check if the resource exists
+ cursor.execute("SELECT id FROM research_resources WHERE id = ?", (resource_id,))
+ result = cursor.fetchone()
+
+ if not result:
+ conn.close()
+ return False
+
+ # Delete the resource
+ cursor.execute("DELETE FROM research_resources WHERE id = ?", (resource_id,))
+
+ rows_affected = cursor.rowcount
+ conn.commit()
+ conn.close()
+
+ logger.info(f"Deleted resource {resource_id}, {rows_affected} rows affected")
+ return rows_affected > 0
+
+ except Exception as e:
+ logger.error(f"Error deleting resource {resource_id}: {str(e)}")
+ raise
diff --git a/src/local_deep_research/web/services/settings_service.py b/src/local_deep_research/web/services/settings_service.py
new file mode 100644
index 0000000000..e820863bff
--- /dev/null
+++ b/src/local_deep_research/web/services/settings_service.py
@@ -0,0 +1,187 @@
+import logging
+from pathlib import Path
+from typing import Any, Dict, Optional, Union
+
+from ..database.models import Setting, SettingType
+from .settings_manager import SettingsManager
+
+# Initialize logger
+logger = logging.getLogger(__name__)
+
+
+def get_settings_manager(db_session=None):
+ """
+ Get or create the settings manager instance.
+
+ Args:
+ db_session: Optional database session to use
+
+ Returns:
+ SettingsManager: The settings manager instance
+ """
+ return SettingsManager.get_instance(db_session)
+
+
+def get_setting(key: str, default: Any = None, db_session=None) -> Any:
+ """
+ Get a setting value by key
+
+ Args:
+ key: Setting key
+ default: Default value if setting not found
+ db_session: Optional database session to use
+
+ Returns:
+ Any: The setting value
+ """
+ manager = get_settings_manager(db_session)
+ return manager.get_setting(key, default)
+
+
+def set_setting(key: str, value: Any, commit: bool = True, db_session=None) -> bool:
+ """
+ Set a setting value
+
+ Args:
+ key: Setting key
+ value: Setting value
+ commit: Whether to commit the change
+ db_session: Optional database session
+
+ Returns:
+ bool: True if successful
+ """
+ manager = get_settings_manager(db_session)
+ return manager.set_setting(key, value, commit)
+
+
+def get_all_settings(
+ setting_type: Optional[SettingType] = None, db_session=None
+) -> Dict[str, Any]:
+ """
+ Get all settings, optionally filtered by type
+
+ Args:
+ setting_type: Optional filter by type
+ db_session: Optional database session
+
+ Returns:
+ Dict[str, Any]: Dictionary of settings
+ """
+ manager = get_settings_manager(db_session)
+ return manager.get_all_settings(setting_type)
+
+
+def get_all_settings_as_dict(db_session=None) -> Dict[str, Dict[str, Any]]:
+ """
+ Get all settings as a structured dictionary
+
+ Args:
+ db_session: Optional database session
+
+ Returns:
+ Dict[str, Dict[str, Any]]: Dictionary of settings grouped by type
+ """
+ # Get settings manager
+ manager = get_settings_manager(db_session)
+
+ # Get all settings
+ all_settings = {}
+
+ for setting_type in SettingType:
+ type_key = setting_type.value.lower()
+ type_settings = manager.get_all_settings(setting_type)
+ if type_settings:
+ all_settings[type_key] = type_settings
+
+ return all_settings
+
+
+def create_or_update_setting(
+ setting: Union[Dict[str, Any], Setting], commit: bool = True, db_session=None
+) -> Optional[Setting]:
+ """
+ Create or update a setting
+
+ Args:
+ setting: Setting dictionary or object
+ commit: Whether to commit the change
+ db_session: Optional database session
+
+ Returns:
+ Optional[Setting]: The setting object if successful
+ """
+ manager = get_settings_manager(db_session)
+ return manager.create_or_update_setting(setting, commit)
+
+
+def bulk_update_settings(
+ settings_dict: Dict[str, Any], commit: bool = True, db_session=None
+) -> bool:
+ """
+ Update multiple settings from a dictionary
+
+ Args:
+ settings_dict: Dictionary of setting keys and values
+ commit: Whether to commit the changes
+ db_session: Optional database session
+
+ Returns:
+ bool: True if all updates were successful
+ """
+ manager = get_settings_manager(db_session)
+ success = True
+
+ for key, value in settings_dict.items():
+ if not manager.set_setting(key, value, commit=False):
+ success = False
+
+ if commit and success and manager.db_session:
+ try:
+ manager.db_session.commit()
+ except Exception as e:
+ logger.error(f"Error committing bulk settings update: {e}")
+ manager.db_session.rollback()
+ success = False
+
+ return success
+
+
+def import_settings_from_file(
+ main_settings_file: Union[str, Path],
+ search_engines_file: Union[str, Path],
+ collections_file: Union[str, Path],
+ db_session=None,
+) -> bool:
+ """
+ Import settings from default configuration files
+
+ Args:
+ main_settings_file: Path to the main settings file
+ search_engines_file: Path to the search engines file
+ collections_file: Path to the collections file
+ db_session: Optional database session
+
+ Returns:
+ bool: True if import was successful
+ """
+ manager = get_settings_manager(db_session)
+ return manager.import_default_settings(
+ main_settings_file, search_engines_file, collections_file
+ )
+
+
+def validate_setting(setting: Setting, value: Any) -> tuple[bool, Optional[str]]:
+ """
+ Validate a setting value based on its type and constraints
+
+ Args:
+ setting: The Setting object to validate against
+ value: The value to validate
+
+ Returns:
+ tuple[bool, Optional[str]]: (is_valid, error_message)
+ """
+ from ..routes.settings_routes import validate_setting as routes_validate_setting
+
+ return routes_validate_setting(setting, value)