mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-20 20:34:36 +00:00
Compare commits
694
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ff4825858 | ||
|
|
c76adee007 | ||
|
|
69cd2a363a | ||
|
|
87e6d9cf25 | ||
|
|
c20ac28a54 | ||
|
|
fd93ee035d | ||
|
|
38ed7aefc6 | ||
|
|
3a13865db8 | ||
|
|
70053beadb | ||
|
|
b89c594aac | ||
|
|
e9a11db7b9 | ||
|
|
05d1792d43 | ||
|
|
53a21dfb0b | ||
|
|
f1ed8912bb | ||
|
|
7c390a7a2e | ||
|
|
bd646819ed | ||
|
|
5d9ea1667b | ||
|
|
c58da381c7 | ||
|
|
290f421f9f | ||
|
|
36ed0e5c9c | ||
|
|
079ec51f50 | ||
|
|
1f2504ebcc | ||
|
|
369e560f1d | ||
|
|
babef2a1ef | ||
|
|
5bc0c68284 | ||
|
|
31549af180 | ||
|
|
1f21f946ba | ||
|
|
9a8098d2d5 | ||
|
|
bb414de33f | ||
|
|
eb8d0d0684 | ||
|
|
35c3d80ab9 | ||
|
|
09179a707f | ||
|
|
57e1bf97a5 | ||
|
|
011ffe1dd5 | ||
|
|
5771abbc81 | ||
|
|
b4bec344f6 | ||
|
|
a79ae80f6e | ||
|
|
b0ca3794ad | ||
|
|
790ec307f9 | ||
|
|
f66376f0b9 | ||
|
|
379167c992 | ||
|
|
28e2f9798c | ||
|
|
bc652c7bf0 | ||
|
|
75fa62653f | ||
|
|
4c78e40da4 | ||
|
|
c44d4fcc7c | ||
|
|
34639c3cf9 | ||
|
|
51aa5ba925 | ||
|
|
1581700646 | ||
|
|
1b849b6d8f | ||
|
|
041a065b2f | ||
|
|
aa2ad995a5 | ||
|
|
aed54adbea | ||
|
|
07c297a51b | ||
|
|
d33ac5e1e9 | ||
|
|
4926630a60 | ||
|
|
893412e965 | ||
|
|
c020e2d03f | ||
|
|
1d0551a991 | ||
|
|
052f1934b8 | ||
|
|
ae6c5eaea5 | ||
|
|
41c52b9df6 | ||
|
|
c13dbcc24e | ||
|
|
ac8fb9706f | ||
|
|
ec8c2c2913 | ||
|
|
c55157dc2e | ||
|
|
ef19f5fc81 | ||
|
|
b17d29ad35 | ||
|
|
292456a884 | ||
|
|
62c4b377dd | ||
|
|
365e8fc2ca | ||
|
|
78ba8ba731 | ||
|
|
dd49a52003 | ||
|
|
cde5081e94 | ||
|
|
acaf250fa8 | ||
|
|
f23fb122e7 | ||
|
|
644ba74bff | ||
|
|
96a8b0cfe2 | ||
|
|
29f22acd77 | ||
|
|
c296790801 | ||
|
|
0e81dee7e2 | ||
|
|
8c6f279f0a | ||
|
|
b9d024af49 | ||
|
|
8d2c00c107 | ||
|
|
125c092866 | ||
|
|
09596d2f54 | ||
|
|
a3c87ceeb4 | ||
|
|
7b16321e50 | ||
|
|
a4756ab7a9 | ||
|
|
7f026e1aab | ||
|
|
a65ced4a92 | ||
|
|
13f51d96cb | ||
|
|
a5cb88323d | ||
|
|
f49d1798d1 | ||
|
|
30dc2e7f34 | ||
|
|
2884a67ed0 | ||
|
|
f63cf9820a | ||
|
|
87132e54d7 | ||
|
|
9f940f162f | ||
|
|
907e3493ee | ||
|
|
8fad0d0325 | ||
|
|
2406daf3ef | ||
|
|
844b855974 | ||
|
|
e40599831a | ||
|
|
1ed1f7b390 | ||
|
|
0b21b07115 | ||
|
|
5036513bf9 | ||
|
|
a47c386319 | ||
|
|
db3fdea4a1 | ||
|
|
917cec3bf6 | ||
|
|
3b0cc913ec | ||
|
|
bbef8da454 | ||
|
|
f85768eef3 | ||
|
|
f33bc56bb9 | ||
|
|
ea90ab6f77 | ||
|
|
56988aea0f | ||
|
|
d417fa534f | ||
|
|
0d82daca6b | ||
|
|
331daa24ad | ||
|
|
95a72a47c8 | ||
|
|
c61453a807 | ||
|
|
7de9757c82 | ||
|
|
ea4e978c60 | ||
|
|
6f3bd7641b | ||
|
|
3b96119050 | ||
|
|
239d7ee0b0 | ||
|
|
303c0f2f53 | ||
|
|
529d9e92c9 | ||
|
|
6f923a28f7 | ||
|
|
c59eee60b7 | ||
|
|
64a8957e60 | ||
|
|
b5da088767 | ||
|
|
2a050dc95d | ||
|
|
f6aa9a067e | ||
|
|
79db0e54be | ||
|
|
b4054e185e | ||
|
|
8122b9fe4b | ||
|
|
c354f88f3b | ||
|
|
e0cb43f38d | ||
|
|
964e86f612 | ||
|
|
eebb873b8a | ||
|
|
6d472d7b4f | ||
|
|
e3301ecc98 | ||
|
|
f5484b0900 | ||
|
|
8d4a7f1f2e | ||
|
|
83c53d05ee | ||
|
|
9cdb18e1d6 | ||
|
|
b39ee66250 | ||
|
|
b79647d886 | ||
|
|
c5cfdb15e5 | ||
|
|
d6c4fc2d82 | ||
|
|
82235202fb | ||
|
|
57971d7a51 | ||
|
|
b94178bfd9 | ||
|
|
94c5a7fd47 | ||
|
|
48dcadd9ef | ||
|
|
6a0b198c7e | ||
|
|
7cd5b15c9b | ||
|
|
2659e490a8 | ||
|
|
c006e672f8 | ||
|
|
b29851d415 | ||
|
|
4f5f1b8f2b | ||
|
|
cbaea358f3 | ||
|
|
b60ec43944 | ||
|
|
b2b5d10b75 | ||
|
|
220b4e5bbd | ||
|
|
40f4b8b856 | ||
|
|
b6c22f2d82 | ||
|
|
44d3066a9c | ||
|
|
60f6a54282 | ||
|
|
049ced5c3f | ||
|
|
69b7462860 | ||
|
|
0626b22e61 | ||
|
|
e38ba28db5 | ||
|
|
9dbf4cd07e | ||
|
|
5633c93622 | ||
|
|
ed52c6147c | ||
|
|
538e3416db | ||
|
|
8e39db2ec7 | ||
|
|
ac4017ea0e | ||
|
|
030c4a9385 | ||
|
|
9985976ebd | ||
|
|
ca1f6271f5 | ||
|
|
4cbd50049e | ||
|
|
dc04d8be52 | ||
|
|
58bf645e66 | ||
|
|
1ca048abdc | ||
|
|
3c773b6d92 | ||
|
|
21387f5c71 | ||
|
|
8e52c403f7 | ||
|
|
8caf47464c | ||
|
|
efd69d8ece | ||
|
|
dc3152d2e3 | ||
|
|
e1864fbf33 | ||
|
|
44ea9abced | ||
|
|
2aeb41cecf | ||
|
|
1d1ee85e37 | ||
|
|
556f50aa2a | ||
|
|
41a4dba670 | ||
|
|
693ce1c55a | ||
|
|
a2db6a6108 | ||
|
|
9fb9e9154b | ||
|
|
c7cc3a1a4d | ||
|
|
fabf06154f | ||
|
|
583053509d | ||
|
|
5e41e079e5 | ||
|
|
d128578efa | ||
|
|
1762ee0d2e | ||
|
|
2e81766c92 | ||
|
|
4330b92612 | ||
|
|
702295d73a | ||
|
|
07c607b709 | ||
|
|
856dc81748 | ||
|
|
6f38e8488a | ||
|
|
d55f215c63 | ||
|
|
69d937a8d3 | ||
|
|
ec672446d1 | ||
|
|
d9a0c9bdc7 | ||
|
|
365415c87a | ||
|
|
c989cdd9d7 | ||
|
|
4f55eccae2 | ||
|
|
37df0b8b98 | ||
|
|
6d9014b61f | ||
|
|
43c121464e | ||
|
|
4567fd1b06 | ||
|
|
c75fb2c814 | ||
|
|
3dedf47839 | ||
|
|
9ee64935fb | ||
|
|
bc58c21673 | ||
|
|
0ed85bb88b | ||
|
|
3abfc83d96 | ||
|
|
d625caddd9 | ||
|
|
fca12a2630 | ||
|
|
bc38b68902 | ||
|
|
5753d1a089 | ||
|
|
387c783b32 | ||
|
|
90d46e7749 | ||
|
|
bebdaa4d65 | ||
|
|
fd30944830 | ||
|
|
46a152a21b | ||
|
|
1074507a2f | ||
|
|
55440f0a39 | ||
|
|
9c02454005 | ||
|
|
1c9601ff32 | ||
|
|
77061aad4b | ||
|
|
45f58d4f91 | ||
|
|
7682accd95 | ||
|
|
17af089189 | ||
|
|
35ab084fc3 | ||
|
|
0e3c809ceb | ||
|
|
28c7f41cba | ||
|
|
d710248407 | ||
|
|
02d0d92a8e | ||
|
|
8306391f91 | ||
|
|
4abd59ce1b | ||
|
|
68282c4aa7 | ||
|
|
603a08fc1a | ||
|
|
c9dc6bd628 | ||
|
|
7cb466a8e6 | ||
|
|
87d813c56c | ||
|
|
bf04a24221 | ||
|
|
f353979e0a | ||
|
|
959067edfb | ||
|
|
f05d68d4ec | ||
|
|
e7f4dd470d | ||
|
|
05b972479a | ||
|
|
fb4f39d300 | ||
|
|
ac1360f479 | ||
|
|
bbcdaab79d | ||
|
|
303685c230 | ||
|
|
0f24cafbdd | ||
|
|
33983b6f3e | ||
|
|
3a54fb7fcf | ||
|
|
430e679e2a | ||
|
|
55901f0bda | ||
|
|
776a9c0049 | ||
|
|
fc0615b171 | ||
|
|
96b6f7e217 | ||
|
|
034d2b6bf8 | ||
|
|
e9dafc7093 | ||
|
|
aee7a5fbc5 | ||
|
|
86cb9c150b | ||
|
|
7d9980e8fa | ||
|
|
d2c5f279f6 | ||
|
|
4ade13a374 | ||
|
|
c4ee063b95 | ||
|
|
f28258de22 | ||
|
|
df0176a188 | ||
|
|
c65275fd32 | ||
|
|
71c185f614 | ||
|
|
65e760aa1a | ||
|
|
01a7cc4a45 | ||
|
|
b3d6d5d71a | ||
|
|
2bcc76220c | ||
|
|
d9c01a638d | ||
|
|
94c1b25146 | ||
|
|
11f0f906bd | ||
|
|
4b681031bf | ||
|
|
2cbb8c7b5c | ||
|
|
de280d993d | ||
|
|
e399edd3cc | ||
|
|
b5959c313d | ||
|
|
4a85b6b97e | ||
|
|
70a8cf026f | ||
|
|
167edfec6c | ||
|
|
a0bb1f3a2b | ||
|
|
412d3442fa | ||
|
|
efa200ec48 | ||
|
|
50d19e204f | ||
|
|
aaec9194d5 | ||
|
|
33f4904b25 | ||
|
|
cecd39317d | ||
|
|
3c62a9a9b0 | ||
|
|
21fad9dbb4 | ||
|
|
48a1c88115 | ||
|
|
8b9ecffc2f | ||
|
|
42e9605871 | ||
|
|
a726e37394 | ||
|
|
f1ee883cd3 | ||
|
|
1c632d151d | ||
|
|
0ec07e57dd | ||
|
|
fdffe49974 | ||
|
|
de0394917a | ||
|
|
ea25949479 | ||
|
|
99fa530967 | ||
|
|
b9589e8ed6 | ||
|
|
0de0ad0d36 | ||
|
|
5ef7693933 | ||
|
|
7f68fa2414 | ||
|
|
bb5ac40a67 | ||
|
|
7efc1629ba | ||
|
|
67f22514ed | ||
|
|
ad868308c0 | ||
|
|
bbe30f53b5 | ||
|
|
feebe6c7f2 | ||
|
|
b67eb6f25d | ||
|
|
644823529f | ||
|
|
bac006e72b | ||
|
|
ad98c9549a | ||
|
|
77ba15f71b | ||
|
|
a1ce124748 | ||
|
|
cedb38d1cf | ||
|
|
c36e214e2e | ||
|
|
32a0d69b17 | ||
|
|
1583cb4ef0 | ||
|
|
cc0cb057b3 | ||
|
|
2710f56ae1 | ||
|
|
8bc2eff58a | ||
|
|
ec79951e7f | ||
|
|
24602055a8 | ||
|
|
4ad6ffefb7 | ||
|
|
1c2153a2cb | ||
|
|
64d233fe93 | ||
|
|
66c5d60b3d | ||
|
|
5f039654f0 | ||
|
|
ed0ac68324 | ||
|
|
605adaa3c2 | ||
|
|
9ef99aa766 | ||
|
|
2007a89594 | ||
|
|
26d13af28f | ||
|
|
be841b88ee | ||
|
|
4b13f9c255 | ||
|
|
74cb53dee1 | ||
|
|
2607888a97 | ||
|
|
8b3670b8dd | ||
|
|
96754f5a33 | ||
|
|
ddd10539ad | ||
|
|
088ab33df8 | ||
|
|
d606faa99c | ||
|
|
53d1fd6c5c | ||
|
|
2ff9e33e26 | ||
|
|
e4c957078c | ||
|
|
cd0cea393c | ||
|
|
c66fa37665 | ||
|
|
f4c6c41f0b | ||
|
|
644d5ea618 | ||
|
|
48104abf51 | ||
|
|
7a1a3408bf | ||
|
|
82c9e0de58 | ||
|
|
e7a185962d | ||
|
|
30a8824b64 | ||
|
|
9d01c80d33 | ||
|
|
1d529c3ce4 | ||
|
|
229d03a690 | ||
|
|
3a9fb3780e | ||
|
|
437aa87c9b | ||
|
|
bcbb94906c | ||
|
|
bfdc0b3989 | ||
|
|
6bfcb0ce79 | ||
|
|
c26de75913 | ||
|
|
bd8c05a830 | ||
|
|
bd3901ca29 | ||
|
|
3636be31d6 | ||
|
|
041ccf0195 | ||
|
|
1fca942b9c | ||
|
|
18d9369ce4 | ||
|
|
c5596e0925 | ||
|
|
5e5b1bce35 | ||
|
|
15ac7fb932 | ||
|
|
8300ee8bbe | ||
|
|
dc1cc7f115 | ||
|
|
28dfcae350 | ||
|
|
a44e5eb1ab | ||
|
|
67fb4eb98e | ||
|
|
66c3dae06b | ||
|
|
1abf8625d8 | ||
|
|
3cbd587b2c | ||
|
|
41f4ee7c7d | ||
|
|
c69ff49758 | ||
|
|
68fddaa319 | ||
|
|
09ac8a1165 | ||
|
|
0dcb8bd714 | ||
|
|
0f0fcd2304 | ||
|
|
c67096b687 | ||
|
|
1721994111 | ||
|
|
4cbe172934 | ||
|
|
4071fdef84 | ||
|
|
c883114a4d | ||
|
|
e50cabac4b | ||
|
|
809feb1e86 | ||
|
|
33130f18d2 | ||
|
|
785b00c312 | ||
|
|
a034cf8b8d | ||
|
|
01171742a6 | ||
|
|
fb08b92402 | ||
|
|
a09a16e8f6 | ||
|
|
68b97075af | ||
|
|
ad3f4f2ce5 | ||
|
|
17a1f53c47 | ||
|
|
ed1458aa6d | ||
|
|
99dfbaef61 | ||
|
|
da6c599efd | ||
|
|
61b39d49bd | ||
|
|
ba5d8ca733 | ||
|
|
28b4b19e7e | ||
|
|
bdc424007e | ||
|
|
e4a93c02c5 | ||
|
|
8262a03f29 | ||
|
|
ecf1c2590c | ||
|
|
162897e02a | ||
|
|
c1caa454b3 | ||
|
|
bf6fa402e2 | ||
|
|
85c0150653 | ||
|
|
89d80bfff4 | ||
|
|
a1eba112f3 | ||
|
|
6b4bc0a9a8 | ||
|
|
08b0fe6816 | ||
|
|
c19ae1d5be | ||
|
|
17be6442a8 | ||
|
|
38dad2afdf | ||
|
|
8e6ef3fa64 | ||
|
|
a1487b0958 | ||
|
|
82ebe24b9e | ||
|
|
2753d9fb71 | ||
|
|
bf0e5c23f7 | ||
|
|
672fdd14ed | ||
|
|
af65908cb0 | ||
|
|
756b600b7a | ||
|
|
054d0dee1d | ||
|
|
d2386a3114 | ||
|
|
7972130513 | ||
|
|
81db7fdc1e | ||
|
|
5fc6f662e1 | ||
|
|
593995a404 | ||
|
|
101b59cfe8 | ||
|
|
56df36895a | ||
|
|
85124f098b | ||
|
|
2efa56dbb8 | ||
|
|
79579c34bf | ||
|
|
18bb9c315f | ||
|
|
b8bba053fc | ||
|
|
8c2f1a80d3 | ||
|
|
e37f3be0bf | ||
|
|
b0dc9df887 | ||
|
|
6187919000 | ||
|
|
4035abc0cd | ||
|
|
101099ca3a | ||
|
|
8b286e8fb3 | ||
|
|
aa70d13f60 | ||
|
|
05ecfb6241 | ||
|
|
243c582159 | ||
|
|
6ba7c810a7 | ||
|
|
f56a19e5b8 | ||
|
|
46018417ad | ||
|
|
dbf432c3e7 | ||
|
|
e3e60f914b | ||
|
|
270e273254 | ||
|
|
359ec30d0c | ||
|
|
a1f0ed9575 | ||
|
|
b3b92f334e | ||
|
|
2c1539ead7 | ||
|
|
0d107dd566 | ||
|
|
1c0c426b85 | ||
|
|
4982512da2 | ||
|
|
2ea8f77efb | ||
|
|
f95ab4cdf1 | ||
|
|
c4965befe7 | ||
|
|
0e1235122e | ||
|
|
e78d45acc9 | ||
|
|
b34f3be13e | ||
|
|
9fb6a49260 | ||
|
|
a992dee4e8 | ||
|
|
3ac39dcc7d | ||
|
|
34027da7f1 | ||
|
|
c523101439 | ||
|
|
d85e13b044 | ||
|
|
01e16a8509 | ||
|
|
04a336f7df | ||
|
|
a325533f20 | ||
|
|
6fc23568df | ||
|
|
736ae61e4a | ||
|
|
c5bea6f6f8 | ||
|
|
c7b28ba058 | ||
|
|
38573050aa | ||
|
|
153d8c160f | ||
|
|
32ef1588e8 | ||
|
|
80eb03709a | ||
|
|
d36e70e9dc | ||
|
|
fd45dece7f | ||
|
|
95318ad46d | ||
|
|
fc1ddf365f | ||
|
|
03ea4e569f | ||
|
|
e707cf7d46 | ||
|
|
0a7c6b0a4a | ||
|
|
ea670ef8c0 | ||
|
|
2c626efc59 | ||
|
|
28d78273e4 | ||
|
|
be0fe6fab3 | ||
|
|
cf043f6c07 | ||
|
|
faad8e30dd | ||
|
|
a7e92e2639 | ||
|
|
4056c2590b | ||
|
|
36cc762fc9 | ||
|
|
5f5d5936fa | ||
|
|
35a58873b0 | ||
|
|
f1ba6151a9 | ||
|
|
7b89583cf8 | ||
|
|
1576d14137 | ||
|
|
736018a0b0 | ||
|
|
7d5f6d9382 | ||
|
|
12195a276e | ||
|
|
25137b1984 | ||
|
|
7ad1900041 | ||
|
|
8eb56e5602 | ||
|
|
f130846ec1 | ||
|
|
920b6efffa | ||
|
|
e75daa299b | ||
|
|
8e49c795f5 | ||
|
|
4de5c29f86 | ||
|
|
4d6457e6ec | ||
|
|
14d46a0a5d | ||
|
|
31934ae04c | ||
|
|
a188159632 | ||
|
|
fd71960c3e | ||
|
|
e935196df4 | ||
|
|
4fc2c619fb | ||
|
|
8ced7a548f | ||
|
|
3444820958 | ||
|
|
1716a845eb | ||
|
|
b6781d69be | ||
|
|
bb8408cef5 | ||
|
|
e6866ff19c | ||
|
|
2ebbb59670 | ||
|
|
8f4a4eabfc | ||
|
|
e05abec01f | ||
|
|
f4eb16102b | ||
|
|
86c856f56f | ||
|
|
2221b8e557 | ||
|
|
c6baa64b4e | ||
|
|
a64141a9a6 | ||
|
|
c26936e2e6 | ||
|
|
894baad829 | ||
|
|
eba561bf6f | ||
|
|
da43f63735 | ||
|
|
d9a3b3e5f3 | ||
|
|
7ec328f952 | ||
|
|
5dcca69e8c | ||
|
|
f5dc6483d5 | ||
|
|
d949921143 | ||
|
|
7b03f04670 | ||
|
|
8f9e6622b0 | ||
|
|
71be5e897b | ||
|
|
1267fddf61 | ||
|
|
ba454dbfbf | ||
|
|
d1508ca030 | ||
|
|
d4a6a5ae15 | ||
|
|
7c24d54ca8 | ||
|
|
a4c1e32ff6 | ||
|
|
f56cf42461 | ||
|
|
3dea1da249 | ||
|
|
8fac29631d | ||
|
|
8fecd625d2 | ||
|
|
10b55b5ddd | ||
|
|
41ae2c81e7 | ||
|
|
278a89824c | ||
|
|
06c3e95454 | ||
|
|
a6cc1a2e71 | ||
|
|
c4459c4346 | ||
|
|
61e0447f92 | ||
|
|
1dc3018fd6 | ||
|
|
26fd3eff03 | ||
|
|
5bfaf8086b | ||
|
|
6c0a1efd71 | ||
|
|
f5ed5c7453 | ||
|
|
65158cce46 | ||
|
|
9cc006c941 | ||
|
|
a583463d60 | ||
|
|
8ed290c1c4 | ||
|
|
727221df2e | ||
|
|
0ab1f5412f | ||
|
|
9ded75d335 | ||
|
|
f135fdf7fc | ||
|
|
828df80088 | ||
|
|
c585caa0ce | ||
|
|
5bb69fa4ab | ||
|
|
85267e7379 | ||
|
|
45dc09d487 | ||
|
|
5ab9afac83 | ||
|
|
65ce86338b | ||
|
|
2a97037d7b | ||
|
|
d801393841 | ||
|
|
b2c0cdfc88 | ||
|
|
f32c8c9620 | ||
|
|
0f45d89255 | ||
|
|
96056d0137 | ||
|
|
f780c289e8 | ||
|
|
ac36119a02 | ||
|
|
39dc4557c1 | ||
|
|
30e94b6792 | ||
|
|
38f0ae5970 | ||
|
|
cf249586a9 | ||
|
|
1dba2d0f81 | ||
|
|
730809d8ea | ||
|
|
e8d1b79cb3 | ||
|
|
5e81b65f2f | ||
|
|
5775476bc0 | ||
|
|
7e8e2226a6 | ||
|
|
f0c20e852f | ||
|
|
7cdf8e9872 | ||
|
|
e2e3c7dde0 | ||
|
|
9e0ab4d116 | ||
|
|
8783caf313 | ||
|
|
f6f4640c5e | ||
|
|
613fe6768d | ||
|
|
ad8e3964ff | ||
|
|
941334da79 | ||
|
|
d54f816363 | ||
|
|
69b950db4c | ||
|
|
343a2fc2f7 | ||
|
|
12b967118b | ||
|
|
70efd4e016 | ||
|
|
f5aa68ecda | ||
|
|
d390b95b76 | ||
|
|
d1f6224b70 | ||
|
|
fcc59d606d | ||
|
|
91e7591955 | ||
|
|
c8b7e2b8d6 | ||
|
|
4ca00f7983 | ||
|
|
d2d0e6f6a1 | ||
|
|
a0fe273081 | ||
|
|
cad45ffa33 | ||
|
|
6a27bceec0 | ||
|
|
163d68318f | ||
|
|
0ea768011b | ||
|
|
8b9dbe10f0 | ||
|
|
29e32aaab9 | ||
|
|
6431cec7d3 | ||
|
|
9f5bdfaa31 | ||
|
|
9eabdd09db | ||
|
|
c3f8dc362e | ||
|
|
b85120873b | ||
|
|
6f58518c69 | ||
|
|
000fcb15fa | ||
|
|
ea43361492 | ||
|
|
c1818f197b | ||
|
|
b0653cec7b | ||
|
|
22a1a24cf5 | ||
|
|
65e9e892a4 | ||
|
|
9b5ce8c64f | ||
|
|
058793c73a | ||
|
|
da3a498a28 | ||
|
|
91387ca247 | ||
|
|
fccfb162b4 | ||
|
|
5fc2bd393e | ||
|
|
66eb12294a | ||
|
|
73b22ec29b | ||
|
|
c31ae2f3b5 | ||
|
|
76b53d6b5b | ||
|
|
a34dfed378 | ||
|
|
e08f68ed7c | ||
|
|
f09ed25fd3 | ||
|
|
e166e56249 | ||
|
|
5f58248016 | ||
|
|
07d6689d87 |
+23
-19
@@ -1,6 +1,6 @@
|
||||
# Git and GitHub folders
|
||||
.git/*
|
||||
.github/*
|
||||
.git
|
||||
.github
|
||||
|
||||
# Docker and CI/CD related files
|
||||
docker-compose.yml
|
||||
@@ -10,28 +10,32 @@ docker-compose.yml
|
||||
Dockerfile
|
||||
|
||||
# Documentation and license
|
||||
docs/*
|
||||
docs
|
||||
README.md
|
||||
README_CN.md
|
||||
LICENSE
|
||||
|
||||
# Runtime data folders (should be mounted as volumes)
|
||||
auths/*
|
||||
logs/*
|
||||
conv/*
|
||||
auths
|
||||
logs
|
||||
conv
|
||||
config.yaml
|
||||
|
||||
# Development/editor
|
||||
bin/*
|
||||
.vscode/*
|
||||
.claude/*
|
||||
.codex/*
|
||||
.gemini/*
|
||||
.serena/*
|
||||
.agent/*
|
||||
.agents/*
|
||||
.opencode/*
|
||||
.idea/*
|
||||
.bmad/*
|
||||
_bmad/*
|
||||
_bmad-output/*
|
||||
bin
|
||||
.vscode
|
||||
.claude
|
||||
.codex
|
||||
.codex-worktrees
|
||||
.gemini
|
||||
.serena
|
||||
.agent
|
||||
.agents
|
||||
.antigravitycli
|
||||
.opencode
|
||||
.idea
|
||||
.junie
|
||||
.worktrees
|
||||
.bmad
|
||||
_bmad
|
||||
_bmad-output
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# Cluster JWT example.
|
||||
# After deploying https://github.com/router-for-me/CLIProxyAPIHome, get the JWT value with:
|
||||
# curl -sS -X POST "http://<home-host>:8327/v0/management/certificates/clients" -H "X-MANAGEMENT-KEY: <management-key>" | jq -r '.home_jwt'
|
||||
# Then paste it into HOME_JWT here or export it before starting Compose.
|
||||
HOME_JWT=your-home-jwt-here
|
||||
@@ -18,7 +18,7 @@ Our team doesn't have any GODs or ORACLEs or MIND READERs. Please make sure to a
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**CLI Type**
|
||||
What type of CLI account do you use? (gemini-cli, gemini, codex, claude code or openai-compatibility)
|
||||
What type of CLI account do you use? (gemini, codex, claude code or openai-compatibility)
|
||||
|
||||
**Model Name**
|
||||
What model are you using? (example: gemini-2.5-pro, claude-sonnet-4-20250514, gpt-5, etc.)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
name: agents-md-guard
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- synchronize
|
||||
- reopened
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
close-when-agents-md-changed:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Detect AGENTS.md changes and close PR
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const prNumber = context.payload.pull_request.number;
|
||||
const { owner, repo } = context.repo;
|
||||
|
||||
const files = await github.paginate(github.rest.pulls.listFiles, {
|
||||
owner,
|
||||
repo,
|
||||
pull_number: prNumber,
|
||||
per_page: 100,
|
||||
});
|
||||
|
||||
const touchesAgentsMd = (path) =>
|
||||
typeof path === "string" &&
|
||||
(path === "AGENTS.md" || path.endsWith("/AGENTS.md"));
|
||||
|
||||
const touched = files.filter(
|
||||
(f) => touchesAgentsMd(f.filename) || touchesAgentsMd(f.previous_filename),
|
||||
);
|
||||
|
||||
if (touched.length === 0) {
|
||||
core.info("No AGENTS.md changes detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
const changedList = touched
|
||||
.map((f) =>
|
||||
f.previous_filename && f.previous_filename !== f.filename
|
||||
? `- ${f.previous_filename} -> ${f.filename}`
|
||||
: `- ${f.filename}`,
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
const body = [
|
||||
"This repository does not allow modifying `AGENTS.md` in pull requests.",
|
||||
"",
|
||||
"Detected changes:",
|
||||
changedList,
|
||||
"",
|
||||
"Please revert these changes and open a new PR without touching `AGENTS.md`.",
|
||||
].join("\n");
|
||||
|
||||
try {
|
||||
await github.rest.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: prNumber,
|
||||
body,
|
||||
});
|
||||
} catch (error) {
|
||||
core.warning(`Failed to comment on PR #${prNumber}: ${error.message}`);
|
||||
}
|
||||
|
||||
await github.rest.pulls.update({
|
||||
owner,
|
||||
repo,
|
||||
pull_number: prNumber,
|
||||
state: "closed",
|
||||
});
|
||||
|
||||
core.setFailed("PR modifies AGENTS.md");
|
||||
@@ -0,0 +1,73 @@
|
||||
name: auto-retarget-main-pr-to-dev
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
- edited
|
||||
branches:
|
||||
- main
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
retarget:
|
||||
if: github.actor != 'github-actions[bot]'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Retarget PR base to dev
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const pr = context.payload.pull_request;
|
||||
const prNumber = pr.number;
|
||||
const { owner, repo } = context.repo;
|
||||
|
||||
const baseRef = pr.base?.ref;
|
||||
const headRef = pr.head?.ref;
|
||||
const desiredBase = "dev";
|
||||
|
||||
if (baseRef !== "main") {
|
||||
core.info(`PR #${prNumber} base is ${baseRef}; nothing to do.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (headRef === desiredBase) {
|
||||
core.info(`PR #${prNumber} is ${desiredBase} -> main; skipping retarget.`);
|
||||
return;
|
||||
}
|
||||
|
||||
core.info(`Retargeting PR #${prNumber} base from ${baseRef} to ${desiredBase}.`);
|
||||
|
||||
try {
|
||||
await github.rest.pulls.update({
|
||||
owner,
|
||||
repo,
|
||||
pull_number: prNumber,
|
||||
base: desiredBase,
|
||||
});
|
||||
} catch (error) {
|
||||
core.setFailed(`Failed to retarget PR #${prNumber} to ${desiredBase}: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const body = [
|
||||
`This pull request targeted \`${baseRef}\`.`,
|
||||
"",
|
||||
`The base branch has been automatically changed to \`${desiredBase}\`.`,
|
||||
].join("\n");
|
||||
|
||||
try {
|
||||
await github.rest.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: prNumber,
|
||||
body,
|
||||
});
|
||||
} catch (error) {
|
||||
core.warning(`Failed to comment on PR #${prNumber}: ${error.message}`);
|
||||
}
|
||||
+657
-21
@@ -1,4 +1,4 @@
|
||||
name: goreleaser
|
||||
name: release
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -9,33 +9,669 @@ on:
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
GH_REPO: ${{ github.repository }}
|
||||
GO_VERSION: '1.26.4'
|
||||
|
||||
jobs:
|
||||
goreleaser:
|
||||
prepare-release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
- name: Create release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
release_notes_file="$(mktemp)"
|
||||
generated_notes_file="$(mktemp)"
|
||||
changelog_entries_file="$(mktemp)"
|
||||
changelog_notes_file="$(mktemp)"
|
||||
updated_notes_file="$(mktemp)"
|
||||
trap 'rm -f "$release_notes_file" "$generated_notes_file" "$changelog_entries_file" "$changelog_notes_file" "$updated_notes_file"' EXIT
|
||||
|
||||
cat > "$release_notes_file" <<'EOF'
|
||||
<!-- cliproxyapi-linux-release-assets:start -->
|
||||
## Linux release assets
|
||||
|
||||
- `CLIProxyAPI_<version>_linux_<arch>.tar.gz` is the default Linux build. It supports dynamic library plugins and is built against a GLIBC 2.17 baseline.
|
||||
- `CLIProxyAPI_<version>_linux_<arch>_no-plugin.tar.gz` is the portable Linux build for musl-based or older systems such as OpenWrt. It does not support dynamic library plugins.
|
||||
|
||||
## FreeBSD release assets
|
||||
|
||||
- `CLIProxyAPI_<version>_freebsd_aarch64_no-plugin.tar.gz` is the FreeBSD arm64 build. It is built without CGO and does not support dynamic library plugins.
|
||||
|
||||
<!-- cliproxyapi-linux-release-assets:end -->
|
||||
EOF
|
||||
|
||||
git fetch --force --tags
|
||||
previous_tag=""
|
||||
if previous_tag_value="$(git describe --tags --abbrev=0 "${GITHUB_REF_NAME}^" 2>/dev/null)"; then
|
||||
previous_tag="$previous_tag_value"
|
||||
changelog_range="${previous_tag}..${GITHUB_REF_NAME}"
|
||||
else
|
||||
changelog_range="$GITHUB_REF_NAME"
|
||||
fi
|
||||
|
||||
git log --reverse --pretty=format:'- %s (%h)' "$changelog_range" |
|
||||
grep -Ev '^- (docs:|test:)' > "$changelog_entries_file" || true
|
||||
|
||||
gh api "repos/${GH_REPO}/releases/generate-notes" \
|
||||
-f tag_name="$GITHUB_REF_NAME" \
|
||||
--jq .body > "$generated_notes_file"
|
||||
if [[ ! -s "$generated_notes_file" ]]; then
|
||||
if [[ -n "$previous_tag" ]]; then
|
||||
printf '**Full Changelog**: https://github.com/%s/compare/%s...%s\n' "$GH_REPO" "$previous_tag" "$GITHUB_REF_NAME" > "$generated_notes_file"
|
||||
else
|
||||
printf '**Full Changelog**: https://github.com/%s/commits/%s\n' "$GH_REPO" "$GITHUB_REF_NAME" > "$generated_notes_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
{
|
||||
if [[ -s "$changelog_entries_file" ]]; then
|
||||
printf '## Changelog\n\n'
|
||||
cat "$changelog_entries_file"
|
||||
printf '\n\n'
|
||||
fi
|
||||
cat "$generated_notes_file"
|
||||
} > "$changelog_notes_file"
|
||||
|
||||
{
|
||||
cat "$release_notes_file"
|
||||
printf '\n'
|
||||
cat "$changelog_notes_file"
|
||||
} > "$updated_notes_file"
|
||||
|
||||
if gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1; then
|
||||
gh release edit "$GITHUB_REF_NAME" --title "$GITHUB_REF_NAME" --notes-file "$updated_notes_file"
|
||||
else
|
||||
gh release create "$GITHUB_REF_NAME" --title "$GITHUB_REF_NAME" --notes-file "$updated_notes_file"
|
||||
fi
|
||||
|
||||
build-hosted:
|
||||
name: build ${{ matrix.target }}
|
||||
needs: prepare-release
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- target: darwin-amd64
|
||||
runner: macos-15-intel
|
||||
goos: darwin
|
||||
goarch: amd64
|
||||
asset_arch: amd64
|
||||
archive_format: tar.gz
|
||||
- target: darwin-arm64
|
||||
runner: macos-15
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
asset_arch: aarch64
|
||||
archive_format: tar.gz
|
||||
- target: windows-amd64
|
||||
runner: windows-latest
|
||||
goos: windows
|
||||
goarch: amd64
|
||||
asset_arch: amd64
|
||||
archive_format: zip
|
||||
- target: windows-arm64
|
||||
runner: windows-11-arm
|
||||
goos: windows
|
||||
goarch: arm64
|
||||
asset_arch: aarch64
|
||||
archive_format: zip
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Refresh models catalog
|
||||
run: curl -fsSL https://raw.githubusercontent.com/router-for-me/models/refs/heads/main/models.json -o internal/registry/models/models.json
|
||||
- run: git fetch --force --tags
|
||||
- uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '>=1.26.0'
|
||||
cache: true
|
||||
- name: Generate Build Metadata
|
||||
shell: bash
|
||||
run: |
|
||||
VERSION=$(git describe --tags --always --dirty)
|
||||
echo "VERSION=${VERSION}" >> $GITHUB_ENV
|
||||
echo COMMIT=`git rev-parse --short HEAD` >> $GITHUB_ENV
|
||||
echo BUILD_DATE=`date -u +%Y-%m-%dT%H:%M:%SZ` >> $GITHUB_ENV
|
||||
- uses: goreleaser/goreleaser-action@v4
|
||||
set -euo pipefail
|
||||
git fetch --depth 1 https://github.com/router-for-me/models.git main
|
||||
git show FETCH_HEAD:models.json > internal/registry/models/models.json
|
||||
- name: Fetch tags
|
||||
shell: bash
|
||||
run: git fetch --force --tags
|
||||
- uses: actions/setup-go@v6
|
||||
with:
|
||||
distribution: goreleaser
|
||||
version: latest
|
||||
args: release --clean
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache: true
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: go-${{ runner.os }}-${{ runner.arch }}-${{ matrix.target }}-${{ hashFiles('go.sum') }}
|
||||
restore-keys: |
|
||||
go-${{ runner.os }}-${{ runner.arch }}-${{ matrix.target }}-
|
||||
go-${{ runner.os }}-${{ runner.arch }}-
|
||||
- name: Generate Build Metadata
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "RELEASE_VERSION=${GITHUB_REF_NAME#v}" >> "$GITHUB_ENV"
|
||||
echo "COMMIT=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV"
|
||||
echo "BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_ENV"
|
||||
- name: Build archive
|
||||
shell: bash
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
GOOS: ${{ matrix.goos }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
ASSET_ARCH: ${{ matrix.asset_arch }}
|
||||
ARCHIVE_FORMAT: ${{ matrix.archive_format }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
binary_name="cli-proxy-api"
|
||||
if [[ "$GOOS" == "windows" ]]; then
|
||||
binary_name="cli-proxy-api.exe"
|
||||
fi
|
||||
|
||||
archive_dir="dist/${TARGET}/archive"
|
||||
archive_name="CLIProxyAPI_${RELEASE_VERSION}_${GOOS}_${ASSET_ARCH}.${ARCHIVE_FORMAT}"
|
||||
rm -rf "dist/${TARGET}"
|
||||
mkdir -p "$archive_dir"
|
||||
|
||||
CGO_ENABLED=1 GOOS="$GOOS" GOARCH="$GOARCH" go build \
|
||||
-ldflags="-s -w -X main.Version=${RELEASE_VERSION} -X main.Commit=${COMMIT} -X main.BuildDate=${BUILD_DATE}" \
|
||||
-o "$archive_dir/$binary_name" ./cmd/server/
|
||||
|
||||
cp LICENSE README.md README_CN.md config.example.yaml "$archive_dir/"
|
||||
if [[ "$ARCHIVE_FORMAT" == "zip" ]]; then
|
||||
powershell -NoProfile -Command "Compress-Archive -Path '${archive_dir}/*' -DestinationPath 'dist/${archive_name}' -Force"
|
||||
else
|
||||
tar -C "$archive_dir" -czf "dist/$archive_name" "$binary_name" LICENSE README.md README_CN.md config.example.yaml
|
||||
fi
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/CLIProxyAPI_*
|
||||
if-no-files-found: error
|
||||
- name: Upload release assets
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
VERSION: ${{ env.VERSION }}
|
||||
COMMIT: ${{ env.COMMIT }}
|
||||
BUILD_DATE: ${{ env.BUILD_DATE }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
shopt -s nullglob
|
||||
assets=(dist/CLIProxyAPI_*.tar.gz dist/CLIProxyAPI_*.zip)
|
||||
if [[ ${#assets[@]} -eq 0 ]]; then
|
||||
printf 'expected archive assets, found %s\n' "${#assets[@]}" >&2
|
||||
printf '%s\n' "${assets[@]}" >&2
|
||||
exit 1
|
||||
fi
|
||||
gh release upload "$GITHUB_REF_NAME" "${assets[@]}" --clobber
|
||||
- name: Refresh release checksums
|
||||
continue-on-error: true
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
tmp_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
gh release download "$GITHUB_REF_NAME" \
|
||||
--pattern 'CLIProxyAPI_*.tar.gz' \
|
||||
--pattern 'CLIProxyAPI_*.zip' \
|
||||
--dir "$tmp_dir" \
|
||||
--clobber
|
||||
(
|
||||
cd "$tmp_dir"
|
||||
shopt -s nullglob
|
||||
archives=(CLIProxyAPI_*.tar.gz CLIProxyAPI_*.zip)
|
||||
if [[ ${#archives[@]} -eq 0 ]]; then
|
||||
echo "No release archives found"
|
||||
exit 0
|
||||
fi
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "${archives[@]}" | sort -k2 > checksums.txt
|
||||
else
|
||||
shasum -a 256 "${archives[@]}" | sort -k2 > checksums.txt
|
||||
fi
|
||||
)
|
||||
gh release upload "$GITHUB_REF_NAME" "$tmp_dir/checksums.txt" --clobber
|
||||
|
||||
build-linux-glibc:
|
||||
name: build linux-${{ matrix.goarch }} glibc
|
||||
needs: prepare-release
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- target: linux-amd64
|
||||
runner: ubuntu-latest
|
||||
goarch: amd64
|
||||
asset_arch: amd64
|
||||
manylinux_image: quay.io/pypa/manylinux2014_x86_64
|
||||
- target: linux-arm64
|
||||
runner: ubuntu-24.04-arm
|
||||
goarch: arm64
|
||||
asset_arch: aarch64
|
||||
manylinux_image: quay.io/pypa/manylinux2014_aarch64
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Refresh models catalog
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git fetch --depth 1 https://github.com/router-for-me/models.git main
|
||||
git show FETCH_HEAD:models.json > internal/registry/models/models.json
|
||||
- name: Fetch tags
|
||||
shell: bash
|
||||
run: git fetch --force --tags
|
||||
- name: Generate Build Metadata
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "RELEASE_VERSION=${GITHUB_REF_NAME#v}" >> "$GITHUB_ENV"
|
||||
echo "COMMIT=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV"
|
||||
echo "BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_ENV"
|
||||
- name: Build archive
|
||||
shell: bash
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
ASSET_ARCH: ${{ matrix.asset_arch }}
|
||||
MANYLINUX_IMAGE: ${{ matrix.manylinux_image }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
archive_dir="dist/${TARGET}/archive"
|
||||
archive_name="CLIProxyAPI_${RELEASE_VERSION}_linux_${ASSET_ARCH}.tar.gz"
|
||||
rm -rf "dist/${TARGET}"
|
||||
mkdir -p "$archive_dir"
|
||||
|
||||
docker run --rm \
|
||||
-v "$PWD:/src" \
|
||||
-w /src \
|
||||
-e GO_VERSION \
|
||||
-e GOARCH \
|
||||
-e RELEASE_VERSION \
|
||||
-e COMMIT \
|
||||
-e BUILD_DATE \
|
||||
"$MANYLINUX_IMAGE" \
|
||||
bash -euo pipefail -c '
|
||||
go_archive="go${GO_VERSION}.linux-${GOARCH}.tar.gz"
|
||||
curl -fsSL "https://go.dev/dl/${go_archive}" -o "/tmp/${go_archive}"
|
||||
rm -rf /usr/local/go
|
||||
tar -C /usr/local -xzf "/tmp/${go_archive}"
|
||||
export PATH="/usr/local/go/bin:${PATH}"
|
||||
|
||||
CGO_ENABLED=1 GOOS=linux GOARCH="${GOARCH}" go build -buildvcs=false \
|
||||
-ldflags="-s -w -X main.Version=${RELEASE_VERSION} -X main.Commit=${COMMIT} -X main.BuildDate=${BUILD_DATE}" \
|
||||
-o "'"$archive_dir"'/cli-proxy-api" ./cmd/server/
|
||||
|
||||
glibc_versions="$(readelf --version-info "'"$archive_dir"'/cli-proxy-api" | sed -n "s/.*Name: GLIBC_\([0-9.]*\).*/\1/p" | sort -Vu)"
|
||||
if [[ -n "${glibc_versions}" ]]; then
|
||||
printf "GLIBC versions:\n%s\n" "${glibc_versions}"
|
||||
max_glibc="$(printf "%s\n" "${glibc_versions}" | sort -V | tail -n 1)"
|
||||
if [[ "$(printf "2.17\n%s\n" "${max_glibc}" | sort -V | tail -n 1)" != "2.17" ]]; then
|
||||
printf "linux ${GOARCH} binary requires GLIBC_%s, expected GLIBC_2.17 or older\n" "${max_glibc}" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
'
|
||||
|
||||
cp LICENSE README.md README_CN.md config.example.yaml "$archive_dir/"
|
||||
tar -C "$archive_dir" -czf "dist/$archive_name" cli-proxy-api LICENSE README.md README_CN.md config.example.yaml
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/CLIProxyAPI_*
|
||||
if-no-files-found: error
|
||||
- name: Upload release assets
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
shopt -s nullglob
|
||||
assets=(dist/CLIProxyAPI_*.tar.gz)
|
||||
if [[ ${#assets[@]} -eq 0 ]]; then
|
||||
printf 'expected archive assets, found %s\n' "${#assets[@]}" >&2
|
||||
printf '%s\n' "${assets[@]}" >&2
|
||||
exit 1
|
||||
fi
|
||||
gh release upload "$GITHUB_REF_NAME" "${assets[@]}" --clobber
|
||||
- name: Refresh release checksums
|
||||
continue-on-error: true
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
tmp_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
gh release download "$GITHUB_REF_NAME" \
|
||||
--pattern 'CLIProxyAPI_*.tar.gz' \
|
||||
--pattern 'CLIProxyAPI_*.zip' \
|
||||
--dir "$tmp_dir" \
|
||||
--clobber
|
||||
(
|
||||
cd "$tmp_dir"
|
||||
shopt -s nullglob
|
||||
archives=(CLIProxyAPI_*.tar.gz CLIProxyAPI_*.zip)
|
||||
if [[ ${#archives[@]} -eq 0 ]]; then
|
||||
echo "No release archives found"
|
||||
exit 0
|
||||
fi
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "${archives[@]}" | sort -k2 > checksums.txt
|
||||
else
|
||||
shasum -a 256 "${archives[@]}" | sort -k2 > checksums.txt
|
||||
fi
|
||||
)
|
||||
gh release upload "$GITHUB_REF_NAME" "$tmp_dir/checksums.txt" --clobber
|
||||
|
||||
build-linux-no-plugin:
|
||||
name: build linux-${{ matrix.goarch }} no-plugin
|
||||
needs: prepare-release
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- target: linux-amd64-no-plugin
|
||||
goarch: amd64
|
||||
asset_arch: amd64
|
||||
- target: linux-arm64-no-plugin
|
||||
goarch: arm64
|
||||
asset_arch: aarch64
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Refresh models catalog
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git fetch --depth 1 https://github.com/router-for-me/models.git main
|
||||
git show FETCH_HEAD:models.json > internal/registry/models/models.json
|
||||
- name: Fetch tags
|
||||
shell: bash
|
||||
run: git fetch --force --tags
|
||||
- uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache: true
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: go-linux-no-plugin-${{ matrix.goarch }}-${{ hashFiles('go.sum') }}
|
||||
restore-keys: |
|
||||
go-linux-no-plugin-${{ matrix.goarch }}-
|
||||
go-linux-no-plugin-
|
||||
- name: Generate Build Metadata
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "RELEASE_VERSION=${GITHUB_REF_NAME#v}" >> "$GITHUB_ENV"
|
||||
echo "COMMIT=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV"
|
||||
echo "BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_ENV"
|
||||
- name: Build archive
|
||||
shell: bash
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
ASSET_ARCH: ${{ matrix.asset_arch }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
archive_dir="dist/${TARGET}/archive"
|
||||
archive_name="CLIProxyAPI_${RELEASE_VERSION}_linux_${ASSET_ARCH}_no-plugin.tar.gz"
|
||||
rm -rf "dist/${TARGET}"
|
||||
mkdir -p "$archive_dir"
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH="$GOARCH" go build -buildvcs=false \
|
||||
-ldflags="-s -w -X main.Version=${RELEASE_VERSION} -X main.Commit=${COMMIT} -X main.BuildDate=${BUILD_DATE}" \
|
||||
-o "$archive_dir/cli-proxy-api" ./cmd/server/
|
||||
|
||||
if readelf -l "$archive_dir/cli-proxy-api" | grep -q 'Requesting program interpreter'; then
|
||||
readelf -l "$archive_dir/cli-proxy-api" >&2
|
||||
echo "no-plugin linux binary must not require a dynamic interpreter" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp LICENSE README.md README_CN.md config.example.yaml "$archive_dir/"
|
||||
tar -C "$archive_dir" -czf "dist/$archive_name" cli-proxy-api LICENSE README.md README_CN.md config.example.yaml
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/CLIProxyAPI_*
|
||||
if-no-files-found: error
|
||||
- name: Upload release assets
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
shopt -s nullglob
|
||||
assets=(dist/CLIProxyAPI_*.tar.gz)
|
||||
if [[ ${#assets[@]} -eq 0 ]]; then
|
||||
printf 'expected archive assets, found %s\n' "${#assets[@]}" >&2
|
||||
printf '%s\n' "${assets[@]}" >&2
|
||||
exit 1
|
||||
fi
|
||||
gh release upload "$GITHUB_REF_NAME" "${assets[@]}" --clobber
|
||||
- name: Refresh release checksums
|
||||
continue-on-error: true
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
tmp_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
gh release download "$GITHUB_REF_NAME" \
|
||||
--pattern 'CLIProxyAPI_*.tar.gz' \
|
||||
--pattern 'CLIProxyAPI_*.zip' \
|
||||
--dir "$tmp_dir" \
|
||||
--clobber
|
||||
(
|
||||
cd "$tmp_dir"
|
||||
shopt -s nullglob
|
||||
archives=(CLIProxyAPI_*.tar.gz CLIProxyAPI_*.zip)
|
||||
if [[ ${#archives[@]} -eq 0 ]]; then
|
||||
echo "No release archives found"
|
||||
exit 0
|
||||
fi
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "${archives[@]}" | sort -k2 > checksums.txt
|
||||
else
|
||||
shasum -a 256 "${archives[@]}" | sort -k2 > checksums.txt
|
||||
fi
|
||||
)
|
||||
gh release upload "$GITHUB_REF_NAME" "$tmp_dir/checksums.txt" --clobber
|
||||
|
||||
build-freebsd:
|
||||
name: build ${{ matrix.target }}
|
||||
needs: prepare-release
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
ASSET_ARCH: ${{ matrix.asset_arch }}
|
||||
ASSET_SUFFIX: ${{ matrix.asset_suffix }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- target: freebsd-amd64
|
||||
goarch: amd64
|
||||
asset_arch: amd64
|
||||
asset_suffix: ''
|
||||
cgo_enabled: true
|
||||
- target: freebsd-arm64-no-plugin
|
||||
goarch: arm64
|
||||
asset_arch: aarch64
|
||||
asset_suffix: _no-plugin
|
||||
cgo_enabled: false
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Refresh models catalog
|
||||
run: |
|
||||
git fetch --depth 1 https://github.com/router-for-me/models.git main
|
||||
git show FETCH_HEAD:models.json > internal/registry/models/models.json
|
||||
- name: Fetch tags
|
||||
run: git fetch --force --tags
|
||||
- uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache: true
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: go-freebsd-${{ matrix.goarch }}-${{ hashFiles('go.sum') }}
|
||||
restore-keys: |
|
||||
go-freebsd-${{ matrix.goarch }}-
|
||||
go-freebsd-
|
||||
- name: Generate Build Metadata
|
||||
id: metadata
|
||||
run: |
|
||||
set -euo pipefail
|
||||
release_version="${GITHUB_REF_NAME#v}"
|
||||
commit="$(git rev-parse --short HEAD)"
|
||||
build_date="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
echo "RELEASE_VERSION=$release_version" >> "$GITHUB_ENV"
|
||||
echo "COMMIT=$commit" >> "$GITHUB_ENV"
|
||||
echo "BUILD_DATE=$build_date" >> "$GITHUB_ENV"
|
||||
echo "release_version=$release_version" >> "$GITHUB_OUTPUT"
|
||||
echo "commit=$commit" >> "$GITHUB_OUTPUT"
|
||||
echo "build_date=$build_date" >> "$GITHUB_OUTPUT"
|
||||
- name: Prepare FreeBSD output
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
rm -rf "dist/${TARGET}"
|
||||
- name: Install FreeBSD cross-build dependencies
|
||||
if: ${{ matrix.cgo_enabled }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y clang lld wget
|
||||
- name: Build FreeBSD binary with CGO
|
||||
if: ${{ matrix.cgo_enabled }}
|
||||
timeout-minutes: 45
|
||||
uses: go-cross/cgo-actions@v1
|
||||
with:
|
||||
dir: .
|
||||
packages: ./cmd/server/
|
||||
targets: ${{ env.TARGET }}
|
||||
out-dir: dist/${{ env.TARGET }}/bin
|
||||
output: cli-proxy-api
|
||||
flags: >-
|
||||
-ldflags=-s -w
|
||||
-X main.Version=${{ steps.metadata.outputs.release_version }}
|
||||
-X main.Commit=${{ steps.metadata.outputs.commit }}
|
||||
-X main.BuildDate=${{ steps.metadata.outputs.build_date }}
|
||||
- name: Build FreeBSD no-plugin binary
|
||||
if: ${{ !matrix.cgo_enabled }}
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mkdir -p "dist/${TARGET}/bin"
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH="$GOARCH" go build -buildvcs=false \
|
||||
-ldflags="-s -w -X main.Version=${RELEASE_VERSION} -X main.Commit=${COMMIT} -X main.BuildDate=${BUILD_DATE}" \
|
||||
-o "dist/${TARGET}/bin/cli-proxy-api" ./cmd/server/
|
||||
- name: Package FreeBSD archive
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
archive_dir="dist/${TARGET}/archive"
|
||||
archive_name="CLIProxyAPI_${RELEASE_VERSION}_freebsd_${ASSET_ARCH}${ASSET_SUFFIX}.tar.gz"
|
||||
mkdir -p "$archive_dir"
|
||||
|
||||
echo "Packaging ${archive_name}"
|
||||
cp "dist/${TARGET}/bin/cli-proxy-api" "$archive_dir/cli-proxy-api"
|
||||
cp LICENSE README.md README_CN.md config.example.yaml "$archive_dir/"
|
||||
tar -C "$archive_dir" -czf "dist/$archive_name" cli-proxy-api LICENSE README.md README_CN.md config.example.yaml
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/CLIProxyAPI_*
|
||||
if-no-files-found: error
|
||||
- name: Upload release assets
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
shopt -s nullglob
|
||||
assets=(dist/CLIProxyAPI_*.tar.gz)
|
||||
if [[ ${#assets[@]} -eq 0 ]]; then
|
||||
printf 'expected archive assets, found %s\n' "${#assets[@]}" >&2
|
||||
printf '%s\n' "${assets[@]}" >&2
|
||||
exit 1
|
||||
fi
|
||||
gh release upload "$GITHUB_REF_NAME" "${assets[@]}" --clobber
|
||||
- name: Refresh release checksums
|
||||
continue-on-error: true
|
||||
shell: bash
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
tmp_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
gh release download "$GITHUB_REF_NAME" \
|
||||
--pattern 'CLIProxyAPI_*.tar.gz' \
|
||||
--pattern 'CLIProxyAPI_*.zip' \
|
||||
--dir "$tmp_dir" \
|
||||
--clobber
|
||||
(
|
||||
cd "$tmp_dir"
|
||||
shopt -s nullglob
|
||||
archives=(CLIProxyAPI_*.tar.gz CLIProxyAPI_*.zip)
|
||||
if [[ ${#archives[@]} -eq 0 ]]; then
|
||||
echo "No release archives found"
|
||||
exit 0
|
||||
fi
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "${archives[@]}" | sort -k2 > checksums.txt
|
||||
else
|
||||
shasum -a 256 "${archives[@]}" | sort -k2 > checksums.txt
|
||||
fi
|
||||
)
|
||||
gh release upload "$GITHUB_REF_NAME" "$tmp_dir/checksums.txt" --clobber
|
||||
|
||||
publish-checksums:
|
||||
runs-on: ubuntu-latest
|
||||
if: always()
|
||||
needs:
|
||||
- build-hosted
|
||||
- build-linux-glibc
|
||||
- build-linux-no-plugin
|
||||
- build-freebsd
|
||||
steps:
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: dist
|
||||
merge-multiple: true
|
||||
- name: Publish final checksums
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
cd dist
|
||||
shopt -s nullglob
|
||||
archives=(CLIProxyAPI_*.tar.gz CLIProxyAPI_*.zip)
|
||||
if [[ ${#archives[@]} -eq 0 ]]; then
|
||||
echo "No release archives found"
|
||||
exit 0
|
||||
fi
|
||||
sha256sum "${archives[@]}" | sort -k2 > checksums.txt
|
||||
gh release upload "$GITHUB_REF_NAME" checksums.txt --clobber
|
||||
|
||||
+9
-2
@@ -1,6 +1,6 @@
|
||||
# Binaries
|
||||
cli-proxy-api
|
||||
cliproxy
|
||||
/cli-proxy-api
|
||||
/cliproxy
|
||||
proxypilot-linux
|
||||
proxypilot-darwin
|
||||
proxypilot-windows
|
||||
@@ -16,6 +16,8 @@ logs/*
|
||||
conv/*
|
||||
temp/*
|
||||
refs/*
|
||||
plugins/*
|
||||
examples/plugin/bin/*
|
||||
|
||||
# Storage backends
|
||||
pgstore/*
|
||||
@@ -33,6 +35,7 @@ memory/*
|
||||
|
||||
# Authentication data
|
||||
auths/*
|
||||
/auths
|
||||
!auths/.gitkeep
|
||||
|
||||
# Documentation
|
||||
@@ -58,12 +61,15 @@ GEMINI.md
|
||||
.worktrees/
|
||||
.codex/*
|
||||
.claude/*
|
||||
.claude
|
||||
.gemini/*
|
||||
.serena/*
|
||||
.agent/*
|
||||
.agents
|
||||
.agents/*
|
||||
.opencode/*
|
||||
.idea/*
|
||||
.beads/*
|
||||
.bmad/*
|
||||
_bmad/*
|
||||
_bmad-output/*
|
||||
@@ -84,6 +90,7 @@ tui-ttyd.png
|
||||
# macOS
|
||||
.DS_Store
|
||||
._*
|
||||
.gocache/
|
||||
|
||||
# Windows artifacts
|
||||
nul
|
||||
|
||||
@@ -27,6 +27,8 @@ builds:
|
||||
archives:
|
||||
- id: "cli-proxy-api-plus"
|
||||
format: tar.gz
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{- if eq .Arch "arm64" -}}aarch64{{- else -}}{{ .Arch }}{{- end -}}
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
15ac7fb9324095330e60f522147b8a8e81f16ab5
|
||||
@@ -0,0 +1 @@
|
||||
v7.0.2
|
||||
@@ -0,0 +1,58 @@
|
||||
# AGENTS.md
|
||||
|
||||
Go 1.26+ proxy server providing OpenAI/Gemini/Claude/Codex compatible APIs with OAuth and round-robin load balancing.
|
||||
|
||||
## Repository
|
||||
- GitHub: https://github.com/router-for-me/CLIProxyAPI
|
||||
|
||||
## Commands
|
||||
```bash
|
||||
gofmt -w . # Format (required after Go changes)
|
||||
go build -o cli-proxy-api ./cmd/server # Build
|
||||
go run ./cmd/server # Run dev server
|
||||
go test ./... # Run all tests
|
||||
go test -v -run TestName ./path/to/pkg # Run single test
|
||||
go build -o test-output ./cmd/server && rm test-output # Verify compile (REQUIRED after changes)
|
||||
```
|
||||
- Common flags: `--config <path>`, `--tui`, `--standalone`, `--local-model`, `--no-browser`, `--oauth-callback-port <port>`
|
||||
|
||||
## Config
|
||||
- Default config: `config.yaml` (template: `config.example.yaml`)
|
||||
- `.env` is auto-loaded from the working directory
|
||||
- Auth material defaults under `auths/`
|
||||
- Storage backends: file-based default; optional Postgres/git/object store (`PGSTORE_*`, `GITSTORE_*`, `OBJECTSTORE_*`)
|
||||
|
||||
## Architecture
|
||||
- `cmd/server/` — Server entrypoint
|
||||
- `internal/api/` — Gin HTTP API (routes, middleware, modules)
|
||||
- `internal/api/modules/amp/` — Amp integration (Amp-style routes + reverse proxy)
|
||||
- `internal/thinking/` — Main thinking/reasoning pipeline. `ApplyThinking()` (apply.go) parses suffixes (`suffix.go`, suffix overrides body), normalizes config to canonical `ThinkingConfig` (`types.go`), normalizes and validates centrally (`validate.go`/`convert.go`), then applies provider-specific output via `ProviderApplier`. Do not break this "canonical representation → per-provider translation" architecture.
|
||||
- `internal/runtime/executor/` — Per-provider runtime executors (incl. Codex WebSocket)
|
||||
- `internal/translator/` — Provider protocol translators (and shared `common`)
|
||||
- `internal/registry/` — Model registry + remote updater (`StartModelsUpdater`); `--local-model` disables remote updates
|
||||
- `internal/store/` — Storage implementations and secret resolution
|
||||
- `internal/managementasset/` — Config snapshots and management assets
|
||||
- `internal/cache/` — Request signature caching
|
||||
- `internal/watcher/` — Config hot-reload and watchers
|
||||
- `internal/wsrelay/` — WebSocket relay sessions
|
||||
- `internal/usage/` — Usage and token accounting
|
||||
- `internal/tui/` — Bubbletea terminal UI (`--tui`, `--standalone`)
|
||||
- `sdk/cliproxy/` — Embeddable SDK entry (service/builder/watchers/pipeline)
|
||||
- `test/` — Cross-module integration tests
|
||||
|
||||
## Code Conventions
|
||||
- Keep changes small and simple (KISS)
|
||||
- Comments in English only
|
||||
- If editing code that already contains non-English comments, translate them to English (don’t add new non-English comments)
|
||||
- For user-visible strings, keep the existing language used in that file/area
|
||||
- New Markdown docs should be in English unless the file is explicitly language-specific (e.g. `README_CN.md`)
|
||||
- As a rule, do not make standalone changes to `internal/translator/`. You may modify it only as part of broader changes elsewhere.
|
||||
- If a task requires changing only `internal/translator/`, run `gh repo view --json viewerPermission -q .viewerPermission` to confirm you have `WRITE`, `MAINTAIN`, or `ADMIN`. If you do, you may proceed; otherwise, file a GitHub issue including the goal, rationale, and the intended implementation code, then stop further work.
|
||||
- `internal/runtime/executor/` should contain executors and their unit tests only. Place any helper/supporting files under `internal/runtime/executor/helps/`.
|
||||
- Follow `gofmt`; keep imports goimports-style; wrap errors with context where helpful
|
||||
- Do not use `log.Fatal`/`log.Fatalf` (terminates the process); prefer returning errors and logging via logrus
|
||||
- Shadowed variables: use method suffix (`errStart := server.Start()`)
|
||||
- Wrap defer errors: `defer func() { if err := f.Close(); err != nil { log.Errorf(...) } }()`
|
||||
- Use logrus structured logging; avoid leaking secrets/tokens in logs
|
||||
- Avoid panics in HTTP handlers; prefer logged errors and meaningful HTTP status codes
|
||||
- Timeouts are allowed only during credential acquisition; after an upstream connection is established, do not set timeouts for any subsequent network behavior. Intentional exceptions that must remain allowed are the Codex websocket liveness deadlines in `internal/runtime/executor/codex_websockets_executor.go`, the wsrelay session deadlines in `internal/wsrelay/session.go`, the management APICall timeout in `internal/api/handlers/management/api_tools.go`, and the `cmd/fetch_antigravity_models` utility timeouts
|
||||
+11
-9
@@ -1,7 +1,9 @@
|
||||
FROM golang:1.26-alpine AS builder
|
||||
FROM golang:1.26-bookworm AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends build-essential git && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
@@ -12,19 +14,19 @@ ARG VERSION=dev
|
||||
ARG COMMIT=none
|
||||
ARG BUILD_DATE=unknown
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X 'main.Version=${VERSION}-plus' -X 'main.Commit=${COMMIT}' -X 'main.BuildDate=${BUILD_DATE}'" -o ./CLIProxyAPIPlus ./cmd/server/
|
||||
RUN CGO_ENABLED=1 GOOS=linux go build -buildvcs=false -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.Commit=${COMMIT}' -X 'main.BuildDate=${BUILD_DATE}'" -o ./CLIProxyAPI ./cmd/server/
|
||||
|
||||
FROM alpine:3.22.0
|
||||
FROM debian:bookworm
|
||||
|
||||
RUN apk add --no-cache tzdata
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends tzdata ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir /ProxyPilot
|
||||
RUN mkdir /CLIProxyAPI
|
||||
|
||||
COPY --from=builder ./app/CLIProxyAPIPlus /CLIProxyAPI/CLIProxyAPIPlus
|
||||
COPY --from=builder ./app/CLIProxyAPI /CLIProxyAPI/CLIProxyAPI
|
||||
|
||||
COPY config.example.yaml /ProxyPilot/config.example.yaml
|
||||
COPY config.example.yaml /CLIProxyAPI/config.example.yaml
|
||||
|
||||
WORKDIR /ProxyPilot
|
||||
WORKDIR /CLIProxyAPI
|
||||
|
||||
EXPOSE 8317
|
||||
|
||||
@@ -32,4 +34,4 @@ ENV TZ=Asia/Shanghai
|
||||
|
||||
RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime && echo "${TZ}" > /etc/timezone
|
||||
|
||||
CMD ["./CLIProxyAPIPlus"]
|
||||
CMD ["./CLIProxyAPI"]
|
||||
|
||||
@@ -1,376 +1,226 @@
|
||||
<p align="center">
|
||||
<img src="./static/icon.png" width="128" height="128" alt="ProxyPilot Logo">
|
||||
</p>
|
||||
# CLI Proxy API
|
||||
|
||||
<h1 align="center">ProxyPilot</h1>
|
||||
English | [中文](README_CN.md) | [日本語](README_JA.md)
|
||||
|
||||
<p align="center">
|
||||
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-28a745" alt="MIT License"></a>
|
||||
<a href="https://golang.org/"><img src="https://img.shields.io/badge/Go-1.24+-00ADD8?logo=go" alt="Go Version"></a>
|
||||
<a href="https://x.com/Finessse377721"><img src="https://img.shields.io/badge/Follow-%F0%9D%95%8F%2F%40Finessse377721-1c9bf0" alt="Follow on 𝕏"></a>
|
||||
<a href="https://github.com/Finesssee/ProxyPilot"><img src="https://img.shields.io/github/stars/Finesssee/ProxyPilot.svg?style=social&label=Star%20this%20repo" alt="Star this repo"></a>
|
||||
</p>
|
||||
A proxy server that provides OpenAI/Gemini/Claude/Codex/Grok compatible API interfaces for CLI.
|
||||
|
||||
<p align="center">
|
||||
<strong>Stop juggling API keys.</strong> ProxyPilot is a powerful local API proxy that lets you use your existing Claude Code, Codex, Gemini, Kiro, and Qwen subscriptions with any AI coding tool – no separate API keys required.
|
||||
</p>
|
||||
It now also supports OpenAI Codex (GPT models) and Claude Code via OAuth.
|
||||
|
||||
<p align="center">
|
||||
Built in Go, it handles OAuth authentication, token management, and API translation automatically. One server to route them all.
|
||||
</p>
|
||||
So you can use local or multi-account CLI access with OpenAI(include Responses)/Gemini/Claude-compatible clients and SDKs.
|
||||
|
||||
## Sponsor
|
||||
|
||||
[](https://www.packyapi.com/register?aff=cliproxyapi)
|
||||
|
||||
Thanks to PackyCode for sponsoring this project!
|
||||
|
||||
PackyCode is a reliable and efficient API relay service provider, offering relay services for Claude Code, Codex, Gemini, and more.
|
||||
|
||||
PackyCode provides special discounts for our software users: register using <a href="https://www.packyapi.com/register?aff=cliproxyapi">this link</a> and enter the "cliproxyapi" promo code during recharge to get 10% off.
|
||||
|
||||
---
|
||||
|
||||
> [!TIP]
|
||||
> 📣 **Latest models supported:**
|
||||
> Claude Opus 4.5 / Sonnet 4.5 with extended thinking, GPT-5.2 / GPT-5.2 Codex, Gemini 3 Pro/Flash, and Kiro (AWS CodeWhisperer)! 🚀
|
||||
|
||||
**Setup Guides:**
|
||||
- [Claude Code Setup →](docs/claude-code-local-proxy.md)
|
||||
- [Cursor IDE Setup →](docs/cursor-ide.md)
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td width="180"><a href="https://www.aicodemirror.com/register?invitecode=TJNAIF"><img src="./assets/aicodemirror.png" alt="AICodeMirror" width="150"></a></td>
|
||||
<td>Thanks to AICodeMirror for sponsoring this project! AICodeMirror provides official high-stability relay services for Claude Code / Codex / Gemini, with enterprise-grade concurrency, fast invoicing, and 24/7 dedicated technical support. Claude Code / Codex / Gemini official channels at 38% / 2% / 9% of original price, with extra discounts on top-ups! AICodeMirror offers special benefits for CLIProxyAPI users: register via <a href="https://www.aicodemirror.com/register?invitecode=TJNAIF">this link</a> to enjoy 20% off your first top-up, and enterprise customers can get up to 25% off!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://shop.bmoplus.com/?utm_source=github"><img src="./assets/bmoplus.png" alt="BmoPlus" width="150"></a></td>
|
||||
<td>Huge thanks to BmoPlus for sponsoring this project! BmoPlus is a highly reliable AI account provider built strictly for heavy AI users and developers. They offer rock-solid, ready-to-use accounts and official top-up services for ChatGPT Plus / ChatGPT Pro (Full Warranty) / Claude Pro / Super Grok / Gemini Pro. By registering and ordering through <a href="https://shop.bmoplus.com/?utm_source=github">BmoPlus - Premium AI Accounts & Top-ups</a>, users can unlock the mind-blowing rate of <b>10% of the official GPT subscription price (90% OFF)</b>!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://coder.visioncoder.cn"><img src="./assets/visioncoder.png" alt="VisionCoder" width="150"></a></td>
|
||||
<td>Thanks to VisionCoder for supporting this project. <a href="https://coder.visioncoder.cn">VisionCoder Developer Platform</a> is a reliable and efficient API relay service provider, offering access to mainstream AI models such as Claude Code, Codex, and Gemini. It helps developers and teams integrate AI capabilities more easily and improve productivity. Additionally, VisionCoder now offers retail channels for <b>Claude Max 200 and GPT Pro 200 premium accounts</b>, providing users with instant access to top-tier AI computing power and features.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://apikey.fun/register?aff=CLIProxyAPI"><img src="./assets/apikey.png" alt="APIKEY.FUN" width="150"></a></td>
|
||||
<td>Thanks to APIKEY.FUN for sponsoring this project! APIKEY.FUN is a professional enterprise-grade AI relay platform dedicated to providing stable, efficient, and low-cost AI model API access for enterprises and individual developers. The platform supports popular mainstream models such as Claude, OpenAI, and Gemini, with prices as low as 7% of the official price. Register through this project's <a href="https://apikey.fun/register?aff=CLIProxyAPI">exclusive link</a> to enjoy a special <b>permanent 5% top-up discount</b>.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://runapi.co/register?aff=FivD"><img src="./assets/runapi.png" alt="RunAPI" width="150"></a></td>
|
||||
<td>RunAPI is an efficient and stable API platform—an alternative to OpenRouter. A single API Key gives you access to 150+ leading models, including OpenAI, Claude, Gemini, DeepSeek, Grok, and more, at prices as low as 10% of the original (up to 90% off), with exceptional stability. It's seamlessly compatible with tools like Claude Code, OpenClaw, and others. RunAPI offers an exclusive perk for CPA users: <a href="https://runapi.co/register?aff=FivD">register</a> and contact an administrator to claim ¥7 in free credit.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://unity2.ai/register?source=cliproxyapi"><img src="./assets/unity2.jpg" alt="Unity2" width="150"></a></td>
|
||||
<td>Thanks to Unity2.ai for sponsoring this project! Unity2.ai is a high-performance AI model API relay platform for individual developers, teams, and enterprises. It has long served leading domestic enterprises, handles more than 30 billion token calls per day, and supports high concurrency at the 5000 RPM level. It supports balance billing, first top-up bonuses, bundled subscriptions, enterprise invoicing, and dedicated integration support. Register through <a href="https://unity2.ai/register?source=cliproxyapi">this link</a> to receive a $2 balance, then join the official group to get another $10 balance, for up to $12 in free credit.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://catapi.ai/sign-up"><img src="./assets/catapi.png" alt="CatAPI" width="150"></a></td>
|
||||
<td>Cat API is an AI model aggregation platform built for individual developers and teams, integrating leading large language models into a single simple, stable, and easy-to-use entry point. It provides an API fully compatible with OpenAI, Claude, and Gemini that plugs seamlessly into mainstream AI IDEs and coding tools such as Claude Code, Cursor, Windsurf, Cline, Roo Code, Continue, Codex, and Trae, and features dedicated CN2 high-speed routing for low-latency, highly reliable access. <a href="https://catapi.ai/sign-up">Sign up</a> to claim 1$ in free credits.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
---
|
||||
## Overview
|
||||
|
||||
## Features
|
||||
|
||||
- 🎯 **10 Auth Providers** - Claude, Codex (OpenAI), Gemini, Gemini CLI, Kiro (AWS), Amazon Q CLI, Qwen, Antigravity, MiniMax, Zhipu AI
|
||||
- 🔄 **Universal API Translation** - Auto-converts between OpenAI, Anthropic, and Gemini formats
|
||||
- 🔧 **Tool Calling Repair** - Fixes tool/function call mismatches between providers automatically
|
||||
- 🧠 **Extended Thinking** - Full support for Claude and Gemini thinking models
|
||||
- 🔐 **OAuth Integration** - Browser-based login with automatic token refresh
|
||||
- 👥 **Multi-Account Support** - Round-robin distribution with automatic failover
|
||||
- ⚡ **Quota Auto-Switch** - Automatically switches to backup project/model when quota exceeded
|
||||
- 📊 **Usage Statistics** - Track requests, tokens, and errors per provider/model
|
||||
- 🧩 **Context Compression** - LLM-based summarization for long sessions (Factory.ai research)
|
||||
- 🤖 **Agentic Harness** - Guided workflow for coding agents (Anthropic research)
|
||||
- 💾 **Session Memory** - Persistent storage across conversation turns
|
||||
- 🎨 **System Tray** - Native Windows tray app for quick access
|
||||
- 🔄 **Auto-Updates** - Background update checking with one-click install
|
||||
- ⏪ **Rollback Support** - Automatic crash detection with version recovery
|
||||
- 📡 **60+ Management APIs** - Full control via REST endpoints
|
||||
|
||||
---
|
||||
|
||||
## Supported Providers
|
||||
|
||||
| Provider | Auth Method | Models |
|
||||
|----------|-------------|--------|
|
||||
| Claude (Anthropic) | OAuth2 / API Key | Claude Opus 4.5, Sonnet 4.5, Haiku 4.5 |
|
||||
| Codex (OpenAI) | OAuth2 / API Key | GPT-5.2, GPT-5.2 Codex |
|
||||
| Gemini | OAuth2 / API Key | Gemini 3 Pro, Gemini 3 Flash |
|
||||
| Gemini CLI | OAuth2 | Cloud Code Assist (separate quota) |
|
||||
| Kiro | OAuth2 + AWS SSO | AWS CodeWhisperer |
|
||||
| Amazon Q CLI | Import from CLI | Amazon Q Developer |
|
||||
| Qwen | OAuth2 | Qwen models |
|
||||
| Antigravity | OAuth2 | Gemini via Antigravity (separate quota) |
|
||||
| MiniMax | API Key | MiniMax M2, M2.1 models |
|
||||
| Zhipu AI | API Key | GLM-4.5, GLM-4.6, GLM-4.7 |
|
||||
| Custom | API Key | Any OpenAI-compatible endpoint |
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
### Download Pre-built Release (Recommended)
|
||||
|
||||
1. Go to the [**Releases**](https://github.com/Finesssee/ProxyPilot/releases) page
|
||||
2. Download the latest binary for your platform
|
||||
3. Run `./proxypilot`
|
||||
|
||||
### Build from Source
|
||||
|
||||
```bash
|
||||
git clone https://github.com/Finesssee/ProxyPilot.git
|
||||
cd ProxyPilot
|
||||
go build -o proxypilot ./cmd/server
|
||||
./proxypilot
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
### First Launch
|
||||
|
||||
1. Packaged installs create `config.yaml` automatically on first start when `config.example.yaml` is bundled next to the executable.
|
||||
2. If you're running from source, copy config: `cp config.example.yaml config.yaml`
|
||||
3. Run: `./proxypilot`
|
||||
4. Server starts on `http://localhost:8317`
|
||||
5. Open dashboard: `http://localhost:8317/management.html`
|
||||
|
||||
### Authentication
|
||||
|
||||
Run OAuth login for your provider:
|
||||
|
||||
```bash
|
||||
# OAuth providers (opens browser)
|
||||
./proxypilot --claude-login # Claude
|
||||
./proxypilot --codex-login # OpenAI/Codex
|
||||
./proxypilot --login # Gemini
|
||||
./proxypilot --kiro-login # Kiro (Google OAuth)
|
||||
./proxypilot --kiro-aws-login # Kiro (AWS Builder ID)
|
||||
./proxypilot --qwen-login # Qwen
|
||||
./proxypilot --antigravity-login # Antigravity
|
||||
|
||||
# Import providers (from existing CLI tools)
|
||||
./proxypilot --kiro-import # Kiro IDE token
|
||||
|
||||
# API key providers (prompts for key)
|
||||
./proxypilot --minimax-login # MiniMax API key
|
||||
./proxypilot --zhipu-login # Zhipu AI API key
|
||||
```
|
||||
|
||||
OAuth tokens are stored locally and auto-refreshed before expiry.
|
||||
|
||||
### Security Defaults (Auth + CORS)
|
||||
|
||||
- Proxy requests require API keys by default. To allow unauthenticated access (not recommended), set `allow-unauthenticated: true` in `config.yaml`.
|
||||
- CORS is enabled for non-management endpoints by default (wildcard `*`). Management endpoints **do not** emit CORS headers unless you explicitly allow origins under `cors.management-allow-origins`.
|
||||
|
||||
Example:
|
||||
|
||||
```yaml
|
||||
allow-unauthenticated: false
|
||||
cors:
|
||||
allow-origins:
|
||||
- "http://localhost:5173"
|
||||
management-allow-origins:
|
||||
- "http://localhost:5173"
|
||||
```
|
||||
|
||||
### Configure Your Tools
|
||||
|
||||
**Claude Code** (`~/.claude/settings.json`):
|
||||
```json
|
||||
{
|
||||
"env": {
|
||||
"ANTHROPIC_BASE_URL": "http://127.0.0.1:8317",
|
||||
"ANTHROPIC_AUTH_TOKEN": "your-api-key"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Codex CLI** (`~/.codex/config.toml`):
|
||||
```toml
|
||||
[openai]
|
||||
api_base_url = "http://127.0.0.1:8317"
|
||||
```
|
||||
|
||||
**Factory Droid** (`~/.factory/settings.json`):
|
||||
```json
|
||||
{
|
||||
"customModels": [{
|
||||
"name": "ProxyPilot",
|
||||
"baseUrl": "http://127.0.0.1:8317"
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API Endpoints
|
||||
|
||||
```
|
||||
POST /v1/chat/completions # OpenAI Chat Completions
|
||||
POST /v1/responses # OpenAI Responses API
|
||||
POST /v1/messages # Anthropic Messages API
|
||||
GET /v1/models # List available models
|
||||
GET /healthz # Health check
|
||||
```
|
||||
|
||||
All endpoints auto-translate between formats based on the target provider.
|
||||
|
||||
---
|
||||
|
||||
## Caching
|
||||
|
||||
ProxyPilot includes two caching layers to reduce latency and token usage.
|
||||
- OpenAI/Gemini/Claude/Grok compatible API endpoints for CLI models
|
||||
- OpenAI Codex support (GPT models) via OAuth login
|
||||
- Claude Code support via OAuth login
|
||||
- Grok Build support via OAuth login
|
||||
- Streaming, non-streaming, and WebSocket responses where supported
|
||||
- Function calling/tools support
|
||||
- Multimodal input support (text and images)
|
||||
- Multiple accounts with round-robin load balancing (Gemini, OpenAI, Claude, Grok)
|
||||
- Simple CLI authentication flows (Gemini, OpenAI, Claude, Grok)
|
||||
- Generative Language API Key support
|
||||
- AI Studio Build multi-account load balancing
|
||||
- Claude Code multi-account load balancing
|
||||
- OpenAI Codex multi-account load balancing
|
||||
- Grok Build multi-account load balancing
|
||||
- OpenAI-compatible upstream providers via config (e.g., OpenRouter)
|
||||
- Reusable Go SDK for embedding the proxy (see `docs/sdk-usage.md`)
|
||||
|
||||
### Response Cache
|
||||
## Getting Started
|
||||
|
||||
Caches full API responses for identical requests. Useful for repeated queries during development.
|
||||
|
||||
**Config** (`config.yaml`):
|
||||
```yaml
|
||||
response-cache:
|
||||
enabled: true # Default: false
|
||||
max-size: 1000 # Max entries (default: 1000)
|
||||
max-bytes: 0 # Optional total cache size cap in bytes
|
||||
ttl-seconds: 300 # Cache TTL (default: 300 = 5 min)
|
||||
exclude-models: # Models to skip (supports wildcards)
|
||||
- "*-thinking"
|
||||
- "o1-*"
|
||||
```
|
||||
|
||||
### Prompt Cache
|
||||
|
||||
Synthetic prompt caching for providers without native support. Tracks repeated system prompts and estimates token savings.
|
||||
CLIProxyAPI Guides: [https://help.router-for.me/](https://help.router-for.me/)
|
||||
|
||||
**Config** (`config.yaml`):
|
||||
```yaml
|
||||
prompt-cache:
|
||||
enabled: true # Default: false
|
||||
max-size: 500 # Max entries (default: 500)
|
||||
max-bytes: 0 # Optional total cache size cap in bytes
|
||||
ttl-seconds: 1800 # Cache TTL (default: 1800 = 30 min)
|
||||
```
|
||||
## Management API
|
||||
|
||||
### Cache Management API
|
||||
see [MANAGEMENT_API.md](https://help.router-for.me/management/api)
|
||||
|
||||
| Endpoint | Method | Description |
|
||||
|----------|--------|-------------|
|
||||
| `/v0/management/cache/stats` | GET | Response cache stats (hits, misses, size) |
|
||||
| `/v0/management/cache/clear` | POST | Clear response cache |
|
||||
| `/v0/management/cache/enabled` | PUT | Enable/disable at runtime `{"enabled": true}` |
|
||||
| `/v0/management/prompt-cache/stats` | GET | Prompt cache stats + estimated tokens saved |
|
||||
| `/v0/management/prompt-cache/clear` | POST | Clear prompt cache |
|
||||
| `/v0/management/prompt-cache/enabled` | PUT | Enable/disable at runtime |
|
||||
| `/v0/management/prompt-cache/top` | GET | Top 10 most-hit prompts |
|
||||
## Usage Statistics
|
||||
|
||||
---
|
||||
Since v6.10.0, CLIProxyAPI and [CPAMC](https://github.com/router-for-me/Cli-Proxy-API-Management-Center) no longer ship built-in usage statistics. If you need usage statistics, use:
|
||||
|
||||
## Lightweight Profile
|
||||
### [CPA Usage Keeper](https://github.com/Willxup/cpa-usage-keeper)
|
||||
|
||||
For low‑memory or high‑throughput setups, you can disable heavier features:
|
||||
Standalone persistence and visualization service for CLIProxyAPI, with periodic data sync, SQLite storage, aggregate APIs, and a built-in dashboard for usage and statistics.
|
||||
|
||||
```yaml
|
||||
commercial-mode: true
|
||||
usage-statistics-enabled: false
|
||||
usage-sample-rate: 0.25
|
||||
metrics-enabled: false
|
||||
request-history-enabled: false
|
||||
request-history-sample-rate: 0.25
|
||||
agentic-harness-enabled: false
|
||||
prompt-budget-enabled: false
|
||||
request-log: false
|
||||
response-cache:
|
||||
enabled: false
|
||||
prompt-cache:
|
||||
enabled: false
|
||||
```
|
||||
### [CPA-Manager-Plus](https://github.com/seakee/CPA-Manager-Plus)
|
||||
|
||||
---
|
||||
Full CLIProxyAPI management center with request-level monitoring and cost estimates. CPA-Manager tracks collected requests by account, model, channel, latency, status, and token usage; estimates cost with editable model prices and one-click LiteLLM price sync; persists events in SQLite; and provides Codex account-pool operations with batch inspection, quota detection, unhealthy account discovery, cleanup suggestions, and one-click execution for day-to-day multi-account maintenance.
|
||||
|
||||
## Auto-Updates
|
||||
## SDK Docs
|
||||
|
||||
ProxyPilot includes a built-in auto-update system that checks for new releases and allows one-click installation.
|
||||
- Usage: [docs/sdk-usage.md](docs/sdk-usage.md)
|
||||
- Advanced (executors & translators): [docs/sdk-advanced.md](docs/sdk-advanced.md)
|
||||
- Access: [docs/sdk-access.md](docs/sdk-access.md)
|
||||
- Watcher: [docs/sdk-watcher.md](docs/sdk-watcher.md)
|
||||
- Custom Provider Example: `examples/custom-provider`
|
||||
|
||||
### Configuration
|
||||
## Contributing
|
||||
|
||||
**Config** (`config.yaml`):
|
||||
```yaml
|
||||
updates:
|
||||
auto-check: true # Enable background update checking (default: true)
|
||||
check-interval-hours: 24 # How often to check (default: 24)
|
||||
notify-on-update: true # Show toast notification when update available (default: true)
|
||||
channel: stable # Update channel: "stable" or "prerelease" (default: stable)
|
||||
```
|
||||
Contributions are welcome! Please feel free to submit a Pull Request.
|
||||
|
||||
### Features
|
||||
1. Fork the repository
|
||||
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
||||
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
||||
4. Push to the branch (`git push origin feature/amazing-feature`)
|
||||
5. Open a Pull Request
|
||||
|
||||
- **Background Polling** - Checks for updates at configurable intervals
|
||||
- **Dashboard Banner** - Proactive notification when update is available
|
||||
- **One-Click Install** - Download, verify, and install from the tray menu
|
||||
- **Session Dismissal** - Dismissed banners won't reappear until next session
|
||||
## Who is with us?
|
||||
|
||||
### Tray Menu
|
||||
Those projects are based on CLIProxyAPI:
|
||||
|
||||
When an update is available:
|
||||
- **Settings → Download & Install vX.X.X** - One-click update flow
|
||||
- **Settings → Check for Updates** - Manual check
|
||||
### [vibeproxy](https://github.com/automazeio/vibeproxy)
|
||||
|
||||
---
|
||||
Native macOS menu bar app to use your Claude Code & ChatGPT subscriptions with AI coding tools - no API keys needed
|
||||
|
||||
## Rollback Support
|
||||
### [Subtitle Translator](https://github.com/VjayC/SRT-Subtitle-Translator-Validator)
|
||||
|
||||
ProxyPilot automatically backs up the previous version during updates and can restore it if something goes wrong.
|
||||
A cross-platform desktop and web app to translate and validate SRT subtitles using your existing LLM subscriptions (Gemini, ChatGPT, Claude, etc.) via CLIProxyAPI - no API keys needed.
|
||||
|
||||
### Automatic Recovery
|
||||
### [CCS (Claude Code Switch)](https://github.com/kaitranntt/ccs)
|
||||
|
||||
- **Crash Detection** - Tracks rapid restarts within a 30-second window
|
||||
- **Auto-Rollback** - After 3 rapid failures, automatically restores previous version
|
||||
- **Health Marking** - After 30 seconds of stable operation, clears the failure counter
|
||||
CLI wrapper for instant switching between multiple Claude accounts and alternative models (Gemini, Codex, Antigravity) via CLIProxyAPI OAuth - no API keys needed
|
||||
|
||||
### Manual Rollback
|
||||
### [Quotio](https://github.com/nguyenphutrong/quotio)
|
||||
|
||||
From the tray menu:
|
||||
- **Settings → Rollback to Previous Version** - Restore the previous version
|
||||
Native macOS menu bar app that unifies Claude, Gemini, OpenAI, and Antigravity subscriptions with real-time quota tracking and smart auto-failover for AI coding tools like Claude Code, OpenCode, and Droid - no API keys needed.
|
||||
|
||||
### How It Works
|
||||
### [ProxyPilot](https://github.com/Finesssee/ProxyPilot)
|
||||
|
||||
1. During update, current binary is saved as `.old` backup
|
||||
2. Rollback metadata stored in `%APPDATA%/ProxyPilot/updates/`
|
||||
3. On crash loop detection, previous version is automatically restored
|
||||
4. After successful startup (30s), backup can be cleaned up
|
||||
Windows-native CLIProxyAPI fork with TUI, system tray, and multi-provider OAuth for AI coding tools - no API keys needed.
|
||||
|
||||
---
|
||||
### [Claude Proxy VSCode](https://github.com/uzhao/claude-proxy-vscode)
|
||||
|
||||
## CLI Tools
|
||||
VSCode extension for quick switching between Claude Code models, featuring integrated CLIProxyAPI as its backend with automatic background lifecycle management.
|
||||
|
||||
| Binary | Description |
|
||||
|--------|-------------|
|
||||
| `proxypilot` | Main CLI with server and config switching |
|
||||
| `proxypilot-tray` | System tray app |
|
||||
### [ZeroLimit](https://github.com/0xtbug/zero-limit)
|
||||
|
||||
---
|
||||
Windows desktop app built with Tauri + React for monitoring AI coding assistant quotas via CLIProxyAPI. Track usage across Gemini, Claude, OpenAI Codex, and Antigravity accounts with real-time dashboard, system tray integration, and one-click proxy control - no API keys needed.
|
||||
|
||||
## Tool Integrations
|
||||
### [CPA-XXX Panel](https://github.com/ferretgeek/CPA-X)
|
||||
|
||||
Works with these AI coding tools:
|
||||
A lightweight web admin panel for CLIProxyAPI with health checks, resource monitoring, real-time logs, auto-update, request statistics and pricing display. Supports one-click installation and systemd service.
|
||||
|
||||
- **Claude Code** - Auto-configure via settings.json
|
||||
- **Codex CLI** - Auto-configure via config.toml
|
||||
- **Factory Droid** - Auto-configure via settings.json
|
||||
- **Cursor IDE** - Manual endpoint configuration
|
||||
- **Continue** - Manual endpoint configuration
|
||||
### [CLIProxyAPI Tray](https://github.com/kitephp/CLIProxyAPI_Tray)
|
||||
|
||||
---
|
||||
A Windows tray application implemented using PowerShell scripts, without relying on any third-party libraries. The main features include: automatic creation of shortcuts, silent running, password management, channel switching (Main / Plus), and automatic downloading and updating.
|
||||
|
||||
## Requirements
|
||||
### [霖君](https://github.com/wangdabaoqq/LinJun)
|
||||
|
||||
- macOS, Linux, or Windows
|
||||
- Go 1.24+ (for building from source)
|
||||
霖君 is a cross-platform desktop application for managing AI programming assistants, supporting macOS, Windows, and Linux systems. Unified management of Claude Code, Gemini, OpenAI Codex, and other AI coding tools, with local proxy for multi-account quota tracking and one-click configuration.
|
||||
|
||||
---
|
||||
### [CLIProxyAPI Dashboard](https://github.com/itsmylife44/cliproxyapi-dashboard)
|
||||
|
||||
## Author
|
||||
A modern web-based management dashboard for CLIProxyAPI built with Next.js, React, and PostgreSQL. Features real-time log streaming, structured configuration editing, API key management, OAuth provider integration for Claude/Gemini/Codex, usage analytics, container management, and config sync with OpenCode via companion plugin - no manual YAML editing needed.
|
||||
|
||||
ProxyPilot is developed and maintained by [@Finesssee](https://github.com/Finesssee). Some contributors shown in the git history are from upstream [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) merges. Direct contributors to ProxyPilot will be listed here as the project grows.
|
||||
### [All API Hub](https://github.com/qixing-jk/all-api-hub)
|
||||
|
||||
---
|
||||
Browser extension for one-stop management of New API-compatible relay site accounts, featuring balance and usage dashboards, auto check-in, one-click key export to common apps, in-page API availability testing, and channel/model sync and redirection. It integrates with CLIProxyAPI through the Management API for one-click provider import and config sync.
|
||||
|
||||
## Credits
|
||||
### [Shadow AI](https://github.com/HEUDavid/shadow-ai)
|
||||
|
||||
ProxyPilot builds upon excellent work from the open-source community:
|
||||
Shadow AI is an AI assistant tool designed specifically for restricted environments. It provides a stealthy operation
|
||||
mode without windows or traces, and enables cross-device AI Q&A interaction and control via the local area network (
|
||||
LAN). Essentially, it is an automated collaboration layer of "screen/audio capture + AI inference + low-friction delivery",
|
||||
helping users to immersively use AI assistants across applications on controlled devices or in restricted environments.
|
||||
|
||||
- **[CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI)** - The original unified proxy server that inspired this project
|
||||
- **[VibeProxy](https://github.com/automazeio/vibeproxy)** - Native macOS menu bar app showcasing clean proxy UX
|
||||
### [ProxyPal](https://github.com/buddingnewinsights/proxypal)
|
||||
|
||||
Long-context features are inspired by research from the AI community:
|
||||
Cross-platform desktop app (macOS, Windows, Linux) wrapping CLIProxyAPI with a native GUI. Connects Claude, ChatGPT, Gemini, GitHub Copilot, and custom OpenAI-compatible endpoints with usage analytics, request monitoring, and auto-configuration for popular coding tools - no API keys needed.
|
||||
|
||||
- **[Factory.ai](https://factory.ai/news/evaluating-compression)** - Context compression techniques for long-running coding agents
|
||||
- **[Anthropic](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents)** - Effective harnesses for long-running agents
|
||||
### [CLIProxyAPI Quota Inspector](https://github.com/AllenReder/CLIProxyAPI-Quota-Inspector)
|
||||
|
||||
Special thanks to these teams for sharing their work and insights.
|
||||
Ready-to-use cross-platform quota inspector for CLIProxyAPI, supporting per-account codex 5h/7d quota windows, plan-based sorting, status coloring, and multi-account summary analytics.
|
||||
|
||||
---
|
||||
### [CLIProxy Pool Watch](https://github.com/murasame612/CLIProxyPoolWidget)
|
||||
|
||||
Native macOS SwiftUI app for monitoring ChatGPT/Codex account quotas in CLIProxyAPI pools. Displays account availability, Plus-base capacity, 5-hour and weekly quota bars, plan weights, and restore forecasts through the Management API.
|
||||
|
||||
### [Panopticon](https://github.com/eltmon/panopticon-cli)
|
||||
|
||||
Multi-agent orchestration for AI coding assistants. Runs CLIProxyAPI as a local sidecar so its agents can drive GPT models through a ChatGPT subscription, pointing Claude Code at an Anthropic-compatible endpoint with no OpenAI API key required.
|
||||
|
||||
### [Tunnel Agent](https://github.com/Villoh/tunnel-agent)
|
||||
|
||||
Windows desktop UI that manages CLIProxyAPI and Perplexity WebUI Scraper from a single interface, inspired by Quotio and VibeProxy. Connect OAuth providers (Claude, Gemini, Codex, Kimi, Antigravity), custom API keys, and Perplexity session accounts, then point any coding agent at the local endpoint.
|
||||
|
||||
### [Quotio Desktop](https://github.com/xiaocoss/quotio-desktop)
|
||||
|
||||
Cross-platform (Tauri) port of Quotio for Windows, macOS and Linux. Manages a pool of AI accounts (Codex, Claude Code, GitHub Copilot, Gemini, Antigravity, Kiro, Cursor, Trae, GLM) through CLIProxyAPI, with per-account 5-hour/weekly quota bars, Codex rate-limit reset credits with one-click reset, smart scheduling, usage statistics, and multi-instance Codex — no API keys needed.
|
||||
|
||||
> [!NOTE]
|
||||
> If you developed a project based on CLIProxyAPI, please open a PR to add it to this list.
|
||||
|
||||
## More choices
|
||||
|
||||
Those projects are ports of CLIProxyAPI or inspired by it:
|
||||
|
||||
### [9Router](https://github.com/decolua/9router)
|
||||
|
||||
A Next.js implementation inspired by CLIProxyAPI, easy to install and use, built from scratch with format translation (OpenAI/Claude/Gemini/Ollama), combo system with auto-fallback, multi-account management with exponential backoff, a Next.js web dashboard, and support for CLI tools (Cursor, Claude Code, Cline, RooCode) - no API keys needed.
|
||||
|
||||
### [OmniRoute](https://github.com/diegosouzapw/OmniRoute)
|
||||
|
||||
Never stop coding. Smart routing to FREE & low-cost AI models with automatic fallback.
|
||||
|
||||
OmniRoute is an AI gateway for multi-provider LLMs: an OpenAI-compatible endpoint with smart routing, load balancing, retries, and fallbacks. Add policies, rate limits, caching, and observability for reliable, cost-aware inference.
|
||||
|
||||
### [Playful Proxy API Panel (PPAP)](https://github.com/daishuge/playful-proxy-api-panel)
|
||||
|
||||
A public CLIProxyAPI-compatible fork and bundled management panel. It keeps upstream-style usage while restoring built-in usage statistics, adding cache hit rate, first-byte latency, TPS tracking, and Docker-oriented self-hosted installation docs.
|
||||
|
||||
### [Codex Switch](https://github.com/9ycrooked/CodexSwitch)
|
||||
|
||||
This is a tool built with Tauri 2 + Vue 3 for managing multiple OpenAI Codex desktop accounts. Switch between saved ChatGPT/Codex certification profiles, check 5-hour and weekly quota usage in real time, verify token health, view active account details, and import or save auth.json files without manual copying.
|
||||
|
||||
> [!NOTE]
|
||||
> If you have developed a port of CLIProxyAPI or a project inspired by it, please open a PR to add it to this list.
|
||||
|
||||
## License
|
||||
|
||||
MIT License - see [LICENSE](LICENSE) for details.
|
||||
|
||||
---
|
||||
|
||||
## Support
|
||||
|
||||
- **Report Issues**: [GitHub Issues](https://github.com/Finesssee/ProxyPilot/issues)
|
||||
|
||||
---
|
||||
|
||||
## Disclaimer
|
||||
|
||||
This project is for educational and interoperability research purposes. It interacts with various APIs to provide compatibility layers.
|
||||
|
||||
- **Use at your own risk.** Authors are not responsible for account suspensions or service interruptions.
|
||||
- **Not affiliated** with Google, OpenAI, Anthropic, Amazon, or any other provider.
|
||||
- Users must comply with the Terms of Service of connected platforms.
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
# CLI 代理 API
|
||||
|
||||
[English](README.md) | 中文 | [日本語](README_JA.md)
|
||||
|
||||
一个为 CLI 提供 OpenAI/Gemini/Claude/Codex/Grok 兼容 API 接口的代理服务器。
|
||||
|
||||
现已支持通过 OAuth 登录接入 OpenAI Codex(GPT 系列)和 Claude Code。
|
||||
|
||||
您可以使用本地或多账户的CLI方式,通过任何与 OpenAI(包括Responses)/Gemini/Claude 兼容的客户端和SDK进行访问。
|
||||
|
||||
## 赞助商
|
||||
|
||||
[](https://www.packyapi.com/register?aff=cliproxyapi)
|
||||
|
||||
感谢 PackyCode 对本项目的赞助!
|
||||
|
||||
PackyCode 是一家可靠高效的 API 中转服务商,提供 Claude Code、Codex、Gemini 等多种服务的中转。
|
||||
|
||||
PackyCode 为本软件用户提供了特别优惠:使用<a href="https://www.packyapi.com/register?aff=cliproxyapi" target="_blank">此链接</a>注册,并在充值时输入 "cliproxyapi" 优惠码即可享受九折优惠。
|
||||
|
||||
---
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td width="180"><a href="https://www.aicodemirror.com/register?invitecode=TJNAIF"><img src="./assets/aicodemirror.png" alt="AICodeMirror" width="150"></a></td>
|
||||
<td>感谢 AICodeMirror 赞助了本项目!AICodeMirror 提供 Claude Code / Codex / Gemini 官方高稳定中转服务,支持企业级高并发、极速开票、7×24 专属技术支持。 Claude Code / Codex / Gemini 官方渠道低至 3.8 / 0.2 / 0.9 折,充值更有折上折!AICodeMirror 为 CLIProxyAPI 的用户提供了特别福利,通过<a href="https://www.aicodemirror.com/register?invitecode=TJNAIF" target="_blank">此链接</a>注册的用户,可享受首充8折,企业客户最高可享 7.5 折!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://shop.bmoplus.com/?utm_source=github"><img src="./assets/bmoplus.png" alt="BmoPlus" width="150"></a></td>
|
||||
<td>感谢 BmoPlus 赞助了本项目!BmoPlus 是一家专为AI订阅重度用户打造的可靠 AI 账号代充服务商,提供稳定的 ChatGPT Plus / ChatGPT Pro(全程质保) / Claude Pro / Super Grok / Gemini Pro 的官方代充&成品账号。 通过<a href="https://shop.bmoplus.com/?utm_source=github" target="_blank">BmoPlus AI成品号专卖/代充</a>注册下单的用户,可享GPT <b>官网订阅一折</b> 的震撼价格!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://coder.visioncoder.cn"><img src="./assets/visioncoder.png" alt="VisionCoder" width="150"></a></td>
|
||||
<td>感谢 VisionCoder 对本项目的支持。<a href="https://coder.visioncoder.cn">VisionCoder 开发平台</a> 是一个可靠高效的 API 中继服务提供商,提供 Claude Code、Codex、Gemini 等主流 AI 模型,帮助开发者和团队更轻松地集成 AI 功能,提升工作效率。此外,VisionCoder 还提供 <b>Claude Max 200 与 GPT Pro 200 高级成品号</b>的独家售卖渠道,助力体验全网顶配 AI 的算力与体验。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://apikey.fun/register?aff=CLIProxyAPI"><img src="./assets/apikey.png" alt="APIKEY.FUN" width="150"></a></td>
|
||||
<td>感谢 APIKEY.FUN 赞助本项目!APIKEY.FUN 是一家专业的企业级 AI 中转站,致力于为企业和个人开发者提供稳定、高效、低成本的 AI 模型 API 接入服务。平台支持 Claude、OpenAI、Gemini 等主流热门模型,价格低至官方原价的 7%。通过本项目<a href="https://apikey.fun/register?aff=CLIProxyAPI">专属链接</a>注册,还可享受最高 <b>充值永久 95 折</b> 专属优惠。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://runapi.co/register?aff=FivD"><img src="./assets/runapi.png" alt="RunAPI" width="150"></a></td>
|
||||
<td>RunAPI 是高效稳定的API OpenRouter平替平台,一个 API Key 即可访问 OpenAI、Claude、Gemini、DeepSeek、Grok 等 150+ 主流模型,低至 1 折,极其稳定,可以无缝兼容 Claude Code、OpenClaw 等工具。RunAPI 为 CPA的用户提供专属福利:<a href="https://runapi.co/register?aff=FivD">注册</a>联系管理员即可领取¥7的免费额度</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://unity2.ai/register?source=cliproxyapi"><img src="./assets/unity2.jpg" alt="Unity2" width="150"></a></td>
|
||||
<td>感谢 Unity2.ai 赞助了本项目!Unity2.ai 是面向个人开发者、团队和企业的高性能 AI 模型 API 中转平台,长期服务国内头部企业,日均承载超 300 亿 token 调用,支持 5000 RPM 级高并发。支持余额计费、首充赠额、组合订阅、企业开票和专属对接。通过<a href="https://unity2.ai/register?source=cliproxyapi">此链接</a>注册可领取 $2 余额,加入官方群再送 $10 余额,最高可领 $12 免费额度。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://catapi.ai/sign-up"><img src="./assets/catapi.png" alt="CatAPI" width="150"></a></td>
|
||||
<td>Cat API 是一家面向个人开发者与团队的 AI 大模型聚合平台,致力于将主流大模型能力整合到一个简单、稳定、易用的入口中。平台提供完全兼容 OpenAI、Claude、Gemini 的 API,可无缝接入 Claude Code、Cursor、Windsurf、Cline、Roo Code、Continue、Codex、Trae 等主流 AI IDE 与编程工具,并主打 CN2 高速线路,为用户带来低延迟、高稳定的访问体验。<a href="https://catapi.ai/sign-up">注册</a>即可领取 1$ 的免费额度。</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 为 CLI 模型提供 OpenAI/Gemini/Claude/Codex/Grok 兼容的 API 端点
|
||||
- 新增 OpenAI Codex(GPT 系列)支持(OAuth 登录)
|
||||
- 新增 Claude Code 支持(OAuth 登录)
|
||||
- 新增 Grok Build 支持(OAuth 登录)
|
||||
- 支持流式、非流式响应,以及受支持场景下的 WebSocket 响应
|
||||
- 函数调用/工具支持
|
||||
- 多模态输入(文本、图片)
|
||||
- 多账户支持与轮询负载均衡(Gemini、OpenAI、Claude、Grok)
|
||||
- 简单的 CLI 身份验证流程(Gemini、OpenAI、Claude、Grok)
|
||||
- 支持 Gemini AIStudio API 密钥
|
||||
- 支持 AI Studio Build 多账户轮询
|
||||
- 支持 Claude Code 多账户轮询
|
||||
- 支持 OpenAI Codex 多账户轮询
|
||||
- 支持 Grok Build 多账户轮询
|
||||
- 通过配置接入上游 OpenAI 兼容提供商(例如 OpenRouter)
|
||||
- 可复用的 Go SDK(见 `docs/sdk-usage_CN.md`)
|
||||
|
||||
## 新手入门
|
||||
|
||||
CLIProxyAPI 用户手册: [https://help.router-for.me/](https://help.router-for.me/cn/)
|
||||
|
||||
## 管理 API 文档
|
||||
|
||||
请参见 [MANAGEMENT_API_CN.md](https://help.router-for.me/cn/management/api)
|
||||
|
||||
## 使用量统计
|
||||
|
||||
自v6.10.0版本以后,CLIProxyAPI及 [CPAMC](https://github.com/router-for-me/Cli-Proxy-API-Management-Center) 项目不再预置数据统计功能,如果有数据统计需求的请使用以下项目:
|
||||
|
||||
### [CPA Usage Keeper](https://github.com/Willxup/cpa-usage-keeper)
|
||||
|
||||
独立的 CLIProxyAPI 使用量持久化与可视化服务,定期同步 CLIProxyAPI 数据,存储到 SQLite,提供聚合 API,并内置使用量分析与统计仪表盘。
|
||||
|
||||
### [CPA-Manager-Plus](https://github.com/seakee/CPA-Manager-Plus)
|
||||
|
||||
面向 CLIProxyAPI 的完整管理中心,提供请求级监控和费用预估。CPA-Manager 可按账号、模型、渠道、延迟、状态和 token 用量追踪采集到的请求;支持可编辑模型价格与一键同步 LiteLLM 价格来估算费用;用 SQLite 持久化事件;并提供面向 Codex 账号池的批量巡检、配额识别、异常账号定位、清理建议与一键执行能力,适合多账号池的日常运维管理。
|
||||
|
||||
## SDK 文档
|
||||
|
||||
- 使用文档:[docs/sdk-usage_CN.md](docs/sdk-usage_CN.md)
|
||||
- 高级(执行器与翻译器):[docs/sdk-advanced_CN.md](docs/sdk-advanced_CN.md)
|
||||
- 认证: [docs/sdk-access_CN.md](docs/sdk-access_CN.md)
|
||||
- 凭据加载/更新: [docs/sdk-watcher_CN.md](docs/sdk-watcher_CN.md)
|
||||
- 自定义 Provider 示例:`examples/custom-provider`
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎贡献!请随时提交 Pull Request。
|
||||
|
||||
1. Fork 仓库
|
||||
2. 创建您的功能分支(`git checkout -b feature/amazing-feature`)
|
||||
3. 提交您的更改(`git commit -m 'Add some amazing feature'`)
|
||||
4. 推送到分支(`git push origin feature/amazing-feature`)
|
||||
5. 打开 Pull Request
|
||||
|
||||
## 谁与我们在一起?
|
||||
|
||||
这些项目基于 CLIProxyAPI:
|
||||
|
||||
### [vibeproxy](https://github.com/automazeio/vibeproxy)
|
||||
|
||||
一个原生 macOS 菜单栏应用,让您可以使用 Claude Code & ChatGPT 订阅服务和 AI 编程工具,无需 API 密钥。
|
||||
|
||||
### [Subtitle Translator](https://github.com/VjayC/SRT-Subtitle-Translator-Validator)
|
||||
|
||||
一款跨平台的桌面和 Web 应用程序,可通过 CLIProxyAPI 使用您现有的 LLM 订阅(Gemini、ChatGPT、Claude, etc.)来翻译和验证 SRT 字幕 - 无需 API 密钥。
|
||||
|
||||
### [CCS (Claude Code Switch)](https://github.com/kaitranntt/ccs)
|
||||
|
||||
CLI 封装器,用于通过 CLIProxyAPI OAuth 即时切换多个 Claude 账户和替代模型(Gemini, Codex, Antigravity),无需 API 密钥。
|
||||
|
||||
### [Quotio](https://github.com/nguyenphutrong/quotio)
|
||||
|
||||
原生 macOS 菜单栏应用,统一管理 Claude、Gemini、OpenAI 和 Antigravity 订阅,提供实时配额追踪和智能自动故障转移,支持 Claude Code、OpenCode 和 Droid 等 AI 编程工具,无需 API 密钥。
|
||||
|
||||
### [ProxyPilot](https://github.com/Finesssee/ProxyPilot)
|
||||
|
||||
原生 Windows CLIProxyAPI 分支,集成 TUI、系统托盘及多服务商 OAuth 认证,专为 AI 编程工具打造,无需 API 密钥。
|
||||
|
||||
### [Claude Proxy VSCode](https://github.com/uzhao/claude-proxy-vscode)
|
||||
|
||||
一款 VSCode 扩展,提供了在 VSCode 中快速切换 Claude Code 模型的功能,内置 CLIProxyAPI 作为其后端,支持后台自动启动和关闭。
|
||||
|
||||
### [ZeroLimit](https://github.com/0xtbug/zero-limit)
|
||||
|
||||
Windows 桌面应用,基于 Tauri + React 构建,用于通过 CLIProxyAPI 监控 AI 编程助手配额。支持跨 Gemini、Claude、OpenAI Codex 和 Antigravity 账户的使用量追踪,提供实时仪表盘、系统托盘集成和一键代理控制,无需 API 密钥。
|
||||
|
||||
### [CPA-XXX Panel](https://github.com/ferretgeek/CPA-X)
|
||||
|
||||
面向 CLIProxyAPI 的 Web 管理面板,提供健康检查、资源监控、日志查看、自动更新、请求统计与定价展示,支持一键安装与 systemd 服务。
|
||||
|
||||
### [CLIProxyAPI Tray](https://github.com/kitephp/CLIProxyAPI_Tray)
|
||||
|
||||
Windows 托盘应用,基于 PowerShell 脚本实现,不依赖任何第三方库。主要功能包括:自动创建快捷方式、静默运行、密码管理、通道切换(Main / Plus)以及自动下载与更新。
|
||||
|
||||
### [霖君](https://github.com/wangdabaoqq/LinJun)
|
||||
|
||||
霖君是一款用于管理AI编程助手的跨平台桌面应用,支持macOS、Windows、Linux系统。统一管理Claude Code、Gemini、OpenAI Codex等AI编程工具,本地代理实现多账户配额跟踪和一键配置。
|
||||
|
||||
### [CLIProxyAPI Dashboard](https://github.com/itsmylife44/cliproxyapi-dashboard)
|
||||
|
||||
一个面向 CLIProxyAPI 的现代化 Web 管理仪表盘,基于 Next.js、React 和 PostgreSQL 构建。支持实时日志流、结构化配置编辑、API Key 管理、Claude/Gemini/Codex 的 OAuth 提供方集成、使用量分析、容器管理,并可通过配套插件与 OpenCode 同步配置,无需手动编辑 YAML。
|
||||
|
||||
### [All API Hub](https://github.com/qixing-jk/all-api-hub)
|
||||
|
||||
用于一站式管理 New API 兼容中转站账号的浏览器扩展,提供余额与用量看板、自动签到、密钥一键导出到常用应用、网页内 API 可用性测试,以及渠道与模型同步和重定向。支持通过 CLIProxyAPI Management API 一键导入 Provider 与同步配置。
|
||||
|
||||
### [Shadow AI](https://github.com/HEUDavid/shadow-ai)
|
||||
|
||||
Shadow AI 是一款专为受限环境设计的 AI 辅助工具。提供无窗口、无痕迹的隐蔽运行方式,并通过局域网实现跨设备的 AI 问答交互与控制。本质上是一个「屏幕/音频采集 + AI 推理 + 低摩擦投送」的自动化协作层,帮助用户在受控设备/受限环境下沉浸式跨应用地使用 AI 助手。
|
||||
|
||||
### [ProxyPal](https://github.com/buddingnewinsights/proxypal)
|
||||
|
||||
跨平台桌面应用(macOS、Windows、Linux),以原生 GUI 封装 CLIProxyAPI。支持连接 Claude、ChatGPT、Gemini、GitHub Copilot 及自定义 OpenAI 兼容端点,具备使用分析、请求监控和热门编程工具自动配置功能,无需 API 密钥。
|
||||
|
||||
### [CLIProxyAPI Quota Inspector](https://github.com/AllenReder/CLIProxyAPI-Quota-Inspector)
|
||||
|
||||
上手即用的面向 CLIProxyAPI 跨平台配额查询工具,支持按账号展示 codex 5h/7d 配额窗口、按计划排序、状态着色及多账号汇总分析。
|
||||
|
||||
### [CLIProxy Pool Watch](https://github.com/murasame612/CLIProxyPoolWidget)
|
||||
|
||||
原生 macOS SwiftUI 应用,用于监控 CLIProxyAPI 池中的 ChatGPT/Codex 账号额度。通过 Management API 展示账号可用状态、Plus 基准容量、5 小时与周额度进度条、套餐权重和恢复预测。
|
||||
|
||||
### [Panopticon](https://github.com/eltmon/panopticon-cli)
|
||||
|
||||
面向 AI 编程助手的多智能体编排工具。它将 CLIProxyAPI 作为本地 sidecar 运行,使其智能体可以通过 ChatGPT 订阅驱动 GPT 模型,并将 Claude Code 指向 Anthropic 兼容端点,无需 OpenAI API 密钥。
|
||||
|
||||
### [Tunnel Agent](https://github.com/Villoh/tunnel-agent)
|
||||
|
||||
Windows 桌面 UI,通过单一界面管理 CLIProxyAPI 和 Perplexity WebUI Scraper,灵感来自 Quotio 和 VibeProxy。连接 OAuth 提供商(Claude、Gemini、Codex、Kimi、Antigravity)、自定义 API 密钥和 Perplexity 会话账号,然后将任意编程智能体指向本地端点。
|
||||
|
||||
### [Quotio Desktop](https://github.com/xiaocoss/quotio-desktop)
|
||||
|
||||
Quotio 的跨平台(Tauri)移植版,支持 Windows / macOS / Linux。通过 CLIProxyAPI 管理多账号代理池(Codex、Claude Code、GitHub Copilot、Gemini、Antigravity、Kiro、Cursor、Trae、GLM),提供每账号 5 小时 / 每周额度进度条、Codex 主动重置次数与一键重置、智能调度、用量统计及 Codex 多开实例,无需 API 密钥。
|
||||
|
||||
> [!NOTE]
|
||||
> 如果你开发了基于 CLIProxyAPI 的项目,请提交一个 PR(拉取请求)将其添加到此列表中。
|
||||
|
||||
## 更多选择
|
||||
|
||||
以下项目是 CLIProxyAPI 的移植版或受其启发:
|
||||
|
||||
### [9Router](https://github.com/decolua/9router)
|
||||
|
||||
基于 Next.js 的实现,灵感来自 CLIProxyAPI,易于安装使用;自研格式转换(OpenAI/Claude/Gemini/Ollama)、组合系统与自动回退、多账户管理(指数退避)、Next.js Web 控制台,并支持 Cursor、Claude Code、Cline、RooCode 等 CLI 工具,无需 API 密钥。
|
||||
|
||||
### [OmniRoute](https://github.com/diegosouzapw/OmniRoute)
|
||||
|
||||
代码不止,创新不停。智能路由至免费及低成本 AI 模型,并支持自动故障转移。
|
||||
|
||||
OmniRoute 是一个面向多供应商大语言模型的 AI 网关:它提供兼容 OpenAI 的端点,具备智能路由、负载均衡、重试及回退机制。通过添加策略、速率限制、缓存和可观测性,确保推理过程既可靠又具备成本意识。
|
||||
|
||||
### [Playful Proxy API Panel (PPAP)](https://github.com/daishuge/playful-proxy-api-panel)
|
||||
|
||||
一个公开的 CLIProxyAPI 兼容二开版本和配套管理面板,尽量保持与上游一致的使用方式,同时恢复内置使用量统计,并补充缓存命中率、首字响应时间、TPS 记录和面向 Docker 自托管的安装说明。
|
||||
|
||||
### [Codex Switch](https://github.com/9ycrooked/CodexSwitch)
|
||||
|
||||
这是一个使用 Tauri 2 + Vue 3 构建的工具,用于管理多个 OpenAI Codex 桌面账户。它可以在已保存的 ChatGPT/Codex 认证配置之间切换,实时查看 5 小时和每周配额使用情况,验证 token 健康状态,查看当前账户详情,并在无需手动复制的情况下导入或保存 auth.json 文件。
|
||||
|
||||
> [!NOTE]
|
||||
> 如果你开发了 CLIProxyAPI 的移植或衍生项目,请提交 PR 将其添加到此列表中。
|
||||
|
||||
## 许可证
|
||||
|
||||
此项目根据 MIT 许可证授权 - 有关详细信息,请参阅 [LICENSE](LICENSE) 文件。
|
||||
|
||||
## 写给所有中国网友的
|
||||
|
||||
QQ 群:188637136(满)、1081218164
|
||||
|
||||
或
|
||||
|
||||
Telegram 群:https://t.me/CLIProxyAPI
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
# CLI Proxy API
|
||||
|
||||
[English](README.md) | [中文](README_CN.md) | 日本語
|
||||
|
||||
CLI向けのOpenAI/Gemini/Claude/Codex/Grok互換APIインターフェースを提供するプロキシサーバーです。
|
||||
|
||||
OAuth経由でOpenAI Codex(GPTモデル)およびClaude Codeもサポートしています。
|
||||
|
||||
ローカルまたはマルチアカウントのCLIアクセスを、OpenAI(Responses含む)/Gemini/Claude互換のクライアントやSDKで利用できます。
|
||||
|
||||
## スポンサー
|
||||
|
||||
[](https://www.packyapi.com/register?aff=cliproxyapi)
|
||||
|
||||
PackyCodeのスポンサーシップに感謝します!
|
||||
|
||||
PackyCodeは信頼性が高く効率的なAPIリレーサービスプロバイダーで、Claude Code、Codex、Geminiなどのリレーサービスを提供しています。
|
||||
|
||||
PackyCodeは当ソフトウェアのユーザーに特別割引を提供しています:<a href="https://www.packyapi.com/register?aff=cliproxyapi">こちらのリンク</a>から登録し、チャージ時にプロモーションコード「cliproxyapi」を入力すると10%割引になります。
|
||||
|
||||
---
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td width="180"><a href="https://www.aicodemirror.com/register?invitecode=TJNAIF"><img src="./assets/aicodemirror.png" alt="AICodeMirror" width="150"></a></td>
|
||||
<td>AICodeMirrorのスポンサーシップに感謝します!AICodeMirrorはClaude Code / Codex / Gemini向けの公式高安定性リレーサービスを提供しており、エンタープライズグレードの同時接続、迅速な請求書発行、24時間365日の専任技術サポートを備えています。Claude Code / Codex / Geminiの公式チャネルが元の価格の38% / 2% / 9%で利用でき、チャージ時にはさらに割引があります!CLIProxyAPIユーザー向けの特別特典:<a href="https://www.aicodemirror.com/register?invitecode=TJNAIF">こちらのリンク</a>から登録すると、初回チャージが20%割引になり、エンタープライズのお客様は最大25%割引を受けられます!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://shop.bmoplus.com/?utm_source=github"><img src="./assets/bmoplus.png" alt="BmoPlus" width="150"></a></td>
|
||||
<td>本プロジェクトにご支援いただいた BmoPlus に感謝いたします!BmoPlusは、AIサブスクリプションのヘビーユーザー向けに特化した信頼性の高いAIアカウントサービスプロバイダーであり、安定した ChatGPT Plus / ChatGPT Pro (完全保証) / Claude Pro / Super Grok / Gemini Pro の公式代行チャージおよび即納アカウントを提供しています。こちらの<a href="https://shop.bmoplus.com/?utm_source=github">BmoPlus AIアカウント専門店/代行チャージ</a>経由でご登録・ご注文いただいたユーザー様は、GPTを <b>公式サイト価格の約1割(90% OFF)</b> という驚異的な価格でご利用いただけます!</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://coder.visioncoder.cn"><img src="./assets/visioncoder.png" alt="VisionCoder" width="150"></a></td>
|
||||
<td>VisionCoderのご支援に感謝します。<a href="https://coder.visioncoder.cn">VisionCoder 開発プラットフォーム</a> は、信頼性が高く効率的なAPIリレーサービスプロバイダーで、Claude Code、Codex、Geminiなどの主要AIモデルを提供し、開発者やチームがより簡単にAI機能を統合して生産性を向上できるよう支援します。さらに、VisionCoderは <b>Claude Max 200 と GPT Pro 200 高級即納アカウント</b> の独占販売チャネルを提供しており、最高クラスのAI算力と体験を手軽に体験できます。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://apikey.fun/register?aff=CLIProxyAPI"><img src="./assets/apikey.png" alt="APIKEY.FUN" width="150"></a></td>
|
||||
<td>APIKEY.FUNのスポンサーシップに感謝します!APIKEY.FUNはプロフェッショナルなエンタープライズ向けAIリレーサービスで、企業および個人開発者に安定・高効率・低コストなAIモデルAPI接続サービスを提供しています。Claude、OpenAI、Geminiなどの主要人気モデルに対応し、価格は公式価格の7%から利用できます。本プロジェクトの<a href="https://apikey.fun/register?aff=CLIProxyAPI">専用リンク</a>から登録すると、さらに<b>チャージが永続的に5%割引</b>となる特別優待を受けられます。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://runapi.co/register?aff=FivD"><img src="./assets/runapi.png" alt="RunAPI" width="150"></a></td>
|
||||
<td>RunAPIは高効率で安定したAPIプラットフォームで、OpenRouterの代替として利用できます。1つのAPI KeyでOpenAI、Claude、Gemini、DeepSeek、Grokなど150以上の主要モデルにアクセスでき、価格は公式価格の10%から、非常に安定しており、Claude Code、OpenClawなどのツールとシームレスに互換性があります。RunAPIはCPAユーザー向けに特別特典を提供しています:<a href="https://runapi.co/register?aff=FivD">登録</a>後に管理者へ連絡すると、7元分の無料クレジットを受け取れます。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://unity2.ai/register?source=cliproxyapi"><img src="./assets/unity2.jpg" alt="Unity2" width="150"></a></td>
|
||||
<td>Unity2.aiのスポンサーシップに感謝します!Unity2.aiは、個人開発者、チーム、企業向けの高性能AIモデルAPIリレープラットフォームです。国内の大手企業に長期的にサービスを提供し、1日あたり300億tokenを超える呼び出しを処理し、5000 RPM級の高同時実行に対応しています。残高課金、初回チャージ特典、組み合わせサブスクリプション、企業向け請求書発行、専任サポートに対応しています。<a href="https://unity2.ai/register?source=cliproxyapi">こちらのリンク</a>から登録すると$2の残高を受け取れ、公式グループに参加するとさらに$10の残高が付与され、最大$12の無料クレジットを受け取れます。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="180"><a href="https://catapi.ai/sign-up"><img src="./assets/catapi.png" alt="CatAPI" width="150"></a></td>
|
||||
<td>Cat APIは、個人開発者やチーム向けのAI大規模モデル集約プラットフォームです。主要な大規模モデルの機能を、シンプルで安定した使いやすい入口に統合することを目指しています。OpenAI、Claude、Geminiと完全互換のAPIを提供し、Claude Code、Cursor、Windsurf、Cline、Roo Code、Continue、Codex、Traeなどの主要なAI IDEやプログラミングツールへシームレスに接続できます。また、CN2高速回線を主な特徴としており、低遅延で高安定なアクセス体験を提供します。<a href="https://catapi.ai/sign-up">登録</a>すると、1$の無料クレジットを受け取れます。</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
## 概要
|
||||
|
||||
- CLIモデル向けのOpenAI/Gemini/Claude/Grok互換APIエンドポイント
|
||||
- OAuthログインによるOpenAI Codexサポート(GPTモデル)
|
||||
- OAuthログインによるClaude Codeサポート
|
||||
- OAuthログインによるGrok Buildサポート
|
||||
- ストリーミング、非ストリーミング、および対応環境でのWebSocketレスポンス
|
||||
- 関数呼び出し/ツールのサポート
|
||||
- マルチモーダル入力サポート(テキストと画像)
|
||||
- ラウンドロビン負荷分散による複数アカウント対応(Gemini、OpenAI、Claude、Grok)
|
||||
- シンプルなCLI認証フロー(Gemini、OpenAI、Claude、Grok)
|
||||
- Generative Language APIキーのサポート
|
||||
- AI Studioビルドのマルチアカウント負荷分散
|
||||
- Claude Codeのマルチアカウント負荷分散
|
||||
- OpenAI Codexのマルチアカウント負荷分散
|
||||
- Grok Buildのマルチアカウント負荷分散
|
||||
- 設定によるOpenAI互換アップストリームプロバイダー(例:OpenRouter)
|
||||
- プロキシ埋め込み用の再利用可能なGo SDK(`docs/sdk-usage.md`を参照)
|
||||
|
||||
## はじめに
|
||||
|
||||
CLIProxyAPIガイド:[https://help.router-for.me/](https://help.router-for.me/)
|
||||
|
||||
## 管理API
|
||||
|
||||
[MANAGEMENT_API.md](https://help.router-for.me/management/api)を参照
|
||||
|
||||
## 使用量統計
|
||||
|
||||
v6.10.0以降、CLIProxyAPIおよび [CPAMC](https://github.com/router-for-me/Cli-Proxy-API-Management-Center) プロジェクトには使用量統計機能がプリセットされなくなりました。使用量統計が必要な場合は、次のプロジェクトをご利用ください:
|
||||
|
||||
### [CPA Usage Keeper](https://github.com/Willxup/cpa-usage-keeper)
|
||||
|
||||
CLIProxyAPI向けの独立した使用量永続化・可視化サービス。CLIProxyAPIデータを定期同期してSQLiteに保存し、集計APIと、使用量や各種統計を確認できる組み込みダッシュボードを提供します。
|
||||
|
||||
### [CPA-Manager-Plus](https://github.com/seakee/CPA-Manager-Plus)
|
||||
|
||||
リクエスト単位の監視とコスト推定を備えたCLIProxyAPI向けのフル管理センターです。CPA-Managerは、収集したリクエストをアカウント、モデル、チャネル、レイテンシ、ステータス、Token使用量ごとに追跡し、編集可能なモデル価格とLiteLLM価格のワンクリック同期でコストを推定します。SQLiteでイベントを永続化し、Codexアカウントプール向けに一括検査、クォータ判定、異常アカウント検出、クリーンアップ提案、ワンクリック実行を提供し、日常的なマルチアカウント運用に適しています。
|
||||
|
||||
## SDKドキュメント
|
||||
|
||||
- 使い方:[docs/sdk-usage.md](docs/sdk-usage.md)
|
||||
- 上級(エグゼキューターとトランスレーター):[docs/sdk-advanced.md](docs/sdk-advanced.md)
|
||||
- アクセス:[docs/sdk-access.md](docs/sdk-access.md)
|
||||
- ウォッチャー:[docs/sdk-watcher.md](docs/sdk-watcher.md)
|
||||
- カスタムプロバイダーの例:`examples/custom-provider`
|
||||
|
||||
## コントリビューション
|
||||
|
||||
コントリビューションを歓迎します!お気軽にPull Requestを送ってください。
|
||||
|
||||
1. リポジトリをフォーク
|
||||
2. フィーチャーブランチを作成(`git checkout -b feature/amazing-feature`)
|
||||
3. 変更をコミット(`git commit -m 'Add some amazing feature'`)
|
||||
4. ブランチにプッシュ(`git push origin feature/amazing-feature`)
|
||||
5. Pull Requestを作成
|
||||
|
||||
## 関連プロジェクト
|
||||
|
||||
CLIProxyAPIをベースにした以下のプロジェクトがあります:
|
||||
|
||||
### [vibeproxy](https://github.com/automazeio/vibeproxy)
|
||||
|
||||
macOSネイティブのメニューバーアプリで、Claude CodeとChatGPTのサブスクリプションをAIコーディングツールで使用可能 - APIキー不要
|
||||
|
||||
### [Subtitle Translator](https://github.com/VjayC/SRT-Subtitle-Translator-Validator)
|
||||
|
||||
CLIProxyAPI経由で既存のLLMサブスクリプション(Gemini、ChatGPT、Claude, etc.)を使用してSRT字幕を翻訳および検証する、クロスプラットフォームのデスクトップおよびWebアプリ - APIキー不要。
|
||||
|
||||
### [CCS (Claude Code Switch)](https://github.com/kaitranntt/ccs)
|
||||
|
||||
CLIProxyAPI OAuthを使用して複数のClaudeアカウントや代替モデル(Gemini、Codex、Antigravity)を即座に切り替えるCLIラッパー - APIキー不要
|
||||
|
||||
### [Quotio](https://github.com/nguyenphutrong/quotio)
|
||||
|
||||
Claude、Gemini、OpenAI、Antigravityのサブスクリプションを統合し、リアルタイムのクォータ追跡とスマート自動フェイルオーバーを備えたmacOSネイティブのメニューバーアプリ。Claude Code、OpenCode、Droidなどのコーディングツール向け - APIキー不要
|
||||
|
||||
### [ProxyPilot](https://github.com/Finesssee/ProxyPilot)
|
||||
|
||||
TUI、システムトレイ、マルチプロバイダーOAuthを備えたWindows向けCLIProxyAPIフォーク - AIコーディングツール用、APIキー不要
|
||||
|
||||
### [Claude Proxy VSCode](https://github.com/uzhao/claude-proxy-vscode)
|
||||
|
||||
Claude Codeモデルを素早く切り替えるVSCode拡張機能。バックエンドとしてCLIProxyAPIを統合し、バックグラウンドでの自動ライフサイクル管理を搭載
|
||||
|
||||
### [ZeroLimit](https://github.com/0xtbug/zero-limit)
|
||||
|
||||
CLIProxyAPIを使用してAIコーディングアシスタントのクォータを監視するTauri + React製のWindowsデスクトップアプリ。Gemini、Claude、OpenAI Codex、Antigravityアカウントの使用量をリアルタイムダッシュボード、システムトレイ統合、ワンクリックプロキシコントロールで追跡 - APIキー不要
|
||||
|
||||
### [CPA-XXX Panel](https://github.com/ferretgeek/CPA-X)
|
||||
|
||||
CLIProxyAPI向けの軽量Web管理パネル。ヘルスチェック、リソース監視、リアルタイムログ、自動更新、リクエスト統計、料金表示機能を搭載。ワンクリックインストールとsystemdサービスに対応
|
||||
|
||||
### [CLIProxyAPI Tray](https://github.com/kitephp/CLIProxyAPI_Tray)
|
||||
|
||||
PowerShellスクリプトで実装されたWindowsトレイアプリケーション。サードパーティライブラリに依存せず、ショートカットの自動作成、サイレント実行、パスワード管理、チャネル切り替え(Main / Plus)、自動ダウンロードおよび自動更新に対応
|
||||
|
||||
### [霖君](https://github.com/wangdabaoqq/LinJun)
|
||||
|
||||
霖君はAIプログラミングアシスタントを管理するクロスプラットフォームデスクトップアプリケーションで、macOS、Windows、Linuxシステムに対応。Claude Code、Gemini、OpenAI Codexなどのコーディングツールを統合管理し、ローカルプロキシによるマルチアカウントクォータ追跡とワンクリック設定が可能
|
||||
|
||||
### [CLIProxyAPI Dashboard](https://github.com/itsmylife44/cliproxyapi-dashboard)
|
||||
|
||||
Next.js、React、PostgreSQLで構築されたCLIProxyAPI用のモダンなWebベース管理ダッシュボード。リアルタイムログストリーミング、構造化された設定編集、APIキー管理、Claude/Gemini/Codex向けOAuthプロバイダー統合、使用量分析、コンテナ管理、コンパニオンプラグインによるOpenCodeとの設定同期機能を搭載 - 手動でのYAML編集は不要
|
||||
|
||||
### [All API Hub](https://github.com/qixing-jk/all-api-hub)
|
||||
|
||||
New API互換リレーサイトアカウントをワンストップで管理するブラウザ拡張機能。残高と使用量のダッシュボード、自動チェックイン、一般的なアプリへのワンクリックキーエクスポート、ページ内API可用性テスト、チャネル/モデルの同期とリダイレクト機能を搭載。Management APIを通じてCLIProxyAPIと統合し、ワンクリックでプロバイダーのインポートと設定同期が可能
|
||||
|
||||
### [Shadow AI](https://github.com/HEUDavid/shadow-ai)
|
||||
|
||||
Shadow AIは制限された環境向けに特別に設計されたAIアシスタントツールです。ウィンドウや痕跡のないステルス動作モードを提供し、LAN(ローカルエリアネットワーク)を介したクロスデバイスAI質疑応答のインタラクションと制御を可能にします。本質的には「画面/音声キャプチャ + AI推論 + 低摩擦デリバリー」の自動化コラボレーションレイヤーであり、制御されたデバイスや制限された環境でアプリケーション横断的にAIアシスタントを没入的に使用できるようユーザーを支援します。
|
||||
|
||||
### [ProxyPal](https://github.com/buddingnewinsights/proxypal)
|
||||
|
||||
CLIProxyAPIをネイティブGUIでラップしたクロスプラットフォームデスクトップアプリ(macOS、Windows、Linux)。Claude、ChatGPT、Gemini、GitHub Copilot、カスタムOpenAI互換エンドポイントに対応し、使用状況分析、リクエスト監視、人気コーディングツールの自動設定機能を搭載 - APIキー不要
|
||||
|
||||
### [CLIProxyAPI Quota Inspector](https://github.com/AllenReder/CLIProxyAPI-Quota-Inspector)
|
||||
|
||||
CLIProxyAPI向けのすぐに使えるクロスプラットフォームのクォータ確認ツール。アカウントごとの codex 5h/7d クォータ表示、プラン別ソート、ステータス色分け、複数アカウントの集計分析に対応。
|
||||
|
||||
### [CLIProxy Pool Watch](https://github.com/murasame612/CLIProxyPoolWidget)
|
||||
|
||||
CLIProxyAPIプール内のChatGPT/Codexアカウントクォータを監視するmacOSネイティブSwiftUIアプリ。Management APIを通じて、アカウントの可用性、Plus基準の容量、5時間/週次クォータバー、プラン重み、復元予測を表示します。
|
||||
|
||||
### [Panopticon](https://github.com/eltmon/panopticon-cli)
|
||||
|
||||
AIコーディングアシスタント向けのマルチエージェントオーケストレーションツール。CLIProxyAPIをローカルsidecarとして実行することで、エージェントがChatGPTサブスクリプション経由でGPTモデルを利用できるようにし、Claude CodeをAnthropic互換エンドポイントへ向けるため、OpenAI APIキーは不要です。
|
||||
|
||||
### [Tunnel Agent](https://github.com/Villoh/tunnel-agent)
|
||||
|
||||
CLIProxyAPIとPerplexity WebUI Scraperをひとつのインターフェースで管理するWindowsデスクトップUI。QuotioとVibeProxyにインスパイアされ、OAuthプロバイダー(Claude、Gemini、Codex、Kimi、Antigravity)、カスタムAPIキー、Perplexityセッションアカウントを接続し、任意のコーディングエージェントをローカルエンドポイントに向けることができます。
|
||||
|
||||
### [Quotio Desktop](https://github.com/xiaocoss/quotio-desktop)
|
||||
|
||||
Quotio のクロスプラットフォーム(Tauri)移植版(Windows / macOS / Linux 対応)。CLIProxyAPI 経由で複数の AI アカウント(Codex、Claude Code、GitHub Copilot、Gemini、Antigravity、Kiro、Cursor、Trae、GLM)のプールを管理し、アカウントごとの 5 時間 / 週間クォータバー、Codex のリセットクレジットとワンクリックリセット、スマートスケジューリング、使用統計、Codex マルチインスタンスに対応。API キー不要。
|
||||
|
||||
> [!NOTE]
|
||||
> CLIProxyAPIをベースにプロジェクトを開発した場合は、PRを送ってこのリストに追加してください。
|
||||
|
||||
## その他の選択肢
|
||||
|
||||
以下のプロジェクトはCLIProxyAPIの移植版またはそれに触発されたものです:
|
||||
|
||||
### [9Router](https://github.com/decolua/9router)
|
||||
|
||||
CLIProxyAPIに触発されたNext.js実装。インストールと使用が簡単で、フォーマット変換(OpenAI/Claude/Gemini/Ollama)、自動フォールバック付きコンボシステム、指数バックオフ付きマルチアカウント管理、Next.js Webダッシュボード、CLIツール(Cursor、Claude Code、Cline、RooCode)のサポートをゼロから構築 - APIキー不要
|
||||
|
||||
### [OmniRoute](https://github.com/diegosouzapw/OmniRoute)
|
||||
|
||||
コーディングを止めない。無料および低コストのAIモデルへのスマートルーティングと自動フォールバック。
|
||||
|
||||
OmniRouteはマルチプロバイダーLLM向けのAIゲートウェイです:スマートルーティング、負荷分散、リトライ、フォールバックを備えたOpenAI互換エンドポイント。ポリシー、レート制限、キャッシュ、可観測性を追加して、信頼性が高くコストを意識した推論を実現します。
|
||||
|
||||
### [Playful Proxy API Panel (PPAP)](https://github.com/daishuge/playful-proxy-api-panel)
|
||||
|
||||
上流に近い使い方を維持する公開CLIProxyAPI互換フォーク兼管理パネルです。内蔵の使用量統計を復元し、キャッシュヒット率、初回バイト待ち時間、TPSの記録、Docker向けのセルフホスト手順を追加しています。
|
||||
|
||||
### [Codex Switch](https://github.com/9ycrooked/CodexSwitch)
|
||||
|
||||
Tauri 2 + Vue 3で構築された、複数のOpenAI Codexデスクトップアカウントを管理するためのツールです。保存済みのChatGPT/Codex認証プロファイルを切り替え、5時間および週次クォータ使用量をリアルタイムで確認し、tokenの状態を検証し、現在のアカウント詳細を表示し、手動コピーなしでauth.jsonファイルをインポートまたは保存できます。
|
||||
|
||||
> [!NOTE]
|
||||
> CLIProxyAPIの移植版またはそれに触発されたプロジェクトを開発した場合は、PRを送ってこのリストに追加してください。
|
||||
|
||||
## ライセンス
|
||||
|
||||
本プロジェクトはMITライセンスの下でライセンスされています - 詳細は[LICENSE](LICENSE)ファイルを参照してください。
|
||||
+1
-1
@@ -1 +1 @@
|
||||
v6.9.15
|
||||
v7.0.2
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 401 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -8,7 +8,8 @@
|
||||
//
|
||||
// Flags:
|
||||
//
|
||||
// --auths-dir <path> Directory containing auth JSON files (default: "auths")
|
||||
// --auths-dir <path> Directory containing auth JSON files (default: config auth-dir)
|
||||
// --config <path> Config file path (default: "config.yaml")
|
||||
// --output <path> Output JSON file path (default: "antigravity_models.json")
|
||||
// --pretty Pretty-print the output JSON (default: true)
|
||||
package main
|
||||
@@ -25,11 +26,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
|
||||
sdkauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/proxyutil"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/misc"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
||||
sdkauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
@@ -66,23 +69,49 @@ type modelEntry struct {
|
||||
|
||||
func main() {
|
||||
var authsDir string
|
||||
var configPath string
|
||||
var outputPath string
|
||||
var pretty bool
|
||||
|
||||
flag.StringVar(&authsDir, "auths-dir", "auths", "Directory containing auth JSON files")
|
||||
flag.StringVar(&authsDir, "auths-dir", "", "Directory containing auth JSON files (overrides config auth-dir)")
|
||||
flag.StringVar(&configPath, "config", "", "Configure File Path")
|
||||
flag.StringVar(&outputPath, "output", "antigravity_models.json", "Output JSON file path")
|
||||
flag.BoolVar(&pretty, "pretty", true, "Pretty-print the output JSON")
|
||||
flag.Parse()
|
||||
authsDirOverridden := false
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
if f.Name == "auths-dir" {
|
||||
authsDirOverridden = true
|
||||
}
|
||||
})
|
||||
|
||||
// Resolve relative paths against the working directory.
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: cannot get working directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !filepath.IsAbs(authsDir) {
|
||||
|
||||
if strings.TrimSpace(configPath) == "" {
|
||||
configPath = filepath.Join(wd, "config.yaml")
|
||||
}
|
||||
cfg, err := config.LoadConfigOptional(configPath, false)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to load config file %s: %v\n", configPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = &config.Config{}
|
||||
}
|
||||
|
||||
if !authsDirOverridden {
|
||||
authsDir = cfg.AuthDir
|
||||
} else if strings.TrimSpace(authsDir) != "" && !strings.HasPrefix(strings.TrimSpace(authsDir), "~") && !filepath.IsAbs(authsDir) {
|
||||
authsDir = filepath.Join(wd, authsDir)
|
||||
}
|
||||
if authsDir, err = util.ResolveAuthDir(authsDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to resolve auth directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !filepath.IsAbs(outputPath) {
|
||||
outputPath = filepath.Join(wd, outputPath)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,333 @@
|
||||
// Command fetch_codex_models connects to the Codex API using stored auth
|
||||
// credentials and saves the dynamically fetched Codex client model catalog to a
|
||||
// JSON file for inspection or offline use.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// go run ./cmd/fetch_codex_models [flags]
|
||||
//
|
||||
// Flags:
|
||||
//
|
||||
// --auths-dir <path> Directory containing auth JSON files (default: config auth-dir)
|
||||
// --config <path> Config file path (default: "config.yaml")
|
||||
// --output <path> Output JSON file path (default: "codex_models.json")
|
||||
// --client-version <ver> Codex client_version query value (default: "0.133.0")
|
||||
// --pretty Pretty-print the output JSON (default: true)
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
codexauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
||||
sdkauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
codexModelsBaseURL = "https://chatgpt.com/backend-api/codex"
|
||||
codexModelsPath = "/models"
|
||||
defaultClientVersion = "0.133.0"
|
||||
defaultCodexUserAgent = "codex_cli_rs/0.133.0 (Mac OS 26.3.1; arm64) iTerm.app/3.6.9"
|
||||
defaultCodexOriginator = "codex_cli_rs"
|
||||
accessTokenRefreshLeeway = 30 * time.Second
|
||||
)
|
||||
|
||||
func init() {
|
||||
logging.SetupBaseLogger()
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var authsDir string
|
||||
var configPath string
|
||||
var outputPath string
|
||||
var clientVersion string
|
||||
var pretty bool
|
||||
|
||||
flag.StringVar(&authsDir, "auths-dir", "", "Directory containing auth JSON files (overrides config auth-dir)")
|
||||
flag.StringVar(&configPath, "config", "", "Configure File Path")
|
||||
flag.StringVar(&outputPath, "output", "codex_models.json", "Output JSON file path")
|
||||
flag.StringVar(&clientVersion, "client-version", defaultClientVersion, "Codex client_version query value")
|
||||
flag.BoolVar(&pretty, "pretty", true, "Pretty-print the output JSON")
|
||||
flag.Parse()
|
||||
authsDirOverridden := false
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
if f.Name == "auths-dir" {
|
||||
authsDirOverridden = true
|
||||
}
|
||||
})
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: cannot get working directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(configPath) == "" {
|
||||
configPath = filepath.Join(wd, "config.yaml")
|
||||
}
|
||||
cfg, err := config.LoadConfigOptional(configPath, false)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to load config file %s: %v\n", configPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = &config.Config{}
|
||||
}
|
||||
|
||||
if !authsDirOverridden {
|
||||
authsDir = cfg.AuthDir
|
||||
} else if strings.TrimSpace(authsDir) != "" && !strings.HasPrefix(strings.TrimSpace(authsDir), "~") && !filepath.IsAbs(authsDir) {
|
||||
authsDir = filepath.Join(wd, authsDir)
|
||||
}
|
||||
if authsDir, err = util.ResolveAuthDir(authsDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to resolve auth directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !filepath.IsAbs(outputPath) {
|
||||
outputPath = filepath.Join(wd, outputPath)
|
||||
}
|
||||
|
||||
fmt.Printf("Scanning auth files in: %s\n", authsDir)
|
||||
|
||||
fileStore := sdkauth.NewFileTokenStore()
|
||||
fileStore.SetBaseDir(authsDir)
|
||||
|
||||
ctx := context.Background()
|
||||
auths, err := fileStore.List(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to list auth files: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(auths) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "error: no auth files found in %s\n", authsDir)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
chosen := findCodexAuth(auths)
|
||||
if chosen == nil {
|
||||
fmt.Fprintf(os.Stderr, "error: no enabled codex auth found in %s\n", authsDir)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Using auth: id=%s label=%s\n", chosen.ID, chosen.Label)
|
||||
|
||||
accessToken, refreshed, err := ensureAccessToken(ctx, fileStore, chosen)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to prepare codex access token: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if refreshed {
|
||||
fmt.Println("Refreshed Codex access token.")
|
||||
}
|
||||
|
||||
fmt.Println("Fetching Codex model list from upstream...")
|
||||
|
||||
raw, count, err := fetchModels(ctx, chosen, accessToken, clientVersion)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to fetch codex models: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Fetched %d models.\n", count)
|
||||
|
||||
if pretty {
|
||||
raw, err = prettyJSON(raw)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to format JSON: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if err = os.WriteFile(outputPath, raw, 0o644); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to write output file %s: %v\n", outputPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Model list saved to: %s\n", outputPath)
|
||||
}
|
||||
|
||||
func findCodexAuth(auths []*coreauth.Auth) *coreauth.Auth {
|
||||
for _, auth := range auths {
|
||||
if auth == nil || auth.Disabled {
|
||||
continue
|
||||
}
|
||||
if !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") {
|
||||
continue
|
||||
}
|
||||
if metaStringValue(auth.Metadata, "access_token") == "" && metaStringValue(auth.Metadata, "refresh_token") == "" {
|
||||
continue
|
||||
}
|
||||
return auth
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureAccessToken(ctx context.Context, store *sdkauth.FileTokenStore, auth *coreauth.Auth) (string, bool, error) {
|
||||
accessToken := metaStringValue(auth.Metadata, "access_token")
|
||||
if accessToken != "" {
|
||||
if expiresAt, ok := auth.ExpirationTime(); !ok || time.Now().Add(accessTokenRefreshLeeway).Before(expiresAt) {
|
||||
return accessToken, false, nil
|
||||
}
|
||||
}
|
||||
|
||||
refreshToken := metaStringValue(auth.Metadata, "refresh_token")
|
||||
if refreshToken == "" {
|
||||
if accessToken != "" {
|
||||
return accessToken, false, nil
|
||||
}
|
||||
return "", false, fmt.Errorf("missing access_token and refresh_token")
|
||||
}
|
||||
|
||||
svc := codexauth.NewCodexAuthWithProxyURL(nil, auth.ProxyURL)
|
||||
tokenData, errRefresh := svc.RefreshTokensWithRetry(ctx, refreshToken, 3)
|
||||
if errRefresh != nil {
|
||||
return "", false, errRefresh
|
||||
}
|
||||
if strings.TrimSpace(tokenData.AccessToken) == "" {
|
||||
return "", false, fmt.Errorf("refresh response did not include access_token")
|
||||
}
|
||||
|
||||
if auth.Metadata == nil {
|
||||
auth.Metadata = make(map[string]any)
|
||||
}
|
||||
auth.Metadata["id_token"] = tokenData.IDToken
|
||||
auth.Metadata["access_token"] = tokenData.AccessToken
|
||||
if tokenData.RefreshToken != "" {
|
||||
auth.Metadata["refresh_token"] = tokenData.RefreshToken
|
||||
}
|
||||
if tokenData.AccountID != "" {
|
||||
auth.Metadata["account_id"] = tokenData.AccountID
|
||||
}
|
||||
if tokenData.Email != "" {
|
||||
auth.Metadata["email"] = tokenData.Email
|
||||
}
|
||||
auth.Metadata["expired"] = tokenData.Expire
|
||||
auth.Metadata["type"] = "codex"
|
||||
auth.Metadata["last_refresh"] = time.Now().Format(time.RFC3339)
|
||||
|
||||
if _, errSave := store.Save(ctx, auth); errSave != nil {
|
||||
return "", false, fmt.Errorf("failed to save refreshed auth: %w", errSave)
|
||||
}
|
||||
|
||||
return tokenData.AccessToken, true, nil
|
||||
}
|
||||
|
||||
func fetchModels(ctx context.Context, auth *coreauth.Auth, accessToken, clientVersion string) ([]byte, int, error) {
|
||||
modelsURL, errURL := codexModelsURL(clientVersion)
|
||||
if errURL != nil {
|
||||
return nil, 0, errURL
|
||||
}
|
||||
|
||||
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodGet, modelsURL, nil)
|
||||
if errReq != nil {
|
||||
return nil, 0, errReq
|
||||
}
|
||||
httpReq.Close = true
|
||||
httpReq.Header.Set("Accept", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
httpReq.Header.Set("Originator", defaultCodexOriginator)
|
||||
httpReq.Header.Set("User-Agent", defaultCodexUserAgent)
|
||||
if accountID := metaStringValue(auth.Metadata, "account_id"); accountID != "" {
|
||||
httpReq.Header.Set("Chatgpt-Account-Id", accountID)
|
||||
}
|
||||
if auth != nil {
|
||||
util.ApplyCustomHeadersFromAttrs(httpReq, auth.Attributes)
|
||||
}
|
||||
|
||||
httpClient := &http.Client{}
|
||||
if auth != nil {
|
||||
if transport, _, errProxy := proxyutil.BuildHTTPTransport(auth.ProxyURL); errProxy == nil && transport != nil {
|
||||
httpClient.Transport = transport
|
||||
}
|
||||
}
|
||||
|
||||
httpResp, errDo := httpClient.Do(httpReq)
|
||||
if errDo != nil {
|
||||
return nil, 0, errDo
|
||||
}
|
||||
|
||||
bodyBytes, errRead := io.ReadAll(httpResp.Body)
|
||||
if errClose := httpResp.Body.Close(); errClose != nil && errRead == nil {
|
||||
errRead = errClose
|
||||
}
|
||||
if errRead != nil {
|
||||
return nil, 0, errRead
|
||||
}
|
||||
|
||||
if httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices {
|
||||
return nil, 0, fmt.Errorf("models request failed with status %d: %s", httpResp.StatusCode, strings.TrimSpace(string(bodyBytes)))
|
||||
}
|
||||
|
||||
count, errCount := countModels(bodyBytes)
|
||||
if errCount != nil {
|
||||
return nil, 0, errCount
|
||||
}
|
||||
return bodyBytes, count, nil
|
||||
}
|
||||
|
||||
func codexModelsURL(clientVersion string) (string, error) {
|
||||
u, err := url.Parse(codexModelsBaseURL + codexModelsPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(clientVersion) != "" {
|
||||
q := u.Query()
|
||||
q.Set("client_version", strings.TrimSpace(clientVersion))
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func countModels(raw []byte) (int, error) {
|
||||
var payload struct {
|
||||
Models []map[string]any `json:"models"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
return 0, fmt.Errorf("failed to parse response JSON: %w", err)
|
||||
}
|
||||
if payload.Models == nil {
|
||||
return 0, fmt.Errorf("response JSON does not contain models array")
|
||||
}
|
||||
return len(payload.Models), nil
|
||||
}
|
||||
|
||||
func prettyJSON(raw []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := json.Indent(&buf, raw, "", " "); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func metaStringValue(m map[string]any, key string) string {
|
||||
if m == nil {
|
||||
return ""
|
||||
}
|
||||
v, ok := m[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
return strings.TrimSpace(val)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
@@ -22,20 +22,20 @@ import (
|
||||
|
||||
"github.com/getlantern/systray"
|
||||
"github.com/jchv/go-webview2"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/cmd"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/trayicon"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/updates"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/winutil"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v7/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/api/middleware"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/cmd"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/integrations"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/trayicon"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/updates"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/usage"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/winutil"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/trayicon"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/trayicon"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
+257
-418
@@ -1,4 +1,4 @@
|
||||
// Package main provides the entry point for the ProxyPilot engine.
|
||||
// Package main provides the entry point for the CLI Proxy API server.
|
||||
// This server acts as a proxy that provides OpenAI/Gemini/Claude compatible API interfaces
|
||||
// for CLI models, allowing CLI models to be used with tools and libraries designed for standard AI APIs.
|
||||
package main
|
||||
@@ -17,23 +17,25 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/cmd"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/startupconfig"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/store"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/tui"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v7/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/buildinfo"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/cmd"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/home"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/homeplugins"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/managementasset"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/misc"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/pluginhost"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/redisqueue"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/safemode"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/store"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/tui"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -52,165 +54,60 @@ func init() {
|
||||
buildinfo.BuildDate = BuildDate
|
||||
}
|
||||
|
||||
// setKiroIncognitoMode sets the incognito browser mode for Kiro authentication.
|
||||
// Kiro defaults to incognito mode for multi-account support.
|
||||
// Users can explicitly override with --incognito or --no-incognito flags.
|
||||
func setKiroIncognitoMode(cfg *config.Config, useIncognito, noIncognito bool) {
|
||||
if useIncognito {
|
||||
cfg.IncognitoBrowser = true
|
||||
} else if noIncognito {
|
||||
cfg.IncognitoBrowser = false
|
||||
} else {
|
||||
cfg.IncognitoBrowser = true // Kiro default
|
||||
func shouldStartExampleAPIKeyWarningServer(cfg *config.Config, commandMode, tuiMode, standalone, cloudConfigMissing, homeMode bool) bool {
|
||||
if cfg == nil || commandMode || homeMode || cloudConfigMissing {
|
||||
return false
|
||||
}
|
||||
if tuiMode && !standalone {
|
||||
return false
|
||||
}
|
||||
return safemode.HasExampleAPIKeys(cfg.APIKeys)
|
||||
}
|
||||
|
||||
// main is the entry point of the application.
|
||||
// It parses command-line flags, loads configuration, and starts the appropriate
|
||||
// service based on the provided flags (login, codex-login, or server mode).
|
||||
func main() {
|
||||
fmt.Printf("ProxyPilot Engine Version: %s, Commit: %s, BuiltAt: %s\n", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate)
|
||||
fmt.Printf("CLIProxyAPI Version: %s, Commit: %s, BuiltAt: %s\n", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate)
|
||||
|
||||
// Command-line flags to control the application's behavior.
|
||||
var login bool
|
||||
var codexLogin bool
|
||||
var codexDeviceLogin bool
|
||||
var claudeLogin bool
|
||||
var qwenLogin bool
|
||||
var iflowLogin bool
|
||||
var iflowCookie bool
|
||||
var noBrowser bool
|
||||
var oauthCallbackPort int
|
||||
var antigravityLogin bool
|
||||
var kiroLogin bool
|
||||
var kiroGoogleLogin bool
|
||||
var kiroAWSLogin bool
|
||||
var kiroAWSAuthCode bool
|
||||
var kiroImport bool
|
||||
var antigravityImport bool
|
||||
var minimaxLogin bool
|
||||
var zhipuLogin bool
|
||||
var kimiLogin bool
|
||||
// var githubCopilotLogin bool // REMOVED - GitHub Copilot excluded
|
||||
var detectAgents bool
|
||||
var setupClaude bool
|
||||
var setupCodex bool
|
||||
var setupDroid bool
|
||||
var setupOpenCode bool
|
||||
var setupGemini bool
|
||||
var setupCursor bool
|
||||
var setupKilo bool
|
||||
var setupRooCode bool
|
||||
var setupAll bool
|
||||
var switchAgent string
|
||||
var switchMode string
|
||||
var projectID string
|
||||
var xaiLogin bool
|
||||
var vertexImport string
|
||||
var vertexImportPrefix string
|
||||
var configPath string
|
||||
var password string
|
||||
var noIncognito bool
|
||||
var useIncognito bool
|
||||
|
||||
// Account management flags
|
||||
var showVersion bool
|
||||
var showStatus bool
|
||||
var listAccounts bool
|
||||
var cleanupExpired bool
|
||||
var removeAccount string
|
||||
var refreshTokens string
|
||||
var jsonOutput bool
|
||||
var quietMode bool
|
||||
var verboseMode bool
|
||||
|
||||
// Usage and logs flags
|
||||
var showUsage bool
|
||||
var showLogs bool
|
||||
var logLines int
|
||||
|
||||
// Model and export flags
|
||||
var listModels bool
|
||||
var exportAccounts string
|
||||
var importAccounts string
|
||||
var includeTokens bool
|
||||
var forceImport bool
|
||||
|
||||
// Windows service flags
|
||||
var runAsService bool
|
||||
var serviceCmd string
|
||||
|
||||
// TUI flag
|
||||
var launchTUI bool
|
||||
var homeJWT string
|
||||
var homeDisableClusterDiscovery bool
|
||||
var tuiMode bool
|
||||
var standalone bool
|
||||
var localModel bool
|
||||
|
||||
// Define command-line flags for different operation modes.
|
||||
flag.BoolVar(&login, "login", false, "Login Google Account")
|
||||
flag.BoolVar(&codexLogin, "codex-login", false, "Login to Codex using OAuth")
|
||||
flag.BoolVar(&codexDeviceLogin, "codex-device-login", false, "Login to Codex using device code flow")
|
||||
flag.BoolVar(&claudeLogin, "claude-login", false, "Login to Claude using OAuth")
|
||||
flag.BoolVar(&qwenLogin, "qwen-login", false, "Login to Qwen using OAuth")
|
||||
flag.BoolVar(&iflowLogin, "iflow-login", false, "Login to iFlow using OAuth")
|
||||
flag.BoolVar(&iflowCookie, "iflow-cookie", false, "Login to iFlow using Cookie")
|
||||
flag.BoolVar(&noBrowser, "no-browser", false, "Don't open browser automatically for OAuth")
|
||||
flag.BoolVar(&useIncognito, "incognito", false, "Open browser in incognito/private mode for OAuth (useful for multiple accounts)")
|
||||
flag.BoolVar(&noIncognito, "no-incognito", false, "Force disable incognito mode (uses existing browser session)")
|
||||
flag.IntVar(&oauthCallbackPort, "oauth-callback-port", 0, "Override OAuth callback port (defaults to provider-specific port)")
|
||||
flag.BoolVar(&antigravityLogin, "antigravity-login", false, "Login to Antigravity using OAuth")
|
||||
flag.BoolVar(&kiroLogin, "kiro-login", false, "Login to Kiro using Google OAuth")
|
||||
flag.BoolVar(&kiroGoogleLogin, "kiro-google-login", false, "Login to Kiro using Google OAuth (same as --kiro-login)")
|
||||
flag.BoolVar(&kiroAWSLogin, "kiro-aws-login", false, "Login to Kiro using AWS Builder ID (device code flow)")
|
||||
flag.BoolVar(&kiroAWSAuthCode, "kiro-aws-authcode", false, "Login to Kiro using AWS Builder ID (authorization code flow, better UX)")
|
||||
flag.BoolVar(&kiroImport, "kiro-import", false, "Import Kiro token from Kiro IDE (~/.aws/sso/cache/kiro-auth-token.json)")
|
||||
flag.BoolVar(&antigravityImport, "antigravity-import", false, "Import Antigravity token from Antigravity IDE")
|
||||
flag.BoolVar(&minimaxLogin, "minimax-login", false, "Add MiniMax API key")
|
||||
flag.BoolVar(&zhipuLogin, "zhipu-login", false, "Add Zhipu AI API key")
|
||||
flag.BoolVar(&kimiLogin, "kimi-login", false, "Login to Kimi using OAuth")
|
||||
// GitHub Copilot login removed
|
||||
flag.BoolVar(&detectAgents, "detect-agents", false, "Detect installed CLI agents")
|
||||
flag.BoolVar(&setupClaude, "setup-claude", false, "Configure Claude Code to use ProxyPilot")
|
||||
flag.BoolVar(&setupCodex, "setup-codex", false, "Configure Codex CLI to use ProxyPilot")
|
||||
flag.BoolVar(&setupDroid, "setup-droid", false, "Configure Factory Droid to use ProxyPilot")
|
||||
flag.BoolVar(&setupOpenCode, "setup-opencode", false, "Configure OpenCode to use ProxyPilot")
|
||||
flag.BoolVar(&setupGemini, "setup-gemini", false, "Configure Gemini CLI to use ProxyPilot")
|
||||
flag.BoolVar(&setupCursor, "setup-cursor", false, "Configure Cursor to use ProxyPilot")
|
||||
flag.BoolVar(&setupKilo, "setup-kilo", false, "Configure Kilo Code CLI to use ProxyPilot")
|
||||
flag.BoolVar(&setupRooCode, "setup-roocode", false, "Configure RooCode (VS Code) to use ProxyPilot")
|
||||
flag.BoolVar(&setupAll, "setup-all", false, "Configure all detected CLI agents (with backup)")
|
||||
flag.StringVar(&switchAgent, "switch", "", "Switch agent config mode (e.g., --switch claude)")
|
||||
flag.StringVar(&switchMode, "mode", "", "Switch mode: proxy, native, or status (default: status)")
|
||||
flag.StringVar(&projectID, "project_id", "", "Project ID (Gemini only, not required)")
|
||||
flag.BoolVar(&xaiLogin, "xai-login", false, "Login to xAI using OAuth")
|
||||
flag.StringVar(&configPath, "config", DefaultConfigPath, "Configure File Path")
|
||||
flag.StringVar(&vertexImport, "vertex-import", "", "Import Vertex service account key JSON file")
|
||||
flag.StringVar(&vertexImportPrefix, "vertex-import-prefix", "", "Prefix for Vertex model namespacing (use with -vertex-import)")
|
||||
flag.StringVar(&password, "password", "", "")
|
||||
flag.BoolVar(&launchTUI, "tui", false, "Start with terminal management UI")
|
||||
flag.StringVar(&homeJWT, "home-jwt", "", "Home control plane JWT for mTLS certificate bootstrap and connection")
|
||||
flag.BoolVar(&homeDisableClusterDiscovery, "home-disable-cluster-discovery", false, "Disable Home CLUSTER NODES discovery and keep using the configured -home-jwt address")
|
||||
flag.BoolVar(&tuiMode, "tui", false, "Start with terminal management UI")
|
||||
flag.BoolVar(&standalone, "standalone", false, "In TUI mode, start an embedded local server")
|
||||
flag.BoolVar(&localModel, "local-model", false, "Use embedded model catalog only, skip remote model fetching")
|
||||
|
||||
flag.BoolVar(&showVersion, "version", false, "Show ProxyPilot version and exit")
|
||||
flag.BoolVar(&showStatus, "status", false, "Show ProxyPilot status and exit")
|
||||
flag.BoolVar(&listAccounts, "list-accounts", false, "List all configured accounts and exit")
|
||||
flag.BoolVar(&cleanupExpired, "cleanup-expired", false, "Remove expired tokens and exit")
|
||||
flag.StringVar(&removeAccount, "remove-account", "", "Remove a specific account by name and exit")
|
||||
flag.StringVar(&refreshTokens, "refresh", "", "Force token refresh (all, or email/id to refresh specific)")
|
||||
flag.BoolVar(&jsonOutput, "json", false, "Output in JSON format (overrides --quiet)")
|
||||
flag.BoolVar(&quietMode, "quiet", false, "Run in quiet mode (overrides --verbose)")
|
||||
flag.BoolVar(&verboseMode, "verbose", false, "Run in verbose mode")
|
||||
|
||||
flag.BoolVar(&listModels, "list-models", false, "List available models per provider and exit")
|
||||
flag.StringVar(&exportAccounts, "export-accounts", "", "Export accounts to JSON file (use - for stdout)")
|
||||
flag.StringVar(&importAccounts, "import-accounts", "", "Import accounts from JSON file")
|
||||
flag.BoolVar(&includeTokens, "include-tokens", false, "Include sensitive tokens in export (use with -export-accounts)")
|
||||
flag.BoolVar(&forceImport, "force", false, "Force overwrite existing accounts on import")
|
||||
flag.BoolVar(&showUsage, "usage", false, "Show token usage statistics and exit")
|
||||
flag.BoolVar(&showLogs, "logs", false, "View recent proxy logs and exit")
|
||||
flag.IntVar(&logLines, "n", 50, "Number of log lines to show (used with -logs)")
|
||||
|
||||
// Windows service flags
|
||||
flag.BoolVar(&runAsService, "service", false, "Run as Windows service (internal)")
|
||||
flag.StringVar(&serviceCmd, "service-cmd", "", "Service command: install, uninstall, start, stop, status")
|
||||
|
||||
flag.CommandLine.Usage = func() {
|
||||
out := flag.CommandLine.Output()
|
||||
_, _ = fmt.Fprintf(out, "Usage of %s\n", os.Args[0])
|
||||
@@ -238,70 +135,22 @@ func main() {
|
||||
})
|
||||
}
|
||||
|
||||
// Check for subcommand-style switch command before flag.Parse()
|
||||
// Supports: proxypilot switch [agent] [mode]
|
||||
// agent can be: claude, gemini, codex, opencode, droid, cursor
|
||||
// mode can be: proxy, native, status (default if omitted)
|
||||
var subcommandSwitch bool
|
||||
args := os.Args[1:]
|
||||
if len(args) > 0 && args[0] == "switch" {
|
||||
subcommandSwitch = true
|
||||
switchArgs := args[1:]
|
||||
// Filter out any flags like --status from positional args
|
||||
var positionalArgs []string
|
||||
for _, arg := range switchArgs {
|
||||
if arg == "--status" {
|
||||
switchMode = "status"
|
||||
} else if !strings.HasPrefix(arg, "-") {
|
||||
positionalArgs = append(positionalArgs, arg)
|
||||
}
|
||||
}
|
||||
// Parse positional arguments: [agent] [mode]
|
||||
if len(positionalArgs) >= 1 {
|
||||
switchAgent = positionalArgs[0]
|
||||
}
|
||||
if len(positionalArgs) >= 2 {
|
||||
switchMode = positionalArgs[1]
|
||||
}
|
||||
// Default mode to "status" if not specified
|
||||
if switchMode == "" {
|
||||
switchMode = "status"
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-process -refresh flag: if -refresh is present without a value, treat as -refresh=all
|
||||
for i, arg := range os.Args[1:] {
|
||||
if arg == "-refresh" || arg == "--refresh" {
|
||||
// Check if next arg exists and is not another flag
|
||||
nextIdx := i + 2 // +1 for 1-based slice, +1 for next
|
||||
if nextIdx >= len(os.Args) || strings.HasPrefix(os.Args[nextIdx], "-") {
|
||||
os.Args[i+1] = "-refresh=all"
|
||||
}
|
||||
break
|
||||
}
|
||||
pluginHost := pluginhost.New()
|
||||
if bootstrapCfg := loadPluginBootstrapConfig(pluginBootstrapConfigPath(os.Args[1:], DefaultConfigPath)); bootstrapCfg != nil {
|
||||
pluginHost.ApplyConfig(context.Background(), bootstrapCfg)
|
||||
pluginHost.RegisterCommandLineFlags(context.Background(), flag.CommandLine)
|
||||
}
|
||||
|
||||
// Parse the command-line flags.
|
||||
flag.Parse()
|
||||
|
||||
// Handle Windows service commands early (before config loading)
|
||||
if serviceCmd != "" {
|
||||
if handleServiceCommand([]string{serviceCmd, configPath}) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if runAsService {
|
||||
if err := runService(configPath); err != nil {
|
||||
log.Errorf("service error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Core application variables.
|
||||
var err error
|
||||
var cfg *config.Config
|
||||
var isCloudDeploy bool
|
||||
var configLoadedFromHome bool
|
||||
var homeClient *home.Client
|
||||
var homePluginSyncReport homeplugins.SyncReport
|
||||
var (
|
||||
usePostgresStore bool
|
||||
pgStoreDSN string
|
||||
@@ -312,6 +161,7 @@ func main() {
|
||||
gitStoreRemoteURL string
|
||||
gitStoreUser string
|
||||
gitStorePassword string
|
||||
gitStoreBranch string
|
||||
gitStoreLocalPath string
|
||||
gitStoreInst *store.GitTokenStore
|
||||
gitStoreRoot string
|
||||
@@ -348,6 +198,13 @@ func main() {
|
||||
return "", false
|
||||
}
|
||||
writableBase := util.WritablePath()
|
||||
|
||||
if strings.TrimSpace(homeJWT) == "" {
|
||||
if v, ok := lookupEnv("HOME_JWT", "home_jwt"); ok {
|
||||
homeJWT = v
|
||||
}
|
||||
}
|
||||
|
||||
if value, ok := lookupEnv("PGSTORE_DSN", "pgstore_dsn"); ok {
|
||||
usePostgresStore = true
|
||||
pgStoreDSN = value
|
||||
@@ -381,6 +238,9 @@ func main() {
|
||||
if value, ok := lookupEnv("GITSTORE_LOCAL_PATH", "gitstore_local_path"); ok {
|
||||
gitStoreLocalPath = value
|
||||
}
|
||||
if value, ok := lookupEnv("GITSTORE_GIT_BRANCH", "gitstore_git_branch"); ok {
|
||||
gitStoreBranch = value
|
||||
}
|
||||
if value, ok := lookupEnv("OBJECTSTORE_ENDPOINT", "objectstore_endpoint"); ok {
|
||||
useObjectStore = true
|
||||
objectStoreEndpoint = value
|
||||
@@ -408,7 +268,69 @@ func main() {
|
||||
// Determine and load the configuration file.
|
||||
// Prefer the Postgres store when configured, otherwise fallback to git or local files.
|
||||
var configFilePath string
|
||||
if usePostgresStore {
|
||||
if strings.TrimSpace(homeJWT) != "" {
|
||||
configLoadedFromHome = true
|
||||
ctxHome, cancelHome := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
homeCfg, errHomeCfg := home.ConfigFromJWT(ctxHome, homeJWT)
|
||||
cancelHome()
|
||||
if errHomeCfg != nil {
|
||||
log.Errorf("invalid -home-jwt: %v", errHomeCfg)
|
||||
return
|
||||
}
|
||||
if homeDisableClusterDiscovery {
|
||||
homeCfg.DisableClusterDiscovery = true
|
||||
}
|
||||
homeClient = home.New(homeCfg)
|
||||
defer homeClient.Close()
|
||||
|
||||
ctxHomeConfig, cancelHomeConfig := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
raw, errGetConfig := homeClient.GetConfig(ctxHomeConfig)
|
||||
cancelHomeConfig()
|
||||
if errGetConfig != nil {
|
||||
log.Errorf("failed to fetch config from home: %v", errGetConfig)
|
||||
return
|
||||
}
|
||||
|
||||
parsed, errParseConfig := config.ParseConfigBytes(raw)
|
||||
if errParseConfig != nil {
|
||||
log.Errorf("failed to parse config payload from home: %v", errParseConfig)
|
||||
return
|
||||
}
|
||||
if parsed == nil {
|
||||
parsed = &config.Config{}
|
||||
}
|
||||
parsed.Home = homeCfg
|
||||
parsed.Port = 8317 // Default to 8317 for home mode, can be overridden by home config
|
||||
parsed.UsageStatisticsEnabled = true
|
||||
ctxHomePlugins, cancelHomePlugins := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
var errHomePlugins error
|
||||
homePluginSyncReport, errHomePlugins = homeplugins.SyncWithReport(ctxHomePlugins, parsed, pluginHost)
|
||||
cancelHomePlugins()
|
||||
errReportPlugins := home.ReportPluginStatus(context.Background(), homeClient, homeCfg.NodeID, homePluginSyncReport)
|
||||
if errHomePlugins != nil {
|
||||
log.Errorf("failed to fetch plugins from home: %v", errHomePlugins)
|
||||
}
|
||||
if errReportPlugins != nil {
|
||||
log.Warnf("failed to report home plugin sync status: %v", errReportPlugins)
|
||||
}
|
||||
if errHomePlugins != nil {
|
||||
return
|
||||
}
|
||||
cfg = parsed
|
||||
|
||||
// Keep a non-empty config path for downstream components (log paths, management assets, etc),
|
||||
// but do not require the file to exist when loading config from home.
|
||||
if strings.TrimSpace(configPath) != "" {
|
||||
configFilePath = configPath
|
||||
} else {
|
||||
configFilePath = filepath.Join(wd, "config.yaml")
|
||||
}
|
||||
|
||||
// Local stores are intentionally disabled when config is loaded from home.
|
||||
usePostgresStore = false
|
||||
useObjectStore = false
|
||||
useGitStore = false
|
||||
} else if usePostgresStore {
|
||||
if pgStoreLocalPath == "" {
|
||||
pgStoreLocalPath = wd
|
||||
}
|
||||
@@ -515,7 +437,7 @@ func main() {
|
||||
}
|
||||
gitStoreRoot = filepath.Join(gitStoreLocalPath, "gitstore")
|
||||
authDir := filepath.Join(gitStoreRoot, "auths")
|
||||
gitStoreInst = store.NewGitTokenStore(gitStoreRemoteURL, gitStoreUser, gitStorePassword)
|
||||
gitStoreInst = store.NewGitTokenStore(gitStoreRemoteURL, gitStoreUser, gitStorePassword, gitStoreBranch)
|
||||
gitStoreInst.SetBaseDir(authDir)
|
||||
if errRepo := gitStoreInst.EnsureRepository(); errRepo != nil {
|
||||
log.Errorf("failed to prepare git token store: %v", errRepo)
|
||||
@@ -553,28 +475,17 @@ func main() {
|
||||
configFilePath = configPath
|
||||
cfg, err = config.LoadConfigOptional(configPath, isCloudDeploy)
|
||||
} else {
|
||||
exePath, errExe := os.Executable()
|
||||
if errExe != nil {
|
||||
log.WithError(errExe).Debug("failed to resolve executable path for default config lookup")
|
||||
}
|
||||
resolution := startupconfig.ResolveConfigPath("", wd, exePath)
|
||||
if created, errPrepare := startupconfig.EnsureDefaultConfig(resolution); errPrepare != nil {
|
||||
log.Errorf("failed to prepare default config: %v", errPrepare)
|
||||
wd, err = os.Getwd()
|
||||
if err != nil {
|
||||
log.Errorf("failed to get working directory: %v", err)
|
||||
return
|
||||
} else if created {
|
||||
log.Infof("default config initialized from template: %s", resolution.ConfigPath)
|
||||
}
|
||||
configFilePath = resolution.ConfigPath
|
||||
configFilePath = filepath.Join(wd, "config.yaml")
|
||||
cfg, err = config.LoadConfigOptional(configFilePath, isCloudDeploy)
|
||||
}
|
||||
if err != nil {
|
||||
// For switch command and TUI, config is optional - use defaults
|
||||
if subcommandSwitch || switchAgent != "" || launchTUI {
|
||||
cfg = &config.Config{Port: 8318}
|
||||
} else {
|
||||
log.Errorf("failed to load config: %v", err)
|
||||
return
|
||||
}
|
||||
log.Errorf("failed to load config: %v", err)
|
||||
return
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = &config.Config{}
|
||||
@@ -583,55 +494,42 @@ func main() {
|
||||
// In cloud deploy mode, check if we have a valid configuration
|
||||
var configFileExists bool
|
||||
if isCloudDeploy {
|
||||
if info, errStat := os.Stat(configFilePath); errStat != nil {
|
||||
// Don't mislead: API server will not start until configuration is provided.
|
||||
log.Info("Cloud deploy mode: No configuration file detected; standing by for configuration")
|
||||
configFileExists = false
|
||||
} else if info.IsDir() {
|
||||
log.Info("Cloud deploy mode: Config path is a directory; standing by for configuration")
|
||||
configFileExists = false
|
||||
} else if cfg.Port == 0 {
|
||||
// LoadConfigOptional returns empty config when file is empty or invalid.
|
||||
// Config file exists but is empty or invalid; treat as missing config
|
||||
log.Info("Cloud deploy mode: Configuration file is empty or invalid; standing by for valid configuration")
|
||||
configFileExists = false
|
||||
if configLoadedFromHome && cfg != nil {
|
||||
configFileExists = cfg.Port != 0
|
||||
} else {
|
||||
log.Info("Cloud deploy mode: Configuration file detected; starting service")
|
||||
configFileExists = true
|
||||
if info, errStat := os.Stat(configFilePath); errStat != nil {
|
||||
// Don't mislead: API server will not start until configuration is provided.
|
||||
log.Info("Cloud deploy mode: No configuration file detected; standing by for configuration")
|
||||
configFileExists = false
|
||||
} else if info.IsDir() {
|
||||
log.Info("Cloud deploy mode: Config path is a directory; standing by for configuration")
|
||||
configFileExists = false
|
||||
} else if cfg.Port == 0 {
|
||||
// LoadConfigOptional returns empty config when file is empty or invalid.
|
||||
// Config file exists but is empty or invalid; treat as missing config
|
||||
log.Info("Cloud deploy mode: Configuration file is empty or invalid; standing by for valid configuration")
|
||||
configFileExists = false
|
||||
} else {
|
||||
log.Info("Cloud deploy mode: Configuration file detected; starting service")
|
||||
configFileExists = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Perform basic semantic validation of the loaded configuration.
|
||||
if warnings, errValidate := config.ValidateConfig(cfg); errValidate != nil {
|
||||
log.Errorf("invalid configuration: %v", errValidate)
|
||||
return
|
||||
} else if len(warnings) > 0 {
|
||||
for _, w := range warnings {
|
||||
log.Warnf("config warning: %s", w)
|
||||
}
|
||||
}
|
||||
|
||||
usage.SetStatisticsEnabled(cfg.UsageStatisticsEnabled)
|
||||
redisqueue.SetUsageStatisticsEnabled(cfg.UsageStatisticsEnabled)
|
||||
redisqueue.SetRetentionSeconds(cfg.RedisUsageQueueRetentionSeconds)
|
||||
coreauth.SetQuotaCooldownDisabled(cfg.DisableCooling)
|
||||
// AntigravityPrimaryEmail removed - field does not exist
|
||||
coreauth.SetTransientErrorCooldownSeconds(cfg.TransientErrorCooldownSeconds)
|
||||
|
||||
if err = logging.ConfigureLogOutput(cfg); err != nil {
|
||||
log.Errorf("failed to configure log output: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("ProxyPilot Engine Version: %s, Commit: %s, BuiltAt: %s", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate)
|
||||
log.Infof("CLIProxyAPI Version: %s, Commit: %s, BuiltAt: %s", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate)
|
||||
|
||||
// Set the log level based on the configuration.
|
||||
util.SetLogLevel(cfg)
|
||||
|
||||
// CLI flags override config-based log level
|
||||
if quietMode {
|
||||
logging.SetLogLevel("quiet")
|
||||
} else if verboseMode {
|
||||
logging.SetLogLevel("verbose")
|
||||
}
|
||||
|
||||
if resolvedAuthDir, errResolveAuthDir := util.ResolveAuthDir(cfg.AuthDir); errResolveAuthDir != nil {
|
||||
log.Errorf("failed to resolve auth directory: %v", errResolveAuthDir)
|
||||
return
|
||||
@@ -646,6 +544,16 @@ func main() {
|
||||
CallbackPort: oauthCallbackPort,
|
||||
}
|
||||
|
||||
commandMode := vertexImport != "" || antigravityLogin || codexLogin || codexDeviceLogin || claudeLogin || kimiLogin || xaiLogin
|
||||
cloudConfigMissing := isCloudDeploy && !configFileExists
|
||||
homeMode := configLoadedFromHome || (cfg != nil && cfg.Home.Enabled)
|
||||
if shouldStartExampleAPIKeyWarningServer(cfg, commandMode, tuiMode, standalone, cloudConfigMissing, homeMode) {
|
||||
matches := safemode.ExampleAPIKeys(cfg.APIKeys)
|
||||
log.WithField("api_keys", strings.Join(matches, ",")).Error("unsafe example API key configured; starting warning-only server")
|
||||
cmd.StartExampleAPIKeyWarningServer(cfg, configFilePath, matches)
|
||||
return
|
||||
}
|
||||
|
||||
// Register the shared token store once so all components use the same persistence backend.
|
||||
if usePostgresStore {
|
||||
sdkAuth.RegisterTokenStore(pgStoreInst)
|
||||
@@ -659,90 +567,34 @@ func main() {
|
||||
|
||||
// Register built-in access providers before constructing services.
|
||||
configaccess.Register(&cfg.SDKConfig)
|
||||
pluginHost.ApplyConfig(context.Background(), cfg)
|
||||
if configLoadedFromHome {
|
||||
errHomePluginLoad := homeplugins.MarkLoadResults(&homePluginSyncReport, pluginHost)
|
||||
errReportPlugins := home.ReportPluginStatus(context.Background(), homeClient, cfg.Home.NodeID, homePluginSyncReport)
|
||||
if errHomePluginLoad != nil {
|
||||
log.Errorf("failed to load home plugins: %v", errHomePluginLoad)
|
||||
}
|
||||
if errReportPlugins != nil {
|
||||
log.Warnf("failed to report home plugin load status: %v", errReportPlugins)
|
||||
}
|
||||
if errHomePluginLoad != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if pluginHost.HasTriggeredCommandLineFlags() {
|
||||
if exitCode, handled := pluginHost.ExecuteCommandLine(context.Background(), os.Args[0], os.Args[1:], configFilePath, flag.CommandLine); handled {
|
||||
if exitCode != 0 {
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Handle different command modes based on the provided flags.
|
||||
|
||||
if vertexImport != "" {
|
||||
// Handle Vertex service account import
|
||||
cmd.DoVertexImport(cfg, vertexImport, vertexImportPrefix)
|
||||
} else if showVersion {
|
||||
// Version already printed at startup, just exit
|
||||
return
|
||||
} else if showStatus {
|
||||
if err := cmd.ShowStatus(jsonOutput); err != nil {
|
||||
log.Errorf("status failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if launchTUI {
|
||||
proxyURL := fmt.Sprintf("http://127.0.0.1:%d", cfg.Port)
|
||||
mgmtKey, _ := desktopctl.GetManagementPassword()
|
||||
if err := tui.Run(proxyURL, mgmtKey); err != nil {
|
||||
log.Errorf("tui failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if listAccounts {
|
||||
if err := cmd.ListAccounts(jsonOutput); err != nil {
|
||||
log.Errorf("list-accounts failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if listModels {
|
||||
if err := cmd.ListModels(jsonOutput); err != nil {
|
||||
log.Errorf("list-models failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if exportAccounts != "" {
|
||||
if err := cmd.ExportAccounts(exportAccounts, includeTokens, jsonOutput); err != nil {
|
||||
log.Errorf("export-accounts failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if importAccounts != "" {
|
||||
if err := cmd.ImportAccounts(importAccounts, forceImport, jsonOutput); err != nil {
|
||||
log.Errorf("import-accounts failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if cleanupExpired {
|
||||
if err := cmd.CleanupExpired(false); err != nil {
|
||||
log.Errorf("cleanup-expired failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if removeAccount != "" {
|
||||
if err := cmd.RemoveAccount(removeAccount); err != nil {
|
||||
log.Errorf("remove-account failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if refreshTokens != "" {
|
||||
identifier := ""
|
||||
if refreshTokens != "all" {
|
||||
identifier = refreshTokens
|
||||
}
|
||||
if err := cmd.RefreshTokens(cfg, identifier, jsonOutput); err != nil {
|
||||
log.Errorf("refresh failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if showUsage {
|
||||
if err := cmd.ShowUsage(jsonOutput); err != nil {
|
||||
log.Errorf("usage failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if showLogs {
|
||||
if err := cmd.ShowLogs(logLines, jsonOutput); err != nil {
|
||||
log.Errorf("logs failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
} else if login {
|
||||
// Handle Google/Gemini login
|
||||
cmd.DoLogin(cfg, projectID, options)
|
||||
} else if antigravityLogin {
|
||||
// Handle Antigravity login
|
||||
cmd.DoAntigravityLogin(cfg, options)
|
||||
@@ -755,69 +607,10 @@ func main() {
|
||||
} else if claudeLogin {
|
||||
// Handle Claude login
|
||||
cmd.DoClaudeLogin(cfg, options)
|
||||
} else if qwenLogin {
|
||||
cmd.DoQwenLogin(cfg, options)
|
||||
} else if kiroLogin {
|
||||
// For Kiro auth, default to incognito mode for multi-account support
|
||||
// Users can explicitly override with --no-incognito
|
||||
// Note: This config mutation is safe - auth commands exit after completion
|
||||
// and don't share config with StartService (which is in the else branch)
|
||||
setKiroIncognitoMode(cfg, useIncognito, noIncognito)
|
||||
cmd.DoKiroLogin(cfg, options)
|
||||
} else if kiroGoogleLogin {
|
||||
// For Kiro auth, default to incognito mode for multi-account support
|
||||
// Users can explicitly override with --no-incognito
|
||||
// Note: This config mutation is safe - auth commands exit after completion
|
||||
setKiroIncognitoMode(cfg, useIncognito, noIncognito)
|
||||
cmd.DoKiroGoogleLogin(cfg, options)
|
||||
} else if kiroAWSLogin {
|
||||
// For Kiro auth, default to incognito mode for multi-account support
|
||||
// Users can explicitly override with --no-incognito
|
||||
setKiroIncognitoMode(cfg, useIncognito, noIncognito)
|
||||
cmd.DoKiroAWSLogin(cfg, options)
|
||||
} else if kiroAWSAuthCode {
|
||||
// For Kiro auth with authorization code flow (better UX)
|
||||
setKiroIncognitoMode(cfg, useIncognito, noIncognito)
|
||||
cmd.DoKiroAWSAuthCodeLogin(cfg, options)
|
||||
} else if kiroImport {
|
||||
cmd.DoKiroImport(cfg, options)
|
||||
} else if antigravityImport {
|
||||
cmd.DoAntigravityImport(cfg)
|
||||
} else if minimaxLogin {
|
||||
cmd.DoMiniMaxLogin(cfg, options)
|
||||
} else if zhipuLogin {
|
||||
cmd.DoZhipuLogin(cfg, options)
|
||||
} else if iflowLogin {
|
||||
cmd.DoIFlowLogin(cfg, options)
|
||||
} else if iflowCookie {
|
||||
cmd.DoIFlowCookieAuth(cfg, options)
|
||||
} else if kimiLogin {
|
||||
cmd.DoKimiLogin(cfg, options)
|
||||
} else if detectAgents {
|
||||
cmd.DoDetectAgents()
|
||||
} else if setupClaude {
|
||||
cmd.DoSetupClaude(cfg)
|
||||
} else if setupCodex {
|
||||
cmd.DoSetupCodex(cfg)
|
||||
} else if setupDroid {
|
||||
cmd.DoSetupDroid(cfg)
|
||||
} else if setupOpenCode {
|
||||
cmd.DoSetupOpenCode(cfg)
|
||||
} else if setupGemini {
|
||||
cmd.DoSetupGeminiCLI(cfg)
|
||||
} else if setupCursor {
|
||||
cmd.DoSetupCursor(cfg)
|
||||
} else if setupKilo {
|
||||
cmd.DoSetupKiloCode(cfg)
|
||||
} else if setupRooCode {
|
||||
cmd.DoSetupRooCode(cfg)
|
||||
} else if setupAll {
|
||||
cmd.DoSetupAll(cfg)
|
||||
} else if subcommandSwitch || switchAgent != "" || switchMode != "" {
|
||||
// Handle switch command:
|
||||
// - Subcommand style: proxypilot switch claude proxy
|
||||
// - Flag style: proxypilot --switch claude --mode proxy
|
||||
cmd.DoSwitch(cfg, switchAgent, switchMode)
|
||||
} else if xaiLogin {
|
||||
cmd.DoXAILogin(cfg, options)
|
||||
} else {
|
||||
// In cloud deploy mode without config file, just wait for shutdown signals
|
||||
if isCloudDeploy && !configFileExists {
|
||||
@@ -825,33 +618,23 @@ func main() {
|
||||
cmd.WaitForCloudDeploy()
|
||||
return
|
||||
}
|
||||
|
||||
if localModel && (!launchTUI || standalone) {
|
||||
if localModel && (!tuiMode || standalone) {
|
||||
log.Info("Local model mode: using embedded model catalog, remote model updates disabled")
|
||||
}
|
||||
|
||||
// Auto-generate management password if not provided.
|
||||
// This enables browser-based webui access without requiring the -password flag.
|
||||
autoGenPassword := false
|
||||
if password == "" {
|
||||
if pw, err := desktopctl.GetManagementPassword(); err == nil {
|
||||
password = pw
|
||||
autoGenPassword = true
|
||||
log.Info("using auto-generated management password for webui access")
|
||||
} else {
|
||||
log.Warnf("failed to get management password: %v (webui may require manual authentication)", err)
|
||||
}
|
||||
}
|
||||
|
||||
if launchTUI {
|
||||
proxyURL := fmt.Sprintf("http://127.0.0.1:%d", cfg.Port)
|
||||
if tuiMode {
|
||||
if standalone {
|
||||
// Standalone mode: start an embedded local server and connect TUI client to it.
|
||||
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
||||
misc.StartAntigravityVersionUpdater(context.Background())
|
||||
if !localModel {
|
||||
if !localModel && !cfg.Home.Enabled {
|
||||
registry.StartModelsUpdater(context.Background())
|
||||
} else if cfg.Home.Enabled {
|
||||
log.Info("Home mode: remote model updates disabled")
|
||||
}
|
||||
hook := tui.NewLogHook(2000)
|
||||
hook.SetFormatter(&logging.LogFormatter{})
|
||||
log.AddHook(hook)
|
||||
|
||||
origStdout := os.Stdout
|
||||
origStderr := os.Stderr
|
||||
origLogOutput := log.StandardLogger().Out
|
||||
@@ -872,17 +655,18 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
localMgmtPassword := fmt.Sprintf("tui-%d-%d", os.Getpid(), time.Now().UnixNano())
|
||||
if password == "" {
|
||||
password = fmt.Sprintf("tui-%d-%d", os.Getpid(), time.Now().UnixNano())
|
||||
password = localMgmtPassword
|
||||
}
|
||||
|
||||
cancel, done := cmd.StartServiceBackground(cfg, configFilePath, password)
|
||||
cancel, done := cmd.StartServiceBackgroundWithPluginHost(cfg, configFilePath, password, pluginHost)
|
||||
|
||||
client := tui.NewClient(proxyURL, password)
|
||||
client := tui.NewClient(cfg.Port, password)
|
||||
ready := false
|
||||
backoff := 100 * time.Millisecond
|
||||
for i := 0; i < 30; i++ {
|
||||
if _, errFetchStatus := client.FetchStatus(); errFetchStatus == nil {
|
||||
if _, errGetConfig := client.GetConfig(); errGetConfig == nil {
|
||||
ready = true
|
||||
break
|
||||
}
|
||||
@@ -900,7 +684,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if errRun := tui.RunWithStdio(proxyURL, password, os.Stdin, origStdout); errRun != nil {
|
||||
if errRun := tui.Run(cfg.Port, password, hook, origStdout); errRun != nil {
|
||||
restoreIO()
|
||||
fmt.Fprintf(os.Stderr, "TUI error: %v\n", errRun)
|
||||
} else {
|
||||
@@ -912,21 +696,76 @@ func main() {
|
||||
} else {
|
||||
// Default TUI mode: pure management client.
|
||||
// The proxy server must already be running.
|
||||
if errRun := tui.Run(proxyURL, password); errRun != nil {
|
||||
if errRun := tui.Run(cfg.Port, password, nil, os.Stdout); errRun != nil {
|
||||
fmt.Fprintf(os.Stderr, "TUI error: %v\n", errRun)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Start the main proxy service
|
||||
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
||||
misc.StartAntigravityVersionUpdater(context.Background())
|
||||
if !localModel {
|
||||
if !localModel && !cfg.Home.Enabled {
|
||||
registry.StartModelsUpdater(context.Background())
|
||||
} else if cfg.Home.Enabled {
|
||||
log.Info("Home mode: remote model updates disabled")
|
||||
}
|
||||
if autoGenPassword {
|
||||
cmd.StartServiceStandalone(cfg, configFilePath, password)
|
||||
} else {
|
||||
cmd.StartService(cfg, configFilePath, password)
|
||||
}
|
||||
cmd.StartServiceWithPluginHost(cfg, configFilePath, password, pluginHost)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func pluginBootstrapConfigPath(args []string, defaultPath string) string {
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
switch {
|
||||
case arg == "--":
|
||||
return defaultPluginBootstrapConfigPath(defaultPath)
|
||||
case arg == "-config" || arg == "--config":
|
||||
if i+1 < len(args) {
|
||||
return args[i+1]
|
||||
}
|
||||
return defaultPluginBootstrapConfigPath(defaultPath)
|
||||
case strings.HasPrefix(arg, "-config="):
|
||||
return strings.TrimPrefix(arg, "-config=")
|
||||
case strings.HasPrefix(arg, "--config="):
|
||||
return strings.TrimPrefix(arg, "--config=")
|
||||
}
|
||||
}
|
||||
return defaultPluginBootstrapConfigPath(defaultPath)
|
||||
}
|
||||
|
||||
func defaultPluginBootstrapConfigPath(defaultPath string) string {
|
||||
if strings.TrimSpace(defaultPath) != "" {
|
||||
return defaultPath
|
||||
}
|
||||
wd, errGetwd := os.Getwd()
|
||||
if errGetwd != nil {
|
||||
return "config.yaml"
|
||||
}
|
||||
return filepath.Join(wd, "config.yaml")
|
||||
}
|
||||
|
||||
func loadPluginBootstrapConfig(path string) *config.Config {
|
||||
raw, errReadFile := os.ReadFile(path)
|
||||
if errReadFile != nil {
|
||||
if !errors.Is(errReadFile, os.ErrNotExist) {
|
||||
log.Warnf("failed to read plugin bootstrap config: %v", errReadFile)
|
||||
}
|
||||
cfg := &config.Config{}
|
||||
cfg.NormalizePluginsConfig()
|
||||
return cfg
|
||||
}
|
||||
if len(strings.TrimSpace(string(raw))) == 0 {
|
||||
cfg := &config.Config{}
|
||||
cfg.NormalizePluginsConfig()
|
||||
return cfg
|
||||
}
|
||||
cfg, errParseConfig := config.ParseConfigBytes(raw)
|
||||
if errParseConfig != nil {
|
||||
log.Warnf("failed to parse plugin bootstrap config: %v", errParseConfig)
|
||||
cfg = &config.Config{}
|
||||
cfg.NormalizePluginsConfig()
|
||||
return cfg
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
)
|
||||
|
||||
func TestShouldStartExampleAPIKeyWarningServer(t *testing.T) {
|
||||
cfgWithExampleKey := &config.Config{
|
||||
SDKConfig: config.SDKConfig{
|
||||
APIKeys: []string{"real-key", " your-api-key-1 "},
|
||||
},
|
||||
}
|
||||
cfgWithRealKey := &config.Config{
|
||||
SDKConfig: config.SDKConfig{
|
||||
APIKeys: []string{"real-key"},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *config.Config
|
||||
commandMode bool
|
||||
tuiMode bool
|
||||
standalone bool
|
||||
cloudConfigMissing bool
|
||||
homeMode bool
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "normal server with example key",
|
||||
cfg: cfgWithExampleKey,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "standalone tui with example key",
|
||||
cfg: cfgWithExampleKey,
|
||||
tuiMode: true,
|
||||
standalone: true,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "pure tui client is not blocked",
|
||||
cfg: cfgWithExampleKey,
|
||||
tuiMode: true,
|
||||
standalone: false,
|
||||
commandMode: false,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "one-shot command is not blocked",
|
||||
cfg: cfgWithExampleKey,
|
||||
commandMode: true,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "home mode is not blocked",
|
||||
cfg: cfgWithExampleKey,
|
||||
homeMode: true,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "cloud standby without config is not blocked",
|
||||
cfg: cfgWithExampleKey,
|
||||
cloudConfigMissing: true,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "normal server with real key",
|
||||
cfg: cfgWithRealKey,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "nil config",
|
||||
cfg: nil,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := shouldStartExampleAPIKeyWarningServer(tt.cfg, tt.commandMode, tt.tuiMode, tt.standalone, tt.cloudConfigMissing, tt.homeMode)
|
||||
if got != tt.want {
|
||||
t.Fatalf("shouldStartExampleAPIKeyWarningServer() = %t, want %t", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,11 @@ import (
|
||||
"golang.org/x/sys/windows/svc/eventlog"
|
||||
"golang.org/x/sys/windows/svc/mgr"
|
||||
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v7/internal/access/config_access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy"
|
||||
)
|
||||
|
||||
const serviceName = "ProxyPilot"
|
||||
|
||||
+154
-69
@@ -49,7 +49,31 @@ pprof:
|
||||
enable: false
|
||||
addr: "127.0.0.1:8316"
|
||||
|
||||
# When true, disable high-overhead HTTP middleware features to reduce per-request memory usage under high concurrency.
|
||||
# Standard dynamic library plugins are trusted in-process code. They are disabled by default.
|
||||
# Build Go examples with go build -buildmode=c-shared for the target GOOS/GOARCH.
|
||||
# Other languages can implement the same C ABI and JSON method protocol.
|
||||
# Plugin executors require a matching auth record with the same provider key.
|
||||
# If the same provider is configured as OpenAI-compatible, the native executor wins.
|
||||
# Plugin command-line flags and Management API routes are optional capabilities.
|
||||
# Existing native flags/routes and higher-priority plugin flags/routes cannot be replaced.
|
||||
# Plugin list Management API reads Logo and ConfigFields from plugin metadata for management UI display.
|
||||
# Per-plugin enabled only controls plugins.configs.<pluginID>.enabled and does not implicitly change global plugins.enabled.
|
||||
plugins:
|
||||
enabled: false
|
||||
dir: "plugins"
|
||||
# Additional plugin store registries. The built-in official registry is always included.
|
||||
# store-sources:
|
||||
# - "https://example.com/cliproxy-plugins/registry.json"
|
||||
configs:
|
||||
example:
|
||||
enabled: true
|
||||
priority: 1
|
||||
config1: true
|
||||
config2: "string"
|
||||
config3: 3
|
||||
mode: "safe" # enum example: safe, fast
|
||||
|
||||
# When true, disable high-overhead request logging and HTTP middleware features to reduce per-request memory usage under high concurrency.
|
||||
commercial-mode: false
|
||||
|
||||
# When true, write application logs to rotating files instead of stdout
|
||||
@@ -66,6 +90,11 @@ error-logs-max-files: 10
|
||||
# When false, disable in-memory usage statistics aggregation
|
||||
usage-statistics-enabled: false
|
||||
|
||||
# How long (in seconds) usage queue items are retained in memory for the Management API.
|
||||
# The local Redis RESP usage output is disabled.
|
||||
# Default: 60. Max: 3600.
|
||||
redis-usage-queue-retention-seconds: 60
|
||||
|
||||
# Proxy URL. Supports socks5/http/https protocols. Example: socks5://user:pass@192.168.1.1:1080/
|
||||
# Per-entry proxy-url also supports "direct" or "none" to bypass both the global proxy-url and environment proxies explicitly.
|
||||
proxy-url: ""
|
||||
@@ -87,35 +116,93 @@ max-retry-credentials: 0
|
||||
# Maximum wait time in seconds for a cooled-down credential before triggering a retry.
|
||||
max-retry-interval: 30
|
||||
|
||||
# When true, disable auth/model cooldown scheduling globally (prevents blackout windows after failure states).
|
||||
disable-cooling: false
|
||||
|
||||
# When true, persist per-auth cooldown status as .cds files next to auth files.
|
||||
# Default is false; when false, cooldown status is kept in memory only.
|
||||
save-cooldown-status: false
|
||||
|
||||
# Cooldown duration in seconds for transient upstream errors (408/500/502/503/504).
|
||||
# Set to 0 to keep the legacy 60-second cooldown; set to -1 to disable transient error cooldowns.
|
||||
transient-error-cooldown-seconds: 0
|
||||
|
||||
# When true, globally disable Claude request cloaking (the Claude Code CLI disguise and
|
||||
# system prompt replacement), so the original system prompt is passed through to Claude as-is.
|
||||
# Individual credentials can still override this: a claude-api-key entry via its "cloak.mode",
|
||||
# or a Claude OAuth/token file via a "cloak_mode" value. Default false keeps the per-client
|
||||
# "auto" behavior (cloak only non-Claude-Code clients).
|
||||
disable-claude-cloak-mode: false
|
||||
|
||||
# disable-image-generation supports: false (default), true, "chat", or "passthrough".
|
||||
# - true: disable image_generation everywhere (also returns 404 for /v1/images/generations and /v1/images/edits).
|
||||
# - "chat": disable image_generation injection on non-images endpoints, but keep /v1/images/generations and /v1/images/edits enabled.
|
||||
# - "passthrough": never inject or strip image_generation on non-images endpoints (forward the client payload unchanged); behaves like "chat" on /v1/images/* endpoints.
|
||||
disable-image-generation: false
|
||||
|
||||
# Base model used by the legacy hosted image_generation tool path when a Codex image request is not proxied directly through the Image API.
|
||||
# Must start with "gpt-" (case-insensitive). If unset or invalid, defaults to "gpt-5.4-mini".
|
||||
# gpt-image-2-base-model: "gpt-5.4-mini"
|
||||
|
||||
# How long video IDs returned by /openai/v1/videos and xAI video creation stay bound
|
||||
# to the credential that created them. Default: 3h.
|
||||
video-result-auth-cache-ttl: "3h"
|
||||
|
||||
# Core auth auto-refresh worker pool size (OAuth/file-based auth token refresh).
|
||||
# When > 0, overrides the default worker count (16).
|
||||
# auth-auto-refresh-workers: 16
|
||||
|
||||
# Quota exceeded behavior
|
||||
quota-exceeded:
|
||||
switch-project: true # Whether to automatically switch to another project when a quota is exceeded
|
||||
switch-preview-model: true # Whether to automatically switch to a preview model when a quota is exceeded
|
||||
antigravity-credits: true # Whether to retry Antigravity quota_exhausted 429s once with enabledCreditTypes=["GOOGLE_ONE_AI"]
|
||||
antigravity-credits: true # Whether to use credits as last-resort fallback when all free-tier auths are exhausted for Claude models
|
||||
|
||||
# Routing strategy for selecting credentials when multiple match.
|
||||
routing:
|
||||
strategy: "round-robin" # round-robin (default), fill-first
|
||||
# Enable universal session-sticky routing for all clients.
|
||||
# Session IDs are extracted from: metadata.user_id (Claude Code session format),
|
||||
# X-Session-ID, Session_id (Codex), X-Client-Request-Id (PI), conversation_id,
|
||||
# or first few messages hash.
|
||||
# Automatic failover is always enabled when bound auth becomes unavailable.
|
||||
session-affinity: false # default: false
|
||||
# How long session-to-auth bindings are retained. Default: 1h
|
||||
session-affinity-ttl: "1h"
|
||||
|
||||
# Codex provider behavior.
|
||||
codex:
|
||||
# When true, and routing.strategy is fill-first or routing.session-affinity is true,
|
||||
# remap Codex prompt_cache_key and installation identity per selected auth.
|
||||
# Some superstitious users believe request tracking identifiers can be used
|
||||
# as evidence for TOS enforcement bans; this option only satisfies those odd concerns.
|
||||
identity-confuse: false
|
||||
|
||||
# When true, enable authentication for the WebSocket API (/v1/ws).
|
||||
ws-auth: false
|
||||
|
||||
# When true, enable Gemini CLI internal endpoints (/v1internal:*).
|
||||
# Default is false for safety.
|
||||
enable-gemini-cli-endpoint: false
|
||||
ws-auth: true
|
||||
|
||||
# When > 0, emit blank lines every N seconds for non-streaming responses to prevent idle timeouts.
|
||||
nonstream-keepalive-interval: 0
|
||||
|
||||
# Streaming behavior (SSE keep-alives + safe bootstrap retries).
|
||||
# streaming:
|
||||
# keepalive-seconds: 15 # Default: 0 (disabled). <= 0 disables keep-alives.
|
||||
# bootstrap-retries: 1 # Default: 0 (disabled). Retries before first byte is sent.
|
||||
|
||||
# Signature cache validation for thinking blocks (Antigravity/Claude).
|
||||
# When true (default), cached signatures are preferred and validated.
|
||||
# When false, client signatures are used directly after normalization (bypass mode for testing).
|
||||
# antigravity-signature-cache-enabled: true
|
||||
|
||||
# Bypass mode signature validation strictness (only applies when signature cache is disabled).
|
||||
# When true, validates full Claude protobuf tree (Field 2 -> Field 1 structure).
|
||||
# When false (default), only checks R/E prefix + base64 + first byte 0x12.
|
||||
# antigravity-signature-bypass-strict: false
|
||||
|
||||
# Gemini API keys
|
||||
# gemini-api-key:
|
||||
# - api-key: "AIzaSy...01"
|
||||
# prefix: "test" # optional: require calls like "test/gemini-3-pro-preview" to target this credential
|
||||
# disable-cooling: false # optional: per-auth override for auth/model cooldown scheduling
|
||||
# base-url: "https://generativelanguage.googleapis.com"
|
||||
# headers:
|
||||
# X-Custom-Header: "custom-value"
|
||||
@@ -135,6 +222,7 @@ nonstream-keepalive-interval: 0
|
||||
# codex-api-key:
|
||||
# - api-key: "sk-atSM..."
|
||||
# prefix: "test" # optional: require calls like "test/gpt-5-codex" to target this credential
|
||||
# disable-cooling: false # optional: per-auth override for auth/model cooldown scheduling
|
||||
# base-url: "https://www.example.com" # use the custom codex API endpoint
|
||||
# headers:
|
||||
# X-Custom-Header: "custom-value"
|
||||
@@ -154,6 +242,7 @@ nonstream-keepalive-interval: 0
|
||||
# - api-key: "sk-atSM..." # use the official claude API key, no need to set the base url
|
||||
# - api-key: "sk-atSM..."
|
||||
# prefix: "test" # optional: require calls like "test/claude-sonnet-latest" to target this credential
|
||||
# disable-cooling: false # optional: per-auth override for auth/model cooldown scheduling
|
||||
# base-url: "https://www.example.com" # use the custom claude API endpoint
|
||||
# headers:
|
||||
# X-Custom-Header: "custom-value"
|
||||
@@ -167,10 +256,15 @@ nonstream-keepalive-interval: 0
|
||||
# - "claude-3-*" # wildcard matching prefix (e.g. claude-3-7-sonnet-20250219)
|
||||
# - "*-thinking" # wildcard matching suffix (e.g. claude-opus-4-5-thinking)
|
||||
# - "*haiku*" # wildcard matching substring (e.g. claude-3-5-haiku-20241022)
|
||||
# rebuild-mid-system-message: false # optional: default is false; when true, move messages with role "system" into the top-level Claude system field
|
||||
# cloak: # optional: request cloaking for non-Claude-Code clients
|
||||
# mode: "auto" # "auto" (default): cloak only when client is not Claude Code
|
||||
# # "always": always apply cloaking
|
||||
# # "never": never apply cloaking
|
||||
# # This "cloak" block applies to this claude-api-key entry only. For Claude OAuth
|
||||
# # credentials, set the same options in the auth/token JSON file via "cloak_mode" /
|
||||
# # "cloak_strict_mode" / "cloak_sensitive_words" / "cloak_cache_user_id". The top-level
|
||||
# # "disable-claude-cloak-mode: true" disables cloaking for all Claude credentials at once.
|
||||
# strict-mode: false # false (default): prepend Claude Code prompt to user system messages
|
||||
# # true: strip all user system messages, keep only Claude Code prompt
|
||||
# sensitive-words: # optional: words to obfuscate with zero-width characters
|
||||
@@ -206,8 +300,10 @@ nonstream-keepalive-interval: 0
|
||||
# OpenAI compatibility providers
|
||||
# openai-compatibility:
|
||||
# - name: "openrouter" # The name of the provider; it will be used in the user agent and other places.
|
||||
# disabled: false # optional: set to true to disable this provider without removing it
|
||||
# prefix: "test" # optional: require calls like "test/kimi-k2" to target this provider's credentials
|
||||
# base-url: "https://openrouter.ai/api/v1" # The base URL of the provider.
|
||||
# disable-cooling: false # optional: per-provider override for auth/model cooldown scheduling
|
||||
# headers:
|
||||
# X-Custom-Header: "custom-value"
|
||||
# api-key-entries:
|
||||
@@ -218,6 +314,7 @@ nonstream-keepalive-interval: 0
|
||||
# models: # The models supported by the provider.
|
||||
# - name: "moonshotai/kimi-k2:free" # The actual model name.
|
||||
# alias: "kimi-k2" # The alias used in the API.
|
||||
# image: false # optional: set true to allow this model on /v1/images/generations and /v1/images/edits
|
||||
# thinking: # optional: omit to default to levels ["low","medium","high"]
|
||||
# levels: ["low", "medium", "high"]
|
||||
# # You may repeat the same alias to build an internal model pool.
|
||||
@@ -225,7 +322,7 @@ nonstream-keepalive-interval: 0
|
||||
# # Requests to that alias will round-robin across the upstream names below,
|
||||
# # and if the chosen upstream fails before producing output, the request will
|
||||
# # continue with the next upstream model in the same alias pool.
|
||||
# - name: "qwen3.5-plus"
|
||||
# - name: "deepseek-v3.1"
|
||||
# alias: "claude-opus-4.66"
|
||||
# - name: "glm-5"
|
||||
# alias: "claude-opus-4.66"
|
||||
@@ -250,54 +347,29 @@ nonstream-keepalive-interval: 0
|
||||
# - "imagen-3.0-generate-002"
|
||||
# - "imagen-*"
|
||||
|
||||
# Amp Integration
|
||||
# ampcode:
|
||||
# # Configure upstream URL for Amp CLI OAuth and management features
|
||||
# upstream-url: "https://ampcode.com"
|
||||
# # Optional: Override API key for Amp upstream (otherwise uses env or file)
|
||||
# upstream-api-key: ""
|
||||
# # Per-client upstream API key mapping
|
||||
# # Maps client API keys (from top-level api-keys) to different Amp upstream API keys.
|
||||
# # Useful when different clients need to use different Amp accounts/quotas.
|
||||
# # If a client key isn't mapped, falls back to upstream-api-key (default behavior).
|
||||
# upstream-api-keys:
|
||||
# - upstream-api-key: "amp_key_for_team_a" # Upstream key to use for these clients
|
||||
# api-keys: # Client keys that use this upstream key
|
||||
# - "your-api-key-1"
|
||||
# - "your-api-key-2"
|
||||
# - upstream-api-key: "amp_key_for_team_b"
|
||||
# api-keys:
|
||||
# - "your-api-key-3"
|
||||
# # Restrict Amp management routes (/api/auth, /api/user, etc.) to localhost only (default: false)
|
||||
# restrict-management-to-localhost: false
|
||||
# # Force model mappings to run before checking local API keys (default: false)
|
||||
# force-model-mappings: false
|
||||
# # Amp Model Mappings
|
||||
# # Route unavailable Amp models to alternative models available in your local proxy.
|
||||
# # Useful when Amp CLI requests models you don't have access to (e.g., Claude Opus 4.5)
|
||||
# # but you have a similar model available (e.g., Claude Sonnet 4).
|
||||
# model-mappings:
|
||||
# - from: "claude-opus-4-5-20251101" # Model requested by Amp CLI
|
||||
# to: "gemini-claude-opus-4-5-thinking" # Route to this available model instead
|
||||
# - from: "claude-sonnet-4-5-20250929"
|
||||
# to: "gemini-claude-sonnet-4-5-thinking"
|
||||
# - from: "claude-haiku-4-5-20251001"
|
||||
# to: "gemini-2.5-flash"
|
||||
|
||||
# Global OAuth model name aliases (per channel)
|
||||
# These aliases rename model IDs for both model listing and request routing.
|
||||
# Supported channels: gemini-cli, vertex, aistudio, antigravity, claude, codex, qwen, iflow, kimi.
|
||||
# NOTE: Aliases do not apply to gemini-api-key, codex-api-key, claude-api-key, openai-compatibility, vertex-api-key, or ampcode.
|
||||
# Supported channels: vertex, aistudio, antigravity, claude, codex, kimi, xai.
|
||||
# NOTE: Aliases do not apply to gemini-api-key, codex-api-key, claude-api-key, openai-compatibility, or vertex-api-key.
|
||||
# NOTE: Because aliases affect the merged /v1 model list and merged request routing, overlapping
|
||||
# client-visible names can become ambiguous across providers. /api/provider/{provider}/... helps
|
||||
# you select the protocol surface, but inference backend selection can still follow the resolved
|
||||
# model/alias. For strict backend pinning, use unique aliases/prefixes or avoid overlapping names.
|
||||
# client-visible names can become ambiguous across providers. For strict backend pinning, use
|
||||
# unique aliases/prefixes or avoid overlapping names.
|
||||
# You can repeat the same name with different aliases to expose multiple client model names.
|
||||
# Optional per-entry flags:
|
||||
# fork: true # keep the upstream model and also expose the alias as a separate client-visible model
|
||||
# force-mapping: true # optional: rewrite upstream response model fields back to the client-visible alias (example below uses antigravity only)
|
||||
# Per-auth OAuth aliases can also be stored in an OAuth auth JSON file as "model-aliases".
|
||||
# They apply only to that selected auth and take precedence over global aliases for the same client-visible alias.
|
||||
# Example auth JSON:
|
||||
# {
|
||||
# "type": "codex",
|
||||
# "email": "user@example.com",
|
||||
# "model-aliases": [
|
||||
# {"name": "gpt-5.3-codex-spark", "alias": "gpt-5.5"},
|
||||
# {"name": "gpt-5.3-codex-spark", "alias": "gpt-5.4"}
|
||||
# ]
|
||||
# }
|
||||
# oauth-model-alias:
|
||||
# gemini-cli:
|
||||
# - name: "gemini-2.5-pro" # original model name under this channel
|
||||
# alias: "g2.5p" # client-visible alias
|
||||
# fork: true # when true, keep original and also add the alias as an extra model (default: false)
|
||||
# vertex:
|
||||
# - name: "gemini-2.5-pro"
|
||||
# alias: "g2.5p"
|
||||
@@ -305,31 +377,28 @@ nonstream-keepalive-interval: 0
|
||||
# - name: "gemini-2.5-pro"
|
||||
# alias: "g2.5p"
|
||||
# antigravity:
|
||||
# - name: "gemini-3-pro-high"
|
||||
# alias: "gemini-3-pro-preview"
|
||||
# - name: "gemini-pro-agent" # upstream Antigravity model id
|
||||
# alias: "gemini-3.1-pro-preview" # client-visible id (Gemini 3.1 Pro Preview)
|
||||
# fork: true
|
||||
# force-mapping: true
|
||||
# claude:
|
||||
# - name: "claude-sonnet-4-5-20250929"
|
||||
# alias: "cs4.5"
|
||||
# codex:
|
||||
# - name: "gpt-5"
|
||||
# alias: "g5"
|
||||
# qwen:
|
||||
# - name: "qwen3-coder-plus"
|
||||
# alias: "qwen-plus"
|
||||
# iflow:
|
||||
# - name: "glm-4.7"
|
||||
# alias: "glm-god"
|
||||
# kimi:
|
||||
# - name: "kimi-k2.5"
|
||||
# alias: "k2.5"
|
||||
# xai:
|
||||
# - name: "grok-4.3"
|
||||
# alias: "grok-latest"
|
||||
# sample-provider: # plugin provider keys are supported for OAuth plugins
|
||||
# - name: "sample-model-latest"
|
||||
# alias: "sample-latest"
|
||||
|
||||
# OAuth provider excluded models
|
||||
# oauth-excluded-models:
|
||||
# gemini-cli:
|
||||
# - "gemini-2.5-pro" # exclude specific models (exact match)
|
||||
# - "gemini-2.5-*" # wildcard matching prefix (e.g. gemini-2.5-flash, gemini-2.5-pro)
|
||||
# - "*-preview" # wildcard matching suffix (e.g. gemini-3-pro-preview)
|
||||
# - "*flash*" # wildcard matching substring (e.g. gemini-2.5-flash-lite)
|
||||
# vertex:
|
||||
# - "gemini-3-pro-preview"
|
||||
# aistudio:
|
||||
@@ -340,12 +409,10 @@ nonstream-keepalive-interval: 0
|
||||
# - "claude-3-5-haiku-20241022"
|
||||
# codex:
|
||||
# - "gpt-5-codex-mini"
|
||||
# qwen:
|
||||
# - "vision-model"
|
||||
# iflow:
|
||||
# - "tstars2.0"
|
||||
# kimi:
|
||||
# - "kimi-k2-thinking"
|
||||
# xai:
|
||||
# - "grok-3-mini"
|
||||
|
||||
# Optional payload configuration
|
||||
# payload:
|
||||
@@ -353,6 +420,17 @@ nonstream-keepalive-interval: 0
|
||||
# - models:
|
||||
# - name: "gemini-2.5-pro" # Supports wildcards (e.g., "gemini-*")
|
||||
# protocol: "gemini" # restricts the rule to a specific protocol, options: openai, gemini, claude, codex, antigravity
|
||||
# from-protocol: "responses" # restricts the rule to the source protocol, options: openai, responses, gemini, claude
|
||||
# headers: # all configured request headers must match; values support "*" wildcards
|
||||
# X-Client-Tier: "tenant-*-region-*"
|
||||
# match: # all payload JSON paths must equal the configured values
|
||||
# - "metadata.client": "codex"
|
||||
# not-match: # payload JSON paths must not equal the configured values
|
||||
# - "metadata.mode": "dev"
|
||||
# exist: # all payload JSON paths must exist and not be null
|
||||
# - "tools.#(type==\"web_search\").type"
|
||||
# not-exist: # all payload JSON paths must be missing or null
|
||||
# - "metadata.disable_payload"
|
||||
# params: # JSON path (gjson/sjson syntax) -> value
|
||||
# "generationConfig.thinkingConfig.thinkingBudget": 32768
|
||||
# default-raw: # Default raw rules set parameters using raw JSON when missing (must be valid JSON).
|
||||
@@ -363,6 +441,13 @@ nonstream-keepalive-interval: 0
|
||||
# "generationConfig.responseJsonSchema": "{\"type\":\"object\",\"properties\":{\"answer\":{\"type\":\"string\"}}}"
|
||||
# override: # Override rules always set parameters, overwriting any existing values.
|
||||
# - models:
|
||||
# - name: "gpt-5.4-fast"
|
||||
# protocol: "codex"
|
||||
# - name: "gpt-5.5-fast"
|
||||
# protocol: "codex"
|
||||
# params:
|
||||
# service_tier: priority
|
||||
# - models:
|
||||
# - name: "gpt-*" # Supports wildcards (e.g., "gpt-*")
|
||||
# protocol: "codex" # restricts the rule to a specific protocol, options: openai, gemini, claude, codex, antigravity
|
||||
# params: # JSON path (gjson/sjson syntax) -> value
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
services:
|
||||
cli-proxy-api:
|
||||
image: ${CLI_PROXY_IMAGE:-eceasy/cli-proxy-api:latest}
|
||||
pull_policy: always
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
VERSION: ${VERSION:-dev}
|
||||
COMMIT: ${COMMIT:-none}
|
||||
BUILD_DATE: ${BUILD_DATE:-unknown}
|
||||
container_name: cli-proxy-api-cluster
|
||||
environment:
|
||||
HOME_JWT: ${HOME_JWT:-}
|
||||
ports:
|
||||
- "8317:8317"
|
||||
volumes:
|
||||
- ./home:/root/.cli-proxy-api
|
||||
- ./logs:/CLIProxyAPI/logs
|
||||
command: >
|
||||
sh -eu -c '
|
||||
if [ -z "$$HOME_JWT" ]; then
|
||||
echo "HOME_JWT is required" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec ./CLIProxyAPI -home-jwt "$$HOME_JWT"
|
||||
'
|
||||
restart: unless-stopped
|
||||
+4
-4
@@ -17,10 +17,10 @@ services:
|
||||
ports:
|
||||
- "8317:8317"
|
||||
- "8085:8085"
|
||||
- "1455:1455"
|
||||
- "54545:54545"
|
||||
- "51121:51121"
|
||||
- "11451:11451"
|
||||
- "127.0.0.1:1455:1455"
|
||||
- "127.0.0.1:54545:54545"
|
||||
- "127.0.0.1:51121:51121"
|
||||
- "127.0.0.1:11451:11451"
|
||||
volumes:
|
||||
- ${CLI_PROXY_CONFIG_PATH:-./config.yaml}:/ProxyPilot/config.yaml
|
||||
- ${CLI_PROXY_AUTH_PATH:-./auths}:/root/.cli-proxy-api
|
||||
|
||||
@@ -32,6 +32,25 @@ Important:
|
||||
- Avoid Claude Code “Auth conflict”: do **not** set both `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_API_KEY` at the same time.
|
||||
- Restart Claude Code after editing settings so the new env vars take effect.
|
||||
|
||||
## Account risk warning
|
||||
|
||||
Claude/Anthropic OAuth, Google/Gemini OAuth, Gemini CLI OAuth, and Antigravity OAuth are unofficial compatibility paths. Providers may treat proxy, automation, shared-token, or third-party OAuth traffic as abnormal usage or a policy violation. These flows can risk quota loss, account restriction, or account suspension.
|
||||
|
||||
Do not use production, billing-critical, school/work, or irreplaceable personal accounts with these OAuth paths unless you accept that risk. Prefer official API keys or officially supported developer access for important accounts.
|
||||
|
||||
## Claude OAuth quota limitation
|
||||
|
||||
Anthropic currently treats third-party app traffic differently from regular Claude.ai or Claude Code subscription usage. If a Claude OAuth request fails with:
|
||||
|
||||
```text
|
||||
Third-party apps now draw from your extra usage, not your plan limits.
|
||||
Add more at claude.ai/settings/usage and keep going.
|
||||
```
|
||||
|
||||
that response is coming from Anthropic, not ProxyPilot. It means the OAuth credential is valid, but Anthropic is requiring paid extra usage for this third-party API path. ProxyPilot cannot make those requests draw from normal Claude.ai subscription quota.
|
||||
|
||||
For Claude through ProxyPilot, either add extra usage in Anthropic settings, use a supported API-key billing path, or route the client to another provider/model alias such as Antigravity, Codex, Gemini, or Kiro.
|
||||
|
||||
## Server-side model aliasing (Claude handler)
|
||||
|
||||
CLIProxyAPI rewrites common Claude Code model aliases to locally-available models before routing:
|
||||
|
||||
+8
-4
@@ -57,12 +57,16 @@ proxypilot --codex-login # Login to Codex using OAuth
|
||||
proxypilot --claude-login # Login to Claude using OAuth
|
||||
proxypilot --qwen-login # Login to Qwen using OAuth
|
||||
proxypilot --antigravity-login # Login to Antigravity using OAuth
|
||||
proxypilot --kiro-login # Login to Kiro using Google OAuth
|
||||
proxypilot --kiro-aws-login # Login to Kiro using AWS Builder ID
|
||||
proxypilot --kiro-login # Login to Kiro using AWS Builder ID
|
||||
proxypilot --kiro-aws-login # Login to Kiro using AWS Builder ID device code
|
||||
proxypilot --minimax-login # Add MiniMax API key
|
||||
proxypilot --zhipu-login # Add Zhipu AI API key
|
||||
```
|
||||
|
||||
Account risk warning: `--claude-login`, `--login` / Gemini, Gemini CLI OAuth, and `--antigravity-login` are unofficial compatibility login paths. Providers may treat proxy, automation, shared-token, or third-party OAuth traffic as abnormal usage or a policy violation. These flows can risk quota loss, account restriction, or account suspension. Prefer official API-key or officially supported developer access for accounts you cannot afford to lose.
|
||||
|
||||
Kiro Google/GitHub OAuth is not currently supported because AWS Cognito rejects third-party localhost callbacks. Use AWS Builder ID or import a token from the official Kiro IDE instead.
|
||||
|
||||
### Login Options
|
||||
|
||||
```bash
|
||||
@@ -309,8 +313,8 @@ proxypilot --codex-login # Codex OAuth
|
||||
proxypilot --claude-login # Claude OAuth
|
||||
proxypilot --qwen-login # Qwen OAuth
|
||||
proxypilot --antigravity-login # Antigravity OAuth
|
||||
proxypilot --kiro-login # Kiro Google OAuth
|
||||
proxypilot --kiro-aws-login # Kiro AWS Builder ID
|
||||
proxypilot --kiro-login # Kiro AWS Builder ID
|
||||
proxypilot --kiro-aws-login # Kiro AWS Builder ID device code
|
||||
|
||||
# Agent Detection & Setup
|
||||
proxypilot --detect-agents # Detect installed agents
|
||||
|
||||
@@ -61,6 +61,8 @@ ProxyPilot tray app includes:
|
||||
- One-click login launchers: Antigravity, Gemini CLI, Codex, Claude, Qwen
|
||||
- `Open Auth Folder` shortcut
|
||||
|
||||
Account risk warning: Antigravity, Gemini/Gemini CLI, and Claude login launchers use unofficial compatibility OAuth paths. Providers may treat proxy, automation, shared-token, or third-party OAuth traffic as abnormal usage or a policy violation. These flows can risk quota loss, account restriction, or account suspension. Use disposable or non-critical accounts only if you accept that risk; prefer official API keys or officially supported developer access for important accounts.
|
||||
|
||||
## Logs / diagnostics
|
||||
|
||||
Useful paths:
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Session 2026-04-11
|
||||
|
||||
- Targeted upstream sync: `CLIProxyAPI v6.9.22`
|
||||
- Merge base from shipping line: `v0.3.0-dev-0.53` on `main`
|
||||
- Upstream merge branch: `sync-upstream-v6.9.18-to-v6.9.22`
|
||||
- Conflict resolution:
|
||||
- kept ProxyPilot-local `.gitignore` entries while accepting upstream code changes
|
||||
- updated upstream tracking markers and release metadata to `v6.9.22`
|
||||
- Planned release target after validation: `v0.3.0-dev-0.54`
|
||||
@@ -0,0 +1,13 @@
|
||||
# Session 2026-04-12
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.23` (`0ab1f54173d477d6a39457b6aaf1f663b315fdb2`)
|
||||
- Base local release line before merge: `ProxyPilot v0.3.0-dev-0.54` on `main`
|
||||
- Merge branch: `sync-upstream-v6.9.22-to-v6.9.23`
|
||||
- Conflict resolution:
|
||||
- kept ProxyPilot branding and docs flow in `README.md`
|
||||
- kept upstream Qwen executor/runtime changes from `v6.9.23`
|
||||
- did not revive upstream locale README files (`README_CN.md`, `README_JA.md`)
|
||||
- Validation plan:
|
||||
- `mise exec go@1.26.0 -- go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `mise exec go@1.26.0 -- go vet ./...`
|
||||
- `mise exec go@1.26.0 -- go test ./...`
|
||||
@@ -0,0 +1,12 @@
|
||||
# Session 2026-04-13
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.24` (`c4459c43462c45bcd8ac346c72b98647675a014e`)
|
||||
- Base local release line before merge: `ProxyPilot main` at `v6.9.23`
|
||||
- Merge branch: `sync-upstream-v6.9.23-to-v6.9.24`
|
||||
- Merge result:
|
||||
- clean merge with no manual conflict resolution required
|
||||
- kept ProxyPilot fork structure intact while taking upstream auth auto-refresh loop and Antigravity fixes
|
||||
- Validation plan:
|
||||
- `mise exec go@1.26.0 -- go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `mise exec go@1.26.0 -- go vet ./...`
|
||||
- `mise exec go@1.26.0 -- go test ./...`
|
||||
@@ -0,0 +1,14 @@
|
||||
# Session 2026-04-16
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.27` (`ba454dbfbfb41c3a8218118323f828ea19e92c3d`)
|
||||
- Base local release line before merge: `ProxyPilot main` at `v6.9.24` (`06c3e954`)
|
||||
- Merge branch: `sync-upstream-v6.9.24-to-v6.9.27`
|
||||
- Conflict resolution:
|
||||
- kept ProxyPilot branding and docs flow in `README.md`
|
||||
- did not revive upstream locale README files (`README_CN.md`, `README_JA.md`)
|
||||
- preserved ProxyPilot Qwen auth/runtime support while taking upstream session-affinity and Antigravity fixes
|
||||
- kept local request execution metadata behavior in `sdk/api/handlers/handlers.go` so pinned-auth selection remains stable
|
||||
- Validation:
|
||||
- `mise exec go@1.26.0 -- go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `mise exec go@1.26.0 -- go vet ./...`
|
||||
- `mise exec go@1.26.0 -- go test ./...`
|
||||
@@ -0,0 +1,14 @@
|
||||
# Session 2026-04-17
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.28` (`5dcca69ea5be9de76e04c7f5dcd51098e7fc9018`)
|
||||
- Base local release line before merge: `ProxyPilot main` at `v6.9.27` (`71be5e89`)
|
||||
- Merge branch: `sync-upstream-v6.9.27-to-v6.9.28`
|
||||
- Conflict resolution:
|
||||
- kept ProxyPilot branding and docs flow in `README.md`
|
||||
- did not revive upstream locale README files (`README_CN.md`, `README_JA.md`)
|
||||
- preserved ProxyPilot `qwen` and `iflow` auth/runtime/config/model support while taking upstream auth proxy-override support and Claude Opus 4.7 model catalog updates
|
||||
- kept local synthetic idempotency-key behavior in `sdk/api/handlers/handlers.go` and updated the new upstream metadata test to match fork behavior
|
||||
- Validation:
|
||||
- `mise exec go@1.26.0 -- go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `mise exec go@1.26.0 -- go vet ./...`
|
||||
- `mise exec go@1.26.0 -- go test ./...`
|
||||
@@ -0,0 +1,14 @@
|
||||
# Session 2026-04-18
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.29` (`c6baa64b4e3cf85f8400e2cc9d9bd3d7040db187`)
|
||||
- Base local release line before merge: `ProxyPilot main` at `v6.9.28` (`7ec328f9`)
|
||||
- Merge branch: `sync-upstream-v6.9.28-to-v6.9.29`
|
||||
- Conflict resolution:
|
||||
- preserved ProxyPilot's clone-and-rollback config persistence behavior in `internal/api/handlers/management/handler.go` while integrating upstream `auth-index` management handlers
|
||||
- took upstream Claude executor test expectations that tighten forwarded system-prompt behavior and strict-mode assertions
|
||||
- no provider-surface carry work was needed this round; local `qwen`, `iflow`, and synthetic idempotency behavior remained intact
|
||||
- Validation:
|
||||
- `go test ./internal/api/handlers/management ./internal/runtime/executor`
|
||||
- `mise exec go@1.26.0 -- go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `mise exec go@1.26.0 -- go vet ./...`
|
||||
- `mise exec go@1.26.0 -- go test ./...`
|
||||
@@ -0,0 +1,13 @@
|
||||
# Session 2026-04-24
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.36` (`f1ba6151a99240902bcda12102c921b0ead01d2d`)
|
||||
- Base local release line before merge: `ProxyPilot main` at upstream `v6.9.30` (`2ebbb596`)
|
||||
- Merge branch: `sync-upstream-v6.9.30-to-v6.9.36`
|
||||
- Conflict resolution:
|
||||
- no textual conflicts in the upstream merge; existing ProxyPilot fork surfaces carried forward cleanly
|
||||
- corrected stale upstream marker files from the prior `v6.9.30` sync so `.upstream-sync-tag` and `.upstream-sync-sha` now match the merged upstream release
|
||||
- kept the existing fork behavior around local provider support while taking upstream Codex image-generation, Antigravity credits fallback, logging, and `/healthz` improvements
|
||||
- Validation:
|
||||
- `go test ./...`
|
||||
- `go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `go vet ./...`
|
||||
@@ -24,14 +24,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||
clipexec "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/logging"
|
||||
sdktr "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/api"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
clipexec "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/logging"
|
||||
sdktr "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||
clipexec "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
clipexec "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
EXAMPLES := simple model auth frontend-auth executor protocol-format request-translator request-normalizer response-translator response-normalizer thinking usage cli management-api host-callback host-callback-auth-files host-model-callback claude-web-search-router
|
||||
LANGUAGES := go c rust
|
||||
BIN_DIR := $(CURDIR)/bin
|
||||
BUILD_DIR := $(BIN_DIR)/build
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
PLUGIN_EXT := dll
|
||||
RUST_DYLIB_PREFIX :=
|
||||
RUST_DYLIB_EXT := dll
|
||||
else ifeq ($(UNAME_S),Darwin)
|
||||
PLUGIN_EXT := dylib
|
||||
RUST_DYLIB_PREFIX := lib
|
||||
RUST_DYLIB_EXT := dylib
|
||||
else
|
||||
PLUGIN_EXT := so
|
||||
RUST_DYLIB_PREFIX := lib
|
||||
RUST_DYLIB_EXT := so
|
||||
endif
|
||||
|
||||
.PHONY: build list clean
|
||||
|
||||
build: $(foreach example,$(EXAMPLES),$(foreach lang,$(LANGUAGES),$(BIN_DIR)/$(example)-$(lang).$(PLUGIN_EXT)))
|
||||
|
||||
list:
|
||||
@$(foreach example,$(EXAMPLES),$(foreach lang,$(LANGUAGES),echo $(example)/$(lang);))
|
||||
|
||||
clean:
|
||||
rm -rf $(BIN_DIR)
|
||||
|
||||
$(BIN_DIR):
|
||||
mkdir -p $(BIN_DIR)
|
||||
|
||||
$(BUILD_DIR):
|
||||
mkdir -p $(BUILD_DIR)
|
||||
|
||||
$(BIN_DIR)/%-go.$(PLUGIN_EXT): %/go/main.go %/go/go.mod | $(BIN_DIR)
|
||||
cd $*/go && go build -buildmode=c-shared -o $(abspath $@) .
|
||||
rm -f $(BIN_DIR)/$*-go.h
|
||||
|
||||
$(BIN_DIR)/%-c.$(PLUGIN_EXT): %/c/CMakeLists.txt %/c/src/plugin.c | $(BIN_DIR) $(BUILD_DIR)
|
||||
cmake -S $*/c -B $(BUILD_DIR)/$*/c -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$(BIN_DIR)
|
||||
cmake --build $(BUILD_DIR)/$*/c
|
||||
|
||||
$(BIN_DIR)/%-rust.$(PLUGIN_EXT): %/rust/Cargo.toml %/rust/Cargo.lock %/rust/src/lib.rs | $(BIN_DIR) $(BUILD_DIR)
|
||||
cd $*/rust && CARGO_TARGET_DIR=$(abspath $(BUILD_DIR)/$*/rust) cargo build --release --locked
|
||||
cp "$(BUILD_DIR)/$*/rust/release/$(RUST_DYLIB_PREFIX)cliproxy_$(subst -,_,$*)_rust.$(RUST_DYLIB_EXT)" "$@"
|
||||
@@ -0,0 +1,109 @@
|
||||
# Standard Dynamic Library Plugin Examples
|
||||
|
||||
This directory contains standard dynamic library plugin examples for the CLIProxyAPI C ABI.
|
||||
|
||||
## Layout
|
||||
|
||||
- `simple/`- : Go-only plugin resource that calls host auth file callbacks (, , , ).
|
||||
- : full provider-native skeleton that declares every supported capability.
|
||||
- `model/`: model capability only.
|
||||
- `auth/`: auth provider capability only.
|
||||
- `frontend-auth/`: frontend auth provider capability only.
|
||||
- `frontend-auth-exclusive/`: frontend auth provider that becomes the only request authentication provider when selected.
|
||||
- `executor/`: executor capability only.
|
||||
- `protocol-format/`: minimal executor focused on input/output format declarations.
|
||||
- `request-translator/`: request translation capability only.
|
||||
- `request-normalizer/`: request normalization capability only.
|
||||
- `codex-service-tier/`: Go-only request normalizer that sets Codex `gpt-5.5` requests to the priority service tier when enabled.
|
||||
- `scheduler/`: Go-only scheduler that can select a configured auth ID, delegate to a built-in scheduler, or deny picks.
|
||||
- `claude-web-search-router/`: ModelRouter + executor for Claude Code built-in `web_search` (antigravity / codex / xai / Tavily). See `claude-web-search-router/README.md`.
|
||||
- `response-translator/`: response translation capability only.
|
||||
- `response-normalizer/`: response normalization capability only.
|
||||
- `thinking/`: thinking applier capability only.
|
||||
- `usage/`: usage observer capability only.
|
||||
- `cli/`: command-line capability only.
|
||||
- `management-api/`: Management API and resource capability only.
|
||||
- `host-callback/`: minimal plugin resource that demonstrates host callbacks.
|
||||
- `host-callback-auth-files/`: Go-only plugin resource that calls host auth file callbacks.
|
||||
- `host-model-callback/`: Go-only plugin resource that calls the host model execution callbacks.
|
||||
|
||||
Most standard capability examples contain `go/`, `c/`, and `rust/` subdirectories. Specialized examples may provide only the implementation language they need.
|
||||
|
||||
## Codex Service Tier
|
||||
|
||||
`codex-service-tier` declares the request normalization capability. When `fast` is `true`, it sets `service_tier` to `priority` for requests where `req.ToFormat` is `codex` and `req.Model` is `gpt-5.5`.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
codex-service-tier:
|
||||
enabled: true
|
||||
priority: 1
|
||||
fast: false
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Host Auth Files Callback
|
||||
|
||||
`host-callback-auth-files` declares the Management API capability and exposes a browser resource named `Host Auth Files`. The resource demonstrates `host.auth.list`, `host.auth.get` (physical JSON file), `host.auth.get_runtime`, and `host.auth.save`.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
host-callback-auth-files:
|
||||
enabled: true
|
||||
priority: 1
|
||||
```
|
||||
|
||||
See `host-callback-auth-files/README.md` for URL examples.
|
||||
|
||||
## Host Model Callback
|
||||
|
||||
`host-model-callback` declares the Management API capability and exposes a browser resource named `Host Model Callback`. The resource calls `host.model.execute` for non-streaming requests and `host.model.execute_stream` plus `host.model.stream_read` for streaming requests. It demonstrates explicit stream close with `host.model.stream_close` and an `implicit_close=true` option for RPC-scope host cleanup.
|
||||
|
||||
When the resource forwards its `host_callback_id`, CPA identifies the plugin that initiated the host model callback and skips that same plugin's interceptors for the nested execution. This makes host model callbacks non-recursive for the caller while allowing other plugins to intercept the nested request.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
host-model-callback:
|
||||
enabled: true
|
||||
priority: 1
|
||||
```
|
||||
|
||||
The default example model is `gpt-5.5`, but the request succeeds only when the current CPA model and auth configuration can route that model.
|
||||
|
||||
## Scheduler
|
||||
|
||||
`scheduler` declares the scheduler capability. It can select a configured auth ID from the candidate list, delegate to the built-in `fill-first` or `round-robin` scheduler, or reject picks when `deny` is `true`.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
scheduler:
|
||||
enabled: true
|
||||
priority: 1
|
||||
auth_id: ""
|
||||
delegate: ""
|
||||
deny: false
|
||||
```
|
||||
|
||||
`auth_id` selects a matching candidate when `delegate` is empty. `delegate` accepts `""`, `fill-first`, or `round-robin`; other non-empty values leave the pick unhandled. `deny` returns a scheduler error.
|
||||
|
||||
## Build All Examples
|
||||
|
||||
```bash
|
||||
make -C examples/plugin list
|
||||
make -C examples/plugin build
|
||||
```
|
||||
|
||||
Artifacts are written to `examples/plugin/bin`.
|
||||
|
||||
## Notes
|
||||
|
||||
`protocol-format` uses a minimal executor because format declarations belong to executor capabilities.
|
||||
|
||||
`host-callback` uses a minimal plugin resource because host callbacks are invoked from plugin methods and are not standalone capabilities.
|
||||
|
||||
Menu resources returned by `management.register` through the `resources` field are exposed by CPA under `/v0/resource/plugins/<pluginID>/...`. Authenticated plugin Management API routes remain under `/v0/management/...`.
|
||||
@@ -0,0 +1,108 @@
|
||||
- :仅 Go 实现的插件资源,演示 host 凭证文件回调(、、、)。
|
||||
- # 标准动态库插件示例
|
||||
|
||||
本目录包含 CLIProxyAPI C ABI 的标准动态库插件示例。
|
||||
|
||||
## 目录布局
|
||||
|
||||
- `simple/`:声明全部支持能力的完整骨架示例。
|
||||
- `model/`:只演示模型能力。
|
||||
- `auth/`:只演示认证提供方能力。
|
||||
- `frontend-auth/`:只演示前端认证提供方能力。
|
||||
- `frontend-auth-exclusive/`:演示被选中后成为唯一请求认证方式的前端认证提供方。
|
||||
- `executor/`:只演示执行器能力。
|
||||
- `protocol-format/`:使用最小执行器重点演示输入和输出格式声明。
|
||||
- `request-translator/`:只演示请求转换能力。
|
||||
- `request-normalizer/`:只演示请求规整能力。
|
||||
- `codex-service-tier/`:仅 Go 实现的请求规整插件,启用后会将 Codex `gpt-5.5` 请求设置为 priority service tier。
|
||||
- `scheduler/`:仅 Go 实现的调度插件,可选择指定 auth ID、委托内置调度器或拒绝调度。
|
||||
- `response-translator/`:只演示响应转换能力。
|
||||
- `response-normalizer/`:只演示响应规整能力。
|
||||
- `thinking/`:只演示 Thinking 处理能力。
|
||||
- `usage/`:只演示 Usage 观察能力。
|
||||
- `cli/`:只演示命令行扩展能力。
|
||||
- `management-api/`:只演示 Management API 和资源扩展能力。
|
||||
- `host-callback/`:使用最小插件资源演示宿主回调。
|
||||
- `host-callback-auth-files/`:仅 Go 实现的插件资源,演示 host 凭证文件回调。
|
||||
- `host-model-callback/`:仅 Go 实现的插件资源,演示调用宿主模型执行回调。
|
||||
|
||||
多数标准能力示例都包含 `go/`、`c/` 和 `rust/` 三个子目录。专用示例可能只提供所需的实现语言。
|
||||
|
||||
## Codex Service Tier
|
||||
|
||||
`codex-service-tier` 声明请求规整能力。当 `fast` 为 `true` 时,如果 `req.ToFormat` 为 `codex` 且 `req.Model` 为 `gpt-5.5`,它会将 `service_tier` 设置为 `priority`。
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
codex-service-tier:
|
||||
enabled: true
|
||||
priority: 1
|
||||
fast: false
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Host Auth Files 回调
|
||||
|
||||
`host-callback-auth-files` 声明 Management API 能力,并暴露名为 `Host Auth Files` 的浏览器资源,演示 `host.auth.list`、`host.auth.get`(物理 JSON 文件)、`host.auth.get_runtime` 与 `host.auth.save`。
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
host-callback-auth-files:
|
||||
enabled: true
|
||||
priority: 1
|
||||
```
|
||||
|
||||
详见 `host-callback-auth-files/README.md`。
|
||||
|
||||
## Host Model Callback
|
||||
|
||||
`host-model-callback` 声明 Management API 能力,并暴露名为 `Host Model Callback` 的浏览器资源。该资源在非流式请求中调用 `host.model.execute`,在流式请求中调用 `host.model.execute_stream` 和 `host.model.stream_read`。它演示了通过 `host.model.stream_close` 显式关闭流,也提供 `implicit_close=true` 用于演示 RPC 作用域结束时的宿主隐式清理。
|
||||
|
||||
当该资源转发自身收到的 `host_callback_id` 时,CPA 会识别发起宿主模型回调的插件,并在嵌套模型执行中跳过同一个插件的拦截器。因此宿主模型回调不会递归调用发起插件自身,但其他已启用插件仍可拦截这次嵌套请求。
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
host-model-callback:
|
||||
enabled: true
|
||||
priority: 1
|
||||
```
|
||||
|
||||
默认示例模型是 `gpt-5.5`,但请求能否成功取决于当前 CPA 模型和认证配置是否可以路由该模型。
|
||||
|
||||
## Scheduler
|
||||
|
||||
`scheduler` 声明调度能力。它可以从候选列表中选择配置的 auth ID,委托内置的 `fill-first` 或 `round-robin` 调度器,或在 `deny` 为 `true` 时拒绝调度。
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
scheduler:
|
||||
enabled: true
|
||||
priority: 1
|
||||
auth_id: ""
|
||||
delegate: ""
|
||||
deny: false
|
||||
```
|
||||
|
||||
`auth_id` 会在 `delegate` 为空时选择匹配候选。`delegate` 支持 `""`、`fill-first` 和 `round-robin`;其他非空值会让本插件不处理本次调度。`deny` 会返回调度错误。
|
||||
|
||||
## 构建全部示例
|
||||
|
||||
```bash
|
||||
make -C examples/plugin list
|
||||
make -C examples/plugin build
|
||||
```
|
||||
|
||||
构建产物会写入 `examples/plugin/bin`。
|
||||
|
||||
## 说明
|
||||
|
||||
`protocol-format` 使用最小执行器承载,因为格式声明属于执行器能力。
|
||||
|
||||
`host-callback` 使用最小插件资源承载,因为宿主回调只能从插件方法内部发起,不是独立能力。
|
||||
|
||||
`management.register` 通过 `resources` 字段返回的菜单资源会由 CPA 暴露在 `/v0/resource/plugins/<pluginID>/...` 下。需要认证的插件自有 Management API 路由仍保留在 `/v0/management/...` 下。
|
||||
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(cliproxy_auth_c C)
|
||||
|
||||
add_library(cliproxy_auth_c SHARED src/plugin.c)
|
||||
set_target_properties(cliproxy_auth_c PROPERTIES
|
||||
OUTPUT_NAME "auth-c"
|
||||
PREFIX ""
|
||||
)
|
||||
@@ -0,0 +1,129 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define CLIPROXY_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define CLIPROXY_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#define ABI_VERSION 1
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
static const cliproxy_host_api* stored_host = NULL;
|
||||
|
||||
static void write_response(cliproxy_buffer* response, const char* text) {
|
||||
if (response == NULL || text == NULL) {
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(text);
|
||||
void* ptr = malloc(len);
|
||||
if (ptr == NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(ptr, text, len);
|
||||
response->ptr = ptr;
|
||||
response->len = len;
|
||||
}
|
||||
|
||||
static void call_host(const char* method, const char* payload) {
|
||||
if (stored_host == NULL || stored_host->call == NULL || method == NULL) {
|
||||
return;
|
||||
}
|
||||
cliproxy_buffer response = {0};
|
||||
const uint8_t* request = (const uint8_t*)payload;
|
||||
size_t request_len = payload == NULL ? 0 : strlen(payload);
|
||||
if (stored_host->call(stored_host->host_ctx, method, request, request_len, &response) == 0 && response.ptr != NULL && stored_host->free_buffer != NULL) {
|
||||
stored_host->free_buffer(response.ptr, response.len);
|
||||
}
|
||||
}
|
||||
|
||||
static int plugin_call(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (response != NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
}
|
||||
if (method == NULL) {
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"invalid_method\",\"message\":\"method is required\"}}");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(method, "plugin.register") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "plugin.reconfigure") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "auth.identifier") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"identifier\":\"example-auth-c\"}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "auth.parse") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Handled\":true,\"Auth\":{\"Provider\":\"example-auth-c\",\"ID\":\"example-auth-c\",\"FileName\":\"example-auth-c.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWMiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-c\"}}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "auth.login.start") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Provider\":\"example-auth-c\",\"URL\":\"https://example.invalid/login\",\"State\":\"example-state\",\"ExpiresAt\":\"2030-01-01T00:00:00Z\"}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "auth.login.poll") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Status\":\"success\",\"Message\":\"example login complete\",\"Auth\":{\"Provider\":\"example-auth-c\",\"ID\":\"example-auth-c\",\"FileName\":\"example-auth-c.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWMiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-c\"}}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "auth.refresh") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Auth\":{\"Provider\":\"example-auth-c\",\"ID\":\"example-auth-c\",\"FileName\":\"example-auth-c.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWMiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-c\"}},\"NextRefreshAfter\":\"2030-01-01T00:00:00Z\"}}");
|
||||
return 0;
|
||||
}
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}");
|
||||
(void)request;
|
||||
(void)request_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void plugin_free(void* ptr, size_t len) {
|
||||
(void)len;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static void plugin_shutdown(void) {}
|
||||
|
||||
CLIPROXY_EXPORT int cliproxy_plugin_init(const cliproxy_host_api* host, cliproxy_plugin_api* plugin) {
|
||||
if (plugin == NULL) {
|
||||
return 1;
|
||||
}
|
||||
stored_host = host;
|
||||
plugin->abi_version = ABI_VERSION;
|
||||
plugin->call = plugin_call;
|
||||
plugin->free_buffer = plugin_free;
|
||||
plugin->shutdown = plugin_shutdown;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/auth/go
|
||||
|
||||
go 1.26
|
||||
@@ -0,0 +1,181 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
|
||||
static const cliproxy_host_api* stored_host;
|
||||
|
||||
static void store_host_api(const cliproxy_host_api* host) {
|
||||
stored_host = host;
|
||||
}
|
||||
|
||||
static int call_host_api(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (stored_host == NULL || stored_host->call == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return stored_host->call(stored_host->host_ctx, method, request, request_len, response);
|
||||
}
|
||||
|
||||
static void free_host_buffer(void* ptr, size_t len) {
|
||||
if (stored_host != NULL && stored_host->free_buffer != NULL && ptr != NULL) {
|
||||
stored_host->free_buffer(ptr, len);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const abiVersion uint32 = 1
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(host *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
C.store_host_api(host)
|
||||
plugin.abi_version = C.uint32_t(abiVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method))
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
_ = request
|
||||
_ = requestLen
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, len C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
_ = len
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string) ([]byte, error) {
|
||||
_ = http.StatusOK
|
||||
_ = time.Second
|
||||
switch method {
|
||||
case "plugin.register":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}")
|
||||
case "plugin.reconfigure":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}")
|
||||
case "auth.identifier":
|
||||
return okEnvelopeJSON("{\"identifier\":\"example-auth-go\"}")
|
||||
case "auth.parse":
|
||||
return okEnvelopeJSON("{\"Handled\":true,\"Auth\":{\"Provider\":\"example-auth-go\",\"ID\":\"example-auth-go\",\"FileName\":\"example-auth-go.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWdvIiwidG9rZW4iOiJleGFtcGxlLXRva2VuIn0=\",\"Metadata\":{\"type\":\"example-auth-go\"}}}")
|
||||
case "auth.login.start":
|
||||
return okEnvelopeJSON("{\"Provider\":\"example-auth-go\",\"URL\":\"https://example.invalid/login\",\"State\":\"example-state\",\"ExpiresAt\":\"2030-01-01T00:00:00Z\"}")
|
||||
case "auth.login.poll":
|
||||
return okEnvelopeJSON("{\"Status\":\"success\",\"Message\":\"example login complete\",\"Auth\":{\"Provider\":\"example-auth-go\",\"ID\":\"example-auth-go\",\"FileName\":\"example-auth-go.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWdvIiwidG9rZW4iOiJleGFtcGxlLXRva2VuIn0=\",\"Metadata\":{\"type\":\"example-auth-go\"}}}")
|
||||
case "auth.refresh":
|
||||
return okEnvelopeJSON("{\"Auth\":{\"Provider\":\"example-auth-go\",\"ID\":\"example-auth-go\",\"FileName\":\"example-auth-go.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLWdvIiwidG9rZW4iOiJleGFtcGxlLXRva2VuIn0=\",\"Metadata\":{\"type\":\"example-auth-go\"}},\"NextRefreshAfter\":\"2030-01-01T00:00:00Z\"}")
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func okEnvelopeJSON(result string) ([]byte, error) {
|
||||
return json.Marshal(envelope{OK: true, Result: json.RawMessage(result)})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
|
||||
func callHost(method string, payload []byte) {
|
||||
cMethod := C.CString(method)
|
||||
defer C.free(unsafe.Pointer(cMethod))
|
||||
var response C.cliproxy_buffer
|
||||
var req *C.uint8_t
|
||||
if len(payload) > 0 {
|
||||
req = (*C.uint8_t)(C.CBytes(payload))
|
||||
defer C.free(unsafe.Pointer(req))
|
||||
}
|
||||
if C.call_host_api(cMethod, req, C.size_t(len(payload)), &response) == 0 && response.ptr != nil {
|
||||
C.free_host_buffer(response.ptr, response.len)
|
||||
}
|
||||
}
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "cliproxy-auth-rust"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "cliproxy-auth-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
@@ -0,0 +1,127 @@
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use std::ptr;
|
||||
|
||||
const ABI_VERSION: u32 = 1;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyBuffer {
|
||||
ptr: *mut u8,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
type HostCall = unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type HostFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginCall = unsafe extern "C" fn(*const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type PluginFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginShutdown = unsafe extern "C" fn();
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyHostApi {
|
||||
abi_version: u32,
|
||||
host_ctx: *mut std::ffi::c_void,
|
||||
call: Option<HostCall>,
|
||||
free_buffer: Option<HostFree>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyPluginApi {
|
||||
abi_version: u32,
|
||||
call: Option<PluginCall>,
|
||||
free_buffer: Option<PluginFree>,
|
||||
shutdown: Option<PluginShutdown>,
|
||||
}
|
||||
|
||||
static mut STORED_HOST: *const CliproxyHostApi = ptr::null();
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cliproxy_plugin_init(host: *const CliproxyHostApi, plugin: *mut CliproxyPluginApi) -> i32 {
|
||||
if plugin.is_null() {
|
||||
return 1;
|
||||
}
|
||||
unsafe {
|
||||
STORED_HOST = host;
|
||||
(*plugin).abi_version = ABI_VERSION;
|
||||
(*plugin).call = Some(plugin_call);
|
||||
(*plugin).free_buffer = Some(plugin_free);
|
||||
(*plugin).shutdown = Some(plugin_shutdown);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_call(method: *const c_char, request: *const u8, request_len: usize, response: *mut CliproxyBuffer) -> i32 {
|
||||
if !response.is_null() {
|
||||
(*response).ptr = ptr::null_mut();
|
||||
(*response).len = 0;
|
||||
}
|
||||
if method.is_null() {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#);
|
||||
return 1;
|
||||
}
|
||||
let method = match CStr::from_ptr(method).to_str() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is not utf-8"}}"#);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let _ = request;
|
||||
let _ = request_len;
|
||||
match method {
|
||||
"plugin.register" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}}"); 0 },"plugin.reconfigure" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-auth-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-auth-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"auth_provider\":true}}}"); 0 },"auth.identifier" => { write_response(response, "{\"ok\":true,\"result\":{\"identifier\":\"example-auth-rust\"}}"); 0 },"auth.parse" => { write_response(response, "{\"ok\":true,\"result\":{\"Handled\":true,\"Auth\":{\"Provider\":\"example-auth-rust\",\"ID\":\"example-auth-rust\",\"FileName\":\"example-auth-rust.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLXJ1c3QiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-rust\"}}}}"); 0 },"auth.login.start" => { write_response(response, "{\"ok\":true,\"result\":{\"Provider\":\"example-auth-rust\",\"URL\":\"https://example.invalid/login\",\"State\":\"example-state\",\"ExpiresAt\":\"2030-01-01T00:00:00Z\"}}"); 0 },"auth.login.poll" => { write_response(response, "{\"ok\":true,\"result\":{\"Status\":\"success\",\"Message\":\"example login complete\",\"Auth\":{\"Provider\":\"example-auth-rust\",\"ID\":\"example-auth-rust\",\"FileName\":\"example-auth-rust.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLXJ1c3QiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-rust\"}}}}"); 0 },"auth.refresh" => { write_response(response, "{\"ok\":true,\"result\":{\"Auth\":{\"Provider\":\"example-auth-rust\",\"ID\":\"example-auth-rust\",\"FileName\":\"example-auth-rust.json\",\"Label\":\"Auth Example\",\"StorageJSON\":\"eyJ0eXBlIjoiZXhhbXBsZS1hdXRoLXJ1c3QiLCJ0b2tlbiI6ImV4YW1wbGUtdG9rZW4ifQ==\",\"Metadata\":{\"type\":\"example-auth-rust\"}},\"NextRefreshAfter\":\"2030-01-01T00:00:00Z\"}}"); 0 },
|
||||
_ => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_free(ptr: *mut std::ffi::c_void, len: usize) {
|
||||
if !ptr.is_null() {
|
||||
let _ = Vec::from_raw_parts(ptr as *mut u8, len, len);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_shutdown() {}
|
||||
|
||||
fn write_response(response: *mut CliproxyBuffer, text: &str) {
|
||||
if response.is_null() {
|
||||
return;
|
||||
}
|
||||
let mut bytes = text.as_bytes().to_vec();
|
||||
let len = bytes.len();
|
||||
let ptr = bytes.as_mut_ptr();
|
||||
std::mem::forget(bytes);
|
||||
unsafe {
|
||||
(*response).ptr = ptr;
|
||||
(*response).len = len;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn call_host(method: &str, payload: &str) {
|
||||
unsafe {
|
||||
if STORED_HOST.is_null() {
|
||||
return;
|
||||
}
|
||||
let host = &*STORED_HOST;
|
||||
let Some(call) = host.call else {
|
||||
return;
|
||||
};
|
||||
let mut method_bytes = method.as_bytes().to_vec();
|
||||
method_bytes.push(0);
|
||||
let mut response = CliproxyBuffer { ptr: ptr::null_mut(), len: 0 };
|
||||
let rc = call(
|
||||
host.host_ctx,
|
||||
method_bytes.as_ptr() as *const c_char,
|
||||
payload.as_ptr(),
|
||||
payload.len(),
|
||||
&mut response,
|
||||
);
|
||||
if rc == 0 && !response.ptr.is_null() {
|
||||
if let Some(free_buffer) = host.free_buffer {
|
||||
free_buffer(response.ptr as *mut std::ffi::c_void, response.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
# Claude Code Web Search Router (ModelRouter example)
|
||||
|
||||
This plugin demonstrates **ModelRouter** on Claude Code built-in `web_search` requests (see `temp/1.json` in the repo root for a captured request/response).
|
||||
|
||||
## What it detects
|
||||
|
||||
- Inbound protocol `claude` / `anthropic`
|
||||
- `tools[]` with `type` `web_search_20250305` or `web_search_20260209`
|
||||
- Optional Claude Code heuristics: system text like “web search tool use”, or user text
|
||||
`Perform a web search for the query: …`
|
||||
|
||||
## Routes (`route` config)
|
||||
|
||||
| Value | Behavior |
|
||||
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `fallback` (**default**) | Plugin **executor** runs **antigravity → codex → xai → tavily** (built-ins via `host.model.*`, Tavily in-plugin). On **429/503/502**, tries the next backend in the same request. Backends that fail often are **deprioritized on later requests** (in-memory penalty; no extra config). |
|
||||
| `antigravity_google` / `codex_web_search` / `xai_web_search` / `tavily` | Same orchestration for that backend’s chain member(s): execution retry + penalty apply when multiple backends are eligible. |
|
||||
| `default_provider` | `default_provider` + optional `default_provider_model` via built-in AuthManager (not orchestrated). |
|
||||
Routing for `fallback` requires at least one runnable backend (providers in `AvailableProviders` where needed, resolvable antigravity model, or `tavily_api_keys`).
|
||||
|
||||
### xAI web search notes (aligned with upstream docs)
|
||||
|
||||
- **Model**: xAI documents `grok-4.3` for server-side `web_search`. This example sets `TargetModel` to **`grok-4.3`** when `xai_model` is empty (do not forward `claude-sonnet-4-6` to xAI).
|
||||
- **Request shape**: Responses API `input` + `tools[]` with `"type": "web_search"`. Optional `filters.allowed_domains` / `filters.excluded_domains` (max 5 each, mutually exclusive).
|
||||
- **Claude mapping today**: `internal/translator/codex/claude` copies Claude `allowed_domains` → `filters.allowed_domains`. Claude `blocked_domains` is **not** mapped to `excluded_domains` yet.
|
||||
- **Executor**: `xai_executor` normalizes tools (drops unsupported `external_web_access` if present) and posts to `/responses`.
|
||||
- **Response**: Citations / server tool metadata come back through OpenAI Responses SSE and are converted toward Claude `server_tool_use` / `web_search_tool_result` where the response translator supports it.
|
||||
|
||||
## Configuration
|
||||
|
||||
Plugin config lives under `plugins.configs.claude-web-search-router` (key must match the plugin name). Load the shared library via `plugins.path`.
|
||||
|
||||
### Recommended: fallback chain (default)
|
||||
|
||||
Tries **antigravity → codex → xai → tavily**; configure `tavily_api_keys` so the last step can succeed when built-in providers are missing or unavailable.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
path:
|
||||
- /absolute/path/to/examples/plugin/bin/claude-web-search-router-go.dylib
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: fallback
|
||||
antigravity_model: "" # empty: registry lookup, then first supports_web_search
|
||||
codex_model: "gpt-5.4-mini"
|
||||
xai_model: "grok-4.3"
|
||||
tavily_api_keys:
|
||||
- "tvly-xxxxxxxx"
|
||||
# - "tvly-yyyyyyyy" # optional: round-robin
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
Omit `route` to use the same default (`fallback`).
|
||||
|
||||
### Minimal fallback (Tavily as last resort only)
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: fallback
|
||||
tavily_api_keys:
|
||||
- "tvly-xxxxxxxx"
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
### Single backend (no fallback)
|
||||
|
||||
**Antigravity only:**
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: antigravity_google
|
||||
antigravity_model: "gemini-3.1-flash-lite"
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
**Codex only:**
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: codex_web_search
|
||||
codex_model: "gpt-5.4-mini"
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
**xAI only:**
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: xai_web_search
|
||||
xai_model: "grok-4.3"
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
**Tavily only (plugin executor):**
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: tavily
|
||||
tavily_api_keys:
|
||||
- "tvly-xxxxxxxx"
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
**Built-in provider via `default_provider`:**
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
priority: 20
|
||||
route: default_provider
|
||||
default_provider: claude
|
||||
default_provider_model: ""
|
||||
require_web_search_only: true
|
||||
```
|
||||
|
||||
### Disable or relax detection
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
claude-web-search-router:
|
||||
enabled: false # plugin declines; host may use default Claude path
|
||||
|
||||
# Or keep enabled but allow mixed tool lists:
|
||||
claude-web-search-router:
|
||||
enabled: true
|
||||
route: fallback
|
||||
require_web_search_only: false
|
||||
```
|
||||
|
||||
### Config field reference
|
||||
|
||||
| Field | Description |
|
||||
| ----- | ----------- |
|
||||
| `enabled` | `false` → `Handled: false` for all web_search matches |
|
||||
| `priority` | Host plugin order for ModelRouter (higher runs earlier; see main repo plugins docs) |
|
||||
| `route` | `fallback` (default), `antigravity_google`, `codex_web_search`, `xai_web_search`, `tavily`, `default_provider` |
|
||||
| `antigravity_model` | Antigravity execution model; never the client Claude model name |
|
||||
| `codex_model` | Codex model; empty → `gpt-5.4-mini` |
|
||||
| `xai_model` | xAI model; empty → `grok-4.3` |
|
||||
| `default_provider` / `default_provider_model` | Used when `route=default_provider` |
|
||||
| `tavily_api_keys` | Required for `route=tavily` or fallback last step |
|
||||
| `require_web_search_only` | `true` matches Claude Code–style exclusive `web_search` tools |
|
||||
|
||||
## Build
|
||||
|
||||
```bash
|
||||
make -C examples/plugin bin/claude-web-search-router-go.dylib
|
||||
```
|
||||
|
||||
Use `.so` on Linux and `.dll` on Windows. Point `plugins.path` at the built artifact.
|
||||
@@ -0,0 +1,173 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type claudeStreamBuilder struct {
|
||||
model string
|
||||
messageID string
|
||||
toolUseID string
|
||||
index int
|
||||
inputTokens int
|
||||
}
|
||||
|
||||
func newClaudeStreamBuilder(model string) *claudeStreamBuilder {
|
||||
model = strings.TrimSpace(model)
|
||||
if model == "" {
|
||||
model = "claude-sonnet-4-6"
|
||||
}
|
||||
now := time.Now().UnixNano()
|
||||
return &claudeStreamBuilder{
|
||||
model: model,
|
||||
messageID: fmt.Sprintf("msg_%x", now),
|
||||
toolUseID: fmt.Sprintf("srvtoolu_%d", now),
|
||||
inputTokens: 85,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *claudeStreamBuilder) buildStreamWithQuery(query string, hits []claudeWebSearchHit, answer string) []byte {
|
||||
var chunks []string
|
||||
chunks = append(chunks, b.event("message_start", map[string]any{
|
||||
"type": "message_start",
|
||||
"message": map[string]any{
|
||||
"id": b.messageID, "type": "message", "role": "assistant", "content": []any{},
|
||||
"model": b.model, "stop_reason": nil, "stop_sequence": nil,
|
||||
"usage": map[string]any{"input_tokens": b.inputTokens, "output_tokens": 0},
|
||||
},
|
||||
}))
|
||||
chunks = append(chunks, b.blockStart(b.index, map[string]any{
|
||||
"type": "server_tool_use", "id": b.toolUseID, "name": "web_search", "input": map[string]any{},
|
||||
}))
|
||||
partial, _ := json.Marshal(map[string]string{"query": query})
|
||||
chunks = append(chunks, b.event("content_block_delta", map[string]any{
|
||||
"type": "content_block_delta", "index": b.index,
|
||||
"delta": map[string]any{"type": "input_json_delta", "partial_json": string(partial)},
|
||||
}))
|
||||
chunks = append(chunks, b.event("content_block_stop", map[string]any{"type": "content_block_stop", "index": b.index}))
|
||||
b.index++
|
||||
|
||||
resultContent := webSearchResultBlocks(hits)
|
||||
chunks = append(chunks, b.blockStart(b.index, map[string]any{
|
||||
"type": "web_search_tool_result", "tool_use_id": b.toolUseID, "content": resultContent,
|
||||
}))
|
||||
chunks = append(chunks, b.event("content_block_stop", map[string]any{"type": "content_block_stop", "index": b.index}))
|
||||
b.index++
|
||||
|
||||
text := composeAnswerText(answer, hits)
|
||||
outputTokens := estimateTokens(text)
|
||||
chunks = append(chunks, b.blockStart(b.index, map[string]any{"type": "text", "text": ""}))
|
||||
chunks = append(chunks, b.event("content_block_delta", map[string]any{
|
||||
"type": "content_block_delta", "index": b.index,
|
||||
"delta": map[string]any{"type": "text_delta", "text": text},
|
||||
}))
|
||||
chunks = append(chunks, b.event("content_block_stop", map[string]any{"type": "content_block_stop", "index": b.index}))
|
||||
|
||||
chunks = append(chunks, b.event("message_delta", map[string]any{
|
||||
"type": "message_delta",
|
||||
"delta": map[string]any{"stop_reason": "end_turn", "stop_sequence": nil},
|
||||
"usage": map[string]any{
|
||||
"input_tokens": b.inputTokens, "output_tokens": outputTokens,
|
||||
"server_tool_use": map[string]any{"web_search_requests": 1},
|
||||
},
|
||||
}))
|
||||
chunks = append(chunks, b.event("message_stop", map[string]any{"type": "message_stop"}))
|
||||
return []byte(strings.Join(chunks, ""))
|
||||
}
|
||||
|
||||
func (b *claudeStreamBuilder) buildMessageJSON(query string, hits []claudeWebSearchHit, answer string) []byte {
|
||||
text := composeAnswerText(answer, hits)
|
||||
content := []map[string]any{
|
||||
{"type": "server_tool_use", "id": b.toolUseID, "name": "web_search", "input": map[string]string{"query": query}},
|
||||
{"type": "web_search_tool_result", "tool_use_id": b.toolUseID, "content": webSearchResultBlocks(hits)},
|
||||
{"type": "text", "text": text},
|
||||
}
|
||||
out := map[string]any{
|
||||
"id": b.messageID, "type": "message", "role": "assistant", "model": b.model,
|
||||
"content": content, "stop_reason": "end_turn", "stop_sequence": nil,
|
||||
"usage": map[string]any{
|
||||
"input_tokens": b.inputTokens, "output_tokens": estimateTokens(text),
|
||||
"server_tool_use": map[string]any{"web_search_requests": 1},
|
||||
},
|
||||
}
|
||||
raw, _ := json.Marshal(out)
|
||||
return raw
|
||||
}
|
||||
|
||||
func webSearchResultBlocks(hits []claudeWebSearchHit) []map[string]any {
|
||||
resultContent := make([]map[string]any, 0, len(hits))
|
||||
for _, hit := range hits {
|
||||
title := hit.Title
|
||||
if title == "" {
|
||||
title = hostFromURL(hit.URL)
|
||||
}
|
||||
resultContent = append(resultContent, map[string]any{
|
||||
"type": "web_search_result", "title": title, "url": hit.URL, "page_age": nil,
|
||||
})
|
||||
}
|
||||
return resultContent
|
||||
}
|
||||
|
||||
func (b *claudeStreamBuilder) event(eventType string, data map[string]any) string {
|
||||
raw, _ := json.Marshal(data)
|
||||
return fmt.Sprintf("event: %s\ndata: %s\n\n", eventType, string(raw))
|
||||
}
|
||||
|
||||
func (b *claudeStreamBuilder) blockStart(index int, block map[string]any) string {
|
||||
return b.event("content_block_start", map[string]any{
|
||||
"type": "content_block_start", "index": index, "content_block": block,
|
||||
})
|
||||
}
|
||||
|
||||
func composeAnswerText(answer string, hits []claudeWebSearchHit) string {
|
||||
if strings.TrimSpace(answer) != "" {
|
||||
return answer
|
||||
}
|
||||
if len(hits) == 0 {
|
||||
return "No web search results were returned."
|
||||
}
|
||||
var buf strings.Builder
|
||||
for i, hit := range hits {
|
||||
if i > 0 {
|
||||
buf.WriteString("\n\n")
|
||||
}
|
||||
if hit.Title != "" {
|
||||
buf.WriteString(hit.Title)
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
if hit.URL != "" {
|
||||
buf.WriteString(hit.URL)
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
if hit.Snippet != "" {
|
||||
buf.WriteString(hit.Snippet)
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func hostFromURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
withoutScheme := raw
|
||||
if idx := strings.Index(raw, "://"); idx >= 0 {
|
||||
withoutScheme = raw[idx+3:]
|
||||
}
|
||||
if slash := strings.Index(withoutScheme, "/"); slash >= 0 {
|
||||
return withoutScheme[:slash]
|
||||
}
|
||||
return withoutScheme
|
||||
}
|
||||
|
||||
func estimateTokens(text string) int {
|
||||
n := len([]rune(text)) / 4
|
||||
if n < 1 {
|
||||
return 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestConfigurePreservesDefaultBooleansWhenConfigIsPartial(t *testing.T) {
|
||||
raw := mustJSON(t, lifecycleRequest{ConfigYAML: []byte("route: codex_web_search\n")})
|
||||
|
||||
if errConfigure := configure(raw); errConfigure != nil {
|
||||
t.Fatalf("configure() error = %v", errConfigure)
|
||||
}
|
||||
|
||||
cfg := loadedConfig()
|
||||
if !cfg.Enabled {
|
||||
t.Fatal("Enabled = false, want default true")
|
||||
}
|
||||
if !cfg.RequireWebSearchOnly {
|
||||
t.Fatal("RequireWebSearchOnly = false, want default true")
|
||||
}
|
||||
if cfg.Route != string(backendCodexWebSearch) {
|
||||
t.Fatalf("Route = %q, want codex_web_search", cfg.Route)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
const (
|
||||
claudeWebSearchToolTypeA = "web_search_20250305"
|
||||
claudeWebSearchToolTypeB = "web_search_20260209"
|
||||
)
|
||||
|
||||
// isClaudeSourceFormat reports whether the inbound protocol is Claude / Anthropic Messages.
|
||||
func isClaudeSourceFormat(source string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||
case "claude", "anthropic":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isClaudeTypedWebSearchToolType(toolType string) bool {
|
||||
return toolType == claudeWebSearchToolTypeA || toolType == claudeWebSearchToolTypeB
|
||||
}
|
||||
|
||||
func hasClaudeTypedWebSearchTool(body []byte) bool {
|
||||
tools := gjson.GetBytes(body, "tools")
|
||||
if !tools.IsArray() {
|
||||
return false
|
||||
}
|
||||
for _, tool := range tools.Array() {
|
||||
if isClaudeTypedWebSearchToolType(tool.Get("type").String()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasOnlyClaudeTypedWebSearchTools(body []byte) bool {
|
||||
tools := gjson.GetBytes(body, "tools")
|
||||
if !tools.IsArray() {
|
||||
return false
|
||||
}
|
||||
hasWebSearch := false
|
||||
for _, tool := range tools.Array() {
|
||||
if isClaudeTypedWebSearchToolType(tool.Get("type").String()) {
|
||||
hasWebSearch = true
|
||||
continue
|
||||
}
|
||||
if tool.Get("type").String() != "" || tool.Get("name").String() != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return hasWebSearch
|
||||
}
|
||||
|
||||
func looksLikeClaudeCodeWebSearchAssistant(body []byte) bool {
|
||||
system := gjson.GetBytes(body, "system")
|
||||
if system.IsArray() {
|
||||
for _, block := range system.Array() {
|
||||
text := strings.ToLower(block.Get("text").String())
|
||||
if strings.Contains(text, "web search tool use") ||
|
||||
strings.Contains(text, "performing a web search") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if system.Type == gjson.String {
|
||||
text := strings.ToLower(system.String())
|
||||
if strings.Contains(text, "web search tool use") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
messages := gjson.GetBytes(body, "messages")
|
||||
if !messages.IsArray() {
|
||||
return false
|
||||
}
|
||||
for _, message := range messages.Array() {
|
||||
if message.Get("role").String() != "user" {
|
||||
continue
|
||||
}
|
||||
text := strings.ToLower(extractClaudeMessageText(message.Get("content")))
|
||||
if strings.HasPrefix(text, "perform a web search for the query:") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isClaudeCodeBuiltinWebSearchRequest(body []byte, requireWebSearchOnly bool) bool {
|
||||
if !hasClaudeTypedWebSearchTool(body) {
|
||||
return false
|
||||
}
|
||||
if requireWebSearchOnly && !hasOnlyClaudeTypedWebSearchTools(body) {
|
||||
return false
|
||||
}
|
||||
return looksLikeClaudeCodeWebSearchAssistant(body) || hasOnlyClaudeTypedWebSearchTools(body)
|
||||
}
|
||||
|
||||
func extractClaudeWebSearchQuery(body []byte) string {
|
||||
if q := extractQueryFromPerformPrefix(body); q != "" {
|
||||
return q
|
||||
}
|
||||
return extractQueryFromUserMessages(body)
|
||||
}
|
||||
|
||||
func extractQueryFromPerformPrefix(body []byte) string {
|
||||
messages := gjson.GetBytes(body, "messages")
|
||||
if !messages.IsArray() {
|
||||
return ""
|
||||
}
|
||||
const prefix = "perform a web search for the query:"
|
||||
for _, message := range messages.Array() {
|
||||
if message.Get("role").String() != "user" {
|
||||
continue
|
||||
}
|
||||
text := strings.TrimSpace(extractClaudeMessageText(message.Get("content")))
|
||||
lower := strings.ToLower(text)
|
||||
if strings.HasPrefix(lower, prefix) {
|
||||
return strings.TrimSpace(text[len(prefix):])
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractQueryFromUserMessages(body []byte) string {
|
||||
messages := gjson.GetBytes(body, "messages")
|
||||
if !messages.IsArray() {
|
||||
return ""
|
||||
}
|
||||
arr := messages.Array()
|
||||
for i := len(arr) - 1; i >= 0; i-- {
|
||||
message := arr[i]
|
||||
role := message.Get("role").String()
|
||||
if role != "" && role != "user" {
|
||||
continue
|
||||
}
|
||||
if query := strings.TrimSpace(extractClaudeMessageText(message.Get("content"))); query != "" {
|
||||
return query
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractClaudeMessageText(content gjson.Result) string {
|
||||
if content.Type == gjson.String {
|
||||
return content.String()
|
||||
}
|
||||
if !content.IsArray() {
|
||||
return ""
|
||||
}
|
||||
var parts []string
|
||||
for _, block := range content.Array() {
|
||||
if block.Get("type").String() != "text" {
|
||||
continue
|
||||
}
|
||||
if text := strings.TrimSpace(block.Get("text").String()); text != "" {
|
||||
parts = append(parts, text)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
}
|
||||
|
||||
func extractClaudeWebSearchMaxUses(body []byte, defaultMax int) int {
|
||||
if defaultMax <= 0 {
|
||||
defaultMax = 5
|
||||
}
|
||||
tools := gjson.GetBytes(body, "tools")
|
||||
if !tools.IsArray() {
|
||||
return defaultMax
|
||||
}
|
||||
for _, tool := range tools.Array() {
|
||||
if !isClaudeTypedWebSearchToolType(tool.Get("type").String()) {
|
||||
continue
|
||||
}
|
||||
if maxUses := int(tool.Get("max_uses").Int()); maxUses > 0 {
|
||||
return maxUses
|
||||
}
|
||||
}
|
||||
return defaultMax
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDetectClaudeCodeWebSearchFromFixture(t *testing.T) {
|
||||
root := filepath.Join("..", "..", "..", "..", "temp", "1.json")
|
||||
raw, errRead := os.ReadFile(root)
|
||||
if errRead != nil {
|
||||
t.Skipf("fixture not found: %v", errRead)
|
||||
}
|
||||
// Fixture is HTTP capture; extract JSON request body between first blank line after headers.
|
||||
body := extractHTTPJSONBody(raw)
|
||||
if len(body) == 0 {
|
||||
t.Fatal("empty JSON body in fixture")
|
||||
}
|
||||
if !hasClaudeTypedWebSearchTool(body) {
|
||||
t.Fatal("fixture should declare web_search_20250305")
|
||||
}
|
||||
if !looksLikeClaudeCodeWebSearchAssistant(body) {
|
||||
t.Fatal("fixture should match Claude Code web search assistant heuristics")
|
||||
}
|
||||
if !isClaudeCodeBuiltinWebSearchRequest(body, true) {
|
||||
t.Fatal("expected match with require_web_search_only=true")
|
||||
}
|
||||
query := extractClaudeWebSearchQuery(body)
|
||||
if query == "" {
|
||||
t.Fatal("expected non-empty search query")
|
||||
}
|
||||
if want := "北京天气 2026年6月16日"; query != want {
|
||||
t.Fatalf("query = %q, want %q", query, want)
|
||||
}
|
||||
}
|
||||
|
||||
func extractHTTPJSONBody(raw []byte) []byte {
|
||||
text := string(raw)
|
||||
idx := 0
|
||||
for {
|
||||
next := findDoubleNewline(text, idx)
|
||||
if next < 0 {
|
||||
return nil
|
||||
}
|
||||
rest := trimLeft(text[next:])
|
||||
if len(rest) > 0 && rest[0] == '{' {
|
||||
return []byte(rest)
|
||||
}
|
||||
idx = next + 1
|
||||
}
|
||||
}
|
||||
|
||||
func findDoubleNewline(s string, from int) int {
|
||||
for i := from; i+1 < len(s); i++ {
|
||||
if s[i] == '\n' && s[i+1] == '\n' {
|
||||
return i + 2
|
||||
}
|
||||
if s[i] == '\r' && i+3 < len(s) && s[i+1] == '\n' && s[i+2] == '\r' && s[i+3] == '\n' {
|
||||
return i + 4
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func trimLeft(s string) string {
|
||||
for len(s) > 0 && (s[0] == '\r' || s[0] == '\n' || s[0] == ' ') {
|
||||
s = s[1:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
type streamOrchestrationRunner func(context.Context, pluginapi.ExecutorRequest, string, string) error
|
||||
|
||||
type pluginStreamCloser func(string, string)
|
||||
|
||||
func executeStream(raw []byte) ([]byte, error) {
|
||||
var req rpcExecutorRequest
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return nil, errUnmarshal
|
||||
}
|
||||
return startExecutorStream(req, runWebSearchStreamOrchestration, closePluginStream)
|
||||
}
|
||||
|
||||
func startExecutorStream(req rpcExecutorRequest, runner streamOrchestrationRunner, closeStream pluginStreamCloser) ([]byte, error) {
|
||||
streamID := strings.TrimSpace(req.StreamID)
|
||||
if streamID == "" {
|
||||
return errorEnvelope("executor_error", "stream_id is required for executor.execute_stream"), nil
|
||||
}
|
||||
if runner == nil {
|
||||
return errorEnvelope("executor_error", "stream orchestration runner is unavailable"), nil
|
||||
}
|
||||
if closeStream == nil {
|
||||
closeStream = func(string, string) {}
|
||||
}
|
||||
go func() {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
closeStream(streamID, fmt.Sprintf("stream orchestration panic: %v", recovered))
|
||||
}
|
||||
}()
|
||||
errRun := runner(context.Background(), req.ExecutorRequest, req.HostCallbackID, streamID)
|
||||
if errRun != nil {
|
||||
closeStream(streamID, errRun.Error())
|
||||
return
|
||||
}
|
||||
closeStream(streamID, "")
|
||||
}()
|
||||
return okEnvelope(map[string]any{
|
||||
"headers": http.Header{"Content-Type": []string{"text/event-stream"}},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
type executionPlan struct {
|
||||
backend routeBackend
|
||||
model string
|
||||
}
|
||||
|
||||
func buildExecutionPlans(cfg pluginConfig, req pluginapi.ModelRouteRequest) []executionPlan {
|
||||
return buildExecutionPlansInternal(cfg, req, true)
|
||||
}
|
||||
|
||||
func buildExecutionPlansForExecute(cfg pluginConfig, req pluginapi.ModelRouteRequest) []executionPlan {
|
||||
route := strings.TrimSpace(cfg.Route)
|
||||
if isFallbackRoute(route) {
|
||||
return buildExecutionPlansInternal(cfg, req, false)
|
||||
}
|
||||
return executionPlansForExecuteRoute(cfg, req, route)
|
||||
}
|
||||
|
||||
// executionPlansForExecuteRoute builds plans for plugin executor without requiring
|
||||
// ModelRouteRequest.AvailableProviders (host does not pass it on executor.execute_stream).
|
||||
func executionPlansForExecuteRoute(cfg pluginConfig, req pluginapi.ModelRouteRequest, route string) []executionPlan {
|
||||
backend := routeBackend(strings.TrimSpace(route))
|
||||
if !backendRunnableLenient(backend, cfg, req) {
|
||||
return nil
|
||||
}
|
||||
var plans []executionPlan
|
||||
switch backend {
|
||||
case backendAntigravityGoogle:
|
||||
model := resolveAntigravityWebSearchTargetModel(cfg.AntigravityModel, req.RequestedModel)
|
||||
if model == "" {
|
||||
return nil
|
||||
}
|
||||
plans = append(plans, executionPlan{backend: backend, model: model})
|
||||
case backendCodexWebSearch:
|
||||
plans = append(plans, executionPlan{backend: backend, model: resolveCodexWebSearchTargetModel(cfg.CodexModel)})
|
||||
case backendXAIWebSearch:
|
||||
plans = append(plans, executionPlan{backend: backend, model: resolveXAIWebSearchTargetModel(cfg.XAIModel)})
|
||||
case backendTavily:
|
||||
if !newTavilyClient(cfg.TavilyAPIKeys).available() {
|
||||
return nil
|
||||
}
|
||||
plans = append(plans, executionPlan{backend: backend})
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return plans
|
||||
}
|
||||
|
||||
func buildExecutionPlansInternal(cfg pluginConfig, req pluginapi.ModelRouteRequest, requireProviders bool) []executionPlan {
|
||||
var plans []executionPlan
|
||||
for _, backend := range defaultWebSearchFallbackChain() {
|
||||
if requireProviders {
|
||||
if _, ok := tryRouteBackend(backend, cfg, req); !ok {
|
||||
continue
|
||||
}
|
||||
} else if !backendRunnableLenient(backend, cfg, req) {
|
||||
continue
|
||||
}
|
||||
switch backend {
|
||||
case backendAntigravityGoogle:
|
||||
plans = append(plans, executionPlan{
|
||||
backend: backend,
|
||||
model: resolveAntigravityWebSearchTargetModel(cfg.AntigravityModel, req.RequestedModel),
|
||||
})
|
||||
case backendCodexWebSearch:
|
||||
plans = append(plans, executionPlan{
|
||||
backend: backend,
|
||||
model: resolveCodexWebSearchTargetModel(cfg.CodexModel),
|
||||
})
|
||||
case backendXAIWebSearch:
|
||||
plans = append(plans, executionPlan{
|
||||
backend: backend,
|
||||
model: resolveXAIWebSearchTargetModel(cfg.XAIModel),
|
||||
})
|
||||
case backendTavily:
|
||||
plans = append(plans, executionPlan{backend: backend})
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
return plans
|
||||
}
|
||||
|
||||
func backendRunnableLenient(backend routeBackend, cfg pluginConfig, req pluginapi.ModelRouteRequest) bool {
|
||||
switch backend {
|
||||
case backendTavily:
|
||||
return newTavilyClient(cfg.TavilyAPIKeys).available()
|
||||
case backendAntigravityGoogle:
|
||||
return resolveAntigravityWebSearchTargetModel(cfg.AntigravityModel, req.RequestedModel) != ""
|
||||
case backendCodexWebSearch, backendXAIWebSearch:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func executionPlansForRoute(cfg pluginConfig, req pluginapi.ModelRouteRequest, route string) []executionPlan {
|
||||
if isFallbackRoute(route) {
|
||||
return buildExecutionPlans(cfg, req)
|
||||
}
|
||||
backend := routeBackend(strings.TrimSpace(route))
|
||||
if _, ok := tryRouteBackend(backend, cfg, req); !ok {
|
||||
return nil
|
||||
}
|
||||
var plans []executionPlan
|
||||
for _, b := range []routeBackend{backend} {
|
||||
if !backendRunnableLenient(b, cfg, req) {
|
||||
continue
|
||||
}
|
||||
switch b {
|
||||
case backendAntigravityGoogle:
|
||||
plans = append(plans, executionPlan{backend: b, model: resolveAntigravityWebSearchTargetModel(cfg.AntigravityModel, req.RequestedModel)})
|
||||
case backendCodexWebSearch:
|
||||
plans = append(plans, executionPlan{backend: b, model: resolveCodexWebSearchTargetModel(cfg.CodexModel)})
|
||||
case backendXAIWebSearch:
|
||||
plans = append(plans, executionPlan{backend: b, model: resolveXAIWebSearchTargetModel(cfg.XAIModel)})
|
||||
case backendTavily:
|
||||
plans = append(plans, executionPlan{backend: b})
|
||||
}
|
||||
}
|
||||
return plans
|
||||
}
|
||||
|
||||
func claudeRequestBody(exec pluginapi.ExecutorRequest) []byte {
|
||||
if len(exec.OriginalRequest) > 0 {
|
||||
return exec.OriginalRequest
|
||||
}
|
||||
return exec.Payload
|
||||
}
|
||||
|
||||
func runWebSearchWithExecutionFallback(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID string) ([]byte, http.Header, error) {
|
||||
cfg := loadedConfig()
|
||||
req := pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
RequestedModel: strings.TrimSpace(exec.Model),
|
||||
Body: claudeRequestBody(exec),
|
||||
AvailableProviders: availableProvidersFromMetadata(exec.Metadata),
|
||||
}
|
||||
return runOrderedExecutionPlans(ctx, exec, hostCallbackID, cfg, buildExecutionPlansForExecute(cfg, req), false)
|
||||
}
|
||||
|
||||
// runWebSearchStreamWithExecutionFallback buffers the full host stream (non-streaming RPC path only).
|
||||
func runWebSearchStreamWithExecutionFallback(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID string) ([]byte, http.Header, error) {
|
||||
cfg := loadedConfig()
|
||||
req := pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
RequestedModel: strings.TrimSpace(exec.Model),
|
||||
Body: claudeRequestBody(exec),
|
||||
AvailableProviders: availableProvidersFromMetadata(exec.Metadata),
|
||||
}
|
||||
return runOrderedExecutionPlans(ctx, exec, hostCallbackID, cfg, buildExecutionPlansForExecute(cfg, req), true)
|
||||
}
|
||||
|
||||
func runOrderedExecutionPlans(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID string, cfg pluginConfig, plans []executionPlan, stream bool) ([]byte, http.Header, error) {
|
||||
if len(plans) == 0 {
|
||||
return nil, nil, fmt.Errorf("web search execution: no backend available")
|
||||
}
|
||||
backends := make([]routeBackend, 0, len(plans))
|
||||
for _, p := range plans {
|
||||
backends = append(backends, p.backend)
|
||||
}
|
||||
ordered := sortBackendsByPenalty(backends)
|
||||
planByBackend := make(map[routeBackend]executionPlan, len(plans))
|
||||
for _, p := range plans {
|
||||
planByBackend[p.backend] = p
|
||||
}
|
||||
|
||||
body := claudeRequestBody(exec)
|
||||
var lastErr error
|
||||
for _, backend := range ordered {
|
||||
plan := planByBackend[backend]
|
||||
switch backend {
|
||||
case backendTavily:
|
||||
var payload []byte
|
||||
var headers http.Header
|
||||
var errRun error
|
||||
if stream {
|
||||
payload, headers, errRun = runTavilyClaudeStreamWithClient(ctx, exec, newTavilyClient(cfg.TavilyAPIKeys))
|
||||
} else {
|
||||
payload, headers, errRun = runTavilyClaudeWithClient(ctx, exec, newTavilyClient(cfg.TavilyAPIKeys))
|
||||
}
|
||||
if errRun != nil {
|
||||
lastErr = errRun
|
||||
continue
|
||||
}
|
||||
recordBackendSuccess(backend)
|
||||
return payload, headers, nil
|
||||
default:
|
||||
payload, status, errRun := hostModelExecuteClaude(ctx, hostCallbackID, plan.model, body, stream)
|
||||
if errRun != nil {
|
||||
lastErr = errRun
|
||||
if isRetryableHTTPStatus(hostHTTPStatusFromError(errRun)) {
|
||||
recordBackendFailure(backend)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if isRetryableHTTPStatus(status) {
|
||||
recordBackendFailure(backend)
|
||||
lastErr = fmt.Errorf("host model status %d", status)
|
||||
continue
|
||||
}
|
||||
recordBackendSuccess(backend)
|
||||
headers := http.Header{"Content-Type": []string{"application/json"}}
|
||||
if stream {
|
||||
headers = http.Header{"Content-Type": []string{"text/event-stream"}}
|
||||
}
|
||||
return payload, headers, nil
|
||||
}
|
||||
}
|
||||
if lastErr != nil {
|
||||
return nil, nil, lastErr
|
||||
}
|
||||
return nil, nil, fmt.Errorf("web search execution: all backends failed")
|
||||
}
|
||||
|
||||
func availableProvidersFromMetadata(meta map[string]any) []string {
|
||||
if meta == nil {
|
||||
return nil
|
||||
}
|
||||
raw, ok := meta["available_providers"]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
switch v := raw.(type) {
|
||||
case []string:
|
||||
return v
|
||||
case []any:
|
||||
out := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
if s, okItem := item.(string); okItem {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func hostModelExecuteClaude(ctx context.Context, hostCallbackID, execModel string, body []byte, stream bool) ([]byte, int, error) {
|
||||
if stream {
|
||||
return hostModelStreamClaude(ctx, hostCallbackID, execModel, body)
|
||||
}
|
||||
raw, errCall := callHost(pluginabi.MethodHostModelExecute, hostModelExecutionRequest{
|
||||
HostModelExecutionRequest: pluginapi.HostModelExecutionRequest{
|
||||
EntryProtocol: "claude",
|
||||
ExitProtocol: "claude",
|
||||
Model: execModel,
|
||||
Stream: false,
|
||||
Body: body,
|
||||
},
|
||||
HostCallbackID: hostCallbackID,
|
||||
})
|
||||
if errCall != nil {
|
||||
return nil, hostHTTPStatusFromError(errCall), errCall
|
||||
}
|
||||
var resp pluginapi.HostModelExecutionResponse
|
||||
if errDecode := json.Unmarshal(raw, &resp); errDecode != nil {
|
||||
return nil, 0, errDecode
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, resp.StatusCode, fmt.Errorf("host model status %d", resp.StatusCode)
|
||||
}
|
||||
return resp.Body, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func hostModelStreamClaude(ctx context.Context, hostCallbackID, execModel string, body []byte) ([]byte, int, error) {
|
||||
raw, errCall := callHost(pluginabi.MethodHostModelExecuteStream, hostModelExecutionRequest{
|
||||
HostModelExecutionRequest: pluginapi.HostModelExecutionRequest{
|
||||
EntryProtocol: "claude",
|
||||
ExitProtocol: "claude",
|
||||
Model: execModel,
|
||||
Stream: true,
|
||||
Body: body,
|
||||
},
|
||||
HostCallbackID: hostCallbackID,
|
||||
})
|
||||
if errCall != nil {
|
||||
return nil, hostHTTPStatusFromError(errCall), errCall
|
||||
}
|
||||
var resp pluginapi.HostModelStreamResponse
|
||||
if errDecode := json.Unmarshal(raw, &resp); errDecode != nil {
|
||||
return nil, 0, errDecode
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
_ = closeHostModelStream(resp.StreamID)
|
||||
return nil, resp.StatusCode, fmt.Errorf("host model status %d", resp.StatusCode)
|
||||
}
|
||||
if strings.TrimSpace(resp.StreamID) == "" {
|
||||
return nil, 0, fmt.Errorf("host model stream: empty stream_id")
|
||||
}
|
||||
defer func() { _ = closeHostModelStream(resp.StreamID) }()
|
||||
|
||||
var buf bytes.Buffer
|
||||
for {
|
||||
chunkRaw, errRead := callHost(pluginabi.MethodHostModelStreamRead, pluginapi.HostModelStreamReadRequest{StreamID: resp.StreamID})
|
||||
if errRead != nil {
|
||||
return nil, hostHTTPStatusFromError(errRead), errRead
|
||||
}
|
||||
var chunk pluginapi.HostModelStreamReadResponse
|
||||
if errDecode := json.Unmarshal(chunkRaw, &chunk); errDecode != nil {
|
||||
return nil, 0, errDecode
|
||||
}
|
||||
if chunk.Error != "" {
|
||||
code := hostHTTPStatusFromError(fmt.Errorf("%s", chunk.Error))
|
||||
return nil, code, fmt.Errorf("%s", chunk.Error)
|
||||
}
|
||||
if len(chunk.Payload) > 0 {
|
||||
buf.Write(chunk.Payload)
|
||||
}
|
||||
if chunk.Done {
|
||||
break
|
||||
}
|
||||
}
|
||||
return buf.Bytes(), http.StatusOK, nil
|
||||
}
|
||||
|
||||
func closeHostModelStream(streamID string) error {
|
||||
_, errCall := callHost(pluginabi.MethodHostModelStreamClose, pluginapi.HostModelStreamCloseRequest{StreamID: streamID})
|
||||
return errCall
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
func TestBuildExecutionPlansForExecuteRespectsRouteTavily(t *testing.T) {
|
||||
currentConfig.Store(pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendTavily),
|
||||
TavilyAPIKeys: []string{"tvly-test"},
|
||||
})
|
||||
cfg := loadedConfig()
|
||||
req := pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
RequestedModel: "claude-sonnet-4-6",
|
||||
AvailableProviders: []string{"antigravity", "codex", "xai"},
|
||||
}
|
||||
plans := buildExecutionPlansForExecute(cfg, req)
|
||||
if len(plans) != 1 {
|
||||
t.Fatalf("plans len = %d, want 1 for route=tavily", len(plans))
|
||||
}
|
||||
if plans[0].backend != backendTavily {
|
||||
t.Fatalf("backend = %q, want tavily", plans[0].backend)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
// defaultWebSearchFallbackChain is the ordered backend try list when route=fallback.
|
||||
func defaultWebSearchFallbackChain() []routeBackend {
|
||||
return []routeBackend{
|
||||
backendAntigravityGoogle,
|
||||
backendCodexWebSearch,
|
||||
backendXAIWebSearch,
|
||||
backendTavily,
|
||||
}
|
||||
}
|
||||
|
||||
func isFallbackRoute(route string) bool {
|
||||
r := strings.ToLower(strings.TrimSpace(route))
|
||||
return r == "" || r == string(backendFallback)
|
||||
}
|
||||
|
||||
// tryRouteBackend returns a handled ModelRouteResponse and true when this backend can serve the request.
|
||||
func tryRouteBackend(backend routeBackend, cfg pluginConfig, req pluginapi.ModelRouteRequest) (pluginapi.ModelRouteResponse, bool) {
|
||||
switch backend {
|
||||
case backendTavily:
|
||||
client := newTavilyClient(cfg.TavilyAPIKeys)
|
||||
if !client.available() {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "tavily_unavailable"}, false
|
||||
}
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetSelf,
|
||||
Reason: "claude_code_web_search_tavily",
|
||||
}, true
|
||||
case backendAntigravityGoogle:
|
||||
if !hasProvider(req.AvailableProviders, "antigravity") {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "antigravity_unavailable"}, false
|
||||
}
|
||||
targetModel := resolveAntigravityWebSearchTargetModel(cfg.AntigravityModel, req.RequestedModel)
|
||||
if targetModel == "" {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "antigravity_web_search_model_unresolved"}, false
|
||||
}
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetProvider,
|
||||
Target: "antigravity",
|
||||
TargetModel: targetModel,
|
||||
Reason: "claude_code_web_search_antigravity_google",
|
||||
}, true
|
||||
case backendCodexWebSearch:
|
||||
if !hasProvider(req.AvailableProviders, "codex") {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "codex_unavailable"}, false
|
||||
}
|
||||
targetModel := resolveCodexWebSearchTargetModel(cfg.CodexModel)
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetProvider,
|
||||
Target: "codex",
|
||||
TargetModel: targetModel,
|
||||
Reason: "claude_code_web_search_codex",
|
||||
}, true
|
||||
case backendXAIWebSearch:
|
||||
if !hasProvider(req.AvailableProviders, "xai") {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "xai_unavailable"}, false
|
||||
}
|
||||
targetModel := resolveXAIWebSearchTargetModel(cfg.XAIModel)
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetProvider,
|
||||
Target: "xai",
|
||||
TargetModel: targetModel,
|
||||
Reason: "claude_code_web_search_xai",
|
||||
}, true
|
||||
case backendDefaultProvider:
|
||||
provider := cfg.DefaultProvider
|
||||
if provider == "" || !hasProvider(req.AvailableProviders, provider) {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "default_provider_unavailable"}, false
|
||||
}
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetProvider,
|
||||
Target: provider,
|
||||
TargetModel: cfg.DefaultProviderModel,
|
||||
Reason: "claude_code_web_search_default_provider",
|
||||
}, true
|
||||
default:
|
||||
return pluginapi.ModelRouteResponse{Handled: false}, false
|
||||
}
|
||||
}
|
||||
|
||||
func routeWithFallback(cfg pluginConfig, req pluginapi.ModelRouteRequest) pluginapi.ModelRouteResponse {
|
||||
return routeWithExecutionOrchestration(cfg, req, string(backendFallback))
|
||||
}
|
||||
|
||||
func routeWithExecutionOrchestration(cfg pluginConfig, req pluginapi.ModelRouteRequest, route string) pluginapi.ModelRouteResponse {
|
||||
plans := executionPlansForRoute(cfg, req, route)
|
||||
if len(plans) == 0 {
|
||||
return pluginapi.ModelRouteResponse{Handled: false, Reason: "web_search_fallback_exhausted"}
|
||||
}
|
||||
return pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetSelf,
|
||||
Reason: "claude_code_web_search_orchestrated",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
func claudeWebSearchRouteBody(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
body := []byte(`{
|
||||
"tools":[{"type":"web_search_20250305","name":"web_search","max_uses":5}],
|
||||
"system":[{"type":"text","text":"You have access to the web search tool use."}],
|
||||
"messages":[{"role":"user","content":[{"type":"text","text":"Perform a web search for the query: test"}]}]
|
||||
}`)
|
||||
return body
|
||||
}
|
||||
|
||||
func decodeModelRouteResponse(t *testing.T, raw []byte) pluginapi.ModelRouteResponse {
|
||||
t.Helper()
|
||||
var env envelope
|
||||
if err := json.Unmarshal(raw, &env); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var resp pluginapi.ModelRouteResponse
|
||||
if err := json.Unmarshal(env.Result, &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func TestRouteWithFallbackAntigravityFirst(t *testing.T) {
|
||||
reg := registry.GetGlobalRegistry()
|
||||
const clientID = "test-fallback-antigravity"
|
||||
reg.RegisterClient(clientID, "antigravity", []*registry.ModelInfo{
|
||||
{ID: "gem-fallback-test", SupportsWebSearch: true},
|
||||
})
|
||||
t.Cleanup(func() { reg.UnregisterClient(clientID) })
|
||||
|
||||
currentConfig.Store(pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendFallback),
|
||||
})
|
||||
raw, err := routeModel(mustJSON(t, rpcModelRouteRequest{
|
||||
ModelRouteRequest: pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
Body: claudeWebSearchRouteBody(t),
|
||||
RequestedModel: "claude-sonnet-4-6",
|
||||
AvailableProviders: []string{"antigravity", "codex", "xai"},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp := decodeModelRouteResponse(t, raw)
|
||||
if !resp.Handled || resp.TargetKind != pluginapi.ModelRouteTargetSelf {
|
||||
t.Fatalf("resp = %#v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteWithFallbackSkipsAntigravityToCodex(t *testing.T) {
|
||||
currentConfig.Store(pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendFallback),
|
||||
})
|
||||
raw, err := routeModel(mustJSON(t, rpcModelRouteRequest{
|
||||
ModelRouteRequest: pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
Body: claudeWebSearchRouteBody(t),
|
||||
RequestedModel: "claude-sonnet-4-6",
|
||||
AvailableProviders: []string{"codex", "xai"},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp := decodeModelRouteResponse(t, raw)
|
||||
if !resp.Handled || resp.TargetKind != pluginapi.ModelRouteTargetSelf {
|
||||
t.Fatalf("resp = %#v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteWithFallbackToTavily(t *testing.T) {
|
||||
currentConfig.Store(pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendFallback),
|
||||
TavilyAPIKeys: []string{"tvly-test"},
|
||||
})
|
||||
raw, err := routeModel(mustJSON(t, rpcModelRouteRequest{
|
||||
ModelRouteRequest: pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
Body: claudeWebSearchRouteBody(t),
|
||||
AvailableProviders: []string{},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp := decodeModelRouteResponse(t, raw)
|
||||
if !resp.Handled || resp.TargetKind != pluginapi.ModelRouteTargetSelf {
|
||||
t.Fatalf("resp = %#v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteWithFallbackExhausted(t *testing.T) {
|
||||
currentConfig.Store(pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendFallback),
|
||||
})
|
||||
raw, err := routeModel(mustJSON(t, rpcModelRouteRequest{
|
||||
ModelRouteRequest: pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
Body: claudeWebSearchRouteBody(t),
|
||||
AvailableProviders: []string{},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp := decodeModelRouteResponse(t, raw)
|
||||
if resp.Handled {
|
||||
t.Fatalf("expected declined, got %#v", resp)
|
||||
}
|
||||
if resp.Reason == "" || resp.Reason[:len("web_search_fallback_exhausted")] != "web_search_fallback_exhausted" {
|
||||
t.Fatalf("reason = %q", resp.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSON(t *testing.T, v any) []byte {
|
||||
t.Helper()
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/claude-web-search-router/go
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/router-for-me/CLIProxyAPI/v7 v7.0.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/router-for-me/CLIProxyAPI/v7 => ../../../..
|
||||
@@ -0,0 +1,24 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
||||
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,482 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
|
||||
static const cliproxy_host_api* stored_host;
|
||||
|
||||
static void store_host_api(const cliproxy_host_api* host) {
|
||||
stored_host = host;
|
||||
}
|
||||
|
||||
static int call_host_api(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (stored_host == NULL || stored_host->call == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return stored_host->call(stored_host->host_ctx, method, request, request_len, response);
|
||||
}
|
||||
|
||||
static void free_host_buffer(void* ptr, size_t len) {
|
||||
if (stored_host != NULL && stored_host->free_buffer != NULL && ptr != NULL) {
|
||||
stored_host->free_buffer(ptr, len);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const pluginIdentifier = "claude-web-search-router"
|
||||
|
||||
type routeBackend string
|
||||
|
||||
const (
|
||||
backendFallback routeBackend = "fallback"
|
||||
backendAntigravityGoogle routeBackend = "antigravity_google"
|
||||
backendCodexWebSearch routeBackend = "codex_web_search"
|
||||
backendXAIWebSearch routeBackend = "xai_web_search"
|
||||
backendTavily routeBackend = "tavily"
|
||||
backendDefaultProvider routeBackend = "default_provider"
|
||||
)
|
||||
|
||||
var currentConfig atomic.Value
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type lifecycleRequest struct {
|
||||
ConfigYAML []byte `json:"config_yaml"`
|
||||
}
|
||||
|
||||
type pluginConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Route string `yaml:"route"`
|
||||
AntigravityModel string `yaml:"antigravity_model"`
|
||||
CodexModel string `yaml:"codex_model"`
|
||||
XAIModel string `yaml:"xai_model"`
|
||||
DefaultProvider string `yaml:"default_provider"`
|
||||
DefaultProviderModel string `yaml:"default_provider_model"`
|
||||
TavilyAPIKeys []string `yaml:"tavily_api_keys"`
|
||||
RequireWebSearchOnly bool `yaml:"require_web_search_only"`
|
||||
}
|
||||
|
||||
type registration struct {
|
||||
SchemaVersion uint32 `json:"schema_version"`
|
||||
Metadata pluginapi.Metadata `json:"metadata"`
|
||||
Capabilities registrationCapability `json:"capabilities"`
|
||||
}
|
||||
|
||||
type registrationCapability struct {
|
||||
ModelRouter bool `json:"model_router"`
|
||||
Executor bool `json:"executor"`
|
||||
ExecutorModelScope string `json:"executor_model_scope"`
|
||||
ExecutorInputFormats []string `json:"executor_input_formats"`
|
||||
ExecutorOutputFormats []string `json:"executor_output_formats"`
|
||||
}
|
||||
|
||||
type rpcExecutorRequest struct {
|
||||
pluginapi.ExecutorRequest
|
||||
StreamID string `json:"stream_id,omitempty"`
|
||||
HostCallbackID string `json:"host_callback_id,omitempty"`
|
||||
}
|
||||
|
||||
type rpcModelRouteRequest struct {
|
||||
pluginapi.ModelRouteRequest
|
||||
HostCallbackID string `json:"host_callback_id,omitempty"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(host *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
C.store_host_api(host)
|
||||
plugin.abi_version = C.uint32_t(pluginabi.ABIVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
var requestBytes []byte
|
||||
if request != nil && requestLen > 0 {
|
||||
requestBytes = C.GoBytes(unsafe.Pointer(request), C.int(requestLen))
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method), requestBytes)
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, _ C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string, request []byte) ([]byte, error) {
|
||||
switch method {
|
||||
case pluginabi.MethodPluginRegister, pluginabi.MethodPluginReconfigure:
|
||||
if errConfigure := configure(request); errConfigure != nil {
|
||||
return nil, errConfigure
|
||||
}
|
||||
return okEnvelope(pluginRegistration())
|
||||
case pluginabi.MethodModelRoute:
|
||||
return routeModel(request)
|
||||
case pluginabi.MethodExecutorIdentifier:
|
||||
return okEnvelope(map[string]string{"identifier": pluginIdentifier})
|
||||
case pluginabi.MethodExecutorExecute:
|
||||
return execute(request)
|
||||
case pluginabi.MethodExecutorExecuteStream:
|
||||
return executeStream(request)
|
||||
case pluginabi.MethodExecutorCountTokens:
|
||||
return okEnvelope(pluginapi.ExecutorResponse{Payload: []byte(`{"input_tokens":0}`)})
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func configure(raw []byte) error {
|
||||
var req lifecycleRequest
|
||||
if len(raw) > 0 {
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return errUnmarshal
|
||||
}
|
||||
}
|
||||
cfg := defaultPluginConfig()
|
||||
if len(req.ConfigYAML) > 0 {
|
||||
decoded, errDecode := decodeConfig(req.ConfigYAML)
|
||||
if errDecode != nil {
|
||||
return errDecode
|
||||
}
|
||||
cfg = decoded
|
||||
}
|
||||
currentConfig.Store(cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultPluginConfig() pluginConfig {
|
||||
return pluginConfig{
|
||||
Enabled: true,
|
||||
Route: string(backendFallback),
|
||||
RequireWebSearchOnly: true,
|
||||
}
|
||||
}
|
||||
|
||||
func decodeConfig(raw []byte) (pluginConfig, error) {
|
||||
cfg := defaultPluginConfig()
|
||||
if errUnmarshal := yaml.Unmarshal(raw, &cfg); errUnmarshal != nil {
|
||||
return pluginConfig{}, errUnmarshal
|
||||
}
|
||||
cfg.Route = strings.TrimSpace(cfg.Route)
|
||||
cfg.AntigravityModel = strings.TrimSpace(cfg.AntigravityModel)
|
||||
cfg.CodexModel = strings.TrimSpace(cfg.CodexModel)
|
||||
cfg.XAIModel = strings.TrimSpace(cfg.XAIModel)
|
||||
cfg.DefaultProvider = strings.ToLower(strings.TrimSpace(cfg.DefaultProvider))
|
||||
cfg.DefaultProviderModel = strings.TrimSpace(cfg.DefaultProviderModel)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func loadedConfig() pluginConfig {
|
||||
raw := currentConfig.Load()
|
||||
if cfg, ok := raw.(pluginConfig); ok {
|
||||
return cfg
|
||||
}
|
||||
return defaultPluginConfig()
|
||||
}
|
||||
|
||||
func pluginRegistration() registration {
|
||||
return registration{
|
||||
SchemaVersion: pluginabi.SchemaVersion,
|
||||
Metadata: pluginapi.Metadata{
|
||||
Name: "claude-web-search-router",
|
||||
Version: "0.1.0",
|
||||
Author: "router-for-me",
|
||||
GitHubRepository: "https://github.com/router-for-me/CLIProxyAPI",
|
||||
ConfigFields: []pluginapi.ConfigField{
|
||||
{Name: "enabled", Type: pluginapi.ConfigFieldTypeBoolean, Description: "When false, the router declines all Claude web_search requests."},
|
||||
{Name: "route", Type: pluginapi.ConfigFieldTypeEnum, EnumValues: []string{
|
||||
string(backendFallback), string(backendAntigravityGoogle), string(backendCodexWebSearch),
|
||||
string(backendXAIWebSearch), string(backendTavily), string(backendDefaultProvider),
|
||||
}, Description: "Backend for Claude Code web_search. fallback (default): antigravity → codex → xai → tavily."},
|
||||
{Name: "antigravity_model", Type: pluginapi.ConfigFieldTypeString, Description: "Antigravity googleSearch model (empty: registry lookup, then first supports_web_search)."},
|
||||
{Name: "codex_model", Type: pluginapi.ConfigFieldTypeString, Description: "Codex Responses model for web_search (empty defaults to gpt-5.4, never client Claude model)."},
|
||||
{Name: "xai_model", Type: pluginapi.ConfigFieldTypeString, Description: "xAI Responses model with web_search (empty uses grok-4.3, not the client Claude model)."},
|
||||
{Name: "default_provider", Type: pluginapi.ConfigFieldTypeString, Description: "Built-in provider key when route=default_provider."},
|
||||
{Name: "default_provider_model", Type: pluginapi.ConfigFieldTypeString, Description: "Optional execution model on default_provider route."},
|
||||
{Name: "tavily_api_keys", Type: pluginapi.ConfigFieldTypeArray, Description: "Tavily API keys (round-robin) when route=tavily."},
|
||||
{Name: "require_web_search_only", Type: pluginapi.ConfigFieldTypeBoolean, Description: "Require tools to be exclusively typed web_search (matches antigravity-only path)."},
|
||||
},
|
||||
},
|
||||
Capabilities: registrationCapability{
|
||||
ModelRouter: true,
|
||||
Executor: true,
|
||||
ExecutorModelScope: string(pluginapi.ExecutorModelScopeStatic),
|
||||
ExecutorInputFormats: []string{"claude"},
|
||||
ExecutorOutputFormats: []string{"claude"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func routeModel(raw []byte) ([]byte, error) {
|
||||
var req rpcModelRouteRequest
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return nil, errUnmarshal
|
||||
}
|
||||
cfg := loadedConfig()
|
||||
if !cfg.Enabled {
|
||||
return okEnvelope(pluginapi.ModelRouteResponse{Handled: false})
|
||||
}
|
||||
if !isClaudeSourceFormat(req.SourceFormat) {
|
||||
return okEnvelope(pluginapi.ModelRouteResponse{Handled: false})
|
||||
}
|
||||
if !isClaudeCodeBuiltinWebSearchRequest(req.Body, cfg.RequireWebSearchOnly) {
|
||||
return okEnvelope(pluginapi.ModelRouteResponse{Handled: false})
|
||||
}
|
||||
route := strings.TrimSpace(cfg.Route)
|
||||
if isFallbackRoute(route) {
|
||||
return okEnvelope(routeWithFallback(cfg, req.ModelRouteRequest))
|
||||
}
|
||||
if plans := executionPlansForRoute(cfg, req.ModelRouteRequest, route); len(plans) > 0 {
|
||||
return okEnvelope(pluginapi.ModelRouteResponse{
|
||||
Handled: true,
|
||||
TargetKind: pluginapi.ModelRouteTargetSelf,
|
||||
Reason: "claude_code_web_search_orchestrated",
|
||||
})
|
||||
}
|
||||
backend := routeBackend(route)
|
||||
resp, ok := tryRouteBackend(backend, cfg, req.ModelRouteRequest)
|
||||
if ok {
|
||||
return okEnvelope(resp)
|
||||
}
|
||||
if strings.TrimSpace(resp.Reason) != "" {
|
||||
return okEnvelope(resp)
|
||||
}
|
||||
return okEnvelope(pluginapi.ModelRouteResponse{Handled: false})
|
||||
}
|
||||
|
||||
func hasProvider(providers []string, key string) bool {
|
||||
key = strings.ToLower(strings.TrimSpace(key))
|
||||
for _, p := range providers {
|
||||
if strings.ToLower(strings.TrimSpace(p)) == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func execute(raw []byte) ([]byte, error) {
|
||||
var req rpcExecutorRequest
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return nil, errUnmarshal
|
||||
}
|
||||
body, headers, errRun := runWebSearchWithExecutionFallback(context.Background(), req.ExecutorRequest, req.HostCallbackID)
|
||||
if errRun != nil {
|
||||
return errorEnvelope("executor_error", errRun.Error()), nil
|
||||
}
|
||||
return okEnvelope(pluginapi.ExecutorResponse{Payload: body, Headers: headers})
|
||||
}
|
||||
|
||||
func runTavilyClaude(ctx context.Context, req pluginapi.ExecutorRequest) ([]byte, http.Header, error) {
|
||||
return runTavilyClaudeWithClient(ctx, req, newTavilyClient(loadedConfig().TavilyAPIKeys))
|
||||
}
|
||||
|
||||
func runTavilyClaudeWithClient(ctx context.Context, req pluginapi.ExecutorRequest, client *tavilyClient) ([]byte, http.Header, error) {
|
||||
query := extractClaudeWebSearchQuery(req.OriginalRequest)
|
||||
if query == "" {
|
||||
query = extractClaudeWebSearchQuery(req.Payload)
|
||||
}
|
||||
maxResults := extractClaudeWebSearchMaxUses(req.OriginalRequest, 5)
|
||||
hits, answer, errSearch := client.search(ctx, query, maxResults)
|
||||
if errSearch != nil {
|
||||
return nil, nil, errSearch
|
||||
}
|
||||
model := strings.TrimSpace(req.Model)
|
||||
builder := newClaudeStreamBuilder(model)
|
||||
payload := builder.buildMessageJSON(query, hits, answer)
|
||||
headers := http.Header{"Content-Type": []string{"application/json"}}
|
||||
return payload, headers, nil
|
||||
}
|
||||
|
||||
func runTavilyClaudeStream(ctx context.Context, req pluginapi.ExecutorRequest) ([]byte, http.Header, error) {
|
||||
return runTavilyClaudeStreamWithClient(ctx, req, newTavilyClient(loadedConfig().TavilyAPIKeys))
|
||||
}
|
||||
|
||||
func runTavilyClaudeStreamWithClient(ctx context.Context, req pluginapi.ExecutorRequest, client *tavilyClient) ([]byte, http.Header, error) {
|
||||
query := extractClaudeWebSearchQuery(req.OriginalRequest)
|
||||
if query == "" {
|
||||
query = extractClaudeWebSearchQuery(req.Payload)
|
||||
}
|
||||
maxResults := extractClaudeWebSearchMaxUses(req.OriginalRequest, 5)
|
||||
hits, answer, errSearch := client.search(ctx, query, maxResults)
|
||||
if errSearch != nil {
|
||||
return nil, nil, errSearch
|
||||
}
|
||||
model := strings.TrimSpace(req.Model)
|
||||
builder := newClaudeStreamBuilder(model)
|
||||
payload := builder.buildStreamWithQuery(query, hits, answer)
|
||||
headers := http.Header{"Content-Type": []string{"text/event-stream"}}
|
||||
return payload, headers, nil
|
||||
}
|
||||
|
||||
type hostModelExecutionRequest struct {
|
||||
pluginapi.HostModelExecutionRequest
|
||||
HostCallbackID string `json:"host_callback_id,omitempty"`
|
||||
}
|
||||
|
||||
func callHost(method string, payload any) (json.RawMessage, error) {
|
||||
rawPayload, errMarshal := json.Marshal(payload)
|
||||
if errMarshal != nil {
|
||||
return nil, fmt.Errorf("marshal host callback %s: %w", method, errMarshal)
|
||||
}
|
||||
cMethod := C.CString(method)
|
||||
defer C.free(unsafe.Pointer(cMethod))
|
||||
|
||||
var response C.cliproxy_buffer
|
||||
var requestPtr *C.uint8_t
|
||||
if len(rawPayload) > 0 {
|
||||
cPayload := C.CBytes(rawPayload)
|
||||
if cPayload == nil {
|
||||
return nil, fmt.Errorf("allocate host callback %s", method)
|
||||
}
|
||||
defer C.free(cPayload)
|
||||
requestPtr = (*C.uint8_t)(cPayload)
|
||||
}
|
||||
callCode := C.call_host_api(cMethod, requestPtr, C.size_t(len(rawPayload)), &response)
|
||||
var rawResponse []byte
|
||||
if response.ptr != nil && response.len > 0 {
|
||||
rawResponse = C.GoBytes(response.ptr, C.int(response.len))
|
||||
}
|
||||
if response.ptr != nil {
|
||||
C.free_host_buffer(response.ptr, response.len)
|
||||
}
|
||||
if len(rawResponse) == 0 {
|
||||
return nil, fmt.Errorf("host callback %s returned no response, code=%d", method, int(callCode))
|
||||
}
|
||||
|
||||
var env envelope
|
||||
if errUnmarshal := json.Unmarshal(rawResponse, &env); errUnmarshal != nil {
|
||||
return nil, fmt.Errorf("decode host envelope %s: %w", method, errUnmarshal)
|
||||
}
|
||||
if !env.OK {
|
||||
if env.Error != nil {
|
||||
return nil, fmt.Errorf("%s: %s", env.Error.Code, env.Error.Message)
|
||||
}
|
||||
return nil, fmt.Errorf("host callback %s failed", method)
|
||||
}
|
||||
if callCode != 0 {
|
||||
return nil, fmt.Errorf("host callback %s returned code=%d", method, int(callCode))
|
||||
}
|
||||
return append(json.RawMessage(nil), env.Result...), nil
|
||||
}
|
||||
|
||||
func hostHTTPStatusFromError(err error) int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
msg := err.Error()
|
||||
for _, code := range []int{429, 503, 502} {
|
||||
if strings.Contains(msg, fmt.Sprintf("%d", code)) {
|
||||
return code
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func isRetryableHTTPStatus(code int) bool {
|
||||
return code == 429 || code == 503 || code == 502
|
||||
}
|
||||
func okEnvelope(v any) ([]byte, error) {
|
||||
raw, errMarshal := json.Marshal(v)
|
||||
if errMarshal != nil {
|
||||
return nil, errMarshal
|
||||
}
|
||||
return json.Marshal(envelope{OK: true, Result: raw})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
|
||||
)
|
||||
|
||||
const (
|
||||
// Default Codex model for Claude web_search → Codex Responses (override with codex_model).
|
||||
defaultCodexWebSearchModel = "gpt-5.4-mini"
|
||||
// Default xAI model for server-side web_search per https://docs.x.ai/developers/tools/web-search
|
||||
defaultXAIWebSearchModel = "grok-4.3"
|
||||
)
|
||||
|
||||
// resolveAntigravityWebSearchTargetModel picks an Antigravity model that can run native googleSearch.
|
||||
// Config antigravity_model wins; otherwise registry.AntigravityWebSearchModelFor(requested) or the
|
||||
// first available antigravity model with SupportsWebSearch.
|
||||
func resolveAntigravityWebSearchTargetModel(configured, requested string) string {
|
||||
if m := strings.TrimSpace(configured); m != "" {
|
||||
return m
|
||||
}
|
||||
if m := registry.AntigravityWebSearchModelFor(strings.TrimSpace(requested)); m != "" {
|
||||
return m
|
||||
}
|
||||
for _, model := range registry.GetGlobalRegistry().GetAvailableModelsByProvider("antigravity") {
|
||||
if model == nil || !model.SupportsWebSearch {
|
||||
continue
|
||||
}
|
||||
if id := strings.TrimSpace(model.ID); id != "" {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// resolveCodexWebSearchTargetModel never forwards the client Claude model to Codex.
|
||||
func resolveCodexWebSearchTargetModel(configured string) string {
|
||||
if m := strings.TrimSpace(configured); m != "" {
|
||||
return m
|
||||
}
|
||||
return defaultCodexWebSearchModel
|
||||
}
|
||||
|
||||
// resolveXAIWebSearchTargetModel never forwards the client Claude model to xAI Responses.
|
||||
func resolveXAIWebSearchTargetModel(configured string) string {
|
||||
if m := strings.TrimSpace(configured); m != "" {
|
||||
return m
|
||||
}
|
||||
return defaultXAIWebSearchModel
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
|
||||
)
|
||||
|
||||
func TestResolveCodexWebSearchTargetModelNeverUsesClaudeName(t *testing.T) {
|
||||
got := resolveCodexWebSearchTargetModel("")
|
||||
if got != defaultCodexWebSearchModel {
|
||||
t.Fatalf("empty config = %q, want %q", got, defaultCodexWebSearchModel)
|
||||
}
|
||||
if got := resolveCodexWebSearchTargetModel("gpt-5.5"); got != "gpt-5.5" {
|
||||
t.Fatalf("configured = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveXAIWebSearchTargetModelNeverUsesClaudeName(t *testing.T) {
|
||||
got := resolveXAIWebSearchTargetModel("")
|
||||
if got != defaultXAIWebSearchModel {
|
||||
t.Fatalf("empty config = %q, want %q", got, defaultXAIWebSearchModel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAntigravityWebSearchTargetModelConfiguredWins(t *testing.T) {
|
||||
if got := resolveAntigravityWebSearchTargetModel("my-gemini", "claude-sonnet-4-6"); got != "my-gemini" {
|
||||
t.Fatalf("configured = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAntigravityWebSearchTargetModelFromRegistry(t *testing.T) {
|
||||
reg := registry.GetGlobalRegistry()
|
||||
const clientID = "test-claude-web-search-router-antigravity"
|
||||
reg.RegisterClient(clientID, "antigravity", []*registry.ModelInfo{
|
||||
{ID: "gemini-web-search-test", SupportsWebSearch: true},
|
||||
})
|
||||
t.Cleanup(func() { reg.UnregisterClient(clientID) })
|
||||
got := resolveAntigravityWebSearchTargetModel("", "claude-sonnet-4-6")
|
||||
if got != "gemini-web-search-test" {
|
||||
t.Fatalf("fallback = %q, want gemini-web-search-test", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
penaltyBumpOn429503 = 5
|
||||
penaltyDecaySuccess = 1
|
||||
)
|
||||
|
||||
var backendPenalties = struct {
|
||||
sync.Mutex
|
||||
scores map[routeBackend]int
|
||||
}{
|
||||
scores: make(map[routeBackend]int),
|
||||
}
|
||||
|
||||
func recordBackendFailure(backend routeBackend) {
|
||||
backendPenalties.Lock()
|
||||
defer backendPenalties.Unlock()
|
||||
backendPenalties.scores[backend] += penaltyBumpOn429503
|
||||
}
|
||||
|
||||
func recordBackendSuccess(backend routeBackend) {
|
||||
backendPenalties.Lock()
|
||||
defer backendPenalties.Unlock()
|
||||
score := backendPenalties.scores[backend] - penaltyDecaySuccess
|
||||
if score < 0 {
|
||||
score = 0
|
||||
}
|
||||
backendPenalties.scores[backend] = score
|
||||
}
|
||||
|
||||
func penaltyScore(backend routeBackend) int {
|
||||
backendPenalties.Lock()
|
||||
defer backendPenalties.Unlock()
|
||||
return backendPenalties.scores[backend]
|
||||
}
|
||||
|
||||
func sortBackendsByPenalty(backends []routeBackend) []routeBackend {
|
||||
if len(backends) <= 1 {
|
||||
return append([]routeBackend(nil), backends...)
|
||||
}
|
||||
out := append([]routeBackend(nil), backends...)
|
||||
sort.SliceStable(out, func(i, j int) bool {
|
||||
return penaltyScore(out[i]) < penaltyScore(out[j])
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
func resetBackendPenaltiesForTest() {
|
||||
backendPenalties.Lock()
|
||||
defer backendPenalties.Unlock()
|
||||
backendPenalties.scores = make(map[routeBackend]int)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSortBackendsByPenaltyDeprioritizesFailures(t *testing.T) {
|
||||
resetBackendPenaltiesForTest()
|
||||
t.Cleanup(resetBackendPenaltiesForTest)
|
||||
recordBackendFailure(backendAntigravityGoogle)
|
||||
recordBackendFailure(backendAntigravityGoogle)
|
||||
ordered := sortBackendsByPenalty([]routeBackend{
|
||||
backendAntigravityGoogle,
|
||||
backendCodexWebSearch,
|
||||
backendXAIWebSearch,
|
||||
})
|
||||
if ordered[0] != backendCodexWebSearch {
|
||||
t.Fatalf("ordered = %v, want codex first after antigravity penalty", ordered)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
type rpcStreamEmitRequest struct {
|
||||
StreamID string `json:"stream_id"`
|
||||
Payload []byte `json:"payload,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type rpcStreamCloseRequest struct {
|
||||
StreamID string `json:"stream_id"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func emitPluginStreamChunk(streamID string, payload []byte) error {
|
||||
if strings.TrimSpace(streamID) == "" {
|
||||
return fmt.Errorf("plugin stream id is required")
|
||||
}
|
||||
_, errCall := callHost(pluginabi.MethodHostStreamEmit, rpcStreamEmitRequest{
|
||||
StreamID: streamID,
|
||||
Payload: payload,
|
||||
})
|
||||
return errCall
|
||||
}
|
||||
|
||||
func closePluginStream(streamID, errMsg string) {
|
||||
if strings.TrimSpace(streamID) == "" {
|
||||
return
|
||||
}
|
||||
_, _ = callHost(pluginabi.MethodHostStreamClose, rpcStreamCloseRequest{
|
||||
StreamID: streamID,
|
||||
Error: strings.TrimSpace(errMsg),
|
||||
})
|
||||
}
|
||||
|
||||
func looksLikeOpenAIResponsesSSE(payload []byte) bool {
|
||||
if len(payload) == 0 {
|
||||
return false
|
||||
}
|
||||
s := string(payload)
|
||||
if strings.Contains(s, "event: message_start") {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(s, "event: response.") ||
|
||||
strings.Contains(s, `"type":"response.`) ||
|
||||
strings.Contains(s, `"type": "response.`)
|
||||
}
|
||||
|
||||
func runWebSearchStreamOrchestration(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID, pluginStreamID string) error {
|
||||
cfg := loadedConfig()
|
||||
req := pluginapi.ModelRouteRequest{
|
||||
SourceFormat: "claude",
|
||||
RequestedModel: strings.TrimSpace(exec.Model),
|
||||
Body: claudeRequestBody(exec),
|
||||
AvailableProviders: availableProvidersFromMetadata(exec.Metadata),
|
||||
}
|
||||
return runOrderedExecutionPlansStream(ctx, exec, hostCallbackID, pluginStreamID, cfg, buildExecutionPlansForExecute(cfg, req))
|
||||
}
|
||||
|
||||
func runOrderedExecutionPlansStream(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID, pluginStreamID string, cfg pluginConfig, plans []executionPlan) error {
|
||||
if len(plans) == 0 {
|
||||
return fmt.Errorf("web search execution: no backend available")
|
||||
}
|
||||
backends := make([]routeBackend, 0, len(plans))
|
||||
for _, p := range plans {
|
||||
backends = append(backends, p.backend)
|
||||
}
|
||||
ordered := sortBackendsByPenalty(backends)
|
||||
planByBackend := make(map[routeBackend]executionPlan, len(plans))
|
||||
for _, p := range plans {
|
||||
planByBackend[p.backend] = p
|
||||
}
|
||||
|
||||
body := claudeRequestBody(exec)
|
||||
var lastErr error
|
||||
for _, backend := range ordered {
|
||||
plan := planByBackend[backend]
|
||||
switch backend {
|
||||
case backendTavily:
|
||||
payload, _, errRun := runTavilyClaudeStreamWithClient(ctx, exec, newTavilyClient(cfg.TavilyAPIKeys))
|
||||
if errRun != nil {
|
||||
lastErr = errRun
|
||||
continue
|
||||
}
|
||||
if errEmit := emitPluginStreamChunk(pluginStreamID, payload); errEmit != nil {
|
||||
return errEmit
|
||||
}
|
||||
recordBackendSuccess(backend)
|
||||
return nil
|
||||
default:
|
||||
status, errRun := hostModelStreamForwardClaude(ctx, hostCallbackID, plan.model, body, pluginStreamID)
|
||||
if errRun != nil {
|
||||
lastErr = errRun
|
||||
if isRetryableHTTPStatus(hostHTTPStatusFromError(errRun)) {
|
||||
recordBackendFailure(backend)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if isRetryableHTTPStatus(status) {
|
||||
recordBackendFailure(backend)
|
||||
lastErr = fmt.Errorf("host model status %d", status)
|
||||
continue
|
||||
}
|
||||
recordBackendSuccess(backend)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if lastErr != nil {
|
||||
return lastErr
|
||||
}
|
||||
return fmt.Errorf("web search execution: all backends failed")
|
||||
}
|
||||
|
||||
func hostModelStreamForwardClaude(ctx context.Context, hostCallbackID, execModel string, body []byte, pluginStreamID string) (int, error) {
|
||||
raw, errCall := callHost(pluginabi.MethodHostModelExecuteStream, hostModelExecutionRequest{
|
||||
HostModelExecutionRequest: pluginapi.HostModelExecutionRequest{
|
||||
EntryProtocol: "claude",
|
||||
ExitProtocol: "claude",
|
||||
Model: execModel,
|
||||
Stream: true,
|
||||
Body: body,
|
||||
},
|
||||
HostCallbackID: hostCallbackID,
|
||||
})
|
||||
if errCall != nil {
|
||||
return hostHTTPStatusFromError(errCall), errCall
|
||||
}
|
||||
var resp pluginapi.HostModelStreamResponse
|
||||
if errDecode := json.Unmarshal(raw, &resp); errDecode != nil {
|
||||
return 0, errDecode
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
_ = closeHostModelStream(resp.StreamID)
|
||||
return resp.StatusCode, fmt.Errorf("host model status %d", resp.StatusCode)
|
||||
}
|
||||
if strings.TrimSpace(resp.StreamID) == "" {
|
||||
return 0, fmt.Errorf("host model stream: empty stream_id")
|
||||
}
|
||||
defer func() { _ = closeHostModelStream(resp.StreamID) }()
|
||||
|
||||
firstPayload := true
|
||||
for {
|
||||
chunkRaw, errRead := callHost(pluginabi.MethodHostModelStreamRead, pluginapi.HostModelStreamReadRequest{StreamID: resp.StreamID})
|
||||
if errRead != nil {
|
||||
return hostHTTPStatusFromError(errRead), errRead
|
||||
}
|
||||
var chunk pluginapi.HostModelStreamReadResponse
|
||||
if errDecode := json.Unmarshal(chunkRaw, &chunk); errDecode != nil {
|
||||
return 0, errDecode
|
||||
}
|
||||
if chunk.Error != "" {
|
||||
code := hostHTTPStatusFromError(fmt.Errorf("%s", chunk.Error))
|
||||
return code, fmt.Errorf("%s", chunk.Error)
|
||||
}
|
||||
if len(chunk.Payload) > 0 {
|
||||
if firstPayload && looksLikeOpenAIResponsesSSE(chunk.Payload) {
|
||||
return 0, fmt.Errorf("host model stream returned OpenAI Responses SSE instead of Claude Messages SSE")
|
||||
}
|
||||
firstPayload = false
|
||||
if errEmit := emitPluginStreamChunk(pluginStreamID, bytes.Clone(chunk.Payload)); errEmit != nil {
|
||||
return 0, errEmit
|
||||
}
|
||||
}
|
||||
if chunk.Done {
|
||||
break
|
||||
}
|
||||
}
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
func TestLooksLikeOpenAIResponsesSSE(t *testing.T) {
|
||||
if !looksLikeOpenAIResponsesSSE([]byte("event: response.created\ndata: {\"type\":\"response.created\"}\n\n")) {
|
||||
t.Fatal("expected OpenAI Responses SSE detection")
|
||||
}
|
||||
if looksLikeOpenAIResponsesSSE([]byte("event: message_start\ndata: {\"type\":\"message_start\"}\n\n")) {
|
||||
t.Fatal("expected Claude Messages SSE to not match Responses detector")
|
||||
}
|
||||
if looksLikeOpenAIResponsesSSE(nil) {
|
||||
t.Fatal("empty payload should not match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartExecutorStreamRunsOrchestrationAfterRPCReturns(t *testing.T) {
|
||||
started := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
closed := make(chan string, 1)
|
||||
req := rpcExecutorRequest{
|
||||
ExecutorRequest: pluginapi.ExecutorRequest{Stream: true},
|
||||
StreamID: "stream-1",
|
||||
HostCallbackID: "callback-1",
|
||||
}
|
||||
|
||||
raw, errStart := startExecutorStream(req, func(ctx context.Context, exec pluginapi.ExecutorRequest, hostCallbackID, pluginStreamID string) error {
|
||||
if hostCallbackID != "callback-1" || pluginStreamID != "stream-1" {
|
||||
t.Errorf("runner ids = %q/%q, want callback-1/stream-1", hostCallbackID, pluginStreamID)
|
||||
}
|
||||
close(started)
|
||||
<-release
|
||||
return nil
|
||||
}, func(streamID, errMsg string) {
|
||||
closed <- streamID + "|" + errMsg
|
||||
})
|
||||
if errStart != nil {
|
||||
t.Fatalf("startExecutorStream() error = %v", errStart)
|
||||
}
|
||||
if !strings.Contains(string(raw), "text/event-stream") {
|
||||
t.Fatalf("response does not include stream headers: %s", raw)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("orchestration did not start")
|
||||
}
|
||||
select {
|
||||
case got := <-closed:
|
||||
t.Fatalf("stream closed before orchestration finished: %q", got)
|
||||
default:
|
||||
}
|
||||
|
||||
close(release)
|
||||
select {
|
||||
case got := <-closed:
|
||||
if got != "stream-1|" {
|
||||
t.Fatalf("close call = %q, want stream-1|", got)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("stream was not closed after orchestration finished")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const tavilySearchURL = "https://api.tavily.com/search"
|
||||
|
||||
type tavilyClient struct {
|
||||
keys []string
|
||||
idx atomic.Uint64
|
||||
http *http.Client
|
||||
baseURL string // empty → https://api.tavily.com/search
|
||||
}
|
||||
|
||||
func newTavilyClient(keys []string) *tavilyClient {
|
||||
return newTavilyClientWithOptions(keys, nil, "")
|
||||
}
|
||||
|
||||
func newTavilyClientWithOptions(keys []string, httpClient *http.Client, baseURL string) *tavilyClient {
|
||||
trimmed := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
if k := strings.TrimSpace(key); k != "" {
|
||||
trimmed = append(trimmed, k)
|
||||
}
|
||||
}
|
||||
if httpClient == nil {
|
||||
httpClient = &http.Client{}
|
||||
}
|
||||
return &tavilyClient{
|
||||
keys: trimmed,
|
||||
http: httpClient,
|
||||
baseURL: strings.TrimSpace(baseURL),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *tavilyClient) searchEndpoint() string {
|
||||
if c != nil && c.baseURL != "" {
|
||||
return c.baseURL
|
||||
}
|
||||
return tavilySearchURL
|
||||
}
|
||||
|
||||
func (c *tavilyClient) available() bool {
|
||||
return c != nil && len(c.keys) > 0
|
||||
}
|
||||
|
||||
func (c *tavilyClient) nextKey() string {
|
||||
if len(c.keys) == 0 {
|
||||
return ""
|
||||
}
|
||||
n := c.idx.Add(1)
|
||||
return c.keys[int(n-1)%len(c.keys)]
|
||||
}
|
||||
|
||||
type tavilySearchRequest struct {
|
||||
APIKey string `json:"api_key"`
|
||||
Query string `json:"query"`
|
||||
SearchDepth string `json:"search_depth,omitempty"`
|
||||
MaxResults int `json:"max_results,omitempty"`
|
||||
IncludeAnswer bool `json:"include_answer,omitempty"`
|
||||
}
|
||||
|
||||
type tavilySearchResponse struct {
|
||||
Answer string `json:"answer"`
|
||||
Results []struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
Content string `json:"content"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
type claudeWebSearchHit struct {
|
||||
Title string
|
||||
URL string
|
||||
Snippet string
|
||||
}
|
||||
|
||||
func (c *tavilyClient) search(ctx context.Context, query string, maxResults int) ([]claudeWebSearchHit, string, error) {
|
||||
if !c.available() {
|
||||
return nil, "", fmt.Errorf("tavily_api_keys is empty")
|
||||
}
|
||||
query = strings.TrimSpace(query)
|
||||
if query == "" {
|
||||
return nil, "", fmt.Errorf("web search query is empty")
|
||||
}
|
||||
if maxResults <= 0 {
|
||||
maxResults = 5
|
||||
}
|
||||
payload, errMarshal := json.Marshal(tavilySearchRequest{
|
||||
APIKey: c.nextKey(),
|
||||
Query: query,
|
||||
SearchDepth: "basic",
|
||||
MaxResults: maxResults,
|
||||
IncludeAnswer: true,
|
||||
})
|
||||
if errMarshal != nil {
|
||||
return nil, "", errMarshal
|
||||
}
|
||||
req, errNew := http.NewRequestWithContext(ctx, http.MethodPost, c.searchEndpoint(), bytes.NewReader(payload))
|
||||
if errNew != nil {
|
||||
return nil, "", errNew
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, errDo := c.http.Do(req)
|
||||
if errDo != nil {
|
||||
return nil, "", errDo
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, errRead := io.ReadAll(resp.Body)
|
||||
if errRead != nil {
|
||||
return nil, "", errRead
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, "", fmt.Errorf("tavily http %d: %s", resp.StatusCode, truncate(string(body), 512))
|
||||
}
|
||||
var parsed tavilySearchResponse
|
||||
if errDecode := json.Unmarshal(body, &parsed); errDecode != nil {
|
||||
return nil, "", errDecode
|
||||
}
|
||||
hits := make([]claudeWebSearchHit, 0, len(parsed.Results))
|
||||
for _, r := range parsed.Results {
|
||||
hits = append(hits, claudeWebSearchHit{
|
||||
Title: strings.TrimSpace(r.Title),
|
||||
URL: strings.TrimSpace(r.URL),
|
||||
Snippet: strings.TrimSpace(r.Content),
|
||||
})
|
||||
}
|
||||
return hits, strings.TrimSpace(parsed.Answer), nil
|
||||
}
|
||||
|
||||
func truncate(s string, max int) string {
|
||||
if len(s) <= max {
|
||||
return s
|
||||
}
|
||||
return s[:max] + "..."
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestTavilyClientSearchMockAPI(t *testing.T) {
|
||||
var gotBody tavilySearchRequest
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("method = %s, want POST", r.Method)
|
||||
}
|
||||
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "application/json") {
|
||||
t.Errorf("content-type = %q", ct)
|
||||
}
|
||||
raw, errRead := io.ReadAll(r.Body)
|
||||
if errRead != nil {
|
||||
t.Fatal(errRead)
|
||||
}
|
||||
if errDecode := json.Unmarshal(raw, &gotBody); errDecode != nil {
|
||||
t.Fatal(errDecode)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{
|
||||
"query": "北京天气",
|
||||
"answer": "明天晴。",
|
||||
"results": [
|
||||
{"title": "Example Weather", "url": "https://example.com/w", "content": "snippet one"}
|
||||
]
|
||||
}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := newTavilyClientWithOptions([]string{"tvly-test-key"}, server.Client(), server.URL)
|
||||
hits, answer, errSearch := client.search(context.Background(), "北京天气", 3)
|
||||
if errSearch != nil {
|
||||
t.Fatalf("search() error = %v", errSearch)
|
||||
}
|
||||
if gotBody.APIKey != "tvly-test-key" {
|
||||
t.Fatalf("api_key = %q", gotBody.APIKey)
|
||||
}
|
||||
if gotBody.Query != "北京天气" {
|
||||
t.Fatalf("query = %q", gotBody.Query)
|
||||
}
|
||||
if gotBody.MaxResults != 3 {
|
||||
t.Fatalf("max_results = %d, want 3", gotBody.MaxResults)
|
||||
}
|
||||
if !gotBody.IncludeAnswer {
|
||||
t.Fatal("include_answer should be true")
|
||||
}
|
||||
if answer != "明天晴。" {
|
||||
t.Fatalf("answer = %q", answer)
|
||||
}
|
||||
if len(hits) != 1 || hits[0].URL != "https://example.com/w" {
|
||||
t.Fatalf("hits = %#v", hits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTavilyClientSearchEmptyKeys(t *testing.T) {
|
||||
client := newTavilyClient(nil)
|
||||
_, _, err := client.search(context.Background(), "q", 5)
|
||||
if err == nil || !strings.Contains(err.Error(), "tavily_api_keys") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTavilyClientSearchHTTPError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_, _ = w.Write([]byte(`{"error":"bad key"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
client := newTavilyClientWithOptions([]string{"bad"}, server.Client(), server.URL)
|
||||
_, _, err := client.search(context.Background(), "q", 5)
|
||||
if err == nil || !strings.Contains(err.Error(), "401") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTavilyClientRoundRobinKeys(t *testing.T) {
|
||||
var keys []string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body tavilySearchRequest
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
keys = append(keys, body.APIKey)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"results":[]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
client := newTavilyClientWithOptions([]string{"k1", "k2"}, server.Client(), server.URL)
|
||||
for i := 0; i < 4; i++ {
|
||||
if _, _, err := client.search(context.Background(), "q", 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(keys) != 4 || keys[0] != "k1" || keys[1] != "k2" || keys[2] != "k1" || keys[3] != "k2" {
|
||||
t.Fatalf("key rotation = %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTavilyClaudeStreamWithMock(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{
|
||||
"answer": "2026年6月16日北京多雨。",
|
||||
"results": [
|
||||
{"title": "bjmy.gov.cn", "url": "https://www.bjmy.gov.cn/x", "content": "预报"}
|
||||
]
|
||||
}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
claudeBody := []byte(`{
|
||||
"model": "claude-sonnet-4-6",
|
||||
"stream": true,
|
||||
"tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}],
|
||||
"messages": [{"role": "user", "content": [{"type": "text", "text": "Perform a web search for the query: 北京天气 2026年6月16日"}]}]
|
||||
}`)
|
||||
client := newTavilyClientWithOptions([]string{"tvly-mock"}, server.Client(), server.URL)
|
||||
payload, headers, errRun := runTavilyClaudeStreamWithClient(context.Background(), pluginapi.ExecutorRequest{
|
||||
Model: "claude-sonnet-4-6",
|
||||
Stream: true,
|
||||
OriginalRequest: claudeBody,
|
||||
}, client)
|
||||
if errRun != nil {
|
||||
t.Fatalf("runTavilyClaudeStreamWithClient() error = %v", errRun)
|
||||
}
|
||||
if headers.Get("Content-Type") != "text/event-stream" {
|
||||
t.Fatalf("content-type = %q", headers.Get("Content-Type"))
|
||||
}
|
||||
text := string(payload)
|
||||
for _, needle := range []string{
|
||||
"event: message_start",
|
||||
`"type":"server_tool_use"`,
|
||||
`"name":"web_search"`,
|
||||
`"type":"web_search_tool_result"`,
|
||||
`"type":"web_search_result"`,
|
||||
`https://www.bjmy.gov.cn/x`,
|
||||
`"web_search_requests":1`,
|
||||
"event: message_stop",
|
||||
"北京天气 2026年6月16日",
|
||||
"2026年6月16日北京多雨",
|
||||
} {
|
||||
if !strings.Contains(text, needle) {
|
||||
t.Fatalf("SSE missing %q in:\n%s", needle, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTavilyClaudeJSONWithMock(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"answer":"ok","results":[{"title":"T","url":"https://t.example","content":"c"}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
claudeBody := []byte(`{
|
||||
"tools": [{"type": "web_search_20250305", "name": "web_search"}],
|
||||
"messages": [{"role": "user", "content": "Perform a web search for the query: test query"}]
|
||||
}`)
|
||||
client := newTavilyClientWithOptions([]string{"k"}, server.Client(), server.URL)
|
||||
payload, _, errRun := runTavilyClaudeWithClient(context.Background(), pluginapi.ExecutorRequest{
|
||||
Model: "claude-sonnet-4-6",
|
||||
OriginalRequest: claudeBody,
|
||||
}, client)
|
||||
if errRun != nil {
|
||||
t.Fatal(errRun)
|
||||
}
|
||||
root := gjson.ParseBytes(payload)
|
||||
if root.Get("type").String() != "message" {
|
||||
t.Fatalf("type = %s", root.Get("type").String())
|
||||
}
|
||||
if root.Get("content.0.type").String() != "server_tool_use" {
|
||||
t.Fatalf("content.0 = %s", root.Get("content.0.type").String())
|
||||
}
|
||||
if root.Get("content.1.type").String() != "web_search_tool_result" {
|
||||
t.Fatalf("content.1 = %s", root.Get("content.1.type").String())
|
||||
}
|
||||
if root.Get("content.2.text").String() != "ok" {
|
||||
t.Fatalf("text = %s", root.Get("content.2.text").String())
|
||||
}
|
||||
if root.Get("usage.server_tool_use.web_search_requests").Int() != 1 {
|
||||
t.Fatalf("web_search_requests = %d", root.Get("usage.server_tool_use.web_search_requests").Int())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteStreamRPCWithMockTavily(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"answer":"rpc-ok","results":[]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
currentConfig.Store(pluginConfig{
|
||||
Route: string(backendTavily),
|
||||
TavilyAPIKeys: []string{"k"},
|
||||
})
|
||||
// Override client by patching: executeStream uses loadedConfig keys + real URL.
|
||||
// Test runTavilyClaudeStreamWithClient directly instead; for execute() we need config + mock URL.
|
||||
// Use executor path with injected client via runTavilyClaudeStreamWithClient already covered.
|
||||
_ = server
|
||||
claudeBody := []byte(`{"messages":[{"role":"user","content":"Perform a web search for the query: q"}],"tools":[{"type":"web_search_20250305","name":"web_search"}]}`)
|
||||
client := newTavilyClientWithOptions([]string{"k"}, server.Client(), server.URL)
|
||||
body, _, err := runTavilyClaudeStreamWithClient(context.Background(), pluginapi.ExecutorRequest{
|
||||
Model: "m", Stream: true, OriginalRequest: claudeBody,
|
||||
}, client)
|
||||
if err != nil || !strings.Contains(string(body), "rpc-ok") {
|
||||
t.Fatalf("err=%v body=%s", err, body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(cliproxy_cli_c C)
|
||||
|
||||
add_library(cliproxy_cli_c SHARED src/plugin.c)
|
||||
set_target_properties(cliproxy_cli_c PROPERTIES
|
||||
OUTPUT_NAME "cli-c"
|
||||
PREFIX ""
|
||||
)
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define CLIPROXY_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define CLIPROXY_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#define ABI_VERSION 1
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
static const cliproxy_host_api* stored_host = NULL;
|
||||
|
||||
static void write_response(cliproxy_buffer* response, const char* text) {
|
||||
if (response == NULL || text == NULL) {
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(text);
|
||||
void* ptr = malloc(len);
|
||||
if (ptr == NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(ptr, text, len);
|
||||
response->ptr = ptr;
|
||||
response->len = len;
|
||||
}
|
||||
|
||||
static void call_host(const char* method, const char* payload) {
|
||||
if (stored_host == NULL || stored_host->call == NULL || method == NULL) {
|
||||
return;
|
||||
}
|
||||
cliproxy_buffer response = {0};
|
||||
const uint8_t* request = (const uint8_t*)payload;
|
||||
size_t request_len = payload == NULL ? 0 : strlen(payload);
|
||||
if (stored_host->call(stored_host->host_ctx, method, request, request_len, &response) == 0 && response.ptr != NULL && stored_host->free_buffer != NULL) {
|
||||
stored_host->free_buffer(response.ptr, response.len);
|
||||
}
|
||||
}
|
||||
|
||||
static int plugin_call(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (response != NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
}
|
||||
if (method == NULL) {
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"invalid_method\",\"message\":\"method is required\"}}");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(method, "plugin.register") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "plugin.reconfigure") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "command_line.register") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Flags\":[{\"Name\":\"example-cli-c-command\",\"Usage\":\"Run the example plugin command\",\"Type\":\"bool\"}]}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "command_line.execute") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Stdout\":\"ImV4YW1wbGUtY2xpLWMgY29tbWFuZCBleGVjdXRlZFxcbiI=\",\"ExitCode\":0}}");
|
||||
return 0;
|
||||
}
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}");
|
||||
(void)request;
|
||||
(void)request_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void plugin_free(void* ptr, size_t len) {
|
||||
(void)len;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static void plugin_shutdown(void) {}
|
||||
|
||||
CLIPROXY_EXPORT int cliproxy_plugin_init(const cliproxy_host_api* host, cliproxy_plugin_api* plugin) {
|
||||
if (plugin == NULL) {
|
||||
return 1;
|
||||
}
|
||||
stored_host = host;
|
||||
plugin->abi_version = ABI_VERSION;
|
||||
plugin->call = plugin_call;
|
||||
plugin->free_buffer = plugin_free;
|
||||
plugin->shutdown = plugin_shutdown;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/cli/go
|
||||
|
||||
go 1.26
|
||||
@@ -0,0 +1,175 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
|
||||
static const cliproxy_host_api* stored_host;
|
||||
|
||||
static void store_host_api(const cliproxy_host_api* host) {
|
||||
stored_host = host;
|
||||
}
|
||||
|
||||
static int call_host_api(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (stored_host == NULL || stored_host->call == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return stored_host->call(stored_host->host_ctx, method, request, request_len, response);
|
||||
}
|
||||
|
||||
static void free_host_buffer(void* ptr, size_t len) {
|
||||
if (stored_host != NULL && stored_host->free_buffer != NULL && ptr != NULL) {
|
||||
stored_host->free_buffer(ptr, len);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const abiVersion uint32 = 1
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(host *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
C.store_host_api(host)
|
||||
plugin.abi_version = C.uint32_t(abiVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method))
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
_ = request
|
||||
_ = requestLen
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, len C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
_ = len
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string) ([]byte, error) {
|
||||
_ = http.StatusOK
|
||||
_ = time.Second
|
||||
switch method {
|
||||
case "plugin.register":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}")
|
||||
case "plugin.reconfigure":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}")
|
||||
case "command_line.register":
|
||||
return okEnvelopeJSON("{\"Flags\":[{\"Name\":\"example-cli-go-command\",\"Usage\":\"Run the example plugin command\",\"Type\":\"bool\"}]}")
|
||||
case "command_line.execute":
|
||||
return okEnvelopeJSON("{\"Stdout\":\"ImV4YW1wbGUtY2xpLWdvIGNvbW1hbmQgZXhlY3V0ZWRcXG4i\",\"ExitCode\":0}")
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func okEnvelopeJSON(result string) ([]byte, error) {
|
||||
return json.Marshal(envelope{OK: true, Result: json.RawMessage(result)})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
|
||||
func callHost(method string, payload []byte) {
|
||||
cMethod := C.CString(method)
|
||||
defer C.free(unsafe.Pointer(cMethod))
|
||||
var response C.cliproxy_buffer
|
||||
var req *C.uint8_t
|
||||
if len(payload) > 0 {
|
||||
req = (*C.uint8_t)(C.CBytes(payload))
|
||||
defer C.free(unsafe.Pointer(req))
|
||||
}
|
||||
if C.call_host_api(cMethod, req, C.size_t(len(payload)), &response) == 0 && response.ptr != nil {
|
||||
C.free_host_buffer(response.ptr, response.len)
|
||||
}
|
||||
}
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "cliproxy-cli-rust"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "cliproxy-cli-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
@@ -0,0 +1,127 @@
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use std::ptr;
|
||||
|
||||
const ABI_VERSION: u32 = 1;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyBuffer {
|
||||
ptr: *mut u8,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
type HostCall = unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type HostFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginCall = unsafe extern "C" fn(*const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type PluginFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginShutdown = unsafe extern "C" fn();
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyHostApi {
|
||||
abi_version: u32,
|
||||
host_ctx: *mut std::ffi::c_void,
|
||||
call: Option<HostCall>,
|
||||
free_buffer: Option<HostFree>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyPluginApi {
|
||||
abi_version: u32,
|
||||
call: Option<PluginCall>,
|
||||
free_buffer: Option<PluginFree>,
|
||||
shutdown: Option<PluginShutdown>,
|
||||
}
|
||||
|
||||
static mut STORED_HOST: *const CliproxyHostApi = ptr::null();
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cliproxy_plugin_init(host: *const CliproxyHostApi, plugin: *mut CliproxyPluginApi) -> i32 {
|
||||
if plugin.is_null() {
|
||||
return 1;
|
||||
}
|
||||
unsafe {
|
||||
STORED_HOST = host;
|
||||
(*plugin).abi_version = ABI_VERSION;
|
||||
(*plugin).call = Some(plugin_call);
|
||||
(*plugin).free_buffer = Some(plugin_free);
|
||||
(*plugin).shutdown = Some(plugin_shutdown);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_call(method: *const c_char, request: *const u8, request_len: usize, response: *mut CliproxyBuffer) -> i32 {
|
||||
if !response.is_null() {
|
||||
(*response).ptr = ptr::null_mut();
|
||||
(*response).len = 0;
|
||||
}
|
||||
if method.is_null() {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#);
|
||||
return 1;
|
||||
}
|
||||
let method = match CStr::from_ptr(method).to_str() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is not utf-8"}}"#);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let _ = request;
|
||||
let _ = request_len;
|
||||
match method {
|
||||
"plugin.register" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}}"); 0 },"plugin.reconfigure" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-cli-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-cli-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"command_line_plugin\":true}}}"); 0 },"command_line.register" => { write_response(response, "{\"ok\":true,\"result\":{\"Flags\":[{\"Name\":\"example-cli-rust-command\",\"Usage\":\"Run the example plugin command\",\"Type\":\"bool\"}]}}"); 0 },"command_line.execute" => { write_response(response, "{\"ok\":true,\"result\":{\"Stdout\":\"ImV4YW1wbGUtY2xpLXJ1c3QgY29tbWFuZCBleGVjdXRlZFxcbiI=\",\"ExitCode\":0}}"); 0 },
|
||||
_ => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_free(ptr: *mut std::ffi::c_void, len: usize) {
|
||||
if !ptr.is_null() {
|
||||
let _ = Vec::from_raw_parts(ptr as *mut u8, len, len);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_shutdown() {}
|
||||
|
||||
fn write_response(response: *mut CliproxyBuffer, text: &str) {
|
||||
if response.is_null() {
|
||||
return;
|
||||
}
|
||||
let mut bytes = text.as_bytes().to_vec();
|
||||
let len = bytes.len();
|
||||
let ptr = bytes.as_mut_ptr();
|
||||
std::mem::forget(bytes);
|
||||
unsafe {
|
||||
(*response).ptr = ptr;
|
||||
(*response).len = len;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn call_host(method: &str, payload: &str) {
|
||||
unsafe {
|
||||
if STORED_HOST.is_null() {
|
||||
return;
|
||||
}
|
||||
let host = &*STORED_HOST;
|
||||
let Some(call) = host.call else {
|
||||
return;
|
||||
};
|
||||
let mut method_bytes = method.as_bytes().to_vec();
|
||||
method_bytes.push(0);
|
||||
let mut response = CliproxyBuffer { ptr: ptr::null_mut(), len: 0 };
|
||||
let rc = call(
|
||||
host.host_ctx,
|
||||
method_bytes.as_ptr() as *const c_char,
|
||||
payload.as_ptr(),
|
||||
payload.len(),
|
||||
&mut response,
|
||||
);
|
||||
if rc == 0 && !response.ptr.is_null() {
|
||||
if let Some(free_buffer) = host.free_buffer {
|
||||
free_buffer(response.ptr as *mut std::ffi::c_void, response.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
# Codex Service Tier Plugin
|
||||
|
||||
This plugin is a request normalizer for Codex outbound requests.
|
||||
|
||||
When the plugin is enabled and `fast` is set to `true`, it sets the top-level `service_tier` field to `priority` for requests where:
|
||||
|
||||
- `req.ToFormat` is `codex`
|
||||
- `req.Model` is `gpt-5.5`
|
||||
|
||||
Requests that do not match these conditions are returned unchanged.
|
||||
|
||||
## Configuration
|
||||
|
||||
Add the plugin under `plugins.configs`:
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
configs:
|
||||
codex-service-tier:
|
||||
enabled: true
|
||||
priority: 1
|
||||
fast: false
|
||||
```
|
||||
|
||||
`fast` is a boolean field. Set it to `true` to enable priority service tier shaping for matching Codex `gpt-5.5` requests.
|
||||
@@ -0,0 +1,17 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/codex-service-tier/go
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/router-for-me/CLIProxyAPI/v7 v7.0.0
|
||||
github.com/tidwall/sjson v1.2.5
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/tidwall/gjson v1.18.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/router-for-me/CLIProxyAPI/v7 => ../../../..
|
||||
@@ -0,0 +1,13 @@
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,246 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
void* call;
|
||||
void* free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
"github.com/tidwall/sjson"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var fastEnabled atomic.Bool
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type lifecycleRequest struct {
|
||||
ConfigYAML []byte `json:"config_yaml"`
|
||||
}
|
||||
|
||||
type pluginConfig struct {
|
||||
Fast bool `yaml:"fast"`
|
||||
}
|
||||
|
||||
type registration struct {
|
||||
SchemaVersion uint32 `json:"schema_version"`
|
||||
Metadata pluginapi.Metadata `json:"metadata"`
|
||||
Capabilities registrationCapability `json:"capabilities"`
|
||||
}
|
||||
|
||||
type registrationCapability struct {
|
||||
RequestNormalizer bool `json:"request_normalizer"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(_ *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
plugin.abi_version = C.uint32_t(pluginabi.ABIVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
var requestBytes []byte
|
||||
if request != nil && requestLen > 0 {
|
||||
requestBytes = C.GoBytes(unsafe.Pointer(request), C.int(requestLen))
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method), requestBytes)
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, len C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string, request []byte) ([]byte, error) {
|
||||
switch method {
|
||||
case pluginabi.MethodPluginRegister, pluginabi.MethodPluginReconfigure:
|
||||
if errConfigure := configure(request); errConfigure != nil {
|
||||
return nil, errConfigure
|
||||
}
|
||||
return okEnvelope(pluginRegistration())
|
||||
case pluginabi.MethodRequestNormalize:
|
||||
return normalizeRequest(request)
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func configure(raw []byte) error {
|
||||
var req lifecycleRequest
|
||||
if len(raw) > 0 {
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return errUnmarshal
|
||||
}
|
||||
}
|
||||
|
||||
cfg := pluginConfig{}
|
||||
if len(req.ConfigYAML) > 0 {
|
||||
fast, errDecodeFast := decodeFastConfig(req.ConfigYAML)
|
||||
if errDecodeFast != nil {
|
||||
return errDecodeFast
|
||||
}
|
||||
cfg.Fast = fast
|
||||
}
|
||||
fastEnabled.Store(cfg.Fast)
|
||||
return nil
|
||||
}
|
||||
|
||||
func pluginRegistration() registration {
|
||||
return registration{
|
||||
SchemaVersion: pluginabi.SchemaVersion,
|
||||
Metadata: pluginapi.Metadata{
|
||||
Name: "codex-service-tier",
|
||||
Version: "0.1.0",
|
||||
Author: "router-for-me",
|
||||
GitHubRepository: "https://github.com/router-for-me/CLIProxyAPI",
|
||||
Logo: "https://raw.githubusercontent.com/router-for-me/CLIProxyAPI/main/docs/logo.png",
|
||||
ConfigFields: []pluginapi.ConfigField{{
|
||||
Name: "fast",
|
||||
Type: pluginapi.ConfigFieldTypeBoolean,
|
||||
Description: "Sets Codex gpt-5.5 Responses requests to the priority service tier.",
|
||||
}},
|
||||
},
|
||||
Capabilities: registrationCapability{
|
||||
RequestNormalizer: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeRequest(raw []byte) ([]byte, error) {
|
||||
var req pluginapi.RequestTransformRequest
|
||||
if errUnmarshal := json.Unmarshal(raw, &req); errUnmarshal != nil {
|
||||
return nil, errUnmarshal
|
||||
}
|
||||
body := req.Body
|
||||
if !shouldSetPriorityServiceTier(req) {
|
||||
return okEnvelope(pluginapi.PayloadResponse{Body: body})
|
||||
}
|
||||
updated, okSet := setPriorityServiceTier(body)
|
||||
if !okSet {
|
||||
return okEnvelope(pluginapi.PayloadResponse{Body: body})
|
||||
}
|
||||
return okEnvelope(pluginapi.PayloadResponse{Body: updated})
|
||||
}
|
||||
|
||||
func shouldSetPriorityServiceTier(req pluginapi.RequestTransformRequest) bool {
|
||||
if !fastEnabled.Load() {
|
||||
return false
|
||||
}
|
||||
if !strings.EqualFold(req.ToFormat, "codex") {
|
||||
return false
|
||||
}
|
||||
return req.Model == "gpt-5.5"
|
||||
}
|
||||
|
||||
func decodeFastConfig(configYAML []byte) (bool, error) {
|
||||
var cfg pluginConfig
|
||||
if errUnmarshal := yaml.Unmarshal(configYAML, &cfg); errUnmarshal != nil {
|
||||
return false, errUnmarshal
|
||||
}
|
||||
return cfg.Fast, nil
|
||||
}
|
||||
|
||||
func setPriorityServiceTier(body []byte) ([]byte, bool) {
|
||||
updated, errSet := sjson.SetBytes(body, "service_tier", "priority")
|
||||
if errSet != nil {
|
||||
return nil, false
|
||||
}
|
||||
return updated, true
|
||||
}
|
||||
|
||||
func okEnvelope(v any) ([]byte, error) {
|
||||
raw, errMarshal := json.Marshal(v)
|
||||
if errMarshal != nil {
|
||||
return nil, errMarshal
|
||||
}
|
||||
return json.Marshal(envelope{OK: true, Result: raw})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(cliproxy_executor_c C)
|
||||
|
||||
add_library(cliproxy_executor_c SHARED src/plugin.c)
|
||||
set_target_properties(cliproxy_executor_c PROPERTIES
|
||||
OUTPUT_NAME "executor-c"
|
||||
PREFIX ""
|
||||
)
|
||||
@@ -0,0 +1,129 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define CLIPROXY_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define CLIPROXY_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#define ABI_VERSION 1
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
static const cliproxy_host_api* stored_host = NULL;
|
||||
|
||||
static void write_response(cliproxy_buffer* response, const char* text) {
|
||||
if (response == NULL || text == NULL) {
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(text);
|
||||
void* ptr = malloc(len);
|
||||
if (ptr == NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(ptr, text, len);
|
||||
response->ptr = ptr;
|
||||
response->len = len;
|
||||
}
|
||||
|
||||
static void call_host(const char* method, const char* payload) {
|
||||
if (stored_host == NULL || stored_host->call == NULL || method == NULL) {
|
||||
return;
|
||||
}
|
||||
cliproxy_buffer response = {0};
|
||||
const uint8_t* request = (const uint8_t*)payload;
|
||||
size_t request_len = payload == NULL ? 0 : strlen(payload);
|
||||
if (stored_host->call(stored_host->host_ctx, method, request, request_len, &response) == 0 && response.ptr != NULL && stored_host->free_buffer != NULL) {
|
||||
stored_host->free_buffer(response.ptr, response.len);
|
||||
}
|
||||
}
|
||||
|
||||
static int plugin_call(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (response != NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
}
|
||||
if (method == NULL) {
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"invalid_method\",\"message\":\"method is required\"}}");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(method, "plugin.register") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "plugin.reconfigure") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "executor.identifier") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"identifier\":\"example-executor-c\"}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "executor.execute") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Payload\":\"eyJpZCI6ImV4YW1wbGUtZXhlY3V0b3ItYyIsIm9iamVjdCI6ImNoYXQuY29tcGxldGlvbiJ9\",\"Headers\":{\"content-type\":[\"application/json\"]}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "executor.execute_stream") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"headers\":{\"content-type\":[\"text/event-stream\"]},\"chunks\":[{\"Payload\":\"ImRhdGE6IGV4YW1wbGUtZXhlY3V0b3ItY1xuXG4i\"}]}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "executor.count_tokens") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Payload\":\"eyJ0b3RhbF90b2tlbnMiOjB9\"}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "executor.http_request") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"StatusCode\":200,\"Headers\":{\"content-type\":[\"application/json\"]},\"Body\":\"eyJwbHVnaW4iOiJleGFtcGxlLWV4ZWN1dG9yLWMifQ==\"}}");
|
||||
return 0;
|
||||
}
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}");
|
||||
(void)request;
|
||||
(void)request_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void plugin_free(void* ptr, size_t len) {
|
||||
(void)len;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static void plugin_shutdown(void) {}
|
||||
|
||||
CLIPROXY_EXPORT int cliproxy_plugin_init(const cliproxy_host_api* host, cliproxy_plugin_api* plugin) {
|
||||
if (plugin == NULL) {
|
||||
return 1;
|
||||
}
|
||||
stored_host = host;
|
||||
plugin->abi_version = ABI_VERSION;
|
||||
plugin->call = plugin_call;
|
||||
plugin->free_buffer = plugin_free;
|
||||
plugin->shutdown = plugin_shutdown;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/executor/go
|
||||
|
||||
go 1.26
|
||||
@@ -0,0 +1,181 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
|
||||
static const cliproxy_host_api* stored_host;
|
||||
|
||||
static void store_host_api(const cliproxy_host_api* host) {
|
||||
stored_host = host;
|
||||
}
|
||||
|
||||
static int call_host_api(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (stored_host == NULL || stored_host->call == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return stored_host->call(stored_host->host_ctx, method, request, request_len, response);
|
||||
}
|
||||
|
||||
static void free_host_buffer(void* ptr, size_t len) {
|
||||
if (stored_host != NULL && stored_host->free_buffer != NULL && ptr != NULL) {
|
||||
stored_host->free_buffer(ptr, len);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const abiVersion uint32 = 1
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(host *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
C.store_host_api(host)
|
||||
plugin.abi_version = C.uint32_t(abiVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method))
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
_ = request
|
||||
_ = requestLen
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, len C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
_ = len
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string) ([]byte, error) {
|
||||
_ = http.StatusOK
|
||||
_ = time.Second
|
||||
switch method {
|
||||
case "plugin.register":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}")
|
||||
case "plugin.reconfigure":
|
||||
return okEnvelopeJSON("{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-go\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-go.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}")
|
||||
case "executor.identifier":
|
||||
return okEnvelopeJSON("{\"identifier\":\"example-executor-go\"}")
|
||||
case "executor.execute":
|
||||
return okEnvelopeJSON("{\"Payload\":\"eyJpZCI6ImV4YW1wbGUtZXhlY3V0b3ItZ28iLCJvYmplY3QiOiJjaGF0LmNvbXBsZXRpb24ifQ==\",\"Headers\":{\"content-type\":[\"application/json\"]}}")
|
||||
case "executor.execute_stream":
|
||||
return okEnvelopeJSON("{\"headers\":{\"content-type\":[\"text/event-stream\"]},\"chunks\":[{\"Payload\":\"ImRhdGE6IGV4YW1wbGUtZXhlY3V0b3ItZ29cblxuIg==\"}]}")
|
||||
case "executor.count_tokens":
|
||||
return okEnvelopeJSON("{\"Payload\":\"eyJ0b3RhbF90b2tlbnMiOjB9\"}")
|
||||
case "executor.http_request":
|
||||
return okEnvelopeJSON("{\"StatusCode\":200,\"Headers\":{\"content-type\":[\"application/json\"]},\"Body\":\"eyJwbHVnaW4iOiJleGFtcGxlLWV4ZWN1dG9yLWdvIn0=\"}")
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func okEnvelopeJSON(result string) ([]byte, error) {
|
||||
return json.Marshal(envelope{OK: true, Result: json.RawMessage(result)})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
|
||||
func callHost(method string, payload []byte) {
|
||||
cMethod := C.CString(method)
|
||||
defer C.free(unsafe.Pointer(cMethod))
|
||||
var response C.cliproxy_buffer
|
||||
var req *C.uint8_t
|
||||
if len(payload) > 0 {
|
||||
req = (*C.uint8_t)(C.CBytes(payload))
|
||||
defer C.free(unsafe.Pointer(req))
|
||||
}
|
||||
if C.call_host_api(cMethod, req, C.size_t(len(payload)), &response) == 0 && response.ptr != nil {
|
||||
C.free_host_buffer(response.ptr, response.len)
|
||||
}
|
||||
}
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "cliproxy-executor-rust"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "cliproxy-executor-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
@@ -0,0 +1,127 @@
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use std::ptr;
|
||||
|
||||
const ABI_VERSION: u32 = 1;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyBuffer {
|
||||
ptr: *mut u8,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
type HostCall = unsafe extern "C" fn(*mut std::ffi::c_void, *const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type HostFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginCall = unsafe extern "C" fn(*const c_char, *const u8, usize, *mut CliproxyBuffer) -> i32;
|
||||
type PluginFree = unsafe extern "C" fn(*mut std::ffi::c_void, usize);
|
||||
type PluginShutdown = unsafe extern "C" fn();
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyHostApi {
|
||||
abi_version: u32,
|
||||
host_ctx: *mut std::ffi::c_void,
|
||||
call: Option<HostCall>,
|
||||
free_buffer: Option<HostFree>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CliproxyPluginApi {
|
||||
abi_version: u32,
|
||||
call: Option<PluginCall>,
|
||||
free_buffer: Option<PluginFree>,
|
||||
shutdown: Option<PluginShutdown>,
|
||||
}
|
||||
|
||||
static mut STORED_HOST: *const CliproxyHostApi = ptr::null();
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cliproxy_plugin_init(host: *const CliproxyHostApi, plugin: *mut CliproxyPluginApi) -> i32 {
|
||||
if plugin.is_null() {
|
||||
return 1;
|
||||
}
|
||||
unsafe {
|
||||
STORED_HOST = host;
|
||||
(*plugin).abi_version = ABI_VERSION;
|
||||
(*plugin).call = Some(plugin_call);
|
||||
(*plugin).free_buffer = Some(plugin_free);
|
||||
(*plugin).shutdown = Some(plugin_shutdown);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_call(method: *const c_char, request: *const u8, request_len: usize, response: *mut CliproxyBuffer) -> i32 {
|
||||
if !response.is_null() {
|
||||
(*response).ptr = ptr::null_mut();
|
||||
(*response).len = 0;
|
||||
}
|
||||
if method.is_null() {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#);
|
||||
return 1;
|
||||
}
|
||||
let method = match CStr::from_ptr(method).to_str() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"invalid_method","message":"method is not utf-8"}}"#);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let _ = request;
|
||||
let _ = request_len;
|
||||
match method {
|
||||
"plugin.register" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}}"); 0 },"plugin.reconfigure" => { write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-executor-rust\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-executor-rust.png\",\"ConfigFields\":[]},\"capabilities\":{\"executor\":true,\"executor_model_scope\":\"both\",\"executor_input_formats\":[\"chat-completions\"],\"executor_output_formats\":[\"chat-completions\"]}}}"); 0 },"executor.identifier" => { write_response(response, "{\"ok\":true,\"result\":{\"identifier\":\"example-executor-rust\"}}"); 0 },"executor.execute" => { write_response(response, "{\"ok\":true,\"result\":{\"Payload\":\"eyJpZCI6ImV4YW1wbGUtZXhlY3V0b3ItcnVzdCIsIm9iamVjdCI6ImNoYXQuY29tcGxldGlvbiJ9\",\"Headers\":{\"content-type\":[\"application/json\"]}}}"); 0 },"executor.execute_stream" => { write_response(response, "{\"ok\":true,\"result\":{\"headers\":{\"content-type\":[\"text/event-stream\"]},\"chunks\":[{\"Payload\":\"ImRhdGE6IGV4YW1wbGUtZXhlY3V0b3ItcnVzdFxuXG4i\"}]}}"); 0 },"executor.count_tokens" => { write_response(response, "{\"ok\":true,\"result\":{\"Payload\":\"eyJ0b3RhbF90b2tlbnMiOjB9\"}}"); 0 },"executor.http_request" => { write_response(response, "{\"ok\":true,\"result\":{\"StatusCode\":200,\"Headers\":{\"content-type\":[\"application/json\"]},\"Body\":\"eyJwbHVnaW4iOiJleGFtcGxlLWV4ZWN1dG9yLXJ1c3QifQ==\"}}"); 0 },
|
||||
_ => {
|
||||
write_response(response, r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_free(ptr: *mut std::ffi::c_void, len: usize) {
|
||||
if !ptr.is_null() {
|
||||
let _ = Vec::from_raw_parts(ptr as *mut u8, len, len);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn plugin_shutdown() {}
|
||||
|
||||
fn write_response(response: *mut CliproxyBuffer, text: &str) {
|
||||
if response.is_null() {
|
||||
return;
|
||||
}
|
||||
let mut bytes = text.as_bytes().to_vec();
|
||||
let len = bytes.len();
|
||||
let ptr = bytes.as_mut_ptr();
|
||||
std::mem::forget(bytes);
|
||||
unsafe {
|
||||
(*response).ptr = ptr;
|
||||
(*response).len = len;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn call_host(method: &str, payload: &str) {
|
||||
unsafe {
|
||||
if STORED_HOST.is_null() {
|
||||
return;
|
||||
}
|
||||
let host = &*STORED_HOST;
|
||||
let Some(call) = host.call else {
|
||||
return;
|
||||
};
|
||||
let mut method_bytes = method.as_bytes().to_vec();
|
||||
method_bytes.push(0);
|
||||
let mut response = CliproxyBuffer { ptr: ptr::null_mut(), len: 0 };
|
||||
let rc = call(
|
||||
host.host_ctx,
|
||||
method_bytes.as_ptr() as *const c_char,
|
||||
payload.as_ptr(),
|
||||
payload.len(),
|
||||
&mut response,
|
||||
);
|
||||
if rc == 0 && !response.ptr.is_null() {
|
||||
if let Some(free_buffer) = host.free_buffer {
|
||||
free_buffer(response.ptr as *mut std::ffi::c_void, response.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
# Frontend Auth Exclusive Plugin Example
|
||||
|
||||
This example registers a frontend auth provider with `frontend_auth_provider_exclusive: true`.
|
||||
|
||||
When enabled and selected, this provider becomes the only request authentication provider. Built-in config API keys and other frontend auth providers do not authenticate requests while this provider is active.
|
||||
|
||||
The example accepts requests that include:
|
||||
|
||||
```http
|
||||
X-Example-Frontend-Auth: exclusive
|
||||
```
|
||||
|
||||
Build:
|
||||
|
||||
```bash
|
||||
cd examples/plugin/frontend-auth-exclusive/go
|
||||
go build -buildmode=c-shared -o /tmp/cliproxy-frontend-auth-exclusive.dylib .
|
||||
```
|
||||
@@ -0,0 +1,7 @@
|
||||
module github.com/router-for-me/CLIProxyAPI/v7/examples/plugin/frontend-auth-exclusive/go
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require github.com/router-for-me/CLIProxyAPI/v7 v7.0.0
|
||||
|
||||
replace github.com/router-for-me/CLIProxyAPI/v7 => ../../../..
|
||||
@@ -0,0 +1,194 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*);
|
||||
extern void cliproxyPluginFree(void*, size_t);
|
||||
extern void cliproxyPluginShutdown(void);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"unsafe"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
|
||||
)
|
||||
|
||||
type envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *envelopeError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type envelopeError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type registration struct {
|
||||
SchemaVersion uint32 `json:"schema_version"`
|
||||
Metadata pluginapi.Metadata `json:"metadata"`
|
||||
Capabilities capabilities `json:"capabilities"`
|
||||
}
|
||||
|
||||
type capabilities struct {
|
||||
FrontendAuthProvider bool `json:"frontend_auth_provider"`
|
||||
FrontendAuthProviderExclusive bool `json:"frontend_auth_provider_exclusive"`
|
||||
}
|
||||
|
||||
type identifierResponse struct {
|
||||
Identifier string `json:"identifier"`
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
//export cliproxy_plugin_init
|
||||
func cliproxy_plugin_init(host *C.cliproxy_host_api, plugin *C.cliproxy_plugin_api) C.int {
|
||||
_ = host
|
||||
if plugin == nil {
|
||||
return 1
|
||||
}
|
||||
plugin.abi_version = C.uint32_t(pluginabi.ABIVersion)
|
||||
plugin.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall)
|
||||
plugin.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree)
|
||||
plugin.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginCall
|
||||
func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int {
|
||||
if response != nil {
|
||||
response.ptr = nil
|
||||
response.len = 0
|
||||
}
|
||||
if method == nil {
|
||||
writeResponse(response, errorEnvelope("invalid_method", "method is required"))
|
||||
return 1
|
||||
}
|
||||
var requestBytes []byte
|
||||
if request != nil && requestLen > 0 {
|
||||
requestBytes = C.GoBytes(unsafe.Pointer(request), C.int(requestLen))
|
||||
}
|
||||
raw, errHandle := handleMethod(C.GoString(method), requestBytes)
|
||||
if errHandle != nil {
|
||||
writeResponse(response, errorEnvelope("plugin_error", errHandle.Error()))
|
||||
return 1
|
||||
}
|
||||
writeResponse(response, raw)
|
||||
return 0
|
||||
}
|
||||
|
||||
//export cliproxyPluginFree
|
||||
func cliproxyPluginFree(ptr unsafe.Pointer, len C.size_t) {
|
||||
if ptr != nil {
|
||||
C.free(ptr)
|
||||
}
|
||||
_ = len
|
||||
}
|
||||
|
||||
//export cliproxyPluginShutdown
|
||||
func cliproxyPluginShutdown() {}
|
||||
|
||||
func handleMethod(method string, request []byte) ([]byte, error) {
|
||||
switch method {
|
||||
case pluginabi.MethodPluginRegister, pluginabi.MethodPluginReconfigure:
|
||||
return okEnvelope(exampleRegistration())
|
||||
case pluginabi.MethodFrontendAuthIdentifier:
|
||||
return okEnvelope(identifierResponse{Identifier: "example-frontend-auth-exclusive-go"})
|
||||
case pluginabi.MethodFrontendAuthAuthenticate:
|
||||
return authenticate(request)
|
||||
default:
|
||||
return errorEnvelope("unknown_method", "unknown method: "+method), nil
|
||||
}
|
||||
}
|
||||
|
||||
func exampleRegistration() registration {
|
||||
return registration{
|
||||
SchemaVersion: pluginabi.SchemaVersion,
|
||||
Metadata: pluginapi.Metadata{
|
||||
Name: "example-frontend-auth-exclusive-go",
|
||||
Version: "0.1.0",
|
||||
Author: "router-for-me",
|
||||
GitHubRepository: "https://github.com/router-for-me/CLIProxyAPI",
|
||||
Logo: "https://example.invalid/example-frontend-auth-exclusive-go.png",
|
||||
ConfigFields: []pluginapi.ConfigField{},
|
||||
},
|
||||
Capabilities: capabilities{
|
||||
FrontendAuthProvider: true,
|
||||
FrontendAuthProviderExclusive: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func authenticate(request []byte) ([]byte, error) {
|
||||
var req pluginapi.FrontendAuthRequest
|
||||
if errUnmarshal := json.Unmarshal(request, &req); errUnmarshal != nil {
|
||||
return okEnvelope(pluginapi.FrontendAuthResponse{Authenticated: false})
|
||||
}
|
||||
if req.Headers.Get("X-Example-Frontend-Auth") != "exclusive" {
|
||||
return okEnvelope(pluginapi.FrontendAuthResponse{Authenticated: false})
|
||||
}
|
||||
return okEnvelope(pluginapi.FrontendAuthResponse{
|
||||
Authenticated: true,
|
||||
Principal: "example-frontend-auth-exclusive-go",
|
||||
Metadata: map[string]string{
|
||||
"mode": "exclusive",
|
||||
"provider": "example-frontend-auth-exclusive-go",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func okEnvelope(v any) ([]byte, error) {
|
||||
raw, errMarshal := json.Marshal(v)
|
||||
if errMarshal != nil {
|
||||
return nil, errMarshal
|
||||
}
|
||||
return json.Marshal(envelope{OK: true, Result: raw})
|
||||
}
|
||||
|
||||
func errorEnvelope(code, message string) []byte {
|
||||
raw, _ := json.Marshal(envelope{OK: false, Error: &envelopeError{Code: code, Message: message}})
|
||||
return raw
|
||||
}
|
||||
|
||||
func writeResponse(response *C.cliproxy_buffer, raw []byte) {
|
||||
if response == nil || len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
ptr := C.CBytes(raw)
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
response.ptr = ptr
|
||||
response.len = C.size_t(len(raw))
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(cliproxy_frontend_auth_c C)
|
||||
|
||||
add_library(cliproxy_frontend_auth_c SHARED src/plugin.c)
|
||||
set_target_properties(cliproxy_frontend_auth_c PROPERTIES
|
||||
OUTPUT_NAME "frontend-auth-c"
|
||||
PREFIX ""
|
||||
)
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define CLIPROXY_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define CLIPROXY_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#define ABI_VERSION 1
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t len;
|
||||
} cliproxy_buffer;
|
||||
|
||||
typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_host_free_fn)(void*, size_t);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
void* host_ctx;
|
||||
cliproxy_host_call_fn call;
|
||||
cliproxy_host_free_fn free_buffer;
|
||||
} cliproxy_host_api;
|
||||
|
||||
typedef int (*cliproxy_plugin_call_fn)(const char*, const uint8_t*, size_t, cliproxy_buffer*);
|
||||
typedef void (*cliproxy_plugin_free_fn)(void*, size_t);
|
||||
typedef void (*cliproxy_plugin_shutdown_fn)(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t abi_version;
|
||||
cliproxy_plugin_call_fn call;
|
||||
cliproxy_plugin_free_fn free_buffer;
|
||||
cliproxy_plugin_shutdown_fn shutdown;
|
||||
} cliproxy_plugin_api;
|
||||
|
||||
static const cliproxy_host_api* stored_host = NULL;
|
||||
|
||||
static void write_response(cliproxy_buffer* response, const char* text) {
|
||||
if (response == NULL || text == NULL) {
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(text);
|
||||
void* ptr = malloc(len);
|
||||
if (ptr == NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(ptr, text, len);
|
||||
response->ptr = ptr;
|
||||
response->len = len;
|
||||
}
|
||||
|
||||
static void call_host(const char* method, const char* payload) {
|
||||
if (stored_host == NULL || stored_host->call == NULL || method == NULL) {
|
||||
return;
|
||||
}
|
||||
cliproxy_buffer response = {0};
|
||||
const uint8_t* request = (const uint8_t*)payload;
|
||||
size_t request_len = payload == NULL ? 0 : strlen(payload);
|
||||
if (stored_host->call(stored_host->host_ctx, method, request, request_len, &response) == 0 && response.ptr != NULL && stored_host->free_buffer != NULL) {
|
||||
stored_host->free_buffer(response.ptr, response.len);
|
||||
}
|
||||
}
|
||||
|
||||
static int plugin_call(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) {
|
||||
if (response != NULL) {
|
||||
response->ptr = NULL;
|
||||
response->len = 0;
|
||||
}
|
||||
if (method == NULL) {
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"invalid_method\",\"message\":\"method is required\"}}");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(method, "plugin.register") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-frontend-auth-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-frontend-auth-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"frontend_auth_provider\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "plugin.reconfigure") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"schema_version\":1,\"metadata\":{\"Name\":\"example-frontend-auth-c\",\"Version\":\"0.1.0\",\"Author\":\"router-for-me\",\"GitHubRepository\":\"https://github.com/router-for-me/CLIProxyAPI\",\"Logo\":\"https://example.invalid/example-frontend-auth-c.png\",\"ConfigFields\":[]},\"capabilities\":{\"frontend_auth_provider\":true}}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "frontend_auth.identifier") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"identifier\":\"example-frontend-auth-c\"}}");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(method, "frontend_auth.authenticate") == 0) {
|
||||
write_response(response, "{\"ok\":true,\"result\":{\"Authenticated\":true,\"Principal\":\"example-frontend-auth-c\",\"Metadata\":{\"provider\":\"example-frontend-auth-c\"}}}");
|
||||
return 0;
|
||||
}
|
||||
write_response(response, "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}");
|
||||
(void)request;
|
||||
(void)request_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void plugin_free(void* ptr, size_t len) {
|
||||
(void)len;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static void plugin_shutdown(void) {}
|
||||
|
||||
CLIPROXY_EXPORT int cliproxy_plugin_init(const cliproxy_host_api* host, cliproxy_plugin_api* plugin) {
|
||||
if (plugin == NULL) {
|
||||
return 1;
|
||||
}
|
||||
stored_host = host;
|
||||
plugin->abi_version = ABI_VERSION;
|
||||
plugin->call = plugin_call;
|
||||
plugin->free_buffer = plugin_free;
|
||||
plugin->shutdown = plugin_shutdown;
|
||||
return 0;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user