Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add EventManager to centralize callbacks (langflow-ai#3434)
* refactor: Update MessageBase text attribute based on isinstance check. * feat: Add update_message function to update a message in the database. * refactor(chat): Update imports and remove unnecessary config method in ChatComponent. * refactor: Add stream_message method to ChatComponent. * refactor: Update method call in ChatOutput component. * feat: Add callback function to custom component and update build_results signature. * feat: Add callback parameter to instantiate_class function. * feat(graph): Add callback functions for sync and async operations. * feat: Add callback function support to vertex build process. * feat: Add handling for added message in InterfaceVertex class. * feat: Add callback support to Graph methods. * feat(chat): Add callback function to build_vertices function. * refactor: Simplify update_message function and use session_scope for session management. * fix: Call set_callback method if available on custom component. * refactor(chat): Update chat message chunk handling and ID conversion. * feat: Add null check before setting cache in build_vertex_stream function. * refactor: Fix send_event_wrapper function and add callback parameter to _build_vertex function. * refactor: Simplify conditional statement and import order in ChatOutput. * refactor: move log method to Component class. * refactor: Simplify CallbackFunction definition. * feat: Initialize _current_output attribute in Component class. * feat: store current output name in custom component during processing. * feat: Add current output and component ID to log data. * fix: Add condition to check current output before invoking callback. * refactor: Update callback to log_callback in graph methods. * feat: Add test for callback graph execution with log messages. * update projects * fix(chat.py): fix condition to check if message text is a string before updating message text in the database * refactor(ChatOutput.py): update ChatOutput class to correctly store and assign the message value to ensure consistency and avoid potential bugs * refactor(chat.py): update return type of store_message method to return a single Message object instead of a list of Messages refactor(chat.py): update logic to correctly handle updating and returning a single stored message object instead of a list of messages * update starter projects * refactor(component.py): update type hint for name parameter in log method to be more explicit * feat: Add EventManager class for managing events and event registration * refactor: Update log_callback to event_manager in custom component classes * refactor(component.py): rename _log_callback to _event_manager and update method call to on_log for better clarity and consistency * refactor(chat.py): rename _log_callback method to _event_manager.on_token for clarity and consistency in method naming * refactor: Rename log_callback to event_manager for clarity and consistency * refactor: Update Vertex class to use EventManager instead of log_callback for better clarity and consistency * refactor: update build_flow to use EventManager * refactor: Update EventManager class to use Protocol for event callbacks * if event_type is not passed, it uses the default send_event * Add method to register event functions in EventManager - Introduced `register_event_function` method to allow passing custom event functions. - Updated `noop` method to accept `event_type` parameter. - Adjusted `__getattr__` to return `EventCallback` type. * update test_callback_graph * Add unit tests for EventManager in test_event_manager.py - Added tests for event registration, including default event type, empty string names, and specific event types. - Added tests for custom event functions and unregistered event access. - Added tests for event sending, including JSON formatting, empty data, and large payloads. - Added tests for handling JSON serialization errors and the noop function. * revert chatOutput change * Add validation for event function in EventManager - Introduced `_validate_event_function` method to ensure event functions are callable and have the correct parameters. - Updated `register_event_function` to use the new validation method. * Add tests for EventManager's event function validation logic - Introduce `TestValidateEventFunction` class to test various scenarios for `_validate_event_function`. - Add tests for valid event functions, non-callable event functions, invalid parameter counts, and parameter type validation. - Include tests for handling unannotated parameters, flexible arguments, and keyword-only parameters. - Ensure proper warnings and exceptions are raised for invalid event functions. * Add type ignore comment to lambda function in test_event_manager.py * refactor: Update EventManager class to use Protocol for event callbacks * refactor(event_manager.py): simplify event registration and validation logic to enhance readability and maintainability feat(event_manager.py): enforce event name conventions and improve callback handling for better error management * refactor(chat.py): standardize event_manager method calls by using keyword arguments for better clarity and consistency refactor(chat.py): extract message processing logic into separate methods for improved readability and maintainability fix(chat.py): ensure proper handling of async iterators in message streaming refactor(component.py): simplify event logging by removing unnecessary event name parameter in on_log method call * update event manager tests * Add callback validation and manager parameter in EventManager - Introduced `_validate_callback` method to ensure callbacks are callable and have the correct parameters. - Updated `register_event` to include `manager` parameter in the callback. * Add support for passing callback through the Graph in test_callback_graph * fix(event_manager.py): update EventCallback signature to include manager parameter for better context in event handling
- Loading branch information