Files
dify-plugin-sdks/scripts/setup-dify-plugin-cli.sh
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

96 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
set -e
# Create directory for dify-plugin-cli
DIFY_HOME="${HOME}/.dify"
DIFY_BIN="${DIFY_HOME}/bin"
mkdir -p "${DIFY_BIN}"
# Determine OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
if [[ "${ARCH}" == "x86_64" ]]; then
ARCH="amd64"
elif [[ "${ARCH}" == "arm64" || "${ARCH}" == "aarch64" ]]; then
ARCH="arm64"
else
echo "Unsupported architecture: ${ARCH}"
exit 1
fi
if [[ "${OS}" == "darwin" ]]; then
OS="darwin"
elif [[ "${OS}" == "linux" ]]; then
OS="linux"
elif [[ "${OS}" =~ msys|mingw|cygwin ]]; then
OS="windows"
EXT=".exe"
else
echo "Unsupported OS: ${OS}"
exit 1
fi
# Get the latest release URL
echo "Fetching latest release information..."
LATEST_RELEASE_URL=$(curl -s https://api.github.com/repos/langgenius/dify-plugin-daemon/releases/latest | grep "browser_download_url.*dify-plugin-${OS}-${ARCH}${EXT:-}" | cut -d '"' -f 4)
if [[ -z "${LATEST_RELEASE_URL}" ]]; then
echo "Failed to find download URL for ${OS}-${ARCH}"
exit 1
fi
# Download the binary
echo "Downloading from ${LATEST_RELEASE_URL}..."
curl -L -o "${DIFY_BIN}/dify-plugin${EXT:-}" "${LATEST_RELEASE_URL}"
# Make it executable
chmod +x "${DIFY_BIN}/dify-plugin${EXT:-}"
# Create symlink to dify
ln -sf "${DIFY_BIN}/dify-plugin${EXT:-}" "${DIFY_BIN}/dify${EXT:-}"
# Add to PATH for GitHub Actions
if [[ -n "${GITHUB_PATH}" ]]; then
echo "${DIFY_BIN}" >> "$GITHUB_PATH"
echo "Added ${DIFY_BIN} to GITHUB_PATH"
fi
# For local development, add to current session PATH
export PATH="${DIFY_BIN}:${PATH}"
# Add to PATH if not already there and if not in CI environment
if [[ ":$PATH:" != *":${DIFY_BIN}:"* ]] && [[ -z "${CI}" ]]; then
echo "Adding ${DIFY_BIN} to PATH in your profile..."
# Determine shell profile file
SHELL_PROFILE=""
if [[ -n "$BASH_VERSION" ]]; then
if [[ -f "$HOME/.bashrc" ]]; then
SHELL_PROFILE="$HOME/.bashrc"
elif [[ -f "$HOME/.bash_profile" ]]; then
SHELL_PROFILE="$HOME/.bash_profile"
fi
elif [[ -n "$ZSH_VERSION" ]]; then
SHELL_PROFILE="$HOME/.zshrc"
fi
if [[ -n "$SHELL_PROFILE" ]]; then
echo "export PATH=\"${DIFY_BIN}:\$PATH\"" >> "$SHELL_PROFILE"
echo "Added ${DIFY_BIN} to PATH in ${SHELL_PROFILE}"
echo "Please run 'source ${SHELL_PROFILE}' or start a new terminal session to use dify-plugin-cli"
else
echo "Could not determine shell profile. Please add ${DIFY_BIN} to your PATH manually"
fi
fi
echo "dify-plugin-cli has been installed to ${DIFY_BIN}/dify-plugin${EXT:-}"
echo "Version information:"
"${DIFY_BIN}/dify-plugin${EXT:-}" version
# Create a GitHub Actions environment file to make the binary available in subsequent steps
if [[ -n "${GITHUB_ENV}" ]]; then
echo "PATH=${DIFY_BIN}:${PATH}" >> "$GITHUB_ENV"
echo "Updated PATH in GITHUB_ENV for subsequent steps"
fi