Files
dify-plugin-sdks/python/tests/__mock_server
Yeuoly 46eb504125 feat[0.3.0]: add plugin runner functionality (#144)
* feat: add integration configuration and plugin runner functionality

- Introduced `IntegrationConfig` class for managing plugin testing configurations, including validation for the `dify_cli_path`.
- Added `PluginInvokeRequest` and `PluginGenericResponse` models to handle plugin invocation requests and responses.
- Implemented `PluginRunner` class to facilitate running plugins locally, including asynchronous message handling and process management.
- Merged access actions into a single `PluginAccessAction` for streamlined access control.

* apply ruff

* feat: enhance plugin runner with new response handling and exception management

- Added `PLUGIN_INVOKE_END` response type to manage the end of plugin invocations.
- Introduced `PluginStopped` exception to handle plugin termination gracefully.
- Updated `PluginRunner` to support stopping the plugin process and handling asynchronous message reading with improved error management.
- Modified message queue to accommodate `None` responses for completed invocations.

* refactor: rename PluginStopped exception to PluginStoppedError

- Renamed `PluginStopped` to `PluginStoppedError` for clarity and consistency.
- Updated references in the `run.py` file to reflect the new exception name.
- Added a docstring to `PluginStoppedError` to describe its purpose.

* fix: update version check for dify CLI in IntegrationConfig

- Modified the version check in the IntegrationConfig class to require a minimum version of 0.1.0 instead of 0.4.0.
- Ensured that the version validation logic is clearer and more robust.

* chore: update dependencies and lock file

- Updated the `content_hash` in `pdm.lock` to reflect changes in dependencies.
- Added `packaging>=25.0` to the dependencies in `pyproject.toml` to ensure compatibility with the latest packaging features.

* feat: enhance IntegrationConfig with improved CLI path validation

- Added a list of potential plugin names to search for the dify CLI executable.
- Updated the validation logic to check for the CLI path against multiple plugin names.
- Changed error handling to use ValidationError for invalid CLI path and version checks.

* feat: add setup script for dify-plugin-cli installation

- Introduced a new script to automate the installation of dify-plugin-cli, including OS and architecture detection.
- Updated the pull request workflow to include the setup of the dify-plugin-cli before installing dependencies.
- Refactored error handling in IntegrationConfig to use ValueError instead of ValidationError for CLI path and version checks.

* feat: add integration test for invoking LLM with mocked OpenAI server

- Introduced a new integration test in `test_invoke_llm.py` to validate the invocation of the LLM plugin using a mocked OpenAI server.
- Implemented a mock server to simulate OpenAI's chat completions API, supporting both streaming and non-streaming responses.
- The test downloads the latest `langgenius-openai` plugin, runs the plugin, and asserts the expected response from the LLM.

* fix: add timeout to requests in LLM integration test

- Updated the `test_invoke_llm.py` file to include a timeout of 10 seconds for both POST and GET requests to the marketplace API.
- This change aims to improve the reliability of the integration test by preventing indefinite blocking on network calls.

* feat: enhance dify-plugin-cli setup script with installation verification and PATH configuration

- Added verification to check if dify-plugin is available in the user's PATH after installation.
- Implemented logic to add dify-plugin to the system PATH for all users when run as root, or to the user's profile for non-root users.
- Created symlinks in common bin directories and provided usage instructions for the dify-plugin commands.

* refactor: update dify-plugin-cli setup script for improved PATH management

- Enhanced the script to add the installation directory to the GITHUB_PATH for GitHub Actions.
- Modified the PATH export for local development to ensure the installation directory is prioritized.
- Removed redundant installation verification and symlink creation logic, streamlining the setup process.

* test: add SSH support to pull request workflow

- Integrated SSH functionality into the pull request workflow using the mxschmitt/action-tmate@v3 action.
- This addition allows for interactive debugging and terminal access during pull request checks.

* chore: remove SSH step from pull request workflow and fix import in integration test

- Eliminated the SSH step from the pull request workflow to streamline the process.
- Fixed the import statement for the `requests` library in `test_invoke_llm.py` to ensure proper functionality during the test execution.

* wtf

* feat: implement OpenAI mock server for integration testing

- Added a new mock server in `python/tests/__mock_server` to simulate OpenAI's chat completions API, supporting both streaming and non-streaming responses.
- Updated the pull request workflow to install the `uv` tool and launch the mock server during testing.
- Refactored `test_invoke_llm.py` to utilize the mock server, ensuring reliable integration tests without external dependencies.

* apply ruff

* refactor: streamline mock server launch in pull request workflow

- Replaced inline mock server launch commands in the pull request workflow with a dedicated script `launch_mock_server.sh` for improved readability and maintainability.
- The new script handles the execution of the mock server, ensuring a cleaner workflow configuration.

* fix: update mock server launch path in pull request workflow

- Changed the path for launching the mock server script from `./scripts/launch_mock_server.sh` to `./python/scripts/launch_mock_server.sh` to ensure correct execution within the workflow.

* fix: run mock server in background during pull request workflow

- Updated the pull request workflow to launch the mock server script in the background by appending `&` to the command. This change allows subsequent steps to execute without waiting for the mock server to terminate.

* chore: clean up dify-plugin-cli setup script by removing outdated installation instructions

- Removed outdated comments regarding the download and installation of dify-plugin-cli, streamlining the script for clarity.

* feat: enhance PluginRunner with graceful shutdown and thread safety

- Introduced a stop flag and a lock to ensure thread-safe access to the stop flag in the PluginRunner class.
- Implemented a _close method to handle graceful termination of the plugin process, including sending a SIGTERM signal and closing pipes.
- Refactored the _read_async method to ensure proper cleanup and handling of messages, maintaining the integrity of the message queue.
2025-05-19 14:56:13 +08:00
..