aicodeswitch 3.0.5 → 3.0.7

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.
@@ -120,7 +120,8 @@ const writeClaudeConfig = (dbManager) => __awaiter(void 0, void 0, void 0, funct
120
120
  ANTHROPIC_AUTH_TOKEN: config.apiKey || "api_key",
121
121
  ANTHROPIC_API_KEY: "",
122
122
  ANTHROPIC_BASE_URL: `http://${host}:${port}/claude-code`,
123
- API_TIMEOUT_MS: "3000000"
123
+ API_TIMEOUT_MS: "3000000",
124
+ CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: 1
124
125
  }
125
126
  };
126
127
  fs_1.default.writeFileSync(claudeSettingsPath, JSON.stringify(claudeSettings, null, 2));
@@ -1145,11 +1145,11 @@ class ProxyServer {
1145
1145
  const accept = typeof req.headers.accept === 'string' ? req.headers.accept : '';
1146
1146
  return (body === null || body === void 0 ? void 0 : body.stream) === true || accept.includes('text/event-stream');
1147
1147
  }
1148
- buildUpstreamHeaders(req, service, sourceType, streamRequested) {
1148
+ buildUpstreamHeaders(req, service, sourceType, streamRequested, requestBody) {
1149
1149
  const headers = {};
1150
1150
  for (const [key, value] of Object.entries(req.headers)) {
1151
1151
  // 排除原始认证头,防止与代理设置的认证头冲突
1152
- if (['host', 'connection', 'content-length', 'authorization', 'x-api-key', 'x-anthropic-api-key', 'anthropic-api-key', 'x-goog-api-key'].includes(key.toLowerCase())) {
1152
+ if (['host', 'content-length', 'authorization', 'x-api-key', 'x-anthropic-api-key', 'anthropic-api-key', 'x-goog-api-key'].includes(key.toLowerCase())) {
1153
1153
  continue;
1154
1154
  }
1155
1155
  if (typeof value === 'string') {
@@ -1159,32 +1159,44 @@ class ProxyServer {
1159
1159
  headers[key] = value.join(', ');
1160
1160
  }
1161
1161
  }
1162
- if (streamRequested) {
1163
- headers.accept = 'text/event-stream';
1164
- }
1165
- // 确定认证方式:优先使用服务配置的 authType,否则根据 sourceType 自动判断
1166
- const authType = service.authType || types_1.AuthType.AUTO;
1167
- if (authType === types_1.AuthType.G_API_KEY || (authType === types_1.AuthType.AUTO && this.isGeminiSource(sourceType))) {
1168
- // 使用 x-goog-api-key 认证(适用于 Google Gemini API)
1162
+ // 确定认证方式:优先使用服务配置的 authType
1163
+ // 注意:向下兼容 'auto' 字符串值(前端已移除 AuthType.AUTO 枚举,但旧数据可能包含此值)
1164
+ const authType = service.authType || types_1.AuthType.AUTH_TOKEN;
1165
+ // 向下兼容:检测旧数据的 'auto'
1166
+ // TODO: 删除
1167
+ const isAuto = authType === 'auto';
1168
+ // 使用 x-goog-api-key 认证(适用于 Google Gemini API)
1169
+ if (authType === types_1.AuthType.G_API_KEY || (isAuto && this.isGeminiSource(sourceType))) {
1169
1170
  headers['x-goog-api-key'] = service.apiKey;
1170
1171
  }
1171
- else if (authType === types_1.AuthType.API_KEY || (authType === types_1.AuthType.AUTO && this.isClaudeSource(sourceType))) {
1172
- // 使用 x-api-key 认证(适用于 claude-chat, claude-code 及某些需要 x-api-key openai-chat 兼容 API)
1172
+ // 使用 x-api-key 认证(适用于 claude-chat, claude-code 及某些需要 x-api-key openai-chat 兼容 API)
1173
+ else if (authType === types_1.AuthType.API_KEY || (isAuto && this.isClaudeSource(sourceType))) {
1173
1174
  headers['x-api-key'] = service.apiKey;
1174
1175
  if (this.isClaudeSource(sourceType) || authType === types_1.AuthType.API_KEY) {
1175
1176
  // 仅在明确配置或 Claude 源时添加 anthropic-version
1176
1177
  headers['anthropic-version'] = headers['anthropic-version'] || '2023-06-01';
1177
1178
  }
1178
1179
  }
1180
+ // 使用 Authorization 认证(适用于 openai-chat, openai-responses, deepseek-reasoning-chat 等)
1179
1181
  else {
1180
- // 使用 Authorization 认证(适用于 openai-chat, openai-responses, deepseek-reasoning-chat 等)
1181
- delete headers['anthropic-version'];
1182
- delete headers['anthropic-beta'];
1183
1182
  headers.authorization = `Bearer ${service.apiKey}`;
1184
1183
  }
1184
+ if (streamRequested && !headers.accept) {
1185
+ headers.accept = 'text/event-stream';
1186
+ }
1187
+ if (!headers.connection) {
1188
+ if (streamRequested) {
1189
+ headers.connection = 'keep-alive';
1190
+ }
1191
+ }
1185
1192
  if (!headers['content-type']) {
1186
1193
  headers['content-type'] = 'application/json';
1187
1194
  }
1195
+ // 添加 content-length(对于有请求体的方法)
1196
+ if (requestBody && ['POST', 'PUT', 'PATCH'].includes(req.method.toUpperCase())) {
1197
+ const bodyStr = JSON.stringify(requestBody);
1198
+ headers['content-length'] = Buffer.byteLength(bodyStr, 'utf8').toString();
1199
+ }
1188
1200
  return headers;
1189
1201
  }
1190
1202
  copyResponseHeaders(responseHeaders, res) {
@@ -1587,7 +1599,7 @@ class ProxyServer {
1587
1599
  const config = {
1588
1600
  method: req.method,
1589
1601
  url: upstreamUrl,
1590
- headers: this.buildUpstreamHeaders(req, service, sourceType, streamRequested),
1602
+ headers: this.buildUpstreamHeaders(req, service, sourceType, streamRequested, requestBody),
1591
1603
  timeout: rule.timeout || 3000000, // 默认300秒
1592
1604
  validateStatus: () => true,
1593
1605
  responseType: streamRequested ? 'stream' : 'json',
@@ -1631,7 +1643,7 @@ class ProxyServer {
1631
1643
  // const actualModel = requestBody?.model || '';
1632
1644
  // const maxTokensFieldName = this.getMaxTokensFieldName(actualModel);
1633
1645
  // const actualMaxTokens = requestBody?.[maxTokensFieldName] || requestBody?.max_tokens;
1634
- const upstreamHeaders = this.buildUpstreamHeaders(req, service, sourceType, streamRequested);
1646
+ const upstreamHeaders = this.buildUpstreamHeaders(req, service, sourceType, streamRequested, requestBody);
1635
1647
  upstreamRequestForLog = {
1636
1648
  url: upstreamUrl,
1637
1649
  // model: actualModel,
@@ -7,6 +7,7 @@ var AuthType;
7
7
  AuthType["AUTH_TOKEN"] = "authorization";
8
8
  AuthType["API_KEY"] = "x-api-key";
9
9
  AuthType["G_API_KEY"] = "x-goog-api-key";
10
- AuthType["AUTO"] = "auto";
10
+ // 注意: 'auto' 值已从前端移除,但后端仍保留兼容性处理
11
+ // AUTO = 'auto', // 已废弃
11
12
  })(AuthType || (exports.AuthType = AuthType = {}));
12
13
  ;