ai-protocol-adapters 1.0.0-alpha.5 → 1.0.0-alpha.8
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 +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +111 -51
- package/dist/index.mjs +111 -51
- package/package.json +1 -1
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,9 +1042,33 @@ 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
|
}
|
|
1035
|
-
|
|
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
|
+
}
|
|
1054
|
+
return convertedMessages.map((msg) => {
|
|
1055
|
+
if (Array.isArray(msg.tools)) {
|
|
1056
|
+
msg.tools = msg.tools.map((tool) => {
|
|
1057
|
+
if (tool?.type === "function" && tool.function) {
|
|
1058
|
+
const description = tool.function.description?.trim() || "Converted tool with no description provided.";
|
|
1059
|
+
return {
|
|
1060
|
+
...tool,
|
|
1061
|
+
function: {
|
|
1062
|
+
...tool.function,
|
|
1063
|
+
description
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
return tool;
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
return msg;
|
|
1071
|
+
});
|
|
1036
1072
|
}
|
|
1037
1073
|
/**
|
|
1038
1074
|
* 创建消息转换上下文
|
|
@@ -4328,26 +4364,34 @@ var ToolCallProcessor = class _ToolCallProcessor {
|
|
|
4328
4364
|
* 处理增量工具调用
|
|
4329
4365
|
*/
|
|
4330
4366
|
static processIncrementalToolCalls(toolCalls, state, sseLines) {
|
|
4331
|
-
|
|
4367
|
+
const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
|
|
4368
|
+
if (debugEnabled) {
|
|
4369
|
+
console.debug("[ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
|
|
4370
|
+
}
|
|
4332
4371
|
for (const toolCall of toolCalls) {
|
|
4333
4372
|
const toolId = toolCall.id;
|
|
4334
4373
|
const toolName = toolCall.function?.name;
|
|
4335
4374
|
const toolArgs = toolCall.function?.arguments;
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
});
|
|
4375
|
+
if (debugEnabled) {
|
|
4376
|
+
console.debug("[ToolProcessor] Processing tool call:", {
|
|
4377
|
+
toolId,
|
|
4378
|
+
toolName,
|
|
4379
|
+
hasArgs: !!toolArgs
|
|
4380
|
+
});
|
|
4381
|
+
}
|
|
4344
4382
|
if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
|
|
4345
|
-
|
|
4383
|
+
if (debugEnabled) {
|
|
4384
|
+
console.debug("[ToolProcessor] Starting new tool call:", toolName);
|
|
4385
|
+
}
|
|
4346
4386
|
_ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
|
|
4347
4387
|
}
|
|
4348
4388
|
if (toolArgs) {
|
|
4349
|
-
|
|
4389
|
+
if (debugEnabled) {
|
|
4390
|
+
console.debug("[ToolProcessor] Processing tool args, calling processToolArgs");
|
|
4391
|
+
}
|
|
4350
4392
|
_ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
|
|
4393
|
+
} else if (toolName && toolId) {
|
|
4394
|
+
_ToolCallProcessor.processToolArgs(toolId, "", state, sseLines);
|
|
4351
4395
|
} else {
|
|
4352
4396
|
console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No tool args to process! This will result in empty input!");
|
|
4353
4397
|
}
|
|
@@ -4357,26 +4401,34 @@ var ToolCallProcessor = class _ToolCallProcessor {
|
|
|
4357
4401
|
* 处理工具调用
|
|
4358
4402
|
*/
|
|
4359
4403
|
static processBatchToolCalls(toolCalls, state, sseLines) {
|
|
4360
|
-
|
|
4404
|
+
const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
|
|
4405
|
+
if (debugEnabled) {
|
|
4406
|
+
console.debug("[ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
|
|
4407
|
+
}
|
|
4361
4408
|
for (const toolCall of toolCalls) {
|
|
4362
4409
|
const toolId = toolCall.id;
|
|
4363
4410
|
const toolName = toolCall.function?.name;
|
|
4364
4411
|
const toolArgs = toolCall.function?.arguments;
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
});
|
|
4412
|
+
if (debugEnabled) {
|
|
4413
|
+
console.debug("[ToolProcessor] Processing batch tool call:", {
|
|
4414
|
+
toolId,
|
|
4415
|
+
toolName,
|
|
4416
|
+
hasArgs: !!toolArgs
|
|
4417
|
+
});
|
|
4418
|
+
}
|
|
4373
4419
|
if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
|
|
4374
|
-
|
|
4420
|
+
if (debugEnabled) {
|
|
4421
|
+
console.debug("[ToolProcessor] Starting new batch tool call:", toolName);
|
|
4422
|
+
}
|
|
4375
4423
|
_ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
|
|
4376
4424
|
}
|
|
4377
4425
|
if (toolArgs) {
|
|
4378
|
-
|
|
4426
|
+
if (debugEnabled) {
|
|
4427
|
+
console.debug("[ToolProcessor] Processing batch tool args, calling processToolArgs");
|
|
4428
|
+
}
|
|
4379
4429
|
_ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
|
|
4430
|
+
} else if (toolName && toolId) {
|
|
4431
|
+
_ToolCallProcessor.processToolArgs(toolId, "", state, sseLines);
|
|
4380
4432
|
} else {
|
|
4381
4433
|
console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No batch tool args to process! This will result in empty input!");
|
|
4382
4434
|
}
|
|
@@ -5279,9 +5331,14 @@ var O2ASSEAdapterStatic = {
|
|
|
5279
5331
|
// src/core/standard/standard-protocol-adapter.ts
|
|
5280
5332
|
var StandardProtocolAdapter = class {
|
|
5281
5333
|
constructor(options = {}) {
|
|
5282
|
-
this.debugMode = options.debugMode
|
|
5334
|
+
this.debugMode = options.debugMode ?? process.env.AI_PROTOCOL_DEBUG === "true";
|
|
5283
5335
|
this.sseAdapter = new O2ASSEAdapter(this.debugMode);
|
|
5284
5336
|
}
|
|
5337
|
+
logDebug(message, meta) {
|
|
5338
|
+
if (this.debugMode) {
|
|
5339
|
+
console.debug(message, meta ?? "");
|
|
5340
|
+
}
|
|
5341
|
+
}
|
|
5285
5342
|
/**
|
|
5286
5343
|
* 转换Anthropic请求为OpenAI请求格式
|
|
5287
5344
|
* @param anthropicRequest - Anthropic格式的请求
|
|
@@ -5305,7 +5362,7 @@ var StandardProtocolAdapter = class {
|
|
|
5305
5362
|
*/
|
|
5306
5363
|
convertFromStreamToStandard(openaiRawStream, modelName, messageId) {
|
|
5307
5364
|
if (this.debugMode) {
|
|
5308
|
-
|
|
5365
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
|
|
5309
5366
|
rawStreamLength: openaiRawStream.length,
|
|
5310
5367
|
modelName,
|
|
5311
5368
|
messageId,
|
|
@@ -5314,14 +5371,14 @@ var StandardProtocolAdapter = class {
|
|
|
5314
5371
|
}
|
|
5315
5372
|
const sseResult = this.sseAdapter.convertToClaudeSSE(openaiRawStream, modelName, messageId);
|
|
5316
5373
|
if (this.debugMode) {
|
|
5317
|
-
|
|
5374
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
|
|
5318
5375
|
sseResultLength: sseResult.length,
|
|
5319
5376
|
ssePreview: sseResult.substring(0, 500)
|
|
5320
5377
|
});
|
|
5321
5378
|
}
|
|
5322
5379
|
const standardResponse = this.extractStandardResponseFromSSE(sseResult, modelName, messageId);
|
|
5323
5380
|
if (this.debugMode) {
|
|
5324
|
-
|
|
5381
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
|
|
5325
5382
|
contentLength: standardResponse.content.length,
|
|
5326
5383
|
usage: standardResponse.usage,
|
|
5327
5384
|
stopReason: standardResponse.stop_reason
|
|
@@ -5336,7 +5393,7 @@ var StandardProtocolAdapter = class {
|
|
|
5336
5393
|
const lines = sseContent.split("\n");
|
|
5337
5394
|
const finalMessageId = messageId || generateMessageId();
|
|
5338
5395
|
if (this.debugMode) {
|
|
5339
|
-
|
|
5396
|
+
this.logDebug("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
|
|
5340
5397
|
totalLines: lines.length,
|
|
5341
5398
|
messageId: finalMessageId
|
|
5342
5399
|
});
|
|
@@ -5391,25 +5448,28 @@ var StandardProtocolAdapter = class {
|
|
|
5391
5448
|
if (data.type === "content_block_delta" && data.delta?.type === "text_delta") {
|
|
5392
5449
|
currentTextContent += data.delta.text;
|
|
5393
5450
|
if (this.debugMode && currentTextContent.length % 50 === 0) {
|
|
5394
|
-
|
|
5451
|
+
this.logDebug(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26)`, currentTextContent.substring(currentTextContent.length - 20));
|
|
5395
5452
|
}
|
|
5396
5453
|
}
|
|
5397
5454
|
if (data.type === "content_block_delta" && data.delta?.type === "input_json_delta") {
|
|
5398
5455
|
const toolIndex = data.index;
|
|
5399
5456
|
const toolId = indexToToolId.get(toolIndex);
|
|
5400
|
-
|
|
5401
|
-
|
|
5402
|
-
|
|
5403
|
-
|
|
5404
|
-
|
|
5457
|
+
if (this.debugMode) {
|
|
5458
|
+
this.logDebug(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
|
|
5459
|
+
toolIndex,
|
|
5460
|
+
toolId: toolId || "NOT_FOUND",
|
|
5461
|
+
delta: data.delta.partial_json
|
|
5462
|
+
});
|
|
5463
|
+
}
|
|
5405
5464
|
if (toolId) {
|
|
5406
5465
|
const currentBuffer = toolInputBuffers.get(toolIndex) || "";
|
|
5407
5466
|
const newBuffer = currentBuffer + data.delta.partial_json;
|
|
5408
5467
|
toolInputBuffers.set(toolIndex, newBuffer);
|
|
5409
|
-
|
|
5410
|
-
|
|
5411
|
-
|
|
5412
|
-
|
|
5468
|
+
if (this.debugMode) {
|
|
5469
|
+
this.logDebug(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId})`, {
|
|
5470
|
+
bufferLength: newBuffer.length
|
|
5471
|
+
});
|
|
5472
|
+
}
|
|
5413
5473
|
} else {
|
|
5414
5474
|
console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u627E\u4E0D\u5230toolId for index=${toolIndex}`);
|
|
5415
5475
|
}
|
|
@@ -5425,7 +5485,7 @@ var StandardProtocolAdapter = class {
|
|
|
5425
5485
|
const parsedInput = JSON.parse(jsonBuffer);
|
|
5426
5486
|
tool.input = parsedInput;
|
|
5427
5487
|
if (this.debugMode) {
|
|
5428
|
-
|
|
5488
|
+
this.logDebug(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId})`, parsedInput);
|
|
5429
5489
|
}
|
|
5430
5490
|
} catch (parseError) {
|
|
5431
5491
|
console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570JSON\u89E3\u6790\u5931\u8D25 (index=${toolIndex}, id=${toolId}):`, {
|
|
@@ -5443,7 +5503,7 @@ var StandardProtocolAdapter = class {
|
|
|
5443
5503
|
if (data.usage) {
|
|
5444
5504
|
response.usage = data.usage;
|
|
5445
5505
|
if (this.debugMode) {
|
|
5446
|
-
|
|
5506
|
+
this.logDebug("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
|
|
5447
5507
|
}
|
|
5448
5508
|
}
|
|
5449
5509
|
}
|
|
@@ -5463,7 +5523,7 @@ var StandardProtocolAdapter = class {
|
|
|
5463
5523
|
}
|
|
5464
5524
|
response.content.push(...Array.from(toolCalls.values()));
|
|
5465
5525
|
if (this.debugMode) {
|
|
5466
|
-
|
|
5526
|
+
this.logDebug("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
|
|
5467
5527
|
contentCount: response.content.length,
|
|
5468
5528
|
textLength: currentTextContent.length,
|
|
5469
5529
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,9 +935,33 @@ 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
|
}
|
|
928
|
-
|
|
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
|
+
}
|
|
947
|
+
return convertedMessages.map((msg) => {
|
|
948
|
+
if (Array.isArray(msg.tools)) {
|
|
949
|
+
msg.tools = msg.tools.map((tool) => {
|
|
950
|
+
if (tool?.type === "function" && tool.function) {
|
|
951
|
+
const description = tool.function.description?.trim() || "Converted tool with no description provided.";
|
|
952
|
+
return {
|
|
953
|
+
...tool,
|
|
954
|
+
function: {
|
|
955
|
+
...tool.function,
|
|
956
|
+
description
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
return tool;
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
return msg;
|
|
964
|
+
});
|
|
929
965
|
}
|
|
930
966
|
/**
|
|
931
967
|
* 创建消息转换上下文
|
|
@@ -4221,26 +4257,34 @@ var ToolCallProcessor = class _ToolCallProcessor {
|
|
|
4221
4257
|
* 处理增量工具调用
|
|
4222
4258
|
*/
|
|
4223
4259
|
static processIncrementalToolCalls(toolCalls, state, sseLines) {
|
|
4224
|
-
|
|
4260
|
+
const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
|
|
4261
|
+
if (debugEnabled) {
|
|
4262
|
+
console.debug("[ToolProcessor] processIncrementalToolCalls called with:", JSON.stringify(toolCalls, null, 2));
|
|
4263
|
+
}
|
|
4225
4264
|
for (const toolCall of toolCalls) {
|
|
4226
4265
|
const toolId = toolCall.id;
|
|
4227
4266
|
const toolName = toolCall.function?.name;
|
|
4228
4267
|
const toolArgs = toolCall.function?.arguments;
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
});
|
|
4268
|
+
if (debugEnabled) {
|
|
4269
|
+
console.debug("[ToolProcessor] Processing tool call:", {
|
|
4270
|
+
toolId,
|
|
4271
|
+
toolName,
|
|
4272
|
+
hasArgs: !!toolArgs
|
|
4273
|
+
});
|
|
4274
|
+
}
|
|
4237
4275
|
if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
|
|
4238
|
-
|
|
4276
|
+
if (debugEnabled) {
|
|
4277
|
+
console.debug("[ToolProcessor] Starting new tool call:", toolName);
|
|
4278
|
+
}
|
|
4239
4279
|
_ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
|
|
4240
4280
|
}
|
|
4241
4281
|
if (toolArgs) {
|
|
4242
|
-
|
|
4282
|
+
if (debugEnabled) {
|
|
4283
|
+
console.debug("[ToolProcessor] Processing tool args, calling processToolArgs");
|
|
4284
|
+
}
|
|
4243
4285
|
_ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
|
|
4286
|
+
} else if (toolName && toolId) {
|
|
4287
|
+
_ToolCallProcessor.processToolArgs(toolId, "", state, sseLines);
|
|
4244
4288
|
} else {
|
|
4245
4289
|
console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No tool args to process! This will result in empty input!");
|
|
4246
4290
|
}
|
|
@@ -4250,26 +4294,34 @@ var ToolCallProcessor = class _ToolCallProcessor {
|
|
|
4250
4294
|
* 处理工具调用
|
|
4251
4295
|
*/
|
|
4252
4296
|
static processBatchToolCalls(toolCalls, state, sseLines) {
|
|
4253
|
-
|
|
4297
|
+
const debugEnabled = process.env.AI_PROTOCOL_DEBUG === "true";
|
|
4298
|
+
if (debugEnabled) {
|
|
4299
|
+
console.debug("[ToolProcessor] processBatchToolCalls called with:", JSON.stringify(toolCalls, null, 2));
|
|
4300
|
+
}
|
|
4254
4301
|
for (const toolCall of toolCalls) {
|
|
4255
4302
|
const toolId = toolCall.id;
|
|
4256
4303
|
const toolName = toolCall.function?.name;
|
|
4257
4304
|
const toolArgs = toolCall.function?.arguments;
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
});
|
|
4305
|
+
if (debugEnabled) {
|
|
4306
|
+
console.debug("[ToolProcessor] Processing batch tool call:", {
|
|
4307
|
+
toolId,
|
|
4308
|
+
toolName,
|
|
4309
|
+
hasArgs: !!toolArgs
|
|
4310
|
+
});
|
|
4311
|
+
}
|
|
4266
4312
|
if (toolName && toolId && !state.toolCallsMap.has(toolId)) {
|
|
4267
|
-
|
|
4313
|
+
if (debugEnabled) {
|
|
4314
|
+
console.debug("[ToolProcessor] Starting new batch tool call:", toolName);
|
|
4315
|
+
}
|
|
4268
4316
|
_ToolCallProcessor.processToolCallStart(toolId, toolName, state, sseLines);
|
|
4269
4317
|
}
|
|
4270
4318
|
if (toolArgs) {
|
|
4271
|
-
|
|
4319
|
+
if (debugEnabled) {
|
|
4320
|
+
console.debug("[ToolProcessor] Processing batch tool args, calling processToolArgs");
|
|
4321
|
+
}
|
|
4272
4322
|
_ToolCallProcessor.processToolArgs(toolId, toolArgs, state, sseLines);
|
|
4323
|
+
} else if (toolName && toolId) {
|
|
4324
|
+
_ToolCallProcessor.processToolArgs(toolId, "", state, sseLines);
|
|
4273
4325
|
} else {
|
|
4274
4326
|
console.warn("\u26A0\uFE0F\u26A0\uFE0F\u26A0\uFE0F [ToolProcessor] No batch tool args to process! This will result in empty input!");
|
|
4275
4327
|
}
|
|
@@ -5172,9 +5224,14 @@ var O2ASSEAdapterStatic = {
|
|
|
5172
5224
|
// src/core/standard/standard-protocol-adapter.ts
|
|
5173
5225
|
var StandardProtocolAdapter = class {
|
|
5174
5226
|
constructor(options = {}) {
|
|
5175
|
-
this.debugMode = options.debugMode
|
|
5227
|
+
this.debugMode = options.debugMode ?? process.env.AI_PROTOCOL_DEBUG === "true";
|
|
5176
5228
|
this.sseAdapter = new O2ASSEAdapter(this.debugMode);
|
|
5177
5229
|
}
|
|
5230
|
+
logDebug(message, meta) {
|
|
5231
|
+
if (this.debugMode) {
|
|
5232
|
+
console.debug(message, meta ?? "");
|
|
5233
|
+
}
|
|
5234
|
+
}
|
|
5178
5235
|
/**
|
|
5179
5236
|
* 转换Anthropic请求为OpenAI请求格式
|
|
5180
5237
|
* @param anthropicRequest - Anthropic格式的请求
|
|
@@ -5198,7 +5255,7 @@ var StandardProtocolAdapter = class {
|
|
|
5198
5255
|
*/
|
|
5199
5256
|
convertFromStreamToStandard(openaiRawStream, modelName, messageId) {
|
|
5200
5257
|
if (this.debugMode) {
|
|
5201
|
-
|
|
5258
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] convertFromStreamToStandard \u5F00\u59CB\u5904\u7406:", {
|
|
5202
5259
|
rawStreamLength: openaiRawStream.length,
|
|
5203
5260
|
modelName,
|
|
5204
5261
|
messageId,
|
|
@@ -5207,14 +5264,14 @@ var StandardProtocolAdapter = class {
|
|
|
5207
5264
|
}
|
|
5208
5265
|
const sseResult = this.sseAdapter.convertToClaudeSSE(openaiRawStream, modelName, messageId);
|
|
5209
5266
|
if (this.debugMode) {
|
|
5210
|
-
|
|
5267
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] SSE\u8F6C\u6362\u5B8C\u6210:", {
|
|
5211
5268
|
sseResultLength: sseResult.length,
|
|
5212
5269
|
ssePreview: sseResult.substring(0, 500)
|
|
5213
5270
|
});
|
|
5214
5271
|
}
|
|
5215
5272
|
const standardResponse = this.extractStandardResponseFromSSE(sseResult, modelName, messageId);
|
|
5216
5273
|
if (this.debugMode) {
|
|
5217
|
-
|
|
5274
|
+
this.logDebug("\u{1F504} [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u63D0\u53D6\u5B8C\u6210:", {
|
|
5218
5275
|
contentLength: standardResponse.content.length,
|
|
5219
5276
|
usage: standardResponse.usage,
|
|
5220
5277
|
stopReason: standardResponse.stop_reason
|
|
@@ -5229,7 +5286,7 @@ var StandardProtocolAdapter = class {
|
|
|
5229
5286
|
const lines = sseContent.split("\n");
|
|
5230
5287
|
const finalMessageId = messageId || generateMessageId();
|
|
5231
5288
|
if (this.debugMode) {
|
|
5232
|
-
|
|
5289
|
+
this.logDebug("\u{1F50D} [StandardProtocolAdapter] extractStandardResponseFromSSE \u5F00\u59CB\u89E3\u6790:", {
|
|
5233
5290
|
totalLines: lines.length,
|
|
5234
5291
|
messageId: finalMessageId
|
|
5235
5292
|
});
|
|
@@ -5284,25 +5341,28 @@ var StandardProtocolAdapter = class {
|
|
|
5284
5341
|
if (data.type === "content_block_delta" && data.delta?.type === "text_delta") {
|
|
5285
5342
|
currentTextContent += data.delta.text;
|
|
5286
5343
|
if (this.debugMode && currentTextContent.length % 50 === 0) {
|
|
5287
|
-
|
|
5344
|
+
this.logDebug(`\u{1F4DD} [StandardProtocolAdapter] \u7D2F\u79EF\u6587\u672C\u5185\u5BB9 (${currentTextContent.length}\u5B57\u7B26)`, currentTextContent.substring(currentTextContent.length - 20));
|
|
5288
5345
|
}
|
|
5289
5346
|
}
|
|
5290
5347
|
if (data.type === "content_block_delta" && data.delta?.type === "input_json_delta") {
|
|
5291
5348
|
const toolIndex = data.index;
|
|
5292
5349
|
const toolId = indexToToolId.get(toolIndex);
|
|
5293
|
-
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5350
|
+
if (this.debugMode) {
|
|
5351
|
+
this.logDebug(`\u{1F527}\u{1F527}\u{1F527} [StandardProtocolAdapter] \u68C0\u6D4B\u5230input_json_delta\u4E8B\u4EF6\uFF01`, {
|
|
5352
|
+
toolIndex,
|
|
5353
|
+
toolId: toolId || "NOT_FOUND",
|
|
5354
|
+
delta: data.delta.partial_json
|
|
5355
|
+
});
|
|
5356
|
+
}
|
|
5298
5357
|
if (toolId) {
|
|
5299
5358
|
const currentBuffer = toolInputBuffers.get(toolIndex) || "";
|
|
5300
5359
|
const newBuffer = currentBuffer + data.delta.partial_json;
|
|
5301
5360
|
toolInputBuffers.set(toolIndex, newBuffer);
|
|
5302
|
-
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5361
|
+
if (this.debugMode) {
|
|
5362
|
+
this.logDebug(`\u{1F527} [StandardProtocolAdapter] \u7D2F\u79EF\u5DE5\u5177\u53C2\u6570 (index=${toolIndex}, id=${toolId})`, {
|
|
5363
|
+
bufferLength: newBuffer.length
|
|
5364
|
+
});
|
|
5365
|
+
}
|
|
5306
5366
|
} else {
|
|
5307
5367
|
console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u627E\u4E0D\u5230toolId for index=${toolIndex}`);
|
|
5308
5368
|
}
|
|
@@ -5318,7 +5378,7 @@ var StandardProtocolAdapter = class {
|
|
|
5318
5378
|
const parsedInput = JSON.parse(jsonBuffer);
|
|
5319
5379
|
tool.input = parsedInput;
|
|
5320
5380
|
if (this.debugMode) {
|
|
5321
|
-
|
|
5381
|
+
this.logDebug(`\u2705 [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570\u89E3\u6790\u5B8C\u6210 (index=${toolIndex}, id=${toolId})`, parsedInput);
|
|
5322
5382
|
}
|
|
5323
5383
|
} catch (parseError) {
|
|
5324
5384
|
console.warn(`\u26A0\uFE0F [StandardProtocolAdapter] \u5DE5\u5177\u53C2\u6570JSON\u89E3\u6790\u5931\u8D25 (index=${toolIndex}, id=${toolId}):`, {
|
|
@@ -5336,7 +5396,7 @@ var StandardProtocolAdapter = class {
|
|
|
5336
5396
|
if (data.usage) {
|
|
5337
5397
|
response.usage = data.usage;
|
|
5338
5398
|
if (this.debugMode) {
|
|
5339
|
-
|
|
5399
|
+
this.logDebug("\u{1F4CA} [StandardProtocolAdapter] \u66F4\u65B0usage\u4FE1\u606F:", data.usage);
|
|
5340
5400
|
}
|
|
5341
5401
|
}
|
|
5342
5402
|
}
|
|
@@ -5356,7 +5416,7 @@ var StandardProtocolAdapter = class {
|
|
|
5356
5416
|
}
|
|
5357
5417
|
response.content.push(...Array.from(toolCalls.values()));
|
|
5358
5418
|
if (this.debugMode) {
|
|
5359
|
-
|
|
5419
|
+
this.logDebug("\u2705 [StandardProtocolAdapter] \u6807\u51C6\u54CD\u5E94\u6784\u5EFA\u5B8C\u6210:", {
|
|
5360
5420
|
contentCount: response.content.length,
|
|
5361
5421
|
textLength: currentTextContent.length,
|
|
5362
5422
|
toolCallsCount: toolCalls.size,
|
package/package.json
CHANGED