Compare commits
446 Commits
tauri-migr
...
v3.6.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d77866160 | ||
|
|
7305b1124b | ||
|
|
dc79c31148 | ||
|
|
03e15916dd | ||
|
|
69c0a09604 | ||
|
|
4e23250755 | ||
|
|
5f78e58ffc | ||
|
|
e4416c9da8 | ||
|
|
6f05a91226 | ||
|
|
db80e96786 | ||
|
|
d6fa0060fb | ||
|
|
4f4c1e4ed7 | ||
|
|
ce24b37b39 | ||
|
|
a428e618d2 | ||
|
|
21d29b9c2d | ||
|
|
254896e5eb | ||
|
|
dbf220d85f | ||
|
|
b498f0fe91 | ||
|
|
f92dd4cc5a | ||
|
|
720c4d9774 | ||
|
|
bafddb8e52 | ||
|
|
05e58e9e14 | ||
|
|
802c3bffd9 | ||
|
|
7be74806e8 | ||
|
|
a6b6c199b4 | ||
|
|
0778347f84 | ||
|
|
49c2855b10 | ||
|
|
ccb011fba1 | ||
|
|
0f62829599 | ||
|
|
cc5d59ce56 | ||
|
|
4afa68eac6 | ||
|
|
36fd61b2a2 | ||
|
|
85334d8dce | ||
|
|
ab2833e626 | ||
|
|
b4f10d8316 | ||
|
|
50eb4538ca | ||
|
|
c56866f48c | ||
|
|
972650377d | ||
|
|
faeca6b6ce | ||
|
|
cb83089866 | ||
|
|
ebb7106102 | ||
|
|
4811aa2dcd | ||
|
|
2ebe34810c | ||
|
|
87f408c163 | ||
|
|
b1f7840e45 | ||
|
|
c168873c1e | ||
|
|
fa9f4997af | ||
|
|
717be12991 | ||
|
|
ef85b015d3 | ||
|
|
def4095e4e | ||
|
|
b3e14b3985 | ||
|
|
64f2220ad9 | ||
|
|
55223bdd46 | ||
|
|
8e4a0a1bbb | ||
|
|
80dd6e9381 | ||
|
|
931ef7d3dd | ||
|
|
2aec407a2f | ||
|
|
ead65d82ad | ||
|
|
08f480ec94 | ||
|
|
590be4e136 | ||
|
|
7d56aed543 | ||
|
|
1841f8b462 | ||
|
|
5c3aca18eb | ||
|
|
88a952023f | ||
|
|
9e72e786e3 | ||
|
|
7b1a68ee4e | ||
|
|
7e27f88154 | ||
|
|
c2e8855a0f | ||
|
|
8e980e6974 | ||
|
|
10abdfa096 | ||
|
|
6a9aa7aeb5 | ||
|
|
9f5c2b427f | ||
|
|
4aa9512e36 | ||
|
|
1cc0e4bc8d | ||
|
|
c01e495eea | ||
|
|
bfab1d0ccb | ||
|
|
d064cb8555 | ||
|
|
76a8d1760b | ||
|
|
885dd94803 | ||
|
|
c3f712bc18 | ||
|
|
c8c4656e0e | ||
|
|
521c69db92 | ||
|
|
d65621a556 | ||
|
|
0b40e200f5 | ||
|
|
b3c333c908 | ||
|
|
001ac14c85 | ||
|
|
96a8712f2d | ||
|
|
2c7dcb023a | ||
|
|
019ad351a1 | ||
|
|
c2031c9b5c | ||
|
|
89aef39c74 | ||
|
|
bbf830a1da | ||
|
|
7325edff35 | ||
|
|
28900b8920 | ||
|
|
c2517571f5 | ||
|
|
d296471b3b | ||
|
|
36767045ce | ||
|
|
fb0dc5b186 | ||
|
|
07787a2ee1 | ||
|
|
495e66e3b6 | ||
|
|
6cc75d5c24 | ||
|
|
e38ff843e7 | ||
|
|
ae6d16ccae | ||
|
|
3504fae4cb | ||
|
|
bc185602ca | ||
|
|
7a694fbcb0 | ||
|
|
cbd1903b90 | ||
|
|
3626880663 | ||
|
|
13acc5323c | ||
|
|
39981f8075 | ||
|
|
9144014803 | ||
|
|
0de818b8b1 | ||
|
|
505fa47feb | ||
|
|
ef53439f83 | ||
|
|
491bbff11d | ||
|
|
43ed1c7533 | ||
|
|
a0cb29d3b2 | ||
|
|
57661817d3 | ||
|
|
eb6948a562 | ||
|
|
bae6a1cf55 | ||
|
|
b036a94281 | ||
|
|
a5fff93732 | ||
|
|
5253e7ec37 | ||
|
|
9fc5555ecf | ||
|
|
9d6ccb6d15 | ||
|
|
08eed46919 | ||
|
|
57552b3159 | ||
|
|
404297cd30 | ||
|
|
320bf3eeac | ||
|
|
5ebe23abc8 | ||
|
|
e6b66f425a | ||
|
|
8e82ded158 | ||
|
|
c04f636bbe | ||
|
|
f5c6363dee | ||
|
|
2d3d717826 | ||
|
|
bcaebc1bcb | ||
|
|
e02175e68d | ||
|
|
9fb000b8fe | ||
|
|
0cff882a3f | ||
|
|
d9c56511b1 | ||
|
|
b8a435a7f6 | ||
|
|
edfb61186d | ||
|
|
f963d58e6a | ||
|
|
8d6ab63648 | ||
|
|
d3f2c3c901 | ||
|
|
c1f5ddf763 | ||
|
|
54b0b3b139 | ||
|
|
51c68ef192 | ||
|
|
41dd487471 | ||
|
|
17f350f2d3 | ||
|
|
8a724b79ec | ||
|
|
9d75a646ee | ||
|
|
0868a71576 | ||
|
|
8ce574bdd2 | ||
|
|
856beb3b70 | ||
|
|
74afca7b58 | ||
|
|
e4f85f4f65 | ||
|
|
6541c14421 | ||
|
|
fe4b3e9957 | ||
|
|
918e519b05 | ||
|
|
a32aeaf73e | ||
|
|
577998fef2 | ||
|
|
3b22bcc134 | ||
|
|
98c35c7c62 | ||
|
|
2c1346a23d | ||
|
|
31f56f7c86 | ||
|
|
cfefe6b52a | ||
|
|
92528e6a9f | ||
|
|
5f2bede5c4 | ||
|
|
bb48f4f6af | ||
|
|
f3e7412a14 | ||
|
|
2b45af118f | ||
|
|
b88eb88608 | ||
|
|
cc0b7053aa | ||
|
|
95e2d84655 | ||
|
|
9eb991d087 | ||
|
|
3b6048b1e8 | ||
|
|
3e4df2c96a | ||
|
|
59644b29e6 | ||
|
|
5427ae04e4 | ||
|
|
a2aa5f8434 | ||
|
|
06010ff78e | ||
|
|
e77eab2116 | ||
|
|
ed9dd7bbc3 | ||
|
|
3d20245a80 | ||
|
|
f55c6d3d91 | ||
|
|
ec83e2ca44 | ||
|
|
60e8351f60 | ||
|
|
4a9eb64f76 | ||
|
|
66bbf63300 | ||
|
|
6e2c80531d | ||
|
|
2ec0a10a2c | ||
|
|
e92d99b758 | ||
|
|
036d41b774 | ||
|
|
3bd70b9508 | ||
|
|
664efc7456 | ||
|
|
1415ef4d78 | ||
|
|
fb137c4a78 | ||
|
|
668ab710c6 | ||
|
|
ea7080a42e | ||
|
|
c2b27a4949 | ||
|
|
a6ee3ba35f | ||
|
|
2a60d20841 | ||
|
|
42329d4dce | ||
|
|
5013d3b4c9 | ||
|
|
9ba9cddf18 | ||
|
|
81356cacee | ||
|
|
3b142155c3 | ||
|
|
4543664ba2 | ||
|
|
e88562be98 | ||
|
|
bfdf7d4ad5 | ||
|
|
c350e64687 | ||
|
|
70d8d2cc43 | ||
|
|
56b2681a6f | ||
|
|
6cf7dacd0e | ||
|
|
428369cae0 | ||
|
|
7f1131dfae | ||
|
|
7493f3f9dd | ||
|
|
eb8d9352c8 | ||
|
|
29b8d5edde | ||
|
|
97d81c13b7 | ||
|
|
511980e3ea | ||
|
|
f6bf8611cd | ||
|
|
0be596afb5 | ||
|
|
2bb847cb3d | ||
|
|
9471cb0d19 | ||
|
|
d0fe9d7533 | ||
|
|
59c13c3366 | ||
|
|
96a4b4fe95 | ||
|
|
e0e84ca58a | ||
|
|
c6a062f64a | ||
|
|
94192a3720 | ||
|
|
e7a584c5ba | ||
|
|
e9833e9a57 | ||
|
|
6afc436946 | ||
|
|
c89bf0c6f0 | ||
|
|
a6d461282d | ||
|
|
75ce5a0723 | ||
|
|
3f3905fda0 | ||
|
|
01da9a1eac | ||
|
|
420a4234de | ||
|
|
ca488cf076 | ||
|
|
3ad11acdb2 | ||
|
|
f8c40d591f | ||
|
|
ce593248fc | ||
|
|
e0908701b4 | ||
|
|
d86994eb7e | ||
|
|
94e93137a2 | ||
|
|
db832a9654 | ||
|
|
45a639e73f | ||
|
|
f74d641f86 | ||
|
|
fcfa9574e8 | ||
|
|
d739bb36e5 | ||
|
|
0bcc04adce | ||
|
|
fee0762e3e | ||
|
|
1a8ae85e55 | ||
|
|
c5aa244d65 | ||
|
|
0bedbb2663 | ||
|
|
5f3caa1484 | ||
|
|
fd0e83ebd5 | ||
|
|
e969bdbd73 | ||
|
|
7435a34c66 | ||
|
|
5d2d15690c | ||
|
|
11ee8bddf7 | ||
|
|
186c361a79 | ||
|
|
cc1caea36d | ||
|
|
9ede0ad27d | ||
|
|
20f0dd7e1c | ||
|
|
4dd07dfd85 | ||
|
|
8c01be42fa | ||
|
|
aaf1af0743 | ||
|
|
aeb0007957 | ||
|
|
077d491720 | ||
|
|
7e9930fe50 | ||
|
|
b17d915086 | ||
|
|
3e834e2c38 | ||
|
|
cae625dab1 | ||
|
|
122d7f1ad6 | ||
|
|
7eaf284400 | ||
|
|
86ef7afbdf | ||
|
|
615c431875 | ||
|
|
d041ea7a56 | ||
|
|
c4c1747563 | ||
|
|
c284fe8348 | ||
|
|
8f932b7358 | ||
|
|
d9e940e7a7 | ||
|
|
2147db6707 | ||
|
|
8c826b3073 | ||
|
|
54f1357bcc | ||
|
|
b8d2daccde | ||
|
|
21205272a5 | ||
|
|
ef067a6968 | ||
|
|
84204889f0 | ||
|
|
31cdc2a5cf | ||
|
|
7522ba3e03 | ||
|
|
3ac3f122eb | ||
|
|
67db492330 | ||
|
|
358d6e001e | ||
|
|
8a26cb51d8 | ||
|
|
9f8c745f8c | ||
|
|
3a9a8036d2 | ||
|
|
04e81ebbe3 | ||
|
|
c6e4f3599e | ||
|
|
60eb9ce2a4 | ||
|
|
50244f0055 | ||
|
|
eca14db58c | ||
|
|
463e430a3d | ||
|
|
32e66e054b | ||
|
|
2a9f093210 | ||
|
|
9bf216b102 | ||
|
|
b69d7f7979 | ||
|
|
efff780eea | ||
|
|
19dcc84c83 | ||
|
|
4e9e63f524 | ||
|
|
1d1440f52f | ||
|
|
36b78d1b4b | ||
|
|
2b59a5d51b | ||
|
|
15c12c8e65 | ||
|
|
3256b2f842 | ||
|
|
7374b934c7 | ||
|
|
d9d7c5c342 | ||
|
|
f4f7e10953 | ||
|
|
6ad7e04a95 | ||
|
|
7122e10646 | ||
|
|
bb685be43d | ||
|
|
c5b3b4027f | ||
|
|
daba6b094b | ||
|
|
711ad843ce | ||
|
|
189a70280f | ||
|
|
7ccef5f385 | ||
|
|
85ba24f1c3 | ||
|
|
0d2dedbb6d | ||
|
|
d76c675feb | ||
|
|
9372ecd3c6 | ||
|
|
d0b654f63e | ||
|
|
f035796654 | ||
|
|
160da2729e | ||
|
|
14db6b8a8f | ||
|
|
d91bbb122c | ||
|
|
6df5dfc123 | ||
|
|
c8327f7632 | ||
|
|
4a0e63d0b7 | ||
|
|
e63b4e069b | ||
|
|
687c7de111 | ||
|
|
876605e983 | ||
|
|
442b05507c | ||
|
|
eca9c02147 | ||
|
|
9fbce5d0cf | ||
|
|
c597b9b122 | ||
|
|
54b88d9c89 | ||
|
|
319e5fa61a | ||
|
|
310086d5c9 | ||
|
|
4297703ebe | ||
|
|
ca7ce99702 | ||
|
|
af8b9289fe | ||
|
|
bf7e13d4e9 | ||
|
|
b015af173a | ||
|
|
4a4779a7e7 | ||
|
|
92a39a1a34 | ||
|
|
ea56794a37 | ||
|
|
fd4864115c | ||
|
|
74d4b42936 | ||
|
|
a95f974787 | ||
|
|
29057c1fe0 | ||
|
|
63285acba8 | ||
|
|
f99b614888 | ||
|
|
41f3aa7d76 | ||
|
|
f23898a5c9 | ||
|
|
664391568c | ||
|
|
081aabe10f | ||
|
|
036069a5c1 | ||
|
|
9b7091ba88 | ||
|
|
2357d976dc | ||
|
|
df43692bb9 | ||
|
|
6ed9cf47df | ||
|
|
a3582f54e9 | ||
|
|
9ff7516c51 | ||
|
|
a1a16be2aa | ||
|
|
df7d818514 | ||
|
|
c0d9d0296d | ||
|
|
77a65aaad8 | ||
|
|
3ce847d2e0 | ||
|
|
1482dc9e66 | ||
|
|
fa2b11fcc2 | ||
|
|
02bfc97ee6 | ||
|
|
77bdeb02fb | ||
|
|
48bd37a74b | ||
|
|
7346fcde2c | ||
|
|
5af476d376 | ||
|
|
07b870488d | ||
|
|
74ab14f572 | ||
|
|
a14f7ef9b2 | ||
|
|
07dd70570e | ||
|
|
37d4c9b48d | ||
|
|
da4f7b5fe4 | ||
|
|
e119d1cb31 | ||
|
|
54003d69e2 | ||
|
|
ab6be1d510 | ||
|
|
a1dfdf4e68 | ||
|
|
464ca70d7b | ||
|
|
2dca85c881 | ||
|
|
837435223a | ||
|
|
79ad0b9368 | ||
|
|
5624a2d11a | ||
|
|
29367ff576 | ||
|
|
64c94804ee | ||
|
|
1d9fb7bf26 | ||
|
|
30a441d9ec | ||
|
|
33753c72cd | ||
|
|
02d7eca2ad | ||
|
|
2c6fe6c31a | ||
|
|
ab71b11532 | ||
|
|
a858596fa2 | ||
|
|
5176134c28 | ||
|
|
79370dd8a1 | ||
|
|
3c32f12152 | ||
|
|
64f7e47b20 | ||
|
|
25c112856d | ||
|
|
3665a79e50 | ||
|
|
4dce31aff7 | ||
|
|
451ca949ec | ||
|
|
a9ff8ce01c | ||
|
|
7848248df7 | ||
|
|
b00e8de26f | ||
|
|
47b06b7773 | ||
|
|
4e66f0c105 | ||
|
|
84c7726940 | ||
|
|
b8f59a4740 | ||
|
|
06a19519c5 | ||
|
|
b4ebb7c9e5 | ||
|
|
5edc3e07a4 | ||
|
|
417dcc1d37 | ||
|
|
72f6068e86 | ||
|
|
97e7f34260 | ||
|
|
74babf9730 | ||
|
|
30fe800ebe | ||
|
|
c98a724935 | ||
|
|
0cb89c8f67 | ||
|
|
7b5d5c6ce1 | ||
|
|
eea5e4123b | ||
|
|
c10ace7a84 | ||
|
|
0e803b53d8 | ||
|
|
49d8787ab9 | ||
|
|
a05fefb54c | ||
|
|
3574fa07cb | ||
|
|
b64b86f3ca |
38
.gitattributes
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
# Explicitly declare text files you want to always be normalized and converted
|
||||
# to native line endings on checkout.
|
||||
*.rs text eol=lf
|
||||
*.toml text eol=lf
|
||||
*.json text eol=lf
|
||||
*.md text eol=lf
|
||||
*.yml text eol=lf
|
||||
*.yaml text eol=lf
|
||||
*.txt text eol=lf
|
||||
|
||||
# TypeScript/JavaScript files
|
||||
*.ts text eol=lf
|
||||
*.tsx text eol=lf
|
||||
*.js text eol=lf
|
||||
*.jsx text eol=lf
|
||||
|
||||
# HTML/CSS files
|
||||
*.html text eol=lf
|
||||
*.css text eol=lf
|
||||
*.scss text eol=lf
|
||||
|
||||
# Shell scripts
|
||||
*.sh text eol=lf
|
||||
|
||||
# Denote all files that are truly binary and should not be modified.
|
||||
*.png binary
|
||||
*.jpg binary
|
||||
*.jpeg binary
|
||||
*.gif binary
|
||||
*.ico binary
|
||||
*.woff binary
|
||||
*.woff2 binary
|
||||
*.ttf binary
|
||||
*.exe binary
|
||||
*.dll binary
|
||||
360
.github/workflows/release.yml
vendored
@@ -8,15 +8,19 @@ on:
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: release-${{ github.ref_name }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: windows-latest
|
||||
- os: ubuntu-latest
|
||||
- os: macos-latest
|
||||
- os: windows-2022
|
||||
- os: ubuntu-22.04
|
||||
- os: macos-14
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -30,6 +34,38 @@ jobs:
|
||||
- name: Setup Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Add macOS targets
|
||||
if: runner.os == 'macOS'
|
||||
run: |
|
||||
rustup target add aarch64-apple-darwin x86_64-apple-darwin
|
||||
|
||||
- name: Install Linux system deps
|
||||
if: runner.os == 'Linux'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euxo pipefail
|
||||
sudo apt-get update
|
||||
# Core build tools and pkg-config
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
pkg-config \
|
||||
curl \
|
||||
wget \
|
||||
file \
|
||||
patchelf \
|
||||
libssl-dev
|
||||
# GTK/GLib stack for gdk-3.0, glib-2.0, gio-2.0
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
libgtk-3-dev \
|
||||
librsvg2-dev \
|
||||
libayatana-appindicator3-dev
|
||||
# WebKit2GTK (version differs across Ubuntu images; try 4.1 then 4.0)
|
||||
sudo apt-get install -y --no-install-recommends libwebkit2gtk-4.1-dev \
|
||||
|| sudo apt-get install -y --no-install-recommends libwebkit2gtk-4.0-dev
|
||||
# libsoup also changed major version; prefer 3.0 with fallback to 2.4
|
||||
sudo apt-get install -y --no-install-recommends libsoup-3.0-dev \
|
||||
|| sudo apt-get install -y --no-install-recommends libsoup2.4-dev
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v2
|
||||
with:
|
||||
@@ -42,7 +78,7 @@ jobs:
|
||||
run: echo "path=$(pnpm store path --silent)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Setup pnpm cache
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ${{ steps.pnpm-store.outputs.path }}
|
||||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
@@ -51,24 +87,225 @@ jobs:
|
||||
- name: Install frontend deps
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Build and Release (Tauri)
|
||||
uses: tauri-apps/tauri-action@v0
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Prepare Tauri signing key
|
||||
shell: bash
|
||||
run: |
|
||||
# 调试:检查 Secret 是否存在
|
||||
if [ -z "${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}" ]; then
|
||||
echo "❌ TAURI_SIGNING_PRIVATE_KEY Secret 为空或不存在" >&2
|
||||
echo "请检查 GitHub 仓库 Settings > Secrets and variables > Actions" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
RAW="${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}"
|
||||
# 目标:提供正确的私钥“文件路径”给 Tauri CLI,避免内容解码歧义
|
||||
KEY_PATH="$RUNNER_TEMP/tauri_signing.key"
|
||||
# 情况 1:原始两行文本(第一行以 "untrusted comment:" 开头)
|
||||
if echo "$RAW" | head -n1 | grep -q '^untrusted comment:'; then
|
||||
printf '%s\n' "$RAW" > "$KEY_PATH"
|
||||
echo "✅ 使用原始两行密钥文件格式"
|
||||
else
|
||||
# 情况 2:整体被 base64 包裹(解包后应当是两行)
|
||||
if DECODED=$(printf '%s' "$RAW" | (base64 --decode 2>/dev/null || base64 -D 2>/dev/null)) \
|
||||
&& echo "$DECODED" | head -n1 | grep -q '^untrusted comment:'; then
|
||||
printf '%s\n' "$DECODED" > "$KEY_PATH"
|
||||
echo "✅ 成功解码 base64 包裹密钥,已还原为两行文件"
|
||||
else
|
||||
# 情况 3:已是第二行(纯 Base64 一行)→ 构造两行文件
|
||||
if echo "$RAW" | grep -Eq '^[A-Za-z0-9+/=]+$'; then
|
||||
ONE=$(printf '%s' "$RAW" | tr -d '\r\n')
|
||||
printf '%s\n%s\n' "untrusted comment: tauri signing key" "$ONE" > "$KEY_PATH"
|
||||
echo "✅ 使用一行 Base64 私钥,已构造两行文件"
|
||||
else
|
||||
echo "❌ TAURI_SIGNING_PRIVATE_KEY 格式无法识别:既不是两行原文,也不是其 base64,亦非一行 base64" >&2
|
||||
echo "密钥前10个字符: $(echo "$RAW" | head -c 10)..." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
# 将“完整两行内容”作为环境变量注入(Tauri 支持传入完整私钥文本或文件路径)
|
||||
# 使用多行写入语法,保持换行以便解析
|
||||
# 将完整两行私钥内容进行 base64 编码,作为单行内容注入环境变量
|
||||
if command -v base64 >/dev/null 2>&1; then
|
||||
KEY_B64=$(base64 < "$KEY_PATH" | tr -d '\r\n')
|
||||
elif command -v openssl >/dev/null 2>&1; then
|
||||
KEY_B64=$(openssl base64 -A -in "$KEY_PATH")
|
||||
else
|
||||
KEY_B64=$(KEY_PATH="$KEY_PATH" node -e "process.stdout.write(require('fs').readFileSync(process.env.KEY_PATH).toString('base64'))")
|
||||
fi
|
||||
if [ -z "$KEY_B64" ]; then
|
||||
echo "❌ 无法生成私钥 base64 内容" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "TAURI_SIGNING_PRIVATE_KEY=$KEY_B64" >> "$GITHUB_ENV"
|
||||
if [ -n "${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}" ]; then
|
||||
echo "TAURI_SIGNING_PRIVATE_KEY_PASSWORD=${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}" >> $GITHUB_ENV
|
||||
fi
|
||||
echo "✅ Tauri signing key prepared"
|
||||
|
||||
- name: Build Tauri App (macOS)
|
||||
if: runner.os == 'macOS'
|
||||
run: pnpm tauri build --target universal-apple-darwin
|
||||
|
||||
- name: Build Tauri App (Windows)
|
||||
if: runner.os == 'Windows'
|
||||
run: pnpm tauri build
|
||||
|
||||
- name: Build Tauri App (Linux)
|
||||
if: runner.os == 'Linux'
|
||||
run: pnpm tauri build
|
||||
|
||||
- name: Prepare macOS Assets
|
||||
if: runner.os == 'macOS'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euxo pipefail
|
||||
mkdir -p release-assets
|
||||
VERSION="${GITHUB_REF_NAME}" # e.g., v3.5.0
|
||||
echo "Looking for updater artifact (.tar.gz) and .app for zip..."
|
||||
TAR_GZ=""; APP_PATH=""
|
||||
for path in \
|
||||
"src-tauri/target/universal-apple-darwin/release/bundle/macos" \
|
||||
"src-tauri/target/aarch64-apple-darwin/release/bundle/macos" \
|
||||
"src-tauri/target/x86_64-apple-darwin/release/bundle/macos" \
|
||||
"src-tauri/target/release/bundle/macos"; do
|
||||
if [ -d "$path" ]; then
|
||||
[ -z "$TAR_GZ" ] && TAR_GZ=$(find "$path" -maxdepth 1 -name "*.tar.gz" -type f | head -1 || true)
|
||||
[ -z "$APP_PATH" ] && APP_PATH=$(find "$path" -maxdepth 1 -name "*.app" -type d | head -1 || true)
|
||||
fi
|
||||
done
|
||||
if [ -z "$TAR_GZ" ]; then
|
||||
echo "No macOS .tar.gz updater artifact found" >&2
|
||||
exit 1
|
||||
fi
|
||||
# 重命名 tar.gz 为统一格式
|
||||
NEW_TAR_GZ="CC-Switch-${VERSION}-macOS.tar.gz"
|
||||
cp "$TAR_GZ" "release-assets/$NEW_TAR_GZ"
|
||||
[ -f "$TAR_GZ.sig" ] && cp "$TAR_GZ.sig" "release-assets/$NEW_TAR_GZ.sig" || echo ".sig for macOS not found yet"
|
||||
echo "macOS updater artifact copied: $NEW_TAR_GZ"
|
||||
if [ -n "$APP_PATH" ]; then
|
||||
APP_DIR=$(dirname "$APP_PATH"); APP_NAME=$(basename "$APP_PATH")
|
||||
NEW_ZIP="CC-Switch-${VERSION}-macOS.zip"
|
||||
cd "$APP_DIR"
|
||||
ditto -c -k --sequesterRsrc --keepParent "$APP_NAME" "$NEW_ZIP"
|
||||
mv "$NEW_ZIP" "$GITHUB_WORKSPACE/release-assets/"
|
||||
echo "macOS zip ready: $NEW_ZIP"
|
||||
else
|
||||
echo "No .app found to zip (optional)" >&2
|
||||
fi
|
||||
|
||||
- name: Prepare Windows Assets
|
||||
if: runner.os == 'Windows'
|
||||
shell: pwsh
|
||||
run: |
|
||||
$ErrorActionPreference = 'Stop'
|
||||
New-Item -ItemType Directory -Force -Path release-assets | Out-Null
|
||||
$VERSION = $env:GITHUB_REF_NAME # e.g., v3.5.0
|
||||
# 仅打包 MSI 安装器 + .sig(用于 Updater)
|
||||
$msi = Get-ChildItem -Path 'src-tauri/target/release/bundle/msi' -Recurse -Include *.msi -ErrorAction SilentlyContinue | Select-Object -First 1
|
||||
if ($null -eq $msi) {
|
||||
# 兜底:全局搜索 .msi
|
||||
$msi = Get-ChildItem -Path 'src-tauri/target/release/bundle' -Recurse -Include *.msi -ErrorAction SilentlyContinue | Select-Object -First 1
|
||||
}
|
||||
if ($null -ne $msi) {
|
||||
$dest = "CC-Switch-$VERSION-Windows.msi"
|
||||
Copy-Item $msi.FullName (Join-Path release-assets $dest)
|
||||
Write-Host "Installer copied: $dest"
|
||||
$sigPath = "$($msi.FullName).sig"
|
||||
if (Test-Path $sigPath) {
|
||||
Copy-Item $sigPath (Join-Path release-assets ("$dest.sig"))
|
||||
Write-Host "Signature copied: $dest.sig"
|
||||
} else {
|
||||
Write-Warning "Signature not found for $($msi.Name)"
|
||||
}
|
||||
} else {
|
||||
Write-Warning 'No Windows MSI installer found'
|
||||
}
|
||||
# 绿色版(portable):仅可执行文件打 zip(不参与 Updater)
|
||||
$exeCandidates = @(
|
||||
'src-tauri/target/release/cc-switch.exe',
|
||||
'src-tauri/target/x86_64-pc-windows-msvc/release/cc-switch.exe'
|
||||
)
|
||||
$exePath = $exeCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
|
||||
if ($null -ne $exePath) {
|
||||
$portableDir = 'release-assets/CC-Switch-Portable'
|
||||
New-Item -ItemType Directory -Force -Path $portableDir | Out-Null
|
||||
Copy-Item $exePath $portableDir
|
||||
$portableIniPath = Join-Path $portableDir 'portable.ini'
|
||||
$portableContent = @(
|
||||
'# CC Switch portable build marker',
|
||||
'portable=true'
|
||||
)
|
||||
$portableContent | Set-Content -Path $portableIniPath -Encoding UTF8
|
||||
$portableZip = "release-assets/CC-Switch-$VERSION-Windows-Portable.zip"
|
||||
Compress-Archive -Path "$portableDir/*" -DestinationPath $portableZip -Force
|
||||
Remove-Item -Recurse -Force $portableDir
|
||||
Write-Host "Windows portable zip created: CC-Switch-$VERSION-Windows-Portable.zip"
|
||||
} else {
|
||||
Write-Warning 'Portable exe not found'
|
||||
}
|
||||
|
||||
- name: Prepare Linux Assets
|
||||
if: runner.os == 'Linux'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euxo pipefail
|
||||
mkdir -p release-assets
|
||||
VERSION="${GITHUB_REF_NAME}" # e.g., v3.5.0
|
||||
# Updater artifact: AppImage(含对应 .sig)
|
||||
APPIMAGE=$(find src-tauri/target/release/bundle -name "*.AppImage" | head -1 || true)
|
||||
if [ -n "$APPIMAGE" ]; then
|
||||
NEW_APPIMAGE="CC-Switch-${VERSION}-Linux.AppImage"
|
||||
cp "$APPIMAGE" "release-assets/$NEW_APPIMAGE"
|
||||
[ -f "$APPIMAGE.sig" ] && cp "$APPIMAGE.sig" "release-assets/$NEW_APPIMAGE.sig" || echo ".sig for AppImage not found"
|
||||
echo "AppImage copied: $NEW_APPIMAGE"
|
||||
else
|
||||
echo "No AppImage found under target/release/bundle" >&2
|
||||
fi
|
||||
# 额外上传 .deb(用于手动安装,不参与 Updater)
|
||||
DEB=$(find src-tauri/target/release/bundle -name "*.deb" | head -1 || true)
|
||||
if [ -n "$DEB" ]; then
|
||||
NEW_DEB="CC-Switch-${VERSION}-Linux.deb"
|
||||
cp "$DEB" "release-assets/$NEW_DEB"
|
||||
echo "Deb package copied: $NEW_DEB"
|
||||
else
|
||||
echo "No .deb found (optional)"
|
||||
fi
|
||||
|
||||
- name: List prepared assets
|
||||
shell: bash
|
||||
run: |
|
||||
ls -la release-assets || true
|
||||
|
||||
- name: Collect Signatures
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "Collected signatures (if any alongside artifacts):"
|
||||
ls -la release-assets/*.sig || echo "No signatures found"
|
||||
|
||||
- name: Upload Release Assets
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tagName: ${{ github.ref_name }}
|
||||
releaseName: CC Switch ${{ github.ref_name }}
|
||||
releaseBody: |
|
||||
tag_name: ${{ github.ref_name }}
|
||||
name: CC Switch ${{ github.ref_name }}
|
||||
prerelease: true
|
||||
body: |
|
||||
## CC Switch ${{ github.ref_name }}
|
||||
|
||||
Claude Code 供应商切换工具(Tauri 构建)
|
||||
Claude Code 供应商切换工具
|
||||
|
||||
- Windows: .msi / NSIS 安装包
|
||||
- macOS: .dmg / .app 压缩包
|
||||
- Linux: AppImage / deb / rpm
|
||||
### 下载
|
||||
|
||||
如遇未知开发者提示,请在系统隐私与安全设置中选择“仍要打开”。
|
||||
tauriScript: pnpm tauri
|
||||
- **macOS**: `CC-Switch-${{ github.ref_name }}-macOS.zip`(解压即用)或 `CC-Switch-${{ github.ref_name }}-macOS.tar.gz`(Homebrew)
|
||||
- **Windows**: `CC-Switch-${{ github.ref_name }}-Windows.msi`(安装版)或 `CC-Switch-${{ github.ref_name }}-Windows-Portable.zip`(绿色版)
|
||||
- **Linux**: `CC-Switch-${{ github.ref_name }}-Linux.AppImage`(AppImage)或 `CC-Switch-${{ github.ref_name }}-Linux.deb`(Debian/Ubuntu)
|
||||
|
||||
---
|
||||
提示:macOS 如遇"已损坏"提示,可在终端执行:`xattr -cr "/Applications/CC Switch.app"`
|
||||
files: release-assets/*
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: List generated bundles (debug)
|
||||
if: always()
|
||||
@@ -76,3 +313,92 @@ jobs:
|
||||
run: |
|
||||
echo "Listing bundles in src-tauri/target..."
|
||||
find src-tauri/target -maxdepth 4 -type f -name "*.*" 2>/dev/null || true
|
||||
|
||||
assemble-latest-json:
|
||||
name: Assemble latest.json
|
||||
runs-on: ubuntu-22.04
|
||||
needs: release
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Prepare GH
|
||||
run: |
|
||||
gh --version || (type -p curl >/dev/null && sudo apt-get update && sudo apt-get install -y gh || true)
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Download all release assets
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euxo pipefail
|
||||
TAG="${GITHUB_REF_NAME}"
|
||||
mkdir -p dl
|
||||
gh release download "$TAG" --dir dl --repo "$GITHUB_REPOSITORY"
|
||||
ls -la dl || true
|
||||
- name: Generate latest.json
|
||||
env:
|
||||
REPO: ${{ github.repository }}
|
||||
TAG: ${{ github.ref_name }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
VERSION="${TAG#v}"
|
||||
PUB_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
base_url="https://github.com/$REPO/releases/download/$TAG"
|
||||
# 初始化空平台映射
|
||||
mac_url=""; mac_sig=""
|
||||
win_url=""; win_sig=""
|
||||
linux_url=""; linux_sig=""
|
||||
shopt -s nullglob
|
||||
for sig in dl/*.sig; do
|
||||
base=${sig%.sig}
|
||||
fname=$(basename "$base")
|
||||
url="$base_url/$fname"
|
||||
sig_content=$(cat "$sig")
|
||||
case "$fname" in
|
||||
*.tar.gz)
|
||||
# 视为 macOS updater artifact
|
||||
mac_url="$url"; mac_sig="$sig_content";;
|
||||
*.AppImage|*.appimage)
|
||||
linux_url="$url"; linux_sig="$sig_content";;
|
||||
*.msi|*.exe)
|
||||
win_url="$url"; win_sig="$sig_content";;
|
||||
esac
|
||||
done
|
||||
# 构造 JSON(仅包含存在的目标)
|
||||
tmp_json=$(mktemp)
|
||||
{
|
||||
echo '{'
|
||||
echo " \"version\": \"$VERSION\",";
|
||||
echo " \"notes\": \"Release $TAG\",";
|
||||
echo " \"pub_date\": \"$PUB_DATE\",";
|
||||
echo ' "platforms": {'
|
||||
first=1
|
||||
if [ -n "$mac_url" ] && [ -n "$mac_sig" ]; then
|
||||
# 为兼容 arm64 / x64,重复写入两个键,指向同一 universal 包
|
||||
for key in darwin-aarch64 darwin-x86_64; do
|
||||
[ $first -eq 0 ] && echo ','
|
||||
echo " \"$key\": {\"signature\": \"$mac_sig\", \"url\": \"$mac_url\"}"
|
||||
first=0
|
||||
done
|
||||
fi
|
||||
if [ -n "$win_url" ] && [ -n "$win_sig" ]; then
|
||||
[ $first -eq 0 ] && echo ','
|
||||
echo " \"windows-x86_64\": {\"signature\": \"$win_sig\", \"url\": \"$win_url\"}"
|
||||
first=0
|
||||
fi
|
||||
if [ -n "$linux_url" ] && [ -n "$linux_sig" ]; then
|
||||
[ $first -eq 0 ] && echo ','
|
||||
echo " \"linux-x86_64\": {\"signature\": \"$linux_sig\", \"url\": \"$linux_url\"}"
|
||||
first=0
|
||||
fi
|
||||
echo ' }'
|
||||
echo '}'
|
||||
} > "$tmp_json"
|
||||
echo "Generated latest.json:" && cat "$tmp_json"
|
||||
mv "$tmp_json" latest.json
|
||||
- name: Upload latest.json to release
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euxo pipefail
|
||||
gh release upload "$GITHUB_REF_NAME" latest.json --clobber --repo "$GITHUB_REPOSITORY"
|
||||
|
||||
3
.gitignore
vendored
@@ -9,3 +9,6 @@ release/
|
||||
.npmrc
|
||||
CLAUDE.md
|
||||
AGENTS.md
|
||||
/.claude
|
||||
/.vscode
|
||||
vitest-report.json
|
||||
|
||||
1
.node-version
Normal file
@@ -0,0 +1 @@
|
||||
v22.4.1
|
||||
334
CHANGELOG.md
@@ -5,9 +5,290 @@ All notable changes to CC Switch will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [3.6.0] - 2025-11-07
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Provider Duplicate** - Quick duplicate existing provider configurations for easy variant creation
|
||||
- **Edit Mode Toggle** - Show/hide drag handles to optimize editing experience
|
||||
- **Custom Endpoint Management** - Support multi-endpoint configuration for aggregator providers
|
||||
- **Usage Query Enhancements**
|
||||
- Auto-refresh interval: Support periodic automatic usage query
|
||||
- Test Script API: Validate JavaScript scripts before execution
|
||||
- Template system expansion: Custom blank template, support for access token and user ID parameters
|
||||
- **Configuration Editor Improvements**
|
||||
- Add JSON format button
|
||||
- Real-time TOML syntax validation for Codex configuration
|
||||
- **Auto-sync on Directory Change** - When switching Claude/Codex config directories (e.g., WSL environment), automatically sync current provider to new directory without manual operation
|
||||
- **Load Live Config When Editing Active Provider** - When editing the currently active provider, prioritize displaying the actual effective configuration to protect user manual modifications
|
||||
- **New Provider Presets** - DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax
|
||||
- **Partner Promotion Mechanism** - Support ecosystem partner promotion (e.g., Zhipu GLM Z.ai)
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- **Configuration Directory Switching**
|
||||
- Introduced unified post-change sync utility (`postChangeSync.ts`)
|
||||
- Auto-sync current providers to new directory when changing Claude/Codex config directories
|
||||
- Perfect support for WSL environment switching
|
||||
- Auto-sync after config import to ensure immediate effectiveness
|
||||
- Use Result pattern for graceful error handling without blocking main flow
|
||||
- Distinguish "fully successful" and "partially successful" states for precise user feedback
|
||||
- **UI/UX Enhancements**
|
||||
- Provider cards: Unique icons and color identification
|
||||
- Unified border design system across all components
|
||||
- Drag interaction optimization: Push effect animation, improved handle icons
|
||||
- Enhanced current provider visual feedback
|
||||
- Dialog size standardization and layout consistency
|
||||
- Form experience: Optimized model placeholders, simplified provider hints, category-specific hints
|
||||
- **Complete Internationalization Coverage**
|
||||
- Error messages internationalization
|
||||
- Tray menu internationalization
|
||||
- All UI components internationalization
|
||||
- **Usage Display Moved Inline** - Usage display moved next to enable button
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Configuration Sync**
|
||||
- Fixed `apiKeyUrl` priority issue
|
||||
- Fixed MCP sync-to-other-side functionality failure
|
||||
- Fixed sync issues after config import
|
||||
- Prevent silent fallback and data loss on config error
|
||||
- **Usage Query**
|
||||
- Fixed auto-query interval timing issue
|
||||
- Ensure refresh button shows loading animation on click
|
||||
- **UI Issues**
|
||||
- Fixed name collision error (`get_init_error` command)
|
||||
- Fixed language setting rollback after successful save
|
||||
- Fixed language switch state reset (dependency cycle)
|
||||
- Fixed edit mode button alignment
|
||||
- **Configuration Management**
|
||||
- Fixed Codex API Key auto-sync
|
||||
- Fixed endpoint speed test functionality
|
||||
- Fixed provider duplicate insertion position (next to original provider)
|
||||
- Fixed custom endpoint preservation in edit mode
|
||||
- **Startup Issues**
|
||||
- Force exit on config error (no silent fallback)
|
||||
- Eliminate code duplication causing initialization errors
|
||||
|
||||
### 🏗️ Technical Improvements (For Developers)
|
||||
|
||||
**Backend Refactoring (Rust)** - Completed 5-phase refactoring:
|
||||
- **Phase 1**: Unified error handling (`AppError` + i18n error messages)
|
||||
- **Phase 2**: Command layer split by domain (`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
|
||||
- **Phase 3**: Integration tests and transaction mechanism (config snapshot + failure rollback)
|
||||
- **Phase 4**: Extracted Service layer (`services/{provider,mcp,config,speedtest}.rs`)
|
||||
- **Phase 5**: Concurrency optimization (`RwLock` instead of `Mutex`, scoped guard to avoid deadlock)
|
||||
|
||||
**Frontend Refactoring (React + TypeScript)** - Completed 4-stage refactoring:
|
||||
- **Stage 1**: Test infrastructure (vitest + MSW + @testing-library/react)
|
||||
- **Stage 2**: Extracted custom hooks (`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport`, etc.)
|
||||
- **Stage 3**: Component splitting and business logic extraction
|
||||
- **Stage 4**: Code cleanup and formatting unification
|
||||
|
||||
**Testing System**:
|
||||
- Hooks unit tests 100% coverage
|
||||
- Integration tests covering key processes (App, SettingsDialog, MCP Panel)
|
||||
- MSW mocking backend API to ensure test independence
|
||||
|
||||
**Code Quality**:
|
||||
- Unified parameter format: All Tauri commands migrated to camelCase (Tauri 2 specification)
|
||||
- `AppType` renamed to `AppId`: Semantically clearer
|
||||
- Unified parsing with `FromStr` trait: Centralized `app` parameter parsing
|
||||
- Eliminate code duplication: DRY violations cleanup
|
||||
- Remove unused code: `missing_param` helper function, deprecated `tauri-api.ts`, redundant `KimiModelSelector` component
|
||||
|
||||
**Internal Optimizations**:
|
||||
- **Removed Legacy Migration Logic**: v3.6 removed v1 config auto-migration and copy file scanning logic
|
||||
- ✅ **Impact**: Improved startup performance, cleaner code
|
||||
- ✅ **Compatibility**: v2 format configs fully compatible, no action required
|
||||
- ⚠️ **Note**: Users upgrading from v3.1.0 or earlier should first upgrade to v3.2.x or v3.5.x for one-time migration, then upgrade to v3.6
|
||||
- **Command Parameter Standardization**: Backend unified to use `app` parameter (values: `claude` or `codex`)
|
||||
- ✅ **Impact**: More standardized code, friendlier error prompts
|
||||
- ✅ **Compatibility**: Frontend fully adapted, users don't need to care about this change
|
||||
|
||||
### 📦 Dependencies
|
||||
|
||||
- Updated to Tauri 2.8.x
|
||||
- Updated to TailwindCSS 4.x
|
||||
- Updated to TanStack Query v5.90.x
|
||||
- Maintained React 18.2.x and TypeScript 5.3.x
|
||||
|
||||
## [3.5.0] - 2025-01-15
|
||||
|
||||
### ⚠ Breaking Changes
|
||||
|
||||
- Tauri 命令仅接受参数 `app`(取值:`claude`/`codex`);移除对 `app_type`/`appType` 的兼容。
|
||||
- 前端类型命名统一为 `AppId`(移除 `AppType` 导出),变量命名统一为 `appId`。
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **MCP (Model Context Protocol) Management** - Complete MCP server configuration management system
|
||||
- Add, edit, delete, and toggle MCP servers in `~/.claude.json`
|
||||
- Support for stdio and http server types with command validation
|
||||
- Built-in templates for popular MCP servers (mcp-fetch, etc.)
|
||||
- Real-time enable/disable toggle for MCP servers
|
||||
- Atomic file writing to prevent configuration corruption
|
||||
- **Configuration Import/Export** - Backup and restore your provider configurations
|
||||
- Export all configurations to JSON file with one click
|
||||
- Import configurations with validation and automatic backup
|
||||
- Automatic backup rotation (keeps 10 most recent backups)
|
||||
- Progress modal with detailed status feedback
|
||||
- **Endpoint Speed Testing** - Test API endpoint response times
|
||||
- Measure latency to different provider endpoints
|
||||
- Visual indicators for connection quality
|
||||
- Help users choose the fastest provider
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- Complete internationalization (i18n) coverage for all UI components
|
||||
- Enhanced error handling and user feedback throughout the application
|
||||
- Improved configuration file management with better validation
|
||||
- Added new provider presets: Longcat, kat-coder
|
||||
- Updated GLM provider configurations with latest models
|
||||
- Refined UI/UX with better spacing, icons, and visual feedback
|
||||
- Enhanced tray menu functionality and responsiveness
|
||||
- **Standardized release artifact naming** - All platform releases now use consistent version-tagged filenames:
|
||||
- macOS: `CC-Switch-v{version}-macOS.tar.gz` / `.zip`
|
||||
- Windows: `CC-Switch-v{version}-Windows.msi` / `-Portable.zip`
|
||||
- Linux: `CC-Switch-v{version}-Linux.AppImage` / `.deb`
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Fixed layout shifts during provider switching
|
||||
- Improved config file path handling across different platforms
|
||||
- Better error messages for configuration validation failures
|
||||
- Fixed various edge cases in configuration import/export
|
||||
|
||||
### 📦 Technical Details
|
||||
|
||||
- Enhanced `import_export.rs` module with backup management
|
||||
- New `claude_mcp.rs` module for MCP configuration handling
|
||||
- Improved state management and lock handling in Rust backend
|
||||
- Better TypeScript type safety across the codebase
|
||||
|
||||
## [3.4.0] - 2025-10-01
|
||||
|
||||
### ✨ Features
|
||||
|
||||
- Enable internationalization via i18next with a Chinese default and English fallback, plus an in-app language switcher
|
||||
- Add Claude plugin sync while retiring the legacy VS Code integration controls (Codex no longer requires settings.json edits)
|
||||
- Extend provider presets with optional API key URLs and updated models, including DeepSeek-V3.1-Terminus and Qwen3-Max
|
||||
- Support portable mode launches and enforce a single running instance to avoid conflicts
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- Allow minimizing the window to the system tray and add macOS Dock visibility management for tray workflows
|
||||
- Refresh the Settings modal with a scrollable layout, save icon, and cleaner language section
|
||||
- Smooth provider toggle states with consistent button widths/icons and prevent layout shifts when switching between Claude and Codex
|
||||
- Adjust the Windows MSI installer to target per-user LocalAppData and improve component tracking reliability
|
||||
|
||||
### 🐛 Fixes
|
||||
|
||||
- Remove the unnecessary OpenAI auth requirement from third-party provider configurations
|
||||
- Fix layout shifts while switching app types with Claude plugin sync enabled
|
||||
- Align Enable/In Use button states to avoid visual jank across app views
|
||||
|
||||
## [3.3.0] - 2025-09-22
|
||||
|
||||
### ✨ Features
|
||||
|
||||
- Add “Apply to VS Code / Remove from VS Code” actions on provider cards, writing settings for Code/Insiders/VSCodium variants _(Removed in 3.4.x)_
|
||||
- Enable VS Code auto-sync by default with window broadcast and tray hooks so Codex switches sync silently _(Removed in 3.4.x)_
|
||||
- Extend the Codex provider wizard with display name, dedicated API key URL, and clearer guidance
|
||||
- Introduce shared common config snippets with JSON/TOML reuse, validation, and consistent error surfaces
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- Keep the tray menu responsive when the window is hidden and standardize button styling and copy
|
||||
- Disable modal backdrop blur on Linux (WebKitGTK/Wayland) to avoid freezes; restore the window when clicking the macOS Dock icon
|
||||
- Support overriding config directories on WSL, refine placeholders/descriptions, and fix VS Code button wrapping on Windows
|
||||
- Add a `created_at` timestamp to provider records for future sorting and analytics
|
||||
|
||||
### 🐛 Fixes
|
||||
|
||||
- Correct regex escapes and common snippet trimming in the Codex wizard to prevent validation issues
|
||||
- Harden the VS Code sync flow with more reliable TOML/JSON parsing while reducing layout jank
|
||||
- Bundle `@codemirror/lint` to reinstate live linting in config editors
|
||||
|
||||
## [3.2.0] - 2025-09-13
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- System tray provider switching with dynamic menu for Claude/Codex
|
||||
- Frontend receives `provider-switched` events and refreshes active app
|
||||
- Built-in update flow via Tauri Updater plugin with dismissible UpdateBadge
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- Single source of truth for provider configs; no duplicate copy files
|
||||
- One-time migration imports existing copies into `config.json` and archives originals
|
||||
- Duplicate provider de-duplication by name + API key at startup
|
||||
- Atomic writes for Codex `auth.json` + `config.toml` with rollback on failure
|
||||
- Logging standardized (Rust): use `log::{info,warn,error}` instead of stdout prints
|
||||
- Tailwind v4 integration and refined dark mode handling
|
||||
|
||||
### 🐛 Fixes
|
||||
|
||||
- Remove/minimize debug console logs in production builds
|
||||
- Fix CSS minifier warnings for scrollbar pseudo-elements
|
||||
- Prettier formatting across codebase for consistent style
|
||||
|
||||
### 📦 Dependencies
|
||||
|
||||
- Tauri: 2.8.x (core, updater, process, opener, log plugins)
|
||||
- React: 18.2.x · TypeScript: 5.3.x · Vite: 5.x
|
||||
|
||||
### 🔄 Notes
|
||||
|
||||
- `connect-src` CSP remains permissive for compatibility; can be tightened later as needed
|
||||
|
||||
## [3.1.1] - 2025-09-03
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Fixed the default codex config.toml to match the latest modifications
|
||||
- Improved provider configuration UX with custom option
|
||||
|
||||
### 📝 Documentation
|
||||
|
||||
- Updated README with latest information
|
||||
|
||||
## [3.1.0] - 2025-09-01
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Added Codex application support** - Now supports both Claude Code and Codex configuration management
|
||||
- Manage auth.json and config.toml for Codex
|
||||
- Support for backup and restore operations
|
||||
- Preset providers for Codex (Official, PackyCode)
|
||||
- API Key auto-write to auth.json when using presets
|
||||
- **New UI components**
|
||||
- App switcher with segmented control design
|
||||
- Dual editor form for Codex configuration
|
||||
- Pills-style app switcher with consistent button widths
|
||||
- **Enhanced configuration management**
|
||||
- Multi-app config v2 structure (claude/codex)
|
||||
- Automatic v1→v2 migration with backup
|
||||
- OPENAI_API_KEY validation for non-official presets
|
||||
- TOML syntax validation for config.toml
|
||||
|
||||
### 🔧 Technical Improvements
|
||||
|
||||
- Unified Tauri command API with app_type parameter
|
||||
- Backward compatibility for app/appType parameters
|
||||
- Added get_config_status/open_config_folder/open_external commands
|
||||
- Improved error handling for empty config.toml
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Fixed config path reporting and folder opening for Codex
|
||||
- Corrected default import behavior when main config is missing
|
||||
- Fixed non_snake_case warnings in commands.rs
|
||||
|
||||
## [3.0.0] - 2025-08-27
|
||||
|
||||
### 🚀 Major Changes
|
||||
|
||||
- **Complete migration from Electron to Tauri 2.0** - The application has been completely rewritten using Tauri, resulting in:
|
||||
- **90% reduction in bundle size** (from ~150MB to ~15MB)
|
||||
- **Significantly improved startup performance**
|
||||
@@ -15,12 +296,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- **Enhanced security** with Rust backend
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Native window controls** with transparent title bar on macOS
|
||||
- **Improved file system operations** using Rust for better performance
|
||||
- **Enhanced security model** with explicit permission declarations
|
||||
- **Better platform detection** using Tauri's native APIs
|
||||
|
||||
### 🔧 Technical Improvements
|
||||
|
||||
- Migrated from Electron IPC to Tauri command system
|
||||
- Replaced Node.js file operations with Rust implementations
|
||||
- Implemented proper CSP (Content Security Policy) for enhanced security
|
||||
@@ -28,23 +311,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Integrated Rust cargo fmt and clippy for code quality
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Fixed bundle identifier conflict on macOS (changed from .app to .desktop)
|
||||
- Resolved platform detection issues
|
||||
- Improved error handling in configuration management
|
||||
|
||||
### 📦 Dependencies
|
||||
|
||||
- **Tauri**: 2.8.2
|
||||
- **React**: 18.2.0
|
||||
- **TypeScript**: 5.3.0
|
||||
- **Vite**: 5.0.0
|
||||
|
||||
### 🔄 Migration Notes
|
||||
|
||||
For users upgrading from v2.x (Electron version):
|
||||
|
||||
- Configuration files remain compatible - no action required
|
||||
- The app will automatically migrate your existing provider configurations
|
||||
- Window position and size preferences have been reset to defaults
|
||||
|
||||
#### Backup on v1→v2 Migration (cc-switch internal config)
|
||||
|
||||
- When the app detects an old v1 config structure at `~/.cc-switch/config.json`, it now creates a timestamped backup before writing the new v2 structure.
|
||||
- Backup location: `~/.cc-switch/config.v1.backup.<timestamp>.json`
|
||||
- This only concerns cc-switch's own metadata file; your actual provider files under `~/.claude/` and `~/.codex/` are untouched.
|
||||
|
||||
### 🛠️ Development
|
||||
|
||||
- Added `pnpm typecheck` command for TypeScript validation
|
||||
- Added `pnpm format` and `pnpm format:check` for code formatting
|
||||
- Rust code now uses cargo fmt for consistent formatting
|
||||
@@ -52,6 +346,7 @@ For users upgrading from v2.x (Electron version):
|
||||
## [2.0.0] - Previous Electron Release
|
||||
|
||||
### Features
|
||||
|
||||
- Multi-provider configuration management
|
||||
- Quick provider switching
|
||||
- Import/export configurations
|
||||
@@ -62,6 +357,43 @@ For users upgrading from v2.x (Electron version):
|
||||
## [1.0.0] - Initial Release
|
||||
|
||||
### Features
|
||||
|
||||
- Basic provider management
|
||||
- Claude Code integration
|
||||
- Configuration file handling
|
||||
- Configuration file handling
|
||||
## [Unreleased]
|
||||
|
||||
### ⚠️ Breaking Changes
|
||||
|
||||
- **Runtime auto-migration from v1 to v2 config format has been removed**
|
||||
- `MultiAppConfig::load()` no longer automatically migrates v1 configs
|
||||
- When a v1 config is detected, the app now returns a clear error with migration instructions
|
||||
- **Migration path**: Install v3.2.x to perform one-time auto-migration, OR manually edit `~/.cc-switch/config.json` to v2 format
|
||||
- **Rationale**: Separates concerns (load() should be read-only), fail-fast principle, simplifies maintenance
|
||||
- Related: `app_config.rs` (v1 detection improved with structural analysis), `app_config_load.rs` (comprehensive test coverage added)
|
||||
|
||||
- **Legacy v1 copy file migration logic has been removed**
|
||||
- Removed entire `migration.rs` module (435 lines) that handled one-time migration from v3.1.0 to v3.2.0
|
||||
- No longer scans/merges legacy copy files (`settings-*.json`, `auth-*.json`, `config-*.toml`)
|
||||
- No longer archives copy files or performs automatic deduplication
|
||||
- **Migration path**: Users upgrading from v3.1.0 must first upgrade to v3.2.x to automatically migrate their configurations
|
||||
- **Benefits**: Improved startup performance (no file scanning), reduced code complexity, cleaner codebase
|
||||
|
||||
- **Tauri commands now only accept `app` parameter**
|
||||
- Removed legacy `app_type`/`appType` compatibility paths
|
||||
- Explicit error with available values when unknown `app` is provided
|
||||
|
||||
### 🔧 Improvements
|
||||
|
||||
- Unified `AppType` parsing: centralized to `FromStr` implementation, command layer no longer implements separate `parse_app()`, reducing code duplication and drift
|
||||
- Localized and user-friendly error messages: returns bilingual (Chinese/English) hints for unsupported `app` values with a list of available options
|
||||
- Simplified startup logic: Only ensures config structure exists, no migration overhead
|
||||
|
||||
### 🧪 Tests
|
||||
|
||||
- Added unit tests covering `AppType::from_str`: case sensitivity, whitespace trimming, unknown value error messages
|
||||
- Added comprehensive config loading tests:
|
||||
- `load_v1_config_returns_error_and_does_not_write`
|
||||
- `load_v1_with_extra_version_still_treated_as_v1`
|
||||
- `load_invalid_json_returns_parse_error_and_does_not_write`
|
||||
- `load_valid_v2_config_succeeds`
|
||||
|
||||
527
README.md
@@ -1,143 +1,516 @@
|
||||
# Claude Code 供应商切换器
|
||||
# Claude Code & Codex Provider Switcher
|
||||
|
||||
[](https://github.com/jasonyoung/cc-switch/releases)
|
||||
[](https://github.com/jasonyoung/cc-switch/releases)
|
||||
[](https://tauri.app/)
|
||||
<div align="center">
|
||||
|
||||
一个用于管理和切换 Claude Code 不同供应商配置的桌面应用。
|
||||
[](https://github.com/farion1231/cc-switch/releases)
|
||||
[](https://github.com/farion1231/cc-switch/releases)
|
||||
[](https://tauri.app/)
|
||||
|
||||
> **v3.0.0 重大更新**:从 Electron 完全迁移到 Tauri 2.0,应用体积减少 85%(从 ~80MB 降至 ~12MB),启动速度提升 10 倍!
|
||||
English | [中文](README_ZH.md) | [Changelog](CHANGELOG.md)
|
||||
|
||||
## 功能特性
|
||||
A desktop application for managing and switching between different provider configurations & MCP for Claude Code and Codex.
|
||||
|
||||
- **极速启动** - 基于 Tauri 2.0,原生性能,秒开应用
|
||||
- 一键切换不同供应商
|
||||
- Qwen coder、kimi k2、智谱 GLM、DeepSeek v3.1、packycode 等预设供应商只需要填写 key 即可一键配置
|
||||
- 支持添加自定义供应商
|
||||
- 简洁美观的图形界面
|
||||
- 信息存储在本地 ~/.cc-switch/config.json,无隐私风险
|
||||
- 超小体积 - 仅 ~5MB 安装包
|
||||
</div>
|
||||
|
||||
## 界面预览
|
||||
## ❤️ Sponsor
|
||||
|
||||
### 主界面
|
||||

|
||||
|
||||

|
||||
This project is sponsored by Z.ai, supporting us with their GLM CODING PLAN.
|
||||
|
||||
### 添加供应商
|
||||
GLM CODING PLAN is a subscription service designed for AI coding, starting at just $3/month. It provides access to their flagship GLM-4.6 model across 10+ popular AI coding tools (Claude Code, Cline, Roo Code, etc.), offering developers top-tier, fast, and stable coding experiences.
|
||||
|
||||

|
||||
Get 10% OFF the GLM CODING PLAN with [this link](https://z.ai/subscribe?ic=8JVLJQFSKB)!
|
||||
|
||||
## 下载安装
|
||||
## Release Notes
|
||||
|
||||
### 系统要求
|
||||
> **v3.6.0**: Added edit mode (provider duplication, manual sorting), custom endpoint management, usage query features. Optimized config directory switching experience (perfect WSL environment support). Added multiple provider presets (DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax). Completed full-stack architecture refactoring and testing infrastructure.
|
||||
|
||||
- **Windows**: Windows 10 及以上
|
||||
- **macOS**: macOS 10.15 (Catalina) 及以上
|
||||
- **Linux**: Ubuntu 20.04+ / Debian 11+ / Fedora 34+ 等主流发行版
|
||||
> v3.5.0: Added MCP management, config import/export, endpoint speed testing. Complete i18n coverage. Added Longcat and kat-coder presets. Standardized release file naming conventions.
|
||||
|
||||
### Windows 用户
|
||||
> v3.4.0: Added i18next internationalization, support for new models (qwen-3-max, GLM-4.6, DeepSeek-V3.2-Exp), Claude plugin, single-instance daemon, tray minimize, and installer optimizations.
|
||||
|
||||
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch_3.0.0_x64.msi` 或 `.exe` 安装包。
|
||||
> v3.3.0: One-click VS Code Codex plugin configuration/removal (auto-sync by default), Codex common config snippets, enhanced custom wizard, WSL environment support, cross-platform tray and UI optimizations. (VS Code write feature deprecated in v3.4.x)
|
||||
|
||||
### macOS 用户
|
||||
> v3.2.0: Brand new UI, macOS system tray, built-in updater, atomic write with rollback, improved dark mode, Single Source of Truth (SSOT) with one-time migration/archival.
|
||||
|
||||
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch_3.0.0_x64.dmg` (Intel) 或 `CC-Switch_3.0.0_aarch64.dmg` (Apple Silicon)。
|
||||
> v3.1.0: Added Codex provider management with one-click switching. Import current Codex config as default provider. Auto-backup before internal config v1 → v2 migration (see "Migration & Archival" below).
|
||||
|
||||
### Linux 用户
|
||||
> v3.0.0 Major Update: Complete migration from Electron to Tauri 2.0. Significantly reduced app size and greatly improved startup performance.
|
||||
|
||||
从 [Releases](../../releases) 页面下载最新版本的 `.AppImage` 或 `.deb` 包。
|
||||
## Features (v3.6.0)
|
||||
|
||||
## 使用说明
|
||||
### Core Features
|
||||
|
||||
1. 点击"添加供应商"添加你的 API 配置
|
||||
2. 选择要使用的供应商,点击单选按钮切换
|
||||
3. 配置会自动保存到 Claude Code 的配置文件中
|
||||
4. 重启或者新打开终端以生效
|
||||
- **MCP (Model Context Protocol) Management**: Complete MCP server configuration management system
|
||||
- Support for stdio and http server types with command validation
|
||||
- Built-in templates for popular MCP servers (e.g., mcp-fetch)
|
||||
- Real-time enable/disable MCP servers with atomic file writes to prevent configuration corruption
|
||||
- **Config Import/Export**: Backup and restore your provider configurations
|
||||
- One-click export all configurations to JSON file
|
||||
- Import configs with automatic validation and backup, auto-rotate backups (keep 10 most recent)
|
||||
- Auto-sync to live config files after import to ensure immediate effect
|
||||
- **Endpoint Speed Testing**: Test API endpoint response times
|
||||
- Measure latency to different provider endpoints with visual connection quality indicators
|
||||
- Help users choose the fastest provider
|
||||
- **Internationalization & Language Switching**: Complete i18next i18n coverage (including error messages, tray menu, all UI components)
|
||||
- **Claude Plugin Sync**: Built-in button to apply or restore Claude plugin configurations with one click. Takes effect immediately after switching providers.
|
||||
|
||||
## 开发
|
||||
### v3.6 New Features
|
||||
|
||||
### 环境要求
|
||||
- **Provider Duplication**: Quickly duplicate existing provider configs to easily create variants
|
||||
- **Manual Sorting**: Drag and drop to manually reorder providers
|
||||
- **Custom Endpoint Management**: Support multi-endpoint configuration for aggregator providers
|
||||
- **Usage Query Features**
|
||||
- Auto-refresh interval: Supports periodic automatic usage queries
|
||||
- Test Script API: Validate JavaScript scripts before execution
|
||||
- Template system expansion: Custom blank templates, support for access token and user ID parameters
|
||||
- **Config Editor Improvements**
|
||||
- Added JSON format button
|
||||
- Real-time TOML syntax validation (for Codex configs)
|
||||
- **Auto-sync on Directory Change**: When switching Claude/Codex config directories (e.g., switching to WSL environment), automatically sync current provider to new directory to avoid config file conflicts
|
||||
- **Load Live Config When Editing Active Provider**: When editing the currently active provider, prioritize displaying the actual effective configuration to protect user manual modifications
|
||||
- **New Provider Presets**: DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax
|
||||
- **Partner Promotion Mechanism**: Support ecosystem partner promotion (e.g., Zhipu GLM Z.ai)
|
||||
|
||||
### v3.6 Architecture Improvements
|
||||
|
||||
- **Backend Refactoring**: Completed 5-phase refactoring (unified error handling → command layer split → integration tests → Service layer extraction → concurrency optimization)
|
||||
- **Frontend Refactoring**: Completed 4-stage refactoring (test infrastructure → Hooks extraction → component splitting → code cleanup)
|
||||
- **Testing System**: 100% Hooks unit test coverage, integration tests covering critical flows (vitest + MSW + @testing-library/react)
|
||||
|
||||
### System Features
|
||||
|
||||
- **System Tray & Window Behavior**: Window can minimize to tray, macOS supports hide/show Dock in tray mode, tray switching syncs Claude/Codex/plugin status.
|
||||
- **Single Instance**: Ensures only one instance runs at a time to avoid multi-instance conflicts.
|
||||
- **Standardized Release Naming**: All platform release files use consistent version-tagged naming (macOS: `.tar.gz` / `.zip`, Windows: `.msi` / `-Portable.zip`, Linux: `.AppImage` / `.deb`).
|
||||
|
||||
## Screenshots
|
||||
|
||||
### Main Interface
|
||||
|
||||

|
||||
|
||||
### Add Provider
|
||||
|
||||

|
||||
|
||||
## Download & Installation
|
||||
|
||||
### System Requirements
|
||||
|
||||
- **Windows**: Windows 10 and above
|
||||
- **macOS**: macOS 10.15 (Catalina) and above
|
||||
- **Linux**: Ubuntu 22.04+ / Debian 11+ / Fedora 34+ and other mainstream distributions
|
||||
|
||||
### Windows Users
|
||||
|
||||
Download the latest `CC-Switch-v{version}-Windows.msi` installer or `CC-Switch-v{version}-Windows-Portable.zip` portable version from the [Releases](../../releases) page.
|
||||
|
||||
### macOS Users
|
||||
|
||||
**Method 1: Install via Homebrew (Recommended)**
|
||||
|
||||
```bash
|
||||
brew tap farion1231/ccswitch
|
||||
brew install --cask cc-switch
|
||||
```
|
||||
|
||||
Update:
|
||||
|
||||
```bash
|
||||
brew upgrade --cask cc-switch
|
||||
```
|
||||
|
||||
**Method 2: Manual Download**
|
||||
|
||||
Download `CC-Switch-v{version}-macOS.zip` from the [Releases](../../releases) page and extract to use.
|
||||
|
||||
> **Note**: Since the author doesn't have an Apple Developer account, you may see an "unidentified developer" warning on first launch. Please close it first, then go to "System Settings" → "Privacy & Security" → click "Open Anyway", and you'll be able to open it normally afterwards.
|
||||
|
||||
### Linux Users
|
||||
|
||||
Download the latest `CC-Switch-v{version}-Linux.deb` package or `CC-Switch-v{version}-Linux.AppImage` from the [Releases](../../releases) page.
|
||||
|
||||
## Usage Guide
|
||||
|
||||
1. Click "Add Provider" to add your API configuration
|
||||
2. Switching methods:
|
||||
- Select a provider on the main interface and click switch
|
||||
- Or directly select target provider from "System Tray (Menu Bar)" for immediate effect
|
||||
3. Switching will write to the corresponding app's "live config file" (Claude: `settings.json`; Codex: `auth.json` + `config.toml`)
|
||||
4. Restart or open new terminal to ensure it takes effect
|
||||
5. To switch back to official login, select "Official Login" from presets and switch; after restarting terminal, follow the official login process
|
||||
|
||||
### MCP Configuration Guide (v3.5.x)
|
||||
|
||||
- Management Location: All MCP server definitions are centrally saved in `~/.cc-switch/config.json` (categorized by client `claude` / `codex`)
|
||||
- Sync Mechanism:
|
||||
- Enabled Claude MCP servers are projected to `~/.claude.json` (path may vary with override directory)
|
||||
- Enabled Codex MCP servers are projected to `~/.codex/config.toml`
|
||||
- Validation & Normalization: Auto-validate field legality (stdio/http) when adding/importing, and auto-fix/populate keys like `id`
|
||||
- Import Sources: Support importing from `~/.claude.json` and `~/.codex/config.toml`; existing entries only force `enabled=true`, don't override other fields
|
||||
|
||||
### Check for Updates
|
||||
|
||||
- Click "Check for Updates" in Settings. If built-in Updater config is available, it will detect and download directly; otherwise, it will fall back to opening the Releases page
|
||||
|
||||
### Codex Guide (SSOT)
|
||||
|
||||
- Config Directory: `~/.codex/`
|
||||
- Live main config: `auth.json` (required), `config.toml` (can be empty)
|
||||
- API Key Field: Uses `OPENAI_API_KEY` in `auth.json`
|
||||
- Switching Behavior (no longer writes "copy files"):
|
||||
- Provider configs are uniformly saved in `~/.cc-switch/config.json`
|
||||
- When switching, writes target provider back to live files (`auth.json` + `config.toml`)
|
||||
- Uses "atomic write + rollback on failure" to avoid half-written state; `config.toml` can be empty
|
||||
- Import Default: When the app has no providers, creates a default entry from existing live main config and sets it as current
|
||||
- Official Login: Can switch to preset "Codex Official Login", restart terminal and follow official login process
|
||||
|
||||
### Claude Code Guide (SSOT)
|
||||
|
||||
- Config Directory: `~/.claude/`
|
||||
- Live main config: `settings.json` (preferred) or legacy-compatible `claude.json`
|
||||
- API Key Field: `env.ANTHROPIC_AUTH_TOKEN`
|
||||
- Switching Behavior (no longer writes "copy files"):
|
||||
- Provider configs are uniformly saved in `~/.cc-switch/config.json`
|
||||
- When switching, writes target provider JSON directly to live file (preferring `settings.json`)
|
||||
- When editing current provider, writes live first successfully, then updates app main config to ensure consistency
|
||||
- Import Default: When the app has no providers, creates a default entry from existing live main config and sets it as current
|
||||
- Official Login: Can switch to preset "Claude Official Login", restart terminal and use `/login` to complete login
|
||||
|
||||
### Migration & Archival
|
||||
|
||||
#### v3.6 Technical Improvements
|
||||
|
||||
**Internal Optimizations (User Transparent)**:
|
||||
|
||||
- **Removed Legacy Migration Logic**: v3.6 removed v1 config auto-migration and copy file scanning logic
|
||||
- ✅ **Impact**: Improved startup performance, cleaner code
|
||||
- ✅ **Compatibility**: v2 format configs are fully compatible, no action required
|
||||
- ⚠️ **Note**: Users upgrading from v3.1.0 or earlier should first upgrade to v3.2.x or v3.5.x for one-time migration, then upgrade to v3.6
|
||||
|
||||
- **Command Parameter Standardization**: Backend unified to use `app` parameter (values: `claude` or `codex`)
|
||||
- ✅ **Impact**: More standardized code, friendlier error messages
|
||||
- ✅ **Compatibility**: Frontend fully adapted, users don't need to care about this change
|
||||
|
||||
#### Startup Failure & Recovery
|
||||
|
||||
- Trigger Conditions: Triggered when `~/.cc-switch/config.json` doesn't exist, is corrupted, or fails to parse.
|
||||
- User Action: Check JSON syntax according to popup prompt, or restore from backup files.
|
||||
- Backup Location & Rotation: `~/.cc-switch/backups/backup_YYYYMMDD_HHMMSS.json` (keep up to 10, see `src-tauri/src/services/config.rs`).
|
||||
- Exit Strategy: To protect data safety, the app will show a popup and force exit when the above errors occur; restart after fixing.
|
||||
|
||||
#### Migration Mechanism (v3.2.0+)
|
||||
|
||||
- One-time Migration: First launch of v3.2.0+ will scan old "copy files" and merge into `~/.cc-switch/config.json`
|
||||
- Claude: `~/.claude/settings-*.json` (excluding `settings.json` / legacy `claude.json`)
|
||||
- Codex: `~/.codex/auth-*.json` and `config-*.toml` (merged in pairs by name)
|
||||
- Deduplication & Current Item: Deduplicate by "name (case-insensitive) + API Key"; if current is empty, set live merged item as current
|
||||
- Archival & Cleanup:
|
||||
- Archive directory: `~/.cc-switch/archive/<timestamp>/<category>/...`
|
||||
- Delete original copies after successful archival; keep original files on failure (conservative strategy)
|
||||
- v1 → v2 Structure Upgrade: Additionally generates `~/.cc-switch/config.v1.backup.<timestamp>.json` for rollback
|
||||
- Note: After migration, daily switch/edit operations are no longer archived; prepare your own backup solution if long-term auditing is needed
|
||||
|
||||
## Architecture Overview (v3.6)
|
||||
|
||||
### Architecture Refactoring Highlights (v3.6)
|
||||
|
||||
**Backend Refactoring (Rust)**: Completed 5-phase refactoring
|
||||
|
||||
- **Phase 1**: Unified error handling (`AppError` + i18n error messages)
|
||||
- **Phase 2**: Command layer split by domain (`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
|
||||
- **Phase 3**: Introduced integration tests and transaction mechanism (config snapshot + failure rollback)
|
||||
- **Phase 4**: Extracted Service layer (`services/{provider,mcp,config,speedtest}.rs`)
|
||||
- **Phase 5**: Concurrency optimization (`RwLock` instead of `Mutex`, scoped guard to avoid deadlock)
|
||||
|
||||
**Frontend Refactoring (React + TypeScript)**: Completed 4-stage refactoring
|
||||
|
||||
- **Stage 1**: Established test infrastructure (vitest + MSW + @testing-library/react)
|
||||
- **Stage 2**: Extracted custom hooks (`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport`, etc.)
|
||||
- **Stage 3**: Component splitting and business logic extraction
|
||||
- **Stage 4**: Code cleanup and formatting unification
|
||||
|
||||
**Test Coverage**:
|
||||
|
||||
- 100% Hooks unit test coverage
|
||||
- Integration tests covering critical flows (App, SettingsDialog, MCP Panel)
|
||||
- MSW mocking backend API to ensure test independence
|
||||
|
||||
### Layered Architecture
|
||||
|
||||
- **Frontend (Renderer)**
|
||||
- Tech Stack: TypeScript + React 18 + Vite + TailwindCSS 4
|
||||
- Data Layer: TanStack React Query unified queries and mutations (`@/lib/query`), Tauri API unified wrapper (`@/lib/api`)
|
||||
- Business Logic Layer: Custom Hooks (`@/hooks`) carry domain logic, components stay simple
|
||||
- Event Flow: Listen to backend `provider-switched` events, drive UI refresh and tray state consistency
|
||||
- Organization: Components split by domain (`providers/settings/mcp/ui`)
|
||||
|
||||
- **Backend (Tauri + Rust)**
|
||||
- **Commands Layer** (Interface Layer): `src-tauri/src/commands/*` split by domain, only responsible for parameter parsing and permission validation
|
||||
- **Services Layer** (Business Layer): `src-tauri/src/services/*` carry core logic, reusable and testable
|
||||
- `ProviderService`: Provider CRUD, switch, backfill, sorting
|
||||
- `McpService`: MCP server management, import/export, sync
|
||||
- `ConfigService`: Config file import/export, backup/restore
|
||||
- `SpeedtestService`: API endpoint latency testing
|
||||
- **Models & State**:
|
||||
- `provider.rs`: Domain models (`Provider`, `ProviderManager`, `ProviderMeta`)
|
||||
- `app_config.rs`: Multi-app config (`MultiAppConfig`, `AppId`, `McpRoot`)
|
||||
- `store.rs`: Global state (`AppState` + `RwLock<MultiAppConfig>`)
|
||||
- **Reliability**:
|
||||
- Unified error type `AppError` (with localized messages)
|
||||
- Transactional changes (config snapshot + failure rollback)
|
||||
- Atomic writes (temp file + rename, avoid half-writes)
|
||||
- Tray menu & events: Rebuild menu after switch and emit `provider-switched` event to frontend
|
||||
|
||||
- **Design Points (SSOT + Dual-way Sync)**
|
||||
- **Single Source of Truth**: Provider configs centrally stored in `~/.cc-switch/config.json`
|
||||
- **Write on Switch**: Write target provider config to live files (Claude: `settings.json`; Codex: `auth.json` + `config.toml`)
|
||||
- **Backfill Mechanism**: Immediately read back live files after switch, update SSOT to protect user manual modifications
|
||||
- **Directory Switch Sync**: Auto-sync current provider to new directory when changing config directories (perfect WSL environment support)
|
||||
- **Prioritize Live When Editing**: When editing current provider, prioritize loading live config to ensure display of actually effective configuration
|
||||
|
||||
- **Compatibility & Changes**
|
||||
- Command Parameters Unified: Tauri commands only accept `app` (values: `claude` / `codex`)
|
||||
- Frontend Types Unified: Use `AppId` to express app identifiers (replacing legacy `AppType` export)
|
||||
|
||||
## Development
|
||||
|
||||
### Environment Requirements
|
||||
|
||||
- Node.js 18+
|
||||
- pnpm 8+
|
||||
- Rust 1.75+
|
||||
- Tauri CLI 2.0+
|
||||
- Rust 1.85+
|
||||
- Tauri CLI 2.8+
|
||||
|
||||
### 开发命令
|
||||
### Development Commands
|
||||
|
||||
```bash
|
||||
# 安装依赖
|
||||
# Install dependencies
|
||||
pnpm install
|
||||
|
||||
# 开发模式(热重载)
|
||||
# Dev mode (hot reload)
|
||||
pnpm dev
|
||||
|
||||
# 类型检查
|
||||
# Type check
|
||||
pnpm typecheck
|
||||
|
||||
# 代码格式化
|
||||
# Format code
|
||||
pnpm format
|
||||
|
||||
# 检查代码格式
|
||||
# Check code format
|
||||
pnpm format:check
|
||||
|
||||
# 构建应用
|
||||
# Run frontend unit tests
|
||||
pnpm test:unit
|
||||
|
||||
# Run tests in watch mode (recommended for development)
|
||||
pnpm test:unit:watch
|
||||
|
||||
# Build application
|
||||
pnpm build
|
||||
|
||||
# 构建调试版本
|
||||
# Build debug version
|
||||
pnpm tauri build --debug
|
||||
```
|
||||
|
||||
### Rust 后端开发
|
||||
### Rust Backend Development
|
||||
|
||||
```bash
|
||||
cd src-tauri
|
||||
|
||||
# 格式化 Rust 代码
|
||||
# Format Rust code
|
||||
cargo fmt
|
||||
|
||||
# 运行 clippy 检查
|
||||
# Run clippy checks
|
||||
cargo clippy
|
||||
|
||||
# 运行测试
|
||||
# Run backend tests
|
||||
cargo test
|
||||
|
||||
# Run specific tests
|
||||
cargo test test_name
|
||||
|
||||
# Run tests with test-hooks feature
|
||||
cargo test --features test-hooks
|
||||
```
|
||||
|
||||
## 技术栈
|
||||
### Testing Guide (v3.6 New)
|
||||
|
||||
- **[Tauri 2.0](https://tauri.app/)** - 跨平台桌面应用框架
|
||||
- **[React 18](https://react.dev/)** - 用户界面库
|
||||
- **[TypeScript](https://www.typescriptlang.org/)** - 类型安全的 JavaScript
|
||||
- **[Vite](https://vitejs.dev/)** - 极速的前端构建工具
|
||||
- **[Rust](https://www.rust-lang.org/)** - 系统级编程语言(后端)
|
||||
**Frontend Testing**:
|
||||
|
||||
## 项目结构
|
||||
- Uses **vitest** as test framework
|
||||
- Uses **MSW (Mock Service Worker)** to mock Tauri API calls
|
||||
- Uses **@testing-library/react** for component testing
|
||||
|
||||
```
|
||||
├── src/ # 前端代码 (React + TypeScript)
|
||||
│ ├── components/ # React 组件
|
||||
│ ├── config/ # 预设供应商配置
|
||||
│ ├── lib/ # Tauri API 封装
|
||||
│ └── utils/ # 工具函数
|
||||
├── src-tauri/ # 后端代码 (Rust)
|
||||
│ ├── src/ # Rust 源代码
|
||||
│ │ ├── commands.rs # Tauri 命令定义
|
||||
│ │ ├── config.rs # 配置文件管理
|
||||
│ │ ├── provider.rs # 供应商管理逻辑
|
||||
│ │ └── store.rs # 状态管理
|
||||
│ ├── capabilities/ # 权限配置
|
||||
│ └── icons/ # 应用图标资源
|
||||
└── screenshots/ # 界面截图
|
||||
**Test Coverage**:
|
||||
|
||||
- ✅ Hooks unit tests (100% coverage)
|
||||
- `useProviderActions` - Provider operations
|
||||
- `useMcpActions` - MCP management
|
||||
- `useSettings` series - Settings management
|
||||
- `useImportExport` - Import/export
|
||||
- ✅ Integration tests
|
||||
- App main application flow
|
||||
- SettingsDialog complete interaction
|
||||
- MCP panel functionality
|
||||
|
||||
**Running Tests**:
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
pnpm test:unit
|
||||
|
||||
# Watch mode (auto re-run)
|
||||
pnpm test:unit:watch
|
||||
|
||||
# With coverage report
|
||||
pnpm test:unit --coverage
|
||||
```
|
||||
|
||||
## 更新日志
|
||||
## Tech Stack
|
||||
|
||||
查看 [CHANGELOG.md](CHANGELOG.md) 了解版本更新详情。
|
||||
### Frontend
|
||||
|
||||
## 贡献
|
||||
- **[React 18](https://react.dev/)** - User interface library
|
||||
- **[TypeScript](https://www.typescriptlang.org/)** - Type-safe JavaScript
|
||||
- **[Vite](https://vitejs.dev/)** - Lightning fast frontend build tool
|
||||
- **[TailwindCSS 4](https://tailwindcss.com/)** - Utility-first CSS framework
|
||||
- **[TanStack Query v5](https://tanstack.com/query/latest)** - Powerful data fetching and caching
|
||||
- **[react-i18next](https://react.i18next.com/)** - React internationalization framework
|
||||
- **[react-hook-form](https://react-hook-form.com/)** - High-performance forms library
|
||||
- **[zod](https://zod.dev/)** - TypeScript-first schema validation
|
||||
- **[shadcn/ui](https://ui.shadcn.com/)** - Reusable React components
|
||||
- **[@dnd-kit](https://dndkit.com/)** - Modern drag and drop toolkit
|
||||
|
||||
欢迎提交 Issue 和 Pull Request!
|
||||
### Backend
|
||||
|
||||
- **[Tauri 2.8](https://tauri.app/)** - Cross-platform desktop app framework
|
||||
- tauri-plugin-updater - Auto update
|
||||
- tauri-plugin-process - Process management
|
||||
- tauri-plugin-dialog - File dialogs
|
||||
- tauri-plugin-store - Persistent storage
|
||||
- tauri-plugin-log - Logging
|
||||
- **[Rust](https://www.rust-lang.org/)** - Systems programming language
|
||||
- **[serde](https://serde.rs/)** - Serialization/deserialization framework
|
||||
- **[tokio](https://tokio.rs/)** - Async runtime
|
||||
- **[thiserror](https://github.com/dtolnay/thiserror)** - Error handling derive macro
|
||||
|
||||
### Testing Tools
|
||||
|
||||
- **[vitest](https://vitest.dev/)** - Fast unit testing framework
|
||||
- **[MSW](https://mswjs.io/)** - API mocking tool
|
||||
- **[@testing-library/react](https://testing-library.com/react)** - React testing utilities
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
├── src/ # Frontend code (React + TypeScript)
|
||||
│ ├── components/ # React components
|
||||
│ │ ├── providers/ # Provider management components
|
||||
│ │ │ ├── forms/ # Form sub-components (Claude/Codex fields)
|
||||
│ │ │ ├── ProviderList.tsx
|
||||
│ │ │ ├── ProviderForm.tsx
|
||||
│ │ │ ├── AddProviderDialog.tsx
|
||||
│ │ │ └── EditProviderDialog.tsx
|
||||
│ │ ├── settings/ # Settings related components
|
||||
│ │ │ ├── SettingsDialog.tsx
|
||||
│ │ │ ├── DirectorySettings.tsx
|
||||
│ │ │ └── ImportExportSection.tsx
|
||||
│ │ ├── mcp/ # MCP management components
|
||||
│ │ │ ├── McpPanel.tsx
|
||||
│ │ │ ├── McpFormModal.tsx
|
||||
│ │ │ └── McpWizard.tsx
|
||||
│ │ └── ui/ # shadcn/ui base components
|
||||
│ ├── hooks/ # Custom Hooks (business logic layer)
|
||||
│ │ ├── useProviderActions.ts # Provider operations
|
||||
│ │ ├── useMcpActions.ts # MCP operations
|
||||
│ │ ├── useSettings.ts # Settings management
|
||||
│ │ ├── useImportExport.ts # Import/export
|
||||
│ │ └── useDirectorySettings.ts # Directory config
|
||||
│ ├── lib/
|
||||
│ │ ├── api/ # Tauri API wrapper (type-safe)
|
||||
│ │ │ ├── providers.ts # Provider API
|
||||
│ │ │ ├── settings.ts # Settings API
|
||||
│ │ │ ├── mcp.ts # MCP API
|
||||
│ │ │ └── usage.ts # Usage query API
|
||||
│ │ └── query/ # TanStack Query config
|
||||
│ │ ├── queries.ts # Query definitions
|
||||
│ │ ├── mutations.ts # Mutation definitions
|
||||
│ │ └── queryClient.ts
|
||||
│ ├── i18n/ # Internationalization resources
|
||||
│ │ └── locales/
|
||||
│ │ ├── zh/ # Chinese translations
|
||||
│ │ └── en/ # English translations
|
||||
│ ├── config/ # Config & presets
|
||||
│ │ ├── claudeProviderPresets.ts # Claude provider presets
|
||||
│ │ ├── codexProviderPresets.ts # Codex provider presets
|
||||
│ │ └── mcpPresets.ts # MCP server templates
|
||||
│ ├── utils/ # Utility functions
|
||||
│ │ ├── postChangeSync.ts # Config sync utility
|
||||
│ │ └── ...
|
||||
│ └── types/ # TypeScript type definitions
|
||||
├── src-tauri/ # Backend code (Rust)
|
||||
│ ├── src/
|
||||
│ │ ├── commands/ # Tauri command layer (split by domain)
|
||||
│ │ │ ├── provider.rs # Provider commands
|
||||
│ │ │ ├── mcp.rs # MCP commands
|
||||
│ │ │ ├── config.rs # Config query commands
|
||||
│ │ │ ├── settings.rs # Settings commands
|
||||
│ │ │ ├── plugin.rs # Plugin commands
|
||||
│ │ │ ├── import_export.rs # Import/export commands
|
||||
│ │ │ └── misc.rs # Misc commands
|
||||
│ │ ├── services/ # Service layer (business logic)
|
||||
│ │ │ ├── provider.rs # ProviderService
|
||||
│ │ │ ├── mcp.rs # McpService
|
||||
│ │ │ ├── config.rs # ConfigService
|
||||
│ │ │ └── speedtest.rs # SpeedtestService
|
||||
│ │ ├── app_config.rs # Config data models
|
||||
│ │ ├── provider.rs # Provider domain models
|
||||
│ │ ├── store.rs # Global state management
|
||||
│ │ ├── mcp.rs # MCP sync & validation
|
||||
│ │ ├── error.rs # Unified error type
|
||||
│ │ ├── usage_script.rs # Usage script execution
|
||||
│ │ ├── claude_plugin.rs # Claude plugin management
|
||||
│ │ └── lib.rs # App entry point
|
||||
│ ├── capabilities/ # Tauri permission config
|
||||
│ └── icons/ # App icons
|
||||
├── tests/ # Frontend tests (v3.6 new)
|
||||
│ ├── hooks/ # Hooks unit tests
|
||||
│ ├── components/ # Component integration tests
|
||||
│ └── setup.ts # Test config
|
||||
└── assets/ # Static resources
|
||||
├── screenshots/ # Interface screenshots
|
||||
└── partners/ # Partner resources
|
||||
├── logos/ # Partner logos
|
||||
└── banners/ # Partner banners/promotional images
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
||||
See [CHANGELOG.md](CHANGELOG.md) for version update details.
|
||||
|
||||
## Legacy Electron Version
|
||||
|
||||
[Releases](../../releases) retains v2.0.3 legacy Electron version
|
||||
|
||||
If you need legacy Electron code, you can pull the electron-legacy branch
|
||||
|
||||
## Contributing
|
||||
|
||||
Issues and suggestions are welcome!
|
||||
|
||||
Before submitting PRs, please ensure:
|
||||
|
||||
- Pass type check: `pnpm typecheck`
|
||||
- Pass format check: `pnpm format:check`
|
||||
- Pass unit tests: `pnpm test:unit`
|
||||
- Functional PRs should be discussed in the issue area first
|
||||
|
||||
## Star History
|
||||
|
||||
[](https://www.star-history.com/#farion1231/cc-switch&Date)
|
||||
|
||||
## License
|
||||
|
||||
|
||||
517
README_ZH.md
Normal file
@@ -0,0 +1,517 @@
|
||||
# Claude Code & Codex 供应商管理器
|
||||
|
||||
<div align="center">
|
||||
|
||||
[](https://github.com/farion1231/cc-switch/releases)
|
||||
[](https://github.com/farion1231/cc-switch/releases)
|
||||
[](https://tauri.app/)
|
||||
|
||||
[English](README.md) | 中文 | [更新日志](CHANGELOG.md)
|
||||
|
||||
一个用于管理和切换 Claude Code 与 Codex 不同供应商配置、MCP的桌面应用。
|
||||
|
||||
</div>
|
||||
|
||||
## ❤️ 赞助商
|
||||
|
||||

|
||||
|
||||
感谢智谱AI的 GLM CODING PLAN 赞助了本项目!
|
||||
|
||||
GLM CODING PLAN 是专为AI编码打造的订阅套餐,每月最低仅需20元,即可在十余款主流AI编码工具如 Claude Code、Cline 中畅享智谱旗舰模型 GLM-4.6,为开发者提供顶尖、高速、稳定的编码体验。
|
||||
|
||||
CC Switch 已经预设了智谱GLM,只需要填写 key 即可一键导入编程工具。智谱AI为本软件的用户提供了特别优惠,使用[此链接](https://www.bigmodel.cn/claude-code?ic=RRVJPB5SII)购买可以享受九折优惠。
|
||||
|
||||
## 更新记录
|
||||
|
||||
> **v3.6.0** :新增编辑模式(供应商复制、手动排序)、自定义端点管理、使用量查询等功能,优化配置目录切换体验(WSL 环境完美支持),新增多个供应商预设(DMXAPI、Azure Codex、AnyRouter、AiHubMix、MiniMax),完成全栈架构重构和测试体系建设。
|
||||
|
||||
> v3.5.0 :新增 MCP 管理、配置导入/导出、端点速度测试功能,完善国际化覆盖,新增 Longcat、kat-coder 预设,标准化发布文件命名规范。
|
||||
|
||||
> v3.4.0 :新增 i18next 国际化、对新模型(qwen-3-max, GLM-4.6, DeepSeek-V3.2-Exp)的支持、Claude 插件、单实例守护、托盘最小化及安装器优化等。
|
||||
|
||||
> v3.3.0 :VS Code Codex 插件一键配置/移除(默认自动同步)、Codex 通用配置片段与自定义向导增强、WSL 环境支持、跨平台托盘与 UI 优化。(该 VS Code 写入功能已在 v3.4.x 停用)
|
||||
|
||||
> v3.2.0 :全新 UI、macOS系统托盘、内置更新器、原子写入与回滚、改进暗色样式、单一事实源(SSOT)与一次性迁移/归档。
|
||||
|
||||
> v3.1.0 :新增 Codex 供应商管理与一键切换,支持导入当前 Codex 配置为默认供应商,并在内部配置从 v1 → v2 迁移前自动备份(详见下文“迁移与归档”)。
|
||||
|
||||
> v3.0.0 重大更新:从 Electron 完全迁移到 Tauri 2.0,应用体积显著降低、启动性能大幅提升。
|
||||
|
||||
## 功能特性(v3.6.0)
|
||||
|
||||
### 核心功能
|
||||
|
||||
- **MCP (Model Context Protocol) 管理**:完整的 MCP 服务器配置管理系统
|
||||
- 支持 stdio 和 http 服务器类型,并提供命令校验
|
||||
- 内置常用 MCP 服务器模板(如 mcp-fetch 等)
|
||||
- 实时启用/禁用 MCP 服务器,原子文件写入防止配置损坏
|
||||
- **配置导入/导出**:备份和恢复你的供应商配置
|
||||
- 一键导出所有配置到 JSON 文件
|
||||
- 导入配置时自动验证并备份,自动轮换备份(保留最近 10 个)
|
||||
- 导入后自动同步到 live 配置文件,确保立即生效
|
||||
- **端点速度测试**:测试 API 端点响应时间
|
||||
- 测量不同供应商端点的延迟,可视化连接质量指示器
|
||||
- 帮助用户选择最快的供应商
|
||||
- **国际化与语言切换**:完整的 i18next 国际化覆盖(包含错误消息、托盘菜单、所有 UI 组件)
|
||||
- **Claude 插件同步**:内置按钮可一键应用或恢复 Claude 插件配置,切换供应商后立即生效。
|
||||
|
||||
### v3.6 新增功能
|
||||
|
||||
- **供应商复制功能**:快速复制现有供应商配置,轻松创建变体配置
|
||||
- **手动排序功能**:通过拖拽来对供应商进行手动排序
|
||||
- **自定义端点管理**:支持聚合类供应商的多端点配置
|
||||
- **使用量查询功能**
|
||||
- 自动刷新间隔:支持定时自动查询使用量
|
||||
- 测试脚本 API:测试 JavaScript 脚本是否正确
|
||||
- 模板系统扩展:自定义空白模板、支持 access token 和 user ID 参数
|
||||
- **配置编辑器改进**
|
||||
- 新增 JSON 格式化按钮
|
||||
- 实时 TOML 语法验证(Codex 配置)
|
||||
- **配置目录切换自动同步**:切换 Claude/Codex 配置目录(如切换到 WSL 环境)时,自动同步当前供应商到新目录,避免冲突导致配置文件混乱
|
||||
- **编辑当前供应商时加载 live 配置**:编辑正在使用的供应商时,优先显示实际生效的配置,保护用户手动修改
|
||||
- **新增供应商预设**:DMXAPI、Azure Codex、AnyRouter、AiHubMix、MiniMax
|
||||
- **合作伙伴推广机制**:支持生态合作伙伴推广(如智谱 GLM Z.ai)
|
||||
|
||||
### v3.6 架构改进
|
||||
|
||||
- **后端重构**:完成 5 阶段重构(统一错误处理 → 命令层拆分 → 集成测试 → Service 层提取 → 并发优化)
|
||||
- **前端重构**:完成 4 阶段重构(测试基础设施 → Hooks 提取 → 组件拆分 → 代码清理)
|
||||
- **测试体系**:Hooks 单元测试 100% 覆盖,集成测试覆盖关键流程(vitest + MSW + @testing-library/react)
|
||||
|
||||
### 系统功能
|
||||
|
||||
- **系统托盘与窗口行为**:窗口关闭可最小化到托盘,macOS 支持托盘模式下隐藏/显示 Dock,托盘切换时同步 Claude/Codex/插件状态。
|
||||
- **单实例**:保证同一时间仅运行一个实例,避免多开冲突。
|
||||
- **标准化发布命名**:所有平台发布文件使用一致的版本标签命名(macOS: `.tar.gz` / `.zip`,Windows: `.msi` / `-Portable.zip`,Linux: `.AppImage` / `.deb`)。
|
||||
|
||||
## 界面预览
|
||||
|
||||
### 主界面
|
||||
|
||||

|
||||
|
||||
### 添加供应商
|
||||
|
||||

|
||||
|
||||
## 下载安装
|
||||
|
||||
### 系统要求
|
||||
|
||||
- **Windows**: Windows 10 及以上
|
||||
- **macOS**: macOS 10.15 (Catalina) 及以上
|
||||
- **Linux**: Ubuntu 22.04+ / Debian 11+ / Fedora 34+ 等主流发行版
|
||||
|
||||
### Windows 用户
|
||||
|
||||
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Windows.msi` 安装包或者 `CC-Switch-v{版本号}-Windows-Portable.zip` 绿色版。
|
||||
|
||||
### macOS 用户
|
||||
|
||||
**方式一:通过 Homebrew 安装(推荐)**
|
||||
|
||||
```bash
|
||||
brew tap farion1231/ccswitch
|
||||
brew install --cask cc-switch
|
||||
```
|
||||
|
||||
更新:
|
||||
|
||||
```bash
|
||||
brew upgrade --cask cc-switch
|
||||
```
|
||||
|
||||
**方式二:手动下载**
|
||||
|
||||
从 [Releases](../../releases) 页面下载 `CC-Switch-v{版本号}-macOS.zip` 解压使用。
|
||||
|
||||
> **注意**:由于作者没有苹果开发者账号,首次打开可能出现"未知开发者"警告,请先关闭,然后前往"系统设置" → "隐私与安全性" → 点击"仍要打开",之后便可以正常打开
|
||||
|
||||
### Linux 用户
|
||||
|
||||
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Linux.deb` 包或者 `CC-Switch-v{版本号}-Linux.AppImage` 安装包。
|
||||
|
||||
## 使用说明
|
||||
|
||||
1. 点击"添加供应商"添加你的 API 配置
|
||||
2. 切换方式:
|
||||
- 在主界面选择供应商后点击切换
|
||||
- 或通过“系统托盘(菜单栏)”直接选择目标供应商,立即生效
|
||||
3. 切换会写入对应应用的“live 配置文件”(Claude:`settings.json`;Codex:`auth.json` + `config.toml`)
|
||||
4. 重启或新开终端以确保生效
|
||||
5. 若需切回官方登录,在预设中选择“官方登录”并切换即可;重启终端后按官方流程登录
|
||||
|
||||
### MCP 配置说明(v3.5.x)
|
||||
|
||||
- 管理位置:所有 MCP 服务器定义集中保存在 `~/.cc-switch/config.json`(按客户端 `claude` / `codex` 分类)
|
||||
- 同步机制:
|
||||
- 启用的 Claude MCP 会投影到 `~/.claude.json`(路径可随覆盖目录而变化)
|
||||
- 启用的 Codex MCP 会投影到 `~/.codex/config.toml`
|
||||
- 校验与归一化:新增/导入时自动校验字段合法性(stdio/http),并自动修复/填充 `id` 等键名
|
||||
- 导入来源:支持从 `~/.claude.json` 与 `~/.codex/config.toml` 导入;已存在条目只强制 `enabled=true`,不覆盖其他字段
|
||||
|
||||
### 检查更新
|
||||
|
||||
- 在“设置”中点击“检查更新”,若内置 Updater 配置可用将直接检测与下载;否则会回退打开 Releases 页面
|
||||
|
||||
### Codex 说明(SSOT)
|
||||
|
||||
- 配置目录:`~/.codex/`
|
||||
- live 主配置:`auth.json`(必需)、`config.toml`(可为空)
|
||||
- API Key 字段:`auth.json` 中使用 `OPENAI_API_KEY`
|
||||
- 切换行为(不再写“副本文件”):
|
||||
- 供应商配置统一保存在 `~/.cc-switch/config.json`
|
||||
- 切换时将目标供应商写回 live 文件(`auth.json` + `config.toml`)
|
||||
- 采用“原子写入 + 失败回滚”,避免半写状态;`config.toml` 可为空
|
||||
- 导入默认:当该应用无任何供应商时,从现有 live 主配置创建一条默认项并设为当前
|
||||
- 官方登录:可切换到预设“Codex 官方登录”,重启终端后按官方流程登录
|
||||
|
||||
### Claude Code 说明(SSOT)
|
||||
|
||||
- 配置目录:`~/.claude/`
|
||||
- live 主配置:`settings.json`(优先)或历史兼容 `claude.json`
|
||||
- API Key 字段:`env.ANTHROPIC_AUTH_TOKEN`
|
||||
- 切换行为(不再写“副本文件”):
|
||||
- 供应商配置统一保存在 `~/.cc-switch/config.json`
|
||||
- 切换时将目标供应商 JSON 直接写入 live 文件(优先 `settings.json`)
|
||||
- 编辑当前供应商时,先写 live 成功,再更新应用主配置,保证一致性
|
||||
- 导入默认:当该应用无任何供应商时,从现有 live 主配置创建一条默认项并设为当前
|
||||
- 官方登录:可切换到预设“Claude 官方登录”,重启终端后可使用 `/login` 完成登录
|
||||
|
||||
### 迁移与归档
|
||||
|
||||
#### v3.6 技术改进
|
||||
|
||||
**内部优化(用户无感知)**:
|
||||
|
||||
- **移除遗留迁移逻辑**:v3.6 移除了 v1 配置自动迁移和副本文件扫描逻辑
|
||||
- ✅ **影响**:启动性能提升,代码更简洁
|
||||
- ✅ **兼容性**:v2 格式配置完全兼容,无需任何操作
|
||||
- ⚠️ **注意**:从 v3.1.0 或更早版本升级的用户,请先升级到 v3.2.x 或 v3.5.x 完成一次性迁移,再升级到 v3.6
|
||||
|
||||
- **命令参数标准化**:后端统一使用 `app` 参数(取值:`claude` 或 `codex`)
|
||||
- ✅ **影响**:代码更规范,错误提示更友好
|
||||
- ✅ **兼容性**:前端已完全适配,用户无需关心此变更
|
||||
|
||||
#### 启动失败与恢复
|
||||
|
||||
- 触发条件:`~/.cc-switch/config.json` 不存在、损坏或解析失败时触发。
|
||||
- 用户动作:根据弹窗提示检查 JSON 语法,或从备份文件恢复。
|
||||
- 备份位置与轮换:`~/.cc-switch/backups/backup_YYYYMMDD_HHMMSS.json`(最多保留 10 个,参见 `src-tauri/src/services/config.rs`)。
|
||||
- 退出策略:为保护数据安全,出现上述错误时应用会弹窗提示并强制退出;修复后重新启动即可。
|
||||
|
||||
#### v3.2.0 起的迁移机制
|
||||
|
||||
- 一次性迁移:首次启动 3.2.0 及以上版本会扫描旧的"副本文件"并合并到 `~/.cc-switch/config.json`
|
||||
- Claude:`~/.claude/settings-*.json`(排除 `settings.json` / 历史 `claude.json`)
|
||||
- Codex:`~/.codex/auth-*.json` 与 `config-*.toml`(按名称成对合并)
|
||||
- 去重与当前项:按"名称(忽略大小写)+ API Key"去重;若当前为空,将 live 合并项设为当前
|
||||
- 归档与清理:
|
||||
- 归档目录:`~/.cc-switch/archive/<timestamp>/<category>/...`
|
||||
- 归档成功后删除原副本;失败则保留原文件(保守策略)
|
||||
- v1 → v2 结构升级:会额外生成 `~/.cc-switch/config.v1.backup.<timestamp>.json` 以便回滚
|
||||
- 注意:迁移后不再持续归档日常切换/编辑操作,如需长期审计请自备备份方案
|
||||
|
||||
## 架构总览(v3.6)
|
||||
|
||||
### 架构重构亮点(v3.6)
|
||||
|
||||
**后端重构(Rust)**:完成 5 阶段重构
|
||||
|
||||
- **Phase 1**:统一错误处理(`AppError` + 国际化错误消息)
|
||||
- **Phase 2**:命令层按领域拆分(`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
|
||||
- **Phase 3**:引入集成测试和事务机制(配置快照 + 失败回滚)
|
||||
- **Phase 4**:提取 Service 层(`services/{provider,mcp,config,speedtest}.rs`)
|
||||
- **Phase 5**:并发优化(`RwLock` 替代 `Mutex`,作用域 guard 避免死锁)
|
||||
|
||||
**前端重构(React + TypeScript)**:完成 4 阶段重构
|
||||
|
||||
- **Stage 1**:建立测试基础设施(vitest + MSW + @testing-library/react)
|
||||
- **Stage 2**:提取自定义 hooks(`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport` 等)
|
||||
- **Stage 3**:组件拆分和业务逻辑提取
|
||||
- **Stage 4**:代码清理和格式化统一
|
||||
|
||||
**测试覆盖**:
|
||||
|
||||
- Hooks 单元测试 100% 覆盖
|
||||
- 集成测试覆盖关键流程(App、SettingsDialog、MCP 面板)
|
||||
- MSW 模拟后端 API,确保测试独立性
|
||||
|
||||
### 分层架构
|
||||
|
||||
- **前端(Renderer)**
|
||||
- 技术栈:TypeScript + React 18 + Vite + TailwindCSS 4
|
||||
- 数据层:TanStack React Query 统一查询与变更(`@/lib/query`),Tauri API 统一封装(`@/lib/api`)
|
||||
- 业务逻辑层:自定义 Hooks(`@/hooks`)承载领域逻辑,组件保持简洁
|
||||
- 事件流:监听后端 `provider-switched` 事件,驱动 UI 刷新与托盘状态一致
|
||||
- 组织结构:按领域拆分组件(`providers/settings/mcp/ui`)
|
||||
|
||||
- **后端(Tauri + Rust)**
|
||||
- **Commands 层**(接口层):`src-tauri/src/commands/*` 按领域拆分,仅负责参数解析和权限校验
|
||||
- **Services 层**(业务层):`src-tauri/src/services/*` 承载核心逻辑,可复用和测试
|
||||
- `ProviderService`:供应商增删改查、切换、回填、排序
|
||||
- `McpService`:MCP 服务器管理、导入导出、同步
|
||||
- `ConfigService`:配置文件导入导出、备份恢复
|
||||
- `SpeedtestService`:API 端点延迟测试
|
||||
- **模型与状态**:
|
||||
- `provider.rs`:领域模型(`Provider`, `ProviderManager`, `ProviderMeta`)
|
||||
- `app_config.rs`:多应用配置(`MultiAppConfig`, `AppId`, `McpRoot`)
|
||||
- `store.rs`:全局状态(`AppState` + `RwLock<MultiAppConfig>`)
|
||||
- **可靠性**:
|
||||
- 统一错误类型 `AppError`(包含本地化消息)
|
||||
- 事务式变更(配置快照 + 失败回滚)
|
||||
- 原子写入(临时文件 + 重命名,避免半写入)
|
||||
- 托盘菜单与事件:切换后重建菜单并向前端发射 `provider-switched` 事件
|
||||
|
||||
- **设计要点(SSOT + 双向同步)**
|
||||
- **单一事实源**:供应商配置集中存放于 `~/.cc-switch/config.json`
|
||||
- **切换时写入**:将目标供应商配置写入 live 文件(Claude: `settings.json`;Codex: `auth.json` + `config.toml`)
|
||||
- **回填机制**:切换后立即读回 live 文件,更新 SSOT,保护用户手动修改
|
||||
- **目录切换同步**:修改配置目录时自动同步当前供应商到新目录(WSL 环境完美支持)
|
||||
- **编辑时优先 live**:编辑当前供应商时,优先加载 live 配置,确保显示实际生效的配置
|
||||
|
||||
- **兼容性与变更**
|
||||
- 命令参数统一:Tauri 命令仅接受 `app`(值为 `claude` / `codex`)
|
||||
- 前端类型统一:使用 `AppId` 表达应用标识(替代历史 `AppType` 导出)
|
||||
|
||||
## 开发
|
||||
|
||||
### 环境要求
|
||||
|
||||
- Node.js 18+
|
||||
- pnpm 8+
|
||||
- Rust 1.85+
|
||||
- Tauri CLI 2.8+
|
||||
|
||||
### 开发命令
|
||||
|
||||
```bash
|
||||
# 安装依赖
|
||||
pnpm install
|
||||
|
||||
# 开发模式(热重载)
|
||||
pnpm dev
|
||||
|
||||
# 类型检查
|
||||
pnpm typecheck
|
||||
|
||||
# 代码格式化
|
||||
pnpm format
|
||||
|
||||
# 检查代码格式
|
||||
pnpm format:check
|
||||
|
||||
# 运行前端单元测试
|
||||
pnpm test:unit
|
||||
|
||||
# 监听模式运行测试(推荐开发时使用)
|
||||
pnpm test:unit:watch
|
||||
|
||||
# 构建应用
|
||||
pnpm build
|
||||
|
||||
# 构建调试版本
|
||||
pnpm tauri build --debug
|
||||
```
|
||||
|
||||
### Rust 后端开发
|
||||
|
||||
```bash
|
||||
cd src-tauri
|
||||
|
||||
# 格式化 Rust 代码
|
||||
cargo fmt
|
||||
|
||||
# 运行 clippy 检查
|
||||
cargo clippy
|
||||
|
||||
# 运行后端测试
|
||||
cargo test
|
||||
|
||||
# 运行特定测试
|
||||
cargo test test_name
|
||||
|
||||
# 运行带测试 hooks 的测试
|
||||
cargo test --features test-hooks
|
||||
```
|
||||
|
||||
### 测试说明(v3.6 新增)
|
||||
|
||||
**前端测试**:
|
||||
|
||||
- 使用 **vitest** 作为测试框架
|
||||
- 使用 **MSW (Mock Service Worker)** 模拟 Tauri API 调用
|
||||
- 使用 **@testing-library/react** 进行组件测试
|
||||
|
||||
**测试覆盖**:
|
||||
|
||||
- ✅ Hooks 单元测试(100% 覆盖)
|
||||
- `useProviderActions` - 供应商操作
|
||||
- `useMcpActions` - MCP 管理
|
||||
- `useSettings` 系列 - 设置管理
|
||||
- `useImportExport` - 导入导出
|
||||
- ✅ 集成测试
|
||||
- App 主应用流程
|
||||
- SettingsDialog 完整交互
|
||||
- MCP 面板功能
|
||||
|
||||
**运行测试**:
|
||||
|
||||
```bash
|
||||
# 运行所有测试
|
||||
pnpm test:unit
|
||||
|
||||
# 监听模式(自动重跑)
|
||||
pnpm test:unit:watch
|
||||
|
||||
# 带覆盖率报告
|
||||
pnpm test:unit --coverage
|
||||
```
|
||||
|
||||
## 技术栈
|
||||
|
||||
### 前端
|
||||
|
||||
- **[React 18](https://react.dev/)** - 用户界面库
|
||||
- **[TypeScript](https://www.typescriptlang.org/)** - 类型安全的 JavaScript
|
||||
- **[Vite](https://vitejs.dev/)** - 极速的前端构建工具
|
||||
- **[TailwindCSS 4](https://tailwindcss.com/)** - 实用优先的 CSS 框架
|
||||
- **[TanStack Query v5](https://tanstack.com/query/latest)** - 强大的数据获取与缓存
|
||||
- **[react-i18next](https://react.i18next.com/)** - React 国际化框架
|
||||
- **[react-hook-form](https://react-hook-form.com/)** - 高性能表单库
|
||||
- **[zod](https://zod.dev/)** - TypeScript 优先的模式验证
|
||||
- **[shadcn/ui](https://ui.shadcn.com/)** - 可复用的 React 组件
|
||||
- **[@dnd-kit](https://dndkit.com/)** - 现代拖拽工具包
|
||||
|
||||
### 后端
|
||||
|
||||
- **[Tauri 2.8](https://tauri.app/)** - 跨平台桌面应用框架
|
||||
- tauri-plugin-updater - 自动更新
|
||||
- tauri-plugin-process - 进程管理
|
||||
- tauri-plugin-dialog - 文件对话框
|
||||
- tauri-plugin-store - 持久化存储
|
||||
- tauri-plugin-log - 日志记录
|
||||
- **[Rust](https://www.rust-lang.org/)** - 系统级编程语言
|
||||
- **[serde](https://serde.rs/)** - 序列化/反序列化框架
|
||||
- **[tokio](https://tokio.rs/)** - 异步运行时
|
||||
- **[thiserror](https://github.com/dtolnay/thiserror)** - 错误处理派生宏
|
||||
|
||||
### 测试工具
|
||||
|
||||
- **[vitest](https://vitest.dev/)** - 快速的单元测试框架
|
||||
- **[MSW](https://mswjs.io/)** - API mock 工具
|
||||
- **[@testing-library/react](https://testing-library.com/react)** - React 测试工具
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
├── src/ # 前端代码 (React + TypeScript)
|
||||
│ ├── components/ # React 组件
|
||||
│ │ ├── providers/ # 供应商管理组件
|
||||
│ │ │ ├── forms/ # 表单子组件(Claude/Codex 字段)
|
||||
│ │ │ ├── ProviderList.tsx
|
||||
│ │ │ ├── ProviderForm.tsx
|
||||
│ │ │ ├── AddProviderDialog.tsx
|
||||
│ │ │ └── EditProviderDialog.tsx
|
||||
│ │ ├── settings/ # 设置相关组件
|
||||
│ │ │ ├── SettingsDialog.tsx
|
||||
│ │ │ ├── DirectorySettings.tsx
|
||||
│ │ │ └── ImportExportSection.tsx
|
||||
│ │ ├── mcp/ # MCP 管理组件
|
||||
│ │ │ ├── McpPanel.tsx
|
||||
│ │ │ ├── McpFormModal.tsx
|
||||
│ │ │ └── McpWizard.tsx
|
||||
│ │ └── ui/ # shadcn/ui 基础组件
|
||||
│ ├── hooks/ # 自定义 Hooks(业务逻辑层)
|
||||
│ │ ├── useProviderActions.ts # 供应商操作
|
||||
│ │ ├── useMcpActions.ts # MCP 操作
|
||||
│ │ ├── useSettings.ts # 设置管理
|
||||
│ │ ├── useImportExport.ts # 导入导出
|
||||
│ │ └── useDirectorySettings.ts # 目录配置
|
||||
│ ├── lib/
|
||||
│ │ ├── api/ # Tauri API 封装(类型安全)
|
||||
│ │ │ ├── providers.ts # 供应商 API
|
||||
│ │ │ ├── settings.ts # 设置 API
|
||||
│ │ │ ├── mcp.ts # MCP API
|
||||
│ │ │ └── usage.ts # 用量查询 API
|
||||
│ │ └── query/ # TanStack Query 配置
|
||||
│ │ ├── queries.ts # 查询定义
|
||||
│ │ ├── mutations.ts # 变更定义
|
||||
│ │ └── queryClient.ts
|
||||
│ ├── i18n/ # 国际化资源
|
||||
│ │ └── locales/
|
||||
│ │ ├── zh/ # 中文翻译
|
||||
│ │ └── en/ # 英文翻译
|
||||
│ ├── config/ # 配置与预设
|
||||
│ │ ├── claudeProviderPresets.ts # Claude 供应商预设
|
||||
│ │ ├── codexProviderPresets.ts # Codex 供应商预设
|
||||
│ │ └── mcpPresets.ts # MCP 服务器模板
|
||||
│ ├── utils/ # 工具函数
|
||||
│ │ ├── postChangeSync.ts # 配置同步工具
|
||||
│ │ └── ...
|
||||
│ └── types/ # TypeScript 类型定义
|
||||
├── src-tauri/ # 后端代码 (Rust)
|
||||
│ ├── src/
|
||||
│ │ ├── commands/ # Tauri 命令层(按领域拆分)
|
||||
│ │ │ ├── provider.rs # 供应商命令
|
||||
│ │ │ ├── mcp.rs # MCP 命令
|
||||
│ │ │ ├── config.rs # 配置查询命令
|
||||
│ │ │ ├── settings.rs # 设置命令
|
||||
│ │ │ ├── plugin.rs # 插件命令
|
||||
│ │ │ ├── import_export.rs # 导入导出命令
|
||||
│ │ │ └── misc.rs # 杂项命令
|
||||
│ │ ├── services/ # Service 层(业务逻辑)
|
||||
│ │ │ ├── provider.rs # ProviderService
|
||||
│ │ │ ├── mcp.rs # McpService
|
||||
│ │ │ ├── config.rs # ConfigService
|
||||
│ │ │ └── speedtest.rs # SpeedtestService
|
||||
│ │ ├── app_config.rs # 配置数据模型
|
||||
│ │ ├── provider.rs # 供应商领域模型
|
||||
│ │ ├── store.rs # 全局状态管理
|
||||
│ │ ├── mcp.rs # MCP 同步与校验
|
||||
│ │ ├── error.rs # 统一错误类型
|
||||
│ │ ├── usage_script.rs # 用量脚本执行
|
||||
│ │ ├── claude_plugin.rs # Claude 插件管理
|
||||
│ │ └── lib.rs # 应用入口
|
||||
│ ├── capabilities/ # Tauri 权限配置
|
||||
│ └── icons/ # 应用图标
|
||||
├── tests/ # 前端测试(v3.6 新增)
|
||||
│ ├── hooks/ # Hooks 单元测试
|
||||
│ ├── components/ # 组件集成测试
|
||||
│ └── setup.ts # 测试配置
|
||||
└── assets/ # 静态资源
|
||||
├── screenshots/ # 界面截图
|
||||
└── partners/ # 合作商资源
|
||||
├── logos/ # 合作商 Logo
|
||||
└── banners/ # 合作商横幅/宣传图
|
||||
```
|
||||
|
||||
## 更新日志
|
||||
|
||||
查看 [CHANGELOG.md](CHANGELOG.md) 了解版本更新详情。
|
||||
|
||||
## Electron 旧版
|
||||
|
||||
[Releases](../../releases) 里保留 v2.0.3 Electron 旧版
|
||||
|
||||
如果需要旧版 Electron 代码,可以拉取 electron-legacy 分支
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎提交 Issue 反馈问题和建议!
|
||||
|
||||
提交 PR 前请确保:
|
||||
|
||||
- 通过类型检查:`pnpm typecheck`
|
||||
- 通过格式检查:`pnpm format:check`
|
||||
- 通过单元测试:`pnpm test:unit`
|
||||
- 功能性 PR 请先经过 issue 区讨论
|
||||
|
||||
## Star History
|
||||
|
||||
[](https://www.star-history.com/#farion1231/cc-switch&Date)
|
||||
|
||||
## License
|
||||
|
||||
MIT © Jason Young
|
||||
76
README_i18n.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# CC Switch 国际化功能说明
|
||||
|
||||
## 已完成的工作
|
||||
|
||||
1. **安装依赖**:添加了 `react-i18next` 和 `i18next` 包
|
||||
2. **配置国际化**:在 `src/i18n/` 目录下创建了配置文件
|
||||
3. **翻译文件**:创建了英文和中文翻译文件
|
||||
4. **组件更新**:替换了主要组件中的硬编码文案
|
||||
5. **语言切换器**:添加了语言切换按钮
|
||||
|
||||
## 文件结构
|
||||
|
||||
```
|
||||
src/
|
||||
├── i18n/
|
||||
│ ├── index.ts # 国际化配置文件
|
||||
│ └── locales/
|
||||
│ ├── en.json # 英文翻译
|
||||
│ └── zh.json # 中文翻译
|
||||
├── components/
|
||||
│ └── LanguageSwitcher.tsx # 语言切换组件
|
||||
└── main.tsx # 导入国际化配置
|
||||
```
|
||||
|
||||
## 默认语言设置
|
||||
|
||||
- **默认语言**:英文 (en)
|
||||
- **回退语言**:英文 (en)
|
||||
|
||||
## 使用方式
|
||||
|
||||
1. 在组件中导入 `useTranslation`:
|
||||
```tsx
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
function MyComponent() {
|
||||
const { t } = useTranslation();
|
||||
return <div>{t('common.save')}</div>;
|
||||
}
|
||||
```
|
||||
|
||||
2. 切换语言:
|
||||
```tsx
|
||||
const { i18n } = useTranslation();
|
||||
i18n.changeLanguage('zh'); // 切换到中文
|
||||
```
|
||||
|
||||
## 翻译键结构
|
||||
|
||||
- `common.*` - 通用文案(保存、取消、设置等)
|
||||
- `header.*` - 头部相关文案
|
||||
- `provider.*` - 供应商相关文案
|
||||
- `notifications.*` - 通知消息
|
||||
- `settings.*` - 设置页面文案
|
||||
- `apps.*` - 应用名称
|
||||
- `console.*` - 控制台日志信息
|
||||
|
||||
## 测试功能
|
||||
|
||||
应用已添加了语言切换按钮(地球图标),点击可以在中英文之间切换,验证国际化功能是否正常工作。
|
||||
|
||||
## 已更新的组件
|
||||
|
||||
- ✅ App.tsx - 主应用组件
|
||||
- ✅ ConfirmDialog.tsx - 确认对话框
|
||||
- ✅ AddProviderModal.tsx - 添加供应商弹窗
|
||||
- ✅ EditProviderModal.tsx - 编辑供应商弹窗
|
||||
- ✅ ProviderList.tsx - 供应商列表
|
||||
- ✅ LanguageSwitcher.tsx - 语言切换器
|
||||
- ✅ settings/SettingsDialog.tsx - 设置对话框
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 所有新的文案都应该添加到翻译文件中,而不是硬编码
|
||||
2. 翻译键名应该有意义且结构化
|
||||
3. 可以通过修改 `src/i18n/index.ts` 中的 `lng` 配置来更改默认语言
|
||||
BIN
assets/partners/banners/glm-en.jpg
Normal file
|
After Width: | Height: | Size: 102 KiB |
BIN
assets/partners/banners/glm-zh.jpg
Normal file
|
After Width: | Height: | Size: 110 KiB |
BIN
assets/screenshots/add-en.png
Normal file
|
After Width: | Height: | Size: 185 KiB |
BIN
assets/screenshots/add-zh.png
Normal file
|
After Width: | Height: | Size: 203 KiB |
BIN
assets/screenshots/main-en.png
Normal file
|
After Width: | Height: | Size: 204 KiB |
BIN
assets/screenshots/main-zh.png
Normal file
|
After Width: | Height: | Size: 205 KiB |
21
components.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"$schema": "https://ui.shadcn.com/schema.json",
|
||||
"style": "default",
|
||||
"rsc": false,
|
||||
"tsx": true,
|
||||
"tailwind": {
|
||||
"config": "tailwind.config.js",
|
||||
"css": "src/index.css",
|
||||
"baseColor": "neutral",
|
||||
"cssVariables": true,
|
||||
"prefix": ""
|
||||
},
|
||||
"iconLibrary": "lucide",
|
||||
"aliases": {
|
||||
"components": "@/components",
|
||||
"utils": "@/lib/utils",
|
||||
"ui": "@/components/ui",
|
||||
"lib": "@/lib",
|
||||
"hooks": "@/hooks"
|
||||
}
|
||||
}
|
||||
169
docs/BACKEND_REFACTOR_PLAN.md
Normal file
@@ -0,0 +1,169 @@
|
||||
# CC Switch Rust 后端重构方案
|
||||
|
||||
## 目录
|
||||
- [背景与现状](#背景与现状)
|
||||
- [问题确认](#问题确认)
|
||||
- [方案评估](#方案评估)
|
||||
- [渐进式重构路线](#渐进式重构路线)
|
||||
- [测试策略](#测试策略)
|
||||
- [风险与对策](#风险与对策)
|
||||
- [总结](#总结)
|
||||
|
||||
## 背景与现状
|
||||
- 前端已完成重构,后端 (Tauri + Rust) 仍维持历史结构。
|
||||
- 核心文件集中在 `src-tauri/src/commands.rs`、`lib.rs` 等超大文件中,业务逻辑与界面事件耦合严重。
|
||||
- 测试覆盖率低,只有零散单元测试,缺乏集成验证。
|
||||
|
||||
## 问题确认
|
||||
|
||||
| 提案问题 | 实际情况 | 严重程度 |
|
||||
| --- | --- | --- |
|
||||
| `commands.rs` 过长 | ✅ 1526 行,包含 32 个命令,职责混杂 | 🔴 高 |
|
||||
| `lib.rs` 缺少服务层 | ✅ 541 行,托盘/事件/业务逻辑耦合 | 🟡 中 |
|
||||
| `Result<T, String>` 泛滥 | ✅ 118 处,错误上下文丢失 | 🟡 中 |
|
||||
| 全局 `Mutex` 阻塞 | ✅ 31 处 `.lock()` 调用,读写不分离 | 🟡 中 |
|
||||
| 配置逻辑分散 | ✅ 分布在 5 个文件 (`config`/`app_config`/`app_store`/`settings`/`codex_config`) | 🟢 低 |
|
||||
|
||||
代码规模分布(约 5.4k SLOC):
|
||||
- `commands.rs`: 1526 行(28%)→ 第一优先级 🎯
|
||||
- `lib.rs`: 541 行(10%)→ 托盘逻辑与业务耦合
|
||||
- `mcp.rs`: 732 行(14%)→ 相对清晰
|
||||
- `migration.rs`: 431 行(8%)→ 一次性逻辑
|
||||
- 其他文件合计:2156 行(40%)
|
||||
|
||||
## 方案评估
|
||||
|
||||
### ✅ 优点
|
||||
1. **分层架构清晰**
|
||||
- `commands/`:Tauri 命令薄层
|
||||
- `services/`:业务流程,如供应商切换、MCP 同步
|
||||
- `infrastructure/`:配置读写、外设交互
|
||||
- `domain/`:数据模型 (`Provider`, `AppType` 等)
|
||||
→ 提升可测试性、降低耦合度、方便团队协作。
|
||||
|
||||
2. **统一错误处理**
|
||||
- 引入 `AppError`(`thiserror`),保留错误链和上下文。
|
||||
- Tauri 命令仍返回 `Result<T, String>`,通过 `From<AppError>` 自动转换。
|
||||
- 改善日志可读性,利于排查。
|
||||
|
||||
3. **并发优化**
|
||||
- `AppState` 切换为 `RwLock<MultiAppConfig>`。
|
||||
- 读多写少的场景提升吞吐(如频繁查询供应商列表)。
|
||||
|
||||
### ⚠️ 风险
|
||||
1. **过度设计**
|
||||
- 完整 DDD 四层在 5k 行项目中会增加 30-50% 维护成本。
|
||||
- Rust trait + repository 样板较多,收益不足。
|
||||
- 推荐“轻量分层”而非正统 DDD。
|
||||
|
||||
2. **迁移成本高**
|
||||
- `commands.rs` 拆分、错误统一、锁改造触及多文件。
|
||||
- 测试缺失导致重构风险高,需先补测试。
|
||||
- 估算完整改造需 5-6 周;建议分阶段输出可落地价值。
|
||||
|
||||
3. **技术选型需谨慎**
|
||||
- `parking_lot` 相比标准库 `RwLock` 提升有限,不必引入。
|
||||
- `spawn_blocking` 仅用于 >100ms 的阻塞任务,避免滥用。
|
||||
- 以现有依赖为主,控制复杂度。
|
||||
|
||||
## 实施进度
|
||||
- **阶段 1:统一错误处理 ✅**
|
||||
- 引入 `thiserror` 并在 `src-tauri/src/error.rs` 定义 `AppError`,提供常用构造函数和 `From<AppError> for String`,保留错误链路。
|
||||
- 配置、存储、同步等核心模块(`config.rs`、`app_config.rs`、`app_store.rs`、`store.rs`、`codex_config.rs`、`claude_mcp.rs`、`claude_plugin.rs`、`import_export.rs`、`mcp.rs`、`migration.rs`、`speedtest.rs`、`usage_script.rs`、`settings.rs`、`lib.rs` 等)已统一返回 `Result<_, AppError>`,避免字符串错误丢失上下文。
|
||||
- Tauri 命令层继续返回 `Result<_, String>`,通过 `?` + `Into<String>` 统一转换,前端无需调整。
|
||||
- `cargo check` 通过,`rg "Result<[^>]+, String"` 巡检确认除命令层外已无字符串错误返回。
|
||||
- **阶段 2:拆分命令层 ✅**
|
||||
- 已将单一 `src-tauri/src/commands.rs` 拆分为 `commands/{provider,mcp,config,settings,misc,plugin}.rs` 并通过 `commands/mod.rs` 统一导出,保持对外 API 不变。
|
||||
- 每个文件聚焦单一功能域(供应商、MCP、配置、设置、杂项、插件),命令函数平均 150-250 行,可读性与后续维护性显著提升。
|
||||
- 相关依赖调整后 `cargo check` 通过,静态巡检确认无重复定义或未注册命令。
|
||||
- **阶段 3:补充测试 ✅**
|
||||
- `tests/import_export_sync.rs` 集成测试涵盖配置备份、Claude/Codex live 同步、MCP 投影与 Codex/Claude 双向导入流程,并新增启用项清理、非法 TOML 抛错等失败场景验证;统一使用隔离 HOME 目录避免污染真实用户环境。
|
||||
- 扩展 `lib.rs` re-export,暴露 `AppType`、`MultiAppConfig`、`AppError`、配置 IO 以及 Codex/Claude MCP 路径与同步函数,方便服务层及测试直接复用核心逻辑。
|
||||
- 新增负向测试验证 Codex 供应商缺少 `auth` 字段时的错误返回,并补充备份数量上限测试;顺带修复 `create_backup` 采用内存读写避免拷贝继承旧的修改时间,确保最新备份不会在清理阶段被误删。
|
||||
- 针对 `codex_config::write_codex_live_atomic` 补充成功与失败场景测试,覆盖 auth/config 原子写入与失败回滚逻辑(模拟目标路径为目录时的 rename 失败),降低 Codex live 写入回归风险。
|
||||
- 新增 `tests/provider_commands.rs` 覆盖 `switch_provider` 的 Codex 正常流程与供应商缺失分支,并抽取 `switch_provider_internal` 以复用 `AppError`,通过 `switch_provider_test_hook` 暴露测试入口;同时共享 `tests/support.rs` 提供隔离 HOME / 互斥工具函数。
|
||||
- 补充 Claude 切换集成测试,验证 live `settings.json` 覆写、新旧供应商快照回填以及 `.cc-switch/config.json` 持久化结果,确保阶段四提取服务层时拥有可回归的用例。
|
||||
- 增加 Codex 缺失 `auth` 场景测试,确认 `switch_provider_internal` 在关键字段缺失时返回带上下文的 `AppError`,同时保持内存状态未被污染。
|
||||
- 为配置导入命令抽取复用逻辑 `import_config_from_path` 并补充成功/失败集成测试,校验备份生成、状态同步、JSON 解析与文件缺失等错误回退路径;`export_config_to_file` 亦具备成功/缺失源文件的命令级回归。
|
||||
- 新增 `tests/mcp_commands.rs`,通过测试钩子覆盖 `import_default_config`、`import_mcp_from_claude`、`set_mcp_enabled` 等命令层行为,验证缺失文件/非法 JSON 的错误回滚以及成功路径落盘效果;阶段三目标达成,命令层关键边界已具备回归保障。
|
||||
- **阶段 4:服务层抽象 🚧(进行中)**
|
||||
- 新增 `services/provider.rs` 并实现 `ProviderService::switch` / `delete`,集中处理供应商切换、回填、MCP 同步等核心业务;命令层改为薄封装并在 `tests/provider_service.rs`、`tests/provider_commands.rs` 中完成成功与失败路径的集成验证。
|
||||
- 新增 `services/mcp.rs` 提供 `McpService`,封装 MCP 服务器的查询、增删改、启用同步与导入流程;命令层改为参数解析 + 调用服务,`tests/mcp_commands.rs` 直接使用 `McpService` 验证成功与失败路径,阶段三测试继续适配。
|
||||
- `McpService` 在内部先复制内存快照、释放写锁,再执行文件同步,避免阶段五升级后的 `RwLock` 在 I/O 场景被长时间占用;`upsert/delete/set_enabled/sync_enabled` 均已修正。
|
||||
- 新增 `services/config.rs` 提供 `ConfigService`,统一处理配置导入导出、备份与 live 同步;命令层迁移至 `commands/import_export.rs`,在落盘操作前释放锁并复用现有集成测试。
|
||||
- 新增 `services/speedtest.rs` 并实现 `SpeedtestService::test_endpoints`,将 URL 校验、超时裁剪与网络请求封装在服务层,命令改为薄封装;补充单元测试覆盖空列表与非法 URL 分支。
|
||||
- 后续可选:应用设置(Store)命令仍较薄,可按需评估是否抽象;当前阶段四核心服务已基本齐备。
|
||||
- **阶段 5:锁与阻塞优化 ✅(首轮)**
|
||||
- `AppState` 已由 `Mutex<MultiAppConfig>` 切换为 `RwLock<MultiAppConfig>`,托盘、命令与测试均按读写语义区分 `read()` / `write()`;`cargo test` 全量通过验证并未破坏现有流程。
|
||||
- 针对高开销 IO 的配置导入/导出命令提取 `load_config_for_import`,并通过 `tauri::async_runtime::spawn_blocking` 将文件读写与备份迁至阻塞线程,保持命令处理线程轻量。
|
||||
- 其余命令梳理后确认仍属轻量同步操作,暂不额外引入 `spawn_blocking`;若后续出现新的长耗时流程,再按同一模式扩展。
|
||||
|
||||
## 渐进式重构路线
|
||||
|
||||
### 阶段 1:统一错误处理(高收益 / 低风险)
|
||||
- 新增 `src-tauri/src/error.rs`,定义 `AppError`。
|
||||
- 底层文件 IO、配置解析等函数返回 `Result<T, AppError>`。
|
||||
- 命令层通过 `?` 自动传播,最终 `.map_err(Into::into)`。
|
||||
- 预估 3-5 天,立即启动。
|
||||
|
||||
### 阶段 2:拆分 `commands.rs`(高收益 / 中风险)
|
||||
- 按业务拆分为 `commands/provider.rs`、`commands/mcp.rs`、`commands/config.rs`、`commands/settings.rs`、`commands/misc.rs`。
|
||||
- `commands/mod.rs` 统一导出和注册。
|
||||
- 文件行数降低到 200-300 行/文件,职责单一。
|
||||
- 预估 5-7 天,可并行进行部分重构。
|
||||
|
||||
### 阶段 3:补充测试(中收益 / 中风险)
|
||||
- 引入 `tests/` 或 `src-tauri/tests/` 集成测试,覆盖供应商切换、MCP 同步、配置迁移。
|
||||
- 使用 `tempfile`/`tempdir` 隔离文件系统,组合少量回归脚本。
|
||||
- 预估 5-7 天,为后续重构提供安全网。
|
||||
|
||||
### 阶段 4:提取轻量服务层(中收益 / 中风险)
|
||||
- 新增 `services/provider_service.rs`、`services/mcp_service.rs`。
|
||||
- 不强制使用 trait;直接以自由函数/结构体实现业务流程。
|
||||
```rust
|
||||
pub struct ProviderService;
|
||||
impl ProviderService {
|
||||
pub fn switch(config: &mut MultiAppConfig, app: AppType, id: &str) -> Result<(), AppError> {
|
||||
// 业务流程:验证、回填、落盘、更新 current、触发事件
|
||||
}
|
||||
}
|
||||
```
|
||||
- 命令层负责参数解析,服务层处理业务逻辑,托盘逻辑重用同一接口。
|
||||
- 预估 7-10 天,可在测试补齐后执行。
|
||||
|
||||
### 阶段 5:锁与阻塞优化(低收益 / 低风险)
|
||||
- ✅ `AppState` 已从 `Mutex` 切换为 `RwLock`,命令与托盘读写按需区分,现有测试全部通过。
|
||||
- ✅ 配置导入/导出命令通过 `spawn_blocking` 处理高开销文件 IO;其他命令维持同步执行以避免不必要调度。
|
||||
- 🔄 持续监控:若后续引入新的批量迁移或耗时任务,再按相同模式扩展到阻塞线程;观察运行时锁竞争情况,必要时考虑进一步拆分状态或引入缓存。
|
||||
|
||||
## 测试策略
|
||||
- **优先覆盖场景**
|
||||
- 供应商切换:状态更新 + live 配置同步
|
||||
- MCP 同步:enabled 服务器快照与落盘
|
||||
- 配置迁移:归档、备份与版本升级
|
||||
- **推荐结构**
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod integration {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn switch_provider_updates_live_config() { /* ... */ }
|
||||
#[test]
|
||||
fn sync_mcp_to_codex_updates_claude_config() { /* ... */ }
|
||||
#[test]
|
||||
fn migration_preserves_backup() { /* ... */ }
|
||||
}
|
||||
```
|
||||
- 目标覆盖率:关键路径 >80%,文件 IO/迁移 >70%。
|
||||
|
||||
## 风险与对策
|
||||
- **测试不足** → 阶段 3 强制补齐,建立基础集成测试。
|
||||
- **重构跨度大** → 按阶段在独立分支推进(如 `refactor/backend-step1` 等)。
|
||||
- **回滚困难** → 每阶段结束打 tag(如 `v3.6.0-backend-step1`),保留回滚点。
|
||||
- **功能回归** → 重构后执行手动冒烟流程:供应商切换、托盘操作、MCP 同步、配置导入导出。
|
||||
|
||||
## 总结
|
||||
- 当前规模下不建议整体引入完整 DDD/四层架构,避免过度设计。
|
||||
- 建议遵循“错误统一 → 命令拆分 → 补测试 → 服务层抽象 → 锁优化”的渐进式策略。
|
||||
- 完成阶段 1-3 后即可显著提升可维护性与可靠性;阶段 4-5 可根据资源灵活安排。
|
||||
- 重构过程中同步维护文档与测试,确保团队成员对架构演进保持一致认知。
|
||||
490
docs/REFACTORING_CHECKLIST.md
Normal file
@@ -0,0 +1,490 @@
|
||||
# CC Switch 重构实施清单
|
||||
|
||||
> 用于跟踪重构进度的详细检查清单
|
||||
|
||||
**开始日期**: ___________
|
||||
**预计完成**: ___________
|
||||
**当前阶段**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 0: 准备阶段 (预计 1 天)
|
||||
|
||||
### 环境准备
|
||||
|
||||
- [ ] 创建新分支 `refactor/modernization`
|
||||
- [ ] 创建备份标签 `git tag backup-before-refactor`
|
||||
- [ ] 备份用户配置文件 `~/.cc-switch/config.json`
|
||||
- [ ] 通知团队成员重构开始
|
||||
|
||||
### 依赖安装
|
||||
|
||||
```bash
|
||||
pnpm add @tanstack/react-query
|
||||
pnpm add react-hook-form @hookform/resolvers
|
||||
pnpm add zod
|
||||
pnpm add sonner
|
||||
pnpm add next-themes
|
||||
pnpm add @radix-ui/react-dialog @radix-ui/react-dropdown-menu
|
||||
pnpm add @radix-ui/react-label @radix-ui/react-select
|
||||
pnpm add @radix-ui/react-slot @radix-ui/react-switch @radix-ui/react-tabs
|
||||
pnpm add class-variance-authority clsx tailwind-merge tailwindcss-animate
|
||||
```
|
||||
|
||||
- [ ] 安装核心依赖 (上述命令)
|
||||
- [ ] 验证依赖安装成功 `pnpm install`
|
||||
- [ ] 验证编译通过 `pnpm typecheck`
|
||||
|
||||
### 配置文件
|
||||
|
||||
- [ ] 创建 `components.json`
|
||||
- [ ] 更新 `tsconfig.json` 添加路径别名
|
||||
- [ ] 更新 `vite.config.mts` 添加路径解析
|
||||
- [ ] 验证开发服务器启动 `pnpm dev`
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 1: 基础设施 (预计 2-3 天)
|
||||
|
||||
### 1.1 工具函数和基础组件
|
||||
|
||||
- [ ] 创建 `src/lib/utils.ts` (cn 函数)
|
||||
- [ ] 创建 `src/components/ui/button.tsx`
|
||||
- [ ] 创建 `src/components/ui/dialog.tsx`
|
||||
- [ ] 创建 `src/components/ui/input.tsx`
|
||||
- [ ] 创建 `src/components/ui/label.tsx`
|
||||
- [ ] 创建 `src/components/ui/textarea.tsx`
|
||||
- [ ] 创建 `src/components/ui/select.tsx`
|
||||
- [ ] 创建 `src/components/ui/switch.tsx`
|
||||
- [ ] 创建 `src/components/ui/tabs.tsx`
|
||||
- [ ] 创建 `src/components/ui/sonner.tsx`
|
||||
- [ ] 创建 `src/components/ui/form.tsx`
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证所有 UI 组件可以正常导入
|
||||
- [ ] 创建一个测试页面验证组件样式
|
||||
|
||||
### 1.2 Query Client 设置
|
||||
|
||||
- [ ] 创建 `src/lib/query/queryClient.ts`
|
||||
- [ ] 配置默认选项 (retry, staleTime 等)
|
||||
- [ ] 导出 queryClient 实例
|
||||
|
||||
### 1.3 API 层
|
||||
|
||||
- [ ] 创建 `src/lib/api/providers.ts`
|
||||
- [ ] getAll
|
||||
- [ ] getCurrent
|
||||
- [ ] add
|
||||
- [ ] update
|
||||
- [ ] delete
|
||||
- [ ] switch
|
||||
- [ ] importDefault
|
||||
- [ ] updateTrayMenu
|
||||
|
||||
- [ ] 创建 `src/lib/api/settings.ts`
|
||||
- [ ] get
|
||||
- [ ] save
|
||||
|
||||
- [ ] 创建 `src/lib/api/mcp.ts`
|
||||
- [ ] getConfig
|
||||
- [ ] upsertServer
|
||||
- [ ] deleteServer
|
||||
|
||||
- [ ] 创建 `src/lib/api/index.ts` (聚合导出)
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证 API 调用不会出现运行时错误
|
||||
- [ ] 确认类型定义正确
|
||||
|
||||
### 1.4 Query Hooks
|
||||
|
||||
- [ ] 创建 `src/lib/query/queries.ts`
|
||||
- [ ] useProvidersQuery
|
||||
- [ ] useSettingsQuery
|
||||
- [ ] useMcpConfigQuery
|
||||
|
||||
- [ ] 创建 `src/lib/query/mutations.ts`
|
||||
- [ ] useAddProviderMutation
|
||||
- [ ] useSwitchProviderMutation
|
||||
- [ ] useDeleteProviderMutation
|
||||
- [ ] useUpdateProviderMutation
|
||||
- [ ] useSaveSettingsMutation
|
||||
|
||||
- [ ] 创建 `src/lib/query/index.ts` (聚合导出)
|
||||
|
||||
**测试**:
|
||||
- [ ] 在临时组件中测试每个 hook
|
||||
- [ ] 验证 loading/error 状态正确
|
||||
- [ ] 验证缓存和自动刷新工作
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 2: 核心功能重构 (预计 3-4 天)
|
||||
|
||||
### 2.1 主题系统
|
||||
|
||||
- [ ] 创建 `src/components/theme-provider.tsx`
|
||||
- [ ] 创建 `src/components/mode-toggle.tsx`
|
||||
- [ ] 更新 `src/index.css` 添加主题变量
|
||||
- [ ] 删除 `src/hooks/useDarkMode.ts`
|
||||
- [ ] 更新所有组件使用新的主题系统
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证主题切换正常工作
|
||||
- [ ] 验证系统主题跟随功能
|
||||
- [ ] 验证主题持久化
|
||||
|
||||
### 2.2 更新 main.tsx
|
||||
|
||||
- [ ] 引入 QueryClientProvider
|
||||
- [ ] 引入 ThemeProvider
|
||||
- [ ] 添加 Toaster 组件
|
||||
- [ ] 移除旧的 API 导入
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证应用可以正常启动
|
||||
- [ ] 验证 Context 正确传递
|
||||
|
||||
### 2.3 重构 App.tsx
|
||||
|
||||
- [ ] 使用 useProvidersQuery 替代手动状态管理
|
||||
- [ ] 移除所有 loadProviders 相关代码
|
||||
- [ ] 移除手动 notification 状态
|
||||
- [ ] 简化事件监听逻辑
|
||||
- [ ] 更新对话框为新的 Dialog 组件
|
||||
|
||||
**目标**: 将 412 行代码减少到 ~100 行
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证供应商列表正常加载
|
||||
- [ ] 验证切换 Claude/Codex 正常工作
|
||||
- [ ] 验证事件监听正常工作
|
||||
|
||||
### 2.4 重构 ProviderList
|
||||
|
||||
- [ ] 创建 `src/components/providers/ProviderList.tsx`
|
||||
- [ ] 使用 mutation hooks 处理操作
|
||||
- [ ] 移除 onNotify prop
|
||||
- [ ] 移除手动状态管理
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证供应商列表渲染
|
||||
- [ ] 验证切换操作
|
||||
- [ ] 验证删除操作
|
||||
|
||||
### 2.5 重构表单系统
|
||||
|
||||
- [ ] 创建 `src/lib/schemas/provider.ts` (Zod schema)
|
||||
- [ ] 创建 `src/components/providers/ProviderForm.tsx`
|
||||
- [ ] 使用 react-hook-form
|
||||
- [ ] 使用 zodResolver
|
||||
- [ ] 字段级验证
|
||||
|
||||
- [ ] 创建 `src/components/providers/AddProviderDialog.tsx`
|
||||
- [ ] 使用新的 Dialog 组件
|
||||
- [ ] 集成 ProviderForm
|
||||
- [ ] 使用 useAddProviderMutation
|
||||
|
||||
- [ ] 创建 `src/components/providers/EditProviderDialog.tsx`
|
||||
- [ ] 使用新的 Dialog 组件
|
||||
- [ ] 集成 ProviderForm
|
||||
- [ ] 使用 useUpdateProviderMutation
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证表单验证正常工作
|
||||
- [ ] 验证错误提示显示正确
|
||||
- [ ] 验证提交操作成功
|
||||
- [ ] 验证表单重置功能
|
||||
|
||||
### 2.6 清理旧组件
|
||||
|
||||
- [x] 删除 `src/components/AddProviderModal.tsx`
|
||||
- [x] 删除 `src/components/EditProviderModal.tsx`
|
||||
- [x] 更新所有引用这些组件的地方
|
||||
- [x] 删除 `src/components/ProviderForm.tsx` 及 `src/components/ProviderForm/`
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 3: 设置和辅助功能 (预计 2-3 天)
|
||||
|
||||
### 3.1 重构 SettingsDialog
|
||||
|
||||
- [ ] 创建 `src/components/settings/SettingsDialog.tsx`
|
||||
- [ ] 使用 Tabs 组件
|
||||
- [ ] 集成各个设置子组件
|
||||
|
||||
- [ ] 创建 `src/components/settings/GeneralSettings.tsx`
|
||||
- [ ] 语言设置
|
||||
- [ ] 配置目录设置
|
||||
- [ ] 其他通用设置
|
||||
|
||||
- [ ] 创建 `src/components/settings/AboutSection.tsx`
|
||||
- [ ] 版本信息
|
||||
- [ ] 更新检查
|
||||
- [ ] 链接
|
||||
|
||||
- [ ] 创建 `src/components/settings/ImportExportSection.tsx`
|
||||
- [ ] 导入功能
|
||||
- [ ] 导出功能
|
||||
|
||||
**目标**: 将 643 行拆分为 4-5 个小组件,每个 100-150 行
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证设置保存功能
|
||||
- [ ] 验证导入导出功能
|
||||
- [ ] 验证更新检查功能
|
||||
|
||||
### 3.2 重构通知系统
|
||||
|
||||
- [ ] 在所有 mutations 中使用 `toast` 替代 `showNotification`
|
||||
- [ ] 移除 App.tsx 中的 notification 状态
|
||||
- [ ] 移除自定义通知组件
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证成功通知显示
|
||||
- [ ] 验证错误通知显示
|
||||
- [ ] 验证通知自动消失
|
||||
|
||||
### 3.3 重构确认对话框
|
||||
|
||||
- [ ] 更新 `src/components/ConfirmDialog.tsx` 使用新的 Dialog
|
||||
- [ ] 或者直接使用 shadcn/ui 的 AlertDialog
|
||||
|
||||
**测试**:
|
||||
- [ ] 验证删除确认对话框
|
||||
- [ ] 验证其他确认场景
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 4: 清理和优化 (预计 1-2 天)
|
||||
|
||||
### 4.1 移除旧代码
|
||||
|
||||
- [x] 删除 `src/lib/styles.ts`
|
||||
- [x] 从 `src/lib/tauri-api.ts` 移除 `window.api` 绑定
|
||||
- [x] 精简 `src/lib/tauri-api.ts`,只保留事件监听相关
|
||||
- [x] 删除或更新 `src/vite-env.d.ts` 中的过时类型
|
||||
|
||||
### 4.2 代码审查
|
||||
|
||||
- [ ] 检查所有 TODO 注释
|
||||
- [x] 检查是否还有 `window.api` 调用
|
||||
- [ ] 检查是否还有手动状态管理
|
||||
- [x] 统一代码风格
|
||||
|
||||
### 4.3 类型检查
|
||||
|
||||
- [x] 运行 `pnpm typecheck` 确保无错误
|
||||
- [x] 修复所有类型错误
|
||||
- [x] 更新类型定义
|
||||
|
||||
### 4.4 性能优化
|
||||
|
||||
- [ ] 检查是否有不必要的重渲染
|
||||
- [ ] 添加必要的 React.memo
|
||||
- [ ] 优化 Query 缓存配置
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 阶段 5: 测试和修复 (预计 2-3 天)
|
||||
|
||||
### 5.1 功能测试
|
||||
|
||||
#### 供应商管理
|
||||
- [ ] 添加供应商 (Claude)
|
||||
- [ ] 添加供应商 (Codex)
|
||||
- [ ] 编辑供应商
|
||||
- [ ] 删除供应商
|
||||
- [ ] 切换供应商
|
||||
- [ ] 导入默认配置
|
||||
|
||||
#### 应用切换
|
||||
- [ ] Claude <-> Codex 切换
|
||||
- [ ] 切换后数据正确加载
|
||||
- [ ] 切换后托盘菜单更新
|
||||
|
||||
#### 设置
|
||||
- [ ] 保存通用设置
|
||||
- [ ] 切换语言
|
||||
- [ ] 配置目录选择
|
||||
- [ ] 导入配置
|
||||
- [ ] 导出配置
|
||||
|
||||
#### UI 交互
|
||||
- [ ] 主题切换 (亮色/暗色)
|
||||
- [ ] 对话框打开/关闭
|
||||
- [ ] 表单验证
|
||||
- [ ] Toast 通知
|
||||
|
||||
#### MCP 管理
|
||||
- [ ] 列表显示
|
||||
- [ ] 添加 MCP
|
||||
- [ ] 编辑 MCP
|
||||
- [ ] 删除 MCP
|
||||
- [ ] 启用/禁用 MCP
|
||||
|
||||
### 5.2 边界情况测试
|
||||
|
||||
- [ ] 空供应商列表
|
||||
- [ ] 无效配置文件
|
||||
- [ ] 网络错误
|
||||
- [ ] 后端错误响应
|
||||
- [ ] 并发操作
|
||||
- [ ] 表单输入边界值
|
||||
|
||||
### 5.3 兼容性测试
|
||||
|
||||
- [ ] Windows 测试
|
||||
- [ ] macOS 测试
|
||||
- [ ] Linux 测试
|
||||
|
||||
### 5.4 性能测试
|
||||
|
||||
- [ ] 100+ 供应商加载速度
|
||||
- [ ] 快速切换供应商
|
||||
- [ ] 内存使用情况
|
||||
- [ ] CPU 使用情况
|
||||
|
||||
### 5.5 Bug 修复
|
||||
|
||||
**Bug 列表** (发现后记录):
|
||||
|
||||
1. ___________
|
||||
- [ ] 已修复
|
||||
- [ ] 已验证
|
||||
|
||||
2. ___________
|
||||
- [ ] 已修复
|
||||
- [ ] 已验证
|
||||
|
||||
**完成时间**: ___________
|
||||
**遇到的问题**: ___________
|
||||
|
||||
---
|
||||
|
||||
## 📋 最终检查
|
||||
|
||||
### 代码质量
|
||||
|
||||
- [ ] 所有 TypeScript 错误已修复
|
||||
- [ ] 运行 `pnpm format` 格式化代码
|
||||
- [ ] 运行 `pnpm typecheck` 通过
|
||||
- [ ] 代码审查完成
|
||||
|
||||
### 文档更新
|
||||
|
||||
- [ ] 更新 `CLAUDE.md` 反映新架构
|
||||
- [ ] 更新 `README.md` (如有必要)
|
||||
- [ ] 添加 Migration Guide (可选)
|
||||
|
||||
### 性能基准
|
||||
|
||||
记录性能数据:
|
||||
|
||||
**旧版本**:
|
||||
- 启动时间: _____ms
|
||||
- 供应商加载: _____ms
|
||||
- 内存占用: _____MB
|
||||
|
||||
**新版本**:
|
||||
- 启动时间: _____ms
|
||||
- 供应商加载: _____ms
|
||||
- 内存占用: _____MB
|
||||
|
||||
### 代码统计
|
||||
|
||||
**代码行数对比**:
|
||||
|
||||
| 文件 | 旧版本 | 新版本 | 减少 |
|
||||
|------|--------|--------|------|
|
||||
| App.tsx | 412 | ~100 | -76% |
|
||||
| tauri-api.ts | 712 | ~50 | -93% |
|
||||
| ProviderForm.tsx | 271 | ~150 | -45% |
|
||||
| settings 模块 | 1046 | ~470 (拆分) | -55% |
|
||||
| **总计** | 2038 | ~700 | **-66%** |
|
||||
|
||||
---
|
||||
|
||||
## 📦 发布准备
|
||||
|
||||
### Pre-release 测试
|
||||
|
||||
- [ ] 创建 beta 版本 `v4.0.0-beta.1`
|
||||
- [ ] 在测试环境验证
|
||||
- [ ] 收集用户反馈
|
||||
|
||||
### 正式发布
|
||||
|
||||
- [ ] 合并到 main 分支
|
||||
- [ ] 创建 Release Tag `v4.0.0`
|
||||
- [ ] 更新 Changelog
|
||||
- [ ] 发布 GitHub Release
|
||||
- [ ] 通知用户更新
|
||||
|
||||
---
|
||||
|
||||
## 🚨 回滚触发条件
|
||||
|
||||
如果出现以下情况,考虑回滚:
|
||||
|
||||
- [ ] 重大功能无法使用
|
||||
- [ ] 用户数据丢失
|
||||
- [ ] 严重性能问题
|
||||
- [ ] 无法修复的兼容性问题
|
||||
|
||||
**回滚命令**:
|
||||
```bash
|
||||
git reset --hard backup-before-refactor
|
||||
# 或
|
||||
git revert <commit-range>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 总结报告
|
||||
|
||||
### 成功指标
|
||||
|
||||
- [ ] 所有现有功能正常工作
|
||||
- [ ] 代码量减少 40%+
|
||||
- [ ] 无用户数据丢失
|
||||
- [ ] 性能未下降
|
||||
|
||||
### 经验教训
|
||||
|
||||
**遇到的主要挑战**:
|
||||
1. ___________
|
||||
2. ___________
|
||||
3. ___________
|
||||
|
||||
**解决方案**:
|
||||
1. ___________
|
||||
2. ___________
|
||||
3. ___________
|
||||
|
||||
**未来改进**:
|
||||
1. ___________
|
||||
2. ___________
|
||||
3. ___________
|
||||
|
||||
---
|
||||
|
||||
**重构完成日期**: ___________
|
||||
**总耗时**: _____ 天
|
||||
**参与人员**: ___________
|
||||
1658
docs/REFACTORING_MASTER_PLAN.md
Normal file
834
docs/REFACTORING_REFERENCE.md
Normal file
@@ -0,0 +1,834 @@
|
||||
# 重构快速参考指南
|
||||
|
||||
> 常见模式和代码示例的速查表
|
||||
|
||||
---
|
||||
|
||||
## 📑 目录
|
||||
|
||||
1. [React Query 使用](#react-query-使用)
|
||||
2. [react-hook-form 使用](#react-hook-form-使用)
|
||||
3. [shadcn/ui 组件使用](#shadcnui-组件使用)
|
||||
4. [代码迁移示例](#代码迁移示例)
|
||||
|
||||
---
|
||||
|
||||
## React Query 使用
|
||||
|
||||
### 基础查询
|
||||
|
||||
```typescript
|
||||
// 定义查询 Hook
|
||||
export const useProvidersQuery = (appId: AppId) => {
|
||||
return useQuery({
|
||||
queryKey: ['providers', appId],
|
||||
queryFn: async () => {
|
||||
const data = await providersApi.getAll(appId)
|
||||
return data
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// 在组件中使用
|
||||
function MyComponent() {
|
||||
const { data, isLoading, error } = useProvidersQuery('claude')
|
||||
|
||||
if (isLoading) return <div>Loading...</div>
|
||||
if (error) return <div>Error: {error.message}</div>
|
||||
|
||||
return <div>{/* 使用 data */}</div>
|
||||
}
|
||||
```
|
||||
|
||||
### Mutation (变更操作)
|
||||
|
||||
```typescript
|
||||
// 定义 Mutation Hook
|
||||
export const useAddProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (provider: Provider) => {
|
||||
return await providersApi.add(provider, appId)
|
||||
},
|
||||
onSuccess: () => {
|
||||
// 重新获取数据
|
||||
queryClient.invalidateQueries({ queryKey: ['providers', appId] })
|
||||
toast.success('添加成功')
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(`添加失败: ${error.message}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// 在组件中使用
|
||||
function AddProviderDialog() {
|
||||
const mutation = useAddProviderMutation('claude')
|
||||
|
||||
const handleSubmit = (data: Provider) => {
|
||||
mutation.mutate(data)
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={() => handleSubmit(formData)}
|
||||
disabled={mutation.isPending}
|
||||
>
|
||||
{mutation.isPending ? '添加中...' : '添加'}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### 乐观更新
|
||||
|
||||
```typescript
|
||||
export const useSwitchProviderMutation = (appId: AppId) => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (providerId: string) => {
|
||||
return await providersApi.switch(providerId, appId)
|
||||
},
|
||||
// 乐观更新: 在请求发送前立即更新 UI
|
||||
onMutate: async (providerId) => {
|
||||
// 取消正在进行的查询
|
||||
await queryClient.cancelQueries({ queryKey: ['providers', appId] })
|
||||
|
||||
// 保存当前数据(以便回滚)
|
||||
const previousData = queryClient.getQueryData(['providers', appId])
|
||||
|
||||
// 乐观更新
|
||||
queryClient.setQueryData(['providers', appId], (old: any) => ({
|
||||
...old,
|
||||
currentProviderId: providerId,
|
||||
}))
|
||||
|
||||
return { previousData }
|
||||
},
|
||||
// 如果失败,回滚
|
||||
onError: (err, providerId, context) => {
|
||||
queryClient.setQueryData(['providers', appId], context?.previousData)
|
||||
toast.error('切换失败')
|
||||
},
|
||||
// 无论成功失败,都重新获取数据
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['providers', appId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### 依赖查询
|
||||
|
||||
```typescript
|
||||
// 第二个查询依赖第一个查询的结果
|
||||
const { data: providers } = useProvidersQuery(appId)
|
||||
const currentProviderId = providers?.currentProviderId
|
||||
|
||||
const { data: currentProvider } = useQuery({
|
||||
queryKey: ['provider', currentProviderId],
|
||||
queryFn: () => providersApi.getById(currentProviderId!),
|
||||
enabled: !!currentProviderId, // 只有当 ID 存在时才执行
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## react-hook-form 使用
|
||||
|
||||
### 基础表单
|
||||
|
||||
```typescript
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
|
||||
// 定义验证 schema
|
||||
const schema = z.object({
|
||||
name: z.string().min(1, '请输入名称'),
|
||||
email: z.string().email('邮箱格式不正确'),
|
||||
age: z.number().min(18, '年龄必须大于18'),
|
||||
})
|
||||
|
||||
type FormData = z.infer<typeof schema>
|
||||
|
||||
function MyForm() {
|
||||
const form = useForm<FormData>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: {
|
||||
name: '',
|
||||
email: '',
|
||||
age: 0,
|
||||
},
|
||||
})
|
||||
|
||||
const onSubmit = (data: FormData) => {
|
||||
console.log(data)
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||
<input {...form.register('name')} />
|
||||
{form.formState.errors.name && (
|
||||
<span>{form.formState.errors.name.message}</span>
|
||||
)}
|
||||
|
||||
<button type="submit">提交</button>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### 使用 shadcn/ui Form 组件
|
||||
|
||||
```typescript
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from '@/components/ui/form'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
function MyForm() {
|
||||
const form = useForm<FormData>({
|
||||
resolver: zodResolver(schema),
|
||||
})
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>名称</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="请输入名称" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit">提交</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### 动态表单验证
|
||||
|
||||
```typescript
|
||||
// 根据条件动态验证
|
||||
const schema = z.object({
|
||||
type: z.enum(['official', 'custom']),
|
||||
apiKey: z.string().optional(),
|
||||
baseUrl: z.string().optional(),
|
||||
}).refine(
|
||||
(data) => {
|
||||
// 如果是自定义供应商,必须填写 baseUrl
|
||||
if (data.type === 'custom') {
|
||||
return !!data.baseUrl
|
||||
}
|
||||
return true
|
||||
},
|
||||
{
|
||||
message: '自定义供应商必须填写 Base URL',
|
||||
path: ['baseUrl'],
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### 手动触发验证
|
||||
|
||||
```typescript
|
||||
function MyForm() {
|
||||
const form = useForm<FormData>()
|
||||
|
||||
const handleBlur = async () => {
|
||||
// 验证单个字段
|
||||
await form.trigger('name')
|
||||
|
||||
// 验证多个字段
|
||||
await form.trigger(['name', 'email'])
|
||||
|
||||
// 验证所有字段
|
||||
const isValid = await form.trigger()
|
||||
}
|
||||
|
||||
return <form>...</form>
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## shadcn/ui 组件使用
|
||||
|
||||
### Dialog (对话框)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
function MyDialog() {
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>标题</DialogTitle>
|
||||
<DialogDescription>描述信息</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{/* 内容 */}
|
||||
<div>对话框内容</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setOpen(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button onClick={handleConfirm}>确认</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### Select (选择器)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
|
||||
function MySelect() {
|
||||
const [value, setValue] = useState('')
|
||||
|
||||
return (
|
||||
<Select value={value} onValueChange={setValue}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="请选择" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="option1">选项1</SelectItem>
|
||||
<SelectItem value="option2">选项2</SelectItem>
|
||||
<SelectItem value="option3">选项3</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### Tabs (标签页)
|
||||
|
||||
```typescript
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
|
||||
function MyTabs() {
|
||||
return (
|
||||
<Tabs defaultValue="tab1">
|
||||
<TabsList>
|
||||
<TabsTrigger value="tab1">标签1</TabsTrigger>
|
||||
<TabsTrigger value="tab2">标签2</TabsTrigger>
|
||||
<TabsTrigger value="tab3">标签3</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="tab1">
|
||||
<div>标签1的内容</div>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="tab2">
|
||||
<div>标签2的内容</div>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="tab3">
|
||||
<div>标签3的内容</div>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### Toast 通知 (Sonner)
|
||||
|
||||
```typescript
|
||||
import { toast } from 'sonner'
|
||||
|
||||
// 成功通知
|
||||
toast.success('操作成功')
|
||||
|
||||
// 错误通知
|
||||
toast.error('操作失败')
|
||||
|
||||
// 加载中
|
||||
const toastId = toast.loading('处理中...')
|
||||
// 完成后更新
|
||||
toast.success('处理完成', { id: toastId })
|
||||
// 或
|
||||
toast.dismiss(toastId)
|
||||
|
||||
// 自定义持续时间
|
||||
toast.success('消息', { duration: 5000 })
|
||||
|
||||
// 带操作按钮
|
||||
toast('确认删除?', {
|
||||
action: {
|
||||
label: '删除',
|
||||
onClick: () => handleDelete(),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 代码迁移示例
|
||||
|
||||
### 示例 1: 状态管理迁移
|
||||
|
||||
**旧代码** (手动状态管理):
|
||||
|
||||
```typescript
|
||||
const [providers, setProviders] = useState<Record<string, Provider>>({})
|
||||
const [currentProviderId, setCurrentProviderId] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const load = async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const data = await window.api.getProviders(appType)
|
||||
const currentId = await window.api.getCurrentProvider(appType)
|
||||
setProviders(data)
|
||||
setCurrentProviderId(currentId)
|
||||
} catch (err) {
|
||||
setError(err as Error)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
load()
|
||||
}, [appId])
|
||||
```
|
||||
|
||||
**新代码** (React Query):
|
||||
|
||||
```typescript
|
||||
const { data, isLoading, error } = useProvidersQuery(appId)
|
||||
const providers = data?.providers || {}
|
||||
const currentProviderId = data?.currentProviderId || ''
|
||||
```
|
||||
|
||||
**减少**: 从 20+ 行到 3 行
|
||||
|
||||
---
|
||||
|
||||
### 示例 2: 表单验证迁移
|
||||
|
||||
**旧代码** (手动验证):
|
||||
|
||||
```typescript
|
||||
const [name, setName] = useState('')
|
||||
const [nameError, setNameError] = useState('')
|
||||
const [apiKey, setApiKey] = useState('')
|
||||
const [apiKeyError, setApiKeyError] = useState('')
|
||||
|
||||
const validate = () => {
|
||||
let valid = true
|
||||
|
||||
if (!name.trim()) {
|
||||
setNameError('请输入名称')
|
||||
valid = false
|
||||
} else {
|
||||
setNameError('')
|
||||
}
|
||||
|
||||
if (!apiKey.trim()) {
|
||||
setApiKeyError('请输入 API Key')
|
||||
valid = false
|
||||
} else if (apiKey.length < 10) {
|
||||
setApiKeyError('API Key 长度不足')
|
||||
valid = false
|
||||
} else {
|
||||
setApiKeyError('')
|
||||
}
|
||||
|
||||
return valid
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (validate()) {
|
||||
// 提交
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form>
|
||||
<input value={name} onChange={e => setName(e.target.value)} />
|
||||
{nameError && <span>{nameError}</span>}
|
||||
|
||||
<input value={apiKey} onChange={e => setApiKey(e.target.value)} />
|
||||
{apiKeyError && <span>{apiKeyError}</span>}
|
||||
|
||||
<button onClick={handleSubmit}>提交</button>
|
||||
</form>
|
||||
)
|
||||
```
|
||||
|
||||
**新代码** (react-hook-form + zod):
|
||||
|
||||
```typescript
|
||||
const schema = z.object({
|
||||
name: z.string().min(1, '请输入名称'),
|
||||
apiKey: z.string().min(10, 'API Key 长度不足'),
|
||||
})
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(schema),
|
||||
})
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="apiKey"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit">提交</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
```
|
||||
|
||||
**减少**: 从 40+ 行到 30 行,且更健壮
|
||||
|
||||
---
|
||||
|
||||
### 示例 3: 通知系统迁移
|
||||
|
||||
**旧代码** (自定义通知):
|
||||
|
||||
```typescript
|
||||
const [notification, setNotification] = useState<{
|
||||
message: string
|
||||
type: 'success' | 'error'
|
||||
} | null>(null)
|
||||
const [isVisible, setIsVisible] = useState(false)
|
||||
|
||||
const showNotification = (message: string, type: 'success' | 'error') => {
|
||||
setNotification({ message, type })
|
||||
setIsVisible(true)
|
||||
setTimeout(() => {
|
||||
setIsVisible(false)
|
||||
setTimeout(() => setNotification(null), 300)
|
||||
}, 3000)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{notification && (
|
||||
<div className={`notification ${isVisible ? 'visible' : ''} ${notification.type}`}>
|
||||
{notification.message}
|
||||
</div>
|
||||
)}
|
||||
{/* 其他内容 */}
|
||||
</>
|
||||
)
|
||||
```
|
||||
|
||||
**新代码** (Sonner):
|
||||
|
||||
```typescript
|
||||
import { toast } from 'sonner'
|
||||
|
||||
// 在需要的地方直接调用
|
||||
toast.success('操作成功')
|
||||
toast.error('操作失败')
|
||||
|
||||
// 在 main.tsx 中只需添加一次
|
||||
import { Toaster } from '@/components/ui/sonner'
|
||||
|
||||
<Toaster />
|
||||
```
|
||||
|
||||
**减少**: 从 20+ 行到 1 行调用
|
||||
|
||||
---
|
||||
|
||||
### 示例 4: 对话框迁移
|
||||
|
||||
**旧代码** (自定义 Modal):
|
||||
|
||||
```typescript
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
|
||||
return (
|
||||
<>
|
||||
<button onClick={() => setIsOpen(true)}>打开</button>
|
||||
|
||||
{isOpen && (
|
||||
<div className="modal-backdrop" onClick={() => setIsOpen(false)}>
|
||||
<div className="modal-content" onClick={e => e.stopPropagation()}>
|
||||
<div className="modal-header">
|
||||
<h2>标题</h2>
|
||||
<button onClick={() => setIsOpen(false)}>×</button>
|
||||
</div>
|
||||
<div className="modal-body">
|
||||
{/* 内容 */}
|
||||
</div>
|
||||
<div className="modal-footer">
|
||||
<button onClick={() => setIsOpen(false)}>取消</button>
|
||||
<button onClick={handleConfirm}>确认</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
```
|
||||
|
||||
**新代码** (shadcn/ui Dialog):
|
||||
|
||||
```typescript
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button onClick={() => setIsOpen(true)}>打开</Button>
|
||||
|
||||
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>标题</DialogTitle>
|
||||
</DialogHeader>
|
||||
{/* 内容 */}
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setIsOpen(false)}>取消</Button>
|
||||
<Button onClick={handleConfirm}>确认</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
)
|
||||
```
|
||||
|
||||
**优势**:
|
||||
- 无需自定义样式
|
||||
- 内置无障碍支持
|
||||
- 自动管理焦点和 ESC 键
|
||||
|
||||
---
|
||||
|
||||
### 示例 5: API 调用迁移
|
||||
|
||||
**旧代码** (window.api):
|
||||
|
||||
```typescript
|
||||
// 添加供应商
|
||||
const handleAdd = async (provider: Provider) => {
|
||||
try {
|
||||
await window.api.addProvider(provider, appType)
|
||||
await loadProviders()
|
||||
showNotification('添加成功', 'success')
|
||||
} catch (error) {
|
||||
showNotification('添加失败', 'error')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**新代码** (React Query Mutation):
|
||||
|
||||
```typescript
|
||||
// 在组件中
|
||||
const addMutation = useAddProviderMutation(appId)
|
||||
|
||||
const handleAdd = (provider: Provider) => {
|
||||
addMutation.mutate(provider)
|
||||
// 成功和错误处理已在 mutation 定义中处理
|
||||
}
|
||||
```
|
||||
|
||||
**优势**:
|
||||
- 自动处理 loading 状态
|
||||
- 统一的错误处理
|
||||
- 自动刷新数据
|
||||
- 更少的样板代码
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Q: 如何在 mutation 成功后关闭对话框?
|
||||
|
||||
```typescript
|
||||
const mutation = useAddProviderMutation(appId)
|
||||
|
||||
const handleSubmit = (data: Provider) => {
|
||||
mutation.mutate(data, {
|
||||
onSuccess: () => {
|
||||
setIsOpen(false) // 关闭对话框
|
||||
},
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Q: 如何在表单中使用异步验证?
|
||||
|
||||
```typescript
|
||||
const schema = z.object({
|
||||
name: z.string().refine(
|
||||
async (name) => {
|
||||
// 检查名称是否已存在
|
||||
const exists = await checkNameExists(name)
|
||||
return !exists
|
||||
},
|
||||
{ message: '名称已存在' }
|
||||
),
|
||||
})
|
||||
```
|
||||
|
||||
### Q: 如何手动刷新 Query 数据?
|
||||
|
||||
```typescript
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
// 方式1: 使缓存失效,触发重新获取
|
||||
queryClient.invalidateQueries({ queryKey: ['providers', appId] })
|
||||
|
||||
// 方式2: 直接刷新
|
||||
queryClient.refetchQueries({ queryKey: ['providers', appId] })
|
||||
|
||||
// 方式3: 更新缓存数据
|
||||
queryClient.setQueryData(['providers', appId], newData)
|
||||
```
|
||||
|
||||
### Q: 如何在组件外部使用 toast?
|
||||
|
||||
```typescript
|
||||
// 直接导入并使用即可
|
||||
import { toast } from 'sonner'
|
||||
|
||||
export const someUtil = () => {
|
||||
toast.success('工具函数中的通知')
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 调试技巧
|
||||
|
||||
### React Query DevTools
|
||||
|
||||
```typescript
|
||||
// 在 main.tsx 中添加
|
||||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
|
||||
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<App />
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
</QueryClientProvider>
|
||||
```
|
||||
|
||||
### 查看表单状态
|
||||
|
||||
```typescript
|
||||
const form = useForm()
|
||||
|
||||
// 在开发模式下打印表单状态
|
||||
console.log('Form values:', form.watch())
|
||||
console.log('Form errors:', form.formState.errors)
|
||||
console.log('Is valid:', form.formState.isValid)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 性能优化建议
|
||||
|
||||
### 1. 避免不必要的重渲染
|
||||
|
||||
```typescript
|
||||
// 使用 React.memo
|
||||
export const ProviderCard = React.memo(({ provider, onEdit }: Props) => {
|
||||
// ...
|
||||
})
|
||||
|
||||
// 或使用 useMemo
|
||||
const sortedProviders = useMemo(
|
||||
() => Object.values(providers).sort(...),
|
||||
[providers]
|
||||
)
|
||||
```
|
||||
|
||||
### 2. Query 配置优化
|
||||
|
||||
```typescript
|
||||
const { data } = useQuery({
|
||||
queryKey: ['providers', appId],
|
||||
queryFn: fetchProviders,
|
||||
staleTime: 1000 * 60 * 5, // 5分钟内不重新获取
|
||||
gcTime: 1000 * 60 * 10, // 10分钟后清除缓存
|
||||
})
|
||||
```
|
||||
|
||||
### 3. 表单性能优化
|
||||
|
||||
```typescript
|
||||
// 使用 mode 控制验证时机
|
||||
const form = useForm({
|
||||
mode: 'onBlur', // 失去焦点时验证
|
||||
// mode: 'onChange', // 每次输入都验证(较慢)
|
||||
// mode: 'onSubmit', // 提交时验证(最快)
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**提示**: 将此文档保存在浏览器书签或编辑器中,方便随时查阅!
|
||||
73
docs/TEST_DEVELOPMENT_PLAN.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# 前端测试开发计划
|
||||
|
||||
## 1. 背景与目标
|
||||
- **背景**:v3.5.0 起前端功能快速扩张(供应商管理、MCP、导入导出、端点测速、国际化),缺失系统化测试导致回归风险与人工验证成本攀升。
|
||||
- **目标**:在 3 个迭代内建立覆盖关键业务的自动化测试体系,形成稳定的手动冒烟流程,并将测试执行纳入 CI/CD。
|
||||
|
||||
## 2. 范围与优先级
|
||||
| 范围 | 内容 | 优先级 |
|
||||
| --- | --- | --- |
|
||||
| 供应商管理 | 列表、排序、预设/自定义表单、切换、复制、删除 | P0 |
|
||||
| 配置导入导出 | JSON 校验、备份、进度反馈、失败回滚 | P0 |
|
||||
| MCP 管理 | 列表、启停、模板、命令校验 | P1 |
|
||||
| 设置面板 | 主题/语言切换、目录设置、关于、更新检查 | P1 |
|
||||
| 端点速度测试 & 使用脚本 | 启动测试、状态指示、脚本保存 | P2 |
|
||||
| 国际化 | 中英切换、缺省文案回退 | P2 |
|
||||
|
||||
## 3. 测试分层策略
|
||||
- **单元测试(Vitest)**:纯函数与 Hook(`useProviderActions`、`useSettingsForm`、`useDragSort`、`useImportExport` 等)验证数据处理、错误分支、排序逻辑。
|
||||
- **组件测试(React Testing Library)**:关键组件(`ProviderList`、`AddProviderDialog`、`SettingsDialog`、`McpPanel`)模拟交互、校验、提示;结合 MSW 模拟 API。
|
||||
- **集成测试(App 级别)**:挂载 `App.tsx`,覆盖应用切换、编辑模式、导入导出回调、语言切换,验证状态同步与 toast 提示。
|
||||
- **端到端测试(Playwright)**:依赖 `pnpm dev:renderer`,串联供应商 CRUD、排序拖拽、MCP 启停、语言切换即时刷新、更新检查跳转。
|
||||
- **手动冒烟**:Tauri 桌面包 + dev server 双通道,验证托盘、系统权限、真实文件写入。
|
||||
|
||||
## 4. 环境与工具
|
||||
- 依赖:Node 18+、pnpm 8+、Vitest、React Testing Library、MSW、Playwright、Testing Library User Event、Playwright Trace Viewer。
|
||||
- 配置要点:
|
||||
- 在 `tsconfig` 中共享别名,Vitest 配合 `vite.config.mts`。
|
||||
- `setupTests.ts` 统一注册 MSW/RTL、自定义 matcher。
|
||||
- Playwright 使用多浏览器矩阵(Chromium 必选,WebKit 可选),并共享 `.env.test`。
|
||||
- Mock `@tauri-apps/api` 与 `providersApi`/`settingsApi`,隔离 Rust 层。
|
||||
|
||||
## 5. 自动化建设里程碑
|
||||
| 周期 | 目标 | 交付 |
|
||||
| --- | --- | --- |
|
||||
| Sprint 1 | Vitest 基础设施、核心 Hook 单测(P0) | `pnpm test:unit`、覆盖率报告、10+ 用例 |
|
||||
| Sprint 2 | 组件/集成测试、MSW Mock 层 | `pnpm test:component`、App 主流程用例 |
|
||||
| Sprint 3 | Playwright E2E、CI 接入 | `pnpm test:e2e`、CI job、冒烟脚本 |
|
||||
| 持续 | 回归用例补齐、视觉比对探索 | Playwright Trace、截图基线 |
|
||||
|
||||
## 6. 用例规划概览
|
||||
- **供应商管理**:新增(预设+自定义)、编辑校验、复制排序、切换失败回退、删除确认、使用脚本保存。
|
||||
- **导入导出**:成功、重复导入、校验失败、备份失败提示、导入后托盘刷新。
|
||||
- **MCP**:模板应用、协议切换(stdio/http)、命令校验、启停状态持久化。
|
||||
- **设置**:主题/语言即时生效、目录路径更新、更新检查按钮外链、关于信息渲染。
|
||||
- **端点速度测试**:触发测试、loading/成功/失败状态、指示器颜色、测速数据排序。
|
||||
- **国际化**:默认中文、切换英文后主界面/对话框文案变化、缺失 key fallback。
|
||||
|
||||
## 7. 数据与 Mock 策略
|
||||
- 在 `tests/fixtures/` 维护标准供应商、MCP、设置数据集。
|
||||
- 使用 MSW 拦截 `providersApi`、`settingsApi`、`providersApi.onSwitched` 等调用;提供延迟/错误注入接口以覆盖异常分支。
|
||||
- Playwright 端提供临时用户目录(`TMP_CC_SWITCH_HOME`)+ 伪配置文件,以验证真实文件交互路径。
|
||||
|
||||
## 8. 质量门禁与指标
|
||||
- 覆盖率目标:单元 ≥75%,分支 ≥70%,逐步提升至 80%+。
|
||||
- CI 阶段:`pnpm typecheck` → `pnpm format:check` → `pnpm test:unit` → `pnpm test:component` → `pnpm test:e2e`(可在 nightly 执行)。
|
||||
- 缺陷处理:修复前补充最小复现测试;E2E 冒烟必须陪跑重大功能发布。
|
||||
|
||||
## 9. 工作流与职责
|
||||
- **测试负责人**:前端工程师轮值;负责测试计划维护、PR 流水线健康。
|
||||
- **开发者职责**:提交功能需附新增/更新测试、列出手动验证步骤、如涉及 UI 提交截图。
|
||||
- **Code Review 检查**:测试覆盖说明、mock 合理性、易读性。
|
||||
|
||||
## 10. 风险与缓解
|
||||
| 风险 | 影响 | 缓解 |
|
||||
| --- | --- | --- |
|
||||
| Tauri API Mock 难度高 | 单测无法稳定 | 抽象 API 适配层 + MSW 统一模拟 |
|
||||
| Playwright 运行时间长 | CI 变慢 | 拆分冒烟/完整版,冒烟只跑关键路径 |
|
||||
| 国际化文案频繁变化 | 用例脆弱 | 优先断言 data-testid/结构,文案使用翻译 key |
|
||||
|
||||
## 11. 输出与维护
|
||||
- 文档维护者:前端团队;每个版本更新后检查测试覆盖清单。
|
||||
- 交付物:测试报告(CI artifact)、Playwright Trace、覆盖率摘要。
|
||||
- 复盘:每次发布后召开 30 分钟测试复盘,记录缺陷、补齐用例。
|
||||
9
docs/roadmap.md
Normal file
@@ -0,0 +1,9 @@
|
||||
- 自动升级自定义路径 ✅
|
||||
- win 绿色版报毒问题 ✅
|
||||
- mcp 管理器 ✅
|
||||
- i18n ✅
|
||||
- gemini cli
|
||||
- homebrew 支持
|
||||
- memory 管理
|
||||
- codex 更多预设供应商
|
||||
- 云同步
|
||||
56
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "cc-switch",
|
||||
"version": "3.0.0",
|
||||
"description": "Claude Code 供应商切换工具",
|
||||
"version": "3.6.0",
|
||||
"description": "Claude Code & Codex 供应商切换工具",
|
||||
"scripts": {
|
||||
"dev": "pnpm tauri dev",
|
||||
"build": "pnpm tauri build",
|
||||
@@ -10,24 +10,70 @@
|
||||
"build:renderer": "vite build",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,css,json}\"",
|
||||
"format:check": "prettier --check \"src/**/*.{js,jsx,ts,tsx,css,json}\""
|
||||
"format:check": "prettier --check \"src/**/*.{js,jsx,ts,tsx,css,json}\"",
|
||||
"test:unit": "vitest run",
|
||||
"test:unit:watch": "vitest watch"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Jason Young",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@tauri-apps/cli": "^2.8.0",
|
||||
"@testing-library/jest-dom": "^6.6.3",
|
||||
"@testing-library/react": "^16.0.1",
|
||||
"@testing-library/user-event": "^14.5.2",
|
||||
"@types/node": "^20.0.0",
|
||||
"@types/react": "^18.2.0",
|
||||
"@types/react-dom": "^18.2.0",
|
||||
"@vitejs/plugin-react": "^4.2.0",
|
||||
"cross-fetch": "^4.1.0",
|
||||
"jsdom": "^25.0.0",
|
||||
"msw": "^2.11.6",
|
||||
"prettier": "^3.6.2",
|
||||
"typescript": "^5.3.0",
|
||||
"vite": "^5.0.0"
|
||||
"vite": "^5.0.0",
|
||||
"vitest": "^2.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@codemirror/lang-javascript": "^6.2.4",
|
||||
"@codemirror/lang-json": "^6.0.2",
|
||||
"@codemirror/lint": "^6.8.5",
|
||||
"@codemirror/state": "^6.5.2",
|
||||
"@codemirror/theme-one-dark": "^6.1.3",
|
||||
"@codemirror/view": "^6.38.2",
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@hookform/resolvers": "^5.2.2",
|
||||
"@radix-ui/react-checkbox": "^1.3.3",
|
||||
"@radix-ui/react-dialog": "^1.1.15",
|
||||
"@radix-ui/react-dropdown-menu": "^2.1.16",
|
||||
"@radix-ui/react-label": "^2.1.7",
|
||||
"@radix-ui/react-select": "^2.2.6",
|
||||
"@radix-ui/react-slot": "^1.2.3",
|
||||
"@radix-ui/react-switch": "^1.2.6",
|
||||
"@radix-ui/react-tabs": "^1.1.13",
|
||||
"@tailwindcss/vite": "^4.1.13",
|
||||
"@tanstack/react-query": "^5.90.3",
|
||||
"@tauri-apps/api": "^2.8.0",
|
||||
"@tauri-apps/plugin-dialog": "^2.4.0",
|
||||
"@tauri-apps/plugin-process": "^2.0.0",
|
||||
"@tauri-apps/plugin-store": "^2.0.0",
|
||||
"@tauri-apps/plugin-updater": "^2.0.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"codemirror": "^6.0.2",
|
||||
"i18next": "^25.5.2",
|
||||
"jsonc-parser": "^3.2.1",
|
||||
"lucide-react": "^0.542.0",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
"react-dom": "^18.2.0",
|
||||
"react-hook-form": "^7.65.0",
|
||||
"react-i18next": "^16.0.0",
|
||||
"smol-toml": "^1.4.2",
|
||||
"sonner": "^2.0.7",
|
||||
"tailwind-merge": "^3.3.1",
|
||||
"tailwindcss": "^4.1.13",
|
||||
"zod": "^4.1.12"
|
||||
}
|
||||
}
|
||||
|
||||
3143
pnpm-lock.yaml
generated
4
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages: []
|
||||
|
||||
onlyBuiltDependencies:
|
||||
- '@tailwindcss/oxide'
|
||||
|
Before Width: | Height: | Size: 128 KiB |
|
Before Width: | Height: | Size: 194 KiB |
1563
src-tauri/Cargo.lock
generated
@@ -1,11 +1,11 @@
|
||||
[package]
|
||||
name = "cc-switch"
|
||||
version = "3.0.0"
|
||||
description = "Claude Code MCP 服务器配置管理工具"
|
||||
version = "3.6.0"
|
||||
description = "Claude Code & Codex 供应商配置管理工具"
|
||||
authors = ["Jason Young"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/jasonyoung/cc-switch"
|
||||
edition = "2024"
|
||||
repository = "https://github.com/farion1231/cc-switch"
|
||||
edition = "2021"
|
||||
rust-version = "1.85.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -14,6 +14,10 @@ rust-version = "1.85.0"
|
||||
name = "cc_switch_lib"
|
||||
crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
test-hooks = []
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2.4.0", features = [] }
|
||||
|
||||
@@ -21,11 +25,35 @@ tauri-build = { version = "2.4.0", features = [] }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
log = "0.4"
|
||||
tauri = { version = "2.8.2", features = [] }
|
||||
chrono = "0.4"
|
||||
tauri = { version = "2.8.2", features = ["tray-icon"] }
|
||||
tauri-plugin-log = "2"
|
||||
tauri-plugin-opener = "2"
|
||||
tauri-plugin-process = "2"
|
||||
tauri-plugin-updater = "2"
|
||||
tauri-plugin-dialog = "2"
|
||||
tauri-plugin-store = "2"
|
||||
dirs = "5.0"
|
||||
toml = "0.8"
|
||||
toml_edit = "0.22"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json"] }
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
|
||||
futures = "0.3"
|
||||
regex = "1.10"
|
||||
rquickjs = { version = "0.8", features = ["array-buffer", "classes"] }
|
||||
thiserror = "1.0"
|
||||
|
||||
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))'.dependencies]
|
||||
tauri-plugin-single-instance = "2"
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
objc2 = "0.5"
|
||||
objc2-app-kit = { version = "0.2", features = ["NSColor"] }
|
||||
|
||||
# Optimize release binary size to help reduce AppImage footprint
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = "thin"
|
||||
opt-level = "s"
|
||||
panic = "abort"
|
||||
strip = "symbols"
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"opener:default"
|
||||
"opener:default",
|
||||
"updater:default",
|
||||
"core:window:allow-set-skip-taskbar",
|
||||
"process:allow-restart",
|
||||
"dialog:default"
|
||||
]
|
||||
}
|
||||
|
||||
BIN
src-tauri/icons/tray/macos/statusTemplate.png
Normal file
|
After Width: | Height: | Size: 564 KiB |
BIN
src-tauri/icons/tray/macos/statusTemplate@2x.png
Normal file
|
After Width: | Height: | Size: 572 KiB |
176
src-tauri/src/app_config.rs
Normal file
@@ -0,0 +1,176 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// MCP 配置:单客户端维度(claude 或 codex 下的一组服务器)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct McpConfig {
|
||||
/// 以 id 为键的服务器定义(宽松 JSON 对象,包含 enabled/source 等 UI 辅助字段)
|
||||
#[serde(default)]
|
||||
pub servers: HashMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
/// MCP 根:按客户端分开维护(无历史兼容压力,直接以 v2 结构落地)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct McpRoot {
|
||||
#[serde(default)]
|
||||
pub claude: McpConfig,
|
||||
#[serde(default)]
|
||||
pub codex: McpConfig,
|
||||
}
|
||||
|
||||
use crate::config::{copy_file, get_app_config_dir, get_app_config_path, write_json_file};
|
||||
use crate::error::AppError;
|
||||
use crate::provider::ProviderManager;
|
||||
|
||||
/// 应用类型
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AppType {
|
||||
Claude,
|
||||
Codex,
|
||||
}
|
||||
|
||||
impl AppType {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
AppType::Claude => "claude",
|
||||
AppType::Codex => "codex",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for AppType {
|
||||
type Err = AppError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let normalized = s.trim().to_lowercase();
|
||||
match normalized.as_str() {
|
||||
"claude" => Ok(AppType::Claude),
|
||||
"codex" => Ok(AppType::Codex),
|
||||
other => Err(AppError::localized(
|
||||
"unsupported_app",
|
||||
format!("不支持的应用标识: '{other}'。可选值: claude, codex。"),
|
||||
format!("Unsupported app id: '{other}'. Allowed: claude, codex."),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 多应用配置结构(向后兼容)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MultiAppConfig {
|
||||
#[serde(default = "default_version")]
|
||||
pub version: u32,
|
||||
/// 应用管理器(claude/codex)
|
||||
#[serde(flatten)]
|
||||
pub apps: HashMap<String, ProviderManager>,
|
||||
/// MCP 配置(按客户端分治)
|
||||
#[serde(default)]
|
||||
pub mcp: McpRoot,
|
||||
}
|
||||
|
||||
fn default_version() -> u32 {
|
||||
2
|
||||
}
|
||||
|
||||
impl Default for MultiAppConfig {
|
||||
fn default() -> Self {
|
||||
let mut apps = HashMap::new();
|
||||
apps.insert("claude".to_string(), ProviderManager::default());
|
||||
apps.insert("codex".to_string(), ProviderManager::default());
|
||||
|
||||
Self {
|
||||
version: 2,
|
||||
apps,
|
||||
mcp: McpRoot::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MultiAppConfig {
|
||||
/// 从文件加载配置(仅支持 v2 结构)
|
||||
pub fn load() -> Result<Self, AppError> {
|
||||
let config_path = get_app_config_path();
|
||||
|
||||
if !config_path.exists() {
|
||||
log::info!("配置文件不存在,创建新的多应用配置");
|
||||
return Ok(Self::default());
|
||||
}
|
||||
|
||||
// 尝试读取文件
|
||||
let content =
|
||||
std::fs::read_to_string(&config_path).map_err(|e| AppError::io(&config_path, e))?;
|
||||
|
||||
// 先解析为 Value,以便严格判定是否为 v1 结构;
|
||||
// 满足:顶层同时包含 providers(object) + current(string),且不包含 version/apps/mcp 关键键,即视为 v1
|
||||
let value: serde_json::Value =
|
||||
serde_json::from_str(&content).map_err(|e| AppError::json(&config_path, e))?;
|
||||
let is_v1 = value.as_object().is_some_and(|map| {
|
||||
let has_providers = map.get("providers").map(|v| v.is_object()).unwrap_or(false);
|
||||
let has_current = map.get("current").map(|v| v.is_string()).unwrap_or(false);
|
||||
// v1 的充分必要条件:有 providers 和 current,且 apps 不存在(version/mcp 可能存在但不作为 v2 判据)
|
||||
let has_apps = map.contains_key("apps");
|
||||
has_providers && has_current && !has_apps
|
||||
});
|
||||
if is_v1 {
|
||||
return Err(AppError::localized(
|
||||
"config.unsupported_v1",
|
||||
"检测到旧版 v1 配置格式。当前版本已不再支持运行时自动迁移。\n\n解决方案:\n1. 安装 v3.2.x 版本进行一次性自动迁移\n2. 或手动编辑 ~/.cc-switch/config.json,将顶层结构调整为:\n {\"version\": 2, \"claude\": {...}, \"codex\": {...}, \"mcp\": {...}}\n\n",
|
||||
"Detected legacy v1 config. Runtime auto-migration is no longer supported.\n\nSolutions:\n1. Install v3.2.x for one-time auto-migration\n2. Or manually edit ~/.cc-switch/config.json to adjust the top-level structure:\n {\"version\": 2, \"claude\": {...}, \"codex\": {...}, \"mcp\": {...}}\n\n",
|
||||
));
|
||||
}
|
||||
|
||||
// 解析 v2 结构
|
||||
serde_json::from_value::<Self>(value).map_err(|e| AppError::json(&config_path, e))
|
||||
}
|
||||
|
||||
/// 保存配置到文件
|
||||
pub fn save(&self) -> Result<(), AppError> {
|
||||
let config_path = get_app_config_path();
|
||||
// 先备份旧版(若存在)到 ~/.cc-switch/config.json.bak,再写入新内容
|
||||
if config_path.exists() {
|
||||
let backup_path = get_app_config_dir().join("config.json.bak");
|
||||
if let Err(e) = copy_file(&config_path, &backup_path) {
|
||||
log::warn!("备份 config.json 到 .bak 失败: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
write_json_file(&config_path, self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取指定应用的管理器
|
||||
pub fn get_manager(&self, app: &AppType) -> Option<&ProviderManager> {
|
||||
self.apps.get(app.as_str())
|
||||
}
|
||||
|
||||
/// 获取指定应用的管理器(可变引用)
|
||||
pub fn get_manager_mut(&mut self, app: &AppType) -> Option<&mut ProviderManager> {
|
||||
self.apps.get_mut(app.as_str())
|
||||
}
|
||||
|
||||
/// 确保应用存在
|
||||
pub fn ensure_app(&mut self, app: &AppType) {
|
||||
if !self.apps.contains_key(app.as_str()) {
|
||||
self.apps
|
||||
.insert(app.as_str().to_string(), ProviderManager::default());
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取指定客户端的 MCP 配置(不可变引用)
|
||||
pub fn mcp_for(&self, app: &AppType) -> &McpConfig {
|
||||
match app {
|
||||
AppType::Claude => &self.mcp.claude,
|
||||
AppType::Codex => &self.mcp.codex,
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取指定客户端的 MCP 配置(可变引用)
|
||||
pub fn mcp_for_mut(&mut self, app: &AppType) -> &mut McpConfig {
|
||||
match app {
|
||||
AppType::Claude => &mut self.mcp.claude,
|
||||
AppType::Codex => &mut self.mcp.codex,
|
||||
}
|
||||
}
|
||||
}
|
||||
139
src-tauri/src/app_store.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
use serde_json::Value;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{OnceLock, RwLock};
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
/// Store 中的键名
|
||||
const STORE_KEY_APP_CONFIG_DIR: &str = "app_config_dir_override";
|
||||
|
||||
/// 缓存当前的 app_config_dir 覆盖路径,避免存储 AppHandle
|
||||
static APP_CONFIG_DIR_OVERRIDE: OnceLock<RwLock<Option<PathBuf>>> = OnceLock::new();
|
||||
|
||||
fn override_cache() -> &'static RwLock<Option<PathBuf>> {
|
||||
APP_CONFIG_DIR_OVERRIDE.get_or_init(|| RwLock::new(None))
|
||||
}
|
||||
|
||||
fn update_cached_override(value: Option<PathBuf>) {
|
||||
if let Ok(mut guard) = override_cache().write() {
|
||||
*guard = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取缓存中的 app_config_dir 覆盖路径
|
||||
pub fn get_app_config_dir_override() -> Option<PathBuf> {
|
||||
override_cache().read().ok()?.clone()
|
||||
}
|
||||
|
||||
fn read_override_from_store(app: &tauri::AppHandle) -> Option<PathBuf> {
|
||||
let store = match app.store_builder("app_paths.json").build() {
|
||||
Ok(store) => store,
|
||||
Err(e) => {
|
||||
log::warn!("无法创建 Store: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
match store.get(STORE_KEY_APP_CONFIG_DIR) {
|
||||
Some(Value::String(path_str)) => {
|
||||
let path_str = path_str.trim();
|
||||
if path_str.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let path = resolve_path(path_str);
|
||||
|
||||
if !path.exists() {
|
||||
log::warn!(
|
||||
"Store 中配置的 app_config_dir 不存在: {:?}\n\
|
||||
将使用默认路径。",
|
||||
path
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
log::info!("使用 Store 中的 app_config_dir: {:?}", path);
|
||||
Some(path)
|
||||
}
|
||||
Some(_) => {
|
||||
log::warn!(
|
||||
"Store 中的 {} 类型不正确,应为字符串",
|
||||
STORE_KEY_APP_CONFIG_DIR
|
||||
);
|
||||
None
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 从 Store 刷新 app_config_dir 覆盖值并更新缓存
|
||||
pub fn refresh_app_config_dir_override(app: &tauri::AppHandle) -> Option<PathBuf> {
|
||||
let value = read_override_from_store(app);
|
||||
update_cached_override(value.clone());
|
||||
value
|
||||
}
|
||||
|
||||
/// 写入 app_config_dir 到 Tauri Store
|
||||
pub fn set_app_config_dir_to_store(
|
||||
app: &tauri::AppHandle,
|
||||
path: Option<&str>,
|
||||
) -> Result<(), AppError> {
|
||||
let store = app
|
||||
.store_builder("app_paths.json")
|
||||
.build()
|
||||
.map_err(|e| AppError::Message(format!("创建 Store 失败: {}", e)))?;
|
||||
|
||||
match path {
|
||||
Some(p) => {
|
||||
let trimmed = p.trim();
|
||||
if !trimmed.is_empty() {
|
||||
store.set(STORE_KEY_APP_CONFIG_DIR, Value::String(trimmed.to_string()));
|
||||
log::info!("已将 app_config_dir 写入 Store: {}", trimmed);
|
||||
} else {
|
||||
store.delete(STORE_KEY_APP_CONFIG_DIR);
|
||||
log::info!("已从 Store 中删除 app_config_dir 配置");
|
||||
}
|
||||
}
|
||||
None => {
|
||||
store.delete(STORE_KEY_APP_CONFIG_DIR);
|
||||
log::info!("已从 Store 中删除 app_config_dir 配置");
|
||||
}
|
||||
}
|
||||
|
||||
store
|
||||
.save()
|
||||
.map_err(|e| AppError::Message(format!("保存 Store 失败: {}", e)))?;
|
||||
|
||||
refresh_app_config_dir_override(app);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 解析路径,支持 ~ 开头的相对路径
|
||||
fn resolve_path(raw: &str) -> PathBuf {
|
||||
if raw == "~" {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home;
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~/") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~\\") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
}
|
||||
|
||||
PathBuf::from(raw)
|
||||
}
|
||||
|
||||
/// 从旧的 settings.json 迁移 app_config_dir 到 Store
|
||||
pub fn migrate_app_config_dir_from_settings(app: &tauri::AppHandle) -> Result<(), AppError> {
|
||||
// app_config_dir 已从 settings.json 移除,此函数保留但不再执行迁移
|
||||
// 如果用户在旧版本设置过 app_config_dir,需要在 Store 中手动配置
|
||||
log::info!("app_config_dir 迁移功能已移除,请在设置中重新配置");
|
||||
|
||||
let _ = refresh_app_config_dir_override(app);
|
||||
Ok(())
|
||||
}
|
||||
286
src-tauri/src/claude_mcp.rs
Normal file
@@ -0,0 +1,286 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Map, Value};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::config::{atomic_write, get_claude_mcp_path, get_default_claude_mcp_path};
|
||||
use crate::error::AppError;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpStatus {
|
||||
pub user_config_path: String,
|
||||
pub user_config_exists: bool,
|
||||
pub server_count: usize,
|
||||
}
|
||||
|
||||
fn user_config_path() -> PathBuf {
|
||||
ensure_mcp_override_migrated();
|
||||
get_claude_mcp_path()
|
||||
}
|
||||
|
||||
fn ensure_mcp_override_migrated() {
|
||||
if crate::settings::get_claude_override_dir().is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
let new_path = get_claude_mcp_path();
|
||||
if new_path.exists() {
|
||||
return;
|
||||
}
|
||||
|
||||
let legacy_path = get_default_claude_mcp_path();
|
||||
if !legacy_path.exists() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(parent) = new_path.parent() {
|
||||
if let Err(err) = fs::create_dir_all(parent) {
|
||||
log::warn!("创建 MCP 目录失败: {}", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
match fs::copy(&legacy_path, &new_path) {
|
||||
Ok(_) => {
|
||||
log::info!(
|
||||
"已根据覆盖目录复制 MCP 配置: {} -> {}",
|
||||
legacy_path.display(),
|
||||
new_path.display()
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!(
|
||||
"复制 MCP 配置失败: {} -> {}: {}",
|
||||
legacy_path.display(),
|
||||
new_path.display(),
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_json_value(path: &Path) -> Result<Value, AppError> {
|
||||
if !path.exists() {
|
||||
return Ok(serde_json::json!({}));
|
||||
}
|
||||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||||
let value: Value = serde_json::from_str(&content).map_err(|e| AppError::json(path, e))?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn write_json_value(path: &Path, value: &Value) -> Result<(), AppError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
let json =
|
||||
serde_json::to_string_pretty(value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
atomic_write(path, json.as_bytes())
|
||||
}
|
||||
|
||||
pub fn get_mcp_status() -> Result<McpStatus, AppError> {
|
||||
let path = user_config_path();
|
||||
let (exists, count) = if path.exists() {
|
||||
let v = read_json_value(&path)?;
|
||||
let servers = v.get("mcpServers").and_then(|x| x.as_object());
|
||||
(true, servers.map(|m| m.len()).unwrap_or(0))
|
||||
} else {
|
||||
(false, 0)
|
||||
};
|
||||
|
||||
Ok(McpStatus {
|
||||
user_config_path: path.to_string_lossy().to_string(),
|
||||
user_config_exists: exists,
|
||||
server_count: count,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_mcp_json() -> Result<Option<String>, AppError> {
|
||||
let path = user_config_path();
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(Some(content))
|
||||
}
|
||||
|
||||
pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
// 基础字段校验(尽量宽松)
|
||||
if !spec.is_object() {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器定义必须为 JSON 对象".into(),
|
||||
));
|
||||
}
|
||||
let t_opt = spec.get("type").and_then(|x| x.as_str());
|
||||
let is_stdio = t_opt.map(|t| t == "stdio").unwrap_or(true); // 兼容缺省(按 stdio 处理)
|
||||
let is_http = t_opt.map(|t| t == "http").unwrap_or(false);
|
||||
if !(is_stdio || is_http) {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 type 必须是 'stdio' 或 'http'(或省略表示 stdio)".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// stdio 类型必须有 command
|
||||
if is_stdio {
|
||||
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if cmd.is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// http 类型必须有 url
|
||||
if is_http {
|
||||
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if url.is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let path = user_config_path();
|
||||
let mut root = if path.exists() {
|
||||
read_json_value(&path)?
|
||||
} else {
|
||||
serde_json::json!({})
|
||||
};
|
||||
|
||||
// 确保 mcpServers 对象存在
|
||||
{
|
||||
let obj = root
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| AppError::Config("mcp.json 根必须是对象".into()))?;
|
||||
if !obj.contains_key("mcpServers") {
|
||||
obj.insert("mcpServers".into(), serde_json::json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
let before = root.clone();
|
||||
if let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) {
|
||||
servers.insert(id.to_string(), spec);
|
||||
}
|
||||
|
||||
if before == root && path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
write_json_value(&path, &root)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn delete_mcp_server(id: &str) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
let path = user_config_path();
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
let mut root = read_json_value(&path)?;
|
||||
let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) else {
|
||||
return Ok(false);
|
||||
};
|
||||
let existed = servers.remove(id).is_some();
|
||||
if !existed {
|
||||
return Ok(false);
|
||||
}
|
||||
write_json_value(&path, &root)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn validate_command_in_path(cmd: &str) -> Result<bool, AppError> {
|
||||
if cmd.trim().is_empty() {
|
||||
return Ok(false);
|
||||
}
|
||||
// 如果包含路径分隔符,直接判断是否存在可执行文件
|
||||
if cmd.contains('/') || cmd.contains('\\') {
|
||||
return Ok(Path::new(cmd).exists());
|
||||
}
|
||||
|
||||
let path_var = env::var_os("PATH").unwrap_or_default();
|
||||
let paths = env::split_paths(&path_var);
|
||||
|
||||
#[cfg(windows)]
|
||||
let exts: Vec<String> = env::var("PATHEXT")
|
||||
.unwrap_or(".COM;.EXE;.BAT;.CMD".into())
|
||||
.split(';')
|
||||
.map(|s| s.trim().to_uppercase())
|
||||
.collect();
|
||||
|
||||
for p in paths {
|
||||
let candidate = p.join(cmd);
|
||||
if candidate.is_file() {
|
||||
return Ok(true);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
for ext in &exts {
|
||||
let cand = p.join(format!("{}{}", cmd, ext));
|
||||
if cand.is_file() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// 将给定的启用 MCP 服务器映射写入到用户级 ~/.claude.json 的 mcpServers 字段
|
||||
/// 仅覆盖 mcpServers,其他字段保持不变
|
||||
pub fn set_mcp_servers_map(
|
||||
servers: &std::collections::HashMap<String, Value>,
|
||||
) -> Result<(), AppError> {
|
||||
let path = user_config_path();
|
||||
let mut root = if path.exists() {
|
||||
read_json_value(&path)?
|
||||
} else {
|
||||
serde_json::json!({})
|
||||
};
|
||||
|
||||
// 构建 mcpServers 对象:移除 UI 辅助字段(enabled/source),仅保留实际 MCP 规范
|
||||
let mut out: Map<String, Value> = Map::new();
|
||||
for (id, spec) in servers.iter() {
|
||||
let mut obj = if let Some(map) = spec.as_object() {
|
||||
map.clone()
|
||||
} else {
|
||||
return Err(AppError::McpValidation(format!(
|
||||
"MCP 服务器 '{}' 不是对象",
|
||||
id
|
||||
)));
|
||||
};
|
||||
|
||||
if let Some(server_val) = obj.remove("server") {
|
||||
let server_obj = server_val.as_object().cloned().ok_or_else(|| {
|
||||
AppError::McpValidation(format!("MCP 服务器 '{}' server 字段不是对象", id))
|
||||
})?;
|
||||
obj = server_obj;
|
||||
}
|
||||
|
||||
obj.remove("enabled");
|
||||
obj.remove("source");
|
||||
obj.remove("id");
|
||||
obj.remove("name");
|
||||
obj.remove("description");
|
||||
obj.remove("tags");
|
||||
obj.remove("homepage");
|
||||
obj.remove("docs");
|
||||
|
||||
out.insert(id.clone(), Value::Object(obj));
|
||||
}
|
||||
|
||||
{
|
||||
let obj = root
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| AppError::Config("~/.claude.json 根必须是对象".into()))?;
|
||||
obj.insert("mcpServers".into(), Value::Object(out));
|
||||
}
|
||||
|
||||
write_json_value(&path, &root)?;
|
||||
Ok(())
|
||||
}
|
||||
131
src-tauri/src/claude_plugin.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
const CLAUDE_DIR: &str = ".claude";
|
||||
const CLAUDE_CONFIG_FILE: &str = "config.json";
|
||||
|
||||
fn claude_dir() -> Result<PathBuf, AppError> {
|
||||
// 优先使用设置中的覆盖目录
|
||||
if let Some(dir) = crate::settings::get_claude_override_dir() {
|
||||
return Ok(dir);
|
||||
}
|
||||
let home = dirs::home_dir().ok_or_else(|| AppError::Config("无法获取用户主目录".into()))?;
|
||||
Ok(home.join(CLAUDE_DIR))
|
||||
}
|
||||
|
||||
pub fn claude_config_path() -> Result<PathBuf, AppError> {
|
||||
Ok(claude_dir()?.join(CLAUDE_CONFIG_FILE))
|
||||
}
|
||||
|
||||
pub fn ensure_claude_dir_exists() -> Result<PathBuf, AppError> {
|
||||
let dir = claude_dir()?;
|
||||
if !dir.exists() {
|
||||
fs::create_dir_all(&dir).map_err(|e| AppError::io(&dir, e))?;
|
||||
}
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
pub fn read_claude_config() -> Result<Option<String>, AppError> {
|
||||
let path = claude_config_path()?;
|
||||
if path.exists() {
|
||||
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(Some(content))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_managed_config(content: &str) -> bool {
|
||||
match serde_json::from_str::<serde_json::Value>(content) {
|
||||
Ok(value) => value
|
||||
.get("primaryApiKey")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|val| val == "any")
|
||||
.unwrap_or(false),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_claude_config() -> Result<bool, AppError> {
|
||||
// 增量写入:仅设置 primaryApiKey = "any",保留其它字段
|
||||
let path = claude_config_path()?;
|
||||
ensure_claude_dir_exists()?;
|
||||
|
||||
// 尝试读取并解析为对象
|
||||
let mut obj = match read_claude_config()? {
|
||||
Some(existing) => match serde_json::from_str::<serde_json::Value>(&existing) {
|
||||
Ok(serde_json::Value::Object(map)) => serde_json::Value::Object(map),
|
||||
_ => serde_json::json!({}),
|
||||
},
|
||||
None => serde_json::json!({}),
|
||||
};
|
||||
|
||||
let mut changed = false;
|
||||
if let Some(map) = obj.as_object_mut() {
|
||||
let cur = map
|
||||
.get("primaryApiKey")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
if cur != "any" {
|
||||
map.insert(
|
||||
"primaryApiKey".to_string(),
|
||||
serde_json::Value::String("any".to_string()),
|
||||
);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if changed || !path.exists() {
|
||||
let serialized = serde_json::to_string_pretty(&obj)
|
||||
.map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
fs::write(&path, format!("{}\n", serialized)).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_claude_config() -> Result<bool, AppError> {
|
||||
let path = claude_config_path()?;
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let content = match read_claude_config()? {
|
||||
Some(content) => content,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
let mut value = match serde_json::from_str::<serde_json::Value>(&content) {
|
||||
Ok(value) => value,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
|
||||
let obj = match value.as_object_mut() {
|
||||
Some(obj) => obj,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
if obj.remove("primaryApiKey").is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let serialized =
|
||||
serde_json::to_string_pretty(&value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
fs::write(&path, format!("{}\n", serialized)).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn claude_config_status() -> Result<(bool, PathBuf), AppError> {
|
||||
let path = claude_config_path()?;
|
||||
Ok((path.exists(), path))
|
||||
}
|
||||
|
||||
pub fn is_claude_config_applied() -> Result<bool, AppError> {
|
||||
match read_claude_config()? {
|
||||
Some(content) => Ok(is_managed_config(&content)),
|
||||
None => Ok(false),
|
||||
}
|
||||
}
|
||||
134
src-tauri/src/codex_config.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
// unused imports removed
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::{
|
||||
atomic_write, delete_file, sanitize_provider_name, write_json_file, write_text_file,
|
||||
};
|
||||
use crate::error::AppError;
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// 获取 Codex 配置目录路径
|
||||
pub fn get_codex_config_dir() -> PathBuf {
|
||||
if let Some(custom) = crate::settings::get_codex_override_dir() {
|
||||
return custom;
|
||||
}
|
||||
|
||||
dirs::home_dir().expect("无法获取用户主目录").join(".codex")
|
||||
}
|
||||
|
||||
/// 获取 Codex auth.json 路径
|
||||
pub fn get_codex_auth_path() -> PathBuf {
|
||||
get_codex_config_dir().join("auth.json")
|
||||
}
|
||||
|
||||
/// 获取 Codex config.toml 路径
|
||||
pub fn get_codex_config_path() -> PathBuf {
|
||||
get_codex_config_dir().join("config.toml")
|
||||
}
|
||||
|
||||
/// 获取 Codex 供应商配置文件路径
|
||||
pub fn get_codex_provider_paths(
|
||||
provider_id: &str,
|
||||
provider_name: Option<&str>,
|
||||
) -> (PathBuf, PathBuf) {
|
||||
let base_name = provider_name
|
||||
.map(sanitize_provider_name)
|
||||
.unwrap_or_else(|| sanitize_provider_name(provider_id));
|
||||
|
||||
let auth_path = get_codex_config_dir().join(format!("auth-{}.json", base_name));
|
||||
let config_path = get_codex_config_dir().join(format!("config-{}.toml", base_name));
|
||||
|
||||
(auth_path, config_path)
|
||||
}
|
||||
|
||||
/// 删除 Codex 供应商配置文件
|
||||
pub fn delete_codex_provider_config(
|
||||
provider_id: &str,
|
||||
provider_name: &str,
|
||||
) -> Result<(), AppError> {
|
||||
let (auth_path, config_path) = get_codex_provider_paths(provider_id, Some(provider_name));
|
||||
|
||||
delete_file(&auth_path).ok();
|
||||
delete_file(&config_path).ok();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步
|
||||
pub fn write_codex_live_atomic(
|
||||
auth: &Value,
|
||||
config_text_opt: Option<&str>,
|
||||
) -> Result<(), AppError> {
|
||||
let auth_path = get_codex_auth_path();
|
||||
let config_path = get_codex_config_path();
|
||||
|
||||
if let Some(parent) = auth_path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
|
||||
// 读取旧内容用于回滚
|
||||
let old_auth = if auth_path.exists() {
|
||||
Some(fs::read(&auth_path).map_err(|e| AppError::io(&auth_path, e))?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let _old_config = if config_path.exists() {
|
||||
Some(fs::read(&config_path).map_err(|e| AppError::io(&config_path, e))?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 准备写入内容
|
||||
let cfg_text = match config_text_opt {
|
||||
Some(s) => s.to_string(),
|
||||
None => String::new(),
|
||||
};
|
||||
if !cfg_text.trim().is_empty() {
|
||||
toml::from_str::<toml::Table>(&cfg_text).map_err(|e| AppError::toml(&config_path, e))?;
|
||||
}
|
||||
|
||||
// 第一步:写 auth.json
|
||||
write_json_file(&auth_path, auth)?;
|
||||
|
||||
// 第二步:写 config.toml(失败则回滚 auth.json)
|
||||
if let Err(e) = write_text_file(&config_path, &cfg_text) {
|
||||
// 回滚 auth.json
|
||||
if let Some(bytes) = old_auth {
|
||||
let _ = atomic_write(&auth_path, &bytes);
|
||||
} else {
|
||||
let _ = delete_file(&auth_path);
|
||||
}
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 读取 `~/.codex/config.toml`,若不存在返回空字符串
|
||||
pub fn read_codex_config_text() -> Result<String, AppError> {
|
||||
let path = get_codex_config_path();
|
||||
if path.exists() {
|
||||
std::fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))
|
||||
} else {
|
||||
Ok(String::new())
|
||||
}
|
||||
}
|
||||
|
||||
/// 对非空的 TOML 文本进行语法校验
|
||||
pub fn validate_config_toml(text: &str) -> Result<(), AppError> {
|
||||
if text.trim().is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
toml::from_str::<toml::Table>(text)
|
||||
.map(|_| ())
|
||||
.map_err(|e| AppError::toml(Path::new("config.toml"), e))
|
||||
}
|
||||
|
||||
/// 读取并校验 `~/.codex/config.toml`,返回文本(可能为空)
|
||||
pub fn read_and_validate_codex_config_text() -> Result<String, AppError> {
|
||||
let s = read_codex_config_text()?;
|
||||
validate_config_toml(&s)?;
|
||||
Ok(s)
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use tauri::State;
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
use crate::config::{ConfigStatus, get_claude_settings_path, import_current_config_as_default};
|
||||
use crate::provider::Provider;
|
||||
use crate::store::AppState;
|
||||
|
||||
/// 获取所有供应商
|
||||
#[tauri::command]
|
||||
pub async fn get_providers(
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<HashMap<String, Provider>, String> {
|
||||
let manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
Ok(manager.get_all_providers().clone())
|
||||
}
|
||||
|
||||
/// 获取当前供应商ID
|
||||
#[tauri::command]
|
||||
pub async fn get_current_provider(state: State<'_, AppState>) -> Result<String, String> {
|
||||
let manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
Ok(manager.current.clone())
|
||||
}
|
||||
|
||||
/// 添加供应商
|
||||
#[tauri::command]
|
||||
pub async fn add_provider(state: State<'_, AppState>, provider: Provider) -> Result<bool, String> {
|
||||
let mut manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
manager.add_provider(provider)?;
|
||||
|
||||
// 保存配置
|
||||
drop(manager); // 释放锁
|
||||
state.save()?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 更新供应商
|
||||
#[tauri::command]
|
||||
pub async fn update_provider(
|
||||
state: State<'_, AppState>,
|
||||
provider: Provider,
|
||||
) -> Result<bool, String> {
|
||||
let mut manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
manager.update_provider(provider)?;
|
||||
|
||||
// 保存配置
|
||||
drop(manager); // 释放锁
|
||||
state.save()?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 删除供应商
|
||||
#[tauri::command]
|
||||
pub async fn delete_provider(state: State<'_, AppState>, id: String) -> Result<bool, String> {
|
||||
let mut manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
manager.delete_provider(&id)?;
|
||||
|
||||
// 保存配置
|
||||
drop(manager); // 释放锁
|
||||
state.save()?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 切换供应商
|
||||
#[tauri::command]
|
||||
pub async fn switch_provider(state: State<'_, AppState>, id: String) -> Result<bool, String> {
|
||||
let mut manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
manager.switch_provider(&id)?;
|
||||
|
||||
// 保存配置
|
||||
drop(manager); // 释放锁
|
||||
state.save()?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 导入当前配置为默认供应商
|
||||
#[tauri::command]
|
||||
pub async fn import_default_config(state: State<'_, AppState>) -> Result<bool, String> {
|
||||
// 若已存在 default 供应商,则直接返回,避免重复导入
|
||||
{
|
||||
let manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
if manager.get_all_providers().contains_key("default") {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
// 导入配置
|
||||
let settings_config = import_current_config_as_default()?;
|
||||
|
||||
// 创建默认供应商
|
||||
let provider = Provider::with_id(
|
||||
"default".to_string(),
|
||||
"default".to_string(),
|
||||
settings_config,
|
||||
None,
|
||||
);
|
||||
|
||||
// 添加到管理器
|
||||
let mut manager = state
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
|
||||
manager.add_provider(provider)?;
|
||||
|
||||
// 如果没有当前供应商,设置为 default
|
||||
if manager.current.is_empty() {
|
||||
manager.current = "default".to_string();
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
drop(manager); // 释放锁
|
||||
state.save()?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 获取 Claude Code 配置状态
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_config_status() -> Result<ConfigStatus, String> {
|
||||
Ok(crate::config::get_claude_config_status())
|
||||
}
|
||||
|
||||
/// 获取 Claude Code 配置文件路径
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_code_config_path() -> Result<String, String> {
|
||||
Ok(get_claude_settings_path().to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// 打开配置文件夹
|
||||
#[tauri::command]
|
||||
pub async fn open_config_folder(app: tauri::AppHandle) -> Result<bool, String> {
|
||||
let config_dir = crate::config::get_claude_config_dir();
|
||||
|
||||
// 确保目录存在
|
||||
if !config_dir.exists() {
|
||||
std::fs::create_dir_all(&config_dir).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
}
|
||||
|
||||
// 使用 opener 插件打开文件夹
|
||||
app.opener()
|
||||
.open_path(config_dir.to_string_lossy().to_string(), None::<String>)
|
||||
.map_err(|e| format!("打开文件夹失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 打开外部链接
|
||||
#[tauri::command]
|
||||
pub async fn open_external(app: tauri::AppHandle, url: String) -> Result<bool, String> {
|
||||
// 规范化 URL,缺少协议时默认加 https://
|
||||
let url = if url.starts_with("http://") || url.starts_with("https://") {
|
||||
url
|
||||
} else {
|
||||
format!("https://{}", url)
|
||||
};
|
||||
|
||||
// 使用 opener 插件打开链接
|
||||
app.opener()
|
||||
.open_url(&url, None::<String>)
|
||||
.map_err(|e| format!("打开链接失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
126
src-tauri/src/commands/config.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use tauri::AppHandle;
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
use crate::app_config::AppType;
|
||||
use crate::codex_config;
|
||||
use crate::config::{self, get_claude_settings_path, ConfigStatus};
|
||||
|
||||
/// 获取 Claude Code 配置状态
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_config_status() -> Result<ConfigStatus, String> {
|
||||
Ok(config::get_claude_config_status())
|
||||
}
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_config_status(app: String) -> Result<ConfigStatus, String> {
|
||||
match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||||
AppType::Claude => Ok(config::get_claude_config_status()),
|
||||
AppType::Codex => {
|
||||
let auth_path = codex_config::get_codex_auth_path();
|
||||
let exists = auth_path.exists();
|
||||
let path = codex_config::get_codex_config_dir()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
Ok(ConfigStatus { exists, path })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取 Claude Code 配置文件路径
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_code_config_path() -> Result<String, String> {
|
||||
Ok(get_claude_settings_path().to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// 获取当前生效的配置目录
|
||||
#[tauri::command]
|
||||
pub async fn get_config_dir(app: String) -> Result<String, String> {
|
||||
let dir = match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||||
AppType::Claude => config::get_claude_config_dir(),
|
||||
AppType::Codex => codex_config::get_codex_config_dir(),
|
||||
};
|
||||
|
||||
Ok(dir.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// 打开配置文件夹
|
||||
#[tauri::command]
|
||||
pub async fn open_config_folder(handle: AppHandle, app: String) -> Result<bool, String> {
|
||||
let config_dir = match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||||
AppType::Claude => config::get_claude_config_dir(),
|
||||
AppType::Codex => codex_config::get_codex_config_dir(),
|
||||
};
|
||||
|
||||
if !config_dir.exists() {
|
||||
std::fs::create_dir_all(&config_dir).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
}
|
||||
|
||||
handle
|
||||
.opener()
|
||||
.open_path(config_dir.to_string_lossy().to_string(), None::<String>)
|
||||
.map_err(|e| format!("打开文件夹失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 弹出系统目录选择器并返回用户选择的路径
|
||||
#[tauri::command]
|
||||
pub async fn pick_directory(
|
||||
app: AppHandle,
|
||||
#[allow(non_snake_case)] defaultPath: Option<String>,
|
||||
) -> Result<Option<String>, String> {
|
||||
let initial = defaultPath
|
||||
.map(|p| p.trim().to_string())
|
||||
.filter(|p| !p.is_empty());
|
||||
|
||||
let result = tauri::async_runtime::spawn_blocking(move || {
|
||||
let mut builder = app.dialog().file();
|
||||
if let Some(path) = initial {
|
||||
builder = builder.set_directory(path);
|
||||
}
|
||||
builder.blocking_pick_folder()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("弹出目录选择器失败: {}", e))?;
|
||||
|
||||
match result {
|
||||
Some(file_path) => {
|
||||
let resolved = file_path
|
||||
.simplified()
|
||||
.into_path()
|
||||
.map_err(|e| format!("解析选择的目录失败: {}", e))?;
|
||||
Ok(Some(resolved.to_string_lossy().to_string()))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取应用配置文件路径
|
||||
#[tauri::command]
|
||||
pub async fn get_app_config_path() -> Result<String, String> {
|
||||
let config_path = config::get_app_config_path();
|
||||
Ok(config_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// 打开应用配置文件夹
|
||||
#[tauri::command]
|
||||
pub async fn open_app_config_folder(handle: AppHandle) -> Result<bool, String> {
|
||||
let config_dir = config::get_app_config_dir();
|
||||
|
||||
if !config_dir.exists() {
|
||||
std::fs::create_dir_all(&config_dir).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
}
|
||||
|
||||
handle
|
||||
.opener()
|
||||
.open_path(config_dir.to_string_lossy().to_string(), None::<String>)
|
||||
.map_err(|e| format!("打开文件夹失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
106
src-tauri/src/commands/import_export.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use serde_json::{json, Value};
|
||||
use std::path::PathBuf;
|
||||
use tauri::State;
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::services::ConfigService;
|
||||
use crate::store::AppState;
|
||||
|
||||
/// 导出配置文件
|
||||
#[tauri::command]
|
||||
pub async fn export_config_to_file(
|
||||
#[allow(non_snake_case)] filePath: String,
|
||||
) -> Result<Value, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let target_path = PathBuf::from(&filePath);
|
||||
ConfigService::export_config_to_path(&target_path)?;
|
||||
Ok::<_, AppError>(json!({
|
||||
"success": true,
|
||||
"message": "Configuration exported successfully",
|
||||
"filePath": filePath
|
||||
}))
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("导出配置失败: {}", e))?
|
||||
.map_err(|e: AppError| e.to_string())
|
||||
}
|
||||
|
||||
/// 从文件导入配置
|
||||
#[tauri::command]
|
||||
pub async fn import_config_from_file(
|
||||
#[allow(non_snake_case)] filePath: String,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Value, String> {
|
||||
let (new_config, backup_id) = tauri::async_runtime::spawn_blocking(move || {
|
||||
let path_buf = PathBuf::from(&filePath);
|
||||
ConfigService::load_config_for_import(&path_buf)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("导入配置失败: {}", e))?
|
||||
.map_err(|e: AppError| e.to_string())?;
|
||||
|
||||
{
|
||||
let mut guard = state
|
||||
.config
|
||||
.write()
|
||||
.map_err(|e| AppError::from(e).to_string())?;
|
||||
*guard = new_config;
|
||||
}
|
||||
|
||||
Ok(json!({
|
||||
"success": true,
|
||||
"message": "Configuration imported successfully",
|
||||
"backupId": backup_id
|
||||
}))
|
||||
}
|
||||
|
||||
/// 同步当前供应商配置到对应的 live 文件
|
||||
#[tauri::command]
|
||||
pub async fn sync_current_providers_live(state: State<'_, AppState>) -> Result<Value, String> {
|
||||
{
|
||||
let mut config_state = state
|
||||
.config
|
||||
.write()
|
||||
.map_err(|e| AppError::from(e).to_string())?;
|
||||
ConfigService::sync_current_providers_to_live(&mut config_state)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
Ok(json!({
|
||||
"success": true,
|
||||
"message": "Live configuration synchronized"
|
||||
}))
|
||||
}
|
||||
|
||||
/// 保存文件对话框
|
||||
#[tauri::command]
|
||||
pub async fn save_file_dialog<R: tauri::Runtime>(
|
||||
app: tauri::AppHandle<R>,
|
||||
#[allow(non_snake_case)] defaultName: String,
|
||||
) -> Result<Option<String>, String> {
|
||||
let dialog = app.dialog();
|
||||
let result = dialog
|
||||
.file()
|
||||
.add_filter("JSON", &["json"])
|
||||
.set_file_name(&defaultName)
|
||||
.blocking_save_file();
|
||||
|
||||
Ok(result.map(|p| p.to_string()))
|
||||
}
|
||||
|
||||
/// 打开文件对话框
|
||||
#[tauri::command]
|
||||
pub async fn open_file_dialog<R: tauri::Runtime>(
|
||||
app: tauri::AppHandle<R>,
|
||||
) -> Result<Option<String>, String> {
|
||||
let dialog = app.dialog();
|
||||
let result = dialog
|
||||
.file()
|
||||
.add_filter("JSON", &["json"])
|
||||
.blocking_pick_file();
|
||||
|
||||
Ok(result.map(|p| p.to_string()))
|
||||
}
|
||||
131
src-tauri/src/commands/mcp.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::State;
|
||||
|
||||
use crate::app_config::AppType;
|
||||
use crate::claude_mcp;
|
||||
use crate::services::McpService;
|
||||
use crate::store::AppState;
|
||||
|
||||
/// 获取 Claude MCP 状态
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_mcp_status() -> Result<claude_mcp::McpStatus, String> {
|
||||
claude_mcp::get_mcp_status().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 读取 mcp.json 文本内容
|
||||
#[tauri::command]
|
||||
pub async fn read_claude_mcp_config() -> Result<Option<String>, String> {
|
||||
claude_mcp::read_mcp_json().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 新增或更新一个 MCP 服务器条目
|
||||
#[tauri::command]
|
||||
pub async fn upsert_claude_mcp_server(id: String, spec: serde_json::Value) -> Result<bool, String> {
|
||||
claude_mcp::upsert_mcp_server(&id, spec).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 删除一个 MCP 服务器条目
|
||||
#[tauri::command]
|
||||
pub async fn delete_claude_mcp_server(id: String) -> Result<bool, String> {
|
||||
claude_mcp::delete_mcp_server(&id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 校验命令是否在 PATH 中可用(不执行)
|
||||
#[tauri::command]
|
||||
pub async fn validate_mcp_command(cmd: String) -> Result<bool, String> {
|
||||
claude_mcp::validate_command_in_path(&cmd).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct McpConfigResponse {
|
||||
pub config_path: String,
|
||||
pub servers: HashMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
/// 获取 MCP 配置(来自 ~/.cc-switch/config.json)
|
||||
use std::str::FromStr;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_mcp_config(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
) -> Result<McpConfigResponse, String> {
|
||||
let config_path = crate::config::get_app_config_path()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let app_ty = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
let servers = McpService::get_servers(&state, app_ty).map_err(|e| e.to_string())?;
|
||||
Ok(McpConfigResponse {
|
||||
config_path,
|
||||
servers,
|
||||
})
|
||||
}
|
||||
|
||||
/// 在 config.json 中新增或更新一个 MCP 服务器定义
|
||||
#[tauri::command]
|
||||
pub async fn upsert_mcp_server_in_config(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
id: String,
|
||||
spec: serde_json::Value,
|
||||
sync_other_side: Option<bool>,
|
||||
) -> Result<bool, String> {
|
||||
let app_ty = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
McpService::upsert_server(&state, app_ty, &id, spec, sync_other_side.unwrap_or(false))
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 在 config.json 中删除一个 MCP 服务器定义
|
||||
#[tauri::command]
|
||||
pub async fn delete_mcp_server_in_config(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
id: String,
|
||||
) -> Result<bool, String> {
|
||||
let app_ty = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
McpService::delete_server(&state, app_ty, &id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 设置启用状态并同步到客户端配置
|
||||
#[tauri::command]
|
||||
pub async fn set_mcp_enabled(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
id: String,
|
||||
enabled: bool,
|
||||
) -> Result<bool, String> {
|
||||
let app_ty = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
McpService::set_enabled(&state, app_ty, &id, enabled).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 手动同步:将启用的 MCP 投影到 ~/.claude.json
|
||||
#[tauri::command]
|
||||
pub async fn sync_enabled_mcp_to_claude(state: State<'_, AppState>) -> Result<bool, String> {
|
||||
McpService::sync_enabled(&state, AppType::Claude)
|
||||
.map(|_| true)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 手动同步:将启用的 MCP 投影到 ~/.codex/config.toml
|
||||
#[tauri::command]
|
||||
pub async fn sync_enabled_mcp_to_codex(state: State<'_, AppState>) -> Result<bool, String> {
|
||||
McpService::sync_enabled(&state, AppType::Codex)
|
||||
.map(|_| true)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 从 ~/.claude.json 导入 MCP 定义到 config.json
|
||||
#[tauri::command]
|
||||
pub async fn import_mcp_from_claude(state: State<'_, AppState>) -> Result<usize, String> {
|
||||
McpService::import_from_claude(&state).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 从 ~/.codex/config.toml 导入 MCP 定义到 config.json
|
||||
#[tauri::command]
|
||||
pub async fn import_mcp_from_codex(state: State<'_, AppState>) -> Result<usize, String> {
|
||||
McpService::import_from_codex(&state).map_err(|e| e.to_string())
|
||||
}
|
||||
53
src-tauri/src/commands/misc.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::init_status::InitErrorPayload;
|
||||
use tauri::AppHandle;
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
/// 打开外部链接
|
||||
#[tauri::command]
|
||||
pub async fn open_external(app: AppHandle, url: String) -> Result<bool, String> {
|
||||
let url = if url.starts_with("http://") || url.starts_with("https://") {
|
||||
url
|
||||
} else {
|
||||
format!("https://{}", url)
|
||||
};
|
||||
|
||||
app.opener()
|
||||
.open_url(&url, None::<String>)
|
||||
.map_err(|e| format!("打开链接失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 检查更新
|
||||
#[tauri::command]
|
||||
pub async fn check_for_updates(handle: AppHandle) -> Result<bool, String> {
|
||||
handle
|
||||
.opener()
|
||||
.open_url(
|
||||
"https://github.com/farion1231/cc-switch/releases/latest",
|
||||
None::<String>,
|
||||
)
|
||||
.map_err(|e| format!("打开更新页面失败: {}", e))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 判断是否为便携版(绿色版)运行
|
||||
#[tauri::command]
|
||||
pub async fn is_portable_mode() -> Result<bool, String> {
|
||||
let exe_path = std::env::current_exe().map_err(|e| format!("获取可执行路径失败: {}", e))?;
|
||||
if let Some(dir) = exe_path.parent() {
|
||||
Ok(dir.join("portable.ini").is_file())
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取应用启动阶段的初始化错误(若有)。
|
||||
/// 用于前端在早期主动拉取,避免事件订阅竞态导致的提示缺失。
|
||||
#[tauri::command]
|
||||
pub async fn get_init_error() -> Result<Option<InitErrorPayload>, String> {
|
||||
Ok(crate::init_status::get_init_error())
|
||||
}
|
||||
17
src-tauri/src/commands/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
mod config;
|
||||
mod import_export;
|
||||
mod mcp;
|
||||
mod misc;
|
||||
mod plugin;
|
||||
mod provider;
|
||||
mod settings;
|
||||
|
||||
pub use config::*;
|
||||
pub use import_export::*;
|
||||
pub use mcp::*;
|
||||
pub use misc::*;
|
||||
pub use plugin::*;
|
||||
pub use provider::*;
|
||||
pub use settings::*;
|
||||
36
src-tauri/src/commands/plugin.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::config::ConfigStatus;
|
||||
|
||||
/// Claude 插件:获取 ~/.claude/config.json 状态
|
||||
#[tauri::command]
|
||||
pub async fn get_claude_plugin_status() -> Result<ConfigStatus, String> {
|
||||
crate::claude_plugin::claude_config_status()
|
||||
.map(|(exists, path)| ConfigStatus {
|
||||
exists,
|
||||
path: path.to_string_lossy().to_string(),
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Claude 插件:读取配置内容(若不存在返回 Ok(None))
|
||||
#[tauri::command]
|
||||
pub async fn read_claude_plugin_config() -> Result<Option<String>, String> {
|
||||
crate::claude_plugin::read_claude_config().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Claude 插件:写入/清除固定配置
|
||||
#[tauri::command]
|
||||
pub async fn apply_claude_plugin_config(official: bool) -> Result<bool, String> {
|
||||
if official {
|
||||
crate::claude_plugin::clear_claude_config().map_err(|e| e.to_string())
|
||||
} else {
|
||||
crate::claude_plugin::write_claude_config().map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Claude 插件:检测是否已写入目标配置
|
||||
#[tauri::command]
|
||||
pub async fn is_claude_plugin_applied() -> Result<bool, String> {
|
||||
crate::claude_plugin::is_claude_config_applied().map_err(|e| e.to_string())
|
||||
}
|
||||
228
src-tauri/src/commands/provider.rs
Normal file
@@ -0,0 +1,228 @@
|
||||
use std::collections::HashMap;
|
||||
use tauri::State;
|
||||
|
||||
use crate::app_config::AppType;
|
||||
use crate::error::AppError;
|
||||
use crate::provider::Provider;
|
||||
use crate::services::{EndpointLatency, ProviderService, ProviderSortUpdate, SpeedtestService};
|
||||
use crate::store::AppState;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// 获取所有供应商
|
||||
#[tauri::command]
|
||||
pub fn get_providers(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
) -> Result<HashMap<String, Provider>, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::list(state.inner(), app_type).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 获取当前供应商ID
|
||||
#[tauri::command]
|
||||
pub fn get_current_provider(state: State<'_, AppState>, app: String) -> Result<String, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::current(state.inner(), app_type).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 添加供应商
|
||||
#[tauri::command]
|
||||
pub fn add_provider(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
provider: Provider,
|
||||
) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::add(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 更新供应商
|
||||
#[tauri::command]
|
||||
pub fn update_provider(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
provider: Provider,
|
||||
) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::update(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 删除供应商
|
||||
#[tauri::command]
|
||||
pub fn delete_provider(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
id: String,
|
||||
) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::delete(state.inner(), app_type, &id)
|
||||
.map(|_| true)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 切换供应商
|
||||
fn switch_provider_internal(state: &AppState, app_type: AppType, id: &str) -> Result<(), AppError> {
|
||||
ProviderService::switch(state, app_type, id)
|
||||
}
|
||||
|
||||
#[cfg_attr(not(feature = "test-hooks"), doc(hidden))]
|
||||
pub fn switch_provider_test_hook(
|
||||
state: &AppState,
|
||||
app_type: AppType,
|
||||
id: &str,
|
||||
) -> Result<(), AppError> {
|
||||
switch_provider_internal(state, app_type, id)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn switch_provider(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
id: String,
|
||||
) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
switch_provider_internal(&state, app_type, &id)
|
||||
.map(|_| true)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn import_default_config_internal(state: &AppState, app_type: AppType) -> Result<(), AppError> {
|
||||
ProviderService::import_default_config(state, app_type)
|
||||
}
|
||||
|
||||
#[cfg_attr(not(feature = "test-hooks"), doc(hidden))]
|
||||
pub fn import_default_config_test_hook(
|
||||
state: &AppState,
|
||||
app_type: AppType,
|
||||
) -> Result<(), AppError> {
|
||||
import_default_config_internal(state, app_type)
|
||||
}
|
||||
|
||||
/// 导入当前配置为默认供应商
|
||||
#[tauri::command]
|
||||
pub fn import_default_config(state: State<'_, AppState>, app: String) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
import_default_config_internal(&state, app_type)
|
||||
.map(|_| true)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// 查询供应商用量
|
||||
#[allow(non_snake_case)]
|
||||
#[tauri::command]
|
||||
pub async fn queryProviderUsage(
|
||||
state: State<'_, AppState>,
|
||||
#[allow(non_snake_case)] providerId: String, // 使用 camelCase 匹配前端
|
||||
app: String,
|
||||
) -> Result<crate::provider::UsageResult, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::query_usage(state.inner(), app_type, &providerId)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 测试用量脚本(使用当前编辑器中的脚本,不保存)
|
||||
#[allow(non_snake_case)]
|
||||
#[tauri::command]
|
||||
pub async fn testUsageScript(
|
||||
state: State<'_, AppState>,
|
||||
#[allow(non_snake_case)] providerId: String,
|
||||
app: String,
|
||||
#[allow(non_snake_case)] scriptCode: String,
|
||||
timeout: Option<u64>,
|
||||
#[allow(non_snake_case)] accessToken: Option<String>,
|
||||
#[allow(non_snake_case)] userId: Option<String>,
|
||||
) -> Result<crate::provider::UsageResult, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::test_usage_script(
|
||||
state.inner(),
|
||||
app_type,
|
||||
&providerId,
|
||||
&scriptCode,
|
||||
timeout.unwrap_or(10),
|
||||
accessToken.as_deref(),
|
||||
userId.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 读取当前生效的配置内容
|
||||
#[tauri::command]
|
||||
pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::read_live_settings(app_type).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 测试第三方/自定义供应商端点的网络延迟
|
||||
#[tauri::command]
|
||||
pub async fn test_api_endpoints(
|
||||
urls: Vec<String>,
|
||||
#[allow(non_snake_case)] timeoutSecs: Option<u64>,
|
||||
) -> Result<Vec<EndpointLatency>, String> {
|
||||
SpeedtestService::test_endpoints(urls, timeoutSecs)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 获取自定义端点列表
|
||||
#[tauri::command]
|
||||
pub fn get_custom_endpoints(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
#[allow(non_snake_case)] providerId: String,
|
||||
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::get_custom_endpoints(state.inner(), app_type, &providerId)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 添加自定义端点
|
||||
#[tauri::command]
|
||||
pub fn add_custom_endpoint(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
#[allow(non_snake_case)] providerId: String,
|
||||
url: String,
|
||||
) -> Result<(), String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::add_custom_endpoint(state.inner(), app_type, &providerId, url)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 删除自定义端点
|
||||
#[tauri::command]
|
||||
pub fn remove_custom_endpoint(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
#[allow(non_snake_case)] providerId: String,
|
||||
url: String,
|
||||
) -> Result<(), String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::remove_custom_endpoint(state.inner(), app_type, &providerId, url)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 更新端点最后使用时间
|
||||
#[tauri::command]
|
||||
pub fn update_endpoint_last_used(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
#[allow(non_snake_case)] providerId: String,
|
||||
url: String,
|
||||
) -> Result<(), String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::update_endpoint_last_used(state.inner(), app_type, &providerId, url)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 更新多个供应商的排序
|
||||
#[tauri::command]
|
||||
pub fn update_providers_sort_order(
|
||||
state: State<'_, AppState>,
|
||||
app: String,
|
||||
updates: Vec<ProviderSortUpdate>,
|
||||
) -> Result<bool, String> {
|
||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||
ProviderService::update_sort_order(state.inner(), app_type, updates).map_err(|e| e.to_string())
|
||||
}
|
||||
39
src-tauri/src/commands/settings.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use tauri::AppHandle;
|
||||
|
||||
/// 获取设置
|
||||
#[tauri::command]
|
||||
pub async fn get_settings() -> Result<crate::settings::AppSettings, String> {
|
||||
Ok(crate::settings::get_settings())
|
||||
}
|
||||
|
||||
/// 保存设置
|
||||
#[tauri::command]
|
||||
pub async fn save_settings(settings: crate::settings::AppSettings) -> Result<bool, String> {
|
||||
crate::settings::update_settings(settings).map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 重启应用程序(当 app_config_dir 变更后使用)
|
||||
#[tauri::command]
|
||||
pub async fn restart_app(app: AppHandle) -> Result<bool, String> {
|
||||
app.restart();
|
||||
}
|
||||
|
||||
/// 获取 app_config_dir 覆盖配置 (从 Store)
|
||||
#[tauri::command]
|
||||
pub async fn get_app_config_dir_override(app: AppHandle) -> Result<Option<String>, String> {
|
||||
Ok(crate::app_store::refresh_app_config_dir_override(&app)
|
||||
.map(|p| p.to_string_lossy().to_string()))
|
||||
}
|
||||
|
||||
/// 设置 app_config_dir 覆盖配置 (到 Store)
|
||||
#[tauri::command]
|
||||
pub async fn set_app_config_dir_override(
|
||||
app: AppHandle,
|
||||
path: Option<String>,
|
||||
) -> Result<bool, String> {
|
||||
crate::app_store::set_app_config_dir_to_store(&app, path.as_deref())?;
|
||||
Ok(true)
|
||||
}
|
||||
@@ -1,15 +1,51 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
/// 获取 Claude Code 配置目录路径
|
||||
pub fn get_claude_config_dir() -> PathBuf {
|
||||
if let Some(custom) = crate::settings::get_claude_override_dir() {
|
||||
return custom;
|
||||
}
|
||||
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".claude")
|
||||
}
|
||||
|
||||
/// 默认 Claude MCP 配置文件路径 (~/.claude.json)
|
||||
pub fn get_default_claude_mcp_path() -> PathBuf {
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".claude.json")
|
||||
}
|
||||
|
||||
fn derive_mcp_path_from_override(dir: &Path) -> Option<PathBuf> {
|
||||
let file_name = dir
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().to_string())?
|
||||
.trim()
|
||||
.to_string();
|
||||
if file_name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let parent = dir.parent().unwrap_or_else(|| Path::new(""));
|
||||
Some(parent.join(format!("{}.json", file_name)))
|
||||
}
|
||||
|
||||
/// 获取 Claude MCP 配置文件路径,若设置了目录覆盖则与覆盖目录同级
|
||||
pub fn get_claude_mcp_path() -> PathBuf {
|
||||
if let Some(custom_dir) = crate::settings::get_claude_override_dir() {
|
||||
if let Some(path) = derive_mcp_path_from_override(&custom_dir) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
get_default_claude_mcp_path()
|
||||
}
|
||||
|
||||
/// 获取 Claude Code 主配置文件路径
|
||||
pub fn get_claude_settings_path() -> PathBuf {
|
||||
let dir = get_claude_config_dir();
|
||||
@@ -28,6 +64,10 @@ pub fn get_claude_settings_path() -> PathBuf {
|
||||
|
||||
/// 获取应用配置目录路径 (~/.cc-switch)
|
||||
pub fn get_app_config_dir() -> PathBuf {
|
||||
if let Some(custom) = crate::app_store::get_app_config_dir_override() {
|
||||
return custom;
|
||||
}
|
||||
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".cc-switch")
|
||||
@@ -52,46 +92,150 @@ pub fn sanitize_provider_name(name: &str) -> String {
|
||||
/// 获取供应商配置文件路径
|
||||
pub fn get_provider_config_path(provider_id: &str, provider_name: Option<&str>) -> PathBuf {
|
||||
let base_name = provider_name
|
||||
.map(|name| sanitize_provider_name(name))
|
||||
.map(sanitize_provider_name)
|
||||
.unwrap_or_else(|| sanitize_provider_name(provider_id));
|
||||
|
||||
get_claude_config_dir().join(format!("settings-{}.json", base_name))
|
||||
}
|
||||
|
||||
/// 读取 JSON 配置文件
|
||||
pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, String> {
|
||||
pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, AppError> {
|
||||
if !path.exists() {
|
||||
return Err(format!("文件不存在: {}", path.display()));
|
||||
return Err(AppError::Config(format!("文件不存在: {}", path.display())));
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(path).map_err(|e| format!("读取文件失败: {}", e))?;
|
||||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||||
|
||||
serde_json::from_str(&content).map_err(|e| format!("解析 JSON 失败: {}", e))
|
||||
serde_json::from_str(&content).map_err(|e| AppError::json(path, e))
|
||||
}
|
||||
|
||||
/// 写入 JSON 配置文件
|
||||
pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), String> {
|
||||
pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), AppError> {
|
||||
// 确保目录存在
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
|
||||
let json =
|
||||
serde_json::to_string_pretty(data).map_err(|e| format!("序列化 JSON 失败: {}", e))?;
|
||||
serde_json::to_string_pretty(data).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
|
||||
fs::write(path, json).map_err(|e| format!("写入文件失败: {}", e))
|
||||
atomic_write(path, json.as_bytes())
|
||||
}
|
||||
|
||||
/// 原子写入文本文件(用于 TOML/纯文本)
|
||||
pub fn write_text_file(path: &Path, data: &str) -> Result<(), AppError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
atomic_write(path, data.as_bytes())
|
||||
}
|
||||
|
||||
/// 原子写入:写入临时文件后 rename 替换,避免半写状态
|
||||
pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), AppError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
|
||||
let parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| AppError::Config("无效的路径".to_string()))?;
|
||||
let mut tmp = parent.to_path_buf();
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.ok_or_else(|| AppError::Config("无效的文件名".to_string()))?
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let ts = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos();
|
||||
tmp.push(format!("{}.tmp.{}", file_name, ts));
|
||||
|
||||
{
|
||||
let mut f = fs::File::create(&tmp).map_err(|e| AppError::io(&tmp, e))?;
|
||||
f.write_all(data).map_err(|e| AppError::io(&tmp, e))?;
|
||||
f.flush().map_err(|e| AppError::io(&tmp, e))?;
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if let Ok(meta) = fs::metadata(path) {
|
||||
let perm = meta.permissions().mode();
|
||||
let _ = fs::set_permissions(&tmp, fs::Permissions::from_mode(perm));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
// Windows 上 rename 目标存在会失败,先移除再重命名(尽量接近原子性)
|
||||
if path.exists() {
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
fs::rename(&tmp, path).map_err(|e| AppError::IoContext {
|
||||
context: format!("原子替换失败: {} -> {}", tmp.display(), path.display()),
|
||||
source: e,
|
||||
})?;
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
fs::rename(&tmp, path).map_err(|e| AppError::IoContext {
|
||||
context: format!("原子替换失败: {} -> {}", tmp.display(), path.display()),
|
||||
source: e,
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn derive_mcp_path_from_override_preserves_folder_name() {
|
||||
let override_dir = PathBuf::from("/tmp/profile/.claude");
|
||||
let derived = derive_mcp_path_from_override(&override_dir)
|
||||
.expect("should derive path for nested dir");
|
||||
assert_eq!(derived, PathBuf::from("/tmp/profile/.claude.json"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_mcp_path_from_override_handles_non_hidden_folder() {
|
||||
let override_dir = PathBuf::from("/data/claude-config");
|
||||
let derived = derive_mcp_path_from_override(&override_dir)
|
||||
.expect("should derive path for standard dir");
|
||||
assert_eq!(derived, PathBuf::from("/data/claude-config.json"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_mcp_path_from_override_supports_relative_rootless_dir() {
|
||||
let override_dir = PathBuf::from("claude");
|
||||
let derived = derive_mcp_path_from_override(&override_dir)
|
||||
.expect("should derive path for single segment");
|
||||
assert_eq!(derived, PathBuf::from("claude.json"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_mcp_path_from_root_like_dir_returns_none() {
|
||||
let override_dir = PathBuf::from("/");
|
||||
assert!(derive_mcp_path_from_override(&override_dir).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
/// 复制文件
|
||||
pub fn copy_file(from: &Path, to: &Path) -> Result<(), String> {
|
||||
fs::copy(from, to).map_err(|e| format!("复制文件失败: {}", e))?;
|
||||
pub fn copy_file(from: &Path, to: &Path) -> Result<(), AppError> {
|
||||
fs::copy(from, to).map_err(|e| AppError::IoContext {
|
||||
context: format!("复制文件失败 ({} -> {})", from.display(), to.display()),
|
||||
source: e,
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 删除文件
|
||||
pub fn delete_file(path: &Path) -> Result<(), String> {
|
||||
pub fn delete_file(path: &Path) -> Result<(), AppError> {
|
||||
if path.exists() {
|
||||
fs::remove_file(path).map_err(|e| format!("删除文件失败: {}", e))?;
|
||||
fs::remove_file(path).map_err(|e| AppError::io(path, e))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -111,31 +255,3 @@ pub fn get_claude_config_status() -> ConfigStatus {
|
||||
path: path.to_string_lossy().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 备份配置文件
|
||||
pub fn backup_config(from: &Path, to: &Path) -> Result<(), String> {
|
||||
if from.exists() {
|
||||
copy_file(from, to)?;
|
||||
log::info!("已备份配置文件: {} -> {}", from.display(), to.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 导入当前 Claude Code 配置为默认供应商
|
||||
pub fn import_current_config_as_default() -> Result<Value, String> {
|
||||
let settings_path = get_claude_settings_path();
|
||||
|
||||
if !settings_path.exists() {
|
||||
return Err("Claude Code 配置文件不存在".to_string());
|
||||
}
|
||||
|
||||
// 读取当前配置
|
||||
let settings_config: Value = read_json_file(&settings_path)?;
|
||||
|
||||
// 保存为 default 供应商
|
||||
let default_provider_path = get_provider_config_path("default", Some("default"));
|
||||
write_json_file(&default_provider_path, &settings_config)?;
|
||||
|
||||
log::info!("已导入当前配置为默认供应商");
|
||||
Ok(settings_config)
|
||||
}
|
||||
|
||||
96
src-tauri/src/error.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use std::path::Path;
|
||||
use std::sync::PoisonError;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AppError {
|
||||
#[error("配置错误: {0}")]
|
||||
Config(String),
|
||||
#[error("无效输入: {0}")]
|
||||
InvalidInput(String),
|
||||
#[error("IO 错误: {path}: {source}")]
|
||||
Io {
|
||||
path: String,
|
||||
#[source]
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[error("{context}: {source}")]
|
||||
IoContext {
|
||||
context: String,
|
||||
#[source]
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[error("JSON 解析错误: {path}: {source}")]
|
||||
Json {
|
||||
path: String,
|
||||
#[source]
|
||||
source: serde_json::Error,
|
||||
},
|
||||
#[error("JSON 序列化失败: {source}")]
|
||||
JsonSerialize {
|
||||
#[source]
|
||||
source: serde_json::Error,
|
||||
},
|
||||
#[error("TOML 解析错误: {path}: {source}")]
|
||||
Toml {
|
||||
path: String,
|
||||
#[source]
|
||||
source: toml::de::Error,
|
||||
},
|
||||
#[error("锁获取失败: {0}")]
|
||||
Lock(String),
|
||||
#[error("MCP 校验失败: {0}")]
|
||||
McpValidation(String),
|
||||
#[error("{0}")]
|
||||
Message(String),
|
||||
#[error("{zh} ({en})")]
|
||||
Localized {
|
||||
key: &'static str,
|
||||
zh: String,
|
||||
en: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl AppError {
|
||||
pub fn io(path: impl AsRef<Path>, source: std::io::Error) -> Self {
|
||||
Self::Io {
|
||||
path: path.as_ref().display().to_string(),
|
||||
source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json(path: impl AsRef<Path>, source: serde_json::Error) -> Self {
|
||||
Self::Json {
|
||||
path: path.as_ref().display().to_string(),
|
||||
source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toml(path: impl AsRef<Path>, source: toml::de::Error) -> Self {
|
||||
Self::Toml {
|
||||
path: path.as_ref().display().to_string(),
|
||||
source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn localized(key: &'static str, zh: impl Into<String>, en: impl Into<String>) -> Self {
|
||||
Self::Localized {
|
||||
key,
|
||||
zh: zh.into(),
|
||||
en: en.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<PoisonError<T>> for AppError {
|
||||
fn from(err: PoisonError<T>) -> Self {
|
||||
Self::Lock(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AppError> for String {
|
||||
fn from(err: AppError) -> Self {
|
||||
err.to_string()
|
||||
}
|
||||
}
|
||||
41
src-tauri/src/init_status.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use serde::Serialize;
|
||||
use std::sync::{OnceLock, RwLock};
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct InitErrorPayload {
|
||||
pub path: String,
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
static INIT_ERROR: OnceLock<RwLock<Option<InitErrorPayload>>> = OnceLock::new();
|
||||
|
||||
fn cell() -> &'static RwLock<Option<InitErrorPayload>> {
|
||||
INIT_ERROR.get_or_init(|| RwLock::new(None))
|
||||
}
|
||||
|
||||
pub fn set_init_error(payload: InitErrorPayload) {
|
||||
if let Ok(mut guard) = cell().write() {
|
||||
*guard = Some(payload);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_init_error() -> Option<InitErrorPayload> {
|
||||
cell().read().ok()?.clone()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn init_error_roundtrip() {
|
||||
let payload = InitErrorPayload {
|
||||
path: "/tmp/config.json".into(),
|
||||
error: "broken json".into(),
|
||||
};
|
||||
set_init_error(payload.clone());
|
||||
let got = get_init_error().expect("should get payload back");
|
||||
assert_eq!(got.path, payload.path);
|
||||
assert_eq!(got.error, payload.error);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,399 @@
|
||||
mod app_config;
|
||||
mod app_store;
|
||||
mod claude_mcp;
|
||||
mod claude_plugin;
|
||||
mod codex_config;
|
||||
mod commands;
|
||||
mod config;
|
||||
mod error;
|
||||
mod init_status;
|
||||
mod mcp;
|
||||
mod provider;
|
||||
mod services;
|
||||
mod settings;
|
||||
mod store;
|
||||
mod usage_script;
|
||||
|
||||
use store::AppState;
|
||||
use tauri::Manager;
|
||||
pub use app_config::{AppType, MultiAppConfig};
|
||||
pub use codex_config::{get_codex_auth_path, get_codex_config_path, write_codex_live_atomic};
|
||||
pub use commands::*;
|
||||
pub use config::{get_claude_mcp_path, get_claude_settings_path, read_json_file};
|
||||
pub use error::AppError;
|
||||
pub use mcp::{
|
||||
import_from_claude, import_from_codex, sync_enabled_to_claude, sync_enabled_to_codex,
|
||||
};
|
||||
pub use provider::Provider;
|
||||
pub use services::{ConfigService, EndpointLatency, McpService, ProviderService, SpeedtestService};
|
||||
pub use settings::{update_settings, AppSettings};
|
||||
pub use store::AppState;
|
||||
|
||||
use tauri::{
|
||||
menu::{CheckMenuItem, Menu, MenuBuilder, MenuItem},
|
||||
tray::{TrayIconBuilder, TrayIconEvent},
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::{ActivationPolicy, RunEvent};
|
||||
use tauri::{Emitter, Manager};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct TrayTexts {
|
||||
show_main: &'static str,
|
||||
no_provider_hint: &'static str,
|
||||
quit: &'static str,
|
||||
}
|
||||
|
||||
impl TrayTexts {
|
||||
fn from_language(language: &str) -> Self {
|
||||
match language {
|
||||
"en" => Self {
|
||||
show_main: "Open main window",
|
||||
no_provider_hint: " (No providers yet, please add them from the main window)",
|
||||
quit: "Quit",
|
||||
},
|
||||
_ => Self {
|
||||
show_main: "打开主界面",
|
||||
no_provider_hint: " (无供应商,请在主界面添加)",
|
||||
quit: "退出",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 创建动态托盘菜单
|
||||
fn create_tray_menu(
|
||||
app: &tauri::AppHandle,
|
||||
app_state: &AppState,
|
||||
) -> Result<Menu<tauri::Wry>, AppError> {
|
||||
let app_settings = crate::settings::get_settings();
|
||||
let tray_texts = TrayTexts::from_language(app_settings.language.as_deref().unwrap_or("zh"));
|
||||
|
||||
let config = app_state.config.read().map_err(AppError::from)?;
|
||||
|
||||
let mut menu_builder = MenuBuilder::new(app);
|
||||
|
||||
// 顶部:打开主界面
|
||||
let show_main_item =
|
||||
MenuItem::with_id(app, "show_main", tray_texts.show_main, true, None::<&str>)
|
||||
.map_err(|e| AppError::Message(format!("创建打开主界面菜单失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&show_main_item).separator();
|
||||
|
||||
// 直接添加所有供应商到主菜单(扁平化结构,更简单可靠)
|
||||
if let Some(claude_manager) = config.get_manager(&crate::app_config::AppType::Claude) {
|
||||
// 添加Claude标题(禁用状态,仅作为分组标识)
|
||||
let claude_header =
|
||||
MenuItem::with_id(app, "claude_header", "─── Claude ───", false, None::<&str>)
|
||||
.map_err(|e| AppError::Message(format!("创建Claude标题失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&claude_header);
|
||||
|
||||
if !claude_manager.providers.is_empty() {
|
||||
// Sort providers by sortIndex, then by createdAt, then by name
|
||||
let mut sorted_providers: Vec<_> = claude_manager.providers.iter().collect();
|
||||
sorted_providers.sort_by(|(_, a), (_, b)| {
|
||||
// Priority 1: sortIndex
|
||||
match (a.sort_index, b.sort_index) {
|
||||
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
|
||||
(Some(_), None) => return std::cmp::Ordering::Less,
|
||||
(None, Some(_)) => return std::cmp::Ordering::Greater,
|
||||
_ => {}
|
||||
}
|
||||
// Priority 2: createdAt
|
||||
match (a.created_at, b.created_at) {
|
||||
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
|
||||
(Some(_), None) => return std::cmp::Ordering::Greater,
|
||||
(None, Some(_)) => return std::cmp::Ordering::Less,
|
||||
_ => {}
|
||||
}
|
||||
// Priority 3: name
|
||||
a.name.cmp(&b.name)
|
||||
});
|
||||
|
||||
for (id, provider) in sorted_providers {
|
||||
let is_current = claude_manager.current == *id;
|
||||
let item = CheckMenuItem::with_id(
|
||||
app,
|
||||
format!("claude_{}", id),
|
||||
&provider.name,
|
||||
true,
|
||||
is_current,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| AppError::Message(format!("创建菜单项失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&item);
|
||||
}
|
||||
} else {
|
||||
// 没有供应商时显示提示
|
||||
let empty_hint = MenuItem::with_id(
|
||||
app,
|
||||
"claude_empty",
|
||||
tray_texts.no_provider_hint,
|
||||
false,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| AppError::Message(format!("创建Claude空提示失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&empty_hint);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(codex_manager) = config.get_manager(&crate::app_config::AppType::Codex) {
|
||||
// 添加Codex标题(禁用状态,仅作为分组标识)
|
||||
let codex_header =
|
||||
MenuItem::with_id(app, "codex_header", "─── Codex ───", false, None::<&str>)
|
||||
.map_err(|e| AppError::Message(format!("创建Codex标题失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&codex_header);
|
||||
|
||||
if !codex_manager.providers.is_empty() {
|
||||
// Sort providers by sortIndex, then by createdAt, then by name
|
||||
let mut sorted_providers: Vec<_> = codex_manager.providers.iter().collect();
|
||||
sorted_providers.sort_by(|(_, a), (_, b)| {
|
||||
// Priority 1: sortIndex
|
||||
match (a.sort_index, b.sort_index) {
|
||||
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
|
||||
(Some(_), None) => return std::cmp::Ordering::Less,
|
||||
(None, Some(_)) => return std::cmp::Ordering::Greater,
|
||||
_ => {}
|
||||
}
|
||||
// Priority 2: createdAt
|
||||
match (a.created_at, b.created_at) {
|
||||
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
|
||||
(Some(_), None) => return std::cmp::Ordering::Greater,
|
||||
(None, Some(_)) => return std::cmp::Ordering::Less,
|
||||
_ => {}
|
||||
}
|
||||
// Priority 3: name
|
||||
a.name.cmp(&b.name)
|
||||
});
|
||||
|
||||
for (id, provider) in sorted_providers {
|
||||
let is_current = codex_manager.current == *id;
|
||||
let item = CheckMenuItem::with_id(
|
||||
app,
|
||||
format!("codex_{}", id),
|
||||
&provider.name,
|
||||
true,
|
||||
is_current,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| AppError::Message(format!("创建菜单项失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&item);
|
||||
}
|
||||
} else {
|
||||
// 没有供应商时显示提示
|
||||
let empty_hint = MenuItem::with_id(
|
||||
app,
|
||||
"codex_empty",
|
||||
tray_texts.no_provider_hint,
|
||||
false,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| AppError::Message(format!("创建Codex空提示失败: {}", e)))?;
|
||||
menu_builder = menu_builder.item(&empty_hint);
|
||||
}
|
||||
}
|
||||
|
||||
// 分隔符和退出菜单
|
||||
let quit_item = MenuItem::with_id(app, "quit", tray_texts.quit, true, None::<&str>)
|
||||
.map_err(|e| AppError::Message(format!("创建退出菜单失败: {}", e)))?;
|
||||
|
||||
menu_builder = menu_builder.separator().item(&quit_item);
|
||||
|
||||
menu_builder
|
||||
.build()
|
||||
.map_err(|e| AppError::Message(format!("构建菜单失败: {}", e)))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn apply_tray_policy(app: &tauri::AppHandle, dock_visible: bool) {
|
||||
let desired_policy = if dock_visible {
|
||||
ActivationPolicy::Regular
|
||||
} else {
|
||||
ActivationPolicy::Accessory
|
||||
};
|
||||
|
||||
if let Err(err) = app.set_dock_visibility(dock_visible) {
|
||||
log::warn!("设置 Dock 显示状态失败: {}", err);
|
||||
}
|
||||
|
||||
if let Err(err) = app.set_activation_policy(desired_policy) {
|
||||
log::warn!("设置激活策略失败: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
/// 处理托盘菜单事件
|
||||
fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
|
||||
log::info!("处理托盘菜单事件: {}", event_id);
|
||||
|
||||
match event_id {
|
||||
"show_main" => {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let _ = window.set_skip_taskbar(false);
|
||||
}
|
||||
let _ = window.unminimize();
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
apply_tray_policy(app, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
"quit" => {
|
||||
log::info!("退出应用");
|
||||
app.exit(0);
|
||||
}
|
||||
id if id.starts_with("claude_") => {
|
||||
let provider_id = id.strip_prefix("claude_").unwrap();
|
||||
log::info!("切换到Claude供应商: {}", provider_id);
|
||||
|
||||
// 执行切换
|
||||
let app_handle = app.clone();
|
||||
let provider_id = provider_id.to_string();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
if let Err(e) = switch_provider_internal(
|
||||
&app_handle,
|
||||
crate::app_config::AppType::Claude,
|
||||
provider_id,
|
||||
) {
|
||||
log::error!("切换Claude供应商失败: {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
id if id.starts_with("codex_") => {
|
||||
let provider_id = id.strip_prefix("codex_").unwrap();
|
||||
log::info!("切换到Codex供应商: {}", provider_id);
|
||||
|
||||
// 执行切换
|
||||
let app_handle = app.clone();
|
||||
let provider_id = provider_id.to_string();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
if let Err(e) = switch_provider_internal(
|
||||
&app_handle,
|
||||
crate::app_config::AppType::Codex,
|
||||
provider_id,
|
||||
) {
|
||||
log::error!("切换Codex供应商失败: {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
log::warn!("未处理的菜单事件: {}", event_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
/// 内部切换供应商函数
|
||||
fn switch_provider_internal(
|
||||
app: &tauri::AppHandle,
|
||||
app_type: crate::app_config::AppType,
|
||||
provider_id: String,
|
||||
) -> Result<(), AppError> {
|
||||
if let Some(app_state) = app.try_state::<AppState>() {
|
||||
// 在使用前先保存需要的值
|
||||
let app_type_str = app_type.as_str().to_string();
|
||||
let provider_id_clone = provider_id.clone();
|
||||
|
||||
crate::commands::switch_provider(app_state.clone(), app_type_str.clone(), provider_id)
|
||||
.map_err(AppError::Message)?;
|
||||
|
||||
// 切换成功后重新创建托盘菜单
|
||||
if let Ok(new_menu) = create_tray_menu(app, app_state.inner()) {
|
||||
if let Some(tray) = app.tray_by_id("main") {
|
||||
if let Err(e) = tray.set_menu(Some(new_menu)) {
|
||||
log::error!("更新托盘菜单失败: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 发射事件到前端,通知供应商已切换
|
||||
let event_data = serde_json::json!({
|
||||
"appType": app_type_str,
|
||||
"providerId": provider_id_clone
|
||||
});
|
||||
if let Err(e) = app.emit("provider-switched", event_data) {
|
||||
log::error!("发射供应商切换事件失败: {}", e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 更新托盘菜单的Tauri命令
|
||||
#[tauri::command]
|
||||
async fn update_tray_menu(
|
||||
app: tauri::AppHandle,
|
||||
state: tauri::State<'_, AppState>,
|
||||
) -> Result<bool, String> {
|
||||
match create_tray_menu(&app, state.inner()) {
|
||||
Ok(new_menu) => {
|
||||
if let Some(tray) = app.tray_by_id("main") {
|
||||
tray.set_menu(Some(new_menu))
|
||||
.map_err(|e| format!("更新托盘菜单失败: {}", e))?;
|
||||
return Ok(true);
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("创建托盘菜单失败: {}", err);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
let mut builder = tauri::Builder::default();
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
|
||||
{
|
||||
builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let _ = window.unminimize();
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
let builder = builder
|
||||
// 拦截窗口关闭:根据设置决定是否最小化到托盘
|
||||
.on_window_event(|window, event| {
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
||||
let settings = crate::settings::get_settings();
|
||||
|
||||
if settings.minimize_to_tray_on_close {
|
||||
api.prevent_close();
|
||||
let _ = window.hide();
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let _ = window.set_skip_taskbar(true);
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
apply_tray_policy(window.app_handle(), false);
|
||||
}
|
||||
} else {
|
||||
window.app_handle().exit(0);
|
||||
}
|
||||
}
|
||||
})
|
||||
.plugin(tauri_plugin_process::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_store::Builder::new().build())
|
||||
.setup(|app| {
|
||||
// 注册 Updater 插件(桌面端)
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
if let Err(e) = app
|
||||
.handle()
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())
|
||||
{
|
||||
// 若配置不完整(如缺少 pubkey),跳过 Updater 而不中断应用
|
||||
log::warn!("初始化 Updater 插件失败,已跳过:{}", e);
|
||||
}
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
// 设置 macOS 标题栏背景色为主界面蓝色
|
||||
@@ -50,39 +433,67 @@ pub fn run() {
|
||||
)?;
|
||||
}
|
||||
|
||||
// 预先刷新 Store 覆盖配置,确保 AppState 初始化时可读取到最新路径
|
||||
app_store::refresh_app_config_dir_override(app.handle());
|
||||
|
||||
// 初始化应用状态(仅创建一次,并在本函数末尾注入 manage)
|
||||
let app_state = AppState::new();
|
||||
|
||||
// 如果没有供应商且存在 Claude Code 配置,自动导入
|
||||
{
|
||||
let manager = app_state.provider_manager.lock().unwrap();
|
||||
if manager.providers.is_empty() {
|
||||
drop(manager); // 释放锁
|
||||
|
||||
let settings_path = config::get_claude_settings_path();
|
||||
if settings_path.exists() {
|
||||
log::info!("检测到 Claude Code 配置,自动导入为默认供应商");
|
||||
|
||||
if let Ok(settings_config) = config::import_current_config_as_default() {
|
||||
let mut manager = app_state.provider_manager.lock().unwrap();
|
||||
let provider = provider::Provider::with_id(
|
||||
"default".to_string(),
|
||||
"default".to_string(),
|
||||
settings_config,
|
||||
None,
|
||||
);
|
||||
|
||||
if manager.add_provider(provider).is_ok() {
|
||||
manager.current = "default".to_string();
|
||||
drop(manager);
|
||||
let _ = app_state.save();
|
||||
log::info!("成功导入默认供应商");
|
||||
}
|
||||
}
|
||||
// 如果配置解析失败,则向前端发送错误事件并提前结束 setup(不落盘、不覆盖配置)。
|
||||
let app_state = match AppState::try_new() {
|
||||
Ok(state) => state,
|
||||
Err(err) => {
|
||||
let path = crate::config::get_app_config_path();
|
||||
let payload_json = serde_json::json!({
|
||||
"path": path.display().to_string(),
|
||||
"error": err.to_string(),
|
||||
});
|
||||
// 事件通知(可能早于前端订阅,不保证送达)
|
||||
if let Err(e) = app.emit("configLoadError", payload_json) {
|
||||
log::error!("发射配置加载错误事件失败: {}", e);
|
||||
}
|
||||
// 同时缓存错误,供前端启动阶段主动拉取
|
||||
crate::init_status::set_init_error(crate::init_status::InitErrorPayload {
|
||||
path: path.display().to_string(),
|
||||
error: err.to_string(),
|
||||
});
|
||||
// 不再继续构建托盘/命令依赖的状态,交由前端提示后退出。
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
// 迁移旧的 app_config_dir 配置到 Store
|
||||
if let Err(e) = app_store::migrate_app_config_dir_from_settings(app.handle()) {
|
||||
log::warn!("迁移 app_config_dir 失败: {}", e);
|
||||
}
|
||||
|
||||
// 确保配置结构就绪(已移除旧版本的副本迁移逻辑)
|
||||
{
|
||||
let mut config_guard = app_state.config.write().unwrap();
|
||||
config_guard.ensure_app(&app_config::AppType::Claude);
|
||||
config_guard.ensure_app(&app_config::AppType::Codex);
|
||||
}
|
||||
|
||||
// 启动阶段不再无条件保存,避免意外覆盖用户配置。
|
||||
|
||||
// 创建动态托盘菜单
|
||||
let menu = create_tray_menu(app.handle(), &app_state)?;
|
||||
|
||||
// 构建托盘
|
||||
let mut tray_builder = TrayIconBuilder::with_id("main")
|
||||
.on_tray_icon_event(|_tray, event| match event {
|
||||
// 左键点击已通过 show_menu_on_left_click(true) 打开菜单,这里不再额外处理
|
||||
TrayIconEvent::Click { .. } => {}
|
||||
_ => log::debug!("unhandled event {event:?}"),
|
||||
})
|
||||
.menu(&menu)
|
||||
.on_menu_event(|app, event| {
|
||||
handle_tray_menu_event(app, &event.id.0);
|
||||
})
|
||||
.show_menu_on_left_click(true);
|
||||
|
||||
// 统一使用应用默认图标;待托盘模板图标就绪后再启用
|
||||
tray_builder = tray_builder.icon(app.default_window_icon().unwrap().clone());
|
||||
|
||||
let _tray = tray_builder.build(app)?;
|
||||
// 将同一个实例注入到全局状态,避免重复创建导致的不一致
|
||||
app.manage(app_state);
|
||||
Ok(())
|
||||
@@ -96,10 +507,86 @@ pub fn run() {
|
||||
commands::switch_provider,
|
||||
commands::import_default_config,
|
||||
commands::get_claude_config_status,
|
||||
commands::get_config_status,
|
||||
commands::get_claude_code_config_path,
|
||||
commands::get_config_dir,
|
||||
commands::open_config_folder,
|
||||
commands::pick_directory,
|
||||
commands::open_external,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
commands::get_init_error,
|
||||
commands::get_app_config_path,
|
||||
commands::open_app_config_folder,
|
||||
commands::read_live_provider_settings,
|
||||
commands::get_settings,
|
||||
commands::save_settings,
|
||||
commands::restart_app,
|
||||
commands::check_for_updates,
|
||||
commands::is_portable_mode,
|
||||
commands::get_claude_plugin_status,
|
||||
commands::read_claude_plugin_config,
|
||||
commands::apply_claude_plugin_config,
|
||||
commands::is_claude_plugin_applied,
|
||||
// Claude MCP management
|
||||
commands::get_claude_mcp_status,
|
||||
commands::read_claude_mcp_config,
|
||||
commands::upsert_claude_mcp_server,
|
||||
commands::delete_claude_mcp_server,
|
||||
commands::validate_mcp_command,
|
||||
// usage query
|
||||
commands::queryProviderUsage,
|
||||
commands::testUsageScript,
|
||||
// New MCP via config.json (SSOT)
|
||||
commands::get_mcp_config,
|
||||
commands::upsert_mcp_server_in_config,
|
||||
commands::delete_mcp_server_in_config,
|
||||
commands::set_mcp_enabled,
|
||||
commands::sync_enabled_mcp_to_claude,
|
||||
commands::sync_enabled_mcp_to_codex,
|
||||
commands::import_mcp_from_claude,
|
||||
commands::import_mcp_from_codex,
|
||||
// ours: endpoint speed test + custom endpoint management
|
||||
commands::test_api_endpoints,
|
||||
commands::get_custom_endpoints,
|
||||
commands::add_custom_endpoint,
|
||||
commands::remove_custom_endpoint,
|
||||
commands::update_endpoint_last_used,
|
||||
// app_config_dir override via Store
|
||||
commands::get_app_config_dir_override,
|
||||
commands::set_app_config_dir_override,
|
||||
// provider sort order management
|
||||
commands::update_providers_sort_order,
|
||||
// theirs: config import/export and dialogs
|
||||
commands::export_config_to_file,
|
||||
commands::import_config_from_file,
|
||||
commands::save_file_dialog,
|
||||
commands::open_file_dialog,
|
||||
commands::sync_current_providers_live,
|
||||
update_tray_menu,
|
||||
]);
|
||||
|
||||
let app = builder
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
app.run(|app_handle, event| {
|
||||
#[cfg(target_os = "macos")]
|
||||
// macOS 在 Dock 图标被点击并重新激活应用时会触发 Reopen 事件,这里手动恢复主窗口
|
||||
if let RunEvent::Reopen { .. } = event {
|
||||
if let Some(window) = app_handle.get_webview_window("main") {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let _ = window.set_skip_taskbar(false);
|
||||
}
|
||||
let _ = window.unminimize();
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
apply_tray_policy(app_handle, true);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
let _ = (app_handle, event);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
720
src-tauri/src/mcp.rs
Normal file
@@ -0,0 +1,720 @@
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::app_config::{AppType, McpConfig, MultiAppConfig};
|
||||
use crate::error::AppError;
|
||||
|
||||
/// 基础校验:允许 stdio/http;或省略 type(视为 stdio)。对应必填字段存在
|
||||
fn validate_server_spec(spec: &Value) -> Result<(), AppError> {
|
||||
if !spec.is_object() {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器连接定义必须为 JSON 对象".into(),
|
||||
));
|
||||
}
|
||||
let t_opt = spec.get("type").and_then(|x| x.as_str());
|
||||
// 支持两种:stdio/http;若缺省 type 则按 stdio 处理(与社区常见 .mcp.json 一致)
|
||||
let is_stdio = t_opt.map(|t| t == "stdio").unwrap_or(true);
|
||||
let is_http = t_opt.map(|t| t == "http").unwrap_or(false);
|
||||
|
||||
if !(is_stdio || is_http) {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 type 必须是 'stdio' 或 'http'(或省略表示 stdio)".into(),
|
||||
));
|
||||
}
|
||||
|
||||
if is_stdio {
|
||||
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if cmd.trim().is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
if is_http {
|
||||
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
||||
if url.trim().is_empty() {
|
||||
return Err(AppError::McpValidation(
|
||||
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_mcp_entry(entry: &Value) -> Result<(), AppError> {
|
||||
let obj = entry
|
||||
.as_object()
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
||||
|
||||
let server = obj
|
||||
.get("server")
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目缺少 server 字段".into()))?;
|
||||
validate_server_spec(server)?;
|
||||
|
||||
for key in ["name", "description", "homepage", "docs"] {
|
||||
if let Some(val) = obj.get(key) {
|
||||
if !val.is_string() {
|
||||
return Err(AppError::McpValidation(format!(
|
||||
"MCP 服务器 {} 必须为字符串",
|
||||
key
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tags) = obj.get("tags") {
|
||||
let arr = tags
|
||||
.as_array()
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器 tags 必须为字符串数组".into()))?;
|
||||
if !arr.iter().all(|item| item.is_string()) {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 tags 必须为字符串数组".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(enabled) = obj.get("enabled") {
|
||||
if !enabled.is_boolean() {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 enabled 必须为布尔值".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn normalize_server_keys(map: &mut HashMap<String, Value>) -> usize {
|
||||
let mut change_count = 0usize;
|
||||
let mut renames: Vec<(String, String)> = Vec::new();
|
||||
|
||||
for (key_ref, value) in map.iter_mut() {
|
||||
let key = key_ref.clone();
|
||||
let Some(obj) = value.as_object_mut() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let id_value = obj.get("id").cloned();
|
||||
|
||||
let target_id: String;
|
||||
|
||||
match id_value {
|
||||
Some(id_val) => match id_val.as_str() {
|
||||
Some(id_str) => {
|
||||
let trimmed = id_str.trim();
|
||||
if trimmed.is_empty() {
|
||||
obj.insert("id".into(), json!(key.clone()));
|
||||
change_count += 1;
|
||||
target_id = key.clone();
|
||||
} else {
|
||||
if trimmed != id_str {
|
||||
obj.insert("id".into(), json!(trimmed));
|
||||
change_count += 1;
|
||||
}
|
||||
target_id = trimmed.to_string();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
obj.insert("id".into(), json!(key.clone()));
|
||||
change_count += 1;
|
||||
target_id = key.clone();
|
||||
}
|
||||
},
|
||||
None => {
|
||||
obj.insert("id".into(), json!(key.clone()));
|
||||
change_count += 1;
|
||||
target_id = key.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if target_id != key {
|
||||
renames.push((key, target_id));
|
||||
}
|
||||
}
|
||||
|
||||
for (old_key, new_key) in renames {
|
||||
if old_key == new_key {
|
||||
continue;
|
||||
}
|
||||
if map.contains_key(&new_key) {
|
||||
log::warn!(
|
||||
"MCP 条目 '{}' 的内部 id '{}' 与现有键冲突,回退为原键",
|
||||
old_key,
|
||||
new_key
|
||||
);
|
||||
if let Some(value) = map.get_mut(&old_key) {
|
||||
if let Some(obj) = value.as_object_mut() {
|
||||
if obj
|
||||
.get("id")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s != old_key)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
obj.insert("id".into(), json!(old_key.clone()));
|
||||
change_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(mut value) = map.remove(&old_key) {
|
||||
if let Some(obj) = value.as_object_mut() {
|
||||
obj.insert("id".into(), json!(new_key.clone()));
|
||||
}
|
||||
log::info!("MCP 条目键名已自动修复: '{}' -> '{}'", old_key, new_key);
|
||||
map.insert(new_key, value);
|
||||
change_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
change_count
|
||||
}
|
||||
|
||||
pub fn normalize_servers_for(config: &mut MultiAppConfig, app: &AppType) -> usize {
|
||||
let servers = &mut config.mcp_for_mut(app).servers;
|
||||
normalize_server_keys(servers)
|
||||
}
|
||||
|
||||
fn extract_server_spec(entry: &Value) -> Result<Value, AppError> {
|
||||
let obj = entry
|
||||
.as_object()
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
||||
let server = obj
|
||||
.get("server")
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目缺少 server 字段".into()))?;
|
||||
|
||||
if !server.is_object() {
|
||||
return Err(AppError::McpValidation(
|
||||
"MCP 服务器 server 字段必须为 JSON 对象".into(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(server.clone())
|
||||
}
|
||||
|
||||
/// 返回已启用的 MCP 服务器(过滤 enabled==true)
|
||||
fn collect_enabled_servers(cfg: &McpConfig) -> HashMap<String, Value> {
|
||||
let mut out = HashMap::new();
|
||||
for (id, entry) in cfg.servers.iter() {
|
||||
let enabled = entry
|
||||
.get("enabled")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
if !enabled {
|
||||
continue;
|
||||
}
|
||||
match extract_server_spec(entry) {
|
||||
Ok(spec) => {
|
||||
out.insert(id.clone(), spec);
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("跳过无效的 MCP 条目 '{}': {}", id, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn get_servers_snapshot_for(
|
||||
config: &mut MultiAppConfig,
|
||||
app: &AppType,
|
||||
) -> (HashMap<String, Value>, usize) {
|
||||
let normalized = normalize_servers_for(config, app);
|
||||
let mut snapshot = config.mcp_for(app).servers.clone();
|
||||
snapshot.retain(|id, value| {
|
||||
let Some(obj) = value.as_object_mut() else {
|
||||
log::warn!("跳过无效的 MCP 条目 '{}': 必须为 JSON 对象", id);
|
||||
return false;
|
||||
};
|
||||
|
||||
obj.entry(String::from("id")).or_insert(json!(id));
|
||||
|
||||
match validate_mcp_entry(value) {
|
||||
Ok(()) => true,
|
||||
Err(err) => {
|
||||
log::error!("config.json 中存在无效的 MCP 条目 '{}': {}", id, err);
|
||||
false
|
||||
}
|
||||
}
|
||||
});
|
||||
(snapshot, normalized)
|
||||
}
|
||||
|
||||
pub fn upsert_in_config_for(
|
||||
config: &mut MultiAppConfig,
|
||||
app: &AppType,
|
||||
id: &str,
|
||||
spec: Value,
|
||||
) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
normalize_servers_for(config, app);
|
||||
validate_mcp_entry(&spec)?;
|
||||
|
||||
let mut entry_obj = spec
|
||||
.as_object()
|
||||
.cloned()
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
||||
if let Some(existing_id) = entry_obj.get("id") {
|
||||
let Some(existing_id_str) = existing_id.as_str() else {
|
||||
return Err(AppError::McpValidation("MCP 服务器 id 必须为字符串".into()));
|
||||
};
|
||||
if existing_id_str != id {
|
||||
return Err(AppError::McpValidation(format!(
|
||||
"MCP 服务器条目中的 id '{}' 与参数 id '{}' 不一致",
|
||||
existing_id_str, id
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
entry_obj.insert(String::from("id"), json!(id));
|
||||
}
|
||||
|
||||
let value = Value::Object(entry_obj);
|
||||
|
||||
let servers = &mut config.mcp_for_mut(app).servers;
|
||||
let before = servers.get(id).cloned();
|
||||
servers.insert(id.to_string(), value);
|
||||
|
||||
Ok(before.is_none())
|
||||
}
|
||||
|
||||
pub fn delete_in_config_for(
|
||||
config: &mut MultiAppConfig,
|
||||
app: &AppType,
|
||||
id: &str,
|
||||
) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
normalize_servers_for(config, app);
|
||||
let existed = config.mcp_for_mut(app).servers.remove(id).is_some();
|
||||
Ok(existed)
|
||||
}
|
||||
|
||||
/// 设置启用状态(不执行落盘或文件同步)
|
||||
pub fn set_enabled_flag_for(
|
||||
config: &mut MultiAppConfig,
|
||||
app: &AppType,
|
||||
id: &str,
|
||||
enabled: bool,
|
||||
) -> Result<bool, AppError> {
|
||||
if id.trim().is_empty() {
|
||||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||||
}
|
||||
normalize_servers_for(config, app);
|
||||
if let Some(spec) = config.mcp_for_mut(app).servers.get_mut(id) {
|
||||
// 写入 enabled 字段
|
||||
let mut obj = spec
|
||||
.as_object()
|
||||
.cloned()
|
||||
.ok_or_else(|| AppError::McpValidation("MCP 服务器定义必须为 JSON 对象".into()))?;
|
||||
obj.insert("enabled".into(), json!(enabled));
|
||||
*spec = Value::Object(obj);
|
||||
} else {
|
||||
// 若不存在则直接返回 false
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 将 config.json 中 enabled==true 的项投影写入 ~/.claude.json
|
||||
pub fn sync_enabled_to_claude(config: &MultiAppConfig) -> Result<(), AppError> {
|
||||
let enabled = collect_enabled_servers(&config.mcp.claude);
|
||||
crate::claude_mcp::set_mcp_servers_map(&enabled)
|
||||
}
|
||||
|
||||
/// 从 ~/.claude.json 导入 mcpServers 到 config.json(设为 enabled=true)。
|
||||
/// 已存在的项仅强制 enabled=true,不覆盖其他字段。
|
||||
pub fn import_from_claude(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
||||
let text_opt = crate::claude_mcp::read_mcp_json()?;
|
||||
let Some(text) = text_opt else { return Ok(0) };
|
||||
let mut changed = normalize_servers_for(config, &AppType::Claude);
|
||||
let v: Value = serde_json::from_str(&text)
|
||||
.map_err(|e| AppError::McpValidation(format!("解析 ~/.claude.json 失败: {}", e)))?;
|
||||
let Some(map) = v.get("mcpServers").and_then(|x| x.as_object()) else {
|
||||
return Ok(changed);
|
||||
};
|
||||
|
||||
for (id, spec) in map.iter() {
|
||||
// 校验目标 spec
|
||||
validate_server_spec(spec)?;
|
||||
|
||||
let entry = config
|
||||
.mcp_for_mut(&AppType::Claude)
|
||||
.servers
|
||||
.entry(id.clone());
|
||||
use std::collections::hash_map::Entry;
|
||||
match entry {
|
||||
Entry::Vacant(vac) => {
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
vac.insert(Value::Object(obj));
|
||||
changed += 1;
|
||||
}
|
||||
Entry::Occupied(mut occ) => {
|
||||
let value = occ.get_mut();
|
||||
let Some(existing) = value.as_object_mut() else {
|
||||
log::warn!("MCP 条目 '{}' 不是 JSON 对象,覆盖为导入数据", id);
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
occ.insert(Value::Object(obj));
|
||||
changed += 1;
|
||||
continue;
|
||||
};
|
||||
|
||||
let mut modified = false;
|
||||
let prev_enabled = existing
|
||||
.get("enabled")
|
||||
.and_then(|b| b.as_bool())
|
||||
.unwrap_or(false);
|
||||
if !prev_enabled {
|
||||
existing.insert(String::from("enabled"), json!(true));
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("server").is_none() {
|
||||
log::warn!("MCP 条目 '{}' 缺少 server 字段,覆盖为导入数据", id);
|
||||
existing.insert(String::from("server"), spec.clone());
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("id").is_none() {
|
||||
log::warn!("MCP 条目 '{}' 缺少 id 字段,自动填充", id);
|
||||
existing.insert(String::from("id"), json!(id));
|
||||
modified = true;
|
||||
}
|
||||
if modified {
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// 从 ~/.codex/config.toml 导入 MCP 到 config.json(Codex 作用域),并将导入项设为 enabled=true。
|
||||
/// 支持两种 schema:[mcp.servers.<id>] 与 [mcp_servers.<id>]。
|
||||
/// 已存在的项仅强制 enabled=true,不覆盖其他字段。
|
||||
pub fn import_from_codex(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
||||
let text = crate::codex_config::read_and_validate_codex_config_text()?;
|
||||
if text.trim().is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
let mut changed_total = normalize_servers_for(config, &AppType::Codex);
|
||||
|
||||
let root: toml::Table = toml::from_str(&text)
|
||||
.map_err(|e| AppError::McpValidation(format!("解析 ~/.codex/config.toml 失败: {}", e)))?;
|
||||
|
||||
// helper:处理一组 servers 表
|
||||
let mut import_servers_tbl = |servers_tbl: &toml::value::Table| {
|
||||
let mut changed = 0usize;
|
||||
for (id, entry_val) in servers_tbl.iter() {
|
||||
let Some(entry_tbl) = entry_val.as_table() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// type 缺省为 stdio
|
||||
let typ = entry_tbl
|
||||
.get("type")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("stdio");
|
||||
|
||||
// 构建 JSON 规范
|
||||
let mut spec = serde_json::Map::new();
|
||||
spec.insert("type".into(), json!(typ));
|
||||
|
||||
match typ {
|
||||
"stdio" => {
|
||||
if let Some(cmd) = entry_tbl.get("command").and_then(|v| v.as_str()) {
|
||||
spec.insert("command".into(), json!(cmd));
|
||||
}
|
||||
if let Some(args) = entry_tbl.get("args").and_then(|v| v.as_array()) {
|
||||
let arr = args
|
||||
.iter()
|
||||
.filter_map(|x| x.as_str())
|
||||
.map(|s| json!(s))
|
||||
.collect::<Vec<_>>();
|
||||
if !arr.is_empty() {
|
||||
spec.insert("args".into(), serde_json::Value::Array(arr));
|
||||
}
|
||||
}
|
||||
if let Some(cwd) = entry_tbl.get("cwd").and_then(|v| v.as_str()) {
|
||||
if !cwd.trim().is_empty() {
|
||||
spec.insert("cwd".into(), json!(cwd));
|
||||
}
|
||||
}
|
||||
if let Some(env_tbl) = entry_tbl.get("env").and_then(|v| v.as_table()) {
|
||||
let mut env_json = serde_json::Map::new();
|
||||
for (k, v) in env_tbl.iter() {
|
||||
if let Some(sv) = v.as_str() {
|
||||
env_json.insert(k.clone(), json!(sv));
|
||||
}
|
||||
}
|
||||
if !env_json.is_empty() {
|
||||
spec.insert("env".into(), serde_json::Value::Object(env_json));
|
||||
}
|
||||
}
|
||||
}
|
||||
"http" => {
|
||||
if let Some(url) = entry_tbl.get("url").and_then(|v| v.as_str()) {
|
||||
spec.insert("url".into(), json!(url));
|
||||
}
|
||||
if let Some(headers_tbl) = entry_tbl.get("headers").and_then(|v| v.as_table()) {
|
||||
let mut headers_json = serde_json::Map::new();
|
||||
for (k, v) in headers_tbl.iter() {
|
||||
if let Some(sv) = v.as_str() {
|
||||
headers_json.insert(k.clone(), json!(sv));
|
||||
}
|
||||
}
|
||||
if !headers_json.is_empty() {
|
||||
spec.insert("headers".into(), serde_json::Value::Object(headers_json));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let spec_v = serde_json::Value::Object(spec);
|
||||
|
||||
// 校验
|
||||
if let Err(e) = validate_server_spec(&spec_v) {
|
||||
log::warn!("跳过无效 Codex MCP 项 '{}': {}", id, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 合并:仅强制 enabled=true
|
||||
use std::collections::hash_map::Entry;
|
||||
let entry = config
|
||||
.mcp_for_mut(&AppType::Codex)
|
||||
.servers
|
||||
.entry(id.clone());
|
||||
match entry {
|
||||
Entry::Vacant(vac) => {
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec_v.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
vac.insert(serde_json::Value::Object(obj));
|
||||
changed += 1;
|
||||
}
|
||||
Entry::Occupied(mut occ) => {
|
||||
let value = occ.get_mut();
|
||||
let Some(existing) = value.as_object_mut() else {
|
||||
log::warn!("MCP 条目 '{}' 不是 JSON 对象,覆盖为导入数据", id);
|
||||
let mut obj = serde_json::Map::new();
|
||||
obj.insert(String::from("id"), json!(id));
|
||||
obj.insert(String::from("name"), json!(id));
|
||||
obj.insert(String::from("server"), spec_v.clone());
|
||||
obj.insert(String::from("enabled"), json!(true));
|
||||
occ.insert(serde_json::Value::Object(obj));
|
||||
changed += 1;
|
||||
continue;
|
||||
};
|
||||
|
||||
let mut modified = false;
|
||||
let prev = existing
|
||||
.get("enabled")
|
||||
.and_then(|b| b.as_bool())
|
||||
.unwrap_or(false);
|
||||
if !prev {
|
||||
existing.insert(String::from("enabled"), json!(true));
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("server").is_none() {
|
||||
log::warn!("MCP 条目 '{}' 缺少 server 字段,覆盖为导入数据", id);
|
||||
existing.insert(String::from("server"), spec_v.clone());
|
||||
modified = true;
|
||||
}
|
||||
if existing.get("id").is_none() {
|
||||
log::warn!("MCP 条目 '{}' 缺少 id 字段,自动填充", id);
|
||||
existing.insert(String::from("id"), json!(id));
|
||||
modified = true;
|
||||
}
|
||||
if modified {
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
};
|
||||
|
||||
// 1) 处理 mcp.servers
|
||||
if let Some(mcp_val) = root.get("mcp") {
|
||||
if let Some(mcp_tbl) = mcp_val.as_table() {
|
||||
if let Some(servers_val) = mcp_tbl.get("servers") {
|
||||
if let Some(servers_tbl) = servers_val.as_table() {
|
||||
changed_total += import_servers_tbl(servers_tbl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 处理 mcp_servers
|
||||
if let Some(servers_val) = root.get("mcp_servers") {
|
||||
if let Some(servers_tbl) = servers_val.as_table() {
|
||||
changed_total += import_servers_tbl(servers_tbl);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(changed_total)
|
||||
}
|
||||
|
||||
/// 将 config.json 中 Codex 的 enabled==true 项以 TOML 形式写入 ~/.codex/config.toml 的 [mcp.servers]
|
||||
/// 策略:
|
||||
/// - 读取现有 config.toml;若语法无效则报错,不尝试覆盖
|
||||
/// - 仅更新 `mcp.servers` 或 `mcp_servers` 子表,保留 `mcp` 其它键
|
||||
/// - 仅写入启用项;无启用项时清理对应子表
|
||||
pub fn sync_enabled_to_codex(config: &MultiAppConfig) -> Result<(), AppError> {
|
||||
use toml_edit::{DocumentMut, Item, Table};
|
||||
|
||||
// 1) 收集启用项(Codex 维度)
|
||||
let enabled = collect_enabled_servers(&config.mcp.codex);
|
||||
|
||||
// 2) 读取现有 config.toml 文本;保持无效 TOML 的错误返回(不覆盖文件)
|
||||
let base_text = crate::codex_config::read_and_validate_codex_config_text()?;
|
||||
|
||||
// 3) 使用 toml_edit 解析(允许空文件)
|
||||
let mut doc: DocumentMut = if base_text.trim().is_empty() {
|
||||
DocumentMut::default()
|
||||
} else {
|
||||
base_text
|
||||
.parse::<DocumentMut>()
|
||||
.map_err(|e| AppError::McpValidation(format!("解析 config.toml 失败: {}", e)))?
|
||||
};
|
||||
|
||||
enum Target {
|
||||
McpServers, // 顶层 mcp_servers
|
||||
McpDotServers, // mcp.servers
|
||||
}
|
||||
|
||||
// 4) 选择目标风格:优先沿用既有子表;其次在 mcp 表下新建;最后退回顶层 mcp_servers
|
||||
let has_mcp_dot_servers = doc
|
||||
.get("mcp")
|
||||
.and_then(|m| m.get("servers"))
|
||||
.and_then(|s| s.as_table_like())
|
||||
.is_some();
|
||||
let has_mcp_servers = doc
|
||||
.get("mcp_servers")
|
||||
.and_then(|s| s.as_table_like())
|
||||
.is_some();
|
||||
let mcp_is_table = doc.get("mcp").and_then(|m| m.as_table_like()).is_some();
|
||||
|
||||
let target = if has_mcp_dot_servers {
|
||||
Target::McpDotServers
|
||||
} else if has_mcp_servers {
|
||||
Target::McpServers
|
||||
} else if mcp_is_table {
|
||||
Target::McpDotServers
|
||||
} else {
|
||||
Target::McpServers
|
||||
};
|
||||
|
||||
// 构造目标 servers 表(稳定的键顺序)
|
||||
let build_servers_table = || -> Table {
|
||||
let mut servers = Table::new();
|
||||
let mut ids: Vec<_> = enabled.keys().cloned().collect();
|
||||
ids.sort();
|
||||
for id in ids {
|
||||
let spec = enabled.get(&id).expect("spec must exist");
|
||||
let mut t = Table::new();
|
||||
let typ = spec.get("type").and_then(|v| v.as_str()).unwrap_or("stdio");
|
||||
t["type"] = toml_edit::value(typ);
|
||||
match typ {
|
||||
"stdio" => {
|
||||
let cmd = spec.get("command").and_then(|v| v.as_str()).unwrap_or("");
|
||||
t["command"] = toml_edit::value(cmd);
|
||||
if let Some(args) = spec.get("args").and_then(|v| v.as_array()) {
|
||||
let mut arr_v = toml_edit::Array::default();
|
||||
for a in args.iter().filter_map(|x| x.as_str()) {
|
||||
arr_v.push(a);
|
||||
}
|
||||
if !arr_v.is_empty() {
|
||||
t["args"] = toml_edit::Item::Value(toml_edit::Value::Array(arr_v));
|
||||
}
|
||||
}
|
||||
if let Some(cwd) = spec.get("cwd").and_then(|v| v.as_str()) {
|
||||
if !cwd.trim().is_empty() {
|
||||
t["cwd"] = toml_edit::value(cwd);
|
||||
}
|
||||
}
|
||||
if let Some(env) = spec.get("env").and_then(|v| v.as_object()) {
|
||||
let mut env_tbl = Table::new();
|
||||
for (k, v) in env.iter() {
|
||||
if let Some(s) = v.as_str() {
|
||||
env_tbl[&k[..]] = toml_edit::value(s);
|
||||
}
|
||||
}
|
||||
if !env_tbl.is_empty() {
|
||||
t["env"] = Item::Table(env_tbl);
|
||||
}
|
||||
}
|
||||
}
|
||||
"http" => {
|
||||
let url = spec.get("url").and_then(|v| v.as_str()).unwrap_or("");
|
||||
t["url"] = toml_edit::value(url);
|
||||
if let Some(headers) = spec.get("headers").and_then(|v| v.as_object()) {
|
||||
let mut h_tbl = Table::new();
|
||||
for (k, v) in headers.iter() {
|
||||
if let Some(s) = v.as_str() {
|
||||
h_tbl[&k[..]] = toml_edit::value(s);
|
||||
}
|
||||
}
|
||||
if !h_tbl.is_empty() {
|
||||
t["headers"] = Item::Table(h_tbl);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
servers[&id[..]] = Item::Table(t);
|
||||
}
|
||||
servers
|
||||
};
|
||||
|
||||
// 5) 应用更新:仅就地更新目标子表;避免改动其它键/注释/空白
|
||||
if enabled.is_empty() {
|
||||
// 无启用项:移除两种 servers 表(如果存在),但保留 mcp 其它字段
|
||||
if let Some(mcp_item) = doc.get_mut("mcp") {
|
||||
if let Some(tbl) = mcp_item.as_table_like_mut() {
|
||||
tbl.remove("servers");
|
||||
}
|
||||
}
|
||||
doc.as_table_mut().remove("mcp_servers");
|
||||
} else {
|
||||
let servers_tbl = build_servers_table();
|
||||
match target {
|
||||
Target::McpDotServers => {
|
||||
// 确保 mcp 为表
|
||||
if doc.get("mcp").and_then(|m| m.as_table_like()).is_none() {
|
||||
doc["mcp"] = Item::Table(Table::new());
|
||||
}
|
||||
doc["mcp"]["servers"] = Item::Table(servers_tbl);
|
||||
// 去重:若存在顶层 mcp_servers,则移除以避免重复定义
|
||||
doc.as_table_mut().remove("mcp_servers");
|
||||
}
|
||||
Target::McpServers => {
|
||||
doc["mcp_servers"] = Item::Table(servers_tbl);
|
||||
// 去重:若存在 mcp.servers,则移除该子表,保留 mcp 其它键
|
||||
if let Some(mcp_item) = doc.get_mut("mcp") {
|
||||
if let Some(tbl) = mcp_item.as_table_like_mut() {
|
||||
tbl.remove("servers");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6) 写回(仅改 TOML,不触碰 auth.json);toml_edit 会尽量保留未改区域的注释/空白/顺序
|
||||
let new_text = doc.to_string();
|
||||
let path = crate::codex_config::get_codex_config_path();
|
||||
crate::config::write_text_file(&path, &new_text)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::config::{
|
||||
backup_config, copy_file, delete_file, get_claude_settings_path, get_provider_config_path,
|
||||
read_json_file, write_json_file,
|
||||
};
|
||||
// SSOT 模式:不再写供应商副本文件
|
||||
|
||||
/// 供应商结构体
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -18,6 +14,17 @@ pub struct Provider {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "websiteUrl")]
|
||||
pub website_url: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub category: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: Option<i64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "sortIndex")]
|
||||
pub sort_index: Option<usize>,
|
||||
/// 供应商元数据(不写入 live 配置,仅存于 ~/.cc-switch/config.json)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub meta: Option<ProviderMeta>,
|
||||
}
|
||||
|
||||
impl Provider {
|
||||
@@ -33,145 +40,89 @@ impl Provider {
|
||||
name,
|
||||
settings_config,
|
||||
website_url,
|
||||
category: None,
|
||||
created_at: None,
|
||||
sort_index: None,
|
||||
meta: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 供应商管理器
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct ProviderManager {
|
||||
pub providers: HashMap<String, Provider>,
|
||||
pub current: String,
|
||||
}
|
||||
|
||||
impl Default for ProviderManager {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
providers: HashMap::new(),
|
||||
current: String::new(),
|
||||
}
|
||||
}
|
||||
/// 用量查询脚本配置
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UsageScript {
|
||||
pub enabled: bool,
|
||||
pub language: String,
|
||||
pub code: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub timeout: Option<u64>,
|
||||
/// 访问令牌(用于需要登录的接口)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "accessToken")]
|
||||
pub access_token: Option<String>,
|
||||
/// 用户ID(用于需要用户标识的接口)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "userId")]
|
||||
pub user_id: Option<String>,
|
||||
/// 自动查询间隔(单位:分钟,0 表示禁用自动查询)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "autoQueryInterval")]
|
||||
pub auto_query_interval: Option<u64>,
|
||||
}
|
||||
|
||||
/// 用量数据
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UsageData {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "planName")]
|
||||
pub plan_name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub extra: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "isValid")]
|
||||
pub is_valid: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "invalidMessage")]
|
||||
pub invalid_message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub total: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub used: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub remaining: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub unit: Option<String>,
|
||||
}
|
||||
|
||||
/// 用量查询结果(支持多套餐)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UsageResult {
|
||||
pub success: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<Vec<UsageData>>, // 支持返回多个套餐
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// 供应商元数据
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct ProviderMeta {
|
||||
/// 自定义端点列表(按 URL 去重存储)
|
||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
||||
pub custom_endpoints: HashMap<String, crate::settings::CustomEndpoint>,
|
||||
/// 用量查询脚本配置
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub usage_script: Option<UsageScript>,
|
||||
}
|
||||
|
||||
impl ProviderManager {
|
||||
/// 加载供应商列表
|
||||
pub fn load_from_file(path: &Path) -> Result<Self, String> {
|
||||
if !path.exists() {
|
||||
log::info!("配置文件不存在,创建新的供应商管理器");
|
||||
return Ok(Self::default());
|
||||
}
|
||||
|
||||
read_json_file(path)
|
||||
}
|
||||
|
||||
/// 保存供应商列表
|
||||
pub fn save_to_file(&self, path: &Path) -> Result<(), String> {
|
||||
write_json_file(path, self)
|
||||
}
|
||||
|
||||
/// 添加供应商
|
||||
pub fn add_provider(&mut self, provider: Provider) -> Result<(), String> {
|
||||
// 保存供应商配置到独立文件
|
||||
let config_path = get_provider_config_path(&provider.id, Some(&provider.name));
|
||||
write_json_file(&config_path, &provider.settings_config)?;
|
||||
|
||||
// 添加到管理器
|
||||
self.providers.insert(provider.id.clone(), provider);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 更新供应商
|
||||
pub fn update_provider(&mut self, provider: Provider) -> Result<(), String> {
|
||||
// 检查供应商是否存在
|
||||
if !self.providers.contains_key(&provider.id) {
|
||||
return Err(format!("供应商不存在: {}", provider.id));
|
||||
}
|
||||
|
||||
// 如果名称改变了,需要处理配置文件
|
||||
if let Some(old_provider) = self.providers.get(&provider.id) {
|
||||
if old_provider.name != provider.name {
|
||||
// 删除旧配置文件
|
||||
let old_config_path =
|
||||
get_provider_config_path(&provider.id, Some(&old_provider.name));
|
||||
delete_file(&old_config_path).ok(); // 忽略删除错误
|
||||
}
|
||||
}
|
||||
|
||||
// 保存新配置文件
|
||||
let config_path = get_provider_config_path(&provider.id, Some(&provider.name));
|
||||
write_json_file(&config_path, &provider.settings_config)?;
|
||||
|
||||
// 更新管理器
|
||||
self.providers.insert(provider.id.clone(), provider);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 删除供应商
|
||||
pub fn delete_provider(&mut self, provider_id: &str) -> Result<(), String> {
|
||||
// 检查是否为当前供应商
|
||||
if self.current == provider_id {
|
||||
return Err("不能删除当前正在使用的供应商".to_string());
|
||||
}
|
||||
|
||||
// 获取供应商信息
|
||||
let provider = self
|
||||
.providers
|
||||
.get(provider_id)
|
||||
.ok_or_else(|| format!("供应商不存在: {}", provider_id))?;
|
||||
|
||||
// 删除配置文件
|
||||
let config_path = get_provider_config_path(provider_id, Some(&provider.name));
|
||||
delete_file(&config_path)?;
|
||||
|
||||
// 从管理器删除
|
||||
self.providers.remove(provider_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 切换供应商
|
||||
pub fn switch_provider(&mut self, provider_id: &str) -> Result<(), String> {
|
||||
// 检查供应商是否存在
|
||||
let provider = self
|
||||
.providers
|
||||
.get(provider_id)
|
||||
.ok_or_else(|| format!("供应商不存在: {}", provider_id))?;
|
||||
|
||||
let settings_path = get_claude_settings_path();
|
||||
let provider_config_path = get_provider_config_path(provider_id, Some(&provider.name));
|
||||
|
||||
// 检查供应商配置文件是否存在
|
||||
if !provider_config_path.exists() {
|
||||
return Err(format!(
|
||||
"供应商配置文件不存在: {}",
|
||||
provider_config_path.display()
|
||||
));
|
||||
}
|
||||
|
||||
// 如果当前有配置,先备份到当前供应商
|
||||
if settings_path.exists() && !self.current.is_empty() {
|
||||
if let Some(current_provider) = self.providers.get(&self.current) {
|
||||
let current_provider_path =
|
||||
get_provider_config_path(&self.current, Some(¤t_provider.name));
|
||||
backup_config(&settings_path, ¤t_provider_path)?;
|
||||
log::info!("已备份当前供应商配置: {}", current_provider.name);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保主配置父目录存在
|
||||
if let Some(parent) = settings_path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
}
|
||||
|
||||
// 复制新供应商配置到主配置
|
||||
copy_file(&provider_config_path, &settings_path)?;
|
||||
|
||||
// 更新当前供应商
|
||||
self.current = provider_id.to_string();
|
||||
|
||||
log::info!("成功切换到供应商: {}", provider.name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取所有供应商
|
||||
pub fn get_all_providers(&self) -> &HashMap<String, Provider> {
|
||||
&self.providers
|
||||
|
||||
229
src-tauri/src/services/config.rs
Normal file
@@ -0,0 +1,229 @@
|
||||
use crate::app_config::{AppType, MultiAppConfig};
|
||||
use crate::error::AppError;
|
||||
use crate::provider::Provider;
|
||||
use crate::store::AppState;
|
||||
use chrono::Utc;
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
const MAX_BACKUPS: usize = 10;
|
||||
|
||||
/// 配置导入导出相关业务逻辑
|
||||
pub struct ConfigService;
|
||||
|
||||
impl ConfigService {
|
||||
/// 为当前 config.json 创建备份,返回备份 ID(若文件不存在则返回空字符串)。
|
||||
pub fn create_backup(config_path: &Path) -> Result<String, AppError> {
|
||||
if !config_path.exists() {
|
||||
return Ok(String::new());
|
||||
}
|
||||
|
||||
let timestamp = Utc::now().format("%Y%m%d_%H%M%S");
|
||||
let backup_id = format!("backup_{}", timestamp);
|
||||
|
||||
let backup_dir = config_path
|
||||
.parent()
|
||||
.ok_or_else(|| AppError::Config("Invalid config path".into()))?
|
||||
.join("backups");
|
||||
|
||||
fs::create_dir_all(&backup_dir).map_err(|e| AppError::io(&backup_dir, e))?;
|
||||
|
||||
let backup_path = backup_dir.join(format!("{}.json", backup_id));
|
||||
let contents = fs::read(config_path).map_err(|e| AppError::io(config_path, e))?;
|
||||
fs::write(&backup_path, contents).map_err(|e| AppError::io(&backup_path, e))?;
|
||||
|
||||
Self::cleanup_old_backups(&backup_dir, MAX_BACKUPS)?;
|
||||
|
||||
Ok(backup_id)
|
||||
}
|
||||
|
||||
fn cleanup_old_backups(backup_dir: &Path, retain: usize) -> Result<(), AppError> {
|
||||
if retain == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let entries = match fs::read_dir(backup_dir) {
|
||||
Ok(iter) => iter
|
||||
.filter_map(|entry| entry.ok())
|
||||
.filter(|entry| {
|
||||
entry
|
||||
.path()
|
||||
.extension()
|
||||
.map(|ext| ext == "json")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
Err(_) => return Ok(()),
|
||||
};
|
||||
|
||||
if entries.len() <= retain {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let remove_count = entries.len().saturating_sub(retain);
|
||||
let mut sorted = entries;
|
||||
|
||||
sorted.sort_by(|a, b| {
|
||||
let a_time = a.metadata().and_then(|m| m.modified()).ok();
|
||||
let b_time = b.metadata().and_then(|m| m.modified()).ok();
|
||||
a_time.cmp(&b_time)
|
||||
});
|
||||
|
||||
for entry in sorted.into_iter().take(remove_count) {
|
||||
if let Err(err) = fs::remove_file(entry.path()) {
|
||||
log::warn!(
|
||||
"Failed to remove old backup {}: {}",
|
||||
entry.path().display(),
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 将当前 config.json 拷贝到目标路径。
|
||||
pub fn export_config_to_path(target_path: &Path) -> Result<(), AppError> {
|
||||
let config_path = crate::config::get_app_config_path();
|
||||
let config_content =
|
||||
fs::read_to_string(&config_path).map_err(|e| AppError::io(&config_path, e))?;
|
||||
fs::write(target_path, config_content).map_err(|e| AppError::io(target_path, e))
|
||||
}
|
||||
|
||||
/// 从磁盘文件加载配置并写回 config.json,返回备份 ID 及新配置。
|
||||
pub fn load_config_for_import(file_path: &Path) -> Result<(MultiAppConfig, String), AppError> {
|
||||
let import_content =
|
||||
fs::read_to_string(file_path).map_err(|e| AppError::io(file_path, e))?;
|
||||
|
||||
let new_config: MultiAppConfig =
|
||||
serde_json::from_str(&import_content).map_err(|e| AppError::json(file_path, e))?;
|
||||
|
||||
let config_path = crate::config::get_app_config_path();
|
||||
let backup_id = Self::create_backup(&config_path)?;
|
||||
|
||||
fs::write(&config_path, &import_content).map_err(|e| AppError::io(&config_path, e))?;
|
||||
|
||||
Ok((new_config, backup_id))
|
||||
}
|
||||
|
||||
/// 将外部配置文件内容加载并写入应用状态。
|
||||
pub fn import_config_from_path(file_path: &Path, state: &AppState) -> Result<String, AppError> {
|
||||
let (new_config, backup_id) = Self::load_config_for_import(file_path)?;
|
||||
|
||||
{
|
||||
let mut guard = state.config.write().map_err(AppError::from)?;
|
||||
*guard = new_config;
|
||||
}
|
||||
|
||||
Ok(backup_id)
|
||||
}
|
||||
|
||||
/// 同步当前供应商到对应的 live 配置。
|
||||
pub fn sync_current_providers_to_live(config: &mut MultiAppConfig) -> Result<(), AppError> {
|
||||
Self::sync_current_provider_for_app(config, &AppType::Claude)?;
|
||||
Self::sync_current_provider_for_app(config, &AppType::Codex)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_current_provider_for_app(
|
||||
config: &mut MultiAppConfig,
|
||||
app_type: &AppType,
|
||||
) -> Result<(), AppError> {
|
||||
let (current_id, provider) = {
|
||||
let manager = match config.get_manager(app_type) {
|
||||
Some(manager) => manager,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
if manager.current.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let current_id = manager.current.clone();
|
||||
let provider = match manager.providers.get(¤t_id) {
|
||||
Some(provider) => provider.clone(),
|
||||
None => {
|
||||
log::warn!(
|
||||
"当前应用 {:?} 的供应商 {} 不存在,跳过 live 同步",
|
||||
app_type,
|
||||
current_id
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
(current_id, provider)
|
||||
};
|
||||
|
||||
match app_type {
|
||||
AppType::Codex => Self::sync_codex_live(config, ¤t_id, &provider)?,
|
||||
AppType::Claude => Self::sync_claude_live(config, ¤t_id, &provider)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_codex_live(
|
||||
config: &mut MultiAppConfig,
|
||||
provider_id: &str,
|
||||
provider: &Provider,
|
||||
) -> Result<(), AppError> {
|
||||
let settings = provider.settings_config.as_object().ok_or_else(|| {
|
||||
AppError::Config(format!("供应商 {} 的 Codex 配置必须是对象", provider_id))
|
||||
})?;
|
||||
let auth = settings.get("auth").ok_or_else(|| {
|
||||
AppError::Config(format!(
|
||||
"供应商 {} 的 Codex 配置缺少 auth 字段",
|
||||
provider_id
|
||||
))
|
||||
})?;
|
||||
if !auth.is_object() {
|
||||
return Err(AppError::Config(format!(
|
||||
"供应商 {} 的 Codex auth 配置必须是 JSON 对象",
|
||||
provider_id
|
||||
)));
|
||||
}
|
||||
let cfg_text = settings.get("config").and_then(Value::as_str);
|
||||
|
||||
crate::codex_config::write_codex_live_atomic(auth, cfg_text)?;
|
||||
crate::mcp::sync_enabled_to_codex(config)?;
|
||||
|
||||
let cfg_text_after = crate::codex_config::read_and_validate_codex_config_text()?;
|
||||
if let Some(manager) = config.get_manager_mut(&AppType::Codex) {
|
||||
if let Some(target) = manager.providers.get_mut(provider_id) {
|
||||
if let Some(obj) = target.settings_config.as_object_mut() {
|
||||
obj.insert(
|
||||
"config".to_string(),
|
||||
serde_json::Value::String(cfg_text_after),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_claude_live(
|
||||
config: &mut MultiAppConfig,
|
||||
provider_id: &str,
|
||||
provider: &Provider,
|
||||
) -> Result<(), AppError> {
|
||||
use crate::config::{read_json_file, write_json_file};
|
||||
|
||||
let settings_path = crate::config::get_claude_settings_path();
|
||||
if let Some(parent) = settings_path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
|
||||
write_json_file(&settings_path, &provider.settings_config)?;
|
||||
|
||||
let live_after = read_json_file::<serde_json::Value>(&settings_path)?;
|
||||
if let Some(manager) = config.get_manager_mut(&AppType::Claude) {
|
||||
if let Some(target) = manager.providers.get_mut(provider_id) {
|
||||
target.settings_config = live_after;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
191
src-tauri/src/services/mcp.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::app_config::{AppType, MultiAppConfig};
|
||||
use crate::error::AppError;
|
||||
use crate::mcp;
|
||||
use crate::store::AppState;
|
||||
|
||||
/// MCP 相关业务逻辑
|
||||
pub struct McpService;
|
||||
|
||||
impl McpService {
|
||||
/// 获取指定应用的 MCP 服务器快照,并在必要时回写归一化后的配置。
|
||||
pub fn get_servers(state: &AppState, app: AppType) -> Result<HashMap<String, Value>, AppError> {
|
||||
let mut cfg = state.config.write()?;
|
||||
let (snapshot, normalized) = mcp::get_servers_snapshot_for(&mut cfg, &app);
|
||||
drop(cfg);
|
||||
if normalized > 0 {
|
||||
state.save()?;
|
||||
}
|
||||
Ok(snapshot)
|
||||
}
|
||||
|
||||
/// 在 config.json 中新增或更新指定 MCP 服务器,并按需同步到对应客户端。
|
||||
pub fn upsert_server(
|
||||
state: &AppState,
|
||||
app: AppType,
|
||||
id: &str,
|
||||
spec: Value,
|
||||
sync_other_side: bool,
|
||||
) -> Result<bool, AppError> {
|
||||
let (changed, snapshot, sync_claude, sync_codex): (
|
||||
bool,
|
||||
Option<MultiAppConfig>,
|
||||
bool,
|
||||
bool,
|
||||
) = {
|
||||
let mut cfg = state.config.write()?;
|
||||
let changed = mcp::upsert_in_config_for(&mut cfg, &app, id, spec)?;
|
||||
|
||||
// 修复:默认启用(unwrap_or(true))
|
||||
// 新增的 MCP 如果缺少 enabled 字段,应该默认为启用状态
|
||||
let enabled = cfg
|
||||
.mcp_for(&app)
|
||||
.servers
|
||||
.get(id)
|
||||
.and_then(|entry| entry.get("enabled"))
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(true);
|
||||
|
||||
let mut sync_claude = matches!(app, AppType::Claude) && enabled;
|
||||
let mut sync_codex = matches!(app, AppType::Codex) && enabled;
|
||||
|
||||
// 修复:sync_other_side=true 时,先将 MCP 复制到另一侧,然后强制同步
|
||||
// 这才是"同步到另一侧"的正确语义:将 MCP 跨应用复制
|
||||
if sync_other_side {
|
||||
// 获取当前 MCP 条目的克隆(刚刚插入的,不可能失败)
|
||||
let current_entry = cfg
|
||||
.mcp_for(&app)
|
||||
.servers
|
||||
.get(id)
|
||||
.cloned()
|
||||
.expect("刚刚插入的 MCP 条目必定存在");
|
||||
|
||||
// 将该 MCP 复制到另一侧的 servers
|
||||
let other_app = match app {
|
||||
AppType::Claude => AppType::Codex,
|
||||
AppType::Codex => AppType::Claude,
|
||||
};
|
||||
|
||||
cfg.mcp_for_mut(&other_app)
|
||||
.servers
|
||||
.insert(id.to_string(), current_entry);
|
||||
|
||||
// 强制同步另一侧
|
||||
match app {
|
||||
AppType::Claude => sync_codex = true,
|
||||
AppType::Codex => sync_claude = true,
|
||||
}
|
||||
}
|
||||
|
||||
let snapshot = if sync_claude || sync_codex {
|
||||
Some(cfg.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
(changed, snapshot, sync_claude, sync_codex)
|
||||
};
|
||||
|
||||
// 保持原有行为:始终尝试持久化,避免遗漏 normalize 带来的隐式变更
|
||||
state.save()?;
|
||||
|
||||
if let Some(snapshot) = snapshot {
|
||||
if sync_claude {
|
||||
mcp::sync_enabled_to_claude(&snapshot)?;
|
||||
}
|
||||
if sync_codex {
|
||||
mcp::sync_enabled_to_codex(&snapshot)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// 删除 config.json 中的 MCP 服务器条目,并同步客户端配置。
|
||||
pub fn delete_server(state: &AppState, app: AppType, id: &str) -> Result<bool, AppError> {
|
||||
let (existed, snapshot): (bool, Option<MultiAppConfig>) = {
|
||||
let mut cfg = state.config.write()?;
|
||||
let existed = mcp::delete_in_config_for(&mut cfg, &app, id)?;
|
||||
let snapshot = if existed { Some(cfg.clone()) } else { None };
|
||||
(existed, snapshot)
|
||||
};
|
||||
if existed {
|
||||
state.save()?;
|
||||
if let Some(snapshot) = snapshot {
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(existed)
|
||||
}
|
||||
|
||||
/// 设置 MCP 启用状态,并同步到客户端配置。
|
||||
pub fn set_enabled(
|
||||
state: &AppState,
|
||||
app: AppType,
|
||||
id: &str,
|
||||
enabled: bool,
|
||||
) -> Result<bool, AppError> {
|
||||
let (existed, snapshot): (bool, Option<MultiAppConfig>) = {
|
||||
let mut cfg = state.config.write()?;
|
||||
let existed = mcp::set_enabled_flag_for(&mut cfg, &app, id, enabled)?;
|
||||
let snapshot = if existed { Some(cfg.clone()) } else { None };
|
||||
(existed, snapshot)
|
||||
};
|
||||
|
||||
if existed {
|
||||
state.save()?;
|
||||
if let Some(snapshot) = snapshot {
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(existed)
|
||||
}
|
||||
|
||||
/// 手动同步已启用的 MCP 服务器到客户端配置。
|
||||
pub fn sync_enabled(state: &AppState, app: AppType) -> Result<(), AppError> {
|
||||
let (snapshot, normalized): (MultiAppConfig, usize) = {
|
||||
let mut cfg = state.config.write()?;
|
||||
let normalized = mcp::normalize_servers_for(&mut cfg, &app);
|
||||
(cfg.clone(), normalized)
|
||||
};
|
||||
if normalized > 0 {
|
||||
state.save()?;
|
||||
}
|
||||
match app {
|
||||
AppType::Claude => mcp::sync_enabled_to_claude(&snapshot)?,
|
||||
AppType::Codex => mcp::sync_enabled_to_codex(&snapshot)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 从 Claude 客户端配置导入 MCP 定义。
|
||||
pub fn import_from_claude(state: &AppState) -> Result<usize, AppError> {
|
||||
let mut cfg = state.config.write()?;
|
||||
let changed = mcp::import_from_claude(&mut cfg)?;
|
||||
drop(cfg);
|
||||
if changed > 0 {
|
||||
state.save()?;
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// 从 Codex 客户端配置导入 MCP 定义。
|
||||
pub fn import_from_codex(state: &AppState) -> Result<usize, AppError> {
|
||||
let mut cfg = state.config.write()?;
|
||||
let changed = mcp::import_from_codex(&mut cfg)?;
|
||||
drop(cfg);
|
||||
if changed > 0 {
|
||||
state.save()?;
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
}
|
||||
9
src-tauri/src/services/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub mod config;
|
||||
pub mod mcp;
|
||||
pub mod provider;
|
||||
pub mod speedtest;
|
||||
|
||||
pub use config::ConfigService;
|
||||
pub use mcp::McpService;
|
||||
pub use provider::{ProviderService, ProviderSortUpdate};
|
||||
pub use speedtest::{EndpointLatency, SpeedtestService};
|
||||
1328
src-tauri/src/services/provider.rs
Normal file
174
src-tauri/src/services/speedtest.rs
Normal file
@@ -0,0 +1,174 @@
|
||||
use futures::future::join_all;
|
||||
use reqwest::{Client, Url};
|
||||
use serde::Serialize;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
const DEFAULT_TIMEOUT_SECS: u64 = 8;
|
||||
const MAX_TIMEOUT_SECS: u64 = 30;
|
||||
const MIN_TIMEOUT_SECS: u64 = 2;
|
||||
|
||||
/// 端点测速结果
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct EndpointLatency {
|
||||
pub url: String,
|
||||
pub latency: Option<u128>,
|
||||
pub status: Option<u16>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// 网络测速相关业务
|
||||
pub struct SpeedtestService;
|
||||
|
||||
impl SpeedtestService {
|
||||
/// 测试一组端点的响应延迟。
|
||||
pub async fn test_endpoints(
|
||||
urls: Vec<String>,
|
||||
timeout_secs: Option<u64>,
|
||||
) -> Result<Vec<EndpointLatency>, AppError> {
|
||||
if urls.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let timeout = Self::sanitize_timeout(timeout_secs);
|
||||
let client = Self::build_client(timeout)?;
|
||||
|
||||
let tasks = urls.into_iter().map(|raw_url| {
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let trimmed = raw_url.trim().to_string();
|
||||
if trimmed.is_empty() {
|
||||
return EndpointLatency {
|
||||
url: raw_url,
|
||||
latency: None,
|
||||
status: None,
|
||||
error: Some("URL 不能为空".to_string()),
|
||||
};
|
||||
}
|
||||
|
||||
let parsed_url = match Url::parse(&trimmed) {
|
||||
Ok(url) => url,
|
||||
Err(err) => {
|
||||
return EndpointLatency {
|
||||
url: trimmed,
|
||||
latency: None,
|
||||
status: None,
|
||||
error: Some(format!("URL 无效: {err}")),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// 先进行一次热身请求,忽略结果,仅用于复用连接/绕过首包惩罚。
|
||||
let _ = client.get(parsed_url.clone()).send().await;
|
||||
|
||||
// 第二次请求开始计时,并将其作为结果返回。
|
||||
let start = Instant::now();
|
||||
match client.get(parsed_url).send().await {
|
||||
Ok(resp) => EndpointLatency {
|
||||
url: trimmed,
|
||||
latency: Some(start.elapsed().as_millis()),
|
||||
status: Some(resp.status().as_u16()),
|
||||
error: None,
|
||||
},
|
||||
Err(err) => {
|
||||
let status = err.status().map(|s| s.as_u16());
|
||||
let error_message = if err.is_timeout() {
|
||||
"请求超时".to_string()
|
||||
} else if err.is_connect() {
|
||||
"连接失败".to_string()
|
||||
} else {
|
||||
err.to_string()
|
||||
};
|
||||
|
||||
EndpointLatency {
|
||||
url: trimmed,
|
||||
latency: None,
|
||||
status,
|
||||
error: Some(error_message),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(join_all(tasks).await)
|
||||
}
|
||||
|
||||
fn build_client(timeout_secs: u64) -> Result<Client, AppError> {
|
||||
Client::builder()
|
||||
.timeout(Duration::from_secs(timeout_secs))
|
||||
.redirect(reqwest::redirect::Policy::limited(5))
|
||||
.user_agent("cc-switch-speedtest/1.0")
|
||||
.build()
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"speedtest.client_create_failed",
|
||||
format!("创建 HTTP 客户端失败: {e}"),
|
||||
format!("Failed to create HTTP client: {e}"),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn sanitize_timeout(timeout_secs: Option<u64>) -> u64 {
|
||||
let secs = timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS);
|
||||
secs.clamp(MIN_TIMEOUT_SECS, MAX_TIMEOUT_SECS)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sanitize_timeout_clamps_values() {
|
||||
assert_eq!(
|
||||
SpeedtestService::sanitize_timeout(Some(1)),
|
||||
MIN_TIMEOUT_SECS
|
||||
);
|
||||
assert_eq!(
|
||||
SpeedtestService::sanitize_timeout(Some(999)),
|
||||
MAX_TIMEOUT_SECS
|
||||
);
|
||||
assert_eq!(
|
||||
SpeedtestService::sanitize_timeout(Some(10)),
|
||||
10.clamp(MIN_TIMEOUT_SECS, MAX_TIMEOUT_SECS)
|
||||
);
|
||||
assert_eq!(
|
||||
SpeedtestService::sanitize_timeout(None),
|
||||
DEFAULT_TIMEOUT_SECS
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoints_handles_empty_list() {
|
||||
let result =
|
||||
tauri::async_runtime::block_on(SpeedtestService::test_endpoints(Vec::new(), Some(5)))
|
||||
.expect("empty list should succeed");
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoints_reports_invalid_url() {
|
||||
let result = tauri::async_runtime::block_on(SpeedtestService::test_endpoints(
|
||||
vec!["not a url".into(), "".into()],
|
||||
None,
|
||||
))
|
||||
.expect("invalid inputs should still succeed");
|
||||
|
||||
assert_eq!(result.len(), 2);
|
||||
assert!(
|
||||
result[0]
|
||||
.error
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.starts_with("URL 无效"),
|
||||
"invalid url should yield parse error"
|
||||
);
|
||||
assert_eq!(
|
||||
result[1].error.as_deref(),
|
||||
Some("URL 不能为空"),
|
||||
"empty url should report validation error"
|
||||
);
|
||||
}
|
||||
}
|
||||
188
src-tauri/src/settings.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{OnceLock, RwLock};
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
/// 自定义端点配置
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CustomEndpoint {
|
||||
pub url: String,
|
||||
pub added_at: i64,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub last_used: Option<i64>,
|
||||
}
|
||||
|
||||
/// 应用设置结构,允许覆盖默认配置目录
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AppSettings {
|
||||
#[serde(default = "default_show_in_tray")]
|
||||
pub show_in_tray: bool,
|
||||
#[serde(default = "default_minimize_to_tray_on_close")]
|
||||
pub minimize_to_tray_on_close: bool,
|
||||
/// 是否启用 Claude 插件联动
|
||||
#[serde(default)]
|
||||
pub enable_claude_plugin_integration: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub claude_config_dir: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub codex_config_dir: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub language: Option<String>,
|
||||
/// Claude 自定义端点列表
|
||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
||||
pub custom_endpoints_claude: HashMap<String, CustomEndpoint>,
|
||||
/// Codex 自定义端点列表
|
||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
||||
pub custom_endpoints_codex: HashMap<String, CustomEndpoint>,
|
||||
}
|
||||
|
||||
fn default_show_in_tray() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_minimize_to_tray_on_close() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl Default for AppSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
show_in_tray: true,
|
||||
minimize_to_tray_on_close: true,
|
||||
enable_claude_plugin_integration: false,
|
||||
claude_config_dir: None,
|
||||
codex_config_dir: None,
|
||||
language: None,
|
||||
custom_endpoints_claude: HashMap::new(),
|
||||
custom_endpoints_codex: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppSettings {
|
||||
fn settings_path() -> PathBuf {
|
||||
// settings.json 必须使用固定路径,不能被 app_config_dir 覆盖
|
||||
// 否则会造成循环依赖:读取 settings 需要知道路径,但路径在 settings 中
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".cc-switch")
|
||||
.join("settings.json")
|
||||
}
|
||||
|
||||
fn normalize_paths(&mut self) {
|
||||
self.claude_config_dir = self
|
||||
.claude_config_dir
|
||||
.as_ref()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.to_string());
|
||||
|
||||
self.codex_config_dir = self
|
||||
.codex_config_dir
|
||||
.as_ref()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.to_string());
|
||||
|
||||
self.language = self
|
||||
.language
|
||||
.as_ref()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| matches!(*s, "en" | "zh"))
|
||||
.map(|s| s.to_string());
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = Self::settings_path();
|
||||
if let Ok(content) = fs::read_to_string(&path) {
|
||||
match serde_json::from_str::<AppSettings>(&content) {
|
||||
Ok(mut settings) => {
|
||||
settings.normalize_paths();
|
||||
settings
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!(
|
||||
"解析设置文件失败,将使用默认设置。路径: {}, 错误: {}",
|
||||
path.display(),
|
||||
err
|
||||
);
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<(), AppError> {
|
||||
let mut normalized = self.clone();
|
||||
normalized.normalize_paths();
|
||||
let path = Self::settings_path();
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||||
}
|
||||
|
||||
let json = serde_json::to_string_pretty(&normalized)
|
||||
.map_err(|e| AppError::JsonSerialize { source: e })?;
|
||||
fs::write(&path, json).map_err(|e| AppError::io(&path, e))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn settings_store() -> &'static RwLock<AppSettings> {
|
||||
static STORE: OnceLock<RwLock<AppSettings>> = OnceLock::new();
|
||||
STORE.get_or_init(|| RwLock::new(AppSettings::load()))
|
||||
}
|
||||
|
||||
fn resolve_override_path(raw: &str) -> PathBuf {
|
||||
if raw == "~" {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home;
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~/") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~\\") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
}
|
||||
|
||||
PathBuf::from(raw)
|
||||
}
|
||||
|
||||
pub fn get_settings() -> AppSettings {
|
||||
settings_store().read().expect("读取设置锁失败").clone()
|
||||
}
|
||||
|
||||
pub fn update_settings(mut new_settings: AppSettings) -> Result<(), AppError> {
|
||||
new_settings.normalize_paths();
|
||||
new_settings.save()?;
|
||||
|
||||
let mut guard = settings_store().write().expect("写入设置锁失败");
|
||||
*guard = new_settings;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_claude_override_dir() -> Option<PathBuf> {
|
||||
let settings = settings_store().read().ok()?;
|
||||
settings
|
||||
.claude_config_dir
|
||||
.as_ref()
|
||||
.map(|p| resolve_override_path(p))
|
||||
}
|
||||
|
||||
pub fn get_codex_override_dir() -> Option<PathBuf> {
|
||||
let settings = settings_store().read().ok()?;
|
||||
settings
|
||||
.codex_config_dir
|
||||
.as_ref()
|
||||
.map(|p| resolve_override_path(p))
|
||||
}
|
||||
@@ -1,36 +1,26 @@
|
||||
use crate::config::get_app_config_path;
|
||||
use crate::provider::ProviderManager;
|
||||
use std::sync::Mutex;
|
||||
use crate::app_config::MultiAppConfig;
|
||||
use crate::error::AppError;
|
||||
use std::sync::RwLock;
|
||||
|
||||
/// 全局应用状态
|
||||
pub struct AppState {
|
||||
pub provider_manager: Mutex<ProviderManager>,
|
||||
pub config: RwLock<MultiAppConfig>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
/// 创建新的应用状态
|
||||
pub fn new() -> Self {
|
||||
let config_path = get_app_config_path();
|
||||
let provider_manager = ProviderManager::load_from_file(&config_path).unwrap_or_else(|e| {
|
||||
log::warn!("加载配置失败: {}, 使用默认配置", e);
|
||||
ProviderManager::default()
|
||||
});
|
||||
|
||||
Self {
|
||||
provider_manager: Mutex::new(provider_manager),
|
||||
}
|
||||
/// 注意:仅在配置成功加载时返回;不会在失败时回退默认值。
|
||||
pub fn try_new() -> Result<Self, AppError> {
|
||||
let config = MultiAppConfig::load()?;
|
||||
Ok(Self {
|
||||
config: RwLock::new(config),
|
||||
})
|
||||
}
|
||||
|
||||
/// 保存配置到文件
|
||||
pub fn save(&self) -> Result<(), String> {
|
||||
let config_path = get_app_config_path();
|
||||
let manager = self
|
||||
.provider_manager
|
||||
.lock()
|
||||
.map_err(|e| format!("获取锁失败: {}", e))?;
|
||||
pub fn save(&self) -> Result<(), AppError> {
|
||||
let config = self.config.read().map_err(AppError::from)?;
|
||||
|
||||
manager.save_to_file(&config_path)
|
||||
config.save()
|
||||
}
|
||||
|
||||
// 保留按需扩展:若未来需要热加载,可在此实现
|
||||
}
|
||||
|
||||
396
src-tauri/src/usage_script.rs
Normal file
@@ -0,0 +1,396 @@
|
||||
use reqwest::Client;
|
||||
use rquickjs::{Context, Function, Runtime};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::error::AppError;
|
||||
|
||||
/// 执行用量查询脚本
|
||||
pub async fn execute_usage_script(
|
||||
script_code: &str,
|
||||
api_key: &str,
|
||||
base_url: &str,
|
||||
timeout_secs: u64,
|
||||
access_token: Option<&str>,
|
||||
user_id: Option<&str>,
|
||||
) -> Result<Value, AppError> {
|
||||
// 1. 替换变量
|
||||
let mut replaced = script_code
|
||||
.replace("{{apiKey}}", api_key)
|
||||
.replace("{{baseUrl}}", base_url);
|
||||
|
||||
// 替换 accessToken 和 userId
|
||||
if let Some(token) = access_token {
|
||||
replaced = replaced.replace("{{accessToken}}", token);
|
||||
}
|
||||
if let Some(uid) = user_id {
|
||||
replaced = replaced.replace("{{userId}}", uid);
|
||||
}
|
||||
|
||||
// 2. 在独立作用域中提取 request 配置(确保 Runtime/Context 在 await 前释放)
|
||||
let request_config = {
|
||||
let runtime = Runtime::new().map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.runtime_create_failed",
|
||||
format!("创建 JS 运行时失败: {}", e),
|
||||
format!("Failed to create JS runtime: {}", e),
|
||||
)
|
||||
})?;
|
||||
let context = Context::full(&runtime).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.context_create_failed",
|
||||
format!("创建 JS 上下文失败: {}", e),
|
||||
format!("Failed to create JS context: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
context.with(|ctx| {
|
||||
// 执行用户代码,获取配置对象
|
||||
let config: rquickjs::Object = ctx.eval(replaced.clone()).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.config_parse_failed",
|
||||
format!("解析配置失败: {}", e),
|
||||
format!("Failed to parse config: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 提取 request 配置
|
||||
let request: rquickjs::Object = config.get("request").map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.request_missing",
|
||||
format!("缺少 request 配置: {}", e),
|
||||
format!("Missing request config: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 将 request 转换为 JSON 字符串
|
||||
let request_json: String = ctx
|
||||
.json_stringify(request)
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.request_serialize_failed",
|
||||
format!("序列化 request 失败: {}", e),
|
||||
format!("Failed to serialize request: {}", e),
|
||||
)
|
||||
})?
|
||||
.ok_or_else(|| {
|
||||
AppError::localized(
|
||||
"usage_script.serialize_none",
|
||||
"序列化返回 None",
|
||||
"Serialization returned None",
|
||||
)
|
||||
})?
|
||||
.get()
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.get_string_failed",
|
||||
format!("获取字符串失败: {}", e),
|
||||
format!("Failed to get string: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok::<_, AppError>(request_json)
|
||||
})?
|
||||
}; // Runtime 和 Context 在这里被 drop
|
||||
|
||||
// 3. 解析 request 配置
|
||||
let request: RequestConfig = serde_json::from_str(&request_config).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.request_format_invalid",
|
||||
format!("request 配置格式错误: {}", e),
|
||||
format!("Invalid request config format: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 4. 发送 HTTP 请求
|
||||
let response_data = send_http_request(&request, timeout_secs).await?;
|
||||
|
||||
// 5. 在独立作用域中执行 extractor(确保 Runtime/Context 在函数结束前释放)
|
||||
let result: Value = {
|
||||
let runtime = Runtime::new().map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.runtime_create_failed",
|
||||
format!("创建 JS 运行时失败: {}", e),
|
||||
format!("Failed to create JS runtime: {}", e),
|
||||
)
|
||||
})?;
|
||||
let context = Context::full(&runtime).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.context_create_failed",
|
||||
format!("创建 JS 上下文失败: {}", e),
|
||||
format!("Failed to create JS context: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
context.with(|ctx| {
|
||||
// 重新 eval 获取配置对象
|
||||
let config: rquickjs::Object = ctx.eval(replaced.clone()).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.config_reparse_failed",
|
||||
format!("重新解析配置失败: {}", e),
|
||||
format!("Failed to re-parse config: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 提取 extractor 函数
|
||||
let extractor: Function = config.get("extractor").map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.extractor_missing",
|
||||
format!("缺少 extractor 函数: {}", e),
|
||||
format!("Missing extractor function: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 将响应数据转换为 JS 值
|
||||
let response_js: rquickjs::Value =
|
||||
ctx.json_parse(response_data.as_str()).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.response_parse_failed",
|
||||
format!("解析响应 JSON 失败: {}", e),
|
||||
format!("Failed to parse response JSON: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 调用 extractor(response)
|
||||
let result_js: rquickjs::Value = extractor.call((response_js,)).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.extractor_exec_failed",
|
||||
format!("执行 extractor 失败: {}", e),
|
||||
format!("Failed to execute extractor: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 转换为 JSON 字符串
|
||||
let result_json: String = ctx
|
||||
.json_stringify(result_js)
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.result_serialize_failed",
|
||||
format!("序列化结果失败: {}", e),
|
||||
format!("Failed to serialize result: {}", e),
|
||||
)
|
||||
})?
|
||||
.ok_or_else(|| {
|
||||
AppError::localized(
|
||||
"usage_script.serialize_none",
|
||||
"序列化返回 None",
|
||||
"Serialization returned None",
|
||||
)
|
||||
})?
|
||||
.get()
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.get_string_failed",
|
||||
format!("获取字符串失败: {}", e),
|
||||
format!("Failed to get string: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 解析为 serde_json::Value
|
||||
serde_json::from_str(&result_json).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.json_parse_failed",
|
||||
format!("JSON 解析失败: {}", e),
|
||||
format!("JSON parse failed: {}", e),
|
||||
)
|
||||
})
|
||||
})?
|
||||
}; // Runtime 和 Context 在这里被 drop
|
||||
|
||||
// 6. 验证返回值格式
|
||||
validate_result(&result)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// 请求配置结构
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
struct RequestConfig {
|
||||
url: String,
|
||||
method: String,
|
||||
#[serde(default)]
|
||||
headers: HashMap<String, String>,
|
||||
#[serde(default)]
|
||||
body: Option<String>,
|
||||
}
|
||||
|
||||
/// 发送 HTTP 请求
|
||||
async fn send_http_request(config: &RequestConfig, timeout_secs: u64) -> Result<String, AppError> {
|
||||
// 约束超时范围,防止异常配置导致长时间阻塞
|
||||
let timeout = timeout_secs.clamp(2, 30);
|
||||
let client = Client::builder()
|
||||
.timeout(Duration::from_secs(timeout))
|
||||
.build()
|
||||
.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.client_create_failed",
|
||||
format!("创建客户端失败: {}", e),
|
||||
format!("Failed to create client: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
// 严格校验 HTTP 方法,非法值不回退为 GET
|
||||
let method: reqwest::Method = config.method.parse().map_err(|_| {
|
||||
AppError::localized(
|
||||
"usage_script.invalid_http_method",
|
||||
format!("不支持的 HTTP 方法: {}", config.method),
|
||||
format!("Unsupported HTTP method: {}", config.method),
|
||||
)
|
||||
})?;
|
||||
|
||||
let mut req = client.request(method.clone(), &config.url);
|
||||
|
||||
// 添加请求头
|
||||
for (k, v) in &config.headers {
|
||||
req = req.header(k, v);
|
||||
}
|
||||
|
||||
// 添加请求体
|
||||
if let Some(body) = &config.body {
|
||||
req = req.body(body.clone());
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
let resp = req.send().await.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.request_failed",
|
||||
format!("请求失败: {}", e),
|
||||
format!("Request failed: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
let status = resp.status();
|
||||
let text = resp.text().await.map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.read_response_failed",
|
||||
format!("读取响应失败: {}", e),
|
||||
format!("Failed to read response: {}", e),
|
||||
)
|
||||
})?;
|
||||
|
||||
if !status.is_success() {
|
||||
let preview = if text.len() > 200 {
|
||||
format!("{}...", &text[..200])
|
||||
} else {
|
||||
text.clone()
|
||||
};
|
||||
return Err(AppError::localized(
|
||||
"usage_script.http_error",
|
||||
format!("HTTP {} : {}", status, preview),
|
||||
format!("HTTP {} : {}", status, preview),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(text)
|
||||
}
|
||||
|
||||
/// 验证脚本返回值(支持单对象或数组)
|
||||
fn validate_result(result: &Value) -> Result<(), AppError> {
|
||||
// 如果是数组,验证每个元素
|
||||
if let Some(arr) = result.as_array() {
|
||||
if arr.is_empty() {
|
||||
return Err(AppError::localized(
|
||||
"usage_script.empty_array",
|
||||
"脚本返回的数组不能为空",
|
||||
"Script returned empty array",
|
||||
));
|
||||
}
|
||||
for (idx, item) in arr.iter().enumerate() {
|
||||
validate_single_usage(item).map_err(|e| {
|
||||
AppError::localized(
|
||||
"usage_script.array_validation_failed",
|
||||
format!("数组索引[{}]验证失败: {}", idx, e),
|
||||
format!("Validation failed at index [{}]: {}", idx, e),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 如果是单对象,直接验证(向后兼容)
|
||||
validate_single_usage(result)
|
||||
}
|
||||
|
||||
/// 验证单个用量数据对象
|
||||
fn validate_single_usage(result: &Value) -> Result<(), AppError> {
|
||||
let obj = result.as_object().ok_or_else(|| {
|
||||
AppError::localized(
|
||||
"usage_script.must_return_object",
|
||||
"脚本必须返回对象或对象数组",
|
||||
"Script must return object or array of objects",
|
||||
)
|
||||
})?;
|
||||
|
||||
// 所有字段均为可选,只进行类型检查
|
||||
if obj.contains_key("isValid")
|
||||
&& !result["isValid"].is_null()
|
||||
&& !result["isValid"].is_boolean()
|
||||
{
|
||||
return Err(AppError::localized(
|
||||
"usage_script.isvalid_type_error",
|
||||
"isValid 必须是布尔值或 null",
|
||||
"isValid must be boolean or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("invalidMessage")
|
||||
&& !result["invalidMessage"].is_null()
|
||||
&& !result["invalidMessage"].is_string()
|
||||
{
|
||||
return Err(AppError::localized(
|
||||
"usage_script.invalidmessage_type_error",
|
||||
"invalidMessage 必须是字符串或 null",
|
||||
"invalidMessage must be string or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("remaining")
|
||||
&& !result["remaining"].is_null()
|
||||
&& !result["remaining"].is_number()
|
||||
{
|
||||
return Err(AppError::localized(
|
||||
"usage_script.remaining_type_error",
|
||||
"remaining 必须是数字或 null",
|
||||
"remaining must be number or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("unit") && !result["unit"].is_null() && !result["unit"].is_string() {
|
||||
return Err(AppError::localized(
|
||||
"usage_script.unit_type_error",
|
||||
"unit 必须是字符串或 null",
|
||||
"unit must be string or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("total") && !result["total"].is_null() && !result["total"].is_number() {
|
||||
return Err(AppError::localized(
|
||||
"usage_script.total_type_error",
|
||||
"total 必须是数字或 null",
|
||||
"total must be number or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("used") && !result["used"].is_null() && !result["used"].is_number() {
|
||||
return Err(AppError::localized(
|
||||
"usage_script.used_type_error",
|
||||
"used 必须是数字或 null",
|
||||
"used must be number or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("planName")
|
||||
&& !result["planName"].is_null()
|
||||
&& !result["planName"].is_string()
|
||||
{
|
||||
return Err(AppError::localized(
|
||||
"usage_script.planname_type_error",
|
||||
"planName 必须是字符串或 null",
|
||||
"planName must be string or null",
|
||||
));
|
||||
}
|
||||
if obj.contains_key("extra") && !result["extra"].is_null() && !result["extra"].is_string() {
|
||||
return Err(AppError::localized(
|
||||
"usage_script.extra_type_error",
|
||||
"extra 必须是字符串或 null",
|
||||
"extra must be string or null",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "CC Switch",
|
||||
"version": "3.0.0",
|
||||
"version": "3.6.0",
|
||||
"identifier": "com.ccswitch.desktop",
|
||||
"build": {
|
||||
"frontendDist": "../dist",
|
||||
@@ -12,6 +12,7 @@
|
||||
"app": {
|
||||
"windows": [
|
||||
{
|
||||
"label": "main",
|
||||
"title": "",
|
||||
"width": 900,
|
||||
"height": 650,
|
||||
@@ -23,18 +24,32 @@
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": "default-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https: http:"
|
||||
"csp": "default-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' ipc: http://ipc.localhost https: http:"
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": ["app", "dmg", "nsis", "appimage"],
|
||||
"targets": "all",
|
||||
"createUpdaterArtifacts": true,
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
]
|
||||
],
|
||||
"windows": {
|
||||
"wix": {
|
||||
"template": "wix/per-user-main.wxs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"plugins": {
|
||||
"updater": {
|
||||
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEM4MDI4QzlBNTczOTI4RTMKUldUaktEbFhtb3dDeUM5US9kT0FmdGR5Ti9vQzcwa2dTMlpibDVDUmQ2M0VGTzVOWnd0SGpFVlEK",
|
||||
"endpoints": [
|
||||
"https://github.com/farion1231/cc-switch/releases/latest/download/latest.json"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
107
src-tauri/tests/app_config_load.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use cc_switch_lib::{AppError, MultiAppConfig};
|
||||
|
||||
mod support;
|
||||
use support::{ensure_test_home, reset_test_fs, test_mutex};
|
||||
|
||||
fn cfg_path() -> PathBuf {
|
||||
let home = std::env::var("HOME").expect("HOME should be set by ensure_test_home");
|
||||
PathBuf::from(home).join(".cc-switch").join("config.json")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_v1_config_returns_error_and_does_not_write() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let path = cfg_path();
|
||||
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
|
||||
|
||||
// 最小 v1 形状:providers + current,且不含 version/apps/mcp
|
||||
let v1_json = r#"{"providers":{},"current":""}"#;
|
||||
fs::write(&path, v1_json).expect("seed v1 json");
|
||||
let before = fs::read_to_string(&path).expect("read before");
|
||||
|
||||
let err = MultiAppConfig::load().expect_err("v1 should not be auto-migrated");
|
||||
match err {
|
||||
AppError::Localized { key, .. } => assert_eq!(key, "config.unsupported_v1"),
|
||||
other => panic!("expected Localized v1 error, got {other:?}"),
|
||||
}
|
||||
|
||||
// 文件不应有任何变化,且不应生成 .bak
|
||||
let after = fs::read_to_string(&path).expect("read after");
|
||||
assert_eq!(before, after, "config.json should not be modified");
|
||||
let bak = home.join(".cc-switch").join("config.json.bak");
|
||||
assert!(!bak.exists(), ".bak should not be created on load error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_v1_with_extra_version_still_treated_as_v1() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let path = cfg_path();
|
||||
std::fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
|
||||
|
||||
// 畸形:包含 providers + current + version,但没有 apps,应按 v1 处理
|
||||
let v1_like = r#"{"providers":{},"current":"","version":2}"#;
|
||||
std::fs::write(&path, v1_like).expect("seed v1-like json");
|
||||
let before = std::fs::read_to_string(&path).expect("read before");
|
||||
|
||||
let err = MultiAppConfig::load().expect_err("v1-like should not be parsed as v2");
|
||||
match err {
|
||||
AppError::Localized { key, .. } => assert_eq!(key, "config.unsupported_v1"),
|
||||
other => panic!("expected Localized v1 error, got {other:?}"),
|
||||
}
|
||||
|
||||
let after = std::fs::read_to_string(&path).expect("read after");
|
||||
assert_eq!(before, after, "config.json should not be modified");
|
||||
let bak = home.join(".cc-switch").join("config.json.bak");
|
||||
assert!(!bak.exists(), ".bak should not be created on v1-like error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_invalid_json_returns_parse_error_and_does_not_write() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let path = cfg_path();
|
||||
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
|
||||
|
||||
fs::write(&path, "{not json").expect("seed invalid json");
|
||||
let before = fs::read_to_string(&path).expect("read before");
|
||||
|
||||
let err = MultiAppConfig::load().expect_err("invalid json should error");
|
||||
match err {
|
||||
AppError::Json { .. } => {}
|
||||
other => panic!("expected Json error, got {other:?}"),
|
||||
}
|
||||
|
||||
let after = fs::read_to_string(&path).expect("read after");
|
||||
assert_eq!(before, after, "config.json should remain unchanged");
|
||||
let bak = home.join(".cc-switch").join("config.json.bak");
|
||||
assert!(!bak.exists(), ".bak should not be created on parse error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_valid_v2_config_succeeds() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
let path = cfg_path();
|
||||
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
|
||||
|
||||
// 使用默认结构序列化为 v2
|
||||
let default_cfg = MultiAppConfig::default();
|
||||
let json = serde_json::to_string_pretty(&default_cfg).expect("serialize default cfg");
|
||||
fs::write(&path, json).expect("write v2 json");
|
||||
|
||||
let loaded = MultiAppConfig::load().expect("v2 should load successfully");
|
||||
assert_eq!(loaded.version, 2);
|
||||
assert!(loaded
|
||||
.get_manager(&cc_switch_lib::AppType::Claude)
|
||||
.is_some());
|
||||
assert!(loaded.get_manager(&cc_switch_lib::AppType::Codex).is_some());
|
||||
}
|
||||
22
src-tauri/tests/app_type_parse.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use cc_switch_lib::AppType;
|
||||
|
||||
#[test]
|
||||
fn parse_known_apps_case_insensitive_and_trim() {
|
||||
assert!(matches!(AppType::from_str("claude"), Ok(AppType::Claude)));
|
||||
assert!(matches!(AppType::from_str("codex"), Ok(AppType::Codex)));
|
||||
assert!(matches!(
|
||||
AppType::from_str(" ClAuDe \n"),
|
||||
Ok(AppType::Claude)
|
||||
));
|
||||
assert!(matches!(AppType::from_str("\tcoDeX\t"), Ok(AppType::Codex)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_unknown_app_returns_localized_error_message() {
|
||||
let err = AppType::from_str("unknown").unwrap_err();
|
||||
let msg = err.to_string();
|
||||
assert!(msg.contains("可选值") || msg.contains("Allowed"));
|
||||
assert!(msg.contains("unknown"));
|
||||
}
|
||||
960
src-tauri/tests/import_export_sync.rs
Normal file
@@ -0,0 +1,960 @@
|
||||
use serde_json::json;
|
||||
use std::{fs, path::Path, sync::RwLock};
|
||||
use tauri::async_runtime;
|
||||
|
||||
use cc_switch_lib::{
|
||||
get_claude_settings_path, read_json_file, AppError, AppState, AppType, ConfigService,
|
||||
MultiAppConfig, Provider,
|
||||
};
|
||||
|
||||
#[path = "support.rs"]
|
||||
mod support;
|
||||
use support::{ensure_test_home, reset_test_fs, test_mutex};
|
||||
|
||||
#[test]
|
||||
fn sync_claude_provider_writes_live_settings() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
let provider_config = json!({
|
||||
"env": {
|
||||
"ANTHROPIC_AUTH_TOKEN": "test-key",
|
||||
"ANTHROPIC_BASE_URL": "https://api.test"
|
||||
},
|
||||
"ui": {
|
||||
"displayName": "Test Provider"
|
||||
}
|
||||
});
|
||||
|
||||
let provider = Provider::with_id(
|
||||
"prov-1".to_string(),
|
||||
"Test Claude".to_string(),
|
||||
provider_config.clone(),
|
||||
None,
|
||||
);
|
||||
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.providers.insert("prov-1".to_string(), provider);
|
||||
manager.current = "prov-1".to_string();
|
||||
|
||||
ConfigService::sync_current_providers_to_live(&mut config).expect("sync live settings");
|
||||
|
||||
let settings_path = get_claude_settings_path();
|
||||
assert!(
|
||||
settings_path.exists(),
|
||||
"live settings should be written to {}",
|
||||
settings_path.display()
|
||||
);
|
||||
|
||||
let live_value: serde_json::Value = read_json_file(&settings_path).expect("read live file");
|
||||
assert_eq!(live_value, provider_config);
|
||||
|
||||
// 确认 SSOT 中的供应商也同步了最新内容
|
||||
let updated = config
|
||||
.get_manager(&AppType::Claude)
|
||||
.and_then(|m| m.providers.get("prov-1"))
|
||||
.expect("provider in config");
|
||||
assert_eq!(updated.settings_config, provider_config);
|
||||
|
||||
// 额外确认写入位置位于测试 HOME 下
|
||||
assert!(
|
||||
settings_path.starts_with(home),
|
||||
"settings path {:?} should reside under test HOME {:?}",
|
||||
settings_path,
|
||||
home
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_codex_provider_writes_auth_and_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
|
||||
// 添加入测 MCP 启用项,确保 sync_enabled_to_codex 会写入 TOML
|
||||
config.mcp.codex.servers.insert(
|
||||
"echo-server".into(),
|
||||
json!({
|
||||
"id": "echo-server",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo",
|
||||
"args": ["hello"]
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let provider_config = json!({
|
||||
"auth": {
|
||||
"OPENAI_API_KEY": "codex-key"
|
||||
},
|
||||
"config": r#"base_url = "https://codex.test""#
|
||||
});
|
||||
|
||||
let provider = Provider::with_id(
|
||||
"codex-1".to_string(),
|
||||
"Codex Test".to_string(),
|
||||
provider_config.clone(),
|
||||
None,
|
||||
);
|
||||
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.providers.insert("codex-1".to_string(), provider);
|
||||
manager.current = "codex-1".to_string();
|
||||
|
||||
ConfigService::sync_current_providers_to_live(&mut config).expect("sync codex live");
|
||||
|
||||
let auth_path = cc_switch_lib::get_codex_auth_path();
|
||||
let config_path = cc_switch_lib::get_codex_config_path();
|
||||
|
||||
assert!(
|
||||
auth_path.exists(),
|
||||
"auth.json should exist at {}",
|
||||
auth_path.display()
|
||||
);
|
||||
assert!(
|
||||
config_path.exists(),
|
||||
"config.toml should exist at {}",
|
||||
config_path.display()
|
||||
);
|
||||
|
||||
let auth_value: serde_json::Value = read_json_file(&auth_path).expect("read auth");
|
||||
assert_eq!(
|
||||
auth_value,
|
||||
provider_config.get("auth").cloned().expect("auth object")
|
||||
);
|
||||
|
||||
let toml_text = fs::read_to_string(&config_path).expect("read config.toml");
|
||||
assert!(
|
||||
toml_text.contains("command = \"echo\""),
|
||||
"config.toml should contain serialized enabled MCP server"
|
||||
);
|
||||
|
||||
// 当前供应商应同步最新 config 文本
|
||||
let manager = config.get_manager(&AppType::Codex).expect("codex manager");
|
||||
let synced = manager.providers.get("codex-1").expect("codex provider");
|
||||
let synced_cfg = synced
|
||||
.settings_config
|
||||
.get("config")
|
||||
.and_then(|v| v.as_str())
|
||||
.expect("config string");
|
||||
assert_eq!(synced_cfg, toml_text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_enabled_to_codex_writes_enabled_servers() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.codex.servers.insert(
|
||||
"stdio-enabled".into(),
|
||||
json!({
|
||||
"id": "stdio-enabled",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo",
|
||||
"args": ["ok"],
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
cc_switch_lib::sync_enabled_to_codex(&config).expect("sync codex");
|
||||
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
assert!(path.exists(), "config.toml should be created");
|
||||
let text = fs::read_to_string(&path).expect("read config.toml");
|
||||
assert!(
|
||||
text.contains("mcp_servers") && text.contains("stdio-enabled"),
|
||||
"enabled servers should be serialized"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_enabled_to_codex_preserves_non_mcp_content_and_style() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
// 预置含有顶层注释与非 MCP 键的 config.toml
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
let seed = r#"# top-comment
|
||||
title = "keep-me"
|
||||
|
||||
[profile]
|
||||
mode = "dev"
|
||||
"#;
|
||||
fs::write(&path, seed).expect("seed config.toml");
|
||||
|
||||
// 启用一个 MCP 项,触发增量写入
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.codex.servers.insert(
|
||||
"echo".into(),
|
||||
json!({
|
||||
"id": "echo",
|
||||
"enabled": true,
|
||||
"server": { "type": "stdio", "command": "echo" }
|
||||
}),
|
||||
);
|
||||
|
||||
cc_switch_lib::sync_enabled_to_codex(&config).expect("sync codex");
|
||||
|
||||
let text = fs::read_to_string(&path).expect("read config.toml");
|
||||
// 顶层注释与非 MCP 键应保留
|
||||
assert!(
|
||||
text.contains("# top-comment"),
|
||||
"top comment should be preserved"
|
||||
);
|
||||
assert!(
|
||||
text.contains("title = \"keep-me\""),
|
||||
"top key should be preserved"
|
||||
);
|
||||
assert!(
|
||||
text.contains("[profile]"),
|
||||
"non-MCP table should be preserved"
|
||||
);
|
||||
// 新增的 mcp_servers/或 mcp.servers 应存在并包含 echo
|
||||
assert!(
|
||||
text.contains("mcp_servers") || text.contains("[mcp.servers]"),
|
||||
"one server table style should be present"
|
||||
);
|
||||
assert!(
|
||||
text.contains("echo") && text.contains("command = \"echo\""),
|
||||
"echo server should be serialized"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_enabled_to_codex_keeps_existing_style_mcp_dot_servers() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
// 预置 mcp.servers 风格
|
||||
let seed = r#"[mcp]
|
||||
other = "keep"
|
||||
[mcp.servers]
|
||||
"#;
|
||||
fs::write(&path, seed).expect("seed config.toml");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.codex.servers.insert(
|
||||
"echo".into(),
|
||||
json!({
|
||||
"id": "echo",
|
||||
"enabled": true,
|
||||
"server": { "type": "stdio", "command": "echo" }
|
||||
}),
|
||||
);
|
||||
|
||||
cc_switch_lib::sync_enabled_to_codex(&config).expect("sync codex");
|
||||
let text = fs::read_to_string(&path).expect("read config.toml");
|
||||
// 仍应采用 mcp.servers 风格
|
||||
assert!(
|
||||
text.contains("[mcp.servers]"),
|
||||
"should keep mcp.servers style"
|
||||
);
|
||||
assert!(
|
||||
!text.contains("mcp_servers"),
|
||||
"should not switch to mcp_servers"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_enabled_to_codex_removes_servers_when_none_enabled() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
fs::write(
|
||||
&path,
|
||||
r#"[mcp_servers]
|
||||
disabled = { type = "stdio", command = "noop" }
|
||||
"#,
|
||||
)
|
||||
.expect("seed config file");
|
||||
|
||||
let config = MultiAppConfig::default(); // 无启用项
|
||||
cc_switch_lib::sync_enabled_to_codex(&config).expect("sync codex");
|
||||
|
||||
let text = fs::read_to_string(&path).expect("read config.toml");
|
||||
assert!(
|
||||
!text.contains("mcp_servers") && !text.contains("servers"),
|
||||
"disabled entries should be removed from config.toml"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_enabled_to_codex_returns_error_on_invalid_toml() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
fs::write(&path, "invalid = [").expect("write invalid config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.codex.servers.insert(
|
||||
"broken".into(),
|
||||
json!({
|
||||
"id": "broken",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let err = cc_switch_lib::sync_enabled_to_codex(&config).expect_err("sync should fail");
|
||||
match err {
|
||||
cc_switch_lib::AppError::Toml { path, .. } => {
|
||||
assert!(
|
||||
path.ends_with("config.toml"),
|
||||
"path should reference config.toml"
|
||||
);
|
||||
}
|
||||
cc_switch_lib::AppError::McpValidation(msg) => {
|
||||
assert!(
|
||||
msg.contains("config.toml"),
|
||||
"error message should mention config.toml"
|
||||
);
|
||||
}
|
||||
other => panic!("unexpected error: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_codex_provider_missing_auth_returns_error() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
let provider = Provider::with_id(
|
||||
"codex-missing-auth".to_string(),
|
||||
"No Auth".to_string(),
|
||||
json!({
|
||||
"config": "model = \"test\""
|
||||
}),
|
||||
None,
|
||||
);
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.providers.insert(provider.id.clone(), provider);
|
||||
manager.current = "codex-missing-auth".to_string();
|
||||
|
||||
let err = ConfigService::sync_current_providers_to_live(&mut config)
|
||||
.expect_err("sync should fail when auth missing");
|
||||
match err {
|
||||
cc_switch_lib::AppError::Config(msg) => {
|
||||
assert!(msg.contains("auth"), "error message should mention auth");
|
||||
}
|
||||
other => panic!("unexpected error variant: {other:?}"),
|
||||
}
|
||||
|
||||
// 确认未产生任何 live 配置文件
|
||||
assert!(
|
||||
!cc_switch_lib::get_codex_auth_path().exists(),
|
||||
"auth.json should not be created on failure"
|
||||
);
|
||||
assert!(
|
||||
!cc_switch_lib::get_codex_config_path().exists(),
|
||||
"config.toml should not be created on failure"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_codex_live_atomic_persists_auth_and_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let auth = json!({ "OPENAI_API_KEY": "dev-key" });
|
||||
let config_text = r#"
|
||||
[mcp_servers.echo]
|
||||
type = "stdio"
|
||||
command = "echo"
|
||||
args = ["ok"]
|
||||
"#;
|
||||
|
||||
cc_switch_lib::write_codex_live_atomic(&auth, Some(config_text))
|
||||
.expect("atomic write should succeed");
|
||||
|
||||
let auth_path = cc_switch_lib::get_codex_auth_path();
|
||||
let config_path = cc_switch_lib::get_codex_config_path();
|
||||
assert!(auth_path.exists(), "auth.json should be created");
|
||||
assert!(config_path.exists(), "config.toml should be created");
|
||||
|
||||
let stored_auth: serde_json::Value =
|
||||
cc_switch_lib::read_json_file(&auth_path).expect("read auth");
|
||||
assert_eq!(stored_auth, auth, "auth.json should match input");
|
||||
|
||||
let stored_config = std::fs::read_to_string(&config_path).expect("read config");
|
||||
assert!(
|
||||
stored_config.contains("mcp_servers.echo"),
|
||||
"config.toml should contain serialized table"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_codex_live_atomic_rolls_back_auth_when_config_write_fails() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let auth_path = cc_switch_lib::get_codex_auth_path();
|
||||
if let Some(parent) = auth_path.parent() {
|
||||
std::fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
std::fs::write(&auth_path, r#"{"OPENAI_API_KEY":"legacy"}"#).expect("seed auth");
|
||||
|
||||
let config_path = cc_switch_lib::get_codex_config_path();
|
||||
std::fs::create_dir_all(&config_path).expect("create blocking directory");
|
||||
|
||||
let auth = json!({ "OPENAI_API_KEY": "new-key" });
|
||||
let config_text = r#"[mcp_servers.sample]
|
||||
type = "stdio"
|
||||
command = "noop"
|
||||
"#;
|
||||
|
||||
let err = cc_switch_lib::write_codex_live_atomic(&auth, Some(config_text))
|
||||
.expect_err("config write should fail when target is directory");
|
||||
match err {
|
||||
cc_switch_lib::AppError::Io { path, .. } => {
|
||||
assert!(
|
||||
path.ends_with("config.toml"),
|
||||
"io error path should point to config.toml"
|
||||
);
|
||||
}
|
||||
cc_switch_lib::AppError::IoContext { context, .. } => {
|
||||
assert!(
|
||||
context.contains("config.toml"),
|
||||
"error context should mention config path"
|
||||
);
|
||||
}
|
||||
other => panic!("unexpected error variant: {other:?}"),
|
||||
}
|
||||
|
||||
let stored = std::fs::read_to_string(&auth_path).expect("read existing auth");
|
||||
assert!(
|
||||
stored.contains("legacy"),
|
||||
"auth.json should roll back to legacy content"
|
||||
);
|
||||
assert!(
|
||||
std::fs::metadata(&config_path)
|
||||
.expect("config path metadata")
|
||||
.is_dir(),
|
||||
"config path should remain a directory after failure"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_from_codex_adds_servers_from_mcp_servers_table() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
fs::write(
|
||||
&path,
|
||||
r#"[mcp_servers.echo_server]
|
||||
type = "stdio"
|
||||
command = "echo"
|
||||
args = ["hello"]
|
||||
|
||||
[mcp_servers.http_server]
|
||||
type = "http"
|
||||
url = "https://example.com"
|
||||
"#,
|
||||
)
|
||||
.expect("write codex config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
let changed = cc_switch_lib::import_from_codex(&mut config).expect("import codex");
|
||||
assert!(changed >= 2, "should import both servers");
|
||||
|
||||
let servers = &config.mcp.codex.servers;
|
||||
let echo = servers
|
||||
.get("echo_server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("echo server");
|
||||
assert_eq!(echo.get("enabled").and_then(|v| v.as_bool()), Some(true));
|
||||
let server_spec = echo
|
||||
.get("server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("server spec");
|
||||
assert_eq!(
|
||||
server_spec
|
||||
.get("command")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(""),
|
||||
"echo"
|
||||
);
|
||||
|
||||
let http = servers
|
||||
.get("http_server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("http server");
|
||||
let http_spec = http
|
||||
.get("server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("http spec");
|
||||
assert_eq!(
|
||||
http_spec.get("url").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"https://example.com"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_from_codex_merges_into_existing_entries() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let path = cc_switch_lib::get_codex_config_path();
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).expect("create codex dir");
|
||||
}
|
||||
fs::write(
|
||||
&path,
|
||||
r#"[mcp.servers.existing]
|
||||
type = "stdio"
|
||||
command = "echo"
|
||||
"#,
|
||||
)
|
||||
.expect("write codex config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.codex.servers.insert(
|
||||
"existing".into(),
|
||||
json!({
|
||||
"id": "existing",
|
||||
"name": "existing",
|
||||
"enabled": false,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "prev"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let changed = cc_switch_lib::import_from_codex(&mut config).expect("import codex");
|
||||
assert!(changed >= 1, "should mark change for enabled flag");
|
||||
|
||||
let entry = config
|
||||
.mcp
|
||||
.codex
|
||||
.servers
|
||||
.get("existing")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("existing entry");
|
||||
assert_eq!(entry.get("enabled").and_then(|v| v.as_bool()), Some(true));
|
||||
let spec = entry
|
||||
.get("server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("server spec");
|
||||
// 保留原 command,确保导入不会覆盖现有 server 细节
|
||||
assert_eq!(spec.get("command").and_then(|v| v.as_str()), Some("prev"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_claude_enabled_mcp_projects_to_user_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let mut config = MultiAppConfig::default();
|
||||
|
||||
config.mcp.claude.servers.insert(
|
||||
"stdio-enabled".into(),
|
||||
json!({
|
||||
"id": "stdio-enabled",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo",
|
||||
"args": ["hi"],
|
||||
}
|
||||
}),
|
||||
);
|
||||
config.mcp.claude.servers.insert(
|
||||
"http-disabled".into(),
|
||||
json!({
|
||||
"id": "http-disabled",
|
||||
"enabled": false,
|
||||
"server": {
|
||||
"type": "http",
|
||||
"url": "https://example.com",
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
cc_switch_lib::sync_enabled_to_claude(&config).expect("sync Claude MCP");
|
||||
|
||||
let claude_path = cc_switch_lib::get_claude_mcp_path();
|
||||
assert!(claude_path.exists(), "claude config should exist");
|
||||
let text = fs::read_to_string(&claude_path).expect("read .claude.json");
|
||||
let value: serde_json::Value = serde_json::from_str(&text).expect("parse claude json");
|
||||
let servers = value
|
||||
.get("mcpServers")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("mcpServers map");
|
||||
assert_eq!(servers.len(), 1, "only enabled entries should be written");
|
||||
let enabled = servers.get("stdio-enabled").expect("enabled entry");
|
||||
assert_eq!(
|
||||
enabled
|
||||
.get("command")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_default(),
|
||||
"echo"
|
||||
);
|
||||
assert!(servers.get("http-disabled").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_from_claude_merges_into_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let claude_path = home.join(".claude.json");
|
||||
|
||||
fs::write(
|
||||
&claude_path,
|
||||
serde_json::to_string_pretty(&json!({
|
||||
"mcpServers": {
|
||||
"stdio-enabled": {
|
||||
"type": "stdio",
|
||||
"command": "echo",
|
||||
"args": ["hello"]
|
||||
}
|
||||
}
|
||||
}))
|
||||
.unwrap(),
|
||||
)
|
||||
.expect("write claude json");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.mcp.claude.servers.insert(
|
||||
"stdio-enabled".into(),
|
||||
json!({
|
||||
"id": "stdio-enabled",
|
||||
"name": "stdio-enabled",
|
||||
"enabled": false,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "prev"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let changed = cc_switch_lib::import_from_claude(&mut config).expect("import from claude");
|
||||
assert!(changed >= 1, "should mark at least one change");
|
||||
|
||||
let entry = config
|
||||
.mcp
|
||||
.claude
|
||||
.servers
|
||||
.get("stdio-enabled")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("entry exists");
|
||||
assert_eq!(entry.get("enabled").and_then(|v| v.as_bool()), Some(true));
|
||||
let server = entry
|
||||
.get("server")
|
||||
.and_then(|v| v.as_object())
|
||||
.expect("server obj");
|
||||
assert_eq!(
|
||||
server.get("command").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"prev",
|
||||
"existing server config should be preserved"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_backup_skips_missing_file() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let config_path = home.join(".cc-switch").join("config.json");
|
||||
|
||||
// 未创建文件时应返回空字符串,不报错
|
||||
let result = ConfigService::create_backup(&config_path).expect("create backup");
|
||||
assert!(
|
||||
result.is_empty(),
|
||||
"expected empty backup id when config file missing"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_backup_generates_snapshot_file() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let config_dir = home.join(".cc-switch");
|
||||
let config_path = config_dir.join("config.json");
|
||||
fs::create_dir_all(&config_dir).expect("prepare config dir");
|
||||
fs::write(&config_path, r#"{"version":2}"#).expect("write config file");
|
||||
|
||||
let backup_id = ConfigService::create_backup(&config_path).expect("backup success");
|
||||
assert!(
|
||||
!backup_id.is_empty(),
|
||||
"backup id should contain timestamp information"
|
||||
);
|
||||
|
||||
let backup_path = config_dir.join("backups").join(format!("{backup_id}.json"));
|
||||
assert!(
|
||||
backup_path.exists(),
|
||||
"expected backup file at {}",
|
||||
backup_path.display()
|
||||
);
|
||||
|
||||
let backup_content = fs::read_to_string(&backup_path).expect("read backup");
|
||||
assert!(
|
||||
backup_content.contains(r#""version":2"#),
|
||||
"backup content should match original config"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_backup_retains_only_latest_entries() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
let config_dir = home.join(".cc-switch");
|
||||
let config_path = config_dir.join("config.json");
|
||||
fs::create_dir_all(&config_dir).expect("prepare config dir");
|
||||
fs::write(&config_path, r#"{"version":3}"#).expect("write config file");
|
||||
|
||||
let backups_dir = config_dir.join("backups");
|
||||
fs::create_dir_all(&backups_dir).expect("create backups dir");
|
||||
for idx in 0..12 {
|
||||
let manual = backups_dir.join(format!("manual_{idx:02}.json"));
|
||||
fs::write(&manual, format!("{{\"idx\":{idx}}}")).expect("seed manual backup");
|
||||
}
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
|
||||
let latest_backup_id =
|
||||
ConfigService::create_backup(&config_path).expect("create backup with cleanup");
|
||||
assert!(
|
||||
!latest_backup_id.is_empty(),
|
||||
"backup id should not be empty when config exists"
|
||||
);
|
||||
|
||||
let entries: Vec<_> = fs::read_dir(&backups_dir)
|
||||
.expect("read backups dir")
|
||||
.filter_map(|entry| entry.ok())
|
||||
.collect();
|
||||
assert!(
|
||||
entries.len() <= 10,
|
||||
"expected backups to be trimmed to at most 10 files, got {}",
|
||||
entries.len()
|
||||
);
|
||||
|
||||
let latest_path = backups_dir.join(format!("{latest_backup_id}.json"));
|
||||
assert!(
|
||||
latest_path.exists(),
|
||||
"latest backup {} should be preserved",
|
||||
latest_path.display()
|
||||
);
|
||||
|
||||
// 进一步确认保留的条目包含一些历史文件,说明清理逻辑仅裁剪多余部分
|
||||
let manual_kept = entries
|
||||
.iter()
|
||||
.filter_map(|entry| entry.file_name().into_string().ok())
|
||||
.any(|name| name.starts_with("manual_"));
|
||||
assert!(
|
||||
manual_kept,
|
||||
"cleanup should keep part of the older backups to maintain history"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_config_from_path_overwrites_state_and_creates_backup() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let config_dir = home.join(".cc-switch");
|
||||
fs::create_dir_all(&config_dir).expect("create config dir");
|
||||
let config_path = config_dir.join("config.json");
|
||||
fs::write(&config_path, r#"{"version":1}"#).expect("seed original config");
|
||||
|
||||
let import_payload = serde_json::json!({
|
||||
"version": 2,
|
||||
"claude": {
|
||||
"providers": {
|
||||
"p-new": {
|
||||
"id": "p-new",
|
||||
"name": "Test Claude",
|
||||
"settingsConfig": {
|
||||
"env": { "ANTHROPIC_API_KEY": "new-key" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"current": "p-new"
|
||||
},
|
||||
"codex": {
|
||||
"providers": {},
|
||||
"current": ""
|
||||
},
|
||||
"mcp": {
|
||||
"claude": { "servers": {} },
|
||||
"codex": { "servers": {} }
|
||||
}
|
||||
});
|
||||
|
||||
let import_path = config_dir.join("import.json");
|
||||
fs::write(
|
||||
&import_path,
|
||||
serde_json::to_string_pretty(&import_payload).expect("serialize import payload"),
|
||||
)
|
||||
.expect("write import file");
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let backup_id = ConfigService::import_config_from_path(&import_path, &app_state)
|
||||
.expect("import should succeed");
|
||||
assert!(
|
||||
!backup_id.is_empty(),
|
||||
"expected backup id when original config exists"
|
||||
);
|
||||
|
||||
let backup_path = config_dir.join("backups").join(format!("{backup_id}.json"));
|
||||
assert!(
|
||||
backup_path.exists(),
|
||||
"backup file should exist at {}",
|
||||
backup_path.display()
|
||||
);
|
||||
|
||||
let updated_content = fs::read_to_string(&config_path).expect("read updated config");
|
||||
let parsed: serde_json::Value =
|
||||
serde_json::from_str(&updated_content).expect("parse updated config");
|
||||
assert_eq!(
|
||||
parsed
|
||||
.get("claude")
|
||||
.and_then(|c| c.get("current"))
|
||||
.and_then(|c| c.as_str()),
|
||||
Some("p-new"),
|
||||
"saved config should record new current provider"
|
||||
);
|
||||
|
||||
let guard = app_state.config.read().expect("lock state after import");
|
||||
let claude_manager = guard
|
||||
.get_manager(&AppType::Claude)
|
||||
.expect("claude manager in state");
|
||||
assert_eq!(
|
||||
claude_manager.current, "p-new",
|
||||
"state should reflect new current provider"
|
||||
);
|
||||
assert!(
|
||||
claude_manager.providers.contains_key("p-new"),
|
||||
"new provider should exist in state"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_config_from_path_invalid_json_returns_error() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let config_dir = home.join(".cc-switch");
|
||||
fs::create_dir_all(&config_dir).expect("create config dir");
|
||||
|
||||
let invalid_path = config_dir.join("broken.json");
|
||||
fs::write(&invalid_path, "{ not-json ").expect("write invalid json");
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let err = ConfigService::import_config_from_path(&invalid_path, &app_state)
|
||||
.expect_err("import should fail");
|
||||
match err {
|
||||
AppError::Json { .. } => {}
|
||||
other => panic!("expected json error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_config_from_path_missing_file_produces_io_error() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let missing_path = Path::new("/nonexistent/import.json");
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let err = ConfigService::import_config_from_path(missing_path, &app_state)
|
||||
.expect_err("import should fail for missing file");
|
||||
match err {
|
||||
AppError::Io { .. } => {}
|
||||
other => panic!("expected io error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_config_to_file_writes_target_path() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let config_dir = home.join(".cc-switch");
|
||||
fs::create_dir_all(&config_dir).expect("create config dir");
|
||||
let config_path = config_dir.join("config.json");
|
||||
fs::write(&config_path, r#"{"version":42,"flag":true}"#).expect("write config");
|
||||
|
||||
let export_path = home.join("exported-config.json");
|
||||
if export_path.exists() {
|
||||
fs::remove_file(&export_path).expect("cleanup export target");
|
||||
}
|
||||
|
||||
let result = async_runtime::block_on(cc_switch_lib::export_config_to_file(
|
||||
export_path.to_string_lossy().to_string(),
|
||||
))
|
||||
.expect("export should succeed");
|
||||
assert_eq!(result.get("success").and_then(|v| v.as_bool()), Some(true));
|
||||
|
||||
let exported = fs::read_to_string(&export_path).expect("read exported file");
|
||||
assert!(
|
||||
exported.contains(r#""version":42"#) && exported.contains(r#""flag":true"#),
|
||||
"exported file should mirror source config content"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_config_to_file_returns_error_when_source_missing() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let export_path = home.join("export-missing.json");
|
||||
if export_path.exists() {
|
||||
fs::remove_file(&export_path).expect("cleanup export target");
|
||||
}
|
||||
|
||||
let err = async_runtime::block_on(cc_switch_lib::export_config_to_file(
|
||||
export_path.to_string_lossy().to_string(),
|
||||
))
|
||||
.expect_err("export should fail when config.json missing");
|
||||
assert!(
|
||||
err.contains("IO 错误"),
|
||||
"expected IO error message, got {err}"
|
||||
);
|
||||
}
|
||||
234
src-tauri/tests/mcp_commands.rs
Normal file
@@ -0,0 +1,234 @@
|
||||
use std::{fs, sync::RwLock};
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use cc_switch_lib::{
|
||||
get_claude_mcp_path, get_claude_settings_path, import_default_config_test_hook, AppError,
|
||||
AppState, AppType, McpService, MultiAppConfig,
|
||||
};
|
||||
|
||||
#[path = "support.rs"]
|
||||
mod support;
|
||||
use support::{ensure_test_home, reset_test_fs, test_mutex};
|
||||
|
||||
#[test]
|
||||
fn import_default_config_claude_persists_provider() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let settings_path = get_claude_settings_path();
|
||||
if let Some(parent) = settings_path.parent() {
|
||||
fs::create_dir_all(parent).expect("create claude settings dir");
|
||||
}
|
||||
let settings = json!({
|
||||
"env": {
|
||||
"ANTHROPIC_AUTH_TOKEN": "test-key",
|
||||
"ANTHROPIC_BASE_URL": "https://api.test"
|
||||
}
|
||||
});
|
||||
fs::write(
|
||||
&settings_path,
|
||||
serde_json::to_string_pretty(&settings).expect("serialize settings"),
|
||||
)
|
||||
.expect("seed claude settings.json");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.ensure_app(&AppType::Claude);
|
||||
let state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
import_default_config_test_hook(&state, AppType::Claude)
|
||||
.expect("import default config succeeds");
|
||||
|
||||
// 验证内存状态
|
||||
let guard = state.config.read().expect("lock config");
|
||||
let manager = guard
|
||||
.get_manager(&AppType::Claude)
|
||||
.expect("claude manager present");
|
||||
assert_eq!(manager.current, "default");
|
||||
let default_provider = manager.providers.get("default").expect("default provider");
|
||||
assert_eq!(
|
||||
default_provider.settings_config, settings,
|
||||
"default provider should capture live settings"
|
||||
);
|
||||
drop(guard);
|
||||
|
||||
// 验证配置已持久化
|
||||
let config_path = home.join(".cc-switch").join("config.json");
|
||||
assert!(
|
||||
config_path.exists(),
|
||||
"importing default config should persist config.json"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_default_config_without_live_file_returns_error() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let err = import_default_config_test_hook(&state, AppType::Claude)
|
||||
.expect_err("missing live file should error");
|
||||
match err {
|
||||
AppError::Localized { zh, .. } => assert!(
|
||||
zh.contains("Claude Code 配置文件不存在"),
|
||||
"unexpected error message: {zh}"
|
||||
),
|
||||
AppError::Message(msg) => assert!(
|
||||
msg.contains("Claude Code 配置文件不存在"),
|
||||
"unexpected error message: {msg}"
|
||||
),
|
||||
other => panic!("unexpected error variant: {other:?}"),
|
||||
}
|
||||
|
||||
let config_path = home.join(".cc-switch").join("config.json");
|
||||
assert!(
|
||||
!config_path.exists(),
|
||||
"failed import should not create config.json"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_mcp_from_claude_creates_config_and_enables_servers() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let mcp_path = get_claude_mcp_path();
|
||||
let claude_json = json!({
|
||||
"mcpServers": {
|
||||
"echo": {
|
||||
"type": "stdio",
|
||||
"command": "echo"
|
||||
}
|
||||
}
|
||||
});
|
||||
fs::write(
|
||||
&mcp_path,
|
||||
serde_json::to_string_pretty(&claude_json).expect("serialize claude mcp"),
|
||||
)
|
||||
.expect("seed ~/.claude.json");
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let changed = McpService::import_from_claude(&state).expect("import mcp from claude succeeds");
|
||||
assert!(
|
||||
changed > 0,
|
||||
"import should report inserted or normalized entries"
|
||||
);
|
||||
|
||||
let guard = state.config.read().expect("lock config");
|
||||
let claude_servers = &guard.mcp.claude.servers;
|
||||
let entry = claude_servers
|
||||
.get("echo")
|
||||
.expect("server imported into config.json");
|
||||
assert!(
|
||||
entry
|
||||
.get("enabled")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false),
|
||||
"imported server should be marked enabled"
|
||||
);
|
||||
drop(guard);
|
||||
|
||||
let config_path = home.join(".cc-switch").join("config.json");
|
||||
assert!(
|
||||
config_path.exists(),
|
||||
"state.save should persist config.json when changes detected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_mcp_from_claude_invalid_json_preserves_state() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let mcp_path = get_claude_mcp_path();
|
||||
fs::write(&mcp_path, "{\"mcpServers\":") // 不完整 JSON
|
||||
.expect("seed invalid ~/.claude.json");
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let err =
|
||||
McpService::import_from_claude(&state).expect_err("invalid json should bubble up error");
|
||||
match err {
|
||||
AppError::McpValidation(msg) => assert!(
|
||||
msg.contains("解析 ~/.claude.json 失败"),
|
||||
"unexpected error message: {msg}"
|
||||
),
|
||||
other => panic!("unexpected error variant: {other:?}"),
|
||||
}
|
||||
|
||||
let config_path = home.join(".cc-switch").join("config.json");
|
||||
assert!(
|
||||
!config_path.exists(),
|
||||
"failed import should not persist config.json"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_mcp_enabled_for_codex_writes_live_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
ensure_test_home();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config.ensure_app(&AppType::Codex);
|
||||
config.mcp.codex.servers.insert(
|
||||
"codex-server".into(),
|
||||
json!({
|
||||
"id": "codex-server",
|
||||
"name": "Codex Server",
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo"
|
||||
},
|
||||
"enabled": false
|
||||
}),
|
||||
);
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
McpService::set_enabled(&state, AppType::Codex, "codex-server", true)
|
||||
.expect("set enabled should succeed");
|
||||
|
||||
let guard = state.config.read().expect("lock config");
|
||||
let entry = guard
|
||||
.mcp
|
||||
.codex
|
||||
.servers
|
||||
.get("codex-server")
|
||||
.expect("codex server exists");
|
||||
assert!(
|
||||
entry
|
||||
.get("enabled")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false),
|
||||
"server should be marked enabled after command"
|
||||
);
|
||||
drop(guard);
|
||||
|
||||
let toml_path = cc_switch_lib::get_codex_config_path();
|
||||
assert!(
|
||||
toml_path.exists(),
|
||||
"enabling server should trigger sync to ~/.codex/config.toml"
|
||||
);
|
||||
let toml_text = fs::read_to_string(&toml_path).expect("read codex config");
|
||||
assert!(
|
||||
toml_text.contains("codex-server"),
|
||||
"codex config should include the enabled server definition"
|
||||
);
|
||||
}
|
||||
327
src-tauri/tests/provider_commands.rs
Normal file
@@ -0,0 +1,327 @@
|
||||
use serde_json::json;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use cc_switch_lib::{
|
||||
get_codex_auth_path, get_codex_config_path, read_json_file, switch_provider_test_hook,
|
||||
write_codex_live_atomic, AppError, AppState, AppType, MultiAppConfig, Provider,
|
||||
};
|
||||
|
||||
#[path = "support.rs"]
|
||||
mod support;
|
||||
use support::{ensure_test_home, reset_test_fs, test_mutex};
|
||||
|
||||
#[test]
|
||||
fn switch_provider_updates_codex_live_and_state() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let legacy_auth = json!({"OPENAI_API_KEY": "legacy-key"});
|
||||
let legacy_config = r#"[mcp_servers.legacy]
|
||||
type = "stdio"
|
||||
command = "echo"
|
||||
"#;
|
||||
write_codex_live_atomic(&legacy_auth, Some(legacy_config))
|
||||
.expect("seed existing codex live config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.current = "old-provider".to_string();
|
||||
manager.providers.insert(
|
||||
"old-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"old-provider".to_string(),
|
||||
"Legacy".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "stale"},
|
||||
"config": "stale-config"
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"new-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"new-provider".to_string(),
|
||||
"Latest".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "fresh-key"},
|
||||
"config": r#"[mcp_servers.latest]
|
||||
type = "stdio"
|
||||
command = "say"
|
||||
"#
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
config.mcp.codex.servers.insert(
|
||||
"echo-server".into(),
|
||||
json!({
|
||||
"id": "echo-server",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
switch_provider_test_hook(&app_state, AppType::Codex, "new-provider")
|
||||
.expect("switch provider should succeed");
|
||||
|
||||
let auth_value: serde_json::Value =
|
||||
read_json_file(&get_codex_auth_path()).expect("read auth.json");
|
||||
assert_eq!(
|
||||
auth_value
|
||||
.get("OPENAI_API_KEY")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(""),
|
||||
"fresh-key",
|
||||
"live auth.json should reflect new provider"
|
||||
);
|
||||
|
||||
let config_text = std::fs::read_to_string(get_codex_config_path()).expect("read config.toml");
|
||||
assert!(
|
||||
config_text.contains("mcp_servers.echo-server"),
|
||||
"config.toml should contain synced MCP servers"
|
||||
);
|
||||
|
||||
let locked = app_state.config.read().expect("lock config after switch");
|
||||
let manager = locked
|
||||
.get_manager(&AppType::Codex)
|
||||
.expect("codex manager after switch");
|
||||
assert_eq!(manager.current, "new-provider", "current provider updated");
|
||||
|
||||
let new_provider = manager
|
||||
.providers
|
||||
.get("new-provider")
|
||||
.expect("new provider exists");
|
||||
let new_config_text = new_provider
|
||||
.settings_config
|
||||
.get("config")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(
|
||||
new_config_text, config_text,
|
||||
"provider config snapshot should match live file"
|
||||
);
|
||||
|
||||
let legacy = manager
|
||||
.providers
|
||||
.get("old-provider")
|
||||
.expect("legacy provider still exists");
|
||||
let legacy_auth_value = legacy
|
||||
.settings_config
|
||||
.get("auth")
|
||||
.and_then(|v| v.get("OPENAI_API_KEY"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
assert_eq!(
|
||||
legacy_auth_value, "legacy-key",
|
||||
"previous provider should be backfilled with live auth"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switch_provider_missing_provider_returns_error() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager")
|
||||
.current = "does-not-exist".to_string();
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
let err = switch_provider_test_hook(&app_state, AppType::Claude, "missing-provider")
|
||||
.expect_err("switching to a missing provider should fail");
|
||||
|
||||
assert!(
|
||||
err.to_string().contains("供应商不存在"),
|
||||
"error message should mention missing provider"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switch_provider_updates_claude_live_and_state() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let settings_path = cc_switch_lib::get_claude_settings_path();
|
||||
if let Some(parent) = settings_path.parent() {
|
||||
std::fs::create_dir_all(parent).expect("create claude settings dir");
|
||||
}
|
||||
let legacy_live = json!({
|
||||
"env": {
|
||||
"ANTHROPIC_API_KEY": "legacy-key"
|
||||
},
|
||||
"workspace": {
|
||||
"path": "/tmp/workspace"
|
||||
}
|
||||
});
|
||||
std::fs::write(
|
||||
&settings_path,
|
||||
serde_json::to_string_pretty(&legacy_live).expect("serialize legacy live"),
|
||||
)
|
||||
.expect("seed claude live config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.current = "old-provider".to_string();
|
||||
manager.providers.insert(
|
||||
"old-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"old-provider".to_string(),
|
||||
"Legacy Claude".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "stale-key" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"new-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"new-provider".to_string(),
|
||||
"Fresh Claude".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "fresh-key" },
|
||||
"workspace": { "path": "/tmp/new-workspace" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
switch_provider_test_hook(&app_state, AppType::Claude, "new-provider")
|
||||
.expect("switch provider should succeed");
|
||||
|
||||
let live_after: serde_json::Value =
|
||||
read_json_file(&settings_path).expect("read claude live settings");
|
||||
assert_eq!(
|
||||
live_after
|
||||
.get("env")
|
||||
.and_then(|env| env.get("ANTHROPIC_API_KEY"))
|
||||
.and_then(|key| key.as_str()),
|
||||
Some("fresh-key"),
|
||||
"live settings.json should reflect new provider auth"
|
||||
);
|
||||
|
||||
let locked = app_state.config.read().expect("lock config after switch");
|
||||
let manager = locked
|
||||
.get_manager(&AppType::Claude)
|
||||
.expect("claude manager after switch");
|
||||
assert_eq!(manager.current, "new-provider", "current provider updated");
|
||||
|
||||
let legacy_provider = manager
|
||||
.providers
|
||||
.get("old-provider")
|
||||
.expect("legacy provider still exists");
|
||||
assert_eq!(
|
||||
legacy_provider.settings_config, legacy_live,
|
||||
"previous provider should receive backfilled live config"
|
||||
);
|
||||
|
||||
let new_provider = manager
|
||||
.providers
|
||||
.get("new-provider")
|
||||
.expect("new provider exists");
|
||||
assert_eq!(
|
||||
new_provider
|
||||
.settings_config
|
||||
.get("env")
|
||||
.and_then(|env| env.get("ANTHROPIC_API_KEY"))
|
||||
.and_then(|key| key.as_str()),
|
||||
Some("fresh-key"),
|
||||
"new provider snapshot should retain fresh auth"
|
||||
);
|
||||
|
||||
drop(locked);
|
||||
|
||||
let home_dir = std::env::var("HOME").expect("HOME should be set by ensure_test_home");
|
||||
let config_path = std::path::Path::new(&home_dir)
|
||||
.join(".cc-switch")
|
||||
.join("config.json");
|
||||
assert!(
|
||||
config_path.exists(),
|
||||
"switching provider should persist config.json"
|
||||
);
|
||||
let persisted: serde_json::Value =
|
||||
serde_json::from_str(&std::fs::read_to_string(&config_path).expect("read saved config"))
|
||||
.expect("parse saved config");
|
||||
assert_eq!(
|
||||
persisted
|
||||
.get("claude")
|
||||
.and_then(|claude| claude.get("current"))
|
||||
.and_then(|current| current.as_str()),
|
||||
Some("new-provider"),
|
||||
"saved config.json should record the new current provider"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switch_provider_codex_missing_auth_returns_error_and_keeps_state() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.providers.insert(
|
||||
"invalid".to_string(),
|
||||
Provider::with_id(
|
||||
"invalid".to_string(),
|
||||
"Broken Codex".to_string(),
|
||||
json!({
|
||||
"config": "[mcp_servers.test]\ncommand = \"noop\""
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
let err = switch_provider_test_hook(&app_state, AppType::Codex, "invalid")
|
||||
.expect_err("switching should fail when auth missing");
|
||||
match err {
|
||||
AppError::Config(msg) => assert!(
|
||||
msg.contains("auth"),
|
||||
"expected auth missing error message, got {msg}"
|
||||
),
|
||||
other => panic!("expected config error, got {other:?}"),
|
||||
}
|
||||
|
||||
let locked = app_state.config.read().expect("lock config after failure");
|
||||
let manager = locked.get_manager(&AppType::Codex).expect("codex manager");
|
||||
assert!(
|
||||
manager.current.is_empty(),
|
||||
"current provider should remain empty on failure"
|
||||
);
|
||||
}
|
||||
450
src-tauri/tests/provider_service.rs
Normal file
@@ -0,0 +1,450 @@
|
||||
use serde_json::json;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use cc_switch_lib::{
|
||||
get_claude_settings_path, read_json_file, write_codex_live_atomic, AppError, AppState, AppType,
|
||||
MultiAppConfig, Provider, ProviderService,
|
||||
};
|
||||
|
||||
#[path = "support.rs"]
|
||||
mod support;
|
||||
use support::{ensure_test_home, reset_test_fs, test_mutex};
|
||||
|
||||
fn sanitize_provider_name(name: &str) -> String {
|
||||
name.chars()
|
||||
.map(|c| match c {
|
||||
'<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*' => '-',
|
||||
_ => c,
|
||||
})
|
||||
.collect::<String>()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_switch_codex_updates_live_and_config() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let legacy_auth = json!({ "OPENAI_API_KEY": "legacy-key" });
|
||||
let legacy_config = r#"[mcp_servers.legacy]
|
||||
type = "stdio"
|
||||
command = "echo"
|
||||
"#;
|
||||
write_codex_live_atomic(&legacy_auth, Some(legacy_config))
|
||||
.expect("seed existing codex live config");
|
||||
|
||||
let mut initial_config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = initial_config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.current = "old-provider".to_string();
|
||||
manager.providers.insert(
|
||||
"old-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"old-provider".to_string(),
|
||||
"Legacy".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "stale"},
|
||||
"config": "stale-config"
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"new-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"new-provider".to_string(),
|
||||
"Latest".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "fresh-key"},
|
||||
"config": r#"[mcp_servers.latest]
|
||||
type = "stdio"
|
||||
command = "say"
|
||||
"#
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
initial_config.mcp.codex.servers.insert(
|
||||
"echo-server".into(),
|
||||
json!({
|
||||
"id": "echo-server",
|
||||
"enabled": true,
|
||||
"server": {
|
||||
"type": "stdio",
|
||||
"command": "echo"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(initial_config),
|
||||
};
|
||||
|
||||
ProviderService::switch(&state, AppType::Codex, "new-provider")
|
||||
.expect("switch provider should succeed");
|
||||
|
||||
let auth_value: serde_json::Value =
|
||||
read_json_file(&cc_switch_lib::get_codex_auth_path()).expect("read auth.json");
|
||||
assert_eq!(
|
||||
auth_value.get("OPENAI_API_KEY").and_then(|v| v.as_str()),
|
||||
Some("fresh-key"),
|
||||
"live auth.json should reflect new provider"
|
||||
);
|
||||
|
||||
let config_text =
|
||||
std::fs::read_to_string(cc_switch_lib::get_codex_config_path()).expect("read config.toml");
|
||||
assert!(
|
||||
config_text.contains("mcp_servers.echo-server"),
|
||||
"config.toml should contain synced MCP servers"
|
||||
);
|
||||
|
||||
let guard = state.config.read().expect("read config after switch");
|
||||
let manager = guard
|
||||
.get_manager(&AppType::Codex)
|
||||
.expect("codex manager after switch");
|
||||
assert_eq!(manager.current, "new-provider", "current provider updated");
|
||||
|
||||
let new_provider = manager
|
||||
.providers
|
||||
.get("new-provider")
|
||||
.expect("new provider exists");
|
||||
let new_config_text = new_provider
|
||||
.settings_config
|
||||
.get("config")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(
|
||||
new_config_text, config_text,
|
||||
"provider config snapshot should match live file"
|
||||
);
|
||||
|
||||
let legacy = manager
|
||||
.providers
|
||||
.get("old-provider")
|
||||
.expect("legacy provider still exists");
|
||||
let legacy_auth_value = legacy
|
||||
.settings_config
|
||||
.get("auth")
|
||||
.and_then(|v| v.get("OPENAI_API_KEY"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
assert_eq!(
|
||||
legacy_auth_value, "legacy-key",
|
||||
"previous provider should be backfilled with live auth"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_switch_claude_updates_live_and_state() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let _home = ensure_test_home();
|
||||
|
||||
let settings_path = get_claude_settings_path();
|
||||
if let Some(parent) = settings_path.parent() {
|
||||
std::fs::create_dir_all(parent).expect("create claude settings dir");
|
||||
}
|
||||
let legacy_live = json!({
|
||||
"env": {
|
||||
"ANTHROPIC_API_KEY": "legacy-key"
|
||||
},
|
||||
"workspace": {
|
||||
"path": "/tmp/workspace"
|
||||
}
|
||||
});
|
||||
std::fs::write(
|
||||
&settings_path,
|
||||
serde_json::to_string_pretty(&legacy_live).expect("serialize legacy live"),
|
||||
)
|
||||
.expect("seed claude live config");
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.current = "old-provider".to_string();
|
||||
manager.providers.insert(
|
||||
"old-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"old-provider".to_string(),
|
||||
"Legacy Claude".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "stale-key" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"new-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"new-provider".to_string(),
|
||||
"Fresh Claude".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "fresh-key" },
|
||||
"workspace": { "path": "/tmp/new-workspace" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
ProviderService::switch(&state, AppType::Claude, "new-provider")
|
||||
.expect("switch provider should succeed");
|
||||
|
||||
let live_after: serde_json::Value =
|
||||
read_json_file(&settings_path).expect("read claude live settings");
|
||||
assert_eq!(
|
||||
live_after
|
||||
.get("env")
|
||||
.and_then(|env| env.get("ANTHROPIC_API_KEY"))
|
||||
.and_then(|key| key.as_str()),
|
||||
Some("fresh-key"),
|
||||
"live settings.json should reflect new provider auth"
|
||||
);
|
||||
|
||||
let guard = state
|
||||
.config
|
||||
.read()
|
||||
.expect("read claude config after switch");
|
||||
let manager = guard
|
||||
.get_manager(&AppType::Claude)
|
||||
.expect("claude manager after switch");
|
||||
assert_eq!(manager.current, "new-provider", "current provider updated");
|
||||
|
||||
let legacy_provider = manager
|
||||
.providers
|
||||
.get("old-provider")
|
||||
.expect("legacy provider still exists");
|
||||
assert_eq!(
|
||||
legacy_provider.settings_config, legacy_live,
|
||||
"previous provider should receive backfilled live config"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_switch_missing_provider_returns_error() {
|
||||
let state = AppState {
|
||||
config: RwLock::new(MultiAppConfig::default()),
|
||||
};
|
||||
|
||||
let err = ProviderService::switch(&state, AppType::Claude, "missing")
|
||||
.expect_err("switching missing provider should fail");
|
||||
match err {
|
||||
AppError::Localized { key, .. } => assert_eq!(key, "provider.not_found"),
|
||||
other => panic!("expected Localized error for provider not found, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_switch_codex_missing_auth_returns_error() {
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.providers.insert(
|
||||
"invalid".to_string(),
|
||||
Provider::with_id(
|
||||
"invalid".to_string(),
|
||||
"Broken Codex".to_string(),
|
||||
json!({
|
||||
"config": "[mcp_servers.test]\ncommand = \"noop\""
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
let err = ProviderService::switch(&state, AppType::Codex, "invalid")
|
||||
.expect_err("switching should fail without auth");
|
||||
match err {
|
||||
AppError::Config(msg) => assert!(
|
||||
msg.contains("auth"),
|
||||
"expected auth related message, got {msg}"
|
||||
),
|
||||
other => panic!("expected config error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_delete_codex_removes_provider_and_files() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Codex)
|
||||
.expect("codex manager");
|
||||
manager.current = "keep".to_string();
|
||||
manager.providers.insert(
|
||||
"keep".to_string(),
|
||||
Provider::with_id(
|
||||
"keep".to_string(),
|
||||
"Keep".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "keep-key"},
|
||||
"config": ""
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"to-delete".to_string(),
|
||||
Provider::with_id(
|
||||
"to-delete".to_string(),
|
||||
"DeleteCodex".to_string(),
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "delete-key"},
|
||||
"config": ""
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let sanitized = sanitize_provider_name("DeleteCodex");
|
||||
let codex_dir = home.join(".codex");
|
||||
std::fs::create_dir_all(&codex_dir).expect("create codex dir");
|
||||
let auth_path = codex_dir.join(format!("auth-{}.json", sanitized));
|
||||
let cfg_path = codex_dir.join(format!("config-{}.toml", sanitized));
|
||||
std::fs::write(&auth_path, "{}").expect("seed auth file");
|
||||
std::fs::write(&cfg_path, "base_url = \"https://example\"").expect("seed config file");
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
ProviderService::delete(&app_state, AppType::Codex, "to-delete")
|
||||
.expect("delete provider should succeed");
|
||||
|
||||
let locked = app_state.config.read().expect("lock config after delete");
|
||||
let manager = locked.get_manager(&AppType::Codex).expect("codex manager");
|
||||
assert!(
|
||||
!manager.providers.contains_key("to-delete"),
|
||||
"provider entry should be removed"
|
||||
);
|
||||
assert!(
|
||||
!auth_path.exists() && !cfg_path.exists(),
|
||||
"provider-specific files should be deleted"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_delete_claude_removes_provider_files() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.current = "keep".to_string();
|
||||
manager.providers.insert(
|
||||
"keep".to_string(),
|
||||
Provider::with_id(
|
||||
"keep".to_string(),
|
||||
"Keep".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "keep-key" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
manager.providers.insert(
|
||||
"delete".to_string(),
|
||||
Provider::with_id(
|
||||
"delete".to_string(),
|
||||
"DeleteClaude".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "delete-key" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let sanitized = sanitize_provider_name("DeleteClaude");
|
||||
let claude_dir = home.join(".claude");
|
||||
std::fs::create_dir_all(&claude_dir).expect("create claude dir");
|
||||
let by_name = claude_dir.join(format!("settings-{}.json", sanitized));
|
||||
let by_id = claude_dir.join("settings-delete.json");
|
||||
std::fs::write(&by_name, "{}").expect("seed settings by name");
|
||||
std::fs::write(&by_id, "{}").expect("seed settings by id");
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
ProviderService::delete(&app_state, AppType::Claude, "delete").expect("delete claude provider");
|
||||
|
||||
let locked = app_state.config.read().expect("lock config after delete");
|
||||
let manager = locked
|
||||
.get_manager(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
assert!(
|
||||
!manager.providers.contains_key("delete"),
|
||||
"claude provider should be removed"
|
||||
);
|
||||
assert!(
|
||||
!by_name.exists() && !by_id.exists(),
|
||||
"provider config files should be deleted"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_service_delete_current_provider_returns_error() {
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.current = "keep".to_string();
|
||||
manager.providers.insert(
|
||||
"keep".to_string(),
|
||||
Provider::with_id(
|
||||
"keep".to_string(),
|
||||
"Keep".to_string(),
|
||||
json!({
|
||||
"env": { "ANTHROPIC_API_KEY": "keep-key" }
|
||||
}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let app_state = AppState {
|
||||
config: RwLock::new(config),
|
||||
};
|
||||
|
||||
let err = ProviderService::delete(&app_state, AppType::Claude, "keep")
|
||||
.expect_err("deleting current provider should fail");
|
||||
match err {
|
||||
AppError::Localized { zh, .. } => assert!(
|
||||
zh.contains("不能删除当前正在使用的供应商"),
|
||||
"unexpected message: {zh}"
|
||||
),
|
||||
AppError::Config(msg) => assert!(
|
||||
msg.contains("不能删除当前正在使用的供应商"),
|
||||
"unexpected message: {msg}"
|
||||
),
|
||||
other => panic!("expected Config error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
47
src-tauri/tests/support.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
use cc_switch_lib::{update_settings, AppSettings};
|
||||
|
||||
/// 为测试设置隔离的 HOME 目录,避免污染真实用户数据。
|
||||
pub fn ensure_test_home() -> &'static Path {
|
||||
static HOME: OnceLock<PathBuf> = OnceLock::new();
|
||||
HOME.get_or_init(|| {
|
||||
let base = std::env::temp_dir().join("cc-switch-test-home");
|
||||
if base.exists() {
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
std::fs::create_dir_all(&base).expect("create test home");
|
||||
std::env::set_var("HOME", &base);
|
||||
#[cfg(windows)]
|
||||
std::env::set_var("USERPROFILE", &base);
|
||||
base
|
||||
})
|
||||
.as_path()
|
||||
}
|
||||
|
||||
/// 清理测试目录中生成的配置文件与缓存。
|
||||
pub fn reset_test_fs() {
|
||||
let home = ensure_test_home();
|
||||
for sub in [".claude", ".codex", ".cc-switch"] {
|
||||
let path = home.join(sub);
|
||||
if path.exists() {
|
||||
if let Err(err) = std::fs::remove_dir_all(&path) {
|
||||
eprintln!("failed to clean {}: {}", path.display(), err);
|
||||
}
|
||||
}
|
||||
}
|
||||
let claude_json = home.join(".claude.json");
|
||||
if claude_json.exists() {
|
||||
let _ = std::fs::remove_file(&claude_json);
|
||||
}
|
||||
|
||||
// 重置内存中的设置缓存,确保测试环境不受上一次调用影响
|
||||
let _ = update_settings(AppSettings::default());
|
||||
}
|
||||
|
||||
/// 全局互斥锁,避免多测试并发写入相同的 HOME 目录。
|
||||
pub fn test_mutex() -> &'static Mutex<()> {
|
||||
static MUTEX: OnceLock<Mutex<()>> = OnceLock::new();
|
||||
MUTEX.get_or_init(|| Mutex::new(()))
|
||||
}
|
||||
360
src-tauri/wix/per-user-main.wxs
Normal file
@@ -0,0 +1,360 @@
|
||||
<?if $(sys.BUILDARCH)="x86"?>
|
||||
<?define Win64 = "no" ?>
|
||||
<?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
|
||||
<?elseif $(sys.BUILDARCH)="x64"?>
|
||||
<?define Win64 = "yes" ?>
|
||||
<?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
|
||||
<?elseif $(sys.BUILDARCH)="arm64"?>
|
||||
<?define Win64 = "yes" ?>
|
||||
<?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
|
||||
<?else?>
|
||||
<?error Unsupported value of sys.BUILDARCH=$(sys.BUILDARCH)?>
|
||||
<?endif?>
|
||||
|
||||
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
|
||||
<Product
|
||||
Id="*"
|
||||
Name="{{product_name}}"
|
||||
UpgradeCode="{{upgrade_code}}"
|
||||
Language="!(loc.TauriLanguage)"
|
||||
Manufacturer="{{manufacturer}}"
|
||||
Version="{{version}}">
|
||||
|
||||
<Package Id="*"
|
||||
Keywords="Installer"
|
||||
InstallerVersion="450"
|
||||
Languages="0"
|
||||
Compressed="yes"
|
||||
InstallScope="perUser"
|
||||
InstallPrivileges="limited"
|
||||
SummaryCodepage="!(loc.TauriCodepage)"/>
|
||||
|
||||
<!-- https://docs.microsoft.com/en-us/windows/win32/msi/reinstallmode -->
|
||||
<!-- reinstall all files; rewrite all registry entries; reinstall all shortcuts -->
|
||||
<Property Id="REINSTALLMODE" Value="amus" />
|
||||
|
||||
<!-- Auto launch app after installation, useful for passive mode which usually used in updates -->
|
||||
<Property Id="AUTOLAUNCHAPP" Secure="yes" />
|
||||
<!-- Property to forward cli args to the launched app to not lose those of the pre-update instance -->
|
||||
<Property Id="LAUNCHAPPARGS" Secure="yes" />
|
||||
|
||||
{{#if allow_downgrades}}
|
||||
<MajorUpgrade Schedule="afterInstallInitialize" AllowDowngrades="yes" />
|
||||
{{else}}
|
||||
<MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="!(loc.DowngradeErrorMessage)" AllowSameVersionUpgrades="yes" />
|
||||
{{/if}}
|
||||
|
||||
<InstallExecuteSequence>
|
||||
<RemoveShortcuts>Installed AND NOT UPGRADINGPRODUCTCODE</RemoveShortcuts>
|
||||
</InstallExecuteSequence>
|
||||
|
||||
<Media Id="1" Cabinet="app.cab" EmbedCab="yes" />
|
||||
|
||||
{{#if banner_path}}
|
||||
<WixVariable Id="WixUIBannerBmp" Value="{{banner_path}}" />
|
||||
{{/if}}
|
||||
{{#if dialog_image_path}}
|
||||
<WixVariable Id="WixUIDialogBmp" Value="{{dialog_image_path}}" />
|
||||
{{/if}}
|
||||
{{#if license}}
|
||||
<WixVariable Id="WixUILicenseRtf" Value="{{license}}" />
|
||||
{{/if}}
|
||||
|
||||
<Icon Id="ProductIcon" SourceFile="{{icon_path}}"/>
|
||||
<Property Id="ARPPRODUCTICON" Value="ProductIcon" />
|
||||
<Property Id="ARPNOREPAIR" Value="yes" Secure="yes" /> <!-- Remove repair -->
|
||||
<SetProperty Id="ARPNOMODIFY" Value="1" After="InstallValidate" Sequence="execute"/>
|
||||
|
||||
{{#if homepage}}
|
||||
<Property Id="ARPURLINFOABOUT" Value="{{homepage}}"/>
|
||||
<Property Id="ARPHELPLINK" Value="{{homepage}}"/>
|
||||
<Property Id="ARPURLUPDATEINFO" Value="{{homepage}}"/>
|
||||
{{/if}}
|
||||
|
||||
<Property Id="INSTALLDIR">
|
||||
<!-- First attempt: Search for "InstallDir" -->
|
||||
<RegistrySearch Id="PrevInstallDirWithName" Root="HKCU" Key="Software\\{{manufacturer}}\\{{product_name}}" Name="InstallDir" Type="raw" />
|
||||
|
||||
<!-- Second attempt: If the first fails, search for the default key value (this is how the nsis installer currently stores the path) -->
|
||||
<RegistrySearch Id="PrevInstallDirNoName" Root="HKCU" Key="Software\\{{manufacturer}}\\{{product_name}}" Type="raw" />
|
||||
</Property>
|
||||
|
||||
<!-- launch app checkbox -->
|
||||
<Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="!(loc.LaunchApp)" />
|
||||
<Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1"/>
|
||||
<CustomAction Id="LaunchApplication" Impersonate="yes" FileKey="Path" ExeCommand="[LAUNCHAPPARGS]" Return="asyncNoWait" />
|
||||
|
||||
<UI>
|
||||
<!-- launch app checkbox -->
|
||||
<Publish Dialog="ExitDialog" Control="Finish" Event="DoAction" Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
|
||||
|
||||
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
|
||||
|
||||
{{#unless license}}
|
||||
<!-- Skip license dialog -->
|
||||
<Publish Dialog="WelcomeDlg"
|
||||
Control="Next"
|
||||
Event="NewDialog"
|
||||
Value="InstallDirDlg"
|
||||
Order="2">1</Publish>
|
||||
<Publish Dialog="InstallDirDlg"
|
||||
Control="Back"
|
||||
Event="NewDialog"
|
||||
Value="WelcomeDlg"
|
||||
Order="2">1</Publish>
|
||||
{{/unless}}
|
||||
</UI>
|
||||
|
||||
<UIRef Id="WixUI_InstallDir" />
|
||||
|
||||
<Directory Id="TARGETDIR" Name="SourceDir">
|
||||
<Directory Id="DesktopFolder" Name="Desktop">
|
||||
<Component Id="ApplicationShortcutDesktop" Guid="*">
|
||||
<Shortcut Id="ApplicationDesktopShortcut" Name="{{product_name}}" Description="Runs {{product_name}}" Target="[!Path]" WorkingDirectory="INSTALLDIR" />
|
||||
<RemoveFolder Id="DesktopFolder" On="uninstall" />
|
||||
<RegistryValue Root="HKCU" Key="Software\\{{manufacturer}}\\{{product_name}}" Name="Desktop Shortcut" Type="integer" Value="1" KeyPath="yes" />
|
||||
</Component>
|
||||
</Directory>
|
||||
<Directory Id="LocalAppDataFolder">
|
||||
<Directory Id="TauriLocalAppDataPrograms" Name="Programs">
|
||||
<Directory Id="INSTALLDIR" Name="{{product_name}}"/>
|
||||
</Directory>
|
||||
</Directory>
|
||||
<Directory Id="ProgramMenuFolder">
|
||||
<Directory Id="ApplicationProgramsFolder" Name="{{product_name}}"/>
|
||||
</Directory>
|
||||
</Directory>
|
||||
|
||||
<DirectoryRef Id="INSTALLDIR">
|
||||
<Component Id="RegistryEntries" Guid="*">
|
||||
<RegistryKey Root="HKCU" Key="Software\\{{manufacturer}}\\{{product_name}}">
|
||||
<RegistryValue Name="InstallDir" Type="string" Value="[INSTALLDIR]" KeyPath="yes" />
|
||||
</RegistryKey>
|
||||
<!-- Change the Root to HKCU for perUser installations -->
|
||||
{{#each deep_link_protocols as |protocol| ~}}
|
||||
<RegistryKey Root="HKCU" Key="Software\Classes\\{{protocol}}">
|
||||
<RegistryValue Type="string" Name="URL Protocol" Value=""/>
|
||||
<RegistryValue Type="string" Value="URL:{{bundle_id}} protocol"/>
|
||||
<RegistryKey Key="DefaultIcon">
|
||||
<RegistryValue Type="string" Value=""[!Path]",0" />
|
||||
</RegistryKey>
|
||||
<RegistryKey Key="shell\open\command">
|
||||
<RegistryValue Type="string" Value=""[!Path]" "%1"" />
|
||||
</RegistryKey>
|
||||
</RegistryKey>
|
||||
{{/each~}}
|
||||
</Component>
|
||||
<Component Id="Path" Guid="{{path_component_guid}}" Win64="$(var.Win64)">
|
||||
<File Id="Path" Source="{{main_binary_path}}" KeyPath="no" Checksum="yes"/>
|
||||
<RegistryValue Root="HKCU" Key="Software\{{manufacturer}}\{{product_name}}" Name="PathComponent" Type="integer" Value="1" KeyPath="yes" />
|
||||
{{#each file_associations as |association| ~}}
|
||||
{{#each association.ext as |ext| ~}}
|
||||
<ProgId Id="{{../../product_name}}.{{ext}}" Advertise="yes" Description="{{association.description}}">
|
||||
<Extension Id="{{ext}}" Advertise="yes">
|
||||
<Verb Id="open" Command="Open with {{../../product_name}}" Argument=""%1"" />
|
||||
</Extension>
|
||||
</ProgId>
|
||||
{{/each~}}
|
||||
{{/each~}}
|
||||
</Component>
|
||||
{{#each binaries as |bin| ~}}
|
||||
<Component Id="{{ bin.id }}" Guid="{{bin.guid}}" Win64="$(var.Win64)">
|
||||
<File Id="Bin_{{ bin.id }}" Source="{{bin.path}}" KeyPath="yes"/>
|
||||
</Component>
|
||||
{{/each~}}
|
||||
{{#if enable_elevated_update_task}}
|
||||
<Component Id="UpdateTask" Guid="C492327D-9720-4CD5-8DB8-F09082AF44BE" Win64="$(var.Win64)">
|
||||
<File Id="UpdateTask" Source="update.xml" KeyPath="yes" Checksum="yes"/>
|
||||
</Component>
|
||||
<Component Id="UpdateTaskInstaller" Guid="011F25ED-9BE3-50A7-9E9B-3519ED2B9932" Win64="$(var.Win64)">
|
||||
<File Id="UpdateTaskInstaller" Source="install-task.ps1" KeyPath="yes" Checksum="yes"/>
|
||||
</Component>
|
||||
<Component Id="UpdateTaskUninstaller" Guid="D4F6CC3F-32DC-5FD0-95E8-782FFD7BBCE1" Win64="$(var.Win64)">
|
||||
<File Id="UpdateTaskUninstaller" Source="uninstall-task.ps1" KeyPath="yes" Checksum="yes"/>
|
||||
</Component>
|
||||
{{/if}}
|
||||
{{resources}}
|
||||
<Component Id="CMP_UninstallShortcut" Guid="*">
|
||||
|
||||
<Shortcut Id="UninstallShortcut"
|
||||
Name="Uninstall {{product_name}}"
|
||||
Description="Uninstalls {{product_name}}"
|
||||
Target="[System64Folder]msiexec.exe"
|
||||
Arguments="/x [ProductCode]" />
|
||||
|
||||
<RemoveFile Id="RemoveUserProgramsFiles" Directory="TauriLocalAppDataPrograms" Name="*" On="uninstall" />
|
||||
<RemoveFolder Id="RemoveUserProgramsFolder" Directory="TauriLocalAppDataPrograms" On="uninstall" />
|
||||
|
||||
<RemoveFolder Id="INSTALLDIR"
|
||||
On="uninstall" />
|
||||
|
||||
<RegistryValue Root="HKCU"
|
||||
Key="Software\\{{manufacturer}}\\{{product_name}}"
|
||||
Name="Uninstaller Shortcut"
|
||||
Type="integer"
|
||||
Value="1"
|
||||
KeyPath="yes" />
|
||||
</Component>
|
||||
</DirectoryRef>
|
||||
|
||||
<DirectoryRef Id="ApplicationProgramsFolder">
|
||||
<Component Id="ApplicationShortcut" Guid="*">
|
||||
<Shortcut Id="ApplicationStartMenuShortcut"
|
||||
Name="{{product_name}}"
|
||||
Description="Runs {{product_name}}"
|
||||
Target="[!Path]"
|
||||
Icon="ProductIcon"
|
||||
WorkingDirectory="INSTALLDIR">
|
||||
<ShortcutProperty Key="System.AppUserModel.ID" Value="{{bundle_id}}"/>
|
||||
</Shortcut>
|
||||
<RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
|
||||
<RegistryValue Root="HKCU" Key="Software\\{{manufacturer}}\\{{product_name}}" Name="Start Menu Shortcut" Type="integer" Value="1" KeyPath="yes"/>
|
||||
</Component>
|
||||
</DirectoryRef>
|
||||
|
||||
{{#each merge_modules as |msm| ~}}
|
||||
<DirectoryRef Id="TARGETDIR">
|
||||
<Merge Id="{{ msm.name }}" SourceFile="{{ msm.path }}" DiskId="1" Language="!(loc.TauriLanguage)" />
|
||||
</DirectoryRef>
|
||||
|
||||
<Feature Id="{{ msm.name }}" Title="{{ msm.name }}" AllowAdvertise="no" Display="hidden" Level="1">
|
||||
<MergeRef Id="{{ msm.name }}"/>
|
||||
</Feature>
|
||||
{{/each~}}
|
||||
|
||||
<Feature
|
||||
Id="MainProgram"
|
||||
Title="Application"
|
||||
Description="!(loc.InstallAppFeature)"
|
||||
Level="1"
|
||||
ConfigurableDirectory="INSTALLDIR"
|
||||
AllowAdvertise="no"
|
||||
Display="expand"
|
||||
Absent="disallow">
|
||||
|
||||
<ComponentRef Id="RegistryEntries"/>
|
||||
|
||||
{{#each resource_file_ids as |resource_file_id| ~}}
|
||||
<ComponentRef Id="{{ resource_file_id }}"/>
|
||||
{{/each~}}
|
||||
|
||||
{{#if enable_elevated_update_task}}
|
||||
<ComponentRef Id="UpdateTask" />
|
||||
<ComponentRef Id="UpdateTaskInstaller" />
|
||||
<ComponentRef Id="UpdateTaskUninstaller" />
|
||||
{{/if}}
|
||||
|
||||
<Feature Id="ShortcutsFeature"
|
||||
Title="Shortcuts"
|
||||
Level="1">
|
||||
<ComponentRef Id="Path"/>
|
||||
<ComponentRef Id="CMP_UninstallShortcut" />
|
||||
<ComponentRef Id="ApplicationShortcut" />
|
||||
<ComponentRef Id="ApplicationShortcutDesktop" />
|
||||
</Feature>
|
||||
|
||||
<Feature
|
||||
Id="Environment"
|
||||
Title="PATH Environment Variable"
|
||||
Description="!(loc.PathEnvVarFeature)"
|
||||
Level="1"
|
||||
Absent="allow">
|
||||
<ComponentRef Id="Path"/>
|
||||
{{#each binaries as |bin| ~}}
|
||||
<ComponentRef Id="{{ bin.id }}"/>
|
||||
{{/each~}}
|
||||
</Feature>
|
||||
</Feature>
|
||||
|
||||
<Feature Id="External" AllowAdvertise="no" Absent="disallow">
|
||||
{{#each component_group_refs as |id| ~}}
|
||||
<ComponentGroupRef Id="{{ id }}"/>
|
||||
{{/each~}}
|
||||
{{#each component_refs as |id| ~}}
|
||||
<ComponentRef Id="{{ id }}"/>
|
||||
{{/each~}}
|
||||
{{#each feature_group_refs as |id| ~}}
|
||||
<FeatureGroupRef Id="{{ id }}"/>
|
||||
{{/each~}}
|
||||
{{#each feature_refs as |id| ~}}
|
||||
<FeatureRef Id="{{ id }}"/>
|
||||
{{/each~}}
|
||||
{{#each merge_refs as |id| ~}}
|
||||
<MergeRef Id="{{ id }}"/>
|
||||
{{/each~}}
|
||||
</Feature>
|
||||
|
||||
{{#if install_webview}}
|
||||
<!-- WebView2 -->
|
||||
<Property Id="WVRTINSTALLED">
|
||||
<RegistrySearch Id="WVRTInstalledSystem" Root="HKLM" Key="SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" Name="pv" Type="raw" Win64="no" />
|
||||
<RegistrySearch Id="WVRTInstalledUser" Root="HKCU" Key="SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" Name="pv" Type="raw"/>
|
||||
</Property>
|
||||
|
||||
{{#if download_bootstrapper}}
|
||||
<CustomAction Id='DownloadAndInvokeBootstrapper' Directory="INSTALLDIR" Execute="deferred" ExeCommand='powershell.exe -NoProfile -windowstyle hidden try [\{] [\[]Net.ServicePointManager[\]]::SecurityProtocol = [\[]Net.SecurityProtocolType[\]]::Tls12 [\}] catch [\{][\}]; Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/p/?LinkId=2124703" -OutFile "$env:TEMP\MicrosoftEdgeWebview2Setup.exe" ; Start-Process -FilePath "$env:TEMP\MicrosoftEdgeWebview2Setup.exe" -ArgumentList ({{webview_installer_args}} '/install') -Wait' Return='check'/>
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action='DownloadAndInvokeBootstrapper' Before='InstallFinalize'>
|
||||
<![CDATA[NOT(REMOVE OR WVRTINSTALLED)]]>
|
||||
</Custom>
|
||||
</InstallExecuteSequence>
|
||||
{{/if}}
|
||||
|
||||
<!-- Embedded webview bootstrapper mode -->
|
||||
{{#if webview2_bootstrapper_path}}
|
||||
<Binary Id="MicrosoftEdgeWebview2Setup.exe" SourceFile="{{webview2_bootstrapper_path}}"/>
|
||||
<CustomAction Id='InvokeBootstrapper' BinaryKey='MicrosoftEdgeWebview2Setup.exe' Execute="deferred" ExeCommand='{{webview_installer_args}} /install' Return='check' />
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action='InvokeBootstrapper' Before='InstallFinalize'>
|
||||
<![CDATA[NOT(REMOVE OR WVRTINSTALLED)]]>
|
||||
</Custom>
|
||||
</InstallExecuteSequence>
|
||||
{{/if}}
|
||||
|
||||
<!-- Embedded offline installer -->
|
||||
{{#if webview2_installer_path}}
|
||||
<Binary Id="MicrosoftEdgeWebView2RuntimeInstaller.exe" SourceFile="{{webview2_installer_path}}"/>
|
||||
<CustomAction Id='InvokeStandalone' BinaryKey='MicrosoftEdgeWebView2RuntimeInstaller.exe' Execute="deferred" ExeCommand='{{webview_installer_args}} /install' Return='check' />
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action='InvokeStandalone' Before='InstallFinalize'>
|
||||
<![CDATA[NOT(REMOVE OR WVRTINSTALLED)]]>
|
||||
</Custom>
|
||||
</InstallExecuteSequence>
|
||||
{{/if}}
|
||||
|
||||
{{/if}}
|
||||
|
||||
{{#if enable_elevated_update_task}}
|
||||
<!-- Install an elevated update task within Windows Task Scheduler -->
|
||||
<CustomAction
|
||||
Id="CreateUpdateTask"
|
||||
Return="check"
|
||||
Directory="INSTALLDIR"
|
||||
Execute="commit"
|
||||
Impersonate="yes"
|
||||
ExeCommand="powershell.exe -WindowStyle hidden .\install-task.ps1" />
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action='CreateUpdateTask' Before='InstallFinalize'>
|
||||
NOT(REMOVE)
|
||||
</Custom>
|
||||
</InstallExecuteSequence>
|
||||
<!-- Remove elevated update task during uninstall -->
|
||||
<CustomAction
|
||||
Id="DeleteUpdateTask"
|
||||
Return="check"
|
||||
Directory="INSTALLDIR"
|
||||
ExeCommand="powershell.exe -WindowStyle hidden .\uninstall-task.ps1" />
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action="DeleteUpdateTask" Before='InstallFinalize'>
|
||||
(REMOVE = "ALL") AND NOT UPGRADINGPRODUCTCODE
|
||||
</Custom>
|
||||
</InstallExecuteSequence>
|
||||
{{/if}}
|
||||
|
||||
<InstallExecuteSequence>
|
||||
<Custom Action="LaunchApplication" After="InstallFinalize">AUTOLAUNCHAPP AND NOT Installed</Custom>
|
||||
</InstallExecuteSequence>
|
||||
|
||||
<SetProperty Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" After="CostFinalize"/>
|
||||
</Product>
|
||||
</Wix>
|
||||
173
src/App.css
@@ -1,173 +0,0 @@
|
||||
.app {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
background: #3498db;
|
||||
color: white;
|
||||
padding: 0.75rem 2rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
user-select: none;
|
||||
min-height: 3rem;
|
||||
}
|
||||
|
||||
.app-header h1 {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.refresh-btn,
|
||||
.add-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.refresh-btn {
|
||||
background: #3498db;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.refresh-btn:hover:not(:disabled) {
|
||||
background: #2980b9;
|
||||
}
|
||||
|
||||
.refresh-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.import-btn {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.import-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.import-btn:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
background: #27ae60;
|
||||
color: white;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.add-btn:hover {
|
||||
background: #229954;
|
||||
}
|
||||
|
||||
.add-btn:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.app-main {
|
||||
flex: 1;
|
||||
padding: 2rem;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.config-path {
|
||||
margin-top: 2rem;
|
||||
padding: 1rem;
|
||||
background: #ecf0f1;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9rem;
|
||||
color: #7f8c8d;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.browse-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
background: #3498db;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
transition: all 0.2s;
|
||||
margin-left: 1rem;
|
||||
}
|
||||
|
||||
.browse-btn:hover {
|
||||
background: #2980b9;
|
||||
}
|
||||
|
||||
/* 供应商列表区域 - 相对定位容器 */
|
||||
.provider-section {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
/* 浮动通知 - 绝对定位,不占据空间 */
|
||||
.notification-floating {
|
||||
position: absolute;
|
||||
top: -10px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
z-index: 100;
|
||||
|
||||
padding: 0.75rem 1.25rem;
|
||||
border-radius: 6px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
|
||||
width: fit-content;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.fade-in {
|
||||
animation: fadeIn 0.3s ease-out;
|
||||
}
|
||||
|
||||
.fade-out {
|
||||
animation: fadeOut 0.3s ease-out;
|
||||
}
|
||||
|
||||
.notification-success {
|
||||
background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%);
|
||||
color: white;
|
||||
box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
|
||||
}
|
||||
|
||||
.notification-error {
|
||||
background: linear-gradient(135deg, #e74c3c 0%, #ec7063 100%);
|
||||
color: white;
|
||||
box-shadow: 0 4px 12px rgba(231, 76, 60, 0.3);
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeOut {
|
||||
from {
|
||||
opacity: 1;
|
||||
}
|
||||
to {
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
484
src/App.tsx
@@ -1,255 +1,297 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Provider } from "./types";
|
||||
import ProviderList from "./components/ProviderList";
|
||||
import AddProviderModal from "./components/AddProviderModal";
|
||||
import EditProviderModal from "./components/EditProviderModal";
|
||||
import { ConfirmDialog } from "./components/ConfirmDialog";
|
||||
import "./App.css";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import { Plus, Settings, Edit3 } from "lucide-react";
|
||||
import type { Provider } from "@/types";
|
||||
import { useProvidersQuery } from "@/lib/query";
|
||||
import {
|
||||
providersApi,
|
||||
settingsApi,
|
||||
type AppId,
|
||||
type ProviderSwitchEvent,
|
||||
} from "@/lib/api";
|
||||
import { useProviderActions } from "@/hooks/useProviderActions";
|
||||
import { extractErrorMessage } from "@/utils/errorUtils";
|
||||
import { AppSwitcher } from "@/components/AppSwitcher";
|
||||
import { ProviderList } from "@/components/providers/ProviderList";
|
||||
import { AddProviderDialog } from "@/components/providers/AddProviderDialog";
|
||||
import { EditProviderDialog } from "@/components/providers/EditProviderDialog";
|
||||
import { ConfirmDialog } from "@/components/ConfirmDialog";
|
||||
import { SettingsDialog } from "@/components/settings/SettingsDialog";
|
||||
import { UpdateBadge } from "@/components/UpdateBadge";
|
||||
import UsageScriptModal from "@/components/UsageScriptModal";
|
||||
import McpPanel from "@/components/mcp/McpPanel";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
function App() {
|
||||
const [providers, setProviders] = useState<Record<string, Provider>>({});
|
||||
const [currentProviderId, setCurrentProviderId] = useState<string>("");
|
||||
const [isAddModalOpen, setIsAddModalOpen] = useState(false);
|
||||
const [configStatus, setConfigStatus] = useState<{
|
||||
exists: boolean;
|
||||
path: string;
|
||||
} | null>(null);
|
||||
const [editingProviderId, setEditingProviderId] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
const [notification, setNotification] = useState<{
|
||||
message: string;
|
||||
type: "success" | "error";
|
||||
} | null>(null);
|
||||
const [isNotificationVisible, setIsNotificationVisible] = useState(false);
|
||||
const [confirmDialog, setConfirmDialog] = useState<{
|
||||
isOpen: boolean;
|
||||
title: string;
|
||||
message: string;
|
||||
onConfirm: () => void;
|
||||
} | null>(null);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 设置通知的辅助函数
|
||||
const showNotification = (
|
||||
message: string,
|
||||
type: "success" | "error",
|
||||
duration = 3000,
|
||||
) => {
|
||||
// 清除之前的定时器
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
const [activeApp, setActiveApp] = useState<AppId>("claude");
|
||||
const [isEditMode, setIsEditMode] = useState(false);
|
||||
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
|
||||
const [isAddOpen, setIsAddOpen] = useState(false);
|
||||
const [isMcpOpen, setIsMcpOpen] = useState(false);
|
||||
const [editingProvider, setEditingProvider] = useState<Provider | null>(null);
|
||||
const [usageProvider, setUsageProvider] = useState<Provider | null>(null);
|
||||
const [confirmDelete, setConfirmDelete] = useState<Provider | null>(null);
|
||||
|
||||
// 立即显示通知
|
||||
setNotification({ message, type });
|
||||
setIsNotificationVisible(true);
|
||||
const { data, isLoading, refetch } = useProvidersQuery(activeApp);
|
||||
const providers = useMemo(() => data?.providers ?? {}, [data]);
|
||||
const currentProviderId = data?.currentProviderId ?? "";
|
||||
|
||||
// 设置淡出定时器
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
setIsNotificationVisible(false);
|
||||
// 等待淡出动画完成后清除通知
|
||||
setTimeout(() => {
|
||||
setNotification(null);
|
||||
timeoutRef.current = null;
|
||||
}, 300); // 与CSS动画时间匹配
|
||||
}, duration);
|
||||
};
|
||||
// 🎯 使用 useProviderActions Hook 统一管理所有 Provider 操作
|
||||
const {
|
||||
addProvider,
|
||||
updateProvider,
|
||||
switchProvider,
|
||||
deleteProvider,
|
||||
saveUsageScript,
|
||||
} = useProviderActions(activeApp);
|
||||
|
||||
// 加载供应商列表
|
||||
// 监听来自托盘菜单的切换事件
|
||||
useEffect(() => {
|
||||
loadProviders();
|
||||
loadConfigStatus();
|
||||
}, []);
|
||||
let unsubscribe: (() => void) | undefined;
|
||||
|
||||
// 清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
const loadProviders = async () => {
|
||||
const loadedProviders = await window.api.getProviders();
|
||||
const currentId = await window.api.getCurrentProvider();
|
||||
setProviders(loadedProviders);
|
||||
setCurrentProviderId(currentId);
|
||||
|
||||
// 如果供应商列表为空,尝试自动导入现有配置为"default"供应商
|
||||
if (Object.keys(loadedProviders).length === 0) {
|
||||
await handleAutoImportDefault();
|
||||
}
|
||||
};
|
||||
|
||||
const loadConfigStatus = async () => {
|
||||
const status = await window.api.getClaudeConfigStatus();
|
||||
setConfigStatus({
|
||||
exists: Boolean(status?.exists),
|
||||
path: String(status?.path || ""),
|
||||
});
|
||||
};
|
||||
|
||||
// 生成唯一ID
|
||||
const generateId = () => {
|
||||
return crypto.randomUUID();
|
||||
};
|
||||
|
||||
const handleAddProvider = async (provider: Omit<Provider, "id">) => {
|
||||
const newProvider: Provider = {
|
||||
...provider,
|
||||
id: generateId(),
|
||||
};
|
||||
await window.api.addProvider(newProvider);
|
||||
await loadProviders();
|
||||
setIsAddModalOpen(false);
|
||||
};
|
||||
|
||||
const handleEditProvider = async (provider: Provider) => {
|
||||
try {
|
||||
await window.api.updateProvider(provider);
|
||||
await loadProviders();
|
||||
setEditingProviderId(null);
|
||||
// 显示编辑成功提示
|
||||
showNotification("供应商配置已保存", "success", 2000);
|
||||
} catch (error) {
|
||||
console.error("更新供应商失败:", error);
|
||||
setEditingProviderId(null);
|
||||
showNotification("保存失败,请重试", "error");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteProvider = async (id: string) => {
|
||||
const provider = providers[id];
|
||||
setConfirmDialog({
|
||||
isOpen: true,
|
||||
title: "删除供应商",
|
||||
message: `确定要删除供应商 "${provider?.name}" 吗?此操作无法撤销。`,
|
||||
onConfirm: async () => {
|
||||
await window.api.deleteProvider(id);
|
||||
await loadProviders();
|
||||
setConfirmDialog(null);
|
||||
showNotification("供应商删除成功", "success");
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const handleSwitchProvider = async (id: string) => {
|
||||
const success = await window.api.switchProvider(id);
|
||||
if (success) {
|
||||
setCurrentProviderId(id);
|
||||
// 显示重启提示
|
||||
showNotification(
|
||||
"切换成功!请重启 Claude Code 终端以生效",
|
||||
"success",
|
||||
2000,
|
||||
);
|
||||
} else {
|
||||
showNotification("切换失败,请检查配置", "error");
|
||||
}
|
||||
};
|
||||
|
||||
// 自动导入现有配置为"default"供应商
|
||||
const handleAutoImportDefault = async () => {
|
||||
try {
|
||||
const result = await window.api.importCurrentConfigAsDefault();
|
||||
|
||||
if (result.success) {
|
||||
await loadProviders();
|
||||
showNotification(
|
||||
"已自动导入现有配置为 default 供应商",
|
||||
"success",
|
||||
3000,
|
||||
const setupListener = async () => {
|
||||
try {
|
||||
unsubscribe = await providersApi.onSwitched(
|
||||
async (event: ProviderSwitchEvent) => {
|
||||
if (event.appType === activeApp) {
|
||||
await refetch();
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("[App] Failed to subscribe provider switch event", error);
|
||||
}
|
||||
// 如果导入失败(比如没有现有配置),静默处理,不显示错误
|
||||
};
|
||||
|
||||
setupListener();
|
||||
return () => {
|
||||
unsubscribe?.();
|
||||
};
|
||||
}, [activeApp, refetch]);
|
||||
|
||||
// 打开网站链接
|
||||
const handleOpenWebsite = async (url: string) => {
|
||||
try {
|
||||
await settingsApi.openExternal(url);
|
||||
} catch (error) {
|
||||
console.error("自动导入默认配置失败:", error);
|
||||
// 静默处理,不影响用户体验
|
||||
const detail =
|
||||
extractErrorMessage(error) ||
|
||||
t("notifications.openLinkFailed", {
|
||||
defaultValue: "链接打开失败",
|
||||
});
|
||||
toast.error(detail);
|
||||
}
|
||||
};
|
||||
|
||||
const handleOpenConfigFolder = async () => {
|
||||
await window.api.openConfigFolder();
|
||||
// 编辑供应商
|
||||
const handleEditProvider = async (provider: Provider) => {
|
||||
await updateProvider(provider);
|
||||
setEditingProvider(null);
|
||||
};
|
||||
|
||||
// 确认删除供应商
|
||||
const handleConfirmDelete = async () => {
|
||||
if (!confirmDelete) return;
|
||||
await deleteProvider(confirmDelete.id);
|
||||
setConfirmDelete(null);
|
||||
};
|
||||
|
||||
// 复制供应商
|
||||
const handleDuplicateProvider = async (provider: Provider) => {
|
||||
// 1️⃣ 计算新的 sortIndex:如果原供应商有 sortIndex,则复制它
|
||||
const newSortIndex =
|
||||
provider.sortIndex !== undefined ? provider.sortIndex + 1 : undefined;
|
||||
|
||||
const duplicatedProvider: Omit<Provider, "id" | "createdAt"> = {
|
||||
name: `${provider.name} copy`,
|
||||
settingsConfig: JSON.parse(JSON.stringify(provider.settingsConfig)), // 深拷贝
|
||||
websiteUrl: provider.websiteUrl,
|
||||
category: provider.category,
|
||||
sortIndex: newSortIndex, // 复制原 sortIndex + 1
|
||||
meta: provider.meta
|
||||
? JSON.parse(JSON.stringify(provider.meta))
|
||||
: undefined, // 深拷贝
|
||||
};
|
||||
|
||||
// 2️⃣ 如果原供应商有 sortIndex,需要将后续所有供应商的 sortIndex +1
|
||||
if (provider.sortIndex !== undefined) {
|
||||
const updates = Object.values(providers)
|
||||
.filter(
|
||||
(p) =>
|
||||
p.sortIndex !== undefined &&
|
||||
p.sortIndex >= newSortIndex! &&
|
||||
p.id !== provider.id,
|
||||
)
|
||||
.map((p) => ({
|
||||
id: p.id,
|
||||
sortIndex: p.sortIndex! + 1,
|
||||
}));
|
||||
|
||||
// 先更新现有供应商的 sortIndex,为新供应商腾出位置
|
||||
if (updates.length > 0) {
|
||||
try {
|
||||
await providersApi.updateSortOrder(updates, activeApp);
|
||||
} catch (error) {
|
||||
console.error("[App] Failed to update sort order", error);
|
||||
toast.error(
|
||||
t("provider.sortUpdateFailed", {
|
||||
defaultValue: "排序更新失败",
|
||||
}),
|
||||
);
|
||||
return; // 如果排序更新失败,不继续添加
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3️⃣ 添加复制的供应商
|
||||
await addProvider(duplicatedProvider);
|
||||
};
|
||||
|
||||
// 导入配置成功后刷新
|
||||
const handleImportSuccess = async () => {
|
||||
await refetch();
|
||||
try {
|
||||
await providersApi.updateTrayMenu();
|
||||
} catch (error) {
|
||||
console.error("[App] Failed to refresh tray menu", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="app">
|
||||
<header className="app-header">
|
||||
<h1>Claude Code 供应商切换器</h1>
|
||||
<div className="header-actions">
|
||||
<button className="add-btn" onClick={() => setIsAddModalOpen(true)}>
|
||||
添加供应商
|
||||
</button>
|
||||
<div className="flex h-screen flex-col bg-gray-50 dark:bg-gray-950">
|
||||
<header className="flex-shrink-0 border-b border-gray-200 bg-white px-6 py-4 dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-1">
|
||||
<a
|
||||
href="https://github.com/farion1231/cc-switch"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
className="text-xl font-semibold text-blue-500 transition-colors hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300"
|
||||
>
|
||||
CC Switch
|
||||
</a>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => setIsSettingsOpen(true)}
|
||||
title={t("common.settings")}
|
||||
className="ml-2"
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => setIsEditMode(!isEditMode)}
|
||||
title={t(
|
||||
isEditMode ? "header.exitEditMode" : "header.enterEditMode",
|
||||
)}
|
||||
className={
|
||||
isEditMode
|
||||
? "text-blue-500 hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300"
|
||||
: ""
|
||||
}
|
||||
>
|
||||
<Edit3 className="h-4 w-4" />
|
||||
</Button>
|
||||
<UpdateBadge onClick={() => setIsSettingsOpen(true)} />
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<AppSwitcher activeApp={activeApp} onSwitch={setActiveApp} />
|
||||
<Button
|
||||
variant="mcp"
|
||||
onClick={() => setIsMcpOpen(true)}
|
||||
className="min-w-[80px]"
|
||||
>
|
||||
MCP
|
||||
</Button>
|
||||
<Button onClick={() => setIsAddOpen(true)}>
|
||||
<Plus className="h-4 w-4" />
|
||||
{t("header.addProvider")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="app-main">
|
||||
<div className="provider-section">
|
||||
{/* 浮动通知组件 */}
|
||||
{notification && (
|
||||
<div
|
||||
className={`notification-floating ${
|
||||
notification.type === "error"
|
||||
? "notification-error"
|
||||
: "notification-success"
|
||||
} ${isNotificationVisible ? "fade-in" : "fade-out"}`}
|
||||
>
|
||||
{notification.message}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<main className="flex-1 overflow-y-scroll">
|
||||
<div className="mx-auto max-w-4xl px-6 py-6">
|
||||
<ProviderList
|
||||
providers={providers}
|
||||
currentProviderId={currentProviderId}
|
||||
onSwitch={handleSwitchProvider}
|
||||
onDelete={handleDeleteProvider}
|
||||
onEdit={setEditingProviderId}
|
||||
appId={activeApp}
|
||||
isLoading={isLoading}
|
||||
isEditMode={isEditMode}
|
||||
onSwitch={switchProvider}
|
||||
onEdit={setEditingProvider}
|
||||
onDelete={setConfirmDelete}
|
||||
onDuplicate={handleDuplicateProvider}
|
||||
onConfigureUsage={setUsageProvider}
|
||||
onOpenWebsite={handleOpenWebsite}
|
||||
onCreate={() => setIsAddOpen(true)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{configStatus && (
|
||||
<div className="config-path">
|
||||
<span>
|
||||
配置文件位置: {configStatus.path}
|
||||
{!configStatus.exists ? "(未创建,切换或保存时会自动创建)" : ""}
|
||||
</span>
|
||||
<button
|
||||
className="browse-btn"
|
||||
onClick={handleOpenConfigFolder}
|
||||
title="打开配置文件夹"
|
||||
>
|
||||
打开
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
|
||||
{isAddModalOpen && (
|
||||
<AddProviderModal
|
||||
onAdd={handleAddProvider}
|
||||
onClose={() => setIsAddModalOpen(false)}
|
||||
<AddProviderDialog
|
||||
open={isAddOpen}
|
||||
onOpenChange={setIsAddOpen}
|
||||
appId={activeApp}
|
||||
onSubmit={addProvider}
|
||||
/>
|
||||
|
||||
<EditProviderDialog
|
||||
open={Boolean(editingProvider)}
|
||||
provider={editingProvider}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
setEditingProvider(null);
|
||||
}
|
||||
}}
|
||||
onSubmit={handleEditProvider}
|
||||
appId={activeApp}
|
||||
/>
|
||||
|
||||
{usageProvider && (
|
||||
<UsageScriptModal
|
||||
provider={usageProvider}
|
||||
appId={activeApp}
|
||||
isOpen={Boolean(usageProvider)}
|
||||
onClose={() => setUsageProvider(null)}
|
||||
onSave={(script) => {
|
||||
void saveUsageScript(usageProvider, script);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{editingProviderId && providers[editingProviderId] && (
|
||||
<EditProviderModal
|
||||
provider={providers[editingProviderId]}
|
||||
onSave={handleEditProvider}
|
||||
onClose={() => setEditingProviderId(null)}
|
||||
/>
|
||||
)}
|
||||
<ConfirmDialog
|
||||
isOpen={Boolean(confirmDelete)}
|
||||
title={t("confirm.deleteProvider")}
|
||||
message={
|
||||
confirmDelete
|
||||
? t("confirm.deleteProviderMessage", {
|
||||
name: confirmDelete.name,
|
||||
})
|
||||
: ""
|
||||
}
|
||||
onConfirm={() => void handleConfirmDelete()}
|
||||
onCancel={() => setConfirmDelete(null)}
|
||||
/>
|
||||
|
||||
{confirmDialog && (
|
||||
<ConfirmDialog
|
||||
isOpen={confirmDialog.isOpen}
|
||||
title={confirmDialog.title}
|
||||
message={confirmDialog.message}
|
||||
onConfirm={confirmDialog.onConfirm}
|
||||
onCancel={() => setConfirmDialog(null)}
|
||||
/>
|
||||
)}
|
||||
<SettingsDialog
|
||||
open={isSettingsOpen}
|
||||
onOpenChange={setIsSettingsOpen}
|
||||
onImportSuccess={handleImportSuccess}
|
||||
/>
|
||||
|
||||
<McpPanel
|
||||
open={isMcpOpen}
|
||||
onOpenChange={setIsMcpOpen}
|
||||
appId={activeApp}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
1
src/assets/icons/chatgpt.svg
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
1
src/assets/icons/claude.svg
Normal file
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1757750114641" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1475" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M202.112 678.656l200.64-112.64 3.392-9.792-3.392-5.44h-9.792l-33.6-2.048-114.624-3.072-99.456-4.224-96.384-5.12-24.192-5.12-22.72-29.952 2.304-14.976 20.48-13.696 29.12 2.56 64.576 4.416 96.832 6.72 70.208 4.096 104.064 10.88h16.576l2.304-6.72-5.696-4.16-4.352-4.096-100.224-67.968-108.48-71.744-56.768-41.344-30.72-20.928-15.488-19.584-6.72-42.88 27.84-30.72 37.504 2.56 9.536 2.56 37.952 29.184 81.088 62.784 105.856 77.952 15.488 12.928 6.208-4.352 0.768-3.136L395.264 360l-57.6-104.064-61.44-105.92-27.392-43.904-7.168-26.304c-2.56-10.88-4.48-19.904-4.48-30.976l31.808-43.136L286.592 0l42.304 5.696 17.856 15.488 26.304 60.16 42.624 94.72 66.112 128.896 19.392 38.208 10.24 35.392 3.904 10.88h6.72v-6.208l5.44-72.576 10.048-89.088 9.856-114.688 3.328-32.256 16-38.72 31.808-20.928 24.768 11.904 20.416 29.184-2.88 18.816-12.16 78.72-23.68 123.52-15.552 82.56h9.088l10.304-10.24 41.856-55.552 70.208-87.808 30.976-34.88 36.16-38.464 23.232-18.368h43.904l32.32 48.064-14.464 49.6-45.184 57.28-37.44 48.576-53.76 72.32-33.536 57.856 3.072 4.608 8-0.768 121.408-25.792 65.6-11.904 78.208-13.44 35.392 16.512 3.84 16.832-13.952 34.304-83.648 20.672-98.112 19.648-146.176 34.56-1.792 1.28 2.048 2.56 65.92 6.272 28.096 1.536h68.928l128.384 9.6 33.536 22.144 20.16 27.136-3.392 20.672-51.648 26.304-69.696-16.512-162.688-38.72-55.744-13.952h-7.744v4.672l46.464 45.44 85.184 76.928 106.688 99.2 5.376 24.512-13.632 19.328-14.464-2.048-93.76-70.464-36.16-31.808-81.856-68.928h-5.44v7.232l18.88 27.648 99.648 149.76 5.184 45.952-7.232 14.976-25.856 9.024-28.352-5.12L673.408 856l-60.16-92.16-48.576-82.624-5.952 3.392-28.672 308.544-13.44 15.744-30.976 11.904-25.792-19.648-13.696-31.744 13.696-62.72 16.512-81.92 13.44-65.024 12.16-80.832 7.232-26.88-0.512-1.792-5.952 0.768-60.928 83.648-92.736 125.248-73.344 78.528-17.536 6.976-30.464-15.808 2.816-28.16 17.024-24.96 101.504-129.152 61.184-80 39.552-46.272-0.256-6.72h-2.368L177.6 789.44l-48 6.144-20.736-19.328 2.56-31.744 9.856-10.368 81.088-55.744-0.256 0.256z" p-id="1476" fill="#bfbfbf"></path></svg>
|
||||
|
After Width: | Height: | Size: 2.3 KiB |
@@ -1,268 +0,0 @@
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
padding: 0;
|
||||
width: 90%;
|
||||
max-width: 640px;
|
||||
max-height: 90vh;
|
||||
overflow: hidden; /* 由 body 滚动,标题栏固定 */
|
||||
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.2);
|
||||
position: relative;
|
||||
z-index: 1001;
|
||||
display: flex; /* 纵向布局,便于底栏固定 */
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
/* 模拟窗口标题栏 */
|
||||
.modal-titlebar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
height: 3rem; /* 与主窗口标题栏一致 */
|
||||
padding: 0 12px; /* 接近主头部的水平留白 */
|
||||
background: #3498db; /* 与 .app-header 相同 */
|
||||
color: #fff;
|
||||
border-top-left-radius: 10px;
|
||||
border-top-right-radius: 10px;
|
||||
}
|
||||
|
||||
/* 左侧占位以保证标题居中(与右侧关闭按钮宽度相当) */
|
||||
.modal-spacer {
|
||||
width: 32px;
|
||||
flex: 0 0 32px;
|
||||
}
|
||||
|
||||
.modal-title {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
color: #fff;
|
||||
font-weight: 600;
|
||||
font-size: 1rem;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.modal-close-btn {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #fff;
|
||||
font-size: 20px;
|
||||
line-height: 1;
|
||||
padding: 2px 6px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.modal-close-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.18);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.modal-form {
|
||||
/* 表单外层包裹 body + footer */
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1 1 auto;
|
||||
min-height: 0; /* 允许子元素正确计算高度 */
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 1.25rem 1.5rem 1.5rem;
|
||||
overflow: auto; /* 仅内容区滚动 */
|
||||
flex: 1 1 auto;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
background: #fee;
|
||||
color: #c33;
|
||||
padding: 0.75rem;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 1rem;
|
||||
border: 1px solid #fcc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.presets {
|
||||
margin-bottom: 1.5rem;
|
||||
padding-bottom: 1.5rem;
|
||||
border-bottom: 1px solid #ecf0f1;
|
||||
}
|
||||
|
||||
.presets label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #555;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.preset-buttons {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.preset-btn {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid #3498db;
|
||||
background: white;
|
||||
color: #3498db;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.preset-btn:hover,
|
||||
.preset-btn.selected {
|
||||
background: #3498db;
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* 官方按钮橙色主题(Anthropic 风格) */
|
||||
.preset-btn.official {
|
||||
border: 1px solid #d97706;
|
||||
color: #d97706;
|
||||
}
|
||||
|
||||
.preset-btn.official:hover,
|
||||
.preset-btn.official.selected {
|
||||
background: #d97706;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #555;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* API Key 输入框容器 - 预留空间避免抖动 */
|
||||
.form-group.api-key-group {
|
||||
min-height: 88px; /* 固定高度:label + input + 间距 */
|
||||
transition: opacity 0.2s ease;
|
||||
}
|
||||
|
||||
.form-group.api-key-group.hidden {
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.form-group input,
|
||||
.form-group textarea {
|
||||
width: 100%;
|
||||
padding: 0.625rem;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
font-size: 0.95rem;
|
||||
transition: border-color 0.2s;
|
||||
background: white;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.form-group textarea {
|
||||
resize: vertical;
|
||||
min-height: 200px;
|
||||
}
|
||||
|
||||
.form-group input:focus,
|
||||
.form-group textarea:focus {
|
||||
outline: none;
|
||||
border-color: #3498db;
|
||||
}
|
||||
|
||||
.modal-footer {
|
||||
/* 固定在弹窗底部(非滚动区) */
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: flex-end;
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-top: 1px solid #ecf0f1;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.cancel-btn,
|
||||
.submit-btn {
|
||||
padding: 0.625rem 1.25rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.95rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.cancel-btn {
|
||||
background: #ecf0f1;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.cancel-btn:hover {
|
||||
background: #bdc3c7;
|
||||
}
|
||||
|
||||
.submit-btn {
|
||||
background: #27ae60;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.submit-btn:hover {
|
||||
background: #229954;
|
||||
}
|
||||
|
||||
.field-hint {
|
||||
display: block;
|
||||
margin-top: 0.25rem;
|
||||
color: #7f8c8d;
|
||||
font-size: 0.8rem;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
/* 添加标签和选择框的样式 */
|
||||
.label-with-checkbox {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.label-with-checkbox label:first-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.checkbox-label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.3rem;
|
||||
font-size: 0.85rem;
|
||||
color: #666;
|
||||
font-weight: normal;
|
||||
margin-bottom: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.checkbox-label input[type="checkbox"] {
|
||||
width: auto;
|
||||
margin: 2px;
|
||||
cursor: pointer;
|
||||
transform: translateY(2px);
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import React from "react";
|
||||
import { Provider } from "../types";
|
||||
import ProviderForm from "./ProviderForm";
|
||||
|
||||
interface AddProviderModalProps {
|
||||
onAdd: (provider: Omit<Provider, "id">) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const AddProviderModal: React.FC<AddProviderModalProps> = ({
|
||||
onAdd,
|
||||
onClose,
|
||||
}) => {
|
||||
return (
|
||||
<ProviderForm
|
||||
title="添加新供应商"
|
||||
submitText="添加"
|
||||
showPresets={true}
|
||||
onSubmit={onAdd}
|
||||
onClose={onClose}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default AddProviderModal;
|
||||
51
src/components/AppSwitcher.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { AppId } from "@/lib/api";
|
||||
import { ClaudeIcon, CodexIcon } from "./BrandIcons";
|
||||
|
||||
interface AppSwitcherProps {
|
||||
activeApp: AppId;
|
||||
onSwitch: (app: AppId) => void;
|
||||
}
|
||||
|
||||
export function AppSwitcher({ activeApp, onSwitch }: AppSwitcherProps) {
|
||||
const handleSwitch = (app: AppId) => {
|
||||
if (app === activeApp) return;
|
||||
onSwitch(app);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="inline-flex bg-gray-100 dark:bg-gray-800 rounded-lg p-1 gap-1 border border-transparent ">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleSwitch("claude")}
|
||||
className={`group inline-flex items-center gap-2 px-3 py-2 rounded-md text-sm font-medium transition-all duration-200 ${
|
||||
activeApp === "claude"
|
||||
? "bg-white text-gray-900 shadow-sm dark:bg-gray-900 dark:text-gray-100 dark:shadow-none"
|
||||
: "text-gray-500 hover:text-gray-900 hover:bg-white/50 dark:text-gray-400 dark:hover:text-gray-100 dark:hover:bg-gray-800/60"
|
||||
}`}
|
||||
>
|
||||
<ClaudeIcon
|
||||
size={16}
|
||||
className={
|
||||
activeApp === "claude"
|
||||
? "text-[#D97757] dark:text-[#D97757] transition-colors duration-200"
|
||||
: "text-gray-500 dark:text-gray-400 group-hover:text-[#D97757] dark:group-hover:text-[#D97757] transition-colors duration-200"
|
||||
}
|
||||
/>
|
||||
<span>Claude</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleSwitch("codex")}
|
||||
className={`inline-flex items-center gap-2 px-3 py-2 rounded-md text-sm font-medium transition-all duration-200 ${
|
||||
activeApp === "codex"
|
||||
? "bg-white text-gray-900 shadow-sm dark:bg-gray-900 dark:text-gray-100 dark:shadow-none"
|
||||
: "text-gray-500 hover:text-gray-900 hover:bg-white/50 dark:text-gray-400 dark:hover:text-gray-100 dark:hover:bg-gray-800/60"
|
||||
}`}
|
||||
>
|
||||
<CodexIcon size={16} />
|
||||
<span>Codex</span>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
34
src/components/BrandIcons.tsx
Normal file
@@ -1,107 +0,0 @@
|
||||
.confirm-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
backdrop-filter: blur(2px);
|
||||
}
|
||||
|
||||
.confirm-dialog {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
||||
min-width: 300px;
|
||||
max-width: 400px;
|
||||
animation: confirmSlideIn 0.2s ease-out;
|
||||
}
|
||||
|
||||
@keyframes confirmSlideIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: scale(0.9) translateY(-20px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: scale(1) translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.confirm-header {
|
||||
padding: 1.5rem 1.5rem 1rem;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
.confirm-header h3 {
|
||||
margin: 0;
|
||||
font-size: 1.1rem;
|
||||
color: #333;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.confirm-content {
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
|
||||
.confirm-content p {
|
||||
margin: 0;
|
||||
color: #666;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.confirm-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
padding: 1rem 1.5rem 1.5rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.confirm-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
transition:
|
||||
background-color 0.2s,
|
||||
transform 0.1s;
|
||||
min-width: 70px;
|
||||
}
|
||||
|
||||
.confirm-btn:hover {
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.confirm-btn:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.cancel-btn {
|
||||
background: #f8f9fa;
|
||||
color: #6c757d;
|
||||
border: 1px solid #dee2e6;
|
||||
}
|
||||
|
||||
.cancel-btn:hover {
|
||||
background: #e9ecef;
|
||||
}
|
||||
|
||||
.confirm-btn-primary {
|
||||
background: #dc3545;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.confirm-btn-primary:hover {
|
||||
background: #c82333;
|
||||
}
|
||||
|
||||
.confirm-btn:focus {
|
||||
outline: 2px solid #007bff;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
@@ -1,5 +1,14 @@
|
||||
import React from "react";
|
||||
import "./ConfirmDialog.css";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { AlertTriangle } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface ConfirmDialogProps {
|
||||
isOpen: boolean;
|
||||
@@ -11,42 +20,45 @@ interface ConfirmDialogProps {
|
||||
onCancel: () => void;
|
||||
}
|
||||
|
||||
export const ConfirmDialog: React.FC<ConfirmDialogProps> = ({
|
||||
export function ConfirmDialog({
|
||||
isOpen,
|
||||
title,
|
||||
message,
|
||||
confirmText = "确定",
|
||||
cancelText = "取消",
|
||||
confirmText,
|
||||
cancelText,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}) => {
|
||||
if (!isOpen) return null;
|
||||
}: ConfirmDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<div className="confirm-overlay">
|
||||
<div className="confirm-dialog">
|
||||
<div className="confirm-header">
|
||||
<h3>{title}</h3>
|
||||
</div>
|
||||
<div className="confirm-content">
|
||||
<p>{message}</p>
|
||||
</div>
|
||||
<div className="confirm-actions">
|
||||
<button
|
||||
className="confirm-btn cancel-btn"
|
||||
onClick={onCancel}
|
||||
autoFocus
|
||||
>
|
||||
{cancelText}
|
||||
</button>
|
||||
<button
|
||||
className="confirm-btn confirm-btn-primary"
|
||||
onClick={onConfirm}
|
||||
>
|
||||
{confirmText}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Dialog
|
||||
open={isOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
onCancel();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<DialogContent className="max-w-sm">
|
||||
<DialogHeader className="space-y-3">
|
||||
<DialogTitle className="flex items-center gap-2 text-lg font-semibold">
|
||||
<AlertTriangle className="h-5 w-5 text-destructive" />
|
||||
{title}
|
||||
</DialogTitle>
|
||||
<DialogDescription className="whitespace-pre-line text-sm leading-relaxed">
|
||||
{message}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter className="flex gap-2 sm:justify-end">
|
||||
<Button variant="outline" onClick={onCancel}>
|
||||
{cancelText || t("common.cancel")}
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={onConfirm}>
|
||||
{confirmText || t("common.confirm")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import React from "react";
|
||||
import { Provider } from "../types";
|
||||
import ProviderForm from "./ProviderForm";
|
||||
|
||||
interface EditProviderModalProps {
|
||||
provider: Provider;
|
||||
onSave: (provider: Provider) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const EditProviderModal: React.FC<EditProviderModalProps> = ({
|
||||
provider,
|
||||
onSave,
|
||||
onClose,
|
||||
}) => {
|
||||
const handleSubmit = (data: Omit<Provider, "id">) => {
|
||||
onSave({
|
||||
...provider,
|
||||
...data,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<ProviderForm
|
||||
title="编辑供应商"
|
||||
submitText="保存"
|
||||
initialData={provider}
|
||||
showPresets={false}
|
||||
onSubmit={handleSubmit}
|
||||
onClose={onClose}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default EditProviderModal;
|
||||
216
src/components/JsonEditor.tsx
Normal file
@@ -0,0 +1,216 @@
|
||||
import React, { useRef, useEffect, useMemo } from "react";
|
||||
import { EditorView, basicSetup } from "codemirror";
|
||||
import { json } from "@codemirror/lang-json";
|
||||
import { javascript } from "@codemirror/lang-javascript";
|
||||
import { oneDark } from "@codemirror/theme-one-dark";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { placeholder } from "@codemirror/view";
|
||||
import { linter, Diagnostic } from "@codemirror/lint";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Wand2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { formatJSON } from "@/utils/formatters";
|
||||
|
||||
interface JsonEditorProps {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
placeholder?: string;
|
||||
darkMode?: boolean;
|
||||
rows?: number;
|
||||
showValidation?: boolean;
|
||||
language?: "json" | "javascript";
|
||||
height?: string;
|
||||
}
|
||||
|
||||
const JsonEditor: React.FC<JsonEditorProps> = ({
|
||||
value,
|
||||
onChange,
|
||||
placeholder: placeholderText = "",
|
||||
darkMode = false,
|
||||
rows = 12,
|
||||
showValidation = true,
|
||||
language = "json",
|
||||
height,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const editorRef = useRef<HTMLDivElement>(null);
|
||||
const viewRef = useRef<EditorView | null>(null);
|
||||
|
||||
// JSON linter 函数
|
||||
const jsonLinter = useMemo(
|
||||
() =>
|
||||
linter((view) => {
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
if (!showValidation || language !== "json") return diagnostics;
|
||||
|
||||
const doc = view.state.doc.toString();
|
||||
if (!doc.trim()) return diagnostics;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(doc);
|
||||
// 检查是否是JSON对象
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
||||
// 格式正确
|
||||
} else {
|
||||
diagnostics.push({
|
||||
from: 0,
|
||||
to: doc.length,
|
||||
severity: "error",
|
||||
message: t("jsonEditor.mustBeObject"),
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// 简单处理JSON解析错误
|
||||
const message =
|
||||
e instanceof SyntaxError ? e.message : t("jsonEditor.invalidJson");
|
||||
diagnostics.push({
|
||||
from: 0,
|
||||
to: doc.length,
|
||||
severity: "error",
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
}),
|
||||
[showValidation, language, t],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editorRef.current) return;
|
||||
|
||||
// 创建编辑器扩展
|
||||
const minHeightPx = height ? undefined : Math.max(1, rows) * 18;
|
||||
|
||||
// 使用 baseTheme 定义基础样式,优先级低于 oneDark,但可以正确响应主题
|
||||
const baseTheme = EditorView.baseTheme({
|
||||
"&light .cm-editor, &dark .cm-editor": {
|
||||
border: "1px solid hsl(var(--border))",
|
||||
borderRadius: "0.5rem",
|
||||
},
|
||||
"&light .cm-editor.cm-focused, &dark .cm-editor.cm-focused": {
|
||||
outline: "none",
|
||||
borderColor: "hsl(var(--primary))",
|
||||
},
|
||||
});
|
||||
|
||||
// 使用 theme 定义尺寸和字体样式
|
||||
const sizingTheme = EditorView.theme({
|
||||
"&": height ? { height } : { minHeight: `${minHeightPx}px` },
|
||||
".cm-scroller": { overflow: "auto" },
|
||||
".cm-content": {
|
||||
fontFamily:
|
||||
"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
|
||||
fontSize: "14px",
|
||||
},
|
||||
});
|
||||
|
||||
const extensions = [
|
||||
basicSetup,
|
||||
language === "javascript" ? javascript() : json(),
|
||||
placeholder(placeholderText || ""),
|
||||
baseTheme,
|
||||
sizingTheme,
|
||||
jsonLinter,
|
||||
EditorView.updateListener.of((update) => {
|
||||
if (update.docChanged) {
|
||||
const newValue = update.state.doc.toString();
|
||||
onChange(newValue);
|
||||
}
|
||||
}),
|
||||
];
|
||||
|
||||
// 如果启用深色模式,添加深色主题
|
||||
if (darkMode) {
|
||||
extensions.push(oneDark);
|
||||
// 在 oneDark 之后强制覆盖边框样式
|
||||
extensions.push(
|
||||
EditorView.theme({
|
||||
".cm-editor": {
|
||||
border: "1px solid hsl(var(--border))",
|
||||
borderRadius: "0.5rem",
|
||||
},
|
||||
".cm-editor.cm-focused": {
|
||||
outline: "none",
|
||||
borderColor: "hsl(var(--primary))",
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// 创建初始状态
|
||||
const state = EditorState.create({
|
||||
doc: value,
|
||||
extensions,
|
||||
});
|
||||
|
||||
// 创建编辑器视图
|
||||
const view = new EditorView({
|
||||
state,
|
||||
parent: editorRef.current,
|
||||
});
|
||||
|
||||
viewRef.current = view;
|
||||
|
||||
// 清理函数
|
||||
return () => {
|
||||
view.destroy();
|
||||
viewRef.current = null;
|
||||
};
|
||||
}, [darkMode, rows, height, language, jsonLinter]); // 依赖项中不包含 onChange 和 placeholder,避免不必要的重建
|
||||
|
||||
// 当 value 从外部改变时更新编辑器内容
|
||||
useEffect(() => {
|
||||
if (viewRef.current && viewRef.current.state.doc.toString() !== value) {
|
||||
const transaction = viewRef.current.state.update({
|
||||
changes: {
|
||||
from: 0,
|
||||
to: viewRef.current.state.doc.length,
|
||||
insert: value,
|
||||
},
|
||||
});
|
||||
viewRef.current.dispatch(transaction);
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
// 格式化处理函数
|
||||
const handleFormat = () => {
|
||||
if (!viewRef.current) return;
|
||||
|
||||
const currentValue = viewRef.current.state.doc.toString();
|
||||
if (!currentValue.trim()) return;
|
||||
|
||||
try {
|
||||
const formatted = formatJSON(currentValue);
|
||||
onChange(formatted);
|
||||
toast.success(t("common.formatSuccess", { defaultValue: "格式化成功" }));
|
||||
} catch (error) {
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : String(error);
|
||||
toast.error(
|
||||
t("common.formatError", {
|
||||
defaultValue: "格式化失败:{{error}}",
|
||||
error: errorMessage,
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ width: "100%" }}>
|
||||
<div ref={editorRef} style={{ width: "100%" }} />
|
||||
{language === "json" && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleFormat}
|
||||
className="mt-2 inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 transition-colors"
|
||||
>
|
||||
<Wand2 className="w-3.5 h-3.5" />
|
||||
{t("common.format", { defaultValue: "格式化" })}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default JsonEditor;
|
||||
@@ -1,355 +0,0 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { Provider } from "../types";
|
||||
import {
|
||||
updateCoAuthoredSetting,
|
||||
checkCoAuthoredSetting,
|
||||
extractWebsiteUrl,
|
||||
getApiKeyFromConfig,
|
||||
hasApiKeyField,
|
||||
setApiKeyInConfig,
|
||||
} from "../utils/providerConfigUtils";
|
||||
import { providerPresets } from "../config/providerPresets";
|
||||
import "./AddProviderModal.css";
|
||||
|
||||
interface ProviderFormProps {
|
||||
title: string;
|
||||
submitText: string;
|
||||
initialData?: Provider;
|
||||
showPresets?: boolean;
|
||||
onSubmit: (data: Omit<Provider, "id">) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
title,
|
||||
submitText,
|
||||
initialData,
|
||||
showPresets = false,
|
||||
onSubmit,
|
||||
onClose,
|
||||
}) => {
|
||||
const [formData, setFormData] = useState({
|
||||
name: initialData?.name || "",
|
||||
websiteUrl: initialData?.websiteUrl || "",
|
||||
settingsConfig: initialData
|
||||
? JSON.stringify(initialData.settingsConfig, null, 2)
|
||||
: "",
|
||||
});
|
||||
const [error, setError] = useState("");
|
||||
const [disableCoAuthored, setDisableCoAuthored] = useState(false);
|
||||
const [selectedPreset, setSelectedPreset] = useState<number | null>(null);
|
||||
const [apiKey, setApiKey] = useState("");
|
||||
|
||||
// 初始化时检查禁用签名状态
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
const configString = JSON.stringify(initialData.settingsConfig, null, 2);
|
||||
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
||||
setDisableCoAuthored(hasCoAuthoredDisabled);
|
||||
}
|
||||
}, [initialData]);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError("");
|
||||
|
||||
if (!formData.name) {
|
||||
setError("请填写供应商名称");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formData.settingsConfig.trim()) {
|
||||
setError("请填写配置内容");
|
||||
return;
|
||||
}
|
||||
|
||||
let settingsConfig: Record<string, any>;
|
||||
|
||||
try {
|
||||
settingsConfig = JSON.parse(formData.settingsConfig);
|
||||
} catch (err) {
|
||||
setError("配置JSON格式错误,请检查语法");
|
||||
return;
|
||||
}
|
||||
|
||||
onSubmit({
|
||||
name: formData.name,
|
||||
websiteUrl: formData.websiteUrl,
|
||||
settingsConfig,
|
||||
});
|
||||
};
|
||||
|
||||
const handleChange = (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
||||
) => {
|
||||
const { name, value } = e.target;
|
||||
|
||||
if (name === "settingsConfig") {
|
||||
// 当用户修改配置时,尝试自动提取官网地址
|
||||
const extractedWebsiteUrl = extractWebsiteUrl(value);
|
||||
|
||||
// 同时检查并同步选择框状态
|
||||
const hasCoAuthoredDisabled = checkCoAuthoredSetting(value);
|
||||
setDisableCoAuthored(hasCoAuthoredDisabled);
|
||||
|
||||
// 同步 API Key 输入框显示与值
|
||||
const parsedKey = getApiKeyFromConfig(value);
|
||||
setApiKey(parsedKey);
|
||||
|
||||
setFormData({
|
||||
...formData,
|
||||
[name]: value,
|
||||
// 只有在官网地址为空时才自动填入
|
||||
websiteUrl: formData.websiteUrl || extractedWebsiteUrl,
|
||||
});
|
||||
} else {
|
||||
setFormData({
|
||||
...formData,
|
||||
[name]: value,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 处理选择框变化
|
||||
const handleCoAuthoredToggle = (checked: boolean) => {
|
||||
setDisableCoAuthored(checked);
|
||||
|
||||
// 更新JSON配置
|
||||
const updatedConfig = updateCoAuthoredSetting(
|
||||
formData.settingsConfig,
|
||||
checked,
|
||||
);
|
||||
setFormData({
|
||||
...formData,
|
||||
settingsConfig: updatedConfig,
|
||||
});
|
||||
};
|
||||
|
||||
const applyPreset = (preset: (typeof providerPresets)[0], index: number) => {
|
||||
const configString = JSON.stringify(preset.settingsConfig, null, 2);
|
||||
|
||||
setFormData({
|
||||
name: preset.name,
|
||||
websiteUrl: preset.websiteUrl,
|
||||
settingsConfig: configString,
|
||||
});
|
||||
|
||||
// 设置选中的预设
|
||||
setSelectedPreset(index);
|
||||
|
||||
// 清空 API Key 输入框,让用户重新输入
|
||||
setApiKey("");
|
||||
|
||||
// 同步选择框状态
|
||||
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
||||
setDisableCoAuthored(hasCoAuthoredDisabled);
|
||||
};
|
||||
|
||||
// 处理 API Key 输入并自动更新配置
|
||||
const handleApiKeyChange = (key: string) => {
|
||||
setApiKey(key);
|
||||
|
||||
const configString = setApiKeyInConfig(
|
||||
formData.settingsConfig,
|
||||
key.trim(),
|
||||
{ createIfMissing: selectedPreset !== null },
|
||||
);
|
||||
|
||||
// 更新表单配置
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
settingsConfig: configString,
|
||||
}));
|
||||
|
||||
// 同步选择框状态
|
||||
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
||||
setDisableCoAuthored(hasCoAuthoredDisabled);
|
||||
};
|
||||
|
||||
// 根据当前配置决定是否展示 API Key 输入框
|
||||
const showApiKey =
|
||||
selectedPreset !== null || hasApiKeyField(formData.settingsConfig);
|
||||
|
||||
// 判断当前选中的预设是否是官方
|
||||
const isOfficialPreset =
|
||||
selectedPreset !== null &&
|
||||
providerPresets[selectedPreset]?.isOfficial === true;
|
||||
|
||||
// 初始时从配置中同步 API Key(编辑模式)
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
const parsedKey = getApiKeyFromConfig(
|
||||
JSON.stringify(initialData.settingsConfig),
|
||||
);
|
||||
if (parsedKey) setApiKey(parsedKey);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
// 支持按下 ESC 关闭弹窗
|
||||
useEffect(() => {
|
||||
const onKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [onClose]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="modal-overlay"
|
||||
onMouseDown={(e) => {
|
||||
if (e.target === e.currentTarget) onClose();
|
||||
}}
|
||||
>
|
||||
<div className="modal-content">
|
||||
<div className="modal-titlebar">
|
||||
<div className="modal-spacer" />
|
||||
<div className="modal-title" title={title}>
|
||||
{title}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="modal-close-btn"
|
||||
aria-label="关闭"
|
||||
onClick={onClose}
|
||||
title="关闭"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="modal-form">
|
||||
<div className="modal-body">
|
||||
{error && <div className="error-message">{error}</div>}
|
||||
|
||||
{showPresets && (
|
||||
<div className="presets">
|
||||
<label>一键导入(只需要填写 key)</label>
|
||||
<div className="preset-buttons">
|
||||
{providerPresets.map((preset, index) => {
|
||||
return (
|
||||
<button
|
||||
key={index}
|
||||
type="button"
|
||||
className={`preset-btn ${
|
||||
selectedPreset === index ? "selected" : ""
|
||||
} ${preset.isOfficial ? "official" : ""}`}
|
||||
onClick={() => applyPreset(preset, index)}
|
||||
>
|
||||
{preset.name}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="form-group">
|
||||
<label htmlFor="name">供应商名称 *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="name"
|
||||
name="name"
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
placeholder="例如:Anthropic 官方"
|
||||
required
|
||||
autoComplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={`form-group api-key-group ${!showApiKey ? "hidden" : ""}`}
|
||||
>
|
||||
<label htmlFor="apiKey">API Key *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="apiKey"
|
||||
value={apiKey}
|
||||
onChange={(e) => handleApiKeyChange(e.target.value)}
|
||||
placeholder={
|
||||
isOfficialPreset
|
||||
? "官方登录无需填写 API Key,直接保存即可"
|
||||
: "只需要填这里,下方配置会自动填充"
|
||||
}
|
||||
disabled={isOfficialPreset}
|
||||
autoComplete="off"
|
||||
style={
|
||||
isOfficialPreset
|
||||
? {
|
||||
backgroundColor: "#f5f5f5",
|
||||
cursor: "not-allowed",
|
||||
color: "#999",
|
||||
}
|
||||
: {}
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label htmlFor="websiteUrl">官网地址</label>
|
||||
<input
|
||||
type="url"
|
||||
id="websiteUrl"
|
||||
name="websiteUrl"
|
||||
value={formData.websiteUrl}
|
||||
onChange={handleChange}
|
||||
placeholder="https://example.com(可选)"
|
||||
autoComplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<div className="label-with-checkbox">
|
||||
<label htmlFor="settingsConfig">
|
||||
Claude Code 配置 (JSON) *
|
||||
</label>
|
||||
<label className="checkbox-label">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={disableCoAuthored}
|
||||
onChange={(e) => handleCoAuthoredToggle(e.target.checked)}
|
||||
/>
|
||||
禁止 Claude Code 签名
|
||||
</label>
|
||||
</div>
|
||||
<textarea
|
||||
id="settingsConfig"
|
||||
name="settingsConfig"
|
||||
value={formData.settingsConfig}
|
||||
onChange={handleChange}
|
||||
placeholder={`{
|
||||
"env": {
|
||||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
|
||||
"ANTHROPIC_AUTH_TOKEN": "sk-your-api-key-here"
|
||||
}
|
||||
}`}
|
||||
rows={12}
|
||||
style={{ fontFamily: "monospace", fontSize: "14px" }}
|
||||
required
|
||||
/>
|
||||
<small className="field-hint">
|
||||
完整的 Claude Code settings.json 配置内容
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="modal-footer">
|
||||
<button type="button" className="cancel-btn" onClick={onClose}>
|
||||
取消
|
||||
</button>
|
||||
<button type="submit" className="submit-btn">
|
||||
{submitText}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProviderForm;
|
||||
@@ -1,206 +0,0 @@
|
||||
.provider-list {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 3rem 1rem;
|
||||
color: #7f8c8d;
|
||||
}
|
||||
|
||||
.empty-state p:first-child {
|
||||
font-size: 1.1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.provider-items {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.provider-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
border: 2px solid #ecf0f1;
|
||||
border-radius: 6px;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.provider-item:hover {
|
||||
border-color: #3498db;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.provider-item.current {
|
||||
border-color: #27ae60;
|
||||
background: #f0fdf4;
|
||||
}
|
||||
|
||||
.provider-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.provider-name {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.provider-name input[type="radio"] {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.provider-name input[type="radio"]:disabled {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.current-badge {
|
||||
background: #27ae60;
|
||||
color: white;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 3px;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.provider-url {
|
||||
color: #7f8c8d;
|
||||
font-size: 0.9rem;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.url-link {
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.url-link:hover {
|
||||
color: #2980b9;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.api-url {
|
||||
color: #7f8c8d;
|
||||
}
|
||||
|
||||
.provider-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-right: 2rem;
|
||||
}
|
||||
|
||||
.status-icon {
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.status-text {
|
||||
color: #555;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.response-time {
|
||||
color: #3498db;
|
||||
font-size: 0.85rem;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.provider-actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.check-btn {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid #f39c12;
|
||||
background: white;
|
||||
color: #f39c12;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.check-btn:hover:not(:disabled) {
|
||||
background: #f39c12;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.check-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.enable-btn {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid #27ae60;
|
||||
background: white;
|
||||
color: #27ae60;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.enable-btn:hover:not(:disabled) {
|
||||
background: #27ae60;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.enable-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.edit-btn {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid #3498db;
|
||||
background: white;
|
||||
color: #3498db;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.edit-btn:hover:not(:disabled) {
|
||||
background: #3498db;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.edit-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.delete-btn {
|
||||
padding: 0.375rem 0.75rem;
|
||||
border: 1px solid #e74c3c;
|
||||
background: white;
|
||||
color: #e74c3c;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.delete-btn:hover:not(:disabled) {
|
||||
background: #e74c3c;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.delete-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
import React from "react";
|
||||
import { Provider } from "../types";
|
||||
import "./ProviderList.css";
|
||||
|
||||
interface ProviderListProps {
|
||||
providers: Record<string, Provider>;
|
||||
currentProviderId: string;
|
||||
onSwitch: (id: string) => void;
|
||||
onDelete: (id: string) => void;
|
||||
onEdit: (id: string) => void;
|
||||
}
|
||||
|
||||
const ProviderList: React.FC<ProviderListProps> = ({
|
||||
providers,
|
||||
currentProviderId,
|
||||
onSwitch,
|
||||
onDelete,
|
||||
onEdit,
|
||||
}) => {
|
||||
// 提取API地址
|
||||
const getApiUrl = (provider: Provider): string => {
|
||||
try {
|
||||
const config = provider.settingsConfig;
|
||||
if (config?.env?.ANTHROPIC_BASE_URL) {
|
||||
return config.env.ANTHROPIC_BASE_URL;
|
||||
}
|
||||
return "未设置";
|
||||
} catch {
|
||||
return "配置错误";
|
||||
}
|
||||
};
|
||||
|
||||
const handleUrlClick = async (url: string) => {
|
||||
try {
|
||||
await window.api.openExternal(url);
|
||||
} catch (error) {
|
||||
console.error("打开链接失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="provider-list">
|
||||
{Object.values(providers).length === 0 ? (
|
||||
<div className="empty-state">
|
||||
<p>还没有添加任何供应商</p>
|
||||
<p>点击右上角的"添加供应商"按钮开始</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="provider-items">
|
||||
{Object.values(providers).map((provider) => {
|
||||
const isCurrent = provider.id === currentProviderId;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={provider.id}
|
||||
className={`provider-item ${isCurrent ? "current" : ""}`}
|
||||
>
|
||||
<div className="provider-info">
|
||||
<div className="provider-name">
|
||||
<span>{provider.name}</span>
|
||||
{isCurrent && (
|
||||
<span className="current-badge">当前使用</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="provider-url">
|
||||
{provider.websiteUrl ? (
|
||||
<a
|
||||
href="#"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
handleUrlClick(provider.websiteUrl!);
|
||||
}}
|
||||
className="url-link"
|
||||
title={`访问 ${provider.websiteUrl}`}
|
||||
>
|
||||
{provider.websiteUrl}
|
||||
</a>
|
||||
) : (
|
||||
<span className="api-url" title={getApiUrl(provider)}>
|
||||
{getApiUrl(provider)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="provider-actions">
|
||||
<button
|
||||
className="enable-btn"
|
||||
onClick={() => onSwitch(provider.id)}
|
||||
disabled={isCurrent}
|
||||
>
|
||||
启用
|
||||
</button>
|
||||
<button
|
||||
className="edit-btn"
|
||||
onClick={() => onEdit(provider.id)}
|
||||
disabled={isCurrent}
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
className="delete-btn"
|
||||
onClick={() => onDelete(provider.id)}
|
||||
disabled={isCurrent}
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProviderList;
|
||||
63
src/components/UpdateBadge.tsx
Normal file
@@ -0,0 +1,63 @@
|
||||
import { X, Download } from "lucide-react";
|
||||
import { useUpdate } from "@/contexts/UpdateContext";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface UpdateBadgeProps {
|
||||
className?: string;
|
||||
onClick?: () => void;
|
||||
}
|
||||
|
||||
export function UpdateBadge({ className = "", onClick }: UpdateBadgeProps) {
|
||||
const { hasUpdate, updateInfo, isDismissed, dismissUpdate } = useUpdate();
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 如果没有更新或已关闭,不显示
|
||||
if (!hasUpdate || isDismissed || !updateInfo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`
|
||||
flex items-center gap-1.5 px-2.5 py-1
|
||||
bg-white dark:bg-gray-800
|
||||
border border-border-default
|
||||
rounded-lg text-xs
|
||||
shadow-sm
|
||||
transition-all duration-200
|
||||
${onClick ? "cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-750" : ""}
|
||||
${className}
|
||||
`}
|
||||
role={onClick ? "button" : undefined}
|
||||
tabIndex={onClick ? 0 : -1}
|
||||
onClick={onClick}
|
||||
onKeyDown={(e) => {
|
||||
if (!onClick) return;
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
onClick();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Download className="w-3 h-3 text-blue-500 dark:text-blue-400" />
|
||||
<span className="text-gray-700 dark:text-gray-300 font-medium">
|
||||
v{updateInfo.availableVersion}
|
||||
</span>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
dismissUpdate();
|
||||
}}
|
||||
className="
|
||||
ml-1 -mr-0.5 p-0.5 rounded
|
||||
hover:bg-gray-100 dark:hover:bg-gray-700
|
||||
transition-colors
|
||||
focus:outline-none focus:ring-2 focus:ring-blue-500/20
|
||||
"
|
||||
aria-label={t("common.close")}
|
||||
>
|
||||
<X className="w-3 h-3 text-gray-400 dark:text-gray-500" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
354
src/components/UsageFooter.tsx
Normal file
@@ -0,0 +1,354 @@
|
||||
import React from "react";
|
||||
import { RefreshCw, AlertCircle, Clock } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { type AppId } from "@/lib/api";
|
||||
import { useUsageQuery } from "@/lib/query/queries";
|
||||
import { UsageData, Provider } from "@/types";
|
||||
|
||||
interface UsageFooterProps {
|
||||
provider: Provider;
|
||||
providerId: string;
|
||||
appId: AppId;
|
||||
usageEnabled: boolean; // 是否启用了用量查询
|
||||
isCurrent: boolean; // 是否为当前激活的供应商
|
||||
inline?: boolean; // 是否内联显示(在按钮左侧)
|
||||
}
|
||||
|
||||
const UsageFooter: React.FC<UsageFooterProps> = ({
|
||||
provider,
|
||||
providerId,
|
||||
appId,
|
||||
usageEnabled,
|
||||
isCurrent,
|
||||
inline = false,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 统一的用量查询(自动查询仅对当前激活的供应商启用)
|
||||
const autoQueryInterval = isCurrent
|
||||
? provider.meta?.usage_script?.autoQueryInterval || 0
|
||||
: 0;
|
||||
|
||||
const {
|
||||
data: usage,
|
||||
isFetching: loading,
|
||||
lastQueriedAt,
|
||||
refetch,
|
||||
} = useUsageQuery(providerId, appId, {
|
||||
enabled: usageEnabled,
|
||||
autoQueryInterval,
|
||||
});
|
||||
|
||||
// 🆕 定期更新当前时间,用于刷新相对时间显示
|
||||
const [now, setNow] = React.useState(Date.now());
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!lastQueriedAt) return;
|
||||
|
||||
// 每30秒更新一次当前时间,触发相对时间显示的刷新
|
||||
const interval = setInterval(() => {
|
||||
setNow(Date.now());
|
||||
}, 30000); // 30秒
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [lastQueriedAt]);
|
||||
|
||||
// 只在启用用量查询且有数据时显示
|
||||
if (!usageEnabled || !usage) return null;
|
||||
|
||||
// 错误状态
|
||||
if (!usage.success) {
|
||||
if (inline) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-xs">
|
||||
<div className="flex items-center gap-1.5 text-red-500 dark:text-red-400">
|
||||
<AlertCircle size={12} />
|
||||
<span>{t("usage.queryFailed")}</span>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
||||
title={t("usage.refreshUsage")}
|
||||
>
|
||||
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-3 pt-3 border-t border-border-default ">
|
||||
<div className="flex items-center justify-between gap-2 text-xs">
|
||||
<div className="flex items-center gap-2 text-red-500 dark:text-red-400">
|
||||
<AlertCircle size={14} />
|
||||
<span>{usage.error || t("usage.queryFailed")}</span>
|
||||
</div>
|
||||
|
||||
{/* 刷新按钮 */}
|
||||
<button
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
||||
title={t("usage.refreshUsage")}
|
||||
>
|
||||
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const usageDataList = usage.data || [];
|
||||
|
||||
// 无数据时不显示
|
||||
if (usageDataList.length === 0) return null;
|
||||
|
||||
// 内联模式:仅显示第一个套餐的核心数据(分上下两行)
|
||||
if (inline) {
|
||||
const firstUsage = usageDataList[0];
|
||||
const isExpired = firstUsage.isValid === false;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-1 text-xs flex-shrink-0">
|
||||
{/* 第一行:刷新时间 + 刷新按钮 */}
|
||||
<div className="flex items-center gap-2 justify-end">
|
||||
{/* 上次查询时间 */}
|
||||
{lastQueriedAt && (
|
||||
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
|
||||
<Clock size={10} />
|
||||
{formatRelativeTime(lastQueriedAt, now, t)}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* 刷新按钮 */}
|
||||
<button
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
||||
title={t("usage.refreshUsage")}
|
||||
>
|
||||
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 第二行:已用 + 剩余 + 单位 */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* 已用 */}
|
||||
{firstUsage.used !== undefined && (
|
||||
<div className="flex items-center gap-0.5">
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{t("usage.used")}
|
||||
</span>
|
||||
<span className="tabular-nums text-gray-600 dark:text-gray-400 font-medium">
|
||||
{firstUsage.used.toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 剩余 */}
|
||||
{firstUsage.remaining !== undefined && (
|
||||
<div className="flex items-center gap-0.5">
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{t("usage.remaining")}
|
||||
</span>
|
||||
<span
|
||||
className={`font-semibold tabular-nums ${
|
||||
isExpired
|
||||
? "text-red-500 dark:text-red-400"
|
||||
: firstUsage.remaining <
|
||||
(firstUsage.total || firstUsage.remaining) * 0.1
|
||||
? "text-orange-500 dark:text-orange-400"
|
||||
: "text-green-600 dark:text-green-400"
|
||||
}`}
|
||||
>
|
||||
{firstUsage.remaining.toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 单位 */}
|
||||
{firstUsage.unit && (
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{firstUsage.unit}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-3 pt-3 border-t border-border-default ">
|
||||
{/* 标题行:包含刷新按钮和自动查询时间 */}
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<span className="text-xs text-gray-500 dark:text-gray-400 font-medium">
|
||||
{t("usage.planUsage")}
|
||||
</span>
|
||||
<div className="flex items-center gap-2">
|
||||
{/* 自动查询时间提示 */}
|
||||
{lastQueriedAt && (
|
||||
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
|
||||
<Clock size={10} />
|
||||
{formatRelativeTime(lastQueriedAt, now, t)}
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
|
||||
title={t("usage.refreshUsage")}
|
||||
>
|
||||
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 套餐列表 */}
|
||||
<div className="flex flex-col gap-3">
|
||||
{usageDataList.map((usageData, index) => (
|
||||
<UsagePlanItem key={index} data={usageData} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 单个套餐数据展示组件
|
||||
const UsagePlanItem: React.FC<{ data: UsageData }> = ({ data }) => {
|
||||
const { t } = useTranslation();
|
||||
const {
|
||||
planName,
|
||||
extra,
|
||||
isValid,
|
||||
invalidMessage,
|
||||
total,
|
||||
used,
|
||||
remaining,
|
||||
unit,
|
||||
} = data;
|
||||
|
||||
// 判断套餐是否失效(isValid 为 false 或未定义时视为有效)
|
||||
const isExpired = isValid === false;
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-3">
|
||||
{/* 标题部分:25% */}
|
||||
<div
|
||||
className="text-xs text-gray-500 dark:text-gray-400 min-w-0"
|
||||
style={{ width: "25%" }}
|
||||
>
|
||||
{planName ? (
|
||||
<span
|
||||
className={`font-medium truncate block ${isExpired ? "text-red-500 dark:text-red-400" : ""}`}
|
||||
title={planName}
|
||||
>
|
||||
💰 {planName}
|
||||
</span>
|
||||
) : (
|
||||
<span className="opacity-50">—</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 扩展字段:30% */}
|
||||
<div
|
||||
className="text-xs text-gray-500 dark:text-gray-400 min-w-0 flex items-center gap-2"
|
||||
style={{ width: "30%" }}
|
||||
>
|
||||
{extra && (
|
||||
<span
|
||||
className={`truncate ${isExpired ? "text-red-500 dark:text-red-400" : ""}`}
|
||||
title={extra}
|
||||
>
|
||||
{extra}
|
||||
</span>
|
||||
)}
|
||||
{isExpired && (
|
||||
<span className="text-red-500 dark:text-red-400 font-medium text-[10px] px-1.5 py-0.5 bg-red-50 dark:bg-red-900/20 rounded flex-shrink-0">
|
||||
{invalidMessage || t("usage.invalid")}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 用量信息:45% */}
|
||||
<div
|
||||
className="flex items-center justify-end gap-2 text-xs flex-shrink-0"
|
||||
style={{ width: "45%" }}
|
||||
>
|
||||
{/* 总额度 */}
|
||||
{total !== undefined && (
|
||||
<>
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{t("usage.total")}
|
||||
</span>
|
||||
<span className="tabular-nums text-gray-600 dark:text-gray-400">
|
||||
{total === -1 ? "∞" : total.toFixed(2)}
|
||||
</span>
|
||||
<span className="text-gray-400 dark:text-gray-600">|</span>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 已用额度 */}
|
||||
{used !== undefined && (
|
||||
<>
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{t("usage.used")}
|
||||
</span>
|
||||
<span className="tabular-nums text-gray-600 dark:text-gray-400">
|
||||
{used.toFixed(2)}
|
||||
</span>
|
||||
<span className="text-gray-400 dark:text-gray-600">|</span>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 剩余额度 - 突出显示 */}
|
||||
{remaining !== undefined && (
|
||||
<>
|
||||
<span className="text-gray-500 dark:text-gray-400">
|
||||
{t("usage.remaining")}
|
||||
</span>
|
||||
<span
|
||||
className={`font-semibold tabular-nums ${
|
||||
isExpired
|
||||
? "text-red-500 dark:text-red-400"
|
||||
: remaining < (total || remaining) * 0.1
|
||||
? "text-orange-500 dark:text-orange-400"
|
||||
: "text-green-600 dark:text-green-400"
|
||||
}`}
|
||||
>
|
||||
{remaining.toFixed(2)}
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
|
||||
{unit && (
|
||||
<span className="text-gray-500 dark:text-gray-400">{unit}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 格式化相对时间
|
||||
function formatRelativeTime(
|
||||
timestamp: number,
|
||||
now: number,
|
||||
t: (key: string, options?: { count?: number }) => string,
|
||||
): string {
|
||||
const diff = Math.floor((now - timestamp) / 1000); // 秒
|
||||
|
||||
if (diff < 60) {
|
||||
return t("usage.justNow");
|
||||
} else if (diff < 3600) {
|
||||
const minutes = Math.floor(diff / 60);
|
||||
return t("usage.minutesAgo", { count: minutes });
|
||||
} else if (diff < 86400) {
|
||||
const hours = Math.floor(diff / 3600);
|
||||
return t("usage.hoursAgo", { count: hours });
|
||||
} else {
|
||||
const days = Math.floor(diff / 86400);
|
||||
return t("usage.daysAgo", { count: days });
|
||||
}
|
||||
}
|
||||
|
||||
export default UsageFooter;
|
||||
503
src/components/UsageScriptModal.tsx
Normal file
@@ -0,0 +1,503 @@
|
||||
import React, { useState } from "react";
|
||||
import { Play, Wand2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Provider, UsageScript } from "@/types";
|
||||
import { usageApi, type AppId } from "@/lib/api";
|
||||
import JsonEditor from "./JsonEditor";
|
||||
import * as prettier from "prettier/standalone";
|
||||
import * as parserBabel from "prettier/parser-babel";
|
||||
import * as pluginEstree from "prettier/plugins/estree";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface UsageScriptModalProps {
|
||||
provider: Provider;
|
||||
appId: AppId;
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
onSave: (script: UsageScript) => void;
|
||||
}
|
||||
|
||||
// 预设模板键名(用于国际化)
|
||||
const TEMPLATE_KEYS = {
|
||||
CUSTOM: "custom",
|
||||
GENERAL: "general",
|
||||
NEW_API: "newapi",
|
||||
} as const;
|
||||
|
||||
// 生成预设模板的函数(支持国际化)
|
||||
const generatePresetTemplates = (
|
||||
t: (key: string) => string,
|
||||
): Record<string, string> => ({
|
||||
[TEMPLATE_KEYS.CUSTOM]: `({
|
||||
request: {
|
||||
url: "",
|
||||
method: "GET",
|
||||
headers: {}
|
||||
},
|
||||
extractor: function(response) {
|
||||
return {
|
||||
remaining: 0,
|
||||
unit: "USD"
|
||||
};
|
||||
}
|
||||
})`,
|
||||
|
||||
[TEMPLATE_KEYS.GENERAL]: `({
|
||||
request: {
|
||||
url: "{{baseUrl}}/user/balance",
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": "Bearer {{apiKey}}",
|
||||
"User-Agent": "cc-switch/1.0"
|
||||
}
|
||||
},
|
||||
extractor: function(response) {
|
||||
return {
|
||||
isValid: response.is_active || true,
|
||||
remaining: response.balance,
|
||||
unit: "USD"
|
||||
};
|
||||
}
|
||||
})`,
|
||||
|
||||
[TEMPLATE_KEYS.NEW_API]: `({
|
||||
request: {
|
||||
url: "{{baseUrl}}/api/user/self",
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer {{accessToken}}",
|
||||
"New-Api-User": "{{userId}}"
|
||||
},
|
||||
},
|
||||
extractor: function (response) {
|
||||
if (response.success && response.data) {
|
||||
return {
|
||||
planName: response.data.group || "${t("usageScript.defaultPlan")}",
|
||||
remaining: response.data.quota / 500000,
|
||||
used: response.data.used_quota / 500000,
|
||||
total: (response.data.quota + response.data.used_quota) / 500000,
|
||||
unit: "USD",
|
||||
};
|
||||
}
|
||||
return {
|
||||
isValid: false,
|
||||
invalidMessage: response.message || "${t("usageScript.queryFailedMessage")}"
|
||||
};
|
||||
},
|
||||
})`,
|
||||
});
|
||||
|
||||
// 模板名称国际化键映射
|
||||
const TEMPLATE_NAME_KEYS: Record<string, string> = {
|
||||
[TEMPLATE_KEYS.CUSTOM]: "usageScript.templateCustom",
|
||||
[TEMPLATE_KEYS.GENERAL]: "usageScript.templateGeneral",
|
||||
[TEMPLATE_KEYS.NEW_API]: "usageScript.templateNewAPI",
|
||||
};
|
||||
|
||||
const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
|
||||
provider,
|
||||
appId,
|
||||
isOpen,
|
||||
onClose,
|
||||
onSave,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 生成带国际化的预设模板
|
||||
const PRESET_TEMPLATES = generatePresetTemplates(t);
|
||||
|
||||
const [script, setScript] = useState<UsageScript>(() => {
|
||||
return (
|
||||
provider.meta?.usage_script || {
|
||||
enabled: false,
|
||||
language: "javascript",
|
||||
code: PRESET_TEMPLATES[TEMPLATE_KEYS.GENERAL],
|
||||
timeout: 10,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
const [testing, setTesting] = useState(false);
|
||||
|
||||
// 跟踪当前选择的模板类型(用于控制高级配置的显示)
|
||||
// 初始化:如果已有 accessToken 或 userId,说明是 NewAPI 模板
|
||||
const [selectedTemplate, setSelectedTemplate] = useState<string | null>(
|
||||
() => {
|
||||
const existingScript = provider.meta?.usage_script;
|
||||
if (existingScript?.accessToken || existingScript?.userId) {
|
||||
return TEMPLATE_KEYS.NEW_API;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
|
||||
const handleSave = () => {
|
||||
// 验证脚本格式
|
||||
if (script.enabled && !script.code.trim()) {
|
||||
toast.error(t("usageScript.scriptEmpty"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 基本的 JS 语法检查(检查是否包含 return 语句)
|
||||
if (script.enabled && !script.code.includes("return")) {
|
||||
toast.error(t("usageScript.mustHaveReturn"), { duration: 5000 });
|
||||
return;
|
||||
}
|
||||
|
||||
onSave(script);
|
||||
onClose();
|
||||
};
|
||||
|
||||
const handleTest = async () => {
|
||||
setTesting(true);
|
||||
try {
|
||||
// 使用当前编辑器中的脚本内容进行测试
|
||||
const result = await usageApi.testScript(
|
||||
provider.id,
|
||||
appId,
|
||||
script.code,
|
||||
script.timeout,
|
||||
script.accessToken,
|
||||
script.userId,
|
||||
);
|
||||
if (result.success && result.data && result.data.length > 0) {
|
||||
// 显示所有套餐数据
|
||||
const summary = result.data
|
||||
.map((plan) => {
|
||||
const planInfo = plan.planName ? `[${plan.planName}]` : "";
|
||||
return `${planInfo} ${t("usage.remaining")} ${plan.remaining} ${plan.unit}`;
|
||||
})
|
||||
.join(", ");
|
||||
toast.success(`${t("usageScript.testSuccess")}${summary}`, {
|
||||
duration: 3000,
|
||||
});
|
||||
} else {
|
||||
toast.error(
|
||||
`${t("usageScript.testFailed")}: ${result.error || t("endpointTest.noResult")}`,
|
||||
{
|
||||
duration: 5000,
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
toast.error(
|
||||
`${t("usageScript.testFailed")}: ${error?.message || t("common.unknown")}`,
|
||||
{
|
||||
duration: 5000,
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
setTesting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleFormat = async () => {
|
||||
try {
|
||||
const formatted = await prettier.format(script.code, {
|
||||
parser: "babel",
|
||||
plugins: [parserBabel as any, pluginEstree as any],
|
||||
semi: true,
|
||||
singleQuote: false,
|
||||
tabWidth: 2,
|
||||
printWidth: 80,
|
||||
});
|
||||
setScript({ ...script, code: formatted.trim() });
|
||||
toast.success(t("usageScript.formatSuccess"), { duration: 1000 });
|
||||
} catch (error: any) {
|
||||
toast.error(
|
||||
`${t("usageScript.formatFailed")}: ${error?.message || t("jsonEditor.invalidJson")}`,
|
||||
{
|
||||
duration: 3000,
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const handleUsePreset = (presetName: string) => {
|
||||
const preset = PRESET_TEMPLATES[presetName];
|
||||
if (preset) {
|
||||
// 如果选择的不是 NewAPI 模板,清空高级配置字段
|
||||
if (presetName !== TEMPLATE_KEYS.NEW_API) {
|
||||
setScript({
|
||||
...script,
|
||||
code: preset,
|
||||
accessToken: undefined,
|
||||
userId: undefined,
|
||||
});
|
||||
} else {
|
||||
setScript({ ...script, code: preset });
|
||||
}
|
||||
setSelectedTemplate(presetName); // 记录选择的模板
|
||||
}
|
||||
};
|
||||
|
||||
// 判断是否应该显示高级配置(仅 NewAPI 模板需要)
|
||||
const shouldShowAdvancedConfig = selectedTemplate === TEMPLATE_KEYS.NEW_API;
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
||||
<DialogContent className="max-w-3xl max-h-[90vh] flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{t("usageScript.title")} - {provider.name}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{/* Content - Scrollable */}
|
||||
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
|
||||
{/* 启用开关 */}
|
||||
<label className="flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={script.enabled}
|
||||
onChange={(e) =>
|
||||
setScript({ ...script, enabled: e.target.checked })
|
||||
}
|
||||
className="w-4 h-4"
|
||||
/>
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("usageScript.enableUsageQuery")}
|
||||
</span>
|
||||
</label>
|
||||
|
||||
{script.enabled && (
|
||||
<>
|
||||
{/* 预设模板选择 */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
|
||||
{t("usageScript.presetTemplate")}
|
||||
</label>
|
||||
<div className="flex gap-2">
|
||||
{Object.keys(PRESET_TEMPLATES).map((name) => {
|
||||
const isSelected = selectedTemplate === name;
|
||||
return (
|
||||
<button
|
||||
key={name}
|
||||
onClick={() => handleUsePreset(name)}
|
||||
className={`px-3 py-1.5 text-xs rounded transition-colors ${
|
||||
isSelected
|
||||
? "bg-blue-500 text-white dark:bg-blue-600"
|
||||
: "bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700"
|
||||
}`}
|
||||
>
|
||||
{t(TEMPLATE_NAME_KEYS[name])}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 高级配置:Access Token 和 User ID(仅 NewAPI 模板显示) */}
|
||||
{shouldShowAdvancedConfig && (
|
||||
<div className="space-y-3 p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg">
|
||||
<label className="block">
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{t("usageScript.accessToken")}
|
||||
</span>
|
||||
<input
|
||||
type="text"
|
||||
value={script.accessToken || ""}
|
||||
onChange={(e) =>
|
||||
setScript({ ...script, accessToken: e.target.value })
|
||||
}
|
||||
placeholder={t("usageScript.accessTokenPlaceholder")}
|
||||
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-sm"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="block">
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{t("usageScript.userId")}
|
||||
</span>
|
||||
<input
|
||||
type="text"
|
||||
value={script.userId || ""}
|
||||
onChange={(e) =>
|
||||
setScript({ ...script, userId: e.target.value })
|
||||
}
|
||||
placeholder={t("usageScript.userIdPlaceholder")}
|
||||
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-sm"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 脚本编辑器 */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
|
||||
{t("usageScript.queryScript")}
|
||||
</label>
|
||||
<JsonEditor
|
||||
value={script.code}
|
||||
onChange={(code) => setScript({ ...script, code })}
|
||||
height="300px"
|
||||
language="javascript"
|
||||
/>
|
||||
<p className="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
{t("usageScript.variablesHint", {
|
||||
apiKey: "{{apiKey}}",
|
||||
baseUrl: "{{baseUrl}}",
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 配置选项 */}
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<label className="block">
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("usageScript.timeoutSeconds")}
|
||||
</span>
|
||||
<input
|
||||
type="number"
|
||||
min="2"
|
||||
max="30"
|
||||
value={script.timeout || 10}
|
||||
onChange={(e) =>
|
||||
setScript({
|
||||
...script,
|
||||
timeout: parseInt(e.target.value),
|
||||
})
|
||||
}
|
||||
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
|
||||
/>
|
||||
</label>
|
||||
|
||||
{/* 🆕 自动查询间隔 */}
|
||||
<label className="block">
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("usageScript.autoQueryInterval")}
|
||||
</span>
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="1440"
|
||||
step="1"
|
||||
value={script.autoQueryInterval || 0}
|
||||
onChange={(e) =>
|
||||
setScript({
|
||||
...script,
|
||||
autoQueryInterval: parseInt(e.target.value) || 0,
|
||||
})
|
||||
}
|
||||
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
|
||||
/>
|
||||
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
{t("usageScript.autoQueryIntervalHint")}
|
||||
</p>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{/* 脚本说明 */}
|
||||
<div className="p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-sm text-gray-700 dark:text-gray-300">
|
||||
<h4 className="font-medium mb-2">
|
||||
{t("usageScript.scriptHelp")}
|
||||
</h4>
|
||||
<div className="space-y-3 text-xs">
|
||||
<div>
|
||||
<strong>{t("usageScript.configFormat")}</strong>
|
||||
<pre className="mt-1 p-2 bg-white/50 dark:bg-black/20 rounded text-[10px] overflow-x-auto">
|
||||
{`({
|
||||
request: {
|
||||
url: "{{baseUrl}}/api/usage",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": "Bearer {{apiKey}}",
|
||||
"User-Agent": "cc-switch/1.0"
|
||||
},
|
||||
body: JSON.stringify({ key: "value" }) // ${t("usageScript.commentOptional")}
|
||||
},
|
||||
extractor: function(response) {
|
||||
// ${t("usageScript.commentResponseIsJson")}
|
||||
return {
|
||||
isValid: !response.error,
|
||||
remaining: response.balance,
|
||||
unit: "USD"
|
||||
};
|
||||
}
|
||||
})`}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>{t("usageScript.extractorFormat")}</strong>
|
||||
<ul className="mt-1 space-y-0.5 ml-2">
|
||||
<li>{t("usageScript.fieldIsValid")}</li>
|
||||
<li>{t("usageScript.fieldInvalidMessage")}</li>
|
||||
<li>{t("usageScript.fieldRemaining")}</li>
|
||||
<li>{t("usageScript.fieldUnit")}</li>
|
||||
<li>{t("usageScript.fieldPlanName")}</li>
|
||||
<li>{t("usageScript.fieldTotal")}</li>
|
||||
<li>{t("usageScript.fieldUsed")}</li>
|
||||
<li>{t("usageScript.fieldExtra")}</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div className="text-gray-600 dark:text-gray-400">
|
||||
<strong>{t("usageScript.tips")}</strong>
|
||||
<ul className="mt-1 space-y-0.5 ml-2">
|
||||
<li>
|
||||
{t("usageScript.tip1", {
|
||||
apiKey: "{{apiKey}}",
|
||||
baseUrl: "{{baseUrl}}",
|
||||
})}
|
||||
</li>
|
||||
<li>{t("usageScript.tip2")}</li>
|
||||
<li>{t("usageScript.tip3")}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<DialogFooter className="flex-col sm:flex-row sm:justify-between gap-3 pt-4">
|
||||
{/* Left side - Test and Format buttons */}
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleTest}
|
||||
disabled={!script.enabled || testing}
|
||||
>
|
||||
<Play size={14} />
|
||||
{testing ? t("usageScript.testing") : t("usageScript.testScript")}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleFormat}
|
||||
disabled={!script.enabled}
|
||||
title={t("usageScript.format")}
|
||||
>
|
||||
<Wand2 size={14} />
|
||||
{t("usageScript.format")}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Right side - Cancel and Save buttons */}
|
||||
<div className="flex gap-2">
|
||||
<Button variant="ghost" size="sm" onClick={onClose}>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
<Button variant="default" size="sm" onClick={handleSave}>
|
||||
{t("usageScript.saveConfig")}
|
||||
</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default UsageScriptModal;
|
||||
693
src/components/mcp/McpFormModal.tsx
Normal file
@@ -0,0 +1,693 @@
|
||||
import React, { useMemo, useState, useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
Save,
|
||||
Plus,
|
||||
AlertCircle,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
AlertTriangle,
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { mcpApi, type AppId } from "@/lib/api";
|
||||
import { McpServer, McpServerSpec } from "@/types";
|
||||
import { mcpPresets, getMcpPresetWithDescription } from "@/config/mcpPresets";
|
||||
import McpWizardModal from "./McpWizardModal";
|
||||
import {
|
||||
extractErrorMessage,
|
||||
translateMcpBackendError,
|
||||
} from "@/utils/errorUtils";
|
||||
import {
|
||||
tomlToMcpServer,
|
||||
extractIdFromToml,
|
||||
mcpServerToToml,
|
||||
} from "@/utils/tomlUtils";
|
||||
import { useMcpValidation } from "./useMcpValidation";
|
||||
|
||||
interface McpFormModalProps {
|
||||
appId: AppId;
|
||||
editingId?: string;
|
||||
initialData?: McpServer;
|
||||
onSave: (
|
||||
id: string,
|
||||
server: McpServer,
|
||||
options?: { syncOtherSide?: boolean },
|
||||
) => Promise<void>;
|
||||
onClose: () => void;
|
||||
existingIds?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* MCP 表单模态框组件(简化版)
|
||||
* Claude: 使用 JSON 格式
|
||||
* Codex: 使用 TOML 格式
|
||||
*/
|
||||
const McpFormModal: React.FC<McpFormModalProps> = ({
|
||||
appId,
|
||||
editingId,
|
||||
initialData,
|
||||
onSave,
|
||||
onClose,
|
||||
existingIds = [],
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const { formatTomlError, validateTomlConfig, validateJsonConfig } =
|
||||
useMcpValidation();
|
||||
|
||||
const [formId, setFormId] = useState(
|
||||
() => editingId || initialData?.id || "",
|
||||
);
|
||||
const [formName, setFormName] = useState(initialData?.name || "");
|
||||
const [formDescription, setFormDescription] = useState(
|
||||
initialData?.description || "",
|
||||
);
|
||||
const [formHomepage, setFormHomepage] = useState(initialData?.homepage || "");
|
||||
const [formDocs, setFormDocs] = useState(initialData?.docs || "");
|
||||
const [formTags, setFormTags] = useState(initialData?.tags?.join(", ") || "");
|
||||
|
||||
// 编辑模式下禁止修改 ID
|
||||
const isEditing = !!editingId;
|
||||
|
||||
// 判断是否在编辑模式下有附加信息
|
||||
const hasAdditionalInfo = !!(
|
||||
initialData?.description ||
|
||||
initialData?.tags?.length ||
|
||||
initialData?.homepage ||
|
||||
initialData?.docs
|
||||
);
|
||||
|
||||
// 附加信息展开状态(编辑模式下有值时默认展开)
|
||||
const [showMetadata, setShowMetadata] = useState(
|
||||
isEditing ? hasAdditionalInfo : false,
|
||||
);
|
||||
|
||||
// 根据 appId 决定初始格式
|
||||
const [formConfig, setFormConfig] = useState(() => {
|
||||
const spec = initialData?.server;
|
||||
if (!spec) return "";
|
||||
if (appId === "codex") {
|
||||
return mcpServerToToml(spec);
|
||||
}
|
||||
return JSON.stringify(spec, null, 2);
|
||||
});
|
||||
|
||||
const [configError, setConfigError] = useState("");
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [isWizardOpen, setIsWizardOpen] = useState(false);
|
||||
const [idError, setIdError] = useState("");
|
||||
const [syncOtherSide, setSyncOtherSide] = useState(false);
|
||||
const [otherSideHasConflict, setOtherSideHasConflict] = useState(false);
|
||||
|
||||
// 判断是否使用 TOML 格式
|
||||
const useToml = appId === "codex";
|
||||
const syncTargetLabel =
|
||||
appId === "claude" ? t("apps.codex") : t("apps.claude");
|
||||
const otherAppType: AppId = appId === "claude" ? "codex" : "claude";
|
||||
const syncCheckboxId = useMemo(() => `sync-other-side-${appId}`, [appId]);
|
||||
|
||||
// 检测另一侧是否有同名 MCP
|
||||
useEffect(() => {
|
||||
const checkOtherSide = async () => {
|
||||
const currentId = formId.trim();
|
||||
if (!currentId) {
|
||||
setOtherSideHasConflict(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const otherConfig = await mcpApi.getConfig(otherAppType);
|
||||
const hasConflict = Object.keys(otherConfig.servers || {}).includes(
|
||||
currentId,
|
||||
);
|
||||
setOtherSideHasConflict(hasConflict);
|
||||
} catch (error) {
|
||||
console.error("检查另一侧 MCP 配置失败:", error);
|
||||
setOtherSideHasConflict(false);
|
||||
}
|
||||
};
|
||||
|
||||
checkOtherSide();
|
||||
}, [formId, otherAppType]);
|
||||
|
||||
const wizardInitialSpec = useMemo(() => {
|
||||
const fallback = initialData?.server;
|
||||
if (!formConfig.trim()) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
if (useToml) {
|
||||
try {
|
||||
return tomlToMcpServer(formConfig);
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(formConfig);
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
||||
return parsed as McpServerSpec;
|
||||
}
|
||||
return fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}, [formConfig, initialData, useToml]);
|
||||
|
||||
// 预设选择状态(仅新增模式显示;-1 表示自定义)
|
||||
const [selectedPreset, setSelectedPreset] = useState<number | null>(
|
||||
isEditing ? null : -1,
|
||||
);
|
||||
|
||||
const handleIdChange = (value: string) => {
|
||||
setFormId(value);
|
||||
if (!isEditing) {
|
||||
const exists = existingIds.includes(value.trim());
|
||||
setIdError(exists ? t("mcp.error.idExists") : "");
|
||||
}
|
||||
};
|
||||
|
||||
const ensureUniqueId = (base: string): string => {
|
||||
let candidate = base.trim();
|
||||
if (!candidate) candidate = "mcp-server";
|
||||
if (!existingIds.includes(candidate)) return candidate;
|
||||
let i = 1;
|
||||
while (existingIds.includes(`${candidate}-${i}`)) i++;
|
||||
return `${candidate}-${i}`;
|
||||
};
|
||||
|
||||
// 应用预设(写入表单但不落库)
|
||||
const applyPreset = (index: number) => {
|
||||
if (index < 0 || index >= mcpPresets.length) return;
|
||||
const preset = mcpPresets[index];
|
||||
const presetWithDesc = getMcpPresetWithDescription(preset, t);
|
||||
|
||||
const id = ensureUniqueId(presetWithDesc.id);
|
||||
setFormId(id);
|
||||
setFormName(presetWithDesc.name || presetWithDesc.id);
|
||||
setFormDescription(presetWithDesc.description || "");
|
||||
setFormHomepage(presetWithDesc.homepage || "");
|
||||
setFormDocs(presetWithDesc.docs || "");
|
||||
setFormTags(presetWithDesc.tags?.join(", ") || "");
|
||||
|
||||
// 根据格式转换配置
|
||||
if (useToml) {
|
||||
const toml = mcpServerToToml(presetWithDesc.server);
|
||||
setFormConfig(toml);
|
||||
setConfigError(validateTomlConfig(toml));
|
||||
} else {
|
||||
const json = JSON.stringify(presetWithDesc.server, null, 2);
|
||||
setFormConfig(json);
|
||||
setConfigError(validateJsonConfig(json));
|
||||
}
|
||||
setSelectedPreset(index);
|
||||
};
|
||||
|
||||
// 切回自定义
|
||||
const applyCustom = () => {
|
||||
setSelectedPreset(-1);
|
||||
// 恢复到空白模板
|
||||
setFormId("");
|
||||
setFormName("");
|
||||
setFormDescription("");
|
||||
setFormHomepage("");
|
||||
setFormDocs("");
|
||||
setFormTags("");
|
||||
setFormConfig("");
|
||||
setConfigError("");
|
||||
};
|
||||
|
||||
const handleConfigChange = (value: string) => {
|
||||
setFormConfig(value);
|
||||
|
||||
if (useToml) {
|
||||
// TOML validation (use hook's complete validation)
|
||||
const err = validateTomlConfig(value);
|
||||
if (err) {
|
||||
setConfigError(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to extract ID (if user hasn't filled it yet)
|
||||
if (value.trim() && !formId.trim()) {
|
||||
const extractedId = extractIdFromToml(value);
|
||||
if (extractedId) {
|
||||
setFormId(extractedId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// JSON validation (use hook's complete validation)
|
||||
const err = validateJsonConfig(value);
|
||||
if (err) {
|
||||
setConfigError(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setConfigError("");
|
||||
};
|
||||
|
||||
const handleWizardApply = (title: string, json: string) => {
|
||||
setFormId(title);
|
||||
if (!formName.trim()) {
|
||||
setFormName(title);
|
||||
}
|
||||
// Wizard returns JSON, convert based on format if needed
|
||||
if (useToml) {
|
||||
try {
|
||||
const server = JSON.parse(json) as McpServerSpec;
|
||||
const toml = mcpServerToToml(server);
|
||||
setFormConfig(toml);
|
||||
setConfigError(validateTomlConfig(toml));
|
||||
} catch (e: any) {
|
||||
setConfigError(t("mcp.error.jsonInvalid"));
|
||||
}
|
||||
} else {
|
||||
setFormConfig(json);
|
||||
setConfigError(validateJsonConfig(json));
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const trimmedId = formId.trim();
|
||||
if (!trimmedId) {
|
||||
toast.error(t("mcp.error.idRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
|
||||
// 新增模式:阻止提交重名 ID
|
||||
if (!isEditing && existingIds.includes(trimmedId)) {
|
||||
setIdError(t("mcp.error.idExists"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate configuration format
|
||||
let serverSpec: McpServerSpec;
|
||||
|
||||
if (useToml) {
|
||||
// TOML mode
|
||||
const tomlError = validateTomlConfig(formConfig);
|
||||
setConfigError(tomlError);
|
||||
if (tomlError) {
|
||||
toast.error(t("mcp.error.tomlInvalid"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formConfig.trim()) {
|
||||
// Empty configuration
|
||||
serverSpec = {
|
||||
type: "stdio",
|
||||
command: "",
|
||||
args: [],
|
||||
};
|
||||
} else {
|
||||
try {
|
||||
serverSpec = tomlToMcpServer(formConfig);
|
||||
} catch (e: any) {
|
||||
const msg = e?.message || String(e);
|
||||
setConfigError(formatTomlError(msg));
|
||||
toast.error(t("mcp.error.tomlInvalid"), { duration: 4000 });
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// JSON mode
|
||||
const jsonError = validateJsonConfig(formConfig);
|
||||
setConfigError(jsonError);
|
||||
if (jsonError) {
|
||||
toast.error(t("mcp.error.jsonInvalid"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formConfig.trim()) {
|
||||
// Empty configuration
|
||||
serverSpec = {
|
||||
type: "stdio",
|
||||
command: "",
|
||||
args: [],
|
||||
};
|
||||
} else {
|
||||
try {
|
||||
serverSpec = JSON.parse(formConfig) as McpServerSpec;
|
||||
} catch (e: any) {
|
||||
setConfigError(t("mcp.error.jsonInvalid"));
|
||||
toast.error(t("mcp.error.jsonInvalid"), { duration: 4000 });
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 前置必填校验
|
||||
if (serverSpec?.type === "stdio" && !serverSpec?.command?.trim()) {
|
||||
toast.error(t("mcp.error.commandRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
if (serverSpec?.type === "http" && !serverSpec?.url?.trim()) {
|
||||
toast.error(t("mcp.wizard.urlRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
try {
|
||||
const entry: McpServer = {
|
||||
...(initialData ? { ...initialData } : {}),
|
||||
id: trimmedId,
|
||||
server: serverSpec,
|
||||
};
|
||||
|
||||
// 修复:新增 MCP 时默认启用(enabled=true)
|
||||
// 编辑模式下保留原有的 enabled 状态
|
||||
if (initialData?.enabled !== undefined) {
|
||||
entry.enabled = initialData.enabled;
|
||||
} else {
|
||||
// 新增模式:默认启用
|
||||
entry.enabled = true;
|
||||
}
|
||||
|
||||
const nameTrimmed = (formName || trimmedId).trim();
|
||||
entry.name = nameTrimmed || trimmedId;
|
||||
|
||||
const descriptionTrimmed = formDescription.trim();
|
||||
if (descriptionTrimmed) {
|
||||
entry.description = descriptionTrimmed;
|
||||
} else {
|
||||
delete entry.description;
|
||||
}
|
||||
|
||||
const homepageTrimmed = formHomepage.trim();
|
||||
if (homepageTrimmed) {
|
||||
entry.homepage = homepageTrimmed;
|
||||
} else {
|
||||
delete entry.homepage;
|
||||
}
|
||||
|
||||
const docsTrimmed = formDocs.trim();
|
||||
if (docsTrimmed) {
|
||||
entry.docs = docsTrimmed;
|
||||
} else {
|
||||
delete entry.docs;
|
||||
}
|
||||
|
||||
const parsedTags = formTags
|
||||
.split(",")
|
||||
.map((tag) => tag.trim())
|
||||
.filter((tag) => tag.length > 0);
|
||||
if (parsedTags.length > 0) {
|
||||
entry.tags = parsedTags;
|
||||
} else {
|
||||
delete entry.tags;
|
||||
}
|
||||
|
||||
// 显式等待父组件保存流程
|
||||
await onSave(trimmedId, entry, { syncOtherSide });
|
||||
} catch (error: any) {
|
||||
const detail = extractErrorMessage(error);
|
||||
const mapped = translateMcpBackendError(detail, t);
|
||||
const msg = mapped || detail || t("mcp.error.saveFailed");
|
||||
toast.error(msg, { duration: mapped || detail ? 6000 : 4000 });
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getFormTitle = () => {
|
||||
if (appId === "claude") {
|
||||
return isEditing ? t("mcp.editClaudeServer") : t("mcp.addClaudeServer");
|
||||
} else {
|
||||
return isEditing ? t("mcp.editCodexServer") : t("mcp.addCodexServer");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog open={true} onOpenChange={(open) => !open && onClose()}>
|
||||
<DialogContent className="max-w-3xl max-h-[90vh] flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{getFormTitle()}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{/* Content - Scrollable */}
|
||||
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
|
||||
{/* 预设选择(仅新增时展示) */}
|
||||
{!isEditing && (
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-900 dark:text-gray-100 mb-3">
|
||||
{t("mcp.presets.title")}
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={applyCustom}
|
||||
className={`inline-flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
|
||||
selectedPreset === -1
|
||||
? "bg-emerald-500 text-white dark:bg-emerald-600"
|
||||
: "bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700"
|
||||
}`}
|
||||
>
|
||||
{t("presetSelector.custom")}
|
||||
</button>
|
||||
{mcpPresets.map((preset, idx) => {
|
||||
const descriptionKey = `mcp.presets.${preset.id}.description`;
|
||||
return (
|
||||
<button
|
||||
key={preset.id}
|
||||
type="button"
|
||||
onClick={() => applyPreset(idx)}
|
||||
className={`inline-flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
|
||||
selectedPreset === idx
|
||||
? "bg-emerald-500 text-white dark:bg-emerald-600"
|
||||
: "bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700"
|
||||
}`}
|
||||
title={t(descriptionKey)}
|
||||
>
|
||||
{preset.id}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* ID (标题) */}
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
{t("mcp.form.title")} <span className="text-red-500">*</span>
|
||||
</label>
|
||||
{!isEditing && idError && (
|
||||
<span className="text-xs text-red-500 dark:text-red-400">
|
||||
{idError}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.titlePlaceholder")}
|
||||
value={formId}
|
||||
onChange={(e) => handleIdChange(e.target.value)}
|
||||
disabled={isEditing}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Name */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{t("mcp.form.name")}
|
||||
</label>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.namePlaceholder")}
|
||||
value={formName}
|
||||
onChange={(e) => setFormName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 可折叠的附加信息按钮 */}
|
||||
<div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowMetadata(!showMetadata)}
|
||||
className="flex items-center gap-2 text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
|
||||
>
|
||||
{showMetadata ? (
|
||||
<ChevronUp size={16} />
|
||||
) : (
|
||||
<ChevronDown size={16} />
|
||||
)}
|
||||
{t("mcp.form.additionalInfo")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 附加信息区域(可折叠) */}
|
||||
{showMetadata && (
|
||||
<>
|
||||
{/* Description (描述) */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{t("mcp.form.description")}
|
||||
</label>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.descriptionPlaceholder")}
|
||||
value={formDescription}
|
||||
onChange={(e) => setFormDescription(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Tags */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{t("mcp.form.tags")}
|
||||
</label>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.tagsPlaceholder")}
|
||||
value={formTags}
|
||||
onChange={(e) => setFormTags(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Homepage */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{t("mcp.form.homepage")}
|
||||
</label>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.homepagePlaceholder")}
|
||||
value={formHomepage}
|
||||
onChange={(e) => setFormHomepage(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Docs */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{t("mcp.form.docs")}
|
||||
</label>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t("mcp.form.docsPlaceholder")}
|
||||
value={formDocs}
|
||||
onChange={(e) => setFormDocs(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 配置输入框(根据格式显示 JSON 或 TOML) */}
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
{useToml
|
||||
? t("mcp.form.tomlConfig")
|
||||
: t("mcp.form.jsonConfig")}
|
||||
</label>
|
||||
{(isEditing || selectedPreset === -1) && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setIsWizardOpen(true)}
|
||||
className="text-sm text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 transition-colors"
|
||||
>
|
||||
{t("mcp.form.useWizard")}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<Textarea
|
||||
className="h-48 resize-none font-mono text-xs"
|
||||
placeholder={
|
||||
useToml
|
||||
? t("mcp.form.tomlPlaceholder")
|
||||
: t("mcp.form.jsonPlaceholder")
|
||||
}
|
||||
value={formConfig}
|
||||
onChange={(e) => handleConfigChange(e.target.value)}
|
||||
/>
|
||||
{configError && (
|
||||
<div className="flex items-center gap-2 mt-2 text-red-500 dark:text-red-400 text-sm">
|
||||
<AlertCircle size={16} />
|
||||
<span>{configError}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<DialogFooter className="flex-col sm:flex-row sm:justify-between gap-3 pt-4">
|
||||
{/* 双端同步选项 */}
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
id={syncCheckboxId}
|
||||
type="checkbox"
|
||||
className="h-4 w-4 rounded border-border-default text-emerald-600 focus:ring-emerald-500 dark:bg-gray-800"
|
||||
checked={syncOtherSide}
|
||||
onChange={(event) => setSyncOtherSide(event.target.checked)}
|
||||
/>
|
||||
<label
|
||||
htmlFor={syncCheckboxId}
|
||||
className="text-sm text-gray-700 dark:text-gray-300 cursor-pointer select-none"
|
||||
title={t("mcp.form.syncOtherSideHint", {
|
||||
target: syncTargetLabel,
|
||||
})}
|
||||
>
|
||||
{t("mcp.form.syncOtherSide", { target: syncTargetLabel })}
|
||||
</label>
|
||||
</div>
|
||||
{syncOtherSide && otherSideHasConflict && (
|
||||
<div className="flex items-center gap-1.5 text-amber-600 dark:text-amber-400">
|
||||
<AlertTriangle size={14} />
|
||||
<span className="text-xs font-medium">
|
||||
{t("mcp.form.willOverwriteWarning", {
|
||||
target: syncTargetLabel,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<div className="flex items-center gap-3">
|
||||
<Button type="button" variant="ghost" onClick={onClose}>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
onClick={handleSubmit}
|
||||
disabled={saving || (!isEditing && !!idError)}
|
||||
variant="mcp"
|
||||
>
|
||||
{isEditing ? <Save size={16} /> : <Plus size={16} />}
|
||||
{saving
|
||||
? t("common.saving")
|
||||
: isEditing
|
||||
? t("common.save")
|
||||
: t("common.add")}
|
||||
</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Wizard Modal */}
|
||||
<McpWizardModal
|
||||
isOpen={isWizardOpen}
|
||||
onClose={() => setIsWizardOpen(false)}
|
||||
onApply={handleWizardApply}
|
||||
initialTitle={formId}
|
||||
initialServer={wizardInitialSpec}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default McpFormModal;
|
||||
122
src/components/mcp/McpListItem.tsx
Normal file
@@ -0,0 +1,122 @@
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Edit3, Trash2 } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { settingsApi } from "@/lib/api";
|
||||
import { McpServer } from "@/types";
|
||||
import { mcpPresets } from "@/config/mcpPresets";
|
||||
import McpToggle from "./McpToggle";
|
||||
|
||||
interface McpListItemProps {
|
||||
id: string;
|
||||
server: McpServer;
|
||||
onToggle: (id: string, enabled: boolean) => void;
|
||||
onEdit: (id: string) => void;
|
||||
onDelete: (id: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* MCP 列表项组件
|
||||
* 每个 MCP 占一行,左侧是 Toggle 开关,中间是名称和详细信息,右侧是编辑和删除按钮
|
||||
*/
|
||||
const McpListItem: React.FC<McpListItemProps> = ({
|
||||
id,
|
||||
server,
|
||||
onToggle,
|
||||
onEdit,
|
||||
onDelete,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 仅当显式为 true 时视为启用;避免 undefined 被误判为启用
|
||||
const enabled = server.enabled === true;
|
||||
const name = server.name || id;
|
||||
|
||||
// 只显示 description,没有则留空
|
||||
const description = server.description || "";
|
||||
|
||||
// 匹配预设元信息(用于展示文档链接等)
|
||||
const meta = mcpPresets.find((p) => p.id === id);
|
||||
const docsUrl = server.docs || meta?.docs;
|
||||
const homepageUrl = server.homepage || meta?.homepage;
|
||||
const tags = server.tags || meta?.tags;
|
||||
|
||||
const openDocs = async () => {
|
||||
const url = docsUrl || homepageUrl;
|
||||
if (!url) return;
|
||||
try {
|
||||
await settingsApi.openExternal(url);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="h-16 rounded-lg border border-border-default bg-card p-4 transition-[border-color,box-shadow] duration-200 hover:border-border-hover hover:shadow-sm">
|
||||
<div className="flex items-center gap-4 h-full">
|
||||
{/* 左侧:Toggle 开关 */}
|
||||
<div className="flex-shrink-0">
|
||||
<McpToggle
|
||||
enabled={enabled}
|
||||
onChange={(newEnabled) => onToggle(id, newEnabled)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 中间:名称和详细信息 */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<h3 className="font-medium text-gray-900 dark:text-gray-100 mb-1">
|
||||
{name}
|
||||
</h3>
|
||||
{description && (
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 truncate">
|
||||
{description}
|
||||
</p>
|
||||
)}
|
||||
{!description && tags && tags.length > 0 && (
|
||||
<p className="text-xs text-gray-400 dark:text-gray-500 truncate">
|
||||
{tags.join(", ")}
|
||||
</p>
|
||||
)}
|
||||
{/* 预设标记已移除 */}
|
||||
</div>
|
||||
|
||||
{/* 右侧:操作按钮 */}
|
||||
<div className="flex items-center gap-2 flex-shrink-0">
|
||||
{docsUrl && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={openDocs}
|
||||
title={t("mcp.presets.docs")}
|
||||
>
|
||||
{t("mcp.presets.docs")}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => onEdit(id)}
|
||||
title={t("common.edit")}
|
||||
>
|
||||
<Edit3 size={16} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => onDelete(id)}
|
||||
className="hover:text-red-500 hover:bg-red-100 dark:hover:text-red-400 dark:hover:bg-red-500/10"
|
||||
title={t("common.delete")}
|
||||
>
|
||||
<Trash2 size={16} />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default McpListItem;
|
||||
229
src/components/mcp/McpPanel.tsx
Normal file
@@ -0,0 +1,229 @@
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Plus, Server, Check } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { type AppId } from "@/lib/api";
|
||||
import { McpServer } from "@/types";
|
||||
import { useMcpActions } from "@/hooks/useMcpActions";
|
||||
import McpListItem from "./McpListItem";
|
||||
import McpFormModal from "./McpFormModal";
|
||||
import { ConfirmDialog } from "../ConfirmDialog";
|
||||
|
||||
interface McpPanelProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
appId: AppId;
|
||||
}
|
||||
|
||||
/**
|
||||
* MCP 管理面板
|
||||
* 采用与主界面一致的设计风格,右上角添加按钮,每个 MCP 占一行
|
||||
*/
|
||||
const McpPanel: React.FC<McpPanelProps> = ({ open, onOpenChange, appId }) => {
|
||||
const { t } = useTranslation();
|
||||
const [isFormOpen, setIsFormOpen] = useState(false);
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [confirmDialog, setConfirmDialog] = useState<{
|
||||
isOpen: boolean;
|
||||
title: string;
|
||||
message: string;
|
||||
onConfirm: () => void;
|
||||
} | null>(null);
|
||||
|
||||
// Use MCP actions hook
|
||||
const { servers, loading, reload, toggleEnabled, saveServer, deleteServer } =
|
||||
useMcpActions(appId);
|
||||
|
||||
useEffect(() => {
|
||||
const setup = async () => {
|
||||
try {
|
||||
// Initialize: only import existing MCPs from corresponding client
|
||||
if (appId === "claude") {
|
||||
const mcpApi = await import("@/lib/api").then((m) => m.mcpApi);
|
||||
await mcpApi.importFromClaude();
|
||||
} else if (appId === "codex") {
|
||||
const mcpApi = await import("@/lib/api").then((m) => m.mcpApi);
|
||||
await mcpApi.importFromCodex();
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("MCP initialization import failed (ignored)", e);
|
||||
} finally {
|
||||
await reload();
|
||||
}
|
||||
};
|
||||
setup();
|
||||
// Re-initialize when appId changes
|
||||
}, [appId, reload]);
|
||||
|
||||
const handleEdit = (id: string) => {
|
||||
setEditingId(id);
|
||||
setIsFormOpen(true);
|
||||
};
|
||||
|
||||
const handleAdd = () => {
|
||||
setEditingId(null);
|
||||
setIsFormOpen(true);
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
setConfirmDialog({
|
||||
isOpen: true,
|
||||
title: t("mcp.confirm.deleteTitle"),
|
||||
message: t("mcp.confirm.deleteMessage", { id }),
|
||||
onConfirm: async () => {
|
||||
try {
|
||||
await deleteServer(id);
|
||||
setConfirmDialog(null);
|
||||
} catch (e) {
|
||||
// Error already handled by useMcpActions
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const handleSave = async (
|
||||
id: string,
|
||||
server: McpServer,
|
||||
options?: { syncOtherSide?: boolean },
|
||||
) => {
|
||||
await saveServer(id, server, options);
|
||||
setIsFormOpen(false);
|
||||
setEditingId(null);
|
||||
};
|
||||
|
||||
const handleCloseForm = () => {
|
||||
setIsFormOpen(false);
|
||||
setEditingId(null);
|
||||
};
|
||||
|
||||
const serverEntries = useMemo(
|
||||
() => Object.entries(servers) as Array<[string, McpServer]>,
|
||||
[servers],
|
||||
);
|
||||
|
||||
const enabledCount = useMemo(
|
||||
() => serverEntries.filter(([_, server]) => server.enabled).length,
|
||||
[serverEntries],
|
||||
);
|
||||
|
||||
const panelTitle =
|
||||
appId === "claude" ? t("mcp.claudeTitle") : t("mcp.codexTitle");
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-3xl max-h-[85vh] min-h-[600px] flex flex-col">
|
||||
<DialogHeader>
|
||||
<div className="flex items-center justify-between pr-8">
|
||||
<DialogTitle>{panelTitle}</DialogTitle>
|
||||
<Button type="button" variant="mcp" onClick={handleAdd}>
|
||||
<Plus size={16} />
|
||||
{t("mcp.add")}
|
||||
</Button>
|
||||
</div>
|
||||
</DialogHeader>
|
||||
|
||||
{/* Info Section */}
|
||||
<div className="flex-shrink-0 px-6 py-4">
|
||||
<div className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{t("mcp.serverCount", { count: Object.keys(servers).length })} ·{" "}
|
||||
{t("mcp.enabledCount", { count: enabledCount })}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Content - Scrollable */}
|
||||
<div className="flex-1 overflow-y-auto px-6 pb-4">
|
||||
{loading ? (
|
||||
<div className="text-center py-12 text-gray-500 dark:text-gray-400">
|
||||
{t("mcp.loading")}
|
||||
</div>
|
||||
) : (
|
||||
(() => {
|
||||
const hasAny = serverEntries.length > 0;
|
||||
if (!hasAny) {
|
||||
return (
|
||||
<div className="text-center py-12">
|
||||
<div className="w-16 h-16 mx-auto mb-4 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center">
|
||||
<Server
|
||||
size={24}
|
||||
className="text-gray-400 dark:text-gray-500"
|
||||
/>
|
||||
</div>
|
||||
<h3 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-2">
|
||||
{t("mcp.empty")}
|
||||
</h3>
|
||||
<p className="text-gray-500 dark:text-gray-400 text-sm">
|
||||
{t("mcp.emptyDescription")}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* 已安装 */}
|
||||
{serverEntries.map(([id, server]) => (
|
||||
<McpListItem
|
||||
key={`installed-${id}`}
|
||||
id={id}
|
||||
server={server}
|
||||
onToggle={toggleEnabled}
|
||||
onEdit={handleEdit}
|
||||
onDelete={handleDelete}
|
||||
/>
|
||||
))}
|
||||
|
||||
{/* 预设已移至"新增 MCP"面板中展示与套用 */}
|
||||
</div>
|
||||
);
|
||||
})()
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="mcp"
|
||||
onClick={() => onOpenChange(false)}
|
||||
>
|
||||
<Check size={16} />
|
||||
{t("common.done")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Form Modal */}
|
||||
{isFormOpen && (
|
||||
<McpFormModal
|
||||
appId={appId}
|
||||
editingId={editingId || undefined}
|
||||
initialData={editingId ? servers[editingId] : undefined}
|
||||
existingIds={Object.keys(servers)}
|
||||
onSave={handleSave}
|
||||
onClose={handleCloseForm}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Confirm Dialog */}
|
||||
{confirmDialog && (
|
||||
<ConfirmDialog
|
||||
isOpen={confirmDialog.isOpen}
|
||||
title={confirmDialog.title}
|
||||
message={confirmDialog.message}
|
||||
onConfirm={confirmDialog.onConfirm}
|
||||
onCancel={() => setConfirmDialog(null)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default McpPanel;
|
||||
41
src/components/mcp/McpToggle.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import React from "react";
|
||||
|
||||
interface McpToggleProps {
|
||||
enabled: boolean;
|
||||
onChange: (enabled: boolean) => void;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle 开关组件
|
||||
* 启用时为淡绿色,禁用时为灰色
|
||||
*/
|
||||
const McpToggle: React.FC<McpToggleProps> = ({
|
||||
enabled,
|
||||
onChange,
|
||||
disabled = false,
|
||||
}) => {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={enabled}
|
||||
disabled={disabled}
|
||||
onClick={() => onChange(!enabled)}
|
||||
className={`
|
||||
relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-emerald-500/20
|
||||
${enabled ? "bg-emerald-500 dark:bg-emerald-600" : "bg-gray-300 dark:bg-gray-600"}
|
||||
${disabled ? "opacity-50 cursor-not-allowed" : "cursor-pointer"}
|
||||
`}
|
||||
>
|
||||
<span
|
||||
className={`
|
||||
inline-block h-4 w-4 transform rounded-full bg-white transition-transform
|
||||
${enabled ? "translate-x-6" : "translate-x-1"}
|
||||
`}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
export default McpToggle;
|
||||
410
src/components/mcp/McpWizardModal.tsx
Normal file
@@ -0,0 +1,410 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { toast } from "sonner";
|
||||
import { Save } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { McpServerSpec } from "@/types";
|
||||
|
||||
interface McpWizardModalProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
onApply: (title: string, json: string) => void;
|
||||
initialTitle?: string;
|
||||
initialServer?: McpServerSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析环境变量文本为对象
|
||||
*/
|
||||
const parseEnvText = (text: string): Record<string, string> => {
|
||||
const lines = text
|
||||
.split("\n")
|
||||
.map((l) => l.trim())
|
||||
.filter((l) => l.length > 0);
|
||||
const env: Record<string, string> = {};
|
||||
for (const l of lines) {
|
||||
const idx = l.indexOf("=");
|
||||
if (idx > 0) {
|
||||
const k = l.slice(0, idx).trim();
|
||||
const v = l.slice(idx + 1).trim();
|
||||
if (k) env[k] = v;
|
||||
}
|
||||
}
|
||||
return env;
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析headers文本为对象(支持 KEY: VALUE 或 KEY=VALUE)
|
||||
*/
|
||||
const parseHeadersText = (text: string): Record<string, string> => {
|
||||
const lines = text
|
||||
.split("\n")
|
||||
.map((l) => l.trim())
|
||||
.filter((l) => l.length > 0);
|
||||
const headers: Record<string, string> = {};
|
||||
for (const l of lines) {
|
||||
// 支持 KEY: VALUE 或 KEY=VALUE
|
||||
const colonIdx = l.indexOf(":");
|
||||
const equalIdx = l.indexOf("=");
|
||||
let idx = -1;
|
||||
if (colonIdx > 0 && (equalIdx === -1 || colonIdx < equalIdx)) {
|
||||
idx = colonIdx;
|
||||
} else if (equalIdx > 0) {
|
||||
idx = equalIdx;
|
||||
}
|
||||
if (idx > 0) {
|
||||
const k = l.slice(0, idx).trim();
|
||||
const v = l.slice(idx + 1).trim();
|
||||
if (k) headers[k] = v;
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
|
||||
/**
|
||||
* MCP 配置向导模态框
|
||||
* 帮助用户快速生成 MCP JSON 配置
|
||||
*/
|
||||
const McpWizardModal: React.FC<McpWizardModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
onApply,
|
||||
initialTitle,
|
||||
initialServer,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const [wizardType, setWizardType] = useState<"stdio" | "http">("stdio");
|
||||
const [wizardTitle, setWizardTitle] = useState("");
|
||||
// stdio 字段
|
||||
const [wizardCommand, setWizardCommand] = useState("");
|
||||
const [wizardArgs, setWizardArgs] = useState("");
|
||||
const [wizardEnv, setWizardEnv] = useState("");
|
||||
// http 字段
|
||||
const [wizardUrl, setWizardUrl] = useState("");
|
||||
const [wizardHeaders, setWizardHeaders] = useState("");
|
||||
|
||||
// 生成预览 JSON
|
||||
const generatePreview = (): string => {
|
||||
const config: McpServerSpec = {
|
||||
type: wizardType,
|
||||
};
|
||||
|
||||
if (wizardType === "stdio") {
|
||||
// stdio 类型必需字段
|
||||
config.command = wizardCommand.trim();
|
||||
|
||||
// 可选字段
|
||||
if (wizardArgs.trim()) {
|
||||
config.args = wizardArgs
|
||||
.split("\n")
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s.length > 0);
|
||||
}
|
||||
|
||||
if (wizardEnv.trim()) {
|
||||
const env = parseEnvText(wizardEnv);
|
||||
if (Object.keys(env).length > 0) {
|
||||
config.env = env;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// http 类型必需字段
|
||||
config.url = wizardUrl.trim();
|
||||
|
||||
// 可选字段
|
||||
if (wizardHeaders.trim()) {
|
||||
const headers = parseHeadersText(wizardHeaders);
|
||||
if (Object.keys(headers).length > 0) {
|
||||
config.headers = headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify(config, null, 2);
|
||||
};
|
||||
|
||||
const handleApply = () => {
|
||||
if (!wizardTitle.trim()) {
|
||||
toast.error(t("mcp.error.idRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
if (wizardType === "stdio" && !wizardCommand.trim()) {
|
||||
toast.error(t("mcp.error.commandRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
if (wizardType === "http" && !wizardUrl.trim()) {
|
||||
toast.error(t("mcp.wizard.urlRequired"), { duration: 3000 });
|
||||
return;
|
||||
}
|
||||
|
||||
const json = generatePreview();
|
||||
onApply(wizardTitle.trim(), json);
|
||||
handleClose();
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
// 重置表单
|
||||
setWizardType("stdio");
|
||||
setWizardTitle("");
|
||||
setWizardCommand("");
|
||||
setWizardArgs("");
|
||||
setWizardEnv("");
|
||||
setWizardUrl("");
|
||||
setWizardHeaders("");
|
||||
onClose();
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter" && e.metaKey) {
|
||||
e.preventDefault();
|
||||
handleApply();
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
|
||||
const title = initialTitle ?? "";
|
||||
setWizardTitle(title);
|
||||
|
||||
const resolvedType =
|
||||
initialServer?.type ?? (initialServer?.url ? "http" : "stdio");
|
||||
|
||||
setWizardType(resolvedType);
|
||||
|
||||
if (resolvedType === "http") {
|
||||
setWizardUrl(initialServer?.url ?? "");
|
||||
const headersCandidate = initialServer?.headers;
|
||||
const headers =
|
||||
headersCandidate && typeof headersCandidate === "object"
|
||||
? headersCandidate
|
||||
: undefined;
|
||||
setWizardHeaders(
|
||||
headers
|
||||
? Object.entries(headers)
|
||||
.map(([k, v]) => `${k}: ${v ?? ""}`)
|
||||
.join("\n")
|
||||
: "",
|
||||
);
|
||||
setWizardCommand("");
|
||||
setWizardArgs("");
|
||||
setWizardEnv("");
|
||||
return;
|
||||
}
|
||||
|
||||
setWizardCommand(initialServer?.command ?? "");
|
||||
const argsValue = initialServer?.args;
|
||||
setWizardArgs(Array.isArray(argsValue) ? argsValue.join("\n") : "");
|
||||
const envCandidate = initialServer?.env;
|
||||
const env =
|
||||
envCandidate && typeof envCandidate === "object"
|
||||
? envCandidate
|
||||
: undefined;
|
||||
setWizardEnv(
|
||||
env
|
||||
? Object.entries(env)
|
||||
.map(([k, v]) => `${k}=${v ?? ""}`)
|
||||
.join("\n")
|
||||
: "",
|
||||
);
|
||||
setWizardUrl("");
|
||||
setWizardHeaders("");
|
||||
}, [isOpen]);
|
||||
|
||||
const preview = generatePreview();
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={(open) => !open && handleClose()}>
|
||||
<DialogContent className="max-w-2xl max-h-[90vh] flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("mcp.wizard.title")}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
|
||||
{/* Hint */}
|
||||
<div className="rounded-lg border border-border-active bg-blue-50 p-3 dark:bg-blue-900/20">
|
||||
<p className="text-sm text-blue-800 dark:text-blue-200">
|
||||
{t("mcp.wizard.hint")}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Form Fields */}
|
||||
<div className="space-y-4 min-h-[400px]">
|
||||
{/* Type */}
|
||||
<div>
|
||||
<label className="mb-2 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.type")} <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="flex gap-4">
|
||||
<label className="inline-flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="radio"
|
||||
value="stdio"
|
||||
checked={wizardType === "stdio"}
|
||||
onChange={(e) =>
|
||||
setWizardType(e.target.value as "stdio" | "http")
|
||||
}
|
||||
className="w-4 h-4 text-emerald-500 bg-white dark:bg-gray-800 border-border-default focus:ring-emerald-500 dark:focus:ring-emerald-400 focus:ring-2"
|
||||
/>
|
||||
<span className="text-sm text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.typeStdio")}
|
||||
</span>
|
||||
</label>
|
||||
<label className="inline-flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="radio"
|
||||
value="http"
|
||||
checked={wizardType === "http"}
|
||||
onChange={(e) =>
|
||||
setWizardType(e.target.value as "stdio" | "http")
|
||||
}
|
||||
className="w-4 h-4 text-emerald-500 bg-white dark:bg-gray-800 border-border-default focus:ring-emerald-500 dark:focus:ring-emerald-400 focus:ring-2"
|
||||
/>
|
||||
<span className="text-sm text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.typeHttp")}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Title */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.form.title")} <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={wizardTitle}
|
||||
onChange={(e) => setWizardTitle(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={t("mcp.form.titlePlaceholder")}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Stdio 类型字段 */}
|
||||
{wizardType === "stdio" && (
|
||||
<>
|
||||
{/* Command */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.command")}{" "}
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={wizardCommand}
|
||||
onChange={(e) => setWizardCommand(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={t("mcp.wizard.commandPlaceholder")}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Args */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.args")}
|
||||
</label>
|
||||
<textarea
|
||||
value={wizardArgs}
|
||||
onChange={(e) => setWizardArgs(e.target.value)}
|
||||
placeholder={t("mcp.wizard.argsPlaceholder")}
|
||||
rows={3}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100 resize-y"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Env */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.env")}
|
||||
</label>
|
||||
<textarea
|
||||
value={wizardEnv}
|
||||
onChange={(e) => setWizardEnv(e.target.value)}
|
||||
placeholder={t("mcp.wizard.envPlaceholder")}
|
||||
rows={3}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100 resize-y"
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* HTTP 类型字段 */}
|
||||
{wizardType === "http" && (
|
||||
<>
|
||||
{/* URL */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.url")}{" "}
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={wizardUrl}
|
||||
onChange={(e) => setWizardUrl(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={t("mcp.wizard.urlPlaceholder")}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Headers */}
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.headers")}
|
||||
</label>
|
||||
<textarea
|
||||
value={wizardHeaders}
|
||||
onChange={(e) => setWizardHeaders(e.target.value)}
|
||||
placeholder={t("mcp.wizard.headersPlaceholder")}
|
||||
rows={3}
|
||||
className="w-full rounded-lg border border-border-default px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-emerald-500/20 dark:bg-gray-800 dark:text-gray-100 resize-y"
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Preview */}
|
||||
{(wizardCommand ||
|
||||
wizardArgs ||
|
||||
wizardEnv ||
|
||||
wizardUrl ||
|
||||
wizardHeaders) && (
|
||||
<div className="space-y-2 border-t border-border-default pt-4 ">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{t("mcp.wizard.preview")}
|
||||
</h3>
|
||||
<pre className="overflow-x-auto rounded-lg bg-gray-50 p-3 text-xs font-mono text-gray-700 dark:bg-gray-800 dark:text-gray-300">
|
||||
{preview}
|
||||
</pre>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<DialogFooter className="gap-3 pt-4">
|
||||
<Button type="button" variant="ghost" onClick={handleClose}>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
<Button type="button" variant="mcp" onClick={handleApply}>
|
||||
<Save className="h-4 w-4" />
|
||||
{t("mcp.wizard.apply")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default McpWizardModal;
|
||||
94
src/components/mcp/useMcpValidation.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { validateToml, tomlToMcpServer } from "@/utils/tomlUtils";
|
||||
|
||||
export function useMcpValidation() {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// JSON basic validation (returns i18n text)
|
||||
const validateJson = (text: string): string => {
|
||||
if (!text.trim()) return "";
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
return t("mcp.error.jsonInvalid");
|
||||
}
|
||||
return "";
|
||||
} catch {
|
||||
return t("mcp.error.jsonInvalid");
|
||||
}
|
||||
};
|
||||
|
||||
// Unified TOML error formatting (localization + details)
|
||||
const formatTomlError = (err: string): string => {
|
||||
if (!err) return "";
|
||||
if (err === "mustBeObject" || err === "parseError") {
|
||||
return t("mcp.error.tomlInvalid");
|
||||
}
|
||||
return `${t("mcp.error.tomlInvalid")}: ${err}`;
|
||||
};
|
||||
|
||||
// Full TOML validation (including required field checks)
|
||||
const validateTomlConfig = (value: string): string => {
|
||||
const err = validateToml(value);
|
||||
if (err) {
|
||||
return formatTomlError(err);
|
||||
}
|
||||
|
||||
// Try to parse and check required fields
|
||||
if (value.trim()) {
|
||||
try {
|
||||
const server = tomlToMcpServer(value);
|
||||
if (server.type === "stdio" && !server.command?.trim()) {
|
||||
return t("mcp.error.commandRequired");
|
||||
}
|
||||
if (server.type === "http" && !server.url?.trim()) {
|
||||
return t("mcp.wizard.urlRequired");
|
||||
}
|
||||
} catch (e: any) {
|
||||
const msg = e?.message || String(e);
|
||||
return formatTomlError(msg);
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
};
|
||||
|
||||
// Full JSON validation (including structure checks)
|
||||
const validateJsonConfig = (value: string): string => {
|
||||
const baseErr = validateJson(value);
|
||||
if (baseErr) {
|
||||
return baseErr;
|
||||
}
|
||||
|
||||
// Further structure validation
|
||||
if (value.trim()) {
|
||||
try {
|
||||
const obj = JSON.parse(value);
|
||||
if (obj && typeof obj === "object") {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, "mcpServers")) {
|
||||
return t("mcp.error.singleServerObjectRequired");
|
||||
}
|
||||
|
||||
const typ = (obj as any)?.type;
|
||||
if (typ === "stdio" && !(obj as any)?.command?.trim()) {
|
||||
return t("mcp.error.commandRequired");
|
||||
}
|
||||
if (typ === "http" && !(obj as any)?.url?.trim()) {
|
||||
return t("mcp.wizard.urlRequired");
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Parse errors already covered by base validation
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
};
|
||||
|
||||
return {
|
||||
validateJson,
|
||||
formatTomlError,
|
||||
validateTomlConfig,
|
||||
validateJsonConfig,
|
||||
};
|
||||
}
|
||||
27
src/components/mode-toggle.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Moon, Sun } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useTheme } from "@/components/theme-provider";
|
||||
|
||||
export function ModeToggle() {
|
||||
const { theme, setTheme } = useTheme();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const toggleTheme = () => {
|
||||
// 如果当前是 dark 或 system(且系统是暗色),切换到 light
|
||||
// 否则切换到 dark
|
||||
if (theme === "dark") {
|
||||
setTheme("light");
|
||||
} else {
|
||||
setTheme("dark");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Button variant="outline" size="icon" onClick={toggleTheme}>
|
||||
<Sun className="h-4 w-4 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
|
||||
<Moon className="absolute h-4 w-4 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
|
||||
<span className="sr-only">{t("common.toggleTheme")}</span>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
179
src/components/providers/AddProviderDialog.tsx
Normal file
@@ -0,0 +1,179 @@
|
||||
import { useCallback } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Plus } from "lucide-react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { Provider, CustomEndpoint } from "@/types";
|
||||
import type { AppId } from "@/lib/api";
|
||||
import {
|
||||
ProviderForm,
|
||||
type ProviderFormValues,
|
||||
} from "@/components/providers/forms/ProviderForm";
|
||||
import { providerPresets } from "@/config/claudeProviderPresets";
|
||||
import { codexProviderPresets } from "@/config/codexProviderPresets";
|
||||
|
||||
interface AddProviderDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
appId: AppId;
|
||||
onSubmit: (provider: Omit<Provider, "id">) => Promise<void> | void;
|
||||
}
|
||||
|
||||
export function AddProviderDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
appId,
|
||||
onSubmit,
|
||||
}: AddProviderDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (values: ProviderFormValues) => {
|
||||
const parsedConfig = JSON.parse(values.settingsConfig) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
|
||||
// 构造基础提交数据
|
||||
const providerData: Omit<Provider, "id"> = {
|
||||
name: values.name.trim(),
|
||||
websiteUrl: values.websiteUrl?.trim() || undefined,
|
||||
settingsConfig: parsedConfig,
|
||||
...(values.presetCategory ? { category: values.presetCategory } : {}),
|
||||
...(values.meta ? { meta: values.meta } : {}),
|
||||
};
|
||||
|
||||
const hasCustomEndpoints =
|
||||
providerData.meta?.custom_endpoints &&
|
||||
Object.keys(providerData.meta.custom_endpoints).length > 0;
|
||||
|
||||
if (!hasCustomEndpoints) {
|
||||
// 收集端点候选(仅在缺少自定义端点时兜底)
|
||||
// 1. 从预设配置中获取 endpointCandidates
|
||||
// 2. 从当前配置中提取 baseUrl (ANTHROPIC_BASE_URL 或 Codex base_url)
|
||||
const urlSet = new Set<string>();
|
||||
|
||||
const addUrl = (rawUrl?: string) => {
|
||||
const url = (rawUrl || "").trim().replace(/\/+$/, "");
|
||||
if (url && url.startsWith("http")) {
|
||||
urlSet.add(url);
|
||||
}
|
||||
};
|
||||
|
||||
if (values.presetId) {
|
||||
if (appId === "claude") {
|
||||
const presets = providerPresets;
|
||||
const presetIndex = parseInt(
|
||||
values.presetId.replace("claude-", ""),
|
||||
);
|
||||
if (
|
||||
!isNaN(presetIndex) &&
|
||||
presetIndex >= 0 &&
|
||||
presetIndex < presets.length
|
||||
) {
|
||||
const preset = presets[presetIndex];
|
||||
if (preset?.endpointCandidates) {
|
||||
preset.endpointCandidates.forEach(addUrl);
|
||||
}
|
||||
}
|
||||
} else if (appId === "codex") {
|
||||
const presets = codexProviderPresets;
|
||||
const presetIndex = parseInt(values.presetId.replace("codex-", ""));
|
||||
if (
|
||||
!isNaN(presetIndex) &&
|
||||
presetIndex >= 0 &&
|
||||
presetIndex < presets.length
|
||||
) {
|
||||
const preset = presets[presetIndex];
|
||||
if (Array.isArray(preset.endpointCandidates)) {
|
||||
preset.endpointCandidates.forEach(addUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (appId === "claude") {
|
||||
const env = parsedConfig.env as Record<string, any> | undefined;
|
||||
if (env?.ANTHROPIC_BASE_URL) {
|
||||
addUrl(env.ANTHROPIC_BASE_URL);
|
||||
}
|
||||
} else if (appId === "codex") {
|
||||
const config = parsedConfig.config as string | undefined;
|
||||
if (config) {
|
||||
const baseUrlMatch = config.match(
|
||||
/base_url\s*=\s*["']([^"']+)["']/,
|
||||
);
|
||||
if (baseUrlMatch?.[1]) {
|
||||
addUrl(baseUrlMatch[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const urls = Array.from(urlSet);
|
||||
if (urls.length > 0) {
|
||||
const now = Date.now();
|
||||
const customEndpoints: Record<string, CustomEndpoint> = {};
|
||||
urls.forEach((url) => {
|
||||
customEndpoints[url] = {
|
||||
url,
|
||||
addedAt: now,
|
||||
lastUsed: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
providerData.meta = {
|
||||
...(providerData.meta ?? {}),
|
||||
custom_endpoints: customEndpoints,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
await onSubmit(providerData);
|
||||
onOpenChange(false);
|
||||
},
|
||||
[appId, onSubmit, onOpenChange],
|
||||
);
|
||||
|
||||
const submitLabel =
|
||||
appId === "claude"
|
||||
? t("provider.addClaudeProvider")
|
||||
: t("provider.addCodexProvider");
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-3xl max-h-[85vh] min-h-[600px] flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{submitLabel}</DialogTitle>
|
||||
<DialogDescription>{t("provider.addProviderHint")}</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex-1 overflow-y-auto px-6 py-4">
|
||||
<ProviderForm
|
||||
appId={appId}
|
||||
submitLabel={t("common.add")}
|
||||
onSubmit={handleSubmit}
|
||||
onCancel={() => onOpenChange(false)}
|
||||
showButtons={false}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
<Button type="submit" form="provider-form">
|
||||
<Plus className="h-4 w-4" />
|
||||
{t("common.add")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
154
src/components/providers/EditProviderDialog.tsx
Normal file
@@ -0,0 +1,154 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Save } from "lucide-react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { Provider } from "@/types";
|
||||
import {
|
||||
ProviderForm,
|
||||
type ProviderFormValues,
|
||||
} from "@/components/providers/forms/ProviderForm";
|
||||
import { providersApi, vscodeApi, type AppId } from "@/lib/api";
|
||||
|
||||
interface EditProviderDialogProps {
|
||||
open: boolean;
|
||||
provider: Provider | null;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onSubmit: (provider: Provider) => Promise<void> | void;
|
||||
appId: AppId;
|
||||
}
|
||||
|
||||
export function EditProviderDialog({
|
||||
open,
|
||||
provider,
|
||||
onOpenChange,
|
||||
onSubmit,
|
||||
appId,
|
||||
}: EditProviderDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 默认使用传入的 provider.settingsConfig,若当前编辑对象是“当前生效供应商”,则尝试读取实时配置替换初始值
|
||||
const [liveSettings, setLiveSettings] = useState<Record<
|
||||
string,
|
||||
unknown
|
||||
> | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const load = async () => {
|
||||
if (!open || !provider) {
|
||||
setLiveSettings(null);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const currentId = await providersApi.getCurrent(appId);
|
||||
if (currentId && provider.id === currentId) {
|
||||
try {
|
||||
const live = (await vscodeApi.getLiveProviderSettings(
|
||||
appId,
|
||||
)) as Record<string, unknown>;
|
||||
if (!cancelled && live && typeof live === "object") {
|
||||
setLiveSettings(live);
|
||||
}
|
||||
} catch {
|
||||
// 读取实时配置失败则回退到 SSOT(不打断编辑流程)
|
||||
if (!cancelled) setLiveSettings(null);
|
||||
}
|
||||
} else {
|
||||
if (!cancelled) setLiveSettings(null);
|
||||
}
|
||||
} finally {
|
||||
// no-op
|
||||
}
|
||||
};
|
||||
void load();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [open, provider, appId]);
|
||||
|
||||
const initialSettingsConfig = useMemo(() => {
|
||||
return (liveSettings ?? provider?.settingsConfig ?? {}) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
}, [liveSettings, provider]);
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (values: ProviderFormValues) => {
|
||||
if (!provider) return;
|
||||
|
||||
const parsedConfig = JSON.parse(values.settingsConfig) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
|
||||
const updatedProvider: Provider = {
|
||||
...provider,
|
||||
name: values.name.trim(),
|
||||
websiteUrl: values.websiteUrl?.trim() || undefined,
|
||||
settingsConfig: parsedConfig,
|
||||
...(values.presetCategory ? { category: values.presetCategory } : {}),
|
||||
// 保留或更新 meta 字段
|
||||
...(values.meta ? { meta: values.meta } : {}),
|
||||
};
|
||||
|
||||
await onSubmit(updatedProvider);
|
||||
onOpenChange(false);
|
||||
},
|
||||
[onSubmit, onOpenChange, provider],
|
||||
);
|
||||
|
||||
if (!provider) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-3xl max-h-[85vh] min-h-[600px] flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("provider.editProvider")}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{t("provider.editProviderHint")}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex-1 overflow-y-auto px-6 py-4">
|
||||
<ProviderForm
|
||||
appId={appId}
|
||||
providerId={provider.id}
|
||||
submitLabel={t("common.save")}
|
||||
onSubmit={handleSubmit}
|
||||
onCancel={() => onOpenChange(false)}
|
||||
initialData={{
|
||||
name: provider.name,
|
||||
websiteUrl: provider.websiteUrl,
|
||||
// 若读取到实时配置则优先使用
|
||||
settingsConfig: initialSettingsConfig,
|
||||
category: provider.category,
|
||||
meta: provider.meta,
|
||||
}}
|
||||
showButtons={false}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
<Button type="submit" form="provider-form">
|
||||
<Save className="h-4 w-4" />
|
||||
{t("common.save")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
83
src/components/providers/ProviderActions.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
import { BarChart3, Check, Edit, Play, Trash2 } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface ProviderActionsProps {
|
||||
isCurrent: boolean;
|
||||
onSwitch: () => void;
|
||||
onEdit: () => void;
|
||||
onConfigureUsage: () => void;
|
||||
onDelete: () => void;
|
||||
}
|
||||
|
||||
export function ProviderActions({
|
||||
isCurrent,
|
||||
onSwitch,
|
||||
onEdit,
|
||||
onConfigureUsage,
|
||||
onDelete,
|
||||
}: ProviderActionsProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant={isCurrent ? "secondary" : "default"}
|
||||
onClick={onSwitch}
|
||||
disabled={isCurrent}
|
||||
className={cn(
|
||||
"w-20",
|
||||
isCurrent &&
|
||||
"bg-gray-200 text-muted-foreground hover:bg-gray-200 hover:text-muted-foreground dark:bg-gray-700 dark:hover:bg-gray-700",
|
||||
)}
|
||||
>
|
||||
{isCurrent ? (
|
||||
<>
|
||||
<Check className="h-4 w-4" />
|
||||
{t("provider.inUse")}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Play className="h-4 w-4" />
|
||||
{t("provider.enable")}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
onClick={onEdit}
|
||||
title={t("common.edit")}
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
onClick={onConfigureUsage}
|
||||
title={t("provider.configureUsage")}
|
||||
>
|
||||
<BarChart3 className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
onClick={isCurrent ? undefined : onDelete}
|
||||
title={t("common.delete")}
|
||||
className={cn(
|
||||
!isCurrent && "hover:text-red-500 dark:hover:text-red-400",
|
||||
isCurrent && "opacity-40 cursor-not-allowed text-muted-foreground",
|
||||
)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
194
src/components/providers/ProviderCard.tsx
Normal file
@@ -0,0 +1,194 @@
|
||||
import { useMemo } from "react";
|
||||
import { MoveVertical, Copy } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type {
|
||||
DraggableAttributes,
|
||||
DraggableSyntheticListeners,
|
||||
} from "@dnd-kit/core";
|
||||
import type { Provider } from "@/types";
|
||||
import type { AppId } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ProviderActions } from "@/components/providers/ProviderActions";
|
||||
import UsageFooter from "@/components/UsageFooter";
|
||||
|
||||
interface DragHandleProps {
|
||||
attributes: DraggableAttributes;
|
||||
listeners: DraggableSyntheticListeners;
|
||||
isDragging: boolean;
|
||||
}
|
||||
|
||||
interface ProviderCardProps {
|
||||
provider: Provider;
|
||||
isCurrent: boolean;
|
||||
appId: AppId;
|
||||
isEditMode?: boolean;
|
||||
onSwitch: (provider: Provider) => void;
|
||||
onEdit: (provider: Provider) => void;
|
||||
onDelete: (provider: Provider) => void;
|
||||
onConfigureUsage: (provider: Provider) => void;
|
||||
onOpenWebsite: (url: string) => void;
|
||||
onDuplicate: (provider: Provider) => void;
|
||||
dragHandleProps?: DragHandleProps;
|
||||
}
|
||||
|
||||
const extractApiUrl = (provider: Provider, fallbackText: string) => {
|
||||
if (provider.websiteUrl) {
|
||||
return provider.websiteUrl;
|
||||
}
|
||||
|
||||
const config = provider.settingsConfig;
|
||||
|
||||
if (config && typeof config === "object") {
|
||||
const envBase = (config as Record<string, any>)?.env?.ANTHROPIC_BASE_URL;
|
||||
if (typeof envBase === "string" && envBase.trim()) {
|
||||
return envBase;
|
||||
}
|
||||
|
||||
const baseUrl = (config as Record<string, any>)?.config;
|
||||
|
||||
if (typeof baseUrl === "string" && baseUrl.includes("base_url")) {
|
||||
const match = baseUrl.match(/base_url\s*=\s*['"]([^'"]+)['"]/);
|
||||
if (match?.[1]) {
|
||||
return match[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fallbackText;
|
||||
};
|
||||
|
||||
export function ProviderCard({
|
||||
provider,
|
||||
isCurrent,
|
||||
appId,
|
||||
isEditMode = false,
|
||||
onSwitch,
|
||||
onEdit,
|
||||
onDelete,
|
||||
onConfigureUsage,
|
||||
onOpenWebsite,
|
||||
onDuplicate,
|
||||
dragHandleProps,
|
||||
}: ProviderCardProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const fallbackUrlText = t("provider.notConfigured", {
|
||||
defaultValue: "未配置接口地址",
|
||||
});
|
||||
|
||||
const displayUrl = useMemo(() => {
|
||||
return extractApiUrl(provider, fallbackUrlText);
|
||||
}, [provider, fallbackUrlText]);
|
||||
|
||||
const usageEnabled = provider.meta?.usage_script?.enabled ?? false;
|
||||
|
||||
const handleOpenWebsite = () => {
|
||||
if (!displayUrl || displayUrl === fallbackUrlText) {
|
||||
return;
|
||||
}
|
||||
onOpenWebsite(displayUrl);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"rounded-lg bg-card p-4 shadow-sm",
|
||||
"transition-[border-color,background-color,box-shadow,ring] duration-200",
|
||||
isCurrent
|
||||
? "border border-border-default bg-primary/5 ring-2 ring-blue-500/30 dark:ring-blue-400/30"
|
||||
: "border border-border-default hover:border-border-hover",
|
||||
dragHandleProps?.isDragging &&
|
||||
"cursor-grabbing border-active border-border-dragging shadow-lg",
|
||||
)}
|
||||
>
|
||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="flex flex-1 items-center gap-2">
|
||||
<div
|
||||
className={cn(
|
||||
"flex items-center gap-1 overflow-hidden",
|
||||
"transition-[max-width,opacity] duration-200 ease-in-out",
|
||||
isEditMode ? "max-w-20 opacity-100" : "max-w-0 opacity-0",
|
||||
)}
|
||||
aria-hidden={!isEditMode}
|
||||
>
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className={cn(
|
||||
"flex-shrink-0 cursor-grab active:cursor-grabbing",
|
||||
dragHandleProps?.isDragging && "cursor-grabbing",
|
||||
)}
|
||||
aria-label={t("provider.dragHandle")}
|
||||
disabled={!isEditMode}
|
||||
{...(dragHandleProps?.attributes ?? {})}
|
||||
{...(dragHandleProps?.listeners ?? {})}
|
||||
>
|
||||
<MoveVertical className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="flex-shrink-0"
|
||||
onClick={() => onDuplicate(provider)}
|
||||
disabled={!isEditMode}
|
||||
aria-label={t("provider.duplicate")}
|
||||
title={t("provider.duplicate")}
|
||||
>
|
||||
<Copy className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<div className="flex flex-wrap items-center gap-2 min-h-[20px]">
|
||||
<h3 className="text-base font-semibold leading-none">
|
||||
{provider.name}
|
||||
</h3>
|
||||
<span
|
||||
className={cn(
|
||||
"rounded-full bg-green-500/10 px-2 py-0.5 text-xs font-medium text-green-500 dark:text-green-400 transition-opacity duration-200",
|
||||
isCurrent ? "opacity-100" : "opacity-0 pointer-events-none",
|
||||
)}
|
||||
>
|
||||
{t("provider.currentlyUsing")}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{displayUrl && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleOpenWebsite}
|
||||
className="inline-flex items-center text-sm text-blue-500 transition-colors hover:underline dark:text-blue-400"
|
||||
title={displayUrl}
|
||||
>
|
||||
<span className="truncate">{displayUrl}</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<UsageFooter
|
||||
provider={provider}
|
||||
providerId={provider.id}
|
||||
appId={appId}
|
||||
usageEnabled={usageEnabled}
|
||||
isCurrent={isCurrent}
|
||||
inline={true}
|
||||
/>
|
||||
|
||||
<ProviderActions
|
||||
isCurrent={isCurrent}
|
||||
onSwitch={() => onSwitch(provider)}
|
||||
onEdit={() => onEdit(provider)}
|
||||
onConfigureUsage={() => onConfigureUsage(provider)}
|
||||
onDelete={() => onDelete(provider)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
28
src/components/providers/ProviderEmptyState.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Users } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface ProviderEmptyStateProps {
|
||||
onCreate?: () => void;
|
||||
}
|
||||
|
||||
export function ProviderEmptyState({ onCreate }: ProviderEmptyStateProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center rounded-lg border border-dashed border-muted-foreground/30 p-10 text-center">
|
||||
<div className="mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-muted">
|
||||
<Users className="h-7 w-7 text-muted-foreground" />
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold">{t("provider.noProviders")}</h3>
|
||||
<p className="mt-2 max-w-sm text-sm text-muted-foreground">
|
||||
{t("provider.noProvidersDescription")}
|
||||
</p>
|
||||
{onCreate && (
|
||||
<Button className="mt-6" onClick={onCreate}>
|
||||
{t("provider.addProvider")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
160
src/components/providers/ProviderList.tsx
Normal file
@@ -0,0 +1,160 @@
|
||||
import { CSS } from "@dnd-kit/utilities";
|
||||
import { DndContext, closestCenter } from "@dnd-kit/core";
|
||||
import {
|
||||
SortableContext,
|
||||
useSortable,
|
||||
verticalListSortingStrategy,
|
||||
} from "@dnd-kit/sortable";
|
||||
import type { CSSProperties } from "react";
|
||||
import type { Provider } from "@/types";
|
||||
import type { AppId } from "@/lib/api";
|
||||
import { useDragSort } from "@/hooks/useDragSort";
|
||||
import { ProviderCard } from "@/components/providers/ProviderCard";
|
||||
import { ProviderEmptyState } from "@/components/providers/ProviderEmptyState";
|
||||
|
||||
interface ProviderListProps {
|
||||
providers: Record<string, Provider>;
|
||||
currentProviderId: string;
|
||||
appId: AppId;
|
||||
isEditMode?: boolean;
|
||||
onSwitch: (provider: Provider) => void;
|
||||
onEdit: (provider: Provider) => void;
|
||||
onDelete: (provider: Provider) => void;
|
||||
onDuplicate: (provider: Provider) => void;
|
||||
onConfigureUsage?: (provider: Provider) => void;
|
||||
onOpenWebsite: (url: string) => void;
|
||||
onCreate?: () => void;
|
||||
isLoading?: boolean;
|
||||
}
|
||||
|
||||
export function ProviderList({
|
||||
providers,
|
||||
currentProviderId,
|
||||
appId,
|
||||
isEditMode = false,
|
||||
onSwitch,
|
||||
onEdit,
|
||||
onDelete,
|
||||
onDuplicate,
|
||||
onConfigureUsage,
|
||||
onOpenWebsite,
|
||||
onCreate,
|
||||
isLoading = false,
|
||||
}: ProviderListProps) {
|
||||
const { sortedProviders, sensors, handleDragEnd } = useDragSort(
|
||||
providers,
|
||||
appId,
|
||||
);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{[0, 1, 2].map((index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="h-28 w-full rounded-lg border border-dashed border-muted-foreground/40 bg-muted/40"
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (sortedProviders.length === 0) {
|
||||
return <ProviderEmptyState onCreate={onCreate} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext
|
||||
items={sortedProviders.map((provider) => provider.id)}
|
||||
strategy={verticalListSortingStrategy}
|
||||
>
|
||||
<div className="space-y-3">
|
||||
{sortedProviders.map((provider) => (
|
||||
<SortableProviderCard
|
||||
key={provider.id}
|
||||
provider={provider}
|
||||
isCurrent={provider.id === currentProviderId}
|
||||
appId={appId}
|
||||
isEditMode={isEditMode}
|
||||
onSwitch={onSwitch}
|
||||
onEdit={onEdit}
|
||||
onDelete={onDelete}
|
||||
onDuplicate={onDuplicate}
|
||||
onConfigureUsage={onConfigureUsage}
|
||||
onOpenWebsite={onOpenWebsite}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
);
|
||||
}
|
||||
|
||||
interface SortableProviderCardProps {
|
||||
provider: Provider;
|
||||
isCurrent: boolean;
|
||||
appId: AppId;
|
||||
isEditMode: boolean;
|
||||
onSwitch: (provider: Provider) => void;
|
||||
onEdit: (provider: Provider) => void;
|
||||
onDelete: (provider: Provider) => void;
|
||||
onDuplicate: (provider: Provider) => void;
|
||||
onConfigureUsage?: (provider: Provider) => void;
|
||||
onOpenWebsite: (url: string) => void;
|
||||
}
|
||||
|
||||
function SortableProviderCard({
|
||||
provider,
|
||||
isCurrent,
|
||||
appId,
|
||||
isEditMode,
|
||||
onSwitch,
|
||||
onEdit,
|
||||
onDelete,
|
||||
onDuplicate,
|
||||
onConfigureUsage,
|
||||
onOpenWebsite,
|
||||
}: SortableProviderCardProps) {
|
||||
const {
|
||||
setNodeRef,
|
||||
attributes,
|
||||
listeners,
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: provider.id });
|
||||
|
||||
const style: CSSProperties = {
|
||||
transform: CSS.Transform.toString(transform),
|
||||
transition,
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={setNodeRef} style={style}>
|
||||
<ProviderCard
|
||||
provider={provider}
|
||||
isCurrent={isCurrent}
|
||||
appId={appId}
|
||||
isEditMode={isEditMode}
|
||||
onSwitch={onSwitch}
|
||||
onEdit={onEdit}
|
||||
onDelete={onDelete}
|
||||
onDuplicate={onDuplicate}
|
||||
onConfigureUsage={
|
||||
onConfigureUsage ? (item) => onConfigureUsage(item) : () => undefined
|
||||
}
|
||||
onOpenWebsite={onOpenWebsite}
|
||||
dragHandleProps={{
|
||||
attributes,
|
||||
listeners,
|
||||
isDragging,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
72
src/components/providers/forms/ApiKeyInput.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import React, { useState } from "react";
|
||||
import { Eye, EyeOff } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface ApiKeyInputProps {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
required?: boolean;
|
||||
label?: string;
|
||||
id?: string;
|
||||
}
|
||||
|
||||
const ApiKeyInput: React.FC<ApiKeyInputProps> = ({
|
||||
value,
|
||||
onChange,
|
||||
placeholder,
|
||||
disabled = false,
|
||||
required = false,
|
||||
label = "API Key",
|
||||
id = "apiKey",
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const [showKey, setShowKey] = useState(false);
|
||||
|
||||
const toggleShowKey = () => {
|
||||
setShowKey(!showKey);
|
||||
};
|
||||
|
||||
const inputClass = `w-full px-3 py-2 pr-10 border rounded-lg text-sm transition-colors ${
|
||||
disabled
|
||||
? "bg-gray-100 dark:bg-gray-800 border-border-default text-gray-400 dark:text-gray-500 cursor-not-allowed"
|
||||
: "border-border-default dark:bg-gray-800 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500/20 dark:focus:ring-blue-400/20"
|
||||
}`;
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<label
|
||||
htmlFor={id}
|
||||
className="block text-sm font-medium text-gray-900 dark:text-gray-100"
|
||||
>
|
||||
{label} {required && "*"}
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showKey ? "text" : "password"}
|
||||
id={id}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder={placeholder ?? t("apiKeyInput.placeholder")}
|
||||
disabled={disabled}
|
||||
required={required}
|
||||
autoComplete="off"
|
||||
className={inputClass}
|
||||
/>
|
||||
{!disabled && value && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggleShowKey}
|
||||
className="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
|
||||
aria-label={showKey ? t("apiKeyInput.hide") : t("apiKeyInput.show")}
|
||||
>
|
||||
{showKey ? <EyeOff size={16} /> : <Eye size={16} />}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ApiKeyInput;
|
||||