@zenmux/rosetta-ai 0.0.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/README.md +399 -0
- package/dist/chat-completions/gemini.d.ts +25 -0
- package/dist/chat-completions/gemini.d.ts.map +1 -0
- package/dist/chat-completions/gemini.js +537 -0
- package/dist/chat-completions/gemini.js.map +1 -0
- package/dist/chat-completions/messages.d.ts +30 -0
- package/dist/chat-completions/messages.d.ts.map +1 -0
- package/dist/chat-completions/messages.js +548 -0
- package/dist/chat-completions/messages.js.map +1 -0
- package/dist/chat-completions/responses.d.ts +33 -0
- package/dist/chat-completions/responses.d.ts.map +1 -0
- package/dist/chat-completions/responses.js +580 -0
- package/dist/chat-completions/responses.js.map +1 -0
- package/dist/gemini/chat-completions.d.ts +19 -0
- package/dist/gemini/chat-completions.d.ts.map +1 -0
- package/dist/gemini/chat-completions.js +417 -0
- package/dist/gemini/chat-completions.js.map +1 -0
- package/dist/gemini/messages.d.ts +20 -0
- package/dist/gemini/messages.d.ts.map +1 -0
- package/dist/gemini/messages.js +404 -0
- package/dist/gemini/messages.js.map +1 -0
- package/dist/gemini/responses.d.ts +20 -0
- package/dist/gemini/responses.d.ts.map +1 -0
- package/dist/gemini/responses.js +400 -0
- package/dist/gemini/responses.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/chat-completions.d.ts +25 -0
- package/dist/messages/chat-completions.d.ts.map +1 -0
- package/dist/messages/chat-completions.js +598 -0
- package/dist/messages/chat-completions.js.map +1 -0
- package/dist/messages/gemini.d.ts +22 -0
- package/dist/messages/gemini.d.ts.map +1 -0
- package/dist/messages/gemini.js +544 -0
- package/dist/messages/gemini.js.map +1 -0
- package/dist/messages/responses.d.ts +19 -0
- package/dist/messages/responses.d.ts.map +1 -0
- package/dist/messages/responses.js +503 -0
- package/dist/messages/responses.js.map +1 -0
- package/dist/responses/chat-completions.d.ts +23 -0
- package/dist/responses/chat-completions.d.ts.map +1 -0
- package/dist/responses/chat-completions.js +611 -0
- package/dist/responses/chat-completions.js.map +1 -0
- package/dist/responses/gemini.d.ts +26 -0
- package/dist/responses/gemini.d.ts.map +1 -0
- package/dist/responses/gemini.js +654 -0
- package/dist/responses/gemini.js.map +1 -0
- package/dist/responses/messages.d.ts +26 -0
- package/dist/responses/messages.d.ts.map +1 -0
- package/dist/responses/messages.js +656 -0
- package/dist/responses/messages.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResponsesToGeminiConverter = void 0;
|
|
4
|
+
class ResponsesToGeminiConverter {
|
|
5
|
+
streamState;
|
|
6
|
+
constructor() {
|
|
7
|
+
this.streamState = this.createStreamState();
|
|
8
|
+
}
|
|
9
|
+
// --- Request conversion (Responses → Gemini, forward) ---
|
|
10
|
+
convertRequest(params) {
|
|
11
|
+
const systemParts = [];
|
|
12
|
+
const contents = [];
|
|
13
|
+
if (params.instructions) {
|
|
14
|
+
systemParts.push({ text: params.instructions });
|
|
15
|
+
}
|
|
16
|
+
this.convertInput(systemParts, contents, params.input);
|
|
17
|
+
const config = {};
|
|
18
|
+
if (systemParts.length > 0) {
|
|
19
|
+
config.systemInstruction = { parts: systemParts };
|
|
20
|
+
}
|
|
21
|
+
if (params.max_output_tokens != null) {
|
|
22
|
+
config.maxOutputTokens = params.max_output_tokens;
|
|
23
|
+
}
|
|
24
|
+
if (params.temperature != null) {
|
|
25
|
+
config.temperature = params.temperature;
|
|
26
|
+
}
|
|
27
|
+
if (params.top_p != null) {
|
|
28
|
+
config.topP = params.top_p;
|
|
29
|
+
}
|
|
30
|
+
if (params.tools) {
|
|
31
|
+
const { tools, hasGoogleSearch } = this.convertTools(params.tools);
|
|
32
|
+
if (tools.length > 0) {
|
|
33
|
+
config.tools = [{ functionDeclarations: tools }];
|
|
34
|
+
}
|
|
35
|
+
if (hasGoogleSearch) {
|
|
36
|
+
config.tools = [...(config.tools ?? []), { googleSearch: {} }];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (params.tool_choice != null) {
|
|
40
|
+
config.toolConfig = {
|
|
41
|
+
functionCallingConfig: this.convertToolChoice(params.tool_choice),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (params.reasoning) {
|
|
45
|
+
config.thinkingConfig = this.convertReasoning(params.reasoning);
|
|
46
|
+
}
|
|
47
|
+
if (params.text?.format) {
|
|
48
|
+
this.applyTextFormat(config, params.text.format);
|
|
49
|
+
}
|
|
50
|
+
if (params.include) {
|
|
51
|
+
for (const inc of params.include) {
|
|
52
|
+
if (inc === "message.output_text.logprobs") {
|
|
53
|
+
config.responseLogprobs = true;
|
|
54
|
+
config.logprobs = 20;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
model: params.model,
|
|
61
|
+
contents,
|
|
62
|
+
config,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// --- Response conversion (Gemini → Responses, backward) ---
|
|
66
|
+
convertResponse(response) {
|
|
67
|
+
const candidate = response.candidates?.[0];
|
|
68
|
+
const parts = candidate?.content?.parts ?? [];
|
|
69
|
+
const output = [];
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
if (part.thought && part.text) {
|
|
72
|
+
output.push({
|
|
73
|
+
type: "reasoning",
|
|
74
|
+
id: `rs_${this.generateId()}`,
|
|
75
|
+
summary: [{ type: "summary_text", text: part.text }],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const part of parts) {
|
|
80
|
+
if (part.functionCall) {
|
|
81
|
+
const fc = part.functionCall;
|
|
82
|
+
output.push({
|
|
83
|
+
type: "function_call",
|
|
84
|
+
id: `fc_${this.generateId()}`,
|
|
85
|
+
call_id: fc.id ?? fc.name ?? `call_${this.generateId()}`,
|
|
86
|
+
name: fc.name ?? "",
|
|
87
|
+
arguments: JSON.stringify(fc.args ?? {}),
|
|
88
|
+
status: "completed",
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const textParts = [];
|
|
93
|
+
const annotations = [];
|
|
94
|
+
for (const part of parts) {
|
|
95
|
+
if (part.text != null && !part.thought) {
|
|
96
|
+
textParts.push(part.text);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.extractGroundingAnnotations(candidate, annotations);
|
|
100
|
+
if (textParts.length > 0 || annotations.length > 0) {
|
|
101
|
+
const content = [];
|
|
102
|
+
content.push({
|
|
103
|
+
type: "output_text",
|
|
104
|
+
text: textParts.join(""),
|
|
105
|
+
annotations,
|
|
106
|
+
logprobs: null,
|
|
107
|
+
});
|
|
108
|
+
output.push({
|
|
109
|
+
type: "message",
|
|
110
|
+
id: `msg_${this.generateId()}`,
|
|
111
|
+
role: "assistant",
|
|
112
|
+
status: "completed",
|
|
113
|
+
content,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (output.length === 0) {
|
|
117
|
+
output.push({
|
|
118
|
+
type: "message",
|
|
119
|
+
id: `msg_${this.generateId()}`,
|
|
120
|
+
role: "assistant",
|
|
121
|
+
status: "completed",
|
|
122
|
+
content: [{ type: "output_text", text: "", annotations: [], logprobs: null }],
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const hasToolCall = output.some(o => o.type === "function_call");
|
|
126
|
+
const status = this.finishReasonToStatus(candidate?.finishReason, hasToolCall);
|
|
127
|
+
const usage = response.usageMetadata;
|
|
128
|
+
const promptTokens = (usage?.promptTokenCount ?? 0) + (usage?.toolUsePromptTokenCount ?? 0);
|
|
129
|
+
const thoughtsTokens = usage?.thoughtsTokenCount ?? 0;
|
|
130
|
+
const candidatesTokens = (usage?.candidatesTokenCount ?? 0) + thoughtsTokens;
|
|
131
|
+
return {
|
|
132
|
+
id: response.responseId ?? `resp_${this.generateId()}`,
|
|
133
|
+
object: "response",
|
|
134
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
135
|
+
model: response.modelVersion ?? "",
|
|
136
|
+
output,
|
|
137
|
+
status,
|
|
138
|
+
error: null,
|
|
139
|
+
incomplete_details: status === "incomplete" ? { reason: "max_output_tokens" } : null,
|
|
140
|
+
instructions: null,
|
|
141
|
+
metadata: {},
|
|
142
|
+
temperature: null,
|
|
143
|
+
top_p: null,
|
|
144
|
+
max_output_tokens: null,
|
|
145
|
+
previous_response_id: null,
|
|
146
|
+
parallel_tool_calls: true,
|
|
147
|
+
tool_choice: "auto",
|
|
148
|
+
tools: [],
|
|
149
|
+
text: { format: { type: "text" } },
|
|
150
|
+
reasoning: null,
|
|
151
|
+
truncation: null,
|
|
152
|
+
user: undefined,
|
|
153
|
+
usage: {
|
|
154
|
+
input_tokens: promptTokens,
|
|
155
|
+
output_tokens: candidatesTokens,
|
|
156
|
+
total_tokens: usage?.totalTokenCount ?? 0,
|
|
157
|
+
input_tokens_details: {
|
|
158
|
+
cached_tokens: usage?.cachedContentTokenCount ?? 0,
|
|
159
|
+
},
|
|
160
|
+
output_tokens_details: {
|
|
161
|
+
reasoning_tokens: thoughtsTokens,
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
// --- Stream conversion (Gemini → Responses, backward) ---
|
|
167
|
+
async *convertStream(stream) {
|
|
168
|
+
for await (const chunk of stream) {
|
|
169
|
+
const events = this.convertStreamChunk(chunk);
|
|
170
|
+
for (const event of events) {
|
|
171
|
+
yield event;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
convertStreamChunk(chunk) {
|
|
176
|
+
const state = this.streamState;
|
|
177
|
+
const events = [];
|
|
178
|
+
const candidate = chunk.candidates?.[0];
|
|
179
|
+
const parts = candidate?.content?.parts ?? [];
|
|
180
|
+
if (chunk.modelVersion) {
|
|
181
|
+
state.model = chunk.modelVersion;
|
|
182
|
+
}
|
|
183
|
+
if (chunk.responseId) {
|
|
184
|
+
state.id = chunk.responseId;
|
|
185
|
+
}
|
|
186
|
+
if (!state.started) {
|
|
187
|
+
state.started = true;
|
|
188
|
+
if (!state.id) {
|
|
189
|
+
state.id = `resp_${this.generateId()}`;
|
|
190
|
+
}
|
|
191
|
+
const skeleton = this.makeSkeletonResponse();
|
|
192
|
+
events.push({
|
|
193
|
+
type: "response.created",
|
|
194
|
+
response: skeleton,
|
|
195
|
+
sequence_number: state.seq++,
|
|
196
|
+
});
|
|
197
|
+
events.push({
|
|
198
|
+
type: "response.in_progress",
|
|
199
|
+
response: skeleton,
|
|
200
|
+
sequence_number: state.seq++,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
for (const part of parts) {
|
|
204
|
+
if (part.thought && part.text) {
|
|
205
|
+
const newThought = part.text.slice(state.prevThought.length);
|
|
206
|
+
if (newThought) {
|
|
207
|
+
if (!state.reasoningStarted) {
|
|
208
|
+
state.reasoningStarted = true;
|
|
209
|
+
events.push({
|
|
210
|
+
type: "response.output_item.added",
|
|
211
|
+
item: {
|
|
212
|
+
type: "reasoning",
|
|
213
|
+
id: `rs_${this.generateId()}`,
|
|
214
|
+
summary: [],
|
|
215
|
+
},
|
|
216
|
+
output_index: state.outputIndex,
|
|
217
|
+
sequence_number: state.seq++,
|
|
218
|
+
});
|
|
219
|
+
events.push({
|
|
220
|
+
type: "response.reasoning_summary_part.added",
|
|
221
|
+
item_id: `rs_${this.generateId()}`,
|
|
222
|
+
output_index: state.outputIndex,
|
|
223
|
+
summary_index: 0,
|
|
224
|
+
part: { type: "summary_text", text: "" },
|
|
225
|
+
sequence_number: state.seq++,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
state.prevThought = part.text;
|
|
229
|
+
events.push({
|
|
230
|
+
type: "response.reasoning_summary_text.delta",
|
|
231
|
+
item_id: `rs_${this.generateId()}`,
|
|
232
|
+
output_index: state.outputIndex,
|
|
233
|
+
summary_index: 0,
|
|
234
|
+
delta: newThought,
|
|
235
|
+
sequence_number: state.seq++,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
else if (part.functionCall) {
|
|
240
|
+
const fc = part.functionCall;
|
|
241
|
+
const fcId = fc.id ?? fc.name ?? "";
|
|
242
|
+
if (!state.seenFunctionCallIds.has(fcId)) {
|
|
243
|
+
state.seenFunctionCallIds.add(fcId);
|
|
244
|
+
if (state.reasoningStarted && state.toolCallCount === 0) {
|
|
245
|
+
state.outputIndex++;
|
|
246
|
+
}
|
|
247
|
+
state.toolCallCount++;
|
|
248
|
+
const toolOutputIndex = state.outputIndex + state.toolCallCount - 1;
|
|
249
|
+
events.push({
|
|
250
|
+
type: "response.output_item.added",
|
|
251
|
+
item: {
|
|
252
|
+
type: "function_call",
|
|
253
|
+
id: `fc_${this.generateId()}`,
|
|
254
|
+
call_id: fc.id ?? `call_${this.generateId()}`,
|
|
255
|
+
name: fc.name ?? "",
|
|
256
|
+
arguments: "",
|
|
257
|
+
status: "in_progress",
|
|
258
|
+
},
|
|
259
|
+
output_index: toolOutputIndex,
|
|
260
|
+
sequence_number: state.seq++,
|
|
261
|
+
});
|
|
262
|
+
const args = JSON.stringify(fc.args ?? {});
|
|
263
|
+
if (args !== "{}") {
|
|
264
|
+
events.push({
|
|
265
|
+
type: "response.function_call_arguments.delta",
|
|
266
|
+
item_id: `fc_${this.generateId()}`,
|
|
267
|
+
output_index: toolOutputIndex,
|
|
268
|
+
delta: args,
|
|
269
|
+
sequence_number: state.seq++,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else if (part.text != null) {
|
|
275
|
+
const newText = part.text.slice(state.prevText.length);
|
|
276
|
+
if (newText) {
|
|
277
|
+
if (!state.textMessageStarted) {
|
|
278
|
+
state.textMessageStarted = true;
|
|
279
|
+
const msgOutputIndex = state.outputIndex + (state.reasoningStarted ? 1 : 0) + state.toolCallCount;
|
|
280
|
+
events.push({
|
|
281
|
+
type: "response.output_item.added",
|
|
282
|
+
item: {
|
|
283
|
+
type: "message",
|
|
284
|
+
id: `msg_${this.generateId()}`,
|
|
285
|
+
role: "assistant",
|
|
286
|
+
status: "in_progress",
|
|
287
|
+
content: [],
|
|
288
|
+
},
|
|
289
|
+
output_index: msgOutputIndex,
|
|
290
|
+
sequence_number: state.seq++,
|
|
291
|
+
});
|
|
292
|
+
events.push({
|
|
293
|
+
type: "response.content_part.added",
|
|
294
|
+
item_id: `msg_${this.generateId()}`,
|
|
295
|
+
output_index: msgOutputIndex,
|
|
296
|
+
content_index: 0,
|
|
297
|
+
part: { type: "output_text", text: "", annotations: [] },
|
|
298
|
+
sequence_number: state.seq++,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
state.prevText = part.text;
|
|
302
|
+
events.push({
|
|
303
|
+
type: "response.output_text.delta",
|
|
304
|
+
item_id: `msg_${this.generateId()}`,
|
|
305
|
+
output_index: state.outputIndex + (state.reasoningStarted ? 1 : 0) + state.toolCallCount,
|
|
306
|
+
content_index: 0,
|
|
307
|
+
delta: newText,
|
|
308
|
+
sequence_number: state.seq++,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (candidate?.finishReason) {
|
|
314
|
+
const hasToolCall = state.seenFunctionCallIds.size > 0;
|
|
315
|
+
const status = this.finishReasonToStatus(candidate.finishReason, hasToolCall);
|
|
316
|
+
const resp = this.makeSkeletonResponse();
|
|
317
|
+
resp.status = status;
|
|
318
|
+
const usage = chunk.usageMetadata;
|
|
319
|
+
if (usage) {
|
|
320
|
+
const promptTokens = (usage.promptTokenCount ?? 0) + (usage.toolUsePromptTokenCount ?? 0);
|
|
321
|
+
const thoughtsTokens = usage.thoughtsTokenCount ?? 0;
|
|
322
|
+
const candidatesTokens = (usage.candidatesTokenCount ?? 0) + thoughtsTokens;
|
|
323
|
+
resp.usage = {
|
|
324
|
+
input_tokens: promptTokens,
|
|
325
|
+
output_tokens: candidatesTokens,
|
|
326
|
+
total_tokens: usage.totalTokenCount ?? 0,
|
|
327
|
+
input_tokens_details: {
|
|
328
|
+
cached_tokens: usage.cachedContentTokenCount ?? 0,
|
|
329
|
+
},
|
|
330
|
+
output_tokens_details: {
|
|
331
|
+
reasoning_tokens: thoughtsTokens,
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
if (status === "incomplete") {
|
|
336
|
+
events.push({
|
|
337
|
+
type: "response.incomplete",
|
|
338
|
+
response: resp,
|
|
339
|
+
sequence_number: state.seq++,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
events.push({
|
|
344
|
+
type: "response.completed",
|
|
345
|
+
response: resp,
|
|
346
|
+
sequence_number: state.seq++,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return events;
|
|
351
|
+
}
|
|
352
|
+
// --- Private: request helpers ---
|
|
353
|
+
convertInput(systemParts, contents, input) {
|
|
354
|
+
if (typeof input === "string") {
|
|
355
|
+
contents.push({ role: "user", parts: [{ text: input }] });
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const pendingFunctionCalls = [];
|
|
359
|
+
const flushFunctionCalls = () => {
|
|
360
|
+
if (pendingFunctionCalls.length > 0) {
|
|
361
|
+
contents.push({ role: "model", parts: [...pendingFunctionCalls] });
|
|
362
|
+
pendingFunctionCalls.length = 0;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
for (const item of input) {
|
|
366
|
+
const typed = item;
|
|
367
|
+
if (typed.type === "message") {
|
|
368
|
+
flushFunctionCalls();
|
|
369
|
+
if (typed.role === "system" || typed.role === "developer") {
|
|
370
|
+
const text = this.extractText(typed.content);
|
|
371
|
+
if (text)
|
|
372
|
+
systemParts.push({ text });
|
|
373
|
+
}
|
|
374
|
+
else if (typed.role === "user") {
|
|
375
|
+
const parts = this.convertInputContent(typed.content);
|
|
376
|
+
contents.push({ role: "user", parts });
|
|
377
|
+
}
|
|
378
|
+
else if (typed.role === "assistant") {
|
|
379
|
+
const parts = [];
|
|
380
|
+
if (Array.isArray(typed.content)) {
|
|
381
|
+
for (const p of typed.content) {
|
|
382
|
+
if (p.type === "output_text")
|
|
383
|
+
parts.push({ text: p.text });
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (parts.length > 0) {
|
|
387
|
+
contents.push({ role: "model", parts });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if (typed.type === "function_call") {
|
|
392
|
+
let args;
|
|
393
|
+
try {
|
|
394
|
+
args = JSON.parse(typed.arguments);
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
args = {};
|
|
398
|
+
}
|
|
399
|
+
pendingFunctionCalls.push({
|
|
400
|
+
functionCall: {
|
|
401
|
+
id: typed.call_id,
|
|
402
|
+
name: typed.name,
|
|
403
|
+
args,
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
else if (typed.type === "function_call_output") {
|
|
408
|
+
flushFunctionCalls();
|
|
409
|
+
const responsePart = {
|
|
410
|
+
functionResponse: {
|
|
411
|
+
id: typed.call_id,
|
|
412
|
+
name: typed.call_id,
|
|
413
|
+
response: { output: typed.output ?? "" },
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
const last = contents[contents.length - 1];
|
|
417
|
+
if (last && last.role === "user" && last.parts?.length) {
|
|
418
|
+
const lastPart = last.parts[last.parts.length - 1];
|
|
419
|
+
if (lastPart.functionResponse) {
|
|
420
|
+
last.parts.push(responsePart);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
contents.push({ role: "user", parts: [responsePart] });
|
|
425
|
+
}
|
|
426
|
+
else if ("role" in typed && "content" in typed && typeof typed.content === "string") {
|
|
427
|
+
flushFunctionCalls();
|
|
428
|
+
const role = typed.role;
|
|
429
|
+
if (role === "system" || role === "developer") {
|
|
430
|
+
systemParts.push({ text: typed.content });
|
|
431
|
+
}
|
|
432
|
+
else if (role === "user") {
|
|
433
|
+
contents.push({
|
|
434
|
+
role: "user",
|
|
435
|
+
parts: [{ text: typed.content }],
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
else if (role === "assistant") {
|
|
439
|
+
contents.push({
|
|
440
|
+
role: "model",
|
|
441
|
+
parts: [{ text: typed.content }],
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
flushFunctionCalls();
|
|
447
|
+
}
|
|
448
|
+
extractText(content) {
|
|
449
|
+
if (typeof content === "string")
|
|
450
|
+
return content;
|
|
451
|
+
if (Array.isArray(content)) {
|
|
452
|
+
return content
|
|
453
|
+
.filter((p) => p.type === "input_text")
|
|
454
|
+
.map((p) => p.text)
|
|
455
|
+
.join("\n");
|
|
456
|
+
}
|
|
457
|
+
return "";
|
|
458
|
+
}
|
|
459
|
+
convertInputContent(content) {
|
|
460
|
+
if (typeof content === "string") {
|
|
461
|
+
return [{ text: content }];
|
|
462
|
+
}
|
|
463
|
+
if (!Array.isArray(content))
|
|
464
|
+
return [{ text: "" }];
|
|
465
|
+
const parts = [];
|
|
466
|
+
for (const p of content) {
|
|
467
|
+
if (p.type === "input_text") {
|
|
468
|
+
parts.push({ text: p.text });
|
|
469
|
+
}
|
|
470
|
+
else if (p.type === "input_image") {
|
|
471
|
+
const url = p.image_url || "";
|
|
472
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
473
|
+
if (match) {
|
|
474
|
+
parts.push({
|
|
475
|
+
inlineData: { mimeType: match[1], data: match[2] },
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
parts.push({ fileData: { fileUri: url, mimeType: "image/*" } });
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else if (p.type === "input_file") {
|
|
483
|
+
const data = p.file_data || p.file_url || "";
|
|
484
|
+
const match = data.match(/^data:([^;]+);base64,(.+)$/);
|
|
485
|
+
if (match) {
|
|
486
|
+
parts.push({
|
|
487
|
+
inlineData: { mimeType: match[1], data: match[2] },
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
parts.push({
|
|
492
|
+
fileData: {
|
|
493
|
+
fileUri: data,
|
|
494
|
+
mimeType: "application/octet-stream",
|
|
495
|
+
},
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return parts.length > 0 ? parts : [{ text: "" }];
|
|
501
|
+
}
|
|
502
|
+
convertTools(tools) {
|
|
503
|
+
if (!tools)
|
|
504
|
+
return { tools: [], hasGoogleSearch: false };
|
|
505
|
+
const functionTools = [];
|
|
506
|
+
let hasGoogleSearch = false;
|
|
507
|
+
for (const t of tools) {
|
|
508
|
+
const tt = t;
|
|
509
|
+
if (tt.type === "function") {
|
|
510
|
+
functionTools.push({
|
|
511
|
+
name: tt.name,
|
|
512
|
+
description: tt.description,
|
|
513
|
+
parametersJsonSchema: tt.parameters,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
else if (this.isWebSearch(tt)) {
|
|
517
|
+
hasGoogleSearch = true;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return { tools: functionTools, hasGoogleSearch };
|
|
521
|
+
}
|
|
522
|
+
isWebSearch(tool) {
|
|
523
|
+
const t = tool.type;
|
|
524
|
+
return (t === "web_search" ||
|
|
525
|
+
t === "web_search_2025_08_26" ||
|
|
526
|
+
t === "web_search_preview" ||
|
|
527
|
+
t === "web_search_preview_2025_03_11");
|
|
528
|
+
}
|
|
529
|
+
convertToolChoice(choice) {
|
|
530
|
+
if (typeof choice === "string") {
|
|
531
|
+
switch (choice) {
|
|
532
|
+
case "auto":
|
|
533
|
+
return { mode: "AUTO" };
|
|
534
|
+
case "required":
|
|
535
|
+
return { mode: "ANY" };
|
|
536
|
+
case "none":
|
|
537
|
+
return { mode: "NONE" };
|
|
538
|
+
default:
|
|
539
|
+
return { mode: "AUTO" };
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (typeof choice === "object" && choice !== null) {
|
|
543
|
+
if (choice.type === "function" && choice.name) {
|
|
544
|
+
return {
|
|
545
|
+
mode: "ANY",
|
|
546
|
+
allowedFunctionNames: [choice.name],
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return { mode: "AUTO" };
|
|
551
|
+
}
|
|
552
|
+
convertReasoning(reasoning) {
|
|
553
|
+
if (!reasoning || !reasoning.effort) {
|
|
554
|
+
return { thinkingBudget: 0 };
|
|
555
|
+
}
|
|
556
|
+
const budgetMap = {
|
|
557
|
+
low: 2048,
|
|
558
|
+
medium: 5120,
|
|
559
|
+
high: 10240,
|
|
560
|
+
};
|
|
561
|
+
return {
|
|
562
|
+
includeThoughts: true,
|
|
563
|
+
thinkingBudget: budgetMap[reasoning.effort] ?? 10240,
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
applyTextFormat(config, format) {
|
|
567
|
+
if (format.type === "json_schema") {
|
|
568
|
+
config.responseMimeType = "application/json";
|
|
569
|
+
config.responseJsonSchema = format.schema;
|
|
570
|
+
}
|
|
571
|
+
else if (format.type === "json_object") {
|
|
572
|
+
config.responseMimeType = "application/json";
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
// --- Private: response helpers ---
|
|
576
|
+
finishReasonToStatus(reason, hasToolCall) {
|
|
577
|
+
if (hasToolCall)
|
|
578
|
+
return "completed";
|
|
579
|
+
switch (reason) {
|
|
580
|
+
case "STOP":
|
|
581
|
+
return "completed";
|
|
582
|
+
case "MAX_TOKENS":
|
|
583
|
+
return "incomplete";
|
|
584
|
+
case "SAFETY":
|
|
585
|
+
case "RECITATION":
|
|
586
|
+
case "BLOCKLIST":
|
|
587
|
+
case "PROHIBITED_CONTENT":
|
|
588
|
+
return "failed";
|
|
589
|
+
default:
|
|
590
|
+
return "completed";
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
extractGroundingAnnotations(candidate, annotations) {
|
|
594
|
+
if (!candidate?.groundingMetadata?.groundingChunks)
|
|
595
|
+
return;
|
|
596
|
+
for (const chunk of candidate.groundingMetadata.groundingChunks) {
|
|
597
|
+
const web = chunk.web;
|
|
598
|
+
if (web) {
|
|
599
|
+
annotations.push({
|
|
600
|
+
type: "url_citation",
|
|
601
|
+
url: web.uri || "",
|
|
602
|
+
title: web.title || "",
|
|
603
|
+
start_index: 0,
|
|
604
|
+
end_index: 0,
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
generateId() {
|
|
610
|
+
return Math.random().toString(36).substring(2, 15);
|
|
611
|
+
}
|
|
612
|
+
// --- Private: stream helpers ---
|
|
613
|
+
createStreamState() {
|
|
614
|
+
return {
|
|
615
|
+
id: "",
|
|
616
|
+
model: "",
|
|
617
|
+
seq: 0,
|
|
618
|
+
started: false,
|
|
619
|
+
outputIndex: 0,
|
|
620
|
+
reasoningStarted: false,
|
|
621
|
+
toolCallCount: 0,
|
|
622
|
+
textMessageStarted: false,
|
|
623
|
+
prevText: "",
|
|
624
|
+
prevThought: "",
|
|
625
|
+
seenFunctionCallIds: new Set(),
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
makeSkeletonResponse() {
|
|
629
|
+
return {
|
|
630
|
+
id: this.streamState.id,
|
|
631
|
+
object: "response",
|
|
632
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
633
|
+
model: this.streamState.model,
|
|
634
|
+
output: [],
|
|
635
|
+
status: "in_progress",
|
|
636
|
+
error: null,
|
|
637
|
+
incomplete_details: null,
|
|
638
|
+
instructions: null,
|
|
639
|
+
metadata: {},
|
|
640
|
+
temperature: null,
|
|
641
|
+
top_p: null,
|
|
642
|
+
max_output_tokens: null,
|
|
643
|
+
previous_response_id: null,
|
|
644
|
+
parallel_tool_calls: true,
|
|
645
|
+
tool_choice: "auto",
|
|
646
|
+
tools: [],
|
|
647
|
+
text: { format: { type: "text" } },
|
|
648
|
+
reasoning: null,
|
|
649
|
+
truncation: null,
|
|
650
|
+
user: undefined,
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
exports.ResponsesToGeminiConverter = ResponsesToGeminiConverter;
|