protocol-proxy 2.8.1 → 2.8.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.
- package/lib/converters/anthropic-to-openai.js +14 -5
- package/lib/proxy-server.js +15 -3
- package/package.json +1 -1
- package/public/app.js +1590 -2027
- package/public/index.html +603 -440
- package/public/style.css +1786 -1996
- package/server.js +98 -1
|
@@ -82,12 +82,15 @@ function convertMessage(msg, idMap) {
|
|
|
82
82
|
// 处理 content 数组
|
|
83
83
|
if (Array.isArray(msg.content)) {
|
|
84
84
|
const textParts = [];
|
|
85
|
+
const thinkingParts = [];
|
|
85
86
|
const toolResults = [];
|
|
86
87
|
const toolUses = [];
|
|
87
88
|
|
|
88
89
|
for (const block of msg.content) {
|
|
89
90
|
if (block.type === 'text') {
|
|
90
91
|
textParts.push(block.text);
|
|
92
|
+
} else if (block.type === 'thinking') {
|
|
93
|
+
thinkingParts.push(block.thinking);
|
|
91
94
|
} else if (block.type === 'tool_result') {
|
|
92
95
|
// 使用映射后的 OpenAI 格式 id
|
|
93
96
|
const openaiId = idMap?.get(block.tool_use_id) || block.tool_use_id;
|
|
@@ -112,13 +115,15 @@ function convertMessage(msg, idMap) {
|
|
|
112
115
|
|
|
113
116
|
// assistant 消息含 tool_use → 需要拆分为 assistant + tool
|
|
114
117
|
if (msg.role === 'assistant' && toolUses.length > 0) {
|
|
115
|
-
const
|
|
116
|
-
result.push({
|
|
118
|
+
const assistantMsg = {
|
|
117
119
|
role: 'assistant',
|
|
118
120
|
content: textParts.join('') || '',
|
|
119
121
|
tool_calls: toolUses,
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
+
};
|
|
123
|
+
if (thinkingParts.length > 0) {
|
|
124
|
+
assistantMsg.reasoning_content = thinkingParts.join('');
|
|
125
|
+
}
|
|
126
|
+
return [assistantMsg];
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
// user 消息含 tool_result → 拆分为多个 tool 消息
|
|
@@ -133,7 +138,11 @@ function convertMessage(msg, idMap) {
|
|
|
133
138
|
}
|
|
134
139
|
|
|
135
140
|
// 普通情况
|
|
136
|
-
|
|
141
|
+
const out = { role: msg.role, content: textParts.join('') };
|
|
142
|
+
if (msg.role === 'assistant' && thinkingParts.length > 0) {
|
|
143
|
+
out.reasoning_content = thinkingParts.join('');
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
137
146
|
}
|
|
138
147
|
|
|
139
148
|
return { role: msg.role, content: msg.content };
|
package/lib/proxy-server.js
CHANGED
|
@@ -82,9 +82,13 @@ function createProxyApp(proxyConfigOrGetter) {
|
|
|
82
82
|
function injectReasoningToMessages(messages) {
|
|
83
83
|
if (!Array.isArray(messages)) return;
|
|
84
84
|
for (const msg of messages) {
|
|
85
|
-
if (msg.role === 'assistant' && msg.reasoning_content === undefined) {
|
|
85
|
+
if (msg.role === 'assistant' && (msg.reasoning_content === undefined || msg.reasoning_content === null)) {
|
|
86
86
|
const reasoning = getReasoning(msg);
|
|
87
|
-
|
|
87
|
+
if (reasoning) {
|
|
88
|
+
msg.reasoning_content = reasoning;
|
|
89
|
+
} else {
|
|
90
|
+
delete msg.reasoning_content;
|
|
91
|
+
}
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
}
|
|
@@ -476,7 +480,7 @@ function createProxyApp(proxyConfigOrGetter) {
|
|
|
476
480
|
method: 'POST',
|
|
477
481
|
headers: keyHeaders,
|
|
478
482
|
body: JSON.stringify(targetBody),
|
|
479
|
-
signal: AbortSignal.timeout(300000),
|
|
483
|
+
signal: AbortSignal.timeout(proxyConfig.timeout || 300000),
|
|
480
484
|
});
|
|
481
485
|
|
|
482
486
|
if (!fetchRes.ok) {
|
|
@@ -511,6 +515,7 @@ function createProxyApp(proxyConfigOrGetter) {
|
|
|
511
515
|
const decoder = new TextDecoder();
|
|
512
516
|
let streamUsage = null;
|
|
513
517
|
let responseText = '';
|
|
518
|
+
let reasoningText = '';
|
|
514
519
|
let toolCallCount = 0;
|
|
515
520
|
|
|
516
521
|
req.on('close', () => {
|
|
@@ -530,6 +535,7 @@ function createProxyApp(proxyConfigOrGetter) {
|
|
|
530
535
|
if (d.usage) streamUsage = d.usage;
|
|
531
536
|
const delta = d.choices?.[0]?.delta;
|
|
532
537
|
if (delta?.content) responseText += delta.content;
|
|
538
|
+
if (delta?.reasoning_content) reasoningText += delta.reasoning_content;
|
|
533
539
|
if (delta?.tool_calls) {
|
|
534
540
|
for (const tc of delta.tool_calls) {
|
|
535
541
|
if (tc.function?.name) toolCallCount++;
|
|
@@ -546,6 +552,12 @@ function createProxyApp(proxyConfigOrGetter) {
|
|
|
546
552
|
}
|
|
547
553
|
}
|
|
548
554
|
|
|
555
|
+
// Cache reasoning_content from streaming response for future requests
|
|
556
|
+
if (reasoningText && responseText) {
|
|
557
|
+
const msg = { content: responseText, tool_calls: null };
|
|
558
|
+
setReasoning(msg, reasoningText);
|
|
559
|
+
}
|
|
560
|
+
|
|
549
561
|
if (streamUsage) {
|
|
550
562
|
recordUsage(proxyConfig.id, candidate.providerName, candidateModel, streamUsage, false);
|
|
551
563
|
requestLog.add({
|