mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 06:30:10 +08:00
Merge branch 'main' into feat/sync_mcprouter
This commit is contained in:
commit
ab39484001
8
.env.example
Normal file
8
.env.example
Normal file
@ -0,0 +1,8 @@
|
||||
NODE_OPTIONS=--max-old-space-size=8000
|
||||
API_KEY="sk-xxx"
|
||||
BASE_URL="https://api.siliconflow.cn/v1/"
|
||||
MODEL="Qwen/Qwen3-235B-A22B-Instruct-2507"
|
||||
CSLOGGER_MAIN_LEVEL=info
|
||||
CSLOGGER_RENDERER_LEVEL=info
|
||||
#CSLOGGER_MAIN_SHOW_MODULES=
|
||||
#CSLOGGER_RENDERER_SHOW_MODULES=
|
||||
4
.github/ISSUE_TEMPLATE/#0_bug_report.yml
vendored
4
.github/ISSUE_TEMPLATE/#0_bug_report.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: 🐛 错误报告 (中文)
|
||||
description: 创建一个报告以帮助我们改进
|
||||
title: '[错误]: '
|
||||
labels: ['kind/bug']
|
||||
labels: ['BUG']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
@ -24,6 +24,8 @@ body:
|
||||
required: true
|
||||
- label: 我填写了简短且清晰明确的标题,以便开发者在翻阅 Issue 列表时能快速确定大致问题。而不是“一个建议”、“卡住了”等。
|
||||
required: true
|
||||
- label: 我确认我正在使用最新版本的 Cherry Studio。
|
||||
required: true
|
||||
|
||||
- type: dropdown
|
||||
id: platform
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
name: 💡 功能建议 (中文)
|
||||
description: 为项目提出新的想法
|
||||
title: '[功能]: '
|
||||
labels: ['kind/enhancement']
|
||||
labels: ['feature']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
|
||||
2
.github/ISSUE_TEMPLATE/#2_question.yml
vendored
2
.github/ISSUE_TEMPLATE/#2_question.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: ❓ 提问 & 讨论 (中文)
|
||||
description: 寻求帮助、讨论问题、提出疑问等...
|
||||
title: '[讨论]: '
|
||||
labels: ['kind/question']
|
||||
labels: ['discussion', 'help wanted']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
|
||||
4
.github/ISSUE_TEMPLATE/0_bug_report.yml
vendored
4
.github/ISSUE_TEMPLATE/0_bug_report.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: 🐛 Bug Report (English)
|
||||
description: Create a report to help us improve
|
||||
title: '[Bug]: '
|
||||
labels: ['kind/bug']
|
||||
labels: ['BUG']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
@ -24,6 +24,8 @@ body:
|
||||
required: true
|
||||
- label: I've filled in short, clear headings so that developers can quickly identify a rough idea of what to expect when flipping through the list of issues. And not "a suggestion", "stuck", etc.
|
||||
required: true
|
||||
- label: I've confirmed that I am using the latest version of Cherry Studio.
|
||||
required: true
|
||||
|
||||
- type: dropdown
|
||||
id: platform
|
||||
|
||||
2
.github/ISSUE_TEMPLATE/1_feature_request.yml
vendored
2
.github/ISSUE_TEMPLATE/1_feature_request.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: 💡 Feature Request (English)
|
||||
description: Suggest an idea for this project
|
||||
title: '[Feature]: '
|
||||
labels: ['kind/enhancement']
|
||||
labels: ['feature']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
|
||||
2
.github/ISSUE_TEMPLATE/2_question.yml
vendored
2
.github/ISSUE_TEMPLATE/2_question.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: ❓ Questions & Discussion
|
||||
description: Seeking help, discussing issues, asking questions, etc...
|
||||
title: '[Discussion]: '
|
||||
labels: ['kind/question']
|
||||
labels: ['discussion', 'help wanted']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
|
||||
23
.github/workflows/nightly-build.yml
vendored
23
.github/workflows/nightly-build.yml
vendored
@ -51,7 +51,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Check out Git repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
ref: main
|
||||
|
||||
@ -93,17 +93,19 @@ jobs:
|
||||
- name: Build Linux
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
run: |
|
||||
yarn build:npm linux
|
||||
sudo apt-get install -y rpm
|
||||
yarn build:linux
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
|
||||
NODE_OPTIONS: --max-old-space-size=8192
|
||||
MAIN_VITE_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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'
|
||||
run: |
|
||||
yarn build:npm mac
|
||||
yarn build:mac
|
||||
env:
|
||||
CSC_LINK: ${{ secrets.CSC_LINK }}
|
||||
@ -111,19 +113,24 @@ 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_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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'
|
||||
run: |
|
||||
yarn build:npm windows
|
||||
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_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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: Rename artifacts with nightly format
|
||||
shell: bash
|
||||
@ -219,7 +226,7 @@ jobs:
|
||||
shell: bash
|
||||
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v5
|
||||
with:
|
||||
path: all-artifacts
|
||||
merge-multiple: false
|
||||
|
||||
19
.github/workflows/pr-ci.yml
vendored
19
.github/workflows/pr-ci.yml
vendored
@ -1,5 +1,8 @@
|
||||
name: Pull Request CI
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
@ -10,10 +13,12 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
PRCI: true
|
||||
|
||||
steps:
|
||||
- name: Check out Git repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v4
|
||||
@ -40,8 +45,14 @@ jobs:
|
||||
- name: Install Dependencies
|
||||
run: yarn install
|
||||
|
||||
- name: Build Check
|
||||
run: yarn build:check
|
||||
|
||||
- name: Lint Check
|
||||
run: yarn test:lint
|
||||
|
||||
- name: Type Check
|
||||
run: yarn typecheck
|
||||
|
||||
- name: i18n Check
|
||||
run: yarn check:i18n
|
||||
|
||||
- name: Test
|
||||
run: yarn test
|
||||
|
||||
18
.github/workflows/release.yml
vendored
18
.github/workflows/release.yml
vendored
@ -25,7 +25,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Check out Git repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@ -39,6 +39,13 @@ jobs:
|
||||
echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Set package.json version
|
||||
shell: bash
|
||||
run: |
|
||||
TAG="${{ steps.get-tag.outputs.tag }}"
|
||||
VERSION="${TAG#v}"
|
||||
npm version "$VERSION" --no-git-tag-version --allow-same-version
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
@ -72,12 +79,13 @@ jobs:
|
||||
- name: Build Linux
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
run: |
|
||||
yarn build:npm linux
|
||||
sudo apt-get install -y rpm
|
||||
yarn build:linux
|
||||
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NODE_OPTIONS: --max-old-space-size=8192
|
||||
MAIN_VITE_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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 }}
|
||||
@ -86,7 +94,6 @@ jobs:
|
||||
if: matrix.os == 'macos-latest'
|
||||
run: |
|
||||
sudo -H pip install setuptools
|
||||
yarn build:npm mac
|
||||
yarn build:mac
|
||||
env:
|
||||
CSC_LINK: ${{ secrets.CSC_LINK }}
|
||||
@ -96,6 +103,7 @@ jobs:
|
||||
APPLE_TEAM_ID: ${{ vars.APPLE_TEAM_ID }}
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NODE_OPTIONS: --max-old-space-size=8192
|
||||
MAIN_VITE_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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 }}
|
||||
@ -103,11 +111,11 @@ jobs:
|
||||
- name: Build Windows
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: |
|
||||
yarn build:npm windows
|
||||
yarn build:win
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NODE_OPTIONS: --max-old-space-size=8192
|
||||
MAIN_VITE_CHERRYIN_CLIENT_SECRET: ${{ secrets.MAIN_VITE_CHERRYIN_CLIENT_SECRET }}
|
||||
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 }}
|
||||
@ -119,5 +127,5 @@ jobs:
|
||||
allowUpdates: true
|
||||
makeLatest: false
|
||||
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'
|
||||
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/beta*.yml,dist/*.blockmap'
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
8
.gitignore
vendored
8
.gitignore
vendored
@ -35,11 +35,13 @@ Thumbs.db
|
||||
node_modules
|
||||
dist
|
||||
out
|
||||
mcp_server
|
||||
stats.html
|
||||
|
||||
# ENV
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
|
||||
# Local
|
||||
local
|
||||
@ -48,13 +50,19 @@ local
|
||||
.cursor/*
|
||||
.claude/*
|
||||
.gemini/*
|
||||
.qwen/*
|
||||
.trae/*
|
||||
.claude-code-router/*
|
||||
CLAUDE.local.md
|
||||
|
||||
# vitest
|
||||
coverage
|
||||
.vitest-cache
|
||||
vitest.config.*.timestamp-*
|
||||
|
||||
# TypeScript incremental build
|
||||
.tsbuildinfo
|
||||
|
||||
# playwright
|
||||
playwright-report
|
||||
test-results
|
||||
|
||||
@ -7,3 +7,4 @@ tsconfig.*.json
|
||||
CHANGELOG*.md
|
||||
agents.json
|
||||
src/renderer/src/integration/nutstore/sso/lib
|
||||
src/main/integration/cherryin/index.js
|
||||
|
||||
13
.prettierrc
13
.prettierrc
@ -1,8 +1,11 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"semi": false,
|
||||
"printWidth": 120,
|
||||
"trailingComma": "none",
|
||||
"bracketSameLine": true,
|
||||
"endOfLine": "lf",
|
||||
"bracketSameLine": true
|
||||
"jsonRecursiveSort": true,
|
||||
"jsonSortOrder": "{\"*\": \"lexical\"}",
|
||||
"plugins": ["prettier-plugin-sort-json"],
|
||||
"printWidth": 120,
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "none"
|
||||
}
|
||||
|
||||
7
.vscode/extensions.json
vendored
7
.vscode/extensions.json
vendored
@ -1,3 +1,8 @@
|
||||
{
|
||||
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "editorconfig.editorconfig"]
|
||||
"recommendations": [
|
||||
"dbaeumer.vscode-eslint",
|
||||
"esbenp.prettier-vscode",
|
||||
"editorconfig.editorconfig",
|
||||
"lokalise.i18n-ally"
|
||||
]
|
||||
}
|
||||
|
||||
47
.vscode/launch.json
vendored
47
.vscode/launch.json
vendored
@ -1,39 +1,40 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"compounds": [
|
||||
{
|
||||
"configurations": ["Debug Main Process", "Debug Renderer Process"],
|
||||
"name": "Debug All",
|
||||
"presentation": {
|
||||
"order": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Debug Main Process",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"cwd": "${workspaceRoot}",
|
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite",
|
||||
"windows": {
|
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd"
|
||||
},
|
||||
"runtimeArgs": ["--sourcemap"],
|
||||
"env": {
|
||||
"REMOTE_DEBUGGING_PORT": "9222"
|
||||
},
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"name": "Debug Main Process",
|
||||
"request": "launch",
|
||||
"runtimeArgs": ["--inspect", "--sourcemap"],
|
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite",
|
||||
"type": "node",
|
||||
"windows": {
|
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "Debug Renderer Process",
|
||||
"port": 9222,
|
||||
"request": "attach",
|
||||
"type": "chrome",
|
||||
"webRoot": "${workspaceFolder}/src/renderer",
|
||||
"timeout": 60000,
|
||||
"presentation": {
|
||||
"hidden": true
|
||||
}
|
||||
},
|
||||
"request": "attach",
|
||||
"timeout": 3000000,
|
||||
"type": "chrome",
|
||||
"webRoot": "${workspaceFolder}/src/renderer"
|
||||
}
|
||||
],
|
||||
"compounds": [
|
||||
{
|
||||
"name": "Debug All",
|
||||
"configurations": ["Debug Main Process", "Debug Renderer Process"],
|
||||
"presentation": {
|
||||
"order": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
"version": "0.2.0"
|
||||
}
|
||||
|
||||
55
.vscode/settings.json
vendored
55
.vscode/settings.json
vendored
@ -1,45 +1,46 @@
|
||||
{
|
||||
"editor.formatOnSave": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": "explicit",
|
||||
"source.organizeImports": "never"
|
||||
},
|
||||
"files.eol": "\n",
|
||||
"search.exclude": {
|
||||
"**/dist/**": true,
|
||||
".yarn/releases/**": true
|
||||
"[css]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[javascript]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[typescript]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[typescriptreact]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[json]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[jsonc]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[css]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
"[markdown]": {
|
||||
"files.trimTrailingWhitespace": false
|
||||
},
|
||||
"[scss]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"[markdown]": {
|
||||
"files.trimTrailingWhitespace": false
|
||||
"[typescript]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"i18n-ally.localesPaths": ["src/renderer/src/i18n/locales"],
|
||||
"i18n-ally.enabledFrameworks": ["react-i18next", "i18next"],
|
||||
"i18n-ally.keystyle": "nested", // 翻译路径格式
|
||||
"i18n-ally.sortKeys": true, // 排序
|
||||
"i18n-ally.namespace": true, // 开启命名空间
|
||||
"i18n-ally.enabledParsers": ["ts", "js", "json"], // 解析语言
|
||||
"i18n-ally.sourceLanguage": "en-us", // 翻译源语言
|
||||
"[typescriptreact]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": "explicit",
|
||||
"source.organizeImports": "never"
|
||||
},
|
||||
"editor.formatOnSave": true,
|
||||
"files.eol": "\n",
|
||||
"i18n-ally.displayLanguage": "zh-cn",
|
||||
"i18n-ally.fullReloadOnChanged": true // 界面显示语言
|
||||
"i18n-ally.enabledFrameworks": ["react-i18next", "i18next"],
|
||||
"i18n-ally.enabledParsers": ["ts", "js", "json"], // 解析语言
|
||||
"i18n-ally.fullReloadOnChanged": true, // 界面显示语言
|
||||
"i18n-ally.keystyle": "nested", // 翻译路径格式
|
||||
"i18n-ally.localesPaths": ["src/renderer/src/i18n/locales"],
|
||||
// "i18n-ally.namespace": true, // 开启命名空间
|
||||
"i18n-ally.sortKeys": true, // 排序
|
||||
"i18n-ally.sourceLanguage": "zh-cn", // 翻译源语言
|
||||
"i18n-ally.usage.derivedKeyRules": ["{key}_one", "{key}_other"], // 标记单复数形式的键为已翻译
|
||||
"search.exclude": {
|
||||
"**/dist/**": true,
|
||||
".yarn/releases/**": true
|
||||
}
|
||||
}
|
||||
|
||||
196
.yarn/patches/@anthropic-ai-vertex-sdk-npm-0.11.4-c19cb41edb.patch
vendored
Normal file
196
.yarn/patches/@anthropic-ai-vertex-sdk-npm-0.11.4-c19cb41edb.patch
vendored
Normal file
@ -0,0 +1,196 @@
|
||||
diff --git a/client.js b/client.js
|
||||
index c2b9cd6e46f9f66f901af259661bc2d2f8b38936..9b6b3af1a6573e1ccaf3a1c5f41b48df198cbbe0 100644
|
||||
--- a/client.js
|
||||
+++ b/client.js
|
||||
@@ -26,7 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AnthropicVertex = exports.BaseAnthropic = void 0;
|
||||
const client_1 = require("@anthropic-ai/sdk/client");
|
||||
const Resources = __importStar(require("@anthropic-ai/sdk/resources/index"));
|
||||
-const google_auth_library_1 = require("google-auth-library");
|
||||
+// const google_auth_library_1 = require("google-auth-library");
|
||||
const env_1 = require("./internal/utils/env.js");
|
||||
const values_1 = require("./internal/utils/values.js");
|
||||
const headers_1 = require("./internal/headers.js");
|
||||
@@ -56,7 +56,7 @@ class AnthropicVertex extends client_1.BaseAnthropic {
|
||||
throw new Error('No region was given. The client should be instantiated with the `region` option or the `CLOUD_ML_REGION` environment variable should be set.');
|
||||
}
|
||||
super({
|
||||
- baseURL: baseURL || `https://${region}-aiplatform.googleapis.com/v1`,
|
||||
+ baseURL: baseURL || (region === 'global' ? 'https://aiplatform.googleapis.com/v1' : `https://${region}-aiplatform.googleapis.com/v1`),
|
||||
...opts,
|
||||
});
|
||||
this.messages = makeMessagesResource(this);
|
||||
@@ -64,22 +64,22 @@ class AnthropicVertex extends client_1.BaseAnthropic {
|
||||
this.region = region;
|
||||
this.projectId = projectId;
|
||||
this.accessToken = opts.accessToken ?? null;
|
||||
- this._auth =
|
||||
- opts.googleAuth ?? new google_auth_library_1.GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
- this._authClientPromise = this._auth.getClient();
|
||||
+ // this._auth =
|
||||
+ // opts.googleAuth ?? new google_auth_library_1.GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
+ // this._authClientPromise = this._auth.getClient();
|
||||
}
|
||||
validateHeaders() {
|
||||
// auth validation is handled in prepareOptions since it needs to be async
|
||||
}
|
||||
- async prepareOptions(options) {
|
||||
- const authClient = await this._authClientPromise;
|
||||
- const authHeaders = await authClient.getRequestHeaders();
|
||||
- const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
- if (!this.projectId && projectId) {
|
||||
- this.projectId = projectId;
|
||||
- }
|
||||
- options.headers = (0, headers_1.buildHeaders)([authHeaders, options.headers]);
|
||||
- }
|
||||
+ // async prepareOptions(options) {
|
||||
+ // const authClient = await this._authClientPromise;
|
||||
+ // const authHeaders = await authClient.getRequestHeaders();
|
||||
+ // const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
+ // if (!this.projectId && projectId) {
|
||||
+ // this.projectId = projectId;
|
||||
+ // }
|
||||
+ // options.headers = (0, headers_1.buildHeaders)([authHeaders, options.headers]);
|
||||
+ // }
|
||||
buildRequest(options) {
|
||||
if ((0, values_1.isObj)(options.body)) {
|
||||
// create a shallow copy of the request body so that code that mutates it later
|
||||
diff --git a/client.mjs b/client.mjs
|
||||
index 70274cbf38f69f87cbcca9567e77e4a7b938cf90..4dea954b6f4afad565663426b7adfad5de973a7d 100644
|
||||
--- a/client.mjs
|
||||
+++ b/client.mjs
|
||||
@@ -1,6 +1,6 @@
|
||||
import { BaseAnthropic } from '@anthropic-ai/sdk/client';
|
||||
import * as Resources from '@anthropic-ai/sdk/resources/index';
|
||||
-import { GoogleAuth } from 'google-auth-library';
|
||||
+// import { GoogleAuth } from 'google-auth-library';
|
||||
import { readEnv } from "./internal/utils/env.mjs";
|
||||
import { isObj } from "./internal/utils/values.mjs";
|
||||
import { buildHeaders } from "./internal/headers.mjs";
|
||||
@@ -29,7 +29,7 @@ export class AnthropicVertex extends BaseAnthropic {
|
||||
throw new Error('No region was given. The client should be instantiated with the `region` option or the `CLOUD_ML_REGION` environment variable should be set.');
|
||||
}
|
||||
super({
|
||||
- baseURL: baseURL || `https://${region}-aiplatform.googleapis.com/v1`,
|
||||
+ baseURL: baseURL || (region === 'global' ? 'https://aiplatform.googleapis.com/v1' : `https://${region}-aiplatform.googleapis.com/v1`),
|
||||
...opts,
|
||||
});
|
||||
this.messages = makeMessagesResource(this);
|
||||
@@ -37,22 +37,22 @@ export class AnthropicVertex extends BaseAnthropic {
|
||||
this.region = region;
|
||||
this.projectId = projectId;
|
||||
this.accessToken = opts.accessToken ?? null;
|
||||
- this._auth =
|
||||
- opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
- this._authClientPromise = this._auth.getClient();
|
||||
+ // this._auth =
|
||||
+ // opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
+ //this._authClientPromise = this._auth.getClient();
|
||||
}
|
||||
validateHeaders() {
|
||||
// auth validation is handled in prepareOptions since it needs to be async
|
||||
}
|
||||
- async prepareOptions(options) {
|
||||
- const authClient = await this._authClientPromise;
|
||||
- const authHeaders = await authClient.getRequestHeaders();
|
||||
- const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
- if (!this.projectId && projectId) {
|
||||
- this.projectId = projectId;
|
||||
- }
|
||||
- options.headers = buildHeaders([authHeaders, options.headers]);
|
||||
- }
|
||||
+ // async prepareOptions(options) {
|
||||
+ // const authClient = await this._authClientPromise;
|
||||
+ // const authHeaders = await authClient.getRequestHeaders();
|
||||
+ // const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
+ // if (!this.projectId && projectId) {
|
||||
+ // this.projectId = projectId;
|
||||
+ // }
|
||||
+ // options.headers = buildHeaders([authHeaders, options.headers]);
|
||||
+ // }
|
||||
buildRequest(options) {
|
||||
if (isObj(options.body)) {
|
||||
// create a shallow copy of the request body so that code that mutates it later
|
||||
diff --git a/src/client.ts b/src/client.ts
|
||||
index a6f9c6be65e4189f4f9601fb560df3f68e7563eb..37b1ad2802e3ca0dae4ca35f9dcb5b22dcf09796 100644
|
||||
--- a/src/client.ts
|
||||
+++ b/src/client.ts
|
||||
@@ -12,22 +12,22 @@ export { BaseAnthropic } from '@anthropic-ai/sdk/client';
|
||||
const DEFAULT_VERSION = 'vertex-2023-10-16';
|
||||
const MODEL_ENDPOINTS = new Set<string>(['/v1/messages', '/v1/messages?beta=true']);
|
||||
|
||||
-export type ClientOptions = Omit<CoreClientOptions, 'apiKey' | 'authToken'> & {
|
||||
- region?: string | null | undefined;
|
||||
- projectId?: string | null | undefined;
|
||||
- accessToken?: string | null | undefined;
|
||||
-
|
||||
- /**
|
||||
- * Override the default google auth config using the
|
||||
- * [google-auth-library](https://www.npmjs.com/package/google-auth-library) package.
|
||||
- *
|
||||
- * Note that you'll likely have to set `scopes`, e.g.
|
||||
- * ```ts
|
||||
- * new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' })
|
||||
- * ```
|
||||
- */
|
||||
- googleAuth?: GoogleAuth | null | undefined;
|
||||
-};
|
||||
+// export type ClientOptions = Omit<CoreClientOptions, 'apiKey' | 'authToken'> & {
|
||||
+// region?: string | null | undefined;
|
||||
+// projectId?: string | null | undefined;
|
||||
+// accessToken?: string | null | undefined;
|
||||
+
|
||||
+// /**
|
||||
+// * Override the default google auth config using the
|
||||
+// * [google-auth-library](https://www.npmjs.com/package/google-auth-library) package.
|
||||
+// *
|
||||
+// * Note that you'll likely have to set `scopes`, e.g.
|
||||
+// * ```ts
|
||||
+// * new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' })
|
||||
+// * ```
|
||||
+// */
|
||||
+// googleAuth?: GoogleAuth | null | undefined;
|
||||
+// };
|
||||
|
||||
export class AnthropicVertex extends BaseAnthropic {
|
||||
region: string;
|
||||
@@ -74,9 +74,9 @@ export class AnthropicVertex extends BaseAnthropic {
|
||||
this.projectId = projectId;
|
||||
this.accessToken = opts.accessToken ?? null;
|
||||
|
||||
- this._auth =
|
||||
- opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
- this._authClientPromise = this._auth.getClient();
|
||||
+ // this._auth =
|
||||
+ // opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
|
||||
+ // this._authClientPromise = this._auth.getClient();
|
||||
}
|
||||
|
||||
messages: MessagesResource = makeMessagesResource(this);
|
||||
@@ -86,17 +86,17 @@ export class AnthropicVertex extends BaseAnthropic {
|
||||
// auth validation is handled in prepareOptions since it needs to be async
|
||||
}
|
||||
|
||||
- protected override async prepareOptions(options: FinalRequestOptions): Promise<void> {
|
||||
- const authClient = await this._authClientPromise;
|
||||
+ // protected override async prepareOptions(options: FinalRequestOptions): Promise<void> {
|
||||
+ // const authClient = await this._authClientPromise;
|
||||
|
||||
- const authHeaders = await authClient.getRequestHeaders();
|
||||
- const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
- if (!this.projectId && projectId) {
|
||||
- this.projectId = projectId;
|
||||
- }
|
||||
+ // const authHeaders = await authClient.getRequestHeaders();
|
||||
+ // const projectId = authClient.projectId ?? authHeaders['x-goog-user-project'];
|
||||
+ // if (!this.projectId && projectId) {
|
||||
+ // this.projectId = projectId;
|
||||
+ // }
|
||||
|
||||
- options.headers = buildHeaders([authHeaders, options.headers]);
|
||||
- }
|
||||
+ // options.headers = buildHeaders([authHeaders, options.headers]);
|
||||
+ // }
|
||||
|
||||
override buildRequest(options: FinalRequestOptions): {
|
||||
req: FinalizedRequestInit;
|
||||
30
.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch
vendored
Normal file
30
.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
diff --git a/index.js b/index.js
|
||||
index dc071739e79876dff88e1be06a9168e294222d13..b9df7525c62bdf777e89e732e1b0c81f84d872f2 100644
|
||||
--- a/index.js
|
||||
+++ b/index.js
|
||||
@@ -380,7 +380,7 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
|
||||
}
|
||||
}
|
||||
|
||||
-if (!nativeBinding) {
|
||||
+if (!nativeBinding && process.platform !== 'linux') {
|
||||
if (loadErrors.length > 0) {
|
||||
throw new Error(
|
||||
`Cannot find native binding. ` +
|
||||
@@ -392,6 +392,13 @@ if (!nativeBinding) {
|
||||
throw new Error(`Failed to load native binding`)
|
||||
}
|
||||
|
||||
-module.exports = nativeBinding
|
||||
-module.exports.OcrAccuracy = nativeBinding.OcrAccuracy
|
||||
-module.exports.recognize = nativeBinding.recognize
|
||||
+if (process.platform === 'linux') {
|
||||
+ module.exports = {OcrAccuracy: {
|
||||
+ Fast: 0,
|
||||
+ Accurate: 1
|
||||
+ }, recognize: () => Promise.resolve({text: '', confidence: 1.0})}
|
||||
+}else{
|
||||
+ module.exports = nativeBinding
|
||||
+ module.exports.OcrAccuracy = nativeBinding.OcrAccuracy
|
||||
+ module.exports.recognize = nativeBinding.recognize
|
||||
+}
|
||||
48
.yarn/patches/@tiptap-extension-drag-handle-npm-3.2.0-5a9ebff7c9.patch
vendored
Normal file
48
.yarn/patches/@tiptap-extension-drag-handle-npm-3.2.0-5a9ebff7c9.patch
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
diff --git a/dist/index.cjs b/dist/index.cjs
|
||||
index 8e560a4406c5cc616c11bb9fd5455ac0dcf47fa3..c7cd0d65ddc971bff71e89f610de82cfdaa5a8c7 100644
|
||||
--- a/dist/index.cjs
|
||||
+++ b/dist/index.cjs
|
||||
@@ -413,6 +413,19 @@ var DragHandlePlugin = ({
|
||||
}
|
||||
return false;
|
||||
},
|
||||
+ scroll(view) {
|
||||
+ if (!element || locked) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ if (view.hasFocus()) {
|
||||
+ hideHandle();
|
||||
+ currentNode = null;
|
||||
+ currentNodePos = -1;
|
||||
+ onNodeChange == null ? void 0 : onNodeChange({ editor, node: null, pos: -1 });
|
||||
+ return false;
|
||||
+ }
|
||||
+ return false;
|
||||
+ },
|
||||
mouseleave(_view, e) {
|
||||
if (locked) {
|
||||
return false;
|
||||
diff --git a/dist/index.js b/dist/index.js
|
||||
index 39e4c3ef9986cd25544d9d3994cf6a9ada74b145..378d9130abbfdd0e1e4f743b5b537743c9ab07d0 100644
|
||||
--- a/dist/index.js
|
||||
+++ b/dist/index.js
|
||||
@@ -387,6 +387,19 @@ var DragHandlePlugin = ({
|
||||
}
|
||||
return false;
|
||||
},
|
||||
+ scroll(view) {
|
||||
+ if (!element || locked) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ if (view.hasFocus()) {
|
||||
+ hideHandle();
|
||||
+ currentNode = null;
|
||||
+ currentNodePos = -1;
|
||||
+ onNodeChange == null ? void 0 : onNodeChange({ editor, node: null, pos: -1 });
|
||||
+ return false;
|
||||
+ }
|
||||
+ return false;
|
||||
+ },
|
||||
mouseleave(_view, e) {
|
||||
if (locked) {
|
||||
return false;
|
||||
@ -1,5 +1,5 @@
|
||||
diff --git a/es/dropdown/dropdown.js b/es/dropdown/dropdown.js
|
||||
index 986877a762b9ad0aca596a8552732cd12d2eaabb..1f18aa2ea745e68950e4cee16d4d655f5c835fd5 100644
|
||||
index 2e45574398ff68450022a0078e213cc81fe7454e..58ba7789939b7805a89f92b93d222f8fb1168bdf 100644
|
||||
--- a/es/dropdown/dropdown.js
|
||||
+++ b/es/dropdown/dropdown.js
|
||||
@@ -2,7 +2,7 @@
|
||||
@ -11,7 +11,7 @@ index 986877a762b9ad0aca596a8552732cd12d2eaabb..1f18aa2ea745e68950e4cee16d4d655f
|
||||
import classNames from 'classnames';
|
||||
import RcDropdown from 'rc-dropdown';
|
||||
import useEvent from "rc-util/es/hooks/useEvent";
|
||||
@@ -158,8 +158,10 @@ const Dropdown = props => {
|
||||
@@ -160,8 +160,10 @@ const Dropdown = props => {
|
||||
className: `${prefixCls}-menu-submenu-arrow`
|
||||
}, direction === 'rtl' ? (/*#__PURE__*/React.createElement(LeftOutlined, {
|
||||
className: `${prefixCls}-menu-submenu-arrow-icon`
|
||||
@ -24,22 +24,8 @@ index 986877a762b9ad0aca596a8552732cd12d2eaabb..1f18aa2ea745e68950e4cee16d4d655f
|
||||
}))),
|
||||
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
|
||||
index 572aaaa0899f429cbf8a7181f2eeada545f76dcb..4e175c8d7713dd6422f8bcdc74ee671a835de6ce 100644
|
||||
--- a/es/select/useIcons.js
|
||||
+++ b/es/select/useIcons.js
|
||||
@@ -4,10 +4,10 @@ import * as React from 'react';
|
||||
@ -51,10 +37,10 @@ index 959115be936ef8901548af2658c5dcfdc5852723..c812edd52123eb0faf4638b1154fcfa1
|
||||
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) {
|
||||
export default function useIcons({
|
||||
suffixIcon,
|
||||
clearIcon,
|
||||
@@ -54,8 +54,10 @@ export default function useIcons({
|
||||
className: iconCls
|
||||
}));
|
||||
}
|
||||
12
.yarn/patches/atomically-npm-1.7.0-e742e5293b.patch
vendored
Normal file
12
.yarn/patches/atomically-npm-1.7.0-e742e5293b.patch
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
diff --git a/dist/utils/temp.js b/dist/utils/temp.js
|
||||
index c0844f640f7927ff87edda13f7c853d10ebb8dd0..3ca3d29e0f4ee700c43ebde47002883955b664b3 100644
|
||||
--- a/dist/utils/temp.js
|
||||
+++ b/dist/utils/temp.js
|
||||
@@ -2,6 +2,7 @@
|
||||
/* IMPORT */
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const path = require("path");
|
||||
+const process = require("process");
|
||||
const consts_1 = require("../consts");
|
||||
const fs_1 = require("./fs");
|
||||
/* TEMP */
|
||||
13
.yarn/patches/file-stream-rotator-npm-0.6.1-eab45fb13d.patch
vendored
Normal file
13
.yarn/patches/file-stream-rotator-npm-0.6.1-eab45fb13d.patch
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
diff --git a/FileStreamRotator.js b/FileStreamRotator.js
|
||||
index 639bb9c8f972ba672bd27d9f8b1739d1030cb44b..a12a6d93b61fe782e981027248fa10876151f65f 100644
|
||||
--- a/FileStreamRotator.js
|
||||
+++ b/FileStreamRotator.js
|
||||
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
-var moment = require('moment');
|
||||
+var moment = require('moment').default || require('moment');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var EventEmitter = require('events');
|
||||
279
.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch
vendored
279
.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch
vendored
@ -1,279 +0,0 @@
|
||||
diff --git a/client.js b/client.js
|
||||
index 33b4ff6309d5f29187dab4e285d07dac20340bab..8f568637ee9e4677585931fb0284c8165a933f69 100644
|
||||
--- a/client.js
|
||||
+++ b/client.js
|
||||
@@ -433,7 +433,7 @@ class OpenAI {
|
||||
'User-Agent': this.getUserAgent(),
|
||||
'X-Stainless-Retry-Count': String(retryCount),
|
||||
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
|
||||
- ...(0, detect_platform_1.getPlatformHeaders)(),
|
||||
+ // ...(0, detect_platform_1.getPlatformHeaders)(),
|
||||
'OpenAI-Organization': this.organization,
|
||||
'OpenAI-Project': this.project,
|
||||
},
|
||||
diff --git a/client.mjs b/client.mjs
|
||||
index c34c18213073540ebb296ea540b1d1ad39527906..1ce1a98256d7e90e26ca963582f235b23e996e73 100644
|
||||
--- a/client.mjs
|
||||
+++ b/client.mjs
|
||||
@@ -430,7 +430,7 @@ export class OpenAI {
|
||||
'User-Agent': this.getUserAgent(),
|
||||
'X-Stainless-Retry-Count': String(retryCount),
|
||||
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
|
||||
- ...getPlatformHeaders(),
|
||||
+ // ...getPlatformHeaders(),
|
||||
'OpenAI-Organization': this.organization,
|
||||
'OpenAI-Project': this.project,
|
||||
},
|
||||
diff --git a/core/error.js b/core/error.js
|
||||
index a12d9d9ccd242050161adeb0f82e1b98d9e78e20..fe3a5462480558bc426deea147f864f12b36f9bd 100644
|
||||
--- a/core/error.js
|
||||
+++ b/core/error.js
|
||||
@@ -40,7 +40,7 @@ class APIError extends OpenAIError {
|
||||
if (!status || !headers) {
|
||||
return new APIConnectionError({ message, cause: (0, errors_1.castToError)(errorResponse) });
|
||||
}
|
||||
- const error = errorResponse?.['error'];
|
||||
+ const error = errorResponse?.['error'] || errorResponse;
|
||||
if (status === 400) {
|
||||
return new BadRequestError(status, error, message, headers);
|
||||
}
|
||||
diff --git a/core/error.mjs b/core/error.mjs
|
||||
index 83cefbaffeb8c657536347322d8de9516af479a2..63334b7972ec04882aa4a0800c1ead5982345045 100644
|
||||
--- a/core/error.mjs
|
||||
+++ b/core/error.mjs
|
||||
@@ -36,7 +36,7 @@ export class APIError extends OpenAIError {
|
||||
if (!status || !headers) {
|
||||
return new APIConnectionError({ message, cause: castToError(errorResponse) });
|
||||
}
|
||||
- const error = errorResponse?.['error'];
|
||||
+ const error = errorResponse?.['error'] || errorResponse;
|
||||
if (status === 400) {
|
||||
return new BadRequestError(status, error, message, headers);
|
||||
}
|
||||
diff --git a/resources/embeddings.js b/resources/embeddings.js
|
||||
index 2404264d4ba0204322548945ebb7eab3bea82173..8f1bc45cc45e0797d50989d96b51147b90ae6790 100644
|
||||
--- a/resources/embeddings.js
|
||||
+++ b/resources/embeddings.js
|
||||
@@ -5,52 +5,64 @@ exports.Embeddings = void 0;
|
||||
const resource_1 = require("../core/resource.js");
|
||||
const utils_1 = require("../internal/utils.js");
|
||||
class Embeddings extends resource_1.APIResource {
|
||||
- /**
|
||||
- * Creates an embedding vector representing the input text.
|
||||
- *
|
||||
- * @example
|
||||
- * ```ts
|
||||
- * const createEmbeddingResponse =
|
||||
- * await client.embeddings.create({
|
||||
- * input: 'The quick brown fox jumped over the lazy dog',
|
||||
- * model: 'text-embedding-3-small',
|
||||
- * });
|
||||
- * ```
|
||||
- */
|
||||
- create(body, options) {
|
||||
- const hasUserProvidedEncodingFormat = !!body.encoding_format;
|
||||
- // No encoding_format specified, defaulting to base64 for performance reasons
|
||||
- // See https://github.com/openai/openai-node/pull/1312
|
||||
- let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';
|
||||
- if (hasUserProvidedEncodingFormat) {
|
||||
- (0, utils_1.loggerFor)(this._client).debug('embeddings/user defined encoding_format:', body.encoding_format);
|
||||
- }
|
||||
- const response = this._client.post('/embeddings', {
|
||||
- body: {
|
||||
- ...body,
|
||||
- encoding_format: encoding_format,
|
||||
- },
|
||||
- ...options,
|
||||
- });
|
||||
- // if the user specified an encoding_format, return the response as-is
|
||||
- if (hasUserProvidedEncodingFormat) {
|
||||
- return response;
|
||||
- }
|
||||
- // in this stage, we are sure the user did not specify an encoding_format
|
||||
- // and we defaulted to base64 for performance reasons
|
||||
- // we are sure then that the response is base64 encoded, let's decode it
|
||||
- // the returned result will be a float32 array since this is OpenAI API's default encoding
|
||||
- (0, utils_1.loggerFor)(this._client).debug('embeddings/decoding base64 embeddings from base64');
|
||||
- return response._thenUnwrap((response) => {
|
||||
- if (response && response.data) {
|
||||
- response.data.forEach((embeddingBase64Obj) => {
|
||||
- const embeddingBase64Str = embeddingBase64Obj.embedding;
|
||||
- embeddingBase64Obj.embedding = (0, utils_1.toFloat32Array)(embeddingBase64Str);
|
||||
- });
|
||||
- }
|
||||
- return response;
|
||||
- });
|
||||
- }
|
||||
+ /**
|
||||
+ * Creates an embedding vector representing the input text.
|
||||
+ *
|
||||
+ * @example
|
||||
+ * ```ts
|
||||
+ * const createEmbeddingResponse =
|
||||
+ * await client.embeddings.create({
|
||||
+ * input: 'The quick brown fox jumped over the lazy dog',
|
||||
+ * model: 'text-embedding-3-small',
|
||||
+ * });
|
||||
+ * ```
|
||||
+ */
|
||||
+ create(body, options) {
|
||||
+ const hasUserProvidedEncodingFormat = !!body.encoding_format;
|
||||
+ // No encoding_format specified, defaulting to base64 for performance reasons
|
||||
+ // See https://github.com/openai/openai-node/pull/1312
|
||||
+ let encoding_format = hasUserProvidedEncodingFormat
|
||||
+ ? body.encoding_format
|
||||
+ : "base64";
|
||||
+ if (body.model.includes("jina")) {
|
||||
+ encoding_format = undefined;
|
||||
+ }
|
||||
+ if (hasUserProvidedEncodingFormat) {
|
||||
+ (0, utils_1.loggerFor)(this._client).debug(
|
||||
+ "embeddings/user defined encoding_format:",
|
||||
+ body.encoding_format
|
||||
+ );
|
||||
+ }
|
||||
+ const response = this._client.post("/embeddings", {
|
||||
+ body: {
|
||||
+ ...body,
|
||||
+ encoding_format: encoding_format,
|
||||
+ },
|
||||
+ ...options,
|
||||
+ });
|
||||
+ // if the user specified an encoding_format, return the response as-is
|
||||
+ if (hasUserProvidedEncodingFormat || body.model.includes("jina")) {
|
||||
+ return response;
|
||||
+ }
|
||||
+ // in this stage, we are sure the user did not specify an encoding_format
|
||||
+ // and we defaulted to base64 for performance reasons
|
||||
+ // we are sure then that the response is base64 encoded, let's decode it
|
||||
+ // the returned result will be a float32 array since this is OpenAI API's default encoding
|
||||
+ (0, utils_1.loggerFor)(this._client).debug(
|
||||
+ "embeddings/decoding base64 embeddings from base64"
|
||||
+ );
|
||||
+ return response._thenUnwrap((response) => {
|
||||
+ if (response && response.data && typeof response.data[0]?.embedding === 'string') {
|
||||
+ response.data.forEach((embeddingBase64Obj) => {
|
||||
+ const embeddingBase64Str = embeddingBase64Obj.embedding;
|
||||
+ embeddingBase64Obj.embedding = (0, utils_1.toFloat32Array)(
|
||||
+ embeddingBase64Str
|
||||
+ );
|
||||
+ });
|
||||
+ }
|
||||
+ return response;
|
||||
+ });
|
||||
+ }
|
||||
}
|
||||
exports.Embeddings = Embeddings;
|
||||
//# sourceMappingURL=embeddings.js.map
|
||||
diff --git a/resources/embeddings.mjs b/resources/embeddings.mjs
|
||||
index 19dcaef578c194a89759c4360073cfd4f7dd2cbf..0284e9cc615c900eff508eb595f7360a74bd9200 100644
|
||||
--- a/resources/embeddings.mjs
|
||||
+++ b/resources/embeddings.mjs
|
||||
@@ -2,51 +2,61 @@
|
||||
import { APIResource } from "../core/resource.mjs";
|
||||
import { loggerFor, toFloat32Array } from "../internal/utils.mjs";
|
||||
export class Embeddings extends APIResource {
|
||||
- /**
|
||||
- * Creates an embedding vector representing the input text.
|
||||
- *
|
||||
- * @example
|
||||
- * ```ts
|
||||
- * const createEmbeddingResponse =
|
||||
- * await client.embeddings.create({
|
||||
- * input: 'The quick brown fox jumped over the lazy dog',
|
||||
- * model: 'text-embedding-3-small',
|
||||
- * });
|
||||
- * ```
|
||||
- */
|
||||
- create(body, options) {
|
||||
- const hasUserProvidedEncodingFormat = !!body.encoding_format;
|
||||
- // No encoding_format specified, defaulting to base64 for performance reasons
|
||||
- // See https://github.com/openai/openai-node/pull/1312
|
||||
- let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';
|
||||
- if (hasUserProvidedEncodingFormat) {
|
||||
- loggerFor(this._client).debug('embeddings/user defined encoding_format:', body.encoding_format);
|
||||
- }
|
||||
- const response = this._client.post('/embeddings', {
|
||||
- body: {
|
||||
- ...body,
|
||||
- encoding_format: encoding_format,
|
||||
- },
|
||||
- ...options,
|
||||
- });
|
||||
- // if the user specified an encoding_format, return the response as-is
|
||||
- if (hasUserProvidedEncodingFormat) {
|
||||
- return response;
|
||||
- }
|
||||
- // in this stage, we are sure the user did not specify an encoding_format
|
||||
- // and we defaulted to base64 for performance reasons
|
||||
- // we are sure then that the response is base64 encoded, let's decode it
|
||||
- // the returned result will be a float32 array since this is OpenAI API's default encoding
|
||||
- loggerFor(this._client).debug('embeddings/decoding base64 embeddings from base64');
|
||||
- return response._thenUnwrap((response) => {
|
||||
- if (response && response.data) {
|
||||
- response.data.forEach((embeddingBase64Obj) => {
|
||||
- const embeddingBase64Str = embeddingBase64Obj.embedding;
|
||||
- embeddingBase64Obj.embedding = toFloat32Array(embeddingBase64Str);
|
||||
- });
|
||||
- }
|
||||
- return response;
|
||||
- });
|
||||
- }
|
||||
+ /**
|
||||
+ * Creates an embedding vector representing the input text.
|
||||
+ *
|
||||
+ * @example
|
||||
+ * ```ts
|
||||
+ * const createEmbeddingResponse =
|
||||
+ * await client.embeddings.create({
|
||||
+ * input: 'The quick brown fox jumped over the lazy dog',
|
||||
+ * model: 'text-embedding-3-small',
|
||||
+ * });
|
||||
+ * ```
|
||||
+ */
|
||||
+ create(body, options) {
|
||||
+ const hasUserProvidedEncodingFormat = !!body.encoding_format;
|
||||
+ // No encoding_format specified, defaulting to base64 for performance reasons
|
||||
+ // See https://github.com/openai/openai-node/pull/1312
|
||||
+ let encoding_format = hasUserProvidedEncodingFormat
|
||||
+ ? body.encoding_format
|
||||
+ : "base64";
|
||||
+ if (body.model.includes("jina")) {
|
||||
+ encoding_format = undefined;
|
||||
+ }
|
||||
+ if (hasUserProvidedEncodingFormat) {
|
||||
+ loggerFor(this._client).debug(
|
||||
+ "embeddings/user defined encoding_format:",
|
||||
+ body.encoding_format
|
||||
+ );
|
||||
+ }
|
||||
+ const response = this._client.post("/embeddings", {
|
||||
+ body: {
|
||||
+ ...body,
|
||||
+ encoding_format: encoding_format,
|
||||
+ },
|
||||
+ ...options,
|
||||
+ });
|
||||
+ // if the user specified an encoding_format, return the response as-is
|
||||
+ if (hasUserProvidedEncodingFormat || body.model.includes("jina")) {
|
||||
+ return response;
|
||||
+ }
|
||||
+ // in this stage, we are sure the user did not specify an encoding_format
|
||||
+ // and we defaulted to base64 for performance reasons
|
||||
+ // we are sure then that the response is base64 encoded, let's decode it
|
||||
+ // the returned result will be a float32 array since this is OpenAI API's default encoding
|
||||
+ loggerFor(this._client).debug(
|
||||
+ "embeddings/decoding base64 embeddings from base64"
|
||||
+ );
|
||||
+ return response._thenUnwrap((response) => {
|
||||
+ if (response && response.data && typeof response.data[0]?.embedding === 'string') {
|
||||
+ response.data.forEach((embeddingBase64Obj) => {
|
||||
+ const embeddingBase64Str = embeddingBase64Obj.embedding;
|
||||
+ embeddingBase64Obj.embedding = toFloat32Array(embeddingBase64Str);
|
||||
+ });
|
||||
+ }
|
||||
+ return response;
|
||||
+ });
|
||||
+ }
|
||||
}
|
||||
//# sourceMappingURL=embeddings.mjs.map
|
||||
BIN
.yarn/patches/openai-npm-5.12.2-30b075401c.patch
vendored
Normal file
BIN
.yarn/patches/openai-npm-5.12.2-30b075401c.patch
vendored
Normal file
Binary file not shown.
348
.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch
vendored
Normal file
348
.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch
vendored
Normal file
@ -0,0 +1,348 @@
|
||||
diff --git a/src/constants/languages.d.ts b/src/constants/languages.d.ts
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..6a2ba5086187622b8ca8887bcc7406018fba8a89
|
||||
--- /dev/null
|
||||
+++ b/src/constants/languages.d.ts
|
||||
@@ -0,0 +1,43 @@
|
||||
+/**
|
||||
+ * Languages with existing tesseract traineddata
|
||||
+ * https://tesseract-ocr.github.io/tessdoc/Data-Files#data-files-for-version-400-november-29-2016
|
||||
+ */
|
||||
+
|
||||
+// Define the language codes as string literals
|
||||
+type LanguageCode =
|
||||
+ | 'afr' | 'amh' | 'ara' | 'asm' | 'aze' | 'aze_cyrl' | 'bel' | 'ben' | 'bod' | 'bos'
|
||||
+ | 'bul' | 'cat' | 'ceb' | 'ces' | 'chi_sim' | 'chi_tra' | 'chr' | 'cym' | 'dan' | 'deu'
|
||||
+ | 'dzo' | 'ell' | 'eng' | 'enm' | 'epo' | 'est' | 'eus' | 'fas' | 'fin' | 'fra'
|
||||
+ | 'frk' | 'frm' | 'gle' | 'glg' | 'grc' | 'guj' | 'hat' | 'heb' | 'hin' | 'hrv'
|
||||
+ | 'hun' | 'iku' | 'ind' | 'isl' | 'ita' | 'ita_old' | 'jav' | 'jpn' | 'kan' | 'kat'
|
||||
+ | 'kat_old' | 'kaz' | 'khm' | 'kir' | 'kor' | 'kur' | 'lao' | 'lat' | 'lav' | 'lit'
|
||||
+ | 'mal' | 'mar' | 'mkd' | 'mlt' | 'msa' | 'mya' | 'nep' | 'nld' | 'nor' | 'ori'
|
||||
+ | 'pan' | 'pol' | 'por' | 'pus' | 'ron' | 'rus' | 'san' | 'sin' | 'slk' | 'slv'
|
||||
+ | 'spa' | 'spa_old' | 'sqi' | 'srp' | 'srp_latn' | 'swa' | 'swe' | 'syr' | 'tam' | 'tel'
|
||||
+ | 'tgk' | 'tgl' | 'tha' | 'tir' | 'tur' | 'uig' | 'ukr' | 'urd' | 'uzb' | 'uzb_cyrl'
|
||||
+ | 'vie' | 'yid';
|
||||
+
|
||||
+// Define the language keys as string literals
|
||||
+type LanguageKey =
|
||||
+ | 'AFR' | 'AMH' | 'ARA' | 'ASM' | 'AZE' | 'AZE_CYRL' | 'BEL' | 'BEN' | 'BOD' | 'BOS'
|
||||
+ | 'BUL' | 'CAT' | 'CEB' | 'CES' | 'CHI_SIM' | 'CHI_TRA' | 'CHR' | 'CYM' | 'DAN' | 'DEU'
|
||||
+ | 'DZO' | 'ELL' | 'ENG' | 'ENM' | 'EPO' | 'EST' | 'EUS' | 'FAS' | 'FIN' | 'FRA'
|
||||
+ | 'FRK' | 'FRM' | 'GLE' | 'GLG' | 'GRC' | 'GUJ' | 'HAT' | 'HEB' | 'HIN' | 'HRV'
|
||||
+ | 'HUN' | 'IKU' | 'IND' | 'ISL' | 'ITA' | 'ITA_OLD' | 'JAV' | 'JPN' | 'KAN' | 'KAT'
|
||||
+ | 'KAT_OLD' | 'KAZ' | 'KHM' | 'KIR' | 'KOR' | 'KUR' | 'LAO' | 'LAT' | 'LAV' | 'LIT'
|
||||
+ | 'MAL' | 'MAR' | 'MKD' | 'MLT' | 'MSA' | 'MYA' | 'NEP' | 'NLD' | 'NOR' | 'ORI'
|
||||
+ | 'PAN' | 'POL' | 'POR' | 'PUS' | 'RON' | 'RUS' | 'SAN' | 'SIN' | 'SLK' | 'SLV'
|
||||
+ | 'SPA' | 'SPA_OLD' | 'SQI' | 'SRP' | 'SRP_LATN' | 'SWA' | 'SWE' | 'SYR' | 'TAM' | 'TEL'
|
||||
+ | 'TGK' | 'TGL' | 'THA' | 'TIR' | 'TUR' | 'UIG' | 'UKR' | 'URD' | 'UZB' | 'UZB_CYRL'
|
||||
+ | 'VIE' | 'YID';
|
||||
+
|
||||
+// Create a mapped type to ensure each key maps to its specific value
|
||||
+type LanguagesMap = {
|
||||
+ [K in LanguageKey]: LanguageCode;
|
||||
+};
|
||||
+
|
||||
+// Declare the exported constant with the specific type
|
||||
+export const LANGUAGES: LanguagesMap;
|
||||
+
|
||||
+// Export the individual types for use in other modules
|
||||
+export type { LanguageCode, LanguageKey, LanguagesMap };
|
||||
\ No newline at end of file
|
||||
diff --git a/src/index.d.ts b/src/index.d.ts
|
||||
index 1f5a9c8094fe4de7983467f9efb43bdb4de535f2..16dc95cf68663673e37e189b719cb74897b7735f 100644
|
||||
--- a/src/index.d.ts
|
||||
+++ b/src/index.d.ts
|
||||
@@ -1,31 +1,74 @@
|
||||
+// Import the languages types
|
||||
+import { LanguagesMap } from "./constants/languages";
|
||||
+
|
||||
+/// <reference types="node" />
|
||||
+
|
||||
declare namespace Tesseract {
|
||||
- function createScheduler(): Scheduler
|
||||
- function createWorker(langs?: string | string[] | Lang[], oem?: OEM, options?: Partial<WorkerOptions>, config?: string | Partial<InitOptions>): Promise<Worker>
|
||||
- function setLogging(logging: boolean): void
|
||||
- function recognize(image: ImageLike, langs?: string, options?: Partial<WorkerOptions>): Promise<RecognizeResult>
|
||||
- function detect(image: ImageLike, options?: Partial<WorkerOptions>): any
|
||||
+ function createScheduler(): Scheduler;
|
||||
+ function createWorker(
|
||||
+ langs?: LanguageCode | LanguageCode[] | Lang[],
|
||||
+ oem?: OEM,
|
||||
+ options?: Partial<WorkerOptions>,
|
||||
+ config?: string | Partial<InitOptions>
|
||||
+ ): Promise<Worker>;
|
||||
+ function setLogging(logging: boolean): void;
|
||||
+ function recognize(
|
||||
+ image: ImageLike,
|
||||
+ langs?: LanguageCode,
|
||||
+ options?: Partial<WorkerOptions>
|
||||
+ ): Promise<RecognizeResult>;
|
||||
+ function detect(image: ImageLike, options?: Partial<WorkerOptions>): any;
|
||||
+
|
||||
+ // Export languages constant
|
||||
+ const languages: LanguagesMap;
|
||||
+
|
||||
+ type LanguageCode = import("./constants/languages").LanguageCode;
|
||||
+ type LanguageKey = import("./constants/languages").LanguageKey;
|
||||
|
||||
interface Scheduler {
|
||||
- addWorker(worker: Worker): string
|
||||
- addJob(action: 'recognize', ...args: Parameters<Worker['recognize']>): Promise<RecognizeResult>
|
||||
- addJob(action: 'detect', ...args: Parameters<Worker['detect']>): Promise<DetectResult>
|
||||
- terminate(): Promise<any>
|
||||
- getQueueLen(): number
|
||||
- getNumWorkers(): number
|
||||
+ addWorker(worker: Worker): string;
|
||||
+ addJob(
|
||||
+ action: "recognize",
|
||||
+ ...args: Parameters<Worker["recognize"]>
|
||||
+ ): Promise<RecognizeResult>;
|
||||
+ addJob(
|
||||
+ action: "detect",
|
||||
+ ...args: Parameters<Worker["detect"]>
|
||||
+ ): Promise<DetectResult>;
|
||||
+ terminate(): Promise<any>;
|
||||
+ getQueueLen(): number;
|
||||
+ getNumWorkers(): number;
|
||||
}
|
||||
|
||||
interface Worker {
|
||||
- load(jobId?: string): Promise<ConfigResult>
|
||||
- writeText(path: string, text: string, jobId?: string): Promise<ConfigResult>
|
||||
- readText(path: string, jobId?: string): Promise<ConfigResult>
|
||||
- removeText(path: string, jobId?: string): Promise<ConfigResult>
|
||||
- FS(method: string, args: any[], jobId?: string): Promise<ConfigResult>
|
||||
- reinitialize(langs?: string | Lang[], oem?: OEM, config?: string | Partial<InitOptions>, jobId?: string): Promise<ConfigResult>
|
||||
- setParameters(params: Partial<WorkerParams>, jobId?: string): Promise<ConfigResult>
|
||||
- getImage(type: imageType): string
|
||||
- recognize(image: ImageLike, options?: Partial<RecognizeOptions>, output?: Partial<OutputFormats>, jobId?: string): Promise<RecognizeResult>
|
||||
- detect(image: ImageLike, jobId?: string): Promise<DetectResult>
|
||||
- terminate(jobId?: string): Promise<ConfigResult>
|
||||
+ load(jobId?: string): Promise<ConfigResult>;
|
||||
+ writeText(
|
||||
+ path: string,
|
||||
+ text: string,
|
||||
+ jobId?: string
|
||||
+ ): Promise<ConfigResult>;
|
||||
+ readText(path: string, jobId?: string): Promise<ConfigResult>;
|
||||
+ removeText(path: string, jobId?: string): Promise<ConfigResult>;
|
||||
+ FS(method: string, args: any[], jobId?: string): Promise<ConfigResult>;
|
||||
+ reinitialize(
|
||||
+ langs?: string | Lang[],
|
||||
+ oem?: OEM,
|
||||
+ config?: string | Partial<InitOptions>,
|
||||
+ jobId?: string
|
||||
+ ): Promise<ConfigResult>;
|
||||
+ setParameters(
|
||||
+ params: Partial<WorkerParams>,
|
||||
+ jobId?: string
|
||||
+ ): Promise<ConfigResult>;
|
||||
+ getImage(type: imageType): string;
|
||||
+ recognize(
|
||||
+ image: ImageLike,
|
||||
+ options?: Partial<RecognizeOptions>,
|
||||
+ output?: Partial<OutputFormats>,
|
||||
+ jobId?: string
|
||||
+ ): Promise<RecognizeResult>;
|
||||
+ detect(image: ImageLike, jobId?: string): Promise<DetectResult>;
|
||||
+ terminate(jobId?: string): Promise<ConfigResult>;
|
||||
}
|
||||
|
||||
interface Lang {
|
||||
@@ -34,43 +77,43 @@ declare namespace Tesseract {
|
||||
}
|
||||
|
||||
interface InitOptions {
|
||||
- load_system_dawg: string
|
||||
- load_freq_dawg: string
|
||||
- load_unambig_dawg: string
|
||||
- load_punc_dawg: string
|
||||
- load_number_dawg: string
|
||||
- load_bigram_dawg: string
|
||||
- }
|
||||
-
|
||||
- type LoggerMessage = {
|
||||
- jobId: string
|
||||
- progress: number
|
||||
- status: string
|
||||
- userJobId: string
|
||||
- workerId: string
|
||||
+ load_system_dawg: string;
|
||||
+ load_freq_dawg: string;
|
||||
+ load_unambig_dawg: string;
|
||||
+ load_punc_dawg: string;
|
||||
+ load_number_dawg: string;
|
||||
+ load_bigram_dawg: string;
|
||||
}
|
||||
-
|
||||
+
|
||||
+ type LoggerMessage = {
|
||||
+ jobId: string;
|
||||
+ progress: number;
|
||||
+ status: string;
|
||||
+ userJobId: string;
|
||||
+ workerId: string;
|
||||
+ };
|
||||
+
|
||||
interface WorkerOptions {
|
||||
- corePath: string
|
||||
- langPath: string
|
||||
- cachePath: string
|
||||
- dataPath: string
|
||||
- workerPath: string
|
||||
- cacheMethod: string
|
||||
- workerBlobURL: boolean
|
||||
- gzip: boolean
|
||||
- legacyLang: boolean
|
||||
- legacyCore: boolean
|
||||
- logger: (arg: LoggerMessage) => void,
|
||||
- errorHandler: (arg: any) => void
|
||||
+ corePath: string;
|
||||
+ langPath: string;
|
||||
+ cachePath: string;
|
||||
+ dataPath: string;
|
||||
+ workerPath: string;
|
||||
+ cacheMethod: string;
|
||||
+ workerBlobURL: boolean;
|
||||
+ gzip: boolean;
|
||||
+ legacyLang: boolean;
|
||||
+ legacyCore: boolean;
|
||||
+ logger: (arg: LoggerMessage) => void;
|
||||
+ errorHandler: (arg: any) => void;
|
||||
}
|
||||
interface WorkerParams {
|
||||
- tessedit_pageseg_mode: PSM
|
||||
- tessedit_char_whitelist: string
|
||||
- tessedit_char_blacklist: string
|
||||
- preserve_interword_spaces: string
|
||||
- user_defined_dpi: string
|
||||
- [propName: string]: any
|
||||
+ tessedit_pageseg_mode: PSM;
|
||||
+ tessedit_char_whitelist: string;
|
||||
+ tessedit_char_blacklist: string;
|
||||
+ preserve_interword_spaces: string;
|
||||
+ user_defined_dpi: string;
|
||||
+ [propName: string]: any;
|
||||
}
|
||||
interface OutputFormats {
|
||||
text: boolean;
|
||||
@@ -88,36 +131,36 @@ declare namespace Tesseract {
|
||||
debug: boolean;
|
||||
}
|
||||
interface RecognizeOptions {
|
||||
- rectangle: Rectangle
|
||||
- pdfTitle: string
|
||||
- pdfTextOnly: boolean
|
||||
- rotateAuto: boolean
|
||||
- rotateRadians: number
|
||||
+ rectangle: Rectangle;
|
||||
+ pdfTitle: string;
|
||||
+ pdfTextOnly: boolean;
|
||||
+ rotateAuto: boolean;
|
||||
+ rotateRadians: number;
|
||||
}
|
||||
interface ConfigResult {
|
||||
- jobId: string
|
||||
- data: any
|
||||
+ jobId: string;
|
||||
+ data: any;
|
||||
}
|
||||
interface RecognizeResult {
|
||||
- jobId: string
|
||||
- data: Page
|
||||
+ jobId: string;
|
||||
+ data: Page;
|
||||
}
|
||||
interface DetectResult {
|
||||
- jobId: string
|
||||
- data: DetectData
|
||||
+ jobId: string;
|
||||
+ data: DetectData;
|
||||
}
|
||||
interface DetectData {
|
||||
- tesseract_script_id: number | null
|
||||
- script: string | null
|
||||
- script_confidence: number | null
|
||||
- orientation_degrees: number | null
|
||||
- orientation_confidence: number | null
|
||||
+ tesseract_script_id: number | null;
|
||||
+ script: string | null;
|
||||
+ script_confidence: number | null;
|
||||
+ orientation_degrees: number | null;
|
||||
+ orientation_confidence: number | null;
|
||||
}
|
||||
interface Rectangle {
|
||||
- left: number
|
||||
- top: number
|
||||
- width: number
|
||||
- height: number
|
||||
+ left: number;
|
||||
+ top: number;
|
||||
+ width: number;
|
||||
+ height: number;
|
||||
}
|
||||
enum OEM {
|
||||
TESSERACT_ONLY,
|
||||
@@ -126,28 +169,36 @@ declare namespace Tesseract {
|
||||
DEFAULT,
|
||||
}
|
||||
enum PSM {
|
||||
- OSD_ONLY = '0',
|
||||
- AUTO_OSD = '1',
|
||||
- AUTO_ONLY = '2',
|
||||
- AUTO = '3',
|
||||
- SINGLE_COLUMN = '4',
|
||||
- SINGLE_BLOCK_VERT_TEXT = '5',
|
||||
- SINGLE_BLOCK = '6',
|
||||
- SINGLE_LINE = '7',
|
||||
- SINGLE_WORD = '8',
|
||||
- CIRCLE_WORD = '9',
|
||||
- SINGLE_CHAR = '10',
|
||||
- SPARSE_TEXT = '11',
|
||||
- SPARSE_TEXT_OSD = '12',
|
||||
- RAW_LINE = '13'
|
||||
+ OSD_ONLY = "0",
|
||||
+ AUTO_OSD = "1",
|
||||
+ AUTO_ONLY = "2",
|
||||
+ AUTO = "3",
|
||||
+ SINGLE_COLUMN = "4",
|
||||
+ SINGLE_BLOCK_VERT_TEXT = "5",
|
||||
+ SINGLE_BLOCK = "6",
|
||||
+ SINGLE_LINE = "7",
|
||||
+ SINGLE_WORD = "8",
|
||||
+ CIRCLE_WORD = "9",
|
||||
+ SINGLE_CHAR = "10",
|
||||
+ SPARSE_TEXT = "11",
|
||||
+ SPARSE_TEXT_OSD = "12",
|
||||
+ RAW_LINE = "13",
|
||||
}
|
||||
const enum imageType {
|
||||
COLOR = 0,
|
||||
GREY = 1,
|
||||
- BINARY = 2
|
||||
+ BINARY = 2,
|
||||
}
|
||||
- type ImageLike = string | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement
|
||||
- | CanvasRenderingContext2D | File | Blob | Buffer | OffscreenCanvas;
|
||||
+ type ImageLike =
|
||||
+ | string
|
||||
+ | HTMLImageElement
|
||||
+ | HTMLCanvasElement
|
||||
+ | HTMLVideoElement
|
||||
+ | CanvasRenderingContext2D
|
||||
+ | File
|
||||
+ | Blob
|
||||
+ | (typeof Buffer extends undefined ? never : Buffer)
|
||||
+ | OffscreenCanvas;
|
||||
interface Block {
|
||||
paragraphs: Paragraph[];
|
||||
text: string;
|
||||
@@ -179,7 +230,7 @@ declare namespace Tesseract {
|
||||
text: string;
|
||||
confidence: number;
|
||||
baseline: Baseline;
|
||||
- rowAttributes: RowAttributes
|
||||
+ rowAttributes: RowAttributes;
|
||||
bbox: Bbox;
|
||||
}
|
||||
interface Paragraph {
|
||||
120
CLAUDE.md
Normal file
120
CLAUDE.md
Normal file
@ -0,0 +1,120 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Development Commands
|
||||
|
||||
### Environment Setup
|
||||
|
||||
- **Prerequisites**: Node.js v22.x.x or higher, Yarn 4.9.1
|
||||
- **Setup Yarn**: `corepack enable && corepack prepare yarn@4.9.1 --activate`
|
||||
- **Install Dependencies**: `yarn install`
|
||||
|
||||
### Development
|
||||
|
||||
- **Start Development**: `yarn dev` - Runs Electron app in development mode
|
||||
- **Debug Mode**: `yarn debug` - Starts with debugging enabled, use chrome://inspect
|
||||
|
||||
### Testing & Quality
|
||||
|
||||
- **Run Tests**: `yarn test` - Runs all tests (Vitest)
|
||||
- **Run E2E Tests**: `yarn test:e2e` - Playwright end-to-end tests
|
||||
- **Type Check**: `yarn typecheck` - Checks TypeScript for both node and web
|
||||
- **Lint**: `yarn lint` - ESLint with auto-fix
|
||||
- **Format**: `yarn format` - Prettier formatting
|
||||
|
||||
### Build & Release
|
||||
|
||||
- **Build**: `yarn build` - Builds for production (includes typecheck)
|
||||
- **Platform-specific builds**:
|
||||
- Windows: `yarn build:win`
|
||||
- macOS: `yarn build:mac`
|
||||
- Linux: `yarn build:linux`
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### Electron Multi-Process Architecture
|
||||
|
||||
- **Main Process** (`src/main/`): Node.js backend handling system integration, file operations, and services
|
||||
- **Renderer Process** (`src/renderer/`): React-based UI running in Chromium
|
||||
- **Preload Scripts** (`src/preload/`): Secure bridge between main and renderer processes
|
||||
|
||||
### Key Architectural Components
|
||||
|
||||
#### Main Process Services (`src/main/services/`)
|
||||
|
||||
- **MCPService**: Model Context Protocol server management
|
||||
- **KnowledgeService**: Document processing and knowledge base management
|
||||
- **FileStorage/S3Storage/WebDav**: Multiple storage backends
|
||||
- **WindowService**: Multi-window management (main, mini, selection windows)
|
||||
- **ProxyManager**: Network proxy handling
|
||||
- **SearchService**: Full-text search capabilities
|
||||
|
||||
#### AI Core (`src/renderer/src/aiCore/`)
|
||||
|
||||
- **Middleware System**: Composable pipeline for AI request processing
|
||||
- **Client Factory**: Supports multiple AI providers (OpenAI, Anthropic, Gemini, etc.)
|
||||
- **Stream Processing**: Real-time response handling
|
||||
|
||||
#### State Management (`src/renderer/src/store/`)
|
||||
|
||||
- **Redux Toolkit**: Centralized state management
|
||||
- **Persistent Storage**: Redux-persist for data persistence
|
||||
- **Thunks**: Async actions for complex operations
|
||||
|
||||
#### Knowledge Management
|
||||
|
||||
- **Embeddings**: Vector search with multiple providers (OpenAI, Voyage, etc.)
|
||||
- **OCR**: Document text extraction (system OCR, Doc2x, Mineru)
|
||||
- **Preprocessing**: Document preparation pipeline
|
||||
- **Loaders**: Support for various file formats (PDF, DOCX, EPUB, etc.)
|
||||
|
||||
### Build System
|
||||
|
||||
- **Electron-Vite**: Development and build tooling (v4.0.0)
|
||||
- **Rolldown-Vite**: Using experimental rolldown-vite instead of standard vite
|
||||
- **Workspaces**: Monorepo structure with `packages/` directory
|
||||
- **Multiple Entry Points**: Main app, mini window, selection toolbar
|
||||
- **Styled Components**: CSS-in-JS styling with SWC optimization
|
||||
|
||||
### Testing Strategy
|
||||
|
||||
- **Vitest**: Unit and integration testing
|
||||
- **Playwright**: End-to-end testing
|
||||
- **Component Testing**: React Testing Library
|
||||
- **Coverage**: Available via `yarn test:coverage`
|
||||
|
||||
### Key Patterns
|
||||
|
||||
- **IPC Communication**: Secure main-renderer communication via preload scripts
|
||||
- **Service Layer**: Clear separation between UI and business logic
|
||||
- **Plugin Architecture**: Extensible via MCP servers and middleware
|
||||
- **Multi-language Support**: i18n with dynamic loading
|
||||
- **Theme System**: Light/dark themes with custom CSS variables
|
||||
|
||||
## Logging Standards
|
||||
|
||||
### Usage
|
||||
|
||||
```typescript
|
||||
// Main process
|
||||
import { loggerService } from '@logger'
|
||||
const logger = loggerService.withContext('moduleName')
|
||||
|
||||
// Renderer process (set window source first)
|
||||
loggerService.initWindowSource('windowName')
|
||||
const logger = loggerService.withContext('moduleName')
|
||||
|
||||
// Logging
|
||||
logger.info('message', CONTEXT)
|
||||
logger.error('message', new Error('error'), CONTEXT)
|
||||
```
|
||||
|
||||
### Log Levels (highest to lowest)
|
||||
|
||||
- `error` - Critical errors causing crash/unusable functionality
|
||||
- `warn` - Potential issues that don't affect core functionality
|
||||
- `info` - Application lifecycle and key user actions
|
||||
- `verbose` - Detailed flow information for feature tracing
|
||||
- `debug` - Development diagnostic info (not for production)
|
||||
- `silly` - Extreme debugging, low-level information
|
||||
36
README.md
36
README.md
@ -13,7 +13,7 @@
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=fr">Français</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=de">Deutsch</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=es">Español</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=it">Itapano</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=it">Italiano</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=ru">Русский</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=pt">Português</a></p>
|
||||
<p><a href="https://openaitx.github.io/view.html?user=CherryHQ&project=cherry-studio&lang=nl">Nederlands</a></p>
|
||||
@ -57,13 +57,13 @@
|
||||
|
||||
<div align="center">
|
||||
<a href="https://hellogithub.com/repository/1605492e1e2a4df3be07abfa4578dd37" target="_blank" style="text-decoration: none"><img src="https://api.hellogithub.com/v1/widgets/recommend.svg?rid=1605492e1e2a4df3be07abfa4578dd37" alt="Featured|HelloGitHub" width="220" height="55" /></a>
|
||||
<a href="https://trendshift.io/repositories/11772" target="_blank" style="text-decoration: none"><img src="https://trendshift.io/api/badge/repositories/11772" alt="kangfenmao%2Fcherry-studio | Trendshift" width="220" height="55" /></a>
|
||||
<a href="https://trendshift.io/repositories/14318" target="_blank" style="text-decoration: none"><img src="https://trendshift.io/api/badge/repositories/14318" alt="CherryHQ%2Fcherry-studio | Trendshift" width="220" height="55" /></a>
|
||||
<a href="https://www.producthunt.com/posts/cherry-studio?embed=true&utm_source=badge-featured&utm_medium=badge&utm_souce=badge-cherry-studio" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=496640&theme=light" alt="Cherry Studio - AI Chatbots, AI Desktop Client | Product Hunt" width="220" height="55" /></a>
|
||||
</div>
|
||||
|
||||
# 🍒 Cherry Studio
|
||||
|
||||
Cherry Studio is a desktop client that supports for multiple LLM providers, available on Windows, Mac and Linux.
|
||||
Cherry Studio is a desktop client that supports multiple LLM providers, available on Windows, Mac and Linux.
|
||||
|
||||
👏 Join [Telegram Group](https://t.me/CherryStudioAI)|[Discord](https://discord.gg/wez8HtpxqQ) | [QQ Group(575014769)](https://qm.qq.com/q/lo0D4qVZKi)
|
||||
|
||||
@ -93,7 +93,7 @@ Cherry Studio is a desktop client that supports for multiple LLM providers, avai
|
||||
|
||||
3. **Document & Data Processing**:
|
||||
|
||||
- 📄 Support for Text, Images, Office, PDF, and more
|
||||
- 📄 Supports Text, Images, Office, PDF, and more
|
||||
- ☁️ WebDAV File Management and Backup
|
||||
- 📊 Mermaid Chart Visualization
|
||||
- 💻 Code Syntax Highlighting
|
||||
@ -110,7 +110,7 @@ Cherry Studio is a desktop client that supports for multiple LLM providers, avai
|
||||
5. **Enhanced User Experience**:
|
||||
|
||||
- 🖥️ Cross-platform Support for Windows, Mac, and Linux
|
||||
- 📦 Ready to Use, No Environment Setup Required
|
||||
- 📦 Ready to Use - No Environment Setup Required
|
||||
- 🎨 Light/Dark Themes and Transparent Window
|
||||
- 📝 Complete Markdown Rendering
|
||||
- 🤲 Easy Content Sharing
|
||||
@ -121,11 +121,11 @@ We're actively working on the following features and improvements:
|
||||
|
||||
1. 🎯 **Core Features**
|
||||
|
||||
- Selection Assistant - Smart content selection enhancement
|
||||
- Deep Research - Advanced research capabilities
|
||||
- Memory System - Global context awareness
|
||||
- Document Preprocessing - Improved document handling
|
||||
- MCP Marketplace - Model Context Protocol ecosystem
|
||||
- Selection Assistant with smart content selection enhancement
|
||||
- Deep Research with advanced research capabilities
|
||||
- Memory System with global context awareness
|
||||
- Document Preprocessing with improved document handling
|
||||
- MCP Marketplace for Model Context Protocol ecosystem
|
||||
|
||||
2. 🗂 **Knowledge Management**
|
||||
|
||||
@ -199,7 +199,7 @@ To give back to our core contributors and create a virtuous cycle, we have estab
|
||||
|
||||
**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:
|
||||
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 will be 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.
|
||||
@ -223,17 +223,17 @@ 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.
|
||||
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.
|
||||
- **Enterprise-Grade Knowledge Base**: Build, manage, and share team-wide knowledge bases. Ensures knowledge retention and consistency, 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.
|
||||
- **Reliable Backend Services**: Provides stable API services and enterprise-grade data backup and recovery mechanisms to ensure business continuity.
|
||||
|
||||
## ✨ Online Demo
|
||||
|
||||
@ -247,23 +247,23 @@ The Enterprise Edition addresses core challenges in team collaboration by centra
|
||||
|
||||
| Feature | Community Edition | Enterprise Edition |
|
||||
| :---------------- | :----------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Open Source** | ✅ Yes | ⭕️ part. released to cust. |
|
||||
| **Open Source** | ✅ Yes | ⭕️ Partially released to customers |
|
||||
| **Cost** | Free for Personal Use / Commercial License | Buyout / Subscription Fee |
|
||||
| **Admin Backend** | — | ● Centralized **Model** Access<br>● **Employee** Management<br>● Shared **Knowledge Base**<br>● **Access** Control<br>● **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.
|
||||
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 feel free to 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.
|
||||
- [one-api](https://github.com/songquanpeng/one-api): LLM API management and distribution system supporting mainstream models like OpenAI, Azure, and Anthropic. Features a unified API interface, suitable for key management and secondary distribution.
|
||||
|
||||
- [ublacklist](https://github.com/iorate/ublacklist):Blocks specific sites from appearing in Google search results
|
||||
- [ublacklist](https://github.com/iorate/ublacklist): Blocks specific sites from appearing in Google search results
|
||||
|
||||
# 🚀 Contributors
|
||||
|
||||
|
||||
64
SECURITY.md
Normal file
64
SECURITY.md
Normal file
@ -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!
|
||||
@ -8,5 +8,7 @@
|
||||
<true/>
|
||||
<key>com.apple.security.cs.allow-dyld-environment-variables</key>
|
||||
<true/>
|
||||
<key>com.apple.security.cs.disable-library-validation</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@ -8,16 +8,93 @@
|
||||
; https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist
|
||||
|
||||
!include LogicLib.nsh
|
||||
!include x64.nsh
|
||||
|
||||
; https://github.com/electron-userland/electron-builder/issues/1122
|
||||
!ifndef BUILD_UNINSTALLER
|
||||
Function checkVCRedist
|
||||
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" "Installed"
|
||||
FunctionEnd
|
||||
|
||||
Function checkArchitectureCompatibility
|
||||
; Initialize variables
|
||||
StrCpy $0 "0" ; Default to incompatible
|
||||
StrCpy $1 "" ; System architecture
|
||||
StrCpy $3 "" ; App architecture
|
||||
|
||||
; Check system architecture using built-in NSIS functions
|
||||
${If} ${RunningX64}
|
||||
; Check if it's ARM64 by looking at processor architecture
|
||||
ReadEnvStr $2 "PROCESSOR_ARCHITECTURE"
|
||||
ReadEnvStr $4 "PROCESSOR_ARCHITEW6432"
|
||||
|
||||
${If} $2 == "ARM64"
|
||||
${OrIf} $4 == "ARM64"
|
||||
StrCpy $1 "arm64"
|
||||
${Else}
|
||||
StrCpy $1 "x64"
|
||||
${EndIf}
|
||||
${Else}
|
||||
StrCpy $1 "x86"
|
||||
${EndIf}
|
||||
|
||||
; Determine app architecture based on build variables
|
||||
!ifdef APP_ARM64_NAME
|
||||
!ifndef APP_64_NAME
|
||||
StrCpy $3 "arm64" ; App is ARM64 only
|
||||
!endif
|
||||
!endif
|
||||
!ifdef APP_64_NAME
|
||||
!ifndef APP_ARM64_NAME
|
||||
StrCpy $3 "x64" ; App is x64 only
|
||||
!endif
|
||||
!endif
|
||||
!ifdef APP_64_NAME
|
||||
!ifdef APP_ARM64_NAME
|
||||
StrCpy $3 "universal" ; Both architectures available
|
||||
!endif
|
||||
!endif
|
||||
|
||||
; If no architecture variables are defined, assume x64
|
||||
${If} $3 == ""
|
||||
StrCpy $3 "x64"
|
||||
${EndIf}
|
||||
|
||||
; Compare system and app architectures
|
||||
${If} $3 == "universal"
|
||||
; Universal build, compatible with all architectures
|
||||
StrCpy $0 "1"
|
||||
${ElseIf} $1 == $3
|
||||
; Architectures match
|
||||
StrCpy $0 "1"
|
||||
${Else}
|
||||
; Architectures don't match
|
||||
StrCpy $0 "0"
|
||||
${EndIf}
|
||||
FunctionEnd
|
||||
!endif
|
||||
|
||||
!macro customInit
|
||||
Push $0
|
||||
Push $1
|
||||
Push $2
|
||||
Push $3
|
||||
Push $4
|
||||
|
||||
; Check architecture compatibility first
|
||||
Call checkArchitectureCompatibility
|
||||
${If} $0 != "1"
|
||||
MessageBox MB_ICONEXCLAMATION "\
|
||||
Architecture Mismatch$\r$\n$\r$\n\
|
||||
This installer is not compatible with your system architecture.$\r$\n\
|
||||
Your system: $1$\r$\n\
|
||||
App architecture: $3$\r$\n$\r$\n\
|
||||
Please download the correct version from:$\r$\n\
|
||||
https://www.cherry-ai.com/"
|
||||
ExecShell "open" "https://www.cherry-ai.com/"
|
||||
Abort
|
||||
${EndIf}
|
||||
|
||||
Call checkVCRedist
|
||||
${If} $0 != "1"
|
||||
MessageBox MB_YESNO "\
|
||||
@ -43,5 +120,9 @@
|
||||
Abort
|
||||
${EndIf}
|
||||
ContinueInstall:
|
||||
Pop $4
|
||||
Pop $3
|
||||
Pop $2
|
||||
Pop $1
|
||||
Pop $0
|
||||
!macroend
|
||||
!macroend
|
||||
|
||||
@ -31,6 +31,12 @@ corepack prepare yarn@4.6.0 --activate
|
||||
yarn install
|
||||
```
|
||||
|
||||
### ENV
|
||||
|
||||
```bash
|
||||
copy .env.example .env
|
||||
```
|
||||
|
||||
### Start
|
||||
|
||||
```bash
|
||||
|
||||
222
docs/features/memory-guide-zh.md
Normal file
222
docs/features/memory-guide-zh.md
Normal file
@ -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 助手的智能程度和用户体验。如有更多问题,欢迎查阅文档或联系支持团队。
|
||||
BIN
docs/technical/.assets.how-to-i18n/demo-1.png
Normal file
BIN
docs/technical/.assets.how-to-i18n/demo-1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 150 KiB |
BIN
docs/technical/.assets.how-to-i18n/demo-2.png
Normal file
BIN
docs/technical/.assets.how-to-i18n/demo-2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
BIN
docs/technical/.assets.how-to-i18n/demo-3.png
Normal file
BIN
docs/technical/.assets.how-to-i18n/demo-3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
180
docs/technical/CodeBlockView-en.md
Normal file
180
docs/technical/CodeBlockView-en.md
Normal file
@ -0,0 +1,180 @@
|
||||
# CodeBlockView Component Structure
|
||||
|
||||
## Overview
|
||||
|
||||
CodeBlockView is the core component in Cherry Studio for displaying and manipulating code blocks. It supports multiple view modes and visual previews for special languages, providing rich interactive tools.
|
||||
|
||||
## Component Structure
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[CodeBlockView] --> B[CodeToolbar]
|
||||
A --> C[SourceView]
|
||||
A --> D[SpecialView]
|
||||
A --> E[StatusBar]
|
||||
|
||||
B --> F[CodeToolButton]
|
||||
|
||||
C --> G[CodeEditor / CodeViewer]
|
||||
|
||||
D --> H[MermaidPreview]
|
||||
D --> I[PlantUmlPreview]
|
||||
D --> J[SvgPreview]
|
||||
D --> K[GraphvizPreview]
|
||||
|
||||
F --> L[useCopyTool]
|
||||
F --> M[useDownloadTool]
|
||||
F --> N[useViewSourceTool]
|
||||
F --> O[useSplitViewTool]
|
||||
F --> P[useRunTool]
|
||||
F --> Q[useExpandTool]
|
||||
F --> R[useWrapTool]
|
||||
F --> S[useSaveTool]
|
||||
```
|
||||
|
||||
## Core Concepts
|
||||
|
||||
### View Types
|
||||
|
||||
- **preview**: Preview view, where non-source code is displayed as special views
|
||||
- **edit**: Edit view
|
||||
|
||||
### View Modes
|
||||
|
||||
- **source**: Source code view mode
|
||||
- **special**: Special view mode (Mermaid, PlantUML, SVG)
|
||||
- **split**: Split view mode (source code and special view displayed side by side)
|
||||
|
||||
### Special View Languages
|
||||
|
||||
- mermaid
|
||||
- plantuml
|
||||
- svg
|
||||
- dot
|
||||
- graphviz
|
||||
|
||||
## Component Details
|
||||
|
||||
### CodeBlockView Main Component
|
||||
|
||||
Main responsibilities:
|
||||
|
||||
1. Managing view mode state
|
||||
2. Coordinating the display of source code view and special view
|
||||
3. Managing toolbar tools
|
||||
4. Handling code execution state
|
||||
|
||||
### Subcomponents
|
||||
|
||||
#### CodeToolbar
|
||||
|
||||
- Toolbar displayed at the top-right corner of the code block
|
||||
- Contains core and quick tools
|
||||
- Dynamically displays relevant tools based on context
|
||||
|
||||
#### CodeEditor/CodeViewer Source View
|
||||
|
||||
- Editable code editor or read-only code viewer
|
||||
- Uses either component based on settings
|
||||
- Supports syntax highlighting for multiple programming languages
|
||||
|
||||
#### Special View Components
|
||||
|
||||
- **MermaidPreview**: Mermaid diagram preview
|
||||
- **PlantUmlPreview**: PlantUML diagram preview
|
||||
- **SvgPreview**: SVG image preview
|
||||
- **GraphvizPreview**: Graphviz diagram preview
|
||||
|
||||
All special view components share a common architecture for consistent user experience and functionality. For detailed information about these components and their implementation, see [Image Preview Components Documentation](./ImagePreview-en.md).
|
||||
|
||||
#### StatusBar
|
||||
|
||||
- Displays Python code execution results
|
||||
- Can show both text and image results
|
||||
|
||||
## Tool System
|
||||
|
||||
CodeBlockView uses a hook-based tool system:
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[CodeBlockView] --> B[useCopyTool]
|
||||
A --> C[useDownloadTool]
|
||||
A --> D[useViewSourceTool]
|
||||
A --> E[useSplitViewTool]
|
||||
A --> F[useRunTool]
|
||||
A --> G[useExpandTool]
|
||||
A --> H[useWrapTool]
|
||||
A --> I[useSaveTool]
|
||||
|
||||
B --> J[ToolManager]
|
||||
C --> J
|
||||
D --> J
|
||||
E --> J
|
||||
F --> J
|
||||
G --> J
|
||||
H --> J
|
||||
I --> J
|
||||
|
||||
J --> K[CodeToolbar]
|
||||
```
|
||||
|
||||
Each tool hook is responsible for registering specific function tool buttons to the tool manager, which then passes these tools to the CodeToolbar component for rendering.
|
||||
|
||||
### Tool Types
|
||||
|
||||
- **core**: Core tools, always displayed in the toolbar
|
||||
- **quick**: Quick tools, displayed in a dropdown menu when there are more than one
|
||||
|
||||
### Tool List
|
||||
|
||||
1. **Copy**: Copy code or image
|
||||
2. **Download**: Download code or image
|
||||
3. **View Source**: Switch between special view and source code view
|
||||
4. **Split View**: Toggle split view mode
|
||||
5. **Run**: Run Python code
|
||||
6. **Expand/Collapse**: Control code block expansion/collapse
|
||||
7. **Wrap**: Control automatic line wrapping
|
||||
8. **Save**: Save edited code
|
||||
|
||||
## State Management
|
||||
|
||||
CodeBlockView manages the following states through React hooks:
|
||||
|
||||
1. **viewMode**: Current view mode ('source' | 'special' | 'split')
|
||||
2. **isRunning**: Python code execution status
|
||||
3. **executionResult**: Python code execution result
|
||||
4. **tools**: Toolbar tool list
|
||||
5. **expandOverride/unwrapOverride**: User override settings for expand/wrap
|
||||
6. **sourceScrollHeight**: Source code view scroll height
|
||||
|
||||
## Interaction Flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant U as User
|
||||
participant CB as CodeBlockView
|
||||
participant CT as CodeToolbar
|
||||
participant SV as SpecialView
|
||||
participant SE as SourceEditor
|
||||
|
||||
U->>CB: View code block
|
||||
CB->>CB: Initialize state
|
||||
CB->>CT: Register tools
|
||||
CB->>SV: Render special view (if applicable)
|
||||
CB->>SE: Render source view
|
||||
U->>CT: Click tool button
|
||||
CT->>CB: Trigger tool callback
|
||||
CB->>CB: Update state
|
||||
CB->>CT: Re-register tools (if needed)
|
||||
```
|
||||
|
||||
## Special Handling
|
||||
|
||||
### HTML Code Blocks
|
||||
|
||||
HTML code blocks are specially handled using the HtmlArtifactsCard component.
|
||||
|
||||
### Python Code Execution
|
||||
|
||||
Supports executing Python code and displaying results using Pyodide to run Python code in the browser.
|
||||
180
docs/technical/CodeBlockView-zh.md
Normal file
180
docs/technical/CodeBlockView-zh.md
Normal file
@ -0,0 +1,180 @@
|
||||
# CodeBlockView 组件结构说明
|
||||
|
||||
## 概述
|
||||
|
||||
CodeBlockView 是 Cherry Studio 中用于显示和操作代码块的核心组件。它支持多种视图模式和特殊语言的可视化预览,提供丰富的交互工具。
|
||||
|
||||
## 组件结构
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[CodeBlockView] --> B[CodeToolbar]
|
||||
A --> C[SourceView]
|
||||
A --> D[SpecialView]
|
||||
A --> E[StatusBar]
|
||||
|
||||
B --> F[CodeToolButton]
|
||||
|
||||
C --> G[CodeEditor / CodeViewer]
|
||||
|
||||
D --> H[MermaidPreview]
|
||||
D --> I[PlantUmlPreview]
|
||||
D --> J[SvgPreview]
|
||||
D --> K[GraphvizPreview]
|
||||
|
||||
F --> L[useCopyTool]
|
||||
F --> M[useDownloadTool]
|
||||
F --> N[useViewSourceTool]
|
||||
F --> O[useSplitViewTool]
|
||||
F --> P[useRunTool]
|
||||
F --> Q[useExpandTool]
|
||||
F --> R[useWrapTool]
|
||||
F --> S[useSaveTool]
|
||||
```
|
||||
|
||||
## 核心概念
|
||||
|
||||
### 视图类型
|
||||
|
||||
- **preview**: 预览视图,非源代码的是特殊视图
|
||||
- **edit**: 编辑视图
|
||||
|
||||
### 视图模式
|
||||
|
||||
- **source**: 源代码视图模式
|
||||
- **special**: 特殊视图模式(Mermaid、PlantUML、SVG)
|
||||
- **split**: 分屏模式(源代码和特殊视图并排显示)
|
||||
|
||||
### 特殊视图语言
|
||||
|
||||
- mermaid
|
||||
- plantuml
|
||||
- svg
|
||||
- dot
|
||||
- graphviz
|
||||
|
||||
## 组件详细说明
|
||||
|
||||
### CodeBlockView 主组件
|
||||
|
||||
主要负责:
|
||||
|
||||
1. 管理视图模式状态
|
||||
2. 协调源代码视图和特殊视图的显示
|
||||
3. 管理工具栏工具
|
||||
4. 处理代码执行状态
|
||||
|
||||
### 子组件
|
||||
|
||||
#### CodeToolbar 工具栏
|
||||
|
||||
- 显示在代码块右上角的工具栏
|
||||
- 包含核心(core)和快捷(quick)两类工具
|
||||
- 根据上下文动态显示相关工具
|
||||
|
||||
#### CodeEditor/CodeViewer 源代码视图
|
||||
|
||||
- 可编辑的代码编辑器或只读的代码查看器
|
||||
- 根据设置决定使用哪个组件
|
||||
- 支持多种编程语言高亮
|
||||
|
||||
#### 特殊视图组件
|
||||
|
||||
- **MermaidPreview**: Mermaid 图表预览
|
||||
- **PlantUmlPreview**: PlantUML 图表预览
|
||||
- **SvgPreview**: SVG 图像预览
|
||||
- **GraphvizPreview**: Graphviz 图表预览
|
||||
|
||||
所有特殊视图组件共享通用架构,以确保一致的用户体验和功能。有关这些组件及其实现的详细信息,请参阅 [图像预览组件文档](./ImagePreview-zh.md)。
|
||||
|
||||
#### StatusBar 状态栏
|
||||
|
||||
- 显示 Python 代码执行结果
|
||||
- 可显示文本和图像结果
|
||||
|
||||
## 工具系统
|
||||
|
||||
CodeBlockView 使用基于 hooks 的工具系统:
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[CodeBlockView] --> B[useCopyTool]
|
||||
A --> C[useDownloadTool]
|
||||
A --> D[useViewSourceTool]
|
||||
A --> E[useSplitViewTool]
|
||||
A --> F[useRunTool]
|
||||
A --> G[useExpandTool]
|
||||
A --> H[useWrapTool]
|
||||
A --> I[useSaveTool]
|
||||
|
||||
B --> J[ToolManager]
|
||||
C --> J
|
||||
D --> J
|
||||
E --> J
|
||||
F --> J
|
||||
G --> J
|
||||
H --> J
|
||||
I --> J
|
||||
|
||||
J --> K[CodeToolbar]
|
||||
```
|
||||
|
||||
每个工具 hook 负责注册特定功能的工具按钮到工具管理器,工具管理器再将这些工具传递给 CodeToolbar 组件进行渲染。
|
||||
|
||||
### 工具类型
|
||||
|
||||
- **core**: 核心工具,始终显示在工具栏
|
||||
- **quick**: 快捷工具,当数量大于1时通过下拉菜单显示
|
||||
|
||||
### 工具列表
|
||||
|
||||
1. **复制(copy)**: 复制代码或图像
|
||||
2. **下载(download)**: 下载代码或图像
|
||||
3. **查看源码(view-source)**: 在特殊视图和源码视图间切换
|
||||
4. **分屏(split-view)**: 切换分屏模式
|
||||
5. **运行(run)**: 运行 Python 代码
|
||||
6. **展开/折叠(expand)**: 控制代码块的展开/折叠
|
||||
7. **换行(wrap)**: 控制代码的自动换行
|
||||
8. **保存(save)**: 保存编辑的代码
|
||||
|
||||
## 状态管理
|
||||
|
||||
CodeBlockView 通过 React hooks 管理以下状态:
|
||||
|
||||
1. **viewMode**: 当前视图模式 ('source' | 'special' | 'split')
|
||||
2. **isRunning**: Python 代码执行状态
|
||||
3. **executionResult**: Python 代码执行结果
|
||||
4. **tools**: 工具栏工具列表
|
||||
5. **expandOverride/unwrapOverride**: 用户展开/换行的覆盖设置
|
||||
6. **sourceScrollHeight**: 源代码视图滚动高度
|
||||
|
||||
## 交互流程
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant U as User
|
||||
participant CB as CodeBlockView
|
||||
participant CT as CodeToolbar
|
||||
participant SV as SpecialView
|
||||
participant SE as SourceEditor
|
||||
|
||||
U->>CB: 查看代码块
|
||||
CB->>CB: 初始化状态
|
||||
CB->>CT: 注册工具
|
||||
CB->>SV: 渲染特殊视图(如果适用)
|
||||
CB->>SE: 渲染源码视图
|
||||
U->>CT: 点击工具按钮
|
||||
CT->>CB: 触发工具回调
|
||||
CB->>CB: 更新状态
|
||||
CB->>CT: 重新注册工具(如果需要)
|
||||
```
|
||||
|
||||
## 特殊处理
|
||||
|
||||
### HTML 代码块
|
||||
|
||||
HTML 代码块会被特殊处理,使用 HtmlArtifactsCard 组件显示。
|
||||
|
||||
### Python 代码执行
|
||||
|
||||
支持执行 Python 代码并显示结果,使用 Pyodide 在浏览器中运行 Python 代码。
|
||||
195
docs/technical/ImagePreview-en.md
Normal file
195
docs/technical/ImagePreview-en.md
Normal file
@ -0,0 +1,195 @@
|
||||
# Image Preview Components
|
||||
|
||||
## Overview
|
||||
|
||||
Image Preview Components are a set of specialized components in Cherry Studio for rendering and displaying various diagram and image formats. They provide a consistent user experience across different preview types with shared functionality for loading states, error handling, and interactive controls.
|
||||
|
||||
## Supported Formats
|
||||
|
||||
- **Mermaid**: Interactive diagrams and flowcharts
|
||||
- **PlantUML**: UML diagrams and system architecture
|
||||
- **SVG**: Scalable vector graphics
|
||||
- **Graphviz/DOT**: Graph visualization and network diagrams
|
||||
|
||||
## Architecture
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[MermaidPreview] --> D[ImagePreviewLayout]
|
||||
B[PlantUmlPreview] --> D
|
||||
C[SvgPreview] --> D
|
||||
E[GraphvizPreview] --> D
|
||||
|
||||
D --> F[ImageToolbar]
|
||||
D --> G[useDebouncedRender]
|
||||
|
||||
F --> H[Pan Controls]
|
||||
F --> I[Zoom Controls]
|
||||
F --> J[Reset Function]
|
||||
F --> K[Dialog Control]
|
||||
|
||||
G --> L[Debounced Rendering]
|
||||
G --> M[Error Handling]
|
||||
G --> N[Loading State]
|
||||
G --> O[Dependency Management]
|
||||
```
|
||||
|
||||
## Core Components
|
||||
|
||||
### ImagePreviewLayout
|
||||
|
||||
A common layout wrapper that provides the foundation for all image preview components.
|
||||
|
||||
**Features:**
|
||||
|
||||
- **Loading State Management**: Shows loading spinner during rendering
|
||||
- **Error Display**: Displays error messages when rendering fails
|
||||
- **Toolbar Integration**: Conditionally renders ImageToolbar when enabled
|
||||
- **Container Management**: Wraps preview content with consistent styling
|
||||
- **Responsive Design**: Adapts to different container sizes
|
||||
|
||||
**Props:**
|
||||
|
||||
- `children`: The preview content to be displayed
|
||||
- `loading`: Boolean indicating if content is being rendered
|
||||
- `error`: Error message to display if rendering fails
|
||||
- `enableToolbar`: Whether to show the interactive toolbar
|
||||
- `imageRef`: Reference to the container element for image manipulation
|
||||
|
||||
### ImageToolbar
|
||||
|
||||
Interactive toolbar component providing image manipulation controls.
|
||||
|
||||
**Features:**
|
||||
|
||||
- **Pan Controls**: 4-directional pan buttons (up, down, left, right)
|
||||
- **Zoom Controls**: Zoom in/out functionality with configurable increments
|
||||
- **Reset Function**: Restore original pan and zoom state
|
||||
- **Dialog Control**: Open preview in expanded dialog view
|
||||
- **Accessible Design**: Full keyboard navigation and screen reader support
|
||||
|
||||
**Layout:**
|
||||
|
||||
- 3x3 grid layout positioned at bottom-right of preview
|
||||
- Responsive button sizing
|
||||
- Tooltip support for all controls
|
||||
|
||||
### useDebouncedRender Hook
|
||||
|
||||
A specialized React hook for managing preview rendering with performance optimizations.
|
||||
|
||||
**Features:**
|
||||
|
||||
- **Debounced Rendering**: Prevents excessive re-renders during rapid content changes (default 300ms delay)
|
||||
- **Automatic Dependency Management**: Handles dependencies for render and condition functions
|
||||
- **Error Handling**: Catches and manages rendering errors with detailed error messages
|
||||
- **Loading State**: Tracks rendering progress with automatic state updates
|
||||
- **Conditional Rendering**: Supports pre-render condition checks
|
||||
- **Manual Controls**: Provides trigger, cancel, and state management functions
|
||||
|
||||
**API:**
|
||||
|
||||
```typescript
|
||||
const { containerRef, error, isLoading, triggerRender, cancelRender, clearError, setLoading } = useDebouncedRender(
|
||||
value,
|
||||
renderFunction,
|
||||
options
|
||||
)
|
||||
```
|
||||
|
||||
**Options:**
|
||||
|
||||
- `debounceDelay`: Customize debounce timing
|
||||
- `shouldRender`: Function for conditional rendering logic
|
||||
|
||||
## Component Implementations
|
||||
|
||||
### MermaidPreview
|
||||
|
||||
Renders Mermaid diagrams with special handling for visibility detection.
|
||||
|
||||
**Special Features:**
|
||||
|
||||
- Syntax validation before rendering
|
||||
- Visibility detection to handle collapsed containers
|
||||
- SVG coordinate fixing for edge cases
|
||||
- Integration with mermaid.js library
|
||||
|
||||
### PlantUmlPreview
|
||||
|
||||
Renders PlantUML diagrams using the online PlantUML server.
|
||||
|
||||
**Special Features:**
|
||||
|
||||
- Network error handling and retry logic
|
||||
- Diagram encoding using deflate compression
|
||||
- Support for light/dark themes
|
||||
- Server status monitoring
|
||||
|
||||
### SvgPreview
|
||||
|
||||
Renders SVG content using Shadow DOM for isolation.
|
||||
|
||||
**Special Features:**
|
||||
|
||||
- Shadow DOM rendering for style isolation
|
||||
- Direct SVG content injection
|
||||
- Minimal processing overhead
|
||||
- Cross-browser compatibility
|
||||
|
||||
### GraphvizPreview
|
||||
|
||||
Renders Graphviz/DOT diagrams using the viz.js library.
|
||||
|
||||
**Special Features:**
|
||||
|
||||
- Client-side rendering with viz.js
|
||||
- Lazy loading of viz.js library
|
||||
- SVG element generation
|
||||
- Memory-efficient processing
|
||||
|
||||
## Shared Functionality
|
||||
|
||||
### Error Handling
|
||||
|
||||
All preview components provide consistent error handling:
|
||||
|
||||
- Network errors (connection failures)
|
||||
- Syntax errors (invalid diagram code)
|
||||
- Server errors (external service failures)
|
||||
- Rendering errors (library failures)
|
||||
|
||||
### Loading States
|
||||
|
||||
Standardized loading indicators across all components:
|
||||
|
||||
- Spinner animation during processing
|
||||
- Progress feedback for long operations
|
||||
- Smooth transitions between states
|
||||
|
||||
### Interactive Controls
|
||||
|
||||
Common interaction patterns:
|
||||
|
||||
- Pan and zoom functionality
|
||||
- Reset to original view
|
||||
- Full-screen dialog mode
|
||||
- Keyboard accessibility
|
||||
|
||||
### Performance Optimizations
|
||||
|
||||
- Debounced rendering to prevent excessive updates
|
||||
- Lazy loading of heavy libraries
|
||||
- Memory management for large diagrams
|
||||
- Efficient re-rendering strategies
|
||||
|
||||
## Integration with CodeBlockView
|
||||
|
||||
Image Preview Components integrate seamlessly with CodeBlockView:
|
||||
|
||||
- Automatic format detection based on language tags
|
||||
- Consistent toolbar integration
|
||||
- Shared state management
|
||||
- Responsive layout adaptation
|
||||
|
||||
For more information about the overall CodeBlockView architecture, see [CodeBlockView Documentation](./CodeBlockView-en.md).
|
||||
195
docs/technical/ImagePreview-zh.md
Normal file
195
docs/technical/ImagePreview-zh.md
Normal file
@ -0,0 +1,195 @@
|
||||
# 图像预览组件
|
||||
|
||||
## 概述
|
||||
|
||||
图像预览组件是 Cherry Studio 中用于渲染和显示各种图表和图像格式的专用组件集合。它们为不同预览类型提供一致的用户体验,具有共享的加载状态、错误处理和交互控制功能。
|
||||
|
||||
## 支持格式
|
||||
|
||||
- **Mermaid**: 交互式图表和流程图
|
||||
- **PlantUML**: UML 图表和系统架构
|
||||
- **SVG**: 可缩放矢量图形
|
||||
- **Graphviz/DOT**: 图形可视化和网络图表
|
||||
|
||||
## 架构
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[MermaidPreview] --> D[ImagePreviewLayout]
|
||||
B[PlantUmlPreview] --> D
|
||||
C[SvgPreview] --> D
|
||||
E[GraphvizPreview] --> D
|
||||
|
||||
D --> F[ImageToolbar]
|
||||
D --> G[useDebouncedRender]
|
||||
|
||||
F --> H[平移控制]
|
||||
F --> I[缩放控制]
|
||||
F --> J[重置功能]
|
||||
F --> K[对话框控制]
|
||||
|
||||
G --> L[防抖渲染]
|
||||
G --> M[错误处理]
|
||||
G --> N[加载状态]
|
||||
G --> O[依赖管理]
|
||||
```
|
||||
|
||||
## 核心组件
|
||||
|
||||
### ImagePreviewLayout 图像预览布局
|
||||
|
||||
为所有图像预览组件提供基础的通用布局包装器。
|
||||
|
||||
**功能特性:**
|
||||
|
||||
- **加载状态管理**: 在渲染期间显示加载动画
|
||||
- **错误显示**: 渲染失败时显示错误信息
|
||||
- **工具栏集成**: 启用时有条件地渲染 ImageToolbar
|
||||
- **容器管理**: 使用一致的样式包装预览内容
|
||||
- **响应式设计**: 适应不同的容器尺寸
|
||||
|
||||
**属性:**
|
||||
|
||||
- `children`: 要显示的预览内容
|
||||
- `loading`: 指示内容是否正在渲染的布尔值
|
||||
- `error`: 渲染失败时显示的错误信息
|
||||
- `enableToolbar`: 是否显示交互式工具栏
|
||||
- `imageRef`: 用于图像操作的容器元素引用
|
||||
|
||||
### ImageToolbar 图像工具栏
|
||||
|
||||
提供图像操作控制的交互式工具栏组件。
|
||||
|
||||
**功能特性:**
|
||||
|
||||
- **平移控制**: 4方向平移按钮(上、下、左、右)
|
||||
- **缩放控制**: 放大/缩小功能,支持可配置的增量
|
||||
- **重置功能**: 恢复原始平移和缩放状态
|
||||
- **对话框控制**: 在展开对话框中打开预览
|
||||
- **无障碍设计**: 完整的键盘导航和屏幕阅读器支持
|
||||
|
||||
**布局:**
|
||||
|
||||
- 3x3 网格布局,位于预览右下角
|
||||
- 响应式按钮尺寸
|
||||
- 所有控件的工具提示支持
|
||||
|
||||
### useDebouncedRender Hook 防抖渲染钩子
|
||||
|
||||
用于管理预览渲染的专用 React Hook,具有性能优化功能。
|
||||
|
||||
**功能特性:**
|
||||
|
||||
- **防抖渲染**: 防止内容快速变化时的过度重新渲染(默认 300ms 延迟)
|
||||
- **自动依赖管理**: 处理渲染和条件函数的依赖项
|
||||
- **错误处理**: 捕获和管理渲染错误,提供详细的错误信息
|
||||
- **加载状态**: 跟踪渲染进度并自动更新状态
|
||||
- **条件渲染**: 支持预渲染条件检查
|
||||
- **手动控制**: 提供触发、取消和状态管理功能
|
||||
|
||||
**API:**
|
||||
|
||||
```typescript
|
||||
const { containerRef, error, isLoading, triggerRender, cancelRender, clearError, setLoading } = useDebouncedRender(
|
||||
value,
|
||||
renderFunction,
|
||||
options
|
||||
)
|
||||
```
|
||||
|
||||
**选项:**
|
||||
|
||||
- `debounceDelay`: 自定义防抖时间
|
||||
- `shouldRender`: 条件渲染逻辑函数
|
||||
|
||||
## 组件实现
|
||||
|
||||
### MermaidPreview Mermaid 预览
|
||||
|
||||
渲染 Mermaid 图表,具有可见性检测的特殊处理。
|
||||
|
||||
**特殊功能:**
|
||||
|
||||
- 渲染前语法验证
|
||||
- 可见性检测以处理折叠的容器
|
||||
- 边缘情况的 SVG 坐标修复
|
||||
- 与 mermaid.js 库集成
|
||||
|
||||
### PlantUmlPreview PlantUML 预览
|
||||
|
||||
使用在线 PlantUML 服务器渲染 PlantUML 图表。
|
||||
|
||||
**特殊功能:**
|
||||
|
||||
- 网络错误处理和重试逻辑
|
||||
- 使用 deflate 压缩的图表编码
|
||||
- 支持明/暗主题
|
||||
- 服务器状态监控
|
||||
|
||||
### SvgPreview SVG 预览
|
||||
|
||||
使用 Shadow DOM 隔离渲染 SVG 内容。
|
||||
|
||||
**特殊功能:**
|
||||
|
||||
- Shadow DOM 渲染实现样式隔离
|
||||
- 直接 SVG 内容注入
|
||||
- 最小化处理开销
|
||||
- 跨浏览器兼容性
|
||||
|
||||
### GraphvizPreview Graphviz 预览
|
||||
|
||||
使用 viz.js 库渲染 Graphviz/DOT 图表。
|
||||
|
||||
**特殊功能:**
|
||||
|
||||
- 使用 viz.js 进行客户端渲染
|
||||
- viz.js 库的懒加载
|
||||
- SVG 元素生成
|
||||
- 内存高效处理
|
||||
|
||||
## 共享功能
|
||||
|
||||
### 错误处理
|
||||
|
||||
所有预览组件提供一致的错误处理:
|
||||
|
||||
- 网络错误(连接失败)
|
||||
- 语法错误(无效的图表代码)
|
||||
- 服务器错误(外部服务失败)
|
||||
- 渲染错误(库失败)
|
||||
|
||||
### 加载状态
|
||||
|
||||
所有组件的标准化加载指示器:
|
||||
|
||||
- 处理期间的动画
|
||||
- 长时间操作的进度反馈
|
||||
- 状态间的平滑过渡
|
||||
|
||||
### 交互控制
|
||||
|
||||
通用交互模式:
|
||||
|
||||
- 平移和缩放功能
|
||||
- 重置到原始视图
|
||||
- 全屏对话框模式
|
||||
- 键盘无障碍访问
|
||||
|
||||
### 性能优化
|
||||
|
||||
- 防抖渲染以防止过度更新
|
||||
- 重型库的懒加载
|
||||
- 大型图表的内存管理
|
||||
- 高效的重新渲染策略
|
||||
|
||||
## 与 CodeBlockView 的集成
|
||||
|
||||
图像预览组件与 CodeBlockView 无缝集成:
|
||||
|
||||
- 基于语言标签的自动格式检测
|
||||
- 一致的工具栏集成
|
||||
- 共享状态管理
|
||||
- 响应式布局适应
|
||||
|
||||
有关整体 CodeBlockView 架构的更多信息,请参阅 [CodeBlockView 文档](./CodeBlockView-zh.md)。
|
||||
127
docs/technical/code-execution.md
Normal file
127
docs/technical/code-execution.md
Normal file
@ -0,0 +1,127 @@
|
||||
# 代码执行功能
|
||||
|
||||
本文档说明了代码块的 Python 代码执行功能。该实现利用 [Pyodide][pyodide-link] 在浏览器环境中直接运行 Python 代码,并将其置于 Web Worker 中,以避免阻塞主 UI 线程。
|
||||
|
||||
整个实现分为三个主要部分:UI 层、服务层和 Worker 层。
|
||||
|
||||
## 执行流程图
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant 用户
|
||||
participant CodeBlockView (UI)
|
||||
participant PyodideService (服务)
|
||||
participant PyodideWorker (Worker)
|
||||
|
||||
用户->>CodeBlockView (UI): 点击“运行”按钮
|
||||
CodeBlockView (UI)->>PyodideService (服务): 调用 runScript(code)
|
||||
PyodideService (服务)->>PyodideWorker (Worker): 发送 postMessage({ id, python: code })
|
||||
PyodideWorker (Worker)->>PyodideWorker (Worker): 加载 Pyodide 和相关包
|
||||
PyodideWorker (Worker)->>PyodideWorker (Worker): (按需)注入垫片并合并代码
|
||||
PyodideWorker (Worker)->>PyodideWorker (Worker): 执行合并后的 Python 代码
|
||||
PyodideWorker (Worker)-->>PyodideService (服务): 返回 postMessage({ id, output })
|
||||
PyodideService (服务)-->>CodeBlockView (UI): 返回 { text, image } 对象
|
||||
CodeBlockView (UI)->>用户: 在状态栏中显示文本和/或图像输出
|
||||
```
|
||||
|
||||
## 1. UI 层
|
||||
|
||||
面向用户的代码执行组件是 [CodeBlockView][codeblock-view-link]。
|
||||
|
||||
### 关键机制:
|
||||
|
||||
- **运行按钮**:当代码块语言为 `python` 且 `codeExecution.enabled` 设置为 true 时,`CodeToolbar` 中会条件性地渲染一个“运行”按钮。
|
||||
- **事件处理**:运行按钮的 `onClick` 事件会触发 `handleRunScript` 函数。
|
||||
- **服务调用**:`handleRunScript` 调用 `pyodideService.runScript(code)`,将代码块中的 Python 代码传递给服务。
|
||||
- **状态管理与输出显示**:使用 `executionResult` 来管理所有执行输出,只要有任何结果(文本或图像),[StatusBar][statusbar-link] 组件就会被渲染以统一显示。
|
||||
|
||||
```typescript
|
||||
// src/renderer/src/components/CodeBlockView/view.tsx
|
||||
const [executionResult, setExecutionResult] = useState<{ text: string; image?: string } | null>(null)
|
||||
|
||||
const handleRunScript = useCallback(() => {
|
||||
setIsRunning(true)
|
||||
setExecutionResult(null)
|
||||
|
||||
pyodideService
|
||||
.runScript(children, {}, codeExecution.timeoutMinutes * 60000)
|
||||
.then((result) => {
|
||||
setExecutionResult(result)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Unexpected error:', error)
|
||||
setExecutionResult({
|
||||
text: `Unexpected error: ${error.message || 'Unknown error'}`
|
||||
})
|
||||
})
|
||||
.finally(() => {
|
||||
setIsRunning(false)
|
||||
})
|
||||
}, [children, codeExecution.timeoutMinutes]);
|
||||
|
||||
// ... 在 JSX 中
|
||||
{isExecutable && executionResult && (
|
||||
<StatusBar>
|
||||
{executionResult.text}
|
||||
{executionResult.image && (
|
||||
<ImageOutput>
|
||||
<img src={executionResult.image} alt="Matplotlib plot" />
|
||||
</ImageOutput>
|
||||
)}
|
||||
</StatusBar>
|
||||
)}
|
||||
```
|
||||
|
||||
## 2. 服务层
|
||||
|
||||
服务层充当 UI 组件和运行 Pyodide 的 Web Worker 之间的桥梁。其逻辑封装在位于单例类 [PyodideService][pyodide-service-link]。
|
||||
|
||||
### 主要职责:
|
||||
|
||||
- **Worker 管理**:初始化、管理并与 Pyodide Web Worker 通信。
|
||||
- **请求处理**:使用 `resolvers` Map 管理并发请求,通过唯一 ID 匹配请求和响应。
|
||||
- **为 UI 提供 API**:向 UI 提供 `runScript(script, context, timeout)` 方法。此方法的返回值已修改为 `Promise<{ text: string; image?: string }>`,以支持包括图像在内的多种输出类型。
|
||||
- **输出处理**:从 Worker 接收包含文本、错误和可选图像数据的 `output` 对象。它将文本和错误格式化为对用户友好的单个字符串,然后连同图像数据一起包装成对象返回给 UI 层。
|
||||
- **IPC 端点**:该服务还提供了一个 `python-execution-request` IPC 端点,允许主进程请求执行 Python 代码,展示了其灵活的架构。
|
||||
|
||||
## 3. Worker 层
|
||||
|
||||
核心的 Python 执行发生在 [pyodide.worker.ts][pyodide-worker-link] 中定义的 Web Worker 内部。这确保了计算密集的 Python 代码不会冻结用户界面。
|
||||
|
||||
### Worker 逻辑:
|
||||
|
||||
- **Pyodide 加载**:Worker 从 CDN 加载 Pyodide 引擎,并设置处理器以捕获 Python 的 `stdout` 和 `stderr`。
|
||||
- **动态包安装**:使用 `pyodide.loadPackagesFromImports()` 自动分析并安装代码中导入的依赖包。
|
||||
- **按需执行垫片代码**:Worker 会检查传入的代码中是否包含 "matplotlib" 字符串。如果是,它会先执行一段 Python“垫片”代码确保图像输出到全局命名空间。
|
||||
- **结果序列化**:执行结果通过 `.toJs()` 等方法被递归转换为可序列化的标准 JavaScript 对象。
|
||||
- **返回结构化输出**:执行后,Worker 将一个包含 `id` 和 `output` 对象的-消息发回服务层。`output` 对象是一个结构化对象,包含 `result`、`text`、`error` 以及一个可选的 `image` 字段(用于 Base64 图像数据)。
|
||||
|
||||
### 数据流
|
||||
|
||||
最终的数据流如下:
|
||||
|
||||
1. **UI 层 ([CodeBlockView][codeblock-view-link])**: 用户点击“运行”按钮。
|
||||
2. **服务层 ([PyodideService][pyodide-service-link])**:
|
||||
- 接收到代码执行请求。
|
||||
- 调用 Web Worker ([pyodide.worker.ts][pyodide-worker-link]),传递用户代码。
|
||||
3. **Worker 层 ([pyodide.worker.ts][pyodide-worker-link])**:
|
||||
- 加载 Pyodide 运行时。
|
||||
- 动态安装代码中 `import` 语句声明的依赖包。
|
||||
- **注入 Matplotlib 垫片**: 如果代码中包含 `matplotlib`,则在用户代码前拼接垫片代码,强制使用 `AGG` 后端。
|
||||
- **执行代码并捕获输出**: 在代码执行后,检查 `matplotlib.pyplot` 的所有 figure,如果存在图像,则将其保存到内存中的 `BytesIO` 对象,并编码为 Base64 字符串。
|
||||
- **结构化返回**: 将捕获的文本输出和 Base64 图像数据封装在一个 JSON 对象中 (`{ "text": "...", "image": "data:image/png;base64,..." }`) 返回给主线程。
|
||||
4. **服务层 ([PyodideService][pyodide-service-link])**:
|
||||
- 接收来自 Worker 的结构化数据。
|
||||
- 将数据原样传递给 UI 层。
|
||||
5. **UI 层 ([CodeBlockView][codeblock-view-link])**:
|
||||
- 接收包含文本和图像数据的对象。
|
||||
- 使用一个 `useState` 来管理执行结果 (`executionResult`)。
|
||||
- 在界面上分别渲染文本输出和图像(如果存在)。
|
||||
|
||||
<!-- Link Definitions -->
|
||||
|
||||
[pyodide-link]: https://pyodide.org/
|
||||
[codeblock-view-link]: /src/renderer/src/components/CodeBlockView/view.tsx
|
||||
[pyodide-service-link]: /src/renderer/src/services/PyodideService.ts
|
||||
[pyodide-worker-link]: /src/renderer/src/workers/pyodide.worker.ts
|
||||
[statusbar-link]: /src/renderer/src/components/CodeBlockView/StatusBar.tsx
|
||||
16
docs/technical/db.translate_languages.md
Normal file
16
docs/technical/db.translate_languages.md
Normal file
@ -0,0 +1,16 @@
|
||||
# `translate_languages` 表技术文档
|
||||
|
||||
## 📄 概述
|
||||
|
||||
`translate_languages` 记录用户自定义的的语言类型(`Language`)。
|
||||
|
||||
### 字段说明
|
||||
|
||||
| 字段名 | 类型 | 是否主键 | 索引 | 说明 |
|
||||
| ---------- | ------ | -------- | ---- | ------------------------------------------------------------------------ |
|
||||
| `id` | string | ✅ 是 | ✅ | 唯一标识符,主键 |
|
||||
| `langCode` | string | ❌ 否 | ✅ | 语言代码(如:`zh-cn`, `en-us`, `ja-jp` 等,均为小写),支持普通索引查询 |
|
||||
| `value` | string | ❌ 否 | ❌ | 语言的名称,用户输入 |
|
||||
| `emoji` | string | ❌ 否 | ❌ | 语言的emoji,用户输入 |
|
||||
|
||||
> `langCode` 虽非主键,但在业务层应当避免重复插入相同语言代码。
|
||||
177
docs/technical/how-to-i18n-en.md
Normal file
177
docs/technical/how-to-i18n-en.md
Normal file
@ -0,0 +1,177 @@
|
||||
# How to Do i18n Gracefully
|
||||
|
||||
> [!WARNING]
|
||||
> This document is machine translated from Chinese. While we strive for accuracy, there may be some imperfections in the translation.
|
||||
|
||||
## Enhance Development Experience with the i18n Ally Plugin
|
||||
|
||||
i18n Ally is a powerful VSCode extension that provides real-time feedback during development, helping developers detect missing or incorrect translations earlier.
|
||||
|
||||
The plugin has already been configured in the project — simply install it to get started.
|
||||
|
||||
### Advantages During Development
|
||||
|
||||
- **Real-time Preview**: Translated texts are displayed directly in the editor.
|
||||
- **Error Detection**: Automatically tracks and highlights missing translations or unused keys.
|
||||
- **Quick Navigation**: Jump to key definitions with Ctrl/Cmd + click.
|
||||
- **Auto-completion**: Provides suggestions when typing i18n keys.
|
||||
|
||||
### Demo
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## i18n Conventions
|
||||
|
||||
### **Avoid Flat Structure at All Costs**
|
||||
|
||||
Never use flat structures like `"add.button.tip": "Add"`. Instead, adopt a clear nested structure:
|
||||
|
||||
```json
|
||||
// Wrong - Flat structure
|
||||
{
|
||||
"add.button.tip": "Add",
|
||||
"delete.button.tip": "Delete"
|
||||
}
|
||||
|
||||
// Correct - Nested structure
|
||||
{
|
||||
"add": {
|
||||
"button": {
|
||||
"tip": "Add"
|
||||
}
|
||||
},
|
||||
"delete": {
|
||||
"button": {
|
||||
"tip": "Delete"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Why Use Nested Structure?
|
||||
|
||||
1. **Natural Grouping**: Related texts are logically grouped by their context through object nesting.
|
||||
2. **Plugin Requirement**: Tools like i18n Ally require either flat or nested format to properly analyze translation files.
|
||||
|
||||
### **Avoid Template Strings in `t()`**
|
||||
|
||||
**We strongly advise against using template strings for dynamic interpolation.** While convenient in general JavaScript development, they cause several issues in i18n scenarios.
|
||||
|
||||
#### 1. **Plugin Cannot Track Dynamic Keys**
|
||||
|
||||
Tools like i18n Ally cannot parse dynamic content within template strings, resulting in:
|
||||
|
||||
- No real-time preview
|
||||
- No detection of missing translations
|
||||
- No navigation to key definitions
|
||||
|
||||
```javascript
|
||||
// Not recommended - Plugin cannot resolve
|
||||
const message = t(`fruits.${fruit}`)
|
||||
```
|
||||
|
||||
#### 2. **No Real-time Rendering in Editor**
|
||||
|
||||
Template strings appear as raw code instead of the final translated text in IDEs, degrading the development experience.
|
||||
|
||||
#### 3. **Harder to Maintain**
|
||||
|
||||
Since the plugin cannot track such usages, developers must manually verify the existence of corresponding keys in language files.
|
||||
|
||||
### Recommended Approach
|
||||
|
||||
To avoid missing keys, all dynamically translated texts should first maintain a `FooKeyMap`, then retrieve the translation text through a function.
|
||||
|
||||
For example:
|
||||
|
||||
```ts
|
||||
// src/renderer/src/i18n/label.ts
|
||||
const themeModeKeyMap = {
|
||||
dark: 'settings.theme.dark',
|
||||
light: 'settings.theme.light',
|
||||
system: 'settings.theme.system'
|
||||
} as const
|
||||
|
||||
export const getThemeModeLabel = (key: string): string => {
|
||||
return themeModeKeyMap[key] ? t(themeModeKeyMap[key]) : key
|
||||
}
|
||||
```
|
||||
|
||||
By avoiding template strings, you gain better developer experience, more reliable translation checks, and a more maintainable codebase.
|
||||
|
||||
## Automation Scripts
|
||||
|
||||
The project includes several scripts to automate i18n-related tasks:
|
||||
|
||||
### `check:i18n` - Validate i18n Structure
|
||||
|
||||
This script checks:
|
||||
|
||||
- Whether all language files use nested structure
|
||||
- For missing or unused keys
|
||||
- Whether keys are properly sorted
|
||||
|
||||
```bash
|
||||
yarn check:i18n
|
||||
```
|
||||
|
||||
### `sync:i18n` - Synchronize JSON Structure and Sort Order
|
||||
|
||||
This script uses `zh-cn.json` as the source of truth to sync structure across all language files, including:
|
||||
|
||||
1. Adding missing keys, with placeholder `[to be translated]`
|
||||
2. Removing obsolete keys
|
||||
3. Sorting keys automatically
|
||||
|
||||
```bash
|
||||
yarn sync:i18n
|
||||
```
|
||||
|
||||
### `auto:i18n` - Automatically Translate Pending Texts
|
||||
|
||||
This script fills in texts marked as `[to be translated]` using machine translation.
|
||||
|
||||
Typically, after adding new texts in `zh-cn.json`, run `sync:i18n`, then `auto:i18n` to complete translations.
|
||||
|
||||
Before using this script, set the required environment variables:
|
||||
|
||||
```bash
|
||||
API_KEY="sk-xxx"
|
||||
BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1/"
|
||||
MODEL="qwen-plus-latest"
|
||||
```
|
||||
|
||||
Alternatively, add these variables directly to your `.env` file.
|
||||
|
||||
```bash
|
||||
yarn auto:i18n
|
||||
```
|
||||
|
||||
### `update:i18n` - Object-level Translation Update
|
||||
|
||||
Updates translations in language files under `src/renderer/src/i18n/translate` at the object level, preserving existing translations and only updating new content.
|
||||
|
||||
**Not recommended** — prefer `auto:i18n` for translation tasks.
|
||||
|
||||
```bash
|
||||
yarn update:i18n
|
||||
```
|
||||
|
||||
### Workflow
|
||||
|
||||
1. During development, first add the required text in `zh-cn.json`
|
||||
2. Confirm it displays correctly in the Chinese environment
|
||||
3. Run `yarn sync:i18n` to propagate the keys to other language files
|
||||
4. Run `yarn auto:i18n` to perform machine translation
|
||||
5. Grab a coffee and let the magic happen!
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use Chinese as Source Language**: All development starts in Chinese, then translates to other languages.
|
||||
2. **Run Check Script Before Commit**: Use `yarn check:i18n` to catch i18n issues early.
|
||||
3. **Translate in Small Increments**: Avoid accumulating a large backlog of untranslated content.
|
||||
4. **Keep Keys Semantically Clear**: Keys should clearly express their purpose, e.g., `user.profile.avatar.upload.error`
|
||||
171
docs/technical/how-to-i18n-zh.md
Normal file
171
docs/technical/how-to-i18n-zh.md
Normal file
@ -0,0 +1,171 @@
|
||||
# 如何优雅地做好 i18n
|
||||
|
||||
## 使用i18n ally插件提升开发体验
|
||||
|
||||
i18n ally是一个强大的VSCode插件,它能在开发阶段提供实时反馈,帮助开发者更早发现文案缺失和错译问题。
|
||||
|
||||
项目中已经配置好了插件设置,直接安装即可。
|
||||
|
||||
### 开发时优势
|
||||
|
||||
- **实时预览**:翻译文案会直接显示在编辑器中
|
||||
- **错误检测**:自动追踪标记出缺失的翻译或未使用的key
|
||||
- **快速跳转**:可通过key直接跳转到定义处(Ctrl/Cmd + click)
|
||||
- **自动补全**:输入i18n key时提供自动补全建议
|
||||
|
||||
### 效果展示
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## i18n 约定
|
||||
|
||||
### **绝对避免使用flat格式**
|
||||
|
||||
绝对避免使用flat格式,如`"add.button.tip": "添加"`。应采用清晰的嵌套结构:
|
||||
|
||||
```json
|
||||
// 错误示例 - flat结构
|
||||
{
|
||||
"add.button.tip": "添加",
|
||||
"delete.button.tip": "删除"
|
||||
}
|
||||
|
||||
// 正确示例 - 嵌套结构
|
||||
{
|
||||
"add": {
|
||||
"button": {
|
||||
"tip": "添加"
|
||||
}
|
||||
},
|
||||
"delete": {
|
||||
"button": {
|
||||
"tip": "删除"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 为什么要使用嵌套结构
|
||||
|
||||
1. **自然分组**:通过对象结构天然能将相关上下文的文案分到一个组别中
|
||||
2. **插件要求**:i18n ally 插件需要嵌套或flat格式其一的文件才能正常分析
|
||||
|
||||
### **避免在`t()`中使用模板字符串**
|
||||
|
||||
**强烈建议避免使用模板字符串**进行动态插值。虽然模板字符串在JavaScript开发中非常方便,但在国际化场景下会带来一系列问题。
|
||||
|
||||
1. **插件无法跟踪**
|
||||
i18n ally等工具无法解析模板字符串中的动态内容,导致:
|
||||
|
||||
- 无法正确显示实时预览
|
||||
- 无法检测翻译缺失
|
||||
- 无法提供跳转到定义的功能
|
||||
|
||||
```javascript
|
||||
// 不推荐 - 插件无法解析
|
||||
const message = t(`fruits.${fruit}`)
|
||||
```
|
||||
|
||||
2. **编辑器无法实时渲染**
|
||||
在IDE中,模板字符串会显示为原始代码而非最终翻译结果,降低了开发体验。
|
||||
|
||||
3. **更难以维护**
|
||||
由于插件无法跟踪这样的文案,编辑器中也无法渲染,开发者必须人工确认语言文件中是否存在相应的文案。
|
||||
|
||||
### 推荐做法
|
||||
|
||||
为了避免键的缺失,所有需要动态翻译的文本都应当先维护一个`FooKeyMap`,再通过函数获取翻译文本。
|
||||
|
||||
例如:
|
||||
|
||||
```ts
|
||||
// src/renderer/src/i18n/label.ts
|
||||
const themeModeKeyMap = {
|
||||
dark: 'settings.theme.dark',
|
||||
light: 'settings.theme.light',
|
||||
system: 'settings.theme.system'
|
||||
} as const
|
||||
|
||||
export const getThemeModeLabel = (key: string): string => {
|
||||
return themeModeKeyMap[key] ? t(themeModeKeyMap[key]) : key
|
||||
}
|
||||
```
|
||||
|
||||
通过避免模板字符串,可以获得更好的开发体验、更可靠的翻译检查以及更易维护的代码库。
|
||||
|
||||
## 自动化脚本
|
||||
|
||||
项目中有一系列脚本来自动化i18n相关任务:
|
||||
|
||||
### `check:i18n` - 检查i18n结构
|
||||
|
||||
此脚本会检查:
|
||||
|
||||
- 所有语言文件是否为嵌套结构
|
||||
- 是否存在缺失的key
|
||||
- 是否存在多余的key
|
||||
- 是否已经有序
|
||||
|
||||
```bash
|
||||
yarn check:i18n
|
||||
```
|
||||
|
||||
### `sync:i18n` - 同步json结构与排序
|
||||
|
||||
此脚本以`zh-cn.json`文件为基准,将结构同步到其他语言文件,包括:
|
||||
|
||||
1. 添加缺失的键。缺少的翻译内容会以`[to be translated]`标记
|
||||
2. 删除多余的键
|
||||
3. 自动排序
|
||||
|
||||
```bash
|
||||
yarn sync:i18n
|
||||
```
|
||||
|
||||
### `auto:i18n` - 自动翻译待翻译文本
|
||||
|
||||
次脚本自动将标记为待翻译的文本通过机器翻译填充。
|
||||
|
||||
通常,在`zh-cn.json`中添加所需文案后,执行`sync:i18n`即可自动完成翻译。
|
||||
|
||||
使用该脚本前,需要配置环境变量,例如:
|
||||
|
||||
```bash
|
||||
API_KEY="sk-xxx"
|
||||
BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1/"
|
||||
MODEL="qwen-plus-latest"
|
||||
```
|
||||
|
||||
你也可以通过直接编辑`.env`文件来添加环境变量。
|
||||
|
||||
```bash
|
||||
yarn auto:i18n
|
||||
```
|
||||
|
||||
### `update:i18n` - 对象级别翻译更新
|
||||
|
||||
对`src/renderer/src/i18n/translate`中的语言文件进行对象级别的翻译更新,保留已有翻译,只更新新增内容。
|
||||
|
||||
**不建议**使用该脚本,更推荐使用`auto:i18n`进行翻译。
|
||||
|
||||
```bash
|
||||
yarn update:i18n
|
||||
```
|
||||
|
||||
### 工作流
|
||||
|
||||
1. 开发阶段,先在`zh-cn.json`中添加所需文案
|
||||
2. 确认在中文环境下显示无误后,使用`yarn sync:i18n`将文案同步到其他语言文件
|
||||
3. 使用`yarn auto:i18n`进行自动翻译
|
||||
4. 喝杯咖啡,等翻译完成吧!
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **以中文为源语言**:所有开发首先使用中文,再翻译为其他语言
|
||||
2. **提交前运行检查脚本**:使用`yarn check:i18n`检查i18n是否有问题
|
||||
3. **小步提交翻译**:避免积累大量未翻译文本
|
||||
4. **保持key语义明确**:key应能清晰表达其用途,如`user.profile.avatar.upload.error`
|
||||
191
docs/technical/how-to-use-logger-en.md
Normal file
191
docs/technical/how-to-use-logger-en.md
Normal file
@ -0,0 +1,191 @@
|
||||
# How to use the LoggerService
|
||||
|
||||
This is a developer document on how to use the logger.
|
||||
|
||||
CherryStudio uses a unified logging service to print and record logs. **Unless there is a special reason, do not use `console.xxx` to print logs**.
|
||||
|
||||
The following are detailed instructions.
|
||||
|
||||
## Usage in the `main` process
|
||||
|
||||
### Importing
|
||||
|
||||
```typescript
|
||||
import { loggerService } from '@logger'
|
||||
```
|
||||
|
||||
### Setting module information (Required by convention)
|
||||
|
||||
After the import statements, set it up as follows:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.withContext('moduleName')
|
||||
```
|
||||
|
||||
- `moduleName` is the name of the current file's module. It can be named after the filename, main class name, main function name, etc. The principle is to be clear and understandable.
|
||||
- `moduleName` will be printed in the terminal and will also be present in the file log, making it easier to filter.
|
||||
|
||||
### Setting `CONTEXT` information (Optional)
|
||||
|
||||
In `withContext`, you can also set other `CONTEXT` information:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.withContext('moduleName', CONTEXT)
|
||||
```
|
||||
|
||||
- `CONTEXT` is an object of the form `{ key: value, ... }`.
|
||||
- `CONTEXT` information will not be printed in the terminal, but it will be recorded in the file log, making it easier to filter.
|
||||
|
||||
### Logging
|
||||
|
||||
In your code, you can call `logger` at any time to record logs. The supported levels are: `error`, `warn`, `info`, `verbose`, `debug`, and `silly`.
|
||||
For the meaning of each level, please refer to the subsequent sections.
|
||||
|
||||
The following are the supported parameters for logging (using `logger.LEVEL` as an example, where `LEVEL` represents one of the levels mentioned above):
|
||||
|
||||
```typescript
|
||||
logger.LEVEL(message)
|
||||
logger.LEVEL(message, CONTEXT)
|
||||
logger.LEVEL(message, error)
|
||||
logger.LEVEL(message, error, CONTEXT)
|
||||
```
|
||||
|
||||
**Only the four calling methods above are supported**.
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `message` | `string` | Required. This is the core field of the log, containing the main content to be recorded. |
|
||||
| `CONTEXT` | `object` | Optional. Additional information to be recorded in the log file. It is recommended to use the `{ key: value, ...}` format. |
|
||||
| `error` | `Error` | Optional. The error stack trace will also be printed.<br />Note that the `error` caught by `catch(error)` is of the `unknown` type. According to TypeScript best practices, you should first use `instanceof` for type checking. If you are certain it is an `Error` type, you can also use a type assertion like `as Error`. |
|
||||
|
||||
#### Recording non-`object` type context information
|
||||
|
||||
```typescript
|
||||
const foo = getFoo()
|
||||
logger.debug(`foo ${foo}`)
|
||||
```
|
||||
|
||||
### Log Levels
|
||||
|
||||
- In the development environment, all log levels are printed to the terminal and recorded in the file log.
|
||||
- In the production environment, the default log level is `info`. Logs are only recorded to the file and are not printed to the terminal.
|
||||
|
||||
Changing the log level:
|
||||
|
||||
- You can change the log level with `logger.setLevel('newLevel')`.
|
||||
- `logger.resetLevel()` resets it to the default level.
|
||||
- `logger.getLevel()` gets the current log level.
|
||||
|
||||
**Note:** Changing the log level has a global effect. Please do not change it arbitrarily in your code unless you are very clear about what you are doing.
|
||||
|
||||
## Usage in the `renderer` process
|
||||
|
||||
Usage in the `renderer` process for _importing_, _setting module information_, and _setting context information_ is **exactly the same** as in the `main` process.
|
||||
The following section focuses on the differences.
|
||||
|
||||
### `initWindowSource`
|
||||
|
||||
In the `renderer` process, there are different `window`s. Before starting to use the `logger`, we must set the `window` information:
|
||||
|
||||
```typescript
|
||||
loggerService.initWindowSource('windowName')
|
||||
```
|
||||
|
||||
As a rule, we will set this in the `window`'s `entryPoint.tsx`. This ensures that `windowName` is set before it's used.
|
||||
|
||||
- An error will be thrown if `windowName` is not set, and the `logger` will not work.
|
||||
- `windowName` can only be set once; subsequent attempts to set it will have no effect.
|
||||
- `windowName` will not be printed in the `devTool`'s `console`, but it will be recorded in the `main` process terminal and the file log.
|
||||
- `initWindowSource` returns the LoggerService instance, allowing for method chaining.
|
||||
|
||||
### Log Levels
|
||||
|
||||
- In the development environment, all log levels are printed to the `devTool`'s `console` by default.
|
||||
- In the production environment, the default log level is `info`, and logs are printed to the `devTool`'s `console`.
|
||||
- In both development and production environments, `warn` and `error` level logs are, by default, transmitted to the `main` process and recorded in the file log.
|
||||
- In the development environment, the `main` process terminal will also print the logs transmitted from the renderer.
|
||||
|
||||
#### Changing the Log Level
|
||||
|
||||
Same as in the `main` process, you can manage the log level using `setLevel('level')`, `resetLevel()`, and `getLevel()`.
|
||||
Similarly, changing the log level is a global adjustment.
|
||||
|
||||
#### Changing the Level Transmitted to `main`
|
||||
|
||||
Logs from the `renderer` are sent to `main` to be managed and recorded to a file centrally (according to `main`'s file logging level). By default, only `warn` and `error` level logs are transmitted to `main`.
|
||||
|
||||
There are two ways to change the log level for transmission to `main`:
|
||||
|
||||
##### Global Change
|
||||
|
||||
The following methods can be used to set, reset, and get the log level for transmission to `main`, respectively.
|
||||
|
||||
```typescript
|
||||
logger.setLogToMainLevel('newLevel')
|
||||
logger.resetLogToMainLevel()
|
||||
logger.getLogToMainLevel()
|
||||
```
|
||||
|
||||
**Note:** This method has a global effect. Please do not change it arbitrarily in your code unless you are very clear about what you are doing.
|
||||
|
||||
##### Per-log Change
|
||||
|
||||
By adding `{ logToMain: true }` at the end of the log call, you can force a single log entry to be transmitted to `main` (bypassing the global log level restriction), for example:
|
||||
|
||||
```typescript
|
||||
logger.info('message', { logToMain: true })
|
||||
```
|
||||
|
||||
## About `worker` Threads
|
||||
|
||||
- Currently, logging is not supported for workers in the `main` process.
|
||||
- Logging is supported for workers started in the `renderer` process, but currently these logs are not sent to `main` for recording.
|
||||
|
||||
### How to Use Logging in `renderer` Workers
|
||||
|
||||
Since worker threads are independent, using LoggerService in them is equivalent to using it in a new `renderer` window. Therefore, you must first call `initWindowSource`.
|
||||
|
||||
If the worker is relatively simple (just one file), you can also use method chaining directly:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.initWindowSource('Worker').withContext('LetsWork')
|
||||
```
|
||||
|
||||
## Filtering Logs with Environment Variables
|
||||
|
||||
In a development environment, you can define environment variables to filter displayed logs by level and module. This helps developers focus on their specific logs and improves development efficiency.
|
||||
|
||||
Environment variables can be set in the terminal or defined in the `.env` file in the project's root directory. The available variables are as follows:
|
||||
|
||||
| Variable Name | Description |
|
||||
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `CSLOGGER_MAIN_LEVEL` | Log level for the `main` process. Logs below this level will not be displayed. |
|
||||
| `CSLOGGER_MAIN_SHOW_MODULES` | Filters log modules for the `main` process. Use a comma (`,`) to separate modules. The filter is case-sensitive. Only logs from modules in this list will be displayed. |
|
||||
| `CSLOGGER_RENDERER_LEVEL` | Log level for the `renderer` process. Logs below this level will not be displayed. |
|
||||
| `CSLOGGER_RENDERER_SHOW_MODULES` | Filters log modules for the `renderer` process. Use a comma (`,`) to separate modules. The filter is case-sensitive. Only logs from modules in this list will be displayed. |
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
CSLOGGER_MAIN_LEVEL=verbose
|
||||
CSLOGGER_MAIN_SHOW_MODULES=MCPService,SelectionService
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
- Environment variables are only effective in the development environment.
|
||||
- These variables only affect the logs displayed in the terminal or DevTools. They do not affect file logging or the `logToMain` recording logic.
|
||||
|
||||
## Log Level Usage Guidelines
|
||||
|
||||
There are many log levels. The following are the guidelines that should be followed in CherryStudio for when to use each level:
|
||||
(Arranged from highest to lowest log level)
|
||||
|
||||
| Log Level | Core Definition & Use case | Example |
|
||||
| :------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **`error`** | **Critical error causing the program to crash or core functionality to become unusable.** <br> This is the highest-priority log, usually requiring immediate reporting or user notification. | - Main or renderer process crash. <br> - Failure to read/write critical user data files (e.g., database, configuration files), preventing the application from running. <br> - All unhandled exceptions. |
|
||||
| **`warn`** | **Potential issue or unexpected situation that does not affect the program's core functionality.** <br> The program can recover or use a fallback. | - Configuration file `settings.json` is missing; started with default settings. <br> - Auto-update check failed, but does not affect the use of the current version. <br> - A non-essential plugin failed to load. |
|
||||
| **`info`** | **Records application lifecycle events and key user actions.** <br> This is the default level that should be recorded in a production release to trace the user's main operational path. | - Application start, exit. <br> - User successfully opens/saves a file. <br> - Main window created/closed. <br> - Starting an important task (e.g., "Start video export"). |
|
||||
| **`verbose`** | **More detailed flow information than `info`, used for tracing specific features.** <br> Enabled when diagnosing issues with a specific feature to help understand the internal execution flow. | - Loading `Toolbar` module. <br> - IPC message `open-file-dialog` sent from the renderer process. <br> - Applying filter 'Sepia' to the image. |
|
||||
| **`debug`** | **Detailed diagnostic information used during development and debugging.** <br> **Must not be enabled by default in production releases**, as it may contain sensitive data and impact performance. | - Parameters for function `renderImage`: `{ width: 800, ... }`. <br> - Specific data content received by IPC message `save-file`. <br> - Details of Redux/Vuex state changes in the renderer process. |
|
||||
| **`silly`** | **The most detailed, low-level information, used only for extreme debugging.** <br> Rarely used in regular development; only for solving very difficult problems. | - Real-time mouse coordinates `(x: 150, y: 320)`. <br> - Size of each data chunk when reading a file. <br> - Time taken for each rendered frame. |
|
||||
194
docs/technical/how-to-use-logger-zh.md
Normal file
194
docs/technical/how-to-use-logger-zh.md
Normal file
@ -0,0 +1,194 @@
|
||||
# 如何使用日志 LoggerService
|
||||
|
||||
这是关于如何使用日志的开发者文档。
|
||||
|
||||
CherryStudio使用统一的日志服务来打印和记录日志,**若无特殊原因,请勿使用`console.xxx`来打印日志**。
|
||||
|
||||
以下是详细说明。
|
||||
|
||||
## 在`main`进程中使用
|
||||
|
||||
### 引入
|
||||
|
||||
```typescript
|
||||
import { loggerService } from '@logger'
|
||||
```
|
||||
|
||||
### 设置module信息(规范要求)
|
||||
|
||||
在import头之后,设置:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.withContext('moduleName')
|
||||
```
|
||||
|
||||
- `moduleName`是当前文件模块的名称,命名可以以文件名、主类名、主函数名等,原则是清晰明了
|
||||
- `moduleName`会在终端中打印出来,也会在文件日志中体现,方便筛选
|
||||
|
||||
### 设置`CONTEXT`信息(可选)
|
||||
|
||||
在`withContext`中,也可以设置其他`CONTEXT`信息:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.withContext('moduleName', CONTEXT)
|
||||
```
|
||||
|
||||
- `CONTEXT`为`{ key: value, ... }`
|
||||
- `CONTEXT`信息不会在终端中打印出来,但是会在文件日志中记录,方便筛选
|
||||
|
||||
### 记录日志
|
||||
|
||||
在代码中,可以随时调用 `logger` 来记录日志,支持的级别有:`error`, `warn`, `info`, `verbose`, `debug`, `silly`。
|
||||
|
||||
各级别的含义,请参考后面的章节。
|
||||
|
||||
以下支持的记录日志的参数(以 `logger.LEVEL` 举例如何使用,`LEVEL`指代为上述级别):
|
||||
|
||||
```typescript
|
||||
logger.LEVEL(message)
|
||||
logger.LEVEL(message, CONTEXT)
|
||||
logger.LEVEL(message, error)
|
||||
logger.LEVEL(message, error, CONTEXT)
|
||||
```
|
||||
|
||||
**只支持上述四种调用方式**。
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `message` | `string` | 必填项。这是日志的核心字段,记录的重点内容 |
|
||||
| `CONTEXT` | `object` | 可选。其他需要再日志文件中记录的信息,建议为`{ key: value, ...}`格式 |
|
||||
| `error` | `Error` | 可选。同时会打印错误堆栈信息。<br />注意`catch(error)`所捕获的`error`是`unknown`类型,按照`Typescript`最佳实践,请先用`instanceof`进行类型判断,如果确信一定是`Error`类型,也可用断言`as Error`。 |
|
||||
|
||||
#### 记录非`object`类型的上下文信息
|
||||
|
||||
```typescript
|
||||
const foo = getFoo()
|
||||
logger.debug(`foo ${foo}`)
|
||||
```
|
||||
|
||||
### 记录级别
|
||||
|
||||
- 开发环境下,所有级别的日志都会打印到终端,并且记录到文件日志中
|
||||
- 生产环境下,默认记录级别为`info`,日志只会记录到文件,不会打印到终端
|
||||
|
||||
更改日志记录级别:
|
||||
|
||||
- 可以通过 `logger.setLevel('newLevel')` 来更改日志记录级别
|
||||
- `logger.resetLevel()` 可以重置为默认级别
|
||||
- `logger.getLevel()` 可以获取当前记录记录级别
|
||||
|
||||
**注意** 更改日志记录级别是全局生效的,请不要在代码中随意更改,除非你非常清楚自己在做什么
|
||||
|
||||
## 在`renderer`进程中使用
|
||||
|
||||
在`renderer`进程中使用,_引入方法_、_设置`module`信息_、*设置`context`信息的方法*和`main`进程中是**完全一样**的。
|
||||
|
||||
下面着重讲一下不同之处。
|
||||
|
||||
### `initWindowSource`
|
||||
|
||||
`renderer`进程中,有不同的`window`,在开始使用`logger`之前,我们必须设置`window`信息:
|
||||
|
||||
```typescript
|
||||
loggerService.initWindowSource('windowName')
|
||||
```
|
||||
|
||||
原则上,我们将在`window`的`entryPoint.tsx`中进行设置,这可以保证`windowName`在开始使用前已经设置好了。
|
||||
|
||||
- 未设置`windowName`会报错,`logger`将不起作用
|
||||
- `windowName`只能设置一次,重复设置将不生效
|
||||
- `windowName`不会在`devTool`的`console`中打印出来,但是会在`main`进程的终端和文件日志中记录
|
||||
- `initWindowSource`返回的是LoggerService的实例,因此可以做链式调用
|
||||
|
||||
### 记录级别
|
||||
|
||||
- 开发环境下,默认所有级别的日志都会打印到`devTool`的`console`
|
||||
- 生产环境下,默认记录级别为`info`,日志会打印到`devTool`的`console`
|
||||
- 在开发和生产环境下,默认`warn`和`error`级别的日志,会传输给`main`进程,并记录到文件日志
|
||||
- 开发环境下,`main`进程终端中也会打印传输过来的日志
|
||||
|
||||
#### 更改日志记录级别
|
||||
|
||||
和`main`进程中一样,你可以通过`setLevel('level')`、`resetLevel()`和`getLevel()`来管理日志记录级别。
|
||||
|
||||
同样,该日志记录级别也是全局调整的。
|
||||
|
||||
#### 更改传输到`main`的级别
|
||||
|
||||
将`renderer`的日志发送到`main`,并由`main`统一管理和记录到文件(根据`main`的记录到文件的级别),默认只有`warn`和`error`级别的日志会传输到`main`
|
||||
|
||||
有以下两种方式,可以更改传输到`main`的日志级别:
|
||||
|
||||
##### 全局更改
|
||||
|
||||
以下方法可以分别设置、重置和获取传输到`main`的日志级别
|
||||
|
||||
```typescript
|
||||
logger.setLogToMainLevel('newLevel')
|
||||
logger.resetLogToMainLevel()
|
||||
logger.getLogToMainLevel()
|
||||
```
|
||||
|
||||
**注意** 该方法是全局生效的,请不要在代码中随意更改,除非你非常清楚自己在做什么
|
||||
|
||||
##### 单条更改
|
||||
|
||||
在日志记录的最末尾,加上`{ logToMain: true }`,即可将本条日志传输到`main`(不受全局日志级别限制),例如:
|
||||
|
||||
```typescript
|
||||
logger.info('message', { logToMain: true })
|
||||
```
|
||||
|
||||
## 关于`worker`线程
|
||||
|
||||
- 现在不支持`main`进程中的`worker`的日志。
|
||||
- 支持`renderer`中起的`worker`的日志,但是现在该日志不会发送给`main`进行记录。
|
||||
|
||||
### 如何在`renderer`的`worker`中使用日志
|
||||
|
||||
由于`worker`线程是独立的,在其中使用LoggerService,等同于在一个新`renderer`窗口中使用。因此也必须先`initWindowSource`。
|
||||
|
||||
如果`worker`比较简单,只有一个文件,也可以使用链式语法直接使用:
|
||||
|
||||
```typescript
|
||||
const logger = loggerService.initWindowSource('Worker').withContext('LetsWork')
|
||||
```
|
||||
|
||||
## 使用环境变量来筛选要显示的日志
|
||||
|
||||
在开发环境中,可以通过环境变量的定义,来筛选要显示的日志的级别和module。开发者可以专注于自己的日志,提高开发效率。
|
||||
|
||||
环境变量可以在终端中自行设置,或者在开发根目录的`.env`文件中进行定义,可以定义的变量如下:
|
||||
|
||||
| 变量名 | 含义 |
|
||||
| -------------------------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `CSLOGGER_MAIN_LEVEL` | 用于`main`进程的日志级别,低于该级别的日志将不显示 |
|
||||
| `CSLOGGER_MAIN_SHOW_MODULES` | 用于`main`进程的日志module筛选,用`,`分隔,区分大小写。只有在该列表中的module的日志才会显示 |
|
||||
| `CSLOGGER_RENDERER_LEVEL` | 用于`renderer`进程的日志级别,低于该级别的日志将不显示 |
|
||||
| `CSLOGGER_RENDERER_SHOW_MODULES` | 用于`renderer`进程的日志module筛选,用`,`分隔,区分大小写。只有在该列表中的module的日志才会显示 |
|
||||
|
||||
示例:
|
||||
|
||||
```bash
|
||||
CSLOGGER_MAIN_LEVEL=verbose
|
||||
CSLOGGER_MAIN_SHOW_MODULES=MCPService,SelectionService
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 环境变量仅在开发环境中生效
|
||||
- 该变量仅会改变在终端或在devTools中显示的日志,不会影响文件日志和`logToMain`的记录逻辑
|
||||
|
||||
## 日志级别的使用规范
|
||||
|
||||
日志有很多级别,什么时候应该用哪个级别,下面是在CherryStudio中应该遵循的规范:
|
||||
(按日志级别从高到低排列)
|
||||
|
||||
| 日志级别 | 核心定义与使用场景 | 示例 |
|
||||
| :------------ | :------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **`error`** | **严重错误,导致程序崩溃或核心功能无法使用。** <br> 这是最高优的日志,通常需要立即上报或提示用户。 | - 主进程或渲染进程崩溃。 <br> - 无法读写用户关键数据文件(如数据库、配置文件),导致应用无法运行。<br> - 所有未捕获的异常。 |
|
||||
| **`warn`** | **潜在问题或非预期情况,但不影响程序核心功能。** <br> 程序可以从中恢复或使用备用方案。 | - 配置文件 `settings.json` 缺失,已使用默认配置启动。 <br> - 自动更新检查失败,但不影响当前版本使用。<br> - 某个非核心插件加载失败。 |
|
||||
| **`info`** | **记录应用生命周期和关键用户行为。** <br> 这是发布版中默认应记录的级别,用于追踪用户的主要操作路径。 | - 应用启动、退出。<br> - 用户成功打开/保存文件。 <br> - 主窗口创建/关闭。<br> - 开始执行一项重要任务(如“开始导出视频”)。 |
|
||||
| **`verbose`** | **比 `info` 更详细的流程信息,用于追踪特定功能。** <br> 在诊断特定功能问题时开启,帮助理解内部执行流程。 | - 正在加载 `Toolbar` 模块。 <br> - IPC 消息 `open-file-dialog` 已从渲染进程发送。<br> - 正在应用滤镜 'Sepia' 到图像。 |
|
||||
| **`debug`** | **开发和调试时使用的详细诊断信息。** <br> **严禁在发布版中默认开启**,因为它可能包含敏感数据并影响性能。 | - 函数 `renderImage` 的入参: `{ width: 800, ... }`。<br> - IPC 消息 `save-file` 收到的具体数据内容。<br> - 渲染进程中 Redux/Vuex 的 state 变更详情。 |
|
||||
| **`silly`** | **最详尽的底层信息,仅用于极限调试。** <br> 几乎不在常规开发中使用,仅为解决棘手问题。 | - 鼠标移动的实时坐标 `(x: 150, y: 320)`。<br> - 读取文件时每个数据块(chunk)的大小。<br> - 每一次渲染帧的耗时。 |
|
||||
@ -80,15 +80,13 @@ import { ChunkType } from '@renderer/types' // 调整路径
|
||||
|
||||
export const createSimpleLoggingMiddleware = (): CompletionsMiddleware => {
|
||||
return (api: MiddlewareAPI<AiProviderMiddlewareCompletionsContext, [CompletionsParams]>) => {
|
||||
// console.log(`[LoggingMiddleware] Initialized for provider: ${api.getProviderId()}`);
|
||||
|
||||
return (next: (context: AiProviderMiddlewareCompletionsContext, params: CompletionsParams) => Promise<any>) => {
|
||||
return async (context: AiProviderMiddlewareCompletionsContext, params: CompletionsParams): Promise<void> => {
|
||||
const startTime = Date.now()
|
||||
// 从 context 中获取 onChunk (它最初来自 params.onChunk)
|
||||
const onChunk = context.onChunk
|
||||
|
||||
console.log(
|
||||
logger.debug(
|
||||
`[LoggingMiddleware] Request for ${context.methodName} with params:`,
|
||||
params.messages?.[params.messages.length - 1]?.content
|
||||
)
|
||||
@ -104,14 +102,14 @@ export const createSimpleLoggingMiddleware = (): CompletionsMiddleware => {
|
||||
// 如果在之前,那么它需要自己处理 rawSdkResponse 或确保下游会处理。
|
||||
|
||||
const duration = Date.now() - startTime
|
||||
console.log(`[LoggingMiddleware] Request for ${context.methodName} completed in ${duration}ms.`)
|
||||
logger.debug(`[LoggingMiddleware] Request for ${context.methodName} completed in ${duration}ms.`)
|
||||
|
||||
// 假设下游已经通过 onChunk 发送了所有数据。
|
||||
// 如果这个中间件是链的末端,并且需要确保 BLOCK_COMPLETE 被发送,
|
||||
// 它可能需要更复杂的逻辑来跟踪何时所有数据都已发送。
|
||||
} catch (error) {
|
||||
const duration = Date.now() - startTime
|
||||
console.error(`[LoggingMiddleware] Request for ${context.methodName} failed after ${duration}ms:`, error)
|
||||
logger.error(`[LoggingMiddleware] Request for ${context.methodName} failed after ${duration}ms:`, error)
|
||||
|
||||
// 如果 onChunk 可用,可以尝试发送一个错误块
|
||||
if (onChunk) {
|
||||
@ -207,7 +205,7 @@ export default middlewareConfig
|
||||
|
||||
### 调试技巧
|
||||
|
||||
- 在中间件的关键点使用 `console.log` 或调试器来检查 `params`、`context` 的状态以及 `next` 的返回值。
|
||||
- 在中间件的关键点使用 `logger.debug` 或调试器来检查 `params`、`context` 的状态以及 `next` 的返回值。
|
||||
- 暂时简化中间件链,只保留你正在调试的中间件和最简单的核心逻辑,以隔离问题。
|
||||
- 编写单元测试来独立验证每个中间件的行为。
|
||||
|
||||
|
||||
@ -53,14 +53,16 @@ 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
|
||||
- '!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
|
||||
- '!node_modules/tesseract.js-core/{tesseract-core.js,tesseract-core.wasm,tesseract-core.wasm.js}' # we don't need source files
|
||||
- '!node_modules/tesseract.js-core/{tesseract-core-lstm.js,tesseract-core-lstm.wasm,tesseract-core-lstm.wasm.js}' # we don't need source files
|
||||
- '!node_modules/tesseract.js-core/{tesseract-core-simd-lstm.js,tesseract-core-simd-lstm.wasm,tesseract-core-simd-lstm.wasm.js}' # 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}'
|
||||
- 'node_modules/@img/sharp-libvips-*/**'
|
||||
win:
|
||||
executableName: Cherry Studio
|
||||
artifactName: ${productName}-${version}-${arch}-setup.${ext}
|
||||
@ -100,6 +102,7 @@ linux:
|
||||
target:
|
||||
- target: AppImage
|
||||
- target: deb
|
||||
- target: rpm
|
||||
maintainer: electronjs.org
|
||||
category: Utility
|
||||
desktop:
|
||||
@ -112,13 +115,30 @@ publish:
|
||||
url: https://releases.cherry-ai.com
|
||||
electronDownload:
|
||||
mirror: https://npmmirror.com/mirrors/electron/
|
||||
beforePack: scripts/before-pack.js
|
||||
afterPack: scripts/after-pack.js
|
||||
afterSign: scripts/notarize.js
|
||||
artifactBuildCompleted: scripts/artifact-build-completed.js
|
||||
releaseInfo:
|
||||
releaseNotes: |
|
||||
• [新增] MCP 工具调用自动审批流程
|
||||
• [优化] 输入框快捷弹窗多选交互支持
|
||||
• [新增] 网页内容生成实时预览功能
|
||||
• [支持] Grok-4 大语言模型接入
|
||||
• [修复] Anthropic 模型输出截断缺陷
|
||||
✨ 重要更新:
|
||||
- 新增笔记模块,支持富文本编辑和管理
|
||||
- 内置 GLM-4.5-Flash 免费模型(由智谱开放平台提供)
|
||||
- 内置 Qwen3-8B 免费模型(由硅基流动提供)
|
||||
- 新增 Nano Banana(Gemini 2.5 Flash Image)模型支持
|
||||
- 新增系统 OCR 功能 (macOS & Windows)
|
||||
- 新增图片 OCR 识别和翻译功能
|
||||
- 模型切换支持通过标签筛选
|
||||
- 翻译功能增强:历史搜索和收藏
|
||||
|
||||
🔧 性能优化:
|
||||
- 优化历史页面搜索性能
|
||||
- 优化拖拽列表组件交互
|
||||
- 升级 Electron 到 37.4.0
|
||||
|
||||
🐛 修复问题:
|
||||
- 修复知识库加密 PDF 文档处理
|
||||
- 修复导航栏在左侧时笔记侧边栏按钮缺失
|
||||
- 修复多个模型兼容性问题
|
||||
- 修复 MCP 相关问题
|
||||
- 其他稳定性改进
|
||||
|
||||
@ -4,6 +4,8 @@ import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
|
||||
import { resolve } from 'path'
|
||||
import { visualizer } from 'rollup-plugin-visualizer'
|
||||
|
||||
import pkg from './package.json' assert { type: 'json' }
|
||||
|
||||
const visualizerPlugin = (type: 'renderer' | 'main') => {
|
||||
return process.env[`VISUALIZER_${type.toUpperCase()}`] ? [visualizer({ open: true })] : []
|
||||
}
|
||||
@ -18,12 +20,15 @@ export default defineConfig({
|
||||
alias: {
|
||||
'@main': resolve('src/main'),
|
||||
'@types': resolve('src/renderer/src/types'),
|
||||
'@shared': resolve('packages/shared')
|
||||
'@shared': resolve('packages/shared'),
|
||||
'@logger': resolve('src/main/services/LoggerService'),
|
||||
'@mcp-trace/trace-core': resolve('packages/mcp-trace/trace-core'),
|
||||
'@mcp-trace/trace-node': resolve('packages/mcp-trace/trace-node')
|
||||
}
|
||||
},
|
||||
build: {
|
||||
rollupOptions: {
|
||||
external: ['@libsql/client', 'bufferutil', 'utf-8-validate', '@cherrystudio/mac-system-ocr'],
|
||||
external: ['bufferutil', 'utf-8-validate', 'electron', ...Object.keys(pkg.dependencies)],
|
||||
output: {
|
||||
manualChunks: undefined, // 彻底禁用代码分割 - 返回 null 强制单文件打包
|
||||
inlineDynamicImports: true // 内联所有动态导入,这是关键配置
|
||||
@ -37,10 +42,16 @@ export default defineConfig({
|
||||
}
|
||||
},
|
||||
preload: {
|
||||
plugins: [externalizeDepsPlugin()],
|
||||
plugins: [
|
||||
react({
|
||||
tsDecorators: true
|
||||
}),
|
||||
externalizeDepsPlugin()
|
||||
],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@shared': resolve('packages/shared')
|
||||
'@shared': resolve('packages/shared'),
|
||||
'@mcp-trace/trace-core': resolve('packages/mcp-trace/trace-core')
|
||||
}
|
||||
},
|
||||
build: {
|
||||
@ -50,6 +61,7 @@ export default defineConfig({
|
||||
renderer: {
|
||||
plugins: [
|
||||
react({
|
||||
tsDecorators: true,
|
||||
plugins: [
|
||||
[
|
||||
'@swc/plugin-styled-components',
|
||||
@ -68,7 +80,11 @@ export default defineConfig({
|
||||
resolve: {
|
||||
alias: {
|
||||
'@renderer': resolve('src/renderer/src'),
|
||||
'@shared': resolve('packages/shared')
|
||||
'@shared': resolve('packages/shared'),
|
||||
'@logger': resolve('src/renderer/src/services/LoggerService'),
|
||||
'@mcp-trace/trace-core': resolve('packages/mcp-trace/trace-core'),
|
||||
'@mcp-trace/trace-web': resolve('packages/mcp-trace/trace-web'),
|
||||
'@cherrystudio/extension-table-plus': resolve('packages/extension-table-plus/src')
|
||||
}
|
||||
},
|
||||
optimizeDeps: {
|
||||
@ -87,7 +103,8 @@ export default defineConfig({
|
||||
index: resolve(__dirname, 'src/renderer/index.html'),
|
||||
miniWindow: resolve(__dirname, 'src/renderer/miniWindow.html'),
|
||||
selectionToolbar: resolve(__dirname, 'src/renderer/selectionToolbar.html'),
|
||||
selectionAction: resolve(__dirname, 'src/renderer/selectionAction.html')
|
||||
selectionAction: resolve(__dirname, 'src/renderer/selectionAction.html'),
|
||||
traceWindow: resolve(__dirname, 'src/renderer/traceWindow.html')
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@ -30,28 +30,88 @@ export default defineConfig([
|
||||
}
|
||||
},
|
||||
// Configuration for ensuring compatibility with the original ESLint(8.x) rules
|
||||
...[
|
||||
{
|
||||
rules: {
|
||||
'@typescript-eslint/no-require-imports': 'off',
|
||||
'@typescript-eslint/no-unused-vars': ['error', { caughtErrors: 'none' }],
|
||||
'@typescript-eslint/no-unused-expressions': 'off',
|
||||
'@typescript-eslint/no-empty-object-type': 'off',
|
||||
'@eslint-react/hooks-extra/no-direct-set-state-in-use-effect': 'off',
|
||||
'@eslint-react/web-api/no-leaked-event-listener': 'off',
|
||||
'@eslint-react/web-api/no-leaked-timeout': 'off',
|
||||
'@eslint-react/no-unknown-property': 'off',
|
||||
'@eslint-react/no-nested-component-definitions': 'off',
|
||||
'@eslint-react/dom/no-dangerously-set-innerhtml': 'off',
|
||||
'@eslint-react/no-array-index-key': 'off',
|
||||
'@eslint-react/no-unstable-default-props': 'off',
|
||||
'@eslint-react/no-unstable-context-value': 'off',
|
||||
'@eslint-react/hooks-extra/prefer-use-state-lazy-initialization': 'off',
|
||||
'@eslint-react/hooks-extra/no-unnecessary-use-prefix': 'off',
|
||||
'@eslint-react/no-children-to-array': 'off'
|
||||
}
|
||||
{
|
||||
rules: {
|
||||
'@typescript-eslint/no-require-imports': 'off',
|
||||
'@typescript-eslint/no-unused-vars': ['error', { caughtErrors: 'none' }],
|
||||
'@typescript-eslint/no-unused-expressions': 'off',
|
||||
'@typescript-eslint/no-empty-object-type': 'off',
|
||||
'@eslint-react/hooks-extra/no-direct-set-state-in-use-effect': 'off',
|
||||
'@eslint-react/web-api/no-leaked-event-listener': 'off',
|
||||
'@eslint-react/web-api/no-leaked-timeout': 'off',
|
||||
'@eslint-react/no-unknown-property': 'off',
|
||||
'@eslint-react/no-nested-component-definitions': 'off',
|
||||
'@eslint-react/dom/no-dangerously-set-innerhtml': 'off',
|
||||
'@eslint-react/no-array-index-key': 'off',
|
||||
'@eslint-react/no-unstable-default-props': 'off',
|
||||
'@eslint-react/no-unstable-context-value': 'off',
|
||||
'@eslint-react/hooks-extra/prefer-use-state-lazy-initialization': 'off',
|
||||
'@eslint-react/hooks-extra/no-unnecessary-use-prefix': 'off',
|
||||
'@eslint-react/no-children-to-array': 'off'
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
// LoggerService Custom Rules - only apply to src directory
|
||||
files: ['src/**/*.{ts,tsx,js,jsx}'],
|
||||
ignores: ['src/**/__tests__/**', 'src/**/__mocks__/**', 'src/**/*.test.*'],
|
||||
rules: {
|
||||
'no-restricted-syntax': [
|
||||
process.env.PRCI ? 'error' : 'warn',
|
||||
{
|
||||
selector: 'CallExpression[callee.object.name="console"]',
|
||||
message:
|
||||
'❗CherryStudio uses unified LoggerService: 📖 docs/technical/how-to-use-logger-en.md\n❗CherryStudio 使用统一的日志服务:📖 docs/technical/how-to-use-logger-zh.md\n\n'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
files: ['**/*.{ts,tsx,js,jsx}'],
|
||||
languageOptions: {
|
||||
ecmaVersion: 2022,
|
||||
sourceType: 'module'
|
||||
},
|
||||
plugins: {
|
||||
i18n: {
|
||||
rules: {
|
||||
'no-template-in-t': {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: '⚠️不建议在 t() 函数中使用模板字符串,这样会导致渲染结果不可预料',
|
||||
recommended: true
|
||||
},
|
||||
messages: {
|
||||
noTemplateInT: '⚠️不建议在 t() 函数中使用模板字符串,这样会导致渲染结果不可预料'
|
||||
}
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
const { callee, arguments: args } = node
|
||||
const isTFunction =
|
||||
(callee.type === 'Identifier' && callee.name === 't') ||
|
||||
(callee.type === 'MemberExpression' &&
|
||||
callee.property.type === 'Identifier' &&
|
||||
callee.property.name === 't')
|
||||
|
||||
if (isTFunction && args[0]?.type === 'TemplateLiteral') {
|
||||
context.report({
|
||||
node: args[0],
|
||||
messageId: 'noTemplateInT'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
rules: {
|
||||
'i18n/no-template-in-t': 'warn'
|
||||
}
|
||||
},
|
||||
{
|
||||
ignores: [
|
||||
'node_modules/**',
|
||||
@ -62,7 +122,8 @@ export default defineConfig([
|
||||
'.yarn/**',
|
||||
'.gitignore',
|
||||
'scripts/cloudflare-worker.js',
|
||||
'src/main/integration/nutstore/sso/lib/**'
|
||||
'src/main/integration/nutstore/sso/lib/**',
|
||||
'src/main/integration/cherryin/index.js'
|
||||
]
|
||||
}
|
||||
])
|
||||
|
||||
192
package.json
192
package.json
@ -1,11 +1,14 @@
|
||||
{
|
||||
"name": "CherryStudio",
|
||||
"version": "1.4.11",
|
||||
"version": "1.5.9",
|
||||
"private": true,
|
||||
"description": "A powerful AI assistant for producer.",
|
||||
"main": "./out/main/index.js",
|
||||
"author": "support@cherry-ai.com",
|
||||
"homepage": "https://github.com/CherryHQ/cherry-studio",
|
||||
"engines": {
|
||||
"node": ">=22.0.0"
|
||||
},
|
||||
"workspaces": {
|
||||
"packages": [
|
||||
"local",
|
||||
@ -13,16 +16,20 @@
|
||||
],
|
||||
"installConfig": {
|
||||
"hoistingLimits": [
|
||||
"packages/database"
|
||||
"packages/database",
|
||||
"packages/mcp-trace/trace-core",
|
||||
"packages/mcp-trace/trace-node",
|
||||
"packages/mcp-trace/trace-web",
|
||||
"packages/extension-table-plus"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"start": "electron-vite preview",
|
||||
"dev": "electron-vite dev",
|
||||
"dev": "dotenv electron-vite dev",
|
||||
"debug": "electron-vite -- --inspect --sourcemap --remote-debugging-port=9222",
|
||||
"build": "npm run typecheck && electron-vite build",
|
||||
"build:check": "yarn typecheck && yarn check:i18n && yarn test",
|
||||
"build:check": "yarn lint && yarn test",
|
||||
"build:unpack": "dotenv npm run build && electron-builder --dir",
|
||||
"build:win": "dotenv npm run build && electron-builder --win --x64 --arm64",
|
||||
"build:win:x64": "dotenv npm run build && electron-builder --win --x64",
|
||||
@ -33,17 +40,21 @@
|
||||
"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",
|
||||
"pulish:artifacts": "cd packages/artifacts && npm publish && cd -",
|
||||
"generate:agents": "yarn workspace @cherry-studio/database agents",
|
||||
"generate:icons": "electron-icon-builder --input=./build/logo.png --output=build",
|
||||
"analyze:renderer": "VISUALIZER_RENDERER=true yarn build",
|
||||
"analyze:main": "VISUALIZER_MAIN=true yarn build",
|
||||
"typecheck": "npm run typecheck:node && npm run typecheck:web",
|
||||
"typecheck": "concurrently -n \"node,web\" -c \"cyan,magenta\" \"npm run typecheck:node\" \"npm run typecheck:web\"",
|
||||
"typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false",
|
||||
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
|
||||
"check:i18n": "node scripts/check-i18n.js",
|
||||
"check:i18n": "tsx scripts/check-i18n.ts",
|
||||
"sync:i18n": "tsx scripts/sync-i18n.ts",
|
||||
"update:i18n": "dotenv -e .env -- tsx scripts/update-i18n.ts",
|
||||
"auto:i18n": "dotenv -e .env -- tsx scripts/auto-translate-i18n.ts",
|
||||
"update:languages": "tsx scripts/update-languages.ts",
|
||||
"test": "vitest run --silent",
|
||||
"test:main": "vitest run --project main",
|
||||
"test:renderer": "vitest run --project renderer",
|
||||
@ -53,25 +64,24 @@
|
||||
"test:watch": "vitest",
|
||||
"test:e2e": "yarn playwright test",
|
||||
"test:lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts",
|
||||
"test:scripts": "vitest scripts",
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
|
||||
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix && yarn typecheck && yarn check:i18n",
|
||||
"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",
|
||||
"@napi-rs/system-ocr": "patch:@napi-rs/system-ocr@npm%3A1.0.2#~/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch",
|
||||
"@strongtz/win32-arm64-msvc": "^0.4.7",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"jschardet": "^3.1.4",
|
||||
"graceful-fs": "^4.2.11",
|
||||
"jsdom": "26.1.0",
|
||||
"macos-release": "^3.4.0",
|
||||
"node-stream-zip": "^1.15.0",
|
||||
"notion-helper": "^1.3.22",
|
||||
"officeparser": "^4.2.0",
|
||||
"os-proxy-config": "^1.1.2",
|
||||
"pdfjs-dist": "4.10.38",
|
||||
"selection-hook": "^1.0.6",
|
||||
"selection-hook": "^1.0.11",
|
||||
"sharp": "^0.34.3",
|
||||
"tesseract.js": "patch:tesseract.js@npm%3A6.0.1#~/.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch",
|
||||
"turndown": "7.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -80,6 +90,10 @@
|
||||
"@agentic/tavily": "^7.3.3",
|
||||
"@ant-design/v5-patch-for-react-19": "^1.0.3",
|
||||
"@anthropic-ai/sdk": "^0.41.0",
|
||||
"@anthropic-ai/vertex-sdk": "patch:@anthropic-ai/vertex-sdk@npm%3A0.11.4#~/.yarn/patches/@anthropic-ai-vertex-sdk-npm-0.11.4-c19cb41edb.patch",
|
||||
"@aws-sdk/client-bedrock": "^3.840.0",
|
||||
"@aws-sdk/client-bedrock-runtime": "^3.840.0",
|
||||
"@aws-sdk/client-s3": "^3.840.0",
|
||||
"@cherrystudio/embedjs": "^0.1.31",
|
||||
"@cherrystudio/embedjs-libsql": "^0.1.31",
|
||||
"@cherrystudio/embedjs-loader-csv": "^0.1.31",
|
||||
@ -92,7 +106,11 @@
|
||||
"@cherrystudio/embedjs-loader-xml": "^0.1.31",
|
||||
"@cherrystudio/embedjs-ollama": "^0.1.31",
|
||||
"@cherrystudio/embedjs-openai": "^0.1.31",
|
||||
"@codemirror/view": "^6.0.0",
|
||||
"@cherrystudio/extension-table-plus": "workspace:^",
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/modifiers": "^9.0.0",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@electron-toolkit/eslint-config-prettier": "^3.0.0",
|
||||
"@electron-toolkit/eslint-config-ts": "^3.0.0",
|
||||
"@electron-toolkit/preload": "^3.0.0",
|
||||
@ -103,69 +121,99 @@
|
||||
"@eslint-react/eslint-plugin": "^1.36.1",
|
||||
"@eslint/js": "^9.22.0",
|
||||
"@google/genai": "patch:@google/genai@npm%3A1.0.1#~/.yarn/patches/@google-genai-npm-1.0.1-e26f0f9af7.patch",
|
||||
"@hello-pangea/dnd": "^16.6.0",
|
||||
"@hello-pangea/dnd": "^18.0.1",
|
||||
"@kangfenmao/keyv-storage": "^0.1.0",
|
||||
"@langchain/community": "^0.3.36",
|
||||
"@langchain/ollama": "^0.2.1",
|
||||
"@mistralai/mistralai": "^1.6.0",
|
||||
"@modelcontextprotocol/sdk": "^1.12.3",
|
||||
"@mistralai/mistralai": "^1.7.5",
|
||||
"@modelcontextprotocol/sdk": "^1.17.0",
|
||||
"@mozilla/readability": "^0.6.0",
|
||||
"@notionhq/client": "^2.2.15",
|
||||
"@opentelemetry/api": "^1.9.0",
|
||||
"@opentelemetry/core": "2.0.0",
|
||||
"@opentelemetry/exporter-trace-otlp-http": "^0.200.0",
|
||||
"@opentelemetry/sdk-trace-base": "^2.0.0",
|
||||
"@opentelemetry/sdk-trace-node": "^2.0.0",
|
||||
"@opentelemetry/sdk-trace-web": "^2.0.0",
|
||||
"@playwright/test": "^1.52.0",
|
||||
"@reduxjs/toolkit": "^2.2.5",
|
||||
"@shikijs/markdown-it": "^3.7.0",
|
||||
"@shikijs/markdown-it": "^3.12.0",
|
||||
"@swc/plugin-styled-components": "^7.1.5",
|
||||
"@tanstack/react-query": "^5.27.0",
|
||||
"@tanstack/react-query": "^5.85.5",
|
||||
"@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",
|
||||
"@testing-library/user-event": "^14.6.1",
|
||||
"@tiptap/extension-collaboration": "^3.2.0",
|
||||
"@tiptap/extension-drag-handle": "patch:@tiptap/extension-drag-handle@npm%3A3.2.0#~/.yarn/patches/@tiptap-extension-drag-handle-npm-3.2.0-5a9ebff7c9.patch",
|
||||
"@tiptap/extension-drag-handle-react": "^3.2.0",
|
||||
"@tiptap/extension-image": "^3.2.0",
|
||||
"@tiptap/extension-list": "^3.2.0",
|
||||
"@tiptap/extension-mathematics": "^3.2.0",
|
||||
"@tiptap/extension-mention": "^3.2.0",
|
||||
"@tiptap/extension-node-range": "^3.2.0",
|
||||
"@tiptap/extension-table-of-contents": "^3.2.0",
|
||||
"@tiptap/extension-typography": "^3.2.0",
|
||||
"@tiptap/extension-underline": "^3.2.0",
|
||||
"@tiptap/pm": "^3.2.0",
|
||||
"@tiptap/react": "^3.2.0",
|
||||
"@tiptap/starter-kit": "^3.2.0",
|
||||
"@tiptap/suggestion": "^3.2.0",
|
||||
"@tiptap/y-tiptap": "^3.0.0",
|
||||
"@truto/turndown-plugin-gfm": "^1.0.2",
|
||||
"@tryfabric/martian": "^1.2.4",
|
||||
"@types/diff": "^7",
|
||||
"@types/cli-progress": "^3",
|
||||
"@types/fs-extra": "^11",
|
||||
"@types/he": "^1",
|
||||
"@types/lodash": "^4.17.5",
|
||||
"@types/markdown-it": "^14",
|
||||
"@types/md5": "^2.3.5",
|
||||
"@types/node": "^18.19.9",
|
||||
"@types/node": "^22.17.1",
|
||||
"@types/pako": "^1.0.2",
|
||||
"@types/react": "^19.0.12",
|
||||
"@types/react-dom": "^19.0.4",
|
||||
"@types/react-infinite-scroll-component": "^5.0.0",
|
||||
"@types/react-window": "^1",
|
||||
"@types/react-transition-group": "^4.4.12",
|
||||
"@types/tinycolor2": "^1",
|
||||
"@types/turndown": "^5.0.5",
|
||||
"@types/word-extractor": "^1",
|
||||
"@uiw/codemirror-extensions-langs": "^4.23.14",
|
||||
"@uiw/codemirror-themes-all": "^4.23.14",
|
||||
"@uiw/react-codemirror": "^4.23.14",
|
||||
"@uiw/codemirror-extensions-langs": "^4.25.1",
|
||||
"@uiw/codemirror-themes-all": "^4.25.1",
|
||||
"@uiw/react-codemirror": "^4.25.1",
|
||||
"@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",
|
||||
"@vitest/browser": "^3.2.4",
|
||||
"@vitest/coverage-v8": "^3.2.4",
|
||||
"@vitest/ui": "^3.2.4",
|
||||
"@vitest/web-worker": "^3.2.4",
|
||||
"@viz-js/lang-dot": "^1.0.5",
|
||||
"@viz-js/viz": "^3.14.0",
|
||||
"@xyflow/react": "^12.4.4",
|
||||
"antd": "patch:antd@npm%3A5.24.7#~/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch",
|
||||
"antd": "patch:antd@npm%3A5.27.0#~/.yarn/patches/antd-npm-5.27.0-aa91c36546.patch",
|
||||
"archiver": "^7.0.1",
|
||||
"async-mutex": "^0.5.0",
|
||||
"axios": "^1.7.3",
|
||||
"browser-image-compression": "^2.0.2",
|
||||
"chardet": "^2.1.0",
|
||||
"chokidar": "^4.0.3",
|
||||
"cli-progress": "^3.12.0",
|
||||
"code-inspector-plugin": "^0.20.14",
|
||||
"color": "^5.0.0",
|
||||
"concurrently": "^9.2.1",
|
||||
"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",
|
||||
"diff": "^8.0.2",
|
||||
"docx": "^9.0.2",
|
||||
"dompurify": "^3.2.6",
|
||||
"dotenv-cli": "^7.4.2",
|
||||
"electron": "35.6.0",
|
||||
"electron": "37.4.0",
|
||||
"electron-builder": "26.0.15",
|
||||
"electron-devtools-installer": "^3.2.0",
|
||||
"electron-log": "^5.1.5",
|
||||
"electron-store": "^8.2.0",
|
||||
"electron-updater": "6.6.4",
|
||||
"electron-vite": "^3.1.0",
|
||||
"electron-vite": "4.0.0",
|
||||
"electron-window-state": "^5.0.3",
|
||||
"emittery": "^1.0.3",
|
||||
"emoji-picker-element": "^1.22.1",
|
||||
@ -176,80 +224,112 @@
|
||||
"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",
|
||||
"he": "^1.2.0",
|
||||
"html-tags": "^5.1.0",
|
||||
"html-to-image": "^1.11.13",
|
||||
"htmlparser2": "^10.0.0",
|
||||
"husky": "^9.1.7",
|
||||
"i18next": "^23.11.5",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"isbinaryfile": "5.0.4",
|
||||
"jaison": "^2.0.2",
|
||||
"jest-styled-components": "^7.2.0",
|
||||
"linguist-languages": "^8.1.0",
|
||||
"lint-staged": "^15.5.0",
|
||||
"lodash": "^4.17.21",
|
||||
"lru-cache": "^11.1.0",
|
||||
"lucide-react": "^0.487.0",
|
||||
"lucide-react": "^0.525.0",
|
||||
"macos-release": "^3.4.0",
|
||||
"markdown-it": "^14.1.0",
|
||||
"mermaid": "^11.7.0",
|
||||
"mermaid": "^11.10.1",
|
||||
"mime": "^4.0.4",
|
||||
"motion": "^12.10.5",
|
||||
"notion-helper": "^1.3.22",
|
||||
"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",
|
||||
"openai": "patch:openai@npm%3A5.12.2#~/.yarn/patches/openai-npm-5.12.2-30b075401c.patch",
|
||||
"p-queue": "^8.1.0",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"playwright": "^1.52.0",
|
||||
"prettier": "^3.5.3",
|
||||
"prettier-plugin-sort-json": "^4.1.1",
|
||||
"proxy-agent": "^6.5.0",
|
||||
"rc-virtual-list": "^3.18.6",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0",
|
||||
"react-error-boundary": "^6.0.0",
|
||||
"react-hotkeys-hook": "^4.6.1",
|
||||
"react-i18next": "^14.1.2",
|
||||
"react-infinite-scroll-component": "^6.1.0",
|
||||
"react-json-view": "^1.21.3",
|
||||
"react-markdown": "^10.1.0",
|
||||
"react-redux": "^9.1.2",
|
||||
"react-router": "6",
|
||||
"react-router-dom": "6",
|
||||
"react-spinners": "^0.14.1",
|
||||
"react-window": "^1.8.11",
|
||||
"react-transition-group": "^4.4.5",
|
||||
"redux": "^5.0.1",
|
||||
"redux-persist": "^6.0.0",
|
||||
"reflect-metadata": "0.2.2",
|
||||
"rehype-katex": "^7.0.1",
|
||||
"rehype-mathjax": "^7.1.0",
|
||||
"rehype-parse": "^9.0.1",
|
||||
"rehype-raw": "^7.0.0",
|
||||
"rehype-stringify": "^10.0.1",
|
||||
"remark-cjk-friendly": "^1.2.0",
|
||||
"remark-gfm": "^4.0.1",
|
||||
"remark-github-blockquote-alert": "^2.0.0",
|
||||
"remark-math": "^6.0.0",
|
||||
"remove-markdown": "^0.6.2",
|
||||
"rollup-plugin-visualizer": "^5.12.0",
|
||||
"sass": "^1.88.0",
|
||||
"shiki": "^3.7.0",
|
||||
"shiki": "^3.12.0",
|
||||
"strict-url-sanitise": "^0.0.1",
|
||||
"string-width": "^7.2.0",
|
||||
"striptags": "^3.2.0",
|
||||
"styled-components": "^6.1.11",
|
||||
"tar": "^7.4.3",
|
||||
"tiny-pinyin": "^1.3.2",
|
||||
"tokenx": "^1.1.0",
|
||||
"tsx": "^4.20.3",
|
||||
"turndown-plugin-gfm": "^1.0.2",
|
||||
"typescript": "^5.6.2",
|
||||
"undici": "6.21.2",
|
||||
"unified": "^11.0.5",
|
||||
"uuid": "^10.0.0",
|
||||
"vite": "6.2.6",
|
||||
"vitest": "^3.1.4",
|
||||
"vite": "npm:rolldown-vite@latest",
|
||||
"vitest": "^3.2.4",
|
||||
"webdav": "^5.8.0",
|
||||
"winston": "^3.17.0",
|
||||
"winston-daily-rotate-file": "^5.0.0",
|
||||
"word-extractor": "^1.0.4",
|
||||
"zipread": "^1.3.3"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@cherrystudio/mac-system-ocr": "^0.2.2"
|
||||
"y-protocols": "^1.0.6",
|
||||
"yjs": "^13.6.27",
|
||||
"zipread": "^1.3.3",
|
||||
"zod": "^3.25.74"
|
||||
},
|
||||
"resolutions": {
|
||||
"pdf-parse@npm:1.1.1": "patch:pdf-parse@npm%3A1.1.1#~/.yarn/patches/pdf-parse-npm-1.1.1-04a6109b2a.patch",
|
||||
"@codemirror/language": "6.11.3",
|
||||
"@codemirror/lint": "6.8.5",
|
||||
"@codemirror/view": "6.38.1",
|
||||
"@langchain/core@npm:^0.3.26": "patch:@langchain/core@npm%3A0.3.44#~/.yarn/patches/@langchain-core-npm-0.3.44-41d5c3cb0a.patch",
|
||||
"@langchain/openai@npm:^0.3.16": "patch:@langchain/openai@npm%3A0.3.16#~/.yarn/patches/@langchain-openai-npm-0.3.16-e525b59526.patch",
|
||||
"@langchain/openai@npm:>=0.1.0 <0.4.0": "patch:@langchain/openai@npm%3A0.3.16#~/.yarn/patches/@langchain-openai-npm-0.3.16-e525b59526.patch",
|
||||
"libsql@npm:^0.4.4": "patch:libsql@npm%3A0.4.7#~/.yarn/patches/libsql-npm-0.4.7-444e260fb1.patch",
|
||||
"openai@npm:^4.77.0": "patch:openai@npm%3A5.1.0#~/.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch",
|
||||
"pkce-challenge@npm:^4.1.0": "patch:pkce-challenge@npm%3A4.1.0#~/.yarn/patches/pkce-challenge-npm-4.1.0-fbc51695a3.patch",
|
||||
"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"
|
||||
"atomically@npm:^1.7.0": "patch:atomically@npm%3A1.7.0#~/.yarn/patches/atomically-npm-1.7.0-e742e5293b.patch",
|
||||
"file-stream-rotator@npm:^0.6.1": "patch:file-stream-rotator@npm%3A0.6.1#~/.yarn/patches/file-stream-rotator-npm-0.6.1-eab45fb13d.patch",
|
||||
"libsql@npm:^0.4.4": "patch:libsql@npm%3A0.4.7#~/.yarn/patches/libsql-npm-0.4.7-444e260fb1.patch",
|
||||
"node-abi": "4.12.0",
|
||||
"openai@npm:^4.77.0": "patch:openai@npm%3A5.12.2#~/.yarn/patches/openai-npm-5.12.2-30b075401c.patch",
|
||||
"openai@npm:^4.87.3": "patch:openai@npm%3A5.12.2#~/.yarn/patches/openai-npm-5.12.2-30b075401c.patch",
|
||||
"pdf-parse@npm:1.1.1": "patch:pdf-parse@npm%3A1.1.1#~/.yarn/patches/pdf-parse-npm-1.1.1-04a6109b2a.patch",
|
||||
"pkce-challenge@npm:^4.1.0": "patch:pkce-challenge@npm%3A4.1.0#~/.yarn/patches/pkce-challenge-npm-4.1.0-fbc51695a3.patch",
|
||||
"undici": "6.21.2",
|
||||
"vite": "npm:rolldown-vite@latest",
|
||||
"tesseract.js@npm:*": "patch:tesseract.js@npm%3A6.0.1#~/.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch"
|
||||
},
|
||||
"packageManager": "yarn@4.9.1",
|
||||
"lint-staged": {
|
||||
|
||||
1457
packages/extension-table-plus/CHANGELOG.md
Executable file
1457
packages/extension-table-plus/CHANGELOG.md
Executable file
File diff suppressed because it is too large
Load Diff
18
packages/extension-table-plus/README.md
Executable file
18
packages/extension-table-plus/README.md
Executable file
@ -0,0 +1,18 @@
|
||||
# @tiptap/extension-table
|
||||
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-table)
|
||||
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-table)
|
||||
[](https://github.com/sponsors/ueberdosis)
|
||||
|
||||
## Introduction
|
||||
|
||||
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
||||
|
||||
## Official Documentation
|
||||
|
||||
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
||||
|
||||
## License
|
||||
|
||||
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
||||
93
packages/extension-table-plus/package.json
Executable file
93
packages/extension-table-plus/package.json
Executable file
@ -0,0 +1,93 @@
|
||||
{
|
||||
"name": "@cherrystudio/extension-table-plus",
|
||||
"description": "table extension for tiptap forked from tiptap/extension-table",
|
||||
"version": "3.0.11",
|
||||
"homepage": "https://cherry-ai.com",
|
||||
"keywords": [
|
||||
"tiptap",
|
||||
"tiptap extension"
|
||||
],
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": {
|
||||
"import": "./dist/index.d.ts",
|
||||
"require": "./dist/index.d.cts"
|
||||
},
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs"
|
||||
},
|
||||
"./table": {
|
||||
"types": {
|
||||
"import": "./dist/table/index.d.ts",
|
||||
"require": "./dist/table/index.d.cts"
|
||||
},
|
||||
"import": "./dist/table/index.js",
|
||||
"require": "./dist/table/index.cjs"
|
||||
},
|
||||
"./cell": {
|
||||
"types": {
|
||||
"import": "./dist/cell/index.d.ts",
|
||||
"require": "./dist/cell/index.d.cts"
|
||||
},
|
||||
"import": "./dist/cell/index.js",
|
||||
"require": "./dist/cell/index.cjs"
|
||||
},
|
||||
"./header": {
|
||||
"types": {
|
||||
"import": "./dist/header/index.d.ts",
|
||||
"require": "./dist/header/index.d.cts"
|
||||
},
|
||||
"import": "./dist/header/index.js",
|
||||
"require": "./dist/header/index.cjs"
|
||||
},
|
||||
"./kit": {
|
||||
"types": {
|
||||
"import": "./dist/kit/index.d.ts",
|
||||
"require": "./dist/kit/index.d.cts"
|
||||
},
|
||||
"import": "./dist/kit/index.js",
|
||||
"require": "./dist/kit/index.cjs"
|
||||
},
|
||||
"./row": {
|
||||
"types": {
|
||||
"import": "./dist/row/index.d.ts",
|
||||
"require": "./dist/row/index.d.cts"
|
||||
},
|
||||
"import": "./dist/row/index.js",
|
||||
"require": "./dist/row/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@tiptap/core": "^3.2.0",
|
||||
"@tiptap/pm": "^3.2.0",
|
||||
"eslint": "^9.22.0",
|
||||
"eslint-plugin-react-hooks": "^5.2.0",
|
||||
"eslint-plugin-simple-import-sort": "^12.1.1",
|
||||
"eslint-plugin-unused-imports": "^4.1.4",
|
||||
"prettier": "^3.5.3",
|
||||
"tsdown": "^0.13.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@tiptap/core": "^3.0.9",
|
||||
"@tiptap/pm": "^3.0.9"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/CherryHQ/cherry-studio",
|
||||
"directory": "packages/extension-table-plus"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"lint": "prettier ./src/ --write && eslint --fix ./src/"
|
||||
},
|
||||
"packageManager": "yarn@4.9.1"
|
||||
}
|
||||
1
packages/extension-table-plus/src/cell/index.ts
Executable file
1
packages/extension-table-plus/src/cell/index.ts
Executable file
@ -0,0 +1 @@
|
||||
export * from './table-cell.js'
|
||||
150
packages/extension-table-plus/src/cell/table-cell.ts
Executable file
150
packages/extension-table-plus/src/cell/table-cell.ts
Executable file
@ -0,0 +1,150 @@
|
||||
import '../types.js'
|
||||
|
||||
import { mergeAttributes, Node } from '@tiptap/core'
|
||||
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
||||
import type { Selection } from '@tiptap/pm/state'
|
||||
import { Plugin, PluginKey } from '@tiptap/pm/state'
|
||||
import { CellSelection, TableMap } from '@tiptap/pm/tables'
|
||||
import { Decoration, DecorationSet } from '@tiptap/pm/view'
|
||||
|
||||
export interface TableCellOptions {
|
||||
/**
|
||||
* The HTML attributes for a table cell node.
|
||||
* @default {}
|
||||
* @example { class: 'foo' }
|
||||
*/
|
||||
HTMLAttributes: Record<string, any>
|
||||
/**
|
||||
* Whether nodes can be nested inside a cell.
|
||||
* @default false
|
||||
*/
|
||||
allowNestedNodes: boolean
|
||||
}
|
||||
|
||||
const cellSelectionPluginKey = new PluginKey('cellSelectionStyling')
|
||||
|
||||
function isTableNode(node: ProseMirrorNode): boolean {
|
||||
const spec = node.type.spec as { tableRole?: string } | undefined
|
||||
return node.type.name === 'table' || spec?.tableRole === 'table'
|
||||
}
|
||||
|
||||
function createCellSelectionDecorationSet(doc: ProseMirrorNode, selection: Selection): DecorationSet {
|
||||
if (!(selection instanceof CellSelection)) {
|
||||
return DecorationSet.empty
|
||||
}
|
||||
|
||||
const $anchor = selection.$anchorCell || selection.$anchor
|
||||
let tableNode: ProseMirrorNode | null = null
|
||||
let tablePos = -1
|
||||
|
||||
for (let depth = $anchor.depth; depth > 0; depth--) {
|
||||
const nodeAtDepth = $anchor.node(depth) as ProseMirrorNode
|
||||
if (isTableNode(nodeAtDepth)) {
|
||||
tableNode = nodeAtDepth
|
||||
tablePos = $anchor.before(depth)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!tableNode) {
|
||||
return DecorationSet.empty
|
||||
}
|
||||
|
||||
const map = TableMap.get(tableNode)
|
||||
const tableStart = tablePos + 1
|
||||
|
||||
type Rect = { top: number; bottom: number; left: number; right: number }
|
||||
type Item = { pos: number; node: ProseMirrorNode; rect: Rect }
|
||||
|
||||
const items: Item[] = []
|
||||
let minRow = Number.POSITIVE_INFINITY
|
||||
let maxRow = Number.NEGATIVE_INFINITY
|
||||
let minCol = Number.POSITIVE_INFINITY
|
||||
let maxCol = Number.NEGATIVE_INFINITY
|
||||
|
||||
selection.forEachCell((cell, pos) => {
|
||||
const rect = map.findCell(pos - tableStart)
|
||||
items.push({ pos, node: cell, rect })
|
||||
|
||||
minRow = Math.min(minRow, rect.top)
|
||||
maxRow = Math.max(maxRow, rect.bottom - 1)
|
||||
minCol = Math.min(minCol, rect.left)
|
||||
maxCol = Math.max(maxCol, rect.right - 1)
|
||||
})
|
||||
|
||||
const decorations: Decoration[] = []
|
||||
for (const { pos, node, rect } of items) {
|
||||
const classes: string[] = ['selectedCell']
|
||||
if (rect.top === minRow) classes.push('selection-top')
|
||||
if (rect.bottom - 1 === maxRow) classes.push('selection-bottom')
|
||||
if (rect.left === minCol) classes.push('selection-left')
|
||||
if (rect.right - 1 === maxCol) classes.push('selection-right')
|
||||
|
||||
decorations.push(
|
||||
Decoration.node(pos, pos + node.nodeSize, {
|
||||
class: classes.join(' ')
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return DecorationSet.create(doc, decorations)
|
||||
}
|
||||
/**
|
||||
* This extension allows you to create table cells.
|
||||
* @see https://www.tiptap.dev/api/nodes/table-cell
|
||||
*/
|
||||
export const TableCell = Node.create<TableCellOptions>({
|
||||
name: 'tableCell',
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {},
|
||||
allowNestedNodes: false
|
||||
}
|
||||
},
|
||||
|
||||
content: '(paragraph | image)+',
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
colspan: {
|
||||
default: 1
|
||||
},
|
||||
rowspan: {
|
||||
default: 1
|
||||
},
|
||||
colwidth: {
|
||||
default: null,
|
||||
parseHTML: (element) => {
|
||||
const colwidth = element.getAttribute('colwidth')
|
||||
const value = colwidth ? colwidth.split(',').map((width) => parseInt(width, 10)) : null
|
||||
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
tableRole: 'cell',
|
||||
|
||||
isolating: true,
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'td' }]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
return [
|
||||
new Plugin({
|
||||
key: cellSelectionPluginKey,
|
||||
props: {
|
||||
decorations: ({ doc, selection }) => createCellSelectionDecorationSet(doc as ProseMirrorNode, selection)
|
||||
}
|
||||
})
|
||||
]
|
||||
}
|
||||
})
|
||||
1
packages/extension-table-plus/src/header/index.ts
Executable file
1
packages/extension-table-plus/src/header/index.ts
Executable file
@ -0,0 +1 @@
|
||||
export * from './table-header.js'
|
||||
60
packages/extension-table-plus/src/header/table-header.ts
Executable file
60
packages/extension-table-plus/src/header/table-header.ts
Executable file
@ -0,0 +1,60 @@
|
||||
import '../types.js'
|
||||
|
||||
import { mergeAttributes, Node } from '@tiptap/core'
|
||||
|
||||
export interface TableHeaderOptions {
|
||||
/**
|
||||
* The HTML attributes for a table header node.
|
||||
* @default {}
|
||||
* @example { class: 'foo' }
|
||||
*/
|
||||
HTMLAttributes: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* This extension allows you to create table headers.
|
||||
* @see https://www.tiptap.dev/api/nodes/table-header
|
||||
*/
|
||||
export const TableHeader = Node.create<TableHeaderOptions>({
|
||||
name: 'tableHeader',
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {}
|
||||
}
|
||||
},
|
||||
|
||||
content: 'paragraph+',
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
colspan: {
|
||||
default: 1
|
||||
},
|
||||
rowspan: {
|
||||
default: 1
|
||||
},
|
||||
colwidth: {
|
||||
default: null,
|
||||
parseHTML: (element) => {
|
||||
const colwidth = element.getAttribute('colwidth')
|
||||
const value = colwidth ? colwidth.split(',').map((width) => parseInt(width, 10)) : null
|
||||
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
tableRole: 'header_cell',
|
||||
|
||||
isolating: true,
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'th' }]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return ['th', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
||||
}
|
||||
})
|
||||
6
packages/extension-table-plus/src/index.ts
Executable file
6
packages/extension-table-plus/src/index.ts
Executable file
@ -0,0 +1,6 @@
|
||||
export * from './cell/index.js'
|
||||
export * from './header/index.js'
|
||||
export * from './kit/index.js'
|
||||
export * from './row/index.js'
|
||||
export * from './table/index.js'
|
||||
export * from './table/TableView.js'
|
||||
64
packages/extension-table-plus/src/kit/index.ts
Executable file
64
packages/extension-table-plus/src/kit/index.ts
Executable file
@ -0,0 +1,64 @@
|
||||
import { Extension, Node } from '@tiptap/core'
|
||||
|
||||
import type { TableCellOptions } from '../cell/index.js'
|
||||
import { TableCell } from '../cell/index.js'
|
||||
import type { TableHeaderOptions } from '../header/index.js'
|
||||
import { TableHeader } from '../header/index.js'
|
||||
import type { TableRowOptions } from '../row/index.js'
|
||||
import { TableRow } from '../row/index.js'
|
||||
import type { TableOptions } from '../table/index.js'
|
||||
import { Table } from '../table/index.js'
|
||||
|
||||
export interface TableKitOptions {
|
||||
/**
|
||||
* If set to false, the table extension will not be registered
|
||||
* @example table: false
|
||||
*/
|
||||
table: Partial<TableOptions> | false
|
||||
/**
|
||||
* If set to false, the table extension will not be registered
|
||||
* @example tableCell: false
|
||||
*/
|
||||
tableCell: Partial<TableCellOptions> | false
|
||||
/**
|
||||
* If set to false, the table extension will not be registered
|
||||
* @example tableHeader: false
|
||||
*/
|
||||
tableHeader: Partial<TableHeaderOptions> | false
|
||||
/**
|
||||
* If set to false, the table extension will not be registered
|
||||
* @example tableRow: false
|
||||
*/
|
||||
tableRow: Partial<TableRowOptions> | false
|
||||
}
|
||||
|
||||
/**
|
||||
* The table kit is a collection of table editor extensions.
|
||||
*
|
||||
* It’s a good starting point for building your own table in Tiptap.
|
||||
*/
|
||||
export const TableKit = Extension.create<TableKitOptions>({
|
||||
name: 'tableKit',
|
||||
|
||||
addExtensions() {
|
||||
const extensions: Node[] = []
|
||||
|
||||
if (this.options.table !== false) {
|
||||
extensions.push(Table.configure(this.options.table))
|
||||
}
|
||||
|
||||
if (this.options.tableCell !== false) {
|
||||
extensions.push(TableCell.configure(this.options.tableCell))
|
||||
}
|
||||
|
||||
if (this.options.tableHeader !== false) {
|
||||
extensions.push(TableHeader.configure(this.options.tableHeader))
|
||||
}
|
||||
|
||||
if (this.options.tableRow !== false) {
|
||||
extensions.push(TableRow.configure(this.options.tableRow))
|
||||
}
|
||||
|
||||
return extensions
|
||||
}
|
||||
})
|
||||
1
packages/extension-table-plus/src/row/index.ts
Executable file
1
packages/extension-table-plus/src/row/index.ts
Executable file
@ -0,0 +1 @@
|
||||
export * from './table-row.js'
|
||||
38
packages/extension-table-plus/src/row/table-row.ts
Executable file
38
packages/extension-table-plus/src/row/table-row.ts
Executable file
@ -0,0 +1,38 @@
|
||||
import '../types.js'
|
||||
|
||||
import { mergeAttributes, Node } from '@tiptap/core'
|
||||
|
||||
export interface TableRowOptions {
|
||||
/**
|
||||
* The HTML attributes for a table row node.
|
||||
* @default {}
|
||||
* @example { class: 'foo' }
|
||||
*/
|
||||
HTMLAttributes: Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* This extension allows you to create table rows.
|
||||
* @see https://www.tiptap.dev/api/nodes/table-row
|
||||
*/
|
||||
export const TableRow = Node.create<TableRowOptions>({
|
||||
name: 'tableRow',
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {}
|
||||
}
|
||||
},
|
||||
|
||||
content: '(tableCell | tableHeader)*',
|
||||
|
||||
tableRole: 'row',
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'tr' }]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return ['tr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
||||
}
|
||||
})
|
||||
558
packages/extension-table-plus/src/table/TableView.ts
Executable file
558
packages/extension-table-plus/src/table/TableView.ts
Executable file
@ -0,0 +1,558 @@
|
||||
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
||||
import { TextSelection } from '@tiptap/pm/state'
|
||||
import { addColumnAfter, addRowAfter, CellSelection, TableMap } from '@tiptap/pm/tables'
|
||||
import type { EditorView, NodeView, ViewMutationRecord } from '@tiptap/pm/view'
|
||||
|
||||
import { getColStyleDeclaration } from './utilities/colStyle.js'
|
||||
import { getElementBorderWidth } from './utilities/getBorderWidth.js'
|
||||
import { isCellSelection } from './utilities/isCellSelection.js'
|
||||
import { getCellSelectionBounds } from './utilities/selectionBounds.js'
|
||||
|
||||
export function updateColumns(
|
||||
node: ProseMirrorNode,
|
||||
colgroup: HTMLTableColElement, // <colgroup> has the same prototype as <col>
|
||||
table: HTMLTableElement,
|
||||
cellMinWidth: number,
|
||||
overrideCol?: number,
|
||||
overrideValue?: number
|
||||
) {
|
||||
let totalWidth = 0
|
||||
let fixedWidth = true
|
||||
let nextDOM = colgroup.firstChild
|
||||
const row = node.firstChild
|
||||
|
||||
if (row !== null) {
|
||||
for (let i = 0, col = 0; i < row.childCount; i += 1) {
|
||||
const { colspan, colwidth } = row.child(i).attrs
|
||||
|
||||
for (let j = 0; j < colspan; j += 1, col += 1) {
|
||||
const hasWidth = overrideCol === col ? overrideValue : ((colwidth && colwidth[j]) as number | undefined)
|
||||
const cssWidth = hasWidth ? `${hasWidth}px` : ''
|
||||
|
||||
totalWidth += hasWidth || cellMinWidth
|
||||
|
||||
if (!hasWidth) {
|
||||
fixedWidth = false
|
||||
}
|
||||
|
||||
if (!nextDOM) {
|
||||
const colElement = document.createElement('col')
|
||||
|
||||
const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth)
|
||||
|
||||
colElement.style.setProperty(propertyKey, propertyValue)
|
||||
|
||||
colgroup.appendChild(colElement)
|
||||
} else {
|
||||
if ((nextDOM as HTMLTableColElement).style.width !== cssWidth) {
|
||||
const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth)
|
||||
|
||||
;(nextDOM as HTMLTableColElement).style.setProperty(propertyKey, propertyValue)
|
||||
}
|
||||
|
||||
nextDOM = nextDOM.nextSibling
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (nextDOM) {
|
||||
const after = nextDOM.nextSibling
|
||||
|
||||
nextDOM.parentNode?.removeChild(nextDOM)
|
||||
nextDOM = after
|
||||
}
|
||||
|
||||
if (fixedWidth) {
|
||||
table.style.width = `${totalWidth}px`
|
||||
table.style.minWidth = ''
|
||||
} else {
|
||||
table.style.width = ''
|
||||
table.style.minWidth = `${totalWidth}px`
|
||||
}
|
||||
}
|
||||
|
||||
// Callbacks are now handled by a decorations plugin; keep type removed here
|
||||
|
||||
type ButtonPosition = { x: number; y: number }
|
||||
type RowActionCallback = (args: { rowIndex: number; view: EditorView; position?: ButtonPosition }) => void
|
||||
type ColumnActionCallback = (args: { colIndex: number; view: EditorView; position?: ButtonPosition }) => void
|
||||
|
||||
export class TableView implements NodeView {
|
||||
node: ProseMirrorNode
|
||||
|
||||
cellMinWidth: number
|
||||
|
||||
dom: HTMLDivElement
|
||||
|
||||
table: HTMLTableElement
|
||||
|
||||
colgroup: HTMLTableColElement
|
||||
|
||||
contentDOM: HTMLTableSectionElement
|
||||
|
||||
view: EditorView
|
||||
|
||||
addRowButton: HTMLButtonElement
|
||||
|
||||
addColumnButton: HTMLButtonElement
|
||||
|
||||
tableContainer: HTMLDivElement
|
||||
|
||||
// Hover add buttons are kept; overlay endpoints absolute on wrapper
|
||||
private selectionChangeDisposer?: () => void
|
||||
private rowEndpoint?: HTMLButtonElement
|
||||
private colEndpoint?: HTMLButtonElement
|
||||
private overlayUpdateRafId: number | null = null
|
||||
private actionCallbacks?: {
|
||||
onRowActionClick?: RowActionCallback
|
||||
onColumnActionClick?: ColumnActionCallback
|
||||
}
|
||||
|
||||
constructor(
|
||||
node: ProseMirrorNode,
|
||||
cellMinWidth: number,
|
||||
view: EditorView,
|
||||
actionCallbacks?: { onRowActionClick?: RowActionCallback; onColumnActionClick?: ColumnActionCallback }
|
||||
) {
|
||||
this.node = node
|
||||
this.cellMinWidth = cellMinWidth
|
||||
this.view = view
|
||||
this.actionCallbacks = actionCallbacks
|
||||
// selection triggers handled by decorations plugin
|
||||
|
||||
// Create the wrapper with grid layout
|
||||
this.dom = document.createElement('div')
|
||||
this.dom.className = 'tableWrapper'
|
||||
|
||||
// Create table container
|
||||
this.tableContainer = document.createElement('div')
|
||||
this.tableContainer.className = 'table-container'
|
||||
|
||||
this.table = this.tableContainer.appendChild(document.createElement('table'))
|
||||
this.colgroup = this.table.appendChild(document.createElement('colgroup'))
|
||||
updateColumns(node, this.colgroup, this.table, cellMinWidth)
|
||||
this.contentDOM = this.table.appendChild(document.createElement('tbody'))
|
||||
|
||||
this.addRowButton = document.createElement('button')
|
||||
this.addColumnButton = document.createElement('button')
|
||||
this.createHoverButtons()
|
||||
|
||||
this.dom.appendChild(this.tableContainer)
|
||||
this.dom.appendChild(this.addColumnButton)
|
||||
this.dom.appendChild(this.addRowButton)
|
||||
|
||||
this.syncEditableState()
|
||||
|
||||
this.setupEventListeners()
|
||||
|
||||
// create overlay endpoints
|
||||
this.rowEndpoint = document.createElement('button')
|
||||
this.rowEndpoint.className = 'row-action-trigger'
|
||||
this.rowEndpoint.type = 'button'
|
||||
this.rowEndpoint.setAttribute('contenteditable', 'false')
|
||||
this.rowEndpoint.style.position = 'absolute'
|
||||
this.rowEndpoint.style.display = 'none'
|
||||
this.rowEndpoint.tabIndex = -1
|
||||
|
||||
this.colEndpoint = document.createElement('button')
|
||||
this.colEndpoint.className = 'column-action-trigger'
|
||||
this.colEndpoint.type = 'button'
|
||||
this.colEndpoint.setAttribute('contenteditable', 'false')
|
||||
this.colEndpoint.style.position = 'absolute'
|
||||
this.colEndpoint.style.display = 'none'
|
||||
this.colEndpoint.tabIndex = -1
|
||||
|
||||
this.dom.appendChild(this.rowEndpoint)
|
||||
this.dom.appendChild(this.colEndpoint)
|
||||
|
||||
this.bindOverlayHandlers()
|
||||
this.startSelectionWatcher()
|
||||
}
|
||||
|
||||
update(node: ProseMirrorNode) {
|
||||
if (node.type !== this.node.type) {
|
||||
return false
|
||||
}
|
||||
|
||||
this.node = node
|
||||
updateColumns(node, this.colgroup, this.table, this.cellMinWidth)
|
||||
|
||||
// Keep buttons' disabled state in sync during updates
|
||||
this.syncEditableState()
|
||||
|
||||
// Recalculate overlay positions after node/table mutations so triggers follow the updated layout
|
||||
this.scheduleOverlayUpdate()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
ignoreMutation(mutation: ViewMutationRecord) {
|
||||
return (
|
||||
(mutation.type === 'attributes' && (mutation.target === this.table || this.colgroup.contains(mutation.target))) ||
|
||||
// Ignore mutations on our action buttons
|
||||
(mutation.target as Element)?.classList?.contains('row-action-trigger') ||
|
||||
(mutation.target as Element)?.classList?.contains('column-action-trigger')
|
||||
)
|
||||
}
|
||||
|
||||
private isEditable(): boolean {
|
||||
// Rely on DOM attribute to avoid depending on EditorView internals
|
||||
return this.view.dom.getAttribute('contenteditable') !== 'false'
|
||||
}
|
||||
|
||||
private syncEditableState() {
|
||||
const editable = this.isEditable()
|
||||
this.addRowButton.toggleAttribute('disabled', !editable)
|
||||
this.addColumnButton.toggleAttribute('disabled', !editable)
|
||||
|
||||
this.addRowButton.style.display = editable ? '' : 'none'
|
||||
this.addColumnButton.style.display = editable ? '' : 'none'
|
||||
this.dom.classList.toggle('is-readonly', !editable)
|
||||
}
|
||||
|
||||
createHoverButtons() {
|
||||
this.addRowButton.className = 'add-row-button'
|
||||
this.addRowButton.type = 'button'
|
||||
this.addRowButton.setAttribute('contenteditable', 'false')
|
||||
|
||||
this.addColumnButton.className = 'add-column-button'
|
||||
this.addColumnButton.type = 'button'
|
||||
this.addColumnButton.setAttribute('contenteditable', 'false')
|
||||
}
|
||||
|
||||
private addTableRowOrColumn(isRow: boolean) {
|
||||
if (!this.isEditable()) return
|
||||
|
||||
this.view.focus()
|
||||
|
||||
// Save current selection info and calculate position in table
|
||||
const { state } = this.view
|
||||
const originalSelection = state.selection
|
||||
|
||||
// Find which cell we're currently in and the relative position within that cell
|
||||
let tablePos = -1
|
||||
let currentCellRow = -1
|
||||
let currentCellCol = -1
|
||||
let relativeOffsetInCell = 0
|
||||
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
const map = TableMap.get(this.node)
|
||||
|
||||
// Find which cell contains our selection
|
||||
const selectionPos = originalSelection.from
|
||||
for (let row = 0; row < map.height; row++) {
|
||||
for (let col = 0; col < map.width; col++) {
|
||||
const cellIndex = row * map.width + col
|
||||
const cellStart = pos + 1 + map.map[cellIndex]
|
||||
const cellNode = state.doc.nodeAt(cellStart)
|
||||
if (cellNode) {
|
||||
const cellEnd = cellStart + cellNode.nodeSize
|
||||
if (selectionPos >= cellStart && selectionPos < cellEnd) {
|
||||
currentCellRow = row
|
||||
currentCellCol = col
|
||||
relativeOffsetInCell = selectionPos - cellStart
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
// Set selection to appropriate position for adding
|
||||
if (isRow) {
|
||||
this.setSelectionToLastRow()
|
||||
} else {
|
||||
this.setSelectionToLastColumn()
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
const { state, dispatch } = this.view
|
||||
const addFunction = isRow ? addRowAfter : addColumnAfter
|
||||
|
||||
if (addFunction(state, dispatch)) {
|
||||
setTimeout(() => {
|
||||
const newState = this.view.state
|
||||
|
||||
// Calculate new position for the same logical cell with same relative offset
|
||||
if (tablePos >= 0 && currentCellRow >= 0 && currentCellCol >= 0) {
|
||||
newState.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && pos === tablePos) {
|
||||
const newMap = TableMap.get(node)
|
||||
const newCellIndex = currentCellRow * newMap.width + currentCellCol
|
||||
const newCellStart = pos + 1 + newMap.map[newCellIndex]
|
||||
const newCellNode = newState.doc.nodeAt(newCellStart)
|
||||
|
||||
if (newCellNode) {
|
||||
// Try to maintain the same relative position within the cell
|
||||
const newCellEnd = newCellStart + newCellNode.nodeSize
|
||||
const targetPos = Math.min(newCellStart + relativeOffsetInCell, newCellEnd - 1)
|
||||
const newSelection = TextSelection.create(newState.doc, targetPos)
|
||||
const newTr = newState.tr.setSelection(newSelection)
|
||||
this.view.dispatch(newTr)
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}, 10)
|
||||
}
|
||||
}, 10)
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
// Add row button click handler
|
||||
this.addRowButton.addEventListener('click', (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
this.addTableRowOrColumn(true)
|
||||
})
|
||||
|
||||
// Add column button click handler
|
||||
this.addColumnButton.addEventListener('click', (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
this.addTableRowOrColumn(false)
|
||||
})
|
||||
}
|
||||
|
||||
private bindOverlayHandlers() {
|
||||
if (!this.rowEndpoint || !this.colEndpoint) return
|
||||
this.rowEndpoint.addEventListener('mousedown', (e) => e.preventDefault())
|
||||
this.colEndpoint.addEventListener('mousedown', (e) => e.preventDefault())
|
||||
this.rowEndpoint.addEventListener('click', (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const bounds = getCellSelectionBounds(this.view, this.node)
|
||||
if (!bounds) return
|
||||
this.selectRow(bounds.maxRow)
|
||||
const rect = this.rowEndpoint!.getBoundingClientRect()
|
||||
const position = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }
|
||||
this.actionCallbacks?.onRowActionClick?.({ rowIndex: bounds.maxRow, view: this.view, position })
|
||||
this.scheduleOverlayUpdate()
|
||||
})
|
||||
this.colEndpoint.addEventListener('click', (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const bounds = getCellSelectionBounds(this.view, this.node)
|
||||
if (!bounds) return
|
||||
this.selectColumn(bounds.maxCol)
|
||||
const rect = this.colEndpoint!.getBoundingClientRect()
|
||||
const position = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }
|
||||
this.actionCallbacks?.onColumnActionClick?.({ colIndex: bounds.maxCol, view: this.view, position })
|
||||
this.scheduleOverlayUpdate()
|
||||
})
|
||||
}
|
||||
|
||||
private startSelectionWatcher() {
|
||||
const owner = this.view.dom.ownerDocument || document
|
||||
const handler = () => this.scheduleOverlayUpdate()
|
||||
owner.addEventListener('selectionchange', handler)
|
||||
this.selectionChangeDisposer = () => owner.removeEventListener('selectionchange', handler)
|
||||
this.scheduleOverlayUpdate()
|
||||
}
|
||||
|
||||
private scheduleOverlayUpdate() {
|
||||
if (this.overlayUpdateRafId !== null) {
|
||||
cancelAnimationFrame(this.overlayUpdateRafId)
|
||||
}
|
||||
this.overlayUpdateRafId = requestAnimationFrame(() => {
|
||||
this.overlayUpdateRafId = null
|
||||
this.updateOverlayPositions()
|
||||
})
|
||||
}
|
||||
|
||||
private updateOverlayPositions() {
|
||||
if (!this.rowEndpoint || !this.colEndpoint) return
|
||||
const bounds = getCellSelectionBounds(this.view, this.node)
|
||||
if (!bounds) {
|
||||
this.rowEndpoint.style.display = 'none'
|
||||
this.colEndpoint.style.display = 'none'
|
||||
return
|
||||
}
|
||||
|
||||
const { map, tableStart, maxRow, maxCol } = bounds
|
||||
|
||||
const getCellDomAndRect = (row: number, col: number) => {
|
||||
const cellIndex = row * map.width + col
|
||||
const cellPos = tableStart + map.map[cellIndex]
|
||||
const cellDom = this.view.nodeDOM(cellPos) as HTMLElement | null
|
||||
return {
|
||||
dom: cellDom,
|
||||
rect: cellDom?.getBoundingClientRect()
|
||||
}
|
||||
}
|
||||
|
||||
// Position row endpoint (left side)
|
||||
const bottomLeft = getCellDomAndRect(maxRow, 0)
|
||||
const topLeft = getCellDomAndRect(0, 0)
|
||||
|
||||
if (bottomLeft.dom && bottomLeft.rect && topLeft.rect) {
|
||||
const midY = (bottomLeft.rect.top + bottomLeft.rect.bottom) / 2
|
||||
this.rowEndpoint.style.display = 'flex'
|
||||
const borderWidth = getElementBorderWidth(this.rowEndpoint)
|
||||
this.rowEndpoint.style.left = `${bottomLeft.rect.left - topLeft.rect.left - this.rowEndpoint.getBoundingClientRect().width / 2 + borderWidth.left / 2}px`
|
||||
this.rowEndpoint.style.top = `${midY - topLeft.rect.top - this.rowEndpoint.getBoundingClientRect().height / 2}px`
|
||||
} else {
|
||||
this.rowEndpoint.style.display = 'none'
|
||||
}
|
||||
|
||||
// Position column endpoint (top side)
|
||||
const topRight = getCellDomAndRect(0, maxCol)
|
||||
const topLeftForCol = getCellDomAndRect(0, 0)
|
||||
|
||||
if (topRight.dom && topRight.rect && topLeftForCol.rect) {
|
||||
const midX = topRight.rect.left + topRight.rect.width / 2
|
||||
const borderWidth = getElementBorderWidth(this.colEndpoint)
|
||||
this.colEndpoint.style.display = 'flex'
|
||||
this.colEndpoint.style.left = `${midX - topLeftForCol.rect.left - this.colEndpoint.getBoundingClientRect().width / 2}px`
|
||||
this.colEndpoint.style.top = `${topRight.rect.top - topLeftForCol.rect.top - this.colEndpoint.getBoundingClientRect().height / 2 + borderWidth.top / 2}px`
|
||||
} else {
|
||||
this.colEndpoint.style.display = 'none'
|
||||
}
|
||||
}
|
||||
|
||||
setSelectionToTable() {
|
||||
const { state } = this.view
|
||||
|
||||
let tablePos = -1
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if (tablePos >= 0) {
|
||||
const firstCellPos = tablePos + 3
|
||||
const selection = TextSelection.create(state.doc, firstCellPos)
|
||||
const tr = state.tr.setSelection(selection)
|
||||
this.view.dispatch(tr)
|
||||
}
|
||||
}
|
||||
|
||||
setSelectionToLastRow() {
|
||||
const { state } = this.view
|
||||
|
||||
let tablePos = -1
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if (tablePos >= 0) {
|
||||
const map = TableMap.get(this.node)
|
||||
const lastRowIndex = map.height - 1
|
||||
const lastRowFirstCell = map.map[lastRowIndex * map.width]
|
||||
const lastRowFirstCellPos = tablePos + 1 + lastRowFirstCell
|
||||
|
||||
const selection = TextSelection.create(state.doc, lastRowFirstCellPos)
|
||||
const tr = state.tr.setSelection(selection)
|
||||
this.view.dispatch(tr)
|
||||
}
|
||||
}
|
||||
|
||||
setSelectionToLastColumn() {
|
||||
const { state } = this.view
|
||||
|
||||
let tablePos = -1
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if (tablePos >= 0) {
|
||||
const map = TableMap.get(this.node)
|
||||
const lastColumnIndex = map.width - 1
|
||||
const lastColumnFirstCell = map.map[lastColumnIndex]
|
||||
const lastColumnFirstCellPos = tablePos + 1 + lastColumnFirstCell
|
||||
|
||||
const selection = TextSelection.create(state.doc, lastColumnFirstCellPos)
|
||||
const tr = state.tr.setSelection(selection)
|
||||
this.view.dispatch(tr)
|
||||
}
|
||||
}
|
||||
|
||||
// selection triggers moved to decorations plugin
|
||||
|
||||
hasTableCellSelection(): boolean {
|
||||
const selection = this.view.state.selection
|
||||
return isCellSelection(selection)
|
||||
}
|
||||
|
||||
selectRow(rowIndex: number) {
|
||||
const { state, dispatch } = this.view
|
||||
|
||||
// Find the table position
|
||||
let tablePos = -1
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if (tablePos >= 0) {
|
||||
const map = TableMap.get(this.node)
|
||||
const firstCellInRow = map.map[rowIndex * map.width]
|
||||
const lastCellInRow = map.map[rowIndex * map.width + map.width - 1]
|
||||
|
||||
const firstCellPos = tablePos + 1 + firstCellInRow
|
||||
const lastCellPos = tablePos + 1 + lastCellInRow
|
||||
|
||||
const selection = CellSelection.create(state.doc, firstCellPos, lastCellPos)
|
||||
const tr = state.tr.setSelection(selection)
|
||||
dispatch(tr)
|
||||
}
|
||||
}
|
||||
|
||||
selectColumn(colIndex: number) {
|
||||
const { state, dispatch } = this.view
|
||||
|
||||
// Find the table position
|
||||
let tablePos = -1
|
||||
state.doc.descendants((node: ProseMirrorNode, pos: number) => {
|
||||
if (node.type.name === 'table' && node === this.node) {
|
||||
tablePos = pos
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if (tablePos >= 0) {
|
||||
const map = TableMap.get(this.node)
|
||||
const firstCellInCol = map.map[colIndex]
|
||||
const lastCellInCol = map.map[(map.height - 1) * map.width + colIndex]
|
||||
|
||||
const firstCellPos = tablePos + 1 + firstCellInCol
|
||||
const lastCellPos = tablePos + 1 + lastCellInCol
|
||||
|
||||
const selection = CellSelection.create(state.doc, firstCellPos, lastCellPos)
|
||||
const tr = state.tr.setSelection(selection)
|
||||
dispatch(tr)
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.addRowButton?.remove()
|
||||
this.addColumnButton?.remove()
|
||||
if (this.rowEndpoint) this.rowEndpoint.remove()
|
||||
if (this.colEndpoint) this.colEndpoint.remove()
|
||||
if (this.selectionChangeDisposer) this.selectionChangeDisposer()
|
||||
if (this.overlayUpdateRafId !== null) cancelAnimationFrame(this.overlayUpdateRafId)
|
||||
}
|
||||
}
|
||||
3
packages/extension-table-plus/src/table/index.ts
Executable file
3
packages/extension-table-plus/src/table/index.ts
Executable file
@ -0,0 +1,3 @@
|
||||
export * from './table.js'
|
||||
export * from './utilities/createColGroup.js'
|
||||
export * from './utilities/createTable.js'
|
||||
486
packages/extension-table-plus/src/table/table.ts
Executable file
486
packages/extension-table-plus/src/table/table.ts
Executable file
@ -0,0 +1,486 @@
|
||||
import '../types.js'
|
||||
|
||||
import { callOrReturn, getExtensionField, mergeAttributes, Node } from '@tiptap/core'
|
||||
import type { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
|
||||
import { TextSelection } from '@tiptap/pm/state'
|
||||
import {
|
||||
addColumnAfter,
|
||||
addColumnBefore,
|
||||
addRowAfter,
|
||||
addRowBefore,
|
||||
CellSelection,
|
||||
columnResizing,
|
||||
deleteColumn,
|
||||
deleteRow,
|
||||
deleteTable,
|
||||
fixTables,
|
||||
goToNextCell,
|
||||
mergeCells,
|
||||
setCellAttr,
|
||||
splitCell,
|
||||
tableEditing,
|
||||
toggleHeader,
|
||||
toggleHeaderCell
|
||||
} from '@tiptap/pm/tables'
|
||||
import { type EditorView, type NodeView } from '@tiptap/pm/view'
|
||||
|
||||
import { TableView } from './TableView.js'
|
||||
import { createColGroup } from './utilities/createColGroup.js'
|
||||
import { createTable } from './utilities/createTable.js'
|
||||
import { deleteTableWhenAllCellsSelected } from './utilities/deleteTableWhenAllCellsSelected.js'
|
||||
|
||||
export interface TableOptions {
|
||||
/**
|
||||
* HTML attributes for the table element.
|
||||
* @default {}
|
||||
* @example { class: 'foo' }
|
||||
*/
|
||||
HTMLAttributes: Record<string, any>
|
||||
|
||||
/**
|
||||
* Enables the resizing of tables.
|
||||
* @default false
|
||||
* @example true
|
||||
*/
|
||||
resizable: boolean
|
||||
|
||||
/**
|
||||
* The width of the resize handle.
|
||||
* @default 5
|
||||
* @example 10
|
||||
*/
|
||||
handleWidth: number
|
||||
|
||||
/**
|
||||
* The minimum width of a cell.
|
||||
* @default 25
|
||||
* @example 50
|
||||
*/
|
||||
cellMinWidth: number
|
||||
|
||||
/**
|
||||
* The node view to render the table.
|
||||
* @default TableView
|
||||
*/
|
||||
View: (new (node: ProseMirrorNode, cellMinWidth: number, view: EditorView) => NodeView) | null
|
||||
|
||||
/**
|
||||
* Enables the resizing of the last column.
|
||||
* @default true
|
||||
* @example false
|
||||
*/
|
||||
lastColumnResizable: boolean
|
||||
|
||||
/**
|
||||
* Allow table node selection.
|
||||
* @default false
|
||||
* @example true
|
||||
*/
|
||||
allowTableNodeSelection: boolean
|
||||
|
||||
/**
|
||||
* Optional callbacks for row/column action triggers
|
||||
*/
|
||||
onRowActionClick?: (args: { rowIndex: number; view: EditorView; position?: { x: number; y: number } }) => void
|
||||
onColumnActionClick?: (args: { colIndex: number; view: EditorView; position?: { x: number; y: number } }) => void
|
||||
}
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface Commands<ReturnType> {
|
||||
table: {
|
||||
/**
|
||||
* Insert a table
|
||||
* @param options The table attributes
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
|
||||
*/
|
||||
insertTable: (options?: { rows?: number; cols?: number; withHeaderRow?: boolean }) => ReturnType
|
||||
|
||||
/**
|
||||
* Add a column before the current column
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.addColumnBefore()
|
||||
*/
|
||||
addColumnBefore: () => ReturnType
|
||||
|
||||
/**
|
||||
* Add a column after the current column
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.addColumnAfter()
|
||||
*/
|
||||
addColumnAfter: () => ReturnType
|
||||
|
||||
/**
|
||||
* Delete the current column
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.deleteColumn()
|
||||
*/
|
||||
deleteColumn: () => ReturnType
|
||||
|
||||
/**
|
||||
* Add a row before the current row
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.addRowBefore()
|
||||
*/
|
||||
addRowBefore: () => ReturnType
|
||||
|
||||
/**
|
||||
* Add a row after the current row
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.addRowAfter()
|
||||
*/
|
||||
addRowAfter: () => ReturnType
|
||||
|
||||
/**
|
||||
* Delete the current row
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.deleteRow()
|
||||
*/
|
||||
deleteRow: () => ReturnType
|
||||
|
||||
/**
|
||||
* Delete the current table
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.deleteTable()
|
||||
*/
|
||||
deleteTable: () => ReturnType
|
||||
|
||||
/**
|
||||
* Merge the currently selected cells
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.mergeCells()
|
||||
*/
|
||||
mergeCells: () => ReturnType
|
||||
|
||||
/**
|
||||
* Split the currently selected cell
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.splitCell()
|
||||
*/
|
||||
splitCell: () => ReturnType
|
||||
|
||||
/**
|
||||
* Toggle the header column
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.toggleHeaderColumn()
|
||||
*/
|
||||
toggleHeaderColumn: () => ReturnType
|
||||
|
||||
/**
|
||||
* Toggle the header row
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.toggleHeaderRow()
|
||||
*/
|
||||
toggleHeaderRow: () => ReturnType
|
||||
|
||||
/**
|
||||
* Toggle the header cell
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.toggleHeaderCell()
|
||||
*/
|
||||
toggleHeaderCell: () => ReturnType
|
||||
|
||||
/**
|
||||
* Merge or split the currently selected cells
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.mergeOrSplit()
|
||||
*/
|
||||
mergeOrSplit: () => ReturnType
|
||||
|
||||
/**
|
||||
* Set a cell attribute
|
||||
* @param name The attribute name
|
||||
* @param value The attribute value
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.setCellAttribute('align', 'right')
|
||||
*/
|
||||
setCellAttribute: (name: string, value: any) => ReturnType
|
||||
|
||||
/**
|
||||
* Moves the selection to the next cell
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.goToNextCell()
|
||||
*/
|
||||
goToNextCell: () => ReturnType
|
||||
|
||||
/**
|
||||
* Moves the selection to the previous cell
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.goToPreviousCell()
|
||||
*/
|
||||
goToPreviousCell: () => ReturnType
|
||||
|
||||
/**
|
||||
* Try to fix the table structure if necessary
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.fixTables()
|
||||
*/
|
||||
fixTables: () => ReturnType
|
||||
|
||||
/**
|
||||
* Set a cell selection inside the current table
|
||||
* @param position The cell position
|
||||
* @returns True if the command was successful, otherwise false
|
||||
* @example editor.commands.setCellSelection({ anchorCell: 1, headCell: 2 })
|
||||
*/
|
||||
setCellSelection: (position: { anchorCell: number; headCell?: number }) => ReturnType
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This extension allows you to create tables.
|
||||
* @see https://www.tiptap.dev/api/nodes/table
|
||||
*/
|
||||
export const Table = Node.create<TableOptions>({
|
||||
name: 'table',
|
||||
|
||||
// @ts-ignore - TODO: fix
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {},
|
||||
resizable: false,
|
||||
handleWidth: 5,
|
||||
cellMinWidth: 25,
|
||||
// TODO: fix
|
||||
View: TableView,
|
||||
lastColumnResizable: true,
|
||||
allowTableNodeSelection: false
|
||||
}
|
||||
},
|
||||
|
||||
content: 'tableRow+',
|
||||
|
||||
tableRole: 'table',
|
||||
|
||||
isolating: true,
|
||||
|
||||
group: 'block',
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'table' }]
|
||||
},
|
||||
|
||||
renderHTML({ node, HTMLAttributes }) {
|
||||
const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth)
|
||||
|
||||
const table: DOMOutputSpec = [
|
||||
'table',
|
||||
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
||||
style: tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`
|
||||
}),
|
||||
colgroup,
|
||||
['tbody', 0]
|
||||
]
|
||||
|
||||
return table
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
insertTable:
|
||||
({ rows = 3, cols = 3, withHeaderRow = true } = {}) =>
|
||||
({ tr, dispatch, editor }) => {
|
||||
// Disallow inserting table inside nested nodes when TableCell option allowNestedNodes is false
|
||||
const tableCellExtension = this.editor.extensionManager.extensions.find((ext) => ext.name === 'tableCell')
|
||||
const allowNestedNodes: boolean = tableCellExtension
|
||||
? Boolean((tableCellExtension.options as { allowNestedNodes?: boolean }).allowNestedNodes)
|
||||
: false
|
||||
|
||||
if (!allowNestedNodes) {
|
||||
const { $from } = tr.selection
|
||||
// Only allow table insertion at top-level (depth <= 1),
|
||||
// disallow when selection is inside any nested node (list, blockquote, table, etc.)
|
||||
if ($from.depth > 1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const node = createTable(editor.schema, rows, cols, withHeaderRow)
|
||||
|
||||
if (dispatch) {
|
||||
const offset = tr.selection.from + 1
|
||||
|
||||
tr.replaceSelectionWith(node)
|
||||
.scrollIntoView()
|
||||
.setSelection(TextSelection.near(tr.doc.resolve(offset)))
|
||||
}
|
||||
|
||||
return true
|
||||
},
|
||||
addColumnBefore:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return addColumnBefore(state, dispatch)
|
||||
},
|
||||
addColumnAfter:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return addColumnAfter(state, dispatch)
|
||||
},
|
||||
deleteColumn:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return deleteColumn(state, dispatch)
|
||||
},
|
||||
addRowBefore:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return addRowBefore(state, dispatch)
|
||||
},
|
||||
addRowAfter:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return addRowAfter(state, dispatch)
|
||||
},
|
||||
deleteRow:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return deleteRow(state, dispatch)
|
||||
},
|
||||
deleteTable:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return deleteTable(state, dispatch)
|
||||
},
|
||||
mergeCells:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return mergeCells(state, dispatch)
|
||||
},
|
||||
splitCell:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return splitCell(state, dispatch)
|
||||
},
|
||||
toggleHeaderColumn:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return toggleHeader('column')(state, dispatch)
|
||||
},
|
||||
toggleHeaderRow:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return toggleHeader('row')(state, dispatch)
|
||||
},
|
||||
toggleHeaderCell:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return toggleHeaderCell(state, dispatch)
|
||||
},
|
||||
mergeOrSplit:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
if (mergeCells(state, dispatch)) {
|
||||
return true
|
||||
}
|
||||
|
||||
return splitCell(state, dispatch)
|
||||
},
|
||||
setCellAttribute:
|
||||
(name, value) =>
|
||||
({ state, dispatch }) => {
|
||||
return setCellAttr(name, value)(state, dispatch)
|
||||
},
|
||||
goToNextCell:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return goToNextCell(1)(state, dispatch)
|
||||
},
|
||||
goToPreviousCell:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
return goToNextCell(-1)(state, dispatch)
|
||||
},
|
||||
fixTables:
|
||||
() =>
|
||||
({ state, dispatch }) => {
|
||||
if (dispatch) {
|
||||
fixTables(state)
|
||||
}
|
||||
|
||||
return true
|
||||
},
|
||||
setCellSelection:
|
||||
(position) =>
|
||||
({ tr, dispatch }) => {
|
||||
if (dispatch) {
|
||||
const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell)
|
||||
|
||||
// @ts-ignore - TODO: fix
|
||||
tr.setSelection(selection)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
addNodeView() {
|
||||
return (props) => {
|
||||
const { node, view } = props
|
||||
const ViewClass = this.options.View || TableView
|
||||
if (ViewClass === TableView) {
|
||||
return new TableView(node, this.options.cellMinWidth, view, {
|
||||
onRowActionClick: this.options.onRowActionClick,
|
||||
onColumnActionClick: this.options.onColumnActionClick
|
||||
})
|
||||
}
|
||||
return new ViewClass(node, this.options.cellMinWidth, view)
|
||||
}
|
||||
},
|
||||
|
||||
addKeyboardShortcuts() {
|
||||
return {
|
||||
Tab: () => {
|
||||
if (this.editor.commands.goToNextCell()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!this.editor.can().addRowAfter()) {
|
||||
return false
|
||||
}
|
||||
|
||||
return this.editor.chain().addRowAfter().goToNextCell().run()
|
||||
},
|
||||
'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
|
||||
Backspace: deleteTableWhenAllCellsSelected,
|
||||
'Mod-Backspace': deleteTableWhenAllCellsSelected,
|
||||
Delete: deleteTableWhenAllCellsSelected,
|
||||
'Mod-Delete': deleteTableWhenAllCellsSelected
|
||||
}
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
const isResizable = this.options.resizable && this.editor.isEditable
|
||||
|
||||
return [
|
||||
...(isResizable
|
||||
? [
|
||||
columnResizing({
|
||||
handleWidth: this.options.handleWidth,
|
||||
cellMinWidth: this.options.cellMinWidth,
|
||||
defaultCellMinWidth: this.options.cellMinWidth,
|
||||
View: this.options.View,
|
||||
lastColumnResizable: this.options.lastColumnResizable
|
||||
})
|
||||
]
|
||||
: []),
|
||||
tableEditing({
|
||||
allowTableNodeSelection: this.options.allowTableNodeSelection
|
||||
})
|
||||
]
|
||||
},
|
||||
|
||||
extendNodeSchema(extension) {
|
||||
const context = {
|
||||
name: extension.name,
|
||||
options: extension.options,
|
||||
storage: extension.storage
|
||||
}
|
||||
|
||||
return {
|
||||
tableRole: callOrReturn(getExtensionField(extension, 'tableRole', context))
|
||||
}
|
||||
}
|
||||
})
|
||||
9
packages/extension-table-plus/src/table/utilities/colStyle.ts
Executable file
9
packages/extension-table-plus/src/table/utilities/colStyle.ts
Executable file
@ -0,0 +1,9 @@
|
||||
export function getColStyleDeclaration(minWidth: number, width: number | undefined): [string, string] {
|
||||
if (width) {
|
||||
// apply the stored width unless it is below the configured minimum cell width
|
||||
return ['width', `${Math.max(width, minWidth)}px`]
|
||||
}
|
||||
|
||||
// set the minimum with on the column if it has no stored width
|
||||
return ['min-width', `${minWidth}px`]
|
||||
}
|
||||
12
packages/extension-table-plus/src/table/utilities/createCell.ts
Executable file
12
packages/extension-table-plus/src/table/utilities/createCell.ts
Executable file
@ -0,0 +1,12 @@
|
||||
import type { Fragment, Node as ProsemirrorNode, NodeType } from '@tiptap/pm/model'
|
||||
|
||||
export function createCell(
|
||||
cellType: NodeType,
|
||||
cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>
|
||||
): ProsemirrorNode | null | undefined {
|
||||
if (cellContent) {
|
||||
return cellType.createChecked(null, cellContent)
|
||||
}
|
||||
|
||||
return cellType.createAndFill()
|
||||
}
|
||||
68
packages/extension-table-plus/src/table/utilities/createColGroup.ts
Executable file
68
packages/extension-table-plus/src/table/utilities/createColGroup.ts
Executable file
@ -0,0 +1,68 @@
|
||||
import type { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
|
||||
|
||||
import { getColStyleDeclaration } from './colStyle.js'
|
||||
|
||||
export type ColGroup =
|
||||
| {
|
||||
colgroup: DOMOutputSpec
|
||||
tableWidth: string
|
||||
tableMinWidth: string
|
||||
}
|
||||
| Record<string, never>
|
||||
|
||||
/**
|
||||
* Creates a colgroup element for a table node in ProseMirror.
|
||||
*
|
||||
* @param node - The ProseMirror node representing the table.
|
||||
* @param cellMinWidth - The minimum width of a cell in the table.
|
||||
* @param overrideCol - (Optional) The index of the column to override the width of.
|
||||
* @param overrideValue - (Optional) The width value to use for the overridden column.
|
||||
* @returns An object containing the colgroup element, the total width of the table, and the minimum width of the table.
|
||||
*/
|
||||
export function createColGroup(node: ProseMirrorNode, cellMinWidth: number): ColGroup
|
||||
export function createColGroup(
|
||||
node: ProseMirrorNode,
|
||||
cellMinWidth: number,
|
||||
overrideCol: number,
|
||||
overrideValue: number
|
||||
): ColGroup
|
||||
export function createColGroup(
|
||||
node: ProseMirrorNode,
|
||||
cellMinWidth: number,
|
||||
overrideCol?: number,
|
||||
overrideValue?: number
|
||||
): ColGroup {
|
||||
let totalWidth = 0
|
||||
let fixedWidth = true
|
||||
const cols: DOMOutputSpec[] = []
|
||||
const row = node.firstChild
|
||||
|
||||
if (!row) {
|
||||
return {}
|
||||
}
|
||||
|
||||
for (let i = 0, col = 0; i < row.childCount; i += 1) {
|
||||
const { colspan, colwidth } = row.child(i).attrs
|
||||
|
||||
for (let j = 0; j < colspan; j += 1, col += 1) {
|
||||
const hasWidth = overrideCol === col ? overrideValue : colwidth && (colwidth[j] as number | undefined)
|
||||
|
||||
totalWidth += hasWidth || cellMinWidth
|
||||
|
||||
if (!hasWidth) {
|
||||
fixedWidth = false
|
||||
}
|
||||
|
||||
const [property, value] = getColStyleDeclaration(cellMinWidth, hasWidth)
|
||||
|
||||
cols.push(['col', { style: `${property}: ${value}` }])
|
||||
}
|
||||
}
|
||||
|
||||
const tableWidth = fixedWidth ? `${totalWidth}px` : ''
|
||||
const tableMinWidth = fixedWidth ? '' : `${totalWidth}px`
|
||||
|
||||
const colgroup: DOMOutputSpec = ['colgroup', {}, ...cols]
|
||||
|
||||
return { colgroup, tableWidth, tableMinWidth }
|
||||
}
|
||||
40
packages/extension-table-plus/src/table/utilities/createTable.ts
Executable file
40
packages/extension-table-plus/src/table/utilities/createTable.ts
Executable file
@ -0,0 +1,40 @@
|
||||
import type { Fragment, Node as ProsemirrorNode, Schema } from '@tiptap/pm/model'
|
||||
|
||||
import { createCell } from './createCell.js'
|
||||
import { getTableNodeTypes } from './getTableNodeTypes.js'
|
||||
|
||||
export function createTable(
|
||||
schema: Schema,
|
||||
rowsCount: number,
|
||||
colsCount: number,
|
||||
withHeaderRow: boolean,
|
||||
cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>
|
||||
): ProsemirrorNode {
|
||||
const types = getTableNodeTypes(schema)
|
||||
const headerCells: ProsemirrorNode[] = []
|
||||
const cells: ProsemirrorNode[] = []
|
||||
|
||||
for (let index = 0; index < colsCount; index += 1) {
|
||||
const cell = createCell(types.cell, cellContent)
|
||||
|
||||
if (cell) {
|
||||
cells.push(cell)
|
||||
}
|
||||
|
||||
if (withHeaderRow) {
|
||||
const headerCell = createCell(types.header_cell, cellContent)
|
||||
|
||||
if (headerCell) {
|
||||
headerCells.push(headerCell)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rows: ProsemirrorNode[] = []
|
||||
|
||||
for (let index = 0; index < rowsCount; index += 1) {
|
||||
rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells))
|
||||
}
|
||||
|
||||
return types.table.createChecked(null, rows)
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
import type { KeyboardShortcutCommand } from '@tiptap/core'
|
||||
import { findParentNodeClosestToPos } from '@tiptap/core'
|
||||
|
||||
import { isCellSelection } from './isCellSelection.js'
|
||||
|
||||
export const deleteTableWhenAllCellsSelected: KeyboardShortcutCommand = ({ editor }) => {
|
||||
const { selection } = editor.state
|
||||
|
||||
if (!isCellSelection(selection)) {
|
||||
return false
|
||||
}
|
||||
|
||||
let cellCount = 0
|
||||
const table = findParentNodeClosestToPos(selection.ranges[0].$from, (node) => {
|
||||
return node.type.name === 'table'
|
||||
})
|
||||
|
||||
table?.node.descendants((node) => {
|
||||
if (node.type.name === 'table') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (['tableCell', 'tableHeader'].includes(node.type.name)) {
|
||||
cellCount += 1
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
const allCellsSelected = cellCount === selection.ranges.length
|
||||
|
||||
if (!allCellsSelected) {
|
||||
return false
|
||||
}
|
||||
|
||||
editor.commands.deleteTable()
|
||||
|
||||
return true
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
export function getElementBorderWidth(element: HTMLElement): {
|
||||
top: number
|
||||
right: number
|
||||
bottom: number
|
||||
left: number
|
||||
} {
|
||||
const style = window.getComputedStyle(element)
|
||||
return {
|
||||
top: parseFloat(style.borderTopWidth),
|
||||
right: parseFloat(style.borderRightWidth),
|
||||
bottom: parseFloat(style.borderBottomWidth),
|
||||
left: parseFloat(style.borderLeftWidth)
|
||||
}
|
||||
}
|
||||
21
packages/extension-table-plus/src/table/utilities/getTableNodeTypes.ts
Executable file
21
packages/extension-table-plus/src/table/utilities/getTableNodeTypes.ts
Executable file
@ -0,0 +1,21 @@
|
||||
import type { NodeType, Schema } from '@tiptap/pm/model'
|
||||
|
||||
export function getTableNodeTypes(schema: Schema): { [key: string]: NodeType } {
|
||||
if (schema.cached.tableNodeTypes) {
|
||||
return schema.cached.tableNodeTypes
|
||||
}
|
||||
|
||||
const roles: { [key: string]: NodeType } = {}
|
||||
|
||||
Object.keys(schema.nodes).forEach((type) => {
|
||||
const nodeType = schema.nodes[type]
|
||||
|
||||
if (nodeType.spec.tableRole) {
|
||||
roles[nodeType.spec.tableRole] = nodeType
|
||||
}
|
||||
})
|
||||
|
||||
schema.cached.tableNodeTypes = roles
|
||||
|
||||
return roles
|
||||
}
|
||||
5
packages/extension-table-plus/src/table/utilities/isCellSelection.ts
Executable file
5
packages/extension-table-plus/src/table/utilities/isCellSelection.ts
Executable file
@ -0,0 +1,5 @@
|
||||
import { CellSelection } from '@tiptap/pm/tables'
|
||||
|
||||
export function isCellSelection(value: unknown): value is CellSelection {
|
||||
return value instanceof CellSelection
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
||||
import { CellSelection, TableMap } from '@tiptap/pm/tables'
|
||||
import type { EditorView } from '@tiptap/pm/view'
|
||||
|
||||
export interface SelectionBounds {
|
||||
tablePos: number
|
||||
tableStart: number
|
||||
map: ReturnType<typeof TableMap.get>
|
||||
minRow: number
|
||||
maxRow: number
|
||||
minCol: number
|
||||
maxCol: number
|
||||
topLeftPos: number
|
||||
topRightPos: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute logical bounds for current CellSelection inside the provided table node.
|
||||
* Returns null if current selection is not a CellSelection or not within the table node.
|
||||
*/
|
||||
export function getCellSelectionBounds(view: EditorView, tableNode: ProseMirrorNode): SelectionBounds | null {
|
||||
const selection = view.state.selection
|
||||
if (!(selection instanceof CellSelection)) return null
|
||||
|
||||
const $anchor = selection.$anchorCell || selection.$anchor
|
||||
let tablePos = -1
|
||||
let currentTable: ProseMirrorNode | null = null
|
||||
for (let d = $anchor.depth; d > 0; d--) {
|
||||
const n = $anchor.node(d)
|
||||
const role = (n.type.spec as { tableRole?: string } | undefined)?.tableRole
|
||||
if (n.type.name === 'table' || role === 'table') {
|
||||
tablePos = $anchor.before(d)
|
||||
currentTable = n
|
||||
break
|
||||
}
|
||||
}
|
||||
if (tablePos < 0 || currentTable !== tableNode) return null
|
||||
|
||||
const map = TableMap.get(tableNode)
|
||||
const tableStart = tablePos + 1
|
||||
|
||||
let minRow = Number.POSITIVE_INFINITY
|
||||
let maxRow = Number.NEGATIVE_INFINITY
|
||||
let minCol = Number.POSITIVE_INFINITY
|
||||
let maxCol = Number.NEGATIVE_INFINITY
|
||||
let topLeftPos: number | null = null
|
||||
let topRightPos: number | null = null
|
||||
|
||||
selection.forEachCell((_cell, pos) => {
|
||||
const rect = map.findCell(pos - tableStart)
|
||||
if (rect.top < minRow) minRow = rect.top
|
||||
if (rect.left < minCol) minCol = rect.left
|
||||
if (rect.bottom - 1 > maxRow) maxRow = rect.bottom - 1
|
||||
if (rect.right - 1 > maxCol) maxCol = rect.right - 1
|
||||
|
||||
if (rect.top === minRow && rect.left === minCol) {
|
||||
if (topLeftPos === null || pos < topLeftPos) topLeftPos = pos
|
||||
}
|
||||
if (rect.top === minRow && rect.right - 1 === maxCol) {
|
||||
if (topRightPos === null || pos < topRightPos) topRightPos = pos
|
||||
}
|
||||
})
|
||||
|
||||
if (!isFinite(minRow) || !isFinite(minCol) || topLeftPos == null) return null
|
||||
if (topRightPos == null) topRightPos = topLeftPos
|
||||
|
||||
return { tablePos, tableStart, map, minRow, maxRow, minCol, maxCol, topLeftPos, topRightPos }
|
||||
}
|
||||
19
packages/extension-table-plus/src/types.ts
Executable file
19
packages/extension-table-plus/src/types.ts
Executable file
@ -0,0 +1,19 @@
|
||||
import type { ParentConfig } from '@tiptap/core'
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface NodeConfig<Options, Storage> {
|
||||
/**
|
||||
* A string or function to determine the role of the table.
|
||||
* @default 'table'
|
||||
* @example () => 'table'
|
||||
*/
|
||||
tableRole?:
|
||||
| string
|
||||
| ((this: {
|
||||
name: string
|
||||
options: Options
|
||||
storage: Storage
|
||||
parent: ParentConfig<NodeConfig<Options>>['tableRole']
|
||||
}) => string)
|
||||
}
|
||||
}
|
||||
20
packages/extension-table-plus/tsdown.config.ts
Executable file
20
packages/extension-table-plus/tsdown.config.ts
Executable file
@ -0,0 +1,20 @@
|
||||
import { defineConfig } from 'tsdown'
|
||||
|
||||
export default defineConfig(
|
||||
[
|
||||
'src/table/index.ts',
|
||||
'src/cell/index.ts',
|
||||
'src/header/index.ts',
|
||||
'src/kit/index.ts',
|
||||
'src/row/index.ts',
|
||||
'src/index.ts'
|
||||
].map((entry) => ({
|
||||
entry: [entry],
|
||||
tsconfig: '../../tsconfig.build.json',
|
||||
outDir: `dist${entry.replace('src', '').split('/').slice(0, -1).join('/')}`,
|
||||
dts: true,
|
||||
sourcemap: true,
|
||||
format: ['esm', 'cjs'],
|
||||
external: [/^[^./]/]
|
||||
}))
|
||||
)
|
||||
26
packages/mcp-trace/trace-core/core/spanConvert.ts
Normal file
26
packages/mcp-trace/trace-core/core/spanConvert.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { SpanKind, SpanStatusCode } from '@opentelemetry/api'
|
||||
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
import { SpanEntity } from '../types/config'
|
||||
|
||||
/**
|
||||
* convert ReadableSpan to SpanEntity
|
||||
* @param span ReadableSpan
|
||||
* @returns SpanEntity
|
||||
*/
|
||||
export function convertSpanToSpanEntity(span: ReadableSpan): SpanEntity {
|
||||
return {
|
||||
id: span.spanContext().spanId,
|
||||
traceId: span.spanContext().traceId,
|
||||
parentId: span.parentSpanContext?.spanId || '',
|
||||
name: span.name,
|
||||
startTime: span.startTime[0] * 1e3 + Math.floor(span.startTime[1] / 1e6), // 转为毫秒
|
||||
endTime: span.endTime ? span.endTime[0] * 1e3 + Math.floor(span.endTime[1] / 1e6) : undefined, // 转为毫秒
|
||||
attributes: { ...span.attributes },
|
||||
status: SpanStatusCode[span.status.code],
|
||||
events: span.events,
|
||||
kind: SpanKind[span.kind],
|
||||
links: span.links,
|
||||
modelName: span.attributes?.modelName
|
||||
} as SpanEntity
|
||||
}
|
||||
7
packages/mcp-trace/trace-core/core/traceCache.ts
Normal file
7
packages/mcp-trace/trace-core/core/traceCache.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
export interface TraceCache {
|
||||
createSpan: (span: ReadableSpan) => void
|
||||
endSpan: (span: ReadableSpan) => void
|
||||
clear: () => void
|
||||
}
|
||||
163
packages/mcp-trace/trace-core/core/traceMethod.ts
Normal file
163
packages/mcp-trace/trace-core/core/traceMethod.ts
Normal file
@ -0,0 +1,163 @@
|
||||
import 'reflect-metadata'
|
||||
|
||||
import { SpanStatusCode, trace } from '@opentelemetry/api'
|
||||
import { context as traceContext } from '@opentelemetry/api'
|
||||
|
||||
import { defaultConfig } from '../types/config'
|
||||
|
||||
export interface SpanDecoratorOptions {
|
||||
spanName?: string
|
||||
traceName?: string
|
||||
tag?: string
|
||||
}
|
||||
|
||||
export function TraceMethod(traced: SpanDecoratorOptions) {
|
||||
return function (target: any, propertyKey?: any, descriptor?: PropertyDescriptor | undefined) {
|
||||
// 兼容静态方法装饰器只传2个参数的情况
|
||||
if (!descriptor) {
|
||||
descriptor = Object.getOwnPropertyDescriptor(target, propertyKey)
|
||||
}
|
||||
if (!descriptor || typeof descriptor.value !== 'function') {
|
||||
throw new Error('TraceMethod can only be applied to methods.')
|
||||
}
|
||||
|
||||
const originalMethod = descriptor.value
|
||||
const traceName = traced.traceName || defaultConfig.defaultTracerName || 'default'
|
||||
const tracer = trace.getTracer(traceName)
|
||||
|
||||
descriptor.value = function (...args: any[]) {
|
||||
const name = traced.spanName || propertyKey
|
||||
return tracer.startActiveSpan(name, async (span) => {
|
||||
try {
|
||||
span.setAttribute('inputs', convertToString(args))
|
||||
span.setAttribute('tags', traced.tag || '')
|
||||
const result = await originalMethod.apply(this, args)
|
||||
span.setAttribute('outputs', convertToString(result))
|
||||
span.setStatus({ code: SpanStatusCode.OK })
|
||||
return result
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error))
|
||||
span.setStatus({
|
||||
code: SpanStatusCode.ERROR,
|
||||
message: err.message
|
||||
})
|
||||
span.recordException(err)
|
||||
throw error
|
||||
} finally {
|
||||
span.end()
|
||||
}
|
||||
})
|
||||
}
|
||||
return descriptor
|
||||
}
|
||||
}
|
||||
|
||||
export function TraceProperty(traced: SpanDecoratorOptions) {
|
||||
return (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => {
|
||||
// 处理箭头函数类属性
|
||||
const traceName = traced.traceName || defaultConfig.defaultTracerName || 'default'
|
||||
const tracer = trace.getTracer(traceName)
|
||||
const name = traced.spanName || propertyKey
|
||||
|
||||
if (!descriptor) {
|
||||
const originalValue = target[propertyKey]
|
||||
|
||||
Object.defineProperty(target, propertyKey, {
|
||||
value: async function (...args: any[]) {
|
||||
const span = tracer.startSpan(name)
|
||||
try {
|
||||
span.setAttribute('inputs', convertToString(args))
|
||||
span.setAttribute('tags', traced.tag || '')
|
||||
const result = await originalValue.apply(this, args)
|
||||
span.setAttribute('outputs', convertToString(result))
|
||||
return result
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error))
|
||||
span.recordException(err)
|
||||
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message })
|
||||
throw error
|
||||
} finally {
|
||||
span.end()
|
||||
}
|
||||
},
|
||||
configurable: true,
|
||||
writable: true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 标准方法装饰器逻辑
|
||||
const originalMethod = descriptor.value
|
||||
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const span = tracer.startSpan(name)
|
||||
try {
|
||||
span.setAttribute('inputs', convertToString(args))
|
||||
span.setAttribute('tags', traced.tag || '')
|
||||
const result = await originalMethod.apply(this, args)
|
||||
span.setAttribute('outputs', convertToString(result))
|
||||
return result
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error))
|
||||
span.recordException(err)
|
||||
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message })
|
||||
throw error
|
||||
} finally {
|
||||
span.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function withSpanFunc<F extends (...args: any[]) => any>(
|
||||
name: string,
|
||||
tag: string,
|
||||
fn: F,
|
||||
args: Parameters<F>
|
||||
): ReturnType<F> {
|
||||
const traceName = defaultConfig.defaultTracerName || 'default'
|
||||
const tracer = trace.getTracer(traceName)
|
||||
const _name = name || fn.name || 'anonymousFunction'
|
||||
return traceContext.with(traceContext.active(), () =>
|
||||
tracer.startActiveSpan(
|
||||
_name,
|
||||
{
|
||||
attributes: {
|
||||
tags: tag || '',
|
||||
inputs: JSON.stringify(args)
|
||||
}
|
||||
},
|
||||
(span) => {
|
||||
// 在这里调用原始函数
|
||||
const result = fn(...args)
|
||||
if (result instanceof Promise) {
|
||||
return result
|
||||
.then((res) => {
|
||||
span.setStatus({ code: SpanStatusCode.OK })
|
||||
span.setAttribute('outputs', convertToString(res))
|
||||
return res
|
||||
})
|
||||
.catch((error) => {
|
||||
const err = error instanceof Error ? error : new Error(String(error))
|
||||
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message })
|
||||
span.recordException(err)
|
||||
throw error
|
||||
})
|
||||
.finally(() => span.end())
|
||||
} else {
|
||||
span.setStatus({ code: SpanStatusCode.OK })
|
||||
span.setAttribute('outputs', convertToString(result))
|
||||
span.end()
|
||||
}
|
||||
return result
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function convertToString(args: any | any[]): string | boolean | number {
|
||||
if (typeof args === 'string' || typeof args === 'boolean' || typeof args === 'number') {
|
||||
return args
|
||||
}
|
||||
return JSON.stringify(args)
|
||||
}
|
||||
26
packages/mcp-trace/trace-core/exporters/FuncSpanExporter.ts
Normal file
26
packages/mcp-trace/trace-core/exporters/FuncSpanExporter.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { ExportResult, ExportResultCode } from '@opentelemetry/core'
|
||||
import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
export type SaveFunction = (spans: ReadableSpan[]) => Promise<void>
|
||||
|
||||
export class FunctionSpanExporter implements SpanExporter {
|
||||
private exportFunction: SaveFunction
|
||||
|
||||
constructor(fn: SaveFunction) {
|
||||
this.exportFunction = fn
|
||||
}
|
||||
|
||||
shutdown(): Promise<void> {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void {
|
||||
this.exportFunction(spans)
|
||||
.then(() => {
|
||||
resultCallback({ code: ExportResultCode.SUCCESS })
|
||||
})
|
||||
.catch((error) => {
|
||||
resultCallback({ code: ExportResultCode.FAILED, error: error })
|
||||
})
|
||||
}
|
||||
}
|
||||
8
packages/mcp-trace/trace-core/index.ts
Normal file
8
packages/mcp-trace/trace-core/index.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export * from './core/spanConvert'
|
||||
export * from './core/traceCache'
|
||||
export * from './core/traceMethod'
|
||||
export * from './exporters/FuncSpanExporter'
|
||||
export * from './processors/CacheSpanProcessor'
|
||||
export * from './processors/EmitterSpanProcessor'
|
||||
export * from './processors/FuncSpanProcessor'
|
||||
export * from './types/config'
|
||||
@ -0,0 +1,40 @@
|
||||
import { Context, trace } from '@opentelemetry/api'
|
||||
import { BatchSpanProcessor, BufferConfig, ReadableSpan, Span, SpanExporter } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
import { TraceCache } from '../core/traceCache'
|
||||
|
||||
export class CacheBatchSpanProcessor extends BatchSpanProcessor {
|
||||
private cache: TraceCache
|
||||
|
||||
constructor(_exporter: SpanExporter, cache: TraceCache, config?: BufferConfig) {
|
||||
super(_exporter, config)
|
||||
this.cache = cache
|
||||
}
|
||||
|
||||
override onEnd(span: ReadableSpan): void {
|
||||
super.onEnd(span)
|
||||
this.cache.endSpan(span)
|
||||
}
|
||||
|
||||
override onStart(span: Span, parentContext: Context): void {
|
||||
super.onStart(span, parentContext)
|
||||
this.cache.createSpan({
|
||||
name: span.name,
|
||||
kind: span.kind,
|
||||
spanContext: () => span.spanContext(),
|
||||
parentSpanContext: trace.getSpanContext(parentContext),
|
||||
startTime: span.startTime,
|
||||
status: span.status,
|
||||
attributes: span.attributes,
|
||||
links: span.links,
|
||||
events: span.events,
|
||||
duration: span.duration,
|
||||
ended: span.ended,
|
||||
resource: span.resource,
|
||||
instrumentationScope: span.instrumentationScope,
|
||||
droppedAttributesCount: span.droppedAttributesCount,
|
||||
droppedEventsCount: span.droppedEventsCount,
|
||||
droppedLinksCount: span.droppedLinksCount
|
||||
} as ReadableSpan)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
import { Context } from '@opentelemetry/api'
|
||||
import { BatchSpanProcessor, BufferConfig, ReadableSpan, Span, SpanExporter } from '@opentelemetry/sdk-trace-base'
|
||||
import { EventEmitter } from 'stream'
|
||||
|
||||
import { convertSpanToSpanEntity } from '../core/spanConvert'
|
||||
|
||||
export const TRACE_DATA_EVENT = 'trace_data_event'
|
||||
export const ON_START = 'start'
|
||||
export const ON_END = 'end'
|
||||
|
||||
export class EmitterSpanProcessor extends BatchSpanProcessor {
|
||||
private emitter: EventEmitter
|
||||
|
||||
constructor(_exporter: SpanExporter, emitter: NodeJS.EventEmitter, config?: BufferConfig) {
|
||||
super(_exporter, config)
|
||||
this.emitter = emitter
|
||||
}
|
||||
|
||||
override onEnd(span: ReadableSpan): void {
|
||||
super.onEnd(span)
|
||||
this.emitter.emit(TRACE_DATA_EVENT, ON_END, convertSpanToSpanEntity(span))
|
||||
}
|
||||
|
||||
override onStart(span: Span, parentContext: Context): void {
|
||||
super.onStart(span, parentContext)
|
||||
this.emitter.emit(TRACE_DATA_EVENT, ON_START, convertSpanToSpanEntity(span))
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
import { Context, trace } from '@opentelemetry/api'
|
||||
import { BatchSpanProcessor, BufferConfig, ReadableSpan, Span, SpanExporter } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
export type SpanFunction = (span: ReadableSpan) => void
|
||||
|
||||
export class FunctionSpanProcessor extends BatchSpanProcessor {
|
||||
private start: SpanFunction
|
||||
private end: SpanFunction
|
||||
|
||||
constructor(_exporter: SpanExporter, start: SpanFunction, end: SpanFunction, config?: BufferConfig) {
|
||||
super(_exporter, config)
|
||||
this.start = start
|
||||
this.end = end
|
||||
}
|
||||
|
||||
override onEnd(span: ReadableSpan): void {
|
||||
super.onEnd(span)
|
||||
this.end(span)
|
||||
}
|
||||
|
||||
override onStart(span: Span, parentContext: Context): void {
|
||||
super.onStart(span, parentContext)
|
||||
this.start({
|
||||
name: span.name,
|
||||
kind: span.kind,
|
||||
spanContext: () => span.spanContext(),
|
||||
parentSpanContext: trace.getSpanContext(parentContext),
|
||||
startTime: span.startTime,
|
||||
status: span.status,
|
||||
attributes: span.attributes,
|
||||
links: span.links,
|
||||
events: span.events,
|
||||
duration: span.duration,
|
||||
ended: span.ended,
|
||||
resource: span.resource,
|
||||
instrumentationScope: span.instrumentationScope,
|
||||
droppedAttributesCount: span.droppedAttributesCount,
|
||||
droppedEventsCount: span.droppedEventsCount,
|
||||
droppedLinksCount: span.droppedLinksCount
|
||||
} as ReadableSpan)
|
||||
}
|
||||
}
|
||||
65
packages/mcp-trace/trace-core/types/config.ts
Normal file
65
packages/mcp-trace/trace-core/types/config.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { Link } from '@opentelemetry/api'
|
||||
import { TimedEvent } from '@opentelemetry/sdk-trace-base'
|
||||
|
||||
export type AttributeValue =
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| Array<null | undefined | string>
|
||||
| Array<null | undefined | number>
|
||||
| Array<null | undefined | boolean>
|
||||
| { [key: string]: string | number | boolean }
|
||||
| Array<null | undefined | { [key: string]: string | number | boolean }>
|
||||
|
||||
export type Attributes = {
|
||||
[key: string]: AttributeValue
|
||||
}
|
||||
|
||||
export interface TelemetryConfig {
|
||||
serviceName: string
|
||||
endpoint?: string
|
||||
headers?: Record<string, string>
|
||||
defaultTracerName?: string
|
||||
}
|
||||
|
||||
export interface TraceConfig extends TelemetryConfig {
|
||||
maxAttributesPerSpan?: number
|
||||
}
|
||||
|
||||
export interface TraceEntity {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
|
||||
export interface TokenUsage {
|
||||
prompt_tokens: number
|
||||
completion_tokens: number
|
||||
total_tokens: number
|
||||
prompt_tokens_details?: {
|
||||
[key: string]: number
|
||||
}
|
||||
}
|
||||
|
||||
export interface SpanEntity {
|
||||
id: string
|
||||
name: string
|
||||
parentId: string
|
||||
traceId: string
|
||||
status: string
|
||||
kind: string
|
||||
attributes: Attributes | undefined
|
||||
isEnd: boolean
|
||||
events: TimedEvent[] | undefined
|
||||
startTime: number
|
||||
endTime: number | null
|
||||
links: Link[] | undefined
|
||||
topicId?: string
|
||||
usage?: TokenUsage
|
||||
modelName?: string
|
||||
}
|
||||
|
||||
export const defaultConfig: TelemetryConfig = {
|
||||
serviceName: 'default',
|
||||
headers: {},
|
||||
defaultTracerName: 'default'
|
||||
}
|
||||
46
packages/mcp-trace/trace-node/nodeTracer.ts
Normal file
46
packages/mcp-trace/trace-node/nodeTracer.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { trace, Tracer } from '@opentelemetry/api'
|
||||
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
|
||||
import { W3CTraceContextPropagator } from '@opentelemetry/core'
|
||||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
|
||||
import { BatchSpanProcessor, ConsoleSpanExporter, SpanProcessor } from '@opentelemetry/sdk-trace-base'
|
||||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
|
||||
|
||||
import { defaultConfig, TraceConfig } from '../trace-core/types/config'
|
||||
|
||||
export class NodeTracer {
|
||||
private static provider: NodeTracerProvider
|
||||
private static defaultTracer: Tracer
|
||||
private static spanProcessor: SpanProcessor
|
||||
|
||||
static init(config?: TraceConfig, spanProcessor?: SpanProcessor) {
|
||||
if (config) {
|
||||
defaultConfig.serviceName = config.serviceName || defaultConfig.serviceName
|
||||
defaultConfig.endpoint = config.endpoint || defaultConfig.endpoint
|
||||
defaultConfig.headers = config.headers || defaultConfig.headers
|
||||
defaultConfig.defaultTracerName = config.defaultTracerName || defaultConfig.defaultTracerName
|
||||
}
|
||||
this.spanProcessor = spanProcessor || new BatchSpanProcessor(this.getExporter())
|
||||
this.provider = new NodeTracerProvider({
|
||||
spanProcessors: [this.spanProcessor]
|
||||
})
|
||||
this.provider.register({
|
||||
propagator: new W3CTraceContextPropagator(),
|
||||
contextManager: new AsyncLocalStorageContextManager()
|
||||
})
|
||||
this.defaultTracer = trace.getTracer(config?.defaultTracerName || 'default')
|
||||
}
|
||||
|
||||
private static getExporter(config?: TraceConfig) {
|
||||
if (config && config.endpoint) {
|
||||
return new OTLPTraceExporter({
|
||||
url: `${config.endpoint}/v1/traces`,
|
||||
headers: config.headers || undefined
|
||||
})
|
||||
}
|
||||
return new ConsoleSpanExporter()
|
||||
}
|
||||
|
||||
public static getTracer() {
|
||||
return this.defaultTracer
|
||||
}
|
||||
}
|
||||
75
packages/mcp-trace/trace-web/TopicContextManager.ts
Normal file
75
packages/mcp-trace/trace-web/TopicContextManager.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { Context, ContextManager, ROOT_CONTEXT } from '@opentelemetry/api'
|
||||
|
||||
export class TopicContextManager implements ContextManager {
|
||||
private topicContextStack: Map<string, Context[]>
|
||||
private _topicContexts: Map<string, Context>
|
||||
|
||||
constructor() {
|
||||
// topicId -> context
|
||||
this.topicContextStack = new Map()
|
||||
this._topicContexts = new Map()
|
||||
}
|
||||
|
||||
// 绑定一个context到topicId
|
||||
startContextForTopic(topicId, context: Context) {
|
||||
const currentContext = this.getCurrentContext(topicId)
|
||||
this._topicContexts.set(topicId, context)
|
||||
if (!this.topicContextStack.has(topicId) && !this.topicContextStack.get(topicId)) {
|
||||
this.topicContextStack.set(topicId, [currentContext])
|
||||
} else {
|
||||
this.topicContextStack.get(topicId)?.push(currentContext)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取topicId对应的context
|
||||
getContextForTopic(topicId) {
|
||||
return this.getCurrentContext(topicId)
|
||||
}
|
||||
|
||||
endContextForTopic(topicId) {
|
||||
const context = this.getHistoryContext(topicId)
|
||||
this._topicContexts.set(topicId, context)
|
||||
}
|
||||
|
||||
cleanContextForTopic(topicId) {
|
||||
this.topicContextStack.delete(topicId)
|
||||
this._topicContexts.delete(topicId)
|
||||
}
|
||||
|
||||
private getHistoryContext(topicId): Context {
|
||||
const hasContext = this.topicContextStack.has(topicId) && this.topicContextStack.get(topicId)
|
||||
const context = hasContext && hasContext.length > 0 && hasContext.pop()
|
||||
return context ? context : ROOT_CONTEXT
|
||||
}
|
||||
|
||||
private getCurrentContext(topicId): Context {
|
||||
const hasContext = this._topicContexts.has(topicId) && this._topicContexts.get(topicId)
|
||||
return hasContext || ROOT_CONTEXT
|
||||
}
|
||||
|
||||
// OpenTelemetry接口实现
|
||||
active() {
|
||||
// 不支持全局active,必须显式传递
|
||||
return ROOT_CONTEXT
|
||||
}
|
||||
|
||||
with(_, fn, thisArg, ...args) {
|
||||
// 直接调用fn,不做全局active切换
|
||||
return fn.apply(thisArg, args)
|
||||
}
|
||||
|
||||
bind(target, context) {
|
||||
// 显式绑定
|
||||
target.__ot_context = context
|
||||
return target
|
||||
}
|
||||
|
||||
enable() {
|
||||
return this
|
||||
}
|
||||
|
||||
disable() {
|
||||
this._topicContexts.clear()
|
||||
return this
|
||||
}
|
||||
}
|
||||
3
packages/mcp-trace/trace-web/index.ts
Normal file
3
packages/mcp-trace/trace-web/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './TopicContextManager'
|
||||
export * from './traceContextPromise'
|
||||
export * from './webTracer'
|
||||
99
packages/mcp-trace/trace-web/traceContextPromise.ts
Normal file
99
packages/mcp-trace/trace-web/traceContextPromise.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import { Context, context } from '@opentelemetry/api'
|
||||
|
||||
const originalPromise = globalThis.Promise
|
||||
|
||||
class TraceContextPromise<T> extends Promise<T> {
|
||||
_context: Context
|
||||
|
||||
constructor(
|
||||
executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void,
|
||||
ctx?: Context
|
||||
) {
|
||||
const capturedContext = ctx || context.active()
|
||||
super((resolve, reject) => {
|
||||
context.with(capturedContext, () => {
|
||||
executor(
|
||||
(value) => context.with(capturedContext, () => resolve(value)),
|
||||
(reason) => context.with(capturedContext, () => reject(reason))
|
||||
)
|
||||
})
|
||||
})
|
||||
this._context = capturedContext
|
||||
}
|
||||
|
||||
// 兼容 Promise.resolve/reject
|
||||
static resolve(): Promise<void>
|
||||
static resolve<T>(value: T | PromiseLike<T>): Promise<T>
|
||||
static resolve<T>(value: T | PromiseLike<T>, ctx?: Context): Promise<T>
|
||||
static resolve<T>(value?: T | PromiseLike<T>, ctx?: Context): Promise<T | void> {
|
||||
return new TraceContextPromise<T | void>((resolve) => resolve(value as T), ctx)
|
||||
}
|
||||
|
||||
static reject<T = never>(reason?: any): Promise<T>
|
||||
static reject<T = never>(reason?: any, ctx?: Context): Promise<T> {
|
||||
return new TraceContextPromise<T>((_, reject) => reject(reason), ctx)
|
||||
}
|
||||
|
||||
static all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]> {
|
||||
// 尝试从缓存获取 context
|
||||
let capturedContext = context.active()
|
||||
const newValues = values.map((v) => {
|
||||
if (v instanceof Promise && !(v instanceof TraceContextPromise)) {
|
||||
return new TraceContextPromise((resolve, reject) => v.then(resolve, reject), capturedContext)
|
||||
} else if (typeof v === 'function') {
|
||||
// 如果 v 是一个 Function,使用 context 传递 trace 上下文
|
||||
return (...args: any[]) => context.with(capturedContext, () => v(...args))
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
})
|
||||
if (Array.isArray(values) && values.length > 0 && values[0] instanceof TraceContextPromise) {
|
||||
capturedContext = (values[0] as TraceContextPromise<any>)._context
|
||||
}
|
||||
return originalPromise.all(newValues) as Promise<T[]>
|
||||
}
|
||||
|
||||
static race<T>(values: (T | PromiseLike<T>)[]): Promise<T> {
|
||||
const capturedContext = context.active()
|
||||
return new TraceContextPromise<T>((resolve, reject) => {
|
||||
originalPromise.race(values).then(
|
||||
(result) => context.with(capturedContext, () => resolve(result)),
|
||||
(err) => context.with(capturedContext, () => reject(err))
|
||||
)
|
||||
}, capturedContext)
|
||||
}
|
||||
|
||||
static allSettled<T>(values: (T | PromiseLike<T>)[]): Promise<PromiseSettledResult<T>[]> {
|
||||
const capturedContext = context.active()
|
||||
return new TraceContextPromise<PromiseSettledResult<T>[]>((resolve, reject) => {
|
||||
originalPromise.allSettled(values).then(
|
||||
(result) => context.with(capturedContext, () => resolve(result)),
|
||||
(err) => context.with(capturedContext, () => reject(err))
|
||||
)
|
||||
}, capturedContext)
|
||||
}
|
||||
|
||||
static any<T>(values: (T | PromiseLike<T>)[]): Promise<T> {
|
||||
const capturedContext = context.active()
|
||||
return new TraceContextPromise<T>((resolve, reject) => {
|
||||
originalPromise.any(values).then(
|
||||
(result) => context.with(capturedContext, () => resolve(result)),
|
||||
(err) => context.with(capturedContext, () => reject(err))
|
||||
)
|
||||
}, capturedContext)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用 TraceContextPromise 替换全局 Promise
|
||||
*/
|
||||
export function instrumentPromises() {
|
||||
globalThis.Promise = TraceContextPromise as unknown as PromiseConstructor
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复原生 Promise
|
||||
*/
|
||||
export function uninstrumentPromises() {
|
||||
globalThis.Promise = originalPromise
|
||||
}
|
||||
46
packages/mcp-trace/trace-web/webTracer.ts
Normal file
46
packages/mcp-trace/trace-web/webTracer.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { W3CTraceContextPropagator } from '@opentelemetry/core'
|
||||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
|
||||
import { BatchSpanProcessor, ConsoleSpanExporter, SpanProcessor } from '@opentelemetry/sdk-trace-base'
|
||||
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'
|
||||
|
||||
import { defaultConfig, TraceConfig } from '../trace-core/types/config'
|
||||
import { TopicContextManager } from './TopicContextManager'
|
||||
|
||||
export const contextManager = new TopicContextManager()
|
||||
|
||||
export class WebTracer {
|
||||
private static provider: WebTracerProvider
|
||||
private static processor: SpanProcessor
|
||||
|
||||
static init(config?: TraceConfig, spanProcessor?: SpanProcessor) {
|
||||
if (config) {
|
||||
defaultConfig.serviceName = config.serviceName || defaultConfig.serviceName
|
||||
defaultConfig.endpoint = config.endpoint || defaultConfig.endpoint
|
||||
defaultConfig.headers = config.headers || defaultConfig.headers
|
||||
defaultConfig.defaultTracerName = config.defaultTracerName || defaultConfig.defaultTracerName
|
||||
}
|
||||
this.processor = spanProcessor || new BatchSpanProcessor(this.getExporter())
|
||||
this.provider = new WebTracerProvider({
|
||||
spanProcessors: [this.processor]
|
||||
})
|
||||
this.provider.register({
|
||||
propagator: new W3CTraceContextPropagator(),
|
||||
contextManager: contextManager
|
||||
})
|
||||
}
|
||||
|
||||
private static getExporter() {
|
||||
if (defaultConfig.endpoint) {
|
||||
return new OTLPTraceExporter({
|
||||
url: `${defaultConfig.endpoint}/v1/traces`,
|
||||
headers: defaultConfig.headers
|
||||
})
|
||||
}
|
||||
return new ConsoleSpanExporter()
|
||||
}
|
||||
}
|
||||
|
||||
export const startContext = contextManager.startContextForTopic.bind(contextManager)
|
||||
export const getContext = contextManager.getContextForTopic.bind(contextManager)
|
||||
export const endContext = contextManager.endContextForTopic.bind(contextManager)
|
||||
export const cleanContext = contextManager.cleanContextForTopic.bind(contextManager)
|
||||
@ -20,6 +20,8 @@ export enum IpcChannel {
|
||||
App_HandleZoomFactor = 'app:handle-zoom-factor',
|
||||
App_Select = 'app:select',
|
||||
App_HasWritePermission = 'app:has-write-permission',
|
||||
App_ResolvePath = 'app:resolve-path',
|
||||
App_IsPathInside = 'app:is-path-inside',
|
||||
App_Copy = 'app:copy',
|
||||
App_SetStopQuitApp = 'app:set-stop-quit-app',
|
||||
App_SetAppDataPath = 'app:set-app-data-path',
|
||||
@ -31,6 +33,10 @@ export enum IpcChannel {
|
||||
App_GetBinaryPath = 'app:get-binary-path',
|
||||
App_InstallUvBinary = 'app:install-uv-binary',
|
||||
App_InstallBunBinary = 'app:install-bun-binary',
|
||||
App_LogToMain = 'app:log-to-main',
|
||||
App_SaveData = 'app:save-data',
|
||||
App_SetFullScreen = 'app:set-full-screen',
|
||||
App_IsFullScreen = 'app:is-full-screen',
|
||||
|
||||
App_MacIsProcessTrusted = 'app:mac-is-process-trusted',
|
||||
App_MacRequestProcessTrust = 'app:mac-request-process-trust',
|
||||
@ -75,7 +81,6 @@ export enum IpcChannel {
|
||||
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',
|
||||
|
||||
@ -111,10 +116,13 @@ export enum IpcChannel {
|
||||
|
||||
// VertexAI
|
||||
VertexAI_GetAuthHeaders = 'vertexai:get-auth-headers',
|
||||
VertexAI_GetAccessToken = 'vertexai:get-access-token',
|
||||
VertexAI_ClearAuthCache = 'vertexai:clear-auth-cache',
|
||||
|
||||
Windows_ResetMinimumSize = 'window:reset-minimum-size',
|
||||
Windows_SetMinimumSize = 'window:set-minimum-size',
|
||||
Windows_Resize = 'window:resize',
|
||||
Windows_GetSize = 'window:get-size',
|
||||
|
||||
KnowledgeBase_Create = 'knowledge-base:create',
|
||||
KnowledgeBase_Reset = 'knowledge-base:reset',
|
||||
@ -133,23 +141,39 @@ export enum IpcChannel {
|
||||
File_Upload = 'file:upload',
|
||||
File_Clear = 'file:clear',
|
||||
File_Read = 'file:read',
|
||||
File_ReadExternal = 'file:readExternal',
|
||||
File_Delete = 'file:delete',
|
||||
File_DeleteDir = 'file:deleteDir',
|
||||
File_DeleteExternalFile = 'file:deleteExternalFile',
|
||||
File_DeleteExternalDir = 'file:deleteExternalDir',
|
||||
File_Move = 'file:move',
|
||||
File_MoveDir = 'file:moveDir',
|
||||
File_Rename = 'file:rename',
|
||||
File_RenameDir = 'file:renameDir',
|
||||
File_Get = 'file:get',
|
||||
File_SelectFolder = 'file:selectFolder',
|
||||
File_CreateTempFile = 'file:createTempFile',
|
||||
File_Mkdir = 'file:mkdir',
|
||||
File_Write = 'file:write',
|
||||
File_WriteWithId = 'file:writeWithId',
|
||||
File_SaveImage = 'file:saveImage',
|
||||
File_Base64Image = 'file:base64Image',
|
||||
File_SaveBase64Image = 'file:saveBase64Image',
|
||||
File_SavePastedImage = 'file:savePastedImage',
|
||||
File_Download = 'file:download',
|
||||
File_Copy = 'file:copy',
|
||||
File_BinaryImage = 'file:binaryImage',
|
||||
File_Base64File = 'file:base64File',
|
||||
File_GetPdfInfo = 'file:getPdfInfo',
|
||||
Fs_Read = 'fs:read',
|
||||
Fs_ReadText = 'fs:readText',
|
||||
File_OpenWithRelativePath = 'file:openWithRelativePath',
|
||||
File_IsTextFile = 'file:isTextFile',
|
||||
File_GetDirectoryStructure = 'file:getDirectoryStructure',
|
||||
File_CheckFileName = 'file:checkFileName',
|
||||
File_ValidateNotesDirectory = 'file:validateNotesDirectory',
|
||||
File_StartWatcher = 'file:startWatcher',
|
||||
File_StopWatcher = 'file:stopWatcher',
|
||||
|
||||
// file service
|
||||
FileService_Upload = 'file-service:upload',
|
||||
@ -174,7 +198,6 @@ export enum IpcChannel {
|
||||
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',
|
||||
@ -244,5 +267,41 @@ 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',
|
||||
|
||||
// TRACE
|
||||
TRACE_SAVE_DATA = 'trace:saveData',
|
||||
TRACE_GET_DATA = 'trace:getData',
|
||||
TRACE_SAVE_ENTITY = 'trace:saveEntity',
|
||||
TRACE_GET_ENTITY = 'trace:getEntity',
|
||||
TRACE_BIND_TOPIC = 'trace:bindTopic',
|
||||
TRACE_CLEAN_TOPIC = 'trace:cleanTopic',
|
||||
TRACE_TOKEN_USAGE = 'trace:tokenUsage',
|
||||
TRACE_CLEAN_HISTORY = 'trace:cleanHistory',
|
||||
TRACE_OPEN_WINDOW = 'trace:openWindow',
|
||||
TRACE_SET_TITLE = 'trace:setTitle',
|
||||
TRACE_ADD_END_MESSAGE = 'trace:addEndMessage',
|
||||
TRACE_CLEAN_LOCAL_DATA = 'trace:cleanLocalData',
|
||||
TRACE_ADD_STREAM_MESSAGE = 'trace:addStreamMessage',
|
||||
|
||||
// CodeTools
|
||||
CodeTools_Run = 'code-tools:run',
|
||||
|
||||
// OCR
|
||||
OCR_ocr = 'ocr:ocr',
|
||||
|
||||
// Cherryin
|
||||
Cherryin_GetSignature = 'cherryin:get-signature'
|
||||
}
|
||||
|
||||
@ -1,312 +1,127 @@
|
||||
import { languages } from './languages'
|
||||
|
||||
export const imageExts = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']
|
||||
export const videoExts = ['.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv']
|
||||
export const audioExts = ['.mp3', '.wav', '.ogg', '.flac', '.aac']
|
||||
export const documentExts = ['.pdf', '.doc', '.docx', '.pptx', '.xlsx', '.odt', '.odp', '.ods']
|
||||
export const thirdPartyApplicationExts = ['.draftsExport']
|
||||
export const bookExts = ['.epub']
|
||||
const textExtsByCategory = new Map([
|
||||
|
||||
/**
|
||||
* A flat array of all file extensions known by the linguist database.
|
||||
* This is the primary source for identifying code files.
|
||||
*/
|
||||
const linguistExtSet = new Set<string>()
|
||||
for (const lang of Object.values(languages)) {
|
||||
if (lang.extensions) {
|
||||
for (const ext of lang.extensions) {
|
||||
linguistExtSet.add(ext)
|
||||
}
|
||||
}
|
||||
}
|
||||
export const codeLangExts = Array.from(linguistExtSet)
|
||||
|
||||
/**
|
||||
* A categorized map of custom text-based file extensions that are NOT included
|
||||
* in the linguist database. This is for special cases or project-specific files.
|
||||
*/
|
||||
export const customTextExts = new Map([
|
||||
[
|
||||
'language',
|
||||
[
|
||||
'.js',
|
||||
'.mjs',
|
||||
'.cjs',
|
||||
'.ts',
|
||||
'.jsx',
|
||||
'.tsx', // JavaScript/TypeScript
|
||||
'.py', // Python
|
||||
'.java', // Java
|
||||
'.cs', // C#
|
||||
'.cpp',
|
||||
'.c',
|
||||
'.h',
|
||||
'.hpp',
|
||||
'.cc',
|
||||
'.cxx',
|
||||
'.cppm',
|
||||
'.ipp',
|
||||
'.ixx', // C/C++
|
||||
'.php', // PHP
|
||||
'.rb', // Ruby
|
||||
'.pl', // Perl
|
||||
'.go', // Go
|
||||
'.rs', // Rust
|
||||
'.swift', // Swift
|
||||
'.kt',
|
||||
'.kts', // Kotlin
|
||||
'.scala', // Scala
|
||||
'.lua', // Lua
|
||||
'.groovy', // Groovy
|
||||
'.dart', // Dart
|
||||
'.hs', // Haskell
|
||||
'.clj',
|
||||
'.cljs', // Clojure
|
||||
'.elm', // Elm
|
||||
'.erl', // Erlang
|
||||
'.ex',
|
||||
'.exs', // Elixir
|
||||
'.ml',
|
||||
'.mli', // OCaml
|
||||
'.fs', // F#
|
||||
'.r',
|
||||
'.R', // R
|
||||
'.sol', // Solidity
|
||||
'.awk', // AWK
|
||||
'.cob', // COBOL
|
||||
'.asm',
|
||||
'.s', // Assembly
|
||||
'.lisp',
|
||||
'.lsp', // Lisp
|
||||
'.coffee', // CoffeeScript
|
||||
'.ino', // Arduino
|
||||
'.jl', // Julia
|
||||
'.nim', // Nim
|
||||
'.zig', // Zig
|
||||
'.d', // D语言
|
||||
'.pas', // Pascal
|
||||
'.vb', // Visual Basic
|
||||
'.rkt', // Racket
|
||||
'.scm', // Scheme
|
||||
'.hx', // Haxe
|
||||
'.as', // ActionScript
|
||||
'.pde', // Processing
|
||||
'.f90',
|
||||
'.f',
|
||||
'.f03',
|
||||
'.for',
|
||||
'.f95', // Fortran
|
||||
'.adb',
|
||||
'.ads', // Ada
|
||||
'.pro', // Prolog
|
||||
'.m',
|
||||
'.mm', // Objective-C/MATLAB
|
||||
'.rpy', // Ren'Py
|
||||
'.ets', // OpenHarmony,
|
||||
'.uniswap', // DeFi
|
||||
'.vy', // Vyper
|
||||
'.shader',
|
||||
'.glsl',
|
||||
'.frag',
|
||||
'.vert',
|
||||
'.gd' // Godot
|
||||
]
|
||||
],
|
||||
[
|
||||
'script',
|
||||
[
|
||||
'.sh', // Shell
|
||||
'.bat',
|
||||
'.cmd', // Windows批处理
|
||||
'.ps1', // PowerShell
|
||||
'.tcl',
|
||||
'.do', // Tcl
|
||||
'.ahk', // AutoHotkey
|
||||
'.zsh', // Zsh
|
||||
'.fish', // Fish shell
|
||||
'.csh', // C shell
|
||||
'.vbs', // VBScript
|
||||
'.applescript', // AppleScript
|
||||
'.au3', // AutoIt
|
||||
'.bash',
|
||||
'.nu'
|
||||
]
|
||||
],
|
||||
[
|
||||
'style',
|
||||
[
|
||||
'.css', // CSS
|
||||
'.less', // Less
|
||||
'.scss',
|
||||
'.sass', // Sass
|
||||
'.styl', // Stylus
|
||||
'.pcss', // PostCSS
|
||||
'.postcss' // PostCSS
|
||||
'.usf', // Unreal shader format
|
||||
'.ush' // Unreal shader header
|
||||
]
|
||||
],
|
||||
[
|
||||
'template',
|
||||
[
|
||||
'.vue', // Vue.js
|
||||
'.pug',
|
||||
'.jade', // Pug/Jade
|
||||
'.haml', // Haml
|
||||
'.slim', // Slim
|
||||
'.tpl', // 通用模板
|
||||
'.ejs', // EJS
|
||||
'.hbs', // Handlebars
|
||||
'.mustache', // Mustache
|
||||
'.twig', // Twig
|
||||
'.blade', // Blade (Laravel)
|
||||
'.liquid', // Liquid
|
||||
'.jinja',
|
||||
'.jinja2',
|
||||
'.j2', // Jinja
|
||||
'.erb', // ERB
|
||||
'.vm', // Velocity
|
||||
'.ftl', // FreeMarker
|
||||
'.svelte', // Svelte
|
||||
'.astro' // Astro
|
||||
'.vm' // Velocity
|
||||
]
|
||||
],
|
||||
[
|
||||
'config',
|
||||
[
|
||||
'.ini', // INI配置
|
||||
'.babelrc', // Babel
|
||||
'.bashrc',
|
||||
'.browserslistrc',
|
||||
'.conf',
|
||||
'.config', // 通用配置
|
||||
'.env', // 环境变量
|
||||
'.toml', // TOML
|
||||
'.cfg', // 通用配置
|
||||
'.properties', // Java属性
|
||||
'.desktop', // Linux桌面文件
|
||||
'.service', // systemd服务
|
||||
'.rc',
|
||||
'.bashrc',
|
||||
'.zshrc', // Shell配置
|
||||
'.fishrc', // Fish shell配置
|
||||
'.vimrc', // Vim配置
|
||||
'.htaccess', // Apache配置
|
||||
'.robots', // robots.txt
|
||||
'.editorconfig', // EditorConfig
|
||||
'.eslintrc', // ESLint
|
||||
'.prettierrc', // Prettier
|
||||
'.babelrc', // Babel
|
||||
'.npmrc', // npm
|
||||
'.dockerignore', // Docker ignore
|
||||
'.npmignore',
|
||||
'.yarnrc',
|
||||
'.prettierignore',
|
||||
'.eslintignore',
|
||||
'.browserslistrc',
|
||||
'.json5',
|
||||
'.tfvars'
|
||||
'.eslintrc', // ESLint
|
||||
'.fishrc', // Fish shell配置
|
||||
'.htaccess', // Apache配置
|
||||
'.npmignore',
|
||||
'.npmrc', // npm
|
||||
'.prettierignore',
|
||||
'.prettierrc', // Prettier
|
||||
'.rc',
|
||||
'.robots', // robots.txt
|
||||
'.yarnrc',
|
||||
'.zshrc'
|
||||
]
|
||||
],
|
||||
[
|
||||
'document',
|
||||
[
|
||||
'.txt',
|
||||
'.text', // 纯文本
|
||||
'.md',
|
||||
'.mdx', // Markdown
|
||||
'.html',
|
||||
'.htm',
|
||||
'.xhtml', // HTML
|
||||
'.xml', // XML
|
||||
'.fxml', // JavaFX XML
|
||||
'.org', // Org-mode
|
||||
'.wiki', // Wiki
|
||||
'.tex',
|
||||
'.bib', // LaTeX
|
||||
'.rst', // reStructuredText
|
||||
'.rtf', // 富文本
|
||||
'.nfo', // 信息文件
|
||||
'.adoc',
|
||||
'.asciidoc', // AsciiDoc
|
||||
'.pod', // Perl文档
|
||||
'.1',
|
||||
'.2',
|
||||
'.3',
|
||||
'.4',
|
||||
'.5',
|
||||
'.6',
|
||||
'.7',
|
||||
'.8',
|
||||
'.9', // man页面
|
||||
'.man', // man页面
|
||||
'.texi',
|
||||
'.texinfo', // Texinfo
|
||||
'.readme',
|
||||
'.me', // README
|
||||
'.authors', // 作者文件
|
||||
'.changelog', // 变更日志
|
||||
'.license', // 许可证
|
||||
'.authors', // 作者文件
|
||||
'.po',
|
||||
'.pot'
|
||||
'.nfo', // 信息文件
|
||||
'.readme',
|
||||
'.text' // 纯文本
|
||||
]
|
||||
],
|
||||
[
|
||||
'data',
|
||||
[
|
||||
'.json', // JSON
|
||||
'.jsonc', // JSON with comments
|
||||
'.yaml',
|
||||
'.yml', // YAML
|
||||
'.csv',
|
||||
'.tsv', // 分隔值文件
|
||||
'.edn', // Clojure数据
|
||||
'.jsonl',
|
||||
'.ndjson', // 换行分隔JSON
|
||||
'.geojson', // GeoJSON
|
||||
'.gpx', // GPS Exchange
|
||||
'.kml', // Keyhole Markup
|
||||
'.rss',
|
||||
'.atom', // Feed格式
|
||||
'.vcf', // vCard
|
||||
'.ics', // iCalendar
|
||||
'.ldif', // LDAP数据交换
|
||||
'.pbtxt',
|
||||
'.map'
|
||||
'.ldif',
|
||||
'.map',
|
||||
'.ndjson' // 换行分隔JSON
|
||||
]
|
||||
],
|
||||
[
|
||||
'build',
|
||||
[
|
||||
'.gradle', // Gradle
|
||||
'.make',
|
||||
'.mk', // Make
|
||||
'.cmake', // CMake
|
||||
'.sbt', // SBT
|
||||
'.rake', // Rake
|
||||
'.spec', // RPM spec
|
||||
'.pom',
|
||||
'.bazel', // Bazel
|
||||
'.build', // Meson
|
||||
'.bazel' // Bazel
|
||||
'.pom'
|
||||
]
|
||||
],
|
||||
[
|
||||
'database',
|
||||
[
|
||||
'.sql', // SQL
|
||||
'.ddl',
|
||||
'.dml', // DDL/DML
|
||||
'.plsql', // PL/SQL
|
||||
'.psql', // PostgreSQL
|
||||
'.cypher', // Cypher
|
||||
'.sparql' // SPARQL
|
||||
'.psql' // PostgreSQL
|
||||
]
|
||||
],
|
||||
[
|
||||
'web',
|
||||
[
|
||||
'.graphql',
|
||||
'.gql', // GraphQL
|
||||
'.proto', // Protocol Buffers
|
||||
'.thrift', // Thrift
|
||||
'.wsdl', // WSDL
|
||||
'.raml', // RAML
|
||||
'.swagger',
|
||||
'.openapi' // API文档
|
||||
'.openapi', // API文档
|
||||
'.swagger'
|
||||
]
|
||||
],
|
||||
[
|
||||
'version',
|
||||
[
|
||||
'.gitignore', // Git ignore
|
||||
'.gitattributes', // Git attributes
|
||||
'.gitconfig', // Git config
|
||||
'.hgignore', // Mercurial ignore
|
||||
'.bzrignore', // Bazaar ignore
|
||||
'.svnignore', // SVN ignore
|
||||
'.githistory' // Git history
|
||||
'.gitattributes', // Git attributes
|
||||
'.githistory', // Git history
|
||||
'.hgignore', // Mercurial ignore
|
||||
'.svnignore' // SVN ignore
|
||||
]
|
||||
],
|
||||
[
|
||||
'subtitle',
|
||||
[
|
||||
'.srt',
|
||||
'.sub',
|
||||
'.ass' // 字幕格式
|
||||
'.ass', // 字幕格式
|
||||
'.sub'
|
||||
]
|
||||
],
|
||||
[
|
||||
@ -319,54 +134,26 @@ const textExtsByCategory = new Map([
|
||||
[
|
||||
'eda',
|
||||
[
|
||||
'.v',
|
||||
'.sv',
|
||||
'.svh', // Verilog/SystemVerilog
|
||||
'.vhd',
|
||||
'.vhdl', // VHDL
|
||||
'.lef',
|
||||
'.cir',
|
||||
'.def', // LEF/DEF
|
||||
'.edif', // EDIF
|
||||
'.sdf', // SDF
|
||||
'.sdc',
|
||||
'.xdc', // 约束文件
|
||||
'.sp',
|
||||
'.spi',
|
||||
'.cir',
|
||||
'.net', // SPICE
|
||||
'.scs', // Spectre
|
||||
'.asc', // LTspice
|
||||
'.tf', // Technology File
|
||||
'.il',
|
||||
'.ils' // SKILL
|
||||
]
|
||||
],
|
||||
[
|
||||
'game',
|
||||
[
|
||||
'.mtl', // Material Template Library
|
||||
'.x3d', // X3D文件
|
||||
'.gltf', // glTF JSON
|
||||
'.prefab', // Unity预制体 (YAML格式)
|
||||
'.meta' // Unity元数据文件 (YAML格式)
|
||||
]
|
||||
],
|
||||
[
|
||||
'other',
|
||||
[
|
||||
'.mcfunction', // Minecraft函数
|
||||
'.jsp', // JSP
|
||||
'.aspx', // ASP.NET
|
||||
'.ipynb', // Jupyter Notebook
|
||||
'.cake',
|
||||
'.ctp', // CakePHP
|
||||
'.cfm',
|
||||
'.cfc' // ColdFusion
|
||||
'.ils', // SKILL
|
||||
'.lef',
|
||||
'.net',
|
||||
'.scs', // Spectre
|
||||
'.sdf', // SDF
|
||||
'.spi'
|
||||
]
|
||||
]
|
||||
])
|
||||
|
||||
export const textExts = Array.from(textExtsByCategory.values()).flat()
|
||||
/**
|
||||
* A comprehensive list of all text-based file extensions, combining the
|
||||
* extensive list from the linguist database with our custom additions.
|
||||
* The Set ensures there are no duplicates.
|
||||
*/
|
||||
export const textExts = [...new Set([...Array.from(customTextExts.values()).flat(), ...codeLangExts])]
|
||||
|
||||
export const ZOOM_LEVELS = [0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5]
|
||||
|
||||
@ -407,8 +194,7 @@ export const defaultLanguage = 'en-US'
|
||||
|
||||
export enum FeedUrl {
|
||||
PRODUCTION = 'https://releases.cherry-ai.com',
|
||||
GITHUB_LATEST = 'https://github.com/CherryHQ/cherry-studio/releases/latest/download',
|
||||
PRERELEASE_LOWEST = 'https://github.com/CherryHQ/cherry-studio/releases/download/v1.4.0'
|
||||
GITHUB_LATEST = 'https://github.com/CherryHQ/cherry-studio/releases/latest/download'
|
||||
}
|
||||
|
||||
export enum UpgradeChannel {
|
||||
@ -420,3 +206,15 @@ export enum UpgradeChannel {
|
||||
export const defaultTimeout = 10 * 1000 * 60
|
||||
|
||||
export const occupiedDirs = ['logs', 'Network', 'Partitions/webview/Network']
|
||||
|
||||
export const MIN_WINDOW_WIDTH = 960
|
||||
export const SECOND_MIN_WINDOW_WIDTH = 520
|
||||
export const MIN_WINDOW_HEIGHT = 600
|
||||
export const defaultByPassRules = 'localhost,127.0.0.1,::1'
|
||||
|
||||
export enum codeTools {
|
||||
qwenCode = 'qwen-code',
|
||||
claudeCode = 'claude-code',
|
||||
geminiCli = 'gemini-cli',
|
||||
openaiCodex = 'openai-codex'
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
32
packages/shared/config/logger.ts
Normal file
32
packages/shared/config/logger.ts
Normal file
@ -0,0 +1,32 @@
|
||||
export type LogSourceWithContext = {
|
||||
process: 'main' | 'renderer'
|
||||
window?: string // only for renderer process
|
||||
module?: string
|
||||
context?: Record<string, any>
|
||||
}
|
||||
|
||||
type NullableObject = object | undefined | null
|
||||
|
||||
export type LogContextData = [] | [Error | NullableObject] | [Error | NullableObject, ...NullableObject[]]
|
||||
|
||||
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose' | 'silly' | 'none'
|
||||
|
||||
export const LEVEL = {
|
||||
ERROR: 'error',
|
||||
WARN: 'warn',
|
||||
INFO: 'info',
|
||||
DEBUG: 'debug',
|
||||
VERBOSE: 'verbose',
|
||||
SILLY: 'silly',
|
||||
NONE: 'none'
|
||||
} satisfies Record<string, LogLevel>
|
||||
|
||||
export const LEVEL_MAP: Record<LogLevel, number> = {
|
||||
error: 10,
|
||||
warn: 8,
|
||||
info: 6,
|
||||
debug: 4,
|
||||
verbose: 2,
|
||||
silly: 0,
|
||||
none: -1
|
||||
}
|
||||
@ -9,3 +9,11 @@ export type LoaderReturn = {
|
||||
message?: string
|
||||
messageSource?: 'preprocess' | 'embedding'
|
||||
}
|
||||
|
||||
export type FileChangeEventType = 'add' | 'change' | 'unlink' | 'addDir' | 'unlinkDir'
|
||||
|
||||
export type FileChangeEvent = {
|
||||
eventType: FileChangeEventType
|
||||
filePath: string
|
||||
watchPath: string
|
||||
}
|
||||
|
||||
@ -2089,7 +2089,7 @@
|
||||
"Design",
|
||||
"Education"
|
||||
],
|
||||
"prompt": "I want you to act as a Graphviz DOT generator, an expert to create meaningful diagrams. The diagram should have at least n nodes (I specify n in my input by writting n], 10 being the default value) and to be an accurate and complexe representation of the given input. Each node is indexed by a number to reduce the size of the output, should not include any styling, and with layout=neato, overlap=false, node shape=rectangle] as parameters. The code should be valid, bugless and returned on a single line, without any explanation. Provide a clear and organized diagram, the relationships between the nodes have to make sense for an expert of that input. My first diagram is: \"The water cycle 8]\".\n\n",
|
||||
"prompt": "I want you to act as a Graphviz DOT generator, an expert to create meaningful diagrams. The diagram should have at least n nodes (I specify n in my input by writing n], 10 being the default value) and to be an accurate and complex representation of the given input. Each node is indexed by a number to reduce the size of the output, should not include any styling, and with layout=neato, overlap=false, node shape=rectangle] as parameters. The code should be valid, bugless and returned on a single line, without any explanation. Provide a clear and organized diagram, the relationships between the nodes have to make sense for an expert of that input. My first diagram is: \"The water cycle 8]\".\n\n",
|
||||
"description": "Generate meaningful charts."
|
||||
},
|
||||
{
|
||||
@ -2148,7 +2148,7 @@
|
||||
"Career",
|
||||
"Business"
|
||||
],
|
||||
"prompt": "Please acknowledge my following request. Please respond to me as a product manager. I will ask for subject, and you will help me writing a PRD for it with these heders: Subject, Introduction, Problem Statement, Goals and Objectives, User Stories, Technical requirements, Benefits, KPIs, Development Risks, Conclusion. Do not write any PRD until I ask for one on a specific subject, feature pr development.\n\n",
|
||||
"prompt": "Please acknowledge my following request. Please respond to me as a product manager. I will ask for subject, and you will help me writing a PRD for it with these headers: Subject, Introduction, Problem Statement, Goals and Objectives, User Stories, Technical requirements, Benefits, KPIs, Development Risks, Conclusion. Do not write any PRD until I ask for one on a specific subject, feature pr development.\n\n",
|
||||
"description": "Help draft the Product Requirements Document."
|
||||
},
|
||||
{
|
||||
@ -2159,7 +2159,7 @@
|
||||
"Entertainment",
|
||||
"General"
|
||||
],
|
||||
"prompt": "I want you to act as a drunk person. You will only answer like a very drunk person texting and nothing else. Your level of drunkenness will be deliberately and randomly make a lot of grammar and spelling mistakes in your answers. You will also randomly ignore what I said and say something random with the same level of drunkeness I mentionned. Do not write explanations on replies. My first sentence is \"how are you?",
|
||||
"prompt": "I want you to act as a drunk person. You will only answer like a very drunk person texting and nothing else. Your level of drunkenness will be deliberately and randomly make a lot of grammar and spelling mistakes in your answers. You will also randomly ignore what I said and say something random with the same level of drunkenness I mentioned. Do not write explanations on replies. My first sentence is \"how are you?",
|
||||
"description": "Mimic the speech pattern of a drunk person."
|
||||
},
|
||||
{
|
||||
@ -3517,7 +3517,7 @@
|
||||
"Tools",
|
||||
"Copywriting"
|
||||
],
|
||||
"prompt": "I want you to act as a scientific manuscript matcher. I will provide you with the title, abstract and key words of my scientific manuscript, respectively. Your task is analyzing my title, abstract and key words synthetically to find the most related, reputable journals for potential publication of my research based on an analysis of tens of millions of citation connections in database, such as Web of Science, Pubmed, Scopus, ScienceDirect and so on. You only need to provide me with the 15 most suitable journals. Your reply should include the name of journal, the cooresponding match score (The full score is ten). I want you to reply in text-based excel sheet and sort by matching scores in reverse order.\nMy title is \"XXX\" My abstract is \"XXX\" My key words are \"XXX\"\n\n",
|
||||
"prompt": "I want you to act as a scientific manuscript matcher. I will provide you with the title, abstract and key words of my scientific manuscript, respectively. Your task is analyzing my title, abstract and key words synthetically to find the most related, reputable journals for potential publication of my research based on an analysis of tens of millions of citation connections in database, such as Web of Science, Pubmed, Scopus, ScienceDirect and so on. You only need to provide me with the 15 most suitable journals. Your reply should include the name of journal, the corresponding match score (The full score is ten). I want you to reply in text-based excel sheet and sort by matching scores in reverse order.\nMy title is \"XXX\" My abstract is \"XXX\" My key words are \"XXX\"\n\n",
|
||||
"description": ""
|
||||
},
|
||||
{
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
88
resources/scripts/ipService.js
Normal file
88
resources/scripts/ipService.js
Normal file
@ -0,0 +1,88 @@
|
||||
const https = require('https')
|
||||
const { loggerService } = require('@logger')
|
||||
|
||||
const logger = loggerService.withContext('IpService')
|
||||
|
||||
/**
|
||||
* 获取用户的IP地址所在国家
|
||||
* @returns {Promise<string>} 返回国家代码,默认为'CN'
|
||||
*/
|
||||
async function getIpCountry() {
|
||||
return new Promise((resolve) => {
|
||||
// 添加超时控制
|
||||
const timeout = setTimeout(() => {
|
||||
logger.info('IP Address Check Timeout, default to China Mirror')
|
||||
resolve('CN')
|
||||
}, 5000)
|
||||
|
||||
const options = {
|
||||
hostname: 'ipinfo.io',
|
||||
path: '/json',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
||||
'Accept-Language': 'en-US,en;q=0.9'
|
||||
}
|
||||
}
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
clearTimeout(timeout)
|
||||
let data = ''
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk
|
||||
})
|
||||
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const parsed = JSON.parse(data)
|
||||
const country = parsed.country || 'CN'
|
||||
logger.info(`Detected user IP address country: ${country}`)
|
||||
resolve(country)
|
||||
} catch (error) {
|
||||
logger.error('Failed to parse IP address information:', error.message)
|
||||
resolve('CN')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
req.on('error', (error) => {
|
||||
clearTimeout(timeout)
|
||||
logger.error('Failed to get IP address information:', error.message)
|
||||
resolve('CN')
|
||||
})
|
||||
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否在中国
|
||||
* @returns {Promise<boolean>} 如果用户在中国返回true,否则返回false
|
||||
*/
|
||||
async function isUserInChina() {
|
||||
const country = await getIpCountry()
|
||||
return country.toLowerCase() === 'cn'
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户位置获取适合的npm镜像URL
|
||||
* @returns {Promise<string>} 返回npm镜像URL
|
||||
*/
|
||||
async function getNpmRegistryUrl() {
|
||||
const inChina = await isUserInChina()
|
||||
if (inChina) {
|
||||
logger.info('User in China, using Taobao npm mirror')
|
||||
return 'https://registry.npmmirror.com'
|
||||
} else {
|
||||
logger.info('User not in China, using default npm mirror')
|
||||
return 'https://registry.npmjs.org'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getIpCountry,
|
||||
isUserInChina,
|
||||
getNpmRegistryUrl
|
||||
}
|
||||
92
scripts/__tests__/sort.test.ts
Normal file
92
scripts/__tests__/sort.test.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import { sortedObjectByKeys } from '../sort'
|
||||
|
||||
describe('sortedObjectByKeys', () => {
|
||||
test('should sort keys of a flat object alphabetically', () => {
|
||||
const obj = { b: 2, a: 1, c: 3 }
|
||||
const sortedObj = { a: 1, b: 2, c: 3 }
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should sort keys of nested objects alphabetically', () => {
|
||||
const obj = {
|
||||
c: { z: 3, y: 2, x: 1 },
|
||||
a: 1,
|
||||
b: { f: 6, d: 4, e: 5 }
|
||||
}
|
||||
const sortedObj = {
|
||||
a: 1,
|
||||
b: { d: 4, e: 5, f: 6 },
|
||||
c: { x: 1, y: 2, z: 3 }
|
||||
}
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should handle empty objects', () => {
|
||||
const obj = {}
|
||||
expect(sortedObjectByKeys(obj)).toEqual({})
|
||||
})
|
||||
|
||||
test('should handle objects with non-object values', () => {
|
||||
const obj = { b: 'hello', a: 123, c: true }
|
||||
const sortedObj = { a: 123, b: 'hello', c: true }
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should handle objects with array values', () => {
|
||||
const obj = { b: [2, 1], a: [1, 2] }
|
||||
const sortedObj = { a: [1, 2], b: [2, 1] }
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should handle objects with null values', () => {
|
||||
const obj = { b: null, a: 1 }
|
||||
const sortedObj = { a: 1, b: null }
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should handle objects with undefined values', () => {
|
||||
const obj = { b: undefined, a: 1 }
|
||||
const sortedObj = { a: 1, b: undefined }
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
|
||||
test('should not modify the original object', () => {
|
||||
const obj = { b: 2, a: 1 }
|
||||
sortedObjectByKeys(obj)
|
||||
expect(obj).toEqual({ b: 2, a: 1 })
|
||||
})
|
||||
|
||||
test('should handle objects read from i18n JSON files', () => {
|
||||
const obj = {
|
||||
translation: {
|
||||
backup: {
|
||||
progress: {
|
||||
writing_data: '写入数据...',
|
||||
preparing: '准备备份...',
|
||||
completed: '备份完成'
|
||||
}
|
||||
},
|
||||
agents: {
|
||||
'delete.popup.content': '确定要删除此智能体吗?',
|
||||
'edit.model.select.title': '选择模型'
|
||||
}
|
||||
}
|
||||
}
|
||||
const sortedObj = {
|
||||
translation: {
|
||||
agents: {
|
||||
'delete.popup.content': '确定要删除此智能体吗?',
|
||||
'edit.model.select.title': '选择模型'
|
||||
},
|
||||
backup: {
|
||||
progress: {
|
||||
completed: '备份完成',
|
||||
preparing: '准备备份...',
|
||||
writing_data: '写入数据...'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(sortedObjectByKeys(obj)).toEqual(sortedObj)
|
||||
})
|
||||
})
|
||||
@ -1,89 +1,10 @@
|
||||
const { Arch } = require('electron-builder')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
exports.default = async function (context) {
|
||||
const platform = context.packager.platform.name
|
||||
const arch = context.arch
|
||||
|
||||
if (platform === 'mac') {
|
||||
const node_modules_path = path.join(
|
||||
context.appOutDir,
|
||||
'Cherry Studio.app',
|
||||
'Contents',
|
||||
'Resources',
|
||||
'app.asar.unpacked',
|
||||
'node_modules'
|
||||
)
|
||||
|
||||
keepPackageNodeFiles(node_modules_path, '@libsql', arch === Arch.arm64 ? ['darwin-arm64'] : ['darwin-x64'])
|
||||
}
|
||||
|
||||
if (platform === 'linux') {
|
||||
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') {
|
||||
const node_modules_path = path.join(context.appOutDir, 'resources', 'app.asar.unpacked', 'node_modules')
|
||||
if (arch === Arch.arm64) {
|
||||
keepPackageNodeFiles(node_modules_path, '@strongtz', ['win32-arm64-msvc'])
|
||||
keepPackageNodeFiles(node_modules_path, '@libsql', ['win32-arm64-msvc'])
|
||||
}
|
||||
if (arch === Arch.x64) {
|
||||
keepPackageNodeFiles(node_modules_path, '@strongtz', ['win32-x64-msvc'])
|
||||
keepPackageNodeFiles(node_modules_path, '@libsql', ['win32-x64-msvc'])
|
||||
}
|
||||
|
||||
removeMacOnlyPackages(node_modules_path)
|
||||
}
|
||||
|
||||
if (platform === 'windows') {
|
||||
fs.rmSync(path.join(context.appOutDir, 'LICENSE.electron.txt'), { force: true })
|
||||
fs.rmSync(path.join(context.appOutDir, 'LICENSES.chromium.html'), { force: true })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 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
|
||||
* @param {*} packageName
|
||||
* @param {*} arch
|
||||
* @returns
|
||||
*/
|
||||
function keepPackageNodeFiles(nodeModulesPath, packageName, arch) {
|
||||
const modulePath = path.join(nodeModulesPath, packageName)
|
||||
|
||||
if (!fs.existsSync(modulePath)) {
|
||||
console.log(`[After Pack] Directory does not exist: ${modulePath}`)
|
||||
return
|
||||
}
|
||||
|
||||
const dirs = fs.readdirSync(modulePath)
|
||||
dirs
|
||||
.filter((dir) => !arch.includes(dir))
|
||||
.forEach((dir) => {
|
||||
fs.rmSync(path.join(modulePath, dir), { recursive: true, force: true })
|
||||
console.log(`[After Pack] Removed dir: ${dir}`, arch)
|
||||
})
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user