bingocode 1.0.19 → 1.0.21
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/package.json
CHANGED
|
@@ -818,7 +818,7 @@ export const CliMenuManager: React.FC = () => {
|
|
|
818
818
|
<Box flexDirection="column" width={VIEW_W} height={MID_H}>
|
|
819
819
|
{creating && <Text color="yellow">新建中...</Text>}
|
|
820
820
|
{createErr && <Text color="red">新建失败: {createErr}</Text>}
|
|
821
|
-
{newSessionId && <Text color="green"
|
|
821
|
+
{newSessionId && <Text color="green">新建会话: {newSessionId}</Text>}
|
|
822
822
|
{!creating && !createErr && !newSessionId && <Text dimColor>已进入新建会话页,等待创建结果...</Text>}
|
|
823
823
|
</Box>
|
|
824
824
|
);
|
|
@@ -1,302 +1,289 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Proxy Handler — protocol-translating reverse proxy for OpenAI-compatible APIs.
|
|
3
|
-
*
|
|
4
|
-
* Receives Anthropic Messages API requests from the CLI, transforms them to
|
|
5
|
-
* OpenAI Chat Completions or Responses API format, forwards to the upstream
|
|
6
|
-
* provider, and transforms the response back to Anthropic format.
|
|
7
|
-
*
|
|
8
|
-
* Supports slot-based routing: each request's model field is mapped to a slot
|
|
9
|
-
* (main/haiku/sonnet/opus), and the corresponding configured provider is used.
|
|
10
|
-
*
|
|
11
|
-
* Derived from cc-switch (https://github.com/farion1231/cc-switch)
|
|
12
|
-
* Original work by Jason Young, MIT License
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { ProviderService } from '../services/providerService.js'
|
|
16
|
-
import { anthropicToOpenaiChat } from './transform/anthropicToOpenaiChat.js'
|
|
17
|
-
import { anthropicToOpenaiResponses } from './transform/anthropicToOpenaiResponses.js'
|
|
18
|
-
import { openaiChatToAnthropic } from './transform/openaiChatToAnthropic.js'
|
|
19
|
-
import { openaiResponsesToAnthropic } from './transform/openaiResponsesToAnthropic.js'
|
|
20
|
-
import { openaiChatStreamToAnthropic } from './streaming/openaiChatStreamToAnthropic.js'
|
|
21
|
-
import { openaiResponsesStreamToAnthropic } from './streaming/openaiResponsesStreamToAnthropic.js'
|
|
22
|
-
import {
|
|
23
|
-
import type {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
*
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (m.includes('
|
|
52
|
-
if (m.includes('
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
{
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
},
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
{
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
*
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
'
|
|
173
|
-
'
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
type: 'error',
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
headers: {
|
|
291
|
-
'Content-Type': 'text/event-stream',
|
|
292
|
-
'Cache-Control': 'no-cache',
|
|
293
|
-
Connection: 'keep-alive',
|
|
294
|
-
},
|
|
295
|
-
})
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Non-streaming
|
|
299
|
-
const responseBody = await upstream.json()
|
|
300
|
-
const anthropicResponse = openaiResponsesToAnthropic(responseBody, uiLabel || body.model)
|
|
301
|
-
return Response.json(anthropicResponse)
|
|
302
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Proxy Handler — protocol-translating reverse proxy for OpenAI-compatible APIs.
|
|
3
|
+
*
|
|
4
|
+
* Receives Anthropic Messages API requests from the CLI, transforms them to
|
|
5
|
+
* OpenAI Chat Completions or Responses API format, forwards to the upstream
|
|
6
|
+
* provider, and transforms the response back to Anthropic format.
|
|
7
|
+
*
|
|
8
|
+
* Supports slot-based routing: each request's model field is mapped to a slot
|
|
9
|
+
* (main/haiku/sonnet/opus), and the corresponding configured provider is used.
|
|
10
|
+
*
|
|
11
|
+
* Derived from cc-switch (https://github.com/farion1231/cc-switch)
|
|
12
|
+
* Original work by Jason Young, MIT License
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { ProviderService } from '../services/providerService.js'
|
|
16
|
+
import { anthropicToOpenaiChat } from './transform/anthropicToOpenaiChat.js'
|
|
17
|
+
import { anthropicToOpenaiResponses } from './transform/anthropicToOpenaiResponses.js'
|
|
18
|
+
import { openaiChatToAnthropic } from './transform/openaiChatToAnthropic.js'
|
|
19
|
+
import { openaiResponsesToAnthropic } from './transform/openaiResponsesToAnthropic.js'
|
|
20
|
+
import { openaiChatStreamToAnthropic } from './streaming/openaiChatStreamToAnthropic.js'
|
|
21
|
+
import { openaiResponsesStreamToAnthropic } from './streaming/openaiResponsesStreamToAnthropic.js'
|
|
22
|
+
import type { AnthropicRequest } from './transform/types.js'
|
|
23
|
+
import type { SlotName } from '../types/provider.js'
|
|
24
|
+
|
|
25
|
+
const providerService = new ProviderService()
|
|
26
|
+
|
|
27
|
+
function buildUpstreamHeaders(apiKey: string): Record<string, string> {
|
|
28
|
+
const headers: Record<string, string> = {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
Authorization: `Bearer ${apiKey}`,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (process.env.X_API_KEY) {
|
|
34
|
+
headers['x-api-key'] = process.env.X_API_KEY
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (process.env.APPLICATION_NAME) {
|
|
38
|
+
headers['x-application-name'] = process.env.APPLICATION_NAME
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return headers
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Identify which slot a model name belongs to.
|
|
46
|
+
* Claude Code sends model names like "claude-3-5-haiku-20241022".
|
|
47
|
+
*/
|
|
48
|
+
function identifySlot(modelName: string): SlotName {
|
|
49
|
+
const m = modelName.toLowerCase()
|
|
50
|
+
if (m.includes('haiku')) return 'haiku'
|
|
51
|
+
if (m.includes('sonnet')) return 'sonnet'
|
|
52
|
+
if (m.includes('opus')) return 'opus'
|
|
53
|
+
return 'main'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function handleProxyRequest(req: Request, url: URL): Promise<Response> {
|
|
57
|
+
// Only handle POST /proxy/v1/messages
|
|
58
|
+
if (req.method !== 'POST' || url.pathname !== '/proxy/v1/messages') {
|
|
59
|
+
return Response.json(
|
|
60
|
+
{ error: 'Not Found', message: 'Proxy only handles POST /proxy/v1/messages' },
|
|
61
|
+
{ status: 404 },
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Parse request body
|
|
66
|
+
let body: AnthropicRequest
|
|
67
|
+
try {
|
|
68
|
+
body = (await req.json()) as AnthropicRequest
|
|
69
|
+
} catch {
|
|
70
|
+
return Response.json(
|
|
71
|
+
{ type: 'error', error: { type: 'invalid_request_error', message: 'Invalid JSON in request body' } },
|
|
72
|
+
{ status: 400 },
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const isStream = body.stream === true
|
|
77
|
+
|
|
78
|
+
// --- Slot-based routing ---
|
|
79
|
+
const slot = identifySlot(body.model ?? '')
|
|
80
|
+
const slotConfig = await providerService.getProviderForSlot(slot)
|
|
81
|
+
|
|
82
|
+
if (slotConfig) {
|
|
83
|
+
// Use the slot's configured modelId instead of the original Claude model name
|
|
84
|
+
const proxiedBody: AnthropicRequest = { ...body, model: slotConfig.modelId }
|
|
85
|
+
const baseUrl = slotConfig.baseUrl.replace(/\/+$/, '')
|
|
86
|
+
const uiLabel = slotConfig.label || null
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
if (slotConfig.apiFormat === 'anthropic') {
|
|
90
|
+
return await handleAnthropicPassthrough(proxiedBody, baseUrl, slotConfig.apiKey, isStream, uiLabel)
|
|
91
|
+
} else if (slotConfig.apiFormat === 'openai_chat') {
|
|
92
|
+
return await handleOpenaiChat(proxiedBody, baseUrl, slotConfig.apiKey, isStream, uiLabel)
|
|
93
|
+
} else {
|
|
94
|
+
return await handleOpenaiResponses(proxiedBody, baseUrl, slotConfig.apiKey, isStream, uiLabel)
|
|
95
|
+
}
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error(`[Proxy] Slot "${slot}" upstream request failed:`, err)
|
|
98
|
+
return Response.json(
|
|
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
|
+
)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// --- Fallback: legacy single-activeId routing ---
|
|
112
|
+
const config = await providerService.getActiveProviderForProxy()
|
|
113
|
+
if (!config) {
|
|
114
|
+
return Response.json(
|
|
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
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (config.apiFormat === 'anthropic') {
|
|
127
|
+
return Response.json(
|
|
128
|
+
{ type: 'error', error: { type: 'invalid_request_error', message: 'Active provider uses anthropic format — proxy not needed' } },
|
|
129
|
+
{ status: 400 },
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const baseUrl = config.baseUrl.replace(/\/+$/, '')
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
if (config.apiFormat === 'openai_chat') {
|
|
137
|
+
return await handleOpenaiChat(body, baseUrl, config.apiKey, isStream)
|
|
138
|
+
} else {
|
|
139
|
+
return await handleOpenaiResponses(body, baseUrl, config.apiKey, isStream)
|
|
140
|
+
}
|
|
141
|
+
} catch (err) {
|
|
142
|
+
console.error('[Proxy] Upstream request failed:', err)
|
|
143
|
+
return Response.json(
|
|
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
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Pass through to an Anthropic-compatible upstream without format transformation.
|
|
158
|
+
* Used when the slot provider uses apiFormat 'anthropic'.
|
|
159
|
+
*/
|
|
160
|
+
async function handleAnthropicPassthrough(
|
|
161
|
+
body: AnthropicRequest,
|
|
162
|
+
baseUrl: string,
|
|
163
|
+
apiKey: string,
|
|
164
|
+
isStream: boolean,
|
|
165
|
+
uiLabel: string | null = null,
|
|
166
|
+
): Promise<Response> {
|
|
167
|
+
const url = `${baseUrl}/v1/messages`
|
|
168
|
+
const upstream = await fetch(url, {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
headers: {
|
|
171
|
+
'Content-Type': 'application/json',
|
|
172
|
+
'x-api-key': apiKey,
|
|
173
|
+
'anthropic-version': '2023-06-01',
|
|
174
|
+
},
|
|
175
|
+
body: JSON.stringify(body),
|
|
176
|
+
signal: isStream ? AbortSignal.timeout(30_000) : AbortSignal.timeout(300_000),
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// ... (existing error checks)
|
|
180
|
+
|
|
181
|
+
if (isStream) {
|
|
182
|
+
// Anthropic pass-through doesn't easily support label injection without parsing SSE
|
|
183
|
+
// So for native anthropic format, we might just pass original stream
|
|
184
|
+
return new Response(upstream.body, { /* ... */ })
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const responseBody = await upstream.json()
|
|
188
|
+
if (uiLabel) {
|
|
189
|
+
(responseBody as any).model = uiLabel
|
|
190
|
+
}
|
|
191
|
+
return Response.json(responseBody)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function handleOpenaiChat(
|
|
195
|
+
body: AnthropicRequest,
|
|
196
|
+
baseUrl: string,
|
|
197
|
+
apiKey: string,
|
|
198
|
+
isStream: boolean,
|
|
199
|
+
uiLabel: string | null = null,
|
|
200
|
+
): Promise<Response> {
|
|
201
|
+
const transformed = anthropicToOpenaiChat(body)
|
|
202
|
+
const url = `${baseUrl}/v1/chat/completions`
|
|
203
|
+
|
|
204
|
+
const upstream = await fetch(url, {
|
|
205
|
+
method: 'POST',
|
|
206
|
+
headers: buildUpstreamHeaders(apiKey),
|
|
207
|
+
body: JSON.stringify(transformed),
|
|
208
|
+
signal: isStream ? AbortSignal.timeout(30_000) : AbortSignal.timeout(300_000),
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
if (!upstream.ok) {
|
|
212
|
+
// ... error handling
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (isStream) {
|
|
216
|
+
if (!upstream.body) {
|
|
217
|
+
return Response.json(/* ... */)
|
|
218
|
+
}
|
|
219
|
+
const anthropicStream = openaiChatStreamToAnthropic(upstream.body, uiLabel || body.model)
|
|
220
|
+
return new Response(anthropicStream, {
|
|
221
|
+
status: 200,
|
|
222
|
+
headers: {
|
|
223
|
+
'Content-Type': 'text/event-stream',
|
|
224
|
+
'Cache-Control': 'no-cache',
|
|
225
|
+
Connection: 'keep-alive',
|
|
226
|
+
},
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Non-streaming
|
|
231
|
+
const responseBody = await upstream.json()
|
|
232
|
+
const anthropicResponse = openaiChatToAnthropic(responseBody, uiLabel || body.model)
|
|
233
|
+
return Response.json(anthropicResponse)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function handleOpenaiResponses(
|
|
237
|
+
body: AnthropicRequest,
|
|
238
|
+
baseUrl: string,
|
|
239
|
+
apiKey: string,
|
|
240
|
+
isStream: boolean,
|
|
241
|
+
uiLabel: string | null = null,
|
|
242
|
+
): Promise<Response> {
|
|
243
|
+
const transformed = anthropicToOpenaiResponses(body)
|
|
244
|
+
const url = `${baseUrl}/v1/responses`
|
|
245
|
+
|
|
246
|
+
const upstream = await fetch(url, {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
headers: buildUpstreamHeaders(apiKey),
|
|
249
|
+
body: JSON.stringify(transformed),
|
|
250
|
+
signal: isStream ? AbortSignal.timeout(30_000) : AbortSignal.timeout(300_000),
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
if (!upstream.ok) {
|
|
254
|
+
const errText = await upstream.text().catch(() => '')
|
|
255
|
+
return Response.json(
|
|
256
|
+
{
|
|
257
|
+
type: 'error',
|
|
258
|
+
error: {
|
|
259
|
+
type: 'api_error',
|
|
260
|
+
message: `Upstream returned HTTP ${upstream.status}: ${errText.slice(0, 500)}`,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{ status: upstream.status },
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (isStream) {
|
|
268
|
+
if (!upstream.body) {
|
|
269
|
+
return Response.json(
|
|
270
|
+
{ type: 'error', error: { type: 'api_error', message: 'Upstream returned no body for stream' } },
|
|
271
|
+
{ status: 502 },
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
const anthropicStream = openaiResponsesStreamToAnthropic(upstream.body, uiLabel || body.model)
|
|
275
|
+
return new Response(anthropicStream, {
|
|
276
|
+
status: 200,
|
|
277
|
+
headers: {
|
|
278
|
+
'Content-Type': 'text/event-stream',
|
|
279
|
+
'Cache-Control': 'no-cache',
|
|
280
|
+
Connection: 'keep-alive',
|
|
281
|
+
},
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Non-streaming
|
|
286
|
+
const responseBody = await upstream.json()
|
|
287
|
+
const anthropicResponse = openaiResponsesToAnthropic(responseBody, uiLabel || body.model)
|
|
288
|
+
return Response.json(anthropicResponse)
|
|
289
|
+
}
|
|
@@ -409,8 +409,8 @@ export class ProviderService {
|
|
|
409
409
|
}
|
|
410
410
|
|
|
411
411
|
try {
|
|
412
|
-
const
|
|
413
|
-
const res = await fetch(url, { headers, signal: AbortSignal.timeout(10000), ...
|
|
412
|
+
const directOpts = getDirectFetchOptions()
|
|
413
|
+
const res = await fetch(url, { headers, signal: AbortSignal.timeout(10000), ...directOpts })
|
|
414
414
|
if (!res.ok) {
|
|
415
415
|
console.error(`[ProviderService] Failed to fetch models from ${url}: ${res.status}`)
|
|
416
416
|
return []
|
|
@@ -438,11 +438,26 @@ export class ProviderService {
|
|
|
438
438
|
|
|
439
439
|
// If no modelId provided, try to fetch from provider or use preset default
|
|
440
440
|
let modelId = overrides?.modelId || provider.models.main
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
441
|
+
const needsAutoDetect =
|
|
442
|
+
!modelId ||
|
|
443
|
+
modelId === 'auto' ||
|
|
444
|
+
(apiFormat !== 'anthropic' && modelId.startsWith('claude-'))
|
|
445
|
+
if (needsAutoDetect) {
|
|
446
|
+
const fetched = await this.fetchProviderModels(id).catch(() => [])
|
|
447
|
+
if (fetched.length > 0) {
|
|
448
|
+
modelId = fetched[0] // Use first available model for testing
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// 兜底:如果仍然没有有效的 modelId,直接返回有意义的错误
|
|
453
|
+
if (!modelId) {
|
|
454
|
+
return {
|
|
455
|
+
connectivity: {
|
|
456
|
+
success: false,
|
|
457
|
+
latencyMs: 0,
|
|
458
|
+
error: '无法确定测试用模型:models.main 为空且自动拉取模型列表失败。请先在槽位配置中选择模型,或检查 API Key 和网络连接。',
|
|
459
|
+
},
|
|
460
|
+
}
|
|
446
461
|
}
|
|
447
462
|
|
|
448
463
|
if (!baseUrl || !provider.apiKey) {
|
|
@@ -491,7 +506,6 @@ export class ProviderService {
|
|
|
491
506
|
const start = Date.now()
|
|
492
507
|
try {
|
|
493
508
|
const { url, headers, body } = buildDirectTestRequest(base, apiKey, modelId, format)
|
|
494
|
-
// 使用 getDirectFetchOptions 以绕开系统代理,测试直接连接
|
|
495
509
|
const directOpts = getDirectFetchOptions()
|
|
496
510
|
const response = await fetch(url, {
|
|
497
511
|
method: 'POST',
|
|
@@ -556,13 +570,13 @@ export class ProviderService {
|
|
|
556
570
|
}
|
|
557
571
|
|
|
558
572
|
// Call upstream with transformed request
|
|
559
|
-
const
|
|
573
|
+
const directOpts = getDirectFetchOptions()
|
|
560
574
|
const response = await fetch(upstreamUrl, {
|
|
561
575
|
method: 'POST',
|
|
562
576
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}` },
|
|
563
577
|
body: JSON.stringify(transformedBody),
|
|
564
578
|
signal: AbortSignal.timeout(30000),
|
|
565
|
-
...
|
|
579
|
+
...directOpts,
|
|
566
580
|
})
|
|
567
581
|
|
|
568
582
|
if (!response.ok) {
|
package/src/utils/proxy.ts
CHANGED
|
@@ -334,26 +334,14 @@ export function getDirectFetchOptions(): {
|
|
|
334
334
|
return { ...base, proxy: undefined, ...getTLSFetchOptions() }
|
|
335
335
|
}
|
|
336
336
|
|
|
337
|
-
// Check if system proxy exists
|
|
338
|
-
const proxyUrl = getProxyUrl()
|
|
339
|
-
if (!proxyUrl) {
|
|
340
|
-
// No proxy configured, just return normal fetch options
|
|
341
|
-
return { ...base, ...getTLSFetchOptions() }
|
|
342
|
-
}
|
|
343
|
-
|
|
344
337
|
// In Node.js/undici, a fresh Agent with no proxy settings bypasses system defaults
|
|
345
338
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
346
339
|
const undiciMod = require('undici') as typeof undici
|
|
347
340
|
const tlsOpts = getTLSFetchOptions()
|
|
348
341
|
|
|
349
|
-
// Use the global dispatcher's options if possible, or fresh default options
|
|
350
|
-
const agentOptions = tlsOpts.dispatcher && 'options' in (tlsOpts.dispatcher as any)
|
|
351
|
-
? (tlsOpts.dispatcher as any).options
|
|
352
|
-
: {}
|
|
353
|
-
|
|
354
342
|
return {
|
|
355
343
|
...base,
|
|
356
|
-
dispatcher: new undiciMod.Agent(
|
|
344
|
+
dispatcher: new undiciMod.Agent(tlsOpts.dispatcher ? (tlsOpts.dispatcher as any).options : {}),
|
|
357
345
|
}
|
|
358
346
|
}
|
|
359
347
|
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Anthropic-to-Anthropic SSE stream labeler.
|
|
3
|
-
*
|
|
4
|
-
* Intercepts an Anthropic Messages API stream and replaces the 'model' field
|
|
5
|
-
* in the 'message_start' event with a custom label.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export function anthropicStreamLabeler(
|
|
9
|
-
upstream: ReadableStream<Uint8Array>,
|
|
10
|
-
label: string,
|
|
11
|
-
): ReadableStream<Uint8Array> {
|
|
12
|
-
const encoder = new TextEncoder()
|
|
13
|
-
const decoder = new TextDecoder()
|
|
14
|
-
let buffer = ''
|
|
15
|
-
|
|
16
|
-
return new ReadableStream({
|
|
17
|
-
async start(controller) {
|
|
18
|
-
const reader = upstream.getReader()
|
|
19
|
-
try {
|
|
20
|
-
while (true) {
|
|
21
|
-
const { done, value } = await reader.read()
|
|
22
|
-
if (done) break
|
|
23
|
-
|
|
24
|
-
buffer += decoder.decode(value, { stream: true })
|
|
25
|
-
const lines = buffer.split('\n')
|
|
26
|
-
buffer = lines.pop() || ''
|
|
27
|
-
|
|
28
|
-
for (const line of lines) {
|
|
29
|
-
const trimmed = line.trim()
|
|
30
|
-
if (!trimmed || !trimmed.startsWith('data: ')) {
|
|
31
|
-
controller.enqueue(encoder.encode(line + '\n'))
|
|
32
|
-
continue
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const jsonStr = trimmed.slice(6)
|
|
36
|
-
try {
|
|
37
|
-
const data = JSON.parse(jsonStr)
|
|
38
|
-
if (data.type === 'message_start' && data.message) {
|
|
39
|
-
data.message.model = label
|
|
40
|
-
controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n`))
|
|
41
|
-
} else {
|
|
42
|
-
controller.enqueue(encoder.encode(line + '\n'))
|
|
43
|
-
}
|
|
44
|
-
} catch {
|
|
45
|
-
controller.enqueue(encoder.encode(line + '\n'))
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
} catch (err) {
|
|
50
|
-
controller.error(err)
|
|
51
|
-
} finally {
|
|
52
|
-
controller.close()
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
})
|
|
56
|
-
}
|