ai-protocol-adapters 1.0.0-alpha.5 → 1.0.0-alpha.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.
package/dist/index.d.mts CHANGED
@@ -38,6 +38,7 @@ interface ConversionResult$2 {
38
38
  declare class StreamingProtocolAdapter {
39
39
  private config;
40
40
  constructor(options?: StreamingProtocolAdapterOptions);
41
+ private logDebug;
41
42
  /**
42
43
  * 转换Anthropic请求为OpenAI格式
43
44
  */
@@ -5765,6 +5766,7 @@ declare class StandardProtocolAdapter {
5765
5766
  private debugMode;
5766
5767
  private sseAdapter;
5767
5768
  constructor(options?: StandardProtocolAdapterOptions);
5769
+ private logDebug;
5768
5770
  /**
5769
5771
  * 转换Anthropic请求为OpenAI请求格式
5770
5772
  * @param anthropicRequest - Anthropic格式的请求
package/dist/index.d.ts CHANGED
@@ -38,6 +38,7 @@ interface ConversionResult$2 {
38
38
  declare class StreamingProtocolAdapter {
39
39
  private config;
40
40
  constructor(options?: StreamingProtocolAdapterOptions);
41
+ private logDebug;
41
42
  /**
42
43
  * 转换Anthropic请求为OpenAI格式
43
44
  */
@@ -5765,6 +5766,7 @@ declare class StandardProtocolAdapter {
5765
5766
  private debugMode;
5766
5767
  private sseAdapter;
5767
5768
  constructor(options?: StandardProtocolAdapterOptions);
5769
+ private logDebug;
5768
5770
  /**
5769
5771
  * 转换Anthropic请求为OpenAI请求格式
5770
5772
  * @param anthropicRequest - Anthropic格式的请求
package/dist/index.js CHANGED
@@ -518,6 +518,11 @@ var StreamingProtocolAdapter = class {
518
518
  logger: options.logger ?? getGlobalLogger()
519
519
  };
520
520
  }
521
+ logDebug(message, meta) {
522
+ if (this.config.debugMode) {
523
+ this.config.logger.debug(message, meta);
524
+ }
525
+ }
521
526
  /**
522
527
  * 转换Anthropic请求为OpenAI格式
523
528
  */
@@ -630,8 +635,8 @@ var StreamingProtocolAdapter = class {
630
635
  const hasToolCalls = choice.delta?.tool_calls;
631
636
  const hasFinishReason = choice.finish_reason;
632
637
  const isNonText = !choice.delta?.content;
633
- if (hasToolCalls || hasFinishReason || isNonText && choice.delta) {
634
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [StreamingProtocolAdapter] CHUNK:", JSON.stringify(chunk, null, 2));
638
+ if (this.config.debugMode && (hasToolCalls || hasFinishReason || isNonText && choice.delta)) {
639
+ this.logDebug("Streaming chunk processed", { chunk });
635
640
  }
636
641
  }
637
642
  if (!choice) return;
@@ -698,13 +703,13 @@ var StreamingProtocolAdapter = class {
698
703
  * - Chunk N: 继续累积arguments
699
704
  */
700
705
  processToolCalls(toolCalls, state, sseLines) {
701
- console.log("\u{1F50D} [StreamingProtocolAdapter] processToolCalls called:", JSON.stringify(toolCalls, null, 2));
706
+ this.logDebug("processToolCalls called", { toolCalls });
702
707
  for (const toolCall of toolCalls) {
703
708
  const index = toolCall.index ?? 0;
704
709
  const toolId = toolCall.id;
705
710
  const toolName = toolCall.function?.name;
706
711
  const toolArgs = toolCall.function?.arguments;
707
- console.log(`\u{1F50D} [StreamingProtocolAdapter] Processing chunk for index ${index}:`, {
712
+ this.logDebug(`Processing tool chunk for index ${index}`, {
708
713
  hasId: !!toolId,
709
714
  hasName: !!toolName,
710
715
  hasArgs: !!toolArgs,
@@ -727,9 +732,8 @@ var StreamingProtocolAdapter = class {
727
732
  }
728
733
  if (toolArgs) {
729
734
  toolData.input += toolArgs;
730
- console.log(`\u{1F50D} [StreamingProtocolAdapter] Accumulated arguments for index ${index}:`, {
731
- currentLength: toolData.input.length,
732
- totalArgs: toolData.input
735
+ this.logDebug(`Accumulated tool arguments for index ${index}`, {
736
+ currentLength: toolData.input.length
733
737
  });
734
738
  }
735
739
  if (toolData.id && toolData.name && !toolData.blockStartSent) {
@@ -741,7 +745,7 @@ var StreamingProtocolAdapter = class {
741
745
  ""
742
746
  );
743
747
  toolData.blockStartSent = true;
744
- console.log(`\u2705 [StreamingProtocolAdapter] Sent content_block_start for ${toolData.name} at index ${blockIndex}`);
748
+ this.logDebug("Sent content_block_start", { toolName: toolData.name, blockIndex });
745
749
  }
746
750
  if (toolArgs && toolData.blockStartSent && toolData.blockIndex !== void 0) {
747
751
  sseLines.push(
@@ -749,7 +753,7 @@ var StreamingProtocolAdapter = class {
749
753
  `data: {"type":"content_block_delta","index":${toolData.blockIndex},"delta":{"type":"input_json_delta","partial_json":${JSON.stringify(toolArgs)}}}`,
750
754
  ""
751
755
  );
752
- console.log(`\u2705 [StreamingProtocolAdapter] Sent input_json_delta for index ${toolData.blockIndex}`);
756
+ this.logDebug("Sent input_json_delta", { blockIndex: toolData.blockIndex });
753
757
  }
754
758
  }
755
759
  }
@@ -766,7 +770,7 @@ var StreamingProtocolAdapter = class {
766
770
  );
767
771
  toolData.blockStopSent = true;
768
772
  state.completedToolCalls.push(toolData.id);
769
- console.log(`\u2705 [StreamingProtocolAdapter] Sent content_block_stop for ${toolData.name} at index ${toolData.blockIndex}`);
773
+ this.logDebug("Sent content_block_stop", { toolName: toolData.name, blockIndex: toolData.blockIndex });
770
774
  }
771
775
  }
772
776
  }
@@ -1012,6 +1016,14 @@ var MessageConverter = class {
1012
1016
  * 使用tool_use_id溯回工具名称解决unknown_tool问题
1013
1017
  */
1014
1018
  static convertMessages(messages, system) {
1019
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
1020
+ if (debugEnabled) {
1021
+ if (system !== void 0) {
1022
+ console.debug("[MessageConverter] convertMessages called with system:", JSON.stringify(system, null, 2));
1023
+ } else {
1024
+ console.debug("[MessageConverter] convertMessages called WITHOUT system parameter");
1025
+ }
1026
+ }
1015
1027
  const context = this.createConversionContext(messages);
1016
1028
  const convertedMessages = [];
1017
1029
  for (const msg of messages) {
@@ -1030,8 +1042,15 @@ var MessageConverter = class {
1030
1042
  const systemMessage = this.processSystemMessage(system);
1031
1043
  if (systemMessage) {
1032
1044
  convertedMessages.unshift(systemMessage);
1045
+ if (debugEnabled) {
1046
+ console.debug("[MessageConverter] System message added to messages array at index 0");
1047
+ }
1033
1048
  }
1034
1049
  }
1050
+ if (debugEnabled) {
1051
+ console.debug("[MessageConverter] Final converted messages count:", convertedMessages.length);
1052
+ console.debug("[MessageConverter] First message:", JSON.stringify(convertedMessages[0], null, 2));
1053
+ }
1035
1054
  return convertedMessages;
1036
1055
  }
1037
1056
  /**
@@ -4328,25 +4347,31 @@ var ToolCallProcessor = class _ToolCallProcessor {
4328
4347
  * 处理增量工具调用
4329
4348
  */
4330
4349
  static processIncrementalToolCalls(toolCalls, state, sseLines) {
4331
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4350
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
4351
+ if (debugEnabled) {
4352
+ console.debug("[ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4353
+ }
4332
4354
  for (const toolCall of toolCalls) {
4333
4355
  const toolId = toolCall.id;
4334
4356
  const toolName = toolCall.function?.name;
4335
4357
  const toolArgs = toolCall.function?.arguments;
4336
- console.log("\u{1F50D} [ToolProcessor] Processing tool call:", {
4337
- toolId,
4338
- toolName,
4339
- toolArgs: toolArgs || "UNDEFINED",
4340
- toolArgsType: typeof toolArgs,
4341
- toolArgsLength: toolArgs?.length || 0,
4342
- hasArgs: !!toolArgs
4343
- });
4358
+ if (debugEnabled) {
4359
+ console.debug("[ToolProcessor] Processing tool call:", {
4360
+ toolId,
4361
+ toolName,
4362
+ hasArgs: !!toolArgs
4363
+ });
4364
+ }
4344
4365
  if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
4345
- console.log("\u2705 [ToolProcessor] Starting new tool call:", toolName);
4366
+ if (debugEnabled) {
4367
+ console.debug("[ToolProcessor] Starting new tool call:", toolName);
4368
+ }
4346
4369
  _ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
4347
4370
  }
4348
4371
  if (toolArgs) {
4349
- console.log("\u2705 [ToolProcessor] Processing tool args, calling processToolArgs");
4372
+ if (debugEnabled) {
4373
+ console.debug("[ToolProcessor] Processing tool args, calling processToolArgs");
4374
+ }
4350
4375
  _ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
4351
4376
  } else {
4352
4377
  console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No tool args to process! This will result in empty input!");
@@ -4357,25 +4382,31 @@ var ToolCallProcessor = class _ToolCallProcessor {
4357
4382
  * 处理工具调用
4358
4383
  */
4359
4384
  static processBatchToolCalls(toolCalls, state, sseLines) {
4360
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4385
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
4386
+ if (debugEnabled) {
4387
+ console.debug("[ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4388
+ }
4361
4389
  for (const toolCall of toolCalls) {
4362
4390
  const toolId = toolCall.id;
4363
4391
  const toolName = toolCall.function?.name;
4364
4392
  const toolArgs = toolCall.function?.arguments;
4365
- console.log("\u{1F50D} [ToolProcessor] Processing batch tool call:", {
4366
- toolId,
4367
- toolName,
4368
- toolArgs: toolArgs || "UNDEFINED",
4369
- toolArgsType: typeof toolArgs,
4370
- toolArgsLength: toolArgs?.length || 0,
4371
- hasArgs: !!toolArgs
4372
- });
4393
+ if (debugEnabled) {
4394
+ console.debug("[ToolProcessor] Processing batch tool call:", {
4395
+ toolId,
4396
+ toolName,
4397
+ hasArgs: !!toolArgs
4398
+ });
4399
+ }
4373
4400
  if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
4374
- console.log("\u2705 [ToolProcessor] Starting new batch tool call:", toolName);
4401
+ if (debugEnabled) {
4402
+ console.debug("[ToolProcessor] Starting new batch tool call:", toolName);
4403
+ }
4375
4404
  _ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
4376
4405
  }
4377
4406
  if (toolArgs) {
4378
- console.log("\u2705 [ToolProcessor] Processing batch tool args, calling processToolArgs");
4407
+ if (debugEnabled) {
4408
+ console.debug("[ToolProcessor] Processing batch tool args, calling processToolArgs");
4409
+ }
4379
4410
  _ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
4380
4411
  } else {
4381
4412
  console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No batch tool args to process! This will result in empty input!");
@@ -5279,9 +5310,14 @@ var O2ASSEAdapterStatic = {
5279
5310
  // src/core/standard/standard-protocol-adapter.ts
5280
5311
  var StandardProtocolAdapter = class {
5281
5312
  constructor(options = {}) {
5282
- this.debugMode = options.debugMode !== void 0 ? options.debugMode : true;
5313
+ this.debugMode = options.debugMode ?? process.env.AI_PROTOCOL_DEBUG === "true";
5283
5314
  this.sseAdapter = new O2ASSEAdapter(this.debugMode);
5284
5315
  }
5316
+ logDebug(message, meta) {
5317
+ if (this.debugMode) {
5318
+ console.debug(message, meta ?? "");
5319
+ }
5320
+ }
5285
5321
  /**
5286
5322
  * 转换Anthropic请求为OpenAI请求格式
5287
5323
  * @param anthropicRequest - Anthropic格式的请求
@@ -5305,7 +5341,7 @@ var StandardProtocolAdapter = class {
5305
5341
  */
5306
5342
  convertFromStreamToStandard(openaiRawStream, modelName, messageId) {
5307
5343
  if (this.debugMode) {
5308
- console.log("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
5344
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
5309
5345
  rawStreamLength: openaiRawStream.length,
5310
5346
  modelName,
5311
5347
  messageId,
@@ -5314,14 +5350,14 @@ var StandardProtocolAdapter = class {
5314
5350
  }
5315
5351
  const sseResult = this.sseAdapter.convertToClaudeSSE(openaiRawStream, modelName, messageId);
5316
5352
  if (this.debugMode) {
5317
- console.log("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
5353
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
5318
5354
  sseResultLength: sseResult.length,
5319
5355
  ssePreview: sseResult.substring(0, 500)
5320
5356
  });
5321
5357
  }
5322
5358
  const standardResponse = this.extractStandardResponseFromSSE(sseResult, modelName, messageId);
5323
5359
  if (this.debugMode) {
5324
- console.log("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
5360
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
5325
5361
  contentLength: standardResponse.content.length,
5326
5362
  usage: standardResponse.usage,
5327
5363
  stopReason: standardResponse.stop_reason
@@ -5336,7 +5372,7 @@ var StandardProtocolAdapter = class {
5336
5372
  const lines = sseContent.split("\n");
5337
5373
  const finalMessageId = messageId || generateMessageId();
5338
5374
  if (this.debugMode) {
5339
- console.log("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
5375
+ this.logDebug("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
5340
5376
  totalLines: lines.length,
5341
5377
  messageId: finalMessageId
5342
5378
  });
@@ -5391,25 +5427,28 @@ var StandardProtocolAdapter = class {
5391
5427
  if (data.type === "content_block_delta" && data.delta?.type === "text_delta") {
5392
5428
  currentTextContent += data.delta.text;
5393
5429
  if (this.debugMode && currentTextContent.length % 50 === 0) {
5394
- console.log(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26):`, currentTextContent.substring(currentTextContent.length - 20));
5430
+ this.logDebug(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26)`, currentTextContent.substring(currentTextContent.length - 20));
5395
5431
  }
5396
5432
  }
5397
5433
  if (data.type === "content_block_delta" && data.delta?.type === "input_json_delta") {
5398
5434
  const toolIndex = data.index;
5399
5435
  const toolId = indexToToolId.get(toolIndex);
5400
- console.log(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
5401
- toolIndex,
5402
- toolId: toolId || "NOT_FOUND",
5403
- delta: data.delta.partial_json
5404
- });
5436
+ if (this.debugMode) {
5437
+ this.logDebug(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
5438
+ toolIndex,
5439
+ toolId: toolId || "NOT_FOUND",
5440
+ delta: data.delta.partial_json
5441
+ });
5442
+ }
5405
5443
  if (toolId) {
5406
5444
  const currentBuffer = toolInputBuffers.get(toolIndex) || "";
5407
5445
  const newBuffer = currentBuffer + data.delta.partial_json;
5408
5446
  toolInputBuffers.set(toolIndex, newBuffer);
5409
- console.log(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId}):`, {
5410
- delta: data.delta.partial_json,
5411
- bufferLength: newBuffer.length
5412
- });
5447
+ if (this.debugMode) {
5448
+ this.logDebug(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId})`, {
5449
+ bufferLength: newBuffer.length
5450
+ });
5451
+ }
5413
5452
  } else {
5414
5453
  console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u627E\u4E0D\u5230toolId for index=${toolIndex}`);
5415
5454
  }
@@ -5425,7 +5464,7 @@ var StandardProtocolAdapter = class {
5425
5464
  const parsedInput = JSON.parse(jsonBuffer);
5426
5465
  tool.input = parsedInput;
5427
5466
  if (this.debugMode) {
5428
- console.log(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId}):`, parsedInput);
5467
+ this.logDebug(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId})`, parsedInput);
5429
5468
  }
5430
5469
  } catch (parseError) {
5431
5470
  console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570JSON\u89E3\u6790\u5931\u8D25 (index=${toolIndex}, id=${toolId}):`, {
@@ -5443,7 +5482,7 @@ var StandardProtocolAdapter = class {
5443
5482
  if (data.usage) {
5444
5483
  response.usage = data.usage;
5445
5484
  if (this.debugMode) {
5446
- console.log("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
5485
+ this.logDebug("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
5447
5486
  }
5448
5487
  }
5449
5488
  }
@@ -5463,7 +5502,7 @@ var StandardProtocolAdapter = class {
5463
5502
  }
5464
5503
  response.content.push(...Array.from(toolCalls.values()));
5465
5504
  if (this.debugMode) {
5466
- console.log("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
5505
+ this.logDebug("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
5467
5506
  contentCount: response.content.length,
5468
5507
  textLength: currentTextContent.length,
5469
5508
  toolCallsCount: toolCalls.size,
package/dist/index.mjs CHANGED
@@ -411,6 +411,11 @@ var StreamingProtocolAdapter = class {
411
411
  logger: options.logger ?? getGlobalLogger()
412
412
  };
413
413
  }
414
+ logDebug(message, meta) {
415
+ if (this.config.debugMode) {
416
+ this.config.logger.debug(message, meta);
417
+ }
418
+ }
414
419
  /**
415
420
  * 转换Anthropic请求为OpenAI格式
416
421
  */
@@ -523,8 +528,8 @@ var StreamingProtocolAdapter = class {
523
528
  const hasToolCalls = choice.delta?.tool_calls;
524
529
  const hasFinishReason = choice.finish_reason;
525
530
  const isNonText = !choice.delta?.content;
526
- if (hasToolCalls || hasFinishReason || isNonText && choice.delta) {
527
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [StreamingProtocolAdapter] CHUNK:", JSON.stringify(chunk, null, 2));
531
+ if (this.config.debugMode && (hasToolCalls || hasFinishReason || isNonText && choice.delta)) {
532
+ this.logDebug("Streaming chunk processed", { chunk });
528
533
  }
529
534
  }
530
535
  if (!choice) return;
@@ -591,13 +596,13 @@ var StreamingProtocolAdapter = class {
591
596
  * - Chunk N: 继续累积arguments
592
597
  */
593
598
  processToolCalls(toolCalls, state, sseLines) {
594
- console.log("\u{1F50D} [StreamingProtocolAdapter] processToolCalls called:", JSON.stringify(toolCalls, null, 2));
599
+ this.logDebug("processToolCalls called", { toolCalls });
595
600
  for (const toolCall of toolCalls) {
596
601
  const index = toolCall.index ?? 0;
597
602
  const toolId = toolCall.id;
598
603
  const toolName = toolCall.function?.name;
599
604
  const toolArgs = toolCall.function?.arguments;
600
- console.log(`\u{1F50D} [StreamingProtocolAdapter] Processing chunk for index ${index}:`, {
605
+ this.logDebug(`Processing tool chunk for index ${index}`, {
601
606
  hasId: !!toolId,
602
607
  hasName: !!toolName,
603
608
  hasArgs: !!toolArgs,
@@ -620,9 +625,8 @@ var StreamingProtocolAdapter = class {
620
625
  }
621
626
  if (toolArgs) {
622
627
  toolData.input += toolArgs;
623
- console.log(`\u{1F50D} [StreamingProtocolAdapter] Accumulated arguments for index ${index}:`, {
624
- currentLength: toolData.input.length,
625
- totalArgs: toolData.input
628
+ this.logDebug(`Accumulated tool arguments for index ${index}`, {
629
+ currentLength: toolData.input.length
626
630
  });
627
631
  }
628
632
  if (toolData.id && toolData.name && !toolData.blockStartSent) {
@@ -634,7 +638,7 @@ var StreamingProtocolAdapter = class {
634
638
  ""
635
639
  );
636
640
  toolData.blockStartSent = true;
637
- console.log(`\u2705 [StreamingProtocolAdapter] Sent content_block_start for ${toolData.name} at index ${blockIndex}`);
641
+ this.logDebug("Sent content_block_start", { toolName: toolData.name, blockIndex });
638
642
  }
639
643
  if (toolArgs && toolData.blockStartSent && toolData.blockIndex !== void 0) {
640
644
  sseLines.push(
@@ -642,7 +646,7 @@ var StreamingProtocolAdapter = class {
642
646
  `data: {"type":"content_block_delta","index":${toolData.blockIndex},"delta":{"type":"input_json_delta","partial_json":${JSON.stringify(toolArgs)}}}`,
643
647
  ""
644
648
  );
645
- console.log(`\u2705 [StreamingProtocolAdapter] Sent input_json_delta for index ${toolData.blockIndex}`);
649
+ this.logDebug("Sent input_json_delta", { blockIndex: toolData.blockIndex });
646
650
  }
647
651
  }
648
652
  }
@@ -659,7 +663,7 @@ var StreamingProtocolAdapter = class {
659
663
  );
660
664
  toolData.blockStopSent = true;
661
665
  state.completedToolCalls.push(toolData.id);
662
- console.log(`\u2705 [StreamingProtocolAdapter] Sent content_block_stop for ${toolData.name} at index ${toolData.blockIndex}`);
666
+ this.logDebug("Sent content_block_stop", { toolName: toolData.name, blockIndex: toolData.blockIndex });
663
667
  }
664
668
  }
665
669
  }
@@ -905,6 +909,14 @@ var MessageConverter = class {
905
909
  * 使用tool_use_id溯回工具名称解决unknown_tool问题
906
910
  */
907
911
  static convertMessages(messages, system) {
912
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
913
+ if (debugEnabled) {
914
+ if (system !== void 0) {
915
+ console.debug("[MessageConverter] convertMessages called with system:", JSON.stringify(system, null, 2));
916
+ } else {
917
+ console.debug("[MessageConverter] convertMessages called WITHOUT system parameter");
918
+ }
919
+ }
908
920
  const context = this.createConversionContext(messages);
909
921
  const convertedMessages = [];
910
922
  for (const msg of messages) {
@@ -923,8 +935,15 @@ var MessageConverter = class {
923
935
  const systemMessage = this.processSystemMessage(system);
924
936
  if (systemMessage) {
925
937
  convertedMessages.unshift(systemMessage);
938
+ if (debugEnabled) {
939
+ console.debug("[MessageConverter] System message added to messages array at index 0");
940
+ }
926
941
  }
927
942
  }
943
+ if (debugEnabled) {
944
+ console.debug("[MessageConverter] Final converted messages count:", convertedMessages.length);
945
+ console.debug("[MessageConverter] First message:", JSON.stringify(convertedMessages[0], null, 2));
946
+ }
928
947
  return convertedMessages;
929
948
  }
930
949
  /**
@@ -4221,25 +4240,31 @@ var ToolCallProcessor = class _ToolCallProcessor {
4221
4240
  * 处理增量工具调用
4222
4241
  */
4223
4242
  static processIncrementalToolCalls(toolCalls, state, sseLines) {
4224
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4243
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
4244
+ if (debugEnabled) {
4245
+ console.debug("[ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4246
+ }
4225
4247
  for (const toolCall of toolCalls) {
4226
4248
  const toolId = toolCall.id;
4227
4249
  const toolName = toolCall.function?.name;
4228
4250
  const toolArgs = toolCall.function?.arguments;
4229
- console.log("\u{1F50D} [ToolProcessor] Processing tool call:", {
4230
- toolId,
4231
- toolName,
4232
- toolArgs: toolArgs || "UNDEFINED",
4233
- toolArgsType: typeof toolArgs,
4234
- toolArgsLength: toolArgs?.length || 0,
4235
- hasArgs: !!toolArgs
4236
- });
4251
+ if (debugEnabled) {
4252
+ console.debug("[ToolProcessor] Processing tool call:", {
4253
+ toolId,
4254
+ toolName,
4255
+ hasArgs: !!toolArgs
4256
+ });
4257
+ }
4237
4258
  if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
4238
- console.log("\u2705 [ToolProcessor] Starting new tool call:", toolName);
4259
+ if (debugEnabled) {
4260
+ console.debug("[ToolProcessor] Starting new tool call:", toolName);
4261
+ }
4239
4262
  _ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
4240
4263
  }
4241
4264
  if (toolArgs) {
4242
- console.log("\u2705 [ToolProcessor] Processing tool args, calling processToolArgs");
4265
+ if (debugEnabled) {
4266
+ console.debug("[ToolProcessor] Processing tool args, calling processToolArgs");
4267
+ }
4243
4268
  _ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
4244
4269
  } else {
4245
4270
  console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No tool args to process! This will result in empty input!");
@@ -4250,25 +4275,31 @@ var ToolCallProcessor = class _ToolCallProcessor {
4250
4275
  * 处理工具调用
4251
4276
  */
4252
4277
  static processBatchToolCalls(toolCalls, state, sseLines) {
4253
- console.log("\u{1F50D}\u{1F50D}\u{1F50D} [ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4278
+ const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
4279
+ if (debugEnabled) {
4280
+ console.debug("[ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
4281
+ }
4254
4282
  for (const toolCall of toolCalls) {
4255
4283
  const toolId = toolCall.id;
4256
4284
  const toolName = toolCall.function?.name;
4257
4285
  const toolArgs = toolCall.function?.arguments;
4258
- console.log("\u{1F50D} [ToolProcessor] Processing batch tool call:", {
4259
- toolId,
4260
- toolName,
4261
- toolArgs: toolArgs || "UNDEFINED",
4262
- toolArgsType: typeof toolArgs,
4263
- toolArgsLength: toolArgs?.length || 0,
4264
- hasArgs: !!toolArgs
4265
- });
4286
+ if (debugEnabled) {
4287
+ console.debug("[ToolProcessor] Processing batch tool call:", {
4288
+ toolId,
4289
+ toolName,
4290
+ hasArgs: !!toolArgs
4291
+ });
4292
+ }
4266
4293
  if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
4267
- console.log("\u2705 [ToolProcessor] Starting new batch tool call:", toolName);
4294
+ if (debugEnabled) {
4295
+ console.debug("[ToolProcessor] Starting new batch tool call:", toolName);
4296
+ }
4268
4297
  _ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
4269
4298
  }
4270
4299
  if (toolArgs) {
4271
- console.log("\u2705 [ToolProcessor] Processing batch tool args, calling processToolArgs");
4300
+ if (debugEnabled) {
4301
+ console.debug("[ToolProcessor] Processing batch tool args, calling processToolArgs");
4302
+ }
4272
4303
  _ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
4273
4304
  } else {
4274
4305
  console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No batch tool args to process! This will result in empty input!");
@@ -5172,9 +5203,14 @@ var O2ASSEAdapterStatic = {
5172
5203
  // src/core/standard/standard-protocol-adapter.ts
5173
5204
  var StandardProtocolAdapter = class {
5174
5205
  constructor(options = {}) {
5175
- this.debugMode = options.debugMode !== void 0 ? options.debugMode : true;
5206
+ this.debugMode = options.debugMode ?? process.env.AI_PROTOCOL_DEBUG === "true";
5176
5207
  this.sseAdapter = new O2ASSEAdapter(this.debugMode);
5177
5208
  }
5209
+ logDebug(message, meta) {
5210
+ if (this.debugMode) {
5211
+ console.debug(message, meta ?? "");
5212
+ }
5213
+ }
5178
5214
  /**
5179
5215
  * 转换Anthropic请求为OpenAI请求格式
5180
5216
  * @param anthropicRequest - Anthropic格式的请求
@@ -5198,7 +5234,7 @@ var StandardProtocolAdapter = class {
5198
5234
  */
5199
5235
  convertFromStreamToStandard(openaiRawStream, modelName, messageId) {
5200
5236
  if (this.debugMode) {
5201
- console.log("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
5237
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
5202
5238
  rawStreamLength: openaiRawStream.length,
5203
5239
  modelName,
5204
5240
  messageId,
@@ -5207,14 +5243,14 @@ var StandardProtocolAdapter = class {
5207
5243
  }
5208
5244
  const sseResult = this.sseAdapter.convertToClaudeSSE(openaiRawStream, modelName, messageId);
5209
5245
  if (this.debugMode) {
5210
- console.log("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
5246
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
5211
5247
  sseResultLength: sseResult.length,
5212
5248
  ssePreview: sseResult.substring(0, 500)
5213
5249
  });
5214
5250
  }
5215
5251
  const standardResponse = this.extractStandardResponseFromSSE(sseResult, modelName, messageId);
5216
5252
  if (this.debugMode) {
5217
- console.log("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
5253
+ this.logDebug("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
5218
5254
  contentLength: standardResponse.content.length,
5219
5255
  usage: standardResponse.usage,
5220
5256
  stopReason: standardResponse.stop_reason
@@ -5229,7 +5265,7 @@ var StandardProtocolAdapter = class {
5229
5265
  const lines = sseContent.split("\n");
5230
5266
  const finalMessageId = messageId || generateMessageId();
5231
5267
  if (this.debugMode) {
5232
- console.log("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
5268
+ this.logDebug("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
5233
5269
  totalLines: lines.length,
5234
5270
  messageId: finalMessageId
5235
5271
  });
@@ -5284,25 +5320,28 @@ var StandardProtocolAdapter = class {
5284
5320
  if (data.type === "content_block_delta" && data.delta?.type === "text_delta") {
5285
5321
  currentTextContent += data.delta.text;
5286
5322
  if (this.debugMode && currentTextContent.length % 50 === 0) {
5287
- console.log(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26):`, currentTextContent.substring(currentTextContent.length - 20));
5323
+ this.logDebug(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26)`, currentTextContent.substring(currentTextContent.length - 20));
5288
5324
  }
5289
5325
  }
5290
5326
  if (data.type === "content_block_delta" && data.delta?.type === "input_json_delta") {
5291
5327
  const toolIndex = data.index;
5292
5328
  const toolId = indexToToolId.get(toolIndex);
5293
- console.log(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
5294
- toolIndex,
5295
- toolId: toolId || "NOT_FOUND",
5296
- delta: data.delta.partial_json
5297
- });
5329
+ if (this.debugMode) {
5330
+ this.logDebug(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
5331
+ toolIndex,
5332
+ toolId: toolId || "NOT_FOUND",
5333
+ delta: data.delta.partial_json
5334
+ });
5335
+ }
5298
5336
  if (toolId) {
5299
5337
  const currentBuffer = toolInputBuffers.get(toolIndex) || "";
5300
5338
  const newBuffer = currentBuffer + data.delta.partial_json;
5301
5339
  toolInputBuffers.set(toolIndex, newBuffer);
5302
- console.log(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId}):`, {
5303
- delta: data.delta.partial_json,
5304
- bufferLength: newBuffer.length
5305
- });
5340
+ if (this.debugMode) {
5341
+ this.logDebug(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId})`, {
5342
+ bufferLength: newBuffer.length
5343
+ });
5344
+ }
5306
5345
  } else {
5307
5346
  console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u627E\u4E0D\u5230toolId for index=${toolIndex}`);
5308
5347
  }
@@ -5318,7 +5357,7 @@ var StandardProtocolAdapter = class {
5318
5357
  const parsedInput = JSON.parse(jsonBuffer);
5319
5358
  tool.input = parsedInput;
5320
5359
  if (this.debugMode) {
5321
- console.log(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId}):`, parsedInput);
5360
+ this.logDebug(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId})`, parsedInput);
5322
5361
  }
5323
5362
  } catch (parseError) {
5324
5363
  console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570JSON\u89E3\u6790\u5931\u8D25 (index=${toolIndex}, id=${toolId}):`, {
@@ -5336,7 +5375,7 @@ var StandardProtocolAdapter = class {
5336
5375
  if (data.usage) {
5337
5376
  response.usage = data.usage;
5338
5377
  if (this.debugMode) {
5339
- console.log("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
5378
+ this.logDebug("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
5340
5379
  }
5341
5380
  }
5342
5381
  }
@@ -5356,7 +5395,7 @@ var StandardProtocolAdapter = class {
5356
5395
  }
5357
5396
  response.content.push(...Array.from(toolCalls.values()));
5358
5397
  if (this.debugMode) {
5359
- console.log("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
5398
+ this.logDebug("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
5360
5399
  contentCount: response.content.length,
5361
5400
  textLength: currentTextContent.length,
5362
5401
  toolCallsCount: toolCalls.size,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-protocol-adapters",
3
- "version": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.7",
4
4
  "description": "Universal AI Protocol Converter - OpenAI ⇄ Anthropic with full TypeScript support",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",