Files
langflow/scripts/build_component_index.py
Gabriel Luiz Freitas Almeida fd74f194dd feat: add static components index to avoid rebuild on startup (#10181)
* feat: add script to build static component index for fast startup

This script generates a prebuilt index of all built-in components in the lfx.components package, saving it as a JSON file for quick loading at runtime. It includes versioning and integrity verification through SHA256 hashing.

* chore: update package dependencies and versioning

- Bump revision to 3 in uv.lock.
- Update dependency markers for several packages to improve compatibility with Python versions and platforms.
- Increment versions for langflow (1.6.4) and langflow-base (0.6.4).
- Adjust dependency markers for packages related to darwin platform to enhance specificity.

* chore: update .gitignore to include component index cache

- Added entry for user-specific component index cache directory to .gitignore.
- Included member_servers.json in the ignore list for better file management.

* feat: enhance component loading with custom index support

- Introduced functions to detect development mode and read a custom component index from a specified path or URL.
- Added caching mechanism for dynamically generated component indices to improve performance.
- Updated `import_langflow_components` to utilize the new index reading and caching logic, allowing for faster startup in production mode.
- Added `components_index_path` to settings for user-defined index configuration.

* feat: add GitHub Actions workflow to automatically update component index

- Introduced a new workflow that triggers on pull requests and manual dispatch to update the component index.
- The workflow checks for changes in the component index and commits updates if necessary.
- Added a comment feature to notify users when the component index is updated.

* [autofix.ci] apply automated fixes

* fix: enhance development mode detection logic

- Updated the `_dev_mode` function to improve clarity and functionality in detecting development mode.
- Refined environment variable checks to explicitly handle "1"/"true"/"yes" for development and "0"/"false"/"no" for production.
- Maintained the editable install heuristic as a fallback for determining the mode when the environment variable is not set.

* refactor: simplify development mode detection logic

- Revised the `_dev_mode` function to clarify the detection of development mode.
- Removed the editable install heuristic, making the environment variable `LFX_DEV` the sole determinant for development mode.
- Updated documentation to reflect the new behavior and ensure accurate understanding of the mode switching.

* docs: update DEVELOPMENT.md to clarify component development mode

- Added tips for enabling dynamic component loading with `LFX_DEV=1` for faster development.
- Emphasized the importance of using `LFX_DEV=1` for live reloading of components during development.
- Included instructions for manually rebuilding the component index for testing purposes.

* add component index

* test: add unit tests for component index functionality

- Introduced comprehensive unit tests for the component index system, covering functions such as _dev_mode, _read_component_index, _save_generated_index, and import_langflow_components.
- Tests include various scenarios for development mode detection, reading and saving component indices, and handling custom paths and URLs.
- Enhanced test coverage to ensure robustness and reliability of the component index features.

* chore: update GitHub Actions workflow for component index updates

- Modified the workflow to include separate checkout steps for pull requests and manual dispatch events.
- Added an environment variable `LFX_DEV` to the build step for enhanced development mode support.
- Improved clarity in the workflow structure to accommodate different triggering events.

* chore: update component index with new timezones and remove deprecated entries

- Added new timezone options including America/Boise, Australia/North, and Etc/GMT-2.
- Removed outdated timezone entries to streamline the selection process.
- Updated the component index structure to enhance clarity and maintainability.

* docs: clarify output path determination in build_component_index.py

- Updated comment to specify that the output path is relative to the script location and intended for development/CI use, not from the installed package.
- Enhanced clarity for future developers regarding the script's execution context.

* refactor: update component import logic and structure

- Changed the comment to reflect the correct module path for extracting subpackage names.
- Flattened the custom components dictionary if it has a "components" wrapper for consistency.
- Merged built-in and custom components into a single structure, ensuring the output maintains a "components" wrapper.
- Updated the component count calculation to reflect the new merged structure.

* refactor: streamline component merging logic

- Simplified the merging of built-in and custom components by removing the "components" wrapper at the cache level.
- Updated the component count calculation to directly reflect the new structure of the merged components.
- Enhanced code clarity by refining comments related to the merging process.

* chore: enhance GitHub Actions workflow for component index updates

- Updated the workflow to conditionally comment on pull requests from community forks, instructing users to manually update the component index.
- Refined the commit and push logic to ensure it only executes for changes made within the same repository.

* chore: update component index [skip ci]

* chore: refine GitHub Actions workflow for component index checks

- Enhanced the logic to check for changes in the component index file, ensuring it only triggers actions for changes made within the same repository.
- Updated the PR comment step to clarify that it applies only to pull requests from the same repository, improving workflow accuracy.

* chore: update component index [skip ci]

* chore: update component index [skip ci]

* refactor: rename version retrieval function for clarity

- Changed the function name from _get_lfx_version to _get_langflow_version to accurately reflect the version being retrieved.
- Updated the version retrieval logic in the build_component_index function to use the new function name.

* fix: update version check to reflect langflow instead of lfx

- Changed the version check logic in the _read_component_index and _save_generated_index functions to use "langflow" instead of "lfx".
- Ensured that the component index version matches the installed langflow version for better accuracy.

* fix: sort timezone options in CurrentDateComponent

- Updated the timezone options in the CurrentDateComponent to be sorted for better user experience.
- Ensured that the dropdown displays timezones in a consistent and organized manner.

* chore: update component index with new timezone options and MistralAI model configurations

- Replaced outdated timezone options with a more relevant and diverse set for improved user experience.
- Added new configurations for the MistralAI model component, including input fields for API key, model selection, and request parameters.
- Updated version number to reflect recent changes.

* chore: update component index [skip ci]

* chore: update component index [skip ci]

* chore: update component index [skip ci]

* chore: update component index [skip ci]

* chore: minify component index output to reduce file size

* test: Add unit tests for build_component_index script functionality

* chore: enhance push logic in update-component-index workflow to handle concurrent updates with retries

* chore: update component index [skip ci]

* fix: standardize logging messages for MCP server initialization

* chore: update component index [skip ci]

* test: Add unit tests for build_component_index script functionality

* chore: update component index [skip ci]

* fix: improve error handling for missing LangChain dependencies in run.py

* feat: add deterministic normalization for component index serialization

* chore: update component index [skip ci]

* chore: update component index

* chore: update component index

* chore: update component index

* chore: enhance component index update workflow with detailed diff statistics and SHA256 comparison

* chore: update component index

* chore: update component loading message to reflect total components discovered

* chore: sort model options for improved readability in NVIDIA component

* chore: filter out 'localtime' from timezone options for improved user experience

* chore: update component index with sorted lists

* chore: update component index with mistral

* chore: update component index

* fix: improve error message for langflow tests to include detailed instructions for environment setup

* refactor: rename and enhance _dev_mode function to _parse_dev_mode for improved clarity and functionality; support boolean and list modes for module loading

* docs: enhance DEVELOPMENT.md with updated instructions for Component Development Mode, including dynamic loading options for specific components

* fix: enhance error handling in _read_component_index to log specific issues with fetching and parsing component indices, including corrupted JSON and missing SHA256 hash

* chore: update component index

* feat: add metadata to component index including number of modules and components for improved indexing information

* feat: integrate telemetry for component index loading, capturing metrics on modules and components, and enhancing performance tracking

* feat: add build command for dynamic component index generation

* fix: add missing import for json in CustomComponent definition in LoopTest.json

* fix: improve handling of dotted imports in prepare_global_scope function

* chore: update component index

* fix: refine import handling in prepare_global_scope to correctly manage aliased and dotted imports

* chore: update component index

* feat: enhance component index update workflow with detailed change analysis and summary

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-10-13 18:54:17 +00:00

127 lines
4.6 KiB
Python

"""Build a static component index for fast startup.
This script generates a prebuilt index of all built-in components by walking
through the lfx.components package and processing each module. The index is
saved as a JSON file that can be loaded instantly at runtime, avoiding the
need to import all component modules during startup.
"""
import hashlib
import sys
from pathlib import Path
import orjson
def _get_langflow_version():
"""Get the installed langflow version."""
from importlib.metadata import version
return version("langflow")
def _normalize_for_determinism(obj):
"""Recursively normalize data structures for deterministic serialization.
Sorts dictionaries by key and lists (where semantically appropriate) to ensure
the same input always produces the same JSON output.
"""
if isinstance(obj, dict):
# Recursively normalize all dict values and return sorted by keys
return {k: _normalize_for_determinism(v) for k, v in sorted(obj.items())}
if isinstance(obj, list):
# Recursively normalize list items
# Keep list order as-is since some lists are ordered (like field_order)
return [_normalize_for_determinism(item) for item in obj]
# Primitive types, return as-is
return obj
def build_component_index():
"""Build the component index by scanning all modules in lfx.components.
Returns:
A dictionary containing version, entries, and sha256 hash
"""
print("Building component index...")
# Use the existing import_langflow_components function
try:
import asyncio
from lfx.interface.components import import_langflow_components
# Run the async function
components_result = asyncio.run(import_langflow_components())
modules_dict = components_result.get("components", {})
components_count = sum(len(v) for v in modules_dict.values())
print(f"Discovered {components_count} components across {len(modules_dict)} categories")
# Convert modules_dict to entries format and sort for determinism
# Sort by category name (top_level) to ensure consistent ordering
entries = []
for category_name in sorted(modules_dict.keys()):
# Sort components within each category by component name
components_dict = modules_dict[category_name]
sorted_components = {comp_name: components_dict[comp_name] for comp_name in sorted(components_dict.keys())}
entries.append([category_name, sorted_components])
except (ImportError, AttributeError) as e:
print(f"Failed to import components: {e}", file=sys.stderr)
return None
# Build the index structure
index = {
"version": _get_langflow_version(),
"metadata": {
"num_modules": len(modules_dict),
"num_components": components_count,
},
"entries": entries,
}
# Normalize the entire structure for deterministic output
index = _normalize_for_determinism(index)
# Calculate hash for integrity verification
# Note: We calculate hash without the sha256 field itself
# orjson.dumps returns bytes and handles Enums automatically
# OPT_SORT_KEYS ensures consistent key ordering in the final JSON
payload = orjson.dumps(index, option=orjson.OPT_SORT_KEYS)
index["sha256"] = hashlib.sha256(payload).hexdigest()
return index
def main():
"""Main entry point for building the component index."""
# Build the index
index = build_component_index()
if not index:
print("Failed to build component index", file=sys.stderr)
sys.exit(1)
# Determine output path relative to script location (repo structure)
# This script is run during development/CI, not from installed package
output_path = Path(__file__).parent.parent / "src" / "lfx" / "src" / "lfx" / "_assets" / "component_index.json"
# Create directory if it doesn't exist
output_path.parent.mkdir(parents=True, exist_ok=True)
# Always minify to reduce file size and git history bloat (from 83k to ~5k lines)
# The index is auto-generated and can be inspected with `jq` if needed
print(f"\nWriting minified index to {output_path}")
json_bytes = orjson.dumps(index, option=orjson.OPT_SORT_KEYS)
output_path.write_text(json_bytes.decode("utf-8"), encoding="utf-8")
print("\nIndex successfully written!")
print(f" Version: {index['version']}")
print(f" Modules: {index['metadata']['num_modules']}")
print(f" Components: {index['metadata']['num_components']}")
print(f" SHA256: {index['sha256']}")
if __name__ == "__main__":
main()