diff --git a/.editorconfig b/.editorconfig index 9f73416c90..9d08a1a828 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,9 +1,9 @@ -root = true - -[*] -charset = utf-8 -indent_style = space -indent_size = 2 -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000000..80532ea84b --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# ignore #7923 eol change and code formatting +4ac8a388347ff35f34de42c3ef4a2f81f03fb3b1 diff --git a/.gitattributes b/.gitattributes index 9a854faccd..849602b2b6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ +* text=auto eol=lf /.yarn/** linguist-vendored /.yarn/releases/* binary diff --git a/.github/ISSUE_TEMPLATE/#3_others.yml b/.github/ISSUE_TEMPLATE/#3_others.yml index 7b42bcda38..8eec58b381 100644 --- a/.github/ISSUE_TEMPLATE/#3_others.yml +++ b/.github/ISSUE_TEMPLATE/#3_others.yml @@ -73,4 +73,4 @@ body: id: additional attributes: label: 附加信息 - description: 任何能让我们对您的问题有更多了解的信息,包括截图或相关链接 \ No newline at end of file + description: 任何能让我们对您的问题有更多了解的信息,包括截图或相关链接 diff --git a/.github/ISSUE_TEMPLATE/3_others.yml b/.github/ISSUE_TEMPLATE/3_others.yml index dd99048bf7..4d8a383080 100644 --- a/.github/ISSUE_TEMPLATE/3_others.yml +++ b/.github/ISSUE_TEMPLATE/3_others.yml @@ -73,4 +73,4 @@ body: id: additional attributes: label: Additional Information - description: Any other information that could help us better understand your question, including screenshots or relevant links \ No newline at end of file + description: Any other information that could help us better understand your question, including screenshots or relevant links diff --git a/.github/dependabot.yml b/.github/dependabot.yml index f530d6e3bf..5dfa6afc15 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,86 +1,17 @@ version: 2 updates: - - package-ecosystem: "npm" - directory: "/" + - package-ecosystem: 'github-actions' + directory: '/' schedule: - interval: "monthly" - open-pull-requests-limit: 7 - target-branch: "main" - commit-message: - prefix: "chore" - include: "scope" - groups: - # 核心框架 - core-framework: - patterns: - - "react" - - "react-dom" - - "electron" - - "typescript" - - "@types/react*" - - "@types/node" - update-types: - - "minor" - - "patch" - - # Electron 生态和构建工具 - electron-build: - patterns: - - "electron-*" - - "@electron*" - - "vite" - - "@vitejs/*" - - "dotenv-cli" - - "rollup-plugin-*" - - "@swc/*" - update-types: - - "minor" - - "patch" - - # 测试工具 - testing-tools: - patterns: - - "vitest" - - "@vitest/*" - - "playwright" - - "@playwright/*" - - "eslint*" - - "@eslint*" - - "prettier" - - "husky" - - "lint-staged" - update-types: - - "minor" - - "patch" - - # CherryStudio 自定义包 - cherrystudio-packages: - patterns: - - "@cherrystudio/*" - update-types: - - "minor" - - "patch" - - # 兜底其他 dependencies - other-dependencies: - dependency-type: "production" - - # 兜底其他 devDependencies - other-dev-dependencies: - dependency-type: "development" - - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "weekly" + interval: 'monthly' open-pull-requests-limit: 3 commit-message: - prefix: "ci" - include: "scope" + prefix: 'ci' + include: 'scope' groups: github-actions: patterns: - - "*" + - '*' update-types: - - "minor" - - "patch" + - 'minor' + - 'patch' diff --git a/.github/issue-checker.yml b/.github/issue-checker.yml index b126d85477..483e9d966f 100644 --- a/.github/issue-checker.yml +++ b/.github/issue-checker.yml @@ -9,115 +9,115 @@ labels: # skips and removes - name: skip all content: - regexes: "[Ss]kip (?:[Aa]ll |)[Ll]abels?" + regexes: '[Ss]kip (?:[Aa]ll |)[Ll]abels?' - name: remove all content: - regexes: "[Rr]emove (?:[Aa]ll |)[Ll]abels?" + regexes: '[Rr]emove (?:[Aa]ll |)[Ll]abels?' - name: skip kind/bug content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)kind/bug(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)kind/bug(?:`|)' - name: remove kind/bug content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)kind/bug(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)kind/bug(?:`|)' - name: skip kind/enhancement content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)kind/enhancement(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)kind/enhancement(?:`|)' - name: remove kind/enhancement content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)kind/enhancement(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)kind/enhancement(?:`|)' - name: skip kind/question content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)kind/question(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)kind/question(?:`|)' - name: remove kind/question content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)kind/question(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)kind/question(?:`|)' - name: skip area/Connectivity content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)area/Connectivity(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)area/Connectivity(?:`|)' - name: remove area/Connectivity content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)area/Connectivity(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)area/Connectivity(?:`|)' - name: skip area/UI/UX content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)area/UI/UX(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)area/UI/UX(?:`|)' - name: remove area/UI/UX content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)area/UI/UX(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)area/UI/UX(?:`|)' - name: skip kind/documentation content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)kind/documentation(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)kind/documentation(?:`|)' - name: remove kind/documentation content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)kind/documentation(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)kind/documentation(?:`|)' - name: skip client:linux content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)client:linux(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)client:linux(?:`|)' - name: remove client:linux content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)client:linux(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)client:linux(?:`|)' - name: skip client:mac content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)client:mac(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)client:mac(?:`|)' - name: remove client:mac content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)client:mac(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)client:mac(?:`|)' - name: skip client:win content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)client:win(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)client:win(?:`|)' - name: remove client:win content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)client:win(?:`|)" - + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)client:win(?:`|)' + - name: skip sig/Assistant content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)sig/Assistant(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)sig/Assistant(?:`|)' - name: remove sig/Assistant content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)sig/Assistant(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)sig/Assistant(?:`|)' - name: skip sig/Data content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)sig/Data(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)sig/Data(?:`|)' - name: remove sig/Data content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)sig/Data(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)sig/Data(?:`|)' - name: skip sig/MCP content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)sig/MCP(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)sig/MCP(?:`|)' - name: remove sig/MCP content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)sig/MCP(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)sig/MCP(?:`|)' - name: skip sig/RAG content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)sig/RAG(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)sig/RAG(?:`|)' - name: remove sig/RAG content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)sig/RAG(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)sig/RAG(?:`|)' - name: skip lgtm content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)lgtm(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)lgtm(?:`|)' - name: remove lgtm content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)lgtm(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)lgtm(?:`|)' - name: skip License content: - regexes: "[Ss]kip (?:[Ll]abels? |)(?:`|)License(?:`|)" + regexes: '[Ss]kip (?:[Ll]abels? |)(?:`|)License(?:`|)' - name: remove License content: - regexes: "[Rr]emove (?:[Ll]abels? |)(?:`|)License(?:`|)" + regexes: '[Rr]emove (?:[Ll]abels? |)(?:`|)License(?:`|)' # `Dev Team` - name: Dev Team @@ -129,7 +129,7 @@ labels: # Area labels - name: area/Connectivity content: area/Connectivity - regexes: "代理|[Pp]roxy" + regexes: '代理|[Pp]roxy' skip-if: - skip all - skip area/Connectivity @@ -139,7 +139,7 @@ labels: - name: area/UI/UX content: area/UI/UX - regexes: "界面|[Uu][Ii]|重叠|按钮|图标|组件|渲染|菜单|栏目|头像|主题|样式|[Cc][Ss][Ss]" + regexes: '界面|[Uu][Ii]|重叠|按钮|图标|组件|渲染|菜单|栏目|头像|主题|样式|[Cc][Ss][Ss]' skip-if: - skip all - skip area/UI/UX @@ -150,7 +150,7 @@ labels: # Kind labels - name: kind/documentation content: kind/documentation - regexes: "文档|教程|[Dd]oc(s|umentation)|[Rr]eadme" + regexes: '文档|教程|[Dd]oc(s|umentation)|[Rr]eadme' skip-if: - skip all - skip kind/documentation @@ -161,7 +161,7 @@ labels: # Client labels - name: client:linux content: client:linux - regexes: "(?:[Ll]inux|[Uu]buntu|[Dd]ebian)" + regexes: '(?:[Ll]inux|[Uu]buntu|[Dd]ebian)' skip-if: - skip all - skip client:linux @@ -171,7 +171,7 @@ labels: - name: client:mac content: client:mac - regexes: "(?:[Mm]ac|[Mm]acOS|[Oo]SX)" + regexes: '(?:[Mm]ac|[Mm]acOS|[Oo]SX)' skip-if: - skip all - skip client:mac @@ -181,7 +181,7 @@ labels: - name: client:win content: client:win - regexes: "(?:[Ww]in|[Ww]indows)" + regexes: '(?:[Ww]in|[Ww]indows)' skip-if: - skip all - skip client:win @@ -192,7 +192,7 @@ labels: # SIG labels - name: sig/Assistant content: sig/Assistant - regexes: "快捷助手|[Aa]ssistant" + regexes: '快捷助手|[Aa]ssistant' skip-if: - skip all - skip sig/Assistant @@ -202,7 +202,7 @@ labels: - name: sig/Data content: sig/Data - regexes: "[Ww]ebdav|坚果云|备份|同步|数据|Obsidian|Notion|Joplin|思源" + regexes: '[Ww]ebdav|坚果云|备份|同步|数据|Obsidian|Notion|Joplin|思源' skip-if: - skip all - skip sig/Data @@ -212,7 +212,7 @@ labels: - name: sig/MCP content: sig/MCP - regexes: "[Mm][Cc][Pp]" + regexes: '[Mm][Cc][Pp]' skip-if: - skip all - skip sig/MCP @@ -222,7 +222,7 @@ labels: - name: sig/RAG content: sig/RAG - regexes: "知识库|[Rr][Aa][Gg]" + regexes: '知识库|[Rr][Aa][Gg]' skip-if: - skip all - skip sig/RAG @@ -233,7 +233,7 @@ labels: # Other labels - name: lgtm content: lgtm - regexes: "(?:[Ll][Gg][Tt][Mm]|[Ll]ooks [Gg]ood [Tt]o [Mm]e)" + regexes: '(?:[Ll][Gg][Tt][Mm]|[Ll]ooks [Gg]ood [Tt]o [Mm]e)' skip-if: - skip all - skip lgtm @@ -243,7 +243,7 @@ labels: - name: License content: License - regexes: "(?:[Ll]icense|[Cc]opyright|[Mm][Ii][Tt]|[Aa]pache)" + regexes: '(?:[Ll]icense|[Cc]opyright|[Mm][Ii][Tt]|[Aa]pache)' skip-if: - skip all - skip License diff --git a/.github/workflows/dispatch-docs-update.yml b/.github/workflows/dispatch-docs-update.yml new file mode 100644 index 0000000000..b9457faec6 --- /dev/null +++ b/.github/workflows/dispatch-docs-update.yml @@ -0,0 +1,27 @@ +name: Dispatch Docs Update on Release + +on: + release: + types: [released] + +permissions: + contents: write + +jobs: + dispatch-docs-update: + runs-on: ubuntu-latest + steps: + - name: Get Release Tag from Event + id: get-event-tag + shell: bash + run: | + # 从当前 Release 事件中获取 tag_name + 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 + with: + token: ${{ secrets.REPO_DISPATCH_TOKEN }} + repository: CherryHQ/cherry-studio-docs + event-type: update-download-version + client-payload: '{"version": "${{ steps.get-event-tag.outputs.tag }}"}' diff --git a/.github/workflows/issue-checker.yml b/.github/workflows/issue-checker.yml index cb768531c0..45da0f6b50 100644 --- a/.github/workflows/issue-checker.yml +++ b/.github/workflows/issue-checker.yml @@ -1,4 +1,4 @@ -name: "Issue Checker" +name: 'Issue Checker' on: issues: @@ -19,7 +19,7 @@ jobs: steps: - uses: MaaAssistantArknights/issue-checker@v1.14 with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" + repo-token: '${{ secrets.GITHUB_TOKEN }}' configuration-path: .github/issue-checker.yml not-before: 2022-08-05T00:00:00Z - include-title: 1 \ No newline at end of file + include-title: 1 diff --git a/.github/workflows/issue-management.yml b/.github/workflows/issue-management.yml index 59faedc04e..89ccc1fa8d 100644 --- a/.github/workflows/issue-management.yml +++ b/.github/workflows/issue-management.yml @@ -1,8 +1,8 @@ -name: "Stale Issue Management" +name: 'Stale Issue Management' on: schedule: - - cron: "0 0 * * *" + - cron: '0 0 * * *' workflow_dispatch: env: @@ -24,18 +24,18 @@ jobs: uses: actions/stale@v9 with: repo-token: ${{ secrets.GITHUB_TOKEN }} - only-labels: "needs-more-info" + only-labels: 'needs-more-info' days-before-stale: ${{ env.daysBeforeStale }} - days-before-close: 0 # Close immediately after stale - stale-issue-label: "inactive" - close-issue-label: "closed:no-response" + days-before-close: 0 # Close immediately after stale + stale-issue-label: 'inactive' + close-issue-label: 'closed:no-response' stale-issue-message: | This issue has been labeled as needing more information and has been inactive for ${{ env.daysBeforeStale }} days. It will be closed now due to lack of additional information. - + 该问题被标记为"需要更多信息"且已经 ${{ env.daysBeforeStale }} 天没有任何活动,将立即关闭。 operations-per-run: 50 - exempt-issue-labels: "pending, Dev Team" + exempt-issue-labels: 'pending, Dev Team' days-before-pr-stale: -1 days-before-pr-close: -1 @@ -45,11 +45,11 @@ jobs: repo-token: ${{ secrets.GITHUB_TOKEN }} days-before-stale: ${{ env.daysBeforeStale }} days-before-close: ${{ env.daysBeforeClose }} - stale-issue-label: "inactive" + stale-issue-label: 'inactive' stale-issue-message: | This issue has been inactive for a prolonged period and will be closed automatically in ${{ env.daysBeforeClose }} days. 该问题已长时间处于闲置状态,${{ env.daysBeforeClose }} 天后将自动关闭。 - exempt-issue-labels: "pending, Dev Team, kind/enhancement" + exempt-issue-labels: 'pending, Dev Team, kind/enhancement' days-before-pr-stale: -1 # Completely disable stalling for PRs days-before-pr-close: -1 # Completely disable closing for PRs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7a007e4e91..33b1529b40 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -77,8 +77,10 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} NODE_OPTIONS: --max-old-space-size=8192 + MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }} + RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} + RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }} - name: Build Mac if: matrix.os == 'macos-latest' @@ -92,9 +94,11 @@ jobs: APPLE_ID: ${{ vars.APPLE_ID }} APPLE_APP_SPECIFIC_PASSWORD: ${{ vars.APPLE_APP_SPECIFIC_PASSWORD }} APPLE_TEAM_ID: ${{ vars.APPLE_TEAM_ID }} - RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NODE_OPTIONS: --max-old-space-size=8192 + MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }} + RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} + RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }} - name: Build Windows if: matrix.os == 'windows-latest' @@ -103,8 +107,10 @@ jobs: yarn build:win env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} NODE_OPTIONS: --max-old-space-size=8192 + MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }} + RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }} + RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }} - name: Release uses: ncipollo/release-action@v1 @@ -115,38 +121,3 @@ jobs: tag: ${{ steps.get-tag.outputs.tag }} artifacts: 'dist/*.exe,dist/*.zip,dist/*.dmg,dist/*.AppImage,dist/*.snap,dist/*.deb,dist/*.rpm,dist/*.tar.gz,dist/latest*.yml,dist/rc*.yml,dist/*.blockmap' token: ${{ secrets.GITHUB_TOKEN }} - - dispatch-docs-update: - needs: release - if: success() && github.repository == 'CherryHQ/cherry-studio' # 确保所有构建成功且在主仓库中运行 - runs-on: ubuntu-latest - steps: - - name: Get release tag - 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_REF#refs/tags/}" >> $GITHUB_OUTPUT - fi - - - name: Check if tag is pre-release - id: check-tag - shell: bash - run: | - TAG="${{ steps.get-tag.outputs.tag }}" - if [[ "$TAG" == *"rc"* || "$TAG" == *"pre-release"* ]]; then - echo "is_pre_release=true" >> $GITHUB_OUTPUT - else - echo "is_pre_release=false" >> $GITHUB_OUTPUT - fi - - - name: Dispatch update-download-version workflow to cherry-studio-docs - if: steps.check-tag.outputs.is_pre_release == 'false' - uses: peter-evans/repository-dispatch@v3 - with: - token: ${{ secrets.REPO_DISPATCH_TOKEN }} - repository: CherryHQ/cherry-studio-docs - event-type: update-download-version - client-payload: '{"version": "${{ steps.get-tag.outputs.tag }}"}' diff --git a/.gitignore b/.gitignore index f0986c32b7..7f15a6637a 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,10 @@ local .aider* .cursorrules .cursor/* +.claude/* +.gemini/* +.trae/* +.claude-code-router/* # vitest coverage diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 940260d856..ef0b29b6a6 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,3 +1,3 @@ { - "recommendations": ["dbaeumer.vscode-eslint"] + "recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "editorconfig.editorconfig"] } diff --git a/.vscode/launch.json b/.vscode/launch.json index 0b6b9a6499..1519379f6e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "windows": { "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd" }, - "runtimeArgs": ["--sourcemap"], + "runtimeArgs": ["--inspect", "--sourcemap"], "env": { "REMOTE_DEBUGGING_PORT": "9222" } @@ -21,7 +21,7 @@ "request": "attach", "type": "chrome", "webRoot": "${workspaceFolder}/src/renderer", - "timeout": 60000, + "timeout": 3000000, "presentation": { "hidden": true } diff --git a/.vscode/settings.json b/.vscode/settings.json index bb7889776d..edf514d5ef 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,10 @@ { "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit" + "source.fixAll.eslint": "explicit", + "source.organizeImports": "never" }, + "files.eol": "\n", "search.exclude": { "**/dist/**": true, ".yarn/releases/**": true diff --git a/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch b/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch index 49fcd73ad2..330b3b9a42 100644 --- a/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch +++ b/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch @@ -75,6397 +75,6397 @@ index c27ff1fc034001e751c6a995476013145c3ea42f..00000000000000000000000000000000 --- a/dist/web/web.d.ts +++ /dev/null @@ -1,6394 +0,0 @@ --import { Client } from '@modelcontextprotocol/sdk/client/index.js'; --import { GoogleAuthOptions } from 'google-auth-library'; -- --/** Marks the end of user activity. -- -- This can only be sent if automatic (i.e. server-side) activity detection is -- disabled. -- */ --export declare interface ActivityEnd { --} -- --/** The different ways of handling user activity. */ --export declare enum ActivityHandling { -- /** -- * If unspecified, the default behavior is `START_OF_ACTIVITY_INTERRUPTS`. -- */ -- ACTIVITY_HANDLING_UNSPECIFIED = "ACTIVITY_HANDLING_UNSPECIFIED", -- /** -- * If true, start of activity will interrupt the model's response (also called "barge in"). The model's current response will be cut-off in the moment of the interruption. This is the default behavior. -- */ -- START_OF_ACTIVITY_INTERRUPTS = "START_OF_ACTIVITY_INTERRUPTS", -- /** -- * The model's response will not be interrupted. -- */ -- NO_INTERRUPTION = "NO_INTERRUPTION" --} -- --/** Marks the start of user activity. -- -- This can only be sent if automatic (i.e. server-side) activity detection is -- disabled. -- */ --export declare interface ActivityStart { --} -- --/** Optional. Adapter size for tuning. */ --export declare enum AdapterSize { -- /** -- * Adapter size is unspecified. -- */ -- ADAPTER_SIZE_UNSPECIFIED = "ADAPTER_SIZE_UNSPECIFIED", -- /** -- * Adapter size 1. -- */ -- ADAPTER_SIZE_ONE = "ADAPTER_SIZE_ONE", -- /** -- * Adapter size 2. -- */ -- ADAPTER_SIZE_TWO = "ADAPTER_SIZE_TWO", -- /** -- * Adapter size 4. -- */ -- ADAPTER_SIZE_FOUR = "ADAPTER_SIZE_FOUR", -- /** -- * Adapter size 8. -- */ -- ADAPTER_SIZE_EIGHT = "ADAPTER_SIZE_EIGHT", -- /** -- * Adapter size 16. -- */ -- ADAPTER_SIZE_SIXTEEN = "ADAPTER_SIZE_SIXTEEN", -- /** -- * Adapter size 32. -- */ -- ADAPTER_SIZE_THIRTY_TWO = "ADAPTER_SIZE_THIRTY_TWO" --} -- --/** -- * The ApiClient class is used to send requests to the Gemini API or Vertex AI -- * endpoints. -- */ --declare class ApiClient { -- readonly clientOptions: ApiClientInitOptions; -- constructor(opts: ApiClientInitOptions); -- /** -- * Determines the base URL for Vertex AI based on project and location. -- * Uses the global endpoint if location is 'global' or if project/location -- * are not specified (implying API key usage). -- * @private -- */ -- private baseUrlFromProjectLocation; -- /** -- * Normalizes authentication parameters for Vertex AI. -- * If project and location are provided, API key is cleared. -- * If project and location are not provided (implying API key usage), -- * project and location are cleared. -- * @private -- */ -- private normalizeAuthParameters; -- isVertexAI(): boolean; -- getProject(): string | undefined; -- getLocation(): string | undefined; -- getApiVersion(): string; -- getBaseUrl(): string; -- getRequestUrl(): string; -- getHeaders(): Record; -- private getRequestUrlInternal; -- getBaseResourcePath(): string; -- getApiKey(): string | undefined; -- getWebsocketBaseUrl(): string; -- setBaseUrl(url: string): void; -- private constructUrl; -- private shouldPrependVertexProjectPath; -- request(request: HttpRequest): Promise; -- private patchHttpOptions; -- requestStream(request: HttpRequest): Promise>; -- private includeExtraHttpOptionsToRequestInit; -- private unaryApiCall; -- private streamApiCall; -- processStreamResponse(response: Response): AsyncGenerator; -- private apiCall; -- getDefaultHeaders(): Record; -- private getHeadersInternal; -- /** -- * Uploads a file asynchronously using Gemini API only, this is not supported -- * in Vertex AI. -- * -- * @param file The string path to the file to be uploaded or a Blob object. -- * @param config Optional parameters specified in the `UploadFileConfig` -- * interface. @see {@link UploadFileConfig} -- * @return A promise that resolves to a `File` object. -- * @throws An error if called on a Vertex AI client. -- * @throws An error if the `mimeType` is not provided and can not be inferred, -- */ -- uploadFile(file: string | Blob, config?: UploadFileConfig): Promise; -- /** -- * Downloads a file asynchronously to the specified path. -- * -- * @params params - The parameters for the download request, see {@link -- * DownloadFileParameters} -- */ -- downloadFile(params: DownloadFileParameters): Promise; -- private fetchUploadUrl; --} -- --/** -- * Options for initializing the ApiClient. The ApiClient uses the parameters -- * for authentication purposes as well as to infer if SDK should send the -- * request to Vertex AI or Gemini API. -- */ --declare interface ApiClientInitOptions { -- /** -- * The object used for adding authentication headers to API requests. -- */ -- auth: Auth; -- /** -- * The uploader to use for uploading files. This field is required for -- * creating a client, will be set through the Node_client or Web_client. -- */ -- uploader: Uploader; -- /** -- * Optional. The downloader to use for downloading files. This field is -- * required for creating a client, will be set through the Node_client or -- * Web_client. -- */ -- downloader: Downloader; -- /** -- * Optional. The Google Cloud project ID for Vertex AI users. -- * It is not the numeric project name. -- * If not provided, SDK will try to resolve it from runtime environment. -- */ -- project?: string; -- /** -- * Optional. The Google Cloud project location for Vertex AI users. -- * If not provided, SDK will try to resolve it from runtime environment. -- */ -- location?: string; -- /** -- * The API Key. This is required for Gemini API users. -- */ -- apiKey?: string; -- /** -- * Optional. Set to true if you intend to call Vertex AI endpoints. -- * If unset, default SDK behavior is to call Gemini API. -- */ -- vertexai?: boolean; -- /** -- * Optional. The API version for the endpoint. -- * If unset, SDK will choose a default api version. -- */ -- apiVersion?: string; -- /** -- * Optional. A set of customizable configuration for HTTP requests. -- */ -- httpOptions?: HttpOptions; -- /** -- * Optional. An extra string to append at the end of the User-Agent header. -- * -- * This can be used to e.g specify the runtime and its version. -- */ -- userAgentExtra?: string; --} -- --/** Config for authentication with API key. */ --export declare interface ApiKeyConfig { -- /** The API key to be used in the request directly. */ -- apiKeyString?: string; --} -- --/** Representation of an audio chunk. */ --export declare interface AudioChunk { -- /** Raw byets of audio data. */ -- data?: string; -- /** MIME type of the audio chunk. */ -- mimeType?: string; -- /** Prompts and config used for generating this audio chunk. */ -- sourceMetadata?: LiveMusicSourceMetadata; --} -- --/** The audio transcription configuration in Setup. */ --export declare interface AudioTranscriptionConfig { --} -- --/** -- * @license -- * Copyright 2025 Google LLC -- * SPDX-License-Identifier: Apache-2.0 -- */ --/** -- * The Auth interface is used to authenticate with the API service. -- */ --declare interface Auth { -- /** -- * Sets the headers needed to authenticate with the API service. -- * -- * @param headers - The Headers object that will be updated with the authentication headers. -- */ -- addAuthHeaders(headers: Headers): Promise; --} -- --/** Auth configuration to run the extension. */ --export declare interface AuthConfig { -- /** Config for API key auth. */ -- apiKeyConfig?: ApiKeyConfig; -- /** Type of auth scheme. */ -- authType?: AuthType; -- /** Config for Google Service Account auth. */ -- googleServiceAccountConfig?: AuthConfigGoogleServiceAccountConfig; -- /** Config for HTTP Basic auth. */ -- httpBasicAuthConfig?: AuthConfigHttpBasicAuthConfig; -- /** Config for user oauth. */ -- oauthConfig?: AuthConfigOauthConfig; -- /** Config for user OIDC auth. */ -- oidcConfig?: AuthConfigOidcConfig; --} -- --/** Config for Google Service Account Authentication. */ --export declare interface AuthConfigGoogleServiceAccountConfig { -- /** Optional. The service account that the extension execution service runs as. - If the service account is specified, the `iam.serviceAccounts.getAccessToken` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the specified service account. - If not specified, the Vertex AI Extension Service Agent will be used to execute the Extension. */ -- serviceAccount?: string; --} -- --/** Config for HTTP Basic Authentication. */ --export declare interface AuthConfigHttpBasicAuthConfig { -- /** Required. The name of the SecretManager secret version resource storing the base64 encoded credentials. Format: `projects/{project}/secrets/{secrete}/versions/{version}` - If specified, the `secretmanager.versions.access` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the specified resource. */ -- credentialSecret?: string; --} -- --/** Config for user oauth. */ --export declare interface AuthConfigOauthConfig { -- /** Access token for extension endpoint. Only used to propagate token from [[ExecuteExtensionRequest.runtime_auth_config]] at request time. */ -- accessToken?: string; -- /** The service account used to generate access tokens for executing the Extension. - If the service account is specified, the `iam.serviceAccounts.getAccessToken` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the provided service account. */ -- serviceAccount?: string; --} -- --/** Config for user OIDC auth. */ --export declare interface AuthConfigOidcConfig { -- /** OpenID Connect formatted ID token for extension endpoint. Only used to propagate token from [[ExecuteExtensionRequest.runtime_auth_config]] at request time. */ -- idToken?: string; -- /** The service account used to generate an OpenID Connect (OIDC)-compatible JWT token signed by the Google OIDC Provider (accounts.google.com) for extension endpoint (https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-oidc). - The audience for the token will be set to the URL in the server url defined in the OpenApi spec. - If the service account is provided, the service account should grant `iam.serviceAccounts.getOpenIdToken` permission to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents). */ -- serviceAccount?: string; --} -- --/** Type of auth scheme. */ --export declare enum AuthType { -- AUTH_TYPE_UNSPECIFIED = "AUTH_TYPE_UNSPECIFIED", -- /** -- * No Auth. -- */ -- NO_AUTH = "NO_AUTH", -- /** -- * API Key Auth. -- */ -- API_KEY_AUTH = "API_KEY_AUTH", -- /** -- * HTTP Basic Auth. -- */ -- HTTP_BASIC_AUTH = "HTTP_BASIC_AUTH", -- /** -- * Google Service Account Auth. -- */ -- GOOGLE_SERVICE_ACCOUNT_AUTH = "GOOGLE_SERVICE_ACCOUNT_AUTH", -- /** -- * OAuth auth. -- */ -- OAUTH = "OAUTH", -- /** -- * OpenID Connect (OIDC) Auth. -- */ -- OIDC_AUTH = "OIDC_AUTH" --} -- --/** Configures automatic detection of activity. */ --export declare interface AutomaticActivityDetection { -- /** If enabled, detected voice and text input count as activity. If disabled, the client must send activity signals. */ -- disabled?: boolean; -- /** Determines how likely speech is to be detected. */ -- startOfSpeechSensitivity?: StartSensitivity; -- /** Determines how likely detected speech is ended. */ -- endOfSpeechSensitivity?: EndSensitivity; -- /** The required duration of detected speech before start-of-speech is committed. The lower this value the more sensitive the start-of-speech detection is and the shorter speech can be recognized. However, this also increases the probability of false positives. */ -- prefixPaddingMs?: number; -- /** The required duration of detected non-speech (e.g. silence) before end-of-speech is committed. The larger this value, the longer speech gaps can be without interrupting the user's activity but this will increase the model's latency. */ -- silenceDurationMs?: number; --} -- --/** The configuration for automatic function calling. */ --export declare interface AutomaticFunctionCallingConfig { -- /** Whether to disable automatic function calling. -- If not set or set to False, will enable automatic function calling. -- If set to True, will disable automatic function calling. -- */ -- disable?: boolean; -- /** If automatic function calling is enabled, -- maximum number of remote calls for automatic function calling. -- This number should be a positive integer. -- If not set, SDK will set maximum number of remote calls to 10. -- */ -- maximumRemoteCalls?: number; -- /** If automatic function calling is enabled, -- whether to ignore call history to the response. -- If not set, SDK will set ignore_call_history to false, -- and will append the call history to -- GenerateContentResponse.automatic_function_calling_history. -- */ -- ignoreCallHistory?: boolean; --} -- --/** -- * @license -- * Copyright 2025 Google LLC -- * SPDX-License-Identifier: Apache-2.0 -- */ --declare class BaseModule { --} -- --/** -- * Parameters for setting the base URLs for the Gemini API and Vertex AI API. -- */ --export declare interface BaseUrlParameters { -- geminiUrl?: string; -- vertexUrl?: string; --} -- --/** Defines the function behavior. Defaults to `BLOCKING`. */ --export declare enum Behavior { -- /** -- * This value is unused. -- */ -- UNSPECIFIED = "UNSPECIFIED", -- /** -- * If set, the system will wait to receive the function response before continuing the conversation. -- */ -- BLOCKING = "BLOCKING", -- /** -- * If set, the system will not wait to receive the function response. Instead, it will attempt to handle function responses as they become available while maintaining the conversation between the user and the model. -- */ -- NON_BLOCKING = "NON_BLOCKING" --} -- --/** Content blob. */ --declare interface Blob_2 { -- /** Optional. Display name of the blob. Used to provide a label or filename to distinguish blobs. This field is not currently used in the Gemini GenerateContent calls. */ -- displayName?: string; -- /** Required. Raw bytes. */ -- data?: string; -- /** Required. The IANA standard MIME type of the source data. */ -- mimeType?: string; --} --export { Blob_2 as Blob } -- --export declare type BlobImageUnion = Blob_2; -- --/** Output only. Blocked reason. */ --export declare enum BlockedReason { -- /** -- * Unspecified blocked reason. -- */ -- BLOCKED_REASON_UNSPECIFIED = "BLOCKED_REASON_UNSPECIFIED", -- /** -- * Candidates blocked due to safety. -- */ -- SAFETY = "SAFETY", -- /** -- * Candidates blocked due to other reason. -- */ -- OTHER = "OTHER", -- /** -- * Candidates blocked due to the terms which are included from the terminology blocklist. -- */ -- BLOCKLIST = "BLOCKLIST", -- /** -- * Candidates blocked due to prohibited content. -- */ -- PROHIBITED_CONTENT = "PROHIBITED_CONTENT" --} -- --/** A resource used in LLM queries for users to explicitly specify what to cache. */ --export declare interface CachedContent { -- /** The server-generated resource name of the cached content. */ -- name?: string; -- /** The user-generated meaningful display name of the cached content. */ -- displayName?: string; -- /** The name of the publisher model to use for cached content. */ -- model?: string; -- /** Creation time of the cache entry. */ -- createTime?: string; -- /** When the cache entry was last updated in UTC time. */ -- updateTime?: string; -- /** Expiration time of the cached content. */ -- expireTime?: string; -- /** Metadata on the usage of the cached content. */ -- usageMetadata?: CachedContentUsageMetadata; --} -- --/** Metadata on the usage of the cached content. */ --export declare interface CachedContentUsageMetadata { -- /** Duration of audio in seconds. */ -- audioDurationSeconds?: number; -- /** Number of images. */ -- imageCount?: number; -- /** Number of text characters. */ -- textCount?: number; -- /** Total number of tokens that the cached content consumes. */ -- totalTokenCount?: number; -- /** Duration of video in seconds. */ -- videoDurationSeconds?: number; --} -- --export declare class Caches extends BaseModule { -- private readonly apiClient; -- constructor(apiClient: ApiClient); -- /** -- * Lists cached content configurations. -- * -- * @param params - The parameters for the list request. -- * @return The paginated results of the list of cached contents. -- * -- * @example -- * ```ts -- * const cachedContents = await ai.caches.list({config: {'pageSize': 2}}); -- * for (const cachedContent of cachedContents) { -- * console.log(cachedContent); -- * } -- * ``` -- */ -- list: (params?: types.ListCachedContentsParameters) => Promise>; -- /** -- * Creates a cached contents resource. -- * -- * @remarks -- * Context caching is only supported for specific models. See [Gemini -- * Developer API reference](https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac) -- * and [Vertex AI reference](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models) -- * for more information. -- * -- * @param params - The parameters for the create request. -- * @return The created cached content. -- * -- * @example -- * ```ts -- * const contents = ...; // Initialize the content to cache. -- * const response = await ai.caches.create({ -- * model: 'gemini-2.0-flash-001', -- * config: { -- * 'contents': contents, -- * 'displayName': 'test cache', -- * 'systemInstruction': 'What is the sum of the two pdfs?', -- * 'ttl': '86400s', -- * } -- * }); -- * ``` -- */ -- create(params: types.CreateCachedContentParameters): Promise; -- /** -- * Gets cached content configurations. -- * -- * @param params - The parameters for the get request. -- * @return The cached content. -- * -- * @example -- * ```ts -- * await ai.caches.get({name: '...'}); // The server-generated resource name. -- * ``` -- */ -- get(params: types.GetCachedContentParameters): Promise; -- /** -- * Deletes cached content. -- * -- * @param params - The parameters for the delete request. -- * @return The empty response returned by the API. -- * -- * @example -- * ```ts -- * await ai.caches.delete({name: '...'}); // The server-generated resource name. -- * ``` -- */ -- delete(params: types.DeleteCachedContentParameters): Promise; -- /** -- * Updates cached content configurations. -- * -- * @param params - The parameters for the update request. -- * @return The updated cached content. -- * -- * @example -- * ```ts -- * const response = await ai.caches.update({ -- * name: '...', // The server-generated resource name. -- * config: {'ttl': '7600s'} -- * }); -- * ``` -- */ -- update(params: types.UpdateCachedContentParameters): Promise; -- private listInternal; --} -- --/** -- * CallableTool is an invokable tool that can be executed with external -- * application (e.g., via Model Context Protocol) or local functions with -- * function calling. -- */ --export declare interface CallableTool { -- /** -- * Returns tool that can be called by Gemini. -- */ -- tool(): Promise; -- /** -- * Executes the callable tool with the given function call arguments and -- * returns the response parts from the tool execution. -- */ -- callTool(functionCalls: FunctionCall[]): Promise; --} -- --/** -- * CallableToolConfig is the configuration for a callable tool. -- */ --export declare interface CallableToolConfig { -- /** -- * Specifies the model's behavior after invoking this tool. -- */ -- behavior?: Behavior; --} -- --/** A response candidate generated from the model. */ --export declare interface Candidate { -- /** Contains the multi-part content of the response. -- */ -- content?: Content; -- /** Source attribution of the generated content. -- */ -- citationMetadata?: CitationMetadata; -- /** Describes the reason the model stopped generating tokens. -- */ -- finishMessage?: string; -- /** Number of tokens for this candidate. -- */ -- tokenCount?: number; -- /** The reason why the model stopped generating tokens. -- If empty, the model has not stopped generating the tokens. -- */ -- finishReason?: FinishReason; -- /** Metadata related to url context retrieval tool. */ -- urlContextMetadata?: UrlContextMetadata; -- /** Output only. Average log probability score of the candidate. */ -- avgLogprobs?: number; -- /** Output only. Metadata specifies sources used to ground generated content. */ -- groundingMetadata?: GroundingMetadata; -- /** Output only. Index of the candidate. */ -- index?: number; -- /** Output only. Log-likelihood scores for the response tokens and top tokens */ -- logprobsResult?: LogprobsResult; -- /** Output only. List of ratings for the safety of a response candidate. There is at most one rating per category. */ -- safetyRatings?: SafetyRating[]; --} -- --/** -- * Chat session that enables sending messages to the model with previous -- * conversation context. -- * -- * @remarks -- * The session maintains all the turns between user and model. -- */ --export declare class Chat { -- private readonly apiClient; -- private readonly modelsModule; -- private readonly model; -- private readonly config; -- private history; -- private sendPromise; -- constructor(apiClient: ApiClient, modelsModule: Models, model: string, config?: types.GenerateContentConfig, history?: types.Content[]); -- /** -- * Sends a message to the model and returns the response. -- * -- * @remarks -- * This method will wait for the previous message to be processed before -- * sending the next message. -- * -- * @see {@link Chat#sendMessageStream} for streaming method. -- * @param params - parameters for sending messages within a chat session. -- * @returns The model's response. -- * -- * @example -- * ```ts -- * const chat = ai.chats.create({model: 'gemini-2.0-flash'}); -- * const response = await chat.sendMessage({ -- * message: 'Why is the sky blue?' -- * }); -- * console.log(response.text); -- * ``` -- */ -- sendMessage(params: types.SendMessageParameters): Promise; -- /** -- * Sends a message to the model and returns the response in chunks. -- * -- * @remarks -- * This method will wait for the previous message to be processed before -- * sending the next message. -- * -- * @see {@link Chat#sendMessage} for non-streaming method. -- * @param params - parameters for sending the message. -- * @return The model's response. -- * -- * @example -- * ```ts -- * const chat = ai.chats.create({model: 'gemini-2.0-flash'}); -- * const response = await chat.sendMessageStream({ -- * message: 'Why is the sky blue?' -- * }); -- * for await (const chunk of response) { -- * console.log(chunk.text); -- * } -- * ``` -- */ -- sendMessageStream(params: types.SendMessageParameters): Promise>; -- /** -- * Returns the chat history. -- * -- * @remarks -- * The history is a list of contents alternating between user and model. -- * -- * There are two types of history: -- * - The `curated history` contains only the valid turns between user and -- * model, which will be included in the subsequent requests sent to the model. -- * - The `comprehensive history` contains all turns, including invalid or -- * empty model outputs, providing a complete record of the history. -- * -- * The history is updated after receiving the response from the model, -- * for streaming response, it means receiving the last chunk of the response. -- * -- * The `comprehensive history` is returned by default. To get the `curated -- * history`, set the `curated` parameter to `true`. -- * -- * @param curated - whether to return the curated history or the comprehensive -- * history. -- * @return History contents alternating between user and model for the entire -- * chat session. -- */ -- getHistory(curated?: boolean): types.Content[]; -- private processStreamResponse; -- private recordHistory; --} -- --/** -- * A utility class to create a chat session. -- */ --export declare class Chats { -- private readonly modelsModule; -- private readonly apiClient; -- constructor(modelsModule: Models, apiClient: ApiClient); -- /** -- * Creates a new chat session. -- * -- * @remarks -- * The config in the params will be used for all requests within the chat -- * session unless overridden by a per-request `config` in -- * @see {@link types.SendMessageParameters#config}. -- * -- * @param params - Parameters for creating a chat session. -- * @returns A new chat session. -- * -- * @example -- * ```ts -- * const chat = ai.chats.create({ -- * model: 'gemini-2.0-flash' -- * config: { -- * temperature: 0.5, -- * maxOutputTokens: 1024, -- * } -- * }); -- * ``` -- */ -- create(params: types.CreateChatParameters): Chat; --} -- --/** Describes the machine learning model version checkpoint. */ --export declare interface Checkpoint { -- /** The ID of the checkpoint. -- */ -- checkpointId?: string; -- /** The epoch of the checkpoint. -- */ -- epoch?: string; -- /** The step of the checkpoint. -- */ -- step?: string; --} -- --/** Source attributions for content. */ --export declare interface Citation { -- /** Output only. End index into the content. */ -- endIndex?: number; -- /** Output only. License of the attribution. */ -- license?: string; -- /** Output only. Publication date of the attribution. */ -- publicationDate?: GoogleTypeDate; -- /** Output only. Start index into the content. */ -- startIndex?: number; -- /** Output only. Title of the attribution. */ -- title?: string; -- /** Output only. Url reference of the attribution. */ -- uri?: string; --} -- --/** Citation information when the model quotes another source. */ --export declare interface CitationMetadata { -- /** Contains citation information when the model directly quotes, at -- length, from another source. Can include traditional websites and code -- repositories. -- */ -- citations?: Citation[]; --} -- --/** Result of executing the [ExecutableCode]. Always follows a `part` containing the [ExecutableCode]. */ --export declare interface CodeExecutionResult { -- /** Required. Outcome of the code execution. */ -- outcome?: Outcome; -- /** Optional. Contains stdout when code execution is successful, stderr or other description otherwise. */ -- output?: string; --} -- --/** Optional parameters for computing tokens. */ --export declare interface ComputeTokensConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for computing tokens. */ --export declare interface ComputeTokensParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** Input content. */ -- contents: ContentListUnion; -- /** Optional parameters for the request. -- */ -- config?: ComputeTokensConfig; --} -- --/** Response for computing tokens. */ --export declare class ComputeTokensResponse { -- /** Lists of tokens info from the input. A ComputeTokensRequest could have multiple instances with a prompt in each instance. We also need to return lists of tokens info for the request with multiple instances. */ -- tokensInfo?: TokensInfo[]; --} -- --/** Contains the multi-part content of a message. */ --export declare interface Content { -- /** List of parts that constitute a single message. Each part may have -- a different IANA MIME type. */ -- parts?: Part[]; -- /** Optional. The producer of the content. Must be either 'user' or -- 'model'. Useful to set for multi-turn conversations, otherwise can be -- empty. If role is not specified, SDK will determine the role. */ -- role?: string; --} -- --/** The embedding generated from an input content. */ --export declare interface ContentEmbedding { -- /** A list of floats representing an embedding. -- */ -- values?: number[]; -- /** Vertex API only. Statistics of the input text associated with this -- embedding. -- */ -- statistics?: ContentEmbeddingStatistics; --} -- --/** Statistics of the input text associated with the result of content embedding. */ --export declare interface ContentEmbeddingStatistics { -- /** Vertex API only. If the input text was truncated due to having -- a length longer than the allowed maximum input. -- */ -- truncated?: boolean; -- /** Vertex API only. Number of tokens of the input text. -- */ -- tokenCount?: number; --} -- --export declare type ContentListUnion = Content | Content[] | PartUnion | PartUnion[]; -- --export declare type ContentUnion = Content | PartUnion[] | PartUnion; -- --/** Enables context window compression -- mechanism managing model context window so it does not exceed given length. */ --export declare interface ContextWindowCompressionConfig { -- /** Number of tokens (before running turn) that triggers context window compression mechanism. */ -- triggerTokens?: string; -- /** Sliding window compression mechanism. */ -- slidingWindow?: SlidingWindow; --} -- --/** Configuration for a Control reference image. */ --export declare interface ControlReferenceConfig { -- /** The type of control reference image to use. */ -- controlType?: ControlReferenceType; -- /** Defaults to False. When set to True, the control image will be -- computed by the model based on the control type. When set to False, -- the control image must be provided by the user. */ -- enableControlImageComputation?: boolean; --} -- --/** A control reference image. -- -- The image of the control reference image is either a control image provided -- by the user, or a regular image which the backend will use to generate a -- control image of. In the case of the latter, the -- enable_control_image_computation field in the config should be set to True. -- -- A control image is an image that represents a sketch image of areas for the -- model to fill in based on the prompt. -- */ --export declare class ControlReferenceImage { -- /** The reference image for the editing operation. */ -- referenceImage?: Image_2; -- /** The id of the reference image. */ -- referenceId?: number; -- /** The type of the reference image. Only set by the SDK. */ -- referenceType?: string; -- /** Configuration for the control reference image. */ -- config?: ControlReferenceConfig; -- /** Internal method to convert to ReferenceImageAPIInternal. */ -- toReferenceImageAPI(): any; --} -- --/** Enum representing the control type of a control reference image. */ --export declare enum ControlReferenceType { -- CONTROL_TYPE_DEFAULT = "CONTROL_TYPE_DEFAULT", -- CONTROL_TYPE_CANNY = "CONTROL_TYPE_CANNY", -- CONTROL_TYPE_SCRIBBLE = "CONTROL_TYPE_SCRIBBLE", -- CONTROL_TYPE_FACE_MESH = "CONTROL_TYPE_FACE_MESH" --} -- --/** Config for the count_tokens method. */ --export declare interface CountTokensConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Instructions for the model to steer it toward better performance. -- */ -- systemInstruction?: ContentUnion; -- /** Code that enables the system to interact with external systems to -- perform an action outside of the knowledge and scope of the model. -- */ -- tools?: Tool[]; -- /** Configuration that the model uses to generate the response. Not -- supported by the Gemini Developer API. -- */ -- generationConfig?: GenerationConfig; --} -- --/** Parameters for counting tokens. */ --export declare interface CountTokensParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** Input content. */ -- contents: ContentListUnion; -- /** Configuration for counting tokens. */ -- config?: CountTokensConfig; --} -- --/** Response for counting tokens. */ --export declare class CountTokensResponse { -- /** Total number of tokens. */ -- totalTokens?: number; -- /** Number of tokens in the cached part of the prompt (the cached content). */ -- cachedContentTokenCount?: number; --} -- --/** Optional parameters. */ --export declare interface CreateAuthTokenConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** An optional time after which, when using the resulting token, -- messages in Live API sessions will be rejected. (Gemini may -- preemptively close the session after this time.) -- -- If not set then this defaults to 30 minutes in the future. If set, this -- value must be less than 20 hours in the future. */ -- expireTime?: string; -- /** The time after which new Live API sessions using the token -- resulting from this request will be rejected. -- -- If not set this defaults to 60 seconds in the future. If set, this value -- must be less than 20 hours in the future. */ -- newSessionExpireTime?: string; -- /** The number of times the token can be used. If this value is zero -- then no limit is applied. Default is 1. Resuming a Live API session does -- not count as a use. */ -- uses?: number; -- /** Configuration specific to Live API connections created using this token. */ -- liveEphemeralParameters?: LiveEphemeralParameters; -- /** Additional fields to lock in the effective LiveConnectParameters. */ -- lockAdditionalFields?: string[]; --} -- --/** Optional configuration for cached content creation. */ --export declare interface CreateCachedContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** The TTL for this resource. The expiration time is computed: now + TTL. It is a duration string, with up to nine fractional digits, terminated by 's'. Example: "3.5s". */ -- ttl?: string; -- /** Timestamp of when this resource is considered expired. Uses RFC 3339 format, Example: 2014-10-02T15:01:23Z. */ -- expireTime?: string; -- /** The user-generated meaningful display name of the cached content. -- */ -- displayName?: string; -- /** The content to cache. -- */ -- contents?: ContentListUnion; -- /** Developer set system instruction. -- */ -- systemInstruction?: ContentUnion; -- /** A list of `Tools` the model may use to generate the next response. -- */ -- tools?: Tool[]; -- /** Configuration for the tools to use. This config is shared for all tools. -- */ -- toolConfig?: ToolConfig; -- /** The Cloud KMS resource identifier of the customer managed -- encryption key used to protect a resource. -- The key needs to be in the same region as where the compute resource is -- created. See -- https://cloud.google.com/vertex-ai/docs/general/cmek for more -- details. If this is set, then all created CachedContent objects -- will be encrypted with the provided encryption key. -- Allowed formats: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key} -- */ -- kmsKeyName?: string; --} -- --/** Parameters for caches.create method. */ --export declare interface CreateCachedContentParameters { -- /** ID of the model to use. Example: gemini-2.0-flash */ -- model: string; -- /** Configuration that contains optional parameters. -- */ -- config?: CreateCachedContentConfig; --} -- --/** Parameters for initializing a new chat session. -- -- These parameters are used when creating a chat session with the -- `chats.create()` method. -- */ --export declare interface CreateChatParameters { -- /** The name of the model to use for the chat session. -- -- For example: 'gemini-2.0-flash', 'gemini-2.0-flash-lite', etc. See Gemini API -- docs to find the available models. -- */ -- model: string; -- /** Config for the entire chat session. -- -- This config applies to all requests within the session -- unless overridden by a per-request `config` in `SendMessageParameters`. -- */ -- config?: GenerateContentConfig; -- /** The initial conversation history for the chat session. -- -- This allows you to start the chat with a pre-existing history. The history -- must be a list of `Content` alternating between 'user' and 'model' roles. -- It should start with a 'user' message. -- */ -- history?: Content[]; --} -- --/** Used to override the default configuration. */ --export declare interface CreateFileConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Generates the parameters for the private _create method. */ --export declare interface CreateFileParameters { -- /** The file to be uploaded. -- mime_type: (Required) The MIME type of the file. Must be provided. -- name: (Optional) The name of the file in the destination (e.g. -- 'files/sample-image'). -- display_name: (Optional) The display name of the file. -- */ -- file: File_2; -- /** Used to override the default configuration. */ -- config?: CreateFileConfig; --} -- --/** Response for the create file method. */ --export declare class CreateFileResponse { -- /** Used to retain the full HTTP response. */ -- sdkHttpResponse?: HttpResponse; --} -- --/** -- * Creates a `Content` object with a model role from a `PartListUnion` object or `string`. -- */ --export declare function createModelContent(partOrString: PartListUnion | string): Content; -- --/** -- * Creates a `Part` object from a `base64` encoded `string`. -- */ --export declare function createPartFromBase64(data: string, mimeType: string): Part; -- --/** -- * Creates a `Part` object from the `outcome` and `output` of a `CodeExecutionResult` object. -- */ --export declare function createPartFromCodeExecutionResult(outcome: Outcome, output: string): Part; -- --/** -- * Creates a `Part` object from the `code` and `language` of an `ExecutableCode` object. -- */ --export declare function createPartFromExecutableCode(code: string, language: Language): Part; -- --/** -- * Creates a `Part` object from a `FunctionCall` object. -- */ --export declare function createPartFromFunctionCall(name: string, args: Record): Part; -- --/** -- * Creates a `Part` object from a `FunctionResponse` object. -- */ --export declare function createPartFromFunctionResponse(id: string, name: string, response: Record): Part; -- --/** -- * Creates a `Part` object from a `text` string. -- */ --export declare function createPartFromText(text: string): Part; -- --/** -- * Creates a `Part` object from a `URI` string. -- */ --export declare function createPartFromUri(uri: string, mimeType: string): Part; -- --/** Supervised fine-tuning job creation request - optional fields. */ --export declare interface CreateTuningJobConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ -- validationDataset?: TuningValidationDataset; -- /** The display name of the tuned Model. The name can be up to 128 characters long and can consist of any UTF-8 characters. */ -- tunedModelDisplayName?: string; -- /** The description of the TuningJob */ -- description?: string; -- /** Number of complete passes the model makes over the entire training dataset during training. */ -- epochCount?: number; -- /** Multiplier for adjusting the default learning rate. */ -- learningRateMultiplier?: number; -- /** If set to true, disable intermediate checkpoints for SFT and only the last checkpoint will be exported. Otherwise, enable intermediate checkpoints for SFT. */ -- exportLastCheckpointOnly?: boolean; -- /** Adapter size for tuning. */ -- adapterSize?: AdapterSize; -- /** The batch size hyperparameter for tuning. If not set, a default of 4 or 16 will be used based on the number of training examples. */ -- batchSize?: number; -- /** The learning rate hyperparameter for tuning. If not set, a default of 0.001 or 0.0002 will be calculated based on the number of training examples. */ -- learningRate?: number; --} -- --/** Supervised fine-tuning job creation parameters - optional fields. */ --export declare interface CreateTuningJobParameters { -- /** The base model that is being tuned, e.g., "gemini-1.0-pro-002". */ -- baseModel: string; -- /** Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ -- trainingDataset: TuningDataset; -- /** Configuration for the tuning job. */ -- config?: CreateTuningJobConfig; --} -- --/** -- * Creates a `Content` object with a user role from a `PartListUnion` object or `string`. -- */ --export declare function createUserContent(partOrString: PartListUnion | string): Content; -- --/** Distribution computed over a tuning dataset. */ --export declare interface DatasetDistribution { -- /** Output only. Defines the histogram bucket. */ -- buckets?: DatasetDistributionDistributionBucket[]; -- /** Output only. The maximum of the population values. */ -- max?: number; -- /** Output only. The arithmetic mean of the values in the population. */ -- mean?: number; -- /** Output only. The median of the values in the population. */ -- median?: number; -- /** Output only. The minimum of the population values. */ -- min?: number; -- /** Output only. The 5th percentile of the values in the population. */ -- p5?: number; -- /** Output only. The 95th percentile of the values in the population. */ -- p95?: number; -- /** Output only. Sum of a given population of values. */ -- sum?: number; --} -- --/** Dataset bucket used to create a histogram for the distribution given a population of values. */ --export declare interface DatasetDistributionDistributionBucket { -- /** Output only. Number of values in the bucket. */ -- count?: string; -- /** Output only. Left bound of the bucket. */ -- left?: number; -- /** Output only. Right bound of the bucket. */ -- right?: number; --} -- --/** Statistics computed over a tuning dataset. */ --export declare interface DatasetStats { -- /** Output only. Number of billable characters in the tuning dataset. */ -- totalBillableCharacterCount?: string; -- /** Output only. Number of tuning characters in the tuning dataset. */ -- totalTuningCharacterCount?: string; -- /** Output only. Number of examples in the tuning dataset. */ -- tuningDatasetExampleCount?: string; -- /** Output only. Number of tuning steps for this Tuning Job. */ -- tuningStepCount?: string; -- /** Output only. Sample user messages in the training dataset uri. */ -- userDatasetExamples?: Content[]; -- /** Output only. Dataset distributions for the user input tokens. */ -- userInputTokenDistribution?: DatasetDistribution; -- /** Output only. Dataset distributions for the messages per example. */ -- userMessagePerExampleDistribution?: DatasetDistribution; -- /** Output only. Dataset distributions for the user output tokens. */ -- userOutputTokenDistribution?: DatasetDistribution; --} -- --/** Optional parameters for caches.delete method. */ --export declare interface DeleteCachedContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for caches.delete method. */ --export declare interface DeleteCachedContentParameters { -- /** The server-generated resource name of the cached content. -- */ -- name: string; -- /** Optional parameters for the request. -- */ -- config?: DeleteCachedContentConfig; --} -- --/** Empty response for caches.delete method. */ --export declare class DeleteCachedContentResponse { --} -- --/** Used to override the default configuration. */ --export declare interface DeleteFileConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Generates the parameters for the get method. */ --export declare interface DeleteFileParameters { -- /** The name identifier for the file to be deleted. */ -- name: string; -- /** Used to override the default configuration. */ -- config?: DeleteFileConfig; --} -- --/** Response for the delete file method. */ --export declare class DeleteFileResponse { --} -- --/** Configuration for deleting a tuned model. */ --export declare interface DeleteModelConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for deleting a tuned model. */ --export declare interface DeleteModelParameters { -- model: string; -- /** Optional parameters for the request. */ -- config?: DeleteModelConfig; --} -- --export declare class DeleteModelResponse { --} -- --/** Statistics computed for datasets used for distillation. */ --export declare interface DistillationDataStats { -- /** Output only. Statistics computed for the training dataset. */ -- trainingDatasetStats?: DatasetStats; --} -- --/** Hyperparameters for Distillation. */ --export declare interface DistillationHyperParameters { -- /** Optional. Adapter size for distillation. */ -- adapterSize?: AdapterSize; -- /** Optional. Number of complete passes the model makes over the entire training dataset during training. */ -- epochCount?: string; -- /** Optional. Multiplier for adjusting the default learning rate. */ -- learningRateMultiplier?: number; --} -- --/** Tuning Spec for Distillation. */ --export declare interface DistillationSpec { -- /** The base teacher model that is being distilled, e.g., "gemini-1.0-pro-002". */ -- baseTeacherModel?: string; -- /** Optional. Hyperparameters for Distillation. */ -- hyperParameters?: DistillationHyperParameters; -- /** Required. A path in a Cloud Storage bucket, which will be treated as the root output directory of the distillation pipeline. It is used by the system to generate the paths of output artifacts. */ -- pipelineRootDirectory?: string; -- /** The student model that is being tuned, e.g., "google/gemma-2b-1.1-it". */ -- studentModel?: string; -- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ -- trainingDatasetUri?: string; -- /** The resource name of the Tuned teacher model. Format: `projects/{project}/locations/{location}/models/{model}`. */ -- tunedTeacherModelSource?: string; -- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ -- validationDatasetUri?: string; --} -- --export declare type DownloadableFileUnion = string | File_2 | GeneratedVideo | Video; -- --declare interface Downloader { -- /** -- * Downloads a file to the given location. -- * -- * @param params The parameters for downloading the file. -- * @param apiClient The ApiClient to use for uploading. -- * @return A Promises that resolves when the download is complete. -- */ -- download(params: DownloadFileParameters, apiClient: ApiClient): Promise; --} -- --/** Used to override the default configuration. */ --export declare interface DownloadFileConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters used to download a file. */ --export declare interface DownloadFileParameters { -- /** The file to download. It can be a file name, a file object or a generated video. */ -- file: DownloadableFileUnion; -- /** Location where the file should be downloaded to. */ -- downloadPath: string; -- /** Configuration to for the download operation. */ -- config?: DownloadFileConfig; --} -- --/** Describes the options to customize dynamic retrieval. */ --export declare interface DynamicRetrievalConfig { -- /** The mode of the predictor to be used in dynamic retrieval. */ -- mode?: DynamicRetrievalConfigMode; -- /** Optional. The threshold to be used in dynamic retrieval. If not set, a system default value is used. */ -- dynamicThreshold?: number; --} -- --/** Config for the dynamic retrieval config mode. */ --export declare enum DynamicRetrievalConfigMode { -- /** -- * Always trigger retrieval. -- */ -- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", -- /** -- * Run retrieval only when system decides it is necessary. -- */ -- MODE_DYNAMIC = "MODE_DYNAMIC" --} -- --/** Configuration for editing an image. */ --export declare interface EditImageConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Cloud Storage URI used to store the generated images. -- */ -- outputGcsUri?: string; -- /** Description of what to discourage in the generated images. -- */ -- negativePrompt?: string; -- /** Number of images to generate. -- */ -- numberOfImages?: number; -- /** Aspect ratio of the generated images. -- */ -- aspectRatio?: string; -- /** Controls how much the model adheres to the text prompt. Large -- values increase output and prompt alignment, but may compromise image -- quality. -- */ -- guidanceScale?: number; -- /** Random seed for image generation. This is not available when -- ``add_watermark`` is set to true. -- */ -- seed?: number; -- /** Filter level for safety filtering. -- */ -- safetyFilterLevel?: SafetyFilterLevel; -- /** Allows generation of people by the model. -- */ -- personGeneration?: PersonGeneration; -- /** Whether to report the safety scores of each generated image and -- the positive prompt in the response. -- */ -- includeSafetyAttributes?: boolean; -- /** Whether to include the Responsible AI filter reason if the image -- is filtered out of the response. -- */ -- includeRaiReason?: boolean; -- /** Language of the text in the prompt. -- */ -- language?: ImagePromptLanguage; -- /** MIME type of the generated image. -- */ -- outputMimeType?: string; -- /** Compression quality of the generated image (for ``image/jpeg`` -- only). -- */ -- outputCompressionQuality?: number; -- /** Describes the editing mode for the request. */ -- editMode?: EditMode; -- /** The number of sampling steps. A higher value has better image -- quality, while a lower value has better latency. */ -- baseSteps?: number; --} -- --/** Parameters for the request to edit an image. */ --export declare interface EditImageParameters { -- /** The model to use. */ -- model: string; -- /** A text description of the edit to apply to the image. */ -- prompt: string; -- /** The reference images for Imagen 3 editing. */ -- referenceImages: ReferenceImage[]; -- /** Configuration for editing. */ -- config?: EditImageConfig; --} -- --/** Response for the request to edit an image. */ --export declare class EditImageResponse { -- /** Generated images. */ -- generatedImages?: GeneratedImage[]; --} -- --/** Enum representing the Imagen 3 Edit mode. */ --export declare enum EditMode { -- EDIT_MODE_DEFAULT = "EDIT_MODE_DEFAULT", -- EDIT_MODE_INPAINT_REMOVAL = "EDIT_MODE_INPAINT_REMOVAL", -- EDIT_MODE_INPAINT_INSERTION = "EDIT_MODE_INPAINT_INSERTION", -- EDIT_MODE_OUTPAINT = "EDIT_MODE_OUTPAINT", -- EDIT_MODE_CONTROLLED_EDITING = "EDIT_MODE_CONTROLLED_EDITING", -- EDIT_MODE_STYLE = "EDIT_MODE_STYLE", -- EDIT_MODE_BGSWAP = "EDIT_MODE_BGSWAP", -- EDIT_MODE_PRODUCT_IMAGE = "EDIT_MODE_PRODUCT_IMAGE" --} -- --/** Optional parameters for the embed_content method. */ --export declare interface EmbedContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Type of task for which the embedding will be used. -- */ -- taskType?: string; -- /** Title for the text. Only applicable when TaskType is -- `RETRIEVAL_DOCUMENT`. -- */ -- title?: string; -- /** Reduced dimension for the output embedding. If set, -- excessive values in the output embedding are truncated from the end. -- Supported by newer models since 2024 only. You cannot set this value if -- using the earlier model (`models/embedding-001`). -- */ -- outputDimensionality?: number; -- /** Vertex API only. The MIME type of the input. -- */ -- mimeType?: string; -- /** Vertex API only. Whether to silently truncate inputs longer than -- the max sequence length. If this option is set to false, oversized inputs -- will lead to an INVALID_ARGUMENT error, similar to other text APIs. -- */ -- autoTruncate?: boolean; --} -- --/** Request-level metadata for the Vertex Embed Content API. */ --export declare interface EmbedContentMetadata { -- /** Vertex API only. The total number of billable characters included -- in the request. -- */ -- billableCharacterCount?: number; --} -- --/** Parameters for the embed_content method. */ --export declare interface EmbedContentParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** The content to embed. Only the `parts.text` fields will be counted. -- */ -- contents: ContentListUnion; -- /** Configuration that contains optional parameters. -- */ -- config?: EmbedContentConfig; --} -- --/** Response for the embed_content method. */ --export declare class EmbedContentResponse { -- /** The embeddings for each request, in the same order as provided in -- the batch request. -- */ -- embeddings?: ContentEmbedding[]; -- /** Vertex API only. Metadata about the request. -- */ -- metadata?: EmbedContentMetadata; --} -- --/** Represents a customer-managed encryption key spec that can be applied to a top-level resource. */ --export declare interface EncryptionSpec { -- /** Required. The Cloud KMS resource identifier of the customer managed encryption key used to protect a resource. Has the form: `projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`. The key needs to be in the same region as where the compute resource is created. */ -- kmsKeyName?: string; --} -- --/** An endpoint where you deploy models. */ --export declare interface Endpoint { -- /** Resource name of the endpoint. */ -- name?: string; -- /** ID of the model that's deployed to the endpoint. */ -- deployedModelId?: string; --} -- --/** End of speech sensitivity. */ --export declare enum EndSensitivity { -- /** -- * The default is END_SENSITIVITY_LOW. -- */ -- END_SENSITIVITY_UNSPECIFIED = "END_SENSITIVITY_UNSPECIFIED", -- /** -- * Automatic detection ends speech more often. -- */ -- END_SENSITIVITY_HIGH = "END_SENSITIVITY_HIGH", -- /** -- * Automatic detection ends speech less often. -- */ -- END_SENSITIVITY_LOW = "END_SENSITIVITY_LOW" --} -- --/** Tool to search public web data, powered by Vertex AI Search and Sec4 compliance. */ --export declare interface EnterpriseWebSearch { --} -- --/** Code generated by the model that is meant to be executed, and the result returned to the model. Generated when using the [FunctionDeclaration] tool and [FunctionCallingConfig] mode is set to [Mode.CODE]. */ --export declare interface ExecutableCode { -- /** Required. The code to be executed. */ -- code?: string; -- /** Required. Programming language of the `code`. */ -- language?: Language; --} -- --/** Options for feature selection preference. */ --export declare enum FeatureSelectionPreference { -- FEATURE_SELECTION_PREFERENCE_UNSPECIFIED = "FEATURE_SELECTION_PREFERENCE_UNSPECIFIED", -- PRIORITIZE_QUALITY = "PRIORITIZE_QUALITY", -- BALANCED = "BALANCED", -- PRIORITIZE_COST = "PRIORITIZE_COST" --} -- --export declare interface FetchPredictOperationConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for the fetchPredictOperation method. */ --export declare interface FetchPredictOperationParameters { -- /** The server-assigned name for the operation. */ -- operationName: string; -- resourceName: string; -- /** Used to override the default configuration. */ -- config?: FetchPredictOperationConfig; --} -- --/** A file uploaded to the API. */ --declare interface File_2 { -- /** The `File` resource name. The ID (name excluding the "files/" prefix) can contain up to 40 characters that are lowercase alphanumeric or dashes (-). The ID cannot start or end with a dash. If the name is empty on create, a unique name will be generated. Example: `files/123-456` */ -- name?: string; -- /** Optional. The human-readable display name for the `File`. The display name must be no more than 512 characters in length, including spaces. Example: 'Welcome Image' */ -- displayName?: string; -- /** Output only. MIME type of the file. */ -- mimeType?: string; -- /** Output only. Size of the file in bytes. */ -- sizeBytes?: string; -- /** Output only. The timestamp of when the `File` was created. */ -- createTime?: string; -- /** Output only. The timestamp of when the `File` will be deleted. Only set if the `File` is scheduled to expire. */ -- expirationTime?: string; -- /** Output only. The timestamp of when the `File` was last updated. */ -- updateTime?: string; -- /** Output only. SHA-256 hash of the uploaded bytes. The hash value is encoded in base64 format. */ -- sha256Hash?: string; -- /** Output only. The URI of the `File`. */ -- uri?: string; -- /** Output only. The URI of the `File`, only set for downloadable (generated) files. */ -- downloadUri?: string; -- /** Output only. Processing state of the File. */ -- state?: FileState; -- /** Output only. The source of the `File`. */ -- source?: FileSource; -- /** Output only. Metadata for a video. */ -- videoMetadata?: Record; -- /** Output only. Error status if File processing failed. */ -- error?: FileStatus; --} --export { File_2 as File } -- --/** URI based data. */ --export declare interface FileData { -- /** Required. URI. */ -- fileUri?: string; -- /** Required. The IANA standard MIME type of the source data. */ -- mimeType?: string; --} -- --export declare class Files extends BaseModule { -- private readonly apiClient; -- constructor(apiClient: ApiClient); -- /** -- * Lists all current project files from the service. -- * -- * @param params - The parameters for the list request -- * @return The paginated results of the list of files -- * -- * @example -- * The following code prints the names of all files from the service, the -- * size of each page is 10. -- * -- * ```ts -- * const listResponse = await ai.files.list({config: {'pageSize': 10}}); -- * for await (const file of listResponse) { -- * console.log(file.name); -- * } -- * ``` -- */ -- list: (params?: types.ListFilesParameters) => Promise>; -- /** -- * Uploads a file asynchronously to the Gemini API. -- * This method is not available in Vertex AI. -- * Supported upload sources: -- * - Node.js: File path (string) or Blob object. -- * - Browser: Blob object (e.g., File). -- * -- * @remarks -- * The `mimeType` can be specified in the `config` parameter. If omitted: -- * - For file path (string) inputs, the `mimeType` will be inferred from the -- * file extension. -- * - For Blob object inputs, the `mimeType` will be set to the Blob's `type` -- * property. -- * Somex eamples for file extension to mimeType mapping: -- * .txt -> text/plain -- * .json -> application/json -- * .jpg -> image/jpeg -- * .png -> image/png -- * .mp3 -> audio/mpeg -- * .mp4 -> video/mp4 -- * -- * This section can contain multiple paragraphs and code examples. -- * -- * @param params - Optional parameters specified in the -- * `types.UploadFileParameters` interface. -- * @see {@link types.UploadFileParameters#config} for the optional -- * config in the parameters. -- * @return A promise that resolves to a `types.File` object. -- * @throws An error if called on a Vertex AI client. -- * @throws An error if the `mimeType` is not provided and can not be inferred, -- * the `mimeType` can be provided in the `params.config` parameter. -- * @throws An error occurs if a suitable upload location cannot be established. -- * -- * @example -- * The following code uploads a file to Gemini API. -- * -- * ```ts -- * const file = await ai.files.upload({file: 'file.txt', config: { -- * mimeType: 'text/plain', -- * }}); -- * console.log(file.name); -- * ``` -- */ -- upload(params: types.UploadFileParameters): Promise; -- /** -- * Downloads a remotely stored file asynchronously to a location specified in -- * the `params` object. This method only works on Node environment, to -- * download files in the browser, use a browser compliant method like an -- * tag. -- * -- * @param params - The parameters for the download request. -- * -- * @example -- * The following code downloads an example file named "files/mehozpxf877d" as -- * "file.txt". -- * -- * ```ts -- * await ai.files.download({file: file.name, downloadPath: 'file.txt'}); -- * ``` -- */ -- download(params: types.DownloadFileParameters): Promise; -- private listInternal; -- private createInternal; -- /** -- * Retrieves the file information from the service. -- * -- * @param params - The parameters for the get request -- * @return The Promise that resolves to the types.File object requested. -- * -- * @example -- * ```ts -- * const config: GetFileParameters = { -- * name: fileName, -- * }; -- * file = await ai.files.get(config); -- * console.log(file.name); -- * ``` -- */ -- get(params: types.GetFileParameters): Promise; -- /** -- * Deletes a remotely stored file. -- * -- * @param params - The parameters for the delete request. -- * @return The DeleteFileResponse, the response for the delete method. -- * -- * @example -- * The following code deletes an example file named "files/mehozpxf877d". -- * -- * ```ts -- * await ai.files.delete({name: file.name}); -- * ``` -- */ -- delete(params: types.DeleteFileParameters): Promise; --} -- --/** Source of the File. */ --export declare enum FileSource { -- SOURCE_UNSPECIFIED = "SOURCE_UNSPECIFIED", -- UPLOADED = "UPLOADED", -- GENERATED = "GENERATED" --} -- --/** -- * Represents the size and mimeType of a file. The information is used to -- * request the upload URL from the https://generativelanguage.googleapis.com/upload/v1beta/files endpoint. -- * This interface defines the structure for constructing and executing HTTP -- * requests. -- */ --declare interface FileStat { -- /** -- * The size of the file in bytes. -- */ -- size: number; -- /** -- * The MIME type of the file. -- */ -- type: string | undefined; --} -- --/** State for the lifecycle of a File. */ --export declare enum FileState { -- STATE_UNSPECIFIED = "STATE_UNSPECIFIED", -- PROCESSING = "PROCESSING", -- ACTIVE = "ACTIVE", -- FAILED = "FAILED" --} -- --/** Status of a File that uses a common error model. */ --export declare interface FileStatus { -- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ -- details?: Record[]; -- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ -- message?: string; -- /** The status code. 0 for OK, 1 for CANCELLED */ -- code?: number; --} -- --/** Output only. The reason why the model stopped generating tokens. -- -- If empty, the model has not stopped generating the tokens. -- */ --export declare enum FinishReason { -- /** -- * The finish reason is unspecified. -- */ -- FINISH_REASON_UNSPECIFIED = "FINISH_REASON_UNSPECIFIED", -- /** -- * Token generation reached a natural stopping point or a configured stop sequence. -- */ -- STOP = "STOP", -- /** -- * Token generation reached the configured maximum output tokens. -- */ -- MAX_TOKENS = "MAX_TOKENS", -- /** -- * Token generation stopped because the content potentially contains safety violations. NOTE: When streaming, [content][] is empty if content filters blocks the output. -- */ -- SAFETY = "SAFETY", -- /** -- * The token generation stopped because of potential recitation. -- */ -- RECITATION = "RECITATION", -- /** -- * The token generation stopped because of using an unsupported language. -- */ -- LANGUAGE = "LANGUAGE", -- /** -- * All other reasons that stopped the token generation. -- */ -- OTHER = "OTHER", -- /** -- * Token generation stopped because the content contains forbidden terms. -- */ -- BLOCKLIST = "BLOCKLIST", -- /** -- * Token generation stopped for potentially containing prohibited content. -- */ -- PROHIBITED_CONTENT = "PROHIBITED_CONTENT", -- /** -- * Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII). -- */ -- SPII = "SPII", -- /** -- * The function call generated by the model is invalid. -- */ -- MALFORMED_FUNCTION_CALL = "MALFORMED_FUNCTION_CALL", -- /** -- * Token generation stopped because generated images have safety violations. -- */ -- IMAGE_SAFETY = "IMAGE_SAFETY" --} -- --/** A function call. */ --export declare interface FunctionCall { -- /** The unique id of the function call. If populated, the client to execute the -- `function_call` and return the response with the matching `id`. */ -- id?: string; -- /** Optional. Required. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details. */ -- args?: Record; -- /** Required. The name of the function to call. Matches [FunctionDeclaration.name]. */ -- name?: string; --} -- --/** Function calling config. */ --export declare interface FunctionCallingConfig { -- /** Optional. Function calling mode. */ -- mode?: FunctionCallingConfigMode; -- /** Optional. Function names to call. Only set when the Mode is ANY. Function names should match [FunctionDeclaration.name]. With mode set to ANY, model will predict a function call from the set of function names provided. */ -- allowedFunctionNames?: string[]; --} -- --/** Config for the function calling config mode. */ --export declare enum FunctionCallingConfigMode { -- /** -- * The function calling config mode is unspecified. Should not be used. -- */ -- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", -- /** -- * Default model behavior, model decides to predict either function calls or natural language response. -- */ -- AUTO = "AUTO", -- /** -- * Model is constrained to always predicting function calls only. If "allowed_function_names" are set, the predicted function calls will be limited to any one of "allowed_function_names", else the predicted function calls will be any one of the provided "function_declarations". -- */ -- ANY = "ANY", -- /** -- * Model will not predict any function calls. Model behavior is same as when not passing any function declarations. -- */ -- NONE = "NONE" --} -- --/** Defines a function that the model can generate JSON inputs for. -- -- The inputs are based on `OpenAPI 3.0 specifications -- `_. -- */ --export declare interface FunctionDeclaration { -- /** Defines the function behavior. */ -- behavior?: Behavior; -- /** Optional. Description and purpose of the function. Model uses it to decide how and whether to call the function. */ -- description?: string; -- /** Required. The name of the function to call. Must start with a letter or an underscore. Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a maximum length of 64. */ -- name?: string; -- /** Optional. Describes the parameters to this function in JSON Schema Object format. Reflects the Open API 3.03 Parameter Object. string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter. For function with no parameters, this can be left unset. Parameter names must start with a letter or an underscore and must only contain chars a-z, A-Z, 0-9, or underscores with a maximum length of 64. Example with 1 required and 1 optional parameter: type: OBJECT properties: param1: type: STRING param2: type: INTEGER required: - param1 */ -- parameters?: Schema; -- /** Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function. */ -- response?: Schema; --} -- --/** A function response. */ --export declare class FunctionResponse { -- /** Signals that function call continues, and more responses will be returned, turning the function call into a generator. Is only applicable to NON_BLOCKING function calls (see FunctionDeclaration.behavior for details), ignored otherwise. If false, the default, future responses will not be considered. Is only applicable to NON_BLOCKING function calls, is ignored otherwise. If set to false, future responses will not be considered. It is allowed to return empty `response` with `will_continue=False` to signal that the function call is finished. */ -- willContinue?: boolean; -- /** Specifies how the response should be scheduled in the conversation. Only applicable to NON_BLOCKING function calls, is ignored otherwise. Defaults to WHEN_IDLE. */ -- scheduling?: FunctionResponseScheduling; -- /** Optional. The id of the function call this response is for. Populated by the client to match the corresponding function call `id`. */ -- id?: string; -- /** Required. The name of the function to call. Matches [FunctionDeclaration.name] and [FunctionCall.name]. */ -- name?: string; -- /** Required. The function response in JSON object format. Use "output" key to specify function output and "error" key to specify error details (if any). If "output" and "error" keys are not specified, then whole "response" is treated as function output. */ -- response?: Record; --} -- --/** Specifies how the response should be scheduled in the conversation. */ --export declare enum FunctionResponseScheduling { -- /** -- * This value is unused. -- */ -- SCHEDULING_UNSPECIFIED = "SCHEDULING_UNSPECIFIED", -- /** -- * Only add the result to the conversation context, do not interrupt or trigger generation. -- */ -- SILENT = "SILENT", -- /** -- * Add the result to the conversation context, and prompt to generate output without interrupting ongoing generation. -- */ -- WHEN_IDLE = "WHEN_IDLE", -- /** -- * Add the result to the conversation context, interrupt ongoing generation and prompt to generate output. -- */ -- INTERRUPT = "INTERRUPT" --} -- --/** Optional model configuration parameters. -- -- For more information, see `Content generation parameters -- `_. -- */ --export declare interface GenerateContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Instructions for the model to steer it toward better performance. -- For example, "Answer as concisely as possible" or "Don't use technical -- terms in your response". -- */ -- systemInstruction?: ContentUnion; -- /** Value that controls the degree of randomness in token selection. -- Lower temperatures are good for prompts that require a less open-ended or -- creative response, while higher temperatures can lead to more diverse or -- creative results. -- */ -- temperature?: number; -- /** Tokens are selected from the most to least probable until the sum -- of their probabilities equals this value. Use a lower value for less -- random responses and a higher value for more random responses. -- */ -- topP?: number; -- /** For each token selection step, the ``top_k`` tokens with the -- highest probabilities are sampled. Then tokens are further filtered based -- on ``top_p`` with the final token selected using temperature sampling. Use -- a lower number for less random responses and a higher number for more -- random responses. -- */ -- topK?: number; -- /** Number of response variations to return. -- */ -- candidateCount?: number; -- /** Maximum number of tokens that can be generated in the response. -- */ -- maxOutputTokens?: number; -- /** List of strings that tells the model to stop generating text if one -- of the strings is encountered in the response. -- */ -- stopSequences?: string[]; -- /** Whether to return the log probabilities of the tokens that were -- chosen by the model at each step. -- */ -- responseLogprobs?: boolean; -- /** Number of top candidate tokens to return the log probabilities for -- at each generation step. -- */ -- logprobs?: number; -- /** Positive values penalize tokens that already appear in the -- generated text, increasing the probability of generating more diverse -- content. -- */ -- presencePenalty?: number; -- /** Positive values penalize tokens that repeatedly appear in the -- generated text, increasing the probability of generating more diverse -- content. -- */ -- frequencyPenalty?: number; -- /** When ``seed`` is fixed to a specific number, the model makes a best -- effort to provide the same response for repeated requests. By default, a -- random number is used. -- */ -- seed?: number; -- /** Output response mimetype of the generated candidate text. -- Supported mimetype: -- - `text/plain`: (default) Text output. -- - `application/json`: JSON response in the candidates. -- The model needs to be prompted to output the appropriate response type, -- otherwise the behavior is undefined. -- This is a preview feature. -- */ -- responseMimeType?: string; -- /** The `Schema` object allows the definition of input and output data types. -- These types can be objects, but also primitives and arrays. -- Represents a select subset of an [OpenAPI 3.0 schema -- object](https://spec.openapis.org/oas/v3.0.3#schema). -- If set, a compatible response_mime_type must also be set. -- Compatible mimetypes: `application/json`: Schema for JSON response. -- */ -- responseSchema?: SchemaUnion; -- /** Configuration for model router requests. -- */ -- routingConfig?: GenerationConfigRoutingConfig; -- /** Configuration for model selection. -- */ -- modelSelectionConfig?: ModelSelectionConfig; -- /** Safety settings in the request to block unsafe content in the -- response. -- */ -- safetySettings?: SafetySetting[]; -- /** Code that enables the system to interact with external systems to -- perform an action outside of the knowledge and scope of the model. -- */ -- tools?: ToolListUnion; -- /** Associates model output to a specific function call. -- */ -- toolConfig?: ToolConfig; -- /** Labels with user-defined metadata to break down billed charges. */ -- labels?: Record; -- /** Resource name of a context cache that can be used in subsequent -- requests. -- */ -- cachedContent?: string; -- /** The requested modalities of the response. Represents the set of -- modalities that the model can return. -- */ -- responseModalities?: string[]; -- /** If specified, the media resolution specified will be used. -- */ -- mediaResolution?: MediaResolution; -- /** The speech generation configuration. -- */ -- speechConfig?: SpeechConfigUnion; -- /** If enabled, audio timestamp will be included in the request to the -- model. -- */ -- audioTimestamp?: boolean; -- /** The configuration for automatic function calling. -- */ -- automaticFunctionCalling?: AutomaticFunctionCallingConfig; -- /** The thinking features configuration. -- */ -- thinkingConfig?: ThinkingConfig; --} -- --/** Config for models.generate_content parameters. */ --export declare interface GenerateContentParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** Content of the request. -- */ -- contents: ContentListUnion; -- /** Configuration that contains optional model parameters. -- */ -- config?: GenerateContentConfig; --} -- --/** Response message for PredictionService.GenerateContent. */ --export declare class GenerateContentResponse { -- /** Response variations returned by the model. -- */ -- candidates?: Candidate[]; -- /** Timestamp when the request is made to the server. -- */ -- createTime?: string; -- /** Identifier for each response. -- */ -- responseId?: string; -- /** The history of automatic function calling. -- */ -- automaticFunctionCallingHistory?: Content[]; -- /** Output only. The model version used to generate the response. */ -- modelVersion?: string; -- /** Output only. Content filter results for a prompt sent in the request. Note: Sent only in the first stream chunk. Only happens when no candidates were generated due to content violations. */ -- promptFeedback?: GenerateContentResponsePromptFeedback; -- /** Usage metadata about the response(s). */ -- usageMetadata?: GenerateContentResponseUsageMetadata; -- /** -- * Returns the concatenation of all text parts from the first candidate in the response. -- * -- * @remarks -- * If there are multiple candidates in the response, the text from the first -- * one will be returned. -- * If there are non-text parts in the response, the concatenation of all text -- * parts will be returned, and a warning will be logged. -- * If there are thought parts in the response, the concatenation of all text -- * parts excluding the thought parts will be returned. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateContent({ -- * model: 'gemini-2.0-flash', -- * contents: -- * 'Why is the sky blue?', -- * }); -- * -- * console.debug(response.text); -- * ``` -- */ -- get text(): string | undefined; -- /** -- * Returns the concatenation of all inline data parts from the first candidate -- * in the response. -- * -- * @remarks -- * If there are multiple candidates in the response, the inline data from the -- * first one will be returned. If there are non-inline data parts in the -- * response, the concatenation of all inline data parts will be returned, and -- * a warning will be logged. -- */ -- get data(): string | undefined; -- /** -- * Returns the function calls from the first candidate in the response. -- * -- * @remarks -- * If there are multiple candidates in the response, the function calls from -- * the first one will be returned. -- * If there are no function calls in the response, undefined will be returned. -- * -- * @example -- * ```ts -- * const controlLightFunctionDeclaration: FunctionDeclaration = { -- * name: 'controlLight', -- * parameters: { -- * type: Type.OBJECT, -- * description: 'Set the brightness and color temperature of a room light.', -- * properties: { -- * brightness: { -- * type: Type.NUMBER, -- * description: -- * 'Light level from 0 to 100. Zero is off and 100 is full brightness.', -- * }, -- * colorTemperature: { -- * type: Type.STRING, -- * description: -- * 'Color temperature of the light fixture which can be `daylight`, `cool` or `warm`.', -- * }, -- * }, -- * required: ['brightness', 'colorTemperature'], -- * }; -- * const response = await ai.models.generateContent({ -- * model: 'gemini-2.0-flash', -- * contents: 'Dim the lights so the room feels cozy and warm.', -- * config: { -- * tools: [{functionDeclarations: [controlLightFunctionDeclaration]}], -- * toolConfig: { -- * functionCallingConfig: { -- * mode: FunctionCallingConfigMode.ANY, -- * allowedFunctionNames: ['controlLight'], -- * }, -- * }, -- * }, -- * }); -- * console.debug(JSON.stringify(response.functionCalls)); -- * ``` -- */ -- get functionCalls(): FunctionCall[] | undefined; -- /** -- * Returns the first executable code from the first candidate in the response. -- * -- * @remarks -- * If there are multiple candidates in the response, the executable code from -- * the first one will be returned. -- * If there are no executable code in the response, undefined will be -- * returned. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateContent({ -- * model: 'gemini-2.0-flash', -- * contents: -- * 'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.' -- * config: { -- * tools: [{codeExecution: {}}], -- * }, -- * }); -- * -- * console.debug(response.executableCode); -- * ``` -- */ -- get executableCode(): string | undefined; -- /** -- * Returns the first code execution result from the first candidate in the response. -- * -- * @remarks -- * If there are multiple candidates in the response, the code execution result from -- * the first one will be returned. -- * If there are no code execution result in the response, undefined will be returned. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateContent({ -- * model: 'gemini-2.0-flash', -- * contents: -- * 'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.' -- * config: { -- * tools: [{codeExecution: {}}], -- * }, -- * }); -- * -- * console.debug(response.codeExecutionResult); -- * ``` -- */ -- get codeExecutionResult(): string | undefined; --} -- --/** Content filter results for a prompt sent in the request. */ --export declare class GenerateContentResponsePromptFeedback { -- /** Output only. Blocked reason. */ -- blockReason?: BlockedReason; -- /** Output only. A readable block reason message. */ -- blockReasonMessage?: string; -- /** Output only. Safety ratings. */ -- safetyRatings?: SafetyRating[]; --} -- --/** Usage metadata about response(s). */ --export declare class GenerateContentResponseUsageMetadata { -- /** Output only. List of modalities of the cached content in the request input. */ -- cacheTokensDetails?: ModalityTokenCount[]; -- /** Output only. Number of tokens in the cached part in the input (the cached content). */ -- cachedContentTokenCount?: number; -- /** Number of tokens in the response(s). */ -- candidatesTokenCount?: number; -- /** Output only. List of modalities that were returned in the response. */ -- candidatesTokensDetails?: ModalityTokenCount[]; -- /** Number of tokens in the request. When `cached_content` is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content. */ -- promptTokenCount?: number; -- /** Output only. List of modalities that were processed in the request input. */ -- promptTokensDetails?: ModalityTokenCount[]; -- /** Output only. Number of tokens present in thoughts output. */ -- thoughtsTokenCount?: number; -- /** Output only. Number of tokens present in tool-use prompt(s). */ -- toolUsePromptTokenCount?: number; -- /** Output only. List of modalities that were processed for tool-use request inputs. */ -- toolUsePromptTokensDetails?: ModalityTokenCount[]; -- /** Total token count for prompt, response candidates, and tool-use prompts (if present). */ -- totalTokenCount?: number; -- /** Output only. Traffic type. This shows whether a request consumes Pay-As-You-Go or Provisioned Throughput quota. */ -- trafficType?: TrafficType; --} -- --/** An output image. */ --export declare interface GeneratedImage { -- /** The output image data. -- */ -- image?: Image_2; -- /** Responsible AI filter reason if the image is filtered out of the -- response. -- */ -- raiFilteredReason?: string; -- /** Safety attributes of the image. Lists of RAI categories and their -- scores of each content. -- */ -- safetyAttributes?: SafetyAttributes; -- /** The rewritten prompt used for the image generation if the prompt -- enhancer is enabled. -- */ -- enhancedPrompt?: string; --} -- --/** A generated video. */ --export declare interface GeneratedVideo { -- /** The output video */ -- video?: Video; --} -- --/** The config for generating an images. */ --export declare interface GenerateImagesConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Cloud Storage URI used to store the generated images. -- */ -- outputGcsUri?: string; -- /** Description of what to discourage in the generated images. -- */ -- negativePrompt?: string; -- /** Number of images to generate. -- */ -- numberOfImages?: number; -- /** Aspect ratio of the generated images. -- */ -- aspectRatio?: string; -- /** Controls how much the model adheres to the text prompt. Large -- values increase output and prompt alignment, but may compromise image -- quality. -- */ -- guidanceScale?: number; -- /** Random seed for image generation. This is not available when -- ``add_watermark`` is set to true. -- */ -- seed?: number; -- /** Filter level for safety filtering. -- */ -- safetyFilterLevel?: SafetyFilterLevel; -- /** Allows generation of people by the model. -- */ -- personGeneration?: PersonGeneration; -- /** Whether to report the safety scores of each generated image and -- the positive prompt in the response. -- */ -- includeSafetyAttributes?: boolean; -- /** Whether to include the Responsible AI filter reason if the image -- is filtered out of the response. -- */ -- includeRaiReason?: boolean; -- /** Language of the text in the prompt. -- */ -- language?: ImagePromptLanguage; -- /** MIME type of the generated image. -- */ -- outputMimeType?: string; -- /** Compression quality of the generated image (for ``image/jpeg`` -- only). -- */ -- outputCompressionQuality?: number; -- /** Whether to add a watermark to the generated images. -- */ -- addWatermark?: boolean; -- /** Whether to use the prompt rewriting logic. -- */ -- enhancePrompt?: boolean; --} -- --/** The parameters for generating images. */ --export declare interface GenerateImagesParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** Text prompt that typically describes the images to output. -- */ -- prompt: string; -- /** Configuration for generating images. -- */ -- config?: GenerateImagesConfig; --} -- --/** The output images response. */ --export declare class GenerateImagesResponse { -- /** List of generated images. -- */ -- generatedImages?: GeneratedImage[]; -- /** Safety attributes of the positive prompt. Only populated if -- ``include_safety_attributes`` is set to True. -- */ -- positivePromptSafetyAttributes?: SafetyAttributes; --} -- --/** Configuration for generating videos. */ --export declare interface GenerateVideosConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Number of output videos. */ -- numberOfVideos?: number; -- /** The gcs bucket where to save the generated videos. */ -- outputGcsUri?: string; -- /** Frames per second for video generation. */ -- fps?: number; -- /** Duration of the clip for video generation in seconds. */ -- durationSeconds?: number; -- /** The RNG seed. If RNG seed is exactly same for each request with unchanged inputs, the prediction results will be consistent. Otherwise, a random RNG seed will be used each time to produce a different result. */ -- seed?: number; -- /** The aspect ratio for the generated video. 16:9 (landscape) and 9:16 (portrait) are supported. */ -- aspectRatio?: string; -- /** The resolution for the generated video. 1280x720, 1920x1080 are supported. */ -- resolution?: string; -- /** Whether allow to generate person videos, and restrict to specific ages. Supported values are: dont_allow, allow_adult. */ -- personGeneration?: string; -- /** The pubsub topic where to publish the video generation progress. */ -- pubsubTopic?: string; -- /** Optional field in addition to the text content. Negative prompts can be explicitly stated here to help generate the video. */ -- negativePrompt?: string; -- /** Whether to use the prompt rewriting logic. */ -- enhancePrompt?: boolean; --} -- --/** A video generation operation. */ --export declare interface GenerateVideosOperation { -- /** The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`. */ -- name?: string; -- /** Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any. */ -- metadata?: Record; -- /** If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available. */ -- done?: boolean; -- /** The error result of the operation in case of failure or cancellation. */ -- error?: Record; -- /** The generated videos. */ -- response?: GenerateVideosResponse; --} -- --/** Class that represents the parameters for generating an image. */ --export declare interface GenerateVideosParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** The text prompt for generating the videos. Optional for image to video use cases. */ -- prompt?: string; -- /** The input image for generating the videos. -- Optional if prompt is provided. */ -- image?: Image_2; -- /** Configuration for generating videos. */ -- config?: GenerateVideosConfig; --} -- --/** Response with generated videos. */ --export declare class GenerateVideosResponse { -- /** List of the generated videos */ -- generatedVideos?: GeneratedVideo[]; -- /** Returns if any videos were filtered due to RAI policies. */ -- raiMediaFilteredCount?: number; -- /** Returns rai failure reasons if any. */ -- raiMediaFilteredReasons?: string[]; --} -- --/** Generation config. */ --export declare interface GenerationConfig { -- /** Optional. If enabled, audio timestamp will be included in the request to the model. */ -- audioTimestamp?: boolean; -- /** Optional. Number of candidates to generate. */ -- candidateCount?: number; -- /** Optional. Frequency penalties. */ -- frequencyPenalty?: number; -- /** Optional. Logit probabilities. */ -- logprobs?: number; -- /** Optional. The maximum number of output tokens to generate per message. */ -- maxOutputTokens?: number; -- /** Optional. If specified, the media resolution specified will be used. */ -- mediaResolution?: MediaResolution; -- /** Optional. Positive penalties. */ -- presencePenalty?: number; -- /** Optional. If true, export the logprobs results in response. */ -- responseLogprobs?: boolean; -- /** Optional. Output response mimetype of the generated candidate text. Supported mimetype: - `text/plain`: (default) Text output. - `application/json`: JSON response in the candidates. The model needs to be prompted to output the appropriate response type, otherwise the behavior is undefined. This is a preview feature. */ -- responseMimeType?: string; -- /** Optional. The `Schema` object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). If set, a compatible response_mime_type must also be set. Compatible mimetypes: `application/json`: Schema for JSON response. */ -- responseSchema?: Schema; -- /** Optional. Routing configuration. */ -- routingConfig?: GenerationConfigRoutingConfig; -- /** Optional. Seed. */ -- seed?: number; -- /** Optional. Stop sequences. */ -- stopSequences?: string[]; -- /** Optional. Controls the randomness of predictions. */ -- temperature?: number; -- /** Optional. If specified, top-k sampling will be used. */ -- topK?: number; -- /** Optional. If specified, nucleus sampling will be used. */ -- topP?: number; --} -- --/** The configuration for routing the request to a specific model. */ --export declare interface GenerationConfigRoutingConfig { -- /** Automated routing. */ -- autoMode?: GenerationConfigRoutingConfigAutoRoutingMode; -- /** Manual routing. */ -- manualMode?: GenerationConfigRoutingConfigManualRoutingMode; --} -- --/** When automated routing is specified, the routing will be determined by the pretrained routing model and customer provided model routing preference. */ --export declare interface GenerationConfigRoutingConfigAutoRoutingMode { -- /** The model routing preference. */ -- modelRoutingPreference?: 'UNKNOWN' | 'PRIORITIZE_QUALITY' | 'BALANCED' | 'PRIORITIZE_COST'; --} -- --/** When manual routing is set, the specified model will be used directly. */ --export declare interface GenerationConfigRoutingConfigManualRoutingMode { -- /** The model name to use. Only the public LLM models are accepted. e.g. 'gemini-1.5-pro-001'. */ -- modelName?: string; --} -- --/** Optional parameters for caches.get method. */ --export declare interface GetCachedContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for caches.get method. */ --export declare interface GetCachedContentParameters { -- /** The server-generated resource name of the cached content. -- */ -- name: string; -- /** Optional parameters for the request. -- */ -- config?: GetCachedContentConfig; --} -- --/** Used to override the default configuration. */ --export declare interface GetFileConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Generates the parameters for the get method. */ --export declare interface GetFileParameters { -- /** The name identifier for the file to retrieve. */ -- name: string; -- /** Used to override the default configuration. */ -- config?: GetFileConfig; --} -- --/** Optional parameters for models.get method. */ --export declare interface GetModelConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --export declare interface GetModelParameters { -- model: string; -- /** Optional parameters for the request. */ -- config?: GetModelConfig; --} -- --export declare interface GetOperationConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for the GET method. */ --export declare interface GetOperationParameters { -- /** The server-assigned name for the operation. */ -- operationName: string; -- /** Used to override the default configuration. */ -- config?: GetOperationConfig; --} -- --/** Optional parameters for tunings.get method. */ --export declare interface GetTuningJobConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; --} -- --/** Parameters for the get method. */ --export declare interface GetTuningJobParameters { -- name: string; -- /** Optional parameters for the request. */ -- config?: GetTuningJobConfig; --} -- --/** -- * The Google GenAI SDK. -- * -- * @remarks -- * Provides access to the GenAI features through either the {@link -- * https://cloud.google.com/vertex-ai/docs/reference/rest | Gemini API} or -- * the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI -- * API}. -- * -- * The {@link GoogleGenAIOptions.vertexai} value determines which of the API -- * services to use. -- * -- * When using the Gemini API, a {@link GoogleGenAIOptions.apiKey} must also be -- * set. When using Vertex AI, currently only {@link GoogleGenAIOptions.apiKey} -- * is supported via Express mode. {@link GoogleGenAIOptions.project} and {@link -- * GoogleGenAIOptions.location} should not be set. -- * -- * @example -- * Initializing the SDK for using the Gemini API: -- * ```ts -- * import {GoogleGenAI} from '@google/genai'; -- * const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'}); -- * ``` -- * -- * @example -- * Initializing the SDK for using the Vertex AI API: -- * ```ts -- * import {GoogleGenAI} from '@google/genai'; -- * const ai = new GoogleGenAI({ -- * vertexai: true, -- * project: 'PROJECT_ID', -- * location: 'PROJECT_LOCATION' -- * }); -- * ``` -- * -- */ --export declare class GoogleGenAI { -- protected readonly apiClient: ApiClient; -- private readonly apiKey?; -- readonly vertexai: boolean; -- private readonly apiVersion?; -- readonly models: Models; -- readonly live: Live; -- readonly chats: Chats; -- readonly caches: Caches; -- readonly files: Files; -- readonly operations: Operations; -- readonly tunings: Tunings; -- constructor(options: GoogleGenAIOptions); --} -- --/** -- * Google Gen AI SDK's configuration options. -- * -- * See {@link GoogleGenAI} for usage samples. -- */ --export declare interface GoogleGenAIOptions { -- /** -- * Optional. Determines whether to use the Vertex AI or the Gemini API. -- * -- * @remarks -- * When true, the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI API} will used. -- * When false, the {@link https://ai.google.dev/api | Gemini API} will be used. -- * -- * If unset, default SDK behavior is to use the Gemini API service. -- */ -- vertexai?: boolean; -- /** -- * Optional. The Google Cloud project ID for Vertex AI clients. -- * -- * Find your project ID: https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects -- * -- * @remarks -- * Only supported on Node runtimes, ignored on browser runtimes. -- */ -- project?: string; -- /** -- * Optional. The Google Cloud project {@link https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations | location} for Vertex AI clients. -- * -- * @remarks -- * Only supported on Node runtimes, ignored on browser runtimes. -- * -- */ -- location?: string; -- /** -- * The API Key, required for Gemini API clients. -- * -- * @remarks -- * Required on browser runtimes. -- */ -- apiKey?: string; -- /** -- * Optional. The API version to use. -- * -- * @remarks -- * If unset, the default API version will be used. -- */ -- apiVersion?: string; -- /** -- * Optional. Authentication options defined by the by google-auth-library for Vertex AI clients. -- * -- * @remarks -- * @see {@link https://github.com/googleapis/google-auth-library-nodejs/blob/v9.15.0/src/auth/googleauth.ts | GoogleAuthOptions interface in google-auth-library-nodejs}. -- * -- * Only supported on Node runtimes, ignored on browser runtimes. -- * -- */ -- googleAuthOptions?: GoogleAuthOptions; -- /** -- * Optional. A set of customizable configuration for HTTP requests. -- */ -- httpOptions?: HttpOptions; --} -- --/** Tool to support Google Maps in Model. */ --export declare interface GoogleMaps { -- /** Optional. Auth config for the Google Maps tool. */ -- authConfig?: AuthConfig; --} -- --/** The `Status` type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by [gRPC](https://github.com/grpc). Each `Status` message contains three pieces of data: error code, error message, and error details. You can find out more about this error model and how to work with it in the [API Design Guide](https://cloud.google.com/apis/design/errors). */ --export declare interface GoogleRpcStatus { -- /** The status code, which should be an enum value of google.rpc.Code. */ -- code?: number; -- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ -- details?: Record[]; -- /** A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the google.rpc.Status.details field, or localized by the client. */ -- message?: string; --} -- --/** Tool to support Google Search in Model. Powered by Google. */ --export declare interface GoogleSearch { -- /** Optional. Filter search results to a specific time range. -- If customers set a start time, they must set an end time (and vice versa). -- */ -- timeRangeFilter?: Interval; --} -- --/** Tool to retrieve public web data for grounding, powered by Google. */ --export declare interface GoogleSearchRetrieval { -- /** Specifies the dynamic retrieval configuration for the given source. */ -- dynamicRetrievalConfig?: DynamicRetrievalConfig; --} -- --/** Represents a whole or partial calendar date, such as a birthday. The time of day and time zone are either specified elsewhere or are insignificant. The date is relative to the Gregorian Calendar. This can represent one of the following: * A full date, with non-zero year, month, and day values. * A month and day, with a zero year (for example, an anniversary). * A year on its own, with a zero month and a zero day. * A year and month, with a zero day (for example, a credit card expiration date). Related types: * google.type.TimeOfDay * google.type.DateTime * google.protobuf.Timestamp */ --export declare interface GoogleTypeDate { -- /** Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant. */ -- day?: number; -- /** Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day. */ -- month?: number; -- /** Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year. */ -- year?: number; --} -- --/** Grounding chunk. */ --export declare interface GroundingChunk { -- /** Grounding chunk from context retrieved by the retrieval tools. */ -- retrievedContext?: GroundingChunkRetrievedContext; -- /** Grounding chunk from the web. */ -- web?: GroundingChunkWeb; --} -- --/** Chunk from context retrieved by the retrieval tools. */ --export declare interface GroundingChunkRetrievedContext { -- /** Text of the attribution. */ -- text?: string; -- /** Title of the attribution. */ -- title?: string; -- /** URI reference of the attribution. */ -- uri?: string; --} -- --/** Chunk from the web. */ --export declare interface GroundingChunkWeb { -- /** Domain of the (original) URI. */ -- domain?: string; -- /** Title of the chunk. */ -- title?: string; -- /** URI reference of the chunk. */ -- uri?: string; --} -- --/** Metadata returned to client when grounding is enabled. */ --export declare interface GroundingMetadata { -- /** List of supporting references retrieved from specified grounding source. */ -- groundingChunks?: GroundingChunk[]; -- /** Optional. List of grounding support. */ -- groundingSupports?: GroundingSupport[]; -- /** Optional. Output only. Retrieval metadata. */ -- retrievalMetadata?: RetrievalMetadata; -- /** Optional. Queries executed by the retrieval tools. */ -- retrievalQueries?: string[]; -- /** Optional. Google search entry for the following-up web searches. */ -- searchEntryPoint?: SearchEntryPoint; -- /** Optional. Web search queries for the following-up web search. */ -- webSearchQueries?: string[]; --} -- --/** Grounding support. */ --export declare interface GroundingSupport { -- /** Confidence score of the support references. Ranges from 0 to 1. 1 is the most confident. This list must have the same size as the grounding_chunk_indices. */ -- confidenceScores?: number[]; -- /** A list of indices (into 'grounding_chunk') specifying the citations associated with the claim. For instance [1,3,4] means that grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] are the retrieved content attributed to the claim. */ -- groundingChunkIndices?: number[]; -- /** Segment of the content this support belongs to. */ -- segment?: Segment; --} -- --/** Optional. Specify if the threshold is used for probability or severity score. If not specified, the threshold is used for probability score. */ --export declare enum HarmBlockMethod { -- /** -- * The harm block method is unspecified. -- */ -- HARM_BLOCK_METHOD_UNSPECIFIED = "HARM_BLOCK_METHOD_UNSPECIFIED", -- /** -- * The harm block method uses both probability and severity scores. -- */ -- SEVERITY = "SEVERITY", -- /** -- * The harm block method uses the probability score. -- */ -- PROBABILITY = "PROBABILITY" --} -- --/** Required. The harm block threshold. */ --export declare enum HarmBlockThreshold { -- /** -- * Unspecified harm block threshold. -- */ -- HARM_BLOCK_THRESHOLD_UNSPECIFIED = "HARM_BLOCK_THRESHOLD_UNSPECIFIED", -- /** -- * Block low threshold and above (i.e. block more). -- */ -- BLOCK_LOW_AND_ABOVE = "BLOCK_LOW_AND_ABOVE", -- /** -- * Block medium threshold and above. -- */ -- BLOCK_MEDIUM_AND_ABOVE = "BLOCK_MEDIUM_AND_ABOVE", -- /** -- * Block only high threshold (i.e. block less). -- */ -- BLOCK_ONLY_HIGH = "BLOCK_ONLY_HIGH", -- /** -- * Block none. -- */ -- BLOCK_NONE = "BLOCK_NONE", -- /** -- * Turn off the safety filter. -- */ -- OFF = "OFF" --} -- --/** Required. Harm category. */ --export declare enum HarmCategory { -- /** -- * The harm category is unspecified. -- */ -- HARM_CATEGORY_UNSPECIFIED = "HARM_CATEGORY_UNSPECIFIED", -- /** -- * The harm category is hate speech. -- */ -- HARM_CATEGORY_HATE_SPEECH = "HARM_CATEGORY_HATE_SPEECH", -- /** -- * The harm category is dangerous content. -- */ -- HARM_CATEGORY_DANGEROUS_CONTENT = "HARM_CATEGORY_DANGEROUS_CONTENT", -- /** -- * The harm category is harassment. -- */ -- HARM_CATEGORY_HARASSMENT = "HARM_CATEGORY_HARASSMENT", -- /** -- * The harm category is sexually explicit content. -- */ -- HARM_CATEGORY_SEXUALLY_EXPLICIT = "HARM_CATEGORY_SEXUALLY_EXPLICIT", -- /** -- * The harm category is civic integrity. -- */ -- HARM_CATEGORY_CIVIC_INTEGRITY = "HARM_CATEGORY_CIVIC_INTEGRITY" --} -- --/** Output only. Harm probability levels in the content. */ --export declare enum HarmProbability { -- /** -- * Harm probability unspecified. -- */ -- HARM_PROBABILITY_UNSPECIFIED = "HARM_PROBABILITY_UNSPECIFIED", -- /** -- * Negligible level of harm. -- */ -- NEGLIGIBLE = "NEGLIGIBLE", -- /** -- * Low level of harm. -- */ -- LOW = "LOW", -- /** -- * Medium level of harm. -- */ -- MEDIUM = "MEDIUM", -- /** -- * High level of harm. -- */ -- HIGH = "HIGH" --} -- --/** Output only. Harm severity levels in the content. */ --export declare enum HarmSeverity { -- /** -- * Harm severity unspecified. -- */ -- HARM_SEVERITY_UNSPECIFIED = "HARM_SEVERITY_UNSPECIFIED", -- /** -- * Negligible level of harm severity. -- */ -- HARM_SEVERITY_NEGLIGIBLE = "HARM_SEVERITY_NEGLIGIBLE", -- /** -- * Low level of harm severity. -- */ -- HARM_SEVERITY_LOW = "HARM_SEVERITY_LOW", -- /** -- * Medium level of harm severity. -- */ -- HARM_SEVERITY_MEDIUM = "HARM_SEVERITY_MEDIUM", -- /** -- * High level of harm severity. -- */ -- HARM_SEVERITY_HIGH = "HARM_SEVERITY_HIGH" --} -- --/** HTTP options to be used in each of the requests. */ --export declare interface HttpOptions { -- /** The base URL for the AI platform service endpoint. */ -- baseUrl?: string; -- /** Specifies the version of the API to use. */ -- apiVersion?: string; -- /** Additional HTTP headers to be sent with the request. */ -- headers?: Record; -- /** Timeout for the request in milliseconds. */ -- timeout?: number; --} -- --/** -- * Represents the necessary information to send a request to an API endpoint. -- * This interface defines the structure for constructing and executing HTTP -- * requests. -- */ --declare interface HttpRequest { -- /** -- * URL path from the modules, this path is appended to the base API URL to -- * form the complete request URL. -- * -- * If you wish to set full URL, use httpOptions.baseUrl instead. Example to -- * set full URL in the request: -- * -- * const request: HttpRequest = { -- * path: '', -- * httpOptions: { -- * baseUrl: 'https://', -- * apiVersion: '', -- * }, -- * httpMethod: 'GET', -- * }; -- * -- * The result URL will be: https:// -- * -- */ -- path: string; -- /** -- * Optional query parameters to be appended to the request URL. -- */ -- queryParams?: Record; -- /** -- * Optional request body in json string or Blob format, GET request doesn't -- * need a request body. -- */ -- body?: string | Blob; -- /** -- * The HTTP method to be used for the request. -- */ -- httpMethod: 'GET' | 'POST' | 'PATCH' | 'DELETE'; -- /** -- * Optional set of customizable configuration for HTTP requests. -- */ -- httpOptions?: HttpOptions; -- /** -- * Optional abort signal which can be used to cancel the request. -- */ -- abortSignal?: AbortSignal; --} -- --/** A wrapper class for the http response. */ --export declare class HttpResponse { -- /** Used to retain the processed HTTP headers in the response. */ -- headers?: Record; -- /** -- * The original http response. -- */ -- responseInternal: Response; -- constructor(response: Response); -- json(): Promise; --} -- --/** An image. */ --declare interface Image_2 { -- /** The Cloud Storage URI of the image. ``Image`` can contain a value -- for this field or the ``image_bytes`` field but not both. -- */ -- gcsUri?: string; -- /** The image bytes data. ``Image`` can contain a value for this field -- or the ``gcs_uri`` field but not both. -- */ -- imageBytes?: string; -- /** The MIME type of the image. */ -- mimeType?: string; --} --export { Image_2 as Image } -- --/** Enum that specifies the language of the text in the prompt. */ --export declare enum ImagePromptLanguage { -- auto = "auto", -- en = "en", -- ja = "ja", -- ko = "ko", -- hi = "hi" --} -- --/** Represents a time interval, encoded as a start time (inclusive) and an end time (exclusive). -- -- The start time must be less than or equal to the end time. -- When the start equals the end time, the interval is an empty interval. -- (matches no time) -- When both start and end are unspecified, the interval matches any time. -- */ --export declare interface Interval { -- /** The start time of the interval. */ -- startTime?: string; -- /** The end time of the interval. */ -- endTime?: string; --} -- --/** Output only. The detailed state of the job. */ --export declare enum JobState { -- /** -- * The job state is unspecified. -- */ -- JOB_STATE_UNSPECIFIED = "JOB_STATE_UNSPECIFIED", -- /** -- * The job has been just created or resumed and processing has not yet begun. -- */ -- JOB_STATE_QUEUED = "JOB_STATE_QUEUED", -- /** -- * The service is preparing to run the job. -- */ -- JOB_STATE_PENDING = "JOB_STATE_PENDING", -- /** -- * The job is in progress. -- */ -- JOB_STATE_RUNNING = "JOB_STATE_RUNNING", -- /** -- * The job completed successfully. -- */ -- JOB_STATE_SUCCEEDED = "JOB_STATE_SUCCEEDED", -- /** -- * The job failed. -- */ -- JOB_STATE_FAILED = "JOB_STATE_FAILED", -- /** -- * The job is being cancelled. From this state the job may only go to either `JOB_STATE_SUCCEEDED`, `JOB_STATE_FAILED` or `JOB_STATE_CANCELLED`. -- */ -- JOB_STATE_CANCELLING = "JOB_STATE_CANCELLING", -- /** -- * The job has been cancelled. -- */ -- JOB_STATE_CANCELLED = "JOB_STATE_CANCELLED", -- /** -- * The job has been stopped, and can be resumed. -- */ -- JOB_STATE_PAUSED = "JOB_STATE_PAUSED", -- /** -- * The job has expired. -- */ -- JOB_STATE_EXPIRED = "JOB_STATE_EXPIRED", -- /** -- * The job is being updated. Only jobs in the `RUNNING` state can be updated. After updating, the job goes back to the `RUNNING` state. -- */ -- JOB_STATE_UPDATING = "JOB_STATE_UPDATING", -- /** -- * The job is partially succeeded, some results may be missing due to errors. -- */ -- JOB_STATE_PARTIALLY_SUCCEEDED = "JOB_STATE_PARTIALLY_SUCCEEDED" --} -- --/** Required. Programming language of the `code`. */ --export declare enum Language { -- /** -- * Unspecified language. This value should not be used. -- */ -- LANGUAGE_UNSPECIFIED = "LANGUAGE_UNSPECIFIED", -- /** -- * Python >= 3.10, with numpy and simpy available. -- */ -- PYTHON = "PYTHON" --} -- --/** An object that represents a latitude/longitude pair. -- -- This is expressed as a pair of doubles to represent degrees latitude and -- degrees longitude. Unless specified otherwise, this object must conform to the -- -- WGS84 standard. Values must be within normalized ranges. -- */ --export declare interface LatLng { -- /** The latitude in degrees. It must be in the range [-90.0, +90.0]. */ -- latitude?: number; -- /** The longitude in degrees. It must be in the range [-180.0, +180.0] */ -- longitude?: number; --} -- --/** Config for caches.list method. */ --export declare interface ListCachedContentsConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- pageSize?: number; -- pageToken?: string; --} -- --/** Parameters for caches.list method. */ --export declare interface ListCachedContentsParameters { -- /** Configuration that contains optional parameters. -- */ -- config?: ListCachedContentsConfig; --} -- --export declare class ListCachedContentsResponse { -- nextPageToken?: string; -- /** List of cached contents. -- */ -- cachedContents?: CachedContent[]; --} -- --/** Used to override the default configuration. */ --export declare interface ListFilesConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- pageSize?: number; -- pageToken?: string; --} -- --/** Generates the parameters for the list method. */ --export declare interface ListFilesParameters { -- /** Used to override the default configuration. */ -- config?: ListFilesConfig; --} -- --/** Response for the list files method. */ --export declare class ListFilesResponse { -- /** A token to retrieve next page of results. */ -- nextPageToken?: string; -- /** The list of files. */ -- files?: File_2[]; --} -- --export declare interface ListModelsConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- pageSize?: number; -- pageToken?: string; -- filter?: string; -- /** Set true to list base models, false to list tuned models. */ -- queryBase?: boolean; --} -- --export declare interface ListModelsParameters { -- config?: ListModelsConfig; --} -- --export declare class ListModelsResponse { -- nextPageToken?: string; -- models?: Model[]; --} -- --/** Configuration for the list tuning jobs method. */ --export declare interface ListTuningJobsConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- pageSize?: number; -- pageToken?: string; -- filter?: string; --} -- --/** Parameters for the list tuning jobs method. */ --export declare interface ListTuningJobsParameters { -- config?: ListTuningJobsConfig; --} -- --/** Response for the list tuning jobs method. */ --export declare class ListTuningJobsResponse { -- /** A token to retrieve the next page of results. Pass to ListTuningJobsRequest.page_token to obtain that page. */ -- nextPageToken?: string; -- /** List of TuningJobs in the requested page. */ -- tuningJobs?: TuningJob[]; --} -- --/** -- Live class encapsulates the configuration for live interaction with the -- Generative Language API. It embeds ApiClient for general API settings. -- -- @experimental -- */ --export declare class Live { -- private readonly apiClient; -- private readonly auth; -- private readonly webSocketFactory; -- readonly music: LiveMusic; -- constructor(apiClient: ApiClient, auth: Auth, webSocketFactory: WebSocketFactory); -- /** -- Establishes a connection to the specified model with the given -- configuration and returns a Session object representing that connection. -- -- @experimental Built-in MCP support is an experimental feature, may change in -- future versions. -- -- @remarks -- -- @param params - The parameters for establishing a connection to the model. -- @return A live session. -- -- @example -- ```ts -- let model: string; -- if (GOOGLE_GENAI_USE_VERTEXAI) { -- model = 'gemini-2.0-flash-live-preview-04-09'; -- } else { -- model = 'gemini-2.0-flash-live-001'; -- } -- const session = await ai.live.connect({ -- model: model, -- config: { -- responseModalities: [Modality.AUDIO], -- }, -- callbacks: { -- onopen: () => { -- console.log('Connected to the socket.'); -- }, -- onmessage: (e: MessageEvent) => { -- console.log('Received message from the server: %s\n', debug(e.data)); -- }, -- onerror: (e: ErrorEvent) => { -- console.log('Error occurred: %s\n', debug(e.error)); -- }, -- onclose: (e: CloseEvent) => { -- console.log('Connection closed.'); -- }, -- }, -- }); -- ``` -- */ -- connect(params: types.LiveConnectParameters): Promise; -- private isCallableTool; --} -- --/** Callbacks for the live API. */ --export declare interface LiveCallbacks { -- /** -- * Called when the websocket connection is established. -- */ -- onopen?: (() => void) | null; -- /** -- * Called when a message is received from the server. -- */ -- onmessage: (e: LiveServerMessage) => void; -- /** -- * Called when an error occurs. -- */ -- onerror?: ((e: ErrorEvent) => void) | null; -- /** -- * Called when the websocket connection is closed. -- */ -- onclose?: ((e: CloseEvent) => void) | null; --} -- --/** Incremental update of the current conversation delivered from the client. -- -- All the content here will unconditionally be appended to the conversation -- history and used as part of the prompt to the model to generate content. -- -- A message here will interrupt any current model generation. -- */ --export declare interface LiveClientContent { -- /** The content appended to the current conversation with the model. -- -- For single-turn queries, this is a single instance. For multi-turn -- queries, this is a repeated field that contains conversation history and -- latest request. -- */ -- turns?: Content[]; -- /** If true, indicates that the server content generation should start with -- the currently accumulated prompt. Otherwise, the server will await -- additional messages before starting generation. */ -- turnComplete?: boolean; --} -- --/** Messages sent by the client in the API call. */ --export declare interface LiveClientMessage { -- /** Message to be sent by the system when connecting to the API. SDK users should not send this message. */ -- setup?: LiveClientSetup; -- /** Incremental update of the current conversation delivered from the client. */ -- clientContent?: LiveClientContent; -- /** User input that is sent in real time. */ -- realtimeInput?: LiveClientRealtimeInput; -- /** Response to a `ToolCallMessage` received from the server. */ -- toolResponse?: LiveClientToolResponse; --} -- --/** User input that is sent in real time. -- -- This is different from `LiveClientContent` in a few ways: -- -- - Can be sent continuously without interruption to model generation. -- - If there is a need to mix data interleaved across the -- `LiveClientContent` and the `LiveClientRealtimeInput`, server attempts to -- optimize for best response, but there are no guarantees. -- - End of turn is not explicitly specified, but is rather derived from user -- activity (for example, end of speech). -- - Even before the end of turn, the data is processed incrementally -- to optimize for a fast start of the response from the model. -- - Is always assumed to be the user's input (cannot be used to populate -- conversation history). -- */ --export declare interface LiveClientRealtimeInput { -- /** Inlined bytes data for media input. */ -- mediaChunks?: Blob_2[]; -- /** The realtime audio input stream. */ -- audio?: Blob_2; -- /** -- Indicates that the audio stream has ended, e.g. because the microphone was -- turned off. -- -- This should only be sent when automatic activity detection is enabled -- (which is the default). -- -- The client can reopen the stream by sending an audio message. -- */ -- audioStreamEnd?: boolean; -- /** The realtime video input stream. */ -- video?: Blob_2; -- /** The realtime text input stream. */ -- text?: string; -- /** Marks the start of user activity. */ -- activityStart?: ActivityStart; -- /** Marks the end of user activity. */ -- activityEnd?: ActivityEnd; --} -- --/** Message contains configuration that will apply for the duration of the streaming session. */ --export declare interface LiveClientSetup { -- /** -- The fully qualified name of the publisher model or tuned model endpoint to -- use. -- */ -- model?: string; -- /** The generation configuration for the session. -- Note: only a subset of fields are supported. -- */ -- generationConfig?: GenerationConfig; -- /** The user provided system instructions for the model. -- Note: only text should be used in parts and content in each part will be -- in a separate paragraph. */ -- systemInstruction?: ContentUnion; -- /** A list of `Tools` the model may use to generate the next response. -- -- A `Tool` is a piece of code that enables the system to interact with -- external systems to perform an action, or set of actions, outside of -- knowledge and scope of the model. */ -- tools?: ToolListUnion; -- /** Configures the realtime input behavior in BidiGenerateContent. */ -- realtimeInputConfig?: RealtimeInputConfig; -- /** Configures session resumption mechanism. -- -- If included server will send SessionResumptionUpdate messages. */ -- sessionResumption?: SessionResumptionConfig; -- /** Configures context window compression mechanism. -- -- If included, server will compress context window to fit into given length. */ -- contextWindowCompression?: ContextWindowCompressionConfig; -- /** The transcription of the input aligns with the input audio language. -- */ -- inputAudioTranscription?: AudioTranscriptionConfig; -- /** The transcription of the output aligns with the language code -- specified for the output audio. -- */ -- outputAudioTranscription?: AudioTranscriptionConfig; -- /** Configures the proactivity of the model. This allows the model to respond proactively to -- the input and to ignore irrelevant input. */ -- proactivity?: ProactivityConfig; --} -- --/** Client generated response to a `ToolCall` received from the server. -- -- Individual `FunctionResponse` objects are matched to the respective -- `FunctionCall` objects by the `id` field. -- -- Note that in the unary and server-streaming GenerateContent APIs function -- calling happens by exchanging the `Content` parts, while in the bidi -- GenerateContent APIs function calling happens over this dedicated set of -- messages. -- */ --export declare class LiveClientToolResponse { -- /** The response to the function calls. */ -- functionResponses?: FunctionResponse[]; --} -- --/** Session config for the API connection. */ --export declare interface LiveConnectConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** The generation configuration for the session. */ -- generationConfig?: GenerationConfig; -- /** The requested modalities of the response. Represents the set of -- modalities that the model can return. Defaults to AUDIO if not specified. -- */ -- responseModalities?: Modality[]; -- /** Value that controls the degree of randomness in token selection. -- Lower temperatures are good for prompts that require a less open-ended or -- creative response, while higher temperatures can lead to more diverse or -- creative results. -- */ -- temperature?: number; -- /** Tokens are selected from the most to least probable until the sum -- of their probabilities equals this value. Use a lower value for less -- random responses and a higher value for more random responses. -- */ -- topP?: number; -- /** For each token selection step, the ``top_k`` tokens with the -- highest probabilities are sampled. Then tokens are further filtered based -- on ``top_p`` with the final token selected using temperature sampling. Use -- a lower number for less random responses and a higher number for more -- random responses. -- */ -- topK?: number; -- /** Maximum number of tokens that can be generated in the response. -- */ -- maxOutputTokens?: number; -- /** If specified, the media resolution specified will be used. -- */ -- mediaResolution?: MediaResolution; -- /** When ``seed`` is fixed to a specific number, the model makes a best -- effort to provide the same response for repeated requests. By default, a -- random number is used. -- */ -- seed?: number; -- /** The speech generation configuration. -- */ -- speechConfig?: SpeechConfig; -- /** If enabled, the model will detect emotions and adapt its responses accordingly. */ -- enableAffectiveDialog?: boolean; -- /** The user provided system instructions for the model. -- Note: only text should be used in parts and content in each part will be -- in a separate paragraph. */ -- systemInstruction?: ContentUnion; -- /** A list of `Tools` the model may use to generate the next response. -- -- A `Tool` is a piece of code that enables the system to interact with -- external systems to perform an action, or set of actions, outside of -- knowledge and scope of the model. */ -- tools?: ToolListUnion; -- /** Configures session resumption mechanism. -- -- If included the server will send SessionResumptionUpdate messages. */ -- sessionResumption?: SessionResumptionConfig; -- /** The transcription of the input aligns with the input audio language. -- */ -- inputAudioTranscription?: AudioTranscriptionConfig; -- /** The transcription of the output aligns with the language code -- specified for the output audio. -- */ -- outputAudioTranscription?: AudioTranscriptionConfig; -- /** Configures the realtime input behavior in BidiGenerateContent. */ -- realtimeInputConfig?: RealtimeInputConfig; -- /** Configures context window compression mechanism. -- -- If included, server will compress context window to fit into given length. */ -- contextWindowCompression?: ContextWindowCompressionConfig; -- /** Configures the proactivity of the model. This allows the model to respond proactively to -- the input and to ignore irrelevant input. */ -- proactivity?: ProactivityConfig; --} -- --/** Parameters for connecting to the live API. */ --export declare interface LiveConnectParameters { -- /** ID of the model to use. For a list of models, see `Google models -- `_. */ -- model: string; -- /** callbacks */ -- callbacks: LiveCallbacks; -- /** Optional configuration parameters for the request. -- */ -- config?: LiveConnectConfig; --} -- --/** Config for LiveEphemeralParameters for Auth Token creation. */ --export declare interface LiveEphemeralParameters { -- /** ID of the model to configure in the ephemeral token for Live API. -- For a list of models, see `Gemini models -- `. */ -- model?: string; -- /** Configuration specific to Live API connections created using this token. */ -- config?: LiveConnectConfig; --} -- --/** -- LiveMusic class encapsulates the configuration for live music -- generation via Lyria Live models. -- -- @experimental -- */ --declare class LiveMusic { -- private readonly apiClient; -- private readonly auth; -- private readonly webSocketFactory; -- constructor(apiClient: ApiClient, auth: Auth, webSocketFactory: WebSocketFactory); -- /** -- Establishes a connection to the specified model and returns a -- LiveMusicSession object representing that connection. -- -- @experimental -- -- @remarks -- -- @param params - The parameters for establishing a connection to the model. -- @return A live session. -- -- @example -- ```ts -- let model = 'models/lyria-realtime-exp'; -- const session = await ai.live.music.connect({ -- model: model, -- callbacks: { -- onmessage: (e: MessageEvent) => { -- console.log('Received message from the server: %s\n', debug(e.data)); -- }, -- onerror: (e: ErrorEvent) => { -- console.log('Error occurred: %s\n', debug(e.error)); -- }, -- onclose: (e: CloseEvent) => { -- console.log('Connection closed.'); -- }, -- }, -- }); -- ``` -- */ -- connect(params: types.LiveMusicConnectParameters): Promise; --} -- --/** Callbacks for the realtime music API. */ --export declare interface LiveMusicCallbacks { -- /** -- * Called when a message is received from the server. -- */ -- onmessage: (e: LiveMusicServerMessage) => void; -- /** -- * Called when an error occurs. -- */ -- onerror?: ((e: ErrorEvent) => void) | null; -- /** -- * Called when the websocket connection is closed. -- */ -- onclose?: ((e: CloseEvent) => void) | null; --} -- --/** User input to start or steer the music. */ --export declare interface LiveMusicClientContent { -- /** Weighted prompts as the model input. */ -- weightedPrompts?: WeightedPrompt[]; --} -- --/** Messages sent by the client in the LiveMusicClientMessage call. */ --export declare interface LiveMusicClientMessage { -- /** Message to be sent in the first (and only in the first) `LiveMusicClientMessage`. -- Clients should wait for a `LiveMusicSetupComplete` message before -- sending any additional messages. */ -- setup?: LiveMusicClientSetup; -- /** User input to influence music generation. */ -- clientContent?: LiveMusicClientContent; -- /** Configuration for music generation. */ -- musicGenerationConfig?: LiveMusicGenerationConfig; -- /** Playback control signal for the music generation. */ -- playbackControl?: LiveMusicPlaybackControl; --} -- --/** Message to be sent by the system when connecting to the API. */ --export declare interface LiveMusicClientSetup { -- /** The model's resource name. Format: `models/{model}`. */ -- model?: string; --} -- --/** Parameters for connecting to the live API. */ --export declare interface LiveMusicConnectParameters { -- /** The model's resource name. */ -- model: string; -- /** Callbacks invoked on server events. */ -- callbacks: LiveMusicCallbacks; --} -- --/** A prompt that was filtered with the reason. */ --export declare interface LiveMusicFilteredPrompt { -- /** The text prompt that was filtered. */ -- text?: string; -- /** The reason the prompt was filtered. */ -- filteredReason?: string; --} -- --/** Configuration for music generation. */ --export declare interface LiveMusicGenerationConfig { -- /** Controls the variance in audio generation. Higher values produce -- higher variance. Range is [0.0, 3.0]. */ -- temperature?: number; -- /** Controls how the model selects tokens for output. Samples the topK -- tokens with the highest probabilities. Range is [1, 1000]. */ -- topK?: number; -- /** Seeds audio generation. If not set, the request uses a randomly -- generated seed. */ -- seed?: number; -- /** Controls how closely the model follows prompts. -- Higher guidance follows more closely, but will make transitions more -- abrupt. Range is [0.0, 6.0]. */ -- guidance?: number; -- /** Beats per minute. Range is [60, 200]. */ -- bpm?: number; -- /** Density of sounds. Range is [0.0, 1.0]. */ -- density?: number; -- /** Brightness of the music. Range is [0.0, 1.0]. */ -- brightness?: number; -- /** Scale of the generated music. */ -- scale?: Scale; -- /** Whether the audio output should contain bass. */ -- muteBass?: boolean; -- /** Whether the audio output should contain drums. */ -- muteDrums?: boolean; -- /** Whether the audio output should contain only bass and drums. */ -- onlyBassAndDrums?: boolean; -- /** The mode of music generation. Default mode is QUALITY. */ -- musicGenerationMode?: MusicGenerationMode; --} -- --/** The playback control signal to apply to the music generation. */ --export declare enum LiveMusicPlaybackControl { -- /** -- * This value is unused. -- */ -- PLAYBACK_CONTROL_UNSPECIFIED = "PLAYBACK_CONTROL_UNSPECIFIED", -- /** -- * Start generating the music. -- */ -- PLAY = "PLAY", -- /** -- * Hold the music generation. Use PLAY to resume from the current position. -- */ -- PAUSE = "PAUSE", -- /** -- * Stop the music generation and reset the context (prompts retained). -- Use PLAY to restart the music generation. -- */ -- STOP = "STOP", -- /** -- * Reset the context of the music generation without stopping it. -- Retains the current prompts and config. -- */ -- RESET_CONTEXT = "RESET_CONTEXT" --} -- --/** Server update generated by the model in response to client messages. -- -- Content is generated as quickly as possible, and not in real time. -- Clients may choose to buffer and play it out in real time. -- */ --export declare interface LiveMusicServerContent { -- /** The audio chunks that the model has generated. */ -- audioChunks?: AudioChunk[]; --} -- --/** Response message for the LiveMusicClientMessage call. */ --export declare class LiveMusicServerMessage { -- /** Message sent in response to a `LiveMusicClientSetup` message from the client. -- Clients should wait for this message before sending any additional messages. */ -- setupComplete?: LiveMusicServerSetupComplete; -- /** Content generated by the model in response to client messages. */ -- serverContent?: LiveMusicServerContent; -- /** A prompt that was filtered with the reason. */ -- filteredPrompt?: LiveMusicFilteredPrompt; -- /** -- * Returns the first audio chunk from the server content, if present. -- * -- * @remarks -- * If there are no audio chunks in the response, undefined will be returned. -- */ -- get audioChunk(): AudioChunk | undefined; --} -- --/** Sent in response to a `LiveMusicClientSetup` message from the client. */ --export declare interface LiveMusicServerSetupComplete { --} -- --/** -- Represents a connection to the API. -- -- @experimental -- */ --export declare class LiveMusicSession { -- readonly conn: WebSocket_2; -- private readonly apiClient; -- constructor(conn: WebSocket_2, apiClient: ApiClient); -- /** -- Sets inputs to steer music generation. Updates the session's current -- weighted prompts. -- -- @param params - Contains one property, `weightedPrompts`. -- -- - `weightedPrompts` to send to the model; weights are normalized to -- sum to 1.0. -- -- @experimental -- */ -- setWeightedPrompts(params: types.LiveMusicSetWeightedPromptsParameters): Promise; -- /** -- Sets a configuration to the model. Updates the session's current -- music generation config. -- -- @param params - Contains one property, `musicGenerationConfig`. -- -- - `musicGenerationConfig` to set in the model. Passing an empty or -- undefined config to the model will reset the config to defaults. -- -- @experimental -- */ -- setMusicGenerationConfig(params: types.LiveMusicSetConfigParameters): Promise; -- private sendPlaybackControl; -- /** -- * Start the music stream. -- * -- * @experimental -- */ -- play(): void; -- /** -- * Temporarily halt the music stream. Use `play` to resume from the current -- * position. -- * -- * @experimental -- */ -- pause(): void; -- /** -- * Stop the music stream and reset the state. Retains the current prompts -- * and config. -- * -- * @experimental -- */ -- stop(): void; -- /** -- * Resets the context of the music generation without stopping it. -- * Retains the current prompts and config. -- * -- * @experimental -- */ -- resetContext(): void; -- /** -- Terminates the WebSocket connection. -- -- @experimental -- */ -- close(): void; --} -- --/** Parameters for setting config for the live music API. */ --export declare interface LiveMusicSetConfigParameters { -- /** Configuration for music generation. */ -- musicGenerationConfig: LiveMusicGenerationConfig; --} -- --/** Parameters for setting weighted prompts for the live music API. */ --export declare interface LiveMusicSetWeightedPromptsParameters { -- /** A map of text prompts to weights to use for the generation request. */ -- weightedPrompts: WeightedPrompt[]; --} -- --/** Prompts and config used for generating this audio chunk. */ --export declare interface LiveMusicSourceMetadata { -- /** Weighted prompts for generating this audio chunk. */ -- clientContent?: LiveMusicClientContent; -- /** Music generation config for generating this audio chunk. */ -- musicGenerationConfig?: LiveMusicGenerationConfig; --} -- --/** Parameters for sending client content to the live API. */ --export declare interface LiveSendClientContentParameters { -- /** Client content to send to the session. */ -- turns?: ContentListUnion; -- /** If true, indicates that the server content generation should start with -- the currently accumulated prompt. Otherwise, the server will await -- additional messages before starting generation. */ -- turnComplete?: boolean; --} -- --/** Parameters for sending realtime input to the live API. */ --export declare interface LiveSendRealtimeInputParameters { -- /** Realtime input to send to the session. */ -- media?: BlobImageUnion; -- /** The realtime audio input stream. */ -- audio?: Blob_2; -- /** -- Indicates that the audio stream has ended, e.g. because the microphone was -- turned off. -- -- This should only be sent when automatic activity detection is enabled -- (which is the default). -- -- The client can reopen the stream by sending an audio message. -- */ -- audioStreamEnd?: boolean; -- /** The realtime video input stream. */ -- video?: BlobImageUnion; -- /** The realtime text input stream. */ -- text?: string; -- /** Marks the start of user activity. */ -- activityStart?: ActivityStart; -- /** Marks the end of user activity. */ -- activityEnd?: ActivityEnd; --} -- --/** Parameters for sending tool responses to the live API. */ --export declare class LiveSendToolResponseParameters { -- /** Tool responses to send to the session. */ -- functionResponses: FunctionResponse[] | FunctionResponse; --} -- --/** Incremental server update generated by the model in response to client messages. -- -- Content is generated as quickly as possible, and not in real time. Clients -- may choose to buffer and play it out in real time. -- */ --export declare interface LiveServerContent { -- /** The content that the model has generated as part of the current conversation with the user. */ -- modelTurn?: Content; -- /** If true, indicates that the model is done generating. Generation will only start in response to additional client messages. Can be set alongside `content`, indicating that the `content` is the last in the turn. */ -- turnComplete?: boolean; -- /** If true, indicates that a client message has interrupted current model generation. If the client is playing out the content in realtime, this is a good signal to stop and empty the current queue. */ -- interrupted?: boolean; -- /** Metadata returned to client when grounding is enabled. */ -- groundingMetadata?: GroundingMetadata; -- /** If true, indicates that the model is done generating. When model is -- interrupted while generating there will be no generation_complete message -- in interrupted turn, it will go through interrupted > turn_complete. -- When model assumes realtime playback there will be delay between -- generation_complete and turn_complete that is caused by model -- waiting for playback to finish. If true, indicates that the model -- has finished generating all content. This is a signal to the client -- that it can stop sending messages. */ -- generationComplete?: boolean; -- /** Input transcription. The transcription is independent to the model -- turn which means it doesn’t imply any ordering between transcription and -- model turn. */ -- inputTranscription?: Transcription; -- /** Output transcription. The transcription is independent to the model -- turn which means it doesn’t imply any ordering between transcription and -- model turn. -- */ -- outputTranscription?: Transcription; -- /** Metadata related to url context retrieval tool. */ -- urlContextMetadata?: UrlContextMetadata; --} -- --/** Server will not be able to service client soon. */ --export declare interface LiveServerGoAway { -- /** The remaining time before the connection will be terminated as ABORTED. The minimal time returned here is specified differently together with the rate limits for a given model. */ -- timeLeft?: string; --} -- --/** Response message for API call. */ --export declare class LiveServerMessage { -- /** Sent in response to a `LiveClientSetup` message from the client. */ -- setupComplete?: LiveServerSetupComplete; -- /** Content generated by the model in response to client messages. */ -- serverContent?: LiveServerContent; -- /** Request for the client to execute the `function_calls` and return the responses with the matching `id`s. */ -- toolCall?: LiveServerToolCall; -- /** Notification for the client that a previously issued `ToolCallMessage` with the specified `id`s should have been not executed and should be cancelled. */ -- toolCallCancellation?: LiveServerToolCallCancellation; -- /** Usage metadata about model response(s). */ -- usageMetadata?: UsageMetadata; -- /** Server will disconnect soon. */ -- goAway?: LiveServerGoAway; -- /** Update of the session resumption state. */ -- sessionResumptionUpdate?: LiveServerSessionResumptionUpdate; -- /** -- * Returns the concatenation of all text parts from the server content if present. -- * -- * @remarks -- * If there are non-text parts in the response, the concatenation of all text -- * parts will be returned, and a warning will be logged. -- */ -- get text(): string | undefined; -- /** -- * Returns the concatenation of all inline data parts from the server content if present. -- * -- * @remarks -- * If there are non-inline data parts in the -- * response, the concatenation of all inline data parts will be returned, and -- * a warning will be logged. -- */ -- get data(): string | undefined; --} -- --/** Update of the session resumption state. -- -- Only sent if `session_resumption` was set in the connection config. -- */ --export declare interface LiveServerSessionResumptionUpdate { -- /** New handle that represents state that can be resumed. Empty if `resumable`=false. */ -- newHandle?: string; -- /** True if session can be resumed at this point. It might be not possible to resume session at some points. In that case we send update empty new_handle and resumable=false. Example of such case could be model executing function calls or just generating. Resuming session (using previous session token) in such state will result in some data loss. */ -- resumable?: boolean; -- /** Index of last message sent by client that is included in state represented by this SessionResumptionToken. Only sent when `SessionResumptionConfig.transparent` is set. -- -- Presence of this index allows users to transparently reconnect and avoid issue of losing some part of realtime audio input/video. If client wishes to temporarily disconnect (for example as result of receiving GoAway) they can do it without losing state by buffering messages sent since last `SessionResmumptionTokenUpdate`. This field will enable them to limit buffering (avoid keeping all requests in RAM). -- -- Note: This should not be used for when resuming a session at some time later -- in those cases partial audio and video frames arelikely not needed. */ -- lastConsumedClientMessageIndex?: string; --} -- --export declare interface LiveServerSetupComplete { --} -- --/** Request for the client to execute the `function_calls` and return the responses with the matching `id`s. */ --export declare interface LiveServerToolCall { -- /** The function call to be executed. */ -- functionCalls?: FunctionCall[]; --} -- --/** Notification for the client that a previously issued `ToolCallMessage` with the specified `id`s should have been not executed and should be cancelled. -- -- If there were side-effects to those tool calls, clients may attempt to undo -- the tool calls. This message occurs only in cases where the clients interrupt -- server turns. -- */ --export declare interface LiveServerToolCallCancellation { -- /** The ids of the tool calls to be cancelled. */ -- ids?: string[]; --} -- --/** Logprobs Result */ --export declare interface LogprobsResult { -- /** Length = total number of decoding steps. The chosen candidates may or may not be in top_candidates. */ -- chosenCandidates?: LogprobsResultCandidate[]; -- /** Length = total number of decoding steps. */ -- topCandidates?: LogprobsResultTopCandidates[]; --} -- --/** Candidate for the logprobs token and score. */ --export declare interface LogprobsResultCandidate { -- /** The candidate's log probability. */ -- logProbability?: number; -- /** The candidate's token string value. */ -- token?: string; -- /** The candidate's token id value. */ -- tokenId?: number; --} -- --/** Candidates with top log probabilities at each decoding step. */ --export declare interface LogprobsResultTopCandidates { -- /** Sorted by log probability in descending order. */ -- candidates?: LogprobsResultCandidate[]; --} -- --/** Configuration for a Mask reference image. */ --export declare interface MaskReferenceConfig { -- /** Prompts the model to generate a mask instead of you needing to -- provide one (unless MASK_MODE_USER_PROVIDED is used). */ -- maskMode?: MaskReferenceMode; -- /** A list of up to 5 class ids to use for semantic segmentation. -- Automatically creates an image mask based on specific objects. */ -- segmentationClasses?: number[]; -- /** Dilation percentage of the mask provided. -- Float between 0 and 1. */ -- maskDilation?: number; --} -- --/** A mask reference image. -- -- This encapsulates either a mask image provided by the user and configs for -- the user provided mask, or only config parameters for the model to generate -- a mask. -- -- A mask image is an image whose non-zero values indicate where to edit the base -- image. If the user provides a mask image, the mask must be in the same -- dimensions as the raw image. -- */ --export declare class MaskReferenceImage { -- /** The reference image for the editing operation. */ -- referenceImage?: Image_2; -- /** The id of the reference image. */ -- referenceId?: number; -- /** The type of the reference image. Only set by the SDK. */ -- referenceType?: string; -- /** Configuration for the mask reference image. */ -- config?: MaskReferenceConfig; -- /** Internal method to convert to ReferenceImageAPIInternal. */ -- toReferenceImageAPI(): any; --} -- --/** Enum representing the mask mode of a mask reference image. */ --export declare enum MaskReferenceMode { -- MASK_MODE_DEFAULT = "MASK_MODE_DEFAULT", -- MASK_MODE_USER_PROVIDED = "MASK_MODE_USER_PROVIDED", -- MASK_MODE_BACKGROUND = "MASK_MODE_BACKGROUND", -- MASK_MODE_FOREGROUND = "MASK_MODE_FOREGROUND", -- MASK_MODE_SEMANTIC = "MASK_MODE_SEMANTIC" --} -- --/** -- * Creates a McpCallableTool from MCP clients and an optional config. -- * -- * The callable tool can invoke the MCP clients with given function call -- * arguments. (often for automatic function calling). -- * Use the config to modify tool parameters such as behavior. -- * -- * @experimental Built-in MCP support is an experimental feature, may change in future -- * versions. -- */ --export declare function mcpToTool(...args: [...Client[], CallableToolConfig | Client]): CallableTool; -- --/** Server content modalities. */ --export declare enum MediaModality { -- /** -- * The modality is unspecified. -- */ -- MODALITY_UNSPECIFIED = "MODALITY_UNSPECIFIED", -- /** -- * Plain text. -- */ -- TEXT = "TEXT", -- /** -- * Images. -- */ -- IMAGE = "IMAGE", -- /** -- * Video. -- */ -- VIDEO = "VIDEO", -- /** -- * Audio. -- */ -- AUDIO = "AUDIO", -- /** -- * Document, e.g. PDF. -- */ -- DOCUMENT = "DOCUMENT" --} -- --/** The media resolution to use. */ --export declare enum MediaResolution { -- /** -- * Media resolution has not been set -- */ -- MEDIA_RESOLUTION_UNSPECIFIED = "MEDIA_RESOLUTION_UNSPECIFIED", -- /** -- * Media resolution set to low (64 tokens). -- */ -- MEDIA_RESOLUTION_LOW = "MEDIA_RESOLUTION_LOW", -- /** -- * Media resolution set to medium (256 tokens). -- */ -- MEDIA_RESOLUTION_MEDIUM = "MEDIA_RESOLUTION_MEDIUM", -- /** -- * Media resolution set to high (zoomed reframing with 256 tokens). -- */ -- MEDIA_RESOLUTION_HIGH = "MEDIA_RESOLUTION_HIGH" --} -- --/** Server content modalities. */ --export declare enum Modality { -- /** -- * The modality is unspecified. -- */ -- MODALITY_UNSPECIFIED = "MODALITY_UNSPECIFIED", -- /** -- * Indicates the model should return text -- */ -- TEXT = "TEXT", -- /** -- * Indicates the model should return images. -- */ -- IMAGE = "IMAGE", -- /** -- * Indicates the model should return images. -- */ -- AUDIO = "AUDIO" --} -- --/** Represents token counting info for a single modality. */ --export declare interface ModalityTokenCount { -- /** The modality associated with this token count. */ -- modality?: MediaModality; -- /** Number of tokens. */ -- tokenCount?: number; --} -- --/** The mode of the predictor to be used in dynamic retrieval. */ --export declare enum Mode { -- /** -- * Always trigger retrieval. -- */ -- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", -- /** -- * Run retrieval only when system decides it is necessary. -- */ -- MODE_DYNAMIC = "MODE_DYNAMIC" --} -- --/** A trained machine learning model. */ --export declare interface Model { -- /** Resource name of the model. */ -- name?: string; -- /** Display name of the model. */ -- displayName?: string; -- /** Description of the model. */ -- description?: string; -- /** Version ID of the model. A new version is committed when a new -- model version is uploaded or trained under an existing model ID. The -- version ID is an auto-incrementing decimal number in string -- representation. */ -- version?: string; -- /** List of deployed models created from this base model. Note that a -- model could have been deployed to endpoints in different locations. */ -- endpoints?: Endpoint[]; -- /** Labels with user-defined metadata to organize your models. */ -- labels?: Record; -- /** Information about the tuned model from the base model. */ -- tunedModelInfo?: TunedModelInfo; -- /** The maximum number of input tokens that the model can handle. */ -- inputTokenLimit?: number; -- /** The maximum number of output tokens that the model can generate. */ -- outputTokenLimit?: number; -- /** List of actions that are supported by the model. */ -- supportedActions?: string[]; -- /** The default checkpoint id of a model version. -- */ -- defaultCheckpointId?: string; -- /** The checkpoints of the model. */ -- checkpoints?: Checkpoint[]; --} -- --export declare class Models extends BaseModule { -- private readonly apiClient; -- constructor(apiClient: ApiClient); -- /** -- * Makes an API request to generate content with a given model. -- * -- * For the `model` parameter, supported formats for Vertex AI API include: -- * - The Gemini model ID, for example: 'gemini-2.0-flash' -- * - The full resource name starts with 'projects/', for example: -- * 'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-2.0-flash' -- * - The partial resource name with 'publishers/', for example: -- * 'publishers/google/models/gemini-2.0-flash' or -- * 'publishers/meta/models/llama-3.1-405b-instruct-maas' -- * - `/` separated publisher and model name, for example: -- * 'google/gemini-2.0-flash' or 'meta/llama-3.1-405b-instruct-maas' -- * -- * For the `model` parameter, supported formats for Gemini API include: -- * - The Gemini model ID, for example: 'gemini-2.0-flash' -- * - The model name starts with 'models/', for example: -- * 'models/gemini-2.0-flash' -- * - For tuned models, the model name starts with 'tunedModels/', -- * for example: -- * 'tunedModels/1234567890123456789' -- * -- * Some models support multimodal input and output. -- * -- * @param params - The parameters for generating content. -- * @return The response from generating content. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateContent({ -- * model: 'gemini-2.0-flash', -- * contents: 'why is the sky blue?', -- * config: { -- * candidateCount: 2, -- * } -- * }); -- * console.log(response); -- * ``` -- */ -- generateContent: (params: types.GenerateContentParameters) => Promise; -- /** -- * Makes an API request to generate content with a given model and yields the -- * response in chunks. -- * -- * For the `model` parameter, supported formats for Vertex AI API include: -- * - The Gemini model ID, for example: 'gemini-2.0-flash' -- * - The full resource name starts with 'projects/', for example: -- * 'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-2.0-flash' -- * - The partial resource name with 'publishers/', for example: -- * 'publishers/google/models/gemini-2.0-flash' or -- * 'publishers/meta/models/llama-3.1-405b-instruct-maas' -- * - `/` separated publisher and model name, for example: -- * 'google/gemini-2.0-flash' or 'meta/llama-3.1-405b-instruct-maas' -- * -- * For the `model` parameter, supported formats for Gemini API include: -- * - The Gemini model ID, for example: 'gemini-2.0-flash' -- * - The model name starts with 'models/', for example: -- * 'models/gemini-2.0-flash' -- * - For tuned models, the model name starts with 'tunedModels/', -- * for example: -- * 'tunedModels/1234567890123456789' -- * -- * Some models support multimodal input and output. -- * -- * @param params - The parameters for generating content with streaming response. -- * @return The response from generating content. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateContentStream({ -- * model: 'gemini-2.0-flash', -- * contents: 'why is the sky blue?', -- * config: { -- * maxOutputTokens: 200, -- * } -- * }); -- * for await (const chunk of response) { -- * console.log(chunk); -- * } -- * ``` -- */ -- generateContentStream: (params: types.GenerateContentParameters) => Promise>; -- /** -- * Transforms the CallableTools in the parameters to be simply Tools, it -- * copies the params into a new object and replaces the tools, it does not -- * modify the original params. Also sets the MCP usage header if there are -- * MCP tools in the parameters. -- */ -- private processParamsForMcpUsage; -- private initAfcToolsMap; -- private processAfcStream; -- /** -- * Generates an image based on a text description and configuration. -- * -- * @param params - The parameters for generating images. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await client.models.generateImages({ -- * model: 'imagen-3.0-generate-002', -- * prompt: 'Robot holding a red skateboard', -- * config: { -- * numberOfImages: 1, -- * includeRaiReason: true, -- * }, -- * }); -- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); -- * ``` -- */ -- generateImages: (params: types.GenerateImagesParameters) => Promise; -- list: (params?: types.ListModelsParameters) => Promise>; -- /** -- * Edits an image based on a prompt, list of reference images, and configuration. -- * -- * @param params - The parameters for editing an image. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await client.models.editImage({ -- * model: 'imagen-3.0-capability-001', -- * prompt: 'Generate an image containing a mug with the product logo [1] visible on the side of the mug.', -- * referenceImages: [subjectReferenceImage] -- * config: { -- * numberOfImages: 1, -- * includeRaiReason: true, -- * }, -- * }); -- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); -- * ``` -- */ -- editImage: (params: types.EditImageParameters) => Promise; -- /** -- * Upscales an image based on an image, upscale factor, and configuration. -- * Only supported in Vertex AI currently. -- * -- * @param params - The parameters for upscaling an image. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await client.models.upscaleImage({ -- * model: 'imagen-3.0-generate-002', -- * image: image, -- * upscaleFactor: 'x2', -- * config: { -- * includeRaiReason: true, -- * }, -- * }); -- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); -- * ``` -- */ -- upscaleImage: (params: types.UpscaleImageParameters) => Promise; -- private generateContentInternal; -- private generateContentStreamInternal; -- /** -- * Calculates embeddings for the given contents. Only text is supported. -- * -- * @param params - The parameters for embedding contents. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.embedContent({ -- * model: 'text-embedding-004', -- * contents: [ -- * 'What is your name?', -- * 'What is your favorite color?', -- * ], -- * config: { -- * outputDimensionality: 64, -- * }, -- * }); -- * console.log(response); -- * ``` -- */ -- embedContent(params: types.EmbedContentParameters): Promise; -- /** -- * Generates an image based on a text description and configuration. -- * -- * @param params - The parameters for generating images. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.generateImages({ -- * model: 'imagen-3.0-generate-002', -- * prompt: 'Robot holding a red skateboard', -- * config: { -- * numberOfImages: 1, -- * includeRaiReason: true, -- * }, -- * }); -- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); -- * ``` -- */ -- private generateImagesInternal; -- private editImageInternal; -- private upscaleImageInternal; -- /** -- * Fetches information about a model by name. -- * -- * @example -- * ```ts -- * const modelInfo = await ai.models.get({model: 'gemini-2.0-flash'}); -- * ``` -- */ -- get(params: types.GetModelParameters): Promise; -- private listInternal; -- /** -- * Updates a tuned model by its name. -- * -- * @param params - The parameters for updating the model. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.update({ -- * model: 'tuned-model-name', -- * config: { -- * displayName: 'New display name', -- * description: 'New description', -- * }, -- * }); -- * ``` -- */ -- update(params: types.UpdateModelParameters): Promise; -- /** -- * Deletes a tuned model by its name. -- * -- * @param params - The parameters for deleting the model. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.delete({model: 'tuned-model-name'}); -- * ``` -- */ -- delete(params: types.DeleteModelParameters): Promise; -- /** -- * Counts the number of tokens in the given contents. Multimodal input is -- * supported for Gemini models. -- * -- * @param params - The parameters for counting tokens. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.countTokens({ -- * model: 'gemini-2.0-flash', -- * contents: 'The quick brown fox jumps over the lazy dog.' -- * }); -- * console.log(response); -- * ``` -- */ -- countTokens(params: types.CountTokensParameters): Promise; -- /** -- * Given a list of contents, returns a corresponding TokensInfo containing -- * the list of tokens and list of token ids. -- * -- * This method is not supported by the Gemini Developer API. -- * -- * @param params - The parameters for computing tokens. -- * @return The response from the API. -- * -- * @example -- * ```ts -- * const response = await ai.models.computeTokens({ -- * model: 'gemini-2.0-flash', -- * contents: 'What is your name?' -- * }); -- * console.log(response); -- * ``` -- */ -- computeTokens(params: types.ComputeTokensParameters): Promise; -- /** -- * Generates videos based on a text description and configuration. -- * -- * @param params - The parameters for generating videos. -- * @return A Promise which allows you to track the progress and eventually retrieve the generated videos using the operations.get method. -- * -- * @example -- * ```ts -- * const operation = await ai.models.generateVideos({ -- * model: 'veo-2.0-generate-001', -- * prompt: 'A neon hologram of a cat driving at top speed', -- * config: { -- * numberOfVideos: 1 -- * }); -- * -- * while (!operation.done) { -- * await new Promise(resolve => setTimeout(resolve, 10000)); -- * operation = await ai.operations.getVideosOperation({operation: operation}); -- * } -- * -- * console.log(operation.response?.generatedVideos?.[0]?.video?.uri); -- * ``` -- */ -- generateVideos(params: types.GenerateVideosParameters): Promise; --} -- --/** Config for model selection. */ --export declare interface ModelSelectionConfig { -- /** Options for feature selection preference. */ -- featureSelectionPreference?: FeatureSelectionPreference; --} -- --/** The configuration for the multi-speaker setup. */ --export declare interface MultiSpeakerVoiceConfig { -- /** The configuration for the speaker to use. */ -- speakerVoiceConfigs?: SpeakerVoiceConfig[]; --} -- --/** The mode of music generation. */ --export declare enum MusicGenerationMode { -- /** -- * This value is unused. -- */ -- MUSIC_GENERATION_MODE_UNSPECIFIED = "MUSIC_GENERATION_MODE_UNSPECIFIED", -- /** -- * Steer text prompts to regions of latent space with higher quality -- music. -- */ -- QUALITY = "QUALITY", -- /** -- * Steer text prompts to regions of latent space with a larger diversity -- of music. -- */ -- DIVERSITY = "DIVERSITY" --} -- --/** A long-running operation. */ --export declare interface Operation { -- /** The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`. */ -- name?: string; -- /** Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any. */ -- metadata?: Record; -- /** If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available. */ -- done?: boolean; -- /** The error result of the operation in case of failure or cancellation. */ -- error?: Record; --} -- --/** Parameters for the get method of the operations module. */ --export declare interface OperationGetParameters { -- /** The operation to be retrieved. */ -- operation: GenerateVideosOperation; -- /** Used to override the default configuration. */ -- config?: GetOperationConfig; --} -- --export declare class Operations extends BaseModule { -- private readonly apiClient; -- constructor(apiClient: ApiClient); -- /** -- * Gets the status of a long-running operation. -- * -- * @param parameters The parameters for the get operation request. -- * @return The updated Operation object, with the latest status or result. -- */ -- getVideosOperation(parameters: types.OperationGetParameters): Promise; -- private getVideosOperationInternal; -- private fetchPredictVideosOperationInternal; --} -- --/** -- * @license -- * Copyright 2025 Google LLC -- * SPDX-License-Identifier: Apache-2.0 -- */ --/** Required. Outcome of the code execution. */ --export declare enum Outcome { -- /** -- * Unspecified status. This value should not be used. -- */ -- OUTCOME_UNSPECIFIED = "OUTCOME_UNSPECIFIED", -- /** -- * Code execution completed successfully. -- */ -- OUTCOME_OK = "OUTCOME_OK", -- /** -- * Code execution finished but with a failure. `stderr` should contain the reason. -- */ -- OUTCOME_FAILED = "OUTCOME_FAILED", -- /** -- * Code execution ran for too long, and was cancelled. There may or may not be a partial output present. -- */ -- OUTCOME_DEADLINE_EXCEEDED = "OUTCOME_DEADLINE_EXCEEDED" --} -- --/** -- * @license -- * Copyright 2025 Google LLC -- * SPDX-License-Identifier: Apache-2.0 -- */ --/** -- * Pagers for the GenAI List APIs. -- */ --export declare enum PagedItem { -- PAGED_ITEM_BATCH_JOBS = "batchJobs", -- PAGED_ITEM_MODELS = "models", -- PAGED_ITEM_TUNING_JOBS = "tuningJobs", -- PAGED_ITEM_FILES = "files", -- PAGED_ITEM_CACHED_CONTENTS = "cachedContents" --} -- --declare interface PagedItemConfig { -- config?: { -- pageToken?: string; -- pageSize?: number; -- }; --} -- --declare interface PagedItemResponse { -- nextPageToken?: string; -- batchJobs?: T[]; -- models?: T[]; -- tuningJobs?: T[]; -- files?: T[]; -- cachedContents?: T[]; --} -- --/** -- * Pager class for iterating through paginated results. -- */ --export declare class Pager implements AsyncIterable { -- private nameInternal; -- private pageInternal; -- private paramsInternal; -- private pageInternalSize; -- protected requestInternal: (params: PagedItemConfig) => Promise>; -- protected idxInternal: number; -- constructor(name: PagedItem, request: (params: PagedItemConfig) => Promise>, response: PagedItemResponse, params: PagedItemConfig); -- private init; -- private initNextPage; -- /** -- * Returns the current page, which is a list of items. -- * -- * @remarks -- * The first page is retrieved when the pager is created. The returned list of -- * items could be a subset of the entire list. -- */ -- get page(): T[]; -- /** -- * Returns the type of paged item (for example, ``batch_jobs``). -- */ -- get name(): PagedItem; -- /** -- * Returns the length of the page fetched each time by this pager. -- * -- * @remarks -- * The number of items in the page is less than or equal to the page length. -- */ -- get pageSize(): number; -- /** -- * Returns the parameters when making the API request for the next page. -- * -- * @remarks -- * Parameters contain a set of optional configs that can be -- * used to customize the API request. For example, the `pageToken` parameter -- * contains the token to request the next page. -- */ -- get params(): PagedItemConfig; -- /** -- * Returns the total number of items in the current page. -- */ -- get pageLength(): number; -- /** -- * Returns the item at the given index. -- */ -- getItem(index: number): T; -- /** -- * Returns an async iterator that support iterating through all items -- * retrieved from the API. -- * -- * @remarks -- * The iterator will automatically fetch the next page if there are more items -- * to fetch from the API. -- * -- * @example -- * -- * ```ts -- * const pager = await ai.files.list({config: {pageSize: 10}}); -- * for await (const file of pager) { -- * console.log(file.name); -- * } -- * ``` -- */ -- [Symbol.asyncIterator](): AsyncIterator; -- /** -- * Fetches the next page of items. This makes a new API request. -- * -- * @throws {Error} If there are no more pages to fetch. -- * -- * @example -- * -- * ```ts -- * const pager = await ai.files.list({config: {pageSize: 10}}); -- * let page = pager.page; -- * while (true) { -- * for (const file of page) { -- * console.log(file.name); -- * } -- * if (!pager.hasNextPage()) { -- * break; -- * } -- * page = await pager.nextPage(); -- * } -- * ``` -- */ -- nextPage(): Promise; -- /** -- * Returns true if there are more pages to fetch from the API. -- */ -- hasNextPage(): boolean; --} -- --/** A datatype containing media content. -- -- Exactly one field within a Part should be set, representing the specific type -- of content being conveyed. Using multiple fields within the same `Part` -- instance is considered invalid. -- */ --export declare interface Part { -- /** Metadata for a given video. */ -- videoMetadata?: VideoMetadata; -- /** Indicates if the part is thought from the model. */ -- thought?: boolean; -- /** Optional. Inlined bytes data. */ -- inlineData?: Blob_2; -- /** Optional. Result of executing the [ExecutableCode]. */ -- codeExecutionResult?: CodeExecutionResult; -- /** Optional. Code generated by the model that is meant to be executed. */ -- executableCode?: ExecutableCode; -- /** Optional. URI based data. */ -- fileData?: FileData; -- /** Optional. A predicted [FunctionCall] returned from the model that contains a string representing the [FunctionDeclaration.name] with the parameters and their values. */ -- functionCall?: FunctionCall; -- /** Optional. The result output of a [FunctionCall] that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function call. It is used as context to the model. */ -- functionResponse?: FunctionResponse; -- /** Optional. Text part (can be code). */ -- text?: string; --} -- --export declare type PartListUnion = PartUnion[] | PartUnion; -- --/** Tuning spec for Partner models. */ --export declare interface PartnerModelTuningSpec { -- /** Hyperparameters for tuning. The accepted hyper_parameters and their valid range of values will differ depending on the base model. */ -- hyperParameters?: Record; -- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ -- trainingDatasetUri?: string; -- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ -- validationDatasetUri?: string; --} -- --export declare type PartUnion = Part | string; -- --/** Enum that controls the generation of people. */ --export declare enum PersonGeneration { -- DONT_ALLOW = "DONT_ALLOW", -- ALLOW_ADULT = "ALLOW_ADULT", -- ALLOW_ALL = "ALLOW_ALL" --} -- --/** The configuration for the prebuilt speaker to use. */ --export declare interface PrebuiltVoiceConfig { -- /** The name of the prebuilt voice to use. */ -- voiceName?: string; --} -- --/** Config for proactivity features. */ --export declare interface ProactivityConfig { -- /** If enabled, the model can reject responding to the last prompt. For -- example, this allows the model to ignore out of context speech or to stay -- silent if the user did not make a request, yet. */ -- proactiveAudio?: boolean; --} -- --/** Specifies the context retrieval config. */ --export declare interface RagRetrievalConfig { -- /** Optional. Config for filters. */ -- filter?: RagRetrievalConfigFilter; -- /** Optional. Config for Hybrid Search. */ -- hybridSearch?: RagRetrievalConfigHybridSearch; -- /** Optional. Config for ranking and reranking. */ -- ranking?: RagRetrievalConfigRanking; -- /** Optional. The number of contexts to retrieve. */ -- topK?: number; --} -- --/** Config for filters. */ --export declare interface RagRetrievalConfigFilter { -- /** Optional. String for metadata filtering. */ -- metadataFilter?: string; -- /** Optional. Only returns contexts with vector distance smaller than the threshold. */ -- vectorDistanceThreshold?: number; -- /** Optional. Only returns contexts with vector similarity larger than the threshold. */ -- vectorSimilarityThreshold?: number; --} -- --/** Config for Hybrid Search. */ --export declare interface RagRetrievalConfigHybridSearch { -- /** Optional. Alpha value controls the weight between dense and sparse vector search results. The range is [0, 1], while 0 means sparse vector search only and 1 means dense vector search only. The default value is 0.5 which balances sparse and dense vector search equally. */ -- alpha?: number; --} -- --/** Config for ranking and reranking. */ --export declare interface RagRetrievalConfigRanking { -- /** Optional. Config for LlmRanker. */ -- llmRanker?: RagRetrievalConfigRankingLlmRanker; -- /** Optional. Config for Rank Service. */ -- rankService?: RagRetrievalConfigRankingRankService; --} -- --/** Config for LlmRanker. */ --export declare interface RagRetrievalConfigRankingLlmRanker { -- /** Optional. The model name used for ranking. Format: `gemini-1.5-pro` */ -- modelName?: string; --} -- --/** Config for Rank Service. */ --export declare interface RagRetrievalConfigRankingRankService { -- /** Optional. The model name of the rank service. Format: `semantic-ranker-512@latest` */ -- modelName?: string; --} -- --/** A raw reference image. -- -- A raw reference image represents the base image to edit, provided by the user. -- It can optionally be provided in addition to a mask reference image or -- a style reference image. -- */ --export declare class RawReferenceImage { -- /** The reference image for the editing operation. */ -- referenceImage?: Image_2; -- /** The id of the reference image. */ -- referenceId?: number; -- /** The type of the reference image. Only set by the SDK. */ -- referenceType?: string; -- /** Internal method to convert to ReferenceImageAPIInternal. */ -- toReferenceImageAPI(): any; --} -- --/** Marks the end of user activity. -- -- This can only be sent if automatic (i.e. server-side) activity detection is -- disabled. -- */ --export declare interface RealtimeInputConfig { -- /** If not set, automatic activity detection is enabled by default. If automatic voice detection is disabled, the client must send activity signals. */ -- automaticActivityDetection?: AutomaticActivityDetection; -- /** Defines what effect activity has. */ -- activityHandling?: ActivityHandling; -- /** Defines which input is included in the user's turn. */ -- turnCoverage?: TurnCoverage; --} -- --export declare type ReferenceImage = RawReferenceImage | MaskReferenceImage | ControlReferenceImage | StyleReferenceImage | SubjectReferenceImage; -- --/** Represents a recorded session. */ --export declare interface ReplayFile { -- replayId?: string; -- interactions?: ReplayInteraction[]; --} -- --/** Represents a single interaction, request and response in a replay. */ --export declare interface ReplayInteraction { -- request?: ReplayRequest; -- response?: ReplayResponse; --} -- --/** Represents a single request in a replay. */ --export declare interface ReplayRequest { -- method?: string; -- url?: string; -- headers?: Record; -- bodySegments?: Record[]; --} -- --/** Represents a single response in a replay. */ --export declare class ReplayResponse { -- statusCode?: number; -- headers?: Record; -- bodySegments?: Record[]; -- sdkResponseSegments?: Record[]; --} -- --/** Defines a retrieval tool that model can call to access external knowledge. */ --export declare interface Retrieval { -- /** Optional. Deprecated. This option is no longer supported. */ -- disableAttribution?: boolean; -- /** Set to use data source powered by Vertex AI Search. */ -- vertexAiSearch?: VertexAISearch; -- /** Set to use data source powered by Vertex RAG store. User data is uploaded via the VertexRagDataService. */ -- vertexRagStore?: VertexRagStore; --} -- --/** Retrieval config. -- */ --export declare interface RetrievalConfig { -- /** Optional. The location of the user. */ -- latLng?: LatLng; --} -- --/** Metadata related to retrieval in the grounding flow. */ --export declare interface RetrievalMetadata { -- /** Optional. Score indicating how likely information from Google Search could help answer the prompt. The score is in the range `[0, 1]`, where 0 is the least likely and 1 is the most likely. This score is only populated when Google Search grounding and dynamic retrieval is enabled. It will be compared to the threshold to determine whether to trigger Google Search. */ -- googleSearchDynamicRetrievalScore?: number; --} -- --/** Safety attributes of a GeneratedImage or the user-provided prompt. */ --export declare interface SafetyAttributes { -- /** List of RAI categories. -- */ -- categories?: string[]; -- /** List of scores of each categories. -- */ -- scores?: number[]; -- /** Internal use only. -- */ -- contentType?: string; --} -- --/** Enum that controls the safety filter level for objectionable content. */ --export declare enum SafetyFilterLevel { -- BLOCK_LOW_AND_ABOVE = "BLOCK_LOW_AND_ABOVE", -- BLOCK_MEDIUM_AND_ABOVE = "BLOCK_MEDIUM_AND_ABOVE", -- BLOCK_ONLY_HIGH = "BLOCK_ONLY_HIGH", -- BLOCK_NONE = "BLOCK_NONE" --} -- --/** Safety rating corresponding to the generated content. */ --export declare interface SafetyRating { -- /** Output only. Indicates whether the content was filtered out because of this rating. */ -- blocked?: boolean; -- /** Output only. Harm category. */ -- category?: HarmCategory; -- /** Output only. Harm probability levels in the content. */ -- probability?: HarmProbability; -- /** Output only. Harm probability score. */ -- probabilityScore?: number; -- /** Output only. Harm severity levels in the content. */ -- severity?: HarmSeverity; -- /** Output only. Harm severity score. */ -- severityScore?: number; --} -- --/** Safety settings. */ --export declare interface SafetySetting { -- /** Determines if the harm block method uses probability or probability -- and severity scores. */ -- method?: HarmBlockMethod; -- /** Required. Harm category. */ -- category?: HarmCategory; -- /** Required. The harm block threshold. */ -- threshold?: HarmBlockThreshold; --} -- --/** Scale of the generated music. */ --export declare enum Scale { -- /** -- * Default value. This value is unused. -- */ -- SCALE_UNSPECIFIED = "SCALE_UNSPECIFIED", -- /** -- * C major or A minor. -- */ -- C_MAJOR_A_MINOR = "C_MAJOR_A_MINOR", -- /** -- * Db major or Bb minor. -- */ -- D_FLAT_MAJOR_B_FLAT_MINOR = "D_FLAT_MAJOR_B_FLAT_MINOR", -- /** -- * D major or B minor. -- */ -- D_MAJOR_B_MINOR = "D_MAJOR_B_MINOR", -- /** -- * Eb major or C minor -- */ -- E_FLAT_MAJOR_C_MINOR = "E_FLAT_MAJOR_C_MINOR", -- /** -- * E major or Db minor. -- */ -- E_MAJOR_D_FLAT_MINOR = "E_MAJOR_D_FLAT_MINOR", -- /** -- * F major or D minor. -- */ -- F_MAJOR_D_MINOR = "F_MAJOR_D_MINOR", -- /** -- * Gb major or Eb minor. -- */ -- G_FLAT_MAJOR_E_FLAT_MINOR = "G_FLAT_MAJOR_E_FLAT_MINOR", -- /** -- * G major or E minor. -- */ -- G_MAJOR_E_MINOR = "G_MAJOR_E_MINOR", -- /** -- * Ab major or F minor. -- */ -- A_FLAT_MAJOR_F_MINOR = "A_FLAT_MAJOR_F_MINOR", -- /** -- * A major or Gb minor. -- */ -- A_MAJOR_G_FLAT_MINOR = "A_MAJOR_G_FLAT_MINOR", -- /** -- * Bb major or G minor. -- */ -- B_FLAT_MAJOR_G_MINOR = "B_FLAT_MAJOR_G_MINOR", -- /** -- * B major or Ab minor. -- */ -- B_MAJOR_A_FLAT_MINOR = "B_MAJOR_A_FLAT_MINOR" --} -- --/** Schema is used to define the format of input/output data. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema-object). More fields may be added in the future as needed. */ --export declare interface Schema { -- /** Optional. The value should be validated against any (one or more) of the subschemas in the list. */ -- anyOf?: Schema[]; -- /** Optional. Default value of the data. */ -- default?: unknown; -- /** Optional. The description of the data. */ -- description?: string; -- /** Optional. Possible values of the element of primitive type with enum format. Examples: 1. We can define direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]} 2. We can define apartment number as : {type:INTEGER, format:enum, enum:["101", "201", "301"]} */ -- enum?: string[]; -- /** Optional. Example of the object. Will only populated when the object is the root. */ -- example?: unknown; -- /** Optional. The format of the data. Supported formats: for NUMBER type: "float", "double" for INTEGER type: "int32", "int64" for STRING type: "email", "byte", etc */ -- format?: string; -- /** Optional. SCHEMA FIELDS FOR TYPE ARRAY Schema of the elements of Type.ARRAY. */ -- items?: Schema; -- /** Optional. Maximum number of the elements for Type.ARRAY. */ -- maxItems?: string; -- /** Optional. Maximum length of the Type.STRING */ -- maxLength?: string; -- /** Optional. Maximum number of the properties for Type.OBJECT. */ -- maxProperties?: string; -- /** Optional. Maximum value of the Type.INTEGER and Type.NUMBER */ -- maximum?: number; -- /** Optional. Minimum number of the elements for Type.ARRAY. */ -- minItems?: string; -- /** Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING */ -- minLength?: string; -- /** Optional. Minimum number of the properties for Type.OBJECT. */ -- minProperties?: string; -- /** Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER */ -- minimum?: number; -- /** Optional. Indicates if the value may be null. */ -- nullable?: boolean; -- /** Optional. Pattern of the Type.STRING to restrict a string to a regular expression. */ -- pattern?: string; -- /** Optional. SCHEMA FIELDS FOR TYPE OBJECT Properties of Type.OBJECT. */ -- properties?: Record; -- /** Optional. The order of the properties. Not a standard field in open api spec. Only used to support the order of the properties. */ -- propertyOrdering?: string[]; -- /** Optional. Required properties of Type.OBJECT. */ -- required?: string[]; -- /** Optional. The title of the Schema. */ -- title?: string; -- /** Optional. The type of the data. */ -- type?: Type; --} -- --export declare type SchemaUnion = Schema | unknown; -- --/** Google search entry point. */ --export declare interface SearchEntryPoint { -- /** Optional. Web content snippet that can be embedded in a web page or an app webview. */ -- renderedContent?: string; -- /** Optional. Base64 encoded JSON representing array of tuple. */ -- sdkBlob?: string; --} -- --/** Segment of the content. */ --export declare interface Segment { -- /** Output only. End index in the given Part, measured in bytes. Offset from the start of the Part, exclusive, starting at zero. */ -- endIndex?: number; -- /** Output only. The index of a Part object within its parent Content object. */ -- partIndex?: number; -- /** Output only. Start index in the given Part, measured in bytes. Offset from the start of the Part, inclusive, starting at zero. */ -- startIndex?: number; -- /** Output only. The text corresponding to the segment from the response. */ -- text?: string; --} -- --/** Parameters for sending a message within a chat session. -- -- These parameters are used with the `chat.sendMessage()` method. -- */ --export declare interface SendMessageParameters { -- /** The message to send to the model. -- -- The SDK will combine all parts into a single 'user' content to send to -- the model. -- */ -- message: PartListUnion; -- /** Config for this specific request. -- -- Please note that the per-request config does not change the chat level -- config, nor inherit from it. If you intend to use some values from the -- chat's default config, you must explicitly copy them into this per-request -- config. -- */ -- config?: GenerateContentConfig; --} -- --/** -- Represents a connection to the API. -- -- @experimental -- */ --export declare class Session { -- readonly conn: WebSocket_2; -- private readonly apiClient; -- constructor(conn: WebSocket_2, apiClient: ApiClient); -- private tLiveClientContent; -- private tLiveClienttToolResponse; -- /** -- Send a message over the established connection. -- -- @param params - Contains two **optional** properties, `turns` and -- `turnComplete`. -- -- - `turns` will be converted to a `Content[]` -- - `turnComplete: true` [default] indicates that you are done sending -- content and expect a response. If `turnComplete: false`, the server -- will wait for additional messages before starting generation. -- -- @experimental -- -- @remarks -- There are two ways to send messages to the live API: -- `sendClientContent` and `sendRealtimeInput`. -- -- `sendClientContent` messages are added to the model context **in order**. -- Having a conversation using `sendClientContent` messages is roughly -- equivalent to using the `Chat.sendMessageStream`, except that the state of -- the `chat` history is stored on the API server instead of locally. -- -- Because of `sendClientContent`'s order guarantee, the model cannot respons -- as quickly to `sendClientContent` messages as to `sendRealtimeInput` -- messages. This makes the biggest difference when sending objects that have -- significant preprocessing time (typically images). -- -- The `sendClientContent` message sends a `Content[]` -- which has more options than the `Blob` sent by `sendRealtimeInput`. -- -- So the main use-cases for `sendClientContent` over `sendRealtimeInput` are: -- -- - Sending anything that can't be represented as a `Blob` (text, -- `sendClientContent({turns="Hello?"}`)). -- - Managing turns when not using audio input and voice activity detection. -- (`sendClientContent({turnComplete:true})` or the short form -- `sendClientContent()`) -- - Prefilling a conversation context -- ``` -- sendClientContent({ -- turns: [ -- Content({role:user, parts:...}), -- Content({role:user, parts:...}), -- ... -- ] -- }) -- ``` -- @experimental -- */ -- sendClientContent(params: types.LiveSendClientContentParameters): void; -- /** -- Send a realtime message over the established connection. -- -- @param params - Contains one property, `media`. -- -- - `media` will be converted to a `Blob` -- -- @experimental -- -- @remarks -- Use `sendRealtimeInput` for realtime audio chunks and video frames (images). -- -- With `sendRealtimeInput` the api will respond to audio automatically -- based on voice activity detection (VAD). -- -- `sendRealtimeInput` is optimized for responsivness at the expense of -- deterministic ordering guarantees. Audio and video tokens are to the -- context when they become available. -- -- Note: The Call signature expects a `Blob` object, but only a subset -- of audio and image mimetypes are allowed. -- */ -- sendRealtimeInput(params: types.LiveSendRealtimeInputParameters): void; -- /** -- Send a function response message over the established connection. -- -- @param params - Contains property `functionResponses`. -- -- - `functionResponses` will be converted to a `functionResponses[]` -- -- @remarks -- Use `sendFunctionResponse` to reply to `LiveServerToolCall` from the server. -- -- Use {@link types.LiveConnectConfig#tools} to configure the callable functions. -- -- @experimental -- */ -- sendToolResponse(params: types.LiveSendToolResponseParameters): void; -- /** -- Terminates the WebSocket connection. -- -- @experimental -- -- @example -- ```ts -- let model: string; -- if (GOOGLE_GENAI_USE_VERTEXAI) { -- model = 'gemini-2.0-flash-live-preview-04-09'; -- } else { -- model = 'gemini-2.0-flash-live-001'; -- } -- const session = await ai.live.connect({ -- model: model, -- config: { -- responseModalities: [Modality.AUDIO], -- } -- }); -- -- session.close(); -- ``` -- */ -- close(): void; --} -- --/** Configuration of session resumption mechanism. -- -- Included in `LiveConnectConfig.session_resumption`. If included server -- will send `LiveServerSessionResumptionUpdate` messages. -- */ --export declare interface SessionResumptionConfig { -- /** Session resumption handle of previous session (session to restore). -- -- If not present new session will be started. */ -- handle?: string; -- /** If set the server will send `last_consumed_client_message_index` in the `session_resumption_update` messages to allow for transparent reconnections. */ -- transparent?: boolean; --} -- --/** -- * Overrides the base URLs for the Gemini API and Vertex AI API. -- * -- * @remarks This function should be called before initializing the SDK. If the -- * base URLs are set after initializing the SDK, the base URLs will not be -- * updated. Base URLs provided in the HttpOptions will also take precedence over -- * URLs set here. -- * -- * @example -- * ```ts -- * import {GoogleGenAI, setDefaultBaseUrls} from '@google/genai'; -- * // Override the base URL for the Gemini API. -- * setDefaultBaseUrls({geminiUrl:'https://gemini.google.com'}); -- * -- * // Override the base URL for the Vertex AI API. -- * setDefaultBaseUrls({vertexUrl: 'https://vertexai.googleapis.com'}); -- * -- * const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'}); -- * ``` -- */ --export declare function setDefaultBaseUrls(baseUrlParams: BaseUrlParameters): void; -- --/** Context window will be truncated by keeping only suffix of it. -- -- Context window will always be cut at start of USER role turn. System -- instructions and `BidiGenerateContentSetup.prefix_turns` will not be -- subject to the sliding window mechanism, they will always stay at the -- beginning of context window. -- */ --export declare interface SlidingWindow { -- /** Session reduction target -- how many tokens we should keep. Window shortening operation has some latency costs, so we should avoid running it on every turn. Should be < trigger_tokens. If not set, trigger_tokens/2 is assumed. */ -- targetTokens?: string; --} -- --/** The configuration for the speaker to use. */ --export declare interface SpeakerVoiceConfig { -- /** The name of the speaker to use. Should be the same as in the -- prompt. */ -- speaker?: string; -- /** The configuration for the voice to use. */ -- voiceConfig?: VoiceConfig; --} -- --/** The speech generation configuration. */ --export declare interface SpeechConfig { -- /** The configuration for the speaker to use. -- */ -- voiceConfig?: VoiceConfig; -- /** The configuration for the multi-speaker setup. -- It is mutually exclusive with the voice_config field. -- */ -- multiSpeakerVoiceConfig?: MultiSpeakerVoiceConfig; -- /** Language code (ISO 639. e.g. en-US) for the speech synthesization. -- Only available for Live API. -- */ -- languageCode?: string; --} -- --export declare type SpeechConfigUnion = SpeechConfig | string; -- --/** Start of speech sensitivity. */ --export declare enum StartSensitivity { -- /** -- * The default is START_SENSITIVITY_LOW. -- */ -- START_SENSITIVITY_UNSPECIFIED = "START_SENSITIVITY_UNSPECIFIED", -- /** -- * Automatic detection will detect the start of speech more often. -- */ -- START_SENSITIVITY_HIGH = "START_SENSITIVITY_HIGH", -- /** -- * Automatic detection will detect the start of speech less often. -- */ -- START_SENSITIVITY_LOW = "START_SENSITIVITY_LOW" --} -- --/** Configuration for a Style reference image. */ --export declare interface StyleReferenceConfig { -- /** A text description of the style to use for the generated image. */ -- styleDescription?: string; --} -- --/** A style reference image. -- -- This encapsulates a style reference image provided by the user, and -- additionally optional config parameters for the style reference image. -- -- A raw reference image can also be provided as a destination for the style to -- be applied to. -- */ --export declare class StyleReferenceImage { -- /** The reference image for the editing operation. */ -- referenceImage?: Image_2; -- /** The id of the reference image. */ -- referenceId?: number; -- /** The type of the reference image. Only set by the SDK. */ -- referenceType?: string; -- /** Configuration for the style reference image. */ -- config?: StyleReferenceConfig; -- /** Internal method to convert to ReferenceImageAPIInternal. */ -- toReferenceImageAPI(): any; --} -- --/** Configuration for a Subject reference image. */ --export declare interface SubjectReferenceConfig { -- /** The subject type of a subject reference image. */ -- subjectType?: SubjectReferenceType; -- /** Subject description for the image. */ -- subjectDescription?: string; --} -- --/** A subject reference image. -- -- This encapsulates a subject reference image provided by the user, and -- additionally optional config parameters for the subject reference image. -- -- A raw reference image can also be provided as a destination for the subject to -- be applied to. -- */ --export declare class SubjectReferenceImage { -- /** The reference image for the editing operation. */ -- referenceImage?: Image_2; -- /** The id of the reference image. */ -- referenceId?: number; -- /** The type of the reference image. Only set by the SDK. */ -- referenceType?: string; -- /** Configuration for the subject reference image. */ -- config?: SubjectReferenceConfig; -- toReferenceImageAPI(): any; --} -- --/** Enum representing the subject type of a subject reference image. */ --export declare enum SubjectReferenceType { -- SUBJECT_TYPE_DEFAULT = "SUBJECT_TYPE_DEFAULT", -- SUBJECT_TYPE_PERSON = "SUBJECT_TYPE_PERSON", -- SUBJECT_TYPE_ANIMAL = "SUBJECT_TYPE_ANIMAL", -- SUBJECT_TYPE_PRODUCT = "SUBJECT_TYPE_PRODUCT" --} -- --/** Hyperparameters for SFT. */ --export declare interface SupervisedHyperParameters { -- /** Optional. Adapter size for tuning. */ -- adapterSize?: AdapterSize; -- /** Optional. Number of complete passes the model makes over the entire training dataset during training. */ -- epochCount?: string; -- /** Optional. Multiplier for adjusting the default learning rate. */ -- learningRateMultiplier?: number; --} -- --/** Dataset distribution for Supervised Tuning. */ --export declare interface SupervisedTuningDatasetDistribution { -- /** Output only. Sum of a given population of values that are billable. */ -- billableSum?: string; -- /** Output only. Defines the histogram bucket. */ -- buckets?: SupervisedTuningDatasetDistributionDatasetBucket[]; -- /** Output only. The maximum of the population values. */ -- max?: number; -- /** Output only. The arithmetic mean of the values in the population. */ -- mean?: number; -- /** Output only. The median of the values in the population. */ -- median?: number; -- /** Output only. The minimum of the population values. */ -- min?: number; -- /** Output only. The 5th percentile of the values in the population. */ -- p5?: number; -- /** Output only. The 95th percentile of the values in the population. */ -- p95?: number; -- /** Output only. Sum of a given population of values. */ -- sum?: string; --} -- --/** Dataset bucket used to create a histogram for the distribution given a population of values. */ --export declare interface SupervisedTuningDatasetDistributionDatasetBucket { -- /** Output only. Number of values in the bucket. */ -- count?: number; -- /** Output only. Left bound of the bucket. */ -- left?: number; -- /** Output only. Right bound of the bucket. */ -- right?: number; --} -- --/** Tuning data statistics for Supervised Tuning. */ --export declare interface SupervisedTuningDataStats { -- /** Output only. Number of billable characters in the tuning dataset. */ -- totalBillableCharacterCount?: string; -- /** Output only. Number of billable tokens in the tuning dataset. */ -- totalBillableTokenCount?: string; -- /** The number of examples in the dataset that have been truncated by any amount. */ -- totalTruncatedExampleCount?: string; -- /** Output only. Number of tuning characters in the tuning dataset. */ -- totalTuningCharacterCount?: string; -- /** A partial sample of the indices (starting from 1) of the truncated examples. */ -- truncatedExampleIndices?: string[]; -- /** Output only. Number of examples in the tuning dataset. */ -- tuningDatasetExampleCount?: string; -- /** Output only. Number of tuning steps for this Tuning Job. */ -- tuningStepCount?: string; -- /** Output only. Sample user messages in the training dataset uri. */ -- userDatasetExamples?: Content[]; -- /** Output only. Dataset distributions for the user input tokens. */ -- userInputTokenDistribution?: SupervisedTuningDatasetDistribution; -- /** Output only. Dataset distributions for the messages per example. */ -- userMessagePerExampleDistribution?: SupervisedTuningDatasetDistribution; -- /** Output only. Dataset distributions for the user output tokens. */ -- userOutputTokenDistribution?: SupervisedTuningDatasetDistribution; --} -- --/** Tuning Spec for Supervised Tuning for first party models. */ --export declare interface SupervisedTuningSpec { -- /** Optional. Hyperparameters for SFT. */ -- hyperParameters?: SupervisedHyperParameters; -- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ -- trainingDatasetUri?: string; -- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ -- validationDatasetUri?: string; -- /** Optional. If set to true, disable intermediate checkpoints for SFT and only the last checkpoint will be exported. */ -- exportLastCheckpointOnly?: boolean; --} -- --export declare interface TestTableFile { -- comment?: string; -- testMethod?: string; -- parameterNames?: string[]; -- testTable?: TestTableItem[]; --} -- --export declare interface TestTableItem { -- /** The name of the test. This is used to derive the replay id. */ -- name?: string; -- /** The parameters to the test. Use pydantic models. */ -- parameters?: Record; -- /** Expects an exception for MLDev matching the string. */ -- exceptionIfMldev?: string; -- /** Expects an exception for Vertex matching the string. */ -- exceptionIfVertex?: string; -- /** Use if you don't want to use the default replay id which is derived from the test name. */ -- overrideReplayId?: string; -- /** True if the parameters contain an unsupported union type. This test will be skipped for languages that do not support the union type. */ -- hasUnion?: boolean; -- /** When set to a reason string, this test will be skipped in the API mode. Use this flag for tests that can not be reproduced with the real API. E.g. a test that deletes a resource. */ -- skipInApiMode?: string; -- /** Keys to ignore when comparing the request and response. This is useful for tests that are not deterministic. */ -- ignoreKeys?: string[]; --} -- --/** The thinking features configuration. */ --export declare interface ThinkingConfig { -- /** Indicates whether to include thoughts in the response. If true, thoughts are returned only if the model supports thought and thoughts are available. -- */ -- includeThoughts?: boolean; -- /** Indicates the thinking budget in tokens. -- */ -- thinkingBudget?: number; --} -- --/** Tokens info with a list of tokens and the corresponding list of token ids. */ --export declare interface TokensInfo { -- /** Optional. Optional fields for the role from the corresponding Content. */ -- role?: string; -- /** A list of token ids from the input. */ -- tokenIds?: string[]; -- /** A list of tokens from the input. */ -- tokens?: string[]; --} -- --/** Tool details of a tool that the model may use to generate a response. */ --export declare interface Tool { -- /** List of function declarations that the tool supports. */ -- functionDeclarations?: FunctionDeclaration[]; -- /** Optional. Retrieval tool type. System will always execute the provided retrieval tool(s) to get external knowledge to answer the prompt. Retrieval results are presented to the model for generation. */ -- retrieval?: Retrieval; -- /** Optional. Google Search tool type. Specialized retrieval tool -- that is powered by Google Search. */ -- googleSearch?: GoogleSearch; -- /** Optional. GoogleSearchRetrieval tool type. Specialized retrieval tool that is powered by Google search. */ -- googleSearchRetrieval?: GoogleSearchRetrieval; -- /** Optional. Enterprise web search tool type. Specialized retrieval -- tool that is powered by Vertex AI Search and Sec4 compliance. */ -- enterpriseWebSearch?: EnterpriseWebSearch; -- /** Optional. Google Maps tool type. Specialized retrieval tool -- that is powered by Google Maps. */ -- googleMaps?: GoogleMaps; -- /** Optional. Tool to support URL context retrieval. */ -- urlContext?: UrlContext; -- /** Optional. CodeExecution tool type. Enables the model to execute code as part of generation. This field is only used by the Gemini Developer API services. */ -- codeExecution?: ToolCodeExecution; --} -- --/** Tool that executes code generated by the model, and automatically returns the result to the model. See also [ExecutableCode]and [CodeExecutionResult] which are input and output to this tool. */ --export declare interface ToolCodeExecution { --} -- --/** Tool config. -- -- This config is shared for all tools provided in the request. -- */ --export declare interface ToolConfig { -- /** Optional. Function calling config. */ -- functionCallingConfig?: FunctionCallingConfig; -- /** Optional. Retrieval config. */ -- retrievalConfig?: RetrievalConfig; --} -- --export declare type ToolListUnion = ToolUnion[]; -- --export declare type ToolUnion = Tool | CallableTool; -- --/** Output only. Traffic type. This shows whether a request consumes Pay-As-You-Go or Provisioned Throughput quota. */ --export declare enum TrafficType { -- /** -- * Unspecified request traffic type. -- */ -- TRAFFIC_TYPE_UNSPECIFIED = "TRAFFIC_TYPE_UNSPECIFIED", -- /** -- * Type for Pay-As-You-Go traffic. -- */ -- ON_DEMAND = "ON_DEMAND", -- /** -- * Type for Provisioned Throughput traffic. -- */ -- PROVISIONED_THROUGHPUT = "PROVISIONED_THROUGHPUT" --} -- --/** Audio transcription in Server Conent. */ --export declare interface Transcription { -- /** Transcription text. -- */ -- text?: string; -- /** The bool indicates the end of the transcription. -- */ -- finished?: boolean; --} -- --export declare interface TunedModel { -- /** Output only. The resource name of the TunedModel. Format: `projects/{project}/locations/{location}/models/{model}`. */ -- model?: string; -- /** Output only. A resource name of an Endpoint. Format: `projects/{project}/locations/{location}/endpoints/{endpoint}`. */ -- endpoint?: string; -- /** The checkpoints associated with this TunedModel. -- This field is only populated for tuning jobs that enable intermediate -- checkpoints. */ -- checkpoints?: TunedModelCheckpoint[]; --} -- --/** TunedModelCheckpoint for the Tuned Model of a Tuning Job. */ --export declare interface TunedModelCheckpoint { -- /** The ID of the checkpoint. -- */ -- checkpointId?: string; -- /** The epoch of the checkpoint. -- */ -- epoch?: string; -- /** The step of the checkpoint. -- */ -- step?: string; -- /** The Endpoint resource name that the checkpoint is deployed to. -- Format: `projects/{project}/locations/{location}/endpoints/{endpoint}`. -- */ -- endpoint?: string; --} -- --/** A tuned machine learning model. */ --export declare interface TunedModelInfo { -- /** ID of the base model that you want to tune. */ -- baseModel?: string; -- /** Date and time when the base model was created. */ -- createTime?: string; -- /** Date and time when the base model was last updated. */ -- updateTime?: string; --} -- --/** Supervised fine-tuning training dataset. */ --export declare interface TuningDataset { -- /** GCS URI of the file containing training dataset in JSONL format. */ -- gcsUri?: string; -- /** Inline examples with simple input/output text. */ -- examples?: TuningExample[]; --} -- --/** The tuning data statistic values for TuningJob. */ --export declare interface TuningDataStats { -- /** Output only. Statistics for distillation. */ -- distillationDataStats?: DistillationDataStats; -- /** The SFT Tuning data stats. */ -- supervisedTuningDataStats?: SupervisedTuningDataStats; --} -- --export declare interface TuningExample { -- /** Text model input. */ -- textInput?: string; -- /** The expected model output. */ -- output?: string; --} -- --/** A tuning job. */ --export declare interface TuningJob { -- /** Output only. Identifier. Resource name of a TuningJob. Format: `projects/{project}/locations/{location}/tuningJobs/{tuning_job}` */ -- name?: string; -- /** Output only. The detailed state of the job. */ -- state?: JobState; -- /** Output only. Time when the TuningJob was created. */ -- createTime?: string; -- /** Output only. Time when the TuningJob for the first time entered the `JOB_STATE_RUNNING` state. */ -- startTime?: string; -- /** Output only. Time when the TuningJob entered any of the following JobStates: `JOB_STATE_SUCCEEDED`, `JOB_STATE_FAILED`, `JOB_STATE_CANCELLED`, `JOB_STATE_EXPIRED`. */ -- endTime?: string; -- /** Output only. Time when the TuningJob was most recently updated. */ -- updateTime?: string; -- /** Output only. Only populated when job's state is `JOB_STATE_FAILED` or `JOB_STATE_CANCELLED`. */ -- error?: GoogleRpcStatus; -- /** Optional. The description of the TuningJob. */ -- description?: string; -- /** The base model that is being tuned, e.g., "gemini-1.0-pro-002". . */ -- baseModel?: string; -- /** Output only. The tuned model resources associated with this TuningJob. */ -- tunedModel?: TunedModel; -- /** Tuning Spec for Supervised Fine Tuning. */ -- supervisedTuningSpec?: SupervisedTuningSpec; -- /** Output only. The tuning data statistics associated with this TuningJob. */ -- tuningDataStats?: TuningDataStats; -- /** Customer-managed encryption key options for a TuningJob. If this is set, then all resources created by the TuningJob will be encrypted with the provided encryption key. */ -- encryptionSpec?: EncryptionSpec; -- /** Tuning Spec for open sourced and third party Partner models. */ -- partnerModelTuningSpec?: PartnerModelTuningSpec; -- /** Tuning Spec for Distillation. */ -- distillationSpec?: DistillationSpec; -- /** Output only. The Experiment associated with this TuningJob. */ -- experiment?: string; -- /** Optional. The labels with user-defined metadata to organize TuningJob and generated resources such as Model and Endpoint. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels. */ -- labels?: Record; -- /** Output only. The resource name of the PipelineJob associated with the TuningJob. Format: `projects/{project}/locations/{location}/pipelineJobs/{pipeline_job}`. */ -- pipelineJob?: string; -- /** Optional. The display name of the TunedModel. The name can be up to 128 characters long and can consist of any UTF-8 characters. */ -- tunedModelDisplayName?: string; --} -- --declare class Tunings extends BaseModule { -- private readonly apiClient; -- constructor(apiClient: ApiClient); -- /** -- * Gets a TuningJob. -- * -- * @param name - The resource name of the tuning job. -- * @return - A TuningJob object. -- * -- * @experimental - The SDK's tuning implementation is experimental, and may -- * change in future versions. -- */ -- get: (params: types.GetTuningJobParameters) => Promise; -- /** -- * Lists tuning jobs. -- * -- * @param config - The configuration for the list request. -- * @return - A list of tuning jobs. -- * -- * @experimental - The SDK's tuning implementation is experimental, and may -- * change in future versions. -- */ -- list: (params?: types.ListTuningJobsParameters) => Promise>; -- /** -- * Creates a supervised fine-tuning job. -- * -- * @param params - The parameters for the tuning job. -- * @return - A TuningJob operation. -- * -- * @experimental - The SDK's tuning implementation is experimental, and may -- * change in future versions. -- */ -- tune: (params: types.CreateTuningJobParameters) => Promise; -- private getInternal; -- private listInternal; -- private tuneInternal; -- private tuneMldevInternal; --} -- --export declare interface TuningValidationDataset { -- /** GCS URI of the file containing validation dataset in JSONL format. */ -- gcsUri?: string; --} -- --/** Options about which input is included in the user's turn. */ --export declare enum TurnCoverage { -- /** -- * If unspecified, the default behavior is `TURN_INCLUDES_ONLY_ACTIVITY`. -- */ -- TURN_COVERAGE_UNSPECIFIED = "TURN_COVERAGE_UNSPECIFIED", -- /** -- * The users turn only includes activity since the last turn, excluding inactivity (e.g. silence on the audio stream). This is the default behavior. -- */ -- TURN_INCLUDES_ONLY_ACTIVITY = "TURN_INCLUDES_ONLY_ACTIVITY", -- /** -- * The users turn includes all realtime input since the last turn, including inactivity (e.g. silence on the audio stream). -- */ -- TURN_INCLUDES_ALL_INPUT = "TURN_INCLUDES_ALL_INPUT" --} -- --/** Optional. The type of the data. */ --export declare enum Type { -- /** -- * Not specified, should not be used. -- */ -- TYPE_UNSPECIFIED = "TYPE_UNSPECIFIED", -- /** -- * OpenAPI string type -- */ -- STRING = "STRING", -- /** -- * OpenAPI number type -- */ -- NUMBER = "NUMBER", -- /** -- * OpenAPI integer type -- */ -- INTEGER = "INTEGER", -- /** -- * OpenAPI boolean type -- */ -- BOOLEAN = "BOOLEAN", -- /** -- * OpenAPI array type -- */ -- ARRAY = "ARRAY", -- /** -- * OpenAPI object type -- */ -- OBJECT = "OBJECT" --} -- --declare namespace types { -- export { -- createPartFromUri, -- createPartFromText, -- createPartFromFunctionCall, -- createPartFromFunctionResponse, -- createPartFromBase64, -- createPartFromCodeExecutionResult, -- createPartFromExecutableCode, -- createUserContent, -- createModelContent, -- Outcome, -- Language, -- HarmCategory, -- HarmBlockMethod, -- HarmBlockThreshold, -- Type, -- Mode, -- AuthType, -- FinishReason, -- HarmProbability, -- HarmSeverity, -- BlockedReason, -- TrafficType, -- Modality, -- MediaResolution, -- JobState, -- AdapterSize, -- FeatureSelectionPreference, -- Behavior, -- DynamicRetrievalConfigMode, -- FunctionCallingConfigMode, -- UrlRetrievalStatus, -- SafetyFilterLevel, -- PersonGeneration, -- ImagePromptLanguage, -- MaskReferenceMode, -- ControlReferenceType, -- SubjectReferenceType, -- EditMode, -- FileState, -- FileSource, -- MediaModality, -- StartSensitivity, -- EndSensitivity, -- ActivityHandling, -- TurnCoverage, -- FunctionResponseScheduling, -- Scale, -- MusicGenerationMode, -- LiveMusicPlaybackControl, -- VideoMetadata, -- Blob_2 as Blob, -- CodeExecutionResult, -- ExecutableCode, -- FileData, -- FunctionCall, -- FunctionResponse, -- Part, -- Content, -- HttpOptions, -- ModelSelectionConfig, -- SafetySetting, -- Schema, -- FunctionDeclaration, -- Interval, -- GoogleSearch, -- DynamicRetrievalConfig, -- GoogleSearchRetrieval, -- EnterpriseWebSearch, -- ApiKeyConfig, -- AuthConfigGoogleServiceAccountConfig, -- AuthConfigHttpBasicAuthConfig, -- AuthConfigOauthConfig, -- AuthConfigOidcConfig, -- AuthConfig, -- GoogleMaps, -- UrlContext, -- VertexAISearch, -- VertexRagStoreRagResource, -- RagRetrievalConfigFilter, -- RagRetrievalConfigHybridSearch, -- RagRetrievalConfigRankingLlmRanker, -- RagRetrievalConfigRankingRankService, -- RagRetrievalConfigRanking, -- RagRetrievalConfig, -- VertexRagStore, -- Retrieval, -- ToolCodeExecution, -- Tool, -- FunctionCallingConfig, -- LatLng, -- RetrievalConfig, -- ToolConfig, -- PrebuiltVoiceConfig, -- VoiceConfig, -- SpeakerVoiceConfig, -- MultiSpeakerVoiceConfig, -- SpeechConfig, -- AutomaticFunctionCallingConfig, -- ThinkingConfig, -- GenerationConfigRoutingConfigAutoRoutingMode, -- GenerationConfigRoutingConfigManualRoutingMode, -- GenerationConfigRoutingConfig, -- GenerateContentConfig, -- GenerateContentParameters, -- GoogleTypeDate, -- Citation, -- CitationMetadata, -- UrlMetadata, -- UrlContextMetadata, -- GroundingChunkRetrievedContext, -- GroundingChunkWeb, -- GroundingChunk, -- Segment, -- GroundingSupport, -- RetrievalMetadata, -- SearchEntryPoint, -- GroundingMetadata, -- LogprobsResultCandidate, -- LogprobsResultTopCandidates, -- LogprobsResult, -- SafetyRating, -- Candidate, -- GenerateContentResponsePromptFeedback, -- ModalityTokenCount, -- GenerateContentResponseUsageMetadata, -- GenerateContentResponse, -- ReferenceImage, -- EditImageParameters, -- EmbedContentConfig, -- EmbedContentParameters, -- ContentEmbeddingStatistics, -- ContentEmbedding, -- EmbedContentMetadata, -- EmbedContentResponse, -- GenerateImagesConfig, -- GenerateImagesParameters, -- Image_2 as Image, -- SafetyAttributes, -- GeneratedImage, -- GenerateImagesResponse, -- MaskReferenceConfig, -- ControlReferenceConfig, -- StyleReferenceConfig, -- SubjectReferenceConfig, -- EditImageConfig, -- EditImageResponse, -- UpscaleImageResponse, -- GetModelConfig, -- GetModelParameters, -- Endpoint, -- TunedModelInfo, -- Checkpoint, -- Model, -- ListModelsConfig, -- ListModelsParameters, -- ListModelsResponse, -- UpdateModelConfig, -- UpdateModelParameters, -- DeleteModelConfig, -- DeleteModelParameters, -- DeleteModelResponse, -- GenerationConfig, -- CountTokensConfig, -- CountTokensParameters, -- CountTokensResponse, -- ComputeTokensConfig, -- ComputeTokensParameters, -- TokensInfo, -- ComputeTokensResponse, -- GenerateVideosConfig, -- GenerateVideosParameters, -- Video, -- GeneratedVideo, -- GenerateVideosResponse, -- GenerateVideosOperation, -- GetTuningJobConfig, -- GetTuningJobParameters, -- TunedModelCheckpoint, -- TunedModel, -- GoogleRpcStatus, -- SupervisedHyperParameters, -- SupervisedTuningSpec, -- DatasetDistributionDistributionBucket, -- DatasetDistribution, -- DatasetStats, -- DistillationDataStats, -- SupervisedTuningDatasetDistributionDatasetBucket, -- SupervisedTuningDatasetDistribution, -- SupervisedTuningDataStats, -- TuningDataStats, -- EncryptionSpec, -- PartnerModelTuningSpec, -- DistillationHyperParameters, -- DistillationSpec, -- TuningJob, -- ListTuningJobsConfig, -- ListTuningJobsParameters, -- ListTuningJobsResponse, -- TuningExample, -- TuningDataset, -- TuningValidationDataset, -- CreateTuningJobConfig, -- CreateTuningJobParameters, -- Operation, -- CreateCachedContentConfig, -- CreateCachedContentParameters, -- CachedContentUsageMetadata, -- CachedContent, -- GetCachedContentConfig, -- GetCachedContentParameters, -- DeleteCachedContentConfig, -- DeleteCachedContentParameters, -- DeleteCachedContentResponse, -- UpdateCachedContentConfig, -- UpdateCachedContentParameters, -- ListCachedContentsConfig, -- ListCachedContentsParameters, -- ListCachedContentsResponse, -- ListFilesConfig, -- ListFilesParameters, -- FileStatus, -- File_2 as File, -- ListFilesResponse, -- CreateFileConfig, -- CreateFileParameters, -- HttpResponse, -- LiveCallbacks, -- CreateFileResponse, -- GetFileConfig, -- GetFileParameters, -- DeleteFileConfig, -- DeleteFileParameters, -- DeleteFileResponse, -- GetOperationConfig, -- GetOperationParameters, -- FetchPredictOperationConfig, -- FetchPredictOperationParameters, -- TestTableItem, -- TestTableFile, -- ReplayRequest, -- ReplayResponse, -- ReplayInteraction, -- ReplayFile, -- UploadFileConfig, -- DownloadFileConfig, -- DownloadFileParameters, -- UpscaleImageConfig, -- UpscaleImageParameters, -- RawReferenceImage, -- MaskReferenceImage, -- ControlReferenceImage, -- StyleReferenceImage, -- SubjectReferenceImage, -- LiveServerSetupComplete, -- Transcription, -- LiveServerContent, -- LiveServerToolCall, -- LiveServerToolCallCancellation, -- UsageMetadata, -- LiveServerGoAway, -- LiveServerSessionResumptionUpdate, -- LiveServerMessage, -- AutomaticActivityDetection, -- RealtimeInputConfig, -- SessionResumptionConfig, -- SlidingWindow, -- ContextWindowCompressionConfig, -- AudioTranscriptionConfig, -- ProactivityConfig, -- LiveClientSetup, -- LiveClientContent, -- ActivityStart, -- ActivityEnd, -- LiveClientRealtimeInput, -- LiveSendRealtimeInputParameters, -- LiveClientToolResponse, -- LiveClientMessage, -- LiveConnectConfig, -- LiveConnectParameters, -- CreateChatParameters, -- SendMessageParameters, -- LiveSendClientContentParameters, -- LiveSendToolResponseParameters, -- LiveMusicClientSetup, -- WeightedPrompt, -- LiveMusicClientContent, -- LiveMusicGenerationConfig, -- LiveMusicClientMessage, -- LiveMusicServerSetupComplete, -- LiveMusicSourceMetadata, -- AudioChunk, -- LiveMusicServerContent, -- LiveMusicFilteredPrompt, -- LiveMusicServerMessage, -- LiveMusicCallbacks, -- UploadFileParameters, -- CallableTool, -- CallableToolConfig, -- LiveMusicConnectParameters, -- LiveMusicSetConfigParameters, -- LiveMusicSetWeightedPromptsParameters, -- LiveEphemeralParameters, -- CreateAuthTokenConfig, -- OperationGetParameters, -- BlobImageUnion, -- PartUnion, -- PartListUnion, -- ContentUnion, -- ContentListUnion, -- SchemaUnion, -- SpeechConfigUnion, -- ToolUnion, -- ToolListUnion, -- DownloadableFileUnion -- } --} -- --/** Optional parameters for caches.update method. */ --export declare interface UpdateCachedContentConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** The TTL for this resource. The expiration time is computed: now + TTL. It is a duration string, with up to nine fractional digits, terminated by 's'. Example: "3.5s". */ -- ttl?: string; -- /** Timestamp of when this resource is considered expired. Uses RFC 3339 format, Example: 2014-10-02T15:01:23Z. */ -- expireTime?: string; --} -- --export declare interface UpdateCachedContentParameters { -- /** The server-generated resource name of the cached content. -- */ -- name: string; -- /** Configuration that contains optional parameters. -- */ -- config?: UpdateCachedContentConfig; --} -- --/** Configuration for updating a tuned model. */ --export declare interface UpdateModelConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- displayName?: string; -- description?: string; -- defaultCheckpointId?: string; --} -- --/** Configuration for updating a tuned model. */ --export declare interface UpdateModelParameters { -- model: string; -- config?: UpdateModelConfig; --} -- --declare interface Uploader { -- /** -- * Uploads a file to the given upload url. -- * -- * @param file The file to upload. file is in string type or a Blob. -- * @param uploadUrl The upload URL as a string is where the file will be -- * uploaded to. The uploadUrl must be a url that was returned by the -- * https://generativelanguage.googleapis.com/upload/v1beta/files endpoint -- * @param apiClient The ApiClient to use for uploading. -- * @return A Promise that resolves to types.File. -- */ -- upload(file: string | Blob, uploadUrl: string, apiClient: ApiClient): Promise; -- /** -- * Returns the file's mimeType and the size of a given file. If the file is a -- * string path, the file type is determined by the file extension. If the -- * file's type cannot be determined, the type will be set to undefined. -- * -- * @param file The file to get the stat for. Can be a string path or a Blob. -- * @return A Promise that resolves to the file stat of the given file. -- */ -- stat(file: string | Blob): Promise; --} -- --/** Used to override the default configuration. */ --export declare interface UploadFileConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** The name of the file in the destination (e.g., 'files/sample-image'. If not provided one will be generated. */ -- name?: string; -- /** mime_type: The MIME type of the file. If not provided, it will be inferred from the file extension. */ -- mimeType?: string; -- /** Optional display name of the file. */ -- displayName?: string; --} -- --/** Parameters for the upload file method. */ --export declare interface UploadFileParameters { -- /** The string path to the file to be uploaded or a Blob object. */ -- file: string | globalThis.Blob; -- /** Configuration that contains optional parameters. */ -- config?: UploadFileConfig; --} -- --/** Configuration for upscaling an image. -- -- For more information on this configuration, refer to -- the `Imagen API reference documentation -- `_. -- */ --export declare interface UpscaleImageConfig { -- /** Used to override HTTP request options. */ -- httpOptions?: HttpOptions; -- /** Abort signal which can be used to cancel the request. -- -- NOTE: AbortSignal is a client-only operation. Using it to cancel an -- operation will not cancel the request in the service. You will still -- be charged usage for any applicable operations. -- */ -- abortSignal?: AbortSignal; -- /** Whether to include a reason for filtered-out images in the -- response. */ -- includeRaiReason?: boolean; -- /** The image format that the output should be saved as. */ -- outputMimeType?: string; -- /** The level of compression if the ``output_mime_type`` is -- ``image/jpeg``. */ -- outputCompressionQuality?: number; --} -- --/** User-facing config UpscaleImageParameters. */ --export declare interface UpscaleImageParameters { -- /** The model to use. */ -- model: string; -- /** The input image to upscale. */ -- image: Image_2; -- /** The factor to upscale the image (x2 or x4). */ -- upscaleFactor: string; -- /** Configuration for upscaling. */ -- config?: UpscaleImageConfig; --} -- --export declare class UpscaleImageResponse { -- /** Generated images. */ -- generatedImages?: GeneratedImage[]; --} -- --/** Tool to support URL context retrieval. */ --export declare interface UrlContext { --} -- --/** Metadata related to url context retrieval tool. */ --export declare interface UrlContextMetadata { -- /** List of url context. */ -- urlMetadata?: UrlMetadata[]; --} -- --/** Context for a single url retrieval. */ --export declare interface UrlMetadata { -- /** The URL retrieved by the tool. */ -- retrievedUrl?: string; -- /** Status of the url retrieval. */ -- urlRetrievalStatus?: UrlRetrievalStatus; --} -- --/** Status of the url retrieval. */ --export declare enum UrlRetrievalStatus { -- /** -- * Default value. This value is unused -- */ -- URL_RETRIEVAL_STATUS_UNSPECIFIED = "URL_RETRIEVAL_STATUS_UNSPECIFIED", -- /** -- * Url retrieval is successful. -- */ -- URL_RETRIEVAL_STATUS_SUCCESS = "URL_RETRIEVAL_STATUS_SUCCESS", -- /** -- * Url retrieval is failed due to error. -- */ -- URL_RETRIEVAL_STATUS_ERROR = "URL_RETRIEVAL_STATUS_ERROR" --} -- --/** Usage metadata about response(s). */ --export declare interface UsageMetadata { -- /** Number of tokens in the prompt. When `cached_content` is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content. */ -- promptTokenCount?: number; -- /** Number of tokens in the cached part of the prompt (the cached content). */ -- cachedContentTokenCount?: number; -- /** Total number of tokens across all the generated response candidates. */ -- responseTokenCount?: number; -- /** Number of tokens present in tool-use prompt(s). */ -- toolUsePromptTokenCount?: number; -- /** Number of tokens of thoughts for thinking models. */ -- thoughtsTokenCount?: number; -- /** Total token count for prompt, response candidates, and tool-use prompts(if present). */ -- totalTokenCount?: number; -- /** List of modalities that were processed in the request input. */ -- promptTokensDetails?: ModalityTokenCount[]; -- /** List of modalities that were processed in the cache input. */ -- cacheTokensDetails?: ModalityTokenCount[]; -- /** List of modalities that were returned in the response. */ -- responseTokensDetails?: ModalityTokenCount[]; -- /** List of modalities that were processed in the tool-use prompt. */ -- toolUsePromptTokensDetails?: ModalityTokenCount[]; -- /** Traffic type. This shows whether a request consumes Pay-As-You-Go -- or Provisioned Throughput quota. */ -- trafficType?: TrafficType; --} -- --/** Retrieve from Vertex AI Search datastore or engine for grounding. datastore and engine are mutually exclusive. See https://cloud.google.com/products/agent-builder */ --export declare interface VertexAISearch { -- /** Optional. Fully-qualified Vertex AI Search data store resource ID. Format: `projects/{project}/locations/{location}/collections/{collection}/dataStores/{dataStore}` */ -- datastore?: string; -- /** Optional. Fully-qualified Vertex AI Search engine resource ID. Format: `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}` */ -- engine?: string; --} -- --/** Retrieve from Vertex RAG Store for grounding. */ --export declare interface VertexRagStore { -- /** Optional. Deprecated. Please use rag_resources instead. */ -- ragCorpora?: string[]; -- /** Optional. The representation of the rag source. It can be used to specify corpus only or ragfiles. Currently only support one corpus or multiple files from one corpus. In the future we may open up multiple corpora support. */ -- ragResources?: VertexRagStoreRagResource[]; -- /** Optional. The retrieval config for the Rag query. */ -- ragRetrievalConfig?: RagRetrievalConfig; -- /** Optional. Number of top k results to return from the selected corpora. */ -- similarityTopK?: number; -- /** Optional. Only return results with vector distance smaller than the threshold. */ -- vectorDistanceThreshold?: number; --} -- --/** The definition of the Rag resource. */ --export declare interface VertexRagStoreRagResource { -- /** Optional. RagCorpora resource name. Format: `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}` */ -- ragCorpus?: string; -- /** Optional. rag_file_id. The files should be in the same rag_corpus set in rag_corpus field. */ -- ragFileIds?: string[]; --} -- --/** A generated video. */ --export declare interface Video { -- /** Path to another storage. */ -- uri?: string; -- /** Video bytes. */ -- videoBytes?: string; -- /** Video encoding, for example "video/mp4". */ -- mimeType?: string; --} -- --/** Describes how the video in the Part should be used by the model. */ --export declare interface VideoMetadata { -- /** The frame rate of the video sent to the model. If not specified, the -- default value will be 1.0. The fps range is (0.0, 24.0]. */ -- fps?: number; -- /** Optional. The end offset of the video. */ -- endOffset?: string; -- /** Optional. The start offset of the video. */ -- startOffset?: string; --} -- --/** The configuration for the voice to use. */ --export declare interface VoiceConfig { -- /** The configuration for the speaker to use. -- */ -- prebuiltVoiceConfig?: PrebuiltVoiceConfig; --} -- --declare interface WebSocket_2 { -- /** -- * Connects the socket to the server. -- */ -- connect(): void; -- /** -- * Sends a message to the server. -- */ -- send(message: string): void; -- /** -- * Closes the socket connection. -- */ -- close(): void; --} -- --/** -- * @license -- * Copyright 2025 Google LLC -- * SPDX-License-Identifier: Apache-2.0 -- */ --declare interface WebSocketCallbacks { -- onopen: () => void; -- onerror: (e: any) => void; -- onmessage: (e: any) => void; -- onclose: (e: any) => void; --} -- --declare interface WebSocketFactory { -- /** -- * Returns a new WebSocket instance. -- */ -- create(url: string, headers: Record, callbacks: WebSocketCallbacks): WebSocket_2; --} -- --/** Maps a prompt to a relative weight to steer music generation. */ --export declare interface WeightedPrompt { -- /** Text prompt. */ -- text?: string; -- /** Weight of the prompt. The weight is used to control the relative -- importance of the prompt. Higher weights are more important than lower -- weights. -- -- Weight must not be 0. Weights of all weighted_prompts in this -- LiveMusicClientContent message will be normalized. */ -- weight?: number; --} -- --export { } +-import { Client } from '@modelcontextprotocol/sdk/client/index.js'; +-import { GoogleAuthOptions } from 'google-auth-library'; +- +-/** Marks the end of user activity. +- +- This can only be sent if automatic (i.e. server-side) activity detection is +- disabled. +- */ +-export declare interface ActivityEnd { +-} +- +-/** The different ways of handling user activity. */ +-export declare enum ActivityHandling { +- /** +- * If unspecified, the default behavior is `START_OF_ACTIVITY_INTERRUPTS`. +- */ +- ACTIVITY_HANDLING_UNSPECIFIED = "ACTIVITY_HANDLING_UNSPECIFIED", +- /** +- * If true, start of activity will interrupt the model's response (also called "barge in"). The model's current response will be cut-off in the moment of the interruption. This is the default behavior. +- */ +- START_OF_ACTIVITY_INTERRUPTS = "START_OF_ACTIVITY_INTERRUPTS", +- /** +- * The model's response will not be interrupted. +- */ +- NO_INTERRUPTION = "NO_INTERRUPTION" +-} +- +-/** Marks the start of user activity. +- +- This can only be sent if automatic (i.e. server-side) activity detection is +- disabled. +- */ +-export declare interface ActivityStart { +-} +- +-/** Optional. Adapter size for tuning. */ +-export declare enum AdapterSize { +- /** +- * Adapter size is unspecified. +- */ +- ADAPTER_SIZE_UNSPECIFIED = "ADAPTER_SIZE_UNSPECIFIED", +- /** +- * Adapter size 1. +- */ +- ADAPTER_SIZE_ONE = "ADAPTER_SIZE_ONE", +- /** +- * Adapter size 2. +- */ +- ADAPTER_SIZE_TWO = "ADAPTER_SIZE_TWO", +- /** +- * Adapter size 4. +- */ +- ADAPTER_SIZE_FOUR = "ADAPTER_SIZE_FOUR", +- /** +- * Adapter size 8. +- */ +- ADAPTER_SIZE_EIGHT = "ADAPTER_SIZE_EIGHT", +- /** +- * Adapter size 16. +- */ +- ADAPTER_SIZE_SIXTEEN = "ADAPTER_SIZE_SIXTEEN", +- /** +- * Adapter size 32. +- */ +- ADAPTER_SIZE_THIRTY_TWO = "ADAPTER_SIZE_THIRTY_TWO" +-} +- +-/** +- * The ApiClient class is used to send requests to the Gemini API or Vertex AI +- * endpoints. +- */ +-declare class ApiClient { +- readonly clientOptions: ApiClientInitOptions; +- constructor(opts: ApiClientInitOptions); +- /** +- * Determines the base URL for Vertex AI based on project and location. +- * Uses the global endpoint if location is 'global' or if project/location +- * are not specified (implying API key usage). +- * @private +- */ +- private baseUrlFromProjectLocation; +- /** +- * Normalizes authentication parameters for Vertex AI. +- * If project and location are provided, API key is cleared. +- * If project and location are not provided (implying API key usage), +- * project and location are cleared. +- * @private +- */ +- private normalizeAuthParameters; +- isVertexAI(): boolean; +- getProject(): string | undefined; +- getLocation(): string | undefined; +- getApiVersion(): string; +- getBaseUrl(): string; +- getRequestUrl(): string; +- getHeaders(): Record; +- private getRequestUrlInternal; +- getBaseResourcePath(): string; +- getApiKey(): string | undefined; +- getWebsocketBaseUrl(): string; +- setBaseUrl(url: string): void; +- private constructUrl; +- private shouldPrependVertexProjectPath; +- request(request: HttpRequest): Promise; +- private patchHttpOptions; +- requestStream(request: HttpRequest): Promise>; +- private includeExtraHttpOptionsToRequestInit; +- private unaryApiCall; +- private streamApiCall; +- processStreamResponse(response: Response): AsyncGenerator; +- private apiCall; +- getDefaultHeaders(): Record; +- private getHeadersInternal; +- /** +- * Uploads a file asynchronously using Gemini API only, this is not supported +- * in Vertex AI. +- * +- * @param file The string path to the file to be uploaded or a Blob object. +- * @param config Optional parameters specified in the `UploadFileConfig` +- * interface. @see {@link UploadFileConfig} +- * @return A promise that resolves to a `File` object. +- * @throws An error if called on a Vertex AI client. +- * @throws An error if the `mimeType` is not provided and can not be inferred, +- */ +- uploadFile(file: string | Blob, config?: UploadFileConfig): Promise; +- /** +- * Downloads a file asynchronously to the specified path. +- * +- * @params params - The parameters for the download request, see {@link +- * DownloadFileParameters} +- */ +- downloadFile(params: DownloadFileParameters): Promise; +- private fetchUploadUrl; +-} +- +-/** +- * Options for initializing the ApiClient. The ApiClient uses the parameters +- * for authentication purposes as well as to infer if SDK should send the +- * request to Vertex AI or Gemini API. +- */ +-declare interface ApiClientInitOptions { +- /** +- * The object used for adding authentication headers to API requests. +- */ +- auth: Auth; +- /** +- * The uploader to use for uploading files. This field is required for +- * creating a client, will be set through the Node_client or Web_client. +- */ +- uploader: Uploader; +- /** +- * Optional. The downloader to use for downloading files. This field is +- * required for creating a client, will be set through the Node_client or +- * Web_client. +- */ +- downloader: Downloader; +- /** +- * Optional. The Google Cloud project ID for Vertex AI users. +- * It is not the numeric project name. +- * If not provided, SDK will try to resolve it from runtime environment. +- */ +- project?: string; +- /** +- * Optional. The Google Cloud project location for Vertex AI users. +- * If not provided, SDK will try to resolve it from runtime environment. +- */ +- location?: string; +- /** +- * The API Key. This is required for Gemini API users. +- */ +- apiKey?: string; +- /** +- * Optional. Set to true if you intend to call Vertex AI endpoints. +- * If unset, default SDK behavior is to call Gemini API. +- */ +- vertexai?: boolean; +- /** +- * Optional. The API version for the endpoint. +- * If unset, SDK will choose a default api version. +- */ +- apiVersion?: string; +- /** +- * Optional. A set of customizable configuration for HTTP requests. +- */ +- httpOptions?: HttpOptions; +- /** +- * Optional. An extra string to append at the end of the User-Agent header. +- * +- * This can be used to e.g specify the runtime and its version. +- */ +- userAgentExtra?: string; +-} +- +-/** Config for authentication with API key. */ +-export declare interface ApiKeyConfig { +- /** The API key to be used in the request directly. */ +- apiKeyString?: string; +-} +- +-/** Representation of an audio chunk. */ +-export declare interface AudioChunk { +- /** Raw byets of audio data. */ +- data?: string; +- /** MIME type of the audio chunk. */ +- mimeType?: string; +- /** Prompts and config used for generating this audio chunk. */ +- sourceMetadata?: LiveMusicSourceMetadata; +-} +- +-/** The audio transcription configuration in Setup. */ +-export declare interface AudioTranscriptionConfig { +-} +- +-/** +- * @license +- * Copyright 2025 Google LLC +- * SPDX-License-Identifier: Apache-2.0 +- */ +-/** +- * The Auth interface is used to authenticate with the API service. +- */ +-declare interface Auth { +- /** +- * Sets the headers needed to authenticate with the API service. +- * +- * @param headers - The Headers object that will be updated with the authentication headers. +- */ +- addAuthHeaders(headers: Headers): Promise; +-} +- +-/** Auth configuration to run the extension. */ +-export declare interface AuthConfig { +- /** Config for API key auth. */ +- apiKeyConfig?: ApiKeyConfig; +- /** Type of auth scheme. */ +- authType?: AuthType; +- /** Config for Google Service Account auth. */ +- googleServiceAccountConfig?: AuthConfigGoogleServiceAccountConfig; +- /** Config for HTTP Basic auth. */ +- httpBasicAuthConfig?: AuthConfigHttpBasicAuthConfig; +- /** Config for user oauth. */ +- oauthConfig?: AuthConfigOauthConfig; +- /** Config for user OIDC auth. */ +- oidcConfig?: AuthConfigOidcConfig; +-} +- +-/** Config for Google Service Account Authentication. */ +-export declare interface AuthConfigGoogleServiceAccountConfig { +- /** Optional. The service account that the extension execution service runs as. - If the service account is specified, the `iam.serviceAccounts.getAccessToken` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the specified service account. - If not specified, the Vertex AI Extension Service Agent will be used to execute the Extension. */ +- serviceAccount?: string; +-} +- +-/** Config for HTTP Basic Authentication. */ +-export declare interface AuthConfigHttpBasicAuthConfig { +- /** Required. The name of the SecretManager secret version resource storing the base64 encoded credentials. Format: `projects/{project}/secrets/{secrete}/versions/{version}` - If specified, the `secretmanager.versions.access` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the specified resource. */ +- credentialSecret?: string; +-} +- +-/** Config for user oauth. */ +-export declare interface AuthConfigOauthConfig { +- /** Access token for extension endpoint. Only used to propagate token from [[ExecuteExtensionRequest.runtime_auth_config]] at request time. */ +- accessToken?: string; +- /** The service account used to generate access tokens for executing the Extension. - If the service account is specified, the `iam.serviceAccounts.getAccessToken` permission should be granted to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents) on the provided service account. */ +- serviceAccount?: string; +-} +- +-/** Config for user OIDC auth. */ +-export declare interface AuthConfigOidcConfig { +- /** OpenID Connect formatted ID token for extension endpoint. Only used to propagate token from [[ExecuteExtensionRequest.runtime_auth_config]] at request time. */ +- idToken?: string; +- /** The service account used to generate an OpenID Connect (OIDC)-compatible JWT token signed by the Google OIDC Provider (accounts.google.com) for extension endpoint (https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-oidc). - The audience for the token will be set to the URL in the server url defined in the OpenApi spec. - If the service account is provided, the service account should grant `iam.serviceAccounts.getOpenIdToken` permission to Vertex AI Extension Service Agent (https://cloud.google.com/vertex-ai/docs/general/access-control#service-agents). */ +- serviceAccount?: string; +-} +- +-/** Type of auth scheme. */ +-export declare enum AuthType { +- AUTH_TYPE_UNSPECIFIED = "AUTH_TYPE_UNSPECIFIED", +- /** +- * No Auth. +- */ +- NO_AUTH = "NO_AUTH", +- /** +- * API Key Auth. +- */ +- API_KEY_AUTH = "API_KEY_AUTH", +- /** +- * HTTP Basic Auth. +- */ +- HTTP_BASIC_AUTH = "HTTP_BASIC_AUTH", +- /** +- * Google Service Account Auth. +- */ +- GOOGLE_SERVICE_ACCOUNT_AUTH = "GOOGLE_SERVICE_ACCOUNT_AUTH", +- /** +- * OAuth auth. +- */ +- OAUTH = "OAUTH", +- /** +- * OpenID Connect (OIDC) Auth. +- */ +- OIDC_AUTH = "OIDC_AUTH" +-} +- +-/** Configures automatic detection of activity. */ +-export declare interface AutomaticActivityDetection { +- /** If enabled, detected voice and text input count as activity. If disabled, the client must send activity signals. */ +- disabled?: boolean; +- /** Determines how likely speech is to be detected. */ +- startOfSpeechSensitivity?: StartSensitivity; +- /** Determines how likely detected speech is ended. */ +- endOfSpeechSensitivity?: EndSensitivity; +- /** The required duration of detected speech before start-of-speech is committed. The lower this value the more sensitive the start-of-speech detection is and the shorter speech can be recognized. However, this also increases the probability of false positives. */ +- prefixPaddingMs?: number; +- /** The required duration of detected non-speech (e.g. silence) before end-of-speech is committed. The larger this value, the longer speech gaps can be without interrupting the user's activity but this will increase the model's latency. */ +- silenceDurationMs?: number; +-} +- +-/** The configuration for automatic function calling. */ +-export declare interface AutomaticFunctionCallingConfig { +- /** Whether to disable automatic function calling. +- If not set or set to False, will enable automatic function calling. +- If set to True, will disable automatic function calling. +- */ +- disable?: boolean; +- /** If automatic function calling is enabled, +- maximum number of remote calls for automatic function calling. +- This number should be a positive integer. +- If not set, SDK will set maximum number of remote calls to 10. +- */ +- maximumRemoteCalls?: number; +- /** If automatic function calling is enabled, +- whether to ignore call history to the response. +- If not set, SDK will set ignore_call_history to false, +- and will append the call history to +- GenerateContentResponse.automatic_function_calling_history. +- */ +- ignoreCallHistory?: boolean; +-} +- +-/** +- * @license +- * Copyright 2025 Google LLC +- * SPDX-License-Identifier: Apache-2.0 +- */ +-declare class BaseModule { +-} +- +-/** +- * Parameters for setting the base URLs for the Gemini API and Vertex AI API. +- */ +-export declare interface BaseUrlParameters { +- geminiUrl?: string; +- vertexUrl?: string; +-} +- +-/** Defines the function behavior. Defaults to `BLOCKING`. */ +-export declare enum Behavior { +- /** +- * This value is unused. +- */ +- UNSPECIFIED = "UNSPECIFIED", +- /** +- * If set, the system will wait to receive the function response before continuing the conversation. +- */ +- BLOCKING = "BLOCKING", +- /** +- * If set, the system will not wait to receive the function response. Instead, it will attempt to handle function responses as they become available while maintaining the conversation between the user and the model. +- */ +- NON_BLOCKING = "NON_BLOCKING" +-} +- +-/** Content blob. */ +-declare interface Blob_2 { +- /** Optional. Display name of the blob. Used to provide a label or filename to distinguish blobs. This field is not currently used in the Gemini GenerateContent calls. */ +- displayName?: string; +- /** Required. Raw bytes. */ +- data?: string; +- /** Required. The IANA standard MIME type of the source data. */ +- mimeType?: string; +-} +-export { Blob_2 as Blob } +- +-export declare type BlobImageUnion = Blob_2; +- +-/** Output only. Blocked reason. */ +-export declare enum BlockedReason { +- /** +- * Unspecified blocked reason. +- */ +- BLOCKED_REASON_UNSPECIFIED = "BLOCKED_REASON_UNSPECIFIED", +- /** +- * Candidates blocked due to safety. +- */ +- SAFETY = "SAFETY", +- /** +- * Candidates blocked due to other reason. +- */ +- OTHER = "OTHER", +- /** +- * Candidates blocked due to the terms which are included from the terminology blocklist. +- */ +- BLOCKLIST = "BLOCKLIST", +- /** +- * Candidates blocked due to prohibited content. +- */ +- PROHIBITED_CONTENT = "PROHIBITED_CONTENT" +-} +- +-/** A resource used in LLM queries for users to explicitly specify what to cache. */ +-export declare interface CachedContent { +- /** The server-generated resource name of the cached content. */ +- name?: string; +- /** The user-generated meaningful display name of the cached content. */ +- displayName?: string; +- /** The name of the publisher model to use for cached content. */ +- model?: string; +- /** Creation time of the cache entry. */ +- createTime?: string; +- /** When the cache entry was last updated in UTC time. */ +- updateTime?: string; +- /** Expiration time of the cached content. */ +- expireTime?: string; +- /** Metadata on the usage of the cached content. */ +- usageMetadata?: CachedContentUsageMetadata; +-} +- +-/** Metadata on the usage of the cached content. */ +-export declare interface CachedContentUsageMetadata { +- /** Duration of audio in seconds. */ +- audioDurationSeconds?: number; +- /** Number of images. */ +- imageCount?: number; +- /** Number of text characters. */ +- textCount?: number; +- /** Total number of tokens that the cached content consumes. */ +- totalTokenCount?: number; +- /** Duration of video in seconds. */ +- videoDurationSeconds?: number; +-} +- +-export declare class Caches extends BaseModule { +- private readonly apiClient; +- constructor(apiClient: ApiClient); +- /** +- * Lists cached content configurations. +- * +- * @param params - The parameters for the list request. +- * @return The paginated results of the list of cached contents. +- * +- * @example +- * ```ts +- * const cachedContents = await ai.caches.list({config: {'pageSize': 2}}); +- * for (const cachedContent of cachedContents) { +- * console.log(cachedContent); +- * } +- * ``` +- */ +- list: (params?: types.ListCachedContentsParameters) => Promise>; +- /** +- * Creates a cached contents resource. +- * +- * @remarks +- * Context caching is only supported for specific models. See [Gemini +- * Developer API reference](https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac) +- * and [Vertex AI reference](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models) +- * for more information. +- * +- * @param params - The parameters for the create request. +- * @return The created cached content. +- * +- * @example +- * ```ts +- * const contents = ...; // Initialize the content to cache. +- * const response = await ai.caches.create({ +- * model: 'gemini-2.0-flash-001', +- * config: { +- * 'contents': contents, +- * 'displayName': 'test cache', +- * 'systemInstruction': 'What is the sum of the two pdfs?', +- * 'ttl': '86400s', +- * } +- * }); +- * ``` +- */ +- create(params: types.CreateCachedContentParameters): Promise; +- /** +- * Gets cached content configurations. +- * +- * @param params - The parameters for the get request. +- * @return The cached content. +- * +- * @example +- * ```ts +- * await ai.caches.get({name: '...'}); // The server-generated resource name. +- * ``` +- */ +- get(params: types.GetCachedContentParameters): Promise; +- /** +- * Deletes cached content. +- * +- * @param params - The parameters for the delete request. +- * @return The empty response returned by the API. +- * +- * @example +- * ```ts +- * await ai.caches.delete({name: '...'}); // The server-generated resource name. +- * ``` +- */ +- delete(params: types.DeleteCachedContentParameters): Promise; +- /** +- * Updates cached content configurations. +- * +- * @param params - The parameters for the update request. +- * @return The updated cached content. +- * +- * @example +- * ```ts +- * const response = await ai.caches.update({ +- * name: '...', // The server-generated resource name. +- * config: {'ttl': '7600s'} +- * }); +- * ``` +- */ +- update(params: types.UpdateCachedContentParameters): Promise; +- private listInternal; +-} +- +-/** +- * CallableTool is an invokable tool that can be executed with external +- * application (e.g., via Model Context Protocol) or local functions with +- * function calling. +- */ +-export declare interface CallableTool { +- /** +- * Returns tool that can be called by Gemini. +- */ +- tool(): Promise; +- /** +- * Executes the callable tool with the given function call arguments and +- * returns the response parts from the tool execution. +- */ +- callTool(functionCalls: FunctionCall[]): Promise; +-} +- +-/** +- * CallableToolConfig is the configuration for a callable tool. +- */ +-export declare interface CallableToolConfig { +- /** +- * Specifies the model's behavior after invoking this tool. +- */ +- behavior?: Behavior; +-} +- +-/** A response candidate generated from the model. */ +-export declare interface Candidate { +- /** Contains the multi-part content of the response. +- */ +- content?: Content; +- /** Source attribution of the generated content. +- */ +- citationMetadata?: CitationMetadata; +- /** Describes the reason the model stopped generating tokens. +- */ +- finishMessage?: string; +- /** Number of tokens for this candidate. +- */ +- tokenCount?: number; +- /** The reason why the model stopped generating tokens. +- If empty, the model has not stopped generating the tokens. +- */ +- finishReason?: FinishReason; +- /** Metadata related to url context retrieval tool. */ +- urlContextMetadata?: UrlContextMetadata; +- /** Output only. Average log probability score of the candidate. */ +- avgLogprobs?: number; +- /** Output only. Metadata specifies sources used to ground generated content. */ +- groundingMetadata?: GroundingMetadata; +- /** Output only. Index of the candidate. */ +- index?: number; +- /** Output only. Log-likelihood scores for the response tokens and top tokens */ +- logprobsResult?: LogprobsResult; +- /** Output only. List of ratings for the safety of a response candidate. There is at most one rating per category. */ +- safetyRatings?: SafetyRating[]; +-} +- +-/** +- * Chat session that enables sending messages to the model with previous +- * conversation context. +- * +- * @remarks +- * The session maintains all the turns between user and model. +- */ +-export declare class Chat { +- private readonly apiClient; +- private readonly modelsModule; +- private readonly model; +- private readonly config; +- private history; +- private sendPromise; +- constructor(apiClient: ApiClient, modelsModule: Models, model: string, config?: types.GenerateContentConfig, history?: types.Content[]); +- /** +- * Sends a message to the model and returns the response. +- * +- * @remarks +- * This method will wait for the previous message to be processed before +- * sending the next message. +- * +- * @see {@link Chat#sendMessageStream} for streaming method. +- * @param params - parameters for sending messages within a chat session. +- * @returns The model's response. +- * +- * @example +- * ```ts +- * const chat = ai.chats.create({model: 'gemini-2.0-flash'}); +- * const response = await chat.sendMessage({ +- * message: 'Why is the sky blue?' +- * }); +- * console.log(response.text); +- * ``` +- */ +- sendMessage(params: types.SendMessageParameters): Promise; +- /** +- * Sends a message to the model and returns the response in chunks. +- * +- * @remarks +- * This method will wait for the previous message to be processed before +- * sending the next message. +- * +- * @see {@link Chat#sendMessage} for non-streaming method. +- * @param params - parameters for sending the message. +- * @return The model's response. +- * +- * @example +- * ```ts +- * const chat = ai.chats.create({model: 'gemini-2.0-flash'}); +- * const response = await chat.sendMessageStream({ +- * message: 'Why is the sky blue?' +- * }); +- * for await (const chunk of response) { +- * console.log(chunk.text); +- * } +- * ``` +- */ +- sendMessageStream(params: types.SendMessageParameters): Promise>; +- /** +- * Returns the chat history. +- * +- * @remarks +- * The history is a list of contents alternating between user and model. +- * +- * There are two types of history: +- * - The `curated history` contains only the valid turns between user and +- * model, which will be included in the subsequent requests sent to the model. +- * - The `comprehensive history` contains all turns, including invalid or +- * empty model outputs, providing a complete record of the history. +- * +- * The history is updated after receiving the response from the model, +- * for streaming response, it means receiving the last chunk of the response. +- * +- * The `comprehensive history` is returned by default. To get the `curated +- * history`, set the `curated` parameter to `true`. +- * +- * @param curated - whether to return the curated history or the comprehensive +- * history. +- * @return History contents alternating between user and model for the entire +- * chat session. +- */ +- getHistory(curated?: boolean): types.Content[]; +- private processStreamResponse; +- private recordHistory; +-} +- +-/** +- * A utility class to create a chat session. +- */ +-export declare class Chats { +- private readonly modelsModule; +- private readonly apiClient; +- constructor(modelsModule: Models, apiClient: ApiClient); +- /** +- * Creates a new chat session. +- * +- * @remarks +- * The config in the params will be used for all requests within the chat +- * session unless overridden by a per-request `config` in +- * @see {@link types.SendMessageParameters#config}. +- * +- * @param params - Parameters for creating a chat session. +- * @returns A new chat session. +- * +- * @example +- * ```ts +- * const chat = ai.chats.create({ +- * model: 'gemini-2.0-flash' +- * config: { +- * temperature: 0.5, +- * maxOutputTokens: 1024, +- * } +- * }); +- * ``` +- */ +- create(params: types.CreateChatParameters): Chat; +-} +- +-/** Describes the machine learning model version checkpoint. */ +-export declare interface Checkpoint { +- /** The ID of the checkpoint. +- */ +- checkpointId?: string; +- /** The epoch of the checkpoint. +- */ +- epoch?: string; +- /** The step of the checkpoint. +- */ +- step?: string; +-} +- +-/** Source attributions for content. */ +-export declare interface Citation { +- /** Output only. End index into the content. */ +- endIndex?: number; +- /** Output only. License of the attribution. */ +- license?: string; +- /** Output only. Publication date of the attribution. */ +- publicationDate?: GoogleTypeDate; +- /** Output only. Start index into the content. */ +- startIndex?: number; +- /** Output only. Title of the attribution. */ +- title?: string; +- /** Output only. Url reference of the attribution. */ +- uri?: string; +-} +- +-/** Citation information when the model quotes another source. */ +-export declare interface CitationMetadata { +- /** Contains citation information when the model directly quotes, at +- length, from another source. Can include traditional websites and code +- repositories. +- */ +- citations?: Citation[]; +-} +- +-/** Result of executing the [ExecutableCode]. Always follows a `part` containing the [ExecutableCode]. */ +-export declare interface CodeExecutionResult { +- /** Required. Outcome of the code execution. */ +- outcome?: Outcome; +- /** Optional. Contains stdout when code execution is successful, stderr or other description otherwise. */ +- output?: string; +-} +- +-/** Optional parameters for computing tokens. */ +-export declare interface ComputeTokensConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for computing tokens. */ +-export declare interface ComputeTokensParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** Input content. */ +- contents: ContentListUnion; +- /** Optional parameters for the request. +- */ +- config?: ComputeTokensConfig; +-} +- +-/** Response for computing tokens. */ +-export declare class ComputeTokensResponse { +- /** Lists of tokens info from the input. A ComputeTokensRequest could have multiple instances with a prompt in each instance. We also need to return lists of tokens info for the request with multiple instances. */ +- tokensInfo?: TokensInfo[]; +-} +- +-/** Contains the multi-part content of a message. */ +-export declare interface Content { +- /** List of parts that constitute a single message. Each part may have +- a different IANA MIME type. */ +- parts?: Part[]; +- /** Optional. The producer of the content. Must be either 'user' or +- 'model'. Useful to set for multi-turn conversations, otherwise can be +- empty. If role is not specified, SDK will determine the role. */ +- role?: string; +-} +- +-/** The embedding generated from an input content. */ +-export declare interface ContentEmbedding { +- /** A list of floats representing an embedding. +- */ +- values?: number[]; +- /** Vertex API only. Statistics of the input text associated with this +- embedding. +- */ +- statistics?: ContentEmbeddingStatistics; +-} +- +-/** Statistics of the input text associated with the result of content embedding. */ +-export declare interface ContentEmbeddingStatistics { +- /** Vertex API only. If the input text was truncated due to having +- a length longer than the allowed maximum input. +- */ +- truncated?: boolean; +- /** Vertex API only. Number of tokens of the input text. +- */ +- tokenCount?: number; +-} +- +-export declare type ContentListUnion = Content | Content[] | PartUnion | PartUnion[]; +- +-export declare type ContentUnion = Content | PartUnion[] | PartUnion; +- +-/** Enables context window compression -- mechanism managing model context window so it does not exceed given length. */ +-export declare interface ContextWindowCompressionConfig { +- /** Number of tokens (before running turn) that triggers context window compression mechanism. */ +- triggerTokens?: string; +- /** Sliding window compression mechanism. */ +- slidingWindow?: SlidingWindow; +-} +- +-/** Configuration for a Control reference image. */ +-export declare interface ControlReferenceConfig { +- /** The type of control reference image to use. */ +- controlType?: ControlReferenceType; +- /** Defaults to False. When set to True, the control image will be +- computed by the model based on the control type. When set to False, +- the control image must be provided by the user. */ +- enableControlImageComputation?: boolean; +-} +- +-/** A control reference image. +- +- The image of the control reference image is either a control image provided +- by the user, or a regular image which the backend will use to generate a +- control image of. In the case of the latter, the +- enable_control_image_computation field in the config should be set to True. +- +- A control image is an image that represents a sketch image of areas for the +- model to fill in based on the prompt. +- */ +-export declare class ControlReferenceImage { +- /** The reference image for the editing operation. */ +- referenceImage?: Image_2; +- /** The id of the reference image. */ +- referenceId?: number; +- /** The type of the reference image. Only set by the SDK. */ +- referenceType?: string; +- /** Configuration for the control reference image. */ +- config?: ControlReferenceConfig; +- /** Internal method to convert to ReferenceImageAPIInternal. */ +- toReferenceImageAPI(): any; +-} +- +-/** Enum representing the control type of a control reference image. */ +-export declare enum ControlReferenceType { +- CONTROL_TYPE_DEFAULT = "CONTROL_TYPE_DEFAULT", +- CONTROL_TYPE_CANNY = "CONTROL_TYPE_CANNY", +- CONTROL_TYPE_SCRIBBLE = "CONTROL_TYPE_SCRIBBLE", +- CONTROL_TYPE_FACE_MESH = "CONTROL_TYPE_FACE_MESH" +-} +- +-/** Config for the count_tokens method. */ +-export declare interface CountTokensConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Instructions for the model to steer it toward better performance. +- */ +- systemInstruction?: ContentUnion; +- /** Code that enables the system to interact with external systems to +- perform an action outside of the knowledge and scope of the model. +- */ +- tools?: Tool[]; +- /** Configuration that the model uses to generate the response. Not +- supported by the Gemini Developer API. +- */ +- generationConfig?: GenerationConfig; +-} +- +-/** Parameters for counting tokens. */ +-export declare interface CountTokensParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** Input content. */ +- contents: ContentListUnion; +- /** Configuration for counting tokens. */ +- config?: CountTokensConfig; +-} +- +-/** Response for counting tokens. */ +-export declare class CountTokensResponse { +- /** Total number of tokens. */ +- totalTokens?: number; +- /** Number of tokens in the cached part of the prompt (the cached content). */ +- cachedContentTokenCount?: number; +-} +- +-/** Optional parameters. */ +-export declare interface CreateAuthTokenConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** An optional time after which, when using the resulting token, +- messages in Live API sessions will be rejected. (Gemini may +- preemptively close the session after this time.) +- +- If not set then this defaults to 30 minutes in the future. If set, this +- value must be less than 20 hours in the future. */ +- expireTime?: string; +- /** The time after which new Live API sessions using the token +- resulting from this request will be rejected. +- +- If not set this defaults to 60 seconds in the future. If set, this value +- must be less than 20 hours in the future. */ +- newSessionExpireTime?: string; +- /** The number of times the token can be used. If this value is zero +- then no limit is applied. Default is 1. Resuming a Live API session does +- not count as a use. */ +- uses?: number; +- /** Configuration specific to Live API connections created using this token. */ +- liveEphemeralParameters?: LiveEphemeralParameters; +- /** Additional fields to lock in the effective LiveConnectParameters. */ +- lockAdditionalFields?: string[]; +-} +- +-/** Optional configuration for cached content creation. */ +-export declare interface CreateCachedContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** The TTL for this resource. The expiration time is computed: now + TTL. It is a duration string, with up to nine fractional digits, terminated by 's'. Example: "3.5s". */ +- ttl?: string; +- /** Timestamp of when this resource is considered expired. Uses RFC 3339 format, Example: 2014-10-02T15:01:23Z. */ +- expireTime?: string; +- /** The user-generated meaningful display name of the cached content. +- */ +- displayName?: string; +- /** The content to cache. +- */ +- contents?: ContentListUnion; +- /** Developer set system instruction. +- */ +- systemInstruction?: ContentUnion; +- /** A list of `Tools` the model may use to generate the next response. +- */ +- tools?: Tool[]; +- /** Configuration for the tools to use. This config is shared for all tools. +- */ +- toolConfig?: ToolConfig; +- /** The Cloud KMS resource identifier of the customer managed +- encryption key used to protect a resource. +- The key needs to be in the same region as where the compute resource is +- created. See +- https://cloud.google.com/vertex-ai/docs/general/cmek for more +- details. If this is set, then all created CachedContent objects +- will be encrypted with the provided encryption key. +- Allowed formats: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key} +- */ +- kmsKeyName?: string; +-} +- +-/** Parameters for caches.create method. */ +-export declare interface CreateCachedContentParameters { +- /** ID of the model to use. Example: gemini-2.0-flash */ +- model: string; +- /** Configuration that contains optional parameters. +- */ +- config?: CreateCachedContentConfig; +-} +- +-/** Parameters for initializing a new chat session. +- +- These parameters are used when creating a chat session with the +- `chats.create()` method. +- */ +-export declare interface CreateChatParameters { +- /** The name of the model to use for the chat session. +- +- For example: 'gemini-2.0-flash', 'gemini-2.0-flash-lite', etc. See Gemini API +- docs to find the available models. +- */ +- model: string; +- /** Config for the entire chat session. +- +- This config applies to all requests within the session +- unless overridden by a per-request `config` in `SendMessageParameters`. +- */ +- config?: GenerateContentConfig; +- /** The initial conversation history for the chat session. +- +- This allows you to start the chat with a pre-existing history. The history +- must be a list of `Content` alternating between 'user' and 'model' roles. +- It should start with a 'user' message. +- */ +- history?: Content[]; +-} +- +-/** Used to override the default configuration. */ +-export declare interface CreateFileConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Generates the parameters for the private _create method. */ +-export declare interface CreateFileParameters { +- /** The file to be uploaded. +- mime_type: (Required) The MIME type of the file. Must be provided. +- name: (Optional) The name of the file in the destination (e.g. +- 'files/sample-image'). +- display_name: (Optional) The display name of the file. +- */ +- file: File_2; +- /** Used to override the default configuration. */ +- config?: CreateFileConfig; +-} +- +-/** Response for the create file method. */ +-export declare class CreateFileResponse { +- /** Used to retain the full HTTP response. */ +- sdkHttpResponse?: HttpResponse; +-} +- +-/** +- * Creates a `Content` object with a model role from a `PartListUnion` object or `string`. +- */ +-export declare function createModelContent(partOrString: PartListUnion | string): Content; +- +-/** +- * Creates a `Part` object from a `base64` encoded `string`. +- */ +-export declare function createPartFromBase64(data: string, mimeType: string): Part; +- +-/** +- * Creates a `Part` object from the `outcome` and `output` of a `CodeExecutionResult` object. +- */ +-export declare function createPartFromCodeExecutionResult(outcome: Outcome, output: string): Part; +- +-/** +- * Creates a `Part` object from the `code` and `language` of an `ExecutableCode` object. +- */ +-export declare function createPartFromExecutableCode(code: string, language: Language): Part; +- +-/** +- * Creates a `Part` object from a `FunctionCall` object. +- */ +-export declare function createPartFromFunctionCall(name: string, args: Record): Part; +- +-/** +- * Creates a `Part` object from a `FunctionResponse` object. +- */ +-export declare function createPartFromFunctionResponse(id: string, name: string, response: Record): Part; +- +-/** +- * Creates a `Part` object from a `text` string. +- */ +-export declare function createPartFromText(text: string): Part; +- +-/** +- * Creates a `Part` object from a `URI` string. +- */ +-export declare function createPartFromUri(uri: string, mimeType: string): Part; +- +-/** Supervised fine-tuning job creation request - optional fields. */ +-export declare interface CreateTuningJobConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ +- validationDataset?: TuningValidationDataset; +- /** The display name of the tuned Model. The name can be up to 128 characters long and can consist of any UTF-8 characters. */ +- tunedModelDisplayName?: string; +- /** The description of the TuningJob */ +- description?: string; +- /** Number of complete passes the model makes over the entire training dataset during training. */ +- epochCount?: number; +- /** Multiplier for adjusting the default learning rate. */ +- learningRateMultiplier?: number; +- /** If set to true, disable intermediate checkpoints for SFT and only the last checkpoint will be exported. Otherwise, enable intermediate checkpoints for SFT. */ +- exportLastCheckpointOnly?: boolean; +- /** Adapter size for tuning. */ +- adapterSize?: AdapterSize; +- /** The batch size hyperparameter for tuning. If not set, a default of 4 or 16 will be used based on the number of training examples. */ +- batchSize?: number; +- /** The learning rate hyperparameter for tuning. If not set, a default of 0.001 or 0.0002 will be calculated based on the number of training examples. */ +- learningRate?: number; +-} +- +-/** Supervised fine-tuning job creation parameters - optional fields. */ +-export declare interface CreateTuningJobParameters { +- /** The base model that is being tuned, e.g., "gemini-1.0-pro-002". */ +- baseModel: string; +- /** Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ +- trainingDataset: TuningDataset; +- /** Configuration for the tuning job. */ +- config?: CreateTuningJobConfig; +-} +- +-/** +- * Creates a `Content` object with a user role from a `PartListUnion` object or `string`. +- */ +-export declare function createUserContent(partOrString: PartListUnion | string): Content; +- +-/** Distribution computed over a tuning dataset. */ +-export declare interface DatasetDistribution { +- /** Output only. Defines the histogram bucket. */ +- buckets?: DatasetDistributionDistributionBucket[]; +- /** Output only. The maximum of the population values. */ +- max?: number; +- /** Output only. The arithmetic mean of the values in the population. */ +- mean?: number; +- /** Output only. The median of the values in the population. */ +- median?: number; +- /** Output only. The minimum of the population values. */ +- min?: number; +- /** Output only. The 5th percentile of the values in the population. */ +- p5?: number; +- /** Output only. The 95th percentile of the values in the population. */ +- p95?: number; +- /** Output only. Sum of a given population of values. */ +- sum?: number; +-} +- +-/** Dataset bucket used to create a histogram for the distribution given a population of values. */ +-export declare interface DatasetDistributionDistributionBucket { +- /** Output only. Number of values in the bucket. */ +- count?: string; +- /** Output only. Left bound of the bucket. */ +- left?: number; +- /** Output only. Right bound of the bucket. */ +- right?: number; +-} +- +-/** Statistics computed over a tuning dataset. */ +-export declare interface DatasetStats { +- /** Output only. Number of billable characters in the tuning dataset. */ +- totalBillableCharacterCount?: string; +- /** Output only. Number of tuning characters in the tuning dataset. */ +- totalTuningCharacterCount?: string; +- /** Output only. Number of examples in the tuning dataset. */ +- tuningDatasetExampleCount?: string; +- /** Output only. Number of tuning steps for this Tuning Job. */ +- tuningStepCount?: string; +- /** Output only. Sample user messages in the training dataset uri. */ +- userDatasetExamples?: Content[]; +- /** Output only. Dataset distributions for the user input tokens. */ +- userInputTokenDistribution?: DatasetDistribution; +- /** Output only. Dataset distributions for the messages per example. */ +- userMessagePerExampleDistribution?: DatasetDistribution; +- /** Output only. Dataset distributions for the user output tokens. */ +- userOutputTokenDistribution?: DatasetDistribution; +-} +- +-/** Optional parameters for caches.delete method. */ +-export declare interface DeleteCachedContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for caches.delete method. */ +-export declare interface DeleteCachedContentParameters { +- /** The server-generated resource name of the cached content. +- */ +- name: string; +- /** Optional parameters for the request. +- */ +- config?: DeleteCachedContentConfig; +-} +- +-/** Empty response for caches.delete method. */ +-export declare class DeleteCachedContentResponse { +-} +- +-/** Used to override the default configuration. */ +-export declare interface DeleteFileConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Generates the parameters for the get method. */ +-export declare interface DeleteFileParameters { +- /** The name identifier for the file to be deleted. */ +- name: string; +- /** Used to override the default configuration. */ +- config?: DeleteFileConfig; +-} +- +-/** Response for the delete file method. */ +-export declare class DeleteFileResponse { +-} +- +-/** Configuration for deleting a tuned model. */ +-export declare interface DeleteModelConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for deleting a tuned model. */ +-export declare interface DeleteModelParameters { +- model: string; +- /** Optional parameters for the request. */ +- config?: DeleteModelConfig; +-} +- +-export declare class DeleteModelResponse { +-} +- +-/** Statistics computed for datasets used for distillation. */ +-export declare interface DistillationDataStats { +- /** Output only. Statistics computed for the training dataset. */ +- trainingDatasetStats?: DatasetStats; +-} +- +-/** Hyperparameters for Distillation. */ +-export declare interface DistillationHyperParameters { +- /** Optional. Adapter size for distillation. */ +- adapterSize?: AdapterSize; +- /** Optional. Number of complete passes the model makes over the entire training dataset during training. */ +- epochCount?: string; +- /** Optional. Multiplier for adjusting the default learning rate. */ +- learningRateMultiplier?: number; +-} +- +-/** Tuning Spec for Distillation. */ +-export declare interface DistillationSpec { +- /** The base teacher model that is being distilled, e.g., "gemini-1.0-pro-002". */ +- baseTeacherModel?: string; +- /** Optional. Hyperparameters for Distillation. */ +- hyperParameters?: DistillationHyperParameters; +- /** Required. A path in a Cloud Storage bucket, which will be treated as the root output directory of the distillation pipeline. It is used by the system to generate the paths of output artifacts. */ +- pipelineRootDirectory?: string; +- /** The student model that is being tuned, e.g., "google/gemma-2b-1.1-it". */ +- studentModel?: string; +- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ +- trainingDatasetUri?: string; +- /** The resource name of the Tuned teacher model. Format: `projects/{project}/locations/{location}/models/{model}`. */ +- tunedTeacherModelSource?: string; +- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ +- validationDatasetUri?: string; +-} +- +-export declare type DownloadableFileUnion = string | File_2 | GeneratedVideo | Video; +- +-declare interface Downloader { +- /** +- * Downloads a file to the given location. +- * +- * @param params The parameters for downloading the file. +- * @param apiClient The ApiClient to use for uploading. +- * @return A Promises that resolves when the download is complete. +- */ +- download(params: DownloadFileParameters, apiClient: ApiClient): Promise; +-} +- +-/** Used to override the default configuration. */ +-export declare interface DownloadFileConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters used to download a file. */ +-export declare interface DownloadFileParameters { +- /** The file to download. It can be a file name, a file object or a generated video. */ +- file: DownloadableFileUnion; +- /** Location where the file should be downloaded to. */ +- downloadPath: string; +- /** Configuration to for the download operation. */ +- config?: DownloadFileConfig; +-} +- +-/** Describes the options to customize dynamic retrieval. */ +-export declare interface DynamicRetrievalConfig { +- /** The mode of the predictor to be used in dynamic retrieval. */ +- mode?: DynamicRetrievalConfigMode; +- /** Optional. The threshold to be used in dynamic retrieval. If not set, a system default value is used. */ +- dynamicThreshold?: number; +-} +- +-/** Config for the dynamic retrieval config mode. */ +-export declare enum DynamicRetrievalConfigMode { +- /** +- * Always trigger retrieval. +- */ +- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", +- /** +- * Run retrieval only when system decides it is necessary. +- */ +- MODE_DYNAMIC = "MODE_DYNAMIC" +-} +- +-/** Configuration for editing an image. */ +-export declare interface EditImageConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Cloud Storage URI used to store the generated images. +- */ +- outputGcsUri?: string; +- /** Description of what to discourage in the generated images. +- */ +- negativePrompt?: string; +- /** Number of images to generate. +- */ +- numberOfImages?: number; +- /** Aspect ratio of the generated images. +- */ +- aspectRatio?: string; +- /** Controls how much the model adheres to the text prompt. Large +- values increase output and prompt alignment, but may compromise image +- quality. +- */ +- guidanceScale?: number; +- /** Random seed for image generation. This is not available when +- ``add_watermark`` is set to true. +- */ +- seed?: number; +- /** Filter level for safety filtering. +- */ +- safetyFilterLevel?: SafetyFilterLevel; +- /** Allows generation of people by the model. +- */ +- personGeneration?: PersonGeneration; +- /** Whether to report the safety scores of each generated image and +- the positive prompt in the response. +- */ +- includeSafetyAttributes?: boolean; +- /** Whether to include the Responsible AI filter reason if the image +- is filtered out of the response. +- */ +- includeRaiReason?: boolean; +- /** Language of the text in the prompt. +- */ +- language?: ImagePromptLanguage; +- /** MIME type of the generated image. +- */ +- outputMimeType?: string; +- /** Compression quality of the generated image (for ``image/jpeg`` +- only). +- */ +- outputCompressionQuality?: number; +- /** Describes the editing mode for the request. */ +- editMode?: EditMode; +- /** The number of sampling steps. A higher value has better image +- quality, while a lower value has better latency. */ +- baseSteps?: number; +-} +- +-/** Parameters for the request to edit an image. */ +-export declare interface EditImageParameters { +- /** The model to use. */ +- model: string; +- /** A text description of the edit to apply to the image. */ +- prompt: string; +- /** The reference images for Imagen 3 editing. */ +- referenceImages: ReferenceImage[]; +- /** Configuration for editing. */ +- config?: EditImageConfig; +-} +- +-/** Response for the request to edit an image. */ +-export declare class EditImageResponse { +- /** Generated images. */ +- generatedImages?: GeneratedImage[]; +-} +- +-/** Enum representing the Imagen 3 Edit mode. */ +-export declare enum EditMode { +- EDIT_MODE_DEFAULT = "EDIT_MODE_DEFAULT", +- EDIT_MODE_INPAINT_REMOVAL = "EDIT_MODE_INPAINT_REMOVAL", +- EDIT_MODE_INPAINT_INSERTION = "EDIT_MODE_INPAINT_INSERTION", +- EDIT_MODE_OUTPAINT = "EDIT_MODE_OUTPAINT", +- EDIT_MODE_CONTROLLED_EDITING = "EDIT_MODE_CONTROLLED_EDITING", +- EDIT_MODE_STYLE = "EDIT_MODE_STYLE", +- EDIT_MODE_BGSWAP = "EDIT_MODE_BGSWAP", +- EDIT_MODE_PRODUCT_IMAGE = "EDIT_MODE_PRODUCT_IMAGE" +-} +- +-/** Optional parameters for the embed_content method. */ +-export declare interface EmbedContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Type of task for which the embedding will be used. +- */ +- taskType?: string; +- /** Title for the text. Only applicable when TaskType is +- `RETRIEVAL_DOCUMENT`. +- */ +- title?: string; +- /** Reduced dimension for the output embedding. If set, +- excessive values in the output embedding are truncated from the end. +- Supported by newer models since 2024 only. You cannot set this value if +- using the earlier model (`models/embedding-001`). +- */ +- outputDimensionality?: number; +- /** Vertex API only. The MIME type of the input. +- */ +- mimeType?: string; +- /** Vertex API only. Whether to silently truncate inputs longer than +- the max sequence length. If this option is set to false, oversized inputs +- will lead to an INVALID_ARGUMENT error, similar to other text APIs. +- */ +- autoTruncate?: boolean; +-} +- +-/** Request-level metadata for the Vertex Embed Content API. */ +-export declare interface EmbedContentMetadata { +- /** Vertex API only. The total number of billable characters included +- in the request. +- */ +- billableCharacterCount?: number; +-} +- +-/** Parameters for the embed_content method. */ +-export declare interface EmbedContentParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** The content to embed. Only the `parts.text` fields will be counted. +- */ +- contents: ContentListUnion; +- /** Configuration that contains optional parameters. +- */ +- config?: EmbedContentConfig; +-} +- +-/** Response for the embed_content method. */ +-export declare class EmbedContentResponse { +- /** The embeddings for each request, in the same order as provided in +- the batch request. +- */ +- embeddings?: ContentEmbedding[]; +- /** Vertex API only. Metadata about the request. +- */ +- metadata?: EmbedContentMetadata; +-} +- +-/** Represents a customer-managed encryption key spec that can be applied to a top-level resource. */ +-export declare interface EncryptionSpec { +- /** Required. The Cloud KMS resource identifier of the customer managed encryption key used to protect a resource. Has the form: `projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`. The key needs to be in the same region as where the compute resource is created. */ +- kmsKeyName?: string; +-} +- +-/** An endpoint where you deploy models. */ +-export declare interface Endpoint { +- /** Resource name of the endpoint. */ +- name?: string; +- /** ID of the model that's deployed to the endpoint. */ +- deployedModelId?: string; +-} +- +-/** End of speech sensitivity. */ +-export declare enum EndSensitivity { +- /** +- * The default is END_SENSITIVITY_LOW. +- */ +- END_SENSITIVITY_UNSPECIFIED = "END_SENSITIVITY_UNSPECIFIED", +- /** +- * Automatic detection ends speech more often. +- */ +- END_SENSITIVITY_HIGH = "END_SENSITIVITY_HIGH", +- /** +- * Automatic detection ends speech less often. +- */ +- END_SENSITIVITY_LOW = "END_SENSITIVITY_LOW" +-} +- +-/** Tool to search public web data, powered by Vertex AI Search and Sec4 compliance. */ +-export declare interface EnterpriseWebSearch { +-} +- +-/** Code generated by the model that is meant to be executed, and the result returned to the model. Generated when using the [FunctionDeclaration] tool and [FunctionCallingConfig] mode is set to [Mode.CODE]. */ +-export declare interface ExecutableCode { +- /** Required. The code to be executed. */ +- code?: string; +- /** Required. Programming language of the `code`. */ +- language?: Language; +-} +- +-/** Options for feature selection preference. */ +-export declare enum FeatureSelectionPreference { +- FEATURE_SELECTION_PREFERENCE_UNSPECIFIED = "FEATURE_SELECTION_PREFERENCE_UNSPECIFIED", +- PRIORITIZE_QUALITY = "PRIORITIZE_QUALITY", +- BALANCED = "BALANCED", +- PRIORITIZE_COST = "PRIORITIZE_COST" +-} +- +-export declare interface FetchPredictOperationConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for the fetchPredictOperation method. */ +-export declare interface FetchPredictOperationParameters { +- /** The server-assigned name for the operation. */ +- operationName: string; +- resourceName: string; +- /** Used to override the default configuration. */ +- config?: FetchPredictOperationConfig; +-} +- +-/** A file uploaded to the API. */ +-declare interface File_2 { +- /** The `File` resource name. The ID (name excluding the "files/" prefix) can contain up to 40 characters that are lowercase alphanumeric or dashes (-). The ID cannot start or end with a dash. If the name is empty on create, a unique name will be generated. Example: `files/123-456` */ +- name?: string; +- /** Optional. The human-readable display name for the `File`. The display name must be no more than 512 characters in length, including spaces. Example: 'Welcome Image' */ +- displayName?: string; +- /** Output only. MIME type of the file. */ +- mimeType?: string; +- /** Output only. Size of the file in bytes. */ +- sizeBytes?: string; +- /** Output only. The timestamp of when the `File` was created. */ +- createTime?: string; +- /** Output only. The timestamp of when the `File` will be deleted. Only set if the `File` is scheduled to expire. */ +- expirationTime?: string; +- /** Output only. The timestamp of when the `File` was last updated. */ +- updateTime?: string; +- /** Output only. SHA-256 hash of the uploaded bytes. The hash value is encoded in base64 format. */ +- sha256Hash?: string; +- /** Output only. The URI of the `File`. */ +- uri?: string; +- /** Output only. The URI of the `File`, only set for downloadable (generated) files. */ +- downloadUri?: string; +- /** Output only. Processing state of the File. */ +- state?: FileState; +- /** Output only. The source of the `File`. */ +- source?: FileSource; +- /** Output only. Metadata for a video. */ +- videoMetadata?: Record; +- /** Output only. Error status if File processing failed. */ +- error?: FileStatus; +-} +-export { File_2 as File } +- +-/** URI based data. */ +-export declare interface FileData { +- /** Required. URI. */ +- fileUri?: string; +- /** Required. The IANA standard MIME type of the source data. */ +- mimeType?: string; +-} +- +-export declare class Files extends BaseModule { +- private readonly apiClient; +- constructor(apiClient: ApiClient); +- /** +- * Lists all current project files from the service. +- * +- * @param params - The parameters for the list request +- * @return The paginated results of the list of files +- * +- * @example +- * The following code prints the names of all files from the service, the +- * size of each page is 10. +- * +- * ```ts +- * const listResponse = await ai.files.list({config: {'pageSize': 10}}); +- * for await (const file of listResponse) { +- * console.log(file.name); +- * } +- * ``` +- */ +- list: (params?: types.ListFilesParameters) => Promise>; +- /** +- * Uploads a file asynchronously to the Gemini API. +- * This method is not available in Vertex AI. +- * Supported upload sources: +- * - Node.js: File path (string) or Blob object. +- * - Browser: Blob object (e.g., File). +- * +- * @remarks +- * The `mimeType` can be specified in the `config` parameter. If omitted: +- * - For file path (string) inputs, the `mimeType` will be inferred from the +- * file extension. +- * - For Blob object inputs, the `mimeType` will be set to the Blob's `type` +- * property. +- * Somex eamples for file extension to mimeType mapping: +- * .txt -> text/plain +- * .json -> application/json +- * .jpg -> image/jpeg +- * .png -> image/png +- * .mp3 -> audio/mpeg +- * .mp4 -> video/mp4 +- * +- * This section can contain multiple paragraphs and code examples. +- * +- * @param params - Optional parameters specified in the +- * `types.UploadFileParameters` interface. +- * @see {@link types.UploadFileParameters#config} for the optional +- * config in the parameters. +- * @return A promise that resolves to a `types.File` object. +- * @throws An error if called on a Vertex AI client. +- * @throws An error if the `mimeType` is not provided and can not be inferred, +- * the `mimeType` can be provided in the `params.config` parameter. +- * @throws An error occurs if a suitable upload location cannot be established. +- * +- * @example +- * The following code uploads a file to Gemini API. +- * +- * ```ts +- * const file = await ai.files.upload({file: 'file.txt', config: { +- * mimeType: 'text/plain', +- * }}); +- * console.log(file.name); +- * ``` +- */ +- upload(params: types.UploadFileParameters): Promise; +- /** +- * Downloads a remotely stored file asynchronously to a location specified in +- * the `params` object. This method only works on Node environment, to +- * download files in the browser, use a browser compliant method like an +- * tag. +- * +- * @param params - The parameters for the download request. +- * +- * @example +- * The following code downloads an example file named "files/mehozpxf877d" as +- * "file.txt". +- * +- * ```ts +- * await ai.files.download({file: file.name, downloadPath: 'file.txt'}); +- * ``` +- */ +- download(params: types.DownloadFileParameters): Promise; +- private listInternal; +- private createInternal; +- /** +- * Retrieves the file information from the service. +- * +- * @param params - The parameters for the get request +- * @return The Promise that resolves to the types.File object requested. +- * +- * @example +- * ```ts +- * const config: GetFileParameters = { +- * name: fileName, +- * }; +- * file = await ai.files.get(config); +- * console.log(file.name); +- * ``` +- */ +- get(params: types.GetFileParameters): Promise; +- /** +- * Deletes a remotely stored file. +- * +- * @param params - The parameters for the delete request. +- * @return The DeleteFileResponse, the response for the delete method. +- * +- * @example +- * The following code deletes an example file named "files/mehozpxf877d". +- * +- * ```ts +- * await ai.files.delete({name: file.name}); +- * ``` +- */ +- delete(params: types.DeleteFileParameters): Promise; +-} +- +-/** Source of the File. */ +-export declare enum FileSource { +- SOURCE_UNSPECIFIED = "SOURCE_UNSPECIFIED", +- UPLOADED = "UPLOADED", +- GENERATED = "GENERATED" +-} +- +-/** +- * Represents the size and mimeType of a file. The information is used to +- * request the upload URL from the https://generativelanguage.googleapis.com/upload/v1beta/files endpoint. +- * This interface defines the structure for constructing and executing HTTP +- * requests. +- */ +-declare interface FileStat { +- /** +- * The size of the file in bytes. +- */ +- size: number; +- /** +- * The MIME type of the file. +- */ +- type: string | undefined; +-} +- +-/** State for the lifecycle of a File. */ +-export declare enum FileState { +- STATE_UNSPECIFIED = "STATE_UNSPECIFIED", +- PROCESSING = "PROCESSING", +- ACTIVE = "ACTIVE", +- FAILED = "FAILED" +-} +- +-/** Status of a File that uses a common error model. */ +-export declare interface FileStatus { +- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ +- details?: Record[]; +- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ +- message?: string; +- /** The status code. 0 for OK, 1 for CANCELLED */ +- code?: number; +-} +- +-/** Output only. The reason why the model stopped generating tokens. +- +- If empty, the model has not stopped generating the tokens. +- */ +-export declare enum FinishReason { +- /** +- * The finish reason is unspecified. +- */ +- FINISH_REASON_UNSPECIFIED = "FINISH_REASON_UNSPECIFIED", +- /** +- * Token generation reached a natural stopping point or a configured stop sequence. +- */ +- STOP = "STOP", +- /** +- * Token generation reached the configured maximum output tokens. +- */ +- MAX_TOKENS = "MAX_TOKENS", +- /** +- * Token generation stopped because the content potentially contains safety violations. NOTE: When streaming, [content][] is empty if content filters blocks the output. +- */ +- SAFETY = "SAFETY", +- /** +- * The token generation stopped because of potential recitation. +- */ +- RECITATION = "RECITATION", +- /** +- * The token generation stopped because of using an unsupported language. +- */ +- LANGUAGE = "LANGUAGE", +- /** +- * All other reasons that stopped the token generation. +- */ +- OTHER = "OTHER", +- /** +- * Token generation stopped because the content contains forbidden terms. +- */ +- BLOCKLIST = "BLOCKLIST", +- /** +- * Token generation stopped for potentially containing prohibited content. +- */ +- PROHIBITED_CONTENT = "PROHIBITED_CONTENT", +- /** +- * Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII). +- */ +- SPII = "SPII", +- /** +- * The function call generated by the model is invalid. +- */ +- MALFORMED_FUNCTION_CALL = "MALFORMED_FUNCTION_CALL", +- /** +- * Token generation stopped because generated images have safety violations. +- */ +- IMAGE_SAFETY = "IMAGE_SAFETY" +-} +- +-/** A function call. */ +-export declare interface FunctionCall { +- /** The unique id of the function call. If populated, the client to execute the +- `function_call` and return the response with the matching `id`. */ +- id?: string; +- /** Optional. Required. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details. */ +- args?: Record; +- /** Required. The name of the function to call. Matches [FunctionDeclaration.name]. */ +- name?: string; +-} +- +-/** Function calling config. */ +-export declare interface FunctionCallingConfig { +- /** Optional. Function calling mode. */ +- mode?: FunctionCallingConfigMode; +- /** Optional. Function names to call. Only set when the Mode is ANY. Function names should match [FunctionDeclaration.name]. With mode set to ANY, model will predict a function call from the set of function names provided. */ +- allowedFunctionNames?: string[]; +-} +- +-/** Config for the function calling config mode. */ +-export declare enum FunctionCallingConfigMode { +- /** +- * The function calling config mode is unspecified. Should not be used. +- */ +- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", +- /** +- * Default model behavior, model decides to predict either function calls or natural language response. +- */ +- AUTO = "AUTO", +- /** +- * Model is constrained to always predicting function calls only. If "allowed_function_names" are set, the predicted function calls will be limited to any one of "allowed_function_names", else the predicted function calls will be any one of the provided "function_declarations". +- */ +- ANY = "ANY", +- /** +- * Model will not predict any function calls. Model behavior is same as when not passing any function declarations. +- */ +- NONE = "NONE" +-} +- +-/** Defines a function that the model can generate JSON inputs for. +- +- The inputs are based on `OpenAPI 3.0 specifications +- `_. +- */ +-export declare interface FunctionDeclaration { +- /** Defines the function behavior. */ +- behavior?: Behavior; +- /** Optional. Description and purpose of the function. Model uses it to decide how and whether to call the function. */ +- description?: string; +- /** Required. The name of the function to call. Must start with a letter or an underscore. Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a maximum length of 64. */ +- name?: string; +- /** Optional. Describes the parameters to this function in JSON Schema Object format. Reflects the Open API 3.03 Parameter Object. string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter. For function with no parameters, this can be left unset. Parameter names must start with a letter or an underscore and must only contain chars a-z, A-Z, 0-9, or underscores with a maximum length of 64. Example with 1 required and 1 optional parameter: type: OBJECT properties: param1: type: STRING param2: type: INTEGER required: - param1 */ +- parameters?: Schema; +- /** Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function. */ +- response?: Schema; +-} +- +-/** A function response. */ +-export declare class FunctionResponse { +- /** Signals that function call continues, and more responses will be returned, turning the function call into a generator. Is only applicable to NON_BLOCKING function calls (see FunctionDeclaration.behavior for details), ignored otherwise. If false, the default, future responses will not be considered. Is only applicable to NON_BLOCKING function calls, is ignored otherwise. If set to false, future responses will not be considered. It is allowed to return empty `response` with `will_continue=False` to signal that the function call is finished. */ +- willContinue?: boolean; +- /** Specifies how the response should be scheduled in the conversation. Only applicable to NON_BLOCKING function calls, is ignored otherwise. Defaults to WHEN_IDLE. */ +- scheduling?: FunctionResponseScheduling; +- /** Optional. The id of the function call this response is for. Populated by the client to match the corresponding function call `id`. */ +- id?: string; +- /** Required. The name of the function to call. Matches [FunctionDeclaration.name] and [FunctionCall.name]. */ +- name?: string; +- /** Required. The function response in JSON object format. Use "output" key to specify function output and "error" key to specify error details (if any). If "output" and "error" keys are not specified, then whole "response" is treated as function output. */ +- response?: Record; +-} +- +-/** Specifies how the response should be scheduled in the conversation. */ +-export declare enum FunctionResponseScheduling { +- /** +- * This value is unused. +- */ +- SCHEDULING_UNSPECIFIED = "SCHEDULING_UNSPECIFIED", +- /** +- * Only add the result to the conversation context, do not interrupt or trigger generation. +- */ +- SILENT = "SILENT", +- /** +- * Add the result to the conversation context, and prompt to generate output without interrupting ongoing generation. +- */ +- WHEN_IDLE = "WHEN_IDLE", +- /** +- * Add the result to the conversation context, interrupt ongoing generation and prompt to generate output. +- */ +- INTERRUPT = "INTERRUPT" +-} +- +-/** Optional model configuration parameters. +- +- For more information, see `Content generation parameters +- `_. +- */ +-export declare interface GenerateContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Instructions for the model to steer it toward better performance. +- For example, "Answer as concisely as possible" or "Don't use technical +- terms in your response". +- */ +- systemInstruction?: ContentUnion; +- /** Value that controls the degree of randomness in token selection. +- Lower temperatures are good for prompts that require a less open-ended or +- creative response, while higher temperatures can lead to more diverse or +- creative results. +- */ +- temperature?: number; +- /** Tokens are selected from the most to least probable until the sum +- of their probabilities equals this value. Use a lower value for less +- random responses and a higher value for more random responses. +- */ +- topP?: number; +- /** For each token selection step, the ``top_k`` tokens with the +- highest probabilities are sampled. Then tokens are further filtered based +- on ``top_p`` with the final token selected using temperature sampling. Use +- a lower number for less random responses and a higher number for more +- random responses. +- */ +- topK?: number; +- /** Number of response variations to return. +- */ +- candidateCount?: number; +- /** Maximum number of tokens that can be generated in the response. +- */ +- maxOutputTokens?: number; +- /** List of strings that tells the model to stop generating text if one +- of the strings is encountered in the response. +- */ +- stopSequences?: string[]; +- /** Whether to return the log probabilities of the tokens that were +- chosen by the model at each step. +- */ +- responseLogprobs?: boolean; +- /** Number of top candidate tokens to return the log probabilities for +- at each generation step. +- */ +- logprobs?: number; +- /** Positive values penalize tokens that already appear in the +- generated text, increasing the probability of generating more diverse +- content. +- */ +- presencePenalty?: number; +- /** Positive values penalize tokens that repeatedly appear in the +- generated text, increasing the probability of generating more diverse +- content. +- */ +- frequencyPenalty?: number; +- /** When ``seed`` is fixed to a specific number, the model makes a best +- effort to provide the same response for repeated requests. By default, a +- random number is used. +- */ +- seed?: number; +- /** Output response mimetype of the generated candidate text. +- Supported mimetype: +- - `text/plain`: (default) Text output. +- - `application/json`: JSON response in the candidates. +- The model needs to be prompted to output the appropriate response type, +- otherwise the behavior is undefined. +- This is a preview feature. +- */ +- responseMimeType?: string; +- /** The `Schema` object allows the definition of input and output data types. +- These types can be objects, but also primitives and arrays. +- Represents a select subset of an [OpenAPI 3.0 schema +- object](https://spec.openapis.org/oas/v3.0.3#schema). +- If set, a compatible response_mime_type must also be set. +- Compatible mimetypes: `application/json`: Schema for JSON response. +- */ +- responseSchema?: SchemaUnion; +- /** Configuration for model router requests. +- */ +- routingConfig?: GenerationConfigRoutingConfig; +- /** Configuration for model selection. +- */ +- modelSelectionConfig?: ModelSelectionConfig; +- /** Safety settings in the request to block unsafe content in the +- response. +- */ +- safetySettings?: SafetySetting[]; +- /** Code that enables the system to interact with external systems to +- perform an action outside of the knowledge and scope of the model. +- */ +- tools?: ToolListUnion; +- /** Associates model output to a specific function call. +- */ +- toolConfig?: ToolConfig; +- /** Labels with user-defined metadata to break down billed charges. */ +- labels?: Record; +- /** Resource name of a context cache that can be used in subsequent +- requests. +- */ +- cachedContent?: string; +- /** The requested modalities of the response. Represents the set of +- modalities that the model can return. +- */ +- responseModalities?: string[]; +- /** If specified, the media resolution specified will be used. +- */ +- mediaResolution?: MediaResolution; +- /** The speech generation configuration. +- */ +- speechConfig?: SpeechConfigUnion; +- /** If enabled, audio timestamp will be included in the request to the +- model. +- */ +- audioTimestamp?: boolean; +- /** The configuration for automatic function calling. +- */ +- automaticFunctionCalling?: AutomaticFunctionCallingConfig; +- /** The thinking features configuration. +- */ +- thinkingConfig?: ThinkingConfig; +-} +- +-/** Config for models.generate_content parameters. */ +-export declare interface GenerateContentParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** Content of the request. +- */ +- contents: ContentListUnion; +- /** Configuration that contains optional model parameters. +- */ +- config?: GenerateContentConfig; +-} +- +-/** Response message for PredictionService.GenerateContent. */ +-export declare class GenerateContentResponse { +- /** Response variations returned by the model. +- */ +- candidates?: Candidate[]; +- /** Timestamp when the request is made to the server. +- */ +- createTime?: string; +- /** Identifier for each response. +- */ +- responseId?: string; +- /** The history of automatic function calling. +- */ +- automaticFunctionCallingHistory?: Content[]; +- /** Output only. The model version used to generate the response. */ +- modelVersion?: string; +- /** Output only. Content filter results for a prompt sent in the request. Note: Sent only in the first stream chunk. Only happens when no candidates were generated due to content violations. */ +- promptFeedback?: GenerateContentResponsePromptFeedback; +- /** Usage metadata about the response(s). */ +- usageMetadata?: GenerateContentResponseUsageMetadata; +- /** +- * Returns the concatenation of all text parts from the first candidate in the response. +- * +- * @remarks +- * If there are multiple candidates in the response, the text from the first +- * one will be returned. +- * If there are non-text parts in the response, the concatenation of all text +- * parts will be returned, and a warning will be logged. +- * If there are thought parts in the response, the concatenation of all text +- * parts excluding the thought parts will be returned. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateContent({ +- * model: 'gemini-2.0-flash', +- * contents: +- * 'Why is the sky blue?', +- * }); +- * +- * console.debug(response.text); +- * ``` +- */ +- get text(): string | undefined; +- /** +- * Returns the concatenation of all inline data parts from the first candidate +- * in the response. +- * +- * @remarks +- * If there are multiple candidates in the response, the inline data from the +- * first one will be returned. If there are non-inline data parts in the +- * response, the concatenation of all inline data parts will be returned, and +- * a warning will be logged. +- */ +- get data(): string | undefined; +- /** +- * Returns the function calls from the first candidate in the response. +- * +- * @remarks +- * If there are multiple candidates in the response, the function calls from +- * the first one will be returned. +- * If there are no function calls in the response, undefined will be returned. +- * +- * @example +- * ```ts +- * const controlLightFunctionDeclaration: FunctionDeclaration = { +- * name: 'controlLight', +- * parameters: { +- * type: Type.OBJECT, +- * description: 'Set the brightness and color temperature of a room light.', +- * properties: { +- * brightness: { +- * type: Type.NUMBER, +- * description: +- * 'Light level from 0 to 100. Zero is off and 100 is full brightness.', +- * }, +- * colorTemperature: { +- * type: Type.STRING, +- * description: +- * 'Color temperature of the light fixture which can be `daylight`, `cool` or `warm`.', +- * }, +- * }, +- * required: ['brightness', 'colorTemperature'], +- * }; +- * const response = await ai.models.generateContent({ +- * model: 'gemini-2.0-flash', +- * contents: 'Dim the lights so the room feels cozy and warm.', +- * config: { +- * tools: [{functionDeclarations: [controlLightFunctionDeclaration]}], +- * toolConfig: { +- * functionCallingConfig: { +- * mode: FunctionCallingConfigMode.ANY, +- * allowedFunctionNames: ['controlLight'], +- * }, +- * }, +- * }, +- * }); +- * console.debug(JSON.stringify(response.functionCalls)); +- * ``` +- */ +- get functionCalls(): FunctionCall[] | undefined; +- /** +- * Returns the first executable code from the first candidate in the response. +- * +- * @remarks +- * If there are multiple candidates in the response, the executable code from +- * the first one will be returned. +- * If there are no executable code in the response, undefined will be +- * returned. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateContent({ +- * model: 'gemini-2.0-flash', +- * contents: +- * 'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.' +- * config: { +- * tools: [{codeExecution: {}}], +- * }, +- * }); +- * +- * console.debug(response.executableCode); +- * ``` +- */ +- get executableCode(): string | undefined; +- /** +- * Returns the first code execution result from the first candidate in the response. +- * +- * @remarks +- * If there are multiple candidates in the response, the code execution result from +- * the first one will be returned. +- * If there are no code execution result in the response, undefined will be returned. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateContent({ +- * model: 'gemini-2.0-flash', +- * contents: +- * 'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.' +- * config: { +- * tools: [{codeExecution: {}}], +- * }, +- * }); +- * +- * console.debug(response.codeExecutionResult); +- * ``` +- */ +- get codeExecutionResult(): string | undefined; +-} +- +-/** Content filter results for a prompt sent in the request. */ +-export declare class GenerateContentResponsePromptFeedback { +- /** Output only. Blocked reason. */ +- blockReason?: BlockedReason; +- /** Output only. A readable block reason message. */ +- blockReasonMessage?: string; +- /** Output only. Safety ratings. */ +- safetyRatings?: SafetyRating[]; +-} +- +-/** Usage metadata about response(s). */ +-export declare class GenerateContentResponseUsageMetadata { +- /** Output only. List of modalities of the cached content in the request input. */ +- cacheTokensDetails?: ModalityTokenCount[]; +- /** Output only. Number of tokens in the cached part in the input (the cached content). */ +- cachedContentTokenCount?: number; +- /** Number of tokens in the response(s). */ +- candidatesTokenCount?: number; +- /** Output only. List of modalities that were returned in the response. */ +- candidatesTokensDetails?: ModalityTokenCount[]; +- /** Number of tokens in the request. When `cached_content` is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content. */ +- promptTokenCount?: number; +- /** Output only. List of modalities that were processed in the request input. */ +- promptTokensDetails?: ModalityTokenCount[]; +- /** Output only. Number of tokens present in thoughts output. */ +- thoughtsTokenCount?: number; +- /** Output only. Number of tokens present in tool-use prompt(s). */ +- toolUsePromptTokenCount?: number; +- /** Output only. List of modalities that were processed for tool-use request inputs. */ +- toolUsePromptTokensDetails?: ModalityTokenCount[]; +- /** Total token count for prompt, response candidates, and tool-use prompts (if present). */ +- totalTokenCount?: number; +- /** Output only. Traffic type. This shows whether a request consumes Pay-As-You-Go or Provisioned Throughput quota. */ +- trafficType?: TrafficType; +-} +- +-/** An output image. */ +-export declare interface GeneratedImage { +- /** The output image data. +- */ +- image?: Image_2; +- /** Responsible AI filter reason if the image is filtered out of the +- response. +- */ +- raiFilteredReason?: string; +- /** Safety attributes of the image. Lists of RAI categories and their +- scores of each content. +- */ +- safetyAttributes?: SafetyAttributes; +- /** The rewritten prompt used for the image generation if the prompt +- enhancer is enabled. +- */ +- enhancedPrompt?: string; +-} +- +-/** A generated video. */ +-export declare interface GeneratedVideo { +- /** The output video */ +- video?: Video; +-} +- +-/** The config for generating an images. */ +-export declare interface GenerateImagesConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Cloud Storage URI used to store the generated images. +- */ +- outputGcsUri?: string; +- /** Description of what to discourage in the generated images. +- */ +- negativePrompt?: string; +- /** Number of images to generate. +- */ +- numberOfImages?: number; +- /** Aspect ratio of the generated images. +- */ +- aspectRatio?: string; +- /** Controls how much the model adheres to the text prompt. Large +- values increase output and prompt alignment, but may compromise image +- quality. +- */ +- guidanceScale?: number; +- /** Random seed for image generation. This is not available when +- ``add_watermark`` is set to true. +- */ +- seed?: number; +- /** Filter level for safety filtering. +- */ +- safetyFilterLevel?: SafetyFilterLevel; +- /** Allows generation of people by the model. +- */ +- personGeneration?: PersonGeneration; +- /** Whether to report the safety scores of each generated image and +- the positive prompt in the response. +- */ +- includeSafetyAttributes?: boolean; +- /** Whether to include the Responsible AI filter reason if the image +- is filtered out of the response. +- */ +- includeRaiReason?: boolean; +- /** Language of the text in the prompt. +- */ +- language?: ImagePromptLanguage; +- /** MIME type of the generated image. +- */ +- outputMimeType?: string; +- /** Compression quality of the generated image (for ``image/jpeg`` +- only). +- */ +- outputCompressionQuality?: number; +- /** Whether to add a watermark to the generated images. +- */ +- addWatermark?: boolean; +- /** Whether to use the prompt rewriting logic. +- */ +- enhancePrompt?: boolean; +-} +- +-/** The parameters for generating images. */ +-export declare interface GenerateImagesParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** Text prompt that typically describes the images to output. +- */ +- prompt: string; +- /** Configuration for generating images. +- */ +- config?: GenerateImagesConfig; +-} +- +-/** The output images response. */ +-export declare class GenerateImagesResponse { +- /** List of generated images. +- */ +- generatedImages?: GeneratedImage[]; +- /** Safety attributes of the positive prompt. Only populated if +- ``include_safety_attributes`` is set to True. +- */ +- positivePromptSafetyAttributes?: SafetyAttributes; +-} +- +-/** Configuration for generating videos. */ +-export declare interface GenerateVideosConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Number of output videos. */ +- numberOfVideos?: number; +- /** The gcs bucket where to save the generated videos. */ +- outputGcsUri?: string; +- /** Frames per second for video generation. */ +- fps?: number; +- /** Duration of the clip for video generation in seconds. */ +- durationSeconds?: number; +- /** The RNG seed. If RNG seed is exactly same for each request with unchanged inputs, the prediction results will be consistent. Otherwise, a random RNG seed will be used each time to produce a different result. */ +- seed?: number; +- /** The aspect ratio for the generated video. 16:9 (landscape) and 9:16 (portrait) are supported. */ +- aspectRatio?: string; +- /** The resolution for the generated video. 1280x720, 1920x1080 are supported. */ +- resolution?: string; +- /** Whether allow to generate person videos, and restrict to specific ages. Supported values are: dont_allow, allow_adult. */ +- personGeneration?: string; +- /** The pubsub topic where to publish the video generation progress. */ +- pubsubTopic?: string; +- /** Optional field in addition to the text content. Negative prompts can be explicitly stated here to help generate the video. */ +- negativePrompt?: string; +- /** Whether to use the prompt rewriting logic. */ +- enhancePrompt?: boolean; +-} +- +-/** A video generation operation. */ +-export declare interface GenerateVideosOperation { +- /** The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`. */ +- name?: string; +- /** Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any. */ +- metadata?: Record; +- /** If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available. */ +- done?: boolean; +- /** The error result of the operation in case of failure or cancellation. */ +- error?: Record; +- /** The generated videos. */ +- response?: GenerateVideosResponse; +-} +- +-/** Class that represents the parameters for generating an image. */ +-export declare interface GenerateVideosParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** The text prompt for generating the videos. Optional for image to video use cases. */ +- prompt?: string; +- /** The input image for generating the videos. +- Optional if prompt is provided. */ +- image?: Image_2; +- /** Configuration for generating videos. */ +- config?: GenerateVideosConfig; +-} +- +-/** Response with generated videos. */ +-export declare class GenerateVideosResponse { +- /** List of the generated videos */ +- generatedVideos?: GeneratedVideo[]; +- /** Returns if any videos were filtered due to RAI policies. */ +- raiMediaFilteredCount?: number; +- /** Returns rai failure reasons if any. */ +- raiMediaFilteredReasons?: string[]; +-} +- +-/** Generation config. */ +-export declare interface GenerationConfig { +- /** Optional. If enabled, audio timestamp will be included in the request to the model. */ +- audioTimestamp?: boolean; +- /** Optional. Number of candidates to generate. */ +- candidateCount?: number; +- /** Optional. Frequency penalties. */ +- frequencyPenalty?: number; +- /** Optional. Logit probabilities. */ +- logprobs?: number; +- /** Optional. The maximum number of output tokens to generate per message. */ +- maxOutputTokens?: number; +- /** Optional. If specified, the media resolution specified will be used. */ +- mediaResolution?: MediaResolution; +- /** Optional. Positive penalties. */ +- presencePenalty?: number; +- /** Optional. If true, export the logprobs results in response. */ +- responseLogprobs?: boolean; +- /** Optional. Output response mimetype of the generated candidate text. Supported mimetype: - `text/plain`: (default) Text output. - `application/json`: JSON response in the candidates. The model needs to be prompted to output the appropriate response type, otherwise the behavior is undefined. This is a preview feature. */ +- responseMimeType?: string; +- /** Optional. The `Schema` object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). If set, a compatible response_mime_type must also be set. Compatible mimetypes: `application/json`: Schema for JSON response. */ +- responseSchema?: Schema; +- /** Optional. Routing configuration. */ +- routingConfig?: GenerationConfigRoutingConfig; +- /** Optional. Seed. */ +- seed?: number; +- /** Optional. Stop sequences. */ +- stopSequences?: string[]; +- /** Optional. Controls the randomness of predictions. */ +- temperature?: number; +- /** Optional. If specified, top-k sampling will be used. */ +- topK?: number; +- /** Optional. If specified, nucleus sampling will be used. */ +- topP?: number; +-} +- +-/** The configuration for routing the request to a specific model. */ +-export declare interface GenerationConfigRoutingConfig { +- /** Automated routing. */ +- autoMode?: GenerationConfigRoutingConfigAutoRoutingMode; +- /** Manual routing. */ +- manualMode?: GenerationConfigRoutingConfigManualRoutingMode; +-} +- +-/** When automated routing is specified, the routing will be determined by the pretrained routing model and customer provided model routing preference. */ +-export declare interface GenerationConfigRoutingConfigAutoRoutingMode { +- /** The model routing preference. */ +- modelRoutingPreference?: 'UNKNOWN' | 'PRIORITIZE_QUALITY' | 'BALANCED' | 'PRIORITIZE_COST'; +-} +- +-/** When manual routing is set, the specified model will be used directly. */ +-export declare interface GenerationConfigRoutingConfigManualRoutingMode { +- /** The model name to use. Only the public LLM models are accepted. e.g. 'gemini-1.5-pro-001'. */ +- modelName?: string; +-} +- +-/** Optional parameters for caches.get method. */ +-export declare interface GetCachedContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for caches.get method. */ +-export declare interface GetCachedContentParameters { +- /** The server-generated resource name of the cached content. +- */ +- name: string; +- /** Optional parameters for the request. +- */ +- config?: GetCachedContentConfig; +-} +- +-/** Used to override the default configuration. */ +-export declare interface GetFileConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Generates the parameters for the get method. */ +-export declare interface GetFileParameters { +- /** The name identifier for the file to retrieve. */ +- name: string; +- /** Used to override the default configuration. */ +- config?: GetFileConfig; +-} +- +-/** Optional parameters for models.get method. */ +-export declare interface GetModelConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-export declare interface GetModelParameters { +- model: string; +- /** Optional parameters for the request. */ +- config?: GetModelConfig; +-} +- +-export declare interface GetOperationConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for the GET method. */ +-export declare interface GetOperationParameters { +- /** The server-assigned name for the operation. */ +- operationName: string; +- /** Used to override the default configuration. */ +- config?: GetOperationConfig; +-} +- +-/** Optional parameters for tunings.get method. */ +-export declare interface GetTuningJobConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** Parameters for the get method. */ +-export declare interface GetTuningJobParameters { +- name: string; +- /** Optional parameters for the request. */ +- config?: GetTuningJobConfig; +-} +- +-/** +- * The Google GenAI SDK. +- * +- * @remarks +- * Provides access to the GenAI features through either the {@link +- * https://cloud.google.com/vertex-ai/docs/reference/rest | Gemini API} or +- * the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI +- * API}. +- * +- * The {@link GoogleGenAIOptions.vertexai} value determines which of the API +- * services to use. +- * +- * When using the Gemini API, a {@link GoogleGenAIOptions.apiKey} must also be +- * set. When using Vertex AI, currently only {@link GoogleGenAIOptions.apiKey} +- * is supported via Express mode. {@link GoogleGenAIOptions.project} and {@link +- * GoogleGenAIOptions.location} should not be set. +- * +- * @example +- * Initializing the SDK for using the Gemini API: +- * ```ts +- * import {GoogleGenAI} from '@google/genai'; +- * const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'}); +- * ``` +- * +- * @example +- * Initializing the SDK for using the Vertex AI API: +- * ```ts +- * import {GoogleGenAI} from '@google/genai'; +- * const ai = new GoogleGenAI({ +- * vertexai: true, +- * project: 'PROJECT_ID', +- * location: 'PROJECT_LOCATION' +- * }); +- * ``` +- * +- */ +-export declare class GoogleGenAI { +- protected readonly apiClient: ApiClient; +- private readonly apiKey?; +- readonly vertexai: boolean; +- private readonly apiVersion?; +- readonly models: Models; +- readonly live: Live; +- readonly chats: Chats; +- readonly caches: Caches; +- readonly files: Files; +- readonly operations: Operations; +- readonly tunings: Tunings; +- constructor(options: GoogleGenAIOptions); +-} +- +-/** +- * Google Gen AI SDK's configuration options. +- * +- * See {@link GoogleGenAI} for usage samples. +- */ +-export declare interface GoogleGenAIOptions { +- /** +- * Optional. Determines whether to use the Vertex AI or the Gemini API. +- * +- * @remarks +- * When true, the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI API} will used. +- * When false, the {@link https://ai.google.dev/api | Gemini API} will be used. +- * +- * If unset, default SDK behavior is to use the Gemini API service. +- */ +- vertexai?: boolean; +- /** +- * Optional. The Google Cloud project ID for Vertex AI clients. +- * +- * Find your project ID: https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects +- * +- * @remarks +- * Only supported on Node runtimes, ignored on browser runtimes. +- */ +- project?: string; +- /** +- * Optional. The Google Cloud project {@link https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations | location} for Vertex AI clients. +- * +- * @remarks +- * Only supported on Node runtimes, ignored on browser runtimes. +- * +- */ +- location?: string; +- /** +- * The API Key, required for Gemini API clients. +- * +- * @remarks +- * Required on browser runtimes. +- */ +- apiKey?: string; +- /** +- * Optional. The API version to use. +- * +- * @remarks +- * If unset, the default API version will be used. +- */ +- apiVersion?: string; +- /** +- * Optional. Authentication options defined by the by google-auth-library for Vertex AI clients. +- * +- * @remarks +- * @see {@link https://github.com/googleapis/google-auth-library-nodejs/blob/v9.15.0/src/auth/googleauth.ts | GoogleAuthOptions interface in google-auth-library-nodejs}. +- * +- * Only supported on Node runtimes, ignored on browser runtimes. +- * +- */ +- googleAuthOptions?: GoogleAuthOptions; +- /** +- * Optional. A set of customizable configuration for HTTP requests. +- */ +- httpOptions?: HttpOptions; +-} +- +-/** Tool to support Google Maps in Model. */ +-export declare interface GoogleMaps { +- /** Optional. Auth config for the Google Maps tool. */ +- authConfig?: AuthConfig; +-} +- +-/** The `Status` type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by [gRPC](https://github.com/grpc). Each `Status` message contains three pieces of data: error code, error message, and error details. You can find out more about this error model and how to work with it in the [API Design Guide](https://cloud.google.com/apis/design/errors). */ +-export declare interface GoogleRpcStatus { +- /** The status code, which should be an enum value of google.rpc.Code. */ +- code?: number; +- /** A list of messages that carry the error details. There is a common set of message types for APIs to use. */ +- details?: Record[]; +- /** A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the google.rpc.Status.details field, or localized by the client. */ +- message?: string; +-} +- +-/** Tool to support Google Search in Model. Powered by Google. */ +-export declare interface GoogleSearch { +- /** Optional. Filter search results to a specific time range. +- If customers set a start time, they must set an end time (and vice versa). +- */ +- timeRangeFilter?: Interval; +-} +- +-/** Tool to retrieve public web data for grounding, powered by Google. */ +-export declare interface GoogleSearchRetrieval { +- /** Specifies the dynamic retrieval configuration for the given source. */ +- dynamicRetrievalConfig?: DynamicRetrievalConfig; +-} +- +-/** Represents a whole or partial calendar date, such as a birthday. The time of day and time zone are either specified elsewhere or are insignificant. The date is relative to the Gregorian Calendar. This can represent one of the following: * A full date, with non-zero year, month, and day values. * A month and day, with a zero year (for example, an anniversary). * A year on its own, with a zero month and a zero day. * A year and month, with a zero day (for example, a credit card expiration date). Related types: * google.type.TimeOfDay * google.type.DateTime * google.protobuf.Timestamp */ +-export declare interface GoogleTypeDate { +- /** Day of a month. Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant. */ +- day?: number; +- /** Month of a year. Must be from 1 to 12, or 0 to specify a year without a month and day. */ +- month?: number; +- /** Year of the date. Must be from 1 to 9999, or 0 to specify a date without a year. */ +- year?: number; +-} +- +-/** Grounding chunk. */ +-export declare interface GroundingChunk { +- /** Grounding chunk from context retrieved by the retrieval tools. */ +- retrievedContext?: GroundingChunkRetrievedContext; +- /** Grounding chunk from the web. */ +- web?: GroundingChunkWeb; +-} +- +-/** Chunk from context retrieved by the retrieval tools. */ +-export declare interface GroundingChunkRetrievedContext { +- /** Text of the attribution. */ +- text?: string; +- /** Title of the attribution. */ +- title?: string; +- /** URI reference of the attribution. */ +- uri?: string; +-} +- +-/** Chunk from the web. */ +-export declare interface GroundingChunkWeb { +- /** Domain of the (original) URI. */ +- domain?: string; +- /** Title of the chunk. */ +- title?: string; +- /** URI reference of the chunk. */ +- uri?: string; +-} +- +-/** Metadata returned to client when grounding is enabled. */ +-export declare interface GroundingMetadata { +- /** List of supporting references retrieved from specified grounding source. */ +- groundingChunks?: GroundingChunk[]; +- /** Optional. List of grounding support. */ +- groundingSupports?: GroundingSupport[]; +- /** Optional. Output only. Retrieval metadata. */ +- retrievalMetadata?: RetrievalMetadata; +- /** Optional. Queries executed by the retrieval tools. */ +- retrievalQueries?: string[]; +- /** Optional. Google search entry for the following-up web searches. */ +- searchEntryPoint?: SearchEntryPoint; +- /** Optional. Web search queries for the following-up web search. */ +- webSearchQueries?: string[]; +-} +- +-/** Grounding support. */ +-export declare interface GroundingSupport { +- /** Confidence score of the support references. Ranges from 0 to 1. 1 is the most confident. This list must have the same size as the grounding_chunk_indices. */ +- confidenceScores?: number[]; +- /** A list of indices (into 'grounding_chunk') specifying the citations associated with the claim. For instance [1,3,4] means that grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] are the retrieved content attributed to the claim. */ +- groundingChunkIndices?: number[]; +- /** Segment of the content this support belongs to. */ +- segment?: Segment; +-} +- +-/** Optional. Specify if the threshold is used for probability or severity score. If not specified, the threshold is used for probability score. */ +-export declare enum HarmBlockMethod { +- /** +- * The harm block method is unspecified. +- */ +- HARM_BLOCK_METHOD_UNSPECIFIED = "HARM_BLOCK_METHOD_UNSPECIFIED", +- /** +- * The harm block method uses both probability and severity scores. +- */ +- SEVERITY = "SEVERITY", +- /** +- * The harm block method uses the probability score. +- */ +- PROBABILITY = "PROBABILITY" +-} +- +-/** Required. The harm block threshold. */ +-export declare enum HarmBlockThreshold { +- /** +- * Unspecified harm block threshold. +- */ +- HARM_BLOCK_THRESHOLD_UNSPECIFIED = "HARM_BLOCK_THRESHOLD_UNSPECIFIED", +- /** +- * Block low threshold and above (i.e. block more). +- */ +- BLOCK_LOW_AND_ABOVE = "BLOCK_LOW_AND_ABOVE", +- /** +- * Block medium threshold and above. +- */ +- BLOCK_MEDIUM_AND_ABOVE = "BLOCK_MEDIUM_AND_ABOVE", +- /** +- * Block only high threshold (i.e. block less). +- */ +- BLOCK_ONLY_HIGH = "BLOCK_ONLY_HIGH", +- /** +- * Block none. +- */ +- BLOCK_NONE = "BLOCK_NONE", +- /** +- * Turn off the safety filter. +- */ +- OFF = "OFF" +-} +- +-/** Required. Harm category. */ +-export declare enum HarmCategory { +- /** +- * The harm category is unspecified. +- */ +- HARM_CATEGORY_UNSPECIFIED = "HARM_CATEGORY_UNSPECIFIED", +- /** +- * The harm category is hate speech. +- */ +- HARM_CATEGORY_HATE_SPEECH = "HARM_CATEGORY_HATE_SPEECH", +- /** +- * The harm category is dangerous content. +- */ +- HARM_CATEGORY_DANGEROUS_CONTENT = "HARM_CATEGORY_DANGEROUS_CONTENT", +- /** +- * The harm category is harassment. +- */ +- HARM_CATEGORY_HARASSMENT = "HARM_CATEGORY_HARASSMENT", +- /** +- * The harm category is sexually explicit content. +- */ +- HARM_CATEGORY_SEXUALLY_EXPLICIT = "HARM_CATEGORY_SEXUALLY_EXPLICIT", +- /** +- * The harm category is civic integrity. +- */ +- HARM_CATEGORY_CIVIC_INTEGRITY = "HARM_CATEGORY_CIVIC_INTEGRITY" +-} +- +-/** Output only. Harm probability levels in the content. */ +-export declare enum HarmProbability { +- /** +- * Harm probability unspecified. +- */ +- HARM_PROBABILITY_UNSPECIFIED = "HARM_PROBABILITY_UNSPECIFIED", +- /** +- * Negligible level of harm. +- */ +- NEGLIGIBLE = "NEGLIGIBLE", +- /** +- * Low level of harm. +- */ +- LOW = "LOW", +- /** +- * Medium level of harm. +- */ +- MEDIUM = "MEDIUM", +- /** +- * High level of harm. +- */ +- HIGH = "HIGH" +-} +- +-/** Output only. Harm severity levels in the content. */ +-export declare enum HarmSeverity { +- /** +- * Harm severity unspecified. +- */ +- HARM_SEVERITY_UNSPECIFIED = "HARM_SEVERITY_UNSPECIFIED", +- /** +- * Negligible level of harm severity. +- */ +- HARM_SEVERITY_NEGLIGIBLE = "HARM_SEVERITY_NEGLIGIBLE", +- /** +- * Low level of harm severity. +- */ +- HARM_SEVERITY_LOW = "HARM_SEVERITY_LOW", +- /** +- * Medium level of harm severity. +- */ +- HARM_SEVERITY_MEDIUM = "HARM_SEVERITY_MEDIUM", +- /** +- * High level of harm severity. +- */ +- HARM_SEVERITY_HIGH = "HARM_SEVERITY_HIGH" +-} +- +-/** HTTP options to be used in each of the requests. */ +-export declare interface HttpOptions { +- /** The base URL for the AI platform service endpoint. */ +- baseUrl?: string; +- /** Specifies the version of the API to use. */ +- apiVersion?: string; +- /** Additional HTTP headers to be sent with the request. */ +- headers?: Record; +- /** Timeout for the request in milliseconds. */ +- timeout?: number; +-} +- +-/** +- * Represents the necessary information to send a request to an API endpoint. +- * This interface defines the structure for constructing and executing HTTP +- * requests. +- */ +-declare interface HttpRequest { +- /** +- * URL path from the modules, this path is appended to the base API URL to +- * form the complete request URL. +- * +- * If you wish to set full URL, use httpOptions.baseUrl instead. Example to +- * set full URL in the request: +- * +- * const request: HttpRequest = { +- * path: '', +- * httpOptions: { +- * baseUrl: 'https://', +- * apiVersion: '', +- * }, +- * httpMethod: 'GET', +- * }; +- * +- * The result URL will be: https:// +- * +- */ +- path: string; +- /** +- * Optional query parameters to be appended to the request URL. +- */ +- queryParams?: Record; +- /** +- * Optional request body in json string or Blob format, GET request doesn't +- * need a request body. +- */ +- body?: string | Blob; +- /** +- * The HTTP method to be used for the request. +- */ +- httpMethod: 'GET' | 'POST' | 'PATCH' | 'DELETE'; +- /** +- * Optional set of customizable configuration for HTTP requests. +- */ +- httpOptions?: HttpOptions; +- /** +- * Optional abort signal which can be used to cancel the request. +- */ +- abortSignal?: AbortSignal; +-} +- +-/** A wrapper class for the http response. */ +-export declare class HttpResponse { +- /** Used to retain the processed HTTP headers in the response. */ +- headers?: Record; +- /** +- * The original http response. +- */ +- responseInternal: Response; +- constructor(response: Response); +- json(): Promise; +-} +- +-/** An image. */ +-declare interface Image_2 { +- /** The Cloud Storage URI of the image. ``Image`` can contain a value +- for this field or the ``image_bytes`` field but not both. +- */ +- gcsUri?: string; +- /** The image bytes data. ``Image`` can contain a value for this field +- or the ``gcs_uri`` field but not both. +- */ +- imageBytes?: string; +- /** The MIME type of the image. */ +- mimeType?: string; +-} +-export { Image_2 as Image } +- +-/** Enum that specifies the language of the text in the prompt. */ +-export declare enum ImagePromptLanguage { +- auto = "auto", +- en = "en", +- ja = "ja", +- ko = "ko", +- hi = "hi" +-} +- +-/** Represents a time interval, encoded as a start time (inclusive) and an end time (exclusive). +- +- The start time must be less than or equal to the end time. +- When the start equals the end time, the interval is an empty interval. +- (matches no time) +- When both start and end are unspecified, the interval matches any time. +- */ +-export declare interface Interval { +- /** The start time of the interval. */ +- startTime?: string; +- /** The end time of the interval. */ +- endTime?: string; +-} +- +-/** Output only. The detailed state of the job. */ +-export declare enum JobState { +- /** +- * The job state is unspecified. +- */ +- JOB_STATE_UNSPECIFIED = "JOB_STATE_UNSPECIFIED", +- /** +- * The job has been just created or resumed and processing has not yet begun. +- */ +- JOB_STATE_QUEUED = "JOB_STATE_QUEUED", +- /** +- * The service is preparing to run the job. +- */ +- JOB_STATE_PENDING = "JOB_STATE_PENDING", +- /** +- * The job is in progress. +- */ +- JOB_STATE_RUNNING = "JOB_STATE_RUNNING", +- /** +- * The job completed successfully. +- */ +- JOB_STATE_SUCCEEDED = "JOB_STATE_SUCCEEDED", +- /** +- * The job failed. +- */ +- JOB_STATE_FAILED = "JOB_STATE_FAILED", +- /** +- * The job is being cancelled. From this state the job may only go to either `JOB_STATE_SUCCEEDED`, `JOB_STATE_FAILED` or `JOB_STATE_CANCELLED`. +- */ +- JOB_STATE_CANCELLING = "JOB_STATE_CANCELLING", +- /** +- * The job has been cancelled. +- */ +- JOB_STATE_CANCELLED = "JOB_STATE_CANCELLED", +- /** +- * The job has been stopped, and can be resumed. +- */ +- JOB_STATE_PAUSED = "JOB_STATE_PAUSED", +- /** +- * The job has expired. +- */ +- JOB_STATE_EXPIRED = "JOB_STATE_EXPIRED", +- /** +- * The job is being updated. Only jobs in the `RUNNING` state can be updated. After updating, the job goes back to the `RUNNING` state. +- */ +- JOB_STATE_UPDATING = "JOB_STATE_UPDATING", +- /** +- * The job is partially succeeded, some results may be missing due to errors. +- */ +- JOB_STATE_PARTIALLY_SUCCEEDED = "JOB_STATE_PARTIALLY_SUCCEEDED" +-} +- +-/** Required. Programming language of the `code`. */ +-export declare enum Language { +- /** +- * Unspecified language. This value should not be used. +- */ +- LANGUAGE_UNSPECIFIED = "LANGUAGE_UNSPECIFIED", +- /** +- * Python >= 3.10, with numpy and simpy available. +- */ +- PYTHON = "PYTHON" +-} +- +-/** An object that represents a latitude/longitude pair. +- +- This is expressed as a pair of doubles to represent degrees latitude and +- degrees longitude. Unless specified otherwise, this object must conform to the +- +- WGS84 standard. Values must be within normalized ranges. +- */ +-export declare interface LatLng { +- /** The latitude in degrees. It must be in the range [-90.0, +90.0]. */ +- latitude?: number; +- /** The longitude in degrees. It must be in the range [-180.0, +180.0] */ +- longitude?: number; +-} +- +-/** Config for caches.list method. */ +-export declare interface ListCachedContentsConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- pageSize?: number; +- pageToken?: string; +-} +- +-/** Parameters for caches.list method. */ +-export declare interface ListCachedContentsParameters { +- /** Configuration that contains optional parameters. +- */ +- config?: ListCachedContentsConfig; +-} +- +-export declare class ListCachedContentsResponse { +- nextPageToken?: string; +- /** List of cached contents. +- */ +- cachedContents?: CachedContent[]; +-} +- +-/** Used to override the default configuration. */ +-export declare interface ListFilesConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- pageSize?: number; +- pageToken?: string; +-} +- +-/** Generates the parameters for the list method. */ +-export declare interface ListFilesParameters { +- /** Used to override the default configuration. */ +- config?: ListFilesConfig; +-} +- +-/** Response for the list files method. */ +-export declare class ListFilesResponse { +- /** A token to retrieve next page of results. */ +- nextPageToken?: string; +- /** The list of files. */ +- files?: File_2[]; +-} +- +-export declare interface ListModelsConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- pageSize?: number; +- pageToken?: string; +- filter?: string; +- /** Set true to list base models, false to list tuned models. */ +- queryBase?: boolean; +-} +- +-export declare interface ListModelsParameters { +- config?: ListModelsConfig; +-} +- +-export declare class ListModelsResponse { +- nextPageToken?: string; +- models?: Model[]; +-} +- +-/** Configuration for the list tuning jobs method. */ +-export declare interface ListTuningJobsConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- pageSize?: number; +- pageToken?: string; +- filter?: string; +-} +- +-/** Parameters for the list tuning jobs method. */ +-export declare interface ListTuningJobsParameters { +- config?: ListTuningJobsConfig; +-} +- +-/** Response for the list tuning jobs method. */ +-export declare class ListTuningJobsResponse { +- /** A token to retrieve the next page of results. Pass to ListTuningJobsRequest.page_token to obtain that page. */ +- nextPageToken?: string; +- /** List of TuningJobs in the requested page. */ +- tuningJobs?: TuningJob[]; +-} +- +-/** +- Live class encapsulates the configuration for live interaction with the +- Generative Language API. It embeds ApiClient for general API settings. +- +- @experimental +- */ +-export declare class Live { +- private readonly apiClient; +- private readonly auth; +- private readonly webSocketFactory; +- readonly music: LiveMusic; +- constructor(apiClient: ApiClient, auth: Auth, webSocketFactory: WebSocketFactory); +- /** +- Establishes a connection to the specified model with the given +- configuration and returns a Session object representing that connection. +- +- @experimental Built-in MCP support is an experimental feature, may change in +- future versions. +- +- @remarks +- +- @param params - The parameters for establishing a connection to the model. +- @return A live session. +- +- @example +- ```ts +- let model: string; +- if (GOOGLE_GENAI_USE_VERTEXAI) { +- model = 'gemini-2.0-flash-live-preview-04-09'; +- } else { +- model = 'gemini-2.0-flash-live-001'; +- } +- const session = await ai.live.connect({ +- model: model, +- config: { +- responseModalities: [Modality.AUDIO], +- }, +- callbacks: { +- onopen: () => { +- console.log('Connected to the socket.'); +- }, +- onmessage: (e: MessageEvent) => { +- console.log('Received message from the server: %s\n', debug(e.data)); +- }, +- onerror: (e: ErrorEvent) => { +- console.log('Error occurred: %s\n', debug(e.error)); +- }, +- onclose: (e: CloseEvent) => { +- console.log('Connection closed.'); +- }, +- }, +- }); +- ``` +- */ +- connect(params: types.LiveConnectParameters): Promise; +- private isCallableTool; +-} +- +-/** Callbacks for the live API. */ +-export declare interface LiveCallbacks { +- /** +- * Called when the websocket connection is established. +- */ +- onopen?: (() => void) | null; +- /** +- * Called when a message is received from the server. +- */ +- onmessage: (e: LiveServerMessage) => void; +- /** +- * Called when an error occurs. +- */ +- onerror?: ((e: ErrorEvent) => void) | null; +- /** +- * Called when the websocket connection is closed. +- */ +- onclose?: ((e: CloseEvent) => void) | null; +-} +- +-/** Incremental update of the current conversation delivered from the client. +- +- All the content here will unconditionally be appended to the conversation +- history and used as part of the prompt to the model to generate content. +- +- A message here will interrupt any current model generation. +- */ +-export declare interface LiveClientContent { +- /** The content appended to the current conversation with the model. +- +- For single-turn queries, this is a single instance. For multi-turn +- queries, this is a repeated field that contains conversation history and +- latest request. +- */ +- turns?: Content[]; +- /** If true, indicates that the server content generation should start with +- the currently accumulated prompt. Otherwise, the server will await +- additional messages before starting generation. */ +- turnComplete?: boolean; +-} +- +-/** Messages sent by the client in the API call. */ +-export declare interface LiveClientMessage { +- /** Message to be sent by the system when connecting to the API. SDK users should not send this message. */ +- setup?: LiveClientSetup; +- /** Incremental update of the current conversation delivered from the client. */ +- clientContent?: LiveClientContent; +- /** User input that is sent in real time. */ +- realtimeInput?: LiveClientRealtimeInput; +- /** Response to a `ToolCallMessage` received from the server. */ +- toolResponse?: LiveClientToolResponse; +-} +- +-/** User input that is sent in real time. +- +- This is different from `LiveClientContent` in a few ways: +- +- - Can be sent continuously without interruption to model generation. +- - If there is a need to mix data interleaved across the +- `LiveClientContent` and the `LiveClientRealtimeInput`, server attempts to +- optimize for best response, but there are no guarantees. +- - End of turn is not explicitly specified, but is rather derived from user +- activity (for example, end of speech). +- - Even before the end of turn, the data is processed incrementally +- to optimize for a fast start of the response from the model. +- - Is always assumed to be the user's input (cannot be used to populate +- conversation history). +- */ +-export declare interface LiveClientRealtimeInput { +- /** Inlined bytes data for media input. */ +- mediaChunks?: Blob_2[]; +- /** The realtime audio input stream. */ +- audio?: Blob_2; +- /** +- Indicates that the audio stream has ended, e.g. because the microphone was +- turned off. +- +- This should only be sent when automatic activity detection is enabled +- (which is the default). +- +- The client can reopen the stream by sending an audio message. +- */ +- audioStreamEnd?: boolean; +- /** The realtime video input stream. */ +- video?: Blob_2; +- /** The realtime text input stream. */ +- text?: string; +- /** Marks the start of user activity. */ +- activityStart?: ActivityStart; +- /** Marks the end of user activity. */ +- activityEnd?: ActivityEnd; +-} +- +-/** Message contains configuration that will apply for the duration of the streaming session. */ +-export declare interface LiveClientSetup { +- /** +- The fully qualified name of the publisher model or tuned model endpoint to +- use. +- */ +- model?: string; +- /** The generation configuration for the session. +- Note: only a subset of fields are supported. +- */ +- generationConfig?: GenerationConfig; +- /** The user provided system instructions for the model. +- Note: only text should be used in parts and content in each part will be +- in a separate paragraph. */ +- systemInstruction?: ContentUnion; +- /** A list of `Tools` the model may use to generate the next response. +- +- A `Tool` is a piece of code that enables the system to interact with +- external systems to perform an action, or set of actions, outside of +- knowledge and scope of the model. */ +- tools?: ToolListUnion; +- /** Configures the realtime input behavior in BidiGenerateContent. */ +- realtimeInputConfig?: RealtimeInputConfig; +- /** Configures session resumption mechanism. +- +- If included server will send SessionResumptionUpdate messages. */ +- sessionResumption?: SessionResumptionConfig; +- /** Configures context window compression mechanism. +- +- If included, server will compress context window to fit into given length. */ +- contextWindowCompression?: ContextWindowCompressionConfig; +- /** The transcription of the input aligns with the input audio language. +- */ +- inputAudioTranscription?: AudioTranscriptionConfig; +- /** The transcription of the output aligns with the language code +- specified for the output audio. +- */ +- outputAudioTranscription?: AudioTranscriptionConfig; +- /** Configures the proactivity of the model. This allows the model to respond proactively to +- the input and to ignore irrelevant input. */ +- proactivity?: ProactivityConfig; +-} +- +-/** Client generated response to a `ToolCall` received from the server. +- +- Individual `FunctionResponse` objects are matched to the respective +- `FunctionCall` objects by the `id` field. +- +- Note that in the unary and server-streaming GenerateContent APIs function +- calling happens by exchanging the `Content` parts, while in the bidi +- GenerateContent APIs function calling happens over this dedicated set of +- messages. +- */ +-export declare class LiveClientToolResponse { +- /** The response to the function calls. */ +- functionResponses?: FunctionResponse[]; +-} +- +-/** Session config for the API connection. */ +-export declare interface LiveConnectConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** The generation configuration for the session. */ +- generationConfig?: GenerationConfig; +- /** The requested modalities of the response. Represents the set of +- modalities that the model can return. Defaults to AUDIO if not specified. +- */ +- responseModalities?: Modality[]; +- /** Value that controls the degree of randomness in token selection. +- Lower temperatures are good for prompts that require a less open-ended or +- creative response, while higher temperatures can lead to more diverse or +- creative results. +- */ +- temperature?: number; +- /** Tokens are selected from the most to least probable until the sum +- of their probabilities equals this value. Use a lower value for less +- random responses and a higher value for more random responses. +- */ +- topP?: number; +- /** For each token selection step, the ``top_k`` tokens with the +- highest probabilities are sampled. Then tokens are further filtered based +- on ``top_p`` with the final token selected using temperature sampling. Use +- a lower number for less random responses and a higher number for more +- random responses. +- */ +- topK?: number; +- /** Maximum number of tokens that can be generated in the response. +- */ +- maxOutputTokens?: number; +- /** If specified, the media resolution specified will be used. +- */ +- mediaResolution?: MediaResolution; +- /** When ``seed`` is fixed to a specific number, the model makes a best +- effort to provide the same response for repeated requests. By default, a +- random number is used. +- */ +- seed?: number; +- /** The speech generation configuration. +- */ +- speechConfig?: SpeechConfig; +- /** If enabled, the model will detect emotions and adapt its responses accordingly. */ +- enableAffectiveDialog?: boolean; +- /** The user provided system instructions for the model. +- Note: only text should be used in parts and content in each part will be +- in a separate paragraph. */ +- systemInstruction?: ContentUnion; +- /** A list of `Tools` the model may use to generate the next response. +- +- A `Tool` is a piece of code that enables the system to interact with +- external systems to perform an action, or set of actions, outside of +- knowledge and scope of the model. */ +- tools?: ToolListUnion; +- /** Configures session resumption mechanism. +- +- If included the server will send SessionResumptionUpdate messages. */ +- sessionResumption?: SessionResumptionConfig; +- /** The transcription of the input aligns with the input audio language. +- */ +- inputAudioTranscription?: AudioTranscriptionConfig; +- /** The transcription of the output aligns with the language code +- specified for the output audio. +- */ +- outputAudioTranscription?: AudioTranscriptionConfig; +- /** Configures the realtime input behavior in BidiGenerateContent. */ +- realtimeInputConfig?: RealtimeInputConfig; +- /** Configures context window compression mechanism. +- +- If included, server will compress context window to fit into given length. */ +- contextWindowCompression?: ContextWindowCompressionConfig; +- /** Configures the proactivity of the model. This allows the model to respond proactively to +- the input and to ignore irrelevant input. */ +- proactivity?: ProactivityConfig; +-} +- +-/** Parameters for connecting to the live API. */ +-export declare interface LiveConnectParameters { +- /** ID of the model to use. For a list of models, see `Google models +- `_. */ +- model: string; +- /** callbacks */ +- callbacks: LiveCallbacks; +- /** Optional configuration parameters for the request. +- */ +- config?: LiveConnectConfig; +-} +- +-/** Config for LiveEphemeralParameters for Auth Token creation. */ +-export declare interface LiveEphemeralParameters { +- /** ID of the model to configure in the ephemeral token for Live API. +- For a list of models, see `Gemini models +- `. */ +- model?: string; +- /** Configuration specific to Live API connections created using this token. */ +- config?: LiveConnectConfig; +-} +- +-/** +- LiveMusic class encapsulates the configuration for live music +- generation via Lyria Live models. +- +- @experimental +- */ +-declare class LiveMusic { +- private readonly apiClient; +- private readonly auth; +- private readonly webSocketFactory; +- constructor(apiClient: ApiClient, auth: Auth, webSocketFactory: WebSocketFactory); +- /** +- Establishes a connection to the specified model and returns a +- LiveMusicSession object representing that connection. +- +- @experimental +- +- @remarks +- +- @param params - The parameters for establishing a connection to the model. +- @return A live session. +- +- @example +- ```ts +- let model = 'models/lyria-realtime-exp'; +- const session = await ai.live.music.connect({ +- model: model, +- callbacks: { +- onmessage: (e: MessageEvent) => { +- console.log('Received message from the server: %s\n', debug(e.data)); +- }, +- onerror: (e: ErrorEvent) => { +- console.log('Error occurred: %s\n', debug(e.error)); +- }, +- onclose: (e: CloseEvent) => { +- console.log('Connection closed.'); +- }, +- }, +- }); +- ``` +- */ +- connect(params: types.LiveMusicConnectParameters): Promise; +-} +- +-/** Callbacks for the realtime music API. */ +-export declare interface LiveMusicCallbacks { +- /** +- * Called when a message is received from the server. +- */ +- onmessage: (e: LiveMusicServerMessage) => void; +- /** +- * Called when an error occurs. +- */ +- onerror?: ((e: ErrorEvent) => void) | null; +- /** +- * Called when the websocket connection is closed. +- */ +- onclose?: ((e: CloseEvent) => void) | null; +-} +- +-/** User input to start or steer the music. */ +-export declare interface LiveMusicClientContent { +- /** Weighted prompts as the model input. */ +- weightedPrompts?: WeightedPrompt[]; +-} +- +-/** Messages sent by the client in the LiveMusicClientMessage call. */ +-export declare interface LiveMusicClientMessage { +- /** Message to be sent in the first (and only in the first) `LiveMusicClientMessage`. +- Clients should wait for a `LiveMusicSetupComplete` message before +- sending any additional messages. */ +- setup?: LiveMusicClientSetup; +- /** User input to influence music generation. */ +- clientContent?: LiveMusicClientContent; +- /** Configuration for music generation. */ +- musicGenerationConfig?: LiveMusicGenerationConfig; +- /** Playback control signal for the music generation. */ +- playbackControl?: LiveMusicPlaybackControl; +-} +- +-/** Message to be sent by the system when connecting to the API. */ +-export declare interface LiveMusicClientSetup { +- /** The model's resource name. Format: `models/{model}`. */ +- model?: string; +-} +- +-/** Parameters for connecting to the live API. */ +-export declare interface LiveMusicConnectParameters { +- /** The model's resource name. */ +- model: string; +- /** Callbacks invoked on server events. */ +- callbacks: LiveMusicCallbacks; +-} +- +-/** A prompt that was filtered with the reason. */ +-export declare interface LiveMusicFilteredPrompt { +- /** The text prompt that was filtered. */ +- text?: string; +- /** The reason the prompt was filtered. */ +- filteredReason?: string; +-} +- +-/** Configuration for music generation. */ +-export declare interface LiveMusicGenerationConfig { +- /** Controls the variance in audio generation. Higher values produce +- higher variance. Range is [0.0, 3.0]. */ +- temperature?: number; +- /** Controls how the model selects tokens for output. Samples the topK +- tokens with the highest probabilities. Range is [1, 1000]. */ +- topK?: number; +- /** Seeds audio generation. If not set, the request uses a randomly +- generated seed. */ +- seed?: number; +- /** Controls how closely the model follows prompts. +- Higher guidance follows more closely, but will make transitions more +- abrupt. Range is [0.0, 6.0]. */ +- guidance?: number; +- /** Beats per minute. Range is [60, 200]. */ +- bpm?: number; +- /** Density of sounds. Range is [0.0, 1.0]. */ +- density?: number; +- /** Brightness of the music. Range is [0.0, 1.0]. */ +- brightness?: number; +- /** Scale of the generated music. */ +- scale?: Scale; +- /** Whether the audio output should contain bass. */ +- muteBass?: boolean; +- /** Whether the audio output should contain drums. */ +- muteDrums?: boolean; +- /** Whether the audio output should contain only bass and drums. */ +- onlyBassAndDrums?: boolean; +- /** The mode of music generation. Default mode is QUALITY. */ +- musicGenerationMode?: MusicGenerationMode; +-} +- +-/** The playback control signal to apply to the music generation. */ +-export declare enum LiveMusicPlaybackControl { +- /** +- * This value is unused. +- */ +- PLAYBACK_CONTROL_UNSPECIFIED = "PLAYBACK_CONTROL_UNSPECIFIED", +- /** +- * Start generating the music. +- */ +- PLAY = "PLAY", +- /** +- * Hold the music generation. Use PLAY to resume from the current position. +- */ +- PAUSE = "PAUSE", +- /** +- * Stop the music generation and reset the context (prompts retained). +- Use PLAY to restart the music generation. +- */ +- STOP = "STOP", +- /** +- * Reset the context of the music generation without stopping it. +- Retains the current prompts and config. +- */ +- RESET_CONTEXT = "RESET_CONTEXT" +-} +- +-/** Server update generated by the model in response to client messages. +- +- Content is generated as quickly as possible, and not in real time. +- Clients may choose to buffer and play it out in real time. +- */ +-export declare interface LiveMusicServerContent { +- /** The audio chunks that the model has generated. */ +- audioChunks?: AudioChunk[]; +-} +- +-/** Response message for the LiveMusicClientMessage call. */ +-export declare class LiveMusicServerMessage { +- /** Message sent in response to a `LiveMusicClientSetup` message from the client. +- Clients should wait for this message before sending any additional messages. */ +- setupComplete?: LiveMusicServerSetupComplete; +- /** Content generated by the model in response to client messages. */ +- serverContent?: LiveMusicServerContent; +- /** A prompt that was filtered with the reason. */ +- filteredPrompt?: LiveMusicFilteredPrompt; +- /** +- * Returns the first audio chunk from the server content, if present. +- * +- * @remarks +- * If there are no audio chunks in the response, undefined will be returned. +- */ +- get audioChunk(): AudioChunk | undefined; +-} +- +-/** Sent in response to a `LiveMusicClientSetup` message from the client. */ +-export declare interface LiveMusicServerSetupComplete { +-} +- +-/** +- Represents a connection to the API. +- +- @experimental +- */ +-export declare class LiveMusicSession { +- readonly conn: WebSocket_2; +- private readonly apiClient; +- constructor(conn: WebSocket_2, apiClient: ApiClient); +- /** +- Sets inputs to steer music generation. Updates the session's current +- weighted prompts. +- +- @param params - Contains one property, `weightedPrompts`. +- +- - `weightedPrompts` to send to the model; weights are normalized to +- sum to 1.0. +- +- @experimental +- */ +- setWeightedPrompts(params: types.LiveMusicSetWeightedPromptsParameters): Promise; +- /** +- Sets a configuration to the model. Updates the session's current +- music generation config. +- +- @param params - Contains one property, `musicGenerationConfig`. +- +- - `musicGenerationConfig` to set in the model. Passing an empty or +- undefined config to the model will reset the config to defaults. +- +- @experimental +- */ +- setMusicGenerationConfig(params: types.LiveMusicSetConfigParameters): Promise; +- private sendPlaybackControl; +- /** +- * Start the music stream. +- * +- * @experimental +- */ +- play(): void; +- /** +- * Temporarily halt the music stream. Use `play` to resume from the current +- * position. +- * +- * @experimental +- */ +- pause(): void; +- /** +- * Stop the music stream and reset the state. Retains the current prompts +- * and config. +- * +- * @experimental +- */ +- stop(): void; +- /** +- * Resets the context of the music generation without stopping it. +- * Retains the current prompts and config. +- * +- * @experimental +- */ +- resetContext(): void; +- /** +- Terminates the WebSocket connection. +- +- @experimental +- */ +- close(): void; +-} +- +-/** Parameters for setting config for the live music API. */ +-export declare interface LiveMusicSetConfigParameters { +- /** Configuration for music generation. */ +- musicGenerationConfig: LiveMusicGenerationConfig; +-} +- +-/** Parameters for setting weighted prompts for the live music API. */ +-export declare interface LiveMusicSetWeightedPromptsParameters { +- /** A map of text prompts to weights to use for the generation request. */ +- weightedPrompts: WeightedPrompt[]; +-} +- +-/** Prompts and config used for generating this audio chunk. */ +-export declare interface LiveMusicSourceMetadata { +- /** Weighted prompts for generating this audio chunk. */ +- clientContent?: LiveMusicClientContent; +- /** Music generation config for generating this audio chunk. */ +- musicGenerationConfig?: LiveMusicGenerationConfig; +-} +- +-/** Parameters for sending client content to the live API. */ +-export declare interface LiveSendClientContentParameters { +- /** Client content to send to the session. */ +- turns?: ContentListUnion; +- /** If true, indicates that the server content generation should start with +- the currently accumulated prompt. Otherwise, the server will await +- additional messages before starting generation. */ +- turnComplete?: boolean; +-} +- +-/** Parameters for sending realtime input to the live API. */ +-export declare interface LiveSendRealtimeInputParameters { +- /** Realtime input to send to the session. */ +- media?: BlobImageUnion; +- /** The realtime audio input stream. */ +- audio?: Blob_2; +- /** +- Indicates that the audio stream has ended, e.g. because the microphone was +- turned off. +- +- This should only be sent when automatic activity detection is enabled +- (which is the default). +- +- The client can reopen the stream by sending an audio message. +- */ +- audioStreamEnd?: boolean; +- /** The realtime video input stream. */ +- video?: BlobImageUnion; +- /** The realtime text input stream. */ +- text?: string; +- /** Marks the start of user activity. */ +- activityStart?: ActivityStart; +- /** Marks the end of user activity. */ +- activityEnd?: ActivityEnd; +-} +- +-/** Parameters for sending tool responses to the live API. */ +-export declare class LiveSendToolResponseParameters { +- /** Tool responses to send to the session. */ +- functionResponses: FunctionResponse[] | FunctionResponse; +-} +- +-/** Incremental server update generated by the model in response to client messages. +- +- Content is generated as quickly as possible, and not in real time. Clients +- may choose to buffer and play it out in real time. +- */ +-export declare interface LiveServerContent { +- /** The content that the model has generated as part of the current conversation with the user. */ +- modelTurn?: Content; +- /** If true, indicates that the model is done generating. Generation will only start in response to additional client messages. Can be set alongside `content`, indicating that the `content` is the last in the turn. */ +- turnComplete?: boolean; +- /** If true, indicates that a client message has interrupted current model generation. If the client is playing out the content in realtime, this is a good signal to stop and empty the current queue. */ +- interrupted?: boolean; +- /** Metadata returned to client when grounding is enabled. */ +- groundingMetadata?: GroundingMetadata; +- /** If true, indicates that the model is done generating. When model is +- interrupted while generating there will be no generation_complete message +- in interrupted turn, it will go through interrupted > turn_complete. +- When model assumes realtime playback there will be delay between +- generation_complete and turn_complete that is caused by model +- waiting for playback to finish. If true, indicates that the model +- has finished generating all content. This is a signal to the client +- that it can stop sending messages. */ +- generationComplete?: boolean; +- /** Input transcription. The transcription is independent to the model +- turn which means it doesn’t imply any ordering between transcription and +- model turn. */ +- inputTranscription?: Transcription; +- /** Output transcription. The transcription is independent to the model +- turn which means it doesn’t imply any ordering between transcription and +- model turn. +- */ +- outputTranscription?: Transcription; +- /** Metadata related to url context retrieval tool. */ +- urlContextMetadata?: UrlContextMetadata; +-} +- +-/** Server will not be able to service client soon. */ +-export declare interface LiveServerGoAway { +- /** The remaining time before the connection will be terminated as ABORTED. The minimal time returned here is specified differently together with the rate limits for a given model. */ +- timeLeft?: string; +-} +- +-/** Response message for API call. */ +-export declare class LiveServerMessage { +- /** Sent in response to a `LiveClientSetup` message from the client. */ +- setupComplete?: LiveServerSetupComplete; +- /** Content generated by the model in response to client messages. */ +- serverContent?: LiveServerContent; +- /** Request for the client to execute the `function_calls` and return the responses with the matching `id`s. */ +- toolCall?: LiveServerToolCall; +- /** Notification for the client that a previously issued `ToolCallMessage` with the specified `id`s should have been not executed and should be cancelled. */ +- toolCallCancellation?: LiveServerToolCallCancellation; +- /** Usage metadata about model response(s). */ +- usageMetadata?: UsageMetadata; +- /** Server will disconnect soon. */ +- goAway?: LiveServerGoAway; +- /** Update of the session resumption state. */ +- sessionResumptionUpdate?: LiveServerSessionResumptionUpdate; +- /** +- * Returns the concatenation of all text parts from the server content if present. +- * +- * @remarks +- * If there are non-text parts in the response, the concatenation of all text +- * parts will be returned, and a warning will be logged. +- */ +- get text(): string | undefined; +- /** +- * Returns the concatenation of all inline data parts from the server content if present. +- * +- * @remarks +- * If there are non-inline data parts in the +- * response, the concatenation of all inline data parts will be returned, and +- * a warning will be logged. +- */ +- get data(): string | undefined; +-} +- +-/** Update of the session resumption state. +- +- Only sent if `session_resumption` was set in the connection config. +- */ +-export declare interface LiveServerSessionResumptionUpdate { +- /** New handle that represents state that can be resumed. Empty if `resumable`=false. */ +- newHandle?: string; +- /** True if session can be resumed at this point. It might be not possible to resume session at some points. In that case we send update empty new_handle and resumable=false. Example of such case could be model executing function calls or just generating. Resuming session (using previous session token) in such state will result in some data loss. */ +- resumable?: boolean; +- /** Index of last message sent by client that is included in state represented by this SessionResumptionToken. Only sent when `SessionResumptionConfig.transparent` is set. +- +- Presence of this index allows users to transparently reconnect and avoid issue of losing some part of realtime audio input/video. If client wishes to temporarily disconnect (for example as result of receiving GoAway) they can do it without losing state by buffering messages sent since last `SessionResmumptionTokenUpdate`. This field will enable them to limit buffering (avoid keeping all requests in RAM). +- +- Note: This should not be used for when resuming a session at some time later -- in those cases partial audio and video frames arelikely not needed. */ +- lastConsumedClientMessageIndex?: string; +-} +- +-export declare interface LiveServerSetupComplete { +-} +- +-/** Request for the client to execute the `function_calls` and return the responses with the matching `id`s. */ +-export declare interface LiveServerToolCall { +- /** The function call to be executed. */ +- functionCalls?: FunctionCall[]; +-} +- +-/** Notification for the client that a previously issued `ToolCallMessage` with the specified `id`s should have been not executed and should be cancelled. +- +- If there were side-effects to those tool calls, clients may attempt to undo +- the tool calls. This message occurs only in cases where the clients interrupt +- server turns. +- */ +-export declare interface LiveServerToolCallCancellation { +- /** The ids of the tool calls to be cancelled. */ +- ids?: string[]; +-} +- +-/** Logprobs Result */ +-export declare interface LogprobsResult { +- /** Length = total number of decoding steps. The chosen candidates may or may not be in top_candidates. */ +- chosenCandidates?: LogprobsResultCandidate[]; +- /** Length = total number of decoding steps. */ +- topCandidates?: LogprobsResultTopCandidates[]; +-} +- +-/** Candidate for the logprobs token and score. */ +-export declare interface LogprobsResultCandidate { +- /** The candidate's log probability. */ +- logProbability?: number; +- /** The candidate's token string value. */ +- token?: string; +- /** The candidate's token id value. */ +- tokenId?: number; +-} +- +-/** Candidates with top log probabilities at each decoding step. */ +-export declare interface LogprobsResultTopCandidates { +- /** Sorted by log probability in descending order. */ +- candidates?: LogprobsResultCandidate[]; +-} +- +-/** Configuration for a Mask reference image. */ +-export declare interface MaskReferenceConfig { +- /** Prompts the model to generate a mask instead of you needing to +- provide one (unless MASK_MODE_USER_PROVIDED is used). */ +- maskMode?: MaskReferenceMode; +- /** A list of up to 5 class ids to use for semantic segmentation. +- Automatically creates an image mask based on specific objects. */ +- segmentationClasses?: number[]; +- /** Dilation percentage of the mask provided. +- Float between 0 and 1. */ +- maskDilation?: number; +-} +- +-/** A mask reference image. +- +- This encapsulates either a mask image provided by the user and configs for +- the user provided mask, or only config parameters for the model to generate +- a mask. +- +- A mask image is an image whose non-zero values indicate where to edit the base +- image. If the user provides a mask image, the mask must be in the same +- dimensions as the raw image. +- */ +-export declare class MaskReferenceImage { +- /** The reference image for the editing operation. */ +- referenceImage?: Image_2; +- /** The id of the reference image. */ +- referenceId?: number; +- /** The type of the reference image. Only set by the SDK. */ +- referenceType?: string; +- /** Configuration for the mask reference image. */ +- config?: MaskReferenceConfig; +- /** Internal method to convert to ReferenceImageAPIInternal. */ +- toReferenceImageAPI(): any; +-} +- +-/** Enum representing the mask mode of a mask reference image. */ +-export declare enum MaskReferenceMode { +- MASK_MODE_DEFAULT = "MASK_MODE_DEFAULT", +- MASK_MODE_USER_PROVIDED = "MASK_MODE_USER_PROVIDED", +- MASK_MODE_BACKGROUND = "MASK_MODE_BACKGROUND", +- MASK_MODE_FOREGROUND = "MASK_MODE_FOREGROUND", +- MASK_MODE_SEMANTIC = "MASK_MODE_SEMANTIC" +-} +- +-/** +- * Creates a McpCallableTool from MCP clients and an optional config. +- * +- * The callable tool can invoke the MCP clients with given function call +- * arguments. (often for automatic function calling). +- * Use the config to modify tool parameters such as behavior. +- * +- * @experimental Built-in MCP support is an experimental feature, may change in future +- * versions. +- */ +-export declare function mcpToTool(...args: [...Client[], CallableToolConfig | Client]): CallableTool; +- +-/** Server content modalities. */ +-export declare enum MediaModality { +- /** +- * The modality is unspecified. +- */ +- MODALITY_UNSPECIFIED = "MODALITY_UNSPECIFIED", +- /** +- * Plain text. +- */ +- TEXT = "TEXT", +- /** +- * Images. +- */ +- IMAGE = "IMAGE", +- /** +- * Video. +- */ +- VIDEO = "VIDEO", +- /** +- * Audio. +- */ +- AUDIO = "AUDIO", +- /** +- * Document, e.g. PDF. +- */ +- DOCUMENT = "DOCUMENT" +-} +- +-/** The media resolution to use. */ +-export declare enum MediaResolution { +- /** +- * Media resolution has not been set +- */ +- MEDIA_RESOLUTION_UNSPECIFIED = "MEDIA_RESOLUTION_UNSPECIFIED", +- /** +- * Media resolution set to low (64 tokens). +- */ +- MEDIA_RESOLUTION_LOW = "MEDIA_RESOLUTION_LOW", +- /** +- * Media resolution set to medium (256 tokens). +- */ +- MEDIA_RESOLUTION_MEDIUM = "MEDIA_RESOLUTION_MEDIUM", +- /** +- * Media resolution set to high (zoomed reframing with 256 tokens). +- */ +- MEDIA_RESOLUTION_HIGH = "MEDIA_RESOLUTION_HIGH" +-} +- +-/** Server content modalities. */ +-export declare enum Modality { +- /** +- * The modality is unspecified. +- */ +- MODALITY_UNSPECIFIED = "MODALITY_UNSPECIFIED", +- /** +- * Indicates the model should return text +- */ +- TEXT = "TEXT", +- /** +- * Indicates the model should return images. +- */ +- IMAGE = "IMAGE", +- /** +- * Indicates the model should return images. +- */ +- AUDIO = "AUDIO" +-} +- +-/** Represents token counting info for a single modality. */ +-export declare interface ModalityTokenCount { +- /** The modality associated with this token count. */ +- modality?: MediaModality; +- /** Number of tokens. */ +- tokenCount?: number; +-} +- +-/** The mode of the predictor to be used in dynamic retrieval. */ +-export declare enum Mode { +- /** +- * Always trigger retrieval. +- */ +- MODE_UNSPECIFIED = "MODE_UNSPECIFIED", +- /** +- * Run retrieval only when system decides it is necessary. +- */ +- MODE_DYNAMIC = "MODE_DYNAMIC" +-} +- +-/** A trained machine learning model. */ +-export declare interface Model { +- /** Resource name of the model. */ +- name?: string; +- /** Display name of the model. */ +- displayName?: string; +- /** Description of the model. */ +- description?: string; +- /** Version ID of the model. A new version is committed when a new +- model version is uploaded or trained under an existing model ID. The +- version ID is an auto-incrementing decimal number in string +- representation. */ +- version?: string; +- /** List of deployed models created from this base model. Note that a +- model could have been deployed to endpoints in different locations. */ +- endpoints?: Endpoint[]; +- /** Labels with user-defined metadata to organize your models. */ +- labels?: Record; +- /** Information about the tuned model from the base model. */ +- tunedModelInfo?: TunedModelInfo; +- /** The maximum number of input tokens that the model can handle. */ +- inputTokenLimit?: number; +- /** The maximum number of output tokens that the model can generate. */ +- outputTokenLimit?: number; +- /** List of actions that are supported by the model. */ +- supportedActions?: string[]; +- /** The default checkpoint id of a model version. +- */ +- defaultCheckpointId?: string; +- /** The checkpoints of the model. */ +- checkpoints?: Checkpoint[]; +-} +- +-export declare class Models extends BaseModule { +- private readonly apiClient; +- constructor(apiClient: ApiClient); +- /** +- * Makes an API request to generate content with a given model. +- * +- * For the `model` parameter, supported formats for Vertex AI API include: +- * - The Gemini model ID, for example: 'gemini-2.0-flash' +- * - The full resource name starts with 'projects/', for example: +- * 'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-2.0-flash' +- * - The partial resource name with 'publishers/', for example: +- * 'publishers/google/models/gemini-2.0-flash' or +- * 'publishers/meta/models/llama-3.1-405b-instruct-maas' +- * - `/` separated publisher and model name, for example: +- * 'google/gemini-2.0-flash' or 'meta/llama-3.1-405b-instruct-maas' +- * +- * For the `model` parameter, supported formats for Gemini API include: +- * - The Gemini model ID, for example: 'gemini-2.0-flash' +- * - The model name starts with 'models/', for example: +- * 'models/gemini-2.0-flash' +- * - For tuned models, the model name starts with 'tunedModels/', +- * for example: +- * 'tunedModels/1234567890123456789' +- * +- * Some models support multimodal input and output. +- * +- * @param params - The parameters for generating content. +- * @return The response from generating content. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateContent({ +- * model: 'gemini-2.0-flash', +- * contents: 'why is the sky blue?', +- * config: { +- * candidateCount: 2, +- * } +- * }); +- * console.log(response); +- * ``` +- */ +- generateContent: (params: types.GenerateContentParameters) => Promise; +- /** +- * Makes an API request to generate content with a given model and yields the +- * response in chunks. +- * +- * For the `model` parameter, supported formats for Vertex AI API include: +- * - The Gemini model ID, for example: 'gemini-2.0-flash' +- * - The full resource name starts with 'projects/', for example: +- * 'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-2.0-flash' +- * - The partial resource name with 'publishers/', for example: +- * 'publishers/google/models/gemini-2.0-flash' or +- * 'publishers/meta/models/llama-3.1-405b-instruct-maas' +- * - `/` separated publisher and model name, for example: +- * 'google/gemini-2.0-flash' or 'meta/llama-3.1-405b-instruct-maas' +- * +- * For the `model` parameter, supported formats for Gemini API include: +- * - The Gemini model ID, for example: 'gemini-2.0-flash' +- * - The model name starts with 'models/', for example: +- * 'models/gemini-2.0-flash' +- * - For tuned models, the model name starts with 'tunedModels/', +- * for example: +- * 'tunedModels/1234567890123456789' +- * +- * Some models support multimodal input and output. +- * +- * @param params - The parameters for generating content with streaming response. +- * @return The response from generating content. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateContentStream({ +- * model: 'gemini-2.0-flash', +- * contents: 'why is the sky blue?', +- * config: { +- * maxOutputTokens: 200, +- * } +- * }); +- * for await (const chunk of response) { +- * console.log(chunk); +- * } +- * ``` +- */ +- generateContentStream: (params: types.GenerateContentParameters) => Promise>; +- /** +- * Transforms the CallableTools in the parameters to be simply Tools, it +- * copies the params into a new object and replaces the tools, it does not +- * modify the original params. Also sets the MCP usage header if there are +- * MCP tools in the parameters. +- */ +- private processParamsForMcpUsage; +- private initAfcToolsMap; +- private processAfcStream; +- /** +- * Generates an image based on a text description and configuration. +- * +- * @param params - The parameters for generating images. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await client.models.generateImages({ +- * model: 'imagen-3.0-generate-002', +- * prompt: 'Robot holding a red skateboard', +- * config: { +- * numberOfImages: 1, +- * includeRaiReason: true, +- * }, +- * }); +- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); +- * ``` +- */ +- generateImages: (params: types.GenerateImagesParameters) => Promise; +- list: (params?: types.ListModelsParameters) => Promise>; +- /** +- * Edits an image based on a prompt, list of reference images, and configuration. +- * +- * @param params - The parameters for editing an image. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await client.models.editImage({ +- * model: 'imagen-3.0-capability-001', +- * prompt: 'Generate an image containing a mug with the product logo [1] visible on the side of the mug.', +- * referenceImages: [subjectReferenceImage] +- * config: { +- * numberOfImages: 1, +- * includeRaiReason: true, +- * }, +- * }); +- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); +- * ``` +- */ +- editImage: (params: types.EditImageParameters) => Promise; +- /** +- * Upscales an image based on an image, upscale factor, and configuration. +- * Only supported in Vertex AI currently. +- * +- * @param params - The parameters for upscaling an image. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await client.models.upscaleImage({ +- * model: 'imagen-3.0-generate-002', +- * image: image, +- * upscaleFactor: 'x2', +- * config: { +- * includeRaiReason: true, +- * }, +- * }); +- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); +- * ``` +- */ +- upscaleImage: (params: types.UpscaleImageParameters) => Promise; +- private generateContentInternal; +- private generateContentStreamInternal; +- /** +- * Calculates embeddings for the given contents. Only text is supported. +- * +- * @param params - The parameters for embedding contents. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.embedContent({ +- * model: 'text-embedding-004', +- * contents: [ +- * 'What is your name?', +- * 'What is your favorite color?', +- * ], +- * config: { +- * outputDimensionality: 64, +- * }, +- * }); +- * console.log(response); +- * ``` +- */ +- embedContent(params: types.EmbedContentParameters): Promise; +- /** +- * Generates an image based on a text description and configuration. +- * +- * @param params - The parameters for generating images. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.generateImages({ +- * model: 'imagen-3.0-generate-002', +- * prompt: 'Robot holding a red skateboard', +- * config: { +- * numberOfImages: 1, +- * includeRaiReason: true, +- * }, +- * }); +- * console.log(response?.generatedImages?.[0]?.image?.imageBytes); +- * ``` +- */ +- private generateImagesInternal; +- private editImageInternal; +- private upscaleImageInternal; +- /** +- * Fetches information about a model by name. +- * +- * @example +- * ```ts +- * const modelInfo = await ai.models.get({model: 'gemini-2.0-flash'}); +- * ``` +- */ +- get(params: types.GetModelParameters): Promise; +- private listInternal; +- /** +- * Updates a tuned model by its name. +- * +- * @param params - The parameters for updating the model. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.update({ +- * model: 'tuned-model-name', +- * config: { +- * displayName: 'New display name', +- * description: 'New description', +- * }, +- * }); +- * ``` +- */ +- update(params: types.UpdateModelParameters): Promise; +- /** +- * Deletes a tuned model by its name. +- * +- * @param params - The parameters for deleting the model. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.delete({model: 'tuned-model-name'}); +- * ``` +- */ +- delete(params: types.DeleteModelParameters): Promise; +- /** +- * Counts the number of tokens in the given contents. Multimodal input is +- * supported for Gemini models. +- * +- * @param params - The parameters for counting tokens. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.countTokens({ +- * model: 'gemini-2.0-flash', +- * contents: 'The quick brown fox jumps over the lazy dog.' +- * }); +- * console.log(response); +- * ``` +- */ +- countTokens(params: types.CountTokensParameters): Promise; +- /** +- * Given a list of contents, returns a corresponding TokensInfo containing +- * the list of tokens and list of token ids. +- * +- * This method is not supported by the Gemini Developer API. +- * +- * @param params - The parameters for computing tokens. +- * @return The response from the API. +- * +- * @example +- * ```ts +- * const response = await ai.models.computeTokens({ +- * model: 'gemini-2.0-flash', +- * contents: 'What is your name?' +- * }); +- * console.log(response); +- * ``` +- */ +- computeTokens(params: types.ComputeTokensParameters): Promise; +- /** +- * Generates videos based on a text description and configuration. +- * +- * @param params - The parameters for generating videos. +- * @return A Promise which allows you to track the progress and eventually retrieve the generated videos using the operations.get method. +- * +- * @example +- * ```ts +- * const operation = await ai.models.generateVideos({ +- * model: 'veo-2.0-generate-001', +- * prompt: 'A neon hologram of a cat driving at top speed', +- * config: { +- * numberOfVideos: 1 +- * }); +- * +- * while (!operation.done) { +- * await new Promise(resolve => setTimeout(resolve, 10000)); +- * operation = await ai.operations.getVideosOperation({operation: operation}); +- * } +- * +- * console.log(operation.response?.generatedVideos?.[0]?.video?.uri); +- * ``` +- */ +- generateVideos(params: types.GenerateVideosParameters): Promise; +-} +- +-/** Config for model selection. */ +-export declare interface ModelSelectionConfig { +- /** Options for feature selection preference. */ +- featureSelectionPreference?: FeatureSelectionPreference; +-} +- +-/** The configuration for the multi-speaker setup. */ +-export declare interface MultiSpeakerVoiceConfig { +- /** The configuration for the speaker to use. */ +- speakerVoiceConfigs?: SpeakerVoiceConfig[]; +-} +- +-/** The mode of music generation. */ +-export declare enum MusicGenerationMode { +- /** +- * This value is unused. +- */ +- MUSIC_GENERATION_MODE_UNSPECIFIED = "MUSIC_GENERATION_MODE_UNSPECIFIED", +- /** +- * Steer text prompts to regions of latent space with higher quality +- music. +- */ +- QUALITY = "QUALITY", +- /** +- * Steer text prompts to regions of latent space with a larger diversity +- of music. +- */ +- DIVERSITY = "DIVERSITY" +-} +- +-/** A long-running operation. */ +-export declare interface Operation { +- /** The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`. */ +- name?: string; +- /** Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any. */ +- metadata?: Record; +- /** If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available. */ +- done?: boolean; +- /** The error result of the operation in case of failure or cancellation. */ +- error?: Record; +-} +- +-/** Parameters for the get method of the operations module. */ +-export declare interface OperationGetParameters { +- /** The operation to be retrieved. */ +- operation: GenerateVideosOperation; +- /** Used to override the default configuration. */ +- config?: GetOperationConfig; +-} +- +-export declare class Operations extends BaseModule { +- private readonly apiClient; +- constructor(apiClient: ApiClient); +- /** +- * Gets the status of a long-running operation. +- * +- * @param parameters The parameters for the get operation request. +- * @return The updated Operation object, with the latest status or result. +- */ +- getVideosOperation(parameters: types.OperationGetParameters): Promise; +- private getVideosOperationInternal; +- private fetchPredictVideosOperationInternal; +-} +- +-/** +- * @license +- * Copyright 2025 Google LLC +- * SPDX-License-Identifier: Apache-2.0 +- */ +-/** Required. Outcome of the code execution. */ +-export declare enum Outcome { +- /** +- * Unspecified status. This value should not be used. +- */ +- OUTCOME_UNSPECIFIED = "OUTCOME_UNSPECIFIED", +- /** +- * Code execution completed successfully. +- */ +- OUTCOME_OK = "OUTCOME_OK", +- /** +- * Code execution finished but with a failure. `stderr` should contain the reason. +- */ +- OUTCOME_FAILED = "OUTCOME_FAILED", +- /** +- * Code execution ran for too long, and was cancelled. There may or may not be a partial output present. +- */ +- OUTCOME_DEADLINE_EXCEEDED = "OUTCOME_DEADLINE_EXCEEDED" +-} +- +-/** +- * @license +- * Copyright 2025 Google LLC +- * SPDX-License-Identifier: Apache-2.0 +- */ +-/** +- * Pagers for the GenAI List APIs. +- */ +-export declare enum PagedItem { +- PAGED_ITEM_BATCH_JOBS = "batchJobs", +- PAGED_ITEM_MODELS = "models", +- PAGED_ITEM_TUNING_JOBS = "tuningJobs", +- PAGED_ITEM_FILES = "files", +- PAGED_ITEM_CACHED_CONTENTS = "cachedContents" +-} +- +-declare interface PagedItemConfig { +- config?: { +- pageToken?: string; +- pageSize?: number; +- }; +-} +- +-declare interface PagedItemResponse { +- nextPageToken?: string; +- batchJobs?: T[]; +- models?: T[]; +- tuningJobs?: T[]; +- files?: T[]; +- cachedContents?: T[]; +-} +- +-/** +- * Pager class for iterating through paginated results. +- */ +-export declare class Pager implements AsyncIterable { +- private nameInternal; +- private pageInternal; +- private paramsInternal; +- private pageInternalSize; +- protected requestInternal: (params: PagedItemConfig) => Promise>; +- protected idxInternal: number; +- constructor(name: PagedItem, request: (params: PagedItemConfig) => Promise>, response: PagedItemResponse, params: PagedItemConfig); +- private init; +- private initNextPage; +- /** +- * Returns the current page, which is a list of items. +- * +- * @remarks +- * The first page is retrieved when the pager is created. The returned list of +- * items could be a subset of the entire list. +- */ +- get page(): T[]; +- /** +- * Returns the type of paged item (for example, ``batch_jobs``). +- */ +- get name(): PagedItem; +- /** +- * Returns the length of the page fetched each time by this pager. +- * +- * @remarks +- * The number of items in the page is less than or equal to the page length. +- */ +- get pageSize(): number; +- /** +- * Returns the parameters when making the API request for the next page. +- * +- * @remarks +- * Parameters contain a set of optional configs that can be +- * used to customize the API request. For example, the `pageToken` parameter +- * contains the token to request the next page. +- */ +- get params(): PagedItemConfig; +- /** +- * Returns the total number of items in the current page. +- */ +- get pageLength(): number; +- /** +- * Returns the item at the given index. +- */ +- getItem(index: number): T; +- /** +- * Returns an async iterator that support iterating through all items +- * retrieved from the API. +- * +- * @remarks +- * The iterator will automatically fetch the next page if there are more items +- * to fetch from the API. +- * +- * @example +- * +- * ```ts +- * const pager = await ai.files.list({config: {pageSize: 10}}); +- * for await (const file of pager) { +- * console.log(file.name); +- * } +- * ``` +- */ +- [Symbol.asyncIterator](): AsyncIterator; +- /** +- * Fetches the next page of items. This makes a new API request. +- * +- * @throws {Error} If there are no more pages to fetch. +- * +- * @example +- * +- * ```ts +- * const pager = await ai.files.list({config: {pageSize: 10}}); +- * let page = pager.page; +- * while (true) { +- * for (const file of page) { +- * console.log(file.name); +- * } +- * if (!pager.hasNextPage()) { +- * break; +- * } +- * page = await pager.nextPage(); +- * } +- * ``` +- */ +- nextPage(): Promise; +- /** +- * Returns true if there are more pages to fetch from the API. +- */ +- hasNextPage(): boolean; +-} +- +-/** A datatype containing media content. +- +- Exactly one field within a Part should be set, representing the specific type +- of content being conveyed. Using multiple fields within the same `Part` +- instance is considered invalid. +- */ +-export declare interface Part { +- /** Metadata for a given video. */ +- videoMetadata?: VideoMetadata; +- /** Indicates if the part is thought from the model. */ +- thought?: boolean; +- /** Optional. Inlined bytes data. */ +- inlineData?: Blob_2; +- /** Optional. Result of executing the [ExecutableCode]. */ +- codeExecutionResult?: CodeExecutionResult; +- /** Optional. Code generated by the model that is meant to be executed. */ +- executableCode?: ExecutableCode; +- /** Optional. URI based data. */ +- fileData?: FileData; +- /** Optional. A predicted [FunctionCall] returned from the model that contains a string representing the [FunctionDeclaration.name] with the parameters and their values. */ +- functionCall?: FunctionCall; +- /** Optional. The result output of a [FunctionCall] that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function call. It is used as context to the model. */ +- functionResponse?: FunctionResponse; +- /** Optional. Text part (can be code). */ +- text?: string; +-} +- +-export declare type PartListUnion = PartUnion[] | PartUnion; +- +-/** Tuning spec for Partner models. */ +-export declare interface PartnerModelTuningSpec { +- /** Hyperparameters for tuning. The accepted hyper_parameters and their valid range of values will differ depending on the base model. */ +- hyperParameters?: Record; +- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ +- trainingDatasetUri?: string; +- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ +- validationDatasetUri?: string; +-} +- +-export declare type PartUnion = Part | string; +- +-/** Enum that controls the generation of people. */ +-export declare enum PersonGeneration { +- DONT_ALLOW = "DONT_ALLOW", +- ALLOW_ADULT = "ALLOW_ADULT", +- ALLOW_ALL = "ALLOW_ALL" +-} +- +-/** The configuration for the prebuilt speaker to use. */ +-export declare interface PrebuiltVoiceConfig { +- /** The name of the prebuilt voice to use. */ +- voiceName?: string; +-} +- +-/** Config for proactivity features. */ +-export declare interface ProactivityConfig { +- /** If enabled, the model can reject responding to the last prompt. For +- example, this allows the model to ignore out of context speech or to stay +- silent if the user did not make a request, yet. */ +- proactiveAudio?: boolean; +-} +- +-/** Specifies the context retrieval config. */ +-export declare interface RagRetrievalConfig { +- /** Optional. Config for filters. */ +- filter?: RagRetrievalConfigFilter; +- /** Optional. Config for Hybrid Search. */ +- hybridSearch?: RagRetrievalConfigHybridSearch; +- /** Optional. Config for ranking and reranking. */ +- ranking?: RagRetrievalConfigRanking; +- /** Optional. The number of contexts to retrieve. */ +- topK?: number; +-} +- +-/** Config for filters. */ +-export declare interface RagRetrievalConfigFilter { +- /** Optional. String for metadata filtering. */ +- metadataFilter?: string; +- /** Optional. Only returns contexts with vector distance smaller than the threshold. */ +- vectorDistanceThreshold?: number; +- /** Optional. Only returns contexts with vector similarity larger than the threshold. */ +- vectorSimilarityThreshold?: number; +-} +- +-/** Config for Hybrid Search. */ +-export declare interface RagRetrievalConfigHybridSearch { +- /** Optional. Alpha value controls the weight between dense and sparse vector search results. The range is [0, 1], while 0 means sparse vector search only and 1 means dense vector search only. The default value is 0.5 which balances sparse and dense vector search equally. */ +- alpha?: number; +-} +- +-/** Config for ranking and reranking. */ +-export declare interface RagRetrievalConfigRanking { +- /** Optional. Config for LlmRanker. */ +- llmRanker?: RagRetrievalConfigRankingLlmRanker; +- /** Optional. Config for Rank Service. */ +- rankService?: RagRetrievalConfigRankingRankService; +-} +- +-/** Config for LlmRanker. */ +-export declare interface RagRetrievalConfigRankingLlmRanker { +- /** Optional. The model name used for ranking. Format: `gemini-1.5-pro` */ +- modelName?: string; +-} +- +-/** Config for Rank Service. */ +-export declare interface RagRetrievalConfigRankingRankService { +- /** Optional. The model name of the rank service. Format: `semantic-ranker-512@latest` */ +- modelName?: string; +-} +- +-/** A raw reference image. +- +- A raw reference image represents the base image to edit, provided by the user. +- It can optionally be provided in addition to a mask reference image or +- a style reference image. +- */ +-export declare class RawReferenceImage { +- /** The reference image for the editing operation. */ +- referenceImage?: Image_2; +- /** The id of the reference image. */ +- referenceId?: number; +- /** The type of the reference image. Only set by the SDK. */ +- referenceType?: string; +- /** Internal method to convert to ReferenceImageAPIInternal. */ +- toReferenceImageAPI(): any; +-} +- +-/** Marks the end of user activity. +- +- This can only be sent if automatic (i.e. server-side) activity detection is +- disabled. +- */ +-export declare interface RealtimeInputConfig { +- /** If not set, automatic activity detection is enabled by default. If automatic voice detection is disabled, the client must send activity signals. */ +- automaticActivityDetection?: AutomaticActivityDetection; +- /** Defines what effect activity has. */ +- activityHandling?: ActivityHandling; +- /** Defines which input is included in the user's turn. */ +- turnCoverage?: TurnCoverage; +-} +- +-export declare type ReferenceImage = RawReferenceImage | MaskReferenceImage | ControlReferenceImage | StyleReferenceImage | SubjectReferenceImage; +- +-/** Represents a recorded session. */ +-export declare interface ReplayFile { +- replayId?: string; +- interactions?: ReplayInteraction[]; +-} +- +-/** Represents a single interaction, request and response in a replay. */ +-export declare interface ReplayInteraction { +- request?: ReplayRequest; +- response?: ReplayResponse; +-} +- +-/** Represents a single request in a replay. */ +-export declare interface ReplayRequest { +- method?: string; +- url?: string; +- headers?: Record; +- bodySegments?: Record[]; +-} +- +-/** Represents a single response in a replay. */ +-export declare class ReplayResponse { +- statusCode?: number; +- headers?: Record; +- bodySegments?: Record[]; +- sdkResponseSegments?: Record[]; +-} +- +-/** Defines a retrieval tool that model can call to access external knowledge. */ +-export declare interface Retrieval { +- /** Optional. Deprecated. This option is no longer supported. */ +- disableAttribution?: boolean; +- /** Set to use data source powered by Vertex AI Search. */ +- vertexAiSearch?: VertexAISearch; +- /** Set to use data source powered by Vertex RAG store. User data is uploaded via the VertexRagDataService. */ +- vertexRagStore?: VertexRagStore; +-} +- +-/** Retrieval config. +- */ +-export declare interface RetrievalConfig { +- /** Optional. The location of the user. */ +- latLng?: LatLng; +-} +- +-/** Metadata related to retrieval in the grounding flow. */ +-export declare interface RetrievalMetadata { +- /** Optional. Score indicating how likely information from Google Search could help answer the prompt. The score is in the range `[0, 1]`, where 0 is the least likely and 1 is the most likely. This score is only populated when Google Search grounding and dynamic retrieval is enabled. It will be compared to the threshold to determine whether to trigger Google Search. */ +- googleSearchDynamicRetrievalScore?: number; +-} +- +-/** Safety attributes of a GeneratedImage or the user-provided prompt. */ +-export declare interface SafetyAttributes { +- /** List of RAI categories. +- */ +- categories?: string[]; +- /** List of scores of each categories. +- */ +- scores?: number[]; +- /** Internal use only. +- */ +- contentType?: string; +-} +- +-/** Enum that controls the safety filter level for objectionable content. */ +-export declare enum SafetyFilterLevel { +- BLOCK_LOW_AND_ABOVE = "BLOCK_LOW_AND_ABOVE", +- BLOCK_MEDIUM_AND_ABOVE = "BLOCK_MEDIUM_AND_ABOVE", +- BLOCK_ONLY_HIGH = "BLOCK_ONLY_HIGH", +- BLOCK_NONE = "BLOCK_NONE" +-} +- +-/** Safety rating corresponding to the generated content. */ +-export declare interface SafetyRating { +- /** Output only. Indicates whether the content was filtered out because of this rating. */ +- blocked?: boolean; +- /** Output only. Harm category. */ +- category?: HarmCategory; +- /** Output only. Harm probability levels in the content. */ +- probability?: HarmProbability; +- /** Output only. Harm probability score. */ +- probabilityScore?: number; +- /** Output only. Harm severity levels in the content. */ +- severity?: HarmSeverity; +- /** Output only. Harm severity score. */ +- severityScore?: number; +-} +- +-/** Safety settings. */ +-export declare interface SafetySetting { +- /** Determines if the harm block method uses probability or probability +- and severity scores. */ +- method?: HarmBlockMethod; +- /** Required. Harm category. */ +- category?: HarmCategory; +- /** Required. The harm block threshold. */ +- threshold?: HarmBlockThreshold; +-} +- +-/** Scale of the generated music. */ +-export declare enum Scale { +- /** +- * Default value. This value is unused. +- */ +- SCALE_UNSPECIFIED = "SCALE_UNSPECIFIED", +- /** +- * C major or A minor. +- */ +- C_MAJOR_A_MINOR = "C_MAJOR_A_MINOR", +- /** +- * Db major or Bb minor. +- */ +- D_FLAT_MAJOR_B_FLAT_MINOR = "D_FLAT_MAJOR_B_FLAT_MINOR", +- /** +- * D major or B minor. +- */ +- D_MAJOR_B_MINOR = "D_MAJOR_B_MINOR", +- /** +- * Eb major or C minor +- */ +- E_FLAT_MAJOR_C_MINOR = "E_FLAT_MAJOR_C_MINOR", +- /** +- * E major or Db minor. +- */ +- E_MAJOR_D_FLAT_MINOR = "E_MAJOR_D_FLAT_MINOR", +- /** +- * F major or D minor. +- */ +- F_MAJOR_D_MINOR = "F_MAJOR_D_MINOR", +- /** +- * Gb major or Eb minor. +- */ +- G_FLAT_MAJOR_E_FLAT_MINOR = "G_FLAT_MAJOR_E_FLAT_MINOR", +- /** +- * G major or E minor. +- */ +- G_MAJOR_E_MINOR = "G_MAJOR_E_MINOR", +- /** +- * Ab major or F minor. +- */ +- A_FLAT_MAJOR_F_MINOR = "A_FLAT_MAJOR_F_MINOR", +- /** +- * A major or Gb minor. +- */ +- A_MAJOR_G_FLAT_MINOR = "A_MAJOR_G_FLAT_MINOR", +- /** +- * Bb major or G minor. +- */ +- B_FLAT_MAJOR_G_MINOR = "B_FLAT_MAJOR_G_MINOR", +- /** +- * B major or Ab minor. +- */ +- B_MAJOR_A_FLAT_MINOR = "B_MAJOR_A_FLAT_MINOR" +-} +- +-/** Schema is used to define the format of input/output data. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema-object). More fields may be added in the future as needed. */ +-export declare interface Schema { +- /** Optional. The value should be validated against any (one or more) of the subschemas in the list. */ +- anyOf?: Schema[]; +- /** Optional. Default value of the data. */ +- default?: unknown; +- /** Optional. The description of the data. */ +- description?: string; +- /** Optional. Possible values of the element of primitive type with enum format. Examples: 1. We can define direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]} 2. We can define apartment number as : {type:INTEGER, format:enum, enum:["101", "201", "301"]} */ +- enum?: string[]; +- /** Optional. Example of the object. Will only populated when the object is the root. */ +- example?: unknown; +- /** Optional. The format of the data. Supported formats: for NUMBER type: "float", "double" for INTEGER type: "int32", "int64" for STRING type: "email", "byte", etc */ +- format?: string; +- /** Optional. SCHEMA FIELDS FOR TYPE ARRAY Schema of the elements of Type.ARRAY. */ +- items?: Schema; +- /** Optional. Maximum number of the elements for Type.ARRAY. */ +- maxItems?: string; +- /** Optional. Maximum length of the Type.STRING */ +- maxLength?: string; +- /** Optional. Maximum number of the properties for Type.OBJECT. */ +- maxProperties?: string; +- /** Optional. Maximum value of the Type.INTEGER and Type.NUMBER */ +- maximum?: number; +- /** Optional. Minimum number of the elements for Type.ARRAY. */ +- minItems?: string; +- /** Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING */ +- minLength?: string; +- /** Optional. Minimum number of the properties for Type.OBJECT. */ +- minProperties?: string; +- /** Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER */ +- minimum?: number; +- /** Optional. Indicates if the value may be null. */ +- nullable?: boolean; +- /** Optional. Pattern of the Type.STRING to restrict a string to a regular expression. */ +- pattern?: string; +- /** Optional. SCHEMA FIELDS FOR TYPE OBJECT Properties of Type.OBJECT. */ +- properties?: Record; +- /** Optional. The order of the properties. Not a standard field in open api spec. Only used to support the order of the properties. */ +- propertyOrdering?: string[]; +- /** Optional. Required properties of Type.OBJECT. */ +- required?: string[]; +- /** Optional. The title of the Schema. */ +- title?: string; +- /** Optional. The type of the data. */ +- type?: Type; +-} +- +-export declare type SchemaUnion = Schema | unknown; +- +-/** Google search entry point. */ +-export declare interface SearchEntryPoint { +- /** Optional. Web content snippet that can be embedded in a web page or an app webview. */ +- renderedContent?: string; +- /** Optional. Base64 encoded JSON representing array of tuple. */ +- sdkBlob?: string; +-} +- +-/** Segment of the content. */ +-export declare interface Segment { +- /** Output only. End index in the given Part, measured in bytes. Offset from the start of the Part, exclusive, starting at zero. */ +- endIndex?: number; +- /** Output only. The index of a Part object within its parent Content object. */ +- partIndex?: number; +- /** Output only. Start index in the given Part, measured in bytes. Offset from the start of the Part, inclusive, starting at zero. */ +- startIndex?: number; +- /** Output only. The text corresponding to the segment from the response. */ +- text?: string; +-} +- +-/** Parameters for sending a message within a chat session. +- +- These parameters are used with the `chat.sendMessage()` method. +- */ +-export declare interface SendMessageParameters { +- /** The message to send to the model. +- +- The SDK will combine all parts into a single 'user' content to send to +- the model. +- */ +- message: PartListUnion; +- /** Config for this specific request. +- +- Please note that the per-request config does not change the chat level +- config, nor inherit from it. If you intend to use some values from the +- chat's default config, you must explicitly copy them into this per-request +- config. +- */ +- config?: GenerateContentConfig; +-} +- +-/** +- Represents a connection to the API. +- +- @experimental +- */ +-export declare class Session { +- readonly conn: WebSocket_2; +- private readonly apiClient; +- constructor(conn: WebSocket_2, apiClient: ApiClient); +- private tLiveClientContent; +- private tLiveClienttToolResponse; +- /** +- Send a message over the established connection. +- +- @param params - Contains two **optional** properties, `turns` and +- `turnComplete`. +- +- - `turns` will be converted to a `Content[]` +- - `turnComplete: true` [default] indicates that you are done sending +- content and expect a response. If `turnComplete: false`, the server +- will wait for additional messages before starting generation. +- +- @experimental +- +- @remarks +- There are two ways to send messages to the live API: +- `sendClientContent` and `sendRealtimeInput`. +- +- `sendClientContent` messages are added to the model context **in order**. +- Having a conversation using `sendClientContent` messages is roughly +- equivalent to using the `Chat.sendMessageStream`, except that the state of +- the `chat` history is stored on the API server instead of locally. +- +- Because of `sendClientContent`'s order guarantee, the model cannot respons +- as quickly to `sendClientContent` messages as to `sendRealtimeInput` +- messages. This makes the biggest difference when sending objects that have +- significant preprocessing time (typically images). +- +- The `sendClientContent` message sends a `Content[]` +- which has more options than the `Blob` sent by `sendRealtimeInput`. +- +- So the main use-cases for `sendClientContent` over `sendRealtimeInput` are: +- +- - Sending anything that can't be represented as a `Blob` (text, +- `sendClientContent({turns="Hello?"}`)). +- - Managing turns when not using audio input and voice activity detection. +- (`sendClientContent({turnComplete:true})` or the short form +- `sendClientContent()`) +- - Prefilling a conversation context +- ``` +- sendClientContent({ +- turns: [ +- Content({role:user, parts:...}), +- Content({role:user, parts:...}), +- ... +- ] +- }) +- ``` +- @experimental +- */ +- sendClientContent(params: types.LiveSendClientContentParameters): void; +- /** +- Send a realtime message over the established connection. +- +- @param params - Contains one property, `media`. +- +- - `media` will be converted to a `Blob` +- +- @experimental +- +- @remarks +- Use `sendRealtimeInput` for realtime audio chunks and video frames (images). +- +- With `sendRealtimeInput` the api will respond to audio automatically +- based on voice activity detection (VAD). +- +- `sendRealtimeInput` is optimized for responsivness at the expense of +- deterministic ordering guarantees. Audio and video tokens are to the +- context when they become available. +- +- Note: The Call signature expects a `Blob` object, but only a subset +- of audio and image mimetypes are allowed. +- */ +- sendRealtimeInput(params: types.LiveSendRealtimeInputParameters): void; +- /** +- Send a function response message over the established connection. +- +- @param params - Contains property `functionResponses`. +- +- - `functionResponses` will be converted to a `functionResponses[]` +- +- @remarks +- Use `sendFunctionResponse` to reply to `LiveServerToolCall` from the server. +- +- Use {@link types.LiveConnectConfig#tools} to configure the callable functions. +- +- @experimental +- */ +- sendToolResponse(params: types.LiveSendToolResponseParameters): void; +- /** +- Terminates the WebSocket connection. +- +- @experimental +- +- @example +- ```ts +- let model: string; +- if (GOOGLE_GENAI_USE_VERTEXAI) { +- model = 'gemini-2.0-flash-live-preview-04-09'; +- } else { +- model = 'gemini-2.0-flash-live-001'; +- } +- const session = await ai.live.connect({ +- model: model, +- config: { +- responseModalities: [Modality.AUDIO], +- } +- }); +- +- session.close(); +- ``` +- */ +- close(): void; +-} +- +-/** Configuration of session resumption mechanism. +- +- Included in `LiveConnectConfig.session_resumption`. If included server +- will send `LiveServerSessionResumptionUpdate` messages. +- */ +-export declare interface SessionResumptionConfig { +- /** Session resumption handle of previous session (session to restore). +- +- If not present new session will be started. */ +- handle?: string; +- /** If set the server will send `last_consumed_client_message_index` in the `session_resumption_update` messages to allow for transparent reconnections. */ +- transparent?: boolean; +-} +- +-/** +- * Overrides the base URLs for the Gemini API and Vertex AI API. +- * +- * @remarks This function should be called before initializing the SDK. If the +- * base URLs are set after initializing the SDK, the base URLs will not be +- * updated. Base URLs provided in the HttpOptions will also take precedence over +- * URLs set here. +- * +- * @example +- * ```ts +- * import {GoogleGenAI, setDefaultBaseUrls} from '@google/genai'; +- * // Override the base URL for the Gemini API. +- * setDefaultBaseUrls({geminiUrl:'https://gemini.google.com'}); +- * +- * // Override the base URL for the Vertex AI API. +- * setDefaultBaseUrls({vertexUrl: 'https://vertexai.googleapis.com'}); +- * +- * const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'}); +- * ``` +- */ +-export declare function setDefaultBaseUrls(baseUrlParams: BaseUrlParameters): void; +- +-/** Context window will be truncated by keeping only suffix of it. +- +- Context window will always be cut at start of USER role turn. System +- instructions and `BidiGenerateContentSetup.prefix_turns` will not be +- subject to the sliding window mechanism, they will always stay at the +- beginning of context window. +- */ +-export declare interface SlidingWindow { +- /** Session reduction target -- how many tokens we should keep. Window shortening operation has some latency costs, so we should avoid running it on every turn. Should be < trigger_tokens. If not set, trigger_tokens/2 is assumed. */ +- targetTokens?: string; +-} +- +-/** The configuration for the speaker to use. */ +-export declare interface SpeakerVoiceConfig { +- /** The name of the speaker to use. Should be the same as in the +- prompt. */ +- speaker?: string; +- /** The configuration for the voice to use. */ +- voiceConfig?: VoiceConfig; +-} +- +-/** The speech generation configuration. */ +-export declare interface SpeechConfig { +- /** The configuration for the speaker to use. +- */ +- voiceConfig?: VoiceConfig; +- /** The configuration for the multi-speaker setup. +- It is mutually exclusive with the voice_config field. +- */ +- multiSpeakerVoiceConfig?: MultiSpeakerVoiceConfig; +- /** Language code (ISO 639. e.g. en-US) for the speech synthesization. +- Only available for Live API. +- */ +- languageCode?: string; +-} +- +-export declare type SpeechConfigUnion = SpeechConfig | string; +- +-/** Start of speech sensitivity. */ +-export declare enum StartSensitivity { +- /** +- * The default is START_SENSITIVITY_LOW. +- */ +- START_SENSITIVITY_UNSPECIFIED = "START_SENSITIVITY_UNSPECIFIED", +- /** +- * Automatic detection will detect the start of speech more often. +- */ +- START_SENSITIVITY_HIGH = "START_SENSITIVITY_HIGH", +- /** +- * Automatic detection will detect the start of speech less often. +- */ +- START_SENSITIVITY_LOW = "START_SENSITIVITY_LOW" +-} +- +-/** Configuration for a Style reference image. */ +-export declare interface StyleReferenceConfig { +- /** A text description of the style to use for the generated image. */ +- styleDescription?: string; +-} +- +-/** A style reference image. +- +- This encapsulates a style reference image provided by the user, and +- additionally optional config parameters for the style reference image. +- +- A raw reference image can also be provided as a destination for the style to +- be applied to. +- */ +-export declare class StyleReferenceImage { +- /** The reference image for the editing operation. */ +- referenceImage?: Image_2; +- /** The id of the reference image. */ +- referenceId?: number; +- /** The type of the reference image. Only set by the SDK. */ +- referenceType?: string; +- /** Configuration for the style reference image. */ +- config?: StyleReferenceConfig; +- /** Internal method to convert to ReferenceImageAPIInternal. */ +- toReferenceImageAPI(): any; +-} +- +-/** Configuration for a Subject reference image. */ +-export declare interface SubjectReferenceConfig { +- /** The subject type of a subject reference image. */ +- subjectType?: SubjectReferenceType; +- /** Subject description for the image. */ +- subjectDescription?: string; +-} +- +-/** A subject reference image. +- +- This encapsulates a subject reference image provided by the user, and +- additionally optional config parameters for the subject reference image. +- +- A raw reference image can also be provided as a destination for the subject to +- be applied to. +- */ +-export declare class SubjectReferenceImage { +- /** The reference image for the editing operation. */ +- referenceImage?: Image_2; +- /** The id of the reference image. */ +- referenceId?: number; +- /** The type of the reference image. Only set by the SDK. */ +- referenceType?: string; +- /** Configuration for the subject reference image. */ +- config?: SubjectReferenceConfig; +- toReferenceImageAPI(): any; +-} +- +-/** Enum representing the subject type of a subject reference image. */ +-export declare enum SubjectReferenceType { +- SUBJECT_TYPE_DEFAULT = "SUBJECT_TYPE_DEFAULT", +- SUBJECT_TYPE_PERSON = "SUBJECT_TYPE_PERSON", +- SUBJECT_TYPE_ANIMAL = "SUBJECT_TYPE_ANIMAL", +- SUBJECT_TYPE_PRODUCT = "SUBJECT_TYPE_PRODUCT" +-} +- +-/** Hyperparameters for SFT. */ +-export declare interface SupervisedHyperParameters { +- /** Optional. Adapter size for tuning. */ +- adapterSize?: AdapterSize; +- /** Optional. Number of complete passes the model makes over the entire training dataset during training. */ +- epochCount?: string; +- /** Optional. Multiplier for adjusting the default learning rate. */ +- learningRateMultiplier?: number; +-} +- +-/** Dataset distribution for Supervised Tuning. */ +-export declare interface SupervisedTuningDatasetDistribution { +- /** Output only. Sum of a given population of values that are billable. */ +- billableSum?: string; +- /** Output only. Defines the histogram bucket. */ +- buckets?: SupervisedTuningDatasetDistributionDatasetBucket[]; +- /** Output only. The maximum of the population values. */ +- max?: number; +- /** Output only. The arithmetic mean of the values in the population. */ +- mean?: number; +- /** Output only. The median of the values in the population. */ +- median?: number; +- /** Output only. The minimum of the population values. */ +- min?: number; +- /** Output only. The 5th percentile of the values in the population. */ +- p5?: number; +- /** Output only. The 95th percentile of the values in the population. */ +- p95?: number; +- /** Output only. Sum of a given population of values. */ +- sum?: string; +-} +- +-/** Dataset bucket used to create a histogram for the distribution given a population of values. */ +-export declare interface SupervisedTuningDatasetDistributionDatasetBucket { +- /** Output only. Number of values in the bucket. */ +- count?: number; +- /** Output only. Left bound of the bucket. */ +- left?: number; +- /** Output only. Right bound of the bucket. */ +- right?: number; +-} +- +-/** Tuning data statistics for Supervised Tuning. */ +-export declare interface SupervisedTuningDataStats { +- /** Output only. Number of billable characters in the tuning dataset. */ +- totalBillableCharacterCount?: string; +- /** Output only. Number of billable tokens in the tuning dataset. */ +- totalBillableTokenCount?: string; +- /** The number of examples in the dataset that have been truncated by any amount. */ +- totalTruncatedExampleCount?: string; +- /** Output only. Number of tuning characters in the tuning dataset. */ +- totalTuningCharacterCount?: string; +- /** A partial sample of the indices (starting from 1) of the truncated examples. */ +- truncatedExampleIndices?: string[]; +- /** Output only. Number of examples in the tuning dataset. */ +- tuningDatasetExampleCount?: string; +- /** Output only. Number of tuning steps for this Tuning Job. */ +- tuningStepCount?: string; +- /** Output only. Sample user messages in the training dataset uri. */ +- userDatasetExamples?: Content[]; +- /** Output only. Dataset distributions for the user input tokens. */ +- userInputTokenDistribution?: SupervisedTuningDatasetDistribution; +- /** Output only. Dataset distributions for the messages per example. */ +- userMessagePerExampleDistribution?: SupervisedTuningDatasetDistribution; +- /** Output only. Dataset distributions for the user output tokens. */ +- userOutputTokenDistribution?: SupervisedTuningDatasetDistribution; +-} +- +-/** Tuning Spec for Supervised Tuning for first party models. */ +-export declare interface SupervisedTuningSpec { +- /** Optional. Hyperparameters for SFT. */ +- hyperParameters?: SupervisedHyperParameters; +- /** Required. Cloud Storage path to file containing training dataset for tuning. The dataset must be formatted as a JSONL file. */ +- trainingDatasetUri?: string; +- /** Optional. Cloud Storage path to file containing validation dataset for tuning. The dataset must be formatted as a JSONL file. */ +- validationDatasetUri?: string; +- /** Optional. If set to true, disable intermediate checkpoints for SFT and only the last checkpoint will be exported. */ +- exportLastCheckpointOnly?: boolean; +-} +- +-export declare interface TestTableFile { +- comment?: string; +- testMethod?: string; +- parameterNames?: string[]; +- testTable?: TestTableItem[]; +-} +- +-export declare interface TestTableItem { +- /** The name of the test. This is used to derive the replay id. */ +- name?: string; +- /** The parameters to the test. Use pydantic models. */ +- parameters?: Record; +- /** Expects an exception for MLDev matching the string. */ +- exceptionIfMldev?: string; +- /** Expects an exception for Vertex matching the string. */ +- exceptionIfVertex?: string; +- /** Use if you don't want to use the default replay id which is derived from the test name. */ +- overrideReplayId?: string; +- /** True if the parameters contain an unsupported union type. This test will be skipped for languages that do not support the union type. */ +- hasUnion?: boolean; +- /** When set to a reason string, this test will be skipped in the API mode. Use this flag for tests that can not be reproduced with the real API. E.g. a test that deletes a resource. */ +- skipInApiMode?: string; +- /** Keys to ignore when comparing the request and response. This is useful for tests that are not deterministic. */ +- ignoreKeys?: string[]; +-} +- +-/** The thinking features configuration. */ +-export declare interface ThinkingConfig { +- /** Indicates whether to include thoughts in the response. If true, thoughts are returned only if the model supports thought and thoughts are available. +- */ +- includeThoughts?: boolean; +- /** Indicates the thinking budget in tokens. +- */ +- thinkingBudget?: number; +-} +- +-/** Tokens info with a list of tokens and the corresponding list of token ids. */ +-export declare interface TokensInfo { +- /** Optional. Optional fields for the role from the corresponding Content. */ +- role?: string; +- /** A list of token ids from the input. */ +- tokenIds?: string[]; +- /** A list of tokens from the input. */ +- tokens?: string[]; +-} +- +-/** Tool details of a tool that the model may use to generate a response. */ +-export declare interface Tool { +- /** List of function declarations that the tool supports. */ +- functionDeclarations?: FunctionDeclaration[]; +- /** Optional. Retrieval tool type. System will always execute the provided retrieval tool(s) to get external knowledge to answer the prompt. Retrieval results are presented to the model for generation. */ +- retrieval?: Retrieval; +- /** Optional. Google Search tool type. Specialized retrieval tool +- that is powered by Google Search. */ +- googleSearch?: GoogleSearch; +- /** Optional. GoogleSearchRetrieval tool type. Specialized retrieval tool that is powered by Google search. */ +- googleSearchRetrieval?: GoogleSearchRetrieval; +- /** Optional. Enterprise web search tool type. Specialized retrieval +- tool that is powered by Vertex AI Search and Sec4 compliance. */ +- enterpriseWebSearch?: EnterpriseWebSearch; +- /** Optional. Google Maps tool type. Specialized retrieval tool +- that is powered by Google Maps. */ +- googleMaps?: GoogleMaps; +- /** Optional. Tool to support URL context retrieval. */ +- urlContext?: UrlContext; +- /** Optional. CodeExecution tool type. Enables the model to execute code as part of generation. This field is only used by the Gemini Developer API services. */ +- codeExecution?: ToolCodeExecution; +-} +- +-/** Tool that executes code generated by the model, and automatically returns the result to the model. See also [ExecutableCode]and [CodeExecutionResult] which are input and output to this tool. */ +-export declare interface ToolCodeExecution { +-} +- +-/** Tool config. +- +- This config is shared for all tools provided in the request. +- */ +-export declare interface ToolConfig { +- /** Optional. Function calling config. */ +- functionCallingConfig?: FunctionCallingConfig; +- /** Optional. Retrieval config. */ +- retrievalConfig?: RetrievalConfig; +-} +- +-export declare type ToolListUnion = ToolUnion[]; +- +-export declare type ToolUnion = Tool | CallableTool; +- +-/** Output only. Traffic type. This shows whether a request consumes Pay-As-You-Go or Provisioned Throughput quota. */ +-export declare enum TrafficType { +- /** +- * Unspecified request traffic type. +- */ +- TRAFFIC_TYPE_UNSPECIFIED = "TRAFFIC_TYPE_UNSPECIFIED", +- /** +- * Type for Pay-As-You-Go traffic. +- */ +- ON_DEMAND = "ON_DEMAND", +- /** +- * Type for Provisioned Throughput traffic. +- */ +- PROVISIONED_THROUGHPUT = "PROVISIONED_THROUGHPUT" +-} +- +-/** Audio transcription in Server Conent. */ +-export declare interface Transcription { +- /** Transcription text. +- */ +- text?: string; +- /** The bool indicates the end of the transcription. +- */ +- finished?: boolean; +-} +- +-export declare interface TunedModel { +- /** Output only. The resource name of the TunedModel. Format: `projects/{project}/locations/{location}/models/{model}`. */ +- model?: string; +- /** Output only. A resource name of an Endpoint. Format: `projects/{project}/locations/{location}/endpoints/{endpoint}`. */ +- endpoint?: string; +- /** The checkpoints associated with this TunedModel. +- This field is only populated for tuning jobs that enable intermediate +- checkpoints. */ +- checkpoints?: TunedModelCheckpoint[]; +-} +- +-/** TunedModelCheckpoint for the Tuned Model of a Tuning Job. */ +-export declare interface TunedModelCheckpoint { +- /** The ID of the checkpoint. +- */ +- checkpointId?: string; +- /** The epoch of the checkpoint. +- */ +- epoch?: string; +- /** The step of the checkpoint. +- */ +- step?: string; +- /** The Endpoint resource name that the checkpoint is deployed to. +- Format: `projects/{project}/locations/{location}/endpoints/{endpoint}`. +- */ +- endpoint?: string; +-} +- +-/** A tuned machine learning model. */ +-export declare interface TunedModelInfo { +- /** ID of the base model that you want to tune. */ +- baseModel?: string; +- /** Date and time when the base model was created. */ +- createTime?: string; +- /** Date and time when the base model was last updated. */ +- updateTime?: string; +-} +- +-/** Supervised fine-tuning training dataset. */ +-export declare interface TuningDataset { +- /** GCS URI of the file containing training dataset in JSONL format. */ +- gcsUri?: string; +- /** Inline examples with simple input/output text. */ +- examples?: TuningExample[]; +-} +- +-/** The tuning data statistic values for TuningJob. */ +-export declare interface TuningDataStats { +- /** Output only. Statistics for distillation. */ +- distillationDataStats?: DistillationDataStats; +- /** The SFT Tuning data stats. */ +- supervisedTuningDataStats?: SupervisedTuningDataStats; +-} +- +-export declare interface TuningExample { +- /** Text model input. */ +- textInput?: string; +- /** The expected model output. */ +- output?: string; +-} +- +-/** A tuning job. */ +-export declare interface TuningJob { +- /** Output only. Identifier. Resource name of a TuningJob. Format: `projects/{project}/locations/{location}/tuningJobs/{tuning_job}` */ +- name?: string; +- /** Output only. The detailed state of the job. */ +- state?: JobState; +- /** Output only. Time when the TuningJob was created. */ +- createTime?: string; +- /** Output only. Time when the TuningJob for the first time entered the `JOB_STATE_RUNNING` state. */ +- startTime?: string; +- /** Output only. Time when the TuningJob entered any of the following JobStates: `JOB_STATE_SUCCEEDED`, `JOB_STATE_FAILED`, `JOB_STATE_CANCELLED`, `JOB_STATE_EXPIRED`. */ +- endTime?: string; +- /** Output only. Time when the TuningJob was most recently updated. */ +- updateTime?: string; +- /** Output only. Only populated when job's state is `JOB_STATE_FAILED` or `JOB_STATE_CANCELLED`. */ +- error?: GoogleRpcStatus; +- /** Optional. The description of the TuningJob. */ +- description?: string; +- /** The base model that is being tuned, e.g., "gemini-1.0-pro-002". . */ +- baseModel?: string; +- /** Output only. The tuned model resources associated with this TuningJob. */ +- tunedModel?: TunedModel; +- /** Tuning Spec for Supervised Fine Tuning. */ +- supervisedTuningSpec?: SupervisedTuningSpec; +- /** Output only. The tuning data statistics associated with this TuningJob. */ +- tuningDataStats?: TuningDataStats; +- /** Customer-managed encryption key options for a TuningJob. If this is set, then all resources created by the TuningJob will be encrypted with the provided encryption key. */ +- encryptionSpec?: EncryptionSpec; +- /** Tuning Spec for open sourced and third party Partner models. */ +- partnerModelTuningSpec?: PartnerModelTuningSpec; +- /** Tuning Spec for Distillation. */ +- distillationSpec?: DistillationSpec; +- /** Output only. The Experiment associated with this TuningJob. */ +- experiment?: string; +- /** Optional. The labels with user-defined metadata to organize TuningJob and generated resources such as Model and Endpoint. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels. */ +- labels?: Record; +- /** Output only. The resource name of the PipelineJob associated with the TuningJob. Format: `projects/{project}/locations/{location}/pipelineJobs/{pipeline_job}`. */ +- pipelineJob?: string; +- /** Optional. The display name of the TunedModel. The name can be up to 128 characters long and can consist of any UTF-8 characters. */ +- tunedModelDisplayName?: string; +-} +- +-declare class Tunings extends BaseModule { +- private readonly apiClient; +- constructor(apiClient: ApiClient); +- /** +- * Gets a TuningJob. +- * +- * @param name - The resource name of the tuning job. +- * @return - A TuningJob object. +- * +- * @experimental - The SDK's tuning implementation is experimental, and may +- * change in future versions. +- */ +- get: (params: types.GetTuningJobParameters) => Promise; +- /** +- * Lists tuning jobs. +- * +- * @param config - The configuration for the list request. +- * @return - A list of tuning jobs. +- * +- * @experimental - The SDK's tuning implementation is experimental, and may +- * change in future versions. +- */ +- list: (params?: types.ListTuningJobsParameters) => Promise>; +- /** +- * Creates a supervised fine-tuning job. +- * +- * @param params - The parameters for the tuning job. +- * @return - A TuningJob operation. +- * +- * @experimental - The SDK's tuning implementation is experimental, and may +- * change in future versions. +- */ +- tune: (params: types.CreateTuningJobParameters) => Promise; +- private getInternal; +- private listInternal; +- private tuneInternal; +- private tuneMldevInternal; +-} +- +-export declare interface TuningValidationDataset { +- /** GCS URI of the file containing validation dataset in JSONL format. */ +- gcsUri?: string; +-} +- +-/** Options about which input is included in the user's turn. */ +-export declare enum TurnCoverage { +- /** +- * If unspecified, the default behavior is `TURN_INCLUDES_ONLY_ACTIVITY`. +- */ +- TURN_COVERAGE_UNSPECIFIED = "TURN_COVERAGE_UNSPECIFIED", +- /** +- * The users turn only includes activity since the last turn, excluding inactivity (e.g. silence on the audio stream). This is the default behavior. +- */ +- TURN_INCLUDES_ONLY_ACTIVITY = "TURN_INCLUDES_ONLY_ACTIVITY", +- /** +- * The users turn includes all realtime input since the last turn, including inactivity (e.g. silence on the audio stream). +- */ +- TURN_INCLUDES_ALL_INPUT = "TURN_INCLUDES_ALL_INPUT" +-} +- +-/** Optional. The type of the data. */ +-export declare enum Type { +- /** +- * Not specified, should not be used. +- */ +- TYPE_UNSPECIFIED = "TYPE_UNSPECIFIED", +- /** +- * OpenAPI string type +- */ +- STRING = "STRING", +- /** +- * OpenAPI number type +- */ +- NUMBER = "NUMBER", +- /** +- * OpenAPI integer type +- */ +- INTEGER = "INTEGER", +- /** +- * OpenAPI boolean type +- */ +- BOOLEAN = "BOOLEAN", +- /** +- * OpenAPI array type +- */ +- ARRAY = "ARRAY", +- /** +- * OpenAPI object type +- */ +- OBJECT = "OBJECT" +-} +- +-declare namespace types { +- export { +- createPartFromUri, +- createPartFromText, +- createPartFromFunctionCall, +- createPartFromFunctionResponse, +- createPartFromBase64, +- createPartFromCodeExecutionResult, +- createPartFromExecutableCode, +- createUserContent, +- createModelContent, +- Outcome, +- Language, +- HarmCategory, +- HarmBlockMethod, +- HarmBlockThreshold, +- Type, +- Mode, +- AuthType, +- FinishReason, +- HarmProbability, +- HarmSeverity, +- BlockedReason, +- TrafficType, +- Modality, +- MediaResolution, +- JobState, +- AdapterSize, +- FeatureSelectionPreference, +- Behavior, +- DynamicRetrievalConfigMode, +- FunctionCallingConfigMode, +- UrlRetrievalStatus, +- SafetyFilterLevel, +- PersonGeneration, +- ImagePromptLanguage, +- MaskReferenceMode, +- ControlReferenceType, +- SubjectReferenceType, +- EditMode, +- FileState, +- FileSource, +- MediaModality, +- StartSensitivity, +- EndSensitivity, +- ActivityHandling, +- TurnCoverage, +- FunctionResponseScheduling, +- Scale, +- MusicGenerationMode, +- LiveMusicPlaybackControl, +- VideoMetadata, +- Blob_2 as Blob, +- CodeExecutionResult, +- ExecutableCode, +- FileData, +- FunctionCall, +- FunctionResponse, +- Part, +- Content, +- HttpOptions, +- ModelSelectionConfig, +- SafetySetting, +- Schema, +- FunctionDeclaration, +- Interval, +- GoogleSearch, +- DynamicRetrievalConfig, +- GoogleSearchRetrieval, +- EnterpriseWebSearch, +- ApiKeyConfig, +- AuthConfigGoogleServiceAccountConfig, +- AuthConfigHttpBasicAuthConfig, +- AuthConfigOauthConfig, +- AuthConfigOidcConfig, +- AuthConfig, +- GoogleMaps, +- UrlContext, +- VertexAISearch, +- VertexRagStoreRagResource, +- RagRetrievalConfigFilter, +- RagRetrievalConfigHybridSearch, +- RagRetrievalConfigRankingLlmRanker, +- RagRetrievalConfigRankingRankService, +- RagRetrievalConfigRanking, +- RagRetrievalConfig, +- VertexRagStore, +- Retrieval, +- ToolCodeExecution, +- Tool, +- FunctionCallingConfig, +- LatLng, +- RetrievalConfig, +- ToolConfig, +- PrebuiltVoiceConfig, +- VoiceConfig, +- SpeakerVoiceConfig, +- MultiSpeakerVoiceConfig, +- SpeechConfig, +- AutomaticFunctionCallingConfig, +- ThinkingConfig, +- GenerationConfigRoutingConfigAutoRoutingMode, +- GenerationConfigRoutingConfigManualRoutingMode, +- GenerationConfigRoutingConfig, +- GenerateContentConfig, +- GenerateContentParameters, +- GoogleTypeDate, +- Citation, +- CitationMetadata, +- UrlMetadata, +- UrlContextMetadata, +- GroundingChunkRetrievedContext, +- GroundingChunkWeb, +- GroundingChunk, +- Segment, +- GroundingSupport, +- RetrievalMetadata, +- SearchEntryPoint, +- GroundingMetadata, +- LogprobsResultCandidate, +- LogprobsResultTopCandidates, +- LogprobsResult, +- SafetyRating, +- Candidate, +- GenerateContentResponsePromptFeedback, +- ModalityTokenCount, +- GenerateContentResponseUsageMetadata, +- GenerateContentResponse, +- ReferenceImage, +- EditImageParameters, +- EmbedContentConfig, +- EmbedContentParameters, +- ContentEmbeddingStatistics, +- ContentEmbedding, +- EmbedContentMetadata, +- EmbedContentResponse, +- GenerateImagesConfig, +- GenerateImagesParameters, +- Image_2 as Image, +- SafetyAttributes, +- GeneratedImage, +- GenerateImagesResponse, +- MaskReferenceConfig, +- ControlReferenceConfig, +- StyleReferenceConfig, +- SubjectReferenceConfig, +- EditImageConfig, +- EditImageResponse, +- UpscaleImageResponse, +- GetModelConfig, +- GetModelParameters, +- Endpoint, +- TunedModelInfo, +- Checkpoint, +- Model, +- ListModelsConfig, +- ListModelsParameters, +- ListModelsResponse, +- UpdateModelConfig, +- UpdateModelParameters, +- DeleteModelConfig, +- DeleteModelParameters, +- DeleteModelResponse, +- GenerationConfig, +- CountTokensConfig, +- CountTokensParameters, +- CountTokensResponse, +- ComputeTokensConfig, +- ComputeTokensParameters, +- TokensInfo, +- ComputeTokensResponse, +- GenerateVideosConfig, +- GenerateVideosParameters, +- Video, +- GeneratedVideo, +- GenerateVideosResponse, +- GenerateVideosOperation, +- GetTuningJobConfig, +- GetTuningJobParameters, +- TunedModelCheckpoint, +- TunedModel, +- GoogleRpcStatus, +- SupervisedHyperParameters, +- SupervisedTuningSpec, +- DatasetDistributionDistributionBucket, +- DatasetDistribution, +- DatasetStats, +- DistillationDataStats, +- SupervisedTuningDatasetDistributionDatasetBucket, +- SupervisedTuningDatasetDistribution, +- SupervisedTuningDataStats, +- TuningDataStats, +- EncryptionSpec, +- PartnerModelTuningSpec, +- DistillationHyperParameters, +- DistillationSpec, +- TuningJob, +- ListTuningJobsConfig, +- ListTuningJobsParameters, +- ListTuningJobsResponse, +- TuningExample, +- TuningDataset, +- TuningValidationDataset, +- CreateTuningJobConfig, +- CreateTuningJobParameters, +- Operation, +- CreateCachedContentConfig, +- CreateCachedContentParameters, +- CachedContentUsageMetadata, +- CachedContent, +- GetCachedContentConfig, +- GetCachedContentParameters, +- DeleteCachedContentConfig, +- DeleteCachedContentParameters, +- DeleteCachedContentResponse, +- UpdateCachedContentConfig, +- UpdateCachedContentParameters, +- ListCachedContentsConfig, +- ListCachedContentsParameters, +- ListCachedContentsResponse, +- ListFilesConfig, +- ListFilesParameters, +- FileStatus, +- File_2 as File, +- ListFilesResponse, +- CreateFileConfig, +- CreateFileParameters, +- HttpResponse, +- LiveCallbacks, +- CreateFileResponse, +- GetFileConfig, +- GetFileParameters, +- DeleteFileConfig, +- DeleteFileParameters, +- DeleteFileResponse, +- GetOperationConfig, +- GetOperationParameters, +- FetchPredictOperationConfig, +- FetchPredictOperationParameters, +- TestTableItem, +- TestTableFile, +- ReplayRequest, +- ReplayResponse, +- ReplayInteraction, +- ReplayFile, +- UploadFileConfig, +- DownloadFileConfig, +- DownloadFileParameters, +- UpscaleImageConfig, +- UpscaleImageParameters, +- RawReferenceImage, +- MaskReferenceImage, +- ControlReferenceImage, +- StyleReferenceImage, +- SubjectReferenceImage, +- LiveServerSetupComplete, +- Transcription, +- LiveServerContent, +- LiveServerToolCall, +- LiveServerToolCallCancellation, +- UsageMetadata, +- LiveServerGoAway, +- LiveServerSessionResumptionUpdate, +- LiveServerMessage, +- AutomaticActivityDetection, +- RealtimeInputConfig, +- SessionResumptionConfig, +- SlidingWindow, +- ContextWindowCompressionConfig, +- AudioTranscriptionConfig, +- ProactivityConfig, +- LiveClientSetup, +- LiveClientContent, +- ActivityStart, +- ActivityEnd, +- LiveClientRealtimeInput, +- LiveSendRealtimeInputParameters, +- LiveClientToolResponse, +- LiveClientMessage, +- LiveConnectConfig, +- LiveConnectParameters, +- CreateChatParameters, +- SendMessageParameters, +- LiveSendClientContentParameters, +- LiveSendToolResponseParameters, +- LiveMusicClientSetup, +- WeightedPrompt, +- LiveMusicClientContent, +- LiveMusicGenerationConfig, +- LiveMusicClientMessage, +- LiveMusicServerSetupComplete, +- LiveMusicSourceMetadata, +- AudioChunk, +- LiveMusicServerContent, +- LiveMusicFilteredPrompt, +- LiveMusicServerMessage, +- LiveMusicCallbacks, +- UploadFileParameters, +- CallableTool, +- CallableToolConfig, +- LiveMusicConnectParameters, +- LiveMusicSetConfigParameters, +- LiveMusicSetWeightedPromptsParameters, +- LiveEphemeralParameters, +- CreateAuthTokenConfig, +- OperationGetParameters, +- BlobImageUnion, +- PartUnion, +- PartListUnion, +- ContentUnion, +- ContentListUnion, +- SchemaUnion, +- SpeechConfigUnion, +- ToolUnion, +- ToolListUnion, +- DownloadableFileUnion +- } +-} +- +-/** Optional parameters for caches.update method. */ +-export declare interface UpdateCachedContentConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** The TTL for this resource. The expiration time is computed: now + TTL. It is a duration string, with up to nine fractional digits, terminated by 's'. Example: "3.5s". */ +- ttl?: string; +- /** Timestamp of when this resource is considered expired. Uses RFC 3339 format, Example: 2014-10-02T15:01:23Z. */ +- expireTime?: string; +-} +- +-export declare interface UpdateCachedContentParameters { +- /** The server-generated resource name of the cached content. +- */ +- name: string; +- /** Configuration that contains optional parameters. +- */ +- config?: UpdateCachedContentConfig; +-} +- +-/** Configuration for updating a tuned model. */ +-export declare interface UpdateModelConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- displayName?: string; +- description?: string; +- defaultCheckpointId?: string; +-} +- +-/** Configuration for updating a tuned model. */ +-export declare interface UpdateModelParameters { +- model: string; +- config?: UpdateModelConfig; +-} +- +-declare interface Uploader { +- /** +- * Uploads a file to the given upload url. +- * +- * @param file The file to upload. file is in string type or a Blob. +- * @param uploadUrl The upload URL as a string is where the file will be +- * uploaded to. The uploadUrl must be a url that was returned by the +- * https://generativelanguage.googleapis.com/upload/v1beta/files endpoint +- * @param apiClient The ApiClient to use for uploading. +- * @return A Promise that resolves to types.File. +- */ +- upload(file: string | Blob, uploadUrl: string, apiClient: ApiClient): Promise; +- /** +- * Returns the file's mimeType and the size of a given file. If the file is a +- * string path, the file type is determined by the file extension. If the +- * file's type cannot be determined, the type will be set to undefined. +- * +- * @param file The file to get the stat for. Can be a string path or a Blob. +- * @return A Promise that resolves to the file stat of the given file. +- */ +- stat(file: string | Blob): Promise; +-} +- +-/** Used to override the default configuration. */ +-export declare interface UploadFileConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** The name of the file in the destination (e.g., 'files/sample-image'. If not provided one will be generated. */ +- name?: string; +- /** mime_type: The MIME type of the file. If not provided, it will be inferred from the file extension. */ +- mimeType?: string; +- /** Optional display name of the file. */ +- displayName?: string; +-} +- +-/** Parameters for the upload file method. */ +-export declare interface UploadFileParameters { +- /** The string path to the file to be uploaded or a Blob object. */ +- file: string | globalThis.Blob; +- /** Configuration that contains optional parameters. */ +- config?: UploadFileConfig; +-} +- +-/** Configuration for upscaling an image. +- +- For more information on this configuration, refer to +- the `Imagen API reference documentation +- `_. +- */ +-export declare interface UpscaleImageConfig { +- /** Used to override HTTP request options. */ +- httpOptions?: HttpOptions; +- /** Abort signal which can be used to cancel the request. +- +- NOTE: AbortSignal is a client-only operation. Using it to cancel an +- operation will not cancel the request in the service. You will still +- be charged usage for any applicable operations. +- */ +- abortSignal?: AbortSignal; +- /** Whether to include a reason for filtered-out images in the +- response. */ +- includeRaiReason?: boolean; +- /** The image format that the output should be saved as. */ +- outputMimeType?: string; +- /** The level of compression if the ``output_mime_type`` is +- ``image/jpeg``. */ +- outputCompressionQuality?: number; +-} +- +-/** User-facing config UpscaleImageParameters. */ +-export declare interface UpscaleImageParameters { +- /** The model to use. */ +- model: string; +- /** The input image to upscale. */ +- image: Image_2; +- /** The factor to upscale the image (x2 or x4). */ +- upscaleFactor: string; +- /** Configuration for upscaling. */ +- config?: UpscaleImageConfig; +-} +- +-export declare class UpscaleImageResponse { +- /** Generated images. */ +- generatedImages?: GeneratedImage[]; +-} +- +-/** Tool to support URL context retrieval. */ +-export declare interface UrlContext { +-} +- +-/** Metadata related to url context retrieval tool. */ +-export declare interface UrlContextMetadata { +- /** List of url context. */ +- urlMetadata?: UrlMetadata[]; +-} +- +-/** Context for a single url retrieval. */ +-export declare interface UrlMetadata { +- /** The URL retrieved by the tool. */ +- retrievedUrl?: string; +- /** Status of the url retrieval. */ +- urlRetrievalStatus?: UrlRetrievalStatus; +-} +- +-/** Status of the url retrieval. */ +-export declare enum UrlRetrievalStatus { +- /** +- * Default value. This value is unused +- */ +- URL_RETRIEVAL_STATUS_UNSPECIFIED = "URL_RETRIEVAL_STATUS_UNSPECIFIED", +- /** +- * Url retrieval is successful. +- */ +- URL_RETRIEVAL_STATUS_SUCCESS = "URL_RETRIEVAL_STATUS_SUCCESS", +- /** +- * Url retrieval is failed due to error. +- */ +- URL_RETRIEVAL_STATUS_ERROR = "URL_RETRIEVAL_STATUS_ERROR" +-} +- +-/** Usage metadata about response(s). */ +-export declare interface UsageMetadata { +- /** Number of tokens in the prompt. When `cached_content` is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content. */ +- promptTokenCount?: number; +- /** Number of tokens in the cached part of the prompt (the cached content). */ +- cachedContentTokenCount?: number; +- /** Total number of tokens across all the generated response candidates. */ +- responseTokenCount?: number; +- /** Number of tokens present in tool-use prompt(s). */ +- toolUsePromptTokenCount?: number; +- /** Number of tokens of thoughts for thinking models. */ +- thoughtsTokenCount?: number; +- /** Total token count for prompt, response candidates, and tool-use prompts(if present). */ +- totalTokenCount?: number; +- /** List of modalities that were processed in the request input. */ +- promptTokensDetails?: ModalityTokenCount[]; +- /** List of modalities that were processed in the cache input. */ +- cacheTokensDetails?: ModalityTokenCount[]; +- /** List of modalities that were returned in the response. */ +- responseTokensDetails?: ModalityTokenCount[]; +- /** List of modalities that were processed in the tool-use prompt. */ +- toolUsePromptTokensDetails?: ModalityTokenCount[]; +- /** Traffic type. This shows whether a request consumes Pay-As-You-Go +- or Provisioned Throughput quota. */ +- trafficType?: TrafficType; +-} +- +-/** Retrieve from Vertex AI Search datastore or engine for grounding. datastore and engine are mutually exclusive. See https://cloud.google.com/products/agent-builder */ +-export declare interface VertexAISearch { +- /** Optional. Fully-qualified Vertex AI Search data store resource ID. Format: `projects/{project}/locations/{location}/collections/{collection}/dataStores/{dataStore}` */ +- datastore?: string; +- /** Optional. Fully-qualified Vertex AI Search engine resource ID. Format: `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}` */ +- engine?: string; +-} +- +-/** Retrieve from Vertex RAG Store for grounding. */ +-export declare interface VertexRagStore { +- /** Optional. Deprecated. Please use rag_resources instead. */ +- ragCorpora?: string[]; +- /** Optional. The representation of the rag source. It can be used to specify corpus only or ragfiles. Currently only support one corpus or multiple files from one corpus. In the future we may open up multiple corpora support. */ +- ragResources?: VertexRagStoreRagResource[]; +- /** Optional. The retrieval config for the Rag query. */ +- ragRetrievalConfig?: RagRetrievalConfig; +- /** Optional. Number of top k results to return from the selected corpora. */ +- similarityTopK?: number; +- /** Optional. Only return results with vector distance smaller than the threshold. */ +- vectorDistanceThreshold?: number; +-} +- +-/** The definition of the Rag resource. */ +-export declare interface VertexRagStoreRagResource { +- /** Optional. RagCorpora resource name. Format: `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}` */ +- ragCorpus?: string; +- /** Optional. rag_file_id. The files should be in the same rag_corpus set in rag_corpus field. */ +- ragFileIds?: string[]; +-} +- +-/** A generated video. */ +-export declare interface Video { +- /** Path to another storage. */ +- uri?: string; +- /** Video bytes. */ +- videoBytes?: string; +- /** Video encoding, for example "video/mp4". */ +- mimeType?: string; +-} +- +-/** Describes how the video in the Part should be used by the model. */ +-export declare interface VideoMetadata { +- /** The frame rate of the video sent to the model. If not specified, the +- default value will be 1.0. The fps range is (0.0, 24.0]. */ +- fps?: number; +- /** Optional. The end offset of the video. */ +- endOffset?: string; +- /** Optional. The start offset of the video. */ +- startOffset?: string; +-} +- +-/** The configuration for the voice to use. */ +-export declare interface VoiceConfig { +- /** The configuration for the speaker to use. +- */ +- prebuiltVoiceConfig?: PrebuiltVoiceConfig; +-} +- +-declare interface WebSocket_2 { +- /** +- * Connects the socket to the server. +- */ +- connect(): void; +- /** +- * Sends a message to the server. +- */ +- send(message: string): void; +- /** +- * Closes the socket connection. +- */ +- close(): void; +-} +- +-/** +- * @license +- * Copyright 2025 Google LLC +- * SPDX-License-Identifier: Apache-2.0 +- */ +-declare interface WebSocketCallbacks { +- onopen: () => void; +- onerror: (e: any) => void; +- onmessage: (e: any) => void; +- onclose: (e: any) => void; +-} +- +-declare interface WebSocketFactory { +- /** +- * Returns a new WebSocket instance. +- */ +- create(url: string, headers: Record, callbacks: WebSocketCallbacks): WebSocket_2; +-} +- +-/** Maps a prompt to a relative weight to steer music generation. */ +-export declare interface WeightedPrompt { +- /** Text prompt. */ +- text?: string; +- /** Weight of the prompt. The weight is used to control the relative +- importance of the prompt. Higher weights are more important than lower +- weights. +- +- Weight must not be 0. Weights of all weighted_prompts in this +- LiveMusicClientContent message will be normalized. */ +- weight?: number; +-} +- +-export { } diff --git a/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch b/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch new file mode 100644 index 0000000000..d5f7a89edb --- /dev/null +++ b/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch @@ -0,0 +1,69 @@ +diff --git a/es/dropdown/dropdown.js b/es/dropdown/dropdown.js +index 986877a762b9ad0aca596a8552732cd12d2eaabb..1f18aa2ea745e68950e4cee16d4d655f5c835fd5 100644 +--- a/es/dropdown/dropdown.js ++++ b/es/dropdown/dropdown.js +@@ -2,7 +2,7 @@ + + import * as React from 'react'; + import LeftOutlined from "@ant-design/icons/es/icons/LeftOutlined"; +-import RightOutlined from "@ant-design/icons/es/icons/RightOutlined"; ++import { ChevronRight } from 'lucide-react'; + import classNames from 'classnames'; + import RcDropdown from 'rc-dropdown'; + import useEvent from "rc-util/es/hooks/useEvent"; +@@ -158,8 +158,10 @@ const Dropdown = props => { + className: `${prefixCls}-menu-submenu-arrow` + }, direction === 'rtl' ? (/*#__PURE__*/React.createElement(LeftOutlined, { + className: `${prefixCls}-menu-submenu-arrow-icon` +- })) : (/*#__PURE__*/React.createElement(RightOutlined, { +- className: `${prefixCls}-menu-submenu-arrow-icon` ++ })) : (/*#__PURE__*/React.createElement(ChevronRight, { ++ size: 16, ++ strokeWidth: 1.8, ++ className: `${prefixCls}-menu-submenu-arrow-icon lucide-custom` + }))), + mode: "vertical", + selectable: false, +diff --git a/es/dropdown/style/index.js b/es/dropdown/style/index.js +index 768c01783002c6901c85a73061ff6b3e776a60ce..39b1b95a56cdc9fb586a193c3adad5141f5cf213 100644 +--- a/es/dropdown/style/index.js ++++ b/es/dropdown/style/index.js +@@ -240,7 +240,8 @@ const genBaseStyle = token => { + marginInlineEnd: '0 !important', + color: token.colorTextDescription, + fontSize: fontSizeIcon, +- fontStyle: 'normal' ++ fontStyle: 'normal', ++ marginTop: 3, + } + } + }), +diff --git a/es/select/useIcons.js b/es/select/useIcons.js +index 959115be936ef8901548af2658c5dcfdc5852723..c812edd52123eb0faf4638b1154fcfa1b05b513b 100644 +--- a/es/select/useIcons.js ++++ b/es/select/useIcons.js +@@ -4,10 +4,10 @@ import * as React from 'react'; + import CheckOutlined from "@ant-design/icons/es/icons/CheckOutlined"; + import CloseCircleFilled from "@ant-design/icons/es/icons/CloseCircleFilled"; + import CloseOutlined from "@ant-design/icons/es/icons/CloseOutlined"; +-import DownOutlined from "@ant-design/icons/es/icons/DownOutlined"; + import LoadingOutlined from "@ant-design/icons/es/icons/LoadingOutlined"; + import SearchOutlined from "@ant-design/icons/es/icons/SearchOutlined"; + import { devUseWarning } from '../_util/warning'; ++import { ChevronDown } from 'lucide-react'; + export default function useIcons(_ref) { + let { + suffixIcon, +@@ -56,8 +56,10 @@ export default function useIcons(_ref) { + className: iconCls + })); + } +- return getSuffixIconNode(/*#__PURE__*/React.createElement(DownOutlined, { +- className: iconCls ++ return getSuffixIconNode(/*#__PURE__*/React.createElement(ChevronDown, { ++ size: 16, ++ strokeWidth: 1.8, ++ className: `${iconCls} lucide-custom` + })); + }; + } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9338bc035d..408057252b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -[中文](./docs/CONTRIBUTING.zh.md) | [English](./CONTRIBUTING.md) +[中文](docs/CONTRIBUTING.zh.md) | [English](CONTRIBUTING.md) # Cherry Studio Contributor Guide @@ -58,6 +58,10 @@ git commit --signoff -m "Your commit message" Maintainers are here to help you implement your use case within a reasonable timeframe. They will do their best to review your code and provide constructive feedback promptly. However, if you get stuck during the review process or feel your Pull Request is not receiving the attention it deserves, please contact us via comments in the Issue or through the [Community](README.md#-community). +### 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). + ### Other Suggestions - **Contact Developers**: Before submitting a PR, you can contact the developers first to discuss or get help. diff --git a/README.md b/README.md index 7ce7ada20f..3594915f34 100644 --- a/README.md +++ b/README.md @@ -33,32 +33,22 @@ banner
-

English | 中文 | 日本語 | Official Site | Documents | Development | Feedback

- +

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

- + [![][deepwiki-shield]][deepwiki-link] [![][twitter-shield]][twitter-link] [![][discord-shield]][discord-link] [![][telegram-shield]][telegram-link]
- - -
- -[![][github-stars-shield]][github-stars-link] -[![][github-forks-shield]][github-forks-link] + [![][github-release-shield]][github-release-link] +[![][github-nightly-shield]][github-nightly-link] [![][github-contributors-shield]][github-contributors-link] - -
- -
- [![][license-shield]][license-link] [![][commercial-shield]][commercial-link] [![][sponsor-shield]][sponsor-link] @@ -66,9 +56,9 @@
- Featured|HelloGitHub - kangfenmao%2Fcherry-studio | Trendshift - Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt + Featured|HelloGitHub + kangfenmao%2Fcherry-studio | Trendshift + Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt
# 🍒 Cherry Studio @@ -193,10 +183,82 @@ Refer to the [Branching Strategy](docs/branching-strategy-en.md) for contributio 3. **Submit Changes**: Commit and push your changes. 4. **Open a Pull Request**: Describe your changes and reasons. -For more detailed guidelines, please refer to our [Contributing Guide](./CONTRIBUTING.md). +For more detailed guidelines, please refer to our [Contributing Guide](CONTRIBUTING.md). Thank you for your support and contributions! +# 🔧 Developer Co-creation Program + +We are launching the Cherry Studio Developer Co-creation Program to foster a healthy and positive-feedback loop within the open-source ecosystem. We believe that great software is built collaboratively, and every merged pull request breathes new life into the project. + +We sincerely invite you to join our ranks of contributors and shape the future of Cherry Studio with us. + +## Contributor Rewards Program + +To give back to our core contributors and create a virtuous cycle, we have established the following long-term incentive plan. + +**The inaugural tracking period for this program will be Q3 2025 (July, August, September). Rewards for this cycle will be distributed on October 1st.** + +Within any tracking period (e.g., July 1st to September 30th for the first cycle), any developer who contributes more than **30 meaningful commits** to any of Cherry Studio's open-source projects on GitHub is eligible for the following benefits: + +- **Cursor Subscription Sponsorship**: Receive a **$70 USD** credit or reimbursement for your [Cursor](https://cursor.sh/) subscription, making AI your most efficient coding partner. +- **Unlimited Model Access**: Get **unlimited** API calls for the **DeepSeek** and **Qwen** models. +- **Cutting-Edge Tech Access**: Enjoy occasional perks, including API access to models like **Claude**, **Gemini**, and **OpenAI**, keeping you at the forefront of technology. + +## Growing Together & Future Plans + +A vibrant community is the driving force behind any sustainable open-source project. As Cherry Studio grows, so will our rewards program. We are committed to continuously aligning our benefits with the best-in-class tools and resources in the industry. This ensures our core contributors receive meaningful support, creating a positive cycle where developers, the community, and the project grow together. + +**Moving forward, the project will also embrace an increasingly open stance to give back to the entire open-source community.** + +## How to Get Started? + +We look forward to your first Pull Request! + +You can start by exploring our repositories, picking up a `good first issue`, or proposing your own enhancements. Every commit is a testament to the spirit of open source. + +Thank you for your interest and contributions. + +Let's build together. + +# 🏢 Enterprise Edition + +Building on the Community Edition, we are proud to introduce **Cherry Studio Enterprise Edition**—a privately deployable AI productivity and management platform designed for modern teams and enterprises. + +The Enterprise Edition addresses core challenges in team collaboration by centralizing the management of AI resources, knowledge, and data. It empowers organizations to enhance efficiency, foster innovation, and ensure compliance, all while maintaining 100% control over their data in a secure environment. + +## Core Advantages + +- **Unified Model Management**: Centrally integrate and manage various cloud-based LLMs (e.g., OpenAI, Anthropic, Google Gemini) and locally deployed private models. Employees can use them out-of-the-box without individual configuration. +- **Enterprise-Grade Knowledge Base**: Build, manage, and share team-wide knowledge bases. Ensure knowledge is retained and consistent, enabling team members to interact with AI based on unified and accurate information. +- **Fine-Grained Access Control**: Easily manage employee accounts and assign role-based permissions for different models, knowledge bases, and features through a unified admin backend. +- **Fully Private Deployment**: Deploy the entire backend service on your on-premises servers or private cloud, ensuring your data remains 100% private and under your control to meet the strictest security and compliance standards. +- **Reliable Backend Services**: Provides stable API services, enterprise-grade data backup and recovery mechanisms to ensure business continuity. + +## ✨ Online Demo + +> 🚧 **Public Beta Notice** +> +> The Enterprise Edition is currently in its early public beta stage, and we are actively iterating and optimizing its features. We are aware that it may not be perfectly stable yet. If you encounter any issues or have valuable suggestions during your trial, we would be very grateful if you could contact us via email to provide feedback. + +**🔗 [Cherry Studio Enterprise](https://www.cherry-ai.com/enterprise)** + +## Version Comparison + +| Feature | Community Edition | Enterprise Edition | +| :---------------- | :----------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------- | +| **Open Source** | ✅ Yes | ⭕️ part. released to cust. | +| **Cost** | Free for Personal Use / Commercial License | Buyout / Subscription Fee | +| **Admin Backend** | — | ● Centralized **Model** Access
● **Employee** Management
● Shared **Knowledge Base**
● **Access** Control
● **Data** Backup | +| **Server** | — | ✅ Dedicated Private Deployment | + +## Get the Enterprise Edition + +We believe the Enterprise Edition will become your team's AI productivity engine. If you are interested in Cherry Studio Enterprise Edition and would like to learn more, request a quote, or schedule a demo, please contact us. + +- **For Business Inquiries & Purchasing**: + **📧 [bd@cherry-ai.com](mailto:bd@cherry-ai.com)** + # 🔗 Related Projects - [one-api](https://github.com/songquanpeng/one-api):LLM API management and distribution system, supporting mainstream models like OpenAI, Azure, and Anthropic. Features unified API interface, suitable for key management and secondary distribution. @@ -210,34 +272,45 @@ Thank you for your support and contributions!

+# 📊 GitHub Stats + +![Stats](https://repobeats.axiom.co/api/embed/a693f2e5f773eed620f70031e974552156c7f397.svg 'Repobeats analytics image') + # ⭐️ Star History -[![Star History Chart](https://api.star-history.com/svg?repos=CherryHQ/cherry-studio&type=Timeline)](https://star-history.com/#CherryHQ/cherry-studio&Timeline) + + + + + Star History Chart + + -[deepwiki-shield]: https://img.shields.io/badge/Deepwiki-CherryHQ-0088CC?style=plastic + +[deepwiki-shield]: https://img.shields.io/badge/Deepwiki-CherryHQ-0088CC?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNy45MyAzMiI+PHBhdGggZD0iTTE5LjMzIDE0LjEyYy42Ny0uMzkgMS41LS4zOSAyLjE4IDBsMS43NCAxYy4wNi4wMy4xMS4wNi4xOC4wN2guMDRjLjA2LjAzLjEyLjAzLjE4LjAzaC4wMmMuMDYgMCAuMTEgMCAuMTctLjAyaC4wM2MuMDYtLjAyLjEyLS4wNS4xNy0uMDhoLjAybDMuNDgtMi4wMWMuMjUtLjE0LjQtLjQxLjQtLjdWOC40YS44MS44MSAwIDAgMC0uNC0uN2wtMy40OC0yLjAxYS44My44MyAwIDAgMC0uODEgMEwxOS43NyA3LjdoLS4wMWwtLjE1LjEyLS4wMi4wMnMtLjA3LjA5LS4xLjE0VjhhLjQuNCAwIDAgMC0uMDguMTd2LjA0Yy0uMDMuMDYtLjAzLjEyLS4wMy4xOXYyLjAxYzAgLjc4LS40MSAxLjQ5LTEuMDkgMS44OC0uNjcuMzktMS41LjM5LTIuMTggMGwtMS43NC0xYS42LjYgMCAwIDAtLjIxLS4wOGMtLjA2LS4wMS0uMTItLjAyLS4xOC0uMDJoLS4wM2MtLjA2IDAtLjExLjAxLS4xNy4wMmgtLjAzYy0uMDYuMDItLjEyLjA0LS4xNy4wN2gtLjAybC0zLjQ3IDIuMDFjLS4yNS4xNC0uNC40MS0uNC43VjE4YzAgLjI5LjE1LjU1LjQuN2wzLjQ4IDIuMDFoLjAyYy4wNi4wNC4xMS4wNi4xNy4wOGguMDNjLjA1LjAyLjExLjAzLjE3LjAzaC4wMmMuMDYgMCAuMTIgMCAuMTgtLjAyaC4wNGMuMDYtLjAzLjEyLS4wNS4xOC0uMDhsMS43NC0xYy42Ny0uMzkgMS41LS4zOSAyLjE3IDBzMS4wOSAxLjExIDEuMDkgMS44OHYyLjAxYzAgLjA3IDAgLjEzLjAyLjE5di4wNGMuMDMuMDYuMDUuMTIuMDguMTd2LjAycy4wOC4wOS4xMi4xM2wuMDIuMDJzLjA5LjA4LjE1LjExYzAgMCAuMDEgMCAuMDEuMDFsMy40OCAyLjAxYy4yNS4xNC41Ni4xNC44MSAwbDMuNDgtMi4wMWMuMjUtLjE0LjQtLjQxLjQtLjd2LTQuMDFhLjgxLjgxIDAgMCAwLS40LS43bC0zLjQ4LTIuMDFoLS4wMmMtLjA1LS4wNC0uMTEtLjA2LS4xNy0uMDhoLS4wM2EuNS41IDAgMCAwLS4xNy0uMDNoLS4wM2MtLjA2IDAtLjEyIDAtLjE4LjAyLS4wNy4wMi0uMTUuMDUtLjIxLjA4bC0xLjc0IDFjLS42Ny4zOS0xLjUuMzktMi4xNyAwYTIuMTkgMi4xOSAwIDAgMS0xLjA5LTEuODhjMC0uNzguNDItMS40OSAxLjA5LTEuODhaIiBzdHlsZT0iZmlsbDojNWRiZjlkIi8+PHBhdGggZD0ibS40IDEzLjExIDMuNDcgMi4wMWMuMjUuMTQuNTYuMTQuOCAwbDMuNDctMi4wMWguMDFsLjE1LS4xMi4wMi0uMDJzLjA3LS4wOS4xLS4xNGwuMDItLjAyYy4wMy0uMDUuMDUtLjExLjA3LS4xN3YtLjA0Yy4wMy0uMDYuMDMtLjEyLjAzLS4xOVYxMC40YzAtLjc4LjQyLTEuNDkgMS4wOS0xLjg4czEuNS0uMzkgMi4xOCAwbDEuNzQgMWMuMDcuMDQuMTQuMDcuMjEuMDguMDYuMDEuMTIuMDIuMTguMDJoLjAzYy4wNiAwIC4xMS0uMDEuMTctLjAyaC4wM2MuMDYtLjAyLjEyLS4wNC4xNy0uMDdoLjAybDMuNDctMi4wMmMuMjUtLjE0LjQtLjQxLjQtLjd2LTRhLjgxLjgxIDAgMCAwLS40LS43bC0zLjQ2LTJhLjgzLjgzIDAgMCAwLS44MSAwbC0zLjQ4IDIuMDFoLS4wMWwtLjE1LjEyLS4wMi4wMi0uMS4xMy0uMDIuMDJjLS4wMy4wNS0uMDUuMTEtLjA3LjE3di4wNGMtLjAzLjA2LS4wMy4xMi0uMDMuMTl2Mi4wMWMwIC43OC0uNDIgMS40OS0xLjA5IDEuODhzLTEuNS4zOS0yLjE4IDBsLTEuNzQtMWEuNi42IDAgMCAwLS4yMS0uMDhjLS4wNi0uMDEtLjEyLS4wMi0uMTgtLjAyaC0uMDNjLS4wNiAwLS4xMS4wMS0uMTcuMDJoLS4wM2MtLjA2LjAyLS4xMi4wNS0uMTcuMDhoLS4wMkwuNCA3LjcxYy0uMjUuMTQtLjQuNDEtLjQuNjl2NC4wMWMwIC4yOS4xNS41Ni40LjciIHN0eWxlPSJmaWxsOiM0NDY4YzQiLz48cGF0aCBkPSJtMTcuODQgMjQuNDgtMy40OC0yLjAxaC0uMDJjLS4wNS0uMDQtLjExLS4wNi0uMTctLjA4aC0uMDNhLjUuNSAwIDAgMC0uMTctLjAzaC0uMDNjLS4wNiAwLS4xMiAwLS4xOC4wMmgtLjA0Yy0uMDYuMDMtLjEyLjA1LS4xOC4wOGwtMS43NCAxYy0uNjcuMzktMS41LjM5LTIuMTggMGEyLjE5IDIuMTkgMCAwIDEtMS4wOS0xLjg4di0yLjAxYzAtLjA2IDAtLjEzLS4wMi0uMTl2LS4wNGMtLjAzLS4wNi0uMDUtLjExLS4wOC0uMTdsLS4wMi0uMDJzLS4wNi0uMDktLjEtLjEzTDguMjkgMTlzLS4wOS0uMDgtLjE1LS4xMWgtLjAxbC0zLjQ3LTIuMDJhLjgzLjgzIDAgMCAwLS44MSAwTC4zNyAxOC44OGEuODcuODcgMCAwIDAtLjM3LjcxdjQuMDFjMCAuMjkuMTUuNTUuNC43bDMuNDcgMi4wMWguMDJjLjA1LjA0LjExLjA2LjE3LjA4aC4wM2MuMDUuMDIuMTEuMDMuMTYuMDNoLjAzYy4wNiAwIC4xMiAwIC4xOC0uMDJoLjA0Yy4wNi0uMDMuMTItLjA1LjE4LS4wOGwxLjc0LTFjLjY3LS4zOSAxLjUtLjM5IDIuMTcgMHMxLjA5IDEuMTEgMS4wOSAxLjg4djIuMDFjMCAuMDcgMCAuMTMuMDIuMTl2LjA0Yy4wMy4wNi4wNS4xMS4wOC4xN2wuMDIuMDJzLjA2LjA5LjEuMTRsLjAyLjAycy4wOS4wOC4xNS4xMWguMDFsMy40OCAyLjAyYy4yNS4xNC41Ni4xNC44MSAwbDMuNDgtMi4wMWMuMjUtLjE0LjQtLjQxLjQtLjdWMjUuMmEuODEuODEgMCAwIDAtLjQtLjdaIiBzdHlsZT0iZmlsbDojNDI5M2Q5Ii8+PC9zdmc+ [deepwiki-link]: https://deepwiki.com/CherryHQ/cherry-studio -[twitter-shield]: https://img.shields.io/badge/Twitter-CherryStudioApp-0088CC?style=plastic&logo=x +[twitter-shield]: https://img.shields.io/badge/Twitter-CherryStudioApp-0088CC?logo=x [twitter-link]: https://twitter.com/CherryStudioHQ -[discord-shield]: https://img.shields.io/badge/Discord-@CherryStudio-0088CC?style=plastic&logo=discord +[discord-shield]: https://img.shields.io/badge/Discord-@CherryStudio-0088CC?logo=discord [discord-link]: https://discord.gg/wez8HtpxqQ -[telegram-shield]: https://img.shields.io/badge/Telegram-@CherryStudioAI-0088CC?style=plastic&logo=telegram +[telegram-shield]: https://img.shields.io/badge/Telegram-@CherryStudioAI-0088CC?logo=telegram [telegram-link]: https://t.me/CherryStudioAI -[github-stars-shield]: https://img.shields.io/github/stars/CherryHQ/cherry-studio?style=social -[github-stars-link]: https://github.com/CherryHQ/cherry-studio/stargazers -[github-forks-shield]: https://img.shields.io/github/forks/CherryHQ/cherry-studio?style=social -[github-forks-link]: https://github.com/CherryHQ/cherry-studio/network -[github-release-shield]: https://img.shields.io/github/v/release/CherryHQ/cherry-studio + +[github-release-shield]: https://img.shields.io/github/v/release/CherryHQ/cherry-studio?logo=github [github-release-link]: https://github.com/CherryHQ/cherry-studio/releases -[github-contributors-shield]: https://img.shields.io/github/contributors/CherryHQ/cherry-studio +[github-nightly-shield]: https://img.shields.io/github/actions/workflow/status/CherryHQ/cherry-studio/nightly-build.yml?label=nightly%20build&logo=github +[github-nightly-link]: https://github.com/CherryHQ/cherry-studio/actions/workflows/nightly-build.yml +[github-contributors-shield]: https://img.shields.io/github/contributors/CherryHQ/cherry-studio?logo=github [github-contributors-link]: https://github.com/CherryHQ/cherry-studio/graphs/contributors -[license-shield]: https://img.shields.io/badge/License-AGPLv3-important.svg?style=plastic&logo=gnu + +[license-shield]: https://img.shields.io/badge/License-AGPLv3-important.svg?logo=gnu [license-link]: https://www.gnu.org/licenses/agpl-3.0 -[commercial-shield]: https://img.shields.io/badge/License-Contact-white.svg?style=plastic&logoColor=white&logo=telegram&color=blue +[commercial-shield]: https://img.shields.io/badge/License-Contact-white.svg?logoColor=white&logo=telegram&color=blue [commercial-link]: mailto:license@cherry-ai.com?subject=Commercial%20License%20Inquiry -[sponsor-shield]: https://img.shields.io/badge/Sponsor-FF6699.svg?style=plastic&logo=githubsponsors&logoColor=white +[sponsor-shield]: https://img.shields.io/badge/Sponsor-FF6699.svg?logo=githubsponsors&logoColor=white [sponsor-link]: https://github.com/CherryHQ/cherry-studio/blob/main/docs/sponsor.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000000..f3be8e0c90 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,64 @@ +# Security Policy + +## 📢 Reporting a Vulnerability + +At Cherry Studio, we take security seriously and appreciate your efforts to responsibly disclose vulnerabilities. If you discover a security issue, please report it as soon as possible. + +**Please do not create public issues for security-related reports.** + +- To report a security issue, please use the GitHub Security Advisories tab to "[Open a draft security advisory](https://github.com/CherryHQ/cherry-studio/security/advisories/new)". +- Include a detailed description of the issue, steps to reproduce, potential impact, and any possible mitigations. +- If applicable, please also attach proof-of-concept code or screenshots. + +We will acknowledge your report within **72 hours** and provide a status update as we investigate. + +--- + +## 🔒 Supported Versions + +We aim to support the latest released version and one previous minor release. + +| Version | Supported | +|-----------------|--------------------| +| Latest (`main`) | ✅ Supported | +| Previous minor | ✅ Supported | +| Older versions | ❌ Not supported | + +If you are using an unsupported version, we strongly recommend updating to the latest release to receive security fixes. + +--- + +## 💡 Security Measures + +Cherry Studio integrates several security best practices, including: + +- Strict dependency updates and regular vulnerability scanning. +- TypeScript strict mode and linting to reduce potential injection or runtime issues. +- Enforced code formatting and pre-commit hooks. +- Internal security reviews before releases. +- Dedicated MCP (Model Context Protocol) safeguards for model interactions and data privacy. + +--- + +## 🛡️ Disclosure Policy + +- We follow a **coordinated disclosure** approach. +- We will not publicly disclose vulnerabilities until a fix has been developed and released. +- Credit will be given to researchers who responsibly disclose vulnerabilities, if requested. + +--- + +## 🤝 Acknowledgements + +We greatly appreciate contributions from the security community and strive to recognize all researchers who help keep Cherry Studio safe. + +--- + +## 🌟 Questions? + +For any security-related questions not involving vulnerabilities, please reach out to: +**security@cherry-ai.com** + +--- + +Thank you for helping keep Cherry Studio and its users secure! diff --git a/docs/CONTRIBUTING.zh.md b/docs/CONTRIBUTING.zh.md index 30b1983d18..7574990cd4 100644 --- a/docs/CONTRIBUTING.zh.md +++ b/docs/CONTRIBUTING.zh.md @@ -1,6 +1,6 @@ # Cherry Studio 贡献者指南 -[**English**](../CONTRIBUTING.md) | [**中文**](./CONTRIBUTING.zh.md) +[**English**](../CONTRIBUTING.md) | [**中文**](CONTRIBUTING.zh.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。请参阅[开发者指南](docs/dev.md#test)中的“Test”部分。 +未经测试的功能等同于不存在。为确保代码真正有效,应通过单元测试和功能测试覆盖相关流程。因此,在考虑贡献时,也请考虑可测试性。所有测试均可本地运行,无需依赖 CI。请参阅[开发者指南](dev.md#test)中的“Test”部分。 ### 拉取请求的自动化测试 @@ -60,7 +60,11 @@ git commit --signoff -m "Your commit message" ### 获取代码审查/合并 -维护者在此帮助您在合理时间内实现您的用例。他们会尽力在合理时间内审查您的代码并提供建设性反馈。但如果您在审查过程中受阻,或认为您的 Pull Request 未得到应有的关注,请通过 Issue 中的评论或者[社群](README.md#-community)联系我们 +维护者在此帮助您在合理时间内实现您的用例。他们会尽力在合理时间内审查您的代码并提供建设性反馈。但如果您在审查过程中受阻,或认为您的 Pull Request 未得到应有的关注,请通过 Issue 中的评论或者[社群](README.zh.md#-community)联系我们 + +### 参与测试计划 + +测试计划旨在为用户提供更稳定的应用体验和更快的迭代速度,详细情况请参阅[测试计划](testplan-zh.md)。 ### 其他建议 diff --git a/docs/README.ja.md b/docs/README.ja.md deleted file mode 100644 index 1278edec86..0000000000 --- a/docs/README.ja.md +++ /dev/null @@ -1,215 +0,0 @@ -

- - banner
-
-

-

- English | 中文 | 日本語 | 公式サイト | ドキュメント | 開発 | フィードバック
-

- - - -
- -[![][deepwiki-shield]][deepwiki-link] -[![][twitter-shield]][twitter-link] -[![][discord-shield]][discord-link] -[![][telegram-shield]][telegram-link] - -
- - - -
- -[![][github-stars-shield]][github-stars-link] -[![][github-forks-shield]][github-forks-link] -[![][github-release-shield]][github-release-link] -[![][github-contributors-shield]][github-contributors-link] - -
- -
- -[![][license-shield]][license-link] -[![][commercial-shield]][commercial-link] -[![][sponsor-shield]][sponsor-link] - -
- -
- Featured|HelloGitHub - kangfenmao%2Fcherry-studio | Trendshift - Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt -
- -# 🍒 Cherry Studio - -Cherry Studio は、複数の LLM プロバイダーをサポートするデスクトップクライアントで、Windows、Mac、Linux で利用可能です。 - -👏 [Telegram](https://t.me/CherryStudioAI)|[Discord](https://discord.gg/wez8HtpxqQ) | [QQグループ(575014769)](https://qm.qq.com/q/lo0D4qVZKi) - -❤️ Cherry Studio をお気に入りにしましたか?小さな星をつけてください 🌟 または [スポンサー](sponsor.md) をして開発をサポートしてください! - -# 🌠 スクリーンショット - -![](https://github.com/user-attachments/assets/36dddb2c-e0fb-4a5f-9411-91447bab6e18) - -![](https://github.com/user-attachments/assets/f549e8a0-2385-40b4-b52b-2039e39f2930) - -![](https://github.com/user-attachments/assets/58e0237c-4d36-40de-b428-53051d982026) - -# 🌟 主な機能 - -1. **多様な LLM サービス対応**: - -- ☁️ 主要な LLM クラウドサービス対応:OpenAI、Gemini、Anthropic など -- 🔗 AI Web サービス統合:Claude、Peplexity、Poe など -- 💻 Ollama、LM Studio によるローカルモデル実行対応 - -2. **AI アシスタントと対話**: - -- 📚 300+ の事前設定済み AI アシスタント -- 🤖 カスタム AI アシスタントの作成 -- 💬 複数モデルでの同時対話機能 - -3. **文書とデータ処理**: - -- 📄 テキスト、画像、Office、PDF など多様な形式対応 -- ☁️ WebDAV によるファイル管理とバックアップ -- 📊 Mermaid による図表作成 -- 💻 コードハイライト機能 - -4. **実用的なツール統合**: - -- 🔍 グローバル検索機能 -- 📝 トピック管理システム -- 🔤 AI による翻訳機能 -- 🎯 ドラッグ&ドロップによる整理 -- 🔌 ミニプログラム対応 -- ⚙️ MCP(モデルコンテキストプロトコル)サービス - -5. **優れたユーザー体験**: - -- 🖥️ Windows、Mac、Linux のクロスプラットフォーム対応 -- 📦 環境構築不要ですぐに使用可能 -- 🎨 ライト/ダークテーマと透明ウィンドウ対応 -- 📝 完全な Markdown レンダリング -- 🤲 簡単な共有機能 - -# 📝 開発計画 - -以下の機能と改善に積極的に取り組んでいます: - -1. 🎯 **コア機能** - -- 選択アシスタント - スマートな内容選択の強化 -- ディープリサーチ - 高度な研究能力 -- メモリーシステム - グローバルコンテキスト認識 -- ドキュメント前処理 - 文書処理の改善 -- MCP マーケットプレイス - モデルコンテキストプロトコルエコシステム - -2. 🗂 **ナレッジ管理** - -- ノートとコレクション -- ダイナミックキャンバス可視化 -- OCR 機能 -- TTS(テキスト読み上げ)サポート - -3. 📱 **プラットフォーム対応** - -- HarmonyOS エディション -- Android アプリ(フェーズ1) -- iOS アプリ(フェーズ1) -- マルチウィンドウ対応 -- ウィンドウピン留め機能 - -4. 🔌 **高度な機能** - -- プラグインシステム -- ASR(音声認識) -- アシスタントとトピックの対話機能リファクタリング - -[プロジェクトボード](https://github.com/orgs/CherryHQ/projects/7)で進捗を確認し、貢献することができます。 - -開発計画に影響を与えたいですか?[GitHub ディスカッション](https://github.com/CherryHQ/cherry-studio/discussions)に参加して、アイデアやフィードバックを共有してください! - -# 🌈 テーマ - -- テーマギャラリー:https://cherrycss.com -- Aero テーマ:https://github.com/hakadao/CherryStudio-Aero -- PaperMaterial テーマ:https://github.com/rainoffallingstar/CherryStudio-PaperMaterial -- Claude テーマ:https://github.com/bjl101501/CherryStudio-Claudestyle-dynamic -- メープルネオンテーマ:https://github.com/BoningtonChen/CherryStudio_themes - -より多くのテーマの PR を歓迎します - -# 🤝 貢献 - -Cherry Studio への貢献を歓迎します!以下の方法で貢献できます: - -1. **コードの貢献**:新機能を開発するか、既存のコードを最適化します -2. **バグの修正**:見つけたバグを修正します -3. **問題の管理**:GitHub の問題を管理するのを手伝います -4. **製品デザイン**:デザインの議論に参加します -5. **ドキュメントの作成**:ユーザーマニュアルやガイドを改善します -6. **コミュニティの参加**:ディスカッションに参加し、ユーザーを支援します -7. **使用の促進**:Cherry Studio を広めます - -[ブランチ戦略](branching-strategy-en.md)を参照して貢献ガイドラインを確認してください - -## 始め方 - -1. **リポジトリをフォーク**:フォークしてローカルマシンにクローンします -2. **ブランチを作成**:変更のためのブランチを作成します -3. **変更を提出**:変更をコミットしてプッシュします -4. **プルリクエストを開く**:変更内容と理由を説明します - -詳細なガイドラインについては、[貢献ガイド](../CONTRIBUTING.md)をご覧ください。 - -ご支援と貢献に感謝します! - -# 🔗 関連プロジェクト - -- [one-api](https://github.com/songquanpeng/one-api):LLM API の管理・配信システム。OpenAI、Azure、Anthropic などの主要モデルに対応し、統一 API インターフェースを提供。API キー管理と再配布に利用可能。 - -- [ublacklist](https://github.com/iorate/ublacklist):Google 検索結果から特定のサイトを非表示にします - -# 🚀 コントリビューター - - - - -

- -# ⭐️ スター履歴 - -[![Star History Chart](https://api.star-history.com/svg?repos=CherryHQ/cherry-studio&type=Timeline)](https://star-history.com/#CherryHQ/cherry-studio&Timeline) - - -[deepwiki-shield]: https://img.shields.io/badge/Deepwiki-CherryHQ-0088CC?style=plastic -[deepwiki-link]: https://deepwiki.com/CherryHQ/cherry-studio -[twitter-shield]: https://img.shields.io/badge/Twitter-CherryStudioApp-0088CC?style=plastic&logo=x -[twitter-link]: https://twitter.com/CherryStudioHQ -[discord-shield]: https://img.shields.io/badge/Discord-@CherryStudio-0088CC?style=plastic&logo=discord -[discord-link]: https://discord.gg/wez8HtpxqQ -[telegram-shield]: https://img.shields.io/badge/Telegram-@CherryStudioAI-0088CC?style=plastic&logo=telegram -[telegram-link]: https://t.me/CherryStudioAI - - -[github-stars-shield]: https://img.shields.io/github/stars/CherryHQ/cherry-studio?style=social -[github-stars-link]: https://github.com/CherryHQ/cherry-studio/stargazers -[github-forks-shield]: https://img.shields.io/github/forks/CherryHQ/cherry-studio?style=social -[github-forks-link]: https://github.com/CherryHQ/cherry-studio/network -[github-release-shield]: https://img.shields.io/github/v/release/CherryHQ/cherry-studio -[github-release-link]: https://github.com/CherryHQ/cherry-studio/releases -[github-contributors-shield]: https://img.shields.io/github/contributors/CherryHQ/cherry-studio -[github-contributors-link]: https://github.com/CherryHQ/cherry-studio/graphs/contributors - - -[license-shield]: https://img.shields.io/badge/License-AGPLv3-important.svg?style=plastic&logo=gnu -[license-link]: https://www.gnu.org/licenses/agpl-3.0 -[commercial-shield]: https://img.shields.io/badge/商用ライセンス-お問い合わせ-white.svg?style=plastic&logoColor=white&logo=telegram&color=blue -[commercial-link]: mailto:license@cherry-ai.com?subject=商業ライセンスについて -[sponsor-shield]: https://img.shields.io/badge/スポンサー-FF6699.svg?style=plastic&logo=githubsponsors&logoColor=white -[sponsor-link]: https://github.com/CherryHQ/cherry-studio/blob/main/docs/sponsor.md diff --git a/docs/README.zh.md b/docs/README.zh.md index 1ca483fd19..774db66627 100644 --- a/docs/README.zh.md +++ b/docs/README.zh.md @@ -1,10 +1,40 @@ + +

banner

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

@@ -18,19 +48,10 @@ - -
-[![][github-stars-shield]][github-stars-link] -[![][github-forks-shield]][github-forks-link] [![][github-release-shield]][github-release-link] [![][github-contributors-shield]][github-contributors-link] - -
- -
- [![][license-shield]][license-link] [![][commercial-shield]][commercial-link] [![][sponsor-shield]][sponsor-link] @@ -38,9 +59,9 @@
- Featured|HelloGitHub - kangfenmao%2Fcherry-studio | Trendshift - Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt + Featured|HelloGitHub + kangfenmao%2Fcherry-studio | Trendshift + Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt
# 🍒 Cherry Studio @@ -51,14 +72,6 @@ Cherry Studio 是一款支持多个大语言模型(LLM)服务商的桌面客 ❤️ 喜欢 Cherry Studio? 点亮小星星 🌟 或 [赞助开发者](sponsor.md)! ❤️ -# GitCode✖️Cherry Studio【新源力】贡献挑战赛 - -

- - banner - -

- # 📖 使用教程 https://docs.cherry-ai.com @@ -177,10 +190,82 @@ https://docs.cherry-ai.com 3. **提交更改**:提交并推送您的更改 4. **打开 Pull Request**:描述您的更改和原因 -有关更详细的指南,请参阅我们的 [贡献指南](./CONTRIBUTING.zh.md) +有关更详细的指南,请参阅我们的 [贡献指南](CONTRIBUTING.zh.md) 感谢您的支持和贡献! +# 🔧 开发者共创计划 + +我们正在启动 Cherry Studio 开发者共创计划,旨在为开源生态系统构建一个健康、正向反馈的循环。我们相信,优秀的软件是通过协作构建的,每一个合并的拉取请求都为项目注入新的生命力。 + +我们诚挚地邀请您加入我们的贡献者队伍,与我们一起塑造 Cherry Studio 的未来。 + +## 贡献者奖励计划 + +为了回馈我们的核心贡献者并创造良性循环,我们建立了以下长期激励计划。 + +**该计划的首个跟踪周期将是 2025 年第三季度(7月、8月、9月)。此周期的奖励将在 10月1日 发放。** + +在任何跟踪周期内(例如,首个周期的 7月1日 至 9月30日),任何为 Cherry Studio 在 GitHub 上的开源项目贡献超过 **30 个有意义提交** 的开发者都有资格获得以下福利: + +- **Cursor 订阅赞助**:获得 **70 美元** 的 [Cursor](https://cursor.sh/) 订阅积分或报销,让 AI 成为您最高效的编码伙伴。 +- **无限模型访问**:获得 **DeepSeek** 和 **Qwen** 模型的 **无限次** API 调用。 +- **前沿技术访问**:享受偶尔的特殊福利,包括 **Claude**、**Gemini** 和 **OpenAI** 等模型的 API 访问权限,让您始终站在技术前沿。 + +## 共同成长与未来规划 + +活跃的社区是任何可持续开源项目背后的推动力。随着 Cherry Studio 的发展,我们的奖励计划也将随之发展。我们致力于持续将我们的福利与行业内最优秀的工具和资源保持一致。这确保我们的核心贡献者获得有意义的支持,创造一个开发者、社区和项目共同成长的正向循环。 + +**展望未来,该项目还将采取越来越开放的态度来回馈整个开源社区。** + +## 如何开始? + +我们期待您的第一个拉取请求! + +您可以从探索我们的仓库开始,选择一个 `good first issue`,或者提出您自己的改进建议。每一个提交都是开源精神的体现。 + +感谢您的关注和贡献。 + +让我们一起建设。 + +# 🏢 企业版 + +在社区版的基础上,我们自豪地推出 **Cherry Studio 企业版**——一个为现代团队和企业设计的私有部署 AI 生产力与管理平台。 + +企业版通过集中管理 AI 资源、知识和数据,解决了团队协作中的核心挑战。它赋能组织提升效率、促进创新并确保合规,同时在安全环境中保持对数据的 100% 控制。 + +## 核心优势 + +- **统一模型管理**:集中整合和管理各种基于云的大语言模型(如 OpenAI、Anthropic、Google Gemini)和本地部署的私有模型。员工可以开箱即用,无需单独配置。 +- **企业级知识库**:构建、管理和分享全团队的知识库。确保知识得到保留且一致,使团队成员能够基于统一准确的信息与 AI 交互。 +- **细粒度访问控制**:通过统一的管理后台轻松管理员工账户,并为不同模型、知识库和功能分配基于角色的权限。 +- **完全私有部署**:在您的本地服务器或私有云上部署整个后端服务,确保您的数据 100% 私有且在您的控制之下,满足最严格的安全和合规标准。 +- **可靠的后端服务**:提供稳定的 API 服务、企业级数据备份和恢复机制,确保业务连续性。 + +## ✨ 在线演示 + +> 🚧 **公开测试版通知** +> +> 企业版目前处于早期公开测试阶段,我们正在积极迭代和优化其功能。我们知道它可能还不够完全稳定。如果您在试用过程中遇到任何问题或有宝贵建议,我们非常感谢您能通过邮件联系我们提供反馈。 + +**🔗 [Cherry Studio 企业版](https://www.cherry-ai.com/enterprise)** + +## 版本对比 + +| 功能 | 社区版 | 企业版 | +| :----------- | :---------------------- | :--------------------------------------------------------------------------------------------- | +| **开源** | ✅ 是 | ⭕️ 部分开源,对客户开放 | +| **成本** | 个人使用免费 / 商业授权 | 买断 / 订阅费用 | +| **管理后台** | — | ● 集中化**模型**访问
● **员工**管理
● 共享**知识库**
● **访问**控制
● **数据**备份 | +| **服务器** | — | ✅ 专用私有部署 | + +## 获取企业版 + +我们相信企业版将成为您团队的 AI 生产力引擎。如果您对 Cherry Studio 企业版感兴趣,希望了解更多信息、请求报价或安排演示,请联系我们。 + +- **商业咨询与购买**: + **📧 [bd@cherry-ai.com](mailto:bd@cherry-ai.com)** + # 🔗 相关项目 - [one-api](https://github.com/songquanpeng/one-api):LLM API 管理及分发系统,支持 OpenAI、Azure、Anthropic 等主流模型,统一 API 接口,可用于密钥管理与二次分发。 @@ -194,34 +279,43 @@ https://docs.cherry-ai.com

+# 📊 GitHub 统计 + +![Stats](https://repobeats.axiom.co/api/embed/a693f2e5f773eed620f70031e974552156c7f397.svg 'Repobeats analytics image') + # ⭐️ Star 记录 -[![Star History Chart](https://api.star-history.com/svg?repos=CherryHQ/cherry-studio&type=Timeline)](https://star-history.com/#CherryHQ/cherry-studio&Timeline) + + + + + Star History Chart + + -[deepwiki-shield]: https://img.shields.io/badge/Deepwiki-CherryHQ-0088CC?style=plastic + +[deepwiki-shield]: https://img.shields.io/badge/Deepwiki-CherryHQ-0088CC [deepwiki-link]: https://deepwiki.com/CherryHQ/cherry-studio -[twitter-shield]: https://img.shields.io/badge/Twitter-CherryStudioApp-0088CC?style=plastic&logo=x +[twitter-shield]: https://img.shields.io/badge/Twitter-CherryStudioApp-0088CC?logo=x [twitter-link]: https://twitter.com/CherryStudioHQ -[discord-shield]: https://img.shields.io/badge/Discord-@CherryStudio-0088CC?style=plastic&logo=discord +[discord-shield]: https://img.shields.io/badge/Discord-@CherryStudio-0088CC?logo=discord [discord-link]: https://discord.gg/wez8HtpxqQ -[telegram-shield]: https://img.shields.io/badge/Telegram-@CherryStudioAI-0088CC?style=plastic&logo=telegram +[telegram-shield]: https://img.shields.io/badge/Telegram-@CherryStudioAI-0088CC?logo=telegram [telegram-link]: https://t.me/CherryStudioAI -[github-stars-shield]: https://img.shields.io/github/stars/CherryHQ/cherry-studio?style=social -[github-stars-link]: https://github.com/CherryHQ/cherry-studio/stargazers -[github-forks-shield]: https://img.shields.io/github/forks/CherryHQ/cherry-studio?style=social -[github-forks-link]: https://github.com/CherryHQ/cherry-studio/network + [github-release-shield]: https://img.shields.io/github/v/release/CherryHQ/cherry-studio [github-release-link]: https://github.com/CherryHQ/cherry-studio/releases [github-contributors-shield]: https://img.shields.io/github/contributors/CherryHQ/cherry-studio [github-contributors-link]: https://github.com/CherryHQ/cherry-studio/graphs/contributors -[license-shield]: https://img.shields.io/badge/License-AGPLv3-important.svg?style=plastic&logo=gnu + +[license-shield]: https://img.shields.io/badge/License-AGPLv3-important.svg?logo=gnu [license-link]: https://www.gnu.org/licenses/agpl-3.0 -[commercial-shield]: https://img.shields.io/badge/商用授权-联系-white.svg?style=plastic&logoColor=white&logo=telegram&color=blue +[commercial-shield]: https://img.shields.io/badge/商用授权-联系-white.svg?logoColor=white&logo=telegram&color=blue [commercial-link]: mailto:license@cherry-ai.com?subject=商业授权咨询 -[sponsor-shield]: https://img.shields.io/badge/赞助支持-FF6699.svg?style=plastic&logo=githubsponsors&logoColor=white +[sponsor-shield]: https://img.shields.io/badge/赞助支持-FF6699.svg?logo=githubsponsors&logoColor=white [sponsor-link]: https://github.com/CherryHQ/cherry-studio/blob/main/docs/sponsor.md diff --git a/docs/branching-strategy-en.md b/docs/branching-strategy-en.md index f3b7ddf508..8e646249ad 100644 --- a/docs/branching-strategy-en.md +++ b/docs/branching-strategy-en.md @@ -16,6 +16,8 @@ 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). + ## Contributing Branches When contributing to Cherry Studio, please follow these guidelines: diff --git a/docs/branching-strategy-zh.md b/docs/branching-strategy-zh.md index b1379537a5..36b7ca263d 100644 --- a/docs/branching-strategy-zh.md +++ b/docs/branching-strategy-zh.md @@ -16,6 +16,8 @@ Cherry Studio 采用结构化的分支策略来维护代码质量并简化开发 - 只接受文档更新和 bug 修复 - 经过完整测试后可以发布到生产环境 +关于测试计划所使用的`testplan`分支,请查阅[测试计划](testplan-zh.md)。 + ## 贡献分支 在为 Cherry Studio 贡献代码时,请遵循以下准则: diff --git a/docs/features/memory-guide-zh.md b/docs/features/memory-guide-zh.md new file mode 100644 index 0000000000..6c8c37cbef --- /dev/null +++ b/docs/features/memory-guide-zh.md @@ -0,0 +1,222 @@ +# Cherry Studio 记忆功能指南 + +## 功能介绍 + +Cherry Studio 的记忆功能是一个强大的工具,能够帮助 AI 助手记住对话中的重要信息、用户偏好和上下文。通过记忆功能,您的 AI 助手可以: + +- 📝 **记住重要信息**:自动从对话中提取并存储关键事实和信息 +- 🧠 **个性化响应**:基于存储的记忆提供更加个性化和相关的回答 +- 🔍 **智能检索**:在需要时自动搜索相关记忆,增强对话的连贯性 +- 👥 **多用户支持**:为不同用户维护独立的记忆上下文 + +记忆功能特别适用于需要长期保持上下文的场景,例如个人助手、客户服务、教育辅导等。 + +## 如何启用记忆功能 + +### 1. 全局配置(首次设置) + +在使用记忆功能之前,您需要先进行全局配置: + +1. 点击侧边栏的 **记忆** 图标(记忆棒图标)进入记忆管理页面 +2. 点击右上角的 **更多** 按钮(三个点),选择 **设置** +3. 在设置弹窗中配置以下必要项: + - **LLM 模型**:选择用于处理记忆的语言模型(推荐使用 GPT-4 或 Claude 等高级模型) + - **嵌入模型**:选择用于生成向量嵌入的模型(如 text-embedding-3-small) + - **嵌入维度**:输入嵌入模型的维度(通常为 1536) +4. 点击 **确定** 保存配置 + +> ⚠️ **注意**:嵌入模型和维度一旦设置后无法更改,请谨慎选择。 + +### 2. 为助手启用记忆 + +完成全局配置后,您可以为特定助手启用记忆功能: + +1. 进入 **助手** 页面 +2. 选择要启用记忆的助手,点击 **编辑** +3. 在助手设置中找到 **记忆** 部分 +4. 打开记忆功能开关 +5. 保存助手设置 + +启用后,该助手将在对话过程中自动提取和使用记忆。 + +## 使用方法 + +### 查看记忆 + +1. 点击侧边栏的 **记忆** 图标进入记忆管理页面 +2. 您可以看到所有存储的记忆卡片,包括: + - 记忆内容 + - 创建时间 + - 所属用户 + +### 添加记忆 + +手动添加记忆有两种方式: + +**方式一:在记忆管理页面添加** + +1. 点击右上角的 **添加记忆** 按钮 +2. 在弹窗中输入记忆内容 +3. 点击 **添加** 保存 + +**方式二:在对话中自动提取** + +- 当助手启用记忆功能后,系统会自动从对话中提取重要信息并存储为记忆 + +### 编辑记忆 + +1. 在记忆卡片上点击 **更多** 按钮(三个点) +2. 选择 **编辑** +3. 修改记忆内容 +4. 点击 **保存** + +### 删除记忆 + +1. 在记忆卡片上点击 **更多** 按钮 +2. 选择 **删除** +3. 确认删除操作 + +## 记忆搜索 + +记忆管理页面提供了强大的搜索功能: + +1. 在页面顶部的搜索框中输入关键词 +2. 系统会实时过滤显示匹配的记忆 +3. 搜索支持模糊匹配,可以搜索记忆内容的任何部分 + +## 用户管理 + +记忆功能支持多用户,您可以为不同的用户维护独立的记忆库: + +### 切换用户 + +1. 在记忆管理页面,点击右上角的用户选择器 +2. 选择要切换到的用户 +3. 页面会自动加载该用户的记忆 + +### 添加新用户 + +1. 点击用户选择器 +2. 选择 **添加新用户** +3. 输入用户 ID(支持字母、数字、下划线和连字符) +4. 点击 **添加** + +### 删除用户 + +1. 切换到要删除的用户 +2. 点击右上角的 **更多** 按钮 +3. 选择 **删除用户** +4. 确认删除(注意:这将删除该用户的所有记忆) + +> 💡 **提示**:默认用户(default-user)无法删除。 + +## 设置说明 + +### LLM 模型 + +- 用于处理记忆提取和更新的语言模型 +- 建议选择能力较强的模型以获得更好的记忆提取效果 +- 可随时更改 + +### 嵌入模型 + +- 用于将文本转换为向量,支持语义搜索 +- 一旦设置后无法更改(为了保证现有记忆的兼容性) +- 推荐使用 OpenAI 的 text-embedding 系列模型 + +### 嵌入维度 + +- 嵌入向量的维度,需要与选择的嵌入模型匹配 +- 常见维度: + - text-embedding-3-small: 1536 + - text-embedding-3-large: 3072 + - text-embedding-ada-002: 1536 + +### 自定义提示词(可选) + +- **事实提取提示词**:自定义如何从对话中提取信息 +- **记忆更新提示词**:自定义如何更新现有记忆 + +## 最佳实践 + +### 1. 合理组织记忆 + +- 保持记忆简洁明了,每条记忆专注于一个具体信息 +- 使用清晰的语言描述事实,避免模糊表达 +- 定期审查和清理过时或不准确的记忆 + +### 2. 多用户场景 + +- 为不同的使用场景创建独立用户(如工作、个人、学习等) +- 使用有意义的用户 ID,便于识别和管理 +- 定期备份重要用户的记忆数据 + +### 3. 模型选择建议 + +- **LLM 模型**:GPT-4、Claude 3 等高级模型能更准确地提取和理解信息 +- **嵌入模型**:选择与您的主要使用语言匹配的模型 + +### 4. 性能优化 + +- 避免存储过多冗余记忆,这可能影响搜索性能 +- 定期整理和合并相似的记忆 +- 对于大量记忆的场景,考虑按主题或时间进行分类管理 + +## 常见问题 + +### Q: 为什么我无法启用记忆功能? + +A: 请确保您已经完成全局配置,包括选择 LLM 模型和嵌入模型。 + +### Q: 记忆会自动同步到所有助手吗? + +A: 不会。每个助手的记忆功能需要单独启用,且记忆是按用户隔离的。 + +### Q: 如何导出我的记忆数据? + +A: 目前系统暂不支持直接导出功能,但所有记忆都存储在本地数据库中。 + +### Q: 删除的记忆可以恢复吗? + +A: 删除操作是永久的,无法恢复。建议在删除前仔细确认。 + +### Q: 记忆功能会影响对话速度吗? + +A: 记忆功能在后台异步处理,不会明显影响对话响应速度。但过多的记忆可能会略微增加搜索时间。 + +### Q: 如何清空所有记忆? + +A: 您可以删除当前用户并重新创建,或者手动删除所有记忆条目。 + +## 注意事项 + +### 隐私保护 + +- 所有记忆数据都存储在您的本地设备上,不会上传到云端 +- 请勿在记忆中存储敏感信息(如密码、私钥等) +- 定期审查记忆内容,确保没有意外存储的隐私信息 + +### 数据安全 + +- 记忆数据存储在本地数据库中 +- 建议定期备份重要数据 +- 更换设备时请注意迁移记忆数据 + +### 使用限制 + +- 单条记忆的长度建议不超过 500 字 +- 每个用户的记忆数量建议控制在 1000 条以内 +- 过多的记忆可能影响系统性能 + +## 技术细节 + +记忆功能使用了先进的 RAG(检索增强生成)技术: + +1. **信息提取**:使用 LLM 从对话中智能提取关键信息 +2. **向量化存储**:通过嵌入模型将文本转换为向量,支持语义搜索 +3. **智能检索**:在对话时自动搜索相关记忆,提供给 AI 作为上下文 +4. **持续学习**:随着对话进行,不断更新和完善记忆库 + +--- + +💡 **提示**:记忆功能是 Cherry Studio 的高级特性,合理使用可以大大提升 AI 助手的智能程度和用户体验。如有更多问题,欢迎查阅文档或联系支持团队。 diff --git a/docs/technical/db.settings.md b/docs/technical/db.settings.md new file mode 100644 index 0000000000..1d63098851 --- /dev/null +++ b/docs/technical/db.settings.md @@ -0,0 +1,11 @@ +# 数据库设置字段 + +此文档包含部分字段的数据类型说明。 + +## 字段 + +| 字段名 | 类型 | 说明 | +| ------------------------------ | ------------------------------ | ------------ | +| `translate:target:language` | `LanguageCode` | 翻译目标语言 | +| `translate:source:language` | `LanguageCode` | 翻译源语言 | +| `translate:bidirectional:pair` | `[LanguageCode, LanguageCode]` | 双向翻译对 | diff --git a/docs/testplan-en.md b/docs/testplan-en.md new file mode 100644 index 0000000000..0f7cd41473 --- /dev/null +++ b/docs/testplan-en.md @@ -0,0 +1,99 @@ +# Test Plan + +To provide users with a more stable application experience and faster iteration speed, Cherry Studio has launched the "Test Plan". + +## User Guide + +The Test Plan is divided into the RC channel and the Beta channel, with the following differences: + +- **RC (Release Candidate)**: The features are stable, with fewer bugs, and it is close to the official release. +- **Beta**: Features may change at any time, and there may be more bugs, but users can experience future features earlier. + +Users can enable the "Test Plan" and select the version channel in the software's `Settings` > `About`. Please note that the versions in the "Test Plan" cannot guarantee data consistency, so be sure to back up your data before using them. + +Users are welcome to submit issues or provide feedback through other channels for any bugs encountered during testing. Your feedback is very important to us. + +## Developer Guide + +### 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. + +If the `PR` is added to the Test Plan, the repository maintainers will: + +- Notify the `PR` submitter. +- Set the PR to `draft` status (to avoid accidental merging into `main` before testing is complete). +- Set the `milestone` to the specific Test Plan version. +- Modify the `PR` title. + +During participation in the Test Plan, `PR` submitters should: + +- Keep the `PR` branch synchronized with the latest `main` (i.e., the `PR` branch should always be based on the latest `main` code). +- Ensure the `PR` branch is conflict-free. +- Actively respond to comments & reviews and fix bugs. +- Enable maintainers to modify the `PR` branch to allow for bug fixes at any time. + +Inclusion in the Test Plan does not guarantee the final merging of the `PR`. It may be shelved due to immature features or poor testing feedback. + +### Test Plan Lead + +A maintainer will be assigned as the lead for a specific version (e.g., `1.5.0-rc`). The responsibilities of the Test Plan lead include: + +- Determining whether a `PR` meets the Test Plan requirements and deciding whether it should be included in the current Test Plan. +- Modifying the status of `PRs` added to the Test Plan and communicating relevant matters with the `PR` submitter. +- Before the Test Plan release, merging the branches of `PRs` added to the Test Plan (using squash merge) into the corresponding version branch of `testplan` and resolving conflicts. +- Ensuring the `testplan` branch is synchronized with the latest `main`. +- Overseeing the Test Plan release. + +## In-Depth Understanding + +### About `PRs` + +A `PR` is a collection of a specific branch (and commits), comments, reviews, and other information, and it is the **smallest management unit** of the Test Plan. + +Compared to submitting all features to a single branch, the Test Plan manages features through `PRs`, which offers greater flexibility and efficiency: + +- Features can be added or removed between different versions of the Test Plan without cumbersome `revert` operations. +- Clear feature boundaries and responsibilities are established. Bug fixes are completed within their respective `PRs`, isolating cross-impact and better tracking progress. +- The `PR` submitter is responsible for resolving conflicts with the latest `main`. The Test Plan lead is responsible for resolving conflicts between `PR` branches. However, since features added to the Test Plan are relatively independent (in other words, if a feature has broad implications, it should be independently included in the Test Plan), conflicts are generally few or simple. + +### The `testplan` Branch + +The `testplan` branch is a **temporary** branch used for Test Plan releases. + +Note: + +- **Do not develop based on this branch**. It may change or even be deleted at any time, and there is no guarantee of commit completeness or order. +- **Do not submit `commits` or `PRs` to this branch**, as they will not be retained. +- The `testplan` branch is always based on the latest `main` branch (not on a released version), with features added on top. + +#### RC Branch + +Branch name: `testplan/rc/x.y.z` + +Used for RC releases, where `x.y.z` is the target version number. Note that whether it is rc.1 or rc.5, as long as the major version number is `x.y.z`, it is completed in this branch. + +Generally, the version number for releases from this branch is named `x.y.z-rc.n`. + +#### Beta Branch + +Branch name: `testplan/beta/x.y.z` + +Used for Beta releases, where `x.y.z` is the target version number. Note that whether it is beta.1 or beta.5, as long as the major version number is `x.y.z`, it is completed in this branch. + +Generally, the version number for releases from this branch is named `x.y.z-beta.n`. + +### Version Rules + +The application version number for the Test Plan is: `x.y.z-CHA.n`, where: + +- `x.y.z` is the conventional version number, referred to here as the **target version number**. +- `CHA` is the channel code (Channel), currently divided into `rc` and `beta`. +- `n` is the release number, starting from `1`. + +Examples of complete version numbers: `1.5.0-rc.3`, `1.5.1-beta.1`, `1.6.0-beta.6`. + +The **target version number** of the Test Plan points to the official version number where these features are expected to be added. For example: + +- `1.5.0-rc.3` means this is a preview of the `1.5.0` official release (the current latest official release is `1.4.9`, and `1.5.0` has not yet been officially released). +- `1.5.1-beta.1` means this is a beta version of the `1.5.1` official release (the current latest official release is `1.5.0`, and `1.5.1` has not yet been officially released). diff --git a/docs/testplan-zh.md b/docs/testplan-zh.md new file mode 100644 index 0000000000..ed4913d4a4 --- /dev/null +++ b/docs/testplan-zh.md @@ -0,0 +1,99 @@ +# 测试计划 + +为了给用户提供更稳定的应用体验,并提供更快的迭代速度,Cherry Studio推出“测试计划”。 + +## 用户指南 + +测试计划分为RC版通道和Beta版通道吗,区别在于: + +- **RC版(预览版)**:RC即Release Candidate,功能已经稳定,BUG较少,接近正式版 +- **Beta版(测试版)**:功能可能随时变化,BUG较多,可以较早体验未来功能 + +用户可以在软件的`设置`-`关于`中,开启“测试计划”并选择版本通道。请注意“测试计划”的版本无法保证数据的一致性,请使用前一定要备份数据。 + +用户在测试过程中发现的BUG,欢迎提交issue或通过其他渠道反馈。用户的反馈对我们非常重要。 + +## 开发者指南 + +### 参与测试计划 + +开发者按照[贡献者指南](CONTRIBUTING.zh.md)要求正常提交`PR`(并注意提交target为`main`)。仓库维护者会综合考虑(例如该功能对应用的影响程度,功能的重要性,是否需要更广泛的测试等),决定该`PR`是否应加入测试计划。 + +若该`PR`加入测试计划,仓库维护者会做如下操作: + +- 通知`PR`提交人 +- 设置PR为`draft`状态(避免在测试完成前意外并入`main`) +- `milestone`设置为具体测试计划版本 +- 修改`PR`标题 + +`PR`提交人在参与测试计划过程中,应做到: + +- 保持`PR`分支与最新`main`同步(即`PR`分支总是应基于最新`main`代码) +- 保持`PR`分支为无冲突状态 +- 积极响应 comments & reviews,修复bug +- 开启维护者可以修改`PR`分支的权限,以便维护者能随时修改BUG + +加入测试计划并不保证`PR`的最终合并,也有可能由于功能不成熟或测试反馈不佳而搁置 + +### 测试计划负责人 + +某个维护者会被指定为某个版本期间(例如`1.5.0-rc`)的测试计划负责人。测试计划负责人的工作为: + +- 判断某个`PR`是否符合测试计划要求,并决定是否应合入当期测试计划 +- 修改加入测试计划的`PR`状态,并与`PR`提交人沟通相关事宜 +- 在测试计划发版前,将加入测试计划的`PR`分支逐一合并(采用squash merge)至`testplan`对应版本分支,并解决冲突 +- 保证`testplan`分支与最新`main`同步 +- 负责测试计划发版 + +## 深入理解 + +### 关于`PR` + +`PR`是特定分支(及commits)、comments、reviews等各种信息的集合,也是测试计划的**最小管理单元**。 + +相比将所有功能都提交到某个分支,测试计划通过`PR`来管理功能,这可以带来极大的灵活度和效率: + +- 测试计划的各个版本间,可以随意增减功能,而无需繁琐的`revert`操作 +- 明确了功能边界和负责人,bug修复在各自`PR`中完成,隔离了交叉影响,也能更好观察进度 +- `PR`提交人负责与最新`main`之间的冲突;测试计划负责人负责各`PR`分支之间的冲突,但因加入测试计划的各功能相对比较独立(话句话说,如果功能牵涉较广,则应独立上测试计划),冲突一般比较少或简单。 + +### `testplan`分支 + +`testplan`分支是用于测试计划发版所用的**临时**分支。 + +注意: + +- **请勿基于该分支开发**。该分支随时会变化甚至删除,且并不保证commit的完整和顺序。 +- **请勿向该分支提交`commit`及`PR`**,将不会得到保留 +- `testplan`分支总是基于最新`main`分支(而不是基于已发布版本),在其之上添加功能 + +#### RC版分支 + +分支名称:`testplan/rc/x.y.z` + +用于RC版的发版,x.y.z为目标版本号,注意无论是rc.1还是rc.5,只要主版本号为x.y.z,都在该分支完成。 + +一般而言,该分支发版的版本号命名为`x.y.z-rc.n` + +#### Beta版分支 + +分支名称:`testplan/beta/x.y.z` + +用于Beta版的发版,x.y.z为目标版本号,注意无论是beta.1还是beta.5,只要主版本号为x.y.z,都在该分支完成。 + +一般而言,该分支发版的版本号命名为`x.y.z-beta.n` + +### 版本规则 + +测试计划的应用版本号为:`x.y.z-CHA.n`,其中: + +- `x.y.z`为一般意义上的版本号,在这里称为**目标版本号** +- `CHA`为通道号(Channel),现在分为`rc`和`beta` +- `n`为发版编号,从`1`计数 + +完整的版本号举例:`1.5.0-rc.3`、`1.5.1-beta.1`、`1.6.0-beta.6` + +测试计划的**目标版本号**指向希望添加这些功能的正式版版本号。例如: + +- `1.5.0-rc.3`是指,这是`1.5.0`正式版的预览版(当前最新正式版是`1.4.9`,而`1.5.0`正式版还未发布) +- `1.5.1-beta.1`是指,这是`1.5.1`正式版的测试版(当前最新正式版是`1.5.0`,而`1.5.1`正式版还未发布) diff --git a/electron-builder.yml b/electron-builder.yml index ecbbc10057..17a731d94a 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -11,6 +11,11 @@ electronLanguages: - en # for macOS directories: buildResources: build + +protocols: + - name: Cherry Studio + schemes: + - cherrystudio files: - '**/*' - '!**/{.vscode,.yarn,.yarn-lock,.github,.cursorrules,.prettierrc}' @@ -48,7 +53,11 @@ files: - '!node_modules/pdf-parse/lib/pdf.js/{v1.9.426,v1.10.88,v2.0.550}' - '!node_modules/mammoth/{mammoth.browser.js,mammoth.browser.min.js}' - '!node_modules/selection-hook/prebuilds/**/*' # we rebuild .node, don't use prebuilds - - '!**/*.{h,iobj,ipdb,tlog,recipe,vcxproj,vcxproj.filters}' # filter .node build files + - '!node_modules/pdfjs-dist/web/**/*' + - '!node_modules/pdfjs-dist/legacy/web/*' + - '!node_modules/selection-hook/node_modules' # we don't need what in the node_modules dir + - '!node_modules/selection-hook/src' # we don't need source files + - '!**/*.{h,iobj,ipdb,tlog,recipe,vcxproj,vcxproj.filters,Makefile,*.Makefile}' # filter .node build files asarUnpack: - resources/** - '**/*.{metal,exp,lib}' @@ -108,10 +117,8 @@ afterSign: scripts/notarize.js artifactBuildCompleted: scripts/artifact-build-completed.js releaseInfo: releaseNotes: | - 界面优化:优化多处界面样式,气泡样式改版,自动调整代码预览边栏宽度 - 知识库:修复知识库引用不显示问题,修复部分嵌入模型适配问题 - 备份与恢复:修复超过 2GB 大文件无法恢复问题 - 文件处理:添加 .doc 文件支持 - 划词助手:支持自定义 CSS 样式 - MCP:基于 Pyodide 实现 Python MCP 服务 - 其他错误修复和优化 + 新增全局记忆功能 + MCP 支持 DXT 格式导入 + 全局快捷键支持 Linux 系统 + 模型思考过程增加动画效果 + 错误修复和性能优化 diff --git a/electron.vite.config.ts b/electron.vite.config.ts index fa4b234a54..7930b57f35 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -1,4 +1,5 @@ import react from '@vitejs/plugin-react-swc' +import { CodeInspectorPlugin } from 'code-inspector-plugin' import { defineConfig, externalizeDepsPlugin } from 'electron-vite' import { resolve } from 'path' import { visualizer } from 'rollup-plugin-visualizer' @@ -7,6 +8,9 @@ const visualizerPlugin = (type: 'renderer' | 'main') => { return process.env[`VISUALIZER_${type.toUpperCase()}`] ? [visualizer({ open: true })] : [] } +const isDev = process.env.NODE_ENV === 'development' +const isProd = process.env.NODE_ENV === 'production' + export default defineConfig({ main: { plugins: [externalizeDepsPlugin(), ...visualizerPlugin('main')], @@ -19,18 +23,17 @@ export default defineConfig({ }, build: { rollupOptions: { - external: ['@libsql/client', 'bufferutil', 'utf-8-validate'], + external: ['@libsql/client', 'bufferutil', 'utf-8-validate', '@cherrystudio/mac-system-ocr'], output: { - // 彻底禁用代码分割 - 返回 null 强制单文件打包 - manualChunks: undefined, - // 内联所有动态导入,这是关键配置 - inlineDynamicImports: true + manualChunks: undefined, // 彻底禁用代码分割 - 返回 null 强制单文件打包 + inlineDynamicImports: true // 内联所有动态导入,这是关键配置 } }, - sourcemap: process.env.NODE_ENV === 'development' + sourcemap: isDev }, + esbuild: isProd ? { legalComments: 'none' } : {}, optimizeDeps: { - noDiscovery: process.env.NODE_ENV === 'development' + noDiscovery: isDev } }, preload: { @@ -41,7 +44,7 @@ export default defineConfig({ } }, build: { - sourcemap: process.env.NODE_ENV === 'development' + sourcemap: isDev } }, renderer: { @@ -59,6 +62,7 @@ export default defineConfig({ ] ] }), + ...(isDev ? [CodeInspectorPlugin({ bundler: 'vite' })] : []), // 只在开发环境下启用 CodeInspectorPlugin ...visualizerPlugin('renderer') ], resolve: { @@ -86,6 +90,7 @@ export default defineConfig({ selectionAction: resolve(__dirname, 'src/renderer/selectionAction.html') } } - } + }, + esbuild: isProd ? { legalComments: 'none' } : {} } }) diff --git a/eslint.config.mjs b/eslint.config.mjs index 33e6ae8757..e0a893527e 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -26,7 +26,7 @@ export default defineConfig([ 'simple-import-sort/exports': 'error', 'unused-imports/no-unused-imports': 'error', '@eslint-react/no-prop-types': 'error', - 'prettier/prettier': ['error', { endOfLine: 'auto' }] + 'prettier/prettier': ['error'] } }, // Configuration for ensuring compatibility with the original ESLint(8.x) rules diff --git a/package.json b/package.json index 5455f348b2..e7f6770477 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "CherryStudio", - "version": "1.4.7", + "version": "1.5.1", "private": true, "description": "A powerful AI assistant for producer.", "main": "./out/main/index.js", @@ -27,12 +27,12 @@ "build:win": "dotenv npm run build && electron-builder --win --x64 --arm64", "build:win:x64": "dotenv npm run build && electron-builder --win --x64", "build:win:arm64": "dotenv npm run build && electron-builder --win --arm64", - "build:mac": "dotenv electron-vite build && electron-builder --mac --arm64 --x64", - "build:mac:arm64": "dotenv electron-vite build && electron-builder --mac --arm64", - "build:mac:x64": "dotenv electron-vite build && electron-builder --mac --x64", - "build:linux": "dotenv electron-vite build && electron-builder --linux --x64 --arm64", - "build:linux:arm64": "dotenv electron-vite build && electron-builder --linux --arm64", - "build:linux:x64": "dotenv electron-vite build && electron-builder --linux --x64", + "build:mac": "dotenv npm run build && electron-builder --mac --arm64 --x64", + "build:mac:arm64": "dotenv npm run build && electron-builder --mac --arm64", + "build:mac:x64": "dotenv npm run build && electron-builder --mac --x64", + "build:linux": "dotenv npm run build && electron-builder --linux --x64 --arm64", + "build:linux:arm64": "dotenv npm run build && electron-builder --linux --arm64", + "build:linux:x64": "dotenv npm run build && electron-builder --linux --x64", "build:npm": "node scripts/build-npm.js", "release": "node scripts/version.js", "publish": "yarn build:check && yarn release patch push", @@ -55,17 +55,24 @@ "test:lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts", "format": "prettier --write .", "lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix", - "prepare": "husky" + "prepare": "git config blame.ignoreRevsFile .git-blame-ignore-revs && husky" }, "dependencies": { + "@aws-sdk/client-s3": "^3.840.0", + "@cherrystudio/pdf-to-img-napi": "^0.0.1", "@libsql/client": "0.14.0", "@libsql/win32-x64-msvc": "^0.4.7", "@strongtz/win32-arm64-msvc": "^0.4.7", + "iconv-lite": "^0.6.3", + "jaison": "^2.0.2", + "jschardet": "^3.1.4", "jsdom": "26.1.0", + "macos-release": "^3.4.0", "node-stream-zip": "^1.15.0", "notion-helper": "^1.3.22", "os-proxy-config": "^1.1.2", - "selection-hook": "^0.9.23", + "pdfjs-dist": "4.10.38", + "selection-hook": "^1.0.6", "turndown": "7.2.0" }, "devDependencies": { @@ -86,6 +93,7 @@ "@cherrystudio/embedjs-loader-xml": "^0.1.31", "@cherrystudio/embedjs-ollama": "^0.1.31", "@cherrystudio/embedjs-openai": "^0.1.31", + "@codemirror/view": "^6.0.0", "@electron-toolkit/eslint-config-prettier": "^3.0.0", "@electron-toolkit/eslint-config-ts": "^3.0.0", "@electron-toolkit/preload": "^3.0.0", @@ -100,14 +108,16 @@ "@kangfenmao/keyv-storage": "^0.1.0", "@langchain/community": "^0.3.36", "@langchain/ollama": "^0.2.1", - "@modelcontextprotocol/sdk": "^1.11.4", + "@mistralai/mistralai": "^1.6.0", + "@modelcontextprotocol/sdk": "^1.12.3", "@mozilla/readability": "^0.6.0", "@notionhq/client": "^2.2.15", "@playwright/test": "^1.52.0", "@reduxjs/toolkit": "^2.2.5", - "@shikijs/markdown-it": "^3.4.2", + "@shikijs/markdown-it": "^3.7.0", "@swc/plugin-styled-components": "^7.1.5", "@tanstack/react-query": "^5.27.0", + "@tanstack/react-virtual": "^3.13.12", "@testing-library/dom": "^10.4.0", "@testing-library/jest-dom": "^6.6.3", "@testing-library/react": "^16.3.0", @@ -125,28 +135,32 @@ "@types/react-window": "^1", "@types/tinycolor2": "^1", "@types/word-extractor": "^1", - "@uiw/codemirror-extensions-langs": "^4.23.12", - "@uiw/codemirror-themes-all": "^4.23.12", - "@uiw/react-codemirror": "^4.23.12", + "@uiw/codemirror-extensions-langs": "^4.23.14", + "@uiw/codemirror-themes-all": "^4.23.14", + "@uiw/react-codemirror": "^4.23.14", "@vitejs/plugin-react-swc": "^3.9.0", "@vitest/browser": "^3.1.4", "@vitest/coverage-v8": "^3.1.4", "@vitest/ui": "^3.1.4", "@vitest/web-worker": "^3.1.4", + "@viz-js/lang-dot": "^1.0.5", + "@viz-js/viz": "^3.14.0", "@xyflow/react": "^12.4.4", - "antd": "^5.22.5", + "antd": "patch:antd@npm%3A5.24.7#~/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch", "archiver": "^7.0.1", "async-mutex": "^0.5.0", "axios": "^1.7.3", "browser-image-compression": "^2.0.2", + "code-inspector-plugin": "^0.20.14", "color": "^5.0.0", + "country-flag-emoji-polyfill": "0.1.8", "dayjs": "^1.11.11", "dexie": "^4.0.8", "dexie-react-hooks": "^1.1.7", "diff": "^7.0.0", "docx": "^9.0.2", "dotenv-cli": "^7.4.2", - "electron": "35.4.0", + "electron": "35.6.0", "electron-builder": "26.0.15", "electron-devtools-installer": "^3.2.0", "electron-log": "^5.1.5", @@ -163,6 +177,7 @@ "eslint-plugin-unused-imports": "^4.1.4", "fast-diff": "^1.3.0", "fast-xml-parser": "^5.2.0", + "fetch-socks": "1.3.2", "franc-min": "^6.2.0", "fs-extra": "^11.2.0", "google-auth-library": "^9.15.1", @@ -175,13 +190,12 @@ "lru-cache": "^11.1.0", "lucide-react": "^0.487.0", "markdown-it": "^14.1.0", - "mermaid": "^11.6.0", + "mermaid": "^11.7.0", "mime": "^4.0.4", "motion": "^12.10.5", "npx-scope-finder": "^1.2.0", "officeparser": "^4.1.1", "openai": "patch:openai@npm%3A5.1.0#~/.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch", - "opendal": "0.47.11", "p-queue": "^8.1.0", "playwright": "^1.52.0", "prettier": "^3.5.3", @@ -209,13 +223,15 @@ "remove-markdown": "^0.6.2", "rollup-plugin-visualizer": "^5.12.0", "sass": "^1.88.0", - "shiki": "^3.4.2", + "shiki": "^3.7.0", "string-width": "^7.2.0", "styled-components": "^6.1.11", "tar": "^7.4.3", "tiny-pinyin": "^1.3.2", "tokenx": "^1.1.0", "typescript": "^5.6.2", + "undici": "6.21.2", + "unified": "^11.0.5", "uuid": "^10.0.0", "vite": "6.2.6", "vitest": "^3.1.4", @@ -223,6 +239,9 @@ "word-extractor": "^1.0.4", "zipread": "^1.3.3" }, + "optionalDependencies": { + "@cherrystudio/mac-system-ocr": "^0.2.2" + }, "resolutions": { "pdf-parse@npm:1.1.1": "patch:pdf-parse@npm%3A1.1.1#~/.yarn/patches/pdf-parse-npm-1.1.1-04a6109b2a.patch", "@langchain/openai@npm:^0.3.16": "patch:@langchain/openai@npm%3A0.3.16#~/.yarn/patches/@langchain-openai-npm-0.3.16-e525b59526.patch", @@ -233,7 +252,8 @@ "app-builder-lib@npm:26.0.13": "patch:app-builder-lib@npm%3A26.0.13#~/.yarn/patches/app-builder-lib-npm-26.0.13-a064c9e1d0.patch", "openai@npm:^4.87.3": "patch:openai@npm%3A5.1.0#~/.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch", "app-builder-lib@npm:26.0.15": "patch:app-builder-lib@npm%3A26.0.15#~/.yarn/patches/app-builder-lib-npm-26.0.15-360e5b0476.patch", - "@langchain/core@npm:^0.3.26": "patch:@langchain/core@npm%3A0.3.44#~/.yarn/patches/@langchain-core-npm-0.3.44-41d5c3cb0a.patch" + "@langchain/core@npm:^0.3.26": "patch:@langchain/core@npm%3A0.3.44#~/.yarn/patches/@langchain-core-npm-0.3.44-41d5c3cb0a.patch", + "undici": "6.21.2" }, "packageManager": "yarn@4.9.1", "lint-staged": { diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index ca49bd40c5..057e84ca76 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -32,7 +32,11 @@ export enum IpcChannel { App_InstallUvBinary = 'app:install-uv-binary', App_InstallBunBinary = 'app:install-bun-binary', + App_MacIsProcessTrusted = 'app:mac-is-process-trusted', + App_MacRequestProcessTrust = 'app:mac-request-process-trust', + App_QuoteToMain = 'app:quote-to-main', + App_SetDisableHardwareAcceleration = 'app:set-disable-hardware-acceleration', Notification_Send = 'notification:send', Notification_OnClick = 'notification:on-click', @@ -70,6 +74,10 @@ export enum IpcChannel { Mcp_ServersChanged = 'mcp:servers-changed', Mcp_ServersUpdated = 'mcp:servers-updated', Mcp_CheckConnectivity = 'mcp:check-connectivity', + Mcp_UploadDxt = 'mcp:upload-dxt', + Mcp_SetProgress = 'mcp:set-progress', + Mcp_AbortTool = 'mcp:abort-tool', + Mcp_GetServerVersion = 'mcp:get-server-version', // Python Python_Execute = 'python:execute', @@ -115,6 +123,7 @@ export enum IpcChannel { KnowledgeBase_Remove = 'knowledge-base:remove', KnowledgeBase_Search = 'knowledge-base:search', KnowledgeBase_Rerank = 'knowledge-base:rerank', + KnowledgeBase_Check_Quota = 'knowledge-base:check-quota', //file File_Open = 'file:open', @@ -125,9 +134,10 @@ export enum IpcChannel { File_Clear = 'file:clear', File_Read = 'file:read', File_Delete = 'file:delete', + File_DeleteDir = 'file:deleteDir', File_Get = 'file:get', File_SelectFolder = 'file:selectFolder', - File_Create = 'file:create', + File_CreateTempFile = 'file:createTempFile', File_Write = 'file:write', File_WriteWithId = 'file:writeWithId', File_SaveImage = 'file:saveImage', @@ -139,6 +149,13 @@ export enum IpcChannel { File_Base64File = 'file:base64File', File_GetPdfInfo = 'file:getPdfInfo', Fs_Read = 'fs:read', + File_OpenWithRelativePath = 'file:openWithRelativePath', + + // file service + FileService_Upload = 'file-service:upload', + FileService_List = 'file-service:list', + FileService_Delete = 'file-service:delete', + FileService_Retrieve = 'file-service:retrieve', Export_Word = 'export:word', @@ -153,6 +170,11 @@ export enum IpcChannel { Backup_CheckConnection = 'backup:checkConnection', Backup_CreateDirectory = 'backup:createDirectory', Backup_DeleteWebdavFile = 'backup:deleteWebdavFile', + Backup_BackupToLocalDir = 'backup:backupToLocalDir', + Backup_RestoreFromLocalBackup = 'backup:restoreFromLocalBackup', + Backup_ListLocalBackupFiles = 'backup:listLocalBackupFiles', + Backup_DeleteLocalBackupFile = 'backup:deleteLocalBackupFile', + Backup_SetLocalBackupDir = 'backup:setLocalBackupDir', Backup_BackupToS3 = 'backup:backupToS3', Backup_RestoreFromS3 = 'backup:restoreFromS3', Backup_ListS3Files = 'backup:listS3Files', @@ -222,5 +244,17 @@ export enum IpcChannel { Selection_ActionWindowMinimize = 'selection:action-window-minimize', Selection_ActionWindowPin = 'selection:action-window-pin', Selection_ProcessAction = 'selection:process-action', - Selection_UpdateActionData = 'selection:update-action-data' + Selection_UpdateActionData = 'selection:update-action-data', + + // Memory + Memory_Add = 'memory:add', + Memory_Search = 'memory:search', + Memory_List = 'memory:list', + Memory_Delete = 'memory:delete', + Memory_Update = 'memory:update', + Memory_Get = 'memory:get', + Memory_SetConfig = 'memory:set-config', + Memory_DeleteUser = 'memory:delete-user', + Memory_DeleteAllMemoriesForUser = 'memory:delete-all-memories-for-user', + Memory_GetUsersList = 'memory:get-users-list' } diff --git a/packages/shared/config/constant.ts b/packages/shared/config/constant.ts index e4545d44cb..fc118b6b87 100644 --- a/packages/shared/config/constant.ts +++ b/packages/shared/config/constant.ts @@ -193,6 +193,7 @@ const textExtsByCategory = new Map([ '.htm', '.xhtml', // HTML '.xml', // XML + '.fxml', // JavaFX XML '.org', // Org-mode '.wiki', // Wiki '.tex', @@ -416,6 +417,6 @@ export enum UpgradeChannel { BETA = 'beta' // 预览版本 } -export const defaultTimeout = 5 * 1000 * 60 +export const defaultTimeout = 10 * 1000 * 60 export const occupiedDirs = ['logs', 'Network', 'Partitions/webview/Network'] diff --git a/packages/shared/config/types.ts b/packages/shared/config/types.ts index 48a76c4778..28bb4acf65 100644 --- a/packages/shared/config/types.ts +++ b/packages/shared/config/types.ts @@ -1,6 +1,11 @@ +import { ProcessingStatus } from '@types' + export type LoaderReturn = { entriesAdded: number uniqueId: string uniqueIds: string[] loaderType: string + status?: ProcessingStatus + message?: string + messageSource?: 'preprocess' | 'embedding' } diff --git a/resources/scripts/install-bun.js b/resources/scripts/install-bun.js index 8e232dfa9c..1467a4cde4 100644 --- a/resources/scripts/install-bun.js +++ b/resources/scripts/install-bun.js @@ -43,7 +43,7 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION, if (!packageName) { console.error(`No binary available for ${platformKey}`) - return false + return 101 } // Create output directory structure @@ -86,7 +86,7 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION, fs.chmodSync(outputPath, 0o755) } catch (chmodError) { console.error(`Warning: Failed to set executable permissions on ${filename}`) - return false + return 102 } } console.log(`Extracted ${entry.name} -> ${outputPath}`) @@ -97,8 +97,10 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION, // Clean up fs.unlinkSync(tempFilename) console.log(`Successfully installed bun ${version} for ${platformKey}`) - return true + return 0 } catch (error) { + let retCode = 103 + console.error(`Error installing bun for ${platformKey}: ${error.message}`) // Clean up temporary file if it exists if (fs.existsSync(tempFilename)) { @@ -114,9 +116,10 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION, } } catch (cleanupError) { console.warn(`Warning: Failed to clean up directory: ${cleanupError.message}`) + retCode = 104 } - return false + return retCode } } @@ -159,16 +162,21 @@ async function installBun() { `Installing bun ${version} for ${platform}-${arch}${isMusl ? ' (MUSL)' : ''}${isBaseline ? ' (baseline)' : ''}...` ) - await downloadBunBinary(platform, arch, version, isMusl, isBaseline) + return await downloadBunBinary(platform, arch, version, isMusl, isBaseline) } // Run the installation installBun() - .then(() => { - console.log('Installation successful') - process.exit(0) + .then((retCode) => { + if (retCode === 0) { + console.log('Installation successful') + process.exit(0) + } else { + console.error('Installation failed') + process.exit(retCode) + } }) .catch((error) => { console.error('Installation failed:', error) - process.exit(1) + process.exit(100) }) diff --git a/resources/scripts/install-uv.js b/resources/scripts/install-uv.js index 2c882d07da..3dc8b3e477 100644 --- a/resources/scripts/install-uv.js +++ b/resources/scripts/install-uv.js @@ -44,7 +44,7 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is if (!packageName) { console.error(`No binary available for ${platformKey}`) - return false + return 101 } // Create output directory structure @@ -85,7 +85,7 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is fs.chmodSync(outputPath, 0o755) } catch (chmodError) { console.error(`Warning: Failed to set executable permissions on ${filename}`) - return false + return 102 } } console.log(`Extracted ${entry.name} -> ${outputPath}`) @@ -95,8 +95,10 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is await zip.close() fs.unlinkSync(tempFilename) console.log(`Successfully installed uv ${version} for ${platform}-${arch}`) - return true + return 0 } catch (error) { + let retCode = 103 + console.error(`Error installing uv for ${platformKey}: ${error.message}`) if (fs.existsSync(tempFilename)) { @@ -112,9 +114,10 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is } } catch (cleanupError) { console.warn(`Warning: Failed to clean up directory: ${cleanupError.message}`) + retCode = 104 } - return false + return retCode } } @@ -154,16 +157,21 @@ async function installUv() { console.log(`Installing uv ${version} for ${platform}-${arch}${isMusl ? ' (MUSL)' : ''}...`) - await downloadUvBinary(platform, arch, version, isMusl) + return await downloadUvBinary(platform, arch, version, isMusl) } // Run the installation installUv() - .then(() => { - console.log('Installation successful') - process.exit(0) + .then((retCode) => { + if (retCode === 0) { + console.log('Installation successful') + process.exit(0) + } else { + console.error('Installation failed') + process.exit(retCode) + } }) .catch((error) => { console.error('Installation failed:', error) - process.exit(1) + process.exit(100) }) diff --git a/scripts/after-pack.js b/scripts/after-pack.js index a764642308..4b18d2dacd 100644 --- a/scripts/after-pack.js +++ b/scripts/after-pack.js @@ -23,6 +23,9 @@ exports.default = async function (context) { const node_modules_path = path.join(context.appOutDir, 'resources', 'app.asar.unpacked', 'node_modules') const _arch = arch === Arch.arm64 ? ['linux-arm64-gnu', 'linux-arm64-musl'] : ['linux-x64-gnu', 'linux-x64-musl'] keepPackageNodeFiles(node_modules_path, '@libsql', _arch) + + // 删除 macOS 专用的 OCR 包 + removeMacOnlyPackages(node_modules_path) } if (platform === 'windows') { @@ -35,6 +38,8 @@ exports.default = async function (context) { keepPackageNodeFiles(node_modules_path, '@strongtz', ['win32-x64-msvc']) keepPackageNodeFiles(node_modules_path, '@libsql', ['win32-x64-msvc']) } + + removeMacOnlyPackages(node_modules_path) } if (platform === 'windows') { @@ -43,6 +48,22 @@ exports.default = async function (context) { } } +/** + * 删除 macOS 专用的包 + * @param {string} nodeModulesPath + */ +function removeMacOnlyPackages(nodeModulesPath) { + const macOnlyPackages = ['@cherrystudio/mac-system-ocr'] + + macOnlyPackages.forEach((packageName) => { + const packagePath = path.join(nodeModulesPath, packageName) + if (fs.existsSync(packagePath)) { + fs.rmSync(packagePath, { recursive: true, force: true }) + console.log(`[After Pack] Removed macOS-only package: ${packageName}`) + } + }) +} + /** * 使用指定架构的 node_modules 文件 * @param {*} nodeModulesPath diff --git a/scripts/check-i18n.js b/scripts/check-i18n.js index dd36c2670d..9c99fc9ae0 100644 --- a/scripts/check-i18n.js +++ b/scripts/check-i18n.js @@ -1,9 +1,60 @@ 'use strict' +var __createBinding = + (this && this.__createBinding) || + (Object.create + ? function (o, m, k, k2) { + if (k2 === undefined) k2 = k + var desc = Object.getOwnPropertyDescriptor(m, k) + if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { + enumerable: true, + get: function () { + return m[k] + } + } + } + Object.defineProperty(o, k2, desc) + } + : function (o, m, k, k2) { + if (k2 === undefined) k2 = k + o[k2] = m[k] + }) +var __setModuleDefault = + (this && this.__setModuleDefault) || + (Object.create + ? function (o, v) { + Object.defineProperty(o, 'default', { enumerable: true, value: v }) + } + : function (o, v) { + o['default'] = v + }) +var __importStar = + (this && this.__importStar) || + (function () { + var ownKeys = function (o) { + ownKeys = + Object.getOwnPropertyNames || + function (o) { + var ar = [] + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k + return ar + } + return ownKeys(o) + } + return function (mod) { + if (mod && mod.__esModule) return mod + var result = {} + if (mod != null) + for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== 'default') __createBinding(result, mod, k[i]) + __setModuleDefault(result, mod) + return result + } + })() Object.defineProperty(exports, '__esModule', { value: true }) -var fs = require('fs') -var path = require('path') +var fs = __importStar(require('fs')) +var path = __importStar(require('path')) var translationsDir = path.join(__dirname, '../src/renderer/src/i18n/locales') -var baseLocale = 'en-us' +var baseLocale = 'zh-cn' var baseFileName = ''.concat(baseLocale, '.json') var baseFilePath = path.join(translationsDir, baseFileName) /** @@ -48,12 +99,43 @@ function syncRecursively(target, template) { } return isUpdated } +/** + * 检查 JSON 对象中是否存在重复键,并收集所有重复键 + * @param obj 要检查的对象 + * @returns 返回重复键的数组(若无重复则返回空数组) + */ +function checkDuplicateKeys(obj) { + var keys = new Set() + var duplicateKeys = [] + var checkObject = function (obj, path) { + if (path === void 0) { + path = '' + } + for (var key in obj) { + var fullPath = path ? ''.concat(path, '.').concat(key) : key + if (keys.has(fullPath)) { + // 发现重复键时,添加到数组中(避免重复添加) + if (!duplicateKeys.includes(fullPath)) { + duplicateKeys.push(fullPath) + } + } else { + keys.add(fullPath) + } + // 递归检查子对象 + if (typeof obj[key] === 'object' && obj[key] !== null) { + checkObject(obj[key], fullPath) + } + } + } + checkObject(obj) + return duplicateKeys +} function syncTranslations() { if (!fs.existsSync(baseFilePath)) { console.error( '\u4E3B\u6A21\u677F\u6587\u4EF6 '.concat( baseFileName, - ' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u6216\u6587\u4EF6\u540D\u3002' + ' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u6216\u6587\u4EF6\u540D' ) ) return @@ -63,9 +145,18 @@ function syncTranslations() { try { baseJson = JSON.parse(baseContent) } catch (error) { - console.error('\u89E3\u6790 '.concat(baseFileName, ' \u51FA\u9519:'), error) + console.error('\u89E3\u6790 '.concat(baseFileName, ' \u51FA\u9519\u3002').concat(error)) return } + // 检查主模板是否存在重复键 + var duplicateKeys = checkDuplicateKeys(baseJson) + if (duplicateKeys.length > 0) { + throw new Error( + '\u4E3B\u6A21\u677F\u6587\u4EF6 ' + .concat(baseFileName, ' \u5B58\u5728\u4EE5\u4E0B\u91CD\u590D\u952E\uFF1A\n') + .concat(duplicateKeys.join('\n')) + ) + } var files = fs.readdirSync(translationsDir).filter(function (file) { return file.endsWith('.json') && file !== baseFileName }) @@ -77,27 +168,19 @@ function syncTranslations() { var fileContent = fs.readFileSync(filePath, 'utf-8') targetJson = JSON.parse(fileContent) } catch (error) { - console.error( - '\u89E3\u6790 '.concat( - file, - ' \u51FA\u9519\uFF0C\u8DF3\u8FC7\u6B64\u6587\u4EF6\u3002\u9519\u8BEF\u4FE1\u606F:' - ), - error - ) + console.error('\u89E3\u6790 '.concat(file, ' \u51FA\u9519\uFF0C\u8DF3\u8FC7\u6B64\u6587\u4EF6\u3002'), error) continue } var isUpdated = syncRecursively(targetJson, baseJson) if (isUpdated) { try { - fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2), 'utf-8') - console.log( - '\u6587\u4EF6 '.concat(file, ' \u5DF2\u66F4\u65B0\u540C\u6B65\u4E3B\u6A21\u677F\u7684\u5185\u5BB9\u3002') - ) + fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2) + '\n', 'utf-8') + console.log('\u6587\u4EF6 '.concat(file, ' \u5DF2\u66F4\u65B0\u540C\u6B65\u4E3B\u6A21\u677F\u7684\u5185\u5BB9')) } catch (error) { - console.error('\u5199\u5165 '.concat(file, ' \u51FA\u9519:'), error) + console.error('\u5199\u5165 '.concat(file, ' \u51FA\u9519\u3002').concat(error)) } } else { - console.log('\u6587\u4EF6 '.concat(file, ' \u65E0\u9700\u66F4\u65B0\u3002')) + console.log('\u6587\u4EF6 '.concat(file, ' \u65E0\u9700\u66F4\u65B0')) } } } diff --git a/scripts/check-i18n.ts b/scripts/check-i18n.ts index 915aa31f4f..238b3ca99f 100644 --- a/scripts/check-i18n.ts +++ b/scripts/check-i18n.ts @@ -2,7 +2,7 @@ import * as fs from 'fs' import * as path from 'path' const translationsDir = path.join(__dirname, '../src/renderer/src/i18n/locales') -const baseLocale = 'zh-CN' +const baseLocale = 'zh-cn' const baseFileName = `${baseLocale}.json` const baseFilePath = path.join(translationsDir, baseFileName) @@ -52,6 +52,39 @@ function syncRecursively(target: any, template: any): boolean { return isUpdated } +/** + * 检查 JSON 对象中是否存在重复键,并收集所有重复键 + * @param obj 要检查的对象 + * @returns 返回重复键的数组(若无重复则返回空数组) + */ +function checkDuplicateKeys(obj: Record): string[] { + const keys = new Set() + const duplicateKeys: string[] = [] + + const checkObject = (obj: Record, path: string = '') => { + for (const key in obj) { + const fullPath = path ? `${path}.${key}` : key + + if (keys.has(fullPath)) { + // 发现重复键时,添加到数组中(避免重复添加) + if (!duplicateKeys.includes(fullPath)) { + duplicateKeys.push(fullPath) + } + } else { + keys.add(fullPath) + } + + // 递归检查子对象 + if (typeof obj[key] === 'object' && obj[key] !== null) { + checkObject(obj[key], fullPath) + } + } + } + + checkObject(obj) + return duplicateKeys +} + function syncTranslations() { if (!fs.existsSync(baseFilePath)) { console.error(`主模板文件 ${baseFileName} 不存在,请检查路径或文件名`) @@ -63,10 +96,16 @@ function syncTranslations() { try { baseJson = JSON.parse(baseContent) } catch (error) { - console.error(`解析 ${baseFileName} 出错:`, error) + console.error(`解析 ${baseFileName} 出错。${error}`) return } + // 检查主模板是否存在重复键 + const duplicateKeys = checkDuplicateKeys(baseJson) + if (duplicateKeys.length > 0) { + throw new Error(`主模板文件 ${baseFileName} 存在以下重复键:\n${duplicateKeys.join('\n')}`) + } + const files = fs.readdirSync(translationsDir).filter((file) => file.endsWith('.json') && file !== baseFileName) for (const file of files) { @@ -76,7 +115,7 @@ function syncTranslations() { const fileContent = fs.readFileSync(filePath, 'utf-8') targetJson = JSON.parse(fileContent) } catch (error) { - console.error(`解析 ${file} 出错,跳过此文件。错误信息:`, error) + console.error(`解析 ${file} 出错,跳过此文件。`, error) continue } @@ -87,7 +126,7 @@ function syncTranslations() { fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2) + '\n', 'utf-8') console.log(`文件 ${file} 已更新同步主模板的内容`) } catch (error) { - console.error(`写入 ${file} 出错:`, error) + console.error(`写入 ${file} 出错。${error}`) } } else { console.log(`文件 ${file} 无需更新`) diff --git a/scripts/update-i18n.ts b/scripts/update-i18n.ts index 3af6084384..9363970f74 100644 --- a/scripts/update-i18n.ts +++ b/scripts/update-i18n.ts @@ -1,16 +1,19 @@ /** - * Paratera_API_KEY=sk-abcxxxxxxxxxxxxxxxxxxxxxxx123 ts-node scripts/update-i18n.ts + * 使用 OpenAI 兼容的模型生成 i18n 文本,并更新到 translate 目录 + * + * API_KEY=sk-xxxx BASE_URL=xxxx MODEL=xxxx ts-node scripts/update-i18n.ts */ -// OCOOL API KEY -const Paratera_API_KEY = process.env.Paratera_API_KEY +const API_KEY = process.env.API_KEY +const BASE_URL = process.env.BASE_URL || 'https://llmapi.paratera.com/v1' +const MODEL = process.env.MODEL || 'Qwen3-235B-A22B' const INDEX = [ - // 语言的名称 代码 用来翻译的模型 - { name: 'France', code: 'fr-fr', model: 'Qwen3-235B-A22B' }, - { name: 'Spanish', code: 'es-es', model: 'Qwen3-235B-A22B' }, - { name: 'Portuguese', code: 'pt-pt', model: 'Qwen3-235B-A22B' }, - { name: 'Greek', code: 'el-gr', model: 'Qwen3-235B-A22B' } + // 语言的名称代码用来翻译的模型 + { name: 'France', code: 'fr-fr', model: MODEL }, + { name: 'Spanish', code: 'es-es', model: MODEL }, + { name: 'Portuguese', code: 'pt-pt', model: MODEL }, + { name: 'Greek', code: 'el-gr', model: MODEL } ] const fs = require('fs') @@ -19,8 +22,8 @@ import OpenAI from 'openai' const zh = JSON.parse(fs.readFileSync('src/renderer/src/i18n/locales/zh-cn.json', 'utf8')) as object const openai = new OpenAI({ - apiKey: Paratera_API_KEY, - baseURL: 'https://llmapi.paratera.com/v1' + apiKey: API_KEY, + baseURL: BASE_URL }) // 递归遍历翻译 diff --git a/src/main/configs/SelectionConfig.ts b/src/main/configs/SelectionConfig.ts index 59988ded74..31868a4708 100644 --- a/src/main/configs/SelectionConfig.ts +++ b/src/main/configs/SelectionConfig.ts @@ -1,6 +1,6 @@ interface IFilterList { WINDOWS: string[] - MAC?: string[] + MAC: string[] } interface IFinetunedList { @@ -45,14 +45,17 @@ export const SELECTION_PREDEFINED_BLACKLIST: IFilterList = { 'sldworks.exe', // Remote Desktop 'mstsc.exe' - ] + ], + MAC: [] } export const SELECTION_FINETUNED_LIST: IFinetunedList = { EXCLUDE_CLIPBOARD_CURSOR_DETECT: { - WINDOWS: ['acrobat.exe', 'wps.exe', 'cajviewer.exe'] + WINDOWS: ['acrobat.exe', 'wps.exe', 'cajviewer.exe'], + MAC: [] }, INCLUDE_CLIPBOARD_DELAY_READ: { - WINDOWS: ['acrobat.exe', 'wps.exe', 'cajviewer.exe', 'foxitphantom.exe'] + WINDOWS: ['acrobat.exe', 'wps.exe', 'cajviewer.exe', 'foxitphantom.exe'], + MAC: [] } } diff --git a/src/main/index.ts b/src/main/index.ts index 3699335a90..97bd10bcf7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -11,7 +11,7 @@ import { app } from 'electron' import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer' import Logger from 'electron-log' -import { isDev, isWin } from './constant' +import { isDev, isWin, isLinux } from './constant' import { registerIpc } from './ipc' import { configManager } from './services/ConfigManager' import mcpService from './services/MCPService' @@ -28,6 +28,14 @@ import { windowService } from './services/WindowService' Logger.initialize() +/** + * Disable hardware acceleration if setting is enabled + */ +const disableHardwareAcceleration = configManager.getDisableHardwareAcceleration() +if (disableHardwareAcceleration) { + app.disableHardwareAcceleration() +} + /** * Disable chromium's window animations * main purpose for this is to avoid the transparent window flashing when it is shown @@ -38,6 +46,14 @@ if (isWin) { app.commandLine.appendSwitch('wm-window-animations-disabled') } +/** + * Enable GlobalShortcutsPortal for Linux Wayland Protocol + * see: https://www.electronjs.org/docs/latest/api/global-shortcut + */ +if (isLinux && process.env.XDG_SESSION_TYPE === 'wayland') { + app.commandLine.appendSwitch('enable-features', 'GlobalShortcutsPortal') +} + // Enable features for unresponsive renderer js call stacks app.commandLine.appendSwitch('enable-features', 'DocumentPolicyIncludeJSCallStacksInCrashReports') app.on('web-contents-created', (_, webContents) => { @@ -124,19 +140,27 @@ if (!app.requestSingleInstanceLock()) { registerProtocolClient(app) // macOS specific: handle protocol when app is already running + app.on('open-url', (event, url) => { event.preventDefault() handleProtocolUrl(url) }) + const handleOpenUrl = (args: string[]) => { + const url = args.find((arg) => arg.startsWith(CHERRY_STUDIO_PROTOCOL + '://')) + if (url) handleProtocolUrl(url) + } + + // for windows to start with url + handleOpenUrl(process.argv) + // Listen for second instance app.on('second-instance', (_event, argv) => { windowService.showMainWindow() // Protocol handler for Windows/Linux // The commandLine is an array of strings where the last item might be the URL - const url = argv.find((arg) => arg.startsWith(CHERRY_STUDIO_PROTOCOL + '://')) - if (url) handleProtocolUrl(url) + handleOpenUrl(argv) }) app.on('browser-window-created', (_, window) => { diff --git a/src/main/ipc.ts b/src/main/ipc.ts index af043c7c8c..37abf38dff 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -2,30 +2,34 @@ import fs from 'node:fs' import { arch } from 'node:os' import path from 'node:path' -import { isMac, isWin } from '@main/constant' +import { isLinux, isMac, isWin } from '@main/constant' import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process' import { handleZoomFactor } from '@main/utils/zoom' import { UpgradeChannel } from '@shared/config/constant' import { IpcChannel } from '@shared/IpcChannel' -import { Shortcut, ThemeMode } from '@types' -import { BrowserWindow, dialog, ipcMain, session, shell, webContents } from 'electron' +import { FileMetadata, Provider, Shortcut, ThemeMode } from '@types' +import { BrowserWindow, dialog, ipcMain, ProxyConfig, session, shell, systemPreferences, webContents } from 'electron' import log from 'electron-log' import { Notification } from 'src/renderer/src/types/notification' +import appService from './services/AppService' import AppUpdater from './services/AppUpdater' import BackupManager from './services/BackupManager' import { configManager } from './services/ConfigManager' import CopilotService from './services/CopilotService' +import DxtService from './services/DxtService' import { ExportService } from './services/ExportService' -import FileService from './services/FileService' import FileStorage from './services/FileStorage' +import FileService from './services/FileSystemService' import KnowledgeService from './services/KnowledgeService' import mcpService from './services/MCPService' +import MemoryService from './services/memory/MemoryService' import NotificationService from './services/NotificationService' import * as NutstoreService from './services/NutstoreService' import ObsidianVaultService from './services/ObsidianVaultService' -import { ProxyConfig, proxyManager } from './services/ProxyManager' +import { proxyManager } from './services/ProxyManager' import { pythonService } from './services/PythonService' +import { FileServiceManager } from './services/remotefile/FileServiceManager' import { searchService } from './services/SearchService' import { SelectionService } from './services/SelectionService' import { registerShortcuts, unregisterAllShortcuts } from './services/ShortcutService' @@ -44,6 +48,8 @@ const backupManager = new BackupManager() const exportService = new ExportService(fileManager) const obsidianVaultService = new ObsidianVaultService() const vertexAIService = VertexAIService.getInstance() +const memoryService = MemoryService.getInstance() +const dxtService = new DxtService() export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { const appUpdater = new AppUpdater(mainWindow) @@ -72,9 +78,9 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { if (proxy === 'system') { proxyConfig = { mode: 'system' } } else if (proxy) { - proxyConfig = { mode: 'custom', url: proxy } + proxyConfig = { mode: 'fixed_servers', proxyRules: proxy } } else { - proxyConfig = { mode: 'none' } + proxyConfig = { mode: 'direct' } } await proxyManager.configureProxy(proxyConfig) @@ -113,12 +119,8 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { }) // launch on boot - ipcMain.handle(IpcChannel.App_SetLaunchOnBoot, (_, openAtLogin: boolean) => { - // Set login item settings for windows and mac - // linux is not supported because it requires more file operations - if (isWin || isMac) { - app.setLoginItemSettings({ openAtLogin }) - } + ipcMain.handle(IpcChannel.App_SetLaunchOnBoot, (_, isLaunchOnBoot: boolean) => { + appService.setAppLaunchOnBoot(isLaunchOnBoot) }) // launch to tray @@ -158,6 +160,18 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { } }) + //only for mac + if (isMac) { + ipcMain.handle(IpcChannel.App_MacIsProcessTrusted, (): boolean => { + return systemPreferences.isTrustedAccessibilityClient(false) + }) + + //return is only the current state, not the new state + ipcMain.handle(IpcChannel.App_MacRequestProcessTrust, (): boolean => { + return systemPreferences.isTrustedAccessibilityClient(true) + }) + } + ipcMain.handle(IpcChannel.Config_Set, (_, key: string, value: any, isNotify: boolean = false) => { configManager.set(key, value, isNotify) }) @@ -306,6 +320,17 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { // Relaunch app ipcMain.handle(IpcChannel.App_RelaunchApp, (_, options?: Electron.RelaunchOptions) => { + // Fix for .AppImage + if (isLinux && process.env.APPIMAGE) { + log.info('Relaunching app with options:', process.env.APPIMAGE, options) + // On Linux, we need to use the APPIMAGE environment variable to relaunch + // https://github.com/electron-userland/electron-builder/issues/1727#issuecomment-769896927 + options = options || {} + options.execPath = process.env.APPIMAGE + options.args = options.args || [] + options.args.unshift('--appimage-extract-and-run') + } + app.relaunch(options) app.exit(0) }) @@ -344,6 +369,11 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.Backup_CheckConnection, backupManager.checkConnection) ipcMain.handle(IpcChannel.Backup_CreateDirectory, backupManager.createDirectory) ipcMain.handle(IpcChannel.Backup_DeleteWebdavFile, backupManager.deleteWebdavFile) + ipcMain.handle(IpcChannel.Backup_BackupToLocalDir, backupManager.backupToLocalDir) + ipcMain.handle(IpcChannel.Backup_RestoreFromLocalBackup, backupManager.restoreFromLocalBackup) + ipcMain.handle(IpcChannel.Backup_ListLocalBackupFiles, backupManager.listLocalBackupFiles) + ipcMain.handle(IpcChannel.Backup_DeleteLocalBackupFile, backupManager.deleteLocalBackupFile) + ipcMain.handle(IpcChannel.Backup_SetLocalBackupDir, backupManager.setLocalBackupDir) ipcMain.handle(IpcChannel.Backup_BackupToS3, backupManager.backupToS3) ipcMain.handle(IpcChannel.Backup_RestoreFromS3, backupManager.restoreFromS3) ipcMain.handle(IpcChannel.Backup_ListS3Files, backupManager.listS3Files) @@ -359,9 +389,10 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.File_Clear, fileManager.clear) ipcMain.handle(IpcChannel.File_Read, fileManager.readFile) ipcMain.handle(IpcChannel.File_Delete, fileManager.deleteFile) + ipcMain.handle('file:deleteDir', fileManager.deleteDir) ipcMain.handle(IpcChannel.File_Get, fileManager.getFile) ipcMain.handle(IpcChannel.File_SelectFolder, fileManager.selectFolder) - ipcMain.handle(IpcChannel.File_Create, fileManager.createTempFile) + ipcMain.handle(IpcChannel.File_CreateTempFile, fileManager.createTempFile) ipcMain.handle(IpcChannel.File_Write, fileManager.writeFile) ipcMain.handle(IpcChannel.File_WriteWithId, fileManager.writeFileWithId) ipcMain.handle(IpcChannel.File_SaveImage, fileManager.saveImage) @@ -372,6 +403,28 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.File_Download, fileManager.downloadFile) ipcMain.handle(IpcChannel.File_Copy, fileManager.copyFile) ipcMain.handle(IpcChannel.File_BinaryImage, fileManager.binaryImage) + ipcMain.handle(IpcChannel.File_OpenWithRelativePath, fileManager.openFileWithRelativePath) + + // file service + ipcMain.handle(IpcChannel.FileService_Upload, async (_, provider: Provider, file: FileMetadata) => { + const service = FileServiceManager.getInstance().getService(provider) + return await service.uploadFile(file) + }) + + ipcMain.handle(IpcChannel.FileService_List, async (_, provider: Provider) => { + const service = FileServiceManager.getInstance().getService(provider) + return await service.listFiles() + }) + + ipcMain.handle(IpcChannel.FileService_Delete, async (_, provider: Provider, fileId: string) => { + const service = FileServiceManager.getInstance().getService(provider) + return await service.deleteFile(fileId) + }) + + ipcMain.handle(IpcChannel.FileService_Retrieve, async (_, provider: Provider, fileId: string) => { + const service = FileServiceManager.getInstance().getService(provider) + return await service.retrieveFile(fileId) + }) // fs ipcMain.handle(IpcChannel.Fs_Read, FileService.readFile) @@ -402,6 +455,39 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.KnowledgeBase_Remove, KnowledgeService.remove) ipcMain.handle(IpcChannel.KnowledgeBase_Search, KnowledgeService.search) ipcMain.handle(IpcChannel.KnowledgeBase_Rerank, KnowledgeService.rerank) + ipcMain.handle(IpcChannel.KnowledgeBase_Check_Quota, KnowledgeService.checkQuota) + + // memory + ipcMain.handle(IpcChannel.Memory_Add, async (_, messages, config) => { + return await memoryService.add(messages, config) + }) + ipcMain.handle(IpcChannel.Memory_Search, async (_, query, config) => { + return await memoryService.search(query, config) + }) + ipcMain.handle(IpcChannel.Memory_List, async (_, config) => { + return await memoryService.list(config) + }) + ipcMain.handle(IpcChannel.Memory_Delete, async (_, id) => { + return await memoryService.delete(id) + }) + ipcMain.handle(IpcChannel.Memory_Update, async (_, id, memory, metadata) => { + return await memoryService.update(id, memory, metadata) + }) + ipcMain.handle(IpcChannel.Memory_Get, async (_, memoryId) => { + return await memoryService.get(memoryId) + }) + ipcMain.handle(IpcChannel.Memory_SetConfig, async (_, config) => { + memoryService.setConfig(config) + }) + ipcMain.handle(IpcChannel.Memory_DeleteUser, async (_, userId) => { + return await memoryService.deleteUser(userId) + }) + ipcMain.handle(IpcChannel.Memory_DeleteAllMemoriesForUser, async (_, userId) => { + return await memoryService.deleteAllMemoriesForUser(userId) + }) + ipcMain.handle(IpcChannel.Memory_GetUsersList, async () => { + return await memoryService.getUsersList() + }) // window ipcMain.handle(IpcChannel.Windows_SetMinimumSize, (_, width: number, height: number) => { @@ -452,6 +538,29 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.Mcp_GetResource, mcpService.getResource) ipcMain.handle(IpcChannel.Mcp_GetInstallInfo, mcpService.getInstallInfo) 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_SetProgress, (_, progress: number) => { + mainWindow.webContents.send('mcp-progress', progress) + }) + + // DXT upload handler + ipcMain.handle(IpcChannel.Mcp_UploadDxt, async (event, fileBuffer: ArrayBuffer, fileName: string) => { + try { + // Create a temporary file with the uploaded content + const tempPath = await fileManager.createTempFile(event, fileName) + await fileManager.writeFile(event, tempPath, Buffer.from(fileBuffer)) + + // Process DXT file using the temporary path + return await dxtService.uploadDxt(event, tempPath) + } catch (error) { + log.error('[IPC] DXT upload error:', error) + return { + success: false, + error: error instanceof Error ? error.message : 'Failed to upload DXT file' + } + } + }) // Register Python execution handler ipcMain.handle( @@ -519,4 +628,8 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { SelectionService.registerIpcHandler() ipcMain.handle(IpcChannel.App_QuoteToMain, (_, text: string) => windowService.quoteToMainWindow(text)) + + ipcMain.handle(IpcChannel.App_SetDisableHardwareAcceleration, (_, isDisable: boolean) => { + configManager.setDisableHardwareAcceleration(isDisable) + }) } diff --git a/src/main/knowledage/ocr/BaseOcrProvider.ts b/src/main/knowledage/ocr/BaseOcrProvider.ts new file mode 100644 index 0000000000..1bc7ce8530 --- /dev/null +++ b/src/main/knowledage/ocr/BaseOcrProvider.ts @@ -0,0 +1,122 @@ +import fs from 'node:fs' +import path from 'node:path' + +import { windowService } from '@main/services/WindowService' +import { getFileExt } from '@main/utils/file' +import { FileMetadata, OcrProvider } from '@types' +import { app } from 'electron' +import { TypedArray } from 'pdfjs-dist/types/src/display/api' + +export default abstract class BaseOcrProvider { + protected provider: OcrProvider + public storageDir = path.join(app.getPath('userData'), 'Data', 'Files') + + constructor(provider: OcrProvider) { + if (!provider) { + throw new Error('OCR provider is not set') + } + this.provider = provider + } + abstract parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata; quota?: number }> + + /** + * 检查文件是否已经被预处理过 + * 统一检测方法:如果 Data/Files/{file.id} 是目录,说明已被预处理 + * @param file 文件信息 + * @returns 如果已处理返回处理后的文件信息,否则返回null + */ + public async checkIfAlreadyProcessed(file: FileMetadata): Promise { + try { + // 检查 Data/Files/{file.id} 是否是目录 + const preprocessDirPath = path.join(this.storageDir, file.id) + + if (fs.existsSync(preprocessDirPath)) { + const stats = await fs.promises.stat(preprocessDirPath) + + // 如果是目录,说明已经被预处理过 + if (stats.isDirectory()) { + // 查找目录中的处理结果文件 + const files = await fs.promises.readdir(preprocessDirPath) + + // 查找主要的处理结果文件(.md 或 .txt) + const processedFile = files.find((fileName) => fileName.endsWith('.md') || fileName.endsWith('.txt')) + + if (processedFile) { + const processedFilePath = path.join(preprocessDirPath, processedFile) + const processedStats = await fs.promises.stat(processedFilePath) + const ext = getFileExt(processedFile) + + return { + ...file, + name: file.name.replace(file.ext, ext), + path: processedFilePath, + ext: ext, + size: processedStats.size, + created_at: processedStats.birthtime.toISOString() + } + } + } + } + + return null + } catch (error) { + // 如果检查过程中出现错误,返回null表示未处理 + return null + } + } + + /** + * 辅助方法:延迟执行 + */ + public delay = (ms: number): Promise => { + return new Promise((resolve) => setTimeout(resolve, ms)) + } + + public async readPdf( + source: string | URL | TypedArray, + passwordCallback?: (fn: (password: string) => void, reason: string) => string + ) { + const { getDocument } = await import('pdfjs-dist/legacy/build/pdf.mjs') + const documentLoadingTask = getDocument(source) + if (passwordCallback) { + documentLoadingTask.onPassword = passwordCallback + } + + const document = await documentLoadingTask.promise + return document + } + + public async sendOcrProgress(sourceId: string, progress: number): Promise { + const mainWindow = windowService.getMainWindow() + mainWindow?.webContents.send('file-ocr-progress', { + itemId: sourceId, + progress: progress + }) + } + + /** + * 将文件移动到附件目录 + * @param fileId 文件id + * @param filePaths 需要移动的文件路径数组 + * @returns 移动后的文件路径数组 + */ + public moveToAttachmentsDir(fileId: string, filePaths: string[]): string[] { + const attachmentsPath = path.join(this.storageDir, fileId) + if (!fs.existsSync(attachmentsPath)) { + fs.mkdirSync(attachmentsPath, { recursive: true }) + } + + const movedPaths: string[] = [] + + for (const filePath of filePaths) { + if (fs.existsSync(filePath)) { + const fileName = path.basename(filePath) + const destPath = path.join(attachmentsPath, fileName) + fs.copyFileSync(filePath, destPath) + fs.unlinkSync(filePath) // 删除原文件,实现"移动" + movedPaths.push(destPath) + } + } + return movedPaths + } +} diff --git a/src/main/knowledage/ocr/DefaultOcrProvider.ts b/src/main/knowledage/ocr/DefaultOcrProvider.ts new file mode 100644 index 0000000000..83c8d51c91 --- /dev/null +++ b/src/main/knowledage/ocr/DefaultOcrProvider.ts @@ -0,0 +1,12 @@ +import { FileMetadata, OcrProvider } from '@types' + +import BaseOcrProvider from './BaseOcrProvider' + +export default class DefaultOcrProvider extends BaseOcrProvider { + constructor(provider: OcrProvider) { + super(provider) + } + public parseFile(): Promise<{ processedFile: FileMetadata }> { + throw new Error('Method not implemented.') + } +} diff --git a/src/main/knowledage/ocr/MacSysOcrProvider.ts b/src/main/knowledage/ocr/MacSysOcrProvider.ts new file mode 100644 index 0000000000..df281eb60b --- /dev/null +++ b/src/main/knowledage/ocr/MacSysOcrProvider.ts @@ -0,0 +1,128 @@ +import { isMac } from '@main/constant' +import { FileMetadata, OcrProvider } from '@types' +import Logger from 'electron-log' +import * as fs from 'fs' +import * as path from 'path' +import { TextItem } from 'pdfjs-dist/types/src/display/api' + +import BaseOcrProvider from './BaseOcrProvider' + +export default class MacSysOcrProvider extends BaseOcrProvider { + private readonly MIN_TEXT_LENGTH = 1000 + private MacOCR: any + + private async initMacOCR() { + if (!isMac) { + throw new Error('MacSysOcrProvider is only available on macOS') + } + if (!this.MacOCR) { + try { + // @ts-ignore This module is optional and only installed/available on macOS. Runtime checks prevent execution on other platforms. + const module = await import('@cherrystudio/mac-system-ocr') + this.MacOCR = module.default + } catch (error) { + Logger.error('[OCR] Failed to load mac-system-ocr:', error) + throw error + } + } + return this.MacOCR + } + + private getRecognitionLevel(level?: number) { + return level === 0 ? this.MacOCR.RECOGNITION_LEVEL_FAST : this.MacOCR.RECOGNITION_LEVEL_ACCURATE + } + + constructor(provider: OcrProvider) { + super(provider) + } + + private async processPages( + results: any, + totalPages: number, + sourceId: string, + writeStream: fs.WriteStream + ): Promise { + await this.initMacOCR() + // TODO: 下个版本后面使用批处理,以及p-queue来优化 + for (let i = 0; i < totalPages; i++) { + // Convert pages to buffers + const pageNum = i + 1 + const pageBuffer = await results.getPage(pageNum) + + // Process batch + const ocrResult = await this.MacOCR.recognizeFromBuffer(pageBuffer, { + ocrOptions: { + recognitionLevel: this.getRecognitionLevel(this.provider.options?.recognitionLevel), + minConfidence: this.provider.options?.minConfidence || 0.5 + } + }) + + // Write results in order + writeStream.write(ocrResult.text + '\n') + + // Update progress + await this.sendOcrProgress(sourceId, (pageNum / totalPages) * 100) + } + } + + public async isScanPdf(buffer: Buffer): Promise { + const doc = await this.readPdf(new Uint8Array(buffer)) + const pageLength = doc.numPages + let counts = 0 + const pagesToCheck = Math.min(pageLength, 10) + for (let i = 0; i < pagesToCheck; i++) { + const page = await doc.getPage(i + 1) + const pageData = await page.getTextContent() + const pageText = pageData.items.map((item) => (item as TextItem).str).join('') + counts += pageText.length + if (counts >= this.MIN_TEXT_LENGTH) { + return false + } + } + return true + } + + public async parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata }> { + Logger.info(`[OCR] Starting OCR process for file: ${file.name}`) + if (file.ext === '.pdf') { + try { + const { pdf } = await import('@cherrystudio/pdf-to-img-napi') + const pdfBuffer = await fs.promises.readFile(file.path) + const results = await pdf(pdfBuffer, { + scale: 2 + }) + const totalPages = results.length + + const baseDir = path.dirname(file.path) + const baseName = path.basename(file.path, path.extname(file.path)) + const txtFileName = `${baseName}.txt` + const txtFilePath = path.join(baseDir, txtFileName) + + const writeStream = fs.createWriteStream(txtFilePath) + await this.processPages(results, totalPages, sourceId, writeStream) + + await new Promise((resolve, reject) => { + writeStream.end(() => { + Logger.info(`[OCR] OCR process completed successfully for ${file.origin_name}`) + resolve() + }) + writeStream.on('error', reject) + }) + const movedPaths = this.moveToAttachmentsDir(file.id, [txtFilePath]) + return { + processedFile: { + ...file, + name: txtFileName, + path: movedPaths[0], + ext: '.txt', + size: fs.statSync(movedPaths[0]).size + } + } + } catch (error) { + Logger.error('[OCR] Error during OCR process:', error) + throw error + } + } + return { processedFile: file } + } +} diff --git a/src/main/knowledage/ocr/OcrProvider.ts b/src/main/knowledage/ocr/OcrProvider.ts new file mode 100644 index 0000000000..07587f01e0 --- /dev/null +++ b/src/main/knowledage/ocr/OcrProvider.ts @@ -0,0 +1,26 @@ +import { FileMetadata, OcrProvider as Provider } from '@types' + +import BaseOcrProvider from './BaseOcrProvider' +import OcrProviderFactory from './OcrProviderFactory' + +export default class OcrProvider { + private sdk: BaseOcrProvider + constructor(provider: Provider) { + this.sdk = OcrProviderFactory.create(provider) + } + public async parseFile( + sourceId: string, + file: FileMetadata + ): Promise<{ processedFile: FileMetadata; quota?: number }> { + return this.sdk.parseFile(sourceId, file) + } + + /** + * 检查文件是否已经被预处理过 + * @param file 文件信息 + * @returns 如果已处理返回处理后的文件信息,否则返回null + */ + public async checkIfAlreadyProcessed(file: FileMetadata): Promise { + return this.sdk.checkIfAlreadyProcessed(file) + } +} diff --git a/src/main/knowledage/ocr/OcrProviderFactory.ts b/src/main/knowledage/ocr/OcrProviderFactory.ts new file mode 100644 index 0000000000..96d95a63ad --- /dev/null +++ b/src/main/knowledage/ocr/OcrProviderFactory.ts @@ -0,0 +1,20 @@ +import { isMac } from '@main/constant' +import { OcrProvider } from '@types' +import Logger from 'electron-log' + +import BaseOcrProvider from './BaseOcrProvider' +import DefaultOcrProvider from './DefaultOcrProvider' +import MacSysOcrProvider from './MacSysOcrProvider' +export default class OcrProviderFactory { + static create(provider: OcrProvider): BaseOcrProvider { + switch (provider.id) { + case 'system': + if (!isMac) { + Logger.warn('[OCR] System OCR provider is only available on macOS') + } + return new MacSysOcrProvider(provider) + default: + return new DefaultOcrProvider(provider) + } + } +} diff --git a/src/main/knowledage/preprocess/BasePreprocessProvider.ts b/src/main/knowledage/preprocess/BasePreprocessProvider.ts new file mode 100644 index 0000000000..016e4d10d0 --- /dev/null +++ b/src/main/knowledage/preprocess/BasePreprocessProvider.ts @@ -0,0 +1,126 @@ +import fs from 'node:fs' +import path from 'node:path' + +import { windowService } from '@main/services/WindowService' +import { getFileExt } from '@main/utils/file' +import { FileMetadata, PreprocessProvider } from '@types' +import { app } from 'electron' +import { TypedArray } from 'pdfjs-dist/types/src/display/api' + +export default abstract class BasePreprocessProvider { + protected provider: PreprocessProvider + protected userId?: string + public storageDir = path.join(app.getPath('userData'), 'Data', 'Files') + + constructor(provider: PreprocessProvider, userId?: string) { + if (!provider) { + throw new Error('Preprocess provider is not set') + } + this.provider = provider + this.userId = userId + } + abstract parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata; quota?: number }> + + abstract checkQuota(): Promise + + /** + * 检查文件是否已经被预处理过 + * 统一检测方法:如果 Data/Files/{file.id} 是目录,说明已被预处理 + * @param file 文件信息 + * @returns 如果已处理返回处理后的文件信息,否则返回null + */ + public async checkIfAlreadyProcessed(file: FileMetadata): Promise { + try { + // 检查 Data/Files/{file.id} 是否是目录 + const preprocessDirPath = path.join(this.storageDir, file.id) + + if (fs.existsSync(preprocessDirPath)) { + const stats = await fs.promises.stat(preprocessDirPath) + + // 如果是目录,说明已经被预处理过 + if (stats.isDirectory()) { + // 查找目录中的处理结果文件 + const files = await fs.promises.readdir(preprocessDirPath) + + // 查找主要的处理结果文件(.md 或 .txt) + const processedFile = files.find((fileName) => fileName.endsWith('.md') || fileName.endsWith('.txt')) + + if (processedFile) { + const processedFilePath = path.join(preprocessDirPath, processedFile) + const processedStats = await fs.promises.stat(processedFilePath) + const ext = getFileExt(processedFile) + + return { + ...file, + name: file.name.replace(file.ext, ext), + path: processedFilePath, + ext: ext, + size: processedStats.size, + created_at: processedStats.birthtime.toISOString() + } + } + } + } + + return null + } catch (error) { + // 如果检查过程中出现错误,返回null表示未处理 + return null + } + } + + /** + * 辅助方法:延迟执行 + */ + public delay = (ms: number): Promise => { + return new Promise((resolve) => setTimeout(resolve, ms)) + } + + public async readPdf( + source: string | URL | TypedArray, + passwordCallback?: (fn: (password: string) => void, reason: string) => string + ) { + const { getDocument } = await import('pdfjs-dist/legacy/build/pdf.mjs') + const documentLoadingTask = getDocument(source) + if (passwordCallback) { + documentLoadingTask.onPassword = passwordCallback + } + + const document = await documentLoadingTask.promise + return document + } + + public async sendPreprocessProgress(sourceId: string, progress: number): Promise { + const mainWindow = windowService.getMainWindow() + mainWindow?.webContents.send('file-preprocess-progress', { + itemId: sourceId, + progress: progress + }) + } + + /** + * 将文件移动到附件目录 + * @param fileId 文件id + * @param filePaths 需要移动的文件路径数组 + * @returns 移动后的文件路径数组 + */ + public moveToAttachmentsDir(fileId: string, filePaths: string[]): string[] { + const attachmentsPath = path.join(this.storageDir, fileId) + if (!fs.existsSync(attachmentsPath)) { + fs.mkdirSync(attachmentsPath, { recursive: true }) + } + + const movedPaths: string[] = [] + + for (const filePath of filePaths) { + if (fs.existsSync(filePath)) { + const fileName = path.basename(filePath) + const destPath = path.join(attachmentsPath, fileName) + fs.copyFileSync(filePath, destPath) + fs.unlinkSync(filePath) // 删除原文件,实现"移动" + movedPaths.push(destPath) + } + } + return movedPaths + } +} diff --git a/src/main/knowledage/preprocess/DefaultPreprocessProvider.ts b/src/main/knowledage/preprocess/DefaultPreprocessProvider.ts new file mode 100644 index 0000000000..3899a3d25a --- /dev/null +++ b/src/main/knowledage/preprocess/DefaultPreprocessProvider.ts @@ -0,0 +1,16 @@ +import { FileMetadata, PreprocessProvider } from '@types' + +import BasePreprocessProvider from './BasePreprocessProvider' + +export default class DefaultPreprocessProvider extends BasePreprocessProvider { + constructor(provider: PreprocessProvider) { + super(provider) + } + public parseFile(): Promise<{ processedFile: FileMetadata }> { + throw new Error('Method not implemented.') + } + + public checkQuota(): Promise { + throw new Error('Method not implemented.') + } +} diff --git a/src/main/knowledage/preprocess/Doc2xPreprocessProvider.ts b/src/main/knowledage/preprocess/Doc2xPreprocessProvider.ts new file mode 100644 index 0000000000..5851d33b86 --- /dev/null +++ b/src/main/knowledage/preprocess/Doc2xPreprocessProvider.ts @@ -0,0 +1,329 @@ +import fs from 'node:fs' +import path from 'node:path' + +import { FileMetadata, PreprocessProvider } from '@types' +import AdmZip from 'adm-zip' +import axios, { AxiosRequestConfig } from 'axios' +import Logger from 'electron-log' + +import BasePreprocessProvider from './BasePreprocessProvider' + +type ApiResponse = { + code: string + data: T + message?: string +} + +type PreuploadResponse = { + uid: string + url: string +} + +type StatusResponse = { + status: string + progress: number +} + +type ParsedFileResponse = { + status: string + url: string +} + +export default class Doc2xPreprocessProvider extends BasePreprocessProvider { + constructor(provider: PreprocessProvider) { + super(provider) + } + + private async validateFile(filePath: string): Promise { + const pdfBuffer = await fs.promises.readFile(filePath) + + const doc = await this.readPdf(new Uint8Array(pdfBuffer)) + + // 文件页数小于1000页 + if (doc.numPages >= 1000) { + throw new Error(`PDF page count (${doc.numPages}) exceeds the limit of 1000 pages`) + } + // 文件大小小于300MB + if (pdfBuffer.length >= 300 * 1024 * 1024) { + const fileSizeMB = Math.round(pdfBuffer.length / (1024 * 1024)) + throw new Error(`PDF file size (${fileSizeMB}MB) exceeds the limit of 300MB`) + } + } + + public async parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata }> { + try { + Logger.info(`Preprocess processing started: ${file.path}`) + + // 步骤1: 准备上传 + const { uid, url } = await this.preupload() + Logger.info(`Preprocess preupload completed: uid=${uid}`) + + await this.validateFile(file.path) + + // 步骤2: 上传文件 + await this.putFile(file.path, url) + + // 步骤3: 等待处理完成 + await this.waitForProcessing(sourceId, uid) + Logger.info(`Preprocess parsing completed successfully for: ${file.path}`) + + // 步骤4: 导出文件 + const { path: outputPath } = await this.exportFile(file, uid) + + // 步骤5: 创建处理后的文件信息 + return { + processedFile: this.createProcessedFileInfo(file, outputPath) + } + } catch (error) { + Logger.error( + `Preprocess processing failed for ${file.path}: ${error instanceof Error ? error.message : String(error)}` + ) + throw error + } + } + + private createProcessedFileInfo(file: FileMetadata, outputPath: string): FileMetadata { + const outputFilePath = `${outputPath}/${file.name.split('.').slice(0, -1).join('.')}.md` + return { + ...file, + name: file.name.replace('.pdf', '.md'), + path: outputFilePath, + ext: '.md', + size: fs.statSync(outputFilePath).size + } + } + + /** + * 导出文件 + * @param file 文件信息 + * @param uid 预上传响应的uid + * @returns 导出文件的路径 + */ + public async exportFile(file: FileMetadata, uid: string): Promise<{ path: string }> { + Logger.info(`Exporting file: ${file.path}`) + + // 步骤1: 转换文件 + await this.convertFile(uid, file.path) + Logger.info(`File conversion completed for: ${file.path}`) + + // 步骤2: 等待导出并获取URL + const exportUrl = await this.waitForExport(uid) + + // 步骤3: 下载并解压文件 + return this.downloadFile(exportUrl, file) + } + + /** + * 等待处理完成 + * @param sourceId 源文件ID + * @param uid 预上传响应的uid + */ + private async waitForProcessing(sourceId: string, uid: string): Promise { + while (true) { + await this.delay(1000) + const { status, progress } = await this.getStatus(uid) + await this.sendPreprocessProgress(sourceId, progress) + Logger.info(`Preprocess processing status: ${status}, progress: ${progress}%`) + + if (status === 'success') { + return + } else if (status === 'failed') { + throw new Error('Preprocess processing failed') + } + } + } + + /** + * 等待导出完成 + * @param uid 预上传响应的uid + * @returns 导出文件的url + */ + private async waitForExport(uid: string): Promise { + while (true) { + await this.delay(1000) + const { status, url } = await this.getParsedFile(uid) + Logger.info(`Export status: ${status}`) + + if (status === 'success' && url) { + return url + } else if (status === 'failed') { + throw new Error('Export failed') + } + } + } + + /** + * 预上传文件 + * @returns 预上传响应的url和uid + */ + private async preupload(): Promise { + const config = this.createAuthConfig() + const endpoint = `${this.provider.apiHost}/api/v2/parse/preupload` + + try { + const { data } = await axios.post>(endpoint, null, config) + + if (data.code === 'success' && data.data) { + return data.data + } else { + throw new Error(`API returned error: ${data.message || JSON.stringify(data)}`) + } + } catch (error) { + Logger.error(`Failed to get preupload URL: ${error instanceof Error ? error.message : String(error)}`) + throw new Error('Failed to get preupload URL') + } + } + + /** + * 上传文件 + * @param filePath 文件路径 + * @param url 预上传响应的url + */ + private async putFile(filePath: string, url: string): Promise { + try { + const fileStream = fs.createReadStream(filePath) + const response = await axios.put(url, fileStream) + + if (response.status !== 200) { + throw new Error(`HTTP status ${response.status}: ${response.statusText}`) + } + } catch (error) { + Logger.error(`Failed to upload file ${filePath}: ${error instanceof Error ? error.message : String(error)}`) + throw new Error('Failed to upload file') + } + } + + private async getStatus(uid: string): Promise { + const config = this.createAuthConfig() + const endpoint = `${this.provider.apiHost}/api/v2/parse/status?uid=${uid}` + + try { + const response = await axios.get>(endpoint, config) + + if (response.data.code === 'success' && response.data.data) { + return response.data.data + } else { + throw new Error(`API returned error: ${response.data.message || JSON.stringify(response.data)}`) + } + } catch (error) { + Logger.error(`Failed to get status for uid ${uid}: ${error instanceof Error ? error.message : String(error)}`) + throw new Error('Failed to get processing status') + } + } + + /** + * Preprocess文件 + * @param uid 预上传响应的uid + * @param filePath 文件路径 + */ + private async convertFile(uid: string, filePath: string): Promise { + const fileName = path.parse(filePath).name + const config = { + ...this.createAuthConfig(), + headers: { + ...this.createAuthConfig().headers, + 'Content-Type': 'application/json' + } + } + + const payload = { + uid, + to: 'md', + formula_mode: 'normal', + filename: fileName + } + + const endpoint = `${this.provider.apiHost}/api/v2/convert/parse` + + try { + const response = await axios.post>(endpoint, payload, config) + + if (response.data.code !== 'success') { + throw new Error(`API returned error: ${response.data.message || JSON.stringify(response.data)}`) + } + } catch (error) { + Logger.error(`Failed to convert file ${filePath}: ${error instanceof Error ? error.message : String(error)}`) + throw new Error('Failed to convert file') + } + } + + /** + * 获取解析后的文件信息 + * @param uid 预上传响应的uid + * @returns 解析后的文件信息 + */ + private async getParsedFile(uid: string): Promise { + const config = this.createAuthConfig() + const endpoint = `${this.provider.apiHost}/api/v2/convert/parse/result?uid=${uid}` + + try { + const response = await axios.get>(endpoint, config) + + if (response.status === 200 && response.data.data) { + return response.data.data + } else { + throw new Error(`HTTP status ${response.status}: ${response.statusText}`) + } + } catch (error) { + Logger.error( + `Failed to get parsed file for uid ${uid}: ${error instanceof Error ? error.message : String(error)}` + ) + throw new Error('Failed to get parsed file information') + } + } + + /** + * 下载文件 + * @param url 导出文件的url + * @param file 文件信息 + * @returns 下载文件的路径 + */ + private async downloadFile(url: string, file: FileMetadata): Promise<{ path: string }> { + const dirPath = this.storageDir + // 使用统一的存储路径:Data/Files/{file.id}/ + const extractPath = path.join(dirPath, file.id) + const zipPath = path.join(dirPath, `${file.id}.zip`) + + // 确保目录存在 + fs.mkdirSync(dirPath, { recursive: true }) + fs.mkdirSync(extractPath, { recursive: true }) + + Logger.info(`Downloading to export path: ${zipPath}`) + + try { + // 下载文件 + const response = await axios.get(url, { responseType: 'arraybuffer' }) + fs.writeFileSync(zipPath, response.data) + + // 确保提取目录存在 + if (!fs.existsSync(extractPath)) { + fs.mkdirSync(extractPath, { recursive: true }) + } + + // 解压文件 + const zip = new AdmZip(zipPath) + zip.extractAllTo(extractPath, true) + Logger.info(`Extracted files to: ${extractPath}`) + + // 删除临时ZIP文件 + fs.unlinkSync(zipPath) + + return { path: extractPath } + } catch (error) { + Logger.error(`Failed to download and extract file: ${error instanceof Error ? error.message : String(error)}`) + throw new Error('Failed to download and extract file') + } + } + + private createAuthConfig(): AxiosRequestConfig { + return { + headers: { + Authorization: `Bearer ${this.provider.apiKey}` + } + } + } + + public checkQuota(): Promise { + throw new Error('Method not implemented.') + } +} diff --git a/src/main/knowledage/preprocess/MineruPreprocessProvider.ts b/src/main/knowledage/preprocess/MineruPreprocessProvider.ts new file mode 100644 index 0000000000..58c0c00c23 --- /dev/null +++ b/src/main/knowledage/preprocess/MineruPreprocessProvider.ts @@ -0,0 +1,394 @@ +import fs from 'node:fs' +import path from 'node:path' + +import { FileMetadata, PreprocessProvider } from '@types' +import AdmZip from 'adm-zip' +import axios from 'axios' +import Logger from 'electron-log' + +import BasePreprocessProvider from './BasePreprocessProvider' + +type ApiResponse = { + code: number + data: T + msg?: string + trace_id?: string +} + +type BatchUploadResponse = { + batch_id: string + file_urls: string[] +} + +type ExtractProgress = { + extracted_pages: number + total_pages: number + start_time: string +} + +type ExtractFileResult = { + file_name: string + state: 'done' | 'waiting-file' | 'pending' | 'running' | 'converting' | 'failed' + err_msg: string + full_zip_url?: string + extract_progress?: ExtractProgress +} + +type ExtractResultResponse = { + batch_id: string + extract_result: ExtractFileResult[] +} + +type QuotaResponse = { + code: number + data: { + user_left_quota: number + total_left_quota: number + } + msg?: string + trace_id?: string +} + +export default class MineruPreprocessProvider extends BasePreprocessProvider { + constructor(provider: PreprocessProvider, userId?: string) { + super(provider, userId) + // todo:免费期结束后删除 + this.provider.apiKey = this.provider.apiKey || import.meta.env.MAIN_VITE_MINERU_API_KEY + } + + public async parseFile( + sourceId: string, + file: FileMetadata + ): Promise<{ processedFile: FileMetadata; quota: number }> { + try { + Logger.info(`MinerU preprocess processing started: ${file.path}`) + await this.validateFile(file.path) + + // 1. 获取上传URL并上传文件 + const batchId = await this.uploadFile(file) + Logger.info(`MinerU file upload completed: batch_id=${batchId}`) + + // 2. 等待处理完成并获取结果 + const extractResult = await this.waitForCompletion(sourceId, batchId, file.origin_name) + Logger.info(`MinerU processing completed for batch: ${batchId}`) + + // 3. 下载并解压文件 + const { path: outputPath } = await this.downloadAndExtractFile(extractResult.full_zip_url!, file) + + // 4. check quota + const quota = await this.checkQuota() + + // 5. 创建处理后的文件信息 + return { + processedFile: this.createProcessedFileInfo(file, outputPath), + quota + } + } catch (error: any) { + Logger.error(`MinerU preprocess processing failed for ${file.path}: ${error.message}`) + throw new Error(error.message) + } + } + + public async checkQuota() { + try { + const quota = await fetch(`${this.provider.apiHost}/api/v4/quota`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.provider.apiKey}`, + token: this.userId ?? '' + } + }) + if (!quota.ok) { + throw new Error(`HTTP ${quota.status}: ${quota.statusText}`) + } + const response: QuotaResponse = await quota.json() + return response.data.user_left_quota + } catch (error) { + console.error('Error checking quota:', error) + throw error + } + } + + private async validateFile(filePath: string): Promise { + const pdfBuffer = await fs.promises.readFile(filePath) + + const doc = await this.readPdf(new Uint8Array(pdfBuffer)) + + // 文件页数小于600页 + if (doc.numPages >= 600) { + throw new Error(`PDF page count (${doc.numPages}) exceeds the limit of 600 pages`) + } + // 文件大小小于200MB + if (pdfBuffer.length >= 200 * 1024 * 1024) { + const fileSizeMB = Math.round(pdfBuffer.length / (1024 * 1024)) + throw new Error(`PDF file size (${fileSizeMB}MB) exceeds the limit of 200MB`) + } + } + + private createProcessedFileInfo(file: FileMetadata, outputPath: string): FileMetadata { + // 查找解压后的主要文件 + let finalPath = '' + let finalName = file.origin_name.replace('.pdf', '.md') + + try { + const files = fs.readdirSync(outputPath) + + const mdFile = files.find((f) => f.endsWith('.md')) + if (mdFile) { + const originalMdPath = path.join(outputPath, mdFile) + const newMdPath = path.join(outputPath, finalName) + + // 重命名文件为原始文件名 + try { + fs.renameSync(originalMdPath, newMdPath) + finalPath = newMdPath + Logger.info(`Renamed markdown file from ${mdFile} to ${finalName}`) + } catch (renameError) { + Logger.warn(`Failed to rename file ${mdFile} to ${finalName}: ${renameError}`) + // 如果重命名失败,使用原文件 + finalPath = originalMdPath + finalName = mdFile + } + } + } catch (error) { + Logger.warn(`Failed to read output directory ${outputPath}: ${error}`) + finalPath = path.join(outputPath, `${file.id}.md`) + } + + return { + ...file, + name: finalName, + path: finalPath, + ext: '.md', + size: fs.existsSync(finalPath) ? fs.statSync(finalPath).size : 0 + } + } + + private async downloadAndExtractFile(zipUrl: string, file: FileMetadata): Promise<{ path: string }> { + const dirPath = this.storageDir + + const zipPath = path.join(dirPath, `${file.id}.zip`) + const extractPath = path.join(dirPath, `${file.id}`) + + Logger.info(`Downloading MinerU result to: ${zipPath}`) + + try { + // 下载ZIP文件 + const response = await axios.get(zipUrl, { responseType: 'arraybuffer' }) + fs.writeFileSync(zipPath, response.data) + Logger.info(`Downloaded ZIP file: ${zipPath}`) + + // 确保提取目录存在 + if (!fs.existsSync(extractPath)) { + fs.mkdirSync(extractPath, { recursive: true }) + } + + // 解压文件 + const zip = new AdmZip(zipPath) + zip.extractAllTo(extractPath, true) + Logger.info(`Extracted files to: ${extractPath}`) + + // 删除临时ZIP文件 + fs.unlinkSync(zipPath) + + return { path: extractPath } + } catch (error: any) { + Logger.error(`Failed to download and extract file: ${error.message}`) + throw new Error(error.message) + } + } + + private async uploadFile(file: FileMetadata): Promise { + try { + // 步骤1: 获取上传URL + const { batchId, fileUrls } = await this.getBatchUploadUrls(file) + Logger.info(`Got upload URLs for batch: ${batchId}`) + + console.log('batchId:', batchId, 'fileurls:', fileUrls) + // 步骤2: 上传文件到获取的URL + await this.putFileToUrl(file.path, fileUrls[0]) + Logger.info(`File uploaded successfully: ${file.path}`) + + return batchId + } catch (error: any) { + Logger.error(`Failed to upload file ${file.path}: ${error.message}`) + throw new Error(error.message) + } + } + + private async getBatchUploadUrls(file: FileMetadata): Promise<{ batchId: string; fileUrls: string[] }> { + const endpoint = `${this.provider.apiHost}/api/v4/file-urls/batch` + + const payload = { + language: 'auto', + enable_formula: true, + enable_table: true, + files: [ + { + name: file.origin_name, + is_ocr: true, + data_id: file.id + } + ] + } + + try { + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.provider.apiKey}`, + token: this.userId ?? '', + Accept: '*/*' + }, + body: JSON.stringify(payload) + }) + + if (response.ok) { + const data: ApiResponse = await response.json() + if (data.code === 0 && data.data) { + const { batch_id, file_urls } = data.data + return { + batchId: batch_id, + fileUrls: file_urls + } + } else { + throw new Error(`API returned error: ${data.msg || JSON.stringify(data)}`) + } + } else { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + } catch (error: any) { + Logger.error(`Failed to get batch upload URLs: ${error.message}`) + throw new Error(error.message) + } + } + + private async putFileToUrl(filePath: string, uploadUrl: string): Promise { + try { + const fileBuffer = await fs.promises.readFile(filePath) + + const response = await fetch(uploadUrl, { + method: 'PUT', + body: fileBuffer, + headers: { + 'Content-Type': 'application/pdf' + } + // headers: { + // 'Content-Length': fileBuffer.length.toString() + // } + }) + + if (!response.ok) { + // 克隆 response 以避免消费 body stream + const responseClone = response.clone() + + try { + const responseBody = await responseClone.text() + const errorInfo = { + status: response.status, + statusText: response.statusText, + url: response.url, + type: response.type, + redirected: response.redirected, + headers: Object.fromEntries(response.headers.entries()), + body: responseBody + } + + console.error('Response details:', errorInfo) + throw new Error(`Upload failed with status ${response.status}: ${responseBody}`) + } catch (parseError) { + throw new Error(`Upload failed with status ${response.status}. Could not parse response body.`) + } + } + + Logger.info(`File uploaded successfully to: ${uploadUrl}`) + } catch (error: any) { + Logger.error(`Failed to upload file to URL ${uploadUrl}: ${error}`) + throw new Error(error.message) + } + } + + private async getExtractResults(batchId: string): Promise { + const endpoint = `${this.provider.apiHost}/api/v4/extract-results/batch/${batchId}` + + try { + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.provider.apiKey}`, + token: this.userId ?? '' + } + }) + + if (response.ok) { + const data: ApiResponse = await response.json() + if (data.code === 0 && data.data) { + return data.data + } else { + throw new Error(`API returned error: ${data.msg || JSON.stringify(data)}`) + } + } else { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + } catch (error: any) { + Logger.error(`Failed to get extract results for batch ${batchId}: ${error.message}`) + throw new Error(error.message) + } + } + + private async waitForCompletion( + sourceId: string, + batchId: string, + fileName: string, + maxRetries: number = 60, + intervalMs: number = 5000 + ): Promise { + let retries = 0 + + while (retries < maxRetries) { + try { + const result = await this.getExtractResults(batchId) + + // 查找对应文件的处理结果 + const fileResult = result.extract_result.find((item) => item.file_name === fileName) + if (!fileResult) { + throw new Error(`File ${fileName} not found in batch results`) + } + + // 检查处理状态 + if (fileResult.state === 'done' && fileResult.full_zip_url) { + Logger.info(`Processing completed for file: ${fileName}`) + return fileResult + } else if (fileResult.state === 'failed') { + throw new Error(`Processing failed for file: ${fileName}, error: ${fileResult.err_msg}`) + } else if (fileResult.state === 'running') { + // 发送进度更新 + if (fileResult.extract_progress) { + const progress = Math.round( + (fileResult.extract_progress.extracted_pages / fileResult.extract_progress.total_pages) * 100 + ) + await this.sendPreprocessProgress(sourceId, progress) + Logger.info(`File ${fileName} processing progress: ${progress}%`) + } else { + // 如果没有具体进度信息,发送一个通用进度 + await this.sendPreprocessProgress(sourceId, 50) + Logger.info(`File ${fileName} is still processing...`) + } + } + } catch (error) { + Logger.warn(`Failed to check status for batch ${batchId}, retry ${retries + 1}/${maxRetries}`) + if (retries === maxRetries - 1) { + throw error + } + } + + retries++ + await new Promise((resolve) => setTimeout(resolve, intervalMs)) + } + + throw new Error(`Processing timeout for batch: ${batchId}`) + } +} diff --git a/src/main/knowledage/preprocess/MistralPreprocessProvider.ts b/src/main/knowledage/preprocess/MistralPreprocessProvider.ts new file mode 100644 index 0000000000..3150162801 --- /dev/null +++ b/src/main/knowledage/preprocess/MistralPreprocessProvider.ts @@ -0,0 +1,187 @@ +import fs from 'node:fs' + +import { MistralClientManager } from '@main/services/MistralClientManager' +import { MistralService } from '@main/services/remotefile/MistralService' +import { Mistral } from '@mistralai/mistralai' +import { DocumentURLChunk } from '@mistralai/mistralai/models/components/documenturlchunk' +import { ImageURLChunk } from '@mistralai/mistralai/models/components/imageurlchunk' +import { OCRResponse } from '@mistralai/mistralai/models/components/ocrresponse' +import { FileMetadata, FileTypes, PreprocessProvider, Provider } from '@types' +import Logger from 'electron-log' +import path from 'path' + +import BasePreprocessProvider from './BasePreprocessProvider' + +type PreuploadResponse = DocumentURLChunk | ImageURLChunk + +export default class MistralPreprocessProvider extends BasePreprocessProvider { + private sdk: Mistral + private fileService: MistralService + + constructor(provider: PreprocessProvider) { + super(provider) + const clientManager = MistralClientManager.getInstance() + const aiProvider: Provider = { + id: provider.id, + type: 'mistral', + name: provider.name, + apiKey: provider.apiKey!, + apiHost: provider.apiHost!, + models: [] + } + clientManager.initializeClient(aiProvider) + this.sdk = clientManager.getClient() + this.fileService = new MistralService(aiProvider) + } + + private async preupload(file: FileMetadata): Promise { + let document: PreuploadResponse + Logger.info(`preprocess preupload started for local file: ${file.path}`) + + if (file.ext.toLowerCase() === '.pdf') { + const uploadResponse = await this.fileService.uploadFile(file) + + if (uploadResponse.status === 'failed') { + Logger.error('File upload failed:', uploadResponse) + throw new Error('Failed to upload file: ' + uploadResponse.displayName) + } + await this.sendPreprocessProgress(file.id, 15) + const fileUrl = await this.sdk.files.getSignedUrl({ + fileId: uploadResponse.fileId + }) + Logger.info('Got signed URL:', fileUrl) + await this.sendPreprocessProgress(file.id, 20) + document = { + type: 'document_url', + documentUrl: fileUrl.url + } + } else { + const base64Image = Buffer.from(fs.readFileSync(file.path)).toString('base64') + document = { + type: 'image_url', + imageUrl: `data:image/png;base64,${base64Image}` + } + } + + if (!document) { + throw new Error('Unsupported file type') + } + return document + } + + public async parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata }> { + try { + const document = await this.preupload(file) + const result = await this.sdk.ocr.process({ + model: this.provider.model!, + document: document, + includeImageBase64: true + }) + if (result) { + await this.sendPreprocessProgress(sourceId, 100) + const processedFile = this.convertFile(result, file) + return { + processedFile + } + } else { + throw new Error('preprocess processing failed: OCR response is empty') + } + } catch (error) { + throw new Error('preprocess processing failed: ' + error) + } + } + + private convertFile(result: OCRResponse, file: FileMetadata): FileMetadata { + // 使用统一的存储路径:Data/Files/{file.id}/ + const conversionId = file.id + const outputPath = path.join(this.storageDir, file.id) + // const outputPath = this.storageDir + const outputFileName = path.basename(file.path, path.extname(file.path)) + fs.mkdirSync(outputPath, { recursive: true }) + + const markdownParts: string[] = [] + let counter = 0 + + // Process each page + result.pages.forEach((page) => { + let pageMarkdown = page.markdown + + // Process images from this page + page.images.forEach((image) => { + if (image.imageBase64) { + let imageFormat = 'jpeg' // default format + let imageBase64Data = image.imageBase64 + + // Check for data URL prefix more efficiently + const prefixEnd = image.imageBase64.indexOf(';base64,') + if (prefixEnd > 0) { + const prefix = image.imageBase64.substring(0, prefixEnd) + const formatIndex = prefix.indexOf('image/') + if (formatIndex >= 0) { + imageFormat = prefix.substring(formatIndex + 6) + } + imageBase64Data = image.imageBase64.substring(prefixEnd + 8) + } + + const imageFileName = `img-${counter}.${imageFormat}` + const imagePath = path.join(outputPath, imageFileName) + + // Save image file + try { + fs.writeFileSync(imagePath, Buffer.from(imageBase64Data, 'base64')) + + // Update image reference in markdown + // Use relative path for better portability + const relativeImagePath = `./${imageFileName}` + + // Find the start and end of the image markdown + const imgStart = pageMarkdown.indexOf(image.imageBase64) + if (imgStart >= 0) { + // Find the markdown image syntax around this base64 + const mdStart = pageMarkdown.lastIndexOf('![', imgStart) + const mdEnd = pageMarkdown.indexOf(')', imgStart) + + if (mdStart >= 0 && mdEnd >= 0) { + // Replace just this specific image reference + pageMarkdown = + pageMarkdown.substring(0, mdStart) + + `![Image ${counter}](${relativeImagePath})` + + pageMarkdown.substring(mdEnd + 1) + } + } + + counter++ + } catch (error) { + Logger.error(`Failed to save image ${imageFileName}:`, error) + } + } + }) + + markdownParts.push(pageMarkdown) + }) + + // Combine all markdown content with double newlines for readability + const combinedMarkdown = markdownParts.join('\n\n') + + // Write the markdown content to a file + const mdFileName = `${outputFileName}.md` + const mdFilePath = path.join(outputPath, mdFileName) + fs.writeFileSync(mdFilePath, combinedMarkdown) + + return { + id: conversionId, + name: file.name.replace(/\.[^/.]+$/, '.md'), + origin_name: file.origin_name, + path: mdFilePath, + created_at: new Date().toISOString(), + type: FileTypes.DOCUMENT, + ext: '.md', + size: fs.statSync(mdFilePath).size, + count: 1 + } as FileMetadata + } + + public checkQuota(): Promise { + throw new Error('Method not implemented.') + } +} diff --git a/src/main/knowledage/preprocess/PreprocessProvider.ts b/src/main/knowledage/preprocess/PreprocessProvider.ts new file mode 100644 index 0000000000..44a34f64ae --- /dev/null +++ b/src/main/knowledage/preprocess/PreprocessProvider.ts @@ -0,0 +1,30 @@ +import { FileMetadata, PreprocessProvider as Provider } from '@types' + +import BasePreprocessProvider from './BasePreprocessProvider' +import PreprocessProviderFactory from './PreprocessProviderFactory' + +export default class PreprocessProvider { + private sdk: BasePreprocessProvider + constructor(provider: Provider, userId?: string) { + this.sdk = PreprocessProviderFactory.create(provider, userId) + } + public async parseFile( + sourceId: string, + file: FileMetadata + ): Promise<{ processedFile: FileMetadata; quota?: number }> { + return this.sdk.parseFile(sourceId, file) + } + + public async checkQuota(): Promise { + return this.sdk.checkQuota() + } + + /** + * 检查文件是否已经被预处理过 + * @param file 文件信息 + * @returns 如果已处理返回处理后的文件信息,否则返回null + */ + public async checkIfAlreadyProcessed(file: FileMetadata): Promise { + return this.sdk.checkIfAlreadyProcessed(file) + } +} diff --git a/src/main/knowledage/preprocess/PreprocessProviderFactory.ts b/src/main/knowledage/preprocess/PreprocessProviderFactory.ts new file mode 100644 index 0000000000..bebecd388f --- /dev/null +++ b/src/main/knowledage/preprocess/PreprocessProviderFactory.ts @@ -0,0 +1,21 @@ +import { PreprocessProvider } from '@types' + +import BasePreprocessProvider from './BasePreprocessProvider' +import DefaultPreprocessProvider from './DefaultPreprocessProvider' +import Doc2xPreprocessProvider from './Doc2xPreprocessProvider' +import MineruPreprocessProvider from './MineruPreprocessProvider' +import MistralPreprocessProvider from './MistralPreprocessProvider' +export default class PreprocessProviderFactory { + static create(provider: PreprocessProvider, userId?: string): BasePreprocessProvider { + switch (provider.id) { + case 'doc2x': + return new Doc2xPreprocessProvider(provider) + case 'mistral': + return new MistralPreprocessProvider(provider) + case 'mineru': + return new MineruPreprocessProvider(provider, userId) + default: + return new DefaultPreprocessProvider(provider) + } + } +} diff --git a/src/main/embeddings/Embeddings.ts b/src/main/knowledge/embeddings/Embeddings.ts similarity index 71% rename from src/main/embeddings/Embeddings.ts rename to src/main/knowledge/embeddings/Embeddings.ts index 0701e7db2d..0ec17691ec 100644 --- a/src/main/embeddings/Embeddings.ts +++ b/src/main/knowledge/embeddings/Embeddings.ts @@ -1,19 +1,15 @@ import type { BaseEmbeddings } from '@cherrystudio/embedjs-interfaces' -import { KnowledgeBaseParams } from '@types' +import { ApiClient } from '@types' import EmbeddingsFactory from './EmbeddingsFactory' export default class Embeddings { private sdk: BaseEmbeddings - constructor({ model, provider, apiKey, apiVersion, baseURL, dimensions }: KnowledgeBaseParams) { + constructor({ embedApiClient, dimensions }: { embedApiClient: ApiClient; dimensions?: number }) { this.sdk = EmbeddingsFactory.create({ - model, - provider, - apiKey, - apiVersion, - baseURL, + embedApiClient, dimensions - } as KnowledgeBaseParams) + }) } public async init(): Promise { return this.sdk.init() diff --git a/src/main/embeddings/EmbeddingsFactory.ts b/src/main/knowledge/embeddings/EmbeddingsFactory.ts similarity index 69% rename from src/main/embeddings/EmbeddingsFactory.ts rename to src/main/knowledge/embeddings/EmbeddingsFactory.ts index 808db05794..b0ecf360f9 100644 --- a/src/main/embeddings/EmbeddingsFactory.ts +++ b/src/main/knowledge/embeddings/EmbeddingsFactory.ts @@ -3,28 +3,22 @@ import { OllamaEmbeddings } from '@cherrystudio/embedjs-ollama' import { OpenAiEmbeddings } from '@cherrystudio/embedjs-openai' import { AzureOpenAiEmbeddings } from '@cherrystudio/embedjs-openai/src/azure-openai-embeddings' import { getInstanceName } from '@main/utils' -import { KnowledgeBaseParams } from '@types' +import { ApiClient } from '@types' -import { SUPPORTED_DIM_MODELS as VOYAGE_SUPPORTED_DIM_MODELS, VoyageEmbeddings } from './VoyageEmbeddings' +import { VOYAGE_SUPPORTED_DIM_MODELS } from './utils' +import { VoyageEmbeddings } from './VoyageEmbeddings' export default class EmbeddingsFactory { - static create({ model, provider, apiKey, apiVersion, baseURL, dimensions }: KnowledgeBaseParams): BaseEmbeddings { + static create({ embedApiClient, dimensions }: { embedApiClient: ApiClient; dimensions?: number }): BaseEmbeddings { const batchSize = 10 + const { model, provider, apiKey, apiVersion, baseURL } = embedApiClient if (provider === 'voyageai') { - if (VOYAGE_SUPPORTED_DIM_MODELS.includes(model)) { - return new VoyageEmbeddings({ - modelName: model, - apiKey, - outputDimension: dimensions, - batchSize: 8 - }) - } else { - return new VoyageEmbeddings({ - modelName: model, - apiKey, - batchSize: 8 - }) - } + return new VoyageEmbeddings({ + modelName: model, + apiKey, + outputDimension: VOYAGE_SUPPORTED_DIM_MODELS.includes(model) ? dimensions : undefined, + batchSize: 8 + }) } if (provider === 'ollama') { if (baseURL.includes('v1/')) { diff --git a/src/main/embeddings/VoyageEmbeddings.ts b/src/main/knowledge/embeddings/VoyageEmbeddings.ts similarity index 54% rename from src/main/embeddings/VoyageEmbeddings.ts rename to src/main/knowledge/embeddings/VoyageEmbeddings.ts index edec32dc51..61f23ad767 100644 --- a/src/main/embeddings/VoyageEmbeddings.ts +++ b/src/main/knowledge/embeddings/VoyageEmbeddings.ts @@ -1,27 +1,29 @@ import { BaseEmbeddings } from '@cherrystudio/embedjs-interfaces' import { VoyageEmbeddings as _VoyageEmbeddings } from '@langchain/community/embeddings/voyage' +import { VOYAGE_SUPPORTED_DIM_MODELS } from './utils' + /** * 支持设置嵌入维度的模型 */ -export const SUPPORTED_DIM_MODELS = ['voyage-3-large', 'voyage-3.5', 'voyage-3.5-lite', 'voyage-code-3'] export class VoyageEmbeddings extends BaseEmbeddings { private model: _VoyageEmbeddings constructor(private readonly configuration?: ConstructorParameters[0]) { super() - if (!this.configuration) this.configuration = {} - if (!this.configuration.modelName) this.configuration.modelName = 'voyage-3' - if (!SUPPORTED_DIM_MODELS.includes(this.configuration.modelName) && this.configuration.outputDimension) { - throw new Error(`VoyageEmbeddings only supports ${SUPPORTED_DIM_MODELS.join(', ')}`) + if (!this.configuration) { + throw new Error('Pass in a configuration.') } + if (!this.configuration.modelName) this.configuration.modelName = 'voyage-3' - this.model = new _VoyageEmbeddings(this.configuration) + if (!VOYAGE_SUPPORTED_DIM_MODELS.includes(this.configuration.modelName) && this.configuration.outputDimension) { + console.error(`VoyageEmbeddings only supports ${VOYAGE_SUPPORTED_DIM_MODELS.join(', ')} to set outputDimension.`) + this.model = new _VoyageEmbeddings({ ...this.configuration, outputDimension: undefined }) + } else { + this.model = new _VoyageEmbeddings(this.configuration) + } } override async getDimensions(): Promise { - if (!this.configuration?.outputDimension) { - throw new Error('You need to pass in the optional dimensions parameter for this model') - } - return this.configuration?.outputDimension + return this.configuration?.outputDimension ?? (this.configuration?.modelName === 'voyage-code-2' ? 1536 : 1024) } override async embedDocuments(texts: string[]): Promise { diff --git a/src/main/knowledge/embeddings/utils.ts b/src/main/knowledge/embeddings/utils.ts new file mode 100644 index 0000000000..9b6bd54935 --- /dev/null +++ b/src/main/knowledge/embeddings/utils.ts @@ -0,0 +1,45 @@ +export const VOYAGE_SUPPORTED_DIM_MODELS = ['voyage-3-large', 'voyage-3.5', 'voyage-3.5-lite', 'voyage-code-3'] + +// NOTE: 下面的暂时没用上,但先留着吧 +export const OPENAI_SUPPORTED_DIM_MODELS = ['text-embedding-3-small', 'text-embedding-3-large'] + +export const DASHSCOPE_SUPPORTED_DIM_MODELS = ['text-embedding-v3', 'text-embedding-v4'] + +export const OPENSOURCE_SUPPORTED_DIM_MODELS = ['qwen3-embedding-0.6B', 'qwen3-embedding-4B', 'qwen3-embedding-8B'] + +export const GOOGLE_SUPPORTED_DIM_MODELS = ['gemini-embedding-exp-03-07', 'gemini-embedding-exp'] + +export const SUPPORTED_DIM_MODELS = [ + ...VOYAGE_SUPPORTED_DIM_MODELS, + ...OPENAI_SUPPORTED_DIM_MODELS, + ...DASHSCOPE_SUPPORTED_DIM_MODELS, + ...OPENSOURCE_SUPPORTED_DIM_MODELS, + ...GOOGLE_SUPPORTED_DIM_MODELS +] + +/** + * 从模型 ID 中提取基础名称。 + * 例如: + * - 'deepseek/deepseek-r1' => 'deepseek-r1' + * - 'deepseek-ai/deepseek/deepseek-r1' => 'deepseek-r1' + * @param {string} id 模型 ID + * @param {string} [delimiter='/'] 分隔符,默认为 '/' + * @returns {string} 基础名称 + */ +export const getBaseModelName = (id: string, delimiter: string = '/'): string => { + const parts = id.split(delimiter) + return parts[parts.length - 1] +} + +/** + * 从模型 ID 中提取基础名称并转换为小写。 + * 例如: + * - 'deepseek/DeepSeek-R1' => 'deepseek-r1' + * - 'deepseek-ai/deepseek/DeepSeek-R1' => 'deepseek-r1' + * @param {string} id 模型 ID + * @param {string} [delimiter='/'] 分隔符,默认为 '/' + * @returns {string} 小写的基础名称 + */ +export const getLowerBaseModelName = (id: string, delimiter: string = '/'): string => { + return getBaseModelName(id, delimiter).toLowerCase() +} diff --git a/src/main/loader/draftsExportLoader.ts b/src/main/knowledge/loader/draftsExportLoader.ts similarity index 100% rename from src/main/loader/draftsExportLoader.ts rename to src/main/knowledge/loader/draftsExportLoader.ts diff --git a/src/main/loader/epubLoader.ts b/src/main/knowledge/loader/epubLoader.ts similarity index 100% rename from src/main/loader/epubLoader.ts rename to src/main/knowledge/loader/epubLoader.ts diff --git a/src/main/loader/index.ts b/src/main/knowledge/loader/index.ts similarity index 91% rename from src/main/loader/index.ts rename to src/main/knowledge/loader/index.ts index ba66b33e3d..5fba26436e 100644 --- a/src/main/loader/index.ts +++ b/src/main/knowledge/loader/index.ts @@ -1,10 +1,9 @@ -import * as fs from 'node:fs' - import { JsonLoader, LocalPathLoader, RAGApplication, TextLoader } from '@cherrystudio/embedjs' import type { AddLoaderReturn } from '@cherrystudio/embedjs-interfaces' import { WebLoader } from '@cherrystudio/embedjs-loader-web' +import { readTextFileWithAutoEncoding } from '@main/utils/file' import { LoaderReturn } from '@shared/config/types' -import { FileType, KnowledgeBaseParams } from '@types' +import { FileMetadata, KnowledgeBaseParams } from '@types' import Logger from 'electron-log' import { DraftsExportLoader } from './draftsExportLoader' @@ -39,7 +38,7 @@ const FILE_LOADER_MAP: Record = { export async function addOdLoader( ragApplication: RAGApplication, - file: FileType, + file: FileMetadata, base: KnowledgeBaseParams, forceReload: boolean ): Promise { @@ -65,7 +64,7 @@ export async function addOdLoader( export async function addFileLoader( ragApplication: RAGApplication, - file: FileType, + file: FileMetadata, base: KnowledgeBaseParams, forceReload: boolean ): Promise { @@ -115,7 +114,7 @@ export async function addFileLoader( // HTML类型处理 loaderReturn = await ragApplication.addLoader( new WebLoader({ - urlOrContent: fs.readFileSync(file.path, 'utf-8'), + urlOrContent: await readTextFileWithAutoEncoding(file.path), chunkSize: base.chunkSize, chunkOverlap: base.chunkOverlap }) as any, @@ -125,7 +124,7 @@ export async function addFileLoader( case 'json': try { - jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf-8')) + jsonObject = JSON.parse(await readTextFileWithAutoEncoding(file.path)) } catch (error) { jsonParsed = false Logger.warn('[KnowledgeBase] failed parsing json file, falling back to text processing:', file.path, error) @@ -141,7 +140,7 @@ export async function addFileLoader( // 如果是其他文本类型且尚未读取文件,则读取文件 loaderReturn = await ragApplication.addLoader( new TextLoader({ - text: fs.readFileSync(file.path, 'utf-8'), + text: await readTextFileWithAutoEncoding(file.path), chunkSize: base.chunkSize, chunkOverlap: base.chunkOverlap }) as any, diff --git a/src/main/loader/noteLoader.ts b/src/main/knowledge/loader/noteLoader.ts similarity index 100% rename from src/main/loader/noteLoader.ts rename to src/main/knowledge/loader/noteLoader.ts diff --git a/src/main/loader/odLoader.ts b/src/main/knowledge/loader/odLoader.ts similarity index 100% rename from src/main/loader/odLoader.ts rename to src/main/knowledge/loader/odLoader.ts diff --git a/src/main/reranker/BaseReranker.ts b/src/main/knowledge/reranker/BaseReranker.ts similarity index 87% rename from src/main/reranker/BaseReranker.ts rename to src/main/knowledge/reranker/BaseReranker.ts index 83d241fe85..c3ac979d25 100644 --- a/src/main/reranker/BaseReranker.ts +++ b/src/main/knowledge/reranker/BaseReranker.ts @@ -5,7 +5,7 @@ export default abstract class BaseReranker { protected base: KnowledgeBaseParams constructor(base: KnowledgeBaseParams) { - if (!base.rerankModel) { + if (!base.rerankApiClient) { throw new Error('Rerank model is required') } this.base = base @@ -17,11 +17,11 @@ export default abstract class BaseReranker { * Get Rerank Request Url */ protected getRerankUrl() { - if (this.base.rerankModelProvider === 'bailian') { + if (this.base.rerankApiClient?.provider === 'bailian') { return 'https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank' } - let baseURL = this.base.rerankBaseURL + let baseURL = this.base.rerankApiClient?.baseURL if (baseURL && baseURL.endsWith('/')) { // `/` 结尾强制使用rerankBaseURL @@ -39,20 +39,20 @@ export default abstract class BaseReranker { * Get Rerank Request Body */ protected getRerankRequestBody(query: string, searchResults: ExtractChunkData[]) { - const provider = this.base.rerankModelProvider + const provider = this.base.rerankApiClient?.provider const documents = searchResults.map((doc) => doc.pageContent) const topN = this.base.documentCount if (provider === 'voyageai') { return { - model: this.base.rerankModel, + model: this.base.rerankApiClient?.model, query, documents, top_k: topN } } else if (provider === 'bailian') { return { - model: this.base.rerankModel, + model: this.base.rerankApiClient?.model, input: { query, documents @@ -69,7 +69,7 @@ export default abstract class BaseReranker { } } else { return { - model: this.base.rerankModel, + model: this.base.rerankApiClient?.model, query, documents, top_n: topN @@ -81,7 +81,7 @@ export default abstract class BaseReranker { * Extract Rerank Result */ protected extractRerankResult(data: any) { - const provider = this.base.rerankModelProvider + const provider = this.base.rerankApiClient?.provider if (provider === 'bailian') { return data.output.results } else if (provider === 'voyageai') { @@ -129,7 +129,7 @@ export default abstract class BaseReranker { public defaultHeaders() { return { - Authorization: `Bearer ${this.base.rerankApiKey}`, + Authorization: `Bearer ${this.base.rerankApiClient?.apiKey}`, 'Content-Type': 'application/json' } } diff --git a/src/main/reranker/GeneralReranker.ts b/src/main/knowledge/reranker/GeneralReranker.ts similarity index 84% rename from src/main/reranker/GeneralReranker.ts rename to src/main/knowledge/reranker/GeneralReranker.ts index 185e2132c7..1252ecad57 100644 --- a/src/main/reranker/GeneralReranker.ts +++ b/src/main/knowledge/reranker/GeneralReranker.ts @@ -1,6 +1,6 @@ import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' -import AxiosProxy from '@main/services/AxiosProxy' import { KnowledgeBaseParams } from '@types' +import axios from 'axios' import BaseReranker from './BaseReranker' @@ -15,7 +15,7 @@ export default class GeneralReranker extends BaseReranker { const requestBody = this.getRerankRequestBody(query, searchResults) try { - const { data } = await AxiosProxy.axios.post(url, requestBody, { headers: this.defaultHeaders() }) + const { data } = await axios.post(url, requestBody, { headers: this.defaultHeaders() }) const rerankResults = this.extractRerankResult(data) return this.getRerankResult(searchResults, rerankResults) diff --git a/src/main/reranker/Reranker.ts b/src/main/knowledge/reranker/Reranker.ts similarity index 100% rename from src/main/reranker/Reranker.ts rename to src/main/knowledge/reranker/Reranker.ts diff --git a/src/main/services/AppService.ts b/src/main/services/AppService.ts new file mode 100644 index 0000000000..f7dc5a7657 --- /dev/null +++ b/src/main/services/AppService.ts @@ -0,0 +1,81 @@ +import { isDev, isLinux, isMac, isWin } from '@main/constant' +import { app } from 'electron' +import log from 'electron-log' +import fs from 'fs' +import os from 'os' +import path from 'path' + +export class AppService { + private static instance: AppService + + private constructor() { + // Private constructor to prevent direct instantiation + } + + public static getInstance(): AppService { + if (!AppService.instance) { + AppService.instance = new AppService() + } + return AppService.instance + } + + public async setAppLaunchOnBoot(isLaunchOnBoot: boolean): Promise { + // Set login item settings for windows and mac + // linux is not supported because it requires more file operations + if (isWin || isMac) { + app.setLoginItemSettings({ openAtLogin: isLaunchOnBoot }) + } else if (isLinux) { + try { + const autostartDir = path.join(os.homedir(), '.config', 'autostart') + const desktopFile = path.join(autostartDir, isDev ? 'cherry-studio-dev.desktop' : 'cherry-studio.desktop') + + if (isLaunchOnBoot) { + // Ensure autostart directory exists + try { + await fs.promises.access(autostartDir) + } catch { + await fs.promises.mkdir(autostartDir, { recursive: true }) + } + + // Get executable path + let executablePath = app.getPath('exe') + if (process.env.APPIMAGE) { + // For AppImage packaged apps, use APPIMAGE environment variable + executablePath = process.env.APPIMAGE + } + + // Create desktop file content + const desktopContent = `[Desktop Entry] + Type=Application + Name=Cherry Studio + Comment=A powerful AI assistant for producer. + Exec=${executablePath} + Icon=cherrystudio + Terminal=false + StartupNotify=false + Categories=Development;Utility; + X-GNOME-Autostart-enabled=true + Hidden=false` + + // Write desktop file + await fs.promises.writeFile(desktopFile, desktopContent) + log.info('Created autostart desktop file for Linux') + } else { + // Remove desktop file + try { + await fs.promises.access(desktopFile) + await fs.promises.unlink(desktopFile) + log.info('Removed autostart desktop file for Linux') + } catch { + // File doesn't exist, no need to remove + } + } + } catch (error) { + log.error('Failed to set launch on boot for Linux:', error) + } + } + } +} + +// Default export as singleton instance +export default AppService.getInstance() diff --git a/src/main/services/AppUpdater.ts b/src/main/services/AppUpdater.ts index 82165fd715..effcff00c5 100644 --- a/src/main/services/AppUpdater.ts +++ b/src/main/services/AppUpdater.ts @@ -1,5 +1,6 @@ import { isWin } from '@main/constant' import { locales } from '@main/utils/locales' +import { generateUserAgent } from '@main/utils/systemInfo' import { FeedUrl, UpgradeChannel } from '@shared/config/constant' import { IpcChannel } from '@shared/IpcChannel' import { CancellationToken, UpdateInfo } from 'builder-util-runtime' @@ -24,6 +25,10 @@ export default class AppUpdater { autoUpdater.forceDevUpdateConfig = !app.isPackaged autoUpdater.autoDownload = configManager.getAutoUpdate() autoUpdater.autoInstallOnAppQuit = configManager.getAutoUpdate() + autoUpdater.requestHeaders = { + ...autoUpdater.requestHeaders, + 'User-Agent': generateUserAgent() + } autoUpdater.on('error', (error) => { // 简单记录错误信息和时间戳 diff --git a/src/main/services/AxiosProxy.ts b/src/main/services/AxiosProxy.ts deleted file mode 100644 index 6f767bd3a2..0000000000 --- a/src/main/services/AxiosProxy.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { AxiosInstance, default as axios_ } from 'axios' -import { ProxyAgent } from 'proxy-agent' - -import { proxyManager } from './ProxyManager' - -class AxiosProxy { - private cacheAxios: AxiosInstance | null = null - private proxyAgent: ProxyAgent | null = null - - get axios(): AxiosInstance { - const currentProxyAgent = proxyManager.getProxyAgent() - - // 如果代理发生变化或尚未初始化,则重新创建 axios 实例 - if (this.cacheAxios === null || (currentProxyAgent !== null && this.proxyAgent !== currentProxyAgent)) { - this.proxyAgent = currentProxyAgent - - // 创建带有代理配置的 axios 实例 - this.cacheAxios = axios_.create({ - proxy: false, - httpAgent: currentProxyAgent || undefined, - httpsAgent: currentProxyAgent || undefined - }) - } - - return this.cacheAxios - } -} - -export default new AxiosProxy() diff --git a/src/main/services/BackupManager.ts b/src/main/services/BackupManager.ts index 6e0c813e6d..2b607cb51a 100644 --- a/src/main/services/BackupManager.ts +++ b/src/main/services/BackupManager.ts @@ -11,7 +11,7 @@ import * as path from 'path' import { CreateDirectoryOptions, FileStat } from 'webdav' import { getDataPath } from '../utils' -import S3Storage from './RemoteStorage' +import S3Storage from './S3Storage' import WebDav from './WebDav' import { windowService } from './WindowService' @@ -27,6 +27,11 @@ class BackupManager { this.restoreFromWebdav = this.restoreFromWebdav.bind(this) this.listWebdavFiles = this.listWebdavFiles.bind(this) this.deleteWebdavFile = this.deleteWebdavFile.bind(this) + this.listLocalBackupFiles = this.listLocalBackupFiles.bind(this) + this.deleteLocalBackupFile = this.deleteLocalBackupFile.bind(this) + this.backupToLocalDir = this.backupToLocalDir.bind(this) + this.restoreFromLocalBackup = this.restoreFromLocalBackup.bind(this) + this.setLocalBackupDir = this.setLocalBackupDir.bind(this) this.backupToS3 = this.backupToS3.bind(this) this.restoreFromS3 = this.restoreFromS3.bind(this) this.listS3Files = this.listS3Files.bind(this) @@ -316,14 +321,22 @@ class BackupManager { async backupToWebdav(_: Electron.IpcMainInvokeEvent, data: string, webdavConfig: WebDavConfig) { const filename = webdavConfig.fileName || 'cherry-studio.backup.zip' const backupedFilePath = await this.backup(_, filename, data, undefined, webdavConfig.skipBackupFile) - const contentLength = (await fs.stat(backupedFilePath)).size const webdavClient = new WebDav(webdavConfig) try { - const result = await webdavClient.putFileContents(filename, fs.createReadStream(backupedFilePath), { - overwrite: true, - contentLength - }) - // 上传成功后删除本地备份文件 + let result + if (webdavConfig.disableStream) { + const fileContent = await fs.readFile(backupedFilePath) + result = await webdavClient.putFileContents(filename, fileContent, { + overwrite: true + }) + } else { + const contentLength = (await fs.stat(backupedFilePath)).size + result = await webdavClient.putFileContents(filename, fs.createReadStream(backupedFilePath), { + overwrite: true, + contentLength + }) + } + await fs.remove(backupedFilePath) return result } catch (error) { @@ -477,8 +490,29 @@ class BackupManager { } } + async backupToLocalDir( + _: Electron.IpcMainInvokeEvent, + data: string, + fileName: string, + localConfig: { + localBackupDir: string + skipBackupFile: boolean + } + ) { + try { + const backupDir = localConfig.localBackupDir + // Create backup directory if it doesn't exist + await fs.ensureDir(backupDir) + + const backupedFilePath = await this.backup(_, fileName, data, backupDir, localConfig.skipBackupFile) + return backupedFilePath + } catch (error) { + Logger.error('[BackupManager] Local backup failed:', error) + throw error + } + } + async backupToS3(_: Electron.IpcMainInvokeEvent, data: string, s3Config: S3Config) { - // 获取设备名 const os = require('os') const deviceName = os.hostname ? os.hostname() : 'device' const timestamp = new Date() @@ -487,18 +521,10 @@ class BackupManager { .slice(0, 14) const filename = s3Config.fileName || `cherry-studio.backup.${deviceName}.${timestamp}.zip` - // 不记录详细日志,只记录开始和结束 Logger.log(`[BackupManager] Starting S3 backup to ${filename}`) const backupedFilePath = await this.backup(_, filename, data, undefined, s3Config.skipBackupFile) - const s3Client = new S3Storage('s3', { - endpoint: s3Config.endpoint, - region: s3Config.region, - bucket: s3Config.bucket, - access_key_id: s3Config.access_key_id, - secret_access_key: s3Config.secret_access_key, - root: s3Config.root || '' - }) + const s3Client = new S3Storage(s3Config) try { const fileBuffer = await fs.promises.readFile(backupedFilePath) const result = await s3Client.putFileContents(filename, fileBuffer) @@ -513,20 +539,81 @@ class BackupManager { } } + async restoreFromLocalBackup(_: Electron.IpcMainInvokeEvent, fileName: string, localBackupDir: string) { + try { + const backupDir = localBackupDir + const backupPath = path.join(backupDir, fileName) + + if (!fs.existsSync(backupPath)) { + throw new Error(`Backup file not found: ${backupPath}`) + } + + return await this.restore(_, backupPath) + } catch (error) { + Logger.error('[BackupManager] Local restore failed:', error) + throw error + } + } + + async listLocalBackupFiles(_: Electron.IpcMainInvokeEvent, localBackupDir: string) { + try { + const files = await fs.readdir(localBackupDir) + const result: Array<{ fileName: string; modifiedTime: string; size: number }> = [] + + for (const file of files) { + const filePath = path.join(localBackupDir, file) + const stat = await fs.stat(filePath) + + if (stat.isFile() && file.endsWith('.zip')) { + result.push({ + fileName: file, + modifiedTime: stat.mtime.toISOString(), + size: stat.size + }) + } + } + + // Sort by modified time, newest first + return result.sort((a, b) => new Date(b.modifiedTime).getTime() - new Date(a.modifiedTime).getTime()) + } catch (error) { + Logger.error('[BackupManager] List local backup files failed:', error) + throw error + } + } + + async deleteLocalBackupFile(_: Electron.IpcMainInvokeEvent, fileName: string, localBackupDir: string) { + try { + const filePath = path.join(localBackupDir, fileName) + + if (!fs.existsSync(filePath)) { + throw new Error(`Backup file not found: ${filePath}`) + } + + await fs.remove(filePath) + return true + } catch (error) { + Logger.error('[BackupManager] Delete local backup file failed:', error) + throw error + } + } + + async setLocalBackupDir(_: Electron.IpcMainInvokeEvent, dirPath: string) { + try { + // Check if directory exists + await fs.ensureDir(dirPath) + return true + } catch (error) { + Logger.error('[BackupManager] Set local backup directory failed:', error) + throw error + } + } + async restoreFromS3(_: Electron.IpcMainInvokeEvent, s3Config: S3Config) { const filename = s3Config.fileName || 'cherry-studio.backup.zip' - // 只记录开始和结束或错误 Logger.log(`[BackupManager] Starting restore from S3: ${filename}`) - const s3Client = new S3Storage('s3', { - endpoint: s3Config.endpoint, - region: s3Config.region, - bucket: s3Config.bucket, - access_key_id: s3Config.access_key_id, - secret_access_key: s3Config.secret_access_key, - root: s3Config.root || '' - }) + const s3Client = new S3Storage(s3Config) try { const retrievedFile = await s3Client.getFileContents(filename) const backupedFilePath = path.join(this.backupDir, filename) @@ -551,31 +638,21 @@ class BackupManager { listS3Files = async (_: Electron.IpcMainInvokeEvent, s3Config: S3Config) => { try { - const s3Client = new S3Storage('s3', { - endpoint: s3Config.endpoint, - region: s3Config.region, - bucket: s3Config.bucket, - access_key_id: s3Config.access_key_id, - secret_access_key: s3Config.secret_access_key, - root: s3Config.root || '' - }) - const entries = await s3Client.instance?.list('/') - const files: Array<{ fileName: string; modifiedTime: string; size: number }> = [] - if (entries) { - for await (const entry of entries) { - const path = entry.path() - if (path.endsWith('.zip')) { - const meta = await s3Client.instance!.stat(path) - if (meta.isFile()) { - files.push({ - fileName: path.replace(/^\/+/, ''), - modifiedTime: meta.lastModified || '', - size: Number(meta.contentLength || 0n) - }) - } + const s3Client = new S3Storage(s3Config) + + const objects = await s3Client.listFiles() + const files = objects + .filter((obj) => obj.key.endsWith('.zip')) + .map((obj) => { + const segments = obj.key.split('/') + const fileName = segments[segments.length - 1] + return { + fileName, + modifiedTime: obj.lastModified || '', + size: obj.size } - } - } + }) + return files.sort((a, b) => new Date(b.modifiedTime).getTime() - new Date(a.modifiedTime).getTime()) } catch (error: any) { Logger.error('Failed to list S3 files:', error) @@ -585,14 +662,7 @@ class BackupManager { async deleteS3File(_: Electron.IpcMainInvokeEvent, fileName: string, s3Config: S3Config) { try { - const s3Client = new S3Storage('s3', { - endpoint: s3Config.endpoint, - region: s3Config.region, - bucket: s3Config.bucket, - access_key_id: s3Config.access_key_id, - secret_access_key: s3Config.secret_access_key, - root: s3Config.root || '' - }) + const s3Client = new S3Storage(s3Config) return await s3Client.deleteFile(fileName) } catch (error: any) { Logger.error('Failed to delete S3 file:', error) @@ -601,14 +671,7 @@ class BackupManager { } async checkS3Connection(_: Electron.IpcMainInvokeEvent, s3Config: S3Config) { - const s3Client = new S3Storage('s3', { - endpoint: s3Config.endpoint, - region: s3Config.region, - bucket: s3Config.bucket, - access_key_id: s3Config.access_key_id, - secret_access_key: s3Config.secret_access_key, - root: s3Config.root || '' - }) + const s3Client = new S3Storage(s3Config) return await s3Client.checkConnection() } } diff --git a/src/main/services/ConfigManager.ts b/src/main/services/ConfigManager.ts index 8e4b5d2bf1..cf3185ad70 100644 --- a/src/main/services/ConfigManager.ts +++ b/src/main/services/ConfigManager.ts @@ -24,7 +24,9 @@ export enum ConfigKeys { SelectionAssistantFollowToolbar = 'selectionAssistantFollowToolbar', SelectionAssistantRemeberWinSize = 'selectionAssistantRemeberWinSize', SelectionAssistantFilterMode = 'selectionAssistantFilterMode', - SelectionAssistantFilterList = 'selectionAssistantFilterList' + SelectionAssistantFilterList = 'selectionAssistantFilterList', + DisableHardwareAcceleration = 'disableHardwareAcceleration', + Proxy = 'proxy' } export class ConfigManager { @@ -218,6 +220,14 @@ export class ConfigManager { this.setAndNotify(ConfigKeys.SelectionAssistantFilterList, value) } + getDisableHardwareAcceleration(): boolean { + return this.get(ConfigKeys.DisableHardwareAcceleration, false) + } + + setDisableHardwareAcceleration(value: boolean) { + this.set(ConfigKeys.DisableHardwareAcceleration, value) + } + setAndNotify(key: string, value: unknown) { this.set(key, value, true) } diff --git a/src/main/services/CopilotService.ts b/src/main/services/CopilotService.ts index 0be9ee8a5e..bc331a4468 100644 --- a/src/main/services/CopilotService.ts +++ b/src/main/services/CopilotService.ts @@ -1,11 +1,10 @@ import { AxiosRequestConfig } from 'axios' +import axios from 'axios' import { app, safeStorage } from 'electron' import Logger from 'electron-log' import fs from 'fs/promises' import path from 'path' -import aoxisProxy from './AxiosProxy' - // 配置常量,集中管理 const CONFIG = { GITHUB_CLIENT_ID: 'Iv1.b507a08c87ecfe98', @@ -96,7 +95,7 @@ class CopilotService { } } - const response = await aoxisProxy.axios.get(CONFIG.API_URLS.GITHUB_USER, config) + const response = await axios.get(CONFIG.API_URLS.GITHUB_USER, config) return { login: response.data.login, avatar: response.data.avatar_url @@ -117,7 +116,7 @@ class CopilotService { try { this.updateHeaders(headers) - const response = await aoxisProxy.axios.post( + const response = await axios.post( CONFIG.API_URLS.GITHUB_DEVICE_CODE, { client_id: CONFIG.GITHUB_CLIENT_ID, @@ -149,7 +148,7 @@ class CopilotService { await this.delay(currentDelay) try { - const response = await aoxisProxy.axios.post( + const response = await axios.post( CONFIG.API_URLS.GITHUB_ACCESS_TOKEN, { client_id: CONFIG.GITHUB_CLIENT_ID, @@ -211,7 +210,7 @@ class CopilotService { } } - const response = await aoxisProxy.axios.get(CONFIG.API_URLS.COPILOT_TOKEN, config) + const response = await axios.get(CONFIG.API_URLS.COPILOT_TOKEN, config) return response.data } catch (error) { diff --git a/src/main/services/DxtService.ts b/src/main/services/DxtService.ts new file mode 100644 index 0000000000..f4324d25b4 --- /dev/null +++ b/src/main/services/DxtService.ts @@ -0,0 +1,396 @@ +import { getMcpDir, getTempDir } from '@main/utils/file' +import logger from 'electron-log' +import * as fs from 'fs' +import StreamZip from 'node-stream-zip' +import * as os from 'os' +import * as path from 'path' +import { v4 as uuidv4 } from 'uuid' + +// Type definitions +export interface DxtManifest { + dxt_version: string + name: string + display_name?: string + version: string + description?: string + long_description?: string + author?: { + name?: string + email?: string + url?: string + } + repository?: { + type?: string + url?: string + } + homepage?: string + documentation?: string + support?: string + icon?: string + server: { + type: string + entry_point: string + mcp_config: { + command: string + args: string[] + env?: Record + platform_overrides?: { + [platform: string]: { + command?: string + args?: string[] + env?: Record + } + } + } + } + tools?: Array<{ + name: string + description: string + }> + keywords?: string[] + license?: string + user_config?: Record + compatibility?: { + claude_desktop?: string + platforms?: string[] + runtimes?: Record + } +} + +export interface DxtUploadResult { + success: boolean + data?: { + manifest: DxtManifest + extractDir: string + } + error?: string +} + +export function performVariableSubstitution( + value: string, + extractDir: string, + userConfig?: Record +): string { + let result = value + + // Replace ${__dirname} with the extraction directory + result = result.replace(/\$\{__dirname\}/g, extractDir) + + // Replace ${HOME} with user's home directory + result = result.replace(/\$\{HOME\}/g, os.homedir()) + + // Replace ${DESKTOP} with user's desktop directory + const desktopDir = path.join(os.homedir(), 'Desktop') + result = result.replace(/\$\{DESKTOP\}/g, desktopDir) + + // Replace ${DOCUMENTS} with user's documents directory + const documentsDir = path.join(os.homedir(), 'Documents') + result = result.replace(/\$\{DOCUMENTS\}/g, documentsDir) + + // Replace ${DOWNLOADS} with user's downloads directory + const downloadsDir = path.join(os.homedir(), 'Downloads') + result = result.replace(/\$\{DOWNLOADS\}/g, downloadsDir) + + // Replace ${pathSeparator} or ${/} with the platform-specific path separator + result = result.replace(/\$\{pathSeparator\}/g, path.sep) + result = result.replace(/\$\{\/\}/g, path.sep) + + // Replace ${user_config.KEY} with user-configured values + if (userConfig) { + result = result.replace(/\$\{user_config\.([^}]+)\}/g, (match, key) => { + return userConfig[key] || match // Keep original if not found + }) + } + + return result +} + +export function applyPlatformOverrides(mcpConfig: any, extractDir: string, userConfig?: Record): any { + const platform = process.platform + const resolvedConfig = { ...mcpConfig } + + // Apply platform-specific overrides + if (mcpConfig.platform_overrides && mcpConfig.platform_overrides[platform]) { + const override = mcpConfig.platform_overrides[platform] + + // Override command if specified + if (override.command) { + resolvedConfig.command = override.command + } + + // Override args if specified + if (override.args) { + resolvedConfig.args = override.args + } + + // Merge environment variables + if (override.env) { + resolvedConfig.env = { ...resolvedConfig.env, ...override.env } + } + } + + // Apply variable substitution to all string values + if (resolvedConfig.command) { + resolvedConfig.command = performVariableSubstitution(resolvedConfig.command, extractDir, userConfig) + } + + if (resolvedConfig.args) { + resolvedConfig.args = resolvedConfig.args.map((arg: string) => + performVariableSubstitution(arg, extractDir, userConfig) + ) + } + + if (resolvedConfig.env) { + for (const [key, value] of Object.entries(resolvedConfig.env)) { + resolvedConfig.env[key] = performVariableSubstitution(value as string, extractDir, userConfig) + } + } + + return resolvedConfig +} + +export interface ResolvedMcpConfig { + command: string + args: string[] + env?: Record +} + +class DxtService { + private tempDir = path.join(getTempDir(), 'dxt_uploads') + private mcpDir = getMcpDir() + + constructor() { + this.ensureDirectories() + } + + private ensureDirectories() { + try { + // Create temp directory + if (!fs.existsSync(this.tempDir)) { + fs.mkdirSync(this.tempDir, { recursive: true }) + } + // Create MCP directory + if (!fs.existsSync(this.mcpDir)) { + fs.mkdirSync(this.mcpDir, { recursive: true }) + } + } catch (error) { + logger.error('[DxtService] Failed to create directories:', error) + } + } + + private async moveDirectory(source: string, destination: string): Promise { + try { + // Try rename first (works if on same filesystem) + fs.renameSync(source, destination) + } catch (error) { + // If rename fails (cross-filesystem), use copy + remove + logger.info('[DxtService] Cross-filesystem move detected, using copy + remove') + + // Ensure parent directory exists + const parentDir = path.dirname(destination) + if (!fs.existsSync(parentDir)) { + fs.mkdirSync(parentDir, { recursive: true }) + } + + // Recursively copy directory + await this.copyDirectory(source, destination) + + // Remove source directory + fs.rmSync(source, { recursive: true, force: true }) + } + } + + private async copyDirectory(source: string, destination: string): Promise { + // Create destination directory + fs.mkdirSync(destination, { recursive: true }) + + // Read source directory + const entries = fs.readdirSync(source, { withFileTypes: true }) + + // Copy each entry + for (const entry of entries) { + const sourcePath = path.join(source, entry.name) + const destPath = path.join(destination, entry.name) + + if (entry.isDirectory()) { + await this.copyDirectory(sourcePath, destPath) + } else { + fs.copyFileSync(sourcePath, destPath) + } + } + } + + public async uploadDxt(_: Electron.IpcMainInvokeEvent, filePath: string): Promise { + const tempExtractDir = path.join(this.tempDir, `dxt_${uuidv4()}`) + + try { + // Validate file exists + if (!fs.existsSync(filePath)) { + throw new Error('DXT file not found') + } + + // Extract the DXT file (which is a ZIP archive) to a temporary directory + logger.info('[DxtService] Extracting DXT file:', filePath) + + const zip = new StreamZip.async({ file: filePath }) + await zip.extract(null, tempExtractDir) + await zip.close() + + // Read and validate the manifest.json + const manifestPath = path.join(tempExtractDir, 'manifest.json') + if (!fs.existsSync(manifestPath)) { + throw new Error('manifest.json not found in DXT file') + } + + const manifestContent = fs.readFileSync(manifestPath, 'utf-8') + const manifest: DxtManifest = JSON.parse(manifestContent) + + // Validate required fields in manifest + if (!manifest.dxt_version) { + throw new Error('Invalid manifest: missing dxt_version') + } + if (!manifest.name) { + throw new Error('Invalid manifest: missing name') + } + if (!manifest.version) { + throw new Error('Invalid manifest: missing version') + } + if (!manifest.server) { + throw new Error('Invalid manifest: missing server configuration') + } + if (!manifest.server.mcp_config) { + throw new Error('Invalid manifest: missing server.mcp_config') + } + if (!manifest.server.mcp_config.command) { + throw new Error('Invalid manifest: missing server.mcp_config.command') + } + if (!Array.isArray(manifest.server.mcp_config.args)) { + throw new Error('Invalid manifest: server.mcp_config.args must be an array') + } + + // Use server name as the final extract directory for automatic version management + // Sanitize the name to prevent creating subdirectories + const sanitizedName = manifest.name.replace(/\//g, '-') + const serverDirName = `server-${sanitizedName}` + const finalExtractDir = path.join(this.mcpDir, serverDirName) + + // Clean up any existing version of this server + if (fs.existsSync(finalExtractDir)) { + logger.info('[DxtService] Removing existing server directory:', finalExtractDir) + fs.rmSync(finalExtractDir, { recursive: true, force: true }) + } + + // Move the temporary directory to the final location + // Use recursive copy + remove instead of rename to handle cross-filesystem moves + await this.moveDirectory(tempExtractDir, finalExtractDir) + logger.info('[DxtService] DXT server extracted to:', finalExtractDir) + + // Clean up the uploaded DXT file if it's in temp directory + if (filePath.startsWith(this.tempDir)) { + fs.unlinkSync(filePath) + } + + // Return success with manifest and extraction path + return { + success: true, + data: { + manifest, + extractDir: finalExtractDir + } + } + } catch (error) { + // Clean up on error + if (fs.existsSync(tempExtractDir)) { + fs.rmSync(tempExtractDir, { recursive: true, force: true }) + } + + const errorMessage = error instanceof Error ? error.message : 'Failed to process DXT file' + logger.error('[DxtService] DXT upload error:', error) + + return { + success: false, + error: errorMessage + } + } + } + + /** + * Get resolved MCP configuration for a DXT server with platform overrides and variable substitution + */ + public getResolvedMcpConfig(dxtPath: string, userConfig?: Record): ResolvedMcpConfig | null { + try { + // Read the manifest from the DXT server directory + const manifestPath = path.join(dxtPath, 'manifest.json') + if (!fs.existsSync(manifestPath)) { + logger.error('[DxtService] Manifest not found:', manifestPath) + return null + } + + const manifestContent = fs.readFileSync(manifestPath, 'utf-8') + const manifest: DxtManifest = JSON.parse(manifestContent) + + if (!manifest.server?.mcp_config) { + logger.error('[DxtService] No mcp_config found in manifest') + return null + } + + // Apply platform overrides and variable substitution + const resolvedConfig = applyPlatformOverrides(manifest.server.mcp_config, dxtPath, userConfig) + + logger.info('[DxtService] Resolved MCP config:', { + command: resolvedConfig.command, + args: resolvedConfig.args, + env: resolvedConfig.env ? Object.keys(resolvedConfig.env) : undefined + }) + + return resolvedConfig + } catch (error) { + logger.error('[DxtService] Failed to resolve MCP config:', error) + return null + } + } + + public cleanupDxtServer(serverName: string): boolean { + try { + // Handle server names that might contain slashes (e.g., "anthropic/sequential-thinking") + // by replacing slashes with the same separator used during installation + const sanitizedName = serverName.replace(/\//g, '-') + const serverDirName = `server-${sanitizedName}` + const serverDir = path.join(this.mcpDir, serverDirName) + + // First try the sanitized path + if (fs.existsSync(serverDir)) { + logger.info('[DxtService] Removing DXT server directory:', serverDir) + fs.rmSync(serverDir, { recursive: true, force: true }) + return true + } + + // Fallback: try with original name in case it was stored differently + const originalServerDir = path.join(this.mcpDir, `server-${serverName}`) + if (fs.existsSync(originalServerDir)) { + logger.info('[DxtService] Removing DXT server directory:', originalServerDir) + fs.rmSync(originalServerDir, { recursive: true, force: true }) + return true + } + + logger.warn('[DxtService] Server directory not found:', serverDir) + return false + } catch (error) { + logger.error('[DxtService] Failed to cleanup DXT server:', error) + return false + } + } + + public cleanup() { + try { + // Clean up temp directory + if (fs.existsSync(this.tempDir)) { + fs.rmSync(this.tempDir, { recursive: true, force: true }) + } + } catch (error) { + logger.error('[DxtService] Cleanup error:', error) + } + } +} + +export default DxtService diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index 0c81a454a7..1adecd5733 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -1,6 +1,6 @@ -import { getFilesDir, getFileType, getTempDir } from '@main/utils/file' +import { getFilesDir, getFileType, getTempDir, readTextFileWithAutoEncoding } from '@main/utils/file' import { documentExts, imageExts, MB } from '@shared/config/constant' -import { FileType } from '@types' +import { FileMetadata } from '@types' import * as crypto from 'crypto' import { dialog, @@ -53,8 +53,9 @@ class FileStorage { }) } - findDuplicateFile = async (filePath: string): Promise => { + findDuplicateFile = async (filePath: string): Promise => { const stats = fs.statSync(filePath) + console.log('stats', stats, filePath) const fileSize = stats.size const files = await fs.promises.readdir(this.storageDir) @@ -92,7 +93,7 @@ class FileStorage { public selectFile = async ( _: Electron.IpcMainInvokeEvent, options?: OpenDialogOptions - ): Promise => { + ): Promise => { const defaultOptions: OpenDialogOptions = { properties: ['openFile'] } @@ -151,7 +152,7 @@ class FileStorage { } } - public uploadFile = async (_: Electron.IpcMainInvokeEvent, file: FileType): Promise => { + public uploadFile = async (_: Electron.IpcMainInvokeEvent, file: FileMetadata): Promise => { const duplicateFile = await this.findDuplicateFile(file.path) if (duplicateFile) { @@ -175,7 +176,7 @@ class FileStorage { const stats = await fs.promises.stat(destPath) const fileType = getFileType(ext) - const fileMetadata: FileType = { + const fileMetadata: FileMetadata = { id: uuid, origin_name, name: uuid + ext, @@ -187,10 +188,12 @@ class FileStorage { count: 1 } + logger.info('[FileStorage] File uploaded:', fileMetadata) + return fileMetadata } - public getFile = async (_: Electron.IpcMainInvokeEvent, filePath: string): Promise => { + public getFile = async (_: Electron.IpcMainInvokeEvent, filePath: string): Promise => { if (!fs.existsSync(filePath)) { return null } @@ -199,7 +202,7 @@ class FileStorage { const ext = path.extname(filePath) const fileType = getFileType(ext) - const fileInfo: FileType = { + const fileInfo: FileMetadata = { id: uuidv4(), origin_name: path.basename(filePath), name: path.basename(filePath), @@ -215,10 +218,24 @@ class FileStorage { } public deleteFile = async (_: Electron.IpcMainInvokeEvent, id: string): Promise => { + if (!fs.existsSync(path.join(this.storageDir, id))) { + return + } await fs.promises.unlink(path.join(this.storageDir, id)) } - public readFile = async (_: Electron.IpcMainInvokeEvent, id: string): Promise => { + public deleteDir = async (_: Electron.IpcMainInvokeEvent, id: string): Promise => { + if (!fs.existsSync(path.join(this.storageDir, id))) { + return + } + await fs.promises.rm(path.join(this.storageDir, id), { recursive: true }) + } + + public readFile = async ( + _: Electron.IpcMainInvokeEvent, + id: string, + detectEncoding: boolean = false + ): Promise => { const filePath = path.join(this.storageDir, id) const fileExtension = path.extname(filePath) @@ -245,15 +262,24 @@ class FileStorage { } } - return fs.readFileSync(filePath, 'utf8') + try { + if (detectEncoding) { + return readTextFileWithAutoEncoding(filePath) + } else { + return fs.readFileSync(filePath, 'utf-8') + } + } catch (error) { + logger.error(error) + throw new Error(`Failed to read file: ${filePath}.`) + } } public createTempFile = async (_: Electron.IpcMainInvokeEvent, fileName: string): Promise => { if (!fs.existsSync(this.tempDir)) { fs.mkdirSync(this.tempDir, { recursive: true }) } - const tempFilePath = path.join(this.tempDir, `temp_file_${uuidv4()}_${fileName}`) - return tempFilePath + + return path.join(this.tempDir, `temp_file_${uuidv4()}_${fileName}`) } public writeFile = async ( @@ -280,7 +306,7 @@ class FileStorage { } } - public saveBase64Image = async (_: Electron.IpcMainInvokeEvent, base64Data: string): Promise => { + public saveBase64Image = async (_: Electron.IpcMainInvokeEvent, base64Data: string): Promise => { try { if (!base64Data) { throw new Error('Base64 data is required') @@ -306,7 +332,7 @@ class FileStorage { await fs.promises.writeFile(destPath, buffer) - const fileMetadata: FileType = { + const fileMetadata: FileMetadata = { id: uuid, origin_name: uuid + ext, name: uuid + ext, @@ -398,6 +424,19 @@ class FileStorage { shell.openPath(path).catch((err) => logger.error('[IPC - Error] Failed to open file:', err)) } + /** + * 通过相对路径打开文件,跨设备时使用 + * @param file + */ + public openFileWithRelativePath = async (_: Electron.IpcMainInvokeEvent, file: FileMetadata): Promise => { + const filePath = path.join(this.storageDir, file.name) + if (fs.existsSync(filePath)) { + shell.openPath(filePath).catch((err) => logger.error('[IPC - Error] Failed to open file:', err)) + } else { + logger.warn('[IPC - Warning] File does not exist:', filePath) + } + } + public save = async ( _: Electron.IpcMainInvokeEvent, fileName: string, @@ -465,7 +504,7 @@ class FileStorage { _: Electron.IpcMainInvokeEvent, url: string, isUseContentType?: boolean - ): Promise => { + ): Promise => { try { const response = await fetch(url) if (!response.ok) { @@ -507,7 +546,7 @@ class FileStorage { const stats = await fs.promises.stat(destPath) const fileType = getFileType(ext) - const fileMetadata: FileType = { + const fileMetadata: FileMetadata = { id: uuid, origin_name: filename, name: uuid + ext, diff --git a/src/main/services/FileService.ts b/src/main/services/FileSystemService.ts similarity index 100% rename from src/main/services/FileService.ts rename to src/main/services/FileSystemService.ts diff --git a/src/main/services/KnowledgeService.ts b/src/main/services/KnowledgeService.ts index d2d381c598..736934cd1a 100644 --- a/src/main/services/KnowledgeService.ts +++ b/src/main/services/KnowledgeService.ts @@ -21,17 +21,19 @@ import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' import { LibSqlDb } from '@cherrystudio/embedjs-libsql' import { SitemapLoader } from '@cherrystudio/embedjs-loader-sitemap' import { WebLoader } from '@cherrystudio/embedjs-loader-web' -import Embeddings from '@main/embeddings/Embeddings' -import { addFileLoader } from '@main/loader' -import { NoteLoader } from '@main/loader/noteLoader' -import Reranker from '@main/reranker/Reranker' +import OcrProvider from '@main/knowledage/ocr/OcrProvider' +import PreprocessProvider from '@main/knowledage/preprocess/PreprocessProvider' +import Embeddings from '@main/knowledge/embeddings/Embeddings' +import { addFileLoader } from '@main/knowledge/loader' +import { NoteLoader } from '@main/knowledge/loader/noteLoader' +import Reranker from '@main/knowledge/reranker/Reranker' import { windowService } from '@main/services/WindowService' import { getDataPath } from '@main/utils' import { getAllFiles } from '@main/utils/file' import { MB } from '@shared/config/constant' import type { LoaderReturn } from '@shared/config/types' import { IpcChannel } from '@shared/IpcChannel' -import { FileType, KnowledgeBaseParams, KnowledgeItem } from '@types' +import { FileMetadata, KnowledgeBaseParams, KnowledgeItem } from '@types' import Logger from 'electron-log' import { v4 as uuidv4 } from 'uuid' @@ -39,12 +41,14 @@ export interface KnowledgeBaseAddItemOptions { base: KnowledgeBaseParams item: KnowledgeItem forceReload?: boolean + userId?: string } interface KnowledgeBaseAddItemOptionsNonNullableAttribute { base: KnowledgeBaseParams item: KnowledgeItem forceReload: boolean + userId: string } interface EvaluateTaskWorkload { @@ -96,7 +100,13 @@ class KnowledgeService { private knowledgeItemProcessingQueueMappingPromise: Map void> = new Map() private static MAXIMUM_WORKLOAD = 80 * MB private static MAXIMUM_PROCESSING_ITEM_COUNT = 30 - private static ERROR_LOADER_RETURN: LoaderReturn = { entriesAdded: 0, uniqueId: '', uniqueIds: [''], loaderType: '' } + private static ERROR_LOADER_RETURN: LoaderReturn = { + entriesAdded: 0, + uniqueId: '', + uniqueIds: [''], + loaderType: '', + status: 'failed' + } constructor() { this.initStorageDir() @@ -110,27 +120,21 @@ class KnowledgeService { private getRagApplication = async ({ id, - model, - provider, - apiKey, - apiVersion, - baseURL, - dimensions + embedApiClient, + dimensions, + documentCount }: KnowledgeBaseParams): Promise => { let ragApplication: RAGApplication const embeddings = new Embeddings({ - model, - provider, - apiKey, - apiVersion, - baseURL, + embedApiClient, dimensions - } as KnowledgeBaseParams) + }) try { ragApplication = await new RAGApplicationBuilder() .setModel('NO_MODEL') .setEmbeddingModel(embeddings) .setVectorDatabase(new LibSqlDb({ path: path.join(this.storageDir, id) })) + .setSearchResultCount(documentCount || 30) .build() } catch (e) { Logger.error(e) @@ -150,6 +154,7 @@ class KnowledgeService { } public delete = async (_: Electron.IpcMainInvokeEvent, id: string): Promise => { + console.log('id', id) const dbPath = path.join(this.storageDir, id) if (fs.existsSync(dbPath)) { fs.rmSync(dbPath, { recursive: true }) @@ -162,28 +167,49 @@ class KnowledgeService { this.workload >= KnowledgeService.MAXIMUM_WORKLOAD ) } - private fileTask( ragApplication: RAGApplication, options: KnowledgeBaseAddItemOptionsNonNullableAttribute ): LoaderTask { - const { base, item, forceReload } = options - const file = item.content as FileType + const { base, item, forceReload, userId } = options + const file = item.content as FileMetadata const loaderTask: LoaderTask = { loaderTasks: [ { state: LoaderTaskItemState.PENDING, - task: () => - addFileLoader(ragApplication, file, base, forceReload) - .then((result) => { - loaderTask.loaderDoneReturn = result - return result - }) - .catch((err) => { - Logger.error(err) - return KnowledgeService.ERROR_LOADER_RETURN - }), + task: async () => { + try { + // 添加预处理逻辑 + const fileToProcess: FileMetadata = await this.preprocessing(file, base, item, userId) + + // 使用处理后的文件进行加载 + return addFileLoader(ragApplication, fileToProcess, base, forceReload) + .then((result) => { + loaderTask.loaderDoneReturn = result + return result + }) + .catch((e) => { + Logger.error(`Error in addFileLoader for ${file.name}: ${e}`) + const errorResult: LoaderReturn = { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: e.message, + messageSource: 'embedding' + } + loaderTask.loaderDoneReturn = errorResult + return errorResult + }) + } catch (e: any) { + Logger.error(`Preprocessing failed for ${file.name}: ${e}`) + const errorResult: LoaderReturn = { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: e.message, + messageSource: 'preprocess' + } + loaderTask.loaderDoneReturn = errorResult + return errorResult + } + }, evaluateTaskWorkload: { workload: file.size } } ], @@ -192,7 +218,6 @@ class KnowledgeService { return loaderTask } - private directoryTask( ragApplication: RAGApplication, options: KnowledgeBaseAddItemOptionsNonNullableAttribute @@ -232,7 +257,11 @@ class KnowledgeService { }) .catch((err) => { Logger.error(err) - return KnowledgeService.ERROR_LOADER_RETURN + return { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: `Failed to add dir loader: ${err.message}`, + messageSource: 'embedding' + } }), evaluateTaskWorkload: { workload: file.size } }) @@ -278,7 +307,11 @@ class KnowledgeService { }) .catch((err) => { Logger.error(err) - return KnowledgeService.ERROR_LOADER_RETURN + return { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: `Failed to add url loader: ${err.message}`, + messageSource: 'embedding' + } }) }, evaluateTaskWorkload: { workload: 2 * MB } @@ -318,7 +351,11 @@ class KnowledgeService { }) .catch((err) => { Logger.error(err) - return KnowledgeService.ERROR_LOADER_RETURN + return { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: `Failed to add sitemap loader: ${err.message}`, + messageSource: 'embedding' + } }), evaluateTaskWorkload: { workload: 20 * MB } } @@ -364,7 +401,11 @@ class KnowledgeService { }) .catch((err) => { Logger.error(err) - return KnowledgeService.ERROR_LOADER_RETURN + return { + ...KnowledgeService.ERROR_LOADER_RETURN, + message: `Failed to add note loader: ${err.message}`, + messageSource: 'embedding' + } }) }, evaluateTaskWorkload: { workload: contentBytes.length } @@ -430,10 +471,10 @@ class KnowledgeService { }) } - public add = (_: Electron.IpcMainInvokeEvent, options: KnowledgeBaseAddItemOptions): Promise => { + public add = async (_: Electron.IpcMainInvokeEvent, options: KnowledgeBaseAddItemOptions): Promise => { return new Promise((resolve) => { - const { base, item, forceReload = false } = options - const optionsNonNullableAttribute = { base, item, forceReload } + const { base, item, forceReload = false, userId = '' } = options + const optionsNonNullableAttribute = { base, item, forceReload, userId } this.getRagApplication(base) .then((ragApplication) => { const task = (() => { @@ -459,12 +500,20 @@ class KnowledgeService { }) this.processingQueueHandle() } else { - resolve(KnowledgeService.ERROR_LOADER_RETURN) + resolve({ + ...KnowledgeService.ERROR_LOADER_RETURN, + message: 'Unsupported item type', + messageSource: 'embedding' + }) } }) .catch((err) => { Logger.error(err) - resolve(KnowledgeService.ERROR_LOADER_RETURN) + resolve({ + ...KnowledgeService.ERROR_LOADER_RETURN, + message: `Failed to add item: ${err.message}`, + messageSource: 'embedding' + }) }) }) } @@ -497,6 +546,69 @@ class KnowledgeService { } return await new Reranker(base).rerank(search, results) } + + public getStorageDir = (): string => { + return this.storageDir + } + + private preprocessing = async ( + file: FileMetadata, + base: KnowledgeBaseParams, + item: KnowledgeItem, + userId: string + ): Promise => { + let fileToProcess: FileMetadata = file + if (base.preprocessOrOcrProvider && file.ext.toLowerCase() === '.pdf') { + try { + let provider: PreprocessProvider | OcrProvider + if (base.preprocessOrOcrProvider.type === 'preprocess') { + provider = new PreprocessProvider(base.preprocessOrOcrProvider.provider, userId) + } else { + provider = new OcrProvider(base.preprocessOrOcrProvider.provider) + } + // 首先检查文件是否已经被预处理过 + const alreadyProcessed = await provider.checkIfAlreadyProcessed(file) + if (alreadyProcessed) { + Logger.info(`File already preprocess processed, using cached result: ${file.path}`) + return alreadyProcessed + } + + // 执行预处理 + Logger.info(`Starting preprocess processing for scanned PDF: ${file.path}`) + const { processedFile, quota } = await provider.parseFile(item.id, file) + fileToProcess = processedFile + const mainWindow = windowService.getMainWindow() + mainWindow?.webContents.send('file-preprocess-finished', { + itemId: item.id, + quota: quota + }) + } catch (err) { + Logger.error(`Preprocess processing failed: ${err}`) + // 如果预处理失败,使用原始文件 + // fileToProcess = file + throw new Error(`Preprocess processing failed: ${err}`) + } + } + + return fileToProcess + } + + public checkQuota = async ( + _: Electron.IpcMainInvokeEvent, + base: KnowledgeBaseParams, + userId: string + ): Promise => { + try { + if (base.preprocessOrOcrProvider && base.preprocessOrOcrProvider.type === 'preprocess') { + const provider = new PreprocessProvider(base.preprocessOrOcrProvider.provider, userId) + return await provider.checkQuota() + } + throw new Error('No preprocess provider configured') + } catch (err) { + Logger.error(`Failed to check quota: ${err}`) + throw new Error(`Failed to check quota: ${err}`) + } + } } export default new KnowledgeService() diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index 2515c91416..61959f1676 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -14,6 +14,16 @@ import { type StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp' import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory' +// Import notification schemas from MCP SDK +import { + CancelledNotificationSchema, + LoggingMessageNotificationSchema, + ProgressNotificationSchema, + PromptListChangedNotificationSchema, + ResourceListChangedNotificationSchema, + ResourceUpdatedNotificationSchema, + ToolListChangedNotificationSchema +} from '@modelcontextprotocol/sdk/types.js' import { nanoid } from '@reduxjs/toolkit' import { GetMCPPromptResponse, @@ -28,8 +38,10 @@ import { app } from 'electron' import Logger from 'electron-log' import { EventEmitter } from 'events' import { memoize } from 'lodash' +import { v4 as uuidv4 } from 'uuid' import { CacheService } from './CacheService' +import DxtService from './DxtService' import { CallBackServer } from './mcp/oauth/callback' import { McpOAuthClientProvider } from './mcp/oauth/provider' import getLoginShellEnvironment from './mcp/shell-env' @@ -71,6 +83,8 @@ function withCache( class McpService { private clients: Map = new Map() private pendingClients: Map> = new Map() + private dxtService = new DxtService() + private activeToolCalls: Map = new Map() constructor() { this.initClient = this.initClient.bind(this) @@ -84,7 +98,10 @@ class McpService { this.removeServer = this.removeServer.bind(this) this.restartServer = this.restartServer.bind(this) this.stopServer = this.stopServer.bind(this) + this.abortTool = this.abortTool.bind(this) this.cleanup = this.cleanup.bind(this) + this.checkMcpConnectivity = this.checkMcpConnectivity.bind(this) + this.getServerVersion = this.getServerVersion.bind(this) } private getServerKey(server: MCPServer): string { @@ -133,7 +150,7 @@ class McpService { // Create new client instance for each connection const client = new Client({ name: 'Cherry Studio', version: app.getVersion() }, { capabilities: {} }) - const args = [...(server.args || [])] + let args = [...(server.args || [])] // let transport: StdioClientTransport | SSEClientTransport | InMemoryTransport | StreamableHTTPClientTransport const authProvider = new McpOAuthClientProvider({ @@ -203,6 +220,23 @@ class McpService { } else if (server.command) { let cmd = server.command + // For DXT servers, use resolved configuration with platform overrides and variable substitution + if (server.dxtPath) { + const resolvedConfig = this.dxtService.getResolvedMcpConfig(server.dxtPath) + if (resolvedConfig) { + cmd = resolvedConfig.command + args = resolvedConfig.args + // Merge resolved environment variables with existing ones + server.env = { + ...server.env, + ...resolvedConfig.env + } + Logger.info(`[MCP] Using resolved DXT config - command: ${cmd}, args: ${args?.join(' ')}`) + } else { + Logger.warn(`[MCP] Failed to resolve DXT config for ${server.name}, falling back to manifest values`) + } + } + if (server.command === 'npx') { cmd = await getBinaryPath('bun') Logger.info(`[MCP] Using command: ${cmd}`) @@ -249,7 +283,7 @@ class McpService { this.removeProxyEnv(loginShellEnv) } - const stdioTransport = new StdioClientTransport({ + const transportOptions: any = { command: cmd, args, env: { @@ -257,7 +291,15 @@ class McpService { ...server.env }, stderr: 'pipe' - }) + } + + // For DXT servers, set the working directory to the extracted path + if (server.dxtPath) { + transportOptions.cwd = server.dxtPath + Logger.info(`[MCP] Setting working directory for DXT server: ${server.dxtPath}`) + } + + const stdioTransport = new StdioClientTransport(transportOptions) stdioTransport.stderr?.on('data', (data) => Logger.info(`[MCP] Stdio stderr for server: ${server.name} `, data.toString()) ) @@ -331,6 +373,12 @@ class McpService { // Store the new client in the cache this.clients.set(serverKey, client) + // Set up notification handlers + this.setupNotificationHandlers(client, server) + + // Clear existing cache to ensure fresh data + this.clearServerCache(serverKey) + Logger.info(`[MCP] Activated server: ${server.name}`) return client } catch (error: any) { @@ -349,6 +397,79 @@ class McpService { return initPromise } + /** + * Set up notification handlers for MCP client + */ + private setupNotificationHandlers(client: Client, server: MCPServer) { + const serverKey = this.getServerKey(server) + + try { + // Set up tools list changed notification handler + client.setNotificationHandler(ToolListChangedNotificationSchema, async () => { + Logger.info(`[MCP] Tools list changed for server: ${server.name}`) + // Clear tools cache + CacheService.remove(`mcp:list_tool:${serverKey}`) + }) + + // Set up resources list changed notification handler + client.setNotificationHandler(ResourceListChangedNotificationSchema, async () => { + Logger.info(`[MCP] Resources list changed for server: ${server.name}`) + // Clear resources cache + CacheService.remove(`mcp:list_resources:${serverKey}`) + }) + + // Set up prompts list changed notification handler + client.setNotificationHandler(PromptListChangedNotificationSchema, async () => { + Logger.info(`[MCP] Prompts list changed for server: ${server.name}`) + // Clear prompts cache + CacheService.remove(`mcp:list_prompts:${serverKey}`) + }) + + // Set up resource updated notification handler + client.setNotificationHandler(ResourceUpdatedNotificationSchema, async () => { + Logger.info(`[MCP] Resource updated for server: ${server.name}`) + // Clear resource-specific caches + this.clearResourceCaches(serverKey) + }) + + // Set up progress notification handler + client.setNotificationHandler(ProgressNotificationSchema, async (notification) => { + Logger.info(`[MCP] Progress notification received for server: ${server.name}`, notification.params) + }) + + // Set up cancelled notification handler + client.setNotificationHandler(CancelledNotificationSchema, async (notification) => { + Logger.info(`[MCP] Operation cancelled for server: ${server.name}`, notification.params) + }) + + // Set up logging message notification handler + client.setNotificationHandler(LoggingMessageNotificationSchema, async (notification) => { + Logger.info(`[MCP] Message from server ${server.name}:`, notification.params) + }) + + Logger.info(`[MCP] Set up notification handlers for server: ${server.name}`) + } catch (error) { + Logger.error(`[MCP] Failed to set up notification handlers for server ${server.name}:`, error) + } + } + + /** + * Clear resource-specific caches for a server + */ + private clearResourceCaches(serverKey: string) { + CacheService.remove(`mcp:list_resources:${serverKey}`) + } + + /** + * Clear all caches for a specific server + */ + private clearServerCache(serverKey: string) { + CacheService.remove(`mcp:list_tool:${serverKey}`) + CacheService.remove(`mcp:list_prompts:${serverKey}`) + CacheService.remove(`mcp:list_resources:${serverKey}`) + Logger.info(`[MCP] Cleared all caches for server: ${serverKey}`) + } + async closeClient(serverKey: string) { const client = this.clients.get(serverKey) if (client) { @@ -356,8 +477,8 @@ class McpService { await client.close() Logger.info(`[MCP] Closed server: ${serverKey}`) this.clients.delete(serverKey) - CacheService.remove(`mcp:list_tool:${serverKey}`) - Logger.info(`[MCP] Cleared cache for server: ${serverKey}`) + // Clear all caches for this server + this.clearServerCache(serverKey) } else { Logger.warn(`[MCP] No client found for server: ${serverKey}`) } @@ -375,12 +496,26 @@ class McpService { if (existingClient) { await this.closeClient(serverKey) } + + // If this is a DXT server, cleanup its directory + if (server.dxtPath) { + try { + const cleaned = this.dxtService.cleanupDxtServer(server.name) + if (cleaned) { + Logger.info(`[MCP] Cleaned up DXT server directory for: ${server.name}`) + } + } catch (error) { + Logger.error(`[MCP] Failed to cleanup DXT server: ${server.name}`, error) + } + } } async restartServer(_: Electron.IpcMainInvokeEvent, server: MCPServer) { Logger.info(`[MCP] Restarting server: ${server.name}`) const serverKey = this.getServerKey(server) await this.closeClient(serverKey) + // Clear cache before restarting to ensure fresh data + this.clearServerCache(serverKey) await this.initClient(server) } @@ -400,6 +535,12 @@ class McpService { public async checkMcpConnectivity(_: Electron.IpcMainInvokeEvent, server: MCPServer): Promise { Logger.info(`[MCP] Checking connectivity for server: ${server.name}`) try { + Logger.info(`[MCP] About to call initClient for server: ${server.name}`, { hasInitClient: !!this.initClient }) + + if (!this.initClient) { + throw new Error('initClient method is not available') + } + const client = await this.initClient(server) // Attempt to list tools as a way to check connectivity await client.listTools() @@ -455,10 +596,14 @@ class McpService { */ public async callTool( _: Electron.IpcMainInvokeEvent, - { server, name, args }: { server: MCPServer; name: string; args: any } + { server, name, args, callId }: { server: MCPServer; name: string; args: any; callId?: string } ): Promise { + const toolCallId = callId || uuidv4() + const abortController = new AbortController() + this.activeToolCalls.set(toolCallId, abortController) + try { - Logger.info('[MCP] Calling:', server.name, name, args) + Logger.info('[MCP] Calling:', server.name, name, args, 'callId:', toolCallId) if (typeof args === 'string') { try { args = JSON.parse(args) @@ -468,12 +613,19 @@ class McpService { } const client = await this.initClient(server) const result = await client.callTool({ name, arguments: args }, undefined, { - timeout: server.timeout ? server.timeout * 1000 : 60000 // Default timeout of 1 minute + onprogress: (process) => { + console.log('[MCP] Progress:', process.progress / (process.total || 1)) + window.api.mcp.setProgress(process.progress / (process.total || 1)) + }, + timeout: server.timeout ? server.timeout * 1000 : 60000, // Default timeout of 1 minute + signal: this.activeToolCalls.get(toolCallId)?.signal }) return result as MCPCallToolResponse } catch (error) { Logger.error(`[MCP] Error calling tool ${name} on ${server.name}:`, error) throw error + } finally { + this.activeToolCalls.delete(toolCallId) } } @@ -664,6 +816,45 @@ class McpService { delete env.http_proxy delete env.https_proxy } + + // 实现 abortTool 方法 + public async abortTool(_: Electron.IpcMainInvokeEvent, callId: string) { + const activeToolCall = this.activeToolCalls.get(callId) + if (activeToolCall) { + activeToolCall.abort() + this.activeToolCalls.delete(callId) + Logger.info(`[MCP] Aborted tool call: ${callId}`) + return true + } else { + Logger.warn(`[MCP] No active tool call found for callId: ${callId}`) + return false + } + } + + /** + * Get the server version information + */ + public async getServerVersion(_: Electron.IpcMainInvokeEvent, server: MCPServer): Promise { + try { + Logger.info(`[MCP] Getting server version for: ${server.name}`) + const client = await this.initClient(server) + + // Try to get server information which may include version + const serverInfo = client.getServerVersion() + Logger.info(`[MCP] Server info for ${server.name}:`, serverInfo) + + if (serverInfo && serverInfo.version) { + Logger.info(`[MCP] Server version for ${server.name}: ${serverInfo.version}`) + return serverInfo.version + } + + Logger.warn(`[MCP] No version information available for server: ${server.name}`) + return null + } catch (error: any) { + Logger.error(`[MCP] Failed to get server version for ${server.name}:`, error?.message) + return null + } + } } export default new McpService() diff --git a/src/main/services/MistralClientManager.ts b/src/main/services/MistralClientManager.ts new file mode 100644 index 0000000000..fa4aa53df8 --- /dev/null +++ b/src/main/services/MistralClientManager.ts @@ -0,0 +1,33 @@ +import { Mistral } from '@mistralai/mistralai' +import { Provider } from '@types' + +export class MistralClientManager { + private static instance: MistralClientManager + private client: Mistral | null = null + + // eslint-disable-next-line @typescript-eslint/no-empty-function + private constructor() {} + + public static getInstance(): MistralClientManager { + if (!MistralClientManager.instance) { + MistralClientManager.instance = new MistralClientManager() + } + return MistralClientManager.instance + } + + public initializeClient(provider: Provider): void { + if (!this.client) { + this.client = new Mistral({ + apiKey: provider.apiKey, + serverURL: provider.apiHost + }) + } + } + + public getClient(): Mistral { + if (!this.client) { + throw new Error('Mistral client not initialized. Call initializeClient first.') + } + return this.client + } +} diff --git a/src/main/services/NotificationService.ts b/src/main/services/NotificationService.ts index e06036b523..5ba0d82ce4 100644 --- a/src/main/services/NotificationService.ts +++ b/src/main/services/NotificationService.ts @@ -1,8 +1,6 @@ import { BrowserWindow, Notification as ElectronNotification } from 'electron' import { Notification } from 'src/renderer/src/types/notification' -import icon from '../../../build/icon.png?asset' - class NotificationService { private window: BrowserWindow @@ -15,8 +13,7 @@ class NotificationService { // 使用 Electron Notification API const electronNotification = new ElectronNotification({ title: notification.title, - body: notification.message, - icon: icon + body: notification.message }) electronNotification.on('click', () => { diff --git a/src/main/services/ProtocolClient.ts b/src/main/services/ProtocolClient.ts index 7e0b274816..cac0983fd6 100644 --- a/src/main/services/ProtocolClient.ts +++ b/src/main/services/ProtocolClient.ts @@ -19,7 +19,7 @@ export function registerProtocolClient(app: Electron.App) { } } - app.setAsDefaultProtocolClient('cherrystudio') + app.setAsDefaultProtocolClient(CHERRY_STUDIO_PROTOCOL) } export function handleProtocolUrl(url: string) { diff --git a/src/main/services/ProxyManager.ts b/src/main/services/ProxyManager.ts index 3a4aa09438..a2936e37dc 100644 --- a/src/main/services/ProxyManager.ts +++ b/src/main/services/ProxyManager.ts @@ -1,38 +1,54 @@ -import { ProxyConfig as _ProxyConfig, session } from 'electron' +import axios from 'axios' +import { app, ProxyConfig, session } from 'electron' +import Logger from 'electron-log' +import { socksDispatcher } from 'fetch-socks' +import http from 'http' +import https from 'https' import { getSystemProxy } from 'os-proxy-config' -import { ProxyAgent as GeneralProxyAgent } from 'proxy-agent' -// import { ProxyAgent, setGlobalDispatcher } from 'undici' - -type ProxyMode = 'system' | 'custom' | 'none' - -export interface ProxyConfig { - mode: ProxyMode - url?: string -} +import { ProxyAgent } from 'proxy-agent' +import { Dispatcher, EnvHttpProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from 'undici' export class ProxyManager { - private config: ProxyConfig - private proxyAgent: GeneralProxyAgent | null = null + private config: ProxyConfig = { mode: 'direct' } private systemProxyInterval: NodeJS.Timeout | null = null + private isSettingProxy = false + + private originalGlobalDispatcher: Dispatcher + private originalSocksDispatcher: Dispatcher + // for http and https + private originalHttpGet: typeof http.get + private originalHttpRequest: typeof http.request + private originalHttpsGet: typeof https.get + private originalHttpsRequest: typeof https.request constructor() { - this.config = { - mode: 'none' - } - } - - private async setSessionsProxy(config: _ProxyConfig): Promise { - const sessions = [session.defaultSession, session.fromPartition('persist:webview')] - await Promise.all(sessions.map((session) => session.setProxy(config))) + this.originalGlobalDispatcher = getGlobalDispatcher() + this.originalSocksDispatcher = global[Symbol.for('undici.globalDispatcher.1')] + this.originalHttpGet = http.get + this.originalHttpRequest = http.request + this.originalHttpsGet = https.get + this.originalHttpsRequest = https.request } private async monitorSystemProxy(): Promise { // Clear any existing interval first this.clearSystemProxyMonitor() // Set new interval - this.systemProxyInterval = setInterval(async () => { - await this.setSystemProxy() - }, 10000) + this.systemProxyInterval = setInterval( + async () => { + const currentProxy = await getSystemProxy() + if (currentProxy && currentProxy.proxyUrl.toLowerCase() === this.config.proxyRules) { + return + } + + await this.configureProxy({ + mode: 'system', + proxyRules: currentProxy?.proxyUrl.toLowerCase() + }) + }, + // 1 minutes + 1000 * 60 + ) } private clearSystemProxyMonitor(): void { @@ -43,99 +59,182 @@ export class ProxyManager { } async configureProxy(config: ProxyConfig): Promise { + Logger.info('configureProxy', config.mode, config.proxyRules) + if (this.isSettingProxy) { + return + } + + this.isSettingProxy = true + try { + if (config?.mode === this.config?.mode && config?.proxyRules === this.config?.proxyRules) { + Logger.info('proxy config is the same, skip configure') + return + } + this.config = config this.clearSystemProxyMonitor() - if (this.config.mode === 'system') { - await this.setSystemProxy() - this.monitorSystemProxy() - } else if (this.config.mode === 'custom') { - await this.setCustomProxy() - } else { - await this.clearProxy() + if (config.mode === 'system') { + const currentProxy = await getSystemProxy() + if (currentProxy) { + Logger.info('current system proxy', currentProxy.proxyUrl) + this.config.proxyRules = currentProxy.proxyUrl.toLowerCase() + this.monitorSystemProxy() + } else { + // no system proxy, use direct mode + this.config.mode = 'direct' + } } + + this.setGlobalProxy() } catch (error) { - console.error('Failed to config proxy:', error) + Logger.error('Failed to config proxy:', error) throw error + } finally { + this.isSettingProxy = false } } private setEnvironment(url: string): void { + if (url === '') { + delete process.env.HTTP_PROXY + delete process.env.HTTPS_PROXY + delete process.env.grpc_proxy + delete process.env.http_proxy + delete process.env.https_proxy + + delete process.env.SOCKS_PROXY + delete process.env.ALL_PROXY + return + } + process.env.grpc_proxy = url process.env.HTTP_PROXY = url process.env.HTTPS_PROXY = url process.env.http_proxy = url process.env.https_proxy = url - } - private async setSystemProxy(): Promise { - try { - const currentProxy = await getSystemProxy() - if (!currentProxy || currentProxy.proxyUrl === this.config.url) { - return - } - await this.setSessionsProxy({ mode: 'system' }) - this.config.url = currentProxy.proxyUrl.toLowerCase() - this.setEnvironment(this.config.url) - this.proxyAgent = new GeneralProxyAgent() - } catch (error) { - console.error('Failed to set system proxy:', error) - throw error + if (url.startsWith('socks')) { + process.env.SOCKS_PROXY = url + process.env.ALL_PROXY = url } } - private async setCustomProxy(): Promise { - try { - if (this.config.url) { - this.setEnvironment(this.config.url) - this.proxyAgent = new GeneralProxyAgent() - await this.setSessionsProxy({ proxyRules: this.config.url }) + private setGlobalProxy() { + this.setEnvironment(this.config.proxyRules || '') + this.setGlobalFetchProxy(this.config) + this.setSessionsProxy(this.config) + + this.setGlobalHttpProxy(this.config) + } + + private setGlobalHttpProxy(config: ProxyConfig) { + const proxyUrl = config.proxyRules + if (config.mode === 'direct' || !proxyUrl) { + http.get = this.originalHttpGet + http.request = this.originalHttpRequest + https.get = this.originalHttpsGet + https.request = this.originalHttpsRequest + + axios.defaults.proxy = undefined + axios.defaults.httpAgent = undefined + axios.defaults.httpsAgent = undefined + return + } + + // ProxyAgent 从环境变量读取代理配置 + const agent = new ProxyAgent() + + // axios 使用代理 + axios.defaults.proxy = false + axios.defaults.httpAgent = agent + axios.defaults.httpsAgent = agent + + http.get = this.bindHttpMethod(this.originalHttpGet, agent) + http.request = this.bindHttpMethod(this.originalHttpRequest, agent) + + https.get = this.bindHttpMethod(this.originalHttpsGet, agent) + https.request = this.bindHttpMethod(this.originalHttpsRequest, agent) + } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + private bindHttpMethod(originalMethod: Function, agent: http.Agent | https.Agent) { + return (...args: any[]) => { + let url: string | URL | undefined + let options: http.RequestOptions | https.RequestOptions + let callback: (res: http.IncomingMessage) => void + + if (typeof args[0] === 'string' || args[0] instanceof URL) { + url = args[0] + if (typeof args[1] === 'function') { + options = {} + callback = args[1] + } else { + options = { + ...args[1] + } + callback = args[2] + } + } else { + options = { + ...args[0] + } + callback = args[1] } - } catch (error) { - console.error('Failed to set custom proxy:', error) - throw error + + // for webdav https self-signed certificate + if (options.agent instanceof https.Agent) { + ;(agent as https.Agent).options.rejectUnauthorized = options.agent.options.rejectUnauthorized + } + + // 确保只设置 agent,不修改其他网络选项 + if (!options.agent) { + options.agent = agent + } + + if (url) { + return originalMethod(url, options, callback) + } + return originalMethod(options, callback) } } - private clearEnvironment(): void { - delete process.env.HTTP_PROXY - delete process.env.HTTPS_PROXY - delete process.env.grpc_proxy - delete process.env.http_proxy - delete process.env.https_proxy + private setGlobalFetchProxy(config: ProxyConfig) { + const proxyUrl = config.proxyRules + if (config.mode === 'direct' || !proxyUrl) { + setGlobalDispatcher(this.originalGlobalDispatcher) + global[Symbol.for('undici.globalDispatcher.1')] = this.originalSocksDispatcher + return + } + + const url = new URL(proxyUrl) + if (url.protocol === 'http:' || url.protocol === 'https:') { + setGlobalDispatcher(new EnvHttpProxyAgent()) + return + } + + global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({ + port: parseInt(url.port), + type: url.protocol === 'socks4:' ? 4 : 5, + host: url.hostname, + userId: url.username || undefined, + password: url.password || undefined + }) } - private async clearProxy(): Promise { - this.clearEnvironment() - await this.setSessionsProxy({ mode: 'direct' }) - this.config = { mode: 'none' } - this.proxyAgent = null - } + private async setSessionsProxy(config: ProxyConfig): Promise { + let c = config - getProxyAgent(): GeneralProxyAgent | null { - return this.proxyAgent - } + if (config.mode === 'direct' || !config.proxyRules) { + c = { mode: 'direct' } + } - getProxyUrl(): string { - return this.config.url || '' - } + const sessions = [session.defaultSession, session.fromPartition('persist:webview')] + await Promise.all(sessions.map((session) => session.setProxy(c))) - // setGlobalProxy() { - // const proxyUrl = this.config.url - // if (proxyUrl) { - // const [protocol, address] = proxyUrl.split('://') - // const [host, port] = address.split(':') - // if (!protocol.includes('socks')) { - // setGlobalDispatcher(new ProxyAgent(proxyUrl)) - // } else { - // global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({ - // port: parseInt(port), - // type: protocol === 'socks5' ? 5 : 4, - // host: host - // }) - // } - // } - // } + // set proxy for electron + app.setProxy(c) + } } export const proxyManager = new ProxyManager() diff --git a/src/main/services/RemoteStorage.ts b/src/main/services/RemoteStorage.ts deleted file mode 100644 index 4efc57b6c6..0000000000 --- a/src/main/services/RemoteStorage.ts +++ /dev/null @@ -1,83 +0,0 @@ -import Logger from 'electron-log' -import type { Operator as OperatorType } from 'opendal' -const { Operator } = require('opendal') - -export default class S3Storage { - public instance: OperatorType | undefined - - /** - * - * @param scheme is the scheme for opendal services. Available value includes "azblob", "azdls", "cos", "gcs", "obs", "oss", "s3", "webdav", "webhdfs", "aliyun-drive", "alluxio", "azfile", "dropbox", "gdrive", "onedrive", "postgresql", "mysql", "redis", "swift", "mongodb", "alluxio", "b2", "seafile", "upyun", "koofr", "yandex-disk" - * @param options is the options for given opendal services. Valid options depend on the scheme. Checkout https://docs.rs/opendal/latest/opendal/services/index.html for all valid options. - * - * For example, use minio as remote storage: - * - * ```typescript - * const storage = new S3Storage('s3', { - * endpoint: 'http://localhost:9000', - * region: 'us-east-1', - * bucket: 'testbucket', - * access_key_id: 'user', - * secret_access_key: 'password', - * root: '/path/to/basepath', - * }) - * ``` - */ - constructor(scheme: string, options?: Record | undefined | null) { - this.instance = new Operator(scheme, options) - - this.putFileContents = this.putFileContents.bind(this) - this.getFileContents = this.getFileContents.bind(this) - } - - public putFileContents = async (filename: string, data: string | Buffer) => { - if (!this.instance) { - return new Error('RemoteStorage client not initialized') - } - - try { - return await this.instance.write(filename, data) - } catch (error) { - Logger.error('[RemoteStorage] Error putting file contents:', error) - throw error - } - } - - public getFileContents = async (filename: string) => { - if (!this.instance) { - throw new Error('RemoteStorage client not initialized') - } - - try { - return await this.instance.read(filename) - } catch (error) { - Logger.error('[RemoteStorage] Error getting file contents:', error) - throw error - } - } - - public deleteFile = async (filename: string) => { - if (!this.instance) { - throw new Error('RemoteStorage client not initialized') - } - try { - return await this.instance.delete(filename) - } catch (error) { - Logger.error('[RemoteStorage] Error deleting file:', error) - throw error - } - } - - public checkConnection = async () => { - if (!this.instance) { - throw new Error('RemoteStorage client not initialized') - } - try { - // 检查根目录是否可访问 - return await this.instance.stat('/') - } catch (error) { - Logger.error('[RemoteStorage] Error checking connection:', error) - throw error - } - } -} diff --git a/src/main/services/S3Storage.ts b/src/main/services/S3Storage.ts new file mode 100644 index 0000000000..0b45bb0387 --- /dev/null +++ b/src/main/services/S3Storage.ts @@ -0,0 +1,183 @@ +import { + DeleteObjectCommand, + GetObjectCommand, + HeadBucketCommand, + ListObjectsV2Command, + PutObjectCommand, + S3Client +} from '@aws-sdk/client-s3' +import type { S3Config } from '@types' +import Logger from 'electron-log' +import * as net from 'net' +import { Readable } from 'stream' + +/** + * 将可读流转换为 Buffer + */ +function streamToBuffer(stream: Readable): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = [] + stream.on('data', (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))) + stream.on('error', reject) + stream.on('end', () => resolve(Buffer.concat(chunks))) + }) +} + +// 需要使用 Virtual Host-Style 的服务商域名后缀白名单 +const VIRTUAL_HOST_SUFFIXES = ['aliyuncs.com', 'myqcloud.com'] + +/** + * 使用 AWS SDK v3 的简单 S3 封装,兼容之前 RemoteStorage 的最常用接口。 + */ +export default class S3Storage { + private client: S3Client + private bucket: string + private root: string + + constructor(config: S3Config) { + const { endpoint, region, accessKeyId, secretAccessKey, bucket, root } = config + + const usePathStyle = (() => { + if (!endpoint) return false + + try { + const { hostname } = new URL(endpoint) + + if (hostname === 'localhost' || net.isIP(hostname) !== 0) { + return true + } + + const isInWhiteList = VIRTUAL_HOST_SUFFIXES.some((suffix) => hostname.endsWith(suffix)) + return !isInWhiteList + } catch (e) { + Logger.warn('[S3Storage] Failed to parse endpoint, fallback to Path-Style:', endpoint, e) + return true + } + })() + + this.client = new S3Client({ + region, + endpoint: endpoint || undefined, + credentials: { + accessKeyId: accessKeyId, + secretAccessKey: secretAccessKey + }, + forcePathStyle: usePathStyle + }) + + this.bucket = bucket + this.root = root?.replace(/^\/+/g, '').replace(/\/+$/g, '') || '' + + this.putFileContents = this.putFileContents.bind(this) + this.getFileContents = this.getFileContents.bind(this) + this.deleteFile = this.deleteFile.bind(this) + this.listFiles = this.listFiles.bind(this) + this.checkConnection = this.checkConnection.bind(this) + } + + /** + * 内部辅助方法,用来拼接带 root 的对象 key + */ + private buildKey(key: string): string { + if (!this.root) return key + return key.startsWith(`${this.root}/`) ? key : `${this.root}/${key}` + } + + async putFileContents(key: string, data: Buffer | string) { + try { + const contentType = key.endsWith('.zip') ? 'application/zip' : 'application/octet-stream' + + return await this.client.send( + new PutObjectCommand({ + Bucket: this.bucket, + Key: this.buildKey(key), + Body: data, + ContentType: contentType + }) + ) + } catch (error) { + Logger.error('[S3Storage] Error putting object:', error) + throw error + } + } + + async getFileContents(key: string): Promise { + try { + const res = await this.client.send(new GetObjectCommand({ Bucket: this.bucket, Key: this.buildKey(key) })) + if (!res.Body || !(res.Body instanceof Readable)) { + throw new Error('Empty body received from S3') + } + return await streamToBuffer(res.Body as Readable) + } catch (error) { + Logger.error('[S3Storage] Error getting object:', error) + throw error + } + } + + async deleteFile(key: string) { + try { + const keyWithRoot = this.buildKey(key) + const variations = new Set([keyWithRoot, key.replace(/^\//, '')]) + for (const k of variations) { + try { + await this.client.send(new DeleteObjectCommand({ Bucket: this.bucket, Key: k })) + } catch { + // 忽略删除失败 + } + } + } catch (error) { + Logger.error('[S3Storage] Error deleting object:', error) + throw error + } + } + + /** + * 列举指定前缀下的对象,默认列举全部。 + */ + async listFiles(prefix = ''): Promise> { + const files: Array<{ key: string; lastModified?: string; size: number }> = [] + let continuationToken: string | undefined + const fullPrefix = this.buildKey(prefix) + + try { + do { + const res = await this.client.send( + new ListObjectsV2Command({ + Bucket: this.bucket, + Prefix: fullPrefix === '' ? undefined : fullPrefix, + ContinuationToken: continuationToken + }) + ) + + res.Contents?.forEach((obj) => { + if (!obj.Key) return + files.push({ + key: obj.Key, + lastModified: obj.LastModified?.toISOString(), + size: obj.Size ?? 0 + }) + }) + + continuationToken = res.IsTruncated ? res.NextContinuationToken : undefined + } while (continuationToken) + + return files + } catch (error) { + Logger.error('[S3Storage] Error listing objects:', error) + throw error + } + } + + /** + * 尝试调用 HeadBucket 判断凭证/网络是否可用 + */ + async checkConnection() { + try { + await this.client.send(new HeadBucketCommand({ Bucket: this.bucket })) + return true + } catch (error) { + Logger.error('[S3Storage] Error checking connection:', error) + throw error + } + } +} diff --git a/src/main/services/SelectionService.ts b/src/main/services/SelectionService.ts index eba97179bc..21520a3735 100644 --- a/src/main/services/SelectionService.ts +++ b/src/main/services/SelectionService.ts @@ -1,7 +1,7 @@ import { SELECTION_FINETUNED_LIST, SELECTION_PREDEFINED_BLACKLIST } from '@main/configs/SelectionConfig' -import { isDev, isWin } from '@main/constant' +import { isDev, isMac, isWin } from '@main/constant' import { IpcChannel } from '@shared/IpcChannel' -import { BrowserWindow, ipcMain, screen } from 'electron' +import { app, BrowserWindow, ipcMain, screen, systemPreferences } from 'electron' import Logger from 'electron-log' import { join } from 'path' import type { @@ -16,9 +16,12 @@ import type { ActionItem } from '../../renderer/src/types/selectionTypes' import { ConfigKeys, configManager } from './ConfigManager' import storeSyncService from './StoreSyncService' +const isSupportedOS = isWin || isMac + let SelectionHook: SelectionHookConstructor | null = null try { - if (isWin) { + //since selection-hook v1.0.0, it supports macOS + if (isSupportedOS) { SelectionHook = require('selection-hook') } } catch (error) { @@ -118,7 +121,7 @@ export class SelectionService { } public static getInstance(): SelectionService | null { - if (!isWin) return null + if (!isSupportedOS) return null if (!SelectionService.instance) { SelectionService.instance = new SelectionService() @@ -138,7 +141,7 @@ export class SelectionService { * Initialize zoom factor from config and subscribe to changes * Ensures UI elements scale properly with system DPI settings */ - private initZoomFactor() { + private initZoomFactor(): void { const zoomFactor = configManager.getZoomFactor() if (zoomFactor) { this.setZoomFactor(zoomFactor) @@ -151,7 +154,7 @@ export class SelectionService { this.zoomFactor = zoomFactor } - private initConfig() { + private initConfig(): void { this.triggerMode = configManager.getSelectionAssistantTriggerMode() as TriggerMode this.isFollowToolbar = configManager.getSelectionAssistantFollowToolbar() this.isRemeberWinSize = configManager.getSelectionAssistantRemeberWinSize() @@ -204,7 +207,7 @@ export class SelectionService { * @param mode - The mode to set, either 'default', 'whitelist', or 'blacklist' * @param list - An array of strings representing the list of items to include or exclude */ - private setHookGlobalFilterMode(mode: string, list: string[]) { + private setHookGlobalFilterMode(mode: string, list: string[]): void { if (!this.selectionHook) return const modeMap = { @@ -213,6 +216,8 @@ export class SelectionService { blacklist: SelectionHook!.FilterMode.EXCLUDE_LIST } + const predefinedBlacklist = isWin ? SELECTION_PREDEFINED_BLACKLIST.WINDOWS : SELECTION_PREDEFINED_BLACKLIST.MAC + let combinedList: string[] = list let combinedMode = mode @@ -221,7 +226,7 @@ export class SelectionService { switch (mode) { case 'blacklist': //combine the predefined blacklist with the user-defined blacklist - combinedList = [...new Set([...list, ...SELECTION_PREDEFINED_BLACKLIST.WINDOWS])] + combinedList = [...new Set([...list, ...predefinedBlacklist])] break case 'whitelist': combinedList = [...list] @@ -229,7 +234,7 @@ export class SelectionService { case 'default': default: //use the predefined blacklist as the default filter list - combinedList = [...SELECTION_PREDEFINED_BLACKLIST.WINDOWS] + combinedList = [...predefinedBlacklist] combinedMode = 'blacklist' break } @@ -240,17 +245,24 @@ export class SelectionService { } } - private setHookFineTunedList() { + private setHookFineTunedList(): void { if (!this.selectionHook) return + const excludeClipboardCursorDetectList = isWin + ? SELECTION_FINETUNED_LIST.EXCLUDE_CLIPBOARD_CURSOR_DETECT.WINDOWS + : SELECTION_FINETUNED_LIST.EXCLUDE_CLIPBOARD_CURSOR_DETECT.MAC + const includeClipboardDelayReadList = isWin + ? SELECTION_FINETUNED_LIST.INCLUDE_CLIPBOARD_DELAY_READ.WINDOWS + : SELECTION_FINETUNED_LIST.INCLUDE_CLIPBOARD_DELAY_READ.MAC + this.selectionHook.setFineTunedList( SelectionHook!.FineTunedListType.EXCLUDE_CLIPBOARD_CURSOR_DETECT, - SELECTION_FINETUNED_LIST.EXCLUDE_CLIPBOARD_CURSOR_DETECT.WINDOWS + excludeClipboardCursorDetectList ) this.selectionHook.setFineTunedList( SelectionHook!.FineTunedListType.INCLUDE_CLIPBOARD_DELAY_READ, - SELECTION_FINETUNED_LIST.INCLUDE_CLIPBOARD_DELAY_READ.WINDOWS + includeClipboardDelayReadList ) } @@ -259,11 +271,33 @@ export class SelectionService { * @returns {boolean} Success status of service start */ public start(): boolean { - if (!this.selectionHook || this.started) { - this.logError(new Error('SelectionService start(): instance is null or already started')) + if (!isSupportedOS) { + this.logError(new Error('SelectionService start(): not supported on this OS')) return false } + if (!this.selectionHook) { + this.logError(new Error('SelectionService start(): instance is null')) + return false + } + + if (this.started) { + this.logError(new Error('SelectionService start(): already started')) + return false + } + + //On macOS, we need to check if the process is trusted + if (isMac) { + if (!systemPreferences.isTrustedAccessibilityClient(false)) { + this.logError( + new Error( + 'SelectionSerice not started: process is not trusted on macOS, please turn on the Accessibility permission' + ) + ) + return false + } + } + try { //make sure the toolbar window is ready this.createToolbarWindow() @@ -306,6 +340,7 @@ export class SelectionService { if (!this.selectionHook) return false this.selectionHook.stop() + this.selectionHook.cleanup() //already remove all listeners //reset the listener states @@ -316,6 +351,7 @@ export class SelectionService { this.toolbarWindow.close() this.toolbarWindow = null } + this.closePreloadedActionWindows() this.started = false @@ -342,7 +378,7 @@ export class SelectionService { * Toggle the enabled state of the selection service * Will sync the new enabled store to all renderer windows */ - public toggleEnabled(enabled: boolean | undefined = undefined) { + public toggleEnabled(enabled: boolean | undefined = undefined): void { if (!this.selectionHook) return const newEnabled = enabled === undefined ? !configManager.getSelectionAssistantEnabled() : enabled @@ -358,7 +394,7 @@ export class SelectionService { * Sets up window properties, event handlers, and loads the toolbar UI * @param readyCallback Optional callback when window is ready to show */ - private createToolbarWindow(readyCallback?: () => void) { + private createToolbarWindow(readyCallback?: () => void): void { if (this.isToolbarAlive()) return const { toolbarWidth, toolbarHeight } = this.getToolbarRealSize() @@ -366,21 +402,31 @@ export class SelectionService { this.toolbarWindow = new BrowserWindow({ width: toolbarWidth, height: toolbarHeight, + show: false, frame: false, transparent: true, alwaysOnTop: true, skipTaskbar: true, + autoHideMenuBar: true, resizable: false, minimizable: false, maximizable: false, + fullscreenable: false, // [macOS] must be false movable: true, - focusable: false, hasShadow: false, thickFrame: false, roundedCorners: true, backgroundMaterial: 'none', - type: 'toolbar', - show: false, + + // Platform specific settings + // [macOS] DO NOT set focusable to false, it will make other windows bring to front together + // [macOS] `panel` conflicts with other settings , + // and log will show `NSWindow does not support nonactivating panel styleMask 0x80` + // but it seems still work on fullscreen apps, so we set this anyway + ...(isWin ? { type: 'toolbar', focusable: false } : { type: 'panel' }), + hiddenInMissionControl: true, // [macOS only] + acceptFirstMouse: true, // [macOS only] + webPreferences: { preload: join(__dirname, '../preload/index.js'), contextIsolation: true, @@ -392,7 +438,9 @@ export class SelectionService { // Hide when losing focus this.toolbarWindow.on('blur', () => { - this.hideToolbar() + if (this.toolbarWindow!.isVisible()) { + this.hideToolbar() + } }) // Clean up when closed @@ -437,10 +485,10 @@ export class SelectionService { * @param point Reference point for positioning, logical coordinates * @param orientation Preferred position relative to reference point */ - private showToolbarAtPosition(point: Point, orientation: RelativeOrientation) { + private showToolbarAtPosition(point: Point, orientation: RelativeOrientation, programName: string): void { if (!this.isToolbarAlive()) { this.createToolbarWindow(() => { - this.showToolbarAtPosition(point, orientation) + this.showToolbarAtPosition(point, orientation, programName) }) return } @@ -460,15 +508,56 @@ export class SelectionService { //set the window to always on top (highest level) //should set every time the window is shown this.toolbarWindow!.setAlwaysOnTop(true, 'screen-saver') - this.toolbarWindow!.show() - /** - * In Windows 10, setOpacity(1) will make the window completely transparent - * It's a strange behavior, so we don't use it for compatibility - */ - // this.toolbarWindow!.setOpacity(1) + if (!isMac) { + this.toolbarWindow!.show() + /** + * [Windows] + * In Windows 10, setOpacity(1) will make the window completely transparent + * It's a strange behavior, so we don't use it for compatibility + */ + // this.toolbarWindow!.setOpacity(1) + this.startHideByMouseKeyListener() + return + } + + /************************************************ + * [macOS] the following code is only for macOS + * + * WARNING: + * DO NOT MODIFY THESE CODES, UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!!!! + *************************************************/ + + // [macOS] a hacky way + // when set `skipTransformProcessType: true`, if the selection is in self app, it will make the selection canceled after toolbar showing + // so we just don't set `skipTransformProcessType: true` when in self app + const isSelf = ['com.github.Electron', 'com.kangfenmao.CherryStudio'].includes(programName) + + if (!isSelf) { + // [macOS] an ugly hacky way + // `focusable: true` will make mainWindow disappeared when `setVisibleOnAllWorkspaces` + // so we set `focusable: true` before showing, and then set false after showing + this.toolbarWindow!.setFocusable(false) + + // [macOS] + // force `setVisibleOnAllWorkspaces: true` to let toolbar show in all workspaces. And we MUST not set it to false again + // set `skipTransformProcessType: true` to avoid dock icon spinning when `setVisibleOnAllWorkspaces` + this.toolbarWindow!.setVisibleOnAllWorkspaces(true, { + visibleOnFullScreen: true, + skipTransformProcessType: true + }) + } + + // [macOS] MUST use `showInactive()` to prevent other windows bring to front together + // [Windows] is OK for both `show()` and `showInactive()` because of `focusable: false` + this.toolbarWindow!.showInactive() + + // [macOS] restore the focusable status + this.toolbarWindow!.setFocusable(true) this.startHideByMouseKeyListener() + + return } /** @@ -477,18 +566,60 @@ export class SelectionService { public hideToolbar(): void { if (!this.isToolbarAlive()) return - // this.toolbarWindow!.setOpacity(0) + this.stopHideByMouseKeyListener() + + // [Windows] just hide the toolbar window is enough + if (!isMac) { + this.toolbarWindow!.hide() + return + } + + /************************************************ + * [macOS] the following code is only for macOS + *************************************************/ + + // [macOS] a HACKY way + // make sure other windows do not bring to front when toolbar is hidden + // get all focusable windows and set them to not focusable + const focusableWindows: BrowserWindow[] = [] + for (const window of BrowserWindow.getAllWindows()) { + if (!window.isDestroyed() && window.isVisible()) { + if (window.isFocusable()) { + focusableWindows.push(window) + window.setFocusable(false) + } + } + } + this.toolbarWindow!.hide() - this.stopHideByMouseKeyListener() + // set them back to focusable after 50ms + setTimeout(() => { + for (const window of focusableWindows) { + if (!window.isDestroyed()) { + window.setFocusable(true) + } + } + }, 50) + + // [macOS] hacky way + // Because toolbar is not a FOCUSED window, so the hover status will remain when next time show + // so we just send mouseMove event to the toolbar window to make the hover status disappear + this.toolbarWindow!.webContents.sendInputEvent({ + type: 'mouseMove', + x: -1, + y: -1 + }) + + return } /** * Check if toolbar window exists and is not destroyed * @returns {boolean} Toolbar window status */ - private isToolbarAlive() { - return this.toolbarWindow && !this.toolbarWindow.isDestroyed() + private isToolbarAlive(): boolean { + return !!(this.toolbarWindow && !this.toolbarWindow.isDestroyed()) } /** @@ -497,7 +628,7 @@ export class SelectionService { * @param width New toolbar width * @param height New toolbar height */ - public determineToolbarSize(width: number, height: number) { + public determineToolbarSize(width: number, height: number): void { const toolbarWidth = Math.ceil(width) // only update toolbar width if it's changed @@ -510,7 +641,7 @@ export class SelectionService { * Get actual toolbar dimensions accounting for zoom factor * @returns Object containing toolbar width and height */ - private getToolbarRealSize() { + private getToolbarRealSize(): { toolbarWidth: number; toolbarHeight: number } { return { toolbarWidth: this.TOOLBAR_WIDTH * this.zoomFactor, toolbarHeight: this.TOOLBAR_HEIGHT * this.zoomFactor @@ -520,71 +651,71 @@ export class SelectionService { /** * Calculate optimal toolbar position based on selection context * Ensures toolbar stays within screen boundaries and follows selection direction - * @param point Reference point for positioning, must be INTEGER + * @param refPoint Reference point for positioning, must be INTEGER * @param orientation Preferred position relative to reference point * @returns Calculated screen coordinates for toolbar, INTEGER */ - private calculateToolbarPosition(point: Point, orientation: RelativeOrientation): Point { + private calculateToolbarPosition(refPoint: Point, orientation: RelativeOrientation): Point { // Calculate initial position based on the specified anchor - let posX: number, posY: number + const posPoint: Point = { x: 0, y: 0 } const { toolbarWidth, toolbarHeight } = this.getToolbarRealSize() switch (orientation) { case 'topLeft': - posX = point.x - toolbarWidth - posY = point.y - toolbarHeight + posPoint.x = refPoint.x - toolbarWidth + posPoint.y = refPoint.y - toolbarHeight break case 'topRight': - posX = point.x - posY = point.y - toolbarHeight + posPoint.x = refPoint.x + posPoint.y = refPoint.y - toolbarHeight break case 'topMiddle': - posX = point.x - toolbarWidth / 2 - posY = point.y - toolbarHeight + posPoint.x = refPoint.x - toolbarWidth / 2 + posPoint.y = refPoint.y - toolbarHeight break case 'bottomLeft': - posX = point.x - toolbarWidth - posY = point.y + posPoint.x = refPoint.x - toolbarWidth + posPoint.y = refPoint.y break case 'bottomRight': - posX = point.x - posY = point.y + posPoint.x = refPoint.x + posPoint.y = refPoint.y break case 'bottomMiddle': - posX = point.x - toolbarWidth / 2 - posY = point.y + posPoint.x = refPoint.x - toolbarWidth / 2 + posPoint.y = refPoint.y break case 'middleLeft': - posX = point.x - toolbarWidth - posY = point.y - toolbarHeight / 2 + posPoint.x = refPoint.x - toolbarWidth + posPoint.y = refPoint.y - toolbarHeight / 2 break case 'middleRight': - posX = point.x - posY = point.y - toolbarHeight / 2 + posPoint.x = refPoint.x + posPoint.y = refPoint.y - toolbarHeight / 2 break case 'center': - posX = point.x - toolbarWidth / 2 - posY = point.y - toolbarHeight / 2 + posPoint.x = refPoint.x - toolbarWidth / 2 + posPoint.y = refPoint.y - toolbarHeight / 2 break default: // Default to 'topMiddle' if invalid position - posX = point.x - toolbarWidth / 2 - posY = point.y - toolbarHeight / 2 + posPoint.x = refPoint.x - toolbarWidth / 2 + posPoint.y = refPoint.y - toolbarHeight / 2 } //use original point to get the display - const display = screen.getDisplayNearestPoint({ x: point.x, y: point.y }) + const display = screen.getDisplayNearestPoint(refPoint) // Ensure toolbar stays within screen boundaries - posX = Math.round( - Math.max(display.workArea.x, Math.min(posX, display.workArea.x + display.workArea.width - toolbarWidth)) + posPoint.x = Math.round( + Math.max(display.workArea.x, Math.min(posPoint.x, display.workArea.x + display.workArea.width - toolbarWidth)) ) - posY = Math.round( - Math.max(display.workArea.y, Math.min(posY, display.workArea.y + display.workArea.height - toolbarHeight)) + posPoint.y = Math.round( + Math.max(display.workArea.y, Math.min(posPoint.y, display.workArea.y + display.workArea.height - toolbarHeight)) ) - return { x: posX, y: posY } + return posPoint } private isSamePoint(point1: Point, point2: Point): boolean { @@ -773,13 +904,17 @@ export class SelectionService { } if (!isLogical) { + // [macOS] don't need to convert by screenToDipPoint + if (!isMac) { + refPoint = screen.screenToDipPoint(refPoint) + } //screenToDipPoint can be float, so we need to round it - refPoint = screen.screenToDipPoint(refPoint) refPoint = { x: Math.round(refPoint.x), y: Math.round(refPoint.y) } } - this.showToolbarAtPosition(refPoint, refOrientation) - this.toolbarWindow?.webContents.send(IpcChannel.Selection_TextSelected, selectionData) + // [macOS] isFullscreen is only available on macOS + this.showToolbarAtPosition(refPoint, refOrientation, selectionData.programName) + this.toolbarWindow!.webContents.send(IpcChannel.Selection_TextSelected, selectionData) } /** @@ -787,7 +922,7 @@ export class SelectionService { */ // Start monitoring global mouse clicks - private startHideByMouseKeyListener() { + private startHideByMouseKeyListener(): void { try { // Register event handlers this.selectionHook!.on('mouse-down', this.handleMouseDownHide) @@ -800,7 +935,7 @@ export class SelectionService { } // Stop monitoring global mouse clicks - private stopHideByMouseKeyListener() { + private stopHideByMouseKeyListener(): void { if (!this.isHideByMouseKeyListenerActive) return try { @@ -832,8 +967,8 @@ export class SelectionService { return } - //data point is physical coordinates, convert to logical coordinates - const mousePoint = screen.screenToDipPoint({ x: data.x, y: data.y }) + //data point is physical coordinates, convert to logical coordinates(only for windows/linux) + const mousePoint = isMac ? { x: data.x, y: data.y } : screen.screenToDipPoint({ x: data.x, y: data.y }) const bounds = this.toolbarWindow!.getBounds() @@ -966,7 +1101,8 @@ export class SelectionService { frame: false, transparent: true, autoHideMenuBar: true, - titleBarStyle: 'hidden', + titleBarStyle: 'hidden', // [macOS] + trafficLightPosition: { x: 12, y: 9 }, // [macOS] hasShadow: false, thickFrame: false, show: false, @@ -993,7 +1129,7 @@ export class SelectionService { * Initialize preloaded action windows * Creates a pool of windows at startup for faster response */ - private async initPreloadedActionWindows() { + private async initPreloadedActionWindows(): Promise { try { // Create initial pool of preloaded windows for (let i = 0; i < this.PRELOAD_ACTION_WINDOW_COUNT; i++) { @@ -1007,7 +1143,7 @@ export class SelectionService { /** * Close all preloaded action windows */ - private closePreloadedActionWindows() { + private closePreloadedActionWindows(): void { for (const actionWindow of this.preloadedActionWindows) { if (!actionWindow.isDestroyed()) { actionWindow.destroy() @@ -1019,7 +1155,7 @@ export class SelectionService { * Preload a new action window asynchronously * This method is called after popping a window to ensure we always have windows ready */ - private async pushNewActionWindow() { + private async pushNewActionWindow(): Promise { try { const actionWindow = this.createPreloadedActionWindow() this.preloadedActionWindows.push(actionWindow) @@ -1033,7 +1169,7 @@ export class SelectionService { * Immediately returns a window and asynchronously creates a new one * @returns {BrowserWindow} The action window */ - private popActionWindow() { + private popActionWindow(): BrowserWindow { // Get a window from the preloaded queue or create a new one if empty const actionWindow = this.preloadedActionWindows.pop() || this.createPreloadedActionWindow() @@ -1043,6 +1179,27 @@ export class SelectionService { if (!actionWindow.isDestroyed()) { actionWindow.destroy() } + + // [macOS] a HACKY way + // make sure other windows do not bring to front when action window is closed + if (isMac) { + const focusableWindows: BrowserWindow[] = [] + for (const window of BrowserWindow.getAllWindows()) { + if (!window.isDestroyed() && window.isVisible()) { + if (window.isFocusable()) { + focusableWindows.push(window) + window.setFocusable(false) + } + } + } + setTimeout(() => { + for (const window of focusableWindows) { + if (!window.isDestroyed()) { + window.setFocusable(true) + } + } + }, 50) + } }) //remember the action window size @@ -1063,20 +1220,26 @@ export class SelectionService { return actionWindow } - public processAction(actionItem: ActionItem): void { + /** + * Process action item + * @param actionItem Action item to process + * @param isFullScreen [macOS] only macOS has the available isFullscreen mode + */ + public processAction(actionItem: ActionItem, isFullScreen: boolean = false): void { const actionWindow = this.popActionWindow() actionWindow.webContents.send(IpcChannel.Selection_UpdateActionData, actionItem) - this.showActionWindow(actionWindow) + this.showActionWindow(actionWindow, isFullScreen) } /** * Show action window with proper positioning relative to toolbar * Ensures window stays within screen boundaries * @param actionWindow Window to position and show + * @param isFullScreen [macOS] only macOS has the available isFullscreen mode */ - private showActionWindow(actionWindow: BrowserWindow) { + private showActionWindow(actionWindow: BrowserWindow, isFullScreen: boolean = false): void { let actionWindowWidth = this.ACTION_WINDOW_WIDTH let actionWindowHeight = this.ACTION_WINDOW_HEIGHT @@ -1086,63 +1249,125 @@ export class SelectionService { actionWindowHeight = this.lastActionWindowSize.height } - //center way + /******************************************** + * Setting the position of the action window + ********************************************/ + const display = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()) + const workArea = display.workArea + + // Center of the screen if (!this.isFollowToolbar || !this.toolbarWindow) { - if (this.isRemeberWinSize) { - actionWindow.setBounds({ - width: actionWindowWidth, - height: actionWindowHeight - }) + const centerX = Math.round(workArea.x + (workArea.width - actionWindowWidth) / 2) + const centerY = Math.round(workArea.y + (workArea.height - actionWindowHeight) / 2) + + actionWindow.setPosition(centerX, centerY, false) + actionWindow.setBounds({ + width: actionWindowWidth, + height: actionWindowHeight, + x: centerX, + y: centerY + }) + } else { + // Follow toolbar position + const toolbarBounds = this.toolbarWindow!.getBounds() + const GAP = 6 // 6px gap from screen edges + + //make sure action window is inside screen + if (actionWindowWidth > workArea.width - 2 * GAP) { + actionWindowWidth = workArea.width - 2 * GAP } + if (actionWindowHeight > workArea.height - 2 * GAP) { + actionWindowHeight = workArea.height - 2 * GAP + } + + // Calculate initial position to center action window horizontally below toolbar + let posX = Math.round(toolbarBounds.x + (toolbarBounds.width - actionWindowWidth) / 2) + let posY = Math.round(toolbarBounds.y) + + // Ensure action window stays within screen boundaries with a small gap + if (posX + actionWindowWidth > workArea.x + workArea.width) { + posX = workArea.x + workArea.width - actionWindowWidth - GAP + } else if (posX < workArea.x) { + posX = workArea.x + GAP + } + if (posY + actionWindowHeight > workArea.y + workArea.height) { + // If window would go below screen, try to position it above toolbar + posY = workArea.y + workArea.height - actionWindowHeight - GAP + } else if (posY < workArea.y) { + posY = workArea.y + GAP + } + + actionWindow.setPosition(posX, posY, false) + //KEY to make window not resize + actionWindow.setBounds({ + width: actionWindowWidth, + height: actionWindowHeight, + x: posX, + y: posY + }) + } + + if (!isMac) { actionWindow.show() - this.hideToolbar() return } - //follow toolbar + /************************************************ + * [macOS] the following code is only for macOS + * + * WARNING: + * DO NOT MODIFY THESE CODES, UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!!!! + *************************************************/ - const toolbarBounds = this.toolbarWindow!.getBounds() - const display = screen.getDisplayNearestPoint({ x: toolbarBounds.x, y: toolbarBounds.y }) - const workArea = display.workArea - const GAP = 6 // 6px gap from screen edges - - //make sure action window is inside screen - if (actionWindowWidth > workArea.width - 2 * GAP) { - actionWindowWidth = workArea.width - 2 * GAP + // act normally when the app is not in fullscreen mode + if (!isFullScreen) { + actionWindow.show() + return } - if (actionWindowHeight > workArea.height - 2 * GAP) { - actionWindowHeight = workArea.height - 2 * GAP - } + // [macOS] an UGLY HACKY way for fullscreen override settings - // Calculate initial position to center action window horizontally below toolbar - let posX = Math.round(toolbarBounds.x + (toolbarBounds.width - actionWindowWidth) / 2) - let posY = Math.round(toolbarBounds.y) + // FIXME sometimes the dock will be shown when the action window is shown + // FIXME if actionWindow show on the fullscreen app, switch to other space will cause the mainWindow to be shown + // FIXME When setVisibleOnAllWorkspaces is true, docker icon disappeared when the first action window is shown on the fullscreen app + // use app.dock.show() to show the dock again will cause the action window to be closed when auto hide on blur is enabled - // Ensure action window stays within screen boundaries with a small gap - if (posX + actionWindowWidth > workArea.x + workArea.width) { - posX = workArea.x + workArea.width - actionWindowWidth - GAP - } else if (posX < workArea.x) { - posX = workArea.x + GAP - } - if (posY + actionWindowHeight > workArea.y + workArea.height) { - // If window would go below screen, try to position it above toolbar - posY = workArea.y + workArea.height - actionWindowHeight - GAP - } else if (posY < workArea.y) { - posY = workArea.y + GAP - } + // setFocusable(false) to prevent the action window hide when blur (if auto hide on blur is enabled) + actionWindow.setFocusable(false) + actionWindow.setAlwaysOnTop(true, 'floating') - actionWindow.setPosition(posX, posY, false) - //KEY to make window not resize - actionWindow.setBounds({ - width: actionWindowWidth, - height: actionWindowHeight, - x: posX, - y: posY + // `setVisibleOnAllWorkspaces(true)` will cause the dock icon disappeared + // just store the dock icon status, and show it again + const isDockShown = app.dock?.isVisible() + + // DO NOT set `skipTransformProcessType: true`, + // it will cause the action window to be shown on other space + actionWindow.setVisibleOnAllWorkspaces(true, { + visibleOnFullScreen: true }) - actionWindow.show() + actionWindow.showInactive() + + // show the dock again if last time it was shown + // do not put it after `actionWindow.focus()`, will cause the action window to be closed when auto hide on blur is enabled + if (!app.dock?.isVisible() && isDockShown) { + app.dock?.show() + } + + // unset everything + setTimeout(() => { + actionWindow.setVisibleOnAllWorkspaces(false, { + visibleOnFullScreen: true, + skipTransformProcessType: true + }) + actionWindow.setAlwaysOnTop(false) + + actionWindow.setFocusable(true) + + // regain the focus when all the works done + actionWindow.focus() + }, 50) } public closeActionWindow(actionWindow: BrowserWindow): void { @@ -1162,38 +1387,40 @@ export class SelectionService { * Switches between selection-based and alt-key based triggering * Manages appropriate event listeners for each mode */ - private processTriggerMode() { + private processTriggerMode(): void { + if (!this.selectionHook) return + switch (this.triggerMode) { case TriggerMode.Selected: if (this.isCtrlkeyListenerActive) { - this.selectionHook!.off('key-down', this.handleKeyDownCtrlkeyMode) - this.selectionHook!.off('key-up', this.handleKeyUpCtrlkeyMode) + this.selectionHook.off('key-down', this.handleKeyDownCtrlkeyMode) + this.selectionHook.off('key-up', this.handleKeyUpCtrlkeyMode) this.isCtrlkeyListenerActive = false } - this.selectionHook!.setSelectionPassiveMode(false) + this.selectionHook.setSelectionPassiveMode(false) break case TriggerMode.Ctrlkey: if (!this.isCtrlkeyListenerActive) { - this.selectionHook!.on('key-down', this.handleKeyDownCtrlkeyMode) - this.selectionHook!.on('key-up', this.handleKeyUpCtrlkeyMode) + this.selectionHook.on('key-down', this.handleKeyDownCtrlkeyMode) + this.selectionHook.on('key-up', this.handleKeyUpCtrlkeyMode) this.isCtrlkeyListenerActive = true } - this.selectionHook!.setSelectionPassiveMode(true) + this.selectionHook.setSelectionPassiveMode(true) break case TriggerMode.Shortcut: //remove the ctrlkey listener, don't need any key listener for shortcut mode if (this.isCtrlkeyListenerActive) { - this.selectionHook!.off('key-down', this.handleKeyDownCtrlkeyMode) - this.selectionHook!.off('key-up', this.handleKeyUpCtrlkeyMode) + this.selectionHook.off('key-down', this.handleKeyDownCtrlkeyMode) + this.selectionHook.off('key-up', this.handleKeyUpCtrlkeyMode) this.isCtrlkeyListenerActive = false } - this.selectionHook!.setSelectionPassiveMode(true) + this.selectionHook.setSelectionPassiveMode(true) break } } @@ -1214,7 +1441,7 @@ export class SelectionService { selectionService?.hideToolbar() }) - ipcMain.handle(IpcChannel.Selection_WriteToClipboard, (_, text: string) => { + ipcMain.handle(IpcChannel.Selection_WriteToClipboard, (_, text: string): boolean => { return selectionService?.writeToClipboard(text) ?? false }) @@ -1246,8 +1473,9 @@ export class SelectionService { configManager.setSelectionAssistantFilterList(filterList) }) - ipcMain.handle(IpcChannel.Selection_ProcessAction, (_, actionItem: ActionItem) => { - selectionService?.processAction(actionItem) + // [macOS] only macOS has the available isFullscreen mode + ipcMain.handle(IpcChannel.Selection_ProcessAction, (_, actionItem: ActionItem, isFullScreen: boolean = false) => { + selectionService?.processAction(actionItem, isFullScreen) }) ipcMain.handle(IpcChannel.Selection_ActionWindowClose, (event) => { @@ -1274,13 +1502,13 @@ export class SelectionService { this.isIpcHandlerRegistered = true } - private logInfo(message: string, forceShow: boolean = false) { + private logInfo(message: string, forceShow: boolean = false): void { if (isDev || forceShow) { Logger.info('[SelectionService] Info: ', message) } } - private logError(...args: [...string[], Error]) { + private logError(...args: [...string[], Error]): void { Logger.error('[SelectionService] Error: ', ...args) } } @@ -1291,9 +1519,9 @@ export class SelectionService { * @returns {boolean} Success status of initialization */ export function initSelectionService(): boolean { - if (!isWin) return false + if (!isSupportedOS) return false - configManager.subscribe(ConfigKeys.SelectionAssistantEnabled, (enabled: boolean) => { + configManager.subscribe(ConfigKeys.SelectionAssistantEnabled, (enabled: boolean): void => { //avoid closure const ss = SelectionService.getInstance() if (!ss) { diff --git a/src/main/services/ShortcutService.ts b/src/main/services/ShortcutService.ts index 24ea2324fd..92544e17c0 100644 --- a/src/main/services/ShortcutService.ts +++ b/src/main/services/ShortcutService.ts @@ -55,7 +55,8 @@ function formatShortcutKey(shortcut: string[]): string { return shortcut.join('+') } -// convert the shortcut recorded by keyboard event key value to electron global shortcut format +// convert the shortcut recorded by JS keyboard event key value to electron global shortcut format +// see: https://www.electronjs.org/zh/docs/latest/api/accelerator const convertShortcutFormat = (shortcut: string | string[]): string => { const accelerator = (() => { if (Array.isArray(shortcut)) { @@ -68,12 +69,34 @@ const convertShortcutFormat = (shortcut: string | string[]): string => { return accelerator .map((key) => { switch (key) { + // OLD WAY FOR MODIFIER KEYS, KEEP THEM HERE FOR REFERENCE + // case 'Command': + // return 'CommandOrControl' + // case 'Control': + // return 'Control' + // case 'Ctrl': + // return 'Control' + + // NEW WAY FOR MODIFIER KEYS + // you can see all the modifier keys in the same + case 'CommandOrControl': + return 'CommandOrControl' + case 'Ctrl': + return 'Ctrl' + case 'Alt': + return 'Alt' // Use `Alt` instead of `Option`. The `Option` key only exists on macOS, whereas the `Alt` key is available on all platforms. + case 'Meta': + return 'Meta' // `Meta` key is mapped to the Windows key on Windows and Linux, `Cmd` on macOS. + case 'Shift': + return 'Shift' + + // For backward compatibility with old data case 'Command': + case 'Cmd': return 'CommandOrControl' case 'Control': - return 'Control' - case 'Ctrl': - return 'Control' + return 'Ctrl' + case 'ArrowUp': return 'Up' case 'ArrowDown': @@ -83,7 +106,7 @@ const convertShortcutFormat = (shortcut: string | string[]): string => { case 'ArrowRight': return 'Right' case 'AltGraph': - return 'Alt' + return 'AltGr' case 'Slash': return '/' case 'Semicolon': diff --git a/src/main/services/ThemeService.ts b/src/main/services/ThemeService.ts index 7ccaf3bf9a..a56b559357 100644 --- a/src/main/services/ThemeService.ts +++ b/src/main/services/ThemeService.ts @@ -1,48 +1,48 @@ -import { IpcChannel } from '@shared/IpcChannel' -import { ThemeMode } from '@types' -import { BrowserWindow, nativeTheme } from 'electron' - -import { titleBarOverlayDark, titleBarOverlayLight } from '../config' -import { configManager } from './ConfigManager' - -class ThemeService { - private theme: ThemeMode = ThemeMode.system - constructor() { - this.theme = configManager.getTheme() - - if (this.theme === ThemeMode.dark || this.theme === ThemeMode.light || this.theme === ThemeMode.system) { - nativeTheme.themeSource = this.theme - } else { - // 兼容旧版本 - configManager.setTheme(ThemeMode.system) - nativeTheme.themeSource = ThemeMode.system - } - nativeTheme.on('updated', this.themeUpdatadHandler.bind(this)) - } - - themeUpdatadHandler() { - BrowserWindow.getAllWindows().forEach((win) => { - if (win && !win.isDestroyed() && win.setTitleBarOverlay) { - try { - win.setTitleBarOverlay(nativeTheme.shouldUseDarkColors ? titleBarOverlayDark : titleBarOverlayLight) - } catch (error) { - // don't throw error if setTitleBarOverlay failed - // Because it may be called with some windows have some title bar - } - } - win.webContents.send(IpcChannel.ThemeUpdated, nativeTheme.shouldUseDarkColors ? ThemeMode.dark : ThemeMode.light) - }) - } - - setTheme(theme: ThemeMode) { - if (theme === this.theme) { - return - } - - this.theme = theme - nativeTheme.themeSource = theme - configManager.setTheme(theme) - } -} - -export const themeService = new ThemeService() +import { IpcChannel } from '@shared/IpcChannel' +import { ThemeMode } from '@types' +import { BrowserWindow, nativeTheme } from 'electron' + +import { titleBarOverlayDark, titleBarOverlayLight } from '../config' +import { configManager } from './ConfigManager' + +class ThemeService { + private theme: ThemeMode = ThemeMode.system + constructor() { + this.theme = configManager.getTheme() + + if (this.theme === ThemeMode.dark || this.theme === ThemeMode.light || this.theme === ThemeMode.system) { + nativeTheme.themeSource = this.theme + } else { + // 兼容旧版本 + configManager.setTheme(ThemeMode.system) + nativeTheme.themeSource = ThemeMode.system + } + nativeTheme.on('updated', this.themeUpdatadHandler.bind(this)) + } + + themeUpdatadHandler() { + BrowserWindow.getAllWindows().forEach((win) => { + if (win && !win.isDestroyed() && win.setTitleBarOverlay) { + try { + win.setTitleBarOverlay(nativeTheme.shouldUseDarkColors ? titleBarOverlayDark : titleBarOverlayLight) + } catch (error) { + // don't throw error if setTitleBarOverlay failed + // Because it may be called with some windows have some title bar + } + } + win.webContents.send(IpcChannel.ThemeUpdated, nativeTheme.shouldUseDarkColors ? ThemeMode.dark : ThemeMode.light) + }) + } + + setTheme(theme: ThemeMode) { + if (theme === this.theme) { + return + } + + this.theme = theme + nativeTheme.themeSource = theme + configManager.setTheme(theme) + } +} + +export const themeService = new ThemeService() diff --git a/src/main/services/TrayService.ts b/src/main/services/TrayService.ts index 89c88bc0ae..205d7fdee9 100644 --- a/src/main/services/TrayService.ts +++ b/src/main/services/TrayService.ts @@ -84,10 +84,8 @@ export class TrayService { label: trayLocale.show_mini_window, click: () => windowService.showMiniWindow() }, - isWin && { + (isWin || isMac) && { label: selectionLocale.name + (selectionAssistantEnabled ? ' - On' : ' - Off'), - // type: 'checkbox', - // checked: selectionAssistantEnabled, click: () => { if (selectionService) { selectionService.toggleEnabled() diff --git a/src/main/services/WebDav.ts b/src/main/services/WebDav.ts index fae0e2da38..76996140e0 100644 --- a/src/main/services/WebDav.ts +++ b/src/main/services/WebDav.ts @@ -23,7 +23,9 @@ export default class WebDav { password: params.webdavPass, maxBodyLength: Infinity, maxContentLength: Infinity, - httpsAgent: new https.Agent({ rejectUnauthorized: false }) + httpsAgent: new https.Agent({ + rejectUnauthorized: false + }) }) this.putFileContents = this.putFileContents.bind(this) diff --git a/src/main/services/WindowService.ts b/src/main/services/WindowService.ts index ada014f0db..fd2a3c9c84 100644 --- a/src/main/services/WindowService.ts +++ b/src/main/services/WindowService.ts @@ -5,7 +5,7 @@ import { is } from '@electron-toolkit/utils' import { isDev, isLinux, isMac, isWin } from '@main/constant' import { getFilesDir } from '@main/utils/file' import { IpcChannel } from '@shared/IpcChannel' -import { app, BrowserWindow, nativeTheme, shell } from 'electron' +import { app, BrowserWindow, nativeTheme, screen, shell } from 'electron' import Logger from 'electron-log' import windowStateKeeper from 'electron-window-state' import { join } from 'path' @@ -16,6 +16,9 @@ import { configManager } from './ConfigManager' import { contextMenu } from './ContextMenu' import { initSessionUserAgent } from './WebviewService' +const DEFAULT_MINIWINDOW_WIDTH = 550 +const DEFAULT_MINIWINDOW_HEIGHT = 400 + export class WindowService { private static instance: WindowService | null = null private mainWindow: BrowserWindow | null = null @@ -26,6 +29,11 @@ export class WindowService { private wasMainWindowFocused: boolean = false private lastRendererProcessCrashTime: number = 0 + private miniWindowSize: { width: number; height: number } = { + width: DEFAULT_MINIWINDOW_WIDTH, + height: DEFAULT_MINIWINDOW_HEIGHT + } + public static getInstance(): WindowService { if (!WindowService.instance) { WindowService.instance = new WindowService() @@ -41,8 +49,8 @@ export class WindowService { } const mainWindowState = windowStateKeeper({ - defaultWidth: 1080, - defaultHeight: 670, + defaultWidth: 960, + defaultHeight: 600, fullScreen: false, maximize: false }) @@ -52,7 +60,7 @@ export class WindowService { y: mainWindowState.y, width: mainWindowState.width, height: mainWindowState.height, - minWidth: 1080, + minWidth: 960, minHeight: 600, show: false, autoHideMenuBar: true, @@ -426,8 +434,8 @@ export class WindowService { public createMiniWindow(isPreload: boolean = false): BrowserWindow { this.miniWindow = new BrowserWindow({ - width: 550, - height: 400, + width: this.miniWindowSize.width, + height: this.miniWindowSize.height, minWidth: 350, minHeight: 380, maxWidth: 1024, @@ -437,13 +445,12 @@ export class WindowService { transparent: isMac, vibrancy: 'under-window', visualEffectState: 'followWindow', - center: true, frame: false, alwaysOnTop: true, - resizable: true, useContentSize: true, ...(isMac ? { type: 'panel' } : {}), skipTaskbar: true, + resizable: true, minimizable: false, maximizable: false, fullscreenable: false, @@ -451,8 +458,7 @@ export class WindowService { preload: join(__dirname, '../preload/index.js'), sandbox: false, webSecurity: false, - webviewTag: true, - backgroundThrottling: false + webviewTag: true } }) @@ -486,6 +492,13 @@ export class WindowService { this.miniWindow?.webContents.send(IpcChannel.HideMiniWindow) }) + this.miniWindow.on('resized', () => { + this.miniWindowSize = this.miniWindow?.getBounds() || { + width: DEFAULT_MINIWINDOW_WIDTH, + height: DEFAULT_MINIWINDOW_HEIGHT + } + }) + this.miniWindow.on('show', () => { this.miniWindow?.webContents.send(IpcChannel.ShowMiniWindow) }) @@ -509,10 +522,48 @@ export class WindowService { if (this.miniWindow && !this.miniWindow.isDestroyed()) { this.wasMainWindowFocused = this.mainWindow?.isFocused() || false - if (this.miniWindow.isMinimized()) { - this.miniWindow.restore() + // [Windows] hacky fix + // the window is minimized only when in Windows platform + // because it's a workround for Windows, see `hideMiniWindow()` + if (this.miniWindow?.isMinimized()) { + // don't let the window being seen before we finish adusting the position across screens + this.miniWindow?.setOpacity(0) + // DO NOT use `restore()` here, Electron has the bug with screens of different scale factor + // We have to use `show()` here, then set the position and bounds + this.miniWindow?.show() } - this.miniWindow.show() + + const miniWindowBounds = this.miniWindow.getBounds() + + // Check if miniWindow is on the same screen as mouse cursor + const cursorDisplay = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()) + const miniWindowDisplay = screen.getDisplayNearestPoint(miniWindowBounds) + + // Show the miniWindow on the cursor's screen center + // If miniWindow is not on the same screen as cursor, move it to cursor's screen center + if (cursorDisplay.id !== miniWindowDisplay.id) { + const workArea = cursorDisplay.bounds + + // use remembered size to avoid the bug of Electron with screens of different scale factor + const miniWindowWidth = this.miniWindowSize.width + const miniWindowHeight = this.miniWindowSize.height + + // move to the center of the cursor's screen + const miniWindowX = Math.round(workArea.x + (workArea.width - miniWindowWidth) / 2) + const miniWindowY = Math.round(workArea.y + (workArea.height - miniWindowHeight) / 2) + + this.miniWindow.setPosition(miniWindowX, miniWindowY, false) + this.miniWindow.setBounds({ + x: miniWindowX, + y: miniWindowY, + width: miniWindowWidth, + height: miniWindowHeight + }) + } + + this.miniWindow?.setOpacity(1) + this.miniWindow?.show() + return } @@ -520,20 +571,26 @@ export class WindowService { } public hideMiniWindow() { - //hacky-fix:[mac/win] previous window(not self-app) should be focused again after miniWindow hide + if (!this.miniWindow || this.miniWindow.isDestroyed()) { + return + } + + //[macOs/Windows] hacky fix + // previous window(not self-app) should be focused again after miniWindow hide + // this workaround is to make previous window focused again after miniWindow hide if (isWin) { - this.miniWindow?.minimize() - this.miniWindow?.hide() + this.miniWindow.setOpacity(0) // don't show the minimizing animation + this.miniWindow.minimize() return } else if (isMac) { - this.miniWindow?.hide() + this.miniWindow.hide() if (!this.wasMainWindowFocused) { app.hide() } return } - this.miniWindow?.hide() + this.miniWindow.hide() } public closeMiniWindow() { diff --git a/src/main/services/memory/MemoryService.ts b/src/main/services/memory/MemoryService.ts new file mode 100644 index 0000000000..07f0932525 --- /dev/null +++ b/src/main/services/memory/MemoryService.ts @@ -0,0 +1,829 @@ +import { Client, createClient } from '@libsql/client' +import Embeddings from '@main/knowledge/embeddings/Embeddings' +import type { + AddMemoryOptions, + AssistantMessage, + MemoryConfig, + MemoryHistoryItem, + MemoryItem, + MemoryListOptions, + MemorySearchOptions +} from '@types' +import crypto from 'crypto' +import { app } from 'electron' +import Logger from 'electron-log' +import path from 'path' + +import { MemoryQueries } from './queries' + +export interface EmbeddingOptions { + model: string + provider: string + apiKey: string + apiVersion?: string + baseURL: string + dimensions?: number + batchSize?: number +} + +export interface VectorSearchOptions { + limit?: number + threshold?: number + userId?: string + agentId?: string + filters?: Record +} + +export interface SearchResult { + memories: MemoryItem[] + count: number + error?: string +} + +export class MemoryService { + private static instance: MemoryService | null = null + private db: Client | null = null + private isInitialized = false + private embeddings: Embeddings | null = null + private config: MemoryConfig | null = null + private static readonly UNIFIED_DIMENSION = 1536 + private static readonly SIMILARITY_THRESHOLD = 0.85 + + private constructor() { + // Private constructor to enforce singleton pattern + } + + public static getInstance(): MemoryService { + if (!MemoryService.instance) { + MemoryService.instance = new MemoryService() + } + return MemoryService.instance + } + + public static reload(): MemoryService { + if (MemoryService.instance) { + MemoryService.instance.close() + } + MemoryService.instance = new MemoryService() + return MemoryService.instance + } + + /** + * Initialize the database connection and create tables + */ + private async init(): Promise { + if (this.isInitialized && this.db) { + return + } + + try { + const userDataPath = app.getPath('userData') + const dbPath = path.join(userDataPath, 'memories.db') + + this.db = createClient({ + url: `file:${dbPath}`, + intMode: 'number' + }) + + // Create tables + await this.createTables() + this.isInitialized = true + Logger.info('Memory database initialized successfully') + } catch (error) { + Logger.error('Failed to initialize memory database:', error) + throw new Error( + `Memory database initialization failed: ${error instanceof Error ? error.message : 'Unknown error'}` + ) + } + } + + private async createTables(): Promise { + if (!this.db) throw new Error('Database not initialized') + + // Create memories table with native vector support + await this.db.execute(MemoryQueries.createTables.memories) + + // Create memory history table + await this.db.execute(MemoryQueries.createTables.memoryHistory) + + // Create indexes + await this.db.execute(MemoryQueries.createIndexes.userId) + await this.db.execute(MemoryQueries.createIndexes.agentId) + await this.db.execute(MemoryQueries.createIndexes.createdAt) + await this.db.execute(MemoryQueries.createIndexes.hash) + await this.db.execute(MemoryQueries.createIndexes.memoryHistory) + + // Create vector index for similarity search + try { + await this.db.execute(MemoryQueries.createIndexes.vector) + } catch (error) { + // Vector index might not be supported in all versions + Logger.warn('Failed to create vector index, falling back to non-indexed search:', error) + } + } + + /** + * Add new memories from messages + */ + public async add(messages: string | AssistantMessage[], options: AddMemoryOptions): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + const { userId, agentId, runId, metadata } = options + + try { + // Convert messages to memory strings + const memoryStrings = Array.isArray(messages) + ? messages.map((m) => (typeof m === 'string' ? m : m.content)) + : [messages] + const addedMemories: MemoryItem[] = [] + + for (const memory of memoryStrings) { + const trimmedMemory = memory.trim() + if (!trimmedMemory) continue + + // Generate hash for deduplication + const hash = crypto.createHash('sha256').update(trimmedMemory).digest('hex') + + // Check if memory already exists + const existing = await this.db.execute({ + sql: MemoryQueries.memory.checkExistsIncludeDeleted, + args: [hash] + }) + + if (existing.rows.length > 0) { + const existingRecord = existing.rows[0] as any + const isDeleted = existingRecord.is_deleted === 1 + + if (!isDeleted) { + // Active record exists, skip insertion + Logger.info(`Memory already exists with hash: ${hash}`) + continue + } else { + // Deleted record exists, restore it instead of inserting new one + Logger.info(`Restoring deleted memory with hash: ${hash}`) + + // Generate embedding if model is configured + let embedding: number[] | null = null + const embedderApiClient = this.config?.embedderApiClient + if (embedderApiClient) { + try { + embedding = await this.generateEmbedding(trimmedMemory) + Logger.info( + `Generated embedding for restored memory with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})` + ) + } catch (error) { + Logger.error('Failed to generate embedding for restored memory:', error) + } + } + + const now = new Date().toISOString() + + // Restore the deleted record + await this.db.execute({ + sql: MemoryQueries.memory.restoreDeleted, + args: [ + trimmedMemory, + embedding ? this.embeddingToVector(embedding) : null, + metadata ? JSON.stringify(metadata) : null, + now, + existingRecord.id + ] + }) + + // Add to history + await this.addHistory(existingRecord.id, null, trimmedMemory, 'ADD') + + addedMemories.push({ + id: existingRecord.id, + memory: trimmedMemory, + hash, + createdAt: now, + updatedAt: now, + metadata + }) + continue + } + } + + // Generate embedding if model is configured + let embedding: number[] | null = null + if (this.config?.embedderApiClient) { + try { + embedding = await this.generateEmbedding(trimmedMemory) + Logger.info( + `Generated embedding with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})` + ) + + // Check for similar memories using vector similarity + const similarMemories = await this.hybridSearch(trimmedMemory, embedding, { + limit: 5, + threshold: 0.1, // Lower threshold to get more candidates + userId, + agentId + }) + + // Check if any similar memory exceeds the similarity threshold + if (similarMemories.memories.length > 0) { + const highestSimilarity = Math.max(...similarMemories.memories.map((m) => m.score || 0)) + if (highestSimilarity >= MemoryService.SIMILARITY_THRESHOLD) { + Logger.info( + `Skipping memory addition due to high similarity: ${highestSimilarity.toFixed(3)} >= ${MemoryService.SIMILARITY_THRESHOLD}` + ) + Logger.info(`Similar memory found: "${similarMemories.memories[0].memory}"`) + continue + } + } + } catch (error) { + Logger.error('Failed to generate embedding:', error) + } + } + + // Insert new memory + const id = crypto.randomUUID() + const now = new Date().toISOString() + + await this.db.execute({ + sql: MemoryQueries.memory.insert, + args: [ + id, + trimmedMemory, + hash, + embedding ? this.embeddingToVector(embedding) : null, + metadata ? JSON.stringify(metadata) : null, + userId || null, + agentId || null, + runId || null, + now, + now + ] + }) + + // Add to history + await this.addHistory(id, null, trimmedMemory, 'ADD') + + addedMemories.push({ + id, + memory: trimmedMemory, + hash, + createdAt: now, + updatedAt: now, + metadata + }) + } + + return { + memories: addedMemories, + count: addedMemories.length + } + } catch (error) { + Logger.error('Failed to add memories:', error) + return { + memories: [], + count: 0, + error: error instanceof Error ? error.message : 'Unknown error' + } + } + } + + /** + * Search memories using text or vector similarity + */ + public async search(query: string, options: MemorySearchOptions = {}): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + const { limit = 10, userId, agentId, filters = {} } = options + + try { + // If we have an embedder model configured, use vector search + if (this.config?.embedderApiClient) { + try { + const queryEmbedding = await this.generateEmbedding(query) + return await this.hybridSearch(query, queryEmbedding, { limit, userId, agentId, filters }) + } catch (error) { + Logger.error('Vector search failed, falling back to text search:', error) + } + } + + // Fallback to text search + const conditions: string[] = ['m.is_deleted = 0'] + const params: any[] = [] + + // Add search conditions + conditions.push('(m.memory LIKE ? OR m.memory LIKE ?)') + params.push(`%${query}%`, `%${query.split(' ').join('%')}%`) + + if (userId) { + conditions.push('m.user_id = ?') + params.push(userId) + } + + if (agentId) { + conditions.push('m.agent_id = ?') + params.push(agentId) + } + + // Add custom filters + for (const [key, value] of Object.entries(filters)) { + if (value !== undefined && value !== null) { + conditions.push(`json_extract(m.metadata, '$.${key}') = ?`) + params.push(value) + } + } + + const whereClause = conditions.join(' AND ') + params.push(limit) + + const result = await this.db.execute({ + sql: `${MemoryQueries.memory.list} ${whereClause} + ORDER BY m.created_at DESC + LIMIT ? + `, + args: params + }) + + const memories: MemoryItem[] = result.rows.map((row: any) => ({ + id: row.id as string, + memory: row.memory as string, + hash: (row.hash as string) || undefined, + metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined, + createdAt: row.created_at as string, + updatedAt: row.updated_at as string + })) + + return { + memories, + count: memories.length + } + } catch (error) { + Logger.error('Search failed:', error) + return { + memories: [], + count: 0, + error: error instanceof Error ? error.message : 'Unknown error' + } + } + } + + /** + * List all memories with optional filters + */ + public async list(options: MemoryListOptions = {}): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + const { userId, agentId, limit = 100, offset = 0 } = options + + try { + const conditions: string[] = ['m.is_deleted = 0'] + const params: any[] = [] + + if (userId) { + conditions.push('m.user_id = ?') + params.push(userId) + } + + if (agentId) { + conditions.push('m.agent_id = ?') + params.push(agentId) + } + + const whereClause = conditions.join(' AND ') + + // Get total count + const countResult = await this.db.execute({ + sql: `${MemoryQueries.memory.count} ${whereClause}`, + args: params + }) + const totalCount = (countResult.rows[0] as any).total as number + + // Get paginated results + params.push(limit, offset) + const result = await this.db.execute({ + sql: `${MemoryQueries.memory.list} ${whereClause} + ORDER BY m.created_at DESC + LIMIT ? OFFSET ? + `, + args: params + }) + + const memories: MemoryItem[] = result.rows.map((row: any) => ({ + id: row.id as string, + memory: row.memory as string, + hash: (row.hash as string) || undefined, + metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined, + createdAt: row.created_at as string, + updatedAt: row.updated_at as string + })) + + return { + memories, + count: totalCount + } + } catch (error) { + Logger.error('List failed:', error) + return { + memories: [], + count: 0, + error: error instanceof Error ? error.message : 'Unknown error' + } + } + } + + /** + * Delete a memory (soft delete) + */ + public async delete(id: string): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + try { + // Get current memory value for history + const current = await this.db.execute({ + sql: MemoryQueries.memory.getForDelete, + args: [id] + }) + + if (current.rows.length === 0) { + throw new Error('Memory not found') + } + + const currentMemory = (current.rows[0] as any).memory as string + + // Soft delete + await this.db.execute({ + sql: MemoryQueries.memory.softDelete, + args: [new Date().toISOString(), id] + }) + + // Add to history + await this.addHistory(id, currentMemory, null, 'DELETE') + + Logger.info(`Memory deleted: ${id}`) + } catch (error) { + Logger.error('Delete failed:', error) + throw new Error(`Failed to delete memory: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Update a memory + */ + public async update(id: string, memory: string, metadata?: Record): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + try { + // Get current memory + const current = await this.db.execute({ + sql: MemoryQueries.memory.getForUpdate, + args: [id] + }) + + if (current.rows.length === 0) { + throw new Error('Memory not found') + } + + const row = current.rows[0] as any + const previousMemory = row.memory as string + const previousMetadata = row.metadata ? JSON.parse(row.metadata as string) : {} + + // Generate new hash + const hash = crypto.createHash('sha256').update(memory.trim()).digest('hex') + + // Generate new embedding if model is configured + let embedding: number[] | null = null + if (this.config?.embedderApiClient) { + try { + embedding = await this.generateEmbedding(memory) + Logger.info( + `Updated embedding with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})` + ) + } catch (error) { + Logger.error('Failed to generate embedding for update:', error) + } + } + + // Merge metadata + const mergedMetadata = { ...previousMetadata, ...metadata } + + // Update memory + await this.db.execute({ + sql: MemoryQueries.memory.update, + args: [ + memory.trim(), + hash, + embedding ? this.embeddingToVector(embedding) : null, + JSON.stringify(mergedMetadata), + new Date().toISOString(), + id + ] + }) + + // Add to history + await this.addHistory(id, previousMemory, memory, 'UPDATE') + + Logger.info(`Memory updated: ${id}`) + } catch (error) { + Logger.error('Update failed:', error) + throw new Error(`Failed to update memory: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Get memory history + */ + public async get(memoryId: string): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + try { + const result = await this.db.execute({ + sql: MemoryQueries.history.getByMemoryId, + args: [memoryId] + }) + + return result.rows.map((row: any) => ({ + id: row.id as number, + memoryId: row.memory_id as string, + previousValue: row.previous_value as string | undefined, + newValue: row.new_value as string, + action: row.action as 'ADD' | 'UPDATE' | 'DELETE', + createdAt: row.created_at as string, + updatedAt: row.updated_at as string, + isDeleted: row.is_deleted === 1 + })) + } catch (error) { + Logger.error('Get history failed:', error) + throw new Error(`Failed to get memory history: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Delete all memories for a user without deleting the user (hard delete) + */ + public async deleteAllMemoriesForUser(userId: string): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + if (!userId) { + throw new Error('User ID is required') + } + + try { + // Get count of memories to be deleted + const countResult = await this.db.execute({ + sql: MemoryQueries.users.countMemoriesForUser, + args: [userId] + }) + const totalCount = (countResult.rows[0] as any).total as number + + // Delete history entries for this user's memories + await this.db.execute({ + sql: MemoryQueries.users.deleteHistoryForUser, + args: [userId] + }) + + // Hard delete all memories for this user + await this.db.execute({ + sql: MemoryQueries.users.deleteAllMemoriesForUser, + args: [userId] + }) + + Logger.info(`Reset all memories for user ${userId} (${totalCount} memories deleted)`) + } catch (error) { + Logger.error('Reset user memories failed:', error) + throw new Error(`Failed to reset user memories: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Delete a user and all their memories (hard delete) + */ + public async deleteUser(userId: string): Promise { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + if (!userId) { + throw new Error('User ID is required') + } + + if (userId === 'default-user') { + throw new Error('Cannot delete the default user') + } + + try { + // Get count of memories to be deleted + const countResult = await this.db.execute({ + sql: `SELECT COUNT(*) as total FROM memories WHERE user_id = ?`, + args: [userId] + }) + const totalCount = (countResult.rows[0] as any).total as number + + // Delete history entries for this user's memories + await this.db.execute({ + sql: `DELETE FROM memory_history WHERE memory_id IN (SELECT id FROM memories WHERE user_id = ?)`, + args: [userId] + }) + + // Delete all memories for this user (hard delete) + await this.db.execute({ + sql: `DELETE FROM memories WHERE user_id = ?`, + args: [userId] + }) + + Logger.info(`Deleted user ${userId} and ${totalCount} memories`) + } catch (error) { + Logger.error('Delete user failed:', error) + throw new Error(`Failed to delete user: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Get list of unique user IDs with their memory counts + */ + public async getUsersList(): Promise<{ userId: string; memoryCount: number; lastMemoryDate: string }[]> { + await this.init() + if (!this.db) throw new Error('Database not initialized') + + try { + const result = await this.db.execute({ + sql: MemoryQueries.users.getUniqueUsers, + args: [] + }) + + return result.rows.map((row: any) => ({ + userId: row.user_id as string, + memoryCount: row.memory_count as number, + lastMemoryDate: row.last_memory_date as string + })) + } catch (error) { + Logger.error('Get users list failed:', error) + throw new Error(`Failed to get users list: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + /** + * Update configuration + */ + public setConfig(config: MemoryConfig): void { + this.config = config + // Reset embeddings instance when config changes + this.embeddings = null + } + + /** + * Close database connection + */ + public async close(): Promise { + if (this.db) { + await this.db.close() + this.db = null + this.isInitialized = false + } + } + + // ========== EMBEDDING OPERATIONS (Previously EmbeddingService) ========== + + /** + * Normalize embedding dimensions to unified size + */ + private normalizeEmbedding(embedding: number[]): number[] { + if (embedding.length === MemoryService.UNIFIED_DIMENSION) { + return embedding + } + + if (embedding.length < MemoryService.UNIFIED_DIMENSION) { + // Pad with zeros + return [...embedding, ...new Array(MemoryService.UNIFIED_DIMENSION - embedding.length).fill(0)] + } else { + // Truncate + return embedding.slice(0, MemoryService.UNIFIED_DIMENSION) + } + } + + /** + * Generate embedding for text + */ + private async generateEmbedding(text: string): Promise { + if (!this.config?.embedderApiClient) { + throw new Error('Embedder model not configured') + } + + try { + // Initialize embeddings instance if needed + if (!this.embeddings) { + if (!this.config.embedderApiClient) { + throw new Error('Embedder provider not configured') + } + + this.embeddings = new Embeddings({ + embedApiClient: this.config.embedderApiClient, + dimensions: this.config.embedderDimensions + }) + await this.embeddings.init() + } + + const embedding = await this.embeddings.embedQuery(text) + + // Normalize to unified dimension + return this.normalizeEmbedding(embedding) + } catch (error) { + Logger.error('Embedding generation failed:', error) + throw new Error(`Failed to generate embedding: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + // ========== VECTOR SEARCH OPERATIONS (Previously VectorSearch) ========== + + /** + * Convert embedding array to libsql vector format + */ + private embeddingToVector(embedding: number[]): string { + return `[${embedding.join(',')}]` + } + + /** + * Hybrid search combining text and vector similarity (currently vector-only) + */ + private async hybridSearch( + _: string, + queryEmbedding: number[], + options: VectorSearchOptions = {} + ): Promise { + if (!this.db) throw new Error('Database not initialized') + + const { limit = 10, threshold = 0.5, userId } = options + + try { + const queryVector = this.embeddingToVector(queryEmbedding) + + const conditions: string[] = ['m.is_deleted = 0'] + const params: any[] = [] + + // Vector search only - three vector parameters for distance, vector_similarity, and combined_score + params.push(queryVector, queryVector, queryVector) + + if (userId) { + conditions.push('m.user_id = ?') + params.push(userId) + } + + const whereClause = conditions.join(' AND ') + + const hybridQuery = `${MemoryQueries.search.hybridSearch} ${whereClause} + ) AS results + WHERE vector_similarity >= ? + ORDER BY vector_similarity DESC + LIMIT ?` + + params.push(threshold, limit) + + const result = await this.db.execute({ + sql: hybridQuery, + args: params + }) + + const memories: MemoryItem[] = result.rows.map((row: any) => ({ + id: row.id as string, + memory: row.memory as string, + hash: (row.hash as string) || undefined, + metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined, + createdAt: row.created_at as string, + updatedAt: row.updated_at as string, + score: row.vector_similarity as number + })) + + return { + memories, + count: memories.length + } + } catch (error) { + Logger.error('Hybrid search failed:', error) + throw new Error(`Hybrid search failed: ${error instanceof Error ? error.message : 'Unknown error'}`) + } + } + + // ========== HELPER METHODS ========== + + /** + * Add entry to memory history + */ + private async addHistory( + memoryId: string, + previousValue: string | null, + newValue: string | null, + action: 'ADD' | 'UPDATE' | 'DELETE' + ): Promise { + if (!this.db) throw new Error('Database not initialized') + + const now = new Date().toISOString() + await this.db.execute({ + sql: MemoryQueries.history.insert, + args: [memoryId, previousValue, newValue, action, now, now] + }) + } +} + +export default MemoryService diff --git a/src/main/services/memory/queries.ts b/src/main/services/memory/queries.ts new file mode 100644 index 0000000000..cbb1b81764 --- /dev/null +++ b/src/main/services/memory/queries.ts @@ -0,0 +1,164 @@ +/** + * SQL queries for MemoryService + * All SQL queries are centralized here for better maintainability + */ + +export const MemoryQueries = { + // Table creation queries + createTables: { + memories: ` + CREATE TABLE IF NOT EXISTS memories ( + id TEXT PRIMARY KEY, + memory TEXT NOT NULL, + hash TEXT UNIQUE, + embedding F32_BLOB(1536), -- Native vector column (1536 dimensions for OpenAI embeddings) + metadata TEXT, -- JSON string + user_id TEXT, + agent_id TEXT, + run_id TEXT, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + is_deleted INTEGER DEFAULT 0 + ) + `, + + memoryHistory: ` + CREATE TABLE IF NOT EXISTS memory_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + memory_id TEXT NOT NULL, + previous_value TEXT, + new_value TEXT, + action TEXT NOT NULL, -- ADD, UPDATE, DELETE + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + is_deleted INTEGER DEFAULT 0, + FOREIGN KEY (memory_id) REFERENCES memories (id) + ) + ` + }, + + // Index creation queries + createIndexes: { + userId: 'CREATE INDEX IF NOT EXISTS idx_memories_user_id ON memories(user_id)', + agentId: 'CREATE INDEX IF NOT EXISTS idx_memories_agent_id ON memories(agent_id)', + createdAt: 'CREATE INDEX IF NOT EXISTS idx_memories_created_at ON memories(created_at)', + hash: 'CREATE INDEX IF NOT EXISTS idx_memories_hash ON memories(hash)', + memoryHistory: 'CREATE INDEX IF NOT EXISTS idx_memory_history_memory_id ON memory_history(memory_id)', + vector: 'CREATE INDEX IF NOT EXISTS idx_memories_vector ON memories (libsql_vector_idx(embedding))' + }, + + // Memory operations + memory: { + checkExists: 'SELECT id FROM memories WHERE hash = ? AND is_deleted = 0', + + checkExistsIncludeDeleted: 'SELECT id, is_deleted FROM memories WHERE hash = ?', + + restoreDeleted: ` + UPDATE memories + SET is_deleted = 0, memory = ?, embedding = ?, metadata = ?, updated_at = ? + WHERE id = ? + `, + + insert: ` + INSERT INTO memories (id, memory, hash, embedding, metadata, user_id, agent_id, run_id, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + `, + + getForDelete: 'SELECT memory FROM memories WHERE id = ? AND is_deleted = 0', + + softDelete: 'UPDATE memories SET is_deleted = 1, updated_at = ? WHERE id = ?', + + getForUpdate: 'SELECT memory, metadata FROM memories WHERE id = ? AND is_deleted = 0', + + update: ` + UPDATE memories + SET memory = ?, hash = ?, embedding = ?, metadata = ?, updated_at = ? + WHERE id = ? + `, + + count: 'SELECT COUNT(*) as total FROM memories m WHERE', + + list: ` + SELECT + m.id, + m.memory, + m.hash, + m.metadata, + m.user_id, + m.agent_id, + m.run_id, + m.created_at, + m.updated_at + FROM memories m + WHERE + ` + }, + + // History operations + history: { + insert: ` + INSERT INTO memory_history (memory_id, previous_value, new_value, action, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?) + `, + + getByMemoryId: ` + SELECT * FROM memory_history + WHERE memory_id = ? AND is_deleted = 0 + ORDER BY created_at DESC + ` + }, + + // Search operations + search: { + hybridSearch: ` + SELECT * FROM ( + SELECT + m.id, + m.memory, + m.hash, + m.metadata, + m.user_id, + m.agent_id, + m.run_id, + m.created_at, + m.updated_at, + CASE + WHEN m.embedding IS NULL THEN 2.0 + ELSE vector_distance_cos(m.embedding, vector32(?)) + END as distance, + CASE + WHEN m.embedding IS NULL THEN 0.0 + ELSE (1 - vector_distance_cos(m.embedding, vector32(?))) + END as vector_similarity, + 0.0 as text_similarity, + ( + CASE + WHEN m.embedding IS NULL THEN 0.0 + ELSE (1 - vector_distance_cos(m.embedding, vector32(?))) + END + ) as combined_score + FROM memories m + WHERE + ` + }, + + // User operations + users: { + getUniqueUsers: ` + SELECT DISTINCT + user_id, + COUNT(*) as memory_count, + MAX(created_at) as last_memory_date + FROM memories + WHERE user_id IS NOT NULL AND is_deleted = 0 + GROUP BY user_id + ORDER BY last_memory_date DESC + `, + + countMemoriesForUser: 'SELECT COUNT(*) as total FROM memories WHERE user_id = ?', + + deleteAllMemoriesForUser: 'DELETE FROM memories WHERE user_id = ?', + + deleteHistoryForUser: 'DELETE FROM memory_history WHERE memory_id IN (SELECT id FROM memories WHERE user_id = ?)' + } +} as const diff --git a/src/main/services/remotefile/BaseFileService.ts b/src/main/services/remotefile/BaseFileService.ts new file mode 100644 index 0000000000..ff06eb0b44 --- /dev/null +++ b/src/main/services/remotefile/BaseFileService.ts @@ -0,0 +1,13 @@ +import { FileListResponse, FileMetadata, FileUploadResponse, Provider } from '@types' + +export abstract class BaseFileService { + protected readonly provider: Provider + protected constructor(provider: Provider) { + this.provider = provider + } + + abstract uploadFile(file: FileMetadata): Promise + abstract deleteFile(fileId: string): Promise + abstract listFiles(): Promise + abstract retrieveFile(fileId: string): Promise +} diff --git a/src/main/services/remotefile/FileServiceManager.ts b/src/main/services/remotefile/FileServiceManager.ts new file mode 100644 index 0000000000..9cdf6f834c --- /dev/null +++ b/src/main/services/remotefile/FileServiceManager.ts @@ -0,0 +1,41 @@ +import { Provider } from '@types' + +import { BaseFileService } from './BaseFileService' +import { GeminiService } from './GeminiService' +import { MistralService } from './MistralService' + +export class FileServiceManager { + private static instance: FileServiceManager + private services: Map = new Map() + + // eslint-disable-next-line @typescript-eslint/no-empty-function + private constructor() {} + + static getInstance(): FileServiceManager { + if (!this.instance) { + this.instance = new FileServiceManager() + } + return this.instance + } + + getService(provider: Provider): BaseFileService { + const type = provider.type + let service = this.services.get(type) + + if (!service) { + switch (type) { + case 'gemini': + service = new GeminiService(provider) + break + case 'mistral': + service = new MistralService(provider) + break + default: + throw new Error(`Unsupported service type: ${type}`) + } + this.services.set(type, service) + } + + return service + } +} diff --git a/src/main/services/remotefile/GeminiService.ts b/src/main/services/remotefile/GeminiService.ts new file mode 100644 index 0000000000..82178f5c14 --- /dev/null +++ b/src/main/services/remotefile/GeminiService.ts @@ -0,0 +1,190 @@ +import { File, Files, FileState, GoogleGenAI } from '@google/genai' +import { FileListResponse, FileMetadata, FileUploadResponse, Provider } from '@types' +import Logger from 'electron-log' +import { v4 as uuidv4 } from 'uuid' + +import { CacheService } from '../CacheService' +import { BaseFileService } from './BaseFileService' + +export class GeminiService extends BaseFileService { + private static readonly FILE_LIST_CACHE_KEY = 'gemini_file_list' + private static readonly FILE_CACHE_DURATION = 48 * 60 * 60 * 1000 + private static readonly LIST_CACHE_DURATION = 3000 + + protected readonly fileManager: Files + + constructor(provider: Provider) { + super(provider) + this.fileManager = new GoogleGenAI({ + vertexai: false, + apiKey: provider.apiKey, + httpOptions: { + baseUrl: provider.apiHost + } + }).files + } + + async uploadFile(file: FileMetadata): Promise { + try { + const uploadResult = await this.fileManager.upload({ + file: file.path, + config: { + mimeType: 'application/pdf', + name: file.id, + displayName: file.origin_name + } + }) + + // 根据文件状态设置响应状态 + let status: 'success' | 'processing' | 'failed' | 'unknown' + switch (uploadResult.state) { + case FileState.ACTIVE: + status = 'success' + break + case FileState.PROCESSING: + status = 'processing' + break + case FileState.FAILED: + status = 'failed' + break + default: + status = 'unknown' + } + + const response: FileUploadResponse = { + fileId: uploadResult.name || '', + displayName: file.origin_name, + status, + originalFile: { + type: 'gemini', + file: uploadResult + } + } + + // 只缓存成功的文件 + if (status === 'success') { + const cacheKey = `${GeminiService.FILE_LIST_CACHE_KEY}_${response.fileId}` + CacheService.set(cacheKey, response, GeminiService.FILE_CACHE_DURATION) + } + + return response + } catch (error) { + Logger.error('Error uploading file to Gemini:', error) + return { + fileId: '', + displayName: file.origin_name, + status: 'failed', + originalFile: undefined + } + } + } + + async retrieveFile(fileId: string): Promise { + try { + const cachedResponse = CacheService.get(`${GeminiService.FILE_LIST_CACHE_KEY}_${fileId}`) + Logger.info('[GeminiService] cachedResponse', cachedResponse) + if (cachedResponse) { + return cachedResponse + } + const files: File[] = [] + + for await (const f of await this.fileManager.list()) { + files.push(f) + } + Logger.info('[GeminiService] files', files) + const file = files + .filter((file) => file.state === FileState.ACTIVE) + .find((file) => file.name?.substring(6) === fileId) // 去掉 files/ 前缀 + Logger.info('[GeminiService] file', file) + if (file) { + return { + fileId: fileId, + displayName: file.displayName || '', + status: 'success', + originalFile: { + type: 'gemini', + file + } + } + } + + return { + fileId: fileId, + displayName: '', + status: 'failed', + originalFile: undefined + } + } catch (error) { + Logger.error('Error retrieving file from Gemini:', error) + return { + fileId: fileId, + displayName: '', + status: 'failed', + originalFile: undefined + } + } + } + + async listFiles(): Promise { + try { + const cachedList = CacheService.get(GeminiService.FILE_LIST_CACHE_KEY) + if (cachedList) { + return cachedList + } + const geminiFiles: File[] = [] + + for await (const f of await this.fileManager.list()) { + geminiFiles.push(f) + } + const fileList: FileListResponse = { + files: geminiFiles + .filter((file) => file.state === FileState.ACTIVE) + .map((file) => { + // 更新单个文件的缓存 + const fileResponse: FileUploadResponse = { + fileId: file.name || uuidv4(), + displayName: file.displayName || '', + status: 'success', + originalFile: { + type: 'gemini', + file + } + } + CacheService.set( + `${GeminiService.FILE_LIST_CACHE_KEY}_${file.name}`, + fileResponse, + GeminiService.FILE_CACHE_DURATION + ) + + return { + id: file.name || uuidv4(), + displayName: file.displayName || '', + size: Number(file.sizeBytes), + status: 'success', + originalFile: { + type: 'gemini', + file + } + } + }) + } + + // 更新文件列表缓存 + CacheService.set(GeminiService.FILE_LIST_CACHE_KEY, fileList, GeminiService.LIST_CACHE_DURATION) + return fileList + } catch (error) { + Logger.error('Error listing files from Gemini:', error) + return { files: [] } + } + } + + async deleteFile(fileId: string): Promise { + try { + await this.fileManager.delete({ name: fileId }) + Logger.info(`File ${fileId} deleted from Gemini`) + } catch (error) { + Logger.error('Error deleting file from Gemini:', error) + throw error + } + } +} diff --git a/src/main/services/remotefile/MistralService.ts b/src/main/services/remotefile/MistralService.ts new file mode 100644 index 0000000000..3964871ce4 --- /dev/null +++ b/src/main/services/remotefile/MistralService.ts @@ -0,0 +1,104 @@ +import fs from 'node:fs/promises' + +import { Mistral } from '@mistralai/mistralai' +import { FileListResponse, FileMetadata, FileUploadResponse, Provider } from '@types' +import Logger from 'electron-log' + +import { MistralClientManager } from '../MistralClientManager' +import { BaseFileService } from './BaseFileService' + +export class MistralService extends BaseFileService { + private readonly client: Mistral + + constructor(provider: Provider) { + super(provider) + const clientManager = MistralClientManager.getInstance() + clientManager.initializeClient(provider) + this.client = clientManager.getClient() + } + + async uploadFile(file: FileMetadata): Promise { + try { + const fileBuffer = await fs.readFile(file.path) + const response = await this.client.files.upload({ + file: { + fileName: file.origin_name, + content: new Uint8Array(fileBuffer) + }, + purpose: 'ocr' + }) + + return { + fileId: response.id, + displayName: file.origin_name, + status: 'success', + originalFile: { + type: 'mistral', + file: response + } + } + } catch (error) { + Logger.error('Error uploading file:', error) + return { + fileId: '', + displayName: file.origin_name, + status: 'failed' + } + } + } + + async listFiles(): Promise { + try { + const response = await this.client.files.list({}) + return { + files: response.data.map((file) => ({ + id: file.id, + displayName: file.filename || '', + size: file.sizeBytes, + status: 'success', // All listed files are processed, + originalFile: { + type: 'mistral', + file + } + })) + } + } catch (error) { + Logger.error('Error listing files:', error) + return { files: [] } + } + } + + async deleteFile(fileId: string): Promise { + try { + await this.client.files.delete({ + fileId + }) + Logger.info(`File ${fileId} deleted`) + } catch (error) { + Logger.error('Error deleting file:', error) + throw error + } + } + + async retrieveFile(fileId: string): Promise { + try { + const response = await this.client.files.retrieve({ + fileId + }) + + return { + fileId: response.id, + displayName: response.filename || '', + status: 'success' // Retrieved files are always processed + } + } catch (error) { + Logger.error('Error retrieving file:', error) + return { + fileId: fileId, + displayName: '', + status: 'failed', + originalFile: undefined + } + } + } +} diff --git a/src/main/services/urlschema/handle-providers.ts b/src/main/services/urlschema/handle-providers.ts index bc109437e6..f8b0661370 100644 --- a/src/main/services/urlschema/handle-providers.ts +++ b/src/main/services/urlschema/handle-providers.ts @@ -1,37 +1,72 @@ -import { IpcChannel } from '@shared/IpcChannel' +import { isMac } from '@main/constant' import Logger from 'electron-log' import { windowService } from '../WindowService' -export function handleProvidersProtocolUrl(url: URL) { - const params = new URLSearchParams(url.search) +function ParseData(data: string) { + try { + const result = JSON.parse(Buffer.from(data, 'base64').toString('utf-8')) + + return JSON.stringify(result) + } catch (error) { + Logger.error('ParseData error:', { error }) + return null + } +} + +export async function handleProvidersProtocolUrl(url: URL) { switch (url.pathname) { case '/api-keys': { // jsonConfig example: // { // "id": "tokenflux", // "baseUrl": "https://tokenflux.ai/v1", - // "apiKey": "sk-xxxx" + // "apiKey": "sk-xxxx", + // "name": "TokenFlux", // optional + // "type": "openai" // optional // } - // cherrystudio://providers/api-keys?data={base64Encode(JSON.stringify(jsonConfig))} - const data = params.get('data') - if (data) { - const stringify = Buffer.from(data, 'base64').toString('utf8') - Logger.info('get api keys from urlschema: ', stringify) - const jsonConfig = JSON.parse(stringify) - Logger.info('get api keys from urlschema: ', jsonConfig) - const mainWindow = windowService.getMainWindow() - if (mainWindow && !mainWindow.isDestroyed()) { - mainWindow.webContents.send(IpcChannel.Provider_AddKey, jsonConfig) - mainWindow.webContents.executeJavaScript(`window.navigate('/settings/provider?id=${jsonConfig.id}')`) + // cherrystudio://providers/api-keys?v=1&data={base64Encode(JSON.stringify(jsonConfig))} + + // replace + and / to _ and - because + and / are processed by URLSearchParams + const processedSearch = url.search.replaceAll('+', '_').replaceAll('/', '-') + const params = new URLSearchParams(processedSearch) + const data = ParseData(params.get('data')?.replaceAll('_', '+').replaceAll('-', '/') || '') + + if (!data) { + Logger.error('handleProvidersProtocolUrl data is null or invalid') + return + } + + const mainWindow = windowService.getMainWindow() + const version = params.get('v') + if (version == '1') { + // TODO: handle different version + Logger.info('handleProvidersProtocolUrl', { data, version }) + } + + // add check there is window.navigate function in mainWindow + if ( + mainWindow && + !mainWindow.isDestroyed() && + (await mainWindow.webContents.executeJavaScript(`typeof window.navigate === 'function'`)) + ) { + mainWindow.webContents.executeJavaScript( + `window.navigate('/settings/provider?addProviderData=${encodeURIComponent(data)}')` + ) + + if (isMac) { + windowService.showMainWindow() } } else { - Logger.error('No data found in URL') + setTimeout(() => { + Logger.info('handleProvidersProtocolUrl timeout', { data, version }) + handleProvidersProtocolUrl(url) + }, 1000) } break } default: - console.error(`Unknown MCP protocol URL: ${url}`) + Logger.error(`Unknown MCP protocol URL: ${url}`) break } } diff --git a/src/main/services/urlschema/mcp-install.ts b/src/main/services/urlschema/mcp-install.ts index e5f0a76501..f2e58eef2a 100644 --- a/src/main/services/urlschema/mcp-install.ts +++ b/src/main/services/urlschema/mcp-install.ts @@ -44,7 +44,9 @@ export function handleMcpProtocolUrl(url: URL) { // } // } // cherrystudio://mcp/install?servers={base64Encode(JSON.stringify(jsonConfig))} + const data = params.get('servers') + if (data) { const stringify = Buffer.from(data, 'base64').toString('utf8') Logger.info('install MCP servers from urlschema: ', stringify) @@ -63,10 +65,8 @@ export function handleMcpProtocolUrl(url: URL) { } } - const mainWindow = windowService.getMainWindow() - if (mainWindow && !mainWindow.isDestroyed()) { - mainWindow.webContents.executeJavaScript("window.navigate('/settings/mcp')") - } + windowService.getMainWindow()?.show() + break } default: diff --git a/src/main/utils/__tests__/file.test.ts b/src/main/utils/__tests__/file.test.ts index 14f4801524..fbd734fd3d 100644 --- a/src/main/utils/__tests__/file.test.ts +++ b/src/main/utils/__tests__/file.test.ts @@ -1,14 +1,19 @@ import * as fs from 'node:fs' +import * as fsPromises from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { FileTypes } from '@types' +import iconv from 'iconv-lite' +import { detectAll as detectEncodingAll } from 'jschardet' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { readTextFileWithAutoEncoding } from '../file' import { getAllFiles, getAppConfigDir, getConfigDir, getFilesDir, getFileType, getTempDir } from '../file' // Mock dependencies vi.mock('node:fs') +vi.mock('node:fs/promises') vi.mock('node:os') vi.mock('node:path') vi.mock('uuid', () => ({ @@ -241,4 +246,54 @@ describe('file', () => { expect(appConfigDir).toBe('/mock/home/.cherrystudio/config/') }) }) + + describe('readTextFileWithAutoEncoding', () => { + const mockFilePath = '/path/to/mock/file.txt' + + it('should read file with auto encoding', async () => { + const content = '这是一段GB2312编码的测试内容' + const buffer = iconv.encode(content, 'GB2312') + + // 创建模拟的 FileHandle 对象 + const mockFileHandle = { + read: vi.fn().mockResolvedValue({ + bytesRead: buffer.byteLength, + buffer: buffer + }), + close: vi.fn().mockResolvedValue(undefined) + } + + // 模拟 open 方法 + vi.spyOn(fsPromises, 'open').mockResolvedValue(mockFileHandle as any) + vi.spyOn(fsPromises, 'readFile').mockResolvedValue(buffer) + + const result = await readTextFileWithAutoEncoding(mockFilePath) + expect(result).toBe(content) + }) + + it('should try to fix bad detected encoding', async () => { + const content = '这是一段GB2312编码的测试内容' + const buffer = iconv.encode(content, 'GB2312') + + // 创建模拟的 FileHandle 对象 + const mockFileHandle = { + read: vi.fn().mockResolvedValue({ + bytesRead: buffer.byteLength, + buffer: buffer + }), + close: vi.fn().mockResolvedValue(undefined) + } + + // 模拟 fs.open 方法 + vi.spyOn(fsPromises, 'open').mockResolvedValue(mockFileHandle as any) + vi.spyOn(fsPromises, 'readFile').mockResolvedValue(buffer) + vi.mocked(vi.fn(detectEncodingAll)).mockReturnValue([ + { encoding: 'UTF-8', confidence: 0.9 }, + { encoding: 'GB2312', confidence: 0.8 } + ]) + + const result = await readTextFileWithAutoEncoding(mockFilePath) + expect(result).toBe(content) + }) + }) }) diff --git a/src/main/utils/file.ts b/src/main/utils/file.ts index 177a28a90f..d03af3ad72 100644 --- a/src/main/utils/file.ts +++ b/src/main/utils/file.ts @@ -1,11 +1,15 @@ import * as fs from 'node:fs' +import { open, readFile } from 'node:fs/promises' import os from 'node:os' import path from 'node:path' -import { isPortable } from '@main/constant' -import { audioExts, documentExts, imageExts, textExts, videoExts } from '@shared/config/constant' -import { FileType, FileTypes } from '@types' +import { isLinux, isPortable } from '@main/constant' +import { audioExts, documentExts, imageExts, MB, textExts, videoExts } from '@shared/config/constant' +import { FileMetadata, FileTypes } from '@types' import { app } from 'electron' +import Logger from 'electron-log' +import iconv from 'iconv-lite' +import * as jschardet from 'jschardet' import { v4 as uuidv4 } from 'uuid' export function initAppDataDir() { @@ -59,6 +63,13 @@ function getAppDataPathFromConfig() { return null } + let executablePath = app.getPath('exe') + if (isLinux && process.env.APPIMAGE) { + // 如果是 AppImage 打包的应用,直接使用 APPIMAGE 环境变量 + // 这样可以确保获取到正确的可执行文件路径 + executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage') + } + let appDataPath = null // 兼容旧版本 if (config.appDataPath && typeof config.appDataPath === 'string') { @@ -67,7 +78,7 @@ function getAppDataPathFromConfig() { appDataPath && updateAppDataConfig(appDataPath) } else { appDataPath = config.appDataPath.find( - (item: { executablePath: string }) => item.executablePath === app.getPath('exe') + (item: { executablePath: string }) => item.executablePath === executablePath )?.dataPath } @@ -90,11 +101,13 @@ export function updateAppDataConfig(appDataPath: string) { // config.json // appDataPath: [{ executablePath: string, dataPath: string }] const configPath = path.join(getConfigDir(), 'config.json') + let executablePath = app.getPath('exe') + if (isLinux && process.env.APPIMAGE) { + executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage') + } + if (!fs.existsSync(configPath)) { - fs.writeFileSync( - configPath, - JSON.stringify({ appDataPath: [{ executablePath: app.getPath('exe'), dataPath: appDataPath }] }, null, 2) - ) + fs.writeFileSync(configPath, JSON.stringify({ appDataPath: [{ executablePath, dataPath: appDataPath }] }, null, 2)) return } @@ -104,13 +117,13 @@ export function updateAppDataConfig(appDataPath: string) { } const existingPath = config.appDataPath.find( - (item: { executablePath: string }) => item.executablePath === app.getPath('exe') + (item: { executablePath: string }) => item.executablePath === executablePath ) if (existingPath) { existingPath.dataPath = appDataPath } else { - config.appDataPath.push({ executablePath: app.getPath('exe'), dataPath: appDataPath }) + config.appDataPath.push({ executablePath, dataPath: appDataPath }) } fs.writeFileSync(configPath, JSON.stringify(config, null, 2)) @@ -121,7 +134,19 @@ export function getFileType(ext: string): FileTypes { return fileTypeMap.get(ext) || FileTypes.OTHER } -export function getAllFiles(dirPath: string, arrayOfFiles: FileType[] = []): FileType[] { +export function getFileDir(filePath: string) { + return path.dirname(filePath) +} + +export function getFileName(filePath: string) { + return path.basename(filePath) +} + +export function getFileExt(filePath: string) { + return path.extname(filePath) +} + +export function getAllFiles(dirPath: string, arrayOfFiles: FileMetadata[] = []): FileMetadata[] { const files = fs.readdirSync(dirPath) files.forEach((file) => { @@ -143,7 +168,7 @@ export function getAllFiles(dirPath: string, arrayOfFiles: FileType[] = []): Fil const name = path.basename(file) const size = fs.statSync(fullPath).size - const fileItem: FileType = { + const fileItem: FileMetadata = { id: uuidv4(), name, path: fullPath, @@ -181,3 +206,53 @@ export function getCacheDir() { export function getAppConfigDir(name: string) { return path.join(getConfigDir(), name) } + +export function getMcpDir() { + return path.join(os.homedir(), '.cherrystudio', 'mcp') +} + +/** + * 读取文件内容并自动检测编码格式进行解码 + * @param filePath - 文件路径 + * @returns 解码后的文件内容 + */ +export async function readTextFileWithAutoEncoding(filePath: string): Promise { + // 读取前1MB以检测编码 + const buffer = Buffer.alloc(1 * MB) + const fh = await open(filePath, 'r') + const { buffer: bufferRead } = await fh.read(buffer, 0, 1 * MB, 0) + await fh.close() + + // 获取文件编码格式,最多取前两个可能的编码 + const encodings = jschardet + .detectAll(bufferRead) + .map((item) => ({ + ...item, + encoding: item.encoding === 'ascii' ? 'UTF-8' : item.encoding + })) + .filter((item, index, array) => array.findIndex((prevItem) => prevItem.encoding === item.encoding) === index) + .slice(0, 2) + + if (encodings.length === 0) { + Logger.error('Failed to detect encoding. Use utf-8 to decode.') + const data = await readFile(filePath) + return iconv.decode(data, 'UTF-8') + } + + const data = await readFile(filePath) + + for (const item of encodings) { + const encoding = item.encoding + const content = iconv.decode(data, encoding) + if (content.includes('\uFFFD')) { + Logger.error( + `File ${filePath} was auto-detected as ${encoding} encoding, but contains invalid characters. Trying other encodings` + ) + } else { + return content + } + } + + Logger.error(`File ${filePath} failed to decode with all possible encodings, trying UTF-8 encoding`) + return iconv.decode(data, 'UTF-8') +} diff --git a/src/main/utils/process.ts b/src/main/utils/process.ts index 36a0d731bb..b83f8a8b26 100644 --- a/src/main/utils/process.ts +++ b/src/main/utils/process.ts @@ -49,7 +49,7 @@ export async function getBinaryPath(name?: string): Promise { const binaryName = await getBinaryName(name) const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin') - const binariesDirExists = await fs.existsSync(binariesDir) + const binariesDirExists = fs.existsSync(binariesDir) return binariesDirExists ? path.join(binariesDir, binaryName) : binaryName } diff --git a/src/main/utils/systemInfo.ts b/src/main/utils/systemInfo.ts new file mode 100644 index 0000000000..84db4efed7 --- /dev/null +++ b/src/main/utils/systemInfo.ts @@ -0,0 +1,92 @@ +import { app } from 'electron' +import macosRelease from 'macos-release' +import os from 'os' + +/** + * System information interface + */ +export interface SystemInfo { + platform: NodeJS.Platform + arch: string + osRelease: string + appVersion: string + osString: string + archString: string +} + +/** + * Get basic system constants for quick access + * @returns {Object} Basic system constants + */ +export function getSystemConstants() { + return { + platform: process.platform, + arch: process.arch, + osRelease: os.release(), + appVersion: app.getVersion() + } +} + +/** + * Get system information + * @returns {SystemInfo} Complete system information object + */ +export function getSystemInfo(): SystemInfo { + const platform = process.platform + const arch = process.arch + const osRelease = os.release() + const appVersion = app.getVersion() + + let osString = '' + + switch (platform) { + case 'win32': { + // Get Windows version + const parts = osRelease.split('.') + const buildNumber = parseInt(parts[2], 10) + osString = buildNumber >= 22000 ? 'Windows 11' : 'Windows 10' + break + } + case 'darwin': { + // macOS version handling using macos-release for better accuracy + try { + const macVersionInfo = macosRelease() + const versionString = macVersionInfo.version.replace(/\./g, '_') // 15.0.0 -> 15_0_0 + osString = arch === 'arm64' ? `Mac OS X ${versionString}` : `Intel Mac OS X ${versionString}` // Mac OS X 15_0_0 + } catch (error) { + // Fallback to original logic if macos-release fails + const macVersion = osRelease.split('.').slice(0, 2).join('_') + osString = arch === 'arm64' ? `Mac OS X ${macVersion}` : `Intel Mac OS X ${macVersion}` + } + break + } + case 'linux': { + osString = `Linux ${arch}` + break + } + default: { + osString = `${platform} ${arch}` + } + } + + const archString = arch === 'x64' ? 'x86_64' : arch === 'arm64' ? 'arm64' : arch + + return { + platform, + arch, + osRelease, + appVersion, + osString, + archString + } +} + +/** + * Generate User-Agent string based on user system data + * @returns {string} Dynamically generated User-Agent string + */ +export function generateUserAgent(): string { + const systemInfo = getSystemInfo() + + return `Mozilla/5.0 (${systemInfo.osString}; ${systemInfo.archString}) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/${systemInfo.appVersion} Chrome/124.0.0.0 Safari/537.36` +} diff --git a/src/main/utils/zoom.ts b/src/main/utils/zoom.ts index 8e7145313c..d91d411591 100644 --- a/src/main/utils/zoom.ts +++ b/src/main/utils/zoom.ts @@ -1,26 +1,26 @@ -import { BrowserWindow } from 'electron' - -import { configManager } from '../services/ConfigManager' - -export function handleZoomFactor(wins: BrowserWindow[], delta: number, reset: boolean = false) { - if (reset) { - wins.forEach((win) => { - win.webContents.setZoomFactor(1) - }) - configManager.setZoomFactor(1) - return - } - - if (delta === 0) { - return - } - - const currentZoom = configManager.getZoomFactor() - const newZoom = Number((currentZoom + delta).toFixed(1)) - if (newZoom >= 0.5 && newZoom <= 2.0) { - wins.forEach((win) => { - win.webContents.setZoomFactor(newZoom) - }) - configManager.setZoomFactor(newZoom) - } -} +import { BrowserWindow } from 'electron' + +import { configManager } from '../services/ConfigManager' + +export function handleZoomFactor(wins: BrowserWindow[], delta: number, reset: boolean = false) { + if (reset) { + wins.forEach((win) => { + win.webContents.setZoomFactor(1) + }) + configManager.setZoomFactor(1) + return + } + + if (delta === 0) { + return + } + + const currentZoom = configManager.getZoomFactor() + const newZoom = Number((currentZoom + delta).toFixed(1)) + if (newZoom >= 0.5 && newZoom <= 2.0) { + wins.forEach((win) => { + win.webContents.setZoomFactor(newZoom) + }) + configManager.setZoomFactor(newZoom) + } +} diff --git a/src/preload/index.ts b/src/preload/index.ts index f6e49ece10..ab27e37ebc 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -3,10 +3,18 @@ import { electronAPI } from '@electron-toolkit/preload' import { UpgradeChannel } from '@shared/config/constant' import { IpcChannel } from '@shared/IpcChannel' import { - FileType, + AddMemoryOptions, + AssistantMessage, + FileListResponse, + FileMetadata, + FileUploadResponse, KnowledgeBaseParams, KnowledgeItem, MCPServer, + MemoryConfig, + MemoryListOptions, + MemorySearchOptions, + Provider, S3Config, Shortcut, ThemeMode, @@ -51,6 +59,10 @@ const api = { openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url), getCacheSize: () => ipcRenderer.invoke(IpcChannel.App_GetCacheSize), clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache), + mac: { + isProcessTrusted: (): Promise => ipcRenderer.invoke(IpcChannel.App_MacIsProcessTrusted), + requestProcessTrust: (): Promise => ipcRenderer.invoke(IpcChannel.App_MacRequestProcessTrust) + }, notification: { send: (notification: Notification) => ipcRenderer.invoke(IpcChannel.Notification_Send, notification) }, @@ -66,9 +78,9 @@ const api = { decompress: (text: Buffer) => ipcRenderer.invoke(IpcChannel.Zip_Decompress, text) }, backup: { - backup: (fileName: string, data: string, destinationPath?: string, skipBackupFile?: boolean) => - ipcRenderer.invoke(IpcChannel.Backup_Backup, fileName, data, destinationPath, skipBackupFile), - restore: (backupPath: string) => ipcRenderer.invoke(IpcChannel.Backup_Restore, backupPath), + backup: (filename: string, content: string, path: string, skipBackupFile: boolean) => + ipcRenderer.invoke(IpcChannel.Backup_Backup, filename, content, path, skipBackupFile), + restore: (path: string) => ipcRenderer.invoke(IpcChannel.Backup_Restore, path), backupToWebdav: (data: string, webdavConfig: WebDavConfig) => ipcRenderer.invoke(IpcChannel.Backup_BackupToWebdav, data, webdavConfig), restoreFromWebdav: (webdavConfig: WebDavConfig) => @@ -81,6 +93,21 @@ const api = { ipcRenderer.invoke(IpcChannel.Backup_CreateDirectory, webdavConfig, path, options), deleteWebdavFile: (fileName: string, webdavConfig: WebDavConfig) => ipcRenderer.invoke(IpcChannel.Backup_DeleteWebdavFile, fileName, webdavConfig), + backupToLocalDir: ( + data: string, + fileName: string, + localConfig: { localBackupDir?: string; skipBackupFile?: boolean } + ) => ipcRenderer.invoke(IpcChannel.Backup_BackupToLocalDir, data, fileName, localConfig), + restoreFromLocalBackup: (fileName: string, localBackupDir?: string) => + ipcRenderer.invoke(IpcChannel.Backup_RestoreFromLocalBackup, fileName, localBackupDir), + listLocalBackupFiles: (localBackupDir?: string) => + ipcRenderer.invoke(IpcChannel.Backup_ListLocalBackupFiles, localBackupDir), + deleteLocalBackupFile: (fileName: string, localBackupDir?: string) => + ipcRenderer.invoke(IpcChannel.Backup_DeleteLocalBackupFile, fileName, localBackupDir), + setLocalBackupDir: (dirPath: string) => ipcRenderer.invoke(IpcChannel.Backup_SetLocalBackupDir, dirPath), + checkWebdavConnection: (webdavConfig: WebDavConfig) => + ipcRenderer.invoke(IpcChannel.Backup_CheckConnection, webdavConfig), + backupToS3: (data: string, s3Config: S3Config) => ipcRenderer.invoke(IpcChannel.Backup_BackupToS3, data, s3Config), restoreFromS3: (s3Config: S3Config) => ipcRenderer.invoke(IpcChannel.Backup_RestoreFromS3, s3Config), listS3Files: (s3Config: S3Config) => ipcRenderer.invoke(IpcChannel.Backup_ListS3Files, s3Config), @@ -90,13 +117,26 @@ const api = { }, file: { select: (options?: OpenDialogOptions) => ipcRenderer.invoke(IpcChannel.File_Select, options), - upload: (file: FileType) => ipcRenderer.invoke(IpcChannel.File_Upload, file), + upload: (file: FileMetadata) => ipcRenderer.invoke(IpcChannel.File_Upload, file), delete: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Delete, fileId), - read: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Read, fileId), + deleteDir: (dirPath: string) => ipcRenderer.invoke(IpcChannel.File_DeleteDir, dirPath), + read: (fileId: string, detectEncoding?: boolean) => + ipcRenderer.invoke(IpcChannel.File_Read, fileId, detectEncoding), clear: () => ipcRenderer.invoke(IpcChannel.File_Clear), get: (filePath: string) => ipcRenderer.invoke(IpcChannel.File_Get, filePath), - create: (fileName: string) => ipcRenderer.invoke(IpcChannel.File_Create, fileName), + /** + * 创建一个空的临时文件 + * @param fileName 文件名 + * @returns 临时文件路径 + */ + createTempFile: (fileName: string): Promise => ipcRenderer.invoke(IpcChannel.File_CreateTempFile, fileName), + /** + * 写入文件 + * @param filePath 文件路径 + * @param data 数据 + */ write: (filePath: string, data: Uint8Array | string) => ipcRenderer.invoke(IpcChannel.File_Write, filePath, data), + writeWithId: (id: string, content: string) => ipcRenderer.invoke(IpcChannel.File_WriteWithId, id, content), open: (options?: OpenDialogOptions) => ipcRenderer.invoke(IpcChannel.File_Open, options), openPath: (path: string) => ipcRenderer.invoke(IpcChannel.File_OpenPath, path), @@ -104,15 +144,16 @@ const api = { ipcRenderer.invoke(IpcChannel.File_Save, path, content, options), selectFolder: () => ipcRenderer.invoke(IpcChannel.File_SelectFolder), saveImage: (name: string, data: string) => ipcRenderer.invoke(IpcChannel.File_SaveImage, name, data), + binaryImage: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_BinaryImage, fileId), base64Image: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Base64Image, fileId), saveBase64Image: (data: string) => ipcRenderer.invoke(IpcChannel.File_SaveBase64Image, data), download: (url: string, isUseContentType?: boolean) => ipcRenderer.invoke(IpcChannel.File_Download, url, isUseContentType), copy: (fileId: string, destPath: string) => ipcRenderer.invoke(IpcChannel.File_Copy, fileId, destPath), - binaryImage: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_BinaryImage, fileId), base64File: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Base64File, fileId), pdfInfo: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_GetPdfInfo, fileId), - getPathForFile: (file: File) => webUtils.getPathForFile(file) + getPathForFile: (file: File) => webUtils.getPathForFile(file), + openFileWithRelativePath: (file: FileMetadata) => ipcRenderer.invoke(IpcChannel.File_OpenWithRelativePath, file) }, fs: { read: (pathOrUrl: string, encoding?: BufferEncoding) => ipcRenderer.invoke(IpcChannel.Fs_Read, pathOrUrl, encoding) @@ -131,31 +172,54 @@ const api = { add: ({ base, item, + userId, forceReload = false }: { base: KnowledgeBaseParams item: KnowledgeItem + userId?: string forceReload?: boolean - }) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Add, { base, item, forceReload }), + }) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Add, { base, item, forceReload, userId }), remove: ({ uniqueId, uniqueIds, base }: { uniqueId: string; uniqueIds: string[]; base: KnowledgeBaseParams }) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Remove, { uniqueId, uniqueIds, base }), search: ({ search, base }: { search: string; base: KnowledgeBaseParams }) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Search, { search, base }), rerank: ({ search, base, results }: { search: string; base: KnowledgeBaseParams; results: ExtractChunkData[] }) => - ipcRenderer.invoke(IpcChannel.KnowledgeBase_Rerank, { search, base, results }) + ipcRenderer.invoke(IpcChannel.KnowledgeBase_Rerank, { search, base, results }), + checkQuota: ({ base, userId }: { base: KnowledgeBaseParams; userId: string }) => + ipcRenderer.invoke(IpcChannel.KnowledgeBase_Check_Quota, base, userId) + }, + memory: { + add: (messages: string | AssistantMessage[], options?: AddMemoryOptions) => + ipcRenderer.invoke(IpcChannel.Memory_Add, messages, options), + search: (query: string, options: MemorySearchOptions) => + ipcRenderer.invoke(IpcChannel.Memory_Search, query, options), + list: (options?: MemoryListOptions) => ipcRenderer.invoke(IpcChannel.Memory_List, options), + delete: (id: string) => ipcRenderer.invoke(IpcChannel.Memory_Delete, id), + update: (id: string, memory: string, metadata?: Record) => + ipcRenderer.invoke(IpcChannel.Memory_Update, id, memory, metadata), + get: (id: string) => ipcRenderer.invoke(IpcChannel.Memory_Get, id), + setConfig: (config: MemoryConfig) => ipcRenderer.invoke(IpcChannel.Memory_SetConfig, config), + deleteUser: (userId: string) => ipcRenderer.invoke(IpcChannel.Memory_DeleteUser, userId), + deleteAllMemoriesForUser: (userId: string) => + ipcRenderer.invoke(IpcChannel.Memory_DeleteAllMemoriesForUser, userId), + getUsersList: () => ipcRenderer.invoke(IpcChannel.Memory_GetUsersList) }, window: { setMinimumSize: (width: number, height: number) => ipcRenderer.invoke(IpcChannel.Windows_SetMinimumSize, width, height), resetMinimumSize: () => ipcRenderer.invoke(IpcChannel.Windows_ResetMinimumSize) }, - gemini: { - uploadFile: (file: FileType, { apiKey, baseURL }: { apiKey: string; baseURL: string }) => - ipcRenderer.invoke(IpcChannel.Gemini_UploadFile, file, { apiKey, baseURL }), - base64File: (file: FileType) => ipcRenderer.invoke(IpcChannel.Gemini_Base64File, file), - retrieveFile: (file: FileType, apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_RetrieveFile, file, apiKey), - listFiles: (apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_ListFiles, apiKey), - deleteFile: (fileId: string, apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_DeleteFile, fileId, apiKey) + fileService: { + upload: (provider: Provider, file: FileMetadata): Promise => + ipcRenderer.invoke(IpcChannel.FileService_Upload, provider, file), + list: (provider: Provider): Promise => ipcRenderer.invoke(IpcChannel.FileService_List, provider), + delete: (provider: Provider, fileId: string) => ipcRenderer.invoke(IpcChannel.FileService_Delete, provider, fileId), + retrieve: (provider: Provider, fileId: string): Promise => + ipcRenderer.invoke(IpcChannel.FileService_Retrieve, provider, fileId) + }, + selectionMenu: { + action: (action: string) => ipcRenderer.invoke('selection-menu:action', action) }, vertexAI: { @@ -187,8 +251,8 @@ const api = { restartServer: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_RestartServer, server), stopServer: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_StopServer, server), listTools: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_ListTools, server), - callTool: ({ server, name, args }: { server: MCPServer; name: string; args: any }) => - ipcRenderer.invoke(IpcChannel.Mcp_CallTool, { server, name, args }), + callTool: ({ server, name, args, callId }: { server: MCPServer; name: string; args: any; callId?: string }) => + ipcRenderer.invoke(IpcChannel.Mcp_CallTool, { server, name, args, callId }), listPrompts: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_ListPrompts, server), getPrompt: ({ server, name, args }: { server: MCPServer; name: string; args?: Record }) => ipcRenderer.invoke(IpcChannel.Mcp_GetPrompt, { server, name, args }), @@ -196,7 +260,14 @@ const api = { getResource: ({ server, uri }: { server: MCPServer; uri: string }) => ipcRenderer.invoke(IpcChannel.Mcp_GetResource, { server, uri }), getInstallInfo: () => ipcRenderer.invoke(IpcChannel.Mcp_GetInstallInfo), - checkMcpConnectivity: (server: any) => ipcRenderer.invoke(IpcChannel.Mcp_CheckConnectivity, server) + checkMcpConnectivity: (server: any) => ipcRenderer.invoke(IpcChannel.Mcp_CheckConnectivity, server), + uploadDxt: async (file: File) => { + const buffer = await file.arrayBuffer() + return ipcRenderer.invoke(IpcChannel.Mcp_UploadDxt, buffer, file.name) + }, + abortTool: (callId: string) => ipcRenderer.invoke(IpcChannel.Mcp_AbortTool, callId), + setProgress: (progress: number) => ipcRenderer.invoke(IpcChannel.Mcp_SetProgress, progress), + getServerVersion: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_GetServerVersion, server) }, python: { execute: (script: string, context?: Record, timeout?: number) => @@ -266,12 +337,15 @@ const api = { ipcRenderer.invoke(IpcChannel.Selection_SetRemeberWinSize, isRemeberWinSize), setFilterMode: (filterMode: string) => ipcRenderer.invoke(IpcChannel.Selection_SetFilterMode, filterMode), setFilterList: (filterList: string[]) => ipcRenderer.invoke(IpcChannel.Selection_SetFilterList, filterList), - processAction: (actionItem: ActionItem) => ipcRenderer.invoke(IpcChannel.Selection_ProcessAction, actionItem), + processAction: (actionItem: ActionItem, isFullScreen: boolean = false) => + 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) }, - quoteToMainWindow: (text: string) => ipcRenderer.invoke(IpcChannel.App_QuoteToMain, text) + quoteToMainWindow: (text: string) => ipcRenderer.invoke(IpcChannel.App_QuoteToMain, text), + setDisableHardwareAcceleration: (isDisable: boolean) => + ipcRenderer.invoke(IpcChannel.App_SetDisableHardwareAcceleration, isDisable) } // Use `contextBridge` APIs to expose Electron APIs to diff --git a/src/renderer/index.html b/src/renderer/index.html index c8832dc573..239d9c794c 100644 --- a/src/renderer/index.html +++ b/src/renderer/index.html @@ -1,46 +1,45 @@ + + + + + Cherry Studio - - - - - Cherry Studio + + - #spinner img { - width: 100px; - border-radius: 50px; - } - - - - -
-
- -
- - - - - - \ No newline at end of file + +
+
+ +
+ + + + + diff --git a/src/renderer/miniWindow.html b/src/renderer/miniWindow.html index c2748618f1..83b108b8a4 100644 --- a/src/renderer/miniWindow.html +++ b/src/renderer/miniWindow.html @@ -1,24 +1,23 @@ + + + + + Cherry Studio - - - - - Cherry Studio + + - - - - -
- - - - \ No newline at end of file + +
+ + + diff --git a/src/renderer/selectionAction.html b/src/renderer/selectionAction.html index 1dd3fa616c..1078b35264 100644 --- a/src/renderer/selectionAction.html +++ b/src/renderer/selectionAction.html @@ -1,41 +1,39 @@ - - + - + Cherry Studio Selection Assistant + - - - +
- - - \ No newline at end of file + + diff --git a/src/renderer/selectionToolbar.html b/src/renderer/selectionToolbar.html index 34efa7effc..f8f6777adb 100644 --- a/src/renderer/selectionToolbar.html +++ b/src/renderer/selectionToolbar.html @@ -1,46 +1,43 @@ + + + + + Cherry Studio Selection Toolbar + - - - - - Cherry Studio Selection Toolbar + +
+ + - - - \ No newline at end of file + #root { + margin: 0 !important; + padding: 0 !important; + width: max-content !important; + height: fit-content !important; + } + + + diff --git a/src/renderer/src/aiCore/clients/AihubmixAPIClient.ts b/src/renderer/src/aiCore/clients/AihubmixAPIClient.ts index d940abdfe7..e054057be1 100644 --- a/src/renderer/src/aiCore/clients/AihubmixAPIClient.ts +++ b/src/renderer/src/aiCore/clients/AihubmixAPIClient.ts @@ -103,7 +103,12 @@ export class AihubmixAPIClient extends BaseApiClient { } // gemini开头 且不以-nothink、-search结尾 - if ((id.startsWith('gemini') || id.startsWith('imagen')) && !id.endsWith('-nothink') && !id.endsWith('-search')) { + if ( + (id.startsWith('gemini') || id.startsWith('imagen')) && + !id.endsWith('-nothink') && + !id.endsWith('-search') && + !id.includes('embedding') + ) { const client = this.clients.get('gemini') if (!client || !this.isValidClient(client)) { throw new Error('Gemini client not properly initialized') diff --git a/src/renderer/src/aiCore/clients/ApiClientFactory.ts b/src/renderer/src/aiCore/clients/ApiClientFactory.ts index adc97e70e0..991bdccf34 100644 --- a/src/renderer/src/aiCore/clients/ApiClientFactory.ts +++ b/src/renderer/src/aiCore/clients/ApiClientFactory.ts @@ -5,8 +5,10 @@ import { AnthropicAPIClient } from './anthropic/AnthropicAPIClient' import { BaseApiClient } from './BaseApiClient' import { GeminiAPIClient } from './gemini/GeminiAPIClient' import { VertexAPIClient } from './gemini/VertexAPIClient' +import { NewAPIClient } from './NewAPIClient' import { OpenAIAPIClient } from './openai/OpenAIApiClient' import { OpenAIResponseAPIClient } from './openai/OpenAIResponseAPIClient' +import { PPIOAPIClient } from './ppio/PPIOAPIClient' /** * Factory for creating ApiClient instances based on provider configuration @@ -31,14 +33,23 @@ export class ApiClientFactory { instance = new AihubmixAPIClient(provider) as BaseApiClient return instance } + if (provider.id === 'new-api') { + console.log(`[ApiClientFactory] Creating NewAPIClient for provider: ${provider.id}`) + instance = new NewAPIClient(provider) as BaseApiClient + return instance + } + if (provider.id === 'ppio') { + console.log(`[ApiClientFactory] Creating PPIOAPIClient for provider: ${provider.id}`) + instance = new PPIOAPIClient(provider) as BaseApiClient + return instance + } // 然后检查标准的provider type switch (provider.type) { case 'openai': - case 'azure-openai': - console.log(`[ApiClientFactory] Creating OpenAIApiClient for provider: ${provider.id}`) instance = new OpenAIAPIClient(provider) as BaseApiClient break + case 'azure-openai': case 'openai-response': instance = new OpenAIResponseAPIClient(provider) as BaseApiClient break @@ -61,6 +72,7 @@ export class ApiClientFactory { } } -export function isOpenAIProvider(provider: Provider) { - return !['anthropic', 'gemini'].includes(provider.type) -} +// 移除这个函数,它已经移动到 utils/index.ts +// export function isOpenAIProvider(provider: Provider) { +// return !['anthropic', 'gemini'].includes(provider.type) +// } diff --git a/src/renderer/src/aiCore/clients/BaseApiClient.ts b/src/renderer/src/aiCore/clients/BaseApiClient.ts index d8420135c4..ad1c348475 100644 --- a/src/renderer/src/aiCore/clients/BaseApiClient.ts +++ b/src/renderer/src/aiCore/clients/BaseApiClient.ts @@ -17,6 +17,7 @@ import { MCPCallToolResponse, MCPTool, MCPToolResponse, + MemoryItem, Model, OpenAIServiceTier, Provider, @@ -214,12 +215,14 @@ export abstract class BaseApiClient< public async getMessageContent(message: Message): Promise { const content = getMainTextContent(message) + if (isEmpty(content)) { return '' } const webSearchReferences = await this.getWebSearchReferencesFromCache(message) const knowledgeReferences = await this.getKnowledgeBaseReferencesFromCache(message) + const memoryReferences = this.getMemoryReferencesFromCache(message) // 添加偏移量以避免ID冲突 const reindexedKnowledgeReferences = knowledgeReferences.map((ref) => ({ @@ -227,7 +230,7 @@ export abstract class BaseApiClient< id: ref.id + webSearchReferences.length // 为知识库引用的ID添加网络搜索引用的数量作为偏移量 })) - const allReferences = [...webSearchReferences, ...reindexedKnowledgeReferences] + const allReferences = [...webSearchReferences, ...reindexedKnowledgeReferences, ...memoryReferences] Logger.log(`Found ${allReferences.length} references for ID: ${message.id}`, allReferences) @@ -257,7 +260,7 @@ export abstract class BaseApiClient< for (const fileBlock of textFileBlocks) { const file = fileBlock.file - const fileContent = (await window.api.file.read(file.id + file.ext)).trim() + const fileContent = (await window.api.file.read(file.id + file.ext, true)).trim() const fileNameRow = 'file: ' + file.origin_name + '\n\n' text = text + fileNameRow + fileContent + divider } @@ -269,6 +272,20 @@ export abstract class BaseApiClient< return '' } + private getMemoryReferencesFromCache(message: Message) { + const memories = window.keyv.get(`memory-search-${message.id}`) as MemoryItem[] | undefined + if (memories) { + const memoryReferences: KnowledgeReference[] = memories.map((mem, index) => ({ + id: index + 1, + content: `${mem.memory} -- Created at: ${mem.createdAt}`, + sourceUrl: '', + type: 'memory' + })) + return memoryReferences + } + return [] + } + private async getWebSearchReferencesFromCache(message: Message) { const content = getMainTextContent(message) if (isEmpty(content)) { @@ -277,6 +294,7 @@ export abstract class BaseApiClient< const webSearch: WebSearchResponse = window.keyv.get(`web-search-${message.id}`) if (webSearch) { + window.keyv.remove(`web-search-${message.id}`) return (webSearch.results as WebSearchProviderResponse).results.map( (result, index) => ({ @@ -302,6 +320,7 @@ export abstract class BaseApiClient< const knowledgeReferences: KnowledgeReference[] = window.keyv.get(`knowledge-search-${message.id}`) if (!isEmpty(knowledgeReferences)) { + window.keyv.remove(`knowledge-search-${message.id}`) // Logger.log(`Found ${knowledgeReferences.length} knowledge base references in cache for ID: ${message.id}`) return knowledgeReferences } diff --git a/src/renderer/src/aiCore/clients/NewAPIClient.ts b/src/renderer/src/aiCore/clients/NewAPIClient.ts new file mode 100644 index 0000000000..769ca90acf --- /dev/null +++ b/src/renderer/src/aiCore/clients/NewAPIClient.ts @@ -0,0 +1,233 @@ +import { isSupportedModel } from '@renderer/config/models' +import { + GenerateImageParams, + MCPCallToolResponse, + MCPTool, + MCPToolResponse, + Model, + Provider, + ToolCallResponse +} from '@renderer/types' +import { + NewApiModel, + RequestOptions, + SdkInstance, + SdkMessageParam, + SdkParams, + SdkRawChunk, + SdkRawOutput, + SdkTool, + SdkToolCall +} from '@renderer/types/sdk' + +import { CompletionsContext } from '../middleware/types' +import { AnthropicAPIClient } from './anthropic/AnthropicAPIClient' +import { BaseApiClient } from './BaseApiClient' +import { GeminiAPIClient } from './gemini/GeminiAPIClient' +import { OpenAIAPIClient } from './openai/OpenAIApiClient' +import { OpenAIResponseAPIClient } from './openai/OpenAIResponseAPIClient' +import { RequestTransformer, ResponseChunkTransformer } from './types' + +export class NewAPIClient extends BaseApiClient { + // 使用联合类型而不是any,保持类型安全 + private clients: Map = + new Map() + private defaultClient: OpenAIAPIClient + private currentClient: BaseApiClient + + constructor(provider: Provider) { + super(provider) + + const claudeClient = new AnthropicAPIClient(provider) + const geminiClient = new GeminiAPIClient(provider) + const openaiClient = new OpenAIAPIClient(provider) + const openaiResponseClient = new OpenAIResponseAPIClient(provider) + + this.clients.set('claude', claudeClient) + this.clients.set('gemini', geminiClient) + this.clients.set('openai', openaiClient) + this.clients.set('openai-response', openaiResponseClient) + + // 设置默认client + this.defaultClient = openaiClient + this.currentClient = this.defaultClient as BaseApiClient + } + + override getBaseURL(): string { + if (!this.currentClient) { + return this.provider.apiHost + } + return this.currentClient.getBaseURL() + } + + /** + * 类型守卫:确保client是BaseApiClient的实例 + */ + private isValidClient(client: unknown): client is BaseApiClient { + return ( + client !== null && + client !== undefined && + typeof client === 'object' && + 'createCompletions' in client && + 'getRequestTransformer' in client && + 'getResponseChunkTransformer' in client + ) + } + + /** + * 根据模型获取合适的client + */ + private getClient(model: Model): BaseApiClient { + if (!model.endpoint_type) { + throw new Error('Model endpoint type is not defined') + } + + if (model.endpoint_type === 'anthropic') { + const client = this.clients.get('claude') + if (!client || !this.isValidClient(client)) { + throw new Error('Failed to get claude client') + } + return client + } + + if (model.endpoint_type === 'openai-response') { + const client = this.clients.get('openai-response') + if (!client || !this.isValidClient(client)) { + throw new Error('Failed to get openai-response client') + } + return client + } + + if (model.endpoint_type === 'gemini') { + const client = this.clients.get('gemini') + if (!client || !this.isValidClient(client)) { + throw new Error('Failed to get gemini client') + } + return client + } + + if (model.endpoint_type === 'openai' || model.endpoint_type === 'image-generation') { + const client = this.clients.get('openai') + if (!client || !this.isValidClient(client)) { + throw new Error('Failed to get openai client') + } + return client + } + + throw new Error('Invalid model endpoint type: ' + model.endpoint_type) + } + + /** + * 根据模型选择合适的client并委托调用 + */ + public getClientForModel(model: Model): BaseApiClient { + this.currentClient = this.getClient(model) + return this.currentClient + } + + // ============ BaseApiClient 抽象方法实现 ============ + + async createCompletions(payload: SdkParams, options?: RequestOptions): Promise { + // 尝试从payload中提取模型信息来选择client + const modelId = this.extractModelFromPayload(payload) + if (modelId) { + const modelObj = { id: modelId } as Model + const targetClient = this.getClient(modelObj) + return targetClient.createCompletions(payload, options) + } + + // 如果无法从payload中提取模型,使用当前设置的client + return this.currentClient.createCompletions(payload, options) + } + + /** + * 从SDK payload中提取模型ID + */ + private extractModelFromPayload(payload: SdkParams): string | null { + // 不同的SDK可能有不同的字段名 + if ('model' in payload && typeof payload.model === 'string') { + return payload.model + } + return null + } + + async generateImage(params: GenerateImageParams): Promise { + return this.currentClient.generateImage(params) + } + + async getEmbeddingDimensions(model?: Model): Promise { + const client = model ? this.getClient(model) : this.currentClient + return client.getEmbeddingDimensions(model) + } + + override async listModels(): Promise { + try { + const sdk = await this.defaultClient.getSdkInstance() + // Explicitly type the expected response shape so that `data` is recognised. + const response = await sdk.request<{ data: NewApiModel[] }>({ + method: 'get', + path: '/models' + }) + const models: NewApiModel[] = response.data ?? [] + + models.forEach((model) => { + model.id = model.id.trim() + }) + + return models.filter(isSupportedModel) + } catch (error) { + console.error('Error listing models:', error) + return [] + } + } + + async getSdkInstance(): Promise { + return this.currentClient.getSdkInstance() + } + + getRequestTransformer(): RequestTransformer { + return this.currentClient.getRequestTransformer() + } + + getResponseChunkTransformer(ctx: CompletionsContext): ResponseChunkTransformer { + return this.currentClient.getResponseChunkTransformer(ctx) + } + + convertMcpToolsToSdkTools(mcpTools: MCPTool[]): SdkTool[] { + return this.currentClient.convertMcpToolsToSdkTools(mcpTools) + } + + convertSdkToolCallToMcp(toolCall: SdkToolCall, mcpTools: MCPTool[]): MCPTool | undefined { + return this.currentClient.convertSdkToolCallToMcp(toolCall, mcpTools) + } + + convertSdkToolCallToMcpToolResponse(toolCall: SdkToolCall, mcpTool: MCPTool): ToolCallResponse { + return this.currentClient.convertSdkToolCallToMcpToolResponse(toolCall, mcpTool) + } + + buildSdkMessages( + currentReqMessages: SdkMessageParam[], + output: SdkRawOutput | string, + toolResults: SdkMessageParam[], + toolCalls?: SdkToolCall[] + ): SdkMessageParam[] { + return this.currentClient.buildSdkMessages(currentReqMessages, output, toolResults, toolCalls) + } + + convertMcpToolResponseToSdkMessageParam( + mcpToolResponse: MCPToolResponse, + resp: MCPCallToolResponse, + model: Model + ): SdkMessageParam | undefined { + const client = this.getClient(model) + return client.convertMcpToolResponseToSdkMessageParam(mcpToolResponse, resp, model) + } + + extractMessagesFromSdkPayload(sdkPayload: SdkParams): SdkMessageParam[] { + return this.currentClient.extractMessagesFromSdkPayload(sdkPayload) + } + + estimateMessageTokens(message: SdkMessageParam): number { + return this.currentClient.estimateMessageTokens(message) + } +} diff --git a/src/renderer/src/aiCore/clients/__tests__/ApiClientFactory.test.ts b/src/renderer/src/aiCore/clients/__tests__/ApiClientFactory.test.ts new file mode 100644 index 0000000000..9fdfcb2687 --- /dev/null +++ b/src/renderer/src/aiCore/clients/__tests__/ApiClientFactory.test.ts @@ -0,0 +1,208 @@ +import { Provider } from '@renderer/types' +import { isOpenAIProvider } from '@renderer/utils' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { AihubmixAPIClient } from '../AihubmixAPIClient' +import { AnthropicAPIClient } from '../anthropic/AnthropicAPIClient' +import { ApiClientFactory } from '../ApiClientFactory' +import { GeminiAPIClient } from '../gemini/GeminiAPIClient' +import { VertexAPIClient } from '../gemini/VertexAPIClient' +import { NewAPIClient } from '../NewAPIClient' +import { OpenAIAPIClient } from '../openai/OpenAIApiClient' +import { OpenAIResponseAPIClient } from '../openai/OpenAIResponseAPIClient' +import { PPIOAPIClient } from '../ppio/PPIOAPIClient' + +// 为工厂测试创建最小化 provider 的辅助函数 +// ApiClientFactory 只使用 'id' 和 'type' 字段来决定创建哪个客户端 +// 其他字段会传递给客户端构造函数,但不影响工厂逻辑 +const createTestProvider = (id: string, type: string): Provider => ({ + id, + type: type as Provider['type'], + name: '', + apiKey: '', + apiHost: '', + models: [] +}) + +// Mock 所有客户端模块 +vi.mock('../AihubmixAPIClient', () => ({ + AihubmixAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../anthropic/AnthropicAPIClient', () => ({ + AnthropicAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../gemini/GeminiAPIClient', () => ({ + GeminiAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../gemini/VertexAPIClient', () => ({ + VertexAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../NewAPIClient', () => ({ + NewAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../openai/OpenAIApiClient', () => ({ + OpenAIAPIClient: vi.fn().mockImplementation(() => ({})) +})) +vi.mock('../openai/OpenAIResponseAPIClient', () => ({ + OpenAIResponseAPIClient: vi.fn().mockImplementation(() => ({ + getClient: vi.fn().mockReturnThis() + })) +})) +vi.mock('../ppio/PPIOAPIClient', () => ({ + PPIOAPIClient: vi.fn().mockImplementation(() => ({})) +})) + +describe('ApiClientFactory', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('create', () => { + // 测试特殊 ID 的客户端创建 + it('should create AihubmixAPIClient for aihubmix provider', () => { + const provider = createTestProvider('aihubmix', 'openai') + + const client = ApiClientFactory.create(provider) + + expect(AihubmixAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create NewAPIClient for new-api provider', () => { + const provider = createTestProvider('new-api', 'openai') + + const client = ApiClientFactory.create(provider) + + expect(NewAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create PPIOAPIClient for ppio provider', () => { + const provider = createTestProvider('ppio', 'openai') + + const client = ApiClientFactory.create(provider) + + expect(PPIOAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + // 测试标准类型的客户端创建 + it('should create OpenAIAPIClient for openai type', () => { + const provider = createTestProvider('custom-openai', 'openai') + + const client = ApiClientFactory.create(provider) + + expect(OpenAIAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create OpenAIResponseAPIClient for azure-openai type', () => { + const provider = createTestProvider('azure-openai', 'azure-openai') + + const client = ApiClientFactory.create(provider) + + expect(OpenAIResponseAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create OpenAIResponseAPIClient for openai-response type', () => { + const provider = createTestProvider('response', 'openai-response') + + const client = ApiClientFactory.create(provider) + + expect(OpenAIResponseAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create GeminiAPIClient for gemini type', () => { + const provider = createTestProvider('gemini', 'gemini') + + const client = ApiClientFactory.create(provider) + + expect(GeminiAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create VertexAPIClient for vertexai type', () => { + const provider = createTestProvider('vertex', 'vertexai') + + const client = ApiClientFactory.create(provider) + + expect(VertexAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + it('should create AnthropicAPIClient for anthropic type', () => { + const provider = createTestProvider('anthropic', 'anthropic') + + const client = ApiClientFactory.create(provider) + + expect(AnthropicAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + // 测试默认情况 + it('should create OpenAIAPIClient as default for unknown type', () => { + const provider = createTestProvider('unknown', 'unknown-type') + + const client = ApiClientFactory.create(provider) + + expect(OpenAIAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + // 测试边界条件 + it('should handle provider with minimal configuration', () => { + const provider = createTestProvider('minimal', 'openai') + + const client = ApiClientFactory.create(provider) + + expect(OpenAIAPIClient).toHaveBeenCalledWith(provider) + expect(client).toBeDefined() + }) + + // 测试特殊 ID 优先级高于类型 + it('should prioritize special ID over type', () => { + const provider = createTestProvider('aihubmix', 'anthropic') // 即使类型是 anthropic + + const client = ApiClientFactory.create(provider) + + // 应该创建 AihubmixAPIClient 而不是 AnthropicAPIClient + expect(AihubmixAPIClient).toHaveBeenCalledWith(provider) + expect(AnthropicAPIClient).not.toHaveBeenCalled() + expect(client).toBeDefined() + }) + }) + + describe('isOpenAIProvider', () => { + it('should return true for openai type', () => { + const provider = createTestProvider('openai', 'openai') + expect(isOpenAIProvider(provider)).toBe(true) + }) + + it('should return true for azure-openai type', () => { + const provider = createTestProvider('azure-openai', 'azure-openai') + expect(isOpenAIProvider(provider)).toBe(true) + }) + + it('should return true for unknown type (fallback to OpenAI)', () => { + const provider = createTestProvider('unknown', 'unknown') + expect(isOpenAIProvider(provider)).toBe(true) + }) + + it('should return false for vertexai type', () => { + const provider = createTestProvider('vertex', 'vertexai') + expect(isOpenAIProvider(provider)).toBe(false) + }) + + it('should return false for anthropic type', () => { + const provider = createTestProvider('anthropic', 'anthropic') + expect(isOpenAIProvider(provider)).toBe(false) + }) + + it('should return false for gemini type', () => { + const provider = createTestProvider('gemini', 'gemini') + expect(isOpenAIProvider(provider)).toBe(false) + }) + }) +}) diff --git a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts index e07b7508db..e18b20889a 100644 --- a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts +++ b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts @@ -50,9 +50,11 @@ import { LLMWebSearchInProgressChunk, MCPToolCreatedChunk, TextDeltaChunk, - ThinkingDeltaChunk + TextStartChunk, + ThinkingDeltaChunk, + ThinkingStartChunk } from '@renderer/types/chunk' -import type { Message } from '@renderer/types/newMessage' +import { type Message } from '@renderer/types/newMessage' import { AnthropicSdkMessageParam, AnthropicSdkParams, @@ -229,7 +231,7 @@ export class AnthropicAPIClient extends BaseApiClient< } }) } else { - const fileContent = await (await window.api.file.read(file.id + file.ext)).trim() + const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim() parts.push({ type: 'text', text: file.origin_name + '\n' + fileContent @@ -493,7 +495,8 @@ export class AnthropicAPIClient extends BaseApiClient< system: systemMessage ? [systemMessage] : undefined, thinking: this.getBudgetToken(assistant, model), tools: tools.length > 0 ? tools : undefined, - ...this.getCustomParameters(assistant) + // 只在对话场景下应用自定义参数,避免影响翻译、总结等其他业务逻辑 + ...(coreRequest.callType === 'chat' ? this.getCustomParameters(assistant) : {}) } const finalParams: MessageCreateParams = streamOutput @@ -516,15 +519,23 @@ export class AnthropicAPIClient extends BaseApiClient< return () => { let accumulatedJson = '' const toolCalls: Record = {} - return { async transform(rawChunk: AnthropicSdkRawChunk, controller: TransformStreamDefaultController) { switch (rawChunk.type) { case 'message': { let i = 0 + let hasTextContent = false + let hasThinkingContent = false + for (const content of rawChunk.content) { switch (content.type) { case 'text': { + if (!hasTextContent) { + controller.enqueue({ + type: ChunkType.TEXT_START + } as TextStartChunk) + hasTextContent = true + } controller.enqueue({ type: ChunkType.TEXT_DELTA, text: content.text @@ -537,6 +548,12 @@ export class AnthropicAPIClient extends BaseApiClient< break } case 'thinking': { + if (!hasThinkingContent) { + controller.enqueue({ + type: ChunkType.THINKING_START + } as ThinkingStartChunk) + hasThinkingContent = true + } controller.enqueue({ type: ChunkType.THINKING_DELTA, text: content.thinking @@ -611,6 +628,19 @@ export class AnthropicAPIClient extends BaseApiClient< toolCalls[rawChunk.index] = contentBlock break } + case 'text': { + controller.enqueue({ + type: ChunkType.TEXT_START + } as TextStartChunk) + break + } + case 'thinking': + case 'redacted_thinking': { + controller.enqueue({ + type: ChunkType.THINKING_START + } as ThinkingStartChunk) + break + } } break } diff --git a/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts b/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts index 549e931966..30d497c21d 100644 --- a/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts +++ b/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts @@ -1,7 +1,7 @@ import { Content, + createPartFromUri, File, - FileState, FunctionCall, GenerateContentConfig, GenerateImagesConfig, @@ -10,7 +10,6 @@ import { HarmCategory, Modality, Model as GeminiModel, - Pager, Part, SafetySetting, SendMessageParameters, @@ -22,17 +21,17 @@ import { GenericChunk } from '@renderer/aiCore/middleware/schemas' import { findTokenLimit, GEMINI_FLASH_MODEL_REGEX, - isGeminiReasoningModel, isGemmaModel, + isSupportedThinkingTokenGeminiModel, isVisionModel } from '@renderer/config/models' -import { CacheService } from '@renderer/services/CacheService' import { estimateTextTokens } from '@renderer/services/TokenService' import { Assistant, EFFORT_RATIO, - FileType, + FileMetadata, FileTypes, + FileUploadResponse, GenerateImageParams, MCPCallToolResponse, MCPTool, @@ -42,7 +41,7 @@ import { ToolCallResponse, WebSearchSource } from '@renderer/types' -import { ChunkType, LLMWebSearchCompleteChunk } from '@renderer/types/chunk' +import { ChunkType, LLMWebSearchCompleteChunk, TextStartChunk, ThinkingStartChunk } from '@renderer/types/chunk' import { Message } from '@renderer/types/newMessage' import { GeminiOptions, @@ -60,7 +59,7 @@ import { } from '@renderer/utils/mcp-tools' import { findFileBlocks, findImageBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find' import { buildSystemPrompt } from '@renderer/utils/prompt' -import { MB } from '@shared/config/constant' +import { defaultTimeout, MB } from '@shared/config/constant' import { BaseApiClient } from '../BaseApiClient' import { RequestTransformer, ResponseChunkTransformer } from '../types' @@ -118,7 +117,7 @@ export class GeminiAPIClient extends BaseApiClient< aspectRatio: imageSize, abortSignal: signal, httpOptions: { - timeout: 5 * 60 * 1000 + timeout: defaultTimeout } } const response = await sdk.models.generateImages({ @@ -198,7 +197,7 @@ export class GeminiAPIClient extends BaseApiClient< * @param file - The file * @returns The part */ - private async handlePdfFile(file: FileType): Promise { + private async handlePdfFile(file: FileMetadata): Promise { const smallFileSize = 20 * MB const isSmallFile = file.size < smallFileSize @@ -213,26 +212,17 @@ export class GeminiAPIClient extends BaseApiClient< } // Retrieve file from Gemini uploaded files - const fileMetadata: File | undefined = await this.retrieveFile(file) + const fileMetadata: FileUploadResponse = await window.api.fileService.retrieve(this.provider, file.id) - if (fileMetadata) { - return { - fileData: { - fileUri: fileMetadata.uri, - mimeType: fileMetadata.mimeType - } as Part['fileData'] - } + if (fileMetadata.status === 'success') { + const remoteFile = fileMetadata.originalFile?.file as File + return createPartFromUri(remoteFile.uri!, remoteFile.mimeType!) } // If file is not found, upload it to Gemini - const result = await this.uploadFile(file) - - return { - fileData: { - fileUri: result.uri, - mimeType: result.mimeType - } as Part['fileData'] - } + const result = await window.api.fileService.upload(this.provider, file) + const remoteFile = result.originalFile?.file as File + return createPartFromUri(remoteFile.uri!, remoteFile.mimeType!) } /** @@ -243,6 +233,7 @@ export class GeminiAPIClient extends BaseApiClient< private async convertMessageToSdkParam(message: Message): Promise { const role = message.role === 'user' ? 'user' : 'model' const parts: Part[] = [{ text: await this.getMessageContent(message) }] + // Add any generated images from previous responses const imageBlocks = findImageBlocks(message) for (const imageBlock of imageBlocks) { @@ -297,7 +288,7 @@ export class GeminiAPIClient extends BaseApiClient< continue } if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) { - const fileContent = await (await window.api.file.read(file.id + file.ext)).trim() + const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim() parts.push({ text: file.origin_name + '\n' + fileContent }) @@ -393,29 +384,29 @@ export class GeminiAPIClient extends BaseApiClient< * @returns The reasoning effort */ private getBudgetToken(assistant: Assistant, model: Model) { - if (isGeminiReasoningModel(model)) { + if (isSupportedThinkingTokenGeminiModel(model)) { const reasoningEffort = assistant?.settings?.reasoning_effort // 如果thinking_budget是undefined,不思考 if (reasoningEffort === undefined) { - return { - thinkingConfig: { - includeThoughts: false, - ...(GEMINI_FLASH_MODEL_REGEX.test(model.id) ? { thinkingBudget: 0 } : {}) - } as ThinkingConfig - } + return GEMINI_FLASH_MODEL_REGEX.test(model.id) + ? { + thinkingConfig: { + thinkingBudget: 0 + } + } + : {} } - const effortRatio = EFFORT_RATIO[reasoningEffort] - - if (effortRatio > 1) { + if (reasoningEffort === 'auto') { return { thinkingConfig: { - includeThoughts: true + includeThoughts: true, + thinkingBudget: -1 } } } - + const effortRatio = EFFORT_RATIO[reasoningEffort] const { min, max } = findTokenLimit(model.id) || { min: 0, max: 0 } // 计算 budgetTokens,确保不低于 min const budget = Math.floor((max - min) * effortRatio + min) @@ -452,7 +443,7 @@ export class GeminiAPIClient extends BaseApiClient< messages: GeminiSdkMessageParam[] metadata: Record }> => { - const { messages, mcpTools, maxTokens, enableWebSearch, enableGenerateImage } = coreRequest + const { messages, mcpTools, maxTokens, enableWebSearch, enableUrlContext, enableGenerateImage } = coreRequest // 1. 处理系统消息 let systemInstruction = assistant.prompt @@ -492,6 +483,12 @@ export class GeminiAPIClient extends BaseApiClient< }) } + if (enableUrlContext) { + tools.push({ + urlContext: {} + }) + } + if (isGemmaModel(model) && assistant.prompt) { const isFirstMessage = history.length === 0 if (isFirstMessage && messageContents) { @@ -535,7 +532,8 @@ export class GeminiAPIClient extends BaseApiClient< tools: tools, ...(enableGenerateImage ? this.getGenerateImageParameter() : {}), ...this.getBudgetToken(assistant, model), - ...this.getCustomParameters(assistant) + // 只在对话场景下应用自定义参数,避免影响翻译、总结等其他业务逻辑 + ...(coreRequest.callType === 'chat' ? this.getCustomParameters(assistant) : {}) } const param: GeminiSdkParams = { @@ -555,20 +553,34 @@ export class GeminiAPIClient extends BaseApiClient< } getResponseChunkTransformer(): ResponseChunkTransformer { + const toolCalls: FunctionCall[] = [] + let isFirstTextChunk = true + let isFirstThinkingChunk = true return () => ({ async transform(chunk: GeminiSdkRawChunk, controller: TransformStreamDefaultController) { - const toolCalls: FunctionCall[] = [] if (chunk.candidates && chunk.candidates.length > 0) { for (const candidate of chunk.candidates) { if (candidate.content) { candidate.content.parts?.forEach((part) => { const text = part.text || '' if (part.thought) { + if (isFirstThinkingChunk) { + controller.enqueue({ + type: ChunkType.THINKING_START + } as ThinkingStartChunk) + isFirstThinkingChunk = false + } controller.enqueue({ type: ChunkType.THINKING_DELTA, text: text }) } else if (part.text) { + if (isFirstTextChunk) { + controller.enqueue({ + type: ChunkType.TEXT_START + } as TextStartChunk) + isFirstTextChunk = false + } controller.enqueue({ type: ChunkType.TEXT_DELTA, text: text @@ -601,6 +613,13 @@ export class GeminiAPIClient extends BaseApiClient< } } as LLMWebSearchCompleteChunk) } + if (toolCalls.length > 0) { + controller.enqueue({ + type: ChunkType.MCP_TOOL_CREATED, + tool_calls: [...toolCalls] + }) + toolCalls.length = 0 + } controller.enqueue({ type: ChunkType.LLM_RESPONSE_COMPLETE, response: { @@ -765,61 +784,11 @@ export class GeminiAPIClient extends BaseApiClient< return [...(sdkPayload.history || []), messageParam] } - private async uploadFile(file: FileType): Promise { - return await this.sdkInstance!.files.upload({ - file: file.path, - config: { - mimeType: 'application/pdf', - name: file.id, - displayName: file.origin_name - } - }) - } - - private async base64File(file: FileType) { + private async base64File(file: FileMetadata) { const { data } = await window.api.file.base64File(file.id + file.ext) return { data, mimeType: 'application/pdf' } } - - private async retrieveFile(file: FileType): Promise { - const cachedResponse = CacheService.get('gemini_file_list') - - if (cachedResponse) { - return this.processResponse(cachedResponse, file) - } - - const response = await this.sdkInstance!.files.list() - CacheService.set('gemini_file_list', response, 3000) - - return this.processResponse(response, file) - } - - private async processResponse(response: Pager, file: FileType) { - for await (const f of response) { - if (f.state === FileState.ACTIVE) { - if (f.displayName === file.origin_name && Number(f.sizeBytes) === file.size) { - return f - } - } - } - - return undefined - } - - // @ts-ignore unused - private async listFiles(): Promise { - const files: File[] = [] - for await (const f of await this.sdkInstance!.files.list()) { - files.push(f) - } - return files - } - - // @ts-ignore unused - private async deleteFile(fileId: string) { - await this.sdkInstance!.files.delete({ name: fileId }) - } } diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts index 499edfbb5c..80a611493d 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts @@ -5,6 +5,7 @@ import { GEMINI_FLASH_MODEL_REGEX, getOpenAIWebSearchParams, isDoubaoThinkingAutoModel, + isQwenReasoningModel, isReasoningModel, isSupportedReasoningEffortGrokModel, isSupportedReasoningEffortModel, @@ -31,7 +32,7 @@ import { ToolCallResponse, WebSearchSource } from '@renderer/types' -import { ChunkType } from '@renderer/types/chunk' +import { ChunkType, TextStartChunk, ThinkingStartChunk } from '@renderer/types/chunk' import { Message } from '@renderer/types/newMessage' import { OpenAISdkMessageParam, @@ -114,6 +115,13 @@ export class OpenAIAPIClient extends OpenAIBaseClient< if (!reasoningEffort) { if (model.provider === 'openrouter') { + if ( + isSupportedThinkingTokenGeminiModel(model) && + !GEMINI_FLASH_MODEL_REGEX.test(model.id) && + model.id.includes('grok-4') + ) { + return {} + } return { reasoning: { enabled: false, exclude: true } } } if (isSupportedThinkingTokenQwenModel(model)) { @@ -126,7 +134,15 @@ export class OpenAIAPIClient extends OpenAIBaseClient< if (isSupportedThinkingTokenGeminiModel(model)) { if (GEMINI_FLASH_MODEL_REGEX.test(model.id)) { - return { reasoning_effort: 'none' } + return { + extra_body: { + google: { + thinking_config: { + thinking_budget: 0 + } + } + } + } } return {} } @@ -155,10 +171,17 @@ export class OpenAIAPIClient extends OpenAIBaseClient< // Qwen models if (isSupportedThinkingTokenQwenModel(model)) { - return { + const thinkConfig = { enable_thinking: true, thinking_budget: budgetTokens } + if (this.provider.id === 'dashscope') { + return { + ...thinkConfig, + incremental_output: true + } + } + return thinkConfig } // Grok models @@ -169,12 +192,37 @@ export class OpenAIAPIClient extends OpenAIBaseClient< } // OpenAI models - if (isSupportedReasoningEffortOpenAIModel(model) || isSupportedThinkingTokenGeminiModel(model)) { + if (isSupportedReasoningEffortOpenAIModel(model)) { return { reasoning_effort: reasoningEffort } } + if (isSupportedThinkingTokenGeminiModel(model)) { + if (reasoningEffort === 'auto') { + return { + extra_body: { + google: { + thinking_config: { + thinking_budget: -1, + include_thoughts: true + } + } + } + } + } + return { + extra_body: { + google: { + thinking_config: { + thinking_budget: budgetTokens, + include_thoughts: true + } + } + } + } + } + // Claude models if (isSupportedThinkingTokenClaudeModel(model)) { const maxTokens = assistant.settings?.maxTokens @@ -271,7 +319,7 @@ export class OpenAIAPIClient extends OpenAIBaseClient< } if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) { - const fileContent = await (await window.api.file.read(file.id + file.ext)).trim() + const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim() parts.push({ type: 'text', text: file.origin_name + '\n' + fileContent @@ -323,7 +371,12 @@ export class OpenAIAPIClient extends OpenAIBaseClient< if ('toolUseId' in mcpToolResponse && mcpToolResponse.toolUseId) { // This case is for Anthropic/Claude like tool usage, OpenAI uses tool_call_id // For OpenAI, we primarily expect toolCallId. This might need adjustment if mixing provider concepts. - return mcpToolCallResponseToOpenAICompatibleMessage(mcpToolResponse, resp, isVisionModel(model)) + return mcpToolCallResponseToOpenAICompatibleMessage( + mcpToolResponse, + resp, + isVisionModel(model), + this.provider.isNotSupportArrayContent ?? false + ) } else if ('toolCallId' in mcpToolResponse && mcpToolResponse.toolCallId) { return { role: 'tool', @@ -400,7 +453,14 @@ export class OpenAIAPIClient extends OpenAIBaseClient< messages: OpenAISdkMessageParam[] metadata: Record }> => { - const { messages, mcpTools, maxTokens, streamOutput, enableWebSearch } = coreRequest + const { messages, mcpTools, maxTokens, enableWebSearch } = coreRequest + let { streamOutput } = coreRequest + + // Qwen3商业版(思考模式)、Qwen3开源版、QwQ、QVQ只支持流式输出。 + if (isQwenReasoningModel(model)) { + streamOutput = true + } + // 1. 处理系统消息 let systemMessage = { role: 'system', content: assistant.prompt || '' } @@ -471,7 +531,8 @@ export class OpenAIAPIClient extends OpenAIBaseClient< ...this.getProviderSpecificParameters(assistant, model), ...this.getReasoningEffort(assistant, model), ...getOpenAIWebSearchParams(model, enableWebSearch), - ...this.getCustomParameters(assistant) + // 只在对话场景下应用自定义参数,避免影响翻译、总结等其他业务逻辑 + ...(coreRequest.callType === 'chat' ? this.getCustomParameters(assistant) : {}) } // Create the appropriate parameters object based on whether streaming is enabled @@ -527,11 +588,11 @@ export class OpenAIAPIClient extends OpenAIBaseClient< // Perplexity citations // @ts-ignore - citations may not be in standard type definitions - if (context.provider?.id === 'perplexity' && chunk.citations && chunk.citations.length > 0) { + if (context.provider?.id === 'perplexity' && chunk.search_results && chunk.search_results.length > 0) { hasBeenCollectedWebSearch = true return { // @ts-ignore - citations may not be in standard type definitions - results: chunk.citations, + results: chunk.search_results, source: WebSearchSource.PERPLEXITY } } @@ -622,6 +683,8 @@ export class OpenAIAPIClient extends OpenAIBaseClient< isFinished = true } + let isFirstThinkingChunk = true + let isFirstTextChunk = true return (context: ResponseChunkTransformerContext) => ({ async transform(chunk: OpenAISdkRawChunk, controller: TransformStreamDefaultController) { // 持续更新usage信息 @@ -635,68 +698,35 @@ export class OpenAIAPIClient extends OpenAIBaseClient< // 处理chunk if ('choices' in chunk && chunk.choices && chunk.choices.length > 0) { - const choice = chunk.choices[0] + for (const choice of chunk.choices) { + if (!choice) continue - if (!choice) return - - // 对于流式响应,使用delta;对于非流式响应,使用message - const contentSource: OpenAISdkRawContentSource | null = - 'delta' in choice ? choice.delta : 'message' in choice ? choice.message : null - - if (!contentSource) return - - const webSearchData = collectWebSearchData(chunk, contentSource, context) - if (webSearchData) { - controller.enqueue({ - type: ChunkType.LLM_WEB_SEARCH_COMPLETE, - llm_web_search: webSearchData - }) - } - - // 处理推理内容 (e.g. from OpenRouter DeepSeek-R1) - // @ts-ignore - reasoning_content is not in standard OpenAI types but some providers use it - const reasoningText = contentSource.reasoning_content || contentSource.reasoning - if (reasoningText) { - controller.enqueue({ - type: ChunkType.THINKING_DELTA, - text: reasoningText - }) - } - - // 处理文本内容 - if (contentSource.content) { - controller.enqueue({ - type: ChunkType.TEXT_DELTA, - text: contentSource.content - }) - } - - // 处理工具调用 - if (contentSource.tool_calls) { - for (const toolCall of contentSource.tool_calls) { - if ('index' in toolCall) { - const { id, index, function: fun } = toolCall - if (fun?.name) { - toolCalls[index] = { - id: id || '', - function: { - name: fun.name, - arguments: fun.arguments || '' - }, - type: 'function' - } - } else if (fun?.arguments) { - toolCalls[index].function.arguments += fun.arguments - } - } else { - toolCalls.push(toolCall) - } + // 对于流式响应,使用 delta;对于非流式响应,使用 message。 + // 然而某些 OpenAI 兼容平台在非流式请求时会错误地返回一个空对象的 delta 字段。 + // 如果 delta 为空对象或content为空,应当忽略它并回退到 message,避免造成内容缺失。 + let contentSource: OpenAISdkRawContentSource | null = null + if ( + 'delta' in choice && + choice.delta && + Object.keys(choice.delta).length > 0 && + (!('content' in choice.delta) || + (typeof choice.delta.content === 'string' && choice.delta.content !== '') || + (typeof (choice.delta as any).reasoning_content === 'string' && + (choice.delta as any).reasoning_content !== '') || + (typeof (choice.delta as any).reasoning === 'string' && (choice.delta as any).reasoning !== '')) + ) { + contentSource = choice.delta + } else if ('message' in choice) { + contentSource = choice.message + } + + if (!contentSource) { + if ('finish_reason' in choice && choice.finish_reason) { + emitCompletionSignals(controller) + } + continue } - } - // 处理finish_reason,发送流结束信号 - if ('finish_reason' in choice && choice.finish_reason) { - Logger.debug(`[OpenAIApiClient] Stream finished with reason: ${choice.finish_reason}`) const webSearchData = collectWebSearchData(chunk, contentSource, context) if (webSearchData) { controller.enqueue({ @@ -704,7 +734,72 @@ export class OpenAIAPIClient extends OpenAIBaseClient< llm_web_search: webSearchData }) } - emitCompletionSignals(controller) + + // 处理推理内容 (e.g. from OpenRouter DeepSeek-R1) + // @ts-ignore - reasoning_content is not in standard OpenAI types but some providers use it + const reasoningText = contentSource.reasoning_content || contentSource.reasoning + if (reasoningText) { + if (isFirstThinkingChunk) { + controller.enqueue({ + type: ChunkType.THINKING_START + } as ThinkingStartChunk) + isFirstThinkingChunk = false + } + controller.enqueue({ + type: ChunkType.THINKING_DELTA, + text: reasoningText + }) + } + + // 处理文本内容 + if (contentSource.content) { + if (isFirstTextChunk) { + controller.enqueue({ + type: ChunkType.TEXT_START + } as TextStartChunk) + isFirstTextChunk = false + } + controller.enqueue({ + type: ChunkType.TEXT_DELTA, + text: contentSource.content + }) + } + + // 处理工具调用 + if (contentSource.tool_calls) { + for (const toolCall of contentSource.tool_calls) { + if ('index' in toolCall) { + const { id, index, function: fun } = toolCall + if (fun?.name) { + toolCalls[index] = { + id: id || '', + function: { + name: fun.name, + arguments: fun.arguments || '' + }, + type: 'function' + } + } else if (fun?.arguments) { + toolCalls[index].function.arguments += fun.arguments + } + } else { + toolCalls.push(toolCall) + } + } + } + + // 处理finish_reason,发送流结束信号 + if ('finish_reason' in choice && choice.finish_reason) { + Logger.debug(`[OpenAIApiClient] Stream finished with reason: ${choice.finish_reason}`) + const webSearchData = collectWebSearchData(chunk, contentSource, context) + if (webSearchData) { + controller.enqueue({ + type: ChunkType.LLM_WEB_SEARCH_COMPLETE, + llm_web_search: webSearchData + }) + } + emitCompletionSignals(controller) + } } } }, diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIBaseClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIBaseClient.ts index 72fa1d7df8..95ddcbedd0 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIBaseClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIBaseClient.ts @@ -89,7 +89,7 @@ export abstract class OpenAIBaseClient< const data = await sdk.embeddings.create({ model: model.id, input: model?.provider === 'baidu-cloud' ? ['hi'] : 'hi', - encoding_format: 'float' + encoding_format: this.provider.id === 'voyageai' ? undefined : 'float' }) return data.data[0].embedding.length } diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts index 8fc38a9630..efa34005c6 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts @@ -2,13 +2,14 @@ import { GenericChunk } from '@renderer/aiCore/middleware/schemas' import { CompletionsContext } from '@renderer/aiCore/middleware/types' import { isOpenAIChatCompletionOnlyModel, + isOpenAILLMModel, isOpenAIDeepResearchModel, isSupportedReasoningEffortOpenAIModel, isVisionModel } from '@renderer/config/models' import { estimateTextTokens } from '@renderer/services/TokenService' import { - FileType, + FileMetadata, FileTypes, MCPCallToolResponse, MCPTool, @@ -39,7 +40,7 @@ import { findFileBlocks, findImageBlocks } from '@renderer/utils/messageUtils/fi import { buildSystemPrompt } from '@renderer/utils/prompt' import { MB } from '@shared/config/constant' import { isEmpty } from 'lodash' -import OpenAI from 'openai' +import OpenAI, { AzureOpenAI } from 'openai' import { ResponseInput } from 'openai/resources/responses/responses' import { RequestTransformer, ResponseChunkTransformer } from '../types' @@ -61,15 +62,39 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< this.client = new OpenAIAPIClient(provider) } + private formatApiHost() { + const host = this.provider.apiHost + if (host.endsWith('/openai/v1')) { + return host + } else { + if (host.endsWith('/')) { + return host + 'openai/v1' + } else { + return host + '/openai/v1' + } + } + } + /** * 根据模型特征选择合适的客户端 */ public getClient(model: Model) { - if (isOpenAIChatCompletionOnlyModel(model)) { - return this.client - } else { + if (this.provider.type === 'openai-response' && !isOpenAIChatCompletionOnlyModel(model)) { return this } + if (isOpenAILLMModel(model) && !isOpenAIChatCompletionOnlyModel(model)) { + if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') { + this.provider = { ...this.provider, apiHost: this.formatApiHost() } + if (this.provider.apiVersion === 'preview') { + return this + } else { + return this.client + } + } + return this + } else { + return this.client + } } override async getSdkInstance() { @@ -77,15 +102,24 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< return this.sdkInstance } - return new OpenAI({ - dangerouslyAllowBrowser: true, - apiKey: this.apiKey, - baseURL: this.getBaseURL(), - defaultHeaders: { - ...this.defaultHeaders(), - ...this.provider.extra_headers - } - }) + if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') { + return new AzureOpenAI({ + dangerouslyAllowBrowser: true, + apiKey: this.apiKey, + apiVersion: this.provider.apiVersion, + baseURL: this.provider.apiHost + }) + } else { + return new OpenAI({ + dangerouslyAllowBrowser: true, + apiKey: this.apiKey, + baseURL: this.getBaseURL(), + defaultHeaders: { + ...this.defaultHeaders(), + ...this.provider.extra_headers + } + }) + } } override async createCompletions( @@ -96,7 +130,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< return await sdk.responses.create(payload, options) } - private async handlePdfFile(file: FileType): Promise { + private async handlePdfFile(file: FileMetadata): Promise { if (file.size > 32 * MB) return undefined try { const pageCount = await window.api.file.pdfInfo(file.id + file.ext) @@ -173,7 +207,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< } if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) { - const fileContent = (await window.api.file.read(file.id + file.ext)).trim() + const fileContent = (await window.api.file.read(file.id + file.ext, true)).trim() parts.push({ type: 'input_text', text: file.origin_name + '\n' + fileContent @@ -354,16 +388,15 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< (m) => (m as OpenAI.Responses.EasyInputMessage).role === 'assistant' ) as OpenAI.Responses.EasyInputMessage const finalUserMessage = userMessage.pop() as OpenAI.Responses.EasyInputMessage - if ( - finalAssistantMessage && - Array.isArray(finalAssistantMessage.content) && - finalUserMessage && - Array.isArray(finalUserMessage.content) - ) { - finalAssistantMessage.content = [...finalAssistantMessage.content, ...finalUserMessage.content] + if (finalUserMessage && Array.isArray(finalUserMessage.content)) { + if (finalAssistantMessage && Array.isArray(finalAssistantMessage.content)) { + finalAssistantMessage.content = [...finalAssistantMessage.content, ...finalUserMessage.content] + // 这里是故意将上条助手消息的内容(包含图片和文件)作为用户消息发送 + userMessage = [{ ...finalAssistantMessage, role: 'user' } as OpenAI.Responses.EasyInputMessage] + } else { + userMessage.push(finalUserMessage) + } } - // 这里是故意将上条助手消息的内容(包含图片和文件)作为用户消息发送 - userMessage = [{ ...finalAssistantMessage, role: 'user' } as OpenAI.Responses.EasyInputMessage] } // 4. 最终请求消息 @@ -401,7 +434,8 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< tools: !isEmpty(tools) ? tools : undefined, service_tier: this.getServiceTier(model), ...(this.getReasoningEffort(assistant, model) as OpenAI.Reasoning), - ...this.getCustomParameters(assistant) + // 只在对话场景下应用自定义参数,避免影响翻译、总结等其他业务逻辑 + ...(coreRequest.callType === 'chat' ? this.getCustomParameters(assistant) : {}) } const sdkParams: OpenAIResponseSdkParams = streamOutput ? { @@ -423,6 +457,8 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< const outputItems: OpenAI.Responses.ResponseOutputItem[] = [] let hasBeenCollectedToolCalls = false let hasReasoningSummary = false + let isFirstThinkingChunk = true + let isFirstTextChunk = true return () => ({ async transform(chunk: OpenAIResponseSdkRawChunk, controller: TransformStreamDefaultController) { // 处理chunk @@ -434,6 +470,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< switch (output.type) { case 'message': if (output.content[0].type === 'output_text') { + if (isFirstTextChunk) { + controller.enqueue({ + type: ChunkType.TEXT_START + }) + isFirstTextChunk = false + } controller.enqueue({ type: ChunkType.TEXT_DELTA, text: output.content[0].text @@ -450,6 +492,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< } break case 'reasoning': + if (isFirstThinkingChunk) { + controller.enqueue({ + type: ChunkType.THINKING_START + }) + isFirstThinkingChunk = false + } controller.enqueue({ type: ChunkType.THINKING_DELTA, text: output.summary.map((s) => s.text).join('\n') @@ -492,6 +540,10 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< case 'response.output_item.added': if (chunk.item.type === 'function_call') { outputItems.push(chunk.item) + } else if (chunk.item.type === 'web_search_call') { + controller.enqueue({ + type: ChunkType.LLM_WEB_SEARCH_IN_PROGRESS + }) } break case 'response.reasoning_summary_part.added': @@ -505,6 +557,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< hasReasoningSummary = true break case 'response.reasoning_summary_text.delta': + if (isFirstThinkingChunk) { + controller.enqueue({ + type: ChunkType.THINKING_START + }) + isFirstThinkingChunk = false + } controller.enqueue({ type: ChunkType.THINKING_DELTA, text: chunk.delta @@ -530,6 +588,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< }) break case 'response.output_text.delta': { + if (isFirstTextChunk) { + controller.enqueue({ + type: ChunkType.TEXT_START + }) + isFirstTextChunk = false + } controller.enqueue({ type: ChunkType.TEXT_DELTA, text: chunk.delta diff --git a/src/renderer/src/aiCore/clients/ppio/PPIOAPIClient.ts b/src/renderer/src/aiCore/clients/ppio/PPIOAPIClient.ts new file mode 100644 index 0000000000..2b8dec332d --- /dev/null +++ b/src/renderer/src/aiCore/clients/ppio/PPIOAPIClient.ts @@ -0,0 +1,65 @@ +import { isSupportedModel } from '@renderer/config/models' +import { Provider } from '@renderer/types' +import OpenAI from 'openai' + +import { OpenAIAPIClient } from '../openai/OpenAIApiClient' + +export class PPIOAPIClient extends OpenAIAPIClient { + constructor(provider: Provider) { + super(provider) + } + + override async listModels(): Promise { + try { + const sdk = await this.getSdkInstance() + + // PPIO requires three separate requests to get all model types + const [chatModelsResponse, embeddingModelsResponse, rerankerModelsResponse] = await Promise.all([ + // Chat/completion models + sdk.request({ + method: 'get', + path: '/models' + }), + // Embedding models + sdk.request({ + method: 'get', + path: '/models?model_type=embedding' + }), + // Reranker models + sdk.request({ + method: 'get', + path: '/models?model_type=reranker' + }) + ]) + + // Extract models from all responses + // @ts-ignore - PPIO response structure may not be typed + const allModels = [ + ...((chatModelsResponse as any)?.data || []), + ...((embeddingModelsResponse as any)?.data || []), + ...((rerankerModelsResponse as any)?.data || []) + ] + + // Process and standardize model data + const processedModels = allModels.map((model: any) => ({ + id: model.id || model.name, + description: model.description || model.display_name || model.summary, + object: 'model' as const, + owned_by: model.owned_by || model.publisher || model.organization || 'ppio', + created: model.created || Date.now() + })) + + // Clean up model IDs and filter supported models + processedModels.forEach((model) => { + if (model.id) { + model.id = model.id.trim() + } + }) + + return processedModels.filter(isSupportedModel) + } catch (error) { + console.error('Error listing PPIO models:', error) + return [] + } + } +} diff --git a/src/renderer/src/aiCore/clients/types.ts b/src/renderer/src/aiCore/clients/types.ts index ff03f10d38..a6f4adc38d 100644 --- a/src/renderer/src/aiCore/clients/types.ts +++ b/src/renderer/src/aiCore/clients/types.ts @@ -84,6 +84,7 @@ export interface ResponseChunkTransformerContext { isStreaming: boolean isEnabledToolCalling: boolean isEnabledWebSearch: boolean + isEnabledUrlContext: boolean isEnabledReasoning: boolean mcpTools: MCPTool[] provider: Provider diff --git a/src/renderer/src/aiCore/index.ts b/src/renderer/src/aiCore/index.ts index 5b1bb5e181..f9caa80f81 100644 --- a/src/renderer/src/aiCore/index.ts +++ b/src/renderer/src/aiCore/index.ts @@ -8,6 +8,7 @@ import { isEnabledToolUse } from '@renderer/utils/mcp-tools' import { OpenAIAPIClient } from './clients' import { AihubmixAPIClient } from './clients/AihubmixAPIClient' import { AnthropicAPIClient } from './clients/anthropic/AnthropicAPIClient' +import { NewAPIClient } from './clients/NewAPIClient' import { OpenAIResponseAPIClient } from './clients/openai/OpenAIResponseAPIClient' import { CompletionsMiddlewareBuilder } from './middleware/builder' import { MIDDLEWARE_NAME as AbortHandlerMiddlewareName } from './middleware/common/AbortHandlerMiddleware' @@ -48,6 +49,11 @@ export default class AiProvider { if (client instanceof OpenAIResponseAPIClient) { client = client.getClient(model) as BaseApiClient } + } else if (this.apiClient instanceof NewAPIClient) { + client = this.apiClient.getClientForModel(model) + if (client instanceof OpenAIResponseAPIClient) { + client = client.getClient(model) as BaseApiClient + } } else if (this.apiClient instanceof OpenAIResponseAPIClient) { // OpenAIResponseAPIClient: 根据模型特征选择API类型 client = this.apiClient.getClient(model) as BaseApiClient @@ -69,11 +75,12 @@ export default class AiProvider { } else { // Existing logic for other models if (!params.enableReasoning) { - builder.remove(ThinkingTagExtractionMiddlewareName) + // 这里注释掉不会影响正常的关闭思考,可忽略不计的性能下降 + // builder.remove(ThinkingTagExtractionMiddlewareName) builder.remove(ThinkChunkMiddlewareName) } // 注意:用client判断会导致typescript类型收窄 - if (!(this.apiClient instanceof OpenAIAPIClient)) { + if (!(this.apiClient instanceof OpenAIAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) { builder.remove(ThinkingTagExtractionMiddlewareName) } if (!(this.apiClient instanceof AnthropicAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) { @@ -92,6 +99,10 @@ export default class AiProvider { if (params.callType !== 'chat') { builder.remove(AbortHandlerMiddlewareName) } + if (params.callType === 'test') { + builder.remove(ErrorHandlerMiddlewareName) + builder.remove(FinalChunkConsumerMiddlewareName) + } } const middlewares = builder.build() diff --git a/src/renderer/src/aiCore/middleware/common/AbortHandlerMiddleware.ts b/src/renderer/src/aiCore/middleware/common/AbortHandlerMiddleware.ts index 7186cec12f..2acf553533 100644 --- a/src/renderer/src/aiCore/middleware/common/AbortHandlerMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/common/AbortHandlerMiddleware.ts @@ -67,7 +67,12 @@ export const AbortHandlerMiddleware: CompletionsMiddleware = const streamWithAbortHandler = (result.stream as ReadableStream).pipeThrough( new TransformStream({ transform(chunk, controller) { - // 检查 abort 状态 + // 如果已经收到错误块,不再检查 abort 状态 + if (chunk.type === ChunkType.ERROR) { + controller.enqueue(chunk) + return + } + if (abortSignal?.aborted) { // 转换为 ErrorChunk const errorChunk: ErrorChunk = { diff --git a/src/renderer/src/aiCore/middleware/common/FinalChunkConsumerMiddleware.ts b/src/renderer/src/aiCore/middleware/common/FinalChunkConsumerMiddleware.ts index b0b9bd7ce6..80e0cdc5e6 100644 --- a/src/renderer/src/aiCore/middleware/common/FinalChunkConsumerMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/common/FinalChunkConsumerMiddleware.ts @@ -136,7 +136,6 @@ function extractAndAccumulateUsageMetrics(ctx: CompletionsContext, chunk: Generi Logger.debug(`[${MIDDLEWARE_NAME}] First token timestamp: ${ctx._internal.customState.firstTokenTimestamp}`) } if (chunk.type === ChunkType.LLM_RESPONSE_COMPLETE) { - Logger.debug(`[${MIDDLEWARE_NAME}] LLM_RESPONSE_COMPLETE chunk received:`, ctx._internal) // 从LLM_RESPONSE_COMPLETE chunk中提取usage数据 if (chunk.response?.usage) { accumulateUsage(ctx._internal.observer.usage, chunk.response.usage) diff --git a/src/renderer/src/aiCore/middleware/core/McpToolChunkMiddleware.ts b/src/renderer/src/aiCore/middleware/core/McpToolChunkMiddleware.ts index 893018d4c5..b74c4895dc 100644 --- a/src/renderer/src/aiCore/middleware/core/McpToolChunkMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/core/McpToolChunkMiddleware.ts @@ -89,6 +89,11 @@ function createToolHandlingTransform( let hasToolUseResponses = false let streamEnded = false + // 存储已执行的工具结果 + const executedToolResults: SdkMessageParam[] = [] + const executedToolCalls: SdkToolCall[] = [] + const executionPromises: Promise[] = [] + return new TransformStream({ async transform(chunk: GenericChunk, controller) { try { @@ -98,22 +103,64 @@ function createToolHandlingTransform( // 1. 处理Function Call方式的工具调用 if (createdChunk.tool_calls && createdChunk.tool_calls.length > 0) { - toolCalls.push(...createdChunk.tool_calls) hasToolCalls = true + + for (const toolCall of createdChunk.tool_calls) { + toolCalls.push(toolCall) + + const executionPromise = (async () => { + try { + const result = await executeToolCalls( + ctx, + [toolCall], + mcpTools, + allToolResponses, + currentParams.onChunk, + currentParams.assistant.model! + ) + + // 缓存执行结果 + executedToolResults.push(...result.toolResults) + executedToolCalls.push(...result.confirmedToolCalls) + } catch (error) { + console.error(`🔧 [${MIDDLEWARE_NAME}] Error executing tool call asynchronously:`, error) + } + })() + + executionPromises.push(executionPromise) + } } // 2. 处理Tool Use方式的工具调用 if (createdChunk.tool_use_responses && createdChunk.tool_use_responses.length > 0) { - toolUseResponses.push(...createdChunk.tool_use_responses) hasToolUseResponses = true + for (const toolUseResponse of createdChunk.tool_use_responses) { + toolUseResponses.push(toolUseResponse) + const executionPromise = (async () => { + try { + const result = await executeToolUseResponses( + ctx, + [toolUseResponse], // 单个执行 + mcpTools, + allToolResponses, + currentParams.onChunk, + currentParams.assistant.model! + ) + + // 缓存执行结果 + executedToolResults.push(...result.toolResults) + } catch (error) { + console.error(`🔧 [${MIDDLEWARE_NAME}] Error executing tool use response asynchronously:`, error) + // 错误时不影响其他工具的执行 + } + })() + + executionPromises.push(executionPromise) + } } - - // 不转发MCP工具进展chunks,避免重复处理 - return + } else { + controller.enqueue(chunk) } - - // 转发其他所有chunk - controller.enqueue(chunk) } catch (error) { console.error(`🔧 [${MIDDLEWARE_NAME}] Error processing chunk:`, error) controller.error(error) @@ -121,43 +168,33 @@ function createToolHandlingTransform( }, async flush(controller) { - const shouldExecuteToolCalls = hasToolCalls && toolCalls.length > 0 - const shouldExecuteToolUseResponses = hasToolUseResponses && toolUseResponses.length > 0 - - if (!streamEnded && (shouldExecuteToolCalls || shouldExecuteToolUseResponses)) { + // 在流结束时等待所有异步工具执行完成,然后进行递归调用 + if (!streamEnded && (hasToolCalls || hasToolUseResponses)) { streamEnded = true try { - let toolResult: SdkMessageParam[] = [] - - if (shouldExecuteToolCalls) { - toolResult = await executeToolCalls( - ctx, - toolCalls, - mcpTools, - allToolResponses, - currentParams.onChunk, - currentParams.assistant.model! - ) - } else if (shouldExecuteToolUseResponses) { - toolResult = await executeToolUseResponses( - ctx, - toolUseResponses, - mcpTools, - allToolResponses, - currentParams.onChunk, - currentParams.assistant.model! - ) - } - - if (toolResult.length > 0) { + await Promise.all(executionPromises) + if (executedToolResults.length > 0) { const output = ctx._internal.toolProcessingState?.output + const newParams = buildParamsWithToolResults( + ctx, + currentParams, + output, + executedToolResults, + executedToolCalls + ) + + // 在递归调用前通知UI开始新的LLM响应处理 + if (currentParams.onChunk) { + currentParams.onChunk({ + type: ChunkType.LLM_RESPONSE_CREATED + }) + } - const newParams = buildParamsWithToolResults(ctx, currentParams, output, toolResult, toolCalls) await executeWithToolHandling(newParams, depth + 1) } } catch (error) { - console.error(`🔧 [${MIDDLEWARE_NAME}] Error in tool processing:`, error) + Logger.error(`🔧 [${MIDDLEWARE_NAME}] Error in tool processing:`, error) controller.error(error) } finally { hasToolCalls = false @@ -178,8 +215,7 @@ async function executeToolCalls( allToolResponses: MCPToolResponse[], onChunk: CompletionsParams['onChunk'], model: Model -): Promise { - // 转换为MCPToolResponse格式 +): Promise<{ toolResults: SdkMessageParam[]; confirmedToolCalls: SdkToolCall[] }> { const mcpToolResponses: ToolCallResponse[] = toolCalls .map((toolCall) => { const mcpTool = ctx.apiClientInstance.convertSdkToolCallToMcp(toolCall, mcpTools) @@ -192,11 +228,11 @@ async function executeToolCalls( if (mcpToolResponses.length === 0) { console.warn(`🔧 [${MIDDLEWARE_NAME}] No valid MCP tool responses to execute`) - return [] + return { toolResults: [], confirmedToolCalls: [] } } // 使用现有的parseAndCallTools函数执行工具 - const toolResults = await parseAndCallTools( + const { toolResults, confirmedToolResponses } = await parseAndCallTools( mcpToolResponses, allToolResponses, onChunk, @@ -204,10 +240,26 @@ async function executeToolCalls( return ctx.apiClientInstance.convertMcpToolResponseToSdkMessageParam(mcpToolResponse, resp, model) }, model, - mcpTools + mcpTools, + ctx._internal?.flowControl?.abortSignal ) - return toolResults + // 找出已确认工具对应的原始toolCalls + const confirmedToolCalls = toolCalls.filter((toolCall) => { + return confirmedToolResponses.find((confirmed) => { + // 根据不同的ID字段匹配原始toolCall + return ( + ('name' in toolCall && + (toolCall.name?.includes(confirmed.tool.name) || toolCall.name?.includes(confirmed.tool.id))) || + confirmed.tool.name === toolCall.id || + confirmed.tool.id === toolCall.id || + ('toolCallId' in confirmed && confirmed.toolCallId === toolCall.id) || + ('function' in toolCall && toolCall.function.name.toLowerCase().includes(confirmed.tool.name.toLowerCase())) + ) + }) + }) + + return { toolResults, confirmedToolCalls } } /** @@ -221,9 +273,9 @@ async function executeToolUseResponses( allToolResponses: MCPToolResponse[], onChunk: CompletionsParams['onChunk'], model: Model -): Promise { +): Promise<{ toolResults: SdkMessageParam[] }> { // 直接使用parseAndCallTools函数处理已经解析好的ToolUseResponse - const toolResults = await parseAndCallTools( + const { toolResults } = await parseAndCallTools( toolUseResponses, allToolResponses, onChunk, @@ -231,10 +283,11 @@ async function executeToolUseResponses( return ctx.apiClientInstance.convertMcpToolResponseToSdkMessageParam(mcpToolResponse, resp, model) }, model, - mcpTools + mcpTools, + ctx._internal?.flowControl?.abortSignal ) - return toolResults + return { toolResults } } /** @@ -245,7 +298,7 @@ function buildParamsWithToolResults( currentParams: CompletionsParams, output: SdkRawOutput | string | undefined, toolResults: SdkMessageParam[], - toolCalls: SdkToolCall[] + confirmedToolCalls: SdkToolCall[] ): CompletionsParams { // 获取当前已经转换好的reqMessages,如果没有则使用原始messages const currentReqMessages = getCurrentReqMessages(ctx) @@ -253,7 +306,7 @@ function buildParamsWithToolResults( const apiClient = ctx.apiClientInstance // 从回复中构建助手消息 - const newReqMessages = apiClient.buildSdkMessages(currentReqMessages, output, toolResults, toolCalls) + const newReqMessages = apiClient.buildSdkMessages(currentReqMessages, output, toolResults, confirmedToolCalls) if (output && ctx._internal.toolProcessingState) { ctx._internal.toolProcessingState.output = undefined diff --git a/src/renderer/src/aiCore/middleware/core/ResponseTransformMiddleware.ts b/src/renderer/src/aiCore/middleware/core/ResponseTransformMiddleware.ts index eccbe86bdd..5477aa6557 100644 --- a/src/renderer/src/aiCore/middleware/core/ResponseTransformMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/core/ResponseTransformMiddleware.ts @@ -55,6 +55,7 @@ export const ResponseTransformMiddleware: CompletionsMiddleware = isStreaming: params.streamOutput || false, isEnabledToolCalling: (params.mcpTools && params.mcpTools.length > 0) || false, isEnabledWebSearch: params.enableWebSearch || false, + isEnabledUrlContext: params.enableUrlContext || false, isEnabledReasoning: params.enableReasoning || false, mcpTools: params.mcpTools || [], provider: ctx.apiClientInstance?.provider diff --git a/src/renderer/src/aiCore/middleware/core/TextChunkMiddleware.ts b/src/renderer/src/aiCore/middleware/core/TextChunkMiddleware.ts index 2a3255356f..cfaf70299f 100644 --- a/src/renderer/src/aiCore/middleware/core/TextChunkMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/core/TextChunkMiddleware.ts @@ -1,5 +1,5 @@ import Logger from '@renderer/config/logger' -import { ChunkType, TextDeltaChunk } from '@renderer/types/chunk' +import { ChunkType } from '@renderer/types/chunk' import { CompletionsParams, CompletionsResult, GenericChunk } from '../schemas' import { CompletionsContext, CompletionsMiddleware } from '../types' @@ -38,45 +38,46 @@ export const TextChunkMiddleware: CompletionsMiddleware = // 用于跨chunk的状态管理 let accumulatedTextContent = '' - let hasEnqueue = false const enhancedTextStream = resultFromUpstream.pipeThrough( new TransformStream({ transform(chunk: GenericChunk, controller) { if (chunk.type === ChunkType.TEXT_DELTA) { - const textChunk = chunk as TextDeltaChunk - accumulatedTextContent += textChunk.text + accumulatedTextContent += chunk.text // 处理 onResponse 回调 - 发送增量文本更新 if (params.onResponse) { params.onResponse(accumulatedTextContent, false) } - // 创建新的chunk,包含处理后的文本 - controller.enqueue(chunk) - } else if (accumulatedTextContent) { - if (chunk.type !== ChunkType.LLM_RESPONSE_COMPLETE) { - controller.enqueue(chunk) - hasEnqueue = true - } - const finalText = accumulatedTextContent - ctx._internal.customState!.accumulatedText = finalText - if (ctx._internal.toolProcessingState && !ctx._internal.toolProcessingState?.output) { - ctx._internal.toolProcessingState.output = finalText - } - - // 处理 onResponse 回调 - 发送最终完整文本 - if (params.onResponse) { - params.onResponse(finalText, true) - } - controller.enqueue({ - type: ChunkType.TEXT_COMPLETE, - text: finalText + ...chunk, + text: accumulatedTextContent // 增量更新 }) - accumulatedTextContent = '' - if (!hasEnqueue) { + } else if (accumulatedTextContent && chunk.type !== ChunkType.TEXT_START) { + ctx._internal.customState!.accumulatedText = accumulatedTextContent + if (ctx._internal.toolProcessingState && !ctx._internal.toolProcessingState?.output) { + ctx._internal.toolProcessingState.output = accumulatedTextContent + } + + if (chunk.type === ChunkType.LLM_RESPONSE_COMPLETE) { + // 处理 onResponse 回调 - 发送最终完整文本 + if (params.onResponse) { + params.onResponse(accumulatedTextContent, true) + } + + controller.enqueue({ + type: ChunkType.TEXT_COMPLETE, + text: accumulatedTextContent + }) + controller.enqueue(chunk) + } else { + controller.enqueue({ + type: ChunkType.TEXT_COMPLETE, + text: accumulatedTextContent + }) controller.enqueue(chunk) } + accumulatedTextContent = '' } else { // 其他类型的chunk直接传递 controller.enqueue(chunk) diff --git a/src/renderer/src/aiCore/middleware/core/ThinkChunkMiddleware.ts b/src/renderer/src/aiCore/middleware/core/ThinkChunkMiddleware.ts index b0df8313a5..957b925400 100644 --- a/src/renderer/src/aiCore/middleware/core/ThinkChunkMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/core/ThinkChunkMiddleware.ts @@ -62,10 +62,11 @@ export const ThinkChunkMiddleware: CompletionsMiddleware = // 更新思考时间并传递 const enhancedChunk: ThinkingDeltaChunk = { ...thinkingChunk, + text: accumulatedThinkingContent, thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0 } controller.enqueue(enhancedChunk) - } else if (hasThinkingContent && thinkingStartTime > 0) { + } else if (hasThinkingContent && thinkingStartTime > 0 && chunk.type !== ChunkType.THINKING_START) { // 收到任何非THINKING_DELTA的chunk时,如果有累积的思考内容,生成THINKING_COMPLETE const thinkingCompleteChunk: ThinkingCompleteChunk = { type: ChunkType.THINKING_COMPLETE, diff --git a/src/renderer/src/aiCore/middleware/core/WebSearchMiddleware.ts b/src/renderer/src/aiCore/middleware/core/WebSearchMiddleware.ts index 97261e3d52..d4c8f71eff 100644 --- a/src/renderer/src/aiCore/middleware/core/WebSearchMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/core/WebSearchMiddleware.ts @@ -1,5 +1,5 @@ import { ChunkType } from '@renderer/types/chunk' -import { smartLinkConverter } from '@renderer/utils/linkConverter' +import { flushLinkConverterBuffer, smartLinkConverter } from '@renderer/utils/linkConverter' import { CompletionsParams, CompletionsResult, GenericChunk } from '../schemas' import { CompletionsContext, CompletionsMiddleware } from '../types' @@ -42,20 +42,51 @@ export const WebSearchMiddleware: CompletionsMiddleware = const providerType = model.provider || 'openai' // 使用当前可用的Web搜索结果进行链接转换 const text = chunk.text - const processedText = smartLinkConverter(text, providerType, isFirstChunk) + const result = smartLinkConverter( + text, + providerType, + isFirstChunk, + ctx._internal.webSearchState!.results + ) if (isFirstChunk) { isFirstChunk = false } - controller.enqueue({ - ...chunk, - text: processedText - }) + + // - 如果有内容被缓冲,说明convertLinks正在等待后续chunk,不使用原文本避免重复 + // - 如果没有内容被缓冲且结果为空,可能是其他处理问题,使用原文本作为安全回退 + let finalText: string + if (result.hasBufferedContent) { + // 有内容被缓冲,使用处理后的结果(可能为空,等待后续chunk) + finalText = result.text + } else { + // 没有内容被缓冲,可以安全使用回退逻辑 + finalText = result.text || text + } + + // 只有当finalText不为空时才发送chunk + if (finalText) { + controller.enqueue({ + ...chunk, + text: finalText + }) + } } else if (chunk.type === ChunkType.LLM_WEB_SEARCH_COMPLETE) { // 暂存Web搜索结果用于链接完善 ctx._internal.webSearchState!.results = chunk.llm_web_search // 将Web搜索完成事件继续传递下去 controller.enqueue(chunk) + } else if (chunk.type === ChunkType.LLM_RESPONSE_COMPLETE) { + // 流结束时,清空链接转换器的buffer并处理剩余内容 + const remainingText = flushLinkConverterBuffer() + if (remainingText) { + controller.enqueue({ + type: ChunkType.TEXT_DELTA, + text: remainingText + }) + } + // 继续传递LLM_RESPONSE_COMPLETE事件 + controller.enqueue(chunk) } else { controller.enqueue(chunk) } diff --git a/src/renderer/src/aiCore/middleware/feat/ImageGenerationMiddleware.ts b/src/renderer/src/aiCore/middleware/feat/ImageGenerationMiddleware.ts index d0a4dc4903..ceb8d791d7 100644 --- a/src/renderer/src/aiCore/middleware/feat/ImageGenerationMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/feat/ImageGenerationMiddleware.ts @@ -1,7 +1,9 @@ import { BaseApiClient } from '@renderer/aiCore/clients/BaseApiClient' import { isDedicatedImageGenerationModel } from '@renderer/config/models' +import FileManager from '@renderer/services/FileManager' import { ChunkType } from '@renderer/types/chunk' import { findImageBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find' +import { defaultTimeout } from '@shared/config/constant' import OpenAI from 'openai' import { toFile } from 'openai/uploads' @@ -46,7 +48,7 @@ export const ImageGenerationMiddleware: CompletionsMiddleware = const userImages = await Promise.all( userImageBlocks.map(async (block) => { if (!block.file) return null - const binaryData: Uint8Array = await window.api.file.binaryImage(block.file.id) + const binaryData: Uint8Array = await FileManager.readBinaryImage(block.file) const mimeType = `${block.file.type}/${block.file.ext.slice(1)}` return await toFile(new Blob([binaryData]), block.file.origin_name || 'image.png', { type: mimeType }) }) @@ -73,8 +75,7 @@ export const ImageGenerationMiddleware: CompletionsMiddleware = const startTime = Date.now() let response: OpenAI.Images.ImagesResponse - - const options = { signal, timeout: 300_000 } + const options = { signal, timeout: defaultTimeout } if (imageFiles.length > 0) { response = await sdk.images.edit( diff --git a/src/renderer/src/aiCore/middleware/feat/ThinkingTagExtractionMiddleware.ts b/src/renderer/src/aiCore/middleware/feat/ThinkingTagExtractionMiddleware.ts index 440de40045..425f29c705 100644 --- a/src/renderer/src/aiCore/middleware/feat/ThinkingTagExtractionMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/feat/ThinkingTagExtractionMiddleware.ts @@ -1,5 +1,11 @@ import { Model } from '@renderer/types' -import { ChunkType, TextDeltaChunk, ThinkingCompleteChunk, ThinkingDeltaChunk } from '@renderer/types/chunk' +import { + ChunkType, + TextDeltaChunk, + ThinkingCompleteChunk, + ThinkingDeltaChunk, + ThinkingStartChunk +} from '@renderer/types/chunk' import { TagConfig, TagExtractor } from '@renderer/utils/tagExtraction' import Logger from 'electron-log/renderer' @@ -11,11 +17,13 @@ export const MIDDLEWARE_NAME = 'ThinkingTagExtractionMiddleware' // 不同模型的思考标签配置 const reasoningTags: TagConfig[] = [ { openingTag: '', closingTag: '', separator: '\n' }, + { openingTag: '', closingTag: '', separator: '\n' }, { openingTag: '###Thinking', closingTag: '###Response', separator: '\n' } ] const getAppropriateTag = (model?: Model): TagConfig => { if (model?.id?.includes('qwen3')) return reasoningTags[0] + if (model?.id?.includes('gemini-2.5')) return reasoningTags[1] // 可以在这里添加更多模型特定的标签配置 return reasoningTags[0] // 默认使用 标签 } @@ -57,6 +65,8 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware = let hasThinkingContent = false let thinkingStartTime = 0 + let isFirstTextChunk = true + let accumulatedThinkingContent = '' const processedStream = resultFromUpstream.pipeThrough( new TransformStream({ transform(chunk: GenericChunk, controller) { @@ -67,7 +77,7 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware = const extractionResults = tagExtractor.processText(textChunk.text) for (const extractionResult of extractionResults) { - if (extractionResult.complete && extractionResult.tagContentExtracted) { + if (extractionResult.complete && extractionResult.tagContentExtracted?.trim()) { // 生成 THINKING_COMPLETE 事件 const thinkingCompleteChunk: ThinkingCompleteChunk = { type: ChunkType.THINKING_COMPLETE, @@ -85,15 +95,27 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware = if (!hasThinkingContent) { hasThinkingContent = true thinkingStartTime = Date.now() + controller.enqueue({ + type: ChunkType.THINKING_START + } as ThinkingStartChunk) } - const thinkingDeltaChunk: ThinkingDeltaChunk = { - type: ChunkType.THINKING_DELTA, - text: extractionResult.content, - thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0 + if (extractionResult.content?.trim()) { + accumulatedThinkingContent += extractionResult.content + const thinkingDeltaChunk: ThinkingDeltaChunk = { + type: ChunkType.THINKING_DELTA, + text: accumulatedThinkingContent, + thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0 + } + controller.enqueue(thinkingDeltaChunk) } - controller.enqueue(thinkingDeltaChunk) } else { + if (isFirstTextChunk) { + controller.enqueue({ + type: ChunkType.TEXT_START + }) + isFirstTextChunk = false + } // 发送清理后的文本内容 const cleanTextChunk: TextDeltaChunk = { ...textChunk, @@ -103,7 +125,7 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware = } } } - } else { + } else if (chunk.type !== ChunkType.TEXT_START) { // 其他类型的chunk直接传递(包括 THINKING_DELTA, THINKING_COMPLETE 等) controller.enqueue(chunk) } diff --git a/src/renderer/src/aiCore/middleware/feat/ToolUseExtractionMiddleware.ts b/src/renderer/src/aiCore/middleware/feat/ToolUseExtractionMiddleware.ts index 5f444953a9..b53d7348f1 100644 --- a/src/renderer/src/aiCore/middleware/feat/ToolUseExtractionMiddleware.ts +++ b/src/renderer/src/aiCore/middleware/feat/ToolUseExtractionMiddleware.ts @@ -22,7 +22,8 @@ const TOOL_USE_TAG_CONFIG: TagConfig = { * 1. 从文本流中检测并提取 标签 * 2. 解析工具调用信息并转换为 ToolUseResponse 格式 * 3. 生成 MCP_TOOL_CREATED chunk 供 McpToolChunkMiddleware 处理 - * 4. 清理文本流,移除工具使用标签但保留正常文本 + * 4. 丢弃 tool_use 之后的所有内容(助手幻觉) + * 5. 清理文本流,移除工具使用标签但保留正常文本 * * 注意:此中间件只负责提取和转换,实际工具调用由 McpToolChunkMiddleware 处理 */ @@ -32,13 +33,10 @@ export const ToolUseExtractionMiddleware: CompletionsMiddleware = async (ctx: CompletionsContext, params: CompletionsParams): Promise => { const mcpTools = params.mcpTools || [] - // 如果没有工具,直接调用下一个中间件 if (!mcpTools || mcpTools.length === 0) return next(ctx, params) - // 调用下游中间件 const result = await next(ctx, params) - // 响应后处理:处理工具使用标签提取 if (result.stream) { const resultFromUpstream = result.stream as ReadableStream @@ -60,7 +58,9 @@ function createToolUseExtractionTransform( _ctx: CompletionsContext, mcpTools: MCPTool[] ): TransformStream { - const tagExtractor = new TagExtractor(TOOL_USE_TAG_CONFIG) + const toolUseExtractor = new TagExtractor(TOOL_USE_TAG_CONFIG) + let hasAnyToolUse = false + let toolCounter = 0 return new TransformStream({ async transform(chunk: GenericChunk, controller) { @@ -68,30 +68,37 @@ function createToolUseExtractionTransform( // 处理文本内容,检测工具使用标签 if (chunk.type === ChunkType.TEXT_DELTA) { const textChunk = chunk as TextDeltaChunk - const extractionResults = tagExtractor.processText(textChunk.text) - for (const result of extractionResults) { + // 处理 tool_use 标签 + const toolUseResults = toolUseExtractor.processText(textChunk.text) + + for (const result of toolUseResults) { if (result.complete && result.tagContentExtracted) { // 提取到完整的工具使用内容,解析并转换为 SDK ToolCall 格式 - const toolUseResponses = parseToolUse(result.tagContentExtracted, mcpTools) + const toolUseResponses = parseToolUse(result.tagContentExtracted, mcpTools, toolCounter) + toolCounter += toolUseResponses.length if (toolUseResponses.length > 0) { - // 生成 MCP_TOOL_CREATED chunk,复用现有的处理流程 + // 生成 MCP_TOOL_CREATED chunk const mcpToolCreatedChunk: MCPToolCreatedChunk = { type: ChunkType.MCP_TOOL_CREATED, tool_use_responses: toolUseResponses } controller.enqueue(mcpToolCreatedChunk) + + // 标记已有工具调用 + hasAnyToolUse = true } } else if (!result.isTagContent && result.content) { - // 发送标签外的正常文本内容 - const cleanTextChunk: TextDeltaChunk = { - ...textChunk, - text: result.content + if (!hasAnyToolUse) { + const cleanTextChunk: TextDeltaChunk = { + ...textChunk, + text: result.content + } + controller.enqueue(cleanTextChunk) } - controller.enqueue(cleanTextChunk) } - // 注意:标签内的内容不会作为TEXT_DELTA转发,避免重复显示 + // tool_use 标签内的内容不转发,避免重复显示 } return } @@ -105,16 +112,17 @@ function createToolUseExtractionTransform( }, async flush(controller) { - // 检查是否有未完成的标签内容 - const finalResult = tagExtractor.finalize() - if (finalResult && finalResult.tagContentExtracted) { - const toolUseResponses = parseToolUse(finalResult.tagContentExtracted, mcpTools) + // 检查是否有未完成的 tool_use 标签内容 + const finalToolUseResult = toolUseExtractor.finalize() + if (finalToolUseResult && finalToolUseResult.tagContentExtracted) { + const toolUseResponses = parseToolUse(finalToolUseResult.tagContentExtracted, mcpTools, toolCounter) if (toolUseResponses.length > 0) { const mcpToolCreatedChunk: MCPToolCreatedChunk = { type: ChunkType.MCP_TOOL_CREATED, tool_use_responses: toolUseResponses } controller.enqueue(mcpToolCreatedChunk) + hasAnyToolUse = true } } } diff --git a/src/renderer/src/aiCore/middleware/schemas.ts b/src/renderer/src/aiCore/middleware/schemas.ts index 33d9816b4f..2e60214625 100644 --- a/src/renderer/src/aiCore/middleware/schemas.ts +++ b/src/renderer/src/aiCore/middleware/schemas.ts @@ -23,7 +23,7 @@ export interface CompletionsParams { * 'generate': 生成 * 'check': API检查 */ - callType?: 'chat' | 'translate' | 'summary' | 'search' | 'generate' | 'check' + callType?: 'chat' | 'translate' | 'summary' | 'search' | 'generate' | 'check' | 'test' // 基础对话数据 messages: Message[] | string // 联合类型方便判断是否为空 @@ -49,6 +49,7 @@ export interface CompletionsParams { // 功能开关 streamOutput: boolean enableWebSearch?: boolean + enableUrlContext?: boolean enableReasoning?: boolean enableGenerateImage?: boolean diff --git a/src/renderer/src/assets/fonts/country-flag-fonts/TwemojiCountryFlags.woff2 b/src/renderer/src/assets/fonts/country-flag-fonts/TwemojiCountryFlags.woff2 new file mode 100644 index 0000000000..7f5bebba53 Binary files /dev/null and b/src/renderer/src/assets/fonts/country-flag-fonts/TwemojiCountryFlags.woff2 differ diff --git a/src/renderer/src/assets/fonts/country-flag-fonts/flag.css b/src/renderer/src/assets/fonts/country-flag-fonts/flag.css new file mode 100644 index 0000000000..b73d4ae76f --- /dev/null +++ b/src/renderer/src/assets/fonts/country-flag-fonts/flag.css @@ -0,0 +1,13 @@ +@font-face { + font-family: 'Twemoji Country Flags'; + unicode-range: + U+1F1E6-1F1FF, U+1F3F4, U+E0062-E0063, U+E0065, U+E0067, U+E006C, U+E006E, U+E0073-E0074, U+E0077, U+E007F; + /*https://github.com/beyondkmp/country-flag-emoji-polyfill/blob/master/font/TwemojiCountryFlags.woff2 */ + src: url('TwemojiCountryFlags.woff2') format('woff2'); + font-display: swap; +} + +/* 国旗字体样式类 */ +.country-flag-font { + font-family: 'Twemoji Country Flags', 'Apple Color Emoji', 'Segoe UI Emoji', sans-serif; +} diff --git a/src/renderer/src/assets/fonts/icon-fonts/iconfont.css b/src/renderer/src/assets/fonts/icon-fonts/iconfont.css index 71573edbf2..ae76c0026c 100644 --- a/src/renderer/src/assets/fonts/icon-fonts/iconfont.css +++ b/src/renderer/src/assets/fonts/icon-fonts/iconfont.css @@ -1,6 +1,6 @@ @font-face { font-family: 'iconfont'; /* Project id 4753420 */ - src: url('iconfont.woff2?t=1742184675192') format('woff2'); + src: url('iconfont.woff2?t=1742793497518') format('woff2'); } .iconfont { @@ -11,6 +11,18 @@ -moz-osx-font-smoothing: grayscale; } +.icon-plugin:before { + content: '\e612'; +} + +.icon-tools:before { + content: '\e762'; +} + +.icon-OCRshibie:before { + content: '\e658'; +} + .icon-obsidian:before { content: '\e677'; } diff --git a/src/renderer/src/assets/fonts/icon-fonts/iconfont.woff2 b/src/renderer/src/assets/fonts/icon-fonts/iconfont.woff2 index 9c2ec4a51d..9581311b4c 100644 Binary files a/src/renderer/src/assets/fonts/icon-fonts/iconfont.woff2 and b/src/renderer/src/assets/fonts/icon-fonts/iconfont.woff2 differ diff --git a/src/renderer/src/assets/images/ocr/doc2x.png b/src/renderer/src/assets/images/ocr/doc2x.png new file mode 100644 index 0000000000..4b0d0efa36 Binary files /dev/null and b/src/renderer/src/assets/images/ocr/doc2x.png differ diff --git a/src/renderer/src/assets/images/ocr/mineru.jpg b/src/renderer/src/assets/images/ocr/mineru.jpg new file mode 100644 index 0000000000..c4295d1f65 Binary files /dev/null and b/src/renderer/src/assets/images/ocr/mineru.jpg differ diff --git a/src/renderer/src/assets/images/providers/macos.svg b/src/renderer/src/assets/images/providers/macos.svg new file mode 100644 index 0000000000..3385e73504 --- /dev/null +++ b/src/renderer/src/assets/images/providers/macos.svg @@ -0,0 +1,7 @@ + + \ No newline at end of file diff --git a/src/renderer/src/assets/images/providers/newapi.png b/src/renderer/src/assets/images/providers/newapi.png new file mode 100644 index 0000000000..f62bfd57f1 Binary files /dev/null and b/src/renderer/src/assets/images/providers/newapi.png differ diff --git a/src/renderer/src/assets/images/providers/ph8.png b/src/renderer/src/assets/images/providers/ph8.png new file mode 100644 index 0000000000..a93a89d29a Binary files /dev/null and b/src/renderer/src/assets/images/providers/ph8.png differ diff --git a/src/renderer/src/assets/styles/ant.scss b/src/renderer/src/assets/styles/ant.scss index 225cbe8a9d..efd8fe3de2 100644 --- a/src/renderer/src/assets/styles/ant.scss +++ b/src/renderer/src/assets/styles/ant.scss @@ -110,6 +110,9 @@ border-radius: 10px; border: 0.5px solid var(--color-border); padding: 0 0 8px 0; + .ant-modal-close { + margin-right: 2px; + } .ant-modal-header { padding: 16px 16px 0 16px; border-radius: 10px; @@ -133,17 +136,16 @@ } } -.ant-collapse { +.ant-collapse:not(.ant-collapse-ghost) { border: 1px solid var(--color-border); .ant-color-picker & { border: none; } -} - -.ant-collapse-content { - border-top: 0.5px solid var(--color-border) !important; - .ant-color-picker & { - border-top: none !important; + .ant-collapse-content { + border-top: 0.5px solid var(--color-border) !important; + .ant-color-picker & { + border-top: none !important; + } } } diff --git a/src/renderer/src/assets/styles/color.scss b/src/renderer/src/assets/styles/color.scss index ce7e9cefe9..224566e199 100644 --- a/src/renderer/src/assets/styles/color.scss +++ b/src/renderer/src/assets/styles/color.scss @@ -44,7 +44,7 @@ --color-reference-text: #ffffff; --color-reference-background: #0b0e12; - --color-list-item: #222; + --color-list-item: #252525; --color-list-item-hover: #1e1e1e; --modal-background: #111111; @@ -72,6 +72,10 @@ --chat-text-user: var(--color-black); --list-item-border-radius: 20px; + + --color-status-success: #52c41a; + --color-status-error: #ff4d4f; + --color-status-warning: #faad14; } [theme-mode='light'] { diff --git a/src/renderer/src/assets/styles/font.scss b/src/renderer/src/assets/styles/font.scss index 9d2d139b53..75a0e6fc8b 100644 --- a/src/renderer/src/assets/styles/font.scss +++ b/src/renderer/src/assets/styles/font.scss @@ -10,3 +10,11 @@ --code-font-family: 'Cascadia Code', 'Fira Code', 'Consolas', Menlo, Courier, monospace; } + +// Windows系统专用字体配置 +body[os='windows'] { + --font-family: + 'Twemoji Country Flags', Ubuntu, -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, Roboto, Oxygen, + Cantarell, 'Open Sans', 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', + 'Segoe UI Symbol', 'Noto Color Emoji'; +} diff --git a/src/renderer/src/assets/styles/index.scss b/src/renderer/src/assets/styles/index.scss index c4bd23d1fc..0a6696bd9b 100644 --- a/src/renderer/src/assets/styles/index.scss +++ b/src/renderer/src/assets/styles/index.scss @@ -7,6 +7,7 @@ @use './animation.scss'; @import '../fonts/icon-fonts/iconfont.css'; @import '../fonts/ubuntu/ubuntu.css'; +@import '../fonts/country-flag-fonts/flag.css'; *, *::before, @@ -138,7 +139,7 @@ ul { } } .message-content-container { - border-radius: 10px 0 10px 10px; + border-radius: 10px; padding: 10px 16px 10px 16px; background-color: var(--chat-background-user); align-self: self-end; @@ -169,7 +170,7 @@ ul { } } -.lucide { +.lucide:not(.lucide-custom) { color: var(--color-icon); } diff --git a/src/renderer/src/assets/styles/markdown.scss b/src/renderer/src/assets/styles/markdown.scss index eea9070cae..e861af3618 100644 --- a/src/renderer/src/assets/styles/markdown.scss +++ b/src/renderer/src/assets/styles/markdown.scss @@ -19,12 +19,13 @@ h4, h5, h6 { - margin: 1em 0 1em 0; + margin: 1.5em 0 1em 0; + line-height: 1.3; font-weight: bold; - font-family: var(--font-family); } h1 { + margin-top: 0; font-size: 2em; border-bottom: 0.5px solid var(--color-border); padding-bottom: 0.3em; @@ -53,8 +54,9 @@ } p { - margin: 1em 0; + margin: 1.3em 0; white-space: pre-wrap; + line-height: 1.6; &:last-child { margin-bottom: 5px; @@ -82,7 +84,7 @@ li { margin-bottom: 0.5em; pre { - margin: 1.5em 0; + margin: 1.5em 0 !important; } &::marker { color: var(--color-text-3); @@ -108,6 +110,7 @@ li code { background: var(--color-background-mute); padding: 3px 5px; + margin: 0 2px; border-radius: 5px; word-break: keep-all; white-space: pre; @@ -120,11 +123,9 @@ pre { border-radius: 8px; overflow-x: auto; - font-family: 'Fira Code', 'Courier New', Courier, monospace; + font-family: var(--code-font-family); background-color: var(--color-background-mute); - &:has(.mermaid), - &:has(.plantuml-preview), - &:has(.svg-preview) { + &:has(.special-preview) { background-color: transparent; } &:not(pre pre) { @@ -148,16 +149,19 @@ } blockquote { - margin: 1em 0; - padding-left: 1em; - color: var(--color-text-light); - border-left: 4px solid var(--color-border); - font-family: var(--font-family); + margin: 1.5em 0; + padding: 1em 1.5em; + background-color: var(--color-background-soft); + border-left: 4px solid var(--color-primary); + border-radius: 0 8px 8px 0; + font-style: italic; + position: relative; } table { --table-border-radius: 8px; - margin: 1em 0; + margin: 2em 0; + font-size: 0.9em; width: 100%; border-radius: var(--table-border-radius); overflow: hidden; @@ -182,13 +186,18 @@ th { background-color: var(--color-background-mute); - font-weight: bold; - font-family: var(--font-family); + font-weight: 600; + text-align: left; + } + + tr:hover { + background-color: var(--color-background-soft); } img { max-width: 100%; height: auto; + margin: 10px 0; } a, @@ -323,6 +332,13 @@ mjx-container { margin-top: -2px; } +/* Shiki 相关样式 */ +.shiki { + font-family: var(--code-font-family); + // 保持行高为初始值,在 shiki 代码块中处理 + line-height: initial; +} + /* CodeMirror 相关样式 */ .cm-editor { border-radius: inherit; diff --git a/src/renderer/src/assets/styles/scrollbar.scss b/src/renderer/src/assets/styles/scrollbar.scss index 818c082b7e..21039de9c2 100644 --- a/src/renderer/src/assets/styles/scrollbar.scss +++ b/src/renderer/src/assets/styles/scrollbar.scss @@ -1,11 +1,16 @@ :root { - --color-scrollbar-thumb: rgba(255, 255, 255, 0.15); - --color-scrollbar-thumb-hover: rgba(255, 255, 255, 0.2); + --color-scrollbar-thumb-dark: rgba(255, 255, 255, 0.15); + --color-scrollbar-thumb-dark-hover: rgba(255, 255, 255, 0.2); + --color-scrollbar-thumb-light: rgba(0, 0, 0, 0.15); + --color-scrollbar-thumb-light-hover: rgba(0, 0, 0, 0.2); + + --color-scrollbar-thumb: var(--color-scrollbar-thumb-dark); + --color-scrollbar-thumb-hover: var(--color-scrollbar-thumb-dark-hover); } body[theme-mode='light'] { - --color-scrollbar-thumb: rgba(0, 0, 0, 0.15); - --color-scrollbar-thumb-hover: rgba(0, 0, 0, 0.2); + --color-scrollbar-thumb: var(--color-scrollbar-thumb-light); + --color-scrollbar-thumb-hover: var(--color-scrollbar-thumb-light-hover); } /* 全局初始化滚动条样式 */ @@ -34,3 +39,21 @@ pre:not(.shiki)::-webkit-scrollbar-thumb { background: rgba(0, 0, 0, 0.15); } } + +.shiki-dark { + --color-scrollbar-thumb: var(--color-scrollbar-thumb-dark); + --color-scrollbar-thumb-hover: var(--color-scrollbar-thumb-dark-hover); +} + +.shiki-light { + --color-scrollbar-thumb: var(--color-scrollbar-thumb-light); + --color-scrollbar-thumb-hover: var(--color-scrollbar-thumb-light-hover); +} + +/* 用于截图时隐藏滚动条 + * FIXME: 临时方案,因为 html-to-image 没有正确处理伪元素。 + */ +.hide-scrollbar, +.hide-scrollbar * { + scrollbar-width: none !important; +} diff --git a/src/renderer/src/assets/styles/selection-toolbar.scss b/src/renderer/src/assets/styles/selection-toolbar.scss index bfe329c696..23f0edfb34 100644 --- a/src/renderer/src/assets/styles/selection-toolbar.scss +++ b/src/renderer/src/assets/styles/selection-toolbar.scss @@ -18,25 +18,37 @@ html { --selection-toolbar-logo-display: flex; // values: flex | none --selection-toolbar-logo-size: 22px; // default: 22px - --selection-toolbar-logo-margin: 0 0 0 5px; // default: 0 0 05px + --selection-toolbar-logo-border-width: 0.5px 0 0.5px 0.5px; // default: none + --selection-toolbar-logo-border-style: solid; // default: none + --selection-toolbar-logo-border-color: rgba(255, 255, 255, 0.2); + --selection-toolbar-logo-margin: 0; // default: 0 + --selection-toolbar-logo-padding: 0 6px 0 8px; // default: 0 4px 0 8px + --selection-toolbar-logo-background: transparent; // default: transparent // DO NOT MODIFY THESE VALUES, IF YOU DON'T KNOW WHAT YOU ARE DOING - --selection-toolbar-padding: 2px 4px 2px 2px; // default: 2px 4px 2px 2px + --selection-toolbar-padding: 0; // default: 0 --selection-toolbar-margin: 2px 3px 5px 3px; // default: 2px 3px 5px 3px // ------------------------------------------------------------ - --selection-toolbar-border-radius: 6px; - --selection-toolbar-border: 1px solid rgba(55, 55, 55, 0.5); + --selection-toolbar-border-radius: 10px; + --selection-toolbar-border: none; --selection-toolbar-box-shadow: 0px 2px 3px rgba(50, 50, 50, 0.3); --selection-toolbar-background: rgba(20, 20, 20, 0.95); // Buttons + --selection-toolbar-buttons-border-width: 0.5px 0.5px 0.5px 0; + --selection-toolbar-buttons-border-style: solid; + --selection-toolbar-buttons-border-color: rgba(255, 255, 255, 0.2); + --selection-toolbar-buttons-border-radius: 0 var(--selection-toolbar-border-radius) + var(--selection-toolbar-border-radius) 0; --selection-toolbar-button-icon-size: 16px; // default: 16px - --selection-toolbar-button-text-margin: 0 0 0 3px; // default: 0 0 0 3px - --selection-toolbar-button-margin: 0 2px; // default: 0 2px - --selection-toolbar-button-padding: 4px 6px; // default: 4px 6px - --selection-toolbar-button-border-radius: 4px; // default: 4px + --selection-toolbar-button-direction: row; // default: row | column + --selection-toolbar-button-text-margin: 0 0 0 0; // default: 0 0 0 0 + --selection-toolbar-button-margin: 0; // default: 0 + --selection-toolbar-button-padding: 0 8px; // default: 0 8px + --selection-toolbar-button-last-padding: 0 12px 0 8px; + --selection-toolbar-button-border-radius: 0; // default: 0 --selection-toolbar-button-border: none; // default: none --selection-toolbar-button-box-shadow: none; // default: none @@ -45,14 +57,19 @@ html { --selection-toolbar-button-text-color-hover: var(--selection-toolbar-color-primary); --selection-toolbar-button-icon-color-hover: var(--selection-toolbar-color-primary); --selection-toolbar-button-bgcolor: transparent; // default: transparent - --selection-toolbar-button-bgcolor-hover: #222222; + --selection-toolbar-button-bgcolor-hover: #333333; } [theme-mode='light'] { - --selection-toolbar-border: 1px solid rgba(200, 200, 200, 0.5); - --selection-toolbar-box-shadow: 0px 2px 3px rgba(50, 50, 50, 0.3); + --selection-toolbar-border: none; + --selection-toolbar-box-shadow: 0px 2px 3px rgba(50, 50, 50, 0.1); --selection-toolbar-background: rgba(245, 245, 245, 0.95); + // Buttons + --selection-toolbar-buttons-border-color: rgba(0, 0, 0, 0.08); + + --selection-toolbar-logo-border-color: rgba(0, 0, 0, 0.08); + --selection-toolbar-button-text-color: rgba(0, 0, 0, 1); --selection-toolbar-button-icon-color: var(--selection-toolbar-button-text-color); --selection-toolbar-button-text-color-hover: var(--selection-toolbar-color-primary); diff --git a/src/renderer/src/components/Alert/OpenAIAlert.tsx b/src/renderer/src/components/Alert/OpenAIAlert.tsx index 455ab62987..07579fe87a 100644 --- a/src/renderer/src/components/Alert/OpenAIAlert.tsx +++ b/src/renderer/src/components/Alert/OpenAIAlert.tsx @@ -1,24 +1,28 @@ import { Alert } from 'antd' +import { t } from 'i18next' import { useEffect, useState } from 'react' -import { useTranslation } from 'react-i18next' const LOCALSTORAGE_KEY = 'openai_alert_closed' -const OpenAIAlert = () => { - const { t } = useTranslation() +interface Props { + message?: string + key?: string +} + +const OpenAIAlert = ({ message = t('settings.provider.openai.alert'), key = LOCALSTORAGE_KEY }: Props) => { const [visible, setVisible] = useState(false) useEffect(() => { - const closed = localStorage.getItem(LOCALSTORAGE_KEY) + const closed = localStorage.getItem(key) setVisible(!closed) - }, []) + }, [key]) if (!visible) return null return ( { localStorage.setItem(LOCALSTORAGE_KEY, '1') diff --git a/src/renderer/src/components/Avatar/EmojiAvatar.tsx b/src/renderer/src/components/Avatar/EmojiAvatar.tsx index 553869698a..e01024735a 100644 --- a/src/renderer/src/components/Avatar/EmojiAvatar.tsx +++ b/src/renderer/src/components/Avatar/EmojiAvatar.tsx @@ -44,6 +44,7 @@ const StyledEmojiAvatar = styled.div<{ $size: number; $fontSize: number }>` height: ${(props) => props.$size}px; font-size: ${(props) => props.$fontSize}px; transition: opacity 0.3s ease; + &:hover { opacity: 0.8; } diff --git a/src/renderer/src/components/CodeBlockView/CodePreview.tsx b/src/renderer/src/components/CodeBlockView/CodePreview.tsx index 55a10d5535..c78b4af99c 100644 --- a/src/renderer/src/components/CodeBlockView/CodePreview.tsx +++ b/src/renderer/src/components/CodeBlockView/CodePreview.tsx @@ -1,311 +1,298 @@ -import { CodeTool, TOOL_SPECS, useCodeTool } from '@renderer/components/CodeToolbar' +import { TOOL_SPECS, useCodeTool } from '@renderer/components/CodeToolbar' import { useCodeStyle } from '@renderer/context/CodeStyleProvider' +import { useCodeHighlight } from '@renderer/hooks/useCodeHighlight' import { useSettings } from '@renderer/hooks/useSettings' import { uuid } from '@renderer/utils' import { getReactStyleFromToken } from '@renderer/utils/shiki' +import { useVirtualizer } from '@tanstack/react-virtual' +import { debounce } from 'lodash' import { ChevronsDownUp, ChevronsUpDown, Text as UnWrapIcon, WrapText as WrapIcon } from 'lucide-react' -import React, { memo, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react' +import React, { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { ThemedToken } from 'shiki/core' import styled from 'styled-components' -interface CodePreviewProps { - children: string +import { BasicPreviewProps } from './types' + +interface CodePreviewProps extends BasicPreviewProps { language: string - setTools?: (value: React.SetStateAction) => void } +const MAX_COLLAPSE_HEIGHT = 350 + /** * Shiki 流式代码高亮组件 - * * - 通过 shiki tokenizer 处理流式响应,高性能 - * - 进入视口后触发高亮,改善页面内有大量长代码块时的响应 + * - 使用虚拟滚动和按需高亮,改善页面内有大量长代码块时的响应 * - 并发安全 */ const CodePreview = ({ children, language, setTools }: CodePreviewProps) => { const { codeShowLineNumbers, fontSize, codeCollapsible, codeWrappable } = useSettings() - const { activeShikiTheme, highlightStreamingCode, cleanupTokenizers } = useCodeStyle() - const [isExpanded, setIsExpanded] = useState(!codeCollapsible) - const [isUnwrapped, setIsUnwrapped] = useState(!codeWrappable) - const [tokenLines, setTokenLines] = useState([]) - const [isInViewport, setIsInViewport] = useState(false) - const codeContainerRef = useRef(null) - const processingRef = useRef(false) - const latestRequestedContentRef = useRef(null) + const { getShikiPreProperties, isShikiThemeDark } = useCodeStyle() + const [expandOverride, setExpandOverride] = useState(!codeCollapsible) + const [unwrapOverride, setUnwrapOverride] = useState(!codeWrappable) + const shikiThemeRef = useRef(null) + const scrollerRef = useRef(null) const callerId = useRef(`${Date.now()}-${uuid()}`).current - const shikiThemeRef = useRef(activeShikiTheme) + + const rawLines = useMemo(() => (typeof children === 'string' ? children.trimEnd().split('\n') : []), [children]) const { t } = useTranslation() - const { registerTool, removeTool } = useCodeTool(setTools) // 展开/折叠工具 useEffect(() => { registerTool({ ...TOOL_SPECS.expand, - icon: isExpanded ? : , - tooltip: isExpanded ? t('code_block.collapse') : t('code_block.expand'), + icon: expandOverride ? : , + tooltip: expandOverride ? t('code_block.collapse') : t('code_block.expand'), visible: () => { - const scrollHeight = codeContainerRef.current?.scrollHeight - return codeCollapsible && (scrollHeight ?? 0) > 350 + const scrollHeight = scrollerRef.current?.scrollHeight + return codeCollapsible && (scrollHeight ?? 0) > MAX_COLLAPSE_HEIGHT }, - onClick: () => setIsExpanded((prev) => !prev) + onClick: () => setExpandOverride((prev) => !prev) }) return () => removeTool(TOOL_SPECS.expand.id) - }, [codeCollapsible, isExpanded, registerTool, removeTool, t]) + }, [codeCollapsible, expandOverride, registerTool, removeTool, t]) // 自动换行工具 useEffect(() => { registerTool({ ...TOOL_SPECS.wrap, - icon: isUnwrapped ? : , - tooltip: isUnwrapped ? t('code_block.wrap.on') : t('code_block.wrap.off'), + icon: unwrapOverride ? : , + tooltip: unwrapOverride ? t('code_block.wrap.on') : t('code_block.wrap.off'), visible: () => codeWrappable, - onClick: () => setIsUnwrapped((prev) => !prev) + onClick: () => setUnwrapOverride((prev) => !prev) }) return () => removeTool(TOOL_SPECS.wrap.id) - }, [codeWrappable, isUnwrapped, registerTool, removeTool, t]) + }, [codeWrappable, unwrapOverride, registerTool, removeTool, t]) - // 更新展开状态 + // 重置用户操作(可以考虑移除,保持用户操作结果) useEffect(() => { - setIsExpanded(!codeCollapsible) + setExpandOverride(!codeCollapsible) }, [codeCollapsible]) - // 更新换行状态 + // 重置用户操作(可以考虑移除,保持用户操作结果) useEffect(() => { - setIsUnwrapped(!codeWrappable) + setUnwrapOverride(!codeWrappable) }, [codeWrappable]) - const highlightCode = useCallback(async () => { - const currentContent = typeof children === 'string' ? children.trimEnd() : '' + const shouldCollapse = useMemo(() => codeCollapsible && !expandOverride, [codeCollapsible, expandOverride]) + const shouldWrap = useMemo(() => codeWrappable && !unwrapOverride, [codeWrappable, unwrapOverride]) - // 记录最新要处理的内容,为了保证最终状态正确 - latestRequestedContentRef.current = currentContent - - // 如果正在处理,先跳出,等到完成后会检查是否有新内容 - if (processingRef.current) return - - processingRef.current = true - - try { - // 循环处理,确保会处理最新内容 - while (latestRequestedContentRef.current !== null) { - const contentToProcess = latestRequestedContentRef.current - latestRequestedContentRef.current = null // 标记开始处理 - - // 传入完整内容,让 ShikiStreamService 检测变化并处理增量高亮 - const result = await highlightStreamingCode(contentToProcess, language, callerId) - - // 如有结果,更新 tokenLines - if (result.lines.length > 0 || result.recall !== 0) { - setTokenLines((prev) => { - return result.recall === -1 - ? result.lines - : [...prev.slice(0, Math.max(0, prev.length - result.recall)), ...result.lines] - }) - } - } - } finally { - processingRef.current = false - } - }, [highlightStreamingCode, language, callerId, children]) - - // 主题变化时强制重新高亮 - useEffect(() => { - if (shikiThemeRef.current !== activeShikiTheme) { - shikiThemeRef.current = activeShikiTheme - cleanupTokenizers(callerId) - setTokenLines([]) - } - }, [activeShikiTheme, callerId, cleanupTokenizers]) - - // 组件卸载时清理资源 - useEffect(() => { - return () => cleanupTokenizers(callerId) - }, [callerId, cleanupTokenizers]) - - // 视口检测逻辑,进入视口后触发第一次代码高亮 - useEffect(() => { - const codeElement = codeContainerRef.current - if (!codeElement) return - - const observer = new IntersectionObserver( - (entries) => { - if (entries[0].intersectionRatio > 0) { - setIsInViewport(true) - observer.disconnect() - } - }, - { - rootMargin: '50px 0px 50px 0px' - } - ) - - observer.observe(codeElement) - return () => observer.disconnect() - }, []) // 只执行一次 - - // 触发代码高亮 - useEffect(() => { - if (!isInViewport) return - - setTimeout(highlightCode, 0) - }, [isInViewport, highlightCode]) - - const lastDigitsRef = useRef(1) - - useLayoutEffect(() => { - const container = codeContainerRef.current - if (!container || !codeShowLineNumbers) return - - const digits = Math.max(tokenLines.length.toString().length, 1) - if (digits === lastDigitsRef.current) return - - const gutterWidth = digits * 0.6 - container.style.setProperty('--gutter-width', `${gutterWidth}rem`) - lastDigitsRef.current = digits - }, [codeShowLineNumbers, tokenLines.length]) - - const hasHighlightedCode = tokenLines.length > 0 - - return ( - - {hasHighlightedCode ? ( - - ) : ( - {children} - )} - + // 计算行号数字位数 + const gutterDigits = useMemo( + () => (codeShowLineNumbers ? Math.max(rawLines.length.toString().length, 1) : 0), + [codeShowLineNumbers, rawLines.length] ) -} - -interface ShikiTokensRendererProps { - language: string - tokenLines: ThemedToken[][] - showLineNumbers?: boolean -} - -/** - * 渲染 Shiki 高亮后的 tokens - * - * 独立出来,方便将来做 virtual list - */ -const ShikiTokensRenderer: React.FC = memo(({ language, tokenLines, showLineNumbers }) => { - const { getShikiPreProperties } = useCodeStyle() - const rendererRef = useRef(null) // 设置 pre 标签属性 useLayoutEffect(() => { getShikiPreProperties(language).then((properties) => { - const pre = rendererRef.current - if (pre) { - pre.className = properties.class - pre.style.cssText = properties.style - pre.tabIndex = properties.tabindex + const shikiTheme = shikiThemeRef.current + if (shikiTheme) { + shikiTheme.className = `${properties.class || 'shiki'}` + // 滚动条适应 shiki 主题变化而非应用主题 + shikiTheme.classList.add(isShikiThemeDark ? 'shiki-dark' : 'shiki-light') + + if (properties.style) { + shikiTheme.style.cssText += `${properties.style}` + } + shikiTheme.tabIndex = properties.tabindex } }) - }, [language, getShikiPreProperties]) + }, [language, getShikiPreProperties, isShikiThemeDark]) + + // Virtualizer 配置 + const getScrollElement = useCallback(() => scrollerRef.current, []) + const getItemKey = useCallback((index: number) => `${callerId}-${index}`, [callerId]) + // `line-height: 1.6` 为全局样式,但是为了避免测量误差在这里取整 + const estimateSize = useCallback(() => Math.round((fontSize - 1) * 1.6), [fontSize]) + + // 创建 virtualizer 实例 + const virtualizer = useVirtualizer({ + count: rawLines.length, + getScrollElement, + getItemKey, + estimateSize, + overscan: 20 + }) + + const virtualItems = virtualizer.getVirtualItems() + + // 使用代码高亮 Hook + const { tokenLines, highlightLines } = useCodeHighlight({ + rawLines, + language, + callerId + }) + + // 防抖高亮提高流式响应的性能,数字大一点也不会影响用户体验 + const debouncedHighlightLines = useMemo(() => debounce(highlightLines, 300), [highlightLines]) + + // 渐进式高亮 + useEffect(() => { + if (virtualItems.length > 0 && shikiThemeRef.current) { + const lastIndex = virtualItems[virtualItems.length - 1].index + debouncedHighlightLines(lastIndex + 1) + } + }, [virtualItems, debouncedHighlightLines]) return ( -
-      
-        {tokenLines.map((lineTokens, lineIndex) => (
-          
-            {showLineNumbers && {lineIndex + 1}}
-            
-              {lineTokens.map((token, tokenIndex) => (
-                
-                  {token.content}
-                
-              ))}
-            
-          
-        ))}
-      
-    
+
+ +
+
+ {virtualizer.getVirtualItems().map((virtualItem) => ( +
+ +
+ ))} +
+
+
+
) -}) - -const ContentContainer = styled.div<{ - $wrap: boolean - $fadeIn: boolean -}>` - position: relative; - overflow: auto; - border-radius: inherit; - margin-top: 0; - - /* gutter 宽度默认值 */ - --gutter-width: 0.6rem; - - .shiki { - padding: 1em; - border-radius: inherit; - - code { - display: flex; - flex-direction: column; - - .line { - display: flex; - align-items: flex-start; - min-height: 1.3rem; - - .line-number { - width: var(--gutter-width); - text-align: right; - opacity: 0.35; - margin-right: 1rem; - user-select: none; - flex-shrink: 0; - overflow: hidden; - line-height: inherit; - font-family: inherit; - font-variant-numeric: tabular-nums; - } - - .line-content { - flex: 1; - - * { - overflow-wrap: ${(props) => (props.$wrap ? 'break-word' : 'normal')}; - white-space: ${(props) => (props.$wrap ? 'pre-wrap' : 'pre')}; - } - } - } - } - } - - @keyframes contentFadeIn { - from { - opacity: 0; - } - to { - opacity: 1; - } - } - - animation: ${(props) => (props.$fadeIn ? 'contentFadeIn 0.1s ease-in forwards' : 'none')}; -` - -const CodePlaceholder = styled.div` - display: block; - opacity: 0.1; - white-space: pre-wrap; - word-break: break-all; - overflow-x: hidden; - min-height: 1.3rem; -` +} CodePreview.displayName = 'CodePreview' +/** + * 补全代码行 tokens,把原始内容拼接到高亮内容之后,确保渲染出整行来。 + */ +function completeLineTokens(themedTokens: ThemedToken[], rawLine: string): ThemedToken[] { + // 如果出现空行,补一个空格保证行高 + if (rawLine.length === 0) { + return [ + { + content: ' ', + offset: 0, + color: 'inherit', + bgColor: 'inherit', + htmlStyle: { + opacity: '0.35' + } + } + ] + } + + const themedContent = themedTokens.map((token) => token.content).join('') + const extraContent = rawLine.slice(themedContent.length) + + // 已有内容已经全部高亮,直接返回 + if (!extraContent) return themedTokens + + // 补全剩余内容 + return [ + ...themedTokens, + { + content: extraContent, + offset: themedContent.length, + color: 'inherit', + bgColor: 'inherit', + htmlStyle: { + opacity: '0.35' + } + } + ] +} + +interface VirtualizedRowData { + rawLine: string + tokenLine?: ThemedToken[] + showLineNumbers: boolean +} + +/** + * 单行代码渲染 + */ +const VirtualizedRow = memo( + ({ rawLine, tokenLine, showLineNumbers, index }: VirtualizedRowData & { index: number }) => { + return ( +
+ {showLineNumbers && {index + 1}} + + {completeLineTokens(tokenLine ?? [], rawLine).map((token, tokenIndex) => ( + + {token.content} + + ))} + +
+ ) + } +) + +VirtualizedRow.displayName = 'VirtualizedRow' + +const ScrollContainer = styled.div<{ + $wrap?: boolean + $lineHeight?: number +}>` + display: block; + overflow-x: auto; + position: relative; + border-radius: inherit; + padding: 0.5em 1em; + + .line { + display: flex; + align-items: flex-start; + width: 100%; + line-height: ${(props) => props.$lineHeight}px; + + .line-number { + width: var(--gutter-width, 1.2ch); + text-align: right; + opacity: 0.35; + margin-right: 1rem; + user-select: none; + flex-shrink: 0; + overflow: hidden; + font-family: inherit; + font-variant-numeric: tabular-nums; + } + + .line-content { + flex: 1; + * { + white-space: ${(props) => (props.$wrap ? 'pre-wrap' : 'pre')}; + overflow-wrap: ${(props) => (props.$wrap ? 'break-word' : 'normal')}; + } + } + } +` + export default memo(CodePreview) diff --git a/src/renderer/src/components/CodeBlockView/GraphvizPreview.tsx b/src/renderer/src/components/CodeBlockView/GraphvizPreview.tsx new file mode 100644 index 0000000000..452ed1261b --- /dev/null +++ b/src/renderer/src/components/CodeBlockView/GraphvizPreview.tsx @@ -0,0 +1,102 @@ +import { usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar' +import SvgSpinners180Ring from '@renderer/components/Icons/SvgSpinners180Ring' +import { AsyncInitializer } from '@renderer/utils/asyncInitializer' +import { Flex, Spin } from 'antd' +import { debounce } from 'lodash' +import React, { memo, startTransition, useCallback, useEffect, useMemo, useRef, useState } from 'react' +import styled from 'styled-components' + +import PreviewError from './PreviewError' +import { BasicPreviewProps } from './types' + +// 管理 viz 实例 +const vizInitializer = new AsyncInitializer(async () => { + const module = await import('@viz-js/viz') + return await module.instance() +}) + +/** 预览 Graphviz 图表 + * 通过防抖渲染提供比较统一的体验,减少闪烁。 + */ +const GraphvizPreview: React.FC = ({ children, setTools }) => { + const graphvizRef = useRef(null) + const [error, setError] = useState(null) + const [isLoading, setIsLoading] = useState(false) + + // 使用通用图像工具 + const { handleZoom, handleCopyImage, handleDownload } = usePreviewToolHandlers(graphvizRef, { + imgSelector: 'svg', + prefix: 'graphviz', + enableWheelZoom: true + }) + + // 使用工具栏 + usePreviewTools({ + setTools, + handleZoom, + handleCopyImage, + handleDownload + }) + + // 实际的渲染函数 + const renderGraphviz = useCallback(async (content: string) => { + if (!content || !graphvizRef.current) return + + try { + setIsLoading(true) + + const viz = await vizInitializer.get() + const svgElement = viz.renderSVGElement(content) + + // 清空容器并添加新的 SVG + graphvizRef.current.innerHTML = '' + graphvizRef.current.appendChild(svgElement) + + // 渲染成功,清除错误记录 + setError(null) + } catch (error) { + setError((error as Error).message || 'DOT syntax error or rendering failed') + } finally { + setIsLoading(false) + } + }, []) + + // debounce 渲染 + const debouncedRender = useMemo( + () => + debounce((content: string) => { + startTransition(() => renderGraphviz(content)) + }, 300), + [renderGraphviz] + ) + + // 触发渲染 + useEffect(() => { + if (children) { + setIsLoading(true) + debouncedRender(children) + } else { + debouncedRender.cancel() + setIsLoading(false) + } + + return () => { + debouncedRender.cancel() + } + }, [children, debouncedRender]) + + return ( + }> + + {error && {error}} + + + + ) +} + +const StyledGraphviz = styled.div` + overflow: auto; +` + +export default memo(GraphvizPreview) diff --git a/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx b/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx deleted file mode 100644 index 0dbb0aabb2..0000000000 --- a/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx +++ /dev/null @@ -1,70 +0,0 @@ -import { ExpandOutlined, LinkOutlined } from '@ant-design/icons' -import { AppLogo } from '@renderer/config/env' -import { useMinappPopup } from '@renderer/hooks/useMinappPopup' -import { extractTitle } from '@renderer/utils/formats' -import { Button } from 'antd' -import { FC } from 'react' -import { useTranslation } from 'react-i18next' -import styled from 'styled-components' - -interface Props { - html: string -} - -const Artifacts: FC = ({ html }) => { - const { t } = useTranslation() - const { openMinapp } = useMinappPopup() - - /** - * 在应用内打开 - */ - const handleOpenInApp = async () => { - const path = await window.api.file.create('artifacts-preview.html') - await window.api.file.write(path, html) - const filePath = `file://${path}` - const title = extractTitle(html) || 'Artifacts ' + t('chat.artifacts.button.preview') - openMinapp({ - id: 'artifacts-preview', - name: title, - logo: AppLogo, - url: filePath - }) - } - - /** - * 外部链接打开 - */ - const handleOpenExternal = async () => { - const path = await window.api.file.create('artifacts-preview.html') - await window.api.file.write(path, html) - const filePath = `file://${path}` - - if (window.api.shell && window.api.shell.openExternal) { - window.api.shell.openExternal(filePath) - } else { - console.error(t('artifacts.preview.openExternal.error.content')) - } - } - - return ( - - - - - - ) -} - -const Container = styled.div` - margin: 10px; - display: flex; - flex-direction: row; - gap: 8px; - padding-bottom: 10px; -` - -export default Artifacts diff --git a/src/renderer/src/components/CodeBlockView/HtmlArtifactsCard.tsx b/src/renderer/src/components/CodeBlockView/HtmlArtifactsCard.tsx new file mode 100644 index 0000000000..b3389570c1 --- /dev/null +++ b/src/renderer/src/components/CodeBlockView/HtmlArtifactsCard.tsx @@ -0,0 +1,338 @@ +import { CodeOutlined, LinkOutlined } from '@ant-design/icons' +import { useTheme } from '@renderer/context/ThemeProvider' +import { ThemeMode } from '@renderer/types' +import { extractTitle } from '@renderer/utils/formats' +import { Button } from 'antd' +import { Code, Download, Globe, Sparkles } from 'lucide-react' +import { FC, useMemo, useState } from 'react' +import { useTranslation } from 'react-i18next' +import { ClipLoader } from 'react-spinners' +import styled, { keyframes } from 'styled-components' + +import HtmlArtifactsPopup from './HtmlArtifactsPopup' + +const HTML_VOID_ELEMENTS = new Set([ + 'area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'link', + 'meta', + 'param', + 'source', + 'track', + 'wbr' +]) + +const HTML_COMPLETION_PATTERNS = [ + /<\/html\s*>/i, + //i, + /<\/div\s*>/i, + /<\/script\s*>/i, + /<\/style\s*>/i +] + +interface Props { + html: string +} + +function hasUnmatchedTags(html: string): boolean { + const stack: string[] = [] + const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9]*)[^>]*>/g + let match + + while ((match = tagRegex.exec(html)) !== null) { + const [fullTag, tagName] = match + const isClosing = fullTag.startsWith('') || HTML_VOID_ELEMENTS.has(tagName.toLowerCase()) + + if (isSelfClosing) continue + + if (isClosing) { + if (stack.length === 0 || stack.pop() !== tagName.toLowerCase()) { + return true + } + } else { + stack.push(tagName.toLowerCase()) + } + } + + return stack.length > 0 +} + +function checkIsStreaming(html: string): boolean { + if (!html?.trim()) return false + + const trimmed = html.trim() + + // 快速检查:如果有明显的完成标志,直接返回false + for (const pattern of HTML_COMPLETION_PATTERNS) { + if (pattern.test(trimmed)) { + // 特殊情况:同时有DOCTYPE和 + if (trimmed.includes('/i.test(trimmed)) { + return false + } + // 如果只是以结尾,也认为是完成的 + if (/<\/html\s*>$/i.test(trimmed)) { + return false + } + } + } + + // 检查未完成的标志 + const hasIncompleteTag = /<[^>]*$/.test(trimmed) + const hasUnmatched = hasUnmatchedTags(trimmed) + + if (hasIncompleteTag || hasUnmatched) return true + + // 对于简单片段,如果长度较短且没有明显结束标志,可能还在生成 + const hasStructureTags = /<(html|body|head)[^>]*>/i.test(trimmed) + if (!hasStructureTags && trimmed.length < 500) { + return !HTML_COMPLETION_PATTERNS.some((pattern) => pattern.test(trimmed)) + } + + return false +} + +const getTerminalStyles = (theme: ThemeMode) => ({ + background: theme === 'dark' ? '#1e1e1e' : '#f0f0f0', + color: theme === 'dark' ? '#cccccc' : '#333333', + promptColor: theme === 'dark' ? '#00ff00' : '#007700' +}) + +const HtmlArtifactsCard: FC = ({ html }) => { + const { t } = useTranslation() + const title = extractTitle(html) || 'HTML Artifacts' + const [isPopupOpen, setIsPopupOpen] = useState(false) + const { theme } = useTheme() + + const htmlContent = html || '' + const hasContent = htmlContent.trim().length > 0 + const isStreaming = useMemo(() => checkIsStreaming(htmlContent), [htmlContent]) + + const handleOpenExternal = async () => { + const path = await window.api.file.createTempFile('artifacts-preview.html') + await window.api.file.write(path, htmlContent) + const filePath = `file://${path}` + + if (window.api.shell?.openExternal) { + window.api.shell.openExternal(filePath) + } else { + console.error(t('artifacts.preview.openExternal.error.content')) + } + } + + const handleDownload = async () => { + const fileName = `${title.replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s+/g, '-') || 'html-artifact'}.html` + await window.api.file.save(fileName, htmlContent) + window.message.success({ content: t('message.download.success'), key: 'download' }) + } + + return ( + <> + +
+ + {isStreaming ? : } + + + {title} + + + HTML + + +
+ + {isStreaming && !hasContent ? ( + + + {t('html_artifacts.generating_content', 'Generating content...')} + + ) : isStreaming && hasContent ? ( + <> + + + + $ + + {htmlContent.trim().split('\n').slice(-3).join('\n')} + + + + + + + + + + ) : ( + + + + + + )} + +
+ + setIsPopupOpen(false)} /> + + ) +} + +const Container = styled.div<{ $isStreaming: boolean }>` + background: var(--color-background); + border: 1px solid var(--color-border); + border-radius: 8px; + overflow: hidden; + margin: 10px 0; +` + +const GeneratingContainer = styled.div` + display: flex; + justify-content: center; + align-items: center; + gap: 8px; + padding: 20px; + min-height: 78px; +` + +const GeneratingText = styled.div` + font-size: 14px; + color: var(--color-text-secondary); +` + +const Header = styled.div` + display: flex; + align-items: center; + gap: 12px; + padding: 20px 24px 16px; + background: var(--color-background-soft); + border-bottom: 1px solid var(--color-border); + border-radius: 8px 8px 0 0; +` + +const IconWrapper = styled.div<{ $isStreaming: boolean }>` + display: flex; + align-items: center; + justify-content: center; + width: 40px; + height: 40px; + background: ${(props) => + props.$isStreaming + ? 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)' + : 'linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%)'}; + border-radius: 12px; + color: white; + box-shadow: ${(props) => + props.$isStreaming ? '0 4px 6px -1px rgba(245, 158, 11, 0.3)' : '0 4px 6px -1px rgba(59, 130, 246, 0.3)'}; + transition: background 0.3s ease; +` + +const TitleSection = styled.div` + flex: 1; + display: flex; + flex-direction: column; + gap: 6px; +` + +const Title = styled.h3` + margin: 0 !important; + font-size: 16px; + font-weight: 600; + color: var(--color-text); + line-height: 1.4; +` + +const TypeBadge = styled.div` + display: inline-flex; + align-items: center; + gap: 4px; + padding: 3px 6px; + background: var(--color-background-mute); + border: 1px solid var(--color-border); + border-radius: 6px; + font-size: 10px; + font-weight: 500; + color: var(--color-text-secondary); + width: fit-content; +` + +const Content = styled.div` + padding: 0; + background: var(--color-background); +` + +const ButtonContainer = styled.div` + margin: 10px 16px !important; + display: flex; + flex-direction: row; + gap: 8px; +` + +const TerminalPreview = styled.div<{ $theme: ThemeMode }>` + margin: 16px; + background: ${(props) => getTerminalStyles(props.$theme).background}; + border-radius: 8px; + overflow: hidden; + font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace; +` + +const TerminalContent = styled.div<{ $theme: ThemeMode }>` + padding: 12px; + background: ${(props) => getTerminalStyles(props.$theme).background}; + color: ${(props) => getTerminalStyles(props.$theme).color}; + font-size: 13px; + line-height: 1.4; + min-height: 80px; +` + +const TerminalLine = styled.div` + display: flex; + align-items: flex-start; + gap: 8px; +` + +const TerminalCodeLine = styled.span<{ $theme: ThemeMode }>` + flex: 1; + white-space: pre-wrap; + word-break: break-word; + color: ${(props) => getTerminalStyles(props.$theme).color}; + background-color: transparent !important; +` + +const TerminalPrompt = styled.span<{ $theme: ThemeMode }>` + color: ${(props) => getTerminalStyles(props.$theme).promptColor}; + font-weight: bold; + flex-shrink: 0; +` + +const blinkAnimation = keyframes` + 0%, 50% { opacity: 1; } + 51%, 100% { opacity: 0; } +` + +const TerminalCursor = styled.span<{ $theme: ThemeMode }>` + display: inline-block; + width: 2px; + height: 16px; + background: ${(props) => getTerminalStyles(props.$theme).promptColor}; + animation: ${blinkAnimation} 1s infinite; + margin-left: 2px; +` + +export default HtmlArtifactsCard diff --git a/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx b/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx new file mode 100644 index 0000000000..98c90d4faf --- /dev/null +++ b/src/renderer/src/components/CodeBlockView/HtmlArtifactsPopup.tsx @@ -0,0 +1,352 @@ +import CodeEditor from '@renderer/components/CodeEditor' +import { isLinux, isMac, isWin } from '@renderer/config/constant' +import { classNames } from '@renderer/utils' +import { Button, Modal } from 'antd' +import { Code, Maximize2, Minimize2, Monitor, MonitorSpeaker, X } from 'lucide-react' +import { useEffect, useRef, useState } from 'react' +import { useTranslation } from 'react-i18next' +import styled from 'styled-components' + +interface HtmlArtifactsPopupProps { + open: boolean + title: string + html: string + onClose: () => void +} + +type ViewMode = 'split' | 'code' | 'preview' + +const HtmlArtifactsPopup: React.FC = ({ open, title, html, onClose }) => { + const { t } = useTranslation() + const [viewMode, setViewMode] = useState('split') + const [currentHtml, setCurrentHtml] = useState(html) + const [isFullscreen, setIsFullscreen] = useState(false) + + // 预览刷新相关状态 + const [previewHtml, setPreviewHtml] = useState(html) + const intervalRef = useRef(null) + const latestHtmlRef = useRef(html) + + // 当外部html更新时,同步更新内部状态 + useEffect(() => { + setCurrentHtml(html) + latestHtmlRef.current = html + }, [html]) + + // 当内部编辑的html更新时,更新引用 + useEffect(() => { + latestHtmlRef.current = currentHtml + }, [currentHtml]) + + // 2秒定时检查并刷新预览(仅在内容变化时) + useEffect(() => { + if (!open) return + + // 立即设置初始预览内容 + setPreviewHtml(currentHtml) + + // 设置定时器,每2秒检查一次内容是否有变化 + intervalRef.current = setInterval(() => { + if (latestHtmlRef.current !== previewHtml) { + setPreviewHtml(latestHtmlRef.current) + } + }, 2000) + + // 清理函数 + return () => { + if (intervalRef.current) { + clearInterval(intervalRef.current) + } + } + }, [open, previewHtml]) + + // 全屏时防止 body 滚动 + useEffect(() => { + if (!open || !isFullscreen) return + + const body = document.body + const originalOverflow = body.style.overflow + body.style.overflow = 'hidden' + + return () => { + body.style.overflow = originalOverflow + } + }, [isFullscreen, open]) + + const showCode = viewMode === 'split' || viewMode === 'code' + const showPreview = viewMode === 'split' || viewMode === 'preview' + + const renderHeader = () => ( + setIsFullscreen(!isFullscreen)} className={classNames({ drag: isFullscreen })}> + + {title} + + + + + } + onClick={() => setViewMode('split')}> + {t('html_artifacts.split')} + + } + onClick={() => setViewMode('code')}> + {t('html_artifacts.code')} + + } + onClick={() => setViewMode('preview')}> + {t('html_artifacts.preview')} + + + + + + + + + ) + } + ] + + const rowSelection = { + selectedRowKeys, + onChange: (selectedRowKeys: React.Key[]) => { + setSelectedRowKeys(selectedRowKeys) + } + } + + return ( + } onClick={fetchBackupFiles} disabled={loading}> + {t('settings.data.local.backup.manager.refresh')} + , + , + + ]}> + + + ) +} diff --git a/src/renderer/src/components/LocalBackupModals.tsx b/src/renderer/src/components/LocalBackupModals.tsx new file mode 100644 index 0000000000..420c9c2f37 --- /dev/null +++ b/src/renderer/src/components/LocalBackupModals.tsx @@ -0,0 +1,98 @@ +import { backupToLocal } from '@renderer/services/BackupService' +import { Button, Input, Modal } from 'antd' +import dayjs from 'dayjs' +import { useCallback, useState } from 'react' +import { useTranslation } from 'react-i18next' + +interface LocalBackupModalProps { + isModalVisible: boolean + handleBackup: () => void + handleCancel: () => void + backuping: boolean + customFileName: string + setCustomFileName: (value: string) => void +} + +export function LocalBackupModal({ + isModalVisible, + handleBackup, + handleCancel, + backuping, + customFileName, + setCustomFileName +}: LocalBackupModalProps) { + const { t } = useTranslation() + + return ( + + {t('common.cancel')} + , + + ]}> + setCustomFileName(e.target.value)} + placeholder={t('settings.data.local.backup.modal.filename.placeholder')} + /> + + ) +} + +// Hook for backup modal +export function useLocalBackupModal(localBackupDir: string | undefined) { + const [isModalVisible, setIsModalVisible] = useState(false) + const [backuping, setBackuping] = useState(false) + const [customFileName, setCustomFileName] = useState('') + + const handleCancel = () => { + setIsModalVisible(false) + } + + const showBackupModal = useCallback(async () => { + // 获取默认文件名 + const deviceType = await window.api.system.getDeviceType() + const hostname = await window.api.system.getHostname() + const timestamp = dayjs().format('YYYYMMDDHHmmss') + const defaultFileName = `cherry-studio.${timestamp}.${hostname}.${deviceType}.zip` + setCustomFileName(defaultFileName) + setIsModalVisible(true) + }, []) + + const handleBackup = async () => { + if (!localBackupDir) { + setIsModalVisible(false) + return + } + + setBackuping(true) + try { + await backupToLocal({ + showMessage: true, + customFileName: customFileName || undefined + }) + setIsModalVisible(false) + } catch (error) { + console.error('[LocalBackupModal] Backup failed:', error) + } finally { + setBackuping(false) + } + } + + return { + isModalVisible, + handleBackup, + handleCancel, + backuping, + customFileName, + setCustomFileName, + showBackupModal + } +} diff --git a/src/renderer/src/components/MarkdownEditor/README.md b/src/renderer/src/components/MarkdownEditor/README.md deleted file mode 100644 index 0519ecba6e..0000000000 --- a/src/renderer/src/components/MarkdownEditor/README.md +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/renderer/src/components/MaxContextCount.tsx b/src/renderer/src/components/MaxContextCount.tsx new file mode 100644 index 0000000000..be9c9f293c --- /dev/null +++ b/src/renderer/src/components/MaxContextCount.tsx @@ -0,0 +1,17 @@ +import { MAX_CONTEXT_COUNT } from '@renderer/config/constant' +import { Infinity as InfinityIcon } from 'lucide-react' +import { CSSProperties } from 'react' + +type Props = { + maxContext: number + style?: CSSProperties + size?: number +} + +export default function MaxContextCount({ maxContext, style, size = 14 }: Props) { + return maxContext === MAX_CONTEXT_COUNT ? ( + + ) : ( + {maxContext.toString()} + ) +} diff --git a/src/renderer/src/components/MinApp/MinappPopupContainer.tsx b/src/renderer/src/components/MinApp/MinappPopupContainer.tsx index f9a4ee7e1e..4ba7badff6 100644 --- a/src/renderer/src/components/MinApp/MinappPopupContainer.tsx +++ b/src/renderer/src/components/MinApp/MinappPopupContainer.tsx @@ -22,7 +22,7 @@ import { useAppDispatch } from '@renderer/store' import { setMinappsOpenLinkExternal } from '@renderer/store/settings' import { MinAppType } from '@renderer/types' import { delay } from '@renderer/utils' -import { Avatar, Drawer, Tooltip } from 'antd' +import { Alert, Avatar, Button, Drawer, Tooltip } from 'antd' import { WebviewTag } from 'electron' import { useEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' @@ -39,6 +39,100 @@ interface AppExtraInfo { type AppInfo = MinAppType & AppExtraInfo +/** Google login tip component */ +const GoogleLoginTip = ({ + isReady, + currentUrl, + currentAppId +}: { + appId?: string | null + isReady: boolean + currentUrl: string | null + currentAppId: string | null +}) => { + const { t } = useTranslation() + const [visible, setVisible] = useState(false) + const { openMinappById } = useMinappPopup() + + // 判断当前URL是否涉及Google登录 + const needsGoogleLogin = useMemo(() => { + // 如果当前已经在Google小程序中,不需要显示提示 + if (currentAppId === 'google') return false + + if (!currentUrl) return false + + const googleLoginPatterns = [ + 'accounts.google.com', + 'signin/oauth', + 'auth/google', + 'login/google', + 'sign-in/google', + 'google.com/signin', + 'gsi/client' + ] + + return googleLoginPatterns.some((pattern) => currentUrl.toLowerCase().includes(pattern.toLowerCase())) + }, [currentUrl, currentAppId]) + + // 在URL更新时检查是否需要显示提示 + useEffect(() => { + let showTimer: NodeJS.Timeout | null = null + let hideTimer: NodeJS.Timeout | null = null + + // 如果是Google登录相关URL且小程序已加载完成,则延迟显示提示 + if (needsGoogleLogin && isReady) { + showTimer = setTimeout(() => { + setVisible(true) + hideTimer = setTimeout(() => { + setVisible(false) + }, 30000) + }, 500) + } else { + setVisible(false) + } + + return () => { + if (showTimer) clearTimeout(showTimer) + if (hideTimer) clearTimeout(hideTimer) + } + }, [needsGoogleLogin, isReady, currentUrl]) + + // 处理关闭提示 + const handleClose = () => { + setVisible(false) + } + + // 跳转到Google小程序 + const openGoogleMinApp = () => { + // 使用openMinappById方法打开Google小程序 + openMinappById('google', true) + // 关闭提示 + setVisible(false) + } + + // 只在需要Google登录时显示提示 + if (!needsGoogleLogin || !visible) return null + + // 使用直接的消息文本 + const message = t('miniwindow.alert.google_login') + + return ( + + {t('common.open')} Google + + } + style={{ zIndex: 10, animation: 'fadeIn 0.3s ease-in-out' }} + /> + ) +} + /** The main container for MinApp popup */ const MinappPopupContainer: React.FC = () => { const { openedKeepAliveMinapps, openedOneOffMinapp, currentMinappId, minappShow } = useRuntime() @@ -198,9 +292,11 @@ const MinappPopupContainer: React.FC = () => { } } - /** the callback function to handle the webview navigate to new url */ + /** the callback function to handle webview navigation */ const handleWebviewNavigate = (appid: string, url: string) => { + // 记录当前URL,用于GoogleLoginTip判断 if (appid === currentMinappId) { + console.log('URL changed:', url) setCurrentUrl(url) } } @@ -297,36 +393,36 @@ const MinappPopupContainer: React.FC = () => { {appInfo.canOpenExternalLink && ( - + )} - + - + - + {appInfo.canPinned && ( - + )} { } mouseEnterDelay={0.8} placement="bottom"> - + {isInDevelopment && ( - + )} {canMinimize && ( - + )} - + @@ -399,6 +495,8 @@ const MinappPopupContainer: React.FC = () => { marginLeft: 'var(--sidebar-width)', backgroundColor: window.root.style.background }}> + {/* 在所有小程序中显示GoogleLoginTip */} + {!isReady && ( = ({ provider, onSuccess, ...buttonProps }) => { oauthWithAihubmix(handleSuccess) } + if (provider.id === 'ppio') { + oauthWithPPIO(handleSuccess) + } + if (provider.id === 'tokenflux') { oauthWithTokenFlux() } diff --git a/src/renderer/src/components/Popups/AddAssistantPopup.tsx b/src/renderer/src/components/Popups/AddAssistantPopup.tsx index 8215ee0ae5..dace8e44b5 100644 --- a/src/renderer/src/components/Popups/AddAssistantPopup.tsx +++ b/src/renderer/src/components/Popups/AddAssistantPopup.tsx @@ -157,6 +157,9 @@ const PopupContainer: React.FC = ({ resolve }) => { padding: 0, overflow: 'hidden', paddingBottom: 20 + }, + body: { + padding: 0 } }} closeIcon={null} diff --git a/src/renderer/src/components/Popups/ApiKeyListPopup/hook.ts b/src/renderer/src/components/Popups/ApiKeyListPopup/hook.ts new file mode 100644 index 0000000000..5bd9072d12 --- /dev/null +++ b/src/renderer/src/components/Popups/ApiKeyListPopup/hook.ts @@ -0,0 +1,307 @@ +import Logger from '@renderer/config/logger' +import { isEmbeddingModel, isRerankModel } from '@renderer/config/models' +import SelectProviderModelPopup from '@renderer/pages/settings/ProviderSettings/SelectProviderModelPopup' +import { checkApi } from '@renderer/services/ApiService' +import WebSearchService from '@renderer/services/WebSearchService' +import { Model, PreprocessProvider, Provider, WebSearchProvider } from '@renderer/types' +import { formatApiKeys, splitApiKeyString } from '@renderer/utils/api' +import { formatErrorMessage } from '@renderer/utils/error' +import { TFunction } from 'i18next' +import { isEmpty } from 'lodash' +import { useCallback, useMemo, useState } from 'react' +import { useTranslation } from 'react-i18next' + +import { ApiKeyConnectivity, ApiKeyValidity, ApiKeyWithStatus, ApiProviderKind, ApiProviderUnion } from './types' + +interface UseApiKeysProps { + provider: ApiProviderUnion + updateProvider: (provider: Partial) => void + providerKind: ApiProviderKind +} + +/** + * API Keys 管理 hook + */ +export function useApiKeys({ provider, updateProvider, providerKind }: UseApiKeysProps) { + const { t } = useTranslation() + + // 连通性检查的 UI 状态管理 + const [connectivityStates, setConnectivityStates] = useState>(new Map()) + + // 保存 apiKey 到 provider + const updateProviderWithKey = useCallback( + (newKeys: string[]) => { + const validKeys = newKeys.filter((k) => k.trim()) + const formattedKeyString = formatApiKeys(validKeys.join(',')) + updateProvider({ apiKey: formattedKeyString }) + }, + [updateProvider] + ) + + // 解析 keyString 为数组 + const keys = useMemo(() => { + if (!provider.apiKey) return [] + const formattedApiKeys = formatApiKeys(provider.apiKey) + const keys = splitApiKeyString(formattedApiKeys) + return Array.from(new Set(keys)) + }, [provider.apiKey]) + + // 合并基本数据和连通性状态 + const keysWithStatus = useMemo((): ApiKeyWithStatus[] => { + return keys.map((key) => { + const connectivityState = connectivityStates.get(key) || { + status: 'not_checked' as const, + checking: false, + error: undefined, + model: undefined, + latency: undefined + } + return { + key, + ...connectivityState + } + }) + }, [keys, connectivityStates]) + + // 更新单个 key 的连通性状态 + const updateConnectivityState = useCallback((key: string, state: Partial) => { + setConnectivityStates((prev) => { + const newMap = new Map(prev) + const currentState = prev.get(key) || { + status: 'not_checked' as const, + checking: false, + error: undefined, + model: undefined, + latency: undefined + } + newMap.set(key, { ...currentState, ...state }) + return newMap + }) + }, []) + + // 验证 API key 格式 + const validateApiKey = useCallback( + (key: string, existingKeys: string[] = []): ApiKeyValidity => { + const trimmedKey = key.trim() + + if (!trimmedKey) { + return { isValid: false, error: t('settings.provider.api.key.error.empty') } + } + + if (existingKeys.includes(trimmedKey)) { + return { isValid: false, error: t('settings.provider.api.key.error.duplicate') } + } + + return { isValid: true } + }, + [t] + ) + + // 添加新 key + const addKey = useCallback( + (key: string): ApiKeyValidity => { + const validation = validateApiKey(key, keys) + + if (!validation.isValid) { + return validation + } + + updateProviderWithKey([...keys, key.trim()]) + return { isValid: true } + }, + [validateApiKey, keys, updateProviderWithKey] + ) + + // 更新 key + const updateKey = useCallback( + (index: number, key: string): ApiKeyValidity => { + if (index < 0 || index >= keys.length) { + Logger.error('[ApiKeyList] invalid key index', { index }) + return { isValid: false, error: 'Invalid index' } + } + + const otherKeys = keys.filter((_, i) => i !== index) + const validation = validateApiKey(key, otherKeys) + + if (!validation.isValid) { + return validation + } + + // 清除旧 key 的连通性状态 + const oldKey = keys[index] + if (oldKey !== key.trim()) { + setConnectivityStates((prev) => { + const newMap = new Map(prev) + newMap.delete(oldKey) + return newMap + }) + } + + const newKeys = [...keys] + newKeys[index] = key.trim() + updateProviderWithKey(newKeys) + + return { isValid: true } + }, + [keys, validateApiKey, updateProviderWithKey] + ) + + // 移除 key + const removeKey = useCallback( + (index: number) => { + if (index < 0 || index >= keys.length) return + + const keyToRemove = keys[index] + const newKeys = keys.filter((_, i) => i !== index) + + // 清除对应的连通性状态 + setConnectivityStates((prev) => { + const newMap = new Map(prev) + newMap.delete(keyToRemove) + return newMap + }) + + updateProviderWithKey(newKeys) + }, + [keys, updateProviderWithKey] + ) + + // 移除连通性检查失败的 keys + const removeInvalidKeys = useCallback(() => { + const validKeys = keysWithStatus.filter((keyStatus) => keyStatus.status !== 'error').map((k) => k.key) + + // 清除被删除的 keys 的连通性状态 + const keysToRemove = keysWithStatus.filter((keyStatus) => keyStatus.status === 'error').map((k) => k.key) + + setConnectivityStates((prev) => { + const newMap = new Map(prev) + keysToRemove.forEach((key) => newMap.delete(key)) + return newMap + }) + + updateProviderWithKey(validKeys) + }, [keysWithStatus, updateProviderWithKey]) + + // 检查单个 key 的连通性,不负责选择和验证模型 + const runConnectivityCheck = useCallback( + async (index: number, model?: Model): Promise => { + const keyToCheck = keys[index] + const currentState = connectivityStates.get(keyToCheck) + if (currentState?.checking) return + + // 设置检查状态 + updateConnectivityState(keyToCheck, { checking: true }) + + try { + const startTime = Date.now() + if (isLlmProvider(provider, providerKind) && model) { + await checkApi({ ...provider, apiKey: keyToCheck }, model) + } else { + const result = await WebSearchService.checkSearch({ ...provider, apiKey: keyToCheck }) + if (!result.valid) throw new Error(result.error) + } + const latency = Date.now() - startTime + + // 连通性检查成功 + updateConnectivityState(keyToCheck, { + checking: false, + status: 'success', + model, + latency, + error: undefined + }) + } catch (error: any) { + // 连通性检查失败 + updateConnectivityState(keyToCheck, { + checking: false, + status: 'error', + error: formatErrorMessage(error), + model: undefined, + latency: undefined + }) + + Logger.error('[ApiKeyList] failed to validate the connectivity of the api key', error) + } + }, + [keys, connectivityStates, updateConnectivityState, provider, providerKind] + ) + + // 检查单个 key 的连通性 + const checkKeyConnectivity = useCallback( + async (index: number): Promise => { + if (!provider || index < 0 || index >= keys.length) return + + const keyToCheck = keys[index] + const currentState = connectivityStates.get(keyToCheck) + if (currentState?.checking) return + + const model = isLlmProvider(provider, providerKind) ? await getModelForCheck(provider, t) : undefined + if (model === null) return + + await runConnectivityCheck(index, model) + }, + [provider, keys, connectivityStates, providerKind, t, runConnectivityCheck] + ) + + // 检查所有 keys 的连通性 + const checkAllKeysConnectivity = useCallback(async () => { + if (!provider || keys.length === 0) return + + const model = isLlmProvider(provider, providerKind) ? await getModelForCheck(provider, t) : undefined + if (model === null) return + + await Promise.allSettled(keys.map((_, index) => runConnectivityCheck(index, model))) + }, [provider, keys, providerKind, t, runConnectivityCheck]) + + // 计算是否有 key 正在检查 + const isChecking = useMemo(() => { + return Array.from(connectivityStates.values()).some((state) => state.checking) + }, [connectivityStates]) + + return { + keys: keysWithStatus, + addKey, + updateKey, + removeKey, + removeInvalidKeys, + checkKeyConnectivity, + checkAllKeysConnectivity, + isChecking + } +} + +export function isLlmProvider(obj: any, kind: ApiProviderKind): obj is Provider { + return kind === 'llm' && 'type' in obj && 'models' in obj +} + +export function isWebSearchProvider(obj: any, kind: ApiProviderKind): obj is WebSearchProvider { + return kind === 'websearch' && ('url' in obj || 'engines' in obj) +} + +export function isPreprocessProvider(obj: any, kind: ApiProviderKind): obj is PreprocessProvider { + return kind === 'doc-preprocess' && ('quota' in obj || 'options' in obj) +} + +// 获取模型用于检查 +async function getModelForCheck(provider: Provider, t: TFunction): Promise { + const modelsToCheck = provider.models.filter((model) => !isEmbeddingModel(model) && !isRerankModel(model)) + + if (isEmpty(modelsToCheck)) { + window.message.error({ + key: 'no-models', + style: { marginTop: '3vh' }, + duration: 5, + content: t('settings.provider.no_models_for_check') + }) + return null + } + + try { + const selectedModel = await SelectProviderModelPopup.show({ provider }) + if (!selectedModel) return null + return selectedModel + } catch (error) { + Logger.error('[ApiKeyList] failed to select model', error) + return null + } +} diff --git a/src/renderer/src/components/Popups/ApiKeyListPopup/index.ts b/src/renderer/src/components/Popups/ApiKeyListPopup/index.ts new file mode 100644 index 0000000000..b599832456 --- /dev/null +++ b/src/renderer/src/components/Popups/ApiKeyListPopup/index.ts @@ -0,0 +1,2 @@ +export { default as ApiKeyListPopup } from './popup' +export * from './types' diff --git a/src/renderer/src/components/Popups/ApiKeyListPopup/item.tsx b/src/renderer/src/components/Popups/ApiKeyListPopup/item.tsx new file mode 100644 index 0000000000..cf830897cb --- /dev/null +++ b/src/renderer/src/components/Popups/ApiKeyListPopup/item.tsx @@ -0,0 +1,213 @@ +import { CheckCircleFilled, CloseCircleFilled, MinusOutlined } from '@ant-design/icons' +import { StreamlineGoodHealthAndWellBeing } from '@renderer/components/Icons/SVGIcon' +import { maskApiKey } from '@renderer/utils/api' +import { Button, Flex, Input, InputRef, List, Popconfirm, Tooltip, Typography } from 'antd' +import { Check, PenLine, X } from 'lucide-react' +import { FC, memo, useEffect, useRef, useState } from 'react' +import { useTranslation } from 'react-i18next' +import styled from 'styled-components' + +import { ApiKeyValidity, ApiKeyWithStatus } from './types' + +export interface ApiKeyItemProps { + keyStatus: ApiKeyWithStatus + onUpdate: (newKey: string) => ApiKeyValidity + onRemove: () => void + onCheck: () => Promise + disabled?: boolean + showHealthCheck?: boolean + isNew?: boolean +} + +/** + * API Key 项组件 + * 支持编辑、删除、连接检查等操作 + */ +const ApiKeyItem: FC = ({ + keyStatus, + onUpdate, + onRemove, + onCheck, + disabled: _disabled = false, + showHealthCheck = true, + isNew = false +}) => { + const { t } = useTranslation() + const [isEditing, setIsEditing] = useState(isNew || !keyStatus.key.trim()) + const [editValue, setEditValue] = useState(keyStatus.key) + const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false) + const inputRef = useRef(null) + + const disabled = keyStatus.checking || _disabled + const isNotChecked = keyStatus.status === 'not_checked' + const isSuccess = keyStatus.status === 'success' + const statusColor = isSuccess ? 'var(--color-status-success)' : 'var(--color-status-error)' + + useEffect(() => { + if (isEditing && inputRef.current) { + inputRef.current.focus() + } + }, [isEditing]) + + useEffect(() => { + setHasUnsavedChanges(editValue.trim() !== keyStatus.key.trim()) + }, [editValue, keyStatus.key]) + + const handleEdit = () => { + if (disabled) return + setIsEditing(true) + setEditValue(keyStatus.key) + } + + const handleSave = () => { + const result = onUpdate(editValue) + if (!result.isValid) { + window.message.warning({ + key: 'api-key-error', + content: result.error + }) + return + } + + setIsEditing(false) + } + + const handleCancelEdit = () => { + if (isNew || !keyStatus.key.trim()) { + // 临时项取消时直接移除 + onRemove() + } else { + // 现有项取消时恢复原值 + setEditValue(keyStatus.key) + setIsEditing(false) + } + } + + const renderStatusIcon = () => { + if (keyStatus.checking || isNotChecked) return null + + const StatusIcon = isSuccess ? CheckCircleFilled : CloseCircleFilled + return + } + + const renderKeyCheckResultTooltip = () => { + if (keyStatus.checking) { + return t('settings.models.check.checking') + } + + if (isNotChecked) { + return '' + } + + const statusTitle = isSuccess ? t('settings.models.check.passed') : t('settings.models.check.failed') + + return ( +
+ {statusTitle} + {keyStatus.model && ( +
+ {t('common.model')}: {keyStatus.model.name} +
+ )} + {keyStatus.latency && isSuccess && ( +
+ {t('settings.provider.api.key.check.latency')}: {(keyStatus.latency / 1000).toFixed(2)}s +
+ )} + {keyStatus.error &&
{keyStatus.error}
} +
+ ) + } + + return ( + + {isEditing ? ( + + setEditValue(e.target.value)} + onPressEnter={handleSave} + placeholder={t('settings.provider.api.key.new_key.placeholder')} + style={{ flex: 1, fontSize: '14px', marginLeft: '-10px' }} + spellCheck={false} + disabled={disabled} + /> + + + + + + + ) +} + +interface SpecificApiKeyListProps { + providerId: string + providerKind: ApiProviderKind + showHealthCheck?: boolean +} + +export const LlmApiKeyList: FC = ({ providerId, providerKind, showHealthCheck = true }) => { + const { provider, updateProvider } = useProvider(providerId) + + return ( + + ) +} + +export const WebSearchApiKeyList: FC = ({ + providerId, + providerKind, + showHealthCheck = true +}) => { + const { provider, updateProvider } = useWebSearchProvider(providerId) + + return ( + + ) +} + +export const DocPreprocessApiKeyList: FC = ({ + providerId, + providerKind, + showHealthCheck = true +}) => { + const { provider, updateProvider } = usePreprocessProvider(providerId) + + return ( + + ) +} + +const ListContainer = styled.div` + padding-top: 15px; + padding-bottom: 15px; +` diff --git a/src/renderer/src/components/Popups/ApiKeyListPopup/popup.tsx b/src/renderer/src/components/Popups/ApiKeyListPopup/popup.tsx new file mode 100644 index 0000000000..096e00ca58 --- /dev/null +++ b/src/renderer/src/components/Popups/ApiKeyListPopup/popup.tsx @@ -0,0 +1,88 @@ +import { TopView } from '@renderer/components/TopView' +import { Modal } from 'antd' +import { useMemo, useState } from 'react' +import { useTranslation } from 'react-i18next' + +import { DocPreprocessApiKeyList, LlmApiKeyList, WebSearchApiKeyList } from './list' +import { ApiProviderKind } from './types' + +interface ShowParams { + providerId: string + providerKind: ApiProviderKind + title?: string + showHealthCheck?: boolean +} + +interface Props extends ShowParams { + resolve: (value: any) => void +} + +/** + * API Key 列表弹窗容器组件 + */ +const PopupContainer: React.FC = ({ providerId, providerKind, title, resolve, showHealthCheck = true }) => { + const [open, setOpen] = useState(true) + const { t } = useTranslation() + + const onCancel = () => { + setOpen(false) + } + + const onClose = () => { + resolve(null) + } + + const ListComponent = useMemo(() => { + switch (providerKind) { + case 'llm': + return LlmApiKeyList + case 'websearch': + return WebSearchApiKeyList + case 'doc-preprocess': + return DocPreprocessApiKeyList + default: + return null + } + }, [providerKind]) + + return ( + + {ListComponent && ( + + )} + + ) +} + +const TopViewKey = 'ApiKeyListPopup' + +export default class ApiKeyListPopup { + static topviewId = 0 + + static hide() { + TopView.hide(TopViewKey) + } + + static show(props: ShowParams) { + return new Promise((resolve) => { + TopView.show( + { + resolve(v) + TopView.hide(TopViewKey) + }} + />, + TopViewKey + ) + }) + } +} diff --git a/src/renderer/src/components/Popups/ApiKeyListPopup/types.ts b/src/renderer/src/components/Popups/ApiKeyListPopup/types.ts new file mode 100644 index 0000000000..f5ed1c62d1 --- /dev/null +++ b/src/renderer/src/components/Popups/ApiKeyListPopup/types.ts @@ -0,0 +1,31 @@ +import { Model, PreprocessProvider, Provider, WebSearchProvider } from '@renderer/types' + +/** + * API Key 连通性检查的状态 + */ +export type ApiKeyConnectivity = { + status: 'success' | 'error' | 'not_checked' + checking?: boolean + error?: string + model?: Model + latency?: number +} + +/** + * API key 及其连通性检查的状态 + */ +export type ApiKeyWithStatus = { + key: string +} & ApiKeyConnectivity + +/** + * API key 格式有效性 + */ +export type ApiKeyValidity = { + isValid: boolean + error?: string +} + +export type ApiProviderUnion = Provider | WebSearchProvider | PreprocessProvider + +export type ApiProviderKind = 'llm' | 'websearch' | 'doc-preprocess' diff --git a/src/renderer/src/components/Popups/FloatingSidebar.tsx b/src/renderer/src/components/Popups/FloatingSidebar.tsx index 1c281bc0d2..3fc464465e 100644 --- a/src/renderer/src/components/Popups/FloatingSidebar.tsx +++ b/src/renderer/src/components/Popups/FloatingSidebar.tsx @@ -54,7 +54,7 @@ const FloatingSidebar: FC = ({ style={{ background: 'transparent', border: 'none', - maxHeight: maxHeight + height: '100%' }} /> @@ -82,6 +82,9 @@ const FloatingSidebar: FC = ({ const PopoverContent = styled.div<{ maxHeight: number }>` max-height: ${(props) => props.maxHeight}px; + &.ant-popover-inner-content { + overflow-y: hidden; + } ` export default FloatingSidebar diff --git a/src/renderer/src/components/Popups/MinAppsPopover.tsx b/src/renderer/src/components/Popups/MinAppsPopover.tsx deleted file mode 100644 index 28a9621bc9..0000000000 --- a/src/renderer/src/components/Popups/MinAppsPopover.tsx +++ /dev/null @@ -1,82 +0,0 @@ -import { Center } from '@renderer/components/Layout' -import { useMinapps } from '@renderer/hooks/useMinapps' -import App from '@renderer/pages/apps/App' -import { Popover } from 'antd' -import { Empty } from 'antd' -import { isEmpty } from 'lodash' -import { FC, useEffect, useState } from 'react' -import { useHotkeys } from 'react-hotkeys-hook' -import styled from 'styled-components' - -import Scrollbar from '../Scrollbar' - -interface Props { - children: React.ReactNode -} - -const MinAppsPopover: FC = ({ children }) => { - const [open, setOpen] = useState(false) - const { minapps } = useMinapps() - - useHotkeys('esc', () => { - setOpen(false) - }) - - const handleClose = () => { - setOpen(false) - } - - const [maxHeight, setMaxHeight] = useState(window.innerHeight - 100) - - useEffect(() => { - const handleResize = () => { - setMaxHeight(window.innerHeight - 100) - } - - window.addEventListener('resize', handleResize) - - return () => { - window.removeEventListener('resize', handleResize) - } - }, []) - - const content = ( - - - {minapps.map((app) => ( - - ))} - {isEmpty(minapps) && ( -
- -
- )} -
-
- ) - - return ( - - {children} - - ) -} - -const PopoverContent = styled(Scrollbar)<{ maxHeight: number }>` - max-height: ${(props) => props.maxHeight}px; - overflow-y: auto; -` - -const AppsContainer = styled.div` - display: grid; - grid-template-columns: repeat(8, minmax(90px, 1fr)); - gap: 18px; -` - -export default MinAppsPopover diff --git a/src/renderer/src/components/Popups/SaveToKnowledgePopup.tsx b/src/renderer/src/components/Popups/SaveToKnowledgePopup.tsx new file mode 100644 index 0000000000..b6f0577c4f --- /dev/null +++ b/src/renderer/src/components/Popups/SaveToKnowledgePopup.tsx @@ -0,0 +1,353 @@ +import CustomTag from '@renderer/components/CustomTag' +import { TopView } from '@renderer/components/TopView' +import Logger from '@renderer/config/logger' +import { useKnowledge, useKnowledgeBases } from '@renderer/hooks/useKnowledge' +import { Message } from '@renderer/types/newMessage' +import { + analyzeMessageContent, + CONTENT_TYPES, + ContentType, + MessageContentStats, + processMessageContent +} from '@renderer/utils/knowledge' +import { Flex, Form, Modal, Select, Tooltip, Typography } from 'antd' +import { Check, CircleHelp } from 'lucide-react' +import { useEffect, useMemo, useState } from 'react' +import { useTranslation } from 'react-i18next' +import styled from 'styled-components' + +const { Text } = Typography + +// 内容类型配置 +const CONTENT_TYPE_CONFIG = { + [CONTENT_TYPES.TEXT]: { + label: 'chat.save.knowledge.content.maintext.title', + description: 'chat.save.knowledge.content.maintext.description' + }, + [CONTENT_TYPES.CODE]: { + label: 'chat.save.knowledge.content.code.title', + description: 'chat.save.knowledge.content.code.description' + }, + [CONTENT_TYPES.THINKING]: { + label: 'chat.save.knowledge.content.thinking.title', + description: 'chat.save.knowledge.content.thinking.description' + }, + [CONTENT_TYPES.TOOL_USE]: { + label: 'chat.save.knowledge.content.tool_use.title', + description: 'chat.save.knowledge.content.tool_use.description' + }, + [CONTENT_TYPES.CITATION]: { + label: 'chat.save.knowledge.content.citation.title', + description: 'chat.save.knowledge.content.citation.description' + }, + [CONTENT_TYPES.TRANSLATION]: { + label: 'chat.save.knowledge.content.translation.title', + description: 'chat.save.knowledge.content.translation.description' + }, + [CONTENT_TYPES.ERROR]: { + label: 'chat.save.knowledge.content.error.title', + description: 'chat.save.knowledge.content.error.description' + }, + [CONTENT_TYPES.FILE]: { + label: 'chat.save.knowledge.content.file.title', + description: 'chat.save.knowledge.content.file.description' + } +} as const + +// Tag 颜色常量 +const TAG_COLORS = { + SELECTED: '#008001', + UNSELECTED: '#8c8c8c' +} as const + +interface ContentTypeOption { + type: ContentType + label: string + count: number + enabled: boolean + description?: string +} + +interface ShowParams { + message: Message + title?: string +} + +interface SaveResult { + success: boolean + savedCount: number +} + +interface Props extends ShowParams { + resolve: (data: SaveResult | null) => void +} + +const PopupContainer: React.FC = ({ message, title, resolve }) => { + const [open, setOpen] = useState(true) + const [loading, setLoading] = useState(false) + const [selectedBaseId, setSelectedBaseId] = useState() + const [selectedTypes, setSelectedTypes] = useState([]) + const [hasInitialized, setHasInitialized] = useState(false) + const { bases } = useKnowledgeBases() + const { addNote, addFiles } = useKnowledge(selectedBaseId || '') + const { t } = useTranslation() + + // 分析消息内容统计 + const contentStats = useMemo(() => analyzeMessageContent(message), [message]) + + // 生成内容类型选项(只显示有内容的类型) + const contentTypeOptions: ContentTypeOption[] = useMemo(() => { + return Object.entries(CONTENT_TYPE_CONFIG) + .map(([type, config]) => { + const contentType = type as ContentType + const count = contentStats[contentType as keyof MessageContentStats] || 0 + return { + type: contentType, + count, + enabled: count > 0, + label: t(config.label), + description: t(config.description) + } + }) + .filter((option) => option.enabled) // 只显示有内容的类型 + }, [contentStats, t]) + + // 知识库选项 + const knowledgeBaseOptions = useMemo( + () => + bases.map((base) => ({ + label: base.name, + value: base.id, + disabled: !base.version // 如果知识库没有配置好就禁用 + })), + [bases] + ) + + // 合并状态计算 + const formState = useMemo(() => { + const hasValidBase = selectedBaseId && bases.find((base) => base.id === selectedBaseId)?.version + const hasContent = contentTypeOptions.length > 0 + const selectedCount = contentTypeOptions + .filter((option) => selectedTypes.includes(option.type)) + .reduce((sum, option) => sum + option.count, 0) + + return { + hasValidBase, + hasContent, + canSubmit: hasValidBase && selectedTypes.length > 0 && hasContent, + selectedCount, + hasNoSelection: selectedTypes.length === 0 && hasContent + } + }, [selectedBaseId, bases, contentTypeOptions, selectedTypes]) + + // 默认选择第一个可用的知识库 + useEffect(() => { + if (!selectedBaseId) { + const firstAvailableBase = bases.find((base) => base.version) + if (firstAvailableBase) { + setSelectedBaseId(firstAvailableBase.id) + } + } + }, [bases, selectedBaseId]) + + // 默认选择所有可用的内容类型(仅在初始化时) + useEffect(() => { + if (!hasInitialized && contentTypeOptions.length > 0) { + const availableTypes = contentTypeOptions.map((option) => option.type) + setSelectedTypes(availableTypes) + setHasInitialized(true) + } + }, [contentTypeOptions, hasInitialized]) + + // 计算UI状态 + const uiState = useMemo(() => { + if (!formState.hasContent) { + return { type: 'empty', message: t('chat.save.knowledge.empty.no_content') } + } + if (bases.length === 0) { + return { type: 'empty', message: t('chat.save.knowledge.empty.no_knowledge_base') } + } + return { type: 'form' } + }, [formState.hasContent, bases.length, t]) + + // 处理内容类型选择切换 + const handleContentTypeToggle = (type: ContentType) => { + setSelectedTypes((prev) => (prev.includes(type) ? prev.filter((t) => t !== type) : [...prev, type])) + } + + const onOk = async () => { + if (!formState.canSubmit) { + return + } + + setLoading(true) + let savedCount = 0 + + try { + const result = processMessageContent(message, selectedTypes) + + // 保存文本内容 + if (result.text.trim() && selectedTypes.some((type) => type !== CONTENT_TYPES.FILE)) { + await addNote(result.text) + savedCount++ + } + + // 保存文件 + if (result.files.length > 0 && selectedTypes.includes(CONTENT_TYPES.FILE)) { + addFiles(result.files) + savedCount += result.files.length + } + + setOpen(false) + resolve({ success: true, savedCount }) + } catch (error) { + Logger.error('[SaveToKnowledgePopup] save failed:', error) + window.message.error(t('chat.save.knowledge.error.save_failed')) + setLoading(false) + } + } + + const onCancel = () => { + setOpen(false) + } + + const onClose = () => { + resolve(null) + } + + // 渲染空状态 + const renderEmptyState = () => ( + + {uiState.message} + + ) + + // 渲染表单内容 + const renderFormContent = () => ( + <> +
+ + - ))} - {(editedBlocks.some((block) => block.type === MessageBlockType.FILE || block.type === MessageBlockType.IMAGE) || - files.length > 0) && ( - - {editedBlocks - .filter((block) => block.type === MessageBlockType.FILE || block.type === MessageBlockType.IMAGE) - .map( - (block) => - block.file && ( - handleFileRemove(block.id)}> - - - ) - )} - - {files.map((file) => ( - setFiles((prevFiles) => prevFiles.filter((f) => f.id !== file.id))}> - - + <> + e.preventDefault()} onDrop={handleDrop}> + {editedBlocks + .filter((block) => block.type === MessageBlockType.MAIN_TEXT) + .map((block) => ( + ))} - - )} + {(editedBlocks.some((block) => block.type === MessageBlockType.FILE || block.type === MessageBlockType.IMAGE) || + files.length > 0) && ( + + {editedBlocks + .filter((block) => block.type === MessageBlockType.FILE || block.type === MessageBlockType.IMAGE) + .map( + (block) => + block.file && ( + handleFileRemove(block.id)}> + + + ) + )} + {files.map((file) => ( + setFiles((prevFiles) => prevFiles.filter((f) => f.id !== file.id))}> + + + ))} + + )} + - + {isUserMessage && ( + + )} @@ -302,17 +352,17 @@ const MessageBlockEditor: FC = ({ message, onSave, onResend, onCancel }) )} - + ) } const EditorContainer = styled.div` - padding: 8px 0; + padding: 18px 0; + padding-bottom: 5px; border: 0.5px solid var(--color-border); transition: all 0.2s ease; border-radius: 15px; - margin-top: 5px; - margin-bottom: 10px; + margin-top: 18px; background-color: var(--color-background-opacity); width: 100%; diff --git a/src/renderer/src/pages/home/Messages/MessageGroup.tsx b/src/renderer/src/pages/home/Messages/MessageGroup.tsx index 9d0fb1c6f8..30697712fe 100644 --- a/src/renderer/src/pages/home/Messages/MessageGroup.tsx +++ b/src/renderer/src/pages/home/Messages/MessageGroup.tsx @@ -25,12 +25,18 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => { const { editMessage } = useMessageOperations(topic) const { multiModelMessageStyle: multiModelMessageStyleSetting, gridColumns, gridPopoverTrigger } = useSettings() const { isMultiSelectMode } = useChatContext(topic) + const messageLength = messages.length - const [multiModelMessageStyle, setMultiModelMessageStyle] = useState( + const [_multiModelMessageStyle, setMultiModelMessageStyle] = useState( messages[0].multiModelMessageStyle || multiModelMessageStyleSetting ) - const messageLength = messages.length + // 对于单模型消息,采用简单的样式,避免 overflow 影响内部的 sticky 效果 + const multiModelMessageStyle = useMemo( + () => (messageLength < 2 ? 'fold' : _multiModelMessageStyle), + [_multiModelMessageStyle, messageLength] + ) + const prevMessageLengthRef = useRef(messageLength) const [selectedIndex, setSelectedIndex] = useState(messageLength - 1) @@ -265,7 +271,7 @@ const GridContainer = styled(Scrollbar)<{ $count: number; $gridColumns: number } gap: 16px; &.horizontal { padding-bottom: 4px; - grid-template-columns: repeat(${({ $count }) => $count}, minmax(480px, 1fr)); + grid-template-columns: repeat(${({ $count }) => $count}, minmax(420px, 1fr)); overflow-x: auto; } &.fold, @@ -308,6 +314,7 @@ interface MessageWrapperProps { const MessageWrapper = styled.div` &.horizontal { + padding-right: 1px; overflow-y: auto; .message { height: 100%; @@ -356,6 +363,7 @@ const MessageWrapper = styled.div` cursor: default; .message-content-container { padding-left: 0; + pointer-events: auto; } .MessageFooter { margin-left: 0; diff --git a/src/renderer/src/pages/home/Messages/MessageHeader.tsx b/src/renderer/src/pages/home/Messages/MessageHeader.tsx index 465ca923b8..97a32894bf 100644 --- a/src/renderer/src/pages/home/Messages/MessageHeader.tsx +++ b/src/renderer/src/pages/home/Messages/MessageHeader.tsx @@ -18,13 +18,10 @@ import { FC, memo, useCallback, useMemo } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' -import MessageTokens from './MessageTokens' - interface Props { message: Message assistant: Assistant model?: Model - index: number | undefined topic: Topic } @@ -33,7 +30,7 @@ const getAvatarSource = (isLocalAi: boolean, modelId: string | undefined) => { return modelId ? getModelLogo(modelId) : undefined } -const MessageHeader: FC = memo(({ assistant, model, message, index, topic }) => { +const MessageHeader: FC = memo(({ assistant, model, message, topic }) => { const avatar = useAvatar() const { theme } = useTheme() const { userName, sidebarIcons } = useSettings() @@ -60,12 +57,11 @@ const MessageHeader: FC = memo(({ assistant, model, message, index, topic }, [message, model, t, userName]) const isAssistantMessage = message.role === 'assistant' + const isUserMessage = message.role === 'user' const showMinappIcon = sidebarIcons.visible.includes('minapp') - const { showTokens } = useSettings() const avatarName = useMemo(() => firstLetter(assistant?.name).toUpperCase(), [assistant?.name]) const username = useMemo(() => removeLeadingEmoji(getUserName()), [getUserName]) - const isLastMessage = index === 0 const showMiniApp = useCallback(() => { showMinappIcon && model?.provider && openMinappById(model.provider) @@ -73,6 +69,12 @@ const MessageHeader: FC = memo(({ assistant, model, message, index, topic // eslint-disable-next-line react-hooks/exhaustive-deps }, [model?.provider, showMinappIcon]) + const hideHeader = isBubbleStyle ? isUserMessage && !isMultiSelectMode : false + + if (hideHeader) { + return null + } + return ( {isAssistantMessage ? ( @@ -110,8 +112,6 @@ const MessageHeader: FC = memo(({ assistant, model, message, index, topic {dayjs(message?.updatedAt ?? message.createdAt).format('MM/DD HH:mm')} - {showTokens && | } - {isMultiSelectMode && ( @@ -133,6 +133,7 @@ const Container = styled.div` align-items: center; gap: 10px; position: relative; + margin-bottom: 10px; ` const UserWrap = styled.div` @@ -149,12 +150,6 @@ const InfoWrap = styled.div` gap: 4px; ` -const DividerContainer = styled.div` - font-size: 10px; - color: var(--color-text-3); - margin: 0 2px; -` - const UserName = styled.div<{ isBubbleStyle?: boolean; theme?: string }>` font-size: 14px; font-weight: 600; diff --git a/src/renderer/src/pages/home/Messages/MessageMenubar.tsx b/src/renderer/src/pages/home/Messages/MessageMenubar.tsx index fda0f40a4b..0f0c1f01b7 100644 --- a/src/renderer/src/pages/home/Messages/MessageMenubar.tsx +++ b/src/renderer/src/pages/home/Messages/MessageMenubar.tsx @@ -1,7 +1,9 @@ import { CheckOutlined, EditOutlined, QuestionCircleOutlined, SyncOutlined } from '@ant-design/icons' import ObsidianExportPopup from '@renderer/components/Popups/ObsidianExportPopup' +import SaveToKnowledgePopup from '@renderer/components/Popups/SaveToKnowledgePopup' import SelectModelPopup from '@renderer/components/Popups/SelectModelPopup' -import { TranslateLanguageOptions } from '@renderer/config/translate' +import { isVisionModel } from '@renderer/config/models' +import { translateLanguageOptions } from '@renderer/config/translate' import { useMessageEditing } from '@renderer/context/MessageEditingContext' import { useChatContext } from '@renderer/hooks/useChatContext' import { useMessageOperations, useTopicLoading } from '@renderer/hooks/useMessageOperations' @@ -11,10 +13,10 @@ import { getMessageTitle } from '@renderer/services/MessagesService' import { translateText } from '@renderer/services/TranslateService' import store, { RootState } from '@renderer/store' import { messageBlocksSelectors } from '@renderer/store/messageBlock' -import type { Model } from '@renderer/types' -import type { Assistant, Topic } from '@renderer/types' -import type { Message } from '@renderer/types/newMessage' -import { captureScrollableDivAsBlob, captureScrollableDivAsDataURL } from '@renderer/utils' +import { selectMessagesForTopic } from '@renderer/store/newMessage' +import type { Assistant, Language, Model, Topic } from '@renderer/types' +import { type Message, MessageBlockType } from '@renderer/types/newMessage' +import { captureScrollableDivAsBlob, captureScrollableDivAsDataURL, classNames } from '@renderer/utils' import { copyMessageAsPlainText } from '@renderer/utils/copy' import { exportMarkdownToJoplin, @@ -29,13 +31,27 @@ import { removeTrailingDoubleSpaces } from '@renderer/utils/markdown' import { findMainTextBlocks, findTranslationBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find' import { Dropdown, Popconfirm, Tooltip } from 'antd' import dayjs from 'dayjs' -import { AtSign, Copy, Languages, ListChecks, Menu, RefreshCw, Save, Share, Split, ThumbsUp, Trash } from 'lucide-react' -import { FilePenLine } from 'lucide-react' +import { + AtSign, + Copy, + FilePenLine, + Languages, + ListChecks, + Menu, + RefreshCw, + Save, + Share, + Split, + ThumbsUp, + Trash +} from 'lucide-react' import { FC, memo, useCallback, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import styled from 'styled-components' +import MessageTokens from './MessageTokens' + interface Props { message: Message assistant: Assistant @@ -140,12 +156,12 @@ const MessageMenubar: FC = (props) => { }, [message.id, startEditing]) const handleTranslate = useCallback( - async (language: string) => { + async (language: Language) => { if (isTranslating) return setIsTranslating(true) const messageId = message.id - const translationUpdater = await getTranslationUpdater(messageId, language) + const translationUpdater = await getTranslationUpdater(messageId, language.langCode) if (!translationUpdater) return try { await translateText(mainTextContent, language, translationUpdater) @@ -167,21 +183,12 @@ const MessageMenubar: FC = (props) => { const dropdownItems = useMemo( () => [ - { - label: t('chat.save'), - key: 'save', - icon: , - onClick: () => { - const fileName = dayjs(message.createdAt).format('YYYYMMDDHHmm') + '.md' - window.api.file.save(fileName, mainTextContent) - } - }, ...(isEditable ? [ { label: t('common.edit'), key: 'edit', - icon: , + icon: , onClick: onEdit } ] @@ -189,21 +196,43 @@ const MessageMenubar: FC = (props) => { { label: t('chat.message.new.branch'), key: 'new-branch', - icon: , + icon: , onClick: onNewBranch }, { label: t('chat.multiple.select'), key: 'multi-select', - icon: , + icon: , onClick: () => { toggleMultiSelectMode(true) } }, + { + label: t('chat.save'), + key: 'save', + icon: , + children: [ + { + label: t('chat.save.file.title'), + key: 'file', + onClick: () => { + const fileName = dayjs(message.createdAt).format('YYYYMMDDHHmm') + '.md' + window.api.file.save(fileName, mainTextContent) + } + }, + { + label: t('chat.save.knowledge.title'), + key: 'knowledge', + onClick: () => { + SaveToKnowledgePopup.show({ message }) + } + } + ] + }, { label: t('chat.topics.export.title'), key: 'export', - icon: , + icon: , children: [ exportMenuOptions.plain_text && { label: t('chat.topics.copy.plain_text'), @@ -327,13 +356,47 @@ const MessageMenubar: FC = (props) => { regenerateAssistantMessage(message, assistantWithTopicPrompt) } - const onMentionModel = async (e: React.MouseEvent) => { - e.stopPropagation() - if (loading) return - const selectedModel = await SelectModelPopup.show({ model }) - if (!selectedModel) return - appendAssistantResponse(message, selectedModel, { ...assistant, model: selectedModel }) - } + // 按条件筛选能够提及的模型,该函数仅在isAssistantMessage时会用到 + const mentionModelFilter = useMemo(() => { + if (!isAssistantMessage) { + return () => true + } + const state = store.getState() + const topicMessages: Message[] = selectMessagesForTopic(state, topic.id) + // 理论上助手消息只会关联一条用户消息 + const relatedUserMessage = topicMessages.find((msg) => { + return msg.role === 'user' && message.askId === msg.id + }) + // 无关联用户消息时,默认返回所有模型 + if (!relatedUserMessage) { + return () => true + } + + const relatedUserMessageBlocks = relatedUserMessage.blocks.map((msgBlockId) => + messageBlocksSelectors.selectById(store.getState(), msgBlockId) + ) + + if (!relatedUserMessageBlocks) { + return () => true + } + + if (relatedUserMessageBlocks.some((block) => block && block.type === MessageBlockType.IMAGE)) { + return (m: Model) => isVisionModel(m) + } else { + return () => true + } + }, [isAssistantMessage, message.askId, topic.id]) + + const onMentionModel = useCallback( + async (e: React.MouseEvent) => { + e.stopPropagation() + if (loading) return + const selectedModel = await SelectModelPopup.show({ model, modelFilter: mentionModelFilter }) + if (!selectedModel) return + appendAssistantResponse(message, selectedModel, { ...assistant, model: selectedModel }) + }, + [appendAssistantResponse, assistant, loading, mentionModelFilter, message, model] + ) const onUseful = useCallback( (e: React.MouseEvent) => { @@ -350,173 +413,182 @@ const MessageMenubar: FC = (props) => { }, [message]) const softHoverBg = isBubbleStyle && !isLastMessage + const showMessageTokens = !isBubbleStyle + const isUserBubbleStyleMessage = isBubbleStyle && isUserMessage return ( - - {message.role === 'user' && ( - - handleResendUserMessage()} - $softHoverBg={isBubbleStyle}> - - - - )} - {message.role === 'user' && ( - - - - - - )} - - - {!copied && } - {copied && } - - - {isAssistantMessage && ( - } - onConfirm={onRegenerate} - onOpenChange={(open) => open && setShowRegenerateTooltip(false)}> - - - + <> + {showMessageTokens && } + + {message.role === 'user' && ( + + handleResendUserMessage()} + $softHoverBg={isBubbleStyle}> + - - )} - {isAssistantMessage && ( - - - + )} + {message.role === 'user' && ( + + + + + + )} + + + {!copied && } + {copied && } - )} - {!isUserMessage && ( - ({ - label: item.emoji + ' ' + item.label, - key: item.value, - onClick: () => handleTranslate(item.value) - })), - ...(hasTranslationBlocks - ? [ - { type: 'divider' as const }, - { - label: '📋 ' + t('common.copy'), - key: 'translate-copy', - onClick: () => { - const translationBlocks = message.blocks - .map((blockId) => blockEntities[blockId]) - .filter((block) => block?.type === 'translation') + {isAssistantMessage && ( + } + onConfirm={onRegenerate} + onOpenChange={(open) => open && setShowRegenerateTooltip(false)}> + + + + + + + )} + {isAssistantMessage && ( + + + + + + )} + {!isUserMessage && ( + ({ + label: item.emoji + ' ' + item.label(), + key: item.langCode, + onClick: () => handleTranslate(item) + })), + ...(hasTranslationBlocks + ? [ + { type: 'divider' as const }, + { + label: '📋 ' + t('common.copy'), + key: 'translate-copy', + onClick: () => { + const translationBlocks = message.blocks + .map((blockId) => blockEntities[blockId]) + .filter((block) => block?.type === 'translation') - if (translationBlocks.length > 0) { - const translationContent = translationBlocks - .map((block) => block?.content || '') - .join('\n\n') - .trim() + if (translationBlocks.length > 0) { + const translationContent = translationBlocks + .map((block) => block?.content || '') + .join('\n\n') + .trim() - if (translationContent) { - navigator.clipboard.writeText(translationContent) - window.message.success({ content: t('translate.copied'), key: 'translate-copy' }) - } else { - window.message.warning({ content: t('translate.empty'), key: 'translate-copy' }) + if (translationContent) { + navigator.clipboard.writeText(translationContent) + window.message.success({ content: t('translate.copied'), key: 'translate-copy' }) + } else { + window.message.warning({ content: t('translate.empty'), key: 'translate-copy' }) + } + } + } + }, + { + label: '✖ ' + t('translate.close'), + key: 'translate-close', + onClick: () => { + const translationBlocks = message.blocks + .map((blockId) => blockEntities[blockId]) + .filter((block) => block?.type === 'translation') + .map((block) => block?.id) + + if (translationBlocks.length > 0) { + translationBlocks.forEach((blockId) => { + if (blockId) removeMessageBlock(message.id, blockId) + }) + window.message.success({ content: t('translate.closed'), key: 'translate-close' }) } } } - }, - { - label: '✖ ' + t('translate.close'), - key: 'translate-close', - onClick: () => { - const translationBlocks = message.blocks - .map((blockId) => blockEntities[blockId]) - .filter((block) => block?.type === 'translation') - .map((block) => block?.id) - - if (translationBlocks.length > 0) { - translationBlocks.forEach((blockId) => { - if (blockId) removeMessageBlock(message.id, blockId) - }) - window.message.success({ content: t('translate.closed'), key: 'translate-close' }) - } - } - } - ] - : []) - ], - onClick: (e) => e.domEvent.stopPropagation() - }} - trigger={['click']} - placement="top" - arrow> - - e.stopPropagation()} - $softHoverBg={softHoverBg}> - + ] + : []) + ], + onClick: (e) => e.domEvent.stopPropagation() + }} + trigger={['click']} + placement="top" + arrow> + + e.stopPropagation()} + $softHoverBg={softHoverBg}> + + + + + )} + {isAssistantMessage && isGrouped && ( + + + {message.useful ? ( + + ) : ( + + )} - - )} - {isAssistantMessage && isGrouped && ( - - - {message.useful ? ( - - ) : ( - - )} - - - )} - } - onOpenChange={(open) => open && setShowDeleteTooltip(false)} - onConfirm={() => deleteMessage(message.id)}> - e.stopPropagation()} $softHoverBg={softHoverBg}> - - - - - - {!isUserMessage && ( - e.domEvent.stopPropagation() }} - trigger={['click']} - placement="topRight"> + )} + } + onOpenChange={(open) => open && setShowDeleteTooltip(false)} + onConfirm={() => deleteMessage(message.id)}> e.stopPropagation()} $softHoverBg={softHoverBg}> - + + + - - )} - + + {!isUserMessage && ( + e.domEvent.stopPropagation() }} + trigger={['click']} + placement="topRight"> + e.stopPropagation()} + $softHoverBg={softHoverBg}> + + + + )} + + ) } @@ -525,7 +597,11 @@ const MenusBar = styled.div` flex-direction: row; justify-content: flex-end; align-items: center; - gap: 6px; + gap: 8px; + + &.user-bubble-style { + margin-top: 5px; + } ` const ActionButton = styled.div<{ $softHoverBg?: boolean }>` @@ -535,8 +611,8 @@ const ActionButton = styled.div<{ $softHoverBg?: boolean }>` flex-direction: row; justify-content: center; align-items: center; - width: 30px; - height: 30px; + width: 26px; + height: 26px; transition: all 0.2s ease; &:hover { background-color: ${(props) => diff --git a/src/renderer/src/pages/home/Messages/MessageTokens.tsx b/src/renderer/src/pages/home/Messages/MessageTokens.tsx index 3326e061de..851350a474 100644 --- a/src/renderer/src/pages/home/Messages/MessageTokens.tsx +++ b/src/renderer/src/pages/home/Messages/MessageTokens.tsx @@ -11,7 +11,7 @@ interface MessageTokensProps { isLastMessage?: boolean } -const MessgeTokens: React.FC = ({ message }) => { +const MessageTokens: React.FC = ({ message }) => { const { showTokens } = useSettings() // const { generating } = useRuntime() const locateMessage = () => { @@ -106,4 +106,4 @@ const MessageMetadata = styled.div` } ` -export default MessgeTokens +export default MessageTokens diff --git a/src/renderer/src/pages/home/Messages/MessageTools.tsx b/src/renderer/src/pages/home/Messages/MessageTools.tsx index 4490c3000c..82503c83ef 100644 --- a/src/renderer/src/pages/home/Messages/MessageTools.tsx +++ b/src/renderer/src/pages/home/Messages/MessageTools.tsx @@ -1,24 +1,73 @@ -import { CheckOutlined, ExpandOutlined, LoadingOutlined, WarningOutlined } from '@ant-design/icons' +import { CheckOutlined, CloseOutlined, ExpandOutlined, LoadingOutlined, WarningOutlined } from '@ant-design/icons' import { useCodeStyle } from '@renderer/context/CodeStyleProvider' +import { useMCPServers } from '@renderer/hooks/useMCPServers' import { useSettings } from '@renderer/hooks/useSettings' import type { ToolMessageBlock } from '@renderer/types/newMessage' -import { Collapse, message as antdMessage, Modal, Tabs, Tooltip } from 'antd' -import { FC, memo, useEffect, useMemo, useState } from 'react' +import { isToolAutoApproved } from '@renderer/utils/mcp-tools' +import { cancelToolAction, confirmToolAction } from '@renderer/utils/userConfirmation' +import { Button, Collapse, ConfigProvider, Dropdown, Flex, message as antdMessage, Modal, Tabs, Tooltip } from 'antd' +import { message } from 'antd' +import Logger from 'electron-log/renderer' +import { ChevronDown, ChevronRight, CirclePlay, CircleX, PauseCircle, ShieldCheck } from 'lucide-react' +import { FC, memo, useEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' interface Props { - blocks: ToolMessageBlock + block: ToolMessageBlock } -const MessageTools: FC = ({ blocks }) => { +const COUNTDOWN_TIME = 30 + +const MessageTools: FC = ({ block }) => { const [activeKeys, setActiveKeys] = useState([]) const [copiedMap, setCopiedMap] = useState>({}) - const [expandedResponse, setExpandedResponse] = useState<{ content: string; title: string } | null>(null) + const [countdown, setCountdown] = useState(COUNTDOWN_TIME) const { t } = useTranslation() const { messageFont, fontSize } = useSettings() + const { mcpServers, updateMCPServer } = useMCPServers() + const [expandedResponse, setExpandedResponse] = useState<{ content: string; title: string } | null>(null) - const toolResponse = blocks.metadata?.rawMcpToolResponse + const toolResponse = block.metadata?.rawMcpToolResponse + + const { id, tool, status, response } = toolResponse! + + const isPending = status === 'pending' + const isInvoking = status === 'invoking' + const isDone = status === 'done' + + const timer = useRef(null) + useEffect(() => { + if (!isPending) return + + if (countdown > 0) { + timer.current = setTimeout(() => { + console.log('countdown', countdown) + setCountdown((prev) => prev - 1) + }, 1000) + } else if (countdown === 0) { + confirmToolAction(id) + } + + return () => { + if (timer.current) { + clearTimeout(timer.current) + } + } + }, [countdown, id, isPending]) + + const cancelCountdown = () => { + if (timer.current) { + clearTimeout(timer.current) + } + } + + const argsString = useMemo(() => { + if (toolResponse?.arguments) { + return JSON.stringify(toolResponse.arguments, null, 2) + } + return 'No arguments' + }, [toolResponse]) const resultString = useMemo(() => { try { @@ -50,13 +99,106 @@ const MessageTools: FC = ({ blocks }) => { setActiveKeys(Array.isArray(keys) ? keys : [keys]) } + const handleConfirmTool = () => { + cancelCountdown() + confirmToolAction(id) + } + + const handleCancelTool = () => { + cancelCountdown() + cancelToolAction(id) + } + + const handleAbortTool = async () => { + if (toolResponse?.id) { + try { + const success = await window.api.mcp.abortTool(toolResponse.id) + if (success) { + window.message.success({ content: t('message.tools.aborted'), key: 'abort-tool' }) + } else { + message.error({ content: t('message.tools.abort_failed'), key: 'abort-tool' }) + } + } catch (error) { + Logger.error('Failed to abort tool:', error) + message.error({ content: t('message.tools.abort_failed'), key: 'abort-tool' }) + } + } + } + + const handleAutoApprove = async () => { + cancelCountdown() + + if (!tool || !tool.name) { + return + } + + const server = mcpServers.find((s) => s.id === tool.serverId) + if (!server) { + return + } + + let disabledAutoApproveTools = [...(server.disabledAutoApproveTools || [])] + + // Remove tool from disabledAutoApproveTools to enable auto-approve + disabledAutoApproveTools = disabledAutoApproveTools.filter((name) => name !== tool.name) + + const updatedServer = { + ...server, + disabledAutoApproveTools + } + + updateMCPServer(updatedServer) + + // Also confirm the current tool + confirmToolAction(id) + + window.message.success({ + content: t('message.tools.autoApproveEnabled', 'Auto-approve enabled for this tool'), + key: 'auto-approve' + }) + } + + const renderStatusIndicator = (status: string, hasError: boolean) => { + let label = '' + let icon: React.ReactNode | null = null + switch (status) { + case 'pending': + label = t('message.tools.pending', 'Awaiting Approval') + icon = + break + case 'invoking': + label = t('message.tools.invoking') + icon = + break + case 'cancelled': + label = t('message.tools.cancelled') + icon = + break + case 'done': + if (hasError) { + label = t('message.tools.error') + icon = + } else { + label = t('message.tools.completed') + icon = + } + break + default: + label = '' + icon = null + } + return ( + + {label} + {icon} + + ) + } + // Format tool responses for collapse items const getCollapseItems = () => { const items: { key: string; label: React.ReactNode; children: React.ReactNode }[] = [] - const { id, tool, status, response } = toolResponse - const isInvoking = status === 'invoking' - const isDone = status === 'done' - const hasError = isDone && response?.isError === true + const hasError = response?.isError === true const result = { params: toolResponse.arguments, response: toolResponse.response @@ -67,61 +209,66 @@ const MessageTools: FC = ({ blocks }) => { label: ( - {tool.name} - - {isInvoking - ? t('message.tools.invoking') - : hasError - ? t('message.tools.error') - : t('message.tools.completed')} - {isInvoking && } - {isDone && !hasError && } - {hasError && } - + + {tool.serverName} : {tool.name} + {isToolAutoApproved(tool) && ( + + + + )} + - {isDone && response && ( - <> - - { - e.stopPropagation() - setExpandedResponse({ - content: JSON.stringify(response, null, 2), - title: tool.name - }) - }} - aria-label={t('common.expand')}> - - - - - { - e.stopPropagation() - copyContent(JSON.stringify(result, null, 2), id) - }} - aria-label={t('common.copy')}> - {!copiedMap[id] && } - {copiedMap[id] && } - - - + + {renderStatusIndicator(status, hasError)} + + + { + e.stopPropagation() + setExpandedResponse({ + content: JSON.stringify(response, null, 2), + title: tool.name + }) + }} + aria-label={t('common.expand')}> + + + + {!isPending && !isInvoking && ( + + { + e.stopPropagation() + copyContent(JSON.stringify(result, null, 2), id) + }} + aria-label={t('common.copy')}> + {!copiedMap[id] && } + {copiedMap[id] && } + + )} ), - children: isDone && result && ( - - - - ) + children: + isDone && result ? ( + + + + ) : argsString ? ( + <> + + + + + ) : null }) return items @@ -134,29 +281,109 @@ const MessageTools: FC = ({ blocks }) => { const parsedResult = JSON.parse(content) switch (parsedResult.content[0]?.type) { case 'text': - return {parsedResult.content[0].text} + return ( + + ) + default: - return {content} + return } } catch (e) { console.error('failed to render the preview of mcp results:', e) - return {content} + return } } return ( <> - ( - - )} - /> + + + + ( + + )} + /> + {(isPending || isInvoking) && ( + + + {isPending ? t('settings.mcp.tools.autoApprove.tooltip.confirm') : t('message.tools.invoking')} + + + {isPending && ( + + )} + {isInvoking && toolResponse?.id ? ( + + ) : ( + } + onClick={() => { + handleConfirmTool() + }} + menu={{ + items: [ + { + key: 'autoApprove', + label: t('settings.mcp.tools.autoApprove'), + onClick: () => { + handleAutoApprove() + } + } + ] + }}> + + + {t('settings.mcp.tools.run', 'Run')} ({countdown}s) + + + )} + + + )} + + + = ({ blocks }) => { { key: 'preview', label: t('message.tools.preview'), - children: + children: renderPreview(expandedResponse.content) }, { key: 'raw', label: t('message.tools.raw'), - children: renderPreview(expandedResponse.content) + children: ( + + ) } ]} /> @@ -229,26 +465,87 @@ const CollapsedContent: FC<{ isExpanded: boolean; resultString: string }> = ({ i return } -const CollapseContainer = styled(Collapse)` - margin-top: 10px; - margin-bottom: 12px; +const ToolContentWrapper = styled.div` + padding: 1px; border-radius: 8px; overflow: hidden; - .ant-collapse-header { - background-color: var(--color-bg-2); - transition: background-color 0.2s; + .ant-collapse { + border: 1px solid var(--color-border); + } - &:hover { - background-color: var(--color-bg-3); + &.pending, + &.invoking { + background-color: var(--color-background-soft); + .ant-collapse { + border: none; } } +` + +const ActionsBar = styled.div` + padding: 8px; + display: flex; + flex-direction: row; + align-items: center; + justify-content: space-between; +` + +const ActionLabel = styled.div` + flex: 1; + font-size: 14px; + color: var(--color-text-2); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +` + +const ActionButtonsGroup = styled.div` + display: flex; + gap: 10px; +` + +const CountdownText = styled.span` + width: 65px; + text-align: left; +` + +const StyledDropdownButton = styled(Dropdown.Button)` + .ant-btn-group { + border-radius: 6px; + } +` + +const ExpandIcon = styled(ChevronRight)<{ $isActive?: boolean }>` + transition: transform 0.2s; + transform: ${({ $isActive }) => ($isActive ? 'rotate(90deg)' : 'rotate(0deg)')}; +` + +const CollapseContainer = styled(Collapse)` + --status-color-warning: var(--color-warning, #faad14); + --status-color-invoking: var(--color-primary); + --status-color-error: var(--color-error, #ff4d4f); + --status-color-success: var(--color-success, green); + border-radius: 7px; + border: none; + background-color: var(--color-background); + overflow: hidden; + + .ant-collapse-header { + padding: 8px 10px !important; + align-items: center !important; + } .ant-collapse-content-box { padding: 0 !important; } ` +const ToolContainer = styled.div` + margin-top: 10px; + margin-bottom: 10px; +` + const MarkdownContainer = styled.div` & pre { background: transparent !important; @@ -264,9 +561,9 @@ const MessageTitleLabel = styled.div` align-items: center; justify-content: space-between; width: 100%; - min-height: 26px; gap: 10px; padding: 0; + margin-left: 4px; ` const TitleContent = styled.div` @@ -276,30 +573,40 @@ const TitleContent = styled.div` gap: 8px; ` -const ToolName = styled.span` +const ToolName = styled(Flex)` color: var(--color-text); font-weight: 500; font-size: 13px; ` -const StatusIndicator = styled.span<{ $isInvoking: boolean; $hasError?: boolean }>` +const StatusIndicator = styled.span<{ status: string; hasError?: boolean }>` color: ${(props) => { - if (props.$hasError) return 'var(--color-error, #ff4d4f)' - if (props.$isInvoking) return 'var(--color-primary)' - return 'var(--color-success, #52c41a)' + switch (props.status) { + case 'pending': + return 'var(--status-color-warning)' + case 'invoking': + return 'var(--status-color-invoking)' + case 'cancelled': + return 'var(--status-color-error)' + case 'done': + return props.hasError ? 'var(--status-color-error)' : 'var(--status-color-success)' + default: + return 'var(--color-text)' + } }}; font-size: 11px; + font-weight: ${(props) => (props.status === 'pending' ? '600' : '400')}; display: flex; align-items: center; - opacity: 0.85; - border-left: 1px solid var(--color-border); - padding-left: 8px; + opacity: ${(props) => (props.status === 'pending' ? '1' : '0.85')}; + padding-left: 12px; ` const ActionButtonsContainer = styled.div` display: flex; - gap: 8px; + gap: 6px; margin-left: auto; + align-items: center; ` const ActionButton = styled.button` @@ -307,18 +614,30 @@ const ActionButton = styled.button` border: none; color: var(--color-text-2); cursor: pointer; - padding: 4px 8px; + padding: 4px; display: flex; align-items: center; justify-content: center; opacity: 0.7; transition: all 0.2s; border-radius: 4px; + gap: 4px; + min-width: 28px; + height: 28px; &:hover { opacity: 1; color: var(--color-text); - background-color: var(--color-bg-1); + background-color: var(--color-bg-3); + } + + &.confirm-button { + color: var(--color-primary); + + &:hover { + background-color: var(--color-primary-bg); + color: var(--color-primary); + } } &:focus-visible { @@ -332,12 +651,6 @@ const ActionButton = styled.button` } ` -const CollapsibleIcon = styled.i` - color: var(--color-text-2); - font-size: 12px; - transition: transform 0.2s; -` - const ToolResponseContainer = styled.div` border-radius: 0 0 4px 4px; overflow: auto; @@ -346,14 +659,6 @@ const ToolResponseContainer = styled.div` position: relative; ` -const PreviewBlock = styled.div` - margin: 0; - white-space: pre-wrap; - word-break: break-word; - color: var(--color-text); - user-select: text; -` - const ExpandedResponseContainer = styled.div` background: var(--color-bg-1); border-radius: 8px; diff --git a/src/renderer/src/pages/home/Messages/Messages.tsx b/src/renderer/src/pages/home/Messages/Messages.tsx index 999ddc05ec..03434a0cd1 100644 --- a/src/renderer/src/pages/home/Messages/Messages.tsx +++ b/src/renderer/src/pages/home/Messages/Messages.tsx @@ -275,6 +275,7 @@ const Messages: React.FC = ({ assistant, topic, setActiveTopic, o }, [onComponentUpdate]) const groupedMessages = useMemo(() => Object.entries(getGroupedMessages(displayMessages)), [displayMessages]) + return ( = ({ assistant, topic }) => { } const Container = styled.div<{ $isDark: boolean }>` - padding: 10px 16px; + padding: 11px 16px; border-radius: 10px; cursor: pointer; border: 0.5px solid var(--color-border); - margin: 10px 10px 0 10px; + margin: 15px 24px; + margin-bottom: 0; ` const Text = styled.div` diff --git a/src/renderer/src/pages/home/Navbar.tsx b/src/renderer/src/pages/home/Navbar.tsx index c9e2797a8b..4ef2c7e673 100644 --- a/src/renderer/src/pages/home/Navbar.tsx +++ b/src/renderer/src/pages/home/Navbar.tsx @@ -1,7 +1,6 @@ import { Navbar, NavbarLeft, NavbarRight } from '@renderer/components/app/Navbar' import { HStack } from '@renderer/components/Layout' import FloatingSidebar from '@renderer/components/Popups/FloatingSidebar' -import MinAppsPopover from '@renderer/components/Popups/MinAppsPopover' import SearchPopup from '@renderer/components/Popups/SearchPopup' import { isMac } from '@renderer/config/constant' import { useAssistant } from '@renderer/hooks/useAssistant' @@ -16,7 +15,7 @@ import { setNarrowMode } from '@renderer/store/settings' import { Assistant, Topic } from '@renderer/types' import { Tooltip } from 'antd' import { t } from 'i18next' -import { LayoutGrid, MessageSquareDiff, PanelLeftClose, PanelRightClose, Search } from 'lucide-react' +import { MessageSquareDiff, PanelLeftClose, PanelRightClose, Search } from 'lucide-react' import { FC, useCallback, useState } from 'react' import styled from 'styled-components' @@ -35,7 +34,7 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo const { assistant } = useAssistant(activeAssistant.id) const { showAssistants, toggleShowAssistants } = useShowAssistants() const isFullscreen = useFullscreen() - const { topicPosition, sidebarIcons, narrowMode } = useSettings() + const { topicPosition, narrowMode } = useSettings() const { showTopics, toggleShowTopics } = useShowTopics() const dispatch = useAppDispatch() const [sidebarHideCooldown, setSidebarHideCooldown] = useState(false) @@ -145,15 +144,6 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo - {sidebarIcons.visible.includes('minapp') && ( - - - - - - - - )} {topicPosition === 'right' && !showTopics && !sidebarHideCooldown && ( = ({ {group.tag} - + )} {!collapsedTags[group.tag] && (
- handleGroupReorder(group.tag, newList)} onDragStart={() => setDragging(true)} @@ -111,7 +111,7 @@ const Assistants: FC = ({ handleSortByChange={handleSortByChange} /> )} - +
)} @@ -129,7 +129,7 @@ const Assistants: FC = ({ return ( - setDragging(true)} @@ -148,7 +148,7 @@ const Assistants: FC = ({ handleSortByChange={handleSortByChange} /> )} - + {!dragging && ( @@ -167,6 +167,7 @@ const Container = styled(Scrollbar)` display: flex; flex-direction: column; padding: 10px; + margin-top: 3px; ` const TagsContainer = styled.div` @@ -197,13 +198,16 @@ const AssistantAddItem = styled.div` ` const GroupTitle = styled.div` - padding: 8px 0; - position: relative; color: var(--color-text-2); font-size: 12px; font-weight: 500; - margin-bottom: -8px; cursor: pointer; + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + height: 24px; + margin: 5px 0; ` const GroupTitleName = styled.div` @@ -211,13 +215,18 @@ const GroupTitleName = styled.div` text-overflow: ellipsis; white-space: nowrap; overflow: hidden; - background-color: var(--color-background); box-sizing: border-box; padding: 0 4px; color: var(--color-text); - position: absolute; - transform: translateY(2px); font-size: 13px; + line-height: 24px; + margin-right: 5px; + display: flex; +` + +const GroupTitleDivider = styled.div` + flex: 1; + border-top: 1px solid var(--color-border); ` const AssistantName = styled.div` diff --git a/src/renderer/src/pages/home/Tabs/SettingsTab.tsx b/src/renderer/src/pages/home/Tabs/SettingsTab.tsx index 67c3ba7b97..0725907df6 100644 --- a/src/renderer/src/pages/home/Tabs/SettingsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/SettingsTab.tsx @@ -8,6 +8,7 @@ import { isSupportedFlexServiceTier, isSupportedReasoningEffortOpenAIModel } from '@renderer/config/models' +import { translateLanguageOptions } from '@renderer/config/translate' import { useCodeStyle } from '@renderer/context/CodeStyleProvider' import { useTheme } from '@renderer/context/ThemeProvider' import { useAssistant } from '@renderer/hooks/useAssistant' @@ -40,18 +41,10 @@ import { setRenderInputMessageAsMarkdown, setShowInputEstimatedTokens, setShowPrompt, - setShowTokens, setShowTranslateConfirm, setThoughtAutoCollapse } from '@renderer/store/settings' -import { - Assistant, - AssistantSettings, - CodeStyleVarious, - MathEngine, - ThemeMode, - TranslateLanguageVarious -} from '@renderer/types' +import { Assistant, AssistantSettings, CodeStyleVarious, MathEngine, ThemeMode } from '@renderer/types' import { modalConfirm } from '@renderer/utils' import { getSendMessageShortcutLabel } from '@renderer/utils/input' import { Button, Col, InputNumber, Row, Slider, Switch, Tooltip } from 'antd' @@ -108,8 +101,7 @@ const SettingsTab: FC = (props) => { messageNavigation, enableQuickPanelTriggers, enableBackspaceDeleteModel, - showTranslateConfirm, - showTokens + showTranslateConfirm } = useSettings() const onUpdateAssistantSettings = (settings: Partial) => { @@ -306,11 +298,6 @@ const SettingsTab: FC = (props) => { dispatch(setShowPrompt(checked))} /> - - {t('settings.messages.tokens')} - dispatch(setShowTokens(checked))} /> - - {t('settings.messages.use_serif_font')} = (props) => { {t('settings.input.target_language')} setTargetLanguage(value as TranslateLanguageVarious)} - options={[ - { value: 'chinese', label: t('settings.input.target_language.chinese') }, - { value: 'chinese-traditional', label: t('settings.input.target_language.chinese-traditional') }, - { value: 'english', label: t('settings.input.target_language.english') }, - { value: 'japanese', label: t('settings.input.target_language.japanese') }, - { value: 'russian', label: t('settings.input.target_language.russian') } - ]} + onChange={(value) => setTargetLanguage(value)} + options={translateLanguageOptions.map((item) => { + return { value: item.langCode, label: item.emoji + ' ' + item.label() } + })} /> diff --git a/src/renderer/src/pages/home/Tabs/TopicsTab.tsx b/src/renderer/src/pages/home/Tabs/TopicsTab.tsx index 01a548b8c1..d296ccdcf9 100644 --- a/src/renderer/src/pages/home/Tabs/TopicsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/TopicsTab.tsx @@ -9,11 +9,10 @@ import { QuestionCircleOutlined, UploadOutlined } from '@ant-design/icons' -import DragableList from '@renderer/components/DragableList' +import { DraggableVirtualList as DraggableList } from '@renderer/components/DraggableList' import CopyIcon from '@renderer/components/Icons/CopyIcon' import ObsidianExportPopup from '@renderer/components/Popups/ObsidianExportPopup' import PromptPopup from '@renderer/components/Popups/PromptPopup' -import Scrollbar from '@renderer/components/Scrollbar' import { isMac } from '@renderer/config/constant' import { useAssistant, useAssistants } from '@renderer/hooks/useAssistant' import { modelGenerating } from '@renderer/hooks/useRuntime' @@ -40,7 +39,7 @@ import { Dropdown, MenuProps, Tooltip } from 'antd' import { ItemType, MenuItemType } from 'antd/es/menu/interface' import dayjs from 'dayjs' import { findIndex } from 'lodash' -import { FC, startTransition, useCallback, useDeferredValue, useMemo, useRef, useState } from 'react' +import { FC, useCallback, useDeferredValue, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import styled from 'styled-components' @@ -169,9 +168,7 @@ const Topics: FC = ({ assistant: _assistant, activeTopic, setActiveTopic const onSwitchTopic = useCallback( async (topic: Topic) => { // await modelGenerating() - startTransition(() => { - setActiveTopic(topic) - }) + setActiveTopic(topic) }, [setActiveTopic] ) @@ -447,92 +444,86 @@ const Topics: FC = ({ assistant: _assistant, activeTopic, setActiveTopic }, [assistant.topics, pinTopicsToTop]) return ( - - - - {(topic) => { - const isActive = topic.id === activeTopic?.id - const topicName = topic.name.replace('`', '') - const topicPrompt = topic.prompt - const fullTopicPrompt = t('common.prompt') + ': ' + topicPrompt + + {(topic) => { + const isActive = topic.id === activeTopic?.id + const topicName = topic.name.replace('`', '') + const topicPrompt = topic.prompt + const fullTopicPrompt = t('common.prompt') + ': ' + topicPrompt - const getTopicNameClassName = () => { - if (isRenaming(topic.id)) return 'shimmer' - if (isNewlyRenamed(topic.id)) return 'typing' - return '' - } + const getTopicNameClassName = () => { + if (isRenaming(topic.id)) return 'shimmer' + if (isNewlyRenamed(topic.id)) return 'typing' + return '' + } - return ( - setTargetTopic(topic)} - className={isActive ? 'active' : ''} - onClick={() => onSwitchTopic(topic)} - style={{ borderRadius }}> - {isPending(topic.id) && !isActive && } - - - {topicName} - - {!topic.pinned && ( - -
- {t('chat.topics.delete.shortcut', { key: isMac ? '⌘' : 'Ctrl' })} -
+ return ( + + setTargetTopic(topic)} + className={isActive ? 'active' : ''} + onClick={() => onSwitchTopic(topic)} + style={{ borderRadius }}> + {isPending(topic.id) && !isActive && } + + + {topicName} + + {!topic.pinned && ( + +
+ {t('chat.topics.delete.shortcut', { key: isMac ? '⌘' : 'Ctrl' })}
- }> - { - if (e.ctrlKey || e.metaKey) { - handleConfirmDelete(topic, e) - } else if (deletingTopicId === topic.id) { - handleConfirmDelete(topic, e) - } else { - handleDeleteClick(topic.id, e) - } - }}> - {deletingTopicId === topic.id ? ( - - ) : ( - - )} - -
- )} - {topic.pinned && ( - - + + }> + { + if (e.ctrlKey || e.metaKey) { + handleConfirmDelete(topic, e) + } else if (deletingTopicId === topic.id) { + handleConfirmDelete(topic, e) + } else { + handleDeleteClick(topic.id, e) + } + }}> + {deletingTopicId === topic.id ? ( + + ) : ( + + )} - )} -
- {topicPrompt && ( - - {fullTopicPrompt} - +
)} - {showTopicTime && ( - {dayjs(topic.createdAt).format('MM/DD HH:mm')} + {topic.pinned && ( + + + )} -
- ) - }} -
-
-
-
+ + {topicPrompt && ( + + {fullTopicPrompt} + + )} + {showTopicTime && {dayjs(topic.createdAt).format('MM/DD HH:mm')}} + + + ) + }} + ) } -const Container = styled(Scrollbar)` - display: flex; - flex-direction: column; - padding: 10px; -` - const TopicListItem = styled.div` padding: 7px 12px; border-radius: var(--list-item-border-radius); diff --git a/src/renderer/src/pages/home/Tabs/components/AssistantItem.tsx b/src/renderer/src/pages/home/Tabs/components/AssistantItem.tsx index 518473bb14..f0ae3e8883 100644 --- a/src/renderer/src/pages/home/Tabs/components/AssistantItem.tsx +++ b/src/renderer/src/pages/home/Tabs/components/AssistantItem.tsx @@ -25,7 +25,7 @@ import { hasTopicPendingRequests } from '@renderer/utils/queue' import { Dropdown, MenuProps } from 'antd' import { omit } from 'lodash' import { AlignJustify, Plus, Settings2, Tag, Tags } from 'lucide-react' -import { FC, memo, startTransition, useCallback, useEffect, useMemo, useState } from 'react' +import { FC, memo, useCallback, useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' import * as tinyPinyin from 'tiny-pinyin' @@ -125,12 +125,8 @@ const AssistantItem: FC = ({ if (topicPosition === 'left') { EventEmitter.emit(EVENT_NAMES.SWITCH_TOPIC_SIDEBAR) } - onSwitch(assistant) - } else { - startTransition(() => { - onSwitch(assistant) - }) } + onSwitch(assistant) }, [clickAssistantToShowTopic, onSwitch, assistant, topicPosition]) const assistantName = useMemo(() => assistant.name || t('chat.default.name'), [assistant.name, t]) diff --git a/src/renderer/src/pages/home/Tabs/components/OpenAISettingsGroup.tsx b/src/renderer/src/pages/home/Tabs/components/OpenAISettingsGroup.tsx index 18b6800a6d..9d4cafbbd6 100644 --- a/src/renderer/src/pages/home/Tabs/components/OpenAISettingsGroup.tsx +++ b/src/renderer/src/pages/home/Tabs/components/OpenAISettingsGroup.tsx @@ -4,12 +4,11 @@ import { CollapsibleSettingGroup } from '@renderer/pages/settings/SettingGroup' import { RootState, useAppDispatch } from '@renderer/store' import { setOpenAIServiceTier, setOpenAISummaryText } from '@renderer/store/settings' import { OpenAIServiceTier, OpenAISummaryText } from '@renderer/types' -import { Select, Tooltip } from 'antd' +import { Tooltip } from 'antd' import { CircleHelp } from 'lucide-react' import { FC, useCallback, useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' -import styled from 'styled-components' interface Props { isOpenAIReasoning: boolean @@ -121,13 +120,11 @@ const OpenAISettingsGroup: FC = ({ - { setSummaryText(value as OpenAISummaryText) }} - size="small" options={summaryTextOptions} /> @@ -139,12 +136,4 @@ const OpenAISettingsGroup: FC = ({ ) } -const StyledSelect = styled(Select)` - .ant-select-selector { - border-radius: 15px !important; - padding: 4px 10px !important; - height: 26px !important; - } -` - export default OpenAISettingsGroup diff --git a/src/renderer/src/pages/knowledge/KnowledgeContent.tsx b/src/renderer/src/pages/knowledge/KnowledgeContent.tsx index 7d5d251660..4d4e702d4e 100644 --- a/src/renderer/src/pages/knowledge/KnowledgeContent.tsx +++ b/src/renderer/src/pages/knowledge/KnowledgeContent.tsx @@ -1,233 +1,121 @@ -import { CopyOutlined, DeleteOutlined, EditOutlined, RedoOutlined } from '@ant-design/icons' +import { RedoOutlined } from '@ant-design/icons' import CustomTag from '@renderer/components/CustomTag' -import Ellipsis from '@renderer/components/Ellipsis' import { HStack } from '@renderer/components/Layout' -import PromptPopup from '@renderer/components/Popups/PromptPopup' -import TextEditPopup from '@renderer/components/Popups/TextEditPopup' -import Scrollbar from '@renderer/components/Scrollbar' -import Logger from '@renderer/config/logger' import { useKnowledge } from '@renderer/hooks/useKnowledge' -import FileManager from '@renderer/services/FileManager' +import { NavbarIcon } from '@renderer/pages/home/Navbar' import { getProviderName } from '@renderer/services/ProviderService' -import { FileType, FileTypes, KnowledgeBase, KnowledgeItem } from '@renderer/types' -import { formatFileSize } from '@renderer/utils' -import { bookExts, documentExts, textExts, thirdPartyApplicationExts } from '@shared/config/constant' -import { Alert, Button, Dropdown, Empty, message, Tag, Tooltip, Upload } from 'antd' -import dayjs from 'dayjs' -import { ChevronsDown, ChevronsUp, Plus, Search, Settings2 } from 'lucide-react' -import VirtualList from 'rc-virtual-list' -import { FC, useState } from 'react' +import { KnowledgeBase } from '@renderer/types' +import { Button, Empty, Tabs, Tag, Tooltip } from 'antd' +import { Book, Folder, Globe, Link, Notebook, Search, Settings } from 'lucide-react' +import { FC, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' -import CustomCollapse from '../../components/CustomCollapse' -import FileItem from '../files/FileItem' -import { NavbarIcon } from '../home/Navbar' import KnowledgeSearchPopup from './components/KnowledgeSearchPopup' -import KnowledgeSettingsPopup from './components/KnowledgeSettingsPopup' -import StatusIcon from './components/StatusIcon' - -const { Dragger } = Upload +import KnowledgeSettings from './components/KnowledgeSettings' +import QuotaTag from './components/QuotaTag' +import KnowledgeDirectories from './items/KnowledgeDirectories' +import KnowledgeFiles from './items/KnowledgeFiles' +import KnowledgeNotes from './items/KnowledgeNotes' +import KnowledgeSitemaps from './items/KnowledgeSitemaps' +import KnowledgeUrls from './items/KnowledgeUrls' interface KnowledgeContentProps { selectedBase: KnowledgeBase } -const fileTypes = [...bookExts, ...thirdPartyApplicationExts, ...documentExts, ...textExts] - -const getDisplayTime = (item: KnowledgeItem) => { - const timestamp = item.updated_at && item.updated_at > item.created_at ? item.updated_at : item.created_at - return dayjs(timestamp).format('MM-DD HH:mm') -} - const KnowledgeContent: FC = ({ selectedBase }) => { const { t } = useTranslation() - const [expandAll, setExpandAll] = useState(false) - - const { - base, - noteItems, - fileItems, - urlItems, - sitemapItems, - directoryItems, - addFiles, - updateNoteContent, - refreshItem, - addUrl, - addSitemap, - removeItem, - getProcessingStatus, - getDirectoryProcessingPercent, - addNote, - addDirectory, - updateItem - } = useKnowledge(selectedBase.id || '') + const { base, urlItems, fileItems, directoryItems, noteItems, sitemapItems } = useKnowledge(selectedBase.id || '') + const [activeKey, setActiveKey] = useState('files') + const [quota, setQuota] = useState(undefined) + const [progressMap, setProgressMap] = useState>(new Map()) + const [preprocessMap, setPreprocessMap] = useState>(new Map()) const providerName = getProviderName(base?.model.provider || '') - const disabled = !base?.version || !providerName + + useEffect(() => { + const handlers = [ + window.electron.ipcRenderer.on('file-preprocess-finished', (_, { itemId, quota }) => { + setPreprocessMap((prev) => new Map(prev).set(itemId, true)) + if (quota) { + setQuota(quota) + } + }), + + window.electron.ipcRenderer.on('file-preprocess-progress', (_, { itemId, progress }) => { + setProgressMap((prev) => new Map(prev).set(itemId, progress)) + }), + + window.electron.ipcRenderer.on('file-ocr-progress', (_, { itemId, progress }) => { + setProgressMap((prev) => new Map(prev).set(itemId, progress)) + }), + + window.electron.ipcRenderer.on('directory-processing-percent', (_, { itemId, percent }) => { + console.log('[Progress] Directory:', itemId, percent) + setProgressMap((prev) => new Map(prev).set(itemId, percent)) + }) + ] + + return () => { + handlers.forEach((cleanup) => cleanup()) + } + }, []) + const knowledgeItems = [ + { + key: 'files', + title: t('files.title'), + icon: activeKey === 'files' ? : , + items: fileItems, + content: + }, + { + key: 'notes', + title: t('knowledge.notes'), + icon: activeKey === 'notes' ? : , + items: noteItems, + content: + }, + { + key: 'directories', + title: t('knowledge.directories'), + icon: activeKey === 'directories' ? : , + items: directoryItems, + content: + }, + { + key: 'urls', + title: t('knowledge.urls'), + icon: activeKey === 'urls' ? : , + items: urlItems, + content: + }, + { + key: 'sitemaps', + title: t('knowledge.sitemaps'), + icon: activeKey === 'sitemaps' ? : , + items: sitemapItems, + content: + } + ] if (!base) { return null } - const getProgressingPercentForItem = (itemId: string) => getDirectoryProcessingPercent(itemId) - - const handleAddFile = () => { - if (disabled) { - return - } - const input = document.createElement('input') - input.type = 'file' - input.multiple = true - input.accept = fileTypes.join(',') - input.onchange = (e) => { - const files = (e.target as HTMLInputElement).files - files && handleDrop(Array.from(files)) - } - input.click() - } - - const handleDrop = async (files: File[]) => { - if (disabled) { - return - } - - if (files) { - const _files: FileType[] = files - .map((file) => ({ - id: file.name, - name: file.name, - path: window.api.file.getPathForFile(file), - size: file.size, - ext: `.${file.name.split('.').pop()}`.toLowerCase(), - count: 1, - origin_name: file.name, - type: file.type as FileTypes, - created_at: new Date().toISOString() - })) - .filter(({ ext }) => fileTypes.includes(ext)) - const uploadedFiles = await FileManager.uploadFiles(_files) - addFiles(uploadedFiles) - } - } - - const handleAddUrl = async () => { - if (disabled) { - return - } - - const urlInput = await PromptPopup.show({ - title: t('knowledge.add_url'), - message: '', - inputPlaceholder: t('knowledge.url_placeholder'), - inputProps: { - rows: 10, - onPressEnter: () => {} - } - }) - - if (urlInput) { - // Split input by newlines and filter out empty lines - const urls = urlInput.split('\n').filter((url) => url.trim()) - - for (const url of urls) { - try { - new URL(url.trim()) - if (!urlItems.find((item) => item.content === url.trim())) { - addUrl(url.trim()) - } else { - message.success(t('knowledge.url_added')) - } - } catch (e) { - // Skip invalid URLs silently - continue - } - } - } - } - - const handleAddSitemap = async () => { - if (disabled) { - return - } - - const url = await PromptPopup.show({ - title: t('knowledge.add_sitemap'), - message: '', - inputPlaceholder: t('knowledge.sitemap_placeholder'), - inputProps: { - maxLength: 1000, - rows: 1 - } - }) - - if (url) { - try { - new URL(url) - if (sitemapItems.find((item) => item.content === url)) { - message.success(t('knowledge.sitemap_added')) - return - } - addSitemap(url) - } catch (e) { - console.error('Invalid Sitemap URL:', url) - } - } - } - - const handleAddNote = async () => { - if (disabled) { - return - } - - const note = await TextEditPopup.show({ text: '', textareaProps: { rows: 20 } }) - note && addNote(note) - } - - const handleEditNote = async (note: any) => { - if (disabled) { - return - } - - const editedText = await TextEditPopup.show({ text: note.content as string, textareaProps: { rows: 20 } }) - editedText && updateNoteContent(note.id, editedText) - } - - const handleAddDirectory = async () => { - if (disabled) { - return - } - - const path = await window.api.file.selectFolder() - Logger.log('[KnowledgeContent] Selected directory:', path) - path && addDirectory(path) - } - - const handleEditRemark = async (item: KnowledgeItem) => { - if (disabled) { - return - } - - const editedRemark: string | undefined = await PromptPopup.show({ - title: t('knowledge.edit_remark'), - message: '', - inputPlaceholder: t('knowledge.edit_remark_placeholder'), - defaultValue: item.remark || '', - inputProps: { - maxLength: 100, - rows: 1 - } - }) - - if (editedRemark !== undefined && editedRemark !== null) { - updateItem({ - ...item, - remark: editedRemark, - updated_at: Date.now() - }) - } - } + const tabItems = knowledgeItems.map((item) => ({ + key: item.key, + label: ( + + {item.icon} + {item.title} + 0 ? '#00b96b' : '#cccccc'}> + {item.items.length} + + + ), + children: {item.content} + })) return ( @@ -235,8 +123,8 @@ const KnowledgeContent: FC = ({ selectedBase }) => { - }> - handleDrop([file as File])} - multiple={true} - accept={fileTypes.join(',')} - style={{ marginTop: 10, background: 'transparent' }}> -

{t('knowledge.drag_file')}

-

- {t('knowledge.file_hint', { file_types: 'TXT, MD, HTML, PDF, DOCX, PPTX, XLSX, EPUB...' })} -

-
- - - {fileItems.length === 0 ? ( - - ) : ( - 5 ? 400 : fileItems.length * 75} - itemHeight={75} - itemKey="id" - styles={{ - verticalScrollBar: { - width: 6 - }, - verticalScrollBarThumb: { - background: 'var(--color-scrollbar-thumb)' - } - }}> - {(item) => { - const file = item.content as FileType - return ( -
- window.api.file.openPath(FileManager.getFilePath(file))}> - - {file.origin_name} - - - ), - ext: file.ext, - extra: `${getDisplayTime(item)} · ${formatFileSize(file.size)}`, - actions: ( - - {item.uniqueId && ( -
- ) - }} -
- )} -
- - - } - defaultActiveKey={[]} - activeKey={expandAll ? ['1'] : undefined} - extra={ - - }> - - {directoryItems.length === 0 && } - {directoryItems.reverse().map((item) => ( - window.api.file.openPath(item.content as string)}> - - {item.content as string} - - - ), - ext: '.folder', - extra: getDisplayTime(item), - actions: ( - - {item.uniqueId && - }> - - {urlItems.length === 0 && } - {urlItems.reverse().map((item) => ( - , - label: t('knowledge.edit_remark'), - onClick: () => handleEditRemark(item) - }, - { - key: 'copy', - icon: , - label: t('common.copy'), - onClick: () => { - navigator.clipboard.writeText(item.content as string) - message.success(t('message.copied')) - } - } - ] - }} - trigger={['contextMenu']}> - - - - - {item.remark || (item.content as string)} - - - - - - ), - ext: '.url', - extra: getDisplayTime(item), - actions: ( - - {item.uniqueId && - }> - - {sitemapItems.length === 0 && } - {sitemapItems.reverse().map((item) => ( - - - - - {item.content as string} - - - - - ), - ext: '.sitemap', - extra: getDisplayTime(item), - actions: ( - - {item.uniqueId && - }> - - {noteItems.length === 0 && } - {noteItems.reverse().map((note) => ( - handleEditNote(note)}>{(note.content as string).slice(0, 50)}..., - ext: '.txt', - extra: getDisplayTime(note), - actions: ( - - - -
- maxContext) { - return Promise.reject(new Error(t('knowledge.chunk_size_too_large', { max_context: maxContext }))) - } - return Promise.resolve() - } - } - ]}> - - - ({ - validator(_, value) { - if (!value || getFieldValue('chunkSize') > value) { - return Promise.resolve() - } - return Promise.reject(new Error(t('message.error.chunk_overlap_too_large'))) - } - }) - ]} - dependencies={['chunkSize']}> - - - - 1 || value < 0)) { - return Promise.reject(new Error(t('knowledge.threshold_too_large_or_small'))) - } - return Promise.resolve() - } - } - ]}> - - - - } - /> -
- -
- ) -} - -const TopViewKey = 'KnowledgeSettingsPopup' - -export default class KnowledgeSettingsPopup { - static hide() { - TopView.hide(TopViewKey) - } - - static show(props: ShowParams) { - return new Promise((resolve) => { - TopView.show( - { - resolve(v) - TopView.hide(TopViewKey) - }} - />, - TopViewKey - ) - }) - } -} diff --git a/src/renderer/src/pages/knowledge/components/QuotaTag.tsx b/src/renderer/src/pages/knowledge/components/QuotaTag.tsx new file mode 100644 index 0000000000..fb3f60fe54 --- /dev/null +++ b/src/renderer/src/pages/knowledge/components/QuotaTag.tsx @@ -0,0 +1,66 @@ +import { usePreprocessProvider } from '@renderer/hooks/usePreprocess' +import { getStoreSetting } from '@renderer/hooks/useSettings' +import { getKnowledgeBaseParams } from '@renderer/services/KnowledgeService' +import { KnowledgeBase } from '@renderer/types' +import { Tag } from 'antd' +import { FC, useEffect, useState } from 'react' +import { useTranslation } from 'react-i18next' + +const QuotaTag: FC<{ base: KnowledgeBase; providerId: string; quota?: number }> = ({ + base, + providerId, + quota: _quota +}) => { + const { t } = useTranslation() + const { provider, updateProvider } = usePreprocessProvider(providerId) + const [quota, setQuota] = useState(_quota) + + useEffect(() => { + const checkQuota = async () => { + if (provider.id !== 'mineru') return + // 使用用户的key时quota为无限 + if (provider.apiKey) { + setQuota(-9999) + updateProvider({ quota: -9999 }) + return + } + if (quota === undefined) { + const userId = getStoreSetting('userId') + const baseParams = getKnowledgeBaseParams(base) + try { + const response = await window.api.knowledgeBase.checkQuota({ + base: baseParams, + userId: userId as string + }) + setQuota(response) + } catch (error) { + console.error('[KnowledgeContent] Error checking quota:', error) + } + } + } + if (_quota !== undefined) { + updateProvider({ quota: _quota }) + return + } + checkQuota() + }, [_quota, base, provider.id, provider.apiKey, provider, quota, updateProvider]) + + return ( + <> + {quota && ( + + {quota === -9999 + ? t('knowledge.quota_infinity', { + name: provider.name + }) + : t('knowledge.quota', { + name: provider.name, + quota: quota + })} + + )} + + ) +} + +export default QuotaTag diff --git a/src/renderer/src/pages/knowledge/components/StatusIcon.tsx b/src/renderer/src/pages/knowledge/components/StatusIcon.tsx index 5bf98f5a35..465a7cef0f 100644 --- a/src/renderer/src/pages/knowledge/components/StatusIcon.tsx +++ b/src/renderer/src/pages/knowledge/components/StatusIcon.tsx @@ -1,7 +1,7 @@ import { CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons' import { KnowledgeBase, ProcessingStatus } from '@renderer/types' import { Progress, Tooltip } from 'antd' -import { FC } from 'react' +import React, { FC, useMemo } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' @@ -9,64 +9,81 @@ interface StatusIconProps { sourceId: string base: KnowledgeBase getProcessingStatus: (sourceId: string) => ProcessingStatus | undefined - getProcessingPercent?: (sourceId: string) => number | undefined type: string + progress?: number + isPreprocessed?: boolean } -const StatusIcon: FC = ({ sourceId, base, getProcessingStatus, getProcessingPercent, type }) => { +const StatusIcon: FC = ({ + sourceId, + base, + getProcessingStatus, + type, + progress = 0, + isPreprocessed +}) => { const { t } = useTranslation() const status = getProcessingStatus(sourceId) - const percent = getProcessingPercent?.(sourceId) const item = base.items.find((item) => item.id === sourceId) const errorText = item?.processingError + console.log('[StatusIcon] Rendering for item:', item?.id, 'Status:', status, 'Progress:', progress) - if (!status) { - if (item?.uniqueId) { + return useMemo(() => { + if (!status) { + if (item?.uniqueId) { + if (isPreprocessed && item.type === 'file') { + return ( + + + + ) + } + return ( + + + + ) + } return ( - - + + ) } - return ( - - - - ) - } - switch (status) { - case 'pending': - return ( - - - - ) + switch (status) { + case 'pending': + return ( + + + + ) - case 'processing': { - return type === 'directory' ? ( - - ) : ( - - - - ) + case 'processing': { + return type === 'directory' || type === 'file' ? ( + + ) : ( + + + + ) + } + case 'completed': + return ( + + + + ) + case 'failed': + return ( + + + + ) + default: + return null } - case 'completed': - return ( - - - - ) - case 'failed': - return ( - - - - ) - default: - return null - } + }, [status, item?.uniqueId, item?.type, t, isPreprocessed, errorText, type, progress]) } const StatusDot = styled.div<{ $status: 'pending' | 'processing' | 'new' }>` @@ -91,4 +108,14 @@ const StatusDot = styled.div<{ $status: 'pending' | 'processing' | 'new' }>` } ` -export default StatusIcon +export default React.memo(StatusIcon, (prevProps, nextProps) => { + return ( + prevProps.sourceId === nextProps.sourceId && + prevProps.type === nextProps.type && + prevProps.base.id === nextProps.base.id && + prevProps.progress === nextProps.progress && + prevProps.getProcessingStatus(prevProps.sourceId) === nextProps.getProcessingStatus(nextProps.sourceId) && + prevProps.base.items.find((item) => item.id === prevProps.sourceId)?.processingError === + nextProps.base.items.find((item) => item.id === nextProps.sourceId)?.processingError + ) +}) diff --git a/src/renderer/src/pages/knowledge/items/KnowledgeDirectories.tsx b/src/renderer/src/pages/knowledge/items/KnowledgeDirectories.tsx new file mode 100644 index 0000000000..3cdd480c04 --- /dev/null +++ b/src/renderer/src/pages/knowledge/items/KnowledgeDirectories.tsx @@ -0,0 +1,121 @@ +import { DeleteOutlined } from '@ant-design/icons' +import Ellipsis from '@renderer/components/Ellipsis' +import Scrollbar from '@renderer/components/Scrollbar' +import Logger from '@renderer/config/logger' +import { useKnowledge } from '@renderer/hooks/useKnowledge' +import FileItem from '@renderer/pages/files/FileItem' +import { getProviderName } from '@renderer/services/ProviderService' +import { KnowledgeBase, KnowledgeItem } from '@renderer/types' +import { Button, Tooltip } from 'antd' +import dayjs from 'dayjs' +import { Plus } from 'lucide-react' +import { FC } from 'react' +import { useTranslation } from 'react-i18next' +import styled from 'styled-components' + +import StatusIcon from '../components/StatusIcon' +import { + ClickableSpan, + FlexAlignCenter, + ItemContainer, + ItemHeader, + KnowledgeEmptyView, + RefreshIcon, + StatusIconWrapper +} from '../KnowledgeContent' + +interface KnowledgeContentProps { + selectedBase: KnowledgeBase + progressMap: Map +} + +const getDisplayTime = (item: KnowledgeItem) => { + const timestamp = item.updated_at && item.updated_at > item.created_at ? item.updated_at : item.created_at + return dayjs(timestamp).format('MM-DD HH:mm') +} + +const KnowledgeDirectories: FC = ({ selectedBase, progressMap }) => { + const { t } = useTranslation() + + const { base, directoryItems, refreshItem, removeItem, getProcessingStatus, addDirectory } = useKnowledge( + selectedBase.id || '' + ) + + const providerName = getProviderName(base?.model.provider || '') + const disabled = !base?.version || !providerName + + if (!base) { + return null + } + + const handleAddDirectory = async () => { + if (disabled) { + return + } + + const path = await window.api.file.selectFolder() + Logger.log('[KnowledgeContent] Selected directory:', path) + path && addDirectory(path) + } + + return ( + + + + + + {directoryItems.length === 0 && } + {directoryItems.reverse().map((item) => ( + window.api.file.openPath(item.content as string)}> + + {item.content as string} + + + ), + ext: '.folder', + extra: getDisplayTime(item), + actions: ( + + {item.uniqueId && + + + + handleDrop([file as File])} + multiple={true} + accept={fileTypes.join(',')}> +

{t('knowledge.drag_file')}

+

+ {t('knowledge.file_hint', { file_types: 'TXT, MD, HTML, PDF, DOCX, PPTX, XLSX, EPUB...' })} +

+
+ {fileItems.length === 0 ? ( + + ) : ( + + {(item) => { + const file = item.content as FileType + return ( +
+ window.api.file.openFileWithRelativePath(file)}> + + {file.origin_name} + + + ), + ext: file.ext, + extra: `${getDisplayTime(item)} · ${formatFileSize(file.size)}`, + actions: ( + + {item.uniqueId && ( +
+ ) + }} +
+ )} +
+
+ ) +} + +const ItemFlexColumn = styled.div` + display: flex; + flex-direction: column; + padding: 20px 16px; + gap: 10px; +` + +export default KnowledgeFiles diff --git a/src/renderer/src/pages/knowledge/items/KnowledgeNotes.tsx b/src/renderer/src/pages/knowledge/items/KnowledgeNotes.tsx new file mode 100644 index 0000000000..ca3f8c4d17 --- /dev/null +++ b/src/renderer/src/pages/knowledge/items/KnowledgeNotes.tsx @@ -0,0 +1,107 @@ +import { DeleteOutlined, EditOutlined } from '@ant-design/icons' +import TextEditPopup from '@renderer/components/Popups/TextEditPopup' +import Scrollbar from '@renderer/components/Scrollbar' +import { useKnowledge } from '@renderer/hooks/useKnowledge' +import FileItem from '@renderer/pages/files/FileItem' +import { getProviderName } from '@renderer/services/ProviderService' +import { KnowledgeBase, KnowledgeItem } from '@renderer/types' +import { Button } from 'antd' +import dayjs from 'dayjs' +import { Plus } from 'lucide-react' +import { FC } from 'react' +import { useTranslation } from 'react-i18next' +import styled from 'styled-components' + +import StatusIcon from '../components/StatusIcon' +import { FlexAlignCenter, ItemContainer, ItemHeader, KnowledgeEmptyView, StatusIconWrapper } from '../KnowledgeContent' + +interface KnowledgeContentProps { + selectedBase: KnowledgeBase +} + +const getDisplayTime = (item: KnowledgeItem) => { + const timestamp = item.updated_at && item.updated_at > item.created_at ? item.updated_at : item.created_at + return dayjs(timestamp).format('MM-DD HH:mm') +} + +const KnowledgeNotes: FC = ({ selectedBase }) => { + const { t } = useTranslation() + + const { base, noteItems, updateNoteContent, removeItem, getProcessingStatus, addNote } = useKnowledge( + selectedBase.id || '' + ) + + const providerName = getProviderName(base?.model.provider || '') + const disabled = !base?.version || !providerName + + if (!base) { + return null + } + + const handleAddNote = async () => { + if (disabled) { + return + } + + const note = await TextEditPopup.show({ text: '', textareaProps: { rows: 20 } }) + note && addNote(note) + } + + const handleEditNote = async (note: any) => { + if (disabled) { + return + } + + const editedText = await TextEditPopup.show({ text: note.content as string, textareaProps: { rows: 20 } }) + editedText && updateNoteContent(note.id, editedText) + } + + return ( + + + + + + {noteItems.length === 0 && } + {noteItems.reverse().map((note) => ( + handleEditNote(note)}>{(note.content as string).slice(0, 50)}..., + ext: '.txt', + extra: getDisplayTime(note), + actions: ( + + + + + {sitemapItems.length === 0 && } + {sitemapItems.reverse().map((item) => ( + + + + + {item.content as string} + + + + + ), + ext: '.sitemap', + extra: getDisplayTime(item), + actions: ( + + {item.uniqueId && + + + {urlItems.length === 0 && } + {urlItems.reverse().map((item) => ( + , + label: t('knowledge.edit_remark'), + onClick: () => handleEditRemark(item) + }, + { + key: 'copy', + icon: , + label: t('common.copy'), + onClick: () => { + navigator.clipboard.writeText(item.content as string) + window.message.success(t('message.copied')) + } + } + ] + }} + trigger={['contextMenu']}> + + + + + {item.remark || (item.content as string)} + + + + + + ), + ext: '.url', + extra: getDisplayTime(item), + actions: ( + + {item.uniqueId &&