mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-27 12:23:43 +08:00
Compare commits
503 Commits
feat-redes
...
v2.15.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df62736ff6 | ||
|
|
6a464b3e5f | ||
|
|
57fcda80df | ||
|
|
db39fbc419 | ||
|
|
3dabe47c78 | ||
|
|
affc194cde | ||
|
|
03fa580a55 | ||
|
|
d0dce654bf | ||
|
|
169323e238 | ||
|
|
71df415b14 | ||
|
|
6bb01bc564 | ||
|
|
07c6fe5975 | ||
|
|
5964181cb2 | ||
|
|
4b8288a2b2 | ||
|
|
88b1c1c6a5 | ||
|
|
1234deabfa | ||
|
|
45306bbb6c | ||
|
|
18e2403b01 | ||
|
|
e578c5f3ad | ||
|
|
61245e3d7e | ||
|
|
7804182d0d | ||
|
|
f2195154f6 | ||
|
|
35f77f45a2 | ||
|
|
992c3a5d3a | ||
|
|
d51d7b6797 | ||
|
|
23ac2efd89 | ||
|
|
daeffb2dc6 | ||
|
|
db58ca6c1d | ||
|
|
2ff292cbfa | ||
|
|
5a81393863 | ||
|
|
116a73d398 | ||
|
|
cf0c057164 | ||
|
|
fe5a4f4447 | ||
|
|
c1b74201e4 | ||
|
|
27828d9ca8 | ||
|
|
2bd799fac6 | ||
|
|
9275f2d753 | ||
|
|
7455978ee5 | ||
|
|
7c0acc7b77 | ||
|
|
f32dd69acf | ||
|
|
80b8f956a9 | ||
|
|
caf50b6e6c | ||
|
|
b590d0857c | ||
|
|
982019307c | ||
|
|
09aec7b22e | ||
|
|
f9a047aad4 | ||
|
|
85704570f3 | ||
|
|
53dcae9e9c | ||
|
|
04e1ab63bb | ||
|
|
ed9aae531e | ||
|
|
6ab6b3dbca | ||
|
|
7180ed9a60 | ||
|
|
0a5522d28c | ||
|
|
c7bc93b32b | ||
|
|
d30351e7b0 | ||
|
|
886ffc0af8 | ||
|
|
4fdd997108 | ||
|
|
236736deea | ||
|
|
2b317f60c8 | ||
|
|
3ec67f9f47 | ||
|
|
6435e7a30e | ||
|
|
078305f5ac | ||
|
|
801b62543a | ||
|
|
877668b629 | ||
|
|
f652f73260 | ||
|
|
b2965e1deb | ||
|
|
2214689920 | ||
|
|
9326ff9d08 | ||
|
|
271f58d9cf | ||
|
|
cac99e3908 | ||
|
|
97a4a910e0 | ||
|
|
19c7a84548 | ||
|
|
571ce11e53 | ||
|
|
d2cb984ced | ||
|
|
7fc0d11931 | ||
|
|
341a52a615 | ||
|
|
d58b99d602 | ||
|
|
f7a5f836db | ||
|
|
d212df8b95 | ||
|
|
f3f6dc57c3 | ||
|
|
29b5cd9436 | ||
|
|
f5209fc344 | ||
|
|
b6bb1673d4 | ||
|
|
7b6fe66f2a | ||
|
|
c2fc0b4979 | ||
|
|
0b758941a4 | ||
|
|
492b55c893 | ||
|
|
4060e367ad | ||
|
|
c99cd31b6b | ||
|
|
718782f5b1 | ||
|
|
0c3fb5b2ce | ||
|
|
4ec6b067e7 | ||
|
|
1748dd6a3b | ||
|
|
95332e50ed | ||
|
|
8496980cf9 | ||
|
|
ffe32694b0 | ||
|
|
3d5b21154b | ||
|
|
4b9697e336 | ||
|
|
b0e9a542ba | ||
|
|
8b67536c23 | ||
|
|
cd49c12181 | ||
|
|
a6b14c7910 | ||
|
|
e275abdb9c | ||
|
|
09a90665d5 | ||
|
|
6649fbdfd0 | ||
|
|
64a0ffee7b | ||
|
|
b529118f31 | ||
|
|
39d7d9f13a | ||
|
|
fcd55df969 | ||
|
|
1e59948358 | ||
|
|
1102ef6e6b | ||
|
|
7ce2e8f4c4 | ||
|
|
fd1c656bdd | ||
|
|
82298a760a | ||
|
|
b84bb72e07 | ||
|
|
495b321d0a | ||
|
|
8a38cdc1d7 | ||
|
|
e210db7bd9 | ||
|
|
f64763b74b | ||
|
|
e033bef544 | ||
|
|
d88cc575e5 | ||
|
|
e3f499be0c | ||
|
|
87325fad74 | ||
|
|
86220573b6 | ||
|
|
65ed6b02a4 | ||
|
|
98093a1f31 | ||
|
|
00990dc195 | ||
|
|
122aa94c9f | ||
|
|
3da5284a07 | ||
|
|
cd920364f8 | ||
|
|
e2e8a45104 | ||
|
|
fb5fc13f72 | ||
|
|
edb92f7bfb | ||
|
|
1980f43b9f | ||
|
|
c3c3dd5154 | ||
|
|
356155fd30 | ||
|
|
6f75ef8f0a | ||
|
|
ca865a80dc | ||
|
|
8f759d1c3e | ||
|
|
44787637f2 | ||
|
|
cf1c8e8f2a | ||
|
|
d948be2372 | ||
|
|
cc28aef625 | ||
|
|
036358de7c | ||
|
|
0958b9ee12 | ||
|
|
aff1d7ecd6 | ||
|
|
42fdbd9bb8 | ||
|
|
034c82e514 | ||
|
|
14ff46b5cd | ||
|
|
c9099ca0a5 | ||
|
|
58b144b345 | ||
|
|
af21c57e77 | ||
|
|
624e4dbaaf | ||
|
|
9bbb4f396b | ||
|
|
b2c1644d69 | ||
|
|
5629f842da | ||
|
|
f900283b09 | ||
|
|
b667eff6bd | ||
|
|
54fdf40f5a | ||
|
|
690542145d | ||
|
|
94c4cf0624 | ||
|
|
3da717d9fc | ||
|
|
0902efc719 | ||
|
|
d7e2ee63d8 | ||
|
|
7deb36ee1f | ||
|
|
bfe4e88246 | ||
|
|
9ab45c3969 | ||
|
|
fec80c6c51 | ||
|
|
a6b7432358 | ||
|
|
3486954e07 | ||
|
|
150fc84b9b | ||
|
|
b023a00445 | ||
|
|
d0e296adf8 | ||
|
|
aa40015e9b | ||
|
|
141ce2c99a | ||
|
|
4a95dcb6e9 | ||
|
|
1610675c8f | ||
|
|
724c814bfe | ||
|
|
764c0cb865 | ||
|
|
8a4b8a84d6 | ||
|
|
8ec6acc55a | ||
|
|
b6a022b0ef | ||
|
|
716899c030 | ||
|
|
d9e407fd2b | ||
|
|
deb140de73 | ||
|
|
3c1e5e7978 | ||
|
|
4a8e85c28a | ||
|
|
8498cadae8 | ||
|
|
8c83fe23a1 | ||
|
|
a8c65e3d27 | ||
|
|
324d30bef9 | ||
|
|
46cb48023e | ||
|
|
1c24ca58c7 | ||
|
|
9193a9a0e0 | ||
|
|
957244ba2e | ||
|
|
ac599aa47c | ||
|
|
67a90ffb76 | ||
|
|
feaa6f9bf0 | ||
|
|
753bf3b924 | ||
|
|
b3219f57c8 | ||
|
|
a17df037af | ||
|
|
dfc36e5210 | ||
|
|
c359b92ddc | ||
|
|
e1d6131f13 | ||
|
|
6a0bda00f5 | ||
|
|
f85ec95877 | ||
|
|
a024980c03 | ||
|
|
fd9e94e078 | ||
|
|
f6a6c51d15 | ||
|
|
966db1e4be | ||
|
|
b8bbc37b8e | ||
|
|
40cbabc330 | ||
|
|
04a4e1b39a | ||
|
|
99f3160aa2 | ||
|
|
8cb72d8452 | ||
|
|
c9eb9f3eda | ||
|
|
64c3dcd732 | ||
|
|
d49ececcc5 | ||
|
|
90e1fadb1e | ||
|
|
071391ddff | ||
|
|
d70d46b4d5 | ||
|
|
3ef596b215 | ||
|
|
35c5518668 | ||
|
|
8b513537b7 | ||
|
|
b27f394995 | ||
|
|
3f9f556e1c | ||
|
|
1772d5c4b6 | ||
|
|
715d1dc02f | ||
|
|
6737f016f5 | ||
|
|
f2d2622172 | ||
|
|
72d6f97024 | ||
|
|
a0f0b4ff9e | ||
|
|
c27ef6ffbf | ||
|
|
f5499ff699 | ||
|
|
c4334d4e5f | ||
|
|
51e8f0440d | ||
|
|
5ec0311f84 | ||
|
|
556d563ba0 | ||
|
|
6a083b24c4 | ||
|
|
825929fdc8 | ||
|
|
941a03ed6c | ||
|
|
cf63619182 | ||
|
|
5c04d3c5ea | ||
|
|
46a47db2d8 | ||
|
|
21ef9a4567 | ||
|
|
6f0846b2af | ||
|
|
ecd78b3bdd | ||
|
|
d8afd1af88 | ||
|
|
7c1bc1f1a1 | ||
|
|
763fc89b29 | ||
|
|
47b33f2b17 | ||
|
|
9f0e16b045 | ||
|
|
2efedb1736 | ||
|
|
044116c14c | ||
|
|
b4bf11d648 | ||
|
|
6cc0a5a1a4 | ||
|
|
8f14de5108 | ||
|
|
8f6e5d73a2 | ||
|
|
ab9f5382b2 | ||
|
|
fd441d9303 | ||
|
|
e31bec3aff | ||
|
|
2a1c05a028 | ||
|
|
421bf33c0e | ||
|
|
9c7bacc65e | ||
|
|
3935c725c9 | ||
|
|
908ee0060f | ||
|
|
82e6fd7bb5 | ||
|
|
6b98b14179 | ||
|
|
1ecefd88f7 | ||
|
|
2e9e20ce7c | ||
|
|
fb60fbb217 | ||
|
|
4199e17da0 | ||
|
|
86b8bfcb1f | ||
|
|
dfd089132d | ||
|
|
3a10f58b28 | ||
|
|
9d55adbaf2 | ||
|
|
00be2be24f | ||
|
|
5b126c7e52 | ||
|
|
1943f3b53f | ||
|
|
4a0bef9afb | ||
|
|
dfd2a53129 | ||
|
|
aa4e855012 | ||
|
|
d6089e6309 | ||
|
|
038e6df8f0 | ||
|
|
2fd68bcac3 | ||
|
|
e468fecf12 | ||
|
|
fc31d8e5d1 | ||
|
|
115f357a07 | ||
|
|
ac04a1cac8 | ||
|
|
87a286ef07 | ||
|
|
622d8a4edb | ||
|
|
b44086f0dc | ||
|
|
0236e13187 | ||
|
|
a3d4a7253f | ||
|
|
e079f1b31a | ||
|
|
9a78a72eb3 | ||
|
|
862c2e8810 | ||
|
|
12cad4c418 | ||
|
|
89b9d3a7f7 | ||
|
|
57831d4880 | ||
|
|
052004d70e | ||
|
|
a765237441 | ||
|
|
ac470a6d07 | ||
|
|
7237d33be3 | ||
|
|
1610b480af | ||
|
|
287fa0a39c | ||
|
|
afa1a4303b | ||
|
|
28cedb1493 | ||
|
|
a280e25ee7 | ||
|
|
8464ca8931 | ||
|
|
44340f277d | ||
|
|
74bf99f9ea | ||
|
|
9caf820758 | ||
|
|
26c2598f56 | ||
|
|
6d9abf261c | ||
|
|
b16d0185dd | ||
|
|
ca51c2e93d | ||
|
|
da254975cd | ||
|
|
86bae6be3a | ||
|
|
fc8c7ef18d | ||
|
|
f654629c6a | ||
|
|
d4a87c561a | ||
|
|
d8872d48b3 | ||
|
|
ed16c2c18d | ||
|
|
0478a6ce3b | ||
|
|
68b60e82ba | ||
|
|
8edc0989e2 | ||
|
|
fbf3551bbe | ||
|
|
238d3122c4 | ||
|
|
ee22fba448 | ||
|
|
f8a2a28bff | ||
|
|
b3cfaf1420 | ||
|
|
2e9f701bb7 | ||
|
|
9aabc4ad6a | ||
|
|
5dc731bc77 | ||
|
|
32d05c9855 | ||
|
|
5a0d0c0b75 | ||
|
|
17d4a8fb26 | ||
|
|
348c1a7d5f | ||
|
|
49151dabf5 | ||
|
|
eb7c7cdcb6 | ||
|
|
ec95292209 | ||
|
|
4b84fb328c | ||
|
|
47d27c1f41 | ||
|
|
5267ad46da | ||
|
|
94bc880b7f | ||
|
|
bab3e0bc9b | ||
|
|
b3a324b6f5 | ||
|
|
a1117cd4ee | ||
|
|
6ece818d69 | ||
|
|
5df09d5e2a | ||
|
|
33450ce429 | ||
|
|
e2f0206d88 | ||
|
|
3767b2c7f9 | ||
|
|
1779f1f3da | ||
|
|
b9d1dca65d | ||
|
|
8e4d26163a | ||
|
|
53c1176cbf | ||
|
|
46d3e7884b | ||
|
|
a0290b0c1b | ||
|
|
b4ae706914 | ||
|
|
476bdac717 | ||
|
|
831627268d | ||
|
|
9b97dca601 | ||
|
|
4ea8c0802a | ||
|
|
9203870df5 | ||
|
|
e8088d6e38 | ||
|
|
59d9bcdd27 | ||
|
|
9d1b13ba73 | ||
|
|
dd1030139b | ||
|
|
30ca2117bb | ||
|
|
89024a8dc8 | ||
|
|
728c38396a | ||
|
|
d61cb98ac7 | ||
|
|
a7ceb61e27 | ||
|
|
74b915a790 | ||
|
|
01ea690421 | ||
|
|
17cc9284a0 | ||
|
|
498d0f0b8b | ||
|
|
89049e1a22 | ||
|
|
5e7254e8dc | ||
|
|
f8c2732fdc | ||
|
|
fec36eb298 | ||
|
|
2299a4156d | ||
|
|
32b82b9cb3 | ||
|
|
ba6039fc8b | ||
|
|
6885812d21 | ||
|
|
844025ec14 | ||
|
|
94bc91c554 | ||
|
|
044c16da4c | ||
|
|
cd4784c54a | ||
|
|
814aaa4a69 | ||
|
|
e3b3a4fefa | ||
|
|
3fcbb3010d | ||
|
|
7573a19dc9 | ||
|
|
3628d68d9a | ||
|
|
23872086fa | ||
|
|
bb349a03da | ||
|
|
82be426f78 | ||
|
|
9d2a633f5e | ||
|
|
1149d45589 | ||
|
|
9d7e19cebf | ||
|
|
b3023543d6 | ||
|
|
c229d2c3ce | ||
|
|
47ea383ddd | ||
|
|
f2a35f1114 | ||
|
|
147fc9a35a | ||
|
|
93a03f8fe4 | ||
|
|
230e3823a9 | ||
|
|
b14a0f24ae | ||
|
|
5295802720 | ||
|
|
fadd7f6eb4 | ||
|
|
011b76e4e7 | ||
|
|
f68cd2c5c0 | ||
|
|
6ac9789a1c | ||
|
|
2b0153807c | ||
|
|
34ab37f31e | ||
|
|
71af2628eb | ||
|
|
15f028abfb | ||
|
|
9bdd37bb63 | ||
|
|
d726c71141 | ||
|
|
1caa61f4c0 | ||
|
|
f3e3f08377 | ||
|
|
2ec8b7a804 | ||
|
|
9f7d137b05 | ||
|
|
7218f13783 | ||
|
|
fa31e7802c | ||
|
|
9b3b4494ba | ||
|
|
785d3748e1 | ||
|
|
5e0657ce55 | ||
|
|
700b06f9c5 | ||
|
|
b58bbf8eb4 | ||
|
|
2d1f522aaf | ||
|
|
0b2863dfab | ||
|
|
70907ead8a | ||
|
|
6dc4844c12 | ||
|
|
14bc1b6aac | ||
|
|
183ad2a34b | ||
|
|
d9758be3ae | ||
|
|
6b1b530443 | ||
|
|
1c20137b0e | ||
|
|
c4a6c933f8 | ||
|
|
31d9444264 | ||
|
|
8cb204e22e | ||
|
|
97aa72ec5b | ||
|
|
a68341eae6 | ||
|
|
aa08183439 | ||
|
|
7a5596b909 | ||
|
|
b9ffd50992 | ||
|
|
14f2a8f370 | ||
|
|
e7b16bfbc0 | ||
|
|
a16725ac17 | ||
|
|
2803a91673 | ||
|
|
cf2fce7666 | ||
|
|
1609abd166 | ||
|
|
88c74ae18d | ||
|
|
78e2b41e0c | ||
|
|
501f8b028b | ||
|
|
54401162bd | ||
|
|
7fde9327a2 | ||
|
|
bbbf59c74a | ||
|
|
c4ad66f745 | ||
|
|
69974d5651 | ||
|
|
ce3b6a04c2 | ||
|
|
37e2517dac | ||
|
|
d65ddead11 | ||
|
|
34034be0e3 | ||
|
|
d21481173e | ||
|
|
fa6ebadc7b | ||
|
|
a51fb24f36 | ||
|
|
c359b30763 | ||
|
|
95e3b156c0 | ||
|
|
b972a0d081 | ||
|
|
20749355da | ||
|
|
dad122199a | ||
|
|
9fb8fbcc65 | ||
|
|
78e7ea72dc | ||
|
|
4640060891 | ||
|
|
6efe4fb734 | ||
|
|
74986803db | ||
|
|
9b0a705055 | ||
|
|
163fc9e3a3 | ||
|
|
24bf7950d8 | ||
|
|
b6735bffe4 | ||
|
|
1d8fd480ca | ||
|
|
da2e2372aa | ||
|
|
f3b972e573 | ||
|
|
bf3bc3c7e9 | ||
|
|
38664487a0 | ||
|
|
de1111286c | ||
|
|
d89a12aa05 | ||
|
|
754acd7c26 | ||
|
|
c3e2f3b714 | ||
|
|
22ef3d3a46 | ||
|
|
7f3516f44f | ||
|
|
bfdb47a7ed | ||
|
|
01c9dbc1fd | ||
|
|
e58cb2b0db | ||
|
|
cf29a8f2c8 | ||
|
|
d411159124 | ||
|
|
5bf402710f | ||
|
|
2053db4cfc | ||
|
|
754303e7c7 |
@@ -1,21 +1,20 @@
|
|||||||
|
|
||||||
# Your openai api key. (required)
|
# Your openai api key. (required)
|
||||||
OPENAI_API_KEY=sk-xxxx
|
OPENAI_API_KEY=sk-xxxx
|
||||||
|
|
||||||
# Access password, separated by comma. (optional)
|
# Access password, separated by comma. (optional)
|
||||||
CODE=your-password
|
CODE=your-password
|
||||||
|
|
||||||
# You can start service behind a proxy
|
# You can start service behind a proxy. (optional)
|
||||||
PROXY_URL=http://localhost:7890
|
PROXY_URL=http://localhost:7890
|
||||||
|
|
||||||
# (optional)
|
# (optional)
|
||||||
# Default: Empty
|
# Default: Empty
|
||||||
# Googel Gemini Pro API key, set if you want to use Google Gemini Pro API.
|
# Google Gemini Pro API key, set if you want to use Google Gemini Pro API.
|
||||||
GOOGLE_API_KEY=
|
GOOGLE_API_KEY=
|
||||||
|
|
||||||
# (optional)
|
# (optional)
|
||||||
# Default: https://generativelanguage.googleapis.com/
|
# Default: https://generativelanguage.googleapis.com/
|
||||||
# Googel Gemini Pro API url without pathname, set if you want to customize Google Gemini Pro API url.
|
# Google Gemini Pro API url without pathname, set if you want to customize Google Gemini Pro API url.
|
||||||
GOOGLE_URL=
|
GOOGLE_URL=
|
||||||
|
|
||||||
# Override openai api request base url. (optional)
|
# Override openai api request base url. (optional)
|
||||||
@@ -47,6 +46,15 @@ ENABLE_BALANCE_QUERY=
|
|||||||
# If you want to disable parse settings from url, set this value to 1.
|
# If you want to disable parse settings from url, set this value to 1.
|
||||||
DISABLE_FAST_LINK=
|
DISABLE_FAST_LINK=
|
||||||
|
|
||||||
|
# (optional)
|
||||||
|
# Default: Empty
|
||||||
|
# To control custom models, use + to add a custom model, use - to hide a model, use name=displayName to customize model name, separated by comma.
|
||||||
|
CUSTOM_MODELS=
|
||||||
|
|
||||||
|
# (optional)
|
||||||
|
# Default: Empty
|
||||||
|
# Change default model
|
||||||
|
DEFAULT_MODEL=
|
||||||
|
|
||||||
# anthropic claude Api Key.(optional)
|
# anthropic claude Api Key.(optional)
|
||||||
ANTHROPIC_API_KEY=
|
ANTHROPIC_API_KEY=
|
||||||
@@ -54,8 +62,6 @@ ANTHROPIC_API_KEY=
|
|||||||
### anthropic claude Api version. (optional)
|
### anthropic claude Api version. (optional)
|
||||||
ANTHROPIC_API_VERSION=
|
ANTHROPIC_API_VERSION=
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### anthropic claude Api url (optional)
|
### anthropic claude Api url (optional)
|
||||||
ANTHROPIC_URL=
|
ANTHROPIC_URL=
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,4 @@
|
|||||||
{
|
{
|
||||||
"extends": "next/core-web-vitals",
|
"extends": "next/core-web-vitals",
|
||||||
"plugins": [
|
"plugins": ["prettier"]
|
||||||
"prettier"
|
|
||||||
],
|
|
||||||
"parserOptions": {
|
|
||||||
"ecmaFeatures": {
|
|
||||||
"legacyDecorators": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"ignorePatterns": ["globals.css"]
|
|
||||||
}
|
}
|
||||||
|
|||||||
80
.github/ISSUE_TEMPLATE/1_bug_report.yml
vendored
Normal file
80
.github/ISSUE_TEMPLATE/1_bug_report.yml
vendored
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
name: '🐛 Bug Report'
|
||||||
|
description: 'Report an bug'
|
||||||
|
title: '[Bug] '
|
||||||
|
labels: ['bug']
|
||||||
|
body:
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '📦 Deployment Method'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- 'Official installation package'
|
||||||
|
- 'Vercel'
|
||||||
|
- 'Zeabur'
|
||||||
|
- 'Sealos'
|
||||||
|
- 'Netlify'
|
||||||
|
- 'Docker'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 Version'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '💻 Operating System'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- 'Windows'
|
||||||
|
- 'macOS'
|
||||||
|
- 'Ubuntu'
|
||||||
|
- 'Other Linux'
|
||||||
|
- 'iOS'
|
||||||
|
- 'iPad OS'
|
||||||
|
- 'Android'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 System Version'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '🌐 Browser'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- 'Chrome'
|
||||||
|
- 'Edge'
|
||||||
|
- 'Safari'
|
||||||
|
- 'Firefox'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 Browser Version'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🐛 Bug Description'
|
||||||
|
description: A clear and concise description of the bug, if the above option is `Other`, please also explain in detail.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📷 Recurrence Steps'
|
||||||
|
description: A clear and concise description of how to recurrence.
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🚦 Expected Behavior'
|
||||||
|
description: A clear and concise description of what you expected to happen.
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📝 Additional Information'
|
||||||
|
description: If your problem needs further explanation, or if the issue you're seeing cannot be reproduced in a gist, please add more information here.
|
||||||
80
.github/ISSUE_TEMPLATE/1_bug_report_cn.yml
vendored
Normal file
80
.github/ISSUE_TEMPLATE/1_bug_report_cn.yml
vendored
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
name: '🐛 反馈缺陷'
|
||||||
|
description: '反馈一个问题/缺陷'
|
||||||
|
title: '[Bug] '
|
||||||
|
labels: ['bug']
|
||||||
|
body:
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '📦 部署方式'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- '官方安装包'
|
||||||
|
- 'Vercel'
|
||||||
|
- 'Zeabur'
|
||||||
|
- 'Sealos'
|
||||||
|
- 'Netlify'
|
||||||
|
- 'Docker'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 软件版本'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '💻 系统环境'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- 'Windows'
|
||||||
|
- 'macOS'
|
||||||
|
- 'Ubuntu'
|
||||||
|
- 'Other Linux'
|
||||||
|
- 'iOS'
|
||||||
|
- 'iPad OS'
|
||||||
|
- 'Android'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 系统版本'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: '🌐 浏览器'
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- 'Chrome'
|
||||||
|
- 'Edge'
|
||||||
|
- 'Safari'
|
||||||
|
- 'Firefox'
|
||||||
|
- 'Other'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: '📌 浏览器版本'
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🐛 问题描述'
|
||||||
|
description: 请提供一个清晰且简洁的问题描述,若上述选项为`Other`,也请详细说明。
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📷 复现步骤'
|
||||||
|
description: 请提供一个清晰且简洁的描述,说明如何复现问题。
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🚦 期望结果'
|
||||||
|
description: 请提供一个清晰且简洁的描述,说明您期望发生什么。
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📝 补充信息'
|
||||||
|
description: 如果您的问题需要进一步说明,或者您遇到的问题无法在一个简单的示例中复现,请在这里添加更多信息。
|
||||||
21
.github/ISSUE_TEMPLATE/2_feature_request.yml
vendored
Normal file
21
.github/ISSUE_TEMPLATE/2_feature_request.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: '🌠 Feature Request'
|
||||||
|
description: 'Suggest an idea'
|
||||||
|
title: '[Feature Request] '
|
||||||
|
labels: ['enhancement']
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🥰 Feature Description'
|
||||||
|
description: Please add a clear and concise description of the problem you are seeking to solve with this feature request.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🧐 Proposed Solution'
|
||||||
|
description: Describe the solution you'd like in a clear and concise manner.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📝 Additional Information'
|
||||||
|
description: Add any other context about the problem here.
|
||||||
21
.github/ISSUE_TEMPLATE/2_feature_request_cn.yml
vendored
Normal file
21
.github/ISSUE_TEMPLATE/2_feature_request_cn.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: '🌠 功能需求'
|
||||||
|
description: '提出需求或建议'
|
||||||
|
title: '[Feature Request] '
|
||||||
|
labels: ['enhancement']
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🥰 需求描述'
|
||||||
|
description: 请添加一个清晰且简洁的问题描述,阐述您希望通过这个功能需求解决的问题。
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '🧐 解决方案'
|
||||||
|
description: 请清晰且简洁地描述您想要的解决方案。
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: '📝 补充信息'
|
||||||
|
description: 在这里添加关于问题的任何其他背景信息。
|
||||||
146
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
146
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -1,146 +0,0 @@
|
|||||||
name: Bug report
|
|
||||||
description: Create a report to help us improve
|
|
||||||
title: "[Bug] "
|
|
||||||
labels: ["bug"]
|
|
||||||
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Describe the bug"
|
|
||||||
- type: textarea
|
|
||||||
id: bug-description
|
|
||||||
attributes:
|
|
||||||
label: "Bug Description"
|
|
||||||
description: "A clear and concise description of what the bug is."
|
|
||||||
placeholder: "Explain the bug..."
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## To Reproduce"
|
|
||||||
- type: textarea
|
|
||||||
id: steps-to-reproduce
|
|
||||||
attributes:
|
|
||||||
label: "Steps to Reproduce"
|
|
||||||
description: "Steps to reproduce the behavior:"
|
|
||||||
placeholder: |
|
|
||||||
1. Go to '...'
|
|
||||||
2. Click on '....'
|
|
||||||
3. Scroll down to '....'
|
|
||||||
4. See error
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Expected behavior"
|
|
||||||
- type: textarea
|
|
||||||
id: expected-behavior
|
|
||||||
attributes:
|
|
||||||
label: "Expected Behavior"
|
|
||||||
description: "A clear and concise description of what you expected to happen."
|
|
||||||
placeholder: "Describe what you expected to happen..."
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Screenshots"
|
|
||||||
- type: textarea
|
|
||||||
id: screenshots
|
|
||||||
attributes:
|
|
||||||
label: "Screenshots"
|
|
||||||
description: "If applicable, add screenshots to help explain your problem."
|
|
||||||
placeholder: "Paste your screenshots here or write 'N/A' if not applicable..."
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Deployment"
|
|
||||||
- type: checkboxes
|
|
||||||
id: deployment
|
|
||||||
attributes:
|
|
||||||
label: "Deployment Method"
|
|
||||||
description: "Please select the deployment method you are using."
|
|
||||||
options:
|
|
||||||
- label: "Docker"
|
|
||||||
- label: "Vercel"
|
|
||||||
- label: "Server"
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Desktop (please complete the following information):"
|
|
||||||
- type: input
|
|
||||||
id: desktop-os
|
|
||||||
attributes:
|
|
||||||
label: "Desktop OS"
|
|
||||||
description: "Your desktop operating system."
|
|
||||||
placeholder: "e.g., Windows 10"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
- type: input
|
|
||||||
id: desktop-browser
|
|
||||||
attributes:
|
|
||||||
label: "Desktop Browser"
|
|
||||||
description: "Your desktop browser."
|
|
||||||
placeholder: "e.g., Chrome, Safari"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
- type: input
|
|
||||||
id: desktop-version
|
|
||||||
attributes:
|
|
||||||
label: "Desktop Browser Version"
|
|
||||||
description: "Version of your desktop browser."
|
|
||||||
placeholder: "e.g., 89.0"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Smartphone (please complete the following information):"
|
|
||||||
- type: input
|
|
||||||
id: smartphone-device
|
|
||||||
attributes:
|
|
||||||
label: "Smartphone Device"
|
|
||||||
description: "Your smartphone device."
|
|
||||||
placeholder: "e.g., iPhone X"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
- type: input
|
|
||||||
id: smartphone-os
|
|
||||||
attributes:
|
|
||||||
label: "Smartphone OS"
|
|
||||||
description: "Your smartphone operating system."
|
|
||||||
placeholder: "e.g., iOS 14.4"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
- type: input
|
|
||||||
id: smartphone-browser
|
|
||||||
attributes:
|
|
||||||
label: "Smartphone Browser"
|
|
||||||
description: "Your smartphone browser."
|
|
||||||
placeholder: "e.g., Safari"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
- type: input
|
|
||||||
id: smartphone-version
|
|
||||||
attributes:
|
|
||||||
label: "Smartphone Browser Version"
|
|
||||||
description: "Version of your smartphone browser."
|
|
||||||
placeholder: "e.g., 14"
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Additional Logs"
|
|
||||||
- type: textarea
|
|
||||||
id: additional-logs
|
|
||||||
attributes:
|
|
||||||
label: "Additional Logs"
|
|
||||||
description: "Add any logs about the problem here."
|
|
||||||
placeholder: "Paste any relevant logs here..."
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
53
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
53
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
@@ -1,53 +0,0 @@
|
|||||||
name: Feature request
|
|
||||||
description: Suggest an idea for this project
|
|
||||||
title: "[Feature Request]: "
|
|
||||||
labels: ["enhancement"]
|
|
||||||
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Is your feature request related to a problem? Please describe."
|
|
||||||
- type: textarea
|
|
||||||
id: problem-description
|
|
||||||
attributes:
|
|
||||||
label: Problem Description
|
|
||||||
description: "A clear and concise description of what the problem is. Example: I'm always frustrated when [...]"
|
|
||||||
placeholder: "Explain the problem you are facing..."
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Describe the solution you'd like"
|
|
||||||
- type: textarea
|
|
||||||
id: desired-solution
|
|
||||||
attributes:
|
|
||||||
label: Solution Description
|
|
||||||
description: A clear and concise description of what you want to happen.
|
|
||||||
placeholder: "Describe the solution you'd like..."
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Describe alternatives you've considered"
|
|
||||||
- type: textarea
|
|
||||||
id: alternatives-considered
|
|
||||||
attributes:
|
|
||||||
label: Alternatives Considered
|
|
||||||
description: A clear and concise description of any alternative solutions or features you've considered.
|
|
||||||
placeholder: "Describe any alternative solutions or features you've considered..."
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: "## Additional context"
|
|
||||||
- type: textarea
|
|
||||||
id: additional-context
|
|
||||||
attributes:
|
|
||||||
label: Additional Context
|
|
||||||
description: Add any other context or screenshots about the feature request here.
|
|
||||||
placeholder: "Add any other context or screenshots about the feature request here..."
|
|
||||||
validations:
|
|
||||||
required: false
|
|
||||||
28
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
28
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#### 💻 变更类型 | Change Type
|
||||||
|
|
||||||
|
<!-- For change type, change [ ] to [x]. -->
|
||||||
|
|
||||||
|
- [ ] feat <!-- 引入新功能 | Introduce new features -->
|
||||||
|
- [ ] fix <!-- 修复 Bug | Fix a bug -->
|
||||||
|
- [ ] refactor <!-- 重构代码(既不修复 Bug 也不添加新功能) | Refactor code that neither fixes a bug nor adds a feature -->
|
||||||
|
- [ ] perf <!-- 提升性能的代码变更 | A code change that improves performance -->
|
||||||
|
- [ ] style <!-- 添加或更新不影响代码含义的样式文件 | Add or update style files that do not affect the meaning of the code -->
|
||||||
|
- [ ] test <!-- 添加缺失的测试或纠正现有的测试 | Adding missing tests or correcting existing tests -->
|
||||||
|
- [ ] docs <!-- 仅文档更新 | Documentation only changes -->
|
||||||
|
- [ ] ci <!-- 修改持续集成配置文件和脚本 | Changes to our CI configuration files and scripts -->
|
||||||
|
- [ ] chore <!-- 其他不修改 src 或 test 文件的变更 | Other changes that don’t modify src or test files -->
|
||||||
|
- [ ] build <!-- 进行架构变更 | Make architectural changes -->
|
||||||
|
|
||||||
|
#### 🔀 变更说明 | Description of Change
|
||||||
|
|
||||||
|
<!--
|
||||||
|
感谢您的 Pull Request ,请提供此 Pull Request 的变更说明
|
||||||
|
Thank you for your Pull Request. Please provide a description above.
|
||||||
|
-->
|
||||||
|
|
||||||
|
#### 📝 补充信息 | Additional Information
|
||||||
|
|
||||||
|
<!--
|
||||||
|
请添加与此 Pull Request 相关的补充信息
|
||||||
|
Add any other context about the Pull Request here.
|
||||||
|
-->
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -44,3 +44,5 @@ dev
|
|||||||
|
|
||||||
*.key
|
*.key
|
||||||
*.key.pub
|
*.key.pub
|
||||||
|
|
||||||
|
masks.json
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ COPY --from=builder /app/.next/server ./.next/server
|
|||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
CMD if [ -n "$PROXY_URL" ]; then \
|
CMD if [ -n "$PROXY_URL" ]; then \
|
||||||
export HOSTNAME="127.0.0.1"; \
|
export HOSTNAME="0.0.0.0"; \
|
||||||
protocol=$(echo $PROXY_URL | cut -d: -f1); \
|
protocol=$(echo $PROXY_URL | cut -d: -f1); \
|
||||||
host=$(echo $PROXY_URL | cut -d/ -f3 | cut -d: -f1); \
|
host=$(echo $PROXY_URL | cut -d/ -f3 | cut -d: -f1); \
|
||||||
port=$(echo $PROXY_URL | cut -d: -f3); \
|
port=$(echo $PROXY_URL | cut -d: -f3); \
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2023 Zhang Yifei
|
Copyright (c) 2023-2024 Zhang Yifei
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
135
README.md
135
README.md
@@ -1,5 +1,8 @@
|
|||||||
<div align="center">
|
<div align="center">
|
||||||
<img src="./docs/images/head-cover.png" alt="icon"/>
|
|
||||||
|
<a href='#企业版'>
|
||||||
|
<img src="./docs/images/ent.svg" alt="icon"/>
|
||||||
|
</a>
|
||||||
|
|
||||||
<h1 align="center">NextChat (ChatGPT Next Web)</h1>
|
<h1 align="center">NextChat (ChatGPT Next Web)</h1>
|
||||||
|
|
||||||
@@ -14,27 +17,51 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
|
|||||||
[![MacOS][MacOS-image]][download-url]
|
[![MacOS][MacOS-image]][download-url]
|
||||||
[![Linux][Linux-image]][download-url]
|
[![Linux][Linux-image]][download-url]
|
||||||
|
|
||||||
[Web App](https://app.nextchat.dev/) / [Desktop App](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [Discord](https://discord.gg/YCkeafCafC) / [Twitter](https://twitter.com/NextChatDev)
|
[Web App](https://app.nextchat.dev/) / [Desktop App](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [Discord](https://discord.gg/YCkeafCafC) / [Enterprise Edition](#enterprise-edition) / [Twitter](https://twitter.com/NextChatDev)
|
||||||
|
|
||||||
[网页版](https://app.nextchat.dev/) / [客户端](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [反馈](https://github.com/Yidadaa/ChatGPT-Next-Web/issues)
|
[网页版](https://app.nextchat.dev/) / [客户端](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) / [反馈](https://github.com/Yidadaa/ChatGPT-Next-Web/issues)
|
||||||
|
|
||||||
[web-url]: https://chatgpt.nextweb.fun
|
[web-url]: https://app.nextchat.dev/
|
||||||
[download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases
|
[download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases
|
||||||
[Web-image]: https://img.shields.io/badge/Web-PWA-orange?logo=microsoftedge
|
[Web-image]: https://img.shields.io/badge/Web-PWA-orange?logo=microsoftedge
|
||||||
[Windows-image]: https://img.shields.io/badge/-Windows-blue?logo=windows
|
[Windows-image]: https://img.shields.io/badge/-Windows-blue?logo=windows
|
||||||
[MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple
|
[MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple
|
||||||
[Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu
|
[Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu
|
||||||
|
|
||||||
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat)
|
[<img src="https://vercel.com/button" alt="Deploy on Zeabur" height="30">](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [<img src="https://zeabur.com/button.svg" alt="Deploy on Zeabur" height="30">](https://zeabur.com/templates/ZBUEFA) [<img src="https://gitpod.io/button/open-in-gitpod.svg" alt="Open in Gitpod" height="30">](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
||||||
|
|
||||||
[](https://zeabur.com/templates/ZBUEFA)
|
[<img src="https://github.com/user-attachments/assets/903482d4-3e87-4134-9af1-f2588fa90659" height="60" width="288" >](https://monica.im/?utm=nxcrp)
|
||||||
|
|
||||||
[](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
## Enterprise Edition
|
||||||
|
|
||||||
|
Meeting Your Company's Privatization and Customization Deployment Requirements:
|
||||||
|
- **Brand Customization**: Tailored VI/UI to seamlessly align with your corporate brand image.
|
||||||
|
- **Resource Integration**: Unified configuration and management of dozens of AI resources by company administrators, ready for use by team members.
|
||||||
|
- **Permission Control**: Clearly defined member permissions, resource permissions, and knowledge base permissions, all controlled via a corporate-grade Admin Panel.
|
||||||
|
- **Knowledge Integration**: Combining your internal knowledge base with AI capabilities, making it more relevant to your company's specific business needs compared to general AI.
|
||||||
|
- **Security Auditing**: Automatically intercept sensitive inquiries and trace all historical conversation records, ensuring AI adherence to corporate information security standards.
|
||||||
|
- **Private Deployment**: Enterprise-level private deployment supporting various mainstream private cloud solutions, ensuring data security and privacy protection.
|
||||||
|
- **Continuous Updates**: Ongoing updates and upgrades in cutting-edge capabilities like multimodal AI, ensuring consistent innovation and advancement.
|
||||||
|
|
||||||
|
For enterprise inquiries, please contact: **business@nextchat.dev**
|
||||||
|
|
||||||
|
## 企业版
|
||||||
|
|
||||||
|
满足企业用户私有化部署和个性化定制需求:
|
||||||
|
- **品牌定制**:企业量身定制 VI/UI,与企业品牌形象无缝契合
|
||||||
|
- **资源集成**:由企业管理人员统一配置和管理数十种 AI 资源,团队成员开箱即用
|
||||||
|
- **权限管理**:成员权限、资源权限、知识库权限层级分明,企业级 Admin Panel 统一控制
|
||||||
|
- **知识接入**:企业内部知识库与 AI 能力相结合,比通用 AI 更贴近企业自身业务需求
|
||||||
|
- **安全审计**:自动拦截敏感提问,支持追溯全部历史对话记录,让 AI 也能遵循企业信息安全规范
|
||||||
|
- **私有部署**:企业级私有部署,支持各类主流私有云部署,确保数据安全和隐私保护
|
||||||
|
- **持续更新**:提供多模态、智能体等前沿能力持续更新升级服务,常用常新、持续先进
|
||||||
|
|
||||||
|
企业版咨询: **business@nextchat.dev**
|
||||||
|
|
||||||
|
<img width="300" src="https://github.com/user-attachments/assets/3daeb7b6-ab63-4542-9141-2e4a12c80601">
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Deploy for free with one-click** on Vercel in under 1 minute
|
- **Deploy for free with one-click** on Vercel in under 1 minute
|
||||||
@@ -49,6 +76,12 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
|
|||||||
- Automatically compresses chat history to support long conversations while also saving your tokens
|
- Automatically compresses chat history to support long conversations while also saving your tokens
|
||||||
- I18n: English, 简体中文, 繁体中文, 日本語, Français, Español, Italiano, Türkçe, Deutsch, Tiếng Việt, Русский, Čeština, 한국어, Indonesia
|
- I18n: English, 简体中文, 繁体中文, 日本語, Français, Español, Italiano, Türkçe, Deutsch, Tiếng Việt, Русский, Čeština, 한국어, Indonesia
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
- [x] System Prompt: pin a user defined prompt as system prompt [#138](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138)
|
- [x] System Prompt: pin a user defined prompt as system prompt [#138](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138)
|
||||||
@@ -57,10 +90,15 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
|
|||||||
- [x] Share as image, share to ShareGPT [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741)
|
- [x] Share as image, share to ShareGPT [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741)
|
||||||
- [x] Desktop App with tauri
|
- [x] Desktop App with tauri
|
||||||
- [x] Self-host Model: Fully compatible with [RWKV-Runner](https://github.com/josStorer/RWKV-Runner), as well as server deployment of [LocalAI](https://github.com/go-skynet/LocalAI): llama/gpt4all/rwkv/vicuna/koala/gpt4all-j/cerebras/falcon/dolly etc.
|
- [x] Self-host Model: Fully compatible with [RWKV-Runner](https://github.com/josStorer/RWKV-Runner), as well as server deployment of [LocalAI](https://github.com/go-skynet/LocalAI): llama/gpt4all/rwkv/vicuna/koala/gpt4all-j/cerebras/falcon/dolly etc.
|
||||||
- [ ] Plugins: support network search, calculator, any other apis etc. [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165)
|
- [x] Artifacts: Easily preview, copy and share generated content/webpages through a separate window [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092)
|
||||||
|
- [x] Plugins: support network search, calculator, any other apis etc. [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353)
|
||||||
|
- [x] network search, calculator, any other apis etc. [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353)
|
||||||
|
- [ ] local knowledge base
|
||||||
|
|
||||||
## What's New
|
## What's New
|
||||||
|
|
||||||
|
- 🚀 v2.15.0 Now supports Plugins! Read this: [NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins)
|
||||||
|
- 🚀 v2.14.0 Now supports Artifacts & SD
|
||||||
- 🚀 v2.10.1 support Google Gemini Pro model.
|
- 🚀 v2.10.1 support Google Gemini Pro model.
|
||||||
- 🚀 v2.9.11 you can use azure endpoint now.
|
- 🚀 v2.9.11 you can use azure endpoint now.
|
||||||
- 🚀 v2.8 now we have a client that runs across all platforms!
|
- 🚀 v2.8 now we have a client that runs across all platforms!
|
||||||
@@ -89,15 +127,21 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
|
|||||||
- [x] 分享为图片,分享到 ShareGPT 链接 [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741)
|
- [x] 分享为图片,分享到 ShareGPT 链接 [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741)
|
||||||
- [x] 使用 tauri 打包桌面应用
|
- [x] 使用 tauri 打包桌面应用
|
||||||
- [x] 支持自部署的大语言模型:开箱即用 [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) ,服务端部署 [LocalAI 项目](https://github.com/go-skynet/LocalAI) llama / gpt4all / rwkv / vicuna / koala / gpt4all-j / cerebras / falcon / dolly 等等,或者使用 [api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm)
|
- [x] 支持自部署的大语言模型:开箱即用 [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) ,服务端部署 [LocalAI 项目](https://github.com/go-skynet/LocalAI) llama / gpt4all / rwkv / vicuna / koala / gpt4all-j / cerebras / falcon / dolly 等等,或者使用 [api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm)
|
||||||
- [ ] 插件机制,支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165)
|
- [x] Artifacts: 通过独立窗口,轻松预览、复制和分享生成的内容/可交互网页 [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092)
|
||||||
|
- [x] 插件机制,支持`联网搜索`、`计算器`、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353)
|
||||||
|
- [x] 支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353)
|
||||||
|
- [ ] 本地知识库
|
||||||
|
|
||||||
## 最新动态
|
## 最新动态
|
||||||
|
|
||||||
|
- 🚀 v2.15.0 现在支持插件功能了!了解更多:[NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins)
|
||||||
|
- 🚀 v2.14.0 现在支持 Artifacts & SD 了。
|
||||||
|
- 🚀 v2.10.1 现在支持 Gemini Pro 模型。
|
||||||
|
- 🚀 v2.9.11 现在可以使用自定义 Azure 服务了。
|
||||||
|
- 🚀 v2.8 发布了横跨 Linux/Windows/MacOS 的体积极小的客户端。
|
||||||
|
- 🚀 v2.7 现在可以将会话分享为图片了,也可以分享到 ShareGPT 的在线链接。
|
||||||
- 🚀 v2.0 已经发布,现在你可以使用面具功能快速创建预制对话了! 了解更多: [ChatGPT 提示词高阶技能:零次、一次和少样本提示](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138)。
|
- 🚀 v2.0 已经发布,现在你可以使用面具功能快速创建预制对话了! 了解更多: [ChatGPT 提示词高阶技能:零次、一次和少样本提示](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138)。
|
||||||
- 💡 想要更方便地随时随地使用本项目?可以试下这款桌面插件:https://github.com/mushan0x0/AI0x0.com
|
- 💡 想要更方便地随时随地使用本项目?可以试下这款桌面插件:https://github.com/mushan0x0/AI0x0.com
|
||||||
- 🚀 v2.7 现在可以将会话分享为图片了,也可以分享到 ShareGPT 的在线链接。
|
|
||||||
- 🚀 v2.8 发布了横跨 Linux/Windows/MacOS 的体积极小的客户端。
|
|
||||||
- 🚀 v2.9.11 现在可以使用自定义 Azure 服务了。
|
|
||||||
|
|
||||||
## Get Started
|
## Get Started
|
||||||
|
|
||||||
@@ -180,7 +224,7 @@ Specify OpenAI organization ID.
|
|||||||
|
|
||||||
### `AZURE_URL` (optional)
|
### `AZURE_URL` (optional)
|
||||||
|
|
||||||
> Example: https://{azure-resource-url}/openai/deployments/{deploy-name}
|
> Example: https://{azure-resource-url}/openai
|
||||||
|
|
||||||
Azure deploy url.
|
Azure deploy url.
|
||||||
|
|
||||||
@@ -212,6 +256,46 @@ anthropic claude Api version.
|
|||||||
|
|
||||||
anthropic claude Api Url.
|
anthropic claude Api Url.
|
||||||
|
|
||||||
|
### `BAIDU_API_KEY` (optional)
|
||||||
|
|
||||||
|
Baidu Api Key.
|
||||||
|
|
||||||
|
### `BAIDU_SECRET_KEY` (optional)
|
||||||
|
|
||||||
|
Baidu Secret Key.
|
||||||
|
|
||||||
|
### `BAIDU_URL` (optional)
|
||||||
|
|
||||||
|
Baidu Api Url.
|
||||||
|
|
||||||
|
### `BYTEDANCE_API_KEY` (optional)
|
||||||
|
|
||||||
|
ByteDance Api Key.
|
||||||
|
|
||||||
|
### `BYTEDANCE_URL` (optional)
|
||||||
|
|
||||||
|
ByteDance Api Url.
|
||||||
|
|
||||||
|
### `ALIBABA_API_KEY` (optional)
|
||||||
|
|
||||||
|
Alibaba Cloud Api Key.
|
||||||
|
|
||||||
|
### `ALIBABA_URL` (optional)
|
||||||
|
|
||||||
|
Alibaba Cloud Api Url.
|
||||||
|
|
||||||
|
### `IFLYTEK_URL` (Optional)
|
||||||
|
|
||||||
|
iflytek Api Url.
|
||||||
|
|
||||||
|
### `IFLYTEK_API_KEY` (Optional)
|
||||||
|
|
||||||
|
iflytek Api Key.
|
||||||
|
|
||||||
|
### `IFLYTEK_API_SECRET` (Optional)
|
||||||
|
|
||||||
|
iflytek Api Secret.
|
||||||
|
|
||||||
### `HIDE_USER_API_KEY` (optional)
|
### `HIDE_USER_API_KEY` (optional)
|
||||||
|
|
||||||
> Default: Empty
|
> Default: Empty
|
||||||
@@ -245,6 +329,17 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model
|
|||||||
|
|
||||||
User `-all` to disable all default models, `+all` to enable all default models.
|
User `-all` to disable all default models, `+all` to enable all default models.
|
||||||
|
|
||||||
|
For Azure: use `modelName@azure=deploymentName` to customize model name and deployment name.
|
||||||
|
> Example: `+gpt-3.5-turbo@azure=gpt35` will show option `gpt35(Azure)` in model list.
|
||||||
|
> If you only can use Azure model, `-all,+gpt-3.5-turbo@azure=gpt35` will `gpt35(Azure)` the only option in model list.
|
||||||
|
|
||||||
|
For ByteDance: use `modelName@bytedance=deploymentName` to customize model name and deployment name.
|
||||||
|
> Example: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` will show option `Doubao-lite-4k(ByteDance)` in model list.
|
||||||
|
|
||||||
|
### `DEFAULT_MODEL` (optional)
|
||||||
|
|
||||||
|
Change default model
|
||||||
|
|
||||||
### `WHITE_WEBDEV_ENDPOINTS` (optional)
|
### `WHITE_WEBDEV_ENDPOINTS` (optional)
|
||||||
|
|
||||||
You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format:
|
You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format:
|
||||||
@@ -256,6 +351,14 @@ You can use this option if you want to increase the number of webdav service add
|
|||||||
|
|
||||||
Customize the default template used to initialize the User Input Preprocessing configuration item in Settings.
|
Customize the default template used to initialize the User Input Preprocessing configuration item in Settings.
|
||||||
|
|
||||||
|
### `STABILITY_API_KEY` (optional)
|
||||||
|
|
||||||
|
Stability API key.
|
||||||
|
|
||||||
|
### `STABILITY_URL` (optional)
|
||||||
|
|
||||||
|
Customize Stability API url.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
NodeJS >= 18, Docker >= 20
|
NodeJS >= 18, Docker >= 20
|
||||||
|
|||||||
112
README_CN.md
112
README_CN.md
@@ -1,22 +1,34 @@
|
|||||||
<div align="center">
|
<div align="center">
|
||||||
<img src="./docs/images/icon.svg" alt="预览"/>
|
|
||||||
|
<a href='#企业版'>
|
||||||
|
<img src="./docs/images/ent.svg" alt="icon"/>
|
||||||
|
</a>
|
||||||
|
|
||||||
<h1 align="center">NextChat</h1>
|
<h1 align="center">NextChat</h1>
|
||||||
|
|
||||||
一键免费部署你的私人 ChatGPT 网页应用,支持 GPT3, GPT4 & Gemini Pro 模型。
|
一键免费部署你的私人 ChatGPT 网页应用,支持 GPT3, GPT4 & Gemini Pro 模型。
|
||||||
|
|
||||||
[演示 Demo](https://chat-gpt-next-web.vercel.app/) / [反馈 Issues](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [加入 Discord](https://discord.gg/zrhvHCr79N)
|
[企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) /[演示 Demo](https://chat-gpt-next-web.vercel.app/) / [反馈 Issues](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [加入 Discord](https://discord.gg/zrhvHCr79N)
|
||||||
|
|
||||||
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web)
|
[<img src="https://vercel.com/button" alt="Deploy on Zeabur" height="30">](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [<img src="https://zeabur.com/button.svg" alt="Deploy on Zeabur" height="30">](https://zeabur.com/templates/ZBUEFA) [<img src="https://gitpod.io/button/open-in-gitpod.svg" alt="Open in Gitpod" height="30">](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
||||||
|
|
||||||
[](https://zeabur.com/templates/ZBUEFA)
|
|
||||||
|
|
||||||
[](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
## 企业版
|
||||||
|
|
||||||
|
满足您公司私有化部署和定制需求
|
||||||
|
- **品牌定制**:企业量身定制 VI/UI,与企业品牌形象无缝契合
|
||||||
|
- **资源集成**:由企业管理人员统一配置和管理数十种 AI 资源,团队成员开箱即用
|
||||||
|
- **权限管理**:成员权限、资源权限、知识库权限层级分明,企业级 Admin Panel 统一控制
|
||||||
|
- **知识接入**:企业内部知识库与 AI 能力相结合,比通用 AI 更贴近企业自身业务需求
|
||||||
|
- **安全审计**:自动拦截敏感提问,支持追溯全部历史对话记录,让 AI 也能遵循企业信息安全规范
|
||||||
|
- **私有部署**:企业级私有部署,支持各类主流私有云部署,确保数据安全和隐私保护
|
||||||
|
- **持续更新**:提供多模态、智能体等前沿能力持续更新升级服务,常用常新、持续先进
|
||||||
|
|
||||||
|
企业版咨询: **business@nextchat.dev**
|
||||||
|
|
||||||
|
<img width="300" src="https://github.com/user-attachments/assets/3daeb7b6-ab63-4542-9141-2e4a12c80601">
|
||||||
|
|
||||||
## 开始使用
|
## 开始使用
|
||||||
|
|
||||||
1. 准备好你的 [OpenAI API Key](https://platform.openai.com/account/api-keys);
|
1. 准备好你的 [OpenAI API Key](https://platform.openai.com/account/api-keys);
|
||||||
@@ -25,6 +37,12 @@
|
|||||||
3. 部署完毕后,即可开始使用;
|
3. 部署完毕后,即可开始使用;
|
||||||
4. (可选)[绑定自定义域名](https://vercel.com/docs/concepts/projects/domains/add-a-domain):Vercel 分配的域名 DNS 在某些区域被污染了,绑定自定义域名即可直连。
|
4. (可选)[绑定自定义域名](https://vercel.com/docs/concepts/projects/domains/add-a-domain):Vercel 分配的域名 DNS 在某些区域被污染了,绑定自定义域名即可直连。
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
## 保持更新
|
## 保持更新
|
||||||
|
|
||||||
如果你按照上述步骤一键部署了自己的项目,可能会发现总是提示“存在更新”的问题,这是由于 Vercel 会默认为你创建一个新项目而不是 fork 本项目,这会导致无法正确地检测更新。
|
如果你按照上述步骤一键部署了自己的项目,可能会发现总是提示“存在更新”的问题,这是由于 Vercel 会默认为你创建一个新项目而不是 fork 本项目,这会导致无法正确地检测更新。
|
||||||
@@ -94,7 +112,7 @@ OpenAI 接口代理 URL,如果你手动配置了 openai 接口代理,请填
|
|||||||
|
|
||||||
### `AZURE_URL` (可选)
|
### `AZURE_URL` (可选)
|
||||||
|
|
||||||
> 形如:https://{azure-resource-url}/openai/deployments/{deploy-name}
|
> 形如:https://{azure-resource-url}/openai
|
||||||
|
|
||||||
Azure 部署地址。
|
Azure 部署地址。
|
||||||
|
|
||||||
@@ -106,26 +124,68 @@ Azure 密钥。
|
|||||||
|
|
||||||
Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)。
|
Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)。
|
||||||
|
|
||||||
### `GOOGLE_API_KEY` (optional)
|
### `GOOGLE_API_KEY` (可选)
|
||||||
|
|
||||||
Google Gemini Pro 密钥.
|
Google Gemini Pro 密钥.
|
||||||
|
|
||||||
### `GOOGLE_URL` (optional)
|
### `GOOGLE_URL` (可选)
|
||||||
|
|
||||||
Google Gemini Pro Api Url.
|
Google Gemini Pro Api Url.
|
||||||
|
|
||||||
### `ANTHROPIC_API_KEY` (optional)
|
### `ANTHROPIC_API_KEY` (可选)
|
||||||
|
|
||||||
anthropic claude Api Key.
|
anthropic claude Api Key.
|
||||||
|
|
||||||
### `ANTHROPIC_API_VERSION` (optional)
|
### `ANTHROPIC_API_VERSION` (可选)
|
||||||
|
|
||||||
anthropic claude Api version.
|
anthropic claude Api version.
|
||||||
|
|
||||||
### `ANTHROPIC_URL` (optional)
|
### `ANTHROPIC_URL` (可选)
|
||||||
|
|
||||||
anthropic claude Api Url.
|
anthropic claude Api Url.
|
||||||
|
|
||||||
|
### `BAIDU_API_KEY` (可选)
|
||||||
|
|
||||||
|
Baidu Api Key.
|
||||||
|
|
||||||
|
### `BAIDU_SECRET_KEY` (可选)
|
||||||
|
|
||||||
|
Baidu Secret Key.
|
||||||
|
|
||||||
|
### `BAIDU_URL` (可选)
|
||||||
|
|
||||||
|
Baidu Api Url.
|
||||||
|
|
||||||
|
### `BYTEDANCE_API_KEY` (可选)
|
||||||
|
|
||||||
|
ByteDance Api Key.
|
||||||
|
|
||||||
|
### `BYTEDANCE_URL` (可选)
|
||||||
|
|
||||||
|
ByteDance Api Url.
|
||||||
|
|
||||||
|
### `ALIBABA_API_KEY` (可选)
|
||||||
|
|
||||||
|
阿里云(千问)Api Key.
|
||||||
|
|
||||||
|
### `ALIBABA_URL` (可选)
|
||||||
|
|
||||||
|
阿里云(千问)Api Url.
|
||||||
|
|
||||||
|
### `IFLYTEK_URL` (可选)
|
||||||
|
|
||||||
|
讯飞星火Api Url.
|
||||||
|
|
||||||
|
### `IFLYTEK_API_KEY` (可选)
|
||||||
|
|
||||||
|
讯飞星火Api Key.
|
||||||
|
|
||||||
|
### `IFLYTEK_API_SECRET` (可选)
|
||||||
|
|
||||||
|
讯飞星火Api Secret.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### `HIDE_USER_API_KEY` (可选)
|
### `HIDE_USER_API_KEY` (可选)
|
||||||
|
|
||||||
如果你不想让用户自行填入 API Key,将此环境变量设置为 1 即可。
|
如果你不想让用户自行填入 API Key,将此环境变量设置为 1 即可。
|
||||||
@@ -156,9 +216,31 @@ anthropic claude Api Url.
|
|||||||
|
|
||||||
用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。
|
用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。
|
||||||
|
|
||||||
|
在Azure的模式下,支持使用`modelName@azure=deploymentName`的方式配置模型名称和部署名称(deploy-name)
|
||||||
|
> 示例:`+gpt-3.5-turbo@azure=gpt35`这个配置会在模型列表显示一个`gpt35(Azure)`的选项。
|
||||||
|
> 如果你只能使用Azure模式,那么设置 `-all,+gpt-3.5-turbo@azure=gpt35` 则可以让对话的默认使用 `gpt35(Azure)`
|
||||||
|
|
||||||
|
在ByteDance的模式下,支持使用`modelName@bytedance=deploymentName`的方式配置模型名称和部署名称(deploy-name)
|
||||||
|
> 示例: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx`这个配置会在模型列表显示一个`Doubao-lite-4k(ByteDance)`的选项
|
||||||
|
|
||||||
|
|
||||||
|
### `DEFAULT_MODEL` (可选)
|
||||||
|
|
||||||
|
更改默认模型
|
||||||
|
|
||||||
### `DEFAULT_INPUT_TEMPLATE` (可选)
|
### `DEFAULT_INPUT_TEMPLATE` (可选)
|
||||||
|
|
||||||
自定义默认的 template,用于初始化『设置』中的『用户输入预处理』配置项
|
自定义默认的 template,用于初始化『设置』中的『用户输入预处理』配置项
|
||||||
|
|
||||||
|
### `STABILITY_API_KEY` (optional)
|
||||||
|
|
||||||
|
Stability API密钥
|
||||||
|
|
||||||
|
### `STABILITY_URL` (optional)
|
||||||
|
|
||||||
|
自定义的Stability API请求地址
|
||||||
|
|
||||||
|
|
||||||
## 开发
|
## 开发
|
||||||
|
|
||||||
点击下方按钮,开始二次开发:
|
点击下方按钮,开始二次开发:
|
||||||
|
|||||||
310
README_JA.md
Normal file
310
README_JA.md
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
<div align="center">
|
||||||
|
<img src="./docs/images/ent.svg" alt="プレビュー"/>
|
||||||
|
|
||||||
|
<h1 align="center">NextChat</h1>
|
||||||
|
|
||||||
|
ワンクリックで無料であなた専用の ChatGPT ウェブアプリをデプロイ。GPT3、GPT4 & Gemini Pro モデルをサポート。
|
||||||
|
|
||||||
|
[企業版](#企業版) / [デモ](https://chat-gpt-next-web.vercel.app/) / [フィードバック](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [Discordに参加](https://discord.gg/zrhvHCr79N)
|
||||||
|
|
||||||
|
[<img src="https://vercel.com/button" alt="Zeaburでデプロイ" height="30">](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [<img src="https://zeabur.com/button.svg" alt="Zeaburでデプロイ" height="30">](https://zeabur.com/templates/ZBUEFA) [<img src="https://gitpod.io/button/open-in-gitpod.svg" alt="Gitpodで開く" height="30">](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## 企業版
|
||||||
|
|
||||||
|
あなたの会社のプライベートデプロイとカスタマイズのニーズに応える
|
||||||
|
- **ブランドカスタマイズ**:企業向けに特別に設計された VI/UI、企業ブランドイメージとシームレスにマッチ
|
||||||
|
- **リソース統合**:企業管理者が数十種類のAIリソースを統一管理、チームメンバーはすぐに使用可能
|
||||||
|
- **権限管理**:メンバーの権限、リソースの権限、ナレッジベースの権限を明確にし、企業レベルのAdmin Panelで統一管理
|
||||||
|
- **知識の統合**:企業内部のナレッジベースとAI機能を結びつけ、汎用AIよりも企業自身の業務ニーズに近づける
|
||||||
|
- **セキュリティ監査**:機密質問を自動的にブロックし、すべての履歴対話を追跡可能にし、AIも企業の情報セキュリティ基準に従わせる
|
||||||
|
- **プライベートデプロイ**:企業レベルのプライベートデプロイ、主要なプライベートクラウドデプロイをサポートし、データのセキュリティとプライバシーを保護
|
||||||
|
- **継続的な更新**:マルチモーダル、エージェントなどの最先端機能を継続的に更新し、常に最新であり続ける
|
||||||
|
|
||||||
|
企業版のお問い合わせ: **business@nextchat.dev**
|
||||||
|
|
||||||
|
|
||||||
|
## 始めに
|
||||||
|
|
||||||
|
1. [OpenAI API Key](https://platform.openai.com/account/api-keys)を準備する;
|
||||||
|
2. 右側のボタンをクリックしてデプロイを開始:
|
||||||
|
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&env=GOOGLE_API_KEY&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web) 、GitHubアカウントで直接ログインし、環境変数ページにAPI Keyと[ページアクセスパスワード](#設定ページアクセスパスワード) CODEを入力してください;
|
||||||
|
3. デプロイが完了したら、すぐに使用を開始できます;
|
||||||
|
4. (オプション)[カスタムドメインをバインド](https://vercel.com/docs/concepts/projects/domains/add-a-domain):Vercelが割り当てたドメインDNSは一部の地域で汚染されているため、カスタムドメインをバインドすると直接接続できます。
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
## 更新を維持する
|
||||||
|
|
||||||
|
もし上記の手順に従ってワンクリックでプロジェクトをデプロイした場合、「更新があります」というメッセージが常に表示されることがあります。これは、Vercel がデフォルトで新しいプロジェクトを作成するためで、本プロジェクトを fork していないことが原因です。そのため、正しく更新を検出できません。
|
||||||
|
|
||||||
|
以下の手順に従って再デプロイすることをお勧めします:
|
||||||
|
|
||||||
|
- 元のリポジトリを削除する
|
||||||
|
- ページ右上の fork ボタンを使って、本プロジェクトを fork する
|
||||||
|
- Vercel で再度選択してデプロイする、[詳細な手順はこちらを参照してください](./docs/vercel-ja.md)。
|
||||||
|
|
||||||
|
|
||||||
|
### 自動更新を開く
|
||||||
|
|
||||||
|
> Upstream Sync の実行エラーが発生した場合は、手動で Sync Fork してください!
|
||||||
|
|
||||||
|
プロジェクトを fork した後、GitHub の制限により、fork 後のプロジェクトの Actions ページで Workflows を手動で有効にし、Upstream Sync Action を有効にする必要があります。有効化後、毎時の定期自動更新が可能になります:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
### 手動でコードを更新する
|
||||||
|
|
||||||
|
手動で即座に更新したい場合は、[GitHub のドキュメント](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)を参照して、fork したプロジェクトを上流のコードと同期する方法を確認してください。
|
||||||
|
|
||||||
|
このプロジェクトをスターまたはウォッチしたり、作者をフォローすることで、新機能の更新通知をすぐに受け取ることができます。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## ページアクセスパスワードを設定する
|
||||||
|
|
||||||
|
> パスワードを設定すると、ユーザーは設定ページでアクセスコードを手動で入力しない限り、通常のチャットができず、未承認の状態であることを示すメッセージが表示されます。
|
||||||
|
|
||||||
|
> **警告**:パスワードの桁数は十分に長く設定してください。7桁以上が望ましいです。さもないと、[ブルートフォース攻撃を受ける可能性があります](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/518)。
|
||||||
|
|
||||||
|
このプロジェクトは限られた権限管理機能を提供しています。Vercel プロジェクトのコントロールパネルで、環境変数ページに `CODE` という名前の環境変数を追加し、値をカンマで区切ったカスタムパスワードに設定してください:
|
||||||
|
|
||||||
|
```
|
||||||
|
code1,code2,code3
|
||||||
|
```
|
||||||
|
|
||||||
|
この環境変数を追加または変更した後、**プロジェクトを再デプロイ**して変更を有効にしてください。
|
||||||
|
|
||||||
|
|
||||||
|
## 環境変数
|
||||||
|
|
||||||
|
> 本プロジェクトのほとんどの設定は環境変数で行います。チュートリアル:[Vercel の環境変数を変更する方法](./docs/vercel-ja.md)。
|
||||||
|
|
||||||
|
### `OPENAI_API_KEY` (必須)
|
||||||
|
|
||||||
|
OpenAI の API キー。OpenAI アカウントページで申請したキーをカンマで区切って複数設定できます。これにより、ランダムにキーが選択されます。
|
||||||
|
|
||||||
|
### `CODE` (オプション)
|
||||||
|
|
||||||
|
アクセスパスワード。カンマで区切って複数設定可能。
|
||||||
|
|
||||||
|
**警告**:この項目を設定しないと、誰でもデプロイしたウェブサイトを利用でき、トークンが急速に消耗する可能性があるため、設定をお勧めします。
|
||||||
|
|
||||||
|
### `BASE_URL` (オプション)
|
||||||
|
|
||||||
|
> デフォルト: `https://api.openai.com`
|
||||||
|
|
||||||
|
> 例: `http://your-openai-proxy.com`
|
||||||
|
|
||||||
|
OpenAI API のプロキシ URL。手動で OpenAI API のプロキシを設定している場合はこのオプションを設定してください。
|
||||||
|
|
||||||
|
> SSL 証明書の問題がある場合は、`BASE_URL` のプロトコルを http に設定してください。
|
||||||
|
|
||||||
|
### `OPENAI_ORG_ID` (オプション)
|
||||||
|
|
||||||
|
OpenAI の組織 ID を指定します。
|
||||||
|
|
||||||
|
### `AZURE_URL` (オプション)
|
||||||
|
|
||||||
|
> 形式: https://{azure-resource-url}/openai/deployments/{deploy-name}
|
||||||
|
> `CUSTOM_MODELS` で `displayName` 形式で {deploy-name} を設定した場合、`AZURE_URL` から {deploy-name} を省略できます。
|
||||||
|
|
||||||
|
Azure のデプロイ URL。
|
||||||
|
|
||||||
|
### `AZURE_API_KEY` (オプション)
|
||||||
|
|
||||||
|
Azure の API キー。
|
||||||
|
|
||||||
|
### `AZURE_API_VERSION` (オプション)
|
||||||
|
|
||||||
|
Azure API バージョン。[Azure ドキュメント](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)で確認できます。
|
||||||
|
|
||||||
|
### `GOOGLE_API_KEY` (オプション)
|
||||||
|
|
||||||
|
Google Gemini Pro API キー。
|
||||||
|
|
||||||
|
### `GOOGLE_URL` (オプション)
|
||||||
|
|
||||||
|
Google Gemini Pro API の URL。
|
||||||
|
|
||||||
|
### `ANTHROPIC_API_KEY` (オプション)
|
||||||
|
|
||||||
|
Anthropic Claude API キー。
|
||||||
|
|
||||||
|
### `ANTHROPIC_API_VERSION` (オプション)
|
||||||
|
|
||||||
|
Anthropic Claude API バージョン。
|
||||||
|
|
||||||
|
### `ANTHROPIC_URL` (オプション)
|
||||||
|
|
||||||
|
Anthropic Claude API の URL。
|
||||||
|
|
||||||
|
### `BAIDU_API_KEY` (オプション)
|
||||||
|
|
||||||
|
Baidu API キー。
|
||||||
|
|
||||||
|
### `BAIDU_SECRET_KEY` (オプション)
|
||||||
|
|
||||||
|
Baidu シークレットキー。
|
||||||
|
|
||||||
|
### `BAIDU_URL` (オプション)
|
||||||
|
|
||||||
|
Baidu API の URL。
|
||||||
|
|
||||||
|
### `BYTEDANCE_API_KEY` (オプション)
|
||||||
|
|
||||||
|
ByteDance API キー。
|
||||||
|
|
||||||
|
### `BYTEDANCE_URL` (オプション)
|
||||||
|
|
||||||
|
ByteDance API の URL。
|
||||||
|
|
||||||
|
### `ALIBABA_API_KEY` (オプション)
|
||||||
|
|
||||||
|
アリババ(千问)API キー。
|
||||||
|
|
||||||
|
### `ALIBABA_URL` (オプション)
|
||||||
|
|
||||||
|
アリババ(千问)API の URL。
|
||||||
|
|
||||||
|
### `HIDE_USER_API_KEY` (オプション)
|
||||||
|
|
||||||
|
ユーザーが API キーを入力できないようにしたい場合は、この環境変数を 1 に設定します。
|
||||||
|
|
||||||
|
### `DISABLE_GPT4` (オプション)
|
||||||
|
|
||||||
|
ユーザーが GPT-4 を使用できないようにしたい場合は、この環境変数を 1 に設定します。
|
||||||
|
|
||||||
|
### `ENABLE_BALANCE_QUERY` (オプション)
|
||||||
|
|
||||||
|
バランスクエリ機能を有効にしたい場合は、この環境変数を 1 に設定します。
|
||||||
|
|
||||||
|
### `DISABLE_FAST_LINK` (オプション)
|
||||||
|
|
||||||
|
リンクからのプリセット設定解析を無効にしたい場合は、この環境変数を 1 に設定します。
|
||||||
|
|
||||||
|
### `WHITE_WEBDEV_ENDPOINTS` (オプション)
|
||||||
|
|
||||||
|
アクセス許可を与える WebDAV サービスのアドレスを追加したい場合、このオプションを使用します。フォーマット要件:
|
||||||
|
- 各アドレスは完全なエンドポイントでなければなりません。
|
||||||
|
> `https://xxxx/xxx`
|
||||||
|
- 複数のアドレスは `,` で接続します。
|
||||||
|
|
||||||
|
### `CUSTOM_MODELS` (オプション)
|
||||||
|
|
||||||
|
> 例:`+qwen-7b-chat,+glm-6b,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` は `qwen-7b-chat` と `glm-6b` をモデルリストに追加し、`gpt-3.5-turbo` を削除し、`gpt-4-1106-preview` のモデル名を `gpt-4-turbo` として表示します。
|
||||||
|
> すべてのモデルを無効にし、特定のモデルを有効にしたい場合は、`-all,+gpt-3.5-turbo` を使用します。これは `gpt-3.5-turbo` のみを有効にすることを意味します。
|
||||||
|
|
||||||
|
モデルリストを管理します。`+` でモデルを追加し、`-` でモデルを非表示にし、`モデル名=表示名` でモデルの表示名をカスタマイズし、カンマで区切ります。
|
||||||
|
|
||||||
|
Azure モードでは、`modelName@azure=deploymentName` 形式でモデル名とデプロイ名(deploy-name)を設定できます。
|
||||||
|
> 例:`+gpt-3.5-turbo@azure=gpt35` この設定でモデルリストに `gpt35(Azure)` のオプションが表示されます。
|
||||||
|
|
||||||
|
ByteDance モードでは、`modelName@bytedance=deploymentName` 形式でモデル名とデプロイ名(deploy-name)を設定できます。
|
||||||
|
> 例: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` この設定でモデルリストに `Doubao-lite-4k(ByteDance)` のオプションが表示されます。
|
||||||
|
|
||||||
|
### `DEFAULT_MODEL` (オプション)
|
||||||
|
|
||||||
|
デフォルトのモデルを変更します。
|
||||||
|
|
||||||
|
### `DEFAULT_INPUT_TEMPLATE` (オプション)
|
||||||
|
|
||||||
|
『設定』の『ユーザー入力前処理』の初期設定に使用するテンプレートをカスタマイズします。
|
||||||
|
|
||||||
|
|
||||||
|
## 開発
|
||||||
|
|
||||||
|
下のボタンをクリックして二次開発を開始してください:
|
||||||
|
|
||||||
|
[](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web)
|
||||||
|
|
||||||
|
コードを書く前に、プロジェクトのルートディレクトリに `.env.local` ファイルを新規作成し、環境変数を記入します:
|
||||||
|
|
||||||
|
```
|
||||||
|
OPENAI_API_KEY=<your api key here>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### ローカル開発
|
||||||
|
|
||||||
|
1. Node.js 18 と Yarn をインストールします。具体的な方法は ChatGPT にお尋ねください。
|
||||||
|
2. `yarn install && yarn dev` を実行します。⚠️ 注意:このコマンドはローカル開発用であり、デプロイには使用しないでください。
|
||||||
|
3. ローカルでデプロイしたい場合は、`yarn install && yarn build && yarn start` コマンドを使用してください。プロセスを守るために pm2 を使用することもできます。詳細は ChatGPT にお尋ねください。
|
||||||
|
|
||||||
|
|
||||||
|
## デプロイ
|
||||||
|
|
||||||
|
### コンテナデプロイ(推奨)
|
||||||
|
|
||||||
|
> Docker バージョンは 20 以上が必要です。それ以下だとイメージが見つからないというエラーが出ます。
|
||||||
|
|
||||||
|
> ⚠️ 注意:Docker バージョンは最新バージョンより 1~2 日遅れることが多いため、デプロイ後に「更新があります」の通知が出続けることがありますが、正常です。
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker pull yidadaa/chatgpt-next-web
|
||||||
|
|
||||||
|
docker run -d -p 3000:3000 \
|
||||||
|
-e OPENAI_API_KEY=sk-xxxx \
|
||||||
|
-e CODE=ページアクセスパスワード \
|
||||||
|
yidadaa/chatgpt-next-web
|
||||||
|
```
|
||||||
|
|
||||||
|
プロキシを指定することもできます:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker run -d -p 3000:3000 \
|
||||||
|
-e OPENAI_API_KEY=sk-xxxx \
|
||||||
|
-e CODE=ページアクセスパスワード \
|
||||||
|
--net=host \
|
||||||
|
-e PROXY_URL=http://127.0.0.1:7890 \
|
||||||
|
yidadaa/chatgpt-next-web
|
||||||
|
```
|
||||||
|
|
||||||
|
ローカルプロキシがアカウントとパスワードを必要とする場合は、以下を使用できます:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
-e PROXY_URL="http://127.0.0.1:7890 user password"
|
||||||
|
```
|
||||||
|
|
||||||
|
他の環境変数を指定する必要がある場合は、上記のコマンドに `-e 環境変数=環境変数値` を追加して指定してください。
|
||||||
|
|
||||||
|
|
||||||
|
### ローカルデプロイ
|
||||||
|
|
||||||
|
コンソールで以下のコマンドを実行します:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
bash <(curl -s https://raw.githubusercontent.com/Yidadaa/ChatGPT-Next-Web/main/scripts/setup.sh)
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ 注意:インストール中に問題が発生した場合は、Docker を使用してデプロイしてください。
|
||||||
|
|
||||||
|
|
||||||
|
## 謝辞
|
||||||
|
|
||||||
|
### 寄付者
|
||||||
|
|
||||||
|
> 英語版をご覧ください。
|
||||||
|
|
||||||
|
### 貢献者
|
||||||
|
|
||||||
|
[プロジェクトの貢献者リストはこちら](https://github.com/Yidadaa/ChatGPT-Next-Web/graphs/contributors)
|
||||||
|
|
||||||
|
### 関連プロジェクト
|
||||||
|
|
||||||
|
- [one-api](https://github.com/songquanpeng/one-api): 一つのプラットフォームで大規模モデルのクォータ管理を提供し、市場に出回っているすべての主要な大規模言語モデルをサポートします。
|
||||||
|
|
||||||
|
|
||||||
|
## オープンソースライセンス
|
||||||
|
|
||||||
|
[MIT](https://opensource.org/license/mit/)
|
||||||
70
app/api/[provider]/[...path]/route.ts
Normal file
70
app/api/[provider]/[...path]/route.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import { ApiPath } from "@/app/constant";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { handle as openaiHandler } from "../../openai";
|
||||||
|
import { handle as azureHandler } from "../../azure";
|
||||||
|
import { handle as googleHandler } from "../../google";
|
||||||
|
import { handle as anthropicHandler } from "../../anthropic";
|
||||||
|
import { handle as baiduHandler } from "../../baidu";
|
||||||
|
import { handle as bytedanceHandler } from "../../bytedance";
|
||||||
|
import { handle as alibabaHandler } from "../../alibaba";
|
||||||
|
import { handle as moonshotHandler } from "../../moonshot";
|
||||||
|
import { handle as stabilityHandler } from "../../stability";
|
||||||
|
import { handle as iflytekHandler } from "../../iflytek";
|
||||||
|
import { handle as proxyHandler } from "../../proxy";
|
||||||
|
|
||||||
|
async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { provider: string; path: string[] } },
|
||||||
|
) {
|
||||||
|
const apiPath = `/api/${params.provider}`;
|
||||||
|
console.log(`[${params.provider} Route] params `, params);
|
||||||
|
switch (apiPath) {
|
||||||
|
case ApiPath.Azure:
|
||||||
|
return azureHandler(req, { params });
|
||||||
|
case ApiPath.Google:
|
||||||
|
return googleHandler(req, { params });
|
||||||
|
case ApiPath.Anthropic:
|
||||||
|
return anthropicHandler(req, { params });
|
||||||
|
case ApiPath.Baidu:
|
||||||
|
return baiduHandler(req, { params });
|
||||||
|
case ApiPath.ByteDance:
|
||||||
|
return bytedanceHandler(req, { params });
|
||||||
|
case ApiPath.Alibaba:
|
||||||
|
return alibabaHandler(req, { params });
|
||||||
|
// case ApiPath.Tencent: using "/api/tencent"
|
||||||
|
case ApiPath.Moonshot:
|
||||||
|
return moonshotHandler(req, { params });
|
||||||
|
case ApiPath.Stability:
|
||||||
|
return stabilityHandler(req, { params });
|
||||||
|
case ApiPath.Iflytek:
|
||||||
|
return iflytekHandler(req, { params });
|
||||||
|
case ApiPath.OpenAI:
|
||||||
|
return openaiHandler(req, { params });
|
||||||
|
default:
|
||||||
|
return proxyHandler(req, { params });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const GET = handle;
|
||||||
|
export const POST = handle;
|
||||||
|
|
||||||
|
export const runtime = "edge";
|
||||||
|
export const preferredRegion = [
|
||||||
|
"arn1",
|
||||||
|
"bom1",
|
||||||
|
"cdg1",
|
||||||
|
"cle1",
|
||||||
|
"cpt1",
|
||||||
|
"dub1",
|
||||||
|
"fra1",
|
||||||
|
"gru1",
|
||||||
|
"hnd1",
|
||||||
|
"iad1",
|
||||||
|
"icn1",
|
||||||
|
"kix1",
|
||||||
|
"lhr1",
|
||||||
|
"pdx1",
|
||||||
|
"sfo1",
|
||||||
|
"sin1",
|
||||||
|
"syd1",
|
||||||
|
];
|
||||||
131
app/api/alibaba.ts
Normal file
131
app/api/alibaba.ts
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
Alibaba,
|
||||||
|
ALIBABA_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import type { RequestPayload } from "@/app/client/platforms/openai";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Alibaba Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Qwen);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Alibaba] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
// alibaba use base url or just remove the path
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Alibaba, "");
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchUrl = `${baseUrl}${path}`;
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: req.headers.get("Authorization") ?? "",
|
||||||
|
"X-DashScope-SSE": req.headers.get("X-DashScope-SSE") ?? "disable",
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
// #1815 try to refuse some request to some models
|
||||||
|
if (serverConfig.customModels && req.body) {
|
||||||
|
try {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
|
// not undefined and is false
|
||||||
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Alibaba as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Alibaba] filter`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,16 +4,18 @@ import {
|
|||||||
Anthropic,
|
Anthropic,
|
||||||
ApiPath,
|
ApiPath,
|
||||||
DEFAULT_MODELS,
|
DEFAULT_MODELS,
|
||||||
|
ServiceProvider,
|
||||||
ModelProvider,
|
ModelProvider,
|
||||||
} from "@/app/constant";
|
} from "@/app/constant";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "../../auth";
|
import { auth } from "./auth";
|
||||||
import { collectModelTable } from "@/app/utils/model";
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
|
|
||||||
const ALLOWD_PATH = new Set([Anthropic.ChatPath, Anthropic.ChatPath1]);
|
const ALLOWD_PATH = new Set([Anthropic.ChatPath, Anthropic.ChatPath1]);
|
||||||
|
|
||||||
async function handle(
|
export async function handle(
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
{ params }: { params: { path: string[] } },
|
{ params }: { params: { path: string[] } },
|
||||||
) {
|
) {
|
||||||
@@ -54,30 +56,6 @@ async function handle(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handle;
|
|
||||||
export const POST = handle;
|
|
||||||
|
|
||||||
export const runtime = "edge";
|
|
||||||
export const preferredRegion = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
async function request(req: NextRequest) {
|
async function request(req: NextRequest) {
|
||||||
@@ -113,7 +91,8 @@ async function request(req: NextRequest) {
|
|||||||
10 * 60 * 1000,
|
10 * 60 * 1000,
|
||||||
);
|
);
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}${path}`;
|
// try rebuild url, when using cloudflare ai gateway in server
|
||||||
|
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}${path}`);
|
||||||
|
|
||||||
const fetchOptions: RequestInit = {
|
const fetchOptions: RequestInit = {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -136,17 +115,19 @@ async function request(req: NextRequest) {
|
|||||||
// #1815 try to refuse some request to some models
|
// #1815 try to refuse some request to some models
|
||||||
if (serverConfig.customModels && req.body) {
|
if (serverConfig.customModels && req.body) {
|
||||||
try {
|
try {
|
||||||
const modelTable = collectModelTable(
|
|
||||||
DEFAULT_MODELS,
|
|
||||||
serverConfig.customModels,
|
|
||||||
);
|
|
||||||
const clonedBody = await req.text();
|
const clonedBody = await req.text();
|
||||||
fetchOptions.body = clonedBody;
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
// not undefined and is false
|
// not undefined and is false
|
||||||
if (modelTable[jsonBody?.model ?? ""].available === false) {
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Anthropic as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -161,17 +142,17 @@ async function request(req: NextRequest) {
|
|||||||
console.error(`[Anthropic] filter`, e);
|
console.error(`[Anthropic] filter`, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("[Anthropic request]", fetchOptions.headers, req.method);
|
// console.log("[Anthropic request]", fetchOptions.headers, req.method);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
console.log(
|
// console.log(
|
||||||
"[Anthropic response]",
|
// "[Anthropic response]",
|
||||||
res.status,
|
// res.status,
|
||||||
" ",
|
// " ",
|
||||||
res.headers,
|
// res.headers,
|
||||||
res.url,
|
// res.url,
|
||||||
);
|
// );
|
||||||
// to prevent browser prompt for credentials
|
// to prevent browser prompt for credentials
|
||||||
const newHeaders = new Headers(res.headers);
|
const newHeaders = new Headers(res.headers);
|
||||||
newHeaders.delete("www-authenticate");
|
newHeaders.delete("www-authenticate");
|
||||||
73
app/api/artifacts/route.ts
Normal file
73
app/api/artifacts/route.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import md5 from "spark-md5";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
|
||||||
|
async function handle(req: NextRequest, res: NextResponse) {
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
const storeUrl = () =>
|
||||||
|
`https://api.cloudflare.com/client/v4/accounts/${serverConfig.cloudflareAccountId}/storage/kv/namespaces/${serverConfig.cloudflareKVNamespaceId}`;
|
||||||
|
const storeHeaders = () => ({
|
||||||
|
Authorization: `Bearer ${serverConfig.cloudflareKVApiKey}`,
|
||||||
|
});
|
||||||
|
if (req.method === "POST") {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
const hashedCode = md5.hash(clonedBody).trim();
|
||||||
|
const body: {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
expiration_ttl?: number;
|
||||||
|
} = {
|
||||||
|
key: hashedCode,
|
||||||
|
value: clonedBody,
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const ttl = parseInt(serverConfig.cloudflareKVTTL as string);
|
||||||
|
if (ttl > 60) {
|
||||||
|
body["expiration_ttl"] = ttl;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
const res = await fetch(`${storeUrl()}/bulk`, {
|
||||||
|
headers: {
|
||||||
|
...storeHeaders(),
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
method: "PUT",
|
||||||
|
body: JSON.stringify([body]),
|
||||||
|
});
|
||||||
|
const result = await res.json();
|
||||||
|
console.log("save data", result);
|
||||||
|
if (result?.success) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ code: 0, id: hashedCode, result },
|
||||||
|
{ status: res.status },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: true, msg: "Save data error" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (req.method === "GET") {
|
||||||
|
const id = req?.nextUrl?.searchParams?.get("id");
|
||||||
|
const res = await fetch(`${storeUrl()}/values/${id}`, {
|
||||||
|
headers: storeHeaders(),
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: res.headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: true, msg: "Invalid request" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const POST = handle;
|
||||||
|
export const GET = handle;
|
||||||
|
|
||||||
|
export const runtime = "edge";
|
||||||
@@ -67,15 +67,34 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
|
|||||||
let systemApiKey: string | undefined;
|
let systemApiKey: string | undefined;
|
||||||
|
|
||||||
switch (modelProvider) {
|
switch (modelProvider) {
|
||||||
|
case ModelProvider.Stability:
|
||||||
|
systemApiKey = serverConfig.stabilityApiKey;
|
||||||
|
break;
|
||||||
case ModelProvider.GeminiPro:
|
case ModelProvider.GeminiPro:
|
||||||
systemApiKey = serverConfig.googleApiKey;
|
systemApiKey = serverConfig.googleApiKey;
|
||||||
break;
|
break;
|
||||||
case ModelProvider.Claude:
|
case ModelProvider.Claude:
|
||||||
systemApiKey = serverConfig.anthropicApiKey;
|
systemApiKey = serverConfig.anthropicApiKey;
|
||||||
break;
|
break;
|
||||||
|
case ModelProvider.Doubao:
|
||||||
|
systemApiKey = serverConfig.bytedanceApiKey;
|
||||||
|
break;
|
||||||
|
case ModelProvider.Ernie:
|
||||||
|
systemApiKey = serverConfig.baiduApiKey;
|
||||||
|
break;
|
||||||
|
case ModelProvider.Qwen:
|
||||||
|
systemApiKey = serverConfig.alibabaApiKey;
|
||||||
|
break;
|
||||||
|
case ModelProvider.Moonshot:
|
||||||
|
systemApiKey = serverConfig.moonshotApiKey;
|
||||||
|
break;
|
||||||
|
case ModelProvider.Iflytek:
|
||||||
|
systemApiKey =
|
||||||
|
serverConfig.iflytekApiKey + ":" + serverConfig.iflytekApiSecret;
|
||||||
|
break;
|
||||||
case ModelProvider.GPT:
|
case ModelProvider.GPT:
|
||||||
default:
|
default:
|
||||||
if (serverConfig.isAzure) {
|
if (req.nextUrl.pathname.includes("azure/deployments")) {
|
||||||
systemApiKey = serverConfig.azureApiKey;
|
systemApiKey = serverConfig.azureApiKey;
|
||||||
} else {
|
} else {
|
||||||
systemApiKey = serverConfig.apiKey;
|
systemApiKey = serverConfig.apiKey;
|
||||||
|
|||||||
33
app/api/azure.ts
Normal file
33
app/api/azure.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import { ModelProvider } from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "./auth";
|
||||||
|
import { requestOpenai } from "./common";
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Azure Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const subpath = params.path.join("/");
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.GPT);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await requestOpenai(req);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Azure] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
145
app/api/baidu.ts
Normal file
145
app/api/baidu.ts
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
BAIDU_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
BAIDU_OATUH_URL,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import { getAccessToken } from "@/app/utils/baidu";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Baidu Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Ernie);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!serverConfig.baiduApiKey || !serverConfig.baiduSecretKey) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `missing BAIDU_API_KEY or BAIDU_SECRET_KEY in server env vars`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 401,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Baidu] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Baidu, "");
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.baiduUrl || BAIDU_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const { access_token } = await getAccessToken(
|
||||||
|
serverConfig.baiduApiKey as string,
|
||||||
|
serverConfig.baiduSecretKey as string,
|
||||||
|
);
|
||||||
|
const fetchUrl = `${baseUrl}${path}?access_token=${access_token}`;
|
||||||
|
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
// #1815 try to refuse some request to some models
|
||||||
|
if (serverConfig.customModels && req.body) {
|
||||||
|
try {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
|
// not undefined and is false
|
||||||
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Baidu as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Baidu] filter`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
129
app/api/bytedance.ts
Normal file
129
app/api/bytedance.ts
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
BYTEDANCE_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[ByteDance Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Doubao);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[ByteDance] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ByteDance, "");
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.bytedanceUrl || BYTEDANCE_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchUrl = `${baseUrl}${path}`;
|
||||||
|
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: req.headers.get("Authorization") ?? "",
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
// #1815 try to refuse some request to some models
|
||||||
|
if (serverConfig.customModels && req.body) {
|
||||||
|
try {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
|
// not undefined and is false
|
||||||
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.ByteDance as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[ByteDance] filter`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,24 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { getServerSideConfig } from "../config/server";
|
import { getServerSideConfig } from "../config/server";
|
||||||
import { DEFAULT_MODELS, OPENAI_BASE_URL, GEMINI_BASE_URL } from "../constant";
|
import {
|
||||||
import { collectModelTable } from "../utils/model";
|
DEFAULT_MODELS,
|
||||||
import { makeAzurePath } from "../azure";
|
OPENAI_BASE_URL,
|
||||||
|
GEMINI_BASE_URL,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "../constant";
|
||||||
|
import { isModelAvailableInServer } from "../utils/model";
|
||||||
|
import { cloudflareAIGatewayUrl } from "../utils/cloudflare";
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
export async function requestOpenai(req: NextRequest) {
|
export async function requestOpenai(req: NextRequest) {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
const isAzure = req.nextUrl.pathname.includes("azure/deployments");
|
||||||
|
|
||||||
var authValue,
|
var authValue,
|
||||||
authHeaderName = "";
|
authHeaderName = "";
|
||||||
if (serverConfig.isAzure) {
|
if (isAzure) {
|
||||||
authValue =
|
authValue =
|
||||||
req.headers
|
req.headers
|
||||||
.get("Authorization")
|
.get("Authorization")
|
||||||
@@ -25,13 +32,10 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
authHeaderName = "Authorization";
|
authHeaderName = "Authorization";
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
|
let path = `${req.nextUrl.pathname}`.replaceAll("/api/openai/", "");
|
||||||
"/api/openai/",
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
let baseUrl =
|
let baseUrl =
|
||||||
serverConfig.azureUrl || serverConfig.baseUrl || OPENAI_BASE_URL;
|
(isAzure ? serverConfig.azureUrl : serverConfig.baseUrl) || OPENAI_BASE_URL;
|
||||||
|
|
||||||
if (!baseUrl.startsWith("http")) {
|
if (!baseUrl.startsWith("http")) {
|
||||||
baseUrl = `https://${baseUrl}`;
|
baseUrl = `https://${baseUrl}`;
|
||||||
@@ -51,17 +55,46 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
10 * 60 * 1000,
|
10 * 60 * 1000,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (serverConfig.isAzure) {
|
if (isAzure) {
|
||||||
if (!serverConfig.azureApiVersion) {
|
const azureApiVersion =
|
||||||
return NextResponse.json({
|
req?.nextUrl?.searchParams?.get("api-version") ||
|
||||||
error: true,
|
serverConfig.azureApiVersion;
|
||||||
message: `missing AZURE_API_VERSION in server env vars`,
|
baseUrl = baseUrl.split("/deployments").shift() as string;
|
||||||
});
|
path = `${req.nextUrl.pathname.replaceAll(
|
||||||
|
"/api/azure/",
|
||||||
|
"",
|
||||||
|
)}?api-version=${azureApiVersion}`;
|
||||||
|
|
||||||
|
// Forward compatibility:
|
||||||
|
// if display_name(deployment_name) not set, and '{deploy-id}' in AZURE_URL
|
||||||
|
// then using default '{deploy-id}'
|
||||||
|
if (serverConfig.customModels && serverConfig.azureUrl) {
|
||||||
|
const modelName = path.split("/")[1];
|
||||||
|
let realDeployName = "";
|
||||||
|
serverConfig.customModels
|
||||||
|
.split(",")
|
||||||
|
.filter((v) => !!v && !v.startsWith("-") && v.includes(modelName))
|
||||||
|
.forEach((m) => {
|
||||||
|
const [fullName, displayName] = m.split("=");
|
||||||
|
const [_, providerName] = fullName.split("@");
|
||||||
|
if (providerName === "azure" && !displayName) {
|
||||||
|
const [_, deployId] = (serverConfig?.azureUrl ?? "").split(
|
||||||
|
"deployments/",
|
||||||
|
);
|
||||||
|
if (deployId) {
|
||||||
|
realDeployName = deployId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (realDeployName) {
|
||||||
|
console.log("[Replace with DeployId", realDeployName);
|
||||||
|
path = path.replaceAll(modelName, realDeployName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
path = makeAzurePath(path, serverConfig.azureApiVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}/${path}`;
|
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
|
||||||
|
console.log("fetchUrl", fetchUrl);
|
||||||
const fetchOptions: RequestInit = {
|
const fetchOptions: RequestInit = {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -83,17 +116,24 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
// #1815 try to refuse gpt4 request
|
// #1815 try to refuse gpt4 request
|
||||||
if (serverConfig.customModels && req.body) {
|
if (serverConfig.customModels && req.body) {
|
||||||
try {
|
try {
|
||||||
const modelTable = collectModelTable(
|
|
||||||
DEFAULT_MODELS,
|
|
||||||
serverConfig.customModels,
|
|
||||||
);
|
|
||||||
const clonedBody = await req.text();
|
const clonedBody = await req.text();
|
||||||
fetchOptions.body = clonedBody;
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
// not undefined and is false
|
// not undefined and is false
|
||||||
if (modelTable[jsonBody?.model ?? ""].available === false) {
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.OpenAI as string,
|
||||||
|
) ||
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Azure as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -112,16 +152,16 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
// Extract the OpenAI-Organization header from the response
|
// Extract the OpenAI-Organization header from the response
|
||||||
const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
|
const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
|
||||||
|
|
||||||
// Check if serverConfig.openaiOrgId is defined and not an empty string
|
// Check if serverConfig.openaiOrgId is defined and not an empty string
|
||||||
if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") {
|
if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") {
|
||||||
// If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
|
// If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
|
||||||
console.log("[Org ID]", openaiOrganizationHeader);
|
console.log("[Org ID]", openaiOrganizationHeader);
|
||||||
} else {
|
} else {
|
||||||
console.log("[Org ID] is not set up.");
|
console.log("[Org ID] is not set up.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// to prevent browser prompt for credentials
|
// to prevent browser prompt for credentials
|
||||||
const newHeaders = new Headers(res.headers);
|
const newHeaders = new Headers(res.headers);
|
||||||
@@ -129,7 +169,6 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
// to disable nginx buffering
|
// to disable nginx buffering
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
|
||||||
// Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV)
|
// Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV)
|
||||||
// Also, this is to prevent the header from being sent to the client
|
// Also, this is to prevent the header from being sent to the client
|
||||||
if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") {
|
if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") {
|
||||||
@@ -142,7 +181,6 @@ export async function requestOpenai(req: NextRequest) {
|
|||||||
// The browser will try to decode the response with brotli and fail
|
// The browser will try to decode the response with brotli and fail
|
||||||
newHeaders.delete("content-encoding");
|
newHeaders.delete("content-encoding");
|
||||||
|
|
||||||
|
|
||||||
return new Response(res.body, {
|
return new Response(res.body, {
|
||||||
status: res.status,
|
status: res.status,
|
||||||
statusText: res.statusText,
|
statusText: res.statusText,
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "../../auth";
|
import { auth } from "./auth";
|
||||||
import { getServerSideConfig } from "@/app/config/server";
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
import { GEMINI_BASE_URL, Google, ModelProvider } from "@/app/constant";
|
import {
|
||||||
|
ApiPath,
|
||||||
|
GEMINI_BASE_URL,
|
||||||
|
Google,
|
||||||
|
ModelProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
|
||||||
async function handle(
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
{ params }: { params: { path: string[] } },
|
{ params }: { params: { provider: string; path: string[] } },
|
||||||
) {
|
) {
|
||||||
console.log("[Google Route] params ", params);
|
console.log("[Google Route] params ", params);
|
||||||
|
|
||||||
@@ -13,32 +21,6 @@ async function handle(
|
|||||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
|
||||||
|
|
||||||
let baseUrl = serverConfig.googleUrl || GEMINI_BASE_URL;
|
|
||||||
|
|
||||||
if (!baseUrl.startsWith("http")) {
|
|
||||||
baseUrl = `https://${baseUrl}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (baseUrl.endsWith("/")) {
|
|
||||||
baseUrl = baseUrl.slice(0, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let path = `${req.nextUrl.pathname}`.replaceAll("/api/google/", "");
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", baseUrl);
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
const authResult = auth(req, ModelProvider.GeminiPro);
|
const authResult = auth(req, ModelProvider.GeminiPro);
|
||||||
if (authResult.error) {
|
if (authResult.error) {
|
||||||
return NextResponse.json(authResult, {
|
return NextResponse.json(authResult, {
|
||||||
@@ -49,9 +31,9 @@ async function handle(
|
|||||||
const bearToken = req.headers.get("Authorization") ?? "";
|
const bearToken = req.headers.get("Authorization") ?? "";
|
||||||
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
||||||
|
|
||||||
const key = token ? token : serverConfig.googleApiKey;
|
const apiKey = token ? token : serverConfig.googleApiKey;
|
||||||
|
|
||||||
if (!key) {
|
if (!apiKey) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -62,8 +44,63 @@ async function handle(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
const response = await request(req, apiKey);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Google] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}/${path}?key=${key}`;
|
export const GET = handle;
|
||||||
|
export const POST = handle;
|
||||||
|
|
||||||
|
export const runtime = "edge";
|
||||||
|
export const preferredRegion = [
|
||||||
|
"bom1",
|
||||||
|
"cle1",
|
||||||
|
"cpt1",
|
||||||
|
"gru1",
|
||||||
|
"hnd1",
|
||||||
|
"iad1",
|
||||||
|
"icn1",
|
||||||
|
"kix1",
|
||||||
|
"pdx1",
|
||||||
|
"sfo1",
|
||||||
|
"sin1",
|
||||||
|
"syd1",
|
||||||
|
];
|
||||||
|
|
||||||
|
async function request(req: NextRequest, apiKey: string) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.googleUrl || GEMINI_BASE_URL;
|
||||||
|
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Google, "");
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
const fetchUrl = `${baseUrl}${path}?key=${apiKey}${
|
||||||
|
req?.nextUrl?.searchParams?.get("alt") === "sse" ? "&alt=sse" : ""
|
||||||
|
}`;
|
||||||
|
|
||||||
|
console.log("[Fetch Url] ", fetchUrl);
|
||||||
const fetchOptions: RequestInit = {
|
const fetchOptions: RequestInit = {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -95,22 +132,3 @@ async function handle(
|
|||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handle;
|
|
||||||
export const POST = handle;
|
|
||||||
|
|
||||||
export const runtime = "edge";
|
|
||||||
export const preferredRegion = [
|
|
||||||
"bom1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
131
app/api/iflytek.ts
Normal file
131
app/api/iflytek.ts
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
Iflytek,
|
||||||
|
IFLYTEK_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import type { RequestPayload } from "@/app/client/platforms/openai";
|
||||||
|
// iflytek
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Iflytek Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Iflytek);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Iflytek] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
// iflytek use base url or just remove the path
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Iflytek, "");
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.iflytekUrl || IFLYTEK_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchUrl = `${baseUrl}${path}`;
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: req.headers.get("Authorization") ?? "",
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
// try to refuse some request to some models
|
||||||
|
if (serverConfig.customModels && req.body) {
|
||||||
|
try {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
|
// not undefined and is false
|
||||||
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Iflytek as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Iflytek] filter`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
130
app/api/moonshot.ts
Normal file
130
app/api/moonshot.ts
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
Moonshot,
|
||||||
|
MOONSHOT_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import type { RequestPayload } from "@/app/client/platforms/openai";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Moonshot Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Moonshot);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Moonshot] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
// alibaba use base url or just remove the path
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Moonshot, "");
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.moonshotUrl || MOONSHOT_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy] ", path);
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchUrl = `${baseUrl}${path}`;
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: req.headers.get("Authorization") ?? "",
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
// #1815 try to refuse some request to some models
|
||||||
|
if (serverConfig.customModels && req.body) {
|
||||||
|
try {
|
||||||
|
const clonedBody = await req.text();
|
||||||
|
fetchOptions.body = clonedBody;
|
||||||
|
|
||||||
|
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||||
|
|
||||||
|
// not undefined and is false
|
||||||
|
if (
|
||||||
|
isModelAvailableInServer(
|
||||||
|
serverConfig.customModels,
|
||||||
|
jsonBody?.model as string,
|
||||||
|
ServiceProvider.Moonshot as string,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Moonshot] filter`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@ import { getServerSideConfig } from "@/app/config/server";
|
|||||||
import { ModelProvider, OpenaiPath } from "@/app/constant";
|
import { ModelProvider, OpenaiPath } from "@/app/constant";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "../../auth";
|
import { auth } from "./auth";
|
||||||
import { requestOpenai } from "../../common";
|
import { requestOpenai } from "./common";
|
||||||
|
|
||||||
const ALLOWD_PATH = new Set(Object.values(OpenaiPath));
|
const ALLOWD_PATH = new Set(Object.values(OpenaiPath));
|
||||||
|
|
||||||
@@ -13,14 +13,16 @@ function getModels(remoteModelRes: OpenAIListModelResponse) {
|
|||||||
|
|
||||||
if (config.disableGPT4) {
|
if (config.disableGPT4) {
|
||||||
remoteModelRes.data = remoteModelRes.data.filter(
|
remoteModelRes.data = remoteModelRes.data.filter(
|
||||||
(m) => !m.id.startsWith("gpt-4"),
|
(m) =>
|
||||||
|
!(m.id.startsWith("gpt-4") || m.id.startsWith("chatgpt-4o")) ||
|
||||||
|
m.id.startsWith("gpt-4o-mini"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return remoteModelRes;
|
return remoteModelRes;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handle(
|
export async function handle(
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
{ params }: { params: { path: string[] } },
|
{ params }: { params: { path: string[] } },
|
||||||
) {
|
) {
|
||||||
@@ -70,27 +72,3 @@ async function handle(
|
|||||||
return NextResponse.json(prettyObject(e));
|
return NextResponse.json(prettyObject(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handle;
|
|
||||||
export const POST = handle;
|
|
||||||
|
|
||||||
export const runtime = "edge";
|
|
||||||
export const preferredRegion = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
import * as ProviderTemplates from "@/app/client/providers";
|
|
||||||
import { getServerSideConfig } from "@/app/config/server";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { cloneDeep } from "lodash-es";
|
|
||||||
import {
|
|
||||||
disableSystemApiKey,
|
|
||||||
makeUrlsUsable,
|
|
||||||
modelNameRequestHeader,
|
|
||||||
} from "@/app/client/common";
|
|
||||||
import { collectModelTable } from "@/app/utils/model";
|
|
||||||
|
|
||||||
async function handle(
|
|
||||||
req: NextRequest,
|
|
||||||
{ params }: { params: { path: string[] } },
|
|
||||||
) {
|
|
||||||
const [providerName] = params.path;
|
|
||||||
const { headers } = req;
|
|
||||||
const serverConfig = getServerSideConfig();
|
|
||||||
const modelName = headers.get(modelNameRequestHeader);
|
|
||||||
|
|
||||||
const ProviderTemplate = Object.values(ProviderTemplates).find(
|
|
||||||
(t) => t.prototype.name === providerName,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!ProviderTemplate) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: "No provider found: " + providerName,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 404,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// #1815 try to refuse gpt4 request
|
|
||||||
if (modelName && serverConfig.customModels) {
|
|
||||||
try {
|
|
||||||
const modelTable = collectModelTable([], serverConfig.customModels);
|
|
||||||
|
|
||||||
// not undefined and is false
|
|
||||||
if (modelTable[modelName]?.available === false) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: `you are not allowed to use ${modelName} model`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 403,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("models filter", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const config = disableSystemApiKey(
|
|
||||||
makeUrlsUsable(cloneDeep(serverConfig), [
|
|
||||||
"anthropicUrl",
|
|
||||||
"azureUrl",
|
|
||||||
"googleUrl",
|
|
||||||
"baseUrl",
|
|
||||||
]),
|
|
||||||
["anthropicApiKey", "azureApiKey", "googleApiKey", "apiKey"],
|
|
||||||
serverConfig.needCode &&
|
|
||||||
ProviderTemplate !== ProviderTemplates.NextChatProvider, // if it must take a access code in the req, do not provide system-keys for Non-nextchat providers
|
|
||||||
);
|
|
||||||
|
|
||||||
const request = Object.assign({}, req, {
|
|
||||||
subpath: params.path.join("/"),
|
|
||||||
});
|
|
||||||
|
|
||||||
return new ProviderTemplate().serverSideRequestHandler(request, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const GET = handle;
|
|
||||||
export const POST = handle;
|
|
||||||
export const PUT = handle;
|
|
||||||
export const PATCH = handle;
|
|
||||||
export const DELETE = handle;
|
|
||||||
export const OPTIONS = handle;
|
|
||||||
|
|
||||||
export const runtime = "edge";
|
|
||||||
export const preferredRegion = Array.from(
|
|
||||||
new Set(
|
|
||||||
Object.values(ProviderTemplates).reduce(
|
|
||||||
(arr, t) => [...arr, ...(t.prototype.preferredRegion ?? [])],
|
|
||||||
[] as string[],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
75
app/api/proxy.ts
Normal file
75
app/api/proxy.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Proxy Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove path params from searchParams
|
||||||
|
req.nextUrl.searchParams.delete("path");
|
||||||
|
req.nextUrl.searchParams.delete("provider");
|
||||||
|
|
||||||
|
const subpath = params.path.join("/");
|
||||||
|
const fetchUrl = `${req.headers.get(
|
||||||
|
"x-base-url",
|
||||||
|
)}/${subpath}?${req.nextUrl.searchParams.toString()}`;
|
||||||
|
const skipHeaders = ["connection", "host", "origin", "referer", "cookie"];
|
||||||
|
const headers = new Headers(
|
||||||
|
Array.from(req.headers.entries()).filter((item) => {
|
||||||
|
if (
|
||||||
|
item[0].indexOf("x-") > -1 ||
|
||||||
|
item[0].indexOf("sec-") > -1 ||
|
||||||
|
skipHeaders.includes(item[0])
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const controller = new AbortController();
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers,
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
// The latest version of the OpenAI API forced the content-encoding to be "br" in json response
|
||||||
|
// So if the streaming is disabled, we need to remove the content-encoding header
|
||||||
|
// Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header
|
||||||
|
// The browser will try to decode the response with brotli and fail
|
||||||
|
newHeaders.delete("content-encoding");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
99
app/api/stability.ts
Normal file
99
app/api/stability.ts
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import { ModelProvider, STABILITY_BASE_URL } from "@/app/constant";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
|
||||||
|
export async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Stability] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.stabilityUrl || STABILITY_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = `${req.nextUrl.pathname}`.replaceAll("/api/stability/", "");
|
||||||
|
|
||||||
|
console.log("[Stability Proxy] ", path);
|
||||||
|
console.log("[Stability Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Stability);
|
||||||
|
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const bearToken = req.headers.get("Authorization") ?? "";
|
||||||
|
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
||||||
|
|
||||||
|
const key = token ? token : serverConfig.stabilityApiKey;
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
message: `missing STABILITY_API_KEY in server env vars`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 401,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchUrl = `${baseUrl}/${path}`;
|
||||||
|
console.log("[Stability Url] ", fetchUrl);
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": req.headers.get("Content-Type") || "multipart/form-data",
|
||||||
|
Accept: req.headers.get("Accept") || "application/json",
|
||||||
|
Authorization: `Bearer ${key}`,
|
||||||
|
},
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
124
app/api/tencent/route.ts
Normal file
124
app/api/tencent/route.ts
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import {
|
||||||
|
TENCENT_BASE_URL,
|
||||||
|
ApiPath,
|
||||||
|
ModelProvider,
|
||||||
|
ServiceProvider,
|
||||||
|
Tencent,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/app/api/auth";
|
||||||
|
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||||
|
import { getHeader } from "@/app/utils/tencent";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
async function handle(
|
||||||
|
req: NextRequest,
|
||||||
|
{ params }: { params: { path: string[] } },
|
||||||
|
) {
|
||||||
|
console.log("[Tencent Route] params ", params);
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const authResult = auth(req, ModelProvider.Hunyuan);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request(req);
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Tencent] ", e);
|
||||||
|
return NextResponse.json(prettyObject(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const GET = handle;
|
||||||
|
export const POST = handle;
|
||||||
|
|
||||||
|
export const runtime = "edge";
|
||||||
|
export const preferredRegion = [
|
||||||
|
"arn1",
|
||||||
|
"bom1",
|
||||||
|
"cdg1",
|
||||||
|
"cle1",
|
||||||
|
"cpt1",
|
||||||
|
"dub1",
|
||||||
|
"fra1",
|
||||||
|
"gru1",
|
||||||
|
"hnd1",
|
||||||
|
"iad1",
|
||||||
|
"icn1",
|
||||||
|
"kix1",
|
||||||
|
"lhr1",
|
||||||
|
"pdx1",
|
||||||
|
"sfo1",
|
||||||
|
"sin1",
|
||||||
|
"syd1",
|
||||||
|
];
|
||||||
|
|
||||||
|
async function request(req: NextRequest) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let baseUrl = serverConfig.tencentUrl || TENCENT_BASE_URL;
|
||||||
|
|
||||||
|
if (!baseUrl.startsWith("http")) {
|
||||||
|
baseUrl = `https://${baseUrl}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Base Url]", baseUrl);
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => {
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
10 * 60 * 1000,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchUrl = baseUrl;
|
||||||
|
|
||||||
|
const body = await req.text();
|
||||||
|
const headers = await getHeader(
|
||||||
|
body,
|
||||||
|
serverConfig.tencentSecretId as string,
|
||||||
|
serverConfig.tencentSecretKey as string,
|
||||||
|
);
|
||||||
|
const fetchOptions: RequestInit = {
|
||||||
|
headers,
|
||||||
|
method: req.method,
|
||||||
|
body,
|
||||||
|
redirect: "manual",
|
||||||
|
// @ts-ignore
|
||||||
|
duplex: "half",
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(fetchUrl, fetchOptions);
|
||||||
|
|
||||||
|
// to prevent browser prompt for credentials
|
||||||
|
const newHeaders = new Headers(res.headers);
|
||||||
|
newHeaders.delete("www-authenticate");
|
||||||
|
// to disable nginx buffering
|
||||||
|
newHeaders.set("X-Accel-Buffering", "no");
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,14 @@ const mergedAllowedWebDavEndpoints = [
|
|||||||
...config.allowedWebDevEndpoints,
|
...config.allowedWebDevEndpoints,
|
||||||
].filter((domain) => Boolean(domain.trim()));
|
].filter((domain) => Boolean(domain.trim()));
|
||||||
|
|
||||||
|
const normalizeUrl = (url: string) => {
|
||||||
|
try {
|
||||||
|
return new URL(url);
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
async function handle(
|
async function handle(
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
{ params }: { params: { path: string[] } },
|
{ params }: { params: { path: string[] } },
|
||||||
@@ -21,12 +29,23 @@ async function handle(
|
|||||||
|
|
||||||
const requestUrl = new URL(req.url);
|
const requestUrl = new URL(req.url);
|
||||||
let endpoint = requestUrl.searchParams.get("endpoint");
|
let endpoint = requestUrl.searchParams.get("endpoint");
|
||||||
|
let proxy_method = requestUrl.searchParams.get("proxy_method") || req.method;
|
||||||
|
|
||||||
// Validate the endpoint to prevent potential SSRF attacks
|
// Validate the endpoint to prevent potential SSRF attacks
|
||||||
if (
|
if (
|
||||||
!mergedAllowedWebDavEndpoints.some(
|
!endpoint ||
|
||||||
(allowedEndpoint) => endpoint?.startsWith(allowedEndpoint),
|
!mergedAllowedWebDavEndpoints.some((allowedEndpoint) => {
|
||||||
)
|
const normalizedAllowedEndpoint = normalizeUrl(allowedEndpoint);
|
||||||
|
const normalizedEndpoint = normalizeUrl(endpoint as string);
|
||||||
|
|
||||||
|
return (
|
||||||
|
normalizedEndpoint &&
|
||||||
|
normalizedEndpoint.hostname === normalizedAllowedEndpoint?.hostname &&
|
||||||
|
normalizedEndpoint.pathname.startsWith(
|
||||||
|
normalizedAllowedEndpoint.pathname,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
})
|
||||||
) {
|
) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
@@ -47,7 +66,11 @@ async function handle(
|
|||||||
const targetPath = `${endpoint}${endpointPath}`;
|
const targetPath = `${endpoint}${endpointPath}`;
|
||||||
|
|
||||||
// only allow MKCOL, GET, PUT
|
// only allow MKCOL, GET, PUT
|
||||||
if (req.method !== "MKCOL" && req.method !== "GET" && req.method !== "PUT") {
|
if (
|
||||||
|
proxy_method !== "MKCOL" &&
|
||||||
|
proxy_method !== "GET" &&
|
||||||
|
proxy_method !== "PUT"
|
||||||
|
) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -60,7 +83,7 @@ async function handle(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// for MKCOL request, only allow request ${folder}
|
// for MKCOL request, only allow request ${folder}
|
||||||
if (req.method === "MKCOL" && !targetPath.endsWith(folder)) {
|
if (proxy_method === "MKCOL" && !targetPath.endsWith(folder)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -73,7 +96,7 @@ async function handle(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// for GET request, only allow request ending with fileName
|
// for GET request, only allow request ending with fileName
|
||||||
if (req.method === "GET" && !targetPath.endsWith(fileName)) {
|
if (proxy_method === "GET" && !targetPath.endsWith(fileName)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -86,7 +109,7 @@ async function handle(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// for PUT request, only allow request ending with fileName
|
// for PUT request, only allow request ending with fileName
|
||||||
if (req.method === "PUT" && !targetPath.endsWith(fileName)) {
|
if (proxy_method === "PUT" && !targetPath.endsWith(fileName)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: true,
|
error: true,
|
||||||
@@ -100,7 +123,7 @@ async function handle(
|
|||||||
|
|
||||||
const targetUrl = targetPath;
|
const targetUrl = targetPath;
|
||||||
|
|
||||||
const method = req.method;
|
const method = proxy_method || req.method;
|
||||||
const shouldNotHaveBody = ["get", "head"].includes(
|
const shouldNotHaveBody = ["get", "head"].includes(
|
||||||
method?.toLowerCase() ?? "",
|
method?.toLowerCase() ?? "",
|
||||||
);
|
);
|
||||||
@@ -125,7 +148,7 @@ async function handle(
|
|||||||
"[Any Proxy]",
|
"[Any Proxy]",
|
||||||
targetUrl,
|
targetUrl,
|
||||||
{
|
{
|
||||||
method: req.method,
|
method: method,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: fetchResult?.status,
|
status: fetchResult?.status,
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
export function makeAzurePath(path: string, apiVersion: string) {
|
|
||||||
// should omit /v1 prefix
|
|
||||||
path = path.replaceAll("v1/", "");
|
|
||||||
|
|
||||||
// should add api-key to query string
|
|
||||||
path += `${path.includes("?") ? "&" : "?"}api-version=${apiVersion}`;
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
@@ -5,10 +5,23 @@ import {
|
|||||||
ModelProvider,
|
ModelProvider,
|
||||||
ServiceProvider,
|
ServiceProvider,
|
||||||
} from "../constant";
|
} from "../constant";
|
||||||
import { ChatMessage, ModelType, useAccessStore, useChatStore } from "../store";
|
import {
|
||||||
import { ChatGPTApi } from "./platforms/openai";
|
ChatMessageTool,
|
||||||
|
ChatMessage,
|
||||||
|
ModelType,
|
||||||
|
useAccessStore,
|
||||||
|
useChatStore,
|
||||||
|
} from "../store";
|
||||||
|
import { ChatGPTApi, DalleRequestPayload } from "./platforms/openai";
|
||||||
import { GeminiProApi } from "./platforms/google";
|
import { GeminiProApi } from "./platforms/google";
|
||||||
import { ClaudeApi } from "./platforms/anthropic";
|
import { ClaudeApi } from "./platforms/anthropic";
|
||||||
|
import { ErnieApi } from "./platforms/baidu";
|
||||||
|
import { DoubaoApi } from "./platforms/bytedance";
|
||||||
|
import { QwenApi } from "./platforms/alibaba";
|
||||||
|
import { HunyuanApi } from "./platforms/tencent";
|
||||||
|
import { MoonshotApi } from "./platforms/moonshot";
|
||||||
|
import { SparkApi } from "./platforms/iflytek";
|
||||||
|
|
||||||
export const ROLES = ["system", "user", "assistant"] as const;
|
export const ROLES = ["system", "user", "assistant"] as const;
|
||||||
export type MessageRole = (typeof ROLES)[number];
|
export type MessageRole = (typeof ROLES)[number];
|
||||||
|
|
||||||
@@ -30,11 +43,15 @@ export interface RequestMessage {
|
|||||||
|
|
||||||
export interface LLMConfig {
|
export interface LLMConfig {
|
||||||
model: string;
|
model: string;
|
||||||
|
providerName?: string;
|
||||||
temperature?: number;
|
temperature?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
presence_penalty?: number;
|
presence_penalty?: number;
|
||||||
frequency_penalty?: number;
|
frequency_penalty?: number;
|
||||||
|
size?: DalleRequestPayload["size"];
|
||||||
|
quality?: DalleRequestPayload["quality"];
|
||||||
|
style?: DalleRequestPayload["style"];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChatOptions {
|
export interface ChatOptions {
|
||||||
@@ -45,6 +62,8 @@ export interface ChatOptions {
|
|||||||
onFinish: (message: string) => void;
|
onFinish: (message: string) => void;
|
||||||
onError?: (err: Error) => void;
|
onError?: (err: Error) => void;
|
||||||
onController?: (controller: AbortController) => void;
|
onController?: (controller: AbortController) => void;
|
||||||
|
onBeforeTool?: (tool: ChatMessageTool) => void;
|
||||||
|
onAfterTool?: (tool: ChatMessageTool) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LLMUsage {
|
export interface LLMUsage {
|
||||||
@@ -54,14 +73,17 @@ export interface LLMUsage {
|
|||||||
|
|
||||||
export interface LLMModel {
|
export interface LLMModel {
|
||||||
name: string;
|
name: string;
|
||||||
|
displayName?: string;
|
||||||
available: boolean;
|
available: boolean;
|
||||||
provider: LLMModelProvider;
|
provider: LLMModelProvider;
|
||||||
|
sorted: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LLMModelProvider {
|
export interface LLMModelProvider {
|
||||||
id: string;
|
id: string;
|
||||||
providerName: string;
|
providerName: string;
|
||||||
providerType: string;
|
providerType: string;
|
||||||
|
sorted: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class LLMApi {
|
export abstract class LLMApi {
|
||||||
@@ -102,6 +124,24 @@ export class ClientApi {
|
|||||||
case ModelProvider.Claude:
|
case ModelProvider.Claude:
|
||||||
this.llm = new ClaudeApi();
|
this.llm = new ClaudeApi();
|
||||||
break;
|
break;
|
||||||
|
case ModelProvider.Ernie:
|
||||||
|
this.llm = new ErnieApi();
|
||||||
|
break;
|
||||||
|
case ModelProvider.Doubao:
|
||||||
|
this.llm = new DoubaoApi();
|
||||||
|
break;
|
||||||
|
case ModelProvider.Qwen:
|
||||||
|
this.llm = new QwenApi();
|
||||||
|
break;
|
||||||
|
case ModelProvider.Hunyuan:
|
||||||
|
this.llm = new HunyuanApi();
|
||||||
|
break;
|
||||||
|
case ModelProvider.Moonshot:
|
||||||
|
this.llm = new MoonshotApi();
|
||||||
|
break;
|
||||||
|
case ModelProvider.Iflytek:
|
||||||
|
this.llm = new SparkApi();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
this.llm = new ChatGPTApi();
|
this.llm = new ChatGPTApi();
|
||||||
}
|
}
|
||||||
@@ -153,39 +193,122 @@ export class ClientApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getBearerToken(
|
||||||
|
apiKey: string,
|
||||||
|
noBearer: boolean = false,
|
||||||
|
): string {
|
||||||
|
return validString(apiKey)
|
||||||
|
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
|
||||||
|
: "";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validString(x: string): boolean {
|
||||||
|
return x?.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
export function getHeaders() {
|
export function getHeaders() {
|
||||||
const accessStore = useAccessStore.getState();
|
const accessStore = useAccessStore.getState();
|
||||||
|
const chatStore = useChatStore.getState();
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
};
|
};
|
||||||
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
|
|
||||||
const isGoogle = modelConfig.model.startsWith("gemini");
|
|
||||||
const isAzure = accessStore.provider === ServiceProvider.Azure;
|
|
||||||
const authHeader = isAzure ? "api-key" : "Authorization";
|
|
||||||
const apiKey = isGoogle
|
|
||||||
? accessStore.googleApiKey
|
|
||||||
: isAzure
|
|
||||||
? accessStore.azureApiKey
|
|
||||||
: accessStore.openaiApiKey;
|
|
||||||
const clientConfig = getClientConfig();
|
|
||||||
const makeBearer = (s: string) => `${isAzure ? "" : "Bearer "}${s.trim()}`;
|
|
||||||
const validString = (x: string) => x && x.length > 0;
|
|
||||||
|
|
||||||
|
const clientConfig = getClientConfig();
|
||||||
|
|
||||||
|
function getConfig() {
|
||||||
|
const modelConfig = chatStore.currentSession().mask.modelConfig;
|
||||||
|
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
|
||||||
|
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
|
||||||
|
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
|
||||||
|
const isBaidu = modelConfig.providerName == ServiceProvider.Baidu;
|
||||||
|
const isByteDance = modelConfig.providerName === ServiceProvider.ByteDance;
|
||||||
|
const isAlibaba = modelConfig.providerName === ServiceProvider.Alibaba;
|
||||||
|
const isMoonshot = modelConfig.providerName === ServiceProvider.Moonshot;
|
||||||
|
const isIflytek = modelConfig.providerName === ServiceProvider.Iflytek;
|
||||||
|
const isEnabledAccessControl = accessStore.enabledAccessControl();
|
||||||
|
const apiKey = isGoogle
|
||||||
|
? accessStore.googleApiKey
|
||||||
|
: isAzure
|
||||||
|
? accessStore.azureApiKey
|
||||||
|
: isAnthropic
|
||||||
|
? accessStore.anthropicApiKey
|
||||||
|
: isByteDance
|
||||||
|
? accessStore.bytedanceApiKey
|
||||||
|
: isAlibaba
|
||||||
|
? accessStore.alibabaApiKey
|
||||||
|
: isMoonshot
|
||||||
|
? accessStore.moonshotApiKey
|
||||||
|
: isIflytek
|
||||||
|
? accessStore.iflytekApiKey && accessStore.iflytekApiSecret
|
||||||
|
? accessStore.iflytekApiKey + ":" + accessStore.iflytekApiSecret
|
||||||
|
: ""
|
||||||
|
: accessStore.openaiApiKey;
|
||||||
|
return {
|
||||||
|
isGoogle,
|
||||||
|
isAzure,
|
||||||
|
isAnthropic,
|
||||||
|
isBaidu,
|
||||||
|
isByteDance,
|
||||||
|
isAlibaba,
|
||||||
|
isMoonshot,
|
||||||
|
isIflytek,
|
||||||
|
apiKey,
|
||||||
|
isEnabledAccessControl,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAuthHeader(): string {
|
||||||
|
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
isGoogle,
|
||||||
|
isAzure,
|
||||||
|
isAnthropic,
|
||||||
|
isBaidu,
|
||||||
|
apiKey,
|
||||||
|
isEnabledAccessControl,
|
||||||
|
} = getConfig();
|
||||||
// when using google api in app, not set auth header
|
// when using google api in app, not set auth header
|
||||||
if (!(isGoogle && clientConfig?.isApp)) {
|
if (isGoogle && clientConfig?.isApp) return headers;
|
||||||
// use user's api key first
|
// when using baidu api in app, not set auth header
|
||||||
if (validString(apiKey)) {
|
if (isBaidu && clientConfig?.isApp) return headers;
|
||||||
headers[authHeader] = makeBearer(apiKey);
|
|
||||||
} else if (
|
const authHeader = getAuthHeader();
|
||||||
accessStore.enabledAccessControl() &&
|
|
||||||
validString(accessStore.accessCode)
|
const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
|
||||||
) {
|
|
||||||
headers[authHeader] = makeBearer(
|
if (bearerToken) {
|
||||||
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
headers[authHeader] = bearerToken;
|
||||||
);
|
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
|
||||||
}
|
headers["Authorization"] = getBearerToken(
|
||||||
|
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getClientApi(provider: ServiceProvider): ClientApi {
|
||||||
|
switch (provider) {
|
||||||
|
case ServiceProvider.Google:
|
||||||
|
return new ClientApi(ModelProvider.GeminiPro);
|
||||||
|
case ServiceProvider.Anthropic:
|
||||||
|
return new ClientApi(ModelProvider.Claude);
|
||||||
|
case ServiceProvider.Baidu:
|
||||||
|
return new ClientApi(ModelProvider.Ernie);
|
||||||
|
case ServiceProvider.ByteDance:
|
||||||
|
return new ClientApi(ModelProvider.Doubao);
|
||||||
|
case ServiceProvider.Alibaba:
|
||||||
|
return new ClientApi(ModelProvider.Qwen);
|
||||||
|
case ServiceProvider.Tencent:
|
||||||
|
return new ClientApi(ModelProvider.Hunyuan);
|
||||||
|
case ServiceProvider.Moonshot:
|
||||||
|
return new ClientApi(ModelProvider.Moonshot);
|
||||||
|
case ServiceProvider.Iflytek:
|
||||||
|
return new ClientApi(ModelProvider.Iflytek);
|
||||||
|
default:
|
||||||
|
return new ClientApi(ModelProvider.GPT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
export * from "./types";
|
|
||||||
|
|
||||||
export * from "./locale";
|
|
||||||
|
|
||||||
export * from "./utils";
|
|
||||||
|
|
||||||
export const modelNameRequestHeader = "x-nextchat-model-name";
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { Lang, getLang } from "@/app/locales";
|
|
||||||
|
|
||||||
interface PlainConfig {
|
|
||||||
[k: string]: PlainConfig | string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type LocaleMap<
|
|
||||||
TextPlainConfig extends PlainConfig,
|
|
||||||
Default extends Lang,
|
|
||||||
> = Partial<Record<Lang, TextPlainConfig>> & {
|
|
||||||
[name in Default]: TextPlainConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getLocaleText<
|
|
||||||
TextPlainConfig extends PlainConfig,
|
|
||||||
DefaultLang extends Lang,
|
|
||||||
>(textMap: LocaleMap<TextPlainConfig, DefaultLang>, defaultLang: DefaultLang) {
|
|
||||||
return textMap[getLang()] || textMap[defaultLang];
|
|
||||||
}
|
|
||||||
@@ -1,211 +0,0 @@
|
|||||||
import { RequestMessage } from "../api";
|
|
||||||
import { getServerSideConfig } from "@/app/config/server";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export { type RequestMessage };
|
|
||||||
|
|
||||||
// ===================================== LLM Types start ======================================
|
|
||||||
|
|
||||||
export interface ModelConfig {
|
|
||||||
temperature: number;
|
|
||||||
top_p: number;
|
|
||||||
presence_penalty: number;
|
|
||||||
frequency_penalty: number;
|
|
||||||
max_tokens: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ModelSettings extends Omit<ModelConfig, "max_tokens"> {
|
|
||||||
global_max_tokens: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ModelTemplate = {
|
|
||||||
name: string; // id of model in a provider
|
|
||||||
displayName: string;
|
|
||||||
isVisionModel?: boolean;
|
|
||||||
isDefaultActive: boolean; // model is initialized to be active
|
|
||||||
isDefaultSelected?: boolean; // model is initialized to be as default used model
|
|
||||||
max_tokens?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface Model extends Omit<ModelTemplate, "isDefaultActive"> {
|
|
||||||
providerTemplateName: string;
|
|
||||||
isActive: boolean;
|
|
||||||
providerName: string;
|
|
||||||
available: boolean;
|
|
||||||
customized: boolean; // Only customized model is allowed to be modified
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ModelInfo extends Pick<ModelTemplate, "name"> {
|
|
||||||
[k: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===================================== LLM Types end ======================================
|
|
||||||
|
|
||||||
// ===================================== Chat Request Types start ======================================
|
|
||||||
|
|
||||||
export interface ChatRequestPayload {
|
|
||||||
messages: RequestMessage[];
|
|
||||||
context: {
|
|
||||||
isApp: boolean;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface StandChatRequestPayload extends ChatRequestPayload {
|
|
||||||
modelConfig: ModelConfig;
|
|
||||||
model: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InternalChatRequestPayload<SettingKeys extends string = "">
|
|
||||||
extends StandChatRequestPayload {
|
|
||||||
providerConfig: Partial<Record<SettingKeys, string>>;
|
|
||||||
isVisionModel: Model["isVisionModel"];
|
|
||||||
stream: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ProviderRequestPayload {
|
|
||||||
headers: Record<string, string>;
|
|
||||||
body: string;
|
|
||||||
url: string;
|
|
||||||
method: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InternalChatHandlers {
|
|
||||||
onProgress: (message: string, chunk: string) => void;
|
|
||||||
onFinish: (message: string) => void;
|
|
||||||
onError: (err: Error) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ChatHandlers extends InternalChatHandlers {
|
|
||||||
onProgress: (chunk: string) => void;
|
|
||||||
onFinish: () => void;
|
|
||||||
onFlash: (message: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===================================== Chat Request Types end ======================================
|
|
||||||
|
|
||||||
// ===================================== Chat Response Types start ======================================
|
|
||||||
|
|
||||||
export interface StandChatReponseMessage {
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===================================== Chat Request Types end ======================================
|
|
||||||
|
|
||||||
// ===================================== Provider Settings Types start ======================================
|
|
||||||
|
|
||||||
type NumberRange = [number, number];
|
|
||||||
|
|
||||||
export type Validator =
|
|
||||||
| "required"
|
|
||||||
| "number"
|
|
||||||
| "string"
|
|
||||||
| NumberRange
|
|
||||||
| NumberRange[]
|
|
||||||
| ((v: any) => Promise<string | void>);
|
|
||||||
|
|
||||||
export type CommonSettingItem<SettingKeys extends string> = {
|
|
||||||
name: SettingKeys;
|
|
||||||
title?: string;
|
|
||||||
description?: string;
|
|
||||||
validators?: Validator[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type InputSettingItem = {
|
|
||||||
type: "input";
|
|
||||||
placeholder?: string;
|
|
||||||
} & (
|
|
||||||
| {
|
|
||||||
inputType?: "password" | "normal";
|
|
||||||
defaultValue?: string;
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
inputType?: "number";
|
|
||||||
defaultValue?: number;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export type SelectSettingItem = {
|
|
||||||
type: "select";
|
|
||||||
options: {
|
|
||||||
name: string;
|
|
||||||
value: "number" | "string" | "boolean";
|
|
||||||
}[];
|
|
||||||
placeholder?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type RangeSettingItem = {
|
|
||||||
type: "range";
|
|
||||||
range: NumberRange;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SwitchSettingItem = {
|
|
||||||
type: "switch";
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SettingItem<SettingKeys extends string = ""> =
|
|
||||||
CommonSettingItem<SettingKeys> &
|
|
||||||
(
|
|
||||||
| InputSettingItem
|
|
||||||
| SelectSettingItem
|
|
||||||
| RangeSettingItem
|
|
||||||
| SwitchSettingItem
|
|
||||||
);
|
|
||||||
|
|
||||||
// ===================================== Provider Settings Types end ======================================
|
|
||||||
|
|
||||||
// ===================================== Provider Template Types start ======================================
|
|
||||||
|
|
||||||
export type ServerConfig = ReturnType<typeof getServerSideConfig>;
|
|
||||||
|
|
||||||
export interface IProviderTemplate<
|
|
||||||
SettingKeys extends string,
|
|
||||||
NAME extends string,
|
|
||||||
Meta extends Record<string, any>,
|
|
||||||
> {
|
|
||||||
readonly name: NAME;
|
|
||||||
|
|
||||||
readonly apiRouteRootName: `/api/provider/${NAME}`;
|
|
||||||
|
|
||||||
readonly allowedApiMethods: Array<
|
|
||||||
"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS"
|
|
||||||
>;
|
|
||||||
|
|
||||||
readonly metas: Meta;
|
|
||||||
|
|
||||||
readonly providerMeta: {
|
|
||||||
displayName: string;
|
|
||||||
settingItems: SettingItem<SettingKeys>[];
|
|
||||||
};
|
|
||||||
readonly defaultModels: ModelTemplate[];
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
): AbortController;
|
|
||||||
|
|
||||||
chat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
): Promise<StandChatReponseMessage>;
|
|
||||||
|
|
||||||
getAvailableModels?(
|
|
||||||
providerConfig: InternalChatRequestPayload<SettingKeys>["providerConfig"],
|
|
||||||
): Promise<ModelInfo[]>;
|
|
||||||
|
|
||||||
readonly runtime: "edge";
|
|
||||||
readonly preferredRegion: "auto" | "global" | "home" | string | string[];
|
|
||||||
|
|
||||||
serverSideRequestHandler(
|
|
||||||
req: NextRequest & {
|
|
||||||
subpath: string;
|
|
||||||
},
|
|
||||||
serverConfig: ServerConfig,
|
|
||||||
): Promise<NextResponse>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ProviderTemplate = IProviderTemplate<any, any, any>;
|
|
||||||
|
|
||||||
export interface Serializable<Snapshot> {
|
|
||||||
serialize(): Snapshot;
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import { RequestMessage, ServerConfig } from "./types";
|
|
||||||
import { cloneDeep } from "lodash-es";
|
|
||||||
|
|
||||||
export function getMessageTextContent(message: RequestMessage) {
|
|
||||||
if (typeof message.content === "string") {
|
|
||||||
return message.content;
|
|
||||||
}
|
|
||||||
for (const c of message.content) {
|
|
||||||
if (c.type === "text") {
|
|
||||||
return c.text ?? "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getMessageImages(message: RequestMessage): string[] {
|
|
||||||
if (typeof message.content === "string") {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
const urls: string[] = [];
|
|
||||||
for (const c of message.content) {
|
|
||||||
if (c.type === "image_url") {
|
|
||||||
urls.push(c.image_url?.url ?? "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getIP(req: NextRequest) {
|
|
||||||
let ip = req.ip ?? req.headers.get("x-real-ip");
|
|
||||||
const forwardedFor = req.headers.get("x-forwarded-for");
|
|
||||||
|
|
||||||
if (!ip && forwardedFor) {
|
|
||||||
ip = forwardedFor.split(",").at(0) ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatUrl(baseUrl?: string) {
|
|
||||||
if (baseUrl && !baseUrl.startsWith("http")) {
|
|
||||||
baseUrl = `https://${baseUrl}`;
|
|
||||||
}
|
|
||||||
if (baseUrl?.endsWith("/")) {
|
|
||||||
baseUrl = baseUrl.slice(0, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return baseUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
function travel(
|
|
||||||
config: ServerConfig,
|
|
||||||
keys: Array<keyof ServerConfig>,
|
|
||||||
handle: (prop: any) => any,
|
|
||||||
): ServerConfig {
|
|
||||||
const copiedConfig = cloneDeep(config);
|
|
||||||
keys.forEach((k) => {
|
|
||||||
copiedConfig[k] = handle(copiedConfig[k] as string) as never;
|
|
||||||
});
|
|
||||||
return copiedConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const makeUrlsUsable = (
|
|
||||||
config: ServerConfig,
|
|
||||||
keys: Array<keyof ServerConfig>,
|
|
||||||
) => travel(config, keys, formatUrl);
|
|
||||||
|
|
||||||
export const disableSystemApiKey = (
|
|
||||||
config: ServerConfig,
|
|
||||||
keys: Array<keyof ServerConfig>,
|
|
||||||
forbidden: boolean,
|
|
||||||
) =>
|
|
||||||
travel(config, keys, (p) => {
|
|
||||||
return forbidden ? undefined : p;
|
|
||||||
});
|
|
||||||
|
|
||||||
export function isSameOrigin(requestUrl: string) {
|
|
||||||
var a = document.createElement("a");
|
|
||||||
a.href = requestUrl;
|
|
||||||
|
|
||||||
// 检查协议、主机名和端口号是否与当前页面相同
|
|
||||||
return (
|
|
||||||
a.protocol === window.location.protocol &&
|
|
||||||
a.hostname === window.location.hostname &&
|
|
||||||
a.port === window.location.port
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
export * from "./shim";
|
|
||||||
|
|
||||||
export * from "../common/types";
|
|
||||||
|
|
||||||
export * from "./providerClient";
|
|
||||||
|
|
||||||
export * from "./modelClient";
|
|
||||||
|
|
||||||
export * from "../common/locale";
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
import {
|
|
||||||
ChatRequestPayload,
|
|
||||||
Model,
|
|
||||||
ModelSettings,
|
|
||||||
InternalChatHandlers,
|
|
||||||
} from "../common";
|
|
||||||
import { Provider, ProviderClient } from "./providerClient";
|
|
||||||
|
|
||||||
export class ModelClient {
|
|
||||||
constructor(
|
|
||||||
private model: Model,
|
|
||||||
private modelSettings: ModelSettings,
|
|
||||||
private providerClient: ProviderClient,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
chat(payload: ChatRequestPayload, handlers: InternalChatHandlers) {
|
|
||||||
try {
|
|
||||||
return this.providerClient.streamChat(
|
|
||||||
{
|
|
||||||
...payload,
|
|
||||||
modelConfig: {
|
|
||||||
...this.modelSettings,
|
|
||||||
max_tokens:
|
|
||||||
this.model.max_tokens ?? this.modelSettings.global_max_tokens,
|
|
||||||
},
|
|
||||||
model: this.model.name,
|
|
||||||
},
|
|
||||||
handlers,
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
handlers.onError(e as Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
summerize(payload: ChatRequestPayload) {
|
|
||||||
try {
|
|
||||||
return this.providerClient.chat({
|
|
||||||
...payload,
|
|
||||||
modelConfig: {
|
|
||||||
...this.modelSettings,
|
|
||||||
max_tokens:
|
|
||||||
this.model.max_tokens ?? this.modelSettings.global_max_tokens,
|
|
||||||
},
|
|
||||||
model: this.model.name,
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// must generate new ModelClient during every chat
|
|
||||||
export function ModelClientFactory(
|
|
||||||
model: Model,
|
|
||||||
provider: Provider,
|
|
||||||
modelSettings: ModelSettings,
|
|
||||||
) {
|
|
||||||
const providerClient = new ProviderClient(provider);
|
|
||||||
return new ModelClient(model, modelSettings, providerClient);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getFiltertModels(
|
|
||||||
models: readonly Model[],
|
|
||||||
customModels: string,
|
|
||||||
) {
|
|
||||||
const modelTable: Record<string, Model> = {};
|
|
||||||
|
|
||||||
// default models
|
|
||||||
models.forEach((m) => {
|
|
||||||
modelTable[m.name] = m;
|
|
||||||
});
|
|
||||||
|
|
||||||
// server custom models
|
|
||||||
customModels
|
|
||||||
.split(",")
|
|
||||||
.filter((v) => !!v && v.length > 0)
|
|
||||||
.forEach((m) => {
|
|
||||||
const available = !m.startsWith("-");
|
|
||||||
const nameConfig =
|
|
||||||
m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
|
|
||||||
const [name, displayName] = nameConfig.split("=");
|
|
||||||
|
|
||||||
// enable or disable all models
|
|
||||||
if (name === "all") {
|
|
||||||
Object.values(modelTable).forEach(
|
|
||||||
(model) => (model.available = available),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
modelTable[name] = {
|
|
||||||
...modelTable[name],
|
|
||||||
displayName,
|
|
||||||
available,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return modelTable;
|
|
||||||
}
|
|
||||||
@@ -1,256 +0,0 @@
|
|||||||
import {
|
|
||||||
IProviderTemplate,
|
|
||||||
InternalChatHandlers,
|
|
||||||
Model,
|
|
||||||
ModelTemplate,
|
|
||||||
ProviderTemplate,
|
|
||||||
StandChatReponseMessage,
|
|
||||||
StandChatRequestPayload,
|
|
||||||
isSameOrigin,
|
|
||||||
modelNameRequestHeader,
|
|
||||||
} from "../common";
|
|
||||||
import * as ProviderTemplates from "@/app/client/providers";
|
|
||||||
import { nanoid } from "nanoid";
|
|
||||||
|
|
||||||
export type ProviderTemplateName =
|
|
||||||
(typeof ProviderTemplates)[keyof typeof ProviderTemplates]["prototype"]["name"];
|
|
||||||
|
|
||||||
export interface Provider<
|
|
||||||
Providerconfig extends Record<string, any> = Record<string, any>,
|
|
||||||
> {
|
|
||||||
name: string; // id of provider
|
|
||||||
isActive: boolean;
|
|
||||||
providerTemplateName: ProviderTemplateName;
|
|
||||||
providerConfig: Providerconfig;
|
|
||||||
isDefault: boolean; // Not allow to modify models of default provider
|
|
||||||
updated: boolean; // provider initial is finished
|
|
||||||
|
|
||||||
displayName: string;
|
|
||||||
models: Model[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const providerTemplates = Object.values(ProviderTemplates).reduce(
|
|
||||||
(r, t) => ({
|
|
||||||
...r,
|
|
||||||
[t.prototype.name]: new t(),
|
|
||||||
}),
|
|
||||||
{} as Record<ProviderTemplateName, ProviderTemplate>,
|
|
||||||
);
|
|
||||||
|
|
||||||
export class ProviderClient {
|
|
||||||
providerTemplate: IProviderTemplate<any, any, any>;
|
|
||||||
genFetch: (modelName: string) => typeof window.fetch;
|
|
||||||
|
|
||||||
static ProviderTemplates = providerTemplates;
|
|
||||||
|
|
||||||
static getAllProviderTemplates = () => {
|
|
||||||
return Object.values(providerTemplates).reduce(
|
|
||||||
(r, t) => ({
|
|
||||||
...r,
|
|
||||||
[t.name]: t,
|
|
||||||
}),
|
|
||||||
{} as Record<ProviderTemplateName, ProviderTemplate>,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
static getProviderTemplateMetaList = () => {
|
|
||||||
return Object.values(providerTemplates).map((t) => ({
|
|
||||||
...t.providerMeta,
|
|
||||||
name: t.name,
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor(private provider: Provider) {
|
|
||||||
const { providerTemplateName } = provider;
|
|
||||||
this.providerTemplate = this.getProviderTemplate(providerTemplateName);
|
|
||||||
this.genFetch =
|
|
||||||
(modelName: string) =>
|
|
||||||
(...args) => {
|
|
||||||
const req = new Request(...args);
|
|
||||||
const headers: Record<string, any> = {
|
|
||||||
...req.headers,
|
|
||||||
};
|
|
||||||
if (isSameOrigin(req.url)) {
|
|
||||||
headers[modelNameRequestHeader] = modelName;
|
|
||||||
}
|
|
||||||
|
|
||||||
return window.fetch(req.url, {
|
|
||||||
method: req.method,
|
|
||||||
keepalive: req.keepalive,
|
|
||||||
headers,
|
|
||||||
body: req.body,
|
|
||||||
redirect: req.redirect,
|
|
||||||
integrity: req.integrity,
|
|
||||||
signal: req.signal,
|
|
||||||
credentials: req.credentials,
|
|
||||||
mode: req.mode,
|
|
||||||
referrer: req.referrer,
|
|
||||||
referrerPolicy: req.referrerPolicy,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private getProviderTemplate(providerTemplateName: string) {
|
|
||||||
const providerTemplate = Object.values(providerTemplates).find(
|
|
||||||
(template) => template.name === providerTemplateName,
|
|
||||||
);
|
|
||||||
|
|
||||||
return providerTemplate || providerTemplates.openai;
|
|
||||||
}
|
|
||||||
|
|
||||||
private getModelConfig(modelName: string) {
|
|
||||||
const { models } = this.provider;
|
|
||||||
return (
|
|
||||||
models.find((m) => m.name === modelName) ||
|
|
||||||
models.find((m) => m.isDefaultSelected)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
getAvailableModels() {
|
|
||||||
return Promise.resolve(
|
|
||||||
this.providerTemplate.getAvailableModels?.(this.provider.providerConfig),
|
|
||||||
)
|
|
||||||
.then((res) => {
|
|
||||||
const { defaultModels } = this.providerTemplate;
|
|
||||||
const availableModelsSet = new Set(
|
|
||||||
(res ?? defaultModels).map((o) => o.name),
|
|
||||||
);
|
|
||||||
return defaultModels.filter((m) => availableModelsSet.has(m.name));
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
return this.providerTemplate.defaultModels;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: StandChatRequestPayload,
|
|
||||||
): Promise<StandChatReponseMessage> {
|
|
||||||
return this.providerTemplate.chat(
|
|
||||||
{
|
|
||||||
...payload,
|
|
||||||
stream: false,
|
|
||||||
isVisionModel: this.getModelConfig(payload.model)?.isVisionModel,
|
|
||||||
providerConfig: this.provider.providerConfig,
|
|
||||||
},
|
|
||||||
this.genFetch(payload.model),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(payload: StandChatRequestPayload, handlers: InternalChatHandlers) {
|
|
||||||
let responseText = "";
|
|
||||||
let remainText = "";
|
|
||||||
|
|
||||||
const timer = this.providerTemplate.streamChat(
|
|
||||||
{
|
|
||||||
...payload,
|
|
||||||
stream: true,
|
|
||||||
isVisionModel: this.getModelConfig(payload.model)?.isVisionModel,
|
|
||||||
providerConfig: this.provider.providerConfig,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
onProgress: (chunk) => {
|
|
||||||
remainText += chunk;
|
|
||||||
},
|
|
||||||
onError: (err) => {
|
|
||||||
handlers.onError(err);
|
|
||||||
},
|
|
||||||
onFinish: () => {},
|
|
||||||
onFlash: (message: string) => {
|
|
||||||
handlers.onFinish(message);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
this.genFetch(payload.model),
|
|
||||||
);
|
|
||||||
|
|
||||||
timer.signal.onabort = () => {
|
|
||||||
const message = responseText + remainText;
|
|
||||||
remainText = "";
|
|
||||||
handlers.onFinish(message);
|
|
||||||
};
|
|
||||||
|
|
||||||
const animateResponseText = () => {
|
|
||||||
if (remainText.length > 0) {
|
|
||||||
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
|
||||||
const fetchText = remainText.slice(0, fetchCount);
|
|
||||||
responseText += fetchText;
|
|
||||||
remainText = remainText.slice(fetchCount);
|
|
||||||
handlers.onProgress(responseText, fetchText);
|
|
||||||
}
|
|
||||||
|
|
||||||
requestAnimationFrame(animateResponseText);
|
|
||||||
};
|
|
||||||
|
|
||||||
// start animaion
|
|
||||||
animateResponseText();
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Params = Omit<Provider, "providerTemplateName" | "name" | "isDefault">;
|
|
||||||
|
|
||||||
function createProvider(
|
|
||||||
provider: ProviderTemplateName,
|
|
||||||
isDefault: true,
|
|
||||||
): Provider;
|
|
||||||
function createProvider(provider: ProviderTemplate, isDefault: true): Provider;
|
|
||||||
function createProvider(
|
|
||||||
provider: ProviderTemplateName,
|
|
||||||
isDefault: false,
|
|
||||||
params: Params,
|
|
||||||
): Provider;
|
|
||||||
function createProvider(
|
|
||||||
provider: ProviderTemplate,
|
|
||||||
isDefault: false,
|
|
||||||
params: Params,
|
|
||||||
): Provider;
|
|
||||||
function createProvider(
|
|
||||||
provider: ProviderTemplate | ProviderTemplateName,
|
|
||||||
isDefault: boolean,
|
|
||||||
params?: Params,
|
|
||||||
): Provider {
|
|
||||||
let providerTemplate: ProviderTemplate;
|
|
||||||
if (typeof provider === "string") {
|
|
||||||
providerTemplate = ProviderClient.getAllProviderTemplates()[provider];
|
|
||||||
} else {
|
|
||||||
providerTemplate = provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
const name = `${providerTemplate.name}__${nanoid()}`;
|
|
||||||
|
|
||||||
const {
|
|
||||||
displayName = providerTemplate.providerMeta.displayName,
|
|
||||||
models = providerTemplate.defaultModels.map((m) =>
|
|
||||||
createModelFromModelTemplate(m, providerTemplate, name),
|
|
||||||
),
|
|
||||||
providerConfig,
|
|
||||||
} = params ?? {};
|
|
||||||
|
|
||||||
return {
|
|
||||||
name,
|
|
||||||
displayName,
|
|
||||||
isActive: true,
|
|
||||||
models,
|
|
||||||
providerTemplateName: providerTemplate.name,
|
|
||||||
providerConfig: isDefault ? {} : providerConfig!,
|
|
||||||
isDefault,
|
|
||||||
updated: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createModelFromModelTemplate(
|
|
||||||
m: ModelTemplate,
|
|
||||||
p: ProviderTemplate,
|
|
||||||
providerName: string,
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
...m,
|
|
||||||
providerTemplateName: p.name,
|
|
||||||
providerName,
|
|
||||||
isActive: m.isDefaultActive,
|
|
||||||
available: true,
|
|
||||||
customized: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export { createProvider };
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import { getClientConfig } from "@/app/config/client";
|
|
||||||
|
|
||||||
if (!(window.fetch as any).__hijacked__) {
|
|
||||||
let _fetch = window.fetch;
|
|
||||||
|
|
||||||
function fetch(...args: Parameters<typeof _fetch>) {
|
|
||||||
const { isApp } = getClientConfig() || {};
|
|
||||||
|
|
||||||
let fetch: typeof _fetch = _fetch;
|
|
||||||
|
|
||||||
if (isApp) {
|
|
||||||
try {
|
|
||||||
fetch = window.__TAURI__!.http.fetch;
|
|
||||||
} catch (e) {
|
|
||||||
fetch = _fetch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch.__hijacked__ = true;
|
|
||||||
|
|
||||||
window.fetch = fetch;
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export * from "./core";
|
|
||||||
|
|
||||||
export * from "./providers";
|
|
||||||
268
app/client/platforms/alibaba.ts
Normal file
268
app/client/platforms/alibaba.ts
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
"use client";
|
||||||
|
import {
|
||||||
|
ApiPath,
|
||||||
|
Alibaba,
|
||||||
|
ALIBABA_BASE_URL,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatOptions,
|
||||||
|
getHeaders,
|
||||||
|
LLMApi,
|
||||||
|
LLMModel,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent } from "@/app/utils";
|
||||||
|
|
||||||
|
export interface OpenAIListModelResponse {
|
||||||
|
object: string;
|
||||||
|
data: Array<{
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
root: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestInput {
|
||||||
|
messages: {
|
||||||
|
role: "system" | "user" | "assistant";
|
||||||
|
content: string | MultimodalContent[];
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
interface RequestParam {
|
||||||
|
result_format: string;
|
||||||
|
incremental_output?: boolean;
|
||||||
|
temperature: number;
|
||||||
|
repetition_penalty?: number;
|
||||||
|
top_p: number;
|
||||||
|
max_tokens?: number;
|
||||||
|
}
|
||||||
|
interface RequestPayload {
|
||||||
|
model: string;
|
||||||
|
input: RequestInput;
|
||||||
|
parameters: RequestParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class QwenApi implements LLMApi {
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.alibabaUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
baseUrl = isApp ? ALIBABA_BASE_URL : ApiPath.Alibaba;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Alibaba)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
return [baseUrl, path].join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMessage(res: any) {
|
||||||
|
return res?.output?.choices?.at(0)?.message?.content ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const messages = options.messages.map((v) => ({
|
||||||
|
role: v.role,
|
||||||
|
content: getMessageTextContent(v),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const requestPayload: RequestPayload = {
|
||||||
|
model: modelConfig.model,
|
||||||
|
input: {
|
||||||
|
messages,
|
||||||
|
},
|
||||||
|
parameters: {
|
||||||
|
result_format: "message",
|
||||||
|
incremental_output: shouldStream,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
// max_tokens: modelConfig.max_tokens,
|
||||||
|
top_p: modelConfig.top_p === 1 ? 0.99 : modelConfig.top_p, // qwen top_p is should be < 1
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const chatPath = this.path(Alibaba.ChatPath);
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: {
|
||||||
|
...getHeaders(),
|
||||||
|
"X-DashScope-SSE": shouldStream ? "enable" : "disable",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
// animate response to make it looks smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
if (responseText?.length === 0) {
|
||||||
|
options.onError?.(new Error("empty response from server"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||||
|
const fetchText = remainText.slice(0, fetchCount);
|
||||||
|
responseText += fetchText;
|
||||||
|
remainText = remainText.slice(fetchCount);
|
||||||
|
options.onUpdate?.(responseText, fetchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start animaion
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
|
const finish = () => {
|
||||||
|
if (!finished) {
|
||||||
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
controller.signal.onabort = finish;
|
||||||
|
|
||||||
|
fetchEventSource(chatPath, {
|
||||||
|
...chatPayload,
|
||||||
|
async onopen(res) {
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
console.log(
|
||||||
|
"[Alibaba] request response content type: ",
|
||||||
|
contentType,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (contentType?.startsWith("text/plain")) {
|
||||||
|
responseText = await res.clone().text();
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
const responseTexts = [responseText];
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
|
try {
|
||||||
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
responseTexts.push(Locale.Error.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extraInfo) {
|
||||||
|
responseTexts.push(extraInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseText = responseTexts.join("\n\n");
|
||||||
|
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.output.choices as Array<{
|
||||||
|
message: { content: string };
|
||||||
|
}>;
|
||||||
|
const delta = choices[0]?.message?.content;
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = this.extractMessage(resJson);
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export { Alibaba };
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
import { ACCESS_CODE_PREFIX, Anthropic, ApiPath } from "@/app/constant";
|
import { ACCESS_CODE_PREFIX, Anthropic, ApiPath } from "@/app/constant";
|
||||||
import { ChatOptions, LLMApi, MultimodalContent } from "../api";
|
import { ChatOptions, getHeaders, LLMApi, MultimodalContent } from "../api";
|
||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
import {
|
||||||
|
useAccessStore,
|
||||||
|
useAppConfig,
|
||||||
|
useChatStore,
|
||||||
|
usePluginStore,
|
||||||
|
ChatMessageTool,
|
||||||
|
} from "@/app/store";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { DEFAULT_API_HOST } from "@/app/constant";
|
import { DEFAULT_API_HOST } from "@/app/constant";
|
||||||
import { RequestMessage } from "@/app/typing";
|
|
||||||
import {
|
import {
|
||||||
EventStreamContentType,
|
EventStreamContentType,
|
||||||
fetchEventSource,
|
fetchEventSource,
|
||||||
@@ -12,6 +17,9 @@ import {
|
|||||||
import Locale from "../../locales";
|
import Locale from "../../locales";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { getMessageTextContent, isVisionModel } from "@/app/utils";
|
import { getMessageTextContent, isVisionModel } from "@/app/utils";
|
||||||
|
import { preProcessImageContent, stream } from "@/app/utils/chat";
|
||||||
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
|
import { RequestPayload } from "./openai";
|
||||||
|
|
||||||
export type MultiBlockContent = {
|
export type MultiBlockContent = {
|
||||||
type: "image" | "text";
|
type: "image" | "text";
|
||||||
@@ -92,7 +100,12 @@ export class ClaudeApi implements LLMApi {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const messages = [...options.messages];
|
// try get base64image from local cache image_url
|
||||||
|
const messages: ChatOptions["messages"] = [];
|
||||||
|
for (const v of options.messages) {
|
||||||
|
const content = await preProcessImageContent(v.content);
|
||||||
|
messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
|
||||||
const keys = ["system", "user"];
|
const keys = ["system", "user"];
|
||||||
|
|
||||||
@@ -185,113 +198,126 @@ export class ClaudeApi implements LLMApi {
|
|||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
options.onController?.(controller);
|
options.onController?.(controller);
|
||||||
|
|
||||||
const payload = {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify(requestBody),
|
|
||||||
signal: controller.signal,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
"x-api-key": accessStore.anthropicApiKey,
|
|
||||||
"anthropic-version": accessStore.anthropicApiVersion,
|
|
||||||
Authorization: getAuthKey(accessStore.anthropicApiKey),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if (shouldStream) {
|
if (shouldStream) {
|
||||||
try {
|
let index = -1;
|
||||||
const context = {
|
const [tools, funcs] = usePluginStore
|
||||||
text: "",
|
.getState()
|
||||||
finished: false,
|
.getAsTools(
|
||||||
};
|
useChatStore.getState().currentSession().mask?.plugin || [],
|
||||||
|
);
|
||||||
const finish = () => {
|
return stream(
|
||||||
if (!context.finished) {
|
path,
|
||||||
options.onFinish(context.text);
|
requestBody,
|
||||||
context.finished = true;
|
{
|
||||||
}
|
...getHeaders(),
|
||||||
};
|
"anthropic-version": accessStore.anthropicApiVersion,
|
||||||
|
},
|
||||||
controller.signal.onabort = finish;
|
// @ts-ignore
|
||||||
fetchEventSource(path, {
|
tools.map((tool) => ({
|
||||||
...payload,
|
name: tool?.function?.name,
|
||||||
async onopen(res) {
|
description: tool?.function?.description,
|
||||||
const contentType = res.headers.get("content-type");
|
input_schema: tool?.function?.parameters,
|
||||||
console.log("response content type: ", contentType);
|
})),
|
||||||
|
funcs,
|
||||||
if (contentType?.startsWith("text/plain")) {
|
controller,
|
||||||
context.text = await res.clone().text();
|
// parseSSE
|
||||||
return finish();
|
(text: string, runTools: ChatMessageTool[]) => {
|
||||||
}
|
// console.log("parseSSE", text, runTools);
|
||||||
|
let chunkJson:
|
||||||
if (
|
| undefined
|
||||||
!res.ok ||
|
| {
|
||||||
!res.headers
|
type: "content_block_delta" | "content_block_stop";
|
||||||
.get("content-type")
|
content_block?: {
|
||||||
?.startsWith(EventStreamContentType) ||
|
type: "tool_use";
|
||||||
res.status !== 200
|
id: string;
|
||||||
) {
|
name: string;
|
||||||
const responseTexts = [context.text];
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.text = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return finish();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
let chunkJson:
|
|
||||||
| undefined
|
|
||||||
| {
|
|
||||||
type: "content_block_delta" | "content_block_stop";
|
|
||||||
delta?: {
|
|
||||||
type: "text_delta";
|
|
||||||
text: string;
|
|
||||||
};
|
|
||||||
index: number;
|
|
||||||
};
|
};
|
||||||
try {
|
delta?: {
|
||||||
chunkJson = JSON.parse(msg.data);
|
type: "text_delta" | "input_json_delta";
|
||||||
} catch (e) {
|
text?: string;
|
||||||
console.error("[Response] parse error", msg.data);
|
partial_json?: string;
|
||||||
}
|
};
|
||||||
|
index: number;
|
||||||
|
};
|
||||||
|
chunkJson = JSON.parse(text);
|
||||||
|
|
||||||
if (!chunkJson || chunkJson.type === "content_block_stop") {
|
if (chunkJson?.content_block?.type == "tool_use") {
|
||||||
return finish();
|
index += 1;
|
||||||
}
|
const id = chunkJson?.content_block.id;
|
||||||
|
const name = chunkJson?.content_block.name;
|
||||||
const { delta } = chunkJson;
|
runTools.push({
|
||||||
if (delta?.text) {
|
id,
|
||||||
context.text += delta.text;
|
type: "function",
|
||||||
options.onUpdate?.(context.text, delta.text);
|
function: {
|
||||||
}
|
name,
|
||||||
},
|
arguments: "",
|
||||||
onclose() {
|
},
|
||||||
finish();
|
});
|
||||||
},
|
}
|
||||||
onerror(e) {
|
if (
|
||||||
options.onError?.(e);
|
chunkJson?.delta?.type == "input_json_delta" &&
|
||||||
throw e;
|
chunkJson?.delta?.partial_json
|
||||||
},
|
) {
|
||||||
openWhenHidden: true,
|
// @ts-ignore
|
||||||
});
|
runTools[index]["function"]["arguments"] +=
|
||||||
} catch (e) {
|
chunkJson?.delta?.partial_json;
|
||||||
console.error("failed to chat", e);
|
}
|
||||||
options.onError?.(e as Error);
|
return chunkJson?.delta?.text;
|
||||||
}
|
},
|
||||||
|
// processToolMessage, include tool_calls message and tool call results
|
||||||
|
(
|
||||||
|
requestPayload: RequestPayload,
|
||||||
|
toolCallMessage: any,
|
||||||
|
toolCallResult: any[],
|
||||||
|
) => {
|
||||||
|
// reset index value
|
||||||
|
index = -1;
|
||||||
|
// @ts-ignore
|
||||||
|
requestPayload?.messages?.splice(
|
||||||
|
// @ts-ignore
|
||||||
|
requestPayload?.messages?.length,
|
||||||
|
0,
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: toolCallMessage.tool_calls.map(
|
||||||
|
(tool: ChatMessageTool) => ({
|
||||||
|
type: "tool_use",
|
||||||
|
id: tool.id,
|
||||||
|
name: tool?.function?.name,
|
||||||
|
input: tool?.function?.arguments
|
||||||
|
? JSON.parse(tool?.function?.arguments)
|
||||||
|
: {},
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
// @ts-ignore
|
||||||
|
...toolCallResult.map((result) => ({
|
||||||
|
role: "user",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "tool_result",
|
||||||
|
tool_use_id: result.tool_call_id,
|
||||||
|
content: result.content,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
|
const payload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestBody),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: {
|
||||||
|
...getHeaders(), // get common headers
|
||||||
|
"anthropic-version": accessStore.anthropicApiVersion,
|
||||||
|
// do not send `anthropicApiKey` in browser!!!
|
||||||
|
// Authorization: getAuthKey(accessStore.anthropicApiKey),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
controller.signal.onabort = () => options.onFinish("");
|
controller.signal.onabort = () => options.onFinish("");
|
||||||
|
|
||||||
@@ -376,7 +402,8 @@ export class ClaudeApi implements LLMApi {
|
|||||||
|
|
||||||
baseUrl = trimEnd(baseUrl, "/");
|
baseUrl = trimEnd(baseUrl, "/");
|
||||||
|
|
||||||
return `${baseUrl}/${path}`;
|
// try rebuild url, when using cloudflare ai gateway in client
|
||||||
|
return cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,27 +416,3 @@ function trimEnd(s: string, end = " ") {
|
|||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bearer(value: string) {
|
|
||||||
return `Bearer ${value.trim()}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAuthKey(apiKey = "") {
|
|
||||||
const accessStore = useAccessStore.getState();
|
|
||||||
const isApp = !!getClientConfig()?.isApp;
|
|
||||||
let authKey = "";
|
|
||||||
|
|
||||||
if (apiKey) {
|
|
||||||
// use user's api key first
|
|
||||||
authKey = bearer(apiKey);
|
|
||||||
} else if (
|
|
||||||
accessStore.enabledAccessControl() &&
|
|
||||||
!isApp &&
|
|
||||||
!!accessStore.accessCode
|
|
||||||
) {
|
|
||||||
// or use access code
|
|
||||||
authKey = bearer(ACCESS_CODE_PREFIX + accessStore.accessCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
return authKey;
|
|
||||||
}
|
|
||||||
|
|||||||
281
app/client/platforms/baidu.ts
Normal file
281
app/client/platforms/baidu.ts
Normal file
@@ -0,0 +1,281 @@
|
|||||||
|
"use client";
|
||||||
|
import {
|
||||||
|
ApiPath,
|
||||||
|
Baidu,
|
||||||
|
BAIDU_BASE_URL,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
import { getAccessToken } from "@/app/utils/baidu";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatOptions,
|
||||||
|
getHeaders,
|
||||||
|
LLMApi,
|
||||||
|
LLMModel,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent } from "@/app/utils";
|
||||||
|
|
||||||
|
export interface OpenAIListModelResponse {
|
||||||
|
object: string;
|
||||||
|
data: Array<{
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
root: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestPayload {
|
||||||
|
messages: {
|
||||||
|
role: "system" | "user" | "assistant";
|
||||||
|
content: string | MultimodalContent[];
|
||||||
|
}[];
|
||||||
|
stream?: boolean;
|
||||||
|
model: string;
|
||||||
|
temperature: number;
|
||||||
|
presence_penalty: number;
|
||||||
|
frequency_penalty: number;
|
||||||
|
top_p: number;
|
||||||
|
max_tokens?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ErnieApi implements LLMApi {
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.baiduUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
// do not use proxy for baidubce api
|
||||||
|
baseUrl = isApp ? BAIDU_BASE_URL : ApiPath.Baidu;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Baidu)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
return [baseUrl, path].join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const messages = options.messages.map((v) => ({
|
||||||
|
// "error_code": 336006, "error_msg": "the role of message with even index in the messages must be user or function",
|
||||||
|
role: v.role === "system" ? "user" : v.role,
|
||||||
|
content: getMessageTextContent(v),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// "error_code": 336006, "error_msg": "the length of messages must be an odd number",
|
||||||
|
if (messages.length % 2 === 0) {
|
||||||
|
if (messages.at(0)?.role === "user") {
|
||||||
|
messages.splice(1, 0, {
|
||||||
|
role: "assistant",
|
||||||
|
content: " ",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
messages.unshift({
|
||||||
|
role: "user",
|
||||||
|
content: " ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const requestPayload: RequestPayload = {
|
||||||
|
messages,
|
||||||
|
stream: shouldStream,
|
||||||
|
model: modelConfig.model,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
presence_penalty: modelConfig.presence_penalty,
|
||||||
|
frequency_penalty: modelConfig.frequency_penalty,
|
||||||
|
top_p: modelConfig.top_p,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[Request] Baidu payload: ", requestPayload);
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
let chatPath = this.path(Baidu.ChatPath(modelConfig.model));
|
||||||
|
|
||||||
|
// getAccessToken can not run in browser, because cors error
|
||||||
|
if (!!getClientConfig()?.isApp) {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
if (accessStore.isValidBaidu()) {
|
||||||
|
const { access_token } = await getAccessToken(
|
||||||
|
accessStore.baiduApiKey,
|
||||||
|
accessStore.baiduSecretKey,
|
||||||
|
);
|
||||||
|
chatPath = `${chatPath}${
|
||||||
|
chatPath.includes("?") ? "&" : "?"
|
||||||
|
}access_token=${access_token}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
// animate response to make it looks smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
if (responseText?.length === 0) {
|
||||||
|
options.onError?.(new Error("empty response from server"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||||
|
const fetchText = remainText.slice(0, fetchCount);
|
||||||
|
responseText += fetchText;
|
||||||
|
remainText = remainText.slice(fetchCount);
|
||||||
|
options.onUpdate?.(responseText, fetchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start animaion
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
|
const finish = () => {
|
||||||
|
if (!finished) {
|
||||||
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
controller.signal.onabort = finish;
|
||||||
|
|
||||||
|
fetchEventSource(chatPath, {
|
||||||
|
...chatPayload,
|
||||||
|
async onopen(res) {
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
console.log("[Baidu] request response content type: ", contentType);
|
||||||
|
|
||||||
|
if (contentType?.startsWith("text/plain")) {
|
||||||
|
responseText = await res.clone().text();
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
const responseTexts = [responseText];
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
|
try {
|
||||||
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
responseTexts.push(Locale.Error.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extraInfo) {
|
||||||
|
responseTexts.push(extraInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseText = responseTexts.join("\n\n");
|
||||||
|
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const delta = json?.result;
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = resJson?.result;
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export { Baidu };
|
||||||
255
app/client/platforms/bytedance.ts
Normal file
255
app/client/platforms/bytedance.ts
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
"use client";
|
||||||
|
import {
|
||||||
|
ApiPath,
|
||||||
|
ByteDance,
|
||||||
|
BYTEDANCE_BASE_URL,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatOptions,
|
||||||
|
getHeaders,
|
||||||
|
LLMApi,
|
||||||
|
LLMModel,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent } from "@/app/utils";
|
||||||
|
|
||||||
|
export interface OpenAIListModelResponse {
|
||||||
|
object: string;
|
||||||
|
data: Array<{
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
root: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestPayload {
|
||||||
|
messages: {
|
||||||
|
role: "system" | "user" | "assistant";
|
||||||
|
content: string | MultimodalContent[];
|
||||||
|
}[];
|
||||||
|
stream?: boolean;
|
||||||
|
model: string;
|
||||||
|
temperature: number;
|
||||||
|
presence_penalty: number;
|
||||||
|
frequency_penalty: number;
|
||||||
|
top_p: number;
|
||||||
|
max_tokens?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DoubaoApi implements LLMApi {
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.bytedanceUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
baseUrl = isApp ? BYTEDANCE_BASE_URL : ApiPath.ByteDance;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.ByteDance)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
return [baseUrl, path].join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMessage(res: any) {
|
||||||
|
return res.choices?.at(0)?.message?.content ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const messages = options.messages.map((v) => ({
|
||||||
|
role: v.role,
|
||||||
|
content: getMessageTextContent(v),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const requestPayload: RequestPayload = {
|
||||||
|
messages,
|
||||||
|
stream: shouldStream,
|
||||||
|
model: modelConfig.model,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
presence_penalty: modelConfig.presence_penalty,
|
||||||
|
frequency_penalty: modelConfig.frequency_penalty,
|
||||||
|
top_p: modelConfig.top_p,
|
||||||
|
};
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const chatPath = this.path(ByteDance.ChatPath);
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
// animate response to make it looks smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
if (responseText?.length === 0) {
|
||||||
|
options.onError?.(new Error("empty response from server"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||||
|
const fetchText = remainText.slice(0, fetchCount);
|
||||||
|
responseText += fetchText;
|
||||||
|
remainText = remainText.slice(fetchCount);
|
||||||
|
options.onUpdate?.(responseText, fetchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start animaion
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
|
const finish = () => {
|
||||||
|
if (!finished) {
|
||||||
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
controller.signal.onabort = finish;
|
||||||
|
|
||||||
|
fetchEventSource(chatPath, {
|
||||||
|
...chatPayload,
|
||||||
|
async onopen(res) {
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
console.log(
|
||||||
|
"[ByteDance] request response content type: ",
|
||||||
|
contentType,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (contentType?.startsWith("text/plain")) {
|
||||||
|
responseText = await res.clone().text();
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
const responseTexts = [responseText];
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
|
try {
|
||||||
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
responseTexts.push(Locale.Error.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extraInfo) {
|
||||||
|
responseTexts.push(extraInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseText = responseTexts.join("\n\n");
|
||||||
|
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.choices as Array<{
|
||||||
|
delta: { content: string };
|
||||||
|
}>;
|
||||||
|
const delta = choices[0]?.delta?.content;
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = this.extractMessage(resJson);
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export { ByteDance };
|
||||||
@@ -1,15 +1,52 @@
|
|||||||
import { Google, REQUEST_TIMEOUT_MS } from "@/app/constant";
|
import { ApiPath, Google, REQUEST_TIMEOUT_MS } from "@/app/constant";
|
||||||
import { ChatOptions, getHeaders, LLMApi, LLMModel, LLMUsage } from "../api";
|
import { ChatOptions, getHeaders, LLMApi, LLMModel, LLMUsage } from "../api";
|
||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { DEFAULT_API_HOST } from "@/app/constant";
|
import { DEFAULT_API_HOST } from "@/app/constant";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import {
|
import {
|
||||||
getMessageTextContent,
|
getMessageTextContent,
|
||||||
getMessageImages,
|
getMessageImages,
|
||||||
isVisionModel,
|
isVisionModel,
|
||||||
} from "@/app/utils";
|
} from "@/app/utils";
|
||||||
|
import { preProcessImageContent } from "@/app/utils/chat";
|
||||||
|
|
||||||
export class GeminiProApi implements LLMApi {
|
export class GeminiProApi implements LLMApi {
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.googleUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
baseUrl = isApp ? DEFAULT_API_HOST + `/api/proxy/google` : ApiPath.Google;
|
||||||
|
}
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Google)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
let chatPath = [baseUrl, path].join("/");
|
||||||
|
|
||||||
|
chatPath += chatPath.includes("?") ? "&alt=sse" : "?alt=sse";
|
||||||
|
// if chatPath.startsWith('http') then add key in query string
|
||||||
|
if (chatPath.startsWith("http") && accessStore.googleApiKey) {
|
||||||
|
chatPath += `&key=${accessStore.googleApiKey}`;
|
||||||
|
}
|
||||||
|
return chatPath;
|
||||||
|
}
|
||||||
extractMessage(res: any) {
|
extractMessage(res: any) {
|
||||||
console.log("[Response] gemini-pro response: ", res);
|
console.log("[Response] gemini-pro response: ", res);
|
||||||
|
|
||||||
@@ -20,9 +57,16 @@ export class GeminiProApi implements LLMApi {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
async chat(options: ChatOptions): Promise<void> {
|
async chat(options: ChatOptions): Promise<void> {
|
||||||
// const apiClient = this;
|
const apiClient = this;
|
||||||
let multimodal = false;
|
let multimodal = false;
|
||||||
const messages = options.messages.map((v) => {
|
|
||||||
|
// try get base64image from local cache image_url
|
||||||
|
const _messages: ChatOptions["messages"] = [];
|
||||||
|
for (const v of options.messages) {
|
||||||
|
const content = await preProcessImageContent(v.content);
|
||||||
|
_messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
const messages = _messages.map((v) => {
|
||||||
let parts: any[] = [{ text: getMessageTextContent(v) }];
|
let parts: any[] = [{ text: getMessageTextContent(v) }];
|
||||||
if (isVisionModel(options.config.model)) {
|
if (isVisionModel(options.config.model)) {
|
||||||
const images = getMessageImages(v);
|
const images = getMessageImages(v);
|
||||||
@@ -64,6 +108,9 @@ export class GeminiProApi implements LLMApi {
|
|||||||
// if (visionModel && messages.length > 1) {
|
// if (visionModel && messages.length > 1) {
|
||||||
// options.onError?.(new Error("Multiturn chat is not enabled for models/gemini-pro-vision"));
|
// options.onError?.(new Error("Multiturn chat is not enabled for models/gemini-pro-vision"));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
const modelConfig = {
|
const modelConfig = {
|
||||||
...useAppConfig.getState().modelConfig,
|
...useAppConfig.getState().modelConfig,
|
||||||
...useChatStore.getState().currentSession().mask.modelConfig,
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
@@ -85,50 +132,30 @@ export class GeminiProApi implements LLMApi {
|
|||||||
safetySettings: [
|
safetySettings: [
|
||||||
{
|
{
|
||||||
category: "HARM_CATEGORY_HARASSMENT",
|
category: "HARM_CATEGORY_HARASSMENT",
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
threshold: accessStore.googleSafetySettings,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
category: "HARM_CATEGORY_HATE_SPEECH",
|
category: "HARM_CATEGORY_HATE_SPEECH",
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
threshold: accessStore.googleSafetySettings,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
threshold: accessStore.googleSafetySettings,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
|
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
threshold: accessStore.googleSafetySettings,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const accessStore = useAccessStore.getState();
|
|
||||||
|
|
||||||
let baseUrl = "";
|
|
||||||
|
|
||||||
if (accessStore.useCustomConfig) {
|
|
||||||
baseUrl = accessStore.googleUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isApp = !!getClientConfig()?.isApp;
|
|
||||||
|
|
||||||
let shouldStream = !!options.config.stream;
|
let shouldStream = !!options.config.stream;
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
options.onController?.(controller);
|
options.onController?.(controller);
|
||||||
try {
|
try {
|
||||||
// let baseUrl = accessStore.googleUrl;
|
// https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/Streaming_REST.ipynb
|
||||||
|
const chatPath = this.path(Google.ChatPath(modelConfig.model));
|
||||||
|
|
||||||
if (!baseUrl) {
|
|
||||||
baseUrl = isApp
|
|
||||||
? DEFAULT_API_HOST +
|
|
||||||
"/api/proxy/google/" +
|
|
||||||
Google.ChatPath(modelConfig.model)
|
|
||||||
: this.path(Google.ChatPath(modelConfig.model));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isApp) {
|
|
||||||
baseUrl += `?key=${accessStore.googleApiKey}`;
|
|
||||||
}
|
|
||||||
const chatPayload = {
|
const chatPayload = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(requestPayload),
|
body: JSON.stringify(requestPayload),
|
||||||
@@ -147,10 +174,11 @@ export class GeminiProApi implements LLMApi {
|
|||||||
let remainText = "";
|
let remainText = "";
|
||||||
let finished = false;
|
let finished = false;
|
||||||
|
|
||||||
let existingTexts: string[] = [];
|
|
||||||
const finish = () => {
|
const finish = () => {
|
||||||
finished = true;
|
if (!finished) {
|
||||||
options.onFinish(existingTexts.join(""));
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// animate response to make it looks smooth
|
// animate response to make it looks smooth
|
||||||
@@ -175,74 +203,83 @@ export class GeminiProApi implements LLMApi {
|
|||||||
// start animaion
|
// start animaion
|
||||||
animateResponseText();
|
animateResponseText();
|
||||||
|
|
||||||
fetch(
|
controller.signal.onabort = finish;
|
||||||
baseUrl.replace("generateContent", "streamGenerateContent"),
|
|
||||||
chatPayload,
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
const reader = response?.body?.getReader();
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
let partialData = "";
|
|
||||||
|
|
||||||
return reader?.read().then(function processText({
|
fetchEventSource(chatPath, {
|
||||||
done,
|
...chatPayload,
|
||||||
value,
|
async onopen(res) {
|
||||||
}): Promise<any> {
|
clearTimeout(requestTimeoutId);
|
||||||
if (done) {
|
const contentType = res.headers.get("content-type");
|
||||||
if (response.status !== 200) {
|
console.log(
|
||||||
try {
|
"[Gemini] request response content type: ",
|
||||||
let data = JSON.parse(ensureProperEnding(partialData));
|
contentType,
|
||||||
if (data && data[0].error) {
|
);
|
||||||
options.onError?.(new Error(data[0].error.message));
|
|
||||||
} else {
|
|
||||||
options.onError?.(new Error("Request failed"));
|
|
||||||
}
|
|
||||||
} catch (_) {
|
|
||||||
options.onError?.(new Error("Request failed"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Stream complete");
|
if (contentType?.startsWith("text/plain")) {
|
||||||
// options.onFinish(responseText + remainText);
|
responseText = await res.clone().text();
|
||||||
finished = true;
|
return finish();
|
||||||
return Promise.resolve();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
partialData += decoder.decode(value, { stream: true });
|
|
||||||
|
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
const responseTexts = [responseText];
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
try {
|
try {
|
||||||
let data = JSON.parse(ensureProperEnding(partialData));
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
const textArray = data.reduce(
|
if (res.status === 401) {
|
||||||
(acc: string[], item: { candidates: any[] }) => {
|
responseTexts.push(Locale.Error.Unauthorized);
|
||||||
const texts = item.candidates.map((candidate) =>
|
|
||||||
candidate.content.parts
|
|
||||||
.map((part: { text: any }) => part.text)
|
|
||||||
.join(""),
|
|
||||||
);
|
|
||||||
return acc.concat(texts);
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (textArray.length > existingTexts.length) {
|
|
||||||
const deltaArray = textArray.slice(existingTexts.length);
|
|
||||||
existingTexts = textArray;
|
|
||||||
remainText += deltaArray.join("");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// console.log("[Response Animation] error: ", error,partialData);
|
|
||||||
// skip error message when parsing json
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reader.read().then(processText);
|
if (extraInfo) {
|
||||||
});
|
responseTexts.push(extraInfo);
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error:", error);
|
responseText = responseTexts.join("\n\n");
|
||||||
});
|
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const delta = apiClient.extractMessage(json);
|
||||||
|
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
const blockReason = json?.promptFeedback?.blockReason;
|
||||||
|
if (blockReason) {
|
||||||
|
// being blocked
|
||||||
|
console.log(`[Google] [Safety Ratings] result:`, blockReason);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const res = await fetch(baseUrl, chatPayload);
|
const res = await fetch(chatPath, chatPayload);
|
||||||
clearTimeout(requestTimeoutId);
|
clearTimeout(requestTimeoutId);
|
||||||
const resJson = await res.json();
|
const resJson = await res.json();
|
||||||
if (resJson?.promptFeedback?.blockReason) {
|
if (resJson?.promptFeedback?.blockReason) {
|
||||||
@@ -254,7 +291,7 @@ export class GeminiProApi implements LLMApi {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const message = this.extractMessage(resJson);
|
const message = apiClient.extractMessage(resJson);
|
||||||
options.onFinish(message);
|
options.onFinish(message);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -268,14 +305,4 @@ export class GeminiProApi implements LLMApi {
|
|||||||
async models(): Promise<LLMModel[]> {
|
async models(): Promise<LLMModel[]> {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
path(path: string): string {
|
|
||||||
return "/api/google/" + path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function ensureProperEnding(str: string) {
|
|
||||||
if (str.startsWith("[") && !str.endsWith("]")) {
|
|
||||||
return str + "]";
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|||||||
240
app/client/platforms/iflytek.ts
Normal file
240
app/client/platforms/iflytek.ts
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
"use client";
|
||||||
|
import {
|
||||||
|
ApiPath,
|
||||||
|
DEFAULT_API_HOST,
|
||||||
|
Iflytek,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
|
||||||
|
import { ChatOptions, getHeaders, LLMApi, LLMModel } from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent } from "@/app/utils";
|
||||||
|
|
||||||
|
import { OpenAIListModelResponse, RequestPayload } from "./openai";
|
||||||
|
|
||||||
|
export class SparkApi implements LLMApi {
|
||||||
|
private disableListModels = true;
|
||||||
|
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.iflytekUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
const apiPath = ApiPath.Iflytek;
|
||||||
|
baseUrl = isApp ? DEFAULT_API_HOST + "/proxy" + apiPath : apiPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Iflytek)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
return [baseUrl, path].join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMessage(res: any) {
|
||||||
|
return res.choices?.at(0)?.message?.content ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const messages: ChatOptions["messages"] = [];
|
||||||
|
for (const v of options.messages) {
|
||||||
|
const content = getMessageTextContent(v);
|
||||||
|
messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
providerName: options.config.providerName,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestPayload: RequestPayload = {
|
||||||
|
messages,
|
||||||
|
stream: options.config.stream,
|
||||||
|
model: modelConfig.model,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
presence_penalty: modelConfig.presence_penalty,
|
||||||
|
frequency_penalty: modelConfig.frequency_penalty,
|
||||||
|
top_p: modelConfig.top_p,
|
||||||
|
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
||||||
|
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[Request] Spark payload: ", requestPayload);
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const chatPath = this.path(Iflytek.ChatPath);
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
// Animate response text to make it look smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||||
|
const fetchText = remainText.slice(0, fetchCount);
|
||||||
|
responseText += fetchText;
|
||||||
|
remainText = remainText.slice(fetchCount);
|
||||||
|
options.onUpdate?.(responseText, fetchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start animation
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
|
const finish = () => {
|
||||||
|
if (!finished) {
|
||||||
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
controller.signal.onabort = finish;
|
||||||
|
|
||||||
|
fetchEventSource(chatPath, {
|
||||||
|
...chatPayload,
|
||||||
|
async onopen(res) {
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
console.log("[Spark] request response content type: ", contentType);
|
||||||
|
|
||||||
|
if (contentType?.startsWith("text/plain")) {
|
||||||
|
responseText = await res.clone().text();
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle different error scenarios
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
|
try {
|
||||||
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
extraInfo = Locale.Error.Unauthorized;
|
||||||
|
}
|
||||||
|
|
||||||
|
options.onError?.(
|
||||||
|
new Error(
|
||||||
|
`Request failed with status ${res.status}: ${extraInfo}`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.choices as Array<{
|
||||||
|
delta: { content: string };
|
||||||
|
}>;
|
||||||
|
const delta = choices[0]?.delta?.content;
|
||||||
|
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text);
|
||||||
|
options.onError?.(new Error(`Failed to parse response: ${text}`));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const errorText = await res.text();
|
||||||
|
options.onError?.(
|
||||||
|
new Error(`Request failed with status ${res.status}: ${errorText}`),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = this.extractMessage(resJson);
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
208
app/client/platforms/moonshot.ts
Normal file
208
app/client/platforms/moonshot.ts
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
"use client";
|
||||||
|
// azure and openai, using same models. so using same LLMApi.
|
||||||
|
import {
|
||||||
|
ApiPath,
|
||||||
|
DEFAULT_API_HOST,
|
||||||
|
DEFAULT_MODELS,
|
||||||
|
Moonshot,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
ServiceProvider,
|
||||||
|
} from "@/app/constant";
|
||||||
|
import {
|
||||||
|
useAccessStore,
|
||||||
|
useAppConfig,
|
||||||
|
useChatStore,
|
||||||
|
ChatMessageTool,
|
||||||
|
usePluginStore,
|
||||||
|
} from "@/app/store";
|
||||||
|
import { collectModelsWithDefaultModel } from "@/app/utils/model";
|
||||||
|
import { preProcessImageContent, stream } from "@/app/utils/chat";
|
||||||
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatOptions,
|
||||||
|
getHeaders,
|
||||||
|
LLMApi,
|
||||||
|
LLMModel,
|
||||||
|
LLMUsage,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent } from "@/app/utils";
|
||||||
|
|
||||||
|
import { OpenAIListModelResponse, RequestPayload } from "./openai";
|
||||||
|
|
||||||
|
export class MoonshotApi implements LLMApi {
|
||||||
|
private disableListModels = true;
|
||||||
|
|
||||||
|
path(path: string): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.moonshotUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
const apiPath = ApiPath.Moonshot;
|
||||||
|
baseUrl = isApp ? DEFAULT_API_HOST + "/proxy" + apiPath : apiPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Moonshot)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
|
return [baseUrl, path].join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMessage(res: any) {
|
||||||
|
return res.choices?.at(0)?.message?.content ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const messages: ChatOptions["messages"] = [];
|
||||||
|
for (const v of options.messages) {
|
||||||
|
const content = getMessageTextContent(v);
|
||||||
|
messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
providerName: options.config.providerName,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestPayload: RequestPayload = {
|
||||||
|
messages,
|
||||||
|
stream: options.config.stream,
|
||||||
|
model: modelConfig.model,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
presence_penalty: modelConfig.presence_penalty,
|
||||||
|
frequency_penalty: modelConfig.frequency_penalty,
|
||||||
|
top_p: modelConfig.top_p,
|
||||||
|
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
||||||
|
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[Request] openai payload: ", requestPayload);
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const chatPath = this.path(Moonshot.ChatPath);
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
const [tools, funcs] = usePluginStore
|
||||||
|
.getState()
|
||||||
|
.getAsTools(
|
||||||
|
useChatStore.getState().currentSession().mask?.plugin || [],
|
||||||
|
);
|
||||||
|
return stream(
|
||||||
|
chatPath,
|
||||||
|
requestPayload,
|
||||||
|
getHeaders(),
|
||||||
|
tools as any,
|
||||||
|
funcs,
|
||||||
|
controller,
|
||||||
|
// parseSSE
|
||||||
|
(text: string, runTools: ChatMessageTool[]) => {
|
||||||
|
// console.log("parseSSE", text, runTools);
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.choices as Array<{
|
||||||
|
delta: {
|
||||||
|
content: string;
|
||||||
|
tool_calls: ChatMessageTool[];
|
||||||
|
};
|
||||||
|
}>;
|
||||||
|
const tool_calls = choices[0]?.delta?.tool_calls;
|
||||||
|
if (tool_calls?.length > 0) {
|
||||||
|
const index = tool_calls[0]?.index;
|
||||||
|
const id = tool_calls[0]?.id;
|
||||||
|
const args = tool_calls[0]?.function?.arguments;
|
||||||
|
if (id) {
|
||||||
|
runTools.push({
|
||||||
|
id,
|
||||||
|
type: tool_calls[0]?.type,
|
||||||
|
function: {
|
||||||
|
name: tool_calls[0]?.function?.name as string,
|
||||||
|
arguments: args,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// @ts-ignore
|
||||||
|
runTools[index]["function"]["arguments"] += args;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return choices[0]?.delta?.content;
|
||||||
|
},
|
||||||
|
// processToolMessage, include tool_calls message and tool call results
|
||||||
|
(
|
||||||
|
requestPayload: RequestPayload,
|
||||||
|
toolCallMessage: any,
|
||||||
|
toolCallResult: any[],
|
||||||
|
) => {
|
||||||
|
// @ts-ignore
|
||||||
|
requestPayload?.messages?.splice(
|
||||||
|
// @ts-ignore
|
||||||
|
requestPayload?.messages?.length,
|
||||||
|
0,
|
||||||
|
toolCallMessage,
|
||||||
|
...toolCallResult,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = this.extractMessage(resJson);
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,30 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
// azure and openai, using same models. so using same LLMApi.
|
||||||
import {
|
import {
|
||||||
ApiPath,
|
ApiPath,
|
||||||
DEFAULT_API_HOST,
|
DEFAULT_API_HOST,
|
||||||
DEFAULT_MODELS,
|
DEFAULT_MODELS,
|
||||||
OpenaiPath,
|
OpenaiPath,
|
||||||
|
Azure,
|
||||||
REQUEST_TIMEOUT_MS,
|
REQUEST_TIMEOUT_MS,
|
||||||
ServiceProvider,
|
ServiceProvider,
|
||||||
} from "@/app/constant";
|
} from "@/app/constant";
|
||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
import {
|
||||||
|
ChatMessageTool,
|
||||||
|
useAccessStore,
|
||||||
|
useAppConfig,
|
||||||
|
useChatStore,
|
||||||
|
usePluginStore,
|
||||||
|
} from "@/app/store";
|
||||||
|
import { collectModelsWithDefaultModel } from "@/app/utils/model";
|
||||||
|
import {
|
||||||
|
preProcessImageContent,
|
||||||
|
uploadImage,
|
||||||
|
base64Image2Blob,
|
||||||
|
stream,
|
||||||
|
} from "@/app/utils/chat";
|
||||||
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
|
import { DalleSize, DalleQuality, DalleStyle } from "@/app/typing";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ChatOptions,
|
ChatOptions,
|
||||||
@@ -24,11 +41,11 @@ import {
|
|||||||
} from "@fortaine/fetch-event-source";
|
} from "@fortaine/fetch-event-source";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { makeAzurePath } from "@/app/azure";
|
|
||||||
import {
|
import {
|
||||||
getMessageTextContent,
|
getMessageTextContent,
|
||||||
getMessageImages,
|
getMessageImages,
|
||||||
isVisionModel,
|
isVisionModel,
|
||||||
|
isDalle3 as _isDalle3,
|
||||||
} from "@/app/utils";
|
} from "@/app/utils";
|
||||||
|
|
||||||
export interface OpenAIListModelResponse {
|
export interface OpenAIListModelResponse {
|
||||||
@@ -40,7 +57,7 @@ export interface OpenAIListModelResponse {
|
|||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RequestPayload {
|
export interface RequestPayload {
|
||||||
messages: {
|
messages: {
|
||||||
role: "system" | "user" | "assistant";
|
role: "system" | "user" | "assistant";
|
||||||
content: string | MultimodalContent[];
|
content: string | MultimodalContent[];
|
||||||
@@ -54,6 +71,16 @@ interface RequestPayload {
|
|||||||
max_tokens?: number;
|
max_tokens?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DalleRequestPayload {
|
||||||
|
model: string;
|
||||||
|
prompt: string;
|
||||||
|
response_format: "url" | "b64_json";
|
||||||
|
n: number;
|
||||||
|
size: DalleSize;
|
||||||
|
quality: DalleQuality;
|
||||||
|
style: DalleStyle;
|
||||||
|
}
|
||||||
|
|
||||||
export class ChatGPTApi implements LLMApi {
|
export class ChatGPTApi implements LLMApi {
|
||||||
private disableListModels = true;
|
private disableListModels = true;
|
||||||
|
|
||||||
@@ -62,227 +89,241 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
|
|
||||||
let baseUrl = "";
|
let baseUrl = "";
|
||||||
|
|
||||||
|
const isAzure = path.includes("deployments");
|
||||||
if (accessStore.useCustomConfig) {
|
if (accessStore.useCustomConfig) {
|
||||||
const isAzure = accessStore.provider === ServiceProvider.Azure;
|
|
||||||
|
|
||||||
if (isAzure && !accessStore.isValidAzure()) {
|
if (isAzure && !accessStore.isValidAzure()) {
|
||||||
throw Error(
|
throw Error(
|
||||||
"incomplete azure config, please check it in your settings page",
|
"incomplete azure config, please check it in your settings page",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAzure) {
|
|
||||||
path = makeAzurePath(path, accessStore.azureApiVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl;
|
baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (baseUrl.length === 0) {
|
if (baseUrl.length === 0) {
|
||||||
const isApp = !!getClientConfig()?.isApp;
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
baseUrl = isApp
|
const apiPath = isAzure ? ApiPath.Azure : ApiPath.OpenAI;
|
||||||
? DEFAULT_API_HOST + "/proxy" + ApiPath.OpenAI
|
baseUrl = isApp ? DEFAULT_API_HOST + "/proxy" + apiPath : apiPath;
|
||||||
: ApiPath.OpenAI;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (baseUrl.endsWith("/")) {
|
if (baseUrl.endsWith("/")) {
|
||||||
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
}
|
}
|
||||||
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.OpenAI)) {
|
if (
|
||||||
|
!baseUrl.startsWith("http") &&
|
||||||
|
!isAzure &&
|
||||||
|
!baseUrl.startsWith(ApiPath.OpenAI)
|
||||||
|
) {
|
||||||
baseUrl = "https://" + baseUrl;
|
baseUrl = "https://" + baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[Proxy Endpoint] ", baseUrl, path);
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||||
|
|
||||||
return [baseUrl, path].join("/");
|
// try rebuild url, when using cloudflare ai gateway in client
|
||||||
|
return cloudflareAIGatewayUrl([baseUrl, path].join("/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
extractMessage(res: any) {
|
async extractMessage(res: any) {
|
||||||
return res.choices?.at(0)?.message?.content ?? "";
|
if (res.error) {
|
||||||
|
return "```\n" + JSON.stringify(res, null, 4) + "\n```";
|
||||||
|
}
|
||||||
|
// dalle3 model return url, using url create image message
|
||||||
|
if (res.data) {
|
||||||
|
let url = res.data?.at(0)?.url ?? "";
|
||||||
|
const b64_json = res.data?.at(0)?.b64_json ?? "";
|
||||||
|
if (!url && b64_json) {
|
||||||
|
// uploadImage
|
||||||
|
url = await uploadImage(base64Image2Blob(b64_json, "image/png"));
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
type: "image_url",
|
||||||
|
image_url: {
|
||||||
|
url,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return res.choices?.at(0)?.message?.content ?? res;
|
||||||
}
|
}
|
||||||
|
|
||||||
async chat(options: ChatOptions) {
|
async chat(options: ChatOptions) {
|
||||||
const visionModel = isVisionModel(options.config.model);
|
|
||||||
const messages = options.messages.map((v) => ({
|
|
||||||
role: v.role,
|
|
||||||
content: visionModel ? v.content : getMessageTextContent(v),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const modelConfig = {
|
const modelConfig = {
|
||||||
...useAppConfig.getState().modelConfig,
|
...useAppConfig.getState().modelConfig,
|
||||||
...useChatStore.getState().currentSession().mask.modelConfig,
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
...{
|
...{
|
||||||
model: options.config.model,
|
model: options.config.model,
|
||||||
|
providerName: options.config.providerName,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const requestPayload: RequestPayload = {
|
let requestPayload: RequestPayload | DalleRequestPayload;
|
||||||
messages,
|
|
||||||
stream: options.config.stream,
|
|
||||||
model: modelConfig.model,
|
|
||||||
temperature: modelConfig.temperature,
|
|
||||||
presence_penalty: modelConfig.presence_penalty,
|
|
||||||
frequency_penalty: modelConfig.frequency_penalty,
|
|
||||||
top_p: modelConfig.top_p,
|
|
||||||
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
|
||||||
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
|
||||||
};
|
|
||||||
|
|
||||||
// add max_tokens to vision model
|
const isDalle3 = _isDalle3(options.config.model);
|
||||||
if (visionModel && modelConfig.model.includes("preview")) {
|
const isO1 = options.config.model.startsWith("o1");
|
||||||
requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000);
|
if (isDalle3) {
|
||||||
|
const prompt = getMessageTextContent(
|
||||||
|
options.messages.slice(-1)?.pop() as any,
|
||||||
|
);
|
||||||
|
requestPayload = {
|
||||||
|
model: options.config.model,
|
||||||
|
prompt,
|
||||||
|
// URLs are only valid for 60 minutes after the image has been generated.
|
||||||
|
response_format: "b64_json", // using b64_json, and save image in CacheStorage
|
||||||
|
n: 1,
|
||||||
|
size: options.config?.size ?? "1024x1024",
|
||||||
|
quality: options.config?.quality ?? "standard",
|
||||||
|
style: options.config?.style ?? "vivid",
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const visionModel = isVisionModel(options.config.model);
|
||||||
|
const messages: ChatOptions["messages"] = [];
|
||||||
|
for (const v of options.messages) {
|
||||||
|
const content = visionModel
|
||||||
|
? await preProcessImageContent(v.content)
|
||||||
|
: getMessageTextContent(v);
|
||||||
|
if (!(isO1 && v.role === "system"))
|
||||||
|
messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
|
||||||
|
// O1 not support image, tools (plugin in ChatGPTNextWeb) and system, stream, logprobs, temperature, top_p, n, presence_penalty, frequency_penalty yet.
|
||||||
|
requestPayload = {
|
||||||
|
messages,
|
||||||
|
stream: !isO1 ? options.config.stream : false,
|
||||||
|
model: modelConfig.model,
|
||||||
|
temperature: !isO1 ? modelConfig.temperature : 1,
|
||||||
|
presence_penalty: !isO1 ? modelConfig.presence_penalty : 0,
|
||||||
|
frequency_penalty: !isO1 ? modelConfig.frequency_penalty : 0,
|
||||||
|
top_p: !isO1 ? modelConfig.top_p : 1,
|
||||||
|
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
||||||
|
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
||||||
|
};
|
||||||
|
|
||||||
|
// add max_tokens to vision model
|
||||||
|
if (visionModel) {
|
||||||
|
requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[Request] openai payload: ", requestPayload);
|
console.log("[Request] openai payload: ", requestPayload);
|
||||||
|
|
||||||
const shouldStream = !!options.config.stream;
|
const shouldStream = !isDalle3 && !!options.config.stream && !isO1;
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
options.onController?.(controller);
|
options.onController?.(controller);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const chatPath = this.path(OpenaiPath.ChatPath);
|
let chatPath = "";
|
||||||
const chatPayload = {
|
if (modelConfig.providerName === ServiceProvider.Azure) {
|
||||||
method: "POST",
|
// find model, and get displayName as deployName
|
||||||
body: JSON.stringify(requestPayload),
|
const { models: configModels, customModels: configCustomModels } =
|
||||||
signal: controller.signal,
|
useAppConfig.getState();
|
||||||
headers: getHeaders(),
|
const {
|
||||||
};
|
defaultModel,
|
||||||
|
customModels: accessCustomModels,
|
||||||
// make a fetch request
|
useCustomConfig,
|
||||||
const requestTimeoutId = setTimeout(
|
} = useAccessStore.getState();
|
||||||
() => controller.abort(),
|
const models = collectModelsWithDefaultModel(
|
||||||
REQUEST_TIMEOUT_MS,
|
configModels,
|
||||||
);
|
[configCustomModels, accessCustomModels].join(","),
|
||||||
|
defaultModel,
|
||||||
|
);
|
||||||
|
const model = models.find(
|
||||||
|
(model) =>
|
||||||
|
model.name === modelConfig.model &&
|
||||||
|
model?.provider?.providerName === ServiceProvider.Azure,
|
||||||
|
);
|
||||||
|
chatPath = this.path(
|
||||||
|
(isDalle3 ? Azure.ImagePath : Azure.ChatPath)(
|
||||||
|
(model?.displayName ?? model?.name) as string,
|
||||||
|
useCustomConfig ? useAccessStore.getState().azureApiVersion : "",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
chatPath = this.path(
|
||||||
|
isDalle3 ? OpenaiPath.ImagePath : OpenaiPath.ChatPath,
|
||||||
|
);
|
||||||
|
}
|
||||||
if (shouldStream) {
|
if (shouldStream) {
|
||||||
let responseText = "";
|
const [tools, funcs] = usePluginStore
|
||||||
let remainText = "";
|
.getState()
|
||||||
let finished = false;
|
.getAsTools(
|
||||||
|
useChatStore.getState().currentSession().mask?.plugin || [],
|
||||||
// animate response to make it looks smooth
|
);
|
||||||
function animateResponseText() {
|
// console.log("getAsTools", tools, funcs);
|
||||||
if (finished || controller.signal.aborted) {
|
stream(
|
||||||
responseText += remainText;
|
chatPath,
|
||||||
console.log("[Response Animation] finished");
|
requestPayload,
|
||||||
if (responseText?.length === 0) {
|
getHeaders(),
|
||||||
options.onError?.(new Error("empty response from server"));
|
tools as any,
|
||||||
|
funcs,
|
||||||
|
controller,
|
||||||
|
// parseSSE
|
||||||
|
(text: string, runTools: ChatMessageTool[]) => {
|
||||||
|
// console.log("parseSSE", text, runTools);
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.choices as Array<{
|
||||||
|
delta: {
|
||||||
|
content: string;
|
||||||
|
tool_calls: ChatMessageTool[];
|
||||||
|
};
|
||||||
|
}>;
|
||||||
|
const tool_calls = choices[0]?.delta?.tool_calls;
|
||||||
|
if (tool_calls?.length > 0) {
|
||||||
|
const index = tool_calls[0]?.index;
|
||||||
|
const id = tool_calls[0]?.id;
|
||||||
|
const args = tool_calls[0]?.function?.arguments;
|
||||||
|
if (id) {
|
||||||
|
runTools.push({
|
||||||
|
id,
|
||||||
|
type: tool_calls[0]?.type,
|
||||||
|
function: {
|
||||||
|
name: tool_calls[0]?.function?.name as string,
|
||||||
|
arguments: args,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// @ts-ignore
|
||||||
|
runTools[index]["function"]["arguments"] += args;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return choices[0]?.delta?.content;
|
||||||
}
|
},
|
||||||
|
// processToolMessage, include tool_calls message and tool call results
|
||||||
if (remainText.length > 0) {
|
(
|
||||||
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
requestPayload: RequestPayload,
|
||||||
const fetchText = remainText.slice(0, fetchCount);
|
toolCallMessage: any,
|
||||||
responseText += fetchText;
|
toolCallResult: any[],
|
||||||
remainText = remainText.slice(fetchCount);
|
) => {
|
||||||
options.onUpdate?.(responseText, fetchText);
|
// @ts-ignore
|
||||||
}
|
requestPayload?.messages?.splice(
|
||||||
|
// @ts-ignore
|
||||||
requestAnimationFrame(animateResponseText);
|
requestPayload?.messages?.length,
|
||||||
}
|
0,
|
||||||
|
toolCallMessage,
|
||||||
// start animaion
|
...toolCallResult,
|
||||||
animateResponseText();
|
);
|
||||||
|
},
|
||||||
const finish = () => {
|
options,
|
||||||
if (!finished) {
|
);
|
||||||
finished = true;
|
} else {
|
||||||
options.onFinish(responseText + remainText);
|
const chatPayload = {
|
||||||
}
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
};
|
};
|
||||||
|
|
||||||
controller.signal.onabort = finish;
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
isDalle3 || isO1 ? REQUEST_TIMEOUT_MS * 2 : REQUEST_TIMEOUT_MS, // dalle3 using b64_json is slow.
|
||||||
|
);
|
||||||
|
|
||||||
fetchEventSource(chatPath, {
|
|
||||||
...chatPayload,
|
|
||||||
async onopen(res) {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log(
|
|
||||||
"[OpenAI] request response content type: ",
|
|
||||||
contentType,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
responseText = await res.clone().text();
|
|
||||||
return finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!res.ok ||
|
|
||||||
!res.headers
|
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
|
||||||
const responseTexts = [responseText];
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
responseText = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return finish();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
if (msg.data === "[DONE]" || finished) {
|
|
||||||
return finish();
|
|
||||||
}
|
|
||||||
const text = msg.data;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(text);
|
|
||||||
const choices = json.choices as Array<{
|
|
||||||
delta: { content: string };
|
|
||||||
}>;
|
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
const textmoderation = json?.prompt_filter_results;
|
|
||||||
|
|
||||||
if (delta) {
|
|
||||||
remainText += delta;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
textmoderation &&
|
|
||||||
textmoderation.length > 0 &&
|
|
||||||
ServiceProvider.Azure
|
|
||||||
) {
|
|
||||||
const contentFilterResults =
|
|
||||||
textmoderation[0]?.content_filter_results;
|
|
||||||
console.log(
|
|
||||||
`[${ServiceProvider.Azure}] [Text Moderation] flagged categories result:`,
|
|
||||||
contentFilterResults,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose() {
|
|
||||||
finish();
|
|
||||||
},
|
|
||||||
onerror(e) {
|
|
||||||
options.onError?.(e);
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const res = await fetch(chatPath, chatPayload);
|
const res = await fetch(chatPath, chatPayload);
|
||||||
clearTimeout(requestTimeoutId);
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
const resJson = await res.json();
|
const resJson = await res.json();
|
||||||
const message = this.extractMessage(resJson);
|
const message = await this.extractMessage(resJson);
|
||||||
options.onFinish(message);
|
options.onFinish(message);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -369,20 +410,26 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const resJson = (await res.json()) as OpenAIListModelResponse;
|
const resJson = (await res.json()) as OpenAIListModelResponse;
|
||||||
const chatModels = resJson.data?.filter((m) => m.id.startsWith("gpt-"));
|
const chatModels = resJson.data?.filter(
|
||||||
|
(m) => m.id.startsWith("gpt-") || m.id.startsWith("chatgpt-"),
|
||||||
|
);
|
||||||
console.log("[Models]", chatModels);
|
console.log("[Models]", chatModels);
|
||||||
|
|
||||||
if (!chatModels) {
|
if (!chatModels) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//由于目前 OpenAI 的 disableListModels 默认为 true,所以当前实际不会运行到这场
|
||||||
|
let seq = 1000; //同 Constant.ts 中的排序保持一致
|
||||||
return chatModels.map((m) => ({
|
return chatModels.map((m) => ({
|
||||||
name: m.id,
|
name: m.id,
|
||||||
available: true,
|
available: true,
|
||||||
|
sorted: seq++,
|
||||||
provider: {
|
provider: {
|
||||||
id: "openai",
|
id: "openai",
|
||||||
providerName: "OpenAI",
|
providerName: "OpenAI",
|
||||||
providerType: "openai",
|
providerType: "openai",
|
||||||
|
sorted: 1,
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
268
app/client/platforms/tencent.ts
Normal file
268
app/client/platforms/tencent.ts
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
"use client";
|
||||||
|
import { ApiPath, DEFAULT_API_HOST, REQUEST_TIMEOUT_MS } from "@/app/constant";
|
||||||
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatOptions,
|
||||||
|
getHeaders,
|
||||||
|
LLMApi,
|
||||||
|
LLMModel,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../api";
|
||||||
|
import Locale from "../../locales";
|
||||||
|
import {
|
||||||
|
EventStreamContentType,
|
||||||
|
fetchEventSource,
|
||||||
|
} from "@fortaine/fetch-event-source";
|
||||||
|
import { prettyObject } from "@/app/utils/format";
|
||||||
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { getMessageTextContent, isVisionModel } from "@/app/utils";
|
||||||
|
import mapKeys from "lodash-es/mapKeys";
|
||||||
|
import mapValues from "lodash-es/mapValues";
|
||||||
|
import isArray from "lodash-es/isArray";
|
||||||
|
import isObject from "lodash-es/isObject";
|
||||||
|
|
||||||
|
export interface OpenAIListModelResponse {
|
||||||
|
object: string;
|
||||||
|
data: Array<{
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
root: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestPayload {
|
||||||
|
Messages: {
|
||||||
|
Role: "system" | "user" | "assistant";
|
||||||
|
Content: string | MultimodalContent[];
|
||||||
|
}[];
|
||||||
|
Stream?: boolean;
|
||||||
|
Model: string;
|
||||||
|
Temperature: number;
|
||||||
|
TopP: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
function capitalizeKeys(obj: any): any {
|
||||||
|
if (isArray(obj)) {
|
||||||
|
return obj.map(capitalizeKeys);
|
||||||
|
} else if (isObject(obj)) {
|
||||||
|
return mapValues(
|
||||||
|
mapKeys(obj, (value: any, key: string) =>
|
||||||
|
key.replace(/(^|_)(\w)/g, (m, $1, $2) => $2.toUpperCase()),
|
||||||
|
),
|
||||||
|
capitalizeKeys,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class HunyuanApi implements LLMApi {
|
||||||
|
path(): string {
|
||||||
|
const accessStore = useAccessStore.getState();
|
||||||
|
|
||||||
|
let baseUrl = "";
|
||||||
|
|
||||||
|
if (accessStore.useCustomConfig) {
|
||||||
|
baseUrl = accessStore.tencentUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.length === 0) {
|
||||||
|
const isApp = !!getClientConfig()?.isApp;
|
||||||
|
baseUrl = isApp
|
||||||
|
? DEFAULT_API_HOST + "/api/proxy/tencent"
|
||||||
|
: ApiPath.Tencent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (baseUrl.endsWith("/")) {
|
||||||
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||||
|
}
|
||||||
|
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Tencent)) {
|
||||||
|
baseUrl = "https://" + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Proxy Endpoint] ", baseUrl);
|
||||||
|
return baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMessage(res: any) {
|
||||||
|
return res.Choices?.at(0)?.Message?.Content ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(options: ChatOptions) {
|
||||||
|
const visionModel = isVisionModel(options.config.model);
|
||||||
|
const messages = options.messages.map((v, index) => ({
|
||||||
|
// "Messages 中 system 角色必须位于列表的最开始"
|
||||||
|
role: index !== 0 && v.role === "system" ? "user" : v.role,
|
||||||
|
content: visionModel ? v.content : getMessageTextContent(v),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const modelConfig = {
|
||||||
|
...useAppConfig.getState().modelConfig,
|
||||||
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||||
|
...{
|
||||||
|
model: options.config.model,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestPayload: RequestPayload = capitalizeKeys({
|
||||||
|
model: modelConfig.model,
|
||||||
|
messages,
|
||||||
|
temperature: modelConfig.temperature,
|
||||||
|
top_p: modelConfig.top_p,
|
||||||
|
stream: options.config.stream,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("[Request] Tencent payload: ", requestPayload);
|
||||||
|
|
||||||
|
const shouldStream = !!options.config.stream;
|
||||||
|
const controller = new AbortController();
|
||||||
|
options.onController?.(controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const chatPath = this.path();
|
||||||
|
const chatPayload = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(requestPayload),
|
||||||
|
signal: controller.signal,
|
||||||
|
headers: getHeaders(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// make a fetch request
|
||||||
|
const requestTimeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldStream) {
|
||||||
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
// animate response to make it looks smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
if (responseText?.length === 0) {
|
||||||
|
options.onError?.(new Error("empty response from server"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||||
|
const fetchText = remainText.slice(0, fetchCount);
|
||||||
|
responseText += fetchText;
|
||||||
|
remainText = remainText.slice(fetchCount);
|
||||||
|
options.onUpdate?.(responseText, fetchText);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start animaion
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
|
const finish = () => {
|
||||||
|
if (!finished) {
|
||||||
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
controller.signal.onabort = finish;
|
||||||
|
|
||||||
|
fetchEventSource(chatPath, {
|
||||||
|
...chatPayload,
|
||||||
|
async onopen(res) {
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
console.log(
|
||||||
|
"[Tencent] request response content type: ",
|
||||||
|
contentType,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (contentType?.startsWith("text/plain")) {
|
||||||
|
responseText = await res.clone().text();
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!res.ok ||
|
||||||
|
!res.headers
|
||||||
|
.get("content-type")
|
||||||
|
?.startsWith(EventStreamContentType) ||
|
||||||
|
res.status !== 200
|
||||||
|
) {
|
||||||
|
const responseTexts = [responseText];
|
||||||
|
let extraInfo = await res.clone().text();
|
||||||
|
try {
|
||||||
|
const resJson = await res.clone().json();
|
||||||
|
extraInfo = prettyObject(resJson);
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
responseTexts.push(Locale.Error.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extraInfo) {
|
||||||
|
responseTexts.push(extraInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseText = responseTexts.join("\n\n");
|
||||||
|
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onmessage(msg) {
|
||||||
|
if (msg.data === "[DONE]" || finished) {
|
||||||
|
return finish();
|
||||||
|
}
|
||||||
|
const text = msg.data;
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.Choices as Array<{
|
||||||
|
Delta: { Content: string };
|
||||||
|
}>;
|
||||||
|
const delta = choices[0]?.Delta?.Content;
|
||||||
|
if (delta) {
|
||||||
|
remainText += delta;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Request] parse error", text, msg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
finish();
|
||||||
|
},
|
||||||
|
onerror(e) {
|
||||||
|
options.onError?.(e);
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
openWhenHidden: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const res = await fetch(chatPath, chatPayload);
|
||||||
|
clearTimeout(requestTimeoutId);
|
||||||
|
|
||||||
|
const resJson = await res.json();
|
||||||
|
const message = this.extractMessage(resJson);
|
||||||
|
options.onFinish(message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("[Request] failed to make a chat request", e);
|
||||||
|
options.onError?.(e as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async usage() {
|
||||||
|
return {
|
||||||
|
used: 0,
|
||||||
|
total: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async models(): Promise<LLMModel[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
import { SettingItem } from "../../common";
|
|
||||||
import Locale from "./locale";
|
|
||||||
|
|
||||||
export type SettingKeys =
|
|
||||||
| "anthropicUrl"
|
|
||||||
| "anthropicApiKey"
|
|
||||||
| "anthropicApiVersion";
|
|
||||||
|
|
||||||
export const ANTHROPIC_BASE_URL = "https://api.anthropic.com";
|
|
||||||
|
|
||||||
export const AnthropicMetas = {
|
|
||||||
ChatPath: "v1/messages",
|
|
||||||
ExampleEndpoint: ANTHROPIC_BASE_URL,
|
|
||||||
Vision: "2023-06-01",
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ClaudeMapper = {
|
|
||||||
assistant: "assistant",
|
|
||||||
user: "user",
|
|
||||||
system: "user",
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
export const modelConfigs = [
|
|
||||||
{
|
|
||||||
name: "claude-instant-1.2",
|
|
||||||
displayName: "claude-instant-1.2",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "claude-2.0",
|
|
||||||
displayName: "claude-2.0",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "claude-2.1",
|
|
||||||
displayName: "claude-2.1",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "claude-3-sonnet-20240229",
|
|
||||||
displayName: "claude-3-sonnet-20240229",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "claude-3-opus-20240229",
|
|
||||||
displayName: "claude-3-opus-20240229",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "claude-3-haiku-20240307",
|
|
||||||
displayName: "claude-3-haiku-20240307",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const preferredRegion: string | string[] = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
export const settingItems: (
|
|
||||||
defaultEndpoint: string,
|
|
||||||
) => SettingItem<SettingKeys>[] = (defaultEndpoint) => [
|
|
||||||
{
|
|
||||||
name: "anthropicUrl",
|
|
||||||
title: Locale.Endpoint.Title,
|
|
||||||
description: Locale.Endpoint.SubTitle + AnthropicMetas.ExampleEndpoint,
|
|
||||||
placeholder: AnthropicMetas.ExampleEndpoint,
|
|
||||||
type: "input",
|
|
||||||
defaultValue: defaultEndpoint,
|
|
||||||
validators: [
|
|
||||||
"required",
|
|
||||||
async (v: any) => {
|
|
||||||
if (typeof v === "string" && !v.startsWith(defaultEndpoint)) {
|
|
||||||
try {
|
|
||||||
new URL(v);
|
|
||||||
} catch (e) {
|
|
||||||
return Locale.Endpoint.Error.IllegalURL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof v === "string" && v.endsWith("/")) {
|
|
||||||
return Locale.Endpoint.Error.EndWithBackslash;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "anthropicApiKey",
|
|
||||||
title: Locale.ApiKey.Title,
|
|
||||||
description: Locale.ApiKey.SubTitle,
|
|
||||||
placeholder: Locale.ApiKey.Placeholder,
|
|
||||||
type: "input",
|
|
||||||
inputType: "password",
|
|
||||||
// validators: ["required"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "anthropicApiVersion",
|
|
||||||
title: Locale.ApiVerion.Title,
|
|
||||||
description: Locale.ApiVerion.SubTitle,
|
|
||||||
defaultValue: AnthropicMetas.Vision,
|
|
||||||
type: "input",
|
|
||||||
// validators: ["required"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,356 +0,0 @@
|
|||||||
import {
|
|
||||||
ANTHROPIC_BASE_URL,
|
|
||||||
AnthropicMetas,
|
|
||||||
ClaudeMapper,
|
|
||||||
SettingKeys,
|
|
||||||
modelConfigs,
|
|
||||||
preferredRegion,
|
|
||||||
settingItems,
|
|
||||||
} from "./config";
|
|
||||||
import {
|
|
||||||
ChatHandlers,
|
|
||||||
InternalChatRequestPayload,
|
|
||||||
IProviderTemplate,
|
|
||||||
ServerConfig,
|
|
||||||
} from "../../common";
|
|
||||||
import {
|
|
||||||
EventStreamContentType,
|
|
||||||
fetchEventSource,
|
|
||||||
} from "@fortaine/fetch-event-source";
|
|
||||||
import Locale from "@/app/locales";
|
|
||||||
import {
|
|
||||||
prettyObject,
|
|
||||||
getTimer,
|
|
||||||
authHeaderName,
|
|
||||||
auth,
|
|
||||||
parseResp,
|
|
||||||
formatMessage,
|
|
||||||
} from "./utils";
|
|
||||||
import { cloneDeep } from "lodash-es";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export type AnthropicProviderSettingKeys = SettingKeys;
|
|
||||||
|
|
||||||
export type MultiBlockContent = {
|
|
||||||
type: "image" | "text";
|
|
||||||
source?: {
|
|
||||||
type: string;
|
|
||||||
media_type: string;
|
|
||||||
data: string;
|
|
||||||
};
|
|
||||||
text?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type AnthropicMessage = {
|
|
||||||
role: (typeof ClaudeMapper)[keyof typeof ClaudeMapper];
|
|
||||||
content: string | MultiBlockContent[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface AnthropicChatRequest {
|
|
||||||
model: string; // The model that will complete your prompt.
|
|
||||||
messages: AnthropicMessage[]; // The prompt that you want Claude to complete.
|
|
||||||
max_tokens: number; // The maximum number of tokens to generate before stopping.
|
|
||||||
stop_sequences?: string[]; // Sequences that will cause the model to stop generating completion text.
|
|
||||||
temperature?: number; // Amount of randomness injected into the response.
|
|
||||||
top_p?: number; // Use nucleus sampling.
|
|
||||||
top_k?: number; // Only sample from the top K options for each subsequent token.
|
|
||||||
metadata?: object; // An object describing metadata about the request.
|
|
||||||
stream?: boolean; // Whether to incrementally stream the response using server-sent events.
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ChatRequest {
|
|
||||||
model: string; // The model that will complete your prompt.
|
|
||||||
prompt: string; // The prompt that you want Claude to complete.
|
|
||||||
max_tokens_to_sample: number; // The maximum number of tokens to generate before stopping.
|
|
||||||
stop_sequences?: string[]; // Sequences that will cause the model to stop generating completion text.
|
|
||||||
temperature?: number; // Amount of randomness injected into the response.
|
|
||||||
top_p?: number; // Use nucleus sampling.
|
|
||||||
top_k?: number; // Only sample from the top K options for each subsequent token.
|
|
||||||
metadata?: object; // An object describing metadata about the request.
|
|
||||||
stream?: boolean; // Whether to incrementally stream the response using server-sent events.
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProviderTemplate = IProviderTemplate<
|
|
||||||
SettingKeys,
|
|
||||||
"anthropic",
|
|
||||||
typeof AnthropicMetas
|
|
||||||
>;
|
|
||||||
|
|
||||||
export default class AnthropicProvider implements ProviderTemplate {
|
|
||||||
apiRouteRootName = "/api/provider/anthropic" as const;
|
|
||||||
allowedApiMethods: ["GET", "POST"] = ["GET", "POST"];
|
|
||||||
|
|
||||||
runtime = "edge" as const;
|
|
||||||
preferredRegion = preferredRegion;
|
|
||||||
|
|
||||||
name = "anthropic" as const;
|
|
||||||
|
|
||||||
metas = AnthropicMetas;
|
|
||||||
|
|
||||||
providerMeta = {
|
|
||||||
displayName: "Anthropic",
|
|
||||||
settingItems: settingItems(
|
|
||||||
`${this.apiRouteRootName}//${AnthropicMetas.ChatPath}`,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
defaultModels = modelConfigs;
|
|
||||||
|
|
||||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
|
||||||
const {
|
|
||||||
messages: outsideMessages,
|
|
||||||
model,
|
|
||||||
stream,
|
|
||||||
modelConfig,
|
|
||||||
providerConfig,
|
|
||||||
} = payload;
|
|
||||||
const { anthropicApiKey, anthropicApiVersion, anthropicUrl } =
|
|
||||||
providerConfig;
|
|
||||||
const { temperature, top_p, max_tokens } = modelConfig;
|
|
||||||
|
|
||||||
const keys = ["system", "user"];
|
|
||||||
|
|
||||||
// roles must alternate between "user" and "assistant" in claude, so add a fake assistant message between two user messages
|
|
||||||
const messages = cloneDeep(outsideMessages);
|
|
||||||
|
|
||||||
for (let i = 0; i < messages.length - 1; i++) {
|
|
||||||
const message = messages[i];
|
|
||||||
const nextMessage = messages[i + 1];
|
|
||||||
|
|
||||||
if (keys.includes(message.role) && keys.includes(nextMessage.role)) {
|
|
||||||
messages[i] = [
|
|
||||||
message,
|
|
||||||
{
|
|
||||||
role: "assistant",
|
|
||||||
content: ";",
|
|
||||||
},
|
|
||||||
] as any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const prompt = formatMessage(messages, payload.isVisionModel);
|
|
||||||
|
|
||||||
const requestBody: AnthropicChatRequest = {
|
|
||||||
messages: prompt,
|
|
||||||
stream,
|
|
||||||
model,
|
|
||||||
max_tokens,
|
|
||||||
temperature,
|
|
||||||
top_p,
|
|
||||||
top_k: 5,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
[authHeaderName]: anthropicApiKey ?? "",
|
|
||||||
"anthropic-version": anthropicApiVersion ?? "",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(requestBody),
|
|
||||||
method: "POST",
|
|
||||||
url: anthropicUrl!,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async request(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
const authValue = req.headers.get(authHeaderName) ?? "";
|
|
||||||
|
|
||||||
const path = `${req.nextUrl.pathname}`.replaceAll(
|
|
||||||
this.apiRouteRootName,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
const baseUrl = serverConfig.anthropicUrl || ANTHROPIC_BASE_URL;
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", baseUrl);
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}${path}`;
|
|
||||||
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
[authHeaderName]: authValue,
|
|
||||||
"anthropic-version":
|
|
||||||
req.headers.get("anthropic-version") ||
|
|
||||||
serverConfig.anthropicApiVersion ||
|
|
||||||
AnthropicMetas.Vision,
|
|
||||||
},
|
|
||||||
method: req.method,
|
|
||||||
body: req.body,
|
|
||||||
redirect: "manual",
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
signal: controller.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log("[Anthropic request]", fetchOptions.headers, req.method);
|
|
||||||
try {
|
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
|
||||||
|
|
||||||
// to prevent browser prompt for credentials
|
|
||||||
const newHeaders = new Headers(res.headers);
|
|
||||||
newHeaders.delete("www-authenticate");
|
|
||||||
// to disable nginx buffering
|
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
|
||||||
|
|
||||||
return new NextResponse(res.body, {
|
|
||||||
status: res.status,
|
|
||||||
statusText: res.statusText,
|
|
||||||
headers: newHeaders,
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
const res = await fetch(requestPayload.url, {
|
|
||||||
headers: {
|
|
||||||
...requestPayload.headers,
|
|
||||||
},
|
|
||||||
body: requestPayload.body,
|
|
||||||
method: requestPayload.method,
|
|
||||||
signal: timer.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
timer.clear();
|
|
||||||
|
|
||||||
const resJson = await res.json();
|
|
||||||
const message = parseResp(resJson);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
fetchEventSource(requestPayload.url, {
|
|
||||||
...requestPayload,
|
|
||||||
fetch,
|
|
||||||
async onopen(res) {
|
|
||||||
timer.clear();
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log("[OpenAI] request response content type: ", contentType);
|
|
||||||
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
const responseText = await res.clone().text();
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!res.ok ||
|
|
||||||
!res.headers
|
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
|
||||||
const responseTexts = [];
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseText = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
if (msg.data === "[DONE]") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const text = msg.data;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(text);
|
|
||||||
const choices = json.choices as Array<{
|
|
||||||
delta: { content: string };
|
|
||||||
}>;
|
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
|
|
||||||
if (delta) {
|
|
||||||
handlers.onProgress(delta);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose() {
|
|
||||||
handlers.onFinish();
|
|
||||||
},
|
|
||||||
onerror(e) {
|
|
||||||
handlers.onError(e);
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
serverSideRequestHandler: ProviderTemplate["serverSideRequestHandler"] =
|
|
||||||
async (req, config) => {
|
|
||||||
const { subpath } = req;
|
|
||||||
const ALLOWD_PATH = [AnthropicMetas.ChatPath];
|
|
||||||
|
|
||||||
if (!ALLOWD_PATH.includes(subpath)) {
|
|
||||||
console.log("[Anthropic Route] forbidden path ", subpath);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to request " + subpath,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 403,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const authResult = auth(req, config);
|
|
||||||
|
|
||||||
if (authResult.error) {
|
|
||||||
return NextResponse.json(authResult, {
|
|
||||||
status: 401,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await this.request(req, config);
|
|
||||||
return response;
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Anthropic] ", e);
|
|
||||||
return NextResponse.json(prettyObject(e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
import { getLocaleText } from "../../common";
|
|
||||||
|
|
||||||
export default getLocaleText<
|
|
||||||
{
|
|
||||||
ApiKey: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Placeholder: string;
|
|
||||||
};
|
|
||||||
Endpoint: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: string;
|
|
||||||
IllegalURL: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
ApiVerion: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
"en"
|
|
||||||
>(
|
|
||||||
{
|
|
||||||
cn: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "接口密钥",
|
|
||||||
SubTitle: "使用自定义 Anthropic Key 绕过密码访问限制",
|
|
||||||
Placeholder: "Anthropic API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "接口地址",
|
|
||||||
SubTitle: "样例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」结尾",
|
|
||||||
IllegalURL: "请输入一个完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "接口版本 (claude api version)",
|
|
||||||
SubTitle: "选择一个特定的 API 版本输入",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
en: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "Anthropic API Key",
|
|
||||||
SubTitle:
|
|
||||||
"Use a custom Anthropic Key to bypass password access restrictions",
|
|
||||||
Placeholder: "Anthropic API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Endpoint Address",
|
|
||||||
SubTitle: "Example:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Cannot end with '/'",
|
|
||||||
IllegalURL: "Please enter a complete available url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "API Version (claude api version)",
|
|
||||||
SubTitle: "Select and input a specific API version",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
pt: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "Chave API Anthropic",
|
|
||||||
SubTitle: "Verifique sua chave API do console Anthropic",
|
|
||||||
Placeholder: "Chave API Anthropic",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Endpoint Address",
|
|
||||||
SubTitle: "Exemplo: ",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Não é possível terminar com '/'",
|
|
||||||
IllegalURL: "Insira um URL completo disponível",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "Versão API (Versão api claude)",
|
|
||||||
SubTitle: "Verifique sua versão API do console Anthropic",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
sk: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API kľúč Anthropic",
|
|
||||||
SubTitle: "Skontrolujte svoj API kľúč v Anthropic konzole",
|
|
||||||
Placeholder: "API kľúč Anthropic",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Adresa koncového bodu",
|
|
||||||
SubTitle: "Príklad:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Nemôže končiť znakom „/“",
|
|
||||||
IllegalURL: "Zadajte úplnú dostupnú adresu URL",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "Verzia API (claude verzia API)",
|
|
||||||
SubTitle: "Vyberte špecifickú verziu časti",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tw: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API 金鑰",
|
|
||||||
SubTitle: "從 Anthropic AI 取得您的 API 金鑰",
|
|
||||||
Placeholder: "Anthropic API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "終端地址",
|
|
||||||
SubTitle: "範例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」結尾",
|
|
||||||
IllegalURL: "請輸入一個完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "API 版本 (claude api version)",
|
|
||||||
SubTitle: "選擇一個特定的 API 版本輸入",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"en",
|
|
||||||
);
|
|
||||||
@@ -1,151 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import {
|
|
||||||
RequestMessage,
|
|
||||||
ServerConfig,
|
|
||||||
getIP,
|
|
||||||
getMessageTextContent,
|
|
||||||
} from "../../common";
|
|
||||||
import { ClaudeMapper } from "./config";
|
|
||||||
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
|
||||||
export const authHeaderName = "x-api-key";
|
|
||||||
|
|
||||||
export function trimEnd(s: string, end = " ") {
|
|
||||||
if (end.length === 0) return s;
|
|
||||||
|
|
||||||
while (s.endsWith(end)) {
|
|
||||||
s = s.slice(0, -end.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function bearer(value: string) {
|
|
||||||
return `Bearer ${value.trim()}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prettyObject(msg: any) {
|
|
||||||
const obj = msg;
|
|
||||||
if (typeof msg !== "string") {
|
|
||||||
msg = JSON.stringify(msg, null, " ");
|
|
||||||
}
|
|
||||||
if (msg === "{}") {
|
|
||||||
return obj.toString();
|
|
||||||
}
|
|
||||||
if (msg.startsWith("```json")) {
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
return ["```json", msg, "```"].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTimer() {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
// make a fetch request
|
|
||||||
const requestTimeoutId = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
REQUEST_TIMEOUT_MS,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...controller,
|
|
||||||
clear: () => {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function auth(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const apiKey = req.headers.get(authHeaderName);
|
|
||||||
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (serverConfig.hideUserApiKey && apiKey) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to access with your own api key",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apiKey) {
|
|
||||||
console.log("[Auth] use user api key");
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// if user does not provide an api key, inject system api key
|
|
||||||
const systemApiKey = serverConfig.anthropicApiKey;
|
|
||||||
|
|
||||||
if (systemApiKey) {
|
|
||||||
console.log("[Auth] use system api key");
|
|
||||||
req.headers.set(authHeaderName, systemApiKey);
|
|
||||||
} else {
|
|
||||||
console.log("[Auth] admin did not provide an api key");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseResp(res: any) {
|
|
||||||
return {
|
|
||||||
message: res?.content?.[0]?.text ?? "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatMessage(
|
|
||||||
messages: RequestMessage[],
|
|
||||||
isVisionModel?: boolean,
|
|
||||||
) {
|
|
||||||
return messages
|
|
||||||
.flat()
|
|
||||||
.filter((v) => {
|
|
||||||
if (!v.content) return false;
|
|
||||||
if (typeof v.content === "string" && !v.content.trim()) return false;
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
.map((v) => {
|
|
||||||
const { role, content } = v;
|
|
||||||
const insideRole = ClaudeMapper[role] ?? "user";
|
|
||||||
|
|
||||||
if (!isVisionModel || typeof content === "string") {
|
|
||||||
return {
|
|
||||||
role: insideRole,
|
|
||||||
content: getMessageTextContent(v),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
role: insideRole,
|
|
||||||
content: content
|
|
||||||
.filter((v) => v.image_url || v.text)
|
|
||||||
.map(({ type, text, image_url }) => {
|
|
||||||
if (type === "text") {
|
|
||||||
return {
|
|
||||||
type,
|
|
||||||
text: text!,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const { url = "" } = image_url || {};
|
|
||||||
const colonIndex = url.indexOf(":");
|
|
||||||
const semicolonIndex = url.indexOf(";");
|
|
||||||
const comma = url.indexOf(",");
|
|
||||||
|
|
||||||
const mimeType = url.slice(colonIndex + 1, semicolonIndex);
|
|
||||||
const encodeType = url.slice(semicolonIndex + 1, comma);
|
|
||||||
const data = url.slice(comma + 1);
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: "image" as const,
|
|
||||||
source: {
|
|
||||||
type: encodeType,
|
|
||||||
media_type: mimeType,
|
|
||||||
data,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
import Locale from "./locale";
|
|
||||||
|
|
||||||
import { SettingItem } from "../../common";
|
|
||||||
import { modelConfigs as openaiModelConfigs } from "../openai/config";
|
|
||||||
|
|
||||||
export const AzureMetas = {
|
|
||||||
ExampleEndpoint: "https://{resource-url}/openai/deployments/{deploy-id}",
|
|
||||||
ChatPath: "chat/completions",
|
|
||||||
ListModelPath: "v1/models",
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SettingKeys = "azureUrl" | "azureApiKey" | "azureApiVersion";
|
|
||||||
|
|
||||||
export const preferredRegion: string | string[] = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
export const modelConfigs = openaiModelConfigs;
|
|
||||||
|
|
||||||
export const settingItems: (
|
|
||||||
defaultEndpoint: string,
|
|
||||||
) => SettingItem<SettingKeys>[] = (defaultEndpoint) => [
|
|
||||||
{
|
|
||||||
name: "azureUrl",
|
|
||||||
title: Locale.Endpoint.Title,
|
|
||||||
description: Locale.Endpoint.SubTitle + AzureMetas.ExampleEndpoint,
|
|
||||||
placeholder: AzureMetas.ExampleEndpoint,
|
|
||||||
type: "input",
|
|
||||||
defaultValue: defaultEndpoint,
|
|
||||||
validators: [
|
|
||||||
async (v: any) => {
|
|
||||||
if (typeof v === "string") {
|
|
||||||
try {
|
|
||||||
new URL(v);
|
|
||||||
} catch (e) {
|
|
||||||
return Locale.Endpoint.Error.IllegalURL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof v === "string" && v.endsWith("/")) {
|
|
||||||
return Locale.Endpoint.Error.EndWithBackslash;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "azureApiKey",
|
|
||||||
title: Locale.ApiKey.Title,
|
|
||||||
description: Locale.ApiKey.SubTitle,
|
|
||||||
placeholder: Locale.ApiKey.Placeholder,
|
|
||||||
type: "input",
|
|
||||||
inputType: "password",
|
|
||||||
validators: ["required"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "azureApiVersion",
|
|
||||||
title: Locale.ApiVerion.Title,
|
|
||||||
description: Locale.ApiVerion.SubTitle,
|
|
||||||
placeholder: "2023-08-01-preview",
|
|
||||||
type: "input",
|
|
||||||
validators: ["required"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,408 +0,0 @@
|
|||||||
import {
|
|
||||||
settingItems,
|
|
||||||
SettingKeys,
|
|
||||||
modelConfigs,
|
|
||||||
AzureMetas,
|
|
||||||
preferredRegion,
|
|
||||||
} from "./config";
|
|
||||||
import {
|
|
||||||
ChatHandlers,
|
|
||||||
InternalChatRequestPayload,
|
|
||||||
IProviderTemplate,
|
|
||||||
ModelInfo,
|
|
||||||
getMessageTextContent,
|
|
||||||
ServerConfig,
|
|
||||||
} from "../../common";
|
|
||||||
import {
|
|
||||||
EventStreamContentType,
|
|
||||||
fetchEventSource,
|
|
||||||
} from "@fortaine/fetch-event-source";
|
|
||||||
import Locale from "@/app/locales";
|
|
||||||
import {
|
|
||||||
auth,
|
|
||||||
authHeaderName,
|
|
||||||
getHeaders,
|
|
||||||
getTimer,
|
|
||||||
makeAzurePath,
|
|
||||||
parseResp,
|
|
||||||
prettyObject,
|
|
||||||
} from "./utils";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export type AzureProviderSettingKeys = SettingKeys;
|
|
||||||
|
|
||||||
export const ROLES = ["system", "user", "assistant"] as const;
|
|
||||||
export type MessageRole = (typeof ROLES)[number];
|
|
||||||
|
|
||||||
export interface MultimodalContent {
|
|
||||||
type: "text" | "image_url";
|
|
||||||
text?: string;
|
|
||||||
image_url?: {
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RequestMessage {
|
|
||||||
role: MessageRole;
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RequestPayload {
|
|
||||||
messages: {
|
|
||||||
role: "system" | "user" | "assistant";
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}[];
|
|
||||||
stream?: boolean;
|
|
||||||
model: string;
|
|
||||||
temperature: number;
|
|
||||||
presence_penalty: number;
|
|
||||||
frequency_penalty: number;
|
|
||||||
top_p: number;
|
|
||||||
max_tokens?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ModelList {
|
|
||||||
object: "list";
|
|
||||||
data: Array<{
|
|
||||||
capabilities: {
|
|
||||||
fine_tune: boolean;
|
|
||||||
inference: boolean;
|
|
||||||
completion: boolean;
|
|
||||||
chat_completion: boolean;
|
|
||||||
embeddings: boolean;
|
|
||||||
};
|
|
||||||
lifecycle_status: "generally-available";
|
|
||||||
id: string;
|
|
||||||
created_at: number;
|
|
||||||
object: "model";
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OpenAIListModelResponse {
|
|
||||||
object: string;
|
|
||||||
data: Array<{
|
|
||||||
id: string;
|
|
||||||
object: string;
|
|
||||||
root: string;
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProviderTemplate = IProviderTemplate<
|
|
||||||
SettingKeys,
|
|
||||||
"azure",
|
|
||||||
typeof AzureMetas
|
|
||||||
>;
|
|
||||||
|
|
||||||
export default class Azure implements ProviderTemplate {
|
|
||||||
apiRouteRootName: "/api/provider/azure" = "/api/provider/azure";
|
|
||||||
allowedApiMethods: (
|
|
||||||
| "POST"
|
|
||||||
| "GET"
|
|
||||||
| "OPTIONS"
|
|
||||||
| "PUT"
|
|
||||||
| "PATCH"
|
|
||||||
| "DELETE"
|
|
||||||
)[] = ["POST", "GET"];
|
|
||||||
runtime = "edge" as const;
|
|
||||||
|
|
||||||
preferredRegion = preferredRegion;
|
|
||||||
|
|
||||||
name = "azure" as const;
|
|
||||||
metas = AzureMetas;
|
|
||||||
|
|
||||||
defaultModels = modelConfigs;
|
|
||||||
|
|
||||||
providerMeta = {
|
|
||||||
displayName: "Azure",
|
|
||||||
settingItems: settingItems(
|
|
||||||
`${this.apiRouteRootName}/${AzureMetas.ChatPath}`,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
|
||||||
const {
|
|
||||||
messages,
|
|
||||||
isVisionModel,
|
|
||||||
model,
|
|
||||||
stream,
|
|
||||||
modelConfig: {
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
max_tokens,
|
|
||||||
},
|
|
||||||
providerConfig: { azureUrl, azureApiVersion },
|
|
||||||
} = payload;
|
|
||||||
|
|
||||||
const openAiMessages = messages.map((v) => ({
|
|
||||||
role: v.role,
|
|
||||||
content: isVisionModel ? v.content : getMessageTextContent(v),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const requestPayload: RequestPayload = {
|
|
||||||
messages: openAiMessages,
|
|
||||||
stream,
|
|
||||||
model,
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
};
|
|
||||||
|
|
||||||
// add max_tokens to vision model
|
|
||||||
if (isVisionModel) {
|
|
||||||
requestPayload["max_tokens"] = Math.max(max_tokens, 4000);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Request] openai payload: ", requestPayload);
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: getHeaders(payload.providerConfig.azureApiKey),
|
|
||||||
body: JSON.stringify(requestPayload),
|
|
||||||
method: "POST",
|
|
||||||
url: `${azureUrl}?api-version=${azureApiVersion!}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async requestAzure(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
const authValue =
|
|
||||||
req.headers
|
|
||||||
.get("Authorization")
|
|
||||||
?.trim()
|
|
||||||
.replaceAll("Bearer ", "")
|
|
||||||
.trim() ?? "";
|
|
||||||
|
|
||||||
const { azureUrl, azureApiVersion } = serverConfig;
|
|
||||||
|
|
||||||
if (!azureUrl) {
|
|
||||||
return NextResponse.json({
|
|
||||||
error: true,
|
|
||||||
message: `missing AZURE_URL in server env vars`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!azureApiVersion) {
|
|
||||||
return NextResponse.json({
|
|
||||||
error: true,
|
|
||||||
message: `missing AZURE_API_VERSION in server env vars`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let path = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
|
|
||||||
this.apiRouteRootName,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
path = makeAzurePath(path, azureApiVersion);
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", azureUrl);
|
|
||||||
|
|
||||||
const fetchUrl = `${azureUrl}/${path}`;
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
[authHeaderName]: authValue,
|
|
||||||
},
|
|
||||||
method: req.method,
|
|
||||||
body: req.body,
|
|
||||||
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
|
||||||
redirect: "manual",
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
signal: controller.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
|
||||||
|
|
||||||
// to prevent browser prompt for credentials
|
|
||||||
const newHeaders = new Headers(res.headers);
|
|
||||||
newHeaders.delete("www-authenticate");
|
|
||||||
// to disable nginx buffering
|
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
|
||||||
|
|
||||||
// The latest version of the OpenAI API forced the content-encoding to be "br" in json response
|
|
||||||
// So if the streaming is disabled, we need to remove the content-encoding header
|
|
||||||
// Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header
|
|
||||||
// The browser will try to decode the response with brotli and fail
|
|
||||||
newHeaders.delete("content-encoding");
|
|
||||||
|
|
||||||
return new NextResponse(res.body, {
|
|
||||||
status: res.status,
|
|
||||||
statusText: res.statusText,
|
|
||||||
headers: newHeaders,
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
const res = await fetch(requestPayload.url, {
|
|
||||||
headers: {
|
|
||||||
...requestPayload.headers,
|
|
||||||
},
|
|
||||||
body: requestPayload.body,
|
|
||||||
method: requestPayload.method,
|
|
||||||
signal: timer.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
timer.clear();
|
|
||||||
|
|
||||||
const resJson = await res.json();
|
|
||||||
const message = parseResp(resJson);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
fetchEventSource(requestPayload.url, {
|
|
||||||
...requestPayload,
|
|
||||||
fetch,
|
|
||||||
async onopen(res) {
|
|
||||||
timer.clear();
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log("[OpenAI] request response content type: ", contentType);
|
|
||||||
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
const responseText = await res.clone().text();
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!res.ok ||
|
|
||||||
!res.headers
|
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
|
||||||
const responseTexts = [];
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseText = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
if (msg.data === "[DONE]") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const text = msg.data;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(text);
|
|
||||||
const choices = json.choices as Array<{
|
|
||||||
delta: { content: string };
|
|
||||||
}>;
|
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
|
|
||||||
if (delta) {
|
|
||||||
handlers.onProgress(delta);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose() {
|
|
||||||
handlers.onFinish();
|
|
||||||
},
|
|
||||||
onerror(e) {
|
|
||||||
handlers.onError(e);
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAvailableModels(
|
|
||||||
providerConfig: Record<SettingKeys, string>,
|
|
||||||
): Promise<ModelInfo[]> {
|
|
||||||
const { azureApiKey, azureUrl } = providerConfig;
|
|
||||||
const res = await fetch(`${azureUrl}/${AzureMetas.ListModelPath}`, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${azureApiKey}`,
|
|
||||||
},
|
|
||||||
method: "GET",
|
|
||||||
});
|
|
||||||
const data: ModelList = await res.json();
|
|
||||||
|
|
||||||
return data.data.map((o) => ({
|
|
||||||
name: o.id,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
serverSideRequestHandler: ProviderTemplate["serverSideRequestHandler"] =
|
|
||||||
async (req, config) => {
|
|
||||||
const { subpath } = req;
|
|
||||||
const ALLOWD_PATH = [AzureMetas.ChatPath];
|
|
||||||
|
|
||||||
if (!ALLOWD_PATH.includes(subpath)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to request " + subpath,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 403,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const authResult = auth(req, config);
|
|
||||||
if (authResult.error) {
|
|
||||||
return NextResponse.json(authResult, {
|
|
||||||
status: 401,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await this.requestAzure(req, config);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (e) {
|
|
||||||
return NextResponse.json(prettyObject(e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import { getLocaleText } from "../../common";
|
|
||||||
|
|
||||||
export default getLocaleText<
|
|
||||||
{
|
|
||||||
ApiKey: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Placeholder: string;
|
|
||||||
};
|
|
||||||
Endpoint: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: string;
|
|
||||||
IllegalURL: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
ApiVerion: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
"en"
|
|
||||||
>(
|
|
||||||
{
|
|
||||||
cn: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "接口密钥",
|
|
||||||
SubTitle: "使用自定义 Azure Key 绕过密码访问限制",
|
|
||||||
Placeholder: "Azure API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "接口地址",
|
|
||||||
SubTitle: "样例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」结尾",
|
|
||||||
IllegalURL: "请输入一个完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "接口版本 (azure api version)",
|
|
||||||
SubTitle: "选择指定的部分版本",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
en: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "Azure Api Key",
|
|
||||||
SubTitle: "Check your api key from Azure console",
|
|
||||||
Placeholder: "Azure Api Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Azure Endpoint",
|
|
||||||
SubTitle: "Example: ",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Cannot end with '/'",
|
|
||||||
IllegalURL: "Please enter a complete available url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "Azure Api Version",
|
|
||||||
SubTitle: "Check your api version from azure console",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
pt: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "Chave API Azure",
|
|
||||||
SubTitle: "Verifique sua chave API do console Azure",
|
|
||||||
Placeholder: "Chave API Azure",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Endpoint Azure",
|
|
||||||
SubTitle: "Exemplo: ",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Não é possível terminar com '/'",
|
|
||||||
IllegalURL: "Insira um URL completo disponível",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "Versão API Azure",
|
|
||||||
SubTitle: "Verifique sua versão API do console Azure",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
sk: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API kľúč Azure",
|
|
||||||
SubTitle: "Skontrolujte svoj API kľúč v Azure konzole",
|
|
||||||
Placeholder: "API kľúč Azure",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Koncový bod Azure",
|
|
||||||
SubTitle: "Príklad: ",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Nemôže končiť znakom „/“",
|
|
||||||
IllegalURL: "Zadajte úplnú dostupnú adresu URL",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "Verzia API Azure",
|
|
||||||
SubTitle: "Skontrolujte svoju verziu API v Azure konzole",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tw: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "介面金鑰",
|
|
||||||
SubTitle: "使用自定義 Azure Key 繞過密碼存取限制",
|
|
||||||
Placeholder: "Azure API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "介面(Endpoint) 地址",
|
|
||||||
SubTitle: "樣例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」結尾",
|
|
||||||
IllegalURL: "請輸入一個完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVerion: {
|
|
||||||
Title: "介面版本 (azure api version)",
|
|
||||||
SubTitle: "選擇指定的部分版本",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"en",
|
|
||||||
);
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import { ServerConfig, getIP } from "../../common";
|
|
||||||
|
|
||||||
export const authHeaderName = "api-key";
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
|
||||||
|
|
||||||
export function getHeaders(azureApiKey?: string) {
|
|
||||||
const headers: Record<string, string> = {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (validString(azureApiKey)) {
|
|
||||||
headers[authHeaderName] = makeBearer(azureApiKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseResp(res: any) {
|
|
||||||
return {
|
|
||||||
message: res.choices?.at(0)?.message?.content ?? "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function makeAzurePath(path: string, apiVersion: string) {
|
|
||||||
// should add api-key to query string
|
|
||||||
path += `${path.includes("?") ? "&" : "?"}api-version=${apiVersion}`;
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prettyObject(msg: any) {
|
|
||||||
const obj = msg;
|
|
||||||
if (typeof msg !== "string") {
|
|
||||||
msg = JSON.stringify(msg, null, " ");
|
|
||||||
}
|
|
||||||
if (msg === "{}") {
|
|
||||||
return obj.toString();
|
|
||||||
}
|
|
||||||
if (msg.startsWith("```json")) {
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
return ["```json", msg, "```"].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
|
||||||
export const validString = (x?: string): x is string =>
|
|
||||||
Boolean(x && x.length > 0);
|
|
||||||
|
|
||||||
export function parseApiKey(bearToken: string) {
|
|
||||||
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
|
||||||
|
|
||||||
return {
|
|
||||||
apiKey: token,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTimer() {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
// make a fetch request
|
|
||||||
const requestTimeoutId = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
REQUEST_TIMEOUT_MS,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...controller,
|
|
||||||
clear: () => {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function auth(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const authToken = req.headers.get(authHeaderName) ?? "";
|
|
||||||
|
|
||||||
const { hideUserApiKey, apiKey: systemApiKey } = serverConfig;
|
|
||||||
|
|
||||||
const { apiKey } = parseApiKey(authToken);
|
|
||||||
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (hideUserApiKey && apiKey) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to access with your own api key",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apiKey) {
|
|
||||||
console.log("[Auth] use user api key");
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (systemApiKey) {
|
|
||||||
console.log("[Auth] use system api key");
|
|
||||||
req.headers.set("Authorization", `Bearer ${systemApiKey}`);
|
|
||||||
} else {
|
|
||||||
console.log("[Auth] admin did not provide an api key");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
import { SettingItem } from "../../common";
|
|
||||||
import Locale from "./locale";
|
|
||||||
|
|
||||||
export const preferredRegion: string | string[] = [
|
|
||||||
"bom1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
export const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/";
|
|
||||||
|
|
||||||
export const GoogleMetas = {
|
|
||||||
ExampleEndpoint: GEMINI_BASE_URL,
|
|
||||||
ChatPath: (modelName: string) => `v1beta/models/${modelName}:generateContent`,
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SettingKeys = "googleUrl" | "googleApiKey" | "googleApiVersion";
|
|
||||||
|
|
||||||
export const modelConfigs = [
|
|
||||||
{
|
|
||||||
name: "gemini-1.0-pro",
|
|
||||||
displayName: "gemini-1.0-pro",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gemini-1.5-pro-latest",
|
|
||||||
displayName: "gemini-1.5-pro-latest",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gemini-pro-vision",
|
|
||||||
displayName: "gemini-pro-vision",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const settingItems: (
|
|
||||||
defaultEndpoint: string,
|
|
||||||
) => SettingItem<SettingKeys>[] = (defaultEndpoint) => [
|
|
||||||
{
|
|
||||||
name: "googleUrl",
|
|
||||||
title: Locale.Endpoint.Title,
|
|
||||||
description: Locale.Endpoint.SubTitle + GoogleMetas.ExampleEndpoint,
|
|
||||||
placeholder: GoogleMetas.ExampleEndpoint,
|
|
||||||
type: "input",
|
|
||||||
defaultValue: defaultEndpoint,
|
|
||||||
validators: [
|
|
||||||
async (v: any) => {
|
|
||||||
if (typeof v === "string") {
|
|
||||||
try {
|
|
||||||
new URL(v);
|
|
||||||
} catch (e) {
|
|
||||||
return Locale.Endpoint.Error.IllegalURL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof v === "string" && v.endsWith("/")) {
|
|
||||||
return Locale.Endpoint.Error.EndWithBackslash;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "googleApiKey",
|
|
||||||
title: Locale.ApiKey.Title,
|
|
||||||
description: Locale.ApiKey.SubTitle,
|
|
||||||
placeholder: Locale.ApiKey.Placeholder,
|
|
||||||
type: "input",
|
|
||||||
inputType: "password",
|
|
||||||
// validators: ["required"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "googleApiVersion",
|
|
||||||
title: Locale.ApiVersion.Title,
|
|
||||||
description: Locale.ApiVersion.SubTitle,
|
|
||||||
placeholder: "2023-08-01-preview",
|
|
||||||
type: "input",
|
|
||||||
// validators: ["required"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,353 +0,0 @@
|
|||||||
import {
|
|
||||||
SettingKeys,
|
|
||||||
modelConfigs,
|
|
||||||
settingItems,
|
|
||||||
GoogleMetas,
|
|
||||||
GEMINI_BASE_URL,
|
|
||||||
preferredRegion,
|
|
||||||
} from "./config";
|
|
||||||
import {
|
|
||||||
ChatHandlers,
|
|
||||||
InternalChatRequestPayload,
|
|
||||||
IProviderTemplate,
|
|
||||||
ModelInfo,
|
|
||||||
StandChatReponseMessage,
|
|
||||||
getMessageTextContent,
|
|
||||||
getMessageImages,
|
|
||||||
} from "../../common";
|
|
||||||
import {
|
|
||||||
auth,
|
|
||||||
ensureProperEnding,
|
|
||||||
getTimer,
|
|
||||||
parseResp,
|
|
||||||
urlParamApikeyName,
|
|
||||||
} from "./utils";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export type GoogleProviderSettingKeys = SettingKeys;
|
|
||||||
|
|
||||||
interface ModelList {
|
|
||||||
models: Array<{
|
|
||||||
name: string;
|
|
||||||
baseModelId: string;
|
|
||||||
version: string;
|
|
||||||
displayName: string;
|
|
||||||
description: string;
|
|
||||||
inputTokenLimit: number; // Integer
|
|
||||||
outputTokenLimit: number; // Integer
|
|
||||||
supportedGenerationMethods: [string];
|
|
||||||
temperature: number;
|
|
||||||
topP: number;
|
|
||||||
topK: number; // Integer
|
|
||||||
}>;
|
|
||||||
nextPageToken: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProviderTemplate = IProviderTemplate<
|
|
||||||
SettingKeys,
|
|
||||||
"azure",
|
|
||||||
typeof GoogleMetas
|
|
||||||
>;
|
|
||||||
|
|
||||||
export default class GoogleProvider
|
|
||||||
implements IProviderTemplate<SettingKeys, "google", typeof GoogleMetas>
|
|
||||||
{
|
|
||||||
allowedApiMethods: (
|
|
||||||
| "POST"
|
|
||||||
| "GET"
|
|
||||||
| "OPTIONS"
|
|
||||||
| "PUT"
|
|
||||||
| "PATCH"
|
|
||||||
| "DELETE"
|
|
||||||
)[] = ["GET", "POST"];
|
|
||||||
runtime = "edge" as const;
|
|
||||||
|
|
||||||
apiRouteRootName: "/api/provider/google" = "/api/provider/google";
|
|
||||||
|
|
||||||
preferredRegion = preferredRegion;
|
|
||||||
|
|
||||||
name = "google" as const;
|
|
||||||
metas = GoogleMetas;
|
|
||||||
|
|
||||||
providerMeta = {
|
|
||||||
displayName: "Google",
|
|
||||||
settingItems: settingItems(this.apiRouteRootName),
|
|
||||||
};
|
|
||||||
defaultModels = modelConfigs;
|
|
||||||
|
|
||||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
|
||||||
const {
|
|
||||||
messages,
|
|
||||||
isVisionModel,
|
|
||||||
model,
|
|
||||||
stream,
|
|
||||||
modelConfig,
|
|
||||||
providerConfig,
|
|
||||||
} = payload;
|
|
||||||
const { googleUrl, googleApiKey } = providerConfig;
|
|
||||||
const { temperature, top_p, max_tokens } = modelConfig;
|
|
||||||
|
|
||||||
const internalMessages = messages.map((v) => {
|
|
||||||
let parts: any[] = [{ text: getMessageTextContent(v) }];
|
|
||||||
|
|
||||||
if (isVisionModel) {
|
|
||||||
const images = getMessageImages(v);
|
|
||||||
if (images.length > 0) {
|
|
||||||
parts = parts.concat(
|
|
||||||
images.map((image) => {
|
|
||||||
const imageType = image.split(";")[0].split(":")[1];
|
|
||||||
const imageData = image.split(",")[1];
|
|
||||||
return {
|
|
||||||
inline_data: {
|
|
||||||
mime_type: imageType,
|
|
||||||
data: imageData,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
role: v.role.replace("assistant", "model").replace("system", "user"),
|
|
||||||
parts: parts,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// google requires that role in neighboring messages must not be the same
|
|
||||||
for (let i = 0; i < internalMessages.length - 1; ) {
|
|
||||||
// Check if current and next item both have the role "model"
|
|
||||||
if (internalMessages[i].role === internalMessages[i + 1].role) {
|
|
||||||
// Concatenate the 'parts' of the current and next item
|
|
||||||
internalMessages[i].parts = internalMessages[i].parts.concat(
|
|
||||||
internalMessages[i + 1].parts,
|
|
||||||
);
|
|
||||||
// Remove the next item
|
|
||||||
internalMessages.splice(i + 1, 1);
|
|
||||||
} else {
|
|
||||||
// Move to the next item
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestPayload = {
|
|
||||||
contents: internalMessages,
|
|
||||||
generationConfig: {
|
|
||||||
temperature,
|
|
||||||
maxOutputTokens: max_tokens,
|
|
||||||
topP: top_p,
|
|
||||||
},
|
|
||||||
safetySettings: [
|
|
||||||
{
|
|
||||||
category: "HARM_CATEGORY_HARASSMENT",
|
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "HARM_CATEGORY_HATE_SPEECH",
|
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
||||||
threshold: "BLOCK_ONLY_HIGH",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
const baseUrl = `${googleUrl}/${GoogleMetas.ChatPath(
|
|
||||||
model,
|
|
||||||
)}?${urlParamApikeyName}=${googleApiKey}`;
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(requestPayload),
|
|
||||||
method: "POST",
|
|
||||||
url: stream
|
|
||||||
? baseUrl.replace("generateContent", "streamGenerateContent")
|
|
||||||
: baseUrl,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
let existingTexts: string[] = [];
|
|
||||||
|
|
||||||
fetch(requestPayload.url, {
|
|
||||||
...requestPayload,
|
|
||||||
signal: timer.signal,
|
|
||||||
})
|
|
||||||
.then((response) => {
|
|
||||||
const reader = response?.body?.getReader();
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
let partialData = "";
|
|
||||||
|
|
||||||
return reader?.read().then(function processText({
|
|
||||||
done,
|
|
||||||
value,
|
|
||||||
}): Promise<any> {
|
|
||||||
if (done) {
|
|
||||||
if (response.status !== 200) {
|
|
||||||
try {
|
|
||||||
let data = JSON.parse(ensureProperEnding(partialData));
|
|
||||||
if (data && data[0].error) {
|
|
||||||
handlers.onError(new Error(data[0].error.message));
|
|
||||||
} else {
|
|
||||||
handlers.onError(new Error("Request failed"));
|
|
||||||
}
|
|
||||||
} catch (_) {
|
|
||||||
handlers.onError(new Error("Request failed"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Stream complete");
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
partialData += decoder.decode(value, { stream: true });
|
|
||||||
|
|
||||||
try {
|
|
||||||
let data = JSON.parse(ensureProperEnding(partialData));
|
|
||||||
|
|
||||||
const textArray = data.reduce(
|
|
||||||
(acc: string[], item: { candidates: any[] }) => {
|
|
||||||
const texts = item.candidates.map((candidate) =>
|
|
||||||
candidate.content.parts
|
|
||||||
.map((part: { text: any }) => part.text)
|
|
||||||
.join(""),
|
|
||||||
);
|
|
||||||
return acc.concat(texts);
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (textArray.length > existingTexts.length) {
|
|
||||||
const deltaArray = textArray.slice(existingTexts.length);
|
|
||||||
existingTexts = textArray;
|
|
||||||
handlers.onProgress(deltaArray.join(""));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// console.log("[Response Animation] error: ", error,partialData);
|
|
||||||
// skip error message when parsing json
|
|
||||||
}
|
|
||||||
|
|
||||||
return reader.read().then(processText);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error:", error);
|
|
||||||
});
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
): Promise<StandChatReponseMessage> {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
const res = await fetch(requestPayload.url, {
|
|
||||||
headers: {
|
|
||||||
...requestPayload.headers,
|
|
||||||
},
|
|
||||||
body: requestPayload.body,
|
|
||||||
method: requestPayload.method,
|
|
||||||
signal: timer.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
timer.clear();
|
|
||||||
|
|
||||||
const resJson = await res.json();
|
|
||||||
const message = parseResp(resJson);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAvailableModels(
|
|
||||||
providerConfig: Record<SettingKeys, string>,
|
|
||||||
): Promise<ModelInfo[]> {
|
|
||||||
const { googleApiKey, googleUrl } = providerConfig;
|
|
||||||
const res = await fetch(`${googleUrl}/v1beta/models?key=${googleApiKey}`, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${googleApiKey}`,
|
|
||||||
},
|
|
||||||
method: "GET",
|
|
||||||
});
|
|
||||||
const data: ModelList = await res.json();
|
|
||||||
|
|
||||||
return data.models;
|
|
||||||
}
|
|
||||||
|
|
||||||
serverSideRequestHandler: ProviderTemplate["serverSideRequestHandler"] =
|
|
||||||
async (req, serverConfig) => {
|
|
||||||
const { googleUrl = GEMINI_BASE_URL } = serverConfig;
|
|
||||||
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
const path = `${req.nextUrl.pathname}`.replaceAll(
|
|
||||||
this.apiRouteRootName,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", googleUrl);
|
|
||||||
|
|
||||||
const authResult = auth(req, serverConfig);
|
|
||||||
if (authResult.error) {
|
|
||||||
return NextResponse.json(authResult, {
|
|
||||||
status: 401,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetchUrl = `${googleUrl}/${path}?key=${authResult.apiKey}`;
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
},
|
|
||||||
method: req.method,
|
|
||||||
body: req.body,
|
|
||||||
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
|
||||||
redirect: "manual",
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
signal: controller.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
|
||||||
// to prevent browser prompt for credentials
|
|
||||||
const newHeaders = new Headers(res.headers);
|
|
||||||
newHeaders.delete("www-authenticate");
|
|
||||||
// to disable nginx buffering
|
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
|
||||||
|
|
||||||
return new NextResponse(res.body, {
|
|
||||||
status: res.status,
|
|
||||||
statusText: res.statusText,
|
|
||||||
headers: newHeaders,
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
import { getLocaleText } from "../../common";
|
|
||||||
|
|
||||||
export default getLocaleText<
|
|
||||||
{
|
|
||||||
ApiKey: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Placeholder: string;
|
|
||||||
};
|
|
||||||
Endpoint: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: string;
|
|
||||||
IllegalURL: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
ApiVersion: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
"en"
|
|
||||||
>(
|
|
||||||
{
|
|
||||||
cn: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API 密钥",
|
|
||||||
SubTitle: "从 Google AI 获取您的 API 密钥",
|
|
||||||
Placeholder: "输入您的 Google AI Studio API 密钥",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "终端地址",
|
|
||||||
SubTitle: "示例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」结尾",
|
|
||||||
IllegalURL: "请输入一个完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVersion: {
|
|
||||||
Title: "API 版本(仅适用于 gemini-pro)",
|
|
||||||
SubTitle: "选择一个特定的 API 版本",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
en: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API Key",
|
|
||||||
SubTitle: "Obtain your API Key from Google AI",
|
|
||||||
Placeholder: "Enter your Google AI Studio API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Endpoint Address",
|
|
||||||
SubTitle: "Example:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Cannot end with '/'",
|
|
||||||
IllegalURL: "Please enter a complete available url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVersion: {
|
|
||||||
Title: "API Version (specific to gemini-pro)",
|
|
||||||
SubTitle: "Select a specific API version",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
sk: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API kľúč",
|
|
||||||
SubTitle:
|
|
||||||
"Obísť obmedzenia prístupu heslom pomocou vlastného API kľúča Google AI Studio",
|
|
||||||
Placeholder: "API kľúč Google AI Studio",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Adresa koncového bodu",
|
|
||||||
SubTitle: "Príklad:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Nemôže končiť znakom „/“",
|
|
||||||
IllegalURL: "Zadajte úplnú dostupnú adresu URL",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVersion: {
|
|
||||||
Title: "Verzia API (gemini-pro verzia API)",
|
|
||||||
SubTitle: "Vyberte špecifickú verziu časti",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tw: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API 金鑰",
|
|
||||||
SubTitle: "從 Google AI 取得您的 API 金鑰",
|
|
||||||
Placeholder: "輸入您的 Google AI Studio API 金鑰",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "終端地址",
|
|
||||||
SubTitle: "範例:",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」結尾",
|
|
||||||
IllegalURL: "請輸入一個完整可用的url",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
ApiVersion: {
|
|
||||||
Title: "API 版本(僅適用於 gemini-pro)",
|
|
||||||
SubTitle: "選擇一個特定的 API 版本",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"en",
|
|
||||||
);
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import { ServerConfig, getIP } from "../../common";
|
|
||||||
|
|
||||||
export const urlParamApikeyName = "key";
|
|
||||||
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
|
||||||
|
|
||||||
export const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
|
||||||
export const validString = (x?: string): x is string =>
|
|
||||||
Boolean(x && x.length > 0);
|
|
||||||
|
|
||||||
export function ensureProperEnding(str: string) {
|
|
||||||
if (str.startsWith("[") && !str.endsWith("]")) {
|
|
||||||
return str + "]";
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function auth(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
let apiKey = req.nextUrl.searchParams.get(urlParamApikeyName);
|
|
||||||
|
|
||||||
const { hideUserApiKey, googleApiKey } = serverConfig;
|
|
||||||
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (hideUserApiKey && apiKey) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to access with your own api key",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apiKey) {
|
|
||||||
console.log("[Auth] use user api key");
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
apiKey,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (googleApiKey) {
|
|
||||||
console.log("[Auth] use system api key");
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
apiKey: googleApiKey,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Auth] admin did not provide an api key");
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: `missing api key`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTimer() {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
// make a fetch request
|
|
||||||
const requestTimeoutId = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
REQUEST_TIMEOUT_MS,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...controller,
|
|
||||||
clear: () => {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseResp(res: any) {
|
|
||||||
if (res?.promptFeedback?.blockReason) {
|
|
||||||
// being blocked
|
|
||||||
throw new Error(
|
|
||||||
"Message is being blocked for reason: " + res.promptFeedback.blockReason,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
message:
|
|
||||||
res.candidates?.at(0)?.content?.parts?.at(0)?.text ||
|
|
||||||
res.error?.message ||
|
|
||||||
"",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
export {
|
|
||||||
default as NextChatProvider,
|
|
||||||
type NextChatProviderSettingKeys,
|
|
||||||
} from "@/app/client/providers/nextchat";
|
|
||||||
export {
|
|
||||||
default as GoogleProvider,
|
|
||||||
type GoogleProviderSettingKeys,
|
|
||||||
} from "@/app/client/providers/google";
|
|
||||||
export {
|
|
||||||
default as OpenAIProvider,
|
|
||||||
type OpenAIProviderSettingKeys,
|
|
||||||
} from "@/app/client/providers/openai";
|
|
||||||
export {
|
|
||||||
default as AnthropicProvider,
|
|
||||||
type AnthropicProviderSettingKeys,
|
|
||||||
} from "@/app/client/providers/anthropic";
|
|
||||||
export {
|
|
||||||
default as AzureProvider,
|
|
||||||
type AzureProviderSettingKeys,
|
|
||||||
} from "@/app/client/providers/azure";
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
import { SettingItem } from "../../common";
|
|
||||||
import { isVisionModel } from "@/app/utils";
|
|
||||||
import Locale from "@/app/locales";
|
|
||||||
|
|
||||||
export const OPENAI_BASE_URL = "https://api.openai.com";
|
|
||||||
|
|
||||||
export const NextChatMetas = {
|
|
||||||
ChatPath: "v1/chat/completions",
|
|
||||||
UsagePath: "dashboard/billing/usage",
|
|
||||||
SubsPath: "dashboard/billing/subscription",
|
|
||||||
ListModelPath: "v1/models",
|
|
||||||
};
|
|
||||||
|
|
||||||
export const preferredRegion: string | string[] = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
export type SettingKeys = "accessCode";
|
|
||||||
|
|
||||||
export const defaultModal = "gpt-3.5-turbo";
|
|
||||||
|
|
||||||
export const models = [
|
|
||||||
defaultModal,
|
|
||||||
"gpt-3.5-turbo-0301",
|
|
||||||
"gpt-3.5-turbo-0613",
|
|
||||||
"gpt-3.5-turbo-1106",
|
|
||||||
"gpt-3.5-turbo-0125",
|
|
||||||
"gpt-3.5-turbo-16k",
|
|
||||||
"gpt-3.5-turbo-16k-0613",
|
|
||||||
"gpt-4",
|
|
||||||
"gpt-4-0314",
|
|
||||||
"gpt-4-0613",
|
|
||||||
"gpt-4-1106-preview",
|
|
||||||
"gpt-4-0125-preview",
|
|
||||||
"gpt-4-32k",
|
|
||||||
"gpt-4-32k-0314",
|
|
||||||
"gpt-4-32k-0613",
|
|
||||||
"gpt-4-turbo",
|
|
||||||
"gpt-4-turbo-preview",
|
|
||||||
"gpt-4-vision-preview",
|
|
||||||
"gpt-4-turbo-2024-04-09",
|
|
||||||
|
|
||||||
"gemini-1.0-pro",
|
|
||||||
"gemini-1.5-pro-latest",
|
|
||||||
"gemini-pro-vision",
|
|
||||||
|
|
||||||
"claude-instant-1.2",
|
|
||||||
"claude-2.0",
|
|
||||||
"claude-2.1",
|
|
||||||
"claude-3-sonnet-20240229",
|
|
||||||
"claude-3-opus-20240229",
|
|
||||||
"claude-3-haiku-20240307",
|
|
||||||
];
|
|
||||||
|
|
||||||
export const modelConfigs = models.map((name) => ({
|
|
||||||
name,
|
|
||||||
displayName: name,
|
|
||||||
isVision: isVisionModel(name),
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: name === defaultModal,
|
|
||||||
}));
|
|
||||||
|
|
||||||
export const settingItems: SettingItem<SettingKeys>[] = [
|
|
||||||
{
|
|
||||||
name: "accessCode",
|
|
||||||
title: Locale.Auth.Title,
|
|
||||||
description: Locale.Auth.Tips,
|
|
||||||
placeholder: Locale.Auth.Input,
|
|
||||||
type: "input",
|
|
||||||
inputType: "password",
|
|
||||||
validators: ["required"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,348 +0,0 @@
|
|||||||
import {
|
|
||||||
modelConfigs,
|
|
||||||
settingItems,
|
|
||||||
SettingKeys,
|
|
||||||
NextChatMetas,
|
|
||||||
preferredRegion,
|
|
||||||
OPENAI_BASE_URL,
|
|
||||||
} from "./config";
|
|
||||||
import {
|
|
||||||
ChatHandlers,
|
|
||||||
getMessageTextContent,
|
|
||||||
InternalChatRequestPayload,
|
|
||||||
IProviderTemplate,
|
|
||||||
ServerConfig,
|
|
||||||
StandChatReponseMessage,
|
|
||||||
} from "../../common";
|
|
||||||
import {
|
|
||||||
EventStreamContentType,
|
|
||||||
fetchEventSource,
|
|
||||||
} from "@fortaine/fetch-event-source";
|
|
||||||
import { prettyObject } from "@/app/utils/format";
|
|
||||||
import Locale from "@/app/locales";
|
|
||||||
import { auth, authHeaderName, getHeaders, getTimer, parseResp } from "./utils";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export type NextChatProviderSettingKeys = SettingKeys;
|
|
||||||
|
|
||||||
export const ROLES = ["system", "user", "assistant"] as const;
|
|
||||||
export type MessageRole = (typeof ROLES)[number];
|
|
||||||
|
|
||||||
export interface MultimodalContent {
|
|
||||||
type: "text" | "image_url";
|
|
||||||
text?: string;
|
|
||||||
image_url?: {
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RequestMessage {
|
|
||||||
role: MessageRole;
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RequestPayload {
|
|
||||||
messages: {
|
|
||||||
role: "system" | "user" | "assistant";
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}[];
|
|
||||||
stream?: boolean;
|
|
||||||
model: string;
|
|
||||||
temperature: number;
|
|
||||||
presence_penalty: number;
|
|
||||||
frequency_penalty: number;
|
|
||||||
top_p: number;
|
|
||||||
max_tokens?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProviderTemplate = IProviderTemplate<
|
|
||||||
SettingKeys,
|
|
||||||
"azure",
|
|
||||||
typeof NextChatMetas
|
|
||||||
>;
|
|
||||||
|
|
||||||
export default class NextChatProvider
|
|
||||||
implements IProviderTemplate<SettingKeys, "nextchat", typeof NextChatMetas>
|
|
||||||
{
|
|
||||||
apiRouteRootName: "/api/provider/nextchat" = "/api/provider/nextchat";
|
|
||||||
allowedApiMethods: (
|
|
||||||
| "POST"
|
|
||||||
| "GET"
|
|
||||||
| "OPTIONS"
|
|
||||||
| "PUT"
|
|
||||||
| "PATCH"
|
|
||||||
| "DELETE"
|
|
||||||
)[] = ["GET", "POST"];
|
|
||||||
|
|
||||||
runtime = "edge" as const;
|
|
||||||
preferredRegion = preferredRegion;
|
|
||||||
name = "nextchat" as const;
|
|
||||||
metas = NextChatMetas;
|
|
||||||
|
|
||||||
defaultModels = modelConfigs;
|
|
||||||
|
|
||||||
providerMeta = {
|
|
||||||
displayName: "NextChat",
|
|
||||||
settingItems,
|
|
||||||
};
|
|
||||||
|
|
||||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
|
||||||
const { messages, isVisionModel, model, stream, modelConfig } = payload;
|
|
||||||
const {
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
max_tokens,
|
|
||||||
} = modelConfig;
|
|
||||||
|
|
||||||
const openAiMessages = messages.map((v) => ({
|
|
||||||
role: v.role,
|
|
||||||
content: isVisionModel ? v.content : getMessageTextContent(v),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const requestPayload: RequestPayload = {
|
|
||||||
messages: openAiMessages,
|
|
||||||
stream,
|
|
||||||
model,
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
};
|
|
||||||
|
|
||||||
// add max_tokens to vision model
|
|
||||||
if (isVisionModel) {
|
|
||||||
requestPayload["max_tokens"] = Math.max(max_tokens, 4000);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Request] openai payload: ", requestPayload);
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: getHeaders(payload.providerConfig.accessCode!),
|
|
||||||
body: JSON.stringify(requestPayload),
|
|
||||||
method: "POST",
|
|
||||||
url: [this.apiRouteRootName, NextChatMetas.ChatPath].join("/"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async requestOpenai(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const { baseUrl = OPENAI_BASE_URL, openaiOrgId } = serverConfig;
|
|
||||||
const controller = new AbortController();
|
|
||||||
const authValue = req.headers.get(authHeaderName) ?? "";
|
|
||||||
|
|
||||||
const path = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
|
|
||||||
this.apiRouteRootName,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", baseUrl);
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}/${path}`;
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
[authHeaderName]: authValue,
|
|
||||||
...(openaiOrgId && {
|
|
||||||
"OpenAI-Organization": openaiOrgId,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
method: req.method,
|
|
||||||
body: req.body,
|
|
||||||
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
|
||||||
redirect: "manual",
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
signal: controller.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
|
||||||
|
|
||||||
// Extract the OpenAI-Organization header from the response
|
|
||||||
const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
|
|
||||||
|
|
||||||
// Check if serverConfig.openaiOrgId is defined and not an empty string
|
|
||||||
if (openaiOrgId && openaiOrgId.trim() !== "") {
|
|
||||||
// If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
|
|
||||||
console.log("[Org ID]", openaiOrganizationHeader);
|
|
||||||
} else {
|
|
||||||
console.log("[Org ID] is not set up.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// to prevent browser prompt for credentials
|
|
||||||
const newHeaders = new Headers(res.headers);
|
|
||||||
newHeaders.delete("www-authenticate");
|
|
||||||
// to disable nginx buffering
|
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
|
||||||
|
|
||||||
// Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV)
|
|
||||||
// Also, this is to prevent the header from being sent to the client
|
|
||||||
if (!openaiOrgId || openaiOrgId.trim() === "") {
|
|
||||||
newHeaders.delete("OpenAI-Organization");
|
|
||||||
}
|
|
||||||
|
|
||||||
// The latest version of the OpenAI API forced the content-encoding to be "br" in json response
|
|
||||||
// So if the streaming is disabled, we need to remove the content-encoding header
|
|
||||||
// Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header
|
|
||||||
// The browser will try to decode the response with brotli and fail
|
|
||||||
newHeaders.delete("content-encoding");
|
|
||||||
|
|
||||||
return new NextResponse(res.body, {
|
|
||||||
status: res.status,
|
|
||||||
statusText: res.statusText,
|
|
||||||
headers: newHeaders,
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
fetchEventSource(requestPayload.url, {
|
|
||||||
...requestPayload,
|
|
||||||
fetch,
|
|
||||||
async onopen(res) {
|
|
||||||
timer.clear();
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log("[OpenAI] request response content type: ", contentType);
|
|
||||||
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
const responseText = await res.clone().text();
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!res.ok ||
|
|
||||||
!res.headers
|
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
|
||||||
const responseTexts = [];
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseText = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
if (msg.data === "[DONE]") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const text = msg.data;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(text);
|
|
||||||
const choices = json.choices as Array<{
|
|
||||||
delta: { content: string };
|
|
||||||
}>;
|
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
|
|
||||||
if (delta) {
|
|
||||||
handlers.onProgress(delta);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose() {
|
|
||||||
handlers.onFinish();
|
|
||||||
},
|
|
||||||
onerror(e) {
|
|
||||||
handlers.onError(e);
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: InternalChatRequestPayload<"accessCode">,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
): Promise<StandChatReponseMessage> {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
const res = await fetch(requestPayload.url, {
|
|
||||||
headers: {
|
|
||||||
...requestPayload.headers,
|
|
||||||
},
|
|
||||||
body: requestPayload.body,
|
|
||||||
method: requestPayload.method,
|
|
||||||
signal: timer.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
timer.clear();
|
|
||||||
|
|
||||||
const resJson = await res.json();
|
|
||||||
const message = parseResp(resJson);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
serverSideRequestHandler: ProviderTemplate["serverSideRequestHandler"] =
|
|
||||||
async (req, config) => {
|
|
||||||
const { subpath } = req;
|
|
||||||
const ALLOWD_PATH = new Set(Object.values(NextChatMetas));
|
|
||||||
|
|
||||||
if (!ALLOWD_PATH.has(subpath)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to request " + subpath,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 403,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const authResult = auth(req, config);
|
|
||||||
if (authResult.error) {
|
|
||||||
return NextResponse.json(authResult, {
|
|
||||||
status: 401,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await this.requestOpenai(req, config);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (e) {
|
|
||||||
return NextResponse.json(prettyObject(e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import { ServerConfig, getIP } from "../../common";
|
|
||||||
import md5 from "spark-md5";
|
|
||||||
|
|
||||||
export const ACCESS_CODE_PREFIX = "nk-";
|
|
||||||
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
|
||||||
|
|
||||||
export const authHeaderName = "Authorization";
|
|
||||||
|
|
||||||
export const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
|
||||||
|
|
||||||
export const validString = (x?: string): x is string =>
|
|
||||||
Boolean(x && x.length > 0);
|
|
||||||
|
|
||||||
export function prettyObject(msg: any) {
|
|
||||||
const obj = msg;
|
|
||||||
if (typeof msg !== "string") {
|
|
||||||
msg = JSON.stringify(msg, null, " ");
|
|
||||||
}
|
|
||||||
if (msg === "{}") {
|
|
||||||
return obj.toString();
|
|
||||||
}
|
|
||||||
if (msg.startsWith("```json")) {
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
return ["```json", msg, "```"].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTimer() {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
// make a fetch request
|
|
||||||
const requestTimeoutId = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
REQUEST_TIMEOUT_MS,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...controller,
|
|
||||||
clear: () => {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getHeaders(accessCode: string) {
|
|
||||||
const headers: Record<string, string> = {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
[authHeaderName]: makeBearer(ACCESS_CODE_PREFIX + accessCode),
|
|
||||||
};
|
|
||||||
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseResp(res: { choices: { message: { content: any } }[] }) {
|
|
||||||
return {
|
|
||||||
message: res.choices?.[0]?.message?.content ?? "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseApiKey(req: NextRequest) {
|
|
||||||
const authToken = req.headers.get("Authorization") ?? "";
|
|
||||||
|
|
||||||
return {
|
|
||||||
accessCode:
|
|
||||||
authToken.startsWith(ACCESS_CODE_PREFIX) &&
|
|
||||||
authToken.slice(ACCESS_CODE_PREFIX.length),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function auth(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
// check if it is openai api key or user token
|
|
||||||
const { accessCode } = parseApiKey(req);
|
|
||||||
const { googleApiKey, apiKey, anthropicApiKey, azureApiKey, codes } =
|
|
||||||
serverConfig;
|
|
||||||
|
|
||||||
const hashedCode = md5.hash(accessCode || "").trim();
|
|
||||||
|
|
||||||
console.log("[Auth] allowed hashed codes: ", [...codes]);
|
|
||||||
console.log("[Auth] got access code:", accessCode);
|
|
||||||
console.log("[Auth] hashed access code:", hashedCode);
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (!codes.has(hashedCode)) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: !accessCode ? "empty access code" : "wrong access code",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const systemApiKey = googleApiKey || apiKey || anthropicApiKey || azureApiKey;
|
|
||||||
|
|
||||||
if (systemApiKey) {
|
|
||||||
console.log("[Auth] use system api key");
|
|
||||||
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
accessCode,
|
|
||||||
systemApiKey,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Auth] admin did not provide an api key");
|
|
||||||
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: `Server internal error`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,214 +0,0 @@
|
|||||||
import { SettingItem } from "../../common";
|
|
||||||
import Locale from "./locale";
|
|
||||||
|
|
||||||
export const OPENAI_BASE_URL = "https://api.openai.com";
|
|
||||||
|
|
||||||
export const ROLES = ["system", "user", "assistant"] as const;
|
|
||||||
|
|
||||||
export const preferredRegion: string | string[] = [
|
|
||||||
"arn1",
|
|
||||||
"bom1",
|
|
||||||
"cdg1",
|
|
||||||
"cle1",
|
|
||||||
"cpt1",
|
|
||||||
"dub1",
|
|
||||||
"fra1",
|
|
||||||
"gru1",
|
|
||||||
"hnd1",
|
|
||||||
"iad1",
|
|
||||||
"icn1",
|
|
||||||
"kix1",
|
|
||||||
"lhr1",
|
|
||||||
"pdx1",
|
|
||||||
"sfo1",
|
|
||||||
"sin1",
|
|
||||||
"syd1",
|
|
||||||
];
|
|
||||||
|
|
||||||
export const OpenaiMetas = {
|
|
||||||
ChatPath: "v1/chat/completions",
|
|
||||||
UsagePath: "dashboard/billing/usage",
|
|
||||||
SubsPath: "dashboard/billing/subscription",
|
|
||||||
ListModelPath: "v1/models",
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SettingKeys = "openaiUrl" | "openaiApiKey";
|
|
||||||
|
|
||||||
export const modelConfigs = [
|
|
||||||
{
|
|
||||||
name: "gpt-4o",
|
|
||||||
displayName: "gpt-4o",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo",
|
|
||||||
displayName: "gpt-3.5-turbo",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-0301",
|
|
||||||
displayName: "gpt-3.5-turbo-0301",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-0613",
|
|
||||||
displayName: "gpt-3.5-turbo-0613",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-1106",
|
|
||||||
displayName: "gpt-3.5-turbo-1106",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-0125",
|
|
||||||
displayName: "gpt-3.5-turbo-0125",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-16k",
|
|
||||||
displayName: "gpt-3.5-turbo-16k",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-3.5-turbo-16k-0613",
|
|
||||||
displayName: "gpt-3.5-turbo-16k-0613",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4",
|
|
||||||
displayName: "gpt-4",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-0314",
|
|
||||||
displayName: "gpt-4-0314",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-0613",
|
|
||||||
displayName: "gpt-4-0613",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-1106-preview",
|
|
||||||
displayName: "gpt-4-1106-preview",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-0125-preview",
|
|
||||||
displayName: "gpt-4-0125-preview",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-32k",
|
|
||||||
displayName: "gpt-4-32k",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-32k-0314",
|
|
||||||
displayName: "gpt-4-32k-0314",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-32k-0613",
|
|
||||||
displayName: "gpt-4-32k-0613",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-turbo",
|
|
||||||
displayName: "gpt-4-turbo",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: true,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-turbo-preview",
|
|
||||||
displayName: "gpt-4-turbo-preview",
|
|
||||||
isVision: false,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-vision-preview",
|
|
||||||
displayName: "gpt-4-vision-preview",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "gpt-4-turbo-2024-04-09",
|
|
||||||
displayName: "gpt-4-turbo-2024-04-09",
|
|
||||||
isVision: true,
|
|
||||||
isDefaultActive: false,
|
|
||||||
isDefaultSelected: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const settingItems: (
|
|
||||||
defaultEndpoint: string,
|
|
||||||
) => SettingItem<SettingKeys>[] = (defaultEndpoint) => [
|
|
||||||
{
|
|
||||||
name: "openaiUrl",
|
|
||||||
title: Locale.Endpoint.Title,
|
|
||||||
description: Locale.Endpoint.SubTitle,
|
|
||||||
defaultValue: defaultEndpoint,
|
|
||||||
type: "input",
|
|
||||||
validators: [
|
|
||||||
"required",
|
|
||||||
async (v: any) => {
|
|
||||||
if (typeof v === "string" && v.endsWith("/")) {
|
|
||||||
return Locale.Endpoint.Error.EndWithBackslash;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
typeof v === "string" &&
|
|
||||||
!v.startsWith(defaultEndpoint) &&
|
|
||||||
!v.startsWith("http")
|
|
||||||
) {
|
|
||||||
return Locale.Endpoint.SubTitle;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "openaiApiKey",
|
|
||||||
title: Locale.ApiKey.Title,
|
|
||||||
description: Locale.ApiKey.SubTitle,
|
|
||||||
placeholder: Locale.ApiKey.Placeholder,
|
|
||||||
type: "input",
|
|
||||||
inputType: "password",
|
|
||||||
// validators: ["required"],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,381 +0,0 @@
|
|||||||
import {
|
|
||||||
ChatHandlers,
|
|
||||||
InternalChatRequestPayload,
|
|
||||||
IProviderTemplate,
|
|
||||||
ModelInfo,
|
|
||||||
getMessageTextContent,
|
|
||||||
ServerConfig,
|
|
||||||
} from "../../common";
|
|
||||||
import {
|
|
||||||
EventStreamContentType,
|
|
||||||
fetchEventSource,
|
|
||||||
} from "@fortaine/fetch-event-source";
|
|
||||||
import Locale from "@/app/locales";
|
|
||||||
import {
|
|
||||||
authHeaderName,
|
|
||||||
prettyObject,
|
|
||||||
parseResp,
|
|
||||||
auth,
|
|
||||||
getTimer,
|
|
||||||
getHeaders,
|
|
||||||
} from "./utils";
|
|
||||||
import {
|
|
||||||
modelConfigs,
|
|
||||||
settingItems,
|
|
||||||
SettingKeys,
|
|
||||||
OpenaiMetas,
|
|
||||||
ROLES,
|
|
||||||
OPENAI_BASE_URL,
|
|
||||||
preferredRegion,
|
|
||||||
} from "./config";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { ModelList } from "./type";
|
|
||||||
|
|
||||||
export type OpenAIProviderSettingKeys = SettingKeys;
|
|
||||||
|
|
||||||
export type MessageRole = (typeof ROLES)[number];
|
|
||||||
|
|
||||||
export interface MultimodalContent {
|
|
||||||
type: "text" | "image_url";
|
|
||||||
text?: string;
|
|
||||||
image_url?: {
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RequestMessage {
|
|
||||||
role: MessageRole;
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}
|
|
||||||
interface RequestPayload {
|
|
||||||
messages: {
|
|
||||||
role: "system" | "user" | "assistant";
|
|
||||||
content: string | MultimodalContent[];
|
|
||||||
}[];
|
|
||||||
stream?: boolean;
|
|
||||||
model: string;
|
|
||||||
temperature: number;
|
|
||||||
presence_penalty: number;
|
|
||||||
frequency_penalty: number;
|
|
||||||
top_p: number;
|
|
||||||
max_tokens?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProviderTemplate = IProviderTemplate<
|
|
||||||
SettingKeys,
|
|
||||||
"azure",
|
|
||||||
typeof OpenaiMetas
|
|
||||||
>;
|
|
||||||
|
|
||||||
class OpenAIProvider
|
|
||||||
implements IProviderTemplate<SettingKeys, "openai", typeof OpenaiMetas>
|
|
||||||
{
|
|
||||||
apiRouteRootName: "/api/provider/openai" = "/api/provider/openai";
|
|
||||||
allowedApiMethods: (
|
|
||||||
| "POST"
|
|
||||||
| "GET"
|
|
||||||
| "OPTIONS"
|
|
||||||
| "PUT"
|
|
||||||
| "PATCH"
|
|
||||||
| "DELETE"
|
|
||||||
)[] = ["GET", "POST"];
|
|
||||||
runtime = "edge" as const;
|
|
||||||
preferredRegion = preferredRegion;
|
|
||||||
|
|
||||||
name = "openai" as const;
|
|
||||||
metas = OpenaiMetas;
|
|
||||||
|
|
||||||
defaultModels = modelConfigs;
|
|
||||||
|
|
||||||
providerMeta = {
|
|
||||||
displayName: "OpenAI",
|
|
||||||
settingItems: settingItems(
|
|
||||||
`${this.apiRouteRootName}/${OpenaiMetas.ChatPath}`,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
|
||||||
const {
|
|
||||||
messages,
|
|
||||||
isVisionModel,
|
|
||||||
model,
|
|
||||||
stream,
|
|
||||||
modelConfig: {
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
max_tokens,
|
|
||||||
},
|
|
||||||
providerConfig: { openaiUrl },
|
|
||||||
} = payload;
|
|
||||||
|
|
||||||
const openAiMessages = messages.map((v) => ({
|
|
||||||
role: v.role,
|
|
||||||
content: isVisionModel ? v.content : getMessageTextContent(v),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const requestPayload: RequestPayload = {
|
|
||||||
messages: openAiMessages,
|
|
||||||
stream,
|
|
||||||
model,
|
|
||||||
temperature,
|
|
||||||
presence_penalty,
|
|
||||||
frequency_penalty,
|
|
||||||
top_p,
|
|
||||||
};
|
|
||||||
|
|
||||||
// add max_tokens to vision model
|
|
||||||
if (isVisionModel) {
|
|
||||||
requestPayload["max_tokens"] = Math.max(max_tokens, 4000);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Request] openai payload: ", requestPayload);
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: getHeaders(payload.providerConfig.openaiApiKey),
|
|
||||||
body: JSON.stringify(requestPayload),
|
|
||||||
method: "POST",
|
|
||||||
url: openaiUrl!,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private async requestOpenai(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const { baseUrl = OPENAI_BASE_URL, openaiOrgId } = serverConfig;
|
|
||||||
const controller = new AbortController();
|
|
||||||
const authValue = req.headers.get(authHeaderName) ?? "";
|
|
||||||
|
|
||||||
const path = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
|
|
||||||
this.apiRouteRootName,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log("[Proxy] ", path);
|
|
||||||
console.log("[Base Url]", baseUrl);
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(
|
|
||||||
() => {
|
|
||||||
controller.abort();
|
|
||||||
},
|
|
||||||
10 * 60 * 1000,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchUrl = `${baseUrl}/${path}`;
|
|
||||||
const fetchOptions: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
[authHeaderName]: authValue,
|
|
||||||
...(openaiOrgId && {
|
|
||||||
"OpenAI-Organization": openaiOrgId,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
method: req.method,
|
|
||||||
body: req.body,
|
|
||||||
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
|
||||||
redirect: "manual",
|
|
||||||
// @ts-ignore
|
|
||||||
duplex: "half",
|
|
||||||
signal: controller.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(fetchUrl, fetchOptions);
|
|
||||||
|
|
||||||
// Extract the OpenAI-Organization header from the response
|
|
||||||
const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
|
|
||||||
|
|
||||||
// Check if serverConfig.openaiOrgId is defined and not an empty string
|
|
||||||
if (openaiOrgId && openaiOrgId.trim() !== "") {
|
|
||||||
// If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
|
|
||||||
console.log("[Org ID]", openaiOrganizationHeader);
|
|
||||||
} else {
|
|
||||||
console.log("[Org ID] is not set up.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// to prevent browser prompt for credentials
|
|
||||||
const newHeaders = new Headers(res.headers);
|
|
||||||
newHeaders.delete("www-authenticate");
|
|
||||||
// to disable nginx buffering
|
|
||||||
newHeaders.set("X-Accel-Buffering", "no");
|
|
||||||
|
|
||||||
// Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV)
|
|
||||||
// Also, this is to prevent the header from being sent to the client
|
|
||||||
if (!openaiOrgId || openaiOrgId.trim() === "") {
|
|
||||||
newHeaders.delete("OpenAI-Organization");
|
|
||||||
}
|
|
||||||
|
|
||||||
// The latest version of the OpenAI API forced the content-encoding to be "br" in json response
|
|
||||||
// So if the streaming is disabled, we need to remove the content-encoding header
|
|
||||||
// Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header
|
|
||||||
// The browser will try to decode the response with brotli and fail
|
|
||||||
newHeaders.delete("content-encoding");
|
|
||||||
|
|
||||||
return new NextResponse(res.body, {
|
|
||||||
status: res.status,
|
|
||||||
statusText: res.statusText,
|
|
||||||
headers: newHeaders,
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async chat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
const res = await fetch(requestPayload.url, {
|
|
||||||
headers: {
|
|
||||||
...requestPayload.headers,
|
|
||||||
},
|
|
||||||
body: requestPayload.body,
|
|
||||||
method: requestPayload.method,
|
|
||||||
signal: timer.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
timer.clear();
|
|
||||||
|
|
||||||
const resJson = await res.json();
|
|
||||||
const message = parseResp(resJson);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
streamChat(
|
|
||||||
payload: InternalChatRequestPayload<SettingKeys>,
|
|
||||||
handlers: ChatHandlers,
|
|
||||||
fetch: typeof window.fetch,
|
|
||||||
) {
|
|
||||||
const requestPayload = this.formatChatPayload(payload);
|
|
||||||
|
|
||||||
const timer = getTimer();
|
|
||||||
|
|
||||||
fetchEventSource(requestPayload.url, {
|
|
||||||
...requestPayload,
|
|
||||||
fetch,
|
|
||||||
async onopen(res) {
|
|
||||||
timer.clear();
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log("[OpenAI] request response content type: ", contentType);
|
|
||||||
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
const responseText = await res.clone().text();
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!res.ok ||
|
|
||||||
!res.headers
|
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
|
||||||
const responseTexts = [];
|
|
||||||
if (res.status === 401) {
|
|
||||||
responseTexts.push(Locale.Error.Unauthorized);
|
|
||||||
}
|
|
||||||
|
|
||||||
let extraInfo = await res.clone().text();
|
|
||||||
try {
|
|
||||||
const resJson = await res.clone().json();
|
|
||||||
extraInfo = prettyObject(resJson);
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (extraInfo) {
|
|
||||||
responseTexts.push(extraInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseText = responseTexts.join("\n\n");
|
|
||||||
|
|
||||||
return handlers.onFlash(responseText);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onmessage(msg) {
|
|
||||||
if (msg.data === "[DONE]") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const text = msg.data;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(text);
|
|
||||||
const choices = json.choices as Array<{
|
|
||||||
delta: { content: string };
|
|
||||||
}>;
|
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
|
|
||||||
if (delta) {
|
|
||||||
handlers.onProgress(delta);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose() {
|
|
||||||
handlers.onFinish();
|
|
||||||
},
|
|
||||||
onerror(e) {
|
|
||||||
handlers.onError(e);
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAvailableModels(
|
|
||||||
providerConfig: Record<SettingKeys, string>,
|
|
||||||
): Promise<ModelInfo[]> {
|
|
||||||
const { openaiApiKey, openaiUrl } = providerConfig;
|
|
||||||
const res = await fetch(`${openaiUrl}/v1/models`, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${openaiApiKey}`,
|
|
||||||
},
|
|
||||||
method: "GET",
|
|
||||||
});
|
|
||||||
const data: ModelList = await res.json();
|
|
||||||
|
|
||||||
return data.data.map((o) => ({
|
|
||||||
name: o.id,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
serverSideRequestHandler: ProviderTemplate["serverSideRequestHandler"] =
|
|
||||||
async (req, config) => {
|
|
||||||
const { subpath } = req;
|
|
||||||
const ALLOWD_PATH = new Set(Object.values(OpenaiMetas));
|
|
||||||
|
|
||||||
if (!ALLOWD_PATH.has(subpath)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to request " + subpath,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 403,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const authResult = auth(req, config);
|
|
||||||
if (authResult.error) {
|
|
||||||
return NextResponse.json(authResult, {
|
|
||||||
status: 401,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await this.requestOpenai(req, config);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (e) {
|
|
||||||
return NextResponse.json(prettyObject(e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default OpenAIProvider;
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
import { getLocaleText } from "../../common/locale";
|
|
||||||
|
|
||||||
export default getLocaleText<
|
|
||||||
{
|
|
||||||
ApiKey: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Placeholder: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: string;
|
|
||||||
SubTitle: string;
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
},
|
|
||||||
"en"
|
|
||||||
>(
|
|
||||||
{
|
|
||||||
cn: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API Key",
|
|
||||||
SubTitle: "使用自定义 OpenAI Key 绕过密码访问限制",
|
|
||||||
Placeholder: "OpenAI API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "接口地址",
|
|
||||||
SubTitle: "除默认地址外,必须包含 http(s)://",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」结尾",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
en: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "OpenAI API Key",
|
|
||||||
SubTitle: "User custom OpenAI Api Key",
|
|
||||||
Placeholder: "sk-xxx",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "OpenAI Endpoint",
|
|
||||||
SubTitle: "Must starts with http(s):// or use /api/openai as default",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Cannot end with '/'",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
pt: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "Chave API OpenAI",
|
|
||||||
SubTitle: "Usar Chave API OpenAI personalizada",
|
|
||||||
Placeholder: "sk-xxx",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Endpoint OpenAI",
|
|
||||||
SubTitle: "Deve começar com http(s):// ou usar /api/openai como padrão",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Não é possível terminar com '/'",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
sk: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API kľúč OpenAI",
|
|
||||||
SubTitle: "Použiť vlastný API kľúč OpenAI",
|
|
||||||
Placeholder: "sk-xxx",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "Koncový bod OpenAI",
|
|
||||||
SubTitle:
|
|
||||||
"Musí začínať http(s):// alebo použiť /api/openai ako predvolený",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "Nemôže končiť znakom „/“",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tw: {
|
|
||||||
ApiKey: {
|
|
||||||
Title: "API Key",
|
|
||||||
SubTitle: "使用自定義 OpenAI Key 繞過密碼存取限制",
|
|
||||||
Placeholder: "OpenAI API Key",
|
|
||||||
},
|
|
||||||
|
|
||||||
Endpoint: {
|
|
||||||
Title: "介面(Endpoint) 地址",
|
|
||||||
SubTitle: "除預設地址外,必須包含 http(s)://",
|
|
||||||
Error: {
|
|
||||||
EndWithBackslash: "不能以「/」結尾",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"en",
|
|
||||||
);
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
export interface ModelList {
|
|
||||||
object: "list";
|
|
||||||
data: Array<{
|
|
||||||
id: string;
|
|
||||||
object: "model";
|
|
||||||
created: number;
|
|
||||||
owned_by: "system" | "openai-internal";
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OpenAIListModelResponse {
|
|
||||||
object: string;
|
|
||||||
data: Array<{
|
|
||||||
id: string;
|
|
||||||
object: string;
|
|
||||||
root: string;
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
import { NextRequest } from "next/server";
|
|
||||||
import { ServerConfig, getIP } from "../../common";
|
|
||||||
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
|
||||||
|
|
||||||
export const authHeaderName = "Authorization";
|
|
||||||
|
|
||||||
const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
|
||||||
|
|
||||||
const validString = (x?: string): x is string => Boolean(x && x.length > 0);
|
|
||||||
|
|
||||||
function parseApiKey(bearToken: string) {
|
|
||||||
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
|
||||||
|
|
||||||
return {
|
|
||||||
apiKey: token,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prettyObject(msg: any) {
|
|
||||||
const obj = msg;
|
|
||||||
if (typeof msg !== "string") {
|
|
||||||
msg = JSON.stringify(msg, null, " ");
|
|
||||||
}
|
|
||||||
if (msg === "{}") {
|
|
||||||
return obj.toString();
|
|
||||||
}
|
|
||||||
if (msg.startsWith("```json")) {
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
return ["```json", msg, "```"].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseResp(res: { choices: { message: { content: any } }[] }) {
|
|
||||||
return {
|
|
||||||
message: res.choices?.[0]?.message?.content ?? "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function auth(req: NextRequest, serverConfig: ServerConfig) {
|
|
||||||
const { hideUserApiKey, apiKey: systemApiKey } = serverConfig;
|
|
||||||
const authToken = req.headers.get(authHeaderName) ?? "";
|
|
||||||
|
|
||||||
const { apiKey } = parseApiKey(authToken);
|
|
||||||
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (hideUserApiKey && apiKey) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
message: "you are not allowed to access with your own api key",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apiKey) {
|
|
||||||
console.log("[Auth] use user api key");
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (systemApiKey) {
|
|
||||||
console.log("[Auth] use system api key");
|
|
||||||
req.headers.set(authHeaderName, `Bearer ${systemApiKey}`);
|
|
||||||
} else {
|
|
||||||
console.log("[Auth] admin did not provide an api key");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
error: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTimer() {
|
|
||||||
const controller = new AbortController();
|
|
||||||
|
|
||||||
// make a fetch request
|
|
||||||
const requestTimeoutId = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
REQUEST_TIMEOUT_MS,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...controller,
|
|
||||||
clear: () => {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getHeaders(openaiApiKey?: string) {
|
|
||||||
const headers: Record<string, string> = {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (validString(openaiApiKey)) {
|
|
||||||
headers[authHeaderName] = makeBearer(openaiApiKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
@@ -41,13 +41,16 @@ interface ChatCommands {
|
|||||||
del?: Command;
|
del?: Command;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ChatCommandPrefix = ":";
|
// Compatible with Chinese colon character ":"
|
||||||
|
export const ChatCommandPrefix = /^[::]/;
|
||||||
|
|
||||||
export function useChatCommand(commands: ChatCommands = {}) {
|
export function useChatCommand(commands: ChatCommands = {}) {
|
||||||
function extract(userInput: string) {
|
function extract(userInput: string) {
|
||||||
return (
|
const match = userInput.match(ChatCommandPrefix);
|
||||||
userInput.startsWith(ChatCommandPrefix) ? userInput.slice(1) : userInput
|
if (match) {
|
||||||
) as keyof ChatCommands;
|
return userInput.slice(1) as keyof ChatCommands;
|
||||||
|
}
|
||||||
|
return userInput as keyof ChatCommands;
|
||||||
}
|
}
|
||||||
|
|
||||||
function search(userInput: string) {
|
function search(userInput: string) {
|
||||||
@@ -57,7 +60,7 @@ export function useChatCommand(commands: ChatCommands = {}) {
|
|||||||
.filter((c) => c.startsWith(input))
|
.filter((c) => c.startsWith(input))
|
||||||
.map((c) => ({
|
.map((c) => ({
|
||||||
title: desc[c as keyof ChatCommands],
|
title: desc[c as keyof ChatCommands],
|
||||||
content: ChatCommandPrefix + c,
|
content: ":" + c,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,123 +0,0 @@
|
|||||||
import { isValidElement } from "react";
|
|
||||||
|
|
||||||
type IconMap = {
|
|
||||||
active?: JSX.Element;
|
|
||||||
inactive?: JSX.Element;
|
|
||||||
mobileActive?: JSX.Element;
|
|
||||||
mobileInactive?: JSX.Element;
|
|
||||||
};
|
|
||||||
interface Action {
|
|
||||||
id: string;
|
|
||||||
title?: string;
|
|
||||||
icons: JSX.Element | IconMap;
|
|
||||||
className?: string;
|
|
||||||
onClick?: () => void;
|
|
||||||
activeClassName?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
type Groups = {
|
|
||||||
normal: string[][];
|
|
||||||
mobile: string[][];
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface ActionsBarProps {
|
|
||||||
actionsSchema: Action[];
|
|
||||||
onSelect?: (id: string) => void;
|
|
||||||
selected?: string;
|
|
||||||
groups: string[][] | Groups;
|
|
||||||
className?: string;
|
|
||||||
inMobile?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ActionsBar(props: ActionsBarProps) {
|
|
||||||
const { actionsSchema, onSelect, selected, groups, className, inMobile } =
|
|
||||||
props;
|
|
||||||
|
|
||||||
const handlerClick =
|
|
||||||
(action: Action) => (e: { preventDefault: () => void }) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (action.onClick) {
|
|
||||||
action.onClick();
|
|
||||||
}
|
|
||||||
if (selected !== action.id) {
|
|
||||||
onSelect?.(action.id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const internalGroup = Array.isArray(groups)
|
|
||||||
? groups
|
|
||||||
: inMobile
|
|
||||||
? groups.mobile
|
|
||||||
: groups.normal;
|
|
||||||
|
|
||||||
const content = internalGroup.reduce((res, group, ind, arr) => {
|
|
||||||
res.push(
|
|
||||||
...group.map((i) => {
|
|
||||||
const action = actionsSchema.find((a) => a.id === i);
|
|
||||||
if (!action) {
|
|
||||||
return <></>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { icons } = action;
|
|
||||||
let activeIcon, inactiveIcon, mobileActiveIcon, mobileInactiveIcon;
|
|
||||||
|
|
||||||
if (isValidElement(icons)) {
|
|
||||||
activeIcon = icons;
|
|
||||||
inactiveIcon = icons;
|
|
||||||
mobileActiveIcon = icons;
|
|
||||||
mobileInactiveIcon = icons;
|
|
||||||
} else {
|
|
||||||
activeIcon = (icons as IconMap).active;
|
|
||||||
inactiveIcon = (icons as IconMap).inactive;
|
|
||||||
mobileActiveIcon = (icons as IconMap).mobileActive;
|
|
||||||
mobileInactiveIcon = (icons as IconMap).mobileInactive;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inMobile) {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={action.id}
|
|
||||||
className={` cursor-pointer shrink-1 grow-0 basis-[${
|
|
||||||
(100 - 1) / arr.length
|
|
||||||
}%] flex flex-col items-center justify-around gap-0.5 py-1.5
|
|
||||||
${
|
|
||||||
selected === action.id
|
|
||||||
? "text-text-sidebar-tab-mobile-active"
|
|
||||||
: "text-text-sidebar-tab-mobile-inactive"
|
|
||||||
}
|
|
||||||
`}
|
|
||||||
onClick={handlerClick(action)}
|
|
||||||
>
|
|
||||||
{selected === action.id ? mobileActiveIcon : mobileInactiveIcon}
|
|
||||||
<div className=" leading-3 text-sm-mobile-tab h-3 font-common w-[100%]">
|
|
||||||
{action.title || " "}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={action.id}
|
|
||||||
className={`cursor-pointer p-3 ${
|
|
||||||
selected === action.id
|
|
||||||
? `!bg-actions-bar-btn-default ${action.activeClassName}`
|
|
||||||
: "bg-transparent"
|
|
||||||
} rounded-md items-center ${
|
|
||||||
action.className
|
|
||||||
} transition duration-300 ease-in-out`}
|
|
||||||
onClick={handlerClick(action)}
|
|
||||||
>
|
|
||||||
{selected === action.id ? activeIcon : inactiveIcon}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
if (ind < arr.length - 1) {
|
|
||||||
res.push(<div key={String(ind)} className=" flex-1"></div>);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}, [] as JSX.Element[]);
|
|
||||||
|
|
||||||
return <div className={`flex items-center ${className} `}>{content}</div>;
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
import * as React from "react";
|
|
||||||
|
|
||||||
export type ButtonType = "primary" | "danger" | null;
|
|
||||||
|
|
||||||
export interface BtnProps {
|
|
||||||
onClick?: () => void;
|
|
||||||
icon?: JSX.Element;
|
|
||||||
prefixIcon?: JSX.Element;
|
|
||||||
type?: ButtonType;
|
|
||||||
text?: React.ReactNode;
|
|
||||||
bordered?: boolean;
|
|
||||||
shadow?: boolean;
|
|
||||||
className?: string;
|
|
||||||
title?: string;
|
|
||||||
disabled?: boolean;
|
|
||||||
tabIndex?: number;
|
|
||||||
autoFocus?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Btn(props: BtnProps) {
|
|
||||||
const {
|
|
||||||
onClick,
|
|
||||||
icon,
|
|
||||||
type,
|
|
||||||
text,
|
|
||||||
className,
|
|
||||||
title,
|
|
||||||
disabled,
|
|
||||||
tabIndex,
|
|
||||||
autoFocus,
|
|
||||||
prefixIcon,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
let btnClassName;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "primary":
|
|
||||||
btnClassName = `${
|
|
||||||
disabled
|
|
||||||
? "bg-primary-btn-disabled dark:opacity-30 dark:text-primary-btn-disabled-dark"
|
|
||||||
: "bg-primary-btn shadow-btn"
|
|
||||||
} text-text-btn-primary `;
|
|
||||||
break;
|
|
||||||
case "danger":
|
|
||||||
btnClassName = `bg-danger-btn text-text-btn-danger hover:bg-hovered-danger-btn`;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
btnClassName = `bg-default-btn text-text-btn-default hover:bg-hovered-btn`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
className={`
|
|
||||||
${className ?? ""}
|
|
||||||
py-2 px-3 flex items-center justify-center gap-1 rounded-action-btn transition-all duration-300 select-none
|
|
||||||
${disabled ? "cursor-not-allowed" : "cursor-pointer"}
|
|
||||||
${btnClassName}
|
|
||||||
follow-parent-svg
|
|
||||||
`}
|
|
||||||
onClick={onClick}
|
|
||||||
title={title}
|
|
||||||
disabled={disabled}
|
|
||||||
role="button"
|
|
||||||
tabIndex={tabIndex}
|
|
||||||
autoFocus={autoFocus}
|
|
||||||
>
|
|
||||||
{prefixIcon && (
|
|
||||||
<div className={`flex items-center justify-center`}>{prefixIcon}</div>
|
|
||||||
)}
|
|
||||||
{text && (
|
|
||||||
<div className={`font-common text-sm-title leading-4 line-clamp-1`}>
|
|
||||||
{text}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{icon && <div className={`flex items-center justify-center`}>{icon}</div>}
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import { ReactNode } from "react";
|
|
||||||
|
|
||||||
export interface CardProps {
|
|
||||||
className?: string;
|
|
||||||
children?: ReactNode;
|
|
||||||
title?: ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Card(props: CardProps) {
|
|
||||||
const { className, children, title } = props;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{title && (
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
capitalize !font-semibold text-sm-mobile font-weight-setting-card-title text-text-card-title
|
|
||||||
mb-3
|
|
||||||
|
|
||||||
ml-3
|
|
||||||
md:ml-4
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
{title}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className={`px-4 py-1 rounded-lg bg-card ${className}`}>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import BotIcon from "@/app/icons/bot.svg";
|
|
||||||
import LoadingIcon from "@/app/icons/three-dots.svg";
|
|
||||||
|
|
||||||
export default function GloablLoading({
|
|
||||||
noLogo,
|
|
||||||
}: {
|
|
||||||
noLogo?: boolean;
|
|
||||||
useSkeleton?: boolean;
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`flex flex-col justify-center items-center w-[100%] h-[100%]`}
|
|
||||||
>
|
|
||||||
{!noLogo && <BotIcon />}
|
|
||||||
<LoadingIcon />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import * as HoverCard from "@radix-ui/react-hover-card";
|
|
||||||
import { ComponentProps } from "react";
|
|
||||||
|
|
||||||
export interface PopoverProps {
|
|
||||||
content?: JSX.Element | string;
|
|
||||||
children?: JSX.Element;
|
|
||||||
arrowClassName?: string;
|
|
||||||
popoverClassName?: string;
|
|
||||||
noArrow?: boolean;
|
|
||||||
align?: ComponentProps<typeof HoverCard.Content>["align"];
|
|
||||||
openDelay?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function HoverPopover(props: PopoverProps) {
|
|
||||||
const {
|
|
||||||
content,
|
|
||||||
children,
|
|
||||||
arrowClassName,
|
|
||||||
popoverClassName,
|
|
||||||
noArrow = false,
|
|
||||||
align,
|
|
||||||
openDelay = 300,
|
|
||||||
} = props;
|
|
||||||
return (
|
|
||||||
<HoverCard.Root openDelay={openDelay}>
|
|
||||||
<HoverCard.Trigger asChild>{children}</HoverCard.Trigger>
|
|
||||||
<HoverCard.Portal>
|
|
||||||
<HoverCard.Content
|
|
||||||
className={`${popoverClassName}`}
|
|
||||||
sideOffset={5}
|
|
||||||
align={align}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
{!noArrow && <HoverCard.Arrow className={`${arrowClassName}`} />}
|
|
||||||
</HoverCard.Content>
|
|
||||||
</HoverCard.Portal>
|
|
||||||
</HoverCard.Root>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
import { CSSProperties } from "react";
|
|
||||||
import { getMessageImages } from "@/app/utils";
|
|
||||||
import { RequestMessage } from "@/app/client/api";
|
|
||||||
|
|
||||||
interface ImgsProps {
|
|
||||||
message: RequestMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Imgs(props: ImgsProps) {
|
|
||||||
const { message } = props;
|
|
||||||
const imgSrcs = getMessageImages(message);
|
|
||||||
|
|
||||||
if (imgSrcs.length < 1) {
|
|
||||||
return <></>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const imgVars = {
|
|
||||||
"--imgs-width": `calc(var(--max-message-width) - ${
|
|
||||||
imgSrcs.length - 1
|
|
||||||
}*0.25rem)`,
|
|
||||||
"--img-width": `calc(var(--imgs-width)/ ${imgSrcs.length})`,
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`w-[100%] mt-[0.625rem] flex gap-1`}
|
|
||||||
style={imgVars as CSSProperties}
|
|
||||||
>
|
|
||||||
{imgSrcs.map((image, index) => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={index}
|
|
||||||
className="flex-1 min-w-[var(--img-width)] pb-[var(--img-width)] object-cover bg-cover bg-no-repeat bg-center box-border rounded-chat-img"
|
|
||||||
style={{
|
|
||||||
backgroundImage: `url(${image})`,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
import PasswordVisible from "@/app/icons/passwordVisible.svg";
|
|
||||||
import PasswordInvisible from "@/app/icons/passwordInvisible.svg";
|
|
||||||
import {
|
|
||||||
DetailedHTMLProps,
|
|
||||||
InputHTMLAttributes,
|
|
||||||
useContext,
|
|
||||||
useLayoutEffect,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
import List, { ListContext } from "@/app/components/List";
|
|
||||||
|
|
||||||
export interface CommonInputProps
|
|
||||||
extends Omit<
|
|
||||||
DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>,
|
|
||||||
"onChange" | "type" | "value"
|
|
||||||
> {
|
|
||||||
className?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface NumberInputProps {
|
|
||||||
onChange?: (v: number) => void;
|
|
||||||
type?: "number";
|
|
||||||
value?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TextInputProps {
|
|
||||||
onChange?: (v: string) => void;
|
|
||||||
type?: "text" | "password";
|
|
||||||
value?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InputProps {
|
|
||||||
onChange?: ((v: string) => void) | ((v: number) => void);
|
|
||||||
type?: "text" | "password" | "number";
|
|
||||||
value?: string | number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Input(
|
|
||||||
props: CommonInputProps & NumberInputProps,
|
|
||||||
): JSX.Element;
|
|
||||||
export default function Input(
|
|
||||||
props: CommonInputProps & TextInputProps,
|
|
||||||
): JSX.Element;
|
|
||||||
export default function Input(props: CommonInputProps & InputProps) {
|
|
||||||
const { value, type = "text", onChange, className, ...rest } = props;
|
|
||||||
const [show, setShow] = useState(false);
|
|
||||||
|
|
||||||
const { inputClassName } = useContext(ListContext);
|
|
||||||
|
|
||||||
const internalType = (show && "text") || type;
|
|
||||||
|
|
||||||
const { update, handleValidate } = useContext(List.ListContext);
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
|
||||||
update?.({ type: "input" });
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
|
||||||
handleValidate?.(value);
|
|
||||||
}, [value]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={` group/input w-[100%] rounded-chat-input bg-input transition-colors duration-300 ease-in-out flex gap-3 items-center px-3 py-2 ${className} hover:bg-select-hover ${inputClassName}`}
|
|
||||||
>
|
|
||||||
<input
|
|
||||||
{...rest}
|
|
||||||
className=" overflow-hidden text-text-input text-sm-title leading-input outline-none flex-1 group-hover/input:bg-input-input-ele-hover"
|
|
||||||
type={internalType}
|
|
||||||
value={value}
|
|
||||||
onChange={(e) => {
|
|
||||||
if (type === "number") {
|
|
||||||
const v = e.currentTarget.valueAsNumber;
|
|
||||||
(onChange as NumberInputProps["onChange"])?.(v);
|
|
||||||
} else {
|
|
||||||
const v = e.currentTarget.value;
|
|
||||||
(onChange as TextInputProps["onChange"])?.(v);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
{type == "password" && (
|
|
||||||
<div className=" cursor-pointer" onClick={() => setShow((pre) => !pre)}>
|
|
||||||
{show ? <PasswordVisible /> : <PasswordInvisible />}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,167 +0,0 @@
|
|||||||
import {
|
|
||||||
ReactNode,
|
|
||||||
createContext,
|
|
||||||
useCallback,
|
|
||||||
useContext,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
|
|
||||||
interface WidgetStyle {
|
|
||||||
selectClassName?: string;
|
|
||||||
inputClassName?: string;
|
|
||||||
rangeClassName?: string;
|
|
||||||
switchClassName?: string;
|
|
||||||
inputNextLine?: boolean;
|
|
||||||
rangeNextLine?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ChildrenMeta {
|
|
||||||
type?: "unknown" | "input" | "range";
|
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ListProps {
|
|
||||||
className?: string;
|
|
||||||
children?: ReactNode;
|
|
||||||
id?: string;
|
|
||||||
isMobileScreen?: boolean;
|
|
||||||
widgetStyle?: WidgetStyle;
|
|
||||||
}
|
|
||||||
|
|
||||||
type Error =
|
|
||||||
| {
|
|
||||||
error: true;
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
error: false;
|
|
||||||
};
|
|
||||||
|
|
||||||
type Validate = (v: any) => Error | Promise<Error>;
|
|
||||||
|
|
||||||
export interface ListItemProps {
|
|
||||||
title: string;
|
|
||||||
subTitle?: string;
|
|
||||||
children?: JSX.Element | JSX.Element[];
|
|
||||||
className?: string;
|
|
||||||
onClick?: () => void;
|
|
||||||
nextline?: boolean;
|
|
||||||
validator?: Validate | Validate[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ListContext = createContext<
|
|
||||||
{
|
|
||||||
isMobileScreen?: boolean;
|
|
||||||
update?: (m: ChildrenMeta) => void;
|
|
||||||
handleValidate?: (v: any) => void;
|
|
||||||
} & WidgetStyle
|
|
||||||
>({ isMobileScreen: false });
|
|
||||||
|
|
||||||
export function ListItem(props: ListItemProps) {
|
|
||||||
const {
|
|
||||||
className = "",
|
|
||||||
onClick,
|
|
||||||
title,
|
|
||||||
subTitle,
|
|
||||||
children,
|
|
||||||
nextline,
|
|
||||||
validator,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const context = useContext(ListContext);
|
|
||||||
|
|
||||||
const [childrenMeta, setMeta] = useState<ChildrenMeta>({});
|
|
||||||
|
|
||||||
const { inputNextLine, rangeNextLine } = context;
|
|
||||||
|
|
||||||
const { type, error } = childrenMeta;
|
|
||||||
|
|
||||||
let internalNextLine;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "input":
|
|
||||||
internalNextLine = !!(nextline || inputNextLine);
|
|
||||||
break;
|
|
||||||
case "range":
|
|
||||||
internalNextLine = !!(nextline || rangeNextLine);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
internalNextLine = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const update = useCallback((m: ChildrenMeta) => {
|
|
||||||
setMeta((pre) => ({ ...pre, ...m }));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleValidate = useCallback((v: any) => {
|
|
||||||
let insideValidator;
|
|
||||||
if (!validator) {
|
|
||||||
insideValidator = () => {};
|
|
||||||
} else if (Array.isArray(validator)) {
|
|
||||||
insideValidator = (v: any) =>
|
|
||||||
Promise.race(validator.map((validate) => validate(v)));
|
|
||||||
} else {
|
|
||||||
insideValidator = validator;
|
|
||||||
}
|
|
||||||
|
|
||||||
Promise.resolve(insideValidator(v)).then((result) => {
|
|
||||||
if (result && result.error) {
|
|
||||||
return update({
|
|
||||||
error: result.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
update({
|
|
||||||
error: undefined,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`relative after:h-[0.5px] after:bottom-0 after:w-[100%] after:left-0 after:absolute last:after:hidden after:bg-list-item-divider ${
|
|
||||||
internalNextLine ? "" : "flex gap-3"
|
|
||||||
} justify-between items-center px-0 py-2 md:py-3 ${className}`}
|
|
||||||
onClick={onClick}
|
|
||||||
>
|
|
||||||
<div className={`flex-1 flex flex-col justify-start gap-1`}>
|
|
||||||
<div className=" font-common text-sm-mobile font-weight-[500] line-clamp-1 text-text-list-title">
|
|
||||||
{title}
|
|
||||||
</div>
|
|
||||||
{subTitle && (
|
|
||||||
<div className={` text-sm text-text-list-subtitle`}>{subTitle}</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<ListContext.Provider value={{ ...context, update, handleValidate }}>
|
|
||||||
<div
|
|
||||||
className={`${
|
|
||||||
internalNextLine ? "mt-[0.625rem]" : "max-w-[70%]"
|
|
||||||
} flex flex-col items-center justify-center`}
|
|
||||||
>
|
|
||||||
<div>{children}</div>
|
|
||||||
{!!error && (
|
|
||||||
<div className="text-text-btn-danger text-sm-mobile-tab mt-[0.3125rem] flex items-start w-[100%]">
|
|
||||||
<div className="">{error}</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</ListContext.Provider>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function List(props: ListProps) {
|
|
||||||
const { className, children, id, widgetStyle } = props;
|
|
||||||
const { isMobileScreen } = useContext(ListContext);
|
|
||||||
return (
|
|
||||||
<ListContext.Provider value={{ isMobileScreen, ...widgetStyle }}>
|
|
||||||
<div className={`flex flex-col w-[100%] ${className}`} id={id}>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
</ListContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
List.ListItem = ListItem;
|
|
||||||
List.ListContext = ListContext;
|
|
||||||
|
|
||||||
export default List;
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import BotIcon from "@/app/icons/bot.svg";
|
|
||||||
import LoadingIcon from "@/app/icons/three-dots.svg";
|
|
||||||
|
|
||||||
import { getCSSVar } from "@/app/utils";
|
|
||||||
|
|
||||||
export default function Loading({
|
|
||||||
noLogo,
|
|
||||||
useSkeleton = true,
|
|
||||||
}: {
|
|
||||||
noLogo?: boolean;
|
|
||||||
useSkeleton?: boolean;
|
|
||||||
}) {
|
|
||||||
let theme;
|
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
theme = getCSSVar("--default-container-bg");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
flex flex-col justify-center items-center w-[100%]
|
|
||||||
h-[100%]
|
|
||||||
md:my-2.5
|
|
||||||
md:ml-1
|
|
||||||
md:mr-2.5
|
|
||||||
md:rounded-md
|
|
||||||
md:h-[calc(100%-1.25rem)]
|
|
||||||
`}
|
|
||||||
style={{ background: useSkeleton ? theme : "" }}
|
|
||||||
>
|
|
||||||
{!noLogo && <BotIcon />}
|
|
||||||
<LoadingIcon />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
import {
|
|
||||||
DEFAULT_SIDEBAR_WIDTH,
|
|
||||||
MAX_SIDEBAR_WIDTH,
|
|
||||||
MIN_SIDEBAR_WIDTH,
|
|
||||||
Path,
|
|
||||||
} from "@/app/constant";
|
|
||||||
import useDrag from "@/app/hooks/useDrag";
|
|
||||||
import useMobileScreen from "@/app/hooks/useMobileScreen";
|
|
||||||
import { updateGlobalCSSVars } from "@/app/utils/client";
|
|
||||||
import { ComponentType, useRef, useState } from "react";
|
|
||||||
import { useAppConfig } from "@/app/store/config";
|
|
||||||
|
|
||||||
export interface MenuWrapperInspectProps {
|
|
||||||
setExternalProps?: (v: Record<string, any>) => void;
|
|
||||||
setShowPanel?: (v: boolean) => void;
|
|
||||||
showPanel?: boolean;
|
|
||||||
[k: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function MenuLayout<
|
|
||||||
ListComponentProps extends MenuWrapperInspectProps,
|
|
||||||
PanelComponentProps extends MenuWrapperInspectProps,
|
|
||||||
>(
|
|
||||||
ListComponent: ComponentType<ListComponentProps>,
|
|
||||||
PanelComponent: ComponentType<PanelComponentProps>,
|
|
||||||
) {
|
|
||||||
return function MenuHood(props: ListComponentProps & PanelComponentProps) {
|
|
||||||
const [showPanel, setShowPanel] = useState(false);
|
|
||||||
const [externalProps, setExternalProps] = useState({});
|
|
||||||
const config = useAppConfig();
|
|
||||||
|
|
||||||
const isMobileScreen = useMobileScreen();
|
|
||||||
|
|
||||||
const startDragWidth = useRef(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
|
|
||||||
// drag side bar
|
|
||||||
const { onDragStart } = useDrag({
|
|
||||||
customToggle: () => {
|
|
||||||
config.update((config) => {
|
|
||||||
config.sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
customDragMove: (nextWidth: number) => {
|
|
||||||
const { menuWidth } = updateGlobalCSSVars(nextWidth);
|
|
||||||
|
|
||||||
document.documentElement.style.setProperty(
|
|
||||||
"--menu-width",
|
|
||||||
`${menuWidth}px`,
|
|
||||||
);
|
|
||||||
config.update((config) => {
|
|
||||||
config.sidebarWidth = nextWidth;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
customLimit: (x: number) =>
|
|
||||||
Math.max(
|
|
||||||
MIN_SIDEBAR_WIDTH,
|
|
||||||
Math.min(MAX_SIDEBAR_WIDTH, startDragWidth.current + x),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
w-[100%] relative bg-center
|
|
||||||
max-md:h-[100%]
|
|
||||||
md:flex md:my-2.5
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
flex flex-col px-6
|
|
||||||
h-[100%]
|
|
||||||
max-md:w-[100%] max-md:px-4 max-md:pb-4 max-md:flex-1
|
|
||||||
md:relative md:basis-sidebar md:pb-6 md:rounded-md md:bg-menu
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<ListComponent
|
|
||||||
{...props}
|
|
||||||
setShowPanel={setShowPanel}
|
|
||||||
setExternalProps={setExternalProps}
|
|
||||||
showPanel={showPanel}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
{!isMobileScreen && (
|
|
||||||
<div
|
|
||||||
className={`group/menu-dragger cursor-col-resize w-[0.25rem] flex items-center justify-center`}
|
|
||||||
onPointerDown={(e) => {
|
|
||||||
startDragWidth.current = config.sidebarWidth;
|
|
||||||
onDragStart(e as any);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="w-[2px] opacity-0 group-hover/menu-dragger:opacity-100 bg-menu-dragger h-[100%] rounded-[2px]">
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
md:flex-1 md:h-[100%] md:w-page
|
|
||||||
max-md:transition-all max-md:duration-300 max-md:absolute max-md:top-0 max-md:max-h-[100vh] max-md:w-[100%] ${
|
|
||||||
showPanel ? "max-md:left-0" : "max-md:left-[101%]"
|
|
||||||
} max-md:z-10
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<PanelComponent
|
|
||||||
{...props}
|
|
||||||
{...externalProps}
|
|
||||||
setShowPanel={setShowPanel}
|
|
||||||
setExternalProps={setExternalProps}
|
|
||||||
showPanel={showPanel}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,352 +0,0 @@
|
|||||||
import React, { useLayoutEffect, useState } from "react";
|
|
||||||
import { createRoot } from "react-dom/client";
|
|
||||||
import * as AlertDialog from "@radix-ui/react-alert-dialog";
|
|
||||||
import Btn, { BtnProps } from "@/app/components/Btn";
|
|
||||||
|
|
||||||
import Warning from "@/app/icons/warning.svg";
|
|
||||||
import Close from "@/app/icons/closeIcon.svg";
|
|
||||||
|
|
||||||
export interface ModalProps {
|
|
||||||
onOk?: () => void;
|
|
||||||
onCancel?: () => void;
|
|
||||||
okText?: string;
|
|
||||||
cancelText?: string;
|
|
||||||
okBtnProps?: BtnProps;
|
|
||||||
cancelBtnProps?: BtnProps;
|
|
||||||
content?:
|
|
||||||
| React.ReactNode
|
|
||||||
| ((handlers: { close: () => void }) => JSX.Element);
|
|
||||||
title?: React.ReactNode;
|
|
||||||
visible?: boolean;
|
|
||||||
noFooter?: boolean;
|
|
||||||
noHeader?: boolean;
|
|
||||||
isMobile?: boolean;
|
|
||||||
closeble?: boolean;
|
|
||||||
type?: "modal" | "bottom-drawer";
|
|
||||||
headerBordered?: boolean;
|
|
||||||
modelClassName?: string;
|
|
||||||
onOpen?: (v: boolean) => void;
|
|
||||||
maskCloseble?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WarnProps
|
|
||||||
extends Omit<
|
|
||||||
ModalProps,
|
|
||||||
| "closeble"
|
|
||||||
| "isMobile"
|
|
||||||
| "noHeader"
|
|
||||||
| "noFooter"
|
|
||||||
| "onOk"
|
|
||||||
| "okBtnProps"
|
|
||||||
| "cancelBtnProps"
|
|
||||||
| "content"
|
|
||||||
> {
|
|
||||||
onOk?: () => Promise<void> | void;
|
|
||||||
content?: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TriggerProps
|
|
||||||
extends Omit<ModalProps, "visible" | "onOk" | "onCancel"> {
|
|
||||||
children: JSX.Element;
|
|
||||||
className?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseZIndex = 150;
|
|
||||||
|
|
||||||
const Modal = (props: ModalProps) => {
|
|
||||||
const {
|
|
||||||
onOk,
|
|
||||||
onCancel,
|
|
||||||
okText,
|
|
||||||
cancelText,
|
|
||||||
content,
|
|
||||||
title,
|
|
||||||
visible,
|
|
||||||
noFooter,
|
|
||||||
noHeader,
|
|
||||||
closeble = true,
|
|
||||||
okBtnProps,
|
|
||||||
cancelBtnProps,
|
|
||||||
type = "modal",
|
|
||||||
headerBordered,
|
|
||||||
modelClassName,
|
|
||||||
onOpen,
|
|
||||||
maskCloseble = true,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const [open, setOpen] = useState(!!visible);
|
|
||||||
|
|
||||||
const mergeOpen = visible ?? open;
|
|
||||||
|
|
||||||
const handleClose = () => {
|
|
||||||
setOpen(false);
|
|
||||||
onCancel?.();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleOk = () => {
|
|
||||||
setOpen(false);
|
|
||||||
onOk?.();
|
|
||||||
};
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
|
||||||
onOpen?.(mergeOpen);
|
|
||||||
}, [mergeOpen]);
|
|
||||||
|
|
||||||
let layoutClassName = "";
|
|
||||||
let panelClassName = "";
|
|
||||||
let titleClassName = "";
|
|
||||||
let footerClassName = "";
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "bottom-drawer":
|
|
||||||
layoutClassName = "fixed inset-0 flex flex-col w-[100%] bottom-0";
|
|
||||||
panelClassName =
|
|
||||||
"rounded-t-chat-model-select overflow-y-auto overflow-x-hidden";
|
|
||||||
titleClassName = "px-4 py-3";
|
|
||||||
footerClassName = "absolute w-[100%]";
|
|
||||||
break;
|
|
||||||
case "modal":
|
|
||||||
default:
|
|
||||||
layoutClassName =
|
|
||||||
"fixed inset-0 flex flex-col item-start top-0 left-[50vw] translate-x-[-50%] max-sm:w-modal-modal-type-mobile";
|
|
||||||
panelClassName = "rounded-lg px-6 sm:w-modal-modal-type";
|
|
||||||
titleClassName = "py-6 max-sm:pb-3";
|
|
||||||
footerClassName = "py-6";
|
|
||||||
}
|
|
||||||
const btnCommonClass = "px-4 py-2.5 rounded-md max-sm:flex-1";
|
|
||||||
const { className: okBtnClass } = okBtnProps || {};
|
|
||||||
const { className: cancelBtnClass } = cancelBtnProps || {};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AlertDialog.Root open={mergeOpen} onOpenChange={setOpen}>
|
|
||||||
<AlertDialog.Portal>
|
|
||||||
<AlertDialog.Overlay
|
|
||||||
className="bg-modal-mask fixed inset-0 animate-mask "
|
|
||||||
style={{ zIndex: baseZIndex - 1 }}
|
|
||||||
onClick={() => {
|
|
||||||
if (maskCloseble) {
|
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<AlertDialog.Content
|
|
||||||
className={`
|
|
||||||
${layoutClassName}
|
|
||||||
`}
|
|
||||||
style={{ zIndex: baseZIndex - 1 }}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className="flex-1"
|
|
||||||
onClick={() => {
|
|
||||||
if (maskCloseble) {
|
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
className={`flex flex-col flex-0
|
|
||||||
bg-moda-panel text-modal-panel
|
|
||||||
${modelClassName}
|
|
||||||
${panelClassName}
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
{!noHeader && (
|
|
||||||
<AlertDialog.Title
|
|
||||||
className={`
|
|
||||||
flex items-center justify-between gap-3 font-common
|
|
||||||
md:text-chat-header-title md:font-bold md:leading-5
|
|
||||||
${
|
|
||||||
headerBordered
|
|
||||||
? " border-b border-modal-header-bottom"
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
${titleClassName}
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<div className="flex gap-3 justify-start flex-1 items-center text-text-modal-title text-chat-header-title">
|
|
||||||
{title}
|
|
||||||
</div>
|
|
||||||
{closeble && (
|
|
||||||
<div
|
|
||||||
className="items-center"
|
|
||||||
onClick={() => {
|
|
||||||
handleClose();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Close />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</AlertDialog.Title>
|
|
||||||
)}
|
|
||||||
<div className="flex-1 overflow-hidden text-text-modal-content text-sm-title">
|
|
||||||
{typeof content === "function"
|
|
||||||
? content({
|
|
||||||
close: () => {
|
|
||||||
handleClose();
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: content}
|
|
||||||
</div>
|
|
||||||
{!noFooter && (
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
flex gap-3 sm:justify-end max-sm:justify-between
|
|
||||||
${footerClassName}
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<AlertDialog.Cancel asChild>
|
|
||||||
<Btn
|
|
||||||
{...cancelBtnProps}
|
|
||||||
onClick={() => handleClose()}
|
|
||||||
text={cancelText}
|
|
||||||
className={`${btnCommonClass} ${cancelBtnClass}`}
|
|
||||||
/>
|
|
||||||
</AlertDialog.Cancel>
|
|
||||||
<AlertDialog.Action asChild>
|
|
||||||
<Btn
|
|
||||||
{...okBtnProps}
|
|
||||||
onClick={handleOk}
|
|
||||||
text={okText}
|
|
||||||
className={`${btnCommonClass} ${okBtnClass}`}
|
|
||||||
/>
|
|
||||||
</AlertDialog.Action>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
{type === "modal" && (
|
|
||||||
<div
|
|
||||||
className="flex-1"
|
|
||||||
onClick={() => {
|
|
||||||
if (maskCloseble) {
|
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</AlertDialog.Content>
|
|
||||||
</AlertDialog.Portal>
|
|
||||||
</AlertDialog.Root>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const Warn = ({
|
|
||||||
title,
|
|
||||||
onOk,
|
|
||||||
visible,
|
|
||||||
content,
|
|
||||||
...props
|
|
||||||
}: WarnProps) => {
|
|
||||||
const [internalVisible, setVisible] = useState(visible);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Modal
|
|
||||||
{...props}
|
|
||||||
title={
|
|
||||||
<>
|
|
||||||
<Warning />
|
|
||||||
{title}
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
content={
|
|
||||||
<AlertDialog.Description
|
|
||||||
className={`
|
|
||||||
font-common font-normal
|
|
||||||
md:text-sm-title md:leading-[158%]
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
</AlertDialog.Description>
|
|
||||||
}
|
|
||||||
closeble={false}
|
|
||||||
onOk={() => {
|
|
||||||
const toDo = onOk?.();
|
|
||||||
if (toDo instanceof Promise) {
|
|
||||||
toDo.then(() => {
|
|
||||||
setVisible(false);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setVisible(false);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
visible={internalVisible}
|
|
||||||
okBtnProps={{
|
|
||||||
className: `bg-delete-chat-ok-btn text-text-delete-chat-ok-btn `,
|
|
||||||
}}
|
|
||||||
cancelBtnProps={{
|
|
||||||
className: `bg-delete-chat-cancel-btn border border-delete-chat-cancel-btn text-text-delete-chat-cancel-btn`,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const div = document.createElement("div");
|
|
||||||
div.id = "confirm-root";
|
|
||||||
div.style.height = "0px";
|
|
||||||
document.body.appendChild(div);
|
|
||||||
|
|
||||||
Modal.warn = (props: Omit<WarnProps, "visible" | "onCancel" | "onOk">) => {
|
|
||||||
const root = createRoot(div);
|
|
||||||
const closeModal = () => {
|
|
||||||
root.unmount();
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Promise<boolean>((resolve) => {
|
|
||||||
root.render(
|
|
||||||
<Warn
|
|
||||||
{...props}
|
|
||||||
visible={true}
|
|
||||||
onCancel={() => {
|
|
||||||
closeModal();
|
|
||||||
resolve(false);
|
|
||||||
}}
|
|
||||||
onOk={() => {
|
|
||||||
closeModal();
|
|
||||||
resolve(true);
|
|
||||||
}}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const Trigger = (props: TriggerProps) => {
|
|
||||||
const { children, className, content, ...rest } = props;
|
|
||||||
|
|
||||||
const [internalVisible, setVisible] = useState(false);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div
|
|
||||||
className={className}
|
|
||||||
onClick={() => {
|
|
||||||
setVisible(true);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
<Modal
|
|
||||||
{...rest}
|
|
||||||
visible={internalVisible}
|
|
||||||
onCancel={() => {
|
|
||||||
setVisible(false);
|
|
||||||
}}
|
|
||||||
content={
|
|
||||||
typeof content === "function"
|
|
||||||
? content({
|
|
||||||
close: () => {
|
|
||||||
setVisible(false);
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: content
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
Modal.Trigger = Trigger;
|
|
||||||
|
|
||||||
export default Modal;
|
|
||||||
@@ -1,352 +0,0 @@
|
|||||||
import useRelativePosition from "@/app/hooks/useRelativePosition";
|
|
||||||
import {
|
|
||||||
RefObject,
|
|
||||||
useEffect,
|
|
||||||
useLayoutEffect,
|
|
||||||
useMemo,
|
|
||||||
useRef,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
import { createPortal } from "react-dom";
|
|
||||||
|
|
||||||
const ArrowIcon = ({ sibling }: { sibling: RefObject<HTMLDivElement> }) => {
|
|
||||||
const [color, setColor] = useState<string>("");
|
|
||||||
useEffect(() => {
|
|
||||||
if (sibling.current) {
|
|
||||||
const { backgroundColor } = window.getComputedStyle(sibling.current);
|
|
||||||
setColor(backgroundColor);
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<svg
|
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
width="16"
|
|
||||||
height="6"
|
|
||||||
viewBox="0 0 16 6"
|
|
||||||
fill="none"
|
|
||||||
>
|
|
||||||
<path
|
|
||||||
d="M16 0H0C1.28058 0 2.50871 0.508709 3.41421 1.41421L6.91 4.91C7.51199 5.51199 8.48801 5.51199 9.09 4.91L12.5858 1.41421C13.4913 0.508708 14.7194 0 16 0Z"
|
|
||||||
fill={color}
|
|
||||||
/>
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const baseZIndex = 100;
|
|
||||||
const popoverRootName = "popoverRoot";
|
|
||||||
let popoverRoot = document.querySelector(
|
|
||||||
`#${popoverRootName}`,
|
|
||||||
) as HTMLDivElement;
|
|
||||||
if (!popoverRoot) {
|
|
||||||
popoverRoot = document.createElement("div");
|
|
||||||
document.body.appendChild(popoverRoot);
|
|
||||||
popoverRoot.style.height = "0px";
|
|
||||||
popoverRoot.style.width = "100%";
|
|
||||||
popoverRoot.style.position = "fixed";
|
|
||||||
popoverRoot.style.bottom = "0";
|
|
||||||
popoverRoot.style.zIndex = "10000";
|
|
||||||
popoverRoot.id = "popover-root";
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PopoverProps {
|
|
||||||
content?: JSX.Element | string;
|
|
||||||
children?: JSX.Element;
|
|
||||||
show?: boolean;
|
|
||||||
onShow?: (v: boolean) => void;
|
|
||||||
className?: string;
|
|
||||||
popoverClassName?: string;
|
|
||||||
trigger?: "hover" | "click";
|
|
||||||
placement?: "t" | "lt" | "rt" | "lb" | "rb" | "b" | "l" | "r";
|
|
||||||
noArrow?: boolean;
|
|
||||||
delayClose?: number;
|
|
||||||
useGlobalRoot?: boolean;
|
|
||||||
getPopoverPanelRef?: (ref: RefObject<HTMLDivElement>) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Popover(props: PopoverProps) {
|
|
||||||
const {
|
|
||||||
content,
|
|
||||||
children,
|
|
||||||
show,
|
|
||||||
onShow,
|
|
||||||
className,
|
|
||||||
popoverClassName,
|
|
||||||
trigger = "hover",
|
|
||||||
placement = "t",
|
|
||||||
noArrow = false,
|
|
||||||
delayClose = 0,
|
|
||||||
useGlobalRoot,
|
|
||||||
getPopoverPanelRef,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const [internalShow, setShow] = useState(false);
|
|
||||||
const { position, getRelativePosition } = useRelativePosition({
|
|
||||||
delay: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
const popoverCommonClass = `absolute p-2 box-border`;
|
|
||||||
|
|
||||||
const mergedShow = show ?? internalShow;
|
|
||||||
|
|
||||||
const { arrowClassName, placementStyle, placementClassName } = useMemo(() => {
|
|
||||||
const arrowCommonClassName = `${
|
|
||||||
noArrow ? "hidden" : ""
|
|
||||||
} absolute z-10 left-[50%] translate-x-[calc(-50%)]`;
|
|
||||||
|
|
||||||
let defaultTopPlacement = true; // when users dont config 't' or 'b'
|
|
||||||
|
|
||||||
const {
|
|
||||||
distanceToBottomBoundary = 0,
|
|
||||||
distanceToLeftBoundary = 0,
|
|
||||||
distanceToRightBoundary = -10000,
|
|
||||||
distanceToTopBoundary = 0,
|
|
||||||
targetH = 0,
|
|
||||||
targetW = 0,
|
|
||||||
} = position?.poi || {};
|
|
||||||
|
|
||||||
if (distanceToBottomBoundary > distanceToTopBoundary) {
|
|
||||||
defaultTopPlacement = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const placements = {
|
|
||||||
lt: {
|
|
||||||
placementStyle: {
|
|
||||||
bottom: `calc(${distanceToBottomBoundary + targetH}px + 0.5rem)`,
|
|
||||||
left: `calc(${distanceToLeftBoundary}px - ${targetW * 0.02}px)`,
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} bottom-[calc(100%+0.5rem)] translate-y-[calc(100%)] pb-[0.5rem]`,
|
|
||||||
placementClassName: "bottom-[calc(100%+0.5rem)] left-[calc(-2%)]",
|
|
||||||
},
|
|
||||||
lb: {
|
|
||||||
placementStyle: {
|
|
||||||
top: `calc(-${distanceToBottomBoundary}px + 0.5rem)`,
|
|
||||||
left: `calc(${distanceToLeftBoundary}px - ${targetW * 0.02}px)`,
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} top-[calc(100%+0.5rem)] translate-y-[calc(-100%)] pt-[0.5rem]`,
|
|
||||||
placementClassName: "top-[calc(100%+0.5rem)] left-[calc(-2%)]",
|
|
||||||
},
|
|
||||||
rt: {
|
|
||||||
placementStyle: {
|
|
||||||
bottom: `calc(${distanceToBottomBoundary + targetH}px + 0.5rem)`,
|
|
||||||
right: `calc(${distanceToRightBoundary}px - ${targetW * 0.02}px)`,
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} bottom-[calc(100%+0.5rem)] translate-y-[calc(100%)] pb-[0.5rem]`,
|
|
||||||
placementClassName: "bottom-[calc(100%+0.5rem)] right-[calc(-2%)]",
|
|
||||||
},
|
|
||||||
rb: {
|
|
||||||
placementStyle: {
|
|
||||||
top: `calc(-${distanceToBottomBoundary}px + 0.5rem)`,
|
|
||||||
right: `calc(${distanceToRightBoundary}px - ${targetW * 0.02}px)`,
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} top-[calc(100%+0.5rem)] translate-y-[calc(-100%)] pt-[0.5rem]`,
|
|
||||||
placementClassName: "top-[calc(100%+0.5rem)] right-[calc(-2%)]",
|
|
||||||
},
|
|
||||||
t: {
|
|
||||||
placementStyle: {
|
|
||||||
bottom: `calc(${distanceToBottomBoundary + targetH}px + 0.5rem)`,
|
|
||||||
left: `calc(${distanceToLeftBoundary + targetW / 2}px`,
|
|
||||||
transform: "translateX(-50%)",
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} bottom-[calc(100%+0.5rem)] translate-y-[calc(100%)] pb-[0.5rem]`,
|
|
||||||
placementClassName:
|
|
||||||
"bottom-[calc(100%+0.5rem)] left-[50%] translate-x-[calc(-50%)]",
|
|
||||||
},
|
|
||||||
b: {
|
|
||||||
placementStyle: {
|
|
||||||
top: `calc(-${distanceToBottomBoundary}px + 0.5rem)`,
|
|
||||||
left: `calc(${distanceToLeftBoundary + targetW / 2}px`,
|
|
||||||
transform: "translateX(-50%)",
|
|
||||||
},
|
|
||||||
arrowClassName: `${arrowCommonClassName} top-[calc(100%+0.5rem)] translate-y-[calc(-100%)] pt-[0.5rem]`,
|
|
||||||
placementClassName:
|
|
||||||
"top-[calc(100%+0.5rem)] left-[50%] translate-x-[calc(-50%)]",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const getStyle = () => {
|
|
||||||
if (["l", "r"].includes(placement)) {
|
|
||||||
return placements[
|
|
||||||
`${placement}${defaultTopPlacement ? "t" : "b"}` as
|
|
||||||
| "lt"
|
|
||||||
| "lb"
|
|
||||||
| "rb"
|
|
||||||
| "rt"
|
|
||||||
];
|
|
||||||
}
|
|
||||||
return placements[placement as Exclude<typeof placement, "l" | "r">];
|
|
||||||
};
|
|
||||||
|
|
||||||
return getStyle();
|
|
||||||
}, [Object.values(position?.poi || {})]);
|
|
||||||
|
|
||||||
const popoverRef = useRef<HTMLDivElement>(null);
|
|
||||||
const closeTimer = useRef<number>(0);
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
|
||||||
getPopoverPanelRef?.(popoverRef);
|
|
||||||
onShow?.(internalShow);
|
|
||||||
}, [internalShow]);
|
|
||||||
|
|
||||||
if (trigger === "click") {
|
|
||||||
const handleOpen = (e: { currentTarget: any }) => {
|
|
||||||
clearTimeout(closeTimer.current);
|
|
||||||
setShow(true);
|
|
||||||
getRelativePosition(e.currentTarget, "");
|
|
||||||
window.document.documentElement.style.overflow = "hidden";
|
|
||||||
};
|
|
||||||
const handleClose = () => {
|
|
||||||
if (delayClose) {
|
|
||||||
closeTimer.current = window.setTimeout(() => {
|
|
||||||
setShow(false);
|
|
||||||
}, delayClose);
|
|
||||||
} else {
|
|
||||||
setShow(false);
|
|
||||||
}
|
|
||||||
window.document.documentElement.style.overflow = "auto";
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`relative ${className}`}
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
if (!mergedShow) {
|
|
||||||
handleOpen(e);
|
|
||||||
} else {
|
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
{mergedShow && (
|
|
||||||
<>
|
|
||||||
{!noArrow && (
|
|
||||||
<div className={`${arrowClassName}`}>
|
|
||||||
<ArrowIcon sibling={popoverRef} />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{createPortal(
|
|
||||||
<div
|
|
||||||
className={`${popoverCommonClass} ${popoverClassName} cursor-pointer overflow-auto`}
|
|
||||||
style={{ zIndex: baseZIndex + 1, ...placementStyle }}
|
|
||||||
ref={popoverRef}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
</div>,
|
|
||||||
popoverRoot,
|
|
||||||
)}
|
|
||||||
{createPortal(
|
|
||||||
<div
|
|
||||||
className=" fixed w-[100vw] h-[100vh] right-0 bottom-0"
|
|
||||||
style={{ zIndex: baseZIndex }}
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
handleClose();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
|
|
||||||
</div>,
|
|
||||||
popoverRoot,
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useGlobalRoot) {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`relative ${className}`}
|
|
||||||
onPointerEnter={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
clearTimeout(closeTimer.current);
|
|
||||||
onShow?.(true);
|
|
||||||
setShow(true);
|
|
||||||
getRelativePosition(e.currentTarget, "");
|
|
||||||
window.document.documentElement.style.overflow = "hidden";
|
|
||||||
}}
|
|
||||||
onPointerLeave={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (delayClose) {
|
|
||||||
closeTimer.current = window.setTimeout(() => {
|
|
||||||
onShow?.(false);
|
|
||||||
setShow(false);
|
|
||||||
}, delayClose);
|
|
||||||
} else {
|
|
||||||
onShow?.(false);
|
|
||||||
setShow(false);
|
|
||||||
}
|
|
||||||
window.document.documentElement.style.overflow = "auto";
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
{mergedShow && (
|
|
||||||
<>
|
|
||||||
<div
|
|
||||||
className={`${
|
|
||||||
noArrow ? "opacity-0" : ""
|
|
||||||
} bg-inherit ${arrowClassName}`}
|
|
||||||
style={{ zIndex: baseZIndex + 1 }}
|
|
||||||
>
|
|
||||||
<ArrowIcon sibling={popoverRef} />
|
|
||||||
</div>
|
|
||||||
{createPortal(
|
|
||||||
<div
|
|
||||||
className={` whitespace-nowrap ${popoverCommonClass} ${popoverClassName} cursor-pointer`}
|
|
||||||
style={{ zIndex: baseZIndex + 1, ...placementStyle }}
|
|
||||||
ref={popoverRef}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
</div>,
|
|
||||||
popoverRoot,
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`group/popover relative ${className}`}
|
|
||||||
onPointerEnter={(e) => {
|
|
||||||
getRelativePosition(e.currentTarget, "");
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
}}
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
hidden group-hover/popover:block
|
|
||||||
${noArrow ? "opacity-0" : ""}
|
|
||||||
bg-inherit
|
|
||||||
${arrowClassName}
|
|
||||||
`}
|
|
||||||
style={{ zIndex: baseZIndex + 1 }}
|
|
||||||
>
|
|
||||||
<ArrowIcon sibling={popoverRef} />
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
hidden group-hover/popover:block whitespace-nowrap
|
|
||||||
${popoverCommonClass}
|
|
||||||
${placementClassName}
|
|
||||||
${popoverClassName}
|
|
||||||
`}
|
|
||||||
ref={popoverRef}
|
|
||||||
style={{ zIndex: baseZIndex + 1 }}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
import { useLocation } from "react-router-dom";
|
|
||||||
import { useMemo, ReactNode } from "react";
|
|
||||||
import { Path, SIDEBAR_ID, SlotID } from "@/app/constant";
|
|
||||||
import { getLang } from "@/app/locales";
|
|
||||||
|
|
||||||
import useMobileScreen from "@/app/hooks/useMobileScreen";
|
|
||||||
import { isIOS } from "@/app/utils";
|
|
||||||
import useListenWinResize from "@/app/hooks/useListenWinResize";
|
|
||||||
|
|
||||||
interface ScreenProps {
|
|
||||||
children: ReactNode;
|
|
||||||
noAuth: ReactNode;
|
|
||||||
sidebar: ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Screen(props: ScreenProps) {
|
|
||||||
const location = useLocation();
|
|
||||||
const isAuth = location.pathname === Path.Auth;
|
|
||||||
|
|
||||||
const isMobileScreen = useMobileScreen();
|
|
||||||
const isIOSMobile = useMemo(
|
|
||||||
() => isIOS() && isMobileScreen,
|
|
||||||
[isMobileScreen],
|
|
||||||
);
|
|
||||||
|
|
||||||
useListenWinResize();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
flex h-[100%] w-[100%] bg-center
|
|
||||||
max-md:relative max-md:flex-col-reverse max-md:bg-global-mobile
|
|
||||||
md:overflow-hidden md:bg-global
|
|
||||||
`}
|
|
||||||
style={{
|
|
||||||
direction: getLang() === "ar" ? "rtl" : "ltr",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{isAuth ? (
|
|
||||||
props.noAuth
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
max-md:absolute max-md:w-[100%] max-md:bottom-0 max-md:z-10
|
|
||||||
md:flex-0 md:overflow-hidden
|
|
||||||
`}
|
|
||||||
id={SIDEBAR_ID}
|
|
||||||
>
|
|
||||||
{props.sidebar}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
className={`
|
|
||||||
h-[100%]
|
|
||||||
max-md:w-[100%]
|
|
||||||
md:flex-1 md:min-w-0 md:overflow-hidden md:flex
|
|
||||||
`}
|
|
||||||
id={SlotID.AppBody}
|
|
||||||
style={{
|
|
||||||
// #3016 disable transition on ios mobile screen
|
|
||||||
transition: isIOSMobile ? "none" : undefined,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{props.children}
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
.search {
|
|
||||||
display: flex;
|
|
||||||
max-width: 460px;
|
|
||||||
height: 50px;
|
|
||||||
padding: 16px;
|
|
||||||
align-items: center;
|
|
||||||
gap: 8px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
|
|
||||||
border-radius: 16px;
|
|
||||||
border: 1px solid var(--Light-Text-Black, #18182A);
|
|
||||||
background: var(--light-opacity-white-70, rgba(255, 255, 255, 0.70));
|
|
||||||
box-shadow: 0px 8px 40px 0px rgba(60, 68, 255, 0.12);
|
|
||||||
|
|
||||||
.icon {
|
|
||||||
height: 20px;
|
|
||||||
width: 20px;
|
|
||||||
flex: 0 0;
|
|
||||||
}
|
|
||||||
.input {
|
|
||||||
height: 18px;
|
|
||||||
flex: 1 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
import styles from "./index.module.scss";
|
|
||||||
import SearchIcon from "@/app/icons/search.svg";
|
|
||||||
|
|
||||||
export interface SearchProps {
|
|
||||||
value?: string;
|
|
||||||
onSearch?: (v: string) => void;
|
|
||||||
placeholder?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Search = (props: SearchProps) => {
|
|
||||||
const { placeholder = "", value, onSearch } = props;
|
|
||||||
return (
|
|
||||||
<div className={styles["search"]}>
|
|
||||||
<div className={styles["icon"]}>
|
|
||||||
<SearchIcon />
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
className={styles["input"]}
|
|
||||||
placeholder={placeholder}
|
|
||||||
value={value}
|
|
||||||
onChange={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
onSearch?.(e.target.value);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Search;
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
import SelectIcon from "@/app/icons/downArrowIcon.svg";
|
|
||||||
import Popover from "@/app/components/Popover";
|
|
||||||
import React, { useContext, useMemo, useRef } from "react";
|
|
||||||
import useRelativePosition, {
|
|
||||||
Orientation,
|
|
||||||
} from "@/app/hooks/useRelativePosition";
|
|
||||||
import List from "@/app/components/List";
|
|
||||||
|
|
||||||
import Selected from "@/app/icons/selectedIcon.svg";
|
|
||||||
|
|
||||||
export type Option<Value> = {
|
|
||||||
value: Value;
|
|
||||||
label: string;
|
|
||||||
icon?: React.ReactNode;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface SearchProps<Value> {
|
|
||||||
value?: string;
|
|
||||||
onSelect?: (v: Value) => void;
|
|
||||||
options?: Option<Value>[];
|
|
||||||
inMobile?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Select = <Value extends number | string>(props: SearchProps<Value>) => {
|
|
||||||
const { value, onSelect, options = [], inMobile } = props;
|
|
||||||
|
|
||||||
const { isMobileScreen, selectClassName } = useContext(List.ListContext);
|
|
||||||
|
|
||||||
const optionsRef = useRef<Option<Value>[]>([]);
|
|
||||||
optionsRef.current = options;
|
|
||||||
const selectedOption = useMemo(
|
|
||||||
() => optionsRef.current.find((o) => o.value === value),
|
|
||||||
[value],
|
|
||||||
);
|
|
||||||
|
|
||||||
const contentRef = useRef<HTMLDivElement>(null);
|
|
||||||
|
|
||||||
const { position, getRelativePosition } = useRelativePosition({
|
|
||||||
delay: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
let headerH = 100;
|
|
||||||
let baseH = position?.poi.distanceToBottomBoundary || 0;
|
|
||||||
if (isMobileScreen) {
|
|
||||||
headerH = 60;
|
|
||||||
}
|
|
||||||
if (position?.poi.relativePosition[1] === Orientation.bottom) {
|
|
||||||
baseH = position?.poi.distanceToTopBoundary;
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxHeight = `${baseH - headerH}px`;
|
|
||||||
|
|
||||||
const content = (
|
|
||||||
<div
|
|
||||||
className={` flex flex-col gap-1 overflow-y-auto overflow-x-hidden`}
|
|
||||||
style={{ maxHeight }}
|
|
||||||
>
|
|
||||||
{options?.map((o) => (
|
|
||||||
<div
|
|
||||||
key={o.value}
|
|
||||||
className={`
|
|
||||||
flex items-center px-3 py-2 gap-3 rounded-action-btn hover:bg-select-option-hovered cursor-pointer
|
|
||||||
`}
|
|
||||||
onClick={() => {
|
|
||||||
onSelect?.(o.value);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="flex gap-2 flex-1 follow-parent-svg text-text-select-option">
|
|
||||||
{!!o.icon && <div className="flex items-center">{o.icon}</div>}
|
|
||||||
<div className={`flex-1 text-text-select-option`}>{o.label}</div>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
className={
|
|
||||||
selectedOption?.value === o.value ? "opacity-100" : "opacity-0"
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Selected />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Popover
|
|
||||||
content={content}
|
|
||||||
trigger="click"
|
|
||||||
noArrow
|
|
||||||
placement={
|
|
||||||
position?.poi.relativePosition[1] !== Orientation.bottom ? "rb" : "rt"
|
|
||||||
}
|
|
||||||
popoverClassName="border border-select-popover rounded-lg shadow-select-popover-shadow w-actions-popover bg-select-popover-panel"
|
|
||||||
onShow={(e) => {
|
|
||||||
getRelativePosition(contentRef.current!, "");
|
|
||||||
}}
|
|
||||||
className={selectClassName}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={`flex items-center gap-3 py-2 px-3 bg-select rounded-action-btn font-common text-sm-title cursor-pointer hover:bg-select-hover transition duration-300 ease-in-out`}
|
|
||||||
ref={contentRef}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={`flex items-center gap-2 flex-1 follow-parent-svg text-text-select`}
|
|
||||||
>
|
|
||||||
{!!selectedOption?.icon && (
|
|
||||||
<div className={``}>{selectedOption?.icon}</div>
|
|
||||||
)}
|
|
||||||
<div className={`flex-1`}>{selectedOption?.label}</div>
|
|
||||||
</div>
|
|
||||||
<div className={``}>
|
|
||||||
<SelectIcon />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Popover>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Select;
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
import { useContext, useEffect, useRef } from "react";
|
|
||||||
import { ListContext } from "@/app/components/List";
|
|
||||||
import { useResizeObserver } from "usehooks-ts";
|
|
||||||
|
|
||||||
interface SlideRangeProps {
|
|
||||||
className?: string;
|
|
||||||
description?: string;
|
|
||||||
range?: {
|
|
||||||
start?: number;
|
|
||||||
stroke?: number;
|
|
||||||
};
|
|
||||||
onSlide?: (v: number) => void;
|
|
||||||
value?: number;
|
|
||||||
step?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const margin = 15;
|
|
||||||
|
|
||||||
export default function SlideRange(props: SlideRangeProps) {
|
|
||||||
const {
|
|
||||||
className = "",
|
|
||||||
description = "",
|
|
||||||
range = {},
|
|
||||||
value,
|
|
||||||
onSlide,
|
|
||||||
step,
|
|
||||||
} = props;
|
|
||||||
const { start = 0, stroke = 1 } = range;
|
|
||||||
|
|
||||||
const { rangeClassName, update } = useContext(ListContext);
|
|
||||||
|
|
||||||
const slideRef = useRef<HTMLDivElement>(null);
|
|
||||||
|
|
||||||
useResizeObserver({
|
|
||||||
ref: slideRef,
|
|
||||||
onResize: () => {
|
|
||||||
setProperty(value);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const transformToWidth = (x: number = start) => {
|
|
||||||
const abs = x - start;
|
|
||||||
const maxWidth = (slideRef.current?.clientWidth || 1) - margin * 2;
|
|
||||||
const result = (abs / stroke) * maxWidth;
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
const setProperty = (value?: number) => {
|
|
||||||
const initWidth = transformToWidth(value);
|
|
||||||
slideRef.current?.style.setProperty(
|
|
||||||
"--slide-value-size",
|
|
||||||
`${initWidth + margin}px`,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
update?.({ type: "range" });
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={`flex flex-col justify-center items-end gap-1 w-[100%] ${className} ${rangeClassName}`}
|
|
||||||
>
|
|
||||||
{!!description && (
|
|
||||||
<div className=" text-common text-sm ">{description}</div>
|
|
||||||
)}
|
|
||||||
<div
|
|
||||||
className="flex my-1.5 relative w-[100%] h-1.5 bg-slider rounded-slide cursor-pointer"
|
|
||||||
ref={slideRef}
|
|
||||||
>
|
|
||||||
<div className="cursor-pointer absolute marker:top-0 h-[100%] w-[var(--slide-value-size)] bg-slider-slided-travel rounded-slide">
|
|
||||||
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
className="cursor-pointer absolute z-1 w-[30px] top-[50%] translate-y-[-50%] left-[var(--slide-value-size)] translate-x-[-50%] h-slide-btn leading-slide-btn text-sm-mobile text-center rounded-slide border border-slider-block bg-slider-block hover:bg-slider-block-hover text-text-slider-block"
|
|
||||||
// onPointerDown={onPointerDown}
|
|
||||||
>
|
|
||||||
{value}
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
type="range"
|
|
||||||
className="w-[100%] h-[100%] opacity-0 cursor-pointer"
|
|
||||||
value={value}
|
|
||||||
min={start}
|
|
||||||
max={start + stroke}
|
|
||||||
step={step}
|
|
||||||
onChange={(e) => {
|
|
||||||
setProperty(e.target.valueAsNumber);
|
|
||||||
onSlide?.(e.target.valueAsNumber);
|
|
||||||
}}
|
|
||||||
style={{
|
|
||||||
marginLeft: margin,
|
|
||||||
marginRight: margin,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import * as RadixSwitch from "@radix-ui/react-switch";
|
|
||||||
import { useContext } from "react";
|
|
||||||
import List from "../List";
|
|
||||||
|
|
||||||
interface SwitchProps {
|
|
||||||
value: boolean;
|
|
||||||
onChange: (v: boolean) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Switch(props: SwitchProps) {
|
|
||||||
const { value, onChange } = props;
|
|
||||||
|
|
||||||
const { switchClassName = "" } = useContext(List.ListContext);
|
|
||||||
return (
|
|
||||||
<RadixSwitch.Root
|
|
||||||
checked={value}
|
|
||||||
onCheckedChange={onChange}
|
|
||||||
className={`
|
|
||||||
cursor-pointer flex w-switch h-switch p-0.5 box-content rounded-md transition-colors duration-300 ease-in-out
|
|
||||||
${switchClassName}
|
|
||||||
${
|
|
||||||
value
|
|
||||||
? "bg-switch-checked justify-end"
|
|
||||||
: "bg-switch-unchecked justify-start"
|
|
||||||
}
|
|
||||||
`}
|
|
||||||
>
|
|
||||||
<RadixSwitch.Thumb
|
|
||||||
className={` bg-switch-btn block w-4 h-4 drop-shadow-sm rounded-md`}
|
|
||||||
/>
|
|
||||||
</RadixSwitch.Root>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import ImgDeleteIcon from "@/app/icons/imgDeleteIcon.svg";
|
|
||||||
|
|
||||||
export interface ThumbnailProps {
|
|
||||||
image: string;
|
|
||||||
deleteImage: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Thumbnail(props: ThumbnailProps) {
|
|
||||||
const { image, deleteImage } = props;
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={` h-thumbnail w-thumbnail cursor-default border border-thumbnail rounded-action-btn flex-0 bg-cover bg-center`}
|
|
||||||
style={{ backgroundImage: `url("${image}")` }}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={` w-[100%] h-[100%] opacity-0 transition-all duration-200 rounded-action-btn hover:opacity-100 hover:bg-thumbnail-mask`}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={`cursor-pointer flex items-center justify-center float-right`}
|
|
||||||
onClick={deleteImage}
|
|
||||||
>
|
|
||||||
<ImgDeleteIcon />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
31
app/components/artifacts.module.scss
Normal file
31
app/components/artifacts.module.scss
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
.artifacts {
|
||||||
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
flex-direction: column;
|
||||||
|
&-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
height: 36px;
|
||||||
|
padding: 20px;
|
||||||
|
background: var(--second);
|
||||||
|
}
|
||||||
|
&-title {
|
||||||
|
flex: 1;
|
||||||
|
text-align: center;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 24px;
|
||||||
|
}
|
||||||
|
&-content {
|
||||||
|
flex-grow: 1;
|
||||||
|
padding: 0 20px 20px 20px;
|
||||||
|
background-color: var(--second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.artifacts-iframe {
|
||||||
|
width: 100%;
|
||||||
|
border: var(--border-in-light);
|
||||||
|
border-radius: 6px;
|
||||||
|
background-color: var(--gray);
|
||||||
|
}
|
||||||
267
app/components/artifacts.tsx
Normal file
267
app/components/artifacts.tsx
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
import {
|
||||||
|
useEffect,
|
||||||
|
useState,
|
||||||
|
useRef,
|
||||||
|
useMemo,
|
||||||
|
forwardRef,
|
||||||
|
useImperativeHandle,
|
||||||
|
} from "react";
|
||||||
|
import { useParams } from "react-router";
|
||||||
|
import { useWindowSize } from "@/app/utils";
|
||||||
|
import { IconButton } from "./button";
|
||||||
|
import { nanoid } from "nanoid";
|
||||||
|
import ExportIcon from "../icons/share.svg";
|
||||||
|
import CopyIcon from "../icons/copy.svg";
|
||||||
|
import DownloadIcon from "../icons/download.svg";
|
||||||
|
import GithubIcon from "../icons/github.svg";
|
||||||
|
import LoadingButtonIcon from "../icons/loading.svg";
|
||||||
|
import ReloadButtonIcon from "../icons/reload.svg";
|
||||||
|
import Locale from "../locales";
|
||||||
|
import { Modal, showToast } from "./ui-lib";
|
||||||
|
import { copyToClipboard, downloadAs } from "../utils";
|
||||||
|
import { Path, ApiPath, REPO_URL } from "@/app/constant";
|
||||||
|
import { Loading } from "./home";
|
||||||
|
import styles from "./artifacts.module.scss";
|
||||||
|
|
||||||
|
type HTMLPreviewProps = {
|
||||||
|
code: string;
|
||||||
|
autoHeight?: boolean;
|
||||||
|
height?: number | string;
|
||||||
|
onLoad?: (title?: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type HTMLPreviewHander = {
|
||||||
|
reload: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const HTMLPreview = forwardRef<HTMLPreviewHander, HTMLPreviewProps>(
|
||||||
|
function HTMLPreview(props, ref) {
|
||||||
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||||
|
const [frameId, setFrameId] = useState<string>(nanoid());
|
||||||
|
const [iframeHeight, setIframeHeight] = useState(600);
|
||||||
|
const [title, setTitle] = useState("");
|
||||||
|
/*
|
||||||
|
* https://stackoverflow.com/questions/19739001/what-is-the-difference-between-srcdoc-and-src-datatext-html-in-an
|
||||||
|
* 1. using srcdoc
|
||||||
|
* 2. using src with dataurl:
|
||||||
|
* easy to share
|
||||||
|
* length limit (Data URIs cannot be larger than 32,768 characters.)
|
||||||
|
*/
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleMessage = (e: any) => {
|
||||||
|
const { id, height, title } = e.data;
|
||||||
|
setTitle(title);
|
||||||
|
if (id == frameId) {
|
||||||
|
setIframeHeight(height);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener("message", handleMessage);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("message", handleMessage);
|
||||||
|
};
|
||||||
|
}, [frameId]);
|
||||||
|
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
reload: () => {
|
||||||
|
setFrameId(nanoid());
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
const height = useMemo(() => {
|
||||||
|
if (!props.autoHeight) return props.height || 600;
|
||||||
|
if (typeof props.height === "string") {
|
||||||
|
return props.height;
|
||||||
|
}
|
||||||
|
const parentHeight = props.height || 600;
|
||||||
|
return iframeHeight + 40 > parentHeight
|
||||||
|
? parentHeight
|
||||||
|
: iframeHeight + 40;
|
||||||
|
}, [props.autoHeight, props.height, iframeHeight]);
|
||||||
|
|
||||||
|
const srcDoc = useMemo(() => {
|
||||||
|
const script = `<script>window.addEventListener("DOMContentLoaded", () => new ResizeObserver((entries) => parent.postMessage({id: '${frameId}', height: entries[0].target.clientHeight}, '*')).observe(document.body))</script>`;
|
||||||
|
if (props.code.includes("<!DOCTYPE html>")) {
|
||||||
|
props.code.replace("<!DOCTYPE html>", "<!DOCTYPE html>" + script);
|
||||||
|
}
|
||||||
|
return script + props.code;
|
||||||
|
}, [props.code, frameId]);
|
||||||
|
|
||||||
|
const handleOnLoad = () => {
|
||||||
|
if (props?.onLoad) {
|
||||||
|
props.onLoad(title);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<iframe
|
||||||
|
className={styles["artifacts-iframe"]}
|
||||||
|
key={frameId}
|
||||||
|
ref={iframeRef}
|
||||||
|
sandbox="allow-forms allow-modals allow-scripts"
|
||||||
|
style={{ height }}
|
||||||
|
srcDoc={srcDoc}
|
||||||
|
onLoad={handleOnLoad}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export function ArtifactsShareButton({
|
||||||
|
getCode,
|
||||||
|
id,
|
||||||
|
style,
|
||||||
|
fileName,
|
||||||
|
}: {
|
||||||
|
getCode: () => string;
|
||||||
|
id?: string;
|
||||||
|
style?: any;
|
||||||
|
fileName?: string;
|
||||||
|
}) {
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [name, setName] = useState(id);
|
||||||
|
const [show, setShow] = useState(false);
|
||||||
|
const shareUrl = useMemo(
|
||||||
|
() => [location.origin, "#", Path.Artifacts, "/", name].join(""),
|
||||||
|
[name],
|
||||||
|
);
|
||||||
|
const upload = (code: string) =>
|
||||||
|
id
|
||||||
|
? Promise.resolve({ id })
|
||||||
|
: fetch(ApiPath.Artifacts, {
|
||||||
|
method: "POST",
|
||||||
|
body: code,
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then(({ id }) => {
|
||||||
|
if (id) {
|
||||||
|
return { id };
|
||||||
|
}
|
||||||
|
throw Error();
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
showToast(Locale.Export.Artifacts.Error);
|
||||||
|
});
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="window-action-button" style={style}>
|
||||||
|
<IconButton
|
||||||
|
icon={loading ? <LoadingButtonIcon /> : <ExportIcon />}
|
||||||
|
bordered
|
||||||
|
title={Locale.Export.Artifacts.Title}
|
||||||
|
onClick={() => {
|
||||||
|
if (loading) return;
|
||||||
|
setLoading(true);
|
||||||
|
upload(getCode())
|
||||||
|
.then((res) => {
|
||||||
|
if (res?.id) {
|
||||||
|
setShow(true);
|
||||||
|
setName(res?.id);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{show && (
|
||||||
|
<div className="modal-mask">
|
||||||
|
<Modal
|
||||||
|
title={Locale.Export.Artifacts.Title}
|
||||||
|
onClose={() => setShow(false)}
|
||||||
|
actions={[
|
||||||
|
<IconButton
|
||||||
|
key="download"
|
||||||
|
icon={<DownloadIcon />}
|
||||||
|
bordered
|
||||||
|
text={Locale.Export.Download}
|
||||||
|
onClick={() => {
|
||||||
|
downloadAs(getCode(), `${fileName || name}.html`).then(() =>
|
||||||
|
setShow(false),
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
/>,
|
||||||
|
<IconButton
|
||||||
|
key="copy"
|
||||||
|
icon={<CopyIcon />}
|
||||||
|
bordered
|
||||||
|
text={Locale.Chat.Actions.Copy}
|
||||||
|
onClick={() => {
|
||||||
|
copyToClipboard(shareUrl).then(() => setShow(false));
|
||||||
|
}}
|
||||||
|
/>,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<a target="_blank" href={shareUrl}>
|
||||||
|
{shareUrl}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Artifacts() {
|
||||||
|
const { id } = useParams();
|
||||||
|
const [code, setCode] = useState("");
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [fileName, setFileName] = useState("");
|
||||||
|
const previewRef = useRef<HTMLPreviewHander>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (id) {
|
||||||
|
fetch(`${ApiPath.Artifacts}?id=${id}`)
|
||||||
|
.then((res) => {
|
||||||
|
if (res.status > 300) {
|
||||||
|
throw Error("can not get content");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.then((res) => res.text())
|
||||||
|
.then(setCode)
|
||||||
|
.catch((e) => {
|
||||||
|
showToast(Locale.Export.Artifacts.Error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={styles["artifacts"]}>
|
||||||
|
<div className={styles["artifacts-header"]}>
|
||||||
|
<a href={REPO_URL} target="_blank" rel="noopener noreferrer">
|
||||||
|
<IconButton bordered icon={<GithubIcon />} shadow />
|
||||||
|
</a>
|
||||||
|
<IconButton
|
||||||
|
bordered
|
||||||
|
style={{ marginLeft: 20 }}
|
||||||
|
icon={<ReloadButtonIcon />}
|
||||||
|
shadow
|
||||||
|
onClick={() => previewRef.current?.reload()}
|
||||||
|
/>
|
||||||
|
<div className={styles["artifacts-title"]}>NextChat Artifacts</div>
|
||||||
|
<ArtifactsShareButton
|
||||||
|
id={id}
|
||||||
|
getCode={() => code}
|
||||||
|
fileName={fileName}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className={styles["artifacts-content"]}>
|
||||||
|
{loading && <Loading />}
|
||||||
|
{code && (
|
||||||
|
<HTMLPreview
|
||||||
|
code={code}
|
||||||
|
ref={previewRef}
|
||||||
|
autoHeight={false}
|
||||||
|
height={"100%"}
|
||||||
|
onLoad={(title) => {
|
||||||
|
setFileName(title as string);
|
||||||
|
setLoading(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,8 +6,6 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
background-color: var(--white);
|
|
||||||
|
|
||||||
.auth-logo {
|
.auth-logo {
|
||||||
transform: scale(1.4);
|
transform: scale(1.4);
|
||||||
}
|
}
|
||||||
@@ -35,18 +33,4 @@
|
|||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
input[type="number"],
|
|
||||||
input[type="text"],
|
|
||||||
input[type="password"] {
|
|
||||||
appearance: none;
|
|
||||||
border-radius: 10px;
|
|
||||||
border: var(--border-in-light);
|
|
||||||
min-height: 36px;
|
|
||||||
box-sizing: border-box;
|
|
||||||
background: var(--white);
|
|
||||||
color: var(--black);
|
|
||||||
padding: 0 10px;
|
|
||||||
max-width: 50%;
|
|
||||||
font-family: inherit;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
|
||||||
import styles from "./button.module.scss";
|
import styles from "./button.module.scss";
|
||||||
|
import { CSSProperties } from "react";
|
||||||
|
|
||||||
export type ButtonType = "primary" | "danger" | null;
|
export type ButtonType = "primary" | "danger" | null;
|
||||||
|
|
||||||
@@ -16,6 +17,8 @@ export function IconButton(props: {
|
|||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
tabIndex?: number;
|
tabIndex?: number;
|
||||||
autoFocus?: boolean;
|
autoFocus?: boolean;
|
||||||
|
style?: CSSProperties;
|
||||||
|
aria?: string;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
@@ -31,9 +34,12 @@ export function IconButton(props: {
|
|||||||
role="button"
|
role="button"
|
||||||
tabIndex={props.tabIndex}
|
tabIndex={props.tabIndex}
|
||||||
autoFocus={props.autoFocus}
|
autoFocus={props.autoFocus}
|
||||||
|
style={props.style}
|
||||||
|
aria-label={props.aria}
|
||||||
>
|
>
|
||||||
{props.icon && (
|
{props.icon && (
|
||||||
<div
|
<div
|
||||||
|
aria-label={props.text || props.title}
|
||||||
className={
|
className={
|
||||||
styles["icon-button-icon"] +
|
styles["icon-button-icon"] +
|
||||||
` ${props.type === "primary" && "no-dark"}`
|
` ${props.type === "primary" && "no-dark"}`
|
||||||
@@ -44,7 +50,12 @@ export function IconButton(props: {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{props.text && (
|
{props.text && (
|
||||||
<div className={styles["icon-button-text"]}>{props.text}</div>
|
<div
|
||||||
|
aria-label={props.text || props.title}
|
||||||
|
className={styles["icon-button-text"]}
|
||||||
|
>
|
||||||
|
{props.text}
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -346,6 +346,12 @@
|
|||||||
flex-wrap: nowrap;
|
flex-wrap: nowrap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chat-model-name {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--black);
|
||||||
|
margin-left: 6px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.chat-message-container {
|
.chat-message-container {
|
||||||
@@ -407,6 +413,21 @@
|
|||||||
margin-top: 5px;
|
margin-top: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chat-message-tools {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #aaa;
|
||||||
|
line-height: 1.5;
|
||||||
|
margin-top: 5px;
|
||||||
|
.chat-message-tool {
|
||||||
|
display: flex;
|
||||||
|
align-items: end;
|
||||||
|
svg {
|
||||||
|
margin-left: 5px;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.chat-message-item {
|
.chat-message-item {
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
@@ -625,3 +646,51 @@
|
|||||||
bottom: 30px;
|
bottom: 30px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.shortcut-key-container {
|
||||||
|
padding: 10px;
|
||||||
|
overflow-y: auto;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key-item {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: var(--white);
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key-title {
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--black);
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key-keys {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
border: var(--border-in-light);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 4px;
|
||||||
|
background-color: var(--gray);
|
||||||
|
min-width: 32px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcut-key span {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--black);
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import DeleteIcon from "../icons/clear.svg";
|
|||||||
import PinIcon from "../icons/pin.svg";
|
import PinIcon from "../icons/pin.svg";
|
||||||
import EditIcon from "../icons/rename.svg";
|
import EditIcon from "../icons/rename.svg";
|
||||||
import ConfirmIcon from "../icons/confirm.svg";
|
import ConfirmIcon from "../icons/confirm.svg";
|
||||||
|
import CloseIcon from "../icons/close.svg";
|
||||||
import CancelIcon from "../icons/cancel.svg";
|
import CancelIcon from "../icons/cancel.svg";
|
||||||
import ImageIcon from "../icons/image.svg";
|
import ImageIcon from "../icons/image.svg";
|
||||||
|
|
||||||
@@ -37,6 +38,11 @@ import AutoIcon from "../icons/auto.svg";
|
|||||||
import BottomIcon from "../icons/bottom.svg";
|
import BottomIcon from "../icons/bottom.svg";
|
||||||
import StopIcon from "../icons/pause.svg";
|
import StopIcon from "../icons/pause.svg";
|
||||||
import RobotIcon from "../icons/robot.svg";
|
import RobotIcon from "../icons/robot.svg";
|
||||||
|
import SizeIcon from "../icons/size.svg";
|
||||||
|
import QualityIcon from "../icons/hd.svg";
|
||||||
|
import StyleIcon from "../icons/palette.svg";
|
||||||
|
import PluginIcon from "../icons/plugin.svg";
|
||||||
|
import ShortcutkeyIcon from "../icons/shortcutkey.svg";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ChatMessage,
|
ChatMessage,
|
||||||
@@ -49,6 +55,7 @@ import {
|
|||||||
useAppConfig,
|
useAppConfig,
|
||||||
DEFAULT_TOPIC,
|
DEFAULT_TOPIC,
|
||||||
ModelType,
|
ModelType,
|
||||||
|
usePluginStore,
|
||||||
} from "../store";
|
} from "../store";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -59,13 +66,17 @@ import {
|
|||||||
getMessageTextContent,
|
getMessageTextContent,
|
||||||
getMessageImages,
|
getMessageImages,
|
||||||
isVisionModel,
|
isVisionModel,
|
||||||
|
isDalle3,
|
||||||
|
showPlugins,
|
||||||
|
safeLocalStorage,
|
||||||
} from "../utils";
|
} from "../utils";
|
||||||
|
|
||||||
import { compressImage } from "@/app/utils/chat";
|
import { uploadImage as uploadImageRemote } from "@/app/utils/chat";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
|
|
||||||
import { ChatControllerPool } from "../client/controller";
|
import { ChatControllerPool } from "../client/controller";
|
||||||
|
import { DalleSize, DalleQuality, DalleStyle } from "../typing";
|
||||||
import { Prompt, usePromptStore } from "../store/prompt";
|
import { Prompt, usePromptStore } from "../store/prompt";
|
||||||
import Locale from "../locales";
|
import Locale from "../locales";
|
||||||
|
|
||||||
@@ -88,6 +99,7 @@ import {
|
|||||||
Path,
|
Path,
|
||||||
REQUEST_TIMEOUT_MS,
|
REQUEST_TIMEOUT_MS,
|
||||||
UNFINISHED_INPUT,
|
UNFINISHED_INPUT,
|
||||||
|
ServiceProvider,
|
||||||
} from "../constant";
|
} from "../constant";
|
||||||
import { Avatar } from "./emoji";
|
import { Avatar } from "./emoji";
|
||||||
import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
|
import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
|
||||||
@@ -99,6 +111,8 @@ import { getClientConfig } from "../config/client";
|
|||||||
import { useAllModels } from "../utils/hooks";
|
import { useAllModels } from "../utils/hooks";
|
||||||
import { MultimodalContent } from "../client/api";
|
import { MultimodalContent } from "../client/api";
|
||||||
|
|
||||||
|
const localStorage = safeLocalStorage();
|
||||||
|
|
||||||
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
||||||
loading: () => <LoadingIcon />,
|
loading: () => <LoadingIcon />,
|
||||||
});
|
});
|
||||||
@@ -178,7 +192,7 @@ function PromptToast(props: {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles["prompt-toast"]} key="prompt-toast">
|
<div className={styles["prompt-toast"]} key="prompt-toast">
|
||||||
{props.showToast && (
|
{props.showToast && context.length > 0 && (
|
||||||
<div
|
<div
|
||||||
className={styles["prompt-toast-inner"] + " clickable"}
|
className={styles["prompt-toast-inner"] + " clickable"}
|
||||||
role="button"
|
role="button"
|
||||||
@@ -244,11 +258,11 @@ function useSubmitHandler() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RenderPompt = Pick<Prompt, "title" | "content">;
|
export type RenderPrompt = Pick<Prompt, "title" | "content">;
|
||||||
|
|
||||||
export function PromptHints(props: {
|
export function PromptHints(props: {
|
||||||
prompts: RenderPompt[];
|
prompts: RenderPrompt[];
|
||||||
onPromptSelect: (prompt: RenderPompt) => void;
|
onPromptSelect: (prompt: RenderPrompt) => void;
|
||||||
}) {
|
}) {
|
||||||
const noPrompts = props.prompts.length === 0;
|
const noPrompts = props.prompts.length === 0;
|
||||||
const [selectIndex, setSelectIndex] = useState(0);
|
const [selectIndex, setSelectIndex] = useState(0);
|
||||||
@@ -337,7 +351,7 @@ function ClearContextDivider() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ChatAction(props: {
|
export function ChatAction(props: {
|
||||||
text: string;
|
text: string;
|
||||||
icon: JSX.Element;
|
icon: JSX.Element;
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
@@ -427,10 +441,12 @@ export function ChatActions(props: {
|
|||||||
showPromptHints: () => void;
|
showPromptHints: () => void;
|
||||||
hitBottom: boolean;
|
hitBottom: boolean;
|
||||||
uploading: boolean;
|
uploading: boolean;
|
||||||
|
setShowShortcutKeyModal: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
}) {
|
}) {
|
||||||
const config = useAppConfig();
|
const config = useAppConfig();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const chatStore = useChatStore();
|
const chatStore = useChatStore();
|
||||||
|
const pluginStore = usePluginStore();
|
||||||
|
|
||||||
// switch themes
|
// switch themes
|
||||||
const theme = config.theme;
|
const theme = config.theme;
|
||||||
@@ -448,6 +464,9 @@ export function ChatActions(props: {
|
|||||||
|
|
||||||
// switch model
|
// switch model
|
||||||
const currentModel = chatStore.currentSession().mask.modelConfig.model;
|
const currentModel = chatStore.currentSession().mask.modelConfig.model;
|
||||||
|
const currentProviderName =
|
||||||
|
chatStore.currentSession().mask.modelConfig?.providerName ||
|
||||||
|
ServiceProvider.OpenAI;
|
||||||
const allModels = useAllModels();
|
const allModels = useAllModels();
|
||||||
const models = useMemo(() => {
|
const models = useMemo(() => {
|
||||||
const filteredModels = allModels.filter((m) => m.available);
|
const filteredModels = allModels.filter((m) => m.available);
|
||||||
@@ -463,9 +482,33 @@ export function ChatActions(props: {
|
|||||||
return filteredModels;
|
return filteredModels;
|
||||||
}
|
}
|
||||||
}, [allModels]);
|
}, [allModels]);
|
||||||
|
const currentModelName = useMemo(() => {
|
||||||
|
const model = models.find(
|
||||||
|
(m) =>
|
||||||
|
m.name == currentModel &&
|
||||||
|
m?.provider?.providerName == currentProviderName,
|
||||||
|
);
|
||||||
|
return model?.displayName ?? "";
|
||||||
|
}, [models, currentModel, currentProviderName]);
|
||||||
const [showModelSelector, setShowModelSelector] = useState(false);
|
const [showModelSelector, setShowModelSelector] = useState(false);
|
||||||
|
const [showPluginSelector, setShowPluginSelector] = useState(false);
|
||||||
const [showUploadImage, setShowUploadImage] = useState(false);
|
const [showUploadImage, setShowUploadImage] = useState(false);
|
||||||
|
|
||||||
|
const [showSizeSelector, setShowSizeSelector] = useState(false);
|
||||||
|
const [showQualitySelector, setShowQualitySelector] = useState(false);
|
||||||
|
const [showStyleSelector, setShowStyleSelector] = useState(false);
|
||||||
|
const dalle3Sizes: DalleSize[] = ["1024x1024", "1792x1024", "1024x1792"];
|
||||||
|
const dalle3Qualitys: DalleQuality[] = ["standard", "hd"];
|
||||||
|
const dalle3Styles: DalleStyle[] = ["vivid", "natural"];
|
||||||
|
const currentSize =
|
||||||
|
chatStore.currentSession().mask.modelConfig?.size ?? "1024x1024";
|
||||||
|
const currentQuality =
|
||||||
|
chatStore.currentSession().mask.modelConfig?.quality ?? "standard";
|
||||||
|
const currentStyle =
|
||||||
|
chatStore.currentSession().mask.modelConfig?.style ?? "vivid";
|
||||||
|
|
||||||
|
const isMobileScreen = useMobileScreen();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const show = isVisionModel(currentModel);
|
const show = isVisionModel(currentModel);
|
||||||
setShowUploadImage(show);
|
setShowUploadImage(show);
|
||||||
@@ -479,13 +522,17 @@ export function ChatActions(props: {
|
|||||||
const isUnavaliableModel = !models.some((m) => m.name === currentModel);
|
const isUnavaliableModel = !models.some((m) => m.name === currentModel);
|
||||||
if (isUnavaliableModel && models.length > 0) {
|
if (isUnavaliableModel && models.length > 0) {
|
||||||
// show next model to default model if exist
|
// show next model to default model if exist
|
||||||
let nextModel: ModelType = (
|
let nextModel = models.find((model) => model.isDefault) || models[0];
|
||||||
models.find((model) => model.isDefault) || models[0]
|
chatStore.updateCurrentSession((session) => {
|
||||||
).name;
|
session.mask.modelConfig.model = nextModel.name;
|
||||||
chatStore.updateCurrentSession(
|
session.mask.modelConfig.providerName = nextModel?.provider
|
||||||
(session) => (session.mask.modelConfig.model = nextModel),
|
?.providerName as ServiceProvider;
|
||||||
|
});
|
||||||
|
showToast(
|
||||||
|
nextModel?.provider?.providerName == "ByteDance"
|
||||||
|
? nextModel.displayName
|
||||||
|
: nextModel.name,
|
||||||
);
|
);
|
||||||
showToast(nextModel);
|
|
||||||
}
|
}
|
||||||
}, [chatStore, currentModel, models]);
|
}, [chatStore, currentModel, models]);
|
||||||
|
|
||||||
@@ -567,28 +614,162 @@ export function ChatActions(props: {
|
|||||||
|
|
||||||
<ChatAction
|
<ChatAction
|
||||||
onClick={() => setShowModelSelector(true)}
|
onClick={() => setShowModelSelector(true)}
|
||||||
text={currentModel}
|
text={currentModelName}
|
||||||
icon={<RobotIcon />}
|
icon={<RobotIcon />}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{showModelSelector && (
|
{showModelSelector && (
|
||||||
<Selector
|
<Selector
|
||||||
defaultSelectedValue={currentModel}
|
defaultSelectedValue={`${currentModel}@${currentProviderName}`}
|
||||||
items={models.map((m) => ({
|
items={models.map((m) => ({
|
||||||
title: m.displayName,
|
title: `${m.displayName}${
|
||||||
value: m.name,
|
m?.provider?.providerName
|
||||||
|
? "(" + m?.provider?.providerName + ")"
|
||||||
|
: ""
|
||||||
|
}`,
|
||||||
|
value: `${m.name}@${m?.provider?.providerName}`,
|
||||||
}))}
|
}))}
|
||||||
onClose={() => setShowModelSelector(false)}
|
onClose={() => setShowModelSelector(false)}
|
||||||
onSelection={(s) => {
|
onSelection={(s) => {
|
||||||
if (s.length === 0) return;
|
if (s.length === 0) return;
|
||||||
|
const [model, providerName] = s[0].split("@");
|
||||||
chatStore.updateCurrentSession((session) => {
|
chatStore.updateCurrentSession((session) => {
|
||||||
session.mask.modelConfig.model = s[0] as ModelType;
|
session.mask.modelConfig.model = model as ModelType;
|
||||||
|
session.mask.modelConfig.providerName =
|
||||||
|
providerName as ServiceProvider;
|
||||||
session.mask.syncGlobalConfig = false;
|
session.mask.syncGlobalConfig = false;
|
||||||
});
|
});
|
||||||
showToast(s[0]);
|
if (providerName == "ByteDance") {
|
||||||
|
const selectedModel = models.find(
|
||||||
|
(m) =>
|
||||||
|
m.name == model && m?.provider?.providerName == providerName,
|
||||||
|
);
|
||||||
|
showToast(selectedModel?.displayName ?? "");
|
||||||
|
} else {
|
||||||
|
showToast(model);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{isDalle3(currentModel) && (
|
||||||
|
<ChatAction
|
||||||
|
onClick={() => setShowSizeSelector(true)}
|
||||||
|
text={currentSize}
|
||||||
|
icon={<SizeIcon />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showSizeSelector && (
|
||||||
|
<Selector
|
||||||
|
defaultSelectedValue={currentSize}
|
||||||
|
items={dalle3Sizes.map((m) => ({
|
||||||
|
title: m,
|
||||||
|
value: m,
|
||||||
|
}))}
|
||||||
|
onClose={() => setShowSizeSelector(false)}
|
||||||
|
onSelection={(s) => {
|
||||||
|
if (s.length === 0) return;
|
||||||
|
const size = s[0];
|
||||||
|
chatStore.updateCurrentSession((session) => {
|
||||||
|
session.mask.modelConfig.size = size;
|
||||||
|
});
|
||||||
|
showToast(size);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isDalle3(currentModel) && (
|
||||||
|
<ChatAction
|
||||||
|
onClick={() => setShowQualitySelector(true)}
|
||||||
|
text={currentQuality}
|
||||||
|
icon={<QualityIcon />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showQualitySelector && (
|
||||||
|
<Selector
|
||||||
|
defaultSelectedValue={currentQuality}
|
||||||
|
items={dalle3Qualitys.map((m) => ({
|
||||||
|
title: m,
|
||||||
|
value: m,
|
||||||
|
}))}
|
||||||
|
onClose={() => setShowQualitySelector(false)}
|
||||||
|
onSelection={(q) => {
|
||||||
|
if (q.length === 0) return;
|
||||||
|
const quality = q[0];
|
||||||
|
chatStore.updateCurrentSession((session) => {
|
||||||
|
session.mask.modelConfig.quality = quality;
|
||||||
|
});
|
||||||
|
showToast(quality);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isDalle3(currentModel) && (
|
||||||
|
<ChatAction
|
||||||
|
onClick={() => setShowStyleSelector(true)}
|
||||||
|
text={currentStyle}
|
||||||
|
icon={<StyleIcon />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showStyleSelector && (
|
||||||
|
<Selector
|
||||||
|
defaultSelectedValue={currentStyle}
|
||||||
|
items={dalle3Styles.map((m) => ({
|
||||||
|
title: m,
|
||||||
|
value: m,
|
||||||
|
}))}
|
||||||
|
onClose={() => setShowStyleSelector(false)}
|
||||||
|
onSelection={(s) => {
|
||||||
|
if (s.length === 0) return;
|
||||||
|
const style = s[0];
|
||||||
|
chatStore.updateCurrentSession((session) => {
|
||||||
|
session.mask.modelConfig.style = style;
|
||||||
|
});
|
||||||
|
showToast(style);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{showPlugins(currentProviderName, currentModel) && (
|
||||||
|
<ChatAction
|
||||||
|
onClick={() => {
|
||||||
|
if (pluginStore.getAll().length == 0) {
|
||||||
|
navigate(Path.Plugins);
|
||||||
|
} else {
|
||||||
|
setShowPluginSelector(true);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
text={Locale.Plugin.Name}
|
||||||
|
icon={<PluginIcon />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showPluginSelector && (
|
||||||
|
<Selector
|
||||||
|
multiple
|
||||||
|
defaultSelectedValue={chatStore.currentSession().mask?.plugin}
|
||||||
|
items={pluginStore.getAll().map((item) => ({
|
||||||
|
title: `${item?.title}@${item?.version}`,
|
||||||
|
value: item?.id,
|
||||||
|
}))}
|
||||||
|
onClose={() => setShowPluginSelector(false)}
|
||||||
|
onSelection={(s) => {
|
||||||
|
chatStore.updateCurrentSession((session) => {
|
||||||
|
session.mask.plugin = s as string[];
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!isMobileScreen && (
|
||||||
|
<ChatAction
|
||||||
|
onClick={() => props.setShowShortcutKeyModal(true)}
|
||||||
|
text={Locale.Chat.ShortcutKey.Title}
|
||||||
|
icon={<ShortcutkeyIcon />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -663,6 +844,67 @@ export function DeleteImageButton(props: { deleteImage: () => void }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ShortcutKeyModal(props: { onClose: () => void }) {
|
||||||
|
const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
|
||||||
|
const shortcuts = [
|
||||||
|
{
|
||||||
|
title: Locale.Chat.ShortcutKey.newChat,
|
||||||
|
keys: isMac ? ["⌘", "Shift", "O"] : ["Ctrl", "Shift", "O"],
|
||||||
|
},
|
||||||
|
{ title: Locale.Chat.ShortcutKey.focusInput, keys: ["Shift", "Esc"] },
|
||||||
|
{
|
||||||
|
title: Locale.Chat.ShortcutKey.copyLastCode,
|
||||||
|
keys: isMac ? ["⌘", "Shift", ";"] : ["Ctrl", "Shift", ";"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: Locale.Chat.ShortcutKey.copyLastMessage,
|
||||||
|
keys: isMac ? ["⌘", "Shift", "C"] : ["Ctrl", "Shift", "C"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: Locale.Chat.ShortcutKey.showShortcutKey,
|
||||||
|
keys: isMac ? ["⌘", "/"] : ["Ctrl", "/"],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
return (
|
||||||
|
<div className="modal-mask">
|
||||||
|
<Modal
|
||||||
|
title={Locale.Chat.ShortcutKey.Title}
|
||||||
|
onClose={props.onClose}
|
||||||
|
actions={[
|
||||||
|
<IconButton
|
||||||
|
type="primary"
|
||||||
|
text={Locale.UI.Confirm}
|
||||||
|
icon={<ConfirmIcon />}
|
||||||
|
key="ok"
|
||||||
|
onClick={() => {
|
||||||
|
props.onClose();
|
||||||
|
}}
|
||||||
|
/>,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<div className={styles["shortcut-key-container"]}>
|
||||||
|
<div className={styles["shortcut-key-grid"]}>
|
||||||
|
{shortcuts.map((shortcut, index) => (
|
||||||
|
<div key={index} className={styles["shortcut-key-item"]}>
|
||||||
|
<div className={styles["shortcut-key-title"]}>
|
||||||
|
{shortcut.title}
|
||||||
|
</div>
|
||||||
|
<div className={styles["shortcut-key-keys"]}>
|
||||||
|
{shortcut.keys.map((key, i) => (
|
||||||
|
<div key={i} className={styles["shortcut-key"]}>
|
||||||
|
<span>{key}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function _Chat() {
|
function _Chat() {
|
||||||
type RenderMessage = ChatMessage & { preview?: boolean };
|
type RenderMessage = ChatMessage & { preview?: boolean };
|
||||||
|
|
||||||
@@ -670,6 +912,7 @@ function _Chat() {
|
|||||||
const session = chatStore.currentSession();
|
const session = chatStore.currentSession();
|
||||||
const config = useAppConfig();
|
const config = useAppConfig();
|
||||||
const fontSize = config.fontSize;
|
const fontSize = config.fontSize;
|
||||||
|
const fontFamily = config.fontFamily;
|
||||||
|
|
||||||
const [showExport, setShowExport] = useState(false);
|
const [showExport, setShowExport] = useState(false);
|
||||||
|
|
||||||
@@ -696,7 +939,7 @@ function _Chat() {
|
|||||||
|
|
||||||
// prompt hints
|
// prompt hints
|
||||||
const promptStore = usePromptStore();
|
const promptStore = usePromptStore();
|
||||||
const [promptHints, setPromptHints] = useState<RenderPompt[]>([]);
|
const [promptHints, setPromptHints] = useState<RenderPrompt[]>([]);
|
||||||
const onSearch = useDebouncedCallback(
|
const onSearch = useDebouncedCallback(
|
||||||
(text: string) => {
|
(text: string) => {
|
||||||
const matchedPrompts = promptStore.search(text);
|
const matchedPrompts = promptStore.search(text);
|
||||||
@@ -749,7 +992,7 @@ function _Chat() {
|
|||||||
// clear search results
|
// clear search results
|
||||||
if (n === 0) {
|
if (n === 0) {
|
||||||
setPromptHints([]);
|
setPromptHints([]);
|
||||||
} else if (text.startsWith(ChatCommandPrefix)) {
|
} else if (text.match(ChatCommandPrefix)) {
|
||||||
setPromptHints(chatCommands.search(text));
|
setPromptHints(chatCommands.search(text));
|
||||||
} else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
|
} else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
|
||||||
// check if need to trigger auto completion
|
// check if need to trigger auto completion
|
||||||
@@ -774,14 +1017,14 @@ function _Chat() {
|
|||||||
.onUserInput(userInput, attachImages)
|
.onUserInput(userInput, attachImages)
|
||||||
.then(() => setIsLoading(false));
|
.then(() => setIsLoading(false));
|
||||||
setAttachImages([]);
|
setAttachImages([]);
|
||||||
localStorage.setItem(LAST_INPUT_KEY, userInput);
|
chatStore.setLastInput(userInput);
|
||||||
setUserInput("");
|
setUserInput("");
|
||||||
setPromptHints([]);
|
setPromptHints([]);
|
||||||
if (!isMobileScreen) inputRef.current?.focus();
|
if (!isMobileScreen) inputRef.current?.focus();
|
||||||
setAutoScroll(true);
|
setAutoScroll(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onPromptSelect = (prompt: RenderPompt) => {
|
const onPromptSelect = (prompt: RenderPrompt) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setPromptHints([]);
|
setPromptHints([]);
|
||||||
|
|
||||||
@@ -840,7 +1083,7 @@ function _Chat() {
|
|||||||
userInput.length <= 0 &&
|
userInput.length <= 0 &&
|
||||||
!(e.metaKey || e.altKey || e.ctrlKey)
|
!(e.metaKey || e.altKey || e.ctrlKey)
|
||||||
) {
|
) {
|
||||||
setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
|
setUserInput(chatStore.lastInput ?? "");
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1136,7 +1379,7 @@ function _Chat() {
|
|||||||
...(await new Promise<string[]>((res, rej) => {
|
...(await new Promise<string[]>((res, rej) => {
|
||||||
setUploading(true);
|
setUploading(true);
|
||||||
const imagesData: string[] = [];
|
const imagesData: string[] = [];
|
||||||
compressImage(file, 256 * 1024)
|
uploadImageRemote(file)
|
||||||
.then((dataUrl) => {
|
.then((dataUrl) => {
|
||||||
imagesData.push(dataUrl);
|
imagesData.push(dataUrl);
|
||||||
setUploading(false);
|
setUploading(false);
|
||||||
@@ -1178,7 +1421,7 @@ function _Chat() {
|
|||||||
const imagesData: string[] = [];
|
const imagesData: string[] = [];
|
||||||
for (let i = 0; i < files.length; i++) {
|
for (let i = 0; i < files.length; i++) {
|
||||||
const file = event.target.files[i];
|
const file = event.target.files[i];
|
||||||
compressImage(file, 256 * 1024)
|
uploadImageRemote(file)
|
||||||
.then((dataUrl) => {
|
.then((dataUrl) => {
|
||||||
imagesData.push(dataUrl);
|
imagesData.push(dataUrl);
|
||||||
if (
|
if (
|
||||||
@@ -1206,6 +1449,70 @@ function _Chat() {
|
|||||||
setAttachImages(images);
|
setAttachImages(images);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 快捷键 shortcut keys
|
||||||
|
const [showShortcutKeyModal, setShowShortcutKeyModal] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: any) => {
|
||||||
|
// 打开新聊天 command + shift + o
|
||||||
|
if (
|
||||||
|
(event.metaKey || event.ctrlKey) &&
|
||||||
|
event.shiftKey &&
|
||||||
|
event.key.toLowerCase() === "o"
|
||||||
|
) {
|
||||||
|
event.preventDefault();
|
||||||
|
setTimeout(() => {
|
||||||
|
chatStore.newSession();
|
||||||
|
navigate(Path.Chat);
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
// 聚焦聊天输入 shift + esc
|
||||||
|
else if (event.shiftKey && event.key.toLowerCase() === "escape") {
|
||||||
|
event.preventDefault();
|
||||||
|
inputRef.current?.focus();
|
||||||
|
}
|
||||||
|
// 复制最后一个代码块 command + shift + ;
|
||||||
|
else if (
|
||||||
|
(event.metaKey || event.ctrlKey) &&
|
||||||
|
event.shiftKey &&
|
||||||
|
event.code === "Semicolon"
|
||||||
|
) {
|
||||||
|
event.preventDefault();
|
||||||
|
const copyCodeButton =
|
||||||
|
document.querySelectorAll<HTMLElement>(".copy-code-button");
|
||||||
|
if (copyCodeButton.length > 0) {
|
||||||
|
copyCodeButton[copyCodeButton.length - 1].click();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 复制最后一个回复 command + shift + c
|
||||||
|
else if (
|
||||||
|
(event.metaKey || event.ctrlKey) &&
|
||||||
|
event.shiftKey &&
|
||||||
|
event.key.toLowerCase() === "c"
|
||||||
|
) {
|
||||||
|
event.preventDefault();
|
||||||
|
const lastNonUserMessage = messages
|
||||||
|
.filter((message) => message.role !== "user")
|
||||||
|
.pop();
|
||||||
|
if (lastNonUserMessage) {
|
||||||
|
const lastMessageContent = getMessageTextContent(lastNonUserMessage);
|
||||||
|
copyToClipboard(lastMessageContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 展示快捷键 command + /
|
||||||
|
else if ((event.metaKey || event.ctrlKey) && event.key === "/") {
|
||||||
|
event.preventDefault();
|
||||||
|
setShowShortcutKeyModal(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", handleKeyDown);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("keydown", handleKeyDown);
|
||||||
|
};
|
||||||
|
}, [messages, chatStore, navigate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.chat} key={session.id}>
|
<div className={styles.chat} key={session.id}>
|
||||||
<div className="window-header" data-tauri-drag-region>
|
<div className="window-header" data-tauri-drag-region>
|
||||||
@@ -1239,6 +1546,8 @@ function _Chat() {
|
|||||||
<IconButton
|
<IconButton
|
||||||
icon={<RenameIcon />}
|
icon={<RenameIcon />}
|
||||||
bordered
|
bordered
|
||||||
|
title={Locale.Chat.EditMessage.Title}
|
||||||
|
aria={Locale.Chat.EditMessage.Title}
|
||||||
onClick={() => setIsEditingMessage(true)}
|
onClick={() => setIsEditingMessage(true)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -1258,6 +1567,8 @@ function _Chat() {
|
|||||||
<IconButton
|
<IconButton
|
||||||
icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
|
icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
|
||||||
bordered
|
bordered
|
||||||
|
title={Locale.Chat.Actions.FullScreen}
|
||||||
|
aria={Locale.Chat.Actions.FullScreen}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
config.update(
|
config.update(
|
||||||
(config) => (config.tightBorder = !config.tightBorder),
|
(config) => (config.tightBorder = !config.tightBorder),
|
||||||
@@ -1309,6 +1620,7 @@ function _Chat() {
|
|||||||
<div className={styles["chat-message-edit"]}>
|
<div className={styles["chat-message-edit"]}>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon={<EditIcon />}
|
icon={<EditIcon />}
|
||||||
|
aria={Locale.Chat.Actions.Edit}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const newMessage = await showPrompt(
|
const newMessage = await showPrompt(
|
||||||
Locale.Chat.Actions.Edit,
|
Locale.Chat.Actions.Edit,
|
||||||
@@ -1357,6 +1669,11 @@ function _Chat() {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{!isUser && (
|
||||||
|
<div className={styles["chat-model-name"]}>
|
||||||
|
{message.model}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{showActions && (
|
{showActions && (
|
||||||
<div className={styles["chat-message-actions"]}>
|
<div className={styles["chat-message-actions"]}>
|
||||||
@@ -1401,25 +1718,47 @@ function _Chat() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{showTyping && (
|
{message?.tools?.length == 0 && showTyping && (
|
||||||
<div className={styles["chat-message-status"]}>
|
<div className={styles["chat-message-status"]}>
|
||||||
{Locale.Chat.Typing}
|
{Locale.Chat.Typing}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{/*@ts-ignore*/}
|
||||||
|
{message?.tools?.length > 0 && (
|
||||||
|
<div className={styles["chat-message-tools"]}>
|
||||||
|
{message?.tools?.map((tool) => (
|
||||||
|
<div
|
||||||
|
key={tool.id}
|
||||||
|
className={styles["chat-message-tool"]}
|
||||||
|
>
|
||||||
|
{tool.isError === false ? (
|
||||||
|
<ConfirmIcon />
|
||||||
|
) : tool.isError === true ? (
|
||||||
|
<CloseIcon />
|
||||||
|
) : (
|
||||||
|
<LoadingButtonIcon />
|
||||||
|
)}
|
||||||
|
<span>{tool?.function?.name}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className={styles["chat-message-item"]}>
|
<div className={styles["chat-message-item"]}>
|
||||||
<Markdown
|
<Markdown
|
||||||
|
key={message.streaming ? "loading" : "done"}
|
||||||
content={getMessageTextContent(message)}
|
content={getMessageTextContent(message)}
|
||||||
loading={
|
loading={
|
||||||
(message.preview || message.streaming) &&
|
(message.preview || message.streaming) &&
|
||||||
message.content.length === 0 &&
|
message.content.length === 0 &&
|
||||||
!isUser
|
!isUser
|
||||||
}
|
}
|
||||||
onContextMenu={(e) => onRightClick(e, message)}
|
// onContextMenu={(e) => onRightClick(e, message)} // hard to use
|
||||||
onDoubleClickCapture={() => {
|
onDoubleClickCapture={() => {
|
||||||
if (!isMobileScreen) return;
|
if (!isMobileScreen) return;
|
||||||
setUserInput(getMessageTextContent(message));
|
setUserInput(getMessageTextContent(message));
|
||||||
}}
|
}}
|
||||||
fontSize={fontSize}
|
fontSize={fontSize}
|
||||||
|
fontFamily={fontFamily}
|
||||||
parentRef={scrollRef}
|
parentRef={scrollRef}
|
||||||
defaultShow={i >= messages.length - 6}
|
defaultShow={i >= messages.length - 6}
|
||||||
/>
|
/>
|
||||||
@@ -1490,6 +1829,7 @@ function _Chat() {
|
|||||||
setUserInput("/");
|
setUserInput("/");
|
||||||
onSearch("");
|
onSearch("");
|
||||||
}}
|
}}
|
||||||
|
setShowShortcutKeyModal={setShowShortcutKeyModal}
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
className={`${styles["chat-input-panel-inner"]} ${
|
className={`${styles["chat-input-panel-inner"]} ${
|
||||||
@@ -1514,6 +1854,7 @@ function _Chat() {
|
|||||||
autoFocus={autoFocus}
|
autoFocus={autoFocus}
|
||||||
style={{
|
style={{
|
||||||
fontSize: config.fontSize,
|
fontSize: config.fontSize,
|
||||||
|
fontFamily: config.fontFamily,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{attachImages.length != 0 && (
|
{attachImages.length != 0 && (
|
||||||
@@ -1560,6 +1901,10 @@ function _Chat() {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{showShortcutKeyModal && (
|
||||||
|
<ShortcutKeyModal onClose={() => setShowShortcutKeyModal(false)} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user