bingocode 1.0.37 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -29
- package/bin/bingo-win.cjs +58 -26
- package/package.json +1 -1
- package/src/entrypoints/manager.tsx +10 -10
- package/src/manager/CliMenuManager.tsx +11 -6
- package/src/server/cli/providersMenu.tsx +3 -3
- package/src/server/proxy/handler.ts +94 -72
package/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025-2026 Leanchy
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
BingoCode is an independent open-source project by Leanchy, built upon and
|
|
26
|
-
substantially modified from Claude Code (originally developed by Anthropic, PBC).
|
|
27
|
-
Original upstream components remain the property of their respective copyright holders.
|
|
28
|
-
All modifications, extensions, and original contributions in this repository are
|
|
29
|
-
copyright Leanchy, released under the MIT License above.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Leanchy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
BingoCode is an independent open-source project by Leanchy, built upon and
|
|
26
|
+
substantially modified from Claude Code (originally developed by Anthropic, PBC).
|
|
27
|
+
Original upstream components remain the property of their respective copyright holders.
|
|
28
|
+
All modifications, extensions, and original contributions in this repository are
|
|
29
|
+
copyright Leanchy, released under the MIT License above.
|
package/bin/bingo-win.cjs
CHANGED
|
@@ -8,17 +8,38 @@ const fs = require('fs');
|
|
|
8
8
|
process.env.NoDefaultCurrentDirectoryInExePath = '1';
|
|
9
9
|
|
|
10
10
|
// ── 首次部署:将默认 bingo 配置复制到 ~/.claude/bingo/ ──
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
/**
|
|
12
|
+
* 更加健壮的根目录定位:
|
|
13
|
+
* 1. 如果 preload.ts 在 ../ (当前 bin/ 目录下运行)
|
|
14
|
+
* 2. 否则查找同级及上级目录中的 package.json
|
|
15
|
+
*/
|
|
16
|
+
function getProjectRoot() {
|
|
17
|
+
let curr = __dirname;
|
|
18
|
+
try {
|
|
19
|
+
while (curr !== path.dirname(curr)) {
|
|
20
|
+
if (fs.existsSync(path.join(curr, 'preload.ts')) || fs.existsSync(path.join(curr, 'package.json'))) {
|
|
21
|
+
return curr;
|
|
22
|
+
}
|
|
23
|
+
const parent = path.dirname(curr);
|
|
24
|
+
if (fs.existsSync(path.join(parent, 'preload.ts'))) return parent;
|
|
25
|
+
curr = parent;
|
|
26
|
+
}
|
|
27
|
+
} catch (err) {
|
|
28
|
+
// 防止权限拒绝等导致挂死
|
|
29
|
+
}
|
|
30
|
+
return path.join(__dirname, '..');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const ROOT_DIR = getProjectRoot();
|
|
34
|
+
|
|
14
35
|
(function deployBingoDefaults() {
|
|
15
36
|
const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
|
|
16
37
|
const bingoDir = path.join(configDir, 'bingo');
|
|
17
38
|
const targetSettings = path.join(bingoDir, 'settings.json');
|
|
18
39
|
|
|
19
|
-
// 只在 settings.json
|
|
40
|
+
// 只在 settings.json 不存在时才部署
|
|
20
41
|
if (!fs.existsSync(targetSettings)) {
|
|
21
|
-
const defaultsDir = path.join(
|
|
42
|
+
const defaultsDir = path.join(ROOT_DIR, 'config', 'bingo-defaults');
|
|
22
43
|
const srcSettings = path.join(defaultsDir, 'settings.json');
|
|
23
44
|
|
|
24
45
|
if (fs.existsSync(srcSettings)) {
|
|
@@ -40,42 +61,53 @@ const bunPath =
|
|
|
40
61
|
process.env.BUN_PATH ||
|
|
41
62
|
path.join(os.homedir(), '.bun', 'bin', 'bun.exe');
|
|
42
63
|
|
|
43
|
-
// 检查 bun
|
|
64
|
+
// 检查 bun 是否可用
|
|
44
65
|
function bunExists() {
|
|
45
66
|
if (fs.existsSync(bunPath)) return true;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
67
|
+
try {
|
|
68
|
+
const result = spawnSync('bun', ['--version'], { stdio: 'ignore', shell: true });
|
|
69
|
+
return result.status === 0;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
49
73
|
}
|
|
50
74
|
|
|
51
|
-
// 安装 bun
|
|
75
|
+
// 安装 bun
|
|
52
76
|
function installBun() {
|
|
53
77
|
console.log('[bingocode] bun 未检测到,正在自动安装...');
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
78
|
+
try {
|
|
79
|
+
const result = spawnSync(
|
|
80
|
+
'powershell',
|
|
81
|
+
['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command',
|
|
82
|
+
'irm bun.sh/install.ps1 | iex'],
|
|
83
|
+
{ stdio: 'inherit', shell: false }
|
|
84
|
+
);
|
|
85
|
+
if (result.status !== 0) {
|
|
86
|
+
throw new Error(`Exit code ${result.status}`);
|
|
87
|
+
}
|
|
88
|
+
console.log('[bingocode] bun 安装完成,正在启动...');
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error(`[bingocode] bun 自动安装失败: ${err.message}`);
|
|
91
|
+
console.log('[bingocode] 请手动从 https://bun.sh 安装 Bun 后重试。');
|
|
92
|
+
return false;
|
|
63
93
|
}
|
|
64
|
-
|
|
94
|
+
return true;
|
|
65
95
|
}
|
|
66
96
|
|
|
67
97
|
if (!bunExists()) {
|
|
68
|
-
installBun()
|
|
98
|
+
if (!installBun()) {
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
69
101
|
}
|
|
70
102
|
|
|
71
103
|
// 安装后 bun.exe 在固定位置;若在 PATH 里则直接用 "bun"
|
|
72
104
|
const bun = fs.existsSync(bunPath) ? bunPath : 'bun';
|
|
73
105
|
|
|
74
|
-
// Bingo Manager
|
|
75
|
-
const entry = path.join(
|
|
106
|
+
// Bingo Manager 入口
|
|
107
|
+
const entry = path.join(ROOT_DIR, 'src', 'entrypoints', 'manager.tsx');
|
|
76
108
|
|
|
77
|
-
// preload shim
|
|
78
|
-
const preload = path.join(
|
|
109
|
+
// preload shim
|
|
110
|
+
const preload = path.join(ROOT_DIR, 'preload.ts');
|
|
79
111
|
if (!fs.existsSync(preload)) {
|
|
80
112
|
console.error('[bingocode] 找不到 preload.ts,MACRO 将无法注入:' + preload);
|
|
81
113
|
process.exit(1);
|
|
@@ -83,7 +115,7 @@ if (!fs.existsSync(preload)) {
|
|
|
83
115
|
|
|
84
116
|
// 检查 .env
|
|
85
117
|
let envFlag = '';
|
|
86
|
-
const envPath = path.join(
|
|
118
|
+
const envPath = path.join(ROOT_DIR, '.env');
|
|
87
119
|
if (fs.existsSync(envPath)) {
|
|
88
120
|
envFlag = `--env-file=${envPath}`;
|
|
89
121
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bingo Manager 入口
|
|
3
|
-
* 直接渲染 CliMenuManager(窗口管理控制台),不走 cli.tsx 的完整启动流程。
|
|
4
|
-
* 由 bin/bingo-win.cjs 和 bin/bingo 调用。
|
|
5
|
-
*/
|
|
6
|
-
import React from 'react';
|
|
7
|
-
import { render } from 'ink';
|
|
8
|
-
import { CliMenuManager } from '../manager/CliMenuManager.tsx';
|
|
9
|
-
|
|
10
|
-
render(<CliMenuManager />);
|
|
1
|
+
/**
|
|
2
|
+
* Bingo Manager 入口
|
|
3
|
+
* 直接渲染 CliMenuManager(窗口管理控制台),不走 cli.tsx 的完整启动流程。
|
|
4
|
+
* 由 bin/bingo-win.cjs 和 bin/bingo 调用。
|
|
5
|
+
*/
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import { render } from 'ink';
|
|
8
|
+
import { CliMenuManager } from '../manager/CliMenuManager.tsx';
|
|
9
|
+
|
|
10
|
+
render(<CliMenuManager />);
|
|
@@ -980,7 +980,7 @@ export const CliMenuManager: React.FC = () => {
|
|
|
980
980
|
</Box>
|
|
981
981
|
|
|
982
982
|
{/* ── 消息区 ── */}
|
|
983
|
-
<Box height={MSGS_H} flexDirection="column"
|
|
983
|
+
<Box height={MSGS_H} flexDirection="column">
|
|
984
984
|
{loadingMsgs && <Text color="yellow">加载消息中...</Text>}
|
|
985
985
|
{msgsErr && <Text color="red">错误: {msgsErr}</Text>}
|
|
986
986
|
{!loadingMsgs && !msgsErr && displayMsgs.length === 0 && (
|
|
@@ -1024,11 +1024,16 @@ export const CliMenuManager: React.FC = () => {
|
|
|
1024
1024
|
setHistoryMenuStage('window');
|
|
1025
1025
|
}
|
|
1026
1026
|
}}
|
|
1027
|
-
itemComponent={({ isSelected, label
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1027
|
+
itemComponent={({ isSelected, label }) => {
|
|
1028
|
+
const it = groupedHistoryItems.find(i => i.label === label);
|
|
1029
|
+
const isGroup = it?.isGroup;
|
|
1030
|
+
const color = it?.color;
|
|
1031
|
+
return (
|
|
1032
|
+
<Text color={isGroup ? 'gray' : (color ? color : (isSelected ? 'cyan' : undefined))}>
|
|
1033
|
+
{label}
|
|
1034
|
+
</Text>
|
|
1035
|
+
)
|
|
1036
|
+
}}
|
|
1032
1037
|
/>
|
|
1033
1038
|
<Hint>{i18nMap[lang].historyHint}</Hint>
|
|
1034
1039
|
</Box>
|
|
@@ -82,11 +82,11 @@ const ProvidersMenu: React.FC = () => {
|
|
|
82
82
|
refresh();
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
-
} else if (inputKey.toLowerCase() === 'q') {
|
|
85
|
+
} else if (inputKey.toLowerCase() === 'q' || key.escape) {
|
|
86
86
|
setRemoveConfirm(false); setMode('list');
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
});
|
|
89
|
+
}, { isActive: mode === 'list' || mode === 'removeConfirm' });
|
|
90
90
|
|
|
91
91
|
// ADD模式交互
|
|
92
92
|
useInput((inputKey, key) => {
|
|
@@ -102,7 +102,7 @@ const ProvidersMenu: React.FC = () => {
|
|
|
102
102
|
else if (key.upArrow) setModelSelectIdx(idx => Math.max(0, idx - 1));
|
|
103
103
|
else if (key.return) { setAddStep(1); }
|
|
104
104
|
}
|
|
105
|
-
});
|
|
105
|
+
}, { isActive: mode === 'add' });
|
|
106
106
|
|
|
107
107
|
// 新增表单
|
|
108
108
|
const addSubmit = async (keyInput: string) => {
|
|
@@ -21,9 +21,28 @@ import { openaiChatStreamToAnthropic } from './streaming/openaiChatStreamToAnthr
|
|
|
21
21
|
import { openaiResponsesStreamToAnthropic } from './streaming/openaiResponsesStreamToAnthropic.js'
|
|
22
22
|
import type { AnthropicRequest } from './transform/types.js'
|
|
23
23
|
import type { SlotName } from '../types/provider.js'
|
|
24
|
+
import { logForDebugging } from '../../utils/debug.js'
|
|
25
|
+
import { appendFile, mkdir } from 'node:fs/promises'
|
|
26
|
+
import { join } from 'node:path'
|
|
27
|
+
import { homedir } from 'node:os'
|
|
24
28
|
|
|
25
29
|
const providerService = new ProviderService()
|
|
26
30
|
|
|
31
|
+
async function logToFile(message: string) {
|
|
32
|
+
// Disabled log output for production
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function sendAnthropicError(message: string, _model: string | undefined, status = 502): Response {
|
|
36
|
+
const fullMessage = `[Bingo Proxy] ${message}`
|
|
37
|
+
void logToFile(`ERROR: ${fullMessage} (status: ${status})`)
|
|
38
|
+
|
|
39
|
+
// 统一返回纯文本以规避 CLI 的 JSON 字符串打印行为,确保报错清晰且不带 JSON 外壳
|
|
40
|
+
return new Response(fullMessage, {
|
|
41
|
+
status: status,
|
|
42
|
+
headers: { 'Content-Type': 'text/plain' }
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
27
46
|
function buildUpstreamHeaders(apiKey: string): Record<string, string> {
|
|
28
47
|
const headers: Record<string, string> = {
|
|
29
48
|
'Content-Type': 'application/json',
|
|
@@ -43,23 +62,41 @@ function buildUpstreamHeaders(apiKey: string): Record<string, string> {
|
|
|
43
62
|
|
|
44
63
|
/**
|
|
45
64
|
* Identify which slot a model name belongs to.
|
|
46
|
-
*
|
|
65
|
+
* Checks for exact matches in provider models first, then falls back to keyword matching.
|
|
47
66
|
*/
|
|
48
|
-
function identifySlot(modelName: string): SlotName {
|
|
67
|
+
async function identifySlot(modelName: string): Promise<SlotName> {
|
|
49
68
|
const m = modelName.toLowerCase()
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
69
|
+
void logToFile(`Identifying slot. Input: "${modelName}"`)
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const { providers } = await providerService.listProviders()
|
|
73
|
+
for (const config of providers) {
|
|
74
|
+
if (config.models) {
|
|
75
|
+
for (const [slot, id] of Object.entries(config.models)) {
|
|
76
|
+
if (id && id.toLowerCase() === m) {
|
|
77
|
+
void logToFile(`Match found in config! Slot: ${slot} (ID: ${id})`)
|
|
78
|
+
return slot as SlotName
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch (e) {
|
|
84
|
+
void logToFile(`Error reading providers for identification: ${e}`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let result: SlotName = 'main'
|
|
88
|
+
if (m.includes('opus')) result = 'opus'
|
|
89
|
+
else if (m.includes('sonnet')) result = 'sonnet'
|
|
90
|
+
else if (m.includes('haiku')) result = 'haiku'
|
|
91
|
+
|
|
92
|
+
void logToFile(`Fallback identification result: ${result}`)
|
|
93
|
+
return result
|
|
54
94
|
}
|
|
55
95
|
|
|
56
96
|
export async function handleProxyRequest(req: Request, url: URL): Promise<Response> {
|
|
57
97
|
// Only handle POST /proxy/v1/messages
|
|
58
98
|
if (req.method !== 'POST' || url.pathname !== '/proxy/v1/messages') {
|
|
59
|
-
return
|
|
60
|
-
{ error: 'Not Found', message: 'Proxy only handles POST /proxy/v1/messages' },
|
|
61
|
-
{ status: 404 },
|
|
62
|
-
)
|
|
99
|
+
return sendAnthropicError('Not Found: Proxy only handles POST /proxy/v1/messages', undefined, 404)
|
|
63
100
|
}
|
|
64
101
|
|
|
65
102
|
// Parse request body
|
|
@@ -67,20 +104,19 @@ export async function handleProxyRequest(req: Request, url: URL): Promise<Respon
|
|
|
67
104
|
try {
|
|
68
105
|
body = (await req.json()) as AnthropicRequest
|
|
69
106
|
} catch {
|
|
70
|
-
return
|
|
71
|
-
{ type: 'error', error: { type: 'invalid_request_error', message: 'Invalid JSON in request body' } },
|
|
72
|
-
{ status: 400 },
|
|
73
|
-
)
|
|
107
|
+
return sendAnthropicError('Invalid JSON in request body', undefined, 400)
|
|
74
108
|
}
|
|
75
109
|
|
|
76
110
|
const isStream = body.stream === true
|
|
77
111
|
|
|
78
112
|
// --- Slot-based routing ---
|
|
79
|
-
const slot = identifySlot(body.model ?? '')
|
|
113
|
+
const slot = await identifySlot(body.model ?? '')
|
|
114
|
+
const reqId = Math.random().toString(36).substring(7)
|
|
115
|
+
void logToFile(`[${reqId}] Body Model: "${body.model}". Decided Slot: "${slot}"`)
|
|
116
|
+
|
|
80
117
|
const slotConfig = await providerService.getProviderForSlot(slot)
|
|
81
118
|
|
|
82
119
|
if (slotConfig) {
|
|
83
|
-
// Use the slot's configured modelId instead of the original Claude model name
|
|
84
120
|
const proxiedBody: AnthropicRequest = { ...body, model: slotConfig.modelId }
|
|
85
121
|
const baseUrl = slotConfig.baseUrl.replace(/\/+$/, '')
|
|
86
122
|
const uiLabel = slotConfig.label || null
|
|
@@ -94,40 +130,20 @@ export async function handleProxyRequest(req: Request, url: URL): Promise<Respon
|
|
|
94
130
|
return await handleOpenaiResponses(proxiedBody, baseUrl, slotConfig.apiKey, isStream, uiLabel)
|
|
95
131
|
}
|
|
96
132
|
} catch (err) {
|
|
97
|
-
|
|
98
|
-
return
|
|
99
|
-
{
|
|
100
|
-
type: 'error',
|
|
101
|
-
error: {
|
|
102
|
-
type: 'api_error',
|
|
103
|
-
message: err instanceof Error ? err.message : String(err),
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
{ status: 502 },
|
|
107
|
-
)
|
|
133
|
+
logForDebugging(`[HANDLER][${reqId}] Slot "${slot}" upstream connection failed: ${err}`, { level: 'error' })
|
|
134
|
+
return sendAnthropicError(`API Connection Failed: Ensure baseUrl is correct. Error: ${err instanceof Error ? err.message : String(err)}`, body.model, 502)
|
|
108
135
|
}
|
|
109
136
|
}
|
|
110
137
|
|
|
111
138
|
// --- Fallback: legacy single-activeId routing ---
|
|
112
139
|
const config = await providerService.getActiveProviderForProxy()
|
|
113
140
|
if (!config) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
type: 'error',
|
|
117
|
-
error: {
|
|
118
|
-
type: 'invalid_request_error',
|
|
119
|
-
message: `No provider configured for slot "${slot}". Please configure slots in the Provider panel.`,
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
{ status: 400 },
|
|
123
|
-
)
|
|
141
|
+
logForDebugging(`[HANDLER][${reqId}] No provider configured for slot "${slot}"`, { level: 'warn' })
|
|
142
|
+
return sendAnthropicError(`No provider configured for slot "${slot}". Please configure slots in the Provider panel.`, body.model)
|
|
124
143
|
}
|
|
125
144
|
|
|
126
145
|
if (config.apiFormat === 'anthropic') {
|
|
127
|
-
return
|
|
128
|
-
{ type: 'error', error: { type: 'invalid_request_error', message: 'Active provider uses anthropic format — proxy not needed' } },
|
|
129
|
-
{ status: 400 },
|
|
130
|
-
)
|
|
146
|
+
return sendAnthropicError('Active provider uses anthropic format — proxy not needed', body.model)
|
|
131
147
|
}
|
|
132
148
|
|
|
133
149
|
const baseUrl = config.baseUrl.replace(/\/+$/, '')
|
|
@@ -139,17 +155,8 @@ export async function handleProxyRequest(req: Request, url: URL): Promise<Respon
|
|
|
139
155
|
return await handleOpenaiResponses(body, baseUrl, config.apiKey, isStream)
|
|
140
156
|
}
|
|
141
157
|
} catch (err) {
|
|
142
|
-
|
|
143
|
-
return
|
|
144
|
-
{
|
|
145
|
-
type: 'error',
|
|
146
|
-
error: {
|
|
147
|
-
type: 'api_error',
|
|
148
|
-
message: err instanceof Error ? err.message : String(err),
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
{ status: 502 },
|
|
152
|
-
)
|
|
158
|
+
logForDebugging(`[HANDLER][${reqId}] Upstream connection failed: ${err}`, { level: 'error' })
|
|
159
|
+
return sendAnthropicError(`API Connection Failed: Ensure baseUrl is correct. Error: ${err instanceof Error ? err.message : String(err)}`, body.model, 502)
|
|
153
160
|
}
|
|
154
161
|
}
|
|
155
162
|
|
|
@@ -176,12 +183,22 @@ async function handleAnthropicPassthrough(
|
|
|
176
183
|
signal: isStream ? AbortSignal.timeout(30_000) : AbortSignal.timeout(300_000),
|
|
177
184
|
})
|
|
178
185
|
|
|
179
|
-
|
|
186
|
+
if (!upstream.ok) {
|
|
187
|
+
const errText = await upstream.text().catch(() => '')
|
|
188
|
+
const errorMessage = `Upstream returned HTTP ${upstream.status}: ${errText.slice(0, 500)}`
|
|
189
|
+
return sendAnthropicError(errorMessage, body.model, 502)
|
|
190
|
+
}
|
|
180
191
|
|
|
181
192
|
if (isStream) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
193
|
+
if (!upstream.body) {
|
|
194
|
+
return sendAnthropicError('Upstream returned no body for stream', body.model)
|
|
195
|
+
}
|
|
196
|
+
return new Response(upstream.body, {
|
|
197
|
+
status: 200,
|
|
198
|
+
headers: {
|
|
199
|
+
'Content-Type': 'application/json',
|
|
200
|
+
},
|
|
201
|
+
})
|
|
185
202
|
}
|
|
186
203
|
|
|
187
204
|
const responseBody = await upstream.json()
|
|
@@ -209,12 +226,21 @@ async function handleOpenaiChat(
|
|
|
209
226
|
})
|
|
210
227
|
|
|
211
228
|
if (!upstream.ok) {
|
|
212
|
-
|
|
229
|
+
const errText = await upstream.text().catch(() => '')
|
|
230
|
+
let errorMessage = `Upstream returned HTTP ${upstream.status}: ${errText.slice(0, 500)}`
|
|
231
|
+
let status = upstream.status
|
|
232
|
+
|
|
233
|
+
if (upstream.status === 404) {
|
|
234
|
+
status = 502
|
|
235
|
+
errorMessage = `API Connection Failed: The baseUrl path might be incorrect (404 Not Found). Detail: ${errText}`
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return sendAnthropicError(errorMessage, body.model, status)
|
|
213
239
|
}
|
|
214
240
|
|
|
215
241
|
if (isStream) {
|
|
216
242
|
if (!upstream.body) {
|
|
217
|
-
return
|
|
243
|
+
return sendAnthropicError('Upstream returned no body for stream', body.model)
|
|
218
244
|
}
|
|
219
245
|
const anthropicStream = openaiChatStreamToAnthropic(upstream.body, uiLabel || body.model)
|
|
220
246
|
return new Response(anthropicStream, {
|
|
@@ -252,24 +278,20 @@ async function handleOpenaiResponses(
|
|
|
252
278
|
|
|
253
279
|
if (!upstream.ok) {
|
|
254
280
|
const errText = await upstream.text().catch(() => '')
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
)
|
|
281
|
+
let errorMessage = `Upstream returned HTTP ${upstream.status}: ${errText.slice(0, 500)}`
|
|
282
|
+
let status = upstream.status
|
|
283
|
+
|
|
284
|
+
if (upstream.status === 404) {
|
|
285
|
+
status = 502
|
|
286
|
+
errorMessage = `API Connection Failed: The baseUrl path might be incorrect (404 Not Found). Detail: ${errText}`
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return sendAnthropicError(errorMessage, body.model, status)
|
|
265
290
|
}
|
|
266
291
|
|
|
267
292
|
if (isStream) {
|
|
268
293
|
if (!upstream.body) {
|
|
269
|
-
return
|
|
270
|
-
{ type: 'error', error: { type: 'api_error', message: 'Upstream returned no body for stream' } },
|
|
271
|
-
{ status: 502 },
|
|
272
|
-
)
|
|
294
|
+
return sendAnthropicError('Upstream returned no body for stream', body.model)
|
|
273
295
|
}
|
|
274
296
|
const anthropicStream = openaiResponsesStreamToAnthropic(upstream.body, uiLabel || body.model)
|
|
275
297
|
return new Response(anthropicStream, {
|