devme commited on
Commit
8c61d15
·
verified ·
1 Parent(s): b8df649

Delete transformers/request-anthropic.js

Browse files
Files changed (1) hide show
  1. transformers/request-anthropic.js +0 -242
transformers/request-anthropic.js DELETED
@@ -1,242 +0,0 @@
1
- import { logDebug } from '../logger.js';
2
- import { getSystemPrompt, getModelReasoning, getUserAgent } from '../config.js';
3
-
4
- export function transformToAnthropic(openaiRequest) {
5
- logDebug('将 OpenAI 请求转换为 Anthropic 格式');
6
-
7
- const anthropicRequest = {
8
- model: openaiRequest.model,
9
- messages: []
10
- };
11
-
12
- // 仅在客户端明确提供时添加 stream 参数
13
- if (openaiRequest.stream !== undefined) {
14
- anthropicRequest.stream = openaiRequest.stream;
15
- }
16
-
17
- // 处理 max_tokens
18
- if (openaiRequest.max_tokens) {
19
- anthropicRequest.max_tokens = openaiRequest.max_tokens;
20
- } else if (openaiRequest.max_completion_tokens) {
21
- anthropicRequest.max_tokens = openaiRequest.max_completion_tokens;
22
- } else {
23
- anthropicRequest.max_tokens = 4096;
24
- }
25
-
26
- // 提取系统消息并转换其他消息
27
- let systemContent = [];
28
-
29
- if (openaiRequest.messages && Array.isArray(openaiRequest.messages)) {
30
- for (const msg of openaiRequest.messages) {
31
- // 单独处理系统消息
32
- if (msg.role === 'system') {
33
- if (typeof msg.content === 'string') {
34
- systemContent.push({
35
- type: 'text',
36
- text: msg.content?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'you are bot.')
37
- });
38
- } else if (Array.isArray(msg.content)) {
39
- for (const part of msg.content) {
40
- if (part.type === 'text') {
41
- systemContent.push({
42
- type: 'text',
43
- text: part.text?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'you are bot.')
44
- });
45
- } else {
46
- systemContent.push(part);
47
- }
48
- }
49
- }
50
- continue; // 跳过将系统消息添加到消息数组
51
- }
52
-
53
- const anthropicMsg = {
54
- role: msg.role,
55
- content: []
56
- };
57
-
58
- if (typeof msg.content === 'string') {
59
- anthropicMsg.content.push({
60
- type: 'text',
61
- text: msg.content
62
- });
63
- } else if (Array.isArray(msg.content)) {
64
- for (const part of msg.content) {
65
- if (part.type === 'text') {
66
- anthropicMsg.content.push({
67
- type: 'text',
68
- text: part.text
69
- });
70
- } else if (part.type === 'image_url') {
71
- anthropicMsg.content.push({
72
- type: 'image',
73
- source: part.image_url
74
- });
75
- } else {
76
- anthropicMsg.content.push(part);
77
- }
78
- }
79
- }
80
-
81
- anthropicRequest.messages.push(anthropicMsg);
82
- }
83
- }
84
-
85
- // 添加系统参数,并在前面加上系统提示
86
- const systemPrompt = getSystemPrompt();
87
- if (systemPrompt || systemContent.length > 0) {
88
- anthropicRequest.system = [];
89
- // 如果存在系统提示,则将其作为第一个元素添加
90
- if (systemPrompt) {
91
- anthropicRequest.system.push({
92
- type: 'text',
93
- text: systemPrompt
94
- });
95
- }
96
- // 添加用户提供的系统内容
97
- anthropicRequest.system.push(...systemContent);
98
- }
99
-
100
- // 如果存在工具,则进行转换
101
- if (openaiRequest.tools && Array.isArray(openaiRequest.tools)) {
102
- anthropicRequest.tools = openaiRequest.tools.map(tool => {
103
- if (tool.type === 'function') {
104
- return {
105
- name: tool.function.name,
106
- description: tool.function.description,
107
- input_schema: tool.function.parameters || {}
108
- };
109
- }
110
- return tool;
111
- });
112
- }
113
-
114
- // 根据模型配置处理 thinking 字段
115
- const reasoningLevel = getModelReasoning(openaiRequest.model);
116
- if (reasoningLevel === 'auto') {
117
- // 自动模式:完全保留原始请求的 thinking 字段
118
- if (openaiRequest.thinking !== undefined) {
119
- anthropicRequest.thinking = openaiRequest.thinking;
120
- }
121
- // 如果原始请求没有 thinking 字段,则不添加
122
- } else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
123
- // 特定级别:使用模型配置覆盖
124
- const budgetTokens = {
125
- 'low': 4096,
126
- 'medium': 12288,
127
- 'high': 24576
128
- };
129
-
130
- anthropicRequest.thinking = {
131
- type: 'enabled',
132
- budget_tokens: budgetTokens[reasoningLevel]
133
- };
134
- } else {
135
- // 关闭或无效:显式删除 thinking 字段
136
- // 这确保删除原始请求中的任何 thinking 字段
137
- delete anthropicRequest.thinking;
138
- }
139
-
140
- // 传递其他兼容参数
141
- if (openaiRequest.temperature !== undefined) {
142
- anthropicRequest.temperature = openaiRequest.temperature;
143
- }
144
- if (openaiRequest.top_p !== undefined) {
145
- anthropicRequest.top_p = openaiRequest.top_p;
146
- }
147
- if (openaiRequest.stop !== undefined) {
148
- anthropicRequest.stop_sequences = Array.isArray(openaiRequest.stop)
149
- ? openaiRequest.stop
150
- : [openaiRequest.stop];
151
- }
152
-
153
- logDebug('已转换的 Anthropic 请求', anthropicRequest);
154
- return anthropicRequest;
155
- }
156
-
157
- export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming = true, modelId = null) {
158
- // 如果未提供则生成唯一 ID
159
- const sessionId = clientHeaders['x-session-id'] || generateUUID();
160
- const messageId = clientHeaders['x-assistant-message-id'] || generateUUID();
161
-
162
- const headers = {
163
- 'accept': 'application/json',
164
- 'content-type': 'application/json',
165
- 'anthropic-version': clientHeaders['anthropic-version'] || '2023-06-01',
166
- 'authorization': authHeader || '',
167
- 'x-api-key': 'placeholder',
168
- 'x-api-provider': 'anthropic',
169
- 'x-factory-client': 'cli',
170
- 'x-session-id': sessionId,
171
- 'x-assistant-message-id': messageId,
172
- 'user-agent': getUserAgent(),
173
- 'x-stainless-timeout': '600',
174
- 'connection': 'keep-alive'
175
- }
176
-
177
- // 根据推理配置处理 anthropic-beta 头
178
- const reasoningLevel = modelId ? getModelReasoning(modelId) : null;
179
- let betaValues = [];
180
-
181
- // 从客户端头添加现有的 beta 值
182
- if (clientHeaders['anthropic-beta']) {
183
- const existingBeta = clientHeaders['anthropic-beta'];
184
- betaValues = existingBeta.split(',').map(v => v.trim());
185
- }
186
-
187
- // 根据推理配置处理 thinking beta
188
- const thinkingBeta = 'interleaved-thinking-2025-05-14';
189
- if (reasoningLevel === 'auto') {
190
- // 自动模式:不修改 anthropic-beta 头,保留原始值
191
- // betaValues 保持客户端头的不变
192
- } else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
193
- // 如果尚未存在,则添加 thinking beta
194
- if (!betaValues.includes(thinkingBeta)) {
195
- betaValues.push(thinkingBeta);
196
- }
197
- } else {
198
- // 如果推理关闭或无效,则删除 thinking beta
199
- betaValues = betaValues.filter(v => v !== thinkingBeta);
200
- }
201
-
202
- // 如果有任何值,则设置 anthropic-beta 头
203
- if (betaValues.length > 0) {
204
- headers['anthropic-beta'] = betaValues.join(', ');
205
- }
206
-
207
- // 使用默认值传递 Stainless SDK 头
208
- const stainlessDefaults = {
209
- 'x-stainless-arch': 'x64',
210
- 'x-stainless-lang': 'js',
211
- 'x-stainless-os': 'MacOS',
212
- 'x-stainless-runtime': 'node',
213
- 'x-stainless-retry-count': '0',
214
- 'x-stainless-package-version': '0.57.0',
215
- 'x-stainless-runtime-version': 'v24.3.0'
216
- };
217
-
218
- // 根据流式传输设置 helper-method
219
- if (isStreaming) {
220
- headers['x-stainless-helper-method'] = 'stream';
221
- }
222
-
223
- // 从客户端复制 Stainless 头或使用默认值
224
- Object.keys(stainlessDefaults).forEach(header => {
225
- headers[header] = clientHeaders[header] || stainlessDefaults[header];
226
- });
227
-
228
- // 如果客户端提供,则覆盖默认超时
229
- if (clientHeaders['x-stainless-timeout']) {
230
- headers['x-stainless-timeout'] = clientHeaders['x-stainless-timeout'];
231
- }
232
-
233
- return headers;
234
- }
235
-
236
- function generateUUID() {
237
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
238
- const r = Math.random() * 16 | 0;
239
- const v = c == 'x' ? r : (r & 0x3 | 0x8);
240
- return v.toString(16);
241
- });
242
- }