aicodeswitch 5.1.0 → 5.1.2

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.
@@ -2761,10 +2761,10 @@ class ProxyServer {
2761
2761
  * @param targetModel 目标模型名称(可选)
2762
2762
  * @returns 转换后往服务商API接口的数据
2763
2763
  */
2764
- transformRequestToUpstream(tool, source, payloadData, targetModel, providerConfig) {
2764
+ transformRequestToUpstream(tool, source, payloadData, targetModel, providerConfig, serverToolConfig) {
2765
2765
  const clientFormat = tool === 'codex' ? 'responses' : 'claude';
2766
2766
  const upstreamFormat = (0, index_1.sourceTypeToFormat)(source);
2767
- const result = (0, index_1.transformRequest)({ fromFormat: clientFormat, toFormat: upstreamFormat, body: payloadData, providerConfig });
2767
+ const result = (0, index_1.transformRequest)({ fromFormat: clientFormat, toFormat: upstreamFormat, body: payloadData, providerConfig, serverToolConfig });
2768
2768
  const body = result.body;
2769
2769
  // 模型覆盖:OpenAI 模型族保持原样,其余覆盖为 targetModel
2770
2770
  if (targetModel) {
@@ -2898,6 +2898,12 @@ class ProxyServer {
2898
2898
  const useOriginalConfig = (options === null || options === void 0 ? void 0 : options.useOriginalConfig) === true;
2899
2899
  let relayedForLog = !useOriginalConfig;
2900
2900
  let originalToolRequestBody = this.cloneRequestBody(req.body || {});
2901
+ // 请求体安全性清理:修复控制字符、无效 JSON arguments、undefined 值等问题
2902
+ const sanitizeResult = (0, index_1.sanitizeRequestBody)(originalToolRequestBody);
2903
+ if (sanitizeResult.changes.length > 0) {
2904
+ console.log(`[Body-Sanitize] ${sanitizeResult.changes.length} fix(es): ${sanitizeResult.changes.join('; ')}`);
2905
+ }
2906
+ originalToolRequestBody = sanitizeResult.body;
2901
2907
  let requestBody = this.cloneRequestBody(originalToolRequestBody) || {};
2902
2908
  let usageForLog;
2903
2909
  let logged = false;
@@ -3286,7 +3292,8 @@ class ProxyServer {
3286
3292
  const effectiveApiUrl = this.resolveEffectiveApiUrl(service);
3287
3293
  const effectiveModel = rule.targetModel || (requestBody === null || requestBody === void 0 ? void 0 : requestBody.model);
3288
3294
  const providerConfig = (0, index_1.getReasoningConfig)(service.name || '', effectiveApiUrl || '', effectiveModel || '');
3289
- const transformedRequestBody = this.transformRequestToUpstream(targetType, sourceType, payloadForTransform, rule.targetModel, providerConfig);
3295
+ const serverToolConfig = (0, index_1.getServerToolSupport)(service.name || '', effectiveApiUrl || '');
3296
+ const transformedRequestBody = this.transformRequestToUpstream(targetType, sourceType, payloadForTransform, rule.targetModel, providerConfig, serverToolConfig);
3290
3297
  requestBody = (_b = transformedRequestBody !== null && transformedRequestBody !== void 0 ? transformedRequestBody : this.cloneRequestBody(originalToolRequestBody)) !== null && _b !== void 0 ? _b : {};
3291
3298
  // 对最终即将发送到上游的 Claude compact 请求再做一次兜底清理,
3292
3299
  // 避免中间转换/覆盖步骤重新引入未配对的 tool_use。
@@ -3921,6 +3928,12 @@ class ProxyServer {
3921
3928
  console.log(`\x1b[32m[ApiPathProxy]\x1b[0m path=${apiPath}, clientFormat=${clientFormat}, session=-, rule=${rule.id}(${rule.contentType}), vendor=${(vendor === null || vendor === void 0 ? void 0 : vendor.name) || '-'}, service=${service.name}`);
3922
3929
  const failoverEnabled = (options === null || options === void 0 ? void 0 : options.failoverEnabled) === true;
3923
3930
  let requestBody = this.cloneRequestBody(req.body || {});
3931
+ // 请求体安全性清理:修复控制字符、无效 JSON arguments、undefined 值等问题
3932
+ const sanitizeResult = (0, index_1.sanitizeRequestBody)(requestBody);
3933
+ if (sanitizeResult.changes.length > 0) {
3934
+ console.log(`[Body-Sanitize] ${sanitizeResult.changes.length} fix(es): ${sanitizeResult.changes.join('; ')}`);
3935
+ }
3936
+ requestBody = sanitizeResult.body;
3924
3937
  let usageForLog;
3925
3938
  let responseBodyForLog;
3926
3939
  let downstreamResponseBodyForLog;
@@ -3972,7 +3985,8 @@ class ProxyServer {
3972
3985
  const effectiveApiUrl = this.resolveEffectiveApiUrl(service);
3973
3986
  const effectiveModel = rule.targetModel || (requestBody === null || requestBody === void 0 ? void 0 : requestBody.model);
3974
3987
  const providerConfig = (0, index_1.getReasoningConfig)(service.name || '', effectiveApiUrl || '', effectiveModel || '');
3975
- const transformedRequestBody = this.transformRequestByFormat(clientFormat, sourceType, payloadForTransform, rule.targetModel, providerConfig);
3988
+ const serverToolConfig = (0, index_1.getServerToolSupport)(service.name || '', effectiveApiUrl || '');
3989
+ const transformedRequestBody = this.transformRequestByFormat(clientFormat, sourceType, payloadForTransform, rule.targetModel, providerConfig, serverToolConfig);
3976
3990
  requestBody = (_a = transformedRequestBody !== null && transformedRequestBody !== void 0 ? transformedRequestBody : this.cloneRequestBody(requestBody)) !== null && _a !== void 0 ? _a : {};
3977
3991
  // Compact final sanitize
3978
3992
  if (rule.contentType === 'compact' && clientFormat === 'claude' && Array.isArray(requestBody === null || requestBody === void 0 ? void 0 : requestBody.messages)) {
@@ -4184,9 +4198,9 @@ class ProxyServer {
4184
4198
  /**
4185
4199
  * 使用显式 clientFormat 进行请求转换(取代 tool → format 的硬编码映射)
4186
4200
  */
4187
- transformRequestByFormat(clientFormat, source, payloadData, targetModel, providerConfig) {
4201
+ transformRequestByFormat(clientFormat, source, payloadData, targetModel, providerConfig, serverToolConfig) {
4188
4202
  const upstreamFormat = (0, index_1.sourceTypeToFormat)(source);
4189
- const result = (0, index_1.transformRequest)({ fromFormat: clientFormat, toFormat: upstreamFormat, body: payloadData, providerConfig });
4203
+ const result = (0, index_1.transformRequest)({ fromFormat: clientFormat, toFormat: upstreamFormat, body: payloadData, providerConfig, serverToolConfig });
4190
4204
  const body = result.body;
4191
4205
  if (targetModel) {
4192
4206
  const isOpenAIModel = /^gpt-|o[123]/i.test(targetModel);