From b17871bf442b359c0ad195ede0f7e4ff9a21a60f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 14 Jul 2025 14:23:37 -0300 Subject: [PATCH] refactor(api): update /config endpoint to use ConfigResponse.from_settings (#8674) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: update config response handling and import structure - Moved EventManager import to the correct module path. - Refactored get_config endpoint to utilize ConfigResponse.from_settings for improved clarity and maintainability. - Updated ConfigResponse to include a class method for instantiating from Settings, enhancing the encapsulation of configuration logic. * 📝 Add docstrings to `improve-docs-and-configsetup` (#8677) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/backend/base/langflow/api/v1/endpoints.py | 37 ++++++++----------- src/backend/base/langflow/api/v1/schemas.py | 33 +++++++++++++++-- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/backend/base/langflow/api/v1/endpoints.py b/src/backend/base/langflow/api/v1/endpoints.py index f1ce98f35..9ad6c0fc7 100644 --- a/src/backend/base/langflow/api/v1/endpoints.py +++ b/src/backend/base/langflow/api/v1/endpoints.py @@ -49,13 +49,12 @@ from langflow.services.database.models.flow.model import Flow, FlowRead from langflow.services.database.models.flow.utils import get_all_webhook_components_in_flow from langflow.services.database.models.user.model import User, UserRead from langflow.services.deps import get_session_service, get_settings_service, get_telemetry_service -from langflow.services.settings.feature_flags import FEATURE_FLAGS from langflow.services.telemetry.schema import RunPayload from langflow.utils.compression import compress_response from langflow.utils.version import get_version_info if TYPE_CHECKING: - from langflow.services.event_manager import EventManager + from langflow.events.event_manager import EventManager from langflow.services.settings.service import SettingsService router = APIRouter(tags=["Base"]) @@ -681,22 +680,13 @@ async def custom_component_update( code_request: UpdateCustomComponentRequest, user: CurrentActiveUser, ): - """Update a custom component with the provided code request. + """Update an existing custom component with new code and configuration. - This endpoint generates the CustomComponentFrontendNode normally but then runs the `update_build_config` method - on the latest version of the template. - This ensures that every time it runs, it has the latest version of the template. - - Args: - code_request (CustomComponentRequest): The code request containing the updated code for the custom component. - user (User, optional): The user making the request. Defaults to the current active user. - - Returns: - dict: The updated custom component node. + Processes the provided code and template updates, applies parameter changes (including those loaded from the database), updates the component's build configuration, and validates outputs. Returns the updated component node as a JSON-serializable dictionary. Raises: - HTTPException: If there's an error building or updating the component - SerializationError: If there's an error serializing the component to JSON + HTTPException: If an error occurs during component building or updating. + SerializationError: If serialization of the updated component node fails. """ try: component = Component(_code=code_request.code) @@ -752,14 +742,19 @@ async def custom_component_update( raise SerializationError.from_exception(exc, data=component_node) from exc -@router.get("/config", response_model=ConfigResponse) -async def get_config(): +@router.get("/config") +async def get_config() -> ConfigResponse: + """Retrieve the current application configuration settings. + + Returns: + ConfigResponse: The configuration settings of the application. + + Raises: + HTTPException: If an error occurs while retrieving the configuration. + """ try: settings_service: SettingsService = get_settings_service() + return ConfigResponse.from_settings(settings_service.settings) - return { - "feature_flags": FEATURE_FLAGS, - **settings_service.settings.model_dump(), - } except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc diff --git a/src/backend/base/langflow/api/v1/schemas.py b/src/backend/base/langflow/api/v1/schemas.py index c25345759..4229dfd69 100644 --- a/src/backend/base/langflow/api/v1/schemas.py +++ b/src/backend/base/langflow/api/v1/schemas.py @@ -17,13 +17,13 @@ from langflow.graph.schema import RunOutputs from langflow.schema.dotdict import dotdict from langflow.schema.graph import Tweaks from langflow.schema.schema import InputType, OutputType, OutputValue -from langflow.serialization import constants as serialization_constants from langflow.serialization.serialization import get_max_items_length, get_max_text_length, serialize from langflow.services.database.models.api_key.model import ApiKeyRead from langflow.services.database.models.base import orjson_dumps from langflow.services.database.models.flow.model import FlowCreate, FlowRead from langflow.services.database.models.user.model import UserRead -from langflow.services.settings.feature_flags import FeatureFlags +from langflow.services.settings.base import Settings +from langflow.services.settings.feature_flags import FEATURE_FLAGS, FeatureFlags from langflow.services.tracing.schema import Log @@ -395,8 +395,8 @@ class FlowDataRequest(BaseModel): class ConfigResponse(BaseModel): feature_flags: FeatureFlags - serialization_max_items_length: int = serialization_constants.MAX_ITEMS_LENGTH - serialization_max_text_length: int = serialization_constants.MAX_TEXT_LENGTH + serialization_max_items_length: int + serialization_max_text_length: int frontend_timeout: int auto_saving: bool auto_saving_interval: int @@ -407,6 +407,31 @@ class ConfigResponse(BaseModel): public_flow_expiration: int event_delivery: Literal["polling", "streaming", "direct"] + @classmethod + def from_settings(cls, settings: Settings) -> "ConfigResponse": + """Create a ConfigResponse instance using values from a Settings object and global feature flags. + + Parameters: + settings (Settings): The Settings object containing configuration values. + + Returns: + ConfigResponse: An instance populated with configuration and feature flag values. + """ + return cls( + feature_flags=FEATURE_FLAGS, + serialization_max_items_length=settings.max_items_length, + serialization_max_text_length=settings.max_text_length, + frontend_timeout=settings.frontend_timeout, + auto_saving=settings.auto_saving, + auto_saving_interval=settings.auto_saving_interval, + health_check_max_retries=settings.health_check_max_retries, + max_file_size_upload=settings.max_file_size_upload, + webhook_polling_interval=settings.webhook_polling_interval, + public_flow_cleanup_interval=settings.public_flow_cleanup_interval, + public_flow_expiration=settings.public_flow_expiration, + event_delivery=settings.event_delivery, + ) + class CancelFlowResponse(BaseModel): """Response model for flow build cancellation."""