* refactor: turn sorting methods into functions in a separate module
- Added `layered_topological_sort` function to perform layered topological sorting of graph vertices, accommodating cycles and input vertex checks.
- Introduced `refine_layers` function to ensure proper dependency ordering among vertices.
- Implemented helper functions for sorting layers by dependency and filtering vertices based on predecessors.
- Enhanced utility functions to support better graph traversal and layer management.
This update improves the graph processing capabilities, allowing for more efficient handling of complex graph structures.
* feat(tests): enhance graph utility tests with cycle detection and sorting functionality
- Added a new fixture `graph_with_loop` to simulate a graph containing cycles for testing purposes.
- Improved the `test_large_graph_efficiency` to validate cycle detection in large graphs.
- Introduced multiple tests for sorting vertices in graphs with cycles, ensuring correct order and handling of input vertices.
- Enhanced assertions to provide clearer error messages for failed tests, improving debugging experience.
These changes strengthen the testing framework for graph utilities, ensuring robust handling of complex graph structures.
* refactor(graph): remove unused parent_node_map from Graph class initialization
- Eliminated the `parent_node_map` parameter from the Graph class constructor, streamlining the graph initialization process.
- This change enhances code clarity and reduces unnecessary complexity in graph management.
This update contributes to cleaner and more maintainable graph-related code.
* refactor(graph): optimize dependency sorting and vertex filtering
- Improved the `_max_dependency_index` function by utilizing `index_map.get()` for cleaner code and better handling of missing successors.
- Enhanced the `_sort_single_layer_by_dependency` function with a caching mechanism to avoid redundant calculations, improving performance during vertex sorting.
- Updated `filter_vertices_up_to_vertex` to use a set for `vertices_ids`, optimizing membership checks and enhancing efficiency in vertex filtering.
These changes contribute to more efficient graph processing and improved code readability.
* chore: remove unused 'parent_node_map' parameter
* [autofix.ci] apply automated fixes
* fix: replace old method call with a new func
* test: enhance assertions for file existence in webhook tests
* refactor(graph): enhance component ID retrieval and chat input sorting
- Updated `find_start_component_id` to accept an optional `is_webhook` parameter, allowing for dynamic priority input selection based on the flow type.
- Improved `sort_chat_inputs_first` to handle chat input positioning more efficiently, ensuring only one chat input exists and adjusting its position within the layers as needed.
- These changes enhance the flexibility and efficiency of graph processing, particularly for webhook flows.
* test(graph): update assertions in sort_chat_inputs_first test for accuracy
- Modified assertions in the `test_chat_inputs_at_start` function to reflect the correct expected output of the `sort_chat_inputs_first` utility.
- Adjusted the expected length and order of the result to ensure accurate validation of chat input sorting functionality.
These changes enhance the reliability of the test suite for graph utilities, ensuring that the sorting logic is correctly validated.
* test(chat): update assertion in consume_and_assert_stream for accurate ID validation
- Modified the assertion in the `consume_and_assert_stream` function to include an additional expected ID, ensuring the test accurately reflects the current output of the chat endpoint.
- This change enhances the reliability of the test suite by validating the correct behavior of the chat input sorting functionality.
* test(endpoints): update assertion in test_get_vertices for accurate ID validation
- Modified the assertion in the `test_get_vertices` function to include an additional expected ID, "Webhook", alongside "ChatInput".
- This change ensures the test accurately reflects the current output of the endpoint, enhancing the reliability of the test suite for endpoint functionality.
---------
Co-authored-by: italojohnny <italojohnnydosanjos@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
* feat: add benchmark markers to multiple test cases
* test: add performance tests for database initialization and app startup
Introduce benchmark tests to measure the performance of database initialization and application startup. This helps ensure efficiency and identify potential bottlenecks in the setup process.
* feat: update CodSpeed workflow to enhance test execution and duration tracking
* refactor: streamline performance tests for database initialization and app startup
* test: enhance app startup performance test with pytest benchmarking
* test: update benchmark for database initialization
Remove unnecessary benchmark call and simplify the database initialization test to enhance clarity and reliability in performance testing.
* chore: remove unnecessary pytest options from CodSpeed workflow
* Add environment setup for test_app_startup benchmark test
* Add benchmark test for app startup with database setup and flow loading
* Add benchmark markers to flow building tests in test_chat_endpoint.py
* perf: add benchmarks for service initialization and caching
Introduce benchmarks for various service initialization functions and LLM caching to improve performance evaluations.
* Remove unused benchmark marker from test function in test_chat_endpoint.py
* perf: initialize services in super user benchmark test
* Add benchmarking to test_create_starter_projects in performance tests
* Add asyncio threading to benchmark tests and remove benchmark fixture usage
* Remove database initialization benchmark test from performance suite
* Set `_has_cycle_edges` to `True` for source and target vertices in cycle edges
* feat: Add `has_cycle_edges` method to Vertex class
The `has_cycle_edges` method is added to the `Vertex` class to check if the vertex has any cycle edges. Additionally, the `instantiate_component` method is updated to use the `initialize.loading.instantiate_class` function for custom component instantiation.
* Add `apply_on_outputs` method to Vertex for applying functions to outputs
* Add utility to find vertices in cycles within a directed graph
- Implement `find_cycle_vertices` function to identify all vertices that are part of cycles in a directed graph.
- Utilize depth-first search (DFS) to detect cycles and collect vertices involved in those cycles.
* Add unit tests for `find_cycle_vertices` utility function in graph module
* Add method to set cache for vertices in cycle
- Introduced `_set_cache_to_vertices_in_cycle` method to enable caching for vertices involved in cycles.
- Added `find_cycle_vertices` import to support the new method.
- Refactored vertex instantiation into `_instantiate_components_in_vertices` method for better code organization.
* refactor: Update caching logic for vertices in cycles
Refactor the `_set_cache_to_vertices_in_cycle` method to improve caching logic for vertices involved in cycles. Instead of setting the `cache` attribute to `True`, it is now set to `False` for better clarity and consistency. This change ensures that the cache is properly handled for vertices in cycles.
* Refactor `find_cycle_vertices` to use NetworkX for cycle detection
* Refactor `find_cycle_vertices` tests to remove entry point parameter and add new test case
- Removed the `entry_point` parameter from all test cases for `find_cycle_vertices`.
- Added a new parameterized test case `test_handle_two_inputs_in_cycle` to verify handling of cycles with two inputs.
* Disable cache in cycle: Update `apply_on_outputs` to handle empty outputs in `base.py`
* Add unit test to ensure output cache is disabled in graph cycles
* Add unit test for graph cyclicity with prompt components and OpenAI integration
- Introduce `test_updated_graph_with_prompts` to validate graph cyclicity and execution.
- Integrate `PromptComponent`, `OpenAIModelComponent`, and `ConditionalRouterComponent` in the test.
- Ensure graph execution with a maximum of 20 iterations and cache disabled.
- Validate the presence of expected output vertices in the results.
* Convert `_instantiate_components_in_vertices` to async and disable cache in cycle vertices
* Add default value handling for cycle edges in vertex component
- Introduced `default_value` to handle cases where edges are cycles and target parameters are present.
- Ensured that `default_value` is returned if defined, preventing errors when the component is not built.
* Switch from os.environ to os.getenv for API key retrieval in test_cycles.py
* Add __repr__ method to Edge class to indicate cycle edges with a symbol
* Refactor test_cycles.py to streamline component initialization and update assertions
- Simplified component initialization using method chaining.
- Corrected router input and message parameters to use openai_component_1.
- Updated assertions to check for correct output IDs.
* Refactor test_cycles.py to streamline component initialization and update assertions
* Refactor test to use custom serialization method instead of pickle
* Add cycle_vertices property to optimize cycle detection in graph
- Introduced `_cycle_vertices` attribute to store vertices involved in cycles.
- Added `cycle_vertices` property to compute and cache cycle vertices.
- Updated edge creation logic to use `cycle_vertices` for cycle detection.
* Enhance error message in `types.py` to include component ID for better debugging
* Refactor test_cycles.py to update graph configuration and assertions
- Changed router operator from "equals" to "contains".
- Consolidated chat output to a single component.
- Updated graph construction to use a single chat output.
- Replaced `_snapshot` with `get_snapshot` for graph state capture.
- Adjusted assertions to reflect the updated graph structure and outputs.
* Add api_key_required marker to test_updated_graph_with_prompts test
* Add validation to require max_iterations for cyclic graphs
* run ruff
- Refactored error message handling in `base.py` for cyclic graphs.
- Optimized cycle vertex extraction in `utils.py` by using set comprehension.
* Comment out tests for loading flow from JSON in test_loading.py
* Refactor test fixture for webhook flow creation in conftest.py
* Update unit tests to reflect new webhook flow structure in vertices endpoints
* Temporarily disable tests for loading Langchain objects with and without cached sessions
* Disable caching in vector store and OpenAI model components
* Add async support and dependencies to pyproject.toml files
- Added `asgi-lifespan>=2.1.0` to dependencies.
- Configured `asyncio_mode` and `asyncio_default_fixture_loop_scope` for pytest.
- Updated `tool.uv` section with `asgi-lifespan` in dev-dependencies.
* Convert test fixtures to async and use AsyncClient for HTTP requests
* Handle 'ImportFrom' nodes in AST validation to support module attribute imports
* Convert test cases to use async HTTP client
- Updated test cases in `test_database.py`, `test_endpoints.py`, `test_user.py`, `test_variable.py`, `test_files.py`, `test_chat_endpoint.py`, `test_misc.py`, `test_messages_endpoints.py`, `test_api_key.py`, `test_webhook.py`, and `test_login.py` to use `httpx.AsyncClient` instead of `fastapi.TestClient`.
- Modified test functions to be asynchronous and use `await` for HTTP requests.
- Adjusted fixtures and helper functions to support asynchronous operations.
- Ensured consistency in endpoint paths and request methods across all test cases.
* Refactor string concatenation to f-string in test_chat_endpoint.py
* [autofix.ci] apply automated fixes
* Refactor import validation to use pattern matching for AST nodes
* Set `startup_timeout` and `shutdown_timeout` to `None` in `LifespanManager` for test files.
* Convert test functions to async in `test_messages_endpoints.py`
* Add `api_key_required` marker to assistant component tests
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>