diff --git a/.github/workflows/auto-i18n.yml b/.github/workflows/auto-i18n.yml index 1584ab48db..6141c061fa 100644 --- a/.github/workflows/auto-i18n.yml +++ b/.github/workflows/auto-i18n.yml @@ -23,7 +23,7 @@ jobs: steps: - name: 🐈‍⬛ Checkout - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: fetch-depth: 0 @@ -77,7 +77,7 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} # Use the built-in GITHUB_TOKEN for bot actions commit-message: "feat(bot): Weekly automated script run" - title: "🤖 Weekly Automated Update: ${{ env.CURRENT_DATE }}" + title: "🤖 Weekly Auto I18N Sync: ${{ env.CURRENT_DATE }}" body: | This PR includes changes generated by the weekly auto i18n. Review the changes before merging. diff --git a/.github/workflows/claude-code-review.yml b/.github/workflows/claude-code-review.yml index cc6d28817f..cc24438768 100644 --- a/.github/workflows/claude-code-review.yml +++ b/.github/workflows/claude-code-review.yml @@ -27,7 +27,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: fetch-depth: 1 diff --git a/.github/workflows/claude-translator.yml b/.github/workflows/claude-translator.yml index 23f359021d..71c2e0b87f 100644 --- a/.github/workflows/claude-translator.yml +++ b/.github/workflows/claude-translator.yml @@ -32,7 +32,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: fetch-depth: 1 diff --git a/.github/workflows/claude.yml b/.github/workflows/claude.yml index 82c7b4393b..be018fb5bb 100644 --- a/.github/workflows/claude.yml +++ b/.github/workflows/claude.yml @@ -37,7 +37,7 @@ jobs: actions: read # Required for Claude to read CI results on PRs steps: - name: Checkout repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: fetch-depth: 1 diff --git a/.github/workflows/dispatch-docs-update.yml b/.github/workflows/dispatch-docs-update.yml index b9457faec6..bb33c60b33 100644 --- a/.github/workflows/dispatch-docs-update.yml +++ b/.github/workflows/dispatch-docs-update.yml @@ -19,7 +19,7 @@ jobs: echo "tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT - name: Dispatch update-download-version workflow to cherry-studio-docs - uses: peter-evans/repository-dispatch@v3 + uses: peter-evans/repository-dispatch@v4 with: token: ${{ secrets.REPO_DISPATCH_TOKEN }} repository: CherryHQ/cherry-studio-docs diff --git a/.github/workflows/github-issue-tracker.yml b/.github/workflows/github-issue-tracker.yml index 32bd393145..a628f9f13c 100644 --- a/.github/workflows/github-issue-tracker.yml +++ b/.github/workflows/github-issue-tracker.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 - name: Check Beijing Time id: check_time @@ -42,7 +42,7 @@ jobs: - name: Add pending label if in quiet hours if: steps.check_time.outputs.should_delay == 'true' - uses: actions/github-script@v7 + uses: actions/github-script@v8 with: script: | github.rest.issues.addLabels({ @@ -118,7 +118,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 - name: Setup Node.js uses: actions/setup-node@v6 diff --git a/.github/workflows/nightly-build.yml b/.github/workflows/nightly-build.yml index 523a670064..eb28b91c63 100644 --- a/.github/workflows/nightly-build.yml +++ b/.github/workflows/nightly-build.yml @@ -51,7 +51,7 @@ jobs: steps: - name: Check out Git repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: ref: main diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index aa273cc56e..1258449007 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -21,7 +21,7 @@ jobs: steps: - name: Check out Git repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 - name: Install Node.js uses: actions/setup-node@v6 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8bbb46ee67..4488b1b9d3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,7 +25,7 @@ jobs: steps: - name: Check out Git repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: fetch-depth: 0 diff --git a/.github/workflows/sync-to-gitcode.yml b/.github/workflows/sync-to-gitcode.yml new file mode 100644 index 0000000000..4462ff6375 --- /dev/null +++ b/.github/workflows/sync-to-gitcode.yml @@ -0,0 +1,293 @@ +name: Sync Release to GitCode + +on: + release: + types: [published] + workflow_dispatch: + inputs: + tag: + description: 'Release tag (e.g. v1.0.0)' + required: true + clean: + description: 'Clean node_modules before build' + type: boolean + default: false + +permissions: + contents: read + +jobs: + build-and-sync-to-gitcode: + runs-on: [self-hosted, windows-signing] + steps: + - name: Get tag name + id: get-tag + shell: bash + run: | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + echo "tag=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT + else + echo "tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT + fi + + - name: Check out Git repository + uses: actions/checkout@v6 + with: + fetch-depth: 0 + ref: ${{ steps.get-tag.outputs.tag }} + + - name: Set package.json version + shell: bash + run: | + TAG="${{ steps.get-tag.outputs.tag }}" + VERSION="${TAG#v}" + npm version "$VERSION" --no-git-tag-version --allow-same-version + + - name: Install Node.js + uses: actions/setup-node@v6 + with: + node-version: 22 + + - name: Install corepack + shell: bash + run: corepack enable && corepack prepare yarn@4.9.1 --activate + + - name: Clean node_modules + if: ${{ github.event.inputs.clean == 'true' }} + shell: bash + run: rm -rf node_modules + + - name: Install Dependencies + shell: bash + run: yarn install + + - name: Build Windows with code signing + shell: bash + run: yarn build:win + env: + WIN_SIGN: true + CHERRY_CERT_PATH: ${{ secrets.CHERRY_CERT_PATH }} + CHERRY_CERT_KEY: ${{ secrets.CHERRY_CERT_KEY }} + CHERRY_CERT_CSP: ${{ secrets.CHERRY_CERT_CSP }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NODE_OPTIONS: --max-old-space-size=8192 + MAIN_VITE_CHERRYAI_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYAI_CLIENT_SECRET }} + MAIN_VITE_MINERU_API_KEY: ${{ secrets.MAIN_VITE_MINERU_API_KEY }} + RENDERER_VITE_AIHUBMIX_SECRET: ${{ secrets.RENDERER_VITE_AIHUBMIX_SECRET }} + RENDERER_VITE_PPIO_APP_SECRET: ${{ secrets.RENDERER_VITE_PPIO_APP_SECRET }} + + - name: List built Windows artifacts + shell: bash + run: | + echo "Built Windows artifacts:" + ls -la dist/*.exe dist/*.blockmap dist/latest*.yml + + - name: Download GitHub release assets + shell: bash + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TAG_NAME: ${{ steps.get-tag.outputs.tag }} + run: | + echo "Downloading release assets for $TAG_NAME..." + mkdir -p release-assets + cd release-assets + + # Download all assets from the release + gh release download "$TAG_NAME" \ + --repo "${{ github.repository }}" \ + --pattern "*" \ + --skip-existing + + echo "Downloaded GitHub release assets:" + ls -la + + - name: Replace Windows files with signed versions + shell: bash + run: | + echo "Replacing Windows files with signed versions..." + + # Verify signed files exist first + if ! ls dist/*.exe 1>/dev/null 2>&1; then + echo "ERROR: No signed .exe files found in dist/" + exit 1 + fi + + # Remove unsigned Windows files from downloaded assets + # *.exe, *.exe.blockmap, latest.yml (Windows only) + rm -f release-assets/*.exe release-assets/*.exe.blockmap release-assets/latest.yml 2>/dev/null || true + + # Copy signed Windows files with error checking + cp dist/*.exe release-assets/ || { echo "ERROR: Failed to copy .exe files"; exit 1; } + cp dist/*.exe.blockmap release-assets/ || { echo "ERROR: Failed to copy .blockmap files"; exit 1; } + cp dist/latest.yml release-assets/ || { echo "ERROR: Failed to copy latest.yml"; exit 1; } + + echo "Final release assets:" + ls -la release-assets/ + + - name: Get release info + id: release-info + shell: bash + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TAG_NAME: ${{ steps.get-tag.outputs.tag }} + LANG: C.UTF-8 + LC_ALL: C.UTF-8 + run: | + # Always use gh cli to avoid special character issues + RELEASE_NAME=$(gh release view "$TAG_NAME" --repo "${{ github.repository }}" --json name -q '.name') + # Use delimiter to safely handle special characters in release name + { + echo 'name<> $GITHUB_OUTPUT + # Extract releaseNotes from electron-builder.yml (from releaseNotes: | to end of file, remove 4-space indent) + sed -n '/releaseNotes: |/,$ { /releaseNotes: |/d; s/^ //; p }' electron-builder.yml > release_body.txt + + - name: Create GitCode release and upload files + shell: bash + env: + GITCODE_TOKEN: ${{ secrets.GITCODE_TOKEN }} + GITCODE_OWNER: ${{ vars.GITCODE_OWNER }} + GITCODE_REPO: ${{ vars.GITCODE_REPO }} + GITCODE_API_URL: ${{ vars.GITCODE_API_URL }} + TAG_NAME: ${{ steps.get-tag.outputs.tag }} + RELEASE_NAME: ${{ steps.release-info.outputs.name }} + LANG: C.UTF-8 + LC_ALL: C.UTF-8 + run: | + # Validate required environment variables + if [ -z "$GITCODE_TOKEN" ]; then + echo "ERROR: GITCODE_TOKEN is not set" + exit 1 + fi + if [ -z "$GITCODE_OWNER" ]; then + echo "ERROR: GITCODE_OWNER is not set" + exit 1 + fi + if [ -z "$GITCODE_REPO" ]; then + echo "ERROR: GITCODE_REPO is not set" + exit 1 + fi + + API_URL="${GITCODE_API_URL:-https://api.gitcode.com/api/v5}" + + echo "Creating GitCode release..." + echo "Tag: $TAG_NAME" + echo "Repo: $GITCODE_OWNER/$GITCODE_REPO" + + # Step 1: Create release + # Use --rawfile to read body directly from file, avoiding shell variable encoding issues + jq -n \ + --arg tag "$TAG_NAME" \ + --arg name "$RELEASE_NAME" \ + --rawfile body release_body.txt \ + '{ + tag_name: $tag, + name: $name, + body: $body, + target_commitish: "main" + }' > /tmp/release_payload.json + + RELEASE_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \ + --connect-timeout 30 --max-time 60 \ + "${API_URL}/repos/${GITCODE_OWNER}/${GITCODE_REPO}/releases" \ + -H "Content-Type: application/json; charset=utf-8" \ + -H "Authorization: Bearer ${GITCODE_TOKEN}" \ + --data-binary "@/tmp/release_payload.json") + + HTTP_CODE=$(echo "$RELEASE_RESPONSE" | tail -n1) + RESPONSE_BODY=$(echo "$RELEASE_RESPONSE" | sed '$d') + + if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then + echo "Release created successfully" + else + echo "Warning: Release creation returned HTTP $HTTP_CODE" + echo "$RESPONSE_BODY" + exit 1 + fi + + # Step 2: Upload files to release + echo "Uploading files to GitCode release..." + + # Function to upload a single file with retry + upload_file() { + local file="$1" + local filename=$(basename "$file") + local max_retries=3 + local retry=0 + + echo "Uploading: $filename" + + # URL encode the filename + encoded_filename=$(printf '%s' "$filename" | jq -sRr @uri) + + while [ $retry -lt $max_retries ]; do + # Get upload URL + UPLOAD_INFO=$(curl -s --connect-timeout 30 --max-time 60 \ + -H "Authorization: Bearer ${GITCODE_TOKEN}" \ + "${API_URL}/repos/${GITCODE_OWNER}/${GITCODE_REPO}/releases/${TAG_NAME}/upload_url?file_name=${encoded_filename}") + + UPLOAD_URL=$(echo "$UPLOAD_INFO" | jq -r '.url // empty') + + if [ -n "$UPLOAD_URL" ]; then + # Write headers to temp file to avoid shell escaping issues + echo "$UPLOAD_INFO" | jq -r '.headers | to_entries[] | "header = \"" + .key + ": " + .value + "\""' > /tmp/upload_headers.txt + + # Upload file using PUT with headers from file + UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X PUT \ + -K /tmp/upload_headers.txt \ + --data-binary "@${file}" \ + "$UPLOAD_URL") + + HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1) + RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed '$d') + + if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then + echo " Uploaded: $filename" + return 0 + else + echo " Failed (HTTP $HTTP_CODE), retry $((retry + 1))/$max_retries" + echo " Response: $RESPONSE_BODY" + fi + else + echo " Failed to get upload URL, retry $((retry + 1))/$max_retries" + echo " Response: $UPLOAD_INFO" + fi + + retry=$((retry + 1)) + [ $retry -lt $max_retries ] && sleep 3 + done + + echo " Failed: $filename after $max_retries retries" + exit 1 + } + + # Upload non-yml/json files first + for file in release-assets/*; do + if [ -f "$file" ]; then + filename=$(basename "$file") + if [[ ! "$filename" =~ \.(yml|yaml|json)$ ]]; then + upload_file "$file" + fi + fi + done + + # Upload yml/json files last + for file in release-assets/*; do + if [ -f "$file" ]; then + filename=$(basename "$file") + if [[ "$filename" =~ \.(yml|yaml|json)$ ]]; then + upload_file "$file" + fi + fi + done + + echo "GitCode release sync completed!" + + - name: Cleanup temp files + if: always() + shell: bash + run: | + rm -f /tmp/release_payload.json /tmp/upload_headers.txt release_body.txt + rm -rf release-assets/ diff --git a/.github/workflows/update-app-upgrade-config.yml b/.github/workflows/update-app-upgrade-config.yml index 7470bb0b6c..8b0b198008 100644 --- a/.github/workflows/update-app-upgrade-config.yml +++ b/.github/workflows/update-app-upgrade-config.yml @@ -19,10 +19,9 @@ on: permissions: contents: write - pull-requests: write jobs: - propose-update: + update-config: runs-on: ubuntu-latest if: github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && github.event.release.draft == false) @@ -135,7 +134,7 @@ jobs: - name: Checkout default branch if: steps.check.outputs.should_run == 'true' - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: ref: ${{ github.event.repository.default_branch }} path: main @@ -143,7 +142,7 @@ jobs: - name: Checkout x-files/app-upgrade-config branch if: steps.check.outputs.should_run == 'true' - uses: actions/checkout@v5 + uses: actions/checkout@v6 with: ref: x-files/app-upgrade-config path: cs @@ -187,25 +186,20 @@ jobs: echo "changed=true" >> "$GITHUB_OUTPUT" fi - - name: Create pull request + - name: Commit and push changes if: steps.check.outputs.should_run == 'true' && steps.diff.outputs.changed == 'true' - uses: peter-evans/create-pull-request@v7 - with: - path: cs - base: x-files/app-upgrade-config - branch: chore/update-app-upgrade-config/${{ steps.meta.outputs.safe_tag }} - commit-message: "🤖 chore: sync app-upgrade-config for ${{ steps.meta.outputs.tag }}" - title: "chore: update app-upgrade-config for ${{ steps.meta.outputs.tag }}" - body: | - Automated update triggered by `${{ steps.meta.outputs.trigger }}`. + working-directory: cs + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add app-upgrade-config.json + git commit -m "chore: sync app-upgrade-config for ${{ steps.meta.outputs.tag }}" -m "Automated update triggered by \`${{ steps.meta.outputs.trigger }}\`. - - Source tag: `${{ steps.meta.outputs.tag }}` - - Pre-release: `${{ steps.meta.outputs.prerelease }}` - - Latest: `${{ steps.meta.outputs.latest }}` - - Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - labels: | - automation - app-upgrade + - Source tag: \`${{ steps.meta.outputs.tag }}\` + - Pre-release: \`${{ steps.meta.outputs.prerelease }}\` + - Latest: \`${{ steps.meta.outputs.latest }}\` + - Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" + git push origin x-files/app-upgrade-config - name: No changes detected if: steps.check.outputs.should_run == 'true' && steps.diff.outputs.changed != 'true' diff --git a/.oxlintrc.json b/.oxlintrc.json index 7d18f83c7c..093ae25f18 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -11,6 +11,7 @@ "dist/**", "out/**", "local/**", + "tests/**", ".yarn/**", ".gitignore", "scripts/cloudflare-worker.js", diff --git a/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch b/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch deleted file mode 100644 index 18570d5ced..0000000000 --- a/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch +++ /dev/null @@ -1,152 +0,0 @@ -diff --git a/dist/index.js b/dist/index.js -index c2ef089c42e13a8ee4a833899a415564130e5d79..75efa7baafb0f019fb44dd50dec1641eee8879e7 100644 ---- a/dist/index.js -+++ b/dist/index.js -@@ -471,7 +471,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) { - - // src/get-model-path.ts - function getModelPath(modelId) { -- return modelId.includes("/") ? modelId : `models/${modelId}`; -+ return modelId.includes("models/") ? modelId : `models/${modelId}`; - } - - // src/google-generative-ai-options.ts -diff --git a/dist/index.mjs b/dist/index.mjs -index d75c0cc13c41192408c1f3f2d29d76a7bffa6268..ada730b8cb97d9b7d4cb32883a1d1ff416404d9b 100644 ---- a/dist/index.mjs -+++ b/dist/index.mjs -@@ -477,7 +477,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) { - - // src/get-model-path.ts - function getModelPath(modelId) { -- return modelId.includes("/") ? modelId : `models/${modelId}`; -+ return modelId.includes("models/") ? modelId : `models/${modelId}`; - } - - // src/google-generative-ai-options.ts -diff --git a/dist/internal/index.js b/dist/internal/index.js -index 277cac8dc734bea2fb4f3e9a225986b402b24f48..bb704cd79e602eb8b0cee1889e42497d59ccdb7a 100644 ---- a/dist/internal/index.js -+++ b/dist/internal/index.js -@@ -432,7 +432,15 @@ function prepareTools({ - var _a; - tools = (tools == null ? void 0 : tools.length) ? tools : void 0; - const toolWarnings = []; -- const isGemini2 = modelId.includes("gemini-2"); -+ // These changes could be safely removed when @ai-sdk/google v3 released. -+ const isLatest = ( -+ [ -+ 'gemini-flash-latest', -+ 'gemini-flash-lite-latest', -+ 'gemini-pro-latest', -+ ] -+ ).some(id => id === modelId); -+ const isGemini2OrNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || isLatest; - const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b"); - const supportsFileSearch = modelId.includes("gemini-2.5"); - if (tools == null) { -@@ -458,7 +466,7 @@ function prepareTools({ - providerDefinedTools.forEach((tool) => { - switch (tool.id) { - case "google.google_search": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ googleSearch: {} }); - } else if (supportsDynamicRetrieval) { - googleTools2.push({ -@@ -474,7 +482,7 @@ function prepareTools({ - } - break; - case "google.url_context": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ urlContext: {} }); - } else { - toolWarnings.push({ -@@ -485,7 +493,7 @@ function prepareTools({ - } - break; - case "google.code_execution": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ codeExecution: {} }); - } else { - toolWarnings.push({ -@@ -507,7 +515,7 @@ function prepareTools({ - } - break; - case "google.vertex_rag_store": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ - retrieval: { - vertex_rag_store: { -diff --git a/dist/internal/index.mjs b/dist/internal/index.mjs -index 03b7cc591be9b58bcc2e775a96740d9f98862a10..347d2c12e1cee79f0f8bb258f3844fb0522a6485 100644 ---- a/dist/internal/index.mjs -+++ b/dist/internal/index.mjs -@@ -424,7 +424,15 @@ function prepareTools({ - var _a; - tools = (tools == null ? void 0 : tools.length) ? tools : void 0; - const toolWarnings = []; -- const isGemini2 = modelId.includes("gemini-2"); -+ // These changes could be safely removed when @ai-sdk/google v3 released. -+ const isLatest = ( -+ [ -+ 'gemini-flash-latest', -+ 'gemini-flash-lite-latest', -+ 'gemini-pro-latest', -+ ] -+ ).some(id => id === modelId); -+ const isGemini2OrNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || isLatest; - const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b"); - const supportsFileSearch = modelId.includes("gemini-2.5"); - if (tools == null) { -@@ -450,7 +458,7 @@ function prepareTools({ - providerDefinedTools.forEach((tool) => { - switch (tool.id) { - case "google.google_search": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ googleSearch: {} }); - } else if (supportsDynamicRetrieval) { - googleTools2.push({ -@@ -466,7 +474,7 @@ function prepareTools({ - } - break; - case "google.url_context": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ urlContext: {} }); - } else { - toolWarnings.push({ -@@ -477,7 +485,7 @@ function prepareTools({ - } - break; - case "google.code_execution": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ codeExecution: {} }); - } else { - toolWarnings.push({ -@@ -499,7 +507,7 @@ function prepareTools({ - } - break; - case "google.vertex_rag_store": -- if (isGemini2) { -+ if (isGemini2OrNewer) { - googleTools2.push({ - retrieval: { - vertex_rag_store: { -@@ -1434,9 +1442,7 @@ var googleTools = { - vertexRagStore - }; - export { -- GoogleGenerativeAILanguageModel, - getGroundingMetadataSchema, -- getUrlContextMetadataSchema, -- googleTools -+ getUrlContextMetadataSchema, GoogleGenerativeAILanguageModel, googleTools - }; - //# sourceMappingURL=index.mjs.map -\ No newline at end of file diff --git a/.yarn/patches/@ai-sdk-google-npm-2.0.43-689ed559b3.patch b/.yarn/patches/@ai-sdk-google-npm-2.0.43-689ed559b3.patch new file mode 100644 index 0000000000..3015e702ed --- /dev/null +++ b/.yarn/patches/@ai-sdk-google-npm-2.0.43-689ed559b3.patch @@ -0,0 +1,26 @@ +diff --git a/dist/index.js b/dist/index.js +index 51ce7e423934fb717cb90245cdfcdb3dae6780e6..0f7f7009e2f41a79a8669d38c8a44867bbff5e1f 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -474,7 +474,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) { + + // src/get-model-path.ts + function getModelPath(modelId) { +- return modelId.includes("/") ? modelId : `models/${modelId}`; ++ return modelId.includes("models/") ? modelId : `models/${modelId}`; + } + + // src/google-generative-ai-options.ts +diff --git a/dist/index.mjs b/dist/index.mjs +index f4b77e35c0cbfece85a3ef0d4f4e67aa6dde6271..8d2fecf8155a226006a0bde72b00b6036d4014b6 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -480,7 +480,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) { + + // src/get-model-path.ts + function getModelPath(modelId) { +- return modelId.includes("/") ? modelId : `models/${modelId}`; ++ return modelId.includes("models/") ? modelId : `models/${modelId}`; + } + + // src/google-generative-ai-options.ts diff --git a/.yarn/patches/@ai-sdk-huggingface-npm-0.0.8-d4d0aaac93.patch b/.yarn/patches/@ai-sdk-huggingface-npm-0.0.8-d4d0aaac93.patch deleted file mode 100644 index 7aeb4ea9cf..0000000000 --- a/.yarn/patches/@ai-sdk-huggingface-npm-0.0.8-d4d0aaac93.patch +++ /dev/null @@ -1,131 +0,0 @@ -diff --git a/dist/index.mjs b/dist/index.mjs -index b3f018730a93639aad7c203f15fb1aeb766c73f4..ade2a43d66e9184799d072153df61ef7be4ea110 100644 ---- a/dist/index.mjs -+++ b/dist/index.mjs -@@ -296,7 +296,14 @@ var HuggingFaceResponsesLanguageModel = class { - metadata: huggingfaceOptions == null ? void 0 : huggingfaceOptions.metadata, - instructions: huggingfaceOptions == null ? void 0 : huggingfaceOptions.instructions, - ...preparedTools && { tools: preparedTools }, -- ...preparedToolChoice && { tool_choice: preparedToolChoice } -+ ...preparedToolChoice && { tool_choice: preparedToolChoice }, -+ ...(huggingfaceOptions?.reasoningEffort != null && { -+ reasoning: { -+ ...(huggingfaceOptions?.reasoningEffort != null && { -+ effort: huggingfaceOptions.reasoningEffort, -+ }), -+ }, -+ }), - }; - return { args: baseArgs, warnings }; - } -@@ -365,6 +372,20 @@ var HuggingFaceResponsesLanguageModel = class { - } - break; - } -+ case 'reasoning': { -+ for (const contentPart of part.content) { -+ content.push({ -+ type: 'reasoning', -+ text: contentPart.text, -+ providerMetadata: { -+ huggingface: { -+ itemId: part.id, -+ }, -+ }, -+ }); -+ } -+ break; -+ } - case "mcp_call": { - content.push({ - type: "tool-call", -@@ -519,6 +540,11 @@ var HuggingFaceResponsesLanguageModel = class { - id: value.item.call_id, - toolName: value.item.name - }); -+ } else if (value.item.type === 'reasoning') { -+ controller.enqueue({ -+ type: 'reasoning-start', -+ id: value.item.id, -+ }); - } - return; - } -@@ -570,6 +596,22 @@ var HuggingFaceResponsesLanguageModel = class { - }); - return; - } -+ if (isReasoningDeltaChunk(value)) { -+ controller.enqueue({ -+ type: 'reasoning-delta', -+ id: value.item_id, -+ delta: value.delta, -+ }); -+ return; -+ } -+ -+ if (isReasoningEndChunk(value)) { -+ controller.enqueue({ -+ type: 'reasoning-end', -+ id: value.item_id, -+ }); -+ return; -+ } - }, - flush(controller) { - controller.enqueue({ -@@ -593,7 +635,8 @@ var HuggingFaceResponsesLanguageModel = class { - var huggingfaceResponsesProviderOptionsSchema = z2.object({ - metadata: z2.record(z2.string(), z2.string()).optional(), - instructions: z2.string().optional(), -- strictJsonSchema: z2.boolean().optional() -+ strictJsonSchema: z2.boolean().optional(), -+ reasoningEffort: z2.string().optional(), - }); - var huggingfaceResponsesResponseSchema = z2.object({ - id: z2.string(), -@@ -727,12 +770,31 @@ var responseCreatedChunkSchema = z2.object({ - model: z2.string() - }) - }); -+var reasoningTextDeltaChunkSchema = z2.object({ -+ type: z2.literal('response.reasoning_text.delta'), -+ item_id: z2.string(), -+ output_index: z2.number(), -+ content_index: z2.number(), -+ delta: z2.string(), -+ sequence_number: z2.number(), -+}); -+ -+var reasoningTextEndChunkSchema = z2.object({ -+ type: z2.literal('response.reasoning_text.done'), -+ item_id: z2.string(), -+ output_index: z2.number(), -+ content_index: z2.number(), -+ text: z2.string(), -+ sequence_number: z2.number(), -+}); - var huggingfaceResponsesChunkSchema = z2.union([ - responseOutputItemAddedSchema, - responseOutputItemDoneSchema, - textDeltaChunkSchema, - responseCompletedChunkSchema, - responseCreatedChunkSchema, -+ reasoningTextDeltaChunkSchema, -+ reasoningTextEndChunkSchema, - z2.object({ type: z2.string() }).loose() - // fallback for unknown chunks - ]); -@@ -751,6 +813,12 @@ function isResponseCompletedChunk(chunk) { - function isResponseCreatedChunk(chunk) { - return chunk.type === "response.created"; - } -+function isReasoningDeltaChunk(chunk) { -+ return chunk.type === 'response.reasoning_text.delta'; -+} -+function isReasoningEndChunk(chunk) { -+ return chunk.type === 'response.reasoning_text.done'; -+} - - // src/huggingface-provider.ts - function createHuggingFace(options = {}) { diff --git a/.yarn/patches/@ai-sdk-openai-compatible-npm-1.0.27-06f74278cf.patch b/.yarn/patches/@ai-sdk-openai-compatible-npm-1.0.27-06f74278cf.patch new file mode 100644 index 0000000000..2a13c33a78 --- /dev/null +++ b/.yarn/patches/@ai-sdk-openai-compatible-npm-1.0.27-06f74278cf.patch @@ -0,0 +1,140 @@ +diff --git a/dist/index.js b/dist/index.js +index 73045a7d38faafdc7f7d2cd79d7ff0e2b031056b..8d948c9ac4ea4b474db9ef3c5491961e7fcf9a07 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -421,6 +421,17 @@ var OpenAICompatibleChatLanguageModel = class { + text: reasoning + }); + } ++ if (choice.message.images) { ++ for (const image of choice.message.images) { ++ const match1 = image.image_url.url.match(/^data:([^;]+)/) ++ const match2 = image.image_url.url.match(/^data:[^;]*;base64,(.+)$/); ++ content.push({ ++ type: 'file', ++ mediaType: match1 ? (match1[1] ?? 'image/jpeg') : 'image/jpeg', ++ data: match2 ? match2[1] : image.image_url.url, ++ }); ++ } ++ } + if (choice.message.tool_calls != null) { + for (const toolCall of choice.message.tool_calls) { + content.push({ +@@ -598,6 +609,17 @@ var OpenAICompatibleChatLanguageModel = class { + delta: delta.content + }); + } ++ if (delta.images) { ++ for (const image of delta.images) { ++ const match1 = image.image_url.url.match(/^data:([^;]+)/) ++ const match2 = image.image_url.url.match(/^data:[^;]*;base64,(.+)$/); ++ controller.enqueue({ ++ type: 'file', ++ mediaType: match1 ? (match1[1] ?? 'image/jpeg') : 'image/jpeg', ++ data: match2 ? match2[1] : image.image_url.url, ++ }); ++ } ++ } + if (delta.tool_calls != null) { + for (const toolCallDelta of delta.tool_calls) { + const index = toolCallDelta.index; +@@ -765,6 +787,14 @@ var OpenAICompatibleChatResponseSchema = import_v43.z.object({ + arguments: import_v43.z.string() + }) + }) ++ ).nullish(), ++ images: import_v43.z.array( ++ import_v43.z.object({ ++ type: import_v43.z.literal('image_url'), ++ image_url: import_v43.z.object({ ++ url: import_v43.z.string(), ++ }) ++ }) + ).nullish() + }), + finish_reason: import_v43.z.string().nullish() +@@ -795,6 +825,14 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v43.z.union( + arguments: import_v43.z.string().nullish() + }) + }) ++ ).nullish(), ++ images: import_v43.z.array( ++ import_v43.z.object({ ++ type: import_v43.z.literal('image_url'), ++ image_url: import_v43.z.object({ ++ url: import_v43.z.string(), ++ }) ++ }) + ).nullish() + }).nullish(), + finish_reason: import_v43.z.string().nullish() +diff --git a/dist/index.mjs b/dist/index.mjs +index 1c2b9560bbfbfe10cb01af080aeeed4ff59db29c..2c8ddc4fc9bfc5e7e06cfca105d197a08864c427 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -405,6 +405,17 @@ var OpenAICompatibleChatLanguageModel = class { + text: reasoning + }); + } ++ if (choice.message.images) { ++ for (const image of choice.message.images) { ++ const match1 = image.image_url.url.match(/^data:([^;]+)/) ++ const match2 = image.image_url.url.match(/^data:[^;]*;base64,(.+)$/); ++ content.push({ ++ type: 'file', ++ mediaType: match1 ? (match1[1] ?? 'image/jpeg') : 'image/jpeg', ++ data: match2 ? match2[1] : image.image_url.url, ++ }); ++ } ++ } + if (choice.message.tool_calls != null) { + for (const toolCall of choice.message.tool_calls) { + content.push({ +@@ -582,6 +593,17 @@ var OpenAICompatibleChatLanguageModel = class { + delta: delta.content + }); + } ++ if (delta.images) { ++ for (const image of delta.images) { ++ const match1 = image.image_url.url.match(/^data:([^;]+)/) ++ const match2 = image.image_url.url.match(/^data:[^;]*;base64,(.+)$/); ++ controller.enqueue({ ++ type: 'file', ++ mediaType: match1 ? (match1[1] ?? 'image/jpeg') : 'image/jpeg', ++ data: match2 ? match2[1] : image.image_url.url, ++ }); ++ } ++ } + if (delta.tool_calls != null) { + for (const toolCallDelta of delta.tool_calls) { + const index = toolCallDelta.index; +@@ -749,6 +771,14 @@ var OpenAICompatibleChatResponseSchema = z3.object({ + arguments: z3.string() + }) + }) ++ ).nullish(), ++ images: z3.array( ++ z3.object({ ++ type: z3.literal('image_url'), ++ image_url: z3.object({ ++ url: z3.string(), ++ }) ++ }) + ).nullish() + }), + finish_reason: z3.string().nullish() +@@ -779,6 +809,14 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([ + arguments: z3.string().nullish() + }) + }) ++ ).nullish(), ++ images: z3.array( ++ z3.object({ ++ type: z3.literal('image_url'), ++ image_url: z3.object({ ++ url: z3.string(), ++ }) ++ }) + ).nullish() + }).nullish(), + finish_reason: z3.string().nullish() diff --git a/.yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch b/.yarn/patches/@ai-sdk-openai-npm-2.0.85-27483d1d6a.patch similarity index 84% rename from .yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch rename to .yarn/patches/@ai-sdk-openai-npm-2.0.85-27483d1d6a.patch index 22b5cf6ea8..6fbe30e080 100644 --- a/.yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch +++ b/.yarn/patches/@ai-sdk-openai-npm-2.0.85-27483d1d6a.patch @@ -1,8 +1,8 @@ diff --git a/dist/index.js b/dist/index.js -index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa96b52ac0d 100644 +index 130094d194ea1e8e7d3027d07d82465741192124..4d13dcee8c962ca9ee8f1c3d748f8ffe6a3cfb47 100644 --- a/dist/index.js +++ b/dist/index.js -@@ -274,6 +274,7 @@ var openaiChatResponseSchema = (0, import_provider_utils3.lazyValidator)( +@@ -290,6 +290,7 @@ var openaiChatResponseSchema = (0, import_provider_utils3.lazyValidator)( message: import_v42.z.object({ role: import_v42.z.literal("assistant").nullish(), content: import_v42.z.string().nullish(), @@ -10,7 +10,7 @@ index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa9 tool_calls: import_v42.z.array( import_v42.z.object({ id: import_v42.z.string().nullish(), -@@ -340,6 +341,7 @@ var openaiChatChunkSchema = (0, import_provider_utils3.lazyValidator)( +@@ -356,6 +357,7 @@ var openaiChatChunkSchema = (0, import_provider_utils3.lazyValidator)( delta: import_v42.z.object({ role: import_v42.z.enum(["assistant"]).nullish(), content: import_v42.z.string().nullish(), @@ -18,7 +18,7 @@ index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa9 tool_calls: import_v42.z.array( import_v42.z.object({ index: import_v42.z.number(), -@@ -785,6 +787,13 @@ var OpenAIChatLanguageModel = class { +@@ -814,6 +816,13 @@ var OpenAIChatLanguageModel = class { if (text != null && text.length > 0) { content.push({ type: "text", text }); } @@ -32,7 +32,7 @@ index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa9 for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) { content.push({ type: "tool-call", -@@ -866,6 +875,7 @@ var OpenAIChatLanguageModel = class { +@@ -895,6 +904,7 @@ var OpenAIChatLanguageModel = class { }; let metadataExtracted = false; let isActiveText = false; @@ -40,7 +40,7 @@ index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa9 const providerMetadata = { openai: {} }; return { stream: response.pipeThrough( -@@ -923,6 +933,21 @@ var OpenAIChatLanguageModel = class { +@@ -952,6 +962,21 @@ var OpenAIChatLanguageModel = class { return; } const delta = choice.delta; @@ -62,7 +62,7 @@ index 992c85ac6656e51c3471af741583533c5a7bf79f..83c05952a07aebb95fc6c62f9ddb8aa9 if (delta.content != null) { if (!isActiveText) { controller.enqueue({ type: "text-start", id: "0" }); -@@ -1035,6 +1060,9 @@ var OpenAIChatLanguageModel = class { +@@ -1064,6 +1089,9 @@ var OpenAIChatLanguageModel = class { } }, flush(controller) { diff --git a/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.30-b50a299674.patch b/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.62-23ae56f8c8.patch similarity index 69% rename from .yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.30-b50a299674.patch rename to .yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.62-23ae56f8c8.patch index 896b2d4cbf..62ab767576 100644 --- a/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.30-b50a299674.patch +++ b/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.62-23ae56f8c8.patch @@ -1,8 +1,8 @@ diff --git a/sdk.mjs b/sdk.mjs -index 8cc6aaf0b25bcdf3c579ec95cde12d419fcb2a71..3b3b8beaea5ad2bbac26a15f792058306d0b059f 100755 +index dea7766a3432a1e809f12d6daba4f2834a219689..e0b02ef73da177ba32b903887d7bbbeaa08cc6d3 100755 --- a/sdk.mjs +++ b/sdk.mjs -@@ -6213,7 +6213,7 @@ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) { +@@ -6250,7 +6250,7 @@ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) { } // ../src/transport/ProcessTransport.ts @@ -11,16 +11,20 @@ index 8cc6aaf0b25bcdf3c579ec95cde12d419fcb2a71..3b3b8beaea5ad2bbac26a15f79205830 import { createInterface } from "readline"; // ../src/utils/fsOperations.ts -@@ -6505,14 +6505,11 @@ class ProcessTransport { +@@ -6644,18 +6644,11 @@ class ProcessTransport { const errorMessage = isNativeBinary(pathToClaudeCodeExecutable) ? `Claude Code native binary not found at ${pathToClaudeCodeExecutable}. Please ensure Claude Code is installed via native installer or specify a valid path with options.pathToClaudeCodeExecutable.` : `Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`; throw new ReferenceError(errorMessage); } - const isNative = isNativeBinary(pathToClaudeCodeExecutable); - const spawnCommand = isNative ? pathToClaudeCodeExecutable : executable; - const spawnArgs = isNative ? [...executableArgs, ...args] : [...executableArgs, pathToClaudeCodeExecutable, ...args]; -- this.logForDebugging(isNative ? `Spawning Claude Code native binary: ${spawnCommand} ${spawnArgs.join(" ")}` : `Spawning Claude Code process: ${spawnCommand} ${spawnArgs.join(" ")}`); -+ this.logForDebugging(`Forking Claude Code Node.js process: ${pathToClaudeCodeExecutable} ${args.join(" ")}`); - const stderrMode = env.DEBUG || stderr ? "pipe" : "ignore"; +- const spawnMessage = isNative ? `Spawning Claude Code native binary: ${spawnCommand} ${spawnArgs.join(" ")}` : `Spawning Claude Code process: ${spawnCommand} ${spawnArgs.join(" ")}`; +- logForSdkDebugging(spawnMessage); +- if (stderr) { +- stderr(spawnMessage); +- } ++ logForSdkDebugging(`Forking Claude Code Node.js process: ${pathToClaudeCodeExecutable} ${args.join(" ")}`); + const stderrMode = env.DEBUG_CLAUDE_AGENT_SDK || stderr ? "pipe" : "ignore"; - this.child = spawn(spawnCommand, spawnArgs, { + this.child = fork(pathToClaudeCodeExecutable, args, { cwd, diff --git a/.yarn/patches/ollama-ai-provider-v2-npm-1.5.5-8bef249af9.patch b/.yarn/patches/ollama-ai-provider-v2-npm-1.5.5-8bef249af9.patch new file mode 100644 index 0000000000..ea14381539 --- /dev/null +++ b/.yarn/patches/ollama-ai-provider-v2-npm-1.5.5-8bef249af9.patch @@ -0,0 +1,145 @@ +diff --git a/dist/index.d.ts b/dist/index.d.ts +index 8dd9b498050dbecd8dd6b901acf1aa8ca38a49af..ed644349c9d38fe2a66b2fb44214f7c18eb97f89 100644 +--- a/dist/index.d.ts ++++ b/dist/index.d.ts +@@ -4,7 +4,7 @@ import { z } from 'zod/v4'; + + type OllamaChatModelId = "athene-v2" | "athene-v2:72b" | "aya-expanse" | "aya-expanse:8b" | "aya-expanse:32b" | "codegemma" | "codegemma:2b" | "codegemma:7b" | "codellama" | "codellama:7b" | "codellama:13b" | "codellama:34b" | "codellama:70b" | "codellama:code" | "codellama:python" | "command-r" | "command-r:35b" | "command-r-plus" | "command-r-plus:104b" | "command-r7b" | "command-r7b:7b" | "deepseek-r1" | "deepseek-r1:1.5b" | "deepseek-r1:7b" | "deepseek-r1:8b" | "deepseek-r1:14b" | "deepseek-r1:32b" | "deepseek-r1:70b" | "deepseek-r1:671b" | "deepseek-coder-v2" | "deepseek-coder-v2:16b" | "deepseek-coder-v2:236b" | "deepseek-v3" | "deepseek-v3:671b" | "devstral" | "devstral:24b" | "dolphin3" | "dolphin3:8b" | "exaone3.5" | "exaone3.5:2.4b" | "exaone3.5:7.8b" | "exaone3.5:32b" | "falcon2" | "falcon2:11b" | "falcon3" | "falcon3:1b" | "falcon3:3b" | "falcon3:7b" | "falcon3:10b" | "firefunction-v2" | "firefunction-v2:70b" | "gemma" | "gemma:2b" | "gemma:7b" | "gemma2" | "gemma2:2b" | "gemma2:9b" | "gemma2:27b" | "gemma3" | "gemma3:1b" | "gemma3:4b" | "gemma3:12b" | "gemma3:27b" | "granite3-dense" | "granite3-dense:2b" | "granite3-dense:8b" | "granite3-guardian" | "granite3-guardian:2b" | "granite3-guardian:8b" | "granite3-moe" | "granite3-moe:1b" | "granite3-moe:3b" | "granite3.1-dense" | "granite3.1-dense:2b" | "granite3.1-dense:8b" | "granite3.1-moe" | "granite3.1-moe:1b" | "granite3.1-moe:3b" | "llama2" | "llama2:7b" | "llama2:13b" | "llama2:70b" | "llama3" | "llama3:8b" | "llama3:70b" | "llama3-chatqa" | "llama3-chatqa:8b" | "llama3-chatqa:70b" | "llama3-gradient" | "llama3-gradient:8b" | "llama3-gradient:70b" | "llama3.1" | "llama3.1:8b" | "llama3.1:70b" | "llama3.1:405b" | "llama3.2" | "llama3.2:1b" | "llama3.2:3b" | "llama3.2-vision" | "llama3.2-vision:11b" | "llama3.2-vision:90b" | "llama3.3" | "llama3.3:70b" | "llama4" | "llama4:16x17b" | "llama4:128x17b" | "llama-guard3" | "llama-guard3:1b" | "llama-guard3:8b" | "llava" | "llava:7b" | "llava:13b" | "llava:34b" | "llava-llama3" | "llava-llama3:8b" | "llava-phi3" | "llava-phi3:3.8b" | "marco-o1" | "marco-o1:7b" | "mistral" | "mistral:7b" | "mistral-large" | "mistral-large:123b" | "mistral-nemo" | "mistral-nemo:12b" | "mistral-small" | "mistral-small:22b" | "mixtral" | "mixtral:8x7b" | "mixtral:8x22b" | "moondream" | "moondream:1.8b" | "openhermes" | "openhermes:v2.5" | "nemotron" | "nemotron:70b" | "nemotron-mini" | "nemotron-mini:4b" | "olmo" | "olmo:7b" | "olmo:13b" | "opencoder" | "opencoder:1.5b" | "opencoder:8b" | "phi3" | "phi3:3.8b" | "phi3:14b" | "phi3.5" | "phi3.5:3.8b" | "phi4" | "phi4:14b" | "qwen" | "qwen:7b" | "qwen:14b" | "qwen:32b" | "qwen:72b" | "qwen:110b" | "qwen2" | "qwen2:0.5b" | "qwen2:1.5b" | "qwen2:7b" | "qwen2:72b" | "qwen2.5" | "qwen2.5:0.5b" | "qwen2.5:1.5b" | "qwen2.5:3b" | "qwen2.5:7b" | "qwen2.5:14b" | "qwen2.5:32b" | "qwen2.5:72b" | "qwen2.5-coder" | "qwen2.5-coder:0.5b" | "qwen2.5-coder:1.5b" | "qwen2.5-coder:3b" | "qwen2.5-coder:7b" | "qwen2.5-coder:14b" | "qwen2.5-coder:32b" | "qwen3" | "qwen3:0.6b" | "qwen3:1.7b" | "qwen3:4b" | "qwen3:8b" | "qwen3:14b" | "qwen3:30b" | "qwen3:32b" | "qwen3:235b" | "qwq" | "qwq:32b" | "sailor2" | "sailor2:1b" | "sailor2:8b" | "sailor2:20b" | "shieldgemma" | "shieldgemma:2b" | "shieldgemma:9b" | "shieldgemma:27b" | "smallthinker" | "smallthinker:3b" | "smollm" | "smollm:135m" | "smollm:360m" | "smollm:1.7b" | "tinyllama" | "tinyllama:1.1b" | "tulu3" | "tulu3:8b" | "tulu3:70b" | (string & {}); + declare const ollamaProviderOptions: z.ZodObject<{ +- think: z.ZodOptional; ++ think: z.ZodOptional]>>; + options: z.ZodOptional; + repeat_last_n: z.ZodOptional; +@@ -27,9 +27,11 @@ interface OllamaCompletionSettings { + * the model's thinking from the model's output. When disabled, the model will not think + * and directly output the content. + * ++ * For gpt-oss models, you can also use 'low', 'medium', or 'high' to control the depth of thinking. ++ * + * Only supported by certain models like DeepSeek R1 and Qwen 3. + */ +- think?: boolean; ++ think?: boolean | 'low' | 'medium' | 'high'; + /** + * Echo back the prompt in addition to the completion. + */ +@@ -146,7 +148,7 @@ declare const ollamaEmbeddingProviderOptions: z.ZodObject<{ + type OllamaEmbeddingProviderOptions = z.infer; + + declare const ollamaCompletionProviderOptions: z.ZodObject<{ +- think: z.ZodOptional; ++ think: z.ZodOptional]>>; + user: z.ZodOptional; + suffix: z.ZodOptional; + echo: z.ZodOptional; +diff --git a/dist/index.js b/dist/index.js +index 35b5142ce8476ce2549ed7c2ec48e7d8c46c90d9..2ef64dc9a4c2be043e6af608241a6a8309a5a69f 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -158,7 +158,7 @@ function getResponseMetadata({ + + // src/completion/ollama-completion-language-model.ts + var ollamaCompletionProviderOptions = import_v42.z.object({ +- think: import_v42.z.boolean().optional(), ++ think: import_v42.z.union([import_v42.z.boolean(), import_v42.z.enum(['low', 'medium', 'high'])]).optional(), + user: import_v42.z.string().optional(), + suffix: import_v42.z.string().optional(), + echo: import_v42.z.boolean().optional() +@@ -662,7 +662,7 @@ function convertToOllamaChatMessages({ + const images = content.filter((part) => part.type === "file" && part.mediaType.startsWith("image/")).map((part) => part.data); + messages.push({ + role: "user", +- content: userText.length > 0 ? userText : [], ++ content: userText.length > 0 ? userText : '', + images: images.length > 0 ? images : void 0 + }); + break; +@@ -813,9 +813,11 @@ var ollamaProviderOptions = import_v44.z.object({ + * the model's thinking from the model's output. When disabled, the model will not think + * and directly output the content. + * ++ * For gpt-oss models, you can also use 'low', 'medium', or 'high' to control the depth of thinking. ++ * + * Only supported by certain models like DeepSeek R1 and Qwen 3. + */ +- think: import_v44.z.boolean().optional(), ++ think: import_v44.z.union([import_v44.z.boolean(), import_v44.z.enum(['low', 'medium', 'high'])]).optional(), + options: import_v44.z.object({ + num_ctx: import_v44.z.number().optional(), + repeat_last_n: import_v44.z.number().optional(), +@@ -929,14 +931,16 @@ var OllamaRequestBuilder = class { + prompt, + systemMessageMode: "system" + }), +- temperature, +- top_p: topP, + max_output_tokens: maxOutputTokens, + ...(responseFormat == null ? void 0 : responseFormat.type) === "json" && { + format: responseFormat.schema != null ? responseFormat.schema : "json" + }, + think: (_a = ollamaOptions == null ? void 0 : ollamaOptions.think) != null ? _a : false, +- options: (_b = ollamaOptions == null ? void 0 : ollamaOptions.options) != null ? _b : void 0 ++ options: { ++ ...temperature !== void 0 && { temperature }, ++ ...topP !== void 0 && { top_p: topP }, ++ ...((_b = ollamaOptions == null ? void 0 : ollamaOptions.options) != null ? _b : {}) ++ } + }; + } + }; +diff --git a/dist/index.mjs b/dist/index.mjs +index e2a634a78d80ac9542f2cc4f96cf2291094b10cf..67b23efce3c1cf4f026693d3ff9246988a3ef26e 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -144,7 +144,7 @@ function getResponseMetadata({ + + // src/completion/ollama-completion-language-model.ts + var ollamaCompletionProviderOptions = z2.object({ +- think: z2.boolean().optional(), ++ think: z2.union([z2.boolean(), z2.enum(['low', 'medium', 'high'])]).optional(), + user: z2.string().optional(), + suffix: z2.string().optional(), + echo: z2.boolean().optional() +@@ -662,7 +662,7 @@ function convertToOllamaChatMessages({ + const images = content.filter((part) => part.type === "file" && part.mediaType.startsWith("image/")).map((part) => part.data); + messages.push({ + role: "user", +- content: userText.length > 0 ? userText : [], ++ content: userText.length > 0 ? userText : '', + images: images.length > 0 ? images : void 0 + }); + break; +@@ -815,9 +815,11 @@ var ollamaProviderOptions = z4.object({ + * the model's thinking from the model's output. When disabled, the model will not think + * and directly output the content. + * ++ * For gpt-oss models, you can also use 'low', 'medium', or 'high' to control the depth of thinking. ++ * + * Only supported by certain models like DeepSeek R1 and Qwen 3. + */ +- think: z4.boolean().optional(), ++ think: z4.union([z4.boolean(), z4.enum(['low', 'medium', 'high'])]).optional(), + options: z4.object({ + num_ctx: z4.number().optional(), + repeat_last_n: z4.number().optional(), +@@ -931,14 +933,16 @@ var OllamaRequestBuilder = class { + prompt, + systemMessageMode: "system" + }), +- temperature, +- top_p: topP, + max_output_tokens: maxOutputTokens, + ...(responseFormat == null ? void 0 : responseFormat.type) === "json" && { + format: responseFormat.schema != null ? responseFormat.schema : "json" + }, + think: (_a = ollamaOptions == null ? void 0 : ollamaOptions.think) != null ? _a : false, +- options: (_b = ollamaOptions == null ? void 0 : ollamaOptions.options) != null ? _b : void 0 ++ options: { ++ ...temperature !== void 0 && { temperature }, ++ ...topP !== void 0 && { top_p: topP }, ++ ...((_b = ollamaOptions == null ? void 0 : ollamaOptions.options) != null ? _b : {}) ++ } + }; + } + }; diff --git a/CLAUDE.md b/CLAUDE.md index 372bff256c..c96fc0e403 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -10,8 +10,18 @@ This file provides guidance to AI coding assistants when working with code in th - **Log centrally**: Route all logging through `loggerService` with the right context—no `console.log`. - **Research via subagent**: Lean on `subagent` for external docs, APIs, news, and references. - **Always propose before executing**: Before making any changes, clearly explain your planned approach and wait for explicit user approval to ensure alignment and prevent unwanted modifications. +- **Lint, test, and format before completion**: Coding tasks are only complete after running `yarn lint`, `yarn test`, and `yarn format` successfully. - **Write conventional commits**: Commit small, focused changes using Conventional Commit messages (e.g., `feat:`, `fix:`, `refactor:`, `docs:`). +## Pull Request Workflow (CRITICAL) + +When creating a Pull Request, you MUST: + +1. **Read the PR template first**: Always read `.github/pull_request_template.md` before creating the PR +2. **Follow ALL template sections**: Structure the `--body` parameter to include every section from the template +3. **Never skip sections**: Include all sections even if marking them as N/A or "None" +4. **Use proper formatting**: Match the template's markdown structure exactly (headings, checkboxes, code blocks) + ## Development Commands - **Install**: `yarn install` - Install all project dependencies diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 545c34dc12..3ddc05ce85 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -[中文](docs/CONTRIBUTING.zh.md) | [English](CONTRIBUTING.md) +[中文](docs/zh/guides/contributing.md) | [English](CONTRIBUTING.md) # Cherry Studio Contributor Guide @@ -32,7 +32,7 @@ To help you get familiar with the codebase, we recommend tackling issues tagged ### Testing -Features without tests are considered non-existent. To ensure code is truly effective, relevant processes should be covered by unit tests and functional tests. Therefore, when considering contributions, please also consider testability. All tests can be run locally without dependency on CI. Please refer to the "Testing" section in the [Developer Guide](docs/dev.md). +Features without tests are considered non-existent. To ensure code is truly effective, relevant processes should be covered by unit tests and functional tests. Therefore, when considering contributions, please also consider testability. All tests can be run locally without dependency on CI. Please refer to the "Testing" section in the [Developer Guide](docs/zh/guides/development.md). ### Automated Testing for Pull Requests @@ -60,7 +60,7 @@ Maintainers are here to help you implement your use case within a reasonable tim ### Participating in the Test Plan -The Test Plan aims to provide users with a more stable application experience and faster iteration speed. For details, please refer to the [Test Plan](docs/testplan-en.md). +The Test Plan aims to provide users with a more stable application experience and faster iteration speed. For details, please refer to the [Test Plan](docs/en/guides/test-plan.md). ### Other Suggestions diff --git a/README.md b/README.md index 1223f73ed0..f790c10cbd 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ -

English | 中文 | Official Site | Documents | Development | Feedback

+

English | 中文 | Official Site | Documents | Development | Feedback

@@ -67,7 +67,7 @@ Cherry Studio is a desktop client that supports multiple LLM providers, availabl 👏 Join [Telegram Group](https://t.me/CherryStudioAI)|[Discord](https://discord.gg/wez8HtpxqQ) | [QQ Group(575014769)](https://qm.qq.com/q/lo0D4qVZKi) -❤️ Like Cherry Studio? Give it a star 🌟 or [Sponsor](docs/sponsor.md) to support the development! +❤️ Like Cherry Studio? Give it a star 🌟 or [Sponsor](docs/zh/guides/sponsor.md) to support the development! # 🌠 Screenshot @@ -175,7 +175,7 @@ We welcome contributions to Cherry Studio! Here are some ways you can contribute 6. **Community Engagement**: Join discussions and help users. 7. **Promote Usage**: Spread the word about Cherry Studio. -Refer to the [Branching Strategy](docs/branching-strategy-en.md) for contribution guidelines +Refer to the [Branching Strategy](docs/en/guides/branching-strategy.md) for contribution guidelines ## Getting Started diff --git a/biome.jsonc b/biome.jsonc index 9509135fc4..705b1e01f3 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -14,7 +14,7 @@ } }, "enabled": true, - "includes": ["**/*.json", "!*.json", "!**/package.json"] + "includes": ["**/*.json", "!*.json", "!**/package.json", "!coverage/**"] }, "css": { "formatter": { @@ -23,7 +23,7 @@ }, "files": { "ignoreUnknown": false, - "includes": ["**", "!**/.claude/**"], + "includes": ["**", "!**/.claude/**", "!**/.vscode/**"], "maxSize": 2097152 }, "formatter": { diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..bd5f055766 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,81 @@ +# Cherry Studio Documentation / 文档 + +This directory contains the project documentation in multiple languages. + +本目录包含多语言项目文档。 + +--- + +## Languages / 语言 + +- **[中文文档](./zh/README.md)** - Chinese Documentation +- **English Documentation** - See sections below + +--- + +## English Documentation + +### Guides + +| Document | Description | +|----------|-------------| +| [Development Setup](./en/guides/development.md) | Development environment setup | +| [Branching Strategy](./en/guides/branching-strategy.md) | Git branching workflow | +| [i18n Guide](./en/guides/i18n.md) | Internationalization guide | +| [Logging Guide](./en/guides/logging.md) | How to use the logger service | +| [Test Plan](./en/guides/test-plan.md) | Test plan and release channels | + +### References + +| Document | Description | +|----------|-------------| +| [App Upgrade Config](./en/references/app-upgrade.md) | Application upgrade configuration | +| [CodeBlockView Component](./en/references/components/code-block-view.md) | Code block view component | +| [Image Preview Components](./en/references/components/image-preview.md) | Image preview components | + +--- + +## 中文文档 + +### 指南 (Guides) + +| 文档 | 说明 | +|------|------| +| [开发环境设置](./zh/guides/development.md) | 开发环境配置 | +| [贡献指南](./zh/guides/contributing.md) | 如何贡献代码 | +| [分支策略](./zh/guides/branching-strategy.md) | Git 分支工作流 | +| [测试计划](./zh/guides/test-plan.md) | 测试计划和发布通道 | +| [国际化指南](./zh/guides/i18n.md) | 国际化开发指南 | +| [日志使用指南](./zh/guides/logging.md) | 如何使用日志服务 | +| [中间件开发](./zh/guides/middleware.md) | 如何编写中间件 | +| [记忆功能](./zh/guides/memory.md) | 记忆功能使用指南 | +| [赞助信息](./zh/guides/sponsor.md) | 赞助相关信息 | + +### 参考 (References) + +| 文档 | 说明 | +|------|------| +| [消息系统](./zh/references/message-system.md) | 消息系统架构和 API | +| [数据库结构](./zh/references/database.md) | 数据库表结构 | +| [服务](./zh/references/services.md) | 服务层文档 (KnowledgeService) | +| [代码执行](./zh/references/code-execution.md) | 代码执行功能 | +| [应用升级配置](./zh/references/app-upgrade.md) | 应用升级配置 | +| [CodeBlockView 组件](./zh/references/components/code-block-view.md) | 代码块视图组件 | +| [图像预览组件](./zh/references/components/image-preview.md) | 图像预览组件 | + +--- + +## Missing Translations / 缺少翻译 + +The following documents are only available in Chinese and need English translations: + +以下文档仅有中文版本,需要英文翻译: + +- `guides/contributing.md` +- `guides/memory.md` +- `guides/middleware.md` +- `guides/sponsor.md` +- `references/message-system.md` +- `references/database.md` +- `references/services.md` +- `references/code-execution.md` diff --git a/docs/technical/.assets.how-to-i18n/demo-1.png b/docs/assets/images/i18n/demo-1.png similarity index 100% rename from docs/technical/.assets.how-to-i18n/demo-1.png rename to docs/assets/images/i18n/demo-1.png diff --git a/docs/technical/.assets.how-to-i18n/demo-2.png b/docs/assets/images/i18n/demo-2.png similarity index 100% rename from docs/technical/.assets.how-to-i18n/demo-2.png rename to docs/assets/images/i18n/demo-2.png diff --git a/docs/technical/.assets.how-to-i18n/demo-3.png b/docs/assets/images/i18n/demo-3.png similarity index 100% rename from docs/technical/.assets.how-to-i18n/demo-3.png rename to docs/assets/images/i18n/demo-3.png diff --git a/docs/technical/message-lifecycle.png b/docs/assets/images/message-lifecycle.png similarity index 100% rename from docs/technical/message-lifecycle.png rename to docs/assets/images/message-lifecycle.png diff --git a/docs/branching-strategy-en.md b/docs/en/guides/branching-strategy.md similarity index 98% rename from docs/branching-strategy-en.md rename to docs/en/guides/branching-strategy.md index 8e646249ad..11eabeec73 100644 --- a/docs/branching-strategy-en.md +++ b/docs/en/guides/branching-strategy.md @@ -16,7 +16,7 @@ Cherry Studio implements a structured branching strategy to maintain code qualit - Only accepts documentation updates and bug fixes - Thoroughly tested before production deployment -For details about the `testplan` branch used in the Test Plan, please refer to the [Test Plan](testplan-en.md). +For details about the `testplan` branch used in the Test Plan, please refer to the [Test Plan](./test-plan.md). ## Contributing Branches diff --git a/docs/dev.md b/docs/en/guides/development.md similarity index 100% rename from docs/dev.md rename to docs/en/guides/development.md diff --git a/docs/technical/how-to-i18n-en.md b/docs/en/guides/i18n.md similarity index 97% rename from docs/technical/how-to-i18n-en.md rename to docs/en/guides/i18n.md index 1bbf7edca8..a3284e3ab9 100644 --- a/docs/technical/how-to-i18n-en.md +++ b/docs/en/guides/i18n.md @@ -18,11 +18,11 @@ The plugin has already been configured in the project — simply install it to g ### Demo -![demo-1](./.assets.how-to-i18n/demo-1.png) +![demo-1](../../assets/images/i18n/demo-1.png) -![demo-2](./.assets.how-to-i18n/demo-2.png) +![demo-2](../../assets/images/i18n/demo-2.png) -![demo-3](./.assets.how-to-i18n/demo-3.png) +![demo-3](../../assets/images/i18n/demo-3.png) ## i18n Conventions diff --git a/docs/technical/how-to-use-logger-en.md b/docs/en/guides/logging.md similarity index 100% rename from docs/technical/how-to-use-logger-en.md rename to docs/en/guides/logging.md diff --git a/docs/testplan-en.md b/docs/en/guides/test-plan.md similarity index 95% rename from docs/testplan-en.md rename to docs/en/guides/test-plan.md index fad894f22b..c7d0c4c660 100644 --- a/docs/testplan-en.md +++ b/docs/en/guides/test-plan.md @@ -19,7 +19,7 @@ Users are welcome to submit issues or provide feedback through other channels fo ### Participating in the Test Plan -Developers should submit `PRs` according to the [Contributor Guide](../CONTRIBUTING.md) (and ensure the target branch is `main`). The repository maintainers will evaluate whether the `PR` should be included in the Test Plan based on factors such as the impact of the feature on the application, its importance, and whether broader testing is needed. +Developers should submit `PRs` according to the [Contributor Guide](../../CONTRIBUTING.md) (and ensure the target branch is `main`). The repository maintainers will evaluate whether the `PR` should be included in the Test Plan based on factors such as the impact of the feature on the application, its importance, and whether broader testing is needed. If the `PR` is added to the Test Plan, the repository maintainers will: diff --git a/docs/technical/app-upgrade-config-en.md b/docs/en/references/app-upgrade.md similarity index 100% rename from docs/technical/app-upgrade-config-en.md rename to docs/en/references/app-upgrade.md diff --git a/docs/technical/CodeBlockView-en.md b/docs/en/references/components/code-block-view.md similarity index 98% rename from docs/technical/CodeBlockView-en.md rename to docs/en/references/components/code-block-view.md index 786d7aa029..22a3ea5a1f 100644 --- a/docs/technical/CodeBlockView-en.md +++ b/docs/en/references/components/code-block-view.md @@ -85,7 +85,7 @@ Main responsibilities: - **SvgPreview**: SVG image preview - **GraphvizPreview**: Graphviz diagram preview -All special view components share a common architecture for consistent user experience and functionality. For detailed information about these components and their implementation, see [Image Preview Components Documentation](./ImagePreview-en.md). +All special view components share a common architecture for consistent user experience and functionality. For detailed information about these components and their implementation, see [Image Preview Components Documentation](./image-preview.md). #### StatusBar diff --git a/docs/technical/ImagePreview-en.md b/docs/en/references/components/image-preview.md similarity index 98% rename from docs/technical/ImagePreview-en.md rename to docs/en/references/components/image-preview.md index 383bf5c664..8244f8fe9b 100644 --- a/docs/technical/ImagePreview-en.md +++ b/docs/en/references/components/image-preview.md @@ -192,4 +192,4 @@ Image Preview Components integrate seamlessly with CodeBlockView: - Shared state management - Responsive layout adaptation -For more information about the overall CodeBlockView architecture, see [CodeBlockView Documentation](./CodeBlockView-en.md). +For more information about the overall CodeBlockView architecture, see [CodeBlockView Documentation](./code-block-view.md). diff --git a/docs/technical/Message.md b/docs/technical/Message.md deleted file mode 100644 index 673b1cce7b..0000000000 --- a/docs/technical/Message.md +++ /dev/null @@ -1,3 +0,0 @@ -# 消息的生命周期 - -![image](./message-lifecycle.png) diff --git a/docs/technical/db.settings.md b/docs/technical/db.settings.md deleted file mode 100644 index 1d63098851..0000000000 --- a/docs/technical/db.settings.md +++ /dev/null @@ -1,11 +0,0 @@ -# 数据库设置字段 - -此文档包含部分字段的数据类型说明。 - -## 字段 - -| 字段名 | 类型 | 说明 | -| ------------------------------ | ------------------------------ | ------------ | -| `translate:target:language` | `LanguageCode` | 翻译目标语言 | -| `translate:source:language` | `LanguageCode` | 翻译源语言 | -| `translate:bidirectional:pair` | `[LanguageCode, LanguageCode]` | 双向翻译对 | diff --git a/docs/technical/how-to-use-messageBlock.md b/docs/technical/how-to-use-messageBlock.md deleted file mode 100644 index f60c2851ce..0000000000 --- a/docs/technical/how-to-use-messageBlock.md +++ /dev/null @@ -1,127 +0,0 @@ -# messageBlock.ts 使用指南 - -该文件定义了用于管理应用程序中所有 `MessageBlock` 实体的 Redux Slice。它使用 Redux Toolkit 的 `createSlice` 和 `createEntityAdapter` 来高效地处理规范化的状态,并提供了一系列 actions 和 selectors 用于与消息块数据交互。 - -## 核心目标 - -- **状态管理**: 集中管理所有 `MessageBlock` 的状态。`MessageBlock` 代表消息中的不同内容单元(如文本、代码、图片、引用等)。 -- **规范化**: 使用 `createEntityAdapter` 将 `MessageBlock` 数据存储在规范化的结构中(`{ ids: [], entities: {} }`),这有助于提高性能和简化更新逻辑。 -- **可预测性**: 提供明确的 actions 来修改状态,并通过 selectors 安全地访问状态。 - -## 关键概念 - -- **Slice (`createSlice`)**: Redux Toolkit 的核心 API,用于创建包含 reducer 逻辑、action creators 和初始状态的 Redux 模块。 -- **Entity Adapter (`createEntityAdapter`)**: Redux Toolkit 提供的工具,用于简化对规范化数据的 CRUD(创建、读取、更新、删除)操作。它会自动生成 reducer 函数和 selectors。 -- **Selectors**: 用于从 Redux store 中派生和计算数据的函数。Selectors 可以被记忆化(memoized),以提高性能。 - -## State 结构 - -`messageBlocks` slice 的状态结构由 `createEntityAdapter` 定义,大致如下: - -```typescript -{ - ids: string[]; // 存储所有 MessageBlock ID 的有序列表 - entities: { [id: string]: MessageBlock }; // 按 ID 存储 MessageBlock 对象的字典 - loadingState: 'idle' | 'loading' | 'succeeded' | 'failed'; // (可选) 其他状态,如加载状态 - error: string | null; // (可选) 错误信息 -} -``` - -## Actions - -该 slice 导出以下 actions (由 `createSlice` 和 `createEntityAdapter` 自动生成或自定义): - -- **`upsertOneBlock(payload: MessageBlock)`**: - - - 添加一个新的 `MessageBlock` 或更新一个已存在的 `MessageBlock`。如果 payload 中的 `id` 已存在,则执行更新;否则执行插入。 - -- **`upsertManyBlocks(payload: MessageBlock[])`**: - - - 添加或更新多个 `MessageBlock`。常用于批量加载数据(例如,加载一个 Topic 的所有消息块)。 - -- **`removeOneBlock(payload: string)`**: - - - 根据提供的 `id` (payload) 移除单个 `MessageBlock`。 - -- **`removeManyBlocks(payload: string[])`**: - - - 根据提供的 `id` 数组 (payload) 移除多个 `MessageBlock`。常用于删除消息或清空 Topic 时清理相关的块。 - -- **`removeAllBlocks()`**: - - - 移除 state 中的所有 `MessageBlock` 实体。 - -- **`updateOneBlock(payload: { id: string; changes: Partial })`**: - - - 更新一个已存在的 `MessageBlock`。`payload` 需要包含块的 `id` 和一个包含要更改的字段的 `changes` 对象。 - -- **`setMessageBlocksLoading(payload: 'idle' | 'loading')`**: - - - (自定义) 设置 `loadingState` 属性。 - -- **`setMessageBlocksError(payload: string)`**: - - (自定义) 设置 `loadingState` 为 `'failed'` 并记录错误信息。 - -**使用示例 (在 Thunk 或其他 Dispatch 的地方):** - -```typescript -import { upsertOneBlock, removeManyBlocks, updateOneBlock } from './messageBlock' -import store from './store' // 假设这是你的 Redux store 实例 - -// 添加或更新一个块 -const newBlock: MessageBlock = { - /* ... block data ... */ -} -store.dispatch(upsertOneBlock(newBlock)) - -// 更新一个块的内容 -store.dispatch(updateOneBlock({ id: blockId, changes: { content: 'New content' } })) - -// 删除多个块 -const blockIdsToRemove = ['id1', 'id2'] -store.dispatch(removeManyBlocks(blockIdsToRemove)) -``` - -## Selectors - -该 slice 导出由 `createEntityAdapter` 生成的基础 selectors,并通过 `messageBlocksSelectors` 对象访问: - -- **`messageBlocksSelectors.selectIds(state: RootState): string[]`**: 返回包含所有块 ID 的数组。 -- **`messageBlocksSelectors.selectEntities(state: RootState): { [id: string]: MessageBlock }`**: 返回块 ID 到块对象的映射字典。 -- **`messageBlocksSelectors.selectAll(state: RootState): MessageBlock[]`**: 返回包含所有块对象的数组。 -- **`messageBlocksSelectors.selectTotal(state: RootState): number`**: 返回块的总数。 -- **`messageBlocksSelectors.selectById(state: RootState, id: string): MessageBlock | undefined`**: 根据 ID 返回单个块对象,如果找不到则返回 `undefined`。 - -**此外,还提供了一个自定义的、记忆化的 selector:** - -- **`selectFormattedCitationsByBlockId(state: RootState, blockId: string | undefined): Citation[]`**: - - 接收一个 `blockId`。 - - 如果该 ID 对应的块是 `CITATION` 类型,则提取并格式化其包含的引用信息(来自网页搜索、知识库等),进行去重和重新编号,最后返回一个 `Citation[]` 数组,用于在 UI 中显示。 - - 如果块不存在或类型不匹配,返回空数组 `[]`。 - - 这个 selector 封装了处理不同引用来源(Gemini, OpenAI, OpenRouter, Zhipu 等)的复杂逻辑。 - -**使用示例 (在 React 组件或 `useSelector` 中):** - -```typescript -import { useSelector } from 'react-redux' -import { messageBlocksSelectors, selectFormattedCitationsByBlockId } from './messageBlock' -import type { RootState } from './store' - -// 获取所有块 -const allBlocks = useSelector(messageBlocksSelectors.selectAll) - -// 获取特定 ID 的块 -const specificBlock = useSelector((state: RootState) => messageBlocksSelectors.selectById(state, someBlockId)) - -// 获取特定引用块格式化后的引用列表 -const formattedCitations = useSelector((state: RootState) => selectFormattedCitationsByBlockId(state, citationBlockId)) - -// 在组件中使用引用数据 -// {formattedCitations.map(citation => ...)} -``` - -## 集成 - -`messageBlock.ts` slice 通常与 `messageThunk.ts` 中的 Thunks 紧密协作。Thunks 负责处理异步逻辑(如 API 调用、数据库操作),并在需要时 dispatch `messageBlock` slice 的 actions 来更新状态。例如,当 `messageThunk` 接收到流式响应时,它会 dispatch `upsertOneBlock` 或 `updateOneBlock` 来实时更新对应的 `MessageBlock`。同样,删除消息的 Thunk 会 dispatch `removeManyBlocks`。 - -理解 `messageBlock.ts` 的职责是管理**状态本身**,而 `messageThunk.ts` 负责**触发状态变更**的异步流程,这对于维护清晰的应用架构至关重要。 diff --git a/docs/technical/how-to-use-messageThunk.md b/docs/technical/how-to-use-messageThunk.md deleted file mode 100644 index 86952f99ad..0000000000 --- a/docs/technical/how-to-use-messageThunk.md +++ /dev/null @@ -1,105 +0,0 @@ -# messageThunk.ts 使用指南 - -该文件包含用于管理应用程序中消息流、处理助手交互以及同步 Redux 状态与 IndexedDB 数据库的核心 Thunk Action Creators。主要围绕 `Message` 和 `MessageBlock` 对象进行操作。 - -## 核心功能 - -1. **发送/接收消息**: 处理用户消息的发送,触发助手响应,并流式处理返回的数据,将其解析为不同的 `MessageBlock`。 -2. **状态管理**: 确保 Redux store 中的消息和消息块状态与 IndexedDB 中的持久化数据保持一致。 -3. **消息操作**: 提供删除、重发、重新生成、编辑后重发、追加响应、克隆等消息生命周期管理功能。 -4. **Block 处理**: 动态创建、更新和保存各种类型的 `MessageBlock`(文本、思考过程、工具调用、引用、图片、错误、翻译等)。 - -## 主要 Thunks - -以下是一些关键的 Thunk 函数及其用途: - -1. **`sendMessage(userMessage, userMessageBlocks, assistant, topicId)`** - - - **用途**: 发送一条新的用户消息。 - - **流程**: - - 保存用户消息 (`userMessage`) 及其块 (`userMessageBlocks`) 到 Redux 和 DB。 - - 检查 `@mentions` 以确定是单模型响应还是多模型响应。 - - 创建助手消息(们)的存根 (Stub)。 - - 将存根添加到 Redux 和 DB。 - - 将核心处理逻辑 `fetchAndProcessAssistantResponseImpl` 添加到该 `topicId` 的队列中以获取实际响应。 - - **Block 相关**: 主要处理用户消息的初始 `MessageBlock` 保存。 - -2. **`fetchAndProcessAssistantResponseImpl(dispatch, getState, topicId, assistant, assistantMessage)`** - - - **用途**: (内部函数) 获取并处理单个助手响应的核心逻辑,被 `sendMessage`, `resend...`, `regenerate...`, `append...` 等调用。 - - **流程**: - - 设置 Topic 加载状态。 - - 准备上下文消息。 - - 调用 `fetchChatCompletion` API 服务。 - - 使用 `createStreamProcessor` 处理流式响应。 - - 通过各种回调 (`onTextChunk`, `onThinkingChunk`, `onToolCallComplete`, `onImageGenerated`, `onError`, `onComplete` 等) 处理不同类型的事件。 - - **Block 相关**: - - 根据流事件创建初始 `UNKNOWN` 块。 - - 实时创建和更新 `MAIN_TEXT` 和 `THINKING` 块,使用 `throttledBlockUpdate` 和 `throttledBlockDbUpdate` 进行节流更新。 - - 创建 `TOOL`, `CITATION`, `IMAGE`, `ERROR` 等类型的块。 - - 在事件完成时(如 `onTextComplete`, `onToolCallComplete`)将块状态标记为 `SUCCESS` 或 `ERROR`,并使用 `saveUpdatedBlockToDB` 保存最终状态。 - - 使用 `handleBlockTransition` 管理非流式块(如 `TOOL`, `CITATION`)的添加和状态更新。 - -3. **`loadTopicMessagesThunk(topicId, forceReload)`** - - - **用途**: 从数据库加载指定主题的所有消息及其关联的 `MessageBlock`。 - - **流程**: - - 从 DB 获取 `Topic` 及其 `messages` 列表。 - - 根据消息 ID 列表从 DB 获取所有相关的 `MessageBlock`。 - - 使用 `upsertManyBlocks` 将块更新到 Redux。 - - 将消息更新到 Redux。 - - **Block 相关**: 负责将持久化的 `MessageBlock` 加载到 Redux 状态。 - -4. **删除 Thunks** - - - `deleteSingleMessageThunk(topicId, messageId)`: 删除单个消息及其所有 `MessageBlock`。 - - `deleteMessageGroupThunk(topicId, askId)`: 删除一个用户消息及其所有相关的助手响应消息和它们的所有 `MessageBlock`。 - - `clearTopicMessagesThunk(topicId)`: 清空主题下的所有消息及其所有 `MessageBlock`。 - - **Block 相关**: 从 Redux 和 DB 中移除指定的 `MessageBlock`。 - -5. **重发/重新生成 Thunks** - - - `resendMessageThunk(topicId, userMessageToResend, assistant)`: 重发用户消息。会重置(清空 Block 并标记为 PENDING)所有与该用户消息关联的助手响应,然后重新请求生成。 - - `resendUserMessageWithEditThunk(topicId, originalMessage, mainTextBlockId, editedContent, assistant)`: 用户编辑消息内容后重发。先更新用户消息的 `MAIN_TEXT` 块内容,然后调用 `resendMessageThunk`。 - - `regenerateAssistantResponseThunk(topicId, assistantMessageToRegenerate, assistant)`: 重新生成单个助手响应。重置该助手消息(清空 Block 并标记为 PENDING),然后重新请求生成。 - - **Block 相关**: 删除旧的 `MessageBlock`,并在重新生成过程中创建新的 `MessageBlock`。 - -6. **`appendAssistantResponseThunk(topicId, existingAssistantMessageId, newModel, assistant)`** - - - **用途**: 在已有的对话上下文中,针对同一个用户问题,使用新选择的模型追加一个新的助手响应。 - - **流程**: - - 找到现有助手消息以获取原始 `askId`。 - - 创建使用 `newModel` 的新助手消息存根(使用相同的 `askId`)。 - - 添加新存根到 Redux 和 DB。 - - 将 `fetchAndProcessAssistantResponseImpl` 添加到队列以生成新响应。 - - **Block 相关**: 为新的助手响应创建全新的 `MessageBlock`。 - -7. **`cloneMessagesToNewTopicThunk(sourceTopicId, branchPointIndex, newTopic)`** - - - **用途**: 将源主题的部分消息(及其 Block)克隆到一个**已存在**的新主题中。 - - **流程**: - - 复制指定索引前的消息。 - - 为所有克隆的消息和 Block 生成新的 UUID。 - - 正确映射克隆消息之间的 `askId` 关系。 - - 复制 `MessageBlock` 内容,更新其 `messageId` 指向新的消息 ID。 - - 更新文件引用计数(如果 Block 是文件或图片)。 - - 将克隆的消息和 Block 保存到新主题的 Redux 状态和 DB 中。 - - **Block 相关**: 创建 `MessageBlock` 的副本,并更新其 ID 和 `messageId`。 - -8. **`initiateTranslationThunk(messageId, topicId, targetLanguage, sourceBlockId?, sourceLanguage?)`** - - **用途**: 为指定消息启动翻译流程,创建一个初始的 `TRANSLATION` 类型的 `MessageBlock`。 - - **流程**: - - 创建一个状态为 `STREAMING` 的 `TranslationMessageBlock`。 - - 将其添加到 Redux 和 DB。 - - 更新原消息的 `blocks` 列表以包含新的翻译块 ID。 - - **Block 相关**: 创建并保存一个占位的 `TranslationMessageBlock`。实际翻译内容的获取和填充需要后续步骤。 - -## 内部机制和注意事项 - -- **数据库交互**: 通过 `saveMessageAndBlocksToDB`, `updateExistingMessageAndBlocksInDB`, `saveUpdatesToDB`, `saveUpdatedBlockToDB`, `throttledBlockDbUpdate` 等辅助函数与 IndexedDB (`db`) 交互,确保数据持久化。 -- **状态同步**: Thunks 负责协调 Redux Store 和 IndexedDB 之间的数据一致性。 -- **队列 (`getTopicQueue`)**: 使用 `AsyncQueue` 确保对同一主题的操作(尤其是 API 请求)按顺序执行,避免竞态条件。 -- **节流 (`throttle`)**: 对流式响应中频繁的 Block 更新(文本、思考)使用 `lodash.throttle` 优化性能,减少 Redux dispatch 和 DB 写入次数。 -- **错误处理**: `fetchAndProcessAssistantResponseImpl` 内的回调函数(特别是 `onError`)处理流处理和 API 调用中可能出现的错误,并创建 `ERROR` 类型的 `MessageBlock`。 - -开发者在使用这些 Thunks 时,通常需要提供 `dispatch`, `getState` (由 Redux Thunk 中间件注入),以及如 `topicId`, `assistant` 配置对象, 相关的 `Message` 或 `MessageBlock` 对象/ID 等参数。理解每个 Thunk 的职责和它如何影响消息及块的状态至关重要。 diff --git a/docs/technical/how-to-use-useMessageOperations.md b/docs/technical/how-to-use-useMessageOperations.md deleted file mode 100644 index df56ad5e5f..0000000000 --- a/docs/technical/how-to-use-useMessageOperations.md +++ /dev/null @@ -1,156 +0,0 @@ -# useMessageOperations.ts 使用指南 - -该文件定义了一个名为 `useMessageOperations` 的自定义 React Hook。这个 Hook 的主要目的是为 React 组件提供一个便捷的接口,用于执行与特定主题(Topic)相关的各种消息操作。它封装了调用 Redux Thunks (`messageThunk.ts`) 和 Actions (`newMessage.ts`, `messageBlock.ts`) 的逻辑,简化了组件与消息数据交互的代码。 - -## 核心目标 - -- **封装**: 将复杂的消息操作逻辑(如删除、重发、重新生成、编辑、翻译等)封装在易于使用的函数中。 -- **简化**: 让组件可以直接调用这些操作函数,而无需直接与 Redux `dispatch` 或 Thunks 交互。 -- **上下文关联**: 所有操作都与传入的 `topic` 对象相关联,确保操作作用于正确的主题。 - -## 如何使用 - -在你的 React 函数组件中,导入并调用 `useMessageOperations` Hook,并传入当前活动的 `Topic` 对象。 - -```typescript -import React from 'react'; -import { useMessageOperations } from '@renderer/hooks/useMessageOperations'; -import type { Topic, Message, Assistant, Model } from '@renderer/types'; - -interface MyComponentProps { - currentTopic: Topic; - currentAssistant: Assistant; -} - -function MyComponent({ currentTopic, currentAssistant }: MyComponentProps) { - const { - deleteMessage, - resendMessage, - regenerateAssistantMessage, - appendAssistantResponse, - getTranslationUpdater, - createTopicBranch, - // ... 其他操作函数 - } = useMessageOperations(currentTopic); - - const handleDelete = (messageId: string) => { - deleteMessage(messageId); - }; - - const handleResend = (message: Message) => { - resendMessage(message, currentAssistant); - }; - - const handleAppend = (existingMsg: Message, newModel: Model) => { - appendAssistantResponse(existingMsg, newModel, currentAssistant); - } - - // ... 在组件中使用其他操作函数 - - return ( -
- {/* Component UI */} - - {/* ... */} -
- ); -} -``` - -## 返回值 - -`useMessageOperations(topic)` Hook 返回一个包含以下函数和值的对象: - -- **`deleteMessage(id: string)`**: - - - 删除指定 `id` 的单个消息。 - - 内部调用 `deleteSingleMessageThunk`。 - -- **`deleteGroupMessages(askId: string)`**: - - - 删除与指定 `askId` 相关联的一组消息(通常是用户提问及其所有助手回答)。 - - 内部调用 `deleteMessageGroupThunk`。 - -- **`editMessage(messageId: string, updates: Partial)`**: - - - 更新指定 `messageId` 的消息的部分属性。 - - **注意**: 目前主要用于更新 Redux 状态 - - 内部调用 `newMessagesActions.updateMessage`。 - -- **`resendMessage(message: Message, assistant: Assistant)`**: - - - 重新发送指定的用户消息 (`message`),这将触发其所有关联助手响应的重新生成。 - - 内部调用 `resendMessageThunk`。 - -- **`resendUserMessageWithEdit(message: Message, editedContent: string, assistant: Assistant)`**: - - - 在用户消息的主要文本块被编辑后,重新发送该消息。 - - 会先查找消息的 `MAIN_TEXT` 块 ID,然后调用 `resendUserMessageWithEditThunk`。 - -- **`clearTopicMessages(_topicId?: string)`**: - - - 清除当前主题(或可选的指定 `_topicId`)下的所有消息。 - - 内部调用 `clearTopicMessagesThunk`。 - -- **`createNewContext()`**: - - - 发出一个全局事件 (`EVENT_NAMES.NEW_CONTEXT`),通常用于通知 UI 清空显示,准备新的上下文。不直接修改 Redux 状态。 - -- **`displayCount`**: - - - (非操作函数) 从 Redux store 中获取当前的 `displayCount` 值。 - -- **`pauseMessages()`**: - - - 尝试中止当前主题中正在进行的消息生成(状态为 `processing` 或 `pending`)。 - - 通过查找相关的 `askId` 并调用 `abortCompletion` 来实现。 - - 同时会 dispatch `setTopicLoading` action 将加载状态设为 `false`。 - -- **`resumeMessage(message: Message, assistant: Assistant)`**: - - - 恢复/重新发送一个用户消息。目前实现为直接调用 `resendMessage`。 - -- **`regenerateAssistantMessage(message: Message, assistant: Assistant)`**: - - - 重新生成指定的**助手**消息 (`message`) 的响应。 - - 内部调用 `regenerateAssistantResponseThunk`。 - -- **`appendAssistantResponse(existingAssistantMessage: Message, newModel: Model, assistant: Assistant)`**: - - - 针对 `existingAssistantMessage` 所回复的**同一用户提问**,使用 `newModel` 追加一个新的助手响应。 - - 内部调用 `appendAssistantResponseThunk`。 - -- **`getTranslationUpdater(messageId: string, targetLanguage: string, sourceBlockId?: string, sourceLanguage?: string)`**: - - - **用途**: 获取一个用于逐步更新翻译块内容的函数。 - - **流程**: - 1. 内部调用 `initiateTranslationThunk` 来创建或获取一个 `TRANSLATION` 类型的 `MessageBlock`,并获取其 `blockId`。 - 2. 返回一个**异步更新函数**。 - - **返回的更新函数 `(accumulatedText: string, isComplete?: boolean) => void`**: - - 接收累积的翻译文本和完成状态。 - - 调用 `updateOneBlock` 更新 Redux 中的翻译块内容和状态 (`STREAMING` 或 `SUCCESS`)。 - - 调用 `throttledBlockDbUpdate` 将更新(节流地)保存到数据库。 - - 如果初始化失败(Thunk 返回 `undefined`),则此函数返回 `null`。 - -- **`createTopicBranch(sourceTopicId: string, branchPointIndex: number, newTopic: Topic)`**: - - 创建一个主题分支,将 `sourceTopicId` 主题中 `branchPointIndex` 索引之前的消息克隆到 `newTopic` 中。 - - **注意**: `newTopic` 对象必须是调用此函数**之前**已经创建并添加到 Redux 和数据库中的。 - - 内部调用 `cloneMessagesToNewTopicThunk`。 - -## 依赖 - -- **`topic: Topic`**: 必须传入当前操作上下文的主题对象。Hook 返回的操作函数将始终作用于这个主题的 `topic.id`。 -- **Redux `dispatch`**: Hook 内部使用 `useAppDispatch` 获取 `dispatch` 函数来调用 actions 和 thunks。 - -## 相关 Hooks - -在同一文件中还定义了两个辅助 Hook: - -- **`useTopicMessages(topic: Topic)`**: - - - 使用 `selectMessagesForTopic` selector 来获取并返回指定主题的消息列表。 - -- **`useTopicLoading(topic: Topic)`**: - - 使用 `selectNewTopicLoading` selector 来获取并返回指定主题的加载状态。 - -这些 Hook 可以与 `useMessageOperations` 结合使用,方便地在组件中获取消息数据、加载状态,并执行相关操作。 diff --git a/docs/README.zh.md b/docs/zh/README.md similarity index 97% rename from docs/README.zh.md rename to docs/zh/README.md index 84546c57ee..f8a1f1ab8c 100644 --- a/docs/README.zh.md +++ b/docs/zh/README.md @@ -34,7 +34,7 @@

- English | 中文 | 官方网站 | 文档 | 开发 | 反馈
+ English | 中文 | 官方网站 | 文档 | 开发 | 反馈

@@ -70,7 +70,7 @@ Cherry Studio 是一款支持多个大语言模型(LLM)服务商的桌面客 👏 欢迎加入 [Telegram 群组](https://t.me/CherryStudioAI)|[Discord](https://discord.gg/wez8HtpxqQ) | [QQ群(575014769)](https://qm.qq.com/q/lo0D4qVZKi) -❤️ 喜欢 Cherry Studio? 点亮小星星 🌟 或 [赞助开发者](sponsor.md)! ❤️ +❤️ 喜欢 Cherry Studio? 点亮小星星 🌟 或 [赞助开发者](./guides/sponsor.md)! ❤️ # 📖 使用教程 @@ -181,7 +181,7 @@ https://docs.cherry-ai.com 6. **社区参与**:加入讨论并帮助用户 7. **推广使用**:宣传 Cherry Studio -参考[分支策略](branching-strategy-zh.md)了解贡献指南 +参考[分支策略](./guides/branching-strategy.md)了解贡献指南 ## 入门 @@ -190,7 +190,7 @@ https://docs.cherry-ai.com 3. **提交更改**:提交并推送您的更改 4. **打开 Pull Request**:描述您的更改和原因 -有关更详细的指南,请参阅我们的 [贡献指南](CONTRIBUTING.zh.md) +有关更详细的指南,请参阅我们的 [贡献指南](./guides/contributing.md) 感谢您的支持和贡献! diff --git a/docs/branching-strategy-zh.md b/docs/zh/guides/branching-strategy.md similarity index 98% rename from docs/branching-strategy-zh.md rename to docs/zh/guides/branching-strategy.md index 36b7ca263d..c6ab0eb0b5 100644 --- a/docs/branching-strategy-zh.md +++ b/docs/zh/guides/branching-strategy.md @@ -16,7 +16,7 @@ Cherry Studio 采用结构化的分支策略来维护代码质量并简化开发 - 只接受文档更新和 bug 修复 - 经过完整测试后可以发布到生产环境 -关于测试计划所使用的`testplan`分支,请查阅[测试计划](testplan-zh.md)。 +关于测试计划所使用的`testplan`分支,请查阅[测试计划](./test-plan.md)。 ## 贡献分支 diff --git a/docs/CONTRIBUTING.zh.md b/docs/zh/guides/contributing.md similarity index 94% rename from docs/CONTRIBUTING.zh.md rename to docs/zh/guides/contributing.md index 98efcc286e..dcea60cfbc 100644 --- a/docs/CONTRIBUTING.zh.md +++ b/docs/zh/guides/contributing.md @@ -1,6 +1,6 @@ # Cherry Studio 贡献者指南 -[**English**](../CONTRIBUTING.md) | [**中文**](CONTRIBUTING.zh.md) +[**English**](../../../CONTRIBUTING.md) | **中文** 欢迎来到 Cherry Studio 的贡献者社区!我们致力于将 Cherry Studio 打造成一个长期提供价值的项目,并希望邀请更多的开发者加入我们的行列。无论您是经验丰富的开发者还是刚刚起步的初学者,您的贡献都将帮助我们更好地服务用户,提升软件质量。 @@ -24,7 +24,7 @@ ## 开始之前 -请确保阅读了[行为准则](../CODE_OF_CONDUCT.md)和[LICENSE](../LICENSE)。 +请确保阅读了[行为准则](../../../CODE_OF_CONDUCT.md)和[LICENSE](../../../LICENSE)。 ## 开始贡献 @@ -32,7 +32,7 @@ ### 测试 -未经测试的功能等同于不存在。为确保代码真正有效,应通过单元测试和功能测试覆盖相关流程。因此,在考虑贡献时,也请考虑可测试性。所有测试均可本地运行,无需依赖 CI。请参阅[开发者指南](dev.md#test)中的“Test”部分。 +未经测试的功能等同于不存在。为确保代码真正有效,应通过单元测试和功能测试覆盖相关流程。因此,在考虑贡献时,也请考虑可测试性。所有测试均可本地运行,无需依赖 CI。请参阅[开发者指南](./development.md#test)中的"Test"部分。 ### 拉取请求的自动化测试 @@ -60,11 +60,11 @@ git commit --signoff -m "Your commit message" ### 获取代码审查/合并 -维护者在此帮助您在合理时间内实现您的用例。他们会尽力在合理时间内审查您的代码并提供建设性反馈。但如果您在审查过程中受阻,或认为您的 Pull Request 未得到应有的关注,请通过 Issue 中的评论或者[社群](README.zh.md#-community)联系我们 +维护者在此帮助您在合理时间内实现您的用例。他们会尽力在合理时间内审查您的代码并提供建设性反馈。但如果您在审查过程中受阻,或认为您的 Pull Request 未得到应有的关注,请通过 Issue 中的评论或者[社群](../README.md#-community)联系我们 ### 参与测试计划 -测试计划旨在为用户提供更稳定的应用体验和更快的迭代速度,详细情况请参阅[测试计划](testplan-zh.md)。 +测试计划旨在为用户提供更稳定的应用体验和更快的迭代速度,详细情况请参阅[测试计划](./test-plan.md)。 ### 其他建议 diff --git a/docs/zh/guides/development.md b/docs/zh/guides/development.md new file mode 100644 index 0000000000..fe67742768 --- /dev/null +++ b/docs/zh/guides/development.md @@ -0,0 +1,73 @@ +# 🖥️ Develop + +## IDE Setup + +- Editor: [Cursor](https://www.cursor.com/), etc. Any VS Code compatible editor. +- Linter: [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) +- Formatter: [Biome](https://marketplace.visualstudio.com/items?itemName=biomejs.biome) + +## Project Setup + +### Install + +```bash +yarn +``` + +### Development + +### Setup Node.js + +Download and install [Node.js v22.x.x](https://nodejs.org/en/download) + +### Setup Yarn + +```bash +corepack enable +corepack prepare yarn@4.9.1 --activate +``` + +### Install Dependencies + +```bash +yarn install +``` + +### ENV + +```bash +copy .env.example .env +``` + +### Start + +```bash +yarn dev +``` + +### Debug + +```bash +yarn debug +``` + +Then input chrome://inspect in browser + +### Test + +```bash +yarn test +``` + +### Build + +```bash +# For windows +$ yarn build:win + +# For macOS +$ yarn build:mac + +# For Linux +$ yarn build:linux +``` diff --git a/docs/technical/how-to-i18n-zh.md b/docs/zh/guides/i18n.md similarity index 97% rename from docs/technical/how-to-i18n-zh.md rename to docs/zh/guides/i18n.md index 5d0a93c369..82624d35c8 100644 --- a/docs/technical/how-to-i18n-zh.md +++ b/docs/zh/guides/i18n.md @@ -15,11 +15,11 @@ i18n ally是一个强大的VSCode插件,它能在开发阶段提供实时反 ### 效果展示 -![demo-1](./.assets.how-to-i18n/demo-1.png) +![demo-1](../../assets/images/i18n/demo-1.png) -![demo-2](./.assets.how-to-i18n/demo-2.png) +![demo-2](../../assets/images/i18n/demo-2.png) -![demo-3](./.assets.how-to-i18n/demo-3.png) +![demo-3](../../assets/images/i18n/demo-3.png) ## i18n 约定 diff --git a/docs/technical/how-to-use-logger-zh.md b/docs/zh/guides/logging.md similarity index 100% rename from docs/technical/how-to-use-logger-zh.md rename to docs/zh/guides/logging.md diff --git a/docs/features/memory-guide-zh.md b/docs/zh/guides/memory.md similarity index 100% rename from docs/features/memory-guide-zh.md rename to docs/zh/guides/memory.md diff --git a/docs/technical/how-to-write-middlewares.md b/docs/zh/guides/middleware.md similarity index 100% rename from docs/technical/how-to-write-middlewares.md rename to docs/zh/guides/middleware.md diff --git a/docs/sponsor.md b/docs/zh/guides/sponsor.md similarity index 100% rename from docs/sponsor.md rename to docs/zh/guides/sponsor.md diff --git a/docs/testplan-zh.md b/docs/zh/guides/test-plan.md similarity index 93% rename from docs/testplan-zh.md rename to docs/zh/guides/test-plan.md index 77d25981de..42147e8990 100644 --- a/docs/testplan-zh.md +++ b/docs/zh/guides/test-plan.md @@ -19,7 +19,7 @@ ### 参与测试计划 -开发者按照[贡献者指南](CONTRIBUTING.zh.md)要求正常提交`PR`(并注意提交target为`main`)。仓库维护者会综合考虑(例如该功能对应用的影响程度,功能的重要性,是否需要更广泛的测试等),决定该`PR`是否应加入测试计划。 +开发者按照[贡献者指南](./contributing.md)要求正常提交`PR`(并注意提交target为`main`)。仓库维护者会综合考虑(例如该功能对应用的影响程度,功能的重要性,是否需要更广泛的测试等),决定该`PR`是否应加入测试计划。 若该`PR`加入测试计划,仓库维护者会做如下操作: diff --git a/docs/technical/app-upgrade-config-zh.md b/docs/zh/references/app-upgrade.md similarity index 100% rename from docs/technical/app-upgrade-config-zh.md rename to docs/zh/references/app-upgrade.md diff --git a/docs/technical/code-execution.md b/docs/zh/references/code-execution.md similarity index 100% rename from docs/technical/code-execution.md rename to docs/zh/references/code-execution.md diff --git a/docs/technical/CodeBlockView-zh.md b/docs/zh/references/components/code-block-view.md similarity index 98% rename from docs/technical/CodeBlockView-zh.md rename to docs/zh/references/components/code-block-view.md index a817e99361..6805aac7a9 100644 --- a/docs/technical/CodeBlockView-zh.md +++ b/docs/zh/references/components/code-block-view.md @@ -85,7 +85,7 @@ graph TD - **SvgPreview**: SVG 图像预览 - **GraphvizPreview**: Graphviz 图表预览 -所有特殊视图组件共享通用架构,以确保一致的用户体验和功能。有关这些组件及其实现的详细信息,请参阅 [图像预览组件文档](./ImagePreview-zh.md)。 +所有特殊视图组件共享通用架构,以确保一致的用户体验和功能。有关这些组件及其实现的详细信息,请参阅[图像预览组件文档](./image-preview.md)。 #### StatusBar 状态栏 diff --git a/docs/technical/ImagePreview-zh.md b/docs/zh/references/components/image-preview.md similarity index 99% rename from docs/technical/ImagePreview-zh.md rename to docs/zh/references/components/image-preview.md index 8a68b84312..99c51cb995 100644 --- a/docs/technical/ImagePreview-zh.md +++ b/docs/zh/references/components/image-preview.md @@ -192,4 +192,4 @@ const { containerRef, error, isLoading, triggerRender, cancelRender, clearError, - 共享状态管理 - 响应式布局适应 -有关整体 CodeBlockView 架构的更多信息,请参阅 [CodeBlockView 文档](./CodeBlockView-zh.md)。 +有关整体 CodeBlockView 架构的更多信息,请参阅 [CodeBlockView 文档](./code-block-view.md)。 diff --git a/docs/technical/db.translate_languages.md b/docs/zh/references/database.md similarity index 57% rename from docs/technical/db.translate_languages.md rename to docs/zh/references/database.md index 37231c89cd..9fd72d0286 100644 --- a/docs/technical/db.translate_languages.md +++ b/docs/zh/references/database.md @@ -1,6 +1,24 @@ -# `translate_languages` 表技术文档 +# 数据库参考文档 -## 📄 概述 +本文档介绍 Cherry Studio 的数据库结构,包括设置字段和翻译语言表。 + +--- + +## 设置字段 (settings) + +此部分包含设置相关字段的数据类型说明。 + +### 翻译相关字段 + +| 字段名 | 类型 | 说明 | +| ------------------------------ | ------------------------------ | ------------ | +| `translate:target:language` | `LanguageCode` | 翻译目标语言 | +| `translate:source:language` | `LanguageCode` | 翻译源语言 | +| `translate:bidirectional:pair` | `[LanguageCode, LanguageCode]` | 双向翻译对 | + +--- + +## 翻译语言表 (translate_languages) `translate_languages` 记录用户自定义的的语言类型(`Language`)。 diff --git a/docs/zh/references/message-system.md b/docs/zh/references/message-system.md new file mode 100644 index 0000000000..91eb2fd82f --- /dev/null +++ b/docs/zh/references/message-system.md @@ -0,0 +1,404 @@ +# 消息系统 + +本文档介绍 Cherry Studio 的消息系统架构,包括消息生命周期、状态管理和操作接口。 + +## 消息的生命周期 + +![消息生命周期](../../assets/images/message-lifecycle.png) + +--- + +# messageBlock.ts 使用指南 + +该文件定义了用于管理应用程序中所有 `MessageBlock` 实体的 Redux Slice。它使用 Redux Toolkit 的 `createSlice` 和 `createEntityAdapter` 来高效地处理规范化的状态,并提供了一系列 actions 和 selectors 用于与消息块数据交互。 + +## 核心目标 + +- **状态管理**: 集中管理所有 `MessageBlock` 的状态。`MessageBlock` 代表消息中的不同内容单元(如文本、代码、图片、引用等)。 +- **规范化**: 使用 `createEntityAdapter` 将 `MessageBlock` 数据存储在规范化的结构中(`{ ids: [], entities: {} }`),这有助于提高性能和简化更新逻辑。 +- **可预测性**: 提供明确的 actions 来修改状态,并通过 selectors 安全地访问状态。 + +## 关键概念 + +- **Slice (`createSlice`)**: Redux Toolkit 的核心 API,用于创建包含 reducer 逻辑、action creators 和初始状态的 Redux 模块。 +- **Entity Adapter (`createEntityAdapter`)**: Redux Toolkit 提供的工具,用于简化对规范化数据的 CRUD(创建、读取、更新、删除)操作。它会自动生成 reducer 函数和 selectors。 +- **Selectors**: 用于从 Redux store 中派生和计算数据的函数。Selectors 可以被记忆化(memoized),以提高性能。 + +## State 结构 + +`messageBlocks` slice 的状态结构由 `createEntityAdapter` 定义,大致如下: + +```typescript +{ + ids: string[]; // 存储所有 MessageBlock ID 的有序列表 + entities: { [id: string]: MessageBlock }; // 按 ID 存储 MessageBlock 对象的字典 + loadingState: 'idle' | 'loading' | 'succeeded' | 'failed'; // (可选) 其他状态,如加载状态 + error: string | null; // (可选) 错误信息 +} +``` + +## Actions + +该 slice 导出以下 actions (由 `createSlice` 和 `createEntityAdapter` 自动生成或自定义): + +- **`upsertOneBlock(payload: MessageBlock)`**: + + - 添加一个新的 `MessageBlock` 或更新一个已存在的 `MessageBlock`。如果 payload 中的 `id` 已存在,则执行更新;否则执行插入。 + +- **`upsertManyBlocks(payload: MessageBlock[])`**: + + - 添加或更新多个 `MessageBlock`。常用于批量加载数据(例如,加载一个 Topic 的所有消息块)。 + +- **`removeOneBlock(payload: string)`**: + + - 根据提供的 `id` (payload) 移除单个 `MessageBlock`。 + +- **`removeManyBlocks(payload: string[])`**: + + - 根据提供的 `id` 数组 (payload) 移除多个 `MessageBlock`。常用于删除消息或清空 Topic 时清理相关的块。 + +- **`removeAllBlocks()`**: + + - 移除 state 中的所有 `MessageBlock` 实体。 + +- **`updateOneBlock(payload: { id: string; changes: Partial })`**: + + - 更新一个已存在的 `MessageBlock`。`payload` 需要包含块的 `id` 和一个包含要更改的字段的 `changes` 对象。 + +- **`setMessageBlocksLoading(payload: 'idle' | 'loading')`**: + + - (自定义) 设置 `loadingState` 属性。 + +- **`setMessageBlocksError(payload: string)`**: + - (自定义) 设置 `loadingState` 为 `'failed'` 并记录错误信息。 + +**使用示例 (在 Thunk 或其他 Dispatch 的地方):** + +```typescript +import { upsertOneBlock, removeManyBlocks, updateOneBlock } from './messageBlock' +import store from './store' // 假设这是你的 Redux store 实例 + +// 添加或更新一个块 +const newBlock: MessageBlock = { + /* ... block data ... */ +} +store.dispatch(upsertOneBlock(newBlock)) + +// 更新一个块的内容 +store.dispatch(updateOneBlock({ id: blockId, changes: { content: 'New content' } })) + +// 删除多个块 +const blockIdsToRemove = ['id1', 'id2'] +store.dispatch(removeManyBlocks(blockIdsToRemove)) +``` + +## Selectors + +该 slice 导出由 `createEntityAdapter` 生成的基础 selectors,并通过 `messageBlocksSelectors` 对象访问: + +- **`messageBlocksSelectors.selectIds(state: RootState): string[]`**: 返回包含所有块 ID 的数组。 +- **`messageBlocksSelectors.selectEntities(state: RootState): { [id: string]: MessageBlock }`**: 返回块 ID 到块对象的映射字典。 +- **`messageBlocksSelectors.selectAll(state: RootState): MessageBlock[]`**: 返回包含所有块对象的数组。 +- **`messageBlocksSelectors.selectTotal(state: RootState): number`**: 返回块的总数。 +- **`messageBlocksSelectors.selectById(state: RootState, id: string): MessageBlock | undefined`**: 根据 ID 返回单个块对象,如果找不到则返回 `undefined`。 + +**此外,还提供了一个自定义的、记忆化的 selector:** + +- **`selectFormattedCitationsByBlockId(state: RootState, blockId: string | undefined): Citation[]`**: + - 接收一个 `blockId`。 + - 如果该 ID 对应的块是 `CITATION` 类型,则提取并格式化其包含的引用信息(来自网页搜索、知识库等),进行去重和重新编号,最后返回一个 `Citation[]` 数组,用于在 UI 中显示。 + - 如果块不存在或类型不匹配,返回空数组 `[]`。 + - 这个 selector 封装了处理不同引用来源(Gemini, OpenAI, OpenRouter, Zhipu 等)的复杂逻辑。 + +**使用示例 (在 React 组件或 `useSelector` 中):** + +```typescript +import { useSelector } from 'react-redux' +import { messageBlocksSelectors, selectFormattedCitationsByBlockId } from './messageBlock' +import type { RootState } from './store' + +// 获取所有块 +const allBlocks = useSelector(messageBlocksSelectors.selectAll) + +// 获取特定 ID 的块 +const specificBlock = useSelector((state: RootState) => messageBlocksSelectors.selectById(state, someBlockId)) + +// 获取特定引用块格式化后的引用列表 +const formattedCitations = useSelector((state: RootState) => selectFormattedCitationsByBlockId(state, citationBlockId)) + +// 在组件中使用引用数据 +// {formattedCitations.map(citation => ...)} +``` + +## 集成 + +`messageBlock.ts` slice 通常与 `messageThunk.ts` 中的 Thunks 紧密协作。Thunks 负责处理异步逻辑(如 API 调用、数据库操作),并在需要时 dispatch `messageBlock` slice 的 actions 来更新状态。例如,当 `messageThunk` 接收到流式响应时,它会 dispatch `upsertOneBlock` 或 `updateOneBlock` 来实时更新对应的 `MessageBlock`。同样,删除消息的 Thunk 会 dispatch `removeManyBlocks`。 + +理解 `messageBlock.ts` 的职责是管理**状态本身**,而 `messageThunk.ts` 负责**触发状态变更**的异步流程,这对于维护清晰的应用架构至关重要。 + +--- + +# messageThunk.ts 使用指南 + +该文件包含用于管理应用程序中消息流、处理助手交互以及同步 Redux 状态与 IndexedDB 数据库的核心 Thunk Action Creators。主要围绕 `Message` 和 `MessageBlock` 对象进行操作。 + +## 核心功能 + +1. **发送/接收消息**: 处理用户消息的发送,触发助手响应,并流式处理返回的数据,将其解析为不同的 `MessageBlock`。 +2. **状态管理**: 确保 Redux store 中的消息和消息块状态与 IndexedDB 中的持久化数据保持一致。 +3. **消息操作**: 提供删除、重发、重新生成、编辑后重发、追加响应、克隆等消息生命周期管理功能。 +4. **Block 处理**: 动态创建、更新和保存各种类型的 `MessageBlock`(文本、思考过程、工具调用、引用、图片、错误、翻译等)。 + +## 主要 Thunks + +以下是一些关键的 Thunk 函数及其用途: + +1. **`sendMessage(userMessage, userMessageBlocks, assistant, topicId)`** + + - **用途**: 发送一条新的用户消息。 + - **流程**: + - 保存用户消息 (`userMessage`) 及其块 (`userMessageBlocks`) 到 Redux 和 DB。 + - 检查 `@mentions` 以确定是单模型响应还是多模型响应。 + - 创建助手消息(们)的存根 (Stub)。 + - 将存根添加到 Redux 和 DB。 + - 将核心处理逻辑 `fetchAndProcessAssistantResponseImpl` 添加到该 `topicId` 的队列中以获取实际响应。 + - **Block 相关**: 主要处理用户消息的初始 `MessageBlock` 保存。 + +2. **`fetchAndProcessAssistantResponseImpl(dispatch, getState, topicId, assistant, assistantMessage)`** + + - **用途**: (内部函数) 获取并处理单个助手响应的核心逻辑,被 `sendMessage`, `resend...`, `regenerate...`, `append...` 等调用。 + - **流程**: + - 设置 Topic 加载状态。 + - 准备上下文消息。 + - 调用 `fetchChatCompletion` API 服务。 + - 使用 `createStreamProcessor` 处理流式响应。 + - 通过各种回调 (`onTextChunk`, `onThinkingChunk`, `onToolCallComplete`, `onImageGenerated`, `onError`, `onComplete` 等) 处理不同类型的事件。 + - **Block 相关**: + - 根据流事件创建初始 `UNKNOWN` 块。 + - 实时创建和更新 `MAIN_TEXT` 和 `THINKING` 块,使用 `throttledBlockUpdate` 和 `throttledBlockDbUpdate` 进行节流更新。 + - 创建 `TOOL`, `CITATION`, `IMAGE`, `ERROR` 等类型的块。 + - 在事件完成时(如 `onTextComplete`, `onToolCallComplete`)将块状态标记为 `SUCCESS` 或 `ERROR`,并使用 `saveUpdatedBlockToDB` 保存最终状态。 + - 使用 `handleBlockTransition` 管理非流式块(如 `TOOL`, `CITATION`)的添加和状态更新。 + +3. **`loadTopicMessagesThunk(topicId, forceReload)`** + + - **用途**: 从数据库加载指定主题的所有消息及其关联的 `MessageBlock`。 + - **流程**: + - 从 DB 获取 `Topic` 及其 `messages` 列表。 + - 根据消息 ID 列表从 DB 获取所有相关的 `MessageBlock`。 + - 使用 `upsertManyBlocks` 将块更新到 Redux。 + - 将消息更新到 Redux。 + - **Block 相关**: 负责将持久化的 `MessageBlock` 加载到 Redux 状态。 + +4. **删除 Thunks** + + - `deleteSingleMessageThunk(topicId, messageId)`: 删除单个消息及其所有 `MessageBlock`。 + - `deleteMessageGroupThunk(topicId, askId)`: 删除一个用户消息及其所有相关的助手响应消息和它们的所有 `MessageBlock`。 + - `clearTopicMessagesThunk(topicId)`: 清空主题下的所有消息及其所有 `MessageBlock`。 + - **Block 相关**: 从 Redux 和 DB 中移除指定的 `MessageBlock`。 + +5. **重发/重新生成 Thunks** + + - `resendMessageThunk(topicId, userMessageToResend, assistant)`: 重发用户消息。会重置(清空 Block 并标记为 PENDING)所有与该用户消息关联的助手响应,然后重新请求生成。 + - `resendUserMessageWithEditThunk(topicId, originalMessage, mainTextBlockId, editedContent, assistant)`: 用户编辑消息内容后重发。先更新用户消息的 `MAIN_TEXT` 块内容,然后调用 `resendMessageThunk`。 + - `regenerateAssistantResponseThunk(topicId, assistantMessageToRegenerate, assistant)`: 重新生成单个助手响应。重置该助手消息(清空 Block 并标记为 PENDING),然后重新请求生成。 + - **Block 相关**: 删除旧的 `MessageBlock`,并在重新生成过程中创建新的 `MessageBlock`。 + +6. **`appendAssistantResponseThunk(topicId, existingAssistantMessageId, newModel, assistant)`** + + - **用途**: 在已有的对话上下文中,针对同一个用户问题,使用新选择的模型追加一个新的助手响应。 + - **流程**: + - 找到现有助手消息以获取原始 `askId`。 + - 创建使用 `newModel` 的新助手消息存根(使用相同的 `askId`)。 + - 添加新存根到 Redux 和 DB。 + - 将 `fetchAndProcessAssistantResponseImpl` 添加到队列以生成新响应。 + - **Block 相关**: 为新的助手响应创建全新的 `MessageBlock`。 + +7. **`cloneMessagesToNewTopicThunk(sourceTopicId, branchPointIndex, newTopic)`** + + - **用途**: 将源主题的部分消息(及其 Block)克隆到一个**已存在**的新主题中。 + - **流程**: + - 复制指定索引前的消息。 + - 为所有克隆的消息和 Block 生成新的 UUID。 + - 正确映射克隆消息之间的 `askId` 关系。 + - 复制 `MessageBlock` 内容,更新其 `messageId` 指向新的消息 ID。 + - 更新文件引用计数(如果 Block 是文件或图片)。 + - 将克隆的消息和 Block 保存到新主题的 Redux 状态和 DB 中。 + - **Block 相关**: 创建 `MessageBlock` 的副本,并更新其 ID 和 `messageId`。 + +8. **`initiateTranslationThunk(messageId, topicId, targetLanguage, sourceBlockId?, sourceLanguage?)`** + - **用途**: 为指定消息启动翻译流程,创建一个初始的 `TRANSLATION` 类型的 `MessageBlock`。 + - **流程**: + - 创建一个状态为 `STREAMING` 的 `TranslationMessageBlock`。 + - 将其添加到 Redux 和 DB。 + - 更新原消息的 `blocks` 列表以包含新的翻译块 ID。 + - **Block 相关**: 创建并保存一个占位的 `TranslationMessageBlock`。实际翻译内容的获取和填充需要后续步骤。 + +## 内部机制和注意事项 + +- **数据库交互**: 通过 `saveMessageAndBlocksToDB`, `updateExistingMessageAndBlocksInDB`, `saveUpdatesToDB`, `saveUpdatedBlockToDB`, `throttledBlockDbUpdate` 等辅助函数与 IndexedDB (`db`) 交互,确保数据持久化。 +- **状态同步**: Thunks 负责协调 Redux Store 和 IndexedDB 之间的数据一致性。 +- **队列 (`getTopicQueue`)**: 使用 `AsyncQueue` 确保对同一主题的操作(尤其是 API 请求)按顺序执行,避免竞态条件。 +- **节流 (`throttle`)**: 对流式响应中频繁的 Block 更新(文本、思考)使用 `lodash.throttle` 优化性能,减少 Redux dispatch 和 DB 写入次数。 +- **错误处理**: `fetchAndProcessAssistantResponseImpl` 内的回调函数(特别是 `onError`)处理流处理和 API 调用中可能出现的错误,并创建 `ERROR` 类型的 `MessageBlock`。 + +开发者在使用这些 Thunks 时,通常需要提供 `dispatch`, `getState` (由 Redux Thunk 中间件注入),以及如 `topicId`, `assistant` 配置对象, 相关的 `Message` 或 `MessageBlock` 对象/ID 等参数。理解每个 Thunk 的职责和它如何影响消息及块的状态至关重要。 + +--- + +# useMessageOperations.ts 使用指南 + +该文件定义了一个名为 `useMessageOperations` 的自定义 React Hook。这个 Hook 的主要目的是为 React 组件提供一个便捷的接口,用于执行与特定主题(Topic)相关的各种消息操作。它封装了调用 Redux Thunks (`messageThunk.ts`) 和 Actions (`newMessage.ts`, `messageBlock.ts`) 的逻辑,简化了组件与消息数据交互的代码。 + +## 核心目标 + +- **封装**: 将复杂的消息操作逻辑(如删除、重发、重新生成、编辑、翻译等)封装在易于使用的函数中。 +- **简化**: 让组件可以直接调用这些操作函数,而无需直接与 Redux `dispatch` 或 Thunks 交互。 +- **上下文关联**: 所有操作都与传入的 `topic` 对象相关联,确保操作作用于正确的主题。 + +## 如何使用 + +在你的 React 函数组件中,导入并调用 `useMessageOperations` Hook,并传入当前活动的 `Topic` 对象。 + +```typescript +import React from 'react'; +import { useMessageOperations } from '@renderer/hooks/useMessageOperations'; +import type { Topic, Message, Assistant, Model } from '@renderer/types'; + +interface MyComponentProps { + currentTopic: Topic; + currentAssistant: Assistant; +} + +function MyComponent({ currentTopic, currentAssistant }: MyComponentProps) { + const { + deleteMessage, + resendMessage, + regenerateAssistantMessage, + appendAssistantResponse, + getTranslationUpdater, + createTopicBranch, + // ... 其他操作函数 + } = useMessageOperations(currentTopic); + + const handleDelete = (messageId: string) => { + deleteMessage(messageId); + }; + + const handleResend = (message: Message) => { + resendMessage(message, currentAssistant); + }; + + const handleAppend = (existingMsg: Message, newModel: Model) => { + appendAssistantResponse(existingMsg, newModel, currentAssistant); + } + + // ... 在组件中使用其他操作函数 + + return ( +
+ {/* Component UI */} + + {/* ... */} +
+ ); +} +``` + +## 返回值 + +`useMessageOperations(topic)` Hook 返回一个包含以下函数和值的对象: + +- **`deleteMessage(id: string)`**: + + - 删除指定 `id` 的单个消息。 + - 内部调用 `deleteSingleMessageThunk`。 + +- **`deleteGroupMessages(askId: string)`**: + + - 删除与指定 `askId` 相关联的一组消息(通常是用户提问及其所有助手回答)。 + - 内部调用 `deleteMessageGroupThunk`。 + +- **`editMessage(messageId: string, updates: Partial)`**: + + - 更新指定 `messageId` 的消息的部分属性。 + - **注意**: 目前主要用于更新 Redux 状态 + - 内部调用 `newMessagesActions.updateMessage`。 + +- **`resendMessage(message: Message, assistant: Assistant)`**: + + - 重新发送指定的用户消息 (`message`),这将触发其所有关联助手响应的重新生成。 + - 内部调用 `resendMessageThunk`。 + +- **`resendUserMessageWithEdit(message: Message, editedContent: string, assistant: Assistant)`**: + + - 在用户消息的主要文本块被编辑后,重新发送该消息。 + - 会先查找消息的 `MAIN_TEXT` 块 ID,然后调用 `resendUserMessageWithEditThunk`。 + +- **`clearTopicMessages(_topicId?: string)`**: + + - 清除当前主题(或可选的指定 `_topicId`)下的所有消息。 + - 内部调用 `clearTopicMessagesThunk`。 + +- **`createNewContext()`**: + + - 发出一个全局事件 (`EVENT_NAMES.NEW_CONTEXT`),通常用于通知 UI 清空显示,准备新的上下文。不直接修改 Redux 状态。 + +- **`displayCount`**: + + - (非操作函数) 从 Redux store 中获取当前的 `displayCount` 值。 + +- **`pauseMessages()`**: + + - 尝试中止当前主题中正在进行的消息生成(状态为 `processing` 或 `pending`)。 + - 通过查找相关的 `askId` 并调用 `abortCompletion` 来实现。 + - 同时会 dispatch `setTopicLoading` action 将加载状态设为 `false`。 + +- **`resumeMessage(message: Message, assistant: Assistant)`**: + + - 恢复/重新发送一个用户消息。目前实现为直接调用 `resendMessage`。 + +- **`regenerateAssistantMessage(message: Message, assistant: Assistant)`**: + + - 重新生成指定的**助手**消息 (`message`) 的响应。 + - 内部调用 `regenerateAssistantResponseThunk`。 + +- **`appendAssistantResponse(existingAssistantMessage: Message, newModel: Model, assistant: Assistant)`**: + + - 针对 `existingAssistantMessage` 所回复的**同一用户提问**,使用 `newModel` 追加一个新的助手响应。 + - 内部调用 `appendAssistantResponseThunk`。 + +- **`getTranslationUpdater(messageId: string, targetLanguage: string, sourceBlockId?: string, sourceLanguage?: string)`**: + + - **用途**: 获取一个用于逐步更新翻译块内容的函数。 + - **流程**: + 1. 内部调用 `initiateTranslationThunk` 来创建或获取一个 `TRANSLATION` 类型的 `MessageBlock`,并获取其 `blockId`。 + 2. 返回一个**异步更新函数**。 + - **返回的更新函数 `(accumulatedText: string, isComplete?: boolean) => void`**: + - 接收累积的翻译文本和完成状态。 + - 调用 `updateOneBlock` 更新 Redux 中的翻译块内容和状态 (`STREAMING` 或 `SUCCESS`)。 + - 调用 `throttledBlockDbUpdate` 将更新(节流地)保存到数据库。 + - 如果初始化失败(Thunk 返回 `undefined`),则此函数返回 `null`。 + +- **`createTopicBranch(sourceTopicId: string, branchPointIndex: number, newTopic: Topic)`**: + - 创建一个主题分支,将 `sourceTopicId` 主题中 `branchPointIndex` 索引之前的消息克隆到 `newTopic` 中。 + - **注意**: `newTopic` 对象必须是调用此函数**之前**已经创建并添加到 Redux 和数据库中的。 + - 内部调用 `cloneMessagesToNewTopicThunk`。 + +## 依赖 + +- **`topic: Topic`**: 必须传入当前操作上下文的主题对象。Hook 返回的操作函数将始终作用于这个主题的 `topic.id`。 +- **Redux `dispatch`**: Hook 内部使用 `useAppDispatch` 获取 `dispatch` 函数来调用 actions 和 thunks。 + +## 相关 Hooks + +在同一文件中还定义了两个辅助 Hook: + +- **`useTopicMessages(topic: Topic)`**: + + - 使用 `selectMessagesForTopic` selector 来获取并返回指定主题的消息列表。 + +- **`useTopicLoading(topic: Topic)`**: + - 使用 `selectNewTopicLoading` selector 来获取并返回指定主题的加载状态。 + +这些 Hook 可以与 `useMessageOperations` 结合使用,方便地在组件中获取消息数据、加载状态,并执行相关操作。 diff --git a/docs/technical/KnowledgeService.md b/docs/zh/references/services.md similarity index 100% rename from docs/technical/KnowledgeService.md rename to docs/zh/references/services.md diff --git a/electron-builder.yml b/electron-builder.yml index dfd14c2393..20c183a58b 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -134,58 +134,60 @@ artifactBuildCompleted: scripts/artifact-build-completed.js releaseInfo: releaseNotes: | - What's New in v1.7.0-rc.1 + Cherry Studio 1.7.3 - Feature & Stability Update - 🎉 MAJOR NEW FEATURE: AI Agents - - Create and manage custom AI agents with specialized tools and permissions - - Dedicated agent sessions with persistent SQLite storage, separate from regular chats - - Real-time tool approval system - review and approve agent actions dynamically - - MCP (Model Context Protocol) integration for connecting external tools - - Slash commands support for quick agent interactions - - OpenAI-compatible REST API for agent access + This release brings new features, UI improvements, and important bug fixes. - ✨ New Features: - - AI Providers: Added support for Hugging Face, Mistral, Perplexity, and SophNet - - Knowledge Base: OpenMinerU document preprocessor, full-text search in notes, enhanced tool selection - - Image & OCR: Intel OVMS painting provider and Intel OpenVINO (NPU) OCR support - - MCP Management: Redesigned interface with dual-column layout for easier management - - Languages: Added German language support + ✨ New Features + - Add MCP server log viewer for better debugging + - Support custom Git Bash path configuration + - Add print to PDF and save as HTML for mini program webviews + - Add CherryIN API host selection settings + - Enhance assistant presets with sort and batch delete modes + - Open URL directly for SelectionAssistant search action + - Enhance web search tool switching with provider-specific context - ⚡ Improvements: - - Upgraded to Electron 38.7.0 - - Enhanced system shutdown handling and automatic update checks - - Improved proxy bypass rules + 🔧 Improvements + - Remove Intel Ultra limit for OVMS + - Improve settings tab and assistant item UI - 🐛 Important Bug Fixes: - - Fixed streaming response issues across multiple AI providers - - Fixed session list scrolling problems - - Fixed knowledge base deletion errors + 🐛 Bug Fixes + - Fix stack overflow with base64 images + - Fix infinite loop in knowledge queue processing + - Fix quick panel closing in multiple selection mode + - Fix thinking timer not stopping when reply is aborted + - Fix ThinkingButton icon display for fixed reasoning mode + - Fix knowledge query prioritization and intent prompt + - Fix OpenRouter embeddings support + - Fix SelectionAction window resize on Windows + - Add gpustack provider support for qwen3 thinking mode - v1.7.0-rc.1 新特性 + Cherry Studio 1.7.3 - 功能与稳定性更新 - 🎉 重大更新:AI Agent 智能体系统 - - 创建和管理专属 AI Agent,配置专用工具和权限 - - 独立的 Agent 会话,使用 SQLite 持久化存储,与普通聊天分离 - - 实时工具审批系统 - 动态审查和批准 Agent 操作 - - MCP(模型上下文协议)集成,连接外部工具 - - 支持斜杠命令快速交互 - - 兼容 OpenAI 的 REST API 访问 + 本次更新带来新功能、界面改进和重要的问题修复。 - ✨ 新功能: - - AI 提供商:新增 Hugging Face、Mistral、Perplexity 和 SophNet 支持 - - 知识库:OpenMinerU 文档预处理器、笔记全文搜索、增强的工具选择 - - 图像与 OCR:Intel OVMS 绘图提供商和 Intel OpenVINO (NPU) OCR 支持 - - MCP 管理:重构管理界面,采用双列布局,更加方便管理 - - 语言:新增德语支持 + ✨ 新功能 + - 新增 MCP 服务器日志查看器,便于调试 + - 支持自定义 Git Bash 路径配置 + - 小程序 webview 支持打印 PDF 和保存为 HTML + - 新增 CherryIN API 主机选择设置 + - 助手预设增强:支持排序和批量删除模式 + - 划词助手搜索操作直接打开 URL + - 增强网页搜索工具切换逻辑,支持服务商特定上下文 - ⚡ 改进: - - 升级到 Electron 38.7.0 - - 增强的系统关机处理和自动更新检查 - - 改进的代理绕过规则 + 🔧 功能改进 + - 移除 OVMS 的 Intel Ultra 限制 + - 优化设置标签页和助手项目 UI - 🐛 重要修复: - - 修复多个 AI 提供商的流式响应问题 - - 修复会话列表滚动问题 - - 修复知识库删除错误 + 🐛 问题修复 + - 修复 base64 图片导致的栈溢出问题 + - 修复知识库队列处理的无限循环问题 + - 修复多选模式下快捷面板意外关闭的问题 + - 修复回复中止时思考计时器未停止的问题 + - 修复固定推理模式下思考按钮图标显示问题 + - 修复知识库查询优先级和意图提示 + - 修复 OpenRouter 嵌入模型支持 + - 修复 Windows 上划词助手窗口大小调整问题 + - 为 gpustack 服务商添加 qwen3 思考模式支持 diff --git a/eslint.config.mjs b/eslint.config.mjs index fcc952ed65..64fdefa1dc 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -58,6 +58,7 @@ export default defineConfig([ 'dist/**', 'out/**', 'local/**', + 'tests/**', '.yarn/**', '.gitignore', 'scripts/cloudflare-worker.js', diff --git a/package.json b/package.json index 04ce325558..58bdaf128a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "CherryStudio", - "version": "1.7.0-rc.1", + "version": "1.7.3", "private": true, "description": "A powerful AI assistant for producer.", "main": "./out/main/index.js", @@ -62,6 +62,7 @@ "test": "vitest run --silent", "test:main": "vitest run --project main", "test:renderer": "vitest run --project renderer", + "test:aicore": "vitest run --project aiCore", "test:update": "yarn test:renderer --update", "test:coverage": "vitest run --coverage --silent", "test:ui": "vitest --ui", @@ -74,17 +75,19 @@ "format:check": "biome format && biome lint", "prepare": "git config blame.ignoreRevsFile .git-blame-ignore-revs && husky", "claude": "dotenv -e .env -- claude", - "release:aicore:alpha": "yarn workspace @cherrystudio/ai-core version prerelease --immediate && yarn workspace @cherrystudio/ai-core npm publish --tag alpha --access public", - "release:aicore:beta": "yarn workspace @cherrystudio/ai-core version prerelease --immediate && yarn workspace @cherrystudio/ai-core npm publish --tag beta --access public", - "release:aicore": "yarn workspace @cherrystudio/ai-core version patch --immediate && yarn workspace @cherrystudio/ai-core npm publish --access public" + "release:aicore:alpha": "yarn workspace @cherrystudio/ai-core version prerelease --preid alpha --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --tag alpha --access public", + "release:aicore:beta": "yarn workspace @cherrystudio/ai-core version prerelease --preid beta --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --tag beta --access public", + "release:aicore": "yarn workspace @cherrystudio/ai-core version patch --immediate && yarn workspace @cherrystudio/ai-core build && yarn workspace @cherrystudio/ai-core npm publish --access public", + "release:ai-sdk-provider": "yarn workspace @cherrystudio/ai-sdk-provider version patch --immediate && yarn workspace @cherrystudio/ai-sdk-provider build && yarn workspace @cherrystudio/ai-sdk-provider npm publish --access public" }, "dependencies": { - "@anthropic-ai/claude-agent-sdk": "patch:@anthropic-ai/claude-agent-sdk@npm%3A0.1.30#~/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.30-b50a299674.patch", + "@anthropic-ai/claude-agent-sdk": "patch:@anthropic-ai/claude-agent-sdk@npm%3A0.1.62#~/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.62-23ae56f8c8.patch", "@libsql/client": "0.14.0", "@libsql/win32-x64-msvc": "^0.4.7", "@napi-rs/system-ocr": "patch:@napi-rs/system-ocr@npm%3A1.0.2#~/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch", "@paymoapp/electron-shutdown-handler": "^1.1.2", "@strongtz/win32-arm64-msvc": "^0.4.7", + "emoji-picker-element-data": "^1", "express": "^5.1.0", "font-list": "^2.0.0", "graceful-fs": "^4.2.11", @@ -107,15 +110,17 @@ "@agentic/exa": "^7.3.3", "@agentic/searxng": "^7.3.3", "@agentic/tavily": "^7.3.3", - "@ai-sdk/amazon-bedrock": "^3.0.53", - "@ai-sdk/anthropic": "^2.0.44", + "@ai-sdk/amazon-bedrock": "^3.0.61", + "@ai-sdk/anthropic": "^2.0.49", "@ai-sdk/cerebras": "^1.0.31", - "@ai-sdk/gateway": "^2.0.9", - "@ai-sdk/google": "patch:@ai-sdk/google@npm%3A2.0.36#~/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch", - "@ai-sdk/google-vertex": "^3.0.68", - "@ai-sdk/huggingface": "patch:@ai-sdk/huggingface@npm%3A0.0.8#~/.yarn/patches/@ai-sdk-huggingface-npm-0.0.8-d4d0aaac93.patch", - "@ai-sdk/mistral": "^2.0.23", - "@ai-sdk/perplexity": "^2.0.17", + "@ai-sdk/gateway": "^2.0.15", + "@ai-sdk/google": "patch:@ai-sdk/google@npm%3A2.0.43#~/.yarn/patches/@ai-sdk-google-npm-2.0.43-689ed559b3.patch", + "@ai-sdk/google-vertex": "^3.0.79", + "@ai-sdk/huggingface": "^0.0.10", + "@ai-sdk/mistral": "^2.0.24", + "@ai-sdk/openai": "patch:@ai-sdk/openai@npm%3A2.0.85#~/.yarn/patches/@ai-sdk-openai-npm-2.0.85-27483d1d6a.patch", + "@ai-sdk/perplexity": "^2.0.20", + "@ai-sdk/test-server": "^0.0.1", "@ant-design/v5-patch-for-react-19": "^1.0.3", "@anthropic-ai/sdk": "^0.41.0", "@anthropic-ai/vertex-sdk": "patch:@anthropic-ai/vertex-sdk@npm%3A0.11.4#~/.yarn/patches/@anthropic-ai-vertex-sdk-npm-0.11.4-c19cb41edb.patch", @@ -123,7 +128,7 @@ "@aws-sdk/client-bedrock-runtime": "^3.910.0", "@aws-sdk/client-s3": "^3.910.0", "@biomejs/biome": "2.2.4", - "@cherrystudio/ai-core": "workspace:^1.0.0-alpha.18", + "@cherrystudio/ai-core": "workspace:^1.0.9", "@cherrystudio/embedjs": "^0.1.31", "@cherrystudio/embedjs-libsql": "^0.1.31", "@cherrystudio/embedjs-loader-csv": "^0.1.31", @@ -137,7 +142,7 @@ "@cherrystudio/embedjs-ollama": "^0.1.31", "@cherrystudio/embedjs-openai": "^0.1.31", "@cherrystudio/extension-table-plus": "workspace:^", - "@cherrystudio/openai": "^6.9.0", + "@cherrystudio/openai": "^6.12.0", "@dnd-kit/core": "^6.3.1", "@dnd-kit/modifiers": "^9.0.0", "@dnd-kit/sortable": "^10.0.0", @@ -157,18 +162,18 @@ "@langchain/core": "patch:@langchain/core@npm%3A1.0.2#~/.yarn/patches/@langchain-core-npm-1.0.2-183ef83fe4.patch", "@langchain/openai": "patch:@langchain/openai@npm%3A1.0.0#~/.yarn/patches/@langchain-openai-npm-1.0.0-474d0ad9d4.patch", "@mistralai/mistralai": "^1.7.5", - "@modelcontextprotocol/sdk": "^1.17.5", + "@modelcontextprotocol/sdk": "^1.23.0", "@mozilla/readability": "^0.6.0", "@notionhq/client": "^2.2.15", - "@openrouter/ai-sdk-provider": "^1.2.0", + "@openrouter/ai-sdk-provider": "^1.2.8", "@opentelemetry/api": "^1.9.0", "@opentelemetry/core": "2.0.0", "@opentelemetry/exporter-trace-otlp-http": "^0.200.0", "@opentelemetry/sdk-trace-base": "^2.0.0", "@opentelemetry/sdk-trace-node": "^2.0.0", "@opentelemetry/sdk-trace-web": "^2.0.0", - "@opeoginni/github-copilot-openai-compatible": "0.1.21", - "@playwright/test": "^1.52.0", + "@opeoginni/github-copilot-openai-compatible": "^0.1.21", + "@playwright/test": "^1.55.1", "@radix-ui/react-context-menu": "^2.2.16", "@reduxjs/toolkit": "^2.2.5", "@shikijs/markdown-it": "^3.12.0", @@ -202,6 +207,7 @@ "@types/content-type": "^1.1.9", "@types/cors": "^2.8.19", "@types/diff": "^7", + "@types/dotenv": "^8.2.3", "@types/express": "^5", "@types/fs-extra": "^11", "@types/he": "^1", @@ -213,8 +219,8 @@ "@types/mime-types": "^3", "@types/node": "^22.17.1", "@types/pako": "^1.0.2", - "@types/react": "^19.0.12", - "@types/react-dom": "^19.0.4", + "@types/react": "^19.2.7", + "@types/react-dom": "^19.2.3", "@types/react-infinite-scroll-component": "^5.0.0", "@types/react-transition-group": "^4.4.12", "@types/react-window": "^1", @@ -236,7 +242,7 @@ "@viz-js/lang-dot": "^1.0.5", "@viz-js/viz": "^3.14.0", "@xyflow/react": "^12.4.4", - "ai": "^5.0.90", + "ai": "^5.0.98", "antd": "patch:antd@npm%3A5.27.0#~/.yarn/patches/antd-npm-5.27.0-aa91c36546.patch", "archiver": "^7.0.1", "async-mutex": "^0.5.0", @@ -312,12 +318,12 @@ "motion": "^12.10.5", "notion-helper": "^1.3.22", "npx-scope-finder": "^1.2.0", + "ollama-ai-provider-v2": "patch:ollama-ai-provider-v2@npm%3A1.5.5#~/.yarn/patches/ollama-ai-provider-v2-npm-1.5.5-8bef249af9.patch", "oxlint": "^1.22.0", "oxlint-tsgolint": "^0.2.0", "p-queue": "^8.1.0", "pdf-lib": "^1.17.1", "pdf-parse": "^1.1.1", - "playwright": "^1.55.1", "proxy-agent": "^6.5.0", "react": "^19.2.0", "react-dom": "^19.2.0", @@ -408,9 +414,9 @@ "@langchain/openai@npm:>=0.1.0 <0.6.0": "patch:@langchain/openai@npm%3A1.0.0#~/.yarn/patches/@langchain-openai-npm-1.0.0-474d0ad9d4.patch", "@langchain/openai@npm:^0.3.16": "patch:@langchain/openai@npm%3A1.0.0#~/.yarn/patches/@langchain-openai-npm-1.0.0-474d0ad9d4.patch", "@langchain/openai@npm:>=0.2.0 <0.7.0": "patch:@langchain/openai@npm%3A1.0.0#~/.yarn/patches/@langchain-openai-npm-1.0.0-474d0ad9d4.patch", - "@ai-sdk/openai@npm:2.0.64": "patch:@ai-sdk/openai@npm%3A2.0.64#~/.yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch", - "@ai-sdk/openai@npm:^2.0.42": "patch:@ai-sdk/openai@npm%3A2.0.64#~/.yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch", - "@ai-sdk/google@npm:2.0.36": "patch:@ai-sdk/google@npm%3A2.0.36#~/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch" + "@ai-sdk/openai@npm:^2.0.42": "patch:@ai-sdk/openai@npm%3A2.0.85#~/.yarn/patches/@ai-sdk-openai-npm-2.0.85-27483d1d6a.patch", + "@ai-sdk/google@npm:^2.0.40": "patch:@ai-sdk/google@npm%3A2.0.40#~/.yarn/patches/@ai-sdk-google-npm-2.0.40-47e0eeee83.patch", + "@ai-sdk/openai-compatible@npm:^1.0.27": "patch:@ai-sdk/openai-compatible@npm%3A1.0.27#~/.yarn/patches/@ai-sdk-openai-compatible-npm-1.0.27-06f74278cf.patch" }, "packageManager": "yarn@4.9.1", "lint-staged": { diff --git a/packages/ai-sdk-provider/package.json b/packages/ai-sdk-provider/package.json index fd0aac2643..25864f3b1f 100644 --- a/packages/ai-sdk-provider/package.json +++ b/packages/ai-sdk-provider/package.json @@ -1,6 +1,6 @@ { "name": "@cherrystudio/ai-sdk-provider", - "version": "0.1.0", + "version": "0.1.3", "description": "Cherry Studio AI SDK provider bundle with CherryIN routing.", "keywords": [ "ai-sdk", @@ -41,8 +41,9 @@ "ai": "^5.0.26" }, "dependencies": { + "@ai-sdk/openai-compatible": "^1.0.28", "@ai-sdk/provider": "^2.0.0", - "@ai-sdk/provider-utils": "^3.0.12" + "@ai-sdk/provider-utils": "^3.0.17" }, "devDependencies": { "tsdown": "^0.13.3", diff --git a/packages/ai-sdk-provider/src/cherryin-provider.ts b/packages/ai-sdk-provider/src/cherryin-provider.ts index 478380a411..33ec1a2a3a 100644 --- a/packages/ai-sdk-provider/src/cherryin-provider.ts +++ b/packages/ai-sdk-provider/src/cherryin-provider.ts @@ -2,7 +2,6 @@ import { AnthropicMessagesLanguageModel } from '@ai-sdk/anthropic/internal' import { GoogleGenerativeAILanguageModel } from '@ai-sdk/google/internal' import type { OpenAIProviderSettings } from '@ai-sdk/openai' import { - OpenAIChatLanguageModel, OpenAICompletionLanguageModel, OpenAIEmbeddingModel, OpenAIImageModel, @@ -10,6 +9,7 @@ import { OpenAISpeechModel, OpenAITranscriptionModel } from '@ai-sdk/openai/internal' +import { OpenAICompatibleChatLanguageModel } from '@ai-sdk/openai-compatible' import { type EmbeddingModelV2, type ImageModelV2, @@ -67,6 +67,11 @@ export interface CherryInProviderSettings { * Optional static headers applied to every request. */ headers?: HeadersInput + /** + * Optional endpoint type to distinguish different endpoint behaviors. + * "image-generation" is also openai endpoint, but specifically for image generation. + */ + endpointType?: 'openai' | 'openai-response' | 'anthropic' | 'gemini' | 'image-generation' | 'jina-rerank' } export interface CherryInProvider extends ProviderV2 { @@ -113,7 +118,7 @@ const createCustomFetch = (originalFetch?: any) => { return originalFetch ? originalFetch(url, options) : fetch(url, options) } } -class CherryInOpenAIChatLanguageModel extends OpenAIChatLanguageModel { +class CherryInOpenAIChatLanguageModel extends OpenAICompatibleChatLanguageModel { constructor(modelId: string, settings: any) { super(modelId, { ...settings, @@ -151,7 +156,8 @@ export const createCherryIn = (options: CherryInProviderSettings = {}): CherryIn baseURL = DEFAULT_CHERRYIN_BASE_URL, anthropicBaseURL = DEFAULT_CHERRYIN_ANTHROPIC_BASE_URL, geminiBaseURL = DEFAULT_CHERRYIN_GEMINI_BASE_URL, - fetch + fetch, + endpointType } = options const getJsonHeaders = createJsonHeadersGetter(options) @@ -205,7 +211,7 @@ export const createCherryIn = (options: CherryInProviderSettings = {}): CherryIn fetch }) - const createChatModel = (modelId: string, settings: OpenAIProviderSettings = {}) => { + const createChatModelByModelId = (modelId: string, settings: OpenAIProviderSettings = {}) => { if (isAnthropicModel(modelId)) { return createAnthropicModel(modelId) } @@ -223,6 +229,29 @@ export const createCherryIn = (options: CherryInProviderSettings = {}): CherryIn }) } + const createChatModel = (modelId: string, settings: OpenAIProviderSettings = {}) => { + if (!endpointType) return createChatModelByModelId(modelId, settings) + switch (endpointType) { + case 'anthropic': + return createAnthropicModel(modelId) + case 'gemini': + return createGeminiModel(modelId) + case 'openai': + return createOpenAIChatModel(modelId) + case 'openai-response': + default: + return new OpenAIResponsesLanguageModel(modelId, { + provider: `${CHERRYIN_PROVIDER_NAME}.openai`, + url, + headers: () => ({ + ...getJsonHeaders(), + ...settings.headers + }), + fetch + }) + } + } + const createCompletionModel = (modelId: string, settings: OpenAIProviderSettings = {}) => new OpenAICompletionLanguageModel(modelId, { provider: `${CHERRYIN_PROVIDER_NAME}.completion`, diff --git a/packages/aiCore/README.md b/packages/aiCore/README.md index 4ca5ea6640..1380019094 100644 --- a/packages/aiCore/README.md +++ b/packages/aiCore/README.md @@ -71,7 +71,7 @@ Cherry Studio AI Core 是一个基于 Vercel AI SDK 的统一 AI Provider 接口 ## 安装 ```bash -npm install @cherrystudio/ai-core ai +npm install @cherrystudio/ai-core ai @ai-sdk/google @ai-sdk/openai ``` ### React Native diff --git a/packages/aiCore/package.json b/packages/aiCore/package.json index 12249cfb7b..6fc0f53344 100644 --- a/packages/aiCore/package.json +++ b/packages/aiCore/package.json @@ -1,6 +1,6 @@ { "name": "@cherrystudio/ai-core", - "version": "1.0.1", + "version": "1.0.9", "description": "Cherry Studio AI Core - Unified AI Provider Interface Based on Vercel AI SDK", "main": "dist/index.js", "module": "dist/index.mjs", @@ -33,19 +33,19 @@ }, "homepage": "https://github.com/CherryHQ/cherry-studio#readme", "peerDependencies": { + "@ai-sdk/google": "^2.0.36", + "@ai-sdk/openai": "^2.0.64", + "@cherrystudio/ai-sdk-provider": "^0.1.3", "ai": "^5.0.26" }, "dependencies": { - "@ai-sdk/anthropic": "^2.0.43", - "@ai-sdk/azure": "^2.0.66", - "@ai-sdk/deepseek": "^1.0.27", - "@ai-sdk/google": "patch:@ai-sdk/google@npm%3A2.0.36#~/.yarn/patches/@ai-sdk-google-npm-2.0.36-6f3cc06026.patch", - "@ai-sdk/openai": "patch:@ai-sdk/openai@npm%3A2.0.64#~/.yarn/patches/@ai-sdk-openai-npm-2.0.64-48f99f5bf3.patch", - "@ai-sdk/openai-compatible": "^1.0.26", + "@ai-sdk/anthropic": "^2.0.49", + "@ai-sdk/azure": "^2.0.87", + "@ai-sdk/deepseek": "^1.0.31", + "@ai-sdk/openai-compatible": "patch:@ai-sdk/openai-compatible@npm%3A1.0.27#~/.yarn/patches/@ai-sdk-openai-compatible-npm-1.0.27-06f74278cf.patch", "@ai-sdk/provider": "^2.0.0", - "@ai-sdk/provider-utils": "^3.0.16", - "@ai-sdk/xai": "^2.0.31", - "@cherrystudio/ai-sdk-provider": "workspace:*", + "@ai-sdk/provider-utils": "^3.0.17", + "@ai-sdk/xai": "^2.0.36", "zod": "^4.1.5" }, "devDependencies": { diff --git a/packages/aiCore/src/__tests__/fixtures/mock-providers.ts b/packages/aiCore/src/__tests__/fixtures/mock-providers.ts new file mode 100644 index 0000000000..e8ec2a4a05 --- /dev/null +++ b/packages/aiCore/src/__tests__/fixtures/mock-providers.ts @@ -0,0 +1,180 @@ +/** + * Mock Provider Instances + * Provides mock implementations for all supported AI providers + */ + +import type { ImageModelV2, LanguageModelV2 } from '@ai-sdk/provider' +import { vi } from 'vitest' + +/** + * Creates a mock language model with customizable behavior + */ +export function createMockLanguageModel(overrides?: Partial): LanguageModelV2 { + return { + specificationVersion: 'v1', + provider: 'mock-provider', + modelId: 'mock-model', + defaultObjectGenerationMode: 'tool', + + doGenerate: vi.fn().mockResolvedValue({ + text: 'Mock response text', + finishReason: 'stop', + usage: { + promptTokens: 10, + completionTokens: 20, + totalTokens: 30 + }, + rawCall: { rawPrompt: null, rawSettings: {} }, + rawResponse: { headers: {} }, + warnings: [] + }), + + doStream: vi.fn().mockReturnValue({ + stream: (async function* () { + yield { + type: 'text-delta', + textDelta: 'Mock ' + } + yield { + type: 'text-delta', + textDelta: 'streaming ' + } + yield { + type: 'text-delta', + textDelta: 'response' + } + yield { + type: 'finish', + finishReason: 'stop', + usage: { + promptTokens: 10, + completionTokens: 15, + totalTokens: 25 + } + } + })(), + rawCall: { rawPrompt: null, rawSettings: {} }, + rawResponse: { headers: {} }, + warnings: [] + }), + + ...overrides + } as LanguageModelV2 +} + +/** + * Creates a mock image model with customizable behavior + */ +export function createMockImageModel(overrides?: Partial): ImageModelV2 { + return { + specificationVersion: 'v2', + provider: 'mock-provider', + modelId: 'mock-image-model', + + doGenerate: vi.fn().mockResolvedValue({ + images: [ + { + base64: 'mock-base64-image-data', + uint8Array: new Uint8Array([1, 2, 3, 4, 5]), + mimeType: 'image/png' + } + ], + warnings: [] + }), + + ...overrides + } as ImageModelV2 +} + +/** + * Mock provider configurations for testing + */ +export const mockProviderConfigs = { + openai: { + apiKey: 'sk-test-openai-key-123456789', + baseURL: 'https://api.openai.com/v1', + organization: 'test-org' + }, + + anthropic: { + apiKey: 'sk-ant-test-key-123456789', + baseURL: 'https://api.anthropic.com' + }, + + google: { + apiKey: 'test-google-api-key-123456789', + baseURL: 'https://generativelanguage.googleapis.com/v1' + }, + + xai: { + apiKey: 'xai-test-key-123456789', + baseURL: 'https://api.x.ai/v1' + }, + + azure: { + apiKey: 'test-azure-key-123456789', + resourceName: 'test-resource', + deployment: 'test-deployment' + }, + + deepseek: { + apiKey: 'sk-test-deepseek-key-123456789', + baseURL: 'https://api.deepseek.com/v1' + }, + + openrouter: { + apiKey: 'sk-or-test-key-123456789', + baseURL: 'https://openrouter.ai/api/v1' + }, + + huggingface: { + apiKey: 'hf_test_key_123456789', + baseURL: 'https://api-inference.huggingface.co' + }, + + 'openai-compatible': { + apiKey: 'test-compatible-key-123456789', + baseURL: 'https://api.example.com/v1', + name: 'test-provider' + }, + + 'openai-chat': { + apiKey: 'sk-test-chat-key-123456789', + baseURL: 'https://api.openai.com/v1' + } +} as const + +/** + * Mock provider instances for testing + */ +export const mockProviderInstances = { + openai: { + name: 'openai-mock', + languageModel: createMockLanguageModel({ provider: 'openai', modelId: 'gpt-4' }), + imageModel: createMockImageModel({ provider: 'openai', modelId: 'dall-e-3' }) + }, + + anthropic: { + name: 'anthropic-mock', + languageModel: createMockLanguageModel({ provider: 'anthropic', modelId: 'claude-3-5-sonnet-20241022' }) + }, + + google: { + name: 'google-mock', + languageModel: createMockLanguageModel({ provider: 'google', modelId: 'gemini-2.0-flash-exp' }), + imageModel: createMockImageModel({ provider: 'google', modelId: 'imagen-3.0-generate-001' }) + }, + + xai: { + name: 'xai-mock', + languageModel: createMockLanguageModel({ provider: 'xai', modelId: 'grok-2-latest' }), + imageModel: createMockImageModel({ provider: 'xai', modelId: 'grok-2-image-latest' }) + }, + + deepseek: { + name: 'deepseek-mock', + languageModel: createMockLanguageModel({ provider: 'deepseek', modelId: 'deepseek-chat' }) + } +} + +export type ProviderId = keyof typeof mockProviderConfigs diff --git a/packages/aiCore/src/__tests__/fixtures/mock-responses.ts b/packages/aiCore/src/__tests__/fixtures/mock-responses.ts new file mode 100644 index 0000000000..388a4f7fd5 --- /dev/null +++ b/packages/aiCore/src/__tests__/fixtures/mock-responses.ts @@ -0,0 +1,238 @@ +/** + * Mock Responses + * Provides realistic mock responses for all provider types + */ + +import type { ModelMessage, Tool } from 'ai' +import { jsonSchema } from 'ai' + +/** + * Standard test messages for all scenarios + */ +export const testMessages: Record = { + simple: [{ role: 'user' as const, content: 'Hello, how are you?' }], + + conversation: [ + { role: 'user' as const, content: 'What is the capital of France?' }, + { role: 'assistant' as const, content: 'The capital of France is Paris.' }, + { role: 'user' as const, content: 'What is its population?' } + ], + + withSystem: [ + { role: 'system' as const, content: 'You are a helpful assistant that provides concise answers.' }, + { role: 'user' as const, content: 'Explain quantum computing in one sentence.' } + ], + + withImages: [ + { + role: 'user' as const, + content: [ + { type: 'text' as const, text: 'What is in this image?' }, + { + type: 'image' as const, + image: + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==' + } + ] + } + ], + + toolUse: [{ role: 'user' as const, content: 'What is the weather in San Francisco?' }], + + multiTurn: [ + { role: 'user' as const, content: 'Can you help me with a math problem?' }, + { role: 'assistant' as const, content: 'Of course! What math problem would you like help with?' }, + { role: 'user' as const, content: 'What is 15 * 23?' }, + { role: 'assistant' as const, content: '15 * 23 = 345' }, + { role: 'user' as const, content: 'Now divide that by 5' } + ] +} + +/** + * Standard test tools for tool calling scenarios + */ +export const testTools: Record = { + getWeather: { + description: 'Get the current weather in a given location', + inputSchema: jsonSchema({ + type: 'object', + properties: { + location: { + type: 'string', + description: 'The city and state, e.g. San Francisco, CA' + }, + unit: { + type: 'string', + enum: ['celsius', 'fahrenheit'], + description: 'The temperature unit to use' + } + }, + required: ['location'] + }), + execute: async ({ location, unit = 'fahrenheit' }) => { + return { + location, + temperature: unit === 'celsius' ? 22 : 72, + unit, + condition: 'sunny' + } + } + }, + + calculate: { + description: 'Perform a mathematical calculation', + inputSchema: jsonSchema({ + type: 'object', + properties: { + operation: { + type: 'string', + enum: ['add', 'subtract', 'multiply', 'divide'], + description: 'The operation to perform' + }, + a: { + type: 'number', + description: 'The first number' + }, + b: { + type: 'number', + description: 'The second number' + } + }, + required: ['operation', 'a', 'b'] + }), + execute: async ({ operation, a, b }) => { + const operations = { + add: (x: number, y: number) => x + y, + subtract: (x: number, y: number) => x - y, + multiply: (x: number, y: number) => x * y, + divide: (x: number, y: number) => x / y + } + return { result: operations[operation as keyof typeof operations](a, b) } + } + }, + + searchDatabase: { + description: 'Search for information in a database', + inputSchema: jsonSchema({ + type: 'object', + properties: { + query: { + type: 'string', + description: 'The search query' + }, + limit: { + type: 'number', + description: 'Maximum number of results to return', + default: 10 + } + }, + required: ['query'] + }), + execute: async ({ query, limit = 10 }) => { + return { + results: [ + { id: 1, title: `Result 1 for ${query}`, relevance: 0.95 }, + { id: 2, title: `Result 2 for ${query}`, relevance: 0.87 } + ].slice(0, limit) + } + } + } +} + +/** + * Mock complete responses for non-streaming scenarios + * Note: AI SDK v5 uses inputTokens/outputTokens instead of promptTokens/completionTokens + */ +export const mockCompleteResponses = { + simple: { + text: 'This is a simple response.', + finishReason: 'stop' as const, + usage: { + inputTokens: 15, + outputTokens: 8, + totalTokens: 23 + } + }, + + withToolCalls: { + text: 'I will check the weather for you.', + toolCalls: [ + { + toolCallId: 'call_456', + toolName: 'getWeather', + args: { location: 'New York, NY', unit: 'celsius' } + } + ], + finishReason: 'tool-calls' as const, + usage: { + inputTokens: 25, + outputTokens: 12, + totalTokens: 37 + } + }, + + withWarnings: { + text: 'Response with warnings.', + finishReason: 'stop' as const, + usage: { + inputTokens: 10, + outputTokens: 5, + totalTokens: 15 + }, + warnings: [ + { + type: 'unsupported-setting' as const, + setting: 'temperature', + details: 'Temperature parameter not supported for this model' + } + ] + } +} + +/** + * Mock image generation responses + */ +export const mockImageResponses = { + single: { + image: { + base64: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', + uint8Array: new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82]), + mimeType: 'image/png' as const + }, + warnings: [] + }, + + multiple: { + images: [ + { + base64: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', + uint8Array: new Uint8Array([137, 80, 78, 71]), + mimeType: 'image/png' as const + }, + { + base64: 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCKAgdZ9zImAAAAAElFTkSuQmCC', + uint8Array: new Uint8Array([137, 80, 78, 71]), + mimeType: 'image/png' as const + } + ], + warnings: [] + }, + + withProviderMetadata: { + image: { + base64: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', + uint8Array: new Uint8Array([137, 80, 78, 71]), + mimeType: 'image/png' as const + }, + providerMetadata: { + openai: { + images: [ + { + revisedPrompt: 'A detailed and enhanced version of the original prompt' + } + ] + } + }, + warnings: [] + } +} diff --git a/packages/aiCore/src/__tests__/helpers/provider-test-utils.ts b/packages/aiCore/src/__tests__/helpers/provider-test-utils.ts new file mode 100644 index 0000000000..f8a2051b4b --- /dev/null +++ b/packages/aiCore/src/__tests__/helpers/provider-test-utils.ts @@ -0,0 +1,329 @@ +/** + * Provider-Specific Test Utilities + * Helper functions for testing individual providers with all their parameters + */ + +import type { Tool } from 'ai' +import { expect } from 'vitest' + +/** + * Provider parameter configurations for comprehensive testing + */ +export const providerParameterMatrix = { + openai: { + models: ['gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo', 'gpt-4o'], + parameters: { + temperature: [0, 0.5, 0.7, 1.0, 1.5, 2.0], + maxTokens: [100, 500, 1000, 2000, 4000], + topP: [0.1, 0.5, 0.9, 1.0], + frequencyPenalty: [-2.0, -1.0, 0, 1.0, 2.0], + presencePenalty: [-2.0, -1.0, 0, 1.0, 2.0], + stop: [undefined, ['stop'], ['STOP', 'END']], + seed: [undefined, 12345, 67890], + responseFormat: [undefined, { type: 'json_object' as const }], + user: [undefined, 'test-user-123'] + }, + toolChoice: ['auto', 'required', 'none', { type: 'function' as const, name: 'getWeather' }], + parallelToolCalls: [true, false] + }, + + anthropic: { + models: ['claude-3-5-sonnet-20241022', 'claude-3-opus-20240229', 'claude-3-haiku-20240307'], + parameters: { + temperature: [0, 0.5, 1.0], + maxTokens: [100, 1000, 4000, 8000], + topP: [0.1, 0.5, 0.9, 1.0], + topK: [undefined, 1, 5, 10, 40], + stop: [undefined, ['Human:', 'Assistant:']], + metadata: [undefined, { userId: 'test-123' }] + }, + toolChoice: ['auto', 'any', { type: 'tool' as const, name: 'getWeather' }] + }, + + google: { + models: ['gemini-2.0-flash-exp', 'gemini-1.5-pro', 'gemini-1.5-flash'], + parameters: { + temperature: [0, 0.5, 0.9, 1.0], + maxTokens: [100, 1000, 2000, 8000], + topP: [0.1, 0.5, 0.95, 1.0], + topK: [undefined, 1, 16, 40], + stopSequences: [undefined, ['END'], ['STOP', 'TERMINATE']] + }, + safetySettings: [ + undefined, + [ + { category: 'HARM_CATEGORY_HARASSMENT', threshold: 'BLOCK_MEDIUM_AND_ABOVE' }, + { category: 'HARM_CATEGORY_HATE_SPEECH', threshold: 'BLOCK_ONLY_HIGH' } + ] + ] + }, + + xai: { + models: ['grok-2-latest', 'grok-2-1212'], + parameters: { + temperature: [0, 0.5, 1.0, 1.5], + maxTokens: [100, 500, 2000, 4000], + topP: [0.1, 0.5, 0.9, 1.0], + stop: [undefined, ['STOP'], ['END', 'TERMINATE']], + seed: [undefined, 12345] + } + }, + + deepseek: { + models: ['deepseek-chat', 'deepseek-coder'], + parameters: { + temperature: [0, 0.5, 1.0], + maxTokens: [100, 1000, 4000], + topP: [0.1, 0.5, 0.95], + frequencyPenalty: [0, 0.5, 1.0], + presencePenalty: [0, 0.5, 1.0], + stop: [undefined, ['```'], ['END']] + } + }, + + azure: { + deployments: ['gpt-4-deployment', 'gpt-35-turbo-deployment'], + parameters: { + temperature: [0, 0.7, 1.0], + maxTokens: [100, 1000, 2000], + topP: [0.1, 0.5, 0.95], + frequencyPenalty: [0, 1.0], + presencePenalty: [0, 1.0], + stop: [undefined, ['STOP']] + } + } +} as const + +/** + * Creates test cases for all parameter combinations + */ +export function generateParameterTestCases>( + params: T, + maxCombinations = 50 +): Array> { + const keys = Object.keys(params) as Array + const testCases: Array> = [] + + // Generate combinations using sampling strategy for large parameter spaces + const totalCombinations = keys.reduce((acc, key) => acc * params[key].length, 1) + + if (totalCombinations <= maxCombinations) { + // Generate all combinations if total is small + generateAllCombinations(params, keys, 0, {}, testCases) + } else { + // Sample diverse combinations if total is large + generateSampledCombinations(params, keys, maxCombinations, testCases) + } + + return testCases +} + +function generateAllCombinations>( + params: T, + keys: Array, + index: number, + current: Partial<{ [K in keyof T]: T[K][number] }>, + results: Array> +) { + if (index === keys.length) { + results.push({ ...current }) + return + } + + const key = keys[index] + for (const value of params[key]) { + generateAllCombinations(params, keys, index + 1, { ...current, [key]: value }, results) + } +} + +function generateSampledCombinations>( + params: T, + keys: Array, + count: number, + results: Array> +) { + // Generate edge cases first (min/max values) + const edgeCase1: any = {} + const edgeCase2: any = {} + + for (const key of keys) { + edgeCase1[key] = params[key][0] + edgeCase2[key] = params[key][params[key].length - 1] + } + + results.push(edgeCase1, edgeCase2) + + // Generate random combinations for the rest + for (let i = results.length; i < count; i++) { + const combination: any = {} + for (const key of keys) { + const values = params[key] + combination[key] = values[Math.floor(Math.random() * values.length)] + } + results.push(combination) + } +} + +/** + * Validates that all provider-specific parameters are correctly passed through + */ +export function validateProviderParams(providerId: string, actualParams: any, expectedParams: any): void { + const requiredFields: Record = { + openai: ['model', 'messages'], + anthropic: ['model', 'messages'], + google: ['model', 'contents'], + xai: ['model', 'messages'], + deepseek: ['model', 'messages'], + azure: ['messages'] + } + + const fields = requiredFields[providerId] || ['model', 'messages'] + + for (const field of fields) { + expect(actualParams).toHaveProperty(field) + } + + // Validate optional parameters if they were provided + const optionalParams = ['temperature', 'max_tokens', 'top_p', 'stop', 'tools'] + + for (const param of optionalParams) { + if (expectedParams[param] !== undefined) { + expect(actualParams[param]).toEqual(expectedParams[param]) + } + } +} + +/** + * Creates a comprehensive test suite for a provider + */ +// oxlint-disable-next-line no-unused-vars +export function createProviderTestSuite(_providerId: string) { + return { + testBasicCompletion: async (executor: any, model: string) => { + const result = await executor.generateText({ + model, + messages: [{ role: 'user' as const, content: 'Hello' }] + }) + + expect(result).toBeDefined() + expect(result.text).toBeDefined() + expect(typeof result.text).toBe('string') + }, + + testStreaming: async (executor: any, model: string) => { + const chunks: any[] = [] + const result = await executor.streamText({ + model, + messages: [{ role: 'user' as const, content: 'Hello' }] + }) + + for await (const chunk of result.textStream) { + chunks.push(chunk) + } + + expect(chunks.length).toBeGreaterThan(0) + }, + + testTemperature: async (executor: any, model: string, temperatures: number[]) => { + for (const temperature of temperatures) { + const result = await executor.generateText({ + model, + messages: [{ role: 'user' as const, content: 'Hello' }], + temperature + }) + + expect(result).toBeDefined() + } + }, + + testMaxTokens: async (executor: any, model: string, maxTokensValues: number[]) => { + for (const maxTokens of maxTokensValues) { + const result = await executor.generateText({ + model, + messages: [{ role: 'user' as const, content: 'Hello' }], + maxTokens + }) + + expect(result).toBeDefined() + if (result.usage?.completionTokens) { + expect(result.usage.completionTokens).toBeLessThanOrEqual(maxTokens) + } + } + }, + + testToolCalling: async (executor: any, model: string, tools: Record) => { + const result = await executor.generateText({ + model, + messages: [{ role: 'user' as const, content: 'What is the weather in SF?' }], + tools + }) + + expect(result).toBeDefined() + }, + + testStopSequences: async (executor: any, model: string, stopSequences: string[][]) => { + for (const stop of stopSequences) { + const result = await executor.generateText({ + model, + messages: [{ role: 'user' as const, content: 'Count to 10' }], + stop + }) + + expect(result).toBeDefined() + } + } + } +} + +/** + * Generates test data for vision/multimodal testing + */ +export function createVisionTestData() { + return { + imageUrl: 'https://example.com/test-image.jpg', + base64Image: + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', + messages: [ + { + role: 'user' as const, + content: [ + { type: 'text' as const, text: 'What is in this image?' }, + { + type: 'image' as const, + image: + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==' + } + ] + } + ] + } +} + +/** + * Creates mock responses for different finish reasons + */ +export function createFinishReasonMocks() { + return { + stop: { + text: 'Complete response.', + finishReason: 'stop' as const, + usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 } + }, + length: { + text: 'Incomplete response due to', + finishReason: 'length' as const, + usage: { promptTokens: 10, completionTokens: 100, totalTokens: 110 } + }, + 'tool-calls': { + text: 'Calling tools', + finishReason: 'tool-calls' as const, + toolCalls: [{ toolCallId: 'call_1', toolName: 'getWeather', args: { location: 'SF' } }], + usage: { promptTokens: 10, completionTokens: 8, totalTokens: 18 } + }, + 'content-filter': { + text: '', + finishReason: 'content-filter' as const, + usage: { promptTokens: 10, completionTokens: 0, totalTokens: 10 } + } + } +} diff --git a/packages/aiCore/src/__tests__/helpers/test-utils.ts b/packages/aiCore/src/__tests__/helpers/test-utils.ts new file mode 100644 index 0000000000..8231075785 --- /dev/null +++ b/packages/aiCore/src/__tests__/helpers/test-utils.ts @@ -0,0 +1,291 @@ +/** + * Test Utilities + * Helper functions for testing AI Core functionality + */ + +import { expect, vi } from 'vitest' + +import type { ProviderId } from '../fixtures/mock-providers' +import { createMockImageModel, createMockLanguageModel, mockProviderConfigs } from '../fixtures/mock-providers' + +/** + * Creates a test provider with streaming support + */ +export function createTestStreamingProvider(chunks: any[]) { + return createMockLanguageModel({ + doStream: vi.fn().mockReturnValue({ + stream: (async function* () { + for (const chunk of chunks) { + yield chunk + } + })(), + rawCall: { rawPrompt: null, rawSettings: {} }, + rawResponse: { headers: {} }, + warnings: [] + }) + }) +} + +/** + * Creates a test provider that throws errors + */ +export function createErrorProvider(error: Error) { + return createMockLanguageModel({ + doGenerate: vi.fn().mockRejectedValue(error), + doStream: vi.fn().mockImplementation(() => { + throw error + }) + }) +} + +/** + * Collects all chunks from a stream + */ +export async function collectStreamChunks(stream: AsyncIterable): Promise { + const chunks: T[] = [] + for await (const chunk of stream) { + chunks.push(chunk) + } + return chunks +} + +/** + * Waits for a specific number of milliseconds + */ +export function wait(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +/** + * Creates a mock abort controller that aborts after a delay + */ +export function createDelayedAbortController(delayMs: number): AbortController { + const controller = new AbortController() + setTimeout(() => controller.abort(), delayMs) + return controller +} + +/** + * Asserts that a function throws an error with a specific message + */ +export async function expectError(fn: () => Promise, expectedMessage?: string | RegExp): Promise { + try { + await fn() + throw new Error('Expected function to throw an error, but it did not') + } catch (error) { + if (expectedMessage) { + const message = (error as Error).message + if (typeof expectedMessage === 'string') { + if (!message.includes(expectedMessage)) { + throw new Error(`Expected error message to include "${expectedMessage}", but got "${message}"`) + } + } else { + if (!expectedMessage.test(message)) { + throw new Error(`Expected error message to match ${expectedMessage}, but got "${message}"`) + } + } + } + return error as Error + } +} + +/** + * Creates a spy function that tracks calls and arguments + */ +export function createSpy any>() { + const calls: Array<{ args: Parameters; result?: ReturnType; error?: Error }> = [] + + const spy = vi.fn((...args: Parameters) => { + try { + const result = undefined as ReturnType + calls.push({ args, result }) + return result + } catch (error) { + calls.push({ args, error: error as Error }) + throw error + } + }) + + return { + fn: spy, + calls, + getCalls: () => calls, + getCallCount: () => calls.length, + getLastCall: () => calls[calls.length - 1], + reset: () => { + calls.length = 0 + spy.mockClear() + } + } +} + +/** + * Validates provider configuration + */ +export function validateProviderConfig(providerId: ProviderId) { + const config = mockProviderConfigs[providerId] + if (!config) { + throw new Error(`No mock configuration found for provider: ${providerId}`) + } + + if (!config.apiKey) { + throw new Error(`Provider ${providerId} is missing apiKey in mock config`) + } + + return config +} + +/** + * Creates a test context with common setup + */ +export function createTestContext() { + const mocks = { + languageModel: createMockLanguageModel(), + imageModel: createMockImageModel(), + providers: new Map() + } + + const cleanup = () => { + mocks.providers.clear() + vi.clearAllMocks() + } + + return { + mocks, + cleanup + } +} + +/** + * Measures execution time of an async function + */ +export async function measureTime(fn: () => Promise): Promise<{ result: T; duration: number }> { + const start = Date.now() + const result = await fn() + const duration = Date.now() - start + return { result, duration } +} + +/** + * Retries a function until it succeeds or max attempts reached + */ +export async function retryUntilSuccess(fn: () => Promise, maxAttempts = 3, delayMs = 100): Promise { + let lastError: Error | undefined + + for (let attempt = 1; attempt <= maxAttempts; attempt++) { + try { + return await fn() + } catch (error) { + lastError = error as Error + if (attempt < maxAttempts) { + await wait(delayMs) + } + } + } + + throw lastError || new Error('All retry attempts failed') +} + +/** + * Creates a mock streaming response that emits chunks at intervals + */ +export function createTimedStream(chunks: T[], intervalMs = 10) { + return { + async *[Symbol.asyncIterator]() { + for (const chunk of chunks) { + await wait(intervalMs) + yield chunk + } + } + } +} + +/** + * Asserts that two objects are deeply equal, ignoring specified keys + */ +export function assertDeepEqualIgnoring>( + actual: T, + expected: T, + ignoreKeys: string[] = [] +): void { + const filterKeys = (obj: T): Partial => { + const filtered = { ...obj } + for (const key of ignoreKeys) { + delete filtered[key] + } + return filtered + } + + const filteredActual = filterKeys(actual) + const filteredExpected = filterKeys(expected) + + expect(filteredActual).toEqual(filteredExpected) +} + +/** + * Creates a provider mock that simulates rate limiting + */ +export function createRateLimitedProvider(limitPerSecond: number) { + const calls: number[] = [] + + return createMockLanguageModel({ + doGenerate: vi.fn().mockImplementation(async () => { + const now = Date.now() + calls.push(now) + + // Remove calls older than 1 second + const recentCalls = calls.filter((time) => now - time < 1000) + + if (recentCalls.length > limitPerSecond) { + throw new Error('Rate limit exceeded') + } + + return { + text: 'Rate limited response', + finishReason: 'stop' as const, + usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 }, + rawCall: { rawPrompt: null, rawSettings: {} }, + rawResponse: { headers: {} }, + warnings: [] + } + }) + }) +} + +/** + * Validates streaming response structure + */ +export function validateStreamChunk(chunk: any): void { + expect(chunk).toBeDefined() + expect(chunk).toHaveProperty('type') + + if (chunk.type === 'text-delta') { + expect(chunk).toHaveProperty('textDelta') + expect(typeof chunk.textDelta).toBe('string') + } else if (chunk.type === 'finish') { + expect(chunk).toHaveProperty('finishReason') + expect(chunk).toHaveProperty('usage') + } else if (chunk.type === 'tool-call') { + expect(chunk).toHaveProperty('toolCallId') + expect(chunk).toHaveProperty('toolName') + expect(chunk).toHaveProperty('args') + } +} + +/** + * Creates a test logger that captures log messages + */ +export function createTestLogger() { + const logs: Array<{ level: string; message: string; meta?: any }> = [] + + return { + info: (message: string, meta?: any) => logs.push({ level: 'info', message, meta }), + warn: (message: string, meta?: any) => logs.push({ level: 'warn', message, meta }), + error: (message: string, meta?: any) => logs.push({ level: 'error', message, meta }), + debug: (message: string, meta?: any) => logs.push({ level: 'debug', message, meta }), + getLogs: () => logs, + clear: () => { + logs.length = 0 + } + } +} diff --git a/packages/aiCore/src/__tests__/index.ts b/packages/aiCore/src/__tests__/index.ts new file mode 100644 index 0000000000..23ecd167a4 --- /dev/null +++ b/packages/aiCore/src/__tests__/index.ts @@ -0,0 +1,12 @@ +/** + * Test Infrastructure Exports + * Central export point for all test utilities, fixtures, and helpers + */ + +// Fixtures +export * from './fixtures/mock-providers' +export * from './fixtures/mock-responses' + +// Helpers +export * from './helpers/provider-test-utils' +export * from './helpers/test-utils' diff --git a/packages/aiCore/src/__tests__/mocks/ai-sdk-provider.ts b/packages/aiCore/src/__tests__/mocks/ai-sdk-provider.ts new file mode 100644 index 0000000000..57dcdd0fd1 --- /dev/null +++ b/packages/aiCore/src/__tests__/mocks/ai-sdk-provider.ts @@ -0,0 +1,35 @@ +/** + * Mock for @cherrystudio/ai-sdk-provider + * This mock is used in tests to avoid importing the actual package + */ + +export type CherryInProviderSettings = { + apiKey?: string + baseURL?: string +} + +// oxlint-disable-next-line no-unused-vars +export const createCherryIn = (_options?: CherryInProviderSettings) => ({ + // oxlint-disable-next-line no-unused-vars + languageModel: (_modelId: string) => ({ + specificationVersion: 'v1', + provider: 'cherryin', + modelId: 'mock-model', + doGenerate: async () => ({ text: 'mock response' }), + doStream: async () => ({ stream: (async function* () {})() }) + }), + // oxlint-disable-next-line no-unused-vars + chat: (_modelId: string) => ({ + specificationVersion: 'v1', + provider: 'cherryin-chat', + modelId: 'mock-model', + doGenerate: async () => ({ text: 'mock response' }), + doStream: async () => ({ stream: (async function* () {})() }) + }), + // oxlint-disable-next-line no-unused-vars + textEmbeddingModel: (_modelId: string) => ({ + specificationVersion: 'v1', + provider: 'cherryin', + modelId: 'mock-embedding-model' + }) +}) diff --git a/packages/aiCore/src/__tests__/setup.ts b/packages/aiCore/src/__tests__/setup.ts new file mode 100644 index 0000000000..1e35458ad6 --- /dev/null +++ b/packages/aiCore/src/__tests__/setup.ts @@ -0,0 +1,9 @@ +/** + * Vitest Setup File + * Global test configuration and mocks for @cherrystudio/ai-core package + */ + +// Mock Vite SSR helper to avoid Node environment errors +;(globalThis as any).__vite_ssr_exportName__ = (_name: string, value: any) => value + +// Note: @cherrystudio/ai-sdk-provider is mocked via alias in vitest.config.ts diff --git a/packages/aiCore/src/core/options/__tests__/factory.test.ts b/packages/aiCore/src/core/options/__tests__/factory.test.ts new file mode 100644 index 0000000000..86f8017818 --- /dev/null +++ b/packages/aiCore/src/core/options/__tests__/factory.test.ts @@ -0,0 +1,109 @@ +import { describe, expect, it } from 'vitest' + +import { createOpenAIOptions, createOpenRouterOptions, mergeProviderOptions } from '../factory' + +describe('mergeProviderOptions', () => { + it('deep merges provider options for the same provider', () => { + const reasoningOptions = createOpenRouterOptions({ + reasoning: { + enabled: true, + effort: 'medium' + } + }) + const webSearchOptions = createOpenRouterOptions({ + plugins: [{ id: 'web', max_results: 5 }] + }) + + const merged = mergeProviderOptions(reasoningOptions, webSearchOptions) + + expect(merged.openrouter).toEqual({ + reasoning: { + enabled: true, + effort: 'medium' + }, + plugins: [{ id: 'web', max_results: 5 }] + }) + }) + + it('preserves options from other providers while merging', () => { + const openRouter = createOpenRouterOptions({ + reasoning: { enabled: true } + }) + const openAI = createOpenAIOptions({ + reasoningEffort: 'low' + }) + const merged = mergeProviderOptions(openRouter, openAI) + + expect(merged.openrouter).toEqual({ reasoning: { enabled: true } }) + expect(merged.openai).toEqual({ reasoningEffort: 'low' }) + }) + + it('overwrites primitive values with later values', () => { + const first = createOpenAIOptions({ + reasoningEffort: 'low', + user: 'user-123' + }) + const second = createOpenAIOptions({ + reasoningEffort: 'high', + maxToolCalls: 5 + }) + + const merged = mergeProviderOptions(first, second) + + expect(merged.openai).toEqual({ + reasoningEffort: 'high', // overwritten by second + user: 'user-123', // preserved from first + maxToolCalls: 5 // added from second + }) + }) + + it('overwrites arrays with later values instead of merging', () => { + const first = createOpenRouterOptions({ + models: ['gpt-4', 'gpt-3.5-turbo'] + }) + const second = createOpenRouterOptions({ + models: ['claude-3-opus', 'claude-3-sonnet'] + }) + + const merged = mergeProviderOptions(first, second) + + // Array is completely replaced, not merged + expect(merged.openrouter?.models).toEqual(['claude-3-opus', 'claude-3-sonnet']) + }) + + it('deeply merges nested objects while overwriting primitives', () => { + const first = createOpenRouterOptions({ + reasoning: { + enabled: true, + effort: 'low' + }, + user: 'user-123' + }) + const second = createOpenRouterOptions({ + reasoning: { + effort: 'high', + max_tokens: 500 + }, + user: 'user-456' + }) + + const merged = mergeProviderOptions(first, second) + + expect(merged.openrouter).toEqual({ + reasoning: { + enabled: true, // preserved from first + effort: 'high', // overwritten by second + max_tokens: 500 // added from second + }, + user: 'user-456' // overwritten by second + }) + }) + + it('replaces arrays instead of merging them', () => { + const first = createOpenRouterOptions({ plugins: [{ id: 'old' }] }) + const second = createOpenRouterOptions({ plugins: [{ id: 'new' }] }) + const merged = mergeProviderOptions(first, second) + // @ts-expect-error type-check for openrouter options is skipped. see function signature of createOpenRouterOptions + expect(merged.openrouter?.plugins).toEqual([{ id: 'new' }]) + }) +}) diff --git a/packages/aiCore/src/core/options/factory.ts b/packages/aiCore/src/core/options/factory.ts index ecd53e6330..1e493b2337 100644 --- a/packages/aiCore/src/core/options/factory.ts +++ b/packages/aiCore/src/core/options/factory.ts @@ -26,13 +26,65 @@ export function createGenericProviderOptions( return { [provider]: options } as Record> } +type PlainObject = Record + +const isPlainObject = (value: unknown): value is PlainObject => { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function deepMergeObjects(target: T, source: PlainObject): T { + const result: PlainObject = { ...target } + Object.entries(source).forEach(([key, value]) => { + if (isPlainObject(value) && isPlainObject(result[key])) { + result[key] = deepMergeObjects(result[key], value) + } else { + result[key] = value + } + }) + return result as T +} + /** - * 合并多个供应商的options - * @param optionsMap 包含多个供应商选项的对象 - * @returns 合并后的TypedProviderOptions + * Deep-merge multiple provider-specific options. + * Nested objects are recursively merged; primitive values are overwritten. + * + * When the same key appears in multiple options: + * - If both values are plain objects: they are deeply merged (recursive merge) + * - If values are primitives/arrays: the later value overwrites the earlier one + * + * @example + * mergeProviderOptions( + * { openrouter: { reasoning: { enabled: true, effort: 'low' }, user: 'user-123' } }, + * { openrouter: { reasoning: { effort: 'high', max_tokens: 500 }, models: ['gpt-4'] } } + * ) + * // Result: { + * // openrouter: { + * // reasoning: { enabled: true, effort: 'high', max_tokens: 500 }, + * // user: 'user-123', + * // models: ['gpt-4'] + * // } + * // } + * + * @param optionsMap Objects containing options for multiple providers + * @returns Fully merged TypedProviderOptions */ export function mergeProviderOptions(...optionsMap: Partial[]): TypedProviderOptions { - return Object.assign({}, ...optionsMap) + return optionsMap.reduce((acc, options) => { + if (!options) { + return acc + } + Object.entries(options).forEach(([providerId, providerOptions]) => { + if (!providerOptions) { + return + } + if (acc[providerId]) { + acc[providerId] = deepMergeObjects(acc[providerId] as PlainObject, providerOptions as PlainObject) + } else { + acc[providerId] = providerOptions as any + } + }) + return acc + }, {} as TypedProviderOptions) } /** diff --git a/packages/aiCore/src/core/plugins/built-in/index.ts b/packages/aiCore/src/core/plugins/built-in/index.ts index 1f8916b09a..d7f35d0cd1 100644 --- a/packages/aiCore/src/core/plugins/built-in/index.ts +++ b/packages/aiCore/src/core/plugins/built-in/index.ts @@ -4,12 +4,7 @@ */ export const BUILT_IN_PLUGIN_PREFIX = 'built-in:' -export { googleToolsPlugin } from './googleToolsPlugin' -export { createLoggingPlugin } from './logging' -export { createPromptToolUsePlugin } from './toolUsePlugin/promptToolUsePlugin' -export type { - PromptToolUseConfig, - ToolUseRequestContext, - ToolUseResult -} from './toolUsePlugin/type' -export { webSearchPlugin, type WebSearchPluginConfig } from './webSearchPlugin' +export * from './googleToolsPlugin' +export * from './toolUsePlugin/promptToolUsePlugin' +export * from './toolUsePlugin/type' +export * from './webSearchPlugin' diff --git a/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/StreamEventManager.ts b/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/StreamEventManager.ts index 59a425712c..c30c2015f6 100644 --- a/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/StreamEventManager.ts +++ b/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/StreamEventManager.ts @@ -62,7 +62,7 @@ export class StreamEventManager { const recursiveResult = await context.recursiveCall(recursiveParams) if (recursiveResult && recursiveResult.fullStream) { - await this.pipeRecursiveStream(controller, recursiveResult.fullStream, context) + await this.pipeRecursiveStream(controller, recursiveResult.fullStream) } else { console.warn('[MCP Prompt] No fullstream found in recursive result:', recursiveResult) } @@ -74,11 +74,7 @@ export class StreamEventManager { /** * 将递归流的数据传递到当前流 */ - private async pipeRecursiveStream( - controller: StreamController, - recursiveStream: ReadableStream, - context?: AiRequestContext - ): Promise { + private async pipeRecursiveStream(controller: StreamController, recursiveStream: ReadableStream): Promise { const reader = recursiveStream.getReader() try { while (true) { @@ -86,18 +82,14 @@ export class StreamEventManager { if (done) { break } + if (value.type === 'start') { + continue + } + if (value.type === 'finish') { - // 迭代的流不发finish,但需要累加其 usage - if (value.usage && context?.accumulatedUsage) { - this.accumulateUsage(context.accumulatedUsage, value.usage) - } break } - // 对于 finish-step 类型,累加其 usage - if (value.type === 'finish-step' && value.usage && context?.accumulatedUsage) { - this.accumulateUsage(context.accumulatedUsage, value.usage) - } - // 将递归流的数据传递到当前流 + controller.enqueue(value) } } finally { @@ -135,10 +127,8 @@ export class StreamEventManager { // 构建新的对话消息 const newMessages: ModelMessage[] = [ ...(context.originalParams.messages || []), - { - role: 'assistant', - content: textBuffer - }, + // 只有当 textBuffer 有内容时才添加 assistant 消息,避免空消息导致 API 错误 + ...(textBuffer ? [{ role: 'assistant' as const, content: textBuffer }] : []), { role: 'user', content: toolResultsText @@ -161,7 +151,7 @@ export class StreamEventManager { /** * 累加 usage 数据 */ - private accumulateUsage(target: any, source: any): void { + accumulateUsage(target: any, source: any): void { if (!target || !source) return // 累加各种 token 类型 diff --git a/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/promptToolUsePlugin.ts b/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/promptToolUsePlugin.ts index 274fdcee5c..22e8b5a605 100644 --- a/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/promptToolUsePlugin.ts +++ b/packages/aiCore/src/core/plugins/built-in/toolUsePlugin/promptToolUsePlugin.ts @@ -411,7 +411,10 @@ export const createPromptToolUsePlugin = (config: PromptToolUseConfig = {}) => { } } - // 如果没有执行工具调用,直接传递原始finish-step事件 + // 如果没有执行工具调用,累加 usage 后透传 finish-step 事件 + if (chunk.usage && context.accumulatedUsage) { + streamEventManager.accumulateUsage(context.accumulatedUsage, chunk.usage) + } controller.enqueue(chunk) // 清理状态 diff --git a/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/helper.ts b/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/helper.ts index a50356130d..6e313bdd27 100644 --- a/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/helper.ts +++ b/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/helper.ts @@ -6,6 +6,7 @@ import { type Tool } from 'ai' import { createOpenRouterOptions, createXaiOptions, mergeProviderOptions } from '../../../options' import type { ProviderOptionsMap } from '../../../options/types' +import type { AiRequestContext } from '../../' import type { OpenRouterSearchConfig } from './openrouter' /** @@ -35,7 +36,6 @@ export interface WebSearchPluginConfig { anthropic?: AnthropicSearchConfig xai?: ProviderOptionsMap['xai']['searchParameters'] google?: GoogleSearchConfig - 'google-vertex'?: GoogleSearchConfig openrouter?: OpenRouterSearchConfig } @@ -44,7 +44,6 @@ export interface WebSearchPluginConfig { */ export const DEFAULT_WEB_SEARCH_CONFIG: WebSearchPluginConfig = { google: {}, - 'google-vertex': {}, openai: {}, 'openai-chat': {}, xai: { @@ -97,55 +96,84 @@ export type WebSearchToolInputSchema = { 'openai-chat': InferToolInput } -export const switchWebSearchTool = (providerId: string, config: WebSearchPluginConfig, params: any) => { - switch (providerId) { - case 'openai': { - if (config.openai) { - if (!params.tools) params.tools = {} - params.tools.web_search = openai.tools.webSearch(config.openai) - } - break - } - case 'openai-chat': { - if (config['openai-chat']) { - if (!params.tools) params.tools = {} - params.tools.web_search_preview = openai.tools.webSearchPreview(config['openai-chat']) - } - break - } +/** + * Helper function to ensure params.tools object exists + */ +const ensureToolsObject = (params: any) => { + if (!params.tools) params.tools = {} +} - case 'anthropic': { - if (config.anthropic) { - if (!params.tools) params.tools = {} - params.tools.web_search = anthropic.tools.webSearch_20250305(config.anthropic) - } - break - } +/** + * Helper function to apply tool-based web search configuration + */ +const applyToolBasedSearch = (params: any, toolName: string, toolInstance: any) => { + ensureToolsObject(params) + params.tools[toolName] = toolInstance +} - case 'google': { - // case 'google-vertex': - if (!params.tools) params.tools = {} - params.tools.web_search = google.tools.googleSearch(config.google || {}) - break - } +/** + * Helper function to apply provider options-based web search configuration + */ +const applyProviderOptionsSearch = (params: any, searchOptions: any) => { + params.providerOptions = mergeProviderOptions(params.providerOptions, searchOptions) +} - case 'xai': { - if (config.xai) { - const searchOptions = createXaiOptions({ - searchParameters: { ...config.xai, mode: 'on' } - }) - params.providerOptions = mergeProviderOptions(params.providerOptions, searchOptions) - } - break - } +export const switchWebSearchTool = (config: WebSearchPluginConfig, params: any, context?: AiRequestContext) => { + const providerId = context?.providerId - case 'openrouter': { - if (config.openrouter) { - const searchOptions = createOpenRouterOptions(config.openrouter) - params.providerOptions = mergeProviderOptions(params.providerOptions, searchOptions) - } + // Provider-specific configuration map + const providerHandlers: Record void> = { + openai: () => { + const cfg = config.openai ?? DEFAULT_WEB_SEARCH_CONFIG.openai + applyToolBasedSearch(params, 'web_search', openai.tools.webSearch(cfg)) + }, + 'openai-chat': () => { + const cfg = (config['openai-chat'] ?? DEFAULT_WEB_SEARCH_CONFIG['openai-chat']) as OpenAISearchPreviewConfig + applyToolBasedSearch(params, 'web_search_preview', openai.tools.webSearchPreview(cfg)) + }, + anthropic: () => { + const cfg = config.anthropic ?? DEFAULT_WEB_SEARCH_CONFIG.anthropic + applyToolBasedSearch(params, 'web_search', anthropic.tools.webSearch_20250305(cfg)) + }, + google: () => { + const cfg = (config.google ?? DEFAULT_WEB_SEARCH_CONFIG.google) as GoogleSearchConfig + applyToolBasedSearch(params, 'web_search', google.tools.googleSearch(cfg)) + }, + xai: () => { + const cfg = config.xai ?? DEFAULT_WEB_SEARCH_CONFIG.xai + const searchOptions = createXaiOptions({ searchParameters: { ...cfg, mode: 'on' } }) + applyProviderOptionsSearch(params, searchOptions) + }, + openrouter: () => { + const cfg = (config.openrouter ?? DEFAULT_WEB_SEARCH_CONFIG.openrouter) as OpenRouterSearchConfig + const searchOptions = createOpenRouterOptions(cfg) + applyProviderOptionsSearch(params, searchOptions) + } + } + + // Try provider-specific handler first + const handler = providerId && providerHandlers[providerId] + if (handler) { + handler() + return params + } + + // Fallback: apply based on available config keys (prioritized order) + const fallbackOrder: Array = [ + 'openai', + 'openai-chat', + 'anthropic', + 'google', + 'xai', + 'openrouter' + ] + + for (const key of fallbackOrder) { + if (config[key]) { + providerHandlers[key]() break } } + return params } diff --git a/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/index.ts b/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/index.ts index 23ea952323..e02fd179fe 100644 --- a/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/index.ts +++ b/packages/aiCore/src/core/plugins/built-in/webSearchPlugin/index.ts @@ -4,7 +4,6 @@ */ import { definePlugin } from '../../' -import type { AiRequestContext } from '../../types' import type { WebSearchPluginConfig } from './helper' import { DEFAULT_WEB_SEARCH_CONFIG, switchWebSearchTool } from './helper' @@ -18,21 +17,28 @@ export const webSearchPlugin = (config: WebSearchPluginConfig = DEFAULT_WEB_SEAR name: 'webSearch', enforce: 'pre', - transformParams: async (params: any, context: AiRequestContext) => { - const { providerId } = context - switchWebSearchTool(providerId, config, params) + transformParams: async (params: any, context) => { + let { providerId } = context + // For cherryin providers, extract the actual provider from the model's provider string + // Expected format: "cherryin.{actualProvider}" (e.g., "cherryin.gemini") if (providerId === 'cherryin' || providerId === 'cherryin-chat') { - // cherryin.gemini - const _providerId = params.model.provider.split('.')[1] - switchWebSearchTool(_providerId, config, params) + const provider = params.model?.provider + if (provider && typeof provider === 'string' && provider.includes('.')) { + const extractedProviderId = provider.split('.')[1] + if (extractedProviderId) { + providerId = extractedProviderId + } + } } + + switchWebSearchTool(config, params, { ...context, providerId }) return params } }) // 导出类型定义供开发者使用 -export type { WebSearchPluginConfig, WebSearchToolOutputSchema } from './helper' +export * from './helper' // 默认导出 export default webSearchPlugin diff --git a/packages/aiCore/src/core/providers/__tests__/schemas.test.ts b/packages/aiCore/src/core/providers/__tests__/schemas.test.ts index 82b390ba05..02fe21889a 100644 --- a/packages/aiCore/src/core/providers/__tests__/schemas.test.ts +++ b/packages/aiCore/src/core/providers/__tests__/schemas.test.ts @@ -19,15 +19,20 @@ describe('Provider Schemas', () => { expect(Array.isArray(baseProviders)).toBe(true) expect(baseProviders.length).toBeGreaterThan(0) + // These are the actual base providers defined in schemas.ts const expectedIds = [ 'openai', - 'openai-responses', + 'openai-chat', 'openai-compatible', 'anthropic', 'google', 'xai', 'azure', - 'deepseek' + 'azure-responses', + 'deepseek', + 'openrouter', + 'cherryin', + 'cherryin-chat' ] const actualIds = baseProviders.map((p) => p.id) expectedIds.forEach((id) => { diff --git a/packages/aiCore/src/core/providers/index.ts b/packages/aiCore/src/core/providers/index.ts index 3ac445cb22..b9ebd6f682 100644 --- a/packages/aiCore/src/core/providers/index.ts +++ b/packages/aiCore/src/core/providers/index.ts @@ -44,7 +44,7 @@ export { // ==================== 基础数据和类型 ==================== // 基础Provider数据源 -export { baseProviderIds, baseProviders } from './schemas' +export { baseProviderIds, baseProviders, isBaseProvider } from './schemas' // 类型定义和Schema export type { diff --git a/packages/aiCore/src/core/providers/schemas.ts b/packages/aiCore/src/core/providers/schemas.ts index 778b1b705a..43a370af9b 100644 --- a/packages/aiCore/src/core/providers/schemas.ts +++ b/packages/aiCore/src/core/providers/schemas.ts @@ -7,7 +7,6 @@ import { createAzure } from '@ai-sdk/azure' import { type AzureOpenAIProviderSettings } from '@ai-sdk/azure' import { createDeepSeek } from '@ai-sdk/deepseek' import { createGoogleGenerativeAI } from '@ai-sdk/google' -import { createHuggingFace } from '@ai-sdk/huggingface' import { createOpenAI, type OpenAIProviderSettings } from '@ai-sdk/openai' import { createOpenAICompatible } from '@ai-sdk/openai-compatible' import type { LanguageModelV2 } from '@ai-sdk/provider' @@ -33,8 +32,7 @@ export const baseProviderIds = [ 'deepseek', 'openrouter', 'cherryin', - 'cherryin-chat', - 'huggingface' + 'cherryin-chat' ] as const /** @@ -158,12 +156,6 @@ export const baseProviders = [ }) }, supportsImageGeneration: true - }, - { - id: 'huggingface', - name: 'HuggingFace', - creator: createHuggingFace, - supportsImageGeneration: true } ] as const satisfies BaseProvider[] diff --git a/packages/aiCore/src/core/runtime/__tests__/generateImage.test.ts b/packages/aiCore/src/core/runtime/__tests__/generateImage.test.ts index 217319aacc..56ab87dbcc 100644 --- a/packages/aiCore/src/core/runtime/__tests__/generateImage.test.ts +++ b/packages/aiCore/src/core/runtime/__tests__/generateImage.test.ts @@ -232,11 +232,13 @@ describe('RuntimeExecutor.generateImage', () => { expect(pluginCallOrder).toEqual(['onRequestStart', 'transformParams', 'transformResult', 'onRequestEnd']) + // transformParams receives params without model (model is handled separately) + // and context with core fields + dynamic fields (requestId, startTime, etc.) expect(testPlugin.transformParams).toHaveBeenCalledWith( - { prompt: 'A test image' }, + expect.objectContaining({ prompt: 'A test image' }), expect.objectContaining({ providerId: 'openai', - modelId: 'dall-e-3' + model: 'dall-e-3' }) ) @@ -273,11 +275,12 @@ describe('RuntimeExecutor.generateImage', () => { await executorWithPlugin.generateImage({ model: 'dall-e-3', prompt: 'A test image' }) + // resolveModel receives model id and context with core fields expect(modelResolutionPlugin.resolveModel).toHaveBeenCalledWith( 'dall-e-3', expect.objectContaining({ providerId: 'openai', - modelId: 'dall-e-3' + model: 'dall-e-3' }) ) @@ -339,12 +342,11 @@ describe('RuntimeExecutor.generateImage', () => { .generateImage({ model: 'invalid-model', prompt: 'A test image' }) .catch((error) => error) - expect(thrownError).toBeInstanceOf(ImageGenerationError) - expect(thrownError.message).toContain('Failed to generate image:') + // Error is thrown from pluginEngine directly as ImageModelResolutionError + expect(thrownError).toBeInstanceOf(ImageModelResolutionError) + expect(thrownError.message).toContain('Failed to resolve image model: invalid-model') expect(thrownError.providerId).toBe('openai') expect(thrownError.modelId).toBe('invalid-model') - expect(thrownError.cause).toBeInstanceOf(ImageModelResolutionError) - expect(thrownError.cause.message).toContain('Failed to resolve image model: invalid-model') }) it('should handle ImageModelResolutionError without provider', async () => { @@ -362,8 +364,9 @@ describe('RuntimeExecutor.generateImage', () => { const apiError = new Error('API request failed') vi.mocked(aiGenerateImage).mockRejectedValue(apiError) + // Error propagates directly from pluginEngine without wrapping await expect(executor.generateImage({ model: 'dall-e-3', prompt: 'A test image' })).rejects.toThrow( - 'Failed to generate image:' + 'API request failed' ) }) @@ -376,8 +379,9 @@ describe('RuntimeExecutor.generateImage', () => { vi.mocked(aiGenerateImage).mockRejectedValue(noImageError) vi.mocked(NoImageGeneratedError.isInstance).mockReturnValue(true) + // Error propagates directly from pluginEngine await expect(executor.generateImage({ model: 'dall-e-3', prompt: 'A test image' })).rejects.toThrow( - 'Failed to generate image:' + 'No image generated' ) }) @@ -398,15 +402,17 @@ describe('RuntimeExecutor.generateImage', () => { [errorPlugin] ) + // Error propagates directly from pluginEngine await expect(executorWithPlugin.generateImage({ model: 'dall-e-3', prompt: 'A test image' })).rejects.toThrow( - 'Failed to generate image:' + 'Generation failed' ) + // onError receives the original error and context with core fields expect(errorPlugin.onError).toHaveBeenCalledWith( error, expect.objectContaining({ providerId: 'openai', - modelId: 'dall-e-3' + model: 'dall-e-3' }) ) }) @@ -419,9 +425,10 @@ describe('RuntimeExecutor.generateImage', () => { const abortController = new AbortController() setTimeout(() => abortController.abort(), 10) + // Error propagates directly from pluginEngine await expect( executor.generateImage({ model: 'dall-e-3', prompt: 'A test image', abortSignal: abortController.signal }) - ).rejects.toThrow('Failed to generate image:') + ).rejects.toThrow('Operation was aborted') }) }) diff --git a/packages/aiCore/src/core/runtime/__tests__/generateText.test.ts b/packages/aiCore/src/core/runtime/__tests__/generateText.test.ts new file mode 100644 index 0000000000..cb1d1d671a --- /dev/null +++ b/packages/aiCore/src/core/runtime/__tests__/generateText.test.ts @@ -0,0 +1,504 @@ +/** + * RuntimeExecutor.generateText Comprehensive Tests + * Tests non-streaming text generation across all providers with various parameters + */ + +import { generateText } from 'ai' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { + createMockLanguageModel, + mockCompleteResponses, + mockProviderConfigs, + testMessages, + testTools +} from '../../../__tests__' +import type { AiPlugin } from '../../plugins' +import { globalRegistryManagement } from '../../providers/RegistryManagement' +import { RuntimeExecutor } from '../executor' + +// Mock AI SDK - use importOriginal to keep jsonSchema and other non-mocked exports +vi.mock('ai', async (importOriginal) => { + const actual = (await importOriginal()) as Record + return { + ...actual, + generateText: vi.fn() + } +}) + +vi.mock('../../providers/RegistryManagement', () => ({ + globalRegistryManagement: { + languageModel: vi.fn() + }, + DEFAULT_SEPARATOR: '|' +})) + +describe('RuntimeExecutor.generateText', () => { + let executor: RuntimeExecutor<'openai'> + let mockLanguageModel: any + + beforeEach(() => { + vi.clearAllMocks() + + executor = RuntimeExecutor.create('openai', mockProviderConfigs.openai) + + mockLanguageModel = createMockLanguageModel({ + provider: 'openai', + modelId: 'gpt-4' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(mockLanguageModel) + vi.mocked(generateText).mockResolvedValue(mockCompleteResponses.simple as any) + }) + + describe('Basic Functionality', () => { + it('should generate text with minimal parameters', async () => { + const result = await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + expect(generateText).toHaveBeenCalledWith({ + model: mockLanguageModel, + messages: testMessages.simple + }) + + expect(result.text).toBe('This is a simple response.') + expect(result.finishReason).toBe('stop') + expect(result.usage).toBeDefined() + }) + + it('should generate with system messages', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.withSystem + }) + + expect(generateText).toHaveBeenCalledWith({ + model: mockLanguageModel, + messages: testMessages.withSystem + }) + }) + + it('should generate with conversation history', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.conversation + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + messages: testMessages.conversation + }) + ) + }) + }) + + describe('All Parameter Combinations', () => { + it('should support all parameters together', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + temperature: 0.7, + maxOutputTokens: 500, + topP: 0.9, + frequencyPenalty: 0.5, + presencePenalty: 0.3, + stopSequences: ['STOP'], + seed: 12345 + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature: 0.7, + maxOutputTokens: 500, + topP: 0.9, + frequencyPenalty: 0.5, + presencePenalty: 0.3, + stopSequences: ['STOP'], + seed: 12345 + }) + ) + }) + + it('should support partial parameters', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + temperature: 0.5, + maxOutputTokens: 100 + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature: 0.5, + maxOutputTokens: 100 + }) + ) + }) + }) + + describe('Tool Calling', () => { + beforeEach(() => { + vi.mocked(generateText).mockResolvedValue(mockCompleteResponses.withToolCalls as any) + }) + + it('should support tool calling', async () => { + const result = await executor.generateText({ + model: 'gpt-4', + messages: testMessages.toolUse, + tools: testTools + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + tools: testTools + }) + ) + + expect(result.toolCalls).toBeDefined() + expect(result.toolCalls).toHaveLength(1) + }) + + it('should support toolChoice auto', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.toolUse, + tools: testTools, + toolChoice: 'auto' + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + toolChoice: 'auto' + }) + ) + }) + + it('should support toolChoice required', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.toolUse, + tools: testTools, + toolChoice: 'required' + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + toolChoice: 'required' + }) + ) + }) + + it('should support toolChoice none', async () => { + vi.mocked(generateText).mockResolvedValue(mockCompleteResponses.simple as any) + + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + tools: testTools, + toolChoice: 'none' + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + toolChoice: 'none' + }) + ) + }) + + it('should support specific tool selection', async () => { + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.toolUse, + tools: testTools, + toolChoice: { + type: 'tool', + toolName: 'getWeather' + } + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + toolChoice: { + type: 'tool', + toolName: 'getWeather' + } + }) + ) + }) + }) + + describe('Multiple Providers', () => { + it('should work with Anthropic provider', async () => { + const anthropicExecutor = RuntimeExecutor.create('anthropic', mockProviderConfigs.anthropic) + + const anthropicModel = createMockLanguageModel({ + provider: 'anthropic', + modelId: 'claude-3-5-sonnet-20241022' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(anthropicModel) + + await anthropicExecutor.generateText({ + model: 'claude-3-5-sonnet-20241022', + messages: testMessages.simple + }) + + expect(globalRegistryManagement.languageModel).toHaveBeenCalledWith('anthropic|claude-3-5-sonnet-20241022') + }) + + it('should work with Google provider', async () => { + const googleExecutor = RuntimeExecutor.create('google', mockProviderConfigs.google) + + const googleModel = createMockLanguageModel({ + provider: 'google', + modelId: 'gemini-2.0-flash-exp' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(googleModel) + + await googleExecutor.generateText({ + model: 'gemini-2.0-flash-exp', + messages: testMessages.simple + }) + + expect(globalRegistryManagement.languageModel).toHaveBeenCalledWith('google|gemini-2.0-flash-exp') + }) + + it('should work with xAI provider', async () => { + const xaiExecutor = RuntimeExecutor.create('xai', mockProviderConfigs.xai) + + const xaiModel = createMockLanguageModel({ + provider: 'xai', + modelId: 'grok-2-latest' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(xaiModel) + + await xaiExecutor.generateText({ + model: 'grok-2-latest', + messages: testMessages.simple + }) + + expect(globalRegistryManagement.languageModel).toHaveBeenCalledWith('xai|grok-2-latest') + }) + + it('should work with DeepSeek provider', async () => { + const deepseekExecutor = RuntimeExecutor.create('deepseek', mockProviderConfigs.deepseek) + + const deepseekModel = createMockLanguageModel({ + provider: 'deepseek', + modelId: 'deepseek-chat' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(deepseekModel) + + await deepseekExecutor.generateText({ + model: 'deepseek-chat', + messages: testMessages.simple + }) + + expect(globalRegistryManagement.languageModel).toHaveBeenCalledWith('deepseek|deepseek-chat') + }) + }) + + describe('Plugin Integration', () => { + it('should execute all plugin hooks', async () => { + const pluginCalls: string[] = [] + + const testPlugin: AiPlugin = { + name: 'test-plugin', + onRequestStart: vi.fn(async () => { + pluginCalls.push('onRequestStart') + }), + transformParams: vi.fn(async (params) => { + pluginCalls.push('transformParams') + return { ...params, temperature: 0.8 } + }), + transformResult: vi.fn(async (result) => { + pluginCalls.push('transformResult') + return { ...result, text: result.text + ' [modified]' } + }), + onRequestEnd: vi.fn(async () => { + pluginCalls.push('onRequestEnd') + }) + } + + const executorWithPlugin = RuntimeExecutor.create('openai', mockProviderConfigs.openai, [testPlugin]) + + const result = await executorWithPlugin.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + expect(pluginCalls).toEqual(['onRequestStart', 'transformParams', 'transformResult', 'onRequestEnd']) + + // Verify transformed parameters + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature: 0.8 + }) + ) + + // Verify transformed result + expect(result.text).toContain('[modified]') + }) + + it('should handle multiple plugins in order', async () => { + const pluginOrder: string[] = [] + + const plugin1: AiPlugin = { + name: 'plugin-1', + transformParams: vi.fn(async (params) => { + pluginOrder.push('plugin-1') + return { ...params, temperature: 0.5 } + }) + } + + const plugin2: AiPlugin = { + name: 'plugin-2', + transformParams: vi.fn(async (params) => { + pluginOrder.push('plugin-2') + return { ...params, maxTokens: 200 } + }) + } + + const executorWithPlugins = RuntimeExecutor.create('openai', mockProviderConfigs.openai, [plugin1, plugin2]) + + await executorWithPlugins.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + expect(pluginOrder).toEqual(['plugin-1', 'plugin-2']) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature: 0.5, + maxTokens: 200 + }) + ) + }) + }) + + describe('Error Handling', () => { + it('should handle API errors', async () => { + const error = new Error('API request failed') + vi.mocked(generateText).mockRejectedValue(error) + + await expect( + executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + ).rejects.toThrow('API request failed') + }) + + it('should execute onError plugin hook', async () => { + const error = new Error('Generation failed') + vi.mocked(generateText).mockRejectedValue(error) + + const errorPlugin: AiPlugin = { + name: 'error-handler', + onError: vi.fn() + } + + const executorWithPlugin = RuntimeExecutor.create('openai', mockProviderConfigs.openai, [errorPlugin]) + + await expect( + executorWithPlugin.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + ).rejects.toThrow('Generation failed') + + // onError receives the original error and context with core fields + expect(errorPlugin.onError).toHaveBeenCalledWith( + error, + expect.objectContaining({ + providerId: 'openai', + model: 'gpt-4' + }) + ) + }) + + it('should handle model not found error', async () => { + const error = new Error('Model not found: invalid-model') + vi.mocked(globalRegistryManagement.languageModel).mockImplementation(() => { + throw error + }) + + await expect( + executor.generateText({ + model: 'invalid-model', + messages: testMessages.simple + }) + ).rejects.toThrow('Model not found') + }) + }) + + describe('Usage and Metadata', () => { + it('should return usage information', async () => { + const result = await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + expect(result.usage).toBeDefined() + expect(result.usage.inputTokens).toBe(15) + expect(result.usage.outputTokens).toBe(8) + expect(result.usage.totalTokens).toBe(23) + }) + + it('should handle warnings', async () => { + vi.mocked(generateText).mockResolvedValue(mockCompleteResponses.withWarnings as any) + + const result = await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + temperature: 2.5 // Unsupported value + }) + + expect(result.warnings).toBeDefined() + expect(result.warnings).toHaveLength(1) + expect(result.warnings![0].type).toBe('unsupported-setting') + }) + }) + + describe('Abort Signal', () => { + it('should support abort signal', async () => { + const abortController = new AbortController() + + await executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + abortSignal: abortController.signal + }) + + expect(generateText).toHaveBeenCalledWith( + expect.objectContaining({ + abortSignal: abortController.signal + }) + ) + }) + + it('should handle aborted request', async () => { + const abortError = new Error('Request aborted') + abortError.name = 'AbortError' + + vi.mocked(generateText).mockRejectedValue(abortError) + + const abortController = new AbortController() + abortController.abort() + + await expect( + executor.generateText({ + model: 'gpt-4', + messages: testMessages.simple, + abortSignal: abortController.signal + }) + ).rejects.toThrow('Request aborted') + }) + }) +}) diff --git a/packages/aiCore/src/core/runtime/__tests__/streamText.test.ts b/packages/aiCore/src/core/runtime/__tests__/streamText.test.ts new file mode 100644 index 0000000000..49253594cc --- /dev/null +++ b/packages/aiCore/src/core/runtime/__tests__/streamText.test.ts @@ -0,0 +1,531 @@ +/** + * RuntimeExecutor.streamText Comprehensive Tests + * Tests streaming text generation across all providers with various parameters + */ + +import { streamText } from 'ai' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { collectStreamChunks, createMockLanguageModel, mockProviderConfigs, testMessages } from '../../../__tests__' +import type { AiPlugin } from '../../plugins' +import { globalRegistryManagement } from '../../providers/RegistryManagement' +import { RuntimeExecutor } from '../executor' + +// Mock AI SDK - use importOriginal to keep jsonSchema and other non-mocked exports +vi.mock('ai', async (importOriginal) => { + const actual = (await importOriginal()) as Record + return { + ...actual, + streamText: vi.fn() + } +}) + +vi.mock('../../providers/RegistryManagement', () => ({ + globalRegistryManagement: { + languageModel: vi.fn() + }, + DEFAULT_SEPARATOR: '|' +})) + +describe('RuntimeExecutor.streamText', () => { + let executor: RuntimeExecutor<'openai'> + let mockLanguageModel: any + + beforeEach(() => { + vi.clearAllMocks() + + executor = RuntimeExecutor.create('openai', mockProviderConfigs.openai) + + mockLanguageModel = createMockLanguageModel({ + provider: 'openai', + modelId: 'gpt-4' + }) + + vi.mocked(globalRegistryManagement.languageModel).mockReturnValue(mockLanguageModel) + }) + + describe('Basic Functionality', () => { + it('should stream text with minimal parameters', async () => { + const mockStream = { + textStream: (async function* () { + yield 'Hello' + yield ' ' + yield 'World' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Hello' } + yield { type: 'text-delta', textDelta: ' ' } + yield { type: 'text-delta', textDelta: 'World' } + })(), + usage: Promise.resolve({ promptTokens: 5, completionTokens: 3, totalTokens: 8 }) + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + const result = await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + expect(streamText).toHaveBeenCalledWith({ + model: mockLanguageModel, + messages: testMessages.simple + }) + + const chunks = await collectStreamChunks(result.textStream) + expect(chunks).toEqual(['Hello', ' ', 'World']) + }) + + it('should stream with system messages', async () => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.withSystem + }) + + expect(streamText).toHaveBeenCalledWith({ + model: mockLanguageModel, + messages: testMessages.withSystem + }) + }) + + it('should stream multi-turn conversations', async () => { + const mockStream = { + textStream: (async function* () { + yield 'Multi-turn response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Multi-turn response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.multiTurn + }) + + expect(streamText).toHaveBeenCalled() + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + messages: testMessages.multiTurn + }) + ) + }) + }) + + describe('Temperature Parameter', () => { + const temperatures = [0, 0.3, 0.5, 0.7, 0.9, 1.0, 1.5, 2.0] + + it.each(temperatures)('should support temperature=%s', async (temperature) => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + temperature + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature + }) + ) + }) + }) + + describe('Max Tokens Parameter', () => { + const maxTokensValues = [10, 50, 100, 500, 1000, 2000, 4000] + + it.each(maxTokensValues)('should support maxOutputTokens=%s', async (maxOutputTokens) => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + maxOutputTokens + }) + + // Parameters are passed through without transformation + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + maxOutputTokens + }) + ) + }) + }) + + describe('Top P Parameter', () => { + const topPValues = [0.1, 0.3, 0.5, 0.7, 0.9, 0.95, 1.0] + + it.each(topPValues)('should support topP=%s', async (topP) => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + topP + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + topP + }) + ) + }) + }) + + describe('Frequency and Presence Penalty', () => { + it('should support frequency penalty', async () => { + const penalties = [-2.0, -1.0, 0, 0.5, 1.0, 1.5, 2.0] + + for (const frequencyPenalty of penalties) { + vi.clearAllMocks() + + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + frequencyPenalty + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + frequencyPenalty + }) + ) + } + }) + + it('should support presence penalty', async () => { + const penalties = [-2.0, -1.0, 0, 0.5, 1.0, 1.5, 2.0] + + for (const presencePenalty of penalties) { + vi.clearAllMocks() + + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + presencePenalty + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + presencePenalty + }) + ) + } + }) + + it('should support both penalties together', async () => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + frequencyPenalty: 0.5, + presencePenalty: 0.5 + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + frequencyPenalty: 0.5, + presencePenalty: 0.5 + }) + ) + }) + }) + + describe('Seed Parameter', () => { + it('should support seed for deterministic output', async () => { + const seeds = [0, 12345, 67890, 999999] + + for (const seed of seeds) { + vi.clearAllMocks() + + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + seed + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + seed + }) + ) + } + }) + }) + + describe('Abort Signal', () => { + it('should support abort signal', async () => { + const abortController = new AbortController() + + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + abortSignal: abortController.signal + }) + + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + abortSignal: abortController.signal + }) + ) + }) + + it('should handle abort during streaming', async () => { + const abortController = new AbortController() + + const mockStream = { + textStream: (async function* () { + yield 'Start' + // Simulate abort + abortController.abort() + throw new Error('Aborted') + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Start' } + throw new Error('Aborted') + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + const result = await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple, + abortSignal: abortController.signal + }) + + await expect(async () => { + // oxlint-disable-next-line no-unused-vars + for await (const _chunk of result.textStream) { + // Stream should be interrupted + } + }).rejects.toThrow('Aborted') + }) + }) + + describe('Plugin Integration', () => { + it('should execute plugins during streaming', async () => { + const pluginCalls: string[] = [] + + const testPlugin: AiPlugin = { + name: 'test-plugin', + onRequestStart: vi.fn(async () => { + pluginCalls.push('onRequestStart') + }), + transformParams: vi.fn(async (params) => { + pluginCalls.push('transformParams') + return { ...params, temperature: 0.5 } + }), + onRequestEnd: vi.fn(async () => { + pluginCalls.push('onRequestEnd') + }) + } + + const executorWithPlugin = RuntimeExecutor.create('openai', mockProviderConfigs.openai, [testPlugin]) + + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + const result = await executorWithPlugin.streamText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + // Consume stream + // oxlint-disable-next-line no-unused-vars + for await (const _chunk of result.textStream) { + // Stream chunks + } + + expect(pluginCalls).toContain('onRequestStart') + expect(pluginCalls).toContain('transformParams') + + // Verify transformed parameters were used + expect(streamText).toHaveBeenCalledWith( + expect.objectContaining({ + temperature: 0.5 + }) + ) + }) + }) + + describe('Full Stream with Finish Reason', () => { + it('should provide finish reason in full stream', async () => { + const mockStream = { + textStream: (async function* () { + yield 'Response' + })(), + fullStream: (async function* () { + yield { type: 'text-delta', textDelta: 'Response' } + yield { + type: 'finish', + finishReason: 'stop', + usage: { promptTokens: 5, completionTokens: 3, totalTokens: 8 } + } + })() + } + + vi.mocked(streamText).mockResolvedValue(mockStream as any) + + const result = await executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple + }) + + const fullChunks = await collectStreamChunks(result.fullStream) + + expect(fullChunks).toHaveLength(2) + expect(fullChunks[0]).toEqual({ type: 'text-delta', textDelta: 'Response' }) + expect(fullChunks[1]).toEqual({ + type: 'finish', + finishReason: 'stop', + usage: { promptTokens: 5, completionTokens: 3, totalTokens: 8 } + }) + }) + }) + + describe('Error Handling', () => { + it('should handle streaming errors', async () => { + const error = new Error('Streaming failed') + vi.mocked(streamText).mockRejectedValue(error) + + await expect( + executor.streamText({ + model: 'gpt-4', + messages: testMessages.simple + }) + ).rejects.toThrow('Streaming failed') + }) + + it('should execute onError plugin hook on failure', async () => { + const error = new Error('Stream error') + vi.mocked(streamText).mockRejectedValue(error) + + const errorPlugin: AiPlugin = { + name: 'error-handler', + onError: vi.fn() + } + + const executorWithPlugin = RuntimeExecutor.create('openai', mockProviderConfigs.openai, [errorPlugin]) + + await expect( + executorWithPlugin.streamText({ + model: 'gpt-4', + messages: testMessages.simple + }) + ).rejects.toThrow('Stream error') + + // onError receives the original error and context with core fields + expect(errorPlugin.onError).toHaveBeenCalledWith( + error, + expect.objectContaining({ + providerId: 'openai', + model: 'gpt-4' + }) + ) + }) + }) +}) diff --git a/packages/aiCore/vitest.config.ts b/packages/aiCore/vitest.config.ts index 0cc6b51df4..2f520ea967 100644 --- a/packages/aiCore/vitest.config.ts +++ b/packages/aiCore/vitest.config.ts @@ -1,12 +1,20 @@ +import path from 'node:path' +import { fileURLToPath } from 'node:url' + import { defineConfig } from 'vitest/config' +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + export default defineConfig({ test: { - globals: true + globals: true, + setupFiles: [path.resolve(__dirname, './src/__tests__/setup.ts')] }, resolve: { alias: { - '@': './src' + '@': path.resolve(__dirname, './src'), + // Mock external packages that may not be available in test environment + '@cherrystudio/ai-sdk-provider': path.resolve(__dirname, './src/__tests__/mocks/ai-sdk-provider.ts') } }, esbuild: { diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index b90ef3b356..0ebe48266d 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -55,6 +55,8 @@ export enum IpcChannel { Webview_SetOpenLinkExternal = 'webview:set-open-link-external', Webview_SetSpellCheckEnabled = 'webview:set-spell-check-enabled', Webview_SearchHotkey = 'webview:search-hotkey', + Webview_PrintToPDF = 'webview:print-to-pdf', + Webview_SaveAsHTML = 'webview:save-as-html', // Open Open_Path = 'open:path', @@ -90,6 +92,8 @@ export enum IpcChannel { Mcp_AbortTool = 'mcp:abort-tool', Mcp_GetServerVersion = 'mcp:get-server-version', Mcp_Progress = 'mcp:progress', + Mcp_GetServerLogs = 'mcp:get-server-logs', + Mcp_ServerLog = 'mcp:server-log', // Python Python_Execute = 'python:execute', @@ -196,6 +200,9 @@ export enum IpcChannel { File_ValidateNotesDirectory = 'file:validateNotesDirectory', File_StartWatcher = 'file:startWatcher', File_StopWatcher = 'file:stopWatcher', + File_PauseWatcher = 'file:pauseWatcher', + File_ResumeWatcher = 'file:resumeWatcher', + File_BatchUploadMarkdown = 'file:batchUploadMarkdown', File_ShowInFolder = 'file:showInFolder', // file service @@ -235,6 +242,9 @@ export enum IpcChannel { System_GetDeviceType = 'system:getDeviceType', System_GetHostname = 'system:getHostname', System_GetCpuName = 'system:getCpuName', + System_CheckGitBash = 'system:checkGitBash', + System_GetGitBashPath = 'system:getGitBashPath', + System_SetGitBashPath = 'system:setGitBashPath', // DevTools System_ToggleDevTools = 'system:toggleDevTools', @@ -289,6 +299,8 @@ export enum IpcChannel { Selection_ActionWindowClose = 'selection:action-window-close', Selection_ActionWindowMinimize = 'selection:action-window-minimize', Selection_ActionWindowPin = 'selection:action-window-pin', + // [Windows only] Electron bug workaround - can be removed once https://github.com/electron/electron/issues/48554 is fixed + Selection_ActionWindowResize = 'selection:action-window-resize', Selection_ProcessAction = 'selection:process-action', Selection_UpdateActionData = 'selection:update-action-data', diff --git a/packages/shared/config/constant.ts b/packages/shared/config/constant.ts index c05fde902c..1e02ce7706 100644 --- a/packages/shared/config/constant.ts +++ b/packages/shared/config/constant.ts @@ -7,6 +7,11 @@ export const documentExts = ['.pdf', '.doc', '.docx', '.pptx', '.xlsx', '.odt', export const thirdPartyApplicationExts = ['.draftsExport'] export const bookExts = ['.epub'] +export const API_SERVER_DEFAULTS = { + HOST: '127.0.0.1', + PORT: 23333 +} + /** * A flat array of all file extensions known by the linguist database. * This is the primary source for identifying code files. diff --git a/packages/shared/config/providers.ts b/packages/shared/config/providers.ts new file mode 100644 index 0000000000..f7744150e2 --- /dev/null +++ b/packages/shared/config/providers.ts @@ -0,0 +1,48 @@ +/** + * @fileoverview Shared provider configuration for Claude Code and Anthropic API compatibility + * + * This module defines which models from specific providers support the Anthropic API endpoint. + * Used by both the Code Tools page and the Anthropic SDK client. + */ + +/** + * Silicon provider models that support Anthropic API endpoint. + * These models can be used with Claude Code via the Anthropic-compatible API. + * + * @see https://docs.siliconflow.cn/cn/api-reference/chat-completions/messages + */ +export const SILICON_ANTHROPIC_COMPATIBLE_MODELS: readonly string[] = [ + // DeepSeek V3.1 series + 'Pro/deepseek-ai/DeepSeek-V3.1-Terminus', + 'deepseek-ai/DeepSeek-V3.1', + 'Pro/deepseek-ai/DeepSeek-V3.1', + // DeepSeek V3 series + 'deepseek-ai/DeepSeek-V3', + 'Pro/deepseek-ai/DeepSeek-V3', + // Moonshot/Kimi series + 'moonshotai/Kimi-K2-Instruct-0905', + 'Pro/moonshotai/Kimi-K2-Instruct-0905', + 'moonshotai/Kimi-Dev-72B', + // Baidu ERNIE + 'baidu/ERNIE-4.5-300B-A47B' +] + +/** + * Creates a Set for efficient lookup of silicon Anthropic-compatible model IDs. + */ +const SILICON_ANTHROPIC_COMPATIBLE_MODEL_SET = new Set(SILICON_ANTHROPIC_COMPATIBLE_MODELS) + +/** + * Checks if a model ID is compatible with Anthropic API on Silicon provider. + * + * @param modelId - The model ID to check + * @returns true if the model supports Anthropic API endpoint + */ +export function isSiliconAnthropicCompatibleModel(modelId: string): boolean { + return SILICON_ANTHROPIC_COMPATIBLE_MODEL_SET.has(modelId) +} + +/** + * Silicon provider's Anthropic API host URL. + */ +export const SILICON_ANTHROPIC_API_HOST = 'https://api.siliconflow.cn' diff --git a/packages/shared/config/types.ts b/packages/shared/config/types.ts index 5c42f1d2b2..7dff53c753 100644 --- a/packages/shared/config/types.ts +++ b/packages/shared/config/types.ts @@ -10,7 +10,7 @@ export type LoaderReturn = { messageSource?: 'preprocess' | 'embedding' | 'validation' } -export type FileChangeEventType = 'add' | 'change' | 'unlink' | 'addDir' | 'unlinkDir' +export type FileChangeEventType = 'add' | 'change' | 'unlink' | 'addDir' | 'unlinkDir' | 'refresh' export type FileChangeEvent = { eventType: FileChangeEventType @@ -23,6 +23,14 @@ export type MCPProgressEvent = { progress: number // 0-1 range } +export type MCPServerLogEntry = { + timestamp: number + level: 'debug' | 'info' | 'warn' | 'error' | 'stderr' | 'stdout' + message: string + data?: any + source?: string +} + export type WebviewKeyEvent = { webviewId: number key: string diff --git a/packages/shared/utils.ts b/packages/shared/utils.ts index e87e2f2bef..a14f78958d 100644 --- a/packages/shared/utils.ts +++ b/packages/shared/utils.ts @@ -4,3 +4,34 @@ export const defaultAppHeaders = () => { 'X-Title': 'Cherry Studio' } } + +// Following two function are not being used for now. +// I may use them in the future, so just keep them commented. - by eurfelux + +/** + * Converts an `undefined` value to `null`, otherwise returns the value as-is. + * @param value - The value to check + * @returns `null` if the input is `undefined`; otherwise the input value + */ + +// export function toNullIfUndefined(value: T | undefined): T | null { +// if (value === undefined) { +// return null +// } else { +// return value +// } +// } + +/** + * Converts a `null` value to `undefined`, otherwise returns the value as-is. + * @param value - The value to check + * @returns `undefined` if the input is `null`; otherwise the input value + */ + +// export function toUndefinedIfNull(value: T | null): T | undefined { +// if (value === null) { +// return undefined +// } else { +// return value +// } +// } diff --git a/playwright.config.ts b/playwright.config.ts index e12ce7ab6d..0b67f0e76f 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,42 +1,64 @@ -import { defineConfig, devices } from '@playwright/test' +import { defineConfig } from '@playwright/test' /** - * See https://playwright.dev/docs/test-configuration. + * Playwright configuration for Electron e2e testing. + * See https://playwright.dev/docs/test-configuration */ export default defineConfig({ - // Look for test files, relative to this configuration file. - testDir: './tests/e2e', - /* Run tests in files in parallel */ - fullyParallel: true, - /* Fail the build on CI if you accidentally left test.only in the source code. */ - forbidOnly: !!process.env.CI, - /* Retry on CI only */ - retries: process.env.CI ? 2 : 0, - /* Opt out of parallel tests on CI. */ - workers: process.env.CI ? 1 : undefined, - /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: 'html', - /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ - use: { - /* Base URL to use in actions like `await page.goto('/')`. */ - // baseURL: 'http://localhost:3000', + // Look for test files in the specs directory + testDir: './tests/e2e/specs', - /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ - trace: 'on-first-retry' + // Global timeout for each test + timeout: 60000, + + // Assertion timeout + expect: { + timeout: 10000 }, - /* Configure projects for major browsers */ + // Electron apps should run tests sequentially to avoid conflicts + fullyParallel: false, + workers: 1, + + // Fail the build on CI if you accidentally left test.only in the source code + forbidOnly: !!process.env.CI, + + // Retry on CI only + retries: process.env.CI ? 2 : 0, + + // Reporter configuration + reporter: [['html', { outputFolder: 'playwright-report' }], ['list']], + + // Global setup and teardown + globalSetup: './tests/e2e/global-setup.ts', + globalTeardown: './tests/e2e/global-teardown.ts', + + // Output directory for test artifacts + outputDir: './test-results', + + // Shared settings for all tests + use: { + // Collect trace when retrying the failed test + trace: 'retain-on-failure', + + // Take screenshot only on failure + screenshot: 'only-on-failure', + + // Record video only on failure + video: 'retain-on-failure', + + // Action timeout + actionTimeout: 15000, + + // Navigation timeout + navigationTimeout: 30000 + }, + + // Single project for Electron testing projects: [ { - name: 'chromium', - use: { ...devices['Desktop Chrome'] } + name: 'electron', + testMatch: '**/*.spec.ts' } ] - - /* Run your local dev server before starting the tests */ - // webServer: { - // command: 'npm run start', - // url: 'http://localhost:3000', - // reuseExistingServer: !process.env.CI, - // }, }) diff --git a/resources/scripts/install-ovms.js b/resources/scripts/install-ovms.js index e4a5cf0444..f2be80bffe 100644 --- a/resources/scripts/install-ovms.js +++ b/resources/scripts/install-ovms.js @@ -11,7 +11,7 @@ const OVMS_EX_URL = 'https://gitcode.com/gcw_ggDjjkY3/kjfile/releases/download/d /** * error code: - * 101: Unsupported CPU (not Intel Ultra) + * 101: Unsupported CPU (not Intel) * 102: Unsupported platform (not Windows) * 103: Download failed * 104: Installation failed @@ -213,8 +213,8 @@ async function installOvms() { console.log(`CPU Name: ${cpuName}`) // Check if CPU name contains "Ultra" - if (!cpuName.toLowerCase().includes('intel') || !cpuName.toLowerCase().includes('ultra')) { - console.error('OVMS installation requires an Intel(R) Core(TM) Ultra CPU.') + if (!cpuName.toLowerCase().includes('intel')) { + console.error('OVMS installation requires an Intel CPU.') return 101 } diff --git a/scripts/feishu-notify.js b/scripts/feishu-notify.js index aae9004a48..d238dedb90 100644 --- a/scripts/feishu-notify.js +++ b/scripts/feishu-notify.js @@ -91,23 +91,6 @@ function createIssueCard(issueData) { return { elements: [ - { - tag: 'div', - text: { - tag: 'lark_md', - content: `**🐛 New GitHub Issue #${issueNumber}**` - } - }, - { - tag: 'hr' - }, - { - tag: 'div', - text: { - tag: 'lark_md', - content: `**📝 Title:** ${issueTitle}` - } - }, { tag: 'div', text: { @@ -158,7 +141,7 @@ function createIssueCard(issueData) { template: 'blue', title: { tag: 'plain_text', - content: '🆕 Cherry Studio - New Issue' + content: `#${issueNumber} - ${issueTitle}` } } } diff --git a/scripts/win-sign.js b/scripts/win-sign.js index f9b37c3aed..cdbfe11e17 100644 --- a/scripts/win-sign.js +++ b/scripts/win-sign.js @@ -5,9 +5,17 @@ exports.default = async function (configuration) { const { path } = configuration if (configuration.path) { try { + const certPath = process.env.CHERRY_CERT_PATH + const keyContainer = process.env.CHERRY_CERT_KEY + const csp = process.env.CHERRY_CERT_CSP + + if (!certPath || !keyContainer || !csp) { + throw new Error('CHERRY_CERT_PATH, CHERRY_CERT_KEY or CHERRY_CERT_CSP is not set') + } + console.log('Start code signing...') console.log('Signing file:', path) - const signCommand = `signtool sign /tr http://timestamp.comodoca.com /td sha256 /fd sha256 /a /v "${path}"` + const signCommand = `signtool sign /tr http://timestamp.comodoca.com /td sha256 /fd sha256 /v /f "${certPath}" /csp "${csp}" /k "${keyContainer}" "${path}"` execSync(signCommand, { stdio: 'inherit' }) console.log('Code signing completed') } catch (error) { diff --git a/src/main/apiServer/config.ts b/src/main/apiServer/config.ts index 60b1986be9..0966827a7b 100644 --- a/src/main/apiServer/config.ts +++ b/src/main/apiServer/config.ts @@ -1,3 +1,4 @@ +import { API_SERVER_DEFAULTS } from '@shared/config/constant' import type { ApiServerConfig } from '@types' import { v4 as uuidv4 } from 'uuid' @@ -6,9 +7,6 @@ import { reduxService } from '../services/ReduxService' const logger = loggerService.withContext('ApiServerConfig') -const defaultHost = 'localhost' -const defaultPort = 23333 - class ConfigManager { private _config: ApiServerConfig | null = null @@ -30,8 +28,8 @@ class ConfigManager { } this._config = { enabled: serverSettings?.enabled ?? false, - port: serverSettings?.port ?? defaultPort, - host: defaultHost, + port: serverSettings?.port ?? API_SERVER_DEFAULTS.PORT, + host: serverSettings?.host ?? API_SERVER_DEFAULTS.HOST, apiKey: apiKey } return this._config @@ -39,8 +37,8 @@ class ConfigManager { logger.warn('Failed to load config from Redux, using defaults', { error }) this._config = { enabled: false, - port: defaultPort, - host: defaultHost, + port: API_SERVER_DEFAULTS.PORT, + host: API_SERVER_DEFAULTS.HOST, apiKey: this.generateApiKey() } return this._config diff --git a/src/main/apiServer/middleware/openapi.ts b/src/main/apiServer/middleware/openapi.ts index ff01005bd9..6b374901ca 100644 --- a/src/main/apiServer/middleware/openapi.ts +++ b/src/main/apiServer/middleware/openapi.ts @@ -20,8 +20,8 @@ const swaggerOptions: swaggerJSDoc.Options = { }, servers: [ { - url: 'http://localhost:23333', - description: 'Local development server' + url: '/', + description: 'Current server' } ], components: { diff --git a/src/main/apiServer/routes/models.ts b/src/main/apiServer/routes/models.ts index 8481e1ea59..d776d5ea91 100644 --- a/src/main/apiServer/routes/models.ts +++ b/src/main/apiServer/routes/models.ts @@ -104,12 +104,6 @@ const router = express logger.warn('No models available from providers', { filter }) } - logger.info('Models response ready', { - filter, - total: response.total, - modelIds: response.data.map((m) => m.id) - }) - return res.json(response satisfies ApiModelsResponse) } catch (error: any) { logger.error('Error fetching models', { error }) diff --git a/src/main/apiServer/server.ts b/src/main/apiServer/server.ts index 9b15e56da0..e59e6bd504 100644 --- a/src/main/apiServer/server.ts +++ b/src/main/apiServer/server.ts @@ -3,7 +3,6 @@ import { createServer } from 'node:http' import { loggerService } from '@logger' import { IpcChannel } from '@shared/IpcChannel' -import { agentService } from '../services/agents' import { windowService } from '../services/WindowService' import { app } from './app' import { config } from './config' @@ -32,11 +31,6 @@ export class ApiServer { // Load config const { port, host } = await config.load() - // Initialize AgentService - logger.info('Initializing AgentService') - await agentService.initialize() - logger.info('AgentService initialized') - // Create server with Express app this.server = createServer(app) this.applyServerTimeouts(this.server) diff --git a/src/main/apiServer/services/models.ts b/src/main/apiServer/services/models.ts index a32d6d37dc..52f0db857f 100644 --- a/src/main/apiServer/services/models.ts +++ b/src/main/apiServer/services/models.ts @@ -32,7 +32,7 @@ export class ModelsService { for (const model of models) { const provider = providers.find((p) => p.id === model.provider) - logger.debug(`Processing model ${model.id}`) + // logger.debug(`Processing model ${model.id}`) if (!provider) { logger.debug(`Skipping model ${model.id} . Reason: Provider not found.`) continue diff --git a/src/main/apiServer/utils/index.ts b/src/main/apiServer/utils/index.ts index f9f751c559..e25b49e750 100644 --- a/src/main/apiServer/utils/index.ts +++ b/src/main/apiServer/utils/index.ts @@ -1,6 +1,7 @@ import { CacheService } from '@main/services/CacheService' import { loggerService } from '@main/services/LoggerService' import { reduxService } from '@main/services/ReduxService' +import { isSiliconAnthropicCompatibleModel } from '@shared/config/providers' import type { ApiModel, Model, Provider } from '@types' const logger = loggerService.withContext('ApiServerUtils') @@ -287,6 +288,8 @@ export const getProviderAnthropicModelChecker = (providerId: string): ((m: Model return (m: Model) => m.endpoint_type === 'anthropic' case 'aihubmix': return (m: Model) => m.id.includes('claude') + case 'silicon': + return (m: Model) => isSiliconAnthropicCompatibleModel(m.id) default: // allow all models when checker not configured return () => true diff --git a/src/main/index.ts b/src/main/index.ts index 27489a26b5..3588a370ff 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -19,8 +19,8 @@ import { agentService } from './services/agents' import { apiServerService } from './services/ApiServerService' import { appMenuService } from './services/AppMenuService' import { configManager } from './services/ConfigManager' -import mcpService from './services/MCPService' import { nodeTraceService } from './services/NodeTraceService' +import mcpService from './services/MCPService' import powerMonitorService from './services/PowerMonitorService' import { CHERRY_STUDIO_PROTOCOL, @@ -34,6 +34,7 @@ import { TrayService } from './services/TrayService' import { versionService } from './services/VersionService' import { windowService } from './services/WindowService' import { initWebviewHotkeys } from './services/WebviewService' +import { runAsyncFunction } from './utils' const logger = loggerService.withContext('MainEntry') @@ -170,39 +171,33 @@ if (!app.requestSingleInstanceLock()) { //start selection assistant service initSelectionService() - // Initialize Agent Service - try { - await agentService.initialize() - logger.info('Agent service initialized successfully') - } catch (error: any) { - logger.error('Failed to initialize Agent service:', error) - } + runAsyncFunction(async () => { + // Start API server if enabled or if agents exist + try { + const config = await apiServerService.getCurrentConfig() + logger.info('API server config:', config) - // Start API server if enabled or if agents exist - try { - const config = await apiServerService.getCurrentConfig() - logger.info('API server config:', config) - - // Check if there are any agents - let shouldStart = config.enabled - if (!shouldStart) { - try { - const { total } = await agentService.listAgents({ limit: 1 }) - if (total > 0) { - shouldStart = true - logger.info(`Detected ${total} agent(s), auto-starting API server`) + // Check if there are any agents + let shouldStart = config.enabled + if (!shouldStart) { + try { + const { total } = await agentService.listAgents({ limit: 1 }) + if (total > 0) { + shouldStart = true + logger.info(`Detected ${total} agent(s), auto-starting API server`) + } + } catch (error: any) { + logger.warn('Failed to check agent count:', error) } - } catch (error: any) { - logger.warn('Failed to check agent count:', error) } - } - if (shouldStart) { - await apiServerService.start() + if (shouldStart) { + await apiServerService.start() + } + } catch (error: any) { + logger.error('Failed to check/start API server:', error) } - } catch (error: any) { - logger.error('Failed to check/start API server:', error) - } + }) }) registerProtocolClient(app) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 9750a4cf05..d7e82ff875 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -6,7 +6,7 @@ import { loggerService } from '@logger' import { isLinux, isMac, isPortable, isWin } from '@main/constant' import { generateSignature } from '@main/integration/cherryai' import anthropicService from '@main/services/AnthropicService' -import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process' +import { findGitBash, getBinaryPath, isBinaryExists, runInstallScript, validateGitBashPath } from '@main/utils/process' import { handleZoomFactor } from '@main/utils/zoom' import type { SpanEntity, TokenUsage } from '@mcp-trace/trace-core' import type { UpgradeChannel } from '@shared/config/constant' @@ -35,7 +35,7 @@ import appService from './services/AppService' import AppUpdater from './services/AppUpdater' import BackupManager from './services/BackupManager' import { codeToolsService } from './services/CodeToolsService' -import { configManager } from './services/ConfigManager' +import { ConfigKeys, configManager } from './services/ConfigManager' import CopilotService from './services/CopilotService' import DxtService from './services/DxtService' import { ExportService } from './services/ExportService' @@ -493,6 +493,56 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.System_GetDeviceType, () => (isMac ? 'mac' : isWin ? 'windows' : 'linux')) ipcMain.handle(IpcChannel.System_GetHostname, () => require('os').hostname()) ipcMain.handle(IpcChannel.System_GetCpuName, () => require('os').cpus()[0].model) + ipcMain.handle(IpcChannel.System_CheckGitBash, () => { + if (!isWin) { + return true // Non-Windows systems don't need Git Bash + } + + try { + const customPath = configManager.get(ConfigKeys.GitBashPath) as string | undefined + const bashPath = findGitBash(customPath) + + if (bashPath) { + logger.info('Git Bash is available', { path: bashPath }) + return true + } + + logger.warn('Git Bash not found. Please install Git for Windows from https://git-scm.com/downloads/win') + return false + } catch (error) { + logger.error('Unexpected error checking Git Bash', error as Error) + return false + } + }) + + ipcMain.handle(IpcChannel.System_GetGitBashPath, () => { + if (!isWin) { + return null + } + + const customPath = configManager.get(ConfigKeys.GitBashPath) as string | undefined + return customPath ?? null + }) + + ipcMain.handle(IpcChannel.System_SetGitBashPath, (_, newPath: string | null) => { + if (!isWin) { + return false + } + + if (!newPath) { + configManager.set(ConfigKeys.GitBashPath, null) + return true + } + + const validated = validateGitBashPath(newPath) + if (!validated) { + return false + } + + configManager.set(ConfigKeys.GitBashPath, validated) + return true + }) + ipcMain.handle(IpcChannel.System_ToggleDevTools, (e) => { const win = BrowserWindow.fromWebContents(e.sender) win && win.webContents.toggleDevTools() @@ -557,6 +607,9 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.File_ValidateNotesDirectory, fileManager.validateNotesDirectory.bind(fileManager)) ipcMain.handle(IpcChannel.File_StartWatcher, fileManager.startFileWatcher.bind(fileManager)) ipcMain.handle(IpcChannel.File_StopWatcher, fileManager.stopFileWatcher.bind(fileManager)) + ipcMain.handle(IpcChannel.File_PauseWatcher, fileManager.pauseFileWatcher.bind(fileManager)) + ipcMain.handle(IpcChannel.File_ResumeWatcher, fileManager.resumeFileWatcher.bind(fileManager)) + ipcMain.handle(IpcChannel.File_BatchUploadMarkdown, fileManager.batchUploadMarkdownFiles.bind(fileManager)) ipcMain.handle(IpcChannel.File_ShowInFolder, fileManager.showInFolder.bind(fileManager)) // file service @@ -742,6 +795,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.Mcp_CheckConnectivity, mcpService.checkMcpConnectivity) ipcMain.handle(IpcChannel.Mcp_AbortTool, mcpService.abortTool) ipcMain.handle(IpcChannel.Mcp_GetServerVersion, mcpService.getServerVersion) + ipcMain.handle(IpcChannel.Mcp_GetServerLogs, mcpService.getServerLogs) // DXT upload handler ipcMain.handle(IpcChannel.Mcp_UploadDxt, async (event, fileBuffer: ArrayBuffer, fileName: string) => { @@ -820,6 +874,17 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { webview.session.setSpellCheckerEnabled(isEnable) }) + // Webview print and save handlers + ipcMain.handle(IpcChannel.Webview_PrintToPDF, async (_, webviewId: number) => { + const { printWebviewToPDF } = await import('./services/WebviewService') + return await printWebviewToPDF(webviewId) + }) + + ipcMain.handle(IpcChannel.Webview_SaveAsHTML, async (_, webviewId: number) => { + const { saveWebviewAsHTML } = await import('./services/WebviewService') + return await saveWebviewAsHTML(webviewId) + }) + // store sync storeSyncService.registerIpcHandler() diff --git a/src/main/knowledge/embedjs/embeddings/EmbeddingsFactory.ts b/src/main/knowledge/embedjs/embeddings/EmbeddingsFactory.ts index 8a780d5618..e9f459fd6c 100644 --- a/src/main/knowledge/embedjs/embeddings/EmbeddingsFactory.ts +++ b/src/main/knowledge/embedjs/embeddings/EmbeddingsFactory.ts @@ -19,19 +19,9 @@ export default class EmbeddingsFactory { }) } if (provider === 'ollama') { - if (baseURL.includes('v1/')) { - return new OllamaEmbeddings({ - model: model, - baseUrl: baseURL.replace('v1/', ''), - requestOptions: { - // @ts-ignore expected - 'encoding-format': 'float' - } - }) - } return new OllamaEmbeddings({ model: model, - baseUrl: baseURL, + baseUrl: baseURL.replace(/\/api$/, ''), requestOptions: { // @ts-ignore expected 'encoding-format': 'float' diff --git a/src/main/services/CodeToolsService.ts b/src/main/services/CodeToolsService.ts index 82c9c64f87..35655a88e7 100644 --- a/src/main/services/CodeToolsService.ts +++ b/src/main/services/CodeToolsService.ts @@ -548,6 +548,17 @@ class CodeToolsService { logger.debug(`Environment variables:`, Object.keys(env)) logger.debug(`Options:`, options) + // Validate directory exists before proceeding + if (!directory || !fs.existsSync(directory)) { + const errorMessage = `Directory does not exist: ${directory}` + logger.error(errorMessage) + return { + success: false, + message: errorMessage, + command: '' + } + } + const packageName = await this.getPackageName(cliTool) const bunPath = await this.getBunPath() const executableName = await this.getCliExecutableName(cliTool) @@ -709,6 +720,7 @@ class CodeToolsService { // Build bat file content, including debug information const batContent = [ '@echo off', + 'chcp 65001 >nul 2>&1', // Switch to UTF-8 code page for international path support `title ${cliTool} - Cherry Studio`, // Set window title in bat file 'echo ================================================', 'echo Cherry Studio CLI Tool Launcher', diff --git a/src/main/services/ConfigManager.ts b/src/main/services/ConfigManager.ts index 61e285ac1b..c693d4b05a 100644 --- a/src/main/services/ConfigManager.ts +++ b/src/main/services/ConfigManager.ts @@ -31,7 +31,8 @@ export enum ConfigKeys { DisableHardwareAcceleration = 'disableHardwareAcceleration', Proxy = 'proxy', EnableDeveloperMode = 'enableDeveloperMode', - ClientId = 'clientId' + ClientId = 'clientId', + GitBashPath = 'gitBashPath' } export class ConfigManager { diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index 3165fcf27e..81f5c15bd9 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -151,6 +151,7 @@ class FileStorage { private currentWatchPath?: string private debounceTimer?: NodeJS.Timeout private watcherConfig: Required = DEFAULT_WATCHER_CONFIG + private isPaused = false constructor() { this.initStorageDir() @@ -478,13 +479,16 @@ class FileStorage { } } - public readFile = async ( - _: Electron.IpcMainInvokeEvent, - id: string, - detectEncoding: boolean = false - ): Promise => { - const filePath = path.join(this.storageDir, id) - + /** + * Core file reading logic that handles both documents and text files. + * + * @private + * @param filePath - Full path to the file + * @param detectEncoding - Whether to auto-detect text file encoding + * @returns Promise resolving to the extracted text content + * @throws Error if file reading fails + */ + private async readFileCore(filePath: string, detectEncoding: boolean = false): Promise { const fileExtension = path.extname(filePath) if (documentExts.includes(fileExtension)) { @@ -504,7 +508,7 @@ class FileStorage { return data } catch (error) { chdir(originalCwd) - logger.error('Failed to read file:', error as Error) + logger.error('Failed to read document file:', error as Error) throw error } } @@ -516,11 +520,72 @@ class FileStorage { return fs.readFileSync(filePath, 'utf-8') } } catch (error) { - logger.error('Failed to read file:', error as Error) + logger.error('Failed to read text file:', error as Error) throw new Error(`Failed to read file: ${filePath}.`) } } + /** + * Reads and extracts content from a stored file. + * + * Supports multiple file formats including: + * - Complex documents: .pdf, .doc, .docx, .pptx, .xlsx, .odt, .odp, .ods + * - Text files: .txt, .md, .json, .csv, etc. + * - Code files: .js, .ts, .py, .java, etc. + * + * For document formats, extracts text content using specialized parsers: + * - .doc files: Uses word-extractor library + * - Other Office formats: Uses officeparser library + * + * For text files, can optionally detect encoding automatically. + * + * @param _ - Electron IPC invoke event (unused) + * @param id - File identifier with extension (e.g., "uuid.docx") + * @param detectEncoding - Whether to auto-detect text file encoding (default: false) + * @returns Promise resolving to the extracted text content of the file + * @throws Error if file reading fails or file is not found + * + * @example + * // Read a DOCX file + * const content = await readFile(event, "document.docx"); + * + * @example + * // Read a text file with encoding detection + * const content = await readFile(event, "text.txt", true); + * + * @example + * // Read a PDF file + * const content = await readFile(event, "manual.pdf"); + */ + public readFile = async ( + _: Electron.IpcMainInvokeEvent, + id: string, + detectEncoding: boolean = false + ): Promise => { + const filePath = path.join(this.storageDir, id) + return this.readFileCore(filePath, detectEncoding) + } + + /** + * Reads and extracts content from an external file path. + * + * Similar to readFile, but operates on external file paths instead of stored files. + * Supports the same file formats including complex documents and text files. + * + * @param _ - Electron IPC invoke event (unused) + * @param filePath - Absolute path to the external file + * @param detectEncoding - Whether to auto-detect text file encoding (default: false) + * @returns Promise resolving to the extracted text content of the file + * @throws Error if file does not exist or reading fails + * + * @example + * // Read an external DOCX file + * const content = await readExternalFile(event, "/path/to/document.docx"); + * + * @example + * // Read an external text file with encoding detection + * const content = await readExternalFile(event, "/path/to/text.txt", true); + */ public readExternalFile = async ( _: Electron.IpcMainInvokeEvent, filePath: string, @@ -530,40 +595,7 @@ class FileStorage { throw new Error(`File does not exist: ${filePath}`) } - const fileExtension = path.extname(filePath) - - if (documentExts.includes(fileExtension)) { - const originalCwd = process.cwd() - try { - chdir(this.tempDir) - - if (fileExtension === '.doc') { - const extractor = new WordExtractor() - const extracted = await extractor.extract(filePath) - chdir(originalCwd) - return extracted.getBody() - } - - const data = await officeParser.parseOfficeAsync(filePath) - chdir(originalCwd) - return data - } catch (error) { - chdir(originalCwd) - logger.error('Failed to read file:', error as Error) - throw error - } - } - - try { - if (detectEncoding) { - return readTextFileWithAutoEncoding(filePath) - } else { - return fs.readFileSync(filePath, 'utf-8') - } - } catch (error) { - logger.error('Failed to read file:', error as Error) - throw new Error(`Failed to read file: ${filePath}.`) - } + return this.readFileCore(filePath, detectEncoding) } public createTempFile = async (_: Electron.IpcMainInvokeEvent, fileName: string): Promise => { @@ -1448,6 +1480,12 @@ class FileStorage { private createChangeHandler() { return (eventType: string, filePath: string) => { + // Skip processing if watcher is paused + if (this.isPaused) { + logger.debug('File change ignored (watcher paused)', { eventType, filePath }) + return + } + if (!this.shouldWatchFile(filePath, eventType)) { return } @@ -1605,6 +1643,165 @@ class FileStorage { logger.error('Failed to show item in folder:', error as Error) } } + + /** + * Batch upload markdown files from native File objects + * This handles all I/O operations in the Main process to avoid blocking Renderer + */ + public batchUploadMarkdownFiles = async ( + _: Electron.IpcMainInvokeEvent, + filePaths: string[], + targetPath: string + ): Promise<{ + fileCount: number + folderCount: number + skippedFiles: number + }> => { + try { + logger.info('Starting batch upload', { fileCount: filePaths.length, targetPath }) + + const basePath = path.resolve(targetPath) + const MARKDOWN_EXTS = ['.md', '.markdown'] + + // Filter markdown files + const markdownFiles = filePaths.filter((filePath) => { + const ext = path.extname(filePath).toLowerCase() + return MARKDOWN_EXTS.includes(ext) + }) + + const skippedFiles = filePaths.length - markdownFiles.length + + if (markdownFiles.length === 0) { + return { fileCount: 0, folderCount: 0, skippedFiles } + } + + // Collect unique folders needed + const foldersSet = new Set() + const fileOperations: Array<{ sourcePath: string; targetPath: string }> = [] + + for (const filePath of markdownFiles) { + try { + // Get relative path if file is from a directory upload + const fileName = path.basename(filePath) + const relativePath = path.dirname(filePath) + + // Determine target directory structure + let targetDir = basePath + const folderParts: string[] = [] + + // Extract folder structure from file path for nested uploads + // This is a simplified version - in real scenario we'd need the original directory structure + if (relativePath && relativePath !== '.') { + const parts = relativePath.split(path.sep) + // Get the last few parts that represent the folder structure within upload + const relevantParts = parts.slice(Math.max(0, parts.length - 3)) + folderParts.push(...relevantParts) + } + + // Build target directory path + for (const part of folderParts) { + targetDir = path.join(targetDir, part) + foldersSet.add(targetDir) + } + + // Determine final file name + const nameWithoutExt = fileName.endsWith('.md') + ? fileName.slice(0, -3) + : fileName.endsWith('.markdown') + ? fileName.slice(0, -9) + : fileName + + const { safeName } = await this.fileNameGuard(_, targetDir, nameWithoutExt, true) + const finalPath = path.join(targetDir, safeName + '.md') + + fileOperations.push({ sourcePath: filePath, targetPath: finalPath }) + } catch (error) { + logger.error('Failed to prepare file operation:', error as Error, { filePath }) + } + } + + // Create folders in order (shallow to deep) + const sortedFolders = Array.from(foldersSet).sort((a, b) => a.length - b.length) + for (const folder of sortedFolders) { + try { + if (!fs.existsSync(folder)) { + await fs.promises.mkdir(folder, { recursive: true }) + } + } catch (error) { + logger.debug('Folder already exists or creation failed', { folder, error: (error as Error).message }) + } + } + + // Process files in batches + const BATCH_SIZE = 10 // Higher batch size since we're in Main process + let successCount = 0 + + for (let i = 0; i < fileOperations.length; i += BATCH_SIZE) { + const batch = fileOperations.slice(i, i + BATCH_SIZE) + + const results = await Promise.allSettled( + batch.map(async (op) => { + // Read from source and write to target in Main process + const content = await fs.promises.readFile(op.sourcePath, 'utf-8') + await fs.promises.writeFile(op.targetPath, content, 'utf-8') + return true + }) + ) + + results.forEach((result, index) => { + if (result.status === 'fulfilled') { + successCount++ + } else { + logger.error('Failed to upload file:', result.reason, { + file: batch[index].sourcePath + }) + } + }) + } + + logger.info('Batch upload completed', { + successCount, + folderCount: foldersSet.size, + skippedFiles + }) + + return { + fileCount: successCount, + folderCount: foldersSet.size, + skippedFiles + } + } catch (error) { + logger.error('Batch upload failed:', error as Error) + throw error + } + } + + /** + * Pause file watcher to prevent events during batch operations + */ + public pauseFileWatcher = async (): Promise => { + if (this.watcher) { + logger.debug('Pausing file watcher') + this.isPaused = true + // Clear any pending debounced notifications + if (this.debounceTimer) { + clearTimeout(this.debounceTimer) + this.debounceTimer = undefined + } + } + } + + /** + * Resume file watcher and trigger a refresh + */ + public resumeFileWatcher = async (): Promise => { + if (this.watcher && this.currentWatchPath) { + logger.debug('Resuming file watcher') + this.isPaused = false + // Send a synthetic refresh event to trigger tree reload + this.notifyChange('refresh', this.currentWatchPath) + } + } } export const fileStorage = new FileStorage() diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index 3831d0af1e..cc6bbaa366 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -12,6 +12,7 @@ import { TraceMethod, withSpanFunc } from '@mcp-trace/trace-core' import { Client } from '@modelcontextprotocol/sdk/client/index.js' import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js' import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js' +import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js' import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js' import { StreamableHTTPClientTransport, @@ -32,6 +33,7 @@ import { import { nanoid } from '@reduxjs/toolkit' import { HOME_CHERRY_DIR } from '@shared/config/constant' import type { MCPProgressEvent } from '@shared/config/types' +import type { MCPServerLogEntry } from '@shared/config/types' import { IpcChannel } from '@shared/IpcChannel' import { defaultAppHeaders } from '@shared/utils' import { @@ -42,16 +44,20 @@ import { type MCPPrompt, type MCPResource, type MCPServer, - type MCPTool + type MCPTool, + MCPToolInputSchema, + MCPToolOutputSchema } from '@types' import { app, net } from 'electron' import { EventEmitter } from 'events' import { v4 as uuidv4 } from 'uuid' +import * as z from 'zod' import { CacheService } from './CacheService' import DxtService from './DxtService' import { CallBackServer } from './mcp/oauth/callback' import { McpOAuthClientProvider } from './mcp/oauth/provider' +import { ServerLogBuffer } from './mcp/ServerLogBuffer' import { windowService } from './WindowService' // Generic type for caching wrapped functions @@ -138,6 +144,7 @@ class McpService { private pendingClients: Map> = new Map() private dxtService = new DxtService() private activeToolCalls: Map = new Map() + private serverLogs = new ServerLogBuffer(200) constructor() { this.initClient = this.initClient.bind(this) @@ -155,6 +162,7 @@ class McpService { this.cleanup = this.cleanup.bind(this) this.checkMcpConnectivity = this.checkMcpConnectivity.bind(this) this.getServerVersion = this.getServerVersion.bind(this) + this.getServerLogs = this.getServerLogs.bind(this) } private getServerKey(server: MCPServer): string { @@ -168,6 +176,19 @@ class McpService { }) } + private emitServerLog(server: MCPServer, entry: MCPServerLogEntry) { + const serverKey = this.getServerKey(server) + this.serverLogs.append(serverKey, entry) + const mainWindow = windowService.getMainWindow() + if (mainWindow) { + mainWindow.webContents.send(IpcChannel.Mcp_ServerLog, { ...entry, serverId: server.id }) + } + } + + public getServerLogs(_: Electron.IpcMainInvokeEvent, server: MCPServer): MCPServerLogEntry[] { + return this.serverLogs.get(this.getServerKey(server)) + } + async initClient(server: MCPServer): Promise { const serverKey = this.getServerKey(server) @@ -343,7 +364,7 @@ class McpService { removeEnvProxy(loginShellEnv) } - const transportOptions: any = { + const transportOptions: StdioServerParameters = { command: cmd, args, env: { @@ -362,9 +383,18 @@ class McpService { } const stdioTransport = new StdioClientTransport(transportOptions) - stdioTransport.stderr?.on('data', (data) => - getServerLogger(server).debug(`Stdio stderr`, { data: data.toString() }) - ) + stdioTransport.stderr?.on('data', (data) => { + const msg = data.toString() + getServerLogger(server).debug(`Stdio stderr`, { data: msg }) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'stderr', + message: msg.trim(), + source: 'stdio' + }) + }) + // StdioClientTransport does not expose stdout as a readable stream for raw logging + // (stdout is reserved for JSON-RPC). Avoid attaching a listener that would never fire. return stdioTransport } else { throw new Error('Either baseUrl or command must be provided') @@ -432,6 +462,13 @@ class McpService { } } + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'info', + message: 'Server connected', + source: 'client' + }) + // Store the new client in the cache this.clients.set(serverKey, client) @@ -442,9 +479,22 @@ class McpService { this.clearServerCache(serverKey) logger.debug(`Activated server: ${server.name}`) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'info', + message: 'Server activated', + source: 'client' + }) return client } catch (error) { getServerLogger(server).error(`Error activating server ${server.name}`, error as Error) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'error', + message: `Error activating server: ${(error as Error)?.message}`, + data: redactSensitive(error), + source: 'client' + }) throw error } } finally { @@ -502,6 +552,16 @@ class McpService { // Set up logging message notification handler client.setNotificationHandler(LoggingMessageNotificationSchema, async (notification) => { logger.debug(`Message from server ${server.name}:`, notification.params) + const msg = notification.params?.message + if (msg) { + this.emitServerLog(server, { + timestamp: Date.now(), + level: (notification.params?.level as MCPServerLogEntry['level']) || 'info', + message: typeof msg === 'string' ? msg : JSON.stringify(msg), + data: redactSensitive(notification.params?.data), + source: notification.params?.logger || 'server' + }) + } }) getServerLogger(server).debug(`Set up notification handlers`) @@ -536,6 +596,7 @@ class McpService { this.clients.delete(serverKey) // Clear all caches for this server this.clearServerCache(serverKey) + this.serverLogs.remove(serverKey) } else { logger.warn(`No client found for server`, { serverKey }) } @@ -544,6 +605,12 @@ class McpService { async stopServer(_: Electron.IpcMainInvokeEvent, server: MCPServer) { const serverKey = this.getServerKey(server) getServerLogger(server).debug(`Stopping server`) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'info', + message: 'Stopping server', + source: 'client' + }) await this.closeClient(serverKey) } @@ -570,6 +637,12 @@ class McpService { async restartServer(_: Electron.IpcMainInvokeEvent, server: MCPServer) { getServerLogger(server).debug(`Restarting server`) const serverKey = this.getServerKey(server) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'info', + message: 'Restarting server', + source: 'client' + }) await this.closeClient(serverKey) // Clear cache before restarting to ensure fresh data this.clearServerCache(serverKey) @@ -602,9 +675,22 @@ class McpService { // Attempt to list tools as a way to check connectivity await client.listTools() getServerLogger(server).debug(`Connectivity check successful`) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'info', + message: 'Connectivity check successful', + source: 'connectivity' + }) return true } catch (error) { getServerLogger(server).error(`Connectivity check failed`, error as Error) + this.emitServerLog(server, { + timestamp: Date.now(), + level: 'error', + message: `Connectivity check failed: ${(error as Error).message}`, + data: redactSensitive(error), + source: 'connectivity' + }) // Close the client if connectivity check fails to ensure a clean state for the next attempt const serverKey = this.getServerKey(server) await this.closeClient(serverKey) @@ -620,7 +706,9 @@ class McpService { tools.map((tool: SDKTool) => { const serverTool: MCPTool = { ...tool, - id: buildFunctionCallToolName(server.name, tool.name), + inputSchema: z.parse(MCPToolInputSchema, tool.inputSchema), + outputSchema: tool.outputSchema ? z.parse(MCPToolOutputSchema, tool.outputSchema) : undefined, + id: buildFunctionCallToolName(server.name, tool.name, server.id), serverId: server.id, serverName: server.name, type: 'mcp' diff --git a/src/main/services/SelectionService.ts b/src/main/services/SelectionService.ts index a096dfcfd7..695026003b 100644 --- a/src/main/services/SelectionService.ts +++ b/src/main/services/SelectionService.ts @@ -1393,6 +1393,50 @@ export class SelectionService { actionWindow.setAlwaysOnTop(isPinned) } + /** + * [Windows only] Manual window resize handler + * + * ELECTRON BUG WORKAROUND: + * In Electron, when using `frame: false` + `transparent: true`, the native window + * resize functionality is broken on Windows. This is a known Electron bug. + * See: https://github.com/electron/electron/issues/48554 + * + * This method can be removed once the Electron bug is fixed. + */ + public resizeActionWindow(actionWindow: BrowserWindow, deltaX: number, deltaY: number, direction: string): void { + const bounds = actionWindow.getBounds() + const minWidth = 300 + const minHeight = 200 + + let { x, y, width, height } = bounds + + // Handle horizontal resize + if (direction.includes('e')) { + width = Math.max(minWidth, width + deltaX) + } + if (direction.includes('w')) { + const newWidth = Math.max(minWidth, width - deltaX) + if (newWidth !== width) { + x = x + (width - newWidth) + width = newWidth + } + } + + // Handle vertical resize + if (direction.includes('s')) { + height = Math.max(minHeight, height + deltaY) + } + if (direction.includes('n')) { + const newHeight = Math.max(minHeight, height - deltaY) + if (newHeight !== height) { + y = y + (height - newHeight) + height = newHeight + } + } + + actionWindow.setBounds({ x, y, width, height }) + } + /** * Update trigger mode behavior * Switches between selection-based and alt-key based triggering @@ -1510,6 +1554,18 @@ export class SelectionService { } }) + // [Windows only] Electron bug workaround - can be removed once fixed + // See: https://github.com/electron/electron/issues/48554 + ipcMain.handle( + IpcChannel.Selection_ActionWindowResize, + (event, deltaX: number, deltaY: number, direction: string) => { + const actionWindow = BrowserWindow.fromWebContents(event.sender) + if (actionWindow) { + selectionService?.resizeActionWindow(actionWindow, deltaX, deltaY, direction) + } + } + ) + this.isIpcHandlerRegistered = true } diff --git a/src/main/services/ShortcutService.ts b/src/main/services/ShortcutService.ts index 583dbbd95c..a84d8ac248 100644 --- a/src/main/services/ShortcutService.ts +++ b/src/main/services/ShortcutService.ts @@ -35,6 +35,15 @@ function getShortcutHandler(shortcut: Shortcut) { } case 'mini_window': return () => { + // 在处理器内部检查QuickAssistant状态,而不是在注册时检查 + const quickAssistantEnabled = configManager.getEnableQuickAssistant() + logger.info(`mini_window shortcut triggered, QuickAssistant enabled: ${quickAssistantEnabled}`) + + if (!quickAssistantEnabled) { + logger.warn('QuickAssistant is disabled, ignoring mini_window shortcut trigger') + return + } + windowService.toggleMiniWindow() } case 'selection_assistant_toggle': @@ -190,11 +199,10 @@ export function registerShortcuts(window: BrowserWindow) { break case 'mini_window': - //available only when QuickAssistant enabled - if (!configManager.getEnableQuickAssistant()) { - return - } + // 移除注册时的条件检查,在处理器内部进行检查 + logger.info(`Processing mini_window shortcut, enabled: ${shortcut.enabled}`) showMiniWindowAccelerator = formatShortcutKey(shortcut.shortcut) + logger.debug(`Mini window accelerator set to: ${showMiniWindowAccelerator}`) break case 'selection_assistant_toggle': diff --git a/src/main/services/WebviewService.ts b/src/main/services/WebviewService.ts index fb2049de74..7af008bd7a 100644 --- a/src/main/services/WebviewService.ts +++ b/src/main/services/WebviewService.ts @@ -1,5 +1,6 @@ import { IpcChannel } from '@shared/IpcChannel' -import { app, session, shell, webContents } from 'electron' +import { app, dialog, session, shell, webContents } from 'electron' +import { promises as fs } from 'fs' /** * init the useragent of the webview session @@ -53,11 +54,17 @@ const attachKeyboardHandler = (contents: Electron.WebContents) => { return } - const isFindShortcut = (input.control || input.meta) && key === 'f' - const isEscape = key === 'escape' - const isEnter = key === 'enter' + // Helper to check if this is a shortcut we handle + const isHandledShortcut = (k: string) => { + const isFindShortcut = (input.control || input.meta) && k === 'f' + const isPrintShortcut = (input.control || input.meta) && k === 'p' + const isSaveShortcut = (input.control || input.meta) && k === 's' + const isEscape = k === 'escape' + const isEnter = k === 'enter' + return isFindShortcut || isPrintShortcut || isSaveShortcut || isEscape || isEnter + } - if (!isFindShortcut && !isEscape && !isEnter) { + if (!isHandledShortcut(key)) { return } @@ -66,11 +73,20 @@ const attachKeyboardHandler = (contents: Electron.WebContents) => { return } + const isFindShortcut = (input.control || input.meta) && key === 'f' + const isPrintShortcut = (input.control || input.meta) && key === 'p' + const isSaveShortcut = (input.control || input.meta) && key === 's' + // Always prevent Cmd/Ctrl+F to override the guest page's native find dialog if (isFindShortcut) { event.preventDefault() } + // Prevent default print/save dialogs and handle them with custom logic + if (isPrintShortcut || isSaveShortcut) { + event.preventDefault() + } + // Send the hotkey event to the renderer // The renderer will decide whether to preventDefault for Escape and Enter // based on whether the search bar is visible @@ -100,3 +116,130 @@ export function initWebviewHotkeys() { attachKeyboardHandler(contents) }) } + +/** + * Print webview content to PDF + * @param webviewId The webview webContents id + * @returns Path to saved PDF file or null if user cancelled + */ +export async function printWebviewToPDF(webviewId: number): Promise { + const webview = webContents.fromId(webviewId) + if (!webview) { + throw new Error('Webview not found') + } + + try { + // Get the page title for default filename + const pageTitle = await webview.executeJavaScript('document.title || "webpage"').catch(() => 'webpage') + // Sanitize filename by removing invalid characters + const sanitizedTitle = pageTitle.replace(/[<>:"/\\|?*]/g, '-').substring(0, 100) + const defaultFilename = sanitizedTitle ? `${sanitizedTitle}.pdf` : `webpage-${Date.now()}.pdf` + + // Show save dialog + const { canceled, filePath } = await dialog.showSaveDialog({ + title: 'Save as PDF', + defaultPath: defaultFilename, + filters: [{ name: 'PDF Files', extensions: ['pdf'] }] + }) + + if (canceled || !filePath) { + return null + } + + // Generate PDF with settings to capture full page + const pdfData = await webview.printToPDF({ + margins: { + marginType: 'default' + }, + printBackground: true, + landscape: false, + pageSize: 'A4', + preferCSSPageSize: true + }) + + // Save PDF to file + await fs.writeFile(filePath, pdfData) + + return filePath + } catch (error) { + throw new Error(`Failed to print to PDF: ${(error as Error).message}`) + } +} + +/** + * Save webview content as HTML + * @param webviewId The webview webContents id + * @returns Path to saved HTML file or null if user cancelled + */ +export async function saveWebviewAsHTML(webviewId: number): Promise { + const webview = webContents.fromId(webviewId) + if (!webview) { + throw new Error('Webview not found') + } + + try { + // Get the page title for default filename + const pageTitle = await webview.executeJavaScript('document.title || "webpage"').catch(() => 'webpage') + // Sanitize filename by removing invalid characters + const sanitizedTitle = pageTitle.replace(/[<>:"/\\|?*]/g, '-').substring(0, 100) + const defaultFilename = sanitizedTitle ? `${sanitizedTitle}.html` : `webpage-${Date.now()}.html` + + // Show save dialog + const { canceled, filePath } = await dialog.showSaveDialog({ + title: 'Save as HTML', + defaultPath: defaultFilename, + filters: [ + { name: 'HTML Files', extensions: ['html', 'htm'] }, + { name: 'All Files', extensions: ['*'] } + ] + }) + + if (canceled || !filePath) { + return null + } + + // Get the HTML content with safe error handling + const html = await webview.executeJavaScript(` + (() => { + try { + // Build complete DOCTYPE string if present + let doctype = ''; + if (document.doctype) { + const dt = document.doctype; + doctype = ''; + } + return doctype + (document.documentElement?.outerHTML || ''); + } catch (error) { + // Fallback: just return the HTML without DOCTYPE if there's an error + return document.documentElement?.outerHTML || ''; + } + })() + `) + + // Save HTML to file + await fs.writeFile(filePath, html, 'utf-8') + + return filePath + } catch (error) { + throw new Error(`Failed to save as HTML: ${(error as Error).message}`) + } +} diff --git a/src/main/services/WindowService.ts b/src/main/services/WindowService.ts index 63eaaba995..3f96497e63 100644 --- a/src/main/services/WindowService.ts +++ b/src/main/services/WindowService.ts @@ -271,9 +271,9 @@ export class WindowService { 'https://account.siliconflow.cn/oauth', 'https://cloud.siliconflow.cn/bills', 'https://cloud.siliconflow.cn/expensebill', - 'https://aihubmix.com/token', - 'https://aihubmix.com/topup', - 'https://aihubmix.com/statistics', + 'https://console.aihubmix.com/token', + 'https://console.aihubmix.com/topup', + 'https://console.aihubmix.com/statistics', 'https://dash.302.ai/sso/login', 'https://dash.302.ai/charge', 'https://www.aiionly.com/login' diff --git a/src/main/services/__tests__/ServerLogBuffer.test.ts b/src/main/services/__tests__/ServerLogBuffer.test.ts new file mode 100644 index 0000000000..0b7abe91e8 --- /dev/null +++ b/src/main/services/__tests__/ServerLogBuffer.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from 'vitest' + +import { ServerLogBuffer } from '../mcp/ServerLogBuffer' + +describe('ServerLogBuffer', () => { + it('keeps a bounded number of entries per server', () => { + const buffer = new ServerLogBuffer(3) + const key = 'srv' + + buffer.append(key, { timestamp: 1, level: 'info', message: 'a' }) + buffer.append(key, { timestamp: 2, level: 'info', message: 'b' }) + buffer.append(key, { timestamp: 3, level: 'info', message: 'c' }) + buffer.append(key, { timestamp: 4, level: 'info', message: 'd' }) + + const logs = buffer.get(key) + expect(logs).toHaveLength(3) + expect(logs[0].message).toBe('b') + expect(logs[2].message).toBe('d') + }) + + it('isolates entries by server key', () => { + const buffer = new ServerLogBuffer(5) + buffer.append('one', { timestamp: 1, level: 'info', message: 'a' }) + buffer.append('two', { timestamp: 2, level: 'info', message: 'b' }) + + expect(buffer.get('one')).toHaveLength(1) + expect(buffer.get('two')).toHaveLength(1) + }) +}) diff --git a/src/main/services/agents/BaseService.ts b/src/main/services/agents/BaseService.ts index 1c9b438e4a..461fdab96d 100644 --- a/src/main/services/agents/BaseService.ts +++ b/src/main/services/agents/BaseService.ts @@ -1,17 +1,13 @@ -import { type Client, createClient } from '@libsql/client' import { loggerService } from '@logger' import { mcpApiService } from '@main/apiServer/services/mcp' import type { ModelValidationError } from '@main/apiServer/utils' import { validateModelId } from '@main/apiServer/utils' import type { AgentType, MCPTool, SlashCommand, Tool } from '@types' import { objectKeys } from '@types' -import { drizzle, type LibSQLDatabase } from 'drizzle-orm/libsql' import fs from 'fs' import path from 'path' -import { MigrationService } from './database/MigrationService' -import * as schema from './database/schema' -import { dbPath } from './drizzle.config' +import { DatabaseManager } from './database/DatabaseManager' import type { AgentModelField } from './errors' import { AgentModelValidationError } from './errors' import { builtinSlashCommands } from './services/claudecode/commands' @@ -20,22 +16,16 @@ import { builtinTools } from './services/claudecode/tools' const logger = loggerService.withContext('BaseService') /** - * Base service class providing shared database connection and utilities - * for all agent-related services. + * Base service class providing shared utilities for all agent-related services. * * Features: - * - Programmatic schema management (no CLI dependencies) - * - Automatic table creation and migration - * - Schema version tracking and compatibility checks - * - Transaction-based operations for safety - * - Development vs production mode handling - * - Connection retry logic with exponential backoff + * - Database access through DatabaseManager singleton + * - JSON field serialization/deserialization + * - Path validation and creation + * - Model validation + * - MCP tools and slash commands listing */ export abstract class BaseService { - protected static client: Client | null = null - protected static db: LibSQLDatabase | null = null - protected static isInitialized = false - protected static initializationPromise: Promise | null = null protected jsonFields: string[] = [ 'tools', 'mcps', @@ -45,23 +35,6 @@ export abstract class BaseService { 'slash_commands' ] - /** - * Initialize database with retry logic and proper error handling - */ - protected static async initialize(): Promise { - // Return existing initialization if in progress - if (BaseService.initializationPromise) { - return BaseService.initializationPromise - } - - if (BaseService.isInitialized) { - return - } - - BaseService.initializationPromise = BaseService.performInitialization() - return BaseService.initializationPromise - } - public async listMcpTools(agentType: AgentType, ids?: string[]): Promise { const tools: Tool[] = [] if (agentType === 'claude-code') { @@ -101,78 +74,13 @@ export abstract class BaseService { return [] } - private static async performInitialization(): Promise { - const maxRetries = 3 - let lastError: Error - - for (let attempt = 1; attempt <= maxRetries; attempt++) { - try { - logger.info(`Initializing Agent database at: ${dbPath} (attempt ${attempt}/${maxRetries})`) - - // Ensure the database directory exists - const dbDir = path.dirname(dbPath) - if (!fs.existsSync(dbDir)) { - logger.info(`Creating database directory: ${dbDir}`) - fs.mkdirSync(dbDir, { recursive: true }) - } - - BaseService.client = createClient({ - url: `file:${dbPath}` - }) - - BaseService.db = drizzle(BaseService.client, { schema }) - - // Run database migrations - const migrationService = new MigrationService(BaseService.db, BaseService.client) - await migrationService.runMigrations() - - BaseService.isInitialized = true - logger.info('Agent database initialized successfully') - return - } catch (error) { - lastError = error as Error - logger.warn(`Database initialization attempt ${attempt} failed:`, lastError) - - // Clean up on failure - if (BaseService.client) { - try { - BaseService.client.close() - } catch (closeError) { - logger.warn('Failed to close client during cleanup:', closeError as Error) - } - } - BaseService.client = null - BaseService.db = null - - // Wait before retrying (exponential backoff) - if (attempt < maxRetries) { - const delay = Math.pow(2, attempt) * 1000 // 2s, 4s, 8s - logger.info(`Retrying in ${delay}ms...`) - await new Promise((resolve) => setTimeout(resolve, delay)) - } - } - } - - // All retries failed - BaseService.initializationPromise = null - logger.error('Failed to initialize Agent database after all retries:', lastError!) - throw lastError! - } - - protected ensureInitialized(): void { - if (!BaseService.isInitialized || !BaseService.db || !BaseService.client) { - throw new Error('Database not initialized. Call initialize() first.') - } - } - - protected get database(): LibSQLDatabase { - this.ensureInitialized() - return BaseService.db! - } - - protected get rawClient(): Client { - this.ensureInitialized() - return BaseService.client! + /** + * Get database instance + * Automatically waits for initialization to complete + */ + public async getDatabase() { + const dbManager = await DatabaseManager.getInstance() + return dbManager.getDatabase() } protected serializeJsonFields(data: any): any { @@ -284,7 +192,7 @@ export abstract class BaseService { } /** - * Force re-initialization (for development/testing) + * Validate agent model configuration */ protected async validateAgentModels( agentType: AgentType, @@ -325,22 +233,4 @@ export abstract class BaseService { } } } - - static async reinitialize(): Promise { - BaseService.isInitialized = false - BaseService.initializationPromise = null - - if (BaseService.client) { - try { - BaseService.client.close() - } catch (error) { - logger.warn('Failed to close client during reinitialize:', error as Error) - } - } - - BaseService.client = null - BaseService.db = null - - await BaseService.initialize() - } } diff --git a/src/main/services/agents/database/DatabaseManager.ts b/src/main/services/agents/database/DatabaseManager.ts new file mode 100644 index 0000000000..f4b13971c7 --- /dev/null +++ b/src/main/services/agents/database/DatabaseManager.ts @@ -0,0 +1,156 @@ +import { type Client, createClient } from '@libsql/client' +import { loggerService } from '@logger' +import type { LibSQLDatabase } from 'drizzle-orm/libsql' +import { drizzle } from 'drizzle-orm/libsql' +import fs from 'fs' +import path from 'path' + +import { dbPath } from '../drizzle.config' +import { MigrationService } from './MigrationService' +import * as schema from './schema' + +const logger = loggerService.withContext('DatabaseManager') + +/** + * Database initialization state + */ +enum InitState { + INITIALIZING = 'initializing', + INITIALIZED = 'initialized', + FAILED = 'failed' +} + +/** + * DatabaseManager - Singleton class for managing libsql database connections + * + * Responsibilities: + * - Single source of truth for database connection + * - Thread-safe initialization with state management + * - Automatic migration handling + * - Safe connection cleanup + * - Error recovery and retry logic + * - Windows platform compatibility fixes + */ +export class DatabaseManager { + private static instance: DatabaseManager | null = null + + private client: Client | null = null + private db: LibSQLDatabase | null = null + private state: InitState = InitState.INITIALIZING + + /** + * Get the singleton instance (database initialization starts automatically) + */ + public static async getInstance(): Promise { + if (DatabaseManager.instance) { + return DatabaseManager.instance + } + + const instance = new DatabaseManager() + await instance.initialize() + DatabaseManager.instance = instance + + return instance + } + + /** + * Perform the actual initialization + */ + public async initialize(): Promise { + if (this.state === InitState.INITIALIZED) { + return + } + + try { + logger.info(`Initializing database at: ${dbPath}`) + + // Ensure database directory exists + const dbDir = path.dirname(dbPath) + if (!fs.existsSync(dbDir)) { + logger.info(`Creating database directory: ${dbDir}`) + fs.mkdirSync(dbDir, { recursive: true }) + } + + // Check if database file is corrupted (Windows specific check) + if (fs.existsSync(dbPath)) { + const stats = fs.statSync(dbPath) + if (stats.size === 0) { + logger.warn('Database file is empty, removing corrupted file') + fs.unlinkSync(dbPath) + } + } + + // Create client with platform-specific options + this.client = createClient({ + url: `file:${dbPath}`, + // intMode: 'number' helps avoid some Windows compatibility issues + intMode: 'number' + }) + + // Create drizzle instance + this.db = drizzle(this.client, { schema }) + + // Run migrations + const migrationService = new MigrationService(this.db, this.client) + await migrationService.runMigrations() + + this.state = InitState.INITIALIZED + logger.info('Database initialized successfully') + } catch (error) { + const err = error as Error + logger.error('Database initialization failed:', { + error: err.message, + stack: err.stack + }) + + // Clean up failed initialization + this.cleanupFailedInit() + + // Set failed state + this.state = InitState.FAILED + throw new Error(`Database initialization failed: ${err.message || 'Unknown error'}`) + } + } + + /** + * Clean up after failed initialization + */ + private cleanupFailedInit(): void { + if (this.client) { + try { + // On Windows, closing a partially initialized client can crash + // Wrap in try-catch and ignore errors during cleanup + this.client.close() + } catch (error) { + logger.warn('Failed to close client during cleanup:', error as Error) + } + } + this.client = null + this.db = null + } + + /** + * Get the database instance + * Automatically waits for initialization to complete + * @throws Error if database initialization failed + */ + public getDatabase(): LibSQLDatabase { + return this.db! + } + + /** + * Get the raw client (for advanced operations) + * Automatically waits for initialization to complete + * @throws Error if database initialization failed + */ + public async getClient(): Promise { + return this.client! + } + + /** + * Check if database is initialized + */ + public isInitialized(): boolean { + return this.state === InitState.INITIALIZED + } +} diff --git a/src/main/services/agents/database/index.ts b/src/main/services/agents/database/index.ts index 61b3a9ffcc..43302a6b25 100644 --- a/src/main/services/agents/database/index.ts +++ b/src/main/services/agents/database/index.ts @@ -7,8 +7,14 @@ * Schema evolution is handled by Drizzle Kit migrations. */ +// Database Manager (Singleton) +export * from './DatabaseManager' + // Drizzle ORM schemas export * from './schema' // Repository helpers export * from './sessionMessageRepository' + +// Migration Service +export * from './MigrationService' diff --git a/src/main/services/agents/database/sessionMessageRepository.ts b/src/main/services/agents/database/sessionMessageRepository.ts index 4567c61ec0..a9b1d2e572 100644 --- a/src/main/services/agents/database/sessionMessageRepository.ts +++ b/src/main/services/agents/database/sessionMessageRepository.ts @@ -15,26 +15,16 @@ import { sessionMessagesTable } from './schema' const logger = loggerService.withContext('AgentMessageRepository') -type TxClient = any - export type PersistUserMessageParams = AgentMessageUserPersistPayload & { sessionId: string agentSessionId?: string - tx?: TxClient } export type PersistAssistantMessageParams = AgentMessageAssistantPersistPayload & { sessionId: string agentSessionId: string - tx?: TxClient } -type PersistExchangeParams = AgentMessagePersistExchangePayload & { - tx?: TxClient -} - -type PersistExchangeResult = AgentMessagePersistExchangeResult - class AgentMessageRepository extends BaseService { private static instance: AgentMessageRepository | null = null @@ -87,17 +77,13 @@ class AgentMessageRepository extends BaseService { return deserialized } - private getWriter(tx?: TxClient): TxClient { - return tx ?? this.database - } - private async findExistingMessageRow( - writer: TxClient, sessionId: string, role: string, messageId: string ): Promise { - const candidateRows: SessionMessageRow[] = await writer + const database = await this.getDatabase() + const candidateRows: SessionMessageRow[] = await database .select() .from(sessionMessagesTable) .where(and(eq(sessionMessagesTable.session_id, sessionId), eq(sessionMessagesTable.role, role))) @@ -122,10 +108,7 @@ class AgentMessageRepository extends BaseService { private async upsertMessage( params: PersistUserMessageParams | PersistAssistantMessageParams ): Promise { - await AgentMessageRepository.initialize() - this.ensureInitialized() - - const { sessionId, agentSessionId = '', payload, metadata, createdAt, tx } = params + const { sessionId, agentSessionId = '', payload, metadata, createdAt } = params if (!payload?.message?.role) { throw new Error('Message payload missing role') @@ -135,18 +118,18 @@ class AgentMessageRepository extends BaseService { throw new Error('Message payload missing id') } - const writer = this.getWriter(tx) + const database = await this.getDatabase() const now = createdAt ?? payload.message.createdAt ?? new Date().toISOString() const serializedPayload = this.serializeMessage(payload) const serializedMetadata = this.serializeMetadata(metadata) - const existingRow = await this.findExistingMessageRow(writer, sessionId, payload.message.role, payload.message.id) + const existingRow = await this.findExistingMessageRow(sessionId, payload.message.role, payload.message.id) if (existingRow) { const metadataToPersist = serializedMetadata ?? existingRow.metadata ?? undefined const agentSessionToPersist = agentSessionId || existingRow.agent_session_id || '' - await writer + await database .update(sessionMessagesTable) .set({ content: serializedPayload, @@ -175,7 +158,7 @@ class AgentMessageRepository extends BaseService { updated_at: now } - const [saved] = await writer.insert(sessionMessagesTable).values(insertData).returning() + const [saved] = await database.insert(sessionMessagesTable).values(insertData).returning() return this.deserialize(saved) } @@ -188,49 +171,38 @@ class AgentMessageRepository extends BaseService { return this.upsertMessage(params) } - async persistExchange(params: PersistExchangeParams): Promise { - await AgentMessageRepository.initialize() - this.ensureInitialized() - + async persistExchange(params: AgentMessagePersistExchangePayload): Promise { const { sessionId, agentSessionId, user, assistant } = params - const result = await this.database.transaction(async (tx) => { - const exchangeResult: PersistExchangeResult = {} + const exchangeResult: AgentMessagePersistExchangeResult = {} - if (user?.payload) { - exchangeResult.userMessage = await this.persistUserMessage({ - sessionId, - agentSessionId, - payload: user.payload, - metadata: user.metadata, - createdAt: user.createdAt, - tx - }) - } + if (user?.payload) { + exchangeResult.userMessage = await this.persistUserMessage({ + sessionId, + agentSessionId, + payload: user.payload, + metadata: user.metadata, + createdAt: user.createdAt + }) + } - if (assistant?.payload) { - exchangeResult.assistantMessage = await this.persistAssistantMessage({ - sessionId, - agentSessionId, - payload: assistant.payload, - metadata: assistant.metadata, - createdAt: assistant.createdAt, - tx - }) - } + if (assistant?.payload) { + exchangeResult.assistantMessage = await this.persistAssistantMessage({ + sessionId, + agentSessionId, + payload: assistant.payload, + metadata: assistant.metadata, + createdAt: assistant.createdAt + }) + } - return exchangeResult - }) - - return result + return exchangeResult } async getSessionHistory(sessionId: string): Promise { - await AgentMessageRepository.initialize() - this.ensureInitialized() - try { - const rows = await this.database + const database = await this.getDatabase() + const rows = await database .select() .from(sessionMessagesTable) .where(eq(sessionMessagesTable.session_id, sessionId)) diff --git a/src/main/services/agents/services/AgentService.ts b/src/main/services/agents/services/AgentService.ts index 07ed89a0f3..2faa87bb45 100644 --- a/src/main/services/agents/services/AgentService.ts +++ b/src/main/services/agents/services/AgentService.ts @@ -32,14 +32,8 @@ export class AgentService extends BaseService { return AgentService.instance } - async initialize(): Promise { - await BaseService.initialize() - } - // Agent Methods async createAgent(req: CreateAgentRequest): Promise { - this.ensureInitialized() - const id = `agent_${Date.now()}_${Math.random().toString(36).substring(2, 11)}` const now = new Date().toISOString() @@ -75,8 +69,9 @@ export class AgentService extends BaseService { updated_at: now } - await this.database.insert(agentsTable).values(insertData) - const result = await this.database.select().from(agentsTable).where(eq(agentsTable.id, id)).limit(1) + const database = await this.getDatabase() + await database.insert(agentsTable).values(insertData) + const result = await database.select().from(agentsTable).where(eq(agentsTable.id, id)).limit(1) if (!result[0]) { throw new Error('Failed to create agent') } @@ -86,9 +81,8 @@ export class AgentService extends BaseService { } async getAgent(id: string): Promise { - this.ensureInitialized() - - const result = await this.database.select().from(agentsTable).where(eq(agentsTable.id, id)).limit(1) + const database = await this.getDatabase() + const result = await database.select().from(agentsTable).where(eq(agentsTable.id, id)).limit(1) if (!result[0]) { return null @@ -118,9 +112,9 @@ export class AgentService extends BaseService { } async listAgents(options: ListOptions = {}): Promise<{ agents: AgentEntity[]; total: number }> { - this.ensureInitialized() // Build query with pagination - - const totalResult = await this.database.select({ count: count() }).from(agentsTable) + // Build query with pagination + const database = await this.getDatabase() + const totalResult = await database.select({ count: count() }).from(agentsTable) const sortBy = options.sortBy || 'created_at' const orderBy = options.orderBy || 'desc' @@ -128,7 +122,7 @@ export class AgentService extends BaseService { const sortField = agentsTable[sortBy] const orderFn = orderBy === 'asc' ? asc : desc - const baseQuery = this.database.select().from(agentsTable).orderBy(orderFn(sortField)) + const baseQuery = database.select().from(agentsTable).orderBy(orderFn(sortField)) const result = options.limit !== undefined @@ -151,8 +145,6 @@ export class AgentService extends BaseService { updates: UpdateAgentRequest, options: { replace?: boolean } = {} ): Promise { - this.ensureInitialized() - // Check if agent exists const existing = await this.getAgent(id) if (!existing) { @@ -195,22 +187,21 @@ export class AgentService extends BaseService { } } - await this.database.update(agentsTable).set(updateData).where(eq(agentsTable.id, id)) + const database = await this.getDatabase() + await database.update(agentsTable).set(updateData).where(eq(agentsTable.id, id)) return await this.getAgent(id) } async deleteAgent(id: string): Promise { - this.ensureInitialized() - - const result = await this.database.delete(agentsTable).where(eq(agentsTable.id, id)) + const database = await this.getDatabase() + const result = await database.delete(agentsTable).where(eq(agentsTable.id, id)) return result.rowsAffected > 0 } async agentExists(id: string): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .select({ id: agentsTable.id }) .from(agentsTable) .where(eq(agentsTable.id, id)) diff --git a/src/main/services/agents/services/SessionMessageService.ts b/src/main/services/agents/services/SessionMessageService.ts index 46435fa371..48ef8621ef 100644 --- a/src/main/services/agents/services/SessionMessageService.ts +++ b/src/main/services/agents/services/SessionMessageService.ts @@ -104,14 +104,9 @@ export class SessionMessageService extends BaseService { return SessionMessageService.instance } - async initialize(): Promise { - await BaseService.initialize() - } - async sessionMessageExists(id: number): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .select({ id: sessionMessagesTable.id }) .from(sessionMessagesTable) .where(eq(sessionMessagesTable.id, id)) @@ -124,10 +119,9 @@ export class SessionMessageService extends BaseService { sessionId: string, options: ListOptions = {} ): Promise<{ messages: AgentSessionMessageEntity[] }> { - this.ensureInitialized() - // Get messages with pagination - const baseQuery = this.database + const database = await this.getDatabase() + const baseQuery = database .select() .from(sessionMessagesTable) .where(eq(sessionMessagesTable.session_id, sessionId)) @@ -146,9 +140,8 @@ export class SessionMessageService extends BaseService { } async deleteSessionMessage(sessionId: string, messageId: number): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .delete(sessionMessagesTable) .where(and(eq(sessionMessagesTable.id, messageId), eq(sessionMessagesTable.session_id, sessionId))) @@ -160,8 +153,6 @@ export class SessionMessageService extends BaseService { messageData: CreateSessionMessageRequest, abortController: AbortController ): Promise { - this.ensureInitialized() - return await this.startSessionMessageStream(session, messageData, abortController) } @@ -270,10 +261,9 @@ export class SessionMessageService extends BaseService { } private async getLastAgentSessionId(sessionId: string): Promise { - this.ensureInitialized() - try { - const result = await this.database + const database = await this.getDatabase() + const result = await database .select({ agent_session_id: sessionMessagesTable.agent_session_id }) .from(sessionMessagesTable) .where(and(eq(sessionMessagesTable.session_id, sessionId), not(eq(sessionMessagesTable.agent_session_id, '')))) diff --git a/src/main/services/agents/services/SessionService.ts b/src/main/services/agents/services/SessionService.ts index c9ecf72c32..d933ef8dd9 100644 --- a/src/main/services/agents/services/SessionService.ts +++ b/src/main/services/agents/services/SessionService.ts @@ -30,10 +30,6 @@ export class SessionService extends BaseService { return SessionService.instance } - async initialize(): Promise { - await BaseService.initialize() - } - /** * Override BaseService.listSlashCommands to merge builtin and plugin commands */ @@ -84,13 +80,12 @@ export class SessionService extends BaseService { agentId: string, req: Partial = {} ): Promise { - this.ensureInitialized() - // Validate agent exists - we'll need to import AgentService for this check // For now, we'll skip this validation to avoid circular dependencies // The database foreign key constraint will handle this - const agents = await this.database.select().from(agentsTable).where(eq(agentsTable.id, agentId)).limit(1) + const database = await this.getDatabase() + const agents = await database.select().from(agentsTable).where(eq(agentsTable.id, agentId)).limit(1) if (!agents[0]) { throw new Error('Agent not found') } @@ -135,9 +130,10 @@ export class SessionService extends BaseService { updated_at: now } - await this.database.insert(sessionsTable).values(insertData) + const db = await this.getDatabase() + await db.insert(sessionsTable).values(insertData) - const result = await this.database.select().from(sessionsTable).where(eq(sessionsTable.id, id)).limit(1) + const result = await db.select().from(sessionsTable).where(eq(sessionsTable.id, id)).limit(1) if (!result[0]) { throw new Error('Failed to create session') @@ -148,9 +144,8 @@ export class SessionService extends BaseService { } async getSession(agentId: string, id: string): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .select() .from(sessionsTable) .where(and(eq(sessionsTable.id, id), eq(sessionsTable.agent_id, agentId))) @@ -176,8 +171,6 @@ export class SessionService extends BaseService { agentId?: string, options: ListOptions = {} ): Promise<{ sessions: AgentSessionEntity[]; total: number }> { - this.ensureInitialized() - // Build where conditions const whereConditions: SQL[] = [] if (agentId) { @@ -192,16 +185,13 @@ export class SessionService extends BaseService { : undefined // Get total count - const totalResult = await this.database.select({ count: count() }).from(sessionsTable).where(whereClause) + const database = await this.getDatabase() + const totalResult = await database.select({ count: count() }).from(sessionsTable).where(whereClause) const total = totalResult[0].count // Build list query with pagination - sort by updated_at descending (latest first) - const baseQuery = this.database - .select() - .from(sessionsTable) - .where(whereClause) - .orderBy(desc(sessionsTable.updated_at)) + const baseQuery = database.select().from(sessionsTable).where(whereClause).orderBy(desc(sessionsTable.updated_at)) const result = options.limit !== undefined @@ -220,8 +210,6 @@ export class SessionService extends BaseService { id: string, updates: UpdateSessionRequest ): Promise { - this.ensureInitialized() - // Check if session exists const existing = await this.getSession(agentId, id) if (!existing) { @@ -262,15 +250,15 @@ export class SessionService extends BaseService { } } - await this.database.update(sessionsTable).set(updateData).where(eq(sessionsTable.id, id)) + const database = await this.getDatabase() + await database.update(sessionsTable).set(updateData).where(eq(sessionsTable.id, id)) return await this.getSession(agentId, id) } async deleteSession(agentId: string, id: string): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .delete(sessionsTable) .where(and(eq(sessionsTable.id, id), eq(sessionsTable.agent_id, agentId))) @@ -278,9 +266,8 @@ export class SessionService extends BaseService { } async sessionExists(agentId: string, id: string): Promise { - this.ensureInitialized() - - const result = await this.database + const database = await this.getDatabase() + const result = await database .select({ id: sessionsTable.id }) .from(sessionsTable) .where(and(eq(sessionsTable.id, id), eq(sessionsTable.agent_id, agentId))) diff --git a/src/main/services/agents/services/claudecode/__tests__/transform.test.ts b/src/main/services/agents/services/claudecode/__tests__/transform.test.ts index 8f8c1df038..2565f5e605 100644 --- a/src/main/services/agents/services/claudecode/__tests__/transform.test.ts +++ b/src/main/services/agents/services/claudecode/__tests__/transform.test.ts @@ -21,11 +21,16 @@ describe('stripLocalCommandTags', () => { 'line1\nkeep\nError' expect(stripLocalCommandTags(input)).toBe('line1\nkeep\nError') }) + + it('if no tags present, returns original string', () => { + const input = 'just some normal text' + expect(stripLocalCommandTags(input)).toBe(input) + }) }) describe('Claude → AiSDK transform', () => { it('handles tool call streaming lifecycle', () => { - const state = new ClaudeStreamState() + const state = new ClaudeStreamState({ agentSessionId: baseStreamMetadata.session_id }) const parts: ReturnType[number][] = [] const messages: SDKMessage[] = [ @@ -182,14 +187,119 @@ describe('Claude → AiSDK transform', () => { (typeof parts)[number], { type: 'tool-result' } > - expect(toolResult.toolCallId).toBe('tool-1') + expect(toolResult.toolCallId).toBe('session-123:tool-1') expect(toolResult.toolName).toBe('Bash') expect(toolResult.input).toEqual({ command: 'ls' }) expect(toolResult.output).toBe('ok') }) + it('handles tool calls without streaming events (no content_block_start/stop)', () => { + const state = new ClaudeStreamState({ agentSessionId: '12344' }) + const parts: ReturnType[number][] = [] + + const messages: SDKMessage[] = [ + { + ...baseStreamMetadata, + type: 'assistant', + uuid: uuid(20), + message: { + id: 'msg-tool-no-stream', + type: 'message', + role: 'assistant', + model: 'claude-test', + content: [ + { + type: 'tool_use', + id: 'tool-read', + name: 'Read', + input: { file_path: '/test.txt' } + }, + { + type: 'tool_use', + id: 'tool-bash', + name: 'Bash', + input: { command: 'ls -la' } + } + ], + stop_reason: 'tool_use', + stop_sequence: null, + usage: { + input_tokens: 10, + output_tokens: 20 + } + } + } as unknown as SDKMessage, + { + ...baseStreamMetadata, + type: 'user', + uuid: uuid(21), + message: { + role: 'user', + content: [ + { + type: 'tool_result', + tool_use_id: 'tool-read', + content: 'file contents', + is_error: false + } + ] + } + } as SDKMessage, + { + ...baseStreamMetadata, + type: 'user', + uuid: uuid(22), + message: { + role: 'user', + content: [ + { + type: 'tool_result', + tool_use_id: 'tool-bash', + content: 'total 42\n...', + is_error: false + } + ] + } + } as SDKMessage + ] + + for (const message of messages) { + const transformed = transformSDKMessageToStreamParts(message, state) + parts.push(...transformed) + } + + const types = parts.map((part) => part.type) + expect(types).toEqual(['tool-call', 'tool-call', 'tool-result', 'tool-result']) + + const toolCalls = parts.filter((part) => part.type === 'tool-call') as Extract< + (typeof parts)[number], + { type: 'tool-call' } + >[] + expect(toolCalls).toHaveLength(2) + expect(toolCalls[0].toolName).toBe('Read') + expect(toolCalls[0].toolCallId).toBe('12344:tool-read') + expect(toolCalls[1].toolName).toBe('Bash') + expect(toolCalls[1].toolCallId).toBe('12344:tool-bash') + + const toolResults = parts.filter((part) => part.type === 'tool-result') as Extract< + (typeof parts)[number], + { type: 'tool-result' } + >[] + expect(toolResults).toHaveLength(2) + // This is the key assertion - toolName should NOT be 'unknown' + expect(toolResults[0].toolName).toBe('Read') + expect(toolResults[0].toolCallId).toBe('12344:tool-read') + expect(toolResults[0].input).toEqual({ file_path: '/test.txt' }) + expect(toolResults[0].output).toBe('file contents') + + expect(toolResults[1].toolName).toBe('Bash') + expect(toolResults[1].toolCallId).toBe('12344:tool-bash') + expect(toolResults[1].input).toEqual({ command: 'ls -la' }) + expect(toolResults[1].output).toBe('total 42\n...') + }) + it('handles streaming text completion', () => { - const state = new ClaudeStreamState() + const state = new ClaudeStreamState({ agentSessionId: baseStreamMetadata.session_id }) const parts: ReturnType[number][] = [] const messages: SDKMessage[] = [ @@ -300,4 +410,87 @@ describe('Claude → AiSDK transform', () => { expect(finishStep.finishReason).toBe('stop') expect(finishStep.usage).toEqual({ inputTokens: 2, outputTokens: 4, totalTokens: 6 }) }) + + it('emits fallback text when Claude sends a snapshot instead of deltas', () => { + const state = new ClaudeStreamState({ agentSessionId: '12344' }) + const parts: ReturnType[number][] = [] + + const messages: SDKMessage[] = [ + { + ...baseStreamMetadata, + type: 'stream_event', + uuid: uuid(30), + event: { + type: 'message_start', + message: { + id: 'msg-fallback', + type: 'message', + role: 'assistant', + model: 'claude-test', + content: [], + stop_reason: null, + stop_sequence: null, + usage: {} + } + } + } as unknown as SDKMessage, + { + ...baseStreamMetadata, + type: 'stream_event', + uuid: uuid(31), + event: { + type: 'content_block_start', + index: 0, + content_block: { + type: 'text', + text: '' + } + } + } as unknown as SDKMessage, + { + ...baseStreamMetadata, + type: 'assistant', + uuid: uuid(32), + message: { + id: 'msg-fallback-content', + type: 'message', + role: 'assistant', + model: 'claude-test', + content: [ + { + type: 'text', + text: 'Final answer without streaming deltas.' + } + ], + stop_reason: 'end_turn', + stop_sequence: null, + usage: { + input_tokens: 3, + output_tokens: 7 + } + } + } as unknown as SDKMessage + ] + + for (const message of messages) { + const transformed = transformSDKMessageToStreamParts(message, state) + parts.push(...transformed) + } + + const types = parts.map((part) => part.type) + expect(types).toEqual(['start-step', 'text-start', 'text-delta', 'text-end', 'finish-step']) + + const delta = parts.find((part) => part.type === 'text-delta') as Extract< + (typeof parts)[number], + { type: 'text-delta' } + > + expect(delta.text).toBe('Final answer without streaming deltas.') + + const finish = parts.find((part) => part.type === 'finish-step') as Extract< + (typeof parts)[number], + { type: 'finish-step' } + > + expect(finish.usage).toEqual({ inputTokens: 3, outputTokens: 7, totalTokens: 10 }) + expect(finish.finishReason).toBe('stop') + }) }) diff --git a/src/main/services/agents/services/claudecode/claude-stream-state.ts b/src/main/services/agents/services/claudecode/claude-stream-state.ts index 078f048ce8..30b5790c82 100644 --- a/src/main/services/agents/services/claudecode/claude-stream-state.ts +++ b/src/main/services/agents/services/claudecode/claude-stream-state.ts @@ -10,8 +10,21 @@ * Every Claude turn gets its own instance. `resetStep` should be invoked once the finish event has * been emitted to avoid leaking state into the next turn. */ +import { loggerService } from '@logger' import type { FinishReason, LanguageModelUsage, ProviderMetadata } from 'ai' +/** + * Builds a namespaced tool call ID by combining session ID with raw tool call ID. + * This ensures tool calls from different sessions don't conflict even if they have + * the same raw ID from the SDK. + * + * @param sessionId - The agent session ID + * @param rawToolCallId - The raw tool call ID from SDK (e.g., "WebFetch_0") + */ +export function buildNamespacedToolCallId(sessionId: string, rawToolCallId: string): string { + return `${sessionId}:${rawToolCallId}` +} + /** * Shared fields for every block that Claude can stream (text, reasoning, tool). */ @@ -34,6 +47,7 @@ type ReasoningBlockState = BaseBlockState & { type ToolBlockState = BaseBlockState & { kind: 'tool' toolCallId: string + rawToolCallId: string toolName: string inputBuffer: string providerMetadata?: ProviderMetadata @@ -48,12 +62,17 @@ type PendingUsageState = { } type PendingToolCall = { + rawToolCallId: string toolCallId: string toolName: string input: unknown providerMetadata?: ProviderMetadata } +type ClaudeStreamStateOptions = { + agentSessionId: string +} + /** * Tracks the lifecycle of Claude streaming blocks (text, thinking, tool calls) * across individual websocket events. The transformer relies on this class to @@ -61,12 +80,20 @@ type PendingToolCall = { * usage/finish metadata once Anthropic closes a message. */ export class ClaudeStreamState { + private logger + private readonly agentSessionId: string private blocksByIndex = new Map() - private toolIndexById = new Map() + private toolIndexByNamespacedId = new Map() private pendingUsage: PendingUsageState = {} private pendingToolCalls = new Map() private stepActive = false + constructor(options: ClaudeStreamStateOptions) { + this.logger = loggerService.withContext('ClaudeStreamState') + this.agentSessionId = options.agentSessionId + this.logger.silly('ClaudeStreamState', options) + } + /** Marks the beginning of a new AiSDK step. */ beginStep(): void { this.stepActive = true @@ -104,19 +131,21 @@ export class ClaudeStreamState { /** Caches tool metadata so subsequent input deltas and results can find it. */ openToolBlock( index: number, - params: { toolCallId: string; toolName: string; providerMetadata?: ProviderMetadata } + params: { rawToolCallId: string; toolName: string; providerMetadata?: ProviderMetadata } ): ToolBlockState { + const toolCallId = buildNamespacedToolCallId(this.agentSessionId, params.rawToolCallId) const block: ToolBlockState = { kind: 'tool', - id: params.toolCallId, + id: toolCallId, index, - toolCallId: params.toolCallId, + toolCallId, + rawToolCallId: params.rawToolCallId, toolName: params.toolName, inputBuffer: '', providerMetadata: params.providerMetadata } this.blocksByIndex.set(index, block) - this.toolIndexById.set(params.toolCallId, index) + this.toolIndexByNamespacedId.set(toolCallId, index) return block } @@ -124,14 +153,32 @@ export class ClaudeStreamState { return this.blocksByIndex.get(index) } + getFirstOpenTextBlock(): TextBlockState | undefined { + const candidates: TextBlockState[] = [] + for (const block of this.blocksByIndex.values()) { + if (block.kind === 'text') { + candidates.push(block) + } + } + if (candidates.length === 0) { + return undefined + } + candidates.sort((a, b) => a.index - b.index) + return candidates[0] + } + getToolBlockById(toolCallId: string): ToolBlockState | undefined { - const index = this.toolIndexById.get(toolCallId) + const index = this.toolIndexByNamespacedId.get(toolCallId) if (index === undefined) return undefined const block = this.blocksByIndex.get(index) if (!block || block.kind !== 'tool') return undefined return block } + getToolBlockByRawId(rawToolCallId: string): ToolBlockState | undefined { + return this.getToolBlockById(buildNamespacedToolCallId(this.agentSessionId, rawToolCallId)) + } + /** Appends streamed text to a text block, returning the updated state when present. */ appendTextDelta(index: number, text: string): TextBlockState | undefined { const block = this.blocksByIndex.get(index) @@ -158,10 +205,12 @@ export class ClaudeStreamState { /** Records a tool call to be consumed once its result arrives from the user. */ registerToolCall( - toolCallId: string, + rawToolCallId: string, payload: { toolName: string; input: unknown; providerMetadata?: ProviderMetadata } ): void { - this.pendingToolCalls.set(toolCallId, { + const toolCallId = buildNamespacedToolCallId(this.agentSessionId, rawToolCallId) + this.pendingToolCalls.set(rawToolCallId, { + rawToolCallId, toolCallId, toolName: payload.toolName, input: payload.input, @@ -170,10 +219,10 @@ export class ClaudeStreamState { } /** Retrieves and clears the buffered tool call metadata for the given id. */ - consumePendingToolCall(toolCallId: string): PendingToolCall | undefined { - const entry = this.pendingToolCalls.get(toolCallId) + consumePendingToolCall(rawToolCallId: string): PendingToolCall | undefined { + const entry = this.pendingToolCalls.get(rawToolCallId) if (entry) { - this.pendingToolCalls.delete(toolCallId) + this.pendingToolCalls.delete(rawToolCallId) } return entry } @@ -182,13 +231,13 @@ export class ClaudeStreamState { * Persists the final input payload for a tool block once the provider signals * completion so that downstream tool results can reference the original call. */ - completeToolBlock(toolCallId: string, input: unknown, providerMetadata?: ProviderMetadata): void { + completeToolBlock(toolCallId: string, toolName: string, input: unknown, providerMetadata?: ProviderMetadata): void { + const block = this.getToolBlockByRawId(toolCallId) this.registerToolCall(toolCallId, { - toolName: this.getToolBlockById(toolCallId)?.toolName ?? 'unknown', + toolName, input, providerMetadata }) - const block = this.getToolBlockById(toolCallId) if (block) { block.resolvedInput = input } @@ -200,7 +249,7 @@ export class ClaudeStreamState { if (!block) return undefined this.blocksByIndex.delete(index) if (block.kind === 'tool') { - this.toolIndexById.delete(block.toolCallId) + this.toolIndexByNamespacedId.delete(block.toolCallId) } return block } @@ -227,7 +276,7 @@ export class ClaudeStreamState { /** Drops cached block metadata for the currently active message. */ resetBlocks(): void { this.blocksByIndex.clear() - this.toolIndexById.clear() + this.toolIndexByNamespacedId.clear() } /** Resets the entire step lifecycle after emitting a terminal frame. */ @@ -236,6 +285,10 @@ export class ClaudeStreamState { this.resetPendingUsage() this.stepActive = false } + + getNamespacedToolCallId(rawToolCallId: string): string { + return buildNamespacedToolCallId(this.agentSessionId, rawToolCallId) + } } export type { PendingToolCall } diff --git a/src/main/services/agents/services/claudecode/index.ts b/src/main/services/agents/services/claudecode/index.ts index a8f3f54fa8..ba863f7c50 100644 --- a/src/main/services/agents/services/claudecode/index.ts +++ b/src/main/services/agents/services/claudecode/index.ts @@ -1,18 +1,29 @@ // src/main/services/agents/services/claudecode/index.ts import { EventEmitter } from 'node:events' import { createRequire } from 'node:module' +import path from 'node:path' -import type { CanUseTool, McpHttpServerConfig, Options, SDKMessage } from '@anthropic-ai/claude-agent-sdk' +import type { + CanUseTool, + HookCallback, + McpHttpServerConfig, + Options, + PreToolUseHookInput, + SDKMessage +} from '@anthropic-ai/claude-agent-sdk' import { query } from '@anthropic-ai/claude-agent-sdk' import { loggerService } from '@logger' import { config as apiConfigService } from '@main/apiServer/config' import { validateModelId } from '@main/apiServer/utils' +import { ConfigKeys, configManager } from '@main/services/ConfigManager' +import { validateGitBashPath } from '@main/utils/process' import getLoginShellEnvironment from '@main/utils/shell-env' import { app } from 'electron' import type { GetAgentSessionResponse } from '../..' import type { AgentServiceInterface, AgentStream, AgentStreamEvent } from '../../interfaces/AgentStreamInterface' import { sessionService } from '../SessionService' +import { buildNamespacedToolCallId } from './claude-stream-state' import { promptForToolApproval } from './tool-permissions' import { ClaudeStreamState, transformSDKMessageToStreamParts } from './transform' @@ -98,6 +109,8 @@ class ClaudeCodeService implements AgentServiceInterface { Object.entries(loginShellEnv).filter(([key]) => !key.toLowerCase().endsWith('_proxy')) ) as Record + const customGitBashPath = validateGitBashPath(configManager.get(ConfigKeys.GitBashPath) as string | undefined) + const env = { ...loginShellEnvWithoutProxies, // TODO: fix the proxy api server @@ -113,7 +126,12 @@ class ClaudeCodeService implements AgentServiceInterface { // TODO: support set small model in UI ANTHROPIC_DEFAULT_HAIKU_MODEL: modelInfo.modelId, ELECTRON_RUN_AS_NODE: '1', - ELECTRON_NO_ATTACH_CONSOLE: '1' + ELECTRON_NO_ATTACH_CONSOLE: '1', + // Set CLAUDE_CONFIG_DIR to app's userData directory to avoid path encoding issues + // on Windows when the username contains non-ASCII characters (e.g., Chinese characters) + // This prevents the SDK from using the user's home directory which may have encoding problems + CLAUDE_CONFIG_DIR: path.join(app.getPath('userData'), '.claude'), + ...(customGitBashPath ? { CLAUDE_CODE_GIT_BASH_PATH: customGitBashPath } : {}) } const errorChunks: string[] = [] @@ -150,7 +168,67 @@ class ClaudeCodeService implements AgentServiceInterface { return { behavior: 'allow', updatedInput: input } } - return promptForToolApproval(toolName, input, options) + return promptForToolApproval(toolName, input, { + ...options, + toolCallId: buildNamespacedToolCallId(session.id, options.toolUseID) + }) + } + + const preToolUseHook: HookCallback = async (input, toolUseID, options) => { + // Type guard to ensure we're handling PreToolUse event + if (input.hook_event_name !== 'PreToolUse') { + return {} + } + + const hookInput = input as PreToolUseHookInput + const toolName = hookInput.tool_name + + logger.debug('PreToolUse hook triggered', { + session_id: hookInput.session_id, + tool_name: hookInput.tool_name, + tool_use_id: toolUseID, + tool_input: hookInput.tool_input, + cwd: hookInput.cwd, + permission_mode: hookInput.permission_mode, + autoAllowTools: autoAllowTools + }) + + if (options?.signal?.aborted) { + logger.debug('PreToolUse hook signal already aborted; skipping tool use', { + tool_name: hookInput.tool_name + }) + return {} + } + + // handle auto approved tools since it never triggers canUseTool + const normalizedToolName = normalizeToolName(toolName) + if (toolUseID) { + const bypassAll = input.permission_mode === 'bypassPermissions' + const autoAllowed = autoAllowTools.has(toolName) || autoAllowTools.has(normalizedToolName) + if (bypassAll || autoAllowed) { + const namespacedToolCallId = buildNamespacedToolCallId(session.id, toolUseID) + logger.debug('handling auto approved tools', { + toolName, + normalizedToolName, + namespacedToolCallId, + permission_mode: input.permission_mode, + autoAllowTools + }) + const isRecord = (v: unknown): v is Record => { + return !!v && typeof v === 'object' && !Array.isArray(v) + } + const toolInput = isRecord(input.tool_input) ? input.tool_input : {} + + await promptForToolApproval(toolName, toolInput, { + ...options, + toolCallId: namespacedToolCallId, + autoApprove: true + }) + } + } + + // Return to proceed without modification + return {} } // Build SDK options from parameters @@ -176,7 +254,14 @@ class ClaudeCodeService implements AgentServiceInterface { permissionMode: session.configuration?.permission_mode, maxTurns: session.configuration?.max_turns, allowedTools: session.allowed_tools, - canUseTool + canUseTool, + hooks: { + PreToolUse: [ + { + hooks: [preToolUseHook] + } + ] + } } if (session.accessible_paths.length > 1) { @@ -346,7 +431,7 @@ class ClaudeCodeService implements AgentServiceInterface { const jsonOutput: SDKMessage[] = [] let hasCompleted = false const startTime = Date.now() - const streamState = new ClaudeStreamState() + const streamState = new ClaudeStreamState({ agentSessionId: sessionId }) try { for await (const message of query({ prompt: promptStream, options })) { @@ -410,23 +495,6 @@ class ClaudeCodeService implements AgentServiceInterface { } } - if (message.type === 'assistant' || message.type === 'user') { - logger.silly('claude response', { - message, - content: JSON.stringify(message.message.content) - }) - } else if (message.type === 'stream_event') { - // logger.silly('Claude stream event', { - // message, - // event: JSON.stringify(message.event) - // }) - } else { - logger.silly('Claude response', { - message, - event: JSON.stringify(message) - }) - } - const chunks = transformSDKMessageToStreamParts(message, streamState) for (const chunk of chunks) { stream.emit('data', { diff --git a/src/main/services/agents/services/claudecode/tool-permissions.ts b/src/main/services/agents/services/claudecode/tool-permissions.ts index c95f4c679e..bbca3bd40e 100644 --- a/src/main/services/agents/services/claudecode/tool-permissions.ts +++ b/src/main/services/agents/services/claudecode/tool-permissions.ts @@ -31,12 +31,14 @@ type PendingPermissionRequest = { abortListener?: () => void originalInput: Record toolName: string + toolCallId?: string } type RendererPermissionRequestPayload = { requestId: string toolName: string toolId: string + toolCallId: string description?: string requiresPermissions: boolean input: Record @@ -44,6 +46,7 @@ type RendererPermissionRequestPayload = { createdAt: number expiresAt: number suggestions: PermissionUpdate[] + autoApprove?: boolean } type RendererPermissionResultPayload = { @@ -51,6 +54,7 @@ type RendererPermissionResultPayload = { behavior: ToolPermissionBehavior message?: string reason: 'response' | 'timeout' | 'aborted' | 'no-window' + toolCallId?: string } const pendingRequests = new Map() @@ -144,7 +148,8 @@ const finalizeRequest = ( requestId, behavior: update.behavior, message: update.behavior === 'deny' ? update.message : undefined, - reason + reason, + toolCallId: pending.toolCallId } const dispatched = broadcastToRenderer(IpcChannel.AgentToolPermission_Result, resultPayload) @@ -206,10 +211,20 @@ const ensureIpcHandlersRegistered = () => { }) } +type PromptForToolApprovalOptions = { + signal: AbortSignal + suggestions?: PermissionUpdate[] + autoApprove?: boolean + + // NOTICE: This ID is namespaced with session ID, not the raw SDK tool call ID. + // Format: `${sessionId}:${rawToolCallId}`, e.g., `session_123:WebFetch_0` + toolCallId: string +} + export async function promptForToolApproval( toolName: string, input: Record, - options?: { signal: AbortSignal; suggestions?: PermissionUpdate[] } + options: PromptForToolApprovalOptions ): Promise { if (shouldAutoApproveTools) { logger.debug('promptForToolApproval auto-approving tool for test', { @@ -245,6 +260,7 @@ export async function promptForToolApproval( logger.info('Requesting user approval for tool usage', { requestId, toolName, + toolCallId: options.toolCallId, description: toolMetadata?.description }) @@ -252,13 +268,15 @@ export async function promptForToolApproval( requestId, toolName, toolId: toolMetadata?.id ?? toolName, + toolCallId: options.toolCallId, description: toolMetadata?.description, requiresPermissions: toolMetadata?.requirePermissions ?? false, input: sanitizedInput, inputPreview, createdAt, expiresAt, - suggestions: sanitizedSuggestions + suggestions: sanitizedSuggestions, + autoApprove: options.autoApprove } const defaultDenyUpdate: PermissionResult = { behavior: 'deny', message: 'Tool request aborted before user decision' } @@ -266,6 +284,7 @@ export async function promptForToolApproval( logger.debug('Registering tool permission request', { requestId, toolName, + toolCallId: options.toolCallId, requiresPermissions: requestPayload.requiresPermissions, timeoutMs: TOOL_APPROVAL_TIMEOUT_MS, suggestionCount: sanitizedSuggestions.length @@ -273,7 +292,11 @@ export async function promptForToolApproval( return new Promise((resolve) => { const timeout = setTimeout(() => { - logger.info('User tool permission request timed out', { requestId, toolName }) + logger.info('User tool permission request timed out', { + requestId, + toolName, + toolCallId: options.toolCallId + }) finalizeRequest(requestId, { behavior: 'deny', message: 'Timed out waiting for approval' }, 'timeout') }, TOOL_APPROVAL_TIMEOUT_MS) @@ -282,12 +305,17 @@ export async function promptForToolApproval( timeout, originalInput: sanitizedInput, toolName, - signal: options?.signal + signal: options?.signal, + toolCallId: options.toolCallId } if (options?.signal) { const abortListener = () => { - logger.info('Tool permission request aborted before user responded', { requestId, toolName }) + logger.info('Tool permission request aborted before user responded', { + requestId, + toolName, + toolCallId: options.toolCallId + }) finalizeRequest(requestId, defaultDenyUpdate, 'aborted') } diff --git a/src/main/services/agents/services/claudecode/transform.ts b/src/main/services/agents/services/claudecode/transform.ts index 41285175b4..00be683ba8 100644 --- a/src/main/services/agents/services/claudecode/transform.ts +++ b/src/main/services/agents/services/claudecode/transform.ts @@ -110,7 +110,7 @@ const sdkMessageToProviderMetadata = (message: SDKMessage): ProviderMetadata => * blocks across calls so that incremental deltas can be correlated correctly. */ export function transformSDKMessageToStreamParts(sdkMessage: SDKMessage, state: ClaudeStreamState): AgentStreamPart[] { - logger.silly('Transforming SDKMessage', { message: sdkMessage }) + logger.silly('Transforming SDKMessage', { message: JSON.stringify(sdkMessage) }) switch (sdkMessage.type) { case 'assistant': return handleAssistantMessage(sdkMessage, state) @@ -186,14 +186,13 @@ function handleAssistantMessage( for (const block of content) { switch (block.type) { - case 'text': - if (!isStreamingActive) { - const sanitizedText = stripLocalCommandTags(block.text) - if (sanitizedText) { - textBlocks.push(sanitizedText) - } + case 'text': { + const sanitizedText = stripLocalCommandTags(block.text) + if (sanitizedText) { + textBlocks.push(sanitizedText) } break + } case 'tool_use': handleAssistantToolUse(block as ToolUseContent, providerMetadata, state, chunks) break @@ -203,7 +202,16 @@ function handleAssistantMessage( } } - if (!isStreamingActive && textBlocks.length > 0) { + if (textBlocks.length === 0) { + return chunks + } + + const combinedText = textBlocks.join('') + if (!combinedText) { + return chunks + } + + if (!isStreamingActive) { const id = message.uuid?.toString() || generateMessageId() state.beginStep() chunks.push({ @@ -219,7 +227,7 @@ function handleAssistantMessage( chunks.push({ type: 'text-delta', id, - text: textBlocks.join(''), + text: combinedText, providerMetadata }) chunks.push({ @@ -230,7 +238,27 @@ function handleAssistantMessage( return finalizeNonStreamingStep(message, state, chunks) } - return chunks + const existingTextBlock = state.getFirstOpenTextBlock() + const fallbackId = existingTextBlock?.id || message.uuid?.toString() || generateMessageId() + if (!existingTextBlock) { + chunks.push({ + type: 'text-start', + id: fallbackId, + providerMetadata + }) + } + chunks.push({ + type: 'text-delta', + id: fallbackId, + text: combinedText, + providerMetadata + }) + chunks.push({ + type: 'text-end', + id: fallbackId, + providerMetadata + }) + return finalizeNonStreamingStep(message, state, chunks) } /** @@ -243,15 +271,16 @@ function handleAssistantToolUse( state: ClaudeStreamState, chunks: AgentStreamPart[] ): void { + const toolCallId = state.getNamespacedToolCallId(block.id) chunks.push({ type: 'tool-call', - toolCallId: block.id, + toolCallId, toolName: block.name, input: block.input, providerExecuted: true, providerMetadata }) - state.completeToolBlock(block.id, block.input, providerMetadata) + state.completeToolBlock(block.id, block.name, block.input, providerMetadata) } /** @@ -331,10 +360,11 @@ function handleUserMessage( if (block.type === 'tool_result') { const toolResult = block as ToolResultContent const pendingCall = state.consumePendingToolCall(toolResult.tool_use_id) + const toolCallId = pendingCall?.toolCallId ?? state.getNamespacedToolCallId(toolResult.tool_use_id) if (toolResult.is_error) { chunks.push({ type: 'tool-error', - toolCallId: toolResult.tool_use_id, + toolCallId, toolName: pendingCall?.toolName ?? 'unknown', input: pendingCall?.input, error: toolResult.content, @@ -343,7 +373,7 @@ function handleUserMessage( } else { chunks.push({ type: 'tool-result', - toolCallId: toolResult.tool_use_id, + toolCallId, toolName: pendingCall?.toolName ?? 'unknown', input: pendingCall?.input, output: toolResult.content, @@ -457,6 +487,9 @@ function handleStreamEvent( } case 'message_stop': { + if (!state.hasActiveStep()) { + break + } const pending = state.getPendingUsage() chunks.push({ type: 'finish-step', @@ -514,7 +547,7 @@ function handleContentBlockStart( } case 'tool_use': { const block = state.openToolBlock(index, { - toolCallId: contentBlock.id, + rawToolCallId: contentBlock.id, toolName: contentBlock.name, providerMetadata }) diff --git a/src/main/services/mcp/ServerLogBuffer.ts b/src/main/services/mcp/ServerLogBuffer.ts new file mode 100644 index 0000000000..01c45f373f --- /dev/null +++ b/src/main/services/mcp/ServerLogBuffer.ts @@ -0,0 +1,36 @@ +export type MCPServerLogEntry = { + timestamp: number + level: 'debug' | 'info' | 'warn' | 'error' | 'stderr' | 'stdout' + message: string + data?: any + source?: string +} + +/** + * Lightweight ring buffer for per-server MCP logs. + */ +export class ServerLogBuffer { + private maxEntries: number + private logs: Map = new Map() + + constructor(maxEntries = 200) { + this.maxEntries = maxEntries + } + + append(serverKey: string, entry: MCPServerLogEntry) { + const list = this.logs.get(serverKey) ?? [] + list.push(entry) + if (list.length > this.maxEntries) { + list.splice(0, list.length - this.maxEntries) + } + this.logs.set(serverKey, list) + } + + get(serverKey: string): MCPServerLogEntry[] { + return [...(this.logs.get(serverKey) ?? [])] + } + + remove(serverKey: string) { + this.logs.delete(serverKey) + } +} diff --git a/src/main/utils/__tests__/mcp.test.ts b/src/main/utils/__tests__/mcp.test.ts new file mode 100644 index 0000000000..b1a35f925e --- /dev/null +++ b/src/main/utils/__tests__/mcp.test.ts @@ -0,0 +1,196 @@ +import { describe, expect, it } from 'vitest' + +import { buildFunctionCallToolName } from '../mcp' + +describe('buildFunctionCallToolName', () => { + describe('basic functionality', () => { + it('should combine server name and tool name', () => { + const result = buildFunctionCallToolName('github', 'search_issues') + expect(result).toContain('github') + expect(result).toContain('search') + }) + + it('should sanitize names by replacing dashes with underscores', () => { + const result = buildFunctionCallToolName('my-server', 'my-tool') + // Input dashes are replaced, but the separator between server and tool is a dash + expect(result).toBe('my_serv-my_tool') + expect(result).toContain('_') + }) + + it('should handle empty server names gracefully', () => { + const result = buildFunctionCallToolName('', 'tool') + expect(result).toBeTruthy() + }) + }) + + describe('uniqueness with serverId', () => { + it('should generate different IDs for same server name but different serverIds', () => { + const serverId1 = 'server-id-123456' + const serverId2 = 'server-id-789012' + const serverName = 'github' + const toolName = 'search_repos' + + const result1 = buildFunctionCallToolName(serverName, toolName, serverId1) + const result2 = buildFunctionCallToolName(serverName, toolName, serverId2) + + expect(result1).not.toBe(result2) + expect(result1).toContain('123456') + expect(result2).toContain('789012') + }) + + it('should generate same ID when serverId is not provided', () => { + const serverName = 'github' + const toolName = 'search_repos' + + const result1 = buildFunctionCallToolName(serverName, toolName) + const result2 = buildFunctionCallToolName(serverName, toolName) + + expect(result1).toBe(result2) + }) + + it('should include serverId suffix when provided', () => { + const serverId = 'abc123def456' + const result = buildFunctionCallToolName('server', 'tool', serverId) + + // Should include last 6 chars of serverId + expect(result).toContain('ef456') + }) + }) + + describe('character sanitization', () => { + it('should replace invalid characters with underscores', () => { + const result = buildFunctionCallToolName('test@server', 'tool#name') + expect(result).not.toMatch(/[@#]/) + expect(result).toMatch(/^[a-zA-Z0-9_-]+$/) + }) + + it('should ensure name starts with a letter', () => { + const result = buildFunctionCallToolName('123server', '456tool') + expect(result).toMatch(/^[a-zA-Z]/) + }) + + it('should handle consecutive underscores/dashes', () => { + const result = buildFunctionCallToolName('my--server', 'my__tool') + expect(result).not.toMatch(/[_-]{2,}/) + }) + }) + + describe('length constraints', () => { + it('should truncate names longer than 63 characters', () => { + const longServerName = 'a'.repeat(50) + const longToolName = 'b'.repeat(50) + const result = buildFunctionCallToolName(longServerName, longToolName, 'id123456') + + expect(result.length).toBeLessThanOrEqual(63) + }) + + it('should not end with underscore or dash after truncation', () => { + const longServerName = 'a'.repeat(50) + const longToolName = 'b'.repeat(50) + const result = buildFunctionCallToolName(longServerName, longToolName, 'id123456') + + expect(result).not.toMatch(/[_-]$/) + }) + + it('should preserve serverId suffix even with long server/tool names', () => { + const longServerName = 'a'.repeat(50) + const longToolName = 'b'.repeat(50) + const serverId = 'server-id-xyz789' + + const result = buildFunctionCallToolName(longServerName, longToolName, serverId) + + // The suffix should be preserved and not truncated + expect(result).toContain('xyz789') + expect(result.length).toBeLessThanOrEqual(63) + }) + + it('should ensure two long-named servers with different IDs produce different results', () => { + const longServerName = 'a'.repeat(50) + const longToolName = 'b'.repeat(50) + const serverId1 = 'server-id-abc123' + const serverId2 = 'server-id-def456' + + const result1 = buildFunctionCallToolName(longServerName, longToolName, serverId1) + const result2 = buildFunctionCallToolName(longServerName, longToolName, serverId2) + + // Both should be within limit + expect(result1.length).toBeLessThanOrEqual(63) + expect(result2.length).toBeLessThanOrEqual(63) + + // They should be different due to preserved suffix + expect(result1).not.toBe(result2) + }) + }) + + describe('edge cases with serverId', () => { + it('should handle serverId with only non-alphanumeric characters', () => { + const serverId = '------' // All dashes + const result = buildFunctionCallToolName('server', 'tool', serverId) + + // Should still produce a valid unique suffix via fallback hash + expect(result).toBeTruthy() + expect(result.length).toBeLessThanOrEqual(63) + expect(result).toMatch(/^[a-zA-Z][a-zA-Z0-9_-]*$/) + // Should have a suffix (underscore followed by something) + expect(result).toMatch(/_[a-z0-9]+$/) + }) + + it('should produce different results for different non-alphanumeric serverIds', () => { + const serverId1 = '------' + const serverId2 = '!!!!!!' + + const result1 = buildFunctionCallToolName('server', 'tool', serverId1) + const result2 = buildFunctionCallToolName('server', 'tool', serverId2) + + // Should be different because the hash fallback produces different values + expect(result1).not.toBe(result2) + }) + + it('should handle empty string serverId differently from undefined', () => { + const resultWithEmpty = buildFunctionCallToolName('server', 'tool', '') + const resultWithUndefined = buildFunctionCallToolName('server', 'tool', undefined) + + // Empty string is falsy, so both should behave the same (no suffix) + expect(resultWithEmpty).toBe(resultWithUndefined) + }) + + it('should handle serverId with mixed alphanumeric and special chars', () => { + const serverId = 'ab@#cd' // Mixed chars, last 6 chars contain some alphanumeric + const result = buildFunctionCallToolName('server', 'tool', serverId) + + // Should extract alphanumeric chars: 'abcd' from 'ab@#cd' + expect(result).toContain('abcd') + }) + }) + + describe('real-world scenarios', () => { + it('should handle GitHub MCP server instances correctly', () => { + const serverName = 'github' + const toolName = 'search_repositories' + + const githubComId = 'server-github-com-abc123' + const gheId = 'server-ghe-internal-xyz789' + + const tool1 = buildFunctionCallToolName(serverName, toolName, githubComId) + const tool2 = buildFunctionCallToolName(serverName, toolName, gheId) + + // Should be different + expect(tool1).not.toBe(tool2) + + // Both should be valid identifiers + expect(tool1).toMatch(/^[a-zA-Z][a-zA-Z0-9_-]*$/) + expect(tool2).toMatch(/^[a-zA-Z][a-zA-Z0-9_-]*$/) + + // Both should be <= 63 chars + expect(tool1.length).toBeLessThanOrEqual(63) + expect(tool2.length).toBeLessThanOrEqual(63) + }) + + it('should handle tool names that already include server name prefix', () => { + const result = buildFunctionCallToolName('github', 'github_search_repos') + expect(result).toBeTruthy() + // Should not double the server name + expect(result.split('github').length - 1).toBeLessThanOrEqual(2) + }) + }) +}) diff --git a/src/main/utils/__tests__/process.test.ts b/src/main/utils/__tests__/process.test.ts new file mode 100644 index 0000000000..0485ec5fad --- /dev/null +++ b/src/main/utils/__tests__/process.test.ts @@ -0,0 +1,698 @@ +import { execFileSync } from 'child_process' +import fs from 'fs' +import path from 'path' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { findExecutable, findGitBash, validateGitBashPath } from '../process' + +// Mock dependencies +vi.mock('child_process') +vi.mock('fs') +vi.mock('path') + +// These tests only run on Windows since the functions have platform guards +describe.skipIf(process.platform !== 'win32')('process utilities', () => { + beforeEach(() => { + vi.clearAllMocks() + + // Mock path.join to concatenate paths with backslashes (Windows-style) + vi.mocked(path.join).mockImplementation((...args) => args.join('\\')) + + // Mock path.resolve to handle path resolution with .. support + vi.mocked(path.resolve).mockImplementation((...args) => { + let result = args.join('\\') + + // Handle .. navigation + while (result.includes('\\..')) { + result = result.replace(/\\[^\\]+\\\.\./g, '') + } + + // Ensure absolute path + if (!result.match(/^[A-Z]:/)) { + result = `C:\\cwd\\${result}` + } + + return result + }) + + // Mock path.dirname + vi.mocked(path.dirname).mockImplementation((p) => { + const parts = p.split('\\') + parts.pop() + return parts.join('\\') + }) + + // Mock path.sep + Object.defineProperty(path, 'sep', { value: '\\', writable: true }) + + // Mock process.cwd() + vi.spyOn(process, 'cwd').mockReturnValue('C:\\cwd') + }) + + describe('findExecutable', () => { + describe('git common paths', () => { + it('should find git at Program Files path', () => { + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + process.env.ProgramFiles = 'C:\\Program Files' + + vi.mocked(fs.existsSync).mockImplementation((p) => p === gitPath) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + expect(fs.existsSync).toHaveBeenCalledWith(gitPath) + }) + + it('should find git at Program Files (x86) path', () => { + const gitPath = 'C:\\Program Files (x86)\\Git\\cmd\\git.exe' + process.env['ProgramFiles(x86)'] = 'C:\\Program Files (x86)' + + vi.mocked(fs.existsSync).mockImplementation((p) => p === gitPath) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + expect(fs.existsSync).toHaveBeenCalledWith(gitPath) + }) + + it('should use fallback paths when environment variables are not set', () => { + delete process.env.ProgramFiles + delete process.env['ProgramFiles(x86)'] + + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + vi.mocked(fs.existsSync).mockImplementation((p) => p === gitPath) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + }) + }) + + describe('where.exe PATH lookup', () => { + beforeEach(() => { + Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) + // Common paths don't exist + vi.mocked(fs.existsSync).mockReturnValue(false) + }) + + it('should find executable via where.exe', () => { + const gitPath = 'C:\\Git\\bin\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + expect(execFileSync).toHaveBeenCalledWith('where.exe', ['git.exe'], { + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'pipe'] + }) + }) + + it('should add .exe extension when calling where.exe', () => { + vi.mocked(execFileSync).mockImplementation(() => { + throw new Error('Not found') + }) + + findExecutable('node') + + expect(execFileSync).toHaveBeenCalledWith('where.exe', ['node.exe'], expect.any(Object)) + }) + + it('should handle Windows line endings (CRLF)', () => { + const gitPath1 = 'C:\\Git\\bin\\git.exe' + const gitPath2 = 'C:\\Program Files\\Git\\cmd\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`${gitPath1}\r\n${gitPath2}\r\n`) + + const result = findExecutable('git') + + // Should return the first valid path + expect(result).toBe(gitPath1) + }) + + it('should handle Unix line endings (LF)', () => { + const gitPath1 = 'C:\\Git\\bin\\git.exe' + const gitPath2 = 'C:\\Program Files\\Git\\cmd\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`${gitPath1}\n${gitPath2}\n`) + + const result = findExecutable('git') + + expect(result).toBe(gitPath1) + }) + + it('should handle mixed line endings', () => { + const gitPath1 = 'C:\\Git\\bin\\git.exe' + const gitPath2 = 'C:\\Program Files\\Git\\cmd\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`${gitPath1}\r\n${gitPath2}\n`) + + const result = findExecutable('git') + + expect(result).toBe(gitPath1) + }) + + it('should trim whitespace from paths', () => { + const gitPath = 'C:\\Git\\bin\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(` ${gitPath} \n`) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + }) + + it('should filter empty lines', () => { + const gitPath = 'C:\\Git\\bin\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`\n\n${gitPath}\n\n`) + + const result = findExecutable('git') + + expect(result).toBe(gitPath) + }) + }) + + describe('security checks', () => { + beforeEach(() => { + Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) + vi.mocked(fs.existsSync).mockReturnValue(false) + }) + + it('should skip executables in current directory', () => { + const maliciousPath = 'C:\\cwd\\git.exe' + const safePath = 'C:\\Git\\bin\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`${maliciousPath}\n${safePath}`) + + vi.mocked(path.resolve).mockImplementation((p) => { + if (p.includes('cwd\\git.exe')) return 'c:\\cwd\\git.exe' + return 'c:\\git\\bin\\git.exe' + }) + + vi.mocked(path.dirname).mockImplementation((p) => { + if (p.includes('cwd\\git.exe')) return 'c:\\cwd' + return 'c:\\git\\bin' + }) + + const result = findExecutable('git') + + // Should skip malicious path and return safe path + expect(result).toBe(safePath) + }) + + it('should skip executables in current directory subdirectories', () => { + const maliciousPath = 'C:\\cwd\\subdir\\git.exe' + const safePath = 'C:\\Git\\bin\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(`${maliciousPath}\n${safePath}`) + + vi.mocked(path.resolve).mockImplementation((p) => { + if (p.includes('cwd\\subdir')) return 'c:\\cwd\\subdir\\git.exe' + return 'c:\\git\\bin\\git.exe' + }) + + vi.mocked(path.dirname).mockImplementation((p) => { + if (p.includes('cwd\\subdir')) return 'c:\\cwd\\subdir' + return 'c:\\git\\bin' + }) + + const result = findExecutable('git') + + expect(result).toBe(safePath) + }) + + it('should return null when only malicious executables are found', () => { + const maliciousPath = 'C:\\cwd\\git.exe' + + vi.mocked(execFileSync).mockReturnValue(maliciousPath) + + vi.mocked(path.resolve).mockReturnValue('c:\\cwd\\git.exe') + vi.mocked(path.dirname).mockReturnValue('c:\\cwd') + + const result = findExecutable('git') + + expect(result).toBeNull() + }) + }) + + describe('error handling', () => { + beforeEach(() => { + Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) + vi.mocked(fs.existsSync).mockReturnValue(false) + }) + + it('should return null when where.exe fails', () => { + vi.mocked(execFileSync).mockImplementation(() => { + throw new Error('Command failed') + }) + + const result = findExecutable('nonexistent') + + expect(result).toBeNull() + }) + + it('should return null when where.exe returns empty output', () => { + vi.mocked(execFileSync).mockReturnValue('') + + const result = findExecutable('git') + + expect(result).toBeNull() + }) + + it('should return null when where.exe returns only whitespace', () => { + vi.mocked(execFileSync).mockReturnValue(' \n\n ') + + const result = findExecutable('git') + + expect(result).toBeNull() + }) + }) + + describe('non-git executables', () => { + beforeEach(() => { + Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) + }) + + it('should skip common paths check for non-git executables', () => { + const nodePath = 'C:\\Program Files\\nodejs\\node.exe' + + vi.mocked(execFileSync).mockReturnValue(nodePath) + + const result = findExecutable('node') + + expect(result).toBe(nodePath) + // Should not check common Git paths + expect(fs.existsSync).not.toHaveBeenCalledWith(expect.stringContaining('Git\\cmd\\node.exe')) + }) + }) + }) + + describe('validateGitBashPath', () => { + it('returns null when path is null', () => { + const result = validateGitBashPath(null) + + expect(result).toBeNull() + }) + + it('returns null when path is undefined', () => { + const result = validateGitBashPath(undefined) + + expect(result).toBeNull() + }) + + it('returns normalized path when valid bash.exe exists', () => { + const customPath = 'C:\\PortableGit\\bin\\bash.exe' + vi.mocked(fs.existsSync).mockImplementation((p) => p === 'C:\\PortableGit\\bin\\bash.exe') + + const result = validateGitBashPath(customPath) + + expect(result).toBe('C:\\PortableGit\\bin\\bash.exe') + }) + + it('returns null when file does not exist', () => { + vi.mocked(fs.existsSync).mockReturnValue(false) + + const result = validateGitBashPath('C:\\missing\\bash.exe') + + expect(result).toBeNull() + }) + + it('returns null when path is not bash.exe', () => { + const customPath = 'C:\\PortableGit\\bin\\git.exe' + vi.mocked(fs.existsSync).mockReturnValue(true) + + const result = validateGitBashPath(customPath) + + expect(result).toBeNull() + }) + }) + + describe('findGitBash', () => { + describe('customPath parameter', () => { + beforeEach(() => { + delete process.env.CLAUDE_CODE_GIT_BASH_PATH + }) + + it('uses customPath when valid', () => { + const customPath = 'C:\\CustomGit\\bin\\bash.exe' + vi.mocked(fs.existsSync).mockImplementation((p) => p === customPath) + + const result = findGitBash(customPath) + + expect(result).toBe(customPath) + expect(execFileSync).not.toHaveBeenCalled() + }) + + it('falls back when customPath is invalid', () => { + const customPath = 'C:\\Invalid\\bash.exe' + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + if (p === customPath) return false + if (p === gitPath) return true + if (p === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash(customPath) + + expect(result).toBe(bashPath) + }) + + it('prioritizes customPath over env override', () => { + const customPath = 'C:\\CustomGit\\bin\\bash.exe' + const envPath = 'C:\\EnvGit\\bin\\bash.exe' + process.env.CLAUDE_CODE_GIT_BASH_PATH = envPath + + vi.mocked(fs.existsSync).mockImplementation((p) => p === customPath || p === envPath) + + const result = findGitBash(customPath) + + expect(result).toBe(customPath) + }) + }) + + describe('env override', () => { + beforeEach(() => { + delete process.env.CLAUDE_CODE_GIT_BASH_PATH + }) + + it('uses CLAUDE_CODE_GIT_BASH_PATH when valid', () => { + const envPath = 'C:\\OverrideGit\\bin\\bash.exe' + process.env.CLAUDE_CODE_GIT_BASH_PATH = envPath + + vi.mocked(fs.existsSync).mockImplementation((p) => p === envPath) + + const result = findGitBash() + + expect(result).toBe(envPath) + expect(execFileSync).not.toHaveBeenCalled() + }) + + it('falls back when CLAUDE_CODE_GIT_BASH_PATH is invalid', () => { + const envPath = 'C:\\Invalid\\bash.exe' + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + + process.env.CLAUDE_CODE_GIT_BASH_PATH = envPath + + vi.mocked(fs.existsSync).mockImplementation((p) => { + if (p === envPath) return false + if (p === gitPath) return true + if (p === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + }) + + describe('git.exe path derivation', () => { + it('should derive bash.exe from standard Git installation (Git/cmd/git.exe)', () => { + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + + // findExecutable will find git at common path + process.env.ProgramFiles = 'C:\\Program Files' + vi.mocked(fs.existsSync).mockImplementation((p) => { + return p === gitPath || p === bashPath + }) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should derive bash.exe from portable Git installation (Git/bin/git.exe)', () => { + const gitPath = 'C:\\PortableGit\\bin\\git.exe' + const bashPath = 'C:\\PortableGit\\bin\\bash.exe' + + // Mock: common git paths don't exist, but where.exe finds portable git + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common git paths don't exist + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // Portable bash.exe exists at Git/bin/bash.exe (second path in possibleBashPaths) + if (pathStr === bashPath) return true + return false + }) + + // where.exe returns portable git path + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should derive bash.exe from MSYS2 Git installation (Git/usr/bin/bash.exe)', () => { + const gitPath = 'C:\\msys64\\usr\\bin\\git.exe' + const bashPath = 'C:\\msys64\\usr\\bin\\bash.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common git paths don't exist + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // MSYS2 bash.exe exists at usr/bin/bash.exe (third path in possibleBashPaths) + if (pathStr === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should try multiple bash.exe locations in order', () => { + const gitPath = 'C:\\Git\\cmd\\git.exe' + const bashPath = 'C:\\Git\\bin\\bash.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common git paths don't exist + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // Standard path exists (first in possibleBashPaths) + if (pathStr === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should handle when git.exe is found but bash.exe is not at any derived location', () => { + const gitPath = 'C:\\Git\\cmd\\git.exe' + + // git.exe exists via where.exe, but bash.exe doesn't exist at any derived location + vi.mocked(fs.existsSync).mockImplementation(() => { + // Only return false for all bash.exe checks + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + // Should fall back to common paths check + expect(result).toBeNull() + }) + }) + + describe('common paths fallback', () => { + beforeEach(() => { + // git.exe not found + vi.mocked(execFileSync).mockImplementation(() => { + throw new Error('Not found') + }) + }) + + it('should check Program Files path', () => { + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + process.env.ProgramFiles = 'C:\\Program Files' + + vi.mocked(fs.existsSync).mockImplementation((p) => p === bashPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should check Program Files (x86) path', () => { + const bashPath = 'C:\\Program Files (x86)\\Git\\bin\\bash.exe' + process.env['ProgramFiles(x86)'] = 'C:\\Program Files (x86)' + + vi.mocked(fs.existsSync).mockImplementation((p) => p === bashPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should check LOCALAPPDATA path', () => { + const bashPath = 'C:\\Users\\User\\AppData\\Local\\Programs\\Git\\bin\\bash.exe' + process.env.LOCALAPPDATA = 'C:\\Users\\User\\AppData\\Local' + + vi.mocked(fs.existsSync).mockImplementation((p) => p === bashPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should skip LOCALAPPDATA check when environment variable is not set', () => { + delete process.env.LOCALAPPDATA + + vi.mocked(fs.existsSync).mockReturnValue(false) + + const result = findGitBash() + + expect(result).toBeNull() + // Should not check invalid path with empty LOCALAPPDATA + expect(fs.existsSync).not.toHaveBeenCalledWith(expect.stringContaining('undefined')) + }) + + it('should use fallback values when environment variables are not set', () => { + delete process.env.ProgramFiles + delete process.env['ProgramFiles(x86)'] + + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + vi.mocked(fs.existsSync).mockImplementation((p) => p === bashPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + }) + + describe('priority order', () => { + it('should prioritize git.exe derivation over common paths', () => { + const gitPath = 'C:\\CustomPath\\Git\\cmd\\git.exe' + const derivedBashPath = 'C:\\CustomPath\\Git\\bin\\bash.exe' + const commonBashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + + // Both exist + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common git paths don't exist (so findExecutable uses where.exe) + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // Both bash paths exist, but derived should be checked first + if (pathStr === derivedBashPath) return true + if (pathStr === commonBashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + // Should return derived path, not common path + expect(result).toBe(derivedBashPath) + }) + }) + + describe('error scenarios', () => { + it('should return null when Git is not installed anywhere', () => { + vi.mocked(fs.existsSync).mockReturnValue(false) + vi.mocked(execFileSync).mockImplementation(() => { + throw new Error('Not found') + }) + + const result = findGitBash() + + expect(result).toBeNull() + }) + + it('should return null when git.exe exists but bash.exe does not', () => { + const gitPath = 'C:\\Git\\cmd\\git.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + // git.exe exists, but no bash.exe anywhere + return p === gitPath + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBeNull() + }) + }) + + describe('real-world scenarios', () => { + it('should handle official Git for Windows installer', () => { + const gitPath = 'C:\\Program Files\\Git\\cmd\\git.exe' + const bashPath = 'C:\\Program Files\\Git\\bin\\bash.exe' + + process.env.ProgramFiles = 'C:\\Program Files' + vi.mocked(fs.existsSync).mockImplementation((p) => { + return p === gitPath || p === bashPath + }) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should handle portable Git installation in custom directory', () => { + const gitPath = 'D:\\DevTools\\PortableGit\\bin\\git.exe' + const bashPath = 'D:\\DevTools\\PortableGit\\bin\\bash.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common paths don't exist + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // Portable Git paths exist (portable uses second path: Git/bin/bash.exe) + if (pathStr === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + + it('should handle Git installed via Scoop', () => { + // Scoop typically installs to %USERPROFILE%\scoop\apps\git\current + const gitPath = 'C:\\Users\\User\\scoop\\apps\\git\\current\\cmd\\git.exe' + const bashPath = 'C:\\Users\\User\\scoop\\apps\\git\\current\\bin\\bash.exe' + + vi.mocked(fs.existsSync).mockImplementation((p) => { + const pathStr = p?.toString() || '' + // Common paths don't exist + if (pathStr.includes('Program Files\\Git\\cmd\\git.exe')) return false + if (pathStr.includes('Program Files (x86)\\Git\\cmd\\git.exe')) return false + // Scoop bash path exists (standard structure: cmd -> bin) + if (pathStr === bashPath) return true + return false + }) + + vi.mocked(execFileSync).mockReturnValue(gitPath) + + const result = findGitBash() + + expect(result).toBe(bashPath) + }) + }) + }) +}) diff --git a/src/main/utils/mcp.ts b/src/main/utils/mcp.ts index 23d19806d9..cfa700f2e6 100644 --- a/src/main/utils/mcp.ts +++ b/src/main/utils/mcp.ts @@ -1,7 +1,25 @@ -export function buildFunctionCallToolName(serverName: string, toolName: string) { +export function buildFunctionCallToolName(serverName: string, toolName: string, serverId?: string) { const sanitizedServer = serverName.trim().replace(/-/g, '_') const sanitizedTool = toolName.trim().replace(/-/g, '_') + // Calculate suffix first to reserve space for it + // Suffix format: "_" + 6 alphanumeric chars = 7 chars total + let serverIdSuffix = '' + if (serverId) { + // Take the last 6 characters of the serverId for brevity + serverIdSuffix = serverId.slice(-6).replace(/[^a-zA-Z0-9]/g, '') + + // Fallback: if suffix becomes empty (all non-alphanumeric chars), use a simple hash + if (!serverIdSuffix) { + const hash = serverId.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0) + serverIdSuffix = hash.toString(36).slice(-6) || 'x' + } + } + + // Reserve space for suffix when calculating max base name length + const SUFFIX_LENGTH = serverIdSuffix ? serverIdSuffix.length + 1 : 0 // +1 for underscore + const MAX_BASE_LENGTH = 63 - SUFFIX_LENGTH + // Combine server name and tool name let name = sanitizedTool if (!sanitizedTool.includes(sanitizedServer.slice(0, 7))) { @@ -20,9 +38,9 @@ export function buildFunctionCallToolName(serverName: string, toolName: string) // Remove consecutive underscores/dashes (optional improvement) name = name.replace(/[_-]{2,}/g, '_') - // Truncate to 63 characters maximum - if (name.length > 63) { - name = name.slice(0, 63) + // Truncate base name BEFORE adding suffix to ensure suffix is never cut off + if (name.length > MAX_BASE_LENGTH) { + name = name.slice(0, MAX_BASE_LENGTH) } // Handle edge case: ensure we still have a valid name if truncation left invalid chars at edges @@ -30,5 +48,10 @@ export function buildFunctionCallToolName(serverName: string, toolName: string) name = name.slice(0, -1) } + // Now append the suffix - it will always fit within 63 chars + if (serverIdSuffix) { + name = `${name}_${serverIdSuffix}` + } + return name } diff --git a/src/main/utils/process.ts b/src/main/utils/process.ts index f36e86861d..7175af7e75 100644 --- a/src/main/utils/process.ts +++ b/src/main/utils/process.ts @@ -1,10 +1,11 @@ import { loggerService } from '@logger' import { HOME_CHERRY_DIR } from '@shared/config/constant' -import { spawn } from 'child_process' +import { execFileSync, spawn } from 'child_process' import fs from 'fs' import os from 'os' import path from 'path' +import { isWin } from '../constant' import { getResourcePath } from '.' const logger = loggerService.withContext('Utils:Process') @@ -39,7 +40,7 @@ export function runInstallScript(scriptPath: string): Promise { } export async function getBinaryName(name: string): Promise { - if (process.platform === 'win32') { + if (isWin) { return `${name}.exe` } return name @@ -60,3 +61,167 @@ export async function isBinaryExists(name: string): Promise { const cmd = await getBinaryPath(name) return await fs.existsSync(cmd) } + +/** + * Find executable in common paths or PATH environment variable + * Based on Claude Code's implementation with security checks + * @param name - Name of the executable to find (without .exe extension) + * @returns Full path to the executable or null if not found + */ +export function findExecutable(name: string): string | null { + // This implementation uses where.exe which is Windows-only + if (!isWin) { + return null + } + + // Special handling for git - check common installation paths first + if (name === 'git') { + const commonGitPaths = [ + path.join(process.env.ProgramFiles || 'C:\\Program Files', 'Git', 'cmd', 'git.exe'), + path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'Git', 'cmd', 'git.exe') + ] + + for (const gitPath of commonGitPaths) { + if (fs.existsSync(gitPath)) { + logger.debug(`Found ${name} at common path`, { path: gitPath }) + return gitPath + } + } + } + + // Use where.exe to find executable in PATH + // Use execFileSync to prevent command injection + try { + // Add .exe extension for more precise matching on Windows + const executableName = `${name}.exe` + const result = execFileSync('where.exe', [executableName], { + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'pipe'] + }) + + // Handle both Windows (\r\n) and Unix (\n) line endings + const paths = result.trim().split(/\r?\n/).filter(Boolean) + const currentDir = process.cwd().toLowerCase() + + // Security check: skip executables in current directory + for (const exePath of paths) { + // Trim whitespace from where.exe output + const cleanPath = exePath.trim() + const resolvedPath = path.resolve(cleanPath).toLowerCase() + const execDir = path.dirname(resolvedPath).toLowerCase() + + // Skip if in current directory or subdirectory (potential malware) + if (execDir === currentDir || execDir.startsWith(currentDir + path.sep)) { + logger.warn('Skipping potentially malicious executable in current directory', { + path: cleanPath + }) + continue + } + + logger.debug(`Found ${name} via where.exe`, { path: cleanPath }) + return cleanPath + } + + return null + } catch (error) { + logger.debug(`where.exe ${name} failed`, { error }) + return null + } +} + +/** + * Find Git Bash executable on Windows + * @param customPath - Optional custom path from config + * @returns Full path to bash.exe or null if not found + */ +export function findGitBash(customPath?: string | null): string | null { + // Git Bash is Windows-only + if (!isWin) { + return null + } + + // 1. Check custom path from config first + if (customPath) { + const validated = validateGitBashPath(customPath) + if (validated) { + logger.debug('Using custom Git Bash path from config', { path: validated }) + return validated + } + logger.warn('Custom Git Bash path provided but invalid', { path: customPath }) + } + + // 2. Check environment variable override + const envOverride = process.env.CLAUDE_CODE_GIT_BASH_PATH + if (envOverride) { + const validated = validateGitBashPath(envOverride) + if (validated) { + logger.debug('Using CLAUDE_CODE_GIT_BASH_PATH override for bash.exe', { path: validated }) + return validated + } + logger.warn('CLAUDE_CODE_GIT_BASH_PATH provided but path is invalid', { path: envOverride }) + } + + // 3. Find git.exe and derive bash.exe path + const gitPath = findExecutable('git') + if (gitPath) { + // Try multiple possible locations for bash.exe relative to git.exe + // Different Git installations have different directory structures + const possibleBashPaths = [ + path.join(gitPath, '..', '..', 'bin', 'bash.exe'), // Standard Git: git.exe at Git/cmd/ -> navigate up 2 levels -> then bin/bash.exe + path.join(gitPath, '..', 'bash.exe'), // Portable Git: git.exe at Git/bin/ -> bash.exe in same directory + path.join(gitPath, '..', '..', 'usr', 'bin', 'bash.exe') // MSYS2 Git: git.exe at msys64/usr/bin/ -> navigate up 2 levels -> then usr/bin/bash.exe + ] + + for (const bashPath of possibleBashPaths) { + const resolvedBashPath = path.resolve(bashPath) + if (fs.existsSync(resolvedBashPath)) { + logger.debug('Found bash.exe via git.exe path derivation', { path: resolvedBashPath }) + return resolvedBashPath + } + } + + logger.debug('bash.exe not found at expected locations relative to git.exe', { + gitPath, + checkedPaths: possibleBashPaths.map((p) => path.resolve(p)) + }) + } + + // 4. Fallback: check common Git Bash paths directly + const commonBashPaths = [ + path.join(process.env.ProgramFiles || 'C:\\Program Files', 'Git', 'bin', 'bash.exe'), + path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'Git', 'bin', 'bash.exe'), + ...(process.env.LOCALAPPDATA ? [path.join(process.env.LOCALAPPDATA, 'Programs', 'Git', 'bin', 'bash.exe')] : []) + ] + + for (const bashPath of commonBashPaths) { + if (fs.existsSync(bashPath)) { + logger.debug('Found bash.exe at common path', { path: bashPath }) + return bashPath + } + } + + logger.debug('Git Bash not found - checked git derivation and common paths') + return null +} + +export function validateGitBashPath(customPath?: string | null): string | null { + if (!customPath) { + return null + } + + const resolved = path.resolve(customPath) + + if (!fs.existsSync(resolved)) { + logger.warn('Custom Git Bash path does not exist', { path: resolved }) + return null + } + + const isExe = resolved.toLowerCase().endsWith('bash.exe') + if (!isExe) { + logger.warn('Custom Git Bash path is not bash.exe', { path: resolved }) + return null + } + + logger.debug('Validated custom Git Bash path', { path: resolved }) + return resolved +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 11a8e4589f..117bec3b91 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -5,6 +5,7 @@ import type { SpanContext } from '@opentelemetry/api' import type { TerminalConfig, UpgradeChannel } from '@shared/config/constant' import type { LogLevel, LogSourceWithContext } from '@shared/config/logger' import type { FileChangeEvent, WebviewKeyEvent } from '@shared/config/types' +import type { MCPServerLogEntry } from '@shared/config/types' import { IpcChannel } from '@shared/IpcChannel' import type { Notification } from '@types' import type { @@ -122,7 +123,11 @@ const api = { system: { getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType), getHostname: () => ipcRenderer.invoke(IpcChannel.System_GetHostname), - getCpuName: () => ipcRenderer.invoke(IpcChannel.System_GetCpuName) + getCpuName: () => ipcRenderer.invoke(IpcChannel.System_GetCpuName), + checkGitBash: (): Promise => ipcRenderer.invoke(IpcChannel.System_CheckGitBash), + getGitBashPath: (): Promise => ipcRenderer.invoke(IpcChannel.System_GetGitBashPath), + setGitBashPath: (newPath: string | null): Promise => + ipcRenderer.invoke(IpcChannel.System_SetGitBashPath, newPath) }, devTools: { toggle: () => ipcRenderer.invoke(IpcChannel.System_ToggleDevTools) @@ -220,6 +225,10 @@ const api = { startFileWatcher: (dirPath: string, config?: any) => ipcRenderer.invoke(IpcChannel.File_StartWatcher, dirPath, config), stopFileWatcher: () => ipcRenderer.invoke(IpcChannel.File_StopWatcher), + pauseFileWatcher: () => ipcRenderer.invoke(IpcChannel.File_PauseWatcher), + resumeFileWatcher: () => ipcRenderer.invoke(IpcChannel.File_ResumeWatcher), + batchUploadMarkdown: (filePaths: string[], targetPath: string) => + ipcRenderer.invoke(IpcChannel.File_BatchUploadMarkdown, filePaths, targetPath), onFileChange: (callback: (data: FileChangeEvent) => void) => { const listener = (_event: Electron.IpcRendererEvent, data: any) => { if (data && typeof data === 'object') { @@ -367,7 +376,16 @@ const api = { }, abortTool: (callId: string) => ipcRenderer.invoke(IpcChannel.Mcp_AbortTool, callId), getServerVersion: (server: MCPServer): Promise => - ipcRenderer.invoke(IpcChannel.Mcp_GetServerVersion, server) + ipcRenderer.invoke(IpcChannel.Mcp_GetServerVersion, server), + getServerLogs: (server: MCPServer): Promise => + ipcRenderer.invoke(IpcChannel.Mcp_GetServerLogs, server), + onServerLog: (callback: (log: MCPServerLogEntry & { serverId?: string }) => void) => { + const listener = (_event: Electron.IpcRendererEvent, log: MCPServerLogEntry & { serverId?: string }) => { + callback(log) + } + ipcRenderer.on(IpcChannel.Mcp_ServerLog, listener) + return () => ipcRenderer.off(IpcChannel.Mcp_ServerLog, listener) + } }, python: { execute: (script: string, context?: Record, timeout?: number) => @@ -419,6 +437,8 @@ const api = { ipcRenderer.invoke(IpcChannel.Webview_SetOpenLinkExternal, webviewId, isExternal), setSpellCheckEnabled: (webviewId: number, isEnable: boolean) => ipcRenderer.invoke(IpcChannel.Webview_SetSpellCheckEnabled, webviewId, isEnable), + printToPDF: (webviewId: number) => ipcRenderer.invoke(IpcChannel.Webview_PrintToPDF, webviewId), + saveAsHTML: (webviewId: number) => ipcRenderer.invoke(IpcChannel.Webview_SaveAsHTML, webviewId), onFindShortcut: (callback: (payload: WebviewKeyEvent) => void) => { const listener = (_event: Electron.IpcRendererEvent, payload: WebviewKeyEvent) => { callback(payload) @@ -451,7 +471,10 @@ const api = { ipcRenderer.invoke(IpcChannel.Selection_ProcessAction, actionItem, isFullScreen), closeActionWindow: () => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowClose), minimizeActionWindow: () => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowMinimize), - pinActionWindow: (isPinned: boolean) => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowPin, isPinned) + pinActionWindow: (isPinned: boolean) => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowPin, isPinned), + // [Windows only] Electron bug workaround - can be removed once https://github.com/electron/electron/issues/48554 is fixed + resizeActionWindow: (deltaX: number, deltaY: number, direction: string) => + ipcRenderer.invoke(IpcChannel.Selection_ActionWindowResize, deltaX, deltaY, direction) }, agentTools: { respondToPermission: (payload: { diff --git a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts index 544ec443aa..5de2ac3453 100644 --- a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts +++ b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts @@ -386,14 +386,13 @@ export class AiSdkToChunkAdapter { case 'error': this.onChunk({ type: ChunkType.ERROR, - error: - chunk.error instanceof AISDKError - ? chunk.error - : new ProviderSpecificError({ - message: formatErrorMessage(chunk.error), - provider: 'unknown', - cause: chunk.error - }) + error: AISDKError.isInstance(chunk.error) + ? chunk.error + : new ProviderSpecificError({ + message: formatErrorMessage(chunk.error), + provider: 'unknown', + cause: chunk.error + }) }) break diff --git a/src/renderer/src/aiCore/chunk/handleToolCallChunk.ts b/src/renderer/src/aiCore/chunk/handleToolCallChunk.ts index 32c7e534e3..b5acbb690b 100644 --- a/src/renderer/src/aiCore/chunk/handleToolCallChunk.ts +++ b/src/renderer/src/aiCore/chunk/handleToolCallChunk.ts @@ -212,8 +212,9 @@ export class ToolCallChunkHandler { description: toolName, type: 'builtin' } as BaseTool - } else if ((mcpTool = this.mcpTools.find((t) => t.name === toolName) as MCPTool)) { + } else if ((mcpTool = this.mcpTools.find((t) => t.id === toolName) as MCPTool)) { // 如果是客户端执行的 MCP 工具,沿用现有逻辑 + // toolName is mcpTool.id (registered with id as key in convertMcpToolsToAiSdkTools) logger.info(`[ToolCallChunkHandler] Handling client-side MCP tool: ${toolName}`) // mcpTool = this.mcpTools.find((t) => t.name === toolName) as MCPTool // if (!mcpTool) { diff --git a/src/renderer/src/aiCore/index_new.ts b/src/renderer/src/aiCore/index_new.ts index 434b2322cd..5c84a7254e 100644 --- a/src/renderer/src/aiCore/index_new.ts +++ b/src/renderer/src/aiCore/index_new.ts @@ -7,10 +7,10 @@ * 2. 暂时保持接口兼容性 */ -import type { GatewayLanguageModelEntry } from '@ai-sdk/gateway' import { createExecutor } from '@cherrystudio/ai-core' import { loggerService } from '@logger' import { getEnableDeveloperMode } from '@renderer/hooks/useSettings' +import { normalizeGatewayModels, normalizeSdkModels } from '@renderer/services/models/ModelAdapter' import { addSpan, endSpan } from '@renderer/services/SpanManagerService' import type { StartSpanParams } from '@renderer/trace/types/ModelSpanEntity' import { type Assistant, type GenerateImageParams, type Model, type Provider, SystemProviderIds } from '@renderer/types' @@ -27,11 +27,13 @@ import { buildAiSdkMiddlewares } from './middleware/AiSdkMiddlewareBuilder' import { buildPlugins } from './plugins/PluginBuilder' import { createAiSdkProvider } from './provider/factory' import { + adaptProvider, getActualProvider, isModernSdkSupported, prepareSpecialProviderConfig, providerToAiSdkConfig } from './provider/providerConfig' +import type { AiSdkConfig } from './types' const logger = loggerService.withContext('ModernAiProvider') @@ -44,12 +46,44 @@ export type ModernAiProviderConfig = AiSdkMiddlewareConfig & { export default class ModernAiProvider { private legacyProvider: LegacyAiProvider - private config?: ReturnType + private config?: AiSdkConfig private actualProvider: Provider private model?: Model private localProvider: Awaited | null = null - // 构造函数重载签名 + /** + * Constructor for ModernAiProvider + * + * @param modelOrProvider - Model or Provider object + * @param provider - Optional Provider object (only used when first param is Model) + * + * @remarks + * **Important behavior notes**: + * + * 1. When called with `(model)`: + * - Calls `getActualProvider(model)` to retrieve and format the provider + * - URL will be automatically formatted via `formatProviderApiHost`, adding version suffixes like `/v1` + * + * 2. When called with `(model, provider)`: + * - The provided provider will be adapted via `adaptProvider` + * - URL formatting behavior depends on the adapted result + * + * 3. When called with `(provider)`: + * - The provider will be adapted via `adaptProvider` + * - Used for operations that don't need a model (e.g., fetchModels) + * + * @example + * ```typescript + * // Recommended: Auto-format URL + * const ai = new ModernAiProvider(model) + * + * // Provider will be adapted + * const ai = new ModernAiProvider(model, customProvider) + * + * // For operations that don't need a model + * const ai = new ModernAiProvider(provider) + * ``` + */ constructor(model: Model, provider?: Provider) constructor(provider: Provider) constructor(modelOrProvider: Model | Provider, provider?: Provider) @@ -57,12 +91,14 @@ export default class ModernAiProvider { if (this.isModel(modelOrProvider)) { // 传入的是 Model this.model = modelOrProvider - this.actualProvider = provider || getActualProvider(modelOrProvider) + this.actualProvider = provider + ? adaptProvider({ provider, model: modelOrProvider }) + : getActualProvider(modelOrProvider) // 只保存配置,不预先创建executor this.config = providerToAiSdkConfig(this.actualProvider, modelOrProvider) } else { // 传入的是 Provider - this.actualProvider = modelOrProvider + this.actualProvider = adaptProvider({ provider: modelOrProvider }) // model为可选,某些操作(如fetchModels)不需要model } @@ -86,9 +122,17 @@ export default class ModernAiProvider { throw new Error('Model is required for completions. Please use constructor with model parameter.') } - // 每次请求时重新生成配置以确保API key轮换生效 - this.config = providerToAiSdkConfig(this.actualProvider, this.model) - logger.debug('Generated provider config for completions', this.config) + // Config is now set in constructor, ApiService handles key rotation before passing provider + if (!this.config) { + // If config wasn't set in constructor (when provider only), generate it now + this.config = providerToAiSdkConfig(this.actualProvider, this.model!) + } + logger.debug('Using provider config for completions', this.config) + + // 检查 config 是否存在 + if (!this.config) { + throw new Error('Provider config is undefined; cannot proceed with completions') + } if (SUPPORTED_IMAGE_ENDPOINT_LIST.includes(this.config.options.endpoint)) { providerConfig.isImageGenerationEndpoint = true } @@ -149,7 +193,8 @@ export default class ModernAiProvider { params: StreamTextParams, config: ModernAiProviderConfig ): Promise { - if (config.isImageGenerationEndpoint) { + // ai-gateway不是image/generation 端点,所以就先不走legacy了 + if (config.isImageGenerationEndpoint && this.getActualProvider().id !== SystemProviderIds.gateway) { // 使用 legacy 实现处理图像生成(支持图片编辑等高级功能) if (!config.uiMessages) { throw new Error('uiMessages is required for image generation endpoint') @@ -315,10 +360,10 @@ export default class ModernAiProvider { } } - /** - * 使用现代化 AI SDK 的图像生成实现,支持流式输出 - * @deprecated 已改为使用 legacy 实现以支持图片编辑等高级功能 - */ + // /** + // * 使用现代化 AI SDK 的图像生成实现,支持流式输出 + // * @deprecated 已改为使用 legacy 实现以支持图片编辑等高级功能 + // */ /* private async modernImageGeneration( model: ImageModel, @@ -440,19 +485,12 @@ export default class ModernAiProvider { // 代理其他方法到原有实现 public async models() { - if (this.actualProvider.id === SystemProviderIds['ai-gateway']) { - const formatModel = function (models: GatewayLanguageModelEntry[]): Model[] { - return models.map((m) => ({ - id: m.id, - name: m.name, - provider: 'gateway', - group: m.id.split('/')[0], - description: m.description ?? undefined - })) - } - return formatModel((await gateway.getAvailableModels()).models) + if (this.actualProvider.id === SystemProviderIds.gateway) { + const gatewayModels = (await gateway.getAvailableModels()).models + return normalizeGatewayModels(this.actualProvider, gatewayModels) } - return this.legacyProvider.models() + const sdkModels = await this.legacyProvider.models() + return normalizeSdkModels(this.actualProvider, sdkModels) } public async getEmbeddingDimensions(model: Model): Promise { @@ -463,8 +501,13 @@ export default class ModernAiProvider { // 如果支持新的 AI SDK,使用现代化实现 if (isModernSdkSupported(this.actualProvider)) { try { + // 确保 config 已定义 + if (!this.config) { + throw new Error('Provider config is undefined; cannot proceed with generateImage') + } + // 确保本地provider已创建 - if (!this.localProvider) { + if (!this.localProvider && this.config) { this.localProvider = await createAiSdkProvider(this.config) if (!this.localProvider) { throw new Error('Local provider not created') diff --git a/src/renderer/src/aiCore/legacy/clients/ApiClientFactory.ts b/src/renderer/src/aiCore/legacy/clients/ApiClientFactory.ts index bc416161c4..ee878f5861 100644 --- a/src/renderer/src/aiCore/legacy/clients/ApiClientFactory.ts +++ b/src/renderer/src/aiCore/legacy/clients/ApiClientFactory.ts @@ -1,6 +1,6 @@ import { loggerService } from '@logger' -import { isNewApiProvider } from '@renderer/config/providers' import type { Provider } from '@renderer/types' +import { isNewApiProvider } from '@renderer/utils/provider' import { AihubmixAPIClient } from './aihubmix/AihubmixAPIClient' import { AnthropicAPIClient } from './anthropic/AnthropicAPIClient' diff --git a/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts b/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts index f520162496..5d435b9074 100644 --- a/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts @@ -2,14 +2,15 @@ import { loggerService } from '@logger' import { getModelSupportedVerbosity, isFunctionCallingModel, - isNotSupportTemperatureAndTopP, isOpenAIModel, - isSupportFlexServiceTierModel + isSupportFlexServiceTierModel, + isSupportTemperatureModel, + isSupportTopPModel } from '@renderer/config/models' import { REFERENCE_PROMPT } from '@renderer/config/prompts' -import { isSupportServiceTierProvider } from '@renderer/config/providers' import { getLMStudioKeepAliveTime } from '@renderer/hooks/useLMStudio' import { getAssistantSettings } from '@renderer/services/AssistantService' +import type { RootState } from '@renderer/store' import type { Assistant, GenerateImageParams, @@ -19,7 +20,6 @@ import type { MCPToolResponse, MemoryItem, Model, - OpenAIVerbosity, Provider, ToolCallResponse, WebSearchProviderResponse, @@ -33,6 +33,7 @@ import { OpenAIServiceTiers, SystemProviderIds } from '@renderer/types' +import type { OpenAIVerbosity } from '@renderer/types/aiCoreTypes' import type { Message } from '@renderer/types/newMessage' import type { RequestOptions, @@ -48,6 +49,7 @@ import type { import { isJSON, parseJSON } from '@renderer/utils' import { addAbortController, removeAbortController } from '@renderer/utils/abortController' import { findFileBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find' +import { isSupportServiceTierProvider } from '@renderer/utils/provider' import { defaultTimeout } from '@shared/config/constant' import { defaultAppHeaders } from '@shared/utils' import { isEmpty } from 'lodash' @@ -199,7 +201,7 @@ export abstract class BaseApiClient< } public getTemperature(assistant: Assistant, model: Model): number | undefined { - if (isNotSupportTemperatureAndTopP(model)) { + if (!isSupportTemperatureModel(model)) { return undefined } const assistantSettings = getAssistantSettings(assistant) @@ -207,7 +209,7 @@ export abstract class BaseApiClient< } public getTopP(assistant: Assistant, model: Model): number | undefined { - if (isNotSupportTemperatureAndTopP(model)) { + if (!isSupportTopPModel(model)) { return undefined } const assistantSettings = getAssistantSettings(assistant) @@ -245,23 +247,20 @@ export abstract class BaseApiClient< protected getVerbosity(model?: Model): OpenAIVerbosity { try { - const state = window.store?.getState() + const state = window.store?.getState() as RootState const verbosity = state?.settings?.openAI?.verbosity - if (verbosity && ['low', 'medium', 'high'].includes(verbosity)) { - // If model is provided, check if the verbosity is supported by the model - if (model) { - const supportedVerbosity = getModelSupportedVerbosity(model) - // Use user's verbosity if supported, otherwise use the first supported option - return supportedVerbosity.includes(verbosity) ? verbosity : supportedVerbosity[0] - } - return verbosity + // If model is provided, check if the verbosity is supported by the model + if (model) { + const supportedVerbosity = getModelSupportedVerbosity(model) + // Use user's verbosity if supported, otherwise use the first supported option + return supportedVerbosity.includes(verbosity) ? verbosity : supportedVerbosity[0] } + return verbosity } catch (error) { - logger.warn('Failed to get verbosity from state:', error as Error) + logger.warn('Failed to get verbosity from state. Fallback to undefined.', error as Error) + return undefined } - - return 'medium' } protected getTimeout(model: Model) { @@ -405,6 +404,9 @@ export abstract class BaseApiClient< if (!param.name?.trim()) { return acc } + // Parse JSON type parameters (Legacy API clients) + // Related: src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx:133-148 + // The UI stores JSON type params as strings, this function parses them before sending to API if (param.type === 'json') { const value = param.value as string if (value === 'undefined') { diff --git a/src/renderer/src/aiCore/legacy/clients/__tests__/ApiClientFactory.test.ts b/src/renderer/src/aiCore/legacy/clients/__tests__/ApiClientFactory.test.ts index 03ec1e1ea2..991c436ca3 100644 --- a/src/renderer/src/aiCore/legacy/clients/__tests__/ApiClientFactory.test.ts +++ b/src/renderer/src/aiCore/legacy/clients/__tests__/ApiClientFactory.test.ts @@ -58,10 +58,27 @@ vi.mock('../aws/AwsBedrockAPIClient', () => ({ AwsBedrockAPIClient: vi.fn().mockImplementation(() => ({})) })) +vi.mock('@renderer/services/AssistantService.ts', () => ({ + getDefaultAssistant: () => { + return { + id: 'default', + name: 'default', + emoji: '😀', + prompt: '', + topics: [], + messages: [], + type: 'assistant', + regularPhrases: [], + settings: {} + } + } +})) + // Mock the models config to prevent circular dependency issues vi.mock('@renderer/config/models', () => ({ findTokenLimit: vi.fn(), isReasoningModel: vi.fn(), + isOpenAILLMModel: vi.fn(), SYSTEM_MODELS: { silicon: [], defaultModel: [] diff --git a/src/renderer/src/aiCore/legacy/clients/anthropic/AnthropicAPIClient.ts b/src/renderer/src/aiCore/legacy/clients/anthropic/AnthropicAPIClient.ts index 15f3cf1007..9b63b77ddf 100644 --- a/src/renderer/src/aiCore/legacy/clients/anthropic/AnthropicAPIClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/anthropic/AnthropicAPIClient.ts @@ -124,7 +124,8 @@ export class AnthropicAPIClient extends BaseApiClient< override async listModels(): Promise { const sdk = (await this.getSdkInstance()) as Anthropic - const response = await sdk.models.list() + // prevent auto appended /v1. It's included in baseUrl. + const response = await sdk.models.list({ path: '/models' }) return response.data } diff --git a/src/renderer/src/aiCore/legacy/clients/gemini/GeminiAPIClient.ts b/src/renderer/src/aiCore/legacy/clients/gemini/GeminiAPIClient.ts index 27e659c1af..ac10106f37 100644 --- a/src/renderer/src/aiCore/legacy/clients/gemini/GeminiAPIClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/gemini/GeminiAPIClient.ts @@ -46,6 +46,7 @@ import type { GeminiSdkRawOutput, GeminiSdkToolCall } from '@renderer/types/sdk' +import { getTrailingApiVersion, withoutTrailingApiVersion } from '@renderer/utils' import { isToolUseModeFunction } from '@renderer/utils/assistant' import { geminiFunctionCallToMcpTool, @@ -163,18 +164,24 @@ export class GeminiAPIClient extends BaseApiClient< return models } + override getBaseURL(): string { + return withoutTrailingApiVersion(super.getBaseURL()) + } + override async getSdkInstance() { if (this.sdkInstance) { return this.sdkInstance } + const apiVersion = this.getApiVersion() + this.sdkInstance = new GoogleGenAI({ vertexai: false, apiKey: this.apiKey, - apiVersion: this.getApiVersion(), + apiVersion, httpOptions: { baseUrl: this.getBaseURL(), - apiVersion: this.getApiVersion(), + apiVersion, headers: { ...this.provider.extra_headers } @@ -188,7 +195,14 @@ export class GeminiAPIClient extends BaseApiClient< if (this.provider.isVertex) { return 'v1' } - return 'v1beta' + + // Extract trailing API version from the URL + const trailingVersion = getTrailingApiVersion(this.provider.apiHost || '') + if (trailingVersion) { + return trailingVersion + } + + return '' } /** diff --git a/src/renderer/src/aiCore/legacy/clients/gemini/VertexAPIClient.ts b/src/renderer/src/aiCore/legacy/clients/gemini/VertexAPIClient.ts index 49a96a8f19..fb371d9ae5 100644 --- a/src/renderer/src/aiCore/legacy/clients/gemini/VertexAPIClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/gemini/VertexAPIClient.ts @@ -1,7 +1,8 @@ import { GoogleGenAI } from '@google/genai' import { loggerService } from '@logger' -import { createVertexProvider, isVertexAIConfigured, isVertexProvider } from '@renderer/hooks/useVertexAI' +import { createVertexProvider, isVertexAIConfigured } from '@renderer/hooks/useVertexAI' import type { Model, Provider, VertexProvider } from '@renderer/types' +import { isVertexProvider } from '@renderer/utils/provider' import { isEmpty } from 'lodash' import { AnthropicVertexClient } from '../anthropic/AnthropicVertexClient' diff --git a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIApiClient.ts b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIApiClient.ts index ad87331855..cfc9087545 100644 --- a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIApiClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIApiClient.ts @@ -10,12 +10,9 @@ import { DEFAULT_MAX_TOKENS } from '@renderer/config/constant' import { findTokenLimit, GEMINI_FLASH_MODEL_REGEX, - getOpenAIWebSearchParams, getThinkModelType, - isClaudeReasoningModel, isDeepSeekHybridInferenceModel, isDoubaoThinkingAutoModel, - isGeminiReasoningModel, isGPT5SeriesModel, isGrokReasoningModel, isNotSupportSystemMessageModel, @@ -35,17 +32,10 @@ import { isSupportedThinkingTokenModel, isSupportedThinkingTokenQwenModel, isSupportedThinkingTokenZhipuModel, - isSupportVerbosityModel, isVisionModel, MODEL_SUPPORTED_REASONING_EFFORT, ZHIPU_RESULT_TOKENS } from '@renderer/config/models' -import { - isSupportArrayContentProvider, - isSupportDeveloperRoleProvider, - isSupportEnableThinkingProvider, - isSupportStreamOptionsProvider -} from '@renderer/config/providers' import { mapLanguageToQwenMTModel } from '@renderer/config/translate' import { processPostsuffixQwen3Model, processReqMessages } from '@renderer/services/ModelMessageService' import { estimateTextTokens } from '@renderer/services/TokenService' @@ -89,6 +79,12 @@ import { openAIToolsToMcpTool } from '@renderer/utils/mcp-tools' import { findFileBlocks, findImageBlocks } from '@renderer/utils/messageUtils/find' +import { + isSupportArrayContentProvider, + isSupportDeveloperRoleProvider, + isSupportEnableThinkingProvider, + isSupportStreamOptionsProvider +} from '@renderer/utils/provider' import { t } from 'i18next' import type { GenericChunk } from '../../middleware/schemas' @@ -652,7 +648,6 @@ export class OpenAIAPIClient extends OpenAIBaseClient< logger.warn('No user message. Some providers may not support.') } - // poe 需要通过用户消息传递 reasoningEffort const reasoningEffort = this.getReasoningEffort(assistant, model) const lastUserMsg = userMessages.findLast((m) => m.role === 'user') @@ -663,22 +658,6 @@ export class OpenAIAPIClient extends OpenAIBaseClient< lastUserMsg.content = processPostsuffixQwen3Model(currentContent, qwenThinkModeEnabled) } - if (this.provider.id === SystemProviderIds.poe) { - // 如果以后 poe 支持 reasoning_effort 参数了,可以删掉这部分 - let suffix = '' - if (isGPT5SeriesModel(model) && reasoningEffort.reasoning_effort) { - suffix = ` --reasoning_effort ${reasoningEffort.reasoning_effort}` - } else if (isClaudeReasoningModel(model) && reasoningEffort.thinking?.budget_tokens) { - suffix = ` --thinking_budget ${reasoningEffort.thinking.budget_tokens}` - } else if (isGeminiReasoningModel(model) && reasoningEffort.extra_body?.google?.thinking_config) { - suffix = ` --thinking_budget ${reasoningEffort.extra_body.google.thinking_config.thinking_budget}` - } - // FIXME: poe 不支持多个text part,上传文本文件的时候用的不是file part而是text part,因此会出问题 - // 临时解决方案是强制poe用string content,但是其实poe部分支持array - if (typeof lastUserMsg.content === 'string') { - lastUserMsg.content += suffix - } - } } // 4. 最终请求消息 @@ -734,16 +713,11 @@ export class OpenAIAPIClient extends OpenAIBaseClient< ...modalities, // groq 有不同的 service tier 配置,不符合 openai 接口类型 service_tier: this.getServiceTier(model) as OpenAIServiceTier, - ...(isSupportVerbosityModel(model) - ? { - text: { - verbosity: this.getVerbosity(model) - } - } - : {}), + // verbosity. getVerbosity ensures the returned value is valid. + verbosity: this.getVerbosity(model), ...this.getProviderSpecificParameters(assistant, model), ...reasoningEffort, - ...getOpenAIWebSearchParams(model, enableWebSearch), + // ...getOpenAIWebSearchParams(model, enableWebSearch), // OpenRouter usage tracking ...(this.provider.id === 'openrouter' ? { usage: { include: true } } : {}), ...extra_body, diff --git a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts index 9a8d5f8383..937827db01 100644 --- a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts @@ -11,7 +11,7 @@ import { getStoreSetting } from '@renderer/hooks/useSettings' import { getAssistantSettings } from '@renderer/services/AssistantService' import store from '@renderer/store' import type { SettingsState } from '@renderer/store/settings' -import type { Assistant, GenerateImageParams, Model, Provider } from '@renderer/types' +import { type Assistant, type GenerateImageParams, type Model, type Provider } from '@renderer/types' import type { OpenAIResponseSdkMessageParam, OpenAIResponseSdkParams, @@ -25,7 +25,8 @@ import type { OpenAISdkRawOutput, ReasoningEffortOptionalParams } from '@renderer/types/sdk' -import { formatApiHost } from '@renderer/utils/api' +import { withoutTrailingSlash } from '@renderer/utils/api' +import { isOllamaProvider } from '@renderer/utils/provider' import { BaseApiClient } from '../BaseApiClient' @@ -48,8 +49,9 @@ export abstract class OpenAIBaseClient< } // 仅适用于openai - override getBaseURL(isSupportedAPIVerion: boolean = true): string { - return formatApiHost(this.provider.apiHost, isSupportedAPIVerion) + override getBaseURL(): string { + // apiHost is formatted when called by AiProvider + return this.provider.apiHost } override async generateImage({ @@ -86,7 +88,11 @@ export abstract class OpenAIBaseClient< } override async getEmbeddingDimensions(model: Model): Promise { - const sdk = await this.getSdkInstance() + let sdk: OpenAI = await this.getSdkInstance() + if (isOllamaProvider(this.provider)) { + const embedBaseUrl = `${this.provider.apiHost.replace(/(\/(api|v1))\/?$/, '')}/v1` + sdk = sdk.withOptions({ baseURL: embedBaseUrl }) + } const data = await sdk.embeddings.create({ model: model.id, @@ -99,6 +105,17 @@ export abstract class OpenAIBaseClient< override async listModels(): Promise { try { const sdk = await this.getSdkInstance() + if (this.provider.id === 'openrouter') { + // https://openrouter.ai/docs/api/api-reference/embeddings/list-embeddings-models + const embedBaseUrl = 'https://openrouter.ai/api/v1/embeddings' + const embedSdk = sdk.withOptions({ baseURL: embedBaseUrl }) + const modelPromise = sdk.models.list() + const embedModelPromise = embedSdk.models.list() + const [modelResponse, embedModelResponse] = await Promise.all([modelPromise, embedModelPromise]) + const models = [...modelResponse.data, ...embedModelResponse.data] + const uniqueModels = Array.from(new Map(models.map((model) => [model.id, model])).values()) + return uniqueModels.filter(isSupportedModel) + } if (this.provider.id === 'github') { // GitHub Models 其 models 和 chat completions 两个接口的 baseUrl 不一样 const baseUrl = 'https://models.github.ai/catalog/' @@ -115,6 +132,34 @@ export abstract class OpenAIBaseClient< })) .filter(isSupportedModel) } + + if (isOllamaProvider(this.provider)) { + const baseUrl = withoutTrailingSlash(this.getBaseURL()) + .replace(/\/v1$/, '') + .replace(/\/api$/, '') + const response = await fetch(`${baseUrl}/api/tags`, { + headers: { + Authorization: `Bearer ${this.apiKey}`, + ...this.defaultHeaders(), + ...this.provider.extra_headers + } + }) + + if (!response.ok) { + throw new Error(`Ollama server returned ${response.status} ${response.statusText}`) + } + + const data = await response.json() + if (!data?.models || !Array.isArray(data.models)) { + throw new Error('Invalid response from Ollama API: missing models array') + } + + return data.models.map((model) => ({ + id: model.name, + object: 'model', + owned_by: 'ollama' + })) + } const response = await sdk.models.list() if (this.provider.id === 'together') { // @ts-ignore key is not typed @@ -144,6 +189,7 @@ export abstract class OpenAIBaseClient< let apiKeyForSdkInstance = this.apiKey let baseURLForSdkInstance = this.getBaseURL() + logger.debug('baseURLForSdkInstance', { baseURLForSdkInstance }) let headersForSdkInstance = { ...this.defaultHeaders(), ...this.provider.extra_headers @@ -155,7 +201,7 @@ export abstract class OpenAIBaseClient< // this.provider.apiKey不允许修改 // this.provider.apiKey = token apiKeyForSdkInstance = token - baseURLForSdkInstance = this.getBaseURL(false) + baseURLForSdkInstance = this.getBaseURL() headersForSdkInstance = { ...headersForSdkInstance, ...COPILOT_DEFAULT_HEADERS diff --git a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIResponseAPIClient.ts b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIResponseAPIClient.ts index cfbfdfd9df..b4f63e2bce 100644 --- a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIResponseAPIClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIResponseAPIClient.ts @@ -12,7 +12,6 @@ import { isSupportVerbosityModel, isVisionModel } from '@renderer/config/models' -import { isSupportDeveloperRoleProvider } from '@renderer/config/providers' import { estimateTextTokens } from '@renderer/services/TokenService' import type { FileMetadata, @@ -43,6 +42,7 @@ import { openAIToolsToMcpTool } from '@renderer/utils/mcp-tools' import { findFileBlocks, findImageBlocks } from '@renderer/utils/messageUtils/find' +import { isSupportDeveloperRoleProvider } from '@renderer/utils/provider' import { MB } from '@shared/config/constant' import { t } from 'i18next' import { isEmpty } from 'lodash' @@ -122,6 +122,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< if (this.sdkInstance) { return this.sdkInstance } + const baseUrl = this.getBaseURL() if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') { return new AzureOpenAI({ @@ -134,7 +135,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< return new OpenAI({ dangerouslyAllowBrowser: true, apiKey: this.apiKey, - baseURL: this.getBaseURL(), + baseURL: baseUrl, defaultHeaders: { ...this.defaultHeaders(), ...this.provider.extra_headers diff --git a/src/renderer/src/aiCore/legacy/clients/ovms/OVMSClient.ts b/src/renderer/src/aiCore/legacy/clients/ovms/OVMSClient.ts index 179bb54a1e..02ac6de091 100644 --- a/src/renderer/src/aiCore/legacy/clients/ovms/OVMSClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/ovms/OVMSClient.ts @@ -3,6 +3,7 @@ import { loggerService } from '@logger' import { isSupportedModel } from '@renderer/config/models' import type { Provider } from '@renderer/types' import { objectKeys } from '@renderer/types' +import { formatApiHost, withoutTrailingApiVersion } from '@renderer/utils' import { OpenAIAPIClient } from '../openai/OpenAIApiClient' @@ -16,11 +17,8 @@ export class OVMSClient extends OpenAIAPIClient { override async listModels(): Promise { try { const sdk = await this.getSdkInstance() - - const chatModelsResponse = await sdk.request({ - method: 'get', - path: '../v1/config' - }) + const url = formatApiHost(withoutTrailingApiVersion(this.getBaseURL()), true, 'v1') + const chatModelsResponse = await sdk.withOptions({ baseURL: url }).get('/config') logger.debug(`Chat models response: ${JSON.stringify(chatModelsResponse)}`) // Parse the config response to extract model information diff --git a/src/renderer/src/aiCore/legacy/index.ts b/src/renderer/src/aiCore/legacy/index.ts index da6cdb6726..7c5f5211d9 100644 --- a/src/renderer/src/aiCore/legacy/index.ts +++ b/src/renderer/src/aiCore/legacy/index.ts @@ -2,7 +2,6 @@ import { loggerService } from '@logger' import { ApiClientFactory } from '@renderer/aiCore/legacy/clients/ApiClientFactory' import type { BaseApiClient } from '@renderer/aiCore/legacy/clients/BaseApiClient' import { isDedicatedImageGenerationModel, isFunctionCallingModel } from '@renderer/config/models' -import { getProviderByModel } from '@renderer/services/AssistantService' import { withSpanResult } from '@renderer/services/SpanManagerService' import type { StartSpanParams } from '@renderer/trace/types/ModelSpanEntity' import type { GenerateImageParams, Model, Provider } from '@renderer/types' @@ -160,9 +159,6 @@ export default class AiProvider { public async getEmbeddingDimensions(model: Model): Promise { try { // Use the SDK instance to test embedding capabilities - if (this.apiClient instanceof OpenAIResponseAPIClient && getProviderByModel(model).type === 'azure-openai') { - this.apiClient = this.apiClient.getClient(model) as BaseApiClient - } const dimensions = await this.apiClient.getEmbeddingDimensions(model) return dimensions } catch (error) { diff --git a/src/renderer/src/aiCore/legacy/middleware/common/ErrorHandlerMiddleware.ts b/src/renderer/src/aiCore/legacy/middleware/common/ErrorHandlerMiddleware.ts index 7d6a7f631a..c93e42fbb2 100644 --- a/src/renderer/src/aiCore/legacy/middleware/common/ErrorHandlerMiddleware.ts +++ b/src/renderer/src/aiCore/legacy/middleware/common/ErrorHandlerMiddleware.ts @@ -1,6 +1,7 @@ import { loggerService } from '@logger' import { isZhipuModel } from '@renderer/config/models' import { getStoreProviders } from '@renderer/hooks/useStore' +import { getDefaultModel } from '@renderer/services/AssistantService' import type { Chunk } from '@renderer/types/chunk' import type { CompletionsParams, CompletionsResult } from '../schemas' @@ -66,7 +67,7 @@ export const ErrorHandlerMiddleware = } function handleError(error: any, params: CompletionsParams): any { - if (isZhipuModel(params.assistant.model) && error.status && !params.enableGenerateImage) { + if (isZhipuModel(params.assistant.model || getDefaultModel()) && error.status && !params.enableGenerateImage) { return handleZhipuError(error) } diff --git a/src/renderer/src/aiCore/middleware/AiSdkMiddlewareBuilder.ts b/src/renderer/src/aiCore/middleware/AiSdkMiddlewareBuilder.ts index 3f14917cdd..10a4d59384 100644 --- a/src/renderer/src/aiCore/middleware/AiSdkMiddlewareBuilder.ts +++ b/src/renderer/src/aiCore/middleware/AiSdkMiddlewareBuilder.ts @@ -1,18 +1,21 @@ import type { WebSearchPluginConfig } from '@cherrystudio/ai-core/built-in/plugins' import { loggerService } from '@logger' -import { isSupportedThinkingTokenQwenModel } from '@renderer/config/models' -import { isSupportEnableThinkingProvider } from '@renderer/config/providers' +import { isGemini3Model, isSupportedThinkingTokenQwenModel } from '@renderer/config/models' import type { MCPTool } from '@renderer/types' -import { type Assistant, type Message, type Model, type Provider } from '@renderer/types' +import { type Assistant, type Message, type Model, type Provider, SystemProviderIds } from '@renderer/types' import type { Chunk } from '@renderer/types/chunk' +import { isOllamaProvider, isSupportEnableThinkingProvider } from '@renderer/utils/provider' import type { LanguageModelMiddleware } from 'ai' import { extractReasoningMiddleware, simulateStreamingMiddleware } from 'ai' import { isEmpty } from 'lodash' +import { getAiSdkProviderId } from '../provider/factory' import { isOpenRouterGeminiGenerateImageModel } from '../utils/image' import { noThinkMiddleware } from './noThinkMiddleware' import { openrouterGenerateImageMiddleware } from './openrouterGenerateImageMiddleware' +import { openrouterReasoningMiddleware } from './openrouterReasoningMiddleware' import { qwenThinkingMiddleware } from './qwenThinkingMiddleware' +import { skipGeminiThoughtSignatureMiddleware } from './skipGeminiThoughtSignatureMiddleware' import { toolChoiceMiddleware } from './toolChoiceMiddleware' const logger = loggerService.withContext('AiSdkMiddlewareBuilder') @@ -217,6 +220,14 @@ function addProviderSpecificMiddlewares(builder: AiSdkMiddlewareBuilder, config: middleware: noThinkMiddleware() }) } + + if (config.provider.id === SystemProviderIds.openrouter && config.enableReasoning) { + builder.add({ + name: 'openrouter-reasoning-redaction', + middleware: openrouterReasoningMiddleware() + }) + logger.debug('Added OpenRouter reasoning redaction middleware') + } } /** @@ -229,6 +240,7 @@ function addModelSpecificMiddlewares(builder: AiSdkMiddlewareBuilder, config: Ai // Use /think or /no_think suffix to control thinking mode if ( config.provider && + !isOllamaProvider(config.provider) && isSupportedThinkingTokenQwenModel(config.model) && !isSupportEnableThinkingProvider(config.provider) ) { @@ -248,6 +260,15 @@ function addModelSpecificMiddlewares(builder: AiSdkMiddlewareBuilder, config: Ai middleware: openrouterGenerateImageMiddleware() }) } + + if (isGemini3Model(config.model)) { + const aiSdkId = getAiSdkProviderId(config.provider) + builder.add({ + name: 'skip-gemini3-thought-signature', + middleware: skipGeminiThoughtSignatureMiddleware(aiSdkId) + }) + logger.debug('Added skip Gemini3 thought signature middleware') + } } /** diff --git a/src/renderer/src/aiCore/middleware/openrouterReasoningMiddleware.ts b/src/renderer/src/aiCore/middleware/openrouterReasoningMiddleware.ts new file mode 100644 index 0000000000..9ef3df61e9 --- /dev/null +++ b/src/renderer/src/aiCore/middleware/openrouterReasoningMiddleware.ts @@ -0,0 +1,50 @@ +import type { LanguageModelV2StreamPart } from '@ai-sdk/provider' +import type { LanguageModelMiddleware } from 'ai' + +/** + * https://openrouter.ai/docs/docs/best-practices/reasoning-tokens#example-preserving-reasoning-blocks-with-openrouter-and-claude + * + * @returns LanguageModelMiddleware - a middleware filter redacted block + */ +export function openrouterReasoningMiddleware(): LanguageModelMiddleware { + const REDACTED_BLOCK = '[REDACTED]' + return { + middlewareVersion: 'v2', + wrapGenerate: async ({ doGenerate }) => { + const { content, ...rest } = await doGenerate() + const modifiedContent = content.map((part) => { + if (part.type === 'reasoning' && part.text.includes(REDACTED_BLOCK)) { + return { + ...part, + text: part.text.replace(REDACTED_BLOCK, '') + } + } + return part + }) + return { content: modifiedContent, ...rest } + }, + wrapStream: async ({ doStream }) => { + const { stream, ...rest } = await doStream() + return { + stream: stream.pipeThrough( + new TransformStream({ + transform( + chunk: LanguageModelV2StreamPart, + controller: TransformStreamDefaultController + ) { + if (chunk.type === 'reasoning-delta' && chunk.delta.includes(REDACTED_BLOCK)) { + controller.enqueue({ + ...chunk, + delta: chunk.delta.replace(REDACTED_BLOCK, '') + }) + } else { + controller.enqueue(chunk) + } + } + }) + ), + ...rest + } + } + } +} diff --git a/src/renderer/src/aiCore/middleware/skipGeminiThoughtSignatureMiddleware.ts b/src/renderer/src/aiCore/middleware/skipGeminiThoughtSignatureMiddleware.ts new file mode 100644 index 0000000000..da318ea60d --- /dev/null +++ b/src/renderer/src/aiCore/middleware/skipGeminiThoughtSignatureMiddleware.ts @@ -0,0 +1,36 @@ +import type { LanguageModelMiddleware } from 'ai' + +/** + * skip Gemini Thought Signature Middleware + * 由于多模型客户端请求的复杂性(可以中途切换其他模型),这里选择通过中间件方式添加跳过所有 Gemini3 思考签名 + * Due to the complexity of multi-model client requests (which can switch to other models mid-process), + * it was decided to add a skip for all Gemini3 thinking signatures via middleware. + * @param aiSdkId AI SDK Provider ID + * @returns LanguageModelMiddleware + */ +export function skipGeminiThoughtSignatureMiddleware(aiSdkId: string): LanguageModelMiddleware { + const MAGIC_STRING = 'skip_thought_signature_validator' + return { + middlewareVersion: 'v2', + + transformParams: async ({ params }) => { + const transformedParams = { ...params } + // Process messages in prompt + if (transformedParams.prompt && Array.isArray(transformedParams.prompt)) { + transformedParams.prompt = transformedParams.prompt.map((message) => { + if (typeof message.content !== 'string') { + for (const part of message.content) { + const googleOptions = part?.providerOptions?.[aiSdkId] + if (googleOptions?.thoughtSignature) { + googleOptions.thoughtSignature = MAGIC_STRING + } + } + } + return message + }) + } + + return transformedParams + } + } +} diff --git a/src/renderer/src/aiCore/prepareParams/__tests__/message-converter.test.ts b/src/renderer/src/aiCore/prepareParams/__tests__/message-converter.test.ts new file mode 100644 index 0000000000..cb0c5cf9a6 --- /dev/null +++ b/src/renderer/src/aiCore/prepareParams/__tests__/message-converter.test.ts @@ -0,0 +1,408 @@ +import type { Message, Model } from '@renderer/types' +import type { FileMetadata } from '@renderer/types/file' +import { FileTypes } from '@renderer/types/file' +import { + AssistantMessageStatus, + type FileMessageBlock, + type ImageMessageBlock, + MessageBlockStatus, + MessageBlockType, + type ThinkingMessageBlock, + UserMessageStatus +} from '@renderer/types/newMessage' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const { convertFileBlockToFilePartMock, convertFileBlockToTextPartMock } = vi.hoisted(() => ({ + convertFileBlockToFilePartMock: vi.fn(), + convertFileBlockToTextPartMock: vi.fn() +})) + +vi.mock('../fileProcessor', () => ({ + convertFileBlockToFilePart: convertFileBlockToFilePartMock, + convertFileBlockToTextPart: convertFileBlockToTextPartMock +})) + +const visionModelIds = new Set(['gpt-4o-mini', 'qwen-image-edit']) +const imageEnhancementModelIds = new Set(['qwen-image-edit']) + +vi.mock('@renderer/config/models', () => ({ + isVisionModel: (model: Model) => visionModelIds.has(model.id), + isImageEnhancementModel: (model: Model) => imageEnhancementModelIds.has(model.id) +})) + +type MockableMessage = Message & { + __mockContent?: string + __mockFileBlocks?: FileMessageBlock[] + __mockImageBlocks?: ImageMessageBlock[] + __mockThinkingBlocks?: ThinkingMessageBlock[] +} + +vi.mock('@renderer/utils/messageUtils/find', () => ({ + getMainTextContent: (message: Message) => (message as MockableMessage).__mockContent ?? '', + findFileBlocks: (message: Message) => (message as MockableMessage).__mockFileBlocks ?? [], + findImageBlocks: (message: Message) => (message as MockableMessage).__mockImageBlocks ?? [], + findThinkingBlocks: (message: Message) => (message as MockableMessage).__mockThinkingBlocks ?? [] +})) + +import { convertMessagesToSdkMessages, convertMessageToSdkParam } from '../messageConverter' + +let messageCounter = 0 +let blockCounter = 0 + +const createModel = (overrides: Partial = {}): Model => ({ + id: 'gpt-4o-mini', + name: 'GPT-4o mini', + provider: 'openai', + group: 'openai', + ...overrides +}) + +const createMessage = (role: Message['role']): MockableMessage => + ({ + id: `message-${++messageCounter}`, + role, + assistantId: 'assistant-1', + topicId: 'topic-1', + createdAt: new Date(2024, 0, 1, 0, 0, messageCounter).toISOString(), + status: role === 'assistant' ? AssistantMessageStatus.SUCCESS : UserMessageStatus.SUCCESS, + blocks: [] + }) as MockableMessage + +const createFileBlock = ( + messageId: string, + overrides: Partial> & { file?: Partial } = {} +): FileMessageBlock => { + const { file, ...blockOverrides } = overrides + const timestamp = new Date(2024, 0, 1, 0, 0, ++blockCounter).toISOString() + return { + id: blockOverrides.id ?? `file-block-${blockCounter}`, + messageId, + type: MessageBlockType.FILE, + createdAt: blockOverrides.createdAt ?? timestamp, + status: blockOverrides.status ?? MessageBlockStatus.SUCCESS, + file: { + id: file?.id ?? `file-${blockCounter}`, + name: file?.name ?? 'document.txt', + origin_name: file?.origin_name ?? 'document.txt', + path: file?.path ?? '/tmp/document.txt', + size: file?.size ?? 1024, + ext: file?.ext ?? '.txt', + type: file?.type ?? FileTypes.TEXT, + created_at: file?.created_at ?? timestamp, + count: file?.count ?? 1, + ...file + }, + ...blockOverrides + } +} + +const createImageBlock = ( + messageId: string, + overrides: Partial> = {} +): ImageMessageBlock => ({ + id: overrides.id ?? `image-block-${++blockCounter}`, + messageId, + type: MessageBlockType.IMAGE, + createdAt: overrides.createdAt ?? new Date(2024, 0, 1, 0, 0, blockCounter).toISOString(), + status: overrides.status ?? MessageBlockStatus.SUCCESS, + url: overrides.url ?? 'https://example.com/image.png', + ...overrides +}) + +describe('messageConverter', () => { + beforeEach(() => { + convertFileBlockToFilePartMock.mockReset() + convertFileBlockToTextPartMock.mockReset() + convertFileBlockToFilePartMock.mockResolvedValue(null) + convertFileBlockToTextPartMock.mockResolvedValue(null) + messageCounter = 0 + blockCounter = 0 + }) + + describe('convertMessageToSdkParam', () => { + it('includes text and image parts for user messages on vision models', async () => { + const model = createModel() + const message = createMessage('user') + message.__mockContent = 'Describe this picture' + message.__mockImageBlocks = [createImageBlock(message.id, { url: 'https://example.com/cat.png' })] + + const result = await convertMessageToSdkParam(message, true, model) + + expect(result).toEqual({ + role: 'user', + content: [ + { type: 'text', text: 'Describe this picture' }, + { type: 'image', image: 'https://example.com/cat.png' } + ] + }) + }) + + it('extracts base64 data from data URLs and preserves mediaType', async () => { + const model = createModel() + const message = createMessage('user') + message.__mockContent = 'Check this image' + message.__mockImageBlocks = [createImageBlock(message.id, { url: 'data:image/png;base64,iVBORw0KGgoAAAANS' })] + + const result = await convertMessageToSdkParam(message, true, model) + + expect(result).toEqual({ + role: 'user', + content: [ + { type: 'text', text: 'Check this image' }, + { type: 'image', image: 'iVBORw0KGgoAAAANS', mediaType: 'image/png' } + ] + }) + }) + + it('handles data URLs without mediaType gracefully', async () => { + const model = createModel() + const message = createMessage('user') + message.__mockContent = 'Check this' + message.__mockImageBlocks = [createImageBlock(message.id, { url: 'data:;base64,AAABBBCCC' })] + + const result = await convertMessageToSdkParam(message, true, model) + + expect(result).toEqual({ + role: 'user', + content: [ + { type: 'text', text: 'Check this' }, + { type: 'image', image: 'AAABBBCCC' } + ] + }) + }) + + it('skips malformed data URLs without comma separator', async () => { + const model = createModel() + const message = createMessage('user') + message.__mockContent = 'Malformed data url' + message.__mockImageBlocks = [createImageBlock(message.id, { url: 'data:image/pngAAABBB' })] + + const result = await convertMessageToSdkParam(message, true, model) + + expect(result).toEqual({ + role: 'user', + content: [ + { type: 'text', text: 'Malformed data url' } + // Malformed data URL is excluded from the content + ] + }) + }) + + it('handles multiple large base64 images without stack overflow', async () => { + const model = createModel() + const message = createMessage('user') + // Create large base64 strings (~500KB each) to simulate real-world large images + const largeBase64 = 'A'.repeat(500_000) + message.__mockContent = 'Check these images' + message.__mockImageBlocks = [ + createImageBlock(message.id, { url: `data:image/png;base64,${largeBase64}` }), + createImageBlock(message.id, { url: `data:image/png;base64,${largeBase64}` }), + createImageBlock(message.id, { url: `data:image/png;base64,${largeBase64}` }) + ] + + // Should not throw RangeError: Maximum call stack size exceeded + await expect(convertMessageToSdkParam(message, true, model)).resolves.toBeDefined() + }) + + it('returns file instructions as a system message when native uploads succeed', async () => { + const model = createModel() + const message = createMessage('user') + message.__mockContent = 'Summarize the PDF' + message.__mockFileBlocks = [createFileBlock(message.id)] + convertFileBlockToFilePartMock.mockResolvedValueOnce({ + type: 'file', + filename: 'document.pdf', + mediaType: 'application/pdf', + data: 'fileid://remote-file' + }) + + const result = await convertMessageToSdkParam(message, false, model) + + expect(result).toEqual([ + { + role: 'system', + content: 'fileid://remote-file' + }, + { + role: 'user', + content: [{ type: 'text', text: 'Summarize the PDF' }] + } + ]) + }) + }) + + describe('convertMessagesToSdkMessages', () => { + it('collapses to [system?, user(image)] for image enhancement models', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const initialUser = createMessage('user') + initialUser.__mockContent = 'Start editing' + + const assistant = createMessage('assistant') + assistant.__mockContent = 'Here is the current preview' + assistant.__mockImageBlocks = [createImageBlock(assistant.id, { url: 'https://example.com/preview.png' })] + + const finalUser = createMessage('user') + finalUser.__mockContent = 'Increase the brightness' + + const result = await convertMessagesToSdkMessages([initialUser, assistant, finalUser], model) + + expect(result).toEqual([ + { + role: 'user', + content: [ + { type: 'text', text: 'Increase the brightness' }, + { type: 'image', image: 'https://example.com/preview.png' } + ] + } + ]) + }) + + it('preserves system messages and collapses others for enhancement payloads', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const fileUser = createMessage('user') + fileUser.__mockContent = 'Use this document as inspiration' + fileUser.__mockFileBlocks = [createFileBlock(fileUser.id, { file: { ext: '.pdf', type: FileTypes.DOCUMENT } })] + convertFileBlockToFilePartMock.mockResolvedValueOnce({ + type: 'file', + filename: 'reference.pdf', + mediaType: 'application/pdf', + data: 'fileid://reference' + }) + + const assistant = createMessage('assistant') + assistant.__mockContent = 'Generated previews ready' + assistant.__mockImageBlocks = [createImageBlock(assistant.id, { url: 'https://example.com/reference.png' })] + + const finalUser = createMessage('user') + finalUser.__mockContent = 'Apply the edits' + + const result = await convertMessagesToSdkMessages([fileUser, assistant, finalUser], model) + + expect(result).toEqual([ + { role: 'system', content: 'fileid://reference' }, + { + role: 'user', + content: [ + { type: 'text', text: 'Apply the edits' }, + { type: 'image', image: 'https://example.com/reference.png' } + ] + } + ]) + }) + + it('handles no previous assistant message with images', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const user1 = createMessage('user') + user1.__mockContent = 'Start' + + const user2 = createMessage('user') + user2.__mockContent = 'Continue without images' + + const result = await convertMessagesToSdkMessages([user1, user2], model) + + expect(result).toEqual([ + { + role: 'user', + content: [{ type: 'text', text: 'Continue without images' }] + } + ]) + }) + + it('handles assistant message without images', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const user1 = createMessage('user') + user1.__mockContent = 'Start' + + const assistant = createMessage('assistant') + assistant.__mockContent = 'Text only response' + assistant.__mockImageBlocks = [] + + const user2 = createMessage('user') + user2.__mockContent = 'Follow up' + + const result = await convertMessagesToSdkMessages([user1, assistant, user2], model) + + expect(result).toEqual([ + { + role: 'user', + content: [{ type: 'text', text: 'Follow up' }] + } + ]) + }) + + it('handles multiple assistant messages by using the most recent one', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const user1 = createMessage('user') + user1.__mockContent = 'Start' + + const assistant1 = createMessage('assistant') + assistant1.__mockContent = 'First response' + assistant1.__mockImageBlocks = [createImageBlock(assistant1.id, { url: 'https://example.com/old.png' })] + + const user2 = createMessage('user') + user2.__mockContent = 'Continue' + + const assistant2 = createMessage('assistant') + assistant2.__mockContent = 'Second response' + assistant2.__mockImageBlocks = [createImageBlock(assistant2.id, { url: 'https://example.com/new.png' })] + + const user3 = createMessage('user') + user3.__mockContent = 'Final request' + + const result = await convertMessagesToSdkMessages([user1, assistant1, user2, assistant2, user3], model) + + expect(result).toEqual([ + { + role: 'user', + content: [ + { type: 'text', text: 'Final request' }, + { type: 'image', image: 'https://example.com/new.png' } + ] + } + ]) + }) + + it('handles conversation ending with assistant message', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const user = createMessage('user') + user.__mockContent = 'Start' + + const assistant = createMessage('assistant') + assistant.__mockContent = 'Response with image' + assistant.__mockImageBlocks = [createImageBlock(assistant.id, { url: 'https://example.com/image.png' })] + + const result = await convertMessagesToSdkMessages([user, assistant], model) + + // The user message is the last user message, but since the assistant comes after, + // there's no "previous" assistant message (search starts from messages.length-2 backwards) + expect(result).toEqual([ + { + role: 'user', + content: [{ type: 'text', text: 'Start' }] + } + ]) + }) + + it('handles empty content in last user message', async () => { + const model = createModel({ id: 'qwen-image-edit', name: 'Qwen Image Edit', provider: 'qwen', group: 'qwen' }) + const user1 = createMessage('user') + user1.__mockContent = 'Start' + + const assistant = createMessage('assistant') + assistant.__mockContent = 'Here is the preview' + assistant.__mockImageBlocks = [createImageBlock(assistant.id, { url: 'https://example.com/preview.png' })] + + const user2 = createMessage('user') + user2.__mockContent = '' + + const result = await convertMessagesToSdkMessages([user1, assistant, user2], model) + + expect(result).toEqual([ + { + role: 'user', + content: [{ type: 'image', image: 'https://example.com/preview.png' }] + } + ]) + }) + }) +}) diff --git a/src/renderer/src/aiCore/prepareParams/__tests__/model-parameters.test.ts b/src/renderer/src/aiCore/prepareParams/__tests__/model-parameters.test.ts new file mode 100644 index 0000000000..70b4ac84b7 --- /dev/null +++ b/src/renderer/src/aiCore/prepareParams/__tests__/model-parameters.test.ts @@ -0,0 +1,218 @@ +import type { Assistant, AssistantSettings, Model, Topic } from '@renderer/types' +import { TopicType } from '@renderer/types' +import { defaultTimeout } from '@shared/config/constant' +import { describe, expect, it, vi } from 'vitest' + +import { getTemperature, getTimeout, getTopP } from '../modelParameters' + +vi.mock('@renderer/services/AssistantService', () => ({ + getAssistantSettings: (assistant: Assistant): AssistantSettings => ({ + contextCount: assistant.settings?.contextCount ?? 4096, + temperature: assistant.settings?.temperature ?? 0.7, + enableTemperature: assistant.settings?.enableTemperature ?? true, + topP: assistant.settings?.topP ?? 1, + enableTopP: assistant.settings?.enableTopP ?? false, + enableMaxTokens: assistant.settings?.enableMaxTokens ?? false, + maxTokens: assistant.settings?.maxTokens, + streamOutput: assistant.settings?.streamOutput ?? true, + toolUseMode: assistant.settings?.toolUseMode ?? 'prompt', + defaultModel: assistant.defaultModel, + customParameters: assistant.settings?.customParameters ?? [], + reasoning_effort: assistant.settings?.reasoning_effort, + reasoning_effort_cache: assistant.settings?.reasoning_effort_cache, + qwenThinkMode: assistant.settings?.qwenThinkMode + }) +})) + +vi.mock('@renderer/hooks/useSettings', () => ({ + getStoreSetting: vi.fn(), + useSettings: vi.fn(() => ({})), + useNavbarPosition: vi.fn(() => ({ navbarPosition: 'left', isLeftNavbar: true, isTopNavbar: false })) +})) + +vi.mock('@renderer/hooks/useStore', () => ({ + getStoreProviders: vi.fn(() => []) +})) + +vi.mock('@renderer/store/settings', () => ({ + default: (state = { settings: {} }) => state +})) + +vi.mock('@renderer/store/assistants', () => ({ + default: (state = { assistants: [] }) => state +})) + +const createTopic = (assistantId: string): Topic => ({ + id: `topic-${assistantId}`, + assistantId, + name: 'topic', + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + messages: [], + type: TopicType.Chat +}) + +const createAssistant = (settings: Assistant['settings'] = {}): Assistant => { + const assistantId = 'assistant-1' + return { + id: assistantId, + name: 'Test Assistant', + prompt: 'prompt', + topics: [createTopic(assistantId)], + type: 'assistant', + settings + } +} + +const createModel = (overrides: Partial = {}): Model => ({ + id: 'gpt-4o', + provider: 'openai', + name: 'GPT-4o', + group: 'openai', + ...overrides +}) + +describe('modelParameters', () => { + describe('getTemperature', () => { + it('returns undefined when reasoning effort is enabled for Claude models', () => { + const assistant = createAssistant({ reasoning_effort: 'medium' }) + const model = createModel({ id: 'claude-opus-4', name: 'Claude Opus 4', provider: 'anthropic', group: 'claude' }) + + expect(getTemperature(assistant, model)).toBeUndefined() + }) + + it('returns undefined for models without temperature/topP support', () => { + const assistant = createAssistant({ enableTemperature: true }) + const model = createModel({ id: 'qwen-mt-large', name: 'Qwen MT', provider: 'qwen', group: 'qwen' }) + + expect(getTemperature(assistant, model)).toBeUndefined() + }) + + it('returns undefined for Claude 4.5 reasoning models when only TopP is enabled', () => { + const assistant = createAssistant({ enableTopP: true, enableTemperature: false }) + const model = createModel({ + id: 'claude-sonnet-4.5', + name: 'Claude Sonnet 4.5', + provider: 'anthropic', + group: 'claude' + }) + + expect(getTemperature(assistant, model)).toBeUndefined() + }) + + it('returns configured temperature when enabled', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 0.42 }) + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTemperature(assistant, model)).toBe(0.42) + }) + + it('returns undefined when temperature is disabled', () => { + const assistant = createAssistant({ enableTemperature: false, temperature: 0.9 }) + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTemperature(assistant, model)).toBeUndefined() + }) + + it('clamps temperature to max 1.0 for Zhipu models', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 2.0 }) + const model = createModel({ id: 'glm-4-plus', name: 'GLM-4 Plus', provider: 'zhipu', group: 'zhipu' }) + + expect(getTemperature(assistant, model)).toBe(1.0) + }) + + it('clamps temperature to max 1.0 for Anthropic models', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 1.5 }) + const model = createModel({ + id: 'claude-sonnet-3.5', + name: 'Claude 3.5 Sonnet', + provider: 'anthropic', + group: 'claude' + }) + + expect(getTemperature(assistant, model)).toBe(1.0) + }) + + it('clamps temperature to max 1.0 for Moonshot models', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 2.0 }) + const model = createModel({ + id: 'moonshot-v1-8k', + name: 'Moonshot v1 8k', + provider: 'moonshot', + group: 'moonshot' + }) + + expect(getTemperature(assistant, model)).toBe(1.0) + }) + + it('does not clamp temperature for OpenAI models', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 2.0 }) + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTemperature(assistant, model)).toBe(2.0) + }) + + it('does not clamp temperature when it is already within limits', () => { + const assistant = createAssistant({ enableTemperature: true, temperature: 0.8 }) + const model = createModel({ id: 'glm-4-plus', name: 'GLM-4 Plus', provider: 'zhipu', group: 'zhipu' }) + + expect(getTemperature(assistant, model)).toBe(0.8) + }) + }) + + describe('getTopP', () => { + it('returns undefined when reasoning effort is enabled for Claude models', () => { + const assistant = createAssistant({ reasoning_effort: 'high' }) + const model = createModel({ id: 'claude-opus-4', provider: 'anthropic', group: 'claude' }) + + expect(getTopP(assistant, model)).toBeUndefined() + }) + + it('returns undefined for models without TopP support', () => { + const assistant = createAssistant({ enableTopP: true }) + const model = createModel({ id: 'qwen-mt-small', name: 'Qwen MT', provider: 'qwen', group: 'qwen' }) + + expect(getTopP(assistant, model)).toBeUndefined() + }) + + it('returns undefined for Claude 4.5 reasoning models when temperature is enabled', () => { + const assistant = createAssistant({ enableTemperature: true }) + const model = createModel({ + id: 'claude-opus-4.5', + name: 'Claude Opus 4.5', + provider: 'anthropic', + group: 'claude' + }) + + expect(getTopP(assistant, model)).toBeUndefined() + }) + + it('returns configured TopP when enabled', () => { + const assistant = createAssistant({ enableTopP: true, topP: 0.73 }) + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTopP(assistant, model)).toBe(0.73) + }) + + it('returns undefined when TopP is disabled', () => { + const assistant = createAssistant({ enableTopP: false, topP: 0.5 }) + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTopP(assistant, model)).toBeUndefined() + }) + }) + + describe('getTimeout', () => { + it('uses an extended timeout for flex service tier models', () => { + const model = createModel({ id: 'o3-pro', provider: 'openai', group: 'openai' }) + + expect(getTimeout(model)).toBe(15 * 1000 * 60) + }) + + it('falls back to the default timeout otherwise', () => { + const model = createModel({ id: 'gpt-4o', provider: 'openai', group: 'openai' }) + + expect(getTimeout(model)).toBe(defaultTimeout) + }) + }) +}) diff --git a/src/renderer/src/aiCore/prepareParams/header.ts b/src/renderer/src/aiCore/prepareParams/header.ts index 8c53cbce53..480f13314e 100644 --- a/src/renderer/src/aiCore/prepareParams/header.ts +++ b/src/renderer/src/aiCore/prepareParams/header.ts @@ -1,13 +1,33 @@ -import { isClaude45ReasoningModel } from '@renderer/config/models' +import { isClaude4SeriesModel, isClaude45ReasoningModel } from '@renderer/config/models' +import { getProviderByModel } from '@renderer/services/AssistantService' import type { Assistant, Model } from '@renderer/types' import { isToolUseModeFunction } from '@renderer/utils/assistant' +import { isAwsBedrockProvider, isVertexProvider } from '@renderer/utils/provider' +// https://docs.claude.com/en/docs/build-with-claude/extended-thinking#interleaved-thinking const INTERLEAVED_THINKING_HEADER = 'interleaved-thinking-2025-05-14' +// https://docs.claude.com/en/docs/build-with-claude/context-windows#1m-token-context-window +// const CONTEXT_100M_HEADER = 'context-1m-2025-08-07' +// https://docs.cloud.google.com/vertex-ai/generative-ai/docs/partner-models/claude/web-search +const WEBSEARCH_HEADER = 'web-search-2025-03-05' export function addAnthropicHeaders(assistant: Assistant, model: Model): string[] { const anthropicHeaders: string[] = [] - if (isClaude45ReasoningModel(model) && isToolUseModeFunction(assistant)) { + const provider = getProviderByModel(model) + if ( + isClaude45ReasoningModel(model) && + isToolUseModeFunction(assistant) && + !(isVertexProvider(provider) || isAwsBedrockProvider(provider)) + ) { anthropicHeaders.push(INTERLEAVED_THINKING_HEADER) } + if (isClaude4SeriesModel(model)) { + if (isVertexProvider(provider) && assistant.enableWebSearch) { + anthropicHeaders.push(WEBSEARCH_HEADER) + } + // We may add it by user preference in assistant.settings instead of always adding it. + // See #11540, #11397 + // anthropicHeaders.push(CONTEXT_100M_HEADER) + } return anthropicHeaders } diff --git a/src/renderer/src/aiCore/prepareParams/messageConverter.ts b/src/renderer/src/aiCore/prepareParams/messageConverter.ts index 95c75dccad..e0316a4cf1 100644 --- a/src/renderer/src/aiCore/prepareParams/messageConverter.ts +++ b/src/renderer/src/aiCore/prepareParams/messageConverter.ts @@ -12,6 +12,7 @@ import type { ThinkingMessageBlock, ToolMessageBlock } from '@renderer/types/newMessage' +import { parseDataUrlMediaType } from '@renderer/utils/image' import { findFileBlocks, findImageBlocks, @@ -68,23 +69,29 @@ async function convertImageBlockToImagePart(imageBlocks: ImageMessageBlock[]): P mediaType: image.mime }) } catch (error) { - logger.warn('Failed to load image:', error as Error) + logger.error('Failed to load image file, image will be excluded from message:', { + fileId: imageBlock.file.id, + fileName: imageBlock.file.origin_name, + error: error as Error + }) } } else if (imageBlock.url) { - const isBase64 = imageBlock.url.startsWith('data:') - if (isBase64) { - const base64 = imageBlock.url.match(/^data:[^;]*;base64,(.+)$/)![1] - const mimeMatch = imageBlock.url.match(/^data:([^;]+)/) - parts.push({ - type: 'image', - image: base64, - mediaType: mimeMatch ? mimeMatch[1] : 'image/png' - }) + const url = imageBlock.url + const isDataUrl = url.startsWith('data:') + if (isDataUrl) { + const { mediaType } = parseDataUrlMediaType(url) + const commaIndex = url.indexOf(',') + if (commaIndex === -1) { + logger.error('Malformed data URL detected (missing comma separator), image will be excluded:', { + urlPrefix: url.slice(0, 50) + '...' + }) + continue + } + const base64Data = url.slice(commaIndex + 1) + parts.push({ type: 'image', image: base64Data, ...(mediaType ? { mediaType } : {}) }) } else { - parts.push({ - type: 'image', - image: imageBlock.url - }) + // For remote URLs we keep payload minimal to match existing expectations. + parts.push({ type: 'image', image: url }) } } } @@ -258,20 +265,23 @@ async function convertMessageToAssistantAndToolMessages( * This function processes messages and transforms them into the format required by the SDK. * It handles special cases for vision models and image enhancement models. * - * @param messages - Array of messages to convert. Must contain at least 2 messages when using image enhancement models. + * @param messages - Array of messages to convert. * @param model - The model configuration that determines conversion behavior * * @returns A promise that resolves to an array of SDK-compatible model messages * * @remarks - * For image enhancement models with 2+ messages: - * - Expects the second-to-last message (index length-2) to be an assistant message containing image blocks - * - Expects the last message (index length-1) to be a user message - * - Extracts images from the assistant message and appends them to the user message content - * - Returns only the last two processed messages [assistantSdkMessage, userSdkMessage] + * For image enhancement models: + * - Collapses the conversation into [system?, user(image)] format + * - Searches backwards through all messages to find the most recent assistant message with images + * - Preserves all system messages (including ones generated from file uploads like 'fileid://...') + * - Extracts the last user message content and merges images from the previous assistant message + * - Returns only the collapsed messages: system messages (if any) followed by a single user message + * - If no user message is found, returns only system messages + * - Typical pattern: [system?, user, assistant(image), user] -> [system?, user(image)] * * For other models: - * - Returns all converted messages in order + * - Returns all converted messages in order without special image handling * * The function automatically detects vision model capabilities and adjusts conversion accordingly. */ @@ -284,29 +294,66 @@ export async function convertMessagesToSdkMessages(messages: Message[], model: M sdkMessages.push(...(Array.isArray(sdkMessage) ? sdkMessage : [sdkMessage])) } // Special handling for image enhancement models - // Only keep the last two messages and merge images into the user message - // [system?, user, assistant, user] - if (isImageEnhancementModel(model) && messages.length >= 3) { - const needUpdatedMessages = messages.slice(-2) - const needUpdatedSdkMessages = sdkMessages.slice(-2) - const assistantMessage = needUpdatedMessages.filter((m) => m.role === 'assistant')[0] - const assistantSdkMessage = needUpdatedSdkMessages.filter((m) => m.role === 'assistant')[0] - const userSdkMessage = needUpdatedSdkMessages.filter((m) => m.role === 'user')[0] - const systemSdkMessages = sdkMessages.filter((m) => m.role === 'system') - const imageBlocks = findImageBlocks(assistantMessage) - const imageParts = await convertImageBlockToImagePart(imageBlocks) - const parts: Array = [] - if (typeof userSdkMessage.content === 'string') { - parts.push({ type: 'text', text: userSdkMessage.content }) - parts.push(...imageParts) - userSdkMessage.content = parts - } else { - userSdkMessage.content.push(...imageParts) + // Target behavior: Collapse the conversation into [system?, user(image)]. + // Explanation of why we don't simply use slice: + // 1) We need to preserve all system messages: During the convertMessageToSdkParam process, native file uploads may insert `system(fileid://...)`. + // Directly slicing the original messages or already converted sdkMessages could easily result in missing these system instructions. + // Therefore, we first perform a full conversion and then aggregate the system messages afterward. + // 2) The conversion process may split messages: A single user message might be broken into two SDK messages—[system, user]. + // Slicing either side could lead to obtaining semantically incorrect fragments (e.g., only the split-out system message). + // 3) The “previous assistant message” is not necessarily the second-to-last one: There might be system messages or other message blocks inserted in between, + // making a simple slice(-2) assumption too rigid. Here, we trace back from the end of the original messages to locate the most recent assistant message, which better aligns with business semantics. + // 4) This is a “collapse” rather than a simple “slice”: Ultimately, we need to synthesize a new user message + // (with text from the last user message and images from the previous assistant message). Using slice can only extract subarrays, + // which still require reassembly; constructing directly according to the target structure is clearer and more reliable. + if (isImageEnhancementModel(model)) { + // Collect all system messages (including ones generated from file uploads) + const systemMessages = sdkMessages.filter((m): m is SystemModelMessage => m.role === 'system') + + // Find the last user message (SDK converted) + const lastUserSdkIndex = (() => { + for (let i = sdkMessages.length - 1; i >= 0; i--) { + if (sdkMessages[i].role === 'user') return i + } + return -1 + })() + + const lastUserSdk = lastUserSdkIndex >= 0 ? (sdkMessages[lastUserSdkIndex] as UserModelMessage) : null + + // Find the nearest preceding assistant message in original messages + let prevAssistant: Message | null = null + for (let i = messages.length - 2; i >= 0; i--) { + if (messages[i].role === 'assistant') { + prevAssistant = messages[i] + break + } } - if (systemSdkMessages.length > 0) { - return [systemSdkMessages[0], assistantSdkMessage, userSdkMessage] + + // Build the final user content parts + let finalUserParts: Array = [] + if (lastUserSdk) { + if (typeof lastUserSdk.content === 'string') { + finalUserParts.push({ type: 'text', text: lastUserSdk.content }) + } else if (Array.isArray(lastUserSdk.content)) { + finalUserParts = [...lastUserSdk.content] + } } - return [assistantSdkMessage, userSdkMessage] + + // Append images from the previous assistant message if any + if (prevAssistant) { + const imageBlocks = findImageBlocks(prevAssistant) + const imageParts = await convertImageBlockToImagePart(imageBlocks) + if (imageParts.length > 0) { + finalUserParts.push(...imageParts) + } + } + + // If we couldn't find a last user message, fall back to returning collected system messages only + if (!lastUserSdk) { + return systemMessages + } + + return [...systemMessages, { role: 'user', content: finalUserParts }] } return sdkMessages diff --git a/src/renderer/src/aiCore/prepareParams/modelCapabilities.ts b/src/renderer/src/aiCore/prepareParams/modelCapabilities.ts index b6e4b25843..4a3c3f4bbf 100644 --- a/src/renderer/src/aiCore/prepareParams/modelCapabilities.ts +++ b/src/renderer/src/aiCore/prepareParams/modelCapabilities.ts @@ -85,19 +85,6 @@ export function supportsLargeFileUpload(model: Model): boolean { }) } -/** - * 检查模型是否支持TopP - */ -export function supportsTopP(model: Model): boolean { - const provider = getProviderByModel(model) - - if (provider?.type === 'anthropic' || model?.endpoint_type === 'anthropic') { - return false - } - - return true -} - /** * 获取提供商特定的文件大小限制 */ diff --git a/src/renderer/src/aiCore/prepareParams/modelParameters.ts b/src/renderer/src/aiCore/prepareParams/modelParameters.ts index ed3f4fa210..58b4834f53 100644 --- a/src/renderer/src/aiCore/prepareParams/modelParameters.ts +++ b/src/renderer/src/aiCore/prepareParams/modelParameters.ts @@ -4,47 +4,90 @@ */ import { - isClaude45ReasoningModel, isClaudeReasoningModel, - isNotSupportTemperatureAndTopP, - isSupportedFlexServiceTier + isMaxTemperatureOneModel, + isSupportedFlexServiceTier, + isSupportedThinkingTokenClaudeModel, + isSupportTemperatureModel, + isSupportTopPModel, + isTemperatureTopPMutuallyExclusiveModel } from '@renderer/config/models' -import { getAssistantSettings } from '@renderer/services/AssistantService' +import { + DEFAULT_ASSISTANT_SETTINGS, + getAssistantSettings, + getProviderByModel +} from '@renderer/services/AssistantService' import type { Assistant, Model } from '@renderer/types' import { defaultTimeout } from '@shared/config/constant' +import { getAnthropicThinkingBudget } from '../utils/reasoning' + /** - * 获取温度参数 + * Retrieves the temperature parameter, adapting it based on assistant.settings and model capabilities. + * - Disabled for Claude reasoning models when reasoning effort is set. + * - Disabled for models that do not support temperature. + * - Disabled for Claude 4.5 reasoning models when TopP is enabled and temperature is disabled. + * Otherwise, returns the temperature value if the assistant has temperature enabled. + */ export function getTemperature(assistant: Assistant, model: Model): number | undefined { if (assistant.settings?.reasoning_effort && isClaudeReasoningModel(model)) { return undefined } + + if (!isSupportTemperatureModel(model, assistant)) { + return undefined + } + if ( - isNotSupportTemperatureAndTopP(model) || - (isClaude45ReasoningModel(model) && assistant.settings?.enableTopP && !assistant.settings?.enableTemperature) + isTemperatureTopPMutuallyExclusiveModel(model) && + assistant.settings?.enableTopP && + !assistant.settings?.enableTemperature ) { return undefined } + + return getTemperatureValue(assistant, model) +} + +function getTemperatureValue(assistant: Assistant, model: Model): number | undefined { const assistantSettings = getAssistantSettings(assistant) - return assistantSettings?.enableTemperature ? assistantSettings?.temperature : undefined + let temperature = assistantSettings?.temperature + if (temperature && isMaxTemperatureOneModel(model)) { + temperature = Math.min(1, temperature) + } + + // FIXME: assistant.settings.enableTemperature should be always a boolean value. + const enableTemperature = assistantSettings?.enableTemperature ?? DEFAULT_ASSISTANT_SETTINGS.enableTemperature + return enableTemperature ? temperature : undefined } /** - * 获取 TopP 参数 + * Retrieves the TopP parameter, adapting it based on assistant.settings and model capabilities. + * - Disabled for Claude reasoning models when reasoning effort is set. + * - Disabled for models that do not support TopP. + * - Disabled for Claude 4.5 reasoning models when temperature is explicitly enabled. + * Otherwise, returns the TopP value if the assistant has TopP enabled. */ export function getTopP(assistant: Assistant, model: Model): number | undefined { if (assistant.settings?.reasoning_effort && isClaudeReasoningModel(model)) { return undefined } - if ( - isNotSupportTemperatureAndTopP(model) || - (isClaude45ReasoningModel(model) && assistant.settings?.enableTemperature) - ) { + if (!isSupportTopPModel(model, assistant)) { return undefined } + if (isTemperatureTopPMutuallyExclusiveModel(model) && assistant.settings?.enableTemperature) { + return undefined + } + + return getTopPValue(assistant) +} + +function getTopPValue(assistant: Assistant): number | undefined { const assistantSettings = getAssistantSettings(assistant) - return assistantSettings?.enableTopP ? assistantSettings?.topP : undefined + // FIXME: assistant.settings.enableTopP should be always a boolean value. + const enableTopP = assistantSettings.enableTopP ?? DEFAULT_ASSISTANT_SETTINGS.enableTopP + return enableTopP ? assistantSettings?.topP : undefined } /** @@ -56,3 +99,26 @@ export function getTimeout(model: Model): number { } return defaultTimeout } + +export function getMaxTokens(assistant: Assistant, model: Model): number | undefined { + // NOTE: ai-sdk会把maxToken和budgetToken加起来 + const assistantSettings = getAssistantSettings(assistant) + const enabledMaxTokens = assistantSettings.enableMaxTokens ?? false + let maxTokens = assistantSettings.maxTokens + + // If user hasn't enabled enableMaxTokens, return undefined to let the API use its default value. + // Note: Anthropic API requires max_tokens, but that's handled by the Anthropic client with a fallback. + if (!enabledMaxTokens || maxTokens === undefined) { + return undefined + } + + const provider = getProviderByModel(model) + if (isSupportedThinkingTokenClaudeModel(model) && ['anthropic', 'aws-bedrock'].includes(provider.type)) { + const { reasoning_effort: reasoningEffort } = assistantSettings + const budget = getAnthropicThinkingBudget(maxTokens, reasoningEffort, model.id) + if (budget) { + maxTokens -= budget + } + } + return maxTokens +} diff --git a/src/renderer/src/aiCore/prepareParams/parameterBuilder.ts b/src/renderer/src/aiCore/prepareParams/parameterBuilder.ts index e865f9f15f..cba7fcdb10 100644 --- a/src/renderer/src/aiCore/prepareParams/parameterBuilder.ts +++ b/src/renderer/src/aiCore/prepareParams/parameterBuilder.ts @@ -4,48 +4,69 @@ */ import { anthropic } from '@ai-sdk/anthropic' +import { azure } from '@ai-sdk/azure' import { google } from '@ai-sdk/google' import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic/edge' import { vertex } from '@ai-sdk/google-vertex/edge' import { combineHeaders } from '@ai-sdk/provider-utils' -import type { WebSearchPluginConfig } from '@cherrystudio/ai-core/built-in/plugins' +import type { AnthropicSearchConfig, WebSearchPluginConfig } from '@cherrystudio/ai-core/built-in/plugins' import { isBaseProvider } from '@cherrystudio/ai-core/core/providers/schemas' +import type { BaseProviderId } from '@cherrystudio/ai-core/provider' import { loggerService } from '@logger' import { isAnthropicModel, + isFixedReasoningModel, + isGeminiModel, isGenerateImageModel, + isGrokModel, + isOpenAIModel, isOpenRouterBuiltInWebSearchModel, - isReasoningModel, isSupportedReasoningEffortModel, - isSupportedThinkingTokenClaudeModel, isSupportedThinkingTokenModel, isWebSearchModel } from '@renderer/config/models' -import { isAwsBedrockProvider } from '@renderer/config/providers' -import { isVertexProvider } from '@renderer/hooks/useVertexAI' -import { getAssistantSettings, getDefaultModel } from '@renderer/services/AssistantService' +import { getDefaultModel } from '@renderer/services/AssistantService' import store from '@renderer/store' import type { CherryWebSearchConfig } from '@renderer/store/websearch' -import { type Assistant, type MCPTool, type Provider } from '@renderer/types' +import type { Model } from '@renderer/types' +import { type Assistant, type MCPTool, type Provider, SystemProviderIds } from '@renderer/types' import type { StreamTextParams } from '@renderer/types/aiCoreTypes' import { mapRegexToPatterns } from '@renderer/utils/blacklistMatchPattern' import { replacePromptVariables } from '@renderer/utils/prompt' +import { isAIGatewayProvider, isAwsBedrockProvider } from '@renderer/utils/provider' import type { ModelMessage, Tool } from 'ai' import { stepCountIs } from 'ai' import { getAiSdkProviderId } from '../provider/factory' import { setupToolsConfig } from '../utils/mcp' import { buildProviderOptions } from '../utils/options' -import { getAnthropicThinkingBudget } from '../utils/reasoning' import { buildProviderBuiltinWebSearchConfig } from '../utils/websearch' import { addAnthropicHeaders } from './header' -import { supportsTopP } from './modelCapabilities' -import { getTemperature, getTopP } from './modelParameters' +import { getMaxTokens, getTemperature, getTopP } from './modelParameters' const logger = loggerService.withContext('parameterBuilder') type ProviderDefinedTool = Extract, { type: 'provider-defined' }> +function mapVertexAIGatewayModelToProviderId(model: Model): BaseProviderId | undefined { + if (isAnthropicModel(model)) { + return 'anthropic' + } + if (isGeminiModel(model)) { + return 'google' + } + if (isGrokModel(model)) { + return 'xai' + } + if (isOpenAIModel(model)) { + return 'openai' + } + logger.warn( + `[mapVertexAIGatewayModelToProviderId] Unknown model type for AI Gateway: ${model.id}. Web search will not be enabled.` + ) + return undefined +} + /** * 构建 AI SDK 流式参数 * 这是主要的参数构建函数,整合所有转换逻辑 @@ -63,7 +84,7 @@ export async function buildStreamTextParams( timeout?: number headers?: Record } - } = {} + } ): Promise<{ params: StreamTextParams modelId: string @@ -80,15 +101,13 @@ export async function buildStreamTextParams( const model = assistant.model || getDefaultModel() const aiSdkProviderId = getAiSdkProviderId(provider) - let { maxTokens } = getAssistantSettings(assistant) - // 这三个变量透传出来,交给下面启用插件/中间件 // 也可以在外部构建好再传入buildStreamTextParams // FIXME: qwen3即使关闭思考仍然会导致enableReasoning的结果为true const enableReasoning = ((isSupportedThinkingTokenModel(model) || isSupportedReasoningEffortModel(model)) && assistant.settings?.reasoning_effort !== undefined) || - (isReasoningModel(model) && (!isSupportedThinkingTokenModel(model) || !isSupportedReasoningEffortModel(model))) + isFixedReasoningModel(model) // 判断是否使用内置搜索 // 条件:没有外部搜索提供商 && (用户开启了内置搜索 || 模型强制使用内置搜索) @@ -112,26 +131,21 @@ export async function buildStreamTextParams( searchWithTime: store.getState().websearch.searchWithTime } - const providerOptions = buildProviderOptions(assistant, model, provider, { + const { providerOptions, standardParams } = buildProviderOptions(assistant, model, provider, { enableReasoning, enableWebSearch, enableGenerateImage }) - // NOTE: ai-sdk会把maxToken和budgetToken加起来 - if ( - enableReasoning && - maxTokens !== undefined && - isSupportedThinkingTokenClaudeModel(model) && - (provider.type === 'anthropic' || provider.type === 'aws-bedrock') - ) { - maxTokens -= getAnthropicThinkingBudget(assistant, model) - } - let webSearchPluginConfig: WebSearchPluginConfig | undefined = undefined if (enableWebSearch) { if (isBaseProvider(aiSdkProviderId)) { webSearchPluginConfig = buildProviderBuiltinWebSearchConfig(aiSdkProviderId, webSearchConfig, model) + } else if (isAIGatewayProvider(provider) || SystemProviderIds.gateway === provider.id) { + const aiSdkProviderId = mapVertexAIGatewayModelToProviderId(model) + if (aiSdkProviderId) { + webSearchPluginConfig = buildProviderBuiltinWebSearchConfig(aiSdkProviderId, webSearchConfig, model) + } } if (!tools) { tools = {} @@ -144,6 +158,17 @@ export async function buildStreamTextParams( maxUses: webSearchConfig.maxResults, blockedDomains: blockedDomains.length > 0 ? blockedDomains : undefined }) as ProviderDefinedTool + } else if (aiSdkProviderId === 'azure-responses') { + tools.web_search_preview = azure.tools.webSearchPreview({ + searchContextSize: webSearchPluginConfig?.openai!.searchContextSize + }) as ProviderDefinedTool + } else if (aiSdkProviderId === 'azure-anthropic') { + const blockedDomains = mapRegexToPatterns(webSearchConfig.excludeDomains) + const anthropicSearchOptions: AnthropicSearchConfig = { + maxUses: webSearchConfig.maxResults, + blockedDomains: blockedDomains.length > 0 ? blockedDomains : undefined + } + tools.web_search = anthropic.tools.webSearch_20250305(anthropicSearchOptions) as ProviderDefinedTool } } @@ -161,9 +186,10 @@ export async function buildStreamTextParams( tools.url_context = google.tools.urlContext({}) as ProviderDefinedTool break case 'anthropic': + case 'azure-anthropic': case 'google-vertex-anthropic': tools.web_fetch = ( - aiSdkProviderId === 'anthropic' + ['anthropic', 'azure-anthropic'].includes(aiSdkProviderId) ? anthropic.tools.webFetch_20250910({ maxUses: webSearchConfig.maxResults, blockedDomains: blockedDomains.length > 0 ? blockedDomains : undefined @@ -179,17 +205,26 @@ export async function buildStreamTextParams( let headers: Record = options.requestOptions?.headers ?? {} - // https://docs.claude.com/en/docs/build-with-claude/extended-thinking#interleaved-thinking - if (!isVertexProvider(provider) && !isAwsBedrockProvider(provider) && isAnthropicModel(model)) { - const newBetaHeaders = { 'anthropic-beta': addAnthropicHeaders(assistant, model).join(',') } - headers = combineHeaders(headers, newBetaHeaders) + if (isAnthropicModel(model) && !isAwsBedrockProvider(provider)) { + const betaHeaders = addAnthropicHeaders(assistant, model) + // Only add the anthropic-beta header if there are actual beta headers to include + if (betaHeaders.length > 0) { + const newBetaHeaders = { 'anthropic-beta': betaHeaders.join(',') } + headers = combineHeaders(headers, newBetaHeaders) + } } // 构建基础参数 + // Note: standardParams (topK, frequencyPenalty, presencePenalty, stopSequences, seed) + // are extracted from custom parameters and passed directly to streamText() + // instead of being placed in providerOptions const params: StreamTextParams = { messages: sdkMessages, - maxOutputTokens: maxTokens, + maxOutputTokens: getMaxTokens(assistant, model), temperature: getTemperature(assistant, model), + topP: getTopP(assistant, model), + // Include AI SDK standard params extracted from custom parameters + ...standardParams, abortSignal: options.requestOptions?.signal, headers, providerOptions, @@ -197,10 +232,6 @@ export async function buildStreamTextParams( maxRetries: 0 } - if (supportsTopP(model)) { - params.topP = getTopP(assistant, model) - } - if (tools) { params.tools = tools } diff --git a/src/renderer/src/aiCore/provider/__tests__/integratedRegistry.test.ts b/src/renderer/src/aiCore/provider/__tests__/integratedRegistry.test.ts index e26597e2d1..9b2c0639e2 100644 --- a/src/renderer/src/aiCore/provider/__tests__/integratedRegistry.test.ts +++ b/src/renderer/src/aiCore/provider/__tests__/integratedRegistry.test.ts @@ -1,4 +1,4 @@ -import type { Provider } from '@renderer/types' +import type { Model, Provider } from '@renderer/types' import { describe, expect, it, vi } from 'vitest' import { getAiSdkProviderId } from '../factory' @@ -23,6 +23,26 @@ vi.mock('@cherrystudio/ai-core', () => ({ } })) +vi.mock('@renderer/services/AssistantService', () => ({ + getProviderByModel: vi.fn(), + getAssistantSettings: vi.fn(), + getDefaultAssistant: vi.fn().mockReturnValue({ + id: 'default', + name: 'Default Assistant', + prompt: '', + settings: {} + }) +})) + +vi.mock('@renderer/store/settings', () => ({ + default: {}, + settingsSlice: { + name: 'settings', + reducer: vi.fn(), + actions: {} + } +})) + // Mock the provider configs vi.mock('../providerConfigs', () => ({ initializeNewProviders: vi.fn() @@ -48,6 +68,18 @@ function createTestProvider(id: string, type: string): Provider { } as Provider } +function createAzureProvider(id: string, apiVersion?: string, model?: string): Provider { + return { + id, + type: 'azure-openai', + name: `Azure Test ${id}`, + apiKey: 'azure-test-key', + apiHost: 'azure-test-host', + apiVersion, + models: [{ id: model || 'gpt-4' } as Model] + } +} + describe('Integrated Provider Registry', () => { describe('Provider ID Resolution', () => { it('should resolve openrouter provider correctly', () => { @@ -91,6 +123,24 @@ describe('Integrated Provider Registry', () => { const result = getAiSdkProviderId(unknownProvider) expect(result).toBe('unknown-provider') }) + + it('should handle Azure OpenAI providers correctly', () => { + const azureProvider = createAzureProvider('azure-test', '2024-02-15', 'gpt-4o') + const result = getAiSdkProviderId(azureProvider) + expect(result).toBe('azure') + }) + + it('should handle Azure OpenAI providers response endpoint correctly', () => { + const azureProvider = createAzureProvider('azure-test', 'v1', 'gpt-4o') + const result = getAiSdkProviderId(azureProvider) + expect(result).toBe('azure-responses') + }) + + it('should handle Azure provider Claude Models', () => { + const provider = createTestProvider('azure-anthropic', 'anthropic') + const result = getAiSdkProviderId(provider) + expect(result).toBe('azure-anthropic') + }) }) describe('Backward Compatibility', () => { diff --git a/src/renderer/src/aiCore/provider/__tests__/providerConfig.test.ts b/src/renderer/src/aiCore/provider/__tests__/providerConfig.test.ts index 39786231e6..20aa78dcbd 100644 --- a/src/renderer/src/aiCore/provider/__tests__/providerConfig.test.ts +++ b/src/renderer/src/aiCore/provider/__tests__/providerConfig.test.ts @@ -12,14 +12,25 @@ vi.mock('@renderer/services/LoggerService', () => ({ })) vi.mock('@renderer/services/AssistantService', () => ({ - getProviderByModel: vi.fn() + getProviderByModel: vi.fn(), + getAssistantSettings: vi.fn(), + getDefaultAssistant: vi.fn().mockReturnValue({ + id: 'default', + name: 'Default Assistant', + prompt: '', + settings: {} + }) })) -vi.mock('@renderer/store', () => ({ - default: { - getState: () => ({ copilot: { defaultHeaders: {} } }) +vi.mock('@renderer/store', () => { + const mockGetState = vi.fn() + return { + default: { + getState: mockGetState + }, + __mockGetState: mockGetState } -})) +}) vi.mock('@renderer/utils/api', () => ({ formatApiHost: vi.fn((host, isSupportedAPIVersion = true) => { @@ -31,10 +42,11 @@ vi.mock('@renderer/utils/api', () => ({ routeToEndpoint: vi.fn((host) => ({ baseURL: host, endpoint: '/chat/completions' - })) + })), + isWithTrailingSharp: vi.fn((host) => host?.endsWith('#') || false) })) -vi.mock('@renderer/config/providers', async (importOriginal) => { +vi.mock('@renderer/utils/provider', async (importOriginal) => { const actual = (await importOriginal()) as any return { ...actual, @@ -53,14 +65,27 @@ vi.mock('@renderer/hooks/useVertexAI', () => ({ createVertexProvider: vi.fn() })) -import { isCherryAIProvider, isPerplexityProvider } from '@renderer/config/providers' +vi.mock('@renderer/services/AssistantService', () => ({ + getProviderByModel: vi.fn(), + getAssistantSettings: vi.fn(), + getDefaultAssistant: vi.fn().mockReturnValue({ + id: 'default', + name: 'Default Assistant', + prompt: '', + settings: {} + }) +})) + import { getProviderByModel } from '@renderer/services/AssistantService' import type { Model, Provider } from '@renderer/types' import { formatApiHost } from '@renderer/utils/api' +import { isCherryAIProvider, isPerplexityProvider } from '@renderer/utils/provider' import { COPILOT_DEFAULT_HEADERS, COPILOT_EDITOR_VERSION, isCopilotResponsesModel } from '../constants' import { getActualProvider, providerToAiSdkConfig } from '../providerConfig' +const { __mockGetState: mockGetState } = vi.mocked(await import('@renderer/store')) as any + const createWindowKeyv = () => { const store = new Map() return { @@ -114,6 +139,16 @@ describe('Copilot responses routing', () => { ...(globalThis as any).window, keyv: createWindowKeyv() } + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: undefined + } + } + } + }) }) it('detects official GPT-5 Codex identifiers case-insensitively', () => { @@ -149,6 +184,16 @@ describe('CherryAI provider configuration', () => { ...(globalThis as any).window, keyv: createWindowKeyv() } + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: undefined + } + } + } + }) vi.clearAllMocks() }) @@ -183,12 +228,19 @@ describe('CherryAI provider configuration', () => { // Mock the functions to simulate non-CherryAI provider vi.mocked(isCherryAIProvider).mockReturnValue(false) vi.mocked(getProviderByModel).mockReturnValue(provider) + // Mock isWithTrailingSharp to return false for this test + vi.mocked(formatApiHost as any).mockImplementation((host, isSupportedAPIVersion = true) => { + if (isSupportedAPIVersion === false) { + return host + } + return `${host}/v1` + }) // Call getActualProvider const actualProvider = getActualProvider(model) - // Verify that formatApiHost was called with default parameters (true) - expect(formatApiHost).toHaveBeenCalledWith('https://api.openai.com') + // Verify that formatApiHost was called with appendApiVersion parameter + expect(formatApiHost).toHaveBeenCalledWith('https://api.openai.com', true) expect(actualProvider.apiHost).toBe('https://api.openai.com/v1') }) @@ -213,6 +265,16 @@ describe('Perplexity provider configuration', () => { ...(globalThis as any).window, keyv: createWindowKeyv() } + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: undefined + } + } + } + }) vi.clearAllMocks() }) @@ -249,12 +311,19 @@ describe('Perplexity provider configuration', () => { vi.mocked(isCherryAIProvider).mockReturnValue(false) vi.mocked(isPerplexityProvider).mockReturnValue(false) vi.mocked(getProviderByModel).mockReturnValue(provider) + // Mock isWithTrailingSharp to return false for this test + vi.mocked(formatApiHost as any).mockImplementation((host, isSupportedAPIVersion = true) => { + if (isSupportedAPIVersion === false) { + return host + } + return `${host}/v1` + }) // Call getActualProvider const actualProvider = getActualProvider(model) - // Verify that formatApiHost was called with default parameters (true) - expect(formatApiHost).toHaveBeenCalledWith('https://api.openai.com') + // Verify that formatApiHost was called with appendApiVersion parameter + expect(formatApiHost).toHaveBeenCalledWith('https://api.openai.com', true) expect(actualProvider.apiHost).toBe('https://api.openai.com/v1') }) @@ -273,3 +342,165 @@ describe('Perplexity provider configuration', () => { expect(actualProvider.apiHost).toBe('') }) }) + +describe('Stream options includeUsage configuration', () => { + beforeEach(() => { + ;(globalThis as any).window = { + ...(globalThis as any).window, + keyv: createWindowKeyv() + } + vi.clearAllMocks() + }) + + const createOpenAIProvider = (): Provider => ({ + id: 'openai-compatible', + type: 'openai', + name: 'OpenAI', + apiKey: 'test-key', + apiHost: 'https://api.openai.com', + models: [], + isSystem: true + }) + + it('uses includeUsage from settings when undefined', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: undefined + } + } + } + }) + + const provider = createOpenAIProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'openai')) + + expect(config.options.includeUsage).toBeUndefined() + }) + + it('uses includeUsage from settings when set to true', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: true + } + } + } + }) + + const provider = createOpenAIProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'openai')) + + expect(config.options.includeUsage).toBe(true) + }) + + it('uses includeUsage from settings when set to false', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: false + } + } + } + }) + + const provider = createOpenAIProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'openai')) + + expect(config.options.includeUsage).toBe(false) + }) + + it('respects includeUsage setting for non-supporting providers', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: true + } + } + } + }) + + const testProvider: Provider = { + id: 'test', + type: 'openai', + name: 'test', + apiKey: 'test-key', + apiHost: 'https://api.test.com', + models: [], + isSystem: false, + apiOptions: { + isNotSupportStreamOptions: true + } + } + + const config = providerToAiSdkConfig(testProvider, createModel('gpt-4', 'GPT-4', 'test')) + + // Even though setting is true, provider doesn't support it, so includeUsage should be undefined + expect(config.options.includeUsage).toBeUndefined() + }) + + it('uses includeUsage from settings for Copilot provider when set to false', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: false + } + } + } + }) + + const provider = createCopilotProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'copilot')) + + expect(config.options.includeUsage).toBe(false) + expect(config.providerId).toBe('github-copilot-openai-compatible') + }) + + it('uses includeUsage from settings for Copilot provider when set to true', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: true + } + } + } + }) + + const provider = createCopilotProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'copilot')) + + expect(config.options.includeUsage).toBe(true) + expect(config.providerId).toBe('github-copilot-openai-compatible') + }) + + it('uses includeUsage from settings for Copilot provider when undefined', () => { + mockGetState.mockReturnValue({ + copilot: { defaultHeaders: {} }, + settings: { + openAI: { + streamOptions: { + includeUsage: undefined + } + } + } + }) + + const provider = createCopilotProvider() + const config = providerToAiSdkConfig(provider, createModel('gpt-4', 'GPT-4', 'copilot')) + + expect(config.options.includeUsage).toBeUndefined() + expect(config.providerId).toBe('github-copilot-openai-compatible') + }) +}) diff --git a/src/renderer/src/aiCore/provider/config/azure-anthropic.ts b/src/renderer/src/aiCore/provider/config/azure-anthropic.ts new file mode 100644 index 0000000000..c6cb521386 --- /dev/null +++ b/src/renderer/src/aiCore/provider/config/azure-anthropic.ts @@ -0,0 +1,22 @@ +import type { Provider } from '@renderer/types' + +import { provider2Provider, startsWith } from './helper' +import type { RuleSet } from './types' + +// https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry +const AZURE_ANTHROPIC_RULES: RuleSet = { + rules: [ + { + match: startsWith('claude'), + provider: (provider: Provider) => ({ + ...provider, + type: 'anthropic', + apiHost: provider.apiHost + 'anthropic/v1', + id: 'azure-anthropic' + }) + } + ], + fallbackRule: (provider: Provider) => provider +} + +export const azureAnthropicProviderCreator = provider2Provider.bind(null, AZURE_ANTHROPIC_RULES) diff --git a/src/renderer/src/aiCore/provider/factory.ts b/src/renderer/src/aiCore/provider/factory.ts index 569b5628cd..ff100051b7 100644 --- a/src/renderer/src/aiCore/provider/factory.ts +++ b/src/renderer/src/aiCore/provider/factory.ts @@ -2,8 +2,10 @@ import { hasProviderConfigByAlias, type ProviderId, resolveProviderConfigId } fr import { createProvider as createProviderCore } from '@cherrystudio/ai-core/provider' import { loggerService } from '@logger' import type { Provider } from '@renderer/types' +import { isAzureOpenAIProvider, isAzureResponsesEndpoint } from '@renderer/utils/provider' import type { Provider as AiSdkProvider } from 'ai' +import type { AiSdkConfig } from '../types' import { initializeNewProviders } from './providerInitialization' const logger = loggerService.withContext('ProviderFactory') @@ -54,10 +56,18 @@ function tryResolveProviderId(identifier: string): ProviderId | null { /** * 获取AI SDK Provider ID * 简化版:减少重复逻辑,利用通用解析函数 + * TODO: 整理函数逻辑 */ -export function getAiSdkProviderId(provider: Provider): ProviderId | 'openai-compatible' { +export function getAiSdkProviderId(provider: Provider): string { // 1. 尝试解析provider.id const resolvedFromId = tryResolveProviderId(provider.id) + if (isAzureOpenAIProvider(provider)) { + if (isAzureResponsesEndpoint(provider)) { + return 'azure-responses' + } else { + return 'azure' + } + } if (resolvedFromId) { return resolvedFromId } @@ -73,11 +83,11 @@ export function getAiSdkProviderId(provider: Provider): ProviderId | 'openai-com if (provider.apiHost.includes('api.openai.com')) { return 'openai-chat' } - // 3. 最后的fallback(通常会成为openai-compatible) - return provider.id as ProviderId + // 3. 最后的fallback(使用provider本身的id) + return provider.id } -export async function createAiSdkProvider(config) { +export async function createAiSdkProvider(config: AiSdkConfig): Promise { let localProvider: Awaited | null = null try { if (config.providerId === 'openai' && config.options?.mode === 'chat') { diff --git a/src/renderer/src/aiCore/provider/providerConfig.ts b/src/renderer/src/aiCore/provider/providerConfig.ts index 07b4ceaa7d..1c410bf124 100644 --- a/src/renderer/src/aiCore/provider/providerConfig.ts +++ b/src/renderer/src/aiCore/provider/providerConfig.ts @@ -1,19 +1,5 @@ -import { - formatPrivateKey, - hasProviderConfig, - ProviderConfigFactory, - type ProviderId, - type ProviderSettingsMap -} from '@cherrystudio/ai-core/provider' +import { formatPrivateKey, hasProviderConfig, ProviderConfigFactory } from '@cherrystudio/ai-core/provider' import { isOpenAIChatCompletionOnlyModel } from '@renderer/config/models' -import { - isAnthropicProvider, - isAzureOpenAIProvider, - isCherryAIProvider, - isGeminiProvider, - isNewApiProvider, - isPerplexityProvider -} from '@renderer/config/providers' import { getAwsBedrockAccessKeyId, getAwsBedrockApiKey, @@ -21,43 +7,39 @@ import { getAwsBedrockRegion, getAwsBedrockSecretAccessKey } from '@renderer/hooks/useAwsBedrock' -import { createVertexProvider, isVertexAIConfigured, isVertexProvider } from '@renderer/hooks/useVertexAI' +import { createVertexProvider, isVertexAIConfigured } from '@renderer/hooks/useVertexAI' import { getProviderByModel } from '@renderer/services/AssistantService' +import { getProviderById } from '@renderer/services/ProviderService' import store from '@renderer/store' import { isSystemProvider, type Model, type Provider, SystemProviderIds } from '@renderer/types' -import { formatApiHost, formatAzureOpenAIApiHost, formatVertexApiHost, routeToEndpoint } from '@renderer/utils/api' -import { cloneDeep } from 'lodash' +import type { OpenAICompletionsStreamOptions } from '@renderer/types/aiCoreTypes' +import { + formatApiHost, + formatAzureOpenAIApiHost, + formatOllamaApiHost, + formatVertexApiHost, + isWithTrailingSharp, + routeToEndpoint +} from '@renderer/utils/api' +import { + isAnthropicProvider, + isAzureOpenAIProvider, + isCherryAIProvider, + isGeminiProvider, + isNewApiProvider, + isOllamaProvider, + isPerplexityProvider, + isSupportStreamOptionsProvider, + isVertexProvider +} from '@renderer/utils/provider' +import { cloneDeep, isEmpty } from 'lodash' +import type { AiSdkConfig } from '../types' import { aihubmixProviderCreator, newApiResolverCreator, vertexAnthropicProviderCreator } from './config' +import { azureAnthropicProviderCreator } from './config/azure-anthropic' import { COPILOT_DEFAULT_HEADERS } from './constants' import { getAiSdkProviderId } from './factory' -/** - * 获取轮询的API key - * 复用legacy架构的多key轮询逻辑 - */ -function getRotatedApiKey(provider: Provider): string { - const keys = provider.apiKey.split(',').map((key) => key.trim()) - const keyName = `provider:${provider.id}:last_used_key` - - if (keys.length === 1) { - return keys[0] - } - - const lastUsedKey = window.keyv.get(keyName) - if (!lastUsedKey) { - window.keyv.set(keyName, keys[0]) - return keys[0] - } - - const currentIndex = keys.indexOf(lastUsedKey) - const nextIndex = (currentIndex + 1) % keys.length - const nextKey = keys[nextIndex] - window.keyv.set(keyName, nextKey) - - return nextKey -} - /** * 处理特殊provider的转换逻辑 */ @@ -74,30 +56,39 @@ function handleSpecialProviders(model: Model, provider: Provider): Provider { return vertexAnthropicProviderCreator(model, provider) } } + if (isAzureOpenAIProvider(provider)) { + return azureAnthropicProviderCreator(model, provider) + } return provider } /** - * 主要用来对齐AISdk的BaseURL格式 - * @param provider - * @returns + * Format and normalize the API host URL for a provider. + * Handles provider-specific URL formatting rules (e.g., appending version paths, Azure formatting). + * + * @param provider - The provider whose API host is to be formatted. + * @returns A new provider instance with the formatted API host. */ -function formatProviderApiHost(provider: Provider): Provider { +export function formatProviderApiHost(provider: Provider): Provider { const formatted = { ...provider } + const appendApiVersion = !isWithTrailingSharp(provider.apiHost) if (formatted.anthropicApiHost) { - formatted.anthropicApiHost = formatApiHost(formatted.anthropicApiHost) + formatted.anthropicApiHost = formatApiHost(formatted.anthropicApiHost, appendApiVersion) } if (isAnthropicProvider(provider)) { const baseHost = formatted.anthropicApiHost || formatted.apiHost - formatted.apiHost = formatApiHost(baseHost) + // AI SDK needs /v1 in baseURL, Anthropic SDK will strip it in getSdkClient + formatted.apiHost = formatApiHost(baseHost, appendApiVersion) if (!formatted.anthropicApiHost) { formatted.anthropicApiHost = formatted.apiHost } } else if (formatted.id === SystemProviderIds.copilot || formatted.id === SystemProviderIds.github) { formatted.apiHost = formatApiHost(formatted.apiHost, false) + } else if (isOllamaProvider(formatted)) { + formatted.apiHost = formatOllamaApiHost(formatted.apiHost) } else if (isGeminiProvider(formatted)) { - formatted.apiHost = formatApiHost(formatted.apiHost, true, 'v1beta') + formatted.apiHost = formatApiHost(formatted.apiHost, appendApiVersion, 'v1beta') } else if (isAzureOpenAIProvider(formatted)) { formatted.apiHost = formatAzureOpenAIApiHost(formatted.apiHost) } else if (isVertexProvider(formatted)) { @@ -107,44 +98,62 @@ function formatProviderApiHost(provider: Provider): Provider { } else if (isPerplexityProvider(formatted)) { formatted.apiHost = formatApiHost(formatted.apiHost, false) } else { - formatted.apiHost = formatApiHost(formatted.apiHost) + formatted.apiHost = formatApiHost(formatted.apiHost, appendApiVersion) } return formatted } /** - * 获取实际的Provider配置 - * 简化版:将逻辑分解为小函数 + * Retrieve the effective Provider configuration for the given model. + * Applies all necessary transformations (special-provider handling, URL formatting, etc.). + * + * @param model - The model whose provider is to be resolved. + * @returns A new Provider instance with all adaptations applied. */ export function getActualProvider(model: Model): Provider { const baseProvider = getProviderByModel(model) - // 按顺序处理各种转换 - let actualProvider = cloneDeep(baseProvider) - actualProvider = handleSpecialProviders(model, actualProvider) - actualProvider = formatProviderApiHost(actualProvider) + return adaptProvider({ provider: baseProvider, model }) +} - return actualProvider +/** + * Transforms a provider configuration by applying model-specific adaptations and normalizing its API host. + * The transformations are applied in the following order: + * 1. Model-specific provider handling (e.g., New-API, system providers, Azure OpenAI) + * 2. API host formatting (provider-specific URL normalization) + * + * @param provider - The base provider configuration to transform. + * @param model - The model associated with the provider; optional but required for special-provider handling. + * @returns A new Provider instance with all transformations applied. + */ +export function adaptProvider({ provider, model }: { provider: Provider; model?: Model }): Provider { + let adaptedProvider = cloneDeep(provider) + + // Apply transformations in order + if (model) { + adaptedProvider = handleSpecialProviders(model, adaptedProvider) + } + adaptedProvider = formatProviderApiHost(adaptedProvider) + + return adaptedProvider } /** * 将 Provider 配置转换为新 AI SDK 格式 * 简化版:利用新的别名映射系统 */ -export function providerToAiSdkConfig( - actualProvider: Provider, - model: Model -): { - providerId: ProviderId | 'openai-compatible' - options: ProviderSettingsMap[keyof ProviderSettingsMap] -} { +export function providerToAiSdkConfig(actualProvider: Provider, model: Model): AiSdkConfig { const aiSdkProviderId = getAiSdkProviderId(actualProvider) // 构建基础配置 const { baseURL, endpoint } = routeToEndpoint(actualProvider.apiHost) const baseConfig = { baseURL: baseURL, - apiKey: getRotatedApiKey(actualProvider) + apiKey: actualProvider.apiKey + } + let includeUsage: OpenAICompletionsStreamOptions['include_usage'] = undefined + if (isSupportStreamOptionsProvider(actualProvider)) { + includeUsage = store.getState().settings.openAI?.streamOptions?.includeUsage } const isCopilotProvider = actualProvider.id === SystemProviderIds.copilot @@ -157,7 +166,7 @@ export function providerToAiSdkConfig( ...actualProvider.extra_headers }, name: actualProvider.id, - includeUsage: true + includeUsage }) return { @@ -166,6 +175,19 @@ export function providerToAiSdkConfig( } } + if (isOllamaProvider(actualProvider)) { + return { + providerId: 'ollama', + options: { + ...baseConfig, + headers: { + ...actualProvider.extra_headers, + Authorization: !isEmpty(baseConfig.apiKey) ? `Bearer ${baseConfig.apiKey}` : undefined + } + } + } + } + // 处理OpenAI模式 const extraOptions: any = {} extraOptions.endpoint = endpoint @@ -191,13 +213,10 @@ export function providerToAiSdkConfig( // azure // https://learn.microsoft.com/en-us/azure/ai-foundry/openai/latest // https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses?tabs=python-key#responses-api - if (aiSdkProviderId === 'azure' || actualProvider.type === 'azure-openai') { - // extraOptions.apiVersion = actualProvider.apiVersion === 'preview' ? 'v1' : actualProvider.apiVersion 默认使用v1,不使用azure endpoint - if (actualProvider.apiVersion === 'preview' || actualProvider.apiVersion === 'v1') { - extraOptions.mode = 'responses' - } else { - extraOptions.mode = 'chat' - } + if (aiSdkProviderId === 'azure-responses') { + extraOptions.mode = 'responses' + } else if (aiSdkProviderId === 'azure') { + extraOptions.mode = 'chat' } // bedrock @@ -227,10 +246,23 @@ export function providerToAiSdkConfig( baseConfig.baseURL += aiSdkProviderId === 'google-vertex' ? '/publishers/google' : '/publishers/anthropic/models' } + // cherryin + if (aiSdkProviderId === 'cherryin') { + if (model.endpoint_type) { + extraOptions.endpointType = model.endpoint_type + } + // CherryIN API Host + const cherryinProvider = getProviderById(SystemProviderIds.cherryin) + if (cherryinProvider) { + extraOptions.anthropicBaseURL = cherryinProvider.anthropicApiHost + '/v1' + extraOptions.geminiBaseURL = cherryinProvider.apiHost + '/v1beta/models' + } + } + if (hasProviderConfig(aiSdkProviderId) && aiSdkProviderId !== 'openai-compatible') { const options = ProviderConfigFactory.fromProvider(aiSdkProviderId, baseConfig, extraOptions) return { - providerId: aiSdkProviderId as ProviderId, + providerId: aiSdkProviderId, options } } @@ -243,7 +275,7 @@ export function providerToAiSdkConfig( ...options, name: actualProvider.id, ...extraOptions, - includeUsage: true + includeUsage } } } @@ -315,7 +347,6 @@ export async function prepareSpecialProviderConfig( ...(config.options.headers ? config.options.headers : {}), 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01', - 'anthropic-beta': 'oauth-2025-04-20', Authorization: `Bearer ${oauthToken}` }, baseURL: 'https://api.anthropic.com/v1', diff --git a/src/renderer/src/aiCore/provider/providerInitialization.ts b/src/renderer/src/aiCore/provider/providerInitialization.ts index baf400508a..51176c1e60 100644 --- a/src/renderer/src/aiCore/provider/providerInitialization.ts +++ b/src/renderer/src/aiCore/provider/providerInitialization.ts @@ -1,5 +1,6 @@ import { type ProviderConfig, registerMultipleProviderConfigs } from '@cherrystudio/ai-core/provider' import { loggerService } from '@logger' +import * as z from 'zod' const logger = loggerService.withContext('ProviderConfigs') @@ -32,6 +33,14 @@ export const NEW_PROVIDER_CONFIGS: ProviderConfig[] = [ supportsImageGeneration: true, aliases: ['vertexai-anthropic'] }, + { + id: 'azure-anthropic', + name: 'Azure AI Anthropic', + import: () => import('@ai-sdk/anthropic'), + creatorFunctionName: 'createAnthropic', + supportsImageGeneration: false, + aliases: ['azure-anthropic'] + }, { id: 'github-copilot-openai-compatible', name: 'GitHub Copilot OpenAI Compatible', @@ -73,12 +82,12 @@ export const NEW_PROVIDER_CONFIGS: ProviderConfig[] = [ aliases: ['hf', 'hugging-face'] }, { - id: 'ai-gateway', - name: 'AI Gateway', + id: 'gateway', + name: 'Vercel AI Gateway', import: () => import('@ai-sdk/gateway'), creatorFunctionName: 'createGateway', supportsImageGeneration: true, - aliases: ['gateway'] + aliases: ['ai-gateway'] }, { id: 'cerebras', @@ -86,9 +95,19 @@ export const NEW_PROVIDER_CONFIGS: ProviderConfig[] = [ import: () => import('@ai-sdk/cerebras'), creatorFunctionName: 'createCerebras', supportsImageGeneration: false + }, + { + id: 'ollama', + name: 'Ollama', + import: () => import('ollama-ai-provider-v2'), + creatorFunctionName: 'createOllama', + supportsImageGeneration: false } ] as const +export const registeredNewProviderIds = NEW_PROVIDER_CONFIGS.map((config) => config.id) +export const registeredNewProviderIdSchema = z.enum(registeredNewProviderIds) + /** * 初始化新的Providers * 使用aiCore的动态注册功能 diff --git a/src/renderer/src/aiCore/trace/AiSdkSpanAdapter.ts b/src/renderer/src/aiCore/trace/AiSdkSpanAdapter.ts index 732397de40..0c0e08a03d 100644 --- a/src/renderer/src/aiCore/trace/AiSdkSpanAdapter.ts +++ b/src/renderer/src/aiCore/trace/AiSdkSpanAdapter.ts @@ -133,7 +133,7 @@ export class AiSdkSpanAdapter { // 详细记录转换过程 const operationId = attributes['ai.operationId'] - logger.info('Converting AI SDK span to SpanEntity', { + logger.debug('Converting AI SDK span to SpanEntity', { spanName: spanName, operationId, spanTag, @@ -149,7 +149,7 @@ export class AiSdkSpanAdapter { }) if (tokenUsage) { - logger.info('Token usage data found', { + logger.debug('Token usage data found', { spanName: spanName, operationId, usage: tokenUsage, @@ -158,7 +158,7 @@ export class AiSdkSpanAdapter { } if (inputs || outputs) { - logger.info('Input/Output data extracted', { + logger.debug('Input/Output data extracted', { spanName: spanName, operationId, hasInputs: !!inputs, @@ -170,7 +170,7 @@ export class AiSdkSpanAdapter { } if (Object.keys(typeSpecificData).length > 0) { - logger.info('Type-specific data extracted', { + logger.debug('Type-specific data extracted', { spanName: spanName, operationId, typeSpecificKeys: Object.keys(typeSpecificData), @@ -204,7 +204,7 @@ export class AiSdkSpanAdapter { modelName: modelName || this.extractModelFromAttributes(attributes) } - logger.info('AI SDK span successfully converted to SpanEntity', { + logger.debug('AI SDK span successfully converted to SpanEntity', { spanName: spanName, operationId, spanId: spanContext.spanId, @@ -245,8 +245,8 @@ export class AiSdkSpanAdapter { 'gen_ai.usage.output_tokens' ] - const completionTokens = attributes[inputsTokenKeys.find((key) => attributes[key]) || ''] - const promptTokens = attributes[outputTokenKeys.find((key) => attributes[key]) || ''] + const promptTokens = attributes[inputsTokenKeys.find((key) => attributes[key]) || ''] + const completionTokens = attributes[outputTokenKeys.find((key) => attributes[key]) || ''] if (completionTokens !== undefined || promptTokens !== undefined) { const usage: TokenUsage = { diff --git a/src/renderer/src/aiCore/trace/__tests__/AiSdkSpanAdapter.test.ts b/src/renderer/src/aiCore/trace/__tests__/AiSdkSpanAdapter.test.ts new file mode 100644 index 0000000000..4cd6241e64 --- /dev/null +++ b/src/renderer/src/aiCore/trace/__tests__/AiSdkSpanAdapter.test.ts @@ -0,0 +1,53 @@ +import type { Span } from '@opentelemetry/api' +import { SpanKind, SpanStatusCode } from '@opentelemetry/api' +import { describe, expect, it, vi } from 'vitest' + +import { AiSdkSpanAdapter } from '../AiSdkSpanAdapter' + +vi.mock('@logger', () => ({ + loggerService: { + withContext: () => ({ + debug: vi.fn(), + error: vi.fn(), + info: vi.fn(), + warn: vi.fn() + }) + } +})) + +describe('AiSdkSpanAdapter', () => { + const createMockSpan = (attributes: Record): Span => { + const span = { + spanContext: () => ({ + traceId: 'trace-id', + spanId: 'span-id' + }), + _attributes: attributes, + _events: [], + name: 'test span', + status: { code: SpanStatusCode.OK }, + kind: SpanKind.CLIENT, + startTime: [0, 0] as [number, number], + endTime: [0, 1] as [number, number], + ended: true, + parentSpanId: '', + links: [] + } + return span as unknown as Span + } + + it('maps prompt and completion usage tokens to the correct fields', () => { + const attributes = { + 'ai.usage.promptTokens': 321, + 'ai.usage.completionTokens': 654 + } + + const span = createMockSpan(attributes) + const result = AiSdkSpanAdapter.convertToSpanEntity({ span }) + + expect(result.usage).toBeDefined() + expect(result.usage?.prompt_tokens).toBe(321) + expect(result.usage?.completion_tokens).toBe(654) + expect(result.usage?.total_tokens).toBe(975) + }) +}) diff --git a/src/renderer/src/aiCore/types/index.ts b/src/renderer/src/aiCore/types/index.ts new file mode 100644 index 0000000000..a8a64cf45e --- /dev/null +++ b/src/renderer/src/aiCore/types/index.ts @@ -0,0 +1,15 @@ +/** + * This type definition file is only for renderer. + * It cannot be migrated to @renderer/types since files within it are actually being used by both main and renderer. + * If we do that, main would throw an error because it cannot import a module which imports a type from a browser-enviroment-only package. + * (ai-core package is set as browser-enviroment-only) + * + * TODO: We should separate them clearly. Keep renderer only types in renderer, and main only types in main, and shared types in shared. + */ + +import type { ProviderSettingsMap } from '@cherrystudio/ai-core/provider' + +export type AiSdkConfig = { + providerId: string + options: ProviderSettingsMap[keyof ProviderSettingsMap] +} diff --git a/src/renderer/src/aiCore/utils/__tests__/extractAiSdkStandardParams.test.ts b/src/renderer/src/aiCore/utils/__tests__/extractAiSdkStandardParams.test.ts new file mode 100644 index 0000000000..288cc2e4a5 --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/extractAiSdkStandardParams.test.ts @@ -0,0 +1,652 @@ +/** + * extractAiSdkStandardParams Unit Tests + * Tests for extracting AI SDK standard parameters from custom parameters + */ + +import { describe, expect, it, vi } from 'vitest' + +import { extractAiSdkStandardParams } from '../options' + +// Mock logger to prevent errors +vi.mock('@logger', () => ({ + loggerService: { + withContext: () => ({ + debug: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + info: vi.fn() + }) + } +})) + +// Mock settings store +vi.mock('@renderer/store/settings', () => ({ + default: (state = { settings: {} }) => state +})) + +// Mock hooks to prevent uuid errors +vi.mock('@renderer/hooks/useSettings', () => ({ + getStoreSetting: vi.fn(() => ({})) +})) + +// Mock uuid to prevent errors +vi.mock('uuid', () => ({ + v4: vi.fn(() => 'test-uuid') +})) + +// Mock AssistantService to prevent uuid errors +vi.mock('@renderer/services/AssistantService', () => ({ + getDefaultAssistant: vi.fn(() => ({ + id: 'test-assistant', + name: 'Test Assistant', + settings: {} + })), + getDefaultTopic: vi.fn(() => ({ + id: 'test-topic', + assistantId: 'test-assistant', + createdAt: new Date().toISOString() + })) +})) + +// Mock provider service +vi.mock('@renderer/services/ProviderService', () => ({ + getProviderById: vi.fn(() => ({ + id: 'test-provider', + name: 'Test Provider' + })) +})) + +// Mock config modules +vi.mock('@renderer/config/models', () => ({ + isOpenAIModel: vi.fn(() => false), + isQwenMTModel: vi.fn(() => false), + isSupportFlexServiceTierModel: vi.fn(() => false), + isSupportVerbosityModel: vi.fn(() => false), + getModelSupportedVerbosity: vi.fn(() => []) +})) + +vi.mock('@renderer/config/translate', () => ({ + mapLanguageToQwenMTModel: vi.fn() +})) + +vi.mock('@renderer/utils/provider', () => ({ + isSupportServiceTierProvider: vi.fn(() => false), + isSupportVerbosityProvider: vi.fn(() => false) +})) + +describe('extractAiSdkStandardParams', () => { + describe('Positive cases - Standard parameters extraction', () => { + it('should extract all AI SDK standard parameters', () => { + const customParams = { + maxOutputTokens: 1000, + temperature: 0.7, + topP: 0.9, + topK: 40, + presencePenalty: 0.5, + frequencyPenalty: 0.3, + stopSequences: ['STOP', 'END'], + seed: 42 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + maxOutputTokens: 1000, + temperature: 0.7, + topP: 0.9, + topK: 40, + presencePenalty: 0.5, + frequencyPenalty: 0.3, + stopSequences: ['STOP', 'END'], + seed: 42 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract single standard parameter', () => { + const customParams = { + temperature: 0.8 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.8 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract topK parameter', () => { + const customParams = { + topK: 50 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + topK: 50 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract frequencyPenalty parameter', () => { + const customParams = { + frequencyPenalty: 0.6 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + frequencyPenalty: 0.6 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract presencePenalty parameter', () => { + const customParams = { + presencePenalty: 0.4 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + presencePenalty: 0.4 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract stopSequences parameter', () => { + const customParams = { + stopSequences: ['HALT', 'TERMINATE'] + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + stopSequences: ['HALT', 'TERMINATE'] + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract seed parameter', () => { + const customParams = { + seed: 12345 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + seed: 12345 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract maxOutputTokens parameter', () => { + const customParams = { + maxOutputTokens: 2048 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + maxOutputTokens: 2048 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should extract topP parameter', () => { + const customParams = { + topP: 0.95 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + topP: 0.95 + }) + expect(result.providerParams).toStrictEqual({}) + }) + }) + + describe('Negative cases - Provider-specific parameters', () => { + it('should place all non-standard parameters in providerParams', () => { + const customParams = { + customParam: 'value', + anotherParam: 123, + thirdParam: true + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + customParam: 'value', + anotherParam: 123, + thirdParam: true + }) + }) + + it('should place single provider-specific parameter in providerParams', () => { + const customParams = { + reasoningEffort: 'high' + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + reasoningEffort: 'high' + }) + }) + + it('should place model-specific parameter in providerParams', () => { + const customParams = { + thinking: { type: 'enabled', budgetTokens: 5000 } + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + thinking: { type: 'enabled', budgetTokens: 5000 } + }) + }) + + it('should place serviceTier in providerParams', () => { + const customParams = { + serviceTier: 'auto' + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + serviceTier: 'auto' + }) + }) + + it('should place textVerbosity in providerParams', () => { + const customParams = { + textVerbosity: 'high' + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + textVerbosity: 'high' + }) + }) + }) + + describe('Mixed parameters', () => { + it('should correctly separate mixed standard and provider-specific parameters', () => { + const customParams = { + temperature: 0.7, + topK: 40, + customParam: 'custom_value', + reasoningEffort: 'medium', + frequencyPenalty: 0.5, + seed: 999 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.7, + topK: 40, + frequencyPenalty: 0.5, + seed: 999 + }) + expect(result.providerParams).toStrictEqual({ + customParam: 'custom_value', + reasoningEffort: 'medium' + }) + }) + + it('should handle complex mixed parameters with nested objects', () => { + const customParams = { + topP: 0.9, + presencePenalty: 0.3, + thinking: { type: 'enabled', budgetTokens: 5000 }, + stopSequences: ['STOP'], + serviceTier: 'auto', + maxOutputTokens: 4096 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + topP: 0.9, + presencePenalty: 0.3, + stopSequences: ['STOP'], + maxOutputTokens: 4096 + }) + expect(result.providerParams).toStrictEqual({ + thinking: { type: 'enabled', budgetTokens: 5000 }, + serviceTier: 'auto' + }) + }) + + it('should handle all standard params with some provider params', () => { + const customParams = { + maxOutputTokens: 2000, + temperature: 0.8, + topP: 0.95, + topK: 50, + presencePenalty: 0.6, + frequencyPenalty: 0.4, + stopSequences: ['END', 'DONE'], + seed: 777, + customApiParam: 'value', + anotherCustomParam: 123 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + maxOutputTokens: 2000, + temperature: 0.8, + topP: 0.95, + topK: 50, + presencePenalty: 0.6, + frequencyPenalty: 0.4, + stopSequences: ['END', 'DONE'], + seed: 777 + }) + expect(result.providerParams).toStrictEqual({ + customApiParam: 'value', + anotherCustomParam: 123 + }) + }) + }) + + describe('Edge cases', () => { + it('should handle empty object', () => { + const customParams = {} + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should handle zero values for numeric parameters', () => { + const customParams = { + temperature: 0, + topK: 0, + seed: 0 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0, + topK: 0, + seed: 0 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should handle negative values for numeric parameters', () => { + const customParams = { + presencePenalty: -0.5, + frequencyPenalty: -0.3, + seed: -1 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + presencePenalty: -0.5, + frequencyPenalty: -0.3, + seed: -1 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should handle empty arrays for stopSequences', () => { + const customParams = { + stopSequences: [] + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + stopSequences: [] + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should handle null values in mixed parameters', () => { + const customParams = { + temperature: 0.7, + customNull: null, + topK: 40 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.7, + topK: 40 + }) + expect(result.providerParams).toStrictEqual({ + customNull: null + }) + }) + + it('should handle undefined values in mixed parameters', () => { + const customParams = { + temperature: 0.7, + customUndefined: undefined, + topK: 40 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.7, + topK: 40 + }) + expect(result.providerParams).toStrictEqual({ + customUndefined: undefined + }) + }) + + it('should handle boolean values for standard parameters', () => { + const customParams = { + temperature: 0.7, + customBoolean: false, + topK: 40 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.7, + topK: 40 + }) + expect(result.providerParams).toStrictEqual({ + customBoolean: false + }) + }) + + it('should handle very large numeric values', () => { + const customParams = { + maxOutputTokens: 999999, + seed: 2147483647, + topK: 10000 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + maxOutputTokens: 999999, + seed: 2147483647, + topK: 10000 + }) + expect(result.providerParams).toStrictEqual({}) + }) + + it('should handle decimal values with high precision', () => { + const customParams = { + temperature: 0.123456789, + topP: 0.987654321, + presencePenalty: 0.111111111 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.123456789, + topP: 0.987654321, + presencePenalty: 0.111111111 + }) + expect(result.providerParams).toStrictEqual({}) + }) + }) + + describe('Case sensitivity', () => { + it('should NOT extract parameters with incorrect case - uppercase first letter', () => { + const customParams = { + Temperature: 0.7, + TopK: 40, + FrequencyPenalty: 0.5 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + Temperature: 0.7, + TopK: 40, + FrequencyPenalty: 0.5 + }) + }) + + it('should NOT extract parameters with incorrect case - all uppercase', () => { + const customParams = { + TEMPERATURE: 0.7, + TOPK: 40, + SEED: 42 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + TEMPERATURE: 0.7, + TOPK: 40, + SEED: 42 + }) + }) + + it('should NOT extract parameters with incorrect case - all lowercase', () => { + const customParams = { + maxoutputtokens: 1000, + frequencypenalty: 0.5, + stopsequences: ['STOP'] + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + maxoutputtokens: 1000, + frequencypenalty: 0.5, + stopsequences: ['STOP'] + }) + }) + + it('should correctly extract exact case match while rejecting incorrect case', () => { + const customParams = { + temperature: 0.7, + Temperature: 0.8, + TEMPERATURE: 0.9, + topK: 40, + TopK: 50 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + temperature: 0.7, + topK: 40 + }) + expect(result.providerParams).toStrictEqual({ + Temperature: 0.8, + TEMPERATURE: 0.9, + TopK: 50 + }) + }) + }) + + describe('Parameter name variations', () => { + it('should NOT extract similar but incorrect parameter names', () => { + const customParams = { + temp: 0.7, // should not match temperature + top_k: 40, // should not match topK + max_tokens: 1000, // should not match maxOutputTokens + freq_penalty: 0.5 // should not match frequencyPenalty + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + temp: 0.7, + top_k: 40, + max_tokens: 1000, + freq_penalty: 0.5 + }) + }) + + it('should NOT extract snake_case versions of standard parameters', () => { + const customParams = { + top_k: 40, + top_p: 0.9, + presence_penalty: 0.5, + frequency_penalty: 0.3, + stop_sequences: ['STOP'], + max_output_tokens: 1000 + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({}) + expect(result.providerParams).toStrictEqual({ + top_k: 40, + top_p: 0.9, + presence_penalty: 0.5, + frequency_penalty: 0.3, + stop_sequences: ['STOP'], + max_output_tokens: 1000 + }) + }) + + it('should extract exact camelCase parameters only', () => { + const customParams = { + topK: 40, // correct + top_k: 50, // incorrect + topP: 0.9, // correct + top_p: 0.8, // incorrect + frequencyPenalty: 0.5, // correct + frequency_penalty: 0.4 // incorrect + } + + const result = extractAiSdkStandardParams(customParams) + + expect(result.standardParams).toStrictEqual({ + topK: 40, + topP: 0.9, + frequencyPenalty: 0.5 + }) + expect(result.providerParams).toStrictEqual({ + top_k: 50, + top_p: 0.8, + frequency_penalty: 0.4 + }) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/image.test.ts b/src/renderer/src/aiCore/utils/__tests__/image.test.ts new file mode 100644 index 0000000000..1c5381a5ef --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/image.test.ts @@ -0,0 +1,121 @@ +/** + * image.ts Unit Tests + * Tests for Gemini image generation utilities + */ + +import type { Model, Provider } from '@renderer/types' +import { SystemProviderIds } from '@renderer/types' +import { describe, expect, it } from 'vitest' + +import { buildGeminiGenerateImageParams, isOpenRouterGeminiGenerateImageModel } from '../image' + +describe('image utils', () => { + describe('buildGeminiGenerateImageParams', () => { + it('should return correct response modalities', () => { + const result = buildGeminiGenerateImageParams() + + expect(result).toEqual({ + responseModalities: ['TEXT', 'IMAGE'] + }) + }) + + it('should return an object with responseModalities property', () => { + const result = buildGeminiGenerateImageParams() + + expect(result).toHaveProperty('responseModalities') + expect(Array.isArray(result.responseModalities)).toBe(true) + expect(result.responseModalities).toHaveLength(2) + }) + }) + + describe('isOpenRouterGeminiGenerateImageModel', () => { + const mockOpenRouterProvider: Provider = { + id: SystemProviderIds.openrouter, + name: 'OpenRouter', + apiKey: 'test-key', + apiHost: 'https://openrouter.ai/api/v1', + isSystem: true + } as Provider + + const mockOtherProvider: Provider = { + id: SystemProviderIds.openai, + name: 'OpenAI', + apiKey: 'test-key', + apiHost: 'https://api.openai.com/v1', + isSystem: true + } as Provider + + it('should return true for OpenRouter Gemini 2.5 Flash Image model', () => { + const model: Model = { + id: 'google/gemini-2.5-flash-image-preview', + name: 'Gemini 2.5 Flash Image', + provider: SystemProviderIds.openrouter + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, mockOpenRouterProvider) + expect(result).toBe(true) + }) + + it('should return false for non-Gemini model on OpenRouter', () => { + const model: Model = { + id: 'openai/gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.openrouter + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, mockOpenRouterProvider) + expect(result).toBe(false) + }) + + it('should return false for Gemini model on non-OpenRouter provider', () => { + const model: Model = { + id: 'gemini-2.5-flash-image-preview', + name: 'Gemini 2.5 Flash Image', + provider: SystemProviderIds.gemini + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, mockOtherProvider) + expect(result).toBe(false) + }) + + it('should return false for Gemini model without image suffix', () => { + const model: Model = { + id: 'google/gemini-2.5-flash', + name: 'Gemini 2.5 Flash', + provider: SystemProviderIds.openrouter + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, mockOpenRouterProvider) + expect(result).toBe(false) + }) + + it('should handle model ID with partial match', () => { + const model: Model = { + id: 'google/gemini-2.5-flash-image-generation', + name: 'Gemini Image Gen', + provider: SystemProviderIds.openrouter + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, mockOpenRouterProvider) + expect(result).toBe(true) + }) + + it('should return false for custom provider', () => { + const customProvider: Provider = { + id: 'custom-provider-123', + name: 'Custom Provider', + apiKey: 'test-key', + apiHost: 'https://custom.com' + } as Provider + + const model: Model = { + id: 'gemini-2.5-flash-image-preview', + name: 'Gemini 2.5 Flash Image', + provider: 'custom-provider-123' + } as Model + + const result = isOpenRouterGeminiGenerateImageModel(model, customProvider) + expect(result).toBe(false) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/mcp.test.ts b/src/renderer/src/aiCore/utils/__tests__/mcp.test.ts new file mode 100644 index 0000000000..dc26a03c80 --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/mcp.test.ts @@ -0,0 +1,440 @@ +/** + * mcp.ts Unit Tests + * Tests for MCP tools configuration and conversion utilities + */ + +import type { MCPTool } from '@renderer/types' +import type { Tool } from 'ai' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { convertMcpToolsToAiSdkTools, setupToolsConfig } from '../mcp' + +// Mock dependencies +vi.mock('@logger', () => ({ + loggerService: { + withContext: () => ({ + debug: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + info: vi.fn() + }) + } +})) + +vi.mock('@renderer/utils/mcp-tools', () => ({ + getMcpServerByTool: vi.fn(() => ({ id: 'test-server', autoApprove: false })), + isToolAutoApproved: vi.fn(() => false), + callMCPTool: vi.fn(async () => ({ + content: [{ type: 'text', text: 'Tool executed successfully' }], + isError: false + })) +})) + +vi.mock('@renderer/utils/userConfirmation', () => ({ + requestToolConfirmation: vi.fn(async () => true) +})) + +describe('mcp utils', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('setupToolsConfig', () => { + it('should return undefined when no MCP tools provided', () => { + const result = setupToolsConfig() + expect(result).toBeUndefined() + }) + + it('should return undefined when empty MCP tools array provided', () => { + const result = setupToolsConfig([]) + expect(result).toBeUndefined() + }) + + it('should convert MCP tools to AI SDK tools format', () => { + const mcpTools: MCPTool[] = [ + { + id: 'test-tool-1', + serverId: 'test-server', + serverName: 'test-server', + name: 'test-tool', + description: 'A test tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + query: { type: 'string' } + } + } + } + ] + + const result = setupToolsConfig(mcpTools) + + expect(result).not.toBeUndefined() + // Tools are now keyed by id (which includes serverId suffix) for uniqueness + expect(Object.keys(result!)).toEqual(['test-tool-1']) + expect(result!['test-tool-1']).toHaveProperty('description') + expect(result!['test-tool-1']).toHaveProperty('inputSchema') + expect(result!['test-tool-1']).toHaveProperty('execute') + }) + + it('should handle multiple MCP tools', () => { + const mcpTools: MCPTool[] = [ + { + id: 'tool1-id', + serverId: 'server1', + serverName: 'server1', + name: 'tool1', + description: 'First tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + }, + { + id: 'tool2-id', + serverId: 'server2', + serverName: 'server2', + name: 'tool2', + description: 'Second tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const result = setupToolsConfig(mcpTools) + + expect(result).not.toBeUndefined() + expect(Object.keys(result!)).toHaveLength(2) + // Tools are keyed by id for uniqueness + expect(Object.keys(result!)).toEqual(['tool1-id', 'tool2-id']) + }) + }) + + describe('convertMcpToolsToAiSdkTools', () => { + it('should convert single MCP tool to AI SDK tool', () => { + const mcpTools: MCPTool[] = [ + { + id: 'get-weather-id', + serverId: 'weather-server', + serverName: 'weather-server', + name: 'get-weather', + description: 'Get weather information', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + location: { type: 'string' } + }, + required: ['location'] + } + } + ] + + const result = convertMcpToolsToAiSdkTools(mcpTools) + + // Tools are keyed by id for uniqueness when multiple server instances exist + expect(Object.keys(result)).toEqual(['get-weather-id']) + + const tool = result['get-weather-id'] as Tool + expect(tool.description).toBe('Get weather information') + expect(tool.inputSchema).toBeDefined() + expect(typeof tool.execute).toBe('function') + }) + + it('should handle tool without description', () => { + const mcpTools: MCPTool[] = [ + { + id: 'no-desc-tool-id', + serverId: 'test-server', + serverName: 'test-server', + name: 'no-desc-tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const result = convertMcpToolsToAiSdkTools(mcpTools) + + expect(Object.keys(result)).toEqual(['no-desc-tool-id']) + const tool = result['no-desc-tool-id'] as Tool + expect(tool.description).toBe('Tool from test-server') + }) + + it('should convert empty tools array', () => { + const result = convertMcpToolsToAiSdkTools([]) + expect(result).toEqual({}) + }) + + it('should handle complex input schemas', () => { + const mcpTools: MCPTool[] = [ + { + id: 'complex-tool-id', + serverId: 'server', + serverName: 'server', + name: 'complex-tool', + description: 'Tool with complex schema', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + name: { type: 'string' }, + age: { type: 'number' }, + tags: { + type: 'array', + items: { type: 'string' } + }, + metadata: { + type: 'object', + properties: { + key: { type: 'string' } + } + } + }, + required: ['name'] + } + } + ] + + const result = convertMcpToolsToAiSdkTools(mcpTools) + + expect(Object.keys(result)).toEqual(['complex-tool-id']) + const tool = result['complex-tool-id'] as Tool + expect(tool.inputSchema).toBeDefined() + expect(typeof tool.execute).toBe('function') + }) + + it('should preserve tool id with special characters', () => { + const mcpTools: MCPTool[] = [ + { + id: 'special-tool-id', + serverId: 'server', + serverName: 'server', + name: 'tool_with-special.chars', + description: 'Special chars tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const result = convertMcpToolsToAiSdkTools(mcpTools) + // Tools are keyed by id for uniqueness + expect(Object.keys(result)).toEqual(['special-tool-id']) + }) + + it('should handle multiple tools with different schemas', () => { + const mcpTools: MCPTool[] = [ + { + id: 'string-tool-id', + serverId: 'server1', + serverName: 'server1', + name: 'string-tool', + description: 'String tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + input: { type: 'string' } + } + } + }, + { + id: 'number-tool-id', + serverId: 'server2', + serverName: 'server2', + name: 'number-tool', + description: 'Number tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + count: { type: 'number' } + } + } + }, + { + id: 'boolean-tool-id', + serverId: 'server3', + serverName: 'server3', + name: 'boolean-tool', + description: 'Boolean tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: { + enabled: { type: 'boolean' } + } + } + } + ] + + const result = convertMcpToolsToAiSdkTools(mcpTools) + + // Tools are keyed by id for uniqueness + expect(Object.keys(result).sort()).toEqual(['boolean-tool-id', 'number-tool-id', 'string-tool-id']) + expect(result['string-tool-id']).toBeDefined() + expect(result['number-tool-id']).toBeDefined() + expect(result['boolean-tool-id']).toBeDefined() + }) + }) + + describe('tool execution', () => { + it('should execute tool with user confirmation', async () => { + const { callMCPTool } = await import('@renderer/utils/mcp-tools') + const { requestToolConfirmation } = await import('@renderer/utils/userConfirmation') + + vi.mocked(requestToolConfirmation).mockResolvedValue(true) + vi.mocked(callMCPTool).mockResolvedValue({ + content: [{ type: 'text', text: 'Success' }], + isError: false + }) + + const mcpTools: MCPTool[] = [ + { + id: 'test-exec-tool-id', + serverId: 'test-server', + serverName: 'test-server', + name: 'test-exec-tool', + description: 'Test execution tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const tools = convertMcpToolsToAiSdkTools(mcpTools) + const tool = tools['test-exec-tool-id'] as Tool + const result = await tool.execute!({}, { messages: [], abortSignal: undefined, toolCallId: 'test-call-123' }) + + expect(requestToolConfirmation).toHaveBeenCalled() + expect(callMCPTool).toHaveBeenCalled() + expect(result).toEqual({ + content: [{ type: 'text', text: 'Success' }], + isError: false + }) + }) + + it('should handle user cancellation', async () => { + const { requestToolConfirmation } = await import('@renderer/utils/userConfirmation') + const { callMCPTool } = await import('@renderer/utils/mcp-tools') + + vi.mocked(requestToolConfirmation).mockResolvedValue(false) + + const mcpTools: MCPTool[] = [ + { + id: 'cancelled-tool-id', + serverId: 'test-server', + serverName: 'test-server', + name: 'cancelled-tool', + description: 'Tool to cancel', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const tools = convertMcpToolsToAiSdkTools(mcpTools) + const tool = tools['cancelled-tool-id'] as Tool + const result = await tool.execute!({}, { messages: [], abortSignal: undefined, toolCallId: 'cancel-call-123' }) + + expect(requestToolConfirmation).toHaveBeenCalled() + expect(callMCPTool).not.toHaveBeenCalled() + expect(result).toEqual({ + content: [ + { + type: 'text', + text: 'User declined to execute tool "cancelled-tool".' + } + ], + isError: false + }) + }) + + it('should handle tool execution error', async () => { + const { callMCPTool } = await import('@renderer/utils/mcp-tools') + const { requestToolConfirmation } = await import('@renderer/utils/userConfirmation') + + vi.mocked(requestToolConfirmation).mockResolvedValue(true) + vi.mocked(callMCPTool).mockResolvedValue({ + content: [{ type: 'text', text: 'Error occurred' }], + isError: true + }) + + const mcpTools: MCPTool[] = [ + { + id: 'error-tool-id', + serverId: 'test-server', + serverName: 'test-server', + name: 'error-tool', + description: 'Tool that errors', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const tools = convertMcpToolsToAiSdkTools(mcpTools) + const tool = tools['error-tool-id'] as Tool + + await expect( + tool.execute!({}, { messages: [], abortSignal: undefined, toolCallId: 'error-call-123' }) + ).rejects.toEqual({ + content: [{ type: 'text', text: 'Error occurred' }], + isError: true + }) + }) + + it('should auto-approve when enabled', async () => { + const { callMCPTool, isToolAutoApproved } = await import('@renderer/utils/mcp-tools') + const { requestToolConfirmation } = await import('@renderer/utils/userConfirmation') + + vi.mocked(isToolAutoApproved).mockReturnValue(true) + vi.mocked(callMCPTool).mockResolvedValue({ + content: [{ type: 'text', text: 'Auto-approved success' }], + isError: false + }) + + const mcpTools: MCPTool[] = [ + { + id: 'auto-approve-tool-id', + serverId: 'test-server', + serverName: 'test-server', + name: 'auto-approve-tool', + description: 'Auto-approved tool', + type: 'mcp', + inputSchema: { + type: 'object', + properties: {} + } + } + ] + + const tools = convertMcpToolsToAiSdkTools(mcpTools) + const tool = tools['auto-approve-tool-id'] as Tool + const result = await tool.execute!({}, { messages: [], abortSignal: undefined, toolCallId: 'auto-call-123' }) + + expect(requestToolConfirmation).not.toHaveBeenCalled() + expect(callMCPTool).toHaveBeenCalled() + expect(result).toEqual({ + content: [{ type: 'text', text: 'Auto-approved success' }], + isError: false + }) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/options.test.ts b/src/renderer/src/aiCore/utils/__tests__/options.test.ts new file mode 100644 index 0000000000..9eeeac725b --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/options.test.ts @@ -0,0 +1,1156 @@ +/** + * options.ts Unit Tests + * Tests for building provider-specific options + */ + +import type { Assistant, Model, Provider } from '@renderer/types' +import { OpenAIServiceTiers, SystemProviderIds } from '@renderer/types' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { buildProviderOptions } from '../options' + +// Mock dependencies +vi.mock('@cherrystudio/ai-core/provider', async (importOriginal) => { + const actual = (await importOriginal()) as object + return { + ...actual, + baseProviderIdSchema: { + safeParse: vi.fn((id) => { + const baseProviders = [ + 'openai', + 'openai-chat', + 'azure', + 'azure-responses', + 'huggingface', + 'anthropic', + 'google', + 'xai', + 'deepseek', + 'openrouter', + 'openai-compatible', + 'cherryin' + ] + if (baseProviders.includes(id)) { + return { success: true, data: id } + } + return { success: false } + }) + }, + customProviderIdSchema: { + safeParse: vi.fn((id) => { + const customProviders = [ + 'google-vertex', + 'google-vertex-anthropic', + 'bedrock', + 'gateway', + 'aihubmix', + 'newapi', + 'ollama' + ] + if (customProviders.includes(id)) { + return { success: true, data: id } + } + return { success: false, error: new Error('Invalid provider') } + }) + } + } +}) + +// Don't mock getAiSdkProviderId - use real implementation for more accurate tests + +vi.mock('@renderer/config/models', async (importOriginal) => ({ + ...(await importOriginal()), + isOpenAIModel: vi.fn((model) => model.id.includes('gpt') || model.id.includes('o1')), + isQwenMTModel: vi.fn(() => false), + isSupportFlexServiceTierModel: vi.fn(() => true), + isOpenAILLMModel: vi.fn(() => true), + SYSTEM_MODELS: { + defaultModel: [ + { id: 'default-1', name: 'Default 1' }, + { id: 'default-2', name: 'Default 2' }, + { id: 'default-3', name: 'Default 3' } + ] + } +})) + +vi.mock(import('@renderer/utils/provider'), async (importOriginal) => { + return { + ...(await importOriginal()), + isSupportServiceTierProvider: vi.fn((provider) => { + return [SystemProviderIds.openai, SystemProviderIds.groq].includes(provider.id) + }) + } +}) + +vi.mock('@renderer/store/settings', () => ({ + default: (state = { settings: {} }) => state +})) + +vi.mock('@renderer/hooks/useSettings', () => ({ + getStoreSetting: vi.fn((key) => { + if (key === 'openAI') { + return { summaryText: 'off', verbosity: 'medium' } as any + } + return {} + }) +})) + +vi.mock('@renderer/services/AssistantService', () => ({ + getDefaultAssistant: vi.fn(() => ({ + id: 'default', + name: 'Default Assistant', + settings: {} + })), + getAssistantSettings: vi.fn(() => ({ + reasoning_effort: 'medium', + maxTokens: 4096 + })), + getProviderByModel: vi.fn((model: Model) => ({ + id: model.provider, + name: 'Mock Provider' + })) +})) + +vi.mock('../reasoning', () => ({ + getOpenAIReasoningParams: vi.fn(() => ({ reasoningEffort: 'medium' })), + getAnthropicReasoningParams: vi.fn(() => ({ + thinking: { type: 'enabled', budgetTokens: 5000 } + })), + getGeminiReasoningParams: vi.fn(() => ({ + thinkingConfig: { include_thoughts: true } + })), + getXAIReasoningParams: vi.fn(() => ({ reasoningEffort: 'high' })), + getBedrockReasoningParams: vi.fn(() => ({ + reasoningConfig: { type: 'enabled', budgetTokens: 5000 } + })), + getReasoningEffort: vi.fn(() => ({ reasoningEffort: 'medium' })), + getCustomParameters: vi.fn(() => ({})), + extractAiSdkStandardParams: vi.fn((customParams: Record) => { + const AI_SDK_STANDARD_PARAMS = ['topK', 'frequencyPenalty', 'presencePenalty', 'stopSequences', 'seed'] + const standardParams: Record = {} + const providerParams: Record = {} + for (const [key, value] of Object.entries(customParams)) { + if (AI_SDK_STANDARD_PARAMS.includes(key)) { + standardParams[key] = value + } else { + providerParams[key] = value + } + } + return { standardParams, providerParams } + }) +})) + +vi.mock('../image', () => ({ + buildGeminiGenerateImageParams: vi.fn(() => ({ + responseModalities: ['TEXT', 'IMAGE'] + })) +})) + +vi.mock('../websearch', () => ({ + getWebSearchParams: vi.fn(() => ({ enable_search: true })) +})) + +vi.mock('../../prepareParams/header', () => ({ + addAnthropicHeaders: vi.fn(() => ['context-1m-2025-08-07']) +})) + +const ensureWindowApi = () => { + const globalWindow = window as any + globalWindow.api = globalWindow.api || {} + globalWindow.api.getAppInfo = globalWindow.api.getAppInfo || vi.fn(async () => ({ notesPath: '' })) +} + +ensureWindowApi() + +describe('options utils', () => { + const mockAssistant: Assistant = { + id: 'test-assistant', + name: 'Test Assistant', + settings: {} + } as Assistant + + const mockModel: Model = { + id: 'gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.openai + } as Model + + beforeEach(async () => { + vi.clearAllMocks() + // Reset getCustomParameters to return empty object by default + const { getCustomParameters } = await import('../reasoning') + vi.mocked(getCustomParameters).mockReturnValue({}) + }) + + describe('buildProviderOptions', () => { + describe('OpenAI provider', () => { + const openaiProvider: Provider = { + id: SystemProviderIds.openai, + name: 'OpenAI', + type: 'openai-response', + apiKey: 'test-key', + apiHost: 'https://api.openai.com/v1', + isSystem: true + } as Provider + + it('should build basic OpenAI options', () => { + const result = buildProviderOptions(mockAssistant, mockModel, openaiProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('openai') + expect(result.providerOptions.openai).toBeDefined() + expect(result.standardParams).toBeDefined() + }) + + it('should include reasoning parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, mockModel, openaiProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.openai).toHaveProperty('reasoningEffort') + expect(result.providerOptions.openai.reasoningEffort).toBe('medium') + }) + + it('should include service tier when supported', () => { + const providerWithServiceTier: Provider = { + ...openaiProvider, + serviceTier: OpenAIServiceTiers.auto + } + + const result = buildProviderOptions(mockAssistant, mockModel, providerWithServiceTier, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.openai).toHaveProperty('serviceTier') + expect(result.providerOptions.openai.serviceTier).toBe(OpenAIServiceTiers.auto) + }) + }) + + describe('Anthropic provider', () => { + const anthropicProvider: Provider = { + id: SystemProviderIds.anthropic, + name: 'Anthropic', + type: 'anthropic', + apiKey: 'test-key', + apiHost: 'https://api.anthropic.com', + isSystem: true + } as Provider + + const anthropicModel: Model = { + id: 'claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.anthropic + } as Model + + it('should build basic Anthropic options', () => { + const result = buildProviderOptions(mockAssistant, anthropicModel, anthropicProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('anthropic') + expect(result.providerOptions.anthropic).toBeDefined() + }) + + it('should include reasoning parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, anthropicModel, anthropicProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.anthropic).toHaveProperty('thinking') + expect(result.providerOptions.anthropic.thinking).toEqual({ + type: 'enabled', + budgetTokens: 5000 + }) + }) + }) + + describe('Google provider', () => { + const googleProvider: Provider = { + id: SystemProviderIds.gemini, + name: 'Google', + type: 'gemini', + apiKey: 'test-key', + apiHost: 'https://generativelanguage.googleapis.com', + isSystem: true, + models: [{ id: 'gemini-2.0-flash-exp' }] as Model[] + } as Provider + + const googleModel: Model = { + id: 'gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: SystemProviderIds.gemini + } as Model + + it('should build basic Google options', () => { + const result = buildProviderOptions(mockAssistant, googleModel, googleProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('google') + expect(result.providerOptions.google).toBeDefined() + }) + + it('should include reasoning parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, googleModel, googleProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.google).toHaveProperty('thinkingConfig') + expect(result.providerOptions.google.thinkingConfig).toEqual({ + include_thoughts: true + }) + }) + + it('should include image generation parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, googleModel, googleProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: true + }) + + expect(result.providerOptions.google).toHaveProperty('responseModalities') + expect(result.providerOptions.google.responseModalities).toEqual(['TEXT', 'IMAGE']) + }) + }) + + describe('xAI provider', () => { + const xaiProvider = { + id: SystemProviderIds.grok, + name: 'xAI', + type: 'new-api', + apiKey: 'test-key', + apiHost: 'https://api.x.ai/v1', + isSystem: true, + models: [] as Model[] + } as Provider + + const xaiModel: Model = { + id: 'grok-2-latest', + name: 'Grok 2', + provider: SystemProviderIds.grok + } as Model + + it('should build basic xAI options', () => { + const result = buildProviderOptions(mockAssistant, xaiModel, xaiProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('xai') + expect(result.providerOptions.xai).toBeDefined() + }) + + it('should include reasoning parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, xaiModel, xaiProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.xai).toHaveProperty('reasoningEffort') + expect(result.providerOptions.xai.reasoningEffort).toBe('high') + }) + }) + + describe('DeepSeek provider', () => { + const deepseekProvider: Provider = { + id: SystemProviderIds.deepseek, + name: 'DeepSeek', + type: 'openai', + apiKey: 'test-key', + apiHost: 'https://api.deepseek.com', + isSystem: true + } as Provider + + const deepseekModel: Model = { + id: 'deepseek-chat', + name: 'DeepSeek Chat', + provider: SystemProviderIds.deepseek + } as Model + + it('should build basic DeepSeek options', () => { + const result = buildProviderOptions(mockAssistant, deepseekModel, deepseekProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + expect(result.providerOptions).toHaveProperty('deepseek') + expect(result.providerOptions.deepseek).toBeDefined() + }) + }) + + describe('OpenRouter provider', () => { + const openrouterProvider: Provider = { + id: SystemProviderIds.openrouter, + name: 'OpenRouter', + type: 'openai', + apiKey: 'test-key', + apiHost: 'https://openrouter.ai/api/v1', + isSystem: true + } as Provider + + const openrouterModel: Model = { + id: 'openai/gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.openrouter + } as Model + + it('should build basic OpenRouter options', () => { + const result = buildProviderOptions(mockAssistant, openrouterModel, openrouterProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('openrouter') + expect(result.providerOptions.openrouter).toBeDefined() + }) + + it('should include web search parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, openrouterModel, openrouterProvider, { + enableReasoning: false, + enableWebSearch: true, + enableGenerateImage: false + }) + + expect(result.providerOptions.openrouter).toHaveProperty('enable_search') + }) + }) + + describe('Custom parameters', () => { + it('should merge custom provider-specific parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + vi.mocked(getCustomParameters).mockReturnValue({ + custom_param: 'custom_value', + another_param: 123 + }) + + const result = buildProviderOptions( + mockAssistant, + mockModel, + { + id: SystemProviderIds.openai, + name: 'OpenAI', + type: 'openai', + apiKey: 'test-key', + apiHost: 'https://api.openai.com/v1' + } as Provider, + { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + } + ) + + expect(result.providerOptions).toStrictEqual({ + openai: { + custom_param: 'custom_value', + another_param: 123, + serviceTier: undefined, + textVerbosity: undefined + } + }) + }) + + it('should extract AI SDK standard params from custom parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + vi.mocked(getCustomParameters).mockReturnValue({ + topK: 5, + frequencyPenalty: 0.5, + presencePenalty: 0.3, + seed: 42, + custom_param: 'custom_value' + }) + + const result = buildProviderOptions( + mockAssistant, + mockModel, + { + id: SystemProviderIds.gemini, + name: 'Google', + type: 'gemini', + apiKey: 'test-key', + apiHost: 'https://generativelanguage.googleapis.com' + } as Provider, + { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + } + ) + + // Standard params should be extracted and returned separately + expect(result.standardParams).toEqual({ + topK: 5, + frequencyPenalty: 0.5, + presencePenalty: 0.3, + seed: 42 + }) + + // Provider-specific params should still be in providerOptions + expect(result.providerOptions.google).toHaveProperty('custom_param') + expect(result.providerOptions.google.custom_param).toBe('custom_value') + + // Standard params should NOT be in providerOptions + expect(result.providerOptions.google).not.toHaveProperty('topK') + expect(result.providerOptions.google).not.toHaveProperty('frequencyPenalty') + expect(result.providerOptions.google).not.toHaveProperty('presencePenalty') + expect(result.providerOptions.google).not.toHaveProperty('seed') + }) + + it('should handle stopSequences in custom parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + vi.mocked(getCustomParameters).mockReturnValue({ + stopSequences: ['STOP', 'END'], + custom_param: 'value' + }) + + const result = buildProviderOptions( + mockAssistant, + mockModel, + { + id: SystemProviderIds.gemini, + name: 'Google', + type: 'gemini', + apiKey: 'test-key', + apiHost: 'https://generativelanguage.googleapis.com' + } as Provider, + { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + } + ) + + expect(result.standardParams).toEqual({ + stopSequences: ['STOP', 'END'] + }) + expect(result.providerOptions.google).not.toHaveProperty('stopSequences') + }) + }) + + describe('Multiple capabilities', () => { + const googleProvider = { + id: SystemProviderIds.gemini, + name: 'Google', + type: 'gemini', + apiKey: 'test-key', + apiHost: 'https://generativelanguage.googleapis.com', + isSystem: true, + models: [] as Model[] + } as Provider + + const googleModel: Model = { + id: 'gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: SystemProviderIds.gemini + } as Model + + it('should combine reasoning and image generation', () => { + const result = buildProviderOptions(mockAssistant, googleModel, googleProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: true + }) + + expect(result.providerOptions.google).toHaveProperty('thinkingConfig') + expect(result.providerOptions.google).toHaveProperty('responseModalities') + }) + + it('should handle all capabilities enabled', () => { + const result = buildProviderOptions(mockAssistant, googleModel, googleProvider, { + enableReasoning: true, + enableWebSearch: true, + enableGenerateImage: true + }) + + expect(result.providerOptions.google).toBeDefined() + expect(Object.keys(result.providerOptions.google).length).toBeGreaterThan(0) + }) + }) + + describe('Vertex AI providers', () => { + it('should map google-vertex to google', () => { + const vertexProvider = { + id: 'google-vertex', + name: 'Vertex AI', + type: 'vertexai', + apiKey: 'test-key', + apiHost: 'https://vertex-ai.googleapis.com', + models: [] as Model[] + } as Provider + + const vertexModel: Model = { + id: 'gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: 'google-vertex' + } as Model + + const result = buildProviderOptions(mockAssistant, vertexModel, vertexProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('google') + }) + + it('should map google-vertex-anthropic to anthropic', () => { + const vertexAnthropicProvider = { + id: 'google-vertex-anthropic', + name: 'Vertex AI Anthropic', + type: 'vertex-anthropic', + apiKey: 'test-key', + apiHost: 'https://vertex-ai.googleapis.com', + models: [] as Model[] + } as Provider + + const vertexModel: Model = { + id: 'claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: 'google-vertex-anthropic' + } as Model + + const result = buildProviderOptions(mockAssistant, vertexModel, vertexAnthropicProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('anthropic') + }) + }) + + describe('AWS Bedrock provider', () => { + const bedrockProvider = { + id: 'bedrock', + name: 'AWS Bedrock', + type: 'aws-bedrock', + apiKey: 'test-key', + apiHost: 'https://bedrock.us-east-1.amazonaws.com', + models: [] as Model[] + } as Provider + + const bedrockModel: Model = { + id: 'anthropic.claude-sonnet-4-20250514-v1:0', + name: 'Claude Sonnet 4', + provider: 'bedrock' + } as Model + + it('should build basic Bedrock options', () => { + const result = buildProviderOptions(mockAssistant, bedrockModel, bedrockProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('bedrock') + expect(result.providerOptions.bedrock).toBeDefined() + }) + + it('should include anthropicBeta when Anthropic headers are needed', async () => { + const { addAnthropicHeaders } = await import('../../prepareParams/header') + vi.mocked(addAnthropicHeaders).mockReturnValue(['interleaved-thinking-2025-05-14', 'context-1m-2025-08-07']) + + const result = buildProviderOptions(mockAssistant, bedrockModel, bedrockProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.bedrock).toHaveProperty('anthropicBeta') + expect(result.providerOptions.bedrock.anthropicBeta).toEqual([ + 'interleaved-thinking-2025-05-14', + 'context-1m-2025-08-07' + ]) + }) + + it('should include reasoning parameters when enabled', () => { + const result = buildProviderOptions(mockAssistant, bedrockModel, bedrockProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.bedrock).toHaveProperty('reasoningConfig') + expect(result.providerOptions.bedrock.reasoningConfig).toEqual({ + type: 'enabled', + budgetTokens: 5000 + }) + }) + }) + + describe('AI Gateway provider', () => { + const gatewayProvider: Provider = { + id: SystemProviderIds.gateway, + name: 'Vercel AI Gateway', + type: 'gateway', + apiKey: 'test-key', + apiHost: 'https://gateway.vercel.com', + isSystem: true + } as Provider + + it('should build OpenAI options for OpenAI models through gateway', () => { + const openaiModel: Model = { + id: 'openai/gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, openaiModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('openai') + expect(result.providerOptions.openai).toBeDefined() + }) + + it('should build Anthropic options for Anthropic models through gateway', () => { + const anthropicModel: Model = { + id: 'anthropic/claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, anthropicModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('anthropic') + expect(result.providerOptions.anthropic).toBeDefined() + }) + + it('should build Google options for Gemini models through gateway', () => { + const geminiModel: Model = { + id: 'google/gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, geminiModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('google') + expect(result.providerOptions.google).toBeDefined() + }) + + it('should build xAI options for Grok models through gateway', () => { + const grokModel: Model = { + id: 'xai/grok-2-latest', + name: 'Grok 2', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, grokModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('xai') + expect(result.providerOptions.xai).toBeDefined() + }) + + it('should include reasoning parameters for Anthropic models when enabled', () => { + const anthropicModel: Model = { + id: 'anthropic/claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, anthropicModel, gatewayProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions.anthropic).toHaveProperty('thinking') + expect(result.providerOptions.anthropic.thinking).toEqual({ + type: 'enabled', + budgetTokens: 5000 + }) + }) + + it('should merge gateway routing options from custom parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + vi.mocked(getCustomParameters).mockReturnValue({ + gateway: { + order: ['vertex', 'anthropic'], + only: ['vertex', 'anthropic'] + } + }) + + const anthropicModel: Model = { + id: 'anthropic/claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, anthropicModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should have both anthropic provider options and gateway routing options + expect(result.providerOptions).toHaveProperty('anthropic') + expect(result.providerOptions).toHaveProperty('gateway') + expect(result.providerOptions.gateway).toEqual({ + order: ['vertex', 'anthropic'], + only: ['vertex', 'anthropic'] + }) + }) + + it('should combine provider-specific options with gateway routing options', async () => { + const { getCustomParameters } = await import('../reasoning') + + vi.mocked(getCustomParameters).mockReturnValue({ + gateway: { + order: ['openai', 'anthropic'] + } + }) + + const openaiModel: Model = { + id: 'openai/gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, openaiModel, gatewayProvider, { + enableReasoning: true, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should have OpenAI provider options with reasoning + expect(result.providerOptions.openai).toBeDefined() + expect(result.providerOptions.openai).toHaveProperty('reasoningEffort') + + // Should also have gateway routing options + expect(result.providerOptions.gateway).toBeDefined() + expect(result.providerOptions.gateway.order).toEqual(['openai', 'anthropic']) + }) + + it('should build generic options for unknown model types through gateway', () => { + const unknownModel: Model = { + id: 'unknown-provider/model-name', + name: 'Unknown Model', + provider: SystemProviderIds.gateway + } as Model + + const result = buildProviderOptions(mockAssistant, unknownModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + expect(result.providerOptions).toHaveProperty('openai-compatible') + expect(result.providerOptions['openai-compatible']).toBeDefined() + }) + }) + + describe('Proxy provider custom parameters mapping', () => { + it('should map cherryin provider ID to actual AI SDK provider ID (Google)', async () => { + const { getCustomParameters } = await import('../reasoning') + + // Mock Cherry In provider that uses Google SDK + const cherryinProvider = { + id: 'cherryin', + name: 'Cherry In', + type: 'gemini', // Using Google SDK + apiKey: 'test-key', + apiHost: 'https://cherryin.com', + models: [] as Model[] + } as Provider + + const geminiModel: Model = { + id: 'gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: 'cherryin' + } as Model + + // User provides custom parameters with Cherry Studio provider ID + vi.mocked(getCustomParameters).mockReturnValue({ + cherryin: { + customOption1: 'value1', + customOption2: 'value2' + } + }) + + const result = buildProviderOptions(mockAssistant, geminiModel, cherryinProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should map to 'google' AI SDK provider, not 'cherryin' + expect(result.providerOptions).toHaveProperty('google') + expect(result.providerOptions).not.toHaveProperty('cherryin') + expect(result.providerOptions.google).toMatchObject({ + customOption1: 'value1', + customOption2: 'value2' + }) + }) + + it('should map cherryin provider ID to actual AI SDK provider ID (OpenAI)', async () => { + const { getCustomParameters } = await import('../reasoning') + + // Mock Cherry In provider that uses OpenAI SDK + const cherryinProvider = { + id: 'cherryin', + name: 'Cherry In', + type: 'openai-response', // Using OpenAI SDK + apiKey: 'test-key', + apiHost: 'https://cherryin.com', + models: [] as Model[] + } as Provider + + const openaiModel: Model = { + id: 'gpt-4', + name: 'GPT-4', + provider: 'cherryin' + } as Model + + // User provides custom parameters with Cherry Studio provider ID + vi.mocked(getCustomParameters).mockReturnValue({ + cherryin: { + customOpenAIOption: 'openai_value' + } + }) + + const result = buildProviderOptions(mockAssistant, openaiModel, cherryinProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should map to 'openai' AI SDK provider, not 'cherryin' + expect(result.providerOptions).toHaveProperty('openai') + expect(result.providerOptions).not.toHaveProperty('cherryin') + expect(result.providerOptions.openai).toMatchObject({ + customOpenAIOption: 'openai_value' + }) + }) + + it('should allow direct AI SDK provider ID in custom parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + const geminiProvider = { + id: SystemProviderIds.gemini, + name: 'Google', + type: 'gemini', + apiKey: 'test-key', + apiHost: 'https://generativelanguage.googleapis.com', + models: [] as Model[] + } as Provider + + const geminiModel: Model = { + id: 'gemini-2.0-flash-exp', + name: 'Gemini 2.0 Flash', + provider: SystemProviderIds.gemini + } as Model + + // User provides custom parameters directly with AI SDK provider ID + vi.mocked(getCustomParameters).mockReturnValue({ + google: { + directGoogleOption: 'google_value' + } + }) + + const result = buildProviderOptions(mockAssistant, geminiModel, geminiProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should merge directly to 'google' provider + expect(result.providerOptions.google).toMatchObject({ + directGoogleOption: 'google_value' + }) + }) + + it('should map gateway provider custom parameters to actual AI SDK provider', async () => { + const { getCustomParameters } = await import('../reasoning') + + const gatewayProvider: Provider = { + id: SystemProviderIds.gateway, + name: 'Vercel AI Gateway', + type: 'gateway', + apiKey: 'test-key', + apiHost: 'https://gateway.vercel.com', + isSystem: true + } as Provider + + const anthropicModel: Model = { + id: 'anthropic/claude-3-5-sonnet-20241022', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.gateway + } as Model + + // User provides both gateway routing options and gateway-scoped custom parameters + vi.mocked(getCustomParameters).mockReturnValue({ + gateway: { + order: ['vertex', 'anthropic'], + only: ['vertex'] + }, + customParam: 'should_go_to_anthropic' + }) + + const result = buildProviderOptions(mockAssistant, anthropicModel, gatewayProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Gateway routing options should be preserved + expect(result.providerOptions.gateway).toEqual({ + order: ['vertex', 'anthropic'], + only: ['vertex'] + }) + + // Custom parameters should go to the actual AI SDK provider (anthropic) + expect(result.providerOptions.anthropic).toMatchObject({ + customParam: 'should_go_to_anthropic' + }) + }) + + it('should handle mixed custom parameters (AI SDK provider ID + custom params)', async () => { + const { getCustomParameters } = await import('../reasoning') + + const openaiProvider: Provider = { + id: SystemProviderIds.openai, + name: 'OpenAI', + type: 'openai-response', + apiKey: 'test-key', + apiHost: 'https://api.openai.com/v1', + isSystem: true + } as Provider + + // User provides both direct AI SDK provider params and custom params + vi.mocked(getCustomParameters).mockReturnValue({ + openai: { + providerSpecific: 'value1' + }, + customParam1: 'value2', + customParam2: 123 + }) + + const result = buildProviderOptions(mockAssistant, mockModel, openaiProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should merge both into 'openai' provider options + expect(result.providerOptions.openai).toMatchObject({ + providerSpecific: 'value1', + customParam1: 'value2', + customParam2: 123 + }) + }) + + // Note: For proxy providers like aihubmix/newapi, users should write AI SDK provider ID (google/anthropic) + // instead of the Cherry Studio provider ID for custom parameters to work correctly + + it('should handle cherryin fallback to openai-compatible with custom parameters', async () => { + const { getCustomParameters } = await import('../reasoning') + + // Mock cherryin provider that falls back to openai-compatible (default case) + const cherryinProvider = { + id: 'cherryin', + name: 'Cherry In', + type: 'openai', + apiKey: 'test-key', + apiHost: 'https://cherryin.com', + models: [] as Model[] + } as Provider + + const testModel: Model = { + id: 'some-model', + name: 'Some Model', + provider: 'cherryin' + } as Model + + // User provides custom parameters with cherryin provider ID + vi.mocked(getCustomParameters).mockReturnValue({ + customCherryinOption: 'cherryin_value' + }) + + const result = buildProviderOptions(mockAssistant, testModel, cherryinProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // When cherryin falls back to default case, it should use rawProviderId (cherryin) + // User's cherryin params should merge with the provider options + expect(result.providerOptions).toHaveProperty('cherryin') + expect(result.providerOptions.cherryin).toMatchObject({ + customCherryinOption: 'cherryin_value' + }) + }) + + it('should handle cross-provider configurations', async () => { + const { getCustomParameters } = await import('../reasoning') + + const openaiProvider: Provider = { + id: SystemProviderIds.openai, + name: 'OpenAI', + type: 'openai-response', + apiKey: 'test-key', + apiHost: 'https://api.openai.com/v1', + isSystem: true + } as Provider + + // User provides parameters for multiple providers + // In real usage, anthropic/google params would be treated as regular params for openai provider + vi.mocked(getCustomParameters).mockReturnValue({ + openai: { + openaiSpecific: 'openai_value' + }, + customParam: 'value' + }) + + const result = buildProviderOptions(mockAssistant, mockModel, openaiProvider, { + enableReasoning: false, + enableWebSearch: false, + enableGenerateImage: false + }) + + // Should have openai provider options with both scoped and custom params + expect(result.providerOptions).toHaveProperty('openai') + expect(result.providerOptions.openai).toMatchObject({ + openaiSpecific: 'openai_value', + customParam: 'value' + }) + }) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/reasoning.poe.test.ts b/src/renderer/src/aiCore/utils/__tests__/reasoning.poe.test.ts new file mode 100644 index 0000000000..90876998da --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/reasoning.poe.test.ts @@ -0,0 +1,288 @@ +import type { Assistant, Model, ReasoningEffortOption } from '@renderer/types' +import { SystemProviderIds } from '@renderer/types' +import { describe, expect, it, vi } from 'vitest' + +import { getReasoningEffort } from '../reasoning' + +// Mock logger +vi.mock('@logger', () => ({ + loggerService: { + withContext: () => ({ + warn: vi.fn(), + info: vi.fn(), + error: vi.fn() + }) + } +})) + +vi.mock('@renderer/store/settings', () => ({ + default: {}, + settingsSlice: { + name: 'settings', + reducer: vi.fn(), + actions: {} + } +})) + +vi.mock('@renderer/store/assistants', () => { + const mockAssistantsSlice = { + name: 'assistants', + reducer: vi.fn((state = { entities: {}, ids: [] }) => state), + actions: { + updateTopicUpdatedAt: vi.fn(() => ({ type: 'UPDATE_TOPIC_UPDATED_AT' })) + } + } + + return { + default: mockAssistantsSlice.reducer, + updateTopicUpdatedAt: vi.fn(() => ({ type: 'UPDATE_TOPIC_UPDATED_AT' })), + assistantsSlice: mockAssistantsSlice + } +}) + +// Mock provider service +vi.mock('@renderer/services/AssistantService', () => ({ + getProviderByModel: (model: Model) => ({ + id: model.provider, + name: 'Poe', + type: 'openai' + }), + getAssistantSettings: (assistant: Assistant) => assistant.settings || {} +})) + +describe('Poe Provider Reasoning Support', () => { + const createPoeModel = (id: string): Model => ({ + id, + name: id, + provider: SystemProviderIds.poe, + group: 'poe' + }) + + const createAssistant = (reasoning_effort?: ReasoningEffortOption, maxTokens?: number): Assistant => ({ + id: 'test-assistant', + name: 'Test Assistant', + emoji: '🤖', + prompt: '', + topics: [], + messages: [], + type: 'assistant', + regularPhrases: [], + settings: { + reasoning_effort, + maxTokens + } + }) + + describe('GPT-5 Series Models', () => { + it('should return reasoning_effort in extra_body for GPT-5 model with low effort', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant('low') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({ + extra_body: { + reasoning_effort: 'low' + } + }) + }) + + it('should return reasoning_effort in extra_body for GPT-5 model with medium effort', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant('medium') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({ + extra_body: { + reasoning_effort: 'medium' + } + }) + }) + + it('should return reasoning_effort in extra_body for GPT-5 model with high effort', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant('high') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({ + extra_body: { + reasoning_effort: 'high' + } + }) + }) + + it('should convert auto to medium for GPT-5 model in extra_body', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant('auto') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({ + extra_body: { + reasoning_effort: 'medium' + } + }) + }) + + it('should return reasoning_effort in extra_body for GPT-5.1 model', () => { + const model = createPoeModel('gpt-5.1') + const assistant = createAssistant('medium') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({ + extra_body: { + reasoning_effort: 'medium' + } + }) + }) + }) + + describe('Claude Models', () => { + it('should return thinking_budget in extra_body for Claude 3.7 Sonnet', () => { + const model = createPoeModel('claude-3.7-sonnet') + const assistant = createAssistant('medium', 4096) + const result = getReasoningEffort(assistant, model) + + expect(result).toHaveProperty('extra_body') + expect(result.extra_body).toHaveProperty('thinking_budget') + expect(typeof result.extra_body?.thinking_budget).toBe('number') + expect(result.extra_body?.thinking_budget).toBeGreaterThan(0) + }) + + it('should return thinking_budget in extra_body for Claude Sonnet 4', () => { + const model = createPoeModel('claude-sonnet-4') + const assistant = createAssistant('high', 8192) + const result = getReasoningEffort(assistant, model) + + expect(result).toHaveProperty('extra_body') + expect(result.extra_body).toHaveProperty('thinking_budget') + expect(typeof result.extra_body?.thinking_budget).toBe('number') + }) + + it('should calculate thinking_budget based on effort ratio and maxTokens', () => { + const model = createPoeModel('claude-3.7-sonnet') + const assistant = createAssistant('low', 4096) + const result = getReasoningEffort(assistant, model) + + expect(result.extra_body?.thinking_budget).toBeGreaterThanOrEqual(1024) + }) + }) + + describe('Gemini Models', () => { + it('should return thinking_budget in extra_body for Gemini 2.5 Flash', () => { + const model = createPoeModel('gemini-2.5-flash') + const assistant = createAssistant('medium') + const result = getReasoningEffort(assistant, model) + + expect(result).toHaveProperty('extra_body') + expect(result.extra_body).toHaveProperty('thinking_budget') + expect(typeof result.extra_body?.thinking_budget).toBe('number') + }) + + it('should return thinking_budget in extra_body for Gemini 2.5 Pro', () => { + const model = createPoeModel('gemini-2.5-pro') + const assistant = createAssistant('high') + const result = getReasoningEffort(assistant, model) + + expect(result).toHaveProperty('extra_body') + expect(result.extra_body).toHaveProperty('thinking_budget') + }) + + it('should use -1 for auto effort', () => { + const model = createPoeModel('gemini-2.5-flash') + const assistant = createAssistant('auto') + const result = getReasoningEffort(assistant, model) + + expect(result.extra_body?.thinking_budget).toBe(-1) + }) + + it('should calculate thinking_budget for non-auto effort', () => { + const model = createPoeModel('gemini-2.5-flash') + const assistant = createAssistant('low') + const result = getReasoningEffort(assistant, model) + + expect(typeof result.extra_body?.thinking_budget).toBe('number') + }) + }) + + describe('No Reasoning Effort', () => { + it('should return empty object when reasoning_effort is not set', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant(undefined) + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({}) + }) + + it('should return empty object when reasoning_effort is "none"', () => { + const model = createPoeModel('gpt-5') + const assistant = createAssistant('none') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({}) + }) + }) + + describe('Non-Reasoning Models', () => { + it('should return empty object for non-reasoning models', () => { + const model = createPoeModel('gpt-4') + const assistant = createAssistant('medium') + const result = getReasoningEffort(assistant, model) + + expect(result).toEqual({}) + }) + }) + + describe('Edge Cases: Models Without Token Limit Configuration', () => { + it('should return empty object for Claude models without token limit configuration', () => { + const model = createPoeModel('claude-unknown-variant') + const assistant = createAssistant('medium', 4096) + const result = getReasoningEffort(assistant, model) + + // Should return empty object when token limit is not found + expect(result).toEqual({}) + expect(result.extra_body?.thinking_budget).toBeUndefined() + }) + + it('should return empty object for unmatched Poe reasoning models', () => { + // A hypothetical reasoning model that doesn't match GPT-5, Claude, or Gemini + const model = createPoeModel('some-reasoning-model') + // Make it appear as a reasoning model by giving it a name that won't match known categories + const assistant = createAssistant('medium') + const result = getReasoningEffort(assistant, model) + + // Should return empty object for unmatched models + expect(result).toEqual({}) + }) + + it('should fallback to -1 for Gemini models without token limit', () => { + // Use a Gemini model variant that won't match any token limit pattern + // The current regex patterns cover gemini-.*-flash.*$ and gemini-.*-pro.*$ + // so we need a model that matches isSupportedThinkingTokenGeminiModel but not THINKING_TOKEN_MAP + const model = createPoeModel('gemini-2.5-flash') + const assistant = createAssistant('auto') + const result = getReasoningEffort(assistant, model) + + // For 'auto' effort, should use -1 + expect(result.extra_body?.thinking_budget).toBe(-1) + }) + + it('should enforce minimum 1024 token floor for Claude models', () => { + const model = createPoeModel('claude-3.7-sonnet') + // Use very small maxTokens to test the minimum floor + const assistant = createAssistant('low', 100) + const result = getReasoningEffort(assistant, model) + + expect(result.extra_body?.thinking_budget).toBeGreaterThanOrEqual(1024) + }) + + it('should handle undefined maxTokens for Claude models', () => { + const model = createPoeModel('claude-3.7-sonnet') + const assistant = createAssistant('medium', undefined) + const result = getReasoningEffort(assistant, model) + + expect(result).toHaveProperty('extra_body') + expect(result.extra_body).toHaveProperty('thinking_budget') + expect(typeof result.extra_body?.thinking_budget).toBe('number') + expect(result.extra_body?.thinking_budget).toBeGreaterThanOrEqual(1024) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/reasoning.test.ts b/src/renderer/src/aiCore/utils/__tests__/reasoning.test.ts new file mode 100644 index 0000000000..fec4d197e3 --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/reasoning.test.ts @@ -0,0 +1,993 @@ +/** + * reasoning.ts Unit Tests + * Tests for reasoning parameter generation utilities + */ + +import { getStoreSetting } from '@renderer/hooks/useSettings' +import type { SettingsState } from '@renderer/store/settings' +import type { Assistant, Model, Provider } from '@renderer/types' +import { SystemProviderIds } from '@renderer/types' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { + getAnthropicReasoningParams, + getBedrockReasoningParams, + getCustomParameters, + getGeminiReasoningParams, + getOpenAIReasoningParams, + getReasoningEffort, + getXAIReasoningParams +} from '../reasoning' + +function defaultGetStoreSetting(key: K): SettingsState[K] { + if (key === 'openAI') { + return { + summaryText: 'auto', + verbosity: 'medium' + } as SettingsState[K] + } + return undefined as SettingsState[K] +} + +// Mock dependencies +vi.mock('@logger', () => ({ + loggerService: { + withContext: () => ({ + debug: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + info: vi.fn() + }) + } +})) + +vi.mock('@renderer/store/settings', () => ({ + default: (state = { settings: {} }) => state +})) + +vi.mock('@renderer/store/llm', () => ({ + initialState: {}, + default: (state = { llm: {} }) => state +})) + +vi.mock('@renderer/config/constant', () => ({ + DEFAULT_MAX_TOKENS: 4096, + isMac: false, + isWin: false, + TOKENFLUX_HOST: 'mock-host' +})) + +vi.mock('@renderer/utils/provider', () => ({ + isSupportEnableThinkingProvider: vi.fn((provider) => { + return [SystemProviderIds.dashscope, SystemProviderIds.silicon].includes(provider.id) + }) +})) + +vi.mock('@renderer/config/models', async (importOriginal) => { + const actual: any = await importOriginal() + return { + ...actual, + isReasoningModel: vi.fn(() => false), + isOpenAIDeepResearchModel: vi.fn(() => false), + isOpenAIModel: vi.fn(() => false), + isSupportedReasoningEffortOpenAIModel: vi.fn(() => false), + isSupportedThinkingTokenQwenModel: vi.fn(() => false), + isQwenReasoningModel: vi.fn(() => false), + isSupportedThinkingTokenClaudeModel: vi.fn(() => false), + isSupportedThinkingTokenGeminiModel: vi.fn(() => false), + isSupportedThinkingTokenDoubaoModel: vi.fn(() => false), + isSupportedThinkingTokenZhipuModel: vi.fn(() => false), + isSupportedReasoningEffortModel: vi.fn(() => false), + isDeepSeekHybridInferenceModel: vi.fn(() => false), + isSupportedReasoningEffortGrokModel: vi.fn(() => false), + getThinkModelType: vi.fn(() => 'default'), + isDoubaoSeedAfter251015: vi.fn(() => false), + isDoubaoThinkingAutoModel: vi.fn(() => false), + isGrok4FastReasoningModel: vi.fn(() => false), + isGrokReasoningModel: vi.fn(() => false), + isOpenAIReasoningModel: vi.fn(() => false), + isQwenAlwaysThinkModel: vi.fn(() => false), + isSupportedThinkingTokenHunyuanModel: vi.fn(() => false), + isSupportedThinkingTokenModel: vi.fn(() => false), + isGPT51SeriesModel: vi.fn(() => false) + } +}) + +vi.mock('@renderer/hooks/useSettings', () => ({ + getStoreSetting: vi.fn(defaultGetStoreSetting) +})) + +vi.mock('@renderer/services/AssistantService', () => ({ + getAssistantSettings: vi.fn((assistant) => ({ + maxTokens: assistant?.settings?.maxTokens || 4096, + reasoning_effort: assistant?.settings?.reasoning_effort + })), + getProviderByModel: vi.fn((model) => ({ + id: model.provider, + name: 'Test Provider' + })), + getDefaultAssistant: vi.fn(() => ({ + id: 'default', + name: 'Default Assistant', + settings: {} + })) +})) + +const ensureWindowApi = () => { + const globalWindow = window as any + globalWindow.api = globalWindow.api || {} + globalWindow.api.getAppInfo = globalWindow.api.getAppInfo || vi.fn(async () => ({ notesPath: '' })) +} + +ensureWindowApi() + +describe('reasoning utils', () => { + beforeEach(() => { + vi.resetAllMocks() + }) + + describe('getReasoningEffort', () => { + it('should return empty object for non-reasoning model', async () => { + const model: Model = { + id: 'gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({}) + }) + + it('should not override reasoning for OpenRouter when reasoning effort undefined', async () => { + const { isReasoningModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + + const model: Model = { + id: 'anthropic/claude-sonnet-4', + name: 'Claude Sonnet 4', + provider: SystemProviderIds.openrouter + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({}) + }) + + it('should disable reasoning for OpenRouter when reasoning effort explicitly none', async () => { + const { isReasoningModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + + const model: Model = { + id: 'anthropic/claude-sonnet-4', + name: 'Claude Sonnet 4', + provider: SystemProviderIds.openrouter + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'none' + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ reasoning: { enabled: false, exclude: true } }) + }) + + it('should handle Qwen models with enable_thinking', async () => { + const { isReasoningModel, isSupportedThinkingTokenQwenModel, isQwenReasoningModel } = await import( + '@renderer/config/models' + ) + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenQwenModel).mockReturnValue(true) + vi.mocked(isQwenReasoningModel).mockReturnValue(true) + + const model: Model = { + id: 'qwen-plus', + name: 'Qwen Plus', + provider: SystemProviderIds.dashscope + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'medium' + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toHaveProperty('enable_thinking') + }) + + it('should handle Claude models with thinking config', async () => { + const { + isSupportedThinkingTokenClaudeModel, + isReasoningModel, + isQwenReasoningModel, + isSupportedThinkingTokenGeminiModel, + isSupportedThinkingTokenDoubaoModel, + isSupportedThinkingTokenZhipuModel, + isSupportedReasoningEffortModel + } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenClaudeModel).mockReturnValue(true) + vi.mocked(isQwenReasoningModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenGeminiModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenDoubaoModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenZhipuModel).mockReturnValue(false) + vi.mocked(isSupportedReasoningEffortModel).mockReturnValue(false) + + const model: Model = { + id: 'claude-3-7-sonnet', + name: 'Claude 3.7 Sonnet', + provider: SystemProviderIds.anthropic + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high', + maxTokens: 4096 + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ + thinking: { + type: 'enabled', + budget_tokens: expect.any(Number) + } + }) + }) + + it('should handle Gemini Flash models with thinking budget 0', async () => { + const { + isSupportedThinkingTokenGeminiModel, + isReasoningModel, + isQwenReasoningModel, + isSupportedThinkingTokenClaudeModel, + isSupportedThinkingTokenDoubaoModel, + isSupportedThinkingTokenZhipuModel, + isOpenAIDeepResearchModel, + isSupportedThinkingTokenQwenModel, + isSupportedThinkingTokenHunyuanModel, + isDeepSeekHybridInferenceModel + } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIDeepResearchModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenGeminiModel).mockReturnValue(true) + vi.mocked(isQwenReasoningModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenClaudeModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenDoubaoModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenZhipuModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenQwenModel).mockReturnValue(false) + vi.mocked(isSupportedThinkingTokenHunyuanModel).mockReturnValue(false) + vi.mocked(isDeepSeekHybridInferenceModel).mockReturnValue(false) + + const model: Model = { + id: 'gemini-2.5-flash', + name: 'Gemini 2.5 Flash', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'none' + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ + extra_body: { + google: { + thinking_config: { + thinking_budget: 0 + } + } + } + }) + }) + + it('should handle GPT-5.1 reasoning model with effort levels', async () => { + const { + isReasoningModel, + isOpenAIDeepResearchModel, + isSupportedReasoningEffortModel, + isGPT51SeriesModel, + getThinkModelType + } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIDeepResearchModel).mockReturnValue(false) + vi.mocked(isSupportedReasoningEffortModel).mockReturnValue(true) + vi.mocked(getThinkModelType).mockReturnValue('gpt5_1') + vi.mocked(isGPT51SeriesModel).mockReturnValue(true) + + const model: Model = { + id: 'gpt-5.1', + name: 'GPT-5.1', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'none' + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ + reasoningEffort: 'none' + }) + }) + + it('should handle DeepSeek hybrid inference models', async () => { + const { isReasoningModel, isDeepSeekHybridInferenceModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isDeepSeekHybridInferenceModel).mockReturnValue(true) + + const model: Model = { + id: 'deepseek-v3.1', + name: 'DeepSeek V3.1', + provider: SystemProviderIds.silicon + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high' + } + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ + enable_thinking: true + }) + }) + + it('should return medium effort for deep research models', async () => { + const { isReasoningModel, isOpenAIDeepResearchModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIDeepResearchModel).mockReturnValue(true) + + const model: Model = { + id: 'o3-deep-research', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({ reasoning_effort: 'medium' }) + }) + + it('should return empty for groq provider', async () => { + const { getProviderByModel } = await import('@renderer/services/AssistantService') + + vi.mocked(getProviderByModel).mockReturnValue({ + id: 'groq', + name: 'Groq' + } as Provider) + + const model: Model = { + id: 'groq-model', + name: 'Groq Model', + provider: 'groq' + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getReasoningEffort(assistant, model) + expect(result).toEqual({}) + }) + }) + + describe('getOpenAIReasoningParams', () => { + it('should return empty object for non-reasoning model', async () => { + const model: Model = { + id: 'gpt-4', + name: 'GPT-4', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return empty when no reasoning effort set', async () => { + const model: Model = { + id: 'o1-preview', + name: 'O1 Preview', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return reasoning effort for OpenAI models', async () => { + const { isReasoningModel, isOpenAIModel, isSupportedReasoningEffortOpenAIModel } = await import( + '@renderer/config/models' + ) + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIModel).mockReturnValue(true) + vi.mocked(isSupportedReasoningEffortOpenAIModel).mockReturnValue(true) + + const model: Model = { + id: 'gpt-5.1', + name: 'GPT 5.1', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high' + } + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({ + reasoningEffort: 'high', + reasoningSummary: 'auto' + }) + }) + + it('should include reasoning summary when not o1-pro', async () => { + const { isReasoningModel, isOpenAIModel, isSupportedReasoningEffortOpenAIModel } = await import( + '@renderer/config/models' + ) + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIModel).mockReturnValue(true) + vi.mocked(isSupportedReasoningEffortOpenAIModel).mockReturnValue(true) + + const model: Model = { + id: 'gpt-5', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'medium' + } + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({ + reasoningEffort: 'medium', + reasoningSummary: 'auto' + }) + }) + + it('should not include reasoning summary for o1-pro', async () => { + const { isReasoningModel, isOpenAIDeepResearchModel, isSupportedReasoningEffortOpenAIModel } = await import( + '@renderer/config/models' + ) + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIDeepResearchModel).mockReturnValue(false) + vi.mocked(isSupportedReasoningEffortOpenAIModel).mockReturnValue(true) + vi.mocked(getStoreSetting).mockReturnValue({ summaryText: 'off' } as any) + + const model: Model = { + id: 'o1-pro', + name: 'O1 Pro', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high' + } + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({ + reasoningEffort: 'high', + reasoningSummary: undefined + }) + }) + + it('should force medium effort for deep research models', async () => { + const { isReasoningModel, isOpenAIModel, isOpenAIDeepResearchModel, isSupportedReasoningEffortOpenAIModel } = + await import('@renderer/config/models') + const { getStoreSetting } = await import('@renderer/hooks/useSettings') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isOpenAIModel).mockReturnValue(true) + vi.mocked(isOpenAIDeepResearchModel).mockReturnValue(true) + vi.mocked(isSupportedReasoningEffortOpenAIModel).mockReturnValue(true) + vi.mocked(getStoreSetting).mockReturnValue({ summaryText: 'off' } as any) + + const model: Model = { + id: 'o3-deep-research', + name: 'O3 Mini', + provider: SystemProviderIds.openai + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high' + } + } as Assistant + + const result = getOpenAIReasoningParams(assistant, model) + expect(result).toEqual({ + reasoningEffort: 'medium', + reasoningSummary: 'off' + }) + }) + }) + + describe('getAnthropicReasoningParams', () => { + it('should return empty for non-reasoning model', async () => { + const { isReasoningModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(false) + + const model: Model = { + id: 'claude-3-5-sonnet', + name: 'Claude 3.5 Sonnet', + provider: SystemProviderIds.anthropic + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getAnthropicReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return disabled thinking when no reasoning effort', async () => { + const { isReasoningModel, isSupportedThinkingTokenClaudeModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenClaudeModel).mockReturnValue(false) + + const model: Model = { + id: 'claude-3-7-sonnet', + name: 'Claude 3.7 Sonnet', + provider: SystemProviderIds.anthropic + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getAnthropicReasoningParams(assistant, model) + expect(result).toEqual({ + thinking: { + type: 'disabled' + } + }) + }) + + it('should return enabled thinking with budget for Claude models', async () => { + const { isReasoningModel, isSupportedThinkingTokenClaudeModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenClaudeModel).mockReturnValue(true) + + const model: Model = { + id: 'claude-3-7-sonnet', + name: 'Claude 3.7 Sonnet', + provider: SystemProviderIds.anthropic + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'medium', + maxTokens: 4096 + } + } as Assistant + + const result = getAnthropicReasoningParams(assistant, model) + expect(result).toEqual({ + thinking: { + type: 'enabled', + budgetTokens: 2048 + } + }) + }) + }) + + describe('getGeminiReasoningParams', () => { + it('should return empty for non-reasoning model', async () => { + const { isReasoningModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(false) + + const model: Model = { + id: 'gemini-2.0-flash', + name: 'Gemini 2.0 Flash', + provider: SystemProviderIds.gemini + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getGeminiReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should disable thinking for Flash models without reasoning effort', async () => { + const { isReasoningModel, isSupportedThinkingTokenGeminiModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenGeminiModel).mockReturnValue(true) + + const model: Model = { + id: 'gemini-2.5-flash', + name: 'Gemini 2.5 Flash', + provider: SystemProviderIds.gemini + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getGeminiReasoningParams(assistant, model) + expect(result).toEqual({ + thinkingConfig: { + includeThoughts: false, + thinkingBudget: 0 + } + }) + }) + + it('should enable thinking with budget for reasoning effort', async () => { + const { isReasoningModel, isSupportedThinkingTokenGeminiModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenGeminiModel).mockReturnValue(true) + + const model: Model = { + id: 'gemini-2.5-pro', + name: 'Gemini 2.5 Pro', + provider: SystemProviderIds.gemini + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'medium' + } + } as Assistant + + const result = getGeminiReasoningParams(assistant, model) + expect(result).toEqual({ + thinkingConfig: { + thinkingBudget: 16448, + includeThoughts: true + } + }) + }) + + it('should enable thinking without budget for auto effort ratio > 1', async () => { + const { isReasoningModel, isSupportedThinkingTokenGeminiModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenGeminiModel).mockReturnValue(true) + + const model: Model = { + id: 'gemini-2.5-pro', + name: 'Gemini 2.5 Pro', + provider: SystemProviderIds.gemini + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'auto' + } + } as Assistant + + const result = getGeminiReasoningParams(assistant, model) + expect(result).toEqual({ + thinkingConfig: { + includeThoughts: true, + thinkingBudget: -1 + } + }) + }) + }) + + describe('getXAIReasoningParams', () => { + it('should return empty for non-Grok model', async () => { + const { isSupportedReasoningEffortGrokModel } = await import('@renderer/config/models') + + vi.mocked(isSupportedReasoningEffortGrokModel).mockReturnValue(false) + + const model: Model = { + id: 'other-model', + name: 'Other Model', + provider: SystemProviderIds.grok + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getXAIReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return empty when no reasoning effort', async () => { + const { isSupportedReasoningEffortGrokModel } = await import('@renderer/config/models') + + vi.mocked(isSupportedReasoningEffortGrokModel).mockReturnValue(true) + + const model: Model = { + id: 'grok-2', + name: 'Grok 2', + provider: SystemProviderIds.grok + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getXAIReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return reasoning effort for Grok models', async () => { + const { isSupportedReasoningEffortGrokModel } = await import('@renderer/config/models') + + vi.mocked(isSupportedReasoningEffortGrokModel).mockReturnValue(true) + + const model: Model = { + id: 'grok-3', + name: 'Grok 3', + provider: SystemProviderIds.grok + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'high' + } + } as Assistant + + const result = getXAIReasoningParams(assistant, model) + expect(result).toHaveProperty('reasoningEffort') + expect(result.reasoningEffort).toBe('high') + }) + }) + + describe('getBedrockReasoningParams', () => { + it('should return empty for non-reasoning model', async () => { + const model: Model = { + id: 'other-model', + name: 'Other Model', + provider: 'bedrock' + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getBedrockReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return empty when no reasoning effort', async () => { + const model: Model = { + id: 'claude-3-7-sonnet', + name: 'Claude 3.7 Sonnet', + provider: 'bedrock' + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getBedrockReasoningParams(assistant, model) + expect(result).toEqual({}) + }) + + it('should return reasoning config for Claude models on Bedrock', async () => { + const { isReasoningModel, isSupportedThinkingTokenClaudeModel } = await import('@renderer/config/models') + + vi.mocked(isReasoningModel).mockReturnValue(true) + vi.mocked(isSupportedThinkingTokenClaudeModel).mockReturnValue(true) + + const model: Model = { + id: 'claude-3-7-sonnet', + name: 'Claude 3.7 Sonnet', + provider: 'bedrock' + } as Model + + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + reasoning_effort: 'medium', + maxTokens: 4096 + } + } as Assistant + + const result = getBedrockReasoningParams(assistant, model) + expect(result).toEqual({ + reasoningConfig: { + type: 'enabled', + budgetTokens: 2048 + } + }) + }) + }) + + describe('getCustomParameters', () => { + it('should return empty object when no custom parameters', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: {} + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({}) + }) + + it('should return custom parameters as key-value pairs', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + customParameters: [ + { name: 'param1', value: 'value1', type: 'string' }, + { name: 'param2', value: 123, type: 'number' } + ] + } + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({ + param1: 'value1', + param2: 123 + }) + }) + + it('should parse JSON type parameters', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + customParameters: [{ name: 'config', value: '{"key": "value"}', type: 'json' }] + } + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({ + config: { key: 'value' } + }) + }) + + it('should handle invalid JSON gracefully', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + customParameters: [{ name: 'invalid', value: '{invalid json', type: 'json' }] + } + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({ + invalid: '{invalid json' + }) + }) + + it('should handle undefined JSON value', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + customParameters: [{ name: 'undef', value: 'undefined', type: 'json' }] + } + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({ + undef: undefined + }) + }) + + it('should skip parameters with empty names', async () => { + const assistant: Assistant = { + id: 'test', + name: 'Test', + settings: { + customParameters: [ + { name: '', value: 'value1', type: 'string' }, + { name: ' ', value: 'value2', type: 'string' }, + { name: 'valid', value: 'value3', type: 'string' } + ] + } + } as Assistant + + const result = getCustomParameters(assistant) + expect(result).toEqual({ + valid: 'value3' + }) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/__tests__/websearch.test.ts b/src/renderer/src/aiCore/utils/__tests__/websearch.test.ts new file mode 100644 index 0000000000..fa5e3c3b36 --- /dev/null +++ b/src/renderer/src/aiCore/utils/__tests__/websearch.test.ts @@ -0,0 +1,384 @@ +/** + * websearch.ts Unit Tests + * Tests for web search parameters generation utilities + */ + +import type { CherryWebSearchConfig } from '@renderer/store/websearch' +import type { Model } from '@renderer/types' +import { describe, expect, it, vi } from 'vitest' + +import { buildProviderBuiltinWebSearchConfig, getWebSearchParams } from '../websearch' + +// Mock dependencies +vi.mock('@renderer/config/models', () => ({ + isOpenAIWebSearchChatCompletionOnlyModel: vi.fn((model) => model?.id?.includes('o1-pro') ?? false), + isOpenAIDeepResearchModel: vi.fn((model) => model?.id?.includes('o3-mini') ?? false) +})) + +vi.mock('@renderer/utils/blacklistMatchPattern', () => ({ + mapRegexToPatterns: vi.fn((patterns) => patterns || []) +})) + +describe('websearch utils', () => { + describe('getWebSearchParams', () => { + it('should return enhancement params for hunyuan provider', () => { + const model: Model = { + id: 'hunyuan-model', + name: 'Hunyuan Model', + provider: 'hunyuan' + } as Model + + const result = getWebSearchParams(model) + + expect(result).toEqual({ + enable_enhancement: true, + citation: true, + search_info: true + }) + }) + + it('should return search params for dashscope provider', () => { + const model: Model = { + id: 'qwen-model', + name: 'Qwen Model', + provider: 'dashscope' + } as Model + + const result = getWebSearchParams(model) + + expect(result).toEqual({ + enable_search: true, + search_options: { + forced_search: true + } + }) + }) + + it('should return web_search_options for OpenAI web search models', () => { + const model: Model = { + id: 'o1-pro', + name: 'O1 Pro', + provider: 'openai' + } as Model + + const result = getWebSearchParams(model) + + expect(result).toEqual({ + web_search_options: {} + }) + }) + + it('should return empty object for other providers', () => { + const model: Model = { + id: 'gpt-4', + name: 'GPT-4', + provider: 'openai' + } as Model + + const result = getWebSearchParams(model) + + expect(result).toEqual({}) + }) + + it('should return empty object for custom provider', () => { + const model: Model = { + id: 'custom-model', + name: 'Custom Model', + provider: 'custom-provider' + } as Model + + const result = getWebSearchParams(model) + + expect(result).toEqual({}) + }) + }) + + describe('buildProviderBuiltinWebSearchConfig', () => { + const defaultWebSearchConfig: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 50, + excludeDomains: [] + } + + describe('openai provider', () => { + it('should return low search context size for low maxResults', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 20, + excludeDomains: [] + } + + const result = buildProviderBuiltinWebSearchConfig('openai', config) + + expect(result).toEqual({ + openai: { + searchContextSize: 'low' + } + }) + }) + + it('should return medium search context size for medium maxResults', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 50, + excludeDomains: [] + } + + const result = buildProviderBuiltinWebSearchConfig('openai', config) + + expect(result).toEqual({ + openai: { + searchContextSize: 'medium' + } + }) + }) + + it('should return high search context size for high maxResults', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 80, + excludeDomains: [] + } + + const result = buildProviderBuiltinWebSearchConfig('openai', config) + + expect(result).toEqual({ + openai: { + searchContextSize: 'high' + } + }) + }) + + it('should use medium for deep research models regardless of maxResults', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 100, + excludeDomains: [] + } + + const model: Model = { + id: 'o3-mini', + name: 'O3 Mini', + provider: 'openai' + } as Model + + const result = buildProviderBuiltinWebSearchConfig('openai', config, model) + + expect(result).toEqual({ + openai: { + searchContextSize: 'medium' + } + }) + }) + }) + + describe('openai-chat provider', () => { + it('should return correct search context size', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 50, + excludeDomains: [] + } + + const result = buildProviderBuiltinWebSearchConfig('openai-chat', config) + + expect(result).toEqual({ + 'openai-chat': { + searchContextSize: 'medium' + } + }) + }) + + it('should handle deep research models', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 100, + excludeDomains: [] + } + + const model: Model = { + id: 'o3-mini', + name: 'O3 Mini', + provider: 'openai' + } as Model + + const result = buildProviderBuiltinWebSearchConfig('openai-chat', config, model) + + expect(result).toEqual({ + 'openai-chat': { + searchContextSize: 'medium' + } + }) + }) + }) + + describe('anthropic provider', () => { + it('should return anthropic search options with maxUses', () => { + const result = buildProviderBuiltinWebSearchConfig('anthropic', defaultWebSearchConfig) + + expect(result).toEqual({ + anthropic: { + maxUses: 50, + blockedDomains: undefined + } + }) + }) + + it('should include blockedDomains when excludeDomains provided', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 30, + excludeDomains: ['example.com', 'test.com'] + } + + const result = buildProviderBuiltinWebSearchConfig('anthropic', config) + + expect(result).toEqual({ + anthropic: { + maxUses: 30, + blockedDomains: ['example.com', 'test.com'] + } + }) + }) + + it('should not include blockedDomains when empty', () => { + const result = buildProviderBuiltinWebSearchConfig('anthropic', defaultWebSearchConfig) + + expect(result).toEqual({ + anthropic: { + maxUses: 50, + blockedDomains: undefined + } + }) + }) + }) + + describe('xai provider', () => { + it('should return xai search options', () => { + const result = buildProviderBuiltinWebSearchConfig('xai', defaultWebSearchConfig) + + expect(result).toEqual({ + xai: { + maxSearchResults: 50, + returnCitations: true, + sources: [{ type: 'web', excludedWebsites: [] }, { type: 'news' }, { type: 'x' }], + mode: 'on' + } + }) + }) + + it('should limit excluded websites to 5', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 40, + excludeDomains: ['site1.com', 'site2.com', 'site3.com', 'site4.com', 'site5.com', 'site6.com', 'site7.com'] + } + + const result = buildProviderBuiltinWebSearchConfig('xai', config) + + expect(result?.xai?.sources).toBeDefined() + const webSource = result?.xai?.sources?.[0] + if (webSource && webSource.type === 'web') { + expect(webSource.excludedWebsites).toHaveLength(5) + } + }) + + it('should include all sources types', () => { + const result = buildProviderBuiltinWebSearchConfig('xai', defaultWebSearchConfig) + + expect(result?.xai?.sources).toHaveLength(3) + expect(result?.xai?.sources?.[0].type).toBe('web') + expect(result?.xai?.sources?.[1].type).toBe('news') + expect(result?.xai?.sources?.[2].type).toBe('x') + }) + }) + + describe('openrouter provider', () => { + it('should return openrouter plugins config', () => { + const result = buildProviderBuiltinWebSearchConfig('openrouter', defaultWebSearchConfig) + + expect(result).toEqual({ + openrouter: { + plugins: [ + { + id: 'web', + max_results: 50 + } + ] + } + }) + }) + + it('should respect custom maxResults', () => { + const config: CherryWebSearchConfig = { + searchWithTime: true, + maxResults: 75, + excludeDomains: [] + } + + const result = buildProviderBuiltinWebSearchConfig('openrouter', config) + + expect(result).toEqual({ + openrouter: { + plugins: [ + { + id: 'web', + max_results: 75 + } + ] + } + }) + }) + }) + + describe('unsupported provider', () => { + it('should return empty object for unsupported provider', () => { + const result = buildProviderBuiltinWebSearchConfig('unsupported' as any, defaultWebSearchConfig) + + expect(result).toEqual({}) + }) + + it('should return empty object for google provider', () => { + const result = buildProviderBuiltinWebSearchConfig('google', defaultWebSearchConfig) + + expect(result).toEqual({}) + }) + }) + + describe('edge cases', () => { + it('should handle maxResults at boundary values', () => { + // Test boundary at 33 (low/medium) + const config33: CherryWebSearchConfig = { searchWithTime: true, maxResults: 33, excludeDomains: [] } + const result33 = buildProviderBuiltinWebSearchConfig('openai', config33) + expect(result33?.openai?.searchContextSize).toBe('low') + + // Test boundary at 34 (medium) + const config34: CherryWebSearchConfig = { searchWithTime: true, maxResults: 34, excludeDomains: [] } + const result34 = buildProviderBuiltinWebSearchConfig('openai', config34) + expect(result34?.openai?.searchContextSize).toBe('medium') + + // Test boundary at 66 (medium) + const config66: CherryWebSearchConfig = { searchWithTime: true, maxResults: 66, excludeDomains: [] } + const result66 = buildProviderBuiltinWebSearchConfig('openai', config66) + expect(result66?.openai?.searchContextSize).toBe('medium') + + // Test boundary at 67 (high) + const config67: CherryWebSearchConfig = { searchWithTime: true, maxResults: 67, excludeDomains: [] } + const result67 = buildProviderBuiltinWebSearchConfig('openai', config67) + expect(result67?.openai?.searchContextSize).toBe('high') + }) + + it('should handle zero maxResults', () => { + const config: CherryWebSearchConfig = { searchWithTime: true, maxResults: 0, excludeDomains: [] } + const result = buildProviderBuiltinWebSearchConfig('openai', config) + expect(result?.openai?.searchContextSize).toBe('low') + }) + + it('should handle very large maxResults', () => { + const config: CherryWebSearchConfig = { searchWithTime: true, maxResults: 1000, excludeDomains: [] } + const result = buildProviderBuiltinWebSearchConfig('openai', config) + expect(result?.openai?.searchContextSize).toBe('high') + }) + }) + }) +}) diff --git a/src/renderer/src/aiCore/utils/mcp.ts b/src/renderer/src/aiCore/utils/mcp.ts index 84bc661aa0..7d3be9ac96 100644 --- a/src/renderer/src/aiCore/utils/mcp.ts +++ b/src/renderer/src/aiCore/utils/mcp.ts @@ -28,7 +28,9 @@ export function convertMcpToolsToAiSdkTools(mcpTools: MCPTool[]): ToolSet { const tools: ToolSet = {} for (const mcpTool of mcpTools) { - tools[mcpTool.name] = tool({ + // Use mcpTool.id (which includes serverId suffix) to ensure uniqueness + // when multiple instances of the same MCP server type are configured + tools[mcpTool.id] = tool({ description: mcpTool.description || `Tool from ${mcpTool.serverName}`, inputSchema: jsonSchema(mcpTool.inputSchema as JSONSchema7), execute: async (params, { toolCallId }) => { diff --git a/src/renderer/src/aiCore/utils/options.ts b/src/renderer/src/aiCore/utils/options.ts index eacb1b9e05..fd9bc590cd 100644 --- a/src/renderer/src/aiCore/utils/options.ts +++ b/src/renderer/src/aiCore/utils/options.ts @@ -1,25 +1,48 @@ +import type { BedrockProviderOptions } from '@ai-sdk/amazon-bedrock' +import { type AnthropicProviderOptions } from '@ai-sdk/anthropic' +import type { GoogleGenerativeAIProviderOptions } from '@ai-sdk/google' +import type { OpenAIResponsesProviderOptions } from '@ai-sdk/openai' +import type { XaiProviderOptions } from '@ai-sdk/xai' import { baseProviderIdSchema, customProviderIdSchema } from '@cherrystudio/ai-core/provider' import { loggerService } from '@logger' import { getModelSupportedVerbosity, + isAnthropicModel, + isGeminiModel, + isGrokModel, isOpenAIModel, + isOpenAIOpenWeightModel, isQwenMTModel, isSupportFlexServiceTierModel, isSupportVerbosityModel } from '@renderer/config/models' -import { isSupportServiceTierProvider } from '@renderer/config/providers' import { mapLanguageToQwenMTModel } from '@renderer/config/translate' -import type { Assistant, Model, Provider } from '@renderer/types' +import { getStoreSetting } from '@renderer/hooks/useSettings' +import { getProviderById } from '@renderer/services/ProviderService' import { + type Assistant, + type GroqServiceTier, GroqServiceTiers, + type GroqSystemProvider, isGroqServiceTier, + isGroqSystemProvider, isOpenAIServiceTier, isTranslateAssistant, + type Model, + type NotGroqProvider, + type OpenAIServiceTier, OpenAIServiceTiers, + type Provider, + type ServiceTier, SystemProviderIds } from '@renderer/types' +import { type AiSdkParam, isAiSdkParam, type OpenAIVerbosity } from '@renderer/types/aiCoreTypes' +import { isSupportServiceTierProvider, isSupportVerbosityProvider } from '@renderer/utils/provider' +import type { JSONValue } from 'ai' import { t } from 'i18next' +import type { OllamaCompletionProviderOptions } from 'ollama-ai-provider-v2' +import { addAnthropicHeaders } from '../prepareParams/header' import { getAiSdkProviderId } from '../provider/factory' import { buildGeminiGenerateImageParams } from './image' import { @@ -35,8 +58,31 @@ import { getWebSearchParams } from './websearch' const logger = loggerService.withContext('aiCore.utils.options') -// copy from BaseApiClient.ts -const getServiceTier = (model: Model, provider: Provider) => { +function toOpenAIServiceTier(model: Model, serviceTier: ServiceTier): OpenAIServiceTier { + if ( + !isOpenAIServiceTier(serviceTier) || + (serviceTier === OpenAIServiceTiers.flex && !isSupportFlexServiceTierModel(model)) + ) { + return undefined + } else { + return serviceTier + } +} + +function toGroqServiceTier(model: Model, serviceTier: ServiceTier): GroqServiceTier { + if ( + !isGroqServiceTier(serviceTier) || + (serviceTier === GroqServiceTiers.flex && !isSupportFlexServiceTierModel(model)) + ) { + return undefined + } else { + return serviceTier + } +} + +function getServiceTier(model: Model, provider: T): GroqServiceTier +function getServiceTier(model: Model, provider: T): OpenAIServiceTier +function getServiceTier(model: Model, provider: T): OpenAIServiceTier | GroqServiceTier { const serviceTierSetting = provider.serviceTier if (!isSupportServiceTierProvider(provider) || !isOpenAIModel(model) || !serviceTierSetting) { @@ -44,30 +90,64 @@ const getServiceTier = (model: Model, provider: Provider) => { } // 处理不同供应商需要 fallback 到默认值的情况 - if (provider.id === SystemProviderIds.groq) { - if ( - !isGroqServiceTier(serviceTierSetting) || - (serviceTierSetting === GroqServiceTiers.flex && !isSupportFlexServiceTierModel(model)) - ) { - return undefined - } + if (isGroqSystemProvider(provider)) { + return toGroqServiceTier(model, serviceTierSetting) } else { // 其他 OpenAI 供应商,假设他们的服务层级设置和 OpenAI 完全相同 - if ( - !isOpenAIServiceTier(serviceTierSetting) || - (serviceTierSetting === OpenAIServiceTiers.flex && !isSupportFlexServiceTierModel(model)) - ) { - return undefined + return toOpenAIServiceTier(model, serviceTierSetting) + } +} + +function getVerbosity(model: Model): OpenAIVerbosity { + if (!isSupportVerbosityModel(model) || !isSupportVerbosityProvider(getProviderById(model.provider)!)) { + return undefined + } + const openAI = getStoreSetting('openAI') + + const userVerbosity = openAI.verbosity + + if (userVerbosity) { + const supportedVerbosity = getModelSupportedVerbosity(model) + // Use user's verbosity if supported, otherwise use the first supported option + const verbosity = supportedVerbosity.includes(userVerbosity) ? userVerbosity : supportedVerbosity[0] + return verbosity + } + return undefined +} + +/** + * Extract AI SDK standard parameters from custom parameters + * These parameters should be passed directly to streamText() instead of providerOptions + */ +export function extractAiSdkStandardParams(customParams: Record): { + standardParams: Partial> + providerParams: Record +} { + const standardParams: Partial> = {} + const providerParams: Record = {} + + for (const [key, value] of Object.entries(customParams)) { + if (isAiSdkParam(key)) { + standardParams[key] = value + } else { + providerParams[key] = value } } - return serviceTierSetting + return { standardParams, providerParams } } /** * 构建 AI SDK 的 providerOptions * 按 provider 类型分离,保持类型安全 - * 返回格式:{ 'providerId': providerOptions } + * 返回格式:{ + * providerOptions: { 'providerId': providerOptions }, + * standardParams: { topK, frequencyPenalty, presencePenalty, stopSequences, seed } + * } + * + * Custom parameters are split into two categories: + * 1. AI SDK standard parameters (topK, frequencyPenalty, etc.) - returned separately to be passed to streamText() + * 2. Provider-specific parameters - merged into providerOptions */ export function buildProviderOptions( assistant: Assistant, @@ -78,13 +158,16 @@ export function buildProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean } -): Record { - logger.debug('buildProviderOptions', { assistant, model, actualProvider, capabilities }) +): { + providerOptions: Record> + standardParams: Partial> +} { const rawProviderId = getAiSdkProviderId(actualProvider) + logger.debug('buildProviderOptions', { assistant, model, actualProvider, capabilities, rawProviderId }) // 构建 provider 特定的选项 let providerSpecificOptions: Record = {} - const serviceTierSetting = getServiceTier(model, actualProvider) - providerSpecificOptions.serviceTier = serviceTierSetting + const serviceTier = getServiceTier(model, actualProvider) + const textVerbosity = getVerbosity(model) // 根据 provider 类型分离构建逻辑 const { data: baseProviderId, success } = baseProviderIdSchema.safeParse(rawProviderId) if (success) { @@ -94,14 +177,16 @@ export function buildProviderOptions( case 'openai-chat': case 'azure': case 'azure-responses': - providerSpecificOptions = { - ...buildOpenAIProviderOptions(assistant, model, capabilities), - serviceTier: serviceTierSetting + { + providerSpecificOptions = buildOpenAIProviderOptions( + assistant, + model, + capabilities, + serviceTier, + textVerbosity + ) } break - case 'huggingface': - providerSpecificOptions = buildOpenAIProviderOptions(assistant, model, capabilities) - break case 'anthropic': providerSpecificOptions = buildAnthropicProviderOptions(assistant, model, capabilities) break @@ -117,14 +202,25 @@ export function buildProviderOptions( case 'openrouter': case 'openai-compatible': { // 对于其他 provider,使用通用的构建逻辑 + const genericOptions = buildGenericProviderOptions(rawProviderId, assistant, model, capabilities) providerSpecificOptions = { - ...buildGenericProviderOptions(assistant, model, capabilities), - serviceTier: serviceTierSetting + [rawProviderId]: { + ...genericOptions[rawProviderId], + serviceTier, + textVerbosity + } } break } case 'cherryin': - providerSpecificOptions = buildCherryInProviderOptions(assistant, model, capabilities, actualProvider) + providerSpecificOptions = buildCherryInProviderOptions( + assistant, + model, + capabilities, + actualProvider, + serviceTier, + textVerbosity + ) break default: throw new Error(`Unsupported base provider ${baseProviderId}`) @@ -138,44 +234,119 @@ export function buildProviderOptions( case 'google-vertex': providerSpecificOptions = buildGeminiProviderOptions(assistant, model, capabilities) break + case 'azure-anthropic': case 'google-vertex-anthropic': providerSpecificOptions = buildAnthropicProviderOptions(assistant, model, capabilities) break case 'bedrock': providerSpecificOptions = buildBedrockProviderOptions(assistant, model, capabilities) break + case 'huggingface': + providerSpecificOptions = buildOpenAIProviderOptions(assistant, model, capabilities, serviceTier) + break + case SystemProviderIds.ollama: + providerSpecificOptions = buildOllamaProviderOptions(assistant, model, capabilities) + break + case SystemProviderIds.gateway: + providerSpecificOptions = buildAIGatewayOptions(assistant, model, capabilities, serviceTier, textVerbosity) + break default: // 对于其他 provider,使用通用的构建逻辑 + providerSpecificOptions = buildGenericProviderOptions(rawProviderId, assistant, model, capabilities) + // Merge serviceTier and textVerbosity providerSpecificOptions = { - ...buildGenericProviderOptions(assistant, model, capabilities), - serviceTier: serviceTierSetting + ...providerSpecificOptions, + [rawProviderId]: { + ...providerSpecificOptions[rawProviderId], + serviceTier, + textVerbosity + } } } } else { throw error } } + logger.debug('Built providerSpecificOptions', { providerSpecificOptions }) + /** + * Retrieve custom parameters and separate standard parameters from provider-specific parameters. + */ + const customParams = getCustomParameters(assistant) + const { standardParams, providerParams } = extractAiSdkStandardParams(customParams) + logger.debug('Extracted standardParams and providerParams', { standardParams, providerParams }) - // 合并自定义参数到 provider 特定的选项中 - providerSpecificOptions = { - ...providerSpecificOptions, - ...getCustomParameters(assistant) + /** + * Get the actual AI SDK provider ID(s) from the already-built providerSpecificOptions. + * For proxy providers (cherryin, aihubmix, newapi), this will be the actual SDK provider (e.g., 'google', 'openai', 'anthropic') + * For regular providers, this will be the provider itself + */ + const actualAiSdkProviderIds = Object.keys(providerSpecificOptions) + const primaryAiSdkProviderId = actualAiSdkProviderIds[0] // Use the first one as primary for non-scoped params + + /** + * Merge custom parameters into providerSpecificOptions. + * Simple logic: + * 1. If key is in actualAiSdkProviderIds → merge directly (user knows the actual AI SDK provider ID) + * 2. If key == rawProviderId: + * - If it's gateway/ollama → preserve (they need their own config for routing/options) + * - Otherwise → map to primary (this is a proxy provider like cherryin) + * 3. Otherwise → treat as regular parameter, merge to primary provider + * + * Example: + * - User writes `cherryin: { opt: 'val' }` → mapped to `google: { opt: 'val' }` (case 2, proxy) + * - User writes `gateway: { order: [...] }` → stays as `gateway: { order: [...] }` (case 2, routing config) + * - User writes `google: { opt: 'val' }` → stays as `google: { opt: 'val' }` (case 1) + * - User writes `customKey: 'val'` → merged to `google: { customKey: 'val' }` (case 3) + */ + for (const key of Object.keys(providerParams)) { + if (actualAiSdkProviderIds.includes(key)) { + // Case 1: Key is an actual AI SDK provider ID - merge directly + providerSpecificOptions = { + ...providerSpecificOptions, + [key]: { + ...providerSpecificOptions[key], + ...providerParams[key] + } + } + } else if (key === rawProviderId && !actualAiSdkProviderIds.includes(rawProviderId)) { + // Case 2: Key is the current provider (not in actualAiSdkProviderIds, so it's a proxy or special provider) + // Gateway is special: it needs routing config preserved + if (key === SystemProviderIds.gateway) { + // Preserve gateway config for routing + providerSpecificOptions = { + ...providerSpecificOptions, + [key]: { + ...providerSpecificOptions[key], + ...providerParams[key] + } + } + } else { + // Proxy provider (cherryin, etc.) - map to actual AI SDK provider + providerSpecificOptions = { + ...providerSpecificOptions, + [primaryAiSdkProviderId]: { + ...providerSpecificOptions[primaryAiSdkProviderId], + ...providerParams[key] + } + } + } + } else { + // Case 3: Regular parameter - merge to primary provider + providerSpecificOptions = { + ...providerSpecificOptions, + [primaryAiSdkProviderId]: { + ...providerSpecificOptions[primaryAiSdkProviderId], + [key]: providerParams[key] + } + } + } } + logger.debug('Final providerSpecificOptions after merging providerParams', { providerSpecificOptions }) - let rawProviderKey = - { - 'google-vertex': 'google', - 'google-vertex-anthropic': 'anthropic', - 'ai-gateway': 'gateway' - }[rawProviderId] || rawProviderId - - if (rawProviderKey === 'cherryin') { - rawProviderKey = { gemini: 'google' }[actualProvider.type] || actualProvider.type - } - - // 返回 AI Core SDK 要求的格式:{ 'providerId': providerOptions } + // 返回 AI Core SDK 要求的格式:{ 'providerId': providerOptions } 以及提取的标准参数 return { - [rawProviderKey]: providerSpecificOptions + providerOptions: providerSpecificOptions, + standardParams } } @@ -189,10 +360,12 @@ function buildOpenAIProviderOptions( enableReasoning: boolean enableWebSearch: boolean enableGenerateImage: boolean - } -): Record { + }, + serviceTier: OpenAIServiceTier, + textVerbosity?: OpenAIVerbosity +): Record { const { enableReasoning } = capabilities - let providerOptions: Record = {} + let providerOptions: OpenAIResponsesProviderOptions = {} // OpenAI 推理参数 if (enableReasoning) { const reasoningParams = getOpenAIReasoningParams(assistant, model) @@ -201,10 +374,15 @@ function buildOpenAIProviderOptions( ...reasoningParams } } + const provider = getProviderById(model.provider) - if (isSupportVerbosityModel(model)) { - const state = window.store?.getState() - const userVerbosity = state?.settings?.openAI?.verbosity + if (!provider) { + throw new Error(`Provider ${model.provider} not found`) + } + + if (isSupportVerbosityModel(model) && isSupportVerbosityProvider(provider)) { + const openAI = getStoreSetting<'openAI'>('openAI') + const userVerbosity = openAI?.verbosity if (userVerbosity && ['low', 'medium', 'high'].includes(userVerbosity)) { const supportedVerbosity = getModelSupportedVerbosity(model) @@ -218,7 +396,15 @@ function buildOpenAIProviderOptions( } } - return providerOptions + providerOptions = { + ...providerOptions, + serviceTier, + textVerbosity + } + + return { + openai: providerOptions + } } /** @@ -232,9 +418,9 @@ function buildAnthropicProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean } -): Record { +): Record { const { enableReasoning } = capabilities - let providerOptions: Record = {} + let providerOptions: AnthropicProviderOptions = {} // Anthropic 推理参数 if (enableReasoning) { @@ -245,7 +431,11 @@ function buildAnthropicProviderOptions( } } - return providerOptions + return { + anthropic: { + ...providerOptions + } + } } /** @@ -259,9 +449,9 @@ function buildGeminiProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean } -): Record { +): Record { const { enableReasoning, enableGenerateImage } = capabilities - let providerOptions: Record = {} + let providerOptions: GoogleGenerativeAIProviderOptions = {} // Gemini 推理参数 if (enableReasoning) { @@ -279,7 +469,11 @@ function buildGeminiProviderOptions( } } - return providerOptions + return { + google: { + ...providerOptions + } + } } function buildXAIProviderOptions( @@ -290,7 +484,7 @@ function buildXAIProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean } -): Record { +): Record { const { enableReasoning } = capabilities let providerOptions: Record = {} @@ -302,7 +496,11 @@ function buildXAIProviderOptions( } } - return providerOptions + return { + xai: { + ...providerOptions + } + } } function buildCherryInProviderOptions( @@ -313,24 +511,23 @@ function buildCherryInProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean }, - actualProvider: Provider -): Record { - const serviceTierSetting = getServiceTier(model, actualProvider) - + actualProvider: Provider, + serviceTier: OpenAIServiceTier, + textVerbosity: OpenAIVerbosity +): Record { switch (actualProvider.type) { case 'openai': - return { - ...buildOpenAIProviderOptions(assistant, model, capabilities), - serviceTier: serviceTierSetting - } - + return buildGenericProviderOptions('cherryin', assistant, model, capabilities) + case 'openai-response': + return buildOpenAIProviderOptions(assistant, model, capabilities, serviceTier, textVerbosity) case 'anthropic': return buildAnthropicProviderOptions(assistant, model, capabilities) - case 'gemini': return buildGeminiProviderOptions(assistant, model, capabilities) + + default: + return buildGenericProviderOptions('cherryin', assistant, model, capabilities) } - return {} } /** @@ -344,9 +541,9 @@ function buildBedrockProviderOptions( enableWebSearch: boolean enableGenerateImage: boolean } -): Record { +): Record { const { enableReasoning } = capabilities - let providerOptions: Record = {} + let providerOptions: BedrockProviderOptions = {} if (enableReasoning) { const reasoningParams = getBedrockReasoningParams(assistant, model) @@ -356,13 +553,46 @@ function buildBedrockProviderOptions( } } - return providerOptions + const betaHeaders = addAnthropicHeaders(assistant, model) + if (betaHeaders.length > 0) { + providerOptions.anthropicBeta = betaHeaders + } + + return { + bedrock: providerOptions + } +} + +function buildOllamaProviderOptions( + assistant: Assistant, + model: Model, + capabilities: { + enableReasoning: boolean + enableWebSearch: boolean + enableGenerateImage: boolean + } +): Record { + const { enableReasoning } = capabilities + const providerOptions: OllamaCompletionProviderOptions = {} + const reasoningEffort = assistant.settings?.reasoning_effort + if (enableReasoning) { + if (isOpenAIOpenWeightModel(model)) { + // @ts-ignore upstream type error + providerOptions.think = reasoningEffort as any + } else { + providerOptions.think = !['none', undefined].includes(reasoningEffort) + } + } + return { + ollama: providerOptions + } } /** * 构建通用的 providerOptions(用于其他 provider) */ function buildGenericProviderOptions( + providerId: string, assistant: Assistant, model: Model, capabilities: { @@ -405,5 +635,37 @@ function buildGenericProviderOptions( } } - return providerOptions + return { + [providerId]: providerOptions + } +} + +function buildAIGatewayOptions( + assistant: Assistant, + model: Model, + capabilities: { + enableReasoning: boolean + enableWebSearch: boolean + enableGenerateImage: boolean + }, + serviceTier: OpenAIServiceTier, + textVerbosity?: OpenAIVerbosity +): Record< + string, + | OpenAIResponsesProviderOptions + | AnthropicProviderOptions + | GoogleGenerativeAIProviderOptions + | Record +> { + if (isAnthropicModel(model)) { + return buildAnthropicProviderOptions(assistant, model, capabilities) + } else if (isOpenAIModel(model)) { + return buildOpenAIProviderOptions(assistant, model, capabilities, serviceTier, textVerbosity) + } else if (isGeminiModel(model)) { + return buildGeminiProviderOptions(assistant, model, capabilities) + } else if (isGrokModel(model)) { + return buildXAIProviderOptions(assistant, model, capabilities) + } else { + return buildGenericProviderOptions('openai-compatible', assistant, model, capabilities) + } } diff --git a/src/renderer/src/aiCore/utils/reasoning.ts b/src/renderer/src/aiCore/utils/reasoning.ts index dfe084179c..996d676761 100644 --- a/src/renderer/src/aiCore/utils/reasoning.ts +++ b/src/renderer/src/aiCore/utils/reasoning.ts @@ -1,6 +1,7 @@ import type { BedrockProviderOptions } from '@ai-sdk/amazon-bedrock' import type { AnthropicProviderOptions } from '@ai-sdk/anthropic' import type { GoogleGenerativeAIProviderOptions } from '@ai-sdk/google' +import type { OpenAIResponsesProviderOptions } from '@ai-sdk/openai' import type { XaiProviderOptions } from '@ai-sdk/xai' import { loggerService } from '@logger' import { DEFAULT_MAX_TOKENS } from '@renderer/config/constant' @@ -11,9 +12,9 @@ import { isDeepSeekHybridInferenceModel, isDoubaoSeedAfter251015, isDoubaoThinkingAutoModel, + isGemini3ThinkingTokenModel, isGPT51SeriesModel, isGrok4FastReasoningModel, - isGrokReasoningModel, isOpenAIDeepResearchModel, isOpenAIModel, isOpenAIReasoningModel, @@ -32,13 +33,13 @@ import { isSupportedThinkingTokenZhipuModel, MODEL_SUPPORTED_REASONING_EFFORT } from '@renderer/config/models' -import { isSupportEnableThinkingProvider } from '@renderer/config/providers' import { getStoreSetting } from '@renderer/hooks/useSettings' import { getAssistantSettings, getProviderByModel } from '@renderer/services/AssistantService' -import type { SettingsState } from '@renderer/store/settings' import type { Assistant, Model } from '@renderer/types' import { EFFORT_RATIO, isSystemProvider, SystemProviderIds } from '@renderer/types' +import type { OpenAIReasoningSummary } from '@renderer/types/aiCoreTypes' import type { ReasoningEffortOptionalParams } from '@renderer/types/sdk' +import { isSupportEnableThinkingProvider } from '@renderer/utils/provider' import { toInteger } from 'lodash' const logger = loggerService.withContext('reasoning') @@ -61,30 +62,22 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin } const reasoningEffort = assistant?.settings?.reasoning_effort - // Handle undefined and 'none' reasoningEffort. - // TODO: They should be separated. - if (!reasoningEffort || reasoningEffort === 'none') { + // reasoningEffort is not set, no extra reasoning setting + // Generally, for every model which supports reasoning control, the reasoning effort won't be undefined. + // It's for some reasoning models that don't support reasoning control, such as deepseek reasoner. + if (!reasoningEffort) { + return {} + } + + // Handle 'none' reasoningEffort. It's explicitly off. + if (reasoningEffort === 'none') { // openrouter: use reasoning if (model.provider === SystemProviderIds.openrouter) { - // Don't disable reasoning for Gemini models that support thinking tokens - if (isSupportedThinkingTokenGeminiModel(model) && !GEMINI_FLASH_MODEL_REGEX.test(model.id)) { - return {} - } // 'none' is not an available value for effort for now. // I think they should resolve this issue soon, so I'll just go ahead and use this value. if (isGPT51SeriesModel(model) && reasoningEffort === 'none') { return { reasoning: { effort: 'none' } } } - // Don't disable reasoning for models that require it - if ( - isGrokReasoningModel(model) || - isOpenAIReasoningModel(model) || - isQwenAlwaysThinkModel(model) || - model.id.includes('seed-oss') || - model.id.includes('minimax-m2') - ) { - return {} - } return { reasoning: { enabled: false, exclude: true } } } @@ -98,11 +91,6 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin return { enable_thinking: false } } - // claude - if (isSupportedThinkingTokenClaudeModel(model)) { - return {} - } - // gemini if (isSupportedThinkingTokenGeminiModel(model)) { if (GEMINI_FLASH_MODEL_REGEX.test(model.id)) { @@ -115,8 +103,10 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin } } } + } else { + logger.warn(`Model ${model.id} cannot disable reasoning. Fallback to empty reasoning param.`) + return {} } - return {} } // use thinking, doubao, zhipu, etc. @@ -130,16 +120,79 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin } // Specially for GPT-5.1. Suppose this is a OpenAI Compatible provider - if (isGPT51SeriesModel(model) && reasoningEffort === 'none') { + if (isGPT51SeriesModel(model)) { return { reasoningEffort: 'none' } } + logger.warn(`Model ${model.id} doesn't match any disable reasoning behavior. Fallback to empty reasoning param.`) return {} } // reasoningEffort有效的情况 + // https://creator.poe.com/docs/external-applications/openai-compatible-api#additional-considerations + // Poe provider - supports custom bot parameters via extra_body + if (provider.id === SystemProviderIds.poe) { + if (isOpenAIReasoningModel(model)) { + return { + extra_body: { + reasoning_effort: reasoningEffort === 'auto' ? 'medium' : reasoningEffort + } + } + } + + // Claude models use thinking_budget parameter in extra_body + if (isSupportedThinkingTokenClaudeModel(model)) { + const effortRatio = EFFORT_RATIO[reasoningEffort] + const tokenLimit = findTokenLimit(model.id) + const maxTokens = assistant.settings?.maxTokens + + if (!tokenLimit) { + logger.warn( + `No token limit configuration found for Claude model "${model.id}" on Poe provider. ` + + `Reasoning effort setting "${reasoningEffort}" will not be applied.` + ) + return {} + } + + let budgetTokens = Math.floor((tokenLimit.max - tokenLimit.min) * effortRatio + tokenLimit.min) + budgetTokens = Math.floor(Math.max(1024, Math.min(budgetTokens, (maxTokens || DEFAULT_MAX_TOKENS) * effortRatio))) + + return { + extra_body: { + thinking_budget: budgetTokens + } + } + } + + // Gemini models use thinking_budget parameter in extra_body + if (isSupportedThinkingTokenGeminiModel(model)) { + const effortRatio = EFFORT_RATIO[reasoningEffort] + const tokenLimit = findTokenLimit(model.id) + let budgetTokens: number | undefined + if (tokenLimit && reasoningEffort !== 'auto') { + budgetTokens = Math.floor((tokenLimit.max - tokenLimit.min) * effortRatio + tokenLimit.min) + } else if (!tokenLimit && reasoningEffort !== 'auto') { + logger.warn( + `No token limit configuration found for Gemini model "${model.id}" on Poe provider. ` + + `Using auto (-1) instead of requested effort "${reasoningEffort}".` + ) + } + return { + extra_body: { + thinking_budget: budgetTokens ?? -1 + } + } + } + + // Poe reasoning model not in known categories (GPT-5, Claude, Gemini) + logger.warn( + `Poe provider reasoning model "${model.id}" does not match known categories ` + + `(GPT-5, Claude, Gemini). Reasoning effort setting "${reasoningEffort}" will not be applied.` + ) + return {} + } // OpenRouter models if (model.provider === SystemProviderIds.openrouter) { @@ -196,9 +249,25 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin enable_thinking: true, incremental_output: true } + // TODO: 支持 new-api类型 + case SystemProviderIds['new-api']: + case SystemProviderIds.cherryin: { + return { + extra_body: { + thinking: { + type: 'enabled' // auto is invalid + } + } + } + } case SystemProviderIds.hunyuan: case SystemProviderIds['tencent-cloud-ti']: case SystemProviderIds.doubao: + case SystemProviderIds.deepseek: + case SystemProviderIds.aihubmix: + case SystemProviderIds.sophnet: + case SystemProviderIds.ppio: + case SystemProviderIds.dmxapi: return { thinking: { type: 'enabled' // auto is invalid @@ -220,13 +289,12 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin logger.warn( `Skipping thinking options for provider ${provider.name} as DeepSeek v3.1 thinking control method is unknown` ) - case SystemProviderIds.silicon: - // specially handled before } } } // OpenRouter models, use reasoning + // FIXME: duplicated openrouter handling. remove one if (model.provider === SystemProviderIds.openrouter) { if (isSupportedReasoningEffortModel(model) || isSupportedThinkingTokenModel(model)) { return { @@ -278,6 +346,12 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin // gemini series, openai compatible api if (isSupportedThinkingTokenGeminiModel(model)) { + // https://ai.google.dev/gemini-api/docs/gemini-3?thinking=high#openai_compatibility + if (isGemini3ThinkingTokenModel(model)) { + return { + reasoning_effort: reasoningEffort + } + } if (reasoningEffort === 'auto') { return { extra_body: { @@ -341,10 +415,14 @@ export function getReasoningEffort(assistant: Assistant, model: Model): Reasonin } /** - * 获取 OpenAI 推理参数 - * 从 OpenAIResponseAPIClient 和 OpenAIAPIClient 中提取的逻辑 + * Get OpenAI reasoning parameters + * Extracted from OpenAIResponseAPIClient and OpenAIAPIClient logic + * For official OpenAI provider only */ -export function getOpenAIReasoningParams(assistant: Assistant, model: Model): Record { +export function getOpenAIReasoningParams( + assistant: Assistant, + model: Model +): Pick { if (!isReasoningModel(model)) { return {} } @@ -355,6 +433,10 @@ export function getOpenAIReasoningParams(assistant: Assistant, model: Model): Re return {} } + if (isOpenAIDeepResearchModel(model) || reasoningEffort === 'auto') { + reasoningEffort = 'medium' + } + // 非OpenAI模型,但是Provider类型是responses/azure openai的情况 if (!isOpenAIModel(model)) { return { @@ -362,21 +444,17 @@ export function getOpenAIReasoningParams(assistant: Assistant, model: Model): Re } } - const openAI = getStoreSetting('openAI') as SettingsState['openAI'] - const summaryText = openAI?.summaryText || 'off' + const openAI = getStoreSetting('openAI') + const summaryText = openAI.summaryText - let reasoningSummary: string | undefined = undefined + let reasoningSummary: OpenAIReasoningSummary = undefined - if (summaryText === 'off' || model.id.includes('o1-pro')) { + if (model.id.includes('o1-pro')) { reasoningSummary = undefined } else { reasoningSummary = summaryText } - if (isOpenAIDeepResearchModel(model)) { - reasoningEffort = 'medium' - } - // OpenAI 推理参数 if (isSupportedReasoningEffortOpenAIModel(model)) { return { @@ -388,19 +466,26 @@ export function getOpenAIReasoningParams(assistant: Assistant, model: Model): Re return {} } -export function getAnthropicThinkingBudget(assistant: Assistant, model: Model): number { - const { maxTokens, reasoning_effort: reasoningEffort } = getAssistantSettings(assistant) +export function getAnthropicThinkingBudget( + maxTokens: number | undefined, + reasoningEffort: string | undefined, + modelId: string +): number | undefined { if (reasoningEffort === undefined || reasoningEffort === 'none') { - return 0 + return undefined } const effortRatio = EFFORT_RATIO[reasoningEffort] + const tokenLimit = findTokenLimit(modelId) + if (!tokenLimit) { + return undefined + } + const budgetTokens = Math.max( 1024, Math.floor( Math.min( - (findTokenLimit(model.id)?.max! - findTokenLimit(model.id)?.min!) * effortRatio + - findTokenLimit(model.id)?.min!, + (tokenLimit.max - tokenLimit.min) * effortRatio + tokenLimit.min, (maxTokens || DEFAULT_MAX_TOKENS) * effortRatio ) ) @@ -432,7 +517,8 @@ export function getAnthropicReasoningParams( // Claude 推理参数 if (isSupportedThinkingTokenClaudeModel(model)) { - const budgetTokens = getAnthropicThinkingBudget(assistant, model) + const { maxTokens } = getAssistantSettings(assistant) + const budgetTokens = getAnthropicThinkingBudget(maxTokens, reasoningEffort, model.id) return { thinking: { @@ -445,6 +531,21 @@ export function getAnthropicReasoningParams( return {} } +// type GoogleThinkingLevel = NonNullable['thinkingLevel'] + +// function mapToGeminiThinkingLevel(reasoningEffort: ReasoningEffortOption): GoogelThinkingLevel { +// switch (reasoningEffort) { +// case 'low': +// return 'low' +// case 'medium': +// return 'medium' +// case 'high': +// return 'high' +// default: +// return 'medium' +// } +// } + /** * 获取 Gemini 推理参数 * 从 GeminiAPIClient 中提取的逻辑 @@ -472,11 +573,22 @@ export function getGeminiReasoningParams( } } + // TODO: 很多中转还不支持 + // https://ai.google.dev/gemini-api/docs/gemini-3?thinking=high#new_api_features_in_gemini_3 + // if (isGemini3ThinkingTokenModel(model)) { + // return { + // thinkingConfig: { + // thinkingLevel: mapToGeminiThinkingLevel(reasoningEffort) + // } + // } + // } + const effortRatio = EFFORT_RATIO[reasoningEffort] if (effortRatio > 1) { return { thinkingConfig: { + thinkingBudget: -1, includeThoughts: true } } @@ -522,6 +634,8 @@ export function getXAIReasoningParams(assistant: Assistant, model: Model): Pick< case 'low': case 'high': return { reasoningEffort } + case 'xhigh': + return { reasoningEffort: 'high' } } } @@ -555,7 +669,8 @@ export function getBedrockReasoningParams( return {} } - const budgetTokens = getAnthropicThinkingBudget(assistant, model) + const { maxTokens } = getAssistantSettings(assistant) + const budgetTokens = getAnthropicThinkingBudget(maxTokens, reasoningEffort, model.id) return { reasoningConfig: { type: 'enabled', @@ -574,6 +689,10 @@ export function getCustomParameters(assistant: Assistant): Record { if (!param.name?.trim()) { return acc } + // Parse JSON type parameters + // Related: src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx:133-148 + // The UI stores JSON type params as strings (e.g., '{"key":"value"}') + // This function parses them into objects before sending to the API if (param.type === 'json') { const value = param.value as string if (value === 'undefined') { diff --git a/src/renderer/src/aiCore/utils/websearch.ts b/src/renderer/src/aiCore/utils/websearch.ts index 02619b54cf..127636a50b 100644 --- a/src/renderer/src/aiCore/utils/websearch.ts +++ b/src/renderer/src/aiCore/utils/websearch.ts @@ -47,6 +47,7 @@ export function buildProviderBuiltinWebSearchConfig( model?: Model ): WebSearchPluginConfig | undefined { switch (providerId) { + case 'azure-responses': case 'openai': { const searchContextSize = isOpenAIDeepResearchModel(model) ? 'medium' diff --git a/src/renderer/src/assets/images/apps/gemini.png b/src/renderer/src/assets/images/apps/gemini.png index 63c4207896..df8b95ced9 100644 Binary files a/src/renderer/src/assets/images/apps/gemini.png and b/src/renderer/src/assets/images/apps/gemini.png differ diff --git a/src/renderer/src/assets/images/models/gemini.png b/src/renderer/src/assets/images/models/gemini.png index 63c4207896..df8b95ced9 100644 Binary files a/src/renderer/src/assets/images/models/gemini.png and b/src/renderer/src/assets/images/models/gemini.png differ diff --git a/src/renderer/src/assets/styles/ant.css b/src/renderer/src/assets/styles/ant.css index 30005ff738..7d651a6a6a 100644 --- a/src/renderer/src/assets/styles/ant.css +++ b/src/renderer/src/assets/styles/ant.css @@ -215,6 +215,10 @@ border-top: none !important; } +.ant-collapse-header-text { + overflow-x: hidden; +} + .ant-slider .ant-slider-handle::after { box-shadow: 0 1px 4px 0px rgb(128 128 128 / 50%) !important; } diff --git a/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx b/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx index 2cd8171d08..8346eee120 100644 --- a/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx +++ b/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx @@ -25,7 +25,7 @@ type ViewMode = 'split' | 'code' | 'preview' const HtmlArtifactsPopup: React.FC = ({ open, title, html, onSave, onClose }) => { const { t } = useTranslation() const [viewMode, setViewMode] = useState('split') - const [isFullscreen, setIsFullscreen] = useState(false) + const [isFullscreen, setIsFullscreen] = useState(true) const [saved, setSaved] = useTemporaryValue(false, 2000) const codeEditorRef = useRef(null) const previewFrameRef = useRef(null) @@ -78,7 +78,7 @@ const HtmlArtifactsPopup: React.FC = ({ open, title, ht - e.stopPropagation()}> + e.stopPropagation()} className="nodrag"> = memo(({ children, language, onSave expanded={shouldExpand} wrapped={shouldWrap} maxHeight={`${MAX_COLLAPSED_CODE_HEIGHT}px`} + onRequestExpand={codeCollapsible ? () => setExpandOverride(true) : undefined} /> ), - [children, codeEditor.enabled, handleHeightChange, language, onSave, shouldExpand, shouldWrap] + [children, codeCollapsible, codeEditor.enabled, handleHeightChange, language, onSave, shouldExpand, shouldWrap] ) // 特殊视图组件映射 diff --git a/src/renderer/src/components/CodeToolbar/__tests__/__snapshots__/CodeToolbar.test.tsx.snap b/src/renderer/src/components/CodeToolbar/__tests__/__snapshots__/CodeToolbar.test.tsx.snap index c2b4028e32..56fb14ccc4 100644 --- a/src/renderer/src/components/CodeToolbar/__tests__/__snapshots__/CodeToolbar.test.tsx.snap +++ b/src/renderer/src/components/CodeToolbar/__tests__/__snapshots__/CodeToolbar.test.tsx.snap @@ -64,7 +64,11 @@ exports[`CodeToolbar > basic rendering > should match snapshot with mixed tools data-title="code_block.more" > + } + type="error" + showIcon + style={{ marginBottom: 16 }} + /> + )} + + {hasGitBash && customGitBashPath && ( + +
+ {t('agent.gitBash.customPath', { + defaultValue: 'Using custom path: {{path}}', + path: customGitBashPath + })} +
+
+ + +
+
+ } + type="success" + showIcon + style={{ marginBottom: 16 }} + /> + )} - - -