bingocode 1.1.202 → 1.1.204
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/package.json
CHANGED
|
@@ -1,220 +1,247 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request transformation: Anthropic Messages → OpenAI Chat Completions
|
|
3
|
-
* Derived from cc-switch (https://github.com/farion1231/cc-switch)
|
|
4
|
-
* Original work by Jason Young, MIT License
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type {
|
|
8
|
-
AnthropicRequest,
|
|
9
|
-
AnthropicContentBlock,
|
|
10
|
-
AnthropicMessage,
|
|
11
|
-
OpenAIChatRequest,
|
|
12
|
-
OpenAIChatMessage,
|
|
13
|
-
OpenAIChatContentPart,
|
|
14
|
-
OpenAIToolCall,
|
|
15
|
-
OpenAITool,
|
|
16
|
-
} from './types.js'
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Request transformation: Anthropic Messages → OpenAI Chat Completions
|
|
3
|
+
* Derived from cc-switch (https://github.com/farion1231/cc-switch)
|
|
4
|
+
* Original work by Jason Young, MIT License
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AnthropicRequest,
|
|
9
|
+
AnthropicContentBlock,
|
|
10
|
+
AnthropicMessage,
|
|
11
|
+
OpenAIChatRequest,
|
|
12
|
+
OpenAIChatMessage,
|
|
13
|
+
OpenAIChatContentPart,
|
|
14
|
+
OpenAIToolCall,
|
|
15
|
+
OpenAITool,
|
|
16
|
+
} from './types.js'
|
|
17
|
+
|
|
18
|
+
// Params broadly supported by OpenAI-compatible chat completion upstreams.
|
|
19
|
+
// Anything outside this set (e.g. reasoning_effort) is dropped when
|
|
20
|
+
// BINGO_DROP_PARAMS is enabled to avoid upstream 400 rejections.
|
|
21
|
+
const OPENAI_CHAT_ALLOWED_PARAMS = new Set<keyof OpenAIChatRequest>([
|
|
22
|
+
'model',
|
|
23
|
+
'messages',
|
|
24
|
+
'max_tokens',
|
|
25
|
+
'max_completion_tokens',
|
|
26
|
+
'stream',
|
|
27
|
+
'temperature',
|
|
28
|
+
'top_p',
|
|
29
|
+
'stop',
|
|
30
|
+
'tools',
|
|
31
|
+
'tool_choice',
|
|
32
|
+
'frequency_penalty',
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Convert Anthropic Messages request to OpenAI Chat Completions request.
|
|
37
|
+
*/
|
|
38
|
+
export function anthropicToOpenaiChat(body: AnthropicRequest): OpenAIChatRequest {
|
|
39
|
+
const messages: OpenAIChatMessage[] = []
|
|
40
|
+
|
|
41
|
+
// Convert system prompt
|
|
42
|
+
if (body.system) {
|
|
43
|
+
if (typeof body.system === 'string') {
|
|
44
|
+
messages.push({ role: 'system', content: body.system })
|
|
45
|
+
} else if (Array.isArray(body.system)) {
|
|
46
|
+
const text = body.system.map((b) => b.text).join('\n')
|
|
47
|
+
messages.push({ role: 'system', content: text })
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Convert messages
|
|
52
|
+
for (const msg of body.messages) {
|
|
53
|
+
convertMessage(msg, messages, body.model)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Build request
|
|
57
|
+
const result: OpenAIChatRequest = {
|
|
58
|
+
model: body.model,
|
|
59
|
+
messages,
|
|
60
|
+
stream: body.stream,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// max_tokens — cap to avoid upstream 400 errors from Claude's high defaults (e.g. 64k).
|
|
64
|
+
// DeepSeek: tools/thinking fail above 8192. Other providers: 32768 covers most upstreams.
|
|
65
|
+
// GPT models (gpt-*): use max_completion_tokens instead of max_tokens (required by newer GPT models).
|
|
66
|
+
if (body.max_tokens !== undefined) {
|
|
67
|
+
const modelLower = body.model.toLowerCase()
|
|
68
|
+
if (modelLower.includes('deepseek')) {
|
|
69
|
+
result.max_tokens = Math.min(body.max_tokens, 8192)
|
|
70
|
+
} else if (modelLower.startsWith('gpt-') || modelLower.startsWith('o1') || modelLower.startsWith('o3') || modelLower.startsWith('o4')) {
|
|
71
|
+
result.max_completion_tokens = body.max_tokens
|
|
72
|
+
} else {
|
|
73
|
+
result.max_tokens = Math.min(body.max_tokens, 32768)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// temperature & top_p
|
|
78
|
+
if (body.temperature !== undefined) result.temperature = body.temperature
|
|
79
|
+
if (body.top_p !== undefined) result.top_p = body.top_p
|
|
80
|
+
|
|
81
|
+
// frequency_penalty: suppress repetition loops during multi-tool-call sequences.
|
|
82
|
+
// Anthropic API has no equivalent; inject for all OpenAI-compatible upstreams.
|
|
83
|
+
// Configurable via BINGO_FREQUENCY_PENALTY (default 0, opt-in).
|
|
84
|
+
const fp = parseFloat(process.env.BINGO_FREQUENCY_PENALTY ?? '0')
|
|
85
|
+
if (!isNaN(fp) && fp !== 0) result.frequency_penalty = fp
|
|
86
|
+
|
|
87
|
+
// stop_sequences → stop
|
|
88
|
+
if (body.stop_sequences && body.stop_sequences.length > 0) {
|
|
89
|
+
result.stop = body.stop_sequences
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// tools
|
|
93
|
+
if (body.tools && body.tools.length > 0) {
|
|
94
|
+
result.tools = body.tools
|
|
95
|
+
.filter((t) => t.name !== 'BatchTool')
|
|
96
|
+
.map((t): OpenAITool => ({
|
|
97
|
+
type: 'function',
|
|
98
|
+
function: {
|
|
99
|
+
name: t.name,
|
|
100
|
+
description: t.description,
|
|
101
|
+
parameters: t.input_schema,
|
|
102
|
+
},
|
|
103
|
+
}))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// tool_choice
|
|
107
|
+
if (body.tool_choice !== undefined) {
|
|
108
|
+
result.tool_choice = convertToolChoice(body.tool_choice)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// thinking → reasoning_effort
|
|
112
|
+
if (body.thinking) {
|
|
113
|
+
const budget = body.thinking.budget_tokens
|
|
114
|
+
if (budget !== undefined) {
|
|
115
|
+
if (budget <= 1024) result.reasoning_effort = 'low'
|
|
116
|
+
else if (budget <= 8192) result.reasoning_effort = 'medium'
|
|
117
|
+
else result.reasoning_effort = 'high'
|
|
118
|
+
} else if (body.thinking.type === 'enabled') {
|
|
119
|
+
result.reasoning_effort = 'high'
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Drop params that upstreams (e.g. LiteLLM-routed providers) may reject.
|
|
124
|
+
// Default ON because many LiteLLM backends (together_ai, etc.) hard-fail on
|
|
125
|
+
// unknown params. Set BINGO_DROP_PARAMS=0/false to keep all params.
|
|
126
|
+
const dropParams = !/^(0|false|no)$/i.test(process.env.BINGO_DROP_PARAMS ?? '1')
|
|
127
|
+
if (dropParams) {
|
|
128
|
+
for (const key of Object.keys(result) as Array<keyof OpenAIChatRequest>) {
|
|
129
|
+
if (!OPENAI_CHAT_ALLOWED_PARAMS.has(key)) delete result[key]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function convertMessage(msg: AnthropicMessage, output: OpenAIChatMessage[], model: string = ''): void {
|
|
137
|
+
const content = msg.content
|
|
138
|
+
|
|
139
|
+
// Simple string content
|
|
140
|
+
if (typeof content === 'string') {
|
|
141
|
+
output.push({ role: msg.role, content })
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Array content blocks
|
|
146
|
+
if (!Array.isArray(content) || content.length === 0) {
|
|
147
|
+
output.push({ role: msg.role, content: '' })
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (msg.role === 'user') {
|
|
152
|
+
convertUserMessage(content, output)
|
|
153
|
+
} else {
|
|
154
|
+
convertAssistantMessage(content, output, model)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function convertUserMessage(blocks: AnthropicContentBlock[], output: OpenAIChatMessage[]): void {
|
|
159
|
+
// Separate tool_result blocks from other content
|
|
160
|
+
const contentParts: OpenAIChatContentPart[] = []
|
|
161
|
+
|
|
162
|
+
for (const block of blocks) {
|
|
163
|
+
if (block.type === 'text') {
|
|
164
|
+
contentParts.push({ type: 'text', text: block.text })
|
|
165
|
+
} else if (block.type === 'image') {
|
|
166
|
+
const url = `data:${block.source.media_type};base64,${block.source.data}`
|
|
167
|
+
contentParts.push({ type: 'image_url', image_url: { url } })
|
|
168
|
+
} else if (block.type === 'tool_result') {
|
|
169
|
+
// tool_result → separate tool message
|
|
170
|
+
const rawContent = typeof block.content === 'string'
|
|
171
|
+
? block.content
|
|
172
|
+
: Array.isArray(block.content)
|
|
173
|
+
? block.content.filter((b): b is Extract<AnthropicContentBlock, { type: 'text' }> => b.type === 'text').map((b) => b.text).join('\n')
|
|
174
|
+
: ''
|
|
175
|
+
const resultContent = block.is_error
|
|
176
|
+
? `<error>${rawContent}</error>`
|
|
177
|
+
: rawContent
|
|
178
|
+
output.push({
|
|
179
|
+
role: 'tool',
|
|
180
|
+
tool_call_id: block.tool_use_id,
|
|
181
|
+
content: resultContent,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (contentParts.length > 0) {
|
|
187
|
+
output.push({
|
|
188
|
+
role: 'user',
|
|
189
|
+
content: contentParts.length === 1 && contentParts[0].type === 'text'
|
|
190
|
+
? contentParts[0].text
|
|
191
|
+
: contentParts,
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function convertAssistantMessage(blocks: AnthropicContentBlock[], output: OpenAIChatMessage[], model: string = ''): void {
|
|
197
|
+
let textContent = ''
|
|
198
|
+
let reasoningContent = ''
|
|
199
|
+
const toolCalls: OpenAIToolCall[] = []
|
|
200
|
+
|
|
201
|
+
for (const block of blocks) {
|
|
202
|
+
if (block.type === 'text') {
|
|
203
|
+
textContent += block.text
|
|
204
|
+
} else if (block.type === 'thinking') {
|
|
205
|
+
reasoningContent += block.thinking
|
|
206
|
+
} else if (block.type === 'tool_use') {
|
|
207
|
+
toolCalls.push({
|
|
208
|
+
id: block.id,
|
|
209
|
+
type: 'function',
|
|
210
|
+
function: {
|
|
211
|
+
name: block.name,
|
|
212
|
+
arguments: typeof block.input === 'string' ? block.input : JSON.stringify(block.input),
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const msg: OpenAIChatMessage = {
|
|
219
|
+
role: 'assistant',
|
|
220
|
+
content: textContent || null,
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Only pass reasoning_content back for DeepSeek models to satisfy their mandatory back-transmission rule
|
|
224
|
+
if (reasoningContent && model.toLowerCase().includes('deepseek')) {
|
|
225
|
+
(msg as any).reasoning_content = reasoningContent
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (toolCalls.length > 0) {
|
|
229
|
+
msg.tool_calls = toolCalls
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
output.push(msg)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function convertToolChoice(choice: unknown): unknown {
|
|
236
|
+
if (typeof choice === 'string') return choice
|
|
237
|
+
if (typeof choice === 'object' && choice !== null) {
|
|
238
|
+
const c = choice as Record<string, unknown>
|
|
239
|
+
if (c.type === 'auto') return 'auto'
|
|
240
|
+
if (c.type === 'any') return 'required'
|
|
241
|
+
if (c.type === 'none') return 'none'
|
|
242
|
+
if (c.type === 'tool' && typeof c.name === 'string') {
|
|
243
|
+
return { type: 'function', function: { name: c.name } }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return 'auto'
|
|
247
|
+
}
|
package/src/utils/context.ts
CHANGED
|
@@ -6,7 +6,9 @@ import { getCanonicalName } from './model/model.js'
|
|
|
6
6
|
import { getModelCapability } from './model/modelCapabilities.js'
|
|
7
7
|
|
|
8
8
|
// Model context window size (200k tokens for all models right now)
|
|
9
|
-
|
|
9
|
+
// Sized so the default auto-compact threshold lands at 786,432 tokens:
|
|
10
|
+
// 819,432 - 20,000 (summary reserve) - 13,000 (autocompact buffer) = 786,432
|
|
11
|
+
export const MODEL_CONTEXT_WINDOW_DEFAULT = 819_432
|
|
10
12
|
|
|
11
13
|
// Maximum output tokens for compact operations
|
|
12
14
|
export const COMPACT_MAX_OUTPUT_TOKENS = 20_000
|