@wener/mcps 1.0.1
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/LICENSE +21 -0
- package/dist/index.mjs +15 -0
- package/dist/mcps-cli.mjs +174727 -0
- package/lib/chat/agent.js +187 -0
- package/lib/chat/agent.js.map +1 -0
- package/lib/chat/audit.js +238 -0
- package/lib/chat/audit.js.map +1 -0
- package/lib/chat/converters.js +467 -0
- package/lib/chat/converters.js.map +1 -0
- package/lib/chat/handler.js +1068 -0
- package/lib/chat/handler.js.map +1 -0
- package/lib/chat/index.js +12 -0
- package/lib/chat/index.js.map +1 -0
- package/lib/chat/types.js +35 -0
- package/lib/chat/types.js.map +1 -0
- package/lib/contracts/AuditContract.js +85 -0
- package/lib/contracts/AuditContract.js.map +1 -0
- package/lib/contracts/McpsContract.js +113 -0
- package/lib/contracts/McpsContract.js.map +1 -0
- package/lib/contracts/index.js +3 -0
- package/lib/contracts/index.js.map +1 -0
- package/lib/dev.server.js +7 -0
- package/lib/dev.server.js.map +1 -0
- package/lib/entities/ChatRequestEntity.js +318 -0
- package/lib/entities/ChatRequestEntity.js.map +1 -0
- package/lib/entities/McpRequestEntity.js +271 -0
- package/lib/entities/McpRequestEntity.js.map +1 -0
- package/lib/entities/RequestLogEntity.js +177 -0
- package/lib/entities/RequestLogEntity.js.map +1 -0
- package/lib/entities/ResponseEntity.js +150 -0
- package/lib/entities/ResponseEntity.js.map +1 -0
- package/lib/entities/index.js +11 -0
- package/lib/entities/index.js.map +1 -0
- package/lib/entities/types.js +11 -0
- package/lib/entities/types.js.map +1 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/lib/mcps-cli.js +44 -0
- package/lib/mcps-cli.js.map +1 -0
- package/lib/providers/McpServerHandlerDef.js +40 -0
- package/lib/providers/McpServerHandlerDef.js.map +1 -0
- package/lib/providers/findMcpServerDef.js +26 -0
- package/lib/providers/findMcpServerDef.js.map +1 -0
- package/lib/providers/prometheus/def.js +24 -0
- package/lib/providers/prometheus/def.js.map +1 -0
- package/lib/providers/prometheus/index.js +2 -0
- package/lib/providers/prometheus/index.js.map +1 -0
- package/lib/providers/relay/def.js +32 -0
- package/lib/providers/relay/def.js.map +1 -0
- package/lib/providers/relay/index.js +2 -0
- package/lib/providers/relay/index.js.map +1 -0
- package/lib/providers/sql/def.js +31 -0
- package/lib/providers/sql/def.js.map +1 -0
- package/lib/providers/sql/index.js +2 -0
- package/lib/providers/sql/index.js.map +1 -0
- package/lib/providers/tencent-cls/def.js +44 -0
- package/lib/providers/tencent-cls/def.js.map +1 -0
- package/lib/providers/tencent-cls/index.js +2 -0
- package/lib/providers/tencent-cls/index.js.map +1 -0
- package/lib/scripts/bundle.js +90 -0
- package/lib/scripts/bundle.js.map +1 -0
- package/lib/server/api-routes.js +96 -0
- package/lib/server/api-routes.js.map +1 -0
- package/lib/server/audit.js +274 -0
- package/lib/server/audit.js.map +1 -0
- package/lib/server/chat-routes.js +82 -0
- package/lib/server/chat-routes.js.map +1 -0
- package/lib/server/config.js +223 -0
- package/lib/server/config.js.map +1 -0
- package/lib/server/db.js +97 -0
- package/lib/server/db.js.map +1 -0
- package/lib/server/index.js +2 -0
- package/lib/server/index.js.map +1 -0
- package/lib/server/mcp-handler.js +167 -0
- package/lib/server/mcp-handler.js.map +1 -0
- package/lib/server/mcp-routes.js +112 -0
- package/lib/server/mcp-routes.js.map +1 -0
- package/lib/server/mcps-router.js +119 -0
- package/lib/server/mcps-router.js.map +1 -0
- package/lib/server/schema.js +129 -0
- package/lib/server/schema.js.map +1 -0
- package/lib/server/server.js +166 -0
- package/lib/server/server.js.map +1 -0
- package/lib/web/ChatPage.js +827 -0
- package/lib/web/ChatPage.js.map +1 -0
- package/lib/web/McpInspectorPage.js +214 -0
- package/lib/web/McpInspectorPage.js.map +1 -0
- package/lib/web/ServersPage.js +93 -0
- package/lib/web/ServersPage.js.map +1 -0
- package/lib/web/main.js +541 -0
- package/lib/web/main.js.map +1 -0
- package/package.json +83 -0
- package/src/chat/agent.ts +240 -0
- package/src/chat/audit.ts +377 -0
- package/src/chat/converters.test.ts +325 -0
- package/src/chat/converters.ts +459 -0
- package/src/chat/handler.test.ts +137 -0
- package/src/chat/handler.ts +1233 -0
- package/src/chat/index.ts +16 -0
- package/src/chat/types.ts +72 -0
- package/src/contracts/AuditContract.ts +93 -0
- package/src/contracts/McpsContract.ts +141 -0
- package/src/contracts/index.ts +18 -0
- package/src/dev.server.ts +7 -0
- package/src/entities/ChatRequestEntity.ts +157 -0
- package/src/entities/McpRequestEntity.ts +149 -0
- package/src/entities/RequestLogEntity.ts +78 -0
- package/src/entities/ResponseEntity.ts +75 -0
- package/src/entities/index.ts +12 -0
- package/src/entities/types.ts +188 -0
- package/src/index.ts +1 -0
- package/src/mcps-cli.ts +59 -0
- package/src/providers/McpServerHandlerDef.ts +105 -0
- package/src/providers/findMcpServerDef.ts +31 -0
- package/src/providers/prometheus/def.ts +21 -0
- package/src/providers/prometheus/index.ts +1 -0
- package/src/providers/relay/def.ts +31 -0
- package/src/providers/relay/index.ts +1 -0
- package/src/providers/relay/relay.test.ts +47 -0
- package/src/providers/sql/def.ts +33 -0
- package/src/providers/sql/index.ts +1 -0
- package/src/providers/tencent-cls/def.ts +38 -0
- package/src/providers/tencent-cls/index.ts +1 -0
- package/src/scripts/bundle.ts +82 -0
- package/src/server/api-routes.ts +98 -0
- package/src/server/audit.ts +310 -0
- package/src/server/chat-routes.ts +95 -0
- package/src/server/config.test.ts +162 -0
- package/src/server/config.ts +198 -0
- package/src/server/db.ts +115 -0
- package/src/server/index.ts +1 -0
- package/src/server/mcp-handler.ts +209 -0
- package/src/server/mcp-routes.ts +133 -0
- package/src/server/mcps-router.ts +133 -0
- package/src/server/schema.ts +175 -0
- package/src/server/server.ts +163 -0
- package/src/web/ChatPage.tsx +1005 -0
- package/src/web/McpInspectorPage.tsx +254 -0
- package/src/web/ServersPage.tsx +139 -0
- package/src/web/main.tsx +600 -0
- package/src/web/styles.css +15 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol converters between different AI model APIs
|
|
3
|
+
*
|
|
4
|
+
* These converters work with loosely-typed objects to support passthrough of
|
|
5
|
+
* provider-specific fields that aren't in the standard schema.
|
|
6
|
+
*/ // ============================================================================
|
|
7
|
+
// OpenAI to Anthropic Conversion
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Convert OpenAI messages to Anthropic messages
|
|
11
|
+
*/ export function openaiToAnthropicMessages(messages) {
|
|
12
|
+
let system;
|
|
13
|
+
const anthropicMessages = [];
|
|
14
|
+
for (const msg of messages){
|
|
15
|
+
if (msg.role === 'system') {
|
|
16
|
+
// Combine system messages
|
|
17
|
+
const contentStr = typeof msg.content === 'string' ? msg.content : '';
|
|
18
|
+
system = system ? `${system}\n\n${contentStr}` : contentStr;
|
|
19
|
+
} else if (msg.role === 'user') {
|
|
20
|
+
const content = typeof msg.content === 'string' ? msg.content : convertContentParts(msg.content ?? []);
|
|
21
|
+
anthropicMessages.push({
|
|
22
|
+
role: 'user',
|
|
23
|
+
content
|
|
24
|
+
});
|
|
25
|
+
} else if (msg.role === 'assistant') {
|
|
26
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
27
|
+
// Convert tool calls to tool_use blocks
|
|
28
|
+
const content = msg.tool_calls.map((tc)=>({
|
|
29
|
+
type: 'tool_use',
|
|
30
|
+
id: tc.id,
|
|
31
|
+
name: tc.function.name,
|
|
32
|
+
input: JSON.parse(tc.function.arguments || '{}')
|
|
33
|
+
}));
|
|
34
|
+
if (msg.content) {
|
|
35
|
+
const textContent = typeof msg.content === 'string' ? msg.content : '';
|
|
36
|
+
content.unshift({
|
|
37
|
+
type: 'text',
|
|
38
|
+
text: textContent
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
anthropicMessages.push({
|
|
42
|
+
role: 'assistant',
|
|
43
|
+
content
|
|
44
|
+
});
|
|
45
|
+
} else {
|
|
46
|
+
const contentStr = typeof msg.content === 'string' ? msg.content : '';
|
|
47
|
+
anthropicMessages.push({
|
|
48
|
+
role: 'assistant',
|
|
49
|
+
content: contentStr || ''
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
} else if (msg.role === 'tool') {
|
|
53
|
+
// Convert tool message to tool_result
|
|
54
|
+
const contentStr = typeof msg.content === 'string' ? msg.content : '';
|
|
55
|
+
anthropicMessages.push({
|
|
56
|
+
role: 'user',
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: 'tool_result',
|
|
60
|
+
tool_use_id: msg.tool_call_id ?? '',
|
|
61
|
+
content: contentStr
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
system,
|
|
69
|
+
messages: anthropicMessages
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function convertContentParts(parts) {
|
|
73
|
+
return parts.map((part)=>{
|
|
74
|
+
if (part.type === 'text') {
|
|
75
|
+
return {
|
|
76
|
+
type: 'text',
|
|
77
|
+
text: part.text
|
|
78
|
+
};
|
|
79
|
+
} else if (part.type === 'image_url') {
|
|
80
|
+
const url = part.image_url.url;
|
|
81
|
+
if (url.startsWith('data:')) {
|
|
82
|
+
// Base64 data URL
|
|
83
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
84
|
+
if (match) {
|
|
85
|
+
return {
|
|
86
|
+
type: 'image',
|
|
87
|
+
source: {
|
|
88
|
+
type: 'base64',
|
|
89
|
+
media_type: match[1],
|
|
90
|
+
data: match[2]
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// URL reference
|
|
96
|
+
return {
|
|
97
|
+
type: 'image',
|
|
98
|
+
source: {
|
|
99
|
+
type: 'url',
|
|
100
|
+
url
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return part;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Convert OpenAI request to Anthropic request
|
|
109
|
+
*/ export function openaiToAnthropicRequest(req) {
|
|
110
|
+
const { system, messages } = openaiToAnthropicMessages(req.messages);
|
|
111
|
+
const anthropicReq = {
|
|
112
|
+
model: req.model,
|
|
113
|
+
messages,
|
|
114
|
+
max_tokens: req.max_tokens || req.max_completion_tokens || 4096,
|
|
115
|
+
stream: req.stream
|
|
116
|
+
};
|
|
117
|
+
if (system) {
|
|
118
|
+
anthropicReq.system = system;
|
|
119
|
+
}
|
|
120
|
+
if (req.temperature !== undefined && req.temperature !== null) {
|
|
121
|
+
anthropicReq.temperature = Math.min(req.temperature, 1); // Anthropic max is 1
|
|
122
|
+
}
|
|
123
|
+
if (req.top_p !== undefined) {
|
|
124
|
+
anthropicReq.top_p = req.top_p;
|
|
125
|
+
}
|
|
126
|
+
if (req.stop) {
|
|
127
|
+
anthropicReq.stop_sequences = Array.isArray(req.stop) ? req.stop : [
|
|
128
|
+
req.stop
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
if (req.tools && req.tools.length > 0) {
|
|
132
|
+
anthropicReq.tools = req.tools.map((tool)=>({
|
|
133
|
+
name: tool.function.name,
|
|
134
|
+
description: tool.function.description,
|
|
135
|
+
input_schema: {
|
|
136
|
+
type: 'object',
|
|
137
|
+
properties: tool.function.parameters?.properties || {},
|
|
138
|
+
required: tool.function.parameters?.required || []
|
|
139
|
+
}
|
|
140
|
+
}));
|
|
141
|
+
if (req.tool_choice) {
|
|
142
|
+
if (req.tool_choice === 'auto') {
|
|
143
|
+
anthropicReq.tool_choice = {
|
|
144
|
+
type: 'auto'
|
|
145
|
+
};
|
|
146
|
+
} else if (req.tool_choice === 'required') {
|
|
147
|
+
anthropicReq.tool_choice = {
|
|
148
|
+
type: 'any'
|
|
149
|
+
};
|
|
150
|
+
} else if (req.tool_choice === 'none') {
|
|
151
|
+
// Anthropic doesn't have 'none', just don't include tools
|
|
152
|
+
delete anthropicReq.tools;
|
|
153
|
+
} else if (typeof req.tool_choice === 'object') {
|
|
154
|
+
anthropicReq.tool_choice = {
|
|
155
|
+
type: 'tool',
|
|
156
|
+
name: req.tool_choice.function.name
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return anthropicReq;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Convert Anthropic response to OpenAI response
|
|
165
|
+
*/ export function anthropicToOpenaiResponse(res, model) {
|
|
166
|
+
const toolCalls = [];
|
|
167
|
+
let textContent = '';
|
|
168
|
+
for (const block of res.content){
|
|
169
|
+
if (block.type === 'text') {
|
|
170
|
+
textContent += block.text;
|
|
171
|
+
} else if (block.type === 'tool_use') {
|
|
172
|
+
toolCalls.push({
|
|
173
|
+
id: block.id,
|
|
174
|
+
type: 'function',
|
|
175
|
+
function: {
|
|
176
|
+
name: block.name,
|
|
177
|
+
arguments: JSON.stringify(block.input)
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const finishReason = (()=>{
|
|
183
|
+
switch(res.stop_reason){
|
|
184
|
+
case 'end_turn':
|
|
185
|
+
return 'stop';
|
|
186
|
+
case 'max_tokens':
|
|
187
|
+
return 'length';
|
|
188
|
+
case 'tool_use':
|
|
189
|
+
return 'tool_calls';
|
|
190
|
+
case 'stop_sequence':
|
|
191
|
+
return 'stop';
|
|
192
|
+
default:
|
|
193
|
+
return 'stop';
|
|
194
|
+
}
|
|
195
|
+
})();
|
|
196
|
+
return {
|
|
197
|
+
id: res.id,
|
|
198
|
+
object: 'chat.completion',
|
|
199
|
+
created: Math.floor(Date.now() / 1000),
|
|
200
|
+
model: model,
|
|
201
|
+
choices: [
|
|
202
|
+
{
|
|
203
|
+
index: 0,
|
|
204
|
+
message: {
|
|
205
|
+
role: 'assistant',
|
|
206
|
+
content: textContent || null,
|
|
207
|
+
...toolCalls.length > 0 && {
|
|
208
|
+
tool_calls: toolCalls
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
finish_reason: finishReason
|
|
212
|
+
}
|
|
213
|
+
],
|
|
214
|
+
usage: {
|
|
215
|
+
prompt_tokens: res.usage.input_tokens,
|
|
216
|
+
completion_tokens: res.usage.output_tokens,
|
|
217
|
+
total_tokens: res.usage.input_tokens + res.usage.output_tokens
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Convert OpenAI messages to Gemini contents
|
|
223
|
+
*/ export function openaiToGeminiContents(messages) {
|
|
224
|
+
let systemInstruction;
|
|
225
|
+
const contents = [];
|
|
226
|
+
for (const msg of messages){
|
|
227
|
+
if (msg.role === 'system' || msg.role === 'developer') {
|
|
228
|
+
// Gemini uses systemInstruction
|
|
229
|
+
const text = typeof msg.content === 'string' ? msg.content : '';
|
|
230
|
+
if (systemInstruction) {
|
|
231
|
+
// Append to existing
|
|
232
|
+
systemInstruction.parts.push({
|
|
233
|
+
text
|
|
234
|
+
});
|
|
235
|
+
} else {
|
|
236
|
+
systemInstruction = {
|
|
237
|
+
role: 'user',
|
|
238
|
+
parts: [
|
|
239
|
+
{
|
|
240
|
+
text
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
} else if (msg.role === 'user') {
|
|
246
|
+
const contentArr = Array.isArray(msg.content) ? msg.content : null;
|
|
247
|
+
const parts = typeof msg.content === 'string' ? [
|
|
248
|
+
{
|
|
249
|
+
text: msg.content
|
|
250
|
+
}
|
|
251
|
+
] : (contentArr ?? []).map((c)=>{
|
|
252
|
+
if (c.type === 'text') {
|
|
253
|
+
return {
|
|
254
|
+
text: c.text
|
|
255
|
+
};
|
|
256
|
+
} else if (c.type === 'image_url') {
|
|
257
|
+
const url = c.image_url.url;
|
|
258
|
+
if (url.startsWith('data:')) {
|
|
259
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
260
|
+
if (match) {
|
|
261
|
+
return {
|
|
262
|
+
inlineData: {
|
|
263
|
+
mimeType: match[1],
|
|
264
|
+
data: match[2]
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
fileData: {
|
|
271
|
+
fileUri: url
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
text: ''
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
contents.push({
|
|
280
|
+
role: 'user',
|
|
281
|
+
parts
|
|
282
|
+
});
|
|
283
|
+
} else if (msg.role === 'assistant') {
|
|
284
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
285
|
+
const parts = msg.tool_calls.map((tc)=>({
|
|
286
|
+
functionCall: {
|
|
287
|
+
name: tc.function.name,
|
|
288
|
+
args: JSON.parse(tc.function.arguments || '{}')
|
|
289
|
+
}
|
|
290
|
+
}));
|
|
291
|
+
if (msg.content) {
|
|
292
|
+
const textContent = typeof msg.content === 'string' ? msg.content : '';
|
|
293
|
+
parts.unshift({
|
|
294
|
+
text: textContent
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
contents.push({
|
|
298
|
+
role: 'model',
|
|
299
|
+
parts
|
|
300
|
+
});
|
|
301
|
+
} else {
|
|
302
|
+
const textContent = typeof msg.content === 'string' ? msg.content : '';
|
|
303
|
+
contents.push({
|
|
304
|
+
role: 'model',
|
|
305
|
+
parts: [
|
|
306
|
+
{
|
|
307
|
+
text: textContent || ''
|
|
308
|
+
}
|
|
309
|
+
]
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
} else if (msg.role === 'tool') {
|
|
313
|
+
// Convert to function response
|
|
314
|
+
const contentStr = typeof msg.content === 'string' ? msg.content : '';
|
|
315
|
+
contents.push({
|
|
316
|
+
role: 'user',
|
|
317
|
+
parts: [
|
|
318
|
+
{
|
|
319
|
+
functionResponse: {
|
|
320
|
+
name: 'function',
|
|
321
|
+
response: {
|
|
322
|
+
result: contentStr
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
]
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
systemInstruction,
|
|
332
|
+
contents
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Convert OpenAI request to Gemini request
|
|
337
|
+
*/ export function openaiToGeminiRequest(req) {
|
|
338
|
+
const { systemInstruction, contents } = openaiToGeminiContents(req.messages);
|
|
339
|
+
const geminiReq = {
|
|
340
|
+
contents: contents
|
|
341
|
+
};
|
|
342
|
+
if (systemInstruction) {
|
|
343
|
+
geminiReq.systemInstruction = systemInstruction;
|
|
344
|
+
}
|
|
345
|
+
const generationConfig = {};
|
|
346
|
+
if (req.temperature !== undefined) {
|
|
347
|
+
generationConfig.temperature = req.temperature;
|
|
348
|
+
}
|
|
349
|
+
if (req.top_p !== undefined) {
|
|
350
|
+
generationConfig.topP = req.top_p;
|
|
351
|
+
}
|
|
352
|
+
if (req.max_tokens || req.max_completion_tokens) {
|
|
353
|
+
generationConfig.maxOutputTokens = req.max_tokens || req.max_completion_tokens;
|
|
354
|
+
}
|
|
355
|
+
if (req.stop) {
|
|
356
|
+
generationConfig.stopSequences = Array.isArray(req.stop) ? req.stop : [
|
|
357
|
+
req.stop
|
|
358
|
+
];
|
|
359
|
+
}
|
|
360
|
+
if (req.n) {
|
|
361
|
+
generationConfig.candidateCount = req.n;
|
|
362
|
+
}
|
|
363
|
+
if (Object.keys(generationConfig).length > 0) {
|
|
364
|
+
geminiReq.generationConfig = generationConfig;
|
|
365
|
+
}
|
|
366
|
+
if (req.tools && req.tools.length > 0) {
|
|
367
|
+
geminiReq.tools = [
|
|
368
|
+
{
|
|
369
|
+
functionDeclarations: req.tools.map((tool)=>({
|
|
370
|
+
name: tool.function.name,
|
|
371
|
+
description: tool.function.description,
|
|
372
|
+
parameters: tool.function.parameters
|
|
373
|
+
}))
|
|
374
|
+
}
|
|
375
|
+
];
|
|
376
|
+
if (req.tool_choice) {
|
|
377
|
+
if (req.tool_choice === 'auto') {
|
|
378
|
+
geminiReq.toolConfig = {
|
|
379
|
+
functionCallingConfig: {
|
|
380
|
+
mode: 'AUTO'
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
} else if (req.tool_choice === 'required') {
|
|
384
|
+
geminiReq.toolConfig = {
|
|
385
|
+
functionCallingConfig: {
|
|
386
|
+
mode: 'ANY'
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
} else if (req.tool_choice === 'none') {
|
|
390
|
+
geminiReq.toolConfig = {
|
|
391
|
+
functionCallingConfig: {
|
|
392
|
+
mode: 'NONE'
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
} else if (typeof req.tool_choice === 'object') {
|
|
396
|
+
geminiReq.toolConfig = {
|
|
397
|
+
functionCallingConfig: {
|
|
398
|
+
mode: 'ANY',
|
|
399
|
+
allowedFunctionNames: [
|
|
400
|
+
req.tool_choice.function.name
|
|
401
|
+
]
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return geminiReq;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Convert Gemini response to OpenAI response
|
|
411
|
+
*/ export function geminiToOpenaiResponse(res, model) {
|
|
412
|
+
const choices = (res.candidates || []).map((candidate, index)=>{
|
|
413
|
+
const toolCalls = [];
|
|
414
|
+
let textContent = '';
|
|
415
|
+
for (const part of candidate.content?.parts || []){
|
|
416
|
+
if ('text' in part) {
|
|
417
|
+
textContent += part.text;
|
|
418
|
+
} else if ('functionCall' in part) {
|
|
419
|
+
toolCalls.push({
|
|
420
|
+
id: `call_${Date.now()}_${index}`,
|
|
421
|
+
type: 'function',
|
|
422
|
+
function: {
|
|
423
|
+
name: part.functionCall.name,
|
|
424
|
+
arguments: JSON.stringify(part.functionCall.args)
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const finishReason = (()=>{
|
|
430
|
+
switch(candidate.finishReason){
|
|
431
|
+
case 'STOP':
|
|
432
|
+
return 'stop';
|
|
433
|
+
case 'MAX_TOKENS':
|
|
434
|
+
return 'length';
|
|
435
|
+
case 'SAFETY':
|
|
436
|
+
return 'content_filter';
|
|
437
|
+
default:
|
|
438
|
+
return 'stop';
|
|
439
|
+
}
|
|
440
|
+
})();
|
|
441
|
+
return {
|
|
442
|
+
index: candidate.index ?? index,
|
|
443
|
+
message: {
|
|
444
|
+
role: 'assistant',
|
|
445
|
+
content: textContent || null,
|
|
446
|
+
...toolCalls.length > 0 && {
|
|
447
|
+
tool_calls: toolCalls
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
finish_reason: finishReason
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
return {
|
|
454
|
+
id: `gemini-${Date.now()}`,
|
|
455
|
+
object: 'chat.completion',
|
|
456
|
+
created: Math.floor(Date.now() / 1000),
|
|
457
|
+
model,
|
|
458
|
+
choices,
|
|
459
|
+
usage: res.usageMetadata ? {
|
|
460
|
+
prompt_tokens: res.usageMetadata.promptTokenCount || 0,
|
|
461
|
+
completion_tokens: res.usageMetadata.candidatesTokenCount || 0,
|
|
462
|
+
total_tokens: res.usageMetadata.totalTokenCount || 0
|
|
463
|
+
} : undefined
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
//# sourceMappingURL=converters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/chat/converters.ts"],"sourcesContent":["/**\n * Protocol converters between different AI model APIs\n *\n * These converters work with loosely-typed objects to support passthrough of\n * provider-specific fields that aren't in the standard schema.\n */\nimport type { CreateChatCompletionRequest, CreateChatCompletionResponse, Message } from '@wener/ai/openai';\nimport type { CreateMessageRequest, CreateMessageResponse } from '@wener/ai/anthropic';\nimport type { CreateGenerateContentRequest, CreateGenerateContentResponse } from '@wener/ai/google';\n\n// Type aliases for converter functions\ntype ChatMessage = Message;\ntype AnthropicMessage = CreateMessageRequest['messages'][number];\n\n// ============================================================================\n// OpenAI to Anthropic Conversion\n// ============================================================================\n\n/**\n * Convert OpenAI messages to Anthropic messages\n */\nexport function openaiToAnthropicMessages(messages: ChatMessage[]): {\n\tsystem?: string;\n\tmessages: AnthropicMessage[];\n} {\n\tlet system: string | undefined;\n\tconst anthropicMessages: AnthropicMessage[] = [];\n\n\tfor (const msg of messages) {\n\t\tif (msg.role === 'system') {\n\t\t\t// Combine system messages\n\t\t\tconst contentStr = typeof msg.content === 'string' ? msg.content : '';\n\t\t\tsystem = system ? `${system}\\n\\n${contentStr}` : contentStr;\n\t\t} else if (msg.role === 'user') {\n\t\t\tconst content = typeof msg.content === 'string' ? msg.content : convertContentParts((msg.content as any[]) ?? []);\n\t\t\tanthropicMessages.push({ role: 'user', content } as AnthropicMessage);\n\t\t} else if (msg.role === 'assistant') {\n\t\t\tif (msg.tool_calls && msg.tool_calls.length > 0) {\n\t\t\t\t// Convert tool calls to tool_use blocks\n\t\t\t\tconst content = msg.tool_calls.map((tc: any) => ({\n\t\t\t\t\ttype: 'tool_use' as const,\n\t\t\t\t\tid: tc.id,\n\t\t\t\t\tname: tc.function.name,\n\t\t\t\t\tinput: JSON.parse(tc.function.arguments || '{}'),\n\t\t\t\t}));\n\t\t\t\tif (msg.content) {\n\t\t\t\t\tconst textContent = typeof msg.content === 'string' ? msg.content : '';\n\t\t\t\t\tcontent.unshift({ type: 'text' as any, text: textContent } as any);\n\t\t\t\t}\n\t\t\t\tanthropicMessages.push({ role: 'assistant', content } as AnthropicMessage);\n\t\t\t} else {\n\t\t\t\tconst contentStr = typeof msg.content === 'string' ? msg.content : '';\n\t\t\t\tanthropicMessages.push({ role: 'assistant', content: contentStr || '' } as AnthropicMessage);\n\t\t\t}\n\t\t} else if (msg.role === 'tool') {\n\t\t\t// Convert tool message to tool_result\n\t\t\tconst contentStr = typeof msg.content === 'string' ? msg.content : '';\n\t\t\tanthropicMessages.push({\n\t\t\t\trole: 'user',\n\t\t\t\tcontent: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'tool_result',\n\t\t\t\t\t\ttool_use_id: msg.tool_call_id ?? '',\n\t\t\t\t\t\tcontent: contentStr,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t} as AnthropicMessage);\n\t\t}\n\t}\n\n\treturn { system, messages: anthropicMessages };\n}\n\nfunction convertContentParts(parts: any[]): any[] {\n\treturn parts.map((part) => {\n\t\tif (part.type === 'text') {\n\t\t\treturn { type: 'text', text: part.text };\n\t\t} else if (part.type === 'image_url') {\n\t\t\tconst url = part.image_url.url;\n\t\t\tif (url.startsWith('data:')) {\n\t\t\t\t// Base64 data URL\n\t\t\t\tconst match = url.match(/^data:([^;]+);base64,(.+)$/);\n\t\t\t\tif (match) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: 'image',\n\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\ttype: 'base64',\n\t\t\t\t\t\t\tmedia_type: match[1],\n\t\t\t\t\t\t\tdata: match[2],\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\t// URL reference\n\t\t\treturn {\n\t\t\t\ttype: 'image',\n\t\t\t\tsource: {\n\t\t\t\t\ttype: 'url',\n\t\t\t\t\turl,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\treturn part;\n\t});\n}\n\n/**\n * Convert OpenAI request to Anthropic request\n */\nexport function openaiToAnthropicRequest(req: CreateChatCompletionRequest): CreateMessageRequest {\n\tconst { system, messages } = openaiToAnthropicMessages(req.messages);\n\n\tconst anthropicReq: CreateMessageRequest = {\n\t\tmodel: req.model,\n\t\tmessages,\n\t\tmax_tokens: req.max_tokens || req.max_completion_tokens || 4096,\n\t\tstream: req.stream,\n\t};\n\n\tif (system) {\n\t\tanthropicReq.system = system;\n\t}\n\n\tif (req.temperature !== undefined && req.temperature !== null) {\n\t\tanthropicReq.temperature = Math.min(req.temperature, 1); // Anthropic max is 1\n\t}\n\n\tif (req.top_p !== undefined) {\n\t\tanthropicReq.top_p = req.top_p;\n\t}\n\n\tif (req.stop) {\n\t\tanthropicReq.stop_sequences = Array.isArray(req.stop) ? req.stop : [req.stop];\n\t}\n\n\tif (req.tools && req.tools.length > 0) {\n\t\tanthropicReq.tools = req.tools.map((tool: any) => ({\n\t\t\tname: tool.function.name,\n\t\t\tdescription: tool.function.description,\n\t\t\tinput_schema: {\n\t\t\t\ttype: 'object' as const,\n\t\t\t\tproperties: (tool.function.parameters?.properties || {}) as Record<string, unknown>,\n\t\t\t\trequired: (tool.function.parameters?.required || []) as string[],\n\t\t\t},\n\t\t}));\n\n\t\tif (req.tool_choice) {\n\t\t\tif (req.tool_choice === 'auto') {\n\t\t\t\tanthropicReq.tool_choice = { type: 'auto' };\n\t\t\t} else if (req.tool_choice === 'required') {\n\t\t\t\tanthropicReq.tool_choice = { type: 'any' };\n\t\t\t} else if (req.tool_choice === 'none') {\n\t\t\t\t// Anthropic doesn't have 'none', just don't include tools\n\t\t\t\tdelete anthropicReq.tools;\n\t\t\t} else if (typeof req.tool_choice === 'object') {\n\t\t\t\tanthropicReq.tool_choice = {\n\t\t\t\t\ttype: 'tool',\n\t\t\t\t\tname: req.tool_choice.function.name,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\treturn anthropicReq;\n}\n\n/**\n * Convert Anthropic response to OpenAI response\n */\nexport function anthropicToOpenaiResponse(res: CreateMessageResponse, model: string): CreateChatCompletionResponse {\n\tconst toolCalls: any[] = [];\n\tlet textContent = '';\n\n\tfor (const block of res.content) {\n\t\tif (block.type === 'text') {\n\t\t\ttextContent += block.text;\n\t\t} else if (block.type === 'tool_use') {\n\t\t\ttoolCalls.push({\n\t\t\t\tid: block.id,\n\t\t\t\ttype: 'function',\n\t\t\t\tfunction: {\n\t\t\t\t\tname: block.name,\n\t\t\t\t\targuments: JSON.stringify(block.input),\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\tconst finishReason = (() => {\n\t\tswitch (res.stop_reason) {\n\t\t\tcase 'end_turn':\n\t\t\t\treturn 'stop';\n\t\t\tcase 'max_tokens':\n\t\t\t\treturn 'length';\n\t\t\tcase 'tool_use':\n\t\t\t\treturn 'tool_calls';\n\t\t\tcase 'stop_sequence':\n\t\t\t\treturn 'stop';\n\t\t\tdefault:\n\t\t\t\treturn 'stop';\n\t\t}\n\t})();\n\n\treturn {\n\t\tid: res.id,\n\t\tobject: 'chat.completion',\n\t\tcreated: Math.floor(Date.now() / 1000),\n\t\tmodel: model,\n\t\tchoices: [\n\t\t\t{\n\t\t\t\tindex: 0,\n\t\t\t\tmessage: {\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: textContent || null,\n\t\t\t\t\t...(toolCalls.length > 0 && { tool_calls: toolCalls }),\n\t\t\t\t},\n\t\t\t\tfinish_reason: finishReason as any,\n\t\t\t},\n\t\t],\n\t\tusage: {\n\t\t\tprompt_tokens: res.usage.input_tokens,\n\t\t\tcompletion_tokens: res.usage.output_tokens,\n\t\t\ttotal_tokens: res.usage.input_tokens + res.usage.output_tokens,\n\t\t},\n\t};\n}\n\n// ============================================================================\n// OpenAI to Gemini Conversion\n// ============================================================================\n\n// Gemini content type for converter\ntype GeminiContentPart = {\n\ttext?: string;\n\tinlineData?: { mimeType: string; data: string };\n\tfunctionCall?: unknown;\n\tfunctionResponse?: unknown;\n};\ntype GeminiContent = { role: string; parts: GeminiContentPart[] };\n\n/**\n * Convert OpenAI messages to Gemini contents\n */\nexport function openaiToGeminiContents(messages: ChatMessage[]): {\n\tsystemInstruction?: GeminiContent;\n\tcontents: GeminiContent[];\n} {\n\tlet systemInstruction: GeminiContent | undefined;\n\tconst contents: GeminiContent[] = [];\n\n\tfor (const msg of messages) {\n\t\tif (msg.role === 'system' || msg.role === 'developer') {\n\t\t\t// Gemini uses systemInstruction\n\t\t\tconst text = typeof msg.content === 'string' ? msg.content : '';\n\t\t\tif (systemInstruction) {\n\t\t\t\t// Append to existing\n\t\t\t\tsystemInstruction.parts.push({ text });\n\t\t\t} else {\n\t\t\t\tsystemInstruction = {\n\t\t\t\t\trole: 'user' as const, // Gemini system instruction doesn't have role, but we use 'user'\n\t\t\t\t\tparts: [{ text }],\n\t\t\t\t};\n\t\t\t}\n\t\t} else if (msg.role === 'user') {\n\t\t\tconst contentArr = Array.isArray(msg.content) ? msg.content : null;\n\t\t\tconst parts =\n\t\t\t\ttypeof msg.content === 'string'\n\t\t\t\t\t? [{ text: msg.content }]\n\t\t\t\t\t: (contentArr ?? []).map((c: any) => {\n\t\t\t\t\t\t\tif (c.type === 'text') {\n\t\t\t\t\t\t\t\treturn { text: c.text };\n\t\t\t\t\t\t\t} else if (c.type === 'image_url') {\n\t\t\t\t\t\t\t\tconst url = c.image_url.url;\n\t\t\t\t\t\t\t\tif (url.startsWith('data:')) {\n\t\t\t\t\t\t\t\t\tconst match = url.match(/^data:([^;]+);base64,(.+)$/);\n\t\t\t\t\t\t\t\t\tif (match) {\n\t\t\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t\t\tinlineData: {\n\t\t\t\t\t\t\t\t\t\t\t\tmimeType: match[1],\n\t\t\t\t\t\t\t\t\t\t\t\tdata: match[2],\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn { fileData: { fileUri: url } };\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn { text: '' };\n\t\t\t\t\t\t});\n\t\t\tcontents.push({ role: 'user' as const, parts });\n\t\t} else if (msg.role === 'assistant') {\n\t\t\tif (msg.tool_calls && msg.tool_calls.length > 0) {\n\t\t\t\tconst parts = msg.tool_calls.map((tc: any) => ({\n\t\t\t\t\tfunctionCall: {\n\t\t\t\t\t\tname: tc.function.name,\n\t\t\t\t\t\targs: JSON.parse(tc.function.arguments || '{}'),\n\t\t\t\t\t},\n\t\t\t\t}));\n\t\t\t\tif (msg.content) {\n\t\t\t\t\tconst textContent = typeof msg.content === 'string' ? msg.content : '';\n\t\t\t\t\tparts.unshift({ text: textContent } as any);\n\t\t\t\t}\n\t\t\t\tcontents.push({ role: 'model' as const, parts });\n\t\t\t} else {\n\t\t\t\tconst textContent = typeof msg.content === 'string' ? msg.content : '';\n\t\t\t\tcontents.push({ role: 'model' as const, parts: [{ text: textContent || '' }] });\n\t\t\t}\n\t\t} else if (msg.role === 'tool') {\n\t\t\t// Convert to function response\n\t\t\tconst contentStr = typeof msg.content === 'string' ? msg.content : '';\n\t\t\tcontents.push({\n\t\t\t\trole: 'user' as const,\n\t\t\t\tparts: [\n\t\t\t\t\t{\n\t\t\t\t\t\tfunctionResponse: {\n\t\t\t\t\t\t\tname: 'function', // We don't have the function name in tool message\n\t\t\t\t\t\t\tresponse: { result: contentStr },\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t}\n\t}\n\n\treturn { systemInstruction, contents };\n}\n\n/**\n * Convert OpenAI request to Gemini request\n */\nexport function openaiToGeminiRequest(req: CreateChatCompletionRequest): CreateGenerateContentRequest {\n\tconst { systemInstruction, contents } = openaiToGeminiContents(req.messages);\n\n\tconst geminiReq: CreateGenerateContentRequest = {\n\t\tcontents: contents as CreateGenerateContentRequest['contents'],\n\t};\n\n\tif (systemInstruction) {\n\t\tgeminiReq.systemInstruction = systemInstruction as CreateGenerateContentRequest['systemInstruction'];\n\t}\n\n\tconst generationConfig: any = {};\n\n\tif (req.temperature !== undefined) {\n\t\tgenerationConfig.temperature = req.temperature;\n\t}\n\tif (req.top_p !== undefined) {\n\t\tgenerationConfig.topP = req.top_p;\n\t}\n\tif (req.max_tokens || req.max_completion_tokens) {\n\t\tgenerationConfig.maxOutputTokens = req.max_tokens || req.max_completion_tokens;\n\t}\n\tif (req.stop) {\n\t\tgenerationConfig.stopSequences = Array.isArray(req.stop) ? req.stop : [req.stop];\n\t}\n\tif (req.n) {\n\t\tgenerationConfig.candidateCount = req.n;\n\t}\n\n\tif (Object.keys(generationConfig).length > 0) {\n\t\tgeminiReq.generationConfig = generationConfig;\n\t}\n\n\tif (req.tools && req.tools.length > 0) {\n\t\tgeminiReq.tools = [\n\t\t\t{\n\t\t\t\tfunctionDeclarations: req.tools.map((tool: any) => ({\n\t\t\t\t\tname: tool.function.name,\n\t\t\t\t\tdescription: tool.function.description,\n\t\t\t\t\tparameters: tool.function.parameters,\n\t\t\t\t})),\n\t\t\t},\n\t\t];\n\n\t\tif (req.tool_choice) {\n\t\t\tif (req.tool_choice === 'auto') {\n\t\t\t\tgeminiReq.toolConfig = { functionCallingConfig: { mode: 'AUTO' } };\n\t\t\t} else if (req.tool_choice === 'required') {\n\t\t\t\tgeminiReq.toolConfig = { functionCallingConfig: { mode: 'ANY' } };\n\t\t\t} else if (req.tool_choice === 'none') {\n\t\t\t\tgeminiReq.toolConfig = { functionCallingConfig: { mode: 'NONE' } };\n\t\t\t} else if (typeof req.tool_choice === 'object') {\n\t\t\t\tgeminiReq.toolConfig = {\n\t\t\t\t\tfunctionCallingConfig: {\n\t\t\t\t\t\tmode: 'ANY',\n\t\t\t\t\t\tallowedFunctionNames: [req.tool_choice.function.name],\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\treturn geminiReq;\n}\n\n/**\n * Convert Gemini response to OpenAI response\n */\nexport function geminiToOpenaiResponse(\n\tres: CreateGenerateContentResponse,\n\tmodel: string,\n): CreateChatCompletionResponse {\n\tconst choices = (res.candidates || []).map((candidate: any, index: number) => {\n\t\tconst toolCalls: any[] = [];\n\t\tlet textContent = '';\n\n\t\tfor (const part of candidate.content?.parts || []) {\n\t\t\tif ('text' in part) {\n\t\t\t\ttextContent += part.text;\n\t\t\t} else if ('functionCall' in part) {\n\t\t\t\ttoolCalls.push({\n\t\t\t\t\tid: `call_${Date.now()}_${index}`,\n\t\t\t\t\ttype: 'function',\n\t\t\t\t\tfunction: {\n\t\t\t\t\t\tname: part.functionCall.name,\n\t\t\t\t\t\targuments: JSON.stringify(part.functionCall.args),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst finishReason = (() => {\n\t\t\tswitch (candidate.finishReason) {\n\t\t\t\tcase 'STOP':\n\t\t\t\t\treturn 'stop';\n\t\t\t\tcase 'MAX_TOKENS':\n\t\t\t\t\treturn 'length';\n\t\t\t\tcase 'SAFETY':\n\t\t\t\t\treturn 'content_filter';\n\t\t\t\tdefault:\n\t\t\t\t\treturn 'stop';\n\t\t\t}\n\t\t})();\n\n\t\treturn {\n\t\t\tindex: candidate.index ?? index,\n\t\t\tmessage: {\n\t\t\t\trole: 'assistant' as const,\n\t\t\t\tcontent: textContent || null,\n\t\t\t\t...(toolCalls.length > 0 && { tool_calls: toolCalls }),\n\t\t\t},\n\t\t\tfinish_reason: finishReason as any,\n\t\t};\n\t});\n\n\treturn {\n\t\tid: `gemini-${Date.now()}`,\n\t\tobject: 'chat.completion',\n\t\tcreated: Math.floor(Date.now() / 1000),\n\t\tmodel,\n\t\tchoices,\n\t\tusage: res.usageMetadata\n\t\t\t? {\n\t\t\t\t\tprompt_tokens: res.usageMetadata.promptTokenCount || 0,\n\t\t\t\t\tcompletion_tokens: res.usageMetadata.candidatesTokenCount || 0,\n\t\t\t\t\ttotal_tokens: res.usageMetadata.totalTokenCount || 0,\n\t\t\t\t}\n\t\t\t: undefined,\n\t};\n}\n"],"names":["openaiToAnthropicMessages","messages","system","anthropicMessages","msg","role","contentStr","content","convertContentParts","push","tool_calls","length","map","tc","type","id","name","function","input","JSON","parse","arguments","textContent","unshift","text","tool_use_id","tool_call_id","parts","part","url","image_url","startsWith","match","source","media_type","data","openaiToAnthropicRequest","req","anthropicReq","model","max_tokens","max_completion_tokens","stream","temperature","undefined","Math","min","top_p","stop","stop_sequences","Array","isArray","tools","tool","description","input_schema","properties","parameters","required","tool_choice","anthropicToOpenaiResponse","res","toolCalls","block","stringify","finishReason","stop_reason","object","created","floor","Date","now","choices","index","message","finish_reason","usage","prompt_tokens","input_tokens","completion_tokens","output_tokens","total_tokens","openaiToGeminiContents","systemInstruction","contents","contentArr","c","inlineData","mimeType","fileData","fileUri","functionCall","args","functionResponse","response","result","openaiToGeminiRequest","geminiReq","generationConfig","topP","maxOutputTokens","stopSequences","n","candidateCount","Object","keys","functionDeclarations","toolConfig","functionCallingConfig","mode","allowedFunctionNames","geminiToOpenaiResponse","candidates","candidate","usageMetadata","promptTokenCount","candidatesTokenCount","totalTokenCount"],"mappings":"AAAA;;;;;CAKC,GASD,+EAA+E;AAC/E,iCAAiC;AACjC,+EAA+E;AAE/E;;CAEC,GACD,OAAO,SAASA,0BAA0BC,QAAuB;IAIhE,IAAIC;IACJ,MAAMC,oBAAwC,EAAE;IAEhD,KAAK,MAAMC,OAAOH,SAAU;QAC3B,IAAIG,IAAIC,IAAI,KAAK,UAAU;YAC1B,0BAA0B;YAC1B,MAAMC,aAAa,OAAOF,IAAIG,OAAO,KAAK,WAAWH,IAAIG,OAAO,GAAG;YACnEL,SAASA,SAAS,GAAGA,OAAO,IAAI,EAAEI,YAAY,GAAGA;QAClD,OAAO,IAAIF,IAAIC,IAAI,KAAK,QAAQ;YAC/B,MAAME,UAAU,OAAOH,IAAIG,OAAO,KAAK,WAAWH,IAAIG,OAAO,GAAGC,oBAAoB,AAACJ,IAAIG,OAAO,IAAc,EAAE;YAChHJ,kBAAkBM,IAAI,CAAC;gBAAEJ,MAAM;gBAAQE;YAAQ;QAChD,OAAO,IAAIH,IAAIC,IAAI,KAAK,aAAa;YACpC,IAAID,IAAIM,UAAU,IAAIN,IAAIM,UAAU,CAACC,MAAM,GAAG,GAAG;gBAChD,wCAAwC;gBACxC,MAAMJ,UAAUH,IAAIM,UAAU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