RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Remove python source code (#20284)
Jin Hai562e02184a61
diff --git a/.github/workflows/sep-tests.yml b/.github/workflows/sep-tests.ymlindex 0d55407744..156e93f862 100644--- a/.github/workflows/sep-tests.yml+++ b/.github/workflows/sep-tests.yml@@ -51,7 +51,6 @@ jobs:outputs:http_api_test_level: ${{ steps.test_level.outputs.http_api_test_level }}has_go_changes: ${{ steps.detect_changes.outputs.has_go_changes }}- has_python_changes: ${{ steps.detect_changes.outputs.has_python_changes }}has_web_changes: ${{ steps.detect_changes.outputs.has_web_changes }}has_native_deepdoc_changes: ${{ steps.detect_changes.outputs.has_native_deepdoc_changes }}api_proxy_schemes: ${{ steps.detect_changes.outputs.api_proxy_schemes }}@@ -111,38 +110,6 @@ jobs:echo "ARTIFACTS_DIR=${ARTIFACTS_DIR}" >> ${GITHUB_ENV}rm -rf ${ARTIFACTS_DIR} && mkdir -p ${ARTIFACTS_DIR}-# - name: Check comments of changed Python files-# if: ${{ false }}-# run: |-# if [[ ${{ github.event_name }} == 'pull_request' || ${{ github.event_name }} == 'pull_request_target' ]]; then-# CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }} \-# | grep -E '\.(py)$' || true)-#-# if [ -n "$CHANGED_FILES" ]; then-# echo "Check comments of changed Python files with check_comment_ascii.py"-#-# readarray -t files <<< "$CHANGED_FILES"-# HAS_ERROR=0-#-# for file in "${files[@]}"; do-# if [ -f "$file" ]; then-# if python3 check_comment_ascii.py "$file"; then-# echo "✅ $file"-# else-# echo "❌ $file"-# HAS_ERROR=1-# fi-# fi-# done-#-# if [ $HAS_ERROR -ne 0 ]; then-# exit 1-# fi-# else-# echo "No Python files changed"-# fi-# fi-# lefthook's web-checks hook resolves oxlint/oxfmt through# tools/hooks/web_tools.sh, which needs node and npm on PATH.- name: Set up Node.js@@ -197,13 +164,11 @@ jobs:run: |set -euo pipefailhas_go=false- has_python=falsehas_web=falsehas_native_deepdoc=falseif [[ "${GITHUB_EVENT_NAME}" == "schedule" ]]; thenhas_go=true- has_python=truehas_web=truehas_native_deepdoc=trueelse@@ -224,15 +189,10 @@ jobs:# build.sh also controls the C++/native build, so it must trigger native backend.Dockerfile|Dockerfile_ci|docker/**|.github/workflows/**|sdk/**|build.sh)has_go=true- has_python=truehas_native_deepdoc=true;;- test/unit_test/**)- has_python=true- ;;test/**)has_go=true- has_python=true;;# DeepDoc native backend (Go inference layer, C++ static lib, pinned testdata).# Must come BEFORE the generic "*.go"/"*.py" rules, otherwise deepdoc/*.go@@ -242,132 +202,24 @@ jobs:has_native_deepdoc=true;;*.go|go.mod|go.sum) has_go=true ;;- *.py|pyproject.toml|requirements*.txt) has_python=true ;;web/*) has_web=true ;;esacdone <<< "$CHANGED_FILES"- if [[ "$has_go" == "false" && "$has_python" == "false" && "$has_web" == "false" ]]; then+ if [[ "$has_go" == "false" && "$has_web" == "false" ]]; thenhas_go=true- has_python=truehas_web=truehas_native_deepdoc=truefifi- if [[ "$has_go" == "true" && "$has_python" == "true" ]]; then- api_proxy_schemes='["go","python"]'- elif [[ "$has_go" == "true" ]]; then- api_proxy_schemes='["go"]'- elif [[ "$has_python" == "true" ]]; then- api_proxy_schemes='["python"]'- elif [[ "$has_web" == "true" ]]; then- api_proxy_schemes='["web"]'- else- api_proxy_schemes='["go","python"]'- fi-+ api_proxy_schemes='["go"]'+echo "has_go_changes=${has_go}" >> $GITHUB_OUTPUT- echo "has_python_changes=${has_python}" >> $GITHUB_OUTPUTecho "has_web_changes=${has_web}" >> $GITHUB_OUTPUTecho "has_native_deepdoc_changes=${has_native_deepdoc}" >> $GITHUB_OUTPUTecho "api_proxy_schemes=${api_proxy_schemes}" >> $GITHUB_OUTPUT- echo "Go: ${has_go}, Python: ${has_python}, Web: ${has_web}, Native DeepDoc: ${has_native_deepdoc}, proxy schemes: ${api_proxy_schemes}"-- - name: Prepare Python test environment- if: steps.detect_changes.outputs.has_python_changes == 'true'- run: |- git config --global url."https://oauth2:${GITEE_TOKEN}@gitee.com/".insteadOf "https://gitee.com/"- uv sync --python 3.13 --group test --frozen- uv pip install -e sdk/python-- - name: Provision NLTK data for unit tests- if: steps.detect_changes.outputs.has_python_changes == 'true'- run: |- set -euo pipefail-- # The self-hosted runner stores NLTK packages as root-owned zip files.- # Reading them directly as the runner user fails with EACCES, while- # downloading replacements is blocked by the runner's SSRF policy.- # Copy the three required archives into the workspace with sudo, then- # unpack and validate them as the runner user.- NLTK_TARGET="${GITHUB_WORKSPACE}/nltk_data"- rm -rf "${NLTK_TARGET}"- mkdir -p "${NLTK_TARGET}/tokenizers" "${NLTK_TARGET}/corpora"-- copy_nltk_archive() {- local relative_path="$1"- local source=""- for root in /usr/share/nltk_data /usr/local/share/nltk_data; do- if sudo test -f "${root}/${relative_path}"; then- source="${root}/${relative_path}"- break- fi- done- if [ -z "${source}" ]; then- echo "Missing required NLTK archive: ${relative_path}" >&2- return 1- fi- echo "Copying ${source}"- sudo cp "${source}" "${NLTK_TARGET}/${relative_path}"- sudo chown "$(id -u):$(id -g)" "${NLTK_TARGET}/${relative_path}"- chmod 0644 "${NLTK_TARGET}/${relative_path}"- }-- copy_nltk_archive tokenizers/punkt_tab.zip- copy_nltk_archive tokenizers/punkt.zip- copy_nltk_archive corpora/wordnet.zip-- NLTK_DATA="${NLTK_TARGET}" uv run python - <<'PYEOF'- import os- import zipfile-- root = os.environ["NLTK_DATA"]- archives = (- ("tokenizers", "punkt_tab"),- ("tokenizers", "punkt"),- ("corpora", "wordnet"),- )- for category, package in archives:- archive = os.path.join(root, category, f"{package}.zip")- destination = os.path.join(root, category)- with zipfile.ZipFile(archive) as package_zip:- bad_member = package_zip.testzip()- if bad_member is not None:- raise RuntimeError(f"Corrupt NLTK archive {archive}: {bad_member}")- members = [name for name in package_zip.namelist() if not name.startswith("__MACOSX/")]- prefix = f"{package}/"- if not members or not all(name == package or name.startswith(prefix) for name in members):- raise RuntimeError(f"Unexpected layout in {archive}: expected entries below {prefix}")- package_zip.extractall(destination)- print(f"Unpacked {archive} -> {os.path.join(destination, package)}")- PYEOF-- echo "NLTK_DATA=${NLTK_TARGET}" >> "${GITHUB_ENV}"-- # Validate real tokenizer/lemmatizer calls, not only directory names.- # The command exits non-zero if any resource is absent or malformed.- NLTK_DATA="${NLTK_TARGET}" uv run python - <<'PYEOF'- import nltk- from nltk.corpus import wordnet- from nltk.tokenize import word_tokenize-- for resource in ("tokenizers/punkt_tab", "tokenizers/punkt", "corpora/wordnet"):- print(f"[OK] {resource} -> {nltk.data.find(resource)}")- assert word_tokenize("NLTK data is ready.")- assert wordnet.synsets("document")- print("NLTK tokenizer and WordNet smoke checks passed")- PYEOF-- - name: Run unit test- if: steps.detect_changes.outputs.has_python_changes == 'true'- run: |- source .venv/bin/activate- which pytest || echo "pytest not in PATH"- echo "Start to run unit test"- python3 run_tests.py -i--+ echo "Go: ${has_go}, Web: ${has_web}, Native DeepDoc: ${has_native_deepdoc}, proxy schemes: ${api_proxy_schemes}"ragflow_tests_infinity:name: ragflow_tests_infinity (${{ matrix.api_proxy_scheme }})@@ -384,7 +236,6 @@ jobs:RAGFLOW_IMAGE: infiniflow/ragflow:${{ github.run_id }}-infinity-${{ matrix.api_proxy_scheme }}HTTP_API_TEST_LEVEL: ${{ needs.ragflow_preflight.outputs.http_api_test_level }}HAS_GO: ${{ needs.ragflow_preflight.outputs.has_go_changes }}- HAS_PYTHON: ${{ needs.ragflow_preflight.outputs.has_python_changes }}HAS_WEB: ${{ needs.ragflow_preflight.outputs.has_web_changes }}steps:- name: Ensure workspace ownership@@ -527,21 +378,6 @@ jobs:./build.sh --test -- $PKGSfi- - name: Run ragflow_deps regression guards- if: env.API_PROXY_SCHEME == 'go'- # Pure-Python, local, needs no API keys or model downloads; guards (a) the- # ONNX Runtime extraction logic the in-process (Go) DeepDoc backend build- # depends on (notably the version-bump path C2 hardened) and (b) the- # tokenizer-asset chain: the downloader's asset list, the three runtime- # Dockerfiles' copy loops and the loaders' SHA-1 pins have to agree, or the- # Go counters silently degrade to the calibrated cl100k estimate. Run them- # in isolation so the test/testcases conftest (which requires- # SILICONFLOW_API_KEY) cannot fail the collection.- run: |- set -euo pipefail- uv sync --python 3.13 --group test --frozen- uv run pytest ragflow_deps/test_download_go_deps.py ragflow_deps/test_tokenizer_assets.py -q --durations=100-- name: Build ragflow:nightlyrun: |set -euo pipefail@@ -870,27 +706,6 @@ jobs:sudo docker ps -a --filter "label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}" -q | xargs -r sudo docker rm -fsudo docker compose -f "${COMPOSE_FILE}" -p ${COMPOSE_PROJECT_NAME} up -d- - name: Run sdk tests against Infinity- if: env.API_PROXY_SCHEME == 'python'- run: |- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi- echo "Start to run test sdk on Infinity"- source .venv/bin/activate && set -o pipefail; DOC_ENGINE=infinity pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --junitxml=pytest-infinity-sdk.xml --cov=sdk/python/ragflow_sdk --cov-branch --cov-report=xml:coverage-infinity-sdk.xml --durations=100 test/testcases/test_sdk_api 2>&1 | tee infinity_sdk_test.log-- name: Run New RESTFUL api tests against Infinityif: env.API_PROXY_SCHEME != 'web'run: |@@ -911,151 +726,6 @@ jobs:fisource .venv/bin/activate && set -o pipefail; DOC_ENGINE=infinity pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --durations=100 test/testcases/restful_api 2>&1 | tee infinity_restful_api_test.log- - name: RAGFlow CLI retrieval test Infinity- if: env.API_PROXY_SCHEME == 'python'- env:- PYTHONPATH: ${{ github.workspace }}- run: |- set -euo pipefail- source .venv/bin/activate-- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""-- EMAIL="ci-${GITHUB_RUN_ID}@example.com"- PASS="ci-pass-${GITHUB_RUN_ID}"- DATASET="ci_dataset_${GITHUB_RUN_ID}"-- CLI="python admin/client/ragflow_cli.py"-- LOG_FILE="infinity_cli_test.log"- : > "${LOG_FILE}"-- ERROR_RE='Traceback|ModuleNotFoundError|ImportError|Parse error|Bad response|Fail to|code:\\s*[1-9]'- run_cli() {- local logfile="$1"- shift- local allow_re=""- if [[ "${1:-}" == "--allow" ]]; then- allow_re="$2"- shift 2- fi- local cmd_display="$*"- echo "===== $(date -u +\"%Y-%m-%dT%H:%M:%SZ\") CMD: ${cmd_display} =====" | tee -a "${logfile}"- local tmp_log- tmp_log="$(mktemp)"- set +e- timeout 500s "$@" 2>&1 | tee "${tmp_log}"- local status=${PIPESTATUS[0]}- set -e- cat "${tmp_log}" >> "${logfile}"- if grep -qiE "${ERROR_RE}" "${tmp_log}"; then- if [[ -n "${allow_re}" ]] && grep -qiE "${allow_re}" "${tmp_log}"; then- echo "Allowed CLI error markers in ${logfile}"- rm -f "${tmp_log}"- return 0- fi- echo "Detected CLI error markers in ${logfile}"- rm -f "${tmp_log}"- exit 1- fi- rm -f "${tmp_log}"- return ${status}- }-- set -a- source docker/.env- set +a-- USER_HOST="$(echo "${HOST_ADDRESS}" | sed -E 's#^https?://([^:/]+).*#\1#')"- USER_PORT="${RAGFLOW_HTTP_PORT}"- ADMIN_HOST="${USER_HOST}"- ADMIN_PORT="${ADMIN_SVR_HTTP_PORT}"-- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi-- admin_ready=0- for i in $(seq 1 30); do- if run_cli "${LOG_FILE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "ping"; then- admin_ready=1- break- fi- sleep 1- done- if [[ "${admin_ready}" -ne 1 ]]; then- echo "Admin service did not become ready"- exit 1- fi-- run_cli "${LOG_FILE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "show version"- ALLOW_USER_EXISTS_RE='already exists|already exist|duplicate|already.*registered|exist(s)?'- run_cli "${LOG_FILE}" --allow "${ALLOW_USER_EXISTS_RE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "create user '$EMAIL' '$PASS'"-- user_ready=0- for i in $(seq 1 30); do- if run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "ping"; then- user_ready=1- break- fi- sleep 1- done- if [[ "${user_ready}" -ne 1 ]]; then- echo "User service did not become ready"- exit 1- fi-- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "show version"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "create dataset '$DATASET' with embedding 'BAAI/bge-small-en-v1.5@Builtin' parser 'auto'"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "import 'test/benchmark/test_docs/Doc1.pdf,test/benchmark/test_docs/Doc2.pdf' into dataset '$DATASET'"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "parse dataset '$DATASET' sync"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "Benchmark 16 100 search 'what are these documents about' on datasets '$DATASET'"-- - name: Stop ragflow to save coverage Infinity- if: ${{ !cancelled() && env.API_PROXY_SCHEME == 'python' }}- run: |- # Send SIGINT to ragflow_server.py to trigger coverage save- PID=$(sudo docker exec ${RAGFLOW_CONTAINER} ps aux | grep "ragflow_server.py" | grep -v grep | awk '{print $2}' | head -n 1)- if [ -n "$PID" ]; then- echo "Sending SIGINT to ragflow_server.py (PID: $PID)..."- sudo docker exec ${RAGFLOW_CONTAINER} kill -INT $PID- # Wait for process to exit and coverage file to be written- sleep 10- else- echo "ragflow_server.py not found!"- fi- sudo docker compose -f docker/docker-compose.yml -p ${COMPOSE_PROJECT_NAME} stop-- - name: Generate server coverage report Infinity- if: ${{ !cancelled() && env.API_PROXY_SCHEME == 'python' }}- run: |- # .coverage file should be in docker/ragflow-logs/.coverage- if [ -f docker/ragflow-logs/.coverage ]; then- echo "Found .coverage file"- cp docker/ragflow-logs/.coverage .coverage- source .venv/bin/activate- # Create .coveragerc to map container paths to host paths- echo "[paths]" > .coveragerc- echo "source =" >> .coveragerc- echo " ." >> .coveragerc- echo " /ragflow" >> .coveragerc- coverage xml -o coverage-infinity-server.xml- rm .coveragerc- else- echo ".coverage file not found!"- fi-- name: Upload coverage reports to Codecovuses: codecov/codecov-action@v5if: ${{ !cancelled() }}@@ -1128,7 +798,6 @@ jobs:RAGFLOW_IMAGE: infiniflow/ragflow:${{ github.run_id }}-elasticsearch-${{ matrix.api_proxy_scheme }}HTTP_API_TEST_LEVEL: ${{ needs.ragflow_preflight.outputs.http_api_test_level }}HAS_GO: ${{ needs.ragflow_preflight.outputs.has_go_changes }}- HAS_PYTHON: ${{ needs.ragflow_preflight.outputs.has_python_changes }}HAS_WEB: ${{ needs.ragflow_preflight.outputs.has_web_changes }}steps:- name: Ensure workspace ownership@@ -1614,27 +1283,6 @@ jobs:sudo docker ps -a --filter "label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}" -q | xargs -r sudo docker rm -fsudo docker compose -f "${COMPOSE_FILE}" -p ${COMPOSE_PROJECT_NAME} up -d- - name: Run sdk tests against Elasticsearch- if: env.API_PROXY_SCHEME == 'python'- run: |- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi- echo "Start to run test sdk on Elasticsearch"- source .venv/bin/activate && set -o pipefail; pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --junitxml=pytest-es-sdk.xml --cov=sdk/python/ragflow_sdk --cov-branch --cov-report=xml:coverage-es-sdk.xml --durations=100 test/testcases/test_sdk_api 2>&1 | tee es_sdk_test.log-- name: Run New RESTFUL api tests against Elasticsearchif: env.API_PROXY_SCHEME != 'web'run: |@@ -1655,153 +1303,6 @@ jobs:fisource .venv/bin/activate && set -o pipefail; pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --durations=100 test/testcases/restful_api 2>&1 | tee es_restful_api_test.log- - name: RAGFlow CLI retrieval test Elasticsearch- if: env.API_PROXY_SCHEME == 'python'- env:- PYTHONPATH: ${{ github.workspace }}- run: |- set -euo pipefail- source .venv/bin/activate-- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""-- EMAIL="ci-${GITHUB_RUN_ID}@example.com"- PASS="ci-pass-${GITHUB_RUN_ID}"- DATASET="ci_dataset_${GITHUB_RUN_ID}"-- CLI="python admin/client/ragflow_cli.py"-- LOG_FILE="es_cli_test.log"- : > "${LOG_FILE}"-- ERROR_RE='Traceback|ModuleNotFoundError|ImportError|Parse error|Bad response|Fail to|code:\\s*[1-9]'- run_cli() {- local logfile="$1"- shift- local allow_re=""- if [[ "${1:-}" == "--allow" ]]; then- allow_re="$2"- shift 2- fi- local cmd_display="$*"- echo "===== $(date -u +\"%Y-%m-%dT%H:%M:%SZ\") CMD: ${cmd_display} =====" | tee -a "${logfile}"- local tmp_log- tmp_log="$(mktemp)"- set +e- timeout 500s "$@" 2>&1 | tee "${tmp_log}"- local status=${PIPESTATUS[0]}- set -e- cat "${tmp_log}" >> "${logfile}"- if grep -qiE "${ERROR_RE}" "${tmp_log}"; then- if [[ -n "${allow_re}" ]] && grep -qiE "${allow_re}" "${tmp_log}"; then- echo "Allowed CLI error markers in ${logfile}"- rm -f "${tmp_log}"- return 0- fi- echo "Detected CLI error markers in ${logfile}"- rm -f "${tmp_log}"- exit 1- fi- rm -f "${tmp_log}"- return ${status}- }-- set -a- source docker/.env- set +a-- USER_HOST="$(echo "${HOST_ADDRESS}" | sed -E 's#^https?://([^:/]+).*#\1#')"- USER_PORT="${RAGFLOW_HTTP_PORT}"- ADMIN_HOST="${USER_HOST}"- ADMIN_PORT="${ADMIN_SVR_HTTP_PORT}"-- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi-- admin_ready=0- for i in $(seq 1 30); do- if run_cli "${LOG_FILE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "ping"; then- admin_ready=1- break- fi- sleep 1- done- if [[ "${admin_ready}" -ne 1 ]]; then- echo "Admin service did not become ready"- exit 1- fi-- run_cli "${LOG_FILE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "show version"- ALLOW_USER_EXISTS_RE='already exists|already exist|duplicate|already.*registered|exist(s)?'- run_cli "${LOG_FILE}" --allow "${ALLOW_USER_EXISTS_RE}" $CLI --type admin --host "$ADMIN_HOST" --port "$ADMIN_PORT" --username "[email protected]" --password "admin" command "create user '$EMAIL' '$PASS'"-- user_ready=0- for i in $(seq 1 30); do- if run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "ping"; then- user_ready=1- break- fi- sleep 1- done- if [[ "${user_ready}" -ne 1 ]]; then- echo "User service did not become ready"- exit 1- fi-- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "show version"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "create dataset '$DATASET' with embedding 'BAAI/bge-small-en-v1.5@Builtin' parser 'auto'"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "import 'test/benchmark/test_docs/Doc1.pdf,test/benchmark/test_docs/Doc2.pdf' into dataset '$DATASET'"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "parse dataset '$DATASET' sync"- run_cli "${LOG_FILE}" $CLI --type user --host "$USER_HOST" --port "$USER_PORT" --username "$EMAIL" --password "$PASS" command "Benchmark 16 100 search 'what are these documents about' on datasets '$DATASET'"-- - name: Stop ragflow to save coverage Elasticsearch- if: ${{ !cancelled() && env.API_PROXY_SCHEME == 'python' }}- run: |- # Send SIGINT to ragflow_server.py to trigger coverage save- PID=$(sudo docker exec ${RAGFLOW_CONTAINER} ps aux | grep "ragflow_server.py" | grep -v grep | awk '{print $2}' | head -n 1)- if [ -n "$PID" ]; then- echo "Sending SIGINT to ragflow_server.py (PID: $PID)..."- sudo docker exec ${RAGFLOW_CONTAINER} kill -INT $PID- # Wait for process to exit and coverage file to be written- sleep 10- else- echo "ragflow_server.py not found!"- fi- sudo docker compose -f docker/docker-compose.yml -p ${COMPOSE_PROJECT_NAME} stop-- - name: Generate server coverage report Elasticsearch- if: ${{ !cancelled() && env.API_PROXY_SCHEME == 'python' }}- run: |- # .coverage file should be in docker/ragflow-logs/.coverage- if [ -f docker/ragflow-logs/.coverage ]; then- echo "Found .coverage file"- cp docker/ragflow-logs/.coverage .coverage- source .venv/bin/activate- # Create .coveragerc to map container paths to host paths- echo "[paths]" > .coveragerc- echo "source =" >> .coveragerc- echo " ." >> .coveragerc- echo " /ragflow" >> .coveragerc- coverage xml -o coverage-es-server.xml- rm .coveragerc- # Clean up for next run- sudo rm docker/ragflow-logs/.coverage- else- echo ".coverage file not found!"- fi-- name: Collect ragflow log Elasticsearchif: ${{ !cancelled() }}# Keep this step self-sufficient: rebuild the artifacts path instead ofdiff --git a/admin/build_cli_release.sh b/admin/build_cli_release.shdeleted file mode 100755index d9025ff181..0000000000--- a/admin/build_cli_release.sh+++ /dev/null@@ -1,47 +0,0 @@-#!/bin/bash--set -e--echo "🚀 Start building..."-echo "================================"--PROJECT_NAME="ragflow-cli"--RELEASE_DIR="release"-BUILD_DIR="dist"-SOURCE_DIR="src"-PACKAGE_DIR="ragflow_cli"--echo "🧹 Clean old build folder..."-rm -rf release/--echo "📁 Prepare source code..."-mkdir release/$PROJECT_NAME/$SOURCE_DIR -p-cp pyproject.toml release/$PROJECT_NAME/pyproject.toml-cp README.md release/$PROJECT_NAME/README.md--mkdir release/$PROJECT_NAME/$SOURCE_DIR/$PACKAGE_DIR -p-cp ragflow_cli.py release/$PROJECT_NAME/$SOURCE_DIR/$PACKAGE_DIR/ragflow_cli.py--if [ -d "release/$PROJECT_NAME/$SOURCE_DIR" ]; then- echo "✅ source dir: release/$PROJECT_NAME/$SOURCE_DIR"-else- echo "❌ source dir not exist: release/$PROJECT_NAME/$SOURCE_DIR"- exit 1-fi--echo "🔨 Make build file..."-cd release/$PROJECT_NAME-export PYTHONPATH=$(pwd)-python -m build--echo "✅ check build result..."-if [ -d "$BUILD_DIR" ]; then- echo "📦 Package generated:"- ls -la $BUILD_DIR/-else- echo "❌ Build Failed: $BUILD_DIR not exist."- exit 1-fi--echo "🎉 Build finished successfully!"\ No newline at end of filediff --git a/admin/client/COMMAND.md b/admin/client/COMMAND.mddeleted file mode 100644index cd8e376c4d..0000000000--- a/admin/client/COMMAND.md+++ /dev/null@@ -1,779 +0,0 @@-# RAGFlow CLI User Command Reference--This document describes the user commands available in RAGFlow CLI. All commands must end with a semicolon (`;`).--## Command List--### ping_server--**Description**-Tests the connection status to the server.--**Usage**-```-PING;-```--**Parameters**-No parameters.--**Example**-```-ragflow> PING;-```--**Display Effect**-(Sample output will be provided by the user)-------### show_current_user--**Description**-Displays information about the currently logged-in user.--**Usage**-```-SHOW CURRENT USER;-```--**Parameters**-No parameters.--**Example**-```-ragflow> SHOW CURRENT USER;-```--**Display Effect**-(Sample output will be provided by the user)-------### create_model_provider--**Description**-Creates a new model provider.--**Usage**-```-CREATE MODEL PROVIDER <provider_name> <provider_key>;-```--**Parameters**-- `provider_name`: Provider name, quoted string.-- `provider_key`: Provider key, quoted string.--**Example**-```-ragflow> CREATE MODEL PROVIDER 'openai' 'sk-...';-```--**Display Effect**-(Sample output will be provided by the user)-------### drop_model_provider--**Description**-Deletes a model provider.--**Usage**-```-DROP MODEL PROVIDER <provider_name>;-```--**Parameters**-- `provider_name`: Name of the provider to delete, quoted string.--**Example**-```-ragflow> DROP MODEL PROVIDER 'openai';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_llm--**Description**-Sets the default LLM (Large Language Model).--**Usage**-```-SET DEFAULT LLM <llm_id>;-```--**Parameters**-- `llm_id`: LLM identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT LLM 'gpt-4';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_vlm--**Description**-Sets the default VLM (Vision Language Model).--**Usage**-```-SET DEFAULT VLM <vlm_id>;-```--**Parameters**-- `vlm_id`: VLM identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT VLM 'clip-vit-large';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_embedding--**Description**-Sets the default embedding model.--**Usage**-```-SET DEFAULT EMBEDDING <embedding_id>;-```--**Parameters**-- `embedding_id`: Embedding model identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT EMBEDDING 'text-embedding-ada-002';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_reranker--**Description**-Sets the default reranker model.--**Usage**-```-SET DEFAULT RERANKER <reranker_id>;-```--**Parameters**-- `reranker_id`: Reranker model identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT RERANKER 'bge-reranker-large';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_asr--**Description**-Sets the default ASR (Automatic Speech Recognition) model.--**Usage**-```-SET DEFAULT ASR <asr_id>;-```--**Parameters**-- `asr_id`: ASR model identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT ASR 'whisper-large';-```--**Display Effect**-(Sample output will be provided by the user)-------### set_default_tts--**Description**-Sets the default TTS (Text-to-Speech) model.--**Usage**-```-SET DEFAULT TTS <tts_id>;-```--**Parameters**-- `tts_id`: TTS model identifier, quoted string.--**Example**-```-ragflow> SET DEFAULT TTS 'tts-1';-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_llm--**Description**-Resets the default LLM to system default.--**Usage**-```-RESET DEFAULT LLM;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT LLM;-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_vlm--**Description**-Resets the default VLM to system default.--**Usage**-```-RESET DEFAULT VLM;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT VLM;-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_embedding--**Description**-Resets the default embedding model to system default.--**Usage**-```-RESET DEFAULT EMBEDDING;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT EMBEDDING;-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_reranker--**Description**-Resets the default reranker model to system default.--**Usage**-```-RESET DEFAULT RERANKER;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT RERANKER;-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_asr--**Description**-Resets the default ASR model to system default.--**Usage**-```-RESET DEFAULT ASR;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT ASR;-```--**Display Effect**-(Sample output will be provided by the user)-------### reset_default_tts--**Description**-Resets the default TTS model to system default.--**Usage**-```-RESET DEFAULT TTS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> RESET DEFAULT TTS;-```--**Display Effect**-(Sample output will be provided by the user)-------### create_user_dataset_with_parser--**Description**-Creates a user dataset with the specified parser.--**Usage**-```-CREATE DATASET <dataset_name> WITH EMBEDDING <embedding> PARSER <parser_type>;-```--**Parameters**-- `dataset_name`: Dataset name, quoted string.-- `embedding`: Embedding model name, quoted string.-- `parser_type`: Parser type, quoted string.--**Example**-```-ragflow> CREATE DATASET 'my_dataset' WITH EMBEDDING 'text-embedding-ada-002' PARSER 'pdf';-```--**Display Effect**-(Sample output will be provided by the user)-------### create_user_dataset_with_pipeline--**Description**-Creates a user dataset with the specified pipeline.--**Usage**-```-CREATE DATASET <dataset_name> WITH EMBEDDING <embedding> PIPELINE <pipeline>;-```--**Parameters**-- `dataset_name`: Dataset name, quoted string.-- `embedding`: Embedding model name, quoted string.-- `pipeline`: Pipeline name, quoted string.--**Example**-```-ragflow> CREATE DATASET 'my_dataset' WITH EMBEDDING 'text-embedding-ada-002' PIPELINE 'standard';-```--**Display Effect**-(Sample output will be provided by the user)-------### drop_user_dataset--**Description**-Deletes a user dataset.--**Usage**-```-DROP DATASET <dataset_name>;-```--**Parameters**-- `dataset_name`: Name of the dataset to delete, quoted string.--**Example**-```-ragflow> DROP DATASET 'my_dataset';-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_datasets--**Description**-Lists all datasets for the current user.--**Usage**-```-LIST DATASETS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> LIST DATASETS;-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_dataset_files--**Description**-Lists all files in the specified dataset.--**Usage**-```-LIST FILES OF DATASET <dataset_name>;-```--**Parameters**-- `dataset_name`: Dataset name, quoted string.--**Example**-```-ragflow> LIST FILES OF DATASET 'my_dataset';-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_agents--**Description**-Lists all agents for the current user.--**Usage**-```-LIST AGENTS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> LIST AGENTS;-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_chats--**Description**-Lists all chat sessions for the current user.--**Usage**-```-LIST CHATS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> LIST CHATS;-```--**Display Effect**-(Sample output will be provided by the user)-------### create_user_chat--**Description**-Creates a new chat session.--**Usage**-```-CREATE CHAT <chat_name>;-```--**Parameters**-- `chat_name`: Chat session name, quoted string.--**Example**-```-ragflow> CREATE CHAT 'my_chat';-```--**Display Effect**-(Sample output will be provided by the user)-------### drop_user_chat--**Description**-Deletes a chat session.--**Usage**-```-DROP CHAT <chat_name>;-```--**Parameters**-- `chat_name`: Name of the chat session to delete, quoted string.--**Example**-```-ragflow> DROP CHAT 'my_chat';-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_model_providers--**Description**-Lists all model providers for the current user.--**Usage**-```-LIST MODEL PROVIDERS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> LIST MODEL PROVIDERS;-```--**Display Effect**-(Sample output will be provided by the user)-------### list_user_default_models--**Description**-Lists all default model settings for the current user.--**Usage**-```-LIST DEFAULT MODELS;-```--**Parameters**-No parameters.--**Example**-```-ragflow> LIST DEFAULT MODELS;-```--**Display Effect**-(Sample output will be provided by the user)-------### import_docs_into_dataset--**Description**-Imports documents into the specified dataset.--**Usage**-```-IMPORT <document_list> INTO DATASET <dataset_name>;-```--**Parameters**-- `document_list`: List of document paths, multiple paths can be separated by commas, or as a space-separated quoted string.-- `dataset_name`: Target dataset name, quoted string.--**Example**-```-ragflow> IMPORT '/path/to/doc1.pdf,/path/to/doc2.pdf' INTO DATASET 'my_dataset';-```--**Display Effect**-(Sample output will be provided by the user)-------### search_on_datasets--**Description**-Searches in one or more specified datasets.--**Usage**-```-SEARCH <question> ON DATASETS <dataset_list>;-```--**Parameters**-- `question`: Search question, quoted string.-- `dataset_list`: List of dataset names, multiple names can be separated by commas, or as a space-separated quoted string.--**Example**-```-ragflow> SEARCH 'What is RAG?' ON DATASETS 'dataset1,dataset2';-```--**Display Effect**-(Sample output will be provided by the user)-------### parse_dataset_docs--**Description**-Parses specified documents in a dataset.--**Usage**-```-PARSE <document_names> OF DATASET <dataset_name>;-```--**Parameters**-- `document_names`: List of document names, multiple names can be separated by commas, or as a space-separated quoted string.-- `dataset_name`: Dataset name, quoted string.--**Example**-```-ragflow> PARSE 'doc1.pdf,doc2.pdf' OF DATASET 'my_dataset';-```--**Display Effect**-(Sample output will be provided by the user)-------### parse_dataset_sync--**Description**-Synchronously parses the entire dataset.--**Usage**-```-PARSE DATASET <dataset_name> SYNC;-```--**Parameters**-- `dataset_name`: Dataset name, quoted string.--**Example**-```-ragflow> PARSE DATASET 'my_dataset' SYNC;-```--**Display Effect**-(Sample output will be provided by the user)-------### parse_dataset_async--**Description**-Asynchronously parses the entire dataset.--**Usage**-```-PARSE DATASET <dataset_name> ASYNC;-```--**Parameters**-- `dataset_name`: Dataset name, quoted string.--**Example**-```-ragflow> PARSE DATASET 'my_dataset' ASYNC;-```--**Display Effect**-(Sample output will be provided by the user)-------### benchmark--**Description**-Performs performance benchmark testing on the specified user command.--**Usage**-```-BENCHMARK <concurrency> <iterations> <user_command>;-```--**Parameters**-- `concurrency`: Concurrency number, positive integer.-- `iterations`: Number of iterations, positive integer.-- `user_command`: User command to test (must be a valid user command, such as `PING;`).--**Example**-```-ragflow> BENCHMARK 5 10 PING;-```--**Display Effect**-(Sample output will be provided by the user)-------**Notes**-- All string parameters (such as names, IDs, paths) must be enclosed in single quotes (`'`) or double quotes (`"`).-- Commands must end with a semicolon (`;`).-- The prompt is `ragflow>`.diff --git a/admin/client/README.md b/admin/client/README.mddeleted file mode 100644index 4cfabc5dc1..0000000000--- a/admin/client/README.md+++ /dev/null@@ -1,136 +0,0 @@-# RAGFlow Admin Service & CLI--### Introduction--Admin Service is a dedicated management component designed to monitor, maintain, and administrate the RAGFlow system. It provides comprehensive tools for ensuring system stability, performing operational tasks, and managing users and permissions efficiently.--The service offers real-time monitoring of critical components, including the RAGFlow server, Task Executor processes, and dependent services such as MySQL, Infinity, Elasticsearch, Redis, and MinIO. It automatically checks their health status, resource usage, and uptime, and performs restarts in case of failures to minimize downtime.--For user and system management, it supports listing, creating, modifying, and deleting users and their associated resources like knowledge bases and Agents.--Built with scalability and reliability in mind, the Admin Service ensures smooth system operation and simplifies maintenance workflows.--It consists of a server-side Service and a command-line client (CLI), both implemented in Python. User commands are parsed using the Lark parsing toolkit.--- **Admin Service**: A backend service that interfaces with the RAGFlow system to execute administrative operations and monitor its status.-- **Admin CLI**: A command-line interface that allows users to connect to the Admin Service and issue commands for system management.----### Starting the Admin Service--#### Launching from source code--1. Before start Admin Service, please make sure RAGFlow system is already started.--2. Launch from source code:-- ```bash- python admin/server/admin_server.py- ```- The service will start and listen for incoming connections from the CLI on the configured port.--#### Using docker image--1. Before startup, please configure the `docker_compose.yml` file to enable admin server:-- ```bash- command:- - --enable-adminserver- ```--2. Start the containers, the service will start and listen for incoming connections from the CLI on the configured port.----### Using the Admin CLI--1. Ensure the Admin Service is running.-2. Install ragflow-cli.- ```bash- pip install ragflow-cli==0.27.2- ```-3. Launch the CLI client:- ```bash- ragflow-cli -h 127.0.0.1 -p 9381- ```- You will be prompted to enter the superuser's password to log in.- The default password is admin.-- **Parameters:**-- - -h: RAGFlow admin server host address-- - -p: RAGFlow admin server port----## Supported Commands--Commands are case-insensitive and must be terminated with a semicolon (`;`).--### Service Management Commands--- `LIST SERVICES;`- - Lists all available services within the RAGFlow system.-- `SHOW SERVICE <id>;`- - Shows detailed status information for the service identified by `<id>`.---### User Management Commands--- `LIST USERS;`- - Lists all users known to the system.-- `SHOW USER '<username>';`- - Shows details and permissions for the specified user. The username must be enclosed in single or double quotes.--- `CREATE USER <username> <password>;`- - Create user by username and password. The username and password must be enclosed in single or double quotes.--- `DROP USER '<username>';`- - Removes the specified user from the system. Use with caution.-- `ALTER USER PASSWORD '<username>' '<new_password>';`- - Changes the password for the specified user.-- `ALTER USER ACTIVE <username> <on/off>;`- - Changes the user to active or inactive.---### Data and Agent Commands--- `LIST DATASETS OF '<username>';`- - Lists the datasets associated with the specified user.-- `LIST AGENTS OF '<username>';`- - Lists the agents associated with the specified user.--### Meta-Commands--Meta-commands are prefixed with a backslash (`\`).--- `\?` or `\help`- - Shows help information for the available commands.-- `\q` or `\quit`- - Exits the CLI application.--## Examples--```commandline-admin> list users;-+-------------------------------+------------------------+-----------+-------------+-| create_date | email | is_active | nickname |-+-------------------------------+------------------------+-----------+-------------+-| Fri, 22 Nov 2024 16:03:41 GMT | [email protected] | 1 | Jeffery |-| Fri, 22 Nov 2024 16:10:55 GMT | [email protected] | 1 | Waterdancer |-+-------------------------------+------------------------+-----------+-------------+--admin> list services;-+-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+-| extra | host | id | name | port | service_type |-+-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+-| {} | 0.0.0.0 | 0 | ragflow_0 | 9380 | ragflow_server |-| {'meta_type': 'mysql', 'password': 'infini_rag_flow', 'username': 'root'} | localhost | 1 | mysql | 5455 | meta_data |-| {'password': 'infini_rag_flow', 'store_type': 'minio', 'user': 'rag_flow'} | localhost | 2 | minio | 9000 | file_store |-| {'password': 'infini_rag_flow', 'retrieval_type': 'elasticsearch', 'username': 'elastic'} | localhost | 3 | elasticsearch | 1200 | retrieval |-| {'db_name': 'default_db', 'retrieval_type': 'infinity'} | localhost | 4 | infinity | 23817 | retrieval |-| {'database': 1, 'mq_type': 'redis', 'password': 'infini_rag_flow'} | localhost | 5 | redis | 6379 | message_queue |-+-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+-```diff --git a/admin/client/http_client.py b/admin/client/http_client.pydeleted file mode 100644index 3cc62db1a3..0000000000--- a/admin/client/http_client.py+++ /dev/null@@ -1,182 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import time-import json-import typing-from typing import Any, Dict, Optional--import requests-# from requests.sessions import HTTPAdapter---class HttpClient:- def __init__(- self,- host: str = "127.0.0.1",- port: int = 9381,- api_version: str = "v1",- api_key: Optional[str] = None,- connect_timeout: float = 5.0,- read_timeout: float = 60.0,- verify_ssl: bool = False,- ) -> None:- self.host = host- self.port = port- self.api_version = api_version- self.api_key = api_key- self.login_token: str | None = None- self.connect_timeout = connect_timeout- self.read_timeout = read_timeout- self.verify_ssl = verify_ssl-- def api_base(self) -> str:- return f"{self.host}:{self.port}/api/{self.api_version}"-- def non_api_base(self) -> str:- return f"{self.host}:{self.port}/{self.api_version}"-- def build_url(self, path: str, use_api_base: bool = True) -> str:- base = self.api_base() if use_api_base else self.non_api_base()- if self.verify_ssl:- return f"https://{base}/{path.lstrip('/')}"- else:- return f"http://{base}/{path.lstrip('/')}"-- def _headers(self, auth_kind: Optional[str], extra: Optional[Dict[str, str]]) -> Dict[str, str]:- headers = {}- if auth_kind == "api" and self.api_key:- headers["Authorization"] = f"Bearer {self.api_key}"- elif auth_kind == "web" and self.login_token:- headers["Authorization"] = self.login_token- elif auth_kind == "admin" and self.login_token:- headers["Authorization"] = self.login_token- else:- pass- if extra:- headers.update(extra)- return headers-- def request(- self,- method: str,- path: str,- *,- use_api_base: bool = True,- auth_kind: Optional[str] = "api",- headers: Optional[Dict[str, str]] = None,- json_body: Optional[Dict[str, Any]] = None,- data: Any = None,- files: Any = None,- params: Optional[Dict[str, Any]] = None,- stream: bool = False,- iterations: int = 1,- ) -> requests.Response | dict:- url = self.build_url(path, use_api_base=use_api_base)- merged_headers = self._headers(auth_kind, headers)- # timeout: Tuple[float, float] = (self.connect_timeout, self.read_timeout)- session = requests.Session()- # adapter = HTTPAdapter(pool_connections=100, pool_maxsize=100)- # session.mount("http://", adapter)- http_function = typing.Any- match method:- case "GET":- http_function = session.get- case "POST":- http_function = session.post- case "PUT":- http_function = session.put- case "DELETE":- http_function = session.delete- case "PATCH":- http_function = session.patch- case _:- raise ValueError(f"Invalid HTTP method: {method}")-- if iterations > 1:- response_list = []- total_duration = 0.0- for _ in range(iterations):- start_time = time.perf_counter()- response = http_function(url, headers=merged_headers, json=json_body, data=data, stream=stream)- # response = session.get(url, headers=merged_headers, json=json_body, data=data, stream=stream)- # response = requests.request(- # method=method,- # url=url,- # headers=merged_headers,- # json=json_body,- # data=data,- # files=files,- # params=params,- # stream=stream,- # verify=self.verify_ssl,- # )- end_time = time.perf_counter()- total_duration += end_time - start_time- response_list.append(response)- return {"duration": total_duration, "response_list": response_list}- else:- return http_function(url, headers=merged_headers, json=json_body, data=data, stream=stream)- # return session.get(url, headers=merged_headers, json=json_body, data=data, stream=stream)- # return requests.request(- # method=method,- # url=url,- # headers=merged_headers,- # json=json_body,- # data=data,- # files=files,- # params=params,- # stream=stream,- # verify=self.verify_ssl,- # )-- def request_json(- self,- method: str,- path: str,- *,- use_api_base: bool = True,- auth_kind: Optional[str] = "api",- headers: Optional[Dict[str, str]] = None,- json_body: Optional[Dict[str, Any]] = None,- data: Any = None,- files: Any = None,- params: Optional[Dict[str, Any]] = None,- stream: bool = False,- ) -> Dict[str, Any]:- response = self.request(- method,- path,- use_api_base=use_api_base,- auth_kind=auth_kind,- headers=headers,- json_body=json_body,- data=data,- files=files,- params=params,- stream=stream,- )- try:- return response.json()- except Exception as exc:- raise ValueError(f"Non-JSON response from {path}: {exc}") from exc-- @staticmethod- def parse_json_bytes(raw: bytes) -> Dict[str, Any]:- try:- return json.loads(raw.decode("utf-8"))- except Exception as exc:- raise ValueError(f"Invalid JSON payload: {exc}") from excdiff --git a/admin/client/parser.py b/admin/client/parser.pydeleted file mode 100644index c4ad70410c..0000000000--- a/admin/client/parser.py+++ /dev/null@@ -1,926 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--from lark import Transformer--GRAMMAR = r"""-start: command--command: sql_command | meta_command--sql_command: login_user- | ping_server- | list_services- | show_service- | startup_service- | shutdown_service- | restart_service- | register_user- | list_users- | show_user- | drop_user- | alter_user- | create_user- | activate_user- | list_datasets- | list_agents- | create_role- | drop_role- | alter_role- | list_roles- | show_role- | grant_permission- | revoke_permission- | alter_user_role- | show_user_permission- | show_version- | grant_admin- | revoke_admin- | set_variable- | show_variable- | list_variables- | list_configs- | list_environments- | generate_key- | list_keys- | drop_key- | show_current_user- | set_default_llm- | set_default_vlm- | set_default_embedding- | set_default_reranker- | set_default_asr- | set_default_tts- | reset_default_llm- | reset_default_vlm- | reset_default_embedding- | reset_default_reranker- | reset_default_asr- | reset_default_tts- | create_model_provider- | drop_model_provider- | create_user_dataset_with_parser- | create_user_dataset_with_pipeline- | drop_user_dataset- | list_user_datasets- | list_user_dataset_files- | list_user_dataset_documents- | list_user_datasets_metadata- | list_user_documents_metadata_summary- | list_user_agents- | list_user_chats- | create_user_chat- | drop_user_chat- | create_dataset_table- | drop_dataset_table- | create_metadata_table- | drop_metadata_table- | list_user_model_providers- | list_user_default_models- | parse_dataset_docs- | parse_dataset_sync- | parse_dataset_async- | import_docs_into_dataset- | search_on_datasets- | get_chunk- | list_chunks- | update_chunk- | set_metadata- | remove_tags- | remove_chunks- | create_chat_session- | drop_chat_session- | list_chat_sessions- | chat_on_session- | list_server_configs- | show_fingerprint- | set_license- | set_license_config- | show_license- | generate_nav_for_dataset- | navigation_search- | check_license- | benchmark--// meta command definition-meta_command: "\\" meta_command_name [meta_args]--COMMA: ","--meta_command_name: /[a-zA-Z?]+/-meta_args: (meta_arg)+--meta_arg: /[^\s"',]+/ | quoted_string--// command definition--LOGIN: "LOGIN"i-REGISTER: "REGISTER"i-LIST: "LIST"i-SERVICES: "SERVICES"i-SHOW: "SHOW"i-CREATE: "CREATE"i-SERVICE: "SERVICE"i-SHUTDOWN: "SHUTDOWN"i-STARTUP: "STARTUP"i-RESTART: "RESTART"i-USERS: "USERS"i-DROP: "DROP"i-USER: "USER"i-ALTER: "ALTER"i-ACTIVE: "ACTIVE"i-ADMIN: "ADMIN"i-PASSWORD: "PASSWORD"i-DATASET_TABLE: "DATASET TABLE"i-DATASET: "DATASET"i-DATASETS: "DATASETS"i-OF: "OF"i-AGENTS: "AGENTS"i-ROLE: "ROLE"i-ROLES: "ROLES"i-DESCRIPTION: "DESCRIPTION"i-GRANT: "GRANT"i-REVOKE: "REVOKE"i-ALL: "ALL"i-PERMISSION: "PERMISSION"i-TO: "TO"i-FROM: "FROM"i-FOR: "FOR"i-RESOURCES: "RESOURCES"i-ON: "ON"i-SET: "SET"i-RESET: "RESET"i-VERSION: "VERSION"i-VAR: "VAR"i-VARS: "VARS"i-CONFIGS: "CONFIGS"i-ENVS: "ENVS"i-KEY: "KEY"i-KEYS: "KEYS"i-GENERATE: "GENERATE"i-NAVIGATION: "NAVIGATION"i-MODE: "MODE"i-TOPK: "TOPK"i-MODEL: "MODEL"i-MODELS: "MODELS"i-PROVIDER: "PROVIDER"i-PROVIDERS: "PROVIDERS"i-DEFAULT: "DEFAULT"i-CHATS: "CHATS"i-CHAT: "CHAT"i-FILES: "FILES"i-DOCUMENT: "DOCUMENT"i-DOCUMENTS: "DOCUMENTS"i-METADATA: "METADATA"i-SUMMARY: "SUMMARY"i-AS: "AS"i-PARSE: "PARSE"i-IMPORT: "IMPORT"i-INTO: "INTO"i-IN: "IN"i-WITH: "WITH"i-VECTOR: "VECTOR"i-SIZE: "SIZE"i-PARSER: "PARSER"i-PIPELINE: "PIPELINE"i-SEARCH: "SEARCH"i-EXPLORE: "EXPLORE"i-CURRENT: "CURRENT"i-LLM: "LLM"i-VLM: "VLM"i-EMBEDDING: "EMBEDDING"i-RERANKER: "RERANKER"i-ASR: "ASR"i-TTS: "TTS"i-ASYNC: "ASYNC"i-SYNC: "SYNC"i-BENCHMARK: "BENCHMARK"i-PING: "PING"i-SESSION: "SESSION"i-SESSIONS: "SESSIONS"i-SERVER: "SERVER"i-FINGERPRINT: "FINGERPRINT"i-LICENSE: "LICENSE"i-CHECK: "CHECK"i-CONFIG: "CONFIG"i-INDEX: "INDEX"i-TABLE: "TABLE"i-CHUNK: "CHUNK"i-CHUNKS: "CHUNKS"i-GET: "GET"i-PAGE: "PAGE"i-KEYWORDS: "KEYWORDS"i-AVAILABLE: "AVAILABLE"i-UPDATE: "UPDATE"i-REMOVE: "REMOVE"i-TAGS: "TAGS"i--login_user: LOGIN USER quoted_string (PASSWORD quoted_string)? ";"-list_services: LIST SERVICES ";"-show_service: SHOW SERVICE NUMBER ";"-startup_service: STARTUP SERVICE NUMBER ";"-shutdown_service: SHUTDOWN SERVICE NUMBER ";"-restart_service: RESTART SERVICE NUMBER ";"--register_user: REGISTER USER quoted_string AS quoted_string PASSWORD quoted_string ";"-list_users: LIST USERS ";"-drop_user: DROP USER quoted_string ";"-alter_user: ALTER USER PASSWORD quoted_string quoted_string ";"-show_user: SHOW USER quoted_string ";"-create_user: CREATE USER quoted_string quoted_string ";"-activate_user: ALTER USER ACTIVE quoted_string status ";"--list_datasets: LIST DATASETS OF quoted_string ";"-list_agents: LIST AGENTS OF quoted_string ";"--create_role: CREATE ROLE identifier [DESCRIPTION quoted_string] ";"-drop_role: DROP ROLE identifier ";"-alter_role: ALTER ROLE identifier SET DESCRIPTION quoted_string ";"-list_roles: LIST ROLES ";"-show_role: SHOW ROLE identifier ";"--grant_permission: GRANT identifier_list ON identifier TO ROLE identifier ";"-revoke_permission: REVOKE identifier_list ON identifier FROM ROLE identifier ";"-alter_user_role: ALTER USER quoted_string SET ROLE identifier ";"-show_user_permission: SHOW USER PERMISSION quoted_string ";"--show_version: SHOW VERSION ";"--grant_admin: GRANT ADMIN quoted_string ";"-revoke_admin: REVOKE ADMIN quoted_string ";"--generate_key: GENERATE KEY FOR USER quoted_string ";"-list_keys: LIST KEYS OF quoted_string ";"-drop_key: DROP KEY quoted_string OF quoted_string ";"--set_variable: SET VAR identifier variable_value ";"-show_variable: SHOW VAR identifier ";"-list_variables: LIST VARS ";"-list_configs: LIST CONFIGS ";"-list_environments: LIST ENVS ";"--show_fingerprint: SHOW FINGERPRINT ";"-set_license: SET LICENSE quoted_string ";"-set_license_config: SET LICENSE CONFIG NUMBER NUMBER ";"-show_license: SHOW LICENSE ";"-check_license: CHECK LICENSE ";"--list_server_configs: LIST SERVER CONFIGS ";"--benchmark: BENCHMARK NUMBER NUMBER user_statement--user_statement: ping_server- | show_current_user- | create_model_provider- | drop_model_provider- | set_default_llm- | set_default_vlm- | set_default_embedding- | set_default_reranker- | set_default_asr- | set_default_tts- | reset_default_llm- | reset_default_vlm- | reset_default_embedding- | reset_default_reranker- | reset_default_asr- | reset_default_tts- | create_user_dataset_with_parser- | create_user_dataset_with_pipeline- | drop_user_dataset- | list_user_datasets- | list_user_dataset_files- | list_user_agents- | list_user_chats- | create_user_chat- | drop_user_chat- | list_user_model_providers- | list_user_default_models- | import_docs_into_dataset- | search_on_datasets- | update_chunk- | set_metadata- | remove_tags- | create_chat_session- | drop_chat_session- | list_chat_sessions- | chat_on_session--ping_server: PING ";"-show_current_user: SHOW CURRENT USER ";"-create_model_provider: CREATE MODEL PROVIDER quoted_string quoted_string ";"-drop_model_provider: DROP MODEL PROVIDER quoted_string ";"-set_default_llm: SET DEFAULT LLM quoted_string ";"-set_default_vlm: SET DEFAULT VLM quoted_string ";"-set_default_embedding: SET DEFAULT EMBEDDING quoted_string ";"-set_default_reranker: SET DEFAULT RERANKER quoted_string ";"-set_default_asr: SET DEFAULT ASR quoted_string ";"-set_default_tts: SET DEFAULT TTS quoted_string ";"--reset_default_llm: RESET DEFAULT LLM ";"-reset_default_vlm: RESET DEFAULT VLM ";"-reset_default_embedding: RESET DEFAULT EMBEDDING ";"-reset_default_reranker: RESET DEFAULT RERANKER ";"-reset_default_asr: RESET DEFAULT ASR ";"-reset_default_tts: RESET DEFAULT TTS ";"--list_user_datasets: LIST DATASETS ";"-create_user_dataset_with_parser: CREATE DATASET quoted_string WITH EMBEDDING quoted_string PARSER quoted_string ";"-create_user_dataset_with_pipeline: CREATE DATASET quoted_string WITH EMBEDDING quoted_string PIPELINE quoted_string ";"-drop_user_dataset: DROP DATASET quoted_string ";"-list_user_dataset_files: LIST FILES OF DATASET quoted_string ";"-list_user_dataset_documents: LIST DOCUMENTS OF DATASET quoted_string ";"-list_user_datasets_metadata: LIST METADATA OF DATASETS quoted_string (COMMA quoted_string)* ";"-list_user_documents_metadata_summary: LIST METADATA SUMMARY OF DATASET quoted_string (DOCUMENTS quoted_string (COMMA quoted_string)*)? ";"-list_user_agents: LIST AGENTS ";"-list_user_chats: LIST CHATS ";"-create_user_chat: CREATE CHAT quoted_string ";"-drop_user_chat: DROP CHAT quoted_string ";"-create_chat_session: CREATE CHAT quoted_string SESSION ";"-drop_chat_session: DROP CHAT quoted_string SESSION quoted_string ";"-list_chat_sessions: LIST CHAT quoted_string SESSIONS ";"-chat_on_session: CHAT quoted_string ON quoted_string SESSION quoted_string ";"-list_user_model_providers: LIST MODEL PROVIDERS ";"-list_user_default_models: LIST DEFAULT MODELS ";"-import_docs_into_dataset: IMPORT quoted_string INTO DATASET quoted_string ";"-search_on_datasets: SEARCH quoted_string ON DATASETS quoted_string ";"-get_chunk: GET CHUNK quoted_string ";"-list_chunks: LIST CHUNKS OF DOCUMENT quoted_string ("PAGE" NUMBER)? ("SIZE" NUMBER)? ("KEYWORDS" quoted_string)? ("AVAILABLE" NUMBER)? ";"-set_metadata: SET METADATA OF DOCUMENT quoted_string TO quoted_string ";"-remove_tags: REMOVE TAGS quoted_string (COMMA quoted_string)* FROM DATASET quoted_string ";"-remove_chunks: REMOVE CHUNKS quoted_string (COMMA quoted_string)* FROM DOCUMENT quoted_string ";"- | REMOVE ALL CHUNKS FROM DOCUMENT quoted_string ";"--parse_dataset_docs: PARSE quoted_string OF DATASET quoted_string ";"-parse_dataset_sync: PARSE DATASET quoted_string SYNC ";"-parse_dataset_async: PARSE DATASET quoted_string ASYNC ";"--// Internal CLI only for GO-create_dataset_table: CREATE DATASET TABLE quoted_string VECTOR SIZE NUMBER ";"-drop_dataset_table: DROP DATASET TABLE quoted_string ";"-create_metadata_table: CREATE METADATA TABLE ";"-drop_metadata_table: DROP METADATA TABLE ";"-generate_nav_for_dataset: GENERATE NAVIGATION OF DATASET quoted_string ";"-navigation_search: NAVIGATION SEARCH quoted_string IN DATASET quoted_string MODE quoted_string (TOPK NUMBER)? ";"-update_chunk: UPDATE CHUNK quoted_string OF DATASET quoted_string SET quoted_string ";"--identifier_list: identifier (COMMA identifier)*--identifier: WORD-variable_value: WORD | NUMBER | QUOTED_STRING-quoted_string: QUOTED_STRING-status: ON | WORD--QUOTED_STRING: /'[^']+'/ | /"[^"]+"/-WORD: /[a-zA-Z0-9_\-\.]+/-NUMBER: /[0-9]+/--%import common.WS-%ignore WS-"""---class RAGFlowCLITransformer(Transformer):- def start(self, items):- return items[0]-- def command(self, items):- return items[0]-- def login_user(self, items):- email = items[2].children[0].strip("'\"")- if len(items) == 5:- # With password: LOGIN USER email PASSWORD password- password = items[4].children[0].strip("'\"")- return {"type": "login_user", "email": email, "password": password}- else:- # Without password: LOGIN USER email- return {"type": "login_user", "email": email}-- def ping_server(self, items):- return {"type": "ping_server"}-- def list_services(self, items):- result = {"type": "list_services"}- return result-- def show_service(self, items):- service_id = int(items[2])- return {"type": "show_service", "number": service_id}-- def startup_service(self, items):- service_id = int(items[2])- return {"type": "startup_service", "number": service_id}-- def shutdown_service(self, items):- service_id = int(items[2])- return {"type": "shutdown_service", "number": service_id}-- def restart_service(self, items):- service_id = int(items[2])- return {"type": "restart_service", "number": service_id}-- def register_user(self, items):- user_name: str = items[2].children[0].strip("'\"")- nickname: str = items[4].children[0].strip("'\"")- password: str = items[6].children[0].strip("'\"")- return {"type": "register_user", "user_name": user_name, "nickname": nickname, "password": password}-- def list_users(self, items):- return {"type": "list_users"}-- def show_user(self, items):- user_name = items[2]- return {"type": "show_user", "user_name": user_name}-- def drop_user(self, items):- user_name = items[2]- return {"type": "drop_user", "user_name": user_name}-- def alter_user(self, items):- user_name = items[3]- new_password = items[4]- return {"type": "alter_user", "user_name": user_name, "password": new_password}-- def create_user(self, items):- user_name = items[2]- password = items[3]- return {"type": "create_user", "user_name": user_name, "password": password, "role": "user"}-- def activate_user(self, items):- user_name = items[3]- activate_status = items[4]- return {"type": "activate_user", "activate_status": activate_status, "user_name": user_name}-- def list_datasets(self, items):- user_name = items[3]- return {"type": "list_datasets", "user_name": user_name}-- def list_agents(self, items):- user_name = items[3]- return {"type": "list_agents", "user_name": user_name}-- def create_role(self, items):- role_name = items[2]- if len(items) > 4:- description = items[4]- return {"type": "create_role", "role_name": role_name, "description": description}- else:- return {"type": "create_role", "role_name": role_name}-- def drop_role(self, items):- role_name = items[2]- return {"type": "drop_role", "role_name": role_name}-- def alter_role(self, items):- role_name = items[2]- description = items[5]- return {"type": "alter_role", "role_name": role_name, "description": description}-- def list_roles(self, items):- return {"type": "list_roles"}-- def show_role(self, items):- role_name = items[2]- return {"type": "show_role", "role_name": role_name}-- def grant_permission(self, items):- action_list = items[1]- resource = items[3]- role_name = items[6]- return {"type": "grant_permission", "role_name": role_name, "resource": resource, "actions": action_list}-- def revoke_permission(self, items):- action_list = items[1]- resource = items[3]- role_name = items[6]- return {"type": "revoke_permission", "role_name": role_name, "resource": resource, "actions": action_list}-- def alter_user_role(self, items):- user_name = items[2]- role_name = items[5]- return {"type": "alter_user_role", "user_name": user_name, "role_name": role_name}-- def show_user_permission(self, items):- user_name = items[3]- return {"type": "show_user_permission", "user_name": user_name}-- def show_version(self, items):- return {"type": "show_version"}-- def grant_admin(self, items):- user_name = items[2]- return {"type": "grant_admin", "user_name": user_name}-- def revoke_admin(self, items):- user_name = items[2]- return {"type": "revoke_admin", "user_name": user_name}-- def generate_key(self, items):- user_name = items[4]- return {"type": "generate_key", "user_name": user_name}-- def list_keys(self, items):- user_name = items[3]- return {"type": "list_keys", "user_name": user_name}-- def drop_key(self, items):- key = items[2]- user_name = items[4]- return {"type": "drop_key", "key": key, "user_name": user_name}-- def set_variable(self, items):- var_name = items[2]- var_value = items[3]- return {"type": "set_variable", "var_name": var_name, "var_value": var_value}-- def show_variable(self, items):- var_name = items[2]- return {"type": "show_variable", "var_name": var_name}-- def list_variables(self, items):- return {"type": "list_variables"}-- def list_configs(self, items):- return {"type": "list_configs"}-- def list_environments(self, items):- return {"type": "list_environments"}-- def show_fingerprint(self, items):- return {"type": "show_fingerprint"}-- def set_license(self, items):- license = items[2].children[0].strip("'\"")- return {"type": "set_license", "license": license}-- def set_license_config(self, items):- value1: int = int(items[3])- value2: int = int(items[4])- return {"type": "set_license_config", "value1": value1, "value2": value2}-- def show_license(self, items):- return {"type": "show_license"}-- def check_license(self, items):- return {"type": "check_license"}-- def list_server_configs(self, items):- return {"type": "list_server_configs"}-- def create_model_provider(self, items):- provider_name = items[3].children[0].strip("'\"")- provider_key = items[4].children[0].strip("'\"")- return {"type": "create_model_provider", "provider_name": provider_name, "provider_key": provider_key}-- def drop_model_provider(self, items):- provider_name = items[3].children[0].strip("'\"")- return {"type": "drop_model_provider", "provider_name": provider_name}-- def show_current_user(self, items):- return {"type": "show_current_user"}-- def set_default_llm(self, items):- llm_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "llm_id", "model_id": llm_id}-- def set_default_vlm(self, items):- vlm_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "img2txt_id", "model_id": vlm_id}-- def set_default_embedding(self, items):- embedding_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "embd_id", "model_id": embedding_id}-- def set_default_reranker(self, items):- reranker_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "reranker_id", "model_id": reranker_id}-- def set_default_asr(self, items):- asr_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "asr_id", "model_id": asr_id}-- def set_default_tts(self, items):- tts_id = items[3].children[0].strip("'\"")- return {"type": "set_default_model", "model_type": "tts_id", "model_id": tts_id}-- def reset_default_llm(self, items):- return {"type": "reset_default_model", "model_type": "llm_id"}-- def reset_default_vlm(self, items):- return {"type": "reset_default_model", "model_type": "img2txt_id"}-- def reset_default_embedding(self, items):- return {"type": "reset_default_model", "model_type": "embd_id"}-- def reset_default_reranker(self, items):- return {"type": "reset_default_model", "model_type": "reranker_id"}-- def reset_default_asr(self, items):- return {"type": "reset_default_model", "model_type": "asr_id"}-- def reset_default_tts(self, items):- return {"type": "reset_default_model", "model_type": "tts_id"}-- def list_user_datasets(self, items):- return {"type": "list_user_datasets"}-- def create_user_dataset_with_parser(self, items):- dataset_name = items[2].children[0].strip("'\"")- embedding = items[5].children[0].strip("'\"")- parser_type = items[7].children[0].strip("'\"")- return {"type": "create_user_dataset", "dataset_name": dataset_name, "embedding": embedding, "parser_type": parser_type}-- def create_user_dataset_with_pipeline(self, items):- dataset_name = items[2].children[0].strip("'\"")- embedding = items[5].children[0].strip("'\"")- pipeline = items[7].children[0].strip("'\"")- return {"type": "create_user_dataset", "dataset_name": dataset_name, "embedding": embedding, "pipeline": pipeline}-- def drop_user_dataset(self, items):- dataset_name = items[2].children[0].strip("'\"")- return {"type": "drop_user_dataset", "dataset_name": dataset_name}-- def list_user_dataset_files(self, items):- dataset_name = items[4].children[0].strip("'\"")- return {"type": "list_user_dataset_files", "dataset_name": dataset_name}-- def list_user_dataset_documents(self, items):- dataset_name = items[4].children[0].strip("'\"")- return {"type": "list_user_dataset_documents", "dataset_name": dataset_name}-- def list_user_datasets_metadata(self, items):- dataset_names = []- dataset_names.append(items[4].children[0].strip("'\""))- for i in range(5, len(items)):- if items[i] and hasattr(items[i], "children") and items[i].children:- dataset_names.append(items[i].children[0].strip("'\""))- return {"type": "list_user_datasets_metadata", "dataset_names": dataset_names}-- def list_user_documents_metadata_summary(self, items):- dataset_name = items[5].children[0].strip("'\"")- doc_ids = []- if len(items) > 6 and items[6] == "DOCUMENTS":- for i in range(7, len(items)):- if items[i] and hasattr(items[i], "children") and items[i].children:- doc_id = items[i].children[0].strip("'\"")- doc_ids.append(doc_id)- return {"type": "list_user_documents_metadata_summary", "dataset_name": dataset_name, "document_ids": doc_ids}-- def list_user_agents(self, items):- return {"type": "list_user_agents"}-- def list_user_chats(self, items):- return {"type": "list_user_chats"}-- def create_user_chat(self, items):- chat_name = items[2].children[0].strip("'\"")- return {"type": "create_user_chat", "chat_name": chat_name}-- def drop_user_chat(self, items):- chat_name = items[2].children[0].strip("'\"")- return {"type": "drop_user_chat", "chat_name": chat_name}-- def create_dataset_table(self, items):- dataset_name = None- vector_size = None- for i, item in enumerate(items):- if hasattr(item, "data") and item.data == "quoted_string":- dataset_name = item.children[0].strip("'\"")- if hasattr(item, "type") and item.type == "NUMBER":- if i > 0 and items[i - 1].type == "SIZE" and items[i - 2].type == "VECTOR":- vector_size = int(item)- return {"type": "create_dataset_table", "dataset_name": dataset_name, "vector_size": vector_size}-- def drop_dataset_table(self, items):- dataset_name = None- for item in items:- if hasattr(item, "data") and item.data == "quoted_string":- dataset_name = item.children[0].strip("'\"")- return {"type": "drop_dataset_table", "dataset_name": dataset_name}-- def create_metadata_table(self, items):- return {"type": "create_metadata_table"}-- def drop_metadata_table(self, items):- return {"type": "drop_metadata_table"}-- def list_user_model_providers(self, items):- return {"type": "list_user_model_providers"}-- def list_user_default_models(self, items):- return {"type": "list_user_default_models"}-- def parse_dataset_docs(self, items):- document_list_str = items[1].children[0].strip("'\"")- document_names = document_list_str.split(",")- if len(document_names) == 1:- document_names = document_names[0]- document_names = document_names.split(" ")- dataset_name = items[4].children[0].strip("'\"")- return {"type": "parse_dataset_docs", "dataset_name": dataset_name, "document_names": document_names}-- def parse_dataset_sync(self, items):- dataset_name = items[2].children[0].strip("'\"")- return {"type": "parse_dataset", "dataset_name": dataset_name, "method": "sync"}-- def parse_dataset_async(self, items):- dataset_name = items[2].children[0].strip("'\"")- return {"type": "parse_dataset", "dataset_name": dataset_name, "method": "async"}-- def create_chat_session(self, items):- chat_name = items[2].children[0].strip("'\"")- return {"type": "create_chat_session", "chat_name": chat_name}-- def drop_chat_session(self, items):- chat_name = items[2].children[0].strip("'\"")- session_id = items[4].children[0].strip("'\"")- return {"type": "drop_chat_session", "chat_name": chat_name, "session_id": session_id}-- def list_chat_sessions(self, items):- chat_name = items[2].children[0].strip("'\"")- return {"type": "list_chat_sessions", "chat_name": chat_name}-- def chat_on_session(self, items):- message = items[1].children[0].strip("'\"")- chat_name = items[3].children[0].strip("'\"")- session_id = items[5].children[0].strip("'\"")- return {"type": "chat_on_session", "message": message, "chat_name": chat_name, "session_id": session_id}-- def import_docs_into_dataset(self, items):- document_list_str = items[1].children[0].strip("'\"")- document_paths = document_list_str.split(",")- if len(document_paths) == 1:- document_paths = document_paths[0]- document_paths = document_paths.split(" ")- dataset_name = items[4].children[0].strip("'\"")- return {"type": "import_docs_into_dataset", "dataset_name": dataset_name, "document_paths": document_paths}-- def search_on_datasets(self, items):- question = items[1].children[0].strip("'\"")- datasets_str = items[4].children[0].strip("'\"")- datasets = datasets_str.split(",")- if len(datasets) == 1:- datasets = datasets[0]- datasets = datasets.split(" ")- return {"type": "search_on_datasets", "datasets": datasets, "question": question}-- def get_chunk(self, items):- chunk_id = items[2].children[0].strip("'\"")- return {"type": "get_chunk", "chunk_id": chunk_id}-- def generate_nav_for_dataset(self, items):- dataset_id = items[4].children[0].strip("'\"")- return {"type": "generate_nav_for_dataset", "dataset_id": dataset_id}-- def navigation_search(self, items):- query = items[2].children[0].strip("'\"")- dataset_name = items[5].children[0].strip("'\"")- mode = items[7].children[0].strip("'\"")- topk = None- # If TOPK was specified, items will be longer.- if len(items) > 9:- try:- extra = items[8]- if hasattr(extra, "data"):- # group wrapper: extra.children = [TOPK, NUMBER]- topk = int(extra.children[1])- else:- # flattened: items[8] = TOPK, items[9] = NUMBER- topk = int(items[9])- except (ValueError, IndexError):- topk = None- return {- "type": "navigation_search",- "query": query,- "dataset_id": dataset_name,- "mode": mode,- "topk": topk,- }-- def update_chunk(self, items):- def get_quoted_value(item):- if hasattr(item, "children") and item.children:- return item.children[0].strip("'\"")- return str(item).strip("'\"")-- chunk_id = get_quoted_value(items[2])- dataset_name = get_quoted_value(items[5])- json_body = get_quoted_value(items[7])- return {"type": "update_chunk", "chunk_id": chunk_id, "dataset_name": dataset_name, "json_body": json_body}-- def set_metadata(self, items):- doc_id = items[4].children[0].strip("'\"")- meta_json = items[6].children[0].strip("'\"")- return {"type": "set_metadata", "doc_id": doc_id, "meta": meta_json}-- def remove_tags(self, items):- # items: REMOVE, TAGS, quoted_string(tag1), quoted_string(tag2), ..., FROM, DATASET, quoted_string(dataset_name), ";"- tags = []- # Start from index 2 (after TAGS keyword) and parse quoted strings until FROM- for i in range(2, len(items)):- item = items[i]- # Check for FROM token to stop- if hasattr(item, "type") and item.type == "FROM":- break- if hasattr(item, "children") and item.children:- tag = item.children[0].strip("'\"")- tags.append(tag)- # Find dataset_name: quoted_string after DATASET- dataset_name = None- for i, item in enumerate(items):- # Check if item is a DATASET token- if hasattr(item, "type") and item.type == "DATASET":- # Next item should be quoted_string- dataset_name = items[i + 1].children[0].strip("'\"")- break- return {"type": "remove_tags", "dataset_name": dataset_name, "tags": tags}-- def remove_chunks(self, items):- # Handle two cases:- # 1. REMOVE CHUNKS quoted_string (COMMA quoted_string)* FROM DOCUMENT quoted_string ";"- # 2. REMOVE ALL CHUNKS FROM DOCUMENT quoted_string ";"-- # Check if it's "REMOVE ALL CHUNKS"- for item in items:- if hasattr(item, "type") and item.type == "ALL":- # Find doc_id- for j, inner_item in enumerate(items):- if hasattr(inner_item, "type") and inner_item.type == "DOCUMENT":- doc_id = items[j + 1].children[0].strip("'\"")- return {"type": "remove_chunks", "doc_id": doc_id, "delete_all": True}-- # Otherwise, we have chunk_ids- chunk_ids = []- doc_id = None- for i, item in enumerate(items):- if hasattr(item, "type") and item.type == "DOCUMENT":- doc_id = items[i + 1].children[0].strip("'\"")- elif hasattr(item, "children") and item.children:- val = item.children[0].strip("'\"")- # Skip if it's "FROM" or "DOCUMENT"- if val.upper() in ["FROM", "DOCUMENT"]:- continue- chunk_ids.append(val)-- return {"type": "remove_chunks", "doc_id": doc_id, "chunk_ids": chunk_ids}-- def list_chunks(self, items):- doc_id = items[4].children[0].strip("'\"")- result = {"type": "list_chunks", "doc_id": doc_id}-- # Parse optional parameters: PAGE, SIZE, KEYWORDS, AVAILABLE- # items structure varies based on which params are present- for i, item in enumerate(items):- if str(item) == "PAGE":- result["page"] = int(items[i + 1])- elif str(item) == "SIZE":- result["size"] = int(items[i + 1])- elif str(item) == "KEYWORDS":- result["keywords"] = items[i + 1].children[0].strip("'\"")- elif str(item) == "AVAILABLE":- result["available_int"] = int(items[i + 1])-- return result-- def benchmark(self, items):- concurrency: int = int(items[1])- iterations: int = int(items[2])- command = items[3].children[0]- return {"type": "benchmark", "concurrency": concurrency, "iterations": iterations, "command": command}-- def action_list(self, items):- return items-- def meta_command(self, items):- command_name = str(items[0]).lower()- args = items[1:] if len(items) > 1 else []-- # handle quoted parameter- parsed_args = []- for arg in args:- if hasattr(arg, "value"):- parsed_args.append(arg.value)- else:- parsed_args.append(str(arg))-- return {"type": "meta", "command": command_name, "args": parsed_args}-- def meta_command_name(self, items):- return items[0]-- def meta_args(self, items):- return itemsdiff --git a/admin/client/pyproject.toml b/admin/client/pyproject.tomldeleted file mode 100644index 926b704503..0000000000--- a/admin/client/pyproject.toml+++ /dev/null@@ -1,32 +0,0 @@-[project]-name = "ragflow-cli"-version = "0.27.2"-description = "Admin Service's client of [RAGFlow](https://github.com/infiniflow/ragflow). The Admin Service provides user management and system monitoring. "-authors = [{ name = "Lynn", email = "[email protected]" }]-license = { text = "Apache License, Version 2.0" }-readme = "README.md"-requires-python = ">=3.13,<3.14"-dependencies = [- "requests>=2.30.0,<3.0.0",- "beartype>=0.22.9,<1.0.0",- "pycryptodomex>=3.10.0",- "lark>=1.1.0",- "requests-toolbelt>=1.0.0",-]--[dependency-groups]-test = [- "pytest>=8.3.5",- "requests>=2.32.3",-]--[tool.uv]-constraint-dependencies = [- "urllib3>=2.7.0",-]--[tool.setuptools]-py-modules = ["ragflow_cli", "parser", "http_client", "ragflow_client", "user"]--[project.scripts]-ragflow-cli = "ragflow_cli:main"diff --git a/admin/client/ragflow_cli.py b/admin/client/ragflow_cli.pydeleted file mode 100644index 1a48af07ba..0000000000--- a/admin/client/ragflow_cli.py+++ /dev/null@@ -1,330 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import sys-import argparse-import base64-import getpass-import os-import atexit-import readline-from cmd import Cmd-from typing import Any, Dict, List--import requests-import warnings-from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5-from Cryptodome.PublicKey import RSA-from lark import Lark, Tree-from parser import GRAMMAR, RAGFlowCLITransformer-from http_client import HttpClient-from ragflow_client import RAGFlowClient, run_command-from user import login_user--warnings.filterwarnings("ignore", category=getpass.GetPassWarning)---def encrypt(input_string):- pub = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----"- pub_key = RSA.importKey(pub)- cipher = Cipher_pkcs1_v1_5.new(pub_key)- cipher_text = cipher.encrypt(base64.b64encode(input_string.encode("utf-8")))- return base64.b64encode(cipher_text).decode("utf-8")---def encode_to_base64(input_string):- base64_encoded = base64.b64encode(input_string.encode("utf-8"))- return base64_encoded.decode("utf-8")---class RAGFlowCLI(Cmd):- def __init__(self):- super().__init__()- self.parser = Lark(GRAMMAR, start="start", parser="lalr", transformer=RAGFlowCLITransformer())- self.command_history = []- self.account = "[email protected]"- self.account_password: str = "admin"- self.session = requests.Session()- self.host: str = ""- self.port: int = 0- self.mode: str = "admin"- self.ragflow_client = None- # History file for readline persistence- self.history_file = os.path.expanduser("~/.ragflow_cli_history")- # Load existing history- self._load_history()- # Register cleanup to save history on exit- atexit.register(self._save_history)-- intro = r"""Type "\h" for help."""- prompt = "ragflow> "-- def onecmd(self, command: str) -> bool:- try:- result = self.parse_command(command)-- if isinstance(result, dict):- if "type" in result and result.get("type") == "empty":- return False-- self.execute_command(result)-- if isinstance(result, Tree):- return False-- if result.get("type") == "meta" and result.get("command") in ["q", "quit", "exit"]:- return True-- except KeyboardInterrupt:- print("\nUse '\\q' to quit")- except EOFError:- print("\nGoodbye!")- return True- return False-- def emptyline(self) -> bool:- return False-- def default(self, line: str) -> bool:- return self.onecmd(line)-- def parse_command(self, command_str: str) -> dict[str, str]:- if not command_str.strip():- return {"type": "empty"}-- self.command_history.append(command_str)- readline.add_history(command_str)-- try:- result = self.parser.parse(command_str)- return result- except Exception as e:- return {"type": "error", "message": f"Parse error: {str(e)}"}-- def verify_auth(self, arguments: dict, single_command: bool, auth: bool):- server_type = arguments.get("type", "admin")- http_client = HttpClient(arguments["host"], arguments["port"])- if not auth:- self.ragflow_client = RAGFlowClient(http_client, server_type)- return True-- user_name = arguments["username"]- attempt_count = 3- if single_command:- attempt_count = 1-- try_count = 0- while True:- try_count += 1- if try_count > attempt_count:- return False-- if single_command:- user_password = arguments["password"]- else:- user_password = getpass.getpass(f"password for {user_name}: ").strip()-- try:- token = login_user(http_client, server_type, user_name, user_password)- http_client.login_token = token- self.ragflow_client = RAGFlowClient(http_client, server_type)- return True- except Exception as e:- print(str(e))- print("Can't access server for login (connection failed)")-- def _format_service_detail_table(self, data):- if isinstance(data, list):- return data- if not all([isinstance(v, list) for v in data.values()]):- # normal table- return data- # handle task_executor heartbeats map, for example {'name': [{'done': 2, 'now': timestamp1}, {'done': 3, 'now': timestamp2}]- task_executor_list = []- for k, v in data.items():- # display latest status- heartbeats = sorted(v, key=lambda x: x["now"], reverse=True)- task_executor_list.append(- {- "task_executor_name": k,- **heartbeats[0],- }- if heartbeats- else {"task_executor_name": k}- )- return task_executor_list-- def _print_table_simple(self, data):- if not data:- print("No data to print")- return- if isinstance(data, dict):- # handle single row data- data = [data]-- columns = list(set().union(*(d.keys() for d in data)))- columns.sort()- col_widths = {}-- def get_string_width(text):- half_width_chars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\t\n\r"- width = 0- for char in text:- if char in half_width_chars:- width += 1- else:- width += 2- return width-- for col in columns:- max_width = get_string_width(str(col))- for item in data:- value_len = get_string_width(str(item.get(col, "")))- if value_len > max_width:- max_width = value_len- col_widths[col] = max(2, max_width)-- # Generate delimiter- separator = "+" + "+".join(["-" * (col_widths[col] + 2) for col in columns]) + "+"-- # Print header- print(separator)- header = "|" + "|".join([f" {col:<{col_widths[col]}} " for col in columns]) + "|"- print(header)- print(separator)-- # Print data- for item in data:- row = "|"- for col in columns:- value = str(item.get(col, ""))- if get_string_width(value) > col_widths[col]:- value = value[: col_widths[col] - 3] + "..."- row += f" {value:<{col_widths[col] - (get_string_width(value) - len(value))}} |"- print(row)-- print(separator)-- def _load_history(self):- """Load command history from file."""- try:- if os.path.exists(self.history_file):- readline.read_history_file(self.history_file)- except Exception:- pass # Ignore errors loading history-- def _save_history(self):- """Save command history to file."""- try:- readline.write_history_file(self.history_file)- except Exception:- pass # Ignore errors saving history-- def run_interactive(self, args):- if self.verify_auth(args, single_command=False, auth=args["auth"]):- print(r"""- ____ ___ ______________ ________ ____- / __ \/ | / ____/ ____/ /___ _ __ / ____/ / / _/- / /_/ / /| |/ / __/ /_ / / __ \ | /| / / / / / / / /- / _, _/ ___ / /_/ / __/ / / /_/ / |/ |/ / / /___/ /____/ /- /_/ |_/_/ |_\____/_/ /_/\____/|__/|__/ \____/_____/___/- """)- self.cmdloop()-- print("RAGFlow command line interface - Type '\\?' for help, '\\q' to quit")-- def run_single_command(self, args):- if self.verify_auth(args, single_command=True, auth=args["auth"]):- command = args["command"]- result = self.parse_command(command)- self.execute_command(result)-- def parse_connection_args(self, args: List[str]) -> Dict[str, Any]:- parser = argparse.ArgumentParser(description="RAGFlow CLI Client", add_help=False)- parser.add_argument("-h", "--host", default="127.0.0.1", help="Admin or RAGFlow service host")- parser.add_argument("-p", "--port", type=int, default=9381, help="Admin or RAGFlow service port")- parser.add_argument("-w", "--password", default="admin", type=str, help="Superuser password")- parser.add_argument("-t", "--type", default="admin", type=str, help="CLI mode, admin or user")- parser.add_argument("-u", "--username", default=None, help="Username (email). In admin mode defaults to [email protected], in user mode required.")- parser.add_argument("command", nargs="?", help="Single command")- try:- parsed_args, remaining_args = parser.parse_known_args(args)- # Determine username based on mode- username = parsed_args.username- if parsed_args.type == "admin":- if username is None:- username = "[email protected]"-- if remaining_args:- if remaining_args[0] == "command":- command_str = " ".join(remaining_args[1:]) + ";"- auth = True- if remaining_args[1] == "register":- auth = False- else:- if username is None:- print("Error: username (-u) is required in user mode")- return {"error": "Username required"}- return {"host": parsed_args.host, "port": parsed_args.port, "password": parsed_args.password, "type": parsed_args.type, "username": username, "command": command_str, "auth": auth}- else:- return {"error": "Invalid command"}- else:- auth = True- if username is None:- auth = False- return {"host": parsed_args.host, "port": parsed_args.port, "type": parsed_args.type, "username": username, "auth": auth}- except SystemExit:- return {"error": "Invalid connection arguments"}-- def execute_command(self, parsed_command: Dict[str, Any]):- command_dict: dict- if isinstance(parsed_command, Tree):- command_dict = parsed_command.children[0]- else:- if parsed_command["type"] == "error":- print(f"Error: {parsed_command['message']}")- return- else:- command_dict = parsed_command-- # print(f"Parsed command: {command_dict}")- run_command(self.ragflow_client, command_dict)---def main():-- cli = RAGFlowCLI()-- args = cli.parse_connection_args(sys.argv)- if "error" in args:- print("Error: Invalid connection arguments")- return-- if "command" in args:- # single command mode- # for user mode, api key or password is ok- # for admin mode, only password- if "password" not in args:- print("Error: password is missing")- return-- cli.run_single_command(args)- else:- cli.run_interactive(args)---if __name__ == "__main__":- main()diff --git a/admin/client/ragflow_client.py b/admin/client/ragflow_client.pydeleted file mode 100644index bf9f025310..0000000000--- a/admin/client/ragflow_client.py+++ /dev/null@@ -1,2393 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-import base64-import json-import multiprocessing as mp-import time-import urllib.parse-from concurrent.futures import ProcessPoolExecutor, as_completed-from pathlib import Path-from typing import Any--from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5-from Cryptodome.PublicKey import RSA-from http_client import HttpClient-from lark import Tree-from user import encrypt_password, login_user--try:- from requests_toolbelt import MultipartEncoder-except Exception as e: # pragma: no cover - fallback without toolbelt- print(f"Fallback without belt: {e}")- MultipartEncoder = None---def encrypt(input_string):- pub = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----"- pub_key = RSA.importKey(pub)- cipher = Cipher_pkcs1_v1_5.new(pub_key)- cipher_text = cipher.encrypt(base64.b64encode(input_string.encode("utf-8")))- return base64.b64encode(cipher_text).decode("utf-8")---def _strip_tree_value(value):- if isinstance(value, Tree):- value = value.children[0]- return str(value).strip("'\"")---class RAGFlowClient:- def __init__(self, http_client: HttpClient, server_type: str):- self.http_client = http_client- self.server_type = server_type-- def login_user(self, command):- try:- response = self.http_client.request("GET", "/system/ping", use_api_base=True, auth_kind="web")- if response.status_code == 200 and response.content == b"pong":- pass- else:- print("Server is down")- return- except Exception as e:- print(str(e))- print("Can't access server for login (connection failed)")- return-- email: str = command["email"]- user_password: str = command.get("password")- if not user_password:- import getpass-- user_password = getpass.getpass("Password: ")- try:- token = login_user(self.http_client, self.server_type, email, user_password)- self.http_client.login_token = token- # Also store as api_key for API endpoint authentication- if self.server_type == "user":- self.http_client.api_key = token- print(f"Login user {email} successfully")- except Exception as e:- print(str(e))- print("Can't access server for login (connection failed)")-- def ping_server(self, command):- iterations = command.get("iterations", 1)- if iterations > 1:- response = self.http_client.request("GET", "/system/ping", use_api_base=True, auth_kind="web", iterations=iterations)- return response- else:- response = self.http_client.request("GET", "/system/ping", use_api_base=True, auth_kind="web")- if response.status_code == 200 and response.content == b"pong":- print("Server is alive")- else:- print("Server is down")- return None-- def register_user(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- username: str = command["user_name"]- nickname: str = command["nickname"]- password: str = command["password"]- enc_password = encrypt_password(password)- print(f"Register user: {nickname}, email: {username}, password: ******")- payload = {"email": username, "nickname": nickname, "password": enc_password}- response = self.http_client.request(method="POST", path="/users", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to register user {username}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to register user {username}, code: {response.status_code}, body: {response.text}")-- def list_services(self):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/services", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to get all services, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get all services, code: {response.status_code}, body: {response.text}")-- def show_service(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- service_id: int = command["number"]-- response = self.http_client.request("GET", f"/admin/services/{service_id}", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- res_data = res_json["data"]- if "status" in res_data and res_data["status"] == "alive":- print(f"Service {res_data['service_name']} is alive, ")- res_message = res_data["message"]- if res_message is None:- return- elif isinstance(res_message, str):- print(res_message)- else:- data = self._format_service_detail_table(res_message)- self._print_table_simple(data)- else:- print(f"Service {res_data['service_name']} is down, {res_data['message']}")- else:- print(f"Fail to show service, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to show service, code: {response.status_code}, body: {response.text}")-- def restart_service(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- # service_id: int = command["number"]- print("Restart service isn't implemented")-- def shutdown_service(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- # service_id: int = command["number"]- print("Shutdown service isn't implemented")-- def startup_service(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- # service_id: int = command["number"]- print("Startup service isn't implemented")-- def list_users(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/users", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to get all users, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get all users, code: {response.status_code}, body: {response.text}")-- def show_user(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Showing user: {user_name}")- response = self.http_client.request("GET", f"/admin/users/{user_name}", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- table_data = res_json["data"][0]- table_data.pop("avatar")- self._print_table_simple(table_data)- else:- print(f"Fail to get user {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get user {user_name}, code: {response.status_code}, body: {response.text}")-- def drop_user(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Drop user: {user_name}")- response = self.http_client.request("DELETE", f"/admin/users/{user_name}", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to drop user, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to drop user, code: {response.status_code}, body: {response.text}")-- def alter_user(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name: str = user_name_tree.children[0].strip("'\"")- password_tree: Tree = command["password"]- password: str = password_tree.children[0].strip("'\"")- print(f"Alter user: {user_name}, password: ******")- response = self.http_client.request("PUT", f"/admin/users/{user_name}/password", json_body={"new_password": encrypt_password(password)}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to alter password, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to alter password, code: {response.status_code}, body: {response.text}")-- def create_user(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name: str = user_name_tree.children[0].strip("'\"")- password_tree: Tree = command["password"]- password: str = password_tree.children[0].strip("'\"")- role: str = command["role"]- print(f"Create user: {user_name}, password: ******, role: {role}")- # enpass1 = encrypt(password)- enc_password = encrypt_password(password)- response = self.http_client.request(method="POST", path="/admin/users", json_body={"username": user_name, "password": enc_password, "role": role}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to create user {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to create user {user_name}, code: {response.status_code}, body: {response.text}")-- def activate_user(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name: str = user_name_tree.children[0].strip("'\"")- activate_tree: Tree = command["activate_status"]- activate_status: str = activate_tree.children[0].strip("'\"")- if activate_status.lower() in ["on", "off"]:- print(f"Alter user {user_name} activate status, turn {activate_status.lower()}.")- response = self.http_client.request("PUT", f"/admin/users/{user_name}/activate", json_body={"activate_status": activate_status}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to alter activate status, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to alter activate status, code: {response.status_code}, body: {response.text}")- else:- print(f"Unknown activate status: {activate_status}.")-- def grant_admin(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name: str = user_name_tree.children[0].strip("'\"")- response = self.http_client.request("PUT", f"/admin/users/{user_name}/admin", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to grant {user_name} admin authorization, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to grant {user_name} admin authorization, code: {response.status_code}, body: {response.text}")-- def revoke_admin(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name: str = user_name_tree.children[0].strip("'\"")- response = self.http_client.request("DELETE", f"/admin/users/{user_name}/admin", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to revoke {user_name} admin authorization, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to revoke {user_name} admin authorization, code: {response.status_code}, body: {response.text}")-- def create_role(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name: str = role_name_tree.children[0].strip("'\"")- desc_str: str = ""- if "description" in command and command["description"] is not None:- desc_tree: Tree = command["description"]- desc_str = desc_tree.children[0].strip("'\"")-- print(f"create role name: {role_name}, description: {desc_str}")- response = self.http_client.request("POST", "/admin/roles", json_body={"role_name": role_name, "description": desc_str}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to create role {role_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to create role {role_name}, code: {response.status_code}, body: {response.text}")-- def drop_role(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name: str = role_name_tree.children[0].strip("'\"")- print(f"drop role name: {role_name}")- response = self.http_client.request("DELETE", f"/admin/roles/{role_name}", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to drop role {role_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to drop role {role_name}, code: {response.status_code}, body: {response.text}")-- def alter_role(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name: str = role_name_tree.children[0].strip("'\"")- desc_tree: Tree = command["description"]- desc_str: str = desc_tree.children[0].strip("'\"")-- print(f"alter role name: {role_name}, description: {desc_str}")- response = self.http_client.request("PUT", f"/admin/roles/{role_name}", json_body={"description": desc_str}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to update role {role_name} with description: {desc_str}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to update role {role_name} with description: {desc_str}, code: {response.status_code}, body: {response.text}")-- def list_roles(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/roles", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list roles, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list roles, code: {response.status_code}, body: {response.text}")-- def show_role(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name: str = role_name_tree.children[0].strip("'\"")- print(f"show role: {role_name}")- response = self.http_client.request("GET", f"/admin/roles/{role_name}/permission", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list roles, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list roles, code: {response.status_code}, body: {response.text}")-- def grant_permission(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name_str: str = role_name_tree.children[0].strip("'\"")- resource_tree: Tree = command["resource"]- resource_str: str = resource_tree.children[0].strip("'\"")- action_tree_list: list = command["actions"]- actions: list = []- for action_tree in action_tree_list:- action_str: str = action_tree.children[0].strip("'\"")- actions.append(action_str)- print(f"grant role_name: {role_name_str}, resource: {resource_str}, actions: {actions}")- response = self.http_client.request("POST", f"/admin/roles/{role_name_str}/permission", json_body={"actions": actions, "resource": resource_str}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to grant role {role_name_str} with {actions} on {resource_str}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to grant role {role_name_str} with {actions} on {resource_str}, code: {response.status_code}, body: {response.text}")-- def revoke_permission(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name_str: str = role_name_tree.children[0].strip("'\"")- resource_tree: Tree = command["resource"]- resource_str: str = resource_tree.children[0].strip("'\"")- action_tree_list: list = command["actions"]- actions: list = []- for action_tree in action_tree_list:- action_str: str = action_tree.children[0].strip("'\"")- actions.append(action_str)- print(f"revoke role_name: {role_name_str}, resource: {resource_str}, actions: {actions}")- response = self.http_client.request("DELETE", f"/admin/roles/{role_name_str}/permission", json_body={"actions": actions, "resource": resource_str}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to revoke role {role_name_str} with {actions} on {resource_str}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to revoke role {role_name_str} with {actions} on {resource_str}, code: {response.status_code}, body: {response.text}")-- def alter_user_role(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- role_name_tree: Tree = command["role_name"]- role_name_str: str = role_name_tree.children[0].strip("'\"")- user_name_tree: Tree = command["user_name"]- user_name_str: str = user_name_tree.children[0].strip("'\"")- print(f"alter_user_role user_name: {user_name_str}, role_name: {role_name_str}")- response = self.http_client.request("PUT", f"/admin/users/{user_name_str}/role", json_body={"role_name": role_name_str}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to alter user: {user_name_str} to role {role_name_str}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to alter user: {user_name_str} to role {role_name_str}, code: {response.status_code}, body: {response.text}")-- def show_user_permission(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- user_name_tree: Tree = command["user_name"]- user_name_str: str = user_name_tree.children[0].strip("'\"")- print(f"show_user_permission user_name: {user_name_str}")- response = self.http_client.request("GET", f"/admin/users/{user_name_str}/permission", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to show user: {user_name_str} permission, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to show user: {user_name_str} permission, code: {response.status_code}, body: {response.text}")-- def generate_key(self, command: dict[str, Any]) -> None:- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Generating API key for user: {user_name}")- response = self.http_client.request("POST", f"/admin/users/{user_name}/keys", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json: dict[str, Any] = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Failed to generate key for user {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Failed to generate key for user {user_name}, code: {response.status_code}, body: {response.text}")-- def list_keys(self, command: dict[str, Any]) -> None:- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Listing API keys for user: {user_name}")- response = self.http_client.request("GET", f"/admin/users/{user_name}/keys", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json: dict[str, Any] = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Failed to list keys for user {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Failed to list keys for user {user_name}, code: {response.status_code}, body: {response.text}")-- def drop_key(self, command: dict[str, Any]) -> None:- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- key_tree: Tree = command["key"]- key: str = key_tree.children[0].strip("'\"")- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Dropping API key for user: {user_name}")- # URL encode the key to handle special characters- encoded_key: str = urllib.parse.quote(key, safe="")- response = self.http_client.request("DELETE", f"/admin/users/{user_name}/keys/{encoded_key}", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json: dict[str, Any] = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Failed to drop key for user {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Failed to drop key for user {user_name}, code: {response.status_code}, body: {response.text}")-- def set_variable(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- var_name = _strip_tree_value(command["var_name"])- var_value = _strip_tree_value(command["var_value"])- response = self.http_client.request("PUT", "/admin/variables", json_body={"var_name": var_name, "var_value": var_value}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(res_json["message"])- else:- print(f"Fail to set variable {var_name} to {var_value}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to set variable {var_name} to {var_value}, code: {response.status_code}, body: {response.text}")-- def show_variable(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- var_name = _strip_tree_value(command["var_name"])- response = self.http_client.request(method="GET", path="/admin/variables", json_body={"var_name": var_name}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to get variable {var_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get variable {var_name}, code: {response.status_code}, body: {response.text}")-- def list_variables(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/variables", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list variables, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list variables, code: {response.status_code}, body: {response.text}")-- def list_configs(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/configs", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list variables, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list variables, code: {response.status_code}, body: {response.text}")-- def list_environments(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- response = self.http_client.request("GET", "/admin/environments", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list variables, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list variables, code: {response.status_code}, body: {response.text}")-- def show_fingerprint(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")- response = self.http_client.request("GET", "/admin/fingerprint", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to show fingerprint, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to show fingerprint, code: {response.status_code}, body: {response.text}")-- def set_license(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")- license = command["license"]- response = self.http_client.request("POST", "/admin/license", json_body={"license": license}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print("Set license successfully")- else:- print(f"Fail to set license, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to set license, code: {response.status_code}, body: {response.text}")-- def set_license_config(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")- value1 = command["value1"]- value2 = command["value2"]- response = self.http_client.request("POST", "/admin/license/config", json_body={"value1": value1, "value2": value2}, use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print("Set license successfully")- else:- print(f"Fail to set license, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to set license, code: {response.status_code}, body: {response.text}")-- def show_license(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")- response = self.http_client.request("GET", "/admin/license", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to show license, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to show license, code: {response.status_code}, body: {response.text}")-- def check_license(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")- response = self.http_client.request("GET", "/admin/license?check=true", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 320:- print(res_json["data"])- else:- print(f"Invalid, code: {res_json['code']}, message: {res_json['data']}")- else:- print(f"Fail to check license, code: {response.status_code}, body: {response.text}")-- def list_server_configs(self, command):- """List server configs by calling /system/configs API and flattening the JSON response."""- response = self.http_client.request("GET", "/system/configs", use_api_base=False, auth_kind="web")- if response.status_code != 200:- print(f"Fail to list server configs, code: {response.status_code}, body: {response.text}")- return- res_json = response.json()- if res_json.get("code") != 0:- print(f"Fail to list server configs, code: {res_json.get('code')}, message: {res_json.get('message')}")- return-- data = res_json.get("data", {})- if not data:- print("No server configs found")- return-- # Flatten nested JSON with a.b.c notation- def flatten(obj, parent_key=""):- items = []- if isinstance(obj, dict):- for k, v in obj.items():- new_key = f"{parent_key}.{k}" if parent_key else k- if isinstance(v, (dict, list)) and v:- items.extend(flatten(v, new_key))- else:- items.append({"name": new_key, "value": v})- elif isinstance(obj, list):- for i, v in enumerate(obj):- new_key = f"{parent_key}[{i}]"- if isinstance(v, (dict, list)) and v:- items.extend(flatten(v, new_key))- else:- items.append({"name": new_key, "value": v})- return items-- # Reconstruct flattened data and print using _print_table_simple- flattened = flatten(data)- self._print_table_simple(flattened)-- def handle_list_datasets(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Listing all datasets of user: {user_name}")-- response = self.http_client.request("GET", f"/admin/users/{user_name}/datasets", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- table_data = res_json["data"]- for t in table_data:- t.pop("avatar")- self._print_table_simple(table_data)- else:- print(f"Fail to get all datasets of {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get all datasets of {user_name}, code: {response.status_code}, body: {response.text}")-- def handle_list_agents(self, command):- if self.server_type != "admin":- print("This command is only allowed in ADMIN mode")-- username_tree: Tree = command["user_name"]- user_name: str = username_tree.children[0].strip("'\"")- print(f"Listing all agents of user: {user_name}")- response = self.http_client.request("GET", f"/admin/users/{user_name}/agents", use_api_base=True, auth_kind="admin")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- table_data = res_json["data"]- for t in table_data:- t.pop("avatar")- self._print_table_simple(table_data)- else:- print(f"Fail to get all agents of {user_name}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get all agents of {user_name}, code: {response.status_code}, body: {response.text}")-- def show_current_user(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- print("show current user")-- def create_model_provider(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- provider_name: str = command["provider_name"]- api_key: str = command["provider_key"]-- # Step 1: Add provider- provider_payload = {"provider_name": provider_name}- provider_response = self.http_client.request("PUT", "/providers", json_body=provider_payload, use_api_base=True, auth_kind="web")- if provider_response.status_code == 200:- provider_res = provider_response.json()- if provider_res.get("code") == 0:- print(f"Success to add provider {provider_name}")- else:- msg = provider_res.get("message", "")- if "duplicated" in msg.lower() or "already exist" in msg.lower():- print(f"Note: provider {provider_name} already exists, continuing to add instance")- else:- print(f"Fail to add provider {provider_name}, code: {provider_res.get('code')}, message: {msg}")- return-- else:- print(f"Fail to add provider {provider_name}, code: {provider_response.status_code}, body: {provider_response.text}")- return- # Step 2: Add instance- instance_payload = {"instance_name": "default", "api_key": api_key, "region": "default", "base_url": ""}- instance_response = self.http_client.request("POST", f"/providers/{provider_name}/instances", json_body=instance_payload, use_api_base=True, auth_kind="web")- if instance_response.status_code == 200:- instance_res = instance_response.json()- if instance_res.get("code") == 0:- print(f"Success to add instance for provider {provider_name}")- else:- msg = instance_res.get("message", "")- if "already exist" in msg.lower():- print(f"Note: instance for provider {provider_name} already exists, skipping")- else:- print(f"Fail to add instance for provider {provider_name}, code: {instance_res.get('code')}, message: {msg}")-- else:- print(f"Fail to add instance for provider {provider_name}, code: {instance_response.status_code}, body: {instance_response.text}")-- def drop_model_provider(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- provider_name: str = command["provider_name"]- response = self.http_client.request("DELETE", f"/providers/{provider_name}", use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to drop model provider {provider_name}")- else:- print(f"Fail to drop model provider {provider_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to drop model provider {provider_name}, code: {response.status_code}, body: {response.text}")-- # Mapping from legacy model_type keys to API model_type values- _MODEL_TYPE_MAP = {- "llm_id": "chat",- "embd_id": "embedding",- "img2txt_id": "vision",- "reranker_id": "rerank",- "asr_id": "asr",- "tts_id": "tts",- }-- def set_default_model(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- model_type_key: str = command["model_type"]- model_id: str = command["model_id"]-- model_type = self._MODEL_TYPE_MAP.get(model_type_key)- if model_type is None:- print(f"Unknown model type: {model_type_key}")- return-- model_name, model_instance, model_provider = self._parse_model_id(model_id)-- payload = {- "model_provider": model_provider,- "model_instance": model_instance,- "model_type": model_type,- "model_name": model_name,- }- response = self.http_client.request("PATCH", "/models/default", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to set default {model_type} to {model_id}")- else:- print(f"Fail to set default {model_type}, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to set default {model_type}, code: {response.status_code}, body: {response.text}")-- def reset_default_model(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- model_type_key: str = command["model_type"]- model_type = self._MODEL_TYPE_MAP.get(model_type_key)- if model_type is None:- print(f"Unknown model type: {model_type_key}")- return-- payload = {"model_type": model_type}- response = self.http_client.request("PATCH", "/models/default", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to reset default {model_type}")- else:- print(f"Fail to reset default {model_type}, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to reset default {model_type}, code: {response.status_code}, body: {response.text}")-- @staticmethod- def _parse_model_id(model_id: str):- """Parse model_id into (model_name, model_instance, model_provider).-- Accepted formats:- - model_name@instance@provider -> (model_name, instance, provider)- - model_name@provider -> (model_name, "default", provider)- - model_name -> (model_name, "default", "")- """- parts = model_id.split("@")- if len(parts) >= 3:- return parts[0], parts[1], parts[-1]- elif len(parts) == 2:- return parts[0], "default", parts[1]- else:- return model_id, "default", ""-- def list_user_datasets(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- iterations = command.get("iterations", 1)- if iterations > 1:- response = self.http_client.request("GET", "/datasets", use_api_base=True, auth_kind="web", iterations=iterations)- return response- else:- response = self.http_client.request("GET", "/datasets", use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list datasets, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list datasets, code: {response.status_code}, body: {response.text}")- return None-- def create_user_dataset(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- payload = {"name": command["dataset_name"], "embedding_model": command["embedding"]}- if "parser_id" in command:- payload["chunk_method"] = command["parser"]- if "pipeline" in command:- payload["pipeline_id"] = command["pipeline"]- response = self.http_client.request("POST", "/datasets", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to create datasets, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to create datasets, code: {response.status_code}, body: {response.text}")-- def drop_user_dataset(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return- payload = {"ids": [dataset_id]}- response = self.http_client.request("DELETE", "/datasets", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Drop dataset {dataset_name} successfully")- else:- print(f"Fail to drop datasets, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to drop datasets, code: {response.status_code}, body: {response.text}")-- def list_user_dataset_files(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- res_json = self._list_documents(dataset_name, dataset_id)- if res_json is None:- return- self._print_table_simple(res_json)-- def list_user_dataset_documents(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- docs = self._list_documents(dataset_name, dataset_id)- if docs is None:- return-- if not docs:- print(f"No documents found in dataset {dataset_name}")- return-- print(f"Documents in dataset: {dataset_name}")- print("-" * 60)- # Select key fields for display- display_docs = []- for doc in docs:- meta_fields = doc.get("meta_fields", {})- # Convert meta_fields dict to string for display- meta_fields_str = ""- if meta_fields:- meta_fields_str = str(meta_fields)- display_doc = {- "name": doc.get("name", ""),- "id": doc.get("id", ""),- "size": doc.get("size", 0),- "status": doc.get("status", ""),- "created_at": doc.get("created_at", ""),- }- if meta_fields_str:- display_doc["meta_fields"] = meta_fields_str- display_docs.append(display_doc)- self._print_table_simple(display_docs)-- def list_user_datasets_metadata(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- dataset_names = command_dict["dataset_names"]- valid_datasets = []- for dataset_name in dataset_names:- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- print(f"Dataset not found: {dataset_name}")- continue- valid_datasets.append((dataset_name, dataset_id))-- if not valid_datasets:- print("No valid datasets found")- return-- dataset_ids = [dataset_id for _, dataset_id in valid_datasets]- kb_ids_param = ",".join(dataset_ids)- response = self.http_client.request("GET", f"/kb/get_meta?kb_ids={kb_ids_param}", use_api_base=False, auth_kind="web")- if response.status_code != 200:- print(f"Fail to get metadata, code: {response.status_code}, body: {response.text}")- return-- res_json = response.json()- if res_json.get("code") != 0:- print(f"Fail to get metadata, code: {res_json.get('code')}, message: {res_json.get('message')}")- return- meta = res_json.get("data", {})- if not meta:- print("No metadata found")- return-- table_data = []- for field_name, values_dict in meta.items():- for value, docs in values_dict.items():- table_data.append({"field": field_name, "value": value, "doc_ids": ", ".join(docs)})- self._print_table_simple(table_data)-- def list_user_documents_metadata_summary(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- dataset_name = command_dict["dataset_name"]- doc_ids = command_dict.get("document_ids", [])-- kb_id = self._get_dataset_id(dataset_name)- if kb_id is None:- return-- payload = {"kb_id": kb_id}- if doc_ids:- payload["doc_ids"] = doc_ids- response = self.http_client.request("POST", "/document/metadata/summary", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- summary = res_json.get("data", {}).get("summary", {})- if not summary:- if doc_ids:- print(f"No metadata summary found for documents: {', '.join(doc_ids)}")- else:- print(f"No metadata summary found in dataset {dataset_name}")- return- if doc_ids:- print(f"Metadata summary for document(s): {', '.join(doc_ids)}")- else:- print(f"Metadata summary for all documents in dataset: {dataset_name}")- print("-" * 60)- for field_name, field_info in summary.items():- field_type = field_info.get("type", "unknown")- values = field_info.get("values", [])- print(f"\nField: {field_name} (type: {field_type})")- print(f" Total unique values: {len(values)}")- if values:- print(" Values:")- for value, count in values:- print(f" {value}: {count}")- else:- print(f"Fail to get metadata summary, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to get metadata summary, code: {response.status_code}, body: {response.text}")-- def list_user_agents(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- response = self.http_client.request("GET", "/canvas/list", use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"])- else:- print(f"Fail to list datasets, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list datasets, code: {response.status_code}, body: {response.text}")-- def list_user_chats(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- res_json = self._list_chats(command)- if res_json is None:- return None- if "iterations" in command:- # for benchmark- return res_json- self._print_table_simple(res_json)-- def create_user_chat(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- chat_name = command["chat_name"]- default_models = self._get_default_models() or {}- payload = {- "name": chat_name,- "description": "",- "icon": "",- "dataset_ids": [],- "llm_setting": {},- "prompt_config": {- "empty_response": "",- "prologue": "Hi! I'm your assistant. What can I do for you?",- "quote": True,- "keyword": False,- "tts": False,- "system": 'You are an intelligent assistant. Your primary function is to answer questions based strictly on the provided knowledge base.\n\n **Essential Rules:**\n - Your answer must be derived **solely** from this knowledge base: `{knowledge}`.\n - **When information is available**: Summarize the content to give a detailed answer.\n - **When information is unavailable**: Your response must contain this exact sentence: "The answer you are looking for is not found in the knowledge base!"\n - **Always consider** the entire conversation history.',- "refine_multiturn": False,- "use_kg": False,- "reasoning": False,- "parameters": [{"key": "knowledge", "optional": False}],- "toc_enhance": False,- },- "similarity_threshold": 0.2,- "top_n": 8,- "top_k": 1024,- "vector_similarity_weight": 0.3,- "rerank_id": default_models.get("rerank_id", ""),- }- if default_models.get("llm_id"):- payload["llm_id"] = default_models["llm_id"]-- response = self.http_client.request(- "POST",- "/chats",- json_body=payload,- use_api_base=True,- auth_kind="web",- )- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Success to create chat: {chat_name}")- else:- print(f"Fail to create chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to create chat {chat_name}, code: {response.status_code}, body: {response.text}")-- def create_dataset_table(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- dataset_name = command["dataset_name"]- vector_size = command.get("vector_size")- if not vector_size:- print("vector_size is required")- return- # Get dataset ID by name- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return- # Build payload- payload = {"kb_id": dataset_id, "vector_size": vector_size}- # Call API- response = self.http_client.request("POST", "/kb/doc_engine_table", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to create table for dataset: {dataset_name}")- else:- print(f"Fail to create table for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to create table for dataset {dataset_name}, code: {response.status_code}, body: {response.text}")-- def drop_dataset_table(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- dataset_name = command["dataset_name"]- # Get dataset ID by name- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return- # Call API to delete table- payload = {"kb_id": dataset_id}- response = self.http_client.request("DELETE", "/kb/doc_engine_table", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to drop table for dataset: {dataset_name}")- else:- print(f"Fail to drop table for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to drop table for dataset {dataset_name}, code: {response.status_code}, body: {response.text}")-- def create_metadata_table(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- # Call API to create metadata table- response = self.http_client.request("POST", "/tenant/doc_engine_metadata_table", use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print("Success to create metadata table")- else:- print(f"Fail to create metadata table, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to create metadata table, code: {response.status_code}, body: {response.text}")-- def drop_metadata_table(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return- # Call API to delete metadata table- response = self.http_client.request("DELETE", "/tenant/doc_engine_metadata_table", use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print("Success to drop metadata table")- else:- print(f"Fail to drop metadata table, code: {res_json.get('code')}, message: {res_json.get('message')}")-- else:- print(f"Fail to drop metadata table, code: {response.status_code}, body: {response.text}")-- def drop_user_chat(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- chat_name = command["chat_name"]- res_json = self._list_chats(command)- to_drop_chat_ids = []- for elem in res_json:- if elem["name"] == chat_name:- to_drop_chat_ids.append(elem["id"])- payload = {"ids": to_drop_chat_ids}- response = self.http_client.request(- "DELETE",- "/chats",- json_body=payload,- use_api_base=True,- auth_kind="web",- )- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Success to drop chat: {chat_name}")- else:- print(f"Fail to drop chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to drop chat {chat_name}, code: {response.status_code}, body: {response.text}")-- def _get_chat_id_by_name(self, chat_name):- """Get chat (dialog) ID by name."""- res_json = self._list_chats({})- if res_json is None:- return None- for elem in res_json:- if elem["name"] == chat_name:- return elem["id"]- print(f"Chat '{chat_name}' not found")- return None-- def _list_chat_sessions(self, dialog_id):- """List all sessions (conversations) for a given dialog."""- response = self.http_client.request("GET", f"/chats/{dialog_id}/conversations", use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- return res_json["data"]- else:- print(f"Fail to list chat sessions, code: {res_json['code']}, message: {res_json['message']}")- return None-- else:- print(f"Fail to list chat sessions, code: {response.status_code}, body: {response.text}")-- def create_chat_session(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- chat_name = command["chat_name"]- dialog_id = self._get_chat_id_by_name(chat_name)- if dialog_id is None:- return- payload = {"name": "New conversation"}- response = self.http_client.request("POST", f"/chats/{dialog_id}/conversations", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Success to create chat session for chat: {chat_name}")- else:- print(f"Fail to create chat session for chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to create chat session for chat {chat_name}, code: {response.status_code}, body: {response.text}")-- def drop_chat_session(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- chat_name = command["chat_name"]- session_id = command["session_id"]- dialog_id = self._get_chat_id_by_name(chat_name)- if dialog_id is None:- return- sessions = self._list_chat_sessions(dialog_id)- if sessions is None:- return- to_drop_session_ids = []- for session in sessions:- if session["id"] == session_id:- to_drop_session_ids.append(session["id"])- if not to_drop_session_ids:- print(f"Chat session '{session_id}' not found in chat '{chat_name}'")- return- payload = {"ids": to_drop_session_ids}- response = self.http_client.request("DELETE", f"/chats/{dialog_id}/conversations", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Success to drop chat session '{session_id}' from chat: {chat_name}")- else:- print(f"Fail to drop chat session '{session_id}' from chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to drop chat session '{session_id}' from chat {chat_name}, code: {response.status_code}, body: {response.text}")-- def list_chat_sessions(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- chat_name = command["chat_name"]- dialog_id = self._get_chat_id_by_name(chat_name)- if dialog_id is None:- return- sessions = self._list_chat_sessions(dialog_id)- if sessions is None:- return- # Add chat_name to each session for display- for session in sessions:- session["chat_name"] = chat_name- if "iterations" in command:- # for benchmark- return sessions- self._print_table_simple(sessions)-- def chat_on_session(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")- message = command["message"]- session_id = command["session_id"]-- # Prepare payload for completion API- # Note: stream parameter is not sent, server defaults to stream=True- payload = {"session_id": session_id, "messages": [{"role": "user", "content": message}]}-- response = self.http_client.request("POST", "/chat/completions", json_body=payload, use_api_base=True, auth_kind="web", stream=True)-- if response.status_code != 200:- print(f"Fail to chat on session, status code: {response.status_code}")- return-- print("Assistant: ", end="", flush=True)- full_answer = ""- for line in response.iter_lines():- if not line:- continue- line_str = line.decode("utf-8")- if not line_str.startswith("data:"):- continue- data_str = line_str[5:].strip()- if data_str == "[DONE]":- break- try:- data_json = json.loads(data_str)- if data_json.get("code") != 0:- print(f"\nFail to chat on session, code: {data_json.get('code')}, message: {data_json.get('message', '')}")- return- # Check if it's the final message- if data_json.get("data") is True:- break- answer = data_json.get("data", {}).get("answer", "")- if answer:- print(answer, end="", flush=True)- full_answer += answer- except json.JSONDecodeError:- continue- print() # Final newline-- def list_user_model_providers(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- response = self.http_client.request("GET", "/llm/my_llms", use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- new_input = []- for key, value in res_json["data"].items():- new_input.append({"model provider": key, "models": value})- self._print_table_simple(new_input)- else:- print(f"Fail to list model provider, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list model provider, code: {response.status_code}, body: {response.text}")-- def list_user_default_models(self, command):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- res_json = self._get_default_models()- if res_json is None:- return- else:- new_input = []- for key, value in res_json.items():- if key == "asr_id" and value != "":- new_input.append({"model_category": "ASR", "model_name": value})- elif key == "embd_id" and value != "":- new_input.append({"model_category": "Embedding", "model_name": value})- elif key == "llm_id" and value != "":- new_input.append({"model_category": "LLM", "model_name": value})- elif key == "rerank_id" and value != "":- new_input.append({"model_category": "Reranker", "model_name": value})- elif key == "tts_id" and value != "":- new_input.append({"model_category": "TTS", "model_name": value})- elif key == "img2txt_id" and value != "":- new_input.append({"model_category": "VLM", "model_name": value})- else:- continue- self._print_table_simple(new_input)-- def parse_dataset_docs(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- res_json = self._list_documents(dataset_name, dataset_id)- if res_json is None:- return-- document_names = command_dict["document_names"]- document_ids = []- to_parse_doc_names = []- for doc in res_json:- doc_name = doc["name"]- if doc_name in document_names:- document_ids.append(doc["id"])- document_names.remove(doc_name)- to_parse_doc_names.append(doc_name)-- if len(document_ids) == 0:- print(f"No documents found in {dataset_name}")- return-- if len(document_names) != 0:- print(f"Documents {document_names} not found in {dataset_name}")-- payload = {"doc_ids": document_ids, "run": 1}- response = self.http_client.request("POST", "/documents/ingest", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- print(f"Success to parse {to_parse_doc_names} of {dataset_name}")- else:- print(f"Fail to parse documents {res_json.get('data', {}).get('docs')}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to parse documents, code: {response.status_code}, body: {response.text}")-- def parse_dataset(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- res_json = self._list_documents(dataset_name, dataset_id)- if res_json is None:- return- document_ids = []- for doc in res_json:- document_ids.append(doc["id"])-- payload = {"doc_ids": document_ids, "run": 1}- response = self.http_client.request("POST", "/documents/ingest", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- pass- else:- print(f"Fail to parse dataset {dataset_name}, code: {res_json['code']}, message: {res_json['message']}")-- else:- print(f"Fail to parse dataset {dataset_name}, code: {response.status_code}, body: {response.text}")- if command_dict["method"] == "async":- print(f"Success to start parse dataset {dataset_name}")- return- else:- print(f"Start to parse dataset {dataset_name}, please wait...")- if self._wait_parse_done(dataset_name, dataset_id):- print(f"Success to parse dataset {dataset_name}")- else:- print(f"Parse dataset {dataset_name} timeout")-- def import_docs_into_dataset(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- document_paths = command_dict["document_paths"]- paths = [Path(p) for p in document_paths]-- fields = []- file_handles = []- try:- for path in paths:- fh = path.open("rb")- fields.append(("file", (path.name, fh)))- file_handles.append(fh)- fields.append(("kb_id", dataset_id))- encoder = MultipartEncoder(fields=fields)- headers = {"Content-Type": encoder.content_type}- response = self.http_client.request(- "POST", f"/datasets/{dataset_id}/documents?return_raw_files=true", headers=headers, data=encoder, json_body=None, params=None, stream=False, auth_kind="web", use_api_base=True- )- if response.status_code == 200:- res = response.json()- if res["code"] == 0:- print(f"Success to import documents into dataset {dataset_name}")- else:- print(f"Fail to import documents: code: {res['code']}, message: {res['message']}")- else:- print(f"Fail to import documents: code: {response.status_code}, body: {response.text}")- except Exception as exc:- print(f"Fail to import document into dataset: {dataset_name}, error: {exc}")- finally:- for fh in file_handles:- fh.close()-- def search_on_datasets(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")-- dataset_names = command_dict["datasets"]- dataset_ids = []- for dataset_name in dataset_names:- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return- dataset_ids.append(dataset_id)-- payload = {- "question": command_dict["question"],- "dataset_ids": dataset_ids,- "similarity_threshold": 0.2,- "vector_similarity_weight": 0.3,- # "top_k": 1024,- # "kb_id": command_dict["datasets"][0],- }- iterations = command_dict.get("iterations", 1)- if iterations > 1:- response = self.http_client.request("POST", "/retrieval", json_body=payload, use_api_base=True, auth_kind="web", iterations=iterations)- return response- else:- response = self.http_client.request("POST", "/retrieval", json_body=payload, use_api_base=True, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_table_simple(res_json["data"]["chunks"])- else:- print(f"Fail to search datasets: {dataset_names}, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to search datasets: {dataset_names}, code: {response.status_code}, body: {response.text}")-- def get_chunk(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- chunk_id = command_dict["chunk_id"]- response = self.http_client.request("GET", f"/chunk/get?chunk_id={chunk_id}", use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- self._print_key_value(res_json["data"])- else:- print(f"Fail to get chunk, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to get chunk, code: {response.status_code}, body: {response.text}")-- def update_chunk(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- chunk_id = command_dict["chunk_id"]- dataset_name = command_dict["dataset_name"]- json_body_str = command_dict["json_body"]-- # Get dataset_id from dataset_name- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- return-- # Get doc_id from chunk_id via GET /chunk/get- response = self.http_client.request("GET", f"/chunk/get?chunk_id={chunk_id}", use_api_base=False, auth_kind="web")- if response.status_code != 200:- print(f"Fail to get chunk info, code: {response.status_code}, body: {response.text}")- return-- res_json = response.json()- doc_id = None- if res_json.get("code") == 0 and res_json.get("data"):- doc_id = res_json["data"].get("doc_id")-- if not doc_id:- print(f"Could not find document_id for chunk {chunk_id}")- return-- # Parse json_body- try:- payload = json.loads(json_body_str)- except json.JSONDecodeError as e:- print(f"Invalid JSON body: {e}")- return-- # Add IDs to payload- payload["dataset_id"] = dataset_id- payload["document_id"] = doc_id- payload["chunk_id"] = chunk_id-- # Call POST /v1/chunk/update- response = self.http_client.request("POST", "/chunk/update", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to update chunk: {chunk_id}")- else:- print(f"Fail to update chunk, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to update chunk, code: {response.status_code}, body: {response.text}")-- def _get_documents_by_ids(self, ids: list[str]):- response = self.http_client.request("POST", "/document/infos", json_body={"doc_ids": ids}, use_api_base=False, auth_kind="web")-- if response.status_code != 200:- return f"Fail to get document info, HTTP {response.status_code}", None-- res_json = response.json()- if res_json.get("code") != 0:- return f"Fail to get document info: {res_json.get('message')}", None-- docs = res_json.get("data", [])- if not docs:- return f"Document not found: {ids}", None-- return None, docs-- def set_metadata(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- doc_id = command_dict["doc_id"]- meta_json_str = command_dict["meta"]-- # Parse JSON string to dict- import json-- try:- meta_fields = json.loads(meta_json_str)- except json.JSONDecodeError as e:- print(f"Invalid JSON format: {e}")- return-- # Step 1: Get document info to find kb_id (dataset_id)- doc_error_msg, docs = self._get_documents_by_ids([doc_id])- if doc_error_msg:- print(doc_error_msg)- return-- if len(docs) == 0:- print(f"no document found for {doc_id}")- return-- dataset_id = docs[0].get("dataset_id") or docs[0].get("kb_id")- if not dataset_id:- print(f"Dataset ID not found for document: {doc_id}")- return-- # Send meta as JSON string- payload = {- "meta_fields": meta_fields,- }-- response = self.http_client.request("PATCH", f"/datasets/{dataset_id}/documents/{doc_id}", json_body=payload, use_api_base=True, auth_kind="web")-- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to set metadata for document: {doc_id}")- else:- print(f"Fail to set metadata, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to set metadata, code: {response.status_code}, body: {response.text}")-- def generate_nav_for_dataset(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- dataset_name = command_dict["dataset_id"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- print(f"Dataset not found: {dataset_name}")- return-- response = self.http_client.request(- "POST",- f"/datasets/{dataset_id}/navigation",- json_body={},- use_api_base=True,- auth_kind="web",- )- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- data = res_json.get("data", {})- print(f"Navigation tree created: deleted={data.get('deleted', 0)}, upserted={data.get('upserted', 0)}")- else:- print(f"Fail to generate navigation, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to generate navigation, code: {response.status_code}, body: {response.text}")-- def navigation_search(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- query = command_dict["query"]- dataset_name = command_dict["dataset_id"]- mode = command_dict["mode"]- topk = command_dict.get("topk", None)-- # Try name lookup; fall back to using the value directly as an ID.- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- dataset_id = dataset_name-- valid_modes = {"nav_doc", "nav_cluster", "navigation_tree", "chunk", "all"}- if mode not in valid_modes:- print(f"Invalid mode: {mode}, expected one of {valid_modes}")- return-- url = f"/datasets/{dataset_id}/navigation/search?q={query}&mode={mode}"- if topk is not None:- url += f"&top_k={topk}"- response = self.http_client.request(- "GET",- url,- use_api_base=True,- auth_kind="web",- )- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- data = res_json.get("data", {})- items = data.get("items", [])- print(f"Found {len(items)} result(s) for mode '{data.get('mode', mode)}':")- for i, item in enumerate(items, 1):- print(f" [{i}] doc_id: {item.get('doc_id', '')} score: {item.get('score', 0):.4f}")- else:- print(f"Search failed, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Search failed, code: {response.status_code}, body: {response.text}")-- def remove_tags(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- dataset_name = command_dict["dataset_name"]- dataset_id = self._get_dataset_id(dataset_name)- if dataset_id is None:- print(f"Dataset not found: {dataset_name}")- return-- tags = command_dict["tags"]-- payload = {- "tags": tags,- }-- response = self.http_client.request("POST", f"/kb/{dataset_id}/rm_tags", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- print(f"Success to remove tags from dataset: {dataset_name}")- else:- print(f"Fail to remove tags, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to remove tags, code: {response.status_code}, body: {response.text}")-- def remove_chunks(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- doc_id = command_dict["doc_id"]- payload = {"doc_id": doc_id}-- if command_dict.get("delete_all"):- payload["delete_all"] = True- elif command_dict.get("chunk_ids"):- payload["chunk_ids"] = command_dict["chunk_ids"]-- response = self.http_client.request("POST", "/chunk/rm", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json.get("code") == 0:- deleted_count = res_json.get("data", 0)- print(f"Success to remove chunks from document {doc_id}: {deleted_count} chunks deleted")- else:- print(f"Fail to remove chunks, code: {res_json.get('code')}, message: {res_json.get('message')}")- else:- print(f"Fail to remove chunks, code: {response.status_code}, body: {response.text}")-- def list_chunks(self, command_dict):- if self.server_type != "user":- print("This command is only allowed in USER mode")- return-- doc_id = command_dict["doc_id"]- payload = {- "doc_id": doc_id,- }-- # Add optional parameters (only if explicitly provided)- if "page" in command_dict:- payload["page"] = command_dict["page"]- if "size" in command_dict:- payload["size"] = command_dict["size"]- if command_dict.get("keywords"):- payload["keywords"] = command_dict["keywords"]- if "available_int" in command_dict:- payload["available_int"] = command_dict["available_int"]-- response = self.http_client.request("POST", "/chunk/list", json_body=payload, use_api_base=False, auth_kind="web")- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- chunks = res_json["data"]["chunks"]- if chunks:- for i, chunk in enumerate(chunks):- print(f"\n--- Chunk {i + 1} ---")- for key, value in chunk.items():- print(f" {key}: {value}")- else:- print("No chunks found")- else:- print(f"Fail to list chunks, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to list chunks, code: {response.status_code}, body: {response.text}")-- def show_version(self, command):- if self.server_type == "admin":- response = self.http_client.request("GET", "/admin/version", use_api_base=True, auth_kind="admin")- else:- response = self.http_client.request("GET", "/system/version", use_api_base=True, auth_kind="admin")-- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- if self.server_type == "admin":- self._print_table_simple(res_json["data"])- else:- self._print_table_simple({"version": res_json["data"]})- else:- print(f"Fail to show version, code: {res_json['code']}, message: {res_json['message']}")- else:- print(f"Fail to show version, code: {response.status_code}, body: {response.text}")-- def _wait_parse_done(self, dataset_name: str, dataset_id: str):- start = time.monotonic()- while True:- docs = self._list_documents(dataset_name, dataset_id)- if docs is None:- return False- all_done = True- for doc in docs:- if doc.get("run") != "DONE":- print(f"Document {doc['name']} is not done, status: {doc.get('run')}")- all_done = False- break- if all_done:- return True- if time.monotonic() - start > 60:- return False- time.sleep(0.5)-- def _list_documents(self, dataset_name: str, dataset_id: str):- # Use the new RESTful API: GET /api/v1/datasets/<dataset_id>/documents- response = self.http_client.request("GET", f"/datasets/{dataset_id}/documents", use_api_base=True, auth_kind="web")- if response.status_code != 200:- print(f"Fail to list files from dataset {dataset_name}, code: {response.status_code}, body: {response.text}")- return None- res_json = response.json()- if res_json.get("code") != 0:- print(f"Fail to list files from dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")- return None- return res_json["data"]["docs"]-- def _get_dataset_id(self, dataset_name: str):- response = self.http_client.request("GET", "/datasets", use_api_base=True, auth_kind="web")- if response.status_code != 200:- print(f"Fail to list datasets, code: {response.status_code}, body: {response.text}")- return None-- res_json = response.json()- if res_json.get("code") != 0:- print(f"Fail to list datasets, code: {res_json.get('code')}, message: {res_json.get('message')}")- return None- dataset_list = res_json["data"]- dataset_id: str = ""- for dataset in dataset_list:- if dataset["name"] == dataset_name:- dataset_id = dataset["id"]-- if dataset_id == "":- print(f"Dataset {dataset_name} not found")- return None- return dataset_id-- def _list_chats(self, command):- iterations = command.get("iterations", 1)- if iterations > 1:- response = self.http_client.request(- "GET",- "/chats",- use_api_base=True,- auth_kind="web",- iterations=iterations,- )- return response- else:- response = self.http_client.request(- "GET",- "/chats",- use_api_base=True,- auth_kind="web",- iterations=iterations,- )- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- return res_json["data"]["chats"]- else:- print(f"Fail to list chats, code: {res_json['code']}, message: {res_json['message']}")- return None-- else:- print(f"Fail to list chats, code: {response.status_code}, body: {response.text}")-- def _format_service_detail_table(self, data):- if isinstance(data, list):- return data- if not all([isinstance(v, list) for v in data.values()]):- # normal table- return data- # handle task_executor heartbeats map, for example {'name': [{'done': 2, 'now': timestamp1}, {'done': 3, 'now': timestamp2}]- task_executor_list = []- for k, v in data.items():- # display latest status- heartbeats = sorted(v, key=lambda x: x["now"], reverse=True)- task_executor_list.append(- {- "task_executor_name": k,- **heartbeats[0],- }- if heartbeats- else {"task_executor_name": k}- )- return task_executor_list-- def _print_table_simple(self, data):- if not data:- print("No data to print")- return- if isinstance(data, dict):- # handle single row data- data = [data]-- columns = list(set().union(*(d.keys() for d in data)))- columns.sort()- col_widths = {}-- def get_string_width(text):- half_width_chars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\t\n\r"- width = 0- for char in text:- if char in half_width_chars:- width += 1- else:- width += 2- return width-- for col in columns:- max_width = get_string_width(str(col))- for item in data:- value_len = get_string_width(str(item.get(col, "")))- max_width = max(max_width, value_len)- col_widths[col] = max(2, max_width)-- # Generate delimiter- separator = "+" + "+".join(["-" * (col_widths[col] + 2) for col in columns]) + "+"-- # Print header- print(separator)- header = "|" + "|".join([f" {col:<{col_widths[col]}} " for col in columns]) + "|"- print(header)- print(separator)-- # Print data- for item in data:- row = "|"- for col in columns:- value = str(item.get(col, ""))- if get_string_width(value) > col_widths[col]:- value = value[: col_widths[col] - 3] + "..."- row += f" {value:<{col_widths[col] - (get_string_width(value) - len(value))}} |"- print(row)-- print(separator)-- def _print_key_value(self, data: dict):- """Print data as key-value pairs (one per line)"""- if not data:- print("No data to print")- return- for key, value in data.items():- print(f"{key}: {value}")---def run_command(client: RAGFlowClient, command_dict: dict):- command_type = command_dict["type"]-- match command_type:- case "benchmark":- run_benchmark(client, command_dict)- case "login_user":- client.login_user(command_dict)- case "ping_server":- return client.ping_server(command_dict)- case "register_user":- client.register_user(command_dict)- case "list_services":- client.list_services()- case "show_service":- client.show_service(command_dict)- case "restart_service":- client.restart_service(command_dict)- case "shutdown_service":- client.shutdown_service(command_dict)- case "startup_service":- client.startup_service(command_dict)- case "list_users":- client.list_users(command_dict)- case "show_user":- client.show_user(command_dict)- case "drop_user":- client.drop_user(command_dict)- case "alter_user":- client.alter_user(command_dict)- case "create_user":- client.create_user(command_dict)- case "activate_user":- client.activate_user(command_dict)- case "list_datasets":- client.handle_list_datasets(command_dict)- case "list_agents":- client.handle_list_agents(command_dict)- case "create_role":- client.create_role(command_dict)- case "drop_role":- client.drop_role(command_dict)- case "alter_role":- client.alter_role(command_dict)- case "list_roles":- client.list_roles(command_dict)- case "show_role":- client.show_role(command_dict)- case "grant_permission":- client.grant_permission(command_dict)- case "revoke_permission":- client.revoke_permission(command_dict)- case "alter_user_role":- client.alter_user_role(command_dict)- case "show_user_permission":- client.show_user_permission(command_dict)- case "show_version":- client.show_version(command_dict)- case "grant_admin":- client.grant_admin(command_dict)- case "revoke_admin":- client.revoke_admin(command_dict)- case "generate_key":- client.generate_key(command_dict)- case "list_keys":- client.list_keys(command_dict)- case "drop_key":- client.drop_key(command_dict)- case "set_variable":- client.set_variable(command_dict)- case "show_variable":- client.show_variable(command_dict)- case "list_variables":- client.list_variables(command_dict)- case "list_configs":- client.list_configs(command_dict)- case "list_environments":- client.list_environments(command_dict)- case "show_fingerprint":- client.show_fingerprint(command_dict)- case "set_license":- client.set_license(command_dict)- case "set_license_config":- client.set_license_config(command_dict)- case "show_license":- client.show_license(command_dict)- case "check_license":- client.check_license(command_dict)- case "list_server_configs":- client.list_server_configs(command_dict)- case "create_model_provider":- client.create_model_provider(command_dict)- case "drop_model_provider":- client.drop_model_provider(command_dict)- case "show_current_user":- client.show_current_user(command_dict)- case "set_default_model":- client.set_default_model(command_dict)- case "reset_default_model":- client.reset_default_model(command_dict)- case "list_user_datasets":- return client.list_user_datasets(command_dict)- case "create_user_dataset":- client.create_user_dataset(command_dict)- case "drop_user_dataset":- client.drop_user_dataset(command_dict)- case "list_user_dataset_files":- return client.list_user_dataset_files(command_dict)- case "list_user_dataset_documents":- return client.list_user_dataset_documents(command_dict)- case "list_user_datasets_metadata":- return client.list_user_datasets_metadata(command_dict)- case "list_user_documents_metadata_summary":- return client.list_user_documents_metadata_summary(command_dict)- case "list_user_agents":- return client.list_user_agents(command_dict)- case "list_user_chats":- return client.list_user_chats(command_dict)- case "create_user_chat":- client.create_user_chat(command_dict)- case "drop_user_chat":- client.drop_user_chat(command_dict)- case "create_dataset_table":- client.create_dataset_table(command_dict)- case "drop_dataset_table":- client.drop_dataset_table(command_dict)- case "create_metadata_table":- client.create_metadata_table(command_dict)- case "drop_metadata_table":- client.drop_metadata_table(command_dict)- case "create_chat_session":- client.create_chat_session(command_dict)- case "drop_chat_session":- client.drop_chat_session(command_dict)- case "list_chat_sessions":- return client.list_chat_sessions(command_dict)- case "chat_on_session":- client.chat_on_session(command_dict)- case "list_user_model_providers":- client.list_user_model_providers(command_dict)- case "list_user_default_models":- client.list_user_default_models(command_dict)- case "parse_dataset_docs":- client.parse_dataset_docs(command_dict)- case "parse_dataset":- client.parse_dataset(command_dict)- case "import_docs_into_dataset":- client.import_docs_into_dataset(command_dict)- case "search_on_datasets":- return client.search_on_datasets(command_dict)- case "get_chunk":- return client.get_chunk(command_dict)- case "update_chunk":- return client.update_chunk(command_dict)- case "set_metadata":- return client.set_metadata(command_dict)- case "generate_nav_for_dataset":- return client.generate_nav_for_dataset(command_dict)- case "navigation_search":- return client.navigation_search(command_dict)- case "remove_tags":- return client.remove_tags(command_dict)- case "remove_chunks":- return client.remove_chunks(command_dict)- case "list_chunks":- return client.list_chunks(command_dict)- case "meta":- _handle_meta_command(command_dict)- case _:- print(f"Command '{command_type}' would be executed with API")---def _handle_meta_command(command: dict):- meta_command = command["command"]- args = command.get("args", [])-- if meta_command in ["?", "h", "help"]:- show_help()- elif meta_command in ["q", "quit", "exit"]:- print("Goodbye!")- else:- print(f"Meta command '{meta_command}' with args {args}")---def show_help():- """Help info"""- help_text = """-Commands:-LIST SERVICES-SHOW SERVICE <service>-STARTUP SERVICE <service>-SHUTDOWN SERVICE <service>-RESTART SERVICE <service>-LIST USERS-SHOW USER <user>-DROP USER <user>-CREATE USER <user> <password>-ALTER USER PASSWORD <user> <new_password>-ALTER USER ACTIVE <user> <on/off>-LIST DATASETS OF <user>-LIST AGENTS OF <user>-CREATE ROLE <role>-DROP ROLE <role>-ALTER ROLE <role> SET DESCRIPTION <description>-LIST ROLES-SHOW ROLE <role>-GRANT <action_list> ON <function> TO ROLE <role>-REVOKE <action_list> ON <function> TO ROLE <role>-ALTER USER <user> SET ROLE <role>-SHOW USER PERMISSION <user>-SHOW VERSION-GRANT ADMIN <user>-REVOKE ADMIN <user>-GENERATE KEY FOR USER <user>-LIST KEYS OF <user>-DROP KEY <key> OF <user>--User Commands (use -t user):-LIST DATASETS-LIST DOCUMENTS OF DATASET <dataset>-SEARCH <query> ON DATASETS <dataset>-GENERATE NAVIGATION OF DATASET '<id>'-NAVIGATION SEARCH '<query>' IN DATASET '<name or id>' MODE '<mode>' [TOP_K <n>]-LIST METADATA OF DATASETS <dataset>[, <dataset>]*-LIST METADATA SUMMARY OF DATASET <dataset> DOCUMENTS <doc_id>[, <doc_id>]*-GET CHUNK <chunk_id>-LIST CHUNKS OF DOCUMENT <doc_id> [PAGE <page>] [SIZE <size>] [KEYWORDS <keywords>] [AVAILABLE <0|1>]--Meta Commands:-\\?, \\h, \\help Show this help-\\q, \\quit, \\exit Quit the CLI- """- print(help_text)---def run_benchmark(client: RAGFlowClient, command_dict: dict):- concurrency = command_dict.get("concurrency", 1)- iterations = command_dict.get("iterations", 1)- command: dict = command_dict["command"]- command.update({"iterations": iterations})-- command_type = command["type"]- if concurrency < 1:- print("Concurrency must be greater than 0")- return- elif concurrency == 1:- result = run_command(client, command)- success_count: int = 0- response_list = result["response_list"]- for response in response_list:- match command_type:- case "ping_server":- if response.status_code == 200:- success_count += 1- case _:- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- success_count += 1-- total_duration = result["duration"]- qps = iterations / total_duration if total_duration > 0 else None- print(f"command: {command}, Concurrency: {concurrency}, iterations: {iterations}")- print(f"total duration: {total_duration:.4f}s, QPS: {qps}, COMMAND_COUNT: {iterations}, SUCCESS: {success_count}, FAILURE: {iterations - success_count}")- else:- results: list[dict | None] = [None] * concurrency- mp_context = mp.get_context("spawn")- start_time = time.perf_counter()- with ProcessPoolExecutor(max_workers=concurrency, mp_context=mp_context) as executor:- future_map = {executor.submit(run_command, client, command): idx for idx in range(concurrency)}- for future in as_completed(future_map):- idx = future_map[future]- results[idx] = future.result()- end_time = time.perf_counter()- success_count = 0- for result in results:- response_list = result["response_list"]- for response in response_list:- match command_type:- case "ping_server":- if response.status_code == 200:- success_count += 1- case _:- if response.status_code == 200:- res_json = response.json()- if res_json["code"] == 0:- success_count += 1-- total_duration = end_time - start_time- total_command_count = iterations * concurrency- qps = total_command_count / total_duration if total_duration > 0 else None- print(f"command: {command}, Concurrency: {concurrency} , iterations: {iterations}")- print(f"total duration: {total_duration:.4f}s, QPS: {qps}, COMMAND_COUNT: {total_command_count}, SUCCESS: {success_count}, FAILURE: {total_command_count - success_count}")diff --git a/admin/client/user.py b/admin/client/user.pydeleted file mode 100644index 4d417fa2a0..0000000000--- a/admin/client/user.py+++ /dev/null@@ -1,76 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--from http_client import HttpClient---class AuthException(Exception):- def __init__(self, message, code=401):- super().__init__(message)- self.code = code- self.message = message---def encrypt_password(password_plain: str) -> str:- try:- import base64- from Cryptodome.PublicKey import RSA- from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5-- def crypt(line):- """- decrypt(crypt(input_string)) == base64(input_string), which frontend and ragflow_cli use.- """- pub = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----"- rsa_key = RSA.importKey(pub)- cipher = Cipher_pkcs1_v1_5.new(rsa_key)- password_base64 = base64.b64encode(line.encode("utf-8")).decode("utf-8")- encrypted_password = cipher.encrypt(password_base64.encode())- return base64.b64encode(encrypted_password).decode("utf-8")- except Exception as exc:- raise AuthException("Password encryption unavailable; install pycryptodomex (uv sync --python 3.13 --group test).") from exc- return crypt(password_plain)---def register_user(client: HttpClient, email: str, nickname: str, password: str) -> None:- password_enc = encrypt_password(password)- payload = {"email": email, "nickname": nickname, "password": password_enc}- res = client.request_json("POST", "/users", use_api_base=True, auth_kind=None, json_body=payload)- if res.get("code") == 0:- return- msg = res.get("message", "")- if "has already registered" in msg:- return- raise AuthException(f"Register failed: {msg}")---def login_user(client: HttpClient, server_type: str, email: str, password: str) -> str:- password_enc = encrypt_password(password)- payload = {"email": email, "password": password_enc}- if server_type == "admin":- response = client.request("POST", "/admin/login", use_api_base=True, auth_kind=None, json_body=payload)- else:- response = client.request("POST", "/auth/login", use_api_base=True, auth_kind=None, json_body=payload)- try:- res = response.json()- except Exception as exc:- raise AuthException(f"Login failed: invalid JSON response ({exc})") from exc- if res.get("code") != 0:- raise AuthException(f"Login failed: {res.get('message')}")- token = response.headers.get("Authorization")- if not token:- raise AuthException("Login failed: missing Authorization header")- return tokendiff --git a/admin/client/uv.lock b/admin/client/uv.lockdeleted file mode 100644index 27b0f960ae..0000000000--- a/admin/client/uv.lock+++ /dev/null@@ -1,227 +0,0 @@-version = 1-revision = 3-requires-python = "==3.13.*"--[manifest]-constraints = [{ name = "urllib3", specifier = ">=2.7.0" }]--[[package]]-name = "beartype"-version = "0.22.6"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/88/e2/105ceb1704cb80fe4ab3872529ab7b6f365cf7c74f725e6132d0efcf1560/beartype-0.22.6.tar.gz", hash = "sha256:97fbda69c20b48c5780ac2ca60ce3c1bb9af29b3a1a0216898ffabdd523e48f4", size = 1588975, upload-time = "2025-11-20T04:47:14.736Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/98/c9/ceecc71fe2c9495a1d8e08d44f5f31f5bca1350d5b2e27a4b6265424f59e/beartype-0.22.6-py3-none-any.whl", hash = "sha256:0584bc46a2ea2a871509679278cda992eadde676c01356ab0ac77421f3c9a093", size = 1324807, upload-time = "2025-11-20T04:47:11.837Z" },-]--[[package]]-name = "certifi"-version = "2025.11.12"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/a2/8c/58f469717fa48465e4a50c014a0400602d3c437d7c0c468e17ada824da3a/certifi-2025.11.12.tar.gz", hash = "sha256:d8ab5478f2ecd78af242878415affce761ca6bc54a22a27e026d7c25357c3316", size = 160538, upload-time = "2025-11-12T02:54:51.517Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/70/7d/9bc192684cea499815ff478dfcdc13835ddf401365057044fb721ec6bddb/certifi-2025.11.12-py3-none-any.whl", hash = "sha256:97de8790030bbd5c2d96b7ec782fc2f7820ef8dba6db909ccf95449f2d062d4b", size = 159438, upload-time = "2025-11-12T02:54:49.735Z" },-]--[[package]]-name = "charset-normalizer"-version = "3.4.4"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/13/69/33ddede1939fdd074bce5434295f38fae7136463422fe4fd3e0e89b98062/charset_normalizer-3.4.4.tar.gz", hash = "sha256:94537985111c35f28720e43603b8e7b43a6ecfb2ce1d3058bbe955b73404e21a", size = 129418, upload-time = "2025-10-14T04:42:32.879Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/97/45/4b3a1239bbacd321068ea6e7ac28875b03ab8bc0aa0966452db17cd36714/charset_normalizer-3.4.4-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:e1f185f86a6f3403aa2420e815904c67b2f9ebc443f045edd0de921108345794", size = 208091, upload-time = "2025-10-14T04:41:13.346Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/7d/62/73a6d7450829655a35bb88a88fca7d736f9882a27eacdca2c6d505b57e2e/charset_normalizer-3.4.4-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6b39f987ae8ccdf0d2642338faf2abb1862340facc796048b604ef14919e55ed", size = 147936, upload-time = "2025-10-14T04:41:14.461Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/89/c5/adb8c8b3d6625bef6d88b251bbb0d95f8205831b987631ab0c8bb5d937c2/charset_normalizer-3.4.4-cp313-cp313-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:3162d5d8ce1bb98dd51af660f2121c55d0fa541b46dff7bb9b9f86ea1d87de72", size = 144180, upload-time = "2025-10-14T04:41:15.588Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/91/ed/9706e4070682d1cc219050b6048bfd293ccf67b3d4f5a4f39207453d4b99/charset_normalizer-3.4.4-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:81d5eb2a312700f4ecaa977a8235b634ce853200e828fbadf3a9c50bab278328", size = 161346, upload-time = "2025-10-14T04:41:16.738Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/d5/0d/031f0d95e4972901a2f6f09ef055751805ff541511dc1252ba3ca1f80cf5/charset_normalizer-3.4.4-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5bd2293095d766545ec1a8f612559f6b40abc0eb18bb2f5d1171872d34036ede", size = 158874, upload-time = "2025-10-14T04:41:17.923Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/f5/83/6ab5883f57c9c801ce5e5677242328aa45592be8a00644310a008d04f922/charset_normalizer-3.4.4-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:a8a8b89589086a25749f471e6a900d3f662d1d3b6e2e59dcecf787b1cc3a1894", size = 153076, upload-time = "2025-10-14T04:41:19.106Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/75/1e/5ff781ddf5260e387d6419959ee89ef13878229732732ee73cdae01800f2/charset_normalizer-3.4.4-cp313-cp313-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:bc7637e2f80d8530ee4a78e878bce464f70087ce73cf7c1caf142416923b98f1", size = 150601, upload-time = "2025-10-14T04:41:20.245Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/d7/57/71be810965493d3510a6ca79b90c19e48696fb1ff964da319334b12677f0/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:f8bf04158c6b607d747e93949aa60618b61312fe647a6369f88ce2ff16043490", size = 150376, upload-time = "2025-10-14T04:41:21.398Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/e5/d5/c3d057a78c181d007014feb7e9f2e65905a6c4ef182c0ddf0de2924edd65/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_armv7l.whl", hash = "sha256:554af85e960429cf30784dd47447d5125aaa3b99a6f0683589dbd27e2f45da44", size = 144825, upload-time = "2025-10-14T04:41:22.583Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/e6/8c/d0406294828d4976f275ffbe66f00266c4b3136b7506941d87c00cab5272/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:74018750915ee7ad843a774364e13a3db91682f26142baddf775342c3f5b1133", size = 162583, upload-time = "2025-10-14T04:41:23.754Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/d7/24/e2aa1f18c8f15c4c0e932d9287b8609dd30ad56dbe41d926bd846e22fb8d/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_riscv64.whl", hash = "sha256:c0463276121fdee9c49b98908b3a89c39be45d86d1dbaa22957e38f6321d4ce3", size = 150366, upload-time = "2025-10-14T04:41:25.27Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/e4/5b/1e6160c7739aad1e2df054300cc618b06bf784a7a164b0f238360721ab86/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_s390x.whl", hash = "sha256:362d61fd13843997c1c446760ef36f240cf81d3ebf74ac62652aebaf7838561e", size = 160300, upload-time = "2025-10-14T04:41:26.725Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/7a/10/f882167cd207fbdd743e55534d5d9620e095089d176d55cb22d5322f2afd/charset_normalizer-3.4.4-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:9a26f18905b8dd5d685d6d07b0cdf98a79f3c7a918906af7cc143ea2e164c8bc", size = 154465, upload-time = "2025-10-14T04:41:28.322Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/89/66/c7a9e1b7429be72123441bfdbaf2bc13faab3f90b933f664db506dea5915/charset_normalizer-3.4.4-cp313-cp313-win32.whl", hash = "sha256:9b35f4c90079ff2e2edc5b26c0c77925e5d2d255c42c74fdb70fb49b172726ac", size = 99404, upload-time = "2025-10-14T04:41:29.95Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c4/26/b9924fa27db384bdcd97ab83b4f0a8058d96ad9626ead570674d5e737d90/charset_normalizer-3.4.4-cp313-cp313-win_amd64.whl", hash = "sha256:b435cba5f4f750aa6c0a0d92c541fb79f69a387c91e61f1795227e4ed9cece14", size = 107092, upload-time = "2025-10-14T04:41:31.188Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/af/8f/3ed4bfa0c0c72a7ca17f0380cd9e4dd842b09f664e780c13cff1dcf2ef1b/charset_normalizer-3.4.4-cp313-cp313-win_arm64.whl", hash = "sha256:542d2cee80be6f80247095cc36c418f7bddd14f4a6de45af91dfad36d817bba2", size = 100408, upload-time = "2025-10-14T04:41:32.624Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/0a/4c/925909008ed5a988ccbb72dcc897407e5d6d3bd72410d69e051fc0c14647/charset_normalizer-3.4.4-py3-none-any.whl", hash = "sha256:7a32c560861a02ff789ad905a2fe94e3f840803362c84fecf1851cb4cf3dc37f", size = 53402, upload-time = "2025-10-14T04:42:31.76Z" },-]--[[package]]-name = "colorama"-version = "0.4.6"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },-]--[[package]]-name = "idna"-version = "3.11"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/6f/6d/0703ccc57f3a7233505399edb88de3cbd678da106337b9fcde432b65ed60/idna-3.11.tar.gz", hash = "sha256:795dafcc9c04ed0c1fb032c2aa73654d8e8c5023a7df64a53f39190ada629902", size = 194582, upload-time = "2025-10-12T14:55:20.501Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/0e/61/66938bbb5fc52dbdf84594873d5b51fb1f7c7794e9c0f5bd885f30bc507b/idna-3.11-py3-none-any.whl", hash = "sha256:771a87f49d9defaf64091e6e6fe9c18d4833f140bd19464795bc32d966ca37ea", size = 71008, upload-time = "2025-10-12T14:55:18.883Z" },-]--[[package]]-name = "iniconfig"-version = "2.3.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/72/34/14ca021ce8e5dfedc35312d08ba8bf51fdd999c576889fc2c24cb97f4f10/iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730", size = 20503, upload-time = "2025-10-18T21:55:43.219Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/cb/b1/3846dd7f199d53cb17f49cba7e651e9ce294d8497c8c150530ed11865bb8/iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12", size = 7484, upload-time = "2025-10-18T21:55:41.639Z" },-]--[[package]]-name = "lark"-version = "1.3.1"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/da/34/28fff3ab31ccff1fd4f6c7c7b0ceb2b6968d8ea4950663eadcb5720591a0/lark-1.3.1.tar.gz", hash = "sha256:b426a7a6d6d53189d318f2b6236ab5d6429eaf09259f1ca33eb716eed10d2905", size = 382732, upload-time = "2025-10-27T18:25:56.653Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/82/3d/14ce75ef66813643812f3093ab17e46d3a206942ce7376d31ec2d36229e7/lark-1.3.1-py3-none-any.whl", hash = "sha256:c629b661023a014c37da873b4ff58a817398d12635d3bbb2c5a03be7fe5d1e12", size = 113151, upload-time = "2025-10-27T18:25:54.882Z" },-]--[[package]]-name = "packaging"-version = "25.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727, upload-time = "2025-04-19T11:48:59.673Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },-]--[[package]]-name = "pluggy"-version = "1.6.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" },-]--[[package]]-name = "pycryptodomex"-version = "3.23.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c9/85/e24bf90972a30b0fcd16c73009add1d7d7cd9140c2498a68252028899e41/pycryptodomex-3.23.0.tar.gz", hash = "sha256:71909758f010c82bc99b0abf4ea12012c98962fbf0583c2164f8b84533c2e4da", size = 4922157, upload-time = "2025-05-17T17:23:41.434Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/2e/00/10edb04777069a42490a38c137099d4b17ba6e36a4e6e28bdc7470e9e853/pycryptodomex-3.23.0-cp313-cp313t-macosx_10_13_universal2.whl", hash = "sha256:7b37e08e3871efe2187bc1fd9320cc81d87caf19816c648f24443483005ff886", size = 2498764, upload-time = "2025-05-17T17:22:21.453Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/6b/3f/2872a9c2d3a27eac094f9ceaa5a8a483b774ae69018040ea3240d5b11154/pycryptodomex-3.23.0-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:91979028227543010d7b2ba2471cf1d1e398b3f183cb105ac584df0c36dac28d", size = 1643012, upload-time = "2025-05-17T17:22:23.702Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/70/af/774c2e2b4f6570fbf6a4972161adbb183aeeaa1863bde31e8706f123bf92/pycryptodomex-3.23.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6b8962204c47464d5c1c4038abeadd4514a133b28748bcd9fa5b6d62e3cec6fa", size = 2187643, upload-time = "2025-05-17T17:22:26.37Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/de/a3/71065b24cb889d537954cedc3ae5466af00a2cabcff8e29b73be047e9a19/pycryptodomex-3.23.0-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a33986a0066860f7fcf7c7bd2bc804fa90e434183645595ae7b33d01f3c91ed8", size = 2273762, upload-time = "2025-05-17T17:22:28.313Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c9/0b/ff6f43b7fbef4d302c8b981fe58467b8871902cdc3eb28896b52421422cc/pycryptodomex-3.23.0-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c7947ab8d589e3178da3d7cdeabe14f841b391e17046954f2fbcd941705762b5", size = 2313012, upload-time = "2025-05-17T17:22:30.57Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/02/de/9d4772c0506ab6da10b41159493657105d3f8bb5c53615d19452afc6b315/pycryptodomex-3.23.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:c25e30a20e1b426e1f0fa00131c516f16e474204eee1139d1603e132acffc314", size = 2186856, upload-time = "2025-05-17T17:22:32.819Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/28/ad/8b30efcd6341707a234e5eba5493700a17852ca1ac7a75daa7945fcf6427/pycryptodomex-3.23.0-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:da4fa650cef02db88c2b98acc5434461e027dce0ae8c22dd5a69013eaf510006", size = 2347523, upload-time = "2025-05-17T17:22:35.386Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/0f/02/16868e9f655b7670dbb0ac4f2844145cbc42251f916fc35c414ad2359849/pycryptodomex-3.23.0-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:58b851b9effd0d072d4ca2e4542bf2a4abcf13c82a29fd2c93ce27ee2a2e9462", size = 2272825, upload-time = "2025-05-17T17:22:37.632Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/ca/18/4ca89ac737230b52ac8ffaca42f9c6f1fd07c81a6cd821e91af79db60632/pycryptodomex-3.23.0-cp313-cp313t-win32.whl", hash = "sha256:a9d446e844f08299236780f2efa9898c818fe7e02f17263866b8550c7d5fb328", size = 1772078, upload-time = "2025-05-17T17:22:40Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/73/34/13e01c322db027682e00986873eca803f11c56ade9ba5bbf3225841ea2d4/pycryptodomex-3.23.0-cp313-cp313t-win_amd64.whl", hash = "sha256:bc65bdd9fc8de7a35a74cab1c898cab391a4add33a8fe740bda00f5976ca4708", size = 1803656, upload-time = "2025-05-17T17:22:42.139Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/54/68/9504c8796b1805d58f4425002bcca20f12880e6fa4dc2fc9a668705c7a08/pycryptodomex-3.23.0-cp313-cp313t-win_arm64.whl", hash = "sha256:c885da45e70139464f082018ac527fdaad26f1657a99ee13eecdce0f0ca24ab4", size = 1707172, upload-time = "2025-05-17T17:22:44.704Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/dd/9c/1a8f35daa39784ed8adf93a694e7e5dc15c23c741bbda06e1d45f8979e9e/pycryptodomex-3.23.0-cp37-abi3-macosx_10_9_universal2.whl", hash = "sha256:06698f957fe1ab229a99ba2defeeae1c09af185baa909a31a5d1f9d42b1aaed6", size = 2499240, upload-time = "2025-05-17T17:22:46.953Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/7a/62/f5221a191a97157d240cf6643747558759126c76ee92f29a3f4aee3197a5/pycryptodomex-3.23.0-cp37-abi3-macosx_10_9_x86_64.whl", hash = "sha256:b2c2537863eccef2d41061e82a881dcabb04944c5c06c5aa7110b577cc487545", size = 1644042, upload-time = "2025-05-17T17:22:49.098Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/8c/fd/5a054543c8988d4ed7b612721d7e78a4b9bf36bc3c5ad45ef45c22d0060e/pycryptodomex-3.23.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:43c446e2ba8df8889e0e16f02211c25b4934898384c1ec1ec04d7889c0333587", size = 2186227, upload-time = "2025-05-17T17:22:51.139Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c8/a9/8862616a85cf450d2822dbd4fff1fcaba90877907a6ff5bc2672cafe42f8/pycryptodomex-3.23.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f489c4765093fb60e2edafdf223397bc716491b2b69fe74367b70d6999257a5c", size = 2272578, upload-time = "2025-05-17T17:22:53.676Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/46/9f/bda9c49a7c1842820de674ab36c79f4fbeeee03f8ff0e4f3546c3889076b/pycryptodomex-3.23.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:bdc69d0d3d989a1029df0eed67cc5e8e5d968f3724f4519bd03e0ec68df7543c", size = 2312166, upload-time = "2025-05-17T17:22:56.585Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/03/cc/870b9bf8ca92866ca0186534801cf8d20554ad2a76ca959538041b7a7cf4/pycryptodomex-3.23.0-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:6bbcb1dd0f646484939e142462d9e532482bc74475cecf9c4903d4e1cd21f003", size = 2185467, upload-time = "2025-05-17T17:22:59.237Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/96/e3/ce9348236d8e669fea5dd82a90e86be48b9c341210f44e25443162aba187/pycryptodomex-3.23.0-cp37-abi3-musllinux_1_2_i686.whl", hash = "sha256:8a4fcd42ccb04c31268d1efeecfccfd1249612b4de6374205376b8f280321744", size = 2346104, upload-time = "2025-05-17T17:23:02.112Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/a5/e9/e869bcee87beb89040263c416a8a50204f7f7a83ac11897646c9e71e0daf/pycryptodomex-3.23.0-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:55ccbe27f049743a4caf4f4221b166560d3438d0b1e5ab929e07ae1702a4d6fd", size = 2271038, upload-time = "2025-05-17T17:23:04.872Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/8d/67/09ee8500dd22614af5fbaa51a4aee6e342b5fa8aecf0a6cb9cbf52fa6d45/pycryptodomex-3.23.0-cp37-abi3-win32.whl", hash = "sha256:189afbc87f0b9f158386bf051f720e20fa6145975f1e76369303d0f31d1a8d7c", size = 1771969, upload-time = "2025-05-17T17:23:07.115Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/69/96/11f36f71a865dd6df03716d33bd07a67e9d20f6b8d39820470b766af323c/pycryptodomex-3.23.0-cp37-abi3-win_amd64.whl", hash = "sha256:52e5ca58c3a0b0bd5e100a9fbc8015059b05cffc6c66ce9d98b4b45e023443b9", size = 1803124, upload-time = "2025-05-17T17:23:09.267Z" },- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/f9/93/45c1cdcbeb182ccd2e144c693eaa097763b08b38cded279f0053ed53c553/pycryptodomex-3.23.0-cp37-abi3-win_arm64.whl", hash = "sha256:02d87b80778c171445d67e23d1caef279bf4b25c3597050ccd2e13970b57fd51", size = 1707161, upload-time = "2025-05-17T17:23:11.414Z" },-]--[[package]]-name = "pygments"-version = "2.19.2"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/b0/77/a5b8c569bf593b0140bde72ea885a803b82086995367bf2037de0159d924/pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", size = 4968631, upload-time = "2025-06-21T13:39:12.283Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" },-]--[[package]]-name = "pytest"-version = "9.0.1"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-dependencies = [- { name = "colorama", marker = "sys_platform == 'win32'" },- { name = "iniconfig" },- { name = "packaging" },- { name = "pluggy" },- { name = "pygments" },-]-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/07/56/f013048ac4bc4c1d9be45afd4ab209ea62822fb1598f40687e6bf45dcea4/pytest-9.0.1.tar.gz", hash = "sha256:3e9c069ea73583e255c3b21cf46b8d3c56f6e3a1a8f6da94ccb0fcf57b9d73c8", size = 1564125, upload-time = "2025-11-12T13:05:09.333Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/0b/8b/6300fb80f858cda1c51ffa17075df5d846757081d11ab4aa35cef9e6258b/pytest-9.0.1-py3-none-any.whl", hash = "sha256:67be0030d194df2dfa7b556f2e56fb3c3315bd5c8822c6951162b92b32ce7dad", size = 373668, upload-time = "2025-11-12T13:05:07.379Z" },-]--[[package]]-name = "ragflow-cli"-version = "0.27.2"-source = { virtual = "." }-dependencies = [- { name = "beartype" },- { name = "lark" },- { name = "pycryptodomex" },- { name = "requests" },- { name = "requests-toolbelt" },-]--[package.dev-dependencies]-test = [- { name = "pytest" },- { name = "requests" },-]--[package.metadata]-requires-dist = [- { name = "beartype", specifier = ">=0.20.0,<1.0.0" },- { name = "lark", specifier = ">=1.1.0" },- { name = "pycryptodomex", specifier = ">=3.10.0" },- { name = "requests", specifier = ">=2.30.0,<3.0.0" },- { name = "requests-toolbelt", specifier = ">=1.0.0" },-]--[package.metadata.requires-dev]-test = [- { name = "pytest", specifier = ">=8.3.5" },- { name = "requests", specifier = ">=2.32.3" },-]--[[package]]-name = "requests"-version = "2.32.5"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-dependencies = [- { name = "certifi" },- { name = "charset-normalizer" },- { name = "idna" },- { name = "urllib3" },-]-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/c9/74/b3ff8e6c8446842c3f5c837e9c3dfcfe2018ea6ecef224c710c85ef728f4/requests-2.32.5.tar.gz", hash = "sha256:dbba0bac56e100853db0ea71b82b4dfd5fe2bf6d3754a8893c3af500cec7d7cf", size = 134517, upload-time = "2025-08-18T20:46:02.573Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/1e/db/4254e3eabe8020b458f1a747140d32277ec7a271daf1d235b70dc0b4e6e3/requests-2.32.5-py3-none-any.whl", hash = "sha256:2462f94637a34fd532264295e186976db0f5d453d1cdd31473c85a6a161affb6", size = 64738, upload-time = "2025-08-18T20:46:00.542Z" },-]--[[package]]-name = "requests-toolbelt"-version = "1.0.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-dependencies = [- { name = "requests" },-]-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/f3/61/d7545dafb7ac2230c70d38d31cbfe4cc64f7144dc41f6e4e4b78ecd9f5bb/requests-toolbelt-1.0.0.tar.gz", hash = "sha256:7681a0a3d047012b5bdc0ee37d7f8f07ebe76ab08caeccfc3921ce23c88d5bc6", size = 206888, upload-time = "2023-05-01T04:11:33.229Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/3f/51/d4db610ef29373b879047326cbf6fa98b6c1969d6f6dc423279de2b1be2c/requests_toolbelt-1.0.0-py2.py3-none-any.whl", hash = "sha256:cccfdd665f0a24fcf4726e690f65639d272bb0637b9b92dfd91a5568ccf6bd06", size = 54481, upload-time = "2023-05-01T04:11:28.427Z" },-]--[[package]]-name = "urllib3"-version = "2.7.0"-source = { registry = "https://pypi.tuna.tsinghua.edu.cn/simple" }-sdist = { url = "https://pypi.tuna.tsinghua.edu.cn/packages/53/0c/06f8b233b8fd13b9e5ee11424ef85419ba0d8ba0b3138bf360be2ff56953/urllib3-2.7.0.tar.gz", hash = "sha256:231e0ec3b63ceb14667c67be60f2f2c40a518cb38b03af60abc813da26505f4c", size = 433602, upload-time = "2026-05-07T16:13:18.596Z" }-wheels = [- { url = "https://pypi.tuna.tsinghua.edu.cn/packages/7f/3e/5db95bcf282c52709639744ca2a8b149baccf648e39c8cc87553df9eae0c/urllib3-2.7.0-py3-none-any.whl", hash = "sha256:9fb4c81ebbb1ce9531cce37674bbc6f1360472bc18ca9a553ede278ef7276897", size = 131087, upload-time = "2026-05-07T16:13:17.471Z" },-]diff --git a/admin/server/admin_server.py b/admin/server/admin_server.pydeleted file mode 100644index 9e54a1cf21..0000000000--- a/admin/server/admin_server.py+++ /dev/null@@ -1,95 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import time--start_ts = time.time()--import os-import signal-import logging-import threading-import faulthandler--from flask import Flask-from flask_login import LoginManager-from werkzeug.serving import run_simple-from routes import admin_bp-from common.log_utils import init_root_logger-from common.constants import SERVICE_CONF-from common.config_utils import show_configs-from common import settings-from config import load_configurations, SERVICE_CONFIGS-from auth import init_default_admin, setup_auth-from flask_session import Session-from common.versions import get_ragflow_version-from api.db.db_models import close_connection--stop_event = threading.Event()--if __name__ == "__main__":- faulthandler.enable()- init_root_logger("admin_service")- logging.info(r"""- ____ ___ ______________ ___ __ _- / __ \/ | / ____/ ____/ /___ _ __ / | ____/ /___ ___ (_)___- / /_/ / /| |/ / __/ /_ / / __ \ | /| / / / /| |/ __ / __ `__ \/ / __ \- / _, _/ ___ / /_/ / __/ / / /_/ / |/ |/ / / ___ / /_/ / / / / / / / / / /- /_/ |_/_/ |_\____/_/ /_/\____/|__/|__/ /_/ |_\__,_/_/ /_/ /_/_/_/ /_/- """)-- app = Flask(__name__)- app.register_blueprint(admin_bp)- app.config["SESSION_PERMANENT"] = False- app.config["SESSION_TYPE"] = "filesystem"- app.config["MAX_CONTENT_LENGTH"] = int(os.environ.get("MAX_CONTENT_LENGTH", 1024 * 1024 * 1024))- Session(app)- logging.info(f"RAGFlow admin version: {get_ragflow_version()}")- show_configs()- login_manager = LoginManager()- login_manager.init_app(app)-- @app.teardown_request- def _db_close(exception):- if settings.DATABASE_TYPE.lower() == "gaussdb":- if exception:- logging.error(- "Admin request failed: %s",- exception,- exc_info=(type(exception), exception, exception.__traceback__),- )- close_connection()-- settings.init_settings()- setup_auth(login_manager)- init_default_admin()- SERVICE_CONFIGS.configs = load_configurations(SERVICE_CONF)-- try:- logging.info(f"RAGFlow admin is ready after {time.time() - start_ts}s initialization.")- run_simple(- hostname="0.0.0.0",- port=9381,- application=app,- threaded=True,- use_reloader=False,- use_debugger=False,- )- except Exception as e:- logging.exception(f"Unhandled exception: {e}")- stop_event.set()- time.sleep(1)- os.kill(os.getpid(), signal.SIGKILL)diff --git a/admin/server/auth.py b/admin/server/auth.pydeleted file mode 100644index cba5d1f9cb..0000000000--- a/admin/server/auth.py+++ /dev/null@@ -1,353 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#---import logging-import os-import secrets-import uuid-from functools import wraps-from datetime import datetime--from flask_login import current_user, login_user--from api.common.exceptions import AdminException, UserNotFoundError-from api.common.base64 import encode_to_base64-from api.db.services import UserService-from api.db import UserTenantRole-from api.db.services.user_service import TenantService, UserTenantService-from common.constants import ActiveEnum, StatusEnum-from common.file_utils import get_project_base_directory-from api.utils.crypt import CryptPayloadError, decrypt-from common.misc_utils import get_uuid-from common.time_utils import current_timestamp, datetime_format, get_format_time-from common.connection_utils import sync_construct_response-from common import settings---# Bootstrap password for the default superuser created by init_default_admin().-# ADMIN_DEFAULT_PASSWORD is admin-server specific; DEFAULT_SUPERUSER_PASSWORD is-# shared with api/db/init_data.py so a deployment can keep one bootstrap-# password for both sides. Unlike api/db/init_data.py (which still falls back-# to the literal "admin"), the admin server falls back to a random password-# delivered through a 0600 bootstrap file: the admin panel manages sandbox providers-# (including the "local" provider, i.e. host-level execution), so a guessable-# default here would effectively be host RCE.-ADMIN_DEFAULT_PASSWORD_ENV = "ADMIN_DEFAULT_PASSWORD"-DEFAULT_SUPERUSER_PASSWORD_ENV = "DEFAULT_SUPERUSER_PASSWORD"---# The admin server reports DEFAULT_SUPERUSER_EMAIL via-# `services.EnvironmentsMgr.get_all()` and the admin CLI's "list envs"-# output, so operators reasonably expect the env value to drive the-# bootstrap admin account's email. (DEFAULT_SUPERUSER_PASSWORD is-# deliberately NOT exposed through the env-listing API — it would-# leak the bootstrap credential into any audit log that records the-# full env map.) Read once at module load so the bootstrap functions-# below can reference the values as module-level constants, matching-# `api/db/init_data.py:init_superuser`. See infiniflow/ragflow#16876.-#-# Reject empty env values explicitly: ``os.getenv(name, fallback)``-# only uses the fallback when the variable is *unset*, so a misconfigured-# operator setting ``DEFAULT_SUPERUSER_EMAIL=`` would otherwise-# propagate an empty email into the bootstrap flow. We deliberately-# preserve raw password values (no whitespace stripping) so a password-# like ``" s3cret "`` keeps its leading/trailing whitespace and isn't-# silently mutated before being hashed/stored. Email/nickname strip-# separately because whitespace there would be invalid.-def _required_env(name, default):- value = os.getenv(name, default)- if not value:- logging.error(- "admin server: %s is set but empty; either unset it (the default %r will be used) or provide a real value",- name,- default,- )- raise RuntimeError(f"admin server: {name} is set but empty; either unset it (the default {default!r} will be used) or provide a real value")- return value---def _required_env_stripped(name, default):- """Like ``_required_env`` but rejects whitespace-only values.-- A raw value of ``" "`` would survive the empty-check in- ``_required_env`` and then collapse to ``""`` after the caller- ``.strip()``s it, propagating an empty nickname/email into the- bootstrap flow and silently failing later checks. Strip before- the empty check so the failure surfaces at startup with the- intended config-error message."""- raw = os.getenv(name, default)- stripped = raw.strip()- if not stripped:- logging.error(- "admin server: %s is set to whitespace-only value; either unset it (the default %r will be used) or provide a real value",- name,- default,- )- raise RuntimeError(f"admin server: {name} is set to whitespace-only value; either unset it (the default {default!r} will be used) or provide a real value")- return stripped---DEFAULT_SUPERUSER_NICKNAME = _required_env_stripped("DEFAULT_SUPERUSER_NICKNAME", "admin")-DEFAULT_SUPERUSER_EMAIL = _required_env_stripped("DEFAULT_SUPERUSER_EMAIL", "[email protected]")-# NOTE: there is deliberately no DEFAULT_SUPERUSER_PASSWORD module constant-# here. The bootstrap password resolves at startup via-# _resolve_bootstrap_admin_password() — ADMIN_DEFAULT_PASSWORD, then-# DEFAULT_SUPERUSER_PASSWORD, then a random password delivered through a-# 0600 file — instead of falling back to the literal "admin" that-# api/db/init_data.py still uses.---def setup_auth(login_manager):- @login_manager.request_loader- def load_user(web_request):- # Authorization header contains JWT-encoded access token- # First decode JWT to get the UUID, then query database- from itsdangerous.url_safe import URLSafeTimedSerializer as Serializer- from common import settings-- authorization = web_request.headers.get("Authorization")- if authorization:- try:- # Strip "Bearer " prefix if present- jwt_token = authorization- if jwt_token.startswith("Bearer "):- jwt_token = jwt_token[7:]-- jwt_token = jwt_token.strip()- if not jwt_token:- logging.warning("Authentication attempt with empty JWT token")- return None-- # Decode JWT to get the UUID access_token- jwt = Serializer(secret_key=settings.get_secret_key())- access_token = str(jwt.loads(jwt_token))-- if not access_token or not access_token.strip():- logging.warning("Authentication attempt with empty access token after JWT decode")- return None-- # Access tokens stored in database are UUIDs (32 hex characters)- if len(access_token) < 32:- logging.warning(f"Authentication attempt with invalid token format: {len(access_token)} chars")- return None-- user = UserService.query(access_token=access_token, status=StatusEnum.VALID.value)- if user:- if not user[0].access_token or not user[0].access_token.strip():- logging.warning(f"User {user[0].email} has empty access_token in database")- return None- return user[0]- else:- return None- except Exception as e:- logging.warning(f"load_user got exception {e}")- return None- else:- return None---def _resolve_bootstrap_admin_password() -> tuple[str, str | None]:- """Return ``(plain_password, env_var_name)`` for the bootstrap superuser.-- Precedence: ADMIN_DEFAULT_PASSWORD, then DEFAULT_SUPERUSER_PASSWORD (the- variable honoured by api/db/init_data.py on the web side), then a random- password that is delivered through a 0600 bootstrap file, never the log.- """- for env_name in (ADMIN_DEFAULT_PASSWORD_ENV, DEFAULT_SUPERUSER_PASSWORD_ENV):- value = os.getenv(env_name)- if value:- return value, env_name- return secrets.token_urlsafe(18), None---def _write_bootstrap_password_file(password: str) -> str | None:- """Write the generated bootstrap password to a 0600 file beside the logs.-- Returns the file path, or None when the file could not be written (the- password is then unrecoverable and the operator must set- ADMIN_DEFAULT_PASSWORD and recreate the account — failing closed beats- leaking the credential into a log stream).- """- target_dir = os.path.join(get_project_base_directory(), "logs")- path = os.path.join(target_dir, "admin_bootstrap_password.txt")- try:- os.makedirs(target_dir, exist_ok=True)- fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)- with os.fdopen(fd, "w") as fh:- fh.write(password + "\n")- return path- except OSError as e:- logging.error(f"Failed to write the admin bootstrap password file {path}: {e}")- return None---def init_default_admin():- # Verify that at least one active admin user exists. If not, create a default one.- # This runs only from the explicit startup path in admin_server.py; the- # authentication handlers below must never create accounts.- users = UserService.query(is_superuser=True)- if not users:- password, env_name = _resolve_bootstrap_admin_password()- default_admin = {- "id": uuid.uuid1().hex,- # UserService.save() hashes this value and both login paths compare- # against base64(<plain password>) - same convention as- # api/db/init_data.py.- "password": encode_to_base64(password),- "nickname": DEFAULT_SUPERUSER_NICKNAME,- "is_superuser": True,- "email": DEFAULT_SUPERUSER_EMAIL,- "creator": "system",- "status": "1",- }- if not UserService.save(**default_admin):- raise AdminException("Can't init admin.", 500)- add_tenant_for_admin(default_admin, UserTenantRole.OWNER)- if env_name:- logging.info(f"Created default superuser {DEFAULT_SUPERUSER_EMAIL} with the password from {env_name}. Change it after the first login.")- else:- password_file = _write_bootstrap_password_file(password)- if password_file:- logging.warning(- f"No superuser found and neither {ADMIN_DEFAULT_PASSWORD_ENV} nor {DEFAULT_SUPERUSER_PASSWORD_ENV} is set. "- f"Created default superuser {DEFAULT_SUPERUSER_EMAIL}; its randomly generated password was written once to {password_file} (mode 0600). Read it there and change it immediately after the first login."- )- else:- logging.error(- f"No superuser found and neither {ADMIN_DEFAULT_PASSWORD_ENV} nor {DEFAULT_SUPERUSER_PASSWORD_ENV} is set, "- f"and the bootstrap password file could not be written. The randomly generated password for {DEFAULT_SUPERUSER_EMAIL} is NOT recoverable: "- f"delete the account and re-run with {ADMIN_DEFAULT_PASSWORD_ENV} set."- )- elif not any([u.is_active == ActiveEnum.ACTIVE.value for u in users]):- raise AdminException("No active admin. Please update 'is_active' in db manually.", 500)- else:- # Filter existing superuser rows by the configured- # ``DEFAULT_SUPERUSER_EMAIL``. Per @Lynn-Inf's review on- # PR #17674, this intentionally does NOT also filter on- # ``is_active`` — an inactive row matching the configured- # email must still be treated as the configured bootstrap- # admin, otherwise the tenant-backfill branch above could- # silently create a duplicate entry for the (already-existing,- # just-disabled) default admin. Activity gating already lives- # in ``login_admin`` (``is_active == INACTIVE`` rejects the- # session at login time), so the bootstrap path stays- # read-only.- default_admin_rows = [u for u in users if u.email == DEFAULT_SUPERUSER_EMAIL]- if default_admin_rows:- default_admin = default_admin_rows[0].to_dict()- exist, default_admin_tenant = TenantService.get_by_id(default_admin["id"])- if not exist:- add_tenant_for_admin(default_admin, UserTenantRole.OWNER)- elif any(u.email != DEFAULT_SUPERUSER_EMAIL for u in users):- # Active superuser(s) exist but none match the configured- # DEFAULT_SUPERUSER_EMAIL — the operator set the env var to a- # value that doesn't match any existing admin row. Don't- # silently fall back to the original "[email protected]"- # identity (that would diverge the bootstrap credential- # from the env), and don't auto-create a new admin (the- # user may have intentionally kept the old admin). Surface- # the mismatch with explicit migration guidance.- existing_emails = sorted({u.email for u in users})- logging.error(- "admin server: configured DEFAULT_SUPERUSER_EMAIL (%r) "- "does not match any active superuser row; existing admin "- "emails: %r. Refusing to bootstrap to avoid diverging the "- "credential from the env value.",- DEFAULT_SUPERUSER_EMAIL,- existing_emails,- )- raise AdminException(- f"Active superuser(s) exist but none match DEFAULT_SUPERUSER_EMAIL "- f"({DEFAULT_SUPERUSER_EMAIL!r}). Either unset the env var to "- f"keep the existing admin, or run the API-side migration "- f"(``python -m api.ragflow_server --init-superuser``) to align "- f"the configured email with an existing admin row before "- f"restarting.",- 500,- )---def add_tenant_for_admin(user_info: dict, role: str):- tenant = {- "id": user_info["id"],- "name": user_info["nickname"] + "‘s Kingdom",- "llm_id": settings.CHAT_MDL,- "embd_id": settings.EMBEDDING_MDL,- "asr_id": settings.ASR_MDL,- "parser_ids": settings.PARSERS,- "img2txt_id": settings.VISION_MDL,- "rerank_id": settings.RERANK_MDL,- }- usr_tenant = {"tenant_id": user_info["id"], "user_id": user_info["id"], "invited_by": user_info["id"], "role": role}-- # tenant_llm = get_init_tenant_llm(user_info["id"])- TenantService.insert(**tenant)- UserTenantService.insert(**usr_tenant)- # TenantLLMService.insert_many(tenant_llm)- logging.info(f"Added tenant for email: {user_info['email']}, A default tenant has been set; changing the default models after login is strongly recommended.")---def check_admin_auth(func):- @wraps(func)- def wrapper(*args, **kwargs):- user = UserService.filter_by_id(current_user.id)- if not user:- raise UserNotFoundError(current_user.email)- if not user.is_superuser:- raise AdminException("Not admin", 403)- if user.is_active == ActiveEnum.INACTIVE.value:- raise AdminException(f"User {current_user.email} inactive", 403)-- return func(*args, **kwargs)-- return wrapper---def login_admin(email: str, password: str):- """- :param email: admin email- :param password: string before decrypt (RSA encrypted + base64 encoded)- """- users = UserService.query(email=email)- if not users:- raise UserNotFoundError(email)- try:- decrypted = decrypt(password)- except CryptPayloadError:- # A payload the client could not encode correctly is the client's- # failure, not a server fault: answer with the same generic mismatch- # as any other credential rejection. Server-side faults- # (missing/invalid private key) propagate to the caller instead.- logging.info("Admin login with undecryptable password payload.")- raise AdminException("Email and password do not match!")- user = UserService.query_user(email, decrypted)- if not user:- raise AdminException("Email and password do not match!")- if not user.is_superuser:- raise AdminException("Not admin", 403)- if user.is_active == ActiveEnum.INACTIVE.value:- raise AdminException(f"User {email} inactive", 403)-- resp = user.to_json()- user.access_token = get_uuid()- login_user(user)- user.update_time = (current_timestamp(),)- user.update_date = (datetime_format(datetime.now()),)- user.last_login_time = get_format_time()- user.save()- msg = "Welcome back!"- return sync_construct_response(data=resp, auth=user.get_id(), message=msg)diff --git a/admin/server/config.py b/admin/server/config.pydeleted file mode 100644index c64ec98a84..0000000000--- a/admin/server/config.py+++ /dev/null@@ -1,524 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#---import logging-import os-import re-import threading-from enum import Enum--from pydantic import BaseModel-from typing import Any-from common.config_utils import read_config-from urllib.parse import urlparse---class BaseConfig(BaseModel):- id: int- name: str- host: str- port: int- service_type: str- detail_func_name: str-- def to_dict(self) -> dict[str, Any]:- return {"id": self.id, "name": self.name, "host": self.host, "port": self.port, "service_type": self.service_type}---class ServiceConfigs:- configs = list[BaseConfig]-- def __init__(self):- self.configs = []- self.lock = threading.Lock()---SERVICE_CONFIGS = ServiceConfigs-# The Admin service list reads the same service_conf/local.service_conf files,-# but the GaussDB metadata connection does not come from those files because-# their top-level gaussdb section belongs to DocEngine and Memory Store. This-# lightweight parser keeps Admin's metadata display aligned with the main-# service DATABASE settings instead of showing an inactive mysql/postgres block.-GAUSSDB_ENV_DEFAULTS = {- "name": "rag_flow",- "user": "rag_flow",- "password": "infini_rag_flow",- "host": "gaussdb",- "port": 8000,- "schema": "public",-}-_GAUSSDB_SCHEMA_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")---def normalize_database_type(database_type: str | None = None) -> str:- # Only normalize the new GaussDB values. Existing metadata database- # selection retains the upstream behavior.- raw_value = database_type or "mysql"- normalized = raw_value.strip().lower()- if normalized in {"gaussdb", "gauss"}:- return "gaussdb"- return raw_value---def _get_int_env(name: str, default: int) -> int:- raw_value = os.environ.get(name)- if raw_value is None:- return default- try:- return int(raw_value)- except (TypeError, ValueError):- logging.warning("Ignoring invalid %s=%r; using default %d", name, raw_value, default)- return default---def _normalize_gaussdb_metadata_schema(value: str | None = None) -> str:- # Admin only displays the schema and does not create the connection, but it- # must apply the same validation. Otherwise an invalid- # GAUSSDB_METADATA_SCHEMA could look valid in Admin while the main service- # fails during startup.- schema = (value or GAUSSDB_ENV_DEFAULTS["schema"]).strip() or GAUSSDB_ENV_DEFAULTS["schema"]- if not _GAUSSDB_SCHEMA_PATTERN.match(schema):- raise ValueError(f"invalid GAUSSDB_METADATA_SCHEMA: {schema}")- return schema---def _gaussdb_env_config() -> dict[str, Any]:- # This configuration is only for Admin display and health checks; it does- # not create the application ORM connection. Keep the flat metadata fields- # aligned with common.settings._gaussdb_env_config() so both surfaces report- # the same connection information.- return {- "name": os.environ.get("GAUSSDB_METADATA_DBNAME", GAUSSDB_ENV_DEFAULTS["name"]),- "user": os.environ.get("GAUSSDB_METADATA_USER", GAUSSDB_ENV_DEFAULTS["user"]),- "password": os.environ.get("GAUSSDB_METADATA_PASSWORD", GAUSSDB_ENV_DEFAULTS["password"]),- "host": os.environ.get("GAUSSDB_METADATA_HOST", GAUSSDB_ENV_DEFAULTS["host"]),- "port": _get_int_env("GAUSSDB_METADATA_PORT", GAUSSDB_ENV_DEFAULTS["port"]),- "schema": _normalize_gaussdb_metadata_schema(os.environ.get("GAUSSDB_METADATA_SCHEMA")),- }---class ServiceType(Enum):- METADATA = "metadata"- RETRIEVAL = "retrieval"- MESSAGE_QUEUE = "message_queue"- RAGFLOW_SERVER = "ragflow_server"- TASK_EXECUTOR = "task_executor"- FILE_STORE = "file_store"---class MetaConfig(BaseConfig):- meta_type: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["meta_type"] = self.meta_type- result["extra"] = extra_dict- return result---class MySQLConfig(MetaConfig):- username: str- password: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["username"] = self.username- extra_dict["password"] = self.password- result["extra"] = extra_dict- return result---class GaussDBMetadataConfig(MySQLConfig):- metadata_schema: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- result["extra"]["schema"] = self.metadata_schema- result["extra"]["password"] = "*" * 8- return result---class PostgresConfig(MetaConfig):- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- return result---class RetrievalConfig(BaseConfig):- retrieval_type: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["retrieval_type"] = self.retrieval_type- result["extra"] = extra_dict- return result---class GaussDBRetrievalConfig(RetrievalConfig):- database: str- retrieval_schema: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- extra_dict = result["extra"].copy()- extra_dict.update({"database": self.database, "schema": self.retrieval_schema})- result["extra"] = extra_dict- return result---class InfinityConfig(RetrievalConfig):- db_name: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["db_name"] = self.db_name- result["extra"] = extra_dict- return result---class ElasticsearchConfig(RetrievalConfig):- username: str- password: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["username"] = self.username- extra_dict["password"] = self.password- result["extra"] = extra_dict- return result---class MessageQueueConfig(BaseConfig):- mq_type: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["mq_type"] = self.mq_type- result["extra"] = extra_dict- return result---class RedisConfig(MessageQueueConfig):- database: int- password: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["database"] = self.database- extra_dict["password"] = self.password- result["extra"] = extra_dict- return result---class RabbitMQConfig(MessageQueueConfig):- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- return result---class RAGFlowServerConfig(BaseConfig):- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- return result---class TaskExecutorConfig(BaseConfig):- message_queue_type: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- result["extra"]["message_queue_type"] = self.message_queue_type- return result---class FileStoreConfig(BaseConfig):- store_type: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["store_type"] = self.store_type- result["extra"] = extra_dict- return result---class MinioConfig(FileStoreConfig):- user: str- password: str-- def to_dict(self) -> dict[str, Any]:- result = super().to_dict()- if "extra" not in result:- result["extra"] = dict()- extra_dict = result["extra"].copy()- extra_dict["user"] = self.user- extra_dict["password"] = self.password- result["extra"] = extra_dict- return result---def load_configurations(config_path: str) -> list[BaseConfig]:- raw_configs = read_config(config_path)- configurations = []- ragflow_count = 0- id_count = 0- metadata_db_type = normalize_database_type(os.getenv("DB_TYPE", "mysql"))- for k, v in raw_configs.items():- match k:- case "ragflow":- name: str = f"ragflow_{ragflow_count}"- host: str = v["host"]- http_port: int = v["http_port"]- config = RAGFlowServerConfig(id=id_count, name=name, host=host, port=http_port, service_type="ragflow_server", detail_func_name="check_ragflow_server_alive")- configurations.append(config)- id_count += 1- case "es":- name: str = "elasticsearch"- url = v["hosts"]- parsed = urlparse(url)- host: str = parsed.hostname- port: int = parsed.port- username: str = v.get("username")- password: str = v.get("password")- config = ElasticsearchConfig(- id=id_count,- name=name,- host=host,- port=port,- service_type="retrieval",- retrieval_type="elasticsearch",- username=username,- password=password,- detail_func_name="get_es_cluster_stats",- )- configurations.append(config)- id_count += 1-- case "infinity":- name: str = "infinity"- url = v["uri"]- parts = url.split(":", 1)- host = parts[0]- port = int(parts[1])- database: str = v.get("db_name", "default_db")- config = InfinityConfig(id=id_count, name=name, host=host, port=port, service_type="retrieval", retrieval_type="infinity", db_name=database, detail_func_name="get_infinity_status")- configurations.append(config)- id_count += 1- case "minio_0":- name: str = "minio_0"- url = v["host"]- parts = url.split(":", 1)- host = parts[0]- port = int(parts[1])- user = v.get("user")- password = v.get("password")- config = MinioConfig(id=id_count, name=name, host=host, port=port, user=user, password=password, service_type="file_store", store_type="minio", detail_func_name="check_minio_alive")- configurations.append(config)- id_count += 1- case "minio":- name: str = "minio"- url = v["host"]- parts = url.split(":", 1)- host = parts[0]- port = int(parts[1])- user = v.get("user")- password = v.get("password")- config = MinioConfig(id=id_count, name=name, host=host, port=port, user=user, password=password, service_type="file_store", store_type="minio", detail_func_name="check_minio_alive")- configurations.append(config)- id_count += 1- case "s3":- # AWS S3 (or any S3-compatible service: MinIO, R2, ...).- # The config block uses `endpoint_url` instead of `host:port`,- # so parse the URL to derive host/port for the status page.- name: str = "s3"- endpoint_url = v.get("endpoint_url") or ""- if endpoint_url:- try:- parsed = urlparse(endpoint_url)- # `parsed.port` raises ValueError on non-numeric or- # out-of-range ports; `urlparse` itself raises- # ValueError on malformed IPv6 URLs. Fall back to- # the raw endpoint as host and the scheme's- # default port so config loading completes.- host: str = parsed.hostname or endpoint_url- port: int = parsed.port or (443 if parsed.scheme == "https" else 80)- logging.debug(- "Selected S3 host=%s port=%d for endpoint %r.",- host,- port,- endpoint_url,- )- except ValueError:- logging.warning(- "Could not parse S3 endpoint_url %r; using raw value as host with default port.",- endpoint_url,- )- host = endpoint_url- port = 443 if endpoint_url.startswith("https://") else 80- else:- host: str = "s3.amazonaws.com"- port: int = 443- logging.debug("No S3 endpoint_url configured; defaulting to AWS S3 at %s:%d.", host, port)- config = FileStoreConfig(- id=id_count,- name=name,- host=host,- port=port,- service_type="file_store",- store_type="s3",- detail_func_name="check_s3_alive",- )- configurations.append(config)- id_count += 1- case "redis":- name: str = "redis"- url = v["host"]- parts = url.split(":", 1)- host = parts[0]- port = int(parts[1])- password = v.get("password")- db: int = v.get("db")- config = RedisConfig(id=id_count, name=name, host=host, port=port, password=password, database=db, service_type="message_queue", mq_type="redis", detail_func_name="get_redis_info")- configurations.append(config)- id_count += 1- case "mysql":- if metadata_db_type == "gaussdb":- continue- name: str = "mysql"- host: str = v.get("host")- port: int = v.get("port")- username = v.get("user")- password = v.get("password")- config = MySQLConfig(- id=id_count,- name=name,- host=host,- port=port,- username=username,- password=password,- service_type="meta_data",- meta_type="mysql",- detail_func_name="get_mysql_status",- )- configurations.append(config)- id_count += 1- case "gaussdb":- # The gaussdb block in the configuration file belongs to- # DocEngine and Memory Store, not the DB_TYPE=gaussdb metadata- # connection. Add metadata GaussDB separately from- # GAUSSDB_METADATA_* after the loop so Admin does not merge the- # two services.- doc_config = v- host = doc_config.get("host")- port = doc_config.get("port")- if not host or port in (None, ""):- # service_conf.yaml.template always contains a GaussDB- # placeholder block. Default deployments leave it empty;- # do not let an inactive DocEngine config break Admin.- continue- try:- port = int(port)- except (TypeError, ValueError):- logging.warning("Ignoring invalid GaussDB DocEngine port in Admin configuration")- continue- config = GaussDBRetrievalConfig(- id=id_count,- name="gaussdb",- host=host,- port=port,- database=doc_config.get("database"),- retrieval_schema=doc_config.get("schema") or "public",- service_type="retrieval",- retrieval_type="gaussdb",- detail_func_name="get_gaussdb_status",- )- configurations.append(config)- id_count += 1- case "admin":- pass- case "task_executor":- name: str = "task_executor"- host: str = v.get("host", "")- port: int = v.get("port", 0)- message_queue_type: str = v.get("message_queue_type")- config = TaskExecutorConfig(- id=id_count, name=name, host=host, port=port, message_queue_type=message_queue_type, service_type="task_executor", detail_func_name="check_task_executor_alive"- )- configurations.append(config)- id_count += 1- case "rabbitmq":- name: str = "rabbitmq"- host: str = v.get("host")- port: int = v.get("port")- config = RabbitMQConfig(id=id_count, name=name, host=host, port=port, service_type="message_queue", mq_type="rabbitmq", detail_func_name="check_rabbitmq_alive")- configurations.append(config)- id_count += 1- case _:- logging.warning(f"Unknown configuration key: {k}")- continue-- if metadata_db_type == "gaussdb":- # Display the metadata database from the same GAUSSDB_METADATA_*- # variables as the main service, never from the DocEngine/Memory Store- # GAUSSDB_* variables.- v = _gaussdb_env_config()- config = GaussDBMetadataConfig(- id=id_count,- name="gaussdb",- host=v.get("host"),- port=v.get("port"),- username=v.get("user"),- password=v.get("password"),- metadata_schema=v.get("schema"),- service_type="meta_data",- meta_type="gaussdb",- detail_func_name="get_database_status",- )- configurations.append(config)-- return configurationsdiff --git a/admin/server/exceptions.py b/admin/server/exceptions.pydeleted file mode 100644index 81fd800114..0000000000--- a/admin/server/exceptions.py+++ /dev/null@@ -1,20 +0,0 @@-class AdminException(Exception):- def __init__(self, message, code=400):- super().__init__(message)- self.code = code- self.message = message---class UserNotFoundError(AdminException):- def __init__(self, username):- super().__init__(f"User '{username}' not found", 404)---class UserAlreadyExistsError(AdminException):- def __init__(self, username):- super().__init__(f"User '{username}' already exists", 409)---class CannotDeleteAdminError(AdminException):- def __init__(self):- super().__init__("Cannot delete admin account", 403)diff --git a/admin/server/models.py b/admin/server/models.pydeleted file mode 100644index 177b91dd05..0000000000--- a/admin/server/models.py+++ /dev/null@@ -1,15 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#diff --git a/admin/server/responses.py b/admin/server/responses.pydeleted file mode 100644index 1264ed0a0c..0000000000--- a/admin/server/responses.py+++ /dev/null@@ -1,24 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-from flask import jsonify---def success_response(data=None, message="Success", code=0):- return jsonify({"code": code, "message": message, "data": data}), 200---def error_response(message="Error", code=-1, data=None):- return jsonify({"code": code, "message": message, "data": data}), 400diff --git a/admin/server/roles.py b/admin/server/roles.pydeleted file mode 100644index ac04179f06..0000000000--- a/admin/server/roles.py+++ /dev/null@@ -1,76 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-import logging--from typing import Dict, Any--from api.common.exceptions import AdminException---class RoleMgr:- @staticmethod- def create_role(role_name: str, description: str):- error_msg = f"not implement: create role: {role_name}, description: {description}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def update_role_description(role_name: str, description: str) -> Dict[str, Any]:- error_msg = f"not implement: update role: {role_name} with description: {description}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def delete_role(role_name: str) -> Dict[str, Any]:- error_msg = f"not implement: drop role: {role_name}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def list_roles() -> Dict[str, Any]:- error_msg = "not implement: list roles"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def get_role_permission(role_name: str) -> Dict[str, Any]:- error_msg = f"not implement: show role {role_name}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def grant_role_permission(role_name: str, actions: list, resource: str) -> Dict[str, Any]:- error_msg = f"not implement: grant role {role_name} actions: {actions} on {resource}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def revoke_role_permission(role_name: str, actions: list, resource: str) -> Dict[str, Any]:- error_msg = f"not implement: revoke role {role_name} actions: {actions} on {resource}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def update_user_role(user_name: str, role_name: str) -> Dict[str, Any]:- error_msg = f"not implement: update user role: {user_name} to role {role_name}"- logging.error(error_msg)- raise AdminException(error_msg)-- @staticmethod- def get_user_permission(user_name: str) -> Dict[str, Any]:- error_msg = f"not implement: get user permission: {user_name}"- logging.error(error_msg)- raise AdminException(error_msg)diff --git a/admin/server/routes.py b/admin/server/routes.pydeleted file mode 100644index 859471fa01..0000000000--- a/admin/server/routes.py+++ /dev/null@@ -1,684 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import secrets-import logging-from typing import Any--from common.time_utils import current_timestamp, datetime_format-from datetime import datetime-from flask import Blueprint, Response, request-from flask_login import current_user, login_required, logout_user--from auth import login_admin, check_admin_auth-from responses import success_response, error_response-from services import UserMgr, ServiceMgr, UserServiceMgr, SettingsMgr, ConfigMgr, EnvironmentsMgr, SandboxMgr-from roles import RoleMgr-from api.common.exceptions import AdminException-from common.versions import get_ragflow_version-from api.utils.api_utils import generate_confirmation_token-from common.log_utils import get_log_levels, set_log_level--admin_bp = Blueprint("admin", __name__, url_prefix="/api/v1/admin")---@admin_bp.route("/ping", methods=["GET"])-def ping():- return success_response(message="pong")---@admin_bp.route("/login", methods=["POST"])-def login():- if not request.json:- return error_response("Authorize admin failed.", 400)- try:- email = request.json.get("email", "")- password = request.json.get("password", "")- return login_admin(email, password)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/logout", methods=["POST"])-@login_required-def logout():- try:- current_user.access_token = f"INVALID_{secrets.token_hex(16)}"- current_user.save()- logout_user()- return success_response(True)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users", methods=["GET"])-@login_required-@check_admin_auth-def list_users():- try:- users = UserMgr.get_all_users()- return success_response(users, "Get all users", 0)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users", methods=["POST"])-@login_required-@check_admin_auth-def create_user():- try:- data = request.get_json()- if not data or "username" not in data or "password" not in data:- return error_response("Username and password are required", 400)-- username = data["username"]- password = data["password"]- role = data.get("role", "user")-- res = UserMgr.create_user(username, password, role)- if res["success"]:- user_info = res["user_info"]- user_info.pop("password") # do not return password- return success_response(user_info, "User created successfully")- else:- return error_response("create user failed")-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e))---@admin_bp.route("/users/<username>", methods=["DELETE"])-@login_required-@check_admin_auth-def delete_user(username):- try:- res = UserMgr.delete_user(username)- if res["success"]:- return success_response(None, res["message"])- else:- return error_response(res["message"])-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/password", methods=["PUT"])-@login_required-@check_admin_auth-def change_password(username):- try:- data = request.get_json()- if not data or "new_password" not in data:- return error_response("New password is required", 400)-- new_password = data["new_password"]- msg = UserMgr.update_user_password(username, new_password)- return success_response(None, msg)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/activate", methods=["PUT"])-@login_required-@check_admin_auth-def alter_user_activate_status(username):- try:- data = request.get_json()- if current_user.email == username:- return error_response(f"can't alter current user status: {username}", 409)- if not data or "activate_status" not in data:- return error_response("Activation status is required", 400)- activate_status = data["activate_status"]- msg = UserMgr.update_user_activate_status(username, activate_status)- return success_response(None, msg)- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/admin", methods=["PUT"])-@login_required-@check_admin_auth-def grant_admin(username):- try:- if current_user.email == username:- return error_response(f"can't grant current user: {username}", 409)- msg = UserMgr.grant_admin(username)- return success_response(None, msg)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/admin", methods=["DELETE"])-@login_required-@check_admin_auth-def revoke_admin(username):- try:- if current_user.email == username:- return error_response(f"can't grant current user: {username}", 409)- msg = UserMgr.revoke_admin(username)- return success_response(None, msg)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>", methods=["GET"])-@login_required-@check_admin_auth-def get_user_details(username):- try:- user_details = UserMgr.get_user_details(username)- return success_response(user_details)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/datasets", methods=["GET"])-@login_required-@check_admin_auth-def get_user_datasets(username):- try:- datasets_list = UserServiceMgr.get_user_datasets(username)- return success_response(datasets_list)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/agents", methods=["GET"])-@login_required-@check_admin_auth-def get_user_agents(username):- try:- agents_list = UserServiceMgr.get_user_agents(username)- return success_response(agents_list)-- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/services", methods=["GET"])-@login_required-@check_admin_auth-def get_services():- try:- services = ServiceMgr.get_all_services()- return success_response(services, "Get all services", 0)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/service_types/<service_type>", methods=["GET"])-@login_required-@check_admin_auth-def get_services_by_type(service_type_str):- try:- services = ServiceMgr.get_services_by_type(service_type_str)- return success_response(services)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/services/<service_id>", methods=["GET"])-@login_required-@check_admin_auth-def get_service(service_id):- try:- services = ServiceMgr.get_service_details(service_id)- return success_response(services)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/services/<service_id>", methods=["DELETE"])-@login_required-@check_admin_auth-def shutdown_service(service_id):- try:- services = ServiceMgr.shutdown_service(service_id)- return success_response(services)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/services/<service_id>", methods=["PUT"])-@login_required-@check_admin_auth-def restart_service(service_id):- try:- services = ServiceMgr.restart_service(service_id)- return success_response(services)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles", methods=["POST"])-@login_required-@check_admin_auth-def create_role():- try:- data = request.get_json()- if not data or "role_name" not in data:- return error_response("Role name is required", 400)- role_name: str = data["role_name"]- description: str = data["description"]- res = RoleMgr.create_role(role_name, description)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles/<role_name>", methods=["PUT"])-@login_required-@check_admin_auth-def update_role(role_name: str):- try:- data = request.get_json()- if not data or "description" not in data:- return error_response("Role description is required", 400)- description: str = data["description"]- res = RoleMgr.update_role_description(role_name, description)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles/<role_name>", methods=["DELETE"])-@login_required-@check_admin_auth-def delete_role(role_name: str):- try:- res = RoleMgr.delete_role(role_name)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles", methods=["GET"])-@login_required-@check_admin_auth-def list_roles():- try:- res = RoleMgr.list_roles()- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles/<role_name>/permission", methods=["GET"])-@login_required-@check_admin_auth-def get_role_permission(role_name: str):- try:- res = RoleMgr.get_role_permission(role_name)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles/<role_name>/permission", methods=["POST"])-@login_required-@check_admin_auth-def grant_role_permission(role_name: str):- try:- data = request.get_json()- if not data or "actions" not in data or "resource" not in data:- return error_response("Permission is required", 400)- actions: list = data["actions"]- resource: str = data["resource"]- res = RoleMgr.grant_role_permission(role_name, actions, resource)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/roles/<role_name>/permission", methods=["DELETE"])-@login_required-@check_admin_auth-def revoke_role_permission(role_name: str):- try:- data = request.get_json()- if not data or "actions" not in data or "resource" not in data:- return error_response("Permission is required", 400)- actions: list = data["actions"]- resource: str = data["resource"]- res = RoleMgr.revoke_role_permission(role_name, actions, resource)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<user_name>/role", methods=["PUT"])-@login_required-@check_admin_auth-def update_user_role(user_name: str):- try:- data = request.get_json()- if not data or "role_name" not in data:- return error_response("Role name is required", 400)- role_name: str = data["role_name"]- res = RoleMgr.update_user_role(user_name, role_name)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<user_name>/permission", methods=["GET"])-@login_required-@check_admin_auth-def get_user_permission(user_name: str):- try:- res = RoleMgr.get_user_permission(user_name)- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/variables", methods=["PUT"])-@login_required-@check_admin_auth-def set_variable():- try:- data = request.get_json()- if not data or "var_name" not in data:- return error_response("Var name is required", 400)-- if "var_value" not in data:- return error_response("Var value is required", 400)- var_name: str = data["var_name"]- var_value: str = data["var_value"]-- SettingsMgr.update_by_name(var_name, var_value)- return success_response(None, "Set variable successfully")- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/variables", methods=["GET"])-@login_required-@check_admin_auth-def get_variable():- try:- if request.content_length is None or request.content_length == 0:- # list variables- res = list(SettingsMgr.get_all())- return success_response(res)-- # get var- data = request.get_json()- if not data or "var_name" not in data:- return error_response("Var name is required", 400)- var_name: str = data["var_name"]- res = SettingsMgr.get_by_name(var_name)- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/configs", methods=["GET"])-@login_required-@check_admin_auth-def get_config():- try:- res = list(ConfigMgr.get_all())- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/environments", methods=["GET"])-@login_required-@check_admin_auth-def get_environments():- try:- res = list(EnvironmentsMgr.get_all())- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/keys", methods=["POST"])-@login_required-@check_admin_auth-def generate_user_api_key(username: str) -> tuple[Response, int]:- try:- user_details: list[dict[str, Any]] = UserMgr.get_user_details(username)- if not user_details:- return error_response("User not found!", 404)- tenants: list[dict[str, Any]] = UserServiceMgr.get_user_tenants(username)- if not tenants:- return error_response("Tenant not found!", 404)- tenant_id: str = tenants[0]["tenant_id"]- key: str = generate_confirmation_token()- obj: dict[str, Any] = {- "tenant_id": tenant_id,- "token": key,- "beta": generate_confirmation_token().replace("ragflow-", "")[:32],- "create_time": current_timestamp(),- "create_date": datetime_format(datetime.now()),- "update_time": None,- "update_date": None,- }-- if not UserMgr.save_api_key(obj):- return error_response("Failed to generate API key!", 500)- return success_response(obj, "API key generated successfully")- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/keys", methods=["GET"])-@login_required-@check_admin_auth-def get_user_api_keys(username: str) -> tuple[Response, int]:- try:- api_keys: list[dict[str, Any]] = UserMgr.get_user_api_key(username)- return success_response(api_keys, "Get user API keys")- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/users/<username>/keys/<key>", methods=["DELETE"])-@login_required-@check_admin_auth-def delete_user_api_key(username: str, key: str) -> tuple[Response, int]:- try:- deleted = UserMgr.delete_api_key(username, key)- if deleted:- return success_response(None, "API key deleted successfully")- else:- return error_response("API key not found or could not be deleted", 404)- except AdminException as e:- return error_response(e.message, e.code)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/version", methods=["GET"])-@login_required-@check_admin_auth-def show_version():- try:- res = {"version": get_ragflow_version()}- return success_response(res)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/sandbox/providers", methods=["GET"])-@login_required-@check_admin_auth-def list_sandbox_providers():- """List all available sandbox providers."""- try:- res = SandboxMgr.list_providers()- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/sandbox/providers/<provider_id>/schema", methods=["GET"])-@login_required-@check_admin_auth-def get_sandbox_provider_schema(provider_id: str):- """Get configuration schema for a specific provider."""- try:- res = SandboxMgr.get_provider_config_schema(provider_id)- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/sandbox/config", methods=["GET"])-@login_required-@check_admin_auth-def get_sandbox_config():- """Get current sandbox configuration."""- try:- res = SandboxMgr.get_config()- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/sandbox/config", methods=["POST"])-@login_required-@check_admin_auth-def set_sandbox_config():- """Set sandbox provider configuration."""- try:- data = request.get_json()- if not data:- logging.error("set_sandbox_config: Request body is required")- return error_response("Request body is required", 400)-- provider_type = data.get("provider_type")- if not provider_type:- logging.error("set_sandbox_config: provider_type is required")- return error_response("provider_type is required", 400)-- config = data.get("config", {})- set_active = data.get("set_active", True) # Default to True for backward compatibility-- logging.info(f"set_sandbox_config: provider_type={provider_type}, set_active={set_active}")- logging.info(f"set_sandbox_config: config keys={list(config.keys())}")-- res = SandboxMgr.set_config(provider_type, config, set_active)- return success_response(res, "Sandbox configuration updated successfully")- except AdminException as e:- logging.exception("set_sandbox_config AdminException")- return error_response(str(e), 400)- except Exception as e:- logging.exception("set_sandbox_config unexpected error")- return error_response(str(e), 500)---@admin_bp.route("/sandbox/test", methods=["POST"])-@login_required-@check_admin_auth-def test_sandbox_connection():- """Test connection to sandbox provider."""- try:- data = request.get_json()- if not data:- return error_response("Request body is required", 400)-- provider_type = data.get("provider_type")- if not provider_type:- return error_response("provider_type is required", 400)-- config = data.get("config", {})- res = SandboxMgr.test_connection(provider_type, config)- return success_response(res)- except AdminException as e:- return error_response(str(e), 400)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/log_levels", methods=["GET"])-@login_required-@check_admin_auth-def get_logger_levels():- """Get current log levels for all packages."""- try:- res = get_log_levels()- return success_response(res, "Get log levels", 0)- except Exception as e:- return error_response(str(e), 500)---@admin_bp.route("/log_levels", methods=["PUT"])-@login_required-@check_admin_auth-def set_logger_level():- """Set log level for a package."""- try:- data = request.get_json()- if not data or "pkg_name" not in data or "level" not in data:- return error_response("pkg_name and level are required", 400)-- pkg_name = data["pkg_name"]- level = data["level"]- if not isinstance(pkg_name, str) or not isinstance(level, str):- return error_response("pkg_name and level must be strings", 400)-- success = set_log_level(pkg_name, level)- if success:- return success_response({"pkg_name": pkg_name, "level": level}, "Log level updated successfully")- else:- return error_response(f"Invalid log level: {level}", 400)- except Exception as e:- return error_response(str(e), 500)diff --git a/admin/server/services.py b/admin/server/services.pydeleted file mode 100644index c7bd352e37..0000000000--- a/admin/server/services.py+++ /dev/null@@ -1,796 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import json-import os-import logging-import re-import secrets-from typing import Any--from werkzeug.security import check_password_hash-from common.constants import ActiveEnum-from api.db.services import UserService-from api.db.joint_services.user_account_service import create_new_user, delete_user_data-from api.db.services.canvas_service import UserCanvasService-from api.db.services.user_service import TenantService, UserTenantService-from api.db.services.knowledgebase_service import KnowledgebaseService-from api.db.services.system_settings_service import SystemSettingsService-from api.db.services.api_service import APITokenService-from api.db.db_models import APIToken-from api.utils.crypt import decrypt-from api.utils import health_utils--from api.common.exceptions import AdminException, UserAlreadyExistsError, UserNotFoundError-from config import SERVICE_CONFIGS---class UserMgr:- @staticmethod- def get_all_users():- users = UserService.get_all_users()- result = []- for user in users:- result.append(- {- "email": user.email,- "nickname": user.nickname,- "create_date": user.create_date,- "is_active": user.is_active,- "is_superuser": user.is_superuser,- }- )- return result-- @staticmethod- def get_user_details(username):- # use email to query- users = UserService.query_user_by_email(username)- result = []- for user in users:- result.append(- {- "avatar": user.avatar,- "email": user.email,- "language": user.language,- "last_login_time": user.last_login_time,- "is_active": user.is_active,- "is_anonymous": user.is_anonymous,- "login_channel": user.login_channel,- "status": user.status,- "is_superuser": user.is_superuser,- "create_date": user.create_date,- "update_date": user.update_date,- }- )- return result-- @staticmethod- def create_user(username, password, role="user") -> dict:- # Validate the email address- if not re.match(r"^[\w\._-]+@([\w_-]+\.)+[\w-]{2,}$", username):- raise AdminException(f"Invalid email address: {username}!")- # Check if the email address is already used- if UserService.query(email=username):- raise UserAlreadyExistsError(username)- # Construct user info data- user_info_dict = {- "email": username,- "nickname": "", # ask user to edit it manually in settings.- "password": decrypt(password),- "login_channel": "password",- "is_superuser": role == "admin",- }- return create_new_user(user_info_dict)-- @staticmethod- def delete_user(username):- # use email to delete- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- if len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- usr = user_list[0]- return delete_user_data(usr.id)-- @staticmethod- def update_user_password(username, new_password) -> str:- # use email to find user. check exist and unique.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- # check new_password different from old.- usr = user_list[0]- psw = decrypt(new_password)- # SSO-provisioned users (OIDC/OAuth) have no local password (usr.password is None):- # skip the equality check, which would otherwise crash inside werkzeug's split().- if usr.password and check_password_hash(usr.password, psw):- return "Same password, no need to update!"- # update password- UserService.update_user_password(usr.id, psw)- return "Password updated successfully!"-- @staticmethod- def update_user_activate_status(username, activate_status: str):- # use email to find user. check exist and unique.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- # check activate status different from new- usr = user_list[0]- # format activate_status before handle- _activate_status = activate_status.lower()- target_status = {- "on": ActiveEnum.ACTIVE.value,- "off": ActiveEnum.INACTIVE.value,- }.get(_activate_status)- if not target_status:- raise AdminException(f"Invalid activate_status: {activate_status}")- if target_status == usr.is_active:- return f"User activate status is already {_activate_status}!"- # update is_active- update_dict = {"is_active": target_status}- if target_status == ActiveEnum.INACTIVE.value:- update_dict["access_token"] = f"INVALID_{secrets.token_hex(16)}"- UserService.update_user(usr.id, update_dict)- return f"Turn {_activate_status} user activate status successfully!"-- @staticmethod- def get_user_api_key(username: str) -> list[dict[str, Any]]:- # use email to find user. check exist and unique.- user_list: list[Any] = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"More than one user with username '{username}' found!")-- usr: Any = user_list[0]- # tenant_id is typically the same as user_id for the owner tenant- tenant_id: str = usr.id-- # Query all API keys for this tenant- api_keys: Any = APITokenService.query(tenant_id=tenant_id)-- result: list[dict[str, Any]] = []- for key in api_keys:- result.append(key.to_dict())-- return result-- @staticmethod- def save_api_key(api_key: dict[str, Any]) -> bool:- return APITokenService.save(**api_key)-- @staticmethod- def delete_api_key(username: str, key: str) -> bool:- # use email to find user. check exist and unique.- user_list: list[Any] = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")-- usr: Any = user_list[0]- # tenant_id is typically the same as user_id for the owner tenant- tenant_id: str = usr.id-- # Delete the API key- deleted_count: int = APITokenService.filter_delete([APIToken.tenant_id == tenant_id, APIToken.token == key])- return deleted_count > 0-- @staticmethod- def grant_admin(username: str):- # use email to find user. check exist and unique.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")-- # check activate status different from new- usr = user_list[0]- if usr.is_superuser:- return f"{usr} is already superuser!"- # update is_active- UserService.update_user(usr.id, {"is_superuser": True})- return "Grant successfully!"-- @staticmethod- def revoke_admin(username: str):- # use email to find user. check exist and unique.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- # check activate status different from new- usr = user_list[0]- if not usr.is_superuser:- return f"{usr} isn't superuser, yet!"- # update is_active- UserService.update_user(usr.id, {"is_superuser": False})- return "Revoke successfully!"---class UserServiceMgr:- @staticmethod- def get_user_datasets(username):- # use email to find user.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- # find tenants- usr = user_list[0]- tenants = TenantService.get_joined_tenants_by_user_id(usr.id)- tenant_ids = [m["tenant_id"] for m in tenants]- # filter permitted kb and owned kb- return KnowledgebaseService.get_all_kb_by_tenant_ids(tenant_ids, usr.id)-- @staticmethod- def get_user_agents(username):- # use email to find user.- user_list = UserService.query_user_by_email(username)- if not user_list:- raise UserNotFoundError(username)- elif len(user_list) > 1:- raise AdminException(f"Exist more than 1 user: {username}!")- # find tenants- usr = user_list[0]- tenants = TenantService.get_joined_tenants_by_user_id(usr.id)- tenant_ids = [m["tenant_id"] for m in tenants]- # filter permitted agents and owned agents- res = UserCanvasService.get_all_agents_by_tenant_ids(tenant_ids, usr.id)- return [{"title": r["title"], "permission": r["permission"], "canvas_category": r["canvas_category"].split("_")[0], "avatar": r["avatar"]} for r in res]-- @staticmethod- def get_user_tenants(email: str) -> list[dict[str, Any]]:- users: list[Any] = UserService.query_user_by_email(email)- if not users:- raise UserNotFoundError(email)- user: Any = users[0]-- tenants: list[dict[str, Any]] = UserTenantService.get_tenants_by_user_id(user.id)- return tenants---class ServiceMgr:- @staticmethod- def get_all_services():- doc_engine = os.getenv("DOC_ENGINE", "elasticsearch")- # Map STORAGE_IMPL (e.g. "AWS_S3", "MINIO", "OSS") to the lowercase- # `store_type` we use in FileStoreConfig.store_type. The "AWS_"- # prefix is stripped so AWS_S3 matches store_type "s3".- storage_impl = os.getenv("STORAGE_IMPL", "MINIO")- active_store_type = storage_impl.lower().removeprefix("aws_")- result = []- configs = SERVICE_CONFIGS.configs- for service_id, config in enumerate(configs):- config_dict = config.to_dict()- if config_dict["service_type"] == "retrieval":- if config_dict["extra"]["retrieval_type"] != doc_engine:- continue- if config_dict["service_type"] == "file_store":- # Only show the file-store backend that's actually active.- # Without this filter, a stale minio entry from service_conf.yaml- # is returned even when STORAGE_IMPL=AWS_S3 (see #17294).- if config_dict.get("extra", {}).get("store_type") != active_store_type:- continue- try:- service_detail = ServiceMgr.get_service_details(service_id)- if "status" in service_detail:- config_dict["status"] = service_detail["status"]- else:- config_dict["status"] = "timeout"- except Exception as e:- logging.warning(f"Can't get service details, error: {e}")- config_dict["status"] = "timeout"- if not config_dict["host"]:- config_dict["host"] = "-"- if not config_dict["port"]:- config_dict["port"] = "-"- result.append(config_dict)- return result-- @staticmethod- def get_services_by_type(service_type_str: str):- raise AdminException("get_services_by_type: not implemented")-- @staticmethod- def get_service_details(service_id: int):- service_idx = int(service_id)- configs = SERVICE_CONFIGS.configs- if service_idx < 0 or service_idx >= len(configs):- raise AdminException(f"invalid service_index: {service_idx}")-- service_config = configs[service_idx]-- # exclude retrieval service if retrieval_type is not matched- doc_engine = os.getenv("DOC_ENGINE", "elasticsearch")- if service_config.service_type == "retrieval":- if service_config.retrieval_type != doc_engine:- raise AdminException(f"invalid service_index: {service_idx}")-- service_info = {"name": service_config.name, "detail_func_name": service_config.detail_func_name}-- detail_func = getattr(health_utils, service_info.get("detail_func_name"))- res = detail_func()- res.update({"service_name": service_info.get("name")})- return res-- @staticmethod- def shutdown_service(service_id: int):- raise AdminException("shutdown_service: not implemented")-- @staticmethod- def restart_service(service_id: int):- raise AdminException("restart_service: not implemented")---class SettingsMgr:- @staticmethod- def _format_setting(setting):- return {- "data_type": setting.data_type,- "name": setting.name,- "setting_type": "config",- "value": setting.value,- }-- @staticmethod- def _validate_value(name: str, data_type: str, value: str):- data_type = data_type.lower()- value = str(value)- if data_type == "string":- return- if data_type == "integer":- try:- int(value)- except ValueError:- raise AdminException(f"Invalid integer value for {name}: {value}")- return- if data_type in {"bool", "boolean"}:- if value not in {"true", "false"}:- raise AdminException(f"Invalid bool value for {name}: expected true or false")- return- if data_type == "json":- try:- json.loads(value)- except json.JSONDecodeError:- raise AdminException(f"Invalid JSON value for {name}")- return- raise AdminException(f"Unsupported data type for {name}: {data_type}")-- @staticmethod- def _infer_data_type(name: str):- if name.startswith("sandbox."):- return "json"- if name.endswith(".enabled"):- return "bool"- return "string"-- @staticmethod- def get_all():- settings = SystemSettingsService.get_all(reverse=False, order_by="name")- result = []- for setting in settings:- result.append(SettingsMgr._format_setting(setting))- return result-- @staticmethod- def get_by_name(name: str):- settings = SystemSettingsService.get_by_name(name)- if len(settings) == 0:- settings = SystemSettingsService.get_by_name_prefix(name)- if len(settings) == 0:- raise AdminException(f"Can't get setting: {name}")- result = []- for setting in settings:- result.append(SettingsMgr._format_setting(setting))- return result-- @staticmethod- def update_by_name(name: str, value: str):- settings = SystemSettingsService.get_by_name(name)- if len(settings) == 1:- setting = settings[0]- SettingsMgr._validate_value(name, setting.data_type, value)- setting.value = value- setting_dict = setting.to_dict()- SystemSettingsService.update_by_name(name, setting_dict)- elif len(settings) > 1:- raise AdminException(f"Can't update more than 1 setting: {name}")- else:- # Create new setting if it doesn't exist-- # Determine data_type based on name and value- data_type = SettingsMgr._infer_data_type(name)- SettingsMgr._validate_value(name, data_type, value)-- new_setting = {- "name": name,- "value": str(value),- "source": "admin",- "data_type": data_type,- }- SystemSettingsService.save(**new_setting)---class ConfigMgr:- @staticmethod- def get_all():- result = []- configs = SERVICE_CONFIGS.configs- for config in configs:- config_dict = config.to_dict()- result.append(config_dict)- return result---class EnvironmentsMgr:- @staticmethod- def get_all():- result = []-- env_kv = {"env": "DOC_ENGINE", "value": os.getenv("DOC_ENGINE")}- result.append(env_kv)-- env_kv = {"env": "DEFAULT_SUPERUSER_EMAIL", "value": os.getenv("DEFAULT_SUPERUSER_EMAIL", "[email protected]")}- result.append(env_kv)-- env_kv = {"env": "DB_TYPE", "value": os.getenv("DB_TYPE", "mysql")}- result.append(env_kv)-- env_kv = {"env": "DEVICE", "value": os.getenv("DEVICE", "cpu")}- result.append(env_kv)-- env_kv = {"env": "STORAGE_IMPL", "value": os.getenv("STORAGE_IMPL", "MINIO")}- result.append(env_kv)-- return result---class SandboxMgr:- """Manager for sandbox provider configuration and operations."""-- # Provider registry with metadata- PROVIDER_REGISTRY = {- "local": {- "name": "Local",- "description": "Execute code directly on the current host process.",- "tags": ["local", "host", "minimal"],- },- "self_managed": {- "name": "Self-Managed",- "description": "On-premise deployment using Daytona/Docker",- "tags": ["self-hosted", "low-latency", "secure"],- },- "ssh": {- "name": "SSH",- "description": "Execute code on a remote machine over SSH.",- "tags": ["remote", "ssh", "custom-runtime"],- },- "aliyun_codeinterpreter": {- "name": "Aliyun Code Interpreter",- "description": "Aliyun Function Compute Code Interpreter - Code execution in serverless microVMs",- "tags": ["saas", "cloud", "scalable", "aliyun"],- },- "e2b": {- "name": "E2B",- "description": "E2B Cloud - Code Execution Sandboxes",- "tags": ["saas", "fast", "global"],- },- "tenki": {- "name": "Tenki",- "description": "Tenki - Disposable microVM code sandboxes",- "tags": ["saas", "cloud", "microvm", "isolated"],- },- "ucloud_agent_sandbox": {- "name": "UCloud Agent Sandbox",- "description": "UCloud Agent Sandbox - Disposable cloud sandboxes for agent code execution",- "tags": ["saas", "cloud", "isolated", "ucloud"],- },- }-- @staticmethod- def list_providers():- """List all available sandbox providers."""- result = []- for provider_id, metadata in SandboxMgr.PROVIDER_REGISTRY.items():- result.append({"id": provider_id, **metadata})- return result-- @staticmethod- def get_provider_config_schema(provider_id: str):- """Get configuration schema for a specific provider."""- from agent.sandbox.providers import (- LocalProvider,- SelfManagedProvider,- SSHProvider,- AliyunCodeInterpreterProvider,- E2BProvider,- TenkiProvider,- UCloudAgentSandboxProvider,- )-- schemas = {- "local": LocalProvider.get_config_schema(),- "self_managed": SelfManagedProvider.get_config_schema(),- "ssh": SSHProvider.get_config_schema(),- "aliyun_codeinterpreter": AliyunCodeInterpreterProvider.get_config_schema(),- "e2b": E2BProvider.get_config_schema(),- "tenki": TenkiProvider.get_config_schema(),- "ucloud_agent_sandbox": UCloudAgentSandboxProvider.get_config_schema(),- }-- if provider_id not in schemas:- raise AdminException(f"Unknown provider: {provider_id}")-- return schemas.get(provider_id, {})-- @staticmethod- def get_config():- """Get current sandbox configuration."""- try:- # Get active provider type- provider_type_settings = SystemSettingsService.get_by_name("sandbox.provider_type")- if not provider_type_settings:- provider_type = "self_managed"- else:- provider_type = provider_type_settings[0].value-- # Get provider-specific config- provider_config_settings = SystemSettingsService.get_by_name(f"sandbox.{provider_type}")- if not provider_config_settings:- provider_config = {}- else:- try:- provider_config = json.loads(provider_config_settings[0].value)- except json.JSONDecodeError:- provider_config = {}-- if not provider_config:- schema = SandboxMgr.get_provider_config_schema(provider_type)- provider_config = {}- for field_name, field_schema in schema.items():- if field_schema.get("readonly"):- continue- if field_schema.get("default") is not None:- provider_config[field_name] = field_schema["default"]-- return {- "provider_type": provider_type,- "config": provider_config,- }- except Exception as e:- raise AdminException(f"Failed to get sandbox config: {str(e)}")-- @staticmethod- def set_config(provider_type: str, config: dict, set_active: bool = True):- """- Set sandbox provider configuration.-- Args:- provider_type: Provider identifier (e.g., "self_managed", "e2b")- config: Provider configuration dictionary- set_active: If True, also update the active provider. If False,- only update the configuration without switching providers.- Default: True-- Returns:- Dictionary with updated provider_type and config- """- from agent.sandbox.providers import (- LocalProvider,- SelfManagedProvider,- SSHProvider,- AliyunCodeInterpreterProvider,- E2BProvider,- TenkiProvider,- UCloudAgentSandboxProvider,- )-- try:- # Validate provider type- if provider_type not in SandboxMgr.PROVIDER_REGISTRY:- raise AdminException(f"Unknown provider type: {provider_type}")-- # Get provider schema for validation- schema = SandboxMgr.get_provider_config_schema(provider_type)-- # Validate config against schema- for field_name, field_schema in schema.items():- if field_schema.get("required", False) and field_name not in config:- raise AdminException(f"Required field '{field_name}' is missing")-- # Type validation- if field_name in config:- field_type = field_schema.get("type")- if field_type == "integer":- if not isinstance(config[field_name], int):- raise AdminException(f"Field '{field_name}' must be an integer")- elif field_type == "string":- if not isinstance(config[field_name], str):- raise AdminException(f"Field '{field_name}' must be a string")- elif field_type == "boolean":- if not isinstance(config[field_name], bool):- raise AdminException(f"Field '{field_name}' must be a boolean")-- # Range validation for integers- if field_type == "integer" and field_name in config:- min_val = field_schema.get("min")- max_val = field_schema.get("max")- if min_val is not None and config[field_name] < min_val:- raise AdminException(f"Field '{field_name}' must be >= {min_val}")- if max_val is not None and config[field_name] > max_val:- raise AdminException(f"Field '{field_name}' must be <= {max_val}")-- # Provider-specific custom validation- provider_classes = {- "local": LocalProvider,- "self_managed": SelfManagedProvider,- "ssh": SSHProvider,- "aliyun_codeinterpreter": AliyunCodeInterpreterProvider,- "e2b": E2BProvider,- "tenki": TenkiProvider,- "ucloud_agent_sandbox": UCloudAgentSandboxProvider,- }- provider = provider_classes[provider_type]()- is_valid, error_msg = provider.validate_config(config)- if not is_valid:- raise AdminException(f"Provider validation failed: {error_msg}")-- # Update provider_type only if set_active is True- if set_active:- SettingsMgr.update_by_name("sandbox.provider_type", provider_type)-- # Always update the provider config- config_json = json.dumps(config)- SettingsMgr.update_by_name(f"sandbox.{provider_type}", config_json)- from agent.sandbox.client import reload_provider-- reload_provider()-- return {"provider_type": provider_type, "config": config}- except AdminException:- raise- except Exception as e:- raise AdminException(f"Failed to set sandbox config: {str(e)}")-- @staticmethod- def test_connection(provider_type: str, config: dict):- """- Test connection to sandbox provider by executing a simple Python script.-- This creates a temporary sandbox instance and runs a test code to verify:- - Connection credentials are valid- - Sandbox can be created- - Code execution works correctly-- Args:- provider_type: Provider identifier- config: Provider configuration dictionary-- Returns:- dict with test results including stdout, stderr, exit_code, execution_time- """- try:- from agent.sandbox.providers import (- LocalProvider,- SelfManagedProvider,- SSHProvider,- AliyunCodeInterpreterProvider,- E2BProvider,- TenkiProvider,- UCloudAgentSandboxProvider,- )-- # Instantiate provider based on type- provider_classes = {- "local": LocalProvider,- "self_managed": SelfManagedProvider,- "ssh": SSHProvider,- "aliyun_codeinterpreter": AliyunCodeInterpreterProvider,- "e2b": E2BProvider,- "tenki": TenkiProvider,- "ucloud_agent_sandbox": UCloudAgentSandboxProvider,- }-- if provider_type not in provider_classes:- raise AdminException(f"Unknown provider type: {provider_type}")-- provider = provider_classes[provider_type]()-- # Initialize with config- if not provider.initialize(config):- raise AdminException(f"Failed to initialize provider '{provider_type}'")-- # Create a temporary sandbox instance for testing- instance = provider.create_instance(template="python")- if not instance:- raise AdminException("Failed to create sandbox instance.")-- try:- # Keep the probe close to the original coverage, but avoid- # `sys` because the sandbox security analyzer blocks it.- test_code = """-import json-import math---def main() -> dict:- left = 2- right = 2- print(f"2 + 2 = {left + right}")- print(f"JSON dump: {json.dumps({'test': 'data', 'value': 123})}")- print(f"Math.sqrt(16) = {math.sqrt(16)}")- print("TEST_PASSED")- return {"ok": True, "provider_test": "TEST_PASSED"}-"""-- # Execute test code with timeout- execution_result = provider.execute_code(- instance_id=instance.instance_id,- code=test_code,- language="python",- timeout=10,- )- finally:- try:- provider.destroy_instance(instance.instance_id)- logging.info(f"Cleaned up test instance {instance.instance_id}")- except Exception as cleanup_error:- logging.warning(f"Failed to cleanup test instance {instance.instance_id}: {cleanup_error}")-- # Build detailed result message- success = execution_result.exit_code == 0 and "TEST_PASSED" in execution_result.stdout-- message_parts = [f"Test {success and 'PASSED' or 'FAILED'}", f"Exit code: {execution_result.exit_code}", f"Execution time: {execution_result.execution_time:.2f}s"]-- if execution_result.stdout.strip():- stdout_preview = execution_result.stdout.strip()[:200]- message_parts.append(f"Output: {stdout_preview}...")-- if execution_result.stderr.strip():- stderr_preview = execution_result.stderr.strip()[:200]- message_parts.append(f"Errors: {stderr_preview}...")-- message = " | ".join(message_parts)-- return {- "success": success,- "message": message,- "details": {- "exit_code": execution_result.exit_code,- "execution_time": execution_result.execution_time,- "stdout": execution_result.stdout,- "stderr": execution_result.stderr,- },- }-- except AdminException:- raise- except Exception as e:- import traceback-- error_details = traceback.format_exc()- raise AdminException(f"Connection test failed: {str(e)}\\n\\nStack trace:\\n{error_details}")diff --git a/mcp/client/client.py b/mcp/client/client.pydeleted file mode 100644index 3c54ea0309..0000000000--- a/mcp/client/client.py+++ /dev/null@@ -1,47 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#---from mcp.client.session import ClientSession-from mcp.client.sse import sse_client---async def main():- try:- # To access RAGFlow server in `host` mode, you need to attach `api_key` for each request to indicate identification.- # async with sse_client("http://localhost:9382/sse", headers={"api_key": "ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:- # Or follow the requirements of OAuth 2.1 Section 5 with Authorization header- # async with sse_client("http://localhost:9382/sse", headers={"Authorization": "Bearer ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:-- async with sse_client("http://localhost:9382/sse") as streams:- async with ClientSession(- streams[0],- streams[1],- ) as session:- await session.initialize()- tools = await session.list_tools()- print(f"{tools.tools=}")- response = await session.call_tool(name="ragflow_retrieval", arguments={"dataset_ids": ["ce3bb17cf27a11efa69751e139332ced"], "document_ids": [], "question": "How to install neovim?"})- print(f"Tool response: {response.model_dump()}")-- except Exception as e:- print(e)---if __name__ == "__main__":- from anyio import run-- run(main)diff --git a/mcp/client/streamable_http_client.py b/mcp/client/streamable_http_client.pydeleted file mode 100644index d6f317c5b2..0000000000--- a/mcp/client/streamable_http_client.py+++ /dev/null@@ -1,40 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-from mcp import ClientSession-from mcp.client.streamable_http import streamablehttp_client---async def main():- try:- # To access RAGFlow server in `host` mode, you need to attach `api_key` for each request to indicate identification.- # async with streamablehttp_client("http://localhost:9382/mcp/", headers={"api_key": "ragflow-fixS-TicrohljzFkeLLWIaVhW7XlXPXIUW5solFor6o"}) as (read_stream, write_stream, _):- # Or follow the requirements of OAuth 2.1 Section 5 with Authorization header- # async with streamablehttp_client("http://localhost:9382/mcp/", headers={"Authorization": "Bearer ragflow-fixS-TicrohljzFkeLLWIaVhW7XlXPXIUW5solFor6o"}) as (read_stream, write_stream, _):- async with streamablehttp_client("http://localhost:9382/mcp/") as (read_stream, write_stream, _):- async with ClientSession(read_stream, write_stream) as session:- await session.initialize()- tools = await session.list_tools()- print(f"{tools.tools=}")- response = await session.call_tool(name="ragflow_retrieval", arguments={"dataset_ids": ["bc4177924a7a11f09eff238aa5c10c94"], "document_ids": [], "question": "How to install neovim?"})- print(f"Tool response: {response.model_dump()}")- except Exception as e:- print(e)---if __name__ == "__main__":- from anyio import run-- run(main)diff --git a/mcp/server/server.py b/mcp/server/server.pydeleted file mode 100644index 1fafcf857b..0000000000--- a/mcp/server/server.py+++ /dev/null@@ -1,958 +0,0 @@-#-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import json-import logging-import random-import time-from collections import OrderedDict-from collections.abc import AsyncIterator-from contextlib import asynccontextmanager-from functools import wraps-from typing import Any--import click-import httpx-import mcp.types as types-from mcp.server.lowlevel import Server-from starlette.applications import Starlette-from starlette.middleware import Middleware-from starlette.responses import JSONResponse, Response-from starlette.routing import Mount, Route-from enum import StrEnum---class LaunchMode(StrEnum):- SELF_HOST = "self-host"- HOST = "host"---class Transport(StrEnum):- SSE = "sse"- STEAMABLE_HTTP = "streamable-http"---BASE_URL = "http://127.0.0.1:9380"-HOST = "127.0.0.1"-PORT = "9382"-HOST_API_KEY = ""-MODE = ""-TRANSPORT_SSE_ENABLED = True-TRANSPORT_STREAMABLE_HTTP_ENABLED = True-JSON_RESPONSE = True---class RAGFlowConnector:- _MAX_DATASET_CACHE = 32- # Independent from _MAX_DATASET_CACHE: the document cache holds per-dataset- # document lists (far heavier payloads), so its bound is tuned separately.- _MAX_DOCUMENT_CACHE = 32- _CACHE_TTL = 300- # Keep in sync with api.utils.pagination_utils.REST_API_MAX_PAGE_SIZE.- _REST_API_MAX_PAGE_SIZE = 100- # Fixed rerank candidate window sent with every retrieval request, so the- # ranking cannot shift between pages of one pagination sequence (the backend- # reranks this many candidates before slicing the requested page). Requests- # whose page * page_size exceeds it are rejected up front. Keep in sync with- # mcpRerankCandidatesCount in internal/handler/mcp_server.go.- _RERANK_CANDIDATES_COUNT = 512-- _dataset_metadata_cache: OrderedDict[str, tuple[dict, float | int]] = OrderedDict() # "dataset_id" -> (metadata, expiry_ts)- _document_metadata_cache: OrderedDict[str, tuple[list[tuple[str, dict]], float | int]] = OrderedDict() # "dataset_id" -> ([(document_id, doc_metadata)], expiry_ts)-- def __init__(self, base_url: str, version="v1"):- self.base_url = base_url- self.version = version- self.api_url = f"{self.base_url}/api/{self.version}"- self._async_client = None-- async def _get_client(self):- if self._async_client is None:- self._async_client = httpx.AsyncClient(timeout=httpx.Timeout(60.0))- return self._async_client-- async def close(self):- if self._async_client is not None:- await self._async_client.aclose()- self._async_client = None-- async def _post(self, path, json=None, stream=False, files=None, api_key: str = ""):- if not api_key:- return None- client = await self._get_client()- res = await client.post(url=self.api_url + path, json=json, headers={"Authorization": f"Bearer {api_key}"})- return res-- async def _get(self, path, params=None, api_key: str = ""):- if not api_key:- return None- client = await self._get_client()- res = await client.get(url=self.api_url + path, params=params, headers={"Authorization": f"Bearer {api_key}"})- return res-- def _is_cache_valid(self, ts):- return time.time() < ts-- def _get_expiry_timestamp(self):- offset = random.randint(-30, 30)- return time.time() + self._CACHE_TTL + offset-- def _get_cached_dataset_metadata(self, dataset_id):- entry = self._dataset_metadata_cache.get(dataset_id)- if entry:- data, ts = entry- if self._is_cache_valid(ts):- self._dataset_metadata_cache.move_to_end(dataset_id)- return data- return None-- def _set_cached_dataset_metadata(self, dataset_id, metadata):- self._dataset_metadata_cache[dataset_id] = (metadata, self._get_expiry_timestamp())- self._dataset_metadata_cache.move_to_end(dataset_id)- if len(self._dataset_metadata_cache) > self._MAX_DATASET_CACHE:- self._dataset_metadata_cache.popitem(last=False)-- def _get_cached_document_metadata_by_dataset(self, dataset_id):- entry = self._document_metadata_cache.get(dataset_id)- if entry:- data_list, ts = entry- if self._is_cache_valid(ts):- self._document_metadata_cache.move_to_end(dataset_id)- return {doc_id: doc_meta for doc_id, doc_meta in data_list}- del self._document_metadata_cache[dataset_id]- return None-- def _set_cached_document_metadata_by_dataset(self, dataset_id, doc_id_meta_list):- self._document_metadata_cache[dataset_id] = (doc_id_meta_list, self._get_expiry_timestamp())- self._document_metadata_cache.move_to_end(dataset_id)- if len(self._document_metadata_cache) > self._MAX_DOCUMENT_CACHE:- self._document_metadata_cache.popitem(last=False)-- async def _fetch_datasets_page(- self,- *,- api_key: str,- page: int,- page_size: int,- orderby: str = "create_time",- desc: bool = True,- id: str | None = None,- name: str | None = None,- ):- """Fetch one structured page of accessible datasets from the backend API."""- params = {"page": page, "page_size": page_size, "orderby": orderby, "desc": desc}- if id:- params["id"] = id- if name:- params["name"] = name-- res = await self._get("/datasets", params, api_key=api_key)- if not res or res.status_code != 200:- error_message = None- if res is not None:- try:- error_message = res.json().get("message")- except Exception:- error_message = None- raise Exception([types.TextContent(type="text", text=error_message or "Cannot process this operation.")])-- res_json = res.json()- if res_json.get("code") != 0:- raise Exception([types.TextContent(type="text", text=res_json.get("message", "Cannot process this operation."))])-- return res_json-- async def list_chats(self, *, api_key: str, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True):- """Return accessible chat assistants as newline-delimited JSON for MCP tool descriptions."""- logging.info("Listing chat assistants via MCP (page=%s, page_size=%s)", page, page_size)- params = {"page": page, "page_size": page_size, "orderby": orderby, "desc": json.dumps(desc)}- res = await self._get("/chats", params, api_key=api_key)- if not res or res.status_code != 200:- error_message = None- if res is not None:- try:- error_message = res.json().get("message")- logging.warning("list_chats request failed: status=%s message=%s", res.status_code, error_message)- except Exception:- error_message = None- logging.warning("list_chats request failed: status=%s (parse error)", res.status_code)- raise Exception([types.TextContent(type="text", text=error_message or "Cannot list chats.")])- res_json = res.json()- if res_json.get("code") != 0:- logging.warning("list_chats API error: code=%s message=%s", res_json.get("code"), res_json.get("message"))- raise Exception([types.TextContent(type="text", text=res_json.get("message", "Cannot list chats."))])- _chats_data = res_json.get("data", [])- if isinstance(_chats_data, dict):- _chats_data = _chats_data.get("chats", [])- chat_count = len(_chats_data)- logging.info("list_chats returned %d chat(s)", chat_count)- result_list = []- for data in _chats_data:- d = {"id": data.get("id"), "name": data.get("name"), "description": data.get("description", "")}- result_list.append(json.dumps(d, ensure_ascii=False))- return "\n".join(result_list)-- async def _fetch_all_datasets(- self,- *,- api_key: str,- orderby: str = "create_time",- desc: bool = True,- id: str | None = None,- name: str | None = None,- ):- """Fetch all accessible datasets without exceeding the REST API page-size limit."""- datasets = []- page = 1-- while True:- logging.debug("fetching all /datasets page=%s page_size=%s", page, self._REST_API_MAX_PAGE_SIZE)- res_json = await self._fetch_datasets_page(- api_key=api_key,- page=page,- page_size=self._REST_API_MAX_PAGE_SIZE,- orderby=orderby,- desc=desc,- id=id,- name=name,- )- page_datasets = res_json.get("data", [])- logging.debug("received %s datasets from page=%s", len(page_datasets), page)- if not page_datasets:- break-- datasets.extend(page_datasets)- # The REST API reports the dataset total under "total_datasets"- # (see api/utils/api_utils.py get_result).- total = res_json.get("total_datasets", res_json.get("total"))- if total is not None and len(datasets) >= total:- break-- page += 1-- return datasets-- async def list_datasets(self, *, api_key: str, page: int = 1, page_size: int = -1, orderby: str = "create_time", desc: bool = True, id: str | None = None, name: str | None = None):- """Return accessible datasets as newline-delimited JSON for MCP tool descriptions."""- if page_size == -1:- datasets = await self._fetch_all_datasets(api_key=api_key, orderby=orderby, desc=desc, id=id, name=name)- else:- page_size = min(page_size, self._REST_API_MAX_PAGE_SIZE)- res_json = await self._fetch_datasets_page(api_key=api_key, page=page, page_size=page_size, orderby=orderby, desc=desc, id=id, name=name)- datasets = res_json["data"]-- result_list = []- for data in datasets:- d = {"id": data["id"], "name": data.get("name"), "description": data.get("description")}- result_list.append(json.dumps(d, ensure_ascii=False))- return "\n".join(result_list)-- async def resolve_dataset_ids(self, *, api_key: str):- """Resolve all accessible dataset IDs for MCP retrieval fallback."""- logging.info("Resolving accessible dataset IDs for MCP retrieval")- try:- datasets = await self._fetch_all_datasets(api_key=api_key)- except Exception as exc:- logging.warning("resolve_dataset_ids failed to fetch /datasets error=%s", exc)- raise-- dataset_ids = [data["id"] for data in datasets if data.get("id")]- resolved = list(dict.fromkeys(dataset_ids))- logging.info("resolve_dataset_ids resolved %s accessible dataset IDs", len(resolved))- return resolved-- async def retrieval(- self,- *,- api_key: str,- dataset_ids,- document_ids=None,- question="",- page=1,- page_size=30,- similarity_threshold=0.2,- vector_similarity_weight=0.3,- top_k=1024,- rerank_id: str | None = None,- keyword: bool = False,- force_refresh: bool = False,- ):- if document_ids is None:- document_ids = []-- if not dataset_ids:- logging.info("MCP retrieval omitted dataset_ids; resolving accessible datasets")- dataset_ids = await self.resolve_dataset_ids(api_key=api_key)- if not dataset_ids:- logging.info("MCP retrieval found no accessible datasets for current user")- raise Exception([types.TextContent(type="text", text="No accessible datasets found.")])-- if page * page_size > self._RERANK_CANDIDATES_COUNT:- # Fail here rather than letting the backend reject the request: a- # window past the fixed candidate pool cannot be served with a- # stable ranking anyway.- raise Exception(- [- types.TextContent(- type="text",- text=(f"page * page_size ({page * page_size}) exceeds the fixed rerank candidate window ({self._RERANK_CANDIDATES_COUNT}); narrow page or page_size."),- )- ]- )-- data_json = {- "page": page,- "page_size": page_size,- "similarity_threshold": similarity_threshold,- "vector_similarity_weight": vector_similarity_weight,- "top_k": top_k,- "rerank_id": rerank_id,- # A fixed window (not page * page_size) keeps the rerank pool — and- # therefore the ranking — identical on every page of a pagination- # sequence, so pages cannot drift, duplicate, or skip results.- "rerank_candidates_count": self._RERANK_CANDIDATES_COUNT,- "keyword": keyword,- "question": question,- "dataset_ids": dataset_ids,- "document_ids": document_ids,- }- # Send a POST request to the backend service (using requests library as an example, actual implementation may vary)- res = await self._post("/retrieval", json=data_json, api_key=api_key)- if not res or res.status_code != 200:- raise Exception([types.TextContent(type="text", text="Cannot process this operation.")])-- res = res.json()- if res.get("code") == 0:- data = res["data"]- chunks = []-- # Cache document metadata and dataset information- document_cache, dataset_cache = await self._get_document_metadata_cache(dataset_ids, api_key=api_key, force_refresh=force_refresh)-- # Process chunks with enhanced field mapping including per-chunk metadata- for chunk_data in data.get("chunks", []):- enhanced_chunk = self._map_chunk_fields(chunk_data, dataset_cache, document_cache)- chunks.append(enhanced_chunk)-- # Build structured response (no longer need response-level document_metadata)- response = {- "chunks": chunks,- "pagination": {- "page": data.get("page", page),- "page_size": data.get("page_size", page_size),- "total_chunks": data.get("total", len(chunks)),- "total_pages": (data.get("total", len(chunks)) + page_size - 1) // page_size,- },- "query_info": {- "question": question,- "similarity_threshold": similarity_threshold,- "vector_weight": vector_similarity_weight,- "keyword_search": keyword,- "dataset_count": len(dataset_ids),- },- }-- return [types.TextContent(type="text", text=json.dumps(response, ensure_ascii=False))]-- raise Exception([types.TextContent(type="text", text=res.get("message"))])-- async def _get_document_metadata_cache(self, dataset_ids, *, api_key: str, force_refresh=False):- """Cache document metadata for all documents in the specified datasets"""- document_cache = {}- dataset_cache = {}-- try:- for dataset_id in dataset_ids:- dataset_meta = None if force_refresh else self._get_cached_dataset_metadata(dataset_id)- if not dataset_meta:- # First get dataset info for name- dataset_res = await self._get("/datasets", {"id": dataset_id, "page_size": 1}, api_key=api_key)- if dataset_res and dataset_res.status_code == 200:- dataset_data = dataset_res.json()- if dataset_data.get("code") == 0 and dataset_data.get("data"):- dataset_info = dataset_data["data"][0]- dataset_meta = {"name": dataset_info.get("name", "Unknown"), "description": dataset_info.get("description", "")}- self._set_cached_dataset_metadata(dataset_id, dataset_meta)- if dataset_meta:- dataset_cache[dataset_id] = dataset_meta-- docs = None if force_refresh else self._get_cached_document_metadata_by_dataset(dataset_id)- if docs is None:- page = 1- page_size = 30- doc_id_meta_list = []- docs = {}- pagination_succeeded = True- while True:- docs_res = await self._get(f"/datasets/{dataset_id}/documents?page={page}&page_size={page_size}", api_key=api_key)- if not docs_res or docs_res.status_code != 200:- pagination_succeeded = False- break- docs_data = docs_res.json()- if docs_data.get("code") != 0:- pagination_succeeded = False- break- page_docs = docs_data.get("data", {}).get("docs") or []- for doc in page_docs:- doc_id = doc.get("id")- if not doc_id:- continue- doc_meta = {- "document_id": doc_id,- "name": doc.get("name", ""),- "location": doc.get("location", ""),- "type": doc.get("type", ""),- "size": doc.get("size"),- "chunk_count": doc.get("chunk_count"),- "create_date": doc.get("create_date", ""),- "update_date": doc.get("update_date", ""),- "token_count": doc.get("token_count"),- "thumbnail": doc.get("thumbnail", ""),- "dataset_id": doc.get("dataset_id", dataset_id),- "meta_fields": doc.get("meta_fields", {}),- }- doc_id_meta_list.append((doc_id, doc_meta))- docs[doc_id] = doc_meta-- # A page smaller than page_size (including an empty one) is the- # last page. This terminates empty/exhausted result sets, which- # previously looped forever re-requesting the same page (#16248),- # and replaces the old `total - page * page_size` check that- # stopped one page early and silently dropped documents.- if len(page_docs) < page_size:- break- page += 1- if pagination_succeeded:- self._set_cached_document_metadata_by_dataset(dataset_id, doc_id_meta_list)- else:- docs = {}- if docs:- document_cache.update(docs)-- except Exception as e:- # Gracefully handle metadata cache failures- logging.error(f"Problem building the document metadata cache: {str(e)}")- pass-- return document_cache, dataset_cache-- def _map_chunk_fields(self, chunk_data, dataset_cache, document_cache):- """Preserve all original API fields and add per-chunk document metadata"""- # Start with ALL raw data from API (preserve everything like original version)- mapped = dict(chunk_data)-- # Add dataset name enhancement- dataset_id = chunk_data.get("dataset_id") or chunk_data.get("kb_id")- if dataset_id and dataset_id in dataset_cache:- mapped["dataset_name"] = dataset_cache[dataset_id]["name"]- else:- mapped["dataset_name"] = "Unknown"-- # Add document name convenience field- mapped["document_name"] = chunk_data.get("document_keyword", "")-- # Add per-chunk document metadata- document_id = chunk_data.get("document_id")- if document_id and document_id in document_cache:- mapped["document_metadata"] = document_cache[document_id]-- return mapped---class RAGFlowCtx:- def __init__(self, connector: RAGFlowConnector):- self.conn = connector---@asynccontextmanager-async def sse_lifespan(server: Server) -> AsyncIterator[dict]:- ctx = RAGFlowCtx(RAGFlowConnector(base_url=BASE_URL))-- logging.info("Legacy SSE application started with StreamableHTTP session manager!")- try:- yield {"ragflow_ctx": ctx}- finally:- await ctx.conn.close()- logging.info("Legacy SSE application shutting down...")---app = Server("ragflow-mcp-server", lifespan=sse_lifespan)-AUTH_TOKEN_STATE_KEY = "ragflow_auth_token"---def _to_text(value: Any) -> str:- if isinstance(value, bytes):- return value.decode(errors="ignore")- return str(value)---def _extract_token_from_headers(headers: Any) -> str | None:- if not headers or not hasattr(headers, "get"):- return None-- auth_keys = ("authorization", "Authorization", b"authorization", b"Authorization")- for key in auth_keys:- auth = headers.get(key)- if not auth:- continue- auth_text = _to_text(auth).strip()- if auth_text.lower().startswith("bearer "):- token = auth_text[7:].strip()- if token:- return token-- api_key_keys = ("api_key", "x-api-key", "Api-Key", "X-API-Key", b"api_key", b"x-api-key", b"Api-Key", b"X-API-Key")- for key in api_key_keys:- token = headers.get(key)- if token:- token_text = _to_text(token).strip()- if token_text:- return token_text-- return None---def _extract_token_from_request(request: Any) -> str | None:- if request is None:- return None-- state = getattr(request, "state", None)- if state is not None:- token = getattr(state, AUTH_TOKEN_STATE_KEY, None)- if token:- return token-- token = _extract_token_from_headers(getattr(request, "headers", None))- if token and state is not None:- setattr(state, AUTH_TOKEN_STATE_KEY, token)-- return token---def with_api_key(required: bool = True):- def decorator(func):- @wraps(func)- async def wrapper(*args, **kwargs):- ctx = app.request_context- ragflow_ctx = ctx.lifespan_context.get("ragflow_ctx")- if not ragflow_ctx:- raise ValueError("Get RAGFlow Context failed")-- connector = ragflow_ctx.conn- api_key = HOST_API_KEY-- if MODE == LaunchMode.HOST:- api_key = _extract_token_from_request(getattr(ctx, "request", None)) or ""- if required and not api_key:- raise ValueError("RAGFlow API key or Bearer token is required.")-- return await func(*args, connector=connector, api_key=api_key, **kwargs)-- return wrapper-- return decorator--[email protected]_tools()-@with_api_key(required=True)-async def list_tools(*, connector: RAGFlowConnector, api_key: str) -> list[types.Tool]:- dataset_description = await connector.list_datasets(api_key=api_key)- chat_description = await connector.list_chats(api_key=api_key)-- return [- types.Tool(- name="ragflow_retrieval",- description="Retrieve relevant chunks from the RAGFlow retrieve interface based on the question. You can optionally specify dataset_ids to search only specific datasets, or omit dataset_ids entirely to search across ALL available datasets. You can also optionally specify document_ids to search within specific documents. When dataset_ids is not provided or is empty, the system will automatically search across all available datasets. Below is the list of all available datasets, including their descriptions and IDs:"- + dataset_description,- inputSchema={- "type": "object",- "properties": {- "dataset_ids": {"type": "array", "items": {"type": "string"}, "description": "Optional array of dataset IDs to search. If not provided or empty, all datasets will be searched."},- "document_ids": {"type": "array", "items": {"type": "string"}, "description": "Optional array of document IDs to search within."},- "question": {"type": "string", "description": "The question or query to search for."},- "page": {- "type": "integer",- "description": "Page number for pagination",- "default": 1,- "minimum": 1,- },- "page_size": {- "type": "integer",- "description": "Number of results to return per page (default: 10, max recommended: 50 to avoid token limits)",- "default": 10,- "minimum": 1,- "maximum": 100,- },- "similarity_threshold": {- "type": "number",- "description": "Minimum similarity threshold for results",- "default": 0.2,- "minimum": 0.0,- "maximum": 1.0,- },- "vector_similarity_weight": {- "type": "number",- "description": "Weight for vector similarity vs term similarity",- "default": 0.3,- "minimum": 0.0,- "maximum": 1.0,- },- "keyword": {- "type": "boolean",- "description": "Enable keyword-based search",- "default": False,- },- "top_k": {- "type": "integer",- "description": "Maximum results to consider before ranking",- "default": 1024,- "minimum": 1,- "maximum": 1024,- },- "rerank_id": {- "type": "string",- "description": "Optional reranking model identifier",- },- "force_refresh": {- "type": "boolean",- "description": "Set to true only if fresh dataset and document metadata is explicitly required. Otherwise, cached metadata is used (default: false).",- "default": False,- },- },- "required": ["question"],- },- ),- types.Tool(- name="ragflow_list_datasets",- description="List all accessible datasets (knowledge bases) in RAGFlow. Returns dataset IDs, names, and descriptions. Use this tool to discover which datasets are available before performing retrieval."- + dataset_description,- inputSchema={- "type": "object",- "properties": {- "page": {- "type": "integer",- "description": "Page number",- "default": 1,- "minimum": 1,- },- "page_size": {- "type": "integer",- "description": "Results per page",- "default": 100,- "minimum": 1,- "maximum": 1000,- },- },- },- ),- types.Tool(- name="ragflow_list_chats",- description="List all accessible chat assistants in RAGFlow. Returns chat assistant IDs, names, and descriptions. Use this tool to discover available chat assistants that can be used for conversations."- + chat_description,- inputSchema={- "type": "object",- "properties": {- "page": {- "type": "integer",- "description": "Page number",- "default": 1,- "minimum": 1,- },- "page_size": {- "type": "integer",- "description": "Results per page",- "default": 30,- "minimum": 1,- "maximum": 100,- },- },- },- ),- ]--[email protected]_tool()-@with_api_key(required=True)-async def call_tool(- name: str,- arguments: dict,- *,- connector: RAGFlowConnector,- api_key: str,-) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:- if name == "ragflow_retrieval":- document_ids = arguments.get("document_ids", [])- dataset_ids = arguments.get("dataset_ids", [])- question = arguments.get("question", "")- page = arguments.get("page", 1)- page_size = arguments.get("page_size", 10)- similarity_threshold = arguments.get("similarity_threshold", 0.2)- vector_similarity_weight = arguments.get("vector_similarity_weight", 0.3)- keyword = arguments.get("keyword", False)- top_k = arguments.get("top_k", 1024)- rerank_id = arguments.get("rerank_id")- force_refresh = arguments.get("force_refresh", False)-- return await connector.retrieval(- api_key=api_key,- dataset_ids=dataset_ids,- document_ids=document_ids,- question=question,- page=page,- page_size=page_size,- similarity_threshold=similarity_threshold,- vector_similarity_weight=vector_similarity_weight,- keyword=keyword,- top_k=top_k,- rerank_id=rerank_id,- force_refresh=force_refresh,- )-- if name == "ragflow_list_datasets":- page = arguments.get("page", 1)- page_size = arguments.get("page_size", 100)- result = await connector.list_datasets(api_key=api_key, page=page, page_size=page_size)- return [types.TextContent(type="text", text=result)]-- if name == "ragflow_list_chats":- page = arguments.get("page", 1)- page_size = arguments.get("page_size", 30)- result = await connector.list_chats(api_key=api_key, page=page, page_size=page_size)- return [types.TextContent(type="text", text=result)]-- raise ValueError(f"Tool not found: {name}")---def create_starlette_app():- routes = []- middleware = None- if MODE == LaunchMode.HOST:- from starlette.types import ASGIApp, Receive, Scope, Send-- class AuthMiddleware:- def __init__(self, app: ASGIApp):- self.app = app-- async def __call__(self, scope: Scope, receive: Receive, send: Send):- if scope["type"] != "http":- await self.app(scope, receive, send)- return-- path = scope["path"]- if path.startswith("/messages/") or path.startswith("/sse") or path.startswith("/mcp"):- headers = dict(scope["headers"])- token = _extract_token_from_headers(headers)-- if not token:- response = JSONResponse({"error": "Missing or invalid authorization header"}, status_code=401)- await response(scope, receive, send)- return- scope.setdefault("state", {})[AUTH_TOKEN_STATE_KEY] = token-- await self.app(scope, receive, send)-- middleware = [Middleware(AuthMiddleware)]-- # Add SSE routes if enabled- if TRANSPORT_SSE_ENABLED:- from mcp.server.sse import SseServerTransport-- sse = SseServerTransport("/messages/")-- async def handle_sse(request):- async with sse.connect_sse(request.scope, request.receive, request._send) as streams:- await app.run(streams[0], streams[1], app.create_initialization_options(experimental_capabilities={"headers": dict(request.headers)}))- return Response()-- routes.extend(- [- Route("/sse", endpoint=handle_sse, methods=["GET"]),- Mount("/messages/", app=sse.handle_post_message),- ]- )-- # Add streamable HTTP route if enabled- streamablehttp_lifespan = None- if TRANSPORT_STREAMABLE_HTTP_ENABLED:- from mcp.server.streamable_http_manager import StreamableHTTPSessionManager- from starlette.types import Receive, Scope, Send-- session_manager = StreamableHTTPSessionManager(- app=app,- event_store=None,- json_response=JSON_RESPONSE,- stateless=True,- )-- class StreamableHTTPEntry:- async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:- await session_manager.handle_request(scope, receive, send)-- streamable_http_entry = StreamableHTTPEntry()-- @asynccontextmanager- async def streamablehttp_lifespan(app: Starlette) -> AsyncIterator[None]:- async with session_manager.run():- logging.info("StreamableHTTP application started with StreamableHTTP session manager!")- try:- yield- finally:- logging.info("StreamableHTTP application shutting down...")-- routes.extend(- [- Route("/mcp", endpoint=streamable_http_entry, methods=["GET", "POST", "DELETE"]),- Mount("/mcp", app=streamable_http_entry),- ]- )-- return Starlette(- debug=False,- routes=routes,- middleware=middleware,- lifespan=streamablehttp_lifespan,- )--[email protected]("--base-url", type=str, default="http://127.0.0.1:9380", help="API base URL for RAGFlow backend")[email protected]("--host", type=str, default="127.0.0.1", help="Host to bind the RAGFlow MCP server")[email protected]("--port", type=int, default=9382, help="Port to bind the RAGFlow MCP server")- "--mode",- type=click.Choice(["self-host", "host"]),- default="self-host",- help=("Launch mode:\n self-host: run MCP for a single tenant (requires --api-key)\n host: multi-tenant mode, users must provide Authorization headers"),-)[email protected]("--api-key", type=str, default="", help="API key to use when in self-host mode")- "--transport-sse-enabled/--no-transport-sse-enabled",- default=True,- help="Enable or disable legacy SSE transport mode (default: enabled)",-)- "--transport-streamable-http-enabled/--no-transport-streamable-http-enabled",- default=True,- help="Enable or disable streamable-http transport mode (default: enabled)",-)- "--json-response/--no-json-response",- default=True,- help="Enable or disable JSON response mode for streamable-http (default: enabled)",-)-def main(base_url, host, port, mode, api_key, transport_sse_enabled, transport_streamable_http_enabled, json_response):- import os-- import uvicorn- from dotenv import load_dotenv-- load_dotenv()-- def parse_bool_flag(key: str, default: bool) -> bool:- val = os.environ.get(key, str(default))- return str(val).strip().lower() in ("1", "true", "yes", "on")-- global BASE_URL, HOST, PORT, MODE, HOST_API_KEY, TRANSPORT_SSE_ENABLED, TRANSPORT_STREAMABLE_HTTP_ENABLED, JSON_RESPONSE- BASE_URL = os.environ.get("RAGFLOW_MCP_BASE_URL", base_url)- HOST = os.environ.get("RAGFLOW_MCP_HOST", host)- PORT = os.environ.get("RAGFLOW_MCP_PORT", str(port))- MODE = os.environ.get("RAGFLOW_MCP_LAUNCH_MODE", mode)- HOST_API_KEY = os.environ.get("RAGFLOW_MCP_HOST_API_KEY", api_key)- TRANSPORT_SSE_ENABLED = parse_bool_flag("RAGFLOW_MCP_TRANSPORT_SSE_ENABLED", transport_sse_enabled)- TRANSPORT_STREAMABLE_HTTP_ENABLED = parse_bool_flag("RAGFLOW_MCP_TRANSPORT_STREAMABLE_ENABLED", transport_streamable_http_enabled)- JSON_RESPONSE = parse_bool_flag("RAGFLOW_MCP_JSON_RESPONSE", json_response)-- if MODE == LaunchMode.SELF_HOST and not HOST_API_KEY:- raise click.UsageError("--api-key is required when --mode is 'self-host'")-- if not TRANSPORT_STREAMABLE_HTTP_ENABLED and JSON_RESPONSE:- JSON_RESPONSE = False-- print(- r"""-__ __ ____ ____ ____ _____ ______ _______ ____-| \/ |/ ___| _ \ / ___|| ____| _ \ \ / / ____| _ \-| |\/| | | | |_) | \___ \| _| | |_) \ \ / /| _| | |_) |-| | | | |___| __/ ___) | |___| _ < \ V / | |___| _ <-|_| |_|\____|_| |____/|_____|_| \_\ \_/ |_____|_| \_\- """,- flush=True,- )- print(f"MCP launch mode: {MODE}", flush=True)- print(f"MCP host: {HOST}", flush=True)- print(f"MCP port: {PORT}", flush=True)- print(f"MCP base_url: {BASE_URL}", flush=True)-- if not any([TRANSPORT_SSE_ENABLED, TRANSPORT_STREAMABLE_HTTP_ENABLED]):- print("At least one transport should be enabled, enable streamable-http automatically", flush=True)- TRANSPORT_STREAMABLE_HTTP_ENABLED = True-- if TRANSPORT_SSE_ENABLED:- print("SSE transport enabled: yes", flush=True)- print("SSE endpoint available at /sse", flush=True)- else:- print("SSE transport enabled: no", flush=True)-- if TRANSPORT_STREAMABLE_HTTP_ENABLED:- print("Streamable HTTP transport enabled: yes", flush=True)- print("Streamable HTTP endpoint available at /mcp", flush=True)- if JSON_RESPONSE:- print("Streamable HTTP mode: JSON response enabled", flush=True)- else:- print("Streamable HTTP mode: SSE over HTTP enabled", flush=True)- else:- print("Streamable HTTP transport enabled: no", flush=True)- if JSON_RESPONSE:- print("Warning: --json-response ignored because streamable transport is disabled.", flush=True)-- uvicorn.run(- create_starlette_app(),- host=HOST,- port=int(PORT),- )---if __name__ == "__main__":- """- Launch examples:-- 1. Self-host mode with both SSE and Streamable HTTP (in JSON response mode) enabled (default):- uv run mcp/server/server.py --host=127.0.0.1 --port=9382 \- --base-url=http://127.0.0.1:9380 \- --mode=self-host --api-key=ragflow-xxxxx-- 2. Host mode (multi-tenant, clients must provide Authorization headers):- uv run mcp/server/server.py --host=127.0.0.1 --port=9382 \- --base-url=http://127.0.0.1:9380 \- --mode=host-- 3. Disable legacy SSE (only streamable HTTP will be active):- uv run mcp/server/server.py --no-transport-sse-enabled \- --mode=self-host --api-key=ragflow-xxxxx-- 4. Disable streamable HTTP (only legacy SSE will be active):- uv run mcp/server/server.py --no-transport-streamable-http-enabled \- --mode=self-host --api-key=ragflow-xxxxx-- 5. Use streamable HTTP with SSE-style events (disable JSON response):- uv run mcp/server/server.py --transport-streamable-http-enabled --no-json-response \- --mode=self-host --api-key=ragflow-xxxxx-- 6. Disable both transports (for testing):- uv run mcp/server/server.py --no-transport-sse-enabled --no-transport-streamable-http-enabled \- --mode=self-host --api-key=ragflow-xxxxx- """- main()