diff --git a/.env.lite.example b/.env.lite.example new file mode 100644 index 00000000..119a3d67 --- /dev/null +++ b/.env.lite.example @@ -0,0 +1,54 @@ +# WeKnora Lite 配置模板 +# 复制此文件为 .env.lite 并按需修改 +# cp .env.lite.example .env.lite + +GIN_MODE=debug + +# 日志级别,可选值:debug, info, warn, error, fatal +LOG_LEVEL=debug + +# 可选:日志文件绝对路径;未配置时,macOS .app 默认写入 +# ~/Library/Logs/WeKnora Lite/WeKnora Lite.log +# LOG_PATH=/absolute/path/to/weknora-lite.log + +# === 数据库 === +# macOS .app 中相对路径会自动重定向到 +# ~/Library/Application Support/WeKnora Lite/data/ +DB_DRIVER=sqlite +DB_PATH=./data/weknora.db + +# === 检索引擎(FTS5 + sqlite-vec)=== +RETRIEVE_DRIVER=sqlite + +# === 文件存储 === +STORAGE_TYPE=local +LOCAL_STORAGE_BASE_DIR=./data/files + +# === 流管理(内存,无 Redis)=== +STREAM_MANAGER_TYPE=memory + +# === LLM 服务 === +# Ollama 本地服务(默认地址,按需修改) +OLLAMA_BASE_URL=http://127.0.0.1:11434 +# 如使用其他 OpenAI 兼容服务,取消注释: +# OPENAI_API_KEY=sk-xxx +# OPENAI_BASE_URL=https://api.openai.com/v1 + +# === 安全配置(生产环境请务必修改!)=== +TENANT_AES_KEY=CHANGE-ME-32-char-secret-key!!!! +JWT_SECRET=CHANGE-ME-jwt-secret + +# === 功能开关 === +NEO4J_ENABLE=false +WEKNORA_SANDBOX_MODE=disabled +ENABLE_GRAPH_RAG=false +DISABLE_REGISTRATION=false + +# === 性能 === +CONCURRENCY_POOL_SIZE=3 + +# Docreader 地址 +DOCREADER_ADDR=127.0.0.1:50051 + +# Docreader 传输方式 +DOCREADER_TRANSPORT=grpc \ No newline at end of file diff --git a/.github/workflows/release-lite.yml b/.github/workflows/release-lite.yml new file mode 100644 index 00000000..3b384ade --- /dev/null +++ b/.github/workflows/release-lite.yml @@ -0,0 +1,451 @@ +name: Release Lite Binaries + +on: + push: + tags: + - "v*" + workflow_dispatch: + inputs: + tag: + description: "Release tag (e.g. v0.2.0)" + required: true + +concurrency: + group: release-lite-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: write + +env: + GO_VERSION: "1.24" + NODE_VERSION: "22" + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true" + +jobs: + # ── 1. Build frontend once, share as artifact (for Web CLI) ── + build-frontend: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: npm + cache-dependency-path: frontend/package-lock.json + + - name: Build frontend + working-directory: frontend + run: | + npm ci + npm run build + + - uses: actions/upload-artifact@v4 + with: + name: frontend-dist + path: frontend/dist/ + retention-days: 1 + + # ── 2a. Build Web CLI binary per platform (native CGO) ── + build-binary: + needs: build-frontend + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + goos: linux + goarch: amd64 + - os: ubuntu-24.04-arm + goos: linux + goarch: arm64 + - os: macos-14 + goos: darwin + goarch: amd64 + - os: macos-14 + goos: darwin + goarch: arm64 + runs-on: ${{ matrix.os }} + env: + CGO_ENABLED: 1 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - uses: actions/download-artifact@v4 + with: + name: frontend-dist + path: web/ + + - name: Resolve version + id: ver + run: | + if [ -n "${{ inputs.tag }}" ]; then + VERSION="${{ inputs.tag }}" + else + VERSION="${GITHUB_REF#refs/tags/}" + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "Building ${VERSION} for ${{ matrix.goos }}/${{ matrix.goarch }}" + + - name: Build + run: | + export EDITION=lite + eval "$(./scripts/get_version.sh env)" + LDFLAGS="-w -s $(./scripts/get_version.sh ldflags)" + export CGO_CFLAGS="-Wno-deprecated-declarations" + if [ "${{ matrix.goos }}" = "darwin" ]; then + export CGO_LDFLAGS="-Wl,-no_warn_duplicate_libraries" + fi + go build -tags "sqlite_fts5" -ldflags="${LDFLAGS}" \ + -o WeKnora-lite ./cmd/server + + - name: Package tarball + run: | + ARCHIVE="WeKnora-lite_${{ steps.ver.outputs.version }}_${{ matrix.goos }}_${{ matrix.goarch }}" + mkdir -p "${ARCHIVE}/web" + cp WeKnora-lite "${ARCHIVE}/" + cp -r web/* "${ARCHIVE}/web/" + cp .env.lite.example "${ARCHIVE}/" + cp docs/LITE.md "${ARCHIVE}/README.md" + if [ -d config ]; then + cp -r config "${ARCHIVE}/config" + fi + if [ -d migrations/sqlite ]; then + mkdir -p "${ARCHIVE}/migrations/sqlite" + cp -r migrations/sqlite/* "${ARCHIVE}/migrations/sqlite/" + fi + if [ -f deploy/weknora-lite.service ]; then + cp deploy/weknora-lite.service "${ARCHIVE}/" + fi + tar czf "${ARCHIVE}.tar.gz" "${ARCHIVE}" + shasum -a 256 "${ARCHIVE}.tar.gz" > "${ARCHIVE}.tar.gz.sha256" + + - uses: actions/upload-artifact@v4 + with: + name: release-${{ matrix.goos }}-${{ matrix.goarch }} + path: | + WeKnora-lite_*.tar.gz + WeKnora-lite_*.tar.gz.sha256 + retention-days: 3 + + # ── 2b. Build Desktop APP per platform (Wails) ── + build-desktop-app: + strategy: + fail-fast: false + matrix: + include: + - os: macos-14 + platform: darwin/universal + arch_label: macOS_universal + artifact_name: desktop-macos-universal + - os: macos-14 + platform: darwin/amd64 + arch_label: macOS_amd64 + artifact_name: desktop-macos-amd64 + - os: macos-14 + platform: darwin/arm64 + arch_label: macOS_arm64 + artifact_name: desktop-macos-arm64 + - os: ubuntu-latest + platform: linux/amd64 + arch_label: linux_amd64 + artifact_name: desktop-linux-amd64 + - os: windows-latest + platform: windows/amd64 + arch_label: windows_amd64 + artifact_name: desktop-windows-amd64 + runs-on: ${{ matrix.os }} + env: + CGO_ENABLED: 1 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: npm + cache-dependency-path: frontend/package-lock.json + + - name: Install Wails CLI + run: go install github.com/wailsapp/wails/v2/cmd/wails@latest + + - name: Install Linux dependencies + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev + + - name: Install NSIS (Windows) + if: runner.os == 'Windows' + run: choco install nsis -y + + - name: Resolve version + id: ver + shell: bash + run: | + if [ -n "${{ inputs.tag }}" ]; then + VERSION="${{ inputs.tag }}" + else + VERSION="${GITHUB_REF#refs/tags/}" + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + + - name: Pre-compile Go packages (Windows) + if: runner.os == 'Windows' + shell: bash + run: | + export GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn + export CGO_CFLAGS="-Wno-deprecated-declarations" + cd cmd/desktop + go build -p 1 -buildvcs=false -tags "desktop,sqlite_fts5,production" -o _prebuild_tmp.exe . || true + rm -f _prebuild_tmp.exe + + - name: Build desktop app + shell: bash + run: | + export EDITION=lite + export GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn + eval "$(./scripts/get_version.sh env)" + LDFLAGS="$(./scripts/get_version.sh ldflags) -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'" + export CGO_CFLAGS="-Wno-deprecated-declarations" + + BUILD_TAGS="sqlite_fts5" + NSIS_FLAG="" + + if [ "${{ runner.os }}" = "Linux" ]; then + BUILD_TAGS="sqlite_fts5 webkit2_41" + fi + if [ "${{ runner.os }}" = "macOS" ]; then + export CGO_LDFLAGS="-Wl,-no_warn_duplicate_libraries" + fi + if [ "${{ runner.os }}" = "Windows" ]; then + NSIS_FLAG="-nsis" + fi + + cd cmd/desktop + wails build -clean \ + -tags "${BUILD_TAGS}" \ + -platform "${{ matrix.platform }}" \ + ${NSIS_FLAG} \ + -ldflags="${LDFLAGS}" \ + -o "WeKnora Lite" + + # ── macOS: bundle .app with resources, then create DMG ── + - name: Package macOS app + if: runner.os == 'macOS' + run: | + APP_BUNDLE="WeKnora Lite.app" + SRC="cmd/desktop/build/bin/${APP_BUNDLE}" + RESOURCES="${SRC}/Contents/Resources" + + mkdir -p "${RESOURCES}/config" + mkdir -p "${RESOURCES}/migrations/sqlite" + + if [ -f .env.lite.example ]; then + cp .env.lite.example "${RESOURCES}/.env" + fi + if [ -d migrations/sqlite ]; then + cp -r migrations/sqlite/* "${RESOURCES}/migrations/sqlite/" + fi + if [ -d config ]; then + cp -r config/* "${RESOURCES}/config/" + fi + + DMG_NAME="WeKnora-Lite-App_${{ steps.ver.outputs.version }}_${{ matrix.arch_label }}" + + DMG_SRC="dmg_staging" + mkdir -p "${DMG_SRC}" + cp -R "cmd/desktop/build/bin/${APP_BUNDLE}" "${DMG_SRC}/" + ln -s /Applications "${DMG_SRC}/Applications" + + hdiutil create -volname "WeKnora Lite" \ + -srcfolder "${DMG_SRC}" \ + -ov -format UDZO \ + "${DMG_NAME}.dmg" + + shasum -a 256 "${DMG_NAME}.dmg" > "${DMG_NAME}.dmg.sha256" + + # ── Linux: package tarball ── + - name: Package Linux app + if: runner.os == 'Linux' + run: | + ARCHIVE="WeKnora-Lite-App_${{ steps.ver.outputs.version }}_${{ matrix.arch_label }}" + mkdir -p "${ARCHIVE}" + cp "cmd/desktop/build/bin/WeKnora Lite" "${ARCHIVE}/" + if [ -f .env.lite.example ]; then + cp .env.lite.example "${ARCHIVE}/" + fi + cp docs/LITE.md "${ARCHIVE}/README.md" + if [ -d config ]; then + cp -r config "${ARCHIVE}/config" + fi + if [ -d migrations/sqlite ]; then + mkdir -p "${ARCHIVE}/migrations/sqlite" + cp -r migrations/sqlite/* "${ARCHIVE}/migrations/sqlite/" + fi + tar czf "${ARCHIVE}.tar.gz" "${ARCHIVE}" + shasum -a 256 "${ARCHIVE}.tar.gz" > "${ARCHIVE}.tar.gz.sha256" + + # ── Windows: package NSIS installer or plain exe ── + - name: Package Windows app + if: runner.os == 'Windows' + shell: bash + run: | + ARCHIVE="WeKnora-Lite-App_${{ steps.ver.outputs.version }}_${{ matrix.arch_label }}" + # Wails -nsis generates an installer named {app}-amd64-installer.exe + INSTALLER="cmd/desktop/build/bin/WeKnora Lite-amd64-installer.exe" + if [ -f "$INSTALLER" ]; then + cp "$INSTALLER" "${ARCHIVE}_setup.exe" + sha256sum "${ARCHIVE}_setup.exe" > "${ARCHIVE}_setup.exe.sha256" + else + cp "cmd/desktop/build/bin/WeKnora Lite.exe" "${ARCHIVE}.exe" + sha256sum "${ARCHIVE}.exe" > "${ARCHIVE}.exe.sha256" + fi + + - uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.artifact_name }} + path: | + WeKnora-Lite-App_* + retention-days: 3 + + # ── 3. Create GitHub Release ── + release: + needs: [build-binary, build-desktop-app] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/download-artifact@v4 + with: + pattern: release-* + merge-multiple: true + + - uses: actions/download-artifact@v4 + with: + pattern: desktop-* + merge-multiple: true + + - name: Resolve version + id: ver + run: | + if [ -n "${{ inputs.tag }}" ]; then + VERSION="${{ inputs.tag }}" + else + VERSION="${GITHUB_REF#refs/tags/}" + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + + - name: Generate release notes + id: notes + run: | + cat > notes.md << 'NOTES' + ## WeKnora Lite ${{ steps.ver.outputs.version }} + + ### Web CLI 版(命令行 / 服务器部署) + + 单二进制部署,零外部依赖(无需 Docker / PostgreSQL / Redis)。 + + ```bash + # 1. 解压 + tar xzf WeKnora-lite_${{ steps.ver.outputs.version }}__.tar.gz + cd WeKnora-lite_${{ steps.ver.outputs.version }}__ + + # 2. 配置 + cp .env.lite.example .env.lite + # 编辑 .env.lite,至少确认 OLLAMA_BASE_URL 正确 + + # 3. 启动 Ollama(如尚未运行) + ollama serve & + ollama pull qwen2.5:7b + ollama pull nomic-embed-text + + # 4. 运行 + set -a && source .env.lite && set +a + ./WeKnora-lite + # 访问 http://localhost:8080 + ``` + + | 文件 | 平台 | + |------|------| + | `WeKnora-lite_*_linux_amd64.tar.gz` | Linux x86_64 | + | `WeKnora-lite_*_linux_arm64.tar.gz` | Linux ARM64 | + | `WeKnora-lite_*_darwin_amd64.tar.gz` | macOS Intel | + | `WeKnora-lite_*_darwin_arm64.tar.gz` | macOS Apple Silicon | + + --- + + ### 桌面 APP 版(开箱即用,无需命令行) + + 原生桌面应用,内置 Web 服务,双击即可运行。 + + | 文件 | 平台 | + |------|------| + | `WeKnora-Lite-App_*_macOS_universal.dmg` | macOS (Intel + Apple Silicon) | + | `WeKnora-Lite-App_*_macOS_amd64.dmg` | macOS Intel | + | `WeKnora-Lite-App_*_macOS_arm64.dmg` | macOS Apple Silicon | + | `WeKnora-Lite-App_*_linux_amd64.tar.gz` | Linux x86_64 | + | `WeKnora-Lite-App_*_windows_amd64_setup.exe` | Windows x86_64 (安装包) | + + 详细文档见 [LITE.md](docs/LITE.md)。 + NOTES + + - name: Create release + env: + GH_TOKEN: ${{ github.token }} + run: | + gh release create "${{ steps.ver.outputs.version }}" \ + --title "WeKnora Lite ${{ steps.ver.outputs.version }}" \ + --notes-file notes.md \ + WeKnora-lite_*.tar.gz \ + WeKnora-lite_*.tar.gz.sha256 \ + WeKnora-Lite-App_* || true + + # ── 4. Update Homebrew Formula ── + update-homebrew: + needs: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: main + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Resolve version + id: ver + run: | + if [ -n "${{ inputs.tag }}" ]; then + VERSION="${{ inputs.tag }}" + else + VERSION="${GITHUB_REF#refs/tags/}" + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + + - name: Wait for release assets to be available + run: sleep 15 + + - name: Update Formula + run: ./scripts/update-homebrew-formula.sh "${{ steps.ver.outputs.version }}" + + - name: Commit and push + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add Formula/weknora-lite.rb + if git diff --cached --quiet; then + echo "No changes to Formula" + else + git commit -m "formula: update weknora-lite to ${{ steps.ver.outputs.version }}" + git push origin main + fi diff --git a/.gitignore b/.gitignore index d778d957..bae931dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ # 忽略所有隐藏文件和目录 .* -# 但不忽略示例文件 +# 但不忽略特定隐藏文件和目录 !.env.example !.gitignore +!.github/ # 敏感文件 *.pem @@ -29,6 +30,7 @@ logs/ WeKnora WeKnora-lite +cmd/desktop/build/bin/ /models/ test/data/mswag.txt data/files/ @@ -40,7 +42,7 @@ web/ **/__pycache__ /scripts/scale_dev_jobs.sh -server +/server frontend/.vite frontend/chrome-extension/ WeKnora-Chrome-Extension \ No newline at end of file diff --git a/Formula/weknora-lite.rb b/Formula/weknora-lite.rb new file mode 100644 index 00000000..cf925f79 --- /dev/null +++ b/Formula/weknora-lite.rb @@ -0,0 +1,118 @@ +class WeknoraLite < Formula + desc "Knowledge base management system — single-binary Lite edition" + homepage "https://github.com/Tencent/WeKnora" + version "0.3.6-test" + license "Apache-2.0" + + on_macos do + on_arm do + url "https://github.com/Tencent/WeKnora/releases/download/v#{version}/WeKnora-lite_v#{version}_darwin_arm64.tar.gz" + sha256 "1da2d4eef99e5cf8aa7a58501baa059e9e20482e1bd65a36a82321a89926c104" + end + on_intel do + url "https://github.com/Tencent/WeKnora/releases/download/v#{version}/WeKnora-lite_v#{version}_darwin_amd64.tar.gz" + sha256 "c187e16ac7671a615f012c82ebd89786e11fcf67cccc773eff175e4bdf7c9c06" + end + end + + on_linux do + on_arm do + url "https://github.com/Tencent/WeKnora/releases/download/v#{version}/WeKnora-lite_v#{version}_linux_arm64.tar.gz" + sha256 "bc4e184da005b60d1e8c037a61c58e643ebdc9bf14470fae6cd6227f52f02f1c" + end + on_intel do + url "https://github.com/Tencent/WeKnora/releases/download/v#{version}/WeKnora-lite_v#{version}_linux_amd64.tar.gz" + sha256 "cb34c50fb5b05555fca16084ffc7710524ff78badb3b1b82474eb89d21545d6e" + end + end + + def install + libexec.install "WeKnora-lite" + pkgshare.install "web" if File.directory?("web") + pkgshare.install "config" if File.directory?("config") + pkgshare.install ".env.lite.example" + doc.install "README.md" + pkgshare.install "migrations" if File.directory?("migrations") + + (bin/"weknora-lite").write <<~SH + #!/bin/bash + CONFIG_DIR="${WEKNORA_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/weknora}" + DATA_DIR="${WEKNORA_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/weknora}" + + mkdir -p "$DATA_DIR/files" "$CONFIG_DIR/config" 2>/dev/null + + if [ ! -f "$CONFIG_DIR/config/config.yaml" ]; then + cp -r "#{pkgshare}/config/" "$CONFIG_DIR/config/" + fi + + if [ ! -d "$CONFIG_DIR/migrations" ] && [ -d "#{pkgshare}/migrations" ]; then + ln -sf "#{pkgshare}/migrations" "$CONFIG_DIR/migrations" + fi + + if [ ! -f "$CONFIG_DIR/.env.lite" ]; then + cp "#{pkgshare}/.env.lite.example" "$CONFIG_DIR/.env.lite" + sed -i '' "s|DB_PATH=.*|DB_PATH=$DATA_DIR/weknora.db|" "$CONFIG_DIR/.env.lite" + sed -i '' "s|LOCAL_STORAGE_BASE_DIR=.*|LOCAL_STORAGE_BASE_DIR=$DATA_DIR/files|" "$CONFIG_DIR/.env.lite" + rm -f "$CONFIG_DIR/.env.lite-e" + echo "" + echo "已创建配置文件: $CONFIG_DIR/.env.lite" + echo "请根据需要编辑(如修改 LLM 地址、安全密钥等)。" + echo "" + fi + + set -a + source "$CONFIG_DIR/.env.lite" + set +a + + export DB_PATH="${DB_PATH:-$DATA_DIR/weknora.db}" + export LOCAL_STORAGE_BASE_DIR="${LOCAL_STORAGE_BASE_DIR:-$DATA_DIR/files}" + export WEKNORA_WEB_DIR="${WEKNORA_WEB_DIR:-#{pkgshare}/web}" + + cd "$CONFIG_DIR" + exec "#{libexec}/WeKnora-lite" "$@" + SH + end + + def post_install + (var/"weknora").mkpath + (var/"log").mkpath + end + + service do + run [bin/"weknora-lite"] + keep_alive true + working_dir var/"weknora" + log_path var/"log/weknora-lite.log" + error_log_path var/"log/weknora-lite.log" + end + + def caveats + <<~EOS + 前台运行: + weknora-lite + + 后台服务(推荐): + brew services start weknora-lite # 启动并开机自启 + brew services stop weknora-lite # 停止 + brew services restart weknora-lite # 重启 + brew services info weknora-lite # 查看状态 + + 日志: + #{var}/log/weknora-lite.log + + 首次运行会自动创建配置文件: + ~/.config/weknora/.env.lite + + 数据存储在: + ~/.local/share/weknora/ + + 如需修改配置(LLM 服务地址、安全密钥等): + $EDITOR ~/.config/weknora/.env.lite + brew services restart weknora-lite + EOS + end + + test do + assert_predicate bin/"weknora-lite", :executable? + end +end diff --git a/Makefile b/Makefile index 88faf527..547906d8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help build run test clean docker-build-app docker-build-docreader docker-build-frontend docker-build-all docker-run migrate-up migrate-down docker-restart docker-stop start-all stop-all start-ollama stop-ollama build-images build-images-app build-images-docreader build-images-frontend clean-images check-env list-containers pull-images show-platform dev-start dev-stop dev-restart dev-logs dev-status dev-app dev-frontend docs install-swagger +.PHONY: help build run test clean docker-build-app docker-build-docreader docker-build-frontend docker-build-all docker-run migrate-up migrate-down docker-restart docker-stop start-all stop-all start-ollama stop-ollama build-images build-images-app build-images-docreader build-images-frontend clean-images check-env list-containers pull-images show-platform dev-start dev-stop dev-restart dev-logs dev-status dev-app dev-frontend docs install-swagger build-lite run-lite package-lite # Show help help: @@ -56,6 +56,12 @@ help: @echo " dev-status 查看开发环境状态" @echo " dev-app 启动后端应用(本地运行,需先运行 dev-start)" @echo " dev-frontend 启动前端(本地运行,需先运行 dev-start)" + @echo "" + @echo "Lite 模式(零外部依赖):" + @echo " build-lite 构建 Lite 版本(先构建前端到 web/,再构建 Go;SKIP_FRONTEND=1 跳过前端)" + @echo " run-lite 构建并启动 Lite 版本" + @echo " package-lite 构建并打包 Lite 发行包(tarball)" + @echo " package-mac-app 构建并打包 macOS 桌面应用 (.app)" # Go related variables BINARY_NAME=WeKnora @@ -231,6 +237,39 @@ build-prod: LDFLAGS="-X 'github.com/Tencent/WeKnora/internal/handler.Version=$$VERSION' -X 'github.com/Tencent/WeKnora/internal/handler.Edition=standard' -X 'github.com/Tencent/WeKnora/internal/handler.CommitID=$$COMMIT_ID' -X 'github.com/Tencent/WeKnora/internal/handler.BuildTime=$$BUILD_TIME' -X 'github.com/Tencent/WeKnora/internal/handler.GoVersion=$$GO_VERSION' -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"; \ go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH) +# Build Lite version (single binary, SQLite + in-memory queue) +# 会先构建前端到 web/,再构建 Go 二进制;SKIP_FRONTEND=1 可跳过前端 +build-lite: + @if [ -f frontend/package.json ] && [ "$${SKIP_FRONTEND:-}" != "1" ]; then \ + echo ">> Building frontend for Lite..."; \ + (cd frontend && npm ci --prefer-offline && npm run build) && \ + rm -rf web && cp -r frontend/dist web; \ + elif [ "$${SKIP_FRONTEND:-}" = "1" ]; then \ + echo ">> Skipping frontend (SKIP_FRONTEND=1)"; \ + else \ + echo ">> No frontend/package.json, skipping frontend"; \ + fi + export EDITION=lite; \ + eval "$$(./scripts/get_version.sh env)"; \ + LDFLAGS="$$(./scripts/get_version.sh ldflags) -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"; \ + CGO_ENABLED=1 \ + CGO_CFLAGS="-Wno-deprecated-declarations" \ + CGO_LDFLAGS="-Wl,-no_warn_duplicate_libraries" \ + go build -tags "sqlite_fts5" -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME)-lite $(MAIN_PATH) + +# Run Lite version with .env.lite defaults +run-lite: build-lite + @if [ ! -f .env.lite ]; then echo "Error: .env.lite not found"; exit 1; fi + @set -a && . ./.env.lite && set +a && ./$(BINARY_NAME)-lite + +# Package Lite version into distributable tarball +package-lite: + ./scripts/package-lite.sh + +# Package Mac App +package-mac-app: + ./scripts/package-mac-app.sh + download_spatial: go run cmd/download/duckdb/duckdb.go diff --git a/README.md b/README.md index e42816b3..81d0e268 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ Fully modular pipeline from document parsing, vectorization, and retrieval to LL | LLMs | OpenAI / DeepSeek / Qwen (Alibaba Cloud) / Zhipu / Hunyuan / Doubao (Volcengine) / Gemini / MiniMax / NVIDIA / Novita AI / SiliconFlow / OpenRouter / Ollama | | Embeddings | Ollama / BGE / GTE / OpenAI-compatible APIs | | Vector DBs | PostgreSQL (pgvector) / Elasticsearch / Milvus / Weaviate / Qdrant | -| Object Storage | Local / MinIO / AWS S3 / Volcengine TOS | +| Object Storage | Local / MinIO / AWS S3 / Volcengine TOS / Alibaba Cloud OSS | | IM Channels | WeCom / Feishu / Slack / Telegram / DingTalk / Mattermost | | Web Search | DuckDuckGo / Bing / Google / Tavily | diff --git a/README_CN.md b/README_CN.md index 61900914..3cabb884 100644 --- a/README_CN.md +++ b/README_CN.md @@ -180,7 +180,7 @@ WeKnora 提供**快速问答**与**智能推理**两种问答模式。快速问 |------|------| | 模型厂商 | OpenAI / DeepSeek / Qwen(阿里云)/ 智谱 / 混元 / 豆包(火山引擎)/ Gemini / MiniMax / NVIDIA / Novita AI / SiliconFlow / OpenRouter / Ollama | | 向量数据库 | PostgreSQL (pgvector) / Elasticsearch / Milvus / Weaviate / Qdrant | -| 对象存储 | 本地 / 腾讯云COS / 火山引擎 TOS / MinIO / AWS S3 | +| 对象存储 | 本地 / 腾讯云COS / 火山引擎 TOS / MinIO / AWS S3 / 阿里云 OSS | | IM 集成 | 企业微信 / 飞书 / Slack / Telegram / 钉钉 / Mattermost | | 网络搜索 | DuckDuckGo / Bing / Google / Tavily | diff --git a/README_JA.md b/README_JA.md index 875a8e2a..6375076e 100644 --- a/README_JA.md +++ b/README_JA.md @@ -182,7 +182,7 @@ Feishuなどの外部プラットフォームからのナレッジ自動同期 | 大規模モデル | OpenAI / DeepSeek / Qwen (Alibaba Cloud) / Zhipu / Hunyuan / Doubao (Volcengine) / Gemini / MiniMax / NVIDIA / Novita AI / SiliconFlow / OpenRouter / Ollama | | Embedding | Ollama / BGE / GTE / OpenAI 互換 API | | ベクトル DB | PostgreSQL (pgvector) / Elasticsearch / Milvus / Weaviate / Qdrant | -| オブジェクトストレージ | ローカル / MinIO / AWS S3 / 火山引擎 TOS | +| オブジェクトストレージ | ローカル / MinIO / AWS S3 / 火山引擎 TOS / Alibaba Cloud OSS | | IM 統合 | WeChat Work / Feishu / Slack / Telegram / DingTalk / Mattermost | | Web 検索 | DuckDuckGo / Bing / Google / Tavily | diff --git a/README_KO.md b/README_KO.md index e7a8b612..44e0a91d 100644 --- a/README_KO.md +++ b/README_KO.md @@ -191,7 +191,7 @@ Feishu 등 외부 플랫폼에서 지식 자동 동기화를 지원하며(추가 | LLM | OpenAI / DeepSeek / Qwen (Alibaba Cloud) / Zhipu / Hunyuan / Doubao (Volcengine) / Gemini / MiniMax / NVIDIA / Novita AI / SiliconFlow / OpenRouter / Ollama | | Embedding | Ollama / BGE / GTE / OpenAI 호환 API | | 벡터 DB | PostgreSQL (pgvector) / Elasticsearch / Milvus / Weaviate / Qdrant | -| 오브젝트 스토리지 | 로컬 / MinIO / AWS S3 / Volcengine TOS | +| 오브젝트 스토리지 | 로컬 / MinIO / AWS S3 / Volcengine TOS / Alibaba Cloud OSS | | IM 통합 | WeChat Work / Feishu / Slack / Telegram / DingTalk / Mattermost | | 웹 검색 | DuckDuckGo / Bing / Google / Tavily | diff --git a/cmd/desktop/app.go b/cmd/desktop/app.go new file mode 100644 index 00000000..654185a1 --- /dev/null +++ b/cmd/desktop/app.go @@ -0,0 +1,84 @@ +package main + +import ( + "context" + "strings" +) + +// App holds Wails-bound state for the desktop shell. +type App struct { + ctx context.Context + backendURL string + apiLanBaseURL string + listenPublic bool + shutdownCh chan struct{} +} + +// NewApp creates a new App application struct. +func NewApp() *App { + return &App{ + shutdownCh: make(chan struct{}, 1), + } +} + +// startup is called when the application starts. +func (a *App) startup(ctx context.Context) { + a.ctx = ctx +} + +func (a *App) shutdown(ctx context.Context) { + a.shutdownCh <- struct{}{} +} + +// GetAPIBaseURL returns the local HTTP base URL for REST API calls (e.g. http://127.0.0.1:PORT/api/v1). +// The desktop shell proxies the webview to this address; window.location.origin is not the API host. +func (a *App) GetAPIBaseURL() string { + if a.backendURL == "" { + return "" + } + return strings.TrimRight(a.backendURL, "/") + "/api/v1" +} + +// GetDesktopHTTPPortSetting returns the saved local API port (0 = random port each launch). +func (a *App) GetDesktopHTTPPortSetting() int { + return LoadDesktopPrefsHTTPPort() +} + +// SetDesktopHTTPPortSetting saves the preferred local API port to application support. Restart the app for it to take effect unless it matches the current listener. +func (a *App) SetDesktopHTTPPortSetting(port int) error { + return SaveDesktopHTTPPortPreference(port) +} + +// GetDesktopHTTPBindPublicSetting returns whether API listens on all interfaces (0.0.0.0). +func (a *App) GetDesktopHTTPBindPublicSetting() bool { + return LoadDesktopHTTPBindPublic() +} + +// SetDesktopHTTPBindPublicSetting saves LAN/public listen preference. Restart the app for it to take effect. +func (a *App) SetDesktopHTTPBindPublicSetting(v bool) error { + return SaveDesktopHTTPBindPublicPreference(v) +} + +// GetAPILanBaseURL returns a suggested base URL for other devices on the LAN (…/api/v1), or empty if not in bind-public mode or IP detection failed. +func (a *App) GetAPILanBaseURL() string { + return a.apiLanBaseURL +} + +// GetDesktopListenPublicActive is true when this session’s API server is listening on all interfaces (runtime), not the saved preference. +func (a *App) GetDesktopListenPublicActive() bool { + return a.listenPublic +} + +// CheckForUpdates manually triggers the update check from the frontend. +func (a *App) CheckForUpdates() { + if a.ctx != nil { + checkUpdate(a.ctx, desktopAboutVersion(), true, false) + } +} + +// AutoCheckForUpdates silently checks for updates and downloads them. +func (a *App) AutoCheckForUpdates() { + if a.ctx != nil { + checkUpdate(a.ctx, desktopAboutVersion(), false, true) + } +} diff --git a/cmd/desktop/build/appicon.png b/cmd/desktop/build/appicon.png new file mode 100644 index 00000000..63617fe4 Binary files /dev/null and b/cmd/desktop/build/appicon.png differ diff --git a/cmd/desktop/build/darwin/Info.plist b/cmd/desktop/build/darwin/Info.plist new file mode 100644 index 00000000..d17a7475 --- /dev/null +++ b/cmd/desktop/build/darwin/Info.plist @@ -0,0 +1,63 @@ + + + + CFBundlePackageType + APPL + CFBundleName + {{.Info.ProductName}} + CFBundleExecutable + {{.OutputFilename}} + CFBundleIdentifier + com.wails.{{.Name}} + CFBundleVersion + {{.Info.ProductVersion}} + CFBundleGetInfoString + {{.Info.Comments}} + CFBundleShortVersionString + {{.Info.ProductVersion}} + CFBundleIconFile + iconfile + LSMinimumSystemVersion + 10.13.0 + NSHighResolutionCapable + true + NSHumanReadableCopyright + {{.Info.Copyright}} + {{if .Info.FileAssociations}} + CFBundleDocumentTypes + + {{range .Info.FileAssociations}} + + CFBundleTypeExtensions + + {{.Ext}} + + CFBundleTypeName + {{.Name}} + CFBundleTypeRole + {{.Role}} + CFBundleTypeIconFile + {{.IconName}} + + {{end}} + + {{end}} + {{if .Info.Protocols}} + CFBundleURLTypes + + {{range .Info.Protocols}} + + CFBundleURLName + com.wails.{{.Scheme}} + CFBundleURLSchemes + + {{.Scheme}} + + CFBundleTypeRole + {{.Role}} + + {{end}} + + {{end}} + + diff --git a/cmd/desktop/main.go b/cmd/desktop/main.go new file mode 100644 index 00000000..04ae2a2c --- /dev/null +++ b/cmd/desktop/main.go @@ -0,0 +1,517 @@ +//go:build !bindings + +package main + +import ( + "context" + "fmt" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "os/signal" + "path/filepath" + "strconv" + "strings" + "syscall" + "time" + + "github.com/gin-gonic/gin" + "github.com/wailsapp/wails/v2" + "github.com/wailsapp/wails/v2/pkg/menu" + "github.com/wailsapp/wails/v2/pkg/menu/keys" + "github.com/wailsapp/wails/v2/pkg/options" + "github.com/wailsapp/wails/v2/pkg/options/assetserver" + "github.com/wailsapp/wails/v2/pkg/options/mac" + + "github.com/Tencent/WeKnora/internal/config" + "github.com/Tencent/WeKnora/internal/container" + "github.com/Tencent/WeKnora/internal/logger" + "github.com/Tencent/WeKnora/internal/runtime" + "github.com/Tencent/WeKnora/internal/tracing" + "github.com/Tencent/WeKnora/internal/types/interfaces" + "github.com/joho/godotenv" + wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime" +) + +// dragHandlerJS is injected into the webview on DomReady. +// It bypasses Wails' built-in CSS-variable-based drag detection (which uses +// getComputedStyle and has timing/inheritance issues with dynamic SPA content) +// and instead uses robust DOM-traversal via el.closest() plus a Y-position +// fallback for the macOS title-bar region on layout containers. The "drag" +// message is sent directly through the WKWebView script-message bridge, +// which the native Objective-C handler in WailsContext.m converts to +// [NSWindow performWindowDragWithEvent:]. +const dragHandlerJS = `(function(){ +if(window.__wkDragBound)return; +window.__wkDragBound=true; +document.documentElement.classList.add('wails-desktop'); + +// Disable rubber-band overscroll that reveals the dark window background +document.documentElement.style.overscrollBehavior='none'; +document.body.style.overscrollBehavior='none'; + +if(window.wails&&window.wails.flags){ + window.wails.flags.cssDragProperty='__disabled__'; + window.wails.flags.cssDragValue='__never__'; +} + +// Prevent native text/image drag-out to fix the "selected and dragged away" issue +window.addEventListener('dragstart', function(e){ + e.preventDefault(); +}, true); + +var TITLEBAR_H=38; + +// We specifically look for Wails' inline style attributes injected by Vue, +// and custom drag classes, avoiding generic headers like .section-header +var dragSel='.logo_row,.menu_top,.drag-region,[data-wails-drag],' + + '[style*="--wails-draggable: drag"],[style*="--wails-draggable:drag"]'; + +var noDragSel='button,a,input,select,textarea,[role="button"],' + + '.t-button,.t-input,.t-select,.t-textarea,' + + '.header-actions,.header-action-btn,.sidebar-toggle,.logo_box,' + + '.close-btn,.menu_item,.submenu,.submenu_item,.menu_bottom,' + + '.t-popup,.t-dropdown,.t-tooltip,.t-dialog,[data-no-drag],' + + '[style*="--wails-draggable: no-drag"],[style*="--wails-draggable:no-drag"]'; + +var layoutClasses=['main','chat','dialogue-wrap','kb-list-container','kb-list-content', + 'agent-list-container','agent-list-content','org-list-container','org-list-content','aside_box', + 'ks-container','ks-content','settings-overlay','knowledge-layout', + 'faq-manager-wrapper','login-layout']; + +function sendDrag(){ + try{window.webkit.messageHandlers.external.postMessage('drag')} + catch(_){try{window.WailsInvoke('drag')}catch(e){}} +} + +function isLayoutEl(el){ + for(var i=0;i/config/,向上两级即可。 + repoRoot := filepath.Clean(filepath.Join("..", "..")) + if _, err := os.Stat(filepath.Join(repoRoot, "config", "config.yaml")); err == nil { + _ = os.Chdir(repoRoot) + } + } + + // Load .env explicitly for the desktop app so DB_DRIVER gets loaded + _ = godotenv.Load() + configureDesktopStorage(execPath) + logger.ConfigureFromEnv() + + // Set Gin mode + if os.Getenv("GIN_MODE") == "release" { + gin.SetMode(gin.ReleaseMode) + } else { + gin.SetMode(gin.DebugMode) + } + + // Build dependency injection container + c := container.BuildContainer(runtime.GetContainer()) + + // Initialize the WeKnora App struct + app := NewApp() + + // Error channel to capture server startup errors + serverErrCh := make(chan error, 1) + + // Run backend in a separate goroutine + go func() { + err := c.Invoke(func( + cfg *config.Config, + router *gin.Engine, + tracer *tracing.Tracer, + resourceCleaner interfaces.ResourceCleaner, + ) error { + server := &http.Server{Handler: router} + + // 127.0.0.1 + saved port from settings (desktop-prefs.json), or :0 for random free port. + addr := desktopBackendListenAddr() + + listener, err := listenWithRetry(addr, 10, 300*time.Millisecond) + if err != nil { + return fmt.Errorf("failed to start server: %v", err) + } + + tcpAddr := listener.Addr().(*net.TCPAddr) + port := tcpAddr.Port + app.listenPublic = LoadDesktopHTTPBindPublic() + // Reverse proxy and webview API calls always use loopback; avoid 0.0.0.0 / [::] as dial target. + app.backendURL = fmt.Sprintf("http://127.0.0.1:%d", port) + app.apiLanBaseURL = "" + if app.listenPublic { + if ip := desktopPreferredLANIPv4(); ip != nil { + app.apiLanBaseURL = fmt.Sprintf("http://%s:%d/api/v1", ip.String(), port) + } + } + + // Handle graceful shutdown from Wails OnShutdown hook + go func() { + <-app.shutdownCh + logger.Infof(context.Background(), "Wails shutting down, stopping Go backend...") + + listener.Close() + shutdownTimeout := cfg.Server.ShutdownTimeout + if shutdownTimeout == 0 { + shutdownTimeout = 30 * time.Second + } + shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + + if err := server.Shutdown(shutdownCtx); err != nil { + server.Close() + } + resourceCleaner.Cleanup(shutdownCtx) + }() + + // Also listen for OS signals just in case + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-signals + app.shutdownCh <- struct{}{} // trigger shutdown + }() + + logger.Infof(context.Background(), "Server is running at %s (proxy -> %s)", tcpAddr.String(), app.backendURL) + if err := server.Serve(listener); err != nil && err != http.ErrServerClosed { + return fmt.Errorf("server error: %v", err) + } + return nil + }) + + if err != nil { + serverErrCh <- err + logger.Fatalf(context.Background(), "Failed to run backend: %v", err) + } + }() + + // Give the server a moment to start and determine its port + time.Sleep(500 * time.Millisecond) + + // Create application with options + // macOS app menu + AppMenu := menu.NewMenu() + FileMenu := AppMenu.AddSubmenu("WeKnora Lite") + FileMenu.AddText("About WeKnora", keys.CmdOrCtrl("i"), func(_ *menu.CallbackData) { + if app.ctx == nil { + return + } + choice, err := wailsruntime.MessageDialog(app.ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.InfoDialog, + Title: "WeKnora Lite", + Message: fmt.Sprintf("WeKnora Lite — Desktop Edition\n\nA RAG framework for document understanding and semantic Q&A over complex, heterogeneous content.\n\nVersion %s\n© 2026 Tencent\n\nGitHub:\n%s", desktopAboutVersion(), weknoraGitHubRepoURL), + Buttons: []string{"Open GitHub", "OK"}, + DefaultButton: "OK", + }) + if err != nil { + logger.Warnf(context.Background(), "About dialog: %v", err) + return + } + if choice == "Open GitHub" { + wailsruntime.BrowserOpenURL(app.ctx, weknoraGitHubRepoURL) + } + }) + FileMenu.AddText("Check for Updates...", nil, func(_ *menu.CallbackData) { + if app.ctx == nil { + return + } + checkUpdate(app.ctx, desktopAboutVersion(), true, false) + }) + FileMenu.AddSeparator() + FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) { + app.shutdown(context.Background()) + os.Exit(0) + }) + + AppMenu.Append(menu.EditMenu()) + + ViewMenu := AppMenu.AddSubmenu("View") + ViewMenu.AddText("Reload", keys.CmdOrCtrl("r"), func(_ *menu.CallbackData) { + if app.ctx != nil { + wailsruntime.EventsEmit(app.ctx, "app:reload") + } + }) + + // Wait for the backend URL to be set + targetURL, _ := url.Parse(app.backendURL) + proxy := httputil.NewSingleHostReverseProxy(targetURL) + + // Start Wails application + // We use a Reverse Proxy to seamlessly proxy Wails' frontend to our Go backend + err := wails.Run(&options.App{ + Title: "WeKnora Lite", + Width: 1280, + Height: 800, + DisableResize: false, + Menu: AppMenu, + AssetServer: &assetserver.Options{ + Handler: proxy, + }, + StartHidden: false, // Show window on startup + OnStartup: app.startup, + OnDomReady: func(ctx context.Context) { + wailsruntime.WindowExecJS(ctx, wailsThemeSyncJS) + wailsruntime.WindowExecJS(ctx, dragHandlerJS) + // 注入真实 API 根路径(与 window.location.origin 不同);无 Go 绑定时仍可显示。 + if u := strings.TrimSpace(app.backendURL); u != "" { + apiRoot := strings.TrimRight(u, "/") + "/api/v1" + inject := fmt.Sprintf(`try{window.__WEKNORA_API_BASE__=%s}catch(e){}`, strconv.Quote(apiRoot)) + wailsruntime.WindowExecJS(ctx, inject) + } + if lan := strings.TrimSpace(app.apiLanBaseURL); lan != "" { + injectLan := fmt.Sprintf(`try{window.__WEKNORA_API_LAN_BASE__=%s}catch(e){}`, strconv.Quote(lan)) + wailsruntime.WindowExecJS(ctx, injectLan) + } + }, + OnShutdown: app.shutdown, + Bind: []interface{}{ + app, + }, + BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 255}, + Mac: &mac.Options{ + TitleBar: mac.TitleBarHiddenInset(), + WebviewIsTransparent: false, + WindowIsTranslucent: false, + }, + }) + + if err != nil { + println("Error:", err.Error()) + } +} + +func configureDesktopStorage(execPath string) { + if execPath == "" || !strings.Contains(execPath, ".app/Contents/MacOS") { + return + } + + appSupportDir, err := defaultMacAppSupportDir(execPath) + if err != nil { + logger.Warnf(context.Background(), "Failed to resolve app support dir: %v", err) + return + } + + legacyResourcesDir := filepath.Join(filepath.Dir(filepath.Dir(execPath)), "Resources") + targetDataDir := filepath.Join(appSupportDir, "data") + migrateLegacyDesktopData(legacyResourcesDir, targetDataDir) + + dbPath := resolveDesktopDataPath(os.Getenv("DB_PATH"), filepath.Join("data", "weknora.db"), appSupportDir) + filesPath := resolveDesktopDataPath(os.Getenv("LOCAL_STORAGE_BASE_DIR"), filepath.Join("data", "files"), appSupportDir) + + if err := os.MkdirAll(filepath.Dir(dbPath), 0o755); err != nil { + logger.Warnf(context.Background(), "Failed to create desktop DB directory %s: %v", filepath.Dir(dbPath), err) + } + if err := os.MkdirAll(filesPath, 0o755); err != nil { + logger.Warnf(context.Background(), "Failed to create desktop files directory %s: %v", filesPath, err) + } + + _ = os.Setenv("DB_PATH", dbPath) + _ = os.Setenv("LOCAL_STORAGE_BASE_DIR", filesPath) +} + +func defaultMacAppSupportDir(execPath string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + appName := "WeKnora Lite" + if idx := strings.Index(execPath, ".app/Contents/MacOS"); idx >= 0 { + bundleName := filepath.Base(execPath[:idx+4]) + if trimmed := strings.TrimSuffix(bundleName, ".app"); trimmed != "" { + appName = trimmed + } + } + + return filepath.Join(homeDir, "Library", "Application Support", appName), nil +} + +func resolveDesktopDataPath(rawPath, defaultRelativePath, appSupportDir string) string { + trimmed := strings.TrimSpace(rawPath) + if trimmed == "" { + trimmed = defaultRelativePath + } + if filepath.IsAbs(trimmed) { + return filepath.Clean(trimmed) + } + trimmed = strings.TrimPrefix(trimmed, "."+string(filepath.Separator)) + return filepath.Join(appSupportDir, filepath.Clean(trimmed)) +} + +// desktopBackendListenAddr returns the TCP address for the embedded Gin server (Wails desktop). +// Binds 127.0.0.1 by default, or 0.0.0.0 when http_bind_public is set in desktop-prefs.json. +func desktopBackendListenAddr() string { + host := "127.0.0.1" + if LoadDesktopHTTPBindPublic() { + host = "0.0.0.0" + } + pref := LoadDesktopPrefsHTTPPort() + if pref >= 1 && pref <= 65535 { + return net.JoinHostPort(host, strconv.Itoa(pref)) + } + return net.JoinHostPort(host, "0") +} + +// desktopPreferredLANIPv4 picks a non-loopback IPv4 for “other devices on this network” URL hints. +func desktopPreferredLANIPv4() net.IP { + ifaces, err := net.Interfaces() + if err == nil { + for _, iface := range ifaces { + if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { + continue + } + addrs, aerr := iface.Addrs() + if aerr != nil { + continue + } + for _, a := range addrs { + ipNet, ok := a.(*net.IPNet) + if !ok || ipNet.IP == nil { + continue + } + ip4 := ipNet.IP.To4() + if ip4 == nil || ip4.IsLoopback() { + continue + } + if ip4.IsPrivate() { + return ip4 + } + } + } + for _, iface := range ifaces { + if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { + continue + } + addrs, aerr := iface.Addrs() + if aerr != nil { + continue + } + for _, a := range addrs { + ipNet, ok := a.(*net.IPNet) + if !ok || ipNet.IP == nil { + continue + } + ip4 := ipNet.IP.To4() + if ip4 == nil || ip4.IsLoopback() || ip4.IsUnspecified() { + continue + } + return ip4 + } + } + } + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + return nil + } + defer conn.Close() + la, ok := conn.LocalAddr().(*net.UDPAddr) + if !ok || la.IP == nil { + return nil + } + return la.IP.To4() +} + +func migrateLegacyDesktopData(resourcesDir, targetDataDir string) { + legacyDataDir := filepath.Join(resourcesDir, "data") + if info, err := os.Stat(legacyDataDir); err != nil || !info.IsDir() { + return + } + if _, err := os.Stat(targetDataDir); err == nil { + return + } + if err := os.MkdirAll(filepath.Dir(targetDataDir), 0o755); err != nil { + logger.Warnf(context.Background(), "Failed to create app support parent dir %s: %v", filepath.Dir(targetDataDir), err) + return + } + if err := os.Rename(legacyDataDir, targetDataDir); err != nil { + logger.Warnf(context.Background(), "Failed to migrate legacy desktop data from %s to %s: %v", legacyDataDir, targetDataDir, err) + return + } + logger.Infof(context.Background(), "Migrated legacy desktop data to %s", targetDataDir) +} + +func listenWithRetry(addr string, maxRetries int, baseDelay time.Duration) (net.Listener, error) { + var lastErr error + for i := 0; i < maxRetries; i++ { + listener, err := net.Listen("tcp", addr) + if err == nil { + return listener, nil + } + lastErr = err + if i < maxRetries-1 { + delay := baseDelay * time.Duration(1< 3*time.Second { + delay = 3 * time.Second + } + time.Sleep(delay) + } + } + return nil, lastErr +} diff --git a/cmd/desktop/main_bindings.go b/cmd/desktop/main_bindings.go new file mode 100644 index 00000000..650d377d --- /dev/null +++ b/cmd/desktop/main_bindings.go @@ -0,0 +1,17 @@ +//go:build bindings + +package main + +import ( + "github.com/wailsapp/wails/v2" + "github.com/wailsapp/wails/v2/pkg/options" +) + +// Wails「生成绑定」阶段使用 -tags bindings 单独编译本文件,不启动 Gin/数据库,避免依赖本机 Postgres。 +func main() { + app := NewApp() + _ = wails.Run(&options.App{ + Title: "WeKnora Lite", + Bind: []interface{}{app}, + }) +} diff --git a/cmd/desktop/prefs.go b/cmd/desktop/prefs.go new file mode 100644 index 00000000..45a1a635 --- /dev/null +++ b/cmd/desktop/prefs.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" +) + +const desktopPrefsFileName = "desktop-prefs.json" + +type desktopPrefs struct { + HTTPPort int `json:"http_port"` + HTTPBindPublic bool `json:"http_bind_public"` +} + +func desktopPrefsDir() (string, error) { + cfg, err := os.UserConfigDir() + if err != nil { + return "", err + } + dir := filepath.Join(cfg, "WeKnora Lite") + if err := os.MkdirAll(dir, 0o755); err != nil { + return "", err + } + return dir, nil +} + +func desktopPrefsFilePath() (string, error) { + dir, err := desktopPrefsDir() + if err != nil { + return "", err + } + return filepath.Join(dir, desktopPrefsFileName), nil +} + +func loadDesktopPrefs() desktopPrefs { + path, err := desktopPrefsFilePath() + if err != nil { + return desktopPrefs{} + } + data, err := os.ReadFile(path) + if err != nil { + return desktopPrefs{} + } + var p desktopPrefs + if json.Unmarshal(data, &p) != nil { + return desktopPrefs{} + } + if p.HTTPPort < 0 || p.HTTPPort > 65535 { + p.HTTPPort = 0 + } + return p +} + +func saveDesktopPrefs(p desktopPrefs) error { + path, err := desktopPrefsFilePath() + if err != nil { + return err + } + data, err := json.MarshalIndent(p, "", " ") + if err != nil { + return err + } + return os.WriteFile(path, data, 0o600) +} + +// LoadDesktopPrefsHTTPPort returns http_port from prefs file, or 0 if unset / invalid (ephemeral port on each launch). +func LoadDesktopPrefsHTTPPort() int { + return loadDesktopPrefs().HTTPPort +} + +// LoadDesktopHTTPBindPublic returns whether the embedded API server should listen on all interfaces (0.0.0.0). +func LoadDesktopHTTPBindPublic() bool { + return loadDesktopPrefs().HTTPBindPublic +} + +// SaveDesktopHTTPPortPreference persists listen port preference. port 0 means use a random free port on each launch. +func SaveDesktopHTTPPortPreference(port int) error { + if port < 0 || port > 65535 { + return fmt.Errorf("invalid port") + } + cur := loadDesktopPrefs() + cur.HTTPPort = port + return saveDesktopPrefs(cur) +} + +// SaveDesktopHTTPBindPublicPreference persists whether to listen on 0.0.0.0 for LAN/public access. +func SaveDesktopHTTPBindPublicPreference(v bool) error { + cur := loadDesktopPrefs() + cur.HTTPBindPublic = v + return saveDesktopPrefs(cur) +} diff --git a/cmd/desktop/update.go b/cmd/desktop/update.go new file mode 100644 index 00000000..a1bd8684 --- /dev/null +++ b/cmd/desktop/update.go @@ -0,0 +1,413 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "time" + + "github.com/Tencent/WeKnora/internal/handler" + "github.com/Tencent/WeKnora/internal/logger" + wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime" + "golang.org/x/mod/semver" +) + +type githubAsset struct { + Name string `json:"name"` + BrowserDownloadURL string `json:"browser_download_url"` +} + +type githubRelease struct { + TagName string `json:"tag_name"` + HTMLURL string `json:"html_url"` + Assets []githubAsset `json:"assets"` +} + +func checkUpdate(ctx context.Context, currentVersion string, showUpToDate bool, autoDownload bool) { + go func() { + if currentVersion == "unknown" || currentVersion == "" { + if showUpToDate { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.InfoDialog, + Title: "Check for Updates", + Message: "Unable to determine the current version. Cannot check for updates.", + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + + if !strings.HasPrefix(currentVersion, "v") { + currentVersion = "v" + currentVersion + } + + client := &http.Client{Timeout: 10 * time.Second} + req, err := http.NewRequest("GET", "https://api.github.com/repos/Tencent/WeKnora/releases/latest", nil) + if err != nil { + logger.Warnf(context.Background(), "Check update failed: %v", err) + return + } + + // Add User-Agent header which is required/recommended by GitHub API + req.Header.Set("User-Agent", "WeKnora-Lite-Desktop-App") + + // Add Authorization header if GITHUB_TOKEN is present to increase rate limit + if token := os.Getenv("GITHUB_TOKEN"); token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + + resp, err := client.Do(req) + if err != nil { + logger.Warnf(context.Background(), "Check update failed: %v", err) + if showUpToDate { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Check Update Failed", + Message: fmt.Sprintf("Failed to connect to GitHub to check for updates:\n%v", err), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + logger.Warnf(context.Background(), "GitHub API returned status code: %d", resp.StatusCode) + if showUpToDate { + msg := fmt.Sprintf("GitHub API returned an unexpected status code: %d", resp.StatusCode) + if resp.StatusCode == 403 { + msg = "GitHub API rate limit exceeded. Please try again later." + } + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Check Update Failed", + Message: msg, + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + + var release githubRelease + if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { + logger.Warnf(context.Background(), "Failed to parse release info: %v", err) + return + } + + latestVersion := release.TagName + if !strings.HasPrefix(latestVersion, "v") { + latestVersion = "v" + latestVersion + } + + if semver.IsValid(latestVersion) && semver.IsValid(currentVersion) { + if semver.Compare(latestVersion, currentVersion) > 0 { + assetURL, assetName := findBestAsset(release.Assets, runtime.GOOS, runtime.GOARCH) + + if autoDownload && assetURL != "" { + // Silent download in background + downloadAndInstall(ctx, assetURL, assetName, currentVersion, latestVersion, true) + return + } + + msg := fmt.Sprintf("A new version of WeKnora Lite is available!\n\nCurrent version: %s\nLatest version: %s\n\nWould you like to download it now?", currentVersion, latestVersion) + choice, _ := wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.InfoDialog, + Title: "Update Available", + Message: msg, + Buttons: []string{"Download", "Cancel"}, + DefaultButton: "Download", + }) + if choice == "Download" { + if assetURL != "" { + downloadAndInstall(ctx, assetURL, assetName, currentVersion, latestVersion, false) + } else { + // Fallback to opening the release page if no specific asset is found + wailsruntime.BrowserOpenURL(ctx, release.HTMLURL) + } + } + return + } + } + + if showUpToDate { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.InfoDialog, + Title: "Up to Date", + Message: fmt.Sprintf("You are using the latest version of WeKnora Lite.\n\nCurrent version: %s", currentVersion), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + }() +} + +func findBestAsset(assets []githubAsset, goos, goarch string) (string, string) { + osKeyword := "" + switch goos { + case "darwin": + osKeyword = "mac" + case "windows": + osKeyword = "win" + case "linux": + osKeyword = "linux" + } + + archKeyword := "" + switch goarch { + case "amd64": + archKeyword = "amd64" + case "arm64": + archKeyword = "arm64" + } + + // 1. Try to match both OS and Arch + for _, asset := range assets { + name := strings.ToLower(asset.Name) + if strings.Contains(name, osKeyword) && (strings.Contains(name, archKeyword) || strings.Contains(name, "universal") || strings.Contains(name, "aarch64")) { + return asset.BrowserDownloadURL, asset.Name + } + } + + // 2. Try to match OS only (e.g. universal binaries without arch in name) + for _, asset := range assets { + name := strings.ToLower(asset.Name) + if strings.Contains(name, osKeyword) { + return asset.BrowserDownloadURL, asset.Name + } + } + + // 3. MacOS specific fallback (e.g. .dmg) + if goos == "darwin" { + for _, asset := range assets { + if strings.HasSuffix(strings.ToLower(asset.Name), ".dmg") { + return asset.BrowserDownloadURL, asset.Name + } + } + } + + // 4. Windows specific fallback (e.g. .exe) + if goos == "windows" { + for _, asset := range assets { + if strings.HasSuffix(strings.ToLower(asset.Name), ".exe") { + return asset.BrowserDownloadURL, asset.Name + } + } + } + + return "", "" +} + +func downloadAndInstall(ctx context.Context, url string, filename string, currentVersion string, latestVersion string, silent bool) { + tempDir := os.TempDir() + savePath := filepath.Join(tempDir, filename) + + go func() { + logger.Infof(context.Background(), "Starting download from %s to %s", url, savePath) + resp, err := http.Get(url) + if err != nil { + logger.Warnf(context.Background(), "Download failed: %v", err) + if !silent { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Download Failed", + Message: fmt.Sprintf("Failed to download the update:\n%v", err), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + logger.Warnf(context.Background(), "Download failed, server returned status: %d", resp.StatusCode) + if !silent { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Download Failed", + Message: fmt.Sprintf("Server returned status: %d", resp.StatusCode), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + + out, err := os.Create(savePath) + if err != nil { + logger.Warnf(context.Background(), "Failed to create file for download: %v", err) + if !silent { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Save Failed", + Message: fmt.Sprintf("Failed to save the update file:\n%v", err), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + logger.Warnf(context.Background(), "Error occurred during download copying: %v", err) + if !silent { + wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.ErrorDialog, + Title: "Download Error", + Message: fmt.Sprintf("An error occurred while downloading:\n%v", err), + Buttons: []string{"OK"}, + DefaultButton: "OK", + }) + } + return + } + + logger.Infof(context.Background(), "Download completed successfully: %s", savePath) + + // Prompt user to restart + choice, _ := wailsruntime.MessageDialog(ctx, wailsruntime.MessageDialogOptions{ + Type: wailsruntime.InfoDialog, + Title: "Update Ready", + Message: fmt.Sprintf("WeKnora Lite %s has been downloaded successfully.\n\nWould you like to restart and install the new version now?", latestVersion), + Buttons: []string{"Restart Now", "Later"}, + DefaultButton: "Restart Now", + }) + + if choice == "Restart Now" { + applyUpdateAndRestart(ctx, savePath) + } + }() +} + +// desktopAboutVersion 优先使用构建脚本注入的 handler.Version,否则尝试读取仓库根目录 VERSION(本地 wails dev 等未带 ldflags 时)。 +func desktopAboutVersion() string { + if v := strings.TrimSpace(handler.Version); v != "" && v != "unknown" { + return v + } + for _, p := range []string{ + "VERSION", + filepath.Join("..", "..", "VERSION"), + filepath.Join("..", "..", "..", "VERSION"), + } { + b, err := os.ReadFile(p) + if err != nil { + continue + } + if v := strings.TrimSpace(string(b)); v != "" { + return v + } + } + return "unknown" +} + +// applyUpdateAndRestart applies the downloaded update and restarts the application +func applyUpdateAndRestart(ctx context.Context, savePath string) { + if runtime.GOOS == "windows" { + scriptPath := filepath.Join(os.TempDir(), "weknora_update.bat") + execPath, err := os.Executable() + if err != nil { + logger.Warnf(context.Background(), "Failed to get executable path: %v", err) + return + } + scriptContent := fmt.Sprintf(`@echo off +timeout /t 2 /nobreak +start /wait "" "%s" /S +start "" "%s" +del "%%~f0" +`, savePath, execPath) + os.WriteFile(scriptPath, []byte(scriptContent), 0755) + + cmd := exec.Command("cmd.exe", "/C", "start", "/b", scriptPath) + cmd.Start() + wailsruntime.Quit(ctx) + + } else if runtime.GOOS == "darwin" { + if strings.HasSuffix(strings.ToLower(savePath), ".dmg") { + go func() { + execPath, err := os.Executable() + if err != nil { + logger.Warnf(context.Background(), "Failed to get executable path: %v", err) + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + return + } + + appBundlePath := filepath.Dir(filepath.Dir(filepath.Dir(execPath))) + if !strings.HasSuffix(appBundlePath, ".app") { + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + return + } + + mountPoint := filepath.Join(os.TempDir(), "WeKnoraUpdateMount") + os.MkdirAll(mountPoint, 0755) + + cmdMount := exec.Command("hdiutil", "attach", savePath, "-mountpoint", mountPoint, "-nobrowse", "-quiet") + if err := cmdMount.Run(); err != nil { + logger.Warnf(context.Background(), "Failed to mount dmg: %v", err) + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + return + } + + entries, err := os.ReadDir(mountPoint) + if err != nil { + exec.Command("hdiutil", "detach", mountPoint, "-force").Run() + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + return + } + + var newAppPath string + for _, entry := range entries { + if strings.HasSuffix(entry.Name(), ".app") { + newAppPath = filepath.Join(mountPoint, entry.Name()) + break + } + } + + if newAppPath == "" { + exec.Command("hdiutil", "detach", mountPoint, "-force").Run() + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + return + } + + appDir := filepath.Dir(appBundlePath) + scriptPath := filepath.Join(os.TempDir(), "weknora_update.sh") + scriptContent := fmt.Sprintf(`#!/bin/bash +sleep 2 +if ! (rm -rf "%s" && cp -a "%s" "%s"); then + osascript -e "do shell script \"rm -rf \\\"%s\\\" && cp -a \\\"%s\\\" \\\"%s\\\"\" with administrator privileges" +fi +hdiutil detach "%s" -force +open "%s" +rm "$0" +`, appBundlePath, newAppPath, appDir, appBundlePath, newAppPath, appDir, mountPoint, appBundlePath) + + os.WriteFile(scriptPath, []byte(scriptContent), 0755) + + exec.Command("bash", scriptPath).Start() + wailsruntime.Quit(ctx) + }() + } else { + exec.Command("open", savePath).Start() + wailsruntime.Quit(ctx) + } + } else { + exec.Command("xdg-open", savePath).Start() + wailsruntime.Quit(ctx) + } +} diff --git a/cmd/desktop/wails.json b/cmd/desktop/wails.json new file mode 100644 index 00000000..72932f44 --- /dev/null +++ b/cmd/desktop/wails.json @@ -0,0 +1,22 @@ +{ + "$schema": "https://wails.io/schemas/config.v2.json", + "name": "WeKnora Lite", + "outputfilename": "WeKnora Lite", + "frontend:dir": "../../frontend", + "wailsjsdir": "../../frontend/src", + "author": { + "name": "WeKnora Team", + "email": "" + }, + "info": { + "companyName": "Tencent", + "productName": "WeKnora Lite", + "productVersion": "1.0.0", + "copyright": "Copyright 2026 Tencent", + "comments": "WeKnora Desktop Edition" + }, + "mac": { + "category": "public.app-category.productivity", + "titlebar": "hiddenInset" + } +} diff --git a/cmd/server/listen.go b/cmd/server/listen.go new file mode 100644 index 00000000..8df1ddf9 --- /dev/null +++ b/cmd/server/listen.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "net" + "time" + + "github.com/Tencent/WeKnora/internal/logger" +) + +func listenWithRetry(addr string, maxRetries int, baseDelay time.Duration) (net.Listener, error) { + var lastErr error + for i := 0; i < maxRetries; i++ { + listener, err := net.Listen("tcp", addr) + if err == nil { + return listener, nil + } + lastErr = err + if i < maxRetries-1 { + delay := baseDelay * time.Duration(1< 3*time.Second { + delay = 3 * time.Second + } + logger.Warnf(context.Background(), "Port %s in use, retrying in %v... (%d/%d)", addr, delay, i+1, maxRetries) + time.Sleep(delay) + } + } + return nil, lastErr +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 6f471297..459c830f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -25,11 +25,9 @@ package main import ( "context" "fmt" - "net" "net/http" "os" "os/signal" - "syscall" "time" "github.com/gin-gonic/gin" @@ -40,8 +38,6 @@ import ( "github.com/Tencent/WeKnora/internal/runtime" "github.com/Tencent/WeKnora/internal/tracing" "github.com/Tencent/WeKnora/internal/types/interfaces" - - "golang.org/x/sys/unix" ) func main() { @@ -76,7 +72,7 @@ func main() { ctx, done := context.WithCancel(context.Background()) signals := make(chan os.Signal, 1) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + signal.Notify(signals, shutdownSignals...) go func() { sig := <-signals logger.Infof(context.Background(), "Received signal: %v, starting server shutdown...", sig) @@ -125,33 +121,3 @@ func main() { logger.Fatalf(context.Background(), "Failed to run application: %v", err) } } - -// listenWithRetry retries listening with exponential backoff and SO_REUSEPORT, -// useful during hot-reload when the previous process may not have released the port yet. -func listenWithRetry(addr string, maxRetries int, baseDelay time.Duration) (net.Listener, error) { - lc := net.ListenConfig{ - Control: func(network, address string, c syscall.RawConn) error { - return c.Control(func(fd uintptr) { - _ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1) - }) - }, - } - - var lastErr error - for i := 0; i < maxRetries; i++ { - listener, err := lc.Listen(context.Background(), "tcp", addr) - if err == nil { - return listener, nil - } - lastErr = err - if i < maxRetries-1 { - delay := baseDelay * time.Duration(1< 3*time.Second { - delay = 3 * time.Second - } - logger.Warnf(context.Background(), "Port %s in use, retrying in %v... (%d/%d)", addr, delay, i+1, maxRetries) - time.Sleep(delay) - } - } - return nil, lastErr -} diff --git a/cmd/server/signals_unix.go b/cmd/server/signals_unix.go new file mode 100644 index 00000000..0b82816f --- /dev/null +++ b/cmd/server/signals_unix.go @@ -0,0 +1,10 @@ +//go:build !windows + +package main + +import ( + "os" + "syscall" +) + +var shutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP} diff --git a/cmd/server/signals_windows.go b/cmd/server/signals_windows.go new file mode 100644 index 00000000..620cae71 --- /dev/null +++ b/cmd/server/signals_windows.go @@ -0,0 +1,10 @@ +//go:build windows + +package main + +import ( + "os" + "syscall" +) + +var shutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM} diff --git a/deploy/weknora-lite.service b/deploy/weknora-lite.service new file mode 100644 index 00000000..dac3bb29 --- /dev/null +++ b/deploy/weknora-lite.service @@ -0,0 +1,23 @@ +[Unit] +Description=WeKnora Lite - Knowledge Base Management System +After=network.target + +[Service] +Type=simple +User=weknora +Group=weknora +WorkingDirectory=/opt/weknora +EnvironmentFile=/opt/weknora/.env.lite +ExecStart=/opt/weknora/WeKnora-lite +Restart=on-failure +RestartSec=5 +LimitNOFILE=65536 + +# Security hardening +NoNewPrivileges=true +ProtectSystem=strict +ProtectHome=true +ReadWritePaths=/opt/weknora/data + +[Install] +WantedBy=multi-user.target diff --git a/docker-compose.yml b/docker-compose.yml index f10f339b..54549cbe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -116,6 +116,11 @@ services: - SSRF_WHITELIST=${SSRF_WHITELIST:-} - CONCURRENCY_POOL_SIZE=${CONCURRENCY_POOL_SIZE:-5} - JWT_SECRET=${JWT_SECRET:-} + # Crypto: 主密钥和盐值,用于 AppSecret 等敏感字段的 AES-256 加密 + # 若不设置则自动生成并持久化到 data-files volume(/data/files/.crypto_state.json) + # 重启时自动从文件恢复,保证已加密数据可继续解密 + - CRYPTO_MASTER_KEY=${CRYPTO_MASTER_KEY:-} + - CRYPTO_SALT=${CRYPTO_SALT:-} # File size limit (in MB) - MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-50} # Agent Skills Sandbox diff --git a/docker/Dockerfile.app b/docker/Dockerfile.app index 1d9ce32b..d681cd67 100644 --- a/docker/Dockerfile.app +++ b/docker/Dockerfile.app @@ -66,7 +66,8 @@ RUN if [ -n "$APK_MIRROR_ARG" ]; then \ libsqlite3-0 \ python3 python3-pip python3-dev libffi-dev libssl-dev \ nodejs npm \ - gosu && \ + gosu \ + ffmpeg && \ python3 -m pip install --break-system-packages --upgrade pip setuptools wheel && \ mkdir -p /home/appuser/.local/bin && \ curl -LsSf https://astral.sh/uv/install.sh | CARGO_HOME=/home/appuser/.cargo UV_INSTALL_DIR=/home/appuser/.local/bin sh && \ diff --git a/docreader/README.md b/docreader/README.md index 071e2e5b..19f49541 100644 --- a/docreader/README.md +++ b/docreader/README.md @@ -126,6 +126,16 @@ DocReader 支持多种存储后端: - `COS_PATH_PREFIX`: 文件路径前缀 - `COS_ENABLE_OLD_DOMAIN`: 是否使用旧域名(默认:true) +#### 阿里云 OSS 存储 + +- `STORAGE_TYPE`: 设置为 `oss` +- `OSS_ACCESS_KEY_ID`: OSS 访问密钥 ID +- `OSS_ACCESS_KEY_SECRET`: OSS 访问密钥 +- `OSS_ENDPOINT`: OSS 端点(如 `oss-cn-hangzhou.aliyuncs.com`) +- `OSS_BUCKET_NAME`: OSS 存储桶名称 +- `OSS_REGION`: OSS 区域(如 `cn-hangzhou`) +- `OSS_PATH_PREFIX`: 文件路径前缀 + ### 代理配置 如果需要通过代理访问外部服务: @@ -178,6 +188,20 @@ docreader: - MAX_FILE_SIZE_MB=50 ``` +### 使用阿里云 OSS + +```yaml +docreader: + environment: + - STORAGE_TYPE=oss + - OSS_ACCESS_KEY_ID=your_access_key_id + - OSS_ACCESS_KEY_SECRET=your_access_key_secret + - OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com + - OSS_BUCKET_NAME=your-bucket + - OSS_REGION=cn-hangzhou + - MAX_FILE_SIZE_MB=50 +``` + ## 常见问题 ### 1. DocReader 服务无法启动? diff --git a/docreader/parser/storage.py b/docreader/parser/storage.py index 7f62896f..6e97838b 100644 --- a/docreader/parser/storage.py +++ b/docreader/parser/storage.py @@ -10,6 +10,11 @@ from typing import Dict, Optional from minio import Minio from qcloud_cos import CosConfig, CosS3Client +try: + import oss2 +except ImportError: + oss2 = None # type: ignore[assignment] + from docreader.utils import endecode logger = logging.getLogger(__name__) @@ -232,6 +237,99 @@ class MinioStorage(Storage): return "" +class OssStorage(Storage): + """Alibaba Cloud OSS storage implementation""" + + def __init__(self, storage_config: Optional[Dict] = None): + self.storage_config = storage_config + self.bucket, self.endpoint, self.prefix = self._init_oss_bucket() + + def _init_oss_bucket(self): + if oss2 is None: + logger.error("oss2 package not installed. Install with: pip install oss2") + return None, None, None + try: + sc = self.storage_config + access_key = _cfg(sc, "access_key", "OSS_ACCESS_KEY") + secret_key = _cfg(sc, "secret_key", "OSS_SECRET_KEY") + endpoint_raw = _cfg(sc, "endpoint", "OSS_ENDPOINT") + bucket_name = _cfg(sc, "bucket_name", "OSS_BUCKET_NAME") + prefix_raw = _cfg(sc, "path_prefix", "OSS_PATH_PREFIX") + + if not all([access_key, secret_key, endpoint_raw, bucket_name]): + logger.error( + "Incomplete OSS configuration: " + "access_key=%s, endpoint=%s, bucket=%s", + bool(access_key), endpoint_raw, bucket_name, + ) + return None, None, None + + # oss2 requires endpoint to include the scheme (http:// or https://) + if not endpoint_raw.startswith(("http://", "https://")): + endpoint_raw = f"https://{endpoint_raw}" + + auth = oss2.Auth(access_key, secret_key) + bucket = oss2.Bucket(auth, endpoint_raw, bucket_name) + + # Verify bucket exists; do NOT auto-create to avoid unintended public-read buckets + try: + bucket.get_bucket_info() + except oss2.exceptions.NoSuchBucket: + logger.error( + "OSS bucket %s does not exist. Please create it manually in the console.", + bucket_name, + ) + return None, None, None + + prefix = prefix_raw.strip().strip("/") if prefix_raw else "" + return bucket, endpoint_raw, prefix + + except Exception as e: + logger.error("Failed to initialize OSS client: %s", e) + return None, None, None + + def _get_download_url(self, object_key: str) -> str: + # Use virtual-hosted style URL: https://{bucket}.{endpoint_without_scheme}/{key} + endpoint_no_scheme = self.endpoint.removeprefix("https://").removeprefix("http://") + return f"https://{self.bucket.bucket_name}.{endpoint_no_scheme}/{object_key}" + + def upload_file(self, file_path: str) -> str: + try: + if not self.bucket: + return "" + file_ext = os.path.splitext(file_path)[1] + object_key = ( + f"{self.prefix}/images/{uuid.uuid4().hex}{file_ext}" + if self.prefix + else f"images/{uuid.uuid4().hex}{file_ext}" + ) + self.bucket.put_object_from_file(object_key, file_path) + file_url = self._get_download_url(object_key) + logger.info("OSS upload_file ok: %s", file_url) + return file_url + except Exception as e: + logger.error("OSS upload_file failed: %s", e) + return "" + + def upload_bytes(self, content: bytes, file_ext: str = ".png") -> str: + try: + if not self.bucket: + return "" + object_key = ( + f"{self.prefix}/images/{uuid.uuid4().hex}{file_ext}" + if self.prefix + else f"images/{uuid.uuid4().hex}{file_ext}" + ) + self.bucket.put_object(object_key, content) + file_url = self._get_download_url(object_key) + logger.info("OSS upload_bytes ok: %s", file_url) + return file_url + except Exception as e: + logger.error("OSS upload_bytes failed: %s", e) + traceback.print_exc() + return "" + + class LocalStorage(Storage): """Local file system storage implementation. @@ -297,7 +395,7 @@ def create_storage(storage_config: Optional[Dict[str, str]] = None) -> Storage: """Create a storage instance based on storage_config dict. The ``provider`` key in storage_config determines the backend: - minio, cos, local, base64. + minio, cos, oss, local, base64. Falls back to STORAGE_TYPE env var, then ``local``. """ storage_type = "" @@ -315,6 +413,8 @@ def create_storage(storage_config: Optional[Dict[str, str]] = None) -> Storage: return MinioStorage(storage_config) elif storage_type == "cos": return CosStorage(storage_config) + elif storage_type == "oss": + return OssStorage(storage_config) elif storage_type == "local": return LocalStorage(storage_config) elif storage_type == "base64": diff --git a/docreader/pyproject.toml b/docreader/pyproject.toml index bc6e846c..31005e91 100644 --- a/docreader/pyproject.toml +++ b/docreader/pyproject.toml @@ -21,6 +21,7 @@ dependencies = [ "mistletoe>=1.5.0", "ollama>=0.6.0", "openai>=2.7.1", + "oss2>=2.18.0", "paddleocr>=2.10.0,<3.0.0", "paddlepaddle>=3.0.0,<4.0.0", "pdfplumber>=0.11.7", diff --git a/docreader/uv.lock b/docreader/uv.lock index 796919ff..3f138646 100644 --- a/docreader/uv.lock +++ b/docreader/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 2 +revision = 3 requires-python = ">=3.10.18" resolution-markers = [ "python_full_version >= '3.14' and sys_platform == 'darwin'", @@ -59,6 +59,28 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8e/64/013409c451a44b61310fb757af4527f3de57fc98a00f40448de28b864290/albumentations-2.0.8-py3-none-any.whl", hash = "sha256:c4c4259aaf04a7386ad85c7fdcb73c6c7146ca3057446b745cc035805acb1017", size = 369423, upload-time = "2025-05-27T21:23:15.609Z" }, ] +[[package]] +name = "aliyun-python-sdk-core" +version = "2.16.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cryptography" }, + { name = "jmespath" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/3e/09/da9f58eb38b4fdb97ba6523274fbf445ef6a06be64b433693da8307b4bec/aliyun-python-sdk-core-2.16.0.tar.gz", hash = "sha256:651caad597eb39d4fad6cf85133dffe92837d53bdf62db9d8f37dab6508bb8f9", size = 449555, upload-time = "2024-10-09T06:01:01.762Z" } + +[[package]] +name = "aliyun-python-sdk-kms" +version = "2.16.5" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "aliyun-python-sdk-core" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a8/2c/9877d0e6b18ecf246df671ac65a5d1d9fecbf85bdcb5d43efbde0d4662eb/aliyun-python-sdk-kms-2.16.5.tar.gz", hash = "sha256:f328a8a19d83ecbb965ffce0ec1e9930755216d104638cd95ecd362753b813b3", size = 12018, upload-time = "2024-08-30T09:01:20.104Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/11/5c/0132193d7da2c735669a1ed103b142fd63c9455984d48c5a88a1a516efaa/aliyun_python_sdk_kms-2.16.5-py2.py3-none-any.whl", hash = "sha256:24b6cdc4fd161d2942619479c8d050c63ea9cd22b044fe33b60bbb60153786f0", size = 99495, upload-time = "2024-08-30T09:01:18.462Z" }, +] + [[package]] name = "annotated-types" version = "0.7.0" @@ -690,6 +712,7 @@ dependencies = [ { name = "mistletoe" }, { name = "ollama" }, { name = "openai" }, + { name = "oss2" }, { name = "paddleocr" }, { name = "paddlepaddle" }, { name = "pdfplumber" }, @@ -724,6 +747,7 @@ requires-dist = [ { name = "mistletoe", specifier = ">=1.5.0" }, { name = "ollama", specifier = ">=0.6.0" }, { name = "openai", specifier = ">=2.7.1" }, + { name = "oss2", specifier = ">=2.18.0" }, { name = "paddleocr", specifier = ">=2.10.0,<3.0.0" }, { name = "paddlepaddle", specifier = ">=3.0.0,<4.0.0" }, { name = "pdfplumber", specifier = ">=0.11.7" }, @@ -1322,6 +1346,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d9/71/71408b02c6133153336d29fa3ba53000f1e1a3f78bb2fc2d1a1865d2e743/jiter-0.11.1-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:18c77aaa9117510d5bdc6a946baf21b1f0cfa58ef04d31c8d016f206f2118960", size = 343697, upload-time = "2025-10-17T11:31:13.773Z" }, ] +[[package]] +name = "jmespath" +version = "0.10.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/3c/56/3f325b1eef9791759784aa5046a8f6a1aff8f7c898a2e34506771d3b99d8/jmespath-0.10.0.tar.gz", hash = "sha256:b85d0567b8666149a93172712e68920734333c0ce7e89b78b3e987f71e5ed4f9", size = 21607, upload-time = "2020-05-12T22:03:47.267Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/07/cb/5f001272b6faeb23c1c9e0acc04d48eaaf5c862c17709d20e3469c6e0139/jmespath-0.10.0-py2.py3-none-any.whl", hash = "sha256:cdf6525904cc597730141d61b36f2e4b8ecc257c420fa2f4549bac2c2d0cb72f", size = 24489, upload-time = "2020-05-12T22:03:45.643Z" }, +] + [[package]] name = "joblib" version = "1.5.2" @@ -1984,21 +2017,16 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/35/d6/311b1afea060015b56c742f3531168c1644650767f27ef40062569960587/onnxruntime-1.23.2-cp310-cp310-macosx_13_0_arm64.whl", hash = "sha256:a7730122afe186a784660f6ec5807138bf9d792fa1df76556b27307ea9ebcbe3", size = 17195934, upload-time = "2025-10-27T23:06:14.143Z" }, { url = "https://files.pythonhosted.org/packages/db/db/81bf3d7cecfbfed9092b6b4052e857a769d62ed90561b410014e0aae18db/onnxruntime-1.23.2-cp310-cp310-macosx_13_0_x86_64.whl", hash = "sha256:b28740f4ecef1738ea8f807461dd541b8287d5650b5be33bca7b474e3cbd1f36", size = 19153079, upload-time = "2025-10-27T23:05:57.686Z" }, { url = "https://files.pythonhosted.org/packages/2e/4d/a382452b17cf70a2313153c520ea4c96ab670c996cb3a95cc5d5ac7bfdac/onnxruntime-1.23.2-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8f7d1fe034090a1e371b7f3ca9d3ccae2fabae8c1d8844fb7371d1ea38e8e8d2", size = 15219883, upload-time = "2025-10-22T03:46:21.66Z" }, - { url = "https://files.pythonhosted.org/packages/fb/56/179bf90679984c85b417664c26aae4f427cba7514bd2d65c43b181b7b08b/onnxruntime-1.23.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4ca88747e708e5c67337b0f65eed4b7d0dd70d22ac332038c9fc4635760018f7", size = 17370357, upload-time = "2025-10-22T03:46:57.968Z" }, { url = "https://files.pythonhosted.org/packages/44/be/467b00f09061572f022ffd17e49e49e5a7a789056bad95b54dfd3bee73ff/onnxruntime-1.23.2-cp311-cp311-macosx_13_0_arm64.whl", hash = "sha256:6f91d2c9b0965e86827a5ba01531d5b669770b01775b23199565d6c1f136616c", size = 17196113, upload-time = "2025-10-22T03:47:33.526Z" }, { url = "https://files.pythonhosted.org/packages/9f/a8/3c23a8f75f93122d2b3410bfb74d06d0f8da4ac663185f91866b03f7da1b/onnxruntime-1.23.2-cp311-cp311-macosx_13_0_x86_64.whl", hash = "sha256:87d8b6eaf0fbeb6835a60a4265fde7a3b60157cf1b2764773ac47237b4d48612", size = 19153857, upload-time = "2025-10-22T03:46:37.578Z" }, { url = "https://files.pythonhosted.org/packages/3f/d8/506eed9af03d86f8db4880a4c47cd0dffee973ef7e4f4cff9f1d4bcf7d22/onnxruntime-1.23.2-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bbfd2fca76c855317568c1b36a885ddea2272c13cb0e395002c402f2360429a6", size = 15220095, upload-time = "2025-10-22T03:46:24.769Z" }, - { url = "https://files.pythonhosted.org/packages/e9/80/113381ba832d5e777accedc6cb41d10f9eca82321ae31ebb6bcede530cea/onnxruntime-1.23.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:da44b99206e77734c5819aa2142c69e64f3b46edc3bd314f6a45a932defc0b3e", size = 17372080, upload-time = "2025-10-22T03:47:00.265Z" }, { url = "https://files.pythonhosted.org/packages/1b/9e/f748cd64161213adeef83d0cb16cb8ace1e62fa501033acdd9f9341fff57/onnxruntime-1.23.2-cp312-cp312-macosx_13_0_arm64.whl", hash = "sha256:b8f029a6b98d3cf5be564d52802bb50a8489ab73409fa9db0bf583eabb7c2321", size = 17195929, upload-time = "2025-10-22T03:47:36.24Z" }, { url = "https://files.pythonhosted.org/packages/91/9d/a81aafd899b900101988ead7fb14974c8a58695338ab6a0f3d6b0100f30b/onnxruntime-1.23.2-cp312-cp312-macosx_13_0_x86_64.whl", hash = "sha256:218295a8acae83905f6f1aed8cacb8e3eb3bd7513a13fe4ba3b2664a19fc4a6b", size = 19157705, upload-time = "2025-10-22T03:46:40.415Z" }, { url = "https://files.pythonhosted.org/packages/3c/35/4e40f2fba272a6698d62be2cd21ddc3675edfc1a4b9ddefcc4648f115315/onnxruntime-1.23.2-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:76ff670550dc23e58ea9bc53b5149b99a44e63b34b524f7b8547469aaa0dcb8c", size = 15226915, upload-time = "2025-10-22T03:46:27.773Z" }, - { url = "https://files.pythonhosted.org/packages/ef/88/9cc25d2bafe6bc0d4d3c1db3ade98196d5b355c0b273e6a5dc09c5d5d0d5/onnxruntime-1.23.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0f9b4ae77f8e3c9bee50c27bc1beede83f786fe1d52e99ac85aa8d65a01e9b77", size = 17382649, upload-time = "2025-10-22T03:47:02.782Z" }, { url = "https://files.pythonhosted.org/packages/3d/41/fba0cabccecefe4a1b5fc8020c44febb334637f133acefc7ec492029dd2c/onnxruntime-1.23.2-cp313-cp313-macosx_13_0_arm64.whl", hash = "sha256:2ff531ad8496281b4297f32b83b01cdd719617e2351ffe0dba5684fb283afa1f", size = 17196337, upload-time = "2025-10-22T03:46:35.168Z" }, { url = "https://files.pythonhosted.org/packages/fe/f9/2d49ca491c6a986acce9f1d1d5fc2099108958cc1710c28e89a032c9cfe9/onnxruntime-1.23.2-cp313-cp313-macosx_13_0_x86_64.whl", hash = "sha256:162f4ca894ec3de1a6fd53589e511e06ecdc3ff646849b62a9da7489dee9ce95", size = 19157691, upload-time = "2025-10-22T03:46:43.518Z" }, { url = "https://files.pythonhosted.org/packages/1c/a1/428ee29c6eaf09a6f6be56f836213f104618fb35ac6cc586ff0f477263eb/onnxruntime-1.23.2-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:45d127d6e1e9b99d1ebeae9bcd8f98617a812f53f46699eafeb976275744826b", size = 15226898, upload-time = "2025-10-22T03:46:30.039Z" }, - { url = "https://files.pythonhosted.org/packages/f2/2b/b57c8a2466a3126dbe0a792f56ad7290949b02f47b86216cd47d857e4b77/onnxruntime-1.23.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8bace4e0d46480fbeeb7bbe1ffe1f080e6663a42d1086ff95c1551f2d39e7872", size = 17382518, upload-time = "2025-10-22T03:47:05.407Z" }, { url = "https://files.pythonhosted.org/packages/7c/3d/6830fa61c69ca8e905f237001dbfc01689a4e4ab06147020a4518318881f/onnxruntime-1.23.2-cp313-cp313t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9d2385e774f46ac38f02b3a91a91e30263d41b2f1f4f26ae34805b2a9ddef466", size = 15229610, upload-time = "2025-10-22T03:46:32.239Z" }, - { url = "https://files.pythonhosted.org/packages/b6/ca/862b1e7a639460f0ca25fd5b6135fb42cf9deea86d398a92e44dfda2279d/onnxruntime-1.23.2-cp313-cp313t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e2b9233c4947907fd1818d0e581c049c41ccc39b2856cc942ff6d26317cee145", size = 17394184, upload-time = "2025-10-22T03:47:08.127Z" }, ] [[package]] @@ -2099,6 +2127,20 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/bc/19/404708a7e54ad2798907210462fd950c3442ea51acc8790f3da48d2bee8b/opt_einsum-3.3.0-py3-none-any.whl", hash = "sha256:2455e59e3947d3c275477df7f5205b30635e266fe6dc300e3d9f9646bfcea147", size = 65486, upload-time = "2020-07-19T22:40:30.301Z" }, ] +[[package]] +name = "oss2" +version = "2.19.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "aliyun-python-sdk-core" }, + { name = "aliyun-python-sdk-kms" }, + { name = "crcmod" }, + { name = "pycryptodome" }, + { name = "requests" }, + { name = "six" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/df/b5/f2cb1950dda46ac2284d6c950489fdacd0e743c2d79a347924d3cc44b86f/oss2-2.19.1.tar.gz", hash = "sha256:a8ab9ee7eb99e88a7e1382edc6ea641d219d585a7e074e3776e9dec9473e59c1", size = 298845, upload-time = "2024-10-25T11:37:46.638Z" } + [[package]] name = "packaging" version = "25.0" diff --git a/docs/LITE.md b/docs/LITE.md new file mode 100644 index 00000000..d40b53bd --- /dev/null +++ b/docs/LITE.md @@ -0,0 +1,16 @@ +# WeKnora Lite 与标准版区别 + +Lite 面向希望快速在本地使用、部署尽量简单的场景;标准版面向多租户协作与完整企业能力。主要差异如下。 + + +| 维度 | Lite | 标准版 | +| --------- | ----------------------------------------------- | --------------------------------- | +| **共享空间** | 不提供共享空间(成员邀请、跨成员共享知识库与智能体等) | 提供共享空间及租户隔离检索等协作能力 | +| **租户与账号** | 单租户;开箱即用,**无需注册** | 多租户;通常需要注册、登录与组织管理 | +| **文档解析** | 内置仅 **Simple** 类型解析引擎;可通过 **Cloud** 等方式接入其他解析能力 | 可配置多种解析引擎(含高精度等),与完整文档处理链路集成 | +| **部署形态** | **单应用、零依赖**(不依赖独立的数据库、消息队列等外部服务栈) | 典型为 Docker Compose 等多服务部署,依赖与组件更多 | +| **数据归属** | 数据**完全在本地**存储与处理 | 私有化部署时数据也可在本地;具体取决于你的部署方式 | +| **网络暴露** | **默认仅本机访问**;可按需配置,**选择是否放行到公网** | 按部署与安全策略自行绑定地址与网关 | + + +若你不需要多团队协作、复杂解析流水线与多服务架构,Lite 更适合个人或小团队在本机零依赖试用;需要共享空间、多租户与完整解析引擎矩阵时,请使用标准版。 \ No newline at end of file diff --git a/docs/QA.md b/docs/QA.md index 581629e1..96279f93 100644 --- a/docs/QA.md +++ b/docs/QA.md @@ -160,6 +160,24 @@ docker compose restart app 如果重启后短时间恢复正常,但再次访问又出现相同现象,仍应优先检查浏览器代理、缓存和多环境串连问题,而不是直接判断为后端配置丢失。 +## 8. SSRF 校验白名单(`SSRF_WHITELIST`) + +可选配置。在 `.env` 中设置 `SSRF_WHITELIST`,用于在 URL 校验等环节将指定目标加入白名单,从而绕过常规 SSRF 限制。值为逗号分隔的多条规则,每条可以是: + +- **精确域名**:如 `api.internal` +- **通配域名**:如 `*.example.com` +- **IPv4**:如 `203.0.113.5` +- **IPv6**:如 `2001:db8::1`(不要带方括号) +- **CIDR**:如 `10.0.0.0/8`、`2001:db8::/32` + +列入白名单的地址会在 URL 校验等处绕过常规 SSRF 规则,**生产环境请谨慎配置**,仅加入确实需要且可信的目标。 + +示例(与 `.env.example` 一致,可按需取消注释并修改): + +```bash +# SSRF_WHITELIST=internal.service,*.corp.example,172.16.0.0/12,2001:db8::1,fd00::/8 +``` + ## P.S. 如果以上方式未解决问题,请在issue中描述您的问题,并提供必要的日志信息辅助我们进行问题排查 diff --git a/docs/WeKnora.md b/docs/WeKnora.md deleted file mode 100644 index f0de040d..00000000 --- a/docs/WeKnora.md +++ /dev/null @@ -1,330 +0,0 @@ -## 介绍 -WeKora 是一个可立即在生产环境投入的企业级RAG框架,实现智能文档理解和检索功能。该系统采用模块化设计,将文档理解、向量存储、推理文件等功能分离。 - -![arc](./images/arc.png) - ---- - -## PipeLine -WeKnora 处理文档需要多个步骤:插入-》知识提取-》索引-》检索-》生成,整个流程支持多种检索方法, - -![](./images/pipeline2.jpeg) - - -以用户上传的一张住宿流水单pdf文件为例,详细介绍下其数据流: - -### 1. 接收请求与初始化 -+ **请求识别**: 系统收到一个请求,并为其分配了唯一的 `request_id=Lkq0OGLYu2fV`,用于追踪整个处理流程。 -+ **租户与会话验证**: - - 系统首先验证了租户信息(ID: 1, Name: Default Tenant)。 - - 接着开始处理一个知识库问答(Knowledge QA)请求,该请求属于会话 `1f241340-ae75-40a5-8731-9a3a82e34fdd`。 -+ **用户问题**: 用户的原始问题是:“**入住的房型是什么**”。 -+ **消息创建**: 系统为用户的提问和即将生成的回答分别创建了消息记录,ID 分别为 `703ddf09-...` 和 `6f057649-...`。 - -### 2. 知识库问答流程启动 -系统正式调用知识库问答服务,并定义了将要按顺序执行的完整处理管道(Pipeline),包含以下9个事件: -`[rewrite_query, preprocess_query, chunk_search, chunk_rerank, chunk_merge, filter_top_k, into_chat_message, chat_completion_stream, stream_filter]` - ---- - -### 3. 事件执行详情 -#### 事件 1: `rewrite_query` - 问题改写 -+ **目的**: 为了让检索更精确,系统需要结合上下文来理解用户的真实意图。 -+ **操作**: - 1. 系统检索了当前会话最近的20条历史消息(实际检索到8条)作为上下文。 - 2. 调用了一个名为 `deepseek-r1:7b` 的本地大语言模型。 - 3. 模型根据聊天历史分析出提问者是“Liwx”,并将原问题“入住的房型是什么”改写得更具体。 -+ **结果**: 问题被成功改写为:“**Liwx本次入住的房型是什么**”。 - -#### 事件 2: `preprocess_query` - 问题预处理 -+ **目的**: 将改写后的问题进行分词,转换为适合搜索引擎处理的关键词序列。 -+ **操作**: 对改写后的问题进行了分词处理。 -+ **结果**: 生成了一串关键词:“`需要 改写 用户 问题 入住 房型 根据 提供 信息 入住 人 Liwx 选择 房型 双床 房 因此 改写 后 完整 问题 为 Liwx 本次 入住 房型`”。 - -#### 事件 3: `chunk_search` - 知识区块检索 -这是最核心的**检索(Retrieval)**步骤,系统执行了两次混合搜索(Hybrid Search)。 - -+ **第一次搜索 (使用改写后的完整问句)**: - - **向量检索**: - 1. 加载嵌入模型 `bge-m3:latest` 将问句转换为一个1024维的向量。 - 2. 在PostgreSQL数据库中进行向量相似度搜索,找到了2个相关的知识区块(chunk),ID 分别为 `e3bf6599-...` 和 `3989c6ce-...`。 - - **关键词检索**: - 1. 同时,系统也进行了关键词搜索。 - 2. 同样找到了上述2个知识区块。 - - **结果合并**: 两种方法找到的4个结果(实际是2个重复的)被去重,最终得到2个唯一的知识区块。 -+ **第二次搜索 (使用预处理后的关键词序列)**: - - 系统使用分词后的关键词重复了上述的**向量检索**和**关键词检索**过程。 - - 最终也得到了相同的2个知识区块。 -+ **最终结果**: 经过两次搜索和结果合并,系统锁定了2个最相关的知识区块,并将它们的内容提取出来,准备用于生成答案。 - -#### 事件 4: `chunk_rerank` - 结果重排序 -+ **目的**: 使用一个更强大的模型对初步检索出的结果进行更精细的排序,以提高最终答案的质量。 -+ **操作**: 日志显示 `Rerank model ID is empty, skipping reranking`。这意味着系统配置了重排序步骤,但没有指定具体的重排序模型,因此**跳过了此步骤**。 - -#### 事件 5: `chunk_merge` - 区块合并 -+ **目的**: 将内容上相邻或相关的知识区块进行合并,形成更完整的上下文。 -+ **操作**: 系统分析了检索到的2个区块,并尝试进行合并。根据日志,最终处理后仍然是2个独立的区块,但已按相关性分数排好序。 - -#### 事件 6: `filter_top_k` - Top-K 过滤 -+ **目的**: 仅保留最相关的K个结果,防止过多无关信息干扰语言模型。 -+ **操作**: 系统配置保留前5个(Top-K = 5)最相关的区块。由于当前只有2个区块,它们全部通过了此过滤器。 - -#### 事件 7 & 8: `into_chat_message` & `chat_completion_stream` - 生成回答 -这是**生成(Generation)**步骤。 - -+ **目的**: 基于检索到的信息,生成自然流畅的回答。 -+ **操作**: - 1. 系统将检索到的2个知识区块的内容、用户的原始问题以及聊天历史整合在一起,形成一个完整的提示(Prompt)。 - 2. 再次调用 `deepseek-r1:7b` 大语言模型,并以**流式(Stream)**的方式请求生成答案。流式输出可以实现打字机效果,提升用户体验。 - -#### 事件 9: `stream_filter` - 流式输出过滤 -+ **目的**: 对模型生成的实时文本流进行后处理,过滤掉不需要的特殊标记或内容。 -+ **操作**: - - 系统设置了一个过滤器,用于移除模型在思考过程中可能产生的内部标记,如 `` 和 ``。 - - 日志显示,模型输出的第一个词块是 ` 根据`,过滤器成功拦截并移除了 `` 标记,只将“根据”及之后的内容传递下去。 - -### 4. 完成与响应 -+ **发送引用**: 在生成答案的同时,系统将作为依据的2个知识区块作为“参考内容”发送给前端,以便用户查证来源。 -+ **更新消息**: 当模型生成完所有内容后,系统将完整的回答更新到之前创建的消息记录(ID: `6f057649-...`)中。 -+ **请求结束**: 服务器返回 `200` 成功状态码,标志着本次从提问到回答的完整流程结束。 - -### 总结 -这个日志完整地记录了一次典型的RAG流程:系统通过**问题改写**和**预处理**来精确理解用户意图,接着利用**向量与关键词混合检索**从知识库中找到相关信息,虽然跳过了**重排序**,但依然执行了**合并**与**过滤**,最后将检索到的知识作为上下文,交由大语言模型**生成**流畅、准确的回答,并通过**流式过滤**保证了输出的纯净性。 - -## 文档解析切分 -代码实现了一个独立的、通过gRPC通信的微服务,专门负责文档内容的深度解析、分块和多模态信息提取。它正是“异步处理”阶段的核心执行者。 - -### **整体架构** -这是一个基于Python的gRPC服务,其核心职责是接收文件(或URL),并将其解析成结构化的、可供后续处理(如向量化)的文本块(Chunks)。 - -+ `server.py`: 服务的入口和网络层。它负责启动一个多进程、多线程的gRPC服务器,接收来自Go后端的请求,并将解析结果返回。 -+ `parser.py`: 设计模式中的**外观(Facade)模式**。它提供了一个统一的`Parser`类,屏蔽了内部多种具体解析器(如PDF、DOCX、Markdown等)的复杂性。外部调用者(`server.py`)只需与这个`Parser`类交互。 -+ `base_parser.py`: 解析器的基类,定义了所有具体解析器共享的核心逻辑和抽象方法。这是整个解析流程的“大脑”,包含了最复杂的文本分块、图片处理、OCR和图像描述生成等功能。 - ---- - -### **详细工作流程** -当Go后端启动异步任务时,它会携带文件内容和配置信息,向这个Python服务发起一次gRPC调用。以下是完整的处理流程: - -#### **第一步:请求接收与分发 (**`server.py`** & **`parser.py`**) -1. **gRPC服务入口 (**`server.py: serve`**)**: - - 服务通过`serve()`函数启动。它会根据环境变量(`GRPC_WORKER_PROCESSES`, `GRPC_MAX_WORKERS`)启动一个**多进程、多线程**的服务器,以充分利用CPU资源,提高并发处理能力。 - - 每个工作进程都监听在指定的端口(如50051),准备接收请求。 -2. **请求处理 (**`server.py: ReadFromFile`**)**: - - 当Go后端发起`ReadFromFile`请求时,其中一个工作进程会接收到该请求。 - - 该方法首先会解析请求中的参数,包括: - * `file_name`, `file_type`, `file_content`:文件的基本信息和二进制内容。 - * `read_config`: 一个包含所有解析配置的复杂对象,如`chunk_size`(分块大小)、`chunk_overlap`(重叠大小)、`enable_multimodal`(是否启用多模态处理)、`storage_config`(对象存储配置)、`vlm_config`(视觉语言模型配置)等。 - - 它将这些配置整合成一个`ChunkingConfig`数据对象。 - - 最关键的一步是调用 `self.parser.parse_file(...)`,将解析任务交给`Parser`外观类处理。 -3. **解析器选择 (**`parser.py: Parser.parse_file`**)**: - - `Parser`类接收到任务后,首先调用`get_parser(file_type)`方法。 - - 该方法会根据文件类型(例如 `'pdf'`)在一个字典 `self.parsers` 中查找对应的具体解析器类(例如 `PDFParser`)。 - - 找到后,它会**实例化**这个`PDFParser`类,并将`ChunkingConfig`等所有配置信息传递给构造函数。 - -#### **第二步:核心解析与分块 (**`base_parser.py`**)** -它触及了整个流程的核心:**如何保证信息的上下文完整性和原始顺序**。 - -根据 `base_parser.py` 代码,**最终切分出的 Chunk 中的文本、表格和图像是按照它们在原始文档中的出现顺序来保存的**。 - -这个顺序得以保证,主要归功于 `BaseParser` 中几个设计精巧的方法相互协作。我们来详细追踪一下这个流程。 - -整个顺序的保证可以分为三个阶段: - -1. **阶段一:统一的文本流创建 (**`pdf_parser.py`**)**: - - 在 `parse_into_text` 方法中,您的代码会**逐页**处理PDF。 - - 在每一页内部,它会按照一定的逻辑(先提取非表格文本,再附加表格,最后附加图像占位符)将所有内容**拼接成一个长字符串** (`page_content_parts`)。 - - **关键点**: 虽然在这个阶段,文本、表格和图像占位符的拼接顺序可能不是100%精确到字符级别,但它保证了**同一页的内容会在一起**,并且大致遵循了从上到下的阅读顺序。 - - 最后,所有页面的内容被 `"\n\n--- Page Break ---\n\n"` 连接起来,形成一个**包含了所有信息(文本、Markdown表格、图像占位符)的、单一的、有序的文本流 (**`final_text`**)**。 -2. **阶段二:原子化与保护 (**`_split_into_units`**)**: - - 这个单一的 `final_text` 被传递给 `_split_into_units` 方法。 - - 这个方法是**保证结构完整性的关键**。它使用正则表达式,将**整个Markdown表格**和**整个Markdown图像占位符**识别为**不可分割的原子单元 (atomic units)**。 - - 它会将这些原子单元(表格、图片)和它们之间的普通文本块,按照它们在 `final_text` 中出现的**原始顺序**,切分成一个列表 (`units`)。 - - **结果**: 我们现在有了一个列表,例如 `['一些文本', '![...](...)', '另一些文本', '|...|...|\n|---|---|\n...', '更多文本']`。这个列表中的元素顺序**完全等同于它们在原始文档中的顺序**。 -3. **阶段三:顺序分块 (**`chunk_text`**)**: - - `chunk_text` 方法接收到这个**有序的 **`units`** 列表**。 - - 它的工作机制非常简单直接:它会**按顺序**遍历这个列表中的每一个单元(`unit`)。 - - 它将这些单元**依次添加**到一个临时的 `current_chunk` 列表中,直到这个块的长度接近 `chunk_size` 的上限。 - - 当一个块满了之后,它就被保存下来,然后开始一个新的块(可能会带有上一个块的重叠部分)。 - - **关键点**: 因为 `chunk_text` **严格按照 **`units`** 列表的顺序进行处理**,所以它永远不会打乱表格、文本和图像之间的相对顺序。一个在文档中先出现的表格,也必然会出现在一个序号更靠前的 Chunk 中。 -4. **阶段四:图像信息附加 (**`process_chunks_images`**)**: - - 在文本块被切分好之后,`process_chunks_images` 方法会被调用。 - - 它会处理**每一个**已经生成好的 Chunk。 - - 在每个 Chunk 内部,它会找到图像占位符,然后进行AI处理。 - - 最后,它会将处理好的图像信息(包含永久URL、OCR文本、图像描述等)附加到**该 Chunk 自己**的 `.images` 属性中。 - - **关键点**: 这个过程**不会改变 Chunk 的顺序或其 **`.content`** 的内容**。它只是为已经存在的、顺序正确的 Chunk 附加额外的信息。 - -#### **第三步:多模态处理(如果启用) (**`base_parser.py`**)** -如果 `enable_multimodal` 为 `True`,在文本分块完成后,会进入最复杂的多模态处理阶段。 - -1. **并发任务启动 (**`BaseParser.process_chunks_images`**)**: - - 该方法使用`asyncio`(Python的异步I/O框架)来**并发处理所有文本块中的图片**,以极大地提升效率。 - - 它为每个`Chunk`创建一个异步任务`process_chunk_images_async`。 -2. **处理单个块中的图片 (**`BaseParser.process_chunk_images_async`**)**: - - **提取图片引用**: 首先,使用正则表达式 `extract_images_from_chunk` 从当前块的文本中找到所有的图片引用(例如,`![alt text](image.png)`)。 - - **图片持久化**: 对于找到的每个图片,并发地调用 `download_and_upload_image`。这个函数负责: - * 从其原始位置(可能是PDF内部、本地路径或远程URL)获取图片数据。 - * 将图片**上传到配置好的对象存储(COS/MinIO)**。这一步至关重要,它将临时的、不稳定的图片引用转换成一个持久化、可通过URL公开访问的地址。 - * 返回持久化的URL和图片对象(PIL Image)。 - - **并发AI处理**: 将所有成功上传的图片收集起来,调用`process_multiple_images`。 - * 该方法内部使用`asyncio.Semaphore`来限制并发数量(例如最多同时处理5张图片),防止瞬间消耗过多内存或触发模型API的速率限制。 - * 对于每张图片,它会调用`process_image_async`。 -3. **处理单张图片 (**`BaseParser.process_image_async`**)**: - - **OCR**: 调用`perform_ocr`,它会使用一个OCR引擎(如`PaddleOCR`)来识别图片中的所有文字。 - - **图像描述 (Caption)**: 调用`get_image_caption`,它会将图片数据(转为Base64)发送给配置的视觉语言模型(VLM),生成对图片内容的自然语言描述。 - - 该方法返回 `(ocr_text, caption, 持久化URL)`。 -4. **结果聚合**: - - 所有图片处理完成后,包含持久化URL、OCR文本和图像描述的结构化信息,会被附加到对应`Chunk`对象的 `.images` 字段上。 - -#### **第四步:返回结果 (**`server.py`**)** -1. **数据转换 (**`server.py: _convert_chunk_to_proto`**)**: - - 当`parser.parse_file`执行完毕后,它返回一个包含所有处理过的`Chunk`对象的列表(`ParseResult`)。 - - `ReadFromFile`方法接收到这个结果,并调用`_convert_chunk_to_proto`,将Python的`Chunk`对象(包括其内部的图片信息)转换成gRPC定义的Protobuf消息格式。 -2. **响应返回**: - - 最后,gRPC服务器将这个包含所有分块和多模态信息的`ReadResponse`消息发送回给调用方——Go后端服务。 - -至此,Go后端就拿到了结构化、信息丰富的文档数据,可以进行下一步的向量化和索引存储了。 - - -## 部署 -支持Docker 镜像本地部署,并通过API端口提供接口服务 - -## 性能和监控 -Weknora包含丰富的监控和测试组件: - -+ 分布式跟踪:集成Jaeger用于跟踪请求在服务架构中的完整执行路。本质上,Jaeger是一种帮助用户“看见”请求在分布式系统中完整生命周期的技术。 -+ 健康监控:监控服务处在健康状态 -+ 可扩展性:通过容器化部署,可通过多个服务满足大规模并发请求 - -## QA -### 问题1: 在检索过程的执行了两次混合搜索的目的是什么?以及第一次和第二次搜索有什么不同? -这是一个非常好的观察。系统执行两次混合搜索是为了**最大化检索的准确性和召回率**,本质上是一种**查询扩展(Query Expansion)和多策略检索**的组合方法。 - -#### 目的 -通过两种不同形式的查询(原始改写句 vs. 分词后的关键词序列)去搜索,系统可以结合两种查询方式的优点: - -+ **语义检索的深度**: 使用完整的句子进行搜索,能更好地利用向量模型(如`bge-m3`)对句子整体含义的理解能力,找到语义上最接近的知识区块。 -+ **关键词检索的广度**: 使用分词后的关键词进行搜索,能确保即使知识区块的表述方式与原问题不同,但只要包含了核心关键词,就有机会被命中。这对于传统的关键词匹配算法(如BM25)尤其有效。 - -简单来说,就是**用两种不同的“问法”去问同一个问题**,然后将两边的结果汇总起来,确保最相关的知识不会被遗漏。 - -#### 两次搜索的不同点 -它们最核心的不同在于**输入的查询文本(Query Text)**: - -1. **第一次混合搜索** - - **输入**: 使用的是经过`rewrite_query`事件后生成的、**语法完整的自然语言问句**。 - - **日志证据**: - -```plain -INFO [2025-08-29 09:46:36.896] [request_id=Lkq0OGLYu2fV] knowledgebase.go:266[HybridSearch] | Hybrid search parameters, knowledge base ID: kb-00000001, query text: 需要改写的用户问题是:“入住的房型是什么”。根据提供的信息,入住人Liwx选择的房型是双床房。因此,改写后的完整问题为: “Liwx本次入住的房型是什么” -``` - -2. **第二次混合搜索** - - **输入**: 使用的是经过`preprocess_query`事件处理后生成的、**由空格隔开的关键词序列**。 - - **日志证据**: - -```plain -INFO [2025-08-29 09:46:37.257] [request_id=Lkq0OGLYu2fV] knowledgebase.go:266[HybridSearch] | Hybrid search parameters, knowledge base ID: kb-00000001, query text: 需要 改写 用户 问题 入住 房型 根据 提供 信息 入住 人 Liwx 选择 房型 双床 房 因此 改写 后 完整 问题 为 Liwx 本次 入住 房型 -``` - -最终,系统将这两次搜索的结果进行去重和合并(日志中显示每次都找到2个结果,去重后总共还是2个),从而得到一个更可靠的知识集合,用于后续的答案生成。 - - - -### 问题2:重排序模型分析 -Reranker(重排器)是目前RAG领域中非常先进的技术,它们在工作原理和适用场景上有着显著的区别。 - -简单来说,它们代表了从“**专门的判别模型**”到“**利用大语言模型(LLM)进行判别**”再到“**深度挖掘LLM内部信息进行判别**”的演进。 - -以下是它们的详细区别: - - - -#### 1. Normal Reranker (常规重排器 / 交叉编码器) -这是最经典也是最主流的重排方法。 - -+ **模型类型**: **序列分类模型 (Sequence Classification Model)**。本质上是一个**交叉编码器 (Cross-Encoder)**,通常基于BERT、RoBERTa等双向编码器架构。`BAAI/bge-reranker-base/large/v2-m3` 都属于这一类。 -+ **工作原理**: - 1. 它将**查询(Query)**和**待排序的文档(Passage)**拼接成一个单一的输入序列,例如:`[CLS] what is panda? [SEP] The giant panda is a bear species endemic to China. [SEP]`。 - 2. 这个拼接后的序列被完整地送入模型中。模型内部的自注意力机制(Self-Attention)可以同时分析查询和文档中的每一个词,并计算它们之间**细粒度的交互关系**。 - 3. 模型最终输出一个**单一的分数(Logit)**,这个分数直接代表了查询和文档的相关性。分数越高,相关性越强。 -+ **关键特性**: - - **优点**: 由于查询和文档在模型内部进行了充分的、深度的交互,其**准确度通常非常高**,是衡量Reranker性能的黄金标准。 - - **缺点**: **速度较慢**。因为它必须为**每一个“查询-文档”对**都独立执行一次完整的、代价高昂的计算。如果初步检索返回了100个文档,它就需要运行100次。 - - - -#### 2. LLM-based Reranker (基于LLM的重排器) -这种方法创造性地利用了通用大语言模型(LLM)的能力来进行重排。 - -+ **模型类型**: **因果语言模型 (Causal Language Model)**,即我们常说的GPT、Llama、Gemma这类用于生成文本的LLM。`BAAI/bge-reranker-v2-gemma` 就是一个典型的例子。 -+ **工作原理**: - 1. 它**不是直接输出一个分数**,而是将重排任务**转化为一个问答或文本生成任务**。 - 2. 它通过一个精心设计的**提示(Prompt)**来组织输入,例如:`"Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'. A: {query} B: {passage}"`。 - 3. 它将这个完整的Prompt喂给LLM,然后**观察LLM在最后生成“Yes”这个词的概率**。 - 4. 这个**生成“Yes”的概率(或其Logit值)就被当作是相关性分数**。如果模型非常确信答案是“Yes”,说明它认为文档B包含了查询A的答案,即相关性高。 -+ **关键特性**: - - **优点**: 能够利用LLM强大的**语义理解、推理和世界知识**,对于需要深度理解和推理才能判断相关性的复杂查询,效果可能更好。 - - **缺点**: 计算开销可能非常大(取决于LLM的大小),并且性能**高度依赖于Prompt的设计**。 - - - -#### 3. LLM-based Layerwise Reranker (基于LLM分层信息的重排器) -这是第二种方法的“威力加强版”,是一种更前沿、更复杂的探究性技术。 - -+ **模型类型**: 同样是**因果语言模型 (Causal Language Model)**,例如`BAAI/bge-reranker-v2-minicpm-layerwise`。 -+ **工作原理**: - 1. 输入部分与第二种方法完全相同,也是使用“Yes/No”的Prompt。 - 2. 核心区别在于**分数的提取方式**。它不再仅仅依赖LLM**最后一层**的输出(即最终的预测结果)。 - 3. 它认为LLM在逐层处理信息的过程中,不同深度的网络层(Layer)可能捕获了不同层次的语义相关性信息。因此,它会从**模型的多个中间层**提取出关于“Yes”这个词的预测Logit。 - 4. 代码中的 `cutoff_layers=[28]` 参数就是告诉模型:“请把第28层的输出给我”。最终,你会得到一个或多个来自不同网络层的分数,这些分数可以被平均或以其他方式组合,形成一个更鲁棒的最终相关性判断。 -+ **关键特性**: - - **优点**: 理论上可以获得**更丰富、更全面的相关性信号**,可能达到比只看最后一层更高的精度,是目前探索性能极限的一种方法。 - - **缺点**: **复杂度最高**,需要对模型进行特定的修改才能提取中间层信息(代码中的`trust_remote_code=True`就是一个信号),计算开销也很大。 - -#### 总结对比 -| 特性 | 1. Normal Reranker (常规) | 2. LLM-based Reranker (基于LLM) | 3. LLM-based Layerwise Reranker (基于LLM分层) | -| :--- | :--- | :--- | :--- | -| **底层模型** | 交叉编码器 (如BERT) | 因果语言模型 (如Gemma) | 因果语言模型 (如MiniCPM) | -| **工作原理** | 计算Query和Passage的深度交互,直接输出相关分 | 将排序任务转为"Yes/No"预测,用"Yes"的概率作为分数 | 与2类似,但从LLM的多个中间层提取"Yes"的概率 | -| **输出** | 单一的相关性分数 | 单一的相关性分数(来自最后一层) | 多个相关性分数(来自不同层) | -| **优点** | **速度与精度的最佳平衡点**,成熟稳定 | 利用LLM的推理能力,处理复杂问题 | 理论上精度最高,信号更丰富 | -| **缺点** | 相比向量检索慢 | 计算开销大,依赖Prompt设计 | **复杂度最高**,计算开销最大 | -| **推荐场景** | **大多数生产环境的首选**,效果好,易于部署 | 对答案质量有极致要求,且计算资源充足的场景 | 学术研究或追求SOTA(State-of-the-art)性能的场景 | - - -#### 使用建议 -1. **开始阶段**: 强烈建议您**从 **`Normal Reranker`** 开始**,例如 `BAAI/bge-reranker-v2-m3`。它是目前综合表现最好的模型之一,能显著提升您的RAG系统性能,并且相对容易集成和部署。 -2. **进阶探索**: 如果您发现常规Reranker在处理某些非常微妙或需要复杂推理的查询时表现不佳,并且您拥有充足的GPU资源,可以尝试 `LLM-based Reranker`。 -3. **前沿研究**: `Layerwise Reranker` 更适合研究人员或希望在特定任务上压榨出最后一点性能的专家。 - - -### 问题3:粗过滤或细过滤后的知识(带重排)如何组装发送给大模型的? -这一块主要是设计提示词,典型的指令细节,其核心任务是根据上下文回答用户问题。组装上下文时需要指定 -关键约束:必须严格按照所提供文档回答,禁止使用你自己的知识回答 -未知情况处理: 如果文档中没有足够的信息来回答问题,请告知“根据所掌握的资料,无法回答这个问题” -引用要求:在回答时,如果引用了某个文档内容,请在句子末尾加上文档编号 - ---- - -## 手工知识在线编辑 - -平台的知识库页面新增“上传文档 / 在线编辑”双入口,支持直接在浏览器中撰写并维护 Markdown 知识: - -- 草稿模式用于暂存内容,草稿不会参与检索。 -- 发布操作会自动触发向量化与索引构建。 -- 已发布的 Markdown 知识可再次打开编辑并重新发布。 -- 在对话页面的助手回答末尾提供“添加到知识库”工具,可一键带入当前问答到编辑器中确认后保存。 - - - - - - - - - - - - - diff --git a/docs/WeKnora.pdf b/docs/WeKnora.pdf deleted file mode 100644 index f98a9f2d..00000000 Binary files a/docs/WeKnora.pdf and /dev/null differ diff --git a/docs/api/agent.md b/docs/api/agent.md index 02640f58..95151eef 100644 --- a/docs/api/agent.md +++ b/docs/api/agent.md @@ -458,7 +458,7 @@ curl --location 'http://localhost:8080/api/v1/agents/placeholders' \ |------|------|--------|------| | `image_upload_enabled` | bool | false | 是否允许上传图片 | | `vlm_model_id` | string | - | 图片分析所用的 VLM 模型 ID | -| `image_storage_provider` | string | - | 图片存储提供者:`local`/`minio`/`cos`/`tos`,为空使用全局默认 | +| `image_storage_provider` | string | - | 图片存储提供者:`local`/`minio`/`cos`/`tos`/`oss`,为空使用全局默认 | ### FAQ 策略设置 diff --git a/docs/api/system.md b/docs/api/system.md index eeab0cc0..dc1b1cdd 100644 --- a/docs/api/system.md +++ b/docs/api/system.md @@ -155,6 +155,11 @@ curl --location 'http://localhost:8080/api/v1/system/storage-engine-status' \ "name": "s3", "available": false, "description": "AWS S3 对象存储" + }, + { + "name": "oss", + "available": false, + "description": "阿里云 OSS 对象存储" } ], "minio_env_available": true diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d3fbc78c..ae6ca15c 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -429,7 +429,7 @@ definitions: type: integer image_storage_provider: description: |- - Storage provider for image uploads: "local", "minio", "cos", "tos" + Storage provider for image uploads: "local", "minio", "cos", "tos", "s3", "oss" Empty means use the global/tenant default provider. type: string image_upload_enabled: @@ -2067,12 +2067,14 @@ definitions: cos: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.COSEngineConfig' default_provider: - description: '"local", "minio", "cos", "tos", "s3"' + description: '"local", "minio", "cos", "tos", "s3", "oss"' type: string local: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.LocalEngineConfig' minio: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.MinIOEngineConfig' + oss: + $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.OSSEngineConfig' s3: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.S3EngineConfig' tos: @@ -2081,7 +2083,7 @@ definitions: github_com_Tencent_WeKnora_internal_types.StorageProviderConfig: properties: provider: - description: '"local", "minio", "cos", "tos"' + description: '"local", "minio", "cos", "tos", "s3", "oss"' type: string type: object github_com_Tencent_WeKnora_internal_types.SubmitJoinRequestRequest: @@ -2169,6 +2171,27 @@ definitions: secret_key: type: string type: object + github_com_Tencent_WeKnora_internal_types.OSSEngineConfig: + properties: + access_key: + type: string + bucket_name: + type: string + endpoint: + type: string + path_prefix: + type: string + region: + type: string + secret_key: + type: string + temp_bucket_name: + type: string + temp_region: + type: string + use_temp_bucket: + type: boolean + type: object github_com_Tencent_WeKnora_internal_types.Tenant: properties: agent_config: @@ -2671,8 +2694,10 @@ definitions: minio: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.MinIOEngineConfig' provider: - description: '"minio", "cos", "tos", or "s3"' + description: '"minio", "cos", "tos", "s3", or "oss"' type: string + oss: + $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.OSSEngineConfig' s3: $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.S3EngineConfig' tos: @@ -2696,7 +2721,7 @@ definitions: description: short description for UI type: string name: - description: '"local", "minio", "cos", "tos"' + description: '"local", "minio", "cos", "tos", "s3", "oss"' type: string type: object internal_handler.TextRelationExtractionRequest: diff --git a/frontend/index.html b/frontend/index.html index f5538ceb..f09ede50 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -15,8 +15,58 @@ + +
+ diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 7b782e78..8b6278bf 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -1,12 +1,12 @@ { "name": "knowledage-base", - "version": "0.3.4", + "version": "0.3.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "knowledage-base", - "version": "0.3.4", + "version": "0.3.6", "dependencies": { "@microsoft/fetch-event-source": "^2.0.1", "@types/dompurify": "^3.0.5", @@ -463,42 +463,40 @@ "license": "MIT" }, "node_modules/@chevrotain/cst-dts-gen": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/@chevrotain/cst-dts-gen/-/cst-dts-gen-11.1.2.tgz", - "integrity": "sha512-XTsjvDVB5nDZBQB8o0o/0ozNelQtn2KrUVteIHSlPd2VAV2utEb6JzyCJaJ8tGxACR4RiBNWy5uYUHX2eji88Q==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@chevrotain/cst-dts-gen/-/cst-dts-gen-12.0.0.tgz", + "integrity": "sha512-fSL4KXjTl7cDgf0B5Rip9Q05BOrYvkJV/RrBTE/bKDN096E4hN/ySpcBK5B24T76dlQ2i32Zc3PAE27jFnFrKg==", "license": "Apache-2.0", "dependencies": { - "@chevrotain/gast": "11.1.2", - "@chevrotain/types": "11.1.2", - "lodash-es": "4.17.23" + "@chevrotain/gast": "12.0.0", + "@chevrotain/types": "12.0.0" } }, "node_modules/@chevrotain/gast": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/@chevrotain/gast/-/gast-11.1.2.tgz", - "integrity": "sha512-Z9zfXR5jNZb1Hlsd/p+4XWeUFugrHirq36bKzPWDSIacV+GPSVXdk+ahVWZTwjhNwofAWg/sZg58fyucKSQx5g==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@chevrotain/gast/-/gast-12.0.0.tgz", + "integrity": "sha512-1ne/m3XsIT8aEdrvT33so0GUC+wkctpUPK6zU9IlOyJLUbR0rg4G7ZiApiJbggpgPir9ERy3FRjT6T7lpgetnQ==", "license": "Apache-2.0", "dependencies": { - "@chevrotain/types": "11.1.2", - "lodash-es": "4.17.23" + "@chevrotain/types": "12.0.0" } }, "node_modules/@chevrotain/regexp-to-ast": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/@chevrotain/regexp-to-ast/-/regexp-to-ast-11.1.2.tgz", - "integrity": "sha512-nMU3Uj8naWer7xpZTYJdxbAs6RIv/dxYzkYU8GSwgUtcAAlzjcPfX1w+RKRcYG8POlzMeayOQ/znfwxEGo5ulw==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@chevrotain/regexp-to-ast/-/regexp-to-ast-12.0.0.tgz", + "integrity": "sha512-p+EW9MaJwgaHguhoqwOtx/FwuGr+DnNn857sXWOi/mClXIkPGl3rn7hGNWvo31HA3vyeQxjqe+H36yZJwYU8cA==", "license": "Apache-2.0" }, "node_modules/@chevrotain/types": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/@chevrotain/types/-/types-11.1.2.tgz", - "integrity": "sha512-U+HFai5+zmJCkK86QsaJtoITlboZHBqrVketcO2ROv865xfCMSFpELQoz1GkX5GzME8pTa+3kbKrZHQtI0gdbw==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@chevrotain/types/-/types-12.0.0.tgz", + "integrity": "sha512-S+04vjFQKeuYw0/eW3U52LkAHQsB1ASxsPGsLPUyQgrZ2iNNibQrsidruDzjEX2JYfespXMG0eZmXlhA6z7nWA==", "license": "Apache-2.0" }, "node_modules/@chevrotain/utils": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/@chevrotain/utils/-/utils-11.1.2.tgz", - "integrity": "sha512-4mudFAQ6H+MqBTfqLmU7G1ZwRzCLfJEooL/fsF6rCX5eePMbGhoy5n4g+G4vlh2muDcsCTJtL+uKbOzWxs5LHA==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@chevrotain/utils/-/utils-12.0.0.tgz", + "integrity": "sha512-lB59uJoaGIfOOL9knQqQRfhl9g7x8/wqFkp13zTdkRu1huG9kg6IJs1O8hqj9rs6h7orGxHJUKb+mX3rPbWGhA==", "license": "Apache-2.0" }, "node_modules/@esbuild/aix-ppc64": { @@ -2391,13 +2389,14 @@ "license": "MIT" }, "node_modules/axios": { - "version": "1.13.5", - "resolved": "https://mirrors.tencent.com/npm/axios/-/axios-1.13.5.tgz", - "integrity": "sha512-cz4ur7Vb0xS4/KUN0tPWe44eqxrIu31me+fbang3ijiNscE129POzipJJA6zniq2C/Z6sJCjMimjS8Lc/GAs8Q==", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.15.0.tgz", + "integrity": "sha512-wWyJDlAatxk30ZJer+GeCWS209sA42X+N5jU2jy6oHTp7ufw8uzUTVFBX9+wTfAlhiJXGS0Bq7X6efruWjuK9Q==", + "license": "MIT", "dependencies": { "follow-redirects": "^1.15.11", "form-data": "^4.0.5", - "proxy-from-env": "^1.1.0" + "proxy-from-env": "^2.1.0" } }, "node_modules/baseline-browser-mapping": { @@ -2492,28 +2491,31 @@ ] }, "node_modules/chevrotain": { - "version": "11.1.2", - "resolved": "https://mirrors.tencent.com/npm/chevrotain/-/chevrotain-11.1.2.tgz", - "integrity": "sha512-opLQzEVriiH1uUQ4Kctsd49bRoFDXGGSC4GUqj7pGyxM3RehRhvTlZJc1FL/Flew2p5uwxa1tUDWKzI4wNM8pg==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/chevrotain/-/chevrotain-12.0.0.tgz", + "integrity": "sha512-csJvb+6kEiQaqo1woTdSAuOWdN0WTLIydkKrBnS+V5gZz0oqBrp4kQ35519QgK6TpBThiG3V1vNSHlIkv4AglQ==", + "license": "Apache-2.0", "dependencies": { - "@chevrotain/cst-dts-gen": "11.1.2", - "@chevrotain/gast": "11.1.2", - "@chevrotain/regexp-to-ast": "11.1.2", - "@chevrotain/types": "11.1.2", - "@chevrotain/utils": "11.1.2", - "lodash-es": "4.17.23" + "@chevrotain/cst-dts-gen": "12.0.0", + "@chevrotain/gast": "12.0.0", + "@chevrotain/regexp-to-ast": "12.0.0", + "@chevrotain/types": "12.0.0", + "@chevrotain/utils": "12.0.0" + }, + "engines": { + "node": ">=22.0.0" } }, "node_modules/chevrotain-allstar": { - "version": "0.3.1", - "resolved": "https://mirrors.tencent.com/npm/chevrotain-allstar/-/chevrotain-allstar-0.3.1.tgz", - "integrity": "sha512-b7g+y9A0v4mxCW1qUhf3BSVPg+/NvGErk/dOkrDaHA0nQIQGAtrOjlX//9OQtRlSCy+x9rfB5N8yC71lH1nvMw==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/chevrotain-allstar/-/chevrotain-allstar-0.4.1.tgz", + "integrity": "sha512-PvVJm3oGqrveUVW2Vt/eZGeiAIsJszYweUcYwcskg9e+IubNYKKD+rHHem7A6XVO22eDAL+inxNIGAzZ/VIWlA==", "license": "MIT", "dependencies": { "lodash-es": "^4.17.21" }, "peerDependencies": { - "chevrotain": "^11.0.0" + "chevrotain": "^12.0.0" } }, "node_modules/chrome-trace-event": { @@ -3802,13 +3804,14 @@ "integrity": "sha512-Ls993zuzfayK269Svk9hzpeGUKob/sIgZzyHYdjQoAdQetRKpOLj+k/QQQ/6Qi0Yz65mlROrfd+Ev+1+7dz9Kw==" }, "node_modules/langium": { - "version": "4.2.1", - "resolved": "https://mirrors.tencent.com/npm/langium/-/langium-4.2.1.tgz", - "integrity": "sha512-zu9QWmjpzJcomzdJQAHgDVhLGq5bLosVak1KVa40NzQHXfqr4eAHupvnPOVXEoLkg6Ocefvf/93d//SB7du4YQ==", + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/langium/-/langium-4.2.2.tgz", + "integrity": "sha512-JUshTRAfHI4/MF9dH2WupvjSXyn8JBuUEWazB8ZVJUtXutT0doDlAv1XKbZ1Pb5sMexa8FF4CFBc0iiul7gbUQ==", "license": "MIT", "dependencies": { - "chevrotain": "~11.1.1", - "chevrotain-allstar": "~0.3.1", + "@chevrotain/regexp-to-ast": "~12.0.0", + "chevrotain": "~12.0.0", + "chevrotain-allstar": "~0.4.1", "vscode-languageserver": "~9.0.1", "vscode-languageserver-textdocument": "~1.0.11", "vscode-uri": "~3.1.0" @@ -3899,9 +3902,10 @@ } }, "node_modules/lodash-es": { - "version": "4.17.23", - "resolved": "https://mirrors.tencent.com/npm/lodash-es/-/lodash-es-4.17.23.tgz", - "integrity": "sha512-kVI48u3PZr38HdYz98UmfPnXl2DXrpdctLrFLCd3kOx1xUkOmpFPx7gCWWM5MPkL/fD8zb+Ph0QzjGFs4+hHWg==" + "version": "4.18.1", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.18.1.tgz", + "integrity": "sha512-J8xewKD/Gk22OZbhpOVSwcs60zhd95ESDwezOFuA3/099925PdHJ7OFHNTGtajL3AlZkykD32HykiMo+BIBI8A==", + "license": "MIT" }, "node_modules/lru-cache": { "version": "5.1.1", @@ -4367,9 +4371,13 @@ "license": "MIT" }, "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://mirrors.tencent.com/npm/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-2.1.0.tgz", + "integrity": "sha512-cJ+oHTW1VAEa8cJslgmUZrc+sjRKgAKl3Zyse6+PV38hZe/V6Z14TbCuXcan9F9ghlz4QrFr2c92TNF82UkYHA==", + "license": "MIT", + "engines": { + "node": ">=10" + } }, "node_modules/prr": { "version": "1.0.1", @@ -4538,14 +4546,6 @@ "semver": "bin/semver.js" } }, - "node_modules/serialize-javascript": { - "version": "7.0.3", - "resolved": "https://mirrors.tencent.com/npm/serialize-javascript/-/serialize-javascript-7.0.3.tgz", - "integrity": "sha512-h+cZ/XXarqDgCjo+YSyQU/ulDEESGGf8AMK9pPNmhNSl/FzPl6L8pMp1leca5z6NuG6tvV/auC8/43tmovowww==", - "engines": { - "node": ">=20.0.0" - } - }, "node_modules/setimmediate": { "version": "1.0.5", "resolved": "https://mirrors.tencent.com/npm/setimmediate/-/setimmediate-1.0.5.tgz", @@ -4789,14 +4789,14 @@ } }, "node_modules/terser-webpack-plugin": { - "version": "5.3.16", - "resolved": "https://mirrors.tencent.com/npm/terser-webpack-plugin/-/terser-webpack-plugin-5.3.16.tgz", - "integrity": "sha512-h9oBFCWrq78NyWWVcSwZarJkZ01c2AyGrzs1crmHZO3QUg9D61Wu4NPjBy69n7JqylFF5y+CsUZYmYEIZ3mR+Q==", + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.4.0.tgz", + "integrity": "sha512-Bn5vxm48flOIfkdl5CaD2+1CiUVbonWQ3KQPyP7/EuIl9Gbzq/gQFOzaMFUEgVjB1396tcK0SG8XcNJ/2kDH8g==", + "license": "MIT", "dependencies": { "@jridgewell/trace-mapping": "^0.3.25", "jest-worker": "^27.4.5", "schema-utils": "^4.3.0", - "serialize-javascript": "^6.0.2", "terser": "^5.31.1" }, "engines": { @@ -4952,13 +4952,13 @@ } }, "node_modules/vite": { - "version": "7.2.2", - "resolved": "https://mirrors.tencent.com/npm/vite/-/vite-7.2.2.tgz", - "integrity": "sha512-BxAKBWmIbrDgrokdGZH1IgkIk/5mMHDreLDmCJ0qpyJaAteP8NvMhkwr/ZCQNqNH97bw/dANTE9PDzqwJghfMQ==", + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/vite/-/vite-7.3.2.tgz", + "integrity": "sha512-Bby3NOsna2jsjfLVOHKes8sGwgl4TT0E6vvpYgnAYDIF/tie7MRaFthmKuHx1NSXjiTueXH3do80FMQgvEktRg==", "dev": true, "license": "MIT", "dependencies": { - "esbuild": "^0.25.0", + "esbuild": "^0.27.0", "fdir": "^6.5.0", "picomatch": "^4.0.3", "postcss": "^8.5.6", diff --git a/frontend/package.json.md5 b/frontend/package.json.md5 new file mode 100755 index 00000000..e994af2d --- /dev/null +++ b/frontend/package.json.md5 @@ -0,0 +1 @@ +a8c281c741c2499a02564295ccfb3f91 \ No newline at end of file diff --git a/frontend/src/App.vue b/frontend/src/App.vue index d04429fa..60cf56c2 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,10 +1,11 @@ diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts index f168a619..38269d9d 100644 --- a/frontend/src/api/agent/index.ts +++ b/frontend/src/api/agent/index.ts @@ -49,6 +49,7 @@ export interface CustomAgentConfig { // ===== 网络搜索设置 ===== web_search_enabled?: boolean; + web_search_provider_id?: string; web_search_max_results?: number; // ===== 多轮对话设置 ===== @@ -184,10 +185,10 @@ export interface IMChannel { id: string; tenant_id?: number; agent_id: string; - platform: 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost'; + platform: 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost' | 'wechat'; name: string; enabled: boolean; - mode: 'webhook' | 'websocket'; + mode: 'webhook' | 'websocket' | 'longpoll'; output_mode: 'stream' | 'full'; session_mode?: 'user' | 'thread'; knowledge_base_id?: string; @@ -238,3 +239,27 @@ export function getSuggestedQuestions( const qs = query.toString(); return get<{ data: { questions: SuggestedQuestion[] } }>(`/api/v1/agents/${agentId}/suggested-questions${qs ? '?' + qs : ''}`); } +// ===== WeChat QR Code Login ===== + +export interface WeChatQRCodeResult { + qrcode_url: string; + qrcode: string; +} + +export interface WeChatQRCodeStatus { + status: 'wait' | 'scaned' | 'confirmed' | 'expired'; + credentials?: { + bot_token: string; + ilink_bot_id: string; + ilink_user_id: string; + }; + baseurl?: string; +} + +export function getWeChatQRCode() { + return post<{ data: WeChatQRCodeResult }>('/api/v1/wechat/qrcode'); +} + +export function pollWeChatQRCodeStatus(qrcode: string) { + return post<{ data: WeChatQRCodeStatus }>('/api/v1/wechat/qrcode/status', { qrcode }); +} diff --git a/frontend/src/api/auth/index.ts b/frontend/src/api/auth/index.ts index 50003371..17437900 100644 --- a/frontend/src/api/auth/index.ts +++ b/frontend/src/api/auth/index.ts @@ -190,6 +190,21 @@ export async function register(data: RegisterRequest): Promise } } +/** + * Lite 版自动初始化(创建默认用户/租户 + 签发令牌) + */ +export async function autoSetup(): Promise { + try { + const response = await post('/api/v1/auth/auto-setup', {}) + return response as unknown as LoginResponse + } catch (error: any) { + return { + success: false, + message: error.message || 'Auto-setup unavailable' + } + } +} + /** * 获取当前用户信息 */ diff --git a/frontend/src/api/chat/streame.ts b/frontend/src/api/chat/streame.ts index 442b51ec..0bd47b29 100644 --- a/frontend/src/api/chat/streame.ts +++ b/frontend/src/api/chat/streame.ts @@ -1,7 +1,8 @@ -import { fetchEventSource } from '@microsoft/fetch-event-source' -import { ref, type Ref, onUnmounted, nextTick } from 'vue' +import { fetchEventSource } from '@microsoft/fetch-event-source'; +import { ref, onUnmounted } from 'vue'; import { generateRandomString } from '@/utils/index'; import i18n from '@/i18n'; +import { getApiBaseUrl } from '@/utils/api-base'; @@ -37,7 +38,7 @@ export function useStream() { isLoading.value = true; // 获取API配置 - const apiUrl = import.meta.env.VITE_IS_DOCKER ? "" : "http://localhost:8080"; + const apiUrl = getApiBaseUrl(); // 获取JWT Token const token = localStorage.getItem('weknora_token'); diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts index 56303d17..e079816b 100644 --- a/frontend/src/api/initialization/index.ts +++ b/frontend/src/api/initialization/index.ts @@ -255,6 +255,7 @@ export function checkRemoteModel(modelConfig: { modelName: string; baseUrl: string; apiKey?: string; + provider?: string; }): Promise<{ available: boolean; message?: string; diff --git a/frontend/src/api/model/index.ts b/frontend/src/api/model/index.ts index d886a344..84313346 100644 --- a/frontend/src/api/model/index.ts +++ b/frontend/src/api/model/index.ts @@ -126,3 +126,45 @@ export function deleteModel(id: string): Promise { }); } +export interface InitializeWeKnoraCloudRequest { + app_id: string + app_secret: string +} + +export interface InitializeWeKnoraCloudResult { + models: Array<{ + name: string + type: string + action: 'created' | 'updated' + }> +} + +export function initializeWeKnoraCloud(data: InitializeWeKnoraCloudRequest): Promise { + return post('/api/v1/models/weknoracloud/initialize', data) as Promise +} + +export interface WeKnoraCloudStatusResult { + has_models: boolean + needs_reinit: boolean + reason?: string +} + +export function getWeKnoraCloudStatus(): Promise { + return new Promise((resolve, reject) => { + get('/api/v1/models/weknoracloud/status') + .then((response: any) => { + // status 接口直接返回对象,不包在 success/data 中 + if (response && typeof response.has_models === 'boolean') { + resolve(response) + } else if (response?.success && response?.data) { + resolve(response.data) + } else { + resolve({ has_models: false, needs_reinit: false }) + } + }) + .catch(() => { + resolve({ has_models: false, needs_reinit: false }) + }) + }) +} + diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 688e1323..35ad2855 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -183,7 +183,7 @@ export function reconnectDocReader(addr: string): Promise
- + {{ $t('agentEditor.im.wecom') }} {{ $t('agentEditor.im.feishu') }} {{ $t('agentEditor.im.slack') }} {{ $t('agentEditor.im.telegram') }} {{ $t('agentEditor.im.dingtalk') }} {{ $t('agentEditor.im.mattermost') }} + {{ $t('agentEditor.im.wechat') }}
@@ -105,8 +106,8 @@ - -
+ +
WebSocket @@ -116,8 +117,8 @@

{{ $t('agentEditor.im.modeHint') }}

- -
+ +
{{ $t('agentEditor.im.outputStream') }} @@ -173,6 +174,11 @@
+
+ + +

{{ $t('agentEditor.im.wecomWSEndpointHint') }}

+
@@ -339,16 +350,62 @@

{{ $t('agentEditor.im.mattermostPostToMainHint') }}

+ +
\ No newline at end of file + +// --- WeChat QR code binding --- +.wechat-bound-status { + display: flex; + align-items: center; + gap: 8px; + padding: 12px 16px; + background: rgba(7, 193, 96, 0.06); + border: 1px solid rgba(7, 193, 96, 0.2); + border-radius: 8px; + font-size: 14px; + color: var(--td-text-color-primary); + + .bound-icon { + font-size: 18px; + color: #07c160; + } +} + +.wechat-qr-section { + display: flex; + flex-direction: column; + align-items: center; + gap: 12px; +} + +.wechat-bind-action { + padding: 24px 0; +} + +.wechat-qr-display { + display: flex; + flex-direction: column; + align-items: center; + gap: 12px; + padding: 16px 0; +} + +.qr-container { + position: relative; + width: 200px; + height: 200px; + border: 1px solid var(--td-component-stroke); + border-radius: 8px; + overflow: hidden; + // QR code images are always black-on-white; force white background + // so the code remains scannable in dark mode. + background: #fff; + + .qr-image { + width: 100%; + height: 100%; + object-fit: contain; + } +} + +.qr-expired-overlay { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 8px; + background: rgba(0, 0, 0, 0.6); + color: #fff; + cursor: pointer; + font-size: 12px; + + .refresh-icon { + font-size: 24px; + } +} + +.qr-hint { + font-size: 13px; + color: var(--td-text-color-secondary); + text-align: center; +} + diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 5ebc8c22..04ee3b5b 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -15,7 +15,7 @@ import AgentSelector from './AgentSelector.vue'; import { getCaretCoordinates } from '@/utils/caret'; import { listModels, type ModelConfig } from '@/api/model'; import { listAgents, type CustomAgent, BUILTIN_QUICK_ANSWER_ID, BUILTIN_SMART_REASONING_ID } from '@/api/agent'; -import { getTenantWebSearchConfig } from '@/api/web-search'; +import { listWebSearchProviders, type WebSearchProviderEntity } from '@/api/web-search-provider'; import { getConversationConfig, updateConversationConfig, type ConversationConfig } from '@/api/system'; import { useI18n } from 'vue-i18n'; import AttachmentUpload, { type AttachmentFile } from './AttachmentUpload.vue'; @@ -197,6 +197,11 @@ const agentWebSearchEnabled = computed(() => { return currentAgentConfig.value?.web_search_enabled ?? true; }); +const agentWebSearchProviderId = computed(() => { + if (!hasAgentConfig.value) return ''; + return currentAgentConfig.value?.web_search_provider_id || ''; +}); + // 网络搜索是否被智能体禁用(只读状态)- 只有明确设置为 false 时才禁用 const isWebSearchDisabledByAgent = computed(() => { return hasAgentConfig.value && agentWebSearchEnabled.value === false; @@ -287,7 +292,6 @@ const isAgentEnabled = computed(() => settingsStore.isAgentEnabled); const isWebSearchEnabled = computed(() => settingsStore.isWebSearchEnabled); const selectedKbIds = computed(() => settingsStore.settings.selectedKnowledgeBases || []); const selectedFileIds = computed(() => settingsStore.settings.selectedFiles || []); -const isWebSearchConfigured = ref(false); // 获取已选择的知识库信息 const knowledgeBases = ref>([]); @@ -526,19 +530,29 @@ watch(selectedFileIds, () => { loadFiles(); }, { immediate: true }); +const webSearchProviders = ref([]); + +const isWebSearchConfigured = computed(() => { + const agentProviderId = agentWebSearchProviderId.value; + if (agentProviderId) { + return webSearchProviders.value.some(p => p.id === agentProviderId); + } + + return webSearchProviders.value.some(p => p.is_default); +}); + const loadWebSearchConfig = async () => { try { - const response: any = await getTenantWebSearchConfig(); - const config = response?.data; - const configured = !!(config && config.provider); - isWebSearchConfigured.value = configured; + const response = await listWebSearchProviders(); + const providers = (response as any)?.data; + webSearchProviders.value = Array.isArray(providers) ? providers : []; - if (!configured && settingsStore.isWebSearchEnabled) { + if (!isWebSearchConfigured.value && settingsStore.isWebSearchEnabled) { settingsStore.toggleWebSearch(false); } } catch (error) { console.error('Failed to load web search config:', error); - isWebSearchConfigured.value = false; + webSearchProviders.value = []; if (settingsStore.isWebSearchEnabled) { settingsStore.toggleWebSearch(false); } diff --git a/frontend/src/components/ModelEditorDialog.vue b/frontend/src/components/ModelEditorDialog.vue index d9a041ec..269aadef 100644 --- a/frontend/src/components/ModelEditorDialog.vue +++ b/frontend/src/components/ModelEditorDialog.vue @@ -317,6 +317,18 @@ const fallbackProviderOptions = computed(() => [ description: t('model.editor.providers.openai.description'), modelTypes: ['chat', 'embedding', 'vllm', 'asr'] }, + { + value: 'azure_openai', + label: t('model.editor.providers.azure_openai.label'), + defaultUrls: { + chat: 'https://{resource}.openai.azure.com', + embedding: 'https://{resource}.openai.azure.com', + vllm: 'https://{resource}.openai.azure.com', + asr: 'https://{resource}.openai.azure.com' + }, + description: t('model.editor.providers.azure_openai.description'), + modelTypes: ['chat', 'embedding', 'vllm', 'asr'] + }, { value: 'aliyun', label: t('model.editor.providers.aliyun.label'), @@ -810,7 +822,8 @@ const checkRemoteAPI = async () => { result = await checkRemoteModel({ modelName: formData.value.modelName, baseUrl: formData.value.baseUrl, - apiKey: formData.value.apiKey || '' + apiKey: formData.value.apiKey || '', + provider: formData.value.provider }) break @@ -846,7 +859,8 @@ const checkRemoteAPI = async () => { result = await checkRemoteModel({ modelName: formData.value.modelName, baseUrl: formData.value.baseUrl, - apiKey: formData.value.apiKey || '' + apiKey: formData.value.apiKey || '', + provider: formData.value.provider }) break diff --git a/frontend/src/components/UserMenu.vue b/frontend/src/components/UserMenu.vue index 70773ff4..fe44eeff 100644 --- a/frontend/src/components/UserMenu.vue +++ b/frontend/src/components/UserMenu.vue @@ -22,10 +22,6 @@ {{ $t('settings.modelManagement') }} - + -